xhci-ring.c 110 KB

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