xhci-ring.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  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. xhci_dbg(xhci, "In %s\n", __func__);
  816. slot_id = TRB_TO_SLOT_ID(event->flags);
  817. xdev = xhci->devs[slot_id];
  818. if (!xdev) {
  819. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  820. return -ENODEV;
  821. }
  822. /* Endpoint ID is 1 based, our index is zero based */
  823. ep_index = TRB_TO_EP_ID(event->flags) - 1;
  824. xhci_dbg(xhci, "%s - ep index = %d\n", __func__, ep_index);
  825. ep_ring = xdev->ep_rings[ep_index];
  826. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  827. if (!ep_ring || (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
  828. xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n");
  829. return -ENODEV;
  830. }
  831. event_dma = event->buffer;
  832. /* This TRB should be in the TD at the head of this ring's TD list */
  833. xhci_dbg(xhci, "%s - checking for list empty\n", __func__);
  834. if (list_empty(&ep_ring->td_list)) {
  835. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  836. TRB_TO_SLOT_ID(event->flags), ep_index);
  837. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  838. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  839. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  840. urb = NULL;
  841. goto cleanup;
  842. }
  843. xhci_dbg(xhci, "%s - getting list entry\n", __func__);
  844. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  845. /* Is this a TRB in the currently executing TD? */
  846. xhci_dbg(xhci, "%s - looking for TD\n", __func__);
  847. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  848. td->last_trb, event_dma);
  849. xhci_dbg(xhci, "%s - found event_seg = %p\n", __func__, event_seg);
  850. if (!event_seg) {
  851. /* HC is busted, give up! */
  852. xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
  853. return -ESHUTDOWN;
  854. }
  855. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
  856. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  857. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  858. xhci_dbg(xhci, "Offset 0x00 (buffer lo) = 0x%x\n",
  859. lower_32_bits(event->buffer));
  860. xhci_dbg(xhci, "Offset 0x04 (buffer hi) = 0x%x\n",
  861. upper_32_bits(event->buffer));
  862. xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
  863. (unsigned int) event->transfer_len);
  864. xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
  865. (unsigned int) event->flags);
  866. /* Look for common error cases */
  867. switch (GET_COMP_CODE(event->transfer_len)) {
  868. /* Skip codes that require special handling depending on
  869. * transfer type
  870. */
  871. case COMP_SUCCESS:
  872. case COMP_SHORT_TX:
  873. break;
  874. case COMP_STOP:
  875. xhci_dbg(xhci, "Stopped on Transfer TRB\n");
  876. break;
  877. case COMP_STOP_INVAL:
  878. xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
  879. break;
  880. case COMP_STALL:
  881. xhci_warn(xhci, "WARN: Stalled endpoint\n");
  882. ep_ring->state |= EP_HALTED;
  883. status = -EPIPE;
  884. break;
  885. case COMP_TRB_ERR:
  886. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  887. status = -EILSEQ;
  888. break;
  889. case COMP_TX_ERR:
  890. xhci_warn(xhci, "WARN: transfer error on endpoint\n");
  891. status = -EPROTO;
  892. break;
  893. case COMP_BABBLE:
  894. xhci_warn(xhci, "WARN: babble error on endpoint\n");
  895. status = -EOVERFLOW;
  896. break;
  897. case COMP_DB_ERR:
  898. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  899. status = -ENOSR;
  900. break;
  901. default:
  902. xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
  903. urb = NULL;
  904. goto cleanup;
  905. }
  906. /* Now update the urb's actual_length and give back to the core */
  907. /* Was this a control transfer? */
  908. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  909. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  910. switch (GET_COMP_CODE(event->transfer_len)) {
  911. case COMP_SUCCESS:
  912. if (event_trb == ep_ring->dequeue) {
  913. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  914. status = -ESHUTDOWN;
  915. } else if (event_trb != td->last_trb) {
  916. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  917. status = -ESHUTDOWN;
  918. } else {
  919. xhci_dbg(xhci, "Successful control transfer!\n");
  920. status = 0;
  921. }
  922. break;
  923. case COMP_SHORT_TX:
  924. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  925. status = -EREMOTEIO;
  926. break;
  927. case COMP_STALL:
  928. /* Did we transfer part of the data (middle) phase? */
  929. if (event_trb != ep_ring->dequeue &&
  930. event_trb != td->last_trb)
  931. td->urb->actual_length =
  932. td->urb->transfer_buffer_length
  933. - TRB_LEN(event->transfer_len);
  934. else
  935. td->urb->actual_length = 0;
  936. ep_ring->stopped_td = td;
  937. ep_ring->stopped_trb = event_trb;
  938. xhci_queue_reset_ep(xhci, slot_id, ep_index);
  939. xhci_cleanup_stalled_ring(xhci,
  940. td->urb->dev,
  941. ep_index, ep_ring);
  942. xhci_ring_cmd_db(xhci);
  943. goto td_cleanup;
  944. default:
  945. /* Others already handled above */
  946. break;
  947. }
  948. /*
  949. * Did we transfer any data, despite the errors that might have
  950. * happened? I.e. did we get past the setup stage?
  951. */
  952. if (event_trb != ep_ring->dequeue) {
  953. /* The event was for the status stage */
  954. if (event_trb == td->last_trb) {
  955. if (td->urb->actual_length != 0) {
  956. /* Don't overwrite a previously set error code */
  957. if (status == -EINPROGRESS || status == 0)
  958. /* Did we already see a short data stage? */
  959. status = -EREMOTEIO;
  960. } else {
  961. td->urb->actual_length =
  962. td->urb->transfer_buffer_length;
  963. }
  964. } else {
  965. /* Maybe the event was for the data stage? */
  966. if (GET_COMP_CODE(event->transfer_len) != COMP_STOP_INVAL) {
  967. /* We didn't stop on a link TRB in the middle */
  968. td->urb->actual_length =
  969. td->urb->transfer_buffer_length -
  970. TRB_LEN(event->transfer_len);
  971. xhci_dbg(xhci, "Waiting for status stage event\n");
  972. urb = NULL;
  973. goto cleanup;
  974. }
  975. }
  976. }
  977. } else {
  978. switch (GET_COMP_CODE(event->transfer_len)) {
  979. case COMP_SUCCESS:
  980. /* Double check that the HW transferred everything. */
  981. if (event_trb != td->last_trb) {
  982. xhci_warn(xhci, "WARN Successful completion "
  983. "on short TX\n");
  984. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  985. status = -EREMOTEIO;
  986. else
  987. status = 0;
  988. } else {
  989. xhci_dbg(xhci, "Successful bulk transfer!\n");
  990. status = 0;
  991. }
  992. break;
  993. case COMP_SHORT_TX:
  994. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  995. status = -EREMOTEIO;
  996. else
  997. status = 0;
  998. break;
  999. default:
  1000. /* Others already handled above */
  1001. break;
  1002. }
  1003. dev_dbg(&td->urb->dev->dev,
  1004. "ep %#x - asked for %d bytes, "
  1005. "%d bytes untransferred\n",
  1006. td->urb->ep->desc.bEndpointAddress,
  1007. td->urb->transfer_buffer_length,
  1008. TRB_LEN(event->transfer_len));
  1009. /* Fast path - was this the last TRB in the TD for this URB? */
  1010. if (event_trb == td->last_trb) {
  1011. if (TRB_LEN(event->transfer_len) != 0) {
  1012. td->urb->actual_length =
  1013. td->urb->transfer_buffer_length -
  1014. TRB_LEN(event->transfer_len);
  1015. if (td->urb->actual_length < 0) {
  1016. xhci_warn(xhci, "HC gave bad length "
  1017. "of %d bytes left\n",
  1018. TRB_LEN(event->transfer_len));
  1019. td->urb->actual_length = 0;
  1020. }
  1021. /* Don't overwrite a previously set error code */
  1022. if (status == -EINPROGRESS) {
  1023. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1024. status = -EREMOTEIO;
  1025. else
  1026. status = 0;
  1027. }
  1028. } else {
  1029. td->urb->actual_length = td->urb->transfer_buffer_length;
  1030. /* Ignore a short packet completion if the
  1031. * untransferred length was zero.
  1032. */
  1033. if (status == -EREMOTEIO)
  1034. status = 0;
  1035. }
  1036. } else {
  1037. /* Slow path - walk the list, starting from the dequeue
  1038. * pointer, to get the actual length transferred.
  1039. */
  1040. union xhci_trb *cur_trb;
  1041. struct xhci_segment *cur_seg;
  1042. td->urb->actual_length = 0;
  1043. for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
  1044. cur_trb != event_trb;
  1045. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  1046. if (TRB_TYPE(cur_trb->generic.field[3]) != TRB_TR_NOOP &&
  1047. TRB_TYPE(cur_trb->generic.field[3]) != TRB_LINK)
  1048. td->urb->actual_length +=
  1049. TRB_LEN(cur_trb->generic.field[2]);
  1050. }
  1051. /* If the ring didn't stop on a Link or No-op TRB, add
  1052. * in the actual bytes transferred from the Normal TRB
  1053. */
  1054. if (GET_COMP_CODE(event->transfer_len) != COMP_STOP_INVAL)
  1055. td->urb->actual_length +=
  1056. TRB_LEN(cur_trb->generic.field[2]) -
  1057. TRB_LEN(event->transfer_len);
  1058. }
  1059. }
  1060. if (GET_COMP_CODE(event->transfer_len) == COMP_STOP_INVAL ||
  1061. GET_COMP_CODE(event->transfer_len) == COMP_STOP) {
  1062. /* The Endpoint Stop Command completion will take care of any
  1063. * stopped TDs. A stopped TD may be restarted, so don't update
  1064. * the ring dequeue pointer or take this TD off any lists yet.
  1065. */
  1066. ep_ring->stopped_td = td;
  1067. ep_ring->stopped_trb = event_trb;
  1068. } else {
  1069. if (GET_COMP_CODE(event->transfer_len) == COMP_STALL) {
  1070. /* The transfer is completed from the driver's
  1071. * perspective, but we need to issue a set dequeue
  1072. * command for this stalled endpoint to move the dequeue
  1073. * pointer past the TD. We can't do that here because
  1074. * the halt condition must be cleared first.
  1075. */
  1076. ep_ring->stopped_td = td;
  1077. ep_ring->stopped_trb = event_trb;
  1078. } else {
  1079. /* Update ring dequeue pointer */
  1080. while (ep_ring->dequeue != td->last_trb)
  1081. inc_deq(xhci, ep_ring, false);
  1082. inc_deq(xhci, ep_ring, false);
  1083. }
  1084. td_cleanup:
  1085. /* Clean up the endpoint's TD list */
  1086. urb = td->urb;
  1087. list_del(&td->td_list);
  1088. /* Was this TD slated to be cancelled but completed anyway? */
  1089. if (!list_empty(&td->cancelled_td_list)) {
  1090. list_del(&td->cancelled_td_list);
  1091. ep_ring->cancels_pending--;
  1092. }
  1093. /* Leave the TD around for the reset endpoint function to use
  1094. * (but only if it's not a control endpoint, since we already
  1095. * queued the Set TR dequeue pointer command for stalled
  1096. * control endpoints).
  1097. */
  1098. if (usb_endpoint_xfer_control(&urb->ep->desc) ||
  1099. GET_COMP_CODE(event->transfer_len) != COMP_STALL) {
  1100. kfree(td);
  1101. }
  1102. urb->hcpriv = NULL;
  1103. }
  1104. cleanup:
  1105. inc_deq(xhci, xhci->event_ring, true);
  1106. xhci_set_hc_event_deq(xhci);
  1107. /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
  1108. if (urb) {
  1109. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  1110. xhci_dbg(xhci, "Giveback URB %p, len = %d, status = %d\n",
  1111. urb, td->urb->actual_length, status);
  1112. spin_unlock(&xhci->lock);
  1113. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  1114. spin_lock(&xhci->lock);
  1115. }
  1116. return 0;
  1117. }
  1118. /*
  1119. * This function handles all OS-owned events on the event ring. It may drop
  1120. * xhci->lock between event processing (e.g. to pass up port status changes).
  1121. */
  1122. void xhci_handle_event(struct xhci_hcd *xhci)
  1123. {
  1124. union xhci_trb *event;
  1125. int update_ptrs = 1;
  1126. int ret;
  1127. xhci_dbg(xhci, "In %s\n", __func__);
  1128. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  1129. xhci->error_bitmask |= 1 << 1;
  1130. return;
  1131. }
  1132. event = xhci->event_ring->dequeue;
  1133. /* Does the HC or OS own the TRB? */
  1134. if ((event->event_cmd.flags & TRB_CYCLE) !=
  1135. xhci->event_ring->cycle_state) {
  1136. xhci->error_bitmask |= 1 << 2;
  1137. return;
  1138. }
  1139. xhci_dbg(xhci, "%s - OS owns TRB\n", __func__);
  1140. /* FIXME: Handle more event types. */
  1141. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  1142. case TRB_TYPE(TRB_COMPLETION):
  1143. xhci_dbg(xhci, "%s - calling handle_cmd_completion\n", __func__);
  1144. handle_cmd_completion(xhci, &event->event_cmd);
  1145. xhci_dbg(xhci, "%s - returned from handle_cmd_completion\n", __func__);
  1146. break;
  1147. case TRB_TYPE(TRB_PORT_STATUS):
  1148. xhci_dbg(xhci, "%s - calling handle_port_status\n", __func__);
  1149. handle_port_status(xhci, event);
  1150. xhci_dbg(xhci, "%s - returned from handle_port_status\n", __func__);
  1151. update_ptrs = 0;
  1152. break;
  1153. case TRB_TYPE(TRB_TRANSFER):
  1154. xhci_dbg(xhci, "%s - calling handle_tx_event\n", __func__);
  1155. ret = handle_tx_event(xhci, &event->trans_event);
  1156. xhci_dbg(xhci, "%s - returned from handle_tx_event\n", __func__);
  1157. if (ret < 0)
  1158. xhci->error_bitmask |= 1 << 9;
  1159. else
  1160. update_ptrs = 0;
  1161. break;
  1162. default:
  1163. xhci->error_bitmask |= 1 << 3;
  1164. }
  1165. if (update_ptrs) {
  1166. /* Update SW and HC event ring dequeue pointer */
  1167. inc_deq(xhci, xhci->event_ring, true);
  1168. xhci_set_hc_event_deq(xhci);
  1169. }
  1170. /* Are there more items on the event ring? */
  1171. xhci_handle_event(xhci);
  1172. }
  1173. /**** Endpoint Ring Operations ****/
  1174. /*
  1175. * Generic function for queueing a TRB on a ring.
  1176. * The caller must have checked to make sure there's room on the ring.
  1177. */
  1178. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  1179. bool consumer,
  1180. u32 field1, u32 field2, u32 field3, u32 field4)
  1181. {
  1182. struct xhci_generic_trb *trb;
  1183. trb = &ring->enqueue->generic;
  1184. trb->field[0] = field1;
  1185. trb->field[1] = field2;
  1186. trb->field[2] = field3;
  1187. trb->field[3] = field4;
  1188. inc_enq(xhci, ring, consumer);
  1189. }
  1190. /*
  1191. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  1192. * FIXME allocate segments if the ring is full.
  1193. */
  1194. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  1195. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  1196. {
  1197. /* Make sure the endpoint has been added to xHC schedule */
  1198. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  1199. switch (ep_state) {
  1200. case EP_STATE_DISABLED:
  1201. /*
  1202. * USB core changed config/interfaces without notifying us,
  1203. * or hardware is reporting the wrong state.
  1204. */
  1205. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  1206. return -ENOENT;
  1207. case EP_STATE_ERROR:
  1208. xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
  1209. /* FIXME event handling code for error needs to clear it */
  1210. /* XXX not sure if this should be -ENOENT or not */
  1211. return -EINVAL;
  1212. case EP_STATE_HALTED:
  1213. xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
  1214. case EP_STATE_STOPPED:
  1215. case EP_STATE_RUNNING:
  1216. break;
  1217. default:
  1218. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  1219. /*
  1220. * FIXME issue Configure Endpoint command to try to get the HC
  1221. * back into a known state.
  1222. */
  1223. return -EINVAL;
  1224. }
  1225. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  1226. /* FIXME allocate more room */
  1227. xhci_err(xhci, "ERROR no room on ep ring\n");
  1228. return -ENOMEM;
  1229. }
  1230. return 0;
  1231. }
  1232. static int prepare_transfer(struct xhci_hcd *xhci,
  1233. struct xhci_virt_device *xdev,
  1234. unsigned int ep_index,
  1235. unsigned int num_trbs,
  1236. struct urb *urb,
  1237. struct xhci_td **td,
  1238. gfp_t mem_flags)
  1239. {
  1240. int ret;
  1241. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1242. ret = prepare_ring(xhci, xdev->ep_rings[ep_index],
  1243. ep_ctx->ep_info & EP_STATE_MASK,
  1244. num_trbs, mem_flags);
  1245. if (ret)
  1246. return ret;
  1247. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  1248. if (!*td)
  1249. return -ENOMEM;
  1250. INIT_LIST_HEAD(&(*td)->td_list);
  1251. INIT_LIST_HEAD(&(*td)->cancelled_td_list);
  1252. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  1253. if (unlikely(ret)) {
  1254. kfree(*td);
  1255. return ret;
  1256. }
  1257. (*td)->urb = urb;
  1258. urb->hcpriv = (void *) (*td);
  1259. /* Add this TD to the tail of the endpoint ring's TD list */
  1260. list_add_tail(&(*td)->td_list, &xdev->ep_rings[ep_index]->td_list);
  1261. (*td)->start_seg = xdev->ep_rings[ep_index]->enq_seg;
  1262. (*td)->first_trb = xdev->ep_rings[ep_index]->enqueue;
  1263. return 0;
  1264. }
  1265. static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
  1266. {
  1267. int num_sgs, num_trbs, running_total, temp, i;
  1268. struct scatterlist *sg;
  1269. sg = NULL;
  1270. num_sgs = urb->num_sgs;
  1271. temp = urb->transfer_buffer_length;
  1272. xhci_dbg(xhci, "count sg list trbs: \n");
  1273. num_trbs = 0;
  1274. for_each_sg(urb->sg->sg, sg, num_sgs, i) {
  1275. unsigned int previous_total_trbs = num_trbs;
  1276. unsigned int len = sg_dma_len(sg);
  1277. /* Scatter gather list entries may cross 64KB boundaries */
  1278. running_total = TRB_MAX_BUFF_SIZE -
  1279. (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1280. if (running_total != 0)
  1281. num_trbs++;
  1282. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1283. while (running_total < sg_dma_len(sg)) {
  1284. num_trbs++;
  1285. running_total += TRB_MAX_BUFF_SIZE;
  1286. }
  1287. xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
  1288. i, (unsigned long long)sg_dma_address(sg),
  1289. len, len, num_trbs - previous_total_trbs);
  1290. len = min_t(int, len, temp);
  1291. temp -= len;
  1292. if (temp == 0)
  1293. break;
  1294. }
  1295. xhci_dbg(xhci, "\n");
  1296. if (!in_interrupt())
  1297. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, sglist used, num_trbs = %d\n",
  1298. urb->ep->desc.bEndpointAddress,
  1299. urb->transfer_buffer_length,
  1300. num_trbs);
  1301. return num_trbs;
  1302. }
  1303. static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
  1304. {
  1305. if (num_trbs != 0)
  1306. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  1307. "TRBs, %d left\n", __func__,
  1308. urb->ep->desc.bEndpointAddress, num_trbs);
  1309. if (running_total != urb->transfer_buffer_length)
  1310. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
  1311. "queued %#x (%d), asked for %#x (%d)\n",
  1312. __func__,
  1313. urb->ep->desc.bEndpointAddress,
  1314. running_total, running_total,
  1315. urb->transfer_buffer_length,
  1316. urb->transfer_buffer_length);
  1317. }
  1318. static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
  1319. unsigned int ep_index, int start_cycle,
  1320. struct xhci_generic_trb *start_trb, struct xhci_td *td)
  1321. {
  1322. /*
  1323. * Pass all the TRBs to the hardware at once and make sure this write
  1324. * isn't reordered.
  1325. */
  1326. wmb();
  1327. start_trb->field[3] |= start_cycle;
  1328. ring_ep_doorbell(xhci, slot_id, ep_index);
  1329. }
  1330. static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1331. struct urb *urb, int slot_id, unsigned int ep_index)
  1332. {
  1333. struct xhci_ring *ep_ring;
  1334. unsigned int num_trbs;
  1335. struct xhci_td *td;
  1336. struct scatterlist *sg;
  1337. int num_sgs;
  1338. int trb_buff_len, this_sg_len, running_total;
  1339. bool first_trb;
  1340. u64 addr;
  1341. struct xhci_generic_trb *start_trb;
  1342. int start_cycle;
  1343. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1344. num_trbs = count_sg_trbs_needed(xhci, urb);
  1345. num_sgs = urb->num_sgs;
  1346. trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
  1347. ep_index, num_trbs, urb, &td, mem_flags);
  1348. if (trb_buff_len < 0)
  1349. return trb_buff_len;
  1350. /*
  1351. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1352. * until we've finished creating all the other TRBs. The ring's cycle
  1353. * state may change as we enqueue the other TRBs, so save it too.
  1354. */
  1355. start_trb = &ep_ring->enqueue->generic;
  1356. start_cycle = ep_ring->cycle_state;
  1357. running_total = 0;
  1358. /*
  1359. * How much data is in the first TRB?
  1360. *
  1361. * There are three forces at work for TRB buffer pointers and lengths:
  1362. * 1. We don't want to walk off the end of this sg-list entry buffer.
  1363. * 2. The transfer length that the driver requested may be smaller than
  1364. * the amount of memory allocated for this scatter-gather list.
  1365. * 3. TRBs buffers can't cross 64KB boundaries.
  1366. */
  1367. sg = urb->sg->sg;
  1368. addr = (u64) sg_dma_address(sg);
  1369. this_sg_len = sg_dma_len(sg);
  1370. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1371. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1372. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1373. if (trb_buff_len > urb->transfer_buffer_length)
  1374. trb_buff_len = urb->transfer_buffer_length;
  1375. xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
  1376. trb_buff_len);
  1377. first_trb = true;
  1378. /* Queue the first TRB, even if it's zero-length */
  1379. do {
  1380. u32 field = 0;
  1381. u32 length_field = 0;
  1382. /* Don't change the cycle bit of the first TRB until later */
  1383. if (first_trb)
  1384. first_trb = false;
  1385. else
  1386. field |= ep_ring->cycle_state;
  1387. /* Chain all the TRBs together; clear the chain bit in the last
  1388. * TRB to indicate it's the last TRB in the chain.
  1389. */
  1390. if (num_trbs > 1) {
  1391. field |= TRB_CHAIN;
  1392. } else {
  1393. /* FIXME - add check for ZERO_PACKET flag before this */
  1394. td->last_trb = ep_ring->enqueue;
  1395. field |= TRB_IOC;
  1396. }
  1397. xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
  1398. "64KB boundary at %#x, end dma = %#x\n",
  1399. (unsigned int) addr, trb_buff_len, trb_buff_len,
  1400. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1401. (unsigned int) addr + trb_buff_len);
  1402. if (TRB_MAX_BUFF_SIZE -
  1403. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
  1404. xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
  1405. xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
  1406. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1407. (unsigned int) addr + trb_buff_len);
  1408. }
  1409. length_field = TRB_LEN(trb_buff_len) |
  1410. TD_REMAINDER(urb->transfer_buffer_length - running_total) |
  1411. TRB_INTR_TARGET(0);
  1412. queue_trb(xhci, ep_ring, false,
  1413. lower_32_bits(addr),
  1414. upper_32_bits(addr),
  1415. length_field,
  1416. /* We always want to know if the TRB was short,
  1417. * or we won't get an event when it completes.
  1418. * (Unless we use event data TRBs, which are a
  1419. * waste of space and HC resources.)
  1420. */
  1421. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1422. --num_trbs;
  1423. running_total += trb_buff_len;
  1424. /* Calculate length for next transfer --
  1425. * Are we done queueing all the TRBs for this sg entry?
  1426. */
  1427. this_sg_len -= trb_buff_len;
  1428. if (this_sg_len == 0) {
  1429. --num_sgs;
  1430. if (num_sgs == 0)
  1431. break;
  1432. sg = sg_next(sg);
  1433. addr = (u64) sg_dma_address(sg);
  1434. this_sg_len = sg_dma_len(sg);
  1435. } else {
  1436. addr += trb_buff_len;
  1437. }
  1438. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1439. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1440. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1441. if (running_total + trb_buff_len > urb->transfer_buffer_length)
  1442. trb_buff_len =
  1443. urb->transfer_buffer_length - running_total;
  1444. } while (running_total < urb->transfer_buffer_length);
  1445. check_trb_math(urb, num_trbs, running_total);
  1446. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1447. return 0;
  1448. }
  1449. /* This is very similar to what ehci-q.c qtd_fill() does */
  1450. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1451. struct urb *urb, int slot_id, unsigned int ep_index)
  1452. {
  1453. struct xhci_ring *ep_ring;
  1454. struct xhci_td *td;
  1455. int num_trbs;
  1456. struct xhci_generic_trb *start_trb;
  1457. bool first_trb;
  1458. int start_cycle;
  1459. u32 field, length_field;
  1460. int running_total, trb_buff_len, ret;
  1461. u64 addr;
  1462. if (urb->sg)
  1463. return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
  1464. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1465. num_trbs = 0;
  1466. /* How much data is (potentially) left before the 64KB boundary? */
  1467. running_total = TRB_MAX_BUFF_SIZE -
  1468. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1469. /* If there's some data on this 64KB chunk, or we have to send a
  1470. * zero-length transfer, we need at least one TRB
  1471. */
  1472. if (running_total != 0 || urb->transfer_buffer_length == 0)
  1473. num_trbs++;
  1474. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1475. while (running_total < urb->transfer_buffer_length) {
  1476. num_trbs++;
  1477. running_total += TRB_MAX_BUFF_SIZE;
  1478. }
  1479. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  1480. if (!in_interrupt())
  1481. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d), addr = %#llx, num_trbs = %d\n",
  1482. urb->ep->desc.bEndpointAddress,
  1483. urb->transfer_buffer_length,
  1484. urb->transfer_buffer_length,
  1485. (unsigned long long)urb->transfer_dma,
  1486. num_trbs);
  1487. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
  1488. num_trbs, urb, &td, mem_flags);
  1489. if (ret < 0)
  1490. return ret;
  1491. /*
  1492. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1493. * until we've finished creating all the other TRBs. The ring's cycle
  1494. * state may change as we enqueue the other TRBs, so save it too.
  1495. */
  1496. start_trb = &ep_ring->enqueue->generic;
  1497. start_cycle = ep_ring->cycle_state;
  1498. running_total = 0;
  1499. /* How much data is in the first TRB? */
  1500. addr = (u64) urb->transfer_dma;
  1501. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1502. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1503. if (urb->transfer_buffer_length < trb_buff_len)
  1504. trb_buff_len = urb->transfer_buffer_length;
  1505. first_trb = true;
  1506. /* Queue the first TRB, even if it's zero-length */
  1507. do {
  1508. field = 0;
  1509. /* Don't change the cycle bit of the first TRB until later */
  1510. if (first_trb)
  1511. first_trb = false;
  1512. else
  1513. field |= ep_ring->cycle_state;
  1514. /* Chain all the TRBs together; clear the chain bit in the last
  1515. * TRB to indicate it's the last TRB in the chain.
  1516. */
  1517. if (num_trbs > 1) {
  1518. field |= TRB_CHAIN;
  1519. } else {
  1520. /* FIXME - add check for ZERO_PACKET flag before this */
  1521. td->last_trb = ep_ring->enqueue;
  1522. field |= TRB_IOC;
  1523. }
  1524. length_field = TRB_LEN(trb_buff_len) |
  1525. TD_REMAINDER(urb->transfer_buffer_length - running_total) |
  1526. TRB_INTR_TARGET(0);
  1527. queue_trb(xhci, ep_ring, false,
  1528. lower_32_bits(addr),
  1529. upper_32_bits(addr),
  1530. length_field,
  1531. /* We always want to know if the TRB was short,
  1532. * or we won't get an event when it completes.
  1533. * (Unless we use event data TRBs, which are a
  1534. * waste of space and HC resources.)
  1535. */
  1536. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1537. --num_trbs;
  1538. running_total += trb_buff_len;
  1539. /* Calculate length for next transfer */
  1540. addr += trb_buff_len;
  1541. trb_buff_len = urb->transfer_buffer_length - running_total;
  1542. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  1543. trb_buff_len = TRB_MAX_BUFF_SIZE;
  1544. } while (running_total < urb->transfer_buffer_length);
  1545. check_trb_math(urb, num_trbs, running_total);
  1546. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1547. return 0;
  1548. }
  1549. /* Caller must have locked xhci->lock */
  1550. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1551. struct urb *urb, int slot_id, unsigned int ep_index)
  1552. {
  1553. struct xhci_ring *ep_ring;
  1554. int num_trbs;
  1555. int ret;
  1556. struct usb_ctrlrequest *setup;
  1557. struct xhci_generic_trb *start_trb;
  1558. int start_cycle;
  1559. u32 field, length_field;
  1560. struct xhci_td *td;
  1561. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1562. /*
  1563. * Need to copy setup packet into setup TRB, so we can't use the setup
  1564. * DMA address.
  1565. */
  1566. if (!urb->setup_packet)
  1567. return -EINVAL;
  1568. if (!in_interrupt())
  1569. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  1570. slot_id, ep_index);
  1571. /* 1 TRB for setup, 1 for status */
  1572. num_trbs = 2;
  1573. /*
  1574. * Don't need to check if we need additional event data and normal TRBs,
  1575. * since data in control transfers will never get bigger than 16MB
  1576. * XXX: can we get a buffer that crosses 64KB boundaries?
  1577. */
  1578. if (urb->transfer_buffer_length > 0)
  1579. num_trbs++;
  1580. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
  1581. urb, &td, mem_flags);
  1582. if (ret < 0)
  1583. return ret;
  1584. /*
  1585. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1586. * until we've finished creating all the other TRBs. The ring's cycle
  1587. * state may change as we enqueue the other TRBs, so save it too.
  1588. */
  1589. start_trb = &ep_ring->enqueue->generic;
  1590. start_cycle = ep_ring->cycle_state;
  1591. /* Queue setup TRB - see section 6.4.1.2.1 */
  1592. /* FIXME better way to translate setup_packet into two u32 fields? */
  1593. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  1594. queue_trb(xhci, ep_ring, false,
  1595. /* FIXME endianness is probably going to bite my ass here. */
  1596. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  1597. setup->wIndex | setup->wLength << 16,
  1598. TRB_LEN(8) | TRB_INTR_TARGET(0),
  1599. /* Immediate data in pointer */
  1600. TRB_IDT | TRB_TYPE(TRB_SETUP));
  1601. /* If there's data, queue data TRBs */
  1602. field = 0;
  1603. length_field = TRB_LEN(urb->transfer_buffer_length) |
  1604. TD_REMAINDER(urb->transfer_buffer_length) |
  1605. TRB_INTR_TARGET(0);
  1606. if (urb->transfer_buffer_length > 0) {
  1607. if (setup->bRequestType & USB_DIR_IN)
  1608. field |= TRB_DIR_IN;
  1609. queue_trb(xhci, ep_ring, false,
  1610. lower_32_bits(urb->transfer_dma),
  1611. upper_32_bits(urb->transfer_dma),
  1612. length_field,
  1613. /* Event on short tx */
  1614. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  1615. }
  1616. /* Save the DMA address of the last TRB in the TD */
  1617. td->last_trb = ep_ring->enqueue;
  1618. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  1619. /* If the device sent data, the status stage is an OUT transfer */
  1620. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  1621. field = 0;
  1622. else
  1623. field = TRB_DIR_IN;
  1624. queue_trb(xhci, ep_ring, false,
  1625. 0,
  1626. 0,
  1627. TRB_INTR_TARGET(0),
  1628. /* Event on completion */
  1629. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  1630. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1631. return 0;
  1632. }
  1633. /**** Command Ring Operations ****/
  1634. /* Generic function for queueing a command TRB on the command ring */
  1635. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4)
  1636. {
  1637. if (!room_on_ring(xhci, xhci->cmd_ring, 1)) {
  1638. if (!in_interrupt())
  1639. xhci_err(xhci, "ERR: No room for command on command ring\n");
  1640. return -ENOMEM;
  1641. }
  1642. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  1643. field4 | xhci->cmd_ring->cycle_state);
  1644. return 0;
  1645. }
  1646. /* Queue a no-op command on the command ring */
  1647. static int queue_cmd_noop(struct xhci_hcd *xhci)
  1648. {
  1649. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP));
  1650. }
  1651. /*
  1652. * Place a no-op command on the command ring to test the command and
  1653. * event ring.
  1654. */
  1655. void *xhci_setup_one_noop(struct xhci_hcd *xhci)
  1656. {
  1657. if (queue_cmd_noop(xhci) < 0)
  1658. return NULL;
  1659. xhci->noops_submitted++;
  1660. return xhci_ring_cmd_db;
  1661. }
  1662. /* Queue a slot enable or disable request on the command ring */
  1663. int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  1664. {
  1665. return queue_command(xhci, 0, 0, 0,
  1666. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id));
  1667. }
  1668. /* Queue an address device command TRB */
  1669. int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1670. u32 slot_id)
  1671. {
  1672. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1673. upper_32_bits(in_ctx_ptr), 0,
  1674. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id));
  1675. }
  1676. /* Queue a configure endpoint command TRB */
  1677. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1678. u32 slot_id)
  1679. {
  1680. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1681. upper_32_bits(in_ctx_ptr), 0,
  1682. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id));
  1683. }
  1684. /* Queue an evaluate context command TRB */
  1685. int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1686. u32 slot_id)
  1687. {
  1688. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1689. upper_32_bits(in_ctx_ptr), 0,
  1690. TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id));
  1691. }
  1692. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
  1693. unsigned int ep_index)
  1694. {
  1695. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1696. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1697. u32 type = TRB_TYPE(TRB_STOP_RING);
  1698. return queue_command(xhci, 0, 0, 0,
  1699. trb_slot_id | trb_ep_index | type);
  1700. }
  1701. /* Set Transfer Ring Dequeue Pointer command.
  1702. * This should not be used for endpoints that have streams enabled.
  1703. */
  1704. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  1705. unsigned int ep_index, struct xhci_segment *deq_seg,
  1706. union xhci_trb *deq_ptr, u32 cycle_state)
  1707. {
  1708. dma_addr_t addr;
  1709. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1710. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1711. u32 type = TRB_TYPE(TRB_SET_DEQ);
  1712. addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
  1713. if (addr == 0) {
  1714. xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
  1715. xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
  1716. deq_seg, deq_ptr);
  1717. return 0;
  1718. }
  1719. return queue_command(xhci, lower_32_bits(addr) | cycle_state,
  1720. upper_32_bits(addr), 0,
  1721. trb_slot_id | trb_ep_index | type);
  1722. }
  1723. int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
  1724. unsigned int ep_index)
  1725. {
  1726. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1727. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1728. u32 type = TRB_TYPE(TRB_RESET_EP);
  1729. return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type);
  1730. }