pzl.c 10 KB

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