xhci-ring.c 130 KB

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