xhci-ring.c 125 KB

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