xhci-ring.c 113 KB

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