pzl.c 10 KB

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