xhci-ring.c 129 KB

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