xhci-ring.c 53 KB

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