rsv.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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/version.h>
  19. #include <linux/kernel.h>
  20. #include <linux/uwb.h>
  21. #include "uwb-internal.h"
  22. static void uwb_rsv_timer(unsigned long arg);
  23. static const char *rsv_states[] = {
  24. [UWB_RSV_STATE_NONE] = "none",
  25. [UWB_RSV_STATE_O_INITIATED] = "initiated",
  26. [UWB_RSV_STATE_O_PENDING] = "pending",
  27. [UWB_RSV_STATE_O_MODIFIED] = "modified",
  28. [UWB_RSV_STATE_O_ESTABLISHED] = "established",
  29. [UWB_RSV_STATE_T_ACCEPTED] = "accepted",
  30. [UWB_RSV_STATE_T_DENIED] = "denied",
  31. [UWB_RSV_STATE_T_PENDING] = "pending",
  32. };
  33. static const char *rsv_types[] = {
  34. [UWB_DRP_TYPE_ALIEN_BP] = "alien-bp",
  35. [UWB_DRP_TYPE_HARD] = "hard",
  36. [UWB_DRP_TYPE_SOFT] = "soft",
  37. [UWB_DRP_TYPE_PRIVATE] = "private",
  38. [UWB_DRP_TYPE_PCA] = "pca",
  39. };
  40. /**
  41. * uwb_rsv_state_str - return a string for a reservation state
  42. * @state: the reservation state.
  43. */
  44. const char *uwb_rsv_state_str(enum uwb_rsv_state state)
  45. {
  46. if (state < UWB_RSV_STATE_NONE || state >= UWB_RSV_STATE_LAST)
  47. return "unknown";
  48. return rsv_states[state];
  49. }
  50. EXPORT_SYMBOL_GPL(uwb_rsv_state_str);
  51. /**
  52. * uwb_rsv_type_str - return a string for a reservation type
  53. * @type: the reservation type
  54. */
  55. const char *uwb_rsv_type_str(enum uwb_drp_type type)
  56. {
  57. if (type < UWB_DRP_TYPE_ALIEN_BP || type > UWB_DRP_TYPE_PCA)
  58. return "invalid";
  59. return rsv_types[type];
  60. }
  61. EXPORT_SYMBOL_GPL(uwb_rsv_type_str);
  62. static void uwb_rsv_dump(struct uwb_rsv *rsv)
  63. {
  64. struct device *dev = &rsv->rc->uwb_dev.dev;
  65. struct uwb_dev_addr devaddr;
  66. char owner[UWB_ADDR_STRSIZE], target[UWB_ADDR_STRSIZE];
  67. uwb_dev_addr_print(owner, sizeof(owner), &rsv->owner->dev_addr);
  68. if (rsv->target.type == UWB_RSV_TARGET_DEV)
  69. devaddr = rsv->target.dev->dev_addr;
  70. else
  71. devaddr = rsv->target.devaddr;
  72. uwb_dev_addr_print(target, sizeof(target), &devaddr);
  73. dev_dbg(dev, "rsv %s -> %s: %s\n", owner, target, uwb_rsv_state_str(rsv->state));
  74. }
  75. /*
  76. * Get a free stream index for a reservation.
  77. *
  78. * If the target is a DevAddr (e.g., a WUSB cluster reservation) then
  79. * the stream is allocated from a pool of per-RC stream indexes,
  80. * otherwise a unique stream index for the target is selected.
  81. */
  82. static int uwb_rsv_get_stream(struct uwb_rsv *rsv)
  83. {
  84. struct uwb_rc *rc = rsv->rc;
  85. unsigned long *streams_bm;
  86. int stream;
  87. switch (rsv->target.type) {
  88. case UWB_RSV_TARGET_DEV:
  89. streams_bm = rsv->target.dev->streams;
  90. break;
  91. case UWB_RSV_TARGET_DEVADDR:
  92. streams_bm = rc->uwb_dev.streams;
  93. break;
  94. default:
  95. return -EINVAL;
  96. }
  97. stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS);
  98. if (stream >= UWB_NUM_STREAMS)
  99. return -EBUSY;
  100. rsv->stream = stream;
  101. set_bit(stream, streams_bm);
  102. return 0;
  103. }
  104. static void uwb_rsv_put_stream(struct uwb_rsv *rsv)
  105. {
  106. struct uwb_rc *rc = rsv->rc;
  107. unsigned long *streams_bm;
  108. switch (rsv->target.type) {
  109. case UWB_RSV_TARGET_DEV:
  110. streams_bm = rsv->target.dev->streams;
  111. break;
  112. case UWB_RSV_TARGET_DEVADDR:
  113. streams_bm = rc->uwb_dev.streams;
  114. break;
  115. default:
  116. return;
  117. }
  118. clear_bit(rsv->stream, streams_bm);
  119. }
  120. /*
  121. * Generate a MAS allocation with a single row component.
  122. */
  123. static void uwb_rsv_gen_alloc_row(struct uwb_mas_bm *mas,
  124. int first_mas, int mas_per_zone,
  125. int zs, int ze)
  126. {
  127. struct uwb_mas_bm col;
  128. int z;
  129. bitmap_zero(mas->bm, UWB_NUM_MAS);
  130. bitmap_zero(col.bm, UWB_NUM_MAS);
  131. bitmap_fill(col.bm, mas_per_zone);
  132. bitmap_shift_left(col.bm, col.bm, first_mas + zs * UWB_MAS_PER_ZONE, UWB_NUM_MAS);
  133. for (z = zs; z <= ze; z++) {
  134. bitmap_or(mas->bm, mas->bm, col.bm, UWB_NUM_MAS);
  135. bitmap_shift_left(col.bm, col.bm, UWB_MAS_PER_ZONE, UWB_NUM_MAS);
  136. }
  137. }
  138. /*
  139. * Allocate some MAS for this reservation based on current local
  140. * availability, the reservation parameters (max_mas, min_mas,
  141. * sparsity), and the WiMedia rules for MAS allocations.
  142. *
  143. * Returns -EBUSY is insufficient free MAS are available.
  144. *
  145. * FIXME: to simplify this, only safe reservations with a single row
  146. * component in zones 1 to 15 are tried (zone 0 is skipped to avoid
  147. * problems with the MAS reserved for the BP).
  148. *
  149. * [ECMA-368] section B.2.
  150. */
  151. static int uwb_rsv_alloc_mas(struct uwb_rsv *rsv)
  152. {
  153. static const int safe_mas_in_row[UWB_NUM_ZONES] = {
  154. 8, 7, 6, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 2, 1,
  155. };
  156. int n, r;
  157. struct uwb_mas_bm mas;
  158. bool found = false;
  159. /*
  160. * Search all valid safe allocations until either: too few MAS
  161. * are available; or the smallest allocation with sufficient
  162. * MAS is found.
  163. *
  164. * The top of the zones are preferred, so space for larger
  165. * allocations is available in the bottom of the zone (e.g., a
  166. * 15 MAS allocation should start in row 14 leaving space for
  167. * a 120 MAS allocation at row 0).
  168. */
  169. for (n = safe_mas_in_row[0]; n >= 1; n--) {
  170. int num_mas;
  171. num_mas = n * (UWB_NUM_ZONES - 1);
  172. if (num_mas < rsv->min_mas)
  173. break;
  174. if (found && num_mas < rsv->max_mas)
  175. break;
  176. for (r = UWB_MAS_PER_ZONE-1; r >= 0; r--) {
  177. if (safe_mas_in_row[r] < n)
  178. continue;
  179. uwb_rsv_gen_alloc_row(&mas, r, n, 1, UWB_NUM_ZONES);
  180. if (uwb_drp_avail_reserve_pending(rsv->rc, &mas) == 0) {
  181. found = true;
  182. break;
  183. }
  184. }
  185. }
  186. if (!found)
  187. return -EBUSY;
  188. bitmap_copy(rsv->mas.bm, mas.bm, UWB_NUM_MAS);
  189. return 0;
  190. }
  191. static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv)
  192. {
  193. int sframes = UWB_MAX_LOST_BEACONS;
  194. /*
  195. * Multicast reservations can become established within 1
  196. * super frame and should not be terminated if no response is
  197. * received.
  198. */
  199. if (rsv->is_multicast) {
  200. if (rsv->state == UWB_RSV_STATE_O_INITIATED)
  201. sframes = 1;
  202. if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED)
  203. sframes = 0;
  204. }
  205. rsv->expired = false;
  206. if (sframes > 0) {
  207. /*
  208. * Add an additional 2 superframes to account for the
  209. * time to send the SET DRP IE command.
  210. */
  211. unsigned timeout_us = (sframes + 2) * UWB_SUPERFRAME_LENGTH_US;
  212. mod_timer(&rsv->timer, jiffies + usecs_to_jiffies(timeout_us));
  213. } else
  214. del_timer(&rsv->timer);
  215. }
  216. /*
  217. * Update a reservations state, and schedule an update of the
  218. * transmitted DRP IEs.
  219. */
  220. static void uwb_rsv_state_update(struct uwb_rsv *rsv,
  221. enum uwb_rsv_state new_state)
  222. {
  223. rsv->state = new_state;
  224. rsv->ie_valid = false;
  225. uwb_rsv_dump(rsv);
  226. uwb_rsv_stroke_timer(rsv);
  227. uwb_rsv_sched_update(rsv->rc);
  228. }
  229. static void uwb_rsv_callback(struct uwb_rsv *rsv)
  230. {
  231. if (rsv->callback)
  232. rsv->callback(rsv);
  233. }
  234. void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state)
  235. {
  236. if (rsv->state == new_state) {
  237. switch (rsv->state) {
  238. case UWB_RSV_STATE_O_ESTABLISHED:
  239. case UWB_RSV_STATE_T_ACCEPTED:
  240. case UWB_RSV_STATE_NONE:
  241. uwb_rsv_stroke_timer(rsv);
  242. break;
  243. default:
  244. /* Expecting a state transition so leave timer
  245. as-is. */
  246. break;
  247. }
  248. return;
  249. }
  250. switch (new_state) {
  251. case UWB_RSV_STATE_NONE:
  252. uwb_drp_avail_release(rsv->rc, &rsv->mas);
  253. uwb_rsv_put_stream(rsv);
  254. uwb_rsv_state_update(rsv, UWB_RSV_STATE_NONE);
  255. uwb_rsv_callback(rsv);
  256. break;
  257. case UWB_RSV_STATE_O_INITIATED:
  258. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_INITIATED);
  259. break;
  260. case UWB_RSV_STATE_O_PENDING:
  261. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_PENDING);
  262. break;
  263. case UWB_RSV_STATE_O_ESTABLISHED:
  264. uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
  265. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_ESTABLISHED);
  266. uwb_rsv_callback(rsv);
  267. break;
  268. case UWB_RSV_STATE_T_ACCEPTED:
  269. uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
  270. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_ACCEPTED);
  271. uwb_rsv_callback(rsv);
  272. break;
  273. case UWB_RSV_STATE_T_DENIED:
  274. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_DENIED);
  275. break;
  276. default:
  277. dev_err(&rsv->rc->uwb_dev.dev, "unhandled state: %s (%d)\n",
  278. uwb_rsv_state_str(new_state), new_state);
  279. }
  280. }
  281. static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc)
  282. {
  283. struct uwb_rsv *rsv;
  284. rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL);
  285. if (!rsv)
  286. return NULL;
  287. INIT_LIST_HEAD(&rsv->rc_node);
  288. INIT_LIST_HEAD(&rsv->pal_node);
  289. init_timer(&rsv->timer);
  290. rsv->timer.function = uwb_rsv_timer;
  291. rsv->timer.data = (unsigned long)rsv;
  292. rsv->rc = rc;
  293. return rsv;
  294. }
  295. static void uwb_rsv_free(struct uwb_rsv *rsv)
  296. {
  297. uwb_dev_put(rsv->owner);
  298. if (rsv->target.type == UWB_RSV_TARGET_DEV)
  299. uwb_dev_put(rsv->target.dev);
  300. kfree(rsv);
  301. }
  302. /**
  303. * uwb_rsv_create - allocate and initialize a UWB reservation structure
  304. * @rc: the radio controller
  305. * @cb: callback to use when the reservation completes or terminates
  306. * @pal_priv: data private to the PAL to be passed in the callback
  307. *
  308. * The callback is called when the state of the reservation changes from:
  309. *
  310. * - pending to accepted
  311. * - pending to denined
  312. * - accepted to terminated
  313. * - pending to terminated
  314. */
  315. struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv)
  316. {
  317. struct uwb_rsv *rsv;
  318. rsv = uwb_rsv_alloc(rc);
  319. if (!rsv)
  320. return NULL;
  321. rsv->callback = cb;
  322. rsv->pal_priv = pal_priv;
  323. return rsv;
  324. }
  325. EXPORT_SYMBOL_GPL(uwb_rsv_create);
  326. void uwb_rsv_remove(struct uwb_rsv *rsv)
  327. {
  328. if (rsv->state != UWB_RSV_STATE_NONE)
  329. uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
  330. del_timer_sync(&rsv->timer);
  331. list_del(&rsv->rc_node);
  332. uwb_rsv_free(rsv);
  333. }
  334. /**
  335. * uwb_rsv_destroy - free a UWB reservation structure
  336. * @rsv: the reservation to free
  337. *
  338. * The reservation will be terminated if it is pending or established.
  339. */
  340. void uwb_rsv_destroy(struct uwb_rsv *rsv)
  341. {
  342. struct uwb_rc *rc = rsv->rc;
  343. mutex_lock(&rc->rsvs_mutex);
  344. uwb_rsv_remove(rsv);
  345. mutex_unlock(&rc->rsvs_mutex);
  346. }
  347. EXPORT_SYMBOL_GPL(uwb_rsv_destroy);
  348. /**
  349. * usb_rsv_establish - start a reservation establishment
  350. * @rsv: the reservation
  351. *
  352. * The PAL should fill in @rsv's owner, target, type, max_mas,
  353. * min_mas, sparsity and is_multicast fields. If the target is a
  354. * uwb_dev it must be referenced.
  355. *
  356. * The reservation's callback will be called when the reservation is
  357. * accepted, denied or times out.
  358. */
  359. int uwb_rsv_establish(struct uwb_rsv *rsv)
  360. {
  361. struct uwb_rc *rc = rsv->rc;
  362. int ret;
  363. mutex_lock(&rc->rsvs_mutex);
  364. ret = uwb_rsv_get_stream(rsv);
  365. if (ret)
  366. goto out;
  367. ret = uwb_rsv_alloc_mas(rsv);
  368. if (ret) {
  369. uwb_rsv_put_stream(rsv);
  370. goto out;
  371. }
  372. list_add_tail(&rsv->rc_node, &rc->reservations);
  373. rsv->owner = &rc->uwb_dev;
  374. uwb_dev_get(rsv->owner);
  375. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_INITIATED);
  376. out:
  377. mutex_unlock(&rc->rsvs_mutex);
  378. return ret;
  379. }
  380. EXPORT_SYMBOL_GPL(uwb_rsv_establish);
  381. /**
  382. * uwb_rsv_modify - modify an already established reservation
  383. * @rsv: the reservation to modify
  384. * @max_mas: new maximum MAS to reserve
  385. * @min_mas: new minimum MAS to reserve
  386. * @sparsity: new sparsity to use
  387. *
  388. * FIXME: implement this once there are PALs that use it.
  389. */
  390. int uwb_rsv_modify(struct uwb_rsv *rsv, int max_mas, int min_mas, int sparsity)
  391. {
  392. return -ENOSYS;
  393. }
  394. EXPORT_SYMBOL_GPL(uwb_rsv_modify);
  395. /**
  396. * uwb_rsv_terminate - terminate an established reservation
  397. * @rsv: the reservation to terminate
  398. *
  399. * A reservation is terminated by removing the DRP IE from the beacon,
  400. * the other end will consider the reservation to be terminated when
  401. * it does not see the DRP IE for at least mMaxLostBeacons.
  402. *
  403. * If applicable, the reference to the target uwb_dev will be released.
  404. */
  405. void uwb_rsv_terminate(struct uwb_rsv *rsv)
  406. {
  407. struct uwb_rc *rc = rsv->rc;
  408. mutex_lock(&rc->rsvs_mutex);
  409. uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
  410. mutex_unlock(&rc->rsvs_mutex);
  411. }
  412. EXPORT_SYMBOL_GPL(uwb_rsv_terminate);
  413. /**
  414. * uwb_rsv_accept - accept a new reservation from a peer
  415. * @rsv: the reservation
  416. * @cb: call back for reservation changes
  417. * @pal_priv: data to be passed in the above call back
  418. *
  419. * Reservation requests from peers are denied unless a PAL accepts it
  420. * by calling this function.
  421. */
  422. void uwb_rsv_accept(struct uwb_rsv *rsv, uwb_rsv_cb_f cb, void *pal_priv)
  423. {
  424. rsv->callback = cb;
  425. rsv->pal_priv = pal_priv;
  426. rsv->state = UWB_RSV_STATE_T_ACCEPTED;
  427. }
  428. EXPORT_SYMBOL_GPL(uwb_rsv_accept);
  429. /*
  430. * Is a received DRP IE for this reservation?
  431. */
  432. static bool uwb_rsv_match(struct uwb_rsv *rsv, struct uwb_dev *src,
  433. struct uwb_ie_drp *drp_ie)
  434. {
  435. struct uwb_dev_addr *rsv_src;
  436. int stream;
  437. stream = uwb_ie_drp_stream_index(drp_ie);
  438. if (rsv->stream != stream)
  439. return false;
  440. switch (rsv->target.type) {
  441. case UWB_RSV_TARGET_DEVADDR:
  442. return rsv->stream == stream;
  443. case UWB_RSV_TARGET_DEV:
  444. if (uwb_ie_drp_owner(drp_ie))
  445. rsv_src = &rsv->owner->dev_addr;
  446. else
  447. rsv_src = &rsv->target.dev->dev_addr;
  448. return uwb_dev_addr_cmp(&src->dev_addr, rsv_src) == 0;
  449. }
  450. return false;
  451. }
  452. static struct uwb_rsv *uwb_rsv_new_target(struct uwb_rc *rc,
  453. struct uwb_dev *src,
  454. struct uwb_ie_drp *drp_ie)
  455. {
  456. struct uwb_rsv *rsv;
  457. struct uwb_pal *pal;
  458. enum uwb_rsv_state state;
  459. rsv = uwb_rsv_alloc(rc);
  460. if (!rsv)
  461. return NULL;
  462. rsv->rc = rc;
  463. rsv->owner = src;
  464. uwb_dev_get(rsv->owner);
  465. rsv->target.type = UWB_RSV_TARGET_DEV;
  466. rsv->target.dev = &rc->uwb_dev;
  467. rsv->type = uwb_ie_drp_type(drp_ie);
  468. rsv->stream = uwb_ie_drp_stream_index(drp_ie);
  469. set_bit(rsv->stream, rsv->owner->streams);
  470. uwb_drp_ie_to_bm(&rsv->mas, drp_ie);
  471. /*
  472. * See if any PALs are interested in this reservation. If not,
  473. * deny the request.
  474. */
  475. rsv->state = UWB_RSV_STATE_T_DENIED;
  476. spin_lock(&rc->pal_lock);
  477. list_for_each_entry(pal, &rc->pals, node) {
  478. if (pal->new_rsv)
  479. pal->new_rsv(rsv);
  480. if (rsv->state == UWB_RSV_STATE_T_ACCEPTED)
  481. break;
  482. }
  483. spin_unlock(&rc->pal_lock);
  484. list_add_tail(&rsv->rc_node, &rc->reservations);
  485. state = rsv->state;
  486. rsv->state = UWB_RSV_STATE_NONE;
  487. uwb_rsv_set_state(rsv, state);
  488. return rsv;
  489. }
  490. /**
  491. * uwb_rsv_find - find a reservation for a received DRP IE.
  492. * @rc: the radio controller
  493. * @src: source of the DRP IE
  494. * @drp_ie: the DRP IE
  495. *
  496. * If the reservation cannot be found and the DRP IE is from a peer
  497. * attempting to establish a new reservation, create a new reservation
  498. * and add it to the list.
  499. */
  500. struct uwb_rsv *uwb_rsv_find(struct uwb_rc *rc, struct uwb_dev *src,
  501. struct uwb_ie_drp *drp_ie)
  502. {
  503. struct uwb_rsv *rsv;
  504. list_for_each_entry(rsv, &rc->reservations, rc_node) {
  505. if (uwb_rsv_match(rsv, src, drp_ie))
  506. return rsv;
  507. }
  508. if (uwb_ie_drp_owner(drp_ie))
  509. return uwb_rsv_new_target(rc, src, drp_ie);
  510. return NULL;
  511. }
  512. /*
  513. * Go through all the reservations and check for timeouts and (if
  514. * necessary) update their DRP IEs.
  515. *
  516. * FIXME: look at building the SET_DRP_IE command here rather than
  517. * having to rescan the list in uwb_rc_send_all_drp_ie().
  518. */
  519. static bool uwb_rsv_update_all(struct uwb_rc *rc)
  520. {
  521. struct uwb_rsv *rsv, *t;
  522. bool ie_updated = false;
  523. list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
  524. if (rsv->expired)
  525. uwb_drp_handle_timeout(rsv);
  526. if (!rsv->ie_valid) {
  527. uwb_drp_ie_update(rsv);
  528. ie_updated = true;
  529. }
  530. }
  531. return ie_updated;
  532. }
  533. void uwb_rsv_sched_update(struct uwb_rc *rc)
  534. {
  535. queue_work(rc->rsv_workq, &rc->rsv_update_work);
  536. }
  537. /*
  538. * Update DRP IEs and, if necessary, the DRP Availability IE and send
  539. * the updated IEs to the radio controller.
  540. */
  541. static void uwb_rsv_update_work(struct work_struct *work)
  542. {
  543. struct uwb_rc *rc = container_of(work, struct uwb_rc, rsv_update_work);
  544. bool ie_updated;
  545. mutex_lock(&rc->rsvs_mutex);
  546. ie_updated = uwb_rsv_update_all(rc);
  547. if (!rc->drp_avail.ie_valid) {
  548. uwb_drp_avail_ie_update(rc);
  549. ie_updated = true;
  550. }
  551. if (ie_updated)
  552. uwb_rc_send_all_drp_ie(rc);
  553. mutex_unlock(&rc->rsvs_mutex);
  554. }
  555. static void uwb_rsv_timer(unsigned long arg)
  556. {
  557. struct uwb_rsv *rsv = (struct uwb_rsv *)arg;
  558. rsv->expired = true;
  559. uwb_rsv_sched_update(rsv->rc);
  560. }
  561. void uwb_rsv_init(struct uwb_rc *rc)
  562. {
  563. INIT_LIST_HEAD(&rc->reservations);
  564. mutex_init(&rc->rsvs_mutex);
  565. INIT_WORK(&rc->rsv_update_work, uwb_rsv_update_work);
  566. bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS);
  567. }
  568. int uwb_rsv_setup(struct uwb_rc *rc)
  569. {
  570. char name[16];
  571. snprintf(name, sizeof(name), "%s_rsvd", dev_name(&rc->uwb_dev.dev));
  572. rc->rsv_workq = create_singlethread_workqueue(name);
  573. if (rc->rsv_workq == NULL)
  574. return -ENOMEM;
  575. return 0;
  576. }
  577. void uwb_rsv_cleanup(struct uwb_rc *rc)
  578. {
  579. struct uwb_rsv *rsv, *t;
  580. mutex_lock(&rc->rsvs_mutex);
  581. list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
  582. uwb_rsv_remove(rsv);
  583. }
  584. mutex_unlock(&rc->rsvs_mutex);
  585. cancel_work_sync(&rc->rsv_update_work);
  586. destroy_workqueue(rc->rsv_workq);
  587. }