xhci-ring.c 113 KB

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