pzl.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Wireless Host Controller (WHC) periodic 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 update_pzl_pointers(struct whc *whc, int period, u64 addr)
  15. {
  16. switch (period) {
  17. case 0:
  18. whc_qset_set_link_ptr(&whc->pz_list[0], addr);
  19. whc_qset_set_link_ptr(&whc->pz_list[2], addr);
  20. whc_qset_set_link_ptr(&whc->pz_list[4], addr);
  21. whc_qset_set_link_ptr(&whc->pz_list[6], addr);
  22. whc_qset_set_link_ptr(&whc->pz_list[8], addr);
  23. whc_qset_set_link_ptr(&whc->pz_list[10], addr);
  24. whc_qset_set_link_ptr(&whc->pz_list[12], addr);
  25. whc_qset_set_link_ptr(&whc->pz_list[14], addr);
  26. break;
  27. case 1:
  28. whc_qset_set_link_ptr(&whc->pz_list[1], addr);
  29. whc_qset_set_link_ptr(&whc->pz_list[5], addr);
  30. whc_qset_set_link_ptr(&whc->pz_list[9], addr);
  31. whc_qset_set_link_ptr(&whc->pz_list[13], addr);
  32. break;
  33. case 2:
  34. whc_qset_set_link_ptr(&whc->pz_list[3], addr);
  35. whc_qset_set_link_ptr(&whc->pz_list[11], addr);
  36. break;
  37. case 3:
  38. whc_qset_set_link_ptr(&whc->pz_list[7], addr);
  39. break;
  40. case 4:
  41. whc_qset_set_link_ptr(&whc->pz_list[15], addr);
  42. break;
  43. }
  44. }
  45. /*
  46. * Return the 'period' to use for this qset. The minimum interval for
  47. * the endpoint is used so whatever urbs are submitted the device is
  48. * polled often enough.
  49. */
  50. static int qset_get_period(struct whc *whc, struct whc_qset *qset)
  51. {
  52. uint8_t bInterval = qset->ep->desc.bInterval;
  53. if (bInterval < 6)
  54. bInterval = 6;
  55. if (bInterval > 10)
  56. bInterval = 10;
  57. return bInterval - 6;
  58. }
  59. static void qset_insert_in_sw_list(struct whc *whc, struct whc_qset *qset)
  60. {
  61. int period;
  62. period = qset_get_period(whc, qset);
  63. qset_clear(whc, qset);
  64. list_move(&qset->list_node, &whc->periodic_list[period]);
  65. qset->in_sw_list = true;
  66. }
  67. static void pzl_qset_remove(struct whc *whc, struct whc_qset *qset)
  68. {
  69. list_move(&qset->list_node, &whc->periodic_removed_list);
  70. qset->in_hw_list = false;
  71. qset->in_sw_list = false;
  72. }
  73. /**
  74. * pzl_process_qset - process any recently inactivated or halted qTDs
  75. * in a qset.
  76. *
  77. * After inactive qTDs are removed, new qTDs can be added if the
  78. * urb queue still contains URBs.
  79. *
  80. * Returns the schedule updates required.
  81. */
  82. static enum whc_update pzl_process_qset(struct whc *whc, struct whc_qset *qset)
  83. {
  84. enum whc_update update = 0;
  85. uint32_t status = 0;
  86. while (qset->ntds) {
  87. struct whc_qtd *td;
  88. td = &qset->qtd[qset->td_start];
  89. status = le32_to_cpu(td->status);
  90. /*
  91. * Nothing to do with a still active qTD.
  92. */
  93. if (status & QTD_STS_ACTIVE)
  94. break;
  95. if (status & QTD_STS_HALTED) {
  96. /* Ug, an error. */
  97. process_halted_qtd(whc, qset, td);
  98. /* A halted qTD always triggers an update
  99. because the qset was either removed or
  100. reactivated. */
  101. update |= WHC_UPDATE_UPDATED;
  102. goto done;
  103. }
  104. /* Mmm, a completed qTD. */
  105. process_inactive_qtd(whc, qset, td);
  106. }
  107. if (!qset->remove)
  108. update |= qset_add_qtds(whc, qset);
  109. done:
  110. /*
  111. * If there are no qTDs in this qset, remove it from the PZL.
  112. */
  113. if (qset->remove && qset->ntds == 0) {
  114. pzl_qset_remove(whc, qset);
  115. update |= WHC_UPDATE_REMOVED;
  116. }
  117. return update;
  118. }
  119. /**
  120. * pzl_start - start the periodic schedule
  121. * @whc: the WHCI host controller
  122. *
  123. * The PZL must be valid (e.g., all entries in the list should have
  124. * the T bit set).
  125. */
  126. void pzl_start(struct whc *whc)
  127. {
  128. le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
  129. whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, WUSBCMD_PERIODIC_EN);
  130. whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
  131. WUSBSTS_PERIODIC_SCHED, WUSBSTS_PERIODIC_SCHED,
  132. 1000, "start PZL");
  133. }
  134. /**
  135. * pzl_stop - stop the periodic schedule
  136. * @whc: the WHCI host controller
  137. */
  138. void pzl_stop(struct whc *whc)
  139. {
  140. whc_write_wusbcmd(whc, WUSBCMD_PERIODIC_EN, 0);
  141. whci_wait_for(&whc->umc->dev, whc->base + WUSBSTS,
  142. WUSBSTS_PERIODIC_SCHED, 0,
  143. 1000, "stop PZL");
  144. }
  145. /**
  146. * pzl_update - request a PZL update and wait for the hardware to be synced
  147. * @whc: the WHCI HC
  148. * @wusbcmd: WUSBCMD value to start the update.
  149. *
  150. * If the WUSB HC is inactive (i.e., the PZL is stopped) then the
  151. * update must be skipped as the hardware may not respond to update
  152. * requests.
  153. */
  154. void pzl_update(struct whc *whc, uint32_t wusbcmd)
  155. {
  156. struct wusbhc *wusbhc = &whc->wusbhc;
  157. long t;
  158. mutex_lock(&wusbhc->mutex);
  159. if (wusbhc->active) {
  160. whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
  161. t = wait_event_timeout(
  162. whc->periodic_list_wq,
  163. (le_readl(whc->base + WUSBCMD) & WUSBCMD_PERIODIC_UPDATED) == 0,
  164. msecs_to_jiffies(1000));
  165. if (t == 0)
  166. whc_hw_error(whc, "PZL update timeout");
  167. }
  168. mutex_unlock(&wusbhc->mutex);
  169. }
  170. static void update_pzl_hw_view(struct whc *whc)
  171. {
  172. struct whc_qset *qset, *t;
  173. int period;
  174. u64 tmp_qh = 0;
  175. for (period = 0; period < 5; period++) {
  176. list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
  177. whc_qset_set_link_ptr(&qset->qh.link, tmp_qh);
  178. tmp_qh = qset->qset_dma;
  179. qset->in_hw_list = true;
  180. }
  181. update_pzl_pointers(whc, period, tmp_qh);
  182. }
  183. }
  184. /**
  185. * scan_periodic_work - scan the PZL for qsets to process.
  186. *
  187. * Process each qset in the PZL in turn and then signal the WHC that
  188. * the PZL has been updated.
  189. *
  190. * Then start, stop or update the periodic schedule as required.
  191. */
  192. void scan_periodic_work(struct work_struct *work)
  193. {
  194. struct whc *whc = container_of(work, struct whc, periodic_work);
  195. struct whc_qset *qset, *t;
  196. enum whc_update update = 0;
  197. int period;
  198. spin_lock_irq(&whc->lock);
  199. for (period = 4; period >= 0; period--) {
  200. list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
  201. if (!qset->in_hw_list)
  202. update |= WHC_UPDATE_ADDED;
  203. update |= pzl_process_qset(whc, qset);
  204. }
  205. }
  206. if (update & (WHC_UPDATE_ADDED | WHC_UPDATE_REMOVED))
  207. update_pzl_hw_view(whc);
  208. spin_unlock_irq(&whc->lock);
  209. if (update) {
  210. uint32_t wusbcmd = WUSBCMD_PERIODIC_UPDATED | WUSBCMD_PERIODIC_SYNCED_DB;
  211. if (update & WHC_UPDATE_REMOVED)
  212. wusbcmd |= WUSBCMD_PERIODIC_QSET_RM;
  213. pzl_update(whc, wusbcmd);
  214. }
  215. /*
  216. * Now that the PZL is updated, complete the removal of any
  217. * removed qsets.
  218. *
  219. * If the qset was to be reset, do so and reinsert it into the
  220. * PZL if it has pending transfers.
  221. */
  222. spin_lock_irq(&whc->lock);
  223. list_for_each_entry_safe(qset, t, &whc->periodic_removed_list, list_node) {
  224. qset_remove_complete(whc, qset);
  225. if (qset->reset) {
  226. qset_reset(whc, qset);
  227. if (!list_empty(&qset->stds)) {
  228. qset_insert_in_sw_list(whc, qset);
  229. queue_work(whc->workqueue, &whc->periodic_work);
  230. }
  231. }
  232. }
  233. spin_unlock_irq(&whc->lock);
  234. }
  235. /**
  236. * pzl_urb_enqueue - queue an URB onto the periodic list (PZL)
  237. * @whc: the WHCI host controller
  238. * @urb: the URB to enqueue
  239. * @mem_flags: flags for any memory allocations
  240. *
  241. * The qset for the endpoint is obtained and the urb queued on to it.
  242. *
  243. * Work is scheduled to update the hardware's view of the PZL.
  244. */
  245. int pzl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
  246. {
  247. struct whc_qset *qset;
  248. int err;
  249. unsigned long flags;
  250. spin_lock_irqsave(&whc->lock, flags);
  251. err = usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
  252. if (err < 0) {
  253. spin_unlock_irqrestore(&whc->lock, flags);
  254. return err;
  255. }
  256. qset = get_qset(whc, urb, GFP_ATOMIC);
  257. if (qset == NULL)
  258. err = -ENOMEM;
  259. else
  260. err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
  261. if (!err) {
  262. if (!qset->in_sw_list && !qset->remove)
  263. qset_insert_in_sw_list(whc, qset);
  264. } else
  265. usb_hcd_unlink_urb_from_ep(&whc->wusbhc.usb_hcd, urb);
  266. spin_unlock_irqrestore(&whc->lock, flags);
  267. if (!err)
  268. queue_work(whc->workqueue, &whc->periodic_work);
  269. return err;
  270. }
  271. /**
  272. * pzl_urb_dequeue - remove an URB (qset) from the periodic list
  273. * @whc: the WHCI host controller
  274. * @urb: the URB to dequeue
  275. * @status: the current status of the URB
  276. *
  277. * URBs that do yet have qTDs can simply be removed from the software
  278. * queue, otherwise the qset must be removed so the qTDs can be safely
  279. * removed.
  280. */
  281. int pzl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
  282. {
  283. struct whc_urb *wurb = urb->hcpriv;
  284. struct whc_qset *qset = wurb->qset;
  285. struct whc_std *std, *t;
  286. bool has_qtd = false;
  287. int ret;
  288. unsigned long flags;
  289. spin_lock_irqsave(&whc->lock, flags);
  290. ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
  291. if (ret < 0)
  292. goto out;
  293. list_for_each_entry_safe(std, t, &qset->stds, list_node) {
  294. if (std->urb == urb) {
  295. if (std->qtd)
  296. has_qtd = true;
  297. qset_free_std(whc, std);
  298. } else
  299. std->qtd = NULL; /* so this std is re-added when the qset is */
  300. }
  301. if (has_qtd) {
  302. pzl_qset_remove(whc, qset);
  303. update_pzl_hw_view(whc);
  304. wurb->status = status;
  305. wurb->is_async = false;
  306. queue_work(whc->workqueue, &wurb->dequeue_work);
  307. } else
  308. qset_remove_urb(whc, qset, urb, status);
  309. out:
  310. spin_unlock_irqrestore(&whc->lock, flags);
  311. return ret;
  312. }
  313. /**
  314. * pzl_qset_delete - delete a qset from the PZL
  315. */
  316. void pzl_qset_delete(struct whc *whc, struct whc_qset *qset)
  317. {
  318. qset->remove = 1;
  319. queue_work(whc->workqueue, &whc->periodic_work);
  320. qset_delete(whc, qset);
  321. }
  322. /**
  323. * pzl_init - initialize the periodic zone list
  324. * @whc: the WHCI host controller
  325. */
  326. int pzl_init(struct whc *whc)
  327. {
  328. int i;
  329. whc->pz_list = dma_alloc_coherent(&whc->umc->dev, sizeof(u64) * 16,
  330. &whc->pz_list_dma, GFP_KERNEL);
  331. if (whc->pz_list == NULL)
  332. return -ENOMEM;
  333. /* Set T bit on all elements in PZL. */
  334. for (i = 0; i < 16; i++)
  335. whc->pz_list[i] = cpu_to_le64(QH_LINK_NTDS(8) | QH_LINK_T);
  336. le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
  337. return 0;
  338. }
  339. /**
  340. * pzl_clean_up - free PZL resources
  341. * @whc: the WHCI host controller
  342. *
  343. * The PZL is stopped and empty.
  344. */
  345. void pzl_clean_up(struct whc *whc)
  346. {
  347. if (whc->pz_list)
  348. dma_free_coherent(&whc->umc->dev, sizeof(u64) * 16, whc->pz_list,
  349. whc->pz_list_dma);
  350. }