xhci-ring.c 103 KB

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