xhci-ring.c 52 KB

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