xhci-ring.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. /*
  23. * Ring initialization rules:
  24. * 1. Each segment is initialized to zero, except for link TRBs.
  25. * 2. Ring cycle state = 0. This represents Producer Cycle State (PCS) or
  26. * Consumer Cycle State (CCS), depending on ring function.
  27. * 3. Enqueue pointer = dequeue pointer = address of first TRB in the segment.
  28. *
  29. * Ring behavior rules:
  30. * 1. A ring is empty if enqueue == dequeue. This means there will always be at
  31. * least one free TRB in the ring. This is useful if you want to turn that
  32. * into a link TRB and expand the ring.
  33. * 2. When incrementing an enqueue or dequeue pointer, if the next TRB is a
  34. * link TRB, then load the pointer with the address in the link TRB. If the
  35. * link TRB had its toggle bit set, you may need to update the ring cycle
  36. * state (see cycle bit rules). You may have to do this multiple times
  37. * until you reach a non-link TRB.
  38. * 3. A ring is full if enqueue++ (for the definition of increment above)
  39. * equals the dequeue pointer.
  40. *
  41. * Cycle bit rules:
  42. * 1. When a consumer increments a dequeue pointer and encounters a toggle bit
  43. * in a link TRB, it must toggle the ring cycle state.
  44. * 2. When a producer increments an enqueue pointer and encounters a toggle bit
  45. * in a link TRB, it must toggle the ring cycle state.
  46. *
  47. * Producer rules:
  48. * 1. Check if ring is full before you enqueue.
  49. * 2. Write the ring cycle state to the cycle bit in the TRB you're enqueuing.
  50. * Update enqueue pointer between each write (which may update the ring
  51. * cycle state).
  52. * 3. Notify consumer. If SW is producer, it rings the doorbell for command
  53. * and endpoint rings. If HC is the producer for the event ring,
  54. * and it generates an interrupt according to interrupt modulation rules.
  55. *
  56. * Consumer rules:
  57. * 1. Check if TRB belongs to you. If the cycle bit == your ring cycle state,
  58. * the TRB is owned by the consumer.
  59. * 2. Update dequeue pointer (which may update the ring cycle state) and
  60. * continue processing TRBs until you reach a TRB which is not owned by you.
  61. * 3. Notify the producer. SW is the consumer for the event ring, and it
  62. * updates event ring dequeue pointer. HC is the consumer for the command and
  63. * endpoint rings; it generates events on the event ring for these.
  64. */
  65. #include <linux/scatterlist.h>
  66. #include "xhci.h"
  67. /*
  68. * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
  69. * address of the TRB.
  70. */
  71. dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
  72. union xhci_trb *trb)
  73. {
  74. unsigned long segment_offset;
  75. if (!seg || !trb || trb < seg->trbs)
  76. return 0;
  77. /* offset in TRBs */
  78. segment_offset = trb - seg->trbs;
  79. if (segment_offset > TRBS_PER_SEGMENT)
  80. return 0;
  81. return seg->dma + (segment_offset * sizeof(*trb));
  82. }
  83. /* Does this link TRB point to the first segment in a ring,
  84. * or was the previous TRB the last TRB on the last segment in the ERST?
  85. */
  86. static inline bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring,
  87. struct xhci_segment *seg, union xhci_trb *trb)
  88. {
  89. if (ring == xhci->event_ring)
  90. return (trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
  91. (seg->next == xhci->event_ring->first_seg);
  92. else
  93. return trb->link.control & LINK_TOGGLE;
  94. }
  95. /* Is this TRB a link TRB or was the last TRB the last TRB in this event ring
  96. * segment? I.e. would the updated event TRB pointer step off the end of the
  97. * event seg?
  98. */
  99. static inline int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  100. struct xhci_segment *seg, union xhci_trb *trb)
  101. {
  102. if (ring == xhci->event_ring)
  103. return trb == &seg->trbs[TRBS_PER_SEGMENT];
  104. else
  105. return (trb->link.control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK);
  106. }
  107. /* Updates trb to point to the next TRB in the ring, and updates seg if the next
  108. * TRB is in a new segment. This does not skip over link TRBs, and it does not
  109. * effect the ring dequeue or enqueue pointers.
  110. */
  111. static void next_trb(struct xhci_hcd *xhci,
  112. struct xhci_ring *ring,
  113. struct xhci_segment **seg,
  114. union xhci_trb **trb)
  115. {
  116. if (last_trb(xhci, ring, *seg, *trb)) {
  117. *seg = (*seg)->next;
  118. *trb = ((*seg)->trbs);
  119. } else {
  120. *trb = (*trb)++;
  121. }
  122. }
  123. /*
  124. * See Cycle bit rules. SW is the consumer for the event ring only.
  125. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  126. */
  127. static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  128. {
  129. union xhci_trb *next = ++(ring->dequeue);
  130. unsigned long long addr;
  131. ring->deq_updates++;
  132. /* Update the dequeue pointer further if that was a link TRB or we're at
  133. * the end of an event ring segment (which doesn't have link TRBS)
  134. */
  135. while (last_trb(xhci, ring, ring->deq_seg, next)) {
  136. if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
  137. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  138. if (!in_interrupt())
  139. xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
  140. ring,
  141. (unsigned int) ring->cycle_state);
  142. }
  143. ring->deq_seg = ring->deq_seg->next;
  144. ring->dequeue = ring->deq_seg->trbs;
  145. next = ring->dequeue;
  146. }
  147. addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
  148. if (ring == xhci->event_ring)
  149. xhci_dbg(xhci, "Event ring deq = 0x%llx (DMA)\n", addr);
  150. else if (ring == xhci->cmd_ring)
  151. xhci_dbg(xhci, "Command ring deq = 0x%llx (DMA)\n", addr);
  152. else
  153. xhci_dbg(xhci, "Ring deq = 0x%llx (DMA)\n", addr);
  154. }
  155. /*
  156. * See Cycle bit rules. SW is the consumer for the event ring only.
  157. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  158. *
  159. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  160. * chain bit is set), then set the chain bit in all the following link TRBs.
  161. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  162. * have their chain bit cleared (so that each Link TRB is a separate TD).
  163. *
  164. * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
  165. * set, but other sections talk about dealing with the chain bit set. This was
  166. * fixed in the 0.96 specification errata, but we have to assume that all 0.95
  167. * xHCI hardware can't handle the chain bit being cleared on a link TRB.
  168. */
  169. static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  170. {
  171. u32 chain;
  172. union xhci_trb *next;
  173. unsigned long long addr;
  174. chain = ring->enqueue->generic.field[3] & TRB_CHAIN;
  175. next = ++(ring->enqueue);
  176. ring->enq_updates++;
  177. /* Update the dequeue pointer further if that was a link TRB or we're at
  178. * the end of an event ring segment (which doesn't have link TRBS)
  179. */
  180. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  181. if (!consumer) {
  182. if (ring != xhci->event_ring) {
  183. /* If we're not dealing with 0.95 hardware,
  184. * carry over the chain bit of the previous TRB
  185. * (which may mean the chain bit is cleared).
  186. */
  187. if (!xhci_link_trb_quirk(xhci)) {
  188. next->link.control &= ~TRB_CHAIN;
  189. next->link.control |= chain;
  190. }
  191. /* Give this link TRB to the hardware */
  192. wmb();
  193. if (next->link.control & TRB_CYCLE)
  194. next->link.control &= (u32) ~TRB_CYCLE;
  195. else
  196. next->link.control |= (u32) TRB_CYCLE;
  197. }
  198. /* Toggle the cycle bit after the last ring segment. */
  199. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  200. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  201. if (!in_interrupt())
  202. xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
  203. ring,
  204. (unsigned int) ring->cycle_state);
  205. }
  206. }
  207. ring->enq_seg = ring->enq_seg->next;
  208. ring->enqueue = ring->enq_seg->trbs;
  209. next = ring->enqueue;
  210. }
  211. addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
  212. if (ring == xhci->event_ring)
  213. xhci_dbg(xhci, "Event ring enq = 0x%llx (DMA)\n", addr);
  214. else if (ring == xhci->cmd_ring)
  215. xhci_dbg(xhci, "Command ring enq = 0x%llx (DMA)\n", addr);
  216. else
  217. xhci_dbg(xhci, "Ring enq = 0x%llx (DMA)\n", addr);
  218. }
  219. /*
  220. * Check to see if there's room to enqueue num_trbs on the ring. See rules
  221. * above.
  222. * FIXME: this would be simpler and faster if we just kept track of the number
  223. * of free TRBs in a ring.
  224. */
  225. static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
  226. unsigned int num_trbs)
  227. {
  228. int i;
  229. union xhci_trb *enq = ring->enqueue;
  230. struct xhci_segment *enq_seg = ring->enq_seg;
  231. /* Check if ring is empty */
  232. if (enq == ring->dequeue)
  233. return 1;
  234. /* Make sure there's an extra empty TRB available */
  235. for (i = 0; i <= num_trbs; ++i) {
  236. if (enq == ring->dequeue)
  237. return 0;
  238. enq++;
  239. while (last_trb(xhci, ring, enq_seg, enq)) {
  240. enq_seg = enq_seg->next;
  241. enq = enq_seg->trbs;
  242. }
  243. }
  244. return 1;
  245. }
  246. void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
  247. {
  248. u64 temp;
  249. dma_addr_t deq;
  250. deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
  251. xhci->event_ring->dequeue);
  252. if (deq == 0 && !in_interrupt())
  253. xhci_warn(xhci, "WARN something wrong with SW event ring "
  254. "dequeue ptr.\n");
  255. /* Update HC event ring dequeue pointer */
  256. temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  257. temp &= ERST_PTR_MASK;
  258. /* Don't clear the EHB bit (which is RW1C) because
  259. * there might be more events to service.
  260. */
  261. temp &= ~ERST_EHB;
  262. xhci_dbg(xhci, "// Write event ring dequeue pointer, preserving EHB bit\n");
  263. xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
  264. &xhci->ir_set->erst_dequeue);
  265. }
  266. /* Ring the host controller doorbell after placing a command on the ring */
  267. void xhci_ring_cmd_db(struct xhci_hcd *xhci)
  268. {
  269. u32 temp;
  270. xhci_dbg(xhci, "// Ding dong!\n");
  271. temp = xhci_readl(xhci, &xhci->dba->doorbell[0]) & DB_MASK;
  272. xhci_writel(xhci, temp | DB_TARGET_HOST, &xhci->dba->doorbell[0]);
  273. /* Flush PCI posted writes */
  274. xhci_readl(xhci, &xhci->dba->doorbell[0]);
  275. }
  276. static void ring_ep_doorbell(struct xhci_hcd *xhci,
  277. unsigned int slot_id,
  278. unsigned int ep_index)
  279. {
  280. struct xhci_ring *ep_ring;
  281. u32 field;
  282. __u32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
  283. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  284. /* Don't ring the doorbell for this endpoint if there are pending
  285. * cancellations because the we don't want to interrupt processing.
  286. */
  287. if (!ep_ring->cancels_pending && !(ep_ring->state & SET_DEQ_PENDING)
  288. && !(ep_ring->state & EP_HALTED)) {
  289. field = xhci_readl(xhci, db_addr) & DB_MASK;
  290. xhci_writel(xhci, field | EPI_TO_DB(ep_index), db_addr);
  291. /* Flush PCI posted writes - FIXME Matthew Wilcox says this
  292. * isn't time-critical and we shouldn't make the CPU wait for
  293. * the flush.
  294. */
  295. xhci_readl(xhci, db_addr);
  296. }
  297. }
  298. /*
  299. * Find the segment that trb is in. Start searching in start_seg.
  300. * If we must move past a segment that has a link TRB with a toggle cycle state
  301. * bit set, then we will toggle the value pointed at by cycle_state.
  302. */
  303. static struct xhci_segment *find_trb_seg(
  304. struct xhci_segment *start_seg,
  305. union xhci_trb *trb, int *cycle_state)
  306. {
  307. struct xhci_segment *cur_seg = start_seg;
  308. struct xhci_generic_trb *generic_trb;
  309. while (cur_seg->trbs > trb ||
  310. &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
  311. generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
  312. if (TRB_TYPE(generic_trb->field[3]) == TRB_LINK &&
  313. (generic_trb->field[3] & LINK_TOGGLE))
  314. *cycle_state = ~(*cycle_state) & 0x1;
  315. cur_seg = cur_seg->next;
  316. if (cur_seg == start_seg)
  317. /* Looped over the entire list. Oops! */
  318. return 0;
  319. }
  320. return cur_seg;
  321. }
  322. /*
  323. * Move the xHC's endpoint ring dequeue pointer past cur_td.
  324. * Record the new state of the xHC's endpoint ring dequeue segment,
  325. * dequeue pointer, and new consumer cycle state in state.
  326. * Update our internal representation of the ring's dequeue pointer.
  327. *
  328. * We do this in three jumps:
  329. * - First we update our new ring state to be the same as when the xHC stopped.
  330. * - Then we traverse the ring to find the segment that contains
  331. * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
  332. * any link TRBs with the toggle cycle bit set.
  333. * - Finally we move the dequeue state one TRB further, toggling the cycle bit
  334. * if we've moved it past a link TRB with the toggle cycle bit set.
  335. */
  336. void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
  337. unsigned int slot_id, unsigned int ep_index,
  338. struct xhci_td *cur_td, struct xhci_dequeue_state *state)
  339. {
  340. struct xhci_virt_device *dev = xhci->devs[slot_id];
  341. struct xhci_ring *ep_ring = dev->ep_rings[ep_index];
  342. struct xhci_generic_trb *trb;
  343. struct xhci_ep_ctx *ep_ctx;
  344. dma_addr_t addr;
  345. state->new_cycle_state = 0;
  346. xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
  347. state->new_deq_seg = find_trb_seg(cur_td->start_seg,
  348. ep_ring->stopped_trb,
  349. &state->new_cycle_state);
  350. if (!state->new_deq_seg)
  351. BUG();
  352. /* Dig out the cycle state saved by the xHC during the stop ep cmd */
  353. xhci_dbg(xhci, "Finding endpoint context\n");
  354. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  355. state->new_cycle_state = 0x1 & ep_ctx->deq;
  356. state->new_deq_ptr = cur_td->last_trb;
  357. xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
  358. state->new_deq_seg = find_trb_seg(state->new_deq_seg,
  359. state->new_deq_ptr,
  360. &state->new_cycle_state);
  361. if (!state->new_deq_seg)
  362. BUG();
  363. trb = &state->new_deq_ptr->generic;
  364. if (TRB_TYPE(trb->field[3]) == TRB_LINK &&
  365. (trb->field[3] & LINK_TOGGLE))
  366. state->new_cycle_state = ~(state->new_cycle_state) & 0x1;
  367. next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
  368. /* Don't update the ring cycle state for the producer (us). */
  369. xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
  370. state->new_deq_seg);
  371. addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
  372. xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
  373. (unsigned long long) addr);
  374. xhci_dbg(xhci, "Setting dequeue pointer in internal ring state.\n");
  375. ep_ring->dequeue = state->new_deq_ptr;
  376. ep_ring->deq_seg = state->new_deq_seg;
  377. }
  378. static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  379. struct xhci_td *cur_td)
  380. {
  381. struct xhci_segment *cur_seg;
  382. union xhci_trb *cur_trb;
  383. for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
  384. true;
  385. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  386. if ((cur_trb->generic.field[3] & TRB_TYPE_BITMASK) ==
  387. TRB_TYPE(TRB_LINK)) {
  388. /* Unchain any chained Link TRBs, but
  389. * leave the pointers intact.
  390. */
  391. cur_trb->generic.field[3] &= ~TRB_CHAIN;
  392. xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
  393. xhci_dbg(xhci, "Address = %p (0x%llx dma); "
  394. "in seg %p (0x%llx dma)\n",
  395. cur_trb,
  396. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  397. cur_seg,
  398. (unsigned long long)cur_seg->dma);
  399. } else {
  400. cur_trb->generic.field[0] = 0;
  401. cur_trb->generic.field[1] = 0;
  402. cur_trb->generic.field[2] = 0;
  403. /* Preserve only the cycle bit of this TRB */
  404. cur_trb->generic.field[3] &= TRB_CYCLE;
  405. cur_trb->generic.field[3] |= TRB_TYPE(TRB_TR_NOOP);
  406. xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) "
  407. "in seg %p (0x%llx dma)\n",
  408. cur_trb,
  409. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  410. cur_seg,
  411. (unsigned long long)cur_seg->dma);
  412. }
  413. if (cur_trb == cur_td->last_trb)
  414. break;
  415. }
  416. }
  417. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  418. unsigned int ep_index, struct xhci_segment *deq_seg,
  419. union xhci_trb *deq_ptr, u32 cycle_state);
  420. void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
  421. struct xhci_ring *ep_ring, unsigned int slot_id,
  422. unsigned int ep_index, struct xhci_dequeue_state *deq_state)
  423. {
  424. xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
  425. "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
  426. deq_state->new_deq_seg,
  427. (unsigned long long)deq_state->new_deq_seg->dma,
  428. deq_state->new_deq_ptr,
  429. (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
  430. deq_state->new_cycle_state);
  431. queue_set_tr_deq(xhci, slot_id, ep_index,
  432. deq_state->new_deq_seg,
  433. deq_state->new_deq_ptr,
  434. (u32) deq_state->new_cycle_state);
  435. /* Stop the TD queueing code from ringing the doorbell until
  436. * this command completes. The HC won't set the dequeue pointer
  437. * if the ring is running, and ringing the doorbell starts the
  438. * ring running.
  439. */
  440. ep_ring->state |= SET_DEQ_PENDING;
  441. }
  442. /*
  443. * When we get a command completion for a Stop Endpoint Command, we need to
  444. * unlink any cancelled TDs from the ring. There are two ways to do that:
  445. *
  446. * 1. If the HW was in the middle of processing the TD that needs to be
  447. * cancelled, then we must move the ring's dequeue pointer past the last TRB
  448. * in the TD with a Set Dequeue Pointer Command.
  449. * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
  450. * bit cleared) so that the HW will skip over them.
  451. */
  452. static void handle_stopped_endpoint(struct xhci_hcd *xhci,
  453. union xhci_trb *trb)
  454. {
  455. unsigned int slot_id;
  456. unsigned int ep_index;
  457. struct xhci_ring *ep_ring;
  458. struct list_head *entry;
  459. struct xhci_td *cur_td = 0;
  460. struct xhci_td *last_unlinked_td;
  461. struct xhci_dequeue_state deq_state;
  462. #ifdef CONFIG_USB_HCD_STAT
  463. ktime_t stop_time = ktime_get();
  464. #endif
  465. memset(&deq_state, 0, sizeof(deq_state));
  466. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  467. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  468. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  469. if (list_empty(&ep_ring->cancelled_td_list))
  470. return;
  471. /* Fix up the ep ring first, so HW stops executing cancelled TDs.
  472. * We have the xHCI lock, so nothing can modify this list until we drop
  473. * it. We're also in the event handler, so we can't get re-interrupted
  474. * if another Stop Endpoint command completes
  475. */
  476. list_for_each(entry, &ep_ring->cancelled_td_list) {
  477. cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
  478. xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
  479. cur_td->first_trb,
  480. (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
  481. /*
  482. * If we stopped on the TD we need to cancel, then we have to
  483. * move the xHC endpoint ring dequeue pointer past this TD.
  484. */
  485. if (cur_td == ep_ring->stopped_td)
  486. xhci_find_new_dequeue_state(xhci, slot_id, ep_index, cur_td,
  487. &deq_state);
  488. else
  489. td_to_noop(xhci, ep_ring, cur_td);
  490. /*
  491. * The event handler won't see a completion for this TD anymore,
  492. * so remove it from the endpoint ring's TD list. Keep it in
  493. * the cancelled TD list for URB completion later.
  494. */
  495. list_del(&cur_td->td_list);
  496. ep_ring->cancels_pending--;
  497. }
  498. last_unlinked_td = cur_td;
  499. /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
  500. if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
  501. xhci_queue_new_dequeue_state(xhci, ep_ring,
  502. slot_id, ep_index, &deq_state);
  503. xhci_ring_cmd_db(xhci);
  504. } else {
  505. /* Otherwise just ring the doorbell to restart the ring */
  506. ring_ep_doorbell(xhci, slot_id, ep_index);
  507. }
  508. /*
  509. * Drop the lock and complete the URBs in the cancelled TD list.
  510. * New TDs to be cancelled might be added to the end of the list before
  511. * we can complete all the URBs for the TDs we already unlinked.
  512. * So stop when we've completed the URB for the last TD we unlinked.
  513. */
  514. do {
  515. cur_td = list_entry(ep_ring->cancelled_td_list.next,
  516. struct xhci_td, cancelled_td_list);
  517. list_del(&cur_td->cancelled_td_list);
  518. /* Clean up the cancelled URB */
  519. #ifdef CONFIG_USB_HCD_STAT
  520. hcd_stat_update(xhci->tp_stat, cur_td->urb->actual_length,
  521. ktime_sub(stop_time, cur_td->start_time));
  522. #endif
  523. cur_td->urb->hcpriv = NULL;
  524. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), cur_td->urb);
  525. xhci_dbg(xhci, "Giveback cancelled URB %p\n", cur_td->urb);
  526. spin_unlock(&xhci->lock);
  527. /* Doesn't matter what we pass for status, since the core will
  528. * just overwrite it (because the URB has been unlinked).
  529. */
  530. usb_hcd_giveback_urb(xhci_to_hcd(xhci), cur_td->urb, 0);
  531. kfree(cur_td);
  532. spin_lock(&xhci->lock);
  533. } while (cur_td != last_unlinked_td);
  534. /* Return to the event handler with xhci->lock re-acquired */
  535. }
  536. /*
  537. * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
  538. * we need to clear the set deq pending flag in the endpoint ring state, so that
  539. * the TD queueing code can ring the doorbell again. We also need to ring the
  540. * endpoint doorbell to restart the ring, but only if there aren't more
  541. * cancellations pending.
  542. */
  543. static void handle_set_deq_completion(struct xhci_hcd *xhci,
  544. struct xhci_event_cmd *event,
  545. union xhci_trb *trb)
  546. {
  547. unsigned int slot_id;
  548. unsigned int ep_index;
  549. struct xhci_ring *ep_ring;
  550. struct xhci_virt_device *dev;
  551. struct xhci_ep_ctx *ep_ctx;
  552. struct xhci_slot_ctx *slot_ctx;
  553. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  554. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  555. dev = xhci->devs[slot_id];
  556. ep_ring = dev->ep_rings[ep_index];
  557. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  558. slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
  559. if (GET_COMP_CODE(event->status) != COMP_SUCCESS) {
  560. unsigned int ep_state;
  561. unsigned int slot_state;
  562. switch (GET_COMP_CODE(event->status)) {
  563. case COMP_TRB_ERR:
  564. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
  565. "of stream ID configuration\n");
  566. break;
  567. case COMP_CTX_STATE:
  568. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
  569. "to incorrect slot or ep state.\n");
  570. ep_state = ep_ctx->ep_info;
  571. ep_state &= EP_STATE_MASK;
  572. slot_state = slot_ctx->dev_state;
  573. slot_state = GET_SLOT_STATE(slot_state);
  574. xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
  575. slot_state, ep_state);
  576. break;
  577. case COMP_EBADSLT:
  578. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
  579. "slot %u was not enabled.\n", slot_id);
  580. break;
  581. default:
  582. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
  583. "completion code of %u.\n",
  584. GET_COMP_CODE(event->status));
  585. break;
  586. }
  587. /* OK what do we do now? The endpoint state is hosed, and we
  588. * should never get to this point if the synchronization between
  589. * queueing, and endpoint state are correct. This might happen
  590. * if the device gets disconnected after we've finished
  591. * cancelling URBs, which might not be an error...
  592. */
  593. } else {
  594. xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
  595. ep_ctx->deq);
  596. }
  597. ep_ring->state &= ~SET_DEQ_PENDING;
  598. ring_ep_doorbell(xhci, slot_id, ep_index);
  599. }
  600. static void handle_reset_ep_completion(struct xhci_hcd *xhci,
  601. struct xhci_event_cmd *event,
  602. union xhci_trb *trb)
  603. {
  604. int slot_id;
  605. unsigned int ep_index;
  606. struct xhci_ring *ep_ring;
  607. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  608. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  609. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  610. /* This command will only fail if the endpoint wasn't halted,
  611. * but we don't care.
  612. */
  613. xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
  614. (unsigned int) GET_COMP_CODE(event->status));
  615. /* HW with the reset endpoint quirk needs to have a configure endpoint
  616. * command complete before the endpoint can be used. Queue that here
  617. * because the HW can't handle two commands being queued in a row.
  618. */
  619. if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
  620. xhci_dbg(xhci, "Queueing configure endpoint command\n");
  621. xhci_queue_configure_endpoint(xhci,
  622. xhci->devs[slot_id]->in_ctx->dma, slot_id);
  623. xhci_ring_cmd_db(xhci);
  624. } else {
  625. /* Clear our internal halted state and restart the ring */
  626. ep_ring->state &= ~EP_HALTED;
  627. ring_ep_doorbell(xhci, slot_id, ep_index);
  628. }
  629. }
  630. static void handle_cmd_completion(struct xhci_hcd *xhci,
  631. struct xhci_event_cmd *event)
  632. {
  633. int slot_id = TRB_TO_SLOT_ID(event->flags);
  634. u64 cmd_dma;
  635. dma_addr_t cmd_dequeue_dma;
  636. struct xhci_input_control_ctx *ctrl_ctx;
  637. unsigned int ep_index;
  638. struct xhci_ring *ep_ring;
  639. unsigned int ep_state;
  640. cmd_dma = event->cmd_trb;
  641. cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  642. xhci->cmd_ring->dequeue);
  643. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  644. if (cmd_dequeue_dma == 0) {
  645. xhci->error_bitmask |= 1 << 4;
  646. return;
  647. }
  648. /* Does the DMA address match our internal dequeue pointer address? */
  649. if (cmd_dma != (u64) cmd_dequeue_dma) {
  650. xhci->error_bitmask |= 1 << 5;
  651. return;
  652. }
  653. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  654. case TRB_TYPE(TRB_ENABLE_SLOT):
  655. if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
  656. xhci->slot_id = slot_id;
  657. else
  658. xhci->slot_id = 0;
  659. complete(&xhci->addr_dev);
  660. break;
  661. case TRB_TYPE(TRB_DISABLE_SLOT):
  662. if (xhci->devs[slot_id])
  663. xhci_free_virt_device(xhci, slot_id);
  664. break;
  665. case TRB_TYPE(TRB_CONFIG_EP):
  666. /*
  667. * Configure endpoint commands can come from the USB core
  668. * configuration or alt setting changes, or because the HW
  669. * needed an extra configure endpoint command after a reset
  670. * endpoint command. In the latter case, the xHCI driver is
  671. * not waiting on the configure endpoint command.
  672. */
  673. ctrl_ctx = xhci_get_input_control_ctx(xhci,
  674. xhci->devs[slot_id]->in_ctx);
  675. /* Input ctx add_flags are the endpoint index plus one */
  676. ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1;
  677. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  678. if (!ep_ring) {
  679. /* This must have been an initial configure endpoint */
  680. xhci->devs[slot_id]->cmd_status =
  681. GET_COMP_CODE(event->status);
  682. complete(&xhci->devs[slot_id]->cmd_completion);
  683. break;
  684. }
  685. ep_state = ep_ring->state;
  686. xhci_dbg(xhci, "Completed config ep cmd - last ep index = %d, "
  687. "state = %d\n", ep_index, ep_state);
  688. if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
  689. ep_state & EP_HALTED) {
  690. /* Clear our internal halted state and restart ring */
  691. xhci->devs[slot_id]->ep_rings[ep_index]->state &=
  692. ~EP_HALTED;
  693. ring_ep_doorbell(xhci, slot_id, ep_index);
  694. } else {
  695. xhci->devs[slot_id]->cmd_status =
  696. GET_COMP_CODE(event->status);
  697. complete(&xhci->devs[slot_id]->cmd_completion);
  698. }
  699. break;
  700. case TRB_TYPE(TRB_EVAL_CONTEXT):
  701. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  702. complete(&xhci->devs[slot_id]->cmd_completion);
  703. break;
  704. case TRB_TYPE(TRB_ADDR_DEV):
  705. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  706. complete(&xhci->addr_dev);
  707. break;
  708. case TRB_TYPE(TRB_STOP_RING):
  709. handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue);
  710. break;
  711. case TRB_TYPE(TRB_SET_DEQ):
  712. handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
  713. break;
  714. case TRB_TYPE(TRB_CMD_NOOP):
  715. ++xhci->noops_handled;
  716. break;
  717. case TRB_TYPE(TRB_RESET_EP):
  718. handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
  719. break;
  720. default:
  721. /* Skip over unknown commands on the event ring */
  722. xhci->error_bitmask |= 1 << 6;
  723. break;
  724. }
  725. inc_deq(xhci, xhci->cmd_ring, false);
  726. }
  727. static void handle_port_status(struct xhci_hcd *xhci,
  728. union xhci_trb *event)
  729. {
  730. u32 port_id;
  731. /* Port status change events always have a successful completion code */
  732. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  733. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  734. xhci->error_bitmask |= 1 << 8;
  735. }
  736. /* FIXME: core doesn't care about all port link state changes yet */
  737. port_id = GET_PORT_ID(event->generic.field[0]);
  738. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  739. /* Update event ring dequeue pointer before dropping the lock */
  740. inc_deq(xhci, xhci->event_ring, true);
  741. xhci_set_hc_event_deq(xhci);
  742. spin_unlock(&xhci->lock);
  743. /* Pass this up to the core */
  744. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  745. spin_lock(&xhci->lock);
  746. }
  747. /*
  748. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  749. * at end_trb, which may be in another segment. If the suspect DMA address is a
  750. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  751. * returns 0.
  752. */
  753. static struct xhci_segment *trb_in_td(
  754. struct xhci_segment *start_seg,
  755. union xhci_trb *start_trb,
  756. union xhci_trb *end_trb,
  757. dma_addr_t suspect_dma)
  758. {
  759. dma_addr_t start_dma;
  760. dma_addr_t end_seg_dma;
  761. dma_addr_t end_trb_dma;
  762. struct xhci_segment *cur_seg;
  763. start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
  764. cur_seg = start_seg;
  765. do {
  766. /* We may get an event for a Link TRB in the middle of a TD */
  767. end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
  768. &start_seg->trbs[TRBS_PER_SEGMENT - 1]);
  769. /* If the end TRB isn't in this segment, this is set to 0 */
  770. end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
  771. if (end_trb_dma > 0) {
  772. /* The end TRB is in this segment, so suspect should be here */
  773. if (start_dma <= end_trb_dma) {
  774. if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
  775. return cur_seg;
  776. } else {
  777. /* Case for one segment with
  778. * a TD wrapped around to the top
  779. */
  780. if ((suspect_dma >= start_dma &&
  781. suspect_dma <= end_seg_dma) ||
  782. (suspect_dma >= cur_seg->dma &&
  783. suspect_dma <= end_trb_dma))
  784. return cur_seg;
  785. }
  786. return 0;
  787. } else {
  788. /* Might still be somewhere in this segment */
  789. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  790. return cur_seg;
  791. }
  792. cur_seg = cur_seg->next;
  793. start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  794. } while (1);
  795. }
  796. /*
  797. * If this function returns an error condition, it means it got a Transfer
  798. * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
  799. * At this point, the host controller is probably hosed and should be reset.
  800. */
  801. static int handle_tx_event(struct xhci_hcd *xhci,
  802. struct xhci_transfer_event *event)
  803. {
  804. struct xhci_virt_device *xdev;
  805. struct xhci_ring *ep_ring;
  806. unsigned int slot_id;
  807. int ep_index;
  808. struct xhci_td *td = 0;
  809. dma_addr_t event_dma;
  810. struct xhci_segment *event_seg;
  811. union xhci_trb *event_trb;
  812. struct urb *urb = 0;
  813. int status = -EINPROGRESS;
  814. struct xhci_ep_ctx *ep_ctx;
  815. u32 trb_comp_code;
  816. xhci_dbg(xhci, "In %s\n", __func__);
  817. slot_id = TRB_TO_SLOT_ID(event->flags);
  818. xdev = xhci->devs[slot_id];
  819. if (!xdev) {
  820. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  821. return -ENODEV;
  822. }
  823. /* Endpoint ID is 1 based, our index is zero based */
  824. ep_index = TRB_TO_EP_ID(event->flags) - 1;
  825. xhci_dbg(xhci, "%s - ep index = %d\n", __func__, ep_index);
  826. ep_ring = xdev->ep_rings[ep_index];
  827. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  828. if (!ep_ring || (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
  829. xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n");
  830. return -ENODEV;
  831. }
  832. event_dma = event->buffer;
  833. /* This TRB should be in the TD at the head of this ring's TD list */
  834. xhci_dbg(xhci, "%s - checking for list empty\n", __func__);
  835. if (list_empty(&ep_ring->td_list)) {
  836. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  837. TRB_TO_SLOT_ID(event->flags), ep_index);
  838. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  839. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  840. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  841. urb = NULL;
  842. goto cleanup;
  843. }
  844. xhci_dbg(xhci, "%s - getting list entry\n", __func__);
  845. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  846. /* Is this a TRB in the currently executing TD? */
  847. xhci_dbg(xhci, "%s - looking for TD\n", __func__);
  848. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  849. td->last_trb, event_dma);
  850. xhci_dbg(xhci, "%s - found event_seg = %p\n", __func__, event_seg);
  851. if (!event_seg) {
  852. /* HC is busted, give up! */
  853. xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
  854. return -ESHUTDOWN;
  855. }
  856. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
  857. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  858. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  859. xhci_dbg(xhci, "Offset 0x00 (buffer lo) = 0x%x\n",
  860. lower_32_bits(event->buffer));
  861. xhci_dbg(xhci, "Offset 0x04 (buffer hi) = 0x%x\n",
  862. upper_32_bits(event->buffer));
  863. xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
  864. (unsigned int) event->transfer_len);
  865. xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
  866. (unsigned int) event->flags);
  867. /* Look for common error cases */
  868. trb_comp_code = GET_COMP_CODE(event->transfer_len);
  869. switch (trb_comp_code) {
  870. /* Skip codes that require special handling depending on
  871. * transfer type
  872. */
  873. case COMP_SUCCESS:
  874. case COMP_SHORT_TX:
  875. break;
  876. case COMP_STOP:
  877. xhci_dbg(xhci, "Stopped on Transfer TRB\n");
  878. break;
  879. case COMP_STOP_INVAL:
  880. xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
  881. break;
  882. case COMP_STALL:
  883. xhci_warn(xhci, "WARN: Stalled endpoint\n");
  884. ep_ring->state |= EP_HALTED;
  885. status = -EPIPE;
  886. break;
  887. case COMP_TRB_ERR:
  888. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  889. status = -EILSEQ;
  890. break;
  891. case COMP_TX_ERR:
  892. xhci_warn(xhci, "WARN: transfer error on endpoint\n");
  893. status = -EPROTO;
  894. break;
  895. case COMP_BABBLE:
  896. xhci_warn(xhci, "WARN: babble error on endpoint\n");
  897. status = -EOVERFLOW;
  898. break;
  899. case COMP_DB_ERR:
  900. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  901. status = -ENOSR;
  902. break;
  903. default:
  904. xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
  905. urb = NULL;
  906. goto cleanup;
  907. }
  908. /* Now update the urb's actual_length and give back to the core */
  909. /* Was this a control transfer? */
  910. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  911. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  912. switch (trb_comp_code) {
  913. case COMP_SUCCESS:
  914. if (event_trb == ep_ring->dequeue) {
  915. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  916. status = -ESHUTDOWN;
  917. } else if (event_trb != td->last_trb) {
  918. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  919. status = -ESHUTDOWN;
  920. } else {
  921. xhci_dbg(xhci, "Successful control transfer!\n");
  922. status = 0;
  923. }
  924. break;
  925. case COMP_SHORT_TX:
  926. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  927. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  928. status = -EREMOTEIO;
  929. else
  930. status = 0;
  931. break;
  932. case COMP_BABBLE:
  933. /* The 0.96 spec says a babbling control endpoint
  934. * is not halted. The 0.96 spec says it is. Some HW
  935. * claims to be 0.95 compliant, but it halts the control
  936. * endpoint anyway. Check if a babble halted the
  937. * endpoint.
  938. */
  939. if (ep_ctx->ep_info != EP_STATE_HALTED)
  940. break;
  941. /* else fall through */
  942. case COMP_STALL:
  943. /* Did we transfer part of the data (middle) phase? */
  944. if (event_trb != ep_ring->dequeue &&
  945. event_trb != td->last_trb)
  946. td->urb->actual_length =
  947. td->urb->transfer_buffer_length
  948. - TRB_LEN(event->transfer_len);
  949. else
  950. td->urb->actual_length = 0;
  951. ep_ring->stopped_td = td;
  952. ep_ring->stopped_trb = event_trb;
  953. xhci_queue_reset_ep(xhci, slot_id, ep_index);
  954. xhci_cleanup_stalled_ring(xhci,
  955. td->urb->dev,
  956. ep_index, ep_ring);
  957. xhci_ring_cmd_db(xhci);
  958. goto td_cleanup;
  959. default:
  960. /* Others already handled above */
  961. break;
  962. }
  963. /*
  964. * Did we transfer any data, despite the errors that might have
  965. * happened? I.e. did we get past the setup stage?
  966. */
  967. if (event_trb != ep_ring->dequeue) {
  968. /* The event was for the status stage */
  969. if (event_trb == td->last_trb) {
  970. if (td->urb->actual_length != 0) {
  971. /* Don't overwrite a previously set error code */
  972. if ((status == -EINPROGRESS ||
  973. status == 0) &&
  974. (td->urb->transfer_flags
  975. & URB_SHORT_NOT_OK))
  976. /* Did we already see a short data stage? */
  977. status = -EREMOTEIO;
  978. } else {
  979. td->urb->actual_length =
  980. td->urb->transfer_buffer_length;
  981. }
  982. } else {
  983. /* Maybe the event was for the data stage? */
  984. if (trb_comp_code != COMP_STOP_INVAL) {
  985. /* We didn't stop on a link TRB in the middle */
  986. td->urb->actual_length =
  987. td->urb->transfer_buffer_length -
  988. TRB_LEN(event->transfer_len);
  989. xhci_dbg(xhci, "Waiting for status stage event\n");
  990. urb = NULL;
  991. goto cleanup;
  992. }
  993. }
  994. }
  995. } else {
  996. switch (trb_comp_code) {
  997. case COMP_SUCCESS:
  998. /* Double check that the HW transferred everything. */
  999. if (event_trb != td->last_trb) {
  1000. xhci_warn(xhci, "WARN Successful completion "
  1001. "on short TX\n");
  1002. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1003. status = -EREMOTEIO;
  1004. else
  1005. status = 0;
  1006. } else {
  1007. if (usb_endpoint_xfer_bulk(&td->urb->ep->desc))
  1008. xhci_dbg(xhci, "Successful bulk "
  1009. "transfer!\n");
  1010. else
  1011. xhci_dbg(xhci, "Successful interrupt "
  1012. "transfer!\n");
  1013. status = 0;
  1014. }
  1015. break;
  1016. case COMP_SHORT_TX:
  1017. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1018. status = -EREMOTEIO;
  1019. else
  1020. status = 0;
  1021. break;
  1022. default:
  1023. /* Others already handled above */
  1024. break;
  1025. }
  1026. dev_dbg(&td->urb->dev->dev,
  1027. "ep %#x - asked for %d bytes, "
  1028. "%d bytes untransferred\n",
  1029. td->urb->ep->desc.bEndpointAddress,
  1030. td->urb->transfer_buffer_length,
  1031. TRB_LEN(event->transfer_len));
  1032. /* Fast path - was this the last TRB in the TD for this URB? */
  1033. if (event_trb == td->last_trb) {
  1034. if (TRB_LEN(event->transfer_len) != 0) {
  1035. td->urb->actual_length =
  1036. td->urb->transfer_buffer_length -
  1037. TRB_LEN(event->transfer_len);
  1038. if (td->urb->transfer_buffer_length <
  1039. td->urb->actual_length) {
  1040. xhci_warn(xhci, "HC gave bad length "
  1041. "of %d bytes left\n",
  1042. TRB_LEN(event->transfer_len));
  1043. td->urb->actual_length = 0;
  1044. if (td->urb->transfer_flags &
  1045. URB_SHORT_NOT_OK)
  1046. status = -EREMOTEIO;
  1047. else
  1048. status = 0;
  1049. }
  1050. /* Don't overwrite a previously set error code */
  1051. if (status == -EINPROGRESS) {
  1052. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1053. status = -EREMOTEIO;
  1054. else
  1055. status = 0;
  1056. }
  1057. } else {
  1058. td->urb->actual_length = td->urb->transfer_buffer_length;
  1059. /* Ignore a short packet completion if the
  1060. * untransferred length was zero.
  1061. */
  1062. if (status == -EREMOTEIO)
  1063. status = 0;
  1064. }
  1065. } else {
  1066. /* Slow path - walk the list, starting from the dequeue
  1067. * pointer, to get the actual length transferred.
  1068. */
  1069. union xhci_trb *cur_trb;
  1070. struct xhci_segment *cur_seg;
  1071. td->urb->actual_length = 0;
  1072. for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
  1073. cur_trb != event_trb;
  1074. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  1075. if (TRB_TYPE(cur_trb->generic.field[3]) != TRB_TR_NOOP &&
  1076. TRB_TYPE(cur_trb->generic.field[3]) != TRB_LINK)
  1077. td->urb->actual_length +=
  1078. TRB_LEN(cur_trb->generic.field[2]);
  1079. }
  1080. /* If the ring didn't stop on a Link or No-op TRB, add
  1081. * in the actual bytes transferred from the Normal TRB
  1082. */
  1083. if (trb_comp_code != COMP_STOP_INVAL)
  1084. td->urb->actual_length +=
  1085. TRB_LEN(cur_trb->generic.field[2]) -
  1086. TRB_LEN(event->transfer_len);
  1087. }
  1088. }
  1089. if (trb_comp_code == COMP_STOP_INVAL ||
  1090. trb_comp_code == COMP_STOP) {
  1091. /* The Endpoint Stop Command completion will take care of any
  1092. * stopped TDs. A stopped TD may be restarted, so don't update
  1093. * the ring dequeue pointer or take this TD off any lists yet.
  1094. */
  1095. ep_ring->stopped_td = td;
  1096. ep_ring->stopped_trb = event_trb;
  1097. } else {
  1098. if (trb_comp_code == COMP_STALL ||
  1099. trb_comp_code == COMP_BABBLE) {
  1100. /* The transfer is completed from the driver's
  1101. * perspective, but we need to issue a set dequeue
  1102. * command for this stalled endpoint to move the dequeue
  1103. * pointer past the TD. We can't do that here because
  1104. * the halt condition must be cleared first.
  1105. */
  1106. ep_ring->stopped_td = td;
  1107. ep_ring->stopped_trb = event_trb;
  1108. } else {
  1109. /* Update ring dequeue pointer */
  1110. while (ep_ring->dequeue != td->last_trb)
  1111. inc_deq(xhci, ep_ring, false);
  1112. inc_deq(xhci, ep_ring, false);
  1113. }
  1114. td_cleanup:
  1115. /* Clean up the endpoint's TD list */
  1116. urb = td->urb;
  1117. /* Do one last check of the actual transfer length.
  1118. * If the host controller said we transferred more data than
  1119. * the buffer length, urb->actual_length will be a very big
  1120. * number (since it's unsigned). Play it safe and say we didn't
  1121. * transfer anything.
  1122. */
  1123. if (urb->actual_length > urb->transfer_buffer_length) {
  1124. xhci_warn(xhci, "URB transfer length is wrong, "
  1125. "xHC issue? req. len = %u, "
  1126. "act. len = %u\n",
  1127. urb->transfer_buffer_length,
  1128. urb->actual_length);
  1129. urb->actual_length = 0;
  1130. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1131. status = -EREMOTEIO;
  1132. else
  1133. status = 0;
  1134. }
  1135. list_del(&td->td_list);
  1136. /* Was this TD slated to be cancelled but completed anyway? */
  1137. if (!list_empty(&td->cancelled_td_list)) {
  1138. list_del(&td->cancelled_td_list);
  1139. ep_ring->cancels_pending--;
  1140. }
  1141. /* Leave the TD around for the reset endpoint function to use
  1142. * (but only if it's not a control endpoint, since we already
  1143. * queued the Set TR dequeue pointer command for stalled
  1144. * control endpoints).
  1145. */
  1146. if (usb_endpoint_xfer_control(&urb->ep->desc) ||
  1147. (trb_comp_code != COMP_STALL &&
  1148. trb_comp_code != COMP_BABBLE)) {
  1149. kfree(td);
  1150. }
  1151. urb->hcpriv = NULL;
  1152. }
  1153. cleanup:
  1154. inc_deq(xhci, xhci->event_ring, true);
  1155. xhci_set_hc_event_deq(xhci);
  1156. /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
  1157. if (urb) {
  1158. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  1159. xhci_dbg(xhci, "Giveback URB %p, len = %d, status = %d\n",
  1160. urb, urb->actual_length, status);
  1161. spin_unlock(&xhci->lock);
  1162. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  1163. spin_lock(&xhci->lock);
  1164. }
  1165. return 0;
  1166. }
  1167. /*
  1168. * This function handles all OS-owned events on the event ring. It may drop
  1169. * xhci->lock between event processing (e.g. to pass up port status changes).
  1170. */
  1171. void xhci_handle_event(struct xhci_hcd *xhci)
  1172. {
  1173. union xhci_trb *event;
  1174. int update_ptrs = 1;
  1175. int ret;
  1176. xhci_dbg(xhci, "In %s\n", __func__);
  1177. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  1178. xhci->error_bitmask |= 1 << 1;
  1179. return;
  1180. }
  1181. event = xhci->event_ring->dequeue;
  1182. /* Does the HC or OS own the TRB? */
  1183. if ((event->event_cmd.flags & TRB_CYCLE) !=
  1184. xhci->event_ring->cycle_state) {
  1185. xhci->error_bitmask |= 1 << 2;
  1186. return;
  1187. }
  1188. xhci_dbg(xhci, "%s - OS owns TRB\n", __func__);
  1189. /* FIXME: Handle more event types. */
  1190. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  1191. case TRB_TYPE(TRB_COMPLETION):
  1192. xhci_dbg(xhci, "%s - calling handle_cmd_completion\n", __func__);
  1193. handle_cmd_completion(xhci, &event->event_cmd);
  1194. xhci_dbg(xhci, "%s - returned from handle_cmd_completion\n", __func__);
  1195. break;
  1196. case TRB_TYPE(TRB_PORT_STATUS):
  1197. xhci_dbg(xhci, "%s - calling handle_port_status\n", __func__);
  1198. handle_port_status(xhci, event);
  1199. xhci_dbg(xhci, "%s - returned from handle_port_status\n", __func__);
  1200. update_ptrs = 0;
  1201. break;
  1202. case TRB_TYPE(TRB_TRANSFER):
  1203. xhci_dbg(xhci, "%s - calling handle_tx_event\n", __func__);
  1204. ret = handle_tx_event(xhci, &event->trans_event);
  1205. xhci_dbg(xhci, "%s - returned from handle_tx_event\n", __func__);
  1206. if (ret < 0)
  1207. xhci->error_bitmask |= 1 << 9;
  1208. else
  1209. update_ptrs = 0;
  1210. break;
  1211. default:
  1212. xhci->error_bitmask |= 1 << 3;
  1213. }
  1214. if (update_ptrs) {
  1215. /* Update SW and HC event ring dequeue pointer */
  1216. inc_deq(xhci, xhci->event_ring, true);
  1217. xhci_set_hc_event_deq(xhci);
  1218. }
  1219. /* Are there more items on the event ring? */
  1220. xhci_handle_event(xhci);
  1221. }
  1222. /**** Endpoint Ring Operations ****/
  1223. /*
  1224. * Generic function for queueing a TRB on a ring.
  1225. * The caller must have checked to make sure there's room on the ring.
  1226. */
  1227. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  1228. bool consumer,
  1229. u32 field1, u32 field2, u32 field3, u32 field4)
  1230. {
  1231. struct xhci_generic_trb *trb;
  1232. trb = &ring->enqueue->generic;
  1233. trb->field[0] = field1;
  1234. trb->field[1] = field2;
  1235. trb->field[2] = field3;
  1236. trb->field[3] = field4;
  1237. inc_enq(xhci, ring, consumer);
  1238. }
  1239. /*
  1240. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  1241. * FIXME allocate segments if the ring is full.
  1242. */
  1243. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  1244. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  1245. {
  1246. /* Make sure the endpoint has been added to xHC schedule */
  1247. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  1248. switch (ep_state) {
  1249. case EP_STATE_DISABLED:
  1250. /*
  1251. * USB core changed config/interfaces without notifying us,
  1252. * or hardware is reporting the wrong state.
  1253. */
  1254. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  1255. return -ENOENT;
  1256. case EP_STATE_ERROR:
  1257. xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
  1258. /* FIXME event handling code for error needs to clear it */
  1259. /* XXX not sure if this should be -ENOENT or not */
  1260. return -EINVAL;
  1261. case EP_STATE_HALTED:
  1262. xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
  1263. case EP_STATE_STOPPED:
  1264. case EP_STATE_RUNNING:
  1265. break;
  1266. default:
  1267. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  1268. /*
  1269. * FIXME issue Configure Endpoint command to try to get the HC
  1270. * back into a known state.
  1271. */
  1272. return -EINVAL;
  1273. }
  1274. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  1275. /* FIXME allocate more room */
  1276. xhci_err(xhci, "ERROR no room on ep ring\n");
  1277. return -ENOMEM;
  1278. }
  1279. return 0;
  1280. }
  1281. static int prepare_transfer(struct xhci_hcd *xhci,
  1282. struct xhci_virt_device *xdev,
  1283. unsigned int ep_index,
  1284. unsigned int num_trbs,
  1285. struct urb *urb,
  1286. struct xhci_td **td,
  1287. gfp_t mem_flags)
  1288. {
  1289. int ret;
  1290. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1291. ret = prepare_ring(xhci, xdev->ep_rings[ep_index],
  1292. ep_ctx->ep_info & EP_STATE_MASK,
  1293. num_trbs, mem_flags);
  1294. if (ret)
  1295. return ret;
  1296. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  1297. if (!*td)
  1298. return -ENOMEM;
  1299. INIT_LIST_HEAD(&(*td)->td_list);
  1300. INIT_LIST_HEAD(&(*td)->cancelled_td_list);
  1301. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  1302. if (unlikely(ret)) {
  1303. kfree(*td);
  1304. return ret;
  1305. }
  1306. (*td)->urb = urb;
  1307. urb->hcpriv = (void *) (*td);
  1308. /* Add this TD to the tail of the endpoint ring's TD list */
  1309. list_add_tail(&(*td)->td_list, &xdev->ep_rings[ep_index]->td_list);
  1310. (*td)->start_seg = xdev->ep_rings[ep_index]->enq_seg;
  1311. (*td)->first_trb = xdev->ep_rings[ep_index]->enqueue;
  1312. return 0;
  1313. }
  1314. static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
  1315. {
  1316. int num_sgs, num_trbs, running_total, temp, i;
  1317. struct scatterlist *sg;
  1318. sg = NULL;
  1319. num_sgs = urb->num_sgs;
  1320. temp = urb->transfer_buffer_length;
  1321. xhci_dbg(xhci, "count sg list trbs: \n");
  1322. num_trbs = 0;
  1323. for_each_sg(urb->sg->sg, sg, num_sgs, i) {
  1324. unsigned int previous_total_trbs = num_trbs;
  1325. unsigned int len = sg_dma_len(sg);
  1326. /* Scatter gather list entries may cross 64KB boundaries */
  1327. running_total = TRB_MAX_BUFF_SIZE -
  1328. (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1329. if (running_total != 0)
  1330. num_trbs++;
  1331. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1332. while (running_total < sg_dma_len(sg)) {
  1333. num_trbs++;
  1334. running_total += TRB_MAX_BUFF_SIZE;
  1335. }
  1336. xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
  1337. i, (unsigned long long)sg_dma_address(sg),
  1338. len, len, num_trbs - previous_total_trbs);
  1339. len = min_t(int, len, temp);
  1340. temp -= len;
  1341. if (temp == 0)
  1342. break;
  1343. }
  1344. xhci_dbg(xhci, "\n");
  1345. if (!in_interrupt())
  1346. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, sglist used, num_trbs = %d\n",
  1347. urb->ep->desc.bEndpointAddress,
  1348. urb->transfer_buffer_length,
  1349. num_trbs);
  1350. return num_trbs;
  1351. }
  1352. static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
  1353. {
  1354. if (num_trbs != 0)
  1355. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  1356. "TRBs, %d left\n", __func__,
  1357. urb->ep->desc.bEndpointAddress, num_trbs);
  1358. if (running_total != urb->transfer_buffer_length)
  1359. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
  1360. "queued %#x (%d), asked for %#x (%d)\n",
  1361. __func__,
  1362. urb->ep->desc.bEndpointAddress,
  1363. running_total, running_total,
  1364. urb->transfer_buffer_length,
  1365. urb->transfer_buffer_length);
  1366. }
  1367. static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
  1368. unsigned int ep_index, int start_cycle,
  1369. struct xhci_generic_trb *start_trb, struct xhci_td *td)
  1370. {
  1371. /*
  1372. * Pass all the TRBs to the hardware at once and make sure this write
  1373. * isn't reordered.
  1374. */
  1375. wmb();
  1376. start_trb->field[3] |= start_cycle;
  1377. ring_ep_doorbell(xhci, slot_id, ep_index);
  1378. }
  1379. /*
  1380. * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
  1381. * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
  1382. * (comprised of sg list entries) can take several service intervals to
  1383. * transmit.
  1384. */
  1385. int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1386. struct urb *urb, int slot_id, unsigned int ep_index)
  1387. {
  1388. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
  1389. xhci->devs[slot_id]->out_ctx, ep_index);
  1390. int xhci_interval;
  1391. int ep_interval;
  1392. xhci_interval = EP_INTERVAL_TO_UFRAMES(ep_ctx->ep_info);
  1393. ep_interval = urb->interval;
  1394. /* Convert to microframes */
  1395. if (urb->dev->speed == USB_SPEED_LOW ||
  1396. urb->dev->speed == USB_SPEED_FULL)
  1397. ep_interval *= 8;
  1398. /* FIXME change this to a warning and a suggestion to use the new API
  1399. * to set the polling interval (once the API is added).
  1400. */
  1401. if (xhci_interval != ep_interval) {
  1402. if (!printk_ratelimit())
  1403. dev_dbg(&urb->dev->dev, "Driver uses different interval"
  1404. " (%d microframe%s) than xHCI "
  1405. "(%d microframe%s)\n",
  1406. ep_interval,
  1407. ep_interval == 1 ? "" : "s",
  1408. xhci_interval,
  1409. xhci_interval == 1 ? "" : "s");
  1410. urb->interval = xhci_interval;
  1411. /* Convert back to frames for LS/FS devices */
  1412. if (urb->dev->speed == USB_SPEED_LOW ||
  1413. urb->dev->speed == USB_SPEED_FULL)
  1414. urb->interval /= 8;
  1415. }
  1416. return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
  1417. }
  1418. static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1419. struct urb *urb, int slot_id, unsigned int ep_index)
  1420. {
  1421. struct xhci_ring *ep_ring;
  1422. unsigned int num_trbs;
  1423. struct xhci_td *td;
  1424. struct scatterlist *sg;
  1425. int num_sgs;
  1426. int trb_buff_len, this_sg_len, running_total;
  1427. bool first_trb;
  1428. u64 addr;
  1429. struct xhci_generic_trb *start_trb;
  1430. int start_cycle;
  1431. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1432. num_trbs = count_sg_trbs_needed(xhci, urb);
  1433. num_sgs = urb->num_sgs;
  1434. trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
  1435. ep_index, num_trbs, urb, &td, mem_flags);
  1436. if (trb_buff_len < 0)
  1437. return trb_buff_len;
  1438. /*
  1439. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1440. * until we've finished creating all the other TRBs. The ring's cycle
  1441. * state may change as we enqueue the other TRBs, so save it too.
  1442. */
  1443. start_trb = &ep_ring->enqueue->generic;
  1444. start_cycle = ep_ring->cycle_state;
  1445. running_total = 0;
  1446. /*
  1447. * How much data is in the first TRB?
  1448. *
  1449. * There are three forces at work for TRB buffer pointers and lengths:
  1450. * 1. We don't want to walk off the end of this sg-list entry buffer.
  1451. * 2. The transfer length that the driver requested may be smaller than
  1452. * the amount of memory allocated for this scatter-gather list.
  1453. * 3. TRBs buffers can't cross 64KB boundaries.
  1454. */
  1455. sg = urb->sg->sg;
  1456. addr = (u64) sg_dma_address(sg);
  1457. this_sg_len = sg_dma_len(sg);
  1458. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1459. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1460. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1461. if (trb_buff_len > urb->transfer_buffer_length)
  1462. trb_buff_len = urb->transfer_buffer_length;
  1463. xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
  1464. trb_buff_len);
  1465. first_trb = true;
  1466. /* Queue the first TRB, even if it's zero-length */
  1467. do {
  1468. u32 field = 0;
  1469. u32 length_field = 0;
  1470. /* Don't change the cycle bit of the first TRB until later */
  1471. if (first_trb)
  1472. first_trb = false;
  1473. else
  1474. field |= ep_ring->cycle_state;
  1475. /* Chain all the TRBs together; clear the chain bit in the last
  1476. * TRB to indicate it's the last TRB in the chain.
  1477. */
  1478. if (num_trbs > 1) {
  1479. field |= TRB_CHAIN;
  1480. } else {
  1481. /* FIXME - add check for ZERO_PACKET flag before this */
  1482. td->last_trb = ep_ring->enqueue;
  1483. field |= TRB_IOC;
  1484. }
  1485. xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
  1486. "64KB boundary at %#x, end dma = %#x\n",
  1487. (unsigned int) addr, trb_buff_len, trb_buff_len,
  1488. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1489. (unsigned int) addr + trb_buff_len);
  1490. if (TRB_MAX_BUFF_SIZE -
  1491. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
  1492. xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
  1493. xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
  1494. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1495. (unsigned int) addr + trb_buff_len);
  1496. }
  1497. length_field = TRB_LEN(trb_buff_len) |
  1498. TD_REMAINDER(urb->transfer_buffer_length - running_total) |
  1499. TRB_INTR_TARGET(0);
  1500. queue_trb(xhci, ep_ring, false,
  1501. lower_32_bits(addr),
  1502. upper_32_bits(addr),
  1503. length_field,
  1504. /* We always want to know if the TRB was short,
  1505. * or we won't get an event when it completes.
  1506. * (Unless we use event data TRBs, which are a
  1507. * waste of space and HC resources.)
  1508. */
  1509. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1510. --num_trbs;
  1511. running_total += trb_buff_len;
  1512. /* Calculate length for next transfer --
  1513. * Are we done queueing all the TRBs for this sg entry?
  1514. */
  1515. this_sg_len -= trb_buff_len;
  1516. if (this_sg_len == 0) {
  1517. --num_sgs;
  1518. if (num_sgs == 0)
  1519. break;
  1520. sg = sg_next(sg);
  1521. addr = (u64) sg_dma_address(sg);
  1522. this_sg_len = sg_dma_len(sg);
  1523. } else {
  1524. addr += trb_buff_len;
  1525. }
  1526. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1527. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1528. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1529. if (running_total + trb_buff_len > urb->transfer_buffer_length)
  1530. trb_buff_len =
  1531. urb->transfer_buffer_length - running_total;
  1532. } while (running_total < urb->transfer_buffer_length);
  1533. check_trb_math(urb, num_trbs, running_total);
  1534. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1535. return 0;
  1536. }
  1537. /* This is very similar to what ehci-q.c qtd_fill() does */
  1538. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1539. struct urb *urb, int slot_id, unsigned int ep_index)
  1540. {
  1541. struct xhci_ring *ep_ring;
  1542. struct xhci_td *td;
  1543. int num_trbs;
  1544. struct xhci_generic_trb *start_trb;
  1545. bool first_trb;
  1546. int start_cycle;
  1547. u32 field, length_field;
  1548. int running_total, trb_buff_len, ret;
  1549. u64 addr;
  1550. if (urb->sg)
  1551. return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
  1552. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1553. num_trbs = 0;
  1554. /* How much data is (potentially) left before the 64KB boundary? */
  1555. running_total = TRB_MAX_BUFF_SIZE -
  1556. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1557. /* If there's some data on this 64KB chunk, or we have to send a
  1558. * zero-length transfer, we need at least one TRB
  1559. */
  1560. if (running_total != 0 || urb->transfer_buffer_length == 0)
  1561. num_trbs++;
  1562. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1563. while (running_total < urb->transfer_buffer_length) {
  1564. num_trbs++;
  1565. running_total += TRB_MAX_BUFF_SIZE;
  1566. }
  1567. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  1568. if (!in_interrupt())
  1569. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d), addr = %#llx, num_trbs = %d\n",
  1570. urb->ep->desc.bEndpointAddress,
  1571. urb->transfer_buffer_length,
  1572. urb->transfer_buffer_length,
  1573. (unsigned long long)urb->transfer_dma,
  1574. num_trbs);
  1575. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
  1576. num_trbs, urb, &td, mem_flags);
  1577. if (ret < 0)
  1578. return ret;
  1579. /*
  1580. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1581. * until we've finished creating all the other TRBs. The ring's cycle
  1582. * state may change as we enqueue the other TRBs, so save it too.
  1583. */
  1584. start_trb = &ep_ring->enqueue->generic;
  1585. start_cycle = ep_ring->cycle_state;
  1586. running_total = 0;
  1587. /* How much data is in the first TRB? */
  1588. addr = (u64) urb->transfer_dma;
  1589. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1590. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1591. if (urb->transfer_buffer_length < trb_buff_len)
  1592. trb_buff_len = urb->transfer_buffer_length;
  1593. first_trb = true;
  1594. /* Queue the first TRB, even if it's zero-length */
  1595. do {
  1596. field = 0;
  1597. /* Don't change the cycle bit of the first TRB until later */
  1598. if (first_trb)
  1599. first_trb = false;
  1600. else
  1601. field |= ep_ring->cycle_state;
  1602. /* Chain all the TRBs together; clear the chain bit in the last
  1603. * TRB to indicate it's the last TRB in the chain.
  1604. */
  1605. if (num_trbs > 1) {
  1606. field |= TRB_CHAIN;
  1607. } else {
  1608. /* FIXME - add check for ZERO_PACKET flag before this */
  1609. td->last_trb = ep_ring->enqueue;
  1610. field |= TRB_IOC;
  1611. }
  1612. length_field = TRB_LEN(trb_buff_len) |
  1613. TD_REMAINDER(urb->transfer_buffer_length - running_total) |
  1614. TRB_INTR_TARGET(0);
  1615. queue_trb(xhci, ep_ring, false,
  1616. lower_32_bits(addr),
  1617. upper_32_bits(addr),
  1618. length_field,
  1619. /* We always want to know if the TRB was short,
  1620. * or we won't get an event when it completes.
  1621. * (Unless we use event data TRBs, which are a
  1622. * waste of space and HC resources.)
  1623. */
  1624. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1625. --num_trbs;
  1626. running_total += trb_buff_len;
  1627. /* Calculate length for next transfer */
  1628. addr += trb_buff_len;
  1629. trb_buff_len = urb->transfer_buffer_length - running_total;
  1630. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  1631. trb_buff_len = TRB_MAX_BUFF_SIZE;
  1632. } while (running_total < urb->transfer_buffer_length);
  1633. check_trb_math(urb, num_trbs, running_total);
  1634. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1635. return 0;
  1636. }
  1637. /* Caller must have locked xhci->lock */
  1638. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1639. struct urb *urb, int slot_id, unsigned int ep_index)
  1640. {
  1641. struct xhci_ring *ep_ring;
  1642. int num_trbs;
  1643. int ret;
  1644. struct usb_ctrlrequest *setup;
  1645. struct xhci_generic_trb *start_trb;
  1646. int start_cycle;
  1647. u32 field, length_field;
  1648. struct xhci_td *td;
  1649. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1650. /*
  1651. * Need to copy setup packet into setup TRB, so we can't use the setup
  1652. * DMA address.
  1653. */
  1654. if (!urb->setup_packet)
  1655. return -EINVAL;
  1656. if (!in_interrupt())
  1657. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  1658. slot_id, ep_index);
  1659. /* 1 TRB for setup, 1 for status */
  1660. num_trbs = 2;
  1661. /*
  1662. * Don't need to check if we need additional event data and normal TRBs,
  1663. * since data in control transfers will never get bigger than 16MB
  1664. * XXX: can we get a buffer that crosses 64KB boundaries?
  1665. */
  1666. if (urb->transfer_buffer_length > 0)
  1667. num_trbs++;
  1668. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
  1669. urb, &td, mem_flags);
  1670. if (ret < 0)
  1671. return ret;
  1672. /*
  1673. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1674. * until we've finished creating all the other TRBs. The ring's cycle
  1675. * state may change as we enqueue the other TRBs, so save it too.
  1676. */
  1677. start_trb = &ep_ring->enqueue->generic;
  1678. start_cycle = ep_ring->cycle_state;
  1679. /* Queue setup TRB - see section 6.4.1.2.1 */
  1680. /* FIXME better way to translate setup_packet into two u32 fields? */
  1681. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  1682. queue_trb(xhci, ep_ring, false,
  1683. /* FIXME endianness is probably going to bite my ass here. */
  1684. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  1685. setup->wIndex | setup->wLength << 16,
  1686. TRB_LEN(8) | TRB_INTR_TARGET(0),
  1687. /* Immediate data in pointer */
  1688. TRB_IDT | TRB_TYPE(TRB_SETUP));
  1689. /* If there's data, queue data TRBs */
  1690. field = 0;
  1691. length_field = TRB_LEN(urb->transfer_buffer_length) |
  1692. TD_REMAINDER(urb->transfer_buffer_length) |
  1693. TRB_INTR_TARGET(0);
  1694. if (urb->transfer_buffer_length > 0) {
  1695. if (setup->bRequestType & USB_DIR_IN)
  1696. field |= TRB_DIR_IN;
  1697. queue_trb(xhci, ep_ring, false,
  1698. lower_32_bits(urb->transfer_dma),
  1699. upper_32_bits(urb->transfer_dma),
  1700. length_field,
  1701. /* Event on short tx */
  1702. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  1703. }
  1704. /* Save the DMA address of the last TRB in the TD */
  1705. td->last_trb = ep_ring->enqueue;
  1706. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  1707. /* If the device sent data, the status stage is an OUT transfer */
  1708. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  1709. field = 0;
  1710. else
  1711. field = TRB_DIR_IN;
  1712. queue_trb(xhci, ep_ring, false,
  1713. 0,
  1714. 0,
  1715. TRB_INTR_TARGET(0),
  1716. /* Event on completion */
  1717. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  1718. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1719. return 0;
  1720. }
  1721. /**** Command Ring Operations ****/
  1722. /* Generic function for queueing a command TRB on the command ring */
  1723. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4)
  1724. {
  1725. if (!room_on_ring(xhci, xhci->cmd_ring, 1)) {
  1726. if (!in_interrupt())
  1727. xhci_err(xhci, "ERR: No room for command on command ring\n");
  1728. return -ENOMEM;
  1729. }
  1730. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  1731. field4 | xhci->cmd_ring->cycle_state);
  1732. return 0;
  1733. }
  1734. /* Queue a no-op command on the command ring */
  1735. static int queue_cmd_noop(struct xhci_hcd *xhci)
  1736. {
  1737. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP));
  1738. }
  1739. /*
  1740. * Place a no-op command on the command ring to test the command and
  1741. * event ring.
  1742. */
  1743. void *xhci_setup_one_noop(struct xhci_hcd *xhci)
  1744. {
  1745. if (queue_cmd_noop(xhci) < 0)
  1746. return NULL;
  1747. xhci->noops_submitted++;
  1748. return xhci_ring_cmd_db;
  1749. }
  1750. /* Queue a slot enable or disable request on the command ring */
  1751. int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  1752. {
  1753. return queue_command(xhci, 0, 0, 0,
  1754. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id));
  1755. }
  1756. /* Queue an address device command TRB */
  1757. int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1758. u32 slot_id)
  1759. {
  1760. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1761. upper_32_bits(in_ctx_ptr), 0,
  1762. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id));
  1763. }
  1764. /* Queue a configure endpoint command TRB */
  1765. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1766. u32 slot_id)
  1767. {
  1768. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1769. upper_32_bits(in_ctx_ptr), 0,
  1770. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id));
  1771. }
  1772. /* Queue an evaluate context command TRB */
  1773. int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1774. u32 slot_id)
  1775. {
  1776. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1777. upper_32_bits(in_ctx_ptr), 0,
  1778. TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id));
  1779. }
  1780. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
  1781. unsigned int ep_index)
  1782. {
  1783. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1784. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1785. u32 type = TRB_TYPE(TRB_STOP_RING);
  1786. return queue_command(xhci, 0, 0, 0,
  1787. trb_slot_id | trb_ep_index | type);
  1788. }
  1789. /* Set Transfer Ring Dequeue Pointer command.
  1790. * This should not be used for endpoints that have streams enabled.
  1791. */
  1792. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  1793. unsigned int ep_index, struct xhci_segment *deq_seg,
  1794. union xhci_trb *deq_ptr, u32 cycle_state)
  1795. {
  1796. dma_addr_t addr;
  1797. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1798. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1799. u32 type = TRB_TYPE(TRB_SET_DEQ);
  1800. addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
  1801. if (addr == 0) {
  1802. xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
  1803. xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
  1804. deq_seg, deq_ptr);
  1805. return 0;
  1806. }
  1807. return queue_command(xhci, lower_32_bits(addr) | cycle_state,
  1808. upper_32_bits(addr), 0,
  1809. trb_slot_id | trb_ep_index | type);
  1810. }
  1811. int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
  1812. unsigned int ep_index)
  1813. {
  1814. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1815. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1816. u32 type = TRB_TYPE(TRB_RESET_EP);
  1817. return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type);
  1818. }