xhci-ring.c 122 KB

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