xhci-ring.c 97 KB

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