xhci-ring.c 125 KB

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