rsv.c 27 KB

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