xhci-ring.c 111 KB

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