xhci-ring.c 121 KB

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