xhci-ring.c 57 KB

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