xhci-ring.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 "xhci.h"
  66. /*
  67. * Returns zero if the TRB isn't in this segment, otherwise it returns the DMA
  68. * address of the TRB.
  69. */
  70. dma_addr_t trb_virt_to_dma(struct xhci_segment *seg,
  71. union xhci_trb *trb)
  72. {
  73. unsigned int offset;
  74. if (!seg || !trb || (void *) trb < (void *) seg->trbs)
  75. return 0;
  76. /* offset in bytes, since these are byte-addressable */
  77. offset = (unsigned int) trb - (unsigned int) seg->trbs;
  78. /* SEGMENT_SIZE in bytes, trbs are 16-byte aligned */
  79. if (offset > SEGMENT_SIZE || (offset % sizeof(*trb)) != 0)
  80. return 0;
  81. return seg->dma + offset;
  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. /*
  108. * See Cycle bit rules. SW is the consumer for the event ring only.
  109. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  110. */
  111. static void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  112. {
  113. union xhci_trb *next = ++(ring->dequeue);
  114. ring->deq_updates++;
  115. /* Update the dequeue pointer further if that was a link TRB or we're at
  116. * the end of an event ring segment (which doesn't have link TRBS)
  117. */
  118. while (last_trb(xhci, ring, ring->deq_seg, next)) {
  119. if (consumer && last_trb_on_last_seg(xhci, ring, ring->deq_seg, next)) {
  120. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  121. if (!in_interrupt())
  122. xhci_dbg(xhci, "Toggle cycle state for ring 0x%x = %i\n",
  123. (unsigned int) ring,
  124. (unsigned int) ring->cycle_state);
  125. }
  126. ring->deq_seg = ring->deq_seg->next;
  127. ring->dequeue = ring->deq_seg->trbs;
  128. next = ring->dequeue;
  129. }
  130. }
  131. /*
  132. * See Cycle bit rules. SW is the consumer for the event ring only.
  133. * Don't make a ring full of link TRBs. That would be dumb and this would loop.
  134. *
  135. * If we've just enqueued a TRB that is in the middle of a TD (meaning the
  136. * chain bit is set), then set the chain bit in all the following link TRBs.
  137. * If we've enqueued the last TRB in a TD, make sure the following link TRBs
  138. * have their chain bit cleared (so that each Link TRB is a separate TD).
  139. *
  140. * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
  141. * set, but other sections talk about dealing with the chain bit set.
  142. * Assume section 6.4.4.1 is wrong, and the chain bit can be set in a Link TRB.
  143. */
  144. static void inc_enq(struct xhci_hcd *xhci, struct xhci_ring *ring, bool consumer)
  145. {
  146. u32 chain;
  147. union xhci_trb *next;
  148. chain = ring->enqueue->generic.field[3] & TRB_CHAIN;
  149. next = ++(ring->enqueue);
  150. ring->enq_updates++;
  151. /* Update the dequeue pointer further if that was a link TRB or we're at
  152. * the end of an event ring segment (which doesn't have link TRBS)
  153. */
  154. while (last_trb(xhci, ring, ring->enq_seg, next)) {
  155. if (!consumer) {
  156. if (ring != xhci->event_ring) {
  157. /* Give this link TRB to the hardware */
  158. if (next->link.control & TRB_CYCLE)
  159. next->link.control &= (u32) ~TRB_CYCLE;
  160. else
  161. next->link.control |= (u32) TRB_CYCLE;
  162. next->link.control &= TRB_CHAIN;
  163. next->link.control |= chain;
  164. }
  165. /* Toggle the cycle bit after the last ring segment. */
  166. if (last_trb_on_last_seg(xhci, ring, ring->enq_seg, next)) {
  167. ring->cycle_state = (ring->cycle_state ? 0 : 1);
  168. if (!in_interrupt())
  169. xhci_dbg(xhci, "Toggle cycle state for ring 0x%x = %i\n",
  170. (unsigned int) ring,
  171. (unsigned int) ring->cycle_state);
  172. }
  173. }
  174. ring->enq_seg = ring->enq_seg->next;
  175. ring->enqueue = ring->enq_seg->trbs;
  176. next = ring->enqueue;
  177. }
  178. }
  179. /*
  180. * Check to see if there's room to enqueue num_trbs on the ring. See rules
  181. * above.
  182. * FIXME: this would be simpler and faster if we just kept track of the number
  183. * of free TRBs in a ring.
  184. */
  185. static int room_on_ring(struct xhci_hcd *xhci, struct xhci_ring *ring,
  186. unsigned int num_trbs)
  187. {
  188. int i;
  189. union xhci_trb *enq = ring->enqueue;
  190. struct xhci_segment *enq_seg = ring->enq_seg;
  191. /* Check if ring is empty */
  192. if (enq == ring->dequeue)
  193. return 1;
  194. /* Make sure there's an extra empty TRB available */
  195. for (i = 0; i <= num_trbs; ++i) {
  196. if (enq == ring->dequeue)
  197. return 0;
  198. enq++;
  199. while (last_trb(xhci, ring, enq_seg, enq)) {
  200. enq_seg = enq_seg->next;
  201. enq = enq_seg->trbs;
  202. }
  203. }
  204. return 1;
  205. }
  206. void set_hc_event_deq(struct xhci_hcd *xhci)
  207. {
  208. u32 temp;
  209. dma_addr_t deq;
  210. deq = trb_virt_to_dma(xhci->event_ring->deq_seg,
  211. xhci->event_ring->dequeue);
  212. if (deq == 0 && !in_interrupt())
  213. xhci_warn(xhci, "WARN something wrong with SW event ring "
  214. "dequeue ptr.\n");
  215. /* Update HC event ring dequeue pointer */
  216. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
  217. temp &= ERST_PTR_MASK;
  218. if (!in_interrupt())
  219. xhci_dbg(xhci, "// Write event ring dequeue pointer\n");
  220. xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[1]);
  221. xhci_writel(xhci, (deq & ~ERST_PTR_MASK) | temp,
  222. &xhci->ir_set->erst_dequeue[0]);
  223. }
  224. /* Ring the host controller doorbell after placing a command on the ring */
  225. void ring_cmd_db(struct xhci_hcd *xhci)
  226. {
  227. u32 temp;
  228. xhci_dbg(xhci, "// Ding dong!\n");
  229. temp = xhci_readl(xhci, &xhci->dba->doorbell[0]) & DB_MASK;
  230. xhci_writel(xhci, temp | DB_TARGET_HOST, &xhci->dba->doorbell[0]);
  231. /* Flush PCI posted writes */
  232. xhci_readl(xhci, &xhci->dba->doorbell[0]);
  233. }
  234. static void handle_cmd_completion(struct xhci_hcd *xhci,
  235. struct xhci_event_cmd *event)
  236. {
  237. int slot_id = TRB_TO_SLOT_ID(event->flags);
  238. u64 cmd_dma;
  239. dma_addr_t cmd_dequeue_dma;
  240. cmd_dma = (((u64) event->cmd_trb[1]) << 32) + event->cmd_trb[0];
  241. cmd_dequeue_dma = trb_virt_to_dma(xhci->cmd_ring->deq_seg,
  242. xhci->cmd_ring->dequeue);
  243. /* Is the command ring deq ptr out of sync with the deq seg ptr? */
  244. if (cmd_dequeue_dma == 0) {
  245. xhci->error_bitmask |= 1 << 4;
  246. return;
  247. }
  248. /* Does the DMA address match our internal dequeue pointer address? */
  249. if (cmd_dma != (u64) cmd_dequeue_dma) {
  250. xhci->error_bitmask |= 1 << 5;
  251. return;
  252. }
  253. switch (xhci->cmd_ring->dequeue->generic.field[3] & TRB_TYPE_BITMASK) {
  254. case TRB_TYPE(TRB_ENABLE_SLOT):
  255. if (GET_COMP_CODE(event->status) == COMP_SUCCESS)
  256. xhci->slot_id = slot_id;
  257. else
  258. xhci->slot_id = 0;
  259. complete(&xhci->addr_dev);
  260. break;
  261. case TRB_TYPE(TRB_DISABLE_SLOT):
  262. if (xhci->devs[slot_id])
  263. xhci_free_virt_device(xhci, slot_id);
  264. break;
  265. case TRB_TYPE(TRB_CONFIG_EP):
  266. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  267. complete(&xhci->devs[slot_id]->cmd_completion);
  268. break;
  269. case TRB_TYPE(TRB_ADDR_DEV):
  270. xhci->devs[slot_id]->cmd_status = GET_COMP_CODE(event->status);
  271. complete(&xhci->addr_dev);
  272. break;
  273. case TRB_TYPE(TRB_CMD_NOOP):
  274. ++xhci->noops_handled;
  275. break;
  276. default:
  277. /* Skip over unknown commands on the event ring */
  278. xhci->error_bitmask |= 1 << 6;
  279. break;
  280. }
  281. inc_deq(xhci, xhci->cmd_ring, false);
  282. }
  283. static void handle_port_status(struct xhci_hcd *xhci,
  284. union xhci_trb *event)
  285. {
  286. u32 port_id;
  287. /* Port status change events always have a successful completion code */
  288. if (GET_COMP_CODE(event->generic.field[2]) != COMP_SUCCESS) {
  289. xhci_warn(xhci, "WARN: xHC returned failed port status event\n");
  290. xhci->error_bitmask |= 1 << 8;
  291. }
  292. /* FIXME: core doesn't care about all port link state changes yet */
  293. port_id = GET_PORT_ID(event->generic.field[0]);
  294. xhci_dbg(xhci, "Port Status Change Event for port %d\n", port_id);
  295. /* Update event ring dequeue pointer before dropping the lock */
  296. inc_deq(xhci, xhci->event_ring, true);
  297. set_hc_event_deq(xhci);
  298. spin_unlock(&xhci->lock);
  299. /* Pass this up to the core */
  300. usb_hcd_poll_rh_status(xhci_to_hcd(xhci));
  301. spin_lock(&xhci->lock);
  302. }
  303. /*
  304. * This TD is defined by the TRBs starting at start_trb in start_seg and ending
  305. * at end_trb, which may be in another segment. If the suspect DMA address is a
  306. * TRB in this TD, this function returns that TRB's segment. Otherwise it
  307. * returns 0.
  308. */
  309. static struct xhci_segment *trb_in_td(
  310. struct xhci_segment *start_seg,
  311. union xhci_trb *start_trb,
  312. union xhci_trb *end_trb,
  313. dma_addr_t suspect_dma)
  314. {
  315. dma_addr_t start_dma;
  316. dma_addr_t end_seg_dma;
  317. dma_addr_t end_trb_dma;
  318. struct xhci_segment *cur_seg;
  319. start_dma = trb_virt_to_dma(start_seg, start_trb);
  320. cur_seg = start_seg;
  321. do {
  322. /*
  323. * Last TRB is a link TRB (unless we start inserting links in
  324. * the middle, FIXME if you do)
  325. */
  326. end_seg_dma = trb_virt_to_dma(cur_seg, &start_seg->trbs[TRBS_PER_SEGMENT - 2]);
  327. /* If the end TRB isn't in this segment, this is set to 0 */
  328. end_trb_dma = trb_virt_to_dma(cur_seg, end_trb);
  329. if (end_trb_dma > 0) {
  330. /* The end TRB is in this segment, so suspect should be here */
  331. if (start_dma <= end_trb_dma) {
  332. if (suspect_dma >= start_dma && suspect_dma <= end_trb_dma)
  333. return cur_seg;
  334. } else {
  335. /* Case for one segment with
  336. * a TD wrapped around to the top
  337. */
  338. if ((suspect_dma >= start_dma &&
  339. suspect_dma <= end_seg_dma) ||
  340. (suspect_dma >= cur_seg->dma &&
  341. suspect_dma <= end_trb_dma))
  342. return cur_seg;
  343. }
  344. return 0;
  345. } else {
  346. /* Might still be somewhere in this segment */
  347. if (suspect_dma >= start_dma && suspect_dma <= end_seg_dma)
  348. return cur_seg;
  349. }
  350. cur_seg = cur_seg->next;
  351. start_dma = trb_virt_to_dma(cur_seg, &cur_seg->trbs[0]);
  352. } while (1);
  353. }
  354. /*
  355. * If this function returns an error condition, it means it got a Transfer
  356. * event with a corrupted Slot ID, Endpoint ID, or TRB DMA address.
  357. * At this point, the host controller is probably hosed and should be reset.
  358. */
  359. static int handle_tx_event(struct xhci_hcd *xhci,
  360. struct xhci_transfer_event *event)
  361. {
  362. struct xhci_virt_device *xdev;
  363. struct xhci_ring *ep_ring;
  364. int ep_index;
  365. struct xhci_td *td = 0;
  366. dma_addr_t event_dma;
  367. struct xhci_segment *event_seg;
  368. union xhci_trb *event_trb;
  369. struct urb *urb;
  370. int status = -EINPROGRESS;
  371. xdev = xhci->devs[TRB_TO_SLOT_ID(event->flags)];
  372. if (!xdev) {
  373. xhci_err(xhci, "ERROR Transfer event pointed to bad slot\n");
  374. return -ENODEV;
  375. }
  376. /* Endpoint ID is 1 based, our index is zero based */
  377. ep_index = TRB_TO_EP_ID(event->flags) - 1;
  378. ep_ring = xdev->ep_rings[ep_index];
  379. if (!ep_ring || (xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK) == EP_STATE_DISABLED) {
  380. xhci_err(xhci, "ERROR Transfer event pointed to disabled endpoint\n");
  381. return -ENODEV;
  382. }
  383. event_dma = event->buffer[0];
  384. if (event->buffer[1] != 0)
  385. xhci_warn(xhci, "WARN ignoring upper 32-bits of 64-bit TRB dma address\n");
  386. /* This TRB should be in the TD at the head of this ring's TD list */
  387. if (list_empty(&ep_ring->td_list)) {
  388. xhci_warn(xhci, "WARN Event TRB for slot %d ep %d with no TDs queued?\n",
  389. TRB_TO_SLOT_ID(event->flags), ep_index);
  390. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  391. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  392. xhci_print_trb_offsets(xhci, (union xhci_trb *) event);
  393. urb = NULL;
  394. goto cleanup;
  395. }
  396. td = list_entry(ep_ring->td_list.next, struct xhci_td, td_list);
  397. /* Is this a TRB in the currently executing TD? */
  398. event_seg = trb_in_td(ep_ring->deq_seg, ep_ring->dequeue,
  399. td->last_trb, event_dma);
  400. if (!event_seg) {
  401. /* HC is busted, give up! */
  402. xhci_err(xhci, "ERROR Transfer event TRB DMA ptr not part of current TD\n");
  403. return -ESHUTDOWN;
  404. }
  405. event_trb = &event_seg->trbs[(event_dma - event_seg->dma) / sizeof(*event_trb)];
  406. xhci_dbg(xhci, "Event TRB with TRB type ID %u\n",
  407. (unsigned int) (event->flags & TRB_TYPE_BITMASK)>>10);
  408. xhci_dbg(xhci, "Offset 0x00 (buffer[0]) = 0x%x\n",
  409. (unsigned int) event->buffer[0]);
  410. xhci_dbg(xhci, "Offset 0x04 (buffer[0]) = 0x%x\n",
  411. (unsigned int) event->buffer[1]);
  412. xhci_dbg(xhci, "Offset 0x08 (transfer length) = 0x%x\n",
  413. (unsigned int) event->transfer_len);
  414. xhci_dbg(xhci, "Offset 0x0C (flags) = 0x%x\n",
  415. (unsigned int) event->flags);
  416. /* Look for common error cases */
  417. switch (GET_COMP_CODE(event->transfer_len)) {
  418. /* Skip codes that require special handling depending on
  419. * transfer type
  420. */
  421. case COMP_SUCCESS:
  422. case COMP_SHORT_TX:
  423. break;
  424. case COMP_STALL:
  425. xhci_warn(xhci, "WARN: Stalled endpoint\n");
  426. status = -EPIPE;
  427. break;
  428. case COMP_TRB_ERR:
  429. xhci_warn(xhci, "WARN: TRB error on endpoint\n");
  430. status = -EILSEQ;
  431. break;
  432. case COMP_TX_ERR:
  433. xhci_warn(xhci, "WARN: transfer error on endpoint\n");
  434. status = -EPROTO;
  435. break;
  436. case COMP_DB_ERR:
  437. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough\n");
  438. status = -ENOSR;
  439. break;
  440. default:
  441. xhci_warn(xhci, "ERROR Unknown event condition, HC probably busted\n");
  442. urb = NULL;
  443. goto cleanup;
  444. }
  445. /* Now update the urb's actual_length and give back to the core */
  446. /* Was this a control transfer? */
  447. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  448. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  449. switch (GET_COMP_CODE(event->transfer_len)) {
  450. case COMP_SUCCESS:
  451. if (event_trb == ep_ring->dequeue) {
  452. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  453. status = -ESHUTDOWN;
  454. } else if (event_trb != td->last_trb) {
  455. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  456. status = -ESHUTDOWN;
  457. } else {
  458. xhci_dbg(xhci, "Successful control transfer!\n");
  459. status = 0;
  460. }
  461. break;
  462. case COMP_SHORT_TX:
  463. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  464. status = -EREMOTEIO;
  465. break;
  466. default:
  467. /* Others already handled above */
  468. break;
  469. }
  470. /*
  471. * Did we transfer any data, despite the errors that might have
  472. * happened? I.e. did we get past the setup stage?
  473. */
  474. if (event_trb != ep_ring->dequeue) {
  475. /* The event was for the status stage */
  476. if (event_trb == td->last_trb) {
  477. td->urb->actual_length = td->urb->transfer_buffer_length;
  478. } else {
  479. /* The event was for the data stage */
  480. td->urb->actual_length = td->urb->transfer_buffer_length -
  481. TRB_LEN(event->transfer_len);
  482. }
  483. }
  484. } else {
  485. switch (GET_COMP_CODE(event->transfer_len)) {
  486. case COMP_SUCCESS:
  487. /* Double check that the HW transferred everything. */
  488. if (event_trb != td->last_trb) {
  489. xhci_warn(xhci, "WARN Successful completion "
  490. "on short TX\n");
  491. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  492. status = -EREMOTEIO;
  493. else
  494. status = 0;
  495. } else {
  496. xhci_dbg(xhci, "Successful bulk transfer!\n");
  497. status = 0;
  498. }
  499. break;
  500. case COMP_SHORT_TX:
  501. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  502. status = -EREMOTEIO;
  503. else
  504. status = 0;
  505. break;
  506. default:
  507. /* Others already handled above */
  508. break;
  509. }
  510. dev_dbg(&td->urb->dev->dev,
  511. "ep %#x - asked for %d bytes, "
  512. "%d bytes untransferred\n",
  513. td->urb->ep->desc.bEndpointAddress,
  514. td->urb->transfer_buffer_length,
  515. TRB_LEN(event->transfer_len));
  516. /* Fast path - was this the last TRB in the TD for this URB? */
  517. if (event_trb == td->last_trb) {
  518. if (TRB_LEN(event->transfer_len) != 0) {
  519. td->urb->actual_length =
  520. td->urb->transfer_buffer_length -
  521. TRB_LEN(event->transfer_len);
  522. if (td->urb->actual_length < 0) {
  523. xhci_warn(xhci, "HC gave bad length "
  524. "of %d bytes left\n",
  525. TRB_LEN(event->transfer_len));
  526. td->urb->actual_length = 0;
  527. }
  528. if (td->urb->transfer_flags & URB_SHORT_NOT_OK)
  529. status = -EREMOTEIO;
  530. else
  531. status = 0;
  532. } else {
  533. td->urb->actual_length = td->urb->transfer_buffer_length;
  534. /* Ignore a short packet completion if the
  535. * untransferred length was zero.
  536. */
  537. status = 0;
  538. }
  539. } else {
  540. /* Slow path - walk the list, starting from the first
  541. * TRB to get the actual length transferred
  542. */
  543. td->urb->actual_length = 0;
  544. while (ep_ring->dequeue != event_trb) {
  545. td->urb->actual_length += TRB_LEN(ep_ring->dequeue->generic.field[2]);
  546. inc_deq(xhci, ep_ring, false);
  547. }
  548. td->urb->actual_length += TRB_LEN(ep_ring->dequeue->generic.field[2]) -
  549. TRB_LEN(event->transfer_len);
  550. }
  551. }
  552. /* Update ring dequeue pointer */
  553. while (ep_ring->dequeue != td->last_trb)
  554. inc_deq(xhci, ep_ring, false);
  555. inc_deq(xhci, ep_ring, false);
  556. /* Clean up the endpoint's TD list */
  557. urb = td->urb;
  558. list_del(&td->td_list);
  559. kfree(td);
  560. urb->hcpriv = NULL;
  561. cleanup:
  562. inc_deq(xhci, xhci->event_ring, true);
  563. set_hc_event_deq(xhci);
  564. /* FIXME for multi-TD URBs (who have buffers bigger than 64MB) */
  565. if (urb) {
  566. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  567. spin_unlock(&xhci->lock);
  568. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  569. spin_lock(&xhci->lock);
  570. }
  571. return 0;
  572. }
  573. /*
  574. * This function handles all OS-owned events on the event ring. It may drop
  575. * xhci->lock between event processing (e.g. to pass up port status changes).
  576. */
  577. void handle_event(struct xhci_hcd *xhci)
  578. {
  579. union xhci_trb *event;
  580. int update_ptrs = 1;
  581. int ret;
  582. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  583. xhci->error_bitmask |= 1 << 1;
  584. return;
  585. }
  586. event = xhci->event_ring->dequeue;
  587. /* Does the HC or OS own the TRB? */
  588. if ((event->event_cmd.flags & TRB_CYCLE) !=
  589. xhci->event_ring->cycle_state) {
  590. xhci->error_bitmask |= 1 << 2;
  591. return;
  592. }
  593. /* FIXME: Handle more event types. */
  594. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  595. case TRB_TYPE(TRB_COMPLETION):
  596. handle_cmd_completion(xhci, &event->event_cmd);
  597. break;
  598. case TRB_TYPE(TRB_PORT_STATUS):
  599. handle_port_status(xhci, event);
  600. update_ptrs = 0;
  601. break;
  602. case TRB_TYPE(TRB_TRANSFER):
  603. ret = handle_tx_event(xhci, &event->trans_event);
  604. if (ret < 0)
  605. xhci->error_bitmask |= 1 << 9;
  606. else
  607. update_ptrs = 0;
  608. break;
  609. default:
  610. xhci->error_bitmask |= 1 << 3;
  611. }
  612. if (update_ptrs) {
  613. /* Update SW and HC event ring dequeue pointer */
  614. inc_deq(xhci, xhci->event_ring, true);
  615. set_hc_event_deq(xhci);
  616. }
  617. /* Are there more items on the event ring? */
  618. handle_event(xhci);
  619. }
  620. /**** Endpoint Ring Operations ****/
  621. /*
  622. * Generic function for queueing a TRB on a ring.
  623. * The caller must have checked to make sure there's room on the ring.
  624. */
  625. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  626. bool consumer,
  627. u32 field1, u32 field2, u32 field3, u32 field4)
  628. {
  629. struct xhci_generic_trb *trb;
  630. trb = &ring->enqueue->generic;
  631. trb->field[0] = field1;
  632. trb->field[1] = field2;
  633. trb->field[2] = field3;
  634. trb->field[3] = field4;
  635. inc_enq(xhci, ring, consumer);
  636. }
  637. /*
  638. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  639. * FIXME allocate segments if the ring is full.
  640. */
  641. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  642. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  643. {
  644. /* Make sure the endpoint has been added to xHC schedule */
  645. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  646. switch (ep_state) {
  647. case EP_STATE_DISABLED:
  648. /*
  649. * USB core changed config/interfaces without notifying us,
  650. * or hardware is reporting the wrong state.
  651. */
  652. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  653. return -ENOENT;
  654. case EP_STATE_HALTED:
  655. case EP_STATE_ERROR:
  656. xhci_warn(xhci, "WARN waiting for halt or error on ep "
  657. "to be cleared\n");
  658. /* FIXME event handling code for error needs to clear it */
  659. /* XXX not sure if this should be -ENOENT or not */
  660. return -EINVAL;
  661. case EP_STATE_STOPPED:
  662. case EP_STATE_RUNNING:
  663. break;
  664. default:
  665. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  666. /*
  667. * FIXME issue Configure Endpoint command to try to get the HC
  668. * back into a known state.
  669. */
  670. return -EINVAL;
  671. }
  672. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  673. /* FIXME allocate more room */
  674. xhci_err(xhci, "ERROR no room on ep ring\n");
  675. return -ENOMEM;
  676. }
  677. return 0;
  678. }
  679. int xhci_prepare_transfer(struct xhci_hcd *xhci,
  680. struct xhci_virt_device *xdev,
  681. unsigned int ep_index,
  682. unsigned int num_trbs,
  683. struct urb *urb,
  684. struct xhci_td **td,
  685. gfp_t mem_flags)
  686. {
  687. int ret;
  688. ret = prepare_ring(xhci, xdev->ep_rings[ep_index],
  689. xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK,
  690. num_trbs, mem_flags);
  691. if (ret)
  692. return ret;
  693. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  694. if (!*td)
  695. return -ENOMEM;
  696. INIT_LIST_HEAD(&(*td)->td_list);
  697. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  698. if (unlikely(ret)) {
  699. kfree(*td);
  700. return ret;
  701. }
  702. (*td)->urb = urb;
  703. urb->hcpriv = (void *) (*td);
  704. /* Add this TD to the tail of the endpoint ring's TD list */
  705. list_add_tail(&(*td)->td_list, &xdev->ep_rings[ep_index]->td_list);
  706. return 0;
  707. }
  708. /* This is very similar to what ehci-q.c qtd_fill() does */
  709. int queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  710. struct urb *urb, int slot_id, unsigned int ep_index)
  711. {
  712. struct xhci_ring *ep_ring;
  713. struct xhci_td *td;
  714. int num_trbs;
  715. struct xhci_generic_trb *start_trb;
  716. bool first_trb;
  717. int start_cycle;
  718. u32 field;
  719. int running_total, trb_buff_len, ret;
  720. u64 addr;
  721. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  722. num_trbs = 0;
  723. /* How much data is (potentially) left before the 64KB boundary? */
  724. running_total = TRB_MAX_BUFF_SIZE -
  725. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  726. /* If there's some data on this 64KB chunk, or we have to send a
  727. * zero-length transfer, we need at least one TRB
  728. */
  729. if (running_total != 0 || urb->transfer_buffer_length == 0)
  730. num_trbs++;
  731. /* How many more 64KB chunks to transfer, how many more TRBs? */
  732. while (running_total < urb->transfer_buffer_length) {
  733. num_trbs++;
  734. running_total += TRB_MAX_BUFF_SIZE;
  735. }
  736. /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
  737. if (!in_interrupt())
  738. dev_dbg(&urb->dev->dev, "ep %#x - urb len = %d, addr = %#x, num_trbs = %d\n",
  739. urb->ep->desc.bEndpointAddress,
  740. urb->transfer_buffer_length, urb->transfer_dma,
  741. num_trbs);
  742. ret = xhci_prepare_transfer(xhci, xhci->devs[slot_id], ep_index,
  743. num_trbs, urb, &td, mem_flags);
  744. if (ret < 0)
  745. return ret;
  746. /*
  747. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  748. * until we've finished creating all the other TRBs. The ring's cycle
  749. * state may change as we enqueue the other TRBs, so save it too.
  750. */
  751. start_trb = &ep_ring->enqueue->generic;
  752. start_cycle = ep_ring->cycle_state;
  753. running_total = 0;
  754. /* How much data is in the first TRB? */
  755. addr = (u64) urb->transfer_dma;
  756. trb_buff_len = TRB_MAX_BUFF_SIZE -
  757. (urb->transfer_dma & ((1 << TRB_MAX_BUFF_SHIFT) - 1));
  758. if (urb->transfer_buffer_length < trb_buff_len)
  759. trb_buff_len = urb->transfer_buffer_length;
  760. first_trb = true;
  761. /* Queue the first TRB, even if it's zero-length */
  762. do {
  763. field = 0;
  764. /* Don't change the cycle bit of the first TRB until later */
  765. if (first_trb)
  766. first_trb = false;
  767. else
  768. field |= ep_ring->cycle_state;
  769. /* Chain all the TRBs together; clear the chain bit in the last
  770. * TRB to indicate it's the last TRB in the chain.
  771. */
  772. if (num_trbs > 1) {
  773. field |= TRB_CHAIN;
  774. } else {
  775. /* FIXME - add check for ZERO_PACKET flag before this */
  776. td->last_trb = ep_ring->enqueue;
  777. field |= TRB_IOC;
  778. }
  779. queue_trb(xhci, ep_ring, false,
  780. (u32) addr,
  781. (u32) ((u64) addr >> 32),
  782. TRB_LEN(trb_buff_len) | TRB_INTR_TARGET(0),
  783. /* We always want to know if the TRB was short,
  784. * or we won't get an event when it completes.
  785. * (Unless we use event data TRBs, which are a
  786. * waste of space and HC resources.)
  787. */
  788. field | TRB_ISP | TRB_TYPE(TRB_NORMAL));
  789. --num_trbs;
  790. running_total += trb_buff_len;
  791. /* Calculate length for next transfer */
  792. addr += trb_buff_len;
  793. trb_buff_len = urb->transfer_buffer_length - running_total;
  794. if (trb_buff_len > TRB_MAX_BUFF_SIZE)
  795. trb_buff_len = TRB_MAX_BUFF_SIZE;
  796. } while (running_total < urb->transfer_buffer_length);
  797. if (num_trbs != 0)
  798. dev_dbg(&urb->dev->dev, "%s - ep %#x - Miscalculated number of "
  799. "TRBs, %d left\n", __FUNCTION__,
  800. urb->ep->desc.bEndpointAddress, num_trbs);
  801. /*
  802. * Pass all the TRBs to the hardware at once and make sure this write
  803. * isn't reordered.
  804. */
  805. wmb();
  806. start_trb->field[3] |= start_cycle;
  807. field = xhci_readl(xhci, &xhci->dba->doorbell[slot_id]) & DB_MASK;
  808. xhci_writel(xhci, field | EPI_TO_DB(ep_index), &xhci->dba->doorbell[slot_id]);
  809. /* Flush PCI posted writes */
  810. xhci_readl(xhci, &xhci->dba->doorbell[slot_id]);
  811. return 0;
  812. }
  813. /* Caller must have locked xhci->lock */
  814. int queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  815. struct urb *urb, int slot_id, unsigned int ep_index)
  816. {
  817. struct xhci_ring *ep_ring;
  818. int num_trbs;
  819. int ret;
  820. struct usb_ctrlrequest *setup;
  821. struct xhci_generic_trb *start_trb;
  822. int start_cycle;
  823. u32 field;
  824. struct xhci_td *td;
  825. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  826. /*
  827. * Need to copy setup packet into setup TRB, so we can't use the setup
  828. * DMA address.
  829. */
  830. if (!urb->setup_packet)
  831. return -EINVAL;
  832. if (!in_interrupt())
  833. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  834. slot_id, ep_index);
  835. /* 1 TRB for setup, 1 for status */
  836. num_trbs = 2;
  837. /*
  838. * Don't need to check if we need additional event data and normal TRBs,
  839. * since data in control transfers will never get bigger than 16MB
  840. * XXX: can we get a buffer that crosses 64KB boundaries?
  841. */
  842. if (urb->transfer_buffer_length > 0)
  843. num_trbs++;
  844. ret = xhci_prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
  845. urb, &td, mem_flags);
  846. if (ret < 0)
  847. return ret;
  848. /*
  849. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  850. * until we've finished creating all the other TRBs. The ring's cycle
  851. * state may change as we enqueue the other TRBs, so save it too.
  852. */
  853. start_trb = &ep_ring->enqueue->generic;
  854. start_cycle = ep_ring->cycle_state;
  855. /* Queue setup TRB - see section 6.4.1.2.1 */
  856. /* FIXME better way to translate setup_packet into two u32 fields? */
  857. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  858. queue_trb(xhci, ep_ring, false,
  859. /* FIXME endianness is probably going to bite my ass here. */
  860. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  861. setup->wIndex | setup->wLength << 16,
  862. TRB_LEN(8) | TRB_INTR_TARGET(0),
  863. /* Immediate data in pointer */
  864. TRB_IDT | TRB_TYPE(TRB_SETUP));
  865. /* If there's data, queue data TRBs */
  866. field = 0;
  867. if (urb->transfer_buffer_length > 0) {
  868. if (setup->bRequestType & USB_DIR_IN)
  869. field |= TRB_DIR_IN;
  870. queue_trb(xhci, ep_ring, false,
  871. lower_32_bits(urb->transfer_dma),
  872. upper_32_bits(urb->transfer_dma),
  873. TRB_LEN(urb->transfer_buffer_length) | TRB_INTR_TARGET(0),
  874. /* Event on short tx */
  875. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  876. }
  877. /* Save the DMA address of the last TRB in the TD */
  878. td->last_trb = ep_ring->enqueue;
  879. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  880. /* If the device sent data, the status stage is an OUT transfer */
  881. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  882. field = 0;
  883. else
  884. field = TRB_DIR_IN;
  885. queue_trb(xhci, ep_ring, false,
  886. 0,
  887. 0,
  888. TRB_INTR_TARGET(0),
  889. /* Event on completion */
  890. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  891. /*
  892. * Pass all the TRBs to the hardware at once and make sure this write
  893. * isn't reordered.
  894. */
  895. wmb();
  896. start_trb->field[3] |= start_cycle;
  897. field = xhci_readl(xhci, &xhci->dba->doorbell[slot_id]) & DB_MASK;
  898. xhci_writel(xhci, field | EPI_TO_DB(ep_index), &xhci->dba->doorbell[slot_id]);
  899. /* Flush PCI posted writes */
  900. xhci_readl(xhci, &xhci->dba->doorbell[slot_id]);
  901. return 0;
  902. }
  903. /**** Command Ring Operations ****/
  904. /* Generic function for queueing a command TRB on the command ring */
  905. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4)
  906. {
  907. if (!room_on_ring(xhci, xhci->cmd_ring, 1)) {
  908. if (!in_interrupt())
  909. xhci_err(xhci, "ERR: No room for command on command ring\n");
  910. return -ENOMEM;
  911. }
  912. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  913. field4 | xhci->cmd_ring->cycle_state);
  914. return 0;
  915. }
  916. /* Queue a no-op command on the command ring */
  917. static int queue_cmd_noop(struct xhci_hcd *xhci)
  918. {
  919. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP));
  920. }
  921. /*
  922. * Place a no-op command on the command ring to test the command and
  923. * event ring.
  924. */
  925. void *setup_one_noop(struct xhci_hcd *xhci)
  926. {
  927. if (queue_cmd_noop(xhci) < 0)
  928. return NULL;
  929. xhci->noops_submitted++;
  930. return ring_cmd_db;
  931. }
  932. /* Queue a slot enable or disable request on the command ring */
  933. int queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  934. {
  935. return queue_command(xhci, 0, 0, 0,
  936. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id));
  937. }
  938. /* Queue an address device command TRB */
  939. int queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, u32 slot_id)
  940. {
  941. return queue_command(xhci, in_ctx_ptr, 0, 0,
  942. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id));
  943. }
  944. /* Queue a configure endpoint command TRB */
  945. int queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, u32 slot_id)
  946. {
  947. return queue_command(xhci, in_ctx_ptr, 0, 0,
  948. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id));
  949. }