xhci-ring.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. xhci_ring_cmd_db(xhci);
  442. }
  443. /*
  444. * When we get a command completion for a Stop Endpoint Command, we need to
  445. * unlink any cancelled TDs from the ring. There are two ways to do that:
  446. *
  447. * 1. If the HW was in the middle of processing the TD that needs to be
  448. * cancelled, then we must move the ring's dequeue pointer past the last TRB
  449. * in the TD with a Set Dequeue Pointer Command.
  450. * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
  451. * bit cleared) so that the HW will skip over them.
  452. */
  453. static void handle_stopped_endpoint(struct xhci_hcd *xhci,
  454. union xhci_trb *trb)
  455. {
  456. unsigned int slot_id;
  457. unsigned int ep_index;
  458. struct xhci_ring *ep_ring;
  459. struct list_head *entry;
  460. struct xhci_td *cur_td = 0;
  461. struct xhci_td *last_unlinked_td;
  462. struct xhci_dequeue_state deq_state;
  463. #ifdef CONFIG_USB_HCD_STAT
  464. ktime_t stop_time = ktime_get();
  465. #endif
  466. memset(&deq_state, 0, sizeof(deq_state));
  467. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  468. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  469. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  470. if (list_empty(&ep_ring->cancelled_td_list))
  471. return;
  472. /* Fix up the ep ring first, so HW stops executing cancelled TDs.
  473. * We have the xHCI lock, so nothing can modify this list until we drop
  474. * it. We're also in the event handler, so we can't get re-interrupted
  475. * if another Stop Endpoint command completes
  476. */
  477. list_for_each(entry, &ep_ring->cancelled_td_list) {
  478. cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
  479. xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
  480. cur_td->first_trb,
  481. (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
  482. /*
  483. * If we stopped on the TD we need to cancel, then we have to
  484. * move the xHC endpoint ring dequeue pointer past this TD.
  485. */
  486. if (cur_td == ep_ring->stopped_td)
  487. xhci_find_new_dequeue_state(xhci, slot_id, ep_index, cur_td,
  488. &deq_state);
  489. else
  490. td_to_noop(xhci, ep_ring, cur_td);
  491. /*
  492. * The event handler won't see a completion for this TD anymore,
  493. * so remove it from the endpoint ring's TD list. Keep it in
  494. * the cancelled TD list for URB completion later.
  495. */
  496. list_del(&cur_td->td_list);
  497. ep_ring->cancels_pending--;
  498. }
  499. last_unlinked_td = cur_td;
  500. /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
  501. if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
  502. xhci_queue_new_dequeue_state(xhci, ep_ring,
  503. slot_id, ep_index, &deq_state);
  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. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  607. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  608. /* This command will only fail if the endpoint wasn't halted,
  609. * but we don't care.
  610. */
  611. xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
  612. (unsigned int) GET_COMP_CODE(event->status));
  613. /* Clear our internal halted state and restart the ring */
  614. xhci->devs[slot_id]->ep_rings[ep_index]->state &= ~EP_HALTED;
  615. ring_ep_doorbell(xhci, slot_id, ep_index);
  616. }
  617. static void handle_cmd_completion(struct xhci_hcd *xhci,
  618. struct xhci_event_cmd *event)
  619. {
  620. int slot_id = TRB_TO_SLOT_ID(event->flags);
  621. u64 cmd_dma;
  622. dma_addr_t cmd_dequeue_dma;
  623. cmd_dma = event->cmd_trb;
  624. cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  625. xhci->cmd_ring->dequeue);
  626. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  627. if (cmd_dequeue_dma == 0) {
  628. xhci->error_bitmask |= 1 << 4;
  629. return;
  630. }
  631. /* Does the DMA address match our internal dequeue pointer address? */
  632. if (cmd_dma != (u64) cmd_dequeue_dma) {
  633. xhci->error_bitmask |= 1 << 5;
  634. return;
  635. }
  636. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  637. case TRB_TYPE(TRB_ENABLE_SLOT):
  638. if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
  639. xhci->slot_id = slot_id;
  640. else
  641. xhci->slot_id = 0;
  642. complete(&xhci->addr_dev);
  643. break;
  644. case TRB_TYPE(TRB_DISABLE_SLOT):
  645. if (xhci->devs[slot_id])
  646. xhci_free_virt_device(xhci, slot_id);
  647. break;
  648. case TRB_TYPE(TRB_CONFIG_EP):
  649. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  650. complete(&xhci->devs[slot_id]->cmd_completion);
  651. break;
  652. case TRB_TYPE(TRB_ADDR_DEV):
  653. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  654. complete(&xhci->addr_dev);
  655. break;
  656. case TRB_TYPE(TRB_STOP_RING):
  657. handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue);
  658. break;
  659. case TRB_TYPE(TRB_SET_DEQ):
  660. handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
  661. break;
  662. case TRB_TYPE(TRB_CMD_NOOP):
  663. ++xhci->noops_handled;
  664. break;
  665. case TRB_TYPE(TRB_RESET_EP):
  666. handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
  667. break;
  668. default:
  669. /* Skip over unknown commands on the event ring */
  670. xhci->error_bitmask |= 1 << 6;
  671. break;
  672. }
  673. inc_deq(xhci, xhci->cmd_ring, false);
  674. }
  675. static void handle_port_status(struct xhci_hcd *xhci,
  676. union xhci_trb *event)
  677. {
  678. u32 port_id;
  679. /* Port status change events always have a successful completion code */
  680. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  681. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  682. xhci->error_bitmask |= 1 << 8;
  683. }
  684. /* FIXME: core doesn't care about all port link state changes yet */
  685. port_id = GET_PORT_ID(event->generic.field[0]);
  686. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  687. /* Update event ring dequeue pointer before dropping the lock */
  688. inc_deq(xhci, xhci->event_ring, true);
  689. xhci_set_hc_event_deq(xhci);
  690. spin_unlock(&xhci->lock);
  691. /* Pass this up to the core */
  692. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  693. spin_lock(&xhci->lock);
  694. }
  695. /*
  696. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  697. * at end_trb, which may be in another segment. If the suspect DMA address is a
  698. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  699. * returns 0.
  700. */
  701. static struct xhci_segment *trb_in_td(
  702. struct xhci_segment *start_seg,
  703. union xhci_trb *start_trb,
  704. union xhci_trb *end_trb,
  705. dma_addr_t suspect_dma)
  706. {
  707. dma_addr_t start_dma;
  708. dma_addr_t end_seg_dma;
  709. dma_addr_t end_trb_dma;
  710. struct xhci_segment *cur_seg;
  711. start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
  712. cur_seg = start_seg;
  713. do {
  714. /* We may get an event for a Link TRB in the middle of a TD */
  715. end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
  716. &start_seg->trbs[TRBS_PER_SEGMENT - 1]);
  717. /* If the end TRB isn't in this segment, this is set to 0 */
  718. end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
  719. if (end_trb_dma > 0) {
  720. /* The end TRB is in this segment, so suspect should be here */
  721. if (start_dma <= end_trb_dma) {
  722. if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
  723. return cur_seg;
  724. } else {
  725. /* Case for one segment with
  726. * a TD wrapped around to the top
  727. */
  728. if ((suspect_dma >= start_dma &&
  729. suspect_dma <= end_seg_dma) ||
  730. (suspect_dma >= cur_seg->dma &&
  731. suspect_dma <= end_trb_dma))
  732. return cur_seg;
  733. }
  734. return 0;
  735. } else {
  736. /* Might still be somewhere in this segment */
  737. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  738. return cur_seg;
  739. }
  740. cur_seg = cur_seg->next;
  741. start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  742. } while (1);
  743. }
  744. /*
  745. * If this function returns an error condition, it means it got a Transfer
  746. * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
  747. * At this point, the host controller is probably hosed and should be reset.
  748. */
  749. static int handle_tx_event(struct xhci_hcd *xhci,
  750. struct xhci_transfer_event *event)
  751. {
  752. struct xhci_virt_device *xdev;
  753. struct xhci_ring *ep_ring;
  754. int ep_index;
  755. struct xhci_td *td = 0;
  756. dma_addr_t event_dma;
  757. struct xhci_segment *event_seg;
  758. union xhci_trb *event_trb;
  759. struct urb *urb = 0;
  760. int status = -EINPROGRESS;
  761. struct xhci_ep_ctx *ep_ctx;
  762. xhci_dbg(xhci, "In %s\n", __func__);
  763. xdev = xhci->devs[TRB_TO_SLOT_ID(event->flags)];
  764. if (!xdev) {
  765. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  766. return -ENODEV;
  767. }
  768. /* Endpoint ID is 1 based, our index is zero based */
  769. ep_index = TRB_TO_EP_ID(event->flags) - 1;
  770. xhci_dbg(xhci, "%s - ep index = %d\n", __func__, ep_index);
  771. ep_ring = xdev->ep_rings[ep_index];
  772. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  773. if (!ep_ring || (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
  774. xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n");
  775. return -ENODEV;
  776. }
  777. event_dma = event->buffer;
  778. /* This TRB should be in the TD at the head of this ring's TD list */
  779. xhci_dbg(xhci, "%s - checking for list empty\n", __func__);
  780. if (list_empty(&ep_ring->td_list)) {
  781. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  782. TRB_TO_SLOT_ID(event->flags), ep_index);
  783. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  784. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  785. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  786. urb = NULL;
  787. goto cleanup;
  788. }
  789. xhci_dbg(xhci, "%s - getting list entry\n", __func__);
  790. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  791. /* Is this a TRB in the currently executing TD? */
  792. xhci_dbg(xhci, "%s - looking for TD\n", __func__);
  793. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  794. td->last_trb, event_dma);
  795. xhci_dbg(xhci, "%s - found event_seg = %p\n", __func__, event_seg);
  796. if (!event_seg) {
  797. /* HC is busted, give up! */
  798. xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
  799. return -ESHUTDOWN;
  800. }
  801. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
  802. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  803. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  804. xhci_dbg(xhci, "Offset 0x00 (buffer lo) = 0x%x\n",
  805. lower_32_bits(event->buffer));
  806. xhci_dbg(xhci, "Offset 0x04 (buffer hi) = 0x%x\n",
  807. upper_32_bits(event->buffer));
  808. xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
  809. (unsigned int) event->transfer_len);
  810. xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
  811. (unsigned int) event->flags);
  812. /* Look for common error cases */
  813. switch (GET_COMP_CODE(event->transfer_len)) {
  814. /* Skip codes that require special handling depending on
  815. * transfer type
  816. */
  817. case COMP_SUCCESS:
  818. case COMP_SHORT_TX:
  819. break;
  820. case COMP_STOP:
  821. xhci_dbg(xhci, "Stopped on Transfer TRB\n");
  822. break;
  823. case COMP_STOP_INVAL:
  824. xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
  825. break;
  826. case COMP_STALL:
  827. xhci_warn(xhci, "WARN: Stalled endpoint\n");
  828. ep_ring->state |= EP_HALTED;
  829. status = -EPIPE;
  830. break;
  831. case COMP_TRB_ERR:
  832. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  833. status = -EILSEQ;
  834. break;
  835. case COMP_TX_ERR:
  836. xhci_warn(xhci, "WARN: transfer error on endpoint\n");
  837. status = -EPROTO;
  838. break;
  839. case COMP_BABBLE:
  840. xhci_warn(xhci, "WARN: babble error on endpoint\n");
  841. status = -EOVERFLOW;
  842. break;
  843. case COMP_DB_ERR:
  844. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  845. status = -ENOSR;
  846. break;
  847. default:
  848. xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
  849. urb = NULL;
  850. goto cleanup;
  851. }
  852. /* Now update the urb's actual_length and give back to the core */
  853. /* Was this a control transfer? */
  854. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  855. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  856. switch (GET_COMP_CODE(event->transfer_len)) {
  857. case COMP_SUCCESS:
  858. if (event_trb == ep_ring->dequeue) {
  859. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  860. status = -ESHUTDOWN;
  861. } else if (event_trb != td->last_trb) {
  862. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  863. status = -ESHUTDOWN;
  864. } else {
  865. xhci_dbg(xhci, "Successful control transfer!\n");
  866. status = 0;
  867. }
  868. break;
  869. case COMP_SHORT_TX:
  870. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  871. status = -EREMOTEIO;
  872. break;
  873. default:
  874. /* Others already handled above */
  875. break;
  876. }
  877. /*
  878. * Did we transfer any data, despite the errors that might have
  879. * happened? I.e. did we get past the setup stage?
  880. */
  881. if (event_trb != ep_ring->dequeue) {
  882. /* The event was for the status stage */
  883. if (event_trb == td->last_trb) {
  884. if (td->urb->actual_length != 0) {
  885. /* Don't overwrite a previously set error code */
  886. if (status == -EINPROGRESS || status == 0)
  887. /* Did we already see a short data stage? */
  888. status = -EREMOTEIO;
  889. } else {
  890. td->urb->actual_length =
  891. td->urb->transfer_buffer_length;
  892. }
  893. } else {
  894. /* Maybe the event was for the data stage? */
  895. if (GET_COMP_CODE(event->transfer_len) != COMP_STOP_INVAL) {
  896. /* We didn't stop on a link TRB in the middle */
  897. td->urb->actual_length =
  898. td->urb->transfer_buffer_length -
  899. TRB_LEN(event->transfer_len);
  900. xhci_dbg(xhci, "Waiting for status stage event\n");
  901. urb = NULL;
  902. goto cleanup;
  903. }
  904. }
  905. }
  906. } else {
  907. switch (GET_COMP_CODE(event->transfer_len)) {
  908. case COMP_SUCCESS:
  909. /* Double check that the HW transferred everything. */
  910. if (event_trb != td->last_trb) {
  911. xhci_warn(xhci, "WARN Successful completion "
  912. "on short TX\n");
  913. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  914. status = -EREMOTEIO;
  915. else
  916. status = 0;
  917. } else {
  918. xhci_dbg(xhci, "Successful bulk transfer!\n");
  919. status = 0;
  920. }
  921. break;
  922. case COMP_SHORT_TX:
  923. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  924. status = -EREMOTEIO;
  925. else
  926. status = 0;
  927. break;
  928. default:
  929. /* Others already handled above */
  930. break;
  931. }
  932. dev_dbg(&td->urb->dev->dev,
  933. "ep %#x - asked for %d bytes, "
  934. "%d bytes untransferred\n",
  935. td->urb->ep->desc.bEndpointAddress,
  936. td->urb->transfer_buffer_length,
  937. TRB_LEN(event->transfer_len));
  938. /* Fast path - was this the last TRB in the TD for this URB? */
  939. if (event_trb == td->last_trb) {
  940. if (TRB_LEN(event->transfer_len) != 0) {
  941. td->urb->actual_length =
  942. td->urb->transfer_buffer_length -
  943. TRB_LEN(event->transfer_len);
  944. if (td->urb->actual_length < 0) {
  945. xhci_warn(xhci, "HC gave bad length "
  946. "of %d bytes left\n",
  947. TRB_LEN(event->transfer_len));
  948. td->urb->actual_length = 0;
  949. }
  950. /* Don't overwrite a previously set error code */
  951. if (status == -EINPROGRESS) {
  952. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  953. status = -EREMOTEIO;
  954. else
  955. status = 0;
  956. }
  957. } else {
  958. td->urb->actual_length = td->urb->transfer_buffer_length;
  959. /* Ignore a short packet completion if the
  960. * untransferred length was zero.
  961. */
  962. if (status == -EREMOTEIO)
  963. status = 0;
  964. }
  965. } else {
  966. /* Slow path - walk the list, starting from the dequeue
  967. * pointer, to get the actual length transferred.
  968. */
  969. union xhci_trb *cur_trb;
  970. struct xhci_segment *cur_seg;
  971. td->urb->actual_length = 0;
  972. for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
  973. cur_trb != event_trb;
  974. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  975. if (TRB_TYPE(cur_trb->generic.field[3]) != TRB_TR_NOOP &&
  976. TRB_TYPE(cur_trb->generic.field[3]) != TRB_LINK)
  977. td->urb->actual_length +=
  978. TRB_LEN(cur_trb->generic.field[2]);
  979. }
  980. /* If the ring didn't stop on a Link or No-op TRB, add
  981. * in the actual bytes transferred from the Normal TRB
  982. */
  983. if (GET_COMP_CODE(event->transfer_len) != COMP_STOP_INVAL)
  984. td->urb->actual_length +=
  985. TRB_LEN(cur_trb->generic.field[2]) -
  986. TRB_LEN(event->transfer_len);
  987. }
  988. }
  989. if (GET_COMP_CODE(event->transfer_len) == COMP_STOP_INVAL ||
  990. GET_COMP_CODE(event->transfer_len) == COMP_STOP) {
  991. /* The Endpoint Stop Command completion will take care of any
  992. * stopped TDs. A stopped TD may be restarted, so don't update
  993. * the ring dequeue pointer or take this TD off any lists yet.
  994. */
  995. ep_ring->stopped_td = td;
  996. ep_ring->stopped_trb = event_trb;
  997. } else {
  998. if (GET_COMP_CODE(event->transfer_len) == COMP_STALL) {
  999. /* The transfer is completed from the driver's
  1000. * perspective, but we need to issue a set dequeue
  1001. * command for this stalled endpoint to move the dequeue
  1002. * pointer past the TD. We can't do that here because
  1003. * the halt condition must be cleared first.
  1004. */
  1005. ep_ring->stopped_td = td;
  1006. ep_ring->stopped_trb = event_trb;
  1007. } else {
  1008. /* Update ring dequeue pointer */
  1009. while (ep_ring->dequeue != td->last_trb)
  1010. inc_deq(xhci, ep_ring, false);
  1011. inc_deq(xhci, ep_ring, false);
  1012. }
  1013. /* Clean up the endpoint's TD list */
  1014. urb = td->urb;
  1015. list_del(&td->td_list);
  1016. /* Was this TD slated to be cancelled but completed anyway? */
  1017. if (!list_empty(&td->cancelled_td_list)) {
  1018. list_del(&td->cancelled_td_list);
  1019. ep_ring->cancels_pending--;
  1020. }
  1021. /* Leave the TD around for the reset endpoint function to use */
  1022. if (GET_COMP_CODE(event->transfer_len) != COMP_STALL) {
  1023. kfree(td);
  1024. }
  1025. urb->hcpriv = NULL;
  1026. }
  1027. cleanup:
  1028. inc_deq(xhci, xhci->event_ring, true);
  1029. xhci_set_hc_event_deq(xhci);
  1030. /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
  1031. if (urb) {
  1032. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  1033. xhci_dbg(xhci, "Giveback URB %p, len = %d, status = %d\n",
  1034. urb, td->urb->actual_length, status);
  1035. spin_unlock(&xhci->lock);
  1036. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  1037. spin_lock(&xhci->lock);
  1038. }
  1039. return 0;
  1040. }
  1041. /*
  1042. * This function handles all OS-owned events on the event ring. It may drop
  1043. * xhci->lock between event processing (e.g. to pass up port status changes).
  1044. */
  1045. void xhci_handle_event(struct xhci_hcd *xhci)
  1046. {
  1047. union xhci_trb *event;
  1048. int update_ptrs = 1;
  1049. int ret;
  1050. xhci_dbg(xhci, "In %s\n", __func__);
  1051. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  1052. xhci->error_bitmask |= 1 << 1;
  1053. return;
  1054. }
  1055. event = xhci->event_ring->dequeue;
  1056. /* Does the HC or OS own the TRB? */
  1057. if ((event->event_cmd.flags & TRB_CYCLE) !=
  1058. xhci->event_ring->cycle_state) {
  1059. xhci->error_bitmask |= 1 << 2;
  1060. return;
  1061. }
  1062. xhci_dbg(xhci, "%s - OS owns TRB\n", __func__);
  1063. /* FIXME: Handle more event types. */
  1064. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  1065. case TRB_TYPE(TRB_COMPLETION):
  1066. xhci_dbg(xhci, "%s - calling handle_cmd_completion\n", __func__);
  1067. handle_cmd_completion(xhci, &event->event_cmd);
  1068. xhci_dbg(xhci, "%s - returned from handle_cmd_completion\n", __func__);
  1069. break;
  1070. case TRB_TYPE(TRB_PORT_STATUS):
  1071. xhci_dbg(xhci, "%s - calling handle_port_status\n", __func__);
  1072. handle_port_status(xhci, event);
  1073. xhci_dbg(xhci, "%s - returned from handle_port_status\n", __func__);
  1074. update_ptrs = 0;
  1075. break;
  1076. case TRB_TYPE(TRB_TRANSFER):
  1077. xhci_dbg(xhci, "%s - calling handle_tx_event\n", __func__);
  1078. ret = handle_tx_event(xhci, &event->trans_event);
  1079. xhci_dbg(xhci, "%s - returned from handle_tx_event\n", __func__);
  1080. if (ret < 0)
  1081. xhci->error_bitmask |= 1 << 9;
  1082. else
  1083. update_ptrs = 0;
  1084. break;
  1085. default:
  1086. xhci->error_bitmask |= 1 << 3;
  1087. }
  1088. if (update_ptrs) {
  1089. /* Update SW and HC event ring dequeue pointer */
  1090. inc_deq(xhci, xhci->event_ring, true);
  1091. xhci_set_hc_event_deq(xhci);
  1092. }
  1093. /* Are there more items on the event ring? */
  1094. xhci_handle_event(xhci);
  1095. }
  1096. /**** Endpoint Ring Operations ****/
  1097. /*
  1098. * Generic function for queueing a TRB on a ring.
  1099. * The caller must have checked to make sure there's room on the ring.
  1100. */
  1101. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  1102. bool consumer,
  1103. u32 field1, u32 field2, u32 field3, u32 field4)
  1104. {
  1105. struct xhci_generic_trb *trb;
  1106. trb = &ring->enqueue->generic;
  1107. trb->field[0] = field1;
  1108. trb->field[1] = field2;
  1109. trb->field[2] = field3;
  1110. trb->field[3] = field4;
  1111. inc_enq(xhci, ring, consumer);
  1112. }
  1113. /*
  1114. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  1115. * FIXME allocate segments if the ring is full.
  1116. */
  1117. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  1118. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  1119. {
  1120. /* Make sure the endpoint has been added to xHC schedule */
  1121. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  1122. switch (ep_state) {
  1123. case EP_STATE_DISABLED:
  1124. /*
  1125. * USB core changed config/interfaces without notifying us,
  1126. * or hardware is reporting the wrong state.
  1127. */
  1128. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  1129. return -ENOENT;
  1130. case EP_STATE_ERROR:
  1131. xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
  1132. /* FIXME event handling code for error needs to clear it */
  1133. /* XXX not sure if this should be -ENOENT or not */
  1134. return -EINVAL;
  1135. case EP_STATE_HALTED:
  1136. xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
  1137. case EP_STATE_STOPPED:
  1138. case EP_STATE_RUNNING:
  1139. break;
  1140. default:
  1141. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  1142. /*
  1143. * FIXME issue Configure Endpoint command to try to get the HC
  1144. * back into a known state.
  1145. */
  1146. return -EINVAL;
  1147. }
  1148. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  1149. /* FIXME allocate more room */
  1150. xhci_err(xhci, "ERROR no room on ep ring\n");
  1151. return -ENOMEM;
  1152. }
  1153. return 0;
  1154. }
  1155. static int prepare_transfer(struct xhci_hcd *xhci,
  1156. struct xhci_virt_device *xdev,
  1157. unsigned int ep_index,
  1158. unsigned int num_trbs,
  1159. struct urb *urb,
  1160. struct xhci_td **td,
  1161. gfp_t mem_flags)
  1162. {
  1163. int ret;
  1164. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1165. ret = prepare_ring(xhci, xdev->ep_rings[ep_index],
  1166. ep_ctx->ep_info & EP_STATE_MASK,
  1167. num_trbs, mem_flags);
  1168. if (ret)
  1169. return ret;
  1170. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  1171. if (!*td)
  1172. return -ENOMEM;
  1173. INIT_LIST_HEAD(&(*td)->td_list);
  1174. INIT_LIST_HEAD(&(*td)->cancelled_td_list);
  1175. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  1176. if (unlikely(ret)) {
  1177. kfree(*td);
  1178. return ret;
  1179. }
  1180. (*td)->urb = urb;
  1181. urb->hcpriv = (void *) (*td);
  1182. /* Add this TD to the tail of the endpoint ring's TD list */
  1183. list_add_tail(&(*td)->td_list, &xdev->ep_rings[ep_index]->td_list);
  1184. (*td)->start_seg = xdev->ep_rings[ep_index]->enq_seg;
  1185. (*td)->first_trb = xdev->ep_rings[ep_index]->enqueue;
  1186. return 0;
  1187. }
  1188. static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
  1189. {
  1190. int num_sgs, num_trbs, running_total, temp, i;
  1191. struct scatterlist *sg;
  1192. sg = NULL;
  1193. num_sgs = urb->num_sgs;
  1194. temp = urb->transfer_buffer_length;
  1195. xhci_dbg(xhci, "count sg list trbs: \n");
  1196. num_trbs = 0;
  1197. for_each_sg(urb->sg->sg, sg, num_sgs, i) {
  1198. unsigned int previous_total_trbs = num_trbs;
  1199. unsigned int len = sg_dma_len(sg);
  1200. /* Scatter gather list entries may cross 64KB boundaries */
  1201. running_total = TRB_MAX_BUFF_SIZE -
  1202. (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1203. if (running_total != 0)
  1204. num_trbs++;
  1205. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1206. while (running_total < sg_dma_len(sg)) {
  1207. num_trbs++;
  1208. running_total += TRB_MAX_BUFF_SIZE;
  1209. }
  1210. xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
  1211. i, (unsigned long long)sg_dma_address(sg),
  1212. len, len, num_trbs - previous_total_trbs);
  1213. len = min_t(int, len, temp);
  1214. temp -= len;
  1215. if (temp == 0)
  1216. break;
  1217. }
  1218. xhci_dbg(xhci, "\n");
  1219. if (!in_interrupt())
  1220. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, sglist used, num_trbs = %d\n",
  1221. urb->ep->desc.bEndpointAddress,
  1222. urb->transfer_buffer_length,
  1223. num_trbs);
  1224. return num_trbs;
  1225. }
  1226. static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
  1227. {
  1228. if (num_trbs != 0)
  1229. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  1230. "TRBs, %d left\n", __func__,
  1231. urb->ep->desc.bEndpointAddress, num_trbs);
  1232. if (running_total != urb->transfer_buffer_length)
  1233. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
  1234. "queued %#x (%d), asked for %#x (%d)\n",
  1235. __func__,
  1236. urb->ep->desc.bEndpointAddress,
  1237. running_total, running_total,
  1238. urb->transfer_buffer_length,
  1239. urb->transfer_buffer_length);
  1240. }
  1241. static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
  1242. unsigned int ep_index, int start_cycle,
  1243. struct xhci_generic_trb *start_trb, struct xhci_td *td)
  1244. {
  1245. /*
  1246. * Pass all the TRBs to the hardware at once and make sure this write
  1247. * isn't reordered.
  1248. */
  1249. wmb();
  1250. start_trb->field[3] |= start_cycle;
  1251. ring_ep_doorbell(xhci, slot_id, ep_index);
  1252. }
  1253. static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1254. struct urb *urb, int slot_id, unsigned int ep_index)
  1255. {
  1256. struct xhci_ring *ep_ring;
  1257. unsigned int num_trbs;
  1258. struct xhci_td *td;
  1259. struct scatterlist *sg;
  1260. int num_sgs;
  1261. int trb_buff_len, this_sg_len, running_total;
  1262. bool first_trb;
  1263. u64 addr;
  1264. struct xhci_generic_trb *start_trb;
  1265. int start_cycle;
  1266. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1267. num_trbs = count_sg_trbs_needed(xhci, urb);
  1268. num_sgs = urb->num_sgs;
  1269. trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
  1270. ep_index, num_trbs, urb, &td, mem_flags);
  1271. if (trb_buff_len < 0)
  1272. return trb_buff_len;
  1273. /*
  1274. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1275. * until we've finished creating all the other TRBs. The ring's cycle
  1276. * state may change as we enqueue the other TRBs, so save it too.
  1277. */
  1278. start_trb = &ep_ring->enqueue->generic;
  1279. start_cycle = ep_ring->cycle_state;
  1280. running_total = 0;
  1281. /*
  1282. * How much data is in the first TRB?
  1283. *
  1284. * There are three forces at work for TRB buffer pointers and lengths:
  1285. * 1. We don't want to walk off the end of this sg-list entry buffer.
  1286. * 2. The transfer length that the driver requested may be smaller than
  1287. * the amount of memory allocated for this scatter-gather list.
  1288. * 3. TRBs buffers can't cross 64KB boundaries.
  1289. */
  1290. sg = urb->sg->sg;
  1291. addr = (u64) sg_dma_address(sg);
  1292. this_sg_len = sg_dma_len(sg);
  1293. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1294. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1295. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1296. if (trb_buff_len > urb->transfer_buffer_length)
  1297. trb_buff_len = urb->transfer_buffer_length;
  1298. xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
  1299. trb_buff_len);
  1300. first_trb = true;
  1301. /* Queue the first TRB, even if it's zero-length */
  1302. do {
  1303. u32 field = 0;
  1304. u32 length_field = 0;
  1305. /* Don't change the cycle bit of the first TRB until later */
  1306. if (first_trb)
  1307. first_trb = false;
  1308. else
  1309. field |= ep_ring->cycle_state;
  1310. /* Chain all the TRBs together; clear the chain bit in the last
  1311. * TRB to indicate it's the last TRB in the chain.
  1312. */
  1313. if (num_trbs > 1) {
  1314. field |= TRB_CHAIN;
  1315. } else {
  1316. /* FIXME - add check for ZERO_PACKET flag before this */
  1317. td->last_trb = ep_ring->enqueue;
  1318. field |= TRB_IOC;
  1319. }
  1320. xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
  1321. "64KB boundary at %#x, end dma = %#x\n",
  1322. (unsigned int) addr, trb_buff_len, trb_buff_len,
  1323. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1324. (unsigned int) addr + trb_buff_len);
  1325. if (TRB_MAX_BUFF_SIZE -
  1326. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
  1327. xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
  1328. xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
  1329. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1330. (unsigned int) addr + trb_buff_len);
  1331. }
  1332. length_field = TRB_LEN(trb_buff_len) |
  1333. TD_REMAINDER(urb->transfer_buffer_length - running_total) |
  1334. TRB_INTR_TARGET(0);
  1335. queue_trb(xhci, ep_ring, false,
  1336. lower_32_bits(addr),
  1337. upper_32_bits(addr),
  1338. length_field,
  1339. /* We always want to know if the TRB was short,
  1340. * or we won't get an event when it completes.
  1341. * (Unless we use event data TRBs, which are a
  1342. * waste of space and HC resources.)
  1343. */
  1344. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1345. --num_trbs;
  1346. running_total += trb_buff_len;
  1347. /* Calculate length for next transfer --
  1348. * Are we done queueing all the TRBs for this sg entry?
  1349. */
  1350. this_sg_len -= trb_buff_len;
  1351. if (this_sg_len == 0) {
  1352. --num_sgs;
  1353. if (num_sgs == 0)
  1354. break;
  1355. sg = sg_next(sg);
  1356. addr = (u64) sg_dma_address(sg);
  1357. this_sg_len = sg_dma_len(sg);
  1358. } else {
  1359. addr += trb_buff_len;
  1360. }
  1361. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1362. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1363. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1364. if (running_total + trb_buff_len > urb->transfer_buffer_length)
  1365. trb_buff_len =
  1366. urb->transfer_buffer_length - running_total;
  1367. } while (running_total < urb->transfer_buffer_length);
  1368. check_trb_math(urb, num_trbs, running_total);
  1369. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1370. return 0;
  1371. }
  1372. /* This is very similar to what ehci-q.c qtd_fill() does */
  1373. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1374. struct urb *urb, int slot_id, unsigned int ep_index)
  1375. {
  1376. struct xhci_ring *ep_ring;
  1377. struct xhci_td *td;
  1378. int num_trbs;
  1379. struct xhci_generic_trb *start_trb;
  1380. bool first_trb;
  1381. int start_cycle;
  1382. u32 field, length_field;
  1383. int running_total, trb_buff_len, ret;
  1384. u64 addr;
  1385. if (urb->sg)
  1386. return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
  1387. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1388. num_trbs = 0;
  1389. /* How much data is (potentially) left before the 64KB boundary? */
  1390. running_total = TRB_MAX_BUFF_SIZE -
  1391. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1392. /* If there's some data on this 64KB chunk, or we have to send a
  1393. * zero-length transfer, we need at least one TRB
  1394. */
  1395. if (running_total != 0 || urb->transfer_buffer_length == 0)
  1396. num_trbs++;
  1397. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1398. while (running_total < urb->transfer_buffer_length) {
  1399. num_trbs++;
  1400. running_total += TRB_MAX_BUFF_SIZE;
  1401. }
  1402. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  1403. if (!in_interrupt())
  1404. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d), addr = %#llx, num_trbs = %d\n",
  1405. urb->ep->desc.bEndpointAddress,
  1406. urb->transfer_buffer_length,
  1407. urb->transfer_buffer_length,
  1408. (unsigned long long)urb->transfer_dma,
  1409. num_trbs);
  1410. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
  1411. num_trbs, urb, &td, mem_flags);
  1412. if (ret < 0)
  1413. return ret;
  1414. /*
  1415. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1416. * until we've finished creating all the other TRBs. The ring's cycle
  1417. * state may change as we enqueue the other TRBs, so save it too.
  1418. */
  1419. start_trb = &ep_ring->enqueue->generic;
  1420. start_cycle = ep_ring->cycle_state;
  1421. running_total = 0;
  1422. /* How much data is in the first TRB? */
  1423. addr = (u64) urb->transfer_dma;
  1424. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1425. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1426. if (urb->transfer_buffer_length < trb_buff_len)
  1427. trb_buff_len = urb->transfer_buffer_length;
  1428. first_trb = true;
  1429. /* Queue the first TRB, even if it's zero-length */
  1430. do {
  1431. field = 0;
  1432. /* Don't change the cycle bit of the first TRB until later */
  1433. if (first_trb)
  1434. first_trb = false;
  1435. else
  1436. field |= ep_ring->cycle_state;
  1437. /* Chain all the TRBs together; clear the chain bit in the last
  1438. * TRB to indicate it's the last TRB in the chain.
  1439. */
  1440. if (num_trbs > 1) {
  1441. field |= TRB_CHAIN;
  1442. } else {
  1443. /* FIXME - add check for ZERO_PACKET flag before this */
  1444. td->last_trb = ep_ring->enqueue;
  1445. field |= TRB_IOC;
  1446. }
  1447. length_field = TRB_LEN(trb_buff_len) |
  1448. TD_REMAINDER(urb->transfer_buffer_length - running_total) |
  1449. TRB_INTR_TARGET(0);
  1450. queue_trb(xhci, ep_ring, false,
  1451. lower_32_bits(addr),
  1452. upper_32_bits(addr),
  1453. length_field,
  1454. /* We always want to know if the TRB was short,
  1455. * or we won't get an event when it completes.
  1456. * (Unless we use event data TRBs, which are a
  1457. * waste of space and HC resources.)
  1458. */
  1459. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1460. --num_trbs;
  1461. running_total += trb_buff_len;
  1462. /* Calculate length for next transfer */
  1463. addr += trb_buff_len;
  1464. trb_buff_len = urb->transfer_buffer_length - running_total;
  1465. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  1466. trb_buff_len = TRB_MAX_BUFF_SIZE;
  1467. } while (running_total < urb->transfer_buffer_length);
  1468. check_trb_math(urb, num_trbs, running_total);
  1469. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1470. return 0;
  1471. }
  1472. /* Caller must have locked xhci->lock */
  1473. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1474. struct urb *urb, int slot_id, unsigned int ep_index)
  1475. {
  1476. struct xhci_ring *ep_ring;
  1477. int num_trbs;
  1478. int ret;
  1479. struct usb_ctrlrequest *setup;
  1480. struct xhci_generic_trb *start_trb;
  1481. int start_cycle;
  1482. u32 field, length_field;
  1483. struct xhci_td *td;
  1484. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1485. /*
  1486. * Need to copy setup packet into setup TRB, so we can't use the setup
  1487. * DMA address.
  1488. */
  1489. if (!urb->setup_packet)
  1490. return -EINVAL;
  1491. if (!in_interrupt())
  1492. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  1493. slot_id, ep_index);
  1494. /* 1 TRB for setup, 1 for status */
  1495. num_trbs = 2;
  1496. /*
  1497. * Don't need to check if we need additional event data and normal TRBs,
  1498. * since data in control transfers will never get bigger than 16MB
  1499. * XXX: can we get a buffer that crosses 64KB boundaries?
  1500. */
  1501. if (urb->transfer_buffer_length > 0)
  1502. num_trbs++;
  1503. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
  1504. urb, &td, mem_flags);
  1505. if (ret < 0)
  1506. return ret;
  1507. /*
  1508. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1509. * until we've finished creating all the other TRBs. The ring's cycle
  1510. * state may change as we enqueue the other TRBs, so save it too.
  1511. */
  1512. start_trb = &ep_ring->enqueue->generic;
  1513. start_cycle = ep_ring->cycle_state;
  1514. /* Queue setup TRB - see section 6.4.1.2.1 */
  1515. /* FIXME better way to translate setup_packet into two u32 fields? */
  1516. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  1517. queue_trb(xhci, ep_ring, false,
  1518. /* FIXME endianness is probably going to bite my ass here. */
  1519. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  1520. setup->wIndex | setup->wLength << 16,
  1521. TRB_LEN(8) | TRB_INTR_TARGET(0),
  1522. /* Immediate data in pointer */
  1523. TRB_IDT | TRB_TYPE(TRB_SETUP));
  1524. /* If there's data, queue data TRBs */
  1525. field = 0;
  1526. length_field = TRB_LEN(urb->transfer_buffer_length) |
  1527. TD_REMAINDER(urb->transfer_buffer_length) |
  1528. TRB_INTR_TARGET(0);
  1529. if (urb->transfer_buffer_length > 0) {
  1530. if (setup->bRequestType & USB_DIR_IN)
  1531. field |= TRB_DIR_IN;
  1532. queue_trb(xhci, ep_ring, false,
  1533. lower_32_bits(urb->transfer_dma),
  1534. upper_32_bits(urb->transfer_dma),
  1535. length_field,
  1536. /* Event on short tx */
  1537. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  1538. }
  1539. /* Save the DMA address of the last TRB in the TD */
  1540. td->last_trb = ep_ring->enqueue;
  1541. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  1542. /* If the device sent data, the status stage is an OUT transfer */
  1543. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  1544. field = 0;
  1545. else
  1546. field = TRB_DIR_IN;
  1547. queue_trb(xhci, ep_ring, false,
  1548. 0,
  1549. 0,
  1550. TRB_INTR_TARGET(0),
  1551. /* Event on completion */
  1552. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  1553. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1554. return 0;
  1555. }
  1556. /**** Command Ring Operations ****/
  1557. /* Generic function for queueing a command TRB on the command ring */
  1558. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4)
  1559. {
  1560. if (!room_on_ring(xhci, xhci->cmd_ring, 1)) {
  1561. if (!in_interrupt())
  1562. xhci_err(xhci, "ERR: No room for command on command ring\n");
  1563. return -ENOMEM;
  1564. }
  1565. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  1566. field4 | xhci->cmd_ring->cycle_state);
  1567. return 0;
  1568. }
  1569. /* Queue a no-op command on the command ring */
  1570. static int queue_cmd_noop(struct xhci_hcd *xhci)
  1571. {
  1572. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP));
  1573. }
  1574. /*
  1575. * Place a no-op command on the command ring to test the command and
  1576. * event ring.
  1577. */
  1578. void *xhci_setup_one_noop(struct xhci_hcd *xhci)
  1579. {
  1580. if (queue_cmd_noop(xhci) < 0)
  1581. return NULL;
  1582. xhci->noops_submitted++;
  1583. return xhci_ring_cmd_db;
  1584. }
  1585. /* Queue a slot enable or disable request on the command ring */
  1586. int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  1587. {
  1588. return queue_command(xhci, 0, 0, 0,
  1589. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id));
  1590. }
  1591. /* Queue an address device command TRB */
  1592. int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1593. u32 slot_id)
  1594. {
  1595. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1596. upper_32_bits(in_ctx_ptr), 0,
  1597. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id));
  1598. }
  1599. /* Queue a configure endpoint command TRB */
  1600. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1601. u32 slot_id)
  1602. {
  1603. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1604. upper_32_bits(in_ctx_ptr), 0,
  1605. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id));
  1606. }
  1607. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
  1608. unsigned int ep_index)
  1609. {
  1610. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1611. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1612. u32 type = TRB_TYPE(TRB_STOP_RING);
  1613. return queue_command(xhci, 0, 0, 0,
  1614. trb_slot_id | trb_ep_index | type);
  1615. }
  1616. /* Set Transfer Ring Dequeue Pointer command.
  1617. * This should not be used for endpoints that have streams enabled.
  1618. */
  1619. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  1620. unsigned int ep_index, struct xhci_segment *deq_seg,
  1621. union xhci_trb *deq_ptr, u32 cycle_state)
  1622. {
  1623. dma_addr_t addr;
  1624. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1625. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1626. u32 type = TRB_TYPE(TRB_SET_DEQ);
  1627. addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
  1628. if (addr == 0) {
  1629. xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
  1630. xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
  1631. deq_seg, deq_ptr);
  1632. return 0;
  1633. }
  1634. return queue_command(xhci, lower_32_bits(addr) | cycle_state,
  1635. upper_32_bits(addr), 0,
  1636. trb_slot_id | trb_ep_index | type);
  1637. }
  1638. int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
  1639. unsigned int ep_index)
  1640. {
  1641. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1642. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1643. u32 type = TRB_TYPE(TRB_RESET_EP);
  1644. return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type);
  1645. }