qset.c 14 KB

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