xhci-ring.c 124 KB

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