xhci-ring.c 109 KB

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