ehci-q.c 43 KB

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