xhci-ring.c 126 KB

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