pzl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. long t;
  165. mutex_lock(&wusbhc->mutex);
  166. if (wusbhc->active) {
  167. whc_write_wusbcmd(whc, wusbcmd, wusbcmd);
  168. t = wait_event_timeout(
  169. whc->periodic_list_wq,
  170. (le_readl(whc->base + WUSBCMD) & WUSBCMD_PERIODIC_UPDATED) == 0,
  171. msecs_to_jiffies(1000));
  172. if (t == 0)
  173. whc_hw_error(whc, "PZL update timeout");
  174. }
  175. mutex_unlock(&wusbhc->mutex);
  176. }
  177. static void update_pzl_hw_view(struct whc *whc)
  178. {
  179. struct whc_qset *qset, *t;
  180. int period;
  181. u64 tmp_qh = 0;
  182. for (period = 0; period < 5; period++) {
  183. list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
  184. whc_qset_set_link_ptr(&qset->qh.link, tmp_qh);
  185. tmp_qh = qset->qset_dma;
  186. qset->in_hw_list = true;
  187. }
  188. update_pzl_pointers(whc, period, tmp_qh);
  189. }
  190. }
  191. /**
  192. * scan_periodic_work - scan the PZL for qsets to process.
  193. *
  194. * Process each qset in the PZL in turn and then signal the WHC that
  195. * the PZL has been updated.
  196. *
  197. * Then start, stop or update the periodic schedule as required.
  198. */
  199. void scan_periodic_work(struct work_struct *work)
  200. {
  201. struct whc *whc = container_of(work, struct whc, periodic_work);
  202. struct whc_qset *qset, *t;
  203. enum whc_update update = 0;
  204. int period;
  205. spin_lock_irq(&whc->lock);
  206. for (period = 4; period >= 0; period--) {
  207. list_for_each_entry_safe(qset, t, &whc->periodic_list[period], list_node) {
  208. if (!qset->in_hw_list)
  209. update |= WHC_UPDATE_ADDED;
  210. update |= pzl_process_qset(whc, qset);
  211. }
  212. }
  213. if (update & (WHC_UPDATE_ADDED | WHC_UPDATE_REMOVED))
  214. update_pzl_hw_view(whc);
  215. spin_unlock_irq(&whc->lock);
  216. if (update) {
  217. uint32_t wusbcmd = WUSBCMD_PERIODIC_UPDATED | WUSBCMD_PERIODIC_SYNCED_DB;
  218. if (update & WHC_UPDATE_REMOVED)
  219. wusbcmd |= WUSBCMD_PERIODIC_QSET_RM;
  220. pzl_update(whc, wusbcmd);
  221. }
  222. /*
  223. * Now that the PZL is updated, complete the removal of any
  224. * removed qsets.
  225. */
  226. spin_lock_irq(&whc->lock);
  227. list_for_each_entry_safe(qset, t, &whc->periodic_removed_list, list_node) {
  228. qset_remove_complete(whc, qset);
  229. }
  230. spin_unlock_irq(&whc->lock);
  231. }
  232. /**
  233. * pzl_urb_enqueue - queue an URB onto the periodic list (PZL)
  234. * @whc: the WHCI host controller
  235. * @urb: the URB to enqueue
  236. * @mem_flags: flags for any memory allocations
  237. *
  238. * The qset for the endpoint is obtained and the urb queued on to it.
  239. *
  240. * Work is scheduled to update the hardware's view of the PZL.
  241. */
  242. int pzl_urb_enqueue(struct whc *whc, struct urb *urb, gfp_t mem_flags)
  243. {
  244. struct whc_qset *qset;
  245. int err;
  246. unsigned long flags;
  247. spin_lock_irqsave(&whc->lock, flags);
  248. qset = get_qset(whc, urb, GFP_ATOMIC);
  249. if (qset == NULL)
  250. err = -ENOMEM;
  251. else
  252. err = qset_add_urb(whc, qset, urb, GFP_ATOMIC);
  253. if (!err) {
  254. usb_hcd_link_urb_to_ep(&whc->wusbhc.usb_hcd, urb);
  255. if (!qset->in_sw_list)
  256. qset_insert_in_sw_list(whc, qset);
  257. }
  258. spin_unlock_irqrestore(&whc->lock, flags);
  259. if (!err)
  260. queue_work(whc->workqueue, &whc->periodic_work);
  261. return 0;
  262. }
  263. /**
  264. * pzl_urb_dequeue - remove an URB (qset) from the periodic list
  265. * @whc: the WHCI host controller
  266. * @urb: the URB to dequeue
  267. * @status: the current status of the URB
  268. *
  269. * URBs that do yet have qTDs can simply be removed from the software
  270. * queue, otherwise the qset must be removed so the qTDs can be safely
  271. * removed.
  272. */
  273. int pzl_urb_dequeue(struct whc *whc, struct urb *urb, int status)
  274. {
  275. struct whc_urb *wurb = urb->hcpriv;
  276. struct whc_qset *qset = wurb->qset;
  277. struct whc_std *std, *t;
  278. int ret;
  279. unsigned long flags;
  280. spin_lock_irqsave(&whc->lock, flags);
  281. ret = usb_hcd_check_unlink_urb(&whc->wusbhc.usb_hcd, urb, status);
  282. if (ret < 0)
  283. goto out;
  284. list_for_each_entry_safe(std, t, &qset->stds, list_node) {
  285. if (std->urb == urb)
  286. qset_free_std(whc, std);
  287. else
  288. std->qtd = NULL; /* so this std is re-added when the qset is */
  289. }
  290. pzl_qset_remove(whc, qset);
  291. wurb->status = status;
  292. wurb->is_async = false;
  293. queue_work(whc->workqueue, &wurb->dequeue_work);
  294. out:
  295. spin_unlock_irqrestore(&whc->lock, flags);
  296. return ret;
  297. }
  298. /**
  299. * pzl_qset_delete - delete a qset from the PZL
  300. */
  301. void pzl_qset_delete(struct whc *whc, struct whc_qset *qset)
  302. {
  303. qset->remove = 1;
  304. queue_work(whc->workqueue, &whc->periodic_work);
  305. qset_delete(whc, qset);
  306. }
  307. /**
  308. * pzl_init - initialize the periodic zone list
  309. * @whc: the WHCI host controller
  310. */
  311. int pzl_init(struct whc *whc)
  312. {
  313. int i;
  314. whc->pz_list = dma_alloc_coherent(&whc->umc->dev, sizeof(u64) * 16,
  315. &whc->pz_list_dma, GFP_KERNEL);
  316. if (whc->pz_list == NULL)
  317. return -ENOMEM;
  318. /* Set T bit on all elements in PZL. */
  319. for (i = 0; i < 16; i++)
  320. whc->pz_list[i] = cpu_to_le64(QH_LINK_NTDS(8) | QH_LINK_T);
  321. le_writeq(whc->pz_list_dma, whc->base + WUSBPERIODICLISTBASE);
  322. return 0;
  323. }
  324. /**
  325. * pzl_clean_up - free PZL resources
  326. * @whc: the WHCI host controller
  327. *
  328. * The PZL is stopped and empty.
  329. */
  330. void pzl_clean_up(struct whc *whc)
  331. {
  332. if (whc->pz_list)
  333. dma_free_coherent(&whc->umc->dev, sizeof(u64) * 16, whc->pz_list,
  334. whc->pz_list_dma);
  335. }