rsv.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. * UWB reservation management.
  3. *
  4. * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/uwb.h>
  20. #include <linux/slab.h>
  21. #include <linux/random.h>
  22. #include <linux/export.h>
  23. #include "uwb-internal.h"
  24. static void uwb_rsv_timer(unsigned long arg);
  25. static const char *rsv_states[] = {
  26. [UWB_RSV_STATE_NONE] = "none ",
  27. [UWB_RSV_STATE_O_INITIATED] = "o initiated ",
  28. [UWB_RSV_STATE_O_PENDING] = "o pending ",
  29. [UWB_RSV_STATE_O_MODIFIED] = "o modified ",
  30. [UWB_RSV_STATE_O_ESTABLISHED] = "o established ",
  31. [UWB_RSV_STATE_O_TO_BE_MOVED] = "o to be moved ",
  32. [UWB_RSV_STATE_O_MOVE_EXPANDING] = "o move expanding",
  33. [UWB_RSV_STATE_O_MOVE_COMBINING] = "o move combining",
  34. [UWB_RSV_STATE_O_MOVE_REDUCING] = "o move reducing ",
  35. [UWB_RSV_STATE_T_ACCEPTED] = "t accepted ",
  36. [UWB_RSV_STATE_T_CONFLICT] = "t conflict ",
  37. [UWB_RSV_STATE_T_PENDING] = "t pending ",
  38. [UWB_RSV_STATE_T_DENIED] = "t denied ",
  39. [UWB_RSV_STATE_T_RESIZED] = "t resized ",
  40. [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = "t expanding acc ",
  41. [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = "t expanding conf",
  42. [UWB_RSV_STATE_T_EXPANDING_PENDING] = "t expanding pend",
  43. [UWB_RSV_STATE_T_EXPANDING_DENIED] = "t expanding den ",
  44. };
  45. static const char *rsv_types[] = {
  46. [UWB_DRP_TYPE_ALIEN_BP] = "alien-bp",
  47. [UWB_DRP_TYPE_HARD] = "hard",
  48. [UWB_DRP_TYPE_SOFT] = "soft",
  49. [UWB_DRP_TYPE_PRIVATE] = "private",
  50. [UWB_DRP_TYPE_PCA] = "pca",
  51. };
  52. bool uwb_rsv_has_two_drp_ies(struct uwb_rsv *rsv)
  53. {
  54. static const bool has_two_drp_ies[] = {
  55. [UWB_RSV_STATE_O_INITIATED] = false,
  56. [UWB_RSV_STATE_O_PENDING] = false,
  57. [UWB_RSV_STATE_O_MODIFIED] = false,
  58. [UWB_RSV_STATE_O_ESTABLISHED] = false,
  59. [UWB_RSV_STATE_O_TO_BE_MOVED] = false,
  60. [UWB_RSV_STATE_O_MOVE_COMBINING] = false,
  61. [UWB_RSV_STATE_O_MOVE_REDUCING] = false,
  62. [UWB_RSV_STATE_O_MOVE_EXPANDING] = true,
  63. [UWB_RSV_STATE_T_ACCEPTED] = false,
  64. [UWB_RSV_STATE_T_CONFLICT] = false,
  65. [UWB_RSV_STATE_T_PENDING] = false,
  66. [UWB_RSV_STATE_T_DENIED] = false,
  67. [UWB_RSV_STATE_T_RESIZED] = false,
  68. [UWB_RSV_STATE_T_EXPANDING_ACCEPTED] = true,
  69. [UWB_RSV_STATE_T_EXPANDING_CONFLICT] = true,
  70. [UWB_RSV_STATE_T_EXPANDING_PENDING] = true,
  71. [UWB_RSV_STATE_T_EXPANDING_DENIED] = true,
  72. };
  73. return has_two_drp_ies[rsv->state];
  74. }
  75. /**
  76. * uwb_rsv_state_str - return a string for a reservation state
  77. * @state: the reservation state.
  78. */
  79. const char *uwb_rsv_state_str(enum uwb_rsv_state state)
  80. {
  81. if (state < UWB_RSV_STATE_NONE || state >= UWB_RSV_STATE_LAST)
  82. return "unknown";
  83. return rsv_states[state];
  84. }
  85. EXPORT_SYMBOL_GPL(uwb_rsv_state_str);
  86. /**
  87. * uwb_rsv_type_str - return a string for a reservation type
  88. * @type: the reservation type
  89. */
  90. const char *uwb_rsv_type_str(enum uwb_drp_type type)
  91. {
  92. if (type < UWB_DRP_TYPE_ALIEN_BP || type > UWB_DRP_TYPE_PCA)
  93. return "invalid";
  94. return rsv_types[type];
  95. }
  96. EXPORT_SYMBOL_GPL(uwb_rsv_type_str);
  97. void uwb_rsv_dump(char *text, struct uwb_rsv *rsv)
  98. {
  99. struct device *dev = &rsv->rc->uwb_dev.dev;
  100. struct uwb_dev_addr devaddr;
  101. char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE];
  102. uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr);
  103. if (rsv->target.type == UWB_RSV_TARGET_DEV)
  104. devaddr = rsv->target.dev->dev_addr;
  105. else
  106. devaddr = rsv->target.devaddr;
  107. uwb_dev_addr_print(target, sizeof(target), &devaddr);
  108. dev_dbg(dev, "rsv %s %s -> %s: %s\n",
  109. text, owner, target, uwb_rsv_state_str(rsv->state));
  110. }
  111. static void uwb_rsv_release(struct kref *kref)
  112. {
  113. struct uwb_rsv *rsv = container_of(kref, struct uwb_rsv, kref);
  114. kfree(rsv);
  115. }
  116. void uwb_rsv_get(struct uwb_rsv *rsv)
  117. {
  118. kref_get(&rsv->kref);
  119. }
  120. void uwb_rsv_put(struct uwb_rsv *rsv)
  121. {
  122. kref_put(&rsv->kref, uwb_rsv_release);
  123. }
  124. /*
  125. * Get a free stream index for a reservation.
  126. *
  127. * If the target is a DevAddr (e.g., a WUSB cluster reservation) then
  128. * the stream is allocated from a pool of per-RC stream indexes,
  129. * otherwise a unique stream index for the target is selected.
  130. */
  131. static int uwb_rsv_get_stream(struct uwb_rsv *rsv)
  132. {
  133. struct uwb_rc *rc = rsv->rc;
  134. struct device *dev = &rc->uwb_dev.dev;
  135. unsigned long *streams_bm;
  136. int stream;
  137. switch (rsv->target.type) {
  138. case UWB_RSV_TARGET_DEV:
  139. streams_bm = rsv->target.dev->streams;
  140. break;
  141. case UWB_RSV_TARGET_DEVADDR:
  142. streams_bm = rc->uwb_dev.streams;
  143. break;
  144. default:
  145. return -EINVAL;
  146. }
  147. stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS);
  148. if (stream >= UWB_NUM_STREAMS) {
  149. dev_err(dev, "%s: no available stream found\n", __func__);
  150. return -EBUSY;
  151. }
  152. rsv->stream = stream;
  153. set_bit(stream, streams_bm);
  154. dev_dbg(dev, "get stream %d\n", rsv->stream);
  155. return 0;
  156. }
  157. static void uwb_rsv_put_stream(struct uwb_rsv *rsv)
  158. {
  159. struct uwb_rc *rc = rsv->rc;
  160. struct device *dev = &rc->uwb_dev.dev;
  161. unsigned long *streams_bm;
  162. switch (rsv->target.type) {
  163. case UWB_RSV_TARGET_DEV:
  164. streams_bm = rsv->target.dev->streams;
  165. break;
  166. case UWB_RSV_TARGET_DEVADDR:
  167. streams_bm = rc->uwb_dev.streams;
  168. break;
  169. default:
  170. return;
  171. }
  172. clear_bit(rsv->stream, streams_bm);
  173. dev_dbg(dev, "put stream %d\n", rsv->stream);
  174. }
  175. void uwb_rsv_backoff_win_timer(unsigned long arg)
  176. {
  177. struct uwb_drp_backoff_win *bow = (struct uwb_drp_backoff_win *)arg;
  178. struct uwb_rc *rc = container_of(bow, struct uwb_rc, bow);
  179. struct device *dev = &rc->uwb_dev.dev;
  180. bow->can_reserve_extra_mases = true;
  181. if (bow->total_expired <= 4) {
  182. bow->total_expired++;
  183. } else {
  184. /* after 4 backoff window has expired we can exit from
  185. * the backoff procedure */
  186. bow->total_expired = 0;
  187. bow->window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
  188. }
  189. dev_dbg(dev, "backoff_win_timer total_expired=%d, n=%d\n", bow->total_expired, bow->n);
  190. /* try to relocate all the "to be moved" relocations */
  191. uwb_rsv_handle_drp_avail_change(rc);
  192. }
  193. void uwb_rsv_backoff_win_increment(struct uwb_rc *rc)
  194. {
  195. struct uwb_drp_backoff_win *bow = &rc->bow;
  196. struct device *dev = &rc->uwb_dev.dev;
  197. unsigned timeout_us;
  198. dev_dbg(dev, "backoff_win_increment: window=%d\n", bow->window);
  199. bow->can_reserve_extra_mases = false;
  200. if((bow->window << 1) == UWB_DRP_BACKOFF_WIN_MAX)
  201. return;
  202. bow->window <<= 1;
  203. bow->n = prandom_u32() & (bow->window - 1);
  204. dev_dbg(dev, "new_window=%d, n=%d\n", bow->window, bow->n);
  205. /* reset the timer associated variables */
  206. timeout_us = bow->n * UWB_SUPERFRAME_LENGTH_US;
  207. bow->total_expired = 0;
  208. mod_timer(&bow->timer, jiffies + usecs_to_jiffies(timeout_us));
  209. }
  210. static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv)
  211. {
  212. int sframes = UWB_MAX_LOST_BEACONS;
  213. /*
  214. * Multicast reservations can become established within 1
  215. * super frame and should not be terminated if no response is
  216. * received.
  217. */
  218. if (rsv->state == UWB_RSV_STATE_NONE) {
  219. sframes = 0;
  220. } else if (rsv->is_multicast) {
  221. if (rsv->state == UWB_RSV_STATE_O_INITIATED
  222. || rsv->state == UWB_RSV_STATE_O_MOVE_EXPANDING
  223. || rsv->state == UWB_RSV_STATE_O_MOVE_COMBINING
  224. || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING)
  225. sframes = 1;
  226. if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED)
  227. sframes = 0;
  228. }
  229. if (sframes > 0) {
  230. /*
  231. * Add an additional 2 superframes to account for the
  232. * time to send the SET DRP IE command.
  233. */
  234. unsigned timeout_us = (sframes + 2) * UWB_SUPERFRAME_LENGTH_US;
  235. mod_timer(&rsv->timer, jiffies + usecs_to_jiffies(timeout_us));
  236. } else
  237. del_timer(&rsv->timer);
  238. }
  239. /*
  240. * Update a reservations state, and schedule an update of the
  241. * transmitted DRP IEs.
  242. */
  243. static void uwb_rsv_state_update(struct uwb_rsv *rsv,
  244. enum uwb_rsv_state new_state)
  245. {
  246. rsv->state = new_state;
  247. rsv->ie_valid = false;
  248. uwb_rsv_dump("SU", rsv);
  249. uwb_rsv_stroke_timer(rsv);
  250. uwb_rsv_sched_update(rsv->rc);
  251. }
  252. static void uwb_rsv_callback(struct uwb_rsv *rsv)
  253. {
  254. if (rsv->callback)
  255. rsv->callback(rsv);
  256. }
  257. void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state)
  258. {
  259. struct uwb_rsv_move *mv = &rsv->mv;
  260. if (rsv->state == new_state) {
  261. switch (rsv->state) {
  262. case UWB_RSV_STATE_O_ESTABLISHED:
  263. case UWB_RSV_STATE_O_MOVE_EXPANDING:
  264. case UWB_RSV_STATE_O_MOVE_COMBINING:
  265. case UWB_RSV_STATE_O_MOVE_REDUCING:
  266. case UWB_RSV_STATE_T_ACCEPTED:
  267. case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
  268. case UWB_RSV_STATE_T_RESIZED:
  269. case UWB_RSV_STATE_NONE:
  270. uwb_rsv_stroke_timer(rsv);
  271. break;
  272. default:
  273. /* Expecting a state transition so leave timer
  274. as-is. */
  275. break;
  276. }
  277. return;
  278. }
  279. uwb_rsv_dump("SC", rsv);
  280. switch (new_state) {
  281. case UWB_RSV_STATE_NONE:
  282. uwb_rsv_state_update(rsv, UWB_RSV_STATE_NONE);
  283. uwb_rsv_remove(rsv);
  284. uwb_rsv_callback(rsv);
  285. break;
  286. case UWB_RSV_STATE_O_INITIATED:
  287. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_INITIATED);
  288. break;
  289. case UWB_RSV_STATE_O_PENDING:
  290. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_PENDING);
  291. break;
  292. case UWB_RSV_STATE_O_MODIFIED:
  293. /* in the companion there are the MASes to drop */
  294. bitmap_andnot(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS);
  295. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MODIFIED);
  296. break;
  297. case UWB_RSV_STATE_O_ESTABLISHED:
  298. if (rsv->state == UWB_RSV_STATE_O_MODIFIED
  299. || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING) {
  300. uwb_drp_avail_release(rsv->rc, &mv->companion_mas);
  301. rsv->needs_release_companion_mas = false;
  302. }
  303. uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
  304. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_ESTABLISHED);
  305. uwb_rsv_callback(rsv);
  306. break;
  307. case UWB_RSV_STATE_O_MOVE_EXPANDING:
  308. rsv->needs_release_companion_mas = true;
  309. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING);
  310. break;
  311. case UWB_RSV_STATE_O_MOVE_COMBINING:
  312. rsv->needs_release_companion_mas = false;
  313. uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas);
  314. bitmap_or(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS);
  315. rsv->mas.safe += mv->companion_mas.safe;
  316. rsv->mas.unsafe += mv->companion_mas.unsafe;
  317. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_COMBINING);
  318. break;
  319. case UWB_RSV_STATE_O_MOVE_REDUCING:
  320. bitmap_andnot(mv->companion_mas.bm, rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS);
  321. rsv->needs_release_companion_mas = true;
  322. rsv->mas.safe = mv->final_mas.safe;
  323. rsv->mas.unsafe = mv->final_mas.unsafe;
  324. bitmap_copy(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS);
  325. bitmap_copy(rsv->mas.unsafe_bm, mv->final_mas.unsafe_bm, UWB_NUM_MAS);
  326. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_REDUCING);
  327. break;
  328. case UWB_RSV_STATE_T_ACCEPTED:
  329. case UWB_RSV_STATE_T_RESIZED:
  330. rsv->needs_release_companion_mas = false;
  331. uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
  332. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_ACCEPTED);
  333. uwb_rsv_callback(rsv);
  334. break;
  335. case UWB_RSV_STATE_T_DENIED:
  336. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_DENIED);
  337. break;
  338. case UWB_RSV_STATE_T_CONFLICT:
  339. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_CONFLICT);
  340. break;
  341. case UWB_RSV_STATE_T_PENDING:
  342. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_PENDING);
  343. break;
  344. case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
  345. rsv->needs_release_companion_mas = true;
  346. uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas);
  347. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_EXPANDING_ACCEPTED);
  348. break;
  349. default:
  350. dev_err(&rsv->rc->uwb_dev.dev, "unhandled state: %s (%d)\n",
  351. uwb_rsv_state_str(new_state), new_state);
  352. }
  353. }
  354. static void uwb_rsv_handle_timeout_work(struct work_struct *work)
  355. {
  356. struct uwb_rsv *rsv = container_of(work, struct uwb_rsv,
  357. handle_timeout_work);
  358. struct uwb_rc *rc = rsv->rc;
  359. mutex_lock(&rc->rsvs_mutex);
  360. uwb_rsv_dump("TO", rsv);
  361. switch (rsv->state) {
  362. case UWB_RSV_STATE_O_INITIATED:
  363. if (rsv->is_multicast) {
  364. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
  365. goto unlock;
  366. }
  367. break;
  368. case UWB_RSV_STATE_O_MOVE_EXPANDING:
  369. if (rsv->is_multicast) {
  370. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_COMBINING);
  371. goto unlock;
  372. }
  373. break;
  374. case UWB_RSV_STATE_O_MOVE_COMBINING:
  375. if (rsv->is_multicast) {
  376. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_REDUCING);
  377. goto unlock;
  378. }
  379. break;
  380. case UWB_RSV_STATE_O_MOVE_REDUCING:
  381. if (rsv->is_multicast) {
  382. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
  383. goto unlock;
  384. }
  385. break;
  386. case UWB_RSV_STATE_O_ESTABLISHED:
  387. if (rsv->is_multicast)
  388. goto unlock;
  389. break;
  390. case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
  391. /*
  392. * The time out could be for the main or of the
  393. * companion DRP, assume it's for the companion and
  394. * drop that first. A further time out is required to
  395. * drop the main.
  396. */
  397. uwb_rsv_set_state(rsv, UWB_RSV_STATE_T_ACCEPTED);
  398. uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
  399. goto unlock;
  400. case UWB_RSV_STATE_NONE:
  401. goto unlock;
  402. default:
  403. break;
  404. }
  405. uwb_rsv_remove(rsv);
  406. unlock:
  407. mutex_unlock(&rc->rsvs_mutex);
  408. }
  409. static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc)
  410. {
  411. struct uwb_rsv *rsv;
  412. rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL);
  413. if (!rsv)
  414. return NULL;
  415. INIT_LIST_HEAD(&rsv->rc_node);
  416. INIT_LIST_HEAD(&rsv->pal_node);
  417. kref_init(&rsv->kref);
  418. setup_timer(&rsv->timer, uwb_rsv_timer, (unsigned long)rsv);
  419. rsv->rc = rc;
  420. INIT_WORK(&rsv->handle_timeout_work, uwb_rsv_handle_timeout_work);
  421. return rsv;
  422. }
  423. /**
  424. * uwb_rsv_create - allocate and initialize a UWB reservation structure
  425. * @rc: the radio controller
  426. * @cb: callback to use when the reservation completes or terminates
  427. * @pal_priv: data private to the PAL to be passed in the callback
  428. *
  429. * The callback is called when the state of the reservation changes from:
  430. *
  431. * - pending to accepted
  432. * - pending to denined
  433. * - accepted to terminated
  434. * - pending to terminated
  435. */
  436. struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv)
  437. {
  438. struct uwb_rsv *rsv;
  439. rsv = uwb_rsv_alloc(rc);
  440. if (!rsv)
  441. return NULL;
  442. rsv->callback = cb;
  443. rsv->pal_priv = pal_priv;
  444. return rsv;
  445. }
  446. EXPORT_SYMBOL_GPL(uwb_rsv_create);
  447. void uwb_rsv_remove(struct uwb_rsv *rsv)
  448. {
  449. uwb_rsv_dump("RM", rsv);
  450. if (rsv->state != UWB_RSV_STATE_NONE)
  451. uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
  452. if (rsv->needs_release_companion_mas)
  453. uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
  454. uwb_drp_avail_release(rsv->rc, &rsv->mas);
  455. if (uwb_rsv_is_owner(rsv))
  456. uwb_rsv_put_stream(rsv);
  457. uwb_dev_put(rsv->owner);
  458. if (rsv->target.type == UWB_RSV_TARGET_DEV)
  459. uwb_dev_put(rsv->target.dev);
  460. list_del_init(&rsv->rc_node);
  461. uwb_rsv_put(rsv);
  462. }
  463. /**
  464. * uwb_rsv_destroy - free a UWB reservation structure
  465. * @rsv: the reservation to free
  466. *
  467. * The reservation must already be terminated.
  468. */
  469. void uwb_rsv_destroy(struct uwb_rsv *rsv)
  470. {
  471. uwb_rsv_put(rsv);
  472. }
  473. EXPORT_SYMBOL_GPL(uwb_rsv_destroy);
  474. /**
  475. * usb_rsv_establish - start a reservation establishment
  476. * @rsv: the reservation
  477. *
  478. * The PAL should fill in @rsv's owner, target, type, max_mas,
  479. * min_mas, max_interval and is_multicast fields. If the target is a
  480. * uwb_dev it must be referenced.
  481. *
  482. * The reservation's callback will be called when the reservation is
  483. * accepted, denied or times out.
  484. */
  485. int uwb_rsv_establish(struct uwb_rsv *rsv)
  486. {
  487. struct uwb_rc *rc = rsv->rc;
  488. struct uwb_mas_bm available;
  489. struct device *dev = &rc->uwb_dev.dev;
  490. int ret;
  491. mutex_lock(&rc->rsvs_mutex);
  492. ret = uwb_rsv_get_stream(rsv);
  493. if (ret) {
  494. dev_err(dev, "%s: uwb_rsv_get_stream failed: %d\n",
  495. __func__, ret);
  496. goto out;
  497. }
  498. rsv->tiebreaker = prandom_u32() & 1;
  499. /* get available mas bitmap */
  500. uwb_drp_available(rc, &available);
  501. ret = uwb_rsv_find_best_allocation(rsv, &available, &rsv->mas);
  502. if (ret == UWB_RSV_ALLOC_NOT_FOUND) {
  503. ret = -EBUSY;
  504. uwb_rsv_put_stream(rsv);
  505. dev_err(dev, "%s: uwb_rsv_find_best_allocation failed: %d\n",
  506. __func__, ret);
  507. goto out;
  508. }
  509. ret = uwb_drp_avail_reserve_pending(rc, &rsv->mas);
  510. if (ret != 0) {
  511. uwb_rsv_put_stream(rsv);
  512. dev_err(dev, "%s: uwb_drp_avail_reserve_pending failed: %d\n",
  513. __func__, ret);
  514. goto out;
  515. }
  516. uwb_rsv_get(rsv);
  517. list_add_tail(&rsv->rc_node, &rc->reservations);
  518. rsv->owner = &rc->uwb_dev;
  519. uwb_dev_get(rsv->owner);
  520. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_INITIATED);
  521. out:
  522. mutex_unlock(&rc->rsvs_mutex);
  523. return ret;
  524. }
  525. EXPORT_SYMBOL_GPL(uwb_rsv_establish);
  526. /**
  527. * uwb_rsv_modify - modify an already established reservation
  528. * @rsv: the reservation to modify
  529. * @max_mas: new maximum MAS to reserve
  530. * @min_mas: new minimum MAS to reserve
  531. * @max_interval: new max_interval to use
  532. *
  533. * FIXME: implement this once there are PALs that use it.
  534. */
  535. int uwb_rsv_modify(struct uwb_rsv *rsv, int max_mas, int min_mas, int max_interval)
  536. {
  537. return -ENOSYS;
  538. }
  539. EXPORT_SYMBOL_GPL(uwb_rsv_modify);
  540. /*
  541. * move an already established reservation (rc->rsvs_mutex must to be
  542. * taken when tis function is called)
  543. */
  544. int uwb_rsv_try_move(struct uwb_rsv *rsv, struct uwb_mas_bm *available)
  545. {
  546. struct uwb_rc *rc = rsv->rc;
  547. struct uwb_drp_backoff_win *bow = &rc->bow;
  548. struct device *dev = &rc->uwb_dev.dev;
  549. struct uwb_rsv_move *mv;
  550. int ret = 0;
  551. if (bow->can_reserve_extra_mases == false)
  552. return -EBUSY;
  553. mv = &rsv->mv;
  554. if (uwb_rsv_find_best_allocation(rsv, available, &mv->final_mas) == UWB_RSV_ALLOC_FOUND) {
  555. if (!bitmap_equal(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS)) {
  556. /* We want to move the reservation */
  557. bitmap_andnot(mv->companion_mas.bm, mv->final_mas.bm, rsv->mas.bm, UWB_NUM_MAS);
  558. uwb_drp_avail_reserve_pending(rc, &mv->companion_mas);
  559. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING);
  560. }
  561. } else {
  562. dev_dbg(dev, "new allocation not found\n");
  563. }
  564. return ret;
  565. }
  566. /* It will try to move every reservation in state O_ESTABLISHED giving
  567. * to the MAS allocator algorithm an availability that is the real one
  568. * plus the allocation already established from the reservation. */
  569. void uwb_rsv_handle_drp_avail_change(struct uwb_rc *rc)
  570. {
  571. struct uwb_drp_backoff_win *bow = &rc->bow;
  572. struct uwb_rsv *rsv;
  573. struct uwb_mas_bm mas;
  574. if (bow->can_reserve_extra_mases == false)
  575. return;
  576. list_for_each_entry(rsv, &rc->reservations, rc_node) {
  577. if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED ||
  578. rsv->state == UWB_RSV_STATE_O_TO_BE_MOVED) {
  579. uwb_drp_available(rc, &mas);
  580. bitmap_or(mas.bm, mas.bm, rsv->mas.bm, UWB_NUM_MAS);
  581. uwb_rsv_try_move(rsv, &mas);
  582. }
  583. }
  584. }
  585. /**
  586. * uwb_rsv_terminate - terminate an established reservation
  587. * @rsv: the reservation to terminate
  588. *
  589. * A reservation is terminated by removing the DRP IE from the beacon,
  590. * the other end will consider the reservation to be terminated when
  591. * it does not see the DRP IE for at least mMaxLostBeacons.
  592. *
  593. * If applicable, the reference to the target uwb_dev will be released.
  594. */
  595. void uwb_rsv_terminate(struct uwb_rsv *rsv)
  596. {
  597. struct uwb_rc *rc = rsv->rc;
  598. mutex_lock(&rc->rsvs_mutex);
  599. if (rsv->state != UWB_RSV_STATE_NONE)
  600. uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
  601. mutex_unlock(&rc->rsvs_mutex);
  602. }
  603. EXPORT_SYMBOL_GPL(uwb_rsv_terminate);
  604. /**
  605. * uwb_rsv_accept - accept a new reservation from a peer
  606. * @rsv: the reservation
  607. * @cb: call back for reservation changes
  608. * @pal_priv: data to be passed in the above call back
  609. *
  610. * Reservation requests from peers are denied unless a PAL accepts it
  611. * by calling this function.
  612. *
  613. * The PAL call uwb_rsv_destroy() for all accepted reservations before
  614. * calling uwb_pal_unregister().
  615. */
  616. void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv)
  617. {
  618. uwb_rsv_get(rsv);
  619. rsv->callback = cb;
  620. rsv->pal_priv = pal_priv;
  621. rsv->state = UWB_RSV_STATE_T_ACCEPTED;
  622. }
  623. EXPORT_SYMBOL_GPL(uwb_rsv_accept);
  624. /*
  625. * Is a received DRP IE for this reservation?
  626. */
  627. static bool uwb_rsv_match(struct uwb_rsv *rsv, struct uwb_dev *src,
  628. struct uwb_ie_drp *drp_ie)
  629. {
  630. struct uwb_dev_addr *rsv_src;
  631. int stream;
  632. stream = uwb_ie_drp_stream_index(drp_ie);
  633. if (rsv->stream != stream)
  634. return false;
  635. switch (rsv->target.type) {
  636. case UWB_RSV_TARGET_DEVADDR:
  637. return rsv->stream == stream;
  638. case UWB_RSV_TARGET_DEV:
  639. if (uwb_ie_drp_owner(drp_ie))
  640. rsv_src = &rsv->owner->dev_addr;
  641. else
  642. rsv_src = &rsv->target.dev->dev_addr;
  643. return uwb_dev_addr_cmp(&src->dev_addr, rsv_src) == 0;
  644. }
  645. return false;
  646. }
  647. static struct uwb_rsv *uwb_rsv_new_target(struct uwb_rc *rc,
  648. struct uwb_dev *src,
  649. struct uwb_ie_drp *drp_ie)
  650. {
  651. struct uwb_rsv *rsv;
  652. struct uwb_pal *pal;
  653. enum uwb_rsv_state state;
  654. rsv = uwb_rsv_alloc(rc);
  655. if (!rsv)
  656. return NULL;
  657. rsv->rc = rc;
  658. rsv->owner = src;
  659. uwb_dev_get(rsv->owner);
  660. rsv->target.type = UWB_RSV_TARGET_DEV;
  661. rsv->target.dev = &rc->uwb_dev;
  662. uwb_dev_get(&rc->uwb_dev);
  663. rsv->type = uwb_ie_drp_type(drp_ie);
  664. rsv->stream = uwb_ie_drp_stream_index(drp_ie);
  665. uwb_drp_ie_to_bm(&rsv->mas, drp_ie);
  666. /*
  667. * See if any PALs are interested in this reservation. If not,
  668. * deny the request.
  669. */
  670. rsv->state = UWB_RSV_STATE_T_DENIED;
  671. mutex_lock(&rc->uwb_dev.mutex);
  672. list_for_each_entry(pal, &rc->pals, node) {
  673. if (pal->new_rsv)
  674. pal->new_rsv(pal, rsv);
  675. if (rsv->state == UWB_RSV_STATE_T_ACCEPTED)
  676. break;
  677. }
  678. mutex_unlock(&rc->uwb_dev.mutex);
  679. list_add_tail(&rsv->rc_node, &rc->reservations);
  680. state = rsv->state;
  681. rsv->state = UWB_RSV_STATE_NONE;
  682. /* FIXME: do something sensible here */
  683. if (state == UWB_RSV_STATE_T_ACCEPTED
  684. && uwb_drp_avail_reserve_pending(rc, &rsv->mas) == -EBUSY) {
  685. /* FIXME: do something sensible here */
  686. } else {
  687. uwb_rsv_set_state(rsv, state);
  688. }
  689. return rsv;
  690. }
  691. /**
  692. * uwb_rsv_get_usable_mas - get the bitmap of the usable MAS of a reservations
  693. * @rsv: the reservation.
  694. * @mas: returns the available MAS.
  695. *
  696. * The usable MAS of a reservation may be less than the negotiated MAS
  697. * if alien BPs are present.
  698. */
  699. void uwb_rsv_get_usable_mas(struct uwb_rsv *rsv, struct uwb_mas_bm *mas)
  700. {
  701. bitmap_zero(mas->bm, UWB_NUM_MAS);
  702. bitmap_andnot(mas->bm, rsv->mas.bm, rsv->rc->cnflt_alien_bitmap.bm, UWB_NUM_MAS);
  703. }
  704. EXPORT_SYMBOL_GPL(uwb_rsv_get_usable_mas);
  705. /**
  706. * uwb_rsv_find - find a reservation for a received DRP IE.
  707. * @rc: the radio controller
  708. * @src: source of the DRP IE
  709. * @drp_ie: the DRP IE
  710. *
  711. * If the reservation cannot be found and the DRP IE is from a peer
  712. * attempting to establish a new reservation, create a new reservation
  713. * and add it to the list.
  714. */
  715. struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src,
  716. struct uwb_ie_drp *drp_ie)
  717. {
  718. struct uwb_rsv *rsv;
  719. list_for_each_entry(rsv, &rc->reservations, rc_node) {
  720. if (uwb_rsv_match(rsv, src, drp_ie))
  721. return rsv;
  722. }
  723. if (uwb_ie_drp_owner(drp_ie))
  724. return uwb_rsv_new_target(rc, src, drp_ie);
  725. return NULL;
  726. }
  727. /*
  728. * Go through all the reservations and check for timeouts and (if
  729. * necessary) update their DRP IEs.
  730. *
  731. * FIXME: look at building the SET_DRP_IE command here rather than
  732. * having to rescan the list in uwb_rc_send_all_drp_ie().
  733. */
  734. static bool uwb_rsv_update_all(struct uwb_rc *rc)
  735. {
  736. struct uwb_rsv *rsv, *t;
  737. bool ie_updated = false;
  738. list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
  739. if (!rsv->ie_valid) {
  740. uwb_drp_ie_update(rsv);
  741. ie_updated = true;
  742. }
  743. }
  744. return ie_updated;
  745. }
  746. void uwb_rsv_queue_update(struct uwb_rc *rc)
  747. {
  748. unsigned long delay_us = UWB_MAS_LENGTH_US * UWB_MAS_PER_ZONE;
  749. queue_delayed_work(rc->rsv_workq, &rc->rsv_update_work, usecs_to_jiffies(delay_us));
  750. }
  751. /**
  752. * uwb_rsv_sched_update - schedule an update of the DRP IEs
  753. * @rc: the radio controller.
  754. *
  755. * To improve performance and ensure correctness with [ECMA-368] the
  756. * number of SET-DRP-IE commands that are done are limited.
  757. *
  758. * DRP IEs update come from two sources: DRP events from the hardware
  759. * which all occur at the beginning of the superframe ('syncronous'
  760. * events) and reservation establishment/termination requests from
  761. * PALs or timers ('asynchronous' events).
  762. *
  763. * A delayed work ensures that all the synchronous events result in
  764. * one SET-DRP-IE command.
  765. *
  766. * Additional logic (the set_drp_ie_pending and rsv_updated_postponed
  767. * flags) will prevent an asynchrous event starting a SET-DRP-IE
  768. * command if one is currently awaiting a response.
  769. *
  770. * FIXME: this does leave a window where an asynchrous event can delay
  771. * the SET-DRP-IE for a synchronous event by one superframe.
  772. */
  773. void uwb_rsv_sched_update(struct uwb_rc *rc)
  774. {
  775. spin_lock_irq(&rc->rsvs_lock);
  776. if (!delayed_work_pending(&rc->rsv_update_work)) {
  777. if (rc->set_drp_ie_pending > 0) {
  778. rc->set_drp_ie_pending++;
  779. goto unlock;
  780. }
  781. uwb_rsv_queue_update(rc);
  782. }
  783. unlock:
  784. spin_unlock_irq(&rc->rsvs_lock);
  785. }
  786. /*
  787. * Update DRP IEs and, if necessary, the DRP Availability IE and send
  788. * the updated IEs to the radio controller.
  789. */
  790. static void uwb_rsv_update_work(struct work_struct *work)
  791. {
  792. struct uwb_rc *rc = container_of(work, struct uwb_rc,
  793. rsv_update_work.work);
  794. bool ie_updated;
  795. mutex_lock(&rc->rsvs_mutex);
  796. ie_updated = uwb_rsv_update_all(rc);
  797. if (!rc->drp_avail.ie_valid) {
  798. uwb_drp_avail_ie_update(rc);
  799. ie_updated = true;
  800. }
  801. if (ie_updated && (rc->set_drp_ie_pending == 0))
  802. uwb_rc_send_all_drp_ie(rc);
  803. mutex_unlock(&rc->rsvs_mutex);
  804. }
  805. static void uwb_rsv_alien_bp_work(struct work_struct *work)
  806. {
  807. struct uwb_rc *rc = container_of(work, struct uwb_rc,
  808. rsv_alien_bp_work.work);
  809. struct uwb_rsv *rsv;
  810. mutex_lock(&rc->rsvs_mutex);
  811. list_for_each_entry(rsv, &rc->reservations, rc_node) {
  812. if (rsv->type != UWB_DRP_TYPE_ALIEN_BP) {
  813. uwb_rsv_callback(rsv);
  814. }
  815. }
  816. mutex_unlock(&rc->rsvs_mutex);
  817. }
  818. static void uwb_rsv_timer(unsigned long arg)
  819. {
  820. struct uwb_rsv *rsv = (struct uwb_rsv *)arg;
  821. queue_work(rsv->rc->rsv_workq, &rsv->handle_timeout_work);
  822. }
  823. /**
  824. * uwb_rsv_remove_all - remove all reservations
  825. * @rc: the radio controller
  826. *
  827. * A DRP IE update is not done.
  828. */
  829. void uwb_rsv_remove_all(struct uwb_rc *rc)
  830. {
  831. struct uwb_rsv *rsv, *t;
  832. mutex_lock(&rc->rsvs_mutex);
  833. list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
  834. if (rsv->state != UWB_RSV_STATE_NONE)
  835. uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
  836. del_timer_sync(&rsv->timer);
  837. }
  838. /* Cancel any postponed update. */
  839. rc->set_drp_ie_pending = 0;
  840. mutex_unlock(&rc->rsvs_mutex);
  841. cancel_delayed_work_sync(&rc->rsv_update_work);
  842. flush_workqueue(rc->rsv_workq);
  843. mutex_lock(&rc->rsvs_mutex);
  844. list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
  845. uwb_rsv_remove(rsv);
  846. }
  847. mutex_unlock(&rc->rsvs_mutex);
  848. }
  849. void uwb_rsv_init(struct uwb_rc *rc)
  850. {
  851. INIT_LIST_HEAD(&rc->reservations);
  852. INIT_LIST_HEAD(&rc->cnflt_alien_list);
  853. mutex_init(&rc->rsvs_mutex);
  854. spin_lock_init(&rc->rsvs_lock);
  855. INIT_DELAYED_WORK(&rc->rsv_update_work, uwb_rsv_update_work);
  856. INIT_DELAYED_WORK(&rc->rsv_alien_bp_work, uwb_rsv_alien_bp_work);
  857. rc->bow.can_reserve_extra_mases = true;
  858. rc->bow.total_expired = 0;
  859. rc->bow.window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
  860. setup_timer(&rc->bow.timer, uwb_rsv_backoff_win_timer,
  861. (unsigned long)&rc->bow);
  862. bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS);
  863. }
  864. int uwb_rsv_setup(struct uwb_rc *rc)
  865. {
  866. char name[16];
  867. snprintf(name, sizeof(name), "%s_rsvd", dev_name(&rc->uwb_dev.dev));
  868. rc->rsv_workq = create_singlethread_workqueue(name);
  869. if (rc->rsv_workq == NULL)
  870. return -ENOMEM;
  871. return 0;
  872. }
  873. void uwb_rsv_cleanup(struct uwb_rc *rc)
  874. {
  875. uwb_rsv_remove_all(rc);
  876. destroy_workqueue(rc->rsv_workq);
  877. }