qset.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Wireless Host Controller (WHC) qset 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. struct whc_qset *qset_alloc(struct whc *whc, gfp_t mem_flags)
  25. {
  26. struct whc_qset *qset;
  27. dma_addr_t dma;
  28. qset = dma_pool_alloc(whc->qset_pool, mem_flags, &dma);
  29. if (qset == NULL)
  30. return NULL;
  31. memset(qset, 0, sizeof(struct whc_qset));
  32. qset->qset_dma = dma;
  33. qset->whc = whc;
  34. INIT_LIST_HEAD(&qset->list_node);
  35. INIT_LIST_HEAD(&qset->stds);
  36. return qset;
  37. }
  38. /**
  39. * qset_fill_qh - fill the static endpoint state in a qset's QHead
  40. * @qset: the qset whose QH needs initializing with static endpoint
  41. * state
  42. * @urb: an urb for a transfer to this endpoint
  43. */
  44. static void qset_fill_qh(struct whc_qset *qset, struct urb *urb)
  45. {
  46. struct usb_device *usb_dev = urb->dev;
  47. struct usb_wireless_ep_comp_descriptor *epcd;
  48. bool is_out;
  49. is_out = usb_pipeout(urb->pipe);
  50. epcd = (struct usb_wireless_ep_comp_descriptor *)qset->ep->extra;
  51. if (epcd) {
  52. qset->max_seq = epcd->bMaxSequence;
  53. qset->max_burst = epcd->bMaxBurst;
  54. } else {
  55. qset->max_seq = 2;
  56. qset->max_burst = 1;
  57. }
  58. qset->qh.info1 = cpu_to_le32(
  59. QH_INFO1_EP(usb_pipeendpoint(urb->pipe))
  60. | (is_out ? QH_INFO1_DIR_OUT : QH_INFO1_DIR_IN)
  61. | usb_pipe_to_qh_type(urb->pipe)
  62. | QH_INFO1_DEV_INFO_IDX(wusb_port_no_to_idx(usb_dev->portnum))
  63. | QH_INFO1_MAX_PKT_LEN(usb_maxpacket(urb->dev, urb->pipe, is_out))
  64. );
  65. qset->qh.info2 = cpu_to_le32(
  66. QH_INFO2_BURST(qset->max_burst)
  67. | QH_INFO2_DBP(0)
  68. | QH_INFO2_MAX_COUNT(3)
  69. | QH_INFO2_MAX_RETRY(3)
  70. | QH_INFO2_MAX_SEQ(qset->max_seq - 1)
  71. );
  72. /* FIXME: where can we obtain these Tx parameters from? Why
  73. * doesn't the chip know what Tx power to use? It knows the Rx
  74. * strength and can presumably guess the Tx power required
  75. * from that? */
  76. qset->qh.info3 = cpu_to_le32(
  77. QH_INFO3_TX_RATE_53_3
  78. | QH_INFO3_TX_PWR(0) /* 0 == max power */
  79. );
  80. }
  81. /**
  82. * qset_clear - clear fields in a qset so it may be reinserted into a
  83. * schedule
  84. */
  85. void qset_clear(struct whc *whc, struct whc_qset *qset)
  86. {
  87. qset->td_start = qset->td_end = qset->ntds = 0;
  88. qset->remove = 0;
  89. qset->qh.link = cpu_to_le32(QH_LINK_NTDS(8) | QH_LINK_T);
  90. qset->qh.status = cpu_to_le16(QH_STATUS_ICUR(qset->td_start));
  91. qset->qh.err_count = 0;
  92. qset->qh.cur_window = cpu_to_le32((1 << qset->max_burst) - 1);
  93. qset->qh.scratch[0] = 0;
  94. qset->qh.scratch[1] = 0;
  95. qset->qh.scratch[2] = 0;
  96. memset(&qset->qh.overlay, 0, sizeof(qset->qh.overlay));
  97. init_completion(&qset->remove_complete);
  98. }
  99. /**
  100. * get_qset - get the qset for an async endpoint
  101. *
  102. * A new qset is created if one does not already exist.
  103. */
  104. struct whc_qset *get_qset(struct whc *whc, struct urb *urb,
  105. gfp_t mem_flags)
  106. {
  107. struct whc_qset *qset;
  108. qset = urb->ep->hcpriv;
  109. if (qset == NULL) {
  110. qset = qset_alloc(whc, mem_flags);
  111. if (qset == NULL)
  112. return NULL;
  113. qset->ep = urb->ep;
  114. urb->ep->hcpriv = qset;
  115. qset_fill_qh(qset, urb);
  116. }
  117. return qset;
  118. }
  119. void qset_remove_complete(struct whc *whc, struct whc_qset *qset)
  120. {
  121. list_del_init(&qset->list_node);
  122. complete(&qset->remove_complete);
  123. }
  124. /**
  125. * qset_add_qtds - add qTDs for an URB to a qset
  126. *
  127. * Returns true if the list (ASL/PZL) must be updated because (for a
  128. * WHCI 0.95 controller) an activated qTD was pointed to be iCur.
  129. */
  130. enum whc_update qset_add_qtds(struct whc *whc, struct whc_qset *qset)
  131. {
  132. struct whc_std *std;
  133. enum whc_update update = 0;
  134. list_for_each_entry(std, &qset->stds, list_node) {
  135. struct whc_qtd *qtd;
  136. uint32_t status;
  137. if (qset->ntds >= WHCI_QSET_TD_MAX
  138. || (qset->pause_after_urb && std->urb != qset->pause_after_urb))
  139. break;
  140. if (std->qtd)
  141. continue; /* already has a qTD */
  142. qtd = std->qtd = &qset->qtd[qset->td_end];
  143. /* Fill in setup bytes for control transfers. */
  144. if (usb_pipecontrol(std->urb->pipe))
  145. memcpy(qtd->setup, std->urb->setup_packet, 8);
  146. status = QTD_STS_ACTIVE | QTD_STS_LEN(std->len);
  147. if (whc_std_last(std) && usb_pipeout(std->urb->pipe))
  148. status |= QTD_STS_LAST_PKT;
  149. /*
  150. * For an IN transfer the iAlt field should be set so
  151. * the h/w will automatically advance to the next
  152. * transfer. However, if there are 8 or more TDs
  153. * remaining in this transfer then iAlt cannot be set
  154. * as it could point to somewhere in this transfer.
  155. */
  156. if (std->ntds_remaining < WHCI_QSET_TD_MAX) {
  157. int ialt;
  158. ialt = (qset->td_end + std->ntds_remaining) % WHCI_QSET_TD_MAX;
  159. status |= QTD_STS_IALT(ialt);
  160. } else if (usb_pipein(std->urb->pipe))
  161. qset->pause_after_urb = std->urb;
  162. if (std->num_pointers)
  163. qtd->options = cpu_to_le32(QTD_OPT_IOC);
  164. else
  165. qtd->options = cpu_to_le32(QTD_OPT_IOC | QTD_OPT_SMALL);
  166. qtd->page_list_ptr = cpu_to_le64(std->dma_addr);
  167. qtd->status = cpu_to_le32(status);
  168. if (QH_STATUS_TO_ICUR(qset->qh.status) == qset->td_end)
  169. update = WHC_UPDATE_UPDATED;
  170. if (++qset->td_end >= WHCI_QSET_TD_MAX)
  171. qset->td_end = 0;
  172. qset->ntds++;
  173. }
  174. return update;
  175. }
  176. /**
  177. * qset_remove_qtd - remove the first qTD from a qset.
  178. *
  179. * The qTD might be still active (if it's part of a IN URB that
  180. * resulted in a short read) so ensure it's deactivated.
  181. */
  182. static void qset_remove_qtd(struct whc *whc, struct whc_qset *qset)
  183. {
  184. qset->qtd[qset->td_start].status = 0;
  185. if (++qset->td_start >= WHCI_QSET_TD_MAX)
  186. qset->td_start = 0;
  187. qset->ntds--;
  188. }
  189. /**
  190. * qset_free_std - remove an sTD and free it.
  191. * @whc: the WHCI host controller
  192. * @std: the sTD to remove and free.
  193. */
  194. void qset_free_std(struct whc *whc, struct whc_std *std)
  195. {
  196. list_del(&std->list_node);
  197. if (std->num_pointers) {
  198. dma_unmap_single(whc->wusbhc.dev, std->dma_addr,
  199. std->num_pointers * sizeof(struct whc_page_list_entry),
  200. DMA_TO_DEVICE);
  201. kfree(std->pl_virt);
  202. }
  203. kfree(std);
  204. }
  205. /**
  206. * qset_remove_qtds - remove an URB's qTDs (and sTDs).
  207. */
  208. static void qset_remove_qtds(struct whc *whc, struct whc_qset *qset,
  209. struct urb *urb)
  210. {
  211. struct whc_std *std, *t;
  212. list_for_each_entry_safe(std, t, &qset->stds, list_node) {
  213. if (std->urb != urb)
  214. break;
  215. if (std->qtd != NULL)
  216. qset_remove_qtd(whc, qset);
  217. qset_free_std(whc, std);
  218. }
  219. }
  220. /**
  221. * qset_free_stds - free any remaining sTDs for an URB.
  222. */
  223. static void qset_free_stds(struct whc_qset *qset, struct urb *urb)
  224. {
  225. struct whc_std *std, *t;
  226. list_for_each_entry_safe(std, t, &qset->stds, list_node) {
  227. if (std->urb == urb)
  228. qset_free_std(qset->whc, std);
  229. }
  230. }
  231. static int qset_fill_page_list(struct whc *whc, struct whc_std *std, gfp_t mem_flags)
  232. {
  233. dma_addr_t dma_addr = std->dma_addr;
  234. dma_addr_t sp, ep;
  235. size_t std_len = std->len;
  236. size_t pl_len;
  237. int p;
  238. sp = ALIGN(dma_addr, WHCI_PAGE_SIZE);
  239. ep = dma_addr + std_len;
  240. std->num_pointers = DIV_ROUND_UP(ep - sp, WHCI_PAGE_SIZE);
  241. pl_len = std->num_pointers * sizeof(struct whc_page_list_entry);
  242. std->pl_virt = kmalloc(pl_len, mem_flags);
  243. if (std->pl_virt == NULL)
  244. return -ENOMEM;
  245. std->dma_addr = dma_map_single(whc->wusbhc.dev, std->pl_virt, pl_len, DMA_TO_DEVICE);
  246. for (p = 0; p < std->num_pointers; p++) {
  247. std->pl_virt[p].buf_ptr = cpu_to_le64(dma_addr);
  248. dma_addr = ALIGN(dma_addr + WHCI_PAGE_SIZE, WHCI_PAGE_SIZE);
  249. }
  250. return 0;
  251. }
  252. /**
  253. * urb_dequeue_work - executes asl/pzl update and gives back the urb to the system.
  254. */
  255. static void urb_dequeue_work(struct work_struct *work)
  256. {
  257. struct whc_urb *wurb = container_of(work, struct whc_urb, dequeue_work);
  258. struct whc_qset *qset = wurb->qset;
  259. struct whc *whc = qset->whc;
  260. unsigned long flags;
  261. if (wurb->is_async == true)
  262. asl_update(whc, WUSBCMD_ASYNC_UPDATED
  263. | WUSBCMD_ASYNC_SYNCED_DB
  264. | WUSBCMD_ASYNC_QSET_RM);
  265. else
  266. pzl_update(whc, WUSBCMD_PERIODIC_UPDATED
  267. | WUSBCMD_PERIODIC_SYNCED_DB
  268. | WUSBCMD_PERIODIC_QSET_RM);
  269. spin_lock_irqsave(&whc->lock, flags);
  270. qset_remove_urb(whc, qset, wurb->urb, wurb->status);
  271. spin_unlock_irqrestore(&whc->lock, flags);
  272. }
  273. /**
  274. * qset_add_urb - add an urb to the qset's queue.
  275. *
  276. * The URB is chopped into sTDs, one for each qTD that will required.
  277. * At least one qTD (and sTD) is required even if the transfer has no
  278. * data (e.g., for some control transfers).
  279. */
  280. int qset_add_urb(struct whc *whc, struct whc_qset *qset, struct urb *urb,
  281. gfp_t mem_flags)
  282. {
  283. struct whc_urb *wurb;
  284. int remaining = urb->transfer_buffer_length;
  285. u64 transfer_dma = urb->transfer_dma;
  286. int ntds_remaining;
  287. ntds_remaining = DIV_ROUND_UP(remaining, QTD_MAX_XFER_SIZE);
  288. if (ntds_remaining == 0)
  289. ntds_remaining = 1;
  290. wurb = kzalloc(sizeof(struct whc_urb), mem_flags);
  291. if (wurb == NULL)
  292. goto err_no_mem;
  293. urb->hcpriv = wurb;
  294. wurb->qset = qset;
  295. wurb->urb = urb;
  296. INIT_WORK(&wurb->dequeue_work, urb_dequeue_work);
  297. while (ntds_remaining) {
  298. struct whc_std *std;
  299. size_t std_len;
  300. std = kmalloc(sizeof(struct whc_std), mem_flags);
  301. if (std == NULL)
  302. goto err_no_mem;
  303. std_len = remaining;
  304. if (std_len > QTD_MAX_XFER_SIZE)
  305. std_len = QTD_MAX_XFER_SIZE;
  306. std->urb = urb;
  307. std->dma_addr = transfer_dma;
  308. std->len = std_len;
  309. std->ntds_remaining = ntds_remaining;
  310. std->qtd = NULL;
  311. INIT_LIST_HEAD(&std->list_node);
  312. list_add_tail(&std->list_node, &qset->stds);
  313. if (std_len > WHCI_PAGE_SIZE) {
  314. if (qset_fill_page_list(whc, std, mem_flags) < 0)
  315. goto err_no_mem;
  316. } else
  317. std->num_pointers = 0;
  318. ntds_remaining--;
  319. remaining -= std_len;
  320. transfer_dma += std_len;
  321. }
  322. return 0;
  323. err_no_mem:
  324. qset_free_stds(qset, urb);
  325. return -ENOMEM;
  326. }
  327. /**
  328. * qset_remove_urb - remove an URB from the urb queue.
  329. *
  330. * The URB is returned to the USB subsystem.
  331. */
  332. void qset_remove_urb(struct whc *whc, struct whc_qset *qset,
  333. struct urb *urb, int status)
  334. {
  335. struct wusbhc *wusbhc = &whc->wusbhc;
  336. struct whc_urb *wurb = urb->hcpriv;
  337. usb_hcd_unlink_urb_from_ep(&wusbhc->usb_hcd, urb);
  338. /* Drop the lock as urb->complete() may enqueue another urb. */
  339. spin_unlock(&whc->lock);
  340. wusbhc_giveback_urb(wusbhc, urb, status);
  341. spin_lock(&whc->lock);
  342. kfree(wurb);
  343. }
  344. /**
  345. * get_urb_status_from_qtd - get the completed urb status from qTD status
  346. * @urb: completed urb
  347. * @status: qTD status
  348. */
  349. static int get_urb_status_from_qtd(struct urb *urb, u32 status)
  350. {
  351. if (status & QTD_STS_HALTED) {
  352. if (status & QTD_STS_DBE)
  353. return usb_pipein(urb->pipe) ? -ENOSR : -ECOMM;
  354. else if (status & QTD_STS_BABBLE)
  355. return -EOVERFLOW;
  356. else if (status & QTD_STS_RCE)
  357. return -ETIME;
  358. return -EPIPE;
  359. }
  360. if (usb_pipein(urb->pipe)
  361. && (urb->transfer_flags & URB_SHORT_NOT_OK)
  362. && urb->actual_length < urb->transfer_buffer_length)
  363. return -EREMOTEIO;
  364. return 0;
  365. }
  366. /**
  367. * process_inactive_qtd - process an inactive (but not halted) qTD.
  368. *
  369. * Update the urb with the transfer bytes from the qTD, if the urb is
  370. * completely transfered or (in the case of an IN only) the LPF is
  371. * set, then the transfer is complete and the urb should be returned
  372. * to the system.
  373. */
  374. void process_inactive_qtd(struct whc *whc, struct whc_qset *qset,
  375. struct whc_qtd *qtd)
  376. {
  377. struct whc_std *std = list_first_entry(&qset->stds, struct whc_std, list_node);
  378. struct urb *urb = std->urb;
  379. uint32_t status;
  380. bool complete;
  381. status = le32_to_cpu(qtd->status);
  382. urb->actual_length += std->len - QTD_STS_TO_LEN(status);
  383. if (usb_pipein(urb->pipe) && (status & QTD_STS_LAST_PKT))
  384. complete = true;
  385. else
  386. complete = whc_std_last(std);
  387. qset_remove_qtd(whc, qset);
  388. qset_free_std(whc, std);
  389. /*
  390. * Transfers for this URB are complete? Then return it to the
  391. * USB subsystem.
  392. */
  393. if (complete) {
  394. qset_remove_qtds(whc, qset, urb);
  395. qset_remove_urb(whc, qset, urb, get_urb_status_from_qtd(urb, status));
  396. /*
  397. * If iAlt isn't valid then the hardware didn't
  398. * advance iCur. Adjust the start and end pointers to
  399. * match iCur.
  400. */
  401. if (!(status & QTD_STS_IALT_VALID))
  402. qset->td_start = qset->td_end
  403. = QH_STATUS_TO_ICUR(le16_to_cpu(qset->qh.status));
  404. qset->pause_after_urb = NULL;
  405. }
  406. }
  407. /**
  408. * process_halted_qtd - process a qset with a halted qtd
  409. *
  410. * Remove all the qTDs for the failed URB and return the failed URB to
  411. * the USB subsystem. Then remove all other qTDs so the qset can be
  412. * removed.
  413. *
  414. * FIXME: this is the point where rate adaptation can be done. If a
  415. * transfer failed because it exceeded the maximum number of retries
  416. * then it could be reactivated with a slower rate without having to
  417. * remove the qset.
  418. */
  419. void process_halted_qtd(struct whc *whc, struct whc_qset *qset,
  420. struct whc_qtd *qtd)
  421. {
  422. struct whc_std *std = list_first_entry(&qset->stds, struct whc_std, list_node);
  423. struct urb *urb = std->urb;
  424. int urb_status;
  425. urb_status = get_urb_status_from_qtd(urb, le32_to_cpu(qtd->status));
  426. qset_remove_qtds(whc, qset, urb);
  427. qset_remove_urb(whc, qset, urb, urb_status);
  428. list_for_each_entry(std, &qset->stds, list_node) {
  429. if (qset->ntds == 0)
  430. break;
  431. qset_remove_qtd(whc, qset);
  432. std->qtd = NULL;
  433. }
  434. qset->remove = 1;
  435. }
  436. void qset_free(struct whc *whc, struct whc_qset *qset)
  437. {
  438. dma_pool_free(whc->qset_pool, qset, qset->qset_dma);
  439. }
  440. /**
  441. * qset_delete - wait for a qset to be unused, then free it.
  442. */
  443. void qset_delete(struct whc *whc, struct whc_qset *qset)
  444. {
  445. wait_for_completion(&qset->remove_complete);
  446. qset_free(whc, qset);
  447. }