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/pci.h>
24 #include <linux/irq.h>
25 #include <linux/log2.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/slab.h>
32 #define DRIVER_AUTHOR "Sarah Sharp"
33 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
35 /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
36 static int link_quirk;
37 module_param(link_quirk, int, S_IRUGO | S_IWUSR);
38 MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
40 /* TODO: copied from ehci-hcd.c - can this be refactored? */
42 * handshake - spin reading hc until handshake completes or fails
43 * @ptr: address of hc register to be read
44 * @mask: bits to look at in result of read
45 * @done: value of those bits when handshake succeeds
46 * @usec: timeout in microseconds
48 * Returns negative errno, or zero on success
50 * Success happens when the "mask" bits have the specified value (hardware
51 * handshake done). There are two failure modes: "usec" have passed (major
52 * hardware flakeout), or the register reads as all-ones (hardware removed).
54 static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
55 u32 mask, u32 done, int usec)
60 result = xhci_readl(xhci, ptr);
61 if (result == ~(u32)0) /* card removed */
73 * Disable interrupts and begin the xHCI halting process.
75 void xhci_quiesce(struct xhci_hcd *xhci)
82 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
86 cmd = xhci_readl(xhci, &xhci->op_regs->command);
88 xhci_writel(xhci, cmd, &xhci->op_regs->command);
92 * Force HC into halt state.
94 * Disable any IRQs and clear the run/stop bit.
95 * HC will complete any current and actively pipelined transactions, and
96 * should halt within 16 microframes of the run/stop bit being cleared.
97 * Read HC Halted bit in the status register to see when the HC is finished.
98 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
100 int xhci_halt(struct xhci_hcd *xhci)
102 xhci_dbg(xhci, "// Halt the HC\n");
105 return handshake(xhci, &xhci->op_regs->status,
106 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
110 * Set the run bit and wait for the host to be running.
112 int xhci_start(struct xhci_hcd *xhci)
117 temp = xhci_readl(xhci, &xhci->op_regs->command);
119 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
121 xhci_writel(xhci, temp, &xhci->op_regs->command);
124 * Wait for the HCHalted Status bit to be 0 to indicate the host is
127 ret = handshake(xhci, &xhci->op_regs->status,
128 STS_HALT, 0, XHCI_MAX_HALT_USEC);
129 if (ret == -ETIMEDOUT)
130 xhci_err(xhci, "Host took too long to start, "
131 "waited %u microseconds.\n",
137 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
139 * This resets pipelines, timers, counters, state machines, etc.
140 * Transactions will be terminated immediately, and operational registers
141 * will be set to their defaults.
143 int xhci_reset(struct xhci_hcd *xhci)
149 state = xhci_readl(xhci, &xhci->op_regs->status);
150 if ((state & STS_HALT) == 0) {
151 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
155 xhci_dbg(xhci, "// Reset the HC\n");
156 command = xhci_readl(xhci, &xhci->op_regs->command);
157 command |= CMD_RESET;
158 xhci_writel(xhci, command, &xhci->op_regs->command);
159 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
160 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
162 ret = handshake(xhci, &xhci->op_regs->command,
163 CMD_RESET, 0, 250 * 1000);
167 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
169 * xHCI cannot write to any doorbells or operational registers other
170 * than status until the "Controller Not Ready" flag is cleared.
172 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
175 static irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd)
179 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
188 * free all IRQs request
190 static void xhci_free_irq(struct xhci_hcd *xhci)
193 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
195 /* return if using legacy interrupt */
196 if (xhci_to_hcd(xhci)->irq >= 0)
199 if (xhci->msix_entries) {
200 for (i = 0; i < xhci->msix_count; i++)
201 if (xhci->msix_entries[i].vector)
202 free_irq(xhci->msix_entries[i].vector,
204 } else if (pdev->irq >= 0)
205 free_irq(pdev->irq, xhci_to_hcd(xhci));
213 static int xhci_setup_msi(struct xhci_hcd *xhci)
216 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
218 ret = pci_enable_msi(pdev);
220 xhci_err(xhci, "failed to allocate MSI entry\n");
224 ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
225 0, "xhci_hcd", xhci_to_hcd(xhci));
227 xhci_err(xhci, "disable MSI interrupt\n");
228 pci_disable_msi(pdev);
237 static int xhci_setup_msix(struct xhci_hcd *xhci)
240 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
243 * calculate number of msi-x vectors supported.
244 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
245 * with max number of interrupters based on the xhci HCSPARAMS1.
246 * - num_online_cpus: maximum msi-x vectors per CPUs core.
247 * Add additional 1 vector to ensure always available interrupt.
249 xhci->msix_count = min(num_online_cpus() + 1,
250 HCS_MAX_INTRS(xhci->hcs_params1));
253 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
255 if (!xhci->msix_entries) {
256 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
260 for (i = 0; i < xhci->msix_count; i++) {
261 xhci->msix_entries[i].entry = i;
262 xhci->msix_entries[i].vector = 0;
265 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
267 xhci_err(xhci, "Failed to enable MSI-X\n");
271 for (i = 0; i < xhci->msix_count; i++) {
272 ret = request_irq(xhci->msix_entries[i].vector,
273 (irq_handler_t)xhci_msi_irq,
274 0, "xhci_hcd", xhci_to_hcd(xhci));
282 xhci_err(xhci, "disable MSI-X interrupt\n");
284 pci_disable_msix(pdev);
286 kfree(xhci->msix_entries);
287 xhci->msix_entries = NULL;
291 /* Free any IRQs and disable MSI-X */
292 static void xhci_cleanup_msix(struct xhci_hcd *xhci)
294 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
298 if (xhci->msix_entries) {
299 pci_disable_msix(pdev);
300 kfree(xhci->msix_entries);
301 xhci->msix_entries = NULL;
303 pci_disable_msi(pdev);
310 * Initialize memory for HCD and xHC (one-time init).
312 * Program the PAGESIZE register, initialize the device context array, create
313 * device contexts (?), set up a command ring segment (or two?), create event
314 * ring (one for now).
316 int xhci_init(struct usb_hcd *hcd)
318 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
321 xhci_dbg(xhci, "xhci_init\n");
322 spin_lock_init(&xhci->lock);
324 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
325 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
327 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
329 retval = xhci_mem_init(xhci, GFP_KERNEL);
330 xhci_dbg(xhci, "Finished xhci_init\n");
336 * Called in interrupt context when there might be work
337 * queued on the event ring
339 * xhci->lock must be held by caller.
341 static void xhci_work(struct xhci_hcd *xhci)
347 * Clear the op reg interrupt status first,
348 * so we can receive interrupts from other MSI-X interrupters.
349 * Write 1 to clear the interrupt status.
351 temp = xhci_readl(xhci, &xhci->op_regs->status);
353 xhci_writel(xhci, temp, &xhci->op_regs->status);
354 /* FIXME when MSI-X is supported and there are multiple vectors */
355 /* Clear the MSI-X event interrupt status */
357 /* Acknowledge the interrupt */
358 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
360 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
361 /* Flush posted writes */
362 xhci_readl(xhci, &xhci->ir_set->irq_pending);
364 if (xhci->xhc_state & XHCI_STATE_DYING)
365 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
366 "Shouldn't IRQs be disabled?\n");
368 /* FIXME this should be a delayed service routine
369 * that clears the EHB.
371 xhci_handle_event(xhci);
373 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
374 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
375 xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
376 /* Flush posted writes -- FIXME is this necessary? */
377 xhci_readl(xhci, &xhci->ir_set->irq_pending);
380 /*-------------------------------------------------------------------------*/
383 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
384 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
385 * indicators of an event TRB error, but we check the status *first* to be safe.
387 irqreturn_t xhci_irq(struct usb_hcd *hcd)
389 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
393 spin_lock(&xhci->lock);
394 trb = xhci->event_ring->dequeue;
395 /* Check if the xHC generated the interrupt, or the irq is shared */
396 temp = xhci_readl(xhci, &xhci->op_regs->status);
397 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
398 if (temp == 0xffffffff && temp2 == 0xffffffff)
401 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
402 spin_unlock(&xhci->lock);
405 xhci_dbg(xhci, "op reg status = %08x\n", temp);
406 xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2);
407 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
408 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
409 (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
410 lower_32_bits(trb->link.segment_ptr),
411 upper_32_bits(trb->link.segment_ptr),
412 (unsigned int) trb->link.intr_target,
413 (unsigned int) trb->link.control);
415 if (temp & STS_FATAL) {
416 xhci_warn(xhci, "WARNING: Host System Error\n");
419 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
420 spin_unlock(&xhci->lock);
425 spin_unlock(&xhci->lock);
430 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
431 void xhci_event_ring_work(unsigned long arg)
436 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
439 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
441 spin_lock_irqsave(&xhci->lock, flags);
442 temp = xhci_readl(xhci, &xhci->op_regs->status);
443 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
444 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
445 xhci_dbg(xhci, "HW died, polling stopped.\n");
446 spin_unlock_irqrestore(&xhci->lock, flags);
450 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
451 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
452 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
453 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
454 xhci->error_bitmask = 0;
455 xhci_dbg(xhci, "Event ring:\n");
456 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
457 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
458 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
459 temp_64 &= ~ERST_PTR_MASK;
460 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
461 xhci_dbg(xhci, "Command ring:\n");
462 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
463 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
464 xhci_dbg_cmd_ptrs(xhci);
465 for (i = 0; i < MAX_HC_SLOTS; ++i) {
468 for (j = 0; j < 31; ++j) {
469 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
473 if (xhci->noops_submitted != NUM_TEST_NOOPS)
474 if (xhci_setup_one_noop(xhci))
475 xhci_ring_cmd_db(xhci);
476 spin_unlock_irqrestore(&xhci->lock, flags);
479 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
481 xhci_dbg(xhci, "Quit polling the event ring.\n");
486 * Start the HC after it was halted.
488 * This function is called by the USB core when the HC driver is added.
489 * Its opposite is xhci_stop().
491 * xhci_init() must be called once before this function can be called.
492 * Reset the HC, enable device slot contexts, program DCBAAP, and
493 * set command ring pointer and event ring pointer.
495 * Setup MSI-X vectors and enable interrupts.
497 int xhci_run(struct usb_hcd *hcd)
502 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
503 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
504 void (*doorbell)(struct xhci_hcd *) = NULL;
506 hcd->uses_new_polling = 1;
508 xhci_dbg(xhci, "xhci_run\n");
509 /* unregister the legacy interrupt */
511 free_irq(hcd->irq, hcd);
514 ret = xhci_setup_msix(xhci);
516 /* fall back to msi*/
517 ret = xhci_setup_msi(xhci);
520 /* fall back to legacy interrupt*/
521 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
522 hcd->irq_descr, hcd);
524 xhci_err(xhci, "request interrupt %d failed\n",
528 hcd->irq = pdev->irq;
531 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
532 init_timer(&xhci->event_ring_timer);
533 xhci->event_ring_timer.data = (unsigned long) xhci;
534 xhci->event_ring_timer.function = xhci_event_ring_work;
535 /* Poll the event ring */
536 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
538 xhci_dbg(xhci, "Setting event ring polling timer\n");
539 add_timer(&xhci->event_ring_timer);
542 xhci_dbg(xhci, "Command ring memory map follows:\n");
543 xhci_debug_ring(xhci, xhci->cmd_ring);
544 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
545 xhci_dbg_cmd_ptrs(xhci);
547 xhci_dbg(xhci, "ERST memory map follows:\n");
548 xhci_dbg_erst(xhci, &xhci->erst);
549 xhci_dbg(xhci, "Event ring:\n");
550 xhci_debug_ring(xhci, xhci->event_ring);
551 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
552 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
553 temp_64 &= ~ERST_PTR_MASK;
554 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
556 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
557 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
558 temp &= ~ER_IRQ_INTERVAL_MASK;
560 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
562 /* Set the HCD state before we enable the irqs */
563 hcd->state = HC_STATE_RUNNING;
564 temp = xhci_readl(xhci, &xhci->op_regs->command);
566 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
568 xhci_writel(xhci, temp, &xhci->op_regs->command);
570 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
571 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
572 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
573 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
574 &xhci->ir_set->irq_pending);
575 xhci_print_ir_set(xhci, xhci->ir_set, 0);
577 if (NUM_TEST_NOOPS > 0)
578 doorbell = xhci_setup_one_noop(xhci);
579 if (xhci->quirks & XHCI_NEC_HOST)
580 xhci_queue_vendor_command(xhci, 0, 0, 0,
581 TRB_TYPE(TRB_NEC_GET_FW));
583 if (xhci_start(xhci)) {
590 if (xhci->quirks & XHCI_NEC_HOST)
591 xhci_ring_cmd_db(xhci);
593 xhci_dbg(xhci, "Finished xhci_run\n");
600 * This function is called by the USB core when the HC driver is removed.
601 * Its opposite is xhci_run().
603 * Disable device contexts, disable IRQs, and quiesce the HC.
604 * Reset the HC, finish any completed transactions, and cleanup memory.
606 void xhci_stop(struct usb_hcd *hcd)
609 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
611 spin_lock_irq(&xhci->lock);
614 xhci_cleanup_msix(xhci);
615 spin_unlock_irq(&xhci->lock);
617 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
618 /* Tell the event ring poll function not to reschedule */
620 del_timer_sync(&xhci->event_ring_timer);
623 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
624 temp = xhci_readl(xhci, &xhci->op_regs->status);
625 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
626 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
627 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
628 &xhci->ir_set->irq_pending);
629 xhci_print_ir_set(xhci, xhci->ir_set, 0);
631 xhci_dbg(xhci, "cleaning up memory\n");
632 xhci_mem_cleanup(xhci);
633 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
634 xhci_readl(xhci, &xhci->op_regs->status));
638 * Shutdown HC (not bus-specific)
640 * This is called when the machine is rebooting or halting. We assume that the
641 * machine will be powered off, and the HC's internal state will be reset.
642 * Don't bother to free memory.
644 void xhci_shutdown(struct usb_hcd *hcd)
646 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
648 spin_lock_irq(&xhci->lock);
650 xhci_cleanup_msix(xhci);
651 spin_unlock_irq(&xhci->lock);
653 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
654 xhci_readl(xhci, &xhci->op_regs->status));
657 /*-------------------------------------------------------------------------*/
660 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
661 * HCDs. Find the index for an endpoint given its descriptor. Use the return
662 * value to right shift 1 for the bitmask.
664 * Index = (epnum * 2) + direction - 1,
665 * where direction = 0 for OUT, 1 for IN.
666 * For control endpoints, the IN index is used (OUT index is unused), so
667 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
669 unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
672 if (usb_endpoint_xfer_control(desc))
673 index = (unsigned int) (usb_endpoint_num(desc)*2);
675 index = (unsigned int) (usb_endpoint_num(desc)*2) +
676 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
680 /* Find the flag for this endpoint (for use in the control context). Use the
681 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
684 unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
686 return 1 << (xhci_get_endpoint_index(desc) + 1);
689 /* Find the flag for this endpoint (for use in the control context). Use the
690 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
693 unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
695 return 1 << (ep_index + 1);
698 /* Compute the last valid endpoint context index. Basically, this is the
699 * endpoint index plus one. For slot contexts with more than valid endpoint,
700 * we find the most significant bit set in the added contexts flags.
701 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
702 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
704 unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
706 return fls(added_ctxs) - 1;
709 /* Returns 1 if the arguments are OK;
710 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
712 int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
713 struct usb_host_endpoint *ep, int check_ep, const char *func) {
714 if (!hcd || (check_ep && !ep) || !udev) {
715 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
720 printk(KERN_DEBUG "xHCI %s called for root hub\n",
724 if (!udev->slot_id) {
725 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
732 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
733 struct usb_device *udev, struct xhci_command *command,
734 bool ctx_change, bool must_succeed);
737 * Full speed devices may have a max packet size greater than 8 bytes, but the
738 * USB core doesn't know that until it reads the first 8 bytes of the
739 * descriptor. If the usb_device's max packet size changes after that point,
740 * we need to issue an evaluate context command and wait on it.
742 static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
743 unsigned int ep_index, struct urb *urb)
745 struct xhci_container_ctx *in_ctx;
746 struct xhci_container_ctx *out_ctx;
747 struct xhci_input_control_ctx *ctrl_ctx;
748 struct xhci_ep_ctx *ep_ctx;
750 int hw_max_packet_size;
753 out_ctx = xhci->devs[slot_id]->out_ctx;
754 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
755 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
756 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
757 if (hw_max_packet_size != max_packet_size) {
758 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
759 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
761 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
763 xhci_dbg(xhci, "Issuing evaluate context command.\n");
765 /* Set up the modified control endpoint 0 */
766 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
767 xhci->devs[slot_id]->out_ctx, ep_index);
768 in_ctx = xhci->devs[slot_id]->in_ctx;
769 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
770 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
771 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
773 /* Set up the input context flags for the command */
774 /* FIXME: This won't work if a non-default control endpoint
775 * changes max packet sizes.
777 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
778 ctrl_ctx->add_flags = EP0_FLAG;
779 ctrl_ctx->drop_flags = 0;
781 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
782 xhci_dbg_ctx(xhci, in_ctx, ep_index);
783 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
784 xhci_dbg_ctx(xhci, out_ctx, ep_index);
786 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
789 /* Clean up the input context for later use by bandwidth
792 ctrl_ctx->add_flags = SLOT_FLAG;
798 * non-error returns are a promise to giveback() the urb later
799 * we drop ownership so next owner (or urb unlink) can get it
801 int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
803 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
806 unsigned int slot_id, ep_index;
807 struct urb_priv *urb_priv;
810 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
813 slot_id = urb->dev->slot_id;
814 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
816 if (!xhci->devs || !xhci->devs[slot_id]) {
818 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
822 if (!HCD_HW_ACCESSIBLE(hcd)) {
824 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
829 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
830 size = urb->number_of_packets;
834 urb_priv = kzalloc(sizeof(struct urb_priv) +
835 size * sizeof(struct xhci_td *), mem_flags);
839 for (i = 0; i < size; i++) {
840 urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags);
841 if (!urb_priv->td[i]) {
842 urb_priv->length = i;
843 xhci_urb_free_priv(xhci, urb_priv);
848 urb_priv->length = size;
849 urb_priv->td_cnt = 0;
850 urb->hcpriv = urb_priv;
852 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
853 /* Check to see if the max packet size for the default control
854 * endpoint changed during FS device enumeration
856 if (urb->dev->speed == USB_SPEED_FULL) {
857 ret = xhci_check_maxpacket(xhci, slot_id,
863 /* We have a spinlock and interrupts disabled, so we must pass
864 * atomic context to this function, which may allocate memory.
866 spin_lock_irqsave(&xhci->lock, flags);
867 if (xhci->xhc_state & XHCI_STATE_DYING)
869 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
871 spin_unlock_irqrestore(&xhci->lock, flags);
872 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
873 spin_lock_irqsave(&xhci->lock, flags);
874 if (xhci->xhc_state & XHCI_STATE_DYING)
876 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
877 EP_GETTING_STREAMS) {
878 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
879 "is transitioning to using streams.\n");
881 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
882 EP_GETTING_NO_STREAMS) {
883 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
884 "is transitioning to "
885 "not having streams.\n");
888 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
891 spin_unlock_irqrestore(&xhci->lock, flags);
892 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
893 spin_lock_irqsave(&xhci->lock, flags);
894 if (xhci->xhc_state & XHCI_STATE_DYING)
896 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
898 spin_unlock_irqrestore(&xhci->lock, flags);
905 xhci_urb_free_priv(xhci, urb_priv);
907 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
908 "non-responsive xHCI host.\n",
909 urb->ep->desc.bEndpointAddress, urb);
910 spin_unlock_irqrestore(&xhci->lock, flags);
915 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
916 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
917 * should pick up where it left off in the TD, unless a Set Transfer Ring
918 * Dequeue Pointer is issued.
920 * The TRBs that make up the buffers for the canceled URB will be "removed" from
921 * the ring. Since the ring is a contiguous structure, they can't be physically
922 * removed. Instead, there are two options:
924 * 1) If the HC is in the middle of processing the URB to be canceled, we
925 * simply move the ring's dequeue pointer past those TRBs using the Set
926 * Transfer Ring Dequeue Pointer command. This will be the common case,
927 * when drivers timeout on the last submitted URB and attempt to cancel.
929 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
930 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
931 * HC will need to invalidate the any TRBs it has cached after the stop
932 * endpoint command, as noted in the xHCI 0.95 errata.
934 * 3) The TD may have completed by the time the Stop Endpoint Command
935 * completes, so software needs to handle that case too.
937 * This function should protect against the TD enqueueing code ringing the
938 * doorbell while this code is waiting for a Stop Endpoint command to complete.
939 * It also needs to account for multiple cancellations on happening at the same
940 * time for the same endpoint.
942 * Note that this function can be called in any context, or so says
943 * usb_hcd_unlink_urb()
945 int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
950 struct xhci_hcd *xhci;
951 struct urb_priv *urb_priv;
953 unsigned int ep_index;
954 struct xhci_ring *ep_ring;
955 struct xhci_virt_ep *ep;
957 xhci = hcd_to_xhci(hcd);
958 spin_lock_irqsave(&xhci->lock, flags);
959 /* Make sure the URB hasn't completed or been unlinked already */
960 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
961 if (ret || !urb->hcpriv)
963 temp = xhci_readl(xhci, &xhci->op_regs->status);
964 if (temp == 0xffffffff) {
965 xhci_dbg(xhci, "HW died, freeing TD.\n");
966 urb_priv = urb->hcpriv;
968 usb_hcd_unlink_urb_from_ep(hcd, urb);
969 spin_unlock_irqrestore(&xhci->lock, flags);
970 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN);
971 xhci_urb_free_priv(xhci, urb_priv);
974 if (xhci->xhc_state & XHCI_STATE_DYING) {
975 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
976 "non-responsive xHCI host.\n",
977 urb->ep->desc.bEndpointAddress, urb);
978 /* Let the stop endpoint command watchdog timer (which set this
979 * state) finish cleaning up the endpoint TD lists. We must
980 * have caught it in the middle of dropping a lock and giving
986 xhci_dbg(xhci, "Cancel URB %p\n", urb);
987 xhci_dbg(xhci, "Event ring:\n");
988 xhci_debug_ring(xhci, xhci->event_ring);
989 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
990 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
991 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
997 xhci_dbg(xhci, "Endpoint ring:\n");
998 xhci_debug_ring(xhci, ep_ring);
1000 urb_priv = urb->hcpriv;
1002 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1003 td = urb_priv->td[i];
1004 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1007 /* Queue a stop endpoint command, but only if this is
1008 * the first cancellation to be handled.
1010 if (!(ep->ep_state & EP_HALT_PENDING)) {
1011 ep->ep_state |= EP_HALT_PENDING;
1012 ep->stop_cmds_pending++;
1013 ep->stop_cmd_timer.expires = jiffies +
1014 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1015 add_timer(&ep->stop_cmd_timer);
1016 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
1017 xhci_ring_cmd_db(xhci);
1020 spin_unlock_irqrestore(&xhci->lock, flags);
1024 /* Drop an endpoint from a new bandwidth configuration for this device.
1025 * Only one call to this function is allowed per endpoint before
1026 * check_bandwidth() or reset_bandwidth() must be called.
1027 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1028 * add the endpoint to the schedule with possibly new parameters denoted by a
1029 * different endpoint descriptor in usb_host_endpoint.
1030 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1033 * The USB core will not allow URBs to be queued to an endpoint that is being
1034 * disabled, so there's no need for mutual exclusion to protect
1035 * the xhci->devs[slot_id] structure.
1037 int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1038 struct usb_host_endpoint *ep)
1040 struct xhci_hcd *xhci;
1041 struct xhci_container_ctx *in_ctx, *out_ctx;
1042 struct xhci_input_control_ctx *ctrl_ctx;
1043 struct xhci_slot_ctx *slot_ctx;
1044 unsigned int last_ctx;
1045 unsigned int ep_index;
1046 struct xhci_ep_ctx *ep_ctx;
1048 u32 new_add_flags, new_drop_flags, new_slot_info;
1051 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
1054 xhci = hcd_to_xhci(hcd);
1055 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1057 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1058 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1059 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1060 __func__, drop_flag);
1064 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1065 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1070 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
1071 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1072 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1073 ep_index = xhci_get_endpoint_index(&ep->desc);
1074 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1075 /* If the HC already knows the endpoint is disabled,
1076 * or the HCD has noted it is disabled, ignore this request
1078 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
1079 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
1080 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1085 ctrl_ctx->drop_flags |= drop_flag;
1086 new_drop_flags = ctrl_ctx->drop_flags;
1088 ctrl_ctx->add_flags &= ~drop_flag;
1089 new_add_flags = ctrl_ctx->add_flags;
1091 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
1092 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1093 /* Update the last valid endpoint context, if we deleted the last one */
1094 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
1095 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1096 slot_ctx->dev_info |= LAST_CTX(last_ctx);
1098 new_slot_info = slot_ctx->dev_info;
1100 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1102 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1103 (unsigned int) ep->desc.bEndpointAddress,
1105 (unsigned int) new_drop_flags,
1106 (unsigned int) new_add_flags,
1107 (unsigned int) new_slot_info);
1111 /* Add an endpoint to a new possible bandwidth configuration for this device.
1112 * Only one call to this function is allowed per endpoint before
1113 * check_bandwidth() or reset_bandwidth() must be called.
1114 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1115 * add the endpoint to the schedule with possibly new parameters denoted by a
1116 * different endpoint descriptor in usb_host_endpoint.
1117 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1120 * The USB core will not allow URBs to be queued to an endpoint until the
1121 * configuration or alt setting is installed in the device, so there's no need
1122 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1124 int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1125 struct usb_host_endpoint *ep)
1127 struct xhci_hcd *xhci;
1128 struct xhci_container_ctx *in_ctx, *out_ctx;
1129 unsigned int ep_index;
1130 struct xhci_ep_ctx *ep_ctx;
1131 struct xhci_slot_ctx *slot_ctx;
1132 struct xhci_input_control_ctx *ctrl_ctx;
1134 unsigned int last_ctx;
1135 u32 new_add_flags, new_drop_flags, new_slot_info;
1138 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
1140 /* So we won't queue a reset ep command for a root hub */
1144 xhci = hcd_to_xhci(hcd);
1146 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1147 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1148 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1149 /* FIXME when we have to issue an evaluate endpoint command to
1150 * deal with ep0 max packet size changing once we get the
1153 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1154 __func__, added_ctxs);
1158 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1159 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1164 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
1165 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1166 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1167 ep_index = xhci_get_endpoint_index(&ep->desc);
1168 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1169 /* If the HCD has already noted the endpoint is enabled,
1170 * ignore this request.
1172 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
1173 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1179 * Configuration and alternate setting changes must be done in
1180 * process context, not interrupt context (or so documenation
1181 * for usb_set_interface() and usb_set_configuration() claim).
1183 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
1184 udev, ep, GFP_NOIO) < 0) {
1185 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1186 __func__, ep->desc.bEndpointAddress);
1190 ctrl_ctx->add_flags |= added_ctxs;
1191 new_add_flags = ctrl_ctx->add_flags;
1193 /* If xhci_endpoint_disable() was called for this endpoint, but the
1194 * xHC hasn't been notified yet through the check_bandwidth() call,
1195 * this re-adds a new state for the endpoint from the new endpoint
1196 * descriptors. We must drop and re-add this endpoint, so we leave the
1199 new_drop_flags = ctrl_ctx->drop_flags;
1201 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1202 /* Update the last valid endpoint context, if we just added one past */
1203 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
1204 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1205 slot_ctx->dev_info |= LAST_CTX(last_ctx);
1207 new_slot_info = slot_ctx->dev_info;
1209 /* Store the usb_device pointer for later use */
1212 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1213 (unsigned int) ep->desc.bEndpointAddress,
1215 (unsigned int) new_drop_flags,
1216 (unsigned int) new_add_flags,
1217 (unsigned int) new_slot_info);
1221 static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
1223 struct xhci_input_control_ctx *ctrl_ctx;
1224 struct xhci_ep_ctx *ep_ctx;
1225 struct xhci_slot_ctx *slot_ctx;
1228 /* When a device's add flag and drop flag are zero, any subsequent
1229 * configure endpoint command will leave that endpoint's state
1230 * untouched. Make sure we don't leave any old state in the input
1231 * endpoint contexts.
1233 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1234 ctrl_ctx->drop_flags = 0;
1235 ctrl_ctx->add_flags = 0;
1236 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1237 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1238 /* Endpoint 0 is always valid */
1239 slot_ctx->dev_info |= LAST_CTX(1);
1240 for (i = 1; i < 31; ++i) {
1241 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
1242 ep_ctx->ep_info = 0;
1243 ep_ctx->ep_info2 = 0;
1245 ep_ctx->tx_info = 0;
1249 static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
1250 struct usb_device *udev, int *cmd_status)
1254 switch (*cmd_status) {
1256 dev_warn(&udev->dev, "Not enough host controller resources "
1257 "for new device state.\n");
1259 /* FIXME: can we allocate more resources for the HC? */
1262 dev_warn(&udev->dev, "Not enough bandwidth "
1263 "for new device state.\n");
1265 /* FIXME: can we go back to the old state? */
1268 /* the HCD set up something wrong */
1269 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1271 "and endpoint is not disabled.\n");
1275 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1279 xhci_err(xhci, "ERROR: unexpected command completion "
1280 "code 0x%x.\n", *cmd_status);
1287 static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
1288 struct usb_device *udev, int *cmd_status)
1291 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
1293 switch (*cmd_status) {
1295 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1296 "context command.\n");
1300 dev_warn(&udev->dev, "WARN: slot not enabled for"
1301 "evaluate context command.\n");
1302 case COMP_CTX_STATE:
1303 dev_warn(&udev->dev, "WARN: invalid context state for "
1304 "evaluate context command.\n");
1305 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1309 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1313 xhci_err(xhci, "ERROR: unexpected command completion "
1314 "code 0x%x.\n", *cmd_status);
1321 /* Issue a configure endpoint command or evaluate context command
1322 * and wait for it to finish.
1324 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1325 struct usb_device *udev,
1326 struct xhci_command *command,
1327 bool ctx_change, bool must_succeed)
1331 unsigned long flags;
1332 struct xhci_container_ctx *in_ctx;
1333 struct completion *cmd_completion;
1335 struct xhci_virt_device *virt_dev;
1337 spin_lock_irqsave(&xhci->lock, flags);
1338 virt_dev = xhci->devs[udev->slot_id];
1340 in_ctx = command->in_ctx;
1341 cmd_completion = command->completion;
1342 cmd_status = &command->status;
1343 command->command_trb = xhci->cmd_ring->enqueue;
1344 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
1346 in_ctx = virt_dev->in_ctx;
1347 cmd_completion = &virt_dev->cmd_completion;
1348 cmd_status = &virt_dev->cmd_status;
1350 init_completion(cmd_completion);
1353 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
1354 udev->slot_id, must_succeed);
1356 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
1360 list_del(&command->cmd_list);
1361 spin_unlock_irqrestore(&xhci->lock, flags);
1362 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
1365 xhci_ring_cmd_db(xhci);
1366 spin_unlock_irqrestore(&xhci->lock, flags);
1368 /* Wait for the configure endpoint command to complete */
1369 timeleft = wait_for_completion_interruptible_timeout(
1371 USB_CTRL_SET_TIMEOUT);
1372 if (timeleft <= 0) {
1373 xhci_warn(xhci, "%s while waiting for %s command\n",
1374 timeleft == 0 ? "Timeout" : "Signal",
1376 "configure endpoint" :
1377 "evaluate context");
1378 /* FIXME cancel the configure endpoint command */
1383 return xhci_configure_endpoint_result(xhci, udev, cmd_status);
1384 return xhci_evaluate_context_result(xhci, udev, cmd_status);
1387 /* Called after one or more calls to xhci_add_endpoint() or
1388 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1389 * to call xhci_reset_bandwidth().
1391 * Since we are in the middle of changing either configuration or
1392 * installing a new alt setting, the USB core won't allow URBs to be
1393 * enqueued for any endpoint on the old config or interface. Nothing
1394 * else should be touching the xhci->devs[slot_id] structure, so we
1395 * don't need to take the xhci->lock for manipulating that.
1397 int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1401 struct xhci_hcd *xhci;
1402 struct xhci_virt_device *virt_dev;
1403 struct xhci_input_control_ctx *ctrl_ctx;
1404 struct xhci_slot_ctx *slot_ctx;
1406 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1409 xhci = hcd_to_xhci(hcd);
1411 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
1412 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1416 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1417 virt_dev = xhci->devs[udev->slot_id];
1419 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
1420 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1421 ctrl_ctx->add_flags |= SLOT_FLAG;
1422 ctrl_ctx->add_flags &= ~EP0_FLAG;
1423 ctrl_ctx->drop_flags &= ~SLOT_FLAG;
1424 ctrl_ctx->drop_flags &= ~EP0_FLAG;
1425 xhci_dbg(xhci, "New Input Control Context:\n");
1426 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1427 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
1428 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
1430 ret = xhci_configure_endpoint(xhci, udev, NULL,
1433 /* Callee should call reset_bandwidth() */
1437 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
1438 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
1439 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
1441 xhci_zero_in_ctx(xhci, virt_dev);
1442 /* Install new rings and free or cache any old rings */
1443 for (i = 1; i < 31; ++i) {
1444 if (!virt_dev->eps[i].new_ring)
1446 /* Only cache or free the old ring if it exists.
1447 * It may not if this is the first add of an endpoint.
1449 if (virt_dev->eps[i].ring) {
1450 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
1452 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
1453 virt_dev->eps[i].new_ring = NULL;
1459 void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1461 struct xhci_hcd *xhci;
1462 struct xhci_virt_device *virt_dev;
1465 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1468 xhci = hcd_to_xhci(hcd);
1470 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1471 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1475 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1476 virt_dev = xhci->devs[udev->slot_id];
1477 /* Free any rings allocated for added endpoints */
1478 for (i = 0; i < 31; ++i) {
1479 if (virt_dev->eps[i].new_ring) {
1480 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
1481 virt_dev->eps[i].new_ring = NULL;
1484 xhci_zero_in_ctx(xhci, virt_dev);
1487 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
1488 struct xhci_container_ctx *in_ctx,
1489 struct xhci_container_ctx *out_ctx,
1490 u32 add_flags, u32 drop_flags)
1492 struct xhci_input_control_ctx *ctrl_ctx;
1493 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1494 ctrl_ctx->add_flags = add_flags;
1495 ctrl_ctx->drop_flags = drop_flags;
1496 xhci_slot_copy(xhci, in_ctx, out_ctx);
1497 ctrl_ctx->add_flags |= SLOT_FLAG;
1499 xhci_dbg(xhci, "Input Context:\n");
1500 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
1503 void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
1504 unsigned int slot_id, unsigned int ep_index,
1505 struct xhci_dequeue_state *deq_state)
1507 struct xhci_container_ctx *in_ctx;
1508 struct xhci_ep_ctx *ep_ctx;
1512 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1513 xhci->devs[slot_id]->out_ctx, ep_index);
1514 in_ctx = xhci->devs[slot_id]->in_ctx;
1515 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1516 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
1517 deq_state->new_deq_ptr);
1519 xhci_warn(xhci, "WARN Cannot submit config ep after "
1520 "reset ep command\n");
1521 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
1522 deq_state->new_deq_seg,
1523 deq_state->new_deq_ptr);
1526 ep_ctx->deq = addr | deq_state->new_cycle_state;
1528 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
1529 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
1530 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
1533 void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
1534 struct usb_device *udev, unsigned int ep_index)
1536 struct xhci_dequeue_state deq_state;
1537 struct xhci_virt_ep *ep;
1539 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
1540 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1541 /* We need to move the HW's dequeue pointer past this TD,
1542 * or it will attempt to resend it on the next doorbell ring.
1544 xhci_find_new_dequeue_state(xhci, udev->slot_id,
1545 ep_index, ep->stopped_stream, ep->stopped_td,
1548 /* HW with the reset endpoint quirk will use the saved dequeue state to
1549 * issue a configure endpoint command later.
1551 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
1552 xhci_dbg(xhci, "Queueing new dequeue state\n");
1553 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
1554 ep_index, ep->stopped_stream, &deq_state);
1556 /* Better hope no one uses the input context between now and the
1557 * reset endpoint completion!
1558 * XXX: No idea how this hardware will react when stream rings
1561 xhci_dbg(xhci, "Setting up input context for "
1562 "configure endpoint command\n");
1563 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
1564 ep_index, &deq_state);
1568 /* Deal with stalled endpoints. The core should have sent the control message
1569 * to clear the halt condition. However, we need to make the xHCI hardware
1570 * reset its sequence number, since a device will expect a sequence number of
1571 * zero after the halt condition is cleared.
1572 * Context: in_interrupt
1574 void xhci_endpoint_reset(struct usb_hcd *hcd,
1575 struct usb_host_endpoint *ep)
1577 struct xhci_hcd *xhci;
1578 struct usb_device *udev;
1579 unsigned int ep_index;
1580 unsigned long flags;
1582 struct xhci_virt_ep *virt_ep;
1584 xhci = hcd_to_xhci(hcd);
1585 udev = (struct usb_device *) ep->hcpriv;
1586 /* Called with a root hub endpoint (or an endpoint that wasn't added
1587 * with xhci_add_endpoint()
1591 ep_index = xhci_get_endpoint_index(&ep->desc);
1592 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1593 if (!virt_ep->stopped_td) {
1594 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
1595 ep->desc.bEndpointAddress);
1598 if (usb_endpoint_xfer_control(&ep->desc)) {
1599 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
1603 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1604 spin_lock_irqsave(&xhci->lock, flags);
1605 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
1607 * Can't change the ring dequeue pointer until it's transitioned to the
1608 * stopped state, which is only upon a successful reset endpoint
1609 * command. Better hope that last command worked!
1612 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
1613 kfree(virt_ep->stopped_td);
1614 xhci_ring_cmd_db(xhci);
1616 virt_ep->stopped_td = NULL;
1617 virt_ep->stopped_trb = NULL;
1618 virt_ep->stopped_stream = 0;
1619 spin_unlock_irqrestore(&xhci->lock, flags);
1622 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1625 static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
1626 struct usb_device *udev, struct usb_host_endpoint *ep,
1627 unsigned int slot_id)
1630 unsigned int ep_index;
1631 unsigned int ep_state;
1635 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, __func__);
1638 if (ep->ss_ep_comp.bmAttributes == 0) {
1639 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
1640 " descriptor for ep 0x%x does not support streams\n",
1641 ep->desc.bEndpointAddress);
1645 ep_index = xhci_get_endpoint_index(&ep->desc);
1646 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1647 if (ep_state & EP_HAS_STREAMS ||
1648 ep_state & EP_GETTING_STREAMS) {
1649 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
1650 "already has streams set up.\n",
1651 ep->desc.bEndpointAddress);
1652 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
1653 "dynamic stream context array reallocation.\n");
1656 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
1657 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
1658 "endpoint 0x%x; URBs are pending.\n",
1659 ep->desc.bEndpointAddress);
1665 static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
1666 unsigned int *num_streams, unsigned int *num_stream_ctxs)
1668 unsigned int max_streams;
1670 /* The stream context array size must be a power of two */
1671 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
1673 * Find out how many primary stream array entries the host controller
1674 * supports. Later we may use secondary stream arrays (similar to 2nd
1675 * level page entries), but that's an optional feature for xHCI host
1676 * controllers. xHCs must support at least 4 stream IDs.
1678 max_streams = HCC_MAX_PSA(xhci->hcc_params);
1679 if (*num_stream_ctxs > max_streams) {
1680 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
1682 *num_stream_ctxs = max_streams;
1683 *num_streams = max_streams;
1687 /* Returns an error code if one of the endpoint already has streams.
1688 * This does not change any data structures, it only checks and gathers
1691 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
1692 struct usb_device *udev,
1693 struct usb_host_endpoint **eps, unsigned int num_eps,
1694 unsigned int *num_streams, u32 *changed_ep_bitmask)
1696 unsigned int max_streams;
1697 unsigned int endpoint_flag;
1701 for (i = 0; i < num_eps; i++) {
1702 ret = xhci_check_streams_endpoint(xhci, udev,
1703 eps[i], udev->slot_id);
1707 max_streams = USB_SS_MAX_STREAMS(
1708 eps[i]->ss_ep_comp.bmAttributes);
1709 if (max_streams < (*num_streams - 1)) {
1710 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
1711 eps[i]->desc.bEndpointAddress,
1713 *num_streams = max_streams+1;
1716 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
1717 if (*changed_ep_bitmask & endpoint_flag)
1719 *changed_ep_bitmask |= endpoint_flag;
1724 static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
1725 struct usb_device *udev,
1726 struct usb_host_endpoint **eps, unsigned int num_eps)
1728 u32 changed_ep_bitmask = 0;
1729 unsigned int slot_id;
1730 unsigned int ep_index;
1731 unsigned int ep_state;
1734 slot_id = udev->slot_id;
1735 if (!xhci->devs[slot_id])
1738 for (i = 0; i < num_eps; i++) {
1739 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1740 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1741 /* Are streams already being freed for the endpoint? */
1742 if (ep_state & EP_GETTING_NO_STREAMS) {
1743 xhci_warn(xhci, "WARN Can't disable streams for "
1745 "streams are being disabled already.",
1746 eps[i]->desc.bEndpointAddress);
1749 /* Are there actually any streams to free? */
1750 if (!(ep_state & EP_HAS_STREAMS) &&
1751 !(ep_state & EP_GETTING_STREAMS)) {
1752 xhci_warn(xhci, "WARN Can't disable streams for "
1754 "streams are already disabled!",
1755 eps[i]->desc.bEndpointAddress);
1756 xhci_warn(xhci, "WARN xhci_free_streams() called "
1757 "with non-streams endpoint\n");
1760 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
1762 return changed_ep_bitmask;
1766 * The USB device drivers use this function (though the HCD interface in USB
1767 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
1768 * coordinate mass storage command queueing across multiple endpoints (basically
1769 * a stream ID == a task ID).
1771 * Setting up streams involves allocating the same size stream context array
1772 * for each endpoint and issuing a configure endpoint command for all endpoints.
1774 * Don't allow the call to succeed if one endpoint only supports one stream
1775 * (which means it doesn't support streams at all).
1777 * Drivers may get less stream IDs than they asked for, if the host controller
1778 * hardware or endpoints claim they can't support the number of requested
1781 int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1782 struct usb_host_endpoint **eps, unsigned int num_eps,
1783 unsigned int num_streams, gfp_t mem_flags)
1786 struct xhci_hcd *xhci;
1787 struct xhci_virt_device *vdev;
1788 struct xhci_command *config_cmd;
1789 unsigned int ep_index;
1790 unsigned int num_stream_ctxs;
1791 unsigned long flags;
1792 u32 changed_ep_bitmask = 0;
1797 /* Add one to the number of streams requested to account for
1798 * stream 0 that is reserved for xHCI usage.
1801 xhci = hcd_to_xhci(hcd);
1802 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
1805 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
1807 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
1811 /* Check to make sure all endpoints are not already configured for
1812 * streams. While we're at it, find the maximum number of streams that
1813 * all the endpoints will support and check for duplicate endpoints.
1815 spin_lock_irqsave(&xhci->lock, flags);
1816 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
1817 num_eps, &num_streams, &changed_ep_bitmask);
1819 xhci_free_command(xhci, config_cmd);
1820 spin_unlock_irqrestore(&xhci->lock, flags);
1823 if (num_streams <= 1) {
1824 xhci_warn(xhci, "WARN: endpoints can't handle "
1825 "more than one stream.\n");
1826 xhci_free_command(xhci, config_cmd);
1827 spin_unlock_irqrestore(&xhci->lock, flags);
1830 vdev = xhci->devs[udev->slot_id];
1831 /* Mark each endpoint as being in transistion, so
1832 * xhci_urb_enqueue() will reject all URBs.
1834 for (i = 0; i < num_eps; i++) {
1835 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1836 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
1838 spin_unlock_irqrestore(&xhci->lock, flags);
1840 /* Setup internal data structures and allocate HW data structures for
1841 * streams (but don't install the HW structures in the input context
1842 * until we're sure all memory allocation succeeded).
1844 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
1845 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
1846 num_stream_ctxs, num_streams);
1848 for (i = 0; i < num_eps; i++) {
1849 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1850 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
1852 num_streams, mem_flags);
1853 if (!vdev->eps[ep_index].stream_info)
1855 /* Set maxPstreams in endpoint context and update deq ptr to
1856 * point to stream context array. FIXME
1860 /* Set up the input context for a configure endpoint command. */
1861 for (i = 0; i < num_eps; i++) {
1862 struct xhci_ep_ctx *ep_ctx;
1864 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1865 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
1867 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
1868 vdev->out_ctx, ep_index);
1869 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
1870 vdev->eps[ep_index].stream_info);
1872 /* Tell the HW to drop its old copy of the endpoint context info
1873 * and add the updated copy from the input context.
1875 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
1876 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
1878 /* Issue and wait for the configure endpoint command */
1879 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
1882 /* xHC rejected the configure endpoint command for some reason, so we
1883 * leave the old ring intact and free our internal streams data
1889 spin_lock_irqsave(&xhci->lock, flags);
1890 for (i = 0; i < num_eps; i++) {
1891 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1892 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
1893 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
1894 udev->slot_id, ep_index);
1895 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
1897 xhci_free_command(xhci, config_cmd);
1898 spin_unlock_irqrestore(&xhci->lock, flags);
1900 /* Subtract 1 for stream 0, which drivers can't use */
1901 return num_streams - 1;
1904 /* If it didn't work, free the streams! */
1905 for (i = 0; i < num_eps; i++) {
1906 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1907 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
1908 vdev->eps[ep_index].stream_info = NULL;
1909 /* FIXME Unset maxPstreams in endpoint context and
1910 * update deq ptr to point to normal string ring.
1912 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
1913 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
1914 xhci_endpoint_zero(xhci, vdev, eps[i]);
1916 xhci_free_command(xhci, config_cmd);
1920 /* Transition the endpoint from using streams to being a "normal" endpoint
1923 * Modify the endpoint context state, submit a configure endpoint command,
1924 * and free all endpoint rings for streams if that completes successfully.
1926 int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1927 struct usb_host_endpoint **eps, unsigned int num_eps,
1931 struct xhci_hcd *xhci;
1932 struct xhci_virt_device *vdev;
1933 struct xhci_command *command;
1934 unsigned int ep_index;
1935 unsigned long flags;
1936 u32 changed_ep_bitmask;
1938 xhci = hcd_to_xhci(hcd);
1939 vdev = xhci->devs[udev->slot_id];
1941 /* Set up a configure endpoint command to remove the streams rings */
1942 spin_lock_irqsave(&xhci->lock, flags);
1943 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
1944 udev, eps, num_eps);
1945 if (changed_ep_bitmask == 0) {
1946 spin_unlock_irqrestore(&xhci->lock, flags);
1950 /* Use the xhci_command structure from the first endpoint. We may have
1951 * allocated too many, but the driver may call xhci_free_streams() for
1952 * each endpoint it grouped into one call to xhci_alloc_streams().
1954 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
1955 command = vdev->eps[ep_index].stream_info->free_streams_command;
1956 for (i = 0; i < num_eps; i++) {
1957 struct xhci_ep_ctx *ep_ctx;
1959 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1960 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
1961 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
1962 EP_GETTING_NO_STREAMS;
1964 xhci_endpoint_copy(xhci, command->in_ctx,
1965 vdev->out_ctx, ep_index);
1966 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
1967 &vdev->eps[ep_index]);
1969 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
1970 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
1971 spin_unlock_irqrestore(&xhci->lock, flags);
1973 /* Issue and wait for the configure endpoint command,
1974 * which must succeed.
1976 ret = xhci_configure_endpoint(xhci, udev, command,
1979 /* xHC rejected the configure endpoint command for some reason, so we
1980 * leave the streams rings intact.
1985 spin_lock_irqsave(&xhci->lock, flags);
1986 for (i = 0; i < num_eps; i++) {
1987 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1988 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
1989 vdev->eps[ep_index].stream_info = NULL;
1990 /* FIXME Unset maxPstreams in endpoint context and
1991 * update deq ptr to point to normal string ring.
1993 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
1994 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
1996 spin_unlock_irqrestore(&xhci->lock, flags);
2002 * This submits a Reset Device Command, which will set the device state to 0,
2003 * set the device address to 0, and disable all the endpoints except the default
2004 * control endpoint. The USB core should come back and call
2005 * xhci_address_device(), and then re-set up the configuration. If this is
2006 * called because of a usb_reset_and_verify_device(), then the old alternate
2007 * settings will be re-installed through the normal bandwidth allocation
2010 * Wait for the Reset Device command to finish. Remove all structures
2011 * associated with the endpoints that were disabled. Clear the input device
2012 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
2014 int xhci_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
2017 unsigned long flags;
2018 struct xhci_hcd *xhci;
2019 unsigned int slot_id;
2020 struct xhci_virt_device *virt_dev;
2021 struct xhci_command *reset_device_cmd;
2023 int last_freed_endpoint;
2025 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
2028 xhci = hcd_to_xhci(hcd);
2029 slot_id = udev->slot_id;
2030 virt_dev = xhci->devs[slot_id];
2032 xhci_dbg(xhci, "%s called with invalid slot ID %u\n",
2037 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
2038 /* Allocate the command structure that holds the struct completion.
2039 * Assume we're in process context, since the normal device reset
2040 * process has to wait for the device anyway. Storage devices are
2041 * reset as part of error handling, so use GFP_NOIO instead of
2044 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
2045 if (!reset_device_cmd) {
2046 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
2050 /* Attempt to submit the Reset Device command to the command ring */
2051 spin_lock_irqsave(&xhci->lock, flags);
2052 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
2053 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
2054 ret = xhci_queue_reset_device(xhci, slot_id);
2056 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2057 list_del(&reset_device_cmd->cmd_list);
2058 spin_unlock_irqrestore(&xhci->lock, flags);
2059 goto command_cleanup;
2061 xhci_ring_cmd_db(xhci);
2062 spin_unlock_irqrestore(&xhci->lock, flags);
2064 /* Wait for the Reset Device command to finish */
2065 timeleft = wait_for_completion_interruptible_timeout(
2066 reset_device_cmd->completion,
2067 USB_CTRL_SET_TIMEOUT);
2068 if (timeleft <= 0) {
2069 xhci_warn(xhci, "%s while waiting for reset device command\n",
2070 timeleft == 0 ? "Timeout" : "Signal");
2071 spin_lock_irqsave(&xhci->lock, flags);
2072 /* The timeout might have raced with the event ring handler, so
2073 * only delete from the list if the item isn't poisoned.
2075 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
2076 list_del(&reset_device_cmd->cmd_list);
2077 spin_unlock_irqrestore(&xhci->lock, flags);
2079 goto command_cleanup;
2082 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
2083 * unless we tried to reset a slot ID that wasn't enabled,
2084 * or the device wasn't in the addressed or configured state.
2086 ret = reset_device_cmd->status;
2088 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
2089 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
2090 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
2092 xhci_get_slot_state(xhci, virt_dev->out_ctx));
2093 xhci_info(xhci, "Not freeing device rings.\n");
2094 /* Don't treat this as an error. May change my mind later. */
2096 goto command_cleanup;
2098 xhci_dbg(xhci, "Successful reset device command.\n");
2101 if (xhci_is_vendor_info_code(xhci, ret))
2103 xhci_warn(xhci, "Unknown completion code %u for "
2104 "reset device command.\n", ret);
2106 goto command_cleanup;
2109 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
2110 last_freed_endpoint = 1;
2111 for (i = 1; i < 31; ++i) {
2112 if (!virt_dev->eps[i].ring)
2114 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2115 last_freed_endpoint = i;
2117 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
2118 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
2122 xhci_free_command(xhci, reset_device_cmd);
2127 * At this point, the struct usb_device is about to go away, the device has
2128 * disconnected, and all traffic has been stopped and the endpoints have been
2129 * disabled. Free any HC data structures associated with that device.
2131 void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
2133 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2134 struct xhci_virt_device *virt_dev;
2135 unsigned long flags;
2139 if (udev->slot_id == 0)
2141 virt_dev = xhci->devs[udev->slot_id];
2145 /* Stop any wayward timer functions (which may grab the lock) */
2146 for (i = 0; i < 31; ++i) {
2147 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
2148 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
2151 spin_lock_irqsave(&xhci->lock, flags);
2152 /* Don't disable the slot if the host controller is dead. */
2153 state = xhci_readl(xhci, &xhci->op_regs->status);
2154 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
2155 xhci_free_virt_device(xhci, udev->slot_id);
2156 spin_unlock_irqrestore(&xhci->lock, flags);
2160 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
2161 spin_unlock_irqrestore(&xhci->lock, flags);
2162 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2165 xhci_ring_cmd_db(xhci);
2166 spin_unlock_irqrestore(&xhci->lock, flags);
2168 * Event command completion handler will free any data structures
2169 * associated with the slot. XXX Can free sleep?
2174 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
2175 * timed out, or allocating memory failed. Returns 1 on success.
2177 int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
2179 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2180 unsigned long flags;
2184 spin_lock_irqsave(&xhci->lock, flags);
2185 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
2187 spin_unlock_irqrestore(&xhci->lock, flags);
2188 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2191 xhci_ring_cmd_db(xhci);
2192 spin_unlock_irqrestore(&xhci->lock, flags);
2194 /* XXX: how much time for xHC slot assignment? */
2195 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2196 USB_CTRL_SET_TIMEOUT);
2197 if (timeleft <= 0) {
2198 xhci_warn(xhci, "%s while waiting for a slot\n",
2199 timeleft == 0 ? "Timeout" : "Signal");
2200 /* FIXME cancel the enable slot request */
2204 if (!xhci->slot_id) {
2205 xhci_err(xhci, "Error while assigning device slot ID\n");
2208 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
2209 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
2210 /* Disable slot, if we can do it without mem alloc */
2211 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
2212 spin_lock_irqsave(&xhci->lock, flags);
2213 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
2214 xhci_ring_cmd_db(xhci);
2215 spin_unlock_irqrestore(&xhci->lock, flags);
2218 udev->slot_id = xhci->slot_id;
2219 /* Is this a LS or FS device under a HS hub? */
2220 /* Hub or peripherial? */
2225 * Issue an Address Device command (which will issue a SetAddress request to
2227 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
2228 * we should only issue and wait on one address command at the same time.
2230 * We add one to the device address issued by the hardware because the USB core
2231 * uses address 1 for the root hubs (even though they're not really devices).
2233 int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2235 unsigned long flags;
2237 struct xhci_virt_device *virt_dev;
2239 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2240 struct xhci_slot_ctx *slot_ctx;
2241 struct xhci_input_control_ctx *ctrl_ctx;
2244 if (!udev->slot_id) {
2245 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
2249 virt_dev = xhci->devs[udev->slot_id];
2251 /* If this is a Set Address to an unconfigured device, setup ep 0 */
2253 xhci_setup_addressable_virt_dev(xhci, udev);
2255 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
2256 /* Otherwise, assume the core has the device configured how it wants */
2257 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
2258 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
2260 spin_lock_irqsave(&xhci->lock, flags);
2261 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
2264 spin_unlock_irqrestore(&xhci->lock, flags);
2265 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2268 xhci_ring_cmd_db(xhci);
2269 spin_unlock_irqrestore(&xhci->lock, flags);
2271 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
2272 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2273 USB_CTRL_SET_TIMEOUT);
2274 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
2275 * the SetAddress() "recovery interval" required by USB and aborting the
2276 * command on a timeout.
2278 if (timeleft <= 0) {
2279 xhci_warn(xhci, "%s while waiting for a slot\n",
2280 timeleft == 0 ? "Timeout" : "Signal");
2281 /* FIXME cancel the address device command */
2285 switch (virt_dev->cmd_status) {
2286 case COMP_CTX_STATE:
2288 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
2293 dev_warn(&udev->dev, "Device not responding to set address.\n");
2297 xhci_dbg(xhci, "Successful Address Device command\n");
2300 xhci_err(xhci, "ERROR: unexpected command completion "
2301 "code 0x%x.\n", virt_dev->cmd_status);
2302 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
2303 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
2310 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
2311 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
2312 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
2314 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
2315 (unsigned long long)
2316 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
2317 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
2318 (unsigned long long)virt_dev->out_ctx->dma);
2319 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
2320 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
2321 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
2322 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
2324 * USB core uses address 1 for the roothubs, so we add one to the
2325 * address given back to us by the HC.
2327 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
2328 udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
2329 /* Zero the input context control for later use */
2330 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2331 ctrl_ctx->add_flags = 0;
2332 ctrl_ctx->drop_flags = 0;
2334 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
2335 /* XXX Meh, not sure if anyone else but choose_address uses this. */
2336 set_bit(udev->devnum, udev->bus->devmap.devicemap);
2341 /* Once a hub descriptor is fetched for a device, we need to update the xHC's
2342 * internal data structures for the device.
2344 int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
2345 struct usb_tt *tt, gfp_t mem_flags)
2347 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2348 struct xhci_virt_device *vdev;
2349 struct xhci_command *config_cmd;
2350 struct xhci_input_control_ctx *ctrl_ctx;
2351 struct xhci_slot_ctx *slot_ctx;
2352 unsigned long flags;
2353 unsigned think_time;
2356 /* Ignore root hubs */
2360 vdev = xhci->devs[hdev->slot_id];
2362 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
2365 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
2367 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
2371 spin_lock_irqsave(&xhci->lock, flags);
2372 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
2373 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
2374 ctrl_ctx->add_flags |= SLOT_FLAG;
2375 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
2376 slot_ctx->dev_info |= DEV_HUB;
2378 slot_ctx->dev_info |= DEV_MTT;
2379 if (xhci->hci_version > 0x95) {
2380 xhci_dbg(xhci, "xHCI version %x needs hub "
2381 "TT think time and number of ports\n",
2382 (unsigned int) xhci->hci_version);
2383 slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
2384 /* Set TT think time - convert from ns to FS bit times.
2385 * 0 = 8 FS bit times, 1 = 16 FS bit times,
2386 * 2 = 24 FS bit times, 3 = 32 FS bit times.
2388 think_time = tt->think_time;
2389 if (think_time != 0)
2390 think_time = (think_time / 666) - 1;
2391 slot_ctx->tt_info |= TT_THINK_TIME(think_time);
2393 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
2394 "TT think time or number of ports\n",
2395 (unsigned int) xhci->hci_version);
2397 slot_ctx->dev_state = 0;
2398 spin_unlock_irqrestore(&xhci->lock, flags);
2400 xhci_dbg(xhci, "Set up %s for hub device.\n",
2401 (xhci->hci_version > 0x95) ?
2402 "configure endpoint" : "evaluate context");
2403 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
2404 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
2406 /* Issue and wait for the configure endpoint or
2407 * evaluate context command.
2409 if (xhci->hci_version > 0x95)
2410 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2413 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2416 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
2417 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
2419 xhci_free_command(xhci, config_cmd);
2423 int xhci_get_frame(struct usb_hcd *hcd)
2425 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2426 /* EHCI mods by the periodic size. Why? */
2427 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
2430 MODULE_DESCRIPTION(DRIVER_DESC);
2431 MODULE_AUTHOR(DRIVER_AUTHOR);
2432 MODULE_LICENSE("GPL");
2434 static int __init xhci_hcd_init(void)
2439 retval = xhci_register_pci();
2442 printk(KERN_DEBUG "Problem registering PCI driver.");
2447 * Check the compiler generated sizes of structures that must be laid
2448 * out in specific ways for hardware access.
2450 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
2451 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
2452 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
2453 /* xhci_device_control has eight fields, and also
2454 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
2456 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
2457 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
2458 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
2459 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
2460 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
2461 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
2462 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
2463 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
2466 module_init(xhci_hcd_init);
2468 static void __exit xhci_hcd_cleanup(void)
2471 xhci_unregister_pci();
2474 module_exit(xhci_hcd_cleanup);