xhci-ring.c 104 KB

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