pzl.c 9.4 KB

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