xhci-ring.c 111 KB

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