asl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Wireless Host Controller (WHC) asynchronous schedule management.
  4. *
  5. * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/gfp.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/uwb/umc.h>
  11. #include <linux/usb.h>
  12. #include "../../wusbcore/wusbhc.h"
  13. #include "whcd.h"
  14. static void qset_get_next_prev(struct whc *whc, struct whc_qset *qset,
  15. struct whc_qset **next, struct whc_qset **prev)
  16. {
  17. struct list_head *n, *p;
  18. BUG_ON(list_empty(&whc->async_list));
  19. n = qset->list_node.next;
  20. if (n == &whc->async_list)
  21. n = n->next;
  22. p = qset->list_node.prev;
  23. if (p == &whc->async_list)
  24. p = p->prev;
  25. *next = container_of(n, struct whc_qset, list_node);
  26. *prev = container_of(p, struct whc_qset, list_node);
  27. }
  28. static void asl_qset_insert_begin(struct whc *whc, struct whc_qset *qset)
  29. {
  30. list_move(&qset->list_node, &whc->async_list);
  31. qset->in_sw_list = true;
  32. }
  33. static void asl_qset_insert(struct whc *whc, struct whc_qset *qset)
  34. {
  35. struct whc_qset *next, *prev;
  36. qset_clear(whc, qset);
  37. /* Link into ASL. */
  38. qset_get_next_prev(whc, qset, &next, &prev);
  39. whc_qset_set_link_ptr(&qset->qh.link, next->qset_dma);
  40. whc_qset_set_link_ptr(&prev->qh.link, qset->qset_dma);
  41. qset->in_hw_list = true;
  42. }
  43. static void asl_qset_remove(struct whc *whc, struct whc_qset *qset)
  44. {
  45. struct whc_qset *prev, *next;
  46. qset_get_next_prev(whc, qset, &next, &prev);
  47. list_move(&qset->list_node, &whc->async_removed_list);
  48. qset->in_sw_list = false;
  49. /*
  50. * No more qsets in the ASL? The caller must stop the ASL as
  51. * it's no longer valid.
  52. */
  53. if (list_empty(&whc->async_list))
  54. return;
  55. /* Remove from ASL. */
  56. whc_qset_set_link_ptr(&prev->qh.link, next->qset_dma);
  57. qset->in_hw_list = false;
  58. }
  59. /**
  60. * process_qset - process any recently inactivated or halted qTDs in a
  61. * qset.
  62. *
  63. * After inactive qTDs are removed, new qTDs can be added if the
  64. * urb queue still contains URBs.
  65. *
  66. * Returns any additional WUSBCMD bits for the ASL sync command (i.e.,
  67. * WUSBCMD_ASYNC_QSET_RM if a halted qset was removed).
  68. */
  69. static uint32_t process_qset(struct whc *whc, struct whc_qset *qset)
  70. {
  71. enum whc_update update = 0;
  72. uint32_t status = 0;
  73. while (qset->ntds) {
  74. struct whc_qtd *td;
  75. td = &qset->qtd[qset->td_start];
  76. status = le32_to_cpu(td->status);
  77. /*
  78. * Nothing to do with a still active qTD.
  79. */
  80. if (status & QTD_STS_ACTIVE)
  81. break;
  82. if (status & QTD_STS_HALTED) {
  83. /* Ug, an error. */
  84. process_halted_qtd(whc, qset, td);
  85. /* A halted qTD always triggers an update
  86. because the qset was either removed or
  87. reactivated. */
  88. update |= WHC_UPDATE_UPDATED;
  89. goto done;
  90. }
  91. /* Mmm, a completed qTD. */
  92. process_inactive_qtd(whc, qset, td);
  93. }
  94. if (!qset->remove)
  95. update |= qset_add_qtds(whc, qset);
  96. done:
  97. /*
  98. * Remove this qset from the ASL if requested, but only if has
  99. * no qTDs.
  100. */
  101. if (qset->remove && qset->ntds == 0) {
  102. asl_qset_remove(whc, qset);
  103. update |= WHC_UPDATE_REMOVED;
  104. }
  105. return update;
  106. }
  107. void asl_start(struct whc *whc)
  108. {
  109. struct whc_qset *qset;
  110. qset = list_first_entry(&whc->async_list, struct whc_qset, list_node);
  111. le_writeq(qset->qset_dma | QH_LINK_NTDS(8), whc->base + WUSBASYNCLISTADDR);
  112. whc_write_wusbcmd(whc, WUSBCMD_ASYNC_EN, WUSBCMD_ASYNC_EN);
  113. whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
  114. WUSBSTS_ASYNC_SCHED, WUSBSTS_ASYNC_SCHED,
  115. 1000, "start ASL");
  116. }
  117. void asl_stop(struct whc *whc)
  118. {
  119. whc_write_wusbcmd(whc, WUSBCMD_ASYNC_EN, 0);
  120. whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
  121. WUSBSTS_ASYNC_SCHED, 0,
  122. 1000, "stop ASL");
  123. }
  124. /**
  125. * asl_update - request an ASL update and wait for the hardware to be synced
  126. * @whc: the WHCI HC
  127. * @wusbcmd: WUSBCMD value to start the update.
  128. *
  129. * If the WUSB HC is inactive (i.e., the ASL is stopped) then the
  130. * update must be skipped as the hardware may not respond to update
  131. * requests.
  132. */
  133. void asl_update(struct whc *whc, uint32_t wusbcmd)
  134. {
  135. struct wusbhc *wusbhc = &whc->wusbhc;
  136. long t;
  137. mutex_lock(&wusbhc->mutex);
  138. if (wusbhc->active) {
  139. whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
  140. t = wait_event_timeout(
  141. whc->async_list_wq,
  142. (le_readl(whc->base + WUSBCMD) & WUSBCMD_ASYNC_UPDATED) == 0,
  143. msecs_to_jiffies(1000));
  144. if (t == 0)
  145. whc_hw_error(whc, "ASL update timeout");
  146. }
  147. mutex_unlock(&wusbhc->mutex);
  148. }
  149. /**
  150. * scan_async_work - scan the ASL for qsets to process.
  151. *
  152. * Process each qset in the ASL in turn and then signal the WHC that
  153. * the ASL has been updated.
  154. *
  155. * Then start, stop or update the asynchronous schedule as required.
  156. */
  157. void scan_async_work(struct work_struct *work)
  158. {
  159. struct whc *whc = container_of(work, struct whc, async_work);
  160. struct whc_qset *qset, *t;
  161. enum whc_update update = 0;
  162. spin_lock_irq(&whc->lock);
  163. /*
  164. * Transerve the software list backwards so new qsets can be
  165. * safely inserted into the ASL without making it non-circular.
  166. */
  167. list_for_each_entry_safe_reverse(qset, t, &whc->async_list, list_node) {
  168. if (!qset->in_hw_list) {
  169. asl_qset_insert(whc, qset);
  170. update |= WHC_UPDATE_ADDED;
  171. }
  172. update |= process_qset(whc, qset);
  173. }
  174. spin_unlock_irq(&whc->lock);
  175. if (update) {
  176. uint32_t wusbcmd = WUSBCMD_ASYNC_UPDATED | WUSBCMD_ASYNC_SYNCED_DB;
  177. if (update & WHC_UPDATE_REMOVED)
  178. wusbcmd |= WUSBCMD_ASYNC_QSET_RM;
  179. asl_update(whc, wusbcmd);
  180. }
  181. /*
  182. * Now that the ASL is updated, complete the removal of any
  183. * removed qsets.
  184. *
  185. * If the qset was to be reset, do so and reinsert it into the
  186. * ASL if it has pending transfers.
  187. */
  188. spin_lock_irq(&whc->lock);
  189. list_for_each_entry_safe(qset, t, &whc->async_removed_list, list_node) {
  190. qset_remove_complete(whc, qset);
  191. if (qset->reset) {
  192. qset_reset(whc, qset);
  193. if (!list_empty(&qset->stds)) {
  194. asl_qset_insert_begin(whc, qset);
  195. queue_work(whc->workqueue, &whc->async_work);
  196. }
  197. }
  198. }
  199. spin_unlock_irq(&whc->lock);
  200. }
  201. /**
  202. * asl_urb_enqueue - queue an URB onto the asynchronous list (ASL).
  203. * @whc: the WHCI host controller
  204. * @urb: the URB to enqueue
  205. * @mem_flags: flags for any memory allocations
  206. *
  207. * The qset for the endpoint is obtained and the urb queued on to it.
  208. *
  209. * Work is scheduled to update the hardware's view of the ASL.
  210. */
  211. int asl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
  212. {
  213. struct whc_qset *qset;
  214. int err;
  215. unsigned long flags;
  216. spin_lock_irqsave(&whc->lock, flags);
  217. err = usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
  218. if (err < 0) {
  219. spin_unlock_irqrestore(&whc->lock, flags);
  220. return err;
  221. }
  222. qset = get_qset(whc, urb, GFP_ATOMIC);
  223. if (qset == NULL)
  224. err = -ENOMEM;
  225. else
  226. err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
  227. if (!err) {
  228. if (!qset->in_sw_list && !qset->remove)
  229. asl_qset_insert_begin(whc, qset);
  230. } else
  231. usb_hcd_unlink_urb_from_ep(&whc->wusbhc.usb_hcd, urb);
  232. spin_unlock_irqrestore(&whc->lock, flags);
  233. if (!err)
  234. queue_work(whc->workqueue, &whc->async_work);
  235. return err;
  236. }
  237. /**
  238. * asl_urb_dequeue - remove an URB (qset) from the async list.
  239. * @whc: the WHCI host controller
  240. * @urb: the URB to dequeue
  241. * @status: the current status of the URB
  242. *
  243. * URBs that do yet have qTDs can simply be removed from the software
  244. * queue, otherwise the qset must be removed from the ASL so the qTDs
  245. * can be removed.
  246. */
  247. int asl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
  248. {
  249. struct whc_urb *wurb = urb->hcpriv;
  250. struct whc_qset *qset = wurb->qset;
  251. struct whc_std *std, *t;
  252. bool has_qtd = false;
  253. int ret;
  254. unsigned long flags;
  255. spin_lock_irqsave(&whc->lock, flags);
  256. ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
  257. if (ret < 0)
  258. goto out;
  259. list_for_each_entry_safe(std, t, &qset->stds, list_node) {
  260. if (std->urb == urb) {
  261. if (std->qtd)
  262. has_qtd = true;
  263. qset_free_std(whc, std);
  264. } else
  265. std->qtd = NULL; /* so this std is re-added when the qset is */
  266. }
  267. if (has_qtd) {
  268. asl_qset_remove(whc, qset);
  269. wurb->status = status;
  270. wurb->is_async = true;
  271. queue_work(whc->workqueue, &wurb->dequeue_work);
  272. } else
  273. qset_remove_urb(whc, qset, urb, status);
  274. out:
  275. spin_unlock_irqrestore(&whc->lock, flags);
  276. return ret;
  277. }
  278. /**
  279. * asl_qset_delete - delete a qset from the ASL
  280. */
  281. void asl_qset_delete(struct whc *whc, struct whc_qset *qset)
  282. {
  283. qset->remove = 1;
  284. queue_work(whc->workqueue, &whc->async_work);
  285. qset_delete(whc, qset);
  286. }
  287. /**
  288. * asl_init - initialize the asynchronous schedule list
  289. *
  290. * A dummy qset with no qTDs is added to the ASL to simplify removing
  291. * qsets (no need to stop the ASL when the last qset is removed).
  292. */
  293. int asl_init(struct whc *whc)
  294. {
  295. struct whc_qset *qset;
  296. qset = qset_alloc(whc, GFP_KERNEL);
  297. if (qset == NULL)
  298. return -ENOMEM;
  299. asl_qset_insert_begin(whc, qset);
  300. asl_qset_insert(whc, qset);
  301. return 0;
  302. }
  303. /**
  304. * asl_clean_up - free ASL resources
  305. *
  306. * The ASL is stopped and empty except for the dummy qset.
  307. */
  308. void asl_clean_up(struct whc *whc)
  309. {
  310. struct whc_qset *qset;
  311. if (!list_empty(&whc->async_list)) {
  312. qset = list_first_entry(&whc->async_list, struct whc_qset, list_node);
  313. list_del(&qset->list_node);
  314. qset_free(whc, qset);
  315. }
  316. }