xhci-ring.c 63 KB

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