xhci-ring.c 111 KB

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