xhci-ring.c 128 KB

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