xhci-ring.c 106 KB

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