xhci-ring.c 73 KB

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