xhci-ring.c 124 KB

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