xhci-ring.c 80 KB

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