xhci-ring.c 125 KB

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