xhci-ring.c 126 KB

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