xhci-ring.c 122 KB

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