rsv.c 27 KB

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