xhci-ring.c 123 KB

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