rsv.c 27 KB

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