rsv.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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: %s\n",
  107. text, owner, target, uwb_rsv_state_str(rsv->state));
  108. }
  109. static void uwb_rsv_release(struct kref *kref)
  110. {
  111. struct uwb_rsv *rsv = container_of(kref, struct uwb_rsv, kref);
  112. kfree(rsv);
  113. }
  114. void uwb_rsv_get(struct uwb_rsv *rsv)
  115. {
  116. kref_get(&rsv->kref);
  117. }
  118. void uwb_rsv_put(struct uwb_rsv *rsv)
  119. {
  120. kref_put(&rsv->kref, uwb_rsv_release);
  121. }
  122. /*
  123. * Get a free stream index for a reservation.
  124. *
  125. * If the target is a DevAddr (e.g., a WUSB cluster reservation) then
  126. * the stream is allocated from a pool of per-RC stream indexes,
  127. * otherwise a unique stream index for the target is selected.
  128. */
  129. static int uwb_rsv_get_stream(struct uwb_rsv *rsv)
  130. {
  131. struct uwb_rc *rc = rsv->rc;
  132. struct device *dev = &rc->uwb_dev.dev;
  133. unsigned long *streams_bm;
  134. int stream;
  135. switch (rsv->target.type) {
  136. case UWB_RSV_TARGET_DEV:
  137. streams_bm = rsv->target.dev->streams;
  138. break;
  139. case UWB_RSV_TARGET_DEVADDR:
  140. streams_bm = rc->uwb_dev.streams;
  141. break;
  142. default:
  143. return -EINVAL;
  144. }
  145. stream = find_first_zero_bit(streams_bm, UWB_NUM_STREAMS);
  146. if (stream >= UWB_NUM_STREAMS)
  147. return -EBUSY;
  148. rsv->stream = stream;
  149. set_bit(stream, streams_bm);
  150. dev_dbg(dev, "get stream %d\n", rsv->stream);
  151. return 0;
  152. }
  153. static void uwb_rsv_put_stream(struct uwb_rsv *rsv)
  154. {
  155. struct uwb_rc *rc = rsv->rc;
  156. struct device *dev = &rc->uwb_dev.dev;
  157. unsigned long *streams_bm;
  158. switch (rsv->target.type) {
  159. case UWB_RSV_TARGET_DEV:
  160. streams_bm = rsv->target.dev->streams;
  161. break;
  162. case UWB_RSV_TARGET_DEVADDR:
  163. streams_bm = rc->uwb_dev.streams;
  164. break;
  165. default:
  166. return;
  167. }
  168. clear_bit(rsv->stream, streams_bm);
  169. dev_dbg(dev, "put stream %d\n", rsv->stream);
  170. }
  171. void uwb_rsv_backoff_win_timer(unsigned long arg)
  172. {
  173. struct uwb_drp_backoff_win *bow = (struct uwb_drp_backoff_win *)arg;
  174. struct uwb_rc *rc = container_of(bow, struct uwb_rc, bow);
  175. struct device *dev = &rc->uwb_dev.dev;
  176. bow->can_reserve_extra_mases = true;
  177. if (bow->total_expired <= 4) {
  178. bow->total_expired++;
  179. } else {
  180. /* after 4 backoff window has expired we can exit from
  181. * the backoff procedure */
  182. bow->total_expired = 0;
  183. bow->window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
  184. }
  185. dev_dbg(dev, "backoff_win_timer total_expired=%d, n=%d\n: ", bow->total_expired, bow->n);
  186. /* try to relocate all the "to be moved" relocations */
  187. uwb_rsv_handle_drp_avail_change(rc);
  188. }
  189. void uwb_rsv_backoff_win_increment(struct uwb_rc *rc)
  190. {
  191. struct uwb_drp_backoff_win *bow = &rc->bow;
  192. struct device *dev = &rc->uwb_dev.dev;
  193. unsigned timeout_us;
  194. dev_dbg(dev, "backoff_win_increment: window=%d\n", bow->window);
  195. bow->can_reserve_extra_mases = false;
  196. if((bow->window << 1) == UWB_DRP_BACKOFF_WIN_MAX)
  197. return;
  198. bow->window <<= 1;
  199. bow->n = random32() & (bow->window - 1);
  200. dev_dbg(dev, "new_window=%d, n=%d\n: ", bow->window, bow->n);
  201. /* reset the timer associated variables */
  202. timeout_us = bow->n * UWB_SUPERFRAME_LENGTH_US;
  203. bow->total_expired = 0;
  204. mod_timer(&bow->timer, jiffies + usecs_to_jiffies(timeout_us));
  205. }
  206. static void uwb_rsv_stroke_timer(struct uwb_rsv *rsv)
  207. {
  208. int sframes = UWB_MAX_LOST_BEACONS;
  209. /*
  210. * Multicast reservations can become established within 1
  211. * super frame and should not be terminated if no response is
  212. * received.
  213. */
  214. if (rsv->is_multicast) {
  215. if (rsv->state == UWB_RSV_STATE_O_INITIATED
  216. || rsv->state == UWB_RSV_STATE_O_MOVE_EXPANDING
  217. || rsv->state == UWB_RSV_STATE_O_MOVE_COMBINING
  218. || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING)
  219. sframes = 1;
  220. if (rsv->state == UWB_RSV_STATE_O_ESTABLISHED)
  221. sframes = 0;
  222. }
  223. if (sframes > 0) {
  224. /*
  225. * Add an additional 2 superframes to account for the
  226. * time to send the SET DRP IE command.
  227. */
  228. unsigned timeout_us = (sframes + 2) * UWB_SUPERFRAME_LENGTH_US;
  229. mod_timer(&rsv->timer, jiffies + usecs_to_jiffies(timeout_us));
  230. } else
  231. del_timer(&rsv->timer);
  232. }
  233. /*
  234. * Update a reservations state, and schedule an update of the
  235. * transmitted DRP IEs.
  236. */
  237. static void uwb_rsv_state_update(struct uwb_rsv *rsv,
  238. enum uwb_rsv_state new_state)
  239. {
  240. rsv->state = new_state;
  241. rsv->ie_valid = false;
  242. uwb_rsv_dump("SU", rsv);
  243. uwb_rsv_stroke_timer(rsv);
  244. uwb_rsv_sched_update(rsv->rc);
  245. }
  246. static void uwb_rsv_callback(struct uwb_rsv *rsv)
  247. {
  248. if (rsv->callback)
  249. rsv->callback(rsv);
  250. }
  251. void uwb_rsv_set_state(struct uwb_rsv *rsv, enum uwb_rsv_state new_state)
  252. {
  253. struct uwb_rsv_move *mv = &rsv->mv;
  254. if (rsv->state == new_state) {
  255. switch (rsv->state) {
  256. case UWB_RSV_STATE_O_ESTABLISHED:
  257. case UWB_RSV_STATE_O_MOVE_EXPANDING:
  258. case UWB_RSV_STATE_O_MOVE_COMBINING:
  259. case UWB_RSV_STATE_O_MOVE_REDUCING:
  260. case UWB_RSV_STATE_T_ACCEPTED:
  261. case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
  262. case UWB_RSV_STATE_T_RESIZED:
  263. case UWB_RSV_STATE_NONE:
  264. uwb_rsv_stroke_timer(rsv);
  265. break;
  266. default:
  267. /* Expecting a state transition so leave timer
  268. as-is. */
  269. break;
  270. }
  271. return;
  272. }
  273. uwb_rsv_dump("SC", rsv);
  274. switch (new_state) {
  275. case UWB_RSV_STATE_NONE:
  276. uwb_rsv_state_update(rsv, UWB_RSV_STATE_NONE);
  277. uwb_rsv_callback(rsv);
  278. break;
  279. case UWB_RSV_STATE_O_INITIATED:
  280. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_INITIATED);
  281. break;
  282. case UWB_RSV_STATE_O_PENDING:
  283. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_PENDING);
  284. break;
  285. case UWB_RSV_STATE_O_MODIFIED:
  286. /* in the companion there are the MASes to drop */
  287. bitmap_andnot(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS);
  288. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MODIFIED);
  289. break;
  290. case UWB_RSV_STATE_O_ESTABLISHED:
  291. if (rsv->state == UWB_RSV_STATE_O_MODIFIED
  292. || rsv->state == UWB_RSV_STATE_O_MOVE_REDUCING) {
  293. uwb_drp_avail_release(rsv->rc, &mv->companion_mas);
  294. rsv->needs_release_companion_mas = false;
  295. }
  296. uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
  297. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_ESTABLISHED);
  298. uwb_rsv_callback(rsv);
  299. break;
  300. case UWB_RSV_STATE_O_MOVE_EXPANDING:
  301. rsv->needs_release_companion_mas = true;
  302. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_EXPANDING);
  303. break;
  304. case UWB_RSV_STATE_O_MOVE_COMBINING:
  305. rsv->needs_release_companion_mas = false;
  306. uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas);
  307. bitmap_or(rsv->mas.bm, rsv->mas.bm, mv->companion_mas.bm, UWB_NUM_MAS);
  308. rsv->mas.safe += mv->companion_mas.safe;
  309. rsv->mas.unsafe += mv->companion_mas.unsafe;
  310. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_COMBINING);
  311. break;
  312. case UWB_RSV_STATE_O_MOVE_REDUCING:
  313. bitmap_andnot(mv->companion_mas.bm, rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS);
  314. rsv->needs_release_companion_mas = true;
  315. rsv->mas.safe = mv->final_mas.safe;
  316. rsv->mas.unsafe = mv->final_mas.unsafe;
  317. bitmap_copy(rsv->mas.bm, mv->final_mas.bm, UWB_NUM_MAS);
  318. bitmap_copy(rsv->mas.unsafe_bm, mv->final_mas.unsafe_bm, UWB_NUM_MAS);
  319. uwb_rsv_state_update(rsv, UWB_RSV_STATE_O_MOVE_REDUCING);
  320. break;
  321. case UWB_RSV_STATE_T_ACCEPTED:
  322. case UWB_RSV_STATE_T_RESIZED:
  323. rsv->needs_release_companion_mas = false;
  324. uwb_drp_avail_reserve(rsv->rc, &rsv->mas);
  325. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_ACCEPTED);
  326. uwb_rsv_callback(rsv);
  327. break;
  328. case UWB_RSV_STATE_T_DENIED:
  329. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_DENIED);
  330. break;
  331. case UWB_RSV_STATE_T_CONFLICT:
  332. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_CONFLICT);
  333. break;
  334. case UWB_RSV_STATE_T_PENDING:
  335. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_PENDING);
  336. break;
  337. case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
  338. rsv->needs_release_companion_mas = true;
  339. uwb_drp_avail_reserve(rsv->rc, &mv->companion_mas);
  340. uwb_rsv_state_update(rsv, UWB_RSV_STATE_T_EXPANDING_ACCEPTED);
  341. break;
  342. default:
  343. dev_err(&rsv->rc->uwb_dev.dev, "unhandled state: %s (%d)\n",
  344. uwb_rsv_state_str(new_state), new_state);
  345. }
  346. }
  347. static void uwb_rsv_handle_timeout_work(struct work_struct *work)
  348. {
  349. struct uwb_rsv *rsv = container_of(work, struct uwb_rsv,
  350. handle_timeout_work);
  351. struct uwb_rc *rc = rsv->rc;
  352. mutex_lock(&rc->rsvs_mutex);
  353. uwb_rsv_dump("TO", rsv);
  354. switch (rsv->state) {
  355. case UWB_RSV_STATE_O_INITIATED:
  356. if (rsv->is_multicast) {
  357. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
  358. goto unlock;
  359. }
  360. break;
  361. case UWB_RSV_STATE_O_MOVE_EXPANDING:
  362. if (rsv->is_multicast) {
  363. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_COMBINING);
  364. goto unlock;
  365. }
  366. break;
  367. case UWB_RSV_STATE_O_MOVE_COMBINING:
  368. if (rsv->is_multicast) {
  369. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_MOVE_REDUCING);
  370. goto unlock;
  371. }
  372. break;
  373. case UWB_RSV_STATE_O_MOVE_REDUCING:
  374. if (rsv->is_multicast) {
  375. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
  376. goto unlock;
  377. }
  378. break;
  379. case UWB_RSV_STATE_O_ESTABLISHED:
  380. if (rsv->is_multicast)
  381. goto unlock;
  382. break;
  383. case UWB_RSV_STATE_T_EXPANDING_ACCEPTED:
  384. /*
  385. * The time out could be for the main or of the
  386. * companion DRP, assume it's for the companion and
  387. * drop that first. A further time out is required to
  388. * drop the main.
  389. */
  390. uwb_rsv_set_state(rsv, UWB_RSV_STATE_T_ACCEPTED);
  391. uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
  392. goto unlock;
  393. default:
  394. break;
  395. }
  396. uwb_rsv_remove(rsv);
  397. unlock:
  398. mutex_unlock(&rc->rsvs_mutex);
  399. }
  400. static struct uwb_rsv *uwb_rsv_alloc(struct uwb_rc *rc)
  401. {
  402. struct uwb_rsv *rsv;
  403. rsv = kzalloc(sizeof(struct uwb_rsv), GFP_KERNEL);
  404. if (!rsv)
  405. return NULL;
  406. INIT_LIST_HEAD(&rsv->rc_node);
  407. INIT_LIST_HEAD(&rsv->pal_node);
  408. kref_init(&rsv->kref);
  409. init_timer(&rsv->timer);
  410. rsv->timer.function = uwb_rsv_timer;
  411. rsv->timer.data = (unsigned long)rsv;
  412. rsv->rc = rc;
  413. INIT_WORK(&rsv->handle_timeout_work, uwb_rsv_handle_timeout_work);
  414. return rsv;
  415. }
  416. /**
  417. * uwb_rsv_create - allocate and initialize a UWB reservation structure
  418. * @rc: the radio controller
  419. * @cb: callback to use when the reservation completes or terminates
  420. * @pal_priv: data private to the PAL to be passed in the callback
  421. *
  422. * The callback is called when the state of the reservation changes from:
  423. *
  424. * - pending to accepted
  425. * - pending to denined
  426. * - accepted to terminated
  427. * - pending to terminated
  428. */
  429. struct uwb_rsv *uwb_rsv_create(struct uwb_rc *rc, uwb_rsv_cb_f cb, void *pal_priv)
  430. {
  431. struct uwb_rsv *rsv;
  432. rsv = uwb_rsv_alloc(rc);
  433. if (!rsv)
  434. return NULL;
  435. rsv->callback = cb;
  436. rsv->pal_priv = pal_priv;
  437. return rsv;
  438. }
  439. EXPORT_SYMBOL_GPL(uwb_rsv_create);
  440. void uwb_rsv_remove(struct uwb_rsv *rsv)
  441. {
  442. uwb_rsv_dump("RM", rsv);
  443. if (rsv->state != UWB_RSV_STATE_NONE)
  444. uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
  445. if (rsv->needs_release_companion_mas)
  446. uwb_drp_avail_release(rsv->rc, &rsv->mv.companion_mas);
  447. uwb_drp_avail_release(rsv->rc, &rsv->mas);
  448. if (uwb_rsv_is_owner(rsv))
  449. uwb_rsv_put_stream(rsv);
  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_bh(&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_bh(&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. if (rsv->state != UWB_RSV_STATE_NONE)
  820. uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
  821. del_timer_sync(&rsv->timer);
  822. }
  823. /* Cancel any postponed update. */
  824. rc->set_drp_ie_pending = 0;
  825. mutex_unlock(&rc->rsvs_mutex);
  826. cancel_delayed_work_sync(&rc->rsv_update_work);
  827. flush_workqueue(rc->rsv_workq);
  828. mutex_lock(&rc->rsvs_mutex);
  829. list_for_each_entry_safe(rsv, t, &rc->reservations, rc_node) {
  830. uwb_rsv_remove(rsv);
  831. }
  832. mutex_unlock(&rc->rsvs_mutex);
  833. }
  834. void uwb_rsv_init(struct uwb_rc *rc)
  835. {
  836. INIT_LIST_HEAD(&rc->reservations);
  837. INIT_LIST_HEAD(&rc->cnflt_alien_list);
  838. mutex_init(&rc->rsvs_mutex);
  839. spin_lock_init(&rc->rsvs_lock);
  840. INIT_DELAYED_WORK(&rc->rsv_update_work, uwb_rsv_update_work);
  841. INIT_DELAYED_WORK(&rc->rsv_alien_bp_work, uwb_rsv_alien_bp_work);
  842. rc->bow.can_reserve_extra_mases = true;
  843. rc->bow.total_expired = 0;
  844. rc->bow.window = UWB_DRP_BACKOFF_WIN_MIN >> 1;
  845. init_timer(&rc->bow.timer);
  846. rc->bow.timer.function = uwb_rsv_backoff_win_timer;
  847. rc->bow.timer.data = (unsigned long)&rc->bow;
  848. bitmap_complement(rc->uwb_dev.streams, rc->uwb_dev.streams, UWB_NUM_STREAMS);
  849. }
  850. int uwb_rsv_setup(struct uwb_rc *rc)
  851. {
  852. char name[16];
  853. snprintf(name, sizeof(name), "%s_rsvd", dev_name(&rc->uwb_dev.dev));
  854. rc->rsv_workq = create_singlethread_workqueue(name);
  855. if (rc->rsv_workq == NULL)
  856. return -ENOMEM;
  857. return 0;
  858. }
  859. void uwb_rsv_cleanup(struct uwb_rc *rc)
  860. {
  861. uwb_rsv_remove_all(rc);
  862. destroy_workqueue(rc->rsv_workq);
  863. }