xhci-ring.c 123 KB

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