xhci-ring.c 65 KB

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