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