xhci-ring.c 127 KB

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