xhci-ring.c 125 KB

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