xhci-ring.c 125 KB

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