drp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Ultra Wide Band
  3. * Dynamic Reservation Protocol handling
  4. *
  5. * Copyright (C) 2005-2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/kthread.h>
  22. #include <linux/freezer.h>
  23. #include <linux/delay.h>
  24. #include "uwb-internal.h"
  25. /**
  26. * Construct and send the SET DRP IE
  27. *
  28. * @rc: UWB Host controller
  29. * @returns: >= 0 number of bytes still available in the beacon
  30. * < 0 errno code on error.
  31. *
  32. * See WUSB[8.6.2.7]: The host must set all the DRP IEs that it wants the
  33. * device to include in its beacon at the same time. We thus have to
  34. * traverse all reservations and include the DRP IEs of all PENDING
  35. * and NEGOTIATED reservations in a SET DRP command for transmission.
  36. *
  37. * A DRP Availability IE is appended.
  38. *
  39. * rc->uwb_dev.mutex is held
  40. *
  41. * FIXME We currently ignore the returned value indicating the remaining space
  42. * in beacon. This could be used to deny reservation requests earlier if
  43. * determined that they would cause the beacon space to be exceeded.
  44. */
  45. static
  46. int uwb_rc_gen_send_drp_ie(struct uwb_rc *rc)
  47. {
  48. int result;
  49. struct device *dev = &rc->uwb_dev.dev;
  50. struct uwb_rc_cmd_set_drp_ie *cmd;
  51. struct uwb_rc_evt_set_drp_ie reply;
  52. struct uwb_rsv *rsv;
  53. int num_bytes = 0;
  54. u8 *IEDataptr;
  55. result = -ENOMEM;
  56. /* First traverse all reservations to determine memory needed. */
  57. list_for_each_entry(rsv, &rc->reservations, rc_node) {
  58. if (rsv->drp_ie != NULL)
  59. num_bytes += rsv->drp_ie->hdr.length + 2;
  60. }
  61. num_bytes += sizeof(rc->drp_avail.ie);
  62. cmd = kzalloc(sizeof(*cmd) + num_bytes, GFP_KERNEL);
  63. if (cmd == NULL)
  64. goto error;
  65. cmd->rccb.bCommandType = UWB_RC_CET_GENERAL;
  66. cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_SET_DRP_IE);
  67. cmd->wIELength = num_bytes;
  68. IEDataptr = (u8 *)&cmd->IEData[0];
  69. /* Next traverse all reservations to place IEs in allocated memory. */
  70. list_for_each_entry(rsv, &rc->reservations, rc_node) {
  71. if (rsv->drp_ie != NULL) {
  72. memcpy(IEDataptr, rsv->drp_ie,
  73. rsv->drp_ie->hdr.length + 2);
  74. IEDataptr += rsv->drp_ie->hdr.length + 2;
  75. }
  76. }
  77. memcpy(IEDataptr, &rc->drp_avail.ie, sizeof(rc->drp_avail.ie));
  78. reply.rceb.bEventType = UWB_RC_CET_GENERAL;
  79. reply.rceb.wEvent = UWB_RC_CMD_SET_DRP_IE;
  80. result = uwb_rc_cmd(rc, "SET-DRP-IE", &cmd->rccb,
  81. sizeof(*cmd) + num_bytes, &reply.rceb,
  82. sizeof(reply));
  83. if (result < 0)
  84. goto error_cmd;
  85. result = le16_to_cpu(reply.wRemainingSpace);
  86. if (reply.bResultCode != UWB_RC_RES_SUCCESS) {
  87. dev_err(&rc->uwb_dev.dev, "SET-DRP-IE: command execution "
  88. "failed: %s (%d). RemainingSpace in beacon "
  89. "= %d\n", uwb_rc_strerror(reply.bResultCode),
  90. reply.bResultCode, result);
  91. result = -EIO;
  92. } else {
  93. dev_dbg(dev, "SET-DRP-IE sent. RemainingSpace in beacon "
  94. "= %d.\n", result);
  95. result = 0;
  96. }
  97. error_cmd:
  98. kfree(cmd);
  99. error:
  100. return result;
  101. }
  102. /**
  103. * Send all DRP IEs associated with this host
  104. *
  105. * @returns: >= 0 number of bytes still available in the beacon
  106. * < 0 errno code on error.
  107. *
  108. * As per the protocol we obtain the host controller device lock to access
  109. * bandwidth structures.
  110. */
  111. int uwb_rc_send_all_drp_ie(struct uwb_rc *rc)
  112. {
  113. int result;
  114. mutex_lock(&rc->uwb_dev.mutex);
  115. result = uwb_rc_gen_send_drp_ie(rc);
  116. mutex_unlock(&rc->uwb_dev.mutex);
  117. return result;
  118. }
  119. void uwb_drp_handle_timeout(struct uwb_rsv *rsv)
  120. {
  121. struct device *dev = &rsv->rc->uwb_dev.dev;
  122. dev_dbg(dev, "reservation timeout in state %s (%d)\n",
  123. uwb_rsv_state_str(rsv->state), rsv->state);
  124. switch (rsv->state) {
  125. case UWB_RSV_STATE_O_INITIATED:
  126. if (rsv->is_multicast) {
  127. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
  128. return;
  129. }
  130. break;
  131. case UWB_RSV_STATE_O_ESTABLISHED:
  132. if (rsv->is_multicast)
  133. return;
  134. break;
  135. default:
  136. break;
  137. }
  138. uwb_rsv_remove(rsv);
  139. }
  140. /*
  141. * Based on the DRP IE, transition a target reservation to a new
  142. * state.
  143. */
  144. static void uwb_drp_process_target(struct uwb_rc *rc, struct uwb_rsv *rsv,
  145. struct uwb_ie_drp *drp_ie)
  146. {
  147. struct device *dev = &rc->uwb_dev.dev;
  148. int status;
  149. enum uwb_drp_reason reason_code;
  150. status = uwb_ie_drp_status(drp_ie);
  151. reason_code = uwb_ie_drp_reason_code(drp_ie);
  152. if (status) {
  153. switch (reason_code) {
  154. case UWB_DRP_REASON_ACCEPTED:
  155. uwb_rsv_set_state(rsv, UWB_RSV_STATE_T_ACCEPTED);
  156. break;
  157. case UWB_DRP_REASON_MODIFIED:
  158. dev_err(dev, "FIXME: unhandled reason code (%d/%d)\n",
  159. reason_code, status);
  160. break;
  161. default:
  162. dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
  163. reason_code, status);
  164. }
  165. } else {
  166. switch (reason_code) {
  167. case UWB_DRP_REASON_ACCEPTED:
  168. /* New reservations are handled in uwb_rsv_find(). */
  169. break;
  170. case UWB_DRP_REASON_DENIED:
  171. uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
  172. break;
  173. case UWB_DRP_REASON_CONFLICT:
  174. case UWB_DRP_REASON_MODIFIED:
  175. dev_err(dev, "FIXME: unhandled reason code (%d/%d)\n",
  176. reason_code, status);
  177. break;
  178. default:
  179. dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
  180. reason_code, status);
  181. }
  182. }
  183. }
  184. /*
  185. * Based on the DRP IE, transition an owner reservation to a new
  186. * state.
  187. */
  188. static void uwb_drp_process_owner(struct uwb_rc *rc, struct uwb_rsv *rsv,
  189. struct uwb_ie_drp *drp_ie)
  190. {
  191. struct device *dev = &rc->uwb_dev.dev;
  192. int status;
  193. enum uwb_drp_reason reason_code;
  194. status = uwb_ie_drp_status(drp_ie);
  195. reason_code = uwb_ie_drp_reason_code(drp_ie);
  196. if (status) {
  197. switch (reason_code) {
  198. case UWB_DRP_REASON_ACCEPTED:
  199. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_ESTABLISHED);
  200. break;
  201. case UWB_DRP_REASON_MODIFIED:
  202. dev_err(dev, "FIXME: unhandled reason code (%d/%d)\n",
  203. reason_code, status);
  204. break;
  205. default:
  206. dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
  207. reason_code, status);
  208. }
  209. } else {
  210. switch (reason_code) {
  211. case UWB_DRP_REASON_PENDING:
  212. uwb_rsv_set_state(rsv, UWB_RSV_STATE_O_PENDING);
  213. break;
  214. case UWB_DRP_REASON_DENIED:
  215. uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
  216. break;
  217. case UWB_DRP_REASON_CONFLICT:
  218. case UWB_DRP_REASON_MODIFIED:
  219. dev_err(dev, "FIXME: unhandled reason code (%d/%d)\n",
  220. reason_code, status);
  221. break;
  222. default:
  223. dev_warn(dev, "ignoring invalid DRP IE state (%d/%d)\n",
  224. reason_code, status);
  225. }
  226. }
  227. }
  228. /*
  229. * Process a received DRP IE, it's either for a reservation owned by
  230. * the RC or targeted at it (or it's for a WUSB cluster reservation).
  231. */
  232. static void uwb_drp_process(struct uwb_rc *rc, struct uwb_dev *src,
  233. struct uwb_ie_drp *drp_ie)
  234. {
  235. struct uwb_rsv *rsv;
  236. rsv = uwb_rsv_find(rc, src, drp_ie);
  237. if (!rsv) {
  238. /*
  239. * No reservation? It's either for a recently
  240. * terminated reservation; or the DRP IE couldn't be
  241. * processed (e.g., an invalid IE or out of memory).
  242. */
  243. return;
  244. }
  245. /*
  246. * Do nothing with DRP IEs for reservations that have been
  247. * terminated.
  248. */
  249. if (rsv->state == UWB_RSV_STATE_NONE) {
  250. uwb_rsv_set_state(rsv, UWB_RSV_STATE_NONE);
  251. return;
  252. }
  253. if (uwb_ie_drp_owner(drp_ie))
  254. uwb_drp_process_target(rc, rsv, drp_ie);
  255. else
  256. uwb_drp_process_owner(rc, rsv, drp_ie);
  257. }
  258. /*
  259. * Process all the DRP IEs (both DRP IEs and the DRP Availability IE)
  260. * from a device.
  261. */
  262. static
  263. void uwb_drp_process_all(struct uwb_rc *rc, struct uwb_rc_evt_drp *drp_evt,
  264. size_t ielen, struct uwb_dev *src_dev)
  265. {
  266. struct device *dev = &rc->uwb_dev.dev;
  267. struct uwb_ie_hdr *ie_hdr;
  268. void *ptr;
  269. ptr = drp_evt->ie_data;
  270. for (;;) {
  271. ie_hdr = uwb_ie_next(&ptr, &ielen);
  272. if (!ie_hdr)
  273. break;
  274. switch (ie_hdr->element_id) {
  275. case UWB_IE_DRP_AVAILABILITY:
  276. /* FIXME: does something need to be done with this? */
  277. break;
  278. case UWB_IE_DRP:
  279. uwb_drp_process(rc, src_dev, (struct uwb_ie_drp *)ie_hdr);
  280. break;
  281. default:
  282. dev_warn(dev, "unexpected IE in DRP notification\n");
  283. break;
  284. }
  285. }
  286. if (ielen > 0)
  287. dev_warn(dev, "%d octets remaining in DRP notification\n",
  288. (int)ielen);
  289. }
  290. /*
  291. * Go through all the DRP IEs and find the ones that conflict with our
  292. * reservations.
  293. *
  294. * FIXME: must resolve the conflict according the the rules in
  295. * [ECMA-368].
  296. */
  297. static
  298. void uwb_drp_process_conflict_all(struct uwb_rc *rc, struct uwb_rc_evt_drp *drp_evt,
  299. size_t ielen, struct uwb_dev *src_dev)
  300. {
  301. struct device *dev = &rc->uwb_dev.dev;
  302. struct uwb_ie_hdr *ie_hdr;
  303. struct uwb_ie_drp *drp_ie;
  304. void *ptr;
  305. ptr = drp_evt->ie_data;
  306. for (;;) {
  307. ie_hdr = uwb_ie_next(&ptr, &ielen);
  308. if (!ie_hdr)
  309. break;
  310. drp_ie = container_of(ie_hdr, struct uwb_ie_drp, hdr);
  311. /* FIXME: check if this DRP IE conflicts. */
  312. }
  313. if (ielen > 0)
  314. dev_warn(dev, "%d octets remaining in DRP notification\n",
  315. (int)ielen);
  316. }
  317. /*
  318. * Terminate all reservations owned by, or targeted at, 'uwb_dev'.
  319. */
  320. static void uwb_drp_terminate_all(struct uwb_rc *rc, struct uwb_dev *uwb_dev)
  321. {
  322. struct uwb_rsv *rsv;
  323. list_for_each_entry(rsv, &rc->reservations, rc_node) {
  324. if (rsv->owner == uwb_dev
  325. || (rsv->target.type == UWB_RSV_TARGET_DEV && rsv->target.dev == uwb_dev))
  326. uwb_rsv_remove(rsv);
  327. }
  328. }
  329. /**
  330. * uwbd_evt_handle_rc_drp - handle a DRP_IE event
  331. * @evt: the DRP_IE event from the radio controller
  332. *
  333. * This processes DRP notifications from the radio controller, either
  334. * initiating a new reservation or transitioning an existing
  335. * reservation into a different state.
  336. *
  337. * DRP notifications can occur for three different reasons:
  338. *
  339. * - UWB_DRP_NOTIF_DRP_IE_RECVD: one or more DRP IEs with the RC as
  340. * the target or source have been recieved.
  341. *
  342. * These DRP IEs could be new or for an existing reservation.
  343. *
  344. * If the DRP IE for an existing reservation ceases to be to
  345. * recieved for at least mMaxLostBeacons, the reservation should be
  346. * considered to be terminated. Note that the TERMINATE reason (see
  347. * below) may not always be signalled (e.g., the remote device has
  348. * two or more reservations established with the RC).
  349. *
  350. * - UWB_DRP_NOTIF_CONFLICT: DRP IEs from any device in the beacon
  351. * group conflict with the RC's reservations.
  352. *
  353. * - UWB_DRP_NOTIF_TERMINATE: DRP IEs are no longer being received
  354. * from a device (i.e., it's terminated all reservations).
  355. *
  356. * Only the software state of the reservations is changed; the setting
  357. * of the radio controller's DRP IEs is done after all the events in
  358. * an event buffer are processed. This saves waiting multiple times
  359. * for the SET_DRP_IE command to complete.
  360. */
  361. int uwbd_evt_handle_rc_drp(struct uwb_event *evt)
  362. {
  363. struct device *dev = &evt->rc->uwb_dev.dev;
  364. struct uwb_rc *rc = evt->rc;
  365. struct uwb_rc_evt_drp *drp_evt;
  366. size_t ielength, bytes_left;
  367. struct uwb_dev_addr src_addr;
  368. struct uwb_dev *src_dev;
  369. int reason;
  370. /* Is there enough data to decode the event (and any IEs in
  371. its payload)? */
  372. if (evt->notif.size < sizeof(*drp_evt)) {
  373. dev_err(dev, "DRP event: Not enough data to decode event "
  374. "[%zu bytes left, %zu needed]\n",
  375. evt->notif.size, sizeof(*drp_evt));
  376. return 0;
  377. }
  378. bytes_left = evt->notif.size - sizeof(*drp_evt);
  379. drp_evt = container_of(evt->notif.rceb, struct uwb_rc_evt_drp, rceb);
  380. ielength = le16_to_cpu(drp_evt->ie_length);
  381. if (bytes_left != ielength) {
  382. dev_err(dev, "DRP event: Not enough data in payload [%zu"
  383. "bytes left, %zu declared in the event]\n",
  384. bytes_left, ielength);
  385. return 0;
  386. }
  387. memcpy(src_addr.data, &drp_evt->src_addr, sizeof(src_addr));
  388. src_dev = uwb_dev_get_by_devaddr(rc, &src_addr);
  389. if (!src_dev) {
  390. /*
  391. * A DRP notification from an unrecognized device.
  392. *
  393. * This is probably from a WUSB device that doesn't
  394. * have an EUI-48 and therefore doesn't show up in the
  395. * UWB device database. It's safe to simply ignore
  396. * these.
  397. */
  398. return 0;
  399. }
  400. mutex_lock(&rc->rsvs_mutex);
  401. reason = uwb_rc_evt_drp_reason(drp_evt);
  402. switch (reason) {
  403. case UWB_DRP_NOTIF_DRP_IE_RCVD:
  404. uwb_drp_process_all(rc, drp_evt, ielength, src_dev);
  405. break;
  406. case UWB_DRP_NOTIF_CONFLICT:
  407. uwb_drp_process_conflict_all(rc, drp_evt, ielength, src_dev);
  408. break;
  409. case UWB_DRP_NOTIF_TERMINATE:
  410. uwb_drp_terminate_all(rc, src_dev);
  411. break;
  412. default:
  413. dev_warn(dev, "ignored DRP event with reason code: %d\n", reason);
  414. break;
  415. }
  416. mutex_unlock(&rc->rsvs_mutex);
  417. uwb_dev_put(src_dev);
  418. return 0;
  419. }