pzl.c 9.7 KB

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