xhci-ring.c 130 KB

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