2 * xHCI host controller driver
4 * Copyright (C) 2008 Intel Corp.
7 * Some code borrowed from the Linux EHCI driver.
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.
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
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.
23 #include <linux/irq.h>
24 #include <linux/log2.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/slab.h>
31 #define DRIVER_AUTHOR "Sarah Sharp"
32 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
34 /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
35 static int link_quirk;
36 module_param(link_quirk, int, S_IRUGO | S_IWUSR);
37 MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
39 /* TODO: copied from ehci-hcd.c - can this be refactored? */
41 * handshake - spin reading hc until handshake completes or fails
42 * @ptr: address of hc register to be read
43 * @mask: bits to look at in result of read
44 * @done: value of those bits when handshake succeeds
45 * @usec: timeout in microseconds
47 * Returns negative errno, or zero on success
49 * Success happens when the "mask" bits have the specified value (hardware
50 * handshake done). There are two failure modes: "usec" have passed (major
51 * hardware flakeout), or the register reads as all-ones (hardware removed).
53 static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
54 u32 mask, u32 done, int usec)
59 result = xhci_readl(xhci, ptr);
60 if (result == ~(u32)0) /* card removed */
72 * Disable interrupts and begin the xHCI halting process.
74 void xhci_quiesce(struct xhci_hcd *xhci)
81 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
85 cmd = xhci_readl(xhci, &xhci->op_regs->command);
87 xhci_writel(xhci, cmd, &xhci->op_regs->command);
91 * Force HC into halt state.
93 * Disable any IRQs and clear the run/stop bit.
94 * HC will complete any current and actively pipelined transactions, and
95 * should halt within 16 microframes of the run/stop bit being cleared.
96 * Read HC Halted bit in the status register to see when the HC is finished.
97 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
99 int xhci_halt(struct xhci_hcd *xhci)
101 xhci_dbg(xhci, "// Halt the HC\n");
104 return handshake(xhci, &xhci->op_regs->status,
105 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
109 * Set the run bit and wait for the host to be running.
111 int xhci_start(struct xhci_hcd *xhci)
116 temp = xhci_readl(xhci, &xhci->op_regs->command);
118 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
120 xhci_writel(xhci, temp, &xhci->op_regs->command);
123 * Wait for the HCHalted Status bit to be 0 to indicate the host is
126 ret = handshake(xhci, &xhci->op_regs->status,
127 STS_HALT, 0, XHCI_MAX_HALT_USEC);
128 if (ret == -ETIMEDOUT)
129 xhci_err(xhci, "Host took too long to start, "
130 "waited %u microseconds.\n",
136 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
138 * This resets pipelines, timers, counters, state machines, etc.
139 * Transactions will be terminated immediately, and operational registers
140 * will be set to their defaults.
142 int xhci_reset(struct xhci_hcd *xhci)
148 state = xhci_readl(xhci, &xhci->op_regs->status);
149 if ((state & STS_HALT) == 0) {
150 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
154 xhci_dbg(xhci, "// Reset the HC\n");
155 command = xhci_readl(xhci, &xhci->op_regs->command);
156 command |= CMD_RESET;
157 xhci_writel(xhci, command, &xhci->op_regs->command);
158 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
159 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
161 ret = handshake(xhci, &xhci->op_regs->command,
162 CMD_RESET, 0, 250 * 1000);
166 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
168 * xHCI cannot write to any doorbells or operational registers other
169 * than status until the "Controller Not Ready" flag is cleared.
171 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
176 /* Set up MSI-X table for entry 0 (may claim other entries later) */
177 static int xhci_setup_msix(struct xhci_hcd *xhci)
180 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
182 xhci->msix_count = 0;
183 /* XXX: did I do this right? ixgbe does kcalloc for more than one */
184 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
185 if (!xhci->msix_entries) {
186 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
189 xhci->msix_entries[0].entry = 0;
191 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
193 xhci_err(xhci, "Failed to enable MSI-X\n");
198 * Pass the xhci pointer value as the request_irq "cookie".
199 * If more irqs are added, this will need to be unique for each one.
201 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
202 "xHCI", xhci_to_hcd(xhci));
204 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
207 xhci_dbg(xhci, "Finished setting up MSI-X\n");
211 pci_disable_msix(pdev);
213 kfree(xhci->msix_entries);
214 xhci->msix_entries = NULL;
218 /* XXX: code duplication; can xhci_setup_msix call this? */
219 /* Free any IRQs and disable MSI-X */
220 static void xhci_cleanup_msix(struct xhci_hcd *xhci)
222 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
223 if (!xhci->msix_entries)
226 free_irq(xhci->msix_entries[0].vector, xhci);
227 pci_disable_msix(pdev);
228 kfree(xhci->msix_entries);
229 xhci->msix_entries = NULL;
230 xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
235 * Initialize memory for HCD and xHC (one-time init).
237 * Program the PAGESIZE register, initialize the device context array, create
238 * device contexts (?), set up a command ring segment (or two?), create event
239 * ring (one for now).
241 int xhci_init(struct usb_hcd *hcd)
243 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
246 xhci_dbg(xhci, "xhci_init\n");
247 spin_lock_init(&xhci->lock);
249 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
250 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
252 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
254 retval = xhci_mem_init(xhci, GFP_KERNEL);
255 xhci_dbg(xhci, "Finished xhci_init\n");
261 * Called in interrupt context when there might be work
262 * queued on the event ring
264 * xhci->lock must be held by caller.
266 static void xhci_work(struct xhci_hcd *xhci)
272 * Clear the op reg interrupt status first,
273 * so we can receive interrupts from other MSI-X interrupters.
274 * Write 1 to clear the interrupt status.
276 temp = xhci_readl(xhci, &xhci->op_regs->status);
278 xhci_writel(xhci, temp, &xhci->op_regs->status);
279 /* FIXME when MSI-X is supported and there are multiple vectors */
280 /* Clear the MSI-X event interrupt status */
282 /* Acknowledge the interrupt */
283 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
285 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
286 /* Flush posted writes */
287 xhci_readl(xhci, &xhci->ir_set->irq_pending);
289 if (xhci->xhc_state & XHCI_STATE_DYING)
290 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
291 "Shouldn't IRQs be disabled?\n");
293 /* FIXME this should be a delayed service routine
294 * that clears the EHB.
296 xhci_handle_event(xhci);
298 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
299 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
300 xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
301 /* Flush posted writes -- FIXME is this necessary? */
302 xhci_readl(xhci, &xhci->ir_set->irq_pending);
305 /*-------------------------------------------------------------------------*/
308 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
309 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
310 * indicators of an event TRB error, but we check the status *first* to be safe.
312 irqreturn_t xhci_irq(struct usb_hcd *hcd)
314 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
318 spin_lock(&xhci->lock);
319 trb = xhci->event_ring->dequeue;
320 /* Check if the xHC generated the interrupt, or the irq is shared */
321 temp = xhci_readl(xhci, &xhci->op_regs->status);
322 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
323 if (temp == 0xffffffff && temp2 == 0xffffffff)
326 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
327 spin_unlock(&xhci->lock);
330 xhci_dbg(xhci, "op reg status = %08x\n", temp);
331 xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2);
332 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
333 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
334 (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
335 lower_32_bits(trb->link.segment_ptr),
336 upper_32_bits(trb->link.segment_ptr),
337 (unsigned int) trb->link.intr_target,
338 (unsigned int) trb->link.control);
340 if (temp & STS_FATAL) {
341 xhci_warn(xhci, "WARNING: Host System Error\n");
344 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
345 spin_unlock(&xhci->lock);
350 spin_unlock(&xhci->lock);
355 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
356 void xhci_event_ring_work(unsigned long arg)
361 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
364 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
366 spin_lock_irqsave(&xhci->lock, flags);
367 temp = xhci_readl(xhci, &xhci->op_regs->status);
368 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
369 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
370 xhci_dbg(xhci, "HW died, polling stopped.\n");
371 spin_unlock_irqrestore(&xhci->lock, flags);
375 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
376 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
377 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
378 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
379 xhci->error_bitmask = 0;
380 xhci_dbg(xhci, "Event ring:\n");
381 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
382 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
383 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
384 temp_64 &= ~ERST_PTR_MASK;
385 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
386 xhci_dbg(xhci, "Command ring:\n");
387 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
388 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
389 xhci_dbg_cmd_ptrs(xhci);
390 for (i = 0; i < MAX_HC_SLOTS; ++i) {
393 for (j = 0; j < 31; ++j) {
394 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
398 if (xhci->noops_submitted != NUM_TEST_NOOPS)
399 if (xhci_setup_one_noop(xhci))
400 xhci_ring_cmd_db(xhci);
401 spin_unlock_irqrestore(&xhci->lock, flags);
404 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
406 xhci_dbg(xhci, "Quit polling the event ring.\n");
411 * Start the HC after it was halted.
413 * This function is called by the USB core when the HC driver is added.
414 * Its opposite is xhci_stop().
416 * xhci_init() must be called once before this function can be called.
417 * Reset the HC, enable device slot contexts, program DCBAAP, and
418 * set command ring pointer and event ring pointer.
420 * Setup MSI-X vectors and enable interrupts.
422 int xhci_run(struct usb_hcd *hcd)
426 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
427 void (*doorbell)(struct xhci_hcd *) = NULL;
429 hcd->uses_new_polling = 1;
432 xhci_dbg(xhci, "xhci_run\n");
433 #if 0 /* FIXME: MSI not setup yet */
434 /* Do this at the very last minute */
435 ret = xhci_setup_msix(xhci);
441 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
442 init_timer(&xhci->event_ring_timer);
443 xhci->event_ring_timer.data = (unsigned long) xhci;
444 xhci->event_ring_timer.function = xhci_event_ring_work;
445 /* Poll the event ring */
446 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
448 xhci_dbg(xhci, "Setting event ring polling timer\n");
449 add_timer(&xhci->event_ring_timer);
452 xhci_dbg(xhci, "Command ring memory map follows:\n");
453 xhci_debug_ring(xhci, xhci->cmd_ring);
454 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
455 xhci_dbg_cmd_ptrs(xhci);
457 xhci_dbg(xhci, "ERST memory map follows:\n");
458 xhci_dbg_erst(xhci, &xhci->erst);
459 xhci_dbg(xhci, "Event ring:\n");
460 xhci_debug_ring(xhci, xhci->event_ring);
461 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
462 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
463 temp_64 &= ~ERST_PTR_MASK;
464 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
466 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
467 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
468 temp &= ~ER_IRQ_INTERVAL_MASK;
470 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
472 /* Set the HCD state before we enable the irqs */
473 hcd->state = HC_STATE_RUNNING;
474 temp = xhci_readl(xhci, &xhci->op_regs->command);
476 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
478 xhci_writel(xhci, temp, &xhci->op_regs->command);
480 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
481 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
482 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
483 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
484 &xhci->ir_set->irq_pending);
485 xhci_print_ir_set(xhci, xhci->ir_set, 0);
487 if (NUM_TEST_NOOPS > 0)
488 doorbell = xhci_setup_one_noop(xhci);
489 if (xhci->quirks & XHCI_NEC_HOST)
490 xhci_queue_vendor_command(xhci, 0, 0, 0,
491 TRB_TYPE(TRB_NEC_GET_FW));
493 if (xhci_start(xhci)) {
500 if (xhci->quirks & XHCI_NEC_HOST)
501 xhci_ring_cmd_db(xhci);
503 xhci_dbg(xhci, "Finished xhci_run\n");
510 * This function is called by the USB core when the HC driver is removed.
511 * Its opposite is xhci_run().
513 * Disable device contexts, disable IRQs, and quiesce the HC.
514 * Reset the HC, finish any completed transactions, and cleanup memory.
516 void xhci_stop(struct usb_hcd *hcd)
519 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
521 spin_lock_irq(&xhci->lock);
524 spin_unlock_irq(&xhci->lock);
526 #if 0 /* No MSI yet */
527 xhci_cleanup_msix(xhci);
529 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
530 /* Tell the event ring poll function not to reschedule */
532 del_timer_sync(&xhci->event_ring_timer);
535 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
536 temp = xhci_readl(xhci, &xhci->op_regs->status);
537 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
538 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
539 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
540 &xhci->ir_set->irq_pending);
541 xhci_print_ir_set(xhci, xhci->ir_set, 0);
543 xhci_dbg(xhci, "cleaning up memory\n");
544 xhci_mem_cleanup(xhci);
545 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
546 xhci_readl(xhci, &xhci->op_regs->status));
550 * Shutdown HC (not bus-specific)
552 * This is called when the machine is rebooting or halting. We assume that the
553 * machine will be powered off, and the HC's internal state will be reset.
554 * Don't bother to free memory.
556 void xhci_shutdown(struct usb_hcd *hcd)
558 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
560 spin_lock_irq(&xhci->lock);
562 spin_unlock_irq(&xhci->lock);
565 xhci_cleanup_msix(xhci);
568 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
569 xhci_readl(xhci, &xhci->op_regs->status));
572 /*-------------------------------------------------------------------------*/
575 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
576 * HCDs. Find the index for an endpoint given its descriptor. Use the return
577 * value to right shift 1 for the bitmask.
579 * Index = (epnum * 2) + direction - 1,
580 * where direction = 0 for OUT, 1 for IN.
581 * For control endpoints, the IN index is used (OUT index is unused), so
582 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
584 unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
587 if (usb_endpoint_xfer_control(desc))
588 index = (unsigned int) (usb_endpoint_num(desc)*2);
590 index = (unsigned int) (usb_endpoint_num(desc)*2) +
591 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
595 /* Find the flag for this endpoint (for use in the control context). Use the
596 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
599 unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
601 return 1 << (xhci_get_endpoint_index(desc) + 1);
604 /* Find the flag for this endpoint (for use in the control context). Use the
605 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
608 unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
610 return 1 << (ep_index + 1);
613 /* Compute the last valid endpoint context index. Basically, this is the
614 * endpoint index plus one. For slot contexts with more than valid endpoint,
615 * we find the most significant bit set in the added contexts flags.
616 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
617 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
619 unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
621 return fls(added_ctxs) - 1;
624 /* Returns 1 if the arguments are OK;
625 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
627 int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
628 struct usb_host_endpoint *ep, int check_ep, const char *func) {
629 if (!hcd || (check_ep && !ep) || !udev) {
630 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
635 printk(KERN_DEBUG "xHCI %s called for root hub\n",
639 if (!udev->slot_id) {
640 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
647 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
648 struct usb_device *udev, struct xhci_command *command,
649 bool ctx_change, bool must_succeed);
652 * Full speed devices may have a max packet size greater than 8 bytes, but the
653 * USB core doesn't know that until it reads the first 8 bytes of the
654 * descriptor. If the usb_device's max packet size changes after that point,
655 * we need to issue an evaluate context command and wait on it.
657 static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
658 unsigned int ep_index, struct urb *urb)
660 struct xhci_container_ctx *in_ctx;
661 struct xhci_container_ctx *out_ctx;
662 struct xhci_input_control_ctx *ctrl_ctx;
663 struct xhci_ep_ctx *ep_ctx;
665 int hw_max_packet_size;
668 out_ctx = xhci->devs[slot_id]->out_ctx;
669 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
670 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
671 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
672 if (hw_max_packet_size != max_packet_size) {
673 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
674 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
676 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
678 xhci_dbg(xhci, "Issuing evaluate context command.\n");
680 /* Set up the modified control endpoint 0 */
681 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
682 xhci->devs[slot_id]->out_ctx, ep_index);
683 in_ctx = xhci->devs[slot_id]->in_ctx;
684 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
685 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
686 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
688 /* Set up the input context flags for the command */
689 /* FIXME: This won't work if a non-default control endpoint
690 * changes max packet sizes.
692 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
693 ctrl_ctx->add_flags = EP0_FLAG;
694 ctrl_ctx->drop_flags = 0;
696 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
697 xhci_dbg_ctx(xhci, in_ctx, ep_index);
698 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
699 xhci_dbg_ctx(xhci, out_ctx, ep_index);
701 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
704 /* Clean up the input context for later use by bandwidth
707 ctrl_ctx->add_flags = SLOT_FLAG;
713 * non-error returns are a promise to giveback() the urb later
714 * we drop ownership so next owner (or urb unlink) can get it
716 int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
718 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
721 unsigned int slot_id, ep_index;
724 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
727 slot_id = urb->dev->slot_id;
728 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
730 if (!xhci->devs || !xhci->devs[slot_id]) {
732 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
736 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
738 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
742 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
743 /* Check to see if the max packet size for the default control
744 * endpoint changed during FS device enumeration
746 if (urb->dev->speed == USB_SPEED_FULL) {
747 ret = xhci_check_maxpacket(xhci, slot_id,
753 /* We have a spinlock and interrupts disabled, so we must pass
754 * atomic context to this function, which may allocate memory.
756 spin_lock_irqsave(&xhci->lock, flags);
757 if (xhci->xhc_state & XHCI_STATE_DYING)
759 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
761 spin_unlock_irqrestore(&xhci->lock, flags);
762 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
763 spin_lock_irqsave(&xhci->lock, flags);
764 if (xhci->xhc_state & XHCI_STATE_DYING)
766 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
767 EP_GETTING_STREAMS) {
768 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
769 "is transitioning to using streams.\n");
771 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
772 EP_GETTING_NO_STREAMS) {
773 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
774 "is transitioning to "
775 "not having streams.\n");
778 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
781 spin_unlock_irqrestore(&xhci->lock, flags);
782 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
783 spin_lock_irqsave(&xhci->lock, flags);
784 if (xhci->xhc_state & XHCI_STATE_DYING)
786 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
788 spin_unlock_irqrestore(&xhci->lock, flags);
795 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
796 "non-responsive xHCI host.\n",
797 urb->ep->desc.bEndpointAddress, urb);
798 spin_unlock_irqrestore(&xhci->lock, flags);
803 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
804 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
805 * should pick up where it left off in the TD, unless a Set Transfer Ring
806 * Dequeue Pointer is issued.
808 * The TRBs that make up the buffers for the canceled URB will be "removed" from
809 * the ring. Since the ring is a contiguous structure, they can't be physically
810 * removed. Instead, there are two options:
812 * 1) If the HC is in the middle of processing the URB to be canceled, we
813 * simply move the ring's dequeue pointer past those TRBs using the Set
814 * Transfer Ring Dequeue Pointer command. This will be the common case,
815 * when drivers timeout on the last submitted URB and attempt to cancel.
817 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
818 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
819 * HC will need to invalidate the any TRBs it has cached after the stop
820 * endpoint command, as noted in the xHCI 0.95 errata.
822 * 3) The TD may have completed by the time the Stop Endpoint Command
823 * completes, so software needs to handle that case too.
825 * This function should protect against the TD enqueueing code ringing the
826 * doorbell while this code is waiting for a Stop Endpoint command to complete.
827 * It also needs to account for multiple cancellations on happening at the same
828 * time for the same endpoint.
830 * Note that this function can be called in any context, or so says
831 * usb_hcd_unlink_urb()
833 int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
838 struct xhci_hcd *xhci;
840 unsigned int ep_index;
841 struct xhci_ring *ep_ring;
842 struct xhci_virt_ep *ep;
844 xhci = hcd_to_xhci(hcd);
845 spin_lock_irqsave(&xhci->lock, flags);
846 /* Make sure the URB hasn't completed or been unlinked already */
847 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
848 if (ret || !urb->hcpriv)
850 temp = xhci_readl(xhci, &xhci->op_regs->status);
851 if (temp == 0xffffffff) {
852 xhci_dbg(xhci, "HW died, freeing TD.\n");
853 td = (struct xhci_td *) urb->hcpriv;
855 usb_hcd_unlink_urb_from_ep(hcd, urb);
856 spin_unlock_irqrestore(&xhci->lock, flags);
857 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN);
861 if (xhci->xhc_state & XHCI_STATE_DYING) {
862 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
863 "non-responsive xHCI host.\n",
864 urb->ep->desc.bEndpointAddress, urb);
865 /* Let the stop endpoint command watchdog timer (which set this
866 * state) finish cleaning up the endpoint TD lists. We must
867 * have caught it in the middle of dropping a lock and giving
873 xhci_dbg(xhci, "Cancel URB %p\n", urb);
874 xhci_dbg(xhci, "Event ring:\n");
875 xhci_debug_ring(xhci, xhci->event_ring);
876 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
877 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
878 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
884 xhci_dbg(xhci, "Endpoint ring:\n");
885 xhci_debug_ring(xhci, ep_ring);
886 td = (struct xhci_td *) urb->hcpriv;
888 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
889 /* Queue a stop endpoint command, but only if this is
890 * the first cancellation to be handled.
892 if (!(ep->ep_state & EP_HALT_PENDING)) {
893 ep->ep_state |= EP_HALT_PENDING;
894 ep->stop_cmds_pending++;
895 ep->stop_cmd_timer.expires = jiffies +
896 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
897 add_timer(&ep->stop_cmd_timer);
898 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
899 xhci_ring_cmd_db(xhci);
902 spin_unlock_irqrestore(&xhci->lock, flags);
906 /* Drop an endpoint from a new bandwidth configuration for this device.
907 * Only one call to this function is allowed per endpoint before
908 * check_bandwidth() or reset_bandwidth() must be called.
909 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
910 * add the endpoint to the schedule with possibly new parameters denoted by a
911 * different endpoint descriptor in usb_host_endpoint.
912 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
915 * The USB core will not allow URBs to be queued to an endpoint that is being
916 * disabled, so there's no need for mutual exclusion to protect
917 * the xhci->devs[slot_id] structure.
919 int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
920 struct usb_host_endpoint *ep)
922 struct xhci_hcd *xhci;
923 struct xhci_container_ctx *in_ctx, *out_ctx;
924 struct xhci_input_control_ctx *ctrl_ctx;
925 struct xhci_slot_ctx *slot_ctx;
926 unsigned int last_ctx;
927 unsigned int ep_index;
928 struct xhci_ep_ctx *ep_ctx;
930 u32 new_add_flags, new_drop_flags, new_slot_info;
933 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
936 xhci = hcd_to_xhci(hcd);
937 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
939 drop_flag = xhci_get_endpoint_flag(&ep->desc);
940 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
941 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
942 __func__, drop_flag);
946 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
947 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
952 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
953 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
954 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
955 ep_index = xhci_get_endpoint_index(&ep->desc);
956 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
957 /* If the HC already knows the endpoint is disabled,
958 * or the HCD has noted it is disabled, ignore this request
960 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
961 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
962 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
967 ctrl_ctx->drop_flags |= drop_flag;
968 new_drop_flags = ctrl_ctx->drop_flags;
970 ctrl_ctx->add_flags &= ~drop_flag;
971 new_add_flags = ctrl_ctx->add_flags;
973 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
974 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
975 /* Update the last valid endpoint context, if we deleted the last one */
976 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
977 slot_ctx->dev_info &= ~LAST_CTX_MASK;
978 slot_ctx->dev_info |= LAST_CTX(last_ctx);
980 new_slot_info = slot_ctx->dev_info;
982 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
984 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
985 (unsigned int) ep->desc.bEndpointAddress,
987 (unsigned int) new_drop_flags,
988 (unsigned int) new_add_flags,
989 (unsigned int) new_slot_info);
993 /* Add an endpoint to a new possible bandwidth configuration for this device.
994 * Only one call to this function is allowed per endpoint before
995 * check_bandwidth() or reset_bandwidth() must be called.
996 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
997 * add the endpoint to the schedule with possibly new parameters denoted by a
998 * different endpoint descriptor in usb_host_endpoint.
999 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1002 * The USB core will not allow URBs to be queued to an endpoint until the
1003 * configuration or alt setting is installed in the device, so there's no need
1004 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1006 int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1007 struct usb_host_endpoint *ep)
1009 struct xhci_hcd *xhci;
1010 struct xhci_container_ctx *in_ctx, *out_ctx;
1011 unsigned int ep_index;
1012 struct xhci_ep_ctx *ep_ctx;
1013 struct xhci_slot_ctx *slot_ctx;
1014 struct xhci_input_control_ctx *ctrl_ctx;
1016 unsigned int last_ctx;
1017 u32 new_add_flags, new_drop_flags, new_slot_info;
1020 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
1022 /* So we won't queue a reset ep command for a root hub */
1026 xhci = hcd_to_xhci(hcd);
1028 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1029 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1030 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1031 /* FIXME when we have to issue an evaluate endpoint command to
1032 * deal with ep0 max packet size changing once we get the
1035 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1036 __func__, added_ctxs);
1040 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1041 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1046 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
1047 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1048 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1049 ep_index = xhci_get_endpoint_index(&ep->desc);
1050 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1051 /* If the HCD has already noted the endpoint is enabled,
1052 * ignore this request.
1054 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
1055 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1061 * Configuration and alternate setting changes must be done in
1062 * process context, not interrupt context (or so documenation
1063 * for usb_set_interface() and usb_set_configuration() claim).
1065 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
1066 udev, ep, GFP_NOIO) < 0) {
1067 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1068 __func__, ep->desc.bEndpointAddress);
1072 ctrl_ctx->add_flags |= added_ctxs;
1073 new_add_flags = ctrl_ctx->add_flags;
1075 /* If xhci_endpoint_disable() was called for this endpoint, but the
1076 * xHC hasn't been notified yet through the check_bandwidth() call,
1077 * this re-adds a new state for the endpoint from the new endpoint
1078 * descriptors. We must drop and re-add this endpoint, so we leave the
1081 new_drop_flags = ctrl_ctx->drop_flags;
1083 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1084 /* Update the last valid endpoint context, if we just added one past */
1085 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
1086 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1087 slot_ctx->dev_info |= LAST_CTX(last_ctx);
1089 new_slot_info = slot_ctx->dev_info;
1091 /* Store the usb_device pointer for later use */
1094 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1095 (unsigned int) ep->desc.bEndpointAddress,
1097 (unsigned int) new_drop_flags,
1098 (unsigned int) new_add_flags,
1099 (unsigned int) new_slot_info);
1103 static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
1105 struct xhci_input_control_ctx *ctrl_ctx;
1106 struct xhci_ep_ctx *ep_ctx;
1107 struct xhci_slot_ctx *slot_ctx;
1110 /* When a device's add flag and drop flag are zero, any subsequent
1111 * configure endpoint command will leave that endpoint's state
1112 * untouched. Make sure we don't leave any old state in the input
1113 * endpoint contexts.
1115 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1116 ctrl_ctx->drop_flags = 0;
1117 ctrl_ctx->add_flags = 0;
1118 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1119 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1120 /* Endpoint 0 is always valid */
1121 slot_ctx->dev_info |= LAST_CTX(1);
1122 for (i = 1; i < 31; ++i) {
1123 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
1124 ep_ctx->ep_info = 0;
1125 ep_ctx->ep_info2 = 0;
1127 ep_ctx->tx_info = 0;
1131 static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
1132 struct usb_device *udev, int *cmd_status)
1136 switch (*cmd_status) {
1138 dev_warn(&udev->dev, "Not enough host controller resources "
1139 "for new device state.\n");
1141 /* FIXME: can we allocate more resources for the HC? */
1144 dev_warn(&udev->dev, "Not enough bandwidth "
1145 "for new device state.\n");
1147 /* FIXME: can we go back to the old state? */
1150 /* the HCD set up something wrong */
1151 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1153 "and endpoint is not disabled.\n");
1157 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1161 xhci_err(xhci, "ERROR: unexpected command completion "
1162 "code 0x%x.\n", *cmd_status);
1169 static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
1170 struct usb_device *udev, int *cmd_status)
1173 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
1175 switch (*cmd_status) {
1177 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1178 "context command.\n");
1182 dev_warn(&udev->dev, "WARN: slot not enabled for"
1183 "evaluate context command.\n");
1184 case COMP_CTX_STATE:
1185 dev_warn(&udev->dev, "WARN: invalid context state for "
1186 "evaluate context command.\n");
1187 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1191 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1195 xhci_err(xhci, "ERROR: unexpected command completion "
1196 "code 0x%x.\n", *cmd_status);
1203 /* Issue a configure endpoint command or evaluate context command
1204 * and wait for it to finish.
1206 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1207 struct usb_device *udev,
1208 struct xhci_command *command,
1209 bool ctx_change, bool must_succeed)
1213 unsigned long flags;
1214 struct xhci_container_ctx *in_ctx;
1215 struct completion *cmd_completion;
1217 struct xhci_virt_device *virt_dev;
1219 spin_lock_irqsave(&xhci->lock, flags);
1220 virt_dev = xhci->devs[udev->slot_id];
1222 in_ctx = command->in_ctx;
1223 cmd_completion = command->completion;
1224 cmd_status = &command->status;
1225 command->command_trb = xhci->cmd_ring->enqueue;
1226 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
1228 in_ctx = virt_dev->in_ctx;
1229 cmd_completion = &virt_dev->cmd_completion;
1230 cmd_status = &virt_dev->cmd_status;
1232 init_completion(cmd_completion);
1235 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
1236 udev->slot_id, must_succeed);
1238 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
1242 list_del(&command->cmd_list);
1243 spin_unlock_irqrestore(&xhci->lock, flags);
1244 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
1247 xhci_ring_cmd_db(xhci);
1248 spin_unlock_irqrestore(&xhci->lock, flags);
1250 /* Wait for the configure endpoint command to complete */
1251 timeleft = wait_for_completion_interruptible_timeout(
1253 USB_CTRL_SET_TIMEOUT);
1254 if (timeleft <= 0) {
1255 xhci_warn(xhci, "%s while waiting for %s command\n",
1256 timeleft == 0 ? "Timeout" : "Signal",
1258 "configure endpoint" :
1259 "evaluate context");
1260 /* FIXME cancel the configure endpoint command */
1265 return xhci_configure_endpoint_result(xhci, udev, cmd_status);
1266 return xhci_evaluate_context_result(xhci, udev, cmd_status);
1269 /* Called after one or more calls to xhci_add_endpoint() or
1270 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1271 * to call xhci_reset_bandwidth().
1273 * Since we are in the middle of changing either configuration or
1274 * installing a new alt setting, the USB core won't allow URBs to be
1275 * enqueued for any endpoint on the old config or interface. Nothing
1276 * else should be touching the xhci->devs[slot_id] structure, so we
1277 * don't need to take the xhci->lock for manipulating that.
1279 int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1283 struct xhci_hcd *xhci;
1284 struct xhci_virt_device *virt_dev;
1285 struct xhci_input_control_ctx *ctrl_ctx;
1286 struct xhci_slot_ctx *slot_ctx;
1288 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1291 xhci = hcd_to_xhci(hcd);
1293 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
1294 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1298 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1299 virt_dev = xhci->devs[udev->slot_id];
1301 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
1302 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1303 ctrl_ctx->add_flags |= SLOT_FLAG;
1304 ctrl_ctx->add_flags &= ~EP0_FLAG;
1305 ctrl_ctx->drop_flags &= ~SLOT_FLAG;
1306 ctrl_ctx->drop_flags &= ~EP0_FLAG;
1307 xhci_dbg(xhci, "New Input Control Context:\n");
1308 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1309 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
1310 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
1312 ret = xhci_configure_endpoint(xhci, udev, NULL,
1315 /* Callee should call reset_bandwidth() */
1319 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
1320 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
1321 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
1323 xhci_zero_in_ctx(xhci, virt_dev);
1324 /* Install new rings and free or cache any old rings */
1325 for (i = 1; i < 31; ++i) {
1326 if (!virt_dev->eps[i].new_ring)
1328 /* Only cache or free the old ring if it exists.
1329 * It may not if this is the first add of an endpoint.
1331 if (virt_dev->eps[i].ring) {
1332 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
1334 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
1335 virt_dev->eps[i].new_ring = NULL;
1341 void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1343 struct xhci_hcd *xhci;
1344 struct xhci_virt_device *virt_dev;
1347 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1350 xhci = hcd_to_xhci(hcd);
1352 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1353 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1357 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1358 virt_dev = xhci->devs[udev->slot_id];
1359 /* Free any rings allocated for added endpoints */
1360 for (i = 0; i < 31; ++i) {
1361 if (virt_dev->eps[i].new_ring) {
1362 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
1363 virt_dev->eps[i].new_ring = NULL;
1366 xhci_zero_in_ctx(xhci, virt_dev);
1369 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
1370 struct xhci_container_ctx *in_ctx,
1371 struct xhci_container_ctx *out_ctx,
1372 u32 add_flags, u32 drop_flags)
1374 struct xhci_input_control_ctx *ctrl_ctx;
1375 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1376 ctrl_ctx->add_flags = add_flags;
1377 ctrl_ctx->drop_flags = drop_flags;
1378 xhci_slot_copy(xhci, in_ctx, out_ctx);
1379 ctrl_ctx->add_flags |= SLOT_FLAG;
1381 xhci_dbg(xhci, "Input Context:\n");
1382 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
1385 void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
1386 unsigned int slot_id, unsigned int ep_index,
1387 struct xhci_dequeue_state *deq_state)
1389 struct xhci_container_ctx *in_ctx;
1390 struct xhci_ep_ctx *ep_ctx;
1394 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1395 xhci->devs[slot_id]->out_ctx, ep_index);
1396 in_ctx = xhci->devs[slot_id]->in_ctx;
1397 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1398 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
1399 deq_state->new_deq_ptr);
1401 xhci_warn(xhci, "WARN Cannot submit config ep after "
1402 "reset ep command\n");
1403 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
1404 deq_state->new_deq_seg,
1405 deq_state->new_deq_ptr);
1408 ep_ctx->deq = addr | deq_state->new_cycle_state;
1410 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
1411 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
1412 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
1415 void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
1416 struct usb_device *udev, unsigned int ep_index)
1418 struct xhci_dequeue_state deq_state;
1419 struct xhci_virt_ep *ep;
1421 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
1422 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1423 /* We need to move the HW's dequeue pointer past this TD,
1424 * or it will attempt to resend it on the next doorbell ring.
1426 xhci_find_new_dequeue_state(xhci, udev->slot_id,
1427 ep_index, ep->stopped_stream, ep->stopped_td,
1430 /* HW with the reset endpoint quirk will use the saved dequeue state to
1431 * issue a configure endpoint command later.
1433 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
1434 xhci_dbg(xhci, "Queueing new dequeue state\n");
1435 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
1436 ep_index, ep->stopped_stream, &deq_state);
1438 /* Better hope no one uses the input context between now and the
1439 * reset endpoint completion!
1440 * XXX: No idea how this hardware will react when stream rings
1443 xhci_dbg(xhci, "Setting up input context for "
1444 "configure endpoint command\n");
1445 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
1446 ep_index, &deq_state);
1450 /* Deal with stalled endpoints. The core should have sent the control message
1451 * to clear the halt condition. However, we need to make the xHCI hardware
1452 * reset its sequence number, since a device will expect a sequence number of
1453 * zero after the halt condition is cleared.
1454 * Context: in_interrupt
1456 void xhci_endpoint_reset(struct usb_hcd *hcd,
1457 struct usb_host_endpoint *ep)
1459 struct xhci_hcd *xhci;
1460 struct usb_device *udev;
1461 unsigned int ep_index;
1462 unsigned long flags;
1464 struct xhci_virt_ep *virt_ep;
1466 xhci = hcd_to_xhci(hcd);
1467 udev = (struct usb_device *) ep->hcpriv;
1468 /* Called with a root hub endpoint (or an endpoint that wasn't added
1469 * with xhci_add_endpoint()
1473 ep_index = xhci_get_endpoint_index(&ep->desc);
1474 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1475 if (!virt_ep->stopped_td) {
1476 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
1477 ep->desc.bEndpointAddress);
1480 if (usb_endpoint_xfer_control(&ep->desc)) {
1481 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
1485 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1486 spin_lock_irqsave(&xhci->lock, flags);
1487 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
1489 * Can't change the ring dequeue pointer until it's transitioned to the
1490 * stopped state, which is only upon a successful reset endpoint
1491 * command. Better hope that last command worked!
1494 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
1495 kfree(virt_ep->stopped_td);
1496 xhci_ring_cmd_db(xhci);
1498 virt_ep->stopped_td = NULL;
1499 virt_ep->stopped_trb = NULL;
1500 virt_ep->stopped_stream = 0;
1501 spin_unlock_irqrestore(&xhci->lock, flags);
1504 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1507 static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
1508 struct usb_device *udev, struct usb_host_endpoint *ep,
1509 unsigned int slot_id)
1512 unsigned int ep_index;
1513 unsigned int ep_state;
1517 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, __func__);
1520 if (ep->ss_ep_comp.bmAttributes == 0) {
1521 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
1522 " descriptor for ep 0x%x does not support streams\n",
1523 ep->desc.bEndpointAddress);
1527 ep_index = xhci_get_endpoint_index(&ep->desc);
1528 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1529 if (ep_state & EP_HAS_STREAMS ||
1530 ep_state & EP_GETTING_STREAMS) {
1531 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
1532 "already has streams set up.\n",
1533 ep->desc.bEndpointAddress);
1534 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
1535 "dynamic stream context array reallocation.\n");
1538 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
1539 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
1540 "endpoint 0x%x; URBs are pending.\n",
1541 ep->desc.bEndpointAddress);
1547 static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
1548 unsigned int *num_streams, unsigned int *num_stream_ctxs)
1550 unsigned int max_streams;
1552 /* The stream context array size must be a power of two */
1553 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
1555 * Find out how many primary stream array entries the host controller
1556 * supports. Later we may use secondary stream arrays (similar to 2nd
1557 * level page entries), but that's an optional feature for xHCI host
1558 * controllers. xHCs must support at least 4 stream IDs.
1560 max_streams = HCC_MAX_PSA(xhci->hcc_params);
1561 if (*num_stream_ctxs > max_streams) {
1562 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
1564 *num_stream_ctxs = max_streams;
1565 *num_streams = max_streams;
1569 /* Returns an error code if one of the endpoint already has streams.
1570 * This does not change any data structures, it only checks and gathers
1573 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
1574 struct usb_device *udev,
1575 struct usb_host_endpoint **eps, unsigned int num_eps,
1576 unsigned int *num_streams, u32 *changed_ep_bitmask)
1578 unsigned int max_streams;
1579 unsigned int endpoint_flag;
1583 for (i = 0; i < num_eps; i++) {
1584 ret = xhci_check_streams_endpoint(xhci, udev,
1585 eps[i], udev->slot_id);
1589 max_streams = USB_SS_MAX_STREAMS(
1590 eps[i]->ss_ep_comp.bmAttributes);
1591 if (max_streams < (*num_streams - 1)) {
1592 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
1593 eps[i]->desc.bEndpointAddress,
1595 *num_streams = max_streams+1;
1598 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
1599 if (*changed_ep_bitmask & endpoint_flag)
1601 *changed_ep_bitmask |= endpoint_flag;
1606 static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
1607 struct usb_device *udev,
1608 struct usb_host_endpoint **eps, unsigned int num_eps)
1610 u32 changed_ep_bitmask = 0;
1611 unsigned int slot_id;
1612 unsigned int ep_index;
1613 unsigned int ep_state;
1616 slot_id = udev->slot_id;
1617 if (!xhci->devs[slot_id])
1620 for (i = 0; i < num_eps; i++) {
1621 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1622 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1623 /* Are streams already being freed for the endpoint? */
1624 if (ep_state & EP_GETTING_NO_STREAMS) {
1625 xhci_warn(xhci, "WARN Can't disable streams for "
1627 "streams are being disabled already.",
1628 eps[i]->desc.bEndpointAddress);
1631 /* Are there actually any streams to free? */
1632 if (!(ep_state & EP_HAS_STREAMS) &&
1633 !(ep_state & EP_GETTING_STREAMS)) {
1634 xhci_warn(xhci, "WARN Can't disable streams for "
1636 "streams are already disabled!",
1637 eps[i]->desc.bEndpointAddress);
1638 xhci_warn(xhci, "WARN xhci_free_streams() called "
1639 "with non-streams endpoint\n");
1642 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
1644 return changed_ep_bitmask;
1648 * The USB device drivers use this function (though the HCD interface in USB
1649 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
1650 * coordinate mass storage command queueing across multiple endpoints (basically
1651 * a stream ID == a task ID).
1653 * Setting up streams involves allocating the same size stream context array
1654 * for each endpoint and issuing a configure endpoint command for all endpoints.
1656 * Don't allow the call to succeed if one endpoint only supports one stream
1657 * (which means it doesn't support streams at all).
1659 * Drivers may get less stream IDs than they asked for, if the host controller
1660 * hardware or endpoints claim they can't support the number of requested
1663 int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1664 struct usb_host_endpoint **eps, unsigned int num_eps,
1665 unsigned int num_streams, gfp_t mem_flags)
1668 struct xhci_hcd *xhci;
1669 struct xhci_virt_device *vdev;
1670 struct xhci_command *config_cmd;
1671 unsigned int ep_index;
1672 unsigned int num_stream_ctxs;
1673 unsigned long flags;
1674 u32 changed_ep_bitmask = 0;
1679 /* Add one to the number of streams requested to account for
1680 * stream 0 that is reserved for xHCI usage.
1683 xhci = hcd_to_xhci(hcd);
1684 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
1687 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
1689 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
1693 /* Check to make sure all endpoints are not already configured for
1694 * streams. While we're at it, find the maximum number of streams that
1695 * all the endpoints will support and check for duplicate endpoints.
1697 spin_lock_irqsave(&xhci->lock, flags);
1698 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
1699 num_eps, &num_streams, &changed_ep_bitmask);
1701 xhci_free_command(xhci, config_cmd);
1702 spin_unlock_irqrestore(&xhci->lock, flags);
1705 if (num_streams <= 1) {
1706 xhci_warn(xhci, "WARN: endpoints can't handle "
1707 "more than one stream.\n");
1708 xhci_free_command(xhci, config_cmd);
1709 spin_unlock_irqrestore(&xhci->lock, flags);
1712 vdev = xhci->devs[udev->slot_id];
1713 /* Mark each endpoint as being in transistion, so
1714 * xhci_urb_enqueue() will reject all URBs.
1716 for (i = 0; i < num_eps; i++) {
1717 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1718 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
1720 spin_unlock_irqrestore(&xhci->lock, flags);
1722 /* Setup internal data structures and allocate HW data structures for
1723 * streams (but don't install the HW structures in the input context
1724 * until we're sure all memory allocation succeeded).
1726 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
1727 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
1728 num_stream_ctxs, num_streams);
1730 for (i = 0; i < num_eps; i++) {
1731 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1732 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
1734 num_streams, mem_flags);
1735 if (!vdev->eps[ep_index].stream_info)
1737 /* Set maxPstreams in endpoint context and update deq ptr to
1738 * point to stream context array. FIXME
1742 /* Set up the input context for a configure endpoint command. */
1743 for (i = 0; i < num_eps; i++) {
1744 struct xhci_ep_ctx *ep_ctx;
1746 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1747 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
1749 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
1750 vdev->out_ctx, ep_index);
1751 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
1752 vdev->eps[ep_index].stream_info);
1754 /* Tell the HW to drop its old copy of the endpoint context info
1755 * and add the updated copy from the input context.
1757 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
1758 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
1760 /* Issue and wait for the configure endpoint command */
1761 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
1764 /* xHC rejected the configure endpoint command for some reason, so we
1765 * leave the old ring intact and free our internal streams data
1771 spin_lock_irqsave(&xhci->lock, flags);
1772 for (i = 0; i < num_eps; i++) {
1773 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1774 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
1775 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
1776 udev->slot_id, ep_index);
1777 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
1779 xhci_free_command(xhci, config_cmd);
1780 spin_unlock_irqrestore(&xhci->lock, flags);
1782 /* Subtract 1 for stream 0, which drivers can't use */
1783 return num_streams - 1;
1786 /* If it didn't work, free the streams! */
1787 for (i = 0; i < num_eps; i++) {
1788 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1789 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
1790 vdev->eps[ep_index].stream_info = NULL;
1791 /* FIXME Unset maxPstreams in endpoint context and
1792 * update deq ptr to point to normal string ring.
1794 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
1795 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
1796 xhci_endpoint_zero(xhci, vdev, eps[i]);
1798 xhci_free_command(xhci, config_cmd);
1802 /* Transition the endpoint from using streams to being a "normal" endpoint
1805 * Modify the endpoint context state, submit a configure endpoint command,
1806 * and free all endpoint rings for streams if that completes successfully.
1808 int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1809 struct usb_host_endpoint **eps, unsigned int num_eps,
1813 struct xhci_hcd *xhci;
1814 struct xhci_virt_device *vdev;
1815 struct xhci_command *command;
1816 unsigned int ep_index;
1817 unsigned long flags;
1818 u32 changed_ep_bitmask;
1820 xhci = hcd_to_xhci(hcd);
1821 vdev = xhci->devs[udev->slot_id];
1823 /* Set up a configure endpoint command to remove the streams rings */
1824 spin_lock_irqsave(&xhci->lock, flags);
1825 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
1826 udev, eps, num_eps);
1827 if (changed_ep_bitmask == 0) {
1828 spin_unlock_irqrestore(&xhci->lock, flags);
1832 /* Use the xhci_command structure from the first endpoint. We may have
1833 * allocated too many, but the driver may call xhci_free_streams() for
1834 * each endpoint it grouped into one call to xhci_alloc_streams().
1836 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
1837 command = vdev->eps[ep_index].stream_info->free_streams_command;
1838 for (i = 0; i < num_eps; i++) {
1839 struct xhci_ep_ctx *ep_ctx;
1841 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1842 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
1843 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
1844 EP_GETTING_NO_STREAMS;
1846 xhci_endpoint_copy(xhci, command->in_ctx,
1847 vdev->out_ctx, ep_index);
1848 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
1849 &vdev->eps[ep_index]);
1851 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
1852 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
1853 spin_unlock_irqrestore(&xhci->lock, flags);
1855 /* Issue and wait for the configure endpoint command,
1856 * which must succeed.
1858 ret = xhci_configure_endpoint(xhci, udev, command,
1861 /* xHC rejected the configure endpoint command for some reason, so we
1862 * leave the streams rings intact.
1867 spin_lock_irqsave(&xhci->lock, flags);
1868 for (i = 0; i < num_eps; i++) {
1869 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1870 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
1871 vdev->eps[ep_index].stream_info = NULL;
1872 /* FIXME Unset maxPstreams in endpoint context and
1873 * update deq ptr to point to normal string ring.
1875 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
1876 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
1878 spin_unlock_irqrestore(&xhci->lock, flags);
1884 * This submits a Reset Device Command, which will set the device state to 0,
1885 * set the device address to 0, and disable all the endpoints except the default
1886 * control endpoint. The USB core should come back and call
1887 * xhci_address_device(), and then re-set up the configuration. If this is
1888 * called because of a usb_reset_and_verify_device(), then the old alternate
1889 * settings will be re-installed through the normal bandwidth allocation
1892 * Wait for the Reset Device command to finish. Remove all structures
1893 * associated with the endpoints that were disabled. Clear the input device
1894 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
1896 int xhci_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
1899 unsigned long flags;
1900 struct xhci_hcd *xhci;
1901 unsigned int slot_id;
1902 struct xhci_virt_device *virt_dev;
1903 struct xhci_command *reset_device_cmd;
1905 int last_freed_endpoint;
1907 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1910 xhci = hcd_to_xhci(hcd);
1911 slot_id = udev->slot_id;
1912 virt_dev = xhci->devs[slot_id];
1914 xhci_dbg(xhci, "%s called with invalid slot ID %u\n",
1919 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
1920 /* Allocate the command structure that holds the struct completion.
1921 * Assume we're in process context, since the normal device reset
1922 * process has to wait for the device anyway. Storage devices are
1923 * reset as part of error handling, so use GFP_NOIO instead of
1926 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
1927 if (!reset_device_cmd) {
1928 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
1932 /* Attempt to submit the Reset Device command to the command ring */
1933 spin_lock_irqsave(&xhci->lock, flags);
1934 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
1935 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
1936 ret = xhci_queue_reset_device(xhci, slot_id);
1938 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1939 list_del(&reset_device_cmd->cmd_list);
1940 spin_unlock_irqrestore(&xhci->lock, flags);
1941 goto command_cleanup;
1943 xhci_ring_cmd_db(xhci);
1944 spin_unlock_irqrestore(&xhci->lock, flags);
1946 /* Wait for the Reset Device command to finish */
1947 timeleft = wait_for_completion_interruptible_timeout(
1948 reset_device_cmd->completion,
1949 USB_CTRL_SET_TIMEOUT);
1950 if (timeleft <= 0) {
1951 xhci_warn(xhci, "%s while waiting for reset device command\n",
1952 timeleft == 0 ? "Timeout" : "Signal");
1953 spin_lock_irqsave(&xhci->lock, flags);
1954 /* The timeout might have raced with the event ring handler, so
1955 * only delete from the list if the item isn't poisoned.
1957 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
1958 list_del(&reset_device_cmd->cmd_list);
1959 spin_unlock_irqrestore(&xhci->lock, flags);
1961 goto command_cleanup;
1964 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
1965 * unless we tried to reset a slot ID that wasn't enabled,
1966 * or the device wasn't in the addressed or configured state.
1968 ret = reset_device_cmd->status;
1970 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
1971 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
1972 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
1974 xhci_get_slot_state(xhci, virt_dev->out_ctx));
1975 xhci_info(xhci, "Not freeing device rings.\n");
1976 /* Don't treat this as an error. May change my mind later. */
1978 goto command_cleanup;
1980 xhci_dbg(xhci, "Successful reset device command.\n");
1983 if (xhci_is_vendor_info_code(xhci, ret))
1985 xhci_warn(xhci, "Unknown completion code %u for "
1986 "reset device command.\n", ret);
1988 goto command_cleanup;
1991 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
1992 last_freed_endpoint = 1;
1993 for (i = 1; i < 31; ++i) {
1994 if (!virt_dev->eps[i].ring)
1996 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
1997 last_freed_endpoint = i;
1999 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
2000 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
2004 xhci_free_command(xhci, reset_device_cmd);
2009 * At this point, the struct usb_device is about to go away, the device has
2010 * disconnected, and all traffic has been stopped and the endpoints have been
2011 * disabled. Free any HC data structures associated with that device.
2013 void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
2015 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2016 struct xhci_virt_device *virt_dev;
2017 unsigned long flags;
2021 if (udev->slot_id == 0)
2023 virt_dev = xhci->devs[udev->slot_id];
2027 /* Stop any wayward timer functions (which may grab the lock) */
2028 for (i = 0; i < 31; ++i) {
2029 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
2030 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
2033 spin_lock_irqsave(&xhci->lock, flags);
2034 /* Don't disable the slot if the host controller is dead. */
2035 state = xhci_readl(xhci, &xhci->op_regs->status);
2036 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
2037 xhci_free_virt_device(xhci, udev->slot_id);
2038 spin_unlock_irqrestore(&xhci->lock, flags);
2042 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
2043 spin_unlock_irqrestore(&xhci->lock, flags);
2044 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2047 xhci_ring_cmd_db(xhci);
2048 spin_unlock_irqrestore(&xhci->lock, flags);
2050 * Event command completion handler will free any data structures
2051 * associated with the slot. XXX Can free sleep?
2056 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
2057 * timed out, or allocating memory failed. Returns 1 on success.
2059 int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
2061 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2062 unsigned long flags;
2066 spin_lock_irqsave(&xhci->lock, flags);
2067 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
2069 spin_unlock_irqrestore(&xhci->lock, flags);
2070 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2073 xhci_ring_cmd_db(xhci);
2074 spin_unlock_irqrestore(&xhci->lock, flags);
2076 /* XXX: how much time for xHC slot assignment? */
2077 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2078 USB_CTRL_SET_TIMEOUT);
2079 if (timeleft <= 0) {
2080 xhci_warn(xhci, "%s while waiting for a slot\n",
2081 timeleft == 0 ? "Timeout" : "Signal");
2082 /* FIXME cancel the enable slot request */
2086 if (!xhci->slot_id) {
2087 xhci_err(xhci, "Error while assigning device slot ID\n");
2090 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
2091 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
2092 /* Disable slot, if we can do it without mem alloc */
2093 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
2094 spin_lock_irqsave(&xhci->lock, flags);
2095 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
2096 xhci_ring_cmd_db(xhci);
2097 spin_unlock_irqrestore(&xhci->lock, flags);
2100 udev->slot_id = xhci->slot_id;
2101 /* Is this a LS or FS device under a HS hub? */
2102 /* Hub or peripherial? */
2107 * Issue an Address Device command (which will issue a SetAddress request to
2109 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
2110 * we should only issue and wait on one address command at the same time.
2112 * We add one to the device address issued by the hardware because the USB core
2113 * uses address 1 for the root hubs (even though they're not really devices).
2115 int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2117 unsigned long flags;
2119 struct xhci_virt_device *virt_dev;
2121 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2122 struct xhci_slot_ctx *slot_ctx;
2123 struct xhci_input_control_ctx *ctrl_ctx;
2126 if (!udev->slot_id) {
2127 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
2131 virt_dev = xhci->devs[udev->slot_id];
2133 /* If this is a Set Address to an unconfigured device, setup ep 0 */
2135 xhci_setup_addressable_virt_dev(xhci, udev);
2137 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
2138 /* Otherwise, assume the core has the device configured how it wants */
2139 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
2140 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
2142 spin_lock_irqsave(&xhci->lock, flags);
2143 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
2146 spin_unlock_irqrestore(&xhci->lock, flags);
2147 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2150 xhci_ring_cmd_db(xhci);
2151 spin_unlock_irqrestore(&xhci->lock, flags);
2153 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
2154 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2155 USB_CTRL_SET_TIMEOUT);
2156 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
2157 * the SetAddress() "recovery interval" required by USB and aborting the
2158 * command on a timeout.
2160 if (timeleft <= 0) {
2161 xhci_warn(xhci, "%s while waiting for a slot\n",
2162 timeleft == 0 ? "Timeout" : "Signal");
2163 /* FIXME cancel the address device command */
2167 switch (virt_dev->cmd_status) {
2168 case COMP_CTX_STATE:
2170 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
2175 dev_warn(&udev->dev, "Device not responding to set address.\n");
2179 xhci_dbg(xhci, "Successful Address Device command\n");
2182 xhci_err(xhci, "ERROR: unexpected command completion "
2183 "code 0x%x.\n", virt_dev->cmd_status);
2184 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
2185 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
2192 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
2193 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
2194 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
2196 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
2197 (unsigned long long)
2198 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
2199 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
2200 (unsigned long long)virt_dev->out_ctx->dma);
2201 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
2202 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
2203 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
2204 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
2206 * USB core uses address 1 for the roothubs, so we add one to the
2207 * address given back to us by the HC.
2209 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
2210 udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
2211 /* Zero the input context control for later use */
2212 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2213 ctrl_ctx->add_flags = 0;
2214 ctrl_ctx->drop_flags = 0;
2216 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
2217 /* XXX Meh, not sure if anyone else but choose_address uses this. */
2218 set_bit(udev->devnum, udev->bus->devmap.devicemap);
2223 /* Once a hub descriptor is fetched for a device, we need to update the xHC's
2224 * internal data structures for the device.
2226 int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
2227 struct usb_tt *tt, gfp_t mem_flags)
2229 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2230 struct xhci_virt_device *vdev;
2231 struct xhci_command *config_cmd;
2232 struct xhci_input_control_ctx *ctrl_ctx;
2233 struct xhci_slot_ctx *slot_ctx;
2234 unsigned long flags;
2235 unsigned think_time;
2238 /* Ignore root hubs */
2242 vdev = xhci->devs[hdev->slot_id];
2244 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
2247 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
2249 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
2253 spin_lock_irqsave(&xhci->lock, flags);
2254 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
2255 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
2256 ctrl_ctx->add_flags |= SLOT_FLAG;
2257 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
2258 slot_ctx->dev_info |= DEV_HUB;
2260 slot_ctx->dev_info |= DEV_MTT;
2261 if (xhci->hci_version > 0x95) {
2262 xhci_dbg(xhci, "xHCI version %x needs hub "
2263 "TT think time and number of ports\n",
2264 (unsigned int) xhci->hci_version);
2265 slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
2266 /* Set TT think time - convert from ns to FS bit times.
2267 * 0 = 8 FS bit times, 1 = 16 FS bit times,
2268 * 2 = 24 FS bit times, 3 = 32 FS bit times.
2270 think_time = tt->think_time;
2271 if (think_time != 0)
2272 think_time = (think_time / 666) - 1;
2273 slot_ctx->tt_info |= TT_THINK_TIME(think_time);
2275 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
2276 "TT think time or number of ports\n",
2277 (unsigned int) xhci->hci_version);
2279 slot_ctx->dev_state = 0;
2280 spin_unlock_irqrestore(&xhci->lock, flags);
2282 xhci_dbg(xhci, "Set up %s for hub device.\n",
2283 (xhci->hci_version > 0x95) ?
2284 "configure endpoint" : "evaluate context");
2285 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
2286 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
2288 /* Issue and wait for the configure endpoint or
2289 * evaluate context command.
2291 if (xhci->hci_version > 0x95)
2292 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2295 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2298 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
2299 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
2301 xhci_free_command(xhci, config_cmd);
2305 int xhci_get_frame(struct usb_hcd *hcd)
2307 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2308 /* EHCI mods by the periodic size. Why? */
2309 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
2312 MODULE_DESCRIPTION(DRIVER_DESC);
2313 MODULE_AUTHOR(DRIVER_AUTHOR);
2314 MODULE_LICENSE("GPL");
2316 static int __init xhci_hcd_init(void)
2321 retval = xhci_register_pci();
2324 printk(KERN_DEBUG "Problem registering PCI driver.");
2329 * Check the compiler generated sizes of structures that must be laid
2330 * out in specific ways for hardware access.
2332 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
2333 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
2334 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
2335 /* xhci_device_control has eight fields, and also
2336 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
2338 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
2339 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
2340 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
2341 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
2342 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
2343 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
2344 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
2345 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
2348 module_init(xhci_hcd_init);
2350 static void __exit xhci_hcd_cleanup(void)
2353 xhci_unregister_pci();
2356 module_exit(xhci_hcd_cleanup);