xhci-ring.c 99 KB

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