xhci-ring.c 61 KB

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