xhci-ring.c 113 KB

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