xhci-ring.c 121 KB

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