xhci-ring.c 123 KB

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