ehci-q.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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. struct ehci_qh_hw *hw = qh->hw;
  79. /* writes to an active overlay are unsafe */
  80. WARN_ON(qh->qh_state != QH_STATE_IDLE);
  81. hw->hw_qtd_next = QTD_NEXT(ehci, qtd->qtd_dma);
  82. hw->hw_alt_next = EHCI_LIST_END(ehci);
  83. /* Except for control endpoints, we make hardware maintain data
  84. * toggle (like OHCI) ... here (re)initialize the toggle in the QH,
  85. * and set the pseudo-toggle in udev. Only usb_clear_halt() will
  86. * ever clear it.
  87. */
  88. if (!(hw->hw_info1 & cpu_to_hc32(ehci, QH_TOGGLE_CTL))) {
  89. unsigned is_out, epnum;
  90. is_out = qh->is_out;
  91. epnum = (hc32_to_cpup(ehci, &hw->hw_info1) >> 8) & 0x0f;
  92. if (unlikely (!usb_gettoggle (qh->dev, epnum, is_out))) {
  93. hw->hw_token &= ~cpu_to_hc32(ehci, QTD_TOGGLE);
  94. usb_settoggle (qh->dev, epnum, is_out, 1);
  95. }
  96. }
  97. hw->hw_token &= cpu_to_hc32(ehci, QTD_TOGGLE | QTD_STS_PING);
  98. }
  99. /* if it weren't for a common silicon quirk (writing the dummy into the qh
  100. * overlay, so qh->hw_token wrongly becomes inactive/halted), only fault
  101. * recovery (including urb dequeue) would need software changes to a QH...
  102. */
  103. static void
  104. qh_refresh (struct ehci_hcd *ehci, struct ehci_qh *qh)
  105. {
  106. struct ehci_qtd *qtd;
  107. qtd = list_entry(qh->qtd_list.next, struct ehci_qtd, qtd_list);
  108. /*
  109. * first qtd may already be partially processed.
  110. * If we come here during unlink, the QH overlay region
  111. * might have reference to the just unlinked qtd. The
  112. * qtd is updated in qh_completions(). Update the QH
  113. * overlay here.
  114. */
  115. if (qh->hw->hw_token & ACTIVE_BIT(ehci))
  116. qh->hw->hw_qtd_next = qtd->hw_next;
  117. else
  118. qh_update(ehci, qh, qtd);
  119. }
  120. /*-------------------------------------------------------------------------*/
  121. static void qh_link_async(struct ehci_hcd *ehci, struct ehci_qh *qh);
  122. static void ehci_clear_tt_buffer_complete(struct usb_hcd *hcd,
  123. struct usb_host_endpoint *ep)
  124. {
  125. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  126. struct ehci_qh *qh = ep->hcpriv;
  127. unsigned long flags;
  128. spin_lock_irqsave(&ehci->lock, flags);
  129. qh->clearing_tt = 0;
  130. if (qh->qh_state == QH_STATE_IDLE && !list_empty(&qh->qtd_list)
  131. && ehci->rh_state == EHCI_RH_RUNNING)
  132. qh_link_async(ehci, qh);
  133. spin_unlock_irqrestore(&ehci->lock, flags);
  134. }
  135. static void ehci_clear_tt_buffer(struct ehci_hcd *ehci, struct ehci_qh *qh,
  136. struct urb *urb, u32 token)
  137. {
  138. /* If an async split transaction gets an error or is unlinked,
  139. * the TT buffer may be left in an indeterminate state. We
  140. * have to clear the TT buffer.
  141. *
  142. * Note: this routine is never called for Isochronous transfers.
  143. */
  144. if (urb->dev->tt && !usb_pipeint(urb->pipe) && !qh->clearing_tt) {
  145. #ifdef DEBUG
  146. struct usb_device *tt = urb->dev->tt->hub;
  147. dev_dbg(&tt->dev,
  148. "clear tt buffer port %d, a%d ep%d t%08x\n",
  149. urb->dev->ttport, urb->dev->devnum,
  150. usb_pipeendpoint(urb->pipe), token);
  151. #endif /* DEBUG */
  152. if (!ehci_is_TDI(ehci)
  153. || urb->dev->tt->hub !=
  154. ehci_to_hcd(ehci)->self.root_hub) {
  155. if (usb_hub_clear_tt_buffer(urb) == 0)
  156. qh->clearing_tt = 1;
  157. } else {
  158. /* REVISIT ARC-derived cores don't clear the root
  159. * hub TT buffer in this way...
  160. */
  161. }
  162. }
  163. }
  164. static int qtd_copy_status (
  165. struct ehci_hcd *ehci,
  166. struct urb *urb,
  167. size_t length,
  168. u32 token
  169. )
  170. {
  171. int status = -EINPROGRESS;
  172. /* count IN/OUT bytes, not SETUP (even short packets) */
  173. if (likely (QTD_PID (token) != 2))
  174. urb->actual_length += length - QTD_LENGTH (token);
  175. /* don't modify error codes */
  176. if (unlikely(urb->unlinked))
  177. return status;
  178. /* force cleanup after short read; not always an error */
  179. if (unlikely (IS_SHORT_READ (token)))
  180. status = -EREMOTEIO;
  181. /* serious "can't proceed" faults reported by the hardware */
  182. if (token & QTD_STS_HALT) {
  183. if (token & QTD_STS_BABBLE) {
  184. /* FIXME "must" disable babbling device's port too */
  185. status = -EOVERFLOW;
  186. /* CERR nonzero + halt --> stall */
  187. } else if (QTD_CERR(token)) {
  188. status = -EPIPE;
  189. /* In theory, more than one of the following bits can be set
  190. * since they are sticky and the transaction is retried.
  191. * Which to test first is rather arbitrary.
  192. */
  193. } else if (token & QTD_STS_MMF) {
  194. /* fs/ls interrupt xfer missed the complete-split */
  195. status = -EPROTO;
  196. } else if (token & QTD_STS_DBE) {
  197. status = (QTD_PID (token) == 1) /* IN ? */
  198. ? -ENOSR /* hc couldn't read data */
  199. : -ECOMM; /* hc couldn't write data */
  200. } else if (token & QTD_STS_XACT) {
  201. /* timeout, bad CRC, wrong PID, etc */
  202. ehci_dbg(ehci, "devpath %s ep%d%s 3strikes\n",
  203. urb->dev->devpath,
  204. usb_pipeendpoint(urb->pipe),
  205. usb_pipein(urb->pipe) ? "in" : "out");
  206. status = -EPROTO;
  207. } else { /* unknown */
  208. status = -EPROTO;
  209. }
  210. ehci_vdbg (ehci,
  211. "dev%d ep%d%s qtd token %08x --> status %d\n",
  212. usb_pipedevice (urb->pipe),
  213. usb_pipeendpoint (urb->pipe),
  214. usb_pipein (urb->pipe) ? "in" : "out",
  215. token, status);
  216. }
  217. return status;
  218. }
  219. static void
  220. ehci_urb_done(struct ehci_hcd *ehci, struct urb *urb, int status)
  221. __releases(ehci->lock)
  222. __acquires(ehci->lock)
  223. {
  224. if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  225. /* ... update hc-wide periodic stats */
  226. ehci_to_hcd(ehci)->self.bandwidth_int_reqs--;
  227. }
  228. if (unlikely(urb->unlinked)) {
  229. COUNT(ehci->stats.unlink);
  230. } else {
  231. /* report non-error and short read status as zero */
  232. if (status == -EINPROGRESS || status == -EREMOTEIO)
  233. status = 0;
  234. COUNT(ehci->stats.complete);
  235. }
  236. #ifdef EHCI_URB_TRACE
  237. ehci_dbg (ehci,
  238. "%s %s urb %p ep%d%s status %d len %d/%d\n",
  239. __func__, urb->dev->devpath, urb,
  240. usb_pipeendpoint (urb->pipe),
  241. usb_pipein (urb->pipe) ? "in" : "out",
  242. status,
  243. urb->actual_length, urb->transfer_buffer_length);
  244. #endif
  245. /* complete() can reenter this HCD */
  246. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  247. spin_unlock (&ehci->lock);
  248. usb_hcd_giveback_urb(ehci_to_hcd(ehci), urb, status);
  249. spin_lock (&ehci->lock);
  250. }
  251. static int qh_schedule (struct ehci_hcd *ehci, struct ehci_qh *qh);
  252. /*
  253. * Process and free completed qtds for a qh, returning URBs to drivers.
  254. * Chases up to qh->hw_current. Returns number of completions called,
  255. * indicating how much "real" work we did.
  256. */
  257. static unsigned
  258. qh_completions (struct ehci_hcd *ehci, struct ehci_qh *qh)
  259. {
  260. struct ehci_qtd *last, *end = qh->dummy;
  261. struct list_head *entry, *tmp;
  262. int last_status;
  263. int stopped;
  264. unsigned count = 0;
  265. u8 state;
  266. struct ehci_qh_hw *hw = qh->hw;
  267. if (unlikely (list_empty (&qh->qtd_list)))
  268. return count;
  269. /* completions (or tasks on other cpus) must never clobber HALT
  270. * till we've gone through and cleaned everything up, even when
  271. * they add urbs to this qh's queue or mark them for unlinking.
  272. *
  273. * NOTE: unlinking expects to be done in queue order.
  274. *
  275. * It's a bug for qh->qh_state to be anything other than
  276. * QH_STATE_IDLE, unless our caller is scan_async() or
  277. * scan_intr().
  278. */
  279. state = qh->qh_state;
  280. qh->qh_state = QH_STATE_COMPLETING;
  281. stopped = (state == QH_STATE_IDLE);
  282. rescan:
  283. last = NULL;
  284. last_status = -EINPROGRESS;
  285. qh->needs_rescan = 0;
  286. /* remove de-activated QTDs from front of queue.
  287. * after faults (including short reads), cleanup this urb
  288. * then let the queue advance.
  289. * if queue is stopped, handles unlinks.
  290. */
  291. list_for_each_safe (entry, tmp, &qh->qtd_list) {
  292. struct ehci_qtd *qtd;
  293. struct urb *urb;
  294. u32 token = 0;
  295. qtd = list_entry (entry, struct ehci_qtd, qtd_list);
  296. urb = qtd->urb;
  297. /* clean up any state from previous QTD ...*/
  298. if (last) {
  299. if (likely (last->urb != urb)) {
  300. ehci_urb_done(ehci, last->urb, last_status);
  301. count++;
  302. last_status = -EINPROGRESS;
  303. }
  304. ehci_qtd_free (ehci, last);
  305. last = NULL;
  306. }
  307. /* ignore urbs submitted during completions we reported */
  308. if (qtd == end)
  309. break;
  310. /* hardware copies qtd out of qh overlay */
  311. rmb ();
  312. token = hc32_to_cpu(ehci, qtd->hw_token);
  313. /* always clean up qtds the hc de-activated */
  314. retry_xacterr:
  315. if ((token & QTD_STS_ACTIVE) == 0) {
  316. /* Report Data Buffer Error: non-fatal but useful */
  317. if (token & QTD_STS_DBE)
  318. ehci_dbg(ehci,
  319. "detected DataBufferErr for urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  320. urb,
  321. usb_endpoint_num(&urb->ep->desc),
  322. usb_endpoint_dir_in(&urb->ep->desc) ? "in" : "out",
  323. urb->transfer_buffer_length,
  324. qtd,
  325. qh);
  326. /* on STALL, error, and short reads this urb must
  327. * complete and all its qtds must be recycled.
  328. */
  329. if ((token & QTD_STS_HALT) != 0) {
  330. /* retry transaction errors until we
  331. * reach the software xacterr limit
  332. */
  333. if ((token & QTD_STS_XACT) &&
  334. QTD_CERR(token) == 0 &&
  335. ++qh->xacterrs < QH_XACTERR_MAX &&
  336. !urb->unlinked) {
  337. ehci_dbg(ehci,
  338. "detected XactErr len %zu/%zu retry %d\n",
  339. qtd->length - QTD_LENGTH(token), qtd->length, qh->xacterrs);
  340. /* reset the token in the qtd and the
  341. * qh overlay (which still contains
  342. * the qtd) so that we pick up from
  343. * where we left off
  344. */
  345. token &= ~QTD_STS_HALT;
  346. token |= QTD_STS_ACTIVE |
  347. (EHCI_TUNE_CERR << 10);
  348. qtd->hw_token = cpu_to_hc32(ehci,
  349. token);
  350. wmb();
  351. hw->hw_token = cpu_to_hc32(ehci,
  352. token);
  353. goto retry_xacterr;
  354. }
  355. stopped = 1;
  356. /* magic dummy for some short reads; qh won't advance.
  357. * that silicon quirk can kick in with this dummy too.
  358. *
  359. * other short reads won't stop the queue, including
  360. * control transfers (status stage handles that) or
  361. * most other single-qtd reads ... the queue stops if
  362. * URB_SHORT_NOT_OK was set so the driver submitting
  363. * the urbs could clean it up.
  364. */
  365. } else if (IS_SHORT_READ (token)
  366. && !(qtd->hw_alt_next
  367. & EHCI_LIST_END(ehci))) {
  368. stopped = 1;
  369. }
  370. /* stop scanning when we reach qtds the hc is using */
  371. } else if (likely (!stopped
  372. && ehci->rh_state >= EHCI_RH_RUNNING)) {
  373. break;
  374. /* scan the whole queue for unlinks whenever it stops */
  375. } else {
  376. stopped = 1;
  377. /* cancel everything if we halt, suspend, etc */
  378. if (ehci->rh_state < EHCI_RH_RUNNING)
  379. last_status = -ESHUTDOWN;
  380. /* this qtd is active; skip it unless a previous qtd
  381. * for its urb faulted, or its urb was canceled.
  382. */
  383. else if (last_status == -EINPROGRESS && !urb->unlinked)
  384. continue;
  385. /*
  386. * If this was the active qtd when the qh was unlinked
  387. * and the overlay's token is active, then the overlay
  388. * hasn't been written back to the qtd yet so use its
  389. * token instead of the qtd's. After the qtd is
  390. * processed and removed, the overlay won't be valid
  391. * any more.
  392. */
  393. if (state == QH_STATE_IDLE &&
  394. qh->qtd_list.next == &qtd->qtd_list &&
  395. (hw->hw_token & ACTIVE_BIT(ehci))) {
  396. token = hc32_to_cpu(ehci, hw->hw_token);
  397. hw->hw_token &= ~ACTIVE_BIT(ehci);
  398. /* An unlink may leave an incomplete
  399. * async transaction in the TT buffer.
  400. * We have to clear it.
  401. */
  402. ehci_clear_tt_buffer(ehci, qh, urb, token);
  403. }
  404. }
  405. /* unless we already know the urb's status, collect qtd status
  406. * and update count of bytes transferred. in common short read
  407. * cases with only one data qtd (including control transfers),
  408. * queue processing won't halt. but with two or more qtds (for
  409. * example, with a 32 KB transfer), when the first qtd gets a
  410. * short read the second must be removed by hand.
  411. */
  412. if (last_status == -EINPROGRESS) {
  413. last_status = qtd_copy_status(ehci, urb,
  414. qtd->length, token);
  415. if (last_status == -EREMOTEIO
  416. && (qtd->hw_alt_next
  417. & EHCI_LIST_END(ehci)))
  418. last_status = -EINPROGRESS;
  419. /* As part of low/full-speed endpoint-halt processing
  420. * we must clear the TT buffer (11.17.5).
  421. */
  422. if (unlikely(last_status != -EINPROGRESS &&
  423. last_status != -EREMOTEIO)) {
  424. /* The TT's in some hubs malfunction when they
  425. * receive this request following a STALL (they
  426. * stop sending isochronous packets). Since a
  427. * STALL can't leave the TT buffer in a busy
  428. * state (if you believe Figures 11-48 - 11-51
  429. * in the USB 2.0 spec), we won't clear the TT
  430. * buffer in this case. Strictly speaking this
  431. * is a violation of the spec.
  432. */
  433. if (last_status != -EPIPE)
  434. ehci_clear_tt_buffer(ehci, qh, urb,
  435. token);
  436. }
  437. }
  438. /* if we're removing something not at the queue head,
  439. * patch the hardware queue pointer.
  440. */
  441. if (stopped && qtd->qtd_list.prev != &qh->qtd_list) {
  442. last = list_entry (qtd->qtd_list.prev,
  443. struct ehci_qtd, qtd_list);
  444. last->hw_next = qtd->hw_next;
  445. }
  446. /* remove qtd; it's recycled after possible urb completion */
  447. list_del (&qtd->qtd_list);
  448. last = qtd;
  449. /* reinit the xacterr counter for the next qtd */
  450. qh->xacterrs = 0;
  451. }
  452. /* last urb's completion might still need calling */
  453. if (likely (last != NULL)) {
  454. ehci_urb_done(ehci, last->urb, last_status);
  455. count++;
  456. ehci_qtd_free (ehci, last);
  457. }
  458. /* Do we need to rescan for URBs dequeued during a giveback? */
  459. if (unlikely(qh->needs_rescan)) {
  460. /* If the QH is already unlinked, do the rescan now. */
  461. if (state == QH_STATE_IDLE)
  462. goto rescan;
  463. /* Otherwise we have to wait until the QH is fully unlinked.
  464. * Our caller will start an unlink if qh->needs_rescan is
  465. * set. But if an unlink has already started, nothing needs
  466. * to be done.
  467. */
  468. if (state != QH_STATE_LINKED)
  469. qh->needs_rescan = 0;
  470. }
  471. /* restore original state; caller must unlink or relink */
  472. qh->qh_state = state;
  473. /* be sure the hardware's done with the qh before refreshing
  474. * it after fault cleanup, or recovering from silicon wrongly
  475. * overlaying the dummy qtd (which reduces DMA chatter).
  476. */
  477. if (stopped != 0 || hw->hw_qtd_next == EHCI_LIST_END(ehci)) {
  478. if (state == QH_STATE_LINKED) {
  479. /*
  480. * We won't refresh a QH that's linked (after the HC
  481. * stopped the queue). That avoids a race:
  482. * - HC reads first part of QH;
  483. * - CPU updates that first part and the token;
  484. * - HC reads rest of that QH, including token
  485. * Result: HC gets an inconsistent image, and then
  486. * DMAs to/from the wrong memory (corrupting it).
  487. *
  488. * That should be rare for interrupt transfers,
  489. * except maybe high bandwidth ...
  490. *
  491. * Therefore tell the caller to start an unlink.
  492. */
  493. qh->needs_rescan = 1;
  494. }
  495. /* otherwise, unlink already started */
  496. }
  497. return count;
  498. }
  499. /*-------------------------------------------------------------------------*/
  500. // high bandwidth multiplier, as encoded in highspeed endpoint descriptors
  501. #define hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  502. // ... and packet size, for any kind of endpoint descriptor
  503. #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  504. /*
  505. * reverse of qh_urb_transaction: free a list of TDs.
  506. * used for cleanup after errors, before HC sees an URB's TDs.
  507. */
  508. static void qtd_list_free (
  509. struct ehci_hcd *ehci,
  510. struct urb *urb,
  511. struct list_head *qtd_list
  512. ) {
  513. struct list_head *entry, *temp;
  514. list_for_each_safe (entry, temp, qtd_list) {
  515. struct ehci_qtd *qtd;
  516. qtd = list_entry (entry, struct ehci_qtd, qtd_list);
  517. list_del (&qtd->qtd_list);
  518. ehci_qtd_free (ehci, qtd);
  519. }
  520. }
  521. /*
  522. * create a list of filled qtds for this URB; won't link into qh.
  523. */
  524. static struct list_head *
  525. qh_urb_transaction (
  526. struct ehci_hcd *ehci,
  527. struct urb *urb,
  528. struct list_head *head,
  529. gfp_t flags
  530. ) {
  531. struct ehci_qtd *qtd, *qtd_prev;
  532. dma_addr_t buf;
  533. int len, this_sg_len, maxpacket;
  534. int is_input;
  535. u32 token;
  536. int i;
  537. struct scatterlist *sg;
  538. /*
  539. * URBs map to sequences of QTDs: one logical transaction
  540. */
  541. qtd = ehci_qtd_alloc (ehci, flags);
  542. if (unlikely (!qtd))
  543. return NULL;
  544. list_add_tail (&qtd->qtd_list, head);
  545. qtd->urb = urb;
  546. token = QTD_STS_ACTIVE;
  547. token |= (EHCI_TUNE_CERR << 10);
  548. /* for split transactions, SplitXState initialized to zero */
  549. len = urb->transfer_buffer_length;
  550. is_input = usb_pipein (urb->pipe);
  551. if (usb_pipecontrol (urb->pipe)) {
  552. /* SETUP pid */
  553. qtd_fill(ehci, qtd, urb->setup_dma,
  554. sizeof (struct usb_ctrlrequest),
  555. token | (2 /* "setup" */ << 8), 8);
  556. /* ... and always at least one more pid */
  557. token ^= QTD_TOGGLE;
  558. qtd_prev = qtd;
  559. qtd = ehci_qtd_alloc (ehci, flags);
  560. if (unlikely (!qtd))
  561. goto cleanup;
  562. qtd->urb = urb;
  563. qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
  564. list_add_tail (&qtd->qtd_list, head);
  565. /* for zero length DATA stages, STATUS is always IN */
  566. if (len == 0)
  567. token |= (1 /* "in" */ << 8);
  568. }
  569. /*
  570. * data transfer stage: buffer setup
  571. */
  572. i = urb->num_mapped_sgs;
  573. if (len > 0 && i > 0) {
  574. sg = urb->sg;
  575. buf = sg_dma_address(sg);
  576. /* urb->transfer_buffer_length may be smaller than the
  577. * size of the scatterlist (or vice versa)
  578. */
  579. this_sg_len = min_t(int, sg_dma_len(sg), len);
  580. } else {
  581. sg = NULL;
  582. buf = urb->transfer_dma;
  583. this_sg_len = len;
  584. }
  585. if (is_input)
  586. token |= (1 /* "in" */ << 8);
  587. /* else it's already initted to "out" pid (0 << 8) */
  588. maxpacket = max_packet(usb_maxpacket(urb->dev, urb->pipe, !is_input));
  589. /*
  590. * buffer gets wrapped in one or more qtds;
  591. * last one may be "short" (including zero len)
  592. * and may serve as a control status ack
  593. */
  594. for (;;) {
  595. int this_qtd_len;
  596. this_qtd_len = qtd_fill(ehci, qtd, buf, this_sg_len, token,
  597. maxpacket);
  598. this_sg_len -= this_qtd_len;
  599. len -= this_qtd_len;
  600. buf += this_qtd_len;
  601. /*
  602. * short reads advance to a "magic" dummy instead of the next
  603. * qtd ... that forces the queue to stop, for manual cleanup.
  604. * (this will usually be overridden later.)
  605. */
  606. if (is_input)
  607. qtd->hw_alt_next = ehci->async->hw->hw_alt_next;
  608. /* qh makes control packets use qtd toggle; maybe switch it */
  609. if ((maxpacket & (this_qtd_len + (maxpacket - 1))) == 0)
  610. token ^= QTD_TOGGLE;
  611. if (likely(this_sg_len <= 0)) {
  612. if (--i <= 0 || len <= 0)
  613. break;
  614. sg = sg_next(sg);
  615. buf = sg_dma_address(sg);
  616. this_sg_len = min_t(int, sg_dma_len(sg), len);
  617. }
  618. qtd_prev = qtd;
  619. qtd = ehci_qtd_alloc (ehci, flags);
  620. if (unlikely (!qtd))
  621. goto cleanup;
  622. qtd->urb = urb;
  623. qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
  624. list_add_tail (&qtd->qtd_list, head);
  625. }
  626. /*
  627. * unless the caller requires manual cleanup after short reads,
  628. * have the alt_next mechanism keep the queue running after the
  629. * last data qtd (the only one, for control and most other cases).
  630. */
  631. if (likely ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0
  632. || usb_pipecontrol (urb->pipe)))
  633. qtd->hw_alt_next = EHCI_LIST_END(ehci);
  634. /*
  635. * control requests may need a terminating data "status" ack;
  636. * other OUT ones may need a terminating short packet
  637. * (zero length).
  638. */
  639. if (likely (urb->transfer_buffer_length != 0)) {
  640. int one_more = 0;
  641. if (usb_pipecontrol (urb->pipe)) {
  642. one_more = 1;
  643. token ^= 0x0100; /* "in" <--> "out" */
  644. token |= QTD_TOGGLE; /* force DATA1 */
  645. } else if (usb_pipeout(urb->pipe)
  646. && (urb->transfer_flags & URB_ZERO_PACKET)
  647. && !(urb->transfer_buffer_length % maxpacket)) {
  648. one_more = 1;
  649. }
  650. if (one_more) {
  651. qtd_prev = qtd;
  652. qtd = ehci_qtd_alloc (ehci, flags);
  653. if (unlikely (!qtd))
  654. goto cleanup;
  655. qtd->urb = urb;
  656. qtd_prev->hw_next = QTD_NEXT(ehci, qtd->qtd_dma);
  657. list_add_tail (&qtd->qtd_list, head);
  658. /* never any data in such packets */
  659. qtd_fill(ehci, qtd, 0, 0, token, 0);
  660. }
  661. }
  662. /* by default, enable interrupt on urb completion */
  663. if (likely (!(urb->transfer_flags & URB_NO_INTERRUPT)))
  664. qtd->hw_token |= cpu_to_hc32(ehci, QTD_IOC);
  665. return head;
  666. cleanup:
  667. qtd_list_free (ehci, urb, head);
  668. return NULL;
  669. }
  670. /*-------------------------------------------------------------------------*/
  671. // Would be best to create all qh's from config descriptors,
  672. // when each interface/altsetting is established. Unlink
  673. // any previous qh and cancel its urbs first; endpoints are
  674. // implicitly reset then (data toggle too).
  675. // That'd mean updating how usbcore talks to HCDs. (2.7?)
  676. /*
  677. * Each QH holds a qtd list; a QH is used for everything except iso.
  678. *
  679. * For interrupt urbs, the scheduler must set the microframe scheduling
  680. * mask(s) each time the QH gets scheduled. For highspeed, that's
  681. * just one microframe in the s-mask. For split interrupt transactions
  682. * there are additional complications: c-mask, maybe FSTNs.
  683. */
  684. static struct ehci_qh *
  685. qh_make (
  686. struct ehci_hcd *ehci,
  687. struct urb *urb,
  688. gfp_t flags
  689. ) {
  690. struct ehci_qh *qh = ehci_qh_alloc (ehci, flags);
  691. u32 info1 = 0, info2 = 0;
  692. int is_input, type;
  693. int maxp = 0;
  694. struct usb_tt *tt = urb->dev->tt;
  695. struct ehci_qh_hw *hw;
  696. if (!qh)
  697. return qh;
  698. /*
  699. * init endpoint/device data for this QH
  700. */
  701. info1 |= usb_pipeendpoint (urb->pipe) << 8;
  702. info1 |= usb_pipedevice (urb->pipe) << 0;
  703. is_input = usb_pipein (urb->pipe);
  704. type = usb_pipetype (urb->pipe);
  705. maxp = usb_maxpacket (urb->dev, urb->pipe, !is_input);
  706. /* 1024 byte maxpacket is a hardware ceiling. High bandwidth
  707. * acts like up to 3KB, but is built from smaller packets.
  708. */
  709. if (max_packet(maxp) > 1024) {
  710. ehci_dbg(ehci, "bogus qh maxpacket %d\n", max_packet(maxp));
  711. goto done;
  712. }
  713. /* Compute interrupt scheduling parameters just once, and save.
  714. * - allowing for high bandwidth, how many nsec/uframe are used?
  715. * - split transactions need a second CSPLIT uframe; same question
  716. * - splits also need a schedule gap (for full/low speed I/O)
  717. * - qh has a polling interval
  718. *
  719. * For control/bulk requests, the HC or TT handles these.
  720. */
  721. if (type == PIPE_INTERRUPT) {
  722. qh->usecs = NS_TO_US(usb_calc_bus_time(USB_SPEED_HIGH,
  723. is_input, 0,
  724. hb_mult(maxp) * max_packet(maxp)));
  725. qh->start = NO_FRAME;
  726. if (urb->dev->speed == USB_SPEED_HIGH) {
  727. qh->c_usecs = 0;
  728. qh->gap_uf = 0;
  729. qh->period = urb->interval >> 3;
  730. if (qh->period == 0 && urb->interval != 1) {
  731. /* NOTE interval 2 or 4 uframes could work.
  732. * But interval 1 scheduling is simpler, and
  733. * includes high bandwidth.
  734. */
  735. urb->interval = 1;
  736. } else if (qh->period > ehci->periodic_size) {
  737. qh->period = ehci->periodic_size;
  738. urb->interval = qh->period << 3;
  739. }
  740. } else {
  741. int think_time;
  742. /* gap is f(FS/LS transfer times) */
  743. qh->gap_uf = 1 + usb_calc_bus_time (urb->dev->speed,
  744. is_input, 0, maxp) / (125 * 1000);
  745. /* FIXME this just approximates SPLIT/CSPLIT times */
  746. if (is_input) { // SPLIT, gap, CSPLIT+DATA
  747. qh->c_usecs = qh->usecs + HS_USECS (0);
  748. qh->usecs = HS_USECS (1);
  749. } else { // SPLIT+DATA, gap, CSPLIT
  750. qh->usecs += HS_USECS (1);
  751. qh->c_usecs = HS_USECS (0);
  752. }
  753. think_time = tt ? tt->think_time : 0;
  754. qh->tt_usecs = NS_TO_US (think_time +
  755. usb_calc_bus_time (urb->dev->speed,
  756. is_input, 0, max_packet (maxp)));
  757. qh->period = urb->interval;
  758. if (qh->period > ehci->periodic_size) {
  759. qh->period = ehci->periodic_size;
  760. urb->interval = qh->period;
  761. }
  762. }
  763. }
  764. /* support for tt scheduling, and access to toggles */
  765. qh->dev = urb->dev;
  766. /* using TT? */
  767. switch (urb->dev->speed) {
  768. case USB_SPEED_LOW:
  769. info1 |= QH_LOW_SPEED;
  770. /* FALL THROUGH */
  771. case USB_SPEED_FULL:
  772. /* EPS 0 means "full" */
  773. if (type != PIPE_INTERRUPT)
  774. info1 |= (EHCI_TUNE_RL_TT << 28);
  775. if (type == PIPE_CONTROL) {
  776. info1 |= QH_CONTROL_EP; /* for TT */
  777. info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
  778. }
  779. info1 |= maxp << 16;
  780. info2 |= (EHCI_TUNE_MULT_TT << 30);
  781. /* Some Freescale processors have an erratum in which the
  782. * port number in the queue head was 0..N-1 instead of 1..N.
  783. */
  784. if (ehci_has_fsl_portno_bug(ehci))
  785. info2 |= (urb->dev->ttport-1) << 23;
  786. else
  787. info2 |= urb->dev->ttport << 23;
  788. /* set the address of the TT; for TDI's integrated
  789. * root hub tt, leave it zeroed.
  790. */
  791. if (tt && tt->hub != ehci_to_hcd(ehci)->self.root_hub)
  792. info2 |= tt->hub->devnum << 16;
  793. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets c-mask } */
  794. break;
  795. case USB_SPEED_HIGH: /* no TT involved */
  796. info1 |= QH_HIGH_SPEED;
  797. if (type == PIPE_CONTROL) {
  798. info1 |= (EHCI_TUNE_RL_HS << 28);
  799. info1 |= 64 << 16; /* usb2 fixed maxpacket */
  800. info1 |= QH_TOGGLE_CTL; /* toggle from qtd */
  801. info2 |= (EHCI_TUNE_MULT_HS << 30);
  802. } else if (type == PIPE_BULK) {
  803. info1 |= (EHCI_TUNE_RL_HS << 28);
  804. /* The USB spec says that high speed bulk endpoints
  805. * always use 512 byte maxpacket. But some device
  806. * vendors decided to ignore that, and MSFT is happy
  807. * to help them do so. So now people expect to use
  808. * such nonconformant devices with Linux too; sigh.
  809. */
  810. info1 |= max_packet(maxp) << 16;
  811. info2 |= (EHCI_TUNE_MULT_HS << 30);
  812. } else { /* PIPE_INTERRUPT */
  813. info1 |= max_packet (maxp) << 16;
  814. info2 |= hb_mult (maxp) << 30;
  815. }
  816. break;
  817. default:
  818. ehci_dbg(ehci, "bogus dev %p speed %d\n", urb->dev,
  819. urb->dev->speed);
  820. done:
  821. qh_destroy(ehci, qh);
  822. return NULL;
  823. }
  824. /* NOTE: if (PIPE_INTERRUPT) { scheduler sets s-mask } */
  825. /* init as live, toggle clear */
  826. qh->qh_state = QH_STATE_IDLE;
  827. hw = qh->hw;
  828. hw->hw_info1 = cpu_to_hc32(ehci, info1);
  829. hw->hw_info2 = cpu_to_hc32(ehci, info2);
  830. qh->is_out = !is_input;
  831. usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), !is_input, 1);
  832. return qh;
  833. }
  834. /*-------------------------------------------------------------------------*/
  835. static void enable_async(struct ehci_hcd *ehci)
  836. {
  837. if (ehci->async_count++)
  838. return;
  839. /* Stop waiting to turn off the async schedule */
  840. ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_DISABLE_ASYNC);
  841. /* Don't start the schedule until ASS is 0 */
  842. ehci_poll_ASS(ehci);
  843. turn_on_io_watchdog(ehci);
  844. }
  845. static void disable_async(struct ehci_hcd *ehci)
  846. {
  847. if (--ehci->async_count)
  848. return;
  849. /* The async schedule and async_unlink list are supposed to be empty */
  850. WARN_ON(ehci->async->qh_next.qh || ehci->async_unlink);
  851. /* Don't turn off the schedule until ASS is 1 */
  852. ehci_poll_ASS(ehci);
  853. }
  854. /* move qh (and its qtds) onto async queue; maybe enable queue. */
  855. static void qh_link_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
  856. {
  857. __hc32 dma = QH_NEXT(ehci, qh->qh_dma);
  858. struct ehci_qh *head;
  859. /* Don't link a QH if there's a Clear-TT-Buffer pending */
  860. if (unlikely(qh->clearing_tt))
  861. return;
  862. WARN_ON(qh->qh_state != QH_STATE_IDLE);
  863. /* clear halt and/or toggle; and maybe recover from silicon quirk */
  864. qh_refresh(ehci, qh);
  865. /* splice right after start */
  866. head = ehci->async;
  867. qh->qh_next = head->qh_next;
  868. qh->hw->hw_next = head->hw->hw_next;
  869. wmb ();
  870. head->qh_next.qh = qh;
  871. head->hw->hw_next = dma;
  872. qh->xacterrs = 0;
  873. qh->qh_state = QH_STATE_LINKED;
  874. /* qtd completions reported later by interrupt */
  875. enable_async(ehci);
  876. }
  877. /*-------------------------------------------------------------------------*/
  878. /*
  879. * For control/bulk/interrupt, return QH with these TDs appended.
  880. * Allocates and initializes the QH if necessary.
  881. * Returns null if it can't allocate a QH it needs to.
  882. * If the QH has TDs (urbs) already, that's great.
  883. */
  884. static struct ehci_qh *qh_append_tds (
  885. struct ehci_hcd *ehci,
  886. struct urb *urb,
  887. struct list_head *qtd_list,
  888. int epnum,
  889. void **ptr
  890. )
  891. {
  892. struct ehci_qh *qh = NULL;
  893. __hc32 qh_addr_mask = cpu_to_hc32(ehci, 0x7f);
  894. qh = (struct ehci_qh *) *ptr;
  895. if (unlikely (qh == NULL)) {
  896. /* can't sleep here, we have ehci->lock... */
  897. qh = qh_make (ehci, urb, GFP_ATOMIC);
  898. *ptr = qh;
  899. }
  900. if (likely (qh != NULL)) {
  901. struct ehci_qtd *qtd;
  902. if (unlikely (list_empty (qtd_list)))
  903. qtd = NULL;
  904. else
  905. qtd = list_entry (qtd_list->next, struct ehci_qtd,
  906. qtd_list);
  907. /* control qh may need patching ... */
  908. if (unlikely (epnum == 0)) {
  909. /* usb_reset_device() briefly reverts to address 0 */
  910. if (usb_pipedevice (urb->pipe) == 0)
  911. qh->hw->hw_info1 &= ~qh_addr_mask;
  912. }
  913. /* just one way to queue requests: swap with the dummy qtd.
  914. * only hc or qh_refresh() ever modify the overlay.
  915. */
  916. if (likely (qtd != NULL)) {
  917. struct ehci_qtd *dummy;
  918. dma_addr_t dma;
  919. __hc32 token;
  920. /* to avoid racing the HC, use the dummy td instead of
  921. * the first td of our list (becomes new dummy). both
  922. * tds stay deactivated until we're done, when the
  923. * HC is allowed to fetch the old dummy (4.10.2).
  924. */
  925. token = qtd->hw_token;
  926. qtd->hw_token = HALT_BIT(ehci);
  927. dummy = qh->dummy;
  928. dma = dummy->qtd_dma;
  929. *dummy = *qtd;
  930. dummy->qtd_dma = dma;
  931. list_del (&qtd->qtd_list);
  932. list_add (&dummy->qtd_list, qtd_list);
  933. list_splice_tail(qtd_list, &qh->qtd_list);
  934. ehci_qtd_init(ehci, qtd, qtd->qtd_dma);
  935. qh->dummy = qtd;
  936. /* hc must see the new dummy at list end */
  937. dma = qtd->qtd_dma;
  938. qtd = list_entry (qh->qtd_list.prev,
  939. struct ehci_qtd, qtd_list);
  940. qtd->hw_next = QTD_NEXT(ehci, dma);
  941. /* let the hc process these next qtds */
  942. wmb ();
  943. dummy->hw_token = token;
  944. urb->hcpriv = qh;
  945. }
  946. }
  947. return qh;
  948. }
  949. /*-------------------------------------------------------------------------*/
  950. static int
  951. submit_async (
  952. struct ehci_hcd *ehci,
  953. struct urb *urb,
  954. struct list_head *qtd_list,
  955. gfp_t mem_flags
  956. ) {
  957. int epnum;
  958. unsigned long flags;
  959. struct ehci_qh *qh = NULL;
  960. int rc;
  961. epnum = urb->ep->desc.bEndpointAddress;
  962. #ifdef EHCI_URB_TRACE
  963. {
  964. struct ehci_qtd *qtd;
  965. qtd = list_entry(qtd_list->next, struct ehci_qtd, qtd_list);
  966. ehci_dbg(ehci,
  967. "%s %s urb %p ep%d%s len %d, qtd %p [qh %p]\n",
  968. __func__, urb->dev->devpath, urb,
  969. epnum & 0x0f, (epnum & USB_DIR_IN) ? "in" : "out",
  970. urb->transfer_buffer_length,
  971. qtd, urb->ep->hcpriv);
  972. }
  973. #endif
  974. spin_lock_irqsave (&ehci->lock, flags);
  975. if (unlikely(!HCD_HW_ACCESSIBLE(ehci_to_hcd(ehci)))) {
  976. rc = -ESHUTDOWN;
  977. goto done;
  978. }
  979. rc = usb_hcd_link_urb_to_ep(ehci_to_hcd(ehci), urb);
  980. if (unlikely(rc))
  981. goto done;
  982. qh = qh_append_tds(ehci, urb, qtd_list, epnum, &urb->ep->hcpriv);
  983. if (unlikely(qh == NULL)) {
  984. usb_hcd_unlink_urb_from_ep(ehci_to_hcd(ehci), urb);
  985. rc = -ENOMEM;
  986. goto done;
  987. }
  988. /* Control/bulk operations through TTs don't need scheduling,
  989. * the HC and TT handle it when the TT has a buffer ready.
  990. */
  991. if (likely (qh->qh_state == QH_STATE_IDLE))
  992. qh_link_async(ehci, qh);
  993. done:
  994. spin_unlock_irqrestore (&ehci->lock, flags);
  995. if (unlikely (qh == NULL))
  996. qtd_list_free (ehci, urb, qtd_list);
  997. return rc;
  998. }
  999. /*-------------------------------------------------------------------------*/
  1000. static void single_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh)
  1001. {
  1002. struct ehci_qh *prev;
  1003. /* Add to the end of the list of QHs waiting for the next IAAD */
  1004. qh->qh_state = QH_STATE_UNLINK_WAIT;
  1005. if (ehci->async_unlink)
  1006. ehci->async_unlink_last->unlink_next = qh;
  1007. else
  1008. ehci->async_unlink = qh;
  1009. ehci->async_unlink_last = qh;
  1010. /* Unlink it from the schedule */
  1011. prev = ehci->async;
  1012. while (prev->qh_next.qh != qh)
  1013. prev = prev->qh_next.qh;
  1014. prev->hw->hw_next = qh->hw->hw_next;
  1015. prev->qh_next = qh->qh_next;
  1016. if (ehci->qh_scan_next == qh)
  1017. ehci->qh_scan_next = qh->qh_next.qh;
  1018. }
  1019. static void start_iaa_cycle(struct ehci_hcd *ehci, bool nested)
  1020. {
  1021. /*
  1022. * Do nothing if an IAA cycle is already running or
  1023. * if one will be started shortly.
  1024. */
  1025. if (ehci->async_iaa || ehci->async_unlinking)
  1026. return;
  1027. /* If the controller isn't running, we don't have to wait for it */
  1028. if (unlikely(ehci->rh_state < EHCI_RH_RUNNING)) {
  1029. /* Do all the waiting QHs */
  1030. ehci->async_iaa = ehci->async_unlink;
  1031. ehci->async_unlink = NULL;
  1032. if (!nested) /* Avoid recursion */
  1033. end_unlink_async(ehci);
  1034. /* Otherwise start a new IAA cycle */
  1035. } else if (likely(ehci->rh_state == EHCI_RH_RUNNING)) {
  1036. struct ehci_qh *qh;
  1037. /* Do only the first waiting QH (nVidia bug?) */
  1038. qh = ehci->async_unlink;
  1039. /*
  1040. * Intel (?) bug: The HC can write back the overlay region
  1041. * even after the IAA interrupt occurs. In self-defense,
  1042. * always go through two IAA cycles for each QH.
  1043. */
  1044. if (qh->qh_state == QH_STATE_UNLINK_WAIT) {
  1045. qh->qh_state = QH_STATE_UNLINK;
  1046. } else {
  1047. ehci->async_iaa = qh;
  1048. ehci->async_unlink = qh->unlink_next;
  1049. qh->unlink_next = NULL;
  1050. }
  1051. /* Make sure the unlinks are all visible to the hardware */
  1052. wmb();
  1053. ehci_writel(ehci, ehci->command | CMD_IAAD,
  1054. &ehci->regs->command);
  1055. ehci_readl(ehci, &ehci->regs->command);
  1056. ehci_enable_event(ehci, EHCI_HRTIMER_IAA_WATCHDOG, true);
  1057. }
  1058. }
  1059. /* the async qh for the qtds being unlinked are now gone from the HC */
  1060. static void end_unlink_async(struct ehci_hcd *ehci)
  1061. {
  1062. struct ehci_qh *qh;
  1063. if (ehci->has_synopsys_hc_bug)
  1064. ehci_writel(ehci, (u32) ehci->async->qh_dma,
  1065. &ehci->regs->async_next);
  1066. /* Process the idle QHs */
  1067. restart:
  1068. ehci->async_unlinking = true;
  1069. while (ehci->async_iaa) {
  1070. qh = ehci->async_iaa;
  1071. ehci->async_iaa = qh->unlink_next;
  1072. qh->unlink_next = NULL;
  1073. qh->qh_state = QH_STATE_IDLE;
  1074. qh->qh_next.qh = NULL;
  1075. qh_completions(ehci, qh);
  1076. if (!list_empty(&qh->qtd_list) &&
  1077. ehci->rh_state == EHCI_RH_RUNNING)
  1078. qh_link_async(ehci, qh);
  1079. disable_async(ehci);
  1080. }
  1081. ehci->async_unlinking = false;
  1082. /* Start a new IAA cycle if any QHs are waiting for it */
  1083. if (ehci->async_unlink) {
  1084. start_iaa_cycle(ehci, true);
  1085. if (unlikely(ehci->rh_state < EHCI_RH_RUNNING))
  1086. goto restart;
  1087. }
  1088. }
  1089. static void start_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh);
  1090. static void unlink_empty_async(struct ehci_hcd *ehci)
  1091. {
  1092. struct ehci_qh *qh;
  1093. struct ehci_qh *qh_to_unlink = NULL;
  1094. bool check_unlinks_later = false;
  1095. int count = 0;
  1096. /* Find the last async QH which has been empty for a timer cycle */
  1097. for (qh = ehci->async->qh_next.qh; qh; qh = qh->qh_next.qh) {
  1098. if (list_empty(&qh->qtd_list) &&
  1099. qh->qh_state == QH_STATE_LINKED) {
  1100. ++count;
  1101. if (qh->unlink_cycle == ehci->async_unlink_cycle)
  1102. check_unlinks_later = true;
  1103. else
  1104. qh_to_unlink = qh;
  1105. }
  1106. }
  1107. /* If nothing else is being unlinked, unlink the last empty QH */
  1108. if (!ehci->async_iaa && !ehci->async_unlink && qh_to_unlink) {
  1109. start_unlink_async(ehci, qh_to_unlink);
  1110. --count;
  1111. }
  1112. /* Other QHs will be handled later */
  1113. if (count > 0) {
  1114. ehci_enable_event(ehci, EHCI_HRTIMER_ASYNC_UNLINKS, true);
  1115. ++ehci->async_unlink_cycle;
  1116. }
  1117. }
  1118. /* The root hub is suspended; unlink all the async QHs */
  1119. static void unlink_empty_async_suspended(struct ehci_hcd *ehci)
  1120. {
  1121. struct ehci_qh *qh;
  1122. while (ehci->async->qh_next.qh) {
  1123. qh = ehci->async->qh_next.qh;
  1124. WARN_ON(!list_empty(&qh->qtd_list));
  1125. single_unlink_async(ehci, qh);
  1126. }
  1127. start_iaa_cycle(ehci, false);
  1128. }
  1129. /* makes sure the async qh will become idle */
  1130. /* caller must own ehci->lock */
  1131. static void start_unlink_async(struct ehci_hcd *ehci, struct ehci_qh *qh)
  1132. {
  1133. /*
  1134. * If the QH isn't linked then there's nothing we can do
  1135. * unless we were called during a giveback, in which case
  1136. * qh_completions() has to deal with it.
  1137. */
  1138. if (qh->qh_state != QH_STATE_LINKED) {
  1139. if (qh->qh_state == QH_STATE_COMPLETING)
  1140. qh->needs_rescan = 1;
  1141. return;
  1142. }
  1143. single_unlink_async(ehci, qh);
  1144. start_iaa_cycle(ehci, false);
  1145. }
  1146. /*-------------------------------------------------------------------------*/
  1147. static void scan_async (struct ehci_hcd *ehci)
  1148. {
  1149. struct ehci_qh *qh;
  1150. bool check_unlinks_later = false;
  1151. ehci->qh_scan_next = ehci->async->qh_next.qh;
  1152. while (ehci->qh_scan_next) {
  1153. qh = ehci->qh_scan_next;
  1154. ehci->qh_scan_next = qh->qh_next.qh;
  1155. rescan:
  1156. /* clean any finished work for this qh */
  1157. if (!list_empty(&qh->qtd_list)) {
  1158. int temp;
  1159. /*
  1160. * Unlinks could happen here; completion reporting
  1161. * drops the lock. That's why ehci->qh_scan_next
  1162. * always holds the next qh to scan; if the next qh
  1163. * gets unlinked then ehci->qh_scan_next is adjusted
  1164. * in single_unlink_async().
  1165. */
  1166. temp = qh_completions(ehci, qh);
  1167. if (qh->needs_rescan) {
  1168. start_unlink_async(ehci, qh);
  1169. } else if (list_empty(&qh->qtd_list)
  1170. && qh->qh_state == QH_STATE_LINKED) {
  1171. qh->unlink_cycle = ehci->async_unlink_cycle;
  1172. check_unlinks_later = true;
  1173. } else if (temp != 0)
  1174. goto rescan;
  1175. }
  1176. }
  1177. /*
  1178. * Unlink empty entries, reducing DMA usage as well
  1179. * as HCD schedule-scanning costs. Delay for any qh
  1180. * we just scanned, there's a not-unusual case that it
  1181. * doesn't stay idle for long.
  1182. */
  1183. if (check_unlinks_later && ehci->rh_state == EHCI_RH_RUNNING &&
  1184. !(ehci->enabled_hrtimer_events &
  1185. BIT(EHCI_HRTIMER_ASYNC_UNLINKS))) {
  1186. ehci_enable_event(ehci, EHCI_HRTIMER_ASYNC_UNLINKS, true);
  1187. ++ehci->async_unlink_cycle;
  1188. }
  1189. }