xhci-ring.c 55 KB

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