xhci-ring.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  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. /*
  69. * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
  70. * address of the TRB.
  71. */
  72. dma_addr_t xhci_trb_virt_to_dma(struct xhci_segment *seg,
  73. union xhci_trb *trb)
  74. {
  75. unsigned long segment_offset;
  76. if (!seg || !trb || trb < seg->trbs)
  77. return 0;
  78. /* offset in TRBs */
  79. segment_offset = trb - seg->trbs;
  80. if (segment_offset > TRBS_PER_SEGMENT)
  81. return 0;
  82. return seg->dma + (segment_offset * sizeof(*trb));
  83. }
  84. /* Does this link TRB point to the first segment in a ring,
  85. * or was the previous TRB the last TRB on the last segment in the ERST?
  86. */
  87. static inline bool last_trb_on_last_seg(struct xhci_hcd *xhci, struct xhci_ring *ring,
  88. struct xhci_segment *seg, union xhci_trb *trb)
  89. {
  90. if (ring == xhci->event_ring)
  91. return (trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
  92. (seg->next == xhci->event_ring->first_seg);
  93. else
  94. return trb->link.control & LINK_TOGGLE;
  95. }
  96. /* Is this TRB a link TRB or was the last TRB the last TRB in this event ring
  97. * segment? I.e. would the updated event TRB pointer step off the end of the
  98. * event seg?
  99. */
  100. static inline int last_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  101. struct xhci_segment *seg, union xhci_trb *trb)
  102. {
  103. if (ring == xhci->event_ring)
  104. return trb == &seg->trbs[TRBS_PER_SEGMENT];
  105. else
  106. return (trb->link.control & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_LINK);
  107. }
  108. /* Updates trb to point to the next TRB in the ring, and updates seg if the next
  109. * TRB is in a new segment. This does not skip over link TRBs, and it does not
  110. * effect the ring dequeue or enqueue pointers.
  111. */
  112. static void next_trb(struct xhci_hcd *xhci,
  113. struct xhci_ring *ring,
  114. struct xhci_segment **seg,
  115. union xhci_trb **trb)
  116. {
  117. if (last_trb(xhci, ring, *seg, *trb)) {
  118. *seg = (*seg)->next;
  119. *trb = ((*seg)->trbs);
  120. } else {
  121. *trb = (*trb)++;
  122. }
  123. }
  124. /*
  125. * See Cycle bit rules. SW is the consumer for the event ring only.
  126. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  127. */
  128. static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  129. {
  130. union xhci_trb *next = ++(ring->dequeue);
  131. unsigned long long addr;
  132. ring->deq_updates++;
  133. /* Update the dequeue pointer further if that was a link TRB or we're at
  134. * the end of an event ring segment (which doesn't have link TRBS)
  135. */
  136. while (last_trb(xhci, ring, ring->deq_seg, next)) {
  137. if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
  138. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  139. if (!in_interrupt())
  140. xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
  141. ring,
  142. (unsigned int) ring->cycle_state);
  143. }
  144. ring->deq_seg = ring->deq_seg->next;
  145. ring->dequeue = ring->deq_seg->trbs;
  146. next = ring->dequeue;
  147. }
  148. addr = (unsigned long long) xhci_trb_virt_to_dma(ring->deq_seg, ring->dequeue);
  149. if (ring == xhci->event_ring)
  150. xhci_dbg(xhci, "Event ring deq = 0x%llx (DMA)\n", addr);
  151. else if (ring == xhci->cmd_ring)
  152. xhci_dbg(xhci, "Command ring deq = 0x%llx (DMA)\n", addr);
  153. else
  154. xhci_dbg(xhci, "Ring deq = 0x%llx (DMA)\n", addr);
  155. }
  156. /*
  157. * See Cycle bit rules. SW is the consumer for the event ring only.
  158. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  159. *
  160. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  161. * chain bit is set), then set the chain bit in all the following link TRBs.
  162. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  163. * have their chain bit cleared (so that each Link TRB is a separate TD).
  164. *
  165. * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
  166. * set, but other sections talk about dealing with the chain bit set. This was
  167. * fixed in the 0.96 specification errata, but we have to assume that all 0.95
  168. * xHCI hardware can't handle the chain bit being cleared on a link TRB.
  169. */
  170. static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  171. {
  172. u32 chain;
  173. union xhci_trb *next;
  174. unsigned long long addr;
  175. chain = ring->enqueue->generic.field[3] & TRB_CHAIN;
  176. next = ++(ring->enqueue);
  177. ring->enq_updates++;
  178. /* Update the dequeue pointer further if that was a link TRB or we're at
  179. * the end of an event ring segment (which doesn't have link TRBS)
  180. */
  181. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  182. if (!consumer) {
  183. if (ring != xhci->event_ring) {
  184. /* If we're not dealing with 0.95 hardware,
  185. * carry over the chain bit of the previous TRB
  186. * (which may mean the chain bit is cleared).
  187. */
  188. if (!xhci_link_trb_quirk(xhci)) {
  189. next->link.control &= ~TRB_CHAIN;
  190. next->link.control |= chain;
  191. }
  192. /* Give this link TRB to the hardware */
  193. wmb();
  194. if (next->link.control & TRB_CYCLE)
  195. next->link.control &= (u32) ~TRB_CYCLE;
  196. else
  197. next->link.control |= (u32) TRB_CYCLE;
  198. }
  199. /* Toggle the cycle bit after the last ring segment. */
  200. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  201. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  202. if (!in_interrupt())
  203. xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
  204. ring,
  205. (unsigned int) ring->cycle_state);
  206. }
  207. }
  208. ring->enq_seg = ring->enq_seg->next;
  209. ring->enqueue = ring->enq_seg->trbs;
  210. next = ring->enqueue;
  211. }
  212. addr = (unsigned long long) xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
  213. if (ring == xhci->event_ring)
  214. xhci_dbg(xhci, "Event ring enq = 0x%llx (DMA)\n", addr);
  215. else if (ring == xhci->cmd_ring)
  216. xhci_dbg(xhci, "Command ring enq = 0x%llx (DMA)\n", addr);
  217. else
  218. xhci_dbg(xhci, "Ring enq = 0x%llx (DMA)\n", addr);
  219. }
  220. /*
  221. * Check to see if there's room to enqueue num_trbs on the ring. See rules
  222. * above.
  223. * FIXME: this would be simpler and faster if we just kept track of the number
  224. * of free TRBs in a ring.
  225. */
  226. static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
  227. unsigned int num_trbs)
  228. {
  229. int i;
  230. union xhci_trb *enq = ring->enqueue;
  231. struct xhci_segment *enq_seg = ring->enq_seg;
  232. /* Check if ring is empty */
  233. if (enq == ring->dequeue)
  234. return 1;
  235. /* Make sure there's an extra empty TRB available */
  236. for (i = 0; i <= num_trbs; ++i) {
  237. if (enq == ring->dequeue)
  238. return 0;
  239. enq++;
  240. while (last_trb(xhci, ring, enq_seg, enq)) {
  241. enq_seg = enq_seg->next;
  242. enq = enq_seg->trbs;
  243. }
  244. }
  245. return 1;
  246. }
  247. void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
  248. {
  249. u64 temp;
  250. dma_addr_t deq;
  251. deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
  252. xhci->event_ring->dequeue);
  253. if (deq == 0 && !in_interrupt())
  254. xhci_warn(xhci, "WARN something wrong with SW event ring "
  255. "dequeue ptr.\n");
  256. /* Update HC event ring dequeue pointer */
  257. temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
  258. temp &= ERST_PTR_MASK;
  259. /* Don't clear the EHB bit (which is RW1C) because
  260. * there might be more events to service.
  261. */
  262. temp &= ~ERST_EHB;
  263. xhci_dbg(xhci, "// Write event ring dequeue pointer, preserving EHB bit\n");
  264. xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
  265. &xhci->ir_set->erst_dequeue);
  266. }
  267. /* Ring the host controller doorbell after placing a command on the ring */
  268. void xhci_ring_cmd_db(struct xhci_hcd *xhci)
  269. {
  270. u32 temp;
  271. xhci_dbg(xhci, "// Ding dong!\n");
  272. temp = xhci_readl(xhci, &xhci->dba->doorbell[0]) & DB_MASK;
  273. xhci_writel(xhci, temp | DB_TARGET_HOST, &xhci->dba->doorbell[0]);
  274. /* Flush PCI posted writes */
  275. xhci_readl(xhci, &xhci->dba->doorbell[0]);
  276. }
  277. static void ring_ep_doorbell(struct xhci_hcd *xhci,
  278. unsigned int slot_id,
  279. unsigned int ep_index)
  280. {
  281. struct xhci_virt_ep *ep;
  282. unsigned int ep_state;
  283. u32 field;
  284. __u32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
  285. ep = &xhci->devs[slot_id]->eps[ep_index];
  286. ep_state = ep->ep_state;
  287. /* Don't ring the doorbell for this endpoint if there are pending
  288. * cancellations because the we don't want to interrupt processing.
  289. */
  290. if (!(ep_state & EP_HALT_PENDING) && !(ep_state & SET_DEQ_PENDING)
  291. && !(ep_state & EP_HALTED)) {
  292. field = xhci_readl(xhci, db_addr) & DB_MASK;
  293. xhci_writel(xhci, field | EPI_TO_DB(ep_index), db_addr);
  294. /* Flush PCI posted writes - FIXME Matthew Wilcox says this
  295. * isn't time-critical and we shouldn't make the CPU wait for
  296. * the flush.
  297. */
  298. xhci_readl(xhci, db_addr);
  299. }
  300. }
  301. /*
  302. * Find the segment that trb is in. Start searching in start_seg.
  303. * If we must move past a segment that has a link TRB with a toggle cycle state
  304. * bit set, then we will toggle the value pointed at by cycle_state.
  305. */
  306. static struct xhci_segment *find_trb_seg(
  307. struct xhci_segment *start_seg,
  308. union xhci_trb *trb, int *cycle_state)
  309. {
  310. struct xhci_segment *cur_seg = start_seg;
  311. struct xhci_generic_trb *generic_trb;
  312. while (cur_seg->trbs > trb ||
  313. &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
  314. generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
  315. if (TRB_TYPE(generic_trb->field[3]) == TRB_LINK &&
  316. (generic_trb->field[3] & LINK_TOGGLE))
  317. *cycle_state = ~(*cycle_state) & 0x1;
  318. cur_seg = cur_seg->next;
  319. if (cur_seg == start_seg)
  320. /* Looped over the entire list. Oops! */
  321. return 0;
  322. }
  323. return cur_seg;
  324. }
  325. /*
  326. * Move the xHC's endpoint ring dequeue pointer past cur_td.
  327. * Record the new state of the xHC's endpoint ring dequeue segment,
  328. * dequeue pointer, and new consumer cycle state in state.
  329. * Update our internal representation of the ring's dequeue pointer.
  330. *
  331. * We do this in three jumps:
  332. * - First we update our new ring state to be the same as when the xHC stopped.
  333. * - Then we traverse the ring to find the segment that contains
  334. * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
  335. * any link TRBs with the toggle cycle bit set.
  336. * - Finally we move the dequeue state one TRB further, toggling the cycle bit
  337. * if we've moved it past a link TRB with the toggle cycle bit set.
  338. */
  339. void xhci_find_new_dequeue_state(struct xhci_hcd *xhci,
  340. unsigned int slot_id, unsigned int ep_index,
  341. struct xhci_td *cur_td, struct xhci_dequeue_state *state)
  342. {
  343. struct xhci_virt_device *dev = xhci->devs[slot_id];
  344. struct xhci_ring *ep_ring = dev->eps[ep_index].ring;
  345. struct xhci_generic_trb *trb;
  346. struct xhci_ep_ctx *ep_ctx;
  347. dma_addr_t addr;
  348. state->new_cycle_state = 0;
  349. xhci_dbg(xhci, "Finding segment containing stopped TRB.\n");
  350. state->new_deq_seg = find_trb_seg(cur_td->start_seg,
  351. dev->eps[ep_index].stopped_trb,
  352. &state->new_cycle_state);
  353. if (!state->new_deq_seg)
  354. BUG();
  355. /* Dig out the cycle state saved by the xHC during the stop ep cmd */
  356. xhci_dbg(xhci, "Finding endpoint context\n");
  357. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  358. state->new_cycle_state = 0x1 & ep_ctx->deq;
  359. state->new_deq_ptr = cur_td->last_trb;
  360. xhci_dbg(xhci, "Finding segment containing last TRB in TD.\n");
  361. state->new_deq_seg = find_trb_seg(state->new_deq_seg,
  362. state->new_deq_ptr,
  363. &state->new_cycle_state);
  364. if (!state->new_deq_seg)
  365. BUG();
  366. trb = &state->new_deq_ptr->generic;
  367. if (TRB_TYPE(trb->field[3]) == TRB_LINK &&
  368. (trb->field[3] & LINK_TOGGLE))
  369. state->new_cycle_state = ~(state->new_cycle_state) & 0x1;
  370. next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
  371. /* Don't update the ring cycle state for the producer (us). */
  372. xhci_dbg(xhci, "New dequeue segment = %p (virtual)\n",
  373. state->new_deq_seg);
  374. addr = xhci_trb_virt_to_dma(state->new_deq_seg, state->new_deq_ptr);
  375. xhci_dbg(xhci, "New dequeue pointer = 0x%llx (DMA)\n",
  376. (unsigned long long) addr);
  377. xhci_dbg(xhci, "Setting dequeue pointer in internal ring state.\n");
  378. ep_ring->dequeue = state->new_deq_ptr;
  379. ep_ring->deq_seg = state->new_deq_seg;
  380. }
  381. static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  382. struct xhci_td *cur_td)
  383. {
  384. struct xhci_segment *cur_seg;
  385. union xhci_trb *cur_trb;
  386. for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
  387. true;
  388. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  389. if ((cur_trb->generic.field[3] & TRB_TYPE_BITMASK) ==
  390. TRB_TYPE(TRB_LINK)) {
  391. /* Unchain any chained Link TRBs, but
  392. * leave the pointers intact.
  393. */
  394. cur_trb->generic.field[3] &= ~TRB_CHAIN;
  395. xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
  396. xhci_dbg(xhci, "Address = %p (0x%llx dma); "
  397. "in seg %p (0x%llx dma)\n",
  398. cur_trb,
  399. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  400. cur_seg,
  401. (unsigned long long)cur_seg->dma);
  402. } else {
  403. cur_trb->generic.field[0] = 0;
  404. cur_trb->generic.field[1] = 0;
  405. cur_trb->generic.field[2] = 0;
  406. /* Preserve only the cycle bit of this TRB */
  407. cur_trb->generic.field[3] &= TRB_CYCLE;
  408. cur_trb->generic.field[3] |= TRB_TYPE(TRB_TR_NOOP);
  409. xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) "
  410. "in seg %p (0x%llx dma)\n",
  411. cur_trb,
  412. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  413. cur_seg,
  414. (unsigned long long)cur_seg->dma);
  415. }
  416. if (cur_trb == cur_td->last_trb)
  417. break;
  418. }
  419. }
  420. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  421. unsigned int ep_index, struct xhci_segment *deq_seg,
  422. union xhci_trb *deq_ptr, u32 cycle_state);
  423. void xhci_queue_new_dequeue_state(struct xhci_hcd *xhci,
  424. unsigned int slot_id, unsigned int ep_index,
  425. struct xhci_dequeue_state *deq_state)
  426. {
  427. struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
  428. xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
  429. "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
  430. deq_state->new_deq_seg,
  431. (unsigned long long)deq_state->new_deq_seg->dma,
  432. deq_state->new_deq_ptr,
  433. (unsigned long long)xhci_trb_virt_to_dma(deq_state->new_deq_seg, deq_state->new_deq_ptr),
  434. deq_state->new_cycle_state);
  435. queue_set_tr_deq(xhci, slot_id, ep_index,
  436. deq_state->new_deq_seg,
  437. deq_state->new_deq_ptr,
  438. (u32) deq_state->new_cycle_state);
  439. /* Stop the TD queueing code from ringing the doorbell until
  440. * this command completes. The HC won't set the dequeue pointer
  441. * if the ring is running, and ringing the doorbell starts the
  442. * ring running.
  443. */
  444. ep->ep_state |= SET_DEQ_PENDING;
  445. }
  446. static inline void xhci_stop_watchdog_timer_in_irq(struct xhci_hcd *xhci,
  447. struct xhci_virt_ep *ep)
  448. {
  449. ep->ep_state &= ~EP_HALT_PENDING;
  450. /* Can't del_timer_sync in interrupt, so we attempt to cancel. If the
  451. * timer is running on another CPU, we don't decrement stop_cmds_pending
  452. * (since we didn't successfully stop the watchdog timer).
  453. */
  454. if (del_timer(&ep->stop_cmd_timer))
  455. ep->stop_cmds_pending--;
  456. }
  457. /* Must be called with xhci->lock held in interrupt context */
  458. static void xhci_giveback_urb_in_irq(struct xhci_hcd *xhci,
  459. struct xhci_td *cur_td, int status, char *adjective)
  460. {
  461. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  462. cur_td->urb->hcpriv = NULL;
  463. usb_hcd_unlink_urb_from_ep(hcd, cur_td->urb);
  464. xhci_dbg(xhci, "Giveback %s URB %p\n", adjective, cur_td->urb);
  465. spin_unlock(&xhci->lock);
  466. usb_hcd_giveback_urb(hcd, cur_td->urb, status);
  467. kfree(cur_td);
  468. spin_lock(&xhci->lock);
  469. xhci_dbg(xhci, "%s URB given back\n", adjective);
  470. }
  471. /*
  472. * When we get a command completion for a Stop Endpoint Command, we need to
  473. * unlink any cancelled TDs from the ring. There are two ways to do that:
  474. *
  475. * 1. If the HW was in the middle of processing the TD that needs to be
  476. * cancelled, then we must move the ring's dequeue pointer past the last TRB
  477. * in the TD with a Set Dequeue Pointer Command.
  478. * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
  479. * bit cleared) so that the HW will skip over them.
  480. */
  481. static void handle_stopped_endpoint(struct xhci_hcd *xhci,
  482. union xhci_trb *trb)
  483. {
  484. unsigned int slot_id;
  485. unsigned int ep_index;
  486. struct xhci_ring *ep_ring;
  487. struct xhci_virt_ep *ep;
  488. struct list_head *entry;
  489. struct xhci_td *cur_td = 0;
  490. struct xhci_td *last_unlinked_td;
  491. struct xhci_dequeue_state deq_state;
  492. memset(&deq_state, 0, sizeof(deq_state));
  493. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  494. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  495. ep = &xhci->devs[slot_id]->eps[ep_index];
  496. ep_ring = ep->ring;
  497. if (list_empty(&ep->cancelled_td_list)) {
  498. xhci_stop_watchdog_timer_in_irq(xhci, ep);
  499. ring_ep_doorbell(xhci, slot_id, ep_index);
  500. return;
  501. }
  502. /* Fix up the ep ring first, so HW stops executing cancelled TDs.
  503. * We have the xHCI lock, so nothing can modify this list until we drop
  504. * it. We're also in the event handler, so we can't get re-interrupted
  505. * if another Stop Endpoint command completes
  506. */
  507. list_for_each(entry, &ep->cancelled_td_list) {
  508. cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
  509. xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
  510. cur_td->first_trb,
  511. (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
  512. /*
  513. * If we stopped on the TD we need to cancel, then we have to
  514. * move the xHC endpoint ring dequeue pointer past this TD.
  515. */
  516. if (cur_td == ep->stopped_td)
  517. xhci_find_new_dequeue_state(xhci, slot_id, ep_index, cur_td,
  518. &deq_state);
  519. else
  520. td_to_noop(xhci, ep_ring, cur_td);
  521. /*
  522. * The event handler won't see a completion for this TD anymore,
  523. * so remove it from the endpoint ring's TD list. Keep it in
  524. * the cancelled TD list for URB completion later.
  525. */
  526. list_del(&cur_td->td_list);
  527. }
  528. last_unlinked_td = cur_td;
  529. xhci_stop_watchdog_timer_in_irq(xhci, ep);
  530. /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
  531. if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
  532. xhci_queue_new_dequeue_state(xhci,
  533. slot_id, ep_index, &deq_state);
  534. xhci_ring_cmd_db(xhci);
  535. } else {
  536. /* Otherwise just ring the doorbell to restart the ring */
  537. ring_ep_doorbell(xhci, slot_id, ep_index);
  538. }
  539. /*
  540. * Drop the lock and complete the URBs in the cancelled TD list.
  541. * New TDs to be cancelled might be added to the end of the list before
  542. * we can complete all the URBs for the TDs we already unlinked.
  543. * So stop when we've completed the URB for the last TD we unlinked.
  544. */
  545. do {
  546. cur_td = list_entry(ep->cancelled_td_list.next,
  547. struct xhci_td, cancelled_td_list);
  548. list_del(&cur_td->cancelled_td_list);
  549. /* Clean up the cancelled URB */
  550. /* Doesn't matter what we pass for status, since the core will
  551. * just overwrite it (because the URB has been unlinked).
  552. */
  553. xhci_giveback_urb_in_irq(xhci, cur_td, 0, "cancelled");
  554. /* Stop processing the cancelled list if the watchdog timer is
  555. * running.
  556. */
  557. if (xhci->xhc_state & XHCI_STATE_DYING)
  558. return;
  559. } while (cur_td != last_unlinked_td);
  560. /* Return to the event handler with xhci->lock re-acquired */
  561. }
  562. /* Watchdog timer function for when a stop endpoint command fails to complete.
  563. * In this case, we assume the host controller is broken or dying or dead. The
  564. * host may still be completing some other events, so we have to be careful to
  565. * let the event ring handler and the URB dequeueing/enqueueing functions know
  566. * through xhci->state.
  567. *
  568. * The timer may also fire if the host takes a very long time to respond to the
  569. * command, and the stop endpoint command completion handler cannot delete the
  570. * timer before the timer function is called. Another endpoint cancellation may
  571. * sneak in before the timer function can grab the lock, and that may queue
  572. * another stop endpoint command and add the timer back. So we cannot use a
  573. * simple flag to say whether there is a pending stop endpoint command for a
  574. * particular endpoint.
  575. *
  576. * Instead we use a combination of that flag and a counter for the number of
  577. * pending stop endpoint commands. If the timer is the tail end of the last
  578. * stop endpoint command, and the endpoint's command is still pending, we assume
  579. * the host is dying.
  580. */
  581. void xhci_stop_endpoint_command_watchdog(unsigned long arg)
  582. {
  583. struct xhci_hcd *xhci;
  584. struct xhci_virt_ep *ep;
  585. struct xhci_virt_ep *temp_ep;
  586. struct xhci_ring *ring;
  587. struct xhci_td *cur_td;
  588. int ret, i, j;
  589. ep = (struct xhci_virt_ep *) arg;
  590. xhci = ep->xhci;
  591. spin_lock(&xhci->lock);
  592. ep->stop_cmds_pending--;
  593. if (xhci->xhc_state & XHCI_STATE_DYING) {
  594. xhci_dbg(xhci, "Stop EP timer ran, but another timer marked "
  595. "xHCI as DYING, exiting.\n");
  596. spin_unlock(&xhci->lock);
  597. return;
  598. }
  599. if (!(ep->stop_cmds_pending == 0 && (ep->ep_state & EP_HALT_PENDING))) {
  600. xhci_dbg(xhci, "Stop EP timer ran, but no command pending, "
  601. "exiting.\n");
  602. spin_unlock(&xhci->lock);
  603. return;
  604. }
  605. xhci_warn(xhci, "xHCI host not responding to stop endpoint command.\n");
  606. xhci_warn(xhci, "Assuming host is dying, halting host.\n");
  607. /* Oops, HC is dead or dying or at least not responding to the stop
  608. * endpoint command.
  609. */
  610. xhci->xhc_state |= XHCI_STATE_DYING;
  611. /* Disable interrupts from the host controller and start halting it */
  612. xhci_quiesce(xhci);
  613. spin_unlock(&xhci->lock);
  614. ret = xhci_halt(xhci);
  615. spin_lock(&xhci->lock);
  616. if (ret < 0) {
  617. /* This is bad; the host is not responding to commands and it's
  618. * not allowing itself to be halted. At least interrupts are
  619. * disabled, so we can set HC_STATE_HALT and notify the
  620. * USB core. But if we call usb_hc_died(), it will attempt to
  621. * disconnect all device drivers under this host. Those
  622. * disconnect() methods will wait for all URBs to be unlinked,
  623. * so we must complete them.
  624. */
  625. xhci_warn(xhci, "Non-responsive xHCI host is not halting.\n");
  626. xhci_warn(xhci, "Completing active URBs anyway.\n");
  627. /* We could turn all TDs on the rings to no-ops. This won't
  628. * help if the host has cached part of the ring, and is slow if
  629. * we want to preserve the cycle bit. Skip it and hope the host
  630. * doesn't touch the memory.
  631. */
  632. }
  633. for (i = 0; i < MAX_HC_SLOTS; i++) {
  634. if (!xhci->devs[i])
  635. continue;
  636. for (j = 0; j < 31; j++) {
  637. temp_ep = &xhci->devs[i]->eps[j];
  638. ring = temp_ep->ring;
  639. if (!ring)
  640. continue;
  641. xhci_dbg(xhci, "Killing URBs for slot ID %u, "
  642. "ep index %u\n", i, j);
  643. while (!list_empty(&ring->td_list)) {
  644. cur_td = list_first_entry(&ring->td_list,
  645. struct xhci_td,
  646. td_list);
  647. list_del(&cur_td->td_list);
  648. if (!list_empty(&cur_td->cancelled_td_list))
  649. list_del(&cur_td->cancelled_td_list);
  650. xhci_giveback_urb_in_irq(xhci, cur_td,
  651. -ESHUTDOWN, "killed");
  652. }
  653. while (!list_empty(&temp_ep->cancelled_td_list)) {
  654. cur_td = list_first_entry(
  655. &temp_ep->cancelled_td_list,
  656. struct xhci_td,
  657. cancelled_td_list);
  658. list_del(&cur_td->cancelled_td_list);
  659. xhci_giveback_urb_in_irq(xhci, cur_td,
  660. -ESHUTDOWN, "killed");
  661. }
  662. }
  663. }
  664. spin_unlock(&xhci->lock);
  665. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  666. xhci_dbg(xhci, "Calling usb_hc_died()\n");
  667. usb_hc_died(xhci_to_hcd(xhci));
  668. xhci_dbg(xhci, "xHCI host controller is dead.\n");
  669. }
  670. /*
  671. * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
  672. * we need to clear the set deq pending flag in the endpoint ring state, so that
  673. * the TD queueing code can ring the doorbell again. We also need to ring the
  674. * endpoint doorbell to restart the ring, but only if there aren't more
  675. * cancellations pending.
  676. */
  677. static void handle_set_deq_completion(struct xhci_hcd *xhci,
  678. struct xhci_event_cmd *event,
  679. union xhci_trb *trb)
  680. {
  681. unsigned int slot_id;
  682. unsigned int ep_index;
  683. struct xhci_ring *ep_ring;
  684. struct xhci_virt_device *dev;
  685. struct xhci_ep_ctx *ep_ctx;
  686. struct xhci_slot_ctx *slot_ctx;
  687. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  688. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  689. dev = xhci->devs[slot_id];
  690. ep_ring = dev->eps[ep_index].ring;
  691. ep_ctx = xhci_get_ep_ctx(xhci, dev->out_ctx, ep_index);
  692. slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
  693. if (GET_COMP_CODE(event->status) != COMP_SUCCESS) {
  694. unsigned int ep_state;
  695. unsigned int slot_state;
  696. switch (GET_COMP_CODE(event->status)) {
  697. case COMP_TRB_ERR:
  698. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
  699. "of stream ID configuration\n");
  700. break;
  701. case COMP_CTX_STATE:
  702. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
  703. "to incorrect slot or ep state.\n");
  704. ep_state = ep_ctx->ep_info;
  705. ep_state &= EP_STATE_MASK;
  706. slot_state = slot_ctx->dev_state;
  707. slot_state = GET_SLOT_STATE(slot_state);
  708. xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
  709. slot_state, ep_state);
  710. break;
  711. case COMP_EBADSLT:
  712. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
  713. "slot %u was not enabled.\n", slot_id);
  714. break;
  715. default:
  716. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
  717. "completion code of %u.\n",
  718. GET_COMP_CODE(event->status));
  719. break;
  720. }
  721. /* OK what do we do now? The endpoint state is hosed, and we
  722. * should never get to this point if the synchronization between
  723. * queueing, and endpoint state are correct. This might happen
  724. * if the device gets disconnected after we've finished
  725. * cancelling URBs, which might not be an error...
  726. */
  727. } else {
  728. xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq = @%08llx\n",
  729. ep_ctx->deq);
  730. }
  731. dev->eps[ep_index].ep_state &= ~SET_DEQ_PENDING;
  732. ring_ep_doorbell(xhci, slot_id, ep_index);
  733. }
  734. static void handle_reset_ep_completion(struct xhci_hcd *xhci,
  735. struct xhci_event_cmd *event,
  736. union xhci_trb *trb)
  737. {
  738. int slot_id;
  739. unsigned int ep_index;
  740. struct xhci_ring *ep_ring;
  741. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  742. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  743. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  744. /* This command will only fail if the endpoint wasn't halted,
  745. * but we don't care.
  746. */
  747. xhci_dbg(xhci, "Ignoring reset ep completion code of %u\n",
  748. (unsigned int) GET_COMP_CODE(event->status));
  749. /* HW with the reset endpoint quirk needs to have a configure endpoint
  750. * command complete before the endpoint can be used. Queue that here
  751. * because the HW can't handle two commands being queued in a row.
  752. */
  753. if (xhci->quirks & XHCI_RESET_EP_QUIRK) {
  754. xhci_dbg(xhci, "Queueing configure endpoint command\n");
  755. xhci_queue_configure_endpoint(xhci,
  756. xhci->devs[slot_id]->in_ctx->dma, slot_id,
  757. false);
  758. xhci_ring_cmd_db(xhci);
  759. } else {
  760. /* Clear our internal halted state and restart the ring */
  761. xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_HALTED;
  762. ring_ep_doorbell(xhci, slot_id, ep_index);
  763. }
  764. }
  765. /* Check to see if a command in the device's command queue matches this one.
  766. * Signal the completion or free the command, and return 1. Return 0 if the
  767. * completed command isn't at the head of the command list.
  768. */
  769. static int handle_cmd_in_cmd_wait_list(struct xhci_hcd *xhci,
  770. struct xhci_virt_device *virt_dev,
  771. struct xhci_event_cmd *event)
  772. {
  773. struct xhci_command *command;
  774. if (list_empty(&virt_dev->cmd_list))
  775. return 0;
  776. command = list_entry(virt_dev->cmd_list.next,
  777. struct xhci_command, cmd_list);
  778. if (xhci->cmd_ring->dequeue != command->command_trb)
  779. return 0;
  780. command->status =
  781. GET_COMP_CODE(event->status);
  782. list_del(&command->cmd_list);
  783. if (command->completion)
  784. complete(command->completion);
  785. else
  786. xhci_free_command(xhci, command);
  787. return 1;
  788. }
  789. static void handle_cmd_completion(struct xhci_hcd *xhci,
  790. struct xhci_event_cmd *event)
  791. {
  792. int slot_id = TRB_TO_SLOT_ID(event->flags);
  793. u64 cmd_dma;
  794. dma_addr_t cmd_dequeue_dma;
  795. struct xhci_input_control_ctx *ctrl_ctx;
  796. struct xhci_virt_device *virt_dev;
  797. unsigned int ep_index;
  798. struct xhci_ring *ep_ring;
  799. unsigned int ep_state;
  800. cmd_dma = event->cmd_trb;
  801. cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  802. xhci->cmd_ring->dequeue);
  803. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  804. if (cmd_dequeue_dma == 0) {
  805. xhci->error_bitmask |= 1 << 4;
  806. return;
  807. }
  808. /* Does the DMA address match our internal dequeue pointer address? */
  809. if (cmd_dma != (u64) cmd_dequeue_dma) {
  810. xhci->error_bitmask |= 1 << 5;
  811. return;
  812. }
  813. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  814. case TRB_TYPE(TRB_ENABLE_SLOT):
  815. if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
  816. xhci->slot_id = slot_id;
  817. else
  818. xhci->slot_id = 0;
  819. complete(&xhci->addr_dev);
  820. break;
  821. case TRB_TYPE(TRB_DISABLE_SLOT):
  822. if (xhci->devs[slot_id])
  823. xhci_free_virt_device(xhci, slot_id);
  824. break;
  825. case TRB_TYPE(TRB_CONFIG_EP):
  826. virt_dev = xhci->devs[slot_id];
  827. if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
  828. break;
  829. /*
  830. * Configure endpoint commands can come from the USB core
  831. * configuration or alt setting changes, or because the HW
  832. * needed an extra configure endpoint command after a reset
  833. * endpoint command. In the latter case, the xHCI driver is
  834. * not waiting on the configure endpoint command.
  835. */
  836. ctrl_ctx = xhci_get_input_control_ctx(xhci,
  837. virt_dev->in_ctx);
  838. /* Input ctx add_flags are the endpoint index plus one */
  839. ep_index = xhci_last_valid_endpoint(ctrl_ctx->add_flags) - 1;
  840. /* A usb_set_interface() call directly after clearing a halted
  841. * condition may race on this quirky hardware.
  842. * Not worth worrying about, since this is prototype hardware.
  843. */
  844. if (xhci->quirks & XHCI_RESET_EP_QUIRK &&
  845. ep_index != (unsigned int) -1 &&
  846. ctrl_ctx->add_flags - SLOT_FLAG ==
  847. ctrl_ctx->drop_flags) {
  848. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  849. ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
  850. if (!(ep_state & EP_HALTED))
  851. goto bandwidth_change;
  852. xhci_dbg(xhci, "Completed config ep cmd - "
  853. "last ep index = %d, state = %d\n",
  854. ep_index, ep_state);
  855. /* Clear our internal halted state and restart ring */
  856. xhci->devs[slot_id]->eps[ep_index].ep_state &=
  857. ~EP_HALTED;
  858. ring_ep_doorbell(xhci, slot_id, ep_index);
  859. break;
  860. }
  861. bandwidth_change:
  862. xhci_dbg(xhci, "Completed config ep cmd\n");
  863. xhci->devs[slot_id]->cmd_status =
  864. GET_COMP_CODE(event->status);
  865. complete(&xhci->devs[slot_id]->cmd_completion);
  866. break;
  867. case TRB_TYPE(TRB_EVAL_CONTEXT):
  868. virt_dev = xhci->devs[slot_id];
  869. if (handle_cmd_in_cmd_wait_list(xhci, virt_dev, event))
  870. break;
  871. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  872. complete(&xhci->devs[slot_id]->cmd_completion);
  873. break;
  874. case TRB_TYPE(TRB_ADDR_DEV):
  875. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  876. complete(&xhci->addr_dev);
  877. break;
  878. case TRB_TYPE(TRB_STOP_RING):
  879. handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue);
  880. break;
  881. case TRB_TYPE(TRB_SET_DEQ):
  882. handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
  883. break;
  884. case TRB_TYPE(TRB_CMD_NOOP):
  885. ++xhci->noops_handled;
  886. break;
  887. case TRB_TYPE(TRB_RESET_EP):
  888. handle_reset_ep_completion(xhci, event, xhci->cmd_ring->dequeue);
  889. break;
  890. case TRB_TYPE(TRB_RESET_DEV):
  891. xhci_dbg(xhci, "Completed reset device command.\n");
  892. slot_id = TRB_TO_SLOT_ID(
  893. xhci->cmd_ring->dequeue->generic.field[3]);
  894. virt_dev = xhci->devs[slot_id];
  895. if (virt_dev)
  896. handle_cmd_in_cmd_wait_list(xhci, virt_dev, event);
  897. else
  898. xhci_warn(xhci, "Reset device command completion "
  899. "for disabled slot %u\n", slot_id);
  900. break;
  901. default:
  902. /* Skip over unknown commands on the event ring */
  903. xhci->error_bitmask |= 1 << 6;
  904. break;
  905. }
  906. inc_deq(xhci, xhci->cmd_ring, false);
  907. }
  908. static void handle_port_status(struct xhci_hcd *xhci,
  909. union xhci_trb *event)
  910. {
  911. u32 port_id;
  912. /* Port status change events always have a successful completion code */
  913. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  914. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  915. xhci->error_bitmask |= 1 << 8;
  916. }
  917. /* FIXME: core doesn't care about all port link state changes yet */
  918. port_id = GET_PORT_ID(event->generic.field[0]);
  919. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  920. /* Update event ring dequeue pointer before dropping the lock */
  921. inc_deq(xhci, xhci->event_ring, true);
  922. xhci_set_hc_event_deq(xhci);
  923. spin_unlock(&xhci->lock);
  924. /* Pass this up to the core */
  925. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  926. spin_lock(&xhci->lock);
  927. }
  928. /*
  929. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  930. * at end_trb, which may be in another segment. If the suspect DMA address is a
  931. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  932. * returns 0.
  933. */
  934. struct xhci_segment *trb_in_td(struct xhci_segment *start_seg,
  935. union xhci_trb *start_trb,
  936. union xhci_trb *end_trb,
  937. dma_addr_t suspect_dma)
  938. {
  939. dma_addr_t start_dma;
  940. dma_addr_t end_seg_dma;
  941. dma_addr_t end_trb_dma;
  942. struct xhci_segment *cur_seg;
  943. start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
  944. cur_seg = start_seg;
  945. do {
  946. if (start_dma == 0)
  947. return 0;
  948. /* We may get an event for a Link TRB in the middle of a TD */
  949. end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
  950. &cur_seg->trbs[TRBS_PER_SEGMENT - 1]);
  951. /* If the end TRB isn't in this segment, this is set to 0 */
  952. end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
  953. if (end_trb_dma > 0) {
  954. /* The end TRB is in this segment, so suspect should be here */
  955. if (start_dma <= end_trb_dma) {
  956. if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
  957. return cur_seg;
  958. } else {
  959. /* Case for one segment with
  960. * a TD wrapped around to the top
  961. */
  962. if ((suspect_dma >= start_dma &&
  963. suspect_dma <= end_seg_dma) ||
  964. (suspect_dma >= cur_seg->dma &&
  965. suspect_dma <= end_trb_dma))
  966. return cur_seg;
  967. }
  968. return 0;
  969. } else {
  970. /* Might still be somewhere in this segment */
  971. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  972. return cur_seg;
  973. }
  974. cur_seg = cur_seg->next;
  975. start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  976. } while (cur_seg != start_seg);
  977. return 0;
  978. }
  979. static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
  980. unsigned int slot_id, unsigned int ep_index,
  981. struct xhci_td *td, union xhci_trb *event_trb)
  982. {
  983. struct xhci_virt_ep *ep = &xhci->devs[slot_id]->eps[ep_index];
  984. ep->ep_state |= EP_HALTED;
  985. ep->stopped_td = td;
  986. ep->stopped_trb = event_trb;
  987. xhci_queue_reset_ep(xhci, slot_id, ep_index);
  988. xhci_cleanup_stalled_ring(xhci, td->urb->dev, ep_index);
  989. xhci_ring_cmd_db(xhci);
  990. }
  991. /* Check if an error has halted the endpoint ring. The class driver will
  992. * cleanup the halt for a non-default control endpoint if we indicate a stall.
  993. * However, a babble and other errors also halt the endpoint ring, and the class
  994. * driver won't clear the halt in that case, so we need to issue a Set Transfer
  995. * Ring Dequeue Pointer command manually.
  996. */
  997. static int xhci_requires_manual_halt_cleanup(struct xhci_hcd *xhci,
  998. struct xhci_ep_ctx *ep_ctx,
  999. unsigned int trb_comp_code)
  1000. {
  1001. /* TRB completion codes that may require a manual halt cleanup */
  1002. if (trb_comp_code == COMP_TX_ERR ||
  1003. trb_comp_code == COMP_BABBLE ||
  1004. trb_comp_code == COMP_SPLIT_ERR)
  1005. /* The 0.96 spec says a babbling control endpoint
  1006. * is not halted. The 0.96 spec says it is. Some HW
  1007. * claims to be 0.95 compliant, but it halts the control
  1008. * endpoint anyway. Check if a babble halted the
  1009. * endpoint.
  1010. */
  1011. if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_HALTED)
  1012. return 1;
  1013. return 0;
  1014. }
  1015. int xhci_is_vendor_info_code(struct xhci_hcd *xhci, unsigned int trb_comp_code)
  1016. {
  1017. if (trb_comp_code >= 224 && trb_comp_code <= 255) {
  1018. /* Vendor defined "informational" completion code,
  1019. * treat as not-an-error.
  1020. */
  1021. xhci_dbg(xhci, "Vendor defined info completion code %u\n",
  1022. trb_comp_code);
  1023. xhci_dbg(xhci, "Treating code as success.\n");
  1024. return 1;
  1025. }
  1026. return 0;
  1027. }
  1028. /*
  1029. * If this function returns an error condition, it means it got a Transfer
  1030. * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
  1031. * At this point, the host controller is probably hosed and should be reset.
  1032. */
  1033. static int handle_tx_event(struct xhci_hcd *xhci,
  1034. struct xhci_transfer_event *event)
  1035. {
  1036. struct xhci_virt_device *xdev;
  1037. struct xhci_virt_ep *ep;
  1038. struct xhci_ring *ep_ring;
  1039. unsigned int slot_id;
  1040. int ep_index;
  1041. struct xhci_td *td = 0;
  1042. dma_addr_t event_dma;
  1043. struct xhci_segment *event_seg;
  1044. union xhci_trb *event_trb;
  1045. struct urb *urb = 0;
  1046. int status = -EINPROGRESS;
  1047. struct xhci_ep_ctx *ep_ctx;
  1048. u32 trb_comp_code;
  1049. xhci_dbg(xhci, "In %s\n", __func__);
  1050. slot_id = TRB_TO_SLOT_ID(event->flags);
  1051. xdev = xhci->devs[slot_id];
  1052. if (!xdev) {
  1053. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  1054. return -ENODEV;
  1055. }
  1056. /* Endpoint ID is 1 based, our index is zero based */
  1057. ep_index = TRB_TO_EP_ID(event->flags) - 1;
  1058. xhci_dbg(xhci, "%s - ep index = %d\n", __func__, ep_index);
  1059. ep = &xdev->eps[ep_index];
  1060. ep_ring = ep->ring;
  1061. ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1062. if (!ep_ring || (ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
  1063. xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n");
  1064. return -ENODEV;
  1065. }
  1066. event_dma = event->buffer;
  1067. /* This TRB should be in the TD at the head of this ring's TD list */
  1068. xhci_dbg(xhci, "%s - checking for list empty\n", __func__);
  1069. if (list_empty(&ep_ring->td_list)) {
  1070. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  1071. TRB_TO_SLOT_ID(event->flags), ep_index);
  1072. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  1073. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  1074. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  1075. urb = NULL;
  1076. goto cleanup;
  1077. }
  1078. xhci_dbg(xhci, "%s - getting list entry\n", __func__);
  1079. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  1080. /* Is this a TRB in the currently executing TD? */
  1081. xhci_dbg(xhci, "%s - looking for TD\n", __func__);
  1082. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  1083. td->last_trb, event_dma);
  1084. xhci_dbg(xhci, "%s - found event_seg = %p\n", __func__, event_seg);
  1085. if (!event_seg) {
  1086. /* HC is busted, give up! */
  1087. xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
  1088. return -ESHUTDOWN;
  1089. }
  1090. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
  1091. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  1092. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  1093. xhci_dbg(xhci, "Offset 0x00 (buffer lo) = 0x%x\n",
  1094. lower_32_bits(event->buffer));
  1095. xhci_dbg(xhci, "Offset 0x04 (buffer hi) = 0x%x\n",
  1096. upper_32_bits(event->buffer));
  1097. xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
  1098. (unsigned int) event->transfer_len);
  1099. xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
  1100. (unsigned int) event->flags);
  1101. /* Look for common error cases */
  1102. trb_comp_code = GET_COMP_CODE(event->transfer_len);
  1103. switch (trb_comp_code) {
  1104. /* Skip codes that require special handling depending on
  1105. * transfer type
  1106. */
  1107. case COMP_SUCCESS:
  1108. case COMP_SHORT_TX:
  1109. break;
  1110. case COMP_STOP:
  1111. xhci_dbg(xhci, "Stopped on Transfer TRB\n");
  1112. break;
  1113. case COMP_STOP_INVAL:
  1114. xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
  1115. break;
  1116. case COMP_STALL:
  1117. xhci_warn(xhci, "WARN: Stalled endpoint\n");
  1118. ep->ep_state |= EP_HALTED;
  1119. status = -EPIPE;
  1120. break;
  1121. case COMP_TRB_ERR:
  1122. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  1123. status = -EILSEQ;
  1124. break;
  1125. case COMP_SPLIT_ERR:
  1126. case COMP_TX_ERR:
  1127. xhci_warn(xhci, "WARN: transfer error on endpoint\n");
  1128. status = -EPROTO;
  1129. break;
  1130. case COMP_BABBLE:
  1131. xhci_warn(xhci, "WARN: babble error on endpoint\n");
  1132. status = -EOVERFLOW;
  1133. break;
  1134. case COMP_DB_ERR:
  1135. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  1136. status = -ENOSR;
  1137. break;
  1138. default:
  1139. if (xhci_is_vendor_info_code(xhci, trb_comp_code)) {
  1140. status = 0;
  1141. break;
  1142. }
  1143. xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
  1144. urb = NULL;
  1145. goto cleanup;
  1146. }
  1147. /* Now update the urb's actual_length and give back to the core */
  1148. /* Was this a control transfer? */
  1149. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  1150. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  1151. switch (trb_comp_code) {
  1152. case COMP_SUCCESS:
  1153. if (event_trb == ep_ring->dequeue) {
  1154. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  1155. status = -ESHUTDOWN;
  1156. } else if (event_trb != td->last_trb) {
  1157. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  1158. status = -ESHUTDOWN;
  1159. } else {
  1160. xhci_dbg(xhci, "Successful control transfer!\n");
  1161. status = 0;
  1162. }
  1163. break;
  1164. case COMP_SHORT_TX:
  1165. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  1166. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1167. status = -EREMOTEIO;
  1168. else
  1169. status = 0;
  1170. break;
  1171. default:
  1172. if (!xhci_requires_manual_halt_cleanup(xhci,
  1173. ep_ctx, trb_comp_code))
  1174. break;
  1175. xhci_dbg(xhci, "TRB error code %u, "
  1176. "halted endpoint index = %u\n",
  1177. trb_comp_code, ep_index);
  1178. /* else fall through */
  1179. case COMP_STALL:
  1180. /* Did we transfer part of the data (middle) phase? */
  1181. if (event_trb != ep_ring->dequeue &&
  1182. event_trb != td->last_trb)
  1183. td->urb->actual_length =
  1184. td->urb->transfer_buffer_length
  1185. - TRB_LEN(event->transfer_len);
  1186. else
  1187. td->urb->actual_length = 0;
  1188. xhci_cleanup_halted_endpoint(xhci,
  1189. slot_id, ep_index, td, event_trb);
  1190. goto td_cleanup;
  1191. }
  1192. /*
  1193. * Did we transfer any data, despite the errors that might have
  1194. * happened? I.e. did we get past the setup stage?
  1195. */
  1196. if (event_trb != ep_ring->dequeue) {
  1197. /* The event was for the status stage */
  1198. if (event_trb == td->last_trb) {
  1199. if (td->urb->actual_length != 0) {
  1200. /* Don't overwrite a previously set error code */
  1201. if ((status == -EINPROGRESS ||
  1202. status == 0) &&
  1203. (td->urb->transfer_flags
  1204. & URB_SHORT_NOT_OK))
  1205. /* Did we already see a short data stage? */
  1206. status = -EREMOTEIO;
  1207. } else {
  1208. td->urb->actual_length =
  1209. td->urb->transfer_buffer_length;
  1210. }
  1211. } else {
  1212. /* Maybe the event was for the data stage? */
  1213. if (trb_comp_code != COMP_STOP_INVAL) {
  1214. /* We didn't stop on a link TRB in the middle */
  1215. td->urb->actual_length =
  1216. td->urb->transfer_buffer_length -
  1217. TRB_LEN(event->transfer_len);
  1218. xhci_dbg(xhci, "Waiting for status stage event\n");
  1219. urb = NULL;
  1220. goto cleanup;
  1221. }
  1222. }
  1223. }
  1224. } else {
  1225. switch (trb_comp_code) {
  1226. case COMP_SUCCESS:
  1227. /* Double check that the HW transferred everything. */
  1228. if (event_trb != td->last_trb) {
  1229. xhci_warn(xhci, "WARN Successful completion "
  1230. "on short TX\n");
  1231. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1232. status = -EREMOTEIO;
  1233. else
  1234. status = 0;
  1235. } else {
  1236. if (usb_endpoint_xfer_bulk(&td->urb->ep->desc))
  1237. xhci_dbg(xhci, "Successful bulk "
  1238. "transfer!\n");
  1239. else
  1240. xhci_dbg(xhci, "Successful interrupt "
  1241. "transfer!\n");
  1242. status = 0;
  1243. }
  1244. break;
  1245. case COMP_SHORT_TX:
  1246. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1247. status = -EREMOTEIO;
  1248. else
  1249. status = 0;
  1250. break;
  1251. default:
  1252. /* Others already handled above */
  1253. break;
  1254. }
  1255. dev_dbg(&td->urb->dev->dev,
  1256. "ep %#x - asked for %d bytes, "
  1257. "%d bytes untransferred\n",
  1258. td->urb->ep->desc.bEndpointAddress,
  1259. td->urb->transfer_buffer_length,
  1260. TRB_LEN(event->transfer_len));
  1261. /* Fast path - was this the last TRB in the TD for this URB? */
  1262. if (event_trb == td->last_trb) {
  1263. if (TRB_LEN(event->transfer_len) != 0) {
  1264. td->urb->actual_length =
  1265. td->urb->transfer_buffer_length -
  1266. TRB_LEN(event->transfer_len);
  1267. if (td->urb->transfer_buffer_length <
  1268. td->urb->actual_length) {
  1269. xhci_warn(xhci, "HC gave bad length "
  1270. "of %d bytes left\n",
  1271. TRB_LEN(event->transfer_len));
  1272. td->urb->actual_length = 0;
  1273. if (td->urb->transfer_flags &
  1274. URB_SHORT_NOT_OK)
  1275. status = -EREMOTEIO;
  1276. else
  1277. status = 0;
  1278. }
  1279. /* Don't overwrite a previously set error code */
  1280. if (status == -EINPROGRESS) {
  1281. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1282. status = -EREMOTEIO;
  1283. else
  1284. status = 0;
  1285. }
  1286. } else {
  1287. td->urb->actual_length = td->urb->transfer_buffer_length;
  1288. /* Ignore a short packet completion if the
  1289. * untransferred length was zero.
  1290. */
  1291. if (status == -EREMOTEIO)
  1292. status = 0;
  1293. }
  1294. } else {
  1295. /* Slow path - walk the list, starting from the dequeue
  1296. * pointer, to get the actual length transferred.
  1297. */
  1298. union xhci_trb *cur_trb;
  1299. struct xhci_segment *cur_seg;
  1300. td->urb->actual_length = 0;
  1301. for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
  1302. cur_trb != event_trb;
  1303. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  1304. if (TRB_TYPE(cur_trb->generic.field[3]) != TRB_TR_NOOP &&
  1305. TRB_TYPE(cur_trb->generic.field[3]) != TRB_LINK)
  1306. td->urb->actual_length +=
  1307. TRB_LEN(cur_trb->generic.field[2]);
  1308. }
  1309. /* If the ring didn't stop on a Link or No-op TRB, add
  1310. * in the actual bytes transferred from the Normal TRB
  1311. */
  1312. if (trb_comp_code != COMP_STOP_INVAL)
  1313. td->urb->actual_length +=
  1314. TRB_LEN(cur_trb->generic.field[2]) -
  1315. TRB_LEN(event->transfer_len);
  1316. }
  1317. }
  1318. if (trb_comp_code == COMP_STOP_INVAL ||
  1319. trb_comp_code == COMP_STOP) {
  1320. /* The Endpoint Stop Command completion will take care of any
  1321. * stopped TDs. A stopped TD may be restarted, so don't update
  1322. * the ring dequeue pointer or take this TD off any lists yet.
  1323. */
  1324. ep->stopped_td = td;
  1325. ep->stopped_trb = event_trb;
  1326. } else {
  1327. if (trb_comp_code == COMP_STALL) {
  1328. /* The transfer is completed from the driver's
  1329. * perspective, but we need to issue a set dequeue
  1330. * command for this stalled endpoint to move the dequeue
  1331. * pointer past the TD. We can't do that here because
  1332. * the halt condition must be cleared first. Let the
  1333. * USB class driver clear the stall later.
  1334. */
  1335. ep->stopped_td = td;
  1336. ep->stopped_trb = event_trb;
  1337. } else if (xhci_requires_manual_halt_cleanup(xhci,
  1338. ep_ctx, trb_comp_code)) {
  1339. /* Other types of errors halt the endpoint, but the
  1340. * class driver doesn't call usb_reset_endpoint() unless
  1341. * the error is -EPIPE. Clear the halted status in the
  1342. * xHCI hardware manually.
  1343. */
  1344. xhci_cleanup_halted_endpoint(xhci,
  1345. slot_id, ep_index, td, event_trb);
  1346. } else {
  1347. /* Update ring dequeue pointer */
  1348. while (ep_ring->dequeue != td->last_trb)
  1349. inc_deq(xhci, ep_ring, false);
  1350. inc_deq(xhci, ep_ring, false);
  1351. }
  1352. td_cleanup:
  1353. /* Clean up the endpoint's TD list */
  1354. urb = td->urb;
  1355. /* Do one last check of the actual transfer length.
  1356. * If the host controller said we transferred more data than
  1357. * the buffer length, urb->actual_length will be a very big
  1358. * number (since it's unsigned). Play it safe and say we didn't
  1359. * transfer anything.
  1360. */
  1361. if (urb->actual_length > urb->transfer_buffer_length) {
  1362. xhci_warn(xhci, "URB transfer length is wrong, "
  1363. "xHC issue? req. len = %u, "
  1364. "act. len = %u\n",
  1365. urb->transfer_buffer_length,
  1366. urb->actual_length);
  1367. urb->actual_length = 0;
  1368. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  1369. status = -EREMOTEIO;
  1370. else
  1371. status = 0;
  1372. }
  1373. list_del(&td->td_list);
  1374. /* Was this TD slated to be cancelled but completed anyway? */
  1375. if (!list_empty(&td->cancelled_td_list))
  1376. list_del(&td->cancelled_td_list);
  1377. /* Leave the TD around for the reset endpoint function to use
  1378. * (but only if it's not a control endpoint, since we already
  1379. * queued the Set TR dequeue pointer command for stalled
  1380. * control endpoints).
  1381. */
  1382. if (usb_endpoint_xfer_control(&urb->ep->desc) ||
  1383. (trb_comp_code != COMP_STALL &&
  1384. trb_comp_code != COMP_BABBLE)) {
  1385. kfree(td);
  1386. }
  1387. urb->hcpriv = NULL;
  1388. }
  1389. cleanup:
  1390. inc_deq(xhci, xhci->event_ring, true);
  1391. xhci_set_hc_event_deq(xhci);
  1392. /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
  1393. if (urb) {
  1394. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  1395. xhci_dbg(xhci, "Giveback URB %p, len = %d, status = %d\n",
  1396. urb, urb->actual_length, status);
  1397. spin_unlock(&xhci->lock);
  1398. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  1399. spin_lock(&xhci->lock);
  1400. }
  1401. return 0;
  1402. }
  1403. /*
  1404. * This function handles all OS-owned events on the event ring. It may drop
  1405. * xhci->lock between event processing (e.g. to pass up port status changes).
  1406. */
  1407. void xhci_handle_event(struct xhci_hcd *xhci)
  1408. {
  1409. union xhci_trb *event;
  1410. int update_ptrs = 1;
  1411. int ret;
  1412. xhci_dbg(xhci, "In %s\n", __func__);
  1413. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  1414. xhci->error_bitmask |= 1 << 1;
  1415. return;
  1416. }
  1417. event = xhci->event_ring->dequeue;
  1418. /* Does the HC or OS own the TRB? */
  1419. if ((event->event_cmd.flags & TRB_CYCLE) !=
  1420. xhci->event_ring->cycle_state) {
  1421. xhci->error_bitmask |= 1 << 2;
  1422. return;
  1423. }
  1424. xhci_dbg(xhci, "%s - OS owns TRB\n", __func__);
  1425. /* FIXME: Handle more event types. */
  1426. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  1427. case TRB_TYPE(TRB_COMPLETION):
  1428. xhci_dbg(xhci, "%s - calling handle_cmd_completion\n", __func__);
  1429. handle_cmd_completion(xhci, &event->event_cmd);
  1430. xhci_dbg(xhci, "%s - returned from handle_cmd_completion\n", __func__);
  1431. break;
  1432. case TRB_TYPE(TRB_PORT_STATUS):
  1433. xhci_dbg(xhci, "%s - calling handle_port_status\n", __func__);
  1434. handle_port_status(xhci, event);
  1435. xhci_dbg(xhci, "%s - returned from handle_port_status\n", __func__);
  1436. update_ptrs = 0;
  1437. break;
  1438. case TRB_TYPE(TRB_TRANSFER):
  1439. xhci_dbg(xhci, "%s - calling handle_tx_event\n", __func__);
  1440. ret = handle_tx_event(xhci, &event->trans_event);
  1441. xhci_dbg(xhci, "%s - returned from handle_tx_event\n", __func__);
  1442. if (ret < 0)
  1443. xhci->error_bitmask |= 1 << 9;
  1444. else
  1445. update_ptrs = 0;
  1446. break;
  1447. default:
  1448. xhci->error_bitmask |= 1 << 3;
  1449. }
  1450. /* Any of the above functions may drop and re-acquire the lock, so check
  1451. * to make sure a watchdog timer didn't mark the host as non-responsive.
  1452. */
  1453. if (xhci->xhc_state & XHCI_STATE_DYING) {
  1454. xhci_dbg(xhci, "xHCI host dying, returning from "
  1455. "event handler.\n");
  1456. return;
  1457. }
  1458. if (update_ptrs) {
  1459. /* Update SW and HC event ring dequeue pointer */
  1460. inc_deq(xhci, xhci->event_ring, true);
  1461. xhci_set_hc_event_deq(xhci);
  1462. }
  1463. /* Are there more items on the event ring? */
  1464. xhci_handle_event(xhci);
  1465. }
  1466. /**** Endpoint Ring Operations ****/
  1467. /*
  1468. * Generic function for queueing a TRB on a ring.
  1469. * The caller must have checked to make sure there's room on the ring.
  1470. */
  1471. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  1472. bool consumer,
  1473. u32 field1, u32 field2, u32 field3, u32 field4)
  1474. {
  1475. struct xhci_generic_trb *trb;
  1476. trb = &ring->enqueue->generic;
  1477. trb->field[0] = field1;
  1478. trb->field[1] = field2;
  1479. trb->field[2] = field3;
  1480. trb->field[3] = field4;
  1481. inc_enq(xhci, ring, consumer);
  1482. }
  1483. /*
  1484. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  1485. * FIXME allocate segments if the ring is full.
  1486. */
  1487. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  1488. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  1489. {
  1490. /* Make sure the endpoint has been added to xHC schedule */
  1491. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  1492. switch (ep_state) {
  1493. case EP_STATE_DISABLED:
  1494. /*
  1495. * USB core changed config/interfaces without notifying us,
  1496. * or hardware is reporting the wrong state.
  1497. */
  1498. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  1499. return -ENOENT;
  1500. case EP_STATE_ERROR:
  1501. xhci_warn(xhci, "WARN waiting for error on ep to be cleared\n");
  1502. /* FIXME event handling code for error needs to clear it */
  1503. /* XXX not sure if this should be -ENOENT or not */
  1504. return -EINVAL;
  1505. case EP_STATE_HALTED:
  1506. xhci_dbg(xhci, "WARN halted endpoint, queueing URB anyway.\n");
  1507. case EP_STATE_STOPPED:
  1508. case EP_STATE_RUNNING:
  1509. break;
  1510. default:
  1511. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  1512. /*
  1513. * FIXME issue Configure Endpoint command to try to get the HC
  1514. * back into a known state.
  1515. */
  1516. return -EINVAL;
  1517. }
  1518. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  1519. /* FIXME allocate more room */
  1520. xhci_err(xhci, "ERROR no room on ep ring\n");
  1521. return -ENOMEM;
  1522. }
  1523. return 0;
  1524. }
  1525. static int prepare_transfer(struct xhci_hcd *xhci,
  1526. struct xhci_virt_device *xdev,
  1527. unsigned int ep_index,
  1528. unsigned int num_trbs,
  1529. struct urb *urb,
  1530. struct xhci_td **td,
  1531. gfp_t mem_flags)
  1532. {
  1533. int ret;
  1534. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci, xdev->out_ctx, ep_index);
  1535. ret = prepare_ring(xhci, xdev->eps[ep_index].ring,
  1536. ep_ctx->ep_info & EP_STATE_MASK,
  1537. num_trbs, mem_flags);
  1538. if (ret)
  1539. return ret;
  1540. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  1541. if (!*td)
  1542. return -ENOMEM;
  1543. INIT_LIST_HEAD(&(*td)->td_list);
  1544. INIT_LIST_HEAD(&(*td)->cancelled_td_list);
  1545. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  1546. if (unlikely(ret)) {
  1547. kfree(*td);
  1548. return ret;
  1549. }
  1550. (*td)->urb = urb;
  1551. urb->hcpriv = (void *) (*td);
  1552. /* Add this TD to the tail of the endpoint ring's TD list */
  1553. list_add_tail(&(*td)->td_list, &xdev->eps[ep_index].ring->td_list);
  1554. (*td)->start_seg = xdev->eps[ep_index].ring->enq_seg;
  1555. (*td)->first_trb = xdev->eps[ep_index].ring->enqueue;
  1556. return 0;
  1557. }
  1558. static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
  1559. {
  1560. int num_sgs, num_trbs, running_total, temp, i;
  1561. struct scatterlist *sg;
  1562. sg = NULL;
  1563. num_sgs = urb->num_sgs;
  1564. temp = urb->transfer_buffer_length;
  1565. xhci_dbg(xhci, "count sg list trbs: \n");
  1566. num_trbs = 0;
  1567. for_each_sg(urb->sg->sg, sg, num_sgs, i) {
  1568. unsigned int previous_total_trbs = num_trbs;
  1569. unsigned int len = sg_dma_len(sg);
  1570. /* Scatter gather list entries may cross 64KB boundaries */
  1571. running_total = TRB_MAX_BUFF_SIZE -
  1572. (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1573. if (running_total != 0)
  1574. num_trbs++;
  1575. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1576. while (running_total < sg_dma_len(sg)) {
  1577. num_trbs++;
  1578. running_total += TRB_MAX_BUFF_SIZE;
  1579. }
  1580. xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
  1581. i, (unsigned long long)sg_dma_address(sg),
  1582. len, len, num_trbs - previous_total_trbs);
  1583. len = min_t(int, len, temp);
  1584. temp -= len;
  1585. if (temp == 0)
  1586. break;
  1587. }
  1588. xhci_dbg(xhci, "\n");
  1589. if (!in_interrupt())
  1590. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, sglist used, num_trbs = %d\n",
  1591. urb->ep->desc.bEndpointAddress,
  1592. urb->transfer_buffer_length,
  1593. num_trbs);
  1594. return num_trbs;
  1595. }
  1596. static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
  1597. {
  1598. if (num_trbs != 0)
  1599. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  1600. "TRBs, %d left\n", __func__,
  1601. urb->ep->desc.bEndpointAddress, num_trbs);
  1602. if (running_total != urb->transfer_buffer_length)
  1603. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
  1604. "queued %#x (%d), asked for %#x (%d)\n",
  1605. __func__,
  1606. urb->ep->desc.bEndpointAddress,
  1607. running_total, running_total,
  1608. urb->transfer_buffer_length,
  1609. urb->transfer_buffer_length);
  1610. }
  1611. static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
  1612. unsigned int ep_index, int start_cycle,
  1613. struct xhci_generic_trb *start_trb, struct xhci_td *td)
  1614. {
  1615. /*
  1616. * Pass all the TRBs to the hardware at once and make sure this write
  1617. * isn't reordered.
  1618. */
  1619. wmb();
  1620. start_trb->field[3] |= start_cycle;
  1621. ring_ep_doorbell(xhci, slot_id, ep_index);
  1622. }
  1623. /*
  1624. * xHCI uses normal TRBs for both bulk and interrupt. When the interrupt
  1625. * endpoint is to be serviced, the xHC will consume (at most) one TD. A TD
  1626. * (comprised of sg list entries) can take several service intervals to
  1627. * transmit.
  1628. */
  1629. int xhci_queue_intr_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1630. struct urb *urb, int slot_id, unsigned int ep_index)
  1631. {
  1632. struct xhci_ep_ctx *ep_ctx = xhci_get_ep_ctx(xhci,
  1633. xhci->devs[slot_id]->out_ctx, ep_index);
  1634. int xhci_interval;
  1635. int ep_interval;
  1636. xhci_interval = EP_INTERVAL_TO_UFRAMES(ep_ctx->ep_info);
  1637. ep_interval = urb->interval;
  1638. /* Convert to microframes */
  1639. if (urb->dev->speed == USB_SPEED_LOW ||
  1640. urb->dev->speed == USB_SPEED_FULL)
  1641. ep_interval *= 8;
  1642. /* FIXME change this to a warning and a suggestion to use the new API
  1643. * to set the polling interval (once the API is added).
  1644. */
  1645. if (xhci_interval != ep_interval) {
  1646. if (!printk_ratelimit())
  1647. dev_dbg(&urb->dev->dev, "Driver uses different interval"
  1648. " (%d microframe%s) than xHCI "
  1649. "(%d microframe%s)\n",
  1650. ep_interval,
  1651. ep_interval == 1 ? "" : "s",
  1652. xhci_interval,
  1653. xhci_interval == 1 ? "" : "s");
  1654. urb->interval = xhci_interval;
  1655. /* Convert back to frames for LS/FS devices */
  1656. if (urb->dev->speed == USB_SPEED_LOW ||
  1657. urb->dev->speed == USB_SPEED_FULL)
  1658. urb->interval /= 8;
  1659. }
  1660. return xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, slot_id, ep_index);
  1661. }
  1662. /*
  1663. * The TD size is the number of bytes remaining in the TD (including this TRB),
  1664. * right shifted by 10.
  1665. * It must fit in bits 21:17, so it can't be bigger than 31.
  1666. */
  1667. static u32 xhci_td_remainder(unsigned int remainder)
  1668. {
  1669. u32 max = (1 << (21 - 17 + 1)) - 1;
  1670. if ((remainder >> 10) >= max)
  1671. return max << 17;
  1672. else
  1673. return (remainder >> 10) << 17;
  1674. }
  1675. static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1676. struct urb *urb, int slot_id, unsigned int ep_index)
  1677. {
  1678. struct xhci_ring *ep_ring;
  1679. unsigned int num_trbs;
  1680. struct xhci_td *td;
  1681. struct scatterlist *sg;
  1682. int num_sgs;
  1683. int trb_buff_len, this_sg_len, running_total;
  1684. bool first_trb;
  1685. u64 addr;
  1686. struct xhci_generic_trb *start_trb;
  1687. int start_cycle;
  1688. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  1689. num_trbs = count_sg_trbs_needed(xhci, urb);
  1690. num_sgs = urb->num_sgs;
  1691. trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
  1692. ep_index, num_trbs, urb, &td, mem_flags);
  1693. if (trb_buff_len < 0)
  1694. return trb_buff_len;
  1695. /*
  1696. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1697. * until we've finished creating all the other TRBs. The ring's cycle
  1698. * state may change as we enqueue the other TRBs, so save it too.
  1699. */
  1700. start_trb = &ep_ring->enqueue->generic;
  1701. start_cycle = ep_ring->cycle_state;
  1702. running_total = 0;
  1703. /*
  1704. * How much data is in the first TRB?
  1705. *
  1706. * There are three forces at work for TRB buffer pointers and lengths:
  1707. * 1. We don't want to walk off the end of this sg-list entry buffer.
  1708. * 2. The transfer length that the driver requested may be smaller than
  1709. * the amount of memory allocated for this scatter-gather list.
  1710. * 3. TRBs buffers can't cross 64KB boundaries.
  1711. */
  1712. sg = urb->sg->sg;
  1713. addr = (u64) sg_dma_address(sg);
  1714. this_sg_len = sg_dma_len(sg);
  1715. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1716. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1717. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1718. if (trb_buff_len > urb->transfer_buffer_length)
  1719. trb_buff_len = urb->transfer_buffer_length;
  1720. xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
  1721. trb_buff_len);
  1722. first_trb = true;
  1723. /* Queue the first TRB, even if it's zero-length */
  1724. do {
  1725. u32 field = 0;
  1726. u32 length_field = 0;
  1727. u32 remainder = 0;
  1728. /* Don't change the cycle bit of the first TRB until later */
  1729. if (first_trb)
  1730. first_trb = false;
  1731. else
  1732. field |= ep_ring->cycle_state;
  1733. /* Chain all the TRBs together; clear the chain bit in the last
  1734. * TRB to indicate it's the last TRB in the chain.
  1735. */
  1736. if (num_trbs > 1) {
  1737. field |= TRB_CHAIN;
  1738. } else {
  1739. /* FIXME - add check for ZERO_PACKET flag before this */
  1740. td->last_trb = ep_ring->enqueue;
  1741. field |= TRB_IOC;
  1742. }
  1743. xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
  1744. "64KB boundary at %#x, end dma = %#x\n",
  1745. (unsigned int) addr, trb_buff_len, trb_buff_len,
  1746. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1747. (unsigned int) addr + trb_buff_len);
  1748. if (TRB_MAX_BUFF_SIZE -
  1749. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
  1750. xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
  1751. xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
  1752. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1753. (unsigned int) addr + trb_buff_len);
  1754. }
  1755. remainder = xhci_td_remainder(urb->transfer_buffer_length -
  1756. running_total) ;
  1757. length_field = TRB_LEN(trb_buff_len) |
  1758. remainder |
  1759. TRB_INTR_TARGET(0);
  1760. queue_trb(xhci, ep_ring, false,
  1761. lower_32_bits(addr),
  1762. upper_32_bits(addr),
  1763. length_field,
  1764. /* We always want to know if the TRB was short,
  1765. * or we won't get an event when it completes.
  1766. * (Unless we use event data TRBs, which are a
  1767. * waste of space and HC resources.)
  1768. */
  1769. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1770. --num_trbs;
  1771. running_total += trb_buff_len;
  1772. /* Calculate length for next transfer --
  1773. * Are we done queueing all the TRBs for this sg entry?
  1774. */
  1775. this_sg_len -= trb_buff_len;
  1776. if (this_sg_len == 0) {
  1777. --num_sgs;
  1778. if (num_sgs == 0)
  1779. break;
  1780. sg = sg_next(sg);
  1781. addr = (u64) sg_dma_address(sg);
  1782. this_sg_len = sg_dma_len(sg);
  1783. } else {
  1784. addr += trb_buff_len;
  1785. }
  1786. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1787. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1788. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1789. if (running_total + trb_buff_len > urb->transfer_buffer_length)
  1790. trb_buff_len =
  1791. urb->transfer_buffer_length - running_total;
  1792. } while (running_total < urb->transfer_buffer_length);
  1793. check_trb_math(urb, num_trbs, running_total);
  1794. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1795. return 0;
  1796. }
  1797. /* This is very similar to what ehci-q.c qtd_fill() does */
  1798. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1799. struct urb *urb, int slot_id, unsigned int ep_index)
  1800. {
  1801. struct xhci_ring *ep_ring;
  1802. struct xhci_td *td;
  1803. int num_trbs;
  1804. struct xhci_generic_trb *start_trb;
  1805. bool first_trb;
  1806. int start_cycle;
  1807. u32 field, length_field;
  1808. int running_total, trb_buff_len, ret;
  1809. u64 addr;
  1810. if (urb->sg)
  1811. return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
  1812. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  1813. num_trbs = 0;
  1814. /* How much data is (potentially) left before the 64KB boundary? */
  1815. running_total = TRB_MAX_BUFF_SIZE -
  1816. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1817. /* If there's some data on this 64KB chunk, or we have to send a
  1818. * zero-length transfer, we need at least one TRB
  1819. */
  1820. if (running_total != 0 || urb->transfer_buffer_length == 0)
  1821. num_trbs++;
  1822. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1823. while (running_total < urb->transfer_buffer_length) {
  1824. num_trbs++;
  1825. running_total += TRB_MAX_BUFF_SIZE;
  1826. }
  1827. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  1828. if (!in_interrupt())
  1829. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d), addr = %#llx, num_trbs = %d\n",
  1830. urb->ep->desc.bEndpointAddress,
  1831. urb->transfer_buffer_length,
  1832. urb->transfer_buffer_length,
  1833. (unsigned long long)urb->transfer_dma,
  1834. num_trbs);
  1835. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
  1836. num_trbs, urb, &td, mem_flags);
  1837. if (ret < 0)
  1838. return ret;
  1839. /*
  1840. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1841. * until we've finished creating all the other TRBs. The ring's cycle
  1842. * state may change as we enqueue the other TRBs, so save it too.
  1843. */
  1844. start_trb = &ep_ring->enqueue->generic;
  1845. start_cycle = ep_ring->cycle_state;
  1846. running_total = 0;
  1847. /* How much data is in the first TRB? */
  1848. addr = (u64) urb->transfer_dma;
  1849. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1850. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1851. if (urb->transfer_buffer_length < trb_buff_len)
  1852. trb_buff_len = urb->transfer_buffer_length;
  1853. first_trb = true;
  1854. /* Queue the first TRB, even if it's zero-length */
  1855. do {
  1856. u32 remainder = 0;
  1857. field = 0;
  1858. /* Don't change the cycle bit of the first TRB until later */
  1859. if (first_trb)
  1860. first_trb = false;
  1861. else
  1862. field |= ep_ring->cycle_state;
  1863. /* Chain all the TRBs together; clear the chain bit in the last
  1864. * TRB to indicate it's the last TRB in the chain.
  1865. */
  1866. if (num_trbs > 1) {
  1867. field |= TRB_CHAIN;
  1868. } else {
  1869. /* FIXME - add check for ZERO_PACKET flag before this */
  1870. td->last_trb = ep_ring->enqueue;
  1871. field |= TRB_IOC;
  1872. }
  1873. remainder = xhci_td_remainder(urb->transfer_buffer_length -
  1874. running_total);
  1875. length_field = TRB_LEN(trb_buff_len) |
  1876. remainder |
  1877. TRB_INTR_TARGET(0);
  1878. queue_trb(xhci, ep_ring, false,
  1879. lower_32_bits(addr),
  1880. upper_32_bits(addr),
  1881. length_field,
  1882. /* We always want to know if the TRB was short,
  1883. * or we won't get an event when it completes.
  1884. * (Unless we use event data TRBs, which are a
  1885. * waste of space and HC resources.)
  1886. */
  1887. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1888. --num_trbs;
  1889. running_total += trb_buff_len;
  1890. /* Calculate length for next transfer */
  1891. addr += trb_buff_len;
  1892. trb_buff_len = urb->transfer_buffer_length - running_total;
  1893. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  1894. trb_buff_len = TRB_MAX_BUFF_SIZE;
  1895. } while (running_total < urb->transfer_buffer_length);
  1896. check_trb_math(urb, num_trbs, running_total);
  1897. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1898. return 0;
  1899. }
  1900. /* Caller must have locked xhci->lock */
  1901. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1902. struct urb *urb, int slot_id, unsigned int ep_index)
  1903. {
  1904. struct xhci_ring *ep_ring;
  1905. int num_trbs;
  1906. int ret;
  1907. struct usb_ctrlrequest *setup;
  1908. struct xhci_generic_trb *start_trb;
  1909. int start_cycle;
  1910. u32 field, length_field;
  1911. struct xhci_td *td;
  1912. ep_ring = xhci->devs[slot_id]->eps[ep_index].ring;
  1913. /*
  1914. * Need to copy setup packet into setup TRB, so we can't use the setup
  1915. * DMA address.
  1916. */
  1917. if (!urb->setup_packet)
  1918. return -EINVAL;
  1919. if (!in_interrupt())
  1920. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  1921. slot_id, ep_index);
  1922. /* 1 TRB for setup, 1 for status */
  1923. num_trbs = 2;
  1924. /*
  1925. * Don't need to check if we need additional event data and normal TRBs,
  1926. * since data in control transfers will never get bigger than 16MB
  1927. * XXX: can we get a buffer that crosses 64KB boundaries?
  1928. */
  1929. if (urb->transfer_buffer_length > 0)
  1930. num_trbs++;
  1931. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
  1932. urb, &td, mem_flags);
  1933. if (ret < 0)
  1934. return ret;
  1935. /*
  1936. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1937. * until we've finished creating all the other TRBs. The ring's cycle
  1938. * state may change as we enqueue the other TRBs, so save it too.
  1939. */
  1940. start_trb = &ep_ring->enqueue->generic;
  1941. start_cycle = ep_ring->cycle_state;
  1942. /* Queue setup TRB - see section 6.4.1.2.1 */
  1943. /* FIXME better way to translate setup_packet into two u32 fields? */
  1944. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  1945. queue_trb(xhci, ep_ring, false,
  1946. /* FIXME endianness is probably going to bite my ass here. */
  1947. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  1948. setup->wIndex | setup->wLength << 16,
  1949. TRB_LEN(8) | TRB_INTR_TARGET(0),
  1950. /* Immediate data in pointer */
  1951. TRB_IDT | TRB_TYPE(TRB_SETUP));
  1952. /* If there's data, queue data TRBs */
  1953. field = 0;
  1954. length_field = TRB_LEN(urb->transfer_buffer_length) |
  1955. xhci_td_remainder(urb->transfer_buffer_length) |
  1956. TRB_INTR_TARGET(0);
  1957. if (urb->transfer_buffer_length > 0) {
  1958. if (setup->bRequestType & USB_DIR_IN)
  1959. field |= TRB_DIR_IN;
  1960. queue_trb(xhci, ep_ring, false,
  1961. lower_32_bits(urb->transfer_dma),
  1962. upper_32_bits(urb->transfer_dma),
  1963. length_field,
  1964. /* Event on short tx */
  1965. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  1966. }
  1967. /* Save the DMA address of the last TRB in the TD */
  1968. td->last_trb = ep_ring->enqueue;
  1969. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  1970. /* If the device sent data, the status stage is an OUT transfer */
  1971. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  1972. field = 0;
  1973. else
  1974. field = TRB_DIR_IN;
  1975. queue_trb(xhci, ep_ring, false,
  1976. 0,
  1977. 0,
  1978. TRB_INTR_TARGET(0),
  1979. /* Event on completion */
  1980. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  1981. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1982. return 0;
  1983. }
  1984. /**** Command Ring Operations ****/
  1985. /* Generic function for queueing a command TRB on the command ring.
  1986. * Check to make sure there's room on the command ring for one command TRB.
  1987. * Also check that there's room reserved for commands that must not fail.
  1988. * If this is a command that must not fail, meaning command_must_succeed = TRUE,
  1989. * then only check for the number of reserved spots.
  1990. * Don't decrement xhci->cmd_ring_reserved_trbs after we've queued the TRB
  1991. * because the command event handler may want to resubmit a failed command.
  1992. */
  1993. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2,
  1994. u32 field3, u32 field4, bool command_must_succeed)
  1995. {
  1996. int reserved_trbs = xhci->cmd_ring_reserved_trbs;
  1997. if (!command_must_succeed)
  1998. reserved_trbs++;
  1999. if (!room_on_ring(xhci, xhci->cmd_ring, reserved_trbs)) {
  2000. if (!in_interrupt())
  2001. xhci_err(xhci, "ERR: No room for command on command ring\n");
  2002. if (command_must_succeed)
  2003. xhci_err(xhci, "ERR: Reserved TRB counting for "
  2004. "unfailable commands failed.\n");
  2005. return -ENOMEM;
  2006. }
  2007. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  2008. field4 | xhci->cmd_ring->cycle_state);
  2009. return 0;
  2010. }
  2011. /* Queue a no-op command on the command ring */
  2012. static int queue_cmd_noop(struct xhci_hcd *xhci)
  2013. {
  2014. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP), false);
  2015. }
  2016. /*
  2017. * Place a no-op command on the command ring to test the command and
  2018. * event ring.
  2019. */
  2020. void *xhci_setup_one_noop(struct xhci_hcd *xhci)
  2021. {
  2022. if (queue_cmd_noop(xhci) < 0)
  2023. return NULL;
  2024. xhci->noops_submitted++;
  2025. return xhci_ring_cmd_db;
  2026. }
  2027. /* Queue a slot enable or disable request on the command ring */
  2028. int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  2029. {
  2030. return queue_command(xhci, 0, 0, 0,
  2031. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id), false);
  2032. }
  2033. /* Queue an address device command TRB */
  2034. int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  2035. u32 slot_id)
  2036. {
  2037. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  2038. upper_32_bits(in_ctx_ptr), 0,
  2039. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id),
  2040. false);
  2041. }
  2042. /* Queue a reset device command TRB */
  2043. int xhci_queue_reset_device(struct xhci_hcd *xhci, u32 slot_id)
  2044. {
  2045. return queue_command(xhci, 0, 0, 0,
  2046. TRB_TYPE(TRB_RESET_DEV) | SLOT_ID_FOR_TRB(slot_id),
  2047. false);
  2048. }
  2049. /* Queue a configure endpoint command TRB */
  2050. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  2051. u32 slot_id, bool command_must_succeed)
  2052. {
  2053. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  2054. upper_32_bits(in_ctx_ptr), 0,
  2055. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id),
  2056. command_must_succeed);
  2057. }
  2058. /* Queue an evaluate context command TRB */
  2059. int xhci_queue_evaluate_context(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  2060. u32 slot_id)
  2061. {
  2062. return queue_command(xhci, lower_32_bits(in_ctx_ptr),
  2063. upper_32_bits(in_ctx_ptr), 0,
  2064. TRB_TYPE(TRB_EVAL_CONTEXT) | SLOT_ID_FOR_TRB(slot_id),
  2065. false);
  2066. }
  2067. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
  2068. unsigned int ep_index)
  2069. {
  2070. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  2071. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  2072. u32 type = TRB_TYPE(TRB_STOP_RING);
  2073. return queue_command(xhci, 0, 0, 0,
  2074. trb_slot_id | trb_ep_index | type, false);
  2075. }
  2076. /* Set Transfer Ring Dequeue Pointer command.
  2077. * This should not be used for endpoints that have streams enabled.
  2078. */
  2079. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  2080. unsigned int ep_index, struct xhci_segment *deq_seg,
  2081. union xhci_trb *deq_ptr, u32 cycle_state)
  2082. {
  2083. dma_addr_t addr;
  2084. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  2085. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  2086. u32 type = TRB_TYPE(TRB_SET_DEQ);
  2087. addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
  2088. if (addr == 0) {
  2089. xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
  2090. xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
  2091. deq_seg, deq_ptr);
  2092. return 0;
  2093. }
  2094. return queue_command(xhci, lower_32_bits(addr) | cycle_state,
  2095. upper_32_bits(addr), 0,
  2096. trb_slot_id | trb_ep_index | type, false);
  2097. }
  2098. int xhci_queue_reset_ep(struct xhci_hcd *xhci, int slot_id,
  2099. unsigned int ep_index)
  2100. {
  2101. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  2102. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  2103. u32 type = TRB_TYPE(TRB_RESET_EP);
  2104. return queue_command(xhci, 0, 0, 0, trb_slot_id | trb_ep_index | type,
  2105. false);
  2106. }