pzl.c 9.6 KB

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