xhci-ring.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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. dma_addr_t offset;
  75. if (!seg || !trb || (void *) trb < (void *) seg->trbs)
  76. return 0;
  77. /* offset in bytes, since these are byte-addressable */
  78. offset = trb - seg->trbs;
  79. /* SEGMENT_SIZE in bytes, trbs are 16-byte aligned */
  80. if (offset > SEGMENT_SIZE || (offset % sizeof(*trb)) != 0)
  81. return 0;
  82. return seg->dma + offset;
  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. 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. }
  148. /*
  149. * See Cycle bit rules. SW is the consumer for the event ring only.
  150. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  151. *
  152. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  153. * chain bit is set), then set the chain bit in all the following link TRBs.
  154. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  155. * have their chain bit cleared (so that each Link TRB is a separate TD).
  156. *
  157. * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
  158. * set, but other sections talk about dealing with the chain bit set.
  159. * Assume section 6.4.4.1 is wrong, and the chain bit can be set in a Link TRB.
  160. */
  161. static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  162. {
  163. u32 chain;
  164. union xhci_trb *next;
  165. chain = ring->enqueue->generic.field[3] & TRB_CHAIN;
  166. next = ++(ring->enqueue);
  167. ring->enq_updates++;
  168. /* Update the dequeue pointer further if that was a link TRB or we're at
  169. * the end of an event ring segment (which doesn't have link TRBS)
  170. */
  171. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  172. if (!consumer) {
  173. if (ring != xhci->event_ring) {
  174. next->link.control &= ~TRB_CHAIN;
  175. next->link.control |= chain;
  176. /* Give this link TRB to the hardware */
  177. wmb();
  178. if (next->link.control & TRB_CYCLE)
  179. next->link.control &= (u32) ~TRB_CYCLE;
  180. else
  181. next->link.control |= (u32) TRB_CYCLE;
  182. }
  183. /* Toggle the cycle bit after the last ring segment. */
  184. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  185. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  186. if (!in_interrupt())
  187. xhci_dbg(xhci, "Toggle cycle state for ring %p = %i\n",
  188. ring,
  189. (unsigned int) ring->cycle_state);
  190. }
  191. }
  192. ring->enq_seg = ring->enq_seg->next;
  193. ring->enqueue = ring->enq_seg->trbs;
  194. next = ring->enqueue;
  195. }
  196. }
  197. /*
  198. * Check to see if there's room to enqueue num_trbs on the ring. See rules
  199. * above.
  200. * FIXME: this would be simpler and faster if we just kept track of the number
  201. * of free TRBs in a ring.
  202. */
  203. static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
  204. unsigned int num_trbs)
  205. {
  206. int i;
  207. union xhci_trb *enq = ring->enqueue;
  208. struct xhci_segment *enq_seg = ring->enq_seg;
  209. /* Check if ring is empty */
  210. if (enq == ring->dequeue)
  211. return 1;
  212. /* Make sure there's an extra empty TRB available */
  213. for (i = 0; i <= num_trbs; ++i) {
  214. if (enq == ring->dequeue)
  215. return 0;
  216. enq++;
  217. while (last_trb(xhci, ring, enq_seg, enq)) {
  218. enq_seg = enq_seg->next;
  219. enq = enq_seg->trbs;
  220. }
  221. }
  222. return 1;
  223. }
  224. void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
  225. {
  226. u32 temp;
  227. dma_addr_t deq;
  228. deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
  229. xhci->event_ring->dequeue);
  230. if (deq == 0 && !in_interrupt())
  231. xhci_warn(xhci, "WARN something wrong with SW event ring "
  232. "dequeue ptr.\n");
  233. /* Update HC event ring dequeue pointer */
  234. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
  235. temp &= ERST_PTR_MASK;
  236. if (!in_interrupt())
  237. xhci_dbg(xhci, "// Write event ring dequeue pointer\n");
  238. xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[1]);
  239. xhci_writel(xhci, (deq & ~ERST_PTR_MASK) | temp,
  240. &xhci->ir_set->erst_dequeue[0]);
  241. }
  242. /* Ring the host controller doorbell after placing a command on the ring */
  243. void xhci_ring_cmd_db(struct xhci_hcd *xhci)
  244. {
  245. u32 temp;
  246. xhci_dbg(xhci, "// Ding dong!\n");
  247. temp = xhci_readl(xhci, &xhci->dba->doorbell[0]) & DB_MASK;
  248. xhci_writel(xhci, temp | DB_TARGET_HOST, &xhci->dba->doorbell[0]);
  249. /* Flush PCI posted writes */
  250. xhci_readl(xhci, &xhci->dba->doorbell[0]);
  251. }
  252. static void ring_ep_doorbell(struct xhci_hcd *xhci,
  253. unsigned int slot_id,
  254. unsigned int ep_index)
  255. {
  256. struct xhci_ring *ep_ring;
  257. u32 field;
  258. __u32 __iomem *db_addr = &xhci->dba->doorbell[slot_id];
  259. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  260. /* Don't ring the doorbell for this endpoint if there are pending
  261. * cancellations because the we don't want to interrupt processing.
  262. */
  263. if (!ep_ring->cancels_pending && !(ep_ring->state & SET_DEQ_PENDING)) {
  264. field = xhci_readl(xhci, db_addr) & DB_MASK;
  265. xhci_writel(xhci, field | EPI_TO_DB(ep_index), db_addr);
  266. /* Flush PCI posted writes - FIXME Matthew Wilcox says this
  267. * isn't time-critical and we shouldn't make the CPU wait for
  268. * the flush.
  269. */
  270. xhci_readl(xhci, db_addr);
  271. }
  272. }
  273. /*
  274. * Find the segment that trb is in. Start searching in start_seg.
  275. * If we must move past a segment that has a link TRB with a toggle cycle state
  276. * bit set, then we will toggle the value pointed at by cycle_state.
  277. */
  278. static struct xhci_segment *find_trb_seg(
  279. struct xhci_segment *start_seg,
  280. union xhci_trb *trb, int *cycle_state)
  281. {
  282. struct xhci_segment *cur_seg = start_seg;
  283. struct xhci_generic_trb *generic_trb;
  284. while (cur_seg->trbs > trb ||
  285. &cur_seg->trbs[TRBS_PER_SEGMENT - 1] < trb) {
  286. generic_trb = &cur_seg->trbs[TRBS_PER_SEGMENT - 1].generic;
  287. if (TRB_TYPE(generic_trb->field[3]) == TRB_LINK &&
  288. (generic_trb->field[3] & LINK_TOGGLE))
  289. *cycle_state = ~(*cycle_state) & 0x1;
  290. cur_seg = cur_seg->next;
  291. if (cur_seg == start_seg)
  292. /* Looped over the entire list. Oops! */
  293. return 0;
  294. }
  295. return cur_seg;
  296. }
  297. struct dequeue_state {
  298. struct xhci_segment *new_deq_seg;
  299. union xhci_trb *new_deq_ptr;
  300. int new_cycle_state;
  301. };
  302. /*
  303. * Move the xHC's endpoint ring dequeue pointer past cur_td.
  304. * Record the new state of the xHC's endpoint ring dequeue segment,
  305. * dequeue pointer, and new consumer cycle state in state.
  306. * Update our internal representation of the ring's dequeue pointer.
  307. *
  308. * We do this in three jumps:
  309. * - First we update our new ring state to be the same as when the xHC stopped.
  310. * - Then we traverse the ring to find the segment that contains
  311. * the last TRB in the TD. We toggle the xHC's new cycle state when we pass
  312. * any link TRBs with the toggle cycle bit set.
  313. * - Finally we move the dequeue state one TRB further, toggling the cycle bit
  314. * if we've moved it past a link TRB with the toggle cycle bit set.
  315. */
  316. static void find_new_dequeue_state(struct xhci_hcd *xhci,
  317. unsigned int slot_id, unsigned int ep_index,
  318. struct xhci_td *cur_td, struct dequeue_state *state)
  319. {
  320. struct xhci_virt_device *dev = xhci->devs[slot_id];
  321. struct xhci_ring *ep_ring = dev->ep_rings[ep_index];
  322. struct xhci_generic_trb *trb;
  323. state->new_cycle_state = 0;
  324. state->new_deq_seg = find_trb_seg(cur_td->start_seg,
  325. ep_ring->stopped_trb,
  326. &state->new_cycle_state);
  327. if (!state->new_deq_seg)
  328. BUG();
  329. /* Dig out the cycle state saved by the xHC during the stop ep cmd */
  330. state->new_cycle_state = 0x1 & dev->out_ctx->ep[ep_index].deq[0];
  331. state->new_deq_ptr = cur_td->last_trb;
  332. state->new_deq_seg = find_trb_seg(state->new_deq_seg,
  333. state->new_deq_ptr,
  334. &state->new_cycle_state);
  335. if (!state->new_deq_seg)
  336. BUG();
  337. trb = &state->new_deq_ptr->generic;
  338. if (TRB_TYPE(trb->field[3]) == TRB_LINK &&
  339. (trb->field[3] & LINK_TOGGLE))
  340. state->new_cycle_state = ~(state->new_cycle_state) & 0x1;
  341. next_trb(xhci, ep_ring, &state->new_deq_seg, &state->new_deq_ptr);
  342. /* Don't update the ring cycle state for the producer (us). */
  343. ep_ring->dequeue = state->new_deq_ptr;
  344. ep_ring->deq_seg = state->new_deq_seg;
  345. }
  346. static void td_to_noop(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  347. struct xhci_td *cur_td)
  348. {
  349. struct xhci_segment *cur_seg;
  350. union xhci_trb *cur_trb;
  351. for (cur_seg = cur_td->start_seg, cur_trb = cur_td->first_trb;
  352. true;
  353. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  354. if ((cur_trb->generic.field[3] & TRB_TYPE_BITMASK) ==
  355. TRB_TYPE(TRB_LINK)) {
  356. /* Unchain any chained Link TRBs, but
  357. * leave the pointers intact.
  358. */
  359. cur_trb->generic.field[3] &= ~TRB_CHAIN;
  360. xhci_dbg(xhci, "Cancel (unchain) link TRB\n");
  361. xhci_dbg(xhci, "Address = %p (0x%llx dma); "
  362. "in seg %p (0x%llx dma)\n",
  363. cur_trb,
  364. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  365. cur_seg,
  366. (unsigned long long)cur_seg->dma);
  367. } else {
  368. cur_trb->generic.field[0] = 0;
  369. cur_trb->generic.field[1] = 0;
  370. cur_trb->generic.field[2] = 0;
  371. /* Preserve only the cycle bit of this TRB */
  372. cur_trb->generic.field[3] &= TRB_CYCLE;
  373. cur_trb->generic.field[3] |= TRB_TYPE(TRB_TR_NOOP);
  374. xhci_dbg(xhci, "Cancel TRB %p (0x%llx dma) "
  375. "in seg %p (0x%llx dma)\n",
  376. cur_trb,
  377. (unsigned long long)xhci_trb_virt_to_dma(cur_seg, cur_trb),
  378. cur_seg,
  379. (unsigned long long)cur_seg->dma);
  380. }
  381. if (cur_trb == cur_td->last_trb)
  382. break;
  383. }
  384. }
  385. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  386. unsigned int ep_index, struct xhci_segment *deq_seg,
  387. union xhci_trb *deq_ptr, u32 cycle_state);
  388. /*
  389. * When we get a command completion for a Stop Endpoint Command, we need to
  390. * unlink any cancelled TDs from the ring. There are two ways to do that:
  391. *
  392. * 1. If the HW was in the middle of processing the TD that needs to be
  393. * cancelled, then we must move the ring's dequeue pointer past the last TRB
  394. * in the TD with a Set Dequeue Pointer Command.
  395. * 2. Otherwise, we turn all the TRBs in the TD into No-op TRBs (with the chain
  396. * bit cleared) so that the HW will skip over them.
  397. */
  398. static void handle_stopped_endpoint(struct xhci_hcd *xhci,
  399. union xhci_trb *trb)
  400. {
  401. unsigned int slot_id;
  402. unsigned int ep_index;
  403. struct xhci_ring *ep_ring;
  404. struct list_head *entry;
  405. struct xhci_td *cur_td = 0;
  406. struct xhci_td *last_unlinked_td;
  407. struct dequeue_state deq_state;
  408. #ifdef CONFIG_USB_HCD_STAT
  409. ktime_t stop_time = ktime_get();
  410. #endif
  411. memset(&deq_state, 0, sizeof(deq_state));
  412. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  413. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  414. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  415. if (list_empty(&ep_ring->cancelled_td_list))
  416. return;
  417. /* Fix up the ep ring first, so HW stops executing cancelled TDs.
  418. * We have the xHCI lock, so nothing can modify this list until we drop
  419. * it. We're also in the event handler, so we can't get re-interrupted
  420. * if another Stop Endpoint command completes
  421. */
  422. list_for_each(entry, &ep_ring->cancelled_td_list) {
  423. cur_td = list_entry(entry, struct xhci_td, cancelled_td_list);
  424. xhci_dbg(xhci, "Cancelling TD starting at %p, 0x%llx (dma).\n",
  425. cur_td->first_trb,
  426. (unsigned long long)xhci_trb_virt_to_dma(cur_td->start_seg, cur_td->first_trb));
  427. /*
  428. * If we stopped on the TD we need to cancel, then we have to
  429. * move the xHC endpoint ring dequeue pointer past this TD.
  430. */
  431. if (cur_td == ep_ring->stopped_td)
  432. find_new_dequeue_state(xhci, slot_id, ep_index, cur_td,
  433. &deq_state);
  434. else
  435. td_to_noop(xhci, ep_ring, cur_td);
  436. /*
  437. * The event handler won't see a completion for this TD anymore,
  438. * so remove it from the endpoint ring's TD list. Keep it in
  439. * the cancelled TD list for URB completion later.
  440. */
  441. list_del(&cur_td->td_list);
  442. ep_ring->cancels_pending--;
  443. }
  444. last_unlinked_td = cur_td;
  445. /* If necessary, queue a Set Transfer Ring Dequeue Pointer command */
  446. if (deq_state.new_deq_ptr && deq_state.new_deq_seg) {
  447. xhci_dbg(xhci, "Set TR Deq Ptr cmd, new deq seg = %p (0x%llx dma), "
  448. "new deq ptr = %p (0x%llx dma), new cycle = %u\n",
  449. deq_state.new_deq_seg,
  450. (unsigned long long)deq_state.new_deq_seg->dma,
  451. deq_state.new_deq_ptr,
  452. (unsigned long long)xhci_trb_virt_to_dma(deq_state.new_deq_seg, deq_state.new_deq_ptr),
  453. deq_state.new_cycle_state);
  454. queue_set_tr_deq(xhci, slot_id, ep_index,
  455. deq_state.new_deq_seg,
  456. deq_state.new_deq_ptr,
  457. (u32) deq_state.new_cycle_state);
  458. /* Stop the TD queueing code from ringing the doorbell until
  459. * this command completes. The HC won't set the dequeue pointer
  460. * if the ring is running, and ringing the doorbell starts the
  461. * ring running.
  462. */
  463. ep_ring->state |= SET_DEQ_PENDING;
  464. xhci_ring_cmd_db(xhci);
  465. } else {
  466. /* Otherwise just ring the doorbell to restart the ring */
  467. ring_ep_doorbell(xhci, slot_id, ep_index);
  468. }
  469. /*
  470. * Drop the lock and complete the URBs in the cancelled TD list.
  471. * New TDs to be cancelled might be added to the end of the list before
  472. * we can complete all the URBs for the TDs we already unlinked.
  473. * So stop when we've completed the URB for the last TD we unlinked.
  474. */
  475. do {
  476. cur_td = list_entry(ep_ring->cancelled_td_list.next,
  477. struct xhci_td, cancelled_td_list);
  478. list_del(&cur_td->cancelled_td_list);
  479. /* Clean up the cancelled URB */
  480. #ifdef CONFIG_USB_HCD_STAT
  481. hcd_stat_update(xhci->tp_stat, cur_td->urb->actual_length,
  482. ktime_sub(stop_time, cur_td->start_time));
  483. #endif
  484. cur_td->urb->hcpriv = NULL;
  485. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), cur_td->urb);
  486. xhci_dbg(xhci, "Giveback cancelled URB %p\n", cur_td->urb);
  487. spin_unlock(&xhci->lock);
  488. /* Doesn't matter what we pass for status, since the core will
  489. * just overwrite it (because the URB has been unlinked).
  490. */
  491. usb_hcd_giveback_urb(xhci_to_hcd(xhci), cur_td->urb, 0);
  492. kfree(cur_td);
  493. spin_lock(&xhci->lock);
  494. } while (cur_td != last_unlinked_td);
  495. /* Return to the event handler with xhci->lock re-acquired */
  496. }
  497. /*
  498. * When we get a completion for a Set Transfer Ring Dequeue Pointer command,
  499. * we need to clear the set deq pending flag in the endpoint ring state, so that
  500. * the TD queueing code can ring the doorbell again. We also need to ring the
  501. * endpoint doorbell to restart the ring, but only if there aren't more
  502. * cancellations pending.
  503. */
  504. static void handle_set_deq_completion(struct xhci_hcd *xhci,
  505. struct xhci_event_cmd *event,
  506. union xhci_trb *trb)
  507. {
  508. unsigned int slot_id;
  509. unsigned int ep_index;
  510. struct xhci_ring *ep_ring;
  511. struct xhci_virt_device *dev;
  512. slot_id = TRB_TO_SLOT_ID(trb->generic.field[3]);
  513. ep_index = TRB_TO_EP_INDEX(trb->generic.field[3]);
  514. dev = xhci->devs[slot_id];
  515. ep_ring = dev->ep_rings[ep_index];
  516. if (GET_COMP_CODE(event->status) != COMP_SUCCESS) {
  517. unsigned int ep_state;
  518. unsigned int slot_state;
  519. switch (GET_COMP_CODE(event->status)) {
  520. case COMP_TRB_ERR:
  521. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd invalid because "
  522. "of stream ID configuration\n");
  523. break;
  524. case COMP_CTX_STATE:
  525. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed due "
  526. "to incorrect slot or ep state.\n");
  527. ep_state = dev->out_ctx->ep[ep_index].ep_info;
  528. ep_state &= EP_STATE_MASK;
  529. slot_state = dev->out_ctx->slot.dev_state;
  530. slot_state = GET_SLOT_STATE(slot_state);
  531. xhci_dbg(xhci, "Slot state = %u, EP state = %u\n",
  532. slot_state, ep_state);
  533. break;
  534. case COMP_EBADSLT:
  535. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd failed because "
  536. "slot %u was not enabled.\n", slot_id);
  537. break;
  538. default:
  539. xhci_warn(xhci, "WARN Set TR Deq Ptr cmd with unknown "
  540. "completion code of %u.\n",
  541. GET_COMP_CODE(event->status));
  542. break;
  543. }
  544. /* OK what do we do now? The endpoint state is hosed, and we
  545. * should never get to this point if the synchronization between
  546. * queueing, and endpoint state are correct. This might happen
  547. * if the device gets disconnected after we've finished
  548. * cancelling URBs, which might not be an error...
  549. */
  550. } else {
  551. xhci_dbg(xhci, "Successful Set TR Deq Ptr cmd, deq[0] = 0x%x, "
  552. "deq[1] = 0x%x.\n",
  553. dev->out_ctx->ep[ep_index].deq[0],
  554. dev->out_ctx->ep[ep_index].deq[1]);
  555. }
  556. ep_ring->state &= ~SET_DEQ_PENDING;
  557. ring_ep_doorbell(xhci, slot_id, ep_index);
  558. }
  559. static void handle_cmd_completion(struct xhci_hcd *xhci,
  560. struct xhci_event_cmd *event)
  561. {
  562. int slot_id = TRB_TO_SLOT_ID(event->flags);
  563. u64 cmd_dma;
  564. dma_addr_t cmd_dequeue_dma;
  565. cmd_dma = (((u64) event->cmd_trb[1]) << 32) + event->cmd_trb[0];
  566. cmd_dequeue_dma = xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  567. xhci->cmd_ring->dequeue);
  568. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  569. if (cmd_dequeue_dma == 0) {
  570. xhci->error_bitmask |= 1 << 4;
  571. return;
  572. }
  573. /* Does the DMA address match our internal dequeue pointer address? */
  574. if (cmd_dma != (u64) cmd_dequeue_dma) {
  575. xhci->error_bitmask |= 1 << 5;
  576. return;
  577. }
  578. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  579. case TRB_TYPE(TRB_ENABLE_SLOT):
  580. if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
  581. xhci->slot_id = slot_id;
  582. else
  583. xhci->slot_id = 0;
  584. complete(&xhci->addr_dev);
  585. break;
  586. case TRB_TYPE(TRB_DISABLE_SLOT):
  587. if (xhci->devs[slot_id])
  588. xhci_free_virt_device(xhci, slot_id);
  589. break;
  590. case TRB_TYPE(TRB_CONFIG_EP):
  591. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  592. complete(&xhci->devs[slot_id]->cmd_completion);
  593. break;
  594. case TRB_TYPE(TRB_ADDR_DEV):
  595. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  596. complete(&xhci->addr_dev);
  597. break;
  598. case TRB_TYPE(TRB_STOP_RING):
  599. handle_stopped_endpoint(xhci, xhci->cmd_ring->dequeue);
  600. break;
  601. case TRB_TYPE(TRB_SET_DEQ):
  602. handle_set_deq_completion(xhci, event, xhci->cmd_ring->dequeue);
  603. break;
  604. case TRB_TYPE(TRB_CMD_NOOP):
  605. ++xhci->noops_handled;
  606. break;
  607. default:
  608. /* Skip over unknown commands on the event ring */
  609. xhci->error_bitmask |= 1 << 6;
  610. break;
  611. }
  612. inc_deq(xhci, xhci->cmd_ring, false);
  613. }
  614. static void handle_port_status(struct xhci_hcd *xhci,
  615. union xhci_trb *event)
  616. {
  617. u32 port_id;
  618. /* Port status change events always have a successful completion code */
  619. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  620. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  621. xhci->error_bitmask |= 1 << 8;
  622. }
  623. /* FIXME: core doesn't care about all port link state changes yet */
  624. port_id = GET_PORT_ID(event->generic.field[0]);
  625. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  626. /* Update event ring dequeue pointer before dropping the lock */
  627. inc_deq(xhci, xhci->event_ring, true);
  628. xhci_set_hc_event_deq(xhci);
  629. spin_unlock(&xhci->lock);
  630. /* Pass this up to the core */
  631. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  632. spin_lock(&xhci->lock);
  633. }
  634. /*
  635. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  636. * at end_trb, which may be in another segment. If the suspect DMA address is a
  637. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  638. * returns 0.
  639. */
  640. static struct xhci_segment *trb_in_td(
  641. struct xhci_segment *start_seg,
  642. union xhci_trb *start_trb,
  643. union xhci_trb *end_trb,
  644. dma_addr_t suspect_dma)
  645. {
  646. dma_addr_t start_dma;
  647. dma_addr_t end_seg_dma;
  648. dma_addr_t end_trb_dma;
  649. struct xhci_segment *cur_seg;
  650. start_dma = xhci_trb_virt_to_dma(start_seg, start_trb);
  651. cur_seg = start_seg;
  652. do {
  653. /* We may get an event for a Link TRB in the middle of a TD */
  654. end_seg_dma = xhci_trb_virt_to_dma(cur_seg,
  655. &start_seg->trbs[TRBS_PER_SEGMENT - 1]);
  656. /* If the end TRB isn't in this segment, this is set to 0 */
  657. end_trb_dma = xhci_trb_virt_to_dma(cur_seg, end_trb);
  658. if (end_trb_dma > 0) {
  659. /* The end TRB is in this segment, so suspect should be here */
  660. if (start_dma <= end_trb_dma) {
  661. if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
  662. return cur_seg;
  663. } else {
  664. /* Case for one segment with
  665. * a TD wrapped around to the top
  666. */
  667. if ((suspect_dma >= start_dma &&
  668. suspect_dma <= end_seg_dma) ||
  669. (suspect_dma >= cur_seg->dma &&
  670. suspect_dma <= end_trb_dma))
  671. return cur_seg;
  672. }
  673. return 0;
  674. } else {
  675. /* Might still be somewhere in this segment */
  676. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  677. return cur_seg;
  678. }
  679. cur_seg = cur_seg->next;
  680. start_dma = xhci_trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  681. } while (1);
  682. }
  683. /*
  684. * If this function returns an error condition, it means it got a Transfer
  685. * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
  686. * At this point, the host controller is probably hosed and should be reset.
  687. */
  688. static int handle_tx_event(struct xhci_hcd *xhci,
  689. struct xhci_transfer_event *event)
  690. {
  691. struct xhci_virt_device *xdev;
  692. struct xhci_ring *ep_ring;
  693. int ep_index;
  694. struct xhci_td *td = 0;
  695. dma_addr_t event_dma;
  696. struct xhci_segment *event_seg;
  697. union xhci_trb *event_trb;
  698. struct urb *urb = 0;
  699. int status = -EINPROGRESS;
  700. xdev = xhci->devs[TRB_TO_SLOT_ID(event->flags)];
  701. if (!xdev) {
  702. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  703. return -ENODEV;
  704. }
  705. /* Endpoint ID is 1 based, our index is zero based */
  706. ep_index = TRB_TO_EP_ID(event->flags) - 1;
  707. ep_ring = xdev->ep_rings[ep_index];
  708. if (!ep_ring || (xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
  709. xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n");
  710. return -ENODEV;
  711. }
  712. event_dma = event->buffer[0];
  713. if (event->buffer[1] != 0)
  714. xhci_warn(xhci, "WARN ignoring upper 32-bits of 64-bit TRB dma address\n");
  715. /* This TRB should be in the TD at the head of this ring's TD list */
  716. if (list_empty(&ep_ring->td_list)) {
  717. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  718. TRB_TO_SLOT_ID(event->flags), ep_index);
  719. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  720. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  721. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  722. urb = NULL;
  723. goto cleanup;
  724. }
  725. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  726. /* Is this a TRB in the currently executing TD? */
  727. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  728. td->last_trb, event_dma);
  729. if (!event_seg) {
  730. /* HC is busted, give up! */
  731. xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
  732. return -ESHUTDOWN;
  733. }
  734. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
  735. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  736. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  737. xhci_dbg(xhci, "Offset 0x00 (buffer[0]) = 0x%x\n",
  738. (unsigned int) event->buffer[0]);
  739. xhci_dbg(xhci, "Offset 0x04 (buffer[0]) = 0x%x\n",
  740. (unsigned int) event->buffer[1]);
  741. xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
  742. (unsigned int) event->transfer_len);
  743. xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
  744. (unsigned int) event->flags);
  745. /* Look for common error cases */
  746. switch (GET_COMP_CODE(event->transfer_len)) {
  747. /* Skip codes that require special handling depending on
  748. * transfer type
  749. */
  750. case COMP_SUCCESS:
  751. case COMP_SHORT_TX:
  752. break;
  753. case COMP_STOP:
  754. xhci_dbg(xhci, "Stopped on Transfer TRB\n");
  755. break;
  756. case COMP_STOP_INVAL:
  757. xhci_dbg(xhci, "Stopped on No-op or Link TRB\n");
  758. break;
  759. case COMP_STALL:
  760. xhci_warn(xhci, "WARN: Stalled endpoint\n");
  761. status = -EPIPE;
  762. break;
  763. case COMP_TRB_ERR:
  764. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  765. status = -EILSEQ;
  766. break;
  767. case COMP_TX_ERR:
  768. xhci_warn(xhci, "WARN: transfer error on endpoint\n");
  769. status = -EPROTO;
  770. break;
  771. case COMP_DB_ERR:
  772. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  773. status = -ENOSR;
  774. break;
  775. default:
  776. xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
  777. urb = NULL;
  778. goto cleanup;
  779. }
  780. /* Now update the urb's actual_length and give back to the core */
  781. /* Was this a control transfer? */
  782. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  783. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  784. switch (GET_COMP_CODE(event->transfer_len)) {
  785. case COMP_SUCCESS:
  786. if (event_trb == ep_ring->dequeue) {
  787. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  788. status = -ESHUTDOWN;
  789. } else if (event_trb != td->last_trb) {
  790. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  791. status = -ESHUTDOWN;
  792. } else {
  793. xhci_dbg(xhci, "Successful control transfer!\n");
  794. status = 0;
  795. }
  796. break;
  797. case COMP_SHORT_TX:
  798. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  799. status = -EREMOTEIO;
  800. break;
  801. default:
  802. /* Others already handled above */
  803. break;
  804. }
  805. /*
  806. * Did we transfer any data, despite the errors that might have
  807. * happened? I.e. did we get past the setup stage?
  808. */
  809. if (event_trb != ep_ring->dequeue) {
  810. /* The event was for the status stage */
  811. if (event_trb == td->last_trb) {
  812. td->urb->actual_length =
  813. td->urb->transfer_buffer_length;
  814. } else {
  815. /* Maybe the event was for the data stage? */
  816. if (GET_COMP_CODE(event->transfer_len) != COMP_STOP_INVAL)
  817. /* We didn't stop on a link TRB in the middle */
  818. td->urb->actual_length =
  819. td->urb->transfer_buffer_length -
  820. TRB_LEN(event->transfer_len);
  821. }
  822. }
  823. } else {
  824. switch (GET_COMP_CODE(event->transfer_len)) {
  825. case COMP_SUCCESS:
  826. /* Double check that the HW transferred everything. */
  827. if (event_trb != td->last_trb) {
  828. xhci_warn(xhci, "WARN Successful completion "
  829. "on short TX\n");
  830. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  831. status = -EREMOTEIO;
  832. else
  833. status = 0;
  834. } else {
  835. xhci_dbg(xhci, "Successful bulk transfer!\n");
  836. status = 0;
  837. }
  838. break;
  839. case COMP_SHORT_TX:
  840. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  841. status = -EREMOTEIO;
  842. else
  843. status = 0;
  844. break;
  845. default:
  846. /* Others already handled above */
  847. break;
  848. }
  849. dev_dbg(&td->urb->dev->dev,
  850. "ep %#x - asked for %d bytes, "
  851. "%d bytes untransferred\n",
  852. td->urb->ep->desc.bEndpointAddress,
  853. td->urb->transfer_buffer_length,
  854. TRB_LEN(event->transfer_len));
  855. /* Fast path - was this the last TRB in the TD for this URB? */
  856. if (event_trb == td->last_trb) {
  857. if (TRB_LEN(event->transfer_len) != 0) {
  858. td->urb->actual_length =
  859. td->urb->transfer_buffer_length -
  860. TRB_LEN(event->transfer_len);
  861. if (td->urb->actual_length < 0) {
  862. xhci_warn(xhci, "HC gave bad length "
  863. "of %d bytes left\n",
  864. TRB_LEN(event->transfer_len));
  865. td->urb->actual_length = 0;
  866. }
  867. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  868. status = -EREMOTEIO;
  869. else
  870. status = 0;
  871. } else {
  872. td->urb->actual_length = td->urb->transfer_buffer_length;
  873. /* Ignore a short packet completion if the
  874. * untransferred length was zero.
  875. */
  876. status = 0;
  877. }
  878. } else {
  879. /* Slow path - walk the list, starting from the dequeue
  880. * pointer, to get the actual length transferred.
  881. */
  882. union xhci_trb *cur_trb;
  883. struct xhci_segment *cur_seg;
  884. td->urb->actual_length = 0;
  885. for (cur_trb = ep_ring->dequeue, cur_seg = ep_ring->deq_seg;
  886. cur_trb != event_trb;
  887. next_trb(xhci, ep_ring, &cur_seg, &cur_trb)) {
  888. if (TRB_TYPE(cur_trb->generic.field[3]) != TRB_TR_NOOP &&
  889. TRB_TYPE(cur_trb->generic.field[3]) != TRB_LINK)
  890. td->urb->actual_length +=
  891. TRB_LEN(cur_trb->generic.field[2]);
  892. }
  893. /* If the ring didn't stop on a Link or No-op TRB, add
  894. * in the actual bytes transferred from the Normal TRB
  895. */
  896. if (GET_COMP_CODE(event->transfer_len) != COMP_STOP_INVAL)
  897. td->urb->actual_length +=
  898. TRB_LEN(cur_trb->generic.field[2]) -
  899. TRB_LEN(event->transfer_len);
  900. }
  901. }
  902. /* The Endpoint Stop Command completion will take care of
  903. * any stopped TDs. A stopped TD may be restarted, so don't update the
  904. * ring dequeue pointer or take this TD off any lists yet.
  905. */
  906. if (GET_COMP_CODE(event->transfer_len) == COMP_STOP_INVAL ||
  907. GET_COMP_CODE(event->transfer_len) == COMP_STOP) {
  908. ep_ring->stopped_td = td;
  909. ep_ring->stopped_trb = event_trb;
  910. } else {
  911. /* Update ring dequeue pointer */
  912. while (ep_ring->dequeue != td->last_trb)
  913. inc_deq(xhci, ep_ring, false);
  914. inc_deq(xhci, ep_ring, false);
  915. /* Clean up the endpoint's TD list */
  916. urb = td->urb;
  917. list_del(&td->td_list);
  918. /* Was this TD slated to be cancelled but completed anyway? */
  919. if (!list_empty(&td->cancelled_td_list)) {
  920. list_del(&td->cancelled_td_list);
  921. ep_ring->cancels_pending--;
  922. }
  923. kfree(td);
  924. urb->hcpriv = NULL;
  925. }
  926. cleanup:
  927. inc_deq(xhci, xhci->event_ring, true);
  928. xhci_set_hc_event_deq(xhci);
  929. /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
  930. if (urb) {
  931. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  932. spin_unlock(&xhci->lock);
  933. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  934. spin_lock(&xhci->lock);
  935. }
  936. return 0;
  937. }
  938. /*
  939. * This function handles all OS-owned events on the event ring. It may drop
  940. * xhci->lock between event processing (e.g. to pass up port status changes).
  941. */
  942. void xhci_handle_event(struct xhci_hcd *xhci)
  943. {
  944. union xhci_trb *event;
  945. int update_ptrs = 1;
  946. int ret;
  947. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  948. xhci->error_bitmask |= 1 << 1;
  949. return;
  950. }
  951. event = xhci->event_ring->dequeue;
  952. /* Does the HC or OS own the TRB? */
  953. if ((event->event_cmd.flags & TRB_CYCLE) !=
  954. xhci->event_ring->cycle_state) {
  955. xhci->error_bitmask |= 1 << 2;
  956. return;
  957. }
  958. /* FIXME: Handle more event types. */
  959. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  960. case TRB_TYPE(TRB_COMPLETION):
  961. handle_cmd_completion(xhci, &event->event_cmd);
  962. break;
  963. case TRB_TYPE(TRB_PORT_STATUS):
  964. handle_port_status(xhci, event);
  965. update_ptrs = 0;
  966. break;
  967. case TRB_TYPE(TRB_TRANSFER):
  968. ret = handle_tx_event(xhci, &event->trans_event);
  969. if (ret < 0)
  970. xhci->error_bitmask |= 1 << 9;
  971. else
  972. update_ptrs = 0;
  973. break;
  974. default:
  975. xhci->error_bitmask |= 1 << 3;
  976. }
  977. if (update_ptrs) {
  978. /* Update SW and HC event ring dequeue pointer */
  979. inc_deq(xhci, xhci->event_ring, true);
  980. xhci_set_hc_event_deq(xhci);
  981. }
  982. /* Are there more items on the event ring? */
  983. xhci_handle_event(xhci);
  984. }
  985. /**** Endpoint Ring Operations ****/
  986. /*
  987. * Generic function for queueing a TRB on a ring.
  988. * The caller must have checked to make sure there's room on the ring.
  989. */
  990. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  991. bool consumer,
  992. u32 field1, u32 field2, u32 field3, u32 field4)
  993. {
  994. struct xhci_generic_trb *trb;
  995. trb = &ring->enqueue->generic;
  996. trb->field[0] = field1;
  997. trb->field[1] = field2;
  998. trb->field[2] = field3;
  999. trb->field[3] = field4;
  1000. inc_enq(xhci, ring, consumer);
  1001. }
  1002. /*
  1003. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  1004. * FIXME allocate segments if the ring is full.
  1005. */
  1006. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  1007. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  1008. {
  1009. /* Make sure the endpoint has been added to xHC schedule */
  1010. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  1011. switch (ep_state) {
  1012. case EP_STATE_DISABLED:
  1013. /*
  1014. * USB core changed config/interfaces without notifying us,
  1015. * or hardware is reporting the wrong state.
  1016. */
  1017. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  1018. return -ENOENT;
  1019. case EP_STATE_HALTED:
  1020. case EP_STATE_ERROR:
  1021. xhci_warn(xhci, "WARN waiting for halt or error on ep "
  1022. "to be cleared\n");
  1023. /* FIXME event handling code for error needs to clear it */
  1024. /* XXX not sure if this should be -ENOENT or not */
  1025. return -EINVAL;
  1026. case EP_STATE_STOPPED:
  1027. case EP_STATE_RUNNING:
  1028. break;
  1029. default:
  1030. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  1031. /*
  1032. * FIXME issue Configure Endpoint command to try to get the HC
  1033. * back into a known state.
  1034. */
  1035. return -EINVAL;
  1036. }
  1037. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  1038. /* FIXME allocate more room */
  1039. xhci_err(xhci, "ERROR no room on ep ring\n");
  1040. return -ENOMEM;
  1041. }
  1042. return 0;
  1043. }
  1044. static int prepare_transfer(struct xhci_hcd *xhci,
  1045. struct xhci_virt_device *xdev,
  1046. unsigned int ep_index,
  1047. unsigned int num_trbs,
  1048. struct urb *urb,
  1049. struct xhci_td **td,
  1050. gfp_t mem_flags)
  1051. {
  1052. int ret;
  1053. ret = prepare_ring(xhci, xdev->ep_rings[ep_index],
  1054. xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK,
  1055. num_trbs, mem_flags);
  1056. if (ret)
  1057. return ret;
  1058. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  1059. if (!*td)
  1060. return -ENOMEM;
  1061. INIT_LIST_HEAD(&(*td)->td_list);
  1062. INIT_LIST_HEAD(&(*td)->cancelled_td_list);
  1063. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  1064. if (unlikely(ret)) {
  1065. kfree(*td);
  1066. return ret;
  1067. }
  1068. (*td)->urb = urb;
  1069. urb->hcpriv = (void *) (*td);
  1070. /* Add this TD to the tail of the endpoint ring's TD list */
  1071. list_add_tail(&(*td)->td_list, &xdev->ep_rings[ep_index]->td_list);
  1072. (*td)->start_seg = xdev->ep_rings[ep_index]->enq_seg;
  1073. (*td)->first_trb = xdev->ep_rings[ep_index]->enqueue;
  1074. return 0;
  1075. }
  1076. static unsigned int count_sg_trbs_needed(struct xhci_hcd *xhci, struct urb *urb)
  1077. {
  1078. int num_sgs, num_trbs, running_total, temp, i;
  1079. struct scatterlist *sg;
  1080. sg = NULL;
  1081. num_sgs = urb->num_sgs;
  1082. temp = urb->transfer_buffer_length;
  1083. xhci_dbg(xhci, "count sg list trbs: \n");
  1084. num_trbs = 0;
  1085. for_each_sg(urb->sg->sg, sg, num_sgs, i) {
  1086. unsigned int previous_total_trbs = num_trbs;
  1087. unsigned int len = sg_dma_len(sg);
  1088. /* Scatter gather list entries may cross 64KB boundaries */
  1089. running_total = TRB_MAX_BUFF_SIZE -
  1090. (sg_dma_address(sg) & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1091. if (running_total != 0)
  1092. num_trbs++;
  1093. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1094. while (running_total < sg_dma_len(sg)) {
  1095. num_trbs++;
  1096. running_total += TRB_MAX_BUFF_SIZE;
  1097. }
  1098. xhci_dbg(xhci, " sg #%d: dma = %#llx, len = %#x (%d), num_trbs = %d\n",
  1099. i, (unsigned long long)sg_dma_address(sg),
  1100. len, len, num_trbs - previous_total_trbs);
  1101. len = min_t(int, len, temp);
  1102. temp -= len;
  1103. if (temp == 0)
  1104. break;
  1105. }
  1106. xhci_dbg(xhci, "\n");
  1107. if (!in_interrupt())
  1108. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, sglist used, num_trbs = %d\n",
  1109. urb->ep->desc.bEndpointAddress,
  1110. urb->transfer_buffer_length,
  1111. num_trbs);
  1112. return num_trbs;
  1113. }
  1114. static void check_trb_math(struct urb *urb, int num_trbs, int running_total)
  1115. {
  1116. if (num_trbs != 0)
  1117. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  1118. "TRBs, %d left\n", __func__,
  1119. urb->ep->desc.bEndpointAddress, num_trbs);
  1120. if (running_total != urb->transfer_buffer_length)
  1121. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated tx length, "
  1122. "queued %#x (%d), asked for %#x (%d)\n",
  1123. __func__,
  1124. urb->ep->desc.bEndpointAddress,
  1125. running_total, running_total,
  1126. urb->transfer_buffer_length,
  1127. urb->transfer_buffer_length);
  1128. }
  1129. static void giveback_first_trb(struct xhci_hcd *xhci, int slot_id,
  1130. unsigned int ep_index, int start_cycle,
  1131. struct xhci_generic_trb *start_trb, struct xhci_td *td)
  1132. {
  1133. /*
  1134. * Pass all the TRBs to the hardware at once and make sure this write
  1135. * isn't reordered.
  1136. */
  1137. wmb();
  1138. start_trb->field[3] |= start_cycle;
  1139. ring_ep_doorbell(xhci, slot_id, ep_index);
  1140. }
  1141. static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1142. struct urb *urb, int slot_id, unsigned int ep_index)
  1143. {
  1144. struct xhci_ring *ep_ring;
  1145. unsigned int num_trbs;
  1146. struct xhci_td *td;
  1147. struct scatterlist *sg;
  1148. int num_sgs;
  1149. int trb_buff_len, this_sg_len, running_total;
  1150. bool first_trb;
  1151. u64 addr;
  1152. struct xhci_generic_trb *start_trb;
  1153. int start_cycle;
  1154. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1155. num_trbs = count_sg_trbs_needed(xhci, urb);
  1156. num_sgs = urb->num_sgs;
  1157. trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
  1158. ep_index, num_trbs, urb, &td, mem_flags);
  1159. if (trb_buff_len < 0)
  1160. return trb_buff_len;
  1161. /*
  1162. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1163. * until we've finished creating all the other TRBs. The ring's cycle
  1164. * state may change as we enqueue the other TRBs, so save it too.
  1165. */
  1166. start_trb = &ep_ring->enqueue->generic;
  1167. start_cycle = ep_ring->cycle_state;
  1168. running_total = 0;
  1169. /*
  1170. * How much data is in the first TRB?
  1171. *
  1172. * There are three forces at work for TRB buffer pointers and lengths:
  1173. * 1. We don't want to walk off the end of this sg-list entry buffer.
  1174. * 2. The transfer length that the driver requested may be smaller than
  1175. * the amount of memory allocated for this scatter-gather list.
  1176. * 3. TRBs buffers can't cross 64KB boundaries.
  1177. */
  1178. sg = urb->sg->sg;
  1179. addr = (u64) sg_dma_address(sg);
  1180. this_sg_len = sg_dma_len(sg);
  1181. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1182. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1183. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1184. if (trb_buff_len > urb->transfer_buffer_length)
  1185. trb_buff_len = urb->transfer_buffer_length;
  1186. xhci_dbg(xhci, "First length to xfer from 1st sglist entry = %u\n",
  1187. trb_buff_len);
  1188. first_trb = true;
  1189. /* Queue the first TRB, even if it's zero-length */
  1190. do {
  1191. u32 field = 0;
  1192. /* Don't change the cycle bit of the first TRB until later */
  1193. if (first_trb)
  1194. first_trb = false;
  1195. else
  1196. field |= ep_ring->cycle_state;
  1197. /* Chain all the TRBs together; clear the chain bit in the last
  1198. * TRB to indicate it's the last TRB in the chain.
  1199. */
  1200. if (num_trbs > 1) {
  1201. field |= TRB_CHAIN;
  1202. } else {
  1203. /* FIXME - add check for ZERO_PACKET flag before this */
  1204. td->last_trb = ep_ring->enqueue;
  1205. field |= TRB_IOC;
  1206. }
  1207. xhci_dbg(xhci, " sg entry: dma = %#x, len = %#x (%d), "
  1208. "64KB boundary at %#x, end dma = %#x\n",
  1209. (unsigned int) addr, trb_buff_len, trb_buff_len,
  1210. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1211. (unsigned int) addr + trb_buff_len);
  1212. if (TRB_MAX_BUFF_SIZE -
  1213. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1)) < trb_buff_len) {
  1214. xhci_warn(xhci, "WARN: sg dma xfer crosses 64KB boundaries!\n");
  1215. xhci_dbg(xhci, "Next boundary at %#x, end dma = %#x\n",
  1216. (unsigned int) (addr + TRB_MAX_BUFF_SIZE) & ~(TRB_MAX_BUFF_SIZE - 1),
  1217. (unsigned int) addr + trb_buff_len);
  1218. }
  1219. queue_trb(xhci, ep_ring, false,
  1220. (u32) addr,
  1221. (u32) ((u64) addr >> 32),
  1222. TRB_LEN(trb_buff_len) | TRB_INTR_TARGET(0),
  1223. /* We always want to know if the TRB was short,
  1224. * or we won't get an event when it completes.
  1225. * (Unless we use event data TRBs, which are a
  1226. * waste of space and HC resources.)
  1227. */
  1228. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1229. --num_trbs;
  1230. running_total += trb_buff_len;
  1231. /* Calculate length for next transfer --
  1232. * Are we done queueing all the TRBs for this sg entry?
  1233. */
  1234. this_sg_len -= trb_buff_len;
  1235. if (this_sg_len == 0) {
  1236. --num_sgs;
  1237. if (num_sgs == 0)
  1238. break;
  1239. sg = sg_next(sg);
  1240. addr = (u64) sg_dma_address(sg);
  1241. this_sg_len = sg_dma_len(sg);
  1242. } else {
  1243. addr += trb_buff_len;
  1244. }
  1245. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1246. (addr & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1247. trb_buff_len = min_t(int, trb_buff_len, this_sg_len);
  1248. if (running_total + trb_buff_len > urb->transfer_buffer_length)
  1249. trb_buff_len =
  1250. urb->transfer_buffer_length - running_total;
  1251. } while (running_total < urb->transfer_buffer_length);
  1252. check_trb_math(urb, num_trbs, running_total);
  1253. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1254. return 0;
  1255. }
  1256. /* This is very similar to what ehci-q.c qtd_fill() does */
  1257. int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1258. struct urb *urb, int slot_id, unsigned int ep_index)
  1259. {
  1260. struct xhci_ring *ep_ring;
  1261. struct xhci_td *td;
  1262. int num_trbs;
  1263. struct xhci_generic_trb *start_trb;
  1264. bool first_trb;
  1265. int start_cycle;
  1266. u32 field;
  1267. int running_total, trb_buff_len, ret;
  1268. u64 addr;
  1269. if (urb->sg)
  1270. return queue_bulk_sg_tx(xhci, mem_flags, urb, slot_id, ep_index);
  1271. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1272. num_trbs = 0;
  1273. /* How much data is (potentially) left before the 64KB boundary? */
  1274. running_total = TRB_MAX_BUFF_SIZE -
  1275. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1276. /* If there's some data on this 64KB chunk, or we have to send a
  1277. * zero-length transfer, we need at least one TRB
  1278. */
  1279. if (running_total != 0 || urb->transfer_buffer_length == 0)
  1280. num_trbs++;
  1281. /* How many more 64KB chunks to transfer, how many more TRBs? */
  1282. while (running_total < urb->transfer_buffer_length) {
  1283. num_trbs++;
  1284. running_total += TRB_MAX_BUFF_SIZE;
  1285. }
  1286. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  1287. if (!in_interrupt())
  1288. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %#x (%d), addr = %#llx, num_trbs = %d\n",
  1289. urb->ep->desc.bEndpointAddress,
  1290. urb->transfer_buffer_length,
  1291. urb->transfer_buffer_length,
  1292. (unsigned long long)urb->transfer_dma,
  1293. num_trbs);
  1294. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
  1295. num_trbs, urb, &td, mem_flags);
  1296. if (ret < 0)
  1297. return ret;
  1298. /*
  1299. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1300. * until we've finished creating all the other TRBs. The ring's cycle
  1301. * state may change as we enqueue the other TRBs, so save it too.
  1302. */
  1303. start_trb = &ep_ring->enqueue->generic;
  1304. start_cycle = ep_ring->cycle_state;
  1305. running_total = 0;
  1306. /* How much data is in the first TRB? */
  1307. addr = (u64) urb->transfer_dma;
  1308. trb_buff_len = TRB_MAX_BUFF_SIZE -
  1309. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  1310. if (urb->transfer_buffer_length < trb_buff_len)
  1311. trb_buff_len = urb->transfer_buffer_length;
  1312. first_trb = true;
  1313. /* Queue the first TRB, even if it's zero-length */
  1314. do {
  1315. field = 0;
  1316. /* Don't change the cycle bit of the first TRB until later */
  1317. if (first_trb)
  1318. first_trb = false;
  1319. else
  1320. field |= ep_ring->cycle_state;
  1321. /* Chain all the TRBs together; clear the chain bit in the last
  1322. * TRB to indicate it's the last TRB in the chain.
  1323. */
  1324. if (num_trbs > 1) {
  1325. field |= TRB_CHAIN;
  1326. } else {
  1327. /* FIXME - add check for ZERO_PACKET flag before this */
  1328. td->last_trb = ep_ring->enqueue;
  1329. field |= TRB_IOC;
  1330. }
  1331. queue_trb(xhci, ep_ring, false,
  1332. (u32) addr,
  1333. (u32) ((u64) addr >> 32),
  1334. TRB_LEN(trb_buff_len) | TRB_INTR_TARGET(0),
  1335. /* We always want to know if the TRB was short,
  1336. * or we won't get an event when it completes.
  1337. * (Unless we use event data TRBs, which are a
  1338. * waste of space and HC resources.)
  1339. */
  1340. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  1341. --num_trbs;
  1342. running_total += trb_buff_len;
  1343. /* Calculate length for next transfer */
  1344. addr += trb_buff_len;
  1345. trb_buff_len = urb->transfer_buffer_length - running_total;
  1346. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  1347. trb_buff_len = TRB_MAX_BUFF_SIZE;
  1348. } while (running_total < urb->transfer_buffer_length);
  1349. check_trb_math(urb, num_trbs, running_total);
  1350. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1351. return 0;
  1352. }
  1353. /* Caller must have locked xhci->lock */
  1354. int xhci_queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  1355. struct urb *urb, int slot_id, unsigned int ep_index)
  1356. {
  1357. struct xhci_ring *ep_ring;
  1358. int num_trbs;
  1359. int ret;
  1360. struct usb_ctrlrequest *setup;
  1361. struct xhci_generic_trb *start_trb;
  1362. int start_cycle;
  1363. u32 field;
  1364. struct xhci_td *td;
  1365. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  1366. /*
  1367. * Need to copy setup packet into setup TRB, so we can't use the setup
  1368. * DMA address.
  1369. */
  1370. if (!urb->setup_packet)
  1371. return -EINVAL;
  1372. if (!in_interrupt())
  1373. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  1374. slot_id, ep_index);
  1375. /* 1 TRB for setup, 1 for status */
  1376. num_trbs = 2;
  1377. /*
  1378. * Don't need to check if we need additional event data and normal TRBs,
  1379. * since data in control transfers will never get bigger than 16MB
  1380. * XXX: can we get a buffer that crosses 64KB boundaries?
  1381. */
  1382. if (urb->transfer_buffer_length > 0)
  1383. num_trbs++;
  1384. ret = prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
  1385. urb, &td, mem_flags);
  1386. if (ret < 0)
  1387. return ret;
  1388. /*
  1389. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  1390. * until we've finished creating all the other TRBs. The ring's cycle
  1391. * state may change as we enqueue the other TRBs, so save it too.
  1392. */
  1393. start_trb = &ep_ring->enqueue->generic;
  1394. start_cycle = ep_ring->cycle_state;
  1395. /* Queue setup TRB - see section 6.4.1.2.1 */
  1396. /* FIXME better way to translate setup_packet into two u32 fields? */
  1397. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  1398. queue_trb(xhci, ep_ring, false,
  1399. /* FIXME endianness is probably going to bite my ass here. */
  1400. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  1401. setup->wIndex | setup->wLength << 16,
  1402. TRB_LEN(8) | TRB_INTR_TARGET(0),
  1403. /* Immediate data in pointer */
  1404. TRB_IDT | TRB_TYPE(TRB_SETUP));
  1405. /* If there's data, queue data TRBs */
  1406. field = 0;
  1407. if (urb->transfer_buffer_length > 0) {
  1408. if (setup->bRequestType & USB_DIR_IN)
  1409. field |= TRB_DIR_IN;
  1410. queue_trb(xhci, ep_ring, false,
  1411. lower_32_bits(urb->transfer_dma),
  1412. upper_32_bits(urb->transfer_dma),
  1413. TRB_LEN(urb->transfer_buffer_length) | TRB_INTR_TARGET(0),
  1414. /* Event on short tx */
  1415. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  1416. }
  1417. /* Save the DMA address of the last TRB in the TD */
  1418. td->last_trb = ep_ring->enqueue;
  1419. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  1420. /* If the device sent data, the status stage is an OUT transfer */
  1421. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  1422. field = 0;
  1423. else
  1424. field = TRB_DIR_IN;
  1425. queue_trb(xhci, ep_ring, false,
  1426. 0,
  1427. 0,
  1428. TRB_INTR_TARGET(0),
  1429. /* Event on completion */
  1430. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  1431. giveback_first_trb(xhci, slot_id, ep_index, start_cycle, start_trb, td);
  1432. return 0;
  1433. }
  1434. /**** Command Ring Operations ****/
  1435. /* Generic function for queueing a command TRB on the command ring */
  1436. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4)
  1437. {
  1438. if (!room_on_ring(xhci, xhci->cmd_ring, 1)) {
  1439. if (!in_interrupt())
  1440. xhci_err(xhci, "ERR: No room for command on command ring\n");
  1441. return -ENOMEM;
  1442. }
  1443. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  1444. field4 | xhci->cmd_ring->cycle_state);
  1445. return 0;
  1446. }
  1447. /* Queue a no-op command on the command ring */
  1448. static int queue_cmd_noop(struct xhci_hcd *xhci)
  1449. {
  1450. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP));
  1451. }
  1452. /*
  1453. * Place a no-op command on the command ring to test the command and
  1454. * event ring.
  1455. */
  1456. void *xhci_setup_one_noop(struct xhci_hcd *xhci)
  1457. {
  1458. if (queue_cmd_noop(xhci) < 0)
  1459. return NULL;
  1460. xhci->noops_submitted++;
  1461. return xhci_ring_cmd_db;
  1462. }
  1463. /* Queue a slot enable or disable request on the command ring */
  1464. int xhci_queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  1465. {
  1466. return queue_command(xhci, 0, 0, 0,
  1467. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id));
  1468. }
  1469. /* Queue an address device command TRB */
  1470. int xhci_queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1471. u32 slot_id)
  1472. {
  1473. return queue_command(xhci, in_ctx_ptr, 0, 0,
  1474. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id));
  1475. }
  1476. /* Queue a configure endpoint command TRB */
  1477. int xhci_queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr,
  1478. u32 slot_id)
  1479. {
  1480. return queue_command(xhci, in_ctx_ptr, 0, 0,
  1481. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id));
  1482. }
  1483. int xhci_queue_stop_endpoint(struct xhci_hcd *xhci, int slot_id,
  1484. unsigned int ep_index)
  1485. {
  1486. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1487. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1488. u32 type = TRB_TYPE(TRB_STOP_RING);
  1489. return queue_command(xhci, 0, 0, 0,
  1490. trb_slot_id | trb_ep_index | type);
  1491. }
  1492. /* Set Transfer Ring Dequeue Pointer command.
  1493. * This should not be used for endpoints that have streams enabled.
  1494. */
  1495. static int queue_set_tr_deq(struct xhci_hcd *xhci, int slot_id,
  1496. unsigned int ep_index, struct xhci_segment *deq_seg,
  1497. union xhci_trb *deq_ptr, u32 cycle_state)
  1498. {
  1499. dma_addr_t addr;
  1500. u32 trb_slot_id = SLOT_ID_FOR_TRB(slot_id);
  1501. u32 trb_ep_index = EP_ID_FOR_TRB(ep_index);
  1502. u32 type = TRB_TYPE(TRB_SET_DEQ);
  1503. addr = xhci_trb_virt_to_dma(deq_seg, deq_ptr);
  1504. if (addr == 0)
  1505. xhci_warn(xhci, "WARN Cannot submit Set TR Deq Ptr\n");
  1506. xhci_warn(xhci, "WARN deq seg = %p, deq pt = %p\n",
  1507. deq_seg, deq_ptr);
  1508. return queue_command(xhci, (u32) addr | cycle_state, 0, 0,
  1509. trb_slot_id | trb_ep_index | type);
  1510. }