ehci-q.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * Copyright (C) 2001-2004 by David Brownell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* this file is part of ehci-hcd.c */
  19. /*-------------------------------------------------------------------------*/
  20. /*
  21. * EHCI hardware queue manipulation ... the core. QH/QTD manipulation.
  22. *
  23. * Control, bulk, and interrupt traffic all use "qh" lists. They list "qtd"
  24. * entries describing USB transactions, max 16-20kB/entry (with 4kB-aligned
  25. * buffers needed for the larger number). We use one QH per endpoint, queue
  26. * multiple urbs (all three types) per endpoint. URBs may need several qtds.
  27. *
  28. * ISO traffic uses "ISO TD" (itd, and sitd) records, and (along with
  29. * interrupts) needs careful scheduling. Performance improvements can be
  30. * an ongoing challenge. That's in "ehci-sched.c".
  31. *
  32. * USB 1.1 devices are handled (a) by "companion" OHCI or UHCI root hubs,
  33. * or otherwise through transaction translators (TTs) in USB 2.0 hubs using
  34. * (b) special fields in qh entries or (c) split iso entries. TTs will
  35. * buffer low/full speed data so the host collects it at high speed.
  36. */
  37. /*-------------------------------------------------------------------------*/
  38. /* fill a qtd, returning how much of the buffer we were able to queue up */
  39. static int
  40. qtd_fill(struct ehci_hcd *ehci, struct ehci_qtd *qtd, dma_addr_t buf,
  41. size_t len, int token, int maxpacket)
  42. {
  43. int i, count;
  44. u64 addr = buf;
  45. /* one buffer entry per 4K ... first might be short or unaligned */
  46. qtd->hw_buf[0] = cpu_to_hc32(ehci, (u32)addr);
  47. qtd->hw_buf_hi[0] = cpu_to_hc32(ehci, (u32)(addr >> 32));
  48. count = 0x1000 - (buf & 0x0fff); /* rest of that page */
  49. if (likely (len < count)) /* ... iff needed */
  50. count = len;
  51. else {
  52. buf += 0x1000;
  53. buf &= ~0x0fff;
  54. /* per-qtd limit: from 16K to 20K (best alignment) */
  55. for (i = 1; count < len && i < 5; i++) {
  56. addr = buf;
  57. qtd->hw_buf[i] = cpu_to_hc32(ehci, (u32)addr);
  58. qtd->hw_buf_hi[i] = cpu_to_hc32(ehci,
  59. (u32)(addr >> 32));
  60. buf += 0x1000;
  61. if ((count + 0x1000) < len)
  62. count += 0x1000;
  63. else
  64. count = len;
  65. }
  66. /* short packets may only terminate transfers */
  67. if (count != len)
  68. count -= (count % maxpacket);
  69. }
  70. qtd->hw_token = cpu_to_hc32(ehci, (count << 16) | token);
  71. qtd->length = count;
  72. return count;
  73. }
  74. /*-------------------------------------------------------------------------*/
  75. static inline void
  76. qh_update (struct ehci_hcd *ehci, struct ehci_qh *qh, struct ehci_qtd *qtd)
  77. {
  78. /* writes to an active overlay are unsafe */
  79. BUG_ON(qh->qh_state != QH_STATE_IDLE);
  80. qh->hw_qtd_next = QTD_NEXT(ehci, qtd->qtd_dma);
  81. qh->hw_alt_next = EHCI_LIST_END(ehci);
  82. /* Except for control endpoints, we make hardware maintain data
  83. * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
  84. * and set the pseudo-toggle in udev. Only usb_clear_halt() will
  85. * ever clear it.
  86. */
  87. if (!(qh->hw_info1 & cpu_to_hc32(ehci, 1 << 14))) {
  88. unsigned is_out, epnum;
  89. is_out = !(qtd->hw_token & cpu_to_hc32(ehci, 1 << 8));
  90. epnum = (hc32_to_cpup(ehci, &qh->hw_info1) >> 8) & 0x0f;
  91. if (unlikely (!usb_gettoggle (qh->dev, epnum, is_out))) {
  92. qh->hw_token &= ~cpu_to_hc32(ehci, QTD_TOGGLE);
  93. usb_settoggle (qh->dev, epnum, is_out, 1);
  94. }
  95. }
  96. /* HC must see latest qtd and qh data before we clear ACTIVE+HALT */
  97. wmb ();
  98. qh->hw_token &= cpu_to_hc32(ehci, QTD_TOGGLE | QTD_STS_PING);
  99. }
  100. /* if it weren't for a common silicon quirk (writing the dummy into the qh
  101. * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
  102. * recovery (including urb dequeue) would need software changes to a QH...
  103. */
  104. static void
  105. qh_refresh (struct ehci_hcd *ehci, struct ehci_qh *qh)
  106. {
  107. struct ehci_qtd *qtd;
  108. if (list_empty (&qh->qtd_list))
  109. qtd = qh->dummy;
  110. else {
  111. qtd = list_entry (qh->qtd_list.next,
  112. struct ehci_qtd, qtd_list);
  113. /* first qtd may already be partially processed */
  114. if (cpu_to_hc32(ehci, qtd->qtd_dma) == qh->hw_current)
  115. qtd = NULL;
  116. }
  117. if (qtd)
  118. qh_update (ehci, qh, qtd);
  119. }
  120. /*-------------------------------------------------------------------------*/
  121. static void qtd_copy_status (
  122. struct ehci_hcd *ehci,
  123. struct urb *urb,
  124. size_t length,
  125. u32 token
  126. )
  127. {
  128. /* count IN/OUT bytes, not SETUP (even short packets) */
  129. if (likely (QTD_PID (token) != 2))
  130. urb->actual_length += length - QTD_LENGTH (token);
  131. /* don't modify error codes */
  132. if (unlikely (urb->status != -EINPROGRESS))
  133. return;
  134. /* force cleanup after short read; not always an error */
  135. if (unlikely (IS_SHORT_READ (token)))
  136. urb->status = -EREMOTEIO;
  137. /* serious "can't proceed" faults reported by the hardware */
  138. if (token & QTD_STS_HALT) {
  139. if (token & QTD_STS_BABBLE) {
  140. /* FIXME "must" disable babbling device's port too */
  141. urb->status = -EOVERFLOW;
  142. } else if (token & QTD_STS_MMF) {
  143. /* fs/ls interrupt xfer missed the complete-split */
  144. urb->status = -EPROTO;
  145. } else if (token & QTD_STS_DBE) {
  146. urb->status = (QTD_PID (token) == 1) /* IN ? */
  147. ? -ENOSR /* hc couldn't read data */
  148. : -ECOMM; /* hc couldn't write data */
  149. } else if (token & QTD_STS_XACT) {
  150. /* timeout, bad crc, wrong PID, etc; retried */
  151. if (QTD_CERR (token))
  152. urb->status = -EPIPE;
  153. else {
  154. ehci_dbg (ehci, "devpath %s ep%d%s 3strikes\n",
  155. urb->dev->devpath,
  156. usb_pipeendpoint (urb->pipe),
  157. usb_pipein (urb->pipe) ? "in" : "out");
  158. urb->status = -EPROTO;
  159. }
  160. /* CERR nonzero + no errors + halt --> stall */
  161. } else if (QTD_CERR (token))
  162. urb->status = -EPIPE;
  163. else /* unknown */
  164. urb->status = -EPROTO;
  165. ehci_vdbg (ehci,
  166. "dev%d ep%d%s qtd token %08x --> status %d\n",
  167. usb_pipedevice (urb->pipe),
  168. usb_pipeendpoint (urb->pipe),
  169. usb_pipein (urb->pipe) ? "in" : "out",
  170. token, urb->status);
  171. /* if async CSPLIT failed, try cleaning out the TT buffer */
  172. if (urb->status != -EPIPE
  173. && urb->dev->tt && !usb_pipeint (urb->pipe)
  174. && ((token & QTD_STS_MMF) != 0
  175. || QTD_CERR(token) == 0)
  176. && (!ehci_is_TDI(ehci)
  177. || urb->dev->tt->hub !=
  178. ehci_to_hcd(ehci)->self.root_hub)) {
  179. #ifdef DEBUG
  180. struct usb_device *tt = urb->dev->tt->hub;
  181. dev_dbg (&tt->dev,
  182. "clear tt buffer port %d, a%d ep%d t%08x\n",
  183. urb->dev->ttport, urb->dev->devnum,
  184. usb_pipeendpoint (urb->pipe), token);
  185. #endif /* DEBUG */
  186. usb_hub_tt_clear_buffer (urb->dev, urb->pipe);
  187. }
  188. }
  189. }
  190. static void
  191. ehci_urb_done (struct ehci_hcd *ehci, struct urb *urb)
  192. __releases(ehci->lock)
  193. __acquires(ehci->lock)
  194. {
  195. if (likely (urb->hcpriv != NULL)) {
  196. struct ehci_qh *qh = (struct ehci_qh *) urb->hcpriv;
  197. /* S-mask in a QH means it's an interrupt urb */
  198. if ((qh->hw_info2 & cpu_to_hc32(ehci, QH_SMASK)) != 0) {
  199. /* ... update hc-wide periodic stats (for usbfs) */
  200. ehci_to_hcd(ehci)->self.bandwidth_int_reqs--;
  201. }
  202. qh_put (qh);
  203. }
  204. spin_lock (&urb->lock);
  205. switch (urb->status) {
  206. case -EINPROGRESS: /* success */
  207. urb->status = 0;
  208. default: /* fault */
  209. COUNT (ehci->stats.complete);
  210. break;
  211. case -EREMOTEIO: /* fault or normal */
  212. if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
  213. urb->status = 0;
  214. COUNT (ehci->stats.complete);
  215. break;
  216. case -ECONNRESET: /* canceled */
  217. case -ENOENT:
  218. COUNT (ehci->stats.unlink);
  219. break;
  220. }
  221. spin_unlock (&urb->lock);
  222. #ifdef EHCI_URB_TRACE
  223. ehci_dbg (ehci,
  224. "%s %s urb %p ep%d%s status %d len %d/%d\n",
  225. __FUNCTION__, urb->dev->devpath, urb,
  226. usb_pipeendpoint (urb->pipe),
  227. usb_pipein (urb->pipe) ? "in" : "out",
  228. urb->status,
  229. urb->actual_length, urb->transfer_buffer_length);
  230. #endif
  231. /* complete() can reenter this HCD */
  232. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  233. spin_unlock (&ehci->lock);
  234. usb_hcd_giveback_urb (ehci_to_hcd(ehci), urb);
  235. spin_lock (&ehci->lock);
  236. }
  237. static void start_unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh);
  238. static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh);
  239. static void intr_deschedule (struct ehci_hcd *ehci, struct ehci_qh *qh);
  240. static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh);
  241. /*
  242. * Process and free completed qtds for a qh, returning URBs to drivers.
  243. * Chases up to qh->hw_current. Returns number of completions called,
  244. * indicating how much "real" work we did.
  245. */
  246. static unsigned
  247. qh_completions (struct ehci_hcd *ehci, struct ehci_qh *qh)
  248. {
  249. struct ehci_qtd *last = NULL, *end = qh->dummy;
  250. struct list_head *entry, *tmp;
  251. int stopped;
  252. unsigned count = 0;
  253. int do_status = 0;
  254. u8 state;
  255. u32 halt = HALT_BIT(ehci);
  256. if (unlikely (list_empty (&qh->qtd_list)))
  257. return count;
  258. /* completions (or tasks on other cpus) must never clobber HALT
  259. * till we've gone through and cleaned everything up, even when
  260. * they add urbs to this qh's queue or mark them for unlinking.
  261. *
  262. * NOTE: unlinking expects to be done in queue order.
  263. */
  264. state = qh->qh_state;
  265. qh->qh_state = QH_STATE_COMPLETING;
  266. stopped = (state == QH_STATE_IDLE);
  267. /* remove de-activated QTDs from front of queue.
  268. * after faults (including short reads), cleanup this urb
  269. * then let the queue advance.
  270. * if queue is stopped, handles unlinks.
  271. */
  272. list_for_each_safe (entry, tmp, &qh->qtd_list) {
  273. struct ehci_qtd *qtd;
  274. struct urb *urb;
  275. u32 token = 0;
  276. qtd = list_entry (entry, struct ehci_qtd, qtd_list);
  277. urb = qtd->urb;
  278. /* clean up any state from previous QTD ...*/
  279. if (last) {
  280. if (likely (last->urb != urb)) {
  281. ehci_urb_done (ehci, last->urb);
  282. count++;
  283. }
  284. ehci_qtd_free (ehci, last);
  285. last = NULL;
  286. }
  287. /* ignore urbs submitted during completions we reported */
  288. if (qtd == end)
  289. break;
  290. /* hardware copies qtd out of qh overlay */
  291. rmb ();
  292. token = hc32_to_cpu(ehci, qtd->hw_token);
  293. /* always clean up qtds the hc de-activated */
  294. if ((token & QTD_STS_ACTIVE) == 0) {
  295. if ((token & QTD_STS_HALT) != 0) {
  296. stopped = 1;
  297. /* magic dummy for some short reads; qh won't advance.
  298. * that silicon quirk can kick in with this dummy too.
  299. */
  300. } else if (IS_SHORT_READ (token)
  301. && !(qtd->hw_alt_next
  302. & EHCI_LIST_END(ehci))) {
  303. stopped = 1;
  304. goto halt;
  305. }
  306. /* stop scanning when we reach qtds the hc is using */
  307. } else if (likely (!stopped
  308. && HC_IS_RUNNING (ehci_to_hcd(ehci)->state))) {
  309. break;
  310. } else {
  311. stopped = 1;
  312. if (unlikely (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state)))
  313. urb->status = -ESHUTDOWN;
  314. /* ignore active urbs unless some previous qtd
  315. * for the urb faulted (including short read) or
  316. * its urb was canceled. we may patch qh or qtds.
  317. */
  318. if (likely (urb->status == -EINPROGRESS))
  319. continue;
  320. /* issue status after short control reads */
  321. if (unlikely (do_status != 0)
  322. && QTD_PID (token) == 0 /* OUT */) {
  323. do_status = 0;
  324. continue;
  325. }
  326. /* token in overlay may be most current */
  327. if (state == QH_STATE_IDLE
  328. && cpu_to_hc32(ehci, qtd->qtd_dma)
  329. == qh->hw_current)
  330. token = hc32_to_cpu(ehci, qh->hw_token);
  331. /* force halt for unlinked or blocked qh, so we'll
  332. * patch the qh later and so that completions can't
  333. * activate it while we "know" it's stopped.
  334. */
  335. if ((halt & qh->hw_token) == 0) {
  336. halt:
  337. qh->hw_token |= halt;
  338. wmb ();
  339. }
  340. }
  341. /* remove it from the queue */
  342. spin_lock (&urb->lock);
  343. qtd_copy_status (ehci, urb, qtd->length, token);
  344. if (unlikely(urb->status == -EREMOTEIO)) {
  345. do_status = usb_pipecontrol(urb->pipe);
  346. urb->status = 0;
  347. }
  348. spin_unlock (&urb->lock);
  349. if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
  350. last = list_entry (qtd->qtd_list.prev,
  351. struct ehci_qtd, qtd_list);
  352. last->hw_next = qtd->hw_next;
  353. }
  354. list_del (&qtd->qtd_list);
  355. last = qtd;
  356. }
  357. /* last urb's completion might still need calling */
  358. if (likely (last != NULL)) {
  359. ehci_urb_done (ehci, last->urb);
  360. count++;
  361. ehci_qtd_free (ehci, last);
  362. }
  363. /* restore original state; caller must unlink or relink */
  364. qh->qh_state = state;
  365. /* be sure the hardware's done with the qh before refreshing
  366. * it after fault cleanup, or recovering from silicon wrongly
  367. * overlaying the dummy qtd (which reduces DMA chatter).
  368. */
  369. if (stopped != 0 || qh->hw_qtd_next == EHCI_LIST_END(ehci)) {
  370. switch (state) {
  371. case QH_STATE_IDLE:
  372. qh_refresh(ehci, qh);
  373. break;
  374. case QH_STATE_LINKED:
  375. /* should be rare for periodic transfers,
  376. * except maybe high bandwidth ...
  377. */
  378. if ((cpu_to_hc32(ehci, QH_SMASK)
  379. & qh->hw_info2) != 0) {
  380. intr_deschedule (ehci, qh);
  381. (void) qh_schedule (ehci, qh);
  382. } else
  383. unlink_async (ehci, qh);
  384. break;
  385. /* otherwise, unlink already started */
  386. }
  387. }
  388. return count;
  389. }
  390. /*-------------------------------------------------------------------------*/
  391. // high bandwidth multiplier, as encoded in highspeed endpoint descriptors
  392. #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  393. // ... and packet size, for any kind of endpoint descriptor
  394. #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  395. /*
  396. * reverse of qh_urb_transaction: free a list of TDs.
  397. * used for cleanup after errors, before HC sees an URB's TDs.
  398. */
  399. static void qtd_list_free (
  400. struct ehci_hcd *ehci,
  401. struct urb *urb,
  402. struct list_head *qtd_list
  403. ) {
  404. struct list_head *entry, *temp;
  405. list_for_each_safe (entry, temp, qtd_list) {
  406. struct ehci_qtd *qtd;
  407. qtd = list_entry (entry, struct ehci_qtd, qtd_list);
  408. list_del (&qtd->qtd_list);
  409. ehci_qtd_free (ehci, qtd);
  410. }
  411. }
  412. /*
  413. * create a list of filled qtds for this URB; won't link into qh.
  414. */
  415. static struct list_head *
  416. qh_urb_transaction (
  417. struct ehci_hcd *ehci,
  418. struct urb *urb,
  419. struct list_head *head,
  420. gfp_t flags
  421. ) {
  422. struct ehci_qtd *qtd, *qtd_prev;
  423. dma_addr_t buf;
  424. int len, maxpacket;
  425. int is_input;
  426. u32 token;
  427. /*
  428. * URBs map to sequences of QTDs: one logical transaction
  429. */
  430. qtd = ehci_qtd_alloc (ehci, flags);
  431. if (unlikely (!qtd))
  432. return NULL;
  433. list_add_tail (&qtd->qtd_list, head);
  434. qtd->urb = urb;
  435. token = QTD_STS_ACTIVE;
  436. token |= (EHCI_TUNE_CERR << 10);
  437. /* for split transactions, SplitXState initialized to zero */
  438. len = urb->transfer_buffer_length;
  439. is_input = usb_pipein (urb->pipe);
  440. if (usb_pipecontrol (urb->pipe)) {
  441. /* SETUP pid */
  442. qtd_fill(ehci, qtd, urb->setup_dma,
  443. sizeof (struct usb_ctrlrequest),
  444. token | (2 /* "setup" */ << 8), 8);
  445. /* ... and always at least one more pid */
  446. token ^= QTD_TOGGLE;
  447. qtd_prev = qtd;
  448. qtd = ehci_qtd_alloc (ehci, flags);
  449. if (unlikely (!qtd))
  450. goto cleanup;
  451. qtd->urb = urb;
  452. qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
  453. list_add_tail (&qtd->qtd_list, head);
  454. /* for zero length DATA stages, STATUS is always IN */
  455. if (len == 0)
  456. token |= (1 /* "in" */ << 8);
  457. }
  458. /*
  459. * data transfer stage: buffer setup
  460. */
  461. buf = urb->transfer_dma;
  462. if (is_input)
  463. token |= (1 /* "in" */ << 8);
  464. /* else it's already initted to "out" pid (0 << 8) */
  465. maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
  466. /*
  467. * buffer gets wrapped in one or more qtds;
  468. * last one may be "short" (including zero len)
  469. * and may serve as a control status ack
  470. */
  471. for (;;) {
  472. int this_qtd_len;
  473. this_qtd_len = qtd_fill(ehci, qtd, buf, len, token, maxpacket);
  474. len -= this_qtd_len;
  475. buf += this_qtd_len;
  476. if (is_input)
  477. qtd->hw_alt_next = ehci->async->hw_alt_next;
  478. /* qh makes control packets use qtd toggle; maybe switch it */
  479. if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
  480. token ^= QTD_TOGGLE;
  481. if (likely (len <= 0))
  482. break;
  483. qtd_prev = qtd;
  484. qtd = ehci_qtd_alloc (ehci, flags);
  485. if (unlikely (!qtd))
  486. goto cleanup;
  487. qtd->urb = urb;
  488. qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
  489. list_add_tail (&qtd->qtd_list, head);
  490. }
  491. /* unless the bulk/interrupt caller wants a chance to clean
  492. * up after short reads, hc should advance qh past this urb
  493. */
  494. if (likely ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
  495. || usb_pipecontrol (urb->pipe)))
  496. qtd->hw_alt_next = EHCI_LIST_END(ehci);
  497. /*
  498. * control requests may need a terminating data "status" ack;
  499. * bulk ones may need a terminating short packet (zero length).
  500. */
  501. if (likely (urb->transfer_buffer_length != 0)) {
  502. int one_more = 0;
  503. if (usb_pipecontrol (urb->pipe)) {
  504. one_more = 1;
  505. token ^= 0x0100; /* "in" <--> "out" */
  506. token |= QTD_TOGGLE; /* force DATA1 */
  507. } else if (usb_pipebulk (urb->pipe)
  508. && (urb->transfer_flags & URB_ZERO_PACKET)
  509. && !(urb->transfer_buffer_length % maxpacket)) {
  510. one_more = 1;
  511. }
  512. if (one_more) {
  513. qtd_prev = qtd;
  514. qtd = ehci_qtd_alloc (ehci, flags);
  515. if (unlikely (!qtd))
  516. goto cleanup;
  517. qtd->urb = urb;
  518. qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
  519. list_add_tail (&qtd->qtd_list, head);
  520. /* never any data in such packets */
  521. qtd_fill(ehci, qtd, 0, 0, token, 0);
  522. }
  523. }
  524. /* by default, enable interrupt on urb completion */
  525. if (likely (!(urb->transfer_flags & URB_NO_INTERRUPT)))
  526. qtd->hw_token |= cpu_to_hc32(ehci, QTD_IOC);
  527. return head;
  528. cleanup:
  529. qtd_list_free (ehci, urb, head);
  530. return NULL;
  531. }
  532. /*-------------------------------------------------------------------------*/
  533. // Would be best to create all qh's from config descriptors,
  534. // when each interface/altsetting is established. Unlink
  535. // any previous qh and cancel its urbs first; endpoints are
  536. // implicitly reset then (data toggle too).
  537. // That'd mean updating how usbcore talks to HCDs. (2.7?)
  538. /*
  539. * Each QH holds a qtd list; a QH is used for everything except iso.
  540. *
  541. * For interrupt urbs, the scheduler must set the microframe scheduling
  542. * mask(s) each time the QH gets scheduled. For highspeed, that's
  543. * just one microframe in the s-mask. For split interrupt transactions
  544. * there are additional complications: c-mask, maybe FSTNs.
  545. */
  546. static struct ehci_qh *
  547. qh_make (
  548. struct ehci_hcd *ehci,
  549. struct urb *urb,
  550. gfp_t flags
  551. ) {
  552. struct ehci_qh *qh = ehci_qh_alloc (ehci, flags);
  553. u32 info1 = 0, info2 = 0;
  554. int is_input, type;
  555. int maxp = 0;
  556. if (!qh)
  557. return qh;
  558. /*
  559. * init endpoint/device data for this QH
  560. */
  561. info1 |= usb_pipeendpoint (urb->pipe) << 8;
  562. info1 |= usb_pipedevice (urb->pipe) << 0;
  563. is_input = usb_pipein (urb->pipe);
  564. type = usb_pipetype (urb->pipe);
  565. maxp = usb_maxpacket (urb->dev, urb->pipe, !is_input);
  566. /* Compute interrupt scheduling parameters just once, and save.
  567. * - allowing for high bandwidth, how many nsec/uframe are used?
  568. * - split transactions need a second CSPLIT uframe; same question
  569. * - splits also need a schedule gap (for full/low speed I/O)
  570. * - qh has a polling interval
  571. *
  572. * For control/bulk requests, the HC or TT handles these.
  573. */
  574. if (type == PIPE_INTERRUPT) {
  575. qh->usecs = NS_TO_US (usb_calc_bus_time (USB_SPEED_HIGH, is_input, 0,
  576. hb_mult (maxp) * max_packet (maxp)));
  577. qh->start = NO_FRAME;
  578. if (urb->dev->speed == USB_SPEED_HIGH) {
  579. qh->c_usecs = 0;
  580. qh->gap_uf = 0;
  581. qh->period = urb->interval >> 3;
  582. if (qh->period == 0 && urb->interval != 1) {
  583. /* NOTE interval 2 or 4 uframes could work.
  584. * But interval 1 scheduling is simpler, and
  585. * includes high bandwidth.
  586. */
  587. dbg ("intr period %d uframes, NYET!",
  588. urb->interval);
  589. goto done;
  590. }
  591. } else {
  592. struct usb_tt *tt = urb->dev->tt;
  593. int think_time;
  594. /* gap is f(FS/LS transfer times) */
  595. qh->gap_uf = 1 + usb_calc_bus_time (urb->dev->speed,
  596. is_input, 0, maxp) / (125 * 1000);
  597. /* FIXME this just approximates SPLIT/CSPLIT times */
  598. if (is_input) { // SPLIT, gap, CSPLIT+DATA
  599. qh->c_usecs = qh->usecs + HS_USECS (0);
  600. qh->usecs = HS_USECS (1);
  601. } else { // SPLIT+DATA, gap, CSPLIT
  602. qh->usecs += HS_USECS (1);
  603. qh->c_usecs = HS_USECS (0);
  604. }
  605. think_time = tt ? tt->think_time : 0;
  606. qh->tt_usecs = NS_TO_US (think_time +
  607. usb_calc_bus_time (urb->dev->speed,
  608. is_input, 0, max_packet (maxp)));
  609. qh->period = urb->interval;
  610. }
  611. }
  612. /* support for tt scheduling, and access to toggles */
  613. qh->dev = urb->dev;
  614. /* using TT? */
  615. switch (urb->dev->speed) {
  616. case USB_SPEED_LOW:
  617. info1 |= (1 << 12); /* EPS "low" */
  618. /* FALL THROUGH */
  619. case USB_SPEED_FULL:
  620. /* EPS 0 means "full" */
  621. if (type != PIPE_INTERRUPT)
  622. info1 |= (EHCI_TUNE_RL_TT << 28);
  623. if (type == PIPE_CONTROL) {
  624. info1 |= (1 << 27); /* for TT */
  625. info1 |= 1 << 14; /* toggle from qtd */
  626. }
  627. info1 |= maxp << 16;
  628. info2 |= (EHCI_TUNE_MULT_TT << 30);
  629. /* Some Freescale processors have an erratum in which the
  630. * port number in the queue head was 0..N-1 instead of 1..N.
  631. */
  632. if (ehci_has_fsl_portno_bug(ehci))
  633. info2 |= (urb->dev->ttport-1) << 23;
  634. else
  635. info2 |= urb->dev->ttport << 23;
  636. /* set the address of the TT; for TDI's integrated
  637. * root hub tt, leave it zeroed.
  638. */
  639. if (!ehci_is_TDI(ehci)
  640. || urb->dev->tt->hub !=
  641. ehci_to_hcd(ehci)->self.root_hub)
  642. info2 |= urb->dev->tt->hub->devnum << 16;
  643. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
  644. break;
  645. case USB_SPEED_HIGH: /* no TT involved */
  646. info1 |= (2 << 12); /* EPS "high" */
  647. if (type == PIPE_CONTROL) {
  648. info1 |= (EHCI_TUNE_RL_HS << 28);
  649. info1 |= 64 << 16; /* usb2 fixed maxpacket */
  650. info1 |= 1 << 14; /* toggle from qtd */
  651. info2 |= (EHCI_TUNE_MULT_HS << 30);
  652. } else if (type == PIPE_BULK) {
  653. info1 |= (EHCI_TUNE_RL_HS << 28);
  654. info1 |= 512 << 16; /* usb2 fixed maxpacket */
  655. info2 |= (EHCI_TUNE_MULT_HS << 30);
  656. } else { /* PIPE_INTERRUPT */
  657. info1 |= max_packet (maxp) << 16;
  658. info2 |= hb_mult (maxp) << 30;
  659. }
  660. break;
  661. default:
  662. dbg ("bogus dev %p speed %d", urb->dev, urb->dev->speed);
  663. done:
  664. qh_put (qh);
  665. return NULL;
  666. }
  667. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
  668. /* init as live, toggle clear, advance to dummy */
  669. qh->qh_state = QH_STATE_IDLE;
  670. qh->hw_info1 = cpu_to_hc32(ehci, info1);
  671. qh->hw_info2 = cpu_to_hc32(ehci, info2);
  672. usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), !is_input, 1);
  673. qh_refresh (ehci, qh);
  674. return qh;
  675. }
  676. /*-------------------------------------------------------------------------*/
  677. /* move qh (and its qtds) onto async queue; maybe enable queue. */
  678. static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
  679. {
  680. __hc32 dma = QH_NEXT(ehci, qh->qh_dma);
  681. struct ehci_qh *head;
  682. /* (re)start the async schedule? */
  683. head = ehci->async;
  684. timer_action_done (ehci, TIMER_ASYNC_OFF);
  685. if (!head->qh_next.qh) {
  686. u32 cmd = ehci_readl(ehci, &ehci->regs->command);
  687. if (!(cmd & CMD_ASE)) {
  688. /* in case a clear of CMD_ASE didn't take yet */
  689. (void)handshake(ehci, &ehci->regs->status,
  690. STS_ASS, 0, 150);
  691. cmd |= CMD_ASE | CMD_RUN;
  692. ehci_writel(ehci, cmd, &ehci->regs->command);
  693. ehci_to_hcd(ehci)->state = HC_STATE_RUNNING;
  694. /* posted write need not be known to HC yet ... */
  695. }
  696. }
  697. /* clear halt and/or toggle; and maybe recover from silicon quirk */
  698. if (qh->qh_state == QH_STATE_IDLE)
  699. qh_refresh (ehci, qh);
  700. /* splice right after start */
  701. qh->qh_next = head->qh_next;
  702. qh->hw_next = head->hw_next;
  703. wmb ();
  704. head->qh_next.qh = qh;
  705. head->hw_next = dma;
  706. qh->qh_state = QH_STATE_LINKED;
  707. /* qtd completions reported later by interrupt */
  708. }
  709. /*-------------------------------------------------------------------------*/
  710. /*
  711. * For control/bulk/interrupt, return QH with these TDs appended.
  712. * Allocates and initializes the QH if necessary.
  713. * Returns null if it can't allocate a QH it needs to.
  714. * If the QH has TDs (urbs) already, that's great.
  715. */
  716. static struct ehci_qh *qh_append_tds (
  717. struct ehci_hcd *ehci,
  718. struct urb *urb,
  719. struct list_head *qtd_list,
  720. int epnum,
  721. void **ptr
  722. )
  723. {
  724. struct ehci_qh *qh = NULL;
  725. u32 qh_addr_mask = cpu_to_hc32(ehci, 0x7f);
  726. qh = (struct ehci_qh *) *ptr;
  727. if (unlikely (qh == NULL)) {
  728. /* can't sleep here, we have ehci->lock... */
  729. qh = qh_make (ehci, urb, GFP_ATOMIC);
  730. *ptr = qh;
  731. }
  732. if (likely (qh != NULL)) {
  733. struct ehci_qtd *qtd;
  734. if (unlikely (list_empty (qtd_list)))
  735. qtd = NULL;
  736. else
  737. qtd = list_entry (qtd_list->next, struct ehci_qtd,
  738. qtd_list);
  739. /* control qh may need patching ... */
  740. if (unlikely (epnum == 0)) {
  741. /* usb_reset_device() briefly reverts to address 0 */
  742. if (usb_pipedevice (urb->pipe) == 0)
  743. qh->hw_info1 &= ~qh_addr_mask;
  744. }
  745. /* just one way to queue requests: swap with the dummy qtd.
  746. * only hc or qh_refresh() ever modify the overlay.
  747. */
  748. if (likely (qtd != NULL)) {
  749. struct ehci_qtd *dummy;
  750. dma_addr_t dma;
  751. __hc32 token;
  752. /* to avoid racing the HC, use the dummy td instead of
  753. * the first td of our list (becomes new dummy). both
  754. * tds stay deactivated until we're done, when the
  755. * HC is allowed to fetch the old dummy (4.10.2).
  756. */
  757. token = qtd->hw_token;
  758. qtd->hw_token = HALT_BIT(ehci);
  759. wmb ();
  760. dummy = qh->dummy;
  761. dma = dummy->qtd_dma;
  762. *dummy = *qtd;
  763. dummy->qtd_dma = dma;
  764. list_del (&qtd->qtd_list);
  765. list_add (&dummy->qtd_list, qtd_list);
  766. __list_splice (qtd_list, qh->qtd_list.prev);
  767. ehci_qtd_init(ehci, qtd, qtd->qtd_dma);
  768. qh->dummy = qtd;
  769. /* hc must see the new dummy at list end */
  770. dma = qtd->qtd_dma;
  771. qtd = list_entry (qh->qtd_list.prev,
  772. struct ehci_qtd, qtd_list);
  773. qtd->hw_next = QTD_NEXT(ehci, dma);
  774. /* let the hc process these next qtds */
  775. wmb ();
  776. dummy->hw_token = token;
  777. urb->hcpriv = qh_get (qh);
  778. }
  779. }
  780. return qh;
  781. }
  782. /*-------------------------------------------------------------------------*/
  783. static int
  784. submit_async (
  785. struct ehci_hcd *ehci,
  786. struct urb *urb,
  787. struct list_head *qtd_list,
  788. gfp_t mem_flags
  789. ) {
  790. struct ehci_qtd *qtd;
  791. int epnum;
  792. unsigned long flags;
  793. struct ehci_qh *qh = NULL;
  794. int rc;
  795. qtd = list_entry (qtd_list->next, struct ehci_qtd, qtd_list);
  796. epnum = urb->ep->desc.bEndpointAddress;
  797. #ifdef EHCI_URB_TRACE
  798. ehci_dbg (ehci,
  799. "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  800. __FUNCTION__, urb->dev->devpath, urb,
  801. epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
  802. urb->transfer_buffer_length,
  803. qtd, urb->ep->hcpriv);
  804. #endif
  805. spin_lock_irqsave (&ehci->lock, flags);
  806. if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE,
  807. &ehci_to_hcd(ehci)->flags))) {
  808. rc = -ESHUTDOWN;
  809. goto done;
  810. }
  811. rc = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
  812. if (unlikely(rc))
  813. goto done;
  814. qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
  815. if (unlikely(qh == NULL)) {
  816. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  817. rc = -ENOMEM;
  818. goto done;
  819. }
  820. /* Control/bulk operations through TTs don't need scheduling,
  821. * the HC and TT handle it when the TT has a buffer ready.
  822. */
  823. if (likely (qh->qh_state == QH_STATE_IDLE))
  824. qh_link_async (ehci, qh_get (qh));
  825. done:
  826. spin_unlock_irqrestore (&ehci->lock, flags);
  827. if (unlikely (qh == NULL))
  828. qtd_list_free (ehci, urb, qtd_list);
  829. return rc;
  830. }
  831. /*-------------------------------------------------------------------------*/
  832. /* the async qh for the qtds being reclaimed are now unlinked from the HC */
  833. static void end_unlink_async (struct ehci_hcd *ehci)
  834. {
  835. struct ehci_qh *qh = ehci->reclaim;
  836. struct ehci_qh *next;
  837. timer_action_done (ehci, TIMER_IAA_WATCHDOG);
  838. // qh->hw_next = cpu_to_hc32(qh->qh_dma);
  839. qh->qh_state = QH_STATE_IDLE;
  840. qh->qh_next.qh = NULL;
  841. qh_put (qh); // refcount from reclaim
  842. /* other unlink(s) may be pending (in QH_STATE_UNLINK_WAIT) */
  843. next = qh->reclaim;
  844. ehci->reclaim = next;
  845. ehci->reclaim_ready = 0;
  846. qh->reclaim = NULL;
  847. qh_completions (ehci, qh);
  848. if (!list_empty (&qh->qtd_list)
  849. && HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  850. qh_link_async (ehci, qh);
  851. else {
  852. qh_put (qh); // refcount from async list
  853. /* it's not free to turn the async schedule on/off; leave it
  854. * active but idle for a while once it empties.
  855. */
  856. if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state)
  857. && ehci->async->qh_next.qh == NULL)
  858. timer_action (ehci, TIMER_ASYNC_OFF);
  859. }
  860. if (next) {
  861. ehci->reclaim = NULL;
  862. start_unlink_async (ehci, next);
  863. }
  864. }
  865. /* makes sure the async qh will become idle */
  866. /* caller must own ehci->lock */
  867. static void start_unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
  868. {
  869. int cmd = ehci_readl(ehci, &ehci->regs->command);
  870. struct ehci_qh *prev;
  871. #ifdef DEBUG
  872. assert_spin_locked(&ehci->lock);
  873. if (ehci->reclaim
  874. || (qh->qh_state != QH_STATE_LINKED
  875. && qh->qh_state != QH_STATE_UNLINK_WAIT)
  876. )
  877. BUG ();
  878. #endif
  879. /* stop async schedule right now? */
  880. if (unlikely (qh == ehci->async)) {
  881. /* can't get here without STS_ASS set */
  882. if (ehci_to_hcd(ehci)->state != HC_STATE_HALT
  883. && !ehci->reclaim) {
  884. /* ... and CMD_IAAD clear */
  885. ehci_writel(ehci, cmd & ~CMD_ASE,
  886. &ehci->regs->command);
  887. wmb ();
  888. // handshake later, if we need to
  889. timer_action_done (ehci, TIMER_ASYNC_OFF);
  890. }
  891. return;
  892. }
  893. qh->qh_state = QH_STATE_UNLINK;
  894. ehci->reclaim = qh = qh_get (qh);
  895. prev = ehci->async;
  896. while (prev->qh_next.qh != qh)
  897. prev = prev->qh_next.qh;
  898. prev->hw_next = qh->hw_next;
  899. prev->qh_next = qh->qh_next;
  900. wmb ();
  901. if (unlikely (ehci_to_hcd(ehci)->state == HC_STATE_HALT)) {
  902. /* if (unlikely (qh->reclaim != 0))
  903. * this will recurse, probably not much
  904. */
  905. end_unlink_async (ehci);
  906. return;
  907. }
  908. ehci->reclaim_ready = 0;
  909. cmd |= CMD_IAAD;
  910. ehci_writel(ehci, cmd, &ehci->regs->command);
  911. (void)ehci_readl(ehci, &ehci->regs->command);
  912. timer_action (ehci, TIMER_IAA_WATCHDOG);
  913. }
  914. /*-------------------------------------------------------------------------*/
  915. static void scan_async (struct ehci_hcd *ehci)
  916. {
  917. struct ehci_qh *qh;
  918. enum ehci_timer_action action = TIMER_IO_WATCHDOG;
  919. if (!++(ehci->stamp))
  920. ehci->stamp++;
  921. timer_action_done (ehci, TIMER_ASYNC_SHRINK);
  922. rescan:
  923. qh = ehci->async->qh_next.qh;
  924. if (likely (qh != NULL)) {
  925. do {
  926. /* clean any finished work for this qh */
  927. if (!list_empty (&qh->qtd_list)
  928. && qh->stamp != ehci->stamp) {
  929. int temp;
  930. /* unlinks could happen here; completion
  931. * reporting drops the lock. rescan using
  932. * the latest schedule, but don't rescan
  933. * qhs we already finished (no looping).
  934. */
  935. qh = qh_get (qh);
  936. qh->stamp = ehci->stamp;
  937. temp = qh_completions (ehci, qh);
  938. qh_put (qh);
  939. if (temp != 0) {
  940. goto rescan;
  941. }
  942. }
  943. /* unlink idle entries, reducing HC PCI usage as well
  944. * as HCD schedule-scanning costs. delay for any qh
  945. * we just scanned, there's a not-unusual case that it
  946. * doesn't stay idle for long.
  947. * (plus, avoids some kind of re-activation race.)
  948. */
  949. if (list_empty (&qh->qtd_list)) {
  950. if (qh->stamp == ehci->stamp)
  951. action = TIMER_ASYNC_SHRINK;
  952. else if (!ehci->reclaim
  953. && qh->qh_state == QH_STATE_LINKED)
  954. start_unlink_async (ehci, qh);
  955. }
  956. qh = qh->qh_next.qh;
  957. } while (qh);
  958. }
  959. if (action == TIMER_ASYNC_SHRINK)
  960. timer_action (ehci, TIMER_ASYNC_SHRINK);
  961. }