xhci-ring.c 97 KB

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