xhci-ring.c 114 KB

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