xhci-ring.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172
  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_virt_ep *ep;
  281. unsigned int ep_state;
  282. u32 field;
  283. __u32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
  284. ep = &xhci->devs[slot_id]->eps[ep_index];
  285. ep_state = ep->ep_state;
  286. /* Don't ring the doorbell for this endpoint if there are pending
  287. * cancellations because the we don't want to interrupt processing.
  288. */
  289. if (!(ep_state & EP_HALT_PENDING) && !(ep_state & SET_DEQ_PENDING)
  290. && !(ep_state & EP_HALTED)) {
  291. field = xhci_readl(xhci, db_addr) & DB_MASK;
  292. xhci_writel(xhci, field | EPI_TO_DB(ep_index), db_addr);
  293. /* Flush PCI posted writes - FIXME Matthew Wilcox says this
  294. * isn't time-critical and we shouldn't make the CPU wait for
  295. * the flush.
  296. */
  297. xhci_readl(xhci, db_addr);
  298. }
  299. }
  300. /*
  301. * Find the segment that trb is in. Start searching in start_seg.
  302. * If we must move past a segment that has a link TRB with a toggle cycle state
  303. * bit set, then we will toggle the value pointed at by cycle_state.
  304. */
  305. static struct xhci_segment *find_trb_seg(
  306. struct xhci_segment *start_seg,
  307. union xhci_trb *trb, int *cycle_state)
  308. {
  309. struct xhci_segment *cur_seg = start_seg;
  310. struct xhci_generic_trb *generic_trb;
  311. while (cur_seg->trbs > trb ||
  312. &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
  313. generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
  314. if (TRB_TYPE(generic_trb->field[3]) == TRB_LINK &&
  315. (generic_trb->field[3] & LINK_TOGGLE))
  316. *cycle_state = ~(*cycle_state) & 0x1;
  317. cur_seg = cur_seg->next;
  318. if (cur_seg == start_seg)
  319. /* Looped over the entire list. Oops! */
  320. return 0;
  321. }
  322. return cur_seg;
  323. }
  324. /*
  325. * Move the xHC's endpoint ring dequeue pointer past cur_td.
  326. * Record the new state of the xHC's endpoint ring dequeue segment,
  327. * dequeue pointer, and new consumer cycle state in state.
  328. * Update our internal representation of the ring's dequeue pointer.
  329. *
  330. * We do this in three jumps:
  331. * - First we update our new ring state to be the same as when the xHC stopped.
  332. * - Then we traverse the ring to find the segment that contains
  333. * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
  334. * any link TRBs with the toggle cycle bit set.
  335. * - Finally we move the dequeue state one TRB further, toggling the cycle bit
  336. * if we've moved it past a link TRB with the toggle cycle bit set.
  337. */
  338. void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
  339. unsigned int slot_id, unsigned int ep_index,
  340. struct xhci_td *cur_td, struct xhci_dequeue_state *state)
  341. {
  342. struct xhci_virt_device *dev = xhci->devs[slot_id];
  343. struct xhci_ring *ep_ring = dev->eps[ep_index].ring;
  344. struct xhci_generic_trb *trb;
  345. struct xhci_ep_ctx *ep_ctx;
  346. dma_addr_t addr;
  347. state->new_cycle_state = 0;
  348. xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
  349. state->new_deq_seg = find_trb_seg(cur_td->start_seg,
  350. dev->eps[ep_index].stopped_trb,
  351. &state->new_cycle_state);
  352. if (!state->new_deq_seg)
  353. BUG();
  354. /* Dig out the cycle state saved by the xHC during the stop ep cmd */
  355. xhci_dbg(xhci, "Finding endpoint context\n");
  356. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  357. state->new_cycle_state = 0x1 & ep_ctx->deq;
  358. state->new_deq_ptr = cur_td->last_trb;
  359. xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
  360. state->new_deq_seg = find_trb_seg(state->new_deq_seg,
  361. state->new_deq_ptr,
  362. &state->new_cycle_state);
  363. if (!state->new_deq_seg)
  364. BUG();
  365. trb = &state->new_deq_ptr->generic;
  366. if (TRB_TYPE(trb->field[3]) == TRB_LINK &&
  367. (trb->field[3] & LINK_TOGGLE))
  368. state->new_cycle_state = ~(state->new_cycle_state) & 0x1;
  369. next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
  370. /* Don't update the ring cycle state for the producer (us). */
  371. xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
  372. state->new_deq_seg);
  373. addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
  374. xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
  375. (unsigned long long) addr);
  376. xhci_dbg(xhci, "Setting dequeue pointer in internal ring state.\n");
  377. ep_ring->dequeue = state->new_deq_ptr;
  378. ep_ring->deq_seg = state->new_deq_seg;
  379. }
  380. static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  381. struct xhci_td *cur_td)
  382. {
  383. struct xhci_segment *cur_seg;
  384. union xhci_trb *cur_trb;
  385. for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
  386. true;
  387. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  388. if ((cur_trb->generic.field[3] & TRB_TYPE_BITMASK) ==
  389. TRB_TYPE(TRB_LINK)) {
  390. /* Unchain any chained Link TRBs, but
  391. * leave the pointers intact.
  392. */
  393. cur_trb->generic.field[3] &= ~TRB_CHAIN;
  394. xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
  395. xhci_dbg(xhci, "Address = %p (0x%llx dma); "
  396. "in seg %p (0x%llx dma)\n",
  397. cur_trb,
  398. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  399. cur_seg,
  400. (unsigned long long)cur_seg->dma);
  401. } else {
  402. cur_trb->generic.field[0] = 0;
  403. cur_trb->generic.field[1] = 0;
  404. cur_trb->generic.field[2] = 0;
  405. /* Preserve only the cycle bit of this TRB */
  406. cur_trb->generic.field[3] &= TRB_CYCLE;
  407. cur_trb->generic.field[3] |= TRB_TYPE(TRB_TR_NOOP);
  408. xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) "
  409. "in seg %p (0x%llx dma)\n",
  410. cur_trb,
  411. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  412. cur_seg,
  413. (unsigned long long)cur_seg->dma);
  414. }
  415. if (cur_trb == cur_td->last_trb)
  416. break;
  417. }
  418. }
  419. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  420. unsigned int ep_index, struct xhci_segment *deq_seg,
  421. union xhci_trb *deq_ptr, u32 cycle_state);
  422. void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
  423. unsigned int slot_id, unsigned int ep_index,
  424. struct xhci_dequeue_state *deq_state)
  425. {
  426. struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
  427. xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
  428. "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
  429. deq_state->new_deq_seg,
  430. (unsigned long long)deq_state->new_deq_seg->dma,
  431. deq_state->new_deq_ptr,
  432. (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
  433. deq_state->new_cycle_state);
  434. queue_set_tr_deq(xhci, slot_id, ep_index,
  435. deq_state->new_deq_seg,
  436. deq_state->new_deq_ptr,
  437. (u32) deq_state->new_cycle_state);
  438. /* Stop the TD queueing code from ringing the doorbell until
  439. * this command completes. The HC won't set the dequeue pointer
  440. * if the ring is running, and ringing the doorbell starts the
  441. * ring running.
  442. */
  443. ep->ep_state |= SET_DEQ_PENDING;
  444. }
  445. static inline void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
  446. struct xhci_virt_ep *ep)
  447. {
  448. ep->ep_state &= ~EP_HALT_PENDING;
  449. /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
  450. * timer is running on another CPU, we don't decrement stop_cmds_pending
  451. * (since we didn't successfully stop the watchdog timer).
  452. */
  453. if (del_timer(&ep->stop_cmd_timer))
  454. ep->stop_cmds_pending--;
  455. }
  456. /* Must be called with xhci->lock held in interrupt context */
  457. static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
  458. struct xhci_td *cur_td, int status, char *adjective)
  459. {
  460. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  461. cur_td->urb->hcpriv = NULL;
  462. usb_hcd_unlink_urb_from_ep(hcd, cur_td->urb);
  463. xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, cur_td->urb);
  464. spin_unlock(&xhci->lock);
  465. usb_hcd_giveback_urb(hcd, cur_td->urb, status);
  466. kfree(cur_td);
  467. spin_lock(&xhci->lock);
  468. xhci_dbg(xhci, "%s URB given back\n", adjective);
  469. }
  470. /*
  471. * When we get a command completion for a Stop Endpoint Command, we need to
  472. * unlink any cancelled TDs from the ring. There are two ways to do that:
  473. *
  474. * 1. If the HW was in the middle of processing the TD that needs to be
  475. * cancelled, then we must move the ring's dequeue pointer past the last TRB
  476. * in the TD with a Set Dequeue Pointer Command.
  477. * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
  478. * bit cleared) so that the HW will skip over them.
  479. */
  480. static void handle_stopped_endpoint(struct xhci_hcd *xhci,
  481. union xhci_trb *trb)
  482. {
  483. unsigned int slot_id;
  484. unsigned int ep_index;
  485. struct xhci_ring *ep_ring;
  486. struct xhci_virt_ep *ep;
  487. struct list_head *entry;
  488. struct xhci_td *cur_td = 0;
  489. struct xhci_td *last_unlinked_td;
  490. struct xhci_dequeue_state deq_state;
  491. memset(&deq_state, 0, sizeof(deq_state));
  492. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  493. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  494. ep = &xhci->devs[slot_id]->eps[ep_index];
  495. ep_ring = ep->ring;
  496. if (list_empty(&ep->cancelled_td_list)) {
  497. xhci_stop_watchdog_timer_in_irq(xhci, ep);
  498. ring_ep_doorbell(xhci, slot_id, ep_index);
  499. return;
  500. }
  501. /* Fix up the ep ring first, so HW stops executing cancelled TDs.
  502. * We have the xHCI lock, so nothing can modify this list until we drop
  503. * it. We're also in the event handler, so we can't get re-interrupted
  504. * if another Stop Endpoint command completes
  505. */
  506. list_for_each(entry, &ep->cancelled_td_list) {
  507. cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
  508. xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
  509. cur_td->first_trb,
  510. (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
  511. /*
  512. * If we stopped on the TD we need to cancel, then we have to
  513. * move the xHC endpoint ring dequeue pointer past this TD.
  514. */
  515. if (cur_td == ep->stopped_td)
  516. xhci_find_new_dequeue_state(xhci, slot_id, ep_index, cur_td,
  517. &deq_state);
  518. else
  519. td_to_noop(xhci, ep_ring, cur_td);
  520. /*
  521. * The event handler won't see a completion for this TD anymore,
  522. * so remove it from the endpoint ring's TD list. Keep it in
  523. * the cancelled TD list for URB completion later.
  524. */
  525. list_del(&cur_td->td_list);
  526. }
  527. last_unlinked_td = cur_td;
  528. xhci_stop_watchdog_timer_in_irq(xhci, ep);
  529. /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
  530. if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
  531. xhci_queue_new_dequeue_state(xhci,
  532. slot_id, ep_index, &deq_state);
  533. xhci_ring_cmd_db(xhci);
  534. } else {
  535. /* Otherwise just ring the doorbell to restart the ring */
  536. ring_ep_doorbell(xhci, slot_id, ep_index);
  537. }
  538. /*
  539. * Drop the lock and complete the URBs in the cancelled TD list.
  540. * New TDs to be cancelled might be added to the end of the list before
  541. * we can complete all the URBs for the TDs we already unlinked.
  542. * So stop when we've completed the URB for the last TD we unlinked.
  543. */
  544. do {
  545. cur_td = list_entry(ep->cancelled_td_list.next,
  546. struct xhci_td, cancelled_td_list);
  547. list_del(&cur_td->cancelled_td_list);
  548. /* Clean up the cancelled URB */
  549. /* Doesn't matter what we pass for status, since the core will
  550. * just overwrite it (because the URB has been unlinked).
  551. */
  552. xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled");
  553. /* Stop processing the cancelled list if the watchdog timer is
  554. * running.
  555. */
  556. if (xhci->xhc_state & XHCI_STATE_DYING)
  557. return;
  558. } while (cur_td != last_unlinked_td);
  559. /* Return to the event handler with xhci->lock re-acquired */
  560. }
  561. /* Watchdog timer function for when a stop endpoint command fails to complete.
  562. * In this case, we assume the host controller is broken or dying or dead. The
  563. * host may still be completing some other events, so we have to be careful to
  564. * let the event ring handler and the URB dequeueing/enqueueing functions know
  565. * through xhci->state.
  566. *
  567. * The timer may also fire if the host takes a very long time to respond to the
  568. * command, and the stop endpoint command completion handler cannot delete the
  569. * timer before the timer function is called. Another endpoint cancellation may
  570. * sneak in before the timer function can grab the lock, and that may queue
  571. * another stop endpoint command and add the timer back. So we cannot use a
  572. * simple flag to say whether there is a pending stop endpoint command for a
  573. * particular endpoint.
  574. *
  575. * Instead we use a combination of that flag and a counter for the number of
  576. * pending stop endpoint commands. If the timer is the tail end of the last
  577. * stop endpoint command, and the endpoint's command is still pending, we assume
  578. * the host is dying.
  579. */
  580. void xhci_stop_endpoint_command_watchdog(unsigned long arg)
  581. {
  582. struct xhci_hcd *xhci;
  583. struct xhci_virt_ep *ep;
  584. struct xhci_virt_ep *temp_ep;
  585. struct xhci_ring *ring;
  586. struct xhci_td *cur_td;
  587. int ret, i, j;
  588. ep = (struct xhci_virt_ep *) arg;
  589. xhci = ep->xhci;
  590. spin_lock(&xhci->lock);
  591. ep->stop_cmds_pending--;
  592. if (xhci->xhc_state & XHCI_STATE_DYING) {
  593. xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
  594. "xHCI as DYING, exiting.\n");
  595. spin_unlock(&xhci->lock);
  596. return;
  597. }
  598. if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
  599. xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
  600. "exiting.\n");
  601. spin_unlock(&xhci->lock);
  602. return;
  603. }
  604. xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
  605. xhci_warn(xhci, "Assuming host is dying, halting host.\n");
  606. /* Oops, HC is dead or dying or at least not responding to the stop
  607. * endpoint command.
  608. */
  609. xhci->xhc_state |= XHCI_STATE_DYING;
  610. /* Disable interrupts from the host controller and start halting it */
  611. xhci_quiesce(xhci);
  612. spin_unlock(&xhci->lock);
  613. ret = xhci_halt(xhci);
  614. spin_lock(&xhci->lock);
  615. if (ret < 0) {
  616. /* This is bad; the host is not responding to commands and it's
  617. * not allowing itself to be halted. At least interrupts are
  618. * disabled, so we can set HC_STATE_HALT and notify the
  619. * USB core. But if we call usb_hc_died(), it will attempt to
  620. * disconnect all device drivers under this host. Those
  621. * disconnect() methods will wait for all URBs to be unlinked,
  622. * so we must complete them.
  623. */
  624. xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
  625. xhci_warn(xhci, "Completing active URBs anyway.\n");
  626. /* We could turn all TDs on the rings to no-ops. This won't
  627. * help if the host has cached part of the ring, and is slow if
  628. * we want to preserve the cycle bit. Skip it and hope the host
  629. * doesn't touch the memory.
  630. */
  631. }
  632. for (i = 0; i < MAX_HC_SLOTS; i++) {
  633. if (!xhci->devs[i])
  634. continue;
  635. for (j = 0; j < 31; j++) {
  636. temp_ep = &xhci->devs[i]->eps[j];
  637. ring = temp_ep->ring;
  638. if (!ring)
  639. continue;
  640. xhci_dbg(xhci, "Killing URBs for slot ID %u, "
  641. "ep index %u\n", i, j);
  642. while (!list_empty(&ring->td_list)) {
  643. cur_td = list_first_entry(&ring->td_list,
  644. struct xhci_td,
  645. td_list);
  646. list_del(&cur_td->td_list);
  647. if (!list_empty(&cur_td->cancelled_td_list))
  648. list_del(&cur_td->cancelled_td_list);
  649. xhci_giveback_urb_in_irq(xhci, cur_td,
  650. -ESHUTDOWN, "killed");
  651. }
  652. while (!list_empty(&temp_ep->cancelled_td_list)) {
  653. cur_td = list_first_entry(
  654. &temp_ep->cancelled_td_list,
  655. struct xhci_td,
  656. cancelled_td_list);
  657. list_del(&cur_td->cancelled_td_list);
  658. xhci_giveback_urb_in_irq(xhci, cur_td,
  659. -ESHUTDOWN, "killed");
  660. }
  661. }
  662. }
  663. spin_unlock(&xhci->lock);
  664. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  665. xhci_dbg(xhci, "Calling usb_hc_died()\n");
  666. usb_hc_died(xhci_to_hcd(xhci));
  667. xhci_dbg(xhci, "xHCI host controller is dead.\n");
  668. }
  669. /*
  670. * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
  671. * we need to clear the set deq pending flag in the endpoint ring state, so that
  672. * the TD queueing code can ring the doorbell again. We also need to ring the
  673. * endpoint doorbell to restart the ring, but only if there aren't more
  674. * cancellations pending.
  675. */
  676. static void handle_set_deq_completion(struct xhci_hcd *xhci,
  677. struct xhci_event_cmd *event,
  678. union xhci_trb *trb)
  679. {
  680. unsigned int slot_id;
  681. unsigned int ep_index;
  682. struct xhci_ring *ep_ring;
  683. struct xhci_virt_device *dev;
  684. struct xhci_ep_ctx *ep_ctx;
  685. struct xhci_slot_ctx *slot_ctx;
  686. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  687. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  688. dev = xhci->devs[slot_id];
  689. ep_ring = dev->eps[ep_index].ring;
  690. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  691. slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
  692. if (GET_COMP_CODE(event->status) != COMP_SUCCESS) {
  693. unsigned int ep_state;
  694. unsigned int slot_state;
  695. switch (GET_COMP_CODE(event->status)) {
  696. case COMP_TRB_ERR:
  697. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
  698. "of stream ID configuration\n");
  699. break;
  700. case COMP_CTX_STATE:
  701. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
  702. "to incorrect slot or ep state.\n");
  703. ep_state = ep_ctx->ep_info;
  704. ep_state &= EP_STATE_MASK;
  705. slot_state = slot_ctx->dev_state;
  706. slot_state = GET_SLOT_STATE(slot_state);
  707. xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
  708. slot_state, ep_state);
  709. break;
  710. case COMP_EBADSLT:
  711. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
  712. "slot %u was not enabled.\n", slot_id);
  713. break;
  714. default:
  715. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
  716. "completion code of %u.\n",
  717. GET_COMP_CODE(event->status));
  718. break;
  719. }
  720. /* OK what do we do now? The endpoint state is hosed, and we
  721. * should never get to this point if the synchronization between
  722. * queueing, and endpoint state are correct. This might happen
  723. * if the device gets disconnected after we've finished
  724. * cancelling URBs, which might not be an error...
  725. */
  726. } else {
  727. xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
  728. ep_ctx->deq);
  729. }
  730. dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
  731. ring_ep_doorbell(xhci, slot_id, ep_index);
  732. }
  733. static void handle_reset_ep_completion(struct xhci_hcd *xhci,
  734. struct xhci_event_cmd *event,
  735. union xhci_trb *trb)
  736. {
  737. int slot_id;
  738. unsigned int ep_index;
  739. struct xhci_ring *ep_ring;
  740. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  741. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  742. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  743. /* This command will only fail if the endpoint wasn't halted,
  744. * but we don't care.
  745. */
  746. xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
  747. (unsigned int) GET_COMP_CODE(event->status));
  748. /* HW with the reset endpoint quirk needs to have a configure endpoint
  749. * command complete before the endpoint can be used. Queue that here
  750. * because the HW can't handle two commands being queued in a row.
  751. */
  752. if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
  753. xhci_dbg(xhci, "Queueing configure endpoint command\n");
  754. xhci_queue_configure_endpoint(xhci,
  755. xhci->devs[slot_id]->in_ctx->dma, slot_id,
  756. false);
  757. xhci_ring_cmd_db(xhci);
  758. } else {
  759. /* Clear our internal halted state and restart the ring */
  760. xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
  761. ring_ep_doorbell(xhci, slot_id, ep_index);
  762. }
  763. }
  764. /* Check to see if a command in the device's command queue matches this one.
  765. * Signal the completion or free the command, and return 1. Return 0 if the
  766. * completed command isn't at the head of the command list.
  767. */
  768. static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
  769. struct xhci_virt_device *virt_dev,
  770. struct xhci_event_cmd *event)
  771. {
  772. struct xhci_command *command;
  773. if (list_empty(&virt_dev->cmd_list))
  774. return 0;
  775. command = list_entry(virt_dev->cmd_list.next,
  776. struct xhci_command, cmd_list);
  777. if (xhci->cmd_ring->dequeue != command->command_trb)
  778. return 0;
  779. command->status =
  780. GET_COMP_CODE(event->status);
  781. list_del(&command->cmd_list);
  782. if (command->completion)
  783. complete(command->completion);
  784. else
  785. xhci_free_command(xhci, command);
  786. return 1;
  787. }
  788. static void handle_cmd_completion(struct xhci_hcd *xhci,
  789. struct xhci_event_cmd *event)
  790. {
  791. int slot_id = TRB_TO_SLOT_ID(event->flags);
  792. u64 cmd_dma;
  793. dma_addr_t cmd_dequeue_dma;
  794. struct xhci_input_control_ctx *ctrl_ctx;
  795. struct xhci_virt_device *virt_dev;
  796. unsigned int ep_index;
  797. struct xhci_ring *ep_ring;
  798. unsigned int ep_state;
  799. cmd_dma = event->cmd_trb;
  800. cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  801. xhci->cmd_ring->dequeue);
  802. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  803. if (cmd_dequeue_dma == 0) {
  804. xhci->error_bitmask |= 1 << 4;
  805. return;
  806. }
  807. /* Does the DMA address match our internal dequeue pointer address? */
  808. if (cmd_dma != (u64) cmd_dequeue_dma) {
  809. xhci->error_bitmask |= 1 << 5;
  810. return;
  811. }
  812. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  813. case TRB_TYPE(TRB_ENABLE_SLOT):
  814. if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
  815. xhci->slot_id = slot_id;
  816. else
  817. xhci->slot_id = 0;
  818. complete(&xhci->addr_dev);
  819. break;
  820. case TRB_TYPE(TRB_DISABLE_SLOT):
  821. if (xhci->devs[slot_id])
  822. xhci_free_virt_device(xhci, slot_id);
  823. break;
  824. case TRB_TYPE(TRB_CONFIG_EP):
  825. virt_dev = xhci->devs[slot_id];
  826. if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
  827. break;
  828. /*
  829. * Configure endpoint commands can come from the USB core
  830. * configuration or alt setting changes, or because the HW
  831. * needed an extra configure endpoint command after a reset
  832. * endpoint command. In the latter case, the xHCI driver is
  833. * not waiting on the configure endpoint command.
  834. */
  835. ctrl_ctx = xhci_get_input_control_ctx(xhci,
  836. virt_dev->in_ctx);
  837. /* Input ctx add_flags are the endpoint index plus one */
  838. ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1;
  839. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  840. if (!ep_ring) {
  841. /* This must have been an initial configure endpoint */
  842. xhci->devs[slot_id]->cmd_status =
  843. GET_COMP_CODE(event->status);
  844. complete(&xhci->devs[slot_id]->cmd_completion);
  845. break;
  846. }
  847. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  848. xhci_dbg(xhci, "Completed config ep cmd - last ep index = %d, "
  849. "state = %d\n", ep_index, ep_state);
  850. if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
  851. ep_state & EP_HALTED) {
  852. /* Clear our internal halted state and restart ring */
  853. xhci->devs[slot_id]->eps[ep_index].ep_state &=
  854. ~EP_HALTED;
  855. ring_ep_doorbell(xhci, slot_id, ep_index);
  856. } else {
  857. xhci->devs[slot_id]->cmd_status =
  858. GET_COMP_CODE(event->status);
  859. complete(&xhci->devs[slot_id]->cmd_completion);
  860. }
  861. break;
  862. case TRB_TYPE(TRB_EVAL_CONTEXT):
  863. virt_dev = xhci->devs[slot_id];
  864. if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
  865. break;
  866. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  867. complete(&xhci->devs[slot_id]->cmd_completion);
  868. break;
  869. case TRB_TYPE(TRB_ADDR_DEV):
  870. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  871. complete(&xhci->addr_dev);
  872. break;
  873. case TRB_TYPE(TRB_STOP_RING):
  874. handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue);
  875. break;
  876. case TRB_TYPE(TRB_SET_DEQ):
  877. handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
  878. break;
  879. case TRB_TYPE(TRB_CMD_NOOP):
  880. ++xhci->noops_handled;
  881. break;
  882. case TRB_TYPE(TRB_RESET_EP):
  883. handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
  884. break;
  885. default:
  886. /* Skip over unknown commands on the event ring */
  887. xhci->error_bitmask |= 1 << 6;
  888. break;
  889. }
  890. inc_deq(xhci, xhci->cmd_ring, false);
  891. }
  892. static void handle_port_status(struct xhci_hcd *xhci,
  893. union xhci_trb *event)
  894. {
  895. u32 port_id;
  896. /* Port status change events always have a successful completion code */
  897. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  898. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  899. xhci->error_bitmask |= 1 << 8;
  900. }
  901. /* FIXME: core doesn't care about all port link state changes yet */
  902. port_id = GET_PORT_ID(event->generic.field[0]);
  903. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  904. /* Update event ring dequeue pointer before dropping the lock */
  905. inc_deq(xhci, xhci->event_ring, true);
  906. xhci_set_hc_event_deq(xhci);
  907. spin_unlock(&xhci->lock);
  908. /* Pass this up to the core */
  909. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  910. spin_lock(&xhci->lock);
  911. }
  912. /*
  913. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  914. * at end_trb, which may be in another segment. If the suspect DMA address is a
  915. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  916. * returns 0.
  917. */
  918. static struct xhci_segment *trb_in_td(
  919. struct xhci_segment *start_seg,
  920. union xhci_trb *start_trb,
  921. union xhci_trb *end_trb,
  922. dma_addr_t suspect_dma)
  923. {
  924. dma_addr_t start_dma;
  925. dma_addr_t end_seg_dma;
  926. dma_addr_t end_trb_dma;
  927. struct xhci_segment *cur_seg;
  928. start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
  929. cur_seg = start_seg;
  930. do {
  931. if (start_dma == 0)
  932. return 0;
  933. /* We may get an event for a Link TRB in the middle of a TD */
  934. end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
  935. &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
  936. /* If the end TRB isn't in this segment, this is set to 0 */
  937. end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
  938. if (end_trb_dma > 0) {
  939. /* The end TRB is in this segment, so suspect should be here */
  940. if (start_dma <= end_trb_dma) {
  941. if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
  942. return cur_seg;
  943. } else {
  944. /* Case for one segment with
  945. * a TD wrapped around to the top
  946. */
  947. if ((suspect_dma >= start_dma &&
  948. suspect_dma <= end_seg_dma) ||
  949. (suspect_dma >= cur_seg->dma &&
  950. suspect_dma <= end_trb_dma))
  951. return cur_seg;
  952. }
  953. return 0;
  954. } else {
  955. /* Might still be somewhere in this segment */
  956. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  957. return cur_seg;
  958. }
  959. cur_seg = cur_seg->next;
  960. start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  961. } while (cur_seg != start_seg);
  962. return 0;
  963. }
  964. /*
  965. * If this function returns an error condition, it means it got a Transfer
  966. * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
  967. * At this point, the host controller is probably hosed and should be reset.
  968. */
  969. static int handle_tx_event(struct xhci_hcd *xhci,
  970. struct xhci_transfer_event *event)
  971. {
  972. struct xhci_virt_device *xdev;
  973. struct xhci_virt_ep *ep;
  974. struct xhci_ring *ep_ring;
  975. unsigned int slot_id;
  976. int ep_index;
  977. struct xhci_td *td = 0;
  978. dma_addr_t event_dma;
  979. struct xhci_segment *event_seg;
  980. union xhci_trb *event_trb;
  981. struct urb *urb = 0;
  982. int status = -EINPROGRESS;
  983. struct xhci_ep_ctx *ep_ctx;
  984. u32 trb_comp_code;
  985. xhci_dbg(xhci, "In %s\n", __func__);
  986. slot_id = TRB_TO_SLOT_ID(event->flags);
  987. xdev = xhci->devs[slot_id];
  988. if (!xdev) {
  989. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  990. return -ENODEV;
  991. }
  992. /* Endpoint ID is 1 based, our index is zero based */
  993. ep_index = TRB_TO_EP_ID(event->flags) - 1;
  994. xhci_dbg(xhci, "%s - ep index = %d\n", __func__, ep_index);
  995. ep = &xdev->eps[ep_index];
  996. ep_ring = ep->ring;
  997. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  998. if (!ep_ring || (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
  999. xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n");
  1000. return -ENODEV;
  1001. }
  1002. event_dma = event->buffer;
  1003. /* This TRB should be in the TD at the head of this ring's TD list */
  1004. xhci_dbg(xhci, "%s - checking for list empty\n", __func__);
  1005. if (list_empty(&ep_ring->td_list)) {
  1006. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  1007. TRB_TO_SLOT_ID(event->flags), ep_index);
  1008. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  1009. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  1010. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  1011. urb = NULL;
  1012. goto cleanup;
  1013. }
  1014. xhci_dbg(xhci, "%s - getting list entry\n", __func__);
  1015. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  1016. /* Is this a TRB in the currently executing TD? */
  1017. xhci_dbg(xhci, "%s - looking for TD\n", __func__);
  1018. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  1019. td->last_trb, event_dma);
  1020. xhci_dbg(xhci, "%s - found event_seg = %p\n", __func__, event_seg);
  1021. if (!event_seg) {
  1022. /* HC is busted, give up! */
  1023. xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
  1024. return -ESHUTDOWN;
  1025. }
  1026. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
  1027. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  1028. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  1029. xhci_dbg(xhci, "Offset 0x00 (buffer lo) = 0x%x\n",
  1030. lower_32_bits(event->buffer));
  1031. xhci_dbg(xhci, "Offset 0x04 (buffer hi) = 0x%x\n",
  1032. upper_32_bits(event->buffer));
  1033. xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
  1034. (unsigned int) event->transfer_len);
  1035. xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
  1036. (unsigned int) event->flags);
  1037. /* Look for common error cases */
  1038. trb_comp_code = GET_COMP_CODE(event->transfer_len);
  1039. switch (trb_comp_code) {
  1040. /* Skip codes that require special handling depending on
  1041. * transfer type
  1042. */
  1043. case COMP_SUCCESS:
  1044. case COMP_SHORT_TX:
  1045. break;
  1046. case COMP_STOP:
  1047. xhci_dbg(xhci, "Stopped on Transfer TRB\n");
  1048. break;
  1049. case COMP_STOP_INVAL:
  1050. xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
  1051. break;
  1052. case COMP_STALL:
  1053. xhci_warn(xhci, "WARN: Stalled endpoint\n");
  1054. ep->ep_state |= EP_HALTED;
  1055. status = -EPIPE;
  1056. break;
  1057. case COMP_TRB_ERR:
  1058. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  1059. status = -EILSEQ;
  1060. break;
  1061. case COMP_TX_ERR:
  1062. xhci_warn(xhci, "WARN: transfer error on endpoint\n");
  1063. status = -EPROTO;
  1064. break;
  1065. case COMP_BABBLE:
  1066. xhci_warn(xhci, "WARN: babble error on endpoint\n");
  1067. status = -EOVERFLOW;
  1068. break;
  1069. case COMP_DB_ERR:
  1070. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  1071. status = -ENOSR;
  1072. break;
  1073. default:
  1074. xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
  1075. urb = NULL;
  1076. goto cleanup;
  1077. }
  1078. /* Now update the urb's actual_length and give back to the core */
  1079. /* Was this a control transfer? */
  1080. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  1081. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  1082. switch (trb_comp_code) {
  1083. case COMP_SUCCESS:
  1084. if (event_trb == ep_ring->dequeue) {
  1085. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  1086. status = -ESHUTDOWN;
  1087. } else if (event_trb != td->last_trb) {
  1088. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  1089. status = -ESHUTDOWN;
  1090. } else {
  1091. xhci_dbg(xhci, "Successful control transfer!\n");
  1092. status = 0;
  1093. }
  1094. break;
  1095. case COMP_SHORT_TX:
  1096. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  1097. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1098. status = -EREMOTEIO;
  1099. else
  1100. status = 0;
  1101. break;
  1102. case COMP_BABBLE:
  1103. /* The 0.96 spec says a babbling control endpoint
  1104. * is not halted. The 0.96 spec says it is. Some HW
  1105. * claims to be 0.95 compliant, but it halts the control
  1106. * endpoint anyway. Check if a babble halted the
  1107. * endpoint.
  1108. */
  1109. if (ep_ctx->ep_info != EP_STATE_HALTED)
  1110. break;
  1111. /* else fall through */
  1112. case COMP_STALL:
  1113. /* Did we transfer part of the data (middle) phase? */
  1114. if (event_trb != ep_ring->dequeue &&
  1115. event_trb != td->last_trb)
  1116. td->urb->actual_length =
  1117. td->urb->transfer_buffer_length
  1118. - TRB_LEN(event->transfer_len);
  1119. else
  1120. td->urb->actual_length = 0;
  1121. ep->stopped_td = td;
  1122. ep->stopped_trb = event_trb;
  1123. xhci_queue_reset_ep(xhci, slot_id, ep_index);
  1124. xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
  1125. xhci_ring_cmd_db(xhci);
  1126. goto td_cleanup;
  1127. default:
  1128. /* Others already handled above */
  1129. break;
  1130. }
  1131. /*
  1132. * Did we transfer any data, despite the errors that might have
  1133. * happened? I.e. did we get past the setup stage?
  1134. */
  1135. if (event_trb != ep_ring->dequeue) {
  1136. /* The event was for the status stage */
  1137. if (event_trb == td->last_trb) {
  1138. if (td->urb->actual_length != 0) {
  1139. /* Don't overwrite a previously set error code */
  1140. if ((status == -EINPROGRESS ||
  1141. status == 0) &&
  1142. (td->urb->transfer_flags
  1143. & URB_SHORT_NOT_OK))
  1144. /* Did we already see a short data stage? */
  1145. status = -EREMOTEIO;
  1146. } else {
  1147. td->urb->actual_length =
  1148. td->urb->transfer_buffer_length;
  1149. }
  1150. } else {
  1151. /* Maybe the event was for the data stage? */
  1152. if (trb_comp_code != COMP_STOP_INVAL) {
  1153. /* We didn't stop on a link TRB in the middle */
  1154. td->urb->actual_length =
  1155. td->urb->transfer_buffer_length -
  1156. TRB_LEN(event->transfer_len);
  1157. xhci_dbg(xhci, "Waiting for status stage event\n");
  1158. urb = NULL;
  1159. goto cleanup;
  1160. }
  1161. }
  1162. }
  1163. } else {
  1164. switch (trb_comp_code) {
  1165. case COMP_SUCCESS:
  1166. /* Double check that the HW transferred everything. */
  1167. if (event_trb != td->last_trb) {
  1168. xhci_warn(xhci, "WARN Successful completion "
  1169. "on short TX\n");
  1170. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1171. status = -EREMOTEIO;
  1172. else
  1173. status = 0;
  1174. } else {
  1175. if (usb_endpoint_xfer_bulk(&td->urb->ep->desc))
  1176. xhci_dbg(xhci, "Successful bulk "
  1177. "transfer!\n");
  1178. else
  1179. xhci_dbg(xhci, "Successful interrupt "
  1180. "transfer!\n");
  1181. status = 0;
  1182. }
  1183. break;
  1184. case COMP_SHORT_TX:
  1185. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1186. status = -EREMOTEIO;
  1187. else
  1188. status = 0;
  1189. break;
  1190. default:
  1191. /* Others already handled above */
  1192. break;
  1193. }
  1194. dev_dbg(&td->urb->dev->dev,
  1195. "ep %#x - asked for %d bytes, "
  1196. "%d bytes untransferred\n",
  1197. td->urb->ep->desc.bEndpointAddress,
  1198. td->urb->transfer_buffer_length,
  1199. TRB_LEN(event->transfer_len));
  1200. /* Fast path - was this the last TRB in the TD for this URB? */
  1201. if (event_trb == td->last_trb) {
  1202. if (TRB_LEN(event->transfer_len) != 0) {
  1203. td->urb->actual_length =
  1204. td->urb->transfer_buffer_length -
  1205. TRB_LEN(event->transfer_len);
  1206. if (td->urb->transfer_buffer_length <
  1207. td->urb->actual_length) {
  1208. xhci_warn(xhci, "HC gave bad length "
  1209. "of %d bytes left\n",
  1210. TRB_LEN(event->transfer_len));
  1211. td->urb->actual_length = 0;
  1212. if (td->urb->transfer_flags &
  1213. URB_SHORT_NOT_OK)
  1214. status = -EREMOTEIO;
  1215. else
  1216. status = 0;
  1217. }
  1218. /* Don't overwrite a previously set error code */
  1219. if (status == -EINPROGRESS) {
  1220. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1221. status = -EREMOTEIO;
  1222. else
  1223. status = 0;
  1224. }
  1225. } else {
  1226. td->urb->actual_length = td->urb->transfer_buffer_length;
  1227. /* Ignore a short packet completion if the
  1228. * untransferred length was zero.
  1229. */
  1230. if (status == -EREMOTEIO)
  1231. status = 0;
  1232. }
  1233. } else {
  1234. /* Slow path - walk the list, starting from the dequeue
  1235. * pointer, to get the actual length transferred.
  1236. */
  1237. union xhci_trb *cur_trb;
  1238. struct xhci_segment *cur_seg;
  1239. td->urb->actual_length = 0;
  1240. for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
  1241. cur_trb != event_trb;
  1242. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  1243. if (TRB_TYPE(cur_trb->generic.field[3]) != TRB_TR_NOOP &&
  1244. TRB_TYPE(cur_trb->generic.field[3]) != TRB_LINK)
  1245. td->urb->actual_length +=
  1246. TRB_LEN(cur_trb->generic.field[2]);
  1247. }
  1248. /* If the ring didn't stop on a Link or No-op TRB, add
  1249. * in the actual bytes transferred from the Normal TRB
  1250. */
  1251. if (trb_comp_code != COMP_STOP_INVAL)
  1252. td->urb->actual_length +=
  1253. TRB_LEN(cur_trb->generic.field[2]) -
  1254. TRB_LEN(event->transfer_len);
  1255. }
  1256. }
  1257. if (trb_comp_code == COMP_STOP_INVAL ||
  1258. trb_comp_code == COMP_STOP) {
  1259. /* The Endpoint Stop Command completion will take care of any
  1260. * stopped TDs. A stopped TD may be restarted, so don't update
  1261. * the ring dequeue pointer or take this TD off any lists yet.
  1262. */
  1263. ep->stopped_td = td;
  1264. ep->stopped_trb = event_trb;
  1265. } else {
  1266. if (trb_comp_code == COMP_STALL ||
  1267. trb_comp_code == COMP_BABBLE) {
  1268. /* The transfer is completed from the driver's
  1269. * perspective, but we need to issue a set dequeue
  1270. * command for this stalled endpoint to move the dequeue
  1271. * pointer past the TD. We can't do that here because
  1272. * the halt condition must be cleared first.
  1273. */
  1274. ep->stopped_td = td;
  1275. ep->stopped_trb = event_trb;
  1276. } else {
  1277. /* Update ring dequeue pointer */
  1278. while (ep_ring->dequeue != td->last_trb)
  1279. inc_deq(xhci, ep_ring, false);
  1280. inc_deq(xhci, ep_ring, false);
  1281. }
  1282. td_cleanup:
  1283. /* Clean up the endpoint's TD list */
  1284. urb = td->urb;
  1285. /* Do one last check of the actual transfer length.
  1286. * If the host controller said we transferred more data than
  1287. * the buffer length, urb->actual_length will be a very big
  1288. * number (since it's unsigned). Play it safe and say we didn't
  1289. * transfer anything.
  1290. */
  1291. if (urb->actual_length > urb->transfer_buffer_length) {
  1292. xhci_warn(xhci, "URB transfer length is wrong, "
  1293. "xHC issue? req. len = %u, "
  1294. "act. len = %u\n",
  1295. urb->transfer_buffer_length,
  1296. urb->actual_length);
  1297. urb->actual_length = 0;
  1298. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1299. status = -EREMOTEIO;
  1300. else
  1301. status = 0;
  1302. }
  1303. list_del(&td->td_list);
  1304. /* Was this TD slated to be cancelled but completed anyway? */
  1305. if (!list_empty(&td->cancelled_td_list))
  1306. list_del(&td->cancelled_td_list);
  1307. /* Leave the TD around for the reset endpoint function to use
  1308. * (but only if it's not a control endpoint, since we already
  1309. * queued the Set TR dequeue pointer command for stalled
  1310. * control endpoints).
  1311. */
  1312. if (usb_endpoint_xfer_control(&urb->ep->desc) ||
  1313. (trb_comp_code != COMP_STALL &&
  1314. trb_comp_code != COMP_BABBLE)) {
  1315. kfree(td);
  1316. }
  1317. urb->hcpriv = NULL;
  1318. }
  1319. cleanup:
  1320. inc_deq(xhci, xhci->event_ring, true);
  1321. xhci_set_hc_event_deq(xhci);
  1322. /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
  1323. if (urb) {
  1324. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  1325. xhci_dbg(xhci, "Giveback URB %p, len = %d, status = %d\n",
  1326. urb, urb->actual_length, status);
  1327. spin_unlock(&xhci->lock);
  1328. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  1329. spin_lock(&xhci->lock);
  1330. }
  1331. return 0;
  1332. }
  1333. /*
  1334. * This function handles all OS-owned events on the event ring. It may drop
  1335. * xhci->lock between event processing (e.g. to pass up port status changes).
  1336. */
  1337. void xhci_handle_event(struct xhci_hcd *xhci)
  1338. {
  1339. union xhci_trb *event;
  1340. int update_ptrs = 1;
  1341. int ret;
  1342. xhci_dbg(xhci, "In %s\n", __func__);
  1343. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  1344. xhci->error_bitmask |= 1 << 1;
  1345. return;
  1346. }
  1347. event = xhci->event_ring->dequeue;
  1348. /* Does the HC or OS own the TRB? */
  1349. if ((event->event_cmd.flags & TRB_CYCLE) !=
  1350. xhci->event_ring->cycle_state) {
  1351. xhci->error_bitmask |= 1 << 2;
  1352. return;
  1353. }
  1354. xhci_dbg(xhci, "%s - OS owns TRB\n", __func__);
  1355. /* FIXME: Handle more event types. */
  1356. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  1357. case TRB_TYPE(TRB_COMPLETION):
  1358. xhci_dbg(xhci, "%s - calling handle_cmd_completion\n", __func__);
  1359. handle_cmd_completion(xhci, &event->event_cmd);
  1360. xhci_dbg(xhci, "%s - returned from handle_cmd_completion\n", __func__);
  1361. break;
  1362. case TRB_TYPE(TRB_PORT_STATUS):
  1363. xhci_dbg(xhci, "%s - calling handle_port_status\n", __func__);
  1364. handle_port_status(xhci, event);
  1365. xhci_dbg(xhci, "%s - returned from handle_port_status\n", __func__);
  1366. update_ptrs = 0;
  1367. break;
  1368. case TRB_TYPE(TRB_TRANSFER):
  1369. xhci_dbg(xhci, "%s - calling handle_tx_event\n", __func__);
  1370. ret = handle_tx_event(xhci, &event->trans_event);
  1371. xhci_dbg(xhci, "%s - returned from handle_tx_event\n", __func__);
  1372. if (ret < 0)
  1373. xhci->error_bitmask |= 1 << 9;
  1374. else
  1375. update_ptrs = 0;
  1376. break;
  1377. default:
  1378. xhci->error_bitmask |= 1 << 3;
  1379. }
  1380. /* Any of the above functions may drop and re-acquire the lock, so check
  1381. * to make sure a watchdog timer didn't mark the host as non-responsive.
  1382. */
  1383. if (xhci->xhc_state & XHCI_STATE_DYING) {
  1384. xhci_dbg(xhci, "xHCI host dying, returning from "
  1385. "event handler.\n");
  1386. return;
  1387. }
  1388. if (update_ptrs) {
  1389. /* Update SW and HC event ring dequeue pointer */
  1390. inc_deq(xhci, xhci->event_ring, true);
  1391. xhci_set_hc_event_deq(xhci);
  1392. }
  1393. /* Are there more items on the event ring? */
  1394. xhci_handle_event(xhci);
  1395. }
  1396. /**** Endpoint Ring Operations ****/
  1397. /*
  1398. * Generic function for queueing a TRB on a ring.
  1399. * The caller must have checked to make sure there's room on the ring.
  1400. */
  1401. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  1402. bool consumer,
  1403. u32 field1, u32 field2, u32 field3, u32 field4)
  1404. {
  1405. struct xhci_generic_trb *trb;
  1406. trb = &ring->enqueue->generic;
  1407. trb->field[0] = field1;
  1408. trb->field[1] = field2;
  1409. trb->field[2] = field3;
  1410. trb->field[3] = field4;
  1411. inc_enq(xhci, ring, consumer);
  1412. }
  1413. /*
  1414. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  1415. * FIXME allocate segments if the ring is full.
  1416. */
  1417. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  1418. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  1419. {
  1420. /* Make sure the endpoint has been added to xHC schedule */
  1421. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  1422. switch (ep_state) {
  1423. case EP_STATE_DISABLED:
  1424. /*
  1425. * USB core changed config/interfaces without notifying us,
  1426. * or hardware is reporting the wrong state.
  1427. */
  1428. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  1429. return -ENOENT;
  1430. case EP_STATE_ERROR:
  1431. xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
  1432. /* FIXME event handling code for error needs to clear it */
  1433. /* XXX not sure if this should be -ENOENT or not */
  1434. return -EINVAL;
  1435. case EP_STATE_HALTED:
  1436. xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
  1437. case EP_STATE_STOPPED:
  1438. case EP_STATE_RUNNING:
  1439. break;
  1440. default:
  1441. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  1442. /*
  1443. * FIXME issue Configure Endpoint command to try to get the HC
  1444. * back into a known state.
  1445. */
  1446. return -EINVAL;
  1447. }
  1448. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  1449. /* FIXME allocate more room */
  1450. xhci_err(xhci, "ERROR no room on ep ring\n");
  1451. return -ENOMEM;
  1452. }
  1453. return 0;
  1454. }
  1455. static int prepare_transfer(struct xhci_hcd *xhci,
  1456. struct xhci_virt_device *xdev,
  1457. unsigned int ep_index,
  1458. unsigned int num_trbs,
  1459. struct urb *urb,
  1460. struct xhci_td **td,
  1461. gfp_t mem_flags)
  1462. {
  1463. int ret;
  1464. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1465. ret = prepare_ring(xhci, xdev->eps[ep_index].ring,
  1466. ep_ctx->ep_info & EP_STATE_MASK,
  1467. num_trbs, mem_flags);
  1468. if (ret)
  1469. return ret;
  1470. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  1471. if (!*td)
  1472. return -ENOMEM;
  1473. INIT_LIST_HEAD(&(*td)->td_list);
  1474. INIT_LIST_HEAD(&(*td)->cancelled_td_list);
  1475. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  1476. if (unlikely(ret)) {
  1477. kfree(*td);
  1478. return ret;
  1479. }
  1480. (*td)->urb = urb;
  1481. urb->hcpriv = (void *) (*td);
  1482. /* Add this TD to the tail of the endpoint ring's TD list */
  1483. list_add_tail(&(*td)->td_list, &xdev->eps[ep_index].ring->td_list);
  1484. (*td)->start_seg = xdev->eps[ep_index].ring->enq_seg;
  1485. (*td)->first_trb = xdev->eps[ep_index].ring->enqueue;
  1486. return 0;
  1487. }
  1488. static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
  1489. {
  1490. int num_sgs, num_trbs, running_total, temp, i;
  1491. struct scatterlist *sg;
  1492. sg = NULL;
  1493. num_sgs = urb->num_sgs;
  1494. temp = urb->transfer_buffer_length;
  1495. xhci_dbg(xhci, "count sg list trbs: \n");
  1496. num_trbs = 0;
  1497. for_each_sg(urb->sg->sg, sg, num_sgs, i) {
  1498. unsigned int previous_total_trbs = num_trbs;
  1499. unsigned int len = sg_dma_len(sg);
  1500. /* Scatter gather list entries may cross 64KB boundaries */
  1501. running_total = TRB_MAX_BUFF_SIZE -
  1502. (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1503. if (running_total != 0)
  1504. num_trbs++;
  1505. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1506. while (running_total < sg_dma_len(sg)) {
  1507. num_trbs++;
  1508. running_total += TRB_MAX_BUFF_SIZE;
  1509. }
  1510. xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
  1511. i, (unsigned long long)sg_dma_address(sg),
  1512. len, len, num_trbs - previous_total_trbs);
  1513. len = min_t(int, len, temp);
  1514. temp -= len;
  1515. if (temp == 0)
  1516. break;
  1517. }
  1518. xhci_dbg(xhci, "\n");
  1519. if (!in_interrupt())
  1520. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, sglist used, num_trbs = %d\n",
  1521. urb->ep->desc.bEndpointAddress,
  1522. urb->transfer_buffer_length,
  1523. num_trbs);
  1524. return num_trbs;
  1525. }
  1526. static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
  1527. {
  1528. if (num_trbs != 0)
  1529. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  1530. "TRBs, %d left\n", __func__,
  1531. urb->ep->desc.bEndpointAddress, num_trbs);
  1532. if (running_total != urb->transfer_buffer_length)
  1533. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
  1534. "queued %#x (%d), asked for %#x (%d)\n",
  1535. __func__,
  1536. urb->ep->desc.bEndpointAddress,
  1537. running_total, running_total,
  1538. urb->transfer_buffer_length,
  1539. urb->transfer_buffer_length);
  1540. }
  1541. static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
  1542. unsigned int ep_index, int start_cycle,
  1543. struct xhci_generic_trb *start_trb, struct xhci_td *td)
  1544. {
  1545. /*
  1546. * Pass all the TRBs to the hardware at once and make sure this write
  1547. * isn't reordered.
  1548. */
  1549. wmb();
  1550. start_trb->field[3] |= start_cycle;
  1551. ring_ep_doorbell(xhci, slot_id, ep_index);
  1552. }
  1553. /*
  1554. * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
  1555. * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
  1556. * (comprised of sg list entries) can take several service intervals to
  1557. * transmit.
  1558. */
  1559. int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1560. struct urb *urb, int slot_id, unsigned int ep_index)
  1561. {
  1562. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
  1563. xhci->devs[slot_id]->out_ctx, ep_index);
  1564. int xhci_interval;
  1565. int ep_interval;
  1566. xhci_interval = EP_INTERVAL_TO_UFRAMES(ep_ctx->ep_info);
  1567. ep_interval = urb->interval;
  1568. /* Convert to microframes */
  1569. if (urb->dev->speed == USB_SPEED_LOW ||
  1570. urb->dev->speed == USB_SPEED_FULL)
  1571. ep_interval *= 8;
  1572. /* FIXME change this to a warning and a suggestion to use the new API
  1573. * to set the polling interval (once the API is added).
  1574. */
  1575. if (xhci_interval != ep_interval) {
  1576. if (!printk_ratelimit())
  1577. dev_dbg(&urb->dev->dev, "Driver uses different interval"
  1578. " (%d microframe%s) than xHCI "
  1579. "(%d microframe%s)\n",
  1580. ep_interval,
  1581. ep_interval == 1 ? "" : "s",
  1582. xhci_interval,
  1583. xhci_interval == 1 ? "" : "s");
  1584. urb->interval = xhci_interval;
  1585. /* Convert back to frames for LS/FS devices */
  1586. if (urb->dev->speed == USB_SPEED_LOW ||
  1587. urb->dev->speed == USB_SPEED_FULL)
  1588. urb->interval /= 8;
  1589. }
  1590. return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
  1591. }
  1592. static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1593. struct urb *urb, int slot_id, unsigned int ep_index)
  1594. {
  1595. struct xhci_ring *ep_ring;
  1596. unsigned int num_trbs;
  1597. struct xhci_td *td;
  1598. struct scatterlist *sg;
  1599. int num_sgs;
  1600. int trb_buff_len, this_sg_len, running_total;
  1601. bool first_trb;
  1602. u64 addr;
  1603. struct xhci_generic_trb *start_trb;
  1604. int start_cycle;
  1605. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  1606. num_trbs = count_sg_trbs_needed(xhci, urb);
  1607. num_sgs = urb->num_sgs;
  1608. trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
  1609. ep_index, num_trbs, urb, &td, mem_flags);
  1610. if (trb_buff_len < 0)
  1611. return trb_buff_len;
  1612. /*
  1613. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1614. * until we've finished creating all the other TRBs. The ring's cycle
  1615. * state may change as we enqueue the other TRBs, so save it too.
  1616. */
  1617. start_trb = &ep_ring->enqueue->generic;
  1618. start_cycle = ep_ring->cycle_state;
  1619. running_total = 0;
  1620. /*
  1621. * How much data is in the first TRB?
  1622. *
  1623. * There are three forces at work for TRB buffer pointers and lengths:
  1624. * 1. We don't want to walk off the end of this sg-list entry buffer.
  1625. * 2. The transfer length that the driver requested may be smaller than
  1626. * the amount of memory allocated for this scatter-gather list.
  1627. * 3. TRBs buffers can't cross 64KB boundaries.
  1628. */
  1629. sg = urb->sg->sg;
  1630. addr = (u64) sg_dma_address(sg);
  1631. this_sg_len = sg_dma_len(sg);
  1632. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1633. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1634. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1635. if (trb_buff_len > urb->transfer_buffer_length)
  1636. trb_buff_len = urb->transfer_buffer_length;
  1637. xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
  1638. trb_buff_len);
  1639. first_trb = true;
  1640. /* Queue the first TRB, even if it's zero-length */
  1641. do {
  1642. u32 field = 0;
  1643. u32 length_field = 0;
  1644. /* Don't change the cycle bit of the first TRB until later */
  1645. if (first_trb)
  1646. first_trb = false;
  1647. else
  1648. field |= ep_ring->cycle_state;
  1649. /* Chain all the TRBs together; clear the chain bit in the last
  1650. * TRB to indicate it's the last TRB in the chain.
  1651. */
  1652. if (num_trbs > 1) {
  1653. field |= TRB_CHAIN;
  1654. } else {
  1655. /* FIXME - add check for ZERO_PACKET flag before this */
  1656. td->last_trb = ep_ring->enqueue;
  1657. field |= TRB_IOC;
  1658. }
  1659. xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
  1660. "64KB boundary at %#x, end dma = %#x\n",
  1661. (unsigned int) addr, trb_buff_len, trb_buff_len,
  1662. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1663. (unsigned int) addr + trb_buff_len);
  1664. if (TRB_MAX_BUFF_SIZE -
  1665. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
  1666. xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
  1667. xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
  1668. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1669. (unsigned int) addr + trb_buff_len);
  1670. }
  1671. length_field = TRB_LEN(trb_buff_len) |
  1672. TD_REMAINDER(urb->transfer_buffer_length - running_total) |
  1673. TRB_INTR_TARGET(0);
  1674. queue_trb(xhci, ep_ring, false,
  1675. lower_32_bits(addr),
  1676. upper_32_bits(addr),
  1677. length_field,
  1678. /* We always want to know if the TRB was short,
  1679. * or we won't get an event when it completes.
  1680. * (Unless we use event data TRBs, which are a
  1681. * waste of space and HC resources.)
  1682. */
  1683. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1684. --num_trbs;
  1685. running_total += trb_buff_len;
  1686. /* Calculate length for next transfer --
  1687. * Are we done queueing all the TRBs for this sg entry?
  1688. */
  1689. this_sg_len -= trb_buff_len;
  1690. if (this_sg_len == 0) {
  1691. --num_sgs;
  1692. if (num_sgs == 0)
  1693. break;
  1694. sg = sg_next(sg);
  1695. addr = (u64) sg_dma_address(sg);
  1696. this_sg_len = sg_dma_len(sg);
  1697. } else {
  1698. addr += trb_buff_len;
  1699. }
  1700. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1701. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1702. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1703. if (running_total + trb_buff_len > urb->transfer_buffer_length)
  1704. trb_buff_len =
  1705. urb->transfer_buffer_length - running_total;
  1706. } while (running_total < urb->transfer_buffer_length);
  1707. check_trb_math(urb, num_trbs, running_total);
  1708. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1709. return 0;
  1710. }
  1711. /* This is very similar to what ehci-q.c qtd_fill() does */
  1712. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1713. struct urb *urb, int slot_id, unsigned int ep_index)
  1714. {
  1715. struct xhci_ring *ep_ring;
  1716. struct xhci_td *td;
  1717. int num_trbs;
  1718. struct xhci_generic_trb *start_trb;
  1719. bool first_trb;
  1720. int start_cycle;
  1721. u32 field, length_field;
  1722. int running_total, trb_buff_len, ret;
  1723. u64 addr;
  1724. if (urb->sg)
  1725. return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
  1726. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  1727. num_trbs = 0;
  1728. /* How much data is (potentially) left before the 64KB boundary? */
  1729. running_total = TRB_MAX_BUFF_SIZE -
  1730. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1731. /* If there's some data on this 64KB chunk, or we have to send a
  1732. * zero-length transfer, we need at least one TRB
  1733. */
  1734. if (running_total != 0 || urb->transfer_buffer_length == 0)
  1735. num_trbs++;
  1736. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1737. while (running_total < urb->transfer_buffer_length) {
  1738. num_trbs++;
  1739. running_total += TRB_MAX_BUFF_SIZE;
  1740. }
  1741. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  1742. if (!in_interrupt())
  1743. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d), addr = %#llx, num_trbs = %d\n",
  1744. urb->ep->desc.bEndpointAddress,
  1745. urb->transfer_buffer_length,
  1746. urb->transfer_buffer_length,
  1747. (unsigned long long)urb->transfer_dma,
  1748. num_trbs);
  1749. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
  1750. num_trbs, urb, &td, mem_flags);
  1751. if (ret < 0)
  1752. return ret;
  1753. /*
  1754. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1755. * until we've finished creating all the other TRBs. The ring's cycle
  1756. * state may change as we enqueue the other TRBs, so save it too.
  1757. */
  1758. start_trb = &ep_ring->enqueue->generic;
  1759. start_cycle = ep_ring->cycle_state;
  1760. running_total = 0;
  1761. /* How much data is in the first TRB? */
  1762. addr = (u64) urb->transfer_dma;
  1763. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1764. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1765. if (urb->transfer_buffer_length < trb_buff_len)
  1766. trb_buff_len = urb->transfer_buffer_length;
  1767. first_trb = true;
  1768. /* Queue the first TRB, even if it's zero-length */
  1769. do {
  1770. field = 0;
  1771. /* Don't change the cycle bit of the first TRB until later */
  1772. if (first_trb)
  1773. first_trb = false;
  1774. else
  1775. field |= ep_ring->cycle_state;
  1776. /* Chain all the TRBs together; clear the chain bit in the last
  1777. * TRB to indicate it's the last TRB in the chain.
  1778. */
  1779. if (num_trbs > 1) {
  1780. field |= TRB_CHAIN;
  1781. } else {
  1782. /* FIXME - add check for ZERO_PACKET flag before this */
  1783. td->last_trb = ep_ring->enqueue;
  1784. field |= TRB_IOC;
  1785. }
  1786. length_field = TRB_LEN(trb_buff_len) |
  1787. TD_REMAINDER(urb->transfer_buffer_length - running_total) |
  1788. TRB_INTR_TARGET(0);
  1789. queue_trb(xhci, ep_ring, false,
  1790. lower_32_bits(addr),
  1791. upper_32_bits(addr),
  1792. length_field,
  1793. /* We always want to know if the TRB was short,
  1794. * or we won't get an event when it completes.
  1795. * (Unless we use event data TRBs, which are a
  1796. * waste of space and HC resources.)
  1797. */
  1798. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1799. --num_trbs;
  1800. running_total += trb_buff_len;
  1801. /* Calculate length for next transfer */
  1802. addr += trb_buff_len;
  1803. trb_buff_len = urb->transfer_buffer_length - running_total;
  1804. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  1805. trb_buff_len = TRB_MAX_BUFF_SIZE;
  1806. } while (running_total < urb->transfer_buffer_length);
  1807. check_trb_math(urb, num_trbs, running_total);
  1808. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1809. return 0;
  1810. }
  1811. /* Caller must have locked xhci->lock */
  1812. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1813. struct urb *urb, int slot_id, unsigned int ep_index)
  1814. {
  1815. struct xhci_ring *ep_ring;
  1816. int num_trbs;
  1817. int ret;
  1818. struct usb_ctrlrequest *setup;
  1819. struct xhci_generic_trb *start_trb;
  1820. int start_cycle;
  1821. u32 field, length_field;
  1822. struct xhci_td *td;
  1823. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  1824. /*
  1825. * Need to copy setup packet into setup TRB, so we can't use the setup
  1826. * DMA address.
  1827. */
  1828. if (!urb->setup_packet)
  1829. return -EINVAL;
  1830. if (!in_interrupt())
  1831. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  1832. slot_id, ep_index);
  1833. /* 1 TRB for setup, 1 for status */
  1834. num_trbs = 2;
  1835. /*
  1836. * Don't need to check if we need additional event data and normal TRBs,
  1837. * since data in control transfers will never get bigger than 16MB
  1838. * XXX: can we get a buffer that crosses 64KB boundaries?
  1839. */
  1840. if (urb->transfer_buffer_length > 0)
  1841. num_trbs++;
  1842. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
  1843. urb, &td, mem_flags);
  1844. if (ret < 0)
  1845. return ret;
  1846. /*
  1847. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1848. * until we've finished creating all the other TRBs. The ring's cycle
  1849. * state may change as we enqueue the other TRBs, so save it too.
  1850. */
  1851. start_trb = &ep_ring->enqueue->generic;
  1852. start_cycle = ep_ring->cycle_state;
  1853. /* Queue setup TRB - see section 6.4.1.2.1 */
  1854. /* FIXME better way to translate setup_packet into two u32 fields? */
  1855. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  1856. queue_trb(xhci, ep_ring, false,
  1857. /* FIXME endianness is probably going to bite my ass here. */
  1858. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  1859. setup->wIndex | setup->wLength << 16,
  1860. TRB_LEN(8) | TRB_INTR_TARGET(0),
  1861. /* Immediate data in pointer */
  1862. TRB_IDT | TRB_TYPE(TRB_SETUP));
  1863. /* If there's data, queue data TRBs */
  1864. field = 0;
  1865. length_field = TRB_LEN(urb->transfer_buffer_length) |
  1866. TD_REMAINDER(urb->transfer_buffer_length) |
  1867. TRB_INTR_TARGET(0);
  1868. if (urb->transfer_buffer_length > 0) {
  1869. if (setup->bRequestType & USB_DIR_IN)
  1870. field |= TRB_DIR_IN;
  1871. queue_trb(xhci, ep_ring, false,
  1872. lower_32_bits(urb->transfer_dma),
  1873. upper_32_bits(urb->transfer_dma),
  1874. length_field,
  1875. /* Event on short tx */
  1876. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  1877. }
  1878. /* Save the DMA address of the last TRB in the TD */
  1879. td->last_trb = ep_ring->enqueue;
  1880. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  1881. /* If the device sent data, the status stage is an OUT transfer */
  1882. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  1883. field = 0;
  1884. else
  1885. field = TRB_DIR_IN;
  1886. queue_trb(xhci, ep_ring, false,
  1887. 0,
  1888. 0,
  1889. TRB_INTR_TARGET(0),
  1890. /* Event on completion */
  1891. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  1892. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1893. return 0;
  1894. }
  1895. /**** Command Ring Operations ****/
  1896. /* Generic function for queueing a command TRB on the command ring.
  1897. * Check to make sure there's room on the command ring for one command TRB.
  1898. * Also check that there's room reserved for commands that must not fail.
  1899. * If this is a command that must not fail, meaning command_must_succeed = TRUE,
  1900. * then only check for the number of reserved spots.
  1901. * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
  1902. * because the command event handler may want to resubmit a failed command.
  1903. */
  1904. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
  1905. u32 field3, u32 field4, bool command_must_succeed)
  1906. {
  1907. int reserved_trbs = xhci->cmd_ring_reserved_trbs;
  1908. if (!command_must_succeed)
  1909. reserved_trbs++;
  1910. if (!room_on_ring(xhci, xhci->cmd_ring, reserved_trbs)) {
  1911. if (!in_interrupt())
  1912. xhci_err(xhci, "ERR: No room for command on command ring\n");
  1913. if (command_must_succeed)
  1914. xhci_err(xhci, "ERR: Reserved TRB counting for "
  1915. "unfailable commands failed.\n");
  1916. return -ENOMEM;
  1917. }
  1918. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  1919. field4 | xhci->cmd_ring->cycle_state);
  1920. return 0;
  1921. }
  1922. /* Queue a no-op command on the command ring */
  1923. static int queue_cmd_noop(struct xhci_hcd *xhci)
  1924. {
  1925. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP), false);
  1926. }
  1927. /*
  1928. * Place a no-op command on the command ring to test the command and
  1929. * event ring.
  1930. */
  1931. void *xhci_setup_one_noop(struct xhci_hcd *xhci)
  1932. {
  1933. if (queue_cmd_noop(xhci) < 0)
  1934. return NULL;
  1935. xhci->noops_submitted++;
  1936. return xhci_ring_cmd_db;
  1937. }
  1938. /* Queue a slot enable or disable request on the command ring */
  1939. int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  1940. {
  1941. return queue_command(xhci, 0, 0, 0,
  1942. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
  1943. }
  1944. /* Queue an address device command TRB */
  1945. int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1946. u32 slot_id)
  1947. {
  1948. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1949. upper_32_bits(in_ctx_ptr), 0,
  1950. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id),
  1951. false);
  1952. }
  1953. /* Queue a configure endpoint command TRB */
  1954. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1955. u32 slot_id, bool command_must_succeed)
  1956. {
  1957. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1958. upper_32_bits(in_ctx_ptr), 0,
  1959. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
  1960. command_must_succeed);
  1961. }
  1962. /* Queue an evaluate context command TRB */
  1963. int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1964. u32 slot_id)
  1965. {
  1966. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  1967. upper_32_bits(in_ctx_ptr), 0,
  1968. TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
  1969. false);
  1970. }
  1971. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
  1972. unsigned int ep_index)
  1973. {
  1974. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1975. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1976. u32 type = TRB_TYPE(TRB_STOP_RING);
  1977. return queue_command(xhci, 0, 0, 0,
  1978. trb_slot_id | trb_ep_index | type, false);
  1979. }
  1980. /* Set Transfer Ring Dequeue Pointer command.
  1981. * This should not be used for endpoints that have streams enabled.
  1982. */
  1983. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  1984. unsigned int ep_index, struct xhci_segment *deq_seg,
  1985. union xhci_trb *deq_ptr, u32 cycle_state)
  1986. {
  1987. dma_addr_t addr;
  1988. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1989. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1990. u32 type = TRB_TYPE(TRB_SET_DEQ);
  1991. addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
  1992. if (addr == 0) {
  1993. xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
  1994. xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
  1995. deq_seg, deq_ptr);
  1996. return 0;
  1997. }
  1998. return queue_command(xhci, lower_32_bits(addr) | cycle_state,
  1999. upper_32_bits(addr), 0,
  2000. trb_slot_id | trb_ep_index | type, false);
  2001. }
  2002. int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
  2003. unsigned int ep_index)
  2004. {
  2005. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  2006. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  2007. u32 type = TRB_TYPE(TRB_RESET_EP);
  2008. return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type,
  2009. false);
  2010. }