xhci-ring.c 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191
  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 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 le32_to_cpu(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 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_TYPE_LINK_LE32(trb->link.control);
  110. }
  111. static int enqueue_is_link_trb(struct xhci_ring *ring)
  112. {
  113. struct xhci_link_trb *link = &ring->enqueue->link;
  114. return TRB_TYPE_LINK_LE32(link->control);
  115. }
  116. union xhci_trb *xhci_find_next_enqueue(struct xhci_ring *ring)
  117. {
  118. /* Enqueue pointer can be left pointing to the link TRB,
  119. * we must handle that
  120. */
  121. if (TRB_TYPE_LINK_LE32(ring->enqueue->link.control))
  122. return ring->enq_seg->next->trbs;
  123. return ring->enqueue;
  124. }
  125. /* Updates trb to point to the next TRB in the ring, and updates seg if the next
  126. * TRB is in a new segment. This does not skip over link TRBs, and it does not
  127. * effect the ring dequeue or enqueue pointers.
  128. */
  129. static void next_trb(struct xhci_hcd *xhci,
  130. struct xhci_ring *ring,
  131. struct xhci_segment **seg,
  132. union xhci_trb **trb)
  133. {
  134. if (last_trb(xhci, ring, *seg, *trb)) {
  135. *seg = (*seg)->next;
  136. *trb = ((*seg)->trbs);
  137. } else {
  138. (*trb)++;
  139. }
  140. }
  141. /*
  142. * See Cycle bit rules. SW is the consumer for the event ring only.
  143. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  144. */
  145. static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring)
  146. {
  147. unsigned long long addr;
  148. ring->deq_updates++;
  149. /*
  150. * If this is not event ring, and the dequeue pointer
  151. * is not on a link TRB, there is one more usable TRB
  152. */
  153. if (ring->type != TYPE_EVENT &&
  154. !last_trb(xhci, ring, ring->deq_seg, ring->dequeue))
  155. ring->num_trbs_free++;
  156. do {
  157. /*
  158. * Update the dequeue pointer further if that was a link TRB or
  159. * we're at the end of an event ring segment (which doesn't have
  160. * link TRBS)
  161. */
  162. if (last_trb(xhci, ring, ring->deq_seg, ring->dequeue)) {
  163. if (ring->type == TYPE_EVENT &&
  164. last_trb_on_last_seg(xhci, ring,
  165. ring->deq_seg, ring->dequeue)) {
  166. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  167. }
  168. ring->deq_seg = ring->deq_seg->next;
  169. ring->dequeue = ring->deq_seg->trbs;
  170. } else {
  171. ring->dequeue++;
  172. }
  173. } while (last_trb(xhci, ring, ring->deq_seg, ring->dequeue));
  174. addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
  175. }
  176. /*
  177. * See Cycle bit rules. SW is the consumer for the event ring only.
  178. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  179. *
  180. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  181. * chain bit is set), then set the chain bit in all the following link TRBs.
  182. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  183. * have their chain bit cleared (so that each Link TRB is a separate TD).
  184. *
  185. * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
  186. * set, but other sections talk about dealing with the chain bit set. This was
  187. * fixed in the 0.96 specification errata, but we have to assume that all 0.95
  188. * xHCI hardware can't handle the chain bit being cleared on a link TRB.
  189. *
  190. * @more_trbs_coming: Will you enqueue more TRBs before calling
  191. * prepare_transfer()?
  192. */
  193. static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring,
  194. bool more_trbs_coming)
  195. {
  196. u32 chain;
  197. union xhci_trb *next;
  198. unsigned long long addr;
  199. chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
  200. /* If this is not event ring, there is one less usable TRB */
  201. if (ring->type != TYPE_EVENT &&
  202. !last_trb(xhci, ring, ring->enq_seg, ring->enqueue))
  203. ring->num_trbs_free--;
  204. next = ++(ring->enqueue);
  205. ring->enq_updates++;
  206. /* Update the dequeue pointer further if that was a link TRB or we're at
  207. * the end of an event ring segment (which doesn't have link TRBS)
  208. */
  209. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  210. if (ring->type != TYPE_EVENT) {
  211. /*
  212. * If the caller doesn't plan on enqueueing more
  213. * TDs before ringing the doorbell, then we
  214. * don't want to give the link TRB to the
  215. * hardware just yet. We'll give the link TRB
  216. * back in prepare_ring() just before we enqueue
  217. * the TD at the top of the ring.
  218. */
  219. if (!chain && !more_trbs_coming)
  220. break;
  221. /* If we're not dealing with 0.95 hardware or
  222. * isoc rings on AMD 0.96 host,
  223. * carry over the chain bit of the previous TRB
  224. * (which may mean the chain bit is cleared).
  225. */
  226. if (!(ring->type == TYPE_ISOC &&
  227. (xhci->quirks & XHCI_AMD_0x96_HOST))
  228. && !xhci_link_trb_quirk(xhci)) {
  229. next->link.control &=
  230. cpu_to_le32(~TRB_CHAIN);
  231. next->link.control |=
  232. cpu_to_le32(chain);
  233. }
  234. /* Give this link TRB to the hardware */
  235. wmb();
  236. next->link.control ^= cpu_to_le32(TRB_CYCLE);
  237. /* Toggle the cycle bit after the last ring segment. */
  238. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  239. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  240. }
  241. }
  242. ring->enq_seg = ring->enq_seg->next;
  243. ring->enqueue = ring->enq_seg->trbs;
  244. next = ring->enqueue;
  245. }
  246. addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
  247. }
  248. /*
  249. * Check to see if there's room to enqueue num_trbs on the ring and make sure
  250. * enqueue pointer will not advance into dequeue segment. See rules above.
  251. */
  252. static inline int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
  253. unsigned int num_trbs)
  254. {
  255. int num_trbs_in_deq_seg;
  256. if (ring->num_trbs_free < num_trbs)
  257. return 0;
  258. if (ring->type != TYPE_COMMAND && ring->type != TYPE_EVENT) {
  259. num_trbs_in_deq_seg = ring->dequeue - ring->deq_seg->trbs;
  260. if (ring->num_trbs_free < num_trbs + num_trbs_in_deq_seg)
  261. return 0;
  262. }
  263. return 1;
  264. }
  265. /* Ring the host controller doorbell after placing a command on the ring */
  266. void xhci_ring_cmd_db(struct xhci_hcd *xhci)
  267. {
  268. if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING))
  269. return;
  270. xhci_dbg(xhci, "// Ding dong!\n");
  271. xhci_writel(xhci, DB_VALUE_HOST, &xhci->dba->doorbell[0]);
  272. /* Flush PCI posted writes */
  273. xhci_readl(xhci, &xhci->dba->doorbell[0]);
  274. }
  275. static int xhci_abort_cmd_ring(struct xhci_hcd *xhci)
  276. {
  277. u64 temp_64;
  278. int ret;
  279. xhci_dbg(xhci, "Abort command ring\n");
  280. if (!(xhci->cmd_ring_state & CMD_RING_STATE_RUNNING)) {
  281. xhci_dbg(xhci, "The command ring isn't running, "
  282. "Have the command ring been stopped?\n");
  283. return 0;
  284. }
  285. temp_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  286. if (!(temp_64 & CMD_RING_RUNNING)) {
  287. xhci_dbg(xhci, "Command ring had been stopped\n");
  288. return 0;
  289. }
  290. xhci->cmd_ring_state = CMD_RING_STATE_ABORTED;
  291. xhci_write_64(xhci, temp_64 | CMD_RING_ABORT,
  292. &xhci->op_regs->cmd_ring);
  293. /* Section 4.6.1.2 of xHCI 1.0 spec says software should
  294. * time the completion od all xHCI commands, including
  295. * the Command Abort operation. If software doesn't see
  296. * CRR negated in a timely manner (e.g. longer than 5
  297. * seconds), then it should assume that the there are
  298. * larger problems with the xHC and assert HCRST.
  299. */
  300. ret = handshake(xhci, &xhci->op_regs->cmd_ring,
  301. CMD_RING_RUNNING, 0, 5 * 1000 * 1000);
  302. if (ret < 0) {
  303. /* we are about to kill xhci, give it one more chance */
  304. xhci_write_64(xhci, temp_64 | CMD_RING_ABORT,
  305. &xhci->op_regs->cmd_ring);
  306. udelay(1000);
  307. ret = handshake(xhci, &xhci->op_regs->cmd_ring,
  308. CMD_RING_RUNNING, 0, 3 * 1000 * 1000);
  309. if (ret == 0)
  310. return 0;
  311. xhci_err(xhci, "Stopped the command ring failed, "
  312. "maybe the host is dead\n");
  313. xhci->xhc_state |= XHCI_STATE_DYING;
  314. xhci_quiesce(xhci);
  315. xhci_halt(xhci);
  316. return -ESHUTDOWN;
  317. }
  318. return 0;
  319. }
  320. static int xhci_queue_cd(struct xhci_hcd *xhci,
  321. struct xhci_command *command,
  322. union xhci_trb *cmd_trb)
  323. {
  324. struct xhci_cd *cd;
  325. cd = kzalloc(sizeof(struct xhci_cd), GFP_ATOMIC);
  326. if (!cd)
  327. return -ENOMEM;
  328. INIT_LIST_HEAD(&cd->cancel_cmd_list);
  329. cd->command = command;
  330. cd->cmd_trb = cmd_trb;
  331. list_add_tail(&cd->cancel_cmd_list, &xhci->cancel_cmd_list);
  332. return 0;
  333. }
  334. /*
  335. * Cancel the command which has issue.
  336. *
  337. * Some commands may hang due to waiting for acknowledgement from
  338. * usb device. It is outside of the xHC's ability to control and
  339. * will cause the command ring is blocked. When it occurs software
  340. * should intervene to recover the command ring.
  341. * See Section 4.6.1.1 and 4.6.1.2
  342. */
  343. int xhci_cancel_cmd(struct xhci_hcd *xhci, struct xhci_command *command,
  344. union xhci_trb *cmd_trb)
  345. {
  346. int retval = 0;
  347. unsigned long flags;
  348. spin_lock_irqsave(&xhci->lock, flags);
  349. if (xhci->xhc_state & XHCI_STATE_DYING) {
  350. xhci_warn(xhci, "Abort the command ring,"
  351. " but the xHCI is dead.\n");
  352. retval = -ESHUTDOWN;
  353. goto fail;
  354. }
  355. /* queue the cmd desriptor to cancel_cmd_list */
  356. retval = xhci_queue_cd(xhci, command, cmd_trb);
  357. if (retval) {
  358. xhci_warn(xhci, "Queuing command descriptor failed.\n");
  359. goto fail;
  360. }
  361. /* abort command ring */
  362. retval = xhci_abort_cmd_ring(xhci);
  363. if (retval) {
  364. xhci_err(xhci, "Abort command ring failed\n");
  365. if (unlikely(retval == -ESHUTDOWN)) {
  366. spin_unlock_irqrestore(&xhci->lock, flags);
  367. usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
  368. xhci_dbg(xhci, "xHCI host controller is dead.\n");
  369. return retval;
  370. }
  371. }
  372. fail:
  373. spin_unlock_irqrestore(&xhci->lock, flags);
  374. return retval;
  375. }
  376. void xhci_ring_ep_doorbell(struct xhci_hcd *xhci,
  377. unsigned int slot_id,
  378. unsigned int ep_index,
  379. unsigned int stream_id)
  380. {
  381. __le32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
  382. struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
  383. unsigned int ep_state = ep->ep_state;
  384. /* Don't ring the doorbell for this endpoint if there are pending
  385. * cancellations because we don't want to interrupt processing.
  386. * We don't want to restart any stream rings if there's a set dequeue
  387. * pointer command pending because the device can choose to start any
  388. * stream once the endpoint is on the HW schedule.
  389. * FIXME - check all the stream rings for pending cancellations.
  390. */
  391. if ((ep_state & EP_HALT_PENDING) || (ep_state & SET_DEQ_PENDING) ||
  392. (ep_state & EP_HALTED))
  393. return;
  394. xhci_writel(xhci, DB_VALUE(ep_index, stream_id), db_addr);
  395. /* The CPU has better things to do at this point than wait for a
  396. * write-posting flush. It'll get there soon enough.
  397. */
  398. }
  399. /* Ring the doorbell for any rings with pending URBs */
  400. static void ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
  401. unsigned int slot_id,
  402. unsigned int ep_index)
  403. {
  404. unsigned int stream_id;
  405. struct xhci_virt_ep *ep;
  406. ep = &xhci->devs[slot_id]->eps[ep_index];
  407. /* A ring has pending URBs if its TD list is not empty */
  408. if (!(ep->ep_state & EP_HAS_STREAMS)) {
  409. if (ep->ring && !(list_empty(&ep->ring->td_list)))
  410. xhci_ring_ep_doorbell(xhci, slot_id, ep_index, 0);
  411. return;
  412. }
  413. for (stream_id = 1; stream_id < ep->stream_info->num_streams;
  414. stream_id++) {
  415. struct xhci_stream_info *stream_info = ep->stream_info;
  416. if (!list_empty(&stream_info->stream_rings[stream_id]->td_list))
  417. xhci_ring_ep_doorbell(xhci, slot_id, ep_index,
  418. stream_id);
  419. }
  420. }
  421. /*
  422. * Find the segment that trb is in. Start searching in start_seg.
  423. * If we must move past a segment that has a link TRB with a toggle cycle state
  424. * bit set, then we will toggle the value pointed at by cycle_state.
  425. */
  426. static struct xhci_segment *find_trb_seg(
  427. struct xhci_segment *start_seg,
  428. union xhci_trb *trb, int *cycle_state)
  429. {
  430. struct xhci_segment *cur_seg = start_seg;
  431. struct xhci_generic_trb *generic_trb;
  432. while (cur_seg->trbs > trb ||
  433. &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
  434. generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
  435. if (generic_trb->field[3] & cpu_to_le32(LINK_TOGGLE))
  436. *cycle_state ^= 0x1;
  437. cur_seg = cur_seg->next;
  438. if (cur_seg == start_seg)
  439. /* Looped over the entire list. Oops! */
  440. return NULL;
  441. }
  442. return cur_seg;
  443. }
  444. static struct xhci_ring *xhci_triad_to_transfer_ring(struct xhci_hcd *xhci,
  445. unsigned int slot_id, unsigned int ep_index,
  446. unsigned int stream_id)
  447. {
  448. struct xhci_virt_ep *ep;
  449. ep = &xhci->devs[slot_id]->eps[ep_index];
  450. /* Common case: no streams */
  451. if (!(ep->ep_state & EP_HAS_STREAMS))
  452. return ep->ring;
  453. if (stream_id == 0) {
  454. xhci_warn(xhci,
  455. "WARN: Slot ID %u, ep index %u has streams, "
  456. "but URB has no stream ID.\n",
  457. slot_id, ep_index);
  458. return NULL;
  459. }
  460. if (stream_id < ep->stream_info->num_streams)
  461. return ep->stream_info->stream_rings[stream_id];
  462. xhci_warn(xhci,
  463. "WARN: Slot ID %u, ep index %u has "
  464. "stream IDs 1 to %u allocated, "
  465. "but stream ID %u is requested.\n",
  466. slot_id, ep_index,
  467. ep->stream_info->num_streams - 1,
  468. stream_id);
  469. return NULL;
  470. }
  471. /* Get the right ring for the given URB.
  472. * If the endpoint supports streams, boundary check the URB's stream ID.
  473. * If the endpoint doesn't support streams, return the singular endpoint ring.
  474. */
  475. static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
  476. struct urb *urb)
  477. {
  478. return xhci_triad_to_transfer_ring(xhci, urb->dev->slot_id,
  479. xhci_get_endpoint_index(&urb->ep->desc), urb->stream_id);
  480. }
  481. /*
  482. * Move the xHC's endpoint ring dequeue pointer past cur_td.
  483. * Record the new state of the xHC's endpoint ring dequeue segment,
  484. * dequeue pointer, and new consumer cycle state in state.
  485. * Update our internal representation of the ring's dequeue pointer.
  486. *
  487. * We do this in three jumps:
  488. * - First we update our new ring state to be the same as when the xHC stopped.
  489. * - Then we traverse the ring to find the segment that contains
  490. * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
  491. * any link TRBs with the toggle cycle bit set.
  492. * - Finally we move the dequeue state one TRB further, toggling the cycle bit
  493. * if we've moved it past a link TRB with the toggle cycle bit set.
  494. *
  495. * Some of the uses of xhci_generic_trb are grotty, but if they're done
  496. * with correct __le32 accesses they should work fine. Only users of this are
  497. * in here.
  498. */
  499. void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
  500. unsigned int slot_id, unsigned int ep_index,
  501. unsigned int stream_id, struct xhci_td *cur_td,
  502. struct xhci_dequeue_state *state)
  503. {
  504. struct xhci_virt_device *dev = xhci->devs[slot_id];
  505. struct xhci_ring *ep_ring;
  506. struct xhci_generic_trb *trb;
  507. struct xhci_ep_ctx *ep_ctx;
  508. dma_addr_t addr;
  509. ep_ring = xhci_triad_to_transfer_ring(xhci, slot_id,
  510. ep_index, stream_id);
  511. if (!ep_ring) {
  512. xhci_warn(xhci, "WARN can't find new dequeue state "
  513. "for invalid stream ID %u.\n",
  514. stream_id);
  515. return;
  516. }
  517. state->new_cycle_state = 0;
  518. xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
  519. state->new_deq_seg = find_trb_seg(cur_td->start_seg,
  520. dev->eps[ep_index].stopped_trb,
  521. &state->new_cycle_state);
  522. if (!state->new_deq_seg) {
  523. WARN_ON(1);
  524. return;
  525. }
  526. /* Dig out the cycle state saved by the xHC during the stop ep cmd */
  527. xhci_dbg(xhci, "Finding endpoint context\n");
  528. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  529. state->new_cycle_state = 0x1 & le64_to_cpu(ep_ctx->deq);
  530. state->new_deq_ptr = cur_td->last_trb;
  531. xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
  532. state->new_deq_seg = find_trb_seg(state->new_deq_seg,
  533. state->new_deq_ptr,
  534. &state->new_cycle_state);
  535. if (!state->new_deq_seg) {
  536. WARN_ON(1);
  537. return;
  538. }
  539. trb = &state->new_deq_ptr->generic;
  540. if (TRB_TYPE_LINK_LE32(trb->field[3]) &&
  541. (trb->field[3] & cpu_to_le32(LINK_TOGGLE)))
  542. state->new_cycle_state ^= 0x1;
  543. next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
  544. /*
  545. * If there is only one segment in a ring, find_trb_seg()'s while loop
  546. * will not run, and it will return before it has a chance to see if it
  547. * needs to toggle the cycle bit. It can't tell if the stalled transfer
  548. * ended just before the link TRB on a one-segment ring, or if the TD
  549. * wrapped around the top of the ring, because it doesn't have the TD in
  550. * question. Look for the one-segment case where stalled TRB's address
  551. * is greater than the new dequeue pointer address.
  552. */
  553. if (ep_ring->first_seg == ep_ring->first_seg->next &&
  554. state->new_deq_ptr < dev->eps[ep_index].stopped_trb)
  555. state->new_cycle_state ^= 0x1;
  556. xhci_dbg(xhci, "Cycle state = 0x%x\n", state->new_cycle_state);
  557. /* Don't update the ring cycle state for the producer (us). */
  558. xhci_dbg(xhci, "New dequeue segment = %pK (virtual)\n",
  559. state->new_deq_seg);
  560. addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
  561. xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
  562. (unsigned long long) addr);
  563. }
  564. /* flip_cycle means flip the cycle bit of all but the first and last TRB.
  565. * (The last TRB actually points to the ring enqueue pointer, which is not part
  566. * of this TD.) This is used to remove partially enqueued isoc TDs from a ring.
  567. */
  568. static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  569. struct xhci_td *cur_td, bool flip_cycle)
  570. {
  571. struct xhci_segment *cur_seg;
  572. union xhci_trb *cur_trb;
  573. for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
  574. true;
  575. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  576. if (TRB_TYPE_LINK_LE32(cur_trb->generic.field[3])) {
  577. /* Unchain any chained Link TRBs, but
  578. * leave the pointers intact.
  579. */
  580. cur_trb->generic.field[3] &= cpu_to_le32(~TRB_CHAIN);
  581. /* Flip the cycle bit (link TRBs can't be the first
  582. * or last TRB).
  583. */
  584. if (flip_cycle)
  585. cur_trb->generic.field[3] ^=
  586. cpu_to_le32(TRB_CYCLE);
  587. xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
  588. xhci_dbg(xhci, "Address = %pK (0x%llx dma); "
  589. "in seg %pK (0x%llx dma)\n",
  590. cur_trb,
  591. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  592. cur_seg,
  593. (unsigned long long)cur_seg->dma);
  594. } else {
  595. cur_trb->generic.field[0] = 0;
  596. cur_trb->generic.field[1] = 0;
  597. cur_trb->generic.field[2] = 0;
  598. /* Preserve only the cycle bit of this TRB */
  599. cur_trb->generic.field[3] &= cpu_to_le32(TRB_CYCLE);
  600. /* Flip the cycle bit except on the first or last TRB */
  601. if (flip_cycle && cur_trb != cur_td->first_trb &&
  602. cur_trb != cur_td->last_trb)
  603. cur_trb->generic.field[3] ^=
  604. cpu_to_le32(TRB_CYCLE);
  605. cur_trb->generic.field[3] |= cpu_to_le32(
  606. TRB_TYPE(TRB_TR_NOOP));
  607. xhci_dbg(xhci, "TRB to noop at offset 0x%llx\n",
  608. (unsigned long long)
  609. xhci_trb_virt_to_dma(cur_seg, cur_trb));
  610. }
  611. if (cur_trb == cur_td->last_trb)
  612. break;
  613. }
  614. }
  615. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  616. unsigned int ep_index, unsigned int stream_id,
  617. struct xhci_segment *deq_seg,
  618. union xhci_trb *deq_ptr, u32 cycle_state);
  619. void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
  620. unsigned int slot_id, unsigned int ep_index,
  621. unsigned int stream_id,
  622. struct xhci_dequeue_state *deq_state)
  623. {
  624. struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
  625. xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %pK (0x%llx dma), "
  626. "new deq ptr = %pK (0x%llx dma), new cycle = %u\n",
  627. deq_state->new_deq_seg,
  628. (unsigned long long)deq_state->new_deq_seg->dma,
  629. deq_state->new_deq_ptr,
  630. (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
  631. deq_state->new_cycle_state);
  632. queue_set_tr_deq(xhci, slot_id, ep_index, stream_id,
  633. deq_state->new_deq_seg,
  634. deq_state->new_deq_ptr,
  635. (u32) deq_state->new_cycle_state);
  636. /* Stop the TD queueing code from ringing the doorbell until
  637. * this command completes. The HC won't set the dequeue pointer
  638. * if the ring is running, and ringing the doorbell starts the
  639. * ring running.
  640. */
  641. ep->ep_state |= SET_DEQ_PENDING;
  642. }
  643. static void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
  644. struct xhci_virt_ep *ep)
  645. {
  646. ep->ep_state &= ~EP_HALT_PENDING;
  647. /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
  648. * timer is running on another CPU, we don't decrement stop_cmds_pending
  649. * (since we didn't successfully stop the watchdog timer).
  650. */
  651. if (del_timer(&ep->stop_cmd_timer))
  652. ep->stop_cmds_pending--;
  653. }
  654. /* Must be called with xhci->lock held in interrupt context */
  655. static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
  656. struct xhci_td *cur_td, int status, char *adjective)
  657. {
  658. struct usb_hcd *hcd;
  659. struct urb *urb;
  660. struct urb_priv *urb_priv;
  661. urb = cur_td->urb;
  662. urb_priv = urb->hcpriv;
  663. urb_priv->td_cnt++;
  664. hcd = bus_to_hcd(urb->dev->bus);
  665. /* Only giveback urb when this is the last td in urb */
  666. if (urb_priv->td_cnt == urb_priv->length) {
  667. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  668. xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
  669. if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
  670. if (xhci->quirks & XHCI_AMD_PLL_FIX)
  671. usb_amd_quirk_pll_enable();
  672. }
  673. }
  674. usb_hcd_unlink_urb_from_ep(hcd, urb);
  675. spin_unlock(&xhci->lock);
  676. usb_hcd_giveback_urb(hcd, urb, status);
  677. xhci_urb_free_priv(xhci, urb_priv);
  678. spin_lock(&xhci->lock);
  679. }
  680. }
  681. /*
  682. * When we get a command completion for a Stop Endpoint Command, we need to
  683. * unlink any cancelled TDs from the ring. There are two ways to do that:
  684. *
  685. * 1. If the HW was in the middle of processing the TD that needs to be
  686. * cancelled, then we must move the ring's dequeue pointer past the last TRB
  687. * in the TD with a Set Dequeue Pointer Command.
  688. * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
  689. * bit cleared) so that the HW will skip over them.
  690. */
  691. static void handle_stopped_endpoint(struct xhci_hcd *xhci,
  692. union xhci_trb *trb, struct xhci_event_cmd *event)
  693. {
  694. unsigned int slot_id;
  695. unsigned int ep_index;
  696. struct xhci_virt_device *virt_dev;
  697. struct xhci_ring *ep_ring;
  698. struct xhci_virt_ep *ep;
  699. struct list_head *entry;
  700. struct xhci_td *cur_td = NULL;
  701. struct xhci_td *last_unlinked_td;
  702. struct xhci_dequeue_state deq_state;
  703. if (xhci->main_hcd->driver->set_autosuspend)
  704. xhci->main_hcd->driver->set_autosuspend(xhci->main_hcd, 1);
  705. if (unlikely(TRB_TO_SUSPEND_PORT(
  706. le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])))) {
  707. slot_id = TRB_TO_SLOT_ID(
  708. le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]));
  709. virt_dev = xhci->devs[slot_id];
  710. if (virt_dev)
  711. handle_cmd_in_cmd_wait_list(xhci, virt_dev,
  712. event);
  713. else
  714. xhci_warn(xhci, "Stop endpoint command "
  715. "completion for disabled slot %u\n",
  716. slot_id);
  717. return;
  718. }
  719. memset(&deq_state, 0, sizeof(deq_state));
  720. slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
  721. ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
  722. ep = &xhci->devs[slot_id]->eps[ep_index];
  723. if (list_empty(&ep->cancelled_td_list)) {
  724. xhci_stop_watchdog_timer_in_irq(xhci, ep);
  725. ep->stopped_td = NULL;
  726. ep->stopped_trb = NULL;
  727. ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
  728. return;
  729. }
  730. /* Fix up the ep ring first, so HW stops executing cancelled TDs.
  731. * We have the xHCI lock, so nothing can modify this list until we drop
  732. * it. We're also in the event handler, so we can't get re-interrupted
  733. * if another Stop Endpoint command completes
  734. */
  735. list_for_each(entry, &ep->cancelled_td_list) {
  736. cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
  737. xhci_dbg(xhci, "Removing canceled TD starting at 0x%llx (dma).\n",
  738. (unsigned long long)xhci_trb_virt_to_dma(
  739. cur_td->start_seg, cur_td->first_trb));
  740. ep_ring = xhci_urb_to_transfer_ring(xhci, cur_td->urb);
  741. if (!ep_ring) {
  742. /* This shouldn't happen unless a driver is mucking
  743. * with the stream ID after submission. This will
  744. * leave the TD on the hardware ring, and the hardware
  745. * will try to execute it, and may access a buffer
  746. * that has already been freed. In the best case, the
  747. * hardware will execute it, and the event handler will
  748. * ignore the completion event for that TD, since it was
  749. * removed from the td_list for that endpoint. In
  750. * short, don't muck with the stream ID after
  751. * submission.
  752. */
  753. xhci_warn(xhci, "WARN Cancelled URB %pK "
  754. "has invalid stream ID %u.\n",
  755. cur_td->urb,
  756. cur_td->urb->stream_id);
  757. goto remove_finished_td;
  758. }
  759. /*
  760. * If we stopped on the TD we need to cancel, then we have to
  761. * move the xHC endpoint ring dequeue pointer past this TD.
  762. */
  763. if (cur_td == ep->stopped_td)
  764. xhci_find_new_dequeue_state(xhci, slot_id, ep_index,
  765. cur_td->urb->stream_id,
  766. cur_td, &deq_state);
  767. else
  768. td_to_noop(xhci, ep_ring, cur_td, false);
  769. remove_finished_td:
  770. /*
  771. * The event handler won't see a completion for this TD anymore,
  772. * so remove it from the endpoint ring's TD list. Keep it in
  773. * the cancelled TD list for URB completion later.
  774. */
  775. list_del_init(&cur_td->td_list);
  776. }
  777. last_unlinked_td = cur_td;
  778. xhci_stop_watchdog_timer_in_irq(xhci, ep);
  779. /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
  780. if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
  781. xhci_queue_new_dequeue_state(xhci,
  782. slot_id, ep_index,
  783. ep->stopped_td->urb->stream_id,
  784. &deq_state);
  785. xhci_ring_cmd_db(xhci);
  786. } else {
  787. /* Otherwise ring the doorbell(s) to restart queued transfers */
  788. ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
  789. }
  790. /* Clear stopped_td and stopped_trb if endpoint is not halted */
  791. if (!(ep->ep_state & EP_HALTED)) {
  792. ep->stopped_td = NULL;
  793. ep->stopped_trb = NULL;
  794. }
  795. /*
  796. * Drop the lock and complete the URBs in the cancelled TD list.
  797. * New TDs to be cancelled might be added to the end of the list before
  798. * we can complete all the URBs for the TDs we already unlinked.
  799. * So stop when we've completed the URB for the last TD we unlinked.
  800. */
  801. do {
  802. cur_td = list_entry(ep->cancelled_td_list.next,
  803. struct xhci_td, cancelled_td_list);
  804. list_del_init(&cur_td->cancelled_td_list);
  805. /* Clean up the cancelled URB */
  806. /* Doesn't matter what we pass for status, since the core will
  807. * just overwrite it (because the URB has been unlinked).
  808. */
  809. xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled");
  810. /* Stop processing the cancelled list if the watchdog timer is
  811. * running.
  812. */
  813. if (xhci->xhc_state & XHCI_STATE_DYING)
  814. return;
  815. } while (cur_td != last_unlinked_td);
  816. /* Return to the event handler with xhci->lock re-acquired */
  817. }
  818. /* Watchdog timer function for when a stop endpoint command fails to complete.
  819. * In this case, we assume the host controller is broken or dying or dead. The
  820. * host may still be completing some other events, so we have to be careful to
  821. * let the event ring handler and the URB dequeueing/enqueueing functions know
  822. * through xhci->state.
  823. *
  824. * The timer may also fire if the host takes a very long time to respond to the
  825. * command, and the stop endpoint command completion handler cannot delete the
  826. * timer before the timer function is called. Another endpoint cancellation may
  827. * sneak in before the timer function can grab the lock, and that may queue
  828. * another stop endpoint command and add the timer back. So we cannot use a
  829. * simple flag to say whether there is a pending stop endpoint command for a
  830. * particular endpoint.
  831. *
  832. * Instead we use a combination of that flag and a counter for the number of
  833. * pending stop endpoint commands. If the timer is the tail end of the last
  834. * stop endpoint command, and the endpoint's command is still pending, we assume
  835. * the host is dying.
  836. */
  837. void xhci_stop_endpoint_command_watchdog(unsigned long arg)
  838. {
  839. struct xhci_hcd *xhci;
  840. struct xhci_virt_ep *ep;
  841. struct xhci_virt_ep *temp_ep;
  842. struct xhci_ring *ring;
  843. struct xhci_td *cur_td;
  844. int ret, i, j;
  845. unsigned long flags;
  846. ep = (struct xhci_virt_ep *) arg;
  847. xhci = ep->xhci;
  848. spin_lock_irqsave(&xhci->lock, flags);
  849. if (xhci->main_hcd->driver->set_autosuspend)
  850. xhci->main_hcd->driver->set_autosuspend(xhci->main_hcd, 1);
  851. ep->stop_cmds_pending--;
  852. if (xhci->xhc_state & XHCI_STATE_DYING) {
  853. xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
  854. "xHCI as DYING, exiting.\n");
  855. spin_unlock_irqrestore(&xhci->lock, flags);
  856. return;
  857. }
  858. if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
  859. xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
  860. "exiting.\n");
  861. spin_unlock_irqrestore(&xhci->lock, flags);
  862. return;
  863. }
  864. xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
  865. xhci_warn(xhci, "Assuming host is dying, halting host.\n");
  866. /* Oops, HC is dead or dying or at least not responding to the stop
  867. * endpoint command.
  868. */
  869. xhci->xhc_state |= XHCI_STATE_DYING;
  870. /* Disable interrupts from the host controller and start halting it */
  871. xhci_quiesce(xhci);
  872. spin_unlock_irqrestore(&xhci->lock, flags);
  873. ret = xhci_halt(xhci);
  874. spin_lock_irqsave(&xhci->lock, flags);
  875. if (ret < 0) {
  876. /* This is bad; the host is not responding to commands and it's
  877. * not allowing itself to be halted. At least interrupts are
  878. * disabled. If we call usb_hc_died(), it will attempt to
  879. * disconnect all device drivers under this host. Those
  880. * disconnect() methods will wait for all URBs to be unlinked,
  881. * so we must complete them.
  882. */
  883. xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
  884. xhci_warn(xhci, "Completing active URBs anyway.\n");
  885. /* We could turn all TDs on the rings to no-ops. This won't
  886. * help if the host has cached part of the ring, and is slow if
  887. * we want to preserve the cycle bit. Skip it and hope the host
  888. * doesn't touch the memory.
  889. */
  890. }
  891. for (i = 0; i < MAX_HC_SLOTS; i++) {
  892. if (!xhci->devs[i])
  893. continue;
  894. for (j = 0; j < 31; j++) {
  895. temp_ep = &xhci->devs[i]->eps[j];
  896. ring = temp_ep->ring;
  897. if (!ring)
  898. continue;
  899. xhci_dbg(xhci, "Killing URBs for slot ID %u, "
  900. "ep index %u\n", i, j);
  901. while (!list_empty(&ring->td_list)) {
  902. cur_td = list_first_entry(&ring->td_list,
  903. struct xhci_td,
  904. td_list);
  905. list_del_init(&cur_td->td_list);
  906. if (!list_empty(&cur_td->cancelled_td_list))
  907. list_del_init(&cur_td->cancelled_td_list);
  908. xhci_giveback_urb_in_irq(xhci, cur_td,
  909. -ESHUTDOWN, "killed");
  910. }
  911. while (!list_empty(&temp_ep->cancelled_td_list)) {
  912. cur_td = list_first_entry(
  913. &temp_ep->cancelled_td_list,
  914. struct xhci_td,
  915. cancelled_td_list);
  916. list_del_init(&cur_td->cancelled_td_list);
  917. xhci_giveback_urb_in_irq(xhci, cur_td,
  918. -ESHUTDOWN, "killed");
  919. }
  920. }
  921. }
  922. spin_unlock_irqrestore(&xhci->lock, flags);
  923. xhci_dbg(xhci, "Calling usb_hc_died()\n");
  924. usb_hc_died(xhci_to_hcd(xhci)->primary_hcd);
  925. xhci_dbg(xhci, "xHCI host controller is dead.\n");
  926. }
  927. static void update_ring_for_set_deq_completion(struct xhci_hcd *xhci,
  928. struct xhci_virt_device *dev,
  929. struct xhci_ring *ep_ring,
  930. unsigned int ep_index)
  931. {
  932. union xhci_trb *dequeue_temp;
  933. int num_trbs_free_temp;
  934. bool revert = false;
  935. num_trbs_free_temp = ep_ring->num_trbs_free;
  936. dequeue_temp = ep_ring->dequeue;
  937. /* If we get two back-to-back stalls, and the first stalled transfer
  938. * ends just before a link TRB, the dequeue pointer will be left on
  939. * the link TRB by the code in the while loop. So we have to update
  940. * the dequeue pointer one segment further, or we'll jump off
  941. * the segment into la-la-land.
  942. */
  943. if (last_trb(xhci, ep_ring, ep_ring->deq_seg, ep_ring->dequeue)) {
  944. ep_ring->deq_seg = ep_ring->deq_seg->next;
  945. ep_ring->dequeue = ep_ring->deq_seg->trbs;
  946. }
  947. while (ep_ring->dequeue != dev->eps[ep_index].queued_deq_ptr) {
  948. /* We have more usable TRBs */
  949. ep_ring->num_trbs_free++;
  950. ep_ring->dequeue++;
  951. if (last_trb(xhci, ep_ring, ep_ring->deq_seg,
  952. ep_ring->dequeue)) {
  953. if (ep_ring->dequeue ==
  954. dev->eps[ep_index].queued_deq_ptr)
  955. break;
  956. ep_ring->deq_seg = ep_ring->deq_seg->next;
  957. ep_ring->dequeue = ep_ring->deq_seg->trbs;
  958. }
  959. if (ep_ring->dequeue == dequeue_temp) {
  960. revert = true;
  961. break;
  962. }
  963. }
  964. if (revert) {
  965. xhci_dbg(xhci, "Unable to find new dequeue pointer\n");
  966. ep_ring->num_trbs_free = num_trbs_free_temp;
  967. }
  968. }
  969. /*
  970. * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
  971. * we need to clear the set deq pending flag in the endpoint ring state, so that
  972. * the TD queueing code can ring the doorbell again. We also need to ring the
  973. * endpoint doorbell to restart the ring, but only if there aren't more
  974. * cancellations pending.
  975. */
  976. static void handle_set_deq_completion(struct xhci_hcd *xhci,
  977. struct xhci_event_cmd *event,
  978. union xhci_trb *trb)
  979. {
  980. unsigned int slot_id;
  981. unsigned int ep_index;
  982. unsigned int stream_id;
  983. struct xhci_ring *ep_ring;
  984. struct xhci_virt_device *dev;
  985. struct xhci_ep_ctx *ep_ctx;
  986. struct xhci_slot_ctx *slot_ctx;
  987. slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
  988. ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
  989. stream_id = TRB_TO_STREAM_ID(le32_to_cpu(trb->generic.field[2]));
  990. dev = xhci->devs[slot_id];
  991. ep_ring = xhci_stream_id_to_ring(dev, ep_index, stream_id);
  992. if (!ep_ring) {
  993. xhci_warn(xhci, "WARN Set TR deq ptr command for "
  994. "freed stream ID %u\n",
  995. stream_id);
  996. /* XXX: Harmless??? */
  997. dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
  998. return;
  999. }
  1000. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  1001. slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
  1002. if (GET_COMP_CODE(le32_to_cpu(event->status)) != COMP_SUCCESS) {
  1003. unsigned int ep_state;
  1004. unsigned int slot_state;
  1005. switch (GET_COMP_CODE(le32_to_cpu(event->status))) {
  1006. case COMP_TRB_ERR:
  1007. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
  1008. "of stream ID configuration\n");
  1009. break;
  1010. case COMP_CTX_STATE:
  1011. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
  1012. "to incorrect slot or ep state.\n");
  1013. ep_state = le32_to_cpu(ep_ctx->ep_info);
  1014. ep_state &= EP_STATE_MASK;
  1015. slot_state = le32_to_cpu(slot_ctx->dev_state);
  1016. slot_state = GET_SLOT_STATE(slot_state);
  1017. xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
  1018. slot_state, ep_state);
  1019. break;
  1020. case COMP_EBADSLT:
  1021. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
  1022. "slot %u was not enabled.\n", slot_id);
  1023. break;
  1024. default:
  1025. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
  1026. "completion code of %u.\n",
  1027. GET_COMP_CODE(le32_to_cpu(event->status)));
  1028. break;
  1029. }
  1030. /* OK what do we do now? The endpoint state is hosed, and we
  1031. * should never get to this point if the synchronization between
  1032. * queueing, and endpoint state are correct. This might happen
  1033. * if the device gets disconnected after we've finished
  1034. * cancelling URBs, which might not be an error...
  1035. */
  1036. } else {
  1037. xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
  1038. le64_to_cpu(ep_ctx->deq));
  1039. if (xhci_trb_virt_to_dma(dev->eps[ep_index].queued_deq_seg,
  1040. dev->eps[ep_index].queued_deq_ptr) ==
  1041. (le64_to_cpu(ep_ctx->deq) & ~(EP_CTX_CYCLE_MASK))) {
  1042. /* Update the ring's dequeue segment and dequeue pointer
  1043. * to reflect the new position.
  1044. */
  1045. update_ring_for_set_deq_completion(xhci, dev,
  1046. ep_ring, ep_index);
  1047. } else {
  1048. xhci_warn(xhci, "Mismatch between completed Set TR Deq "
  1049. "Ptr command & xHCI internal state.\n");
  1050. xhci_warn(xhci, "ep deq seg = %pK, deq ptr = %pK\n",
  1051. dev->eps[ep_index].queued_deq_seg,
  1052. dev->eps[ep_index].queued_deq_ptr);
  1053. }
  1054. }
  1055. dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
  1056. dev->eps[ep_index].queued_deq_seg = NULL;
  1057. dev->eps[ep_index].queued_deq_ptr = NULL;
  1058. /* Restart any rings with pending URBs */
  1059. ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
  1060. }
  1061. static void handle_reset_ep_completion(struct xhci_hcd *xhci,
  1062. struct xhci_event_cmd *event,
  1063. union xhci_trb *trb)
  1064. {
  1065. int slot_id;
  1066. unsigned int ep_index;
  1067. slot_id = TRB_TO_SLOT_ID(le32_to_cpu(trb->generic.field[3]));
  1068. ep_index = TRB_TO_EP_INDEX(le32_to_cpu(trb->generic.field[3]));
  1069. /* This command will only fail if the endpoint wasn't halted,
  1070. * but we don't care.
  1071. */
  1072. xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
  1073. GET_COMP_CODE(le32_to_cpu(event->status)));
  1074. /* HW with the reset endpoint quirk needs to have a configure endpoint
  1075. * command complete before the endpoint can be used. Queue that here
  1076. * because the HW can't handle two commands being queued in a row.
  1077. */
  1078. if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
  1079. xhci_dbg(xhci, "Queueing configure endpoint command\n");
  1080. xhci_queue_configure_endpoint(xhci,
  1081. xhci->devs[slot_id]->in_ctx->dma, slot_id,
  1082. false);
  1083. xhci_ring_cmd_db(xhci);
  1084. } else {
  1085. /* Clear our internal halted state */
  1086. xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
  1087. }
  1088. }
  1089. /* Complete the command and detele it from the devcie's command queue.
  1090. */
  1091. static void xhci_complete_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
  1092. struct xhci_command *command, u32 status)
  1093. {
  1094. command->status = status;
  1095. list_del(&command->cmd_list);
  1096. if (command->completion)
  1097. complete(command->completion);
  1098. else
  1099. xhci_free_command(xhci, command);
  1100. }
  1101. /* Check to see if a command in the device's command queue matches this one.
  1102. * Signal the completion or free the command, and return 1. Return 0 if the
  1103. * completed command isn't at the head of the command list.
  1104. */
  1105. static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
  1106. struct xhci_virt_device *virt_dev,
  1107. struct xhci_event_cmd *event)
  1108. {
  1109. struct xhci_command *command;
  1110. if (list_empty(&virt_dev->cmd_list))
  1111. return 0;
  1112. command = list_entry(virt_dev->cmd_list.next,
  1113. struct xhci_command, cmd_list);
  1114. if (xhci->cmd_ring->dequeue != command->command_trb)
  1115. return 0;
  1116. xhci_complete_cmd_in_cmd_wait_list(xhci, command,
  1117. GET_COMP_CODE(le32_to_cpu(event->status)));
  1118. return 1;
  1119. }
  1120. /*
  1121. * Finding the command trb need to be cancelled and modifying it to
  1122. * NO OP command. And if the command is in device's command wait
  1123. * list, finishing and freeing it.
  1124. *
  1125. * If we can't find the command trb, we think it had already been
  1126. * executed.
  1127. */
  1128. static void xhci_cmd_to_noop(struct xhci_hcd *xhci, struct xhci_cd *cur_cd)
  1129. {
  1130. struct xhci_segment *cur_seg;
  1131. union xhci_trb *cmd_trb;
  1132. u32 cycle_state;
  1133. if (xhci->cmd_ring->dequeue == xhci->cmd_ring->enqueue)
  1134. return;
  1135. /* find the current segment of command ring */
  1136. cur_seg = find_trb_seg(xhci->cmd_ring->first_seg,
  1137. xhci->cmd_ring->dequeue, &cycle_state);
  1138. if (!cur_seg) {
  1139. xhci_warn(xhci, "Command ring mismatch, dequeue = %pK %llx (dma)\n",
  1140. xhci->cmd_ring->dequeue,
  1141. (unsigned long long)
  1142. xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  1143. xhci->cmd_ring->dequeue));
  1144. xhci_debug_ring(xhci, xhci->cmd_ring);
  1145. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  1146. return;
  1147. }
  1148. /* find the command trb matched by cd from command ring */
  1149. for (cmd_trb = xhci->cmd_ring->dequeue;
  1150. cmd_trb != xhci->cmd_ring->enqueue;
  1151. next_trb(xhci, xhci->cmd_ring, &cur_seg, &cmd_trb)) {
  1152. /* If the trb is link trb, continue */
  1153. if (TRB_TYPE_LINK_LE32(cmd_trb->generic.field[3]))
  1154. continue;
  1155. if (cur_cd->cmd_trb == cmd_trb) {
  1156. /* If the command in device's command list, we should
  1157. * finish it and free the command structure.
  1158. */
  1159. if (cur_cd->command)
  1160. xhci_complete_cmd_in_cmd_wait_list(xhci,
  1161. cur_cd->command, COMP_CMD_STOP);
  1162. /* get cycle state from the origin command trb */
  1163. cycle_state = le32_to_cpu(cmd_trb->generic.field[3])
  1164. & TRB_CYCLE;
  1165. /* modify the command trb to NO OP command */
  1166. cmd_trb->generic.field[0] = 0;
  1167. cmd_trb->generic.field[1] = 0;
  1168. cmd_trb->generic.field[2] = 0;
  1169. cmd_trb->generic.field[3] = cpu_to_le32(
  1170. TRB_TYPE(TRB_CMD_NOOP) | cycle_state);
  1171. break;
  1172. }
  1173. }
  1174. }
  1175. static void xhci_cancel_cmd_in_cd_list(struct xhci_hcd *xhci)
  1176. {
  1177. struct xhci_cd *cur_cd, *next_cd;
  1178. if (list_empty(&xhci->cancel_cmd_list))
  1179. return;
  1180. list_for_each_entry_safe(cur_cd, next_cd,
  1181. &xhci->cancel_cmd_list, cancel_cmd_list) {
  1182. xhci_cmd_to_noop(xhci, cur_cd);
  1183. list_del(&cur_cd->cancel_cmd_list);
  1184. kfree(cur_cd);
  1185. }
  1186. }
  1187. /*
  1188. * traversing the cancel_cmd_list. If the command descriptor according
  1189. * to cmd_trb is found, the function free it and return 1, otherwise
  1190. * return 0.
  1191. */
  1192. static int xhci_search_cmd_trb_in_cd_list(struct xhci_hcd *xhci,
  1193. union xhci_trb *cmd_trb)
  1194. {
  1195. struct xhci_cd *cur_cd, *next_cd;
  1196. if (list_empty(&xhci->cancel_cmd_list))
  1197. return 0;
  1198. list_for_each_entry_safe(cur_cd, next_cd,
  1199. &xhci->cancel_cmd_list, cancel_cmd_list) {
  1200. if (cur_cd->cmd_trb == cmd_trb) {
  1201. if (cur_cd->command)
  1202. xhci_complete_cmd_in_cmd_wait_list(xhci,
  1203. cur_cd->command, COMP_CMD_STOP);
  1204. list_del(&cur_cd->cancel_cmd_list);
  1205. kfree(cur_cd);
  1206. return 1;
  1207. }
  1208. }
  1209. return 0;
  1210. }
  1211. /*
  1212. * If the cmd_trb_comp_code is COMP_CMD_ABORT, we just check whether the
  1213. * trb pointed by the command ring dequeue pointer is the trb we want to
  1214. * cancel or not. And if the cmd_trb_comp_code is COMP_CMD_STOP, we will
  1215. * traverse the cancel_cmd_list to trun the all of the commands according
  1216. * to command descriptor to NO-OP trb.
  1217. */
  1218. static int handle_stopped_cmd_ring(struct xhci_hcd *xhci,
  1219. int cmd_trb_comp_code)
  1220. {
  1221. int cur_trb_is_good = 0;
  1222. /* Searching the cmd trb pointed by the command ring dequeue
  1223. * pointer in command descriptor list. If it is found, free it.
  1224. */
  1225. cur_trb_is_good = xhci_search_cmd_trb_in_cd_list(xhci,
  1226. xhci->cmd_ring->dequeue);
  1227. if (cmd_trb_comp_code == COMP_CMD_ABORT)
  1228. xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
  1229. else if (cmd_trb_comp_code == COMP_CMD_STOP) {
  1230. /* traversing the cancel_cmd_list and canceling
  1231. * the command according to command descriptor
  1232. */
  1233. xhci_cancel_cmd_in_cd_list(xhci);
  1234. xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
  1235. /*
  1236. * ring command ring doorbell again to restart the
  1237. * command ring
  1238. */
  1239. if (xhci->cmd_ring->dequeue != xhci->cmd_ring->enqueue)
  1240. xhci_ring_cmd_db(xhci);
  1241. }
  1242. return cur_trb_is_good;
  1243. }
  1244. static void handle_cmd_completion(struct xhci_hcd *xhci,
  1245. struct xhci_event_cmd *event)
  1246. {
  1247. int slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
  1248. u64 cmd_dma;
  1249. dma_addr_t cmd_dequeue_dma;
  1250. struct xhci_input_control_ctx *ctrl_ctx;
  1251. struct xhci_virt_device *virt_dev;
  1252. unsigned int ep_index;
  1253. struct xhci_ring *ep_ring;
  1254. unsigned int ep_state;
  1255. cmd_dma = le64_to_cpu(event->cmd_trb);
  1256. cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  1257. xhci->cmd_ring->dequeue);
  1258. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  1259. if (cmd_dequeue_dma == 0) {
  1260. xhci->error_bitmask |= 1 << 4;
  1261. return;
  1262. }
  1263. /* Does the DMA address match our internal dequeue pointer address? */
  1264. if (cmd_dma != (u64) cmd_dequeue_dma) {
  1265. xhci->error_bitmask |= 1 << 5;
  1266. return;
  1267. }
  1268. if ((GET_COMP_CODE(le32_to_cpu(event->status)) == COMP_CMD_ABORT) ||
  1269. (GET_COMP_CODE(le32_to_cpu(event->status)) == COMP_CMD_STOP)) {
  1270. /* If the return value is 0, we think the trb pointed by
  1271. * command ring dequeue pointer is a good trb. The good
  1272. * trb means we don't want to cancel the trb, but it have
  1273. * been stopped by host. So we should handle it normally.
  1274. * Otherwise, driver should invoke inc_deq() and return.
  1275. */
  1276. if (handle_stopped_cmd_ring(xhci,
  1277. GET_COMP_CODE(le32_to_cpu(event->status)))) {
  1278. inc_deq(xhci, xhci->cmd_ring);
  1279. return;
  1280. }
  1281. /* There is no command to handle if we get a stop event when the
  1282. * command ring is empty, event->cmd_trb points to the next
  1283. * unset command
  1284. */
  1285. if (xhci->cmd_ring->dequeue == xhci->cmd_ring->enqueue)
  1286. return;
  1287. }
  1288. switch (le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3])
  1289. & TRB_TYPE_BITMASK) {
  1290. case TRB_TYPE(TRB_ENABLE_SLOT):
  1291. if (GET_COMP_CODE(le32_to_cpu(event->status)) == COMP_SUCCESS)
  1292. xhci->slot_id = slot_id;
  1293. else
  1294. xhci->slot_id = 0;
  1295. complete(&xhci->addr_dev);
  1296. break;
  1297. case TRB_TYPE(TRB_DISABLE_SLOT):
  1298. if (xhci->devs[slot_id]) {
  1299. if (xhci->quirks & XHCI_EP_LIMIT_QUIRK)
  1300. /* Delete default control endpoint resources */
  1301. xhci_free_device_endpoint_resources(xhci,
  1302. xhci->devs[slot_id], true);
  1303. xhci_free_virt_device(xhci, slot_id);
  1304. }
  1305. break;
  1306. case TRB_TYPE(TRB_CONFIG_EP):
  1307. virt_dev = xhci->devs[slot_id];
  1308. if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
  1309. break;
  1310. /*
  1311. * Configure endpoint commands can come from the USB core
  1312. * configuration or alt setting changes, or because the HW
  1313. * needed an extra configure endpoint command after a reset
  1314. * endpoint command or streams were being configured.
  1315. * If the command was for a halted endpoint, the xHCI driver
  1316. * is not waiting on the configure endpoint command.
  1317. */
  1318. ctrl_ctx = xhci_get_input_control_ctx(xhci,
  1319. virt_dev->in_ctx);
  1320. /* Input ctx add_flags are the endpoint index plus one */
  1321. ep_index = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags)) - 1;
  1322. /* A usb_set_interface() call directly after clearing a halted
  1323. * condition may race on this quirky hardware. Not worth
  1324. * worrying about, since this is prototype hardware. Not sure
  1325. * if this will work for streams, but streams support was
  1326. * untested on this prototype.
  1327. */
  1328. if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
  1329. ep_index != (unsigned int) -1 &&
  1330. le32_to_cpu(ctrl_ctx->add_flags) - SLOT_FLAG ==
  1331. le32_to_cpu(ctrl_ctx->drop_flags)) {
  1332. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  1333. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  1334. if (!(ep_state & EP_HALTED))
  1335. goto bandwidth_change;
  1336. xhci_dbg(xhci, "Completed config ep cmd - "
  1337. "last ep index = %d, state = %d\n",
  1338. ep_index, ep_state);
  1339. /* Clear internal halted state and restart ring(s) */
  1340. xhci->devs[slot_id]->eps[ep_index].ep_state &=
  1341. ~EP_HALTED;
  1342. ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
  1343. break;
  1344. }
  1345. bandwidth_change:
  1346. xhci_dbg(xhci, "Completed config ep cmd\n");
  1347. xhci->devs[slot_id]->cmd_status =
  1348. GET_COMP_CODE(le32_to_cpu(event->status));
  1349. complete(&xhci->devs[slot_id]->cmd_completion);
  1350. break;
  1351. case TRB_TYPE(TRB_EVAL_CONTEXT):
  1352. virt_dev = xhci->devs[slot_id];
  1353. if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
  1354. break;
  1355. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status));
  1356. complete(&xhci->devs[slot_id]->cmd_completion);
  1357. break;
  1358. case TRB_TYPE(TRB_ADDR_DEV):
  1359. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(le32_to_cpu(event->status));
  1360. complete(&xhci->addr_dev);
  1361. break;
  1362. case TRB_TYPE(TRB_STOP_RING):
  1363. handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue, event);
  1364. break;
  1365. case TRB_TYPE(TRB_SET_DEQ):
  1366. handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
  1367. break;
  1368. case TRB_TYPE(TRB_CMD_NOOP):
  1369. break;
  1370. case TRB_TYPE(TRB_RESET_EP):
  1371. handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
  1372. break;
  1373. case TRB_TYPE(TRB_RESET_DEV):
  1374. xhci_dbg(xhci, "Completed reset device command.\n");
  1375. slot_id = TRB_TO_SLOT_ID(
  1376. le32_to_cpu(xhci->cmd_ring->dequeue->generic.field[3]));
  1377. virt_dev = xhci->devs[slot_id];
  1378. if (virt_dev)
  1379. handle_cmd_in_cmd_wait_list(xhci, virt_dev, event);
  1380. else
  1381. xhci_warn(xhci, "Reset device command completion "
  1382. "for disabled slot %u\n", slot_id);
  1383. break;
  1384. case TRB_TYPE(TRB_NEC_GET_FW):
  1385. if (!(xhci->quirks & XHCI_NEC_HOST)) {
  1386. xhci->error_bitmask |= 1 << 6;
  1387. break;
  1388. }
  1389. xhci_dbg(xhci, "NEC firmware version %2x.%02x\n",
  1390. NEC_FW_MAJOR(le32_to_cpu(event->status)),
  1391. NEC_FW_MINOR(le32_to_cpu(event->status)));
  1392. break;
  1393. default:
  1394. /* Skip over unknown commands on the event ring */
  1395. xhci->error_bitmask |= 1 << 6;
  1396. break;
  1397. }
  1398. inc_deq(xhci, xhci->cmd_ring);
  1399. }
  1400. static void handle_vendor_event(struct xhci_hcd *xhci,
  1401. union xhci_trb *event)
  1402. {
  1403. u32 trb_type;
  1404. trb_type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->generic.field[3]));
  1405. xhci_dbg(xhci, "Vendor specific event TRB type = %u\n", trb_type);
  1406. if (trb_type == TRB_NEC_CMD_COMP && (xhci->quirks & XHCI_NEC_HOST))
  1407. handle_cmd_completion(xhci, &event->event_cmd);
  1408. }
  1409. /* @port_id: the one-based port ID from the hardware (indexed from array of all
  1410. * port registers -- USB 3.0 and USB 2.0).
  1411. *
  1412. * Returns a zero-based port number, which is suitable for indexing into each of
  1413. * the split roothubs' port arrays and bus state arrays.
  1414. * Add one to it in order to call xhci_find_slot_id_by_port.
  1415. */
  1416. static unsigned int find_faked_portnum_from_hw_portnum(struct usb_hcd *hcd,
  1417. struct xhci_hcd *xhci, u32 port_id)
  1418. {
  1419. unsigned int i;
  1420. unsigned int num_similar_speed_ports = 0;
  1421. /* port_id from the hardware is 1-based, but port_array[], usb3_ports[],
  1422. * and usb2_ports are 0-based indexes. Count the number of similar
  1423. * speed ports, up to 1 port before this port.
  1424. */
  1425. for (i = 0; i < (port_id - 1); i++) {
  1426. u8 port_speed = xhci->port_array[i];
  1427. /*
  1428. * Skip ports that don't have known speeds, or have duplicate
  1429. * Extended Capabilities port speed entries.
  1430. */
  1431. if (port_speed == 0 || port_speed == DUPLICATE_ENTRY)
  1432. continue;
  1433. /*
  1434. * USB 3.0 ports are always under a USB 3.0 hub. USB 2.0 and
  1435. * 1.1 ports are under the USB 2.0 hub. If the port speed
  1436. * matches the device speed, it's a similar speed port.
  1437. */
  1438. if ((port_speed == 0x03) == (hcd->speed == HCD_USB3))
  1439. num_similar_speed_ports++;
  1440. }
  1441. return num_similar_speed_ports;
  1442. }
  1443. static void handle_device_notification(struct xhci_hcd *xhci,
  1444. union xhci_trb *event)
  1445. {
  1446. u32 slot_id;
  1447. struct usb_device *udev;
  1448. slot_id = TRB_TO_SLOT_ID(event->generic.field[3]);
  1449. if (!xhci->devs[slot_id]) {
  1450. xhci_warn(xhci, "Device Notification event for "
  1451. "unused slot %u\n", slot_id);
  1452. return;
  1453. }
  1454. xhci_dbg(xhci, "Device Wake Notification event for slot ID %u\n",
  1455. slot_id);
  1456. udev = xhci->devs[slot_id]->udev;
  1457. if (udev && udev->parent)
  1458. usb_wakeup_notification(udev->parent, udev->portnum);
  1459. }
  1460. static void handle_port_status(struct xhci_hcd *xhci,
  1461. union xhci_trb *event)
  1462. {
  1463. struct usb_hcd *hcd = NULL;
  1464. u32 port_id;
  1465. u32 temp, temp1;
  1466. int max_ports;
  1467. int slot_id;
  1468. unsigned int faked_port_index;
  1469. u8 major_revision;
  1470. struct xhci_bus_state *bus_state;
  1471. __le32 __iomem **port_array;
  1472. bool bogus_port_status = false;
  1473. /* Port status change events always have a successful completion code */
  1474. if (GET_COMP_CODE(le32_to_cpu(event->generic.field[2])) != COMP_SUCCESS) {
  1475. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  1476. xhci->error_bitmask |= 1 << 8;
  1477. }
  1478. port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0]));
  1479. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  1480. max_ports = HCS_MAX_PORTS(xhci->hcs_params1);
  1481. if ((port_id <= 0) || (port_id > max_ports)) {
  1482. xhci_warn(xhci, "Invalid port id %d\n", port_id);
  1483. bogus_port_status = true;
  1484. goto cleanup;
  1485. }
  1486. /* Figure out which usb_hcd this port is attached to:
  1487. * is it a USB 3.0 port or a USB 2.0/1.1 port?
  1488. */
  1489. major_revision = xhci->port_array[port_id - 1];
  1490. if (major_revision == 0) {
  1491. xhci_warn(xhci, "Event for port %u not in "
  1492. "Extended Capabilities, ignoring.\n",
  1493. port_id);
  1494. bogus_port_status = true;
  1495. goto cleanup;
  1496. }
  1497. if (major_revision == DUPLICATE_ENTRY) {
  1498. xhci_warn(xhci, "Event for port %u duplicated in"
  1499. "Extended Capabilities, ignoring.\n",
  1500. port_id);
  1501. bogus_port_status = true;
  1502. goto cleanup;
  1503. }
  1504. /*
  1505. * Hardware port IDs reported by a Port Status Change Event include USB
  1506. * 3.0 and USB 2.0 ports. We want to check if the port has reported a
  1507. * resume event, but we first need to translate the hardware port ID
  1508. * into the index into the ports on the correct split roothub, and the
  1509. * correct bus_state structure.
  1510. */
  1511. /* Find the right roothub. */
  1512. hcd = xhci_to_hcd(xhci);
  1513. if (!hcd)
  1514. goto cleanup;
  1515. if ((major_revision == 0x03) != (hcd->speed == HCD_USB3))
  1516. hcd = xhci->shared_hcd;
  1517. bus_state = &xhci->bus_state[hcd_index(hcd)];
  1518. if (hcd->speed == HCD_USB3)
  1519. port_array = xhci->usb3_ports;
  1520. else
  1521. port_array = xhci->usb2_ports;
  1522. /* Find the faked port hub number */
  1523. faked_port_index = find_faked_portnum_from_hw_portnum(hcd, xhci,
  1524. port_id);
  1525. temp = xhci_readl(xhci, port_array[faked_port_index]);
  1526. if (hcd->state == HC_STATE_SUSPENDED) {
  1527. xhci_dbg(xhci, "resume root hub\n");
  1528. usb_hcd_resume_root_hub(hcd);
  1529. }
  1530. if (hcd->speed == HCD_USB3 && (temp & PORT_PLS_MASK) == XDEV_INACTIVE)
  1531. bus_state->port_remote_wakeup &= ~(1 << faked_port_index);
  1532. if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_RESUME) {
  1533. xhci_dbg(xhci, "port resume event for port %d\n", port_id);
  1534. temp1 = xhci_readl(xhci, &xhci->op_regs->command);
  1535. if (!(temp1 & CMD_RUN)) {
  1536. xhci_warn(xhci, "xHC is not running.\n");
  1537. goto cleanup;
  1538. }
  1539. if (DEV_SUPERSPEED(temp)) {
  1540. xhci_dbg(xhci, "remote wake SS port %d\n", port_id);
  1541. /* Set a flag to say the port signaled remote wakeup,
  1542. * so we can tell the difference between the end of
  1543. * device and host initiated resume.
  1544. */
  1545. bus_state->port_remote_wakeup |= 1 << faked_port_index;
  1546. xhci_test_and_clear_bit(xhci, port_array,
  1547. faked_port_index, PORT_PLC);
  1548. xhci_set_link_state(xhci, port_array, faked_port_index,
  1549. XDEV_U0);
  1550. /* Need to wait until the next link state change
  1551. * indicates the device is actually in U0.
  1552. */
  1553. bogus_port_status = true;
  1554. goto cleanup;
  1555. } else {
  1556. xhci_dbg(xhci, "resume HS port %d\n", port_id);
  1557. bus_state->resume_done[faked_port_index] = jiffies +
  1558. msecs_to_jiffies(20);
  1559. set_bit(faked_port_index, &bus_state->resuming_ports);
  1560. mod_timer(&hcd->rh_timer,
  1561. bus_state->resume_done[faked_port_index]);
  1562. /* Do the rest in GetPortStatus */
  1563. }
  1564. }
  1565. if ((temp & PORT_PLC) && (temp & PORT_PLS_MASK) == XDEV_U0 &&
  1566. DEV_SUPERSPEED(temp)) {
  1567. xhci_dbg(xhci, "resume SS port %d finished\n", port_id);
  1568. /* We've just brought the device into U0 through either the
  1569. * Resume state after a device remote wakeup, or through the
  1570. * U3Exit state after a host-initiated resume. If it's a device
  1571. * initiated remote wake, don't pass up the link state change,
  1572. * so the roothub behavior is consistent with external
  1573. * USB 3.0 hub behavior.
  1574. */
  1575. slot_id = xhci_find_slot_id_by_port(hcd, xhci,
  1576. faked_port_index + 1);
  1577. if (slot_id && xhci->devs[slot_id])
  1578. xhci_ring_device(xhci, slot_id);
  1579. if (bus_state->port_remote_wakeup & (1 << faked_port_index)) {
  1580. bus_state->port_remote_wakeup &=
  1581. ~(1 << faked_port_index);
  1582. xhci_test_and_clear_bit(xhci, port_array,
  1583. faked_port_index, PORT_PLC);
  1584. usb_wakeup_notification(hcd->self.root_hub,
  1585. faked_port_index + 1);
  1586. bogus_port_status = true;
  1587. goto cleanup;
  1588. }
  1589. }
  1590. if (hcd->speed != HCD_USB3)
  1591. xhci_test_and_clear_bit(xhci, port_array, faked_port_index,
  1592. PORT_PLC);
  1593. cleanup:
  1594. /* Update event ring dequeue pointer before dropping the lock */
  1595. inc_deq(xhci, xhci->event_ring);
  1596. /* Don't make the USB core poll the roothub if we got a bad port status
  1597. * change event. Besides, at that point we can't tell which roothub
  1598. * (USB 2.0 or USB 3.0) to kick.
  1599. */
  1600. if (bogus_port_status)
  1601. return;
  1602. /*
  1603. * xHCI port-status-change events occur when the "or" of all the
  1604. * status-change bits in the portsc register changes from 0 to 1.
  1605. * New status changes won't cause an event if any other change
  1606. * bits are still set. When an event occurs, switch over to
  1607. * polling to avoid losing status changes.
  1608. */
  1609. xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
  1610. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1611. spin_unlock(&xhci->lock);
  1612. /* Pass this up to the core */
  1613. usb_hcd_poll_rh_status(hcd);
  1614. spin_lock(&xhci->lock);
  1615. }
  1616. /*
  1617. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  1618. * at end_trb, which may be in another segment. If the suspect DMA address is a
  1619. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  1620. * returns 0.
  1621. */
  1622. struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
  1623. union xhci_trb *start_trb,
  1624. union xhci_trb *end_trb,
  1625. dma_addr_t suspect_dma)
  1626. {
  1627. dma_addr_t start_dma;
  1628. dma_addr_t end_seg_dma;
  1629. dma_addr_t end_trb_dma;
  1630. struct xhci_segment *cur_seg;
  1631. start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
  1632. cur_seg = start_seg;
  1633. do {
  1634. if (start_dma == 0)
  1635. return NULL;
  1636. /* We may get an event for a Link TRB in the middle of a TD */
  1637. end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
  1638. &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
  1639. /* If the end TRB isn't in this segment, this is set to 0 */
  1640. end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
  1641. if (end_trb_dma > 0) {
  1642. /* The end TRB is in this segment, so suspect should be here */
  1643. if (start_dma <= end_trb_dma) {
  1644. if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
  1645. return cur_seg;
  1646. } else {
  1647. /* Case for one segment with
  1648. * a TD wrapped around to the top
  1649. */
  1650. if ((suspect_dma >= start_dma &&
  1651. suspect_dma <= end_seg_dma) ||
  1652. (suspect_dma >= cur_seg->dma &&
  1653. suspect_dma <= end_trb_dma))
  1654. return cur_seg;
  1655. }
  1656. return NULL;
  1657. } else {
  1658. /* Might still be somewhere in this segment */
  1659. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  1660. return cur_seg;
  1661. }
  1662. cur_seg = cur_seg->next;
  1663. start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  1664. } while (cur_seg != start_seg);
  1665. return NULL;
  1666. }
  1667. static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
  1668. unsigned int slot_id, unsigned int ep_index,
  1669. unsigned int stream_id,
  1670. struct xhci_td *td, union xhci_trb *event_trb)
  1671. {
  1672. struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
  1673. ep->ep_state |= EP_HALTED;
  1674. ep->stopped_td = td;
  1675. ep->stopped_trb = event_trb;
  1676. ep->stopped_stream = stream_id;
  1677. xhci_queue_reset_ep(xhci, slot_id, ep_index);
  1678. xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
  1679. ep->stopped_td = NULL;
  1680. ep->stopped_trb = NULL;
  1681. ep->stopped_stream = 0;
  1682. xhci_ring_cmd_db(xhci);
  1683. }
  1684. /* Check if an error has halted the endpoint ring. The class driver will
  1685. * cleanup the halt for a non-default control endpoint if we indicate a stall.
  1686. * However, a babble and other errors also halt the endpoint ring, and the class
  1687. * driver won't clear the halt in that case, so we need to issue a Set Transfer
  1688. * Ring Dequeue Pointer command manually.
  1689. */
  1690. static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
  1691. struct xhci_ep_ctx *ep_ctx,
  1692. unsigned int trb_comp_code)
  1693. {
  1694. /* TRB completion codes that may require a manual halt cleanup */
  1695. if (trb_comp_code == COMP_TX_ERR ||
  1696. trb_comp_code == COMP_BABBLE ||
  1697. trb_comp_code == COMP_SPLIT_ERR)
  1698. /* The 0.96 spec says a babbling control endpoint
  1699. * is not halted. The 0.96 spec says it is. Some HW
  1700. * claims to be 0.95 compliant, but it halts the control
  1701. * endpoint anyway. Check if a babble halted the
  1702. * endpoint.
  1703. */
  1704. if ((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
  1705. cpu_to_le32(EP_STATE_HALTED))
  1706. return 1;
  1707. return 0;
  1708. }
  1709. int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
  1710. {
  1711. if (trb_comp_code >= 224 && trb_comp_code <= 255) {
  1712. /* Vendor defined "informational" completion code,
  1713. * treat as not-an-error.
  1714. */
  1715. xhci_dbg(xhci, "Vendor defined info completion code %u\n",
  1716. trb_comp_code);
  1717. xhci_dbg(xhci, "Treating code as success.\n");
  1718. return 1;
  1719. }
  1720. return 0;
  1721. }
  1722. /*
  1723. * Finish the td processing, remove the td from td list;
  1724. * Return 1 if the urb can be given back.
  1725. */
  1726. static int finish_td(struct xhci_hcd *xhci, struct xhci_td *td,
  1727. union xhci_trb *event_trb, struct xhci_transfer_event *event,
  1728. struct xhci_virt_ep *ep, int *status, bool skip)
  1729. {
  1730. struct xhci_virt_device *xdev;
  1731. struct xhci_ring *ep_ring;
  1732. unsigned int slot_id;
  1733. int ep_index;
  1734. struct urb *urb = NULL;
  1735. struct xhci_ep_ctx *ep_ctx;
  1736. int ret = 0;
  1737. struct urb_priv *urb_priv;
  1738. u32 trb_comp_code;
  1739. slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
  1740. xdev = xhci->devs[slot_id];
  1741. ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
  1742. ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
  1743. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1744. trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
  1745. if (skip)
  1746. goto td_cleanup;
  1747. if (trb_comp_code == COMP_STOP_INVAL ||
  1748. trb_comp_code == COMP_STOP) {
  1749. /* The Endpoint Stop Command completion will take care of any
  1750. * stopped TDs. A stopped TD may be restarted, so don't update
  1751. * the ring dequeue pointer or take this TD off any lists yet.
  1752. */
  1753. ep->stopped_td = td;
  1754. ep->stopped_trb = event_trb;
  1755. return 0;
  1756. } else {
  1757. if (trb_comp_code == COMP_STALL ||
  1758. xhci_requires_manual_halt_cleanup(xhci, ep_ctx,
  1759. trb_comp_code)) {
  1760. /* Issue a reset endpoint command to clear the host side * halt, followed by a set dequeue command to move the
  1761. * dequeue pointer past the TD.
  1762. * The class driver clears the device side halt later.
  1763. */
  1764. xhci_cleanup_halted_endpoint(xhci,
  1765. slot_id, ep_index, ep_ring->stream_id,
  1766. td, event_trb);
  1767. } else {
  1768. /* Update ring dequeue pointer */
  1769. while (ep_ring->dequeue != td->last_trb)
  1770. inc_deq(xhci, ep_ring);
  1771. inc_deq(xhci, ep_ring);
  1772. }
  1773. td_cleanup:
  1774. /* Clean up the endpoint's TD list */
  1775. urb = td->urb;
  1776. urb_priv = urb->hcpriv;
  1777. /* Do one last check of the actual transfer length.
  1778. * If the host controller said we transferred more data than
  1779. * the buffer length, urb->actual_length will be a very big
  1780. * number (since it's unsigned). Play it safe and say we didn't
  1781. * transfer anything.
  1782. */
  1783. if (urb->actual_length > urb->transfer_buffer_length) {
  1784. xhci_warn(xhci, "URB transfer length is wrong, "
  1785. "xHC issue? req. len = %u, "
  1786. "act. len = %u\n",
  1787. urb->transfer_buffer_length,
  1788. urb->actual_length);
  1789. urb->actual_length = 0;
  1790. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1791. *status = -EREMOTEIO;
  1792. else
  1793. *status = 0;
  1794. }
  1795. list_del_init(&td->td_list);
  1796. /* Was this TD slated to be cancelled but completed anyway? */
  1797. if (!list_empty(&td->cancelled_td_list))
  1798. list_del_init(&td->cancelled_td_list);
  1799. urb_priv->td_cnt++;
  1800. /* Giveback the urb when all the tds are completed */
  1801. if (urb_priv->td_cnt == urb_priv->length) {
  1802. ret = 1;
  1803. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  1804. xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs--;
  1805. if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs
  1806. == 0) {
  1807. if (xhci->quirks & XHCI_AMD_PLL_FIX)
  1808. usb_amd_quirk_pll_enable();
  1809. }
  1810. }
  1811. }
  1812. }
  1813. return ret;
  1814. }
  1815. /*
  1816. * Process control tds, update urb status and actual_length.
  1817. */
  1818. static int process_ctrl_td(struct xhci_hcd *xhci, struct xhci_td *td,
  1819. union xhci_trb *event_trb, struct xhci_transfer_event *event,
  1820. struct xhci_virt_ep *ep, int *status)
  1821. {
  1822. struct xhci_virt_device *xdev;
  1823. struct xhci_ring *ep_ring;
  1824. unsigned int slot_id;
  1825. int ep_index;
  1826. struct xhci_ep_ctx *ep_ctx;
  1827. u32 trb_comp_code;
  1828. slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
  1829. xdev = xhci->devs[slot_id];
  1830. ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
  1831. ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
  1832. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1833. trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
  1834. switch (trb_comp_code) {
  1835. case COMP_SUCCESS:
  1836. if (event_trb == ep_ring->dequeue) {
  1837. xhci_warn(xhci, "WARN: Success on ctrl setup TRB "
  1838. "without IOC set??\n");
  1839. *status = -ESHUTDOWN;
  1840. } else if (event_trb != td->last_trb) {
  1841. xhci_warn(xhci, "WARN: Success on ctrl data TRB "
  1842. "without IOC set??\n");
  1843. *status = -ESHUTDOWN;
  1844. } else {
  1845. *status = 0;
  1846. }
  1847. break;
  1848. case COMP_SHORT_TX:
  1849. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1850. *status = -EREMOTEIO;
  1851. else
  1852. *status = 0;
  1853. break;
  1854. case COMP_STOP_INVAL:
  1855. case COMP_STOP:
  1856. return finish_td(xhci, td, event_trb, event, ep, status, false);
  1857. default:
  1858. if (!xhci_requires_manual_halt_cleanup(xhci,
  1859. ep_ctx, trb_comp_code))
  1860. break;
  1861. xhci_dbg(xhci, "TRB error code %u, "
  1862. "halted endpoint index = %u\n",
  1863. trb_comp_code, ep_index);
  1864. /* else fall through */
  1865. case COMP_STALL:
  1866. /* Did we transfer part of the data (middle) phase? */
  1867. if (event_trb != ep_ring->dequeue &&
  1868. event_trb != td->last_trb)
  1869. td->urb->actual_length =
  1870. td->urb->transfer_buffer_length -
  1871. EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
  1872. else
  1873. td->urb->actual_length = 0;
  1874. return finish_td(xhci, td, event_trb, event, ep, status, false);
  1875. }
  1876. /*
  1877. * Did we transfer any data, despite the errors that might have
  1878. * happened? I.e. did we get past the setup stage?
  1879. */
  1880. if (event_trb != ep_ring->dequeue) {
  1881. /* The event was for the status stage */
  1882. if (event_trb == td->last_trb) {
  1883. if (td->urb_length_set) {
  1884. /* Don't overwrite a previously set error code
  1885. */
  1886. if ((*status == -EINPROGRESS || *status == 0) &&
  1887. (td->urb->transfer_flags
  1888. & URB_SHORT_NOT_OK))
  1889. /* Did we already see a short data
  1890. * stage? */
  1891. *status = -EREMOTEIO;
  1892. } else {
  1893. td->urb->actual_length =
  1894. td->urb->transfer_buffer_length;
  1895. }
  1896. } else {
  1897. /*
  1898. * Maybe the event was for the data stage? If so, update
  1899. * already the actual_length of the URB and flag it as
  1900. * set, so that it is not overwritten in the event for
  1901. * the last TRB.
  1902. */
  1903. td->urb_length_set = true;
  1904. td->urb->actual_length =
  1905. td->urb->transfer_buffer_length -
  1906. EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
  1907. xhci_dbg(xhci, "Waiting for status "
  1908. "stage event\n");
  1909. return 0;
  1910. }
  1911. }
  1912. return finish_td(xhci, td, event_trb, event, ep, status, false);
  1913. }
  1914. /*
  1915. * Process isochronous tds, update urb packet status and actual_length.
  1916. */
  1917. static int process_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
  1918. union xhci_trb *event_trb, struct xhci_transfer_event *event,
  1919. struct xhci_virt_ep *ep, int *status)
  1920. {
  1921. struct xhci_ring *ep_ring;
  1922. struct urb_priv *urb_priv;
  1923. int idx;
  1924. int len = 0;
  1925. union xhci_trb *cur_trb;
  1926. struct xhci_segment *cur_seg;
  1927. struct usb_iso_packet_descriptor *frame;
  1928. u32 trb_comp_code;
  1929. bool skip_td = false;
  1930. ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
  1931. trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
  1932. urb_priv = td->urb->hcpriv;
  1933. idx = urb_priv->td_cnt;
  1934. frame = &td->urb->iso_frame_desc[idx];
  1935. /* handle completion code */
  1936. switch (trb_comp_code) {
  1937. case COMP_SUCCESS:
  1938. if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) == 0) {
  1939. frame->status = 0;
  1940. break;
  1941. }
  1942. if ((xhci->quirks & XHCI_TRUST_TX_LENGTH))
  1943. trb_comp_code = COMP_SHORT_TX;
  1944. case COMP_SHORT_TX:
  1945. frame->status = td->urb->transfer_flags & URB_SHORT_NOT_OK ?
  1946. -EREMOTEIO : 0;
  1947. break;
  1948. case COMP_BW_OVER:
  1949. frame->status = -ECOMM;
  1950. skip_td = true;
  1951. break;
  1952. case COMP_BUFF_OVER:
  1953. case COMP_BABBLE:
  1954. frame->status = -EOVERFLOW;
  1955. skip_td = true;
  1956. break;
  1957. case COMP_DEV_ERR:
  1958. case COMP_STALL:
  1959. frame->status = -EPROTO;
  1960. skip_td = true;
  1961. break;
  1962. case COMP_TX_ERR:
  1963. frame->status = -EPROTO;
  1964. if (event_trb != td->last_trb)
  1965. return 0;
  1966. skip_td = true;
  1967. break;
  1968. case COMP_STOP:
  1969. case COMP_STOP_INVAL:
  1970. break;
  1971. default:
  1972. frame->status = -1;
  1973. break;
  1974. }
  1975. if (trb_comp_code == COMP_SUCCESS || skip_td) {
  1976. frame->actual_length = frame->length;
  1977. td->urb->actual_length += frame->length;
  1978. } else {
  1979. for (cur_trb = ep_ring->dequeue,
  1980. cur_seg = ep_ring->deq_seg; cur_trb != event_trb;
  1981. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  1982. if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) &&
  1983. !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3]))
  1984. len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
  1985. }
  1986. len += TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
  1987. EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
  1988. if (trb_comp_code != COMP_STOP_INVAL) {
  1989. frame->actual_length = len;
  1990. td->urb->actual_length += len;
  1991. }
  1992. }
  1993. return finish_td(xhci, td, event_trb, event, ep, status, false);
  1994. }
  1995. static int skip_isoc_td(struct xhci_hcd *xhci, struct xhci_td *td,
  1996. struct xhci_transfer_event *event,
  1997. struct xhci_virt_ep *ep, int *status)
  1998. {
  1999. struct xhci_ring *ep_ring;
  2000. struct urb_priv *urb_priv;
  2001. struct usb_iso_packet_descriptor *frame;
  2002. int idx;
  2003. ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
  2004. urb_priv = td->urb->hcpriv;
  2005. idx = urb_priv->td_cnt;
  2006. frame = &td->urb->iso_frame_desc[idx];
  2007. /* The transfer is partly done. */
  2008. frame->status = -EXDEV;
  2009. /* calc actual length */
  2010. frame->actual_length = 0;
  2011. /* Update ring dequeue pointer */
  2012. while (ep_ring->dequeue != td->last_trb)
  2013. inc_deq(xhci, ep_ring);
  2014. inc_deq(xhci, ep_ring);
  2015. return finish_td(xhci, td, NULL, event, ep, status, true);
  2016. }
  2017. /*
  2018. * Process bulk and interrupt tds, update urb status and actual_length.
  2019. */
  2020. static int process_bulk_intr_td(struct xhci_hcd *xhci, struct xhci_td *td,
  2021. union xhci_trb *event_trb, struct xhci_transfer_event *event,
  2022. struct xhci_virt_ep *ep, int *status)
  2023. {
  2024. struct xhci_ring *ep_ring;
  2025. union xhci_trb *cur_trb;
  2026. struct xhci_segment *cur_seg;
  2027. u32 trb_comp_code;
  2028. ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
  2029. trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
  2030. switch (trb_comp_code) {
  2031. case COMP_SUCCESS:
  2032. /* Double check that the HW transferred everything. */
  2033. if (event_trb != td->last_trb ||
  2034. EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
  2035. xhci_warn(xhci, "WARN Successful completion "
  2036. "on short TX\n");
  2037. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  2038. *status = -EREMOTEIO;
  2039. else
  2040. *status = 0;
  2041. if ((xhci->quirks & XHCI_TRUST_TX_LENGTH))
  2042. trb_comp_code = COMP_SHORT_TX;
  2043. } else {
  2044. *status = 0;
  2045. }
  2046. break;
  2047. case COMP_SHORT_TX:
  2048. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  2049. *status = -EREMOTEIO;
  2050. else
  2051. *status = 0;
  2052. break;
  2053. default:
  2054. /* Others already handled above */
  2055. break;
  2056. }
  2057. if (trb_comp_code == COMP_SHORT_TX)
  2058. xhci_dbg(xhci, "ep %#x - asked for %d bytes, "
  2059. "%d bytes untransferred\n",
  2060. td->urb->ep->desc.bEndpointAddress,
  2061. td->urb->transfer_buffer_length,
  2062. EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)));
  2063. /* Fast path - was this the last TRB in the TD for this URB? */
  2064. if (event_trb == td->last_trb) {
  2065. if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) != 0) {
  2066. td->urb->actual_length =
  2067. td->urb->transfer_buffer_length -
  2068. EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
  2069. if (td->urb->transfer_buffer_length <
  2070. td->urb->actual_length) {
  2071. xhci_warn(xhci, "HC gave bad length "
  2072. "of %d bytes left\n",
  2073. EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)));
  2074. td->urb->actual_length = 0;
  2075. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  2076. *status = -EREMOTEIO;
  2077. else
  2078. *status = 0;
  2079. }
  2080. /* Don't overwrite a previously set error code */
  2081. if (*status == -EINPROGRESS) {
  2082. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  2083. *status = -EREMOTEIO;
  2084. else
  2085. *status = 0;
  2086. }
  2087. } else {
  2088. td->urb->actual_length =
  2089. td->urb->transfer_buffer_length;
  2090. /* Ignore a short packet completion if the
  2091. * untransferred length was zero.
  2092. */
  2093. if (*status == -EREMOTEIO)
  2094. *status = 0;
  2095. }
  2096. } else {
  2097. /* Slow path - walk the list, starting from the dequeue
  2098. * pointer, to get the actual length transferred.
  2099. */
  2100. td->urb->actual_length = 0;
  2101. for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
  2102. cur_trb != event_trb;
  2103. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  2104. if (!TRB_TYPE_NOOP_LE32(cur_trb->generic.field[3]) &&
  2105. !TRB_TYPE_LINK_LE32(cur_trb->generic.field[3]))
  2106. td->urb->actual_length +=
  2107. TRB_LEN(le32_to_cpu(cur_trb->generic.field[2]));
  2108. }
  2109. /* If the ring didn't stop on a Link or No-op TRB, add
  2110. * in the actual bytes transferred from the Normal TRB
  2111. */
  2112. if (trb_comp_code != COMP_STOP_INVAL)
  2113. td->urb->actual_length +=
  2114. TRB_LEN(le32_to_cpu(cur_trb->generic.field[2])) -
  2115. EVENT_TRB_LEN(le32_to_cpu(event->transfer_len));
  2116. }
  2117. return finish_td(xhci, td, event_trb, event, ep, status, false);
  2118. }
  2119. /*
  2120. * If this function returns an error condition, it means it got a Transfer
  2121. * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
  2122. * At this point, the host controller is probably hosed and should be reset.
  2123. */
  2124. static int handle_tx_event(struct xhci_hcd *xhci,
  2125. struct xhci_transfer_event *event)
  2126. {
  2127. struct xhci_virt_device *xdev;
  2128. struct xhci_virt_ep *ep;
  2129. struct xhci_ring *ep_ring;
  2130. unsigned int slot_id;
  2131. int ep_index;
  2132. struct xhci_td *td = NULL;
  2133. dma_addr_t event_dma;
  2134. struct xhci_segment *event_seg;
  2135. union xhci_trb *event_trb;
  2136. struct urb *urb = NULL;
  2137. int status = -EINPROGRESS;
  2138. struct urb_priv *urb_priv;
  2139. struct xhci_ep_ctx *ep_ctx;
  2140. struct list_head *tmp;
  2141. u32 trb_comp_code;
  2142. int ret = 0;
  2143. int td_num = 0;
  2144. bool handling_skipped_tds = false;
  2145. slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->flags));
  2146. xdev = xhci->devs[slot_id];
  2147. if (!xdev) {
  2148. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  2149. xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
  2150. (unsigned long long) xhci_trb_virt_to_dma(
  2151. xhci->event_ring->deq_seg,
  2152. xhci->event_ring->dequeue),
  2153. lower_32_bits(le64_to_cpu(event->buffer)),
  2154. upper_32_bits(le64_to_cpu(event->buffer)),
  2155. le32_to_cpu(event->transfer_len),
  2156. le32_to_cpu(event->flags));
  2157. xhci_dbg(xhci, "Event ring:\n");
  2158. xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
  2159. return -ENODEV;
  2160. }
  2161. /* Endpoint ID is 1 based, our index is zero based */
  2162. ep_index = TRB_TO_EP_ID(le32_to_cpu(event->flags)) - 1;
  2163. ep = &xdev->eps[ep_index];
  2164. ep_ring = xhci_dma_to_transfer_ring(ep, le64_to_cpu(event->buffer));
  2165. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  2166. if (!ep_ring ||
  2167. (le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) ==
  2168. EP_STATE_DISABLED) {
  2169. xhci_err(xhci, "ERROR Transfer event for disabled endpoint "
  2170. "or incorrect stream ring\n");
  2171. xhci_err(xhci, "@%016llx %08x %08x %08x %08x\n",
  2172. (unsigned long long) xhci_trb_virt_to_dma(
  2173. xhci->event_ring->deq_seg,
  2174. xhci->event_ring->dequeue),
  2175. lower_32_bits(le64_to_cpu(event->buffer)),
  2176. upper_32_bits(le64_to_cpu(event->buffer)),
  2177. le32_to_cpu(event->transfer_len),
  2178. le32_to_cpu(event->flags));
  2179. xhci_dbg(xhci, "Event ring:\n");
  2180. xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
  2181. return -ENODEV;
  2182. }
  2183. /* Count current td numbers if ep->skip is set */
  2184. if (ep->skip) {
  2185. list_for_each(tmp, &ep_ring->td_list)
  2186. td_num++;
  2187. }
  2188. event_dma = le64_to_cpu(event->buffer);
  2189. trb_comp_code = GET_COMP_CODE(le32_to_cpu(event->transfer_len));
  2190. /* Look for common error cases */
  2191. switch (trb_comp_code) {
  2192. /* Skip codes that require special handling depending on
  2193. * transfer type
  2194. */
  2195. case COMP_SUCCESS:
  2196. if (EVENT_TRB_LEN(le32_to_cpu(event->transfer_len)) == 0)
  2197. break;
  2198. if (xhci->quirks & XHCI_TRUST_TX_LENGTH)
  2199. trb_comp_code = COMP_SHORT_TX;
  2200. else
  2201. xhci_warn(xhci, "WARN Successful completion on short TX: "
  2202. "needs XHCI_TRUST_TX_LENGTH quirk?\n");
  2203. case COMP_SHORT_TX:
  2204. break;
  2205. case COMP_STOP:
  2206. xhci_dbg(xhci, "Stopped on Transfer TRB\n");
  2207. break;
  2208. case COMP_STOP_INVAL:
  2209. xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
  2210. break;
  2211. case COMP_STALL:
  2212. xhci_dbg(xhci, "Stalled endpoint\n");
  2213. ep->ep_state |= EP_HALTED;
  2214. status = -EPIPE;
  2215. break;
  2216. case COMP_TRB_ERR:
  2217. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  2218. status = -EILSEQ;
  2219. break;
  2220. case COMP_SPLIT_ERR:
  2221. case COMP_TX_ERR:
  2222. xhci_dbg(xhci, "Transfer error on endpoint\n");
  2223. status = -EPROTO;
  2224. break;
  2225. case COMP_BABBLE:
  2226. xhci_dbg(xhci, "Babble error on endpoint\n");
  2227. status = -EOVERFLOW;
  2228. break;
  2229. case COMP_DB_ERR:
  2230. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  2231. status = -ENOSR;
  2232. break;
  2233. case COMP_BW_OVER:
  2234. xhci_warn(xhci, "WARN: bandwidth overrun event on endpoint\n");
  2235. break;
  2236. case COMP_BUFF_OVER:
  2237. xhci_warn(xhci, "WARN: buffer overrun event on endpoint\n");
  2238. break;
  2239. case COMP_UNDERRUN:
  2240. /*
  2241. * When the Isoch ring is empty, the xHC will generate
  2242. * a Ring Overrun Event for IN Isoch endpoint or Ring
  2243. * Underrun Event for OUT Isoch endpoint.
  2244. */
  2245. xhci_dbg(xhci, "underrun event on endpoint\n");
  2246. if (!list_empty(&ep_ring->td_list))
  2247. xhci_dbg(xhci, "Underrun Event for slot %d ep %d "
  2248. "still with TDs queued?\n",
  2249. TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
  2250. ep_index);
  2251. goto cleanup;
  2252. case COMP_OVERRUN:
  2253. xhci_dbg(xhci, "overrun event on endpoint\n");
  2254. if (!list_empty(&ep_ring->td_list))
  2255. xhci_dbg(xhci, "Overrun Event for slot %d ep %d "
  2256. "still with TDs queued?\n",
  2257. TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
  2258. ep_index);
  2259. goto cleanup;
  2260. case COMP_DEV_ERR:
  2261. xhci_warn(xhci, "WARN: detect an incompatible device");
  2262. status = -EPROTO;
  2263. break;
  2264. case COMP_MISSED_INT:
  2265. /*
  2266. * When encounter missed service error, one or more isoc tds
  2267. * may be missed by xHC.
  2268. * Set skip flag of the ep_ring; Complete the missed tds as
  2269. * short transfer when process the ep_ring next time.
  2270. */
  2271. ep->skip = true;
  2272. xhci_dbg(xhci, "Miss service interval error, set skip flag\n");
  2273. goto cleanup;
  2274. case COMP_PING_ERR:
  2275. ep->skip = true;
  2276. xhci_dbg(xhci, "No Ping response error, Skip one Isoc TD\n");
  2277. goto cleanup;
  2278. default:
  2279. if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
  2280. status = 0;
  2281. break;
  2282. }
  2283. xhci_warn(xhci, "ERROR Unknown event condition, HC probably "
  2284. "busted\n");
  2285. goto cleanup;
  2286. }
  2287. do {
  2288. /* This TRB should be in the TD at the head of this ring's
  2289. * TD list.
  2290. */
  2291. if (list_empty(&ep_ring->td_list)) {
  2292. /*
  2293. * A stopped endpoint may generate an extra completion
  2294. * event if the device was suspended. Don't print
  2295. * warnings.
  2296. */
  2297. if (!(trb_comp_code == COMP_STOP ||
  2298. trb_comp_code == COMP_STOP_INVAL)) {
  2299. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  2300. TRB_TO_SLOT_ID(le32_to_cpu(event->flags)),
  2301. ep_index);
  2302. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  2303. (le32_to_cpu(event->flags) &
  2304. TRB_TYPE_BITMASK)>>10);
  2305. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  2306. }
  2307. if (ep->skip) {
  2308. ep->skip = false;
  2309. xhci_dbg(xhci, "td_list is empty while skip "
  2310. "flag set. Clear skip flag.\n");
  2311. }
  2312. ret = 0;
  2313. goto cleanup;
  2314. }
  2315. /* We've skipped all the TDs on the ep ring when ep->skip set */
  2316. if (ep->skip && td_num == 0) {
  2317. ep->skip = false;
  2318. xhci_dbg(xhci, "All tds on the ep_ring skipped. "
  2319. "Clear skip flag.\n");
  2320. ret = 0;
  2321. goto cleanup;
  2322. }
  2323. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  2324. if (ep->skip)
  2325. td_num--;
  2326. /* Is this a TRB in the currently executing TD? */
  2327. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  2328. td->last_trb, event_dma);
  2329. /*
  2330. * Skip the Force Stopped Event. The event_trb(event_dma) of FSE
  2331. * is not in the current TD pointed by ep_ring->dequeue because
  2332. * that the hardware dequeue pointer still at the previous TRB
  2333. * of the current TD. The previous TRB maybe a Link TD or the
  2334. * last TRB of the previous TD. The command completion handle
  2335. * will take care the rest.
  2336. */
  2337. if (!event_seg && (trb_comp_code == COMP_STOP ||
  2338. trb_comp_code == COMP_STOP_INVAL)) {
  2339. ret = 0;
  2340. goto cleanup;
  2341. }
  2342. if (!event_seg) {
  2343. if (!ep->skip ||
  2344. !usb_endpoint_xfer_isoc(&td->urb->ep->desc)) {
  2345. /* Some host controllers give a spurious
  2346. * successful event after a short transfer.
  2347. * Ignore it.
  2348. */
  2349. if ((xhci->quirks & XHCI_SPURIOUS_SUCCESS) &&
  2350. ep_ring->last_td_was_short) {
  2351. ep_ring->last_td_was_short = false;
  2352. ret = 0;
  2353. goto cleanup;
  2354. }
  2355. /* HC is busted, give up! */
  2356. xhci_err(xhci,
  2357. "ERROR Transfer event TRB DMA ptr not "
  2358. "part of current TD\n");
  2359. return -ESHUTDOWN;
  2360. }
  2361. ret = skip_isoc_td(xhci, td, event, ep, &status);
  2362. goto cleanup;
  2363. }
  2364. if (trb_comp_code == COMP_SHORT_TX)
  2365. ep_ring->last_td_was_short = true;
  2366. else
  2367. ep_ring->last_td_was_short = false;
  2368. if (ep->skip) {
  2369. xhci_dbg(xhci, "Found td. Clear skip flag.\n");
  2370. ep->skip = false;
  2371. }
  2372. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) /
  2373. sizeof(*event_trb)];
  2374. /*
  2375. * No-op TRB should not trigger interrupts.
  2376. * If event_trb is a no-op TRB, it means the
  2377. * corresponding TD has been cancelled. Just ignore
  2378. * the TD.
  2379. */
  2380. if (TRB_TYPE_NOOP_LE32(event_trb->generic.field[3])) {
  2381. xhci_dbg(xhci,
  2382. "event_trb is a no-op TRB. Skip it\n");
  2383. goto cleanup;
  2384. }
  2385. /* Now update the urb's actual_length and give back to
  2386. * the core
  2387. */
  2388. if (usb_endpoint_xfer_control(&td->urb->ep->desc))
  2389. ret = process_ctrl_td(xhci, td, event_trb, event, ep,
  2390. &status);
  2391. else if (usb_endpoint_xfer_isoc(&td->urb->ep->desc))
  2392. ret = process_isoc_td(xhci, td, event_trb, event, ep,
  2393. &status);
  2394. else
  2395. ret = process_bulk_intr_td(xhci, td, event_trb, event,
  2396. ep, &status);
  2397. cleanup:
  2398. handling_skipped_tds = ep->skip &&
  2399. trb_comp_code != COMP_MISSED_INT &&
  2400. trb_comp_code != COMP_PING_ERR;
  2401. /*
  2402. * Do not update event ring dequeue pointer if we're in a loop
  2403. * processing missed tds.
  2404. */
  2405. if (!handling_skipped_tds)
  2406. inc_deq(xhci, xhci->event_ring);
  2407. if (ret) {
  2408. urb = td->urb;
  2409. urb_priv = urb->hcpriv;
  2410. xhci_urb_free_priv(xhci, urb_priv);
  2411. usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
  2412. if ((urb->actual_length != urb->transfer_buffer_length &&
  2413. (urb->transfer_flags &
  2414. URB_SHORT_NOT_OK)) ||
  2415. (status != 0 &&
  2416. !usb_endpoint_xfer_isoc(&urb->ep->desc)))
  2417. xhci_dbg(xhci, "Giveback URB %pK, len = %d, "
  2418. "expected = %x, status = %d\n",
  2419. urb, urb->actual_length,
  2420. urb->transfer_buffer_length,
  2421. status);
  2422. spin_unlock(&xhci->lock);
  2423. /* EHCI, UHCI, and OHCI always unconditionally set the
  2424. * urb->status of an isochronous endpoint to 0.
  2425. */
  2426. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  2427. status = 0;
  2428. usb_hcd_giveback_urb(bus_to_hcd(urb->dev->bus), urb, status);
  2429. spin_lock(&xhci->lock);
  2430. }
  2431. /*
  2432. * If ep->skip is set, it means there are missed tds on the
  2433. * endpoint ring need to take care of.
  2434. * Process them as short transfer until reach the td pointed by
  2435. * the event.
  2436. */
  2437. } while (handling_skipped_tds);
  2438. return 0;
  2439. }
  2440. /*
  2441. * This function handles all OS-owned events on the event ring. It may drop
  2442. * xhci->lock between event processing (e.g. to pass up port status changes).
  2443. * Returns >0 for "possibly more events to process" (caller should call again),
  2444. * otherwise 0 if done. In future, <0 returns should indicate error code.
  2445. */
  2446. static int xhci_handle_event(struct xhci_hcd *xhci)
  2447. {
  2448. union xhci_trb *event;
  2449. int update_ptrs = 1;
  2450. int ret;
  2451. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  2452. xhci->error_bitmask |= 1 << 1;
  2453. return 0;
  2454. }
  2455. event = xhci->event_ring->dequeue;
  2456. /* Does the HC or OS own the TRB? */
  2457. if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
  2458. xhci->event_ring->cycle_state) {
  2459. xhci->error_bitmask |= 1 << 2;
  2460. return 0;
  2461. }
  2462. /*
  2463. * Barrier between reading the TRB_CYCLE (valid) flag above and any
  2464. * speculative reads of the event's flags/data below.
  2465. */
  2466. rmb();
  2467. /* FIXME: Handle more event types. */
  2468. switch ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK)) {
  2469. case TRB_TYPE(TRB_COMPLETION):
  2470. handle_cmd_completion(xhci, &event->event_cmd);
  2471. break;
  2472. case TRB_TYPE(TRB_PORT_STATUS):
  2473. handle_port_status(xhci, event);
  2474. update_ptrs = 0;
  2475. break;
  2476. case TRB_TYPE(TRB_TRANSFER):
  2477. ret = handle_tx_event(xhci, &event->trans_event);
  2478. if (ret < 0)
  2479. xhci->error_bitmask |= 1 << 9;
  2480. else
  2481. update_ptrs = 0;
  2482. break;
  2483. case TRB_TYPE(TRB_DEV_NOTE):
  2484. handle_device_notification(xhci, event);
  2485. break;
  2486. default:
  2487. if ((le32_to_cpu(event->event_cmd.flags) & TRB_TYPE_BITMASK) >=
  2488. TRB_TYPE(48))
  2489. handle_vendor_event(xhci, event);
  2490. else
  2491. xhci->error_bitmask |= 1 << 3;
  2492. }
  2493. /* Any of the above functions may drop and re-acquire the lock, so check
  2494. * to make sure a watchdog timer didn't mark the host as non-responsive.
  2495. */
  2496. if (xhci->xhc_state & XHCI_STATE_DYING) {
  2497. xhci_dbg(xhci, "xHCI host dying, returning from "
  2498. "event handler.\n");
  2499. return 0;
  2500. }
  2501. if (update_ptrs)
  2502. /* Update SW event ring dequeue pointer */
  2503. inc_deq(xhci, xhci->event_ring);
  2504. /* Are there more items on the event ring? Caller will call us again to
  2505. * check.
  2506. */
  2507. return 1;
  2508. }
  2509. /*
  2510. * xHCI spec says we can get an interrupt, and if the HC has an error condition,
  2511. * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
  2512. * indicators of an event TRB error, but we check the status *first* to be safe.
  2513. */
  2514. irqreturn_t xhci_irq(struct usb_hcd *hcd)
  2515. {
  2516. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2517. u32 status;
  2518. union xhci_trb *trb;
  2519. u64 temp_64;
  2520. union xhci_trb *event_ring_deq;
  2521. dma_addr_t deq;
  2522. spin_lock(&xhci->lock);
  2523. trb = xhci->event_ring->dequeue;
  2524. /* Check if the xHC generated the interrupt, or the irq is shared */
  2525. status = xhci_readl(xhci, &xhci->op_regs->status);
  2526. if (status == 0xffffffff)
  2527. goto hw_died;
  2528. if (!(status & STS_EINT)) {
  2529. spin_unlock(&xhci->lock);
  2530. return IRQ_NONE;
  2531. }
  2532. if (status & STS_FATAL) {
  2533. xhci_warn(xhci, "WARNING: Host System Error\n");
  2534. xhci_halt(xhci);
  2535. hw_died:
  2536. spin_unlock(&xhci->lock);
  2537. return IRQ_HANDLED;
  2538. }
  2539. /*
  2540. * Clear the op reg interrupt status first,
  2541. * so we can receive interrupts from other MSI-X interrupters.
  2542. * Write 1 to clear the interrupt status.
  2543. */
  2544. status |= STS_EINT;
  2545. xhci_writel(xhci, status, &xhci->op_regs->status);
  2546. /* FIXME when MSI-X is supported and there are multiple vectors */
  2547. /* Clear the MSI-X event interrupt status */
  2548. if (hcd->irq) {
  2549. u32 irq_pending;
  2550. /* Acknowledge the PCI interrupt */
  2551. irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  2552. irq_pending |= IMAN_IP;
  2553. xhci_writel(xhci, irq_pending, &xhci->ir_set->irq_pending);
  2554. }
  2555. if (xhci->xhc_state & XHCI_STATE_DYING) {
  2556. xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
  2557. "Shouldn't IRQs be disabled?\n");
  2558. /* Clear the event handler busy flag (RW1C);
  2559. * the event ring should be empty.
  2560. */
  2561. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  2562. xhci_write_64(xhci, temp_64 | ERST_EHB,
  2563. &xhci->ir_set->erst_dequeue);
  2564. spin_unlock(&xhci->lock);
  2565. return IRQ_HANDLED;
  2566. }
  2567. event_ring_deq = xhci->event_ring->dequeue;
  2568. /* FIXME this should be a delayed service routine
  2569. * that clears the EHB.
  2570. */
  2571. while (xhci_handle_event(xhci) > 0) {}
  2572. temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  2573. /* If necessary, update the HW's version of the event ring deq ptr. */
  2574. if (event_ring_deq != xhci->event_ring->dequeue) {
  2575. deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
  2576. xhci->event_ring->dequeue);
  2577. if (deq == 0)
  2578. xhci_warn(xhci, "WARN something wrong with SW event "
  2579. "ring dequeue ptr.\n");
  2580. /* Update HC event ring dequeue pointer */
  2581. temp_64 &= ERST_PTR_MASK;
  2582. temp_64 |= ((u64) deq & (u64) ~ERST_PTR_MASK);
  2583. }
  2584. /* Clear the event handler busy flag (RW1C); event ring is empty. */
  2585. temp_64 |= ERST_EHB;
  2586. xhci_write_64(xhci, temp_64, &xhci->ir_set->erst_dequeue);
  2587. spin_unlock(&xhci->lock);
  2588. return IRQ_HANDLED;
  2589. }
  2590. irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
  2591. {
  2592. return xhci_irq(hcd);
  2593. }
  2594. /**** Endpoint Ring Operations ****/
  2595. /*
  2596. * Generic function for queueing a TRB on a ring.
  2597. * The caller must have checked to make sure there's room on the ring.
  2598. *
  2599. * @more_trbs_coming: Will you enqueue more TRBs before calling
  2600. * prepare_transfer()?
  2601. */
  2602. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  2603. bool more_trbs_coming,
  2604. u32 field1, u32 field2, u32 field3, u32 field4)
  2605. {
  2606. struct xhci_generic_trb *trb;
  2607. trb = &ring->enqueue->generic;
  2608. trb->field[0] = cpu_to_le32(field1);
  2609. trb->field[1] = cpu_to_le32(field2);
  2610. trb->field[2] = cpu_to_le32(field3);
  2611. trb->field[3] = cpu_to_le32(field4);
  2612. inc_enq(xhci, ring, more_trbs_coming);
  2613. }
  2614. /*
  2615. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  2616. * FIXME allocate segments if the ring is full.
  2617. */
  2618. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  2619. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  2620. {
  2621. unsigned int num_trbs_needed;
  2622. /* Make sure the endpoint has been added to xHC schedule */
  2623. switch (ep_state) {
  2624. case EP_STATE_DISABLED:
  2625. /*
  2626. * USB core changed config/interfaces without notifying us,
  2627. * or hardware is reporting the wrong state.
  2628. */
  2629. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  2630. return -ENOENT;
  2631. case EP_STATE_ERROR:
  2632. xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
  2633. /* FIXME event handling code for error needs to clear it */
  2634. /* XXX not sure if this should be -ENOENT or not */
  2635. return -EINVAL;
  2636. case EP_STATE_HALTED:
  2637. xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
  2638. case EP_STATE_STOPPED:
  2639. case EP_STATE_RUNNING:
  2640. break;
  2641. default:
  2642. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  2643. /*
  2644. * FIXME issue Configure Endpoint command to try to get the HC
  2645. * back into a known state.
  2646. */
  2647. return -EINVAL;
  2648. }
  2649. while (1) {
  2650. if (room_on_ring(xhci, ep_ring, num_trbs))
  2651. break;
  2652. if (ep_ring == xhci->cmd_ring) {
  2653. xhci_err(xhci, "Do not support expand command ring\n");
  2654. return -ENOMEM;
  2655. }
  2656. xhci_dbg(xhci, "ERROR no room on ep ring, "
  2657. "try ring expansion\n");
  2658. num_trbs_needed = num_trbs - ep_ring->num_trbs_free;
  2659. if (xhci_ring_expansion(xhci, ep_ring, num_trbs_needed,
  2660. mem_flags)) {
  2661. xhci_err(xhci, "Ring expansion failed\n");
  2662. return -ENOMEM;
  2663. }
  2664. };
  2665. if (enqueue_is_link_trb(ep_ring)) {
  2666. struct xhci_ring *ring = ep_ring;
  2667. union xhci_trb *next;
  2668. next = ring->enqueue;
  2669. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  2670. /* If we're not dealing with 0.95 hardware or isoc rings
  2671. * on AMD 0.96 host, clear the chain bit.
  2672. */
  2673. if (!xhci_link_trb_quirk(xhci) &&
  2674. !(ring->type == TYPE_ISOC &&
  2675. (xhci->quirks & XHCI_AMD_0x96_HOST)))
  2676. next->link.control &= cpu_to_le32(~TRB_CHAIN);
  2677. else
  2678. next->link.control |= cpu_to_le32(TRB_CHAIN);
  2679. wmb();
  2680. next->link.control ^= cpu_to_le32(TRB_CYCLE);
  2681. /* Toggle the cycle bit after the last ring segment. */
  2682. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  2683. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  2684. }
  2685. ring->enq_seg = ring->enq_seg->next;
  2686. ring->enqueue = ring->enq_seg->trbs;
  2687. next = ring->enqueue;
  2688. }
  2689. }
  2690. return 0;
  2691. }
  2692. static int prepare_transfer(struct xhci_hcd *xhci,
  2693. struct xhci_virt_device *xdev,
  2694. unsigned int ep_index,
  2695. unsigned int stream_id,
  2696. unsigned int num_trbs,
  2697. struct urb *urb,
  2698. unsigned int td_index,
  2699. gfp_t mem_flags)
  2700. {
  2701. int ret;
  2702. struct urb_priv *urb_priv;
  2703. struct xhci_td *td;
  2704. struct xhci_ring *ep_ring;
  2705. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  2706. ep_ring = xhci_stream_id_to_ring(xdev, ep_index, stream_id);
  2707. if (!ep_ring) {
  2708. xhci_dbg(xhci, "Can't prepare ring for bad stream ID %u\n",
  2709. stream_id);
  2710. return -EINVAL;
  2711. }
  2712. ret = prepare_ring(xhci, ep_ring,
  2713. le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
  2714. num_trbs, mem_flags);
  2715. if (ret)
  2716. return ret;
  2717. urb_priv = urb->hcpriv;
  2718. td = urb_priv->td[td_index];
  2719. INIT_LIST_HEAD(&td->td_list);
  2720. INIT_LIST_HEAD(&td->cancelled_td_list);
  2721. if (td_index == 0) {
  2722. ret = usb_hcd_link_urb_to_ep(bus_to_hcd(urb->dev->bus), urb);
  2723. if (unlikely(ret))
  2724. return ret;
  2725. }
  2726. td->urb = urb;
  2727. /* Add this TD to the tail of the endpoint ring's TD list */
  2728. list_add_tail(&td->td_list, &ep_ring->td_list);
  2729. td->start_seg = ep_ring->enq_seg;
  2730. td->first_trb = ep_ring->enqueue;
  2731. urb_priv->td[td_index] = td;
  2732. return 0;
  2733. }
  2734. static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
  2735. {
  2736. int num_sgs, num_trbs, running_total, temp, i;
  2737. struct scatterlist *sg;
  2738. sg = NULL;
  2739. num_sgs = urb->num_mapped_sgs;
  2740. temp = urb->transfer_buffer_length;
  2741. num_trbs = 0;
  2742. for_each_sg(urb->sg, sg, num_sgs, i) {
  2743. unsigned int len = sg_dma_len(sg);
  2744. /* Scatter gather list entries may cross 64KB boundaries */
  2745. running_total = TRB_MAX_BUFF_SIZE -
  2746. (sg_dma_address(sg) & (TRB_MAX_BUFF_SIZE - 1));
  2747. running_total &= TRB_MAX_BUFF_SIZE - 1;
  2748. if (running_total != 0)
  2749. num_trbs++;
  2750. /* How many more 64KB chunks to transfer, how many more TRBs? */
  2751. while (running_total < sg_dma_len(sg) && running_total < temp) {
  2752. num_trbs++;
  2753. running_total += TRB_MAX_BUFF_SIZE;
  2754. }
  2755. len = min_t(int, len, temp);
  2756. temp -= len;
  2757. if (temp == 0)
  2758. break;
  2759. }
  2760. return num_trbs;
  2761. }
  2762. static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
  2763. {
  2764. if (num_trbs != 0)
  2765. dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  2766. "TRBs, %d left\n", __func__,
  2767. urb->ep->desc.bEndpointAddress, num_trbs);
  2768. if (running_total != urb->transfer_buffer_length)
  2769. dev_err(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
  2770. "queued %#x (%d), asked for %#x (%d)\n",
  2771. __func__,
  2772. urb->ep->desc.bEndpointAddress,
  2773. running_total, running_total,
  2774. urb->transfer_buffer_length,
  2775. urb->transfer_buffer_length);
  2776. }
  2777. static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
  2778. unsigned int ep_index, unsigned int stream_id, int start_cycle,
  2779. struct xhci_generic_trb *start_trb)
  2780. {
  2781. /*
  2782. * Pass all the TRBs to the hardware at once and make sure this write
  2783. * isn't reordered.
  2784. */
  2785. wmb();
  2786. if (start_cycle)
  2787. start_trb->field[3] |= cpu_to_le32(start_cycle);
  2788. else
  2789. start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
  2790. xhci_ring_ep_doorbell(xhci, slot_id, ep_index, stream_id);
  2791. }
  2792. /*
  2793. * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
  2794. * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
  2795. * (comprised of sg list entries) can take several service intervals to
  2796. * transmit.
  2797. */
  2798. int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  2799. struct urb *urb, int slot_id, unsigned int ep_index)
  2800. {
  2801. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
  2802. xhci->devs[slot_id]->out_ctx, ep_index);
  2803. int xhci_interval;
  2804. int ep_interval;
  2805. xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
  2806. ep_interval = urb->interval;
  2807. /* Convert to microframes */
  2808. if (urb->dev->speed == USB_SPEED_LOW ||
  2809. urb->dev->speed == USB_SPEED_FULL)
  2810. ep_interval *= 8;
  2811. /* FIXME change this to a warning and a suggestion to use the new API
  2812. * to set the polling interval (once the API is added).
  2813. */
  2814. if (xhci_interval != ep_interval) {
  2815. if (printk_ratelimit())
  2816. dev_dbg(&urb->dev->dev, "Driver uses different interval"
  2817. " (%d microframe%s) than xHCI "
  2818. "(%d microframe%s)\n",
  2819. ep_interval,
  2820. ep_interval == 1 ? "" : "s",
  2821. xhci_interval,
  2822. xhci_interval == 1 ? "" : "s");
  2823. urb->interval = xhci_interval;
  2824. /* Convert back to frames for LS/FS devices */
  2825. if (urb->dev->speed == USB_SPEED_LOW ||
  2826. urb->dev->speed == USB_SPEED_FULL)
  2827. urb->interval /= 8;
  2828. }
  2829. return xhci_queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index);
  2830. }
  2831. /*
  2832. * The TD size is the number of bytes remaining in the TD (including this TRB),
  2833. * right shifted by 10.
  2834. * It must fit in bits 21:17, so it can't be bigger than 31.
  2835. */
  2836. static u32 xhci_td_remainder(unsigned int remainder)
  2837. {
  2838. u32 max = (1 << (21 - 17 + 1)) - 1;
  2839. if ((remainder >> 10) >= max)
  2840. return max << 17;
  2841. else
  2842. return (remainder >> 10) << 17;
  2843. }
  2844. /*
  2845. * For xHCI 1.0 host controllers, TD size is the number of max packet sized
  2846. * packets remaining in the TD (*not* including this TRB).
  2847. *
  2848. * Total TD packet count = total_packet_count =
  2849. * DIV_ROUND_UP(TD size in bytes / wMaxPacketSize)
  2850. *
  2851. * Packets transferred up to and including this TRB = packets_transferred =
  2852. * rounddown(total bytes transferred including this TRB / wMaxPacketSize)
  2853. *
  2854. * TD size = total_packet_count - packets_transferred
  2855. *
  2856. * It must fit in bits 21:17, so it can't be bigger than 31.
  2857. * The last TRB in a TD must have the TD size set to zero.
  2858. */
  2859. static u32 xhci_v1_0_td_remainder(int running_total, int trb_buff_len,
  2860. unsigned int total_packet_count, struct urb *urb,
  2861. unsigned int num_trbs_left)
  2862. {
  2863. int packets_transferred;
  2864. /* One TRB with a zero-length data packet. */
  2865. if (num_trbs_left == 0 || (running_total == 0 && trb_buff_len == 0))
  2866. return 0;
  2867. /* All the TRB queueing functions don't count the current TRB in
  2868. * running_total.
  2869. */
  2870. packets_transferred = (running_total + trb_buff_len) /
  2871. GET_MAX_PACKET(usb_endpoint_maxp(&urb->ep->desc));
  2872. if ((total_packet_count - packets_transferred) > 31)
  2873. return 31 << 17;
  2874. return (total_packet_count - packets_transferred) << 17;
  2875. }
  2876. static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  2877. struct urb *urb, int slot_id, unsigned int ep_index)
  2878. {
  2879. struct xhci_ring *ep_ring;
  2880. unsigned int num_trbs;
  2881. struct urb_priv *urb_priv;
  2882. struct xhci_td *td;
  2883. struct scatterlist *sg;
  2884. int num_sgs;
  2885. int trb_buff_len, this_sg_len, running_total;
  2886. unsigned int total_packet_count;
  2887. bool first_trb;
  2888. u64 addr;
  2889. bool more_trbs_coming;
  2890. struct xhci_generic_trb *start_trb;
  2891. int start_cycle;
  2892. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  2893. if (!ep_ring)
  2894. return -EINVAL;
  2895. num_trbs = count_sg_trbs_needed(xhci, urb);
  2896. num_sgs = urb->num_mapped_sgs;
  2897. total_packet_count = DIV_ROUND_UP(urb->transfer_buffer_length,
  2898. usb_endpoint_maxp(&urb->ep->desc));
  2899. trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
  2900. ep_index, urb->stream_id,
  2901. num_trbs, urb, 0, mem_flags);
  2902. if (trb_buff_len < 0)
  2903. return trb_buff_len;
  2904. urb_priv = urb->hcpriv;
  2905. td = urb_priv->td[0];
  2906. /*
  2907. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  2908. * until we've finished creating all the other TRBs. The ring's cycle
  2909. * state may change as we enqueue the other TRBs, so save it too.
  2910. */
  2911. start_trb = &ep_ring->enqueue->generic;
  2912. start_cycle = ep_ring->cycle_state;
  2913. running_total = 0;
  2914. /*
  2915. * How much data is in the first TRB?
  2916. *
  2917. * There are three forces at work for TRB buffer pointers and lengths:
  2918. * 1. We don't want to walk off the end of this sg-list entry buffer.
  2919. * 2. The transfer length that the driver requested may be smaller than
  2920. * the amount of memory allocated for this scatter-gather list.
  2921. * 3. TRBs buffers can't cross 64KB boundaries.
  2922. */
  2923. sg = urb->sg;
  2924. addr = (u64) sg_dma_address(sg);
  2925. this_sg_len = sg_dma_len(sg);
  2926. trb_buff_len = TRB_MAX_BUFF_SIZE - (addr & (TRB_MAX_BUFF_SIZE - 1));
  2927. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  2928. if (trb_buff_len > urb->transfer_buffer_length)
  2929. trb_buff_len = urb->transfer_buffer_length;
  2930. first_trb = true;
  2931. /* Queue the first TRB, even if it's zero-length */
  2932. do {
  2933. u32 field = 0;
  2934. u32 length_field = 0;
  2935. u32 remainder = 0;
  2936. /* Don't change the cycle bit of the first TRB until later */
  2937. if (first_trb) {
  2938. first_trb = false;
  2939. if (start_cycle == 0)
  2940. field |= 0x1;
  2941. } else
  2942. field |= ep_ring->cycle_state;
  2943. /* Chain all the TRBs together; clear the chain bit in the last
  2944. * TRB to indicate it's the last TRB in the chain.
  2945. */
  2946. if (num_trbs > 1) {
  2947. field |= TRB_CHAIN;
  2948. } else {
  2949. /* FIXME - add check for ZERO_PACKET flag before this */
  2950. td->last_trb = ep_ring->enqueue;
  2951. field |= TRB_IOC;
  2952. }
  2953. /* Only set interrupt on short packet for IN endpoints */
  2954. if (usb_urb_dir_in(urb))
  2955. field |= TRB_ISP;
  2956. if (TRB_MAX_BUFF_SIZE -
  2957. (addr & (TRB_MAX_BUFF_SIZE - 1)) < trb_buff_len) {
  2958. xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
  2959. xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
  2960. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  2961. (unsigned int) addr + trb_buff_len);
  2962. }
  2963. /* Set the TRB length, TD size, and interrupter fields. */
  2964. if (xhci->hci_version < 0x100) {
  2965. remainder = xhci_td_remainder(
  2966. urb->transfer_buffer_length -
  2967. running_total);
  2968. } else {
  2969. remainder = xhci_v1_0_td_remainder(running_total,
  2970. trb_buff_len, total_packet_count, urb,
  2971. num_trbs - 1);
  2972. }
  2973. length_field = TRB_LEN(trb_buff_len) |
  2974. remainder |
  2975. TRB_INTR_TARGET(0);
  2976. if (num_trbs > 1)
  2977. more_trbs_coming = true;
  2978. else
  2979. more_trbs_coming = false;
  2980. queue_trb(xhci, ep_ring, more_trbs_coming,
  2981. lower_32_bits(addr),
  2982. upper_32_bits(addr),
  2983. length_field,
  2984. field | TRB_TYPE(TRB_NORMAL));
  2985. --num_trbs;
  2986. running_total += trb_buff_len;
  2987. /* Calculate length for next transfer --
  2988. * Are we done queueing all the TRBs for this sg entry?
  2989. */
  2990. this_sg_len -= trb_buff_len;
  2991. if (this_sg_len == 0) {
  2992. --num_sgs;
  2993. if (num_sgs == 0)
  2994. break;
  2995. sg = sg_next(sg);
  2996. addr = (u64) sg_dma_address(sg);
  2997. this_sg_len = sg_dma_len(sg);
  2998. } else {
  2999. addr += trb_buff_len;
  3000. }
  3001. trb_buff_len = TRB_MAX_BUFF_SIZE -
  3002. (addr & (TRB_MAX_BUFF_SIZE - 1));
  3003. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  3004. if (running_total + trb_buff_len > urb->transfer_buffer_length)
  3005. trb_buff_len =
  3006. urb->transfer_buffer_length - running_total;
  3007. } while (running_total < urb->transfer_buffer_length);
  3008. check_trb_math(urb, num_trbs, running_total);
  3009. giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
  3010. start_cycle, start_trb);
  3011. return 0;
  3012. }
  3013. /* This is very similar to what ehci-q.c qtd_fill() does */
  3014. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  3015. struct urb *urb, int slot_id, unsigned int ep_index)
  3016. {
  3017. struct xhci_ring *ep_ring;
  3018. struct urb_priv *urb_priv;
  3019. struct xhci_td *td;
  3020. int num_trbs;
  3021. struct xhci_generic_trb *start_trb;
  3022. bool first_trb;
  3023. bool more_trbs_coming;
  3024. int start_cycle;
  3025. u32 field, length_field;
  3026. int running_total, trb_buff_len, ret;
  3027. unsigned int total_packet_count;
  3028. u64 addr;
  3029. if (urb->num_sgs)
  3030. return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
  3031. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  3032. if (!ep_ring)
  3033. return -EINVAL;
  3034. num_trbs = 0;
  3035. /* How much data is (potentially) left before the 64KB boundary? */
  3036. running_total = TRB_MAX_BUFF_SIZE -
  3037. (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1));
  3038. running_total &= TRB_MAX_BUFF_SIZE - 1;
  3039. /* If there's some data on this 64KB chunk, or we have to send a
  3040. * zero-length transfer, we need at least one TRB
  3041. */
  3042. if (running_total != 0 || urb->transfer_buffer_length == 0)
  3043. num_trbs++;
  3044. /* How many more 64KB chunks to transfer, how many more TRBs? */
  3045. while (running_total < urb->transfer_buffer_length) {
  3046. num_trbs++;
  3047. running_total += TRB_MAX_BUFF_SIZE;
  3048. }
  3049. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  3050. ret = prepare_transfer(xhci, xhci->devs[slot_id],
  3051. ep_index, urb->stream_id,
  3052. num_trbs, urb, 0, mem_flags);
  3053. if (ret < 0)
  3054. return ret;
  3055. urb_priv = urb->hcpriv;
  3056. td = urb_priv->td[0];
  3057. /*
  3058. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  3059. * until we've finished creating all the other TRBs. The ring's cycle
  3060. * state may change as we enqueue the other TRBs, so save it too.
  3061. */
  3062. start_trb = &ep_ring->enqueue->generic;
  3063. start_cycle = ep_ring->cycle_state;
  3064. running_total = 0;
  3065. total_packet_count = DIV_ROUND_UP(urb->transfer_buffer_length,
  3066. usb_endpoint_maxp(&urb->ep->desc));
  3067. /* How much data is in the first TRB? */
  3068. addr = (u64) urb->transfer_dma;
  3069. trb_buff_len = TRB_MAX_BUFF_SIZE -
  3070. (urb->transfer_dma & (TRB_MAX_BUFF_SIZE - 1));
  3071. if (trb_buff_len > urb->transfer_buffer_length)
  3072. trb_buff_len = urb->transfer_buffer_length;
  3073. first_trb = true;
  3074. /* Queue the first TRB, even if it's zero-length */
  3075. do {
  3076. u32 remainder = 0;
  3077. field = 0;
  3078. /* Don't change the cycle bit of the first TRB until later */
  3079. if (first_trb) {
  3080. first_trb = false;
  3081. if (start_cycle == 0)
  3082. field |= 0x1;
  3083. } else
  3084. field |= ep_ring->cycle_state;
  3085. /* Chain all the TRBs together; clear the chain bit in the last
  3086. * TRB to indicate it's the last TRB in the chain.
  3087. */
  3088. if (num_trbs > 1) {
  3089. field |= TRB_CHAIN;
  3090. } else {
  3091. /* FIXME - add check for ZERO_PACKET flag before this */
  3092. td->last_trb = ep_ring->enqueue;
  3093. field |= TRB_IOC;
  3094. }
  3095. /* Only set interrupt on short packet for IN endpoints */
  3096. if (usb_urb_dir_in(urb))
  3097. field |= TRB_ISP;
  3098. /* Set the TRB length, TD size, and interrupter fields. */
  3099. if (xhci->hci_version < 0x100) {
  3100. remainder = xhci_td_remainder(
  3101. urb->transfer_buffer_length -
  3102. running_total);
  3103. } else {
  3104. remainder = xhci_v1_0_td_remainder(running_total,
  3105. trb_buff_len, total_packet_count, urb,
  3106. num_trbs - 1);
  3107. }
  3108. length_field = TRB_LEN(trb_buff_len) |
  3109. remainder |
  3110. TRB_INTR_TARGET(0);
  3111. if (num_trbs > 1)
  3112. more_trbs_coming = true;
  3113. else
  3114. more_trbs_coming = false;
  3115. queue_trb(xhci, ep_ring, more_trbs_coming,
  3116. lower_32_bits(addr),
  3117. upper_32_bits(addr),
  3118. length_field,
  3119. field | TRB_TYPE(TRB_NORMAL));
  3120. --num_trbs;
  3121. running_total += trb_buff_len;
  3122. /* Calculate length for next transfer */
  3123. addr += trb_buff_len;
  3124. trb_buff_len = urb->transfer_buffer_length - running_total;
  3125. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  3126. trb_buff_len = TRB_MAX_BUFF_SIZE;
  3127. } while (running_total < urb->transfer_buffer_length);
  3128. check_trb_math(urb, num_trbs, running_total);
  3129. giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
  3130. start_cycle, start_trb);
  3131. return 0;
  3132. }
  3133. /* Caller must have locked xhci->lock */
  3134. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  3135. struct urb *urb, int slot_id, unsigned int ep_index)
  3136. {
  3137. struct xhci_ring *ep_ring;
  3138. int num_trbs;
  3139. int ret;
  3140. struct usb_ctrlrequest *setup;
  3141. struct xhci_generic_trb *start_trb;
  3142. int start_cycle;
  3143. u32 field, length_field;
  3144. struct urb_priv *urb_priv;
  3145. struct xhci_td *td;
  3146. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  3147. if (!ep_ring)
  3148. return -EINVAL;
  3149. /*
  3150. * Need to copy setup packet into setup TRB, so we can't use the setup
  3151. * DMA address.
  3152. */
  3153. if (!urb->setup_packet)
  3154. return -EINVAL;
  3155. /* 1 TRB for setup, 1 for status */
  3156. num_trbs = 2;
  3157. /*
  3158. * Don't need to check if we need additional event data and normal TRBs,
  3159. * since data in control transfers will never get bigger than 16MB
  3160. * XXX: can we get a buffer that crosses 64KB boundaries?
  3161. */
  3162. if (urb->transfer_buffer_length > 0)
  3163. num_trbs++;
  3164. ret = prepare_transfer(xhci, xhci->devs[slot_id],
  3165. ep_index, urb->stream_id,
  3166. num_trbs, urb, 0, mem_flags);
  3167. if (ret < 0)
  3168. return ret;
  3169. urb_priv = urb->hcpriv;
  3170. td = urb_priv->td[0];
  3171. /*
  3172. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  3173. * until we've finished creating all the other TRBs. The ring's cycle
  3174. * state may change as we enqueue the other TRBs, so save it too.
  3175. */
  3176. start_trb = &ep_ring->enqueue->generic;
  3177. start_cycle = ep_ring->cycle_state;
  3178. /* Queue setup TRB - see section 6.4.1.2.1 */
  3179. /* FIXME better way to translate setup_packet into two u32 fields? */
  3180. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  3181. field = 0;
  3182. field |= TRB_IDT | TRB_TYPE(TRB_SETUP);
  3183. if (start_cycle == 0)
  3184. field |= 0x1;
  3185. /* xHCI 1.0/1.1 6.4.1.2.1: Transfer Type field */
  3186. if (xhci->hci_version >= 0x100) {
  3187. if (urb->transfer_buffer_length > 0) {
  3188. if (setup->bRequestType & USB_DIR_IN)
  3189. field |= TRB_TX_TYPE(TRB_DATA_IN);
  3190. else
  3191. field |= TRB_TX_TYPE(TRB_DATA_OUT);
  3192. }
  3193. }
  3194. queue_trb(xhci, ep_ring, true,
  3195. setup->bRequestType | setup->bRequest << 8 | le16_to_cpu(setup->wValue) << 16,
  3196. le16_to_cpu(setup->wIndex) | le16_to_cpu(setup->wLength) << 16,
  3197. TRB_LEN(8) | TRB_INTR_TARGET(0),
  3198. /* Immediate data in pointer */
  3199. field);
  3200. /* If there's data, queue data TRBs */
  3201. /* Only set interrupt on short packet for IN endpoints */
  3202. if (usb_urb_dir_in(urb))
  3203. field = TRB_ISP | TRB_TYPE(TRB_DATA);
  3204. else
  3205. field = TRB_TYPE(TRB_DATA);
  3206. length_field = TRB_LEN(urb->transfer_buffer_length) |
  3207. xhci_td_remainder(urb->transfer_buffer_length) |
  3208. TRB_INTR_TARGET(0);
  3209. if (urb->transfer_buffer_length > 0) {
  3210. if (setup->bRequestType & USB_DIR_IN)
  3211. field |= TRB_DIR_IN;
  3212. queue_trb(xhci, ep_ring, true,
  3213. lower_32_bits(urb->transfer_dma),
  3214. upper_32_bits(urb->transfer_dma),
  3215. length_field,
  3216. field | ep_ring->cycle_state);
  3217. }
  3218. /* Save the DMA address of the last TRB in the TD */
  3219. td->last_trb = ep_ring->enqueue;
  3220. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  3221. /* If the device sent data, the status stage is an OUT transfer */
  3222. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  3223. field = 0;
  3224. else
  3225. field = TRB_DIR_IN;
  3226. queue_trb(xhci, ep_ring, false,
  3227. 0,
  3228. 0,
  3229. TRB_INTR_TARGET(0),
  3230. /* Event on completion */
  3231. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  3232. giveback_first_trb(xhci, slot_id, ep_index, 0,
  3233. start_cycle, start_trb);
  3234. return 0;
  3235. }
  3236. /*
  3237. * Variant of xhci_queue_ctrl_tx() used to implement EHSET
  3238. * SINGLE_STEP_SET_FEATURE test mode. It differs in that the control
  3239. * transfer is broken up so that the SETUP stage can happen and call
  3240. * the URB's completion handler before the DATA/STATUS stages are
  3241. * executed by the xHC hardware. This assumes the control transfer is a
  3242. * GetDescriptor, with a DATA stage in the IN direction, and an OUT
  3243. * STATUS stage.
  3244. *
  3245. * This function is called twice, usually with a 15-second delay in between.
  3246. * - with is_setup==true, the SETUP stage for the control request
  3247. * (GetDescriptor) is queued in the TRB ring and sent to HW immediately
  3248. * - with is_setup==false, the DATA and STATUS TRBs are queued and exceuted
  3249. *
  3250. * Caller must have locked xhci->lock
  3251. */
  3252. int xhci_submit_single_step_set_feature(struct usb_hcd *hcd, struct urb *urb,
  3253. int is_setup)
  3254. {
  3255. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  3256. struct xhci_ring *ep_ring;
  3257. int num_trbs;
  3258. int ret;
  3259. unsigned int slot_id, ep_index;
  3260. struct usb_ctrlrequest *setup;
  3261. struct xhci_generic_trb *start_trb;
  3262. int start_cycle;
  3263. u32 field, length_field;
  3264. struct urb_priv *urb_priv;
  3265. struct xhci_td *td;
  3266. ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
  3267. if (!ep_ring)
  3268. return -EINVAL;
  3269. /* Need buffer for data stage */
  3270. if (urb->transfer_buffer_length <= 0)
  3271. return -EINVAL;
  3272. /*
  3273. * Need to copy setup packet into setup TRB, so we can't use the setup
  3274. * DMA address.
  3275. */
  3276. if (!urb->setup_packet)
  3277. return -EINVAL;
  3278. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  3279. slot_id = urb->dev->slot_id;
  3280. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  3281. urb_priv = kzalloc(sizeof(struct urb_priv) +
  3282. sizeof(struct xhci_td *), GFP_ATOMIC);
  3283. if (!urb_priv)
  3284. return -ENOMEM;
  3285. td = urb_priv->td[0] = kzalloc(sizeof(struct xhci_td), GFP_ATOMIC);
  3286. if (!td) {
  3287. kfree(urb_priv);
  3288. return -ENOMEM;
  3289. }
  3290. urb_priv->length = 1;
  3291. urb_priv->td_cnt = 0;
  3292. urb->hcpriv = urb_priv;
  3293. num_trbs = is_setup ? 1 : 2;
  3294. ret = prepare_transfer(xhci, xhci->devs[slot_id],
  3295. ep_index, urb->stream_id,
  3296. num_trbs, urb, 0, GFP_ATOMIC);
  3297. if (ret < 0) {
  3298. kfree(td);
  3299. kfree(urb_priv);
  3300. return ret;
  3301. }
  3302. /*
  3303. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  3304. * until we've finished creating all the other TRBs. The ring's cycle
  3305. * state may change as we enqueue the other TRBs, so save it too.
  3306. */
  3307. start_trb = &ep_ring->enqueue->generic;
  3308. start_cycle = ep_ring->cycle_state;
  3309. if (is_setup) {
  3310. /* Queue only the setup TRB */
  3311. field = TRB_IDT | TRB_IOC | TRB_TYPE(TRB_SETUP);
  3312. if (start_cycle == 0)
  3313. field |= 0x1;
  3314. /* xHCI 1.0 6.4.1.2.1: Transfer Type field */
  3315. if (xhci->hci_version == 0x100) {
  3316. if (setup->bRequestType & USB_DIR_IN)
  3317. field |= TRB_TX_TYPE(TRB_DATA_IN);
  3318. else
  3319. field |= TRB_TX_TYPE(TRB_DATA_OUT);
  3320. }
  3321. /* Save the DMA address of the last TRB in the TD */
  3322. td->last_trb = ep_ring->enqueue;
  3323. queue_trb(xhci, ep_ring, false,
  3324. setup->bRequestType | setup->bRequest << 8 |
  3325. le16_to_cpu(setup->wValue) << 16,
  3326. le16_to_cpu(setup->wIndex) |
  3327. le16_to_cpu(setup->wLength) << 16,
  3328. TRB_LEN(8) | TRB_INTR_TARGET(0),
  3329. field);
  3330. } else {
  3331. /* Queue data TRB */
  3332. field = TRB_ISP | TRB_TYPE(TRB_DATA);
  3333. if (start_cycle == 0)
  3334. field |= 0x1;
  3335. if (setup->bRequestType & USB_DIR_IN)
  3336. field |= TRB_DIR_IN;
  3337. length_field = TRB_LEN(urb->transfer_buffer_length) |
  3338. xhci_td_remainder(urb->transfer_buffer_length) |
  3339. TRB_INTR_TARGET(0);
  3340. queue_trb(xhci, ep_ring, true,
  3341. lower_32_bits(urb->transfer_dma),
  3342. upper_32_bits(urb->transfer_dma),
  3343. length_field,
  3344. field);
  3345. /* Save the DMA address of the last TRB in the TD */
  3346. td->last_trb = ep_ring->enqueue;
  3347. /* Queue status TRB */
  3348. field = TRB_IOC | TRB_TYPE(TRB_STATUS);
  3349. if (!(setup->bRequestType & USB_DIR_IN))
  3350. field |= TRB_DIR_IN;
  3351. queue_trb(xhci, ep_ring, false,
  3352. 0,
  3353. 0,
  3354. TRB_INTR_TARGET(0),
  3355. field | ep_ring->cycle_state);
  3356. }
  3357. giveback_first_trb(xhci, slot_id, ep_index, 0, start_cycle, start_trb);
  3358. return 0;
  3359. }
  3360. static int count_isoc_trbs_needed(struct xhci_hcd *xhci,
  3361. struct urb *urb, int i)
  3362. {
  3363. int num_trbs = 0;
  3364. u64 addr, td_len;
  3365. addr = (u64) (urb->transfer_dma + urb->iso_frame_desc[i].offset);
  3366. td_len = urb->iso_frame_desc[i].length;
  3367. num_trbs = DIV_ROUND_UP(td_len + (addr & (TRB_MAX_BUFF_SIZE - 1)),
  3368. TRB_MAX_BUFF_SIZE);
  3369. if (num_trbs == 0)
  3370. num_trbs++;
  3371. return num_trbs;
  3372. }
  3373. /*
  3374. * The transfer burst count field of the isochronous TRB defines the number of
  3375. * bursts that are required to move all packets in this TD. Only SuperSpeed
  3376. * devices can burst up to bMaxBurst number of packets per service interval.
  3377. * This field is zero based, meaning a value of zero in the field means one
  3378. * burst. Basically, for everything but SuperSpeed devices, this field will be
  3379. * zero. Only xHCI 1.0 host controllers support this field.
  3380. */
  3381. static unsigned int xhci_get_burst_count(struct xhci_hcd *xhci,
  3382. struct usb_device *udev,
  3383. struct urb *urb, unsigned int total_packet_count)
  3384. {
  3385. unsigned int max_burst;
  3386. if (xhci->hci_version < 0x100 || udev->speed != USB_SPEED_SUPER)
  3387. return 0;
  3388. max_burst = urb->ep->ss_ep_comp.bMaxBurst;
  3389. return DIV_ROUND_UP(total_packet_count, max_burst + 1) - 1;
  3390. }
  3391. /*
  3392. * Returns the number of packets in the last "burst" of packets. This field is
  3393. * valid for all speeds of devices. USB 2.0 devices can only do one "burst", so
  3394. * the last burst packet count is equal to the total number of packets in the
  3395. * TD. SuperSpeed endpoints can have up to 3 bursts. All but the last burst
  3396. * must contain (bMaxBurst + 1) number of packets, but the last burst can
  3397. * contain 1 to (bMaxBurst + 1) packets.
  3398. */
  3399. static unsigned int xhci_get_last_burst_packet_count(struct xhci_hcd *xhci,
  3400. struct usb_device *udev,
  3401. struct urb *urb, unsigned int total_packet_count)
  3402. {
  3403. unsigned int max_burst;
  3404. unsigned int residue;
  3405. if (xhci->hci_version < 0x100)
  3406. return 0;
  3407. switch (udev->speed) {
  3408. case USB_SPEED_SUPER:
  3409. /* bMaxBurst is zero based: 0 means 1 packet per burst */
  3410. max_burst = urb->ep->ss_ep_comp.bMaxBurst;
  3411. residue = total_packet_count % (max_burst + 1);
  3412. /* If residue is zero, the last burst contains (max_burst + 1)
  3413. * number of packets, but the TLBPC field is zero-based.
  3414. */
  3415. if (residue == 0)
  3416. return max_burst;
  3417. return residue - 1;
  3418. default:
  3419. if (total_packet_count == 0)
  3420. return 0;
  3421. return total_packet_count - 1;
  3422. }
  3423. }
  3424. /* This is for isoc transfer */
  3425. static int xhci_queue_isoc_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  3426. struct urb *urb, int slot_id, unsigned int ep_index)
  3427. {
  3428. struct xhci_ring *ep_ring;
  3429. struct urb_priv *urb_priv;
  3430. struct xhci_td *td;
  3431. int num_tds, trbs_per_td;
  3432. struct xhci_generic_trb *start_trb;
  3433. bool first_trb;
  3434. int start_cycle;
  3435. u32 field, length_field;
  3436. int running_total, trb_buff_len, td_len, td_remain_len, ret;
  3437. u64 start_addr, addr;
  3438. int i, j;
  3439. bool more_trbs_coming;
  3440. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  3441. num_tds = urb->number_of_packets;
  3442. if (num_tds < 1) {
  3443. xhci_dbg(xhci, "Isoc URB with zero packets?\n");
  3444. return -EINVAL;
  3445. }
  3446. start_addr = (u64) urb->transfer_dma;
  3447. start_trb = &ep_ring->enqueue->generic;
  3448. start_cycle = ep_ring->cycle_state;
  3449. urb_priv = urb->hcpriv;
  3450. /* Queue the first TRB, even if it's zero-length */
  3451. for (i = 0; i < num_tds; i++) {
  3452. unsigned int total_packet_count;
  3453. unsigned int burst_count;
  3454. unsigned int residue;
  3455. first_trb = true;
  3456. running_total = 0;
  3457. addr = start_addr + urb->iso_frame_desc[i].offset;
  3458. td_len = urb->iso_frame_desc[i].length;
  3459. td_remain_len = td_len;
  3460. total_packet_count = DIV_ROUND_UP(td_len,
  3461. GET_MAX_PACKET(
  3462. usb_endpoint_maxp(&urb->ep->desc)));
  3463. /* A zero-length transfer still involves at least one packet. */
  3464. if (total_packet_count == 0)
  3465. total_packet_count++;
  3466. burst_count = xhci_get_burst_count(xhci, urb->dev, urb,
  3467. total_packet_count);
  3468. residue = xhci_get_last_burst_packet_count(xhci,
  3469. urb->dev, urb, total_packet_count);
  3470. trbs_per_td = count_isoc_trbs_needed(xhci, urb, i);
  3471. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
  3472. urb->stream_id, trbs_per_td, urb, i, mem_flags);
  3473. if (ret < 0) {
  3474. if (i == 0)
  3475. return ret;
  3476. goto cleanup;
  3477. }
  3478. td = urb_priv->td[i];
  3479. for (j = 0; j < trbs_per_td; j++) {
  3480. u32 remainder = 0;
  3481. field = 0;
  3482. if (first_trb) {
  3483. field = TRB_TBC(burst_count) |
  3484. TRB_TLBPC(residue);
  3485. /* Queue the isoc TRB */
  3486. field |= TRB_TYPE(TRB_ISOC);
  3487. /* Assume URB_ISO_ASAP is set */
  3488. field |= TRB_SIA;
  3489. if (i == 0) {
  3490. if (start_cycle == 0)
  3491. field |= 0x1;
  3492. } else
  3493. field |= ep_ring->cycle_state;
  3494. first_trb = false;
  3495. } else {
  3496. /* Queue other normal TRBs */
  3497. field |= TRB_TYPE(TRB_NORMAL);
  3498. field |= ep_ring->cycle_state;
  3499. }
  3500. /* Only set interrupt on short packet for IN EPs */
  3501. if (usb_urb_dir_in(urb))
  3502. field |= TRB_ISP;
  3503. /* Chain all the TRBs together; clear the chain bit in
  3504. * the last TRB to indicate it's the last TRB in the
  3505. * chain.
  3506. */
  3507. if (j < trbs_per_td - 1) {
  3508. field |= TRB_CHAIN;
  3509. more_trbs_coming = true;
  3510. } else {
  3511. td->last_trb = ep_ring->enqueue;
  3512. field |= TRB_IOC;
  3513. if (xhci->hci_version == 0x100 &&
  3514. !(xhci->quirks &
  3515. XHCI_AVOID_BEI)) {
  3516. /* Set BEI bit except for the last td */
  3517. if (i < num_tds - 1)
  3518. field |= TRB_BEI;
  3519. }
  3520. more_trbs_coming = false;
  3521. }
  3522. /* Calculate TRB length */
  3523. trb_buff_len = TRB_MAX_BUFF_SIZE -
  3524. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  3525. if (trb_buff_len > td_remain_len)
  3526. trb_buff_len = td_remain_len;
  3527. /* Set the TRB length, TD size, & interrupter fields. */
  3528. if (xhci->hci_version < 0x100) {
  3529. remainder = xhci_td_remainder(
  3530. td_len - running_total);
  3531. } else {
  3532. remainder = xhci_v1_0_td_remainder(
  3533. running_total, trb_buff_len,
  3534. total_packet_count, urb,
  3535. (trbs_per_td - j - 1));
  3536. }
  3537. length_field = TRB_LEN(trb_buff_len) |
  3538. remainder |
  3539. TRB_INTR_TARGET(0);
  3540. queue_trb(xhci, ep_ring, more_trbs_coming,
  3541. lower_32_bits(addr),
  3542. upper_32_bits(addr),
  3543. length_field,
  3544. field);
  3545. running_total += trb_buff_len;
  3546. addr += trb_buff_len;
  3547. td_remain_len -= trb_buff_len;
  3548. }
  3549. /* Check TD length */
  3550. if (running_total != td_len) {
  3551. xhci_err(xhci, "ISOC TD length unmatch\n");
  3552. ret = -EINVAL;
  3553. goto cleanup;
  3554. }
  3555. }
  3556. if (xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs == 0) {
  3557. if (xhci->quirks & XHCI_AMD_PLL_FIX)
  3558. usb_amd_quirk_pll_disable();
  3559. }
  3560. xhci_to_hcd(xhci)->self.bandwidth_isoc_reqs++;
  3561. giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
  3562. start_cycle, start_trb);
  3563. return 0;
  3564. cleanup:
  3565. /* Clean up a partially enqueued isoc transfer. */
  3566. for (i--; i >= 0; i--)
  3567. list_del_init(&urb_priv->td[i]->td_list);
  3568. /* Use the first TD as a temporary variable to turn the TDs we've queued
  3569. * into No-ops with a software-owned cycle bit. That way the hardware
  3570. * won't accidentally start executing bogus TDs when we partially
  3571. * overwrite them. td->first_trb and td->start_seg are already set.
  3572. */
  3573. urb_priv->td[0]->last_trb = ep_ring->enqueue;
  3574. /* Every TRB except the first & last will have its cycle bit flipped. */
  3575. td_to_noop(xhci, ep_ring, urb_priv->td[0], true);
  3576. /* Reset the ring enqueue back to the first TRB and its cycle bit. */
  3577. ep_ring->enqueue = urb_priv->td[0]->first_trb;
  3578. ep_ring->enq_seg = urb_priv->td[0]->start_seg;
  3579. ep_ring->cycle_state = start_cycle;
  3580. ep_ring->num_trbs_free = ep_ring->num_trbs_free_temp;
  3581. usb_hcd_unlink_urb_from_ep(bus_to_hcd(urb->dev->bus), urb);
  3582. return ret;
  3583. }
  3584. /*
  3585. * Check transfer ring to guarantee there is enough room for the urb.
  3586. * Update ISO URB start_frame and interval.
  3587. * Update interval as xhci_queue_intr_tx does. Just use xhci frame_index to
  3588. * update the urb->start_frame by now.
  3589. * Always assume URB_ISO_ASAP set, and NEVER use urb->start_frame as input.
  3590. */
  3591. int xhci_queue_isoc_tx_prepare(struct xhci_hcd *xhci, gfp_t mem_flags,
  3592. struct urb *urb, int slot_id, unsigned int ep_index)
  3593. {
  3594. struct xhci_virt_device *xdev;
  3595. struct xhci_ring *ep_ring;
  3596. struct xhci_ep_ctx *ep_ctx;
  3597. int start_frame;
  3598. int xhci_interval;
  3599. int ep_interval;
  3600. int num_tds, num_trbs, i;
  3601. int ret;
  3602. xdev = xhci->devs[slot_id];
  3603. ep_ring = xdev->eps[ep_index].ring;
  3604. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  3605. num_trbs = 0;
  3606. num_tds = urb->number_of_packets;
  3607. for (i = 0; i < num_tds; i++)
  3608. num_trbs += count_isoc_trbs_needed(xhci, urb, i);
  3609. /* Check the ring to guarantee there is enough room for the whole urb.
  3610. * Do not insert any td of the urb to the ring if the check failed.
  3611. */
  3612. ret = prepare_ring(xhci, ep_ring, le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK,
  3613. num_trbs, mem_flags);
  3614. if (ret)
  3615. return ret;
  3616. start_frame = xhci_readl(xhci, &xhci->run_regs->microframe_index);
  3617. start_frame &= 0x3fff;
  3618. urb->start_frame = start_frame;
  3619. if (urb->dev->speed == USB_SPEED_LOW ||
  3620. urb->dev->speed == USB_SPEED_FULL)
  3621. urb->start_frame >>= 3;
  3622. xhci_interval = EP_INTERVAL_TO_UFRAMES(le32_to_cpu(ep_ctx->ep_info));
  3623. ep_interval = urb->interval;
  3624. /* Convert to microframes */
  3625. if (urb->dev->speed == USB_SPEED_LOW ||
  3626. urb->dev->speed == USB_SPEED_FULL)
  3627. ep_interval *= 8;
  3628. /* FIXME change this to a warning and a suggestion to use the new API
  3629. * to set the polling interval (once the API is added).
  3630. */
  3631. if (xhci_interval != ep_interval) {
  3632. if (printk_ratelimit())
  3633. dev_dbg(&urb->dev->dev, "Driver uses different interval"
  3634. " (%d microframe%s) than xHCI "
  3635. "(%d microframe%s)\n",
  3636. ep_interval,
  3637. ep_interval == 1 ? "" : "s",
  3638. xhci_interval,
  3639. xhci_interval == 1 ? "" : "s");
  3640. urb->interval = xhci_interval;
  3641. /* Convert back to frames for LS/FS devices */
  3642. if (urb->dev->speed == USB_SPEED_LOW ||
  3643. urb->dev->speed == USB_SPEED_FULL)
  3644. urb->interval /= 8;
  3645. }
  3646. ep_ring->num_trbs_free_temp = ep_ring->num_trbs_free;
  3647. return xhci_queue_isoc_tx(xhci, mem_flags, urb, slot_id, ep_index);
  3648. }
  3649. /**** Command Ring Operations ****/
  3650. /* Generic function for queueing a command TRB on the command ring.
  3651. * Check to make sure there's room on the command ring for one command TRB.
  3652. * Also check that there's room reserved for commands that must not fail.
  3653. * If this is a command that must not fail, meaning command_must_succeed = TRUE,
  3654. * then only check for the number of reserved spots.
  3655. * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
  3656. * because the command event handler may want to resubmit a failed command.
  3657. */
  3658. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
  3659. u32 field3, u32 field4, bool command_must_succeed)
  3660. {
  3661. int reserved_trbs = xhci->cmd_ring_reserved_trbs;
  3662. int ret;
  3663. if (!command_must_succeed)
  3664. reserved_trbs++;
  3665. ret = prepare_ring(xhci, xhci->cmd_ring, EP_STATE_RUNNING,
  3666. reserved_trbs, GFP_ATOMIC);
  3667. if (ret < 0) {
  3668. xhci_err(xhci, "ERR: No room for command on command ring\n");
  3669. if (command_must_succeed)
  3670. xhci_err(xhci, "ERR: Reserved TRB counting for "
  3671. "unfailable commands failed.\n");
  3672. return ret;
  3673. }
  3674. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  3675. field4 | xhci->cmd_ring->cycle_state);
  3676. return 0;
  3677. }
  3678. /* Queue a slot enable or disable request on the command ring */
  3679. int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  3680. {
  3681. return queue_command(xhci, 0, 0, 0,
  3682. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
  3683. }
  3684. /* Queue an address device command TRB */
  3685. int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  3686. u32 slot_id)
  3687. {
  3688. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  3689. upper_32_bits(in_ctx_ptr), 0,
  3690. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id),
  3691. false);
  3692. }
  3693. int xhci_queue_vendor_command(struct xhci_hcd *xhci,
  3694. u32 field1, u32 field2, u32 field3, u32 field4)
  3695. {
  3696. return queue_command(xhci, field1, field2, field3, field4, false);
  3697. }
  3698. /* Queue a reset device command TRB */
  3699. int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id)
  3700. {
  3701. return queue_command(xhci, 0, 0, 0,
  3702. TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
  3703. false);
  3704. }
  3705. /* Queue a configure endpoint command TRB */
  3706. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  3707. u32 slot_id, bool command_must_succeed)
  3708. {
  3709. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  3710. upper_32_bits(in_ctx_ptr), 0,
  3711. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
  3712. command_must_succeed);
  3713. }
  3714. /* Queue an evaluate context command TRB */
  3715. int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  3716. u32 slot_id)
  3717. {
  3718. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  3719. upper_32_bits(in_ctx_ptr), 0,
  3720. TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
  3721. false);
  3722. }
  3723. /*
  3724. * Suspend is set to indicate "Stop Endpoint Command" is being issued to stop
  3725. * activity on an endpoint that is about to be suspended.
  3726. */
  3727. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
  3728. unsigned int ep_index, int suspend)
  3729. {
  3730. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  3731. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  3732. u32 type = TRB_TYPE(TRB_STOP_RING);
  3733. u32 trb_suspend = SUSPEND_PORT_FOR_TRB(suspend);
  3734. return queue_command(xhci, 0, 0, 0,
  3735. trb_slot_id | trb_ep_index | type | trb_suspend, false);
  3736. }
  3737. /* Set Transfer Ring Dequeue Pointer command.
  3738. * This should not be used for endpoints that have streams enabled.
  3739. */
  3740. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  3741. unsigned int ep_index, unsigned int stream_id,
  3742. struct xhci_segment *deq_seg,
  3743. union xhci_trb *deq_ptr, u32 cycle_state)
  3744. {
  3745. dma_addr_t addr;
  3746. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  3747. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  3748. u32 trb_stream_id = STREAM_ID_FOR_TRB(stream_id);
  3749. u32 type = TRB_TYPE(TRB_SET_DEQ);
  3750. struct xhci_virt_ep *ep;
  3751. addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
  3752. if (addr == 0) {
  3753. xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
  3754. xhci_warn(xhci, "WARN deq seg = %pK, deq pt = %pK\n",
  3755. deq_seg, deq_ptr);
  3756. return 0;
  3757. }
  3758. ep = &xhci->devs[slot_id]->eps[ep_index];
  3759. if ((ep->ep_state & SET_DEQ_PENDING)) {
  3760. xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
  3761. xhci_warn(xhci, "A Set TR Deq Ptr command is pending.\n");
  3762. return 0;
  3763. }
  3764. ep->queued_deq_seg = deq_seg;
  3765. ep->queued_deq_ptr = deq_ptr;
  3766. return queue_command(xhci, lower_32_bits(addr) | cycle_state,
  3767. upper_32_bits(addr), trb_stream_id,
  3768. trb_slot_id | trb_ep_index | type, false);
  3769. }
  3770. int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
  3771. unsigned int ep_index)
  3772. {
  3773. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  3774. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  3775. u32 type = TRB_TYPE(TRB_RESET_EP);
  3776. return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type,
  3777. false);
  3778. }