rsv.c 27 KB

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