xhci-ring.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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 = NULL;
  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. /* Now update the urb's actual_length and give back to the core */
  407. /* Was this a control transfer? */
  408. if (usb_endpoint_xfer_control(&td->urb->ep->desc)) {
  409. xhci_debug_trb(xhci, xhci->event_ring->dequeue);
  410. switch (GET_COMP_CODE(event->transfer_len)) {
  411. case COMP_SUCCESS:
  412. if (event_trb == ep_ring->dequeue) {
  413. xhci_warn(xhci, "WARN: Success on ctrl setup TRB without IOC set??\n");
  414. status = -ESHUTDOWN;
  415. } else if (event_trb != td->last_trb) {
  416. xhci_warn(xhci, "WARN: Success on ctrl data TRB without IOC set??\n");
  417. status = -ESHUTDOWN;
  418. } else {
  419. xhci_dbg(xhci, "Successful control transfer!\n");
  420. status = 0;
  421. }
  422. break;
  423. case COMP_SHORT_TX:
  424. xhci_warn(xhci, "WARN: short transfer on control ep\n");
  425. status = -EREMOTEIO;
  426. break;
  427. case COMP_STALL:
  428. xhci_warn(xhci, "WARN: Stalled control ep\n");
  429. status = -EPIPE;
  430. break;
  431. case COMP_TRB_ERR:
  432. xhci_warn(xhci, "WARN: TRB error on control ep\n");
  433. status = -EILSEQ;
  434. break;
  435. case COMP_TX_ERR:
  436. xhci_warn(xhci, "WARN: transfer error on control ep\n");
  437. status = -EPROTO;
  438. break;
  439. case COMP_DB_ERR:
  440. xhci_warn(xhci, "WARN: HC couldn't access mem fast enough on control TX\n");
  441. status = -ENOSR;
  442. break;
  443. default:
  444. xhci_dbg(xhci, "ERROR Unknown event condition, HC probably busted\n");
  445. goto cleanup;
  446. }
  447. /*
  448. * Did we transfer any data, despite the errors that might have
  449. * happened? I.e. did we get past the setup stage?
  450. */
  451. if (event_trb != ep_ring->dequeue) {
  452. /* The event was for the status stage */
  453. if (event_trb == td->last_trb) {
  454. td->urb->actual_length = td->urb->transfer_buffer_length;
  455. } else {
  456. /* The event was for the data stage */
  457. td->urb->actual_length = td->urb->transfer_buffer_length -
  458. TRB_LEN(event->transfer_len);
  459. }
  460. }
  461. while (ep_ring->dequeue != td->last_trb)
  462. inc_deq(xhci, ep_ring, false);
  463. inc_deq(xhci, ep_ring, false);
  464. /* Clean up the endpoint's TD list */
  465. urb = td->urb;
  466. list_del(&td->td_list);
  467. kfree(td);
  468. } else {
  469. xhci_dbg(xhci, "FIXME do something for non-control transfers\n");
  470. }
  471. cleanup:
  472. inc_deq(xhci, xhci->event_ring, true);
  473. set_hc_event_deq(xhci);
  474. if (urb) {
  475. usb_hcd_unlink_urb_from_ep(xhci_to_hcd(xhci), urb);
  476. spin_unlock(&xhci->lock);
  477. usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, status);
  478. spin_lock(&xhci->lock);
  479. }
  480. return 0;
  481. }
  482. /*
  483. * This function handles all OS-owned events on the event ring. It may drop
  484. * xhci->lock between event processing (e.g. to pass up port status changes).
  485. */
  486. void handle_event(struct xhci_hcd *xhci)
  487. {
  488. union xhci_trb *event;
  489. int update_ptrs = 1;
  490. int ret;
  491. if (!xhci->event_ring || !xhci->event_ring->dequeue) {
  492. xhci->error_bitmask |= 1 << 1;
  493. return;
  494. }
  495. event = xhci->event_ring->dequeue;
  496. /* Does the HC or OS own the TRB? */
  497. if ((event->event_cmd.flags & TRB_CYCLE) !=
  498. xhci->event_ring->cycle_state) {
  499. xhci->error_bitmask |= 1 << 2;
  500. return;
  501. }
  502. /* FIXME: Handle more event types. */
  503. switch ((event->event_cmd.flags & TRB_TYPE_BITMASK)) {
  504. case TRB_TYPE(TRB_COMPLETION):
  505. handle_cmd_completion(xhci, &event->event_cmd);
  506. break;
  507. case TRB_TYPE(TRB_PORT_STATUS):
  508. handle_port_status(xhci, event);
  509. update_ptrs = 0;
  510. break;
  511. case TRB_TYPE(TRB_TRANSFER):
  512. ret = handle_tx_event(xhci, &event->trans_event);
  513. if (ret < 0)
  514. xhci->error_bitmask |= 1 << 9;
  515. else
  516. update_ptrs = 0;
  517. break;
  518. default:
  519. xhci->error_bitmask |= 1 << 3;
  520. }
  521. if (update_ptrs) {
  522. /* Update SW and HC event ring dequeue pointer */
  523. inc_deq(xhci, xhci->event_ring, true);
  524. set_hc_event_deq(xhci);
  525. }
  526. /* Are there more items on the event ring? */
  527. handle_event(xhci);
  528. }
  529. /**** Endpoint Ring Operations ****/
  530. /*
  531. * Generic function for queueing a TRB on a ring.
  532. * The caller must have checked to make sure there's room on the ring.
  533. */
  534. static void queue_trb(struct xhci_hcd *xhci, struct xhci_ring *ring,
  535. bool consumer,
  536. u32 field1, u32 field2, u32 field3, u32 field4)
  537. {
  538. struct xhci_generic_trb *trb;
  539. trb = &ring->enqueue->generic;
  540. trb->field[0] = field1;
  541. trb->field[1] = field2;
  542. trb->field[2] = field3;
  543. trb->field[3] = field4;
  544. inc_enq(xhci, ring, consumer);
  545. }
  546. /*
  547. * Does various checks on the endpoint ring, and makes it ready to queue num_trbs.
  548. * FIXME allocate segments if the ring is full.
  549. */
  550. static int prepare_ring(struct xhci_hcd *xhci, struct xhci_ring *ep_ring,
  551. u32 ep_state, unsigned int num_trbs, gfp_t mem_flags)
  552. {
  553. /* Make sure the endpoint has been added to xHC schedule */
  554. xhci_dbg(xhci, "Endpoint state = 0x%x\n", ep_state);
  555. switch (ep_state) {
  556. case EP_STATE_DISABLED:
  557. /*
  558. * USB core changed config/interfaces without notifying us,
  559. * or hardware is reporting the wrong state.
  560. */
  561. xhci_warn(xhci, "WARN urb submitted to disabled ep\n");
  562. return -ENOENT;
  563. case EP_STATE_HALTED:
  564. case EP_STATE_ERROR:
  565. xhci_warn(xhci, "WARN waiting for halt or error on ep "
  566. "to be cleared\n");
  567. /* FIXME event handling code for error needs to clear it */
  568. /* XXX not sure if this should be -ENOENT or not */
  569. return -EINVAL;
  570. case EP_STATE_STOPPED:
  571. case EP_STATE_RUNNING:
  572. break;
  573. default:
  574. xhci_err(xhci, "ERROR unknown endpoint state for ep\n");
  575. /*
  576. * FIXME issue Configure Endpoint command to try to get the HC
  577. * back into a known state.
  578. */
  579. return -EINVAL;
  580. }
  581. if (!room_on_ring(xhci, ep_ring, num_trbs)) {
  582. /* FIXME allocate more room */
  583. xhci_err(xhci, "ERROR no room on ep ring\n");
  584. return -ENOMEM;
  585. }
  586. return 0;
  587. }
  588. int xhci_prepare_transfer(struct xhci_hcd *xhci,
  589. struct xhci_virt_device *xdev,
  590. unsigned int ep_index,
  591. unsigned int num_trbs,
  592. struct urb *urb,
  593. struct xhci_td **td,
  594. gfp_t mem_flags)
  595. {
  596. int ret;
  597. ret = prepare_ring(xhci, xdev->ep_rings[ep_index],
  598. xdev->out_ctx->ep[ep_index].ep_info & EP_STATE_MASK,
  599. num_trbs, mem_flags);
  600. if (ret)
  601. return ret;
  602. *td = kzalloc(sizeof(struct xhci_td), mem_flags);
  603. if (!*td)
  604. return -ENOMEM;
  605. INIT_LIST_HEAD(&(*td)->td_list);
  606. ret = usb_hcd_link_urb_to_ep(xhci_to_hcd(xhci), urb);
  607. if (unlikely(ret)) {
  608. kfree(*td);
  609. return ret;
  610. }
  611. (*td)->urb = urb;
  612. urb->hcpriv = (void *) (*td);
  613. /* Add this TD to the tail of the endpoint ring's TD list */
  614. list_add_tail(&(*td)->td_list, &xdev->ep_rings[ep_index]->td_list);
  615. return 0;
  616. }
  617. /* Caller must have locked xhci->lock */
  618. int queue_ctrl_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
  619. struct urb *urb, int slot_id, unsigned int ep_index)
  620. {
  621. struct xhci_ring *ep_ring;
  622. int num_trbs;
  623. int ret;
  624. struct usb_ctrlrequest *setup;
  625. struct xhci_generic_trb *start_trb;
  626. int start_cycle;
  627. u32 field;
  628. struct xhci_td *td;
  629. ep_ring = xhci->devs[slot_id]->ep_rings[ep_index];
  630. /*
  631. * Need to copy setup packet into setup TRB, so we can't use the setup
  632. * DMA address.
  633. */
  634. if (!urb->setup_packet)
  635. return -EINVAL;
  636. if (!in_interrupt())
  637. xhci_dbg(xhci, "Queueing ctrl tx for slot id %d, ep %d\n",
  638. slot_id, ep_index);
  639. /* 1 TRB for setup, 1 for status */
  640. num_trbs = 2;
  641. /*
  642. * Don't need to check if we need additional event data and normal TRBs,
  643. * since data in control transfers will never get bigger than 16MB
  644. * XXX: can we get a buffer that crosses 64KB boundaries?
  645. */
  646. if (urb->transfer_buffer_length > 0)
  647. num_trbs++;
  648. ret = xhci_prepare_transfer(xhci, xhci->devs[slot_id], ep_index, num_trbs,
  649. urb, &td, mem_flags);
  650. if (ret < 0)
  651. return ret;
  652. /*
  653. * Don't give the first TRB to the hardware (by toggling the cycle bit)
  654. * until we've finished creating all the other TRBs. The ring's cycle
  655. * state may change as we enqueue the other TRBs, so save it too.
  656. */
  657. start_trb = &ep_ring->enqueue->generic;
  658. start_cycle = ep_ring->cycle_state;
  659. /* Queue setup TRB - see section 6.4.1.2.1 */
  660. /* FIXME better way to translate setup_packet into two u32 fields? */
  661. setup = (struct usb_ctrlrequest *) urb->setup_packet;
  662. queue_trb(xhci, ep_ring, false,
  663. /* FIXME endianness is probably going to bite my ass here. */
  664. setup->bRequestType | setup->bRequest << 8 | setup->wValue << 16,
  665. setup->wIndex | setup->wLength << 16,
  666. TRB_LEN(8) | TRB_INTR_TARGET(0),
  667. /* Immediate data in pointer */
  668. TRB_IDT | TRB_TYPE(TRB_SETUP));
  669. /* If there's data, queue data TRBs */
  670. field = 0;
  671. if (urb->transfer_buffer_length > 0) {
  672. if (setup->bRequestType & USB_DIR_IN)
  673. field |= TRB_DIR_IN;
  674. queue_trb(xhci, ep_ring, false,
  675. lower_32_bits(urb->transfer_dma),
  676. upper_32_bits(urb->transfer_dma),
  677. TRB_LEN(urb->transfer_buffer_length) | TRB_INTR_TARGET(0),
  678. /* Event on short tx */
  679. field | TRB_ISP | TRB_TYPE(TRB_DATA) | ep_ring->cycle_state);
  680. }
  681. /* Save the DMA address of the last TRB in the TD */
  682. td->last_trb = ep_ring->enqueue;
  683. /* Queue status TRB - see Table 7 and sections 4.11.2.2 and 6.4.1.2.3 */
  684. /* If the device sent data, the status stage is an OUT transfer */
  685. if (urb->transfer_buffer_length > 0 && setup->bRequestType & USB_DIR_IN)
  686. field = 0;
  687. else
  688. field = TRB_DIR_IN;
  689. queue_trb(xhci, ep_ring, false,
  690. 0,
  691. 0,
  692. TRB_INTR_TARGET(0),
  693. /* Event on completion */
  694. field | TRB_IOC | TRB_TYPE(TRB_STATUS) | ep_ring->cycle_state);
  695. /*
  696. * Pass all the TRBs to the hardware at once and make sure this write
  697. * isn't reordered.
  698. */
  699. wmb();
  700. start_trb->field[3] |= start_cycle;
  701. field = xhci_readl(xhci, &xhci->dba->doorbell[slot_id]) & DB_MASK;
  702. xhci_writel(xhci, field | EPI_TO_DB(ep_index), &xhci->dba->doorbell[slot_id]);
  703. /* Flush PCI posted writes */
  704. xhci_readl(xhci, &xhci->dba->doorbell[slot_id]);
  705. return 0;
  706. }
  707. /**** Command Ring Operations ****/
  708. /* Generic function for queueing a command TRB on the command ring */
  709. static int queue_command(struct xhci_hcd *xhci, u32 field1, u32 field2, u32 field3, u32 field4)
  710. {
  711. if (!room_on_ring(xhci, xhci->cmd_ring, 1)) {
  712. if (!in_interrupt())
  713. xhci_err(xhci, "ERR: No room for command on command ring\n");
  714. return -ENOMEM;
  715. }
  716. queue_trb(xhci, xhci->cmd_ring, false, field1, field2, field3,
  717. field4 | xhci->cmd_ring->cycle_state);
  718. return 0;
  719. }
  720. /* Queue a no-op command on the command ring */
  721. static int queue_cmd_noop(struct xhci_hcd *xhci)
  722. {
  723. return queue_command(xhci, 0, 0, 0, TRB_TYPE(TRB_CMD_NOOP));
  724. }
  725. /*
  726. * Place a no-op command on the command ring to test the command and
  727. * event ring.
  728. */
  729. void *setup_one_noop(struct xhci_hcd *xhci)
  730. {
  731. if (queue_cmd_noop(xhci) < 0)
  732. return NULL;
  733. xhci->noops_submitted++;
  734. return ring_cmd_db;
  735. }
  736. /* Queue a slot enable or disable request on the command ring */
  737. int queue_slot_control(struct xhci_hcd *xhci, u32 trb_type, u32 slot_id)
  738. {
  739. return queue_command(xhci, 0, 0, 0,
  740. TRB_TYPE(trb_type) | SLOT_ID_FOR_TRB(slot_id));
  741. }
  742. /* Queue an address device command TRB */
  743. int queue_address_device(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, u32 slot_id)
  744. {
  745. return queue_command(xhci, in_ctx_ptr, 0, 0,
  746. TRB_TYPE(TRB_ADDR_DEV) | SLOT_ID_FOR_TRB(slot_id));
  747. }
  748. /* Queue a configure endpoint command TRB */
  749. int queue_configure_endpoint(struct xhci_hcd *xhci, dma_addr_t in_ctx_ptr, u32 slot_id)
  750. {
  751. return queue_command(xhci, in_ctx_ptr, 0, 0,
  752. TRB_TYPE(TRB_CONFIG_EP) | SLOT_ID_FOR_TRB(slot_id));
  753. }