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>
29 #include <linux/dmi.h>
30 #include <linux/dma-mapping.h>
33 #include "xhci-trace.h"
35 #define DRIVER_AUTHOR "Sarah Sharp"
36 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
38 #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
40 /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
41 static int link_quirk
;
42 module_param(link_quirk
, int, S_IRUGO
| S_IWUSR
);
43 MODULE_PARM_DESC(link_quirk
, "Don't clear the chain bit on a link TRB");
45 static unsigned int quirks
;
46 module_param(quirks
, uint
, S_IRUGO
);
47 MODULE_PARM_DESC(quirks
, "Bit flags for quirks to be enabled as default");
49 /* TODO: copied from ehci-hcd.c - can this be refactored? */
51 * xhci_handshake - spin reading hc until handshake completes or fails
52 * @ptr: address of hc register to be read
53 * @mask: bits to look at in result of read
54 * @done: value of those bits when handshake succeeds
55 * @usec: timeout in microseconds
57 * Returns negative errno, or zero on success
59 * Success happens when the "mask" bits have the specified value (hardware
60 * handshake done). There are two failure modes: "usec" have passed (major
61 * hardware flakeout), or the register reads as all-ones (hardware removed).
63 int xhci_handshake(void __iomem
*ptr
, u32 mask
, u32 done
, int usec
)
69 if (result
== ~(u32
)0) /* card removed */
81 * Disable interrupts and begin the xHCI halting process.
83 void xhci_quiesce(struct xhci_hcd
*xhci
)
90 halted
= readl(&xhci
->op_regs
->status
) & STS_HALT
;
94 cmd
= readl(&xhci
->op_regs
->command
);
96 writel(cmd
, &xhci
->op_regs
->command
);
100 * Force HC into halt state.
102 * Disable any IRQs and clear the run/stop bit.
103 * HC will complete any current and actively pipelined transactions, and
104 * should halt within 16 ms of the run/stop bit being cleared.
105 * Read HC Halted bit in the status register to see when the HC is finished.
107 int xhci_halt(struct xhci_hcd
*xhci
)
110 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "// Halt the HC");
113 ret
= xhci_handshake(&xhci
->op_regs
->status
,
114 STS_HALT
, STS_HALT
, XHCI_MAX_HALT_USEC
);
116 xhci
->xhc_state
|= XHCI_STATE_HALTED
;
117 xhci
->cmd_ring_state
= CMD_RING_STATE_STOPPED
;
119 xhci_warn(xhci
, "Host not halted after %u microseconds.\n",
125 * Set the run bit and wait for the host to be running.
127 static int xhci_start(struct xhci_hcd
*xhci
)
132 temp
= readl(&xhci
->op_regs
->command
);
134 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "// Turn on HC, cmd = 0x%x.",
136 writel(temp
, &xhci
->op_regs
->command
);
139 * Wait for the HCHalted Status bit to be 0 to indicate the host is
142 ret
= xhci_handshake(&xhci
->op_regs
->status
,
143 STS_HALT
, 0, XHCI_MAX_HALT_USEC
);
144 if (ret
== -ETIMEDOUT
)
145 xhci_err(xhci
, "Host took too long to start, "
146 "waited %u microseconds.\n",
149 xhci
->xhc_state
&= ~(XHCI_STATE_HALTED
| XHCI_STATE_DYING
);
157 * This resets pipelines, timers, counters, state machines, etc.
158 * Transactions will be terminated immediately, and operational registers
159 * will be set to their defaults.
161 int xhci_reset(struct xhci_hcd
*xhci
)
167 state
= readl(&xhci
->op_regs
->status
);
168 if ((state
& STS_HALT
) == 0) {
169 xhci_warn(xhci
, "Host controller not halted, aborting reset.\n");
173 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "// Reset the HC");
174 command
= readl(&xhci
->op_regs
->command
);
175 command
|= CMD_RESET
;
176 writel(command
, &xhci
->op_regs
->command
);
178 /* Existing Intel xHCI controllers require a delay of 1 mS,
179 * after setting the CMD_RESET bit, and before accessing any
180 * HC registers. This allows the HC to complete the
181 * reset operation and be ready for HC register access.
182 * Without this delay, the subsequent HC register access,
183 * may result in a system hang very rarely.
185 if (xhci
->quirks
& XHCI_INTEL_HOST
)
188 ret
= xhci_handshake(&xhci
->op_regs
->command
,
189 CMD_RESET
, 0, 10 * 1000 * 1000);
193 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
194 "Wait for controller to be ready for doorbell rings");
196 * xHCI cannot write to any doorbells or operational registers other
197 * than status until the "Controller Not Ready" flag is cleared.
199 ret
= xhci_handshake(&xhci
->op_regs
->status
,
200 STS_CNR
, 0, 10 * 1000 * 1000);
202 for (i
= 0; i
< 2; ++i
) {
203 xhci
->bus_state
[i
].port_c_suspend
= 0;
204 xhci
->bus_state
[i
].suspended_ports
= 0;
205 xhci
->bus_state
[i
].resuming_ports
= 0;
212 static int xhci_free_msi(struct xhci_hcd
*xhci
)
216 if (!xhci
->msix_entries
)
219 for (i
= 0; i
< xhci
->msix_count
; i
++)
220 if (xhci
->msix_entries
[i
].vector
)
221 free_irq(xhci
->msix_entries
[i
].vector
,
229 static int xhci_setup_msi(struct xhci_hcd
*xhci
)
232 struct pci_dev
*pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
234 ret
= pci_enable_msi(pdev
);
236 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
237 "failed to allocate MSI entry");
241 ret
= request_irq(pdev
->irq
, xhci_msi_irq
,
242 0, "xhci_hcd", xhci_to_hcd(xhci
));
244 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
245 "disable MSI interrupt");
246 pci_disable_msi(pdev
);
254 * free all IRQs request
256 static void xhci_free_irq(struct xhci_hcd
*xhci
)
258 struct pci_dev
*pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
261 /* return if using legacy interrupt */
262 if (xhci_to_hcd(xhci
)->irq
> 0)
265 ret
= xhci_free_msi(xhci
);
269 free_irq(pdev
->irq
, xhci_to_hcd(xhci
));
277 static int xhci_setup_msix(struct xhci_hcd
*xhci
)
280 struct usb_hcd
*hcd
= xhci_to_hcd(xhci
);
281 struct pci_dev
*pdev
= to_pci_dev(hcd
->self
.controller
);
284 * calculate number of msi-x vectors supported.
285 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
286 * with max number of interrupters based on the xhci HCSPARAMS1.
287 * - num_online_cpus: maximum msi-x vectors per CPUs core.
288 * Add additional 1 vector to ensure always available interrupt.
290 xhci
->msix_count
= min(num_online_cpus() + 1,
291 HCS_MAX_INTRS(xhci
->hcs_params1
));
294 kmalloc((sizeof(struct msix_entry
))*xhci
->msix_count
,
296 if (!xhci
->msix_entries
) {
297 xhci_err(xhci
, "Failed to allocate MSI-X entries\n");
301 for (i
= 0; i
< xhci
->msix_count
; i
++) {
302 xhci
->msix_entries
[i
].entry
= i
;
303 xhci
->msix_entries
[i
].vector
= 0;
306 ret
= pci_enable_msix_exact(pdev
, xhci
->msix_entries
, xhci
->msix_count
);
308 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
309 "Failed to enable MSI-X");
313 for (i
= 0; i
< xhci
->msix_count
; i
++) {
314 ret
= request_irq(xhci
->msix_entries
[i
].vector
,
316 0, "xhci_hcd", xhci_to_hcd(xhci
));
321 hcd
->msix_enabled
= 1;
325 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "disable MSI-X interrupt");
327 pci_disable_msix(pdev
);
329 kfree(xhci
->msix_entries
);
330 xhci
->msix_entries
= NULL
;
334 /* Free any IRQs and disable MSI-X */
335 static void xhci_cleanup_msix(struct xhci_hcd
*xhci
)
337 struct usb_hcd
*hcd
= xhci_to_hcd(xhci
);
338 struct pci_dev
*pdev
= to_pci_dev(hcd
->self
.controller
);
340 if (xhci
->quirks
& XHCI_PLAT
)
345 if (xhci
->msix_entries
) {
346 pci_disable_msix(pdev
);
347 kfree(xhci
->msix_entries
);
348 xhci
->msix_entries
= NULL
;
350 pci_disable_msi(pdev
);
353 hcd
->msix_enabled
= 0;
357 static void __maybe_unused
xhci_msix_sync_irqs(struct xhci_hcd
*xhci
)
361 if (xhci
->msix_entries
) {
362 for (i
= 0; i
< xhci
->msix_count
; i
++)
363 synchronize_irq(xhci
->msix_entries
[i
].vector
);
367 static int xhci_try_enable_msi(struct usb_hcd
*hcd
)
369 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
370 struct pci_dev
*pdev
;
373 /* The xhci platform device has set up IRQs through usb_add_hcd. */
374 if (xhci
->quirks
& XHCI_PLAT
)
377 pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
379 * Some Fresco Logic host controllers advertise MSI, but fail to
380 * generate interrupts. Don't even try to enable MSI.
382 if (xhci
->quirks
& XHCI_BROKEN_MSI
)
385 /* unregister the legacy interrupt */
387 free_irq(hcd
->irq
, hcd
);
390 ret
= xhci_setup_msix(xhci
);
392 /* fall back to msi*/
393 ret
= xhci_setup_msi(xhci
);
396 /* hcd->irq is 0, we have MSI */
400 xhci_err(xhci
, "No msi-x/msi found and no IRQ in BIOS\n");
405 if (!strlen(hcd
->irq_descr
))
406 snprintf(hcd
->irq_descr
, sizeof(hcd
->irq_descr
), "%s:usb%d",
407 hcd
->driver
->description
, hcd
->self
.busnum
);
409 /* fall back to legacy interrupt*/
410 ret
= request_irq(pdev
->irq
, &usb_hcd_irq
, IRQF_SHARED
,
411 hcd
->irq_descr
, hcd
);
413 xhci_err(xhci
, "request interrupt %d failed\n",
417 hcd
->irq
= pdev
->irq
;
423 static inline int xhci_try_enable_msi(struct usb_hcd
*hcd
)
428 static inline void xhci_cleanup_msix(struct xhci_hcd
*xhci
)
432 static inline void xhci_msix_sync_irqs(struct xhci_hcd
*xhci
)
438 static void compliance_mode_recovery(unsigned long arg
)
440 struct xhci_hcd
*xhci
;
445 xhci
= (struct xhci_hcd
*)arg
;
447 for (i
= 0; i
< xhci
->num_usb3_ports
; i
++) {
448 temp
= readl(xhci
->usb3_ports
[i
]);
449 if ((temp
& PORT_PLS_MASK
) == USB_SS_PORT_LS_COMP_MOD
) {
451 * Compliance Mode Detected. Letting USB Core
452 * handle the Warm Reset
454 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
455 "Compliance mode detected->port %d",
457 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
458 "Attempting compliance mode recovery");
459 hcd
= xhci
->shared_hcd
;
461 if (hcd
->state
== HC_STATE_SUSPENDED
)
462 usb_hcd_resume_root_hub(hcd
);
464 usb_hcd_poll_rh_status(hcd
);
468 if (xhci
->port_status_u0
!= ((1 << xhci
->num_usb3_ports
)-1))
469 mod_timer(&xhci
->comp_mode_recovery_timer
,
470 jiffies
+ msecs_to_jiffies(COMP_MODE_RCVRY_MSECS
));
474 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
475 * that causes ports behind that hardware to enter compliance mode sometimes.
476 * The quirk creates a timer that polls every 2 seconds the link state of
477 * each host controller's port and recovers it by issuing a Warm reset
478 * if Compliance mode is detected, otherwise the port will become "dead" (no
479 * device connections or disconnections will be detected anymore). Becasue no
480 * status event is generated when entering compliance mode (per xhci spec),
481 * this quirk is needed on systems that have the failing hardware installed.
483 static void compliance_mode_recovery_timer_init(struct xhci_hcd
*xhci
)
485 xhci
->port_status_u0
= 0;
486 setup_timer(&xhci
->comp_mode_recovery_timer
,
487 compliance_mode_recovery
, (unsigned long)xhci
);
488 xhci
->comp_mode_recovery_timer
.expires
= jiffies
+
489 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS
);
491 set_timer_slack(&xhci
->comp_mode_recovery_timer
,
492 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS
));
493 add_timer(&xhci
->comp_mode_recovery_timer
);
494 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
495 "Compliance mode recovery timer initialized");
499 * This function identifies the systems that have installed the SN65LVPE502CP
500 * USB3.0 re-driver and that need the Compliance Mode Quirk.
502 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
504 static bool xhci_compliance_mode_recovery_timer_quirk_check(void)
506 const char *dmi_product_name
, *dmi_sys_vendor
;
508 dmi_product_name
= dmi_get_system_info(DMI_PRODUCT_NAME
);
509 dmi_sys_vendor
= dmi_get_system_info(DMI_SYS_VENDOR
);
510 if (!dmi_product_name
|| !dmi_sys_vendor
)
513 if (!(strstr(dmi_sys_vendor
, "Hewlett-Packard")))
516 if (strstr(dmi_product_name
, "Z420") ||
517 strstr(dmi_product_name
, "Z620") ||
518 strstr(dmi_product_name
, "Z820") ||
519 strstr(dmi_product_name
, "Z1 Workstation"))
525 static int xhci_all_ports_seen_u0(struct xhci_hcd
*xhci
)
527 return (xhci
->port_status_u0
== ((1 << xhci
->num_usb3_ports
)-1));
532 * Initialize memory for HCD and xHC (one-time init).
534 * Program the PAGESIZE register, initialize the device context array, create
535 * device contexts (?), set up a command ring segment (or two?), create event
536 * ring (one for now).
538 int xhci_init(struct usb_hcd
*hcd
)
540 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
543 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "xhci_init");
544 spin_lock_init(&xhci
->lock
);
545 if (xhci
->hci_version
== 0x95 && link_quirk
) {
546 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
547 "QUIRK: Not clearing Link TRB chain bits.");
548 xhci
->quirks
|= XHCI_LINK_TRB_QUIRK
;
550 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
551 "xHCI doesn't need link TRB QUIRK");
553 retval
= xhci_mem_init(xhci
, GFP_KERNEL
);
554 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "Finished xhci_init");
556 /* Initializing Compliance Mode Recovery Data If Needed */
557 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
558 xhci
->quirks
|= XHCI_COMP_MODE_QUIRK
;
559 compliance_mode_recovery_timer_init(xhci
);
565 /*-------------------------------------------------------------------------*/
568 static int xhci_run_finished(struct xhci_hcd
*xhci
)
570 if (xhci_start(xhci
)) {
574 xhci
->shared_hcd
->state
= HC_STATE_RUNNING
;
575 xhci
->cmd_ring_state
= CMD_RING_STATE_RUNNING
;
577 if (xhci
->quirks
& XHCI_NEC_HOST
)
578 xhci_ring_cmd_db(xhci
);
580 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
581 "Finished xhci_run for USB3 roothub");
586 * Start the HC after it was halted.
588 * This function is called by the USB core when the HC driver is added.
589 * Its opposite is xhci_stop().
591 * xhci_init() must be called once before this function can be called.
592 * Reset the HC, enable device slot contexts, program DCBAAP, and
593 * set command ring pointer and event ring pointer.
595 * Setup MSI-X vectors and enable interrupts.
597 int xhci_run(struct usb_hcd
*hcd
)
602 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
604 /* Start the xHCI host controller running only after the USB 2.0 roothub
608 hcd
->uses_new_polling
= 1;
609 if (!usb_hcd_is_primary_hcd(hcd
))
610 return xhci_run_finished(xhci
);
612 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "xhci_run");
614 ret
= xhci_try_enable_msi(hcd
);
618 xhci_dbg(xhci
, "Command ring memory map follows:\n");
619 xhci_debug_ring(xhci
, xhci
->cmd_ring
);
620 xhci_dbg_ring_ptrs(xhci
, xhci
->cmd_ring
);
621 xhci_dbg_cmd_ptrs(xhci
);
623 xhci_dbg(xhci
, "ERST memory map follows:\n");
624 xhci_dbg_erst(xhci
, &xhci
->erst
);
625 xhci_dbg(xhci
, "Event ring:\n");
626 xhci_debug_ring(xhci
, xhci
->event_ring
);
627 xhci_dbg_ring_ptrs(xhci
, xhci
->event_ring
);
628 temp_64
= xhci_read_64(xhci
, &xhci
->ir_set
->erst_dequeue
);
629 temp_64
&= ~ERST_PTR_MASK
;
630 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
631 "ERST deq = 64'h%0lx", (long unsigned int) temp_64
);
633 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
634 "// Set the interrupt modulation register");
635 temp
= readl(&xhci
->ir_set
->irq_control
);
636 temp
&= ~ER_IRQ_INTERVAL_MASK
;
638 writel(temp
, &xhci
->ir_set
->irq_control
);
640 /* Set the HCD state before we enable the irqs */
641 temp
= readl(&xhci
->op_regs
->command
);
643 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
644 "// Enable interrupts, cmd = 0x%x.", temp
);
645 writel(temp
, &xhci
->op_regs
->command
);
647 temp
= readl(&xhci
->ir_set
->irq_pending
);
648 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
649 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
650 xhci
->ir_set
, (unsigned int) ER_IRQ_ENABLE(temp
));
651 writel(ER_IRQ_ENABLE(temp
), &xhci
->ir_set
->irq_pending
);
652 xhci_print_ir_set(xhci
, 0);
654 if (xhci
->quirks
& XHCI_NEC_HOST
) {
655 struct xhci_command
*command
;
656 command
= xhci_alloc_command(xhci
, false, false, GFP_KERNEL
);
659 xhci_queue_vendor_command(xhci
, command
, 0, 0, 0,
660 TRB_TYPE(TRB_NEC_GET_FW
));
662 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
663 "Finished xhci_run for USB2 roothub");
666 EXPORT_SYMBOL_GPL(xhci_run
);
671 * This function is called by the USB core when the HC driver is removed.
672 * Its opposite is xhci_run().
674 * Disable device contexts, disable IRQs, and quiesce the HC.
675 * Reset the HC, finish any completed transactions, and cleanup memory.
677 void xhci_stop(struct usb_hcd
*hcd
)
680 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
682 if (xhci
->xhc_state
& XHCI_STATE_HALTED
)
685 mutex_lock(&xhci
->mutex
);
686 spin_lock_irq(&xhci
->lock
);
687 xhci
->xhc_state
|= XHCI_STATE_HALTED
;
688 xhci
->cmd_ring_state
= CMD_RING_STATE_STOPPED
;
690 /* Make sure the xHC is halted for a USB3 roothub
691 * (xhci_stop() could be called as part of failed init).
695 spin_unlock_irq(&xhci
->lock
);
697 xhci_cleanup_msix(xhci
);
699 /* Deleting Compliance Mode Recovery Timer */
700 if ((xhci
->quirks
& XHCI_COMP_MODE_QUIRK
) &&
701 (!(xhci_all_ports_seen_u0(xhci
)))) {
702 del_timer_sync(&xhci
->comp_mode_recovery_timer
);
703 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
704 "%s: compliance mode recovery timer deleted",
708 if (xhci
->quirks
& XHCI_AMD_PLL_FIX
)
711 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
712 "// Disabling event ring interrupts");
713 temp
= readl(&xhci
->op_regs
->status
);
714 writel(temp
& ~STS_EINT
, &xhci
->op_regs
->status
);
715 temp
= readl(&xhci
->ir_set
->irq_pending
);
716 writel(ER_IRQ_DISABLE(temp
), &xhci
->ir_set
->irq_pending
);
717 xhci_print_ir_set(xhci
, 0);
719 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "cleaning up memory");
720 xhci_mem_cleanup(xhci
);
721 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
722 "xhci_stop completed - status = %x",
723 readl(&xhci
->op_regs
->status
));
724 mutex_unlock(&xhci
->mutex
);
728 * Shutdown HC (not bus-specific)
730 * This is called when the machine is rebooting or halting. We assume that the
731 * machine will be powered off, and the HC's internal state will be reset.
732 * Don't bother to free memory.
734 * This will only ever be called with the main usb_hcd (the USB3 roothub).
736 void xhci_shutdown(struct usb_hcd
*hcd
)
738 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
740 if (xhci
->quirks
& XHCI_SPURIOUS_REBOOT
)
741 usb_disable_xhci_ports(to_pci_dev(hcd
->self
.controller
));
743 spin_lock_irq(&xhci
->lock
);
745 /* Workaround for spurious wakeups at shutdown with HSW */
746 if (xhci
->quirks
& XHCI_SPURIOUS_WAKEUP
)
748 spin_unlock_irq(&xhci
->lock
);
750 xhci_cleanup_msix(xhci
);
752 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
753 "xhci_shutdown completed - status = %x",
754 readl(&xhci
->op_regs
->status
));
756 /* Yet another workaround for spurious wakeups at shutdown with HSW */
757 if (xhci
->quirks
& XHCI_SPURIOUS_WAKEUP
)
758 pci_set_power_state(to_pci_dev(hcd
->self
.controller
), PCI_D3hot
);
762 static void xhci_save_registers(struct xhci_hcd
*xhci
)
764 xhci
->s3
.command
= readl(&xhci
->op_regs
->command
);
765 xhci
->s3
.dev_nt
= readl(&xhci
->op_regs
->dev_notification
);
766 xhci
->s3
.dcbaa_ptr
= xhci_read_64(xhci
, &xhci
->op_regs
->dcbaa_ptr
);
767 xhci
->s3
.config_reg
= readl(&xhci
->op_regs
->config_reg
);
768 xhci
->s3
.erst_size
= readl(&xhci
->ir_set
->erst_size
);
769 xhci
->s3
.erst_base
= xhci_read_64(xhci
, &xhci
->ir_set
->erst_base
);
770 xhci
->s3
.erst_dequeue
= xhci_read_64(xhci
, &xhci
->ir_set
->erst_dequeue
);
771 xhci
->s3
.irq_pending
= readl(&xhci
->ir_set
->irq_pending
);
772 xhci
->s3
.irq_control
= readl(&xhci
->ir_set
->irq_control
);
775 static void xhci_restore_registers(struct xhci_hcd
*xhci
)
777 writel(xhci
->s3
.command
, &xhci
->op_regs
->command
);
778 writel(xhci
->s3
.dev_nt
, &xhci
->op_regs
->dev_notification
);
779 xhci_write_64(xhci
, xhci
->s3
.dcbaa_ptr
, &xhci
->op_regs
->dcbaa_ptr
);
780 writel(xhci
->s3
.config_reg
, &xhci
->op_regs
->config_reg
);
781 writel(xhci
->s3
.erst_size
, &xhci
->ir_set
->erst_size
);
782 xhci_write_64(xhci
, xhci
->s3
.erst_base
, &xhci
->ir_set
->erst_base
);
783 xhci_write_64(xhci
, xhci
->s3
.erst_dequeue
, &xhci
->ir_set
->erst_dequeue
);
784 writel(xhci
->s3
.irq_pending
, &xhci
->ir_set
->irq_pending
);
785 writel(xhci
->s3
.irq_control
, &xhci
->ir_set
->irq_control
);
788 static void xhci_set_cmd_ring_deq(struct xhci_hcd
*xhci
)
792 /* step 2: initialize command ring buffer */
793 val_64
= xhci_read_64(xhci
, &xhci
->op_regs
->cmd_ring
);
794 val_64
= (val_64
& (u64
) CMD_RING_RSVD_BITS
) |
795 (xhci_trb_virt_to_dma(xhci
->cmd_ring
->deq_seg
,
796 xhci
->cmd_ring
->dequeue
) &
797 (u64
) ~CMD_RING_RSVD_BITS
) |
798 xhci
->cmd_ring
->cycle_state
;
799 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
800 "// Setting command ring address to 0x%llx",
801 (long unsigned long) val_64
);
802 xhci_write_64(xhci
, val_64
, &xhci
->op_regs
->cmd_ring
);
806 * The whole command ring must be cleared to zero when we suspend the host.
808 * The host doesn't save the command ring pointer in the suspend well, so we
809 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
810 * aligned, because of the reserved bits in the command ring dequeue pointer
811 * register. Therefore, we can't just set the dequeue pointer back in the
812 * middle of the ring (TRBs are 16-byte aligned).
814 static void xhci_clear_command_ring(struct xhci_hcd
*xhci
)
816 struct xhci_ring
*ring
;
817 struct xhci_segment
*seg
;
819 ring
= xhci
->cmd_ring
;
823 sizeof(union xhci_trb
) * (TRBS_PER_SEGMENT
- 1));
824 seg
->trbs
[TRBS_PER_SEGMENT
- 1].link
.control
&=
825 cpu_to_le32(~TRB_CYCLE
);
827 } while (seg
!= ring
->deq_seg
);
829 /* Reset the software enqueue and dequeue pointers */
830 ring
->deq_seg
= ring
->first_seg
;
831 ring
->dequeue
= ring
->first_seg
->trbs
;
832 ring
->enq_seg
= ring
->deq_seg
;
833 ring
->enqueue
= ring
->dequeue
;
835 ring
->num_trbs_free
= ring
->num_segs
* (TRBS_PER_SEGMENT
- 1) - 1;
837 * Ring is now zeroed, so the HW should look for change of ownership
838 * when the cycle bit is set to 1.
840 ring
->cycle_state
= 1;
843 * Reset the hardware dequeue pointer.
844 * Yes, this will need to be re-written after resume, but we're paranoid
845 * and want to make sure the hardware doesn't access bogus memory
846 * because, say, the BIOS or an SMI started the host without changing
847 * the command ring pointers.
849 xhci_set_cmd_ring_deq(xhci
);
852 static void xhci_disable_port_wake_on_bits(struct xhci_hcd
*xhci
)
855 __le32 __iomem
**port_array
;
859 spin_lock_irqsave(&xhci
->lock
, flags
);
861 /* disble usb3 ports Wake bits*/
862 port_index
= xhci
->num_usb3_ports
;
863 port_array
= xhci
->usb3_ports
;
864 while (port_index
--) {
865 t1
= readl(port_array
[port_index
]);
866 t1
= xhci_port_state_to_neutral(t1
);
867 t2
= t1
& ~PORT_WAKE_BITS
;
869 writel(t2
, port_array
[port_index
]);
872 /* disble usb2 ports Wake bits*/
873 port_index
= xhci
->num_usb2_ports
;
874 port_array
= xhci
->usb2_ports
;
875 while (port_index
--) {
876 t1
= readl(port_array
[port_index
]);
877 t1
= xhci_port_state_to_neutral(t1
);
878 t2
= t1
& ~PORT_WAKE_BITS
;
880 writel(t2
, port_array
[port_index
]);
883 spin_unlock_irqrestore(&xhci
->lock
, flags
);
887 * Stop HC (not bus-specific)
889 * This is called when the machine transition into S3/S4 mode.
892 int xhci_suspend(struct xhci_hcd
*xhci
, bool do_wakeup
)
895 unsigned int delay
= XHCI_MAX_HALT_USEC
;
896 struct usb_hcd
*hcd
= xhci_to_hcd(xhci
);
902 if (hcd
->state
!= HC_STATE_SUSPENDED
||
903 xhci
->shared_hcd
->state
!= HC_STATE_SUSPENDED
)
906 /* Clear root port wake on bits if wakeup not allowed. */
908 xhci_disable_port_wake_on_bits(xhci
);
910 /* Don't poll the roothubs on bus suspend. */
911 xhci_dbg(xhci
, "%s: stopping port polling.\n", __func__
);
912 clear_bit(HCD_FLAG_POLL_RH
, &hcd
->flags
);
913 del_timer_sync(&hcd
->rh_timer
);
914 clear_bit(HCD_FLAG_POLL_RH
, &xhci
->shared_hcd
->flags
);
915 del_timer_sync(&xhci
->shared_hcd
->rh_timer
);
917 spin_lock_irq(&xhci
->lock
);
918 clear_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
919 clear_bit(HCD_FLAG_HW_ACCESSIBLE
, &xhci
->shared_hcd
->flags
);
920 /* step 1: stop endpoint */
921 /* skipped assuming that port suspend has done */
923 /* step 2: clear Run/Stop bit */
924 command
= readl(&xhci
->op_regs
->command
);
926 writel(command
, &xhci
->op_regs
->command
);
928 /* Some chips from Fresco Logic need an extraordinary delay */
929 delay
*= (xhci
->quirks
& XHCI_SLOW_SUSPEND
) ? 10 : 1;
931 if (xhci_handshake(&xhci
->op_regs
->status
,
932 STS_HALT
, STS_HALT
, delay
)) {
933 xhci_warn(xhci
, "WARN: xHC CMD_RUN timeout\n");
934 spin_unlock_irq(&xhci
->lock
);
937 xhci_clear_command_ring(xhci
);
939 /* step 3: save registers */
940 xhci_save_registers(xhci
);
942 /* step 4: set CSS flag */
943 command
= readl(&xhci
->op_regs
->command
);
945 writel(command
, &xhci
->op_regs
->command
);
946 if (xhci_handshake(&xhci
->op_regs
->status
,
947 STS_SAVE
, 0, 10 * 1000)) {
948 xhci_warn(xhci
, "WARN: xHC save state timeout\n");
949 spin_unlock_irq(&xhci
->lock
);
952 spin_unlock_irq(&xhci
->lock
);
955 * Deleting Compliance Mode Recovery Timer because the xHCI Host
956 * is about to be suspended.
958 if ((xhci
->quirks
& XHCI_COMP_MODE_QUIRK
) &&
959 (!(xhci_all_ports_seen_u0(xhci
)))) {
960 del_timer_sync(&xhci
->comp_mode_recovery_timer
);
961 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
962 "%s: compliance mode recovery timer deleted",
966 /* step 5: remove core well power */
967 /* synchronize irq when using MSI-X */
968 xhci_msix_sync_irqs(xhci
);
972 EXPORT_SYMBOL_GPL(xhci_suspend
);
975 * start xHC (not bus-specific)
977 * This is called when the machine transition from S3/S4 mode.
980 int xhci_resume(struct xhci_hcd
*xhci
, bool hibernated
)
982 u32 command
, temp
= 0, status
;
983 struct usb_hcd
*hcd
= xhci_to_hcd(xhci
);
984 struct usb_hcd
*secondary_hcd
;
986 bool comp_timer_running
= false;
991 /* Wait a bit if either of the roothubs need to settle from the
992 * transition into bus suspend.
994 if (time_before(jiffies
, xhci
->bus_state
[0].next_statechange
) ||
996 xhci
->bus_state
[1].next_statechange
))
999 set_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
1000 set_bit(HCD_FLAG_HW_ACCESSIBLE
, &xhci
->shared_hcd
->flags
);
1002 spin_lock_irq(&xhci
->lock
);
1003 if (xhci
->quirks
& XHCI_RESET_ON_RESUME
)
1007 /* step 1: restore register */
1008 xhci_restore_registers(xhci
);
1009 /* step 2: initialize command ring buffer */
1010 xhci_set_cmd_ring_deq(xhci
);
1011 /* step 3: restore state and start state*/
1012 /* step 3: set CRS flag */
1013 command
= readl(&xhci
->op_regs
->command
);
1015 writel(command
, &xhci
->op_regs
->command
);
1016 if (xhci_handshake(&xhci
->op_regs
->status
,
1017 STS_RESTORE
, 0, 10 * 1000)) {
1018 xhci_warn(xhci
, "WARN: xHC restore state timeout\n");
1019 spin_unlock_irq(&xhci
->lock
);
1022 temp
= readl(&xhci
->op_regs
->status
);
1025 /* If restore operation fails, re-initialize the HC during resume */
1026 if ((temp
& STS_SRE
) || hibernated
) {
1028 if ((xhci
->quirks
& XHCI_COMP_MODE_QUIRK
) &&
1029 !(xhci_all_ports_seen_u0(xhci
))) {
1030 del_timer_sync(&xhci
->comp_mode_recovery_timer
);
1031 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
1032 "Compliance Mode Recovery Timer deleted!");
1035 /* Let the USB core know _both_ roothubs lost power. */
1036 usb_root_hub_lost_power(xhci
->main_hcd
->self
.root_hub
);
1037 usb_root_hub_lost_power(xhci
->shared_hcd
->self
.root_hub
);
1039 xhci_dbg(xhci
, "Stop HCD\n");
1042 spin_unlock_irq(&xhci
->lock
);
1043 xhci_cleanup_msix(xhci
);
1045 xhci_dbg(xhci
, "// Disabling event ring interrupts\n");
1046 temp
= readl(&xhci
->op_regs
->status
);
1047 writel(temp
& ~STS_EINT
, &xhci
->op_regs
->status
);
1048 temp
= readl(&xhci
->ir_set
->irq_pending
);
1049 writel(ER_IRQ_DISABLE(temp
), &xhci
->ir_set
->irq_pending
);
1050 xhci_print_ir_set(xhci
, 0);
1052 xhci_dbg(xhci
, "cleaning up memory\n");
1053 xhci_mem_cleanup(xhci
);
1054 xhci_dbg(xhci
, "xhci_stop completed - status = %x\n",
1055 readl(&xhci
->op_regs
->status
));
1057 /* USB core calls the PCI reinit and start functions twice:
1058 * first with the primary HCD, and then with the secondary HCD.
1059 * If we don't do the same, the host will never be started.
1061 if (!usb_hcd_is_primary_hcd(hcd
))
1062 secondary_hcd
= hcd
;
1064 secondary_hcd
= xhci
->shared_hcd
;
1066 xhci_dbg(xhci
, "Initialize the xhci_hcd\n");
1067 retval
= xhci_init(hcd
->primary_hcd
);
1070 comp_timer_running
= true;
1072 xhci_dbg(xhci
, "Start the primary HCD\n");
1073 retval
= xhci_run(hcd
->primary_hcd
);
1075 xhci_dbg(xhci
, "Start the secondary HCD\n");
1076 retval
= xhci_run(secondary_hcd
);
1078 hcd
->state
= HC_STATE_SUSPENDED
;
1079 xhci
->shared_hcd
->state
= HC_STATE_SUSPENDED
;
1083 /* step 4: set Run/Stop bit */
1084 command
= readl(&xhci
->op_regs
->command
);
1086 writel(command
, &xhci
->op_regs
->command
);
1087 xhci_handshake(&xhci
->op_regs
->status
, STS_HALT
,
1090 /* step 5: walk topology and initialize portsc,
1091 * portpmsc and portli
1093 /* this is done in bus_resume */
1095 /* step 6: restart each of the previously
1096 * Running endpoints by ringing their doorbells
1099 spin_unlock_irq(&xhci
->lock
);
1103 /* Resume root hubs only when have pending events. */
1104 status
= readl(&xhci
->op_regs
->status
);
1105 if (status
& STS_EINT
) {
1106 usb_hcd_resume_root_hub(hcd
);
1107 usb_hcd_resume_root_hub(xhci
->shared_hcd
);
1112 * If system is subject to the Quirk, Compliance Mode Timer needs to
1113 * be re-initialized Always after a system resume. Ports are subject
1114 * to suffer the Compliance Mode issue again. It doesn't matter if
1115 * ports have entered previously to U0 before system's suspension.
1117 if ((xhci
->quirks
& XHCI_COMP_MODE_QUIRK
) && !comp_timer_running
)
1118 compliance_mode_recovery_timer_init(xhci
);
1120 /* Re-enable port polling. */
1121 xhci_dbg(xhci
, "%s: starting port polling.\n", __func__
);
1122 set_bit(HCD_FLAG_POLL_RH
, &hcd
->flags
);
1123 usb_hcd_poll_rh_status(hcd
);
1124 set_bit(HCD_FLAG_POLL_RH
, &xhci
->shared_hcd
->flags
);
1125 usb_hcd_poll_rh_status(xhci
->shared_hcd
);
1129 EXPORT_SYMBOL_GPL(xhci_resume
);
1130 #endif /* CONFIG_PM */
1132 /*-------------------------------------------------------------------------*/
1135 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1136 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1137 * value to right shift 1 for the bitmask.
1139 * Index = (epnum * 2) + direction - 1,
1140 * where direction = 0 for OUT, 1 for IN.
1141 * For control endpoints, the IN index is used (OUT index is unused), so
1142 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1144 unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor
*desc
)
1147 if (usb_endpoint_xfer_control(desc
))
1148 index
= (unsigned int) (usb_endpoint_num(desc
)*2);
1150 index
= (unsigned int) (usb_endpoint_num(desc
)*2) +
1151 (usb_endpoint_dir_in(desc
) ? 1 : 0) - 1;
1155 /* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1156 * address from the XHCI endpoint index.
1158 unsigned int xhci_get_endpoint_address(unsigned int ep_index
)
1160 unsigned int number
= DIV_ROUND_UP(ep_index
, 2);
1161 unsigned int direction
= ep_index
% 2 ? USB_DIR_OUT
: USB_DIR_IN
;
1162 return direction
| number
;
1165 /* Find the flag for this endpoint (for use in the control context). Use the
1166 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1169 unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor
*desc
)
1171 return 1 << (xhci_get_endpoint_index(desc
) + 1);
1174 /* Find the flag for this endpoint (for use in the control context). Use the
1175 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1178 unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index
)
1180 return 1 << (ep_index
+ 1);
1183 /* Compute the last valid endpoint context index. Basically, this is the
1184 * endpoint index plus one. For slot contexts with more than valid endpoint,
1185 * we find the most significant bit set in the added contexts flags.
1186 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1187 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1189 unsigned int xhci_last_valid_endpoint(u32 added_ctxs
)
1191 return fls(added_ctxs
) - 1;
1194 /* Returns 1 if the arguments are OK;
1195 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1197 static int xhci_check_args(struct usb_hcd
*hcd
, struct usb_device
*udev
,
1198 struct usb_host_endpoint
*ep
, int check_ep
, bool check_virt_dev
,
1200 struct xhci_hcd
*xhci
;
1201 struct xhci_virt_device
*virt_dev
;
1203 if (!hcd
|| (check_ep
&& !ep
) || !udev
) {
1204 pr_debug("xHCI %s called with invalid args\n", func
);
1207 if (!udev
->parent
) {
1208 pr_debug("xHCI %s called for root hub\n", func
);
1212 xhci
= hcd_to_xhci(hcd
);
1213 if (check_virt_dev
) {
1214 if (!udev
->slot_id
|| !xhci
->devs
[udev
->slot_id
]) {
1215 xhci_dbg(xhci
, "xHCI %s called with unaddressed device\n",
1220 virt_dev
= xhci
->devs
[udev
->slot_id
];
1221 if (virt_dev
->udev
!= udev
) {
1222 xhci_dbg(xhci
, "xHCI %s called with udev and "
1223 "virt_dev does not match\n", func
);
1228 if (xhci
->xhc_state
& XHCI_STATE_HALTED
)
1234 static int xhci_configure_endpoint(struct xhci_hcd
*xhci
,
1235 struct usb_device
*udev
, struct xhci_command
*command
,
1236 bool ctx_change
, bool must_succeed
);
1239 * Full speed devices may have a max packet size greater than 8 bytes, but the
1240 * USB core doesn't know that until it reads the first 8 bytes of the
1241 * descriptor. If the usb_device's max packet size changes after that point,
1242 * we need to issue an evaluate context command and wait on it.
1244 static int xhci_check_maxpacket(struct xhci_hcd
*xhci
, unsigned int slot_id
,
1245 unsigned int ep_index
, struct urb
*urb
)
1247 struct xhci_container_ctx
*out_ctx
;
1248 struct xhci_input_control_ctx
*ctrl_ctx
;
1249 struct xhci_ep_ctx
*ep_ctx
;
1250 struct xhci_command
*command
;
1251 int max_packet_size
;
1252 int hw_max_packet_size
;
1255 out_ctx
= xhci
->devs
[slot_id
]->out_ctx
;
1256 ep_ctx
= xhci_get_ep_ctx(xhci
, out_ctx
, ep_index
);
1257 hw_max_packet_size
= MAX_PACKET_DECODED(le32_to_cpu(ep_ctx
->ep_info2
));
1258 max_packet_size
= usb_endpoint_maxp(&urb
->dev
->ep0
.desc
);
1259 if (hw_max_packet_size
!= max_packet_size
) {
1260 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1261 "Max Packet Size for ep 0 changed.");
1262 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1263 "Max packet size in usb_device = %d",
1265 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1266 "Max packet size in xHCI HW = %d",
1267 hw_max_packet_size
);
1268 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1269 "Issuing evaluate context command.");
1271 /* Set up the input context flags for the command */
1272 /* FIXME: This won't work if a non-default control endpoint
1273 * changes max packet sizes.
1276 command
= xhci_alloc_command(xhci
, false, true, GFP_KERNEL
);
1280 command
->in_ctx
= xhci
->devs
[slot_id
]->in_ctx
;
1281 ctrl_ctx
= xhci_get_input_control_ctx(command
->in_ctx
);
1283 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
1286 goto command_cleanup
;
1288 /* Set up the modified control endpoint 0 */
1289 xhci_endpoint_copy(xhci
, xhci
->devs
[slot_id
]->in_ctx
,
1290 xhci
->devs
[slot_id
]->out_ctx
, ep_index
);
1292 ep_ctx
= xhci_get_ep_ctx(xhci
, command
->in_ctx
, ep_index
);
1293 ep_ctx
->ep_info2
&= cpu_to_le32(~MAX_PACKET_MASK
);
1294 ep_ctx
->ep_info2
|= cpu_to_le32(MAX_PACKET(max_packet_size
));
1296 ctrl_ctx
->add_flags
= cpu_to_le32(EP0_FLAG
);
1297 ctrl_ctx
->drop_flags
= 0;
1299 xhci_dbg(xhci
, "Slot %d input context\n", slot_id
);
1300 xhci_dbg_ctx(xhci
, command
->in_ctx
, ep_index
);
1301 xhci_dbg(xhci
, "Slot %d output context\n", slot_id
);
1302 xhci_dbg_ctx(xhci
, out_ctx
, ep_index
);
1304 ret
= xhci_configure_endpoint(xhci
, urb
->dev
, command
,
1307 /* Clean up the input context for later use by bandwidth
1310 ctrl_ctx
->add_flags
= cpu_to_le32(SLOT_FLAG
);
1312 kfree(command
->completion
);
1319 * non-error returns are a promise to giveback() the urb later
1320 * we drop ownership so next owner (or urb unlink) can get it
1322 int xhci_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
, gfp_t mem_flags
)
1324 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
1325 struct xhci_td
*buffer
;
1326 unsigned long flags
;
1328 unsigned int slot_id
, ep_index
;
1329 struct urb_priv
*urb_priv
;
1332 if (!urb
|| xhci_check_args(hcd
, urb
->dev
, urb
->ep
,
1333 true, true, __func__
) <= 0)
1336 slot_id
= urb
->dev
->slot_id
;
1337 ep_index
= xhci_get_endpoint_index(&urb
->ep
->desc
);
1339 if (!HCD_HW_ACCESSIBLE(hcd
)) {
1340 if (!in_interrupt())
1341 xhci_dbg(xhci
, "urb submitted during PCI suspend\n");
1346 if (usb_endpoint_xfer_isoc(&urb
->ep
->desc
))
1347 size
= urb
->number_of_packets
;
1348 else if (usb_endpoint_is_bulk_out(&urb
->ep
->desc
) &&
1349 urb
->transfer_buffer_length
> 0 &&
1350 urb
->transfer_flags
& URB_ZERO_PACKET
&&
1351 !(urb
->transfer_buffer_length
% usb_endpoint_maxp(&urb
->ep
->desc
)))
1356 urb_priv
= kzalloc(sizeof(struct urb_priv
) +
1357 size
* sizeof(struct xhci_td
*), mem_flags
);
1361 buffer
= kzalloc(size
* sizeof(struct xhci_td
), mem_flags
);
1367 for (i
= 0; i
< size
; i
++) {
1368 urb_priv
->td
[i
] = buffer
;
1372 urb_priv
->length
= size
;
1373 urb_priv
->td_cnt
= 0;
1374 urb
->hcpriv
= urb_priv
;
1376 if (usb_endpoint_xfer_control(&urb
->ep
->desc
)) {
1377 /* Check to see if the max packet size for the default control
1378 * endpoint changed during FS device enumeration
1380 if (urb
->dev
->speed
== USB_SPEED_FULL
) {
1381 ret
= xhci_check_maxpacket(xhci
, slot_id
,
1384 xhci_urb_free_priv(urb_priv
);
1390 /* We have a spinlock and interrupts disabled, so we must pass
1391 * atomic context to this function, which may allocate memory.
1393 spin_lock_irqsave(&xhci
->lock
, flags
);
1394 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1396 ret
= xhci_queue_ctrl_tx(xhci
, GFP_ATOMIC
, urb
,
1400 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1401 } else if (usb_endpoint_xfer_bulk(&urb
->ep
->desc
)) {
1402 spin_lock_irqsave(&xhci
->lock
, flags
);
1403 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1405 if (xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
&
1406 EP_GETTING_STREAMS
) {
1407 xhci_warn(xhci
, "WARN: Can't enqueue URB while bulk ep "
1408 "is transitioning to using streams.\n");
1410 } else if (xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
&
1411 EP_GETTING_NO_STREAMS
) {
1412 xhci_warn(xhci
, "WARN: Can't enqueue URB while bulk ep "
1413 "is transitioning to "
1414 "not having streams.\n");
1417 ret
= xhci_queue_bulk_tx(xhci
, GFP_ATOMIC
, urb
,
1422 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1423 } else if (usb_endpoint_xfer_int(&urb
->ep
->desc
)) {
1424 spin_lock_irqsave(&xhci
->lock
, flags
);
1425 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1427 ret
= xhci_queue_intr_tx(xhci
, GFP_ATOMIC
, urb
,
1431 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1433 spin_lock_irqsave(&xhci
->lock
, flags
);
1434 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1436 ret
= xhci_queue_isoc_tx_prepare(xhci
, GFP_ATOMIC
, urb
,
1440 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1445 xhci_dbg(xhci
, "Ep 0x%x: URB %p submitted for "
1446 "non-responsive xHCI host.\n",
1447 urb
->ep
->desc
.bEndpointAddress
, urb
);
1450 xhci_urb_free_priv(urb_priv
);
1452 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1456 /* Get the right ring for the given URB.
1457 * If the endpoint supports streams, boundary check the URB's stream ID.
1458 * If the endpoint doesn't support streams, return the singular endpoint ring.
1460 static struct xhci_ring
*xhci_urb_to_transfer_ring(struct xhci_hcd
*xhci
,
1463 unsigned int slot_id
;
1464 unsigned int ep_index
;
1465 unsigned int stream_id
;
1466 struct xhci_virt_ep
*ep
;
1468 slot_id
= urb
->dev
->slot_id
;
1469 ep_index
= xhci_get_endpoint_index(&urb
->ep
->desc
);
1470 stream_id
= urb
->stream_id
;
1471 ep
= &xhci
->devs
[slot_id
]->eps
[ep_index
];
1472 /* Common case: no streams */
1473 if (!(ep
->ep_state
& EP_HAS_STREAMS
))
1476 if (stream_id
== 0) {
1478 "WARN: Slot ID %u, ep index %u has streams, "
1479 "but URB has no stream ID.\n",
1484 if (stream_id
< ep
->stream_info
->num_streams
)
1485 return ep
->stream_info
->stream_rings
[stream_id
];
1488 "WARN: Slot ID %u, ep index %u has "
1489 "stream IDs 1 to %u allocated, "
1490 "but stream ID %u is requested.\n",
1492 ep
->stream_info
->num_streams
- 1,
1498 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1499 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1500 * should pick up where it left off in the TD, unless a Set Transfer Ring
1501 * Dequeue Pointer is issued.
1503 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1504 * the ring. Since the ring is a contiguous structure, they can't be physically
1505 * removed. Instead, there are two options:
1507 * 1) If the HC is in the middle of processing the URB to be canceled, we
1508 * simply move the ring's dequeue pointer past those TRBs using the Set
1509 * Transfer Ring Dequeue Pointer command. This will be the common case,
1510 * when drivers timeout on the last submitted URB and attempt to cancel.
1512 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1513 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1514 * HC will need to invalidate the any TRBs it has cached after the stop
1515 * endpoint command, as noted in the xHCI 0.95 errata.
1517 * 3) The TD may have completed by the time the Stop Endpoint Command
1518 * completes, so software needs to handle that case too.
1520 * This function should protect against the TD enqueueing code ringing the
1521 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1522 * It also needs to account for multiple cancellations on happening at the same
1523 * time for the same endpoint.
1525 * Note that this function can be called in any context, or so says
1526 * usb_hcd_unlink_urb()
1528 int xhci_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1530 unsigned long flags
;
1533 struct xhci_hcd
*xhci
;
1534 struct urb_priv
*urb_priv
;
1536 unsigned int ep_index
;
1537 struct xhci_ring
*ep_ring
;
1538 struct xhci_virt_ep
*ep
;
1539 struct xhci_command
*command
;
1541 xhci
= hcd_to_xhci(hcd
);
1542 spin_lock_irqsave(&xhci
->lock
, flags
);
1543 /* Make sure the URB hasn't completed or been unlinked already */
1544 ret
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1545 if (ret
|| !urb
->hcpriv
)
1547 temp
= readl(&xhci
->op_regs
->status
);
1548 if (temp
== 0xffffffff || (xhci
->xhc_state
& XHCI_STATE_HALTED
)) {
1549 xhci_dbg_trace(xhci
, trace_xhci_dbg_cancel_urb
,
1550 "HW died, freeing TD.");
1551 urb_priv
= urb
->hcpriv
;
1552 for (i
= urb_priv
->td_cnt
; i
< urb_priv
->length
; i
++) {
1553 td
= urb_priv
->td
[i
];
1554 if (!list_empty(&td
->td_list
))
1555 list_del_init(&td
->td_list
);
1556 if (!list_empty(&td
->cancelled_td_list
))
1557 list_del_init(&td
->cancelled_td_list
);
1560 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
1561 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1562 usb_hcd_giveback_urb(hcd
, urb
, -ESHUTDOWN
);
1563 xhci_urb_free_priv(urb_priv
);
1566 if ((xhci
->xhc_state
& XHCI_STATE_DYING
) ||
1567 (xhci
->xhc_state
& XHCI_STATE_HALTED
)) {
1568 xhci_dbg_trace(xhci
, trace_xhci_dbg_cancel_urb
,
1569 "Ep 0x%x: URB %p to be canceled on "
1570 "non-responsive xHCI host.",
1571 urb
->ep
->desc
.bEndpointAddress
, urb
);
1572 /* Let the stop endpoint command watchdog timer (which set this
1573 * state) finish cleaning up the endpoint TD lists. We must
1574 * have caught it in the middle of dropping a lock and giving
1580 ep_index
= xhci_get_endpoint_index(&urb
->ep
->desc
);
1581 ep
= &xhci
->devs
[urb
->dev
->slot_id
]->eps
[ep_index
];
1582 ep_ring
= xhci_urb_to_transfer_ring(xhci
, urb
);
1588 urb_priv
= urb
->hcpriv
;
1589 i
= urb_priv
->td_cnt
;
1590 if (i
< urb_priv
->length
)
1591 xhci_dbg_trace(xhci
, trace_xhci_dbg_cancel_urb
,
1592 "Cancel URB %p, dev %s, ep 0x%x, "
1593 "starting at offset 0x%llx",
1594 urb
, urb
->dev
->devpath
,
1595 urb
->ep
->desc
.bEndpointAddress
,
1596 (unsigned long long) xhci_trb_virt_to_dma(
1597 urb_priv
->td
[i
]->start_seg
,
1598 urb_priv
->td
[i
]->first_trb
));
1600 for (; i
< urb_priv
->length
; i
++) {
1601 td
= urb_priv
->td
[i
];
1602 list_add_tail(&td
->cancelled_td_list
, &ep
->cancelled_td_list
);
1605 /* Queue a stop endpoint command, but only if this is
1606 * the first cancellation to be handled.
1608 if (!(ep
->ep_state
& EP_HALT_PENDING
)) {
1609 command
= xhci_alloc_command(xhci
, false, false, GFP_ATOMIC
);
1614 ep
->ep_state
|= EP_HALT_PENDING
;
1615 ep
->stop_cmds_pending
++;
1616 ep
->stop_cmd_timer
.expires
= jiffies
+
1617 XHCI_STOP_EP_CMD_TIMEOUT
* HZ
;
1618 add_timer(&ep
->stop_cmd_timer
);
1619 xhci_queue_stop_endpoint(xhci
, command
, urb
->dev
->slot_id
,
1621 xhci_ring_cmd_db(xhci
);
1624 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1628 /* Drop an endpoint from a new bandwidth configuration for this device.
1629 * Only one call to this function is allowed per endpoint before
1630 * check_bandwidth() or reset_bandwidth() must be called.
1631 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1632 * add the endpoint to the schedule with possibly new parameters denoted by a
1633 * different endpoint descriptor in usb_host_endpoint.
1634 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1637 * The USB core will not allow URBs to be queued to an endpoint that is being
1638 * disabled, so there's no need for mutual exclusion to protect
1639 * the xhci->devs[slot_id] structure.
1641 int xhci_drop_endpoint(struct usb_hcd
*hcd
, struct usb_device
*udev
,
1642 struct usb_host_endpoint
*ep
)
1644 struct xhci_hcd
*xhci
;
1645 struct xhci_container_ctx
*in_ctx
, *out_ctx
;
1646 struct xhci_input_control_ctx
*ctrl_ctx
;
1647 unsigned int ep_index
;
1648 struct xhci_ep_ctx
*ep_ctx
;
1650 u32 new_add_flags
, new_drop_flags
;
1653 ret
= xhci_check_args(hcd
, udev
, ep
, 1, true, __func__
);
1656 xhci
= hcd_to_xhci(hcd
);
1657 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1660 xhci_dbg(xhci
, "%s called for udev %p\n", __func__
, udev
);
1661 drop_flag
= xhci_get_endpoint_flag(&ep
->desc
);
1662 if (drop_flag
== SLOT_FLAG
|| drop_flag
== EP0_FLAG
) {
1663 xhci_dbg(xhci
, "xHCI %s - can't drop slot or ep 0 %#x\n",
1664 __func__
, drop_flag
);
1668 in_ctx
= xhci
->devs
[udev
->slot_id
]->in_ctx
;
1669 out_ctx
= xhci
->devs
[udev
->slot_id
]->out_ctx
;
1670 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
1672 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
1677 ep_index
= xhci_get_endpoint_index(&ep
->desc
);
1678 ep_ctx
= xhci_get_ep_ctx(xhci
, out_ctx
, ep_index
);
1679 /* If the HC already knows the endpoint is disabled,
1680 * or the HCD has noted it is disabled, ignore this request
1682 if (((ep_ctx
->ep_info
& cpu_to_le32(EP_STATE_MASK
)) ==
1683 cpu_to_le32(EP_STATE_DISABLED
)) ||
1684 le32_to_cpu(ctrl_ctx
->drop_flags
) &
1685 xhci_get_endpoint_flag(&ep
->desc
)) {
1686 /* Do not warn when called after a usb_device_reset */
1687 if (xhci
->devs
[udev
->slot_id
]->eps
[ep_index
].ring
!= NULL
)
1688 xhci_warn(xhci
, "xHCI %s called with disabled ep %p\n",
1693 ctrl_ctx
->drop_flags
|= cpu_to_le32(drop_flag
);
1694 new_drop_flags
= le32_to_cpu(ctrl_ctx
->drop_flags
);
1696 ctrl_ctx
->add_flags
&= cpu_to_le32(~drop_flag
);
1697 new_add_flags
= le32_to_cpu(ctrl_ctx
->add_flags
);
1699 xhci_endpoint_zero(xhci
, xhci
->devs
[udev
->slot_id
], ep
);
1701 xhci_dbg(xhci
, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
1702 (unsigned int) ep
->desc
.bEndpointAddress
,
1704 (unsigned int) new_drop_flags
,
1705 (unsigned int) new_add_flags
);
1709 /* Add an endpoint to a new possible bandwidth configuration for this device.
1710 * Only one call to this function is allowed per endpoint before
1711 * check_bandwidth() or reset_bandwidth() must be called.
1712 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1713 * add the endpoint to the schedule with possibly new parameters denoted by a
1714 * different endpoint descriptor in usb_host_endpoint.
1715 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1718 * The USB core will not allow URBs to be queued to an endpoint until the
1719 * configuration or alt setting is installed in the device, so there's no need
1720 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1722 int xhci_add_endpoint(struct usb_hcd
*hcd
, struct usb_device
*udev
,
1723 struct usb_host_endpoint
*ep
)
1725 struct xhci_hcd
*xhci
;
1726 struct xhci_container_ctx
*in_ctx
;
1727 unsigned int ep_index
;
1728 struct xhci_input_control_ctx
*ctrl_ctx
;
1730 u32 new_add_flags
, new_drop_flags
;
1731 struct xhci_virt_device
*virt_dev
;
1734 ret
= xhci_check_args(hcd
, udev
, ep
, 1, true, __func__
);
1736 /* So we won't queue a reset ep command for a root hub */
1740 xhci
= hcd_to_xhci(hcd
);
1741 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1744 added_ctxs
= xhci_get_endpoint_flag(&ep
->desc
);
1745 if (added_ctxs
== SLOT_FLAG
|| added_ctxs
== EP0_FLAG
) {
1746 /* FIXME when we have to issue an evaluate endpoint command to
1747 * deal with ep0 max packet size changing once we get the
1750 xhci_dbg(xhci
, "xHCI %s - can't add slot or ep 0 %#x\n",
1751 __func__
, added_ctxs
);
1755 virt_dev
= xhci
->devs
[udev
->slot_id
];
1756 in_ctx
= virt_dev
->in_ctx
;
1757 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
1759 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
1764 ep_index
= xhci_get_endpoint_index(&ep
->desc
);
1765 /* If this endpoint is already in use, and the upper layers are trying
1766 * to add it again without dropping it, reject the addition.
1768 if (virt_dev
->eps
[ep_index
].ring
&&
1769 !(le32_to_cpu(ctrl_ctx
->drop_flags
) & added_ctxs
)) {
1770 xhci_warn(xhci
, "Trying to add endpoint 0x%x "
1771 "without dropping it.\n",
1772 (unsigned int) ep
->desc
.bEndpointAddress
);
1776 /* If the HCD has already noted the endpoint is enabled,
1777 * ignore this request.
1779 if (le32_to_cpu(ctrl_ctx
->add_flags
) & added_ctxs
) {
1780 xhci_warn(xhci
, "xHCI %s called with enabled ep %p\n",
1786 * Configuration and alternate setting changes must be done in
1787 * process context, not interrupt context (or so documenation
1788 * for usb_set_interface() and usb_set_configuration() claim).
1790 if (xhci_endpoint_init(xhci
, virt_dev
, udev
, ep
, GFP_NOIO
) < 0) {
1791 dev_dbg(&udev
->dev
, "%s - could not initialize ep %#x\n",
1792 __func__
, ep
->desc
.bEndpointAddress
);
1796 ctrl_ctx
->add_flags
|= cpu_to_le32(added_ctxs
);
1797 new_add_flags
= le32_to_cpu(ctrl_ctx
->add_flags
);
1799 /* If xhci_endpoint_disable() was called for this endpoint, but the
1800 * xHC hasn't been notified yet through the check_bandwidth() call,
1801 * this re-adds a new state for the endpoint from the new endpoint
1802 * descriptors. We must drop and re-add this endpoint, so we leave the
1805 new_drop_flags
= le32_to_cpu(ctrl_ctx
->drop_flags
);
1807 /* Store the usb_device pointer for later use */
1810 xhci_dbg(xhci
, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
1811 (unsigned int) ep
->desc
.bEndpointAddress
,
1813 (unsigned int) new_drop_flags
,
1814 (unsigned int) new_add_flags
);
1818 static void xhci_zero_in_ctx(struct xhci_hcd
*xhci
, struct xhci_virt_device
*virt_dev
)
1820 struct xhci_input_control_ctx
*ctrl_ctx
;
1821 struct xhci_ep_ctx
*ep_ctx
;
1822 struct xhci_slot_ctx
*slot_ctx
;
1825 ctrl_ctx
= xhci_get_input_control_ctx(virt_dev
->in_ctx
);
1827 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
1832 /* When a device's add flag and drop flag are zero, any subsequent
1833 * configure endpoint command will leave that endpoint's state
1834 * untouched. Make sure we don't leave any old state in the input
1835 * endpoint contexts.
1837 ctrl_ctx
->drop_flags
= 0;
1838 ctrl_ctx
->add_flags
= 0;
1839 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->in_ctx
);
1840 slot_ctx
->dev_info
&= cpu_to_le32(~LAST_CTX_MASK
);
1841 /* Endpoint 0 is always valid */
1842 slot_ctx
->dev_info
|= cpu_to_le32(LAST_CTX(1));
1843 for (i
= 1; i
< 31; ++i
) {
1844 ep_ctx
= xhci_get_ep_ctx(xhci
, virt_dev
->in_ctx
, i
);
1845 ep_ctx
->ep_info
= 0;
1846 ep_ctx
->ep_info2
= 0;
1848 ep_ctx
->tx_info
= 0;
1852 static int xhci_configure_endpoint_result(struct xhci_hcd
*xhci
,
1853 struct usb_device
*udev
, u32
*cmd_status
)
1857 switch (*cmd_status
) {
1858 case COMP_CMD_ABORT
:
1860 xhci_warn(xhci
, "Timeout while waiting for configure endpoint command\n");
1864 dev_warn(&udev
->dev
,
1865 "Not enough host controller resources for new device state.\n");
1867 /* FIXME: can we allocate more resources for the HC? */
1870 case COMP_2ND_BW_ERR
:
1871 dev_warn(&udev
->dev
,
1872 "Not enough bandwidth for new device state.\n");
1874 /* FIXME: can we go back to the old state? */
1877 /* the HCD set up something wrong */
1878 dev_warn(&udev
->dev
, "ERROR: Endpoint drop flag = 0, "
1880 "and endpoint is not disabled.\n");
1884 dev_warn(&udev
->dev
,
1885 "ERROR: Incompatible device for endpoint configure command.\n");
1889 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1890 "Successful Endpoint Configure command");
1894 xhci_err(xhci
, "ERROR: unexpected command completion code 0x%x.\n",
1902 static int xhci_evaluate_context_result(struct xhci_hcd
*xhci
,
1903 struct usb_device
*udev
, u32
*cmd_status
)
1906 struct xhci_virt_device
*virt_dev
= xhci
->devs
[udev
->slot_id
];
1908 switch (*cmd_status
) {
1909 case COMP_CMD_ABORT
:
1911 xhci_warn(xhci
, "Timeout while waiting for evaluate context command\n");
1915 dev_warn(&udev
->dev
,
1916 "WARN: xHCI driver setup invalid evaluate context command.\n");
1920 dev_warn(&udev
->dev
,
1921 "WARN: slot not enabled for evaluate context command.\n");
1924 case COMP_CTX_STATE
:
1925 dev_warn(&udev
->dev
,
1926 "WARN: invalid context state for evaluate context command.\n");
1927 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 1);
1931 dev_warn(&udev
->dev
,
1932 "ERROR: Incompatible device for evaluate context command.\n");
1936 /* Max Exit Latency too large error */
1937 dev_warn(&udev
->dev
, "WARN: Max Exit Latency too large\n");
1941 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1942 "Successful evaluate context command");
1946 xhci_err(xhci
, "ERROR: unexpected command completion code 0x%x.\n",
1954 static u32
xhci_count_num_new_endpoints(struct xhci_hcd
*xhci
,
1955 struct xhci_input_control_ctx
*ctrl_ctx
)
1957 u32 valid_add_flags
;
1958 u32 valid_drop_flags
;
1960 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1961 * (bit 1). The default control endpoint is added during the Address
1962 * Device command and is never removed until the slot is disabled.
1964 valid_add_flags
= le32_to_cpu(ctrl_ctx
->add_flags
) >> 2;
1965 valid_drop_flags
= le32_to_cpu(ctrl_ctx
->drop_flags
) >> 2;
1967 /* Use hweight32 to count the number of ones in the add flags, or
1968 * number of endpoints added. Don't count endpoints that are changed
1969 * (both added and dropped).
1971 return hweight32(valid_add_flags
) -
1972 hweight32(valid_add_flags
& valid_drop_flags
);
1975 static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd
*xhci
,
1976 struct xhci_input_control_ctx
*ctrl_ctx
)
1978 u32 valid_add_flags
;
1979 u32 valid_drop_flags
;
1981 valid_add_flags
= le32_to_cpu(ctrl_ctx
->add_flags
) >> 2;
1982 valid_drop_flags
= le32_to_cpu(ctrl_ctx
->drop_flags
) >> 2;
1984 return hweight32(valid_drop_flags
) -
1985 hweight32(valid_add_flags
& valid_drop_flags
);
1989 * We need to reserve the new number of endpoints before the configure endpoint
1990 * command completes. We can't subtract the dropped endpoints from the number
1991 * of active endpoints until the command completes because we can oversubscribe
1992 * the host in this case:
1994 * - the first configure endpoint command drops more endpoints than it adds
1995 * - a second configure endpoint command that adds more endpoints is queued
1996 * - the first configure endpoint command fails, so the config is unchanged
1997 * - the second command may succeed, even though there isn't enough resources
1999 * Must be called with xhci->lock held.
2001 static int xhci_reserve_host_resources(struct xhci_hcd
*xhci
,
2002 struct xhci_input_control_ctx
*ctrl_ctx
)
2006 added_eps
= xhci_count_num_new_endpoints(xhci
, ctrl_ctx
);
2007 if (xhci
->num_active_eps
+ added_eps
> xhci
->limit_active_eps
) {
2008 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2009 "Not enough ep ctxs: "
2010 "%u active, need to add %u, limit is %u.",
2011 xhci
->num_active_eps
, added_eps
,
2012 xhci
->limit_active_eps
);
2015 xhci
->num_active_eps
+= added_eps
;
2016 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2017 "Adding %u ep ctxs, %u now active.", added_eps
,
2018 xhci
->num_active_eps
);
2023 * The configure endpoint was failed by the xHC for some other reason, so we
2024 * need to revert the resources that failed configuration would have used.
2026 * Must be called with xhci->lock held.
2028 static void xhci_free_host_resources(struct xhci_hcd
*xhci
,
2029 struct xhci_input_control_ctx
*ctrl_ctx
)
2033 num_failed_eps
= xhci_count_num_new_endpoints(xhci
, ctrl_ctx
);
2034 xhci
->num_active_eps
-= num_failed_eps
;
2035 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2036 "Removing %u failed ep ctxs, %u now active.",
2038 xhci
->num_active_eps
);
2042 * Now that the command has completed, clean up the active endpoint count by
2043 * subtracting out the endpoints that were dropped (but not changed).
2045 * Must be called with xhci->lock held.
2047 static void xhci_finish_resource_reservation(struct xhci_hcd
*xhci
,
2048 struct xhci_input_control_ctx
*ctrl_ctx
)
2050 u32 num_dropped_eps
;
2052 num_dropped_eps
= xhci_count_num_dropped_endpoints(xhci
, ctrl_ctx
);
2053 xhci
->num_active_eps
-= num_dropped_eps
;
2054 if (num_dropped_eps
)
2055 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2056 "Removing %u dropped ep ctxs, %u now active.",
2058 xhci
->num_active_eps
);
2061 static unsigned int xhci_get_block_size(struct usb_device
*udev
)
2063 switch (udev
->speed
) {
2065 case USB_SPEED_FULL
:
2067 case USB_SPEED_HIGH
:
2069 case USB_SPEED_SUPER
:
2071 case USB_SPEED_UNKNOWN
:
2072 case USB_SPEED_WIRELESS
:
2074 /* Should never happen */
2080 xhci_get_largest_overhead(struct xhci_interval_bw
*interval_bw
)
2082 if (interval_bw
->overhead
[LS_OVERHEAD_TYPE
])
2084 if (interval_bw
->overhead
[FS_OVERHEAD_TYPE
])
2089 /* If we are changing a LS/FS device under a HS hub,
2090 * make sure (if we are activating a new TT) that the HS bus has enough
2091 * bandwidth for this new TT.
2093 static int xhci_check_tt_bw_table(struct xhci_hcd
*xhci
,
2094 struct xhci_virt_device
*virt_dev
,
2097 struct xhci_interval_bw_table
*bw_table
;
2098 struct xhci_tt_bw_info
*tt_info
;
2100 /* Find the bandwidth table for the root port this TT is attached to. */
2101 bw_table
= &xhci
->rh_bw
[virt_dev
->real_port
- 1].bw_table
;
2102 tt_info
= virt_dev
->tt_info
;
2103 /* If this TT already had active endpoints, the bandwidth for this TT
2104 * has already been added. Removing all periodic endpoints (and thus
2105 * making the TT enactive) will only decrease the bandwidth used.
2109 if (old_active_eps
== 0 && tt_info
->active_eps
!= 0) {
2110 if (bw_table
->bw_used
+ TT_HS_OVERHEAD
> HS_BW_LIMIT
)
2114 /* Not sure why we would have no new active endpoints...
2116 * Maybe because of an Evaluate Context change for a hub update or a
2117 * control endpoint 0 max packet size change?
2118 * FIXME: skip the bandwidth calculation in that case.
2123 static int xhci_check_ss_bw(struct xhci_hcd
*xhci
,
2124 struct xhci_virt_device
*virt_dev
)
2126 unsigned int bw_reserved
;
2128 bw_reserved
= DIV_ROUND_UP(SS_BW_RESERVED
*SS_BW_LIMIT_IN
, 100);
2129 if (virt_dev
->bw_table
->ss_bw_in
> (SS_BW_LIMIT_IN
- bw_reserved
))
2132 bw_reserved
= DIV_ROUND_UP(SS_BW_RESERVED
*SS_BW_LIMIT_OUT
, 100);
2133 if (virt_dev
->bw_table
->ss_bw_out
> (SS_BW_LIMIT_OUT
- bw_reserved
))
2140 * This algorithm is a very conservative estimate of the worst-case scheduling
2141 * scenario for any one interval. The hardware dynamically schedules the
2142 * packets, so we can't tell which microframe could be the limiting factor in
2143 * the bandwidth scheduling. This only takes into account periodic endpoints.
2145 * Obviously, we can't solve an NP complete problem to find the minimum worst
2146 * case scenario. Instead, we come up with an estimate that is no less than
2147 * the worst case bandwidth used for any one microframe, but may be an
2150 * We walk the requirements for each endpoint by interval, starting with the
2151 * smallest interval, and place packets in the schedule where there is only one
2152 * possible way to schedule packets for that interval. In order to simplify
2153 * this algorithm, we record the largest max packet size for each interval, and
2154 * assume all packets will be that size.
2156 * For interval 0, we obviously must schedule all packets for each interval.
2157 * The bandwidth for interval 0 is just the amount of data to be transmitted
2158 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2159 * the number of packets).
2161 * For interval 1, we have two possible microframes to schedule those packets
2162 * in. For this algorithm, if we can schedule the same number of packets for
2163 * each possible scheduling opportunity (each microframe), we will do so. The
2164 * remaining number of packets will be saved to be transmitted in the gaps in
2165 * the next interval's scheduling sequence.
2167 * As we move those remaining packets to be scheduled with interval 2 packets,
2168 * we have to double the number of remaining packets to transmit. This is
2169 * because the intervals are actually powers of 2, and we would be transmitting
2170 * the previous interval's packets twice in this interval. We also have to be
2171 * sure that when we look at the largest max packet size for this interval, we
2172 * also look at the largest max packet size for the remaining packets and take
2173 * the greater of the two.
2175 * The algorithm continues to evenly distribute packets in each scheduling
2176 * opportunity, and push the remaining packets out, until we get to the last
2177 * interval. Then those packets and their associated overhead are just added
2178 * to the bandwidth used.
2180 static int xhci_check_bw_table(struct xhci_hcd
*xhci
,
2181 struct xhci_virt_device
*virt_dev
,
2184 unsigned int bw_reserved
;
2185 unsigned int max_bandwidth
;
2186 unsigned int bw_used
;
2187 unsigned int block_size
;
2188 struct xhci_interval_bw_table
*bw_table
;
2189 unsigned int packet_size
= 0;
2190 unsigned int overhead
= 0;
2191 unsigned int packets_transmitted
= 0;
2192 unsigned int packets_remaining
= 0;
2195 if (virt_dev
->udev
->speed
== USB_SPEED_SUPER
)
2196 return xhci_check_ss_bw(xhci
, virt_dev
);
2198 if (virt_dev
->udev
->speed
== USB_SPEED_HIGH
) {
2199 max_bandwidth
= HS_BW_LIMIT
;
2200 /* Convert percent of bus BW reserved to blocks reserved */
2201 bw_reserved
= DIV_ROUND_UP(HS_BW_RESERVED
* max_bandwidth
, 100);
2203 max_bandwidth
= FS_BW_LIMIT
;
2204 bw_reserved
= DIV_ROUND_UP(FS_BW_RESERVED
* max_bandwidth
, 100);
2207 bw_table
= virt_dev
->bw_table
;
2208 /* We need to translate the max packet size and max ESIT payloads into
2209 * the units the hardware uses.
2211 block_size
= xhci_get_block_size(virt_dev
->udev
);
2213 /* If we are manipulating a LS/FS device under a HS hub, double check
2214 * that the HS bus has enough bandwidth if we are activing a new TT.
2216 if (virt_dev
->tt_info
) {
2217 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2218 "Recalculating BW for rootport %u",
2219 virt_dev
->real_port
);
2220 if (xhci_check_tt_bw_table(xhci
, virt_dev
, old_active_eps
)) {
2221 xhci_warn(xhci
, "Not enough bandwidth on HS bus for "
2222 "newly activated TT.\n");
2225 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2226 "Recalculating BW for TT slot %u port %u",
2227 virt_dev
->tt_info
->slot_id
,
2228 virt_dev
->tt_info
->ttport
);
2230 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2231 "Recalculating BW for rootport %u",
2232 virt_dev
->real_port
);
2235 /* Add in how much bandwidth will be used for interval zero, or the
2236 * rounded max ESIT payload + number of packets * largest overhead.
2238 bw_used
= DIV_ROUND_UP(bw_table
->interval0_esit_payload
, block_size
) +
2239 bw_table
->interval_bw
[0].num_packets
*
2240 xhci_get_largest_overhead(&bw_table
->interval_bw
[0]);
2242 for (i
= 1; i
< XHCI_MAX_INTERVAL
; i
++) {
2243 unsigned int bw_added
;
2244 unsigned int largest_mps
;
2245 unsigned int interval_overhead
;
2248 * How many packets could we transmit in this interval?
2249 * If packets didn't fit in the previous interval, we will need
2250 * to transmit that many packets twice within this interval.
2252 packets_remaining
= 2 * packets_remaining
+
2253 bw_table
->interval_bw
[i
].num_packets
;
2255 /* Find the largest max packet size of this or the previous
2258 if (list_empty(&bw_table
->interval_bw
[i
].endpoints
))
2261 struct xhci_virt_ep
*virt_ep
;
2262 struct list_head
*ep_entry
;
2264 ep_entry
= bw_table
->interval_bw
[i
].endpoints
.next
;
2265 virt_ep
= list_entry(ep_entry
,
2266 struct xhci_virt_ep
, bw_endpoint_list
);
2267 /* Convert to blocks, rounding up */
2268 largest_mps
= DIV_ROUND_UP(
2269 virt_ep
->bw_info
.max_packet_size
,
2272 if (largest_mps
> packet_size
)
2273 packet_size
= largest_mps
;
2275 /* Use the larger overhead of this or the previous interval. */
2276 interval_overhead
= xhci_get_largest_overhead(
2277 &bw_table
->interval_bw
[i
]);
2278 if (interval_overhead
> overhead
)
2279 overhead
= interval_overhead
;
2281 /* How many packets can we evenly distribute across
2282 * (1 << (i + 1)) possible scheduling opportunities?
2284 packets_transmitted
= packets_remaining
>> (i
+ 1);
2286 /* Add in the bandwidth used for those scheduled packets */
2287 bw_added
= packets_transmitted
* (overhead
+ packet_size
);
2289 /* How many packets do we have remaining to transmit? */
2290 packets_remaining
= packets_remaining
% (1 << (i
+ 1));
2292 /* What largest max packet size should those packets have? */
2293 /* If we've transmitted all packets, don't carry over the
2294 * largest packet size.
2296 if (packets_remaining
== 0) {
2299 } else if (packets_transmitted
> 0) {
2300 /* Otherwise if we do have remaining packets, and we've
2301 * scheduled some packets in this interval, take the
2302 * largest max packet size from endpoints with this
2305 packet_size
= largest_mps
;
2306 overhead
= interval_overhead
;
2308 /* Otherwise carry over packet_size and overhead from the last
2309 * time we had a remainder.
2311 bw_used
+= bw_added
;
2312 if (bw_used
> max_bandwidth
) {
2313 xhci_warn(xhci
, "Not enough bandwidth. "
2314 "Proposed: %u, Max: %u\n",
2315 bw_used
, max_bandwidth
);
2320 * Ok, we know we have some packets left over after even-handedly
2321 * scheduling interval 15. We don't know which microframes they will
2322 * fit into, so we over-schedule and say they will be scheduled every
2325 if (packets_remaining
> 0)
2326 bw_used
+= overhead
+ packet_size
;
2328 if (!virt_dev
->tt_info
&& virt_dev
->udev
->speed
== USB_SPEED_HIGH
) {
2329 unsigned int port_index
= virt_dev
->real_port
- 1;
2331 /* OK, we're manipulating a HS device attached to a
2332 * root port bandwidth domain. Include the number of active TTs
2333 * in the bandwidth used.
2335 bw_used
+= TT_HS_OVERHEAD
*
2336 xhci
->rh_bw
[port_index
].num_active_tts
;
2339 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2340 "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2341 "Available: %u " "percent",
2342 bw_used
, max_bandwidth
, bw_reserved
,
2343 (max_bandwidth
- bw_used
- bw_reserved
) * 100 /
2346 bw_used
+= bw_reserved
;
2347 if (bw_used
> max_bandwidth
) {
2348 xhci_warn(xhci
, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2349 bw_used
, max_bandwidth
);
2353 bw_table
->bw_used
= bw_used
;
2357 static bool xhci_is_async_ep(unsigned int ep_type
)
2359 return (ep_type
!= ISOC_OUT_EP
&& ep_type
!= INT_OUT_EP
&&
2360 ep_type
!= ISOC_IN_EP
&&
2361 ep_type
!= INT_IN_EP
);
2364 static bool xhci_is_sync_in_ep(unsigned int ep_type
)
2366 return (ep_type
== ISOC_IN_EP
|| ep_type
== INT_IN_EP
);
2369 static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info
*ep_bw
)
2371 unsigned int mps
= DIV_ROUND_UP(ep_bw
->max_packet_size
, SS_BLOCK
);
2373 if (ep_bw
->ep_interval
== 0)
2374 return SS_OVERHEAD_BURST
+
2375 (ep_bw
->mult
* ep_bw
->num_packets
*
2376 (SS_OVERHEAD
+ mps
));
2377 return DIV_ROUND_UP(ep_bw
->mult
* ep_bw
->num_packets
*
2378 (SS_OVERHEAD
+ mps
+ SS_OVERHEAD_BURST
),
2379 1 << ep_bw
->ep_interval
);
2383 void xhci_drop_ep_from_interval_table(struct xhci_hcd
*xhci
,
2384 struct xhci_bw_info
*ep_bw
,
2385 struct xhci_interval_bw_table
*bw_table
,
2386 struct usb_device
*udev
,
2387 struct xhci_virt_ep
*virt_ep
,
2388 struct xhci_tt_bw_info
*tt_info
)
2390 struct xhci_interval_bw
*interval_bw
;
2391 int normalized_interval
;
2393 if (xhci_is_async_ep(ep_bw
->type
))
2396 if (udev
->speed
== USB_SPEED_SUPER
) {
2397 if (xhci_is_sync_in_ep(ep_bw
->type
))
2398 xhci
->devs
[udev
->slot_id
]->bw_table
->ss_bw_in
-=
2399 xhci_get_ss_bw_consumed(ep_bw
);
2401 xhci
->devs
[udev
->slot_id
]->bw_table
->ss_bw_out
-=
2402 xhci_get_ss_bw_consumed(ep_bw
);
2406 /* SuperSpeed endpoints never get added to intervals in the table, so
2407 * this check is only valid for HS/FS/LS devices.
2409 if (list_empty(&virt_ep
->bw_endpoint_list
))
2411 /* For LS/FS devices, we need to translate the interval expressed in
2412 * microframes to frames.
2414 if (udev
->speed
== USB_SPEED_HIGH
)
2415 normalized_interval
= ep_bw
->ep_interval
;
2417 normalized_interval
= ep_bw
->ep_interval
- 3;
2419 if (normalized_interval
== 0)
2420 bw_table
->interval0_esit_payload
-= ep_bw
->max_esit_payload
;
2421 interval_bw
= &bw_table
->interval_bw
[normalized_interval
];
2422 interval_bw
->num_packets
-= ep_bw
->num_packets
;
2423 switch (udev
->speed
) {
2425 interval_bw
->overhead
[LS_OVERHEAD_TYPE
] -= 1;
2427 case USB_SPEED_FULL
:
2428 interval_bw
->overhead
[FS_OVERHEAD_TYPE
] -= 1;
2430 case USB_SPEED_HIGH
:
2431 interval_bw
->overhead
[HS_OVERHEAD_TYPE
] -= 1;
2433 case USB_SPEED_SUPER
:
2434 case USB_SPEED_UNKNOWN
:
2435 case USB_SPEED_WIRELESS
:
2436 /* Should never happen because only LS/FS/HS endpoints will get
2437 * added to the endpoint list.
2442 tt_info
->active_eps
-= 1;
2443 list_del_init(&virt_ep
->bw_endpoint_list
);
2446 static void xhci_add_ep_to_interval_table(struct xhci_hcd
*xhci
,
2447 struct xhci_bw_info
*ep_bw
,
2448 struct xhci_interval_bw_table
*bw_table
,
2449 struct usb_device
*udev
,
2450 struct xhci_virt_ep
*virt_ep
,
2451 struct xhci_tt_bw_info
*tt_info
)
2453 struct xhci_interval_bw
*interval_bw
;
2454 struct xhci_virt_ep
*smaller_ep
;
2455 int normalized_interval
;
2457 if (xhci_is_async_ep(ep_bw
->type
))
2460 if (udev
->speed
== USB_SPEED_SUPER
) {
2461 if (xhci_is_sync_in_ep(ep_bw
->type
))
2462 xhci
->devs
[udev
->slot_id
]->bw_table
->ss_bw_in
+=
2463 xhci_get_ss_bw_consumed(ep_bw
);
2465 xhci
->devs
[udev
->slot_id
]->bw_table
->ss_bw_out
+=
2466 xhci_get_ss_bw_consumed(ep_bw
);
2470 /* For LS/FS devices, we need to translate the interval expressed in
2471 * microframes to frames.
2473 if (udev
->speed
== USB_SPEED_HIGH
)
2474 normalized_interval
= ep_bw
->ep_interval
;
2476 normalized_interval
= ep_bw
->ep_interval
- 3;
2478 if (normalized_interval
== 0)
2479 bw_table
->interval0_esit_payload
+= ep_bw
->max_esit_payload
;
2480 interval_bw
= &bw_table
->interval_bw
[normalized_interval
];
2481 interval_bw
->num_packets
+= ep_bw
->num_packets
;
2482 switch (udev
->speed
) {
2484 interval_bw
->overhead
[LS_OVERHEAD_TYPE
] += 1;
2486 case USB_SPEED_FULL
:
2487 interval_bw
->overhead
[FS_OVERHEAD_TYPE
] += 1;
2489 case USB_SPEED_HIGH
:
2490 interval_bw
->overhead
[HS_OVERHEAD_TYPE
] += 1;
2492 case USB_SPEED_SUPER
:
2493 case USB_SPEED_UNKNOWN
:
2494 case USB_SPEED_WIRELESS
:
2495 /* Should never happen because only LS/FS/HS endpoints will get
2496 * added to the endpoint list.
2502 tt_info
->active_eps
+= 1;
2503 /* Insert the endpoint into the list, largest max packet size first. */
2504 list_for_each_entry(smaller_ep
, &interval_bw
->endpoints
,
2506 if (ep_bw
->max_packet_size
>=
2507 smaller_ep
->bw_info
.max_packet_size
) {
2508 /* Add the new ep before the smaller endpoint */
2509 list_add_tail(&virt_ep
->bw_endpoint_list
,
2510 &smaller_ep
->bw_endpoint_list
);
2514 /* Add the new endpoint at the end of the list. */
2515 list_add_tail(&virt_ep
->bw_endpoint_list
,
2516 &interval_bw
->endpoints
);
2519 void xhci_update_tt_active_eps(struct xhci_hcd
*xhci
,
2520 struct xhci_virt_device
*virt_dev
,
2523 struct xhci_root_port_bw_info
*rh_bw_info
;
2524 if (!virt_dev
->tt_info
)
2527 rh_bw_info
= &xhci
->rh_bw
[virt_dev
->real_port
- 1];
2528 if (old_active_eps
== 0 &&
2529 virt_dev
->tt_info
->active_eps
!= 0) {
2530 rh_bw_info
->num_active_tts
+= 1;
2531 rh_bw_info
->bw_table
.bw_used
+= TT_HS_OVERHEAD
;
2532 } else if (old_active_eps
!= 0 &&
2533 virt_dev
->tt_info
->active_eps
== 0) {
2534 rh_bw_info
->num_active_tts
-= 1;
2535 rh_bw_info
->bw_table
.bw_used
-= TT_HS_OVERHEAD
;
2539 static int xhci_reserve_bandwidth(struct xhci_hcd
*xhci
,
2540 struct xhci_virt_device
*virt_dev
,
2541 struct xhci_container_ctx
*in_ctx
)
2543 struct xhci_bw_info ep_bw_info
[31];
2545 struct xhci_input_control_ctx
*ctrl_ctx
;
2546 int old_active_eps
= 0;
2548 if (virt_dev
->tt_info
)
2549 old_active_eps
= virt_dev
->tt_info
->active_eps
;
2551 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
2553 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
2558 for (i
= 0; i
< 31; i
++) {
2559 if (!EP_IS_ADDED(ctrl_ctx
, i
) && !EP_IS_DROPPED(ctrl_ctx
, i
))
2562 /* Make a copy of the BW info in case we need to revert this */
2563 memcpy(&ep_bw_info
[i
], &virt_dev
->eps
[i
].bw_info
,
2564 sizeof(ep_bw_info
[i
]));
2565 /* Drop the endpoint from the interval table if the endpoint is
2566 * being dropped or changed.
2568 if (EP_IS_DROPPED(ctrl_ctx
, i
))
2569 xhci_drop_ep_from_interval_table(xhci
,
2570 &virt_dev
->eps
[i
].bw_info
,
2576 /* Overwrite the information stored in the endpoints' bw_info */
2577 xhci_update_bw_info(xhci
, virt_dev
->in_ctx
, ctrl_ctx
, virt_dev
);
2578 for (i
= 0; i
< 31; i
++) {
2579 /* Add any changed or added endpoints to the interval table */
2580 if (EP_IS_ADDED(ctrl_ctx
, i
))
2581 xhci_add_ep_to_interval_table(xhci
,
2582 &virt_dev
->eps
[i
].bw_info
,
2589 if (!xhci_check_bw_table(xhci
, virt_dev
, old_active_eps
)) {
2590 /* Ok, this fits in the bandwidth we have.
2591 * Update the number of active TTs.
2593 xhci_update_tt_active_eps(xhci
, virt_dev
, old_active_eps
);
2597 /* We don't have enough bandwidth for this, revert the stored info. */
2598 for (i
= 0; i
< 31; i
++) {
2599 if (!EP_IS_ADDED(ctrl_ctx
, i
) && !EP_IS_DROPPED(ctrl_ctx
, i
))
2602 /* Drop the new copies of any added or changed endpoints from
2603 * the interval table.
2605 if (EP_IS_ADDED(ctrl_ctx
, i
)) {
2606 xhci_drop_ep_from_interval_table(xhci
,
2607 &virt_dev
->eps
[i
].bw_info
,
2613 /* Revert the endpoint back to its old information */
2614 memcpy(&virt_dev
->eps
[i
].bw_info
, &ep_bw_info
[i
],
2615 sizeof(ep_bw_info
[i
]));
2616 /* Add any changed or dropped endpoints back into the table */
2617 if (EP_IS_DROPPED(ctrl_ctx
, i
))
2618 xhci_add_ep_to_interval_table(xhci
,
2619 &virt_dev
->eps
[i
].bw_info
,
2629 /* Issue a configure endpoint command or evaluate context command
2630 * and wait for it to finish.
2632 static int xhci_configure_endpoint(struct xhci_hcd
*xhci
,
2633 struct usb_device
*udev
,
2634 struct xhci_command
*command
,
2635 bool ctx_change
, bool must_succeed
)
2638 unsigned long flags
;
2639 struct xhci_input_control_ctx
*ctrl_ctx
;
2640 struct xhci_virt_device
*virt_dev
;
2645 spin_lock_irqsave(&xhci
->lock
, flags
);
2646 virt_dev
= xhci
->devs
[udev
->slot_id
];
2648 ctrl_ctx
= xhci_get_input_control_ctx(command
->in_ctx
);
2650 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2651 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
2656 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
) &&
2657 xhci_reserve_host_resources(xhci
, ctrl_ctx
)) {
2658 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2659 xhci_warn(xhci
, "Not enough host resources, "
2660 "active endpoint contexts = %u\n",
2661 xhci
->num_active_eps
);
2664 if ((xhci
->quirks
& XHCI_SW_BW_CHECKING
) &&
2665 xhci_reserve_bandwidth(xhci
, virt_dev
, command
->in_ctx
)) {
2666 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
))
2667 xhci_free_host_resources(xhci
, ctrl_ctx
);
2668 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2669 xhci_warn(xhci
, "Not enough bandwidth\n");
2674 ret
= xhci_queue_configure_endpoint(xhci
, command
,
2675 command
->in_ctx
->dma
,
2676 udev
->slot_id
, must_succeed
);
2678 ret
= xhci_queue_evaluate_context(xhci
, command
,
2679 command
->in_ctx
->dma
,
2680 udev
->slot_id
, must_succeed
);
2682 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
))
2683 xhci_free_host_resources(xhci
, ctrl_ctx
);
2684 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2685 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
2686 "FIXME allocate a new ring segment");
2689 xhci_ring_cmd_db(xhci
);
2690 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2692 /* Wait for the configure endpoint command to complete */
2693 wait_for_completion(command
->completion
);
2696 ret
= xhci_configure_endpoint_result(xhci
, udev
,
2699 ret
= xhci_evaluate_context_result(xhci
, udev
,
2702 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
)) {
2703 spin_lock_irqsave(&xhci
->lock
, flags
);
2704 /* If the command failed, remove the reserved resources.
2705 * Otherwise, clean up the estimate to include dropped eps.
2708 xhci_free_host_resources(xhci
, ctrl_ctx
);
2710 xhci_finish_resource_reservation(xhci
, ctrl_ctx
);
2711 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2716 static void xhci_check_bw_drop_ep_streams(struct xhci_hcd
*xhci
,
2717 struct xhci_virt_device
*vdev
, int i
)
2719 struct xhci_virt_ep
*ep
= &vdev
->eps
[i
];
2721 if (ep
->ep_state
& EP_HAS_STREAMS
) {
2722 xhci_warn(xhci
, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n",
2723 xhci_get_endpoint_address(i
));
2724 xhci_free_stream_info(xhci
, ep
->stream_info
);
2725 ep
->stream_info
= NULL
;
2726 ep
->ep_state
&= ~EP_HAS_STREAMS
;
2730 /* Called after one or more calls to xhci_add_endpoint() or
2731 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2732 * to call xhci_reset_bandwidth().
2734 * Since we are in the middle of changing either configuration or
2735 * installing a new alt setting, the USB core won't allow URBs to be
2736 * enqueued for any endpoint on the old config or interface. Nothing
2737 * else should be touching the xhci->devs[slot_id] structure, so we
2738 * don't need to take the xhci->lock for manipulating that.
2740 int xhci_check_bandwidth(struct usb_hcd
*hcd
, struct usb_device
*udev
)
2744 struct xhci_hcd
*xhci
;
2745 struct xhci_virt_device
*virt_dev
;
2746 struct xhci_input_control_ctx
*ctrl_ctx
;
2747 struct xhci_slot_ctx
*slot_ctx
;
2748 struct xhci_command
*command
;
2750 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, true, __func__
);
2753 xhci
= hcd_to_xhci(hcd
);
2754 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
2757 xhci_dbg(xhci
, "%s called for udev %p\n", __func__
, udev
);
2758 virt_dev
= xhci
->devs
[udev
->slot_id
];
2760 command
= xhci_alloc_command(xhci
, false, true, GFP_KERNEL
);
2764 command
->in_ctx
= virt_dev
->in_ctx
;
2766 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
2767 ctrl_ctx
= xhci_get_input_control_ctx(command
->in_ctx
);
2769 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
2772 goto command_cleanup
;
2774 ctrl_ctx
->add_flags
|= cpu_to_le32(SLOT_FLAG
);
2775 ctrl_ctx
->add_flags
&= cpu_to_le32(~EP0_FLAG
);
2776 ctrl_ctx
->drop_flags
&= cpu_to_le32(~(SLOT_FLAG
| EP0_FLAG
));
2778 /* Don't issue the command if there's no endpoints to update. */
2779 if (ctrl_ctx
->add_flags
== cpu_to_le32(SLOT_FLAG
) &&
2780 ctrl_ctx
->drop_flags
== 0) {
2782 goto command_cleanup
;
2784 /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
2785 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->in_ctx
);
2786 for (i
= 31; i
>= 1; i
--) {
2787 __le32 le32
= cpu_to_le32(BIT(i
));
2789 if ((virt_dev
->eps
[i
-1].ring
&& !(ctrl_ctx
->drop_flags
& le32
))
2790 || (ctrl_ctx
->add_flags
& le32
) || i
== 1) {
2791 slot_ctx
->dev_info
&= cpu_to_le32(~LAST_CTX_MASK
);
2792 slot_ctx
->dev_info
|= cpu_to_le32(LAST_CTX(i
));
2796 xhci_dbg(xhci
, "New Input Control Context:\n");
2797 xhci_dbg_ctx(xhci
, virt_dev
->in_ctx
,
2798 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx
->dev_info
)));
2800 ret
= xhci_configure_endpoint(xhci
, udev
, command
,
2803 /* Callee should call reset_bandwidth() */
2804 goto command_cleanup
;
2806 xhci_dbg(xhci
, "Output context after successful config ep cmd:\n");
2807 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
,
2808 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx
->dev_info
)));
2810 /* Free any rings that were dropped, but not changed. */
2811 for (i
= 1; i
< 31; ++i
) {
2812 if ((le32_to_cpu(ctrl_ctx
->drop_flags
) & (1 << (i
+ 1))) &&
2813 !(le32_to_cpu(ctrl_ctx
->add_flags
) & (1 << (i
+ 1)))) {
2814 xhci_free_or_cache_endpoint_ring(xhci
, virt_dev
, i
);
2815 xhci_check_bw_drop_ep_streams(xhci
, virt_dev
, i
);
2818 xhci_zero_in_ctx(xhci
, virt_dev
);
2820 * Install any rings for completely new endpoints or changed endpoints,
2821 * and free or cache any old rings from changed endpoints.
2823 for (i
= 1; i
< 31; ++i
) {
2824 if (!virt_dev
->eps
[i
].new_ring
)
2826 /* Only cache or free the old ring if it exists.
2827 * It may not if this is the first add of an endpoint.
2829 if (virt_dev
->eps
[i
].ring
) {
2830 xhci_free_or_cache_endpoint_ring(xhci
, virt_dev
, i
);
2832 xhci_check_bw_drop_ep_streams(xhci
, virt_dev
, i
);
2833 virt_dev
->eps
[i
].ring
= virt_dev
->eps
[i
].new_ring
;
2834 virt_dev
->eps
[i
].new_ring
= NULL
;
2837 kfree(command
->completion
);
2843 void xhci_reset_bandwidth(struct usb_hcd
*hcd
, struct usb_device
*udev
)
2845 struct xhci_hcd
*xhci
;
2846 struct xhci_virt_device
*virt_dev
;
2849 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, true, __func__
);
2852 xhci
= hcd_to_xhci(hcd
);
2854 xhci_dbg(xhci
, "%s called for udev %p\n", __func__
, udev
);
2855 virt_dev
= xhci
->devs
[udev
->slot_id
];
2856 /* Free any rings allocated for added endpoints */
2857 for (i
= 0; i
< 31; ++i
) {
2858 if (virt_dev
->eps
[i
].new_ring
) {
2859 xhci_ring_free(xhci
, virt_dev
->eps
[i
].new_ring
);
2860 virt_dev
->eps
[i
].new_ring
= NULL
;
2863 xhci_zero_in_ctx(xhci
, virt_dev
);
2866 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd
*xhci
,
2867 struct xhci_container_ctx
*in_ctx
,
2868 struct xhci_container_ctx
*out_ctx
,
2869 struct xhci_input_control_ctx
*ctrl_ctx
,
2870 u32 add_flags
, u32 drop_flags
)
2872 ctrl_ctx
->add_flags
= cpu_to_le32(add_flags
);
2873 ctrl_ctx
->drop_flags
= cpu_to_le32(drop_flags
);
2874 xhci_slot_copy(xhci
, in_ctx
, out_ctx
);
2875 ctrl_ctx
->add_flags
|= cpu_to_le32(SLOT_FLAG
);
2877 xhci_dbg(xhci
, "Input Context:\n");
2878 xhci_dbg_ctx(xhci
, in_ctx
, xhci_last_valid_endpoint(add_flags
));
2881 static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd
*xhci
,
2882 unsigned int slot_id
, unsigned int ep_index
,
2883 struct xhci_dequeue_state
*deq_state
)
2885 struct xhci_input_control_ctx
*ctrl_ctx
;
2886 struct xhci_container_ctx
*in_ctx
;
2887 struct xhci_ep_ctx
*ep_ctx
;
2891 in_ctx
= xhci
->devs
[slot_id
]->in_ctx
;
2892 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
2894 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
2899 xhci_endpoint_copy(xhci
, xhci
->devs
[slot_id
]->in_ctx
,
2900 xhci
->devs
[slot_id
]->out_ctx
, ep_index
);
2901 ep_ctx
= xhci_get_ep_ctx(xhci
, in_ctx
, ep_index
);
2902 addr
= xhci_trb_virt_to_dma(deq_state
->new_deq_seg
,
2903 deq_state
->new_deq_ptr
);
2905 xhci_warn(xhci
, "WARN Cannot submit config ep after "
2906 "reset ep command\n");
2907 xhci_warn(xhci
, "WARN deq seg = %p, deq ptr = %p\n",
2908 deq_state
->new_deq_seg
,
2909 deq_state
->new_deq_ptr
);
2912 ep_ctx
->deq
= cpu_to_le64(addr
| deq_state
->new_cycle_state
);
2914 added_ctxs
= xhci_get_endpoint_flag_from_index(ep_index
);
2915 xhci_setup_input_ctx_for_config_ep(xhci
, xhci
->devs
[slot_id
]->in_ctx
,
2916 xhci
->devs
[slot_id
]->out_ctx
, ctrl_ctx
,
2917 added_ctxs
, added_ctxs
);
2920 void xhci_cleanup_stalled_ring(struct xhci_hcd
*xhci
,
2921 unsigned int ep_index
, struct xhci_td
*td
)
2923 struct xhci_dequeue_state deq_state
;
2924 struct xhci_virt_ep
*ep
;
2925 struct usb_device
*udev
= td
->urb
->dev
;
2927 xhci_dbg_trace(xhci
, trace_xhci_dbg_reset_ep
,
2928 "Cleaning up stalled endpoint ring");
2929 ep
= &xhci
->devs
[udev
->slot_id
]->eps
[ep_index
];
2930 /* We need to move the HW's dequeue pointer past this TD,
2931 * or it will attempt to resend it on the next doorbell ring.
2933 xhci_find_new_dequeue_state(xhci
, udev
->slot_id
,
2934 ep_index
, ep
->stopped_stream
, td
, &deq_state
);
2936 if (!deq_state
.new_deq_ptr
|| !deq_state
.new_deq_seg
)
2939 /* HW with the reset endpoint quirk will use the saved dequeue state to
2940 * issue a configure endpoint command later.
2942 if (!(xhci
->quirks
& XHCI_RESET_EP_QUIRK
)) {
2943 xhci_dbg_trace(xhci
, trace_xhci_dbg_reset_ep
,
2944 "Queueing new dequeue state");
2945 xhci_queue_new_dequeue_state(xhci
, udev
->slot_id
,
2946 ep_index
, ep
->stopped_stream
, &deq_state
);
2948 /* Better hope no one uses the input context between now and the
2949 * reset endpoint completion!
2950 * XXX: No idea how this hardware will react when stream rings
2953 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2954 "Setting up input context for "
2955 "configure endpoint command");
2956 xhci_setup_input_ctx_for_quirk(xhci
, udev
->slot_id
,
2957 ep_index
, &deq_state
);
2961 /* Called when clearing halted device. The core should have sent the control
2962 * message to clear the device halt condition. The host side of the halt should
2963 * already be cleared with a reset endpoint command issued when the STALL tx
2964 * event was received.
2966 * Context: in_interrupt
2969 void xhci_endpoint_reset(struct usb_hcd
*hcd
,
2970 struct usb_host_endpoint
*ep
)
2972 struct xhci_hcd
*xhci
;
2974 xhci
= hcd_to_xhci(hcd
);
2977 * We might need to implement the config ep cmd in xhci 4.8.1 note:
2978 * The Reset Endpoint Command may only be issued to endpoints in the
2979 * Halted state. If software wishes reset the Data Toggle or Sequence
2980 * Number of an endpoint that isn't in the Halted state, then software
2981 * may issue a Configure Endpoint Command with the Drop and Add bits set
2982 * for the target endpoint. that is in the Stopped state.
2985 /* For now just print debug to follow the situation */
2986 xhci_dbg(xhci
, "Endpoint 0x%x ep reset callback called\n",
2987 ep
->desc
.bEndpointAddress
);
2990 static int xhci_check_streams_endpoint(struct xhci_hcd
*xhci
,
2991 struct usb_device
*udev
, struct usb_host_endpoint
*ep
,
2992 unsigned int slot_id
)
2995 unsigned int ep_index
;
2996 unsigned int ep_state
;
3000 ret
= xhci_check_args(xhci_to_hcd(xhci
), udev
, ep
, 1, true, __func__
);
3003 if (usb_ss_max_streams(&ep
->ss_ep_comp
) == 0) {
3004 xhci_warn(xhci
, "WARN: SuperSpeed Endpoint Companion"
3005 " descriptor for ep 0x%x does not support streams\n",
3006 ep
->desc
.bEndpointAddress
);
3010 ep_index
= xhci_get_endpoint_index(&ep
->desc
);
3011 ep_state
= xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
;
3012 if (ep_state
& EP_HAS_STREAMS
||
3013 ep_state
& EP_GETTING_STREAMS
) {
3014 xhci_warn(xhci
, "WARN: SuperSpeed bulk endpoint 0x%x "
3015 "already has streams set up.\n",
3016 ep
->desc
.bEndpointAddress
);
3017 xhci_warn(xhci
, "Send email to xHCI maintainer and ask for "
3018 "dynamic stream context array reallocation.\n");
3021 if (!list_empty(&xhci
->devs
[slot_id
]->eps
[ep_index
].ring
->td_list
)) {
3022 xhci_warn(xhci
, "Cannot setup streams for SuperSpeed bulk "
3023 "endpoint 0x%x; URBs are pending.\n",
3024 ep
->desc
.bEndpointAddress
);
3030 static void xhci_calculate_streams_entries(struct xhci_hcd
*xhci
,
3031 unsigned int *num_streams
, unsigned int *num_stream_ctxs
)
3033 unsigned int max_streams
;
3035 /* The stream context array size must be a power of two */
3036 *num_stream_ctxs
= roundup_pow_of_two(*num_streams
);
3038 * Find out how many primary stream array entries the host controller
3039 * supports. Later we may use secondary stream arrays (similar to 2nd
3040 * level page entries), but that's an optional feature for xHCI host
3041 * controllers. xHCs must support at least 4 stream IDs.
3043 max_streams
= HCC_MAX_PSA(xhci
->hcc_params
);
3044 if (*num_stream_ctxs
> max_streams
) {
3045 xhci_dbg(xhci
, "xHCI HW only supports %u stream ctx entries.\n",
3047 *num_stream_ctxs
= max_streams
;
3048 *num_streams
= max_streams
;
3052 /* Returns an error code if one of the endpoint already has streams.
3053 * This does not change any data structures, it only checks and gathers
3056 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd
*xhci
,
3057 struct usb_device
*udev
,
3058 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
3059 unsigned int *num_streams
, u32
*changed_ep_bitmask
)
3061 unsigned int max_streams
;
3062 unsigned int endpoint_flag
;
3066 for (i
= 0; i
< num_eps
; i
++) {
3067 ret
= xhci_check_streams_endpoint(xhci
, udev
,
3068 eps
[i
], udev
->slot_id
);
3072 max_streams
= usb_ss_max_streams(&eps
[i
]->ss_ep_comp
);
3073 if (max_streams
< (*num_streams
- 1)) {
3074 xhci_dbg(xhci
, "Ep 0x%x only supports %u stream IDs.\n",
3075 eps
[i
]->desc
.bEndpointAddress
,
3077 *num_streams
= max_streams
+1;
3080 endpoint_flag
= xhci_get_endpoint_flag(&eps
[i
]->desc
);
3081 if (*changed_ep_bitmask
& endpoint_flag
)
3083 *changed_ep_bitmask
|= endpoint_flag
;
3088 static u32
xhci_calculate_no_streams_bitmask(struct xhci_hcd
*xhci
,
3089 struct usb_device
*udev
,
3090 struct usb_host_endpoint
**eps
, unsigned int num_eps
)
3092 u32 changed_ep_bitmask
= 0;
3093 unsigned int slot_id
;
3094 unsigned int ep_index
;
3095 unsigned int ep_state
;
3098 slot_id
= udev
->slot_id
;
3099 if (!xhci
->devs
[slot_id
])
3102 for (i
= 0; i
< num_eps
; i
++) {
3103 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3104 ep_state
= xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
;
3105 /* Are streams already being freed for the endpoint? */
3106 if (ep_state
& EP_GETTING_NO_STREAMS
) {
3107 xhci_warn(xhci
, "WARN Can't disable streams for "
3109 "streams are being disabled already\n",
3110 eps
[i
]->desc
.bEndpointAddress
);
3113 /* Are there actually any streams to free? */
3114 if (!(ep_state
& EP_HAS_STREAMS
) &&
3115 !(ep_state
& EP_GETTING_STREAMS
)) {
3116 xhci_warn(xhci
, "WARN Can't disable streams for "
3118 "streams are already disabled!\n",
3119 eps
[i
]->desc
.bEndpointAddress
);
3120 xhci_warn(xhci
, "WARN xhci_free_streams() called "
3121 "with non-streams endpoint\n");
3124 changed_ep_bitmask
|= xhci_get_endpoint_flag(&eps
[i
]->desc
);
3126 return changed_ep_bitmask
;
3130 * The USB device drivers use this function (through the HCD interface in USB
3131 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3132 * coordinate mass storage command queueing across multiple endpoints (basically
3133 * a stream ID == a task ID).
3135 * Setting up streams involves allocating the same size stream context array
3136 * for each endpoint and issuing a configure endpoint command for all endpoints.
3138 * Don't allow the call to succeed if one endpoint only supports one stream
3139 * (which means it doesn't support streams at all).
3141 * Drivers may get less stream IDs than they asked for, if the host controller
3142 * hardware or endpoints claim they can't support the number of requested
3145 int xhci_alloc_streams(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3146 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
3147 unsigned int num_streams
, gfp_t mem_flags
)
3150 struct xhci_hcd
*xhci
;
3151 struct xhci_virt_device
*vdev
;
3152 struct xhci_command
*config_cmd
;
3153 struct xhci_input_control_ctx
*ctrl_ctx
;
3154 unsigned int ep_index
;
3155 unsigned int num_stream_ctxs
;
3156 unsigned long flags
;
3157 u32 changed_ep_bitmask
= 0;
3162 /* Add one to the number of streams requested to account for
3163 * stream 0 that is reserved for xHCI usage.
3166 xhci
= hcd_to_xhci(hcd
);
3167 xhci_dbg(xhci
, "Driver wants %u stream IDs (including stream 0).\n",
3170 /* MaxPSASize value 0 (2 streams) means streams are not supported */
3171 if ((xhci
->quirks
& XHCI_BROKEN_STREAMS
) ||
3172 HCC_MAX_PSA(xhci
->hcc_params
) < 4) {
3173 xhci_dbg(xhci
, "xHCI controller does not support streams.\n");
3177 config_cmd
= xhci_alloc_command(xhci
, true, true, mem_flags
);
3179 xhci_dbg(xhci
, "Could not allocate xHCI command structure.\n");
3182 ctrl_ctx
= xhci_get_input_control_ctx(config_cmd
->in_ctx
);
3184 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
3186 xhci_free_command(xhci
, config_cmd
);
3190 /* Check to make sure all endpoints are not already configured for
3191 * streams. While we're at it, find the maximum number of streams that
3192 * all the endpoints will support and check for duplicate endpoints.
3194 spin_lock_irqsave(&xhci
->lock
, flags
);
3195 ret
= xhci_calculate_streams_and_bitmask(xhci
, udev
, eps
,
3196 num_eps
, &num_streams
, &changed_ep_bitmask
);
3198 xhci_free_command(xhci
, config_cmd
);
3199 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3202 if (num_streams
<= 1) {
3203 xhci_warn(xhci
, "WARN: endpoints can't handle "
3204 "more than one stream.\n");
3205 xhci_free_command(xhci
, config_cmd
);
3206 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3209 vdev
= xhci
->devs
[udev
->slot_id
];
3210 /* Mark each endpoint as being in transition, so
3211 * xhci_urb_enqueue() will reject all URBs.
3213 for (i
= 0; i
< num_eps
; i
++) {
3214 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3215 vdev
->eps
[ep_index
].ep_state
|= EP_GETTING_STREAMS
;
3217 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3219 /* Setup internal data structures and allocate HW data structures for
3220 * streams (but don't install the HW structures in the input context
3221 * until we're sure all memory allocation succeeded).
3223 xhci_calculate_streams_entries(xhci
, &num_streams
, &num_stream_ctxs
);
3224 xhci_dbg(xhci
, "Need %u stream ctx entries for %u stream IDs.\n",
3225 num_stream_ctxs
, num_streams
);
3227 for (i
= 0; i
< num_eps
; i
++) {
3228 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3229 vdev
->eps
[ep_index
].stream_info
= xhci_alloc_stream_info(xhci
,
3231 num_streams
, mem_flags
);
3232 if (!vdev
->eps
[ep_index
].stream_info
)
3234 /* Set maxPstreams in endpoint context and update deq ptr to
3235 * point to stream context array. FIXME
3239 /* Set up the input context for a configure endpoint command. */
3240 for (i
= 0; i
< num_eps
; i
++) {
3241 struct xhci_ep_ctx
*ep_ctx
;
3243 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3244 ep_ctx
= xhci_get_ep_ctx(xhci
, config_cmd
->in_ctx
, ep_index
);
3246 xhci_endpoint_copy(xhci
, config_cmd
->in_ctx
,
3247 vdev
->out_ctx
, ep_index
);
3248 xhci_setup_streams_ep_input_ctx(xhci
, ep_ctx
,
3249 vdev
->eps
[ep_index
].stream_info
);
3251 /* Tell the HW to drop its old copy of the endpoint context info
3252 * and add the updated copy from the input context.
3254 xhci_setup_input_ctx_for_config_ep(xhci
, config_cmd
->in_ctx
,
3255 vdev
->out_ctx
, ctrl_ctx
,
3256 changed_ep_bitmask
, changed_ep_bitmask
);
3258 /* Issue and wait for the configure endpoint command */
3259 ret
= xhci_configure_endpoint(xhci
, udev
, config_cmd
,
3262 /* xHC rejected the configure endpoint command for some reason, so we
3263 * leave the old ring intact and free our internal streams data
3269 spin_lock_irqsave(&xhci
->lock
, flags
);
3270 for (i
= 0; i
< num_eps
; i
++) {
3271 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3272 vdev
->eps
[ep_index
].ep_state
&= ~EP_GETTING_STREAMS
;
3273 xhci_dbg(xhci
, "Slot %u ep ctx %u now has streams.\n",
3274 udev
->slot_id
, ep_index
);
3275 vdev
->eps
[ep_index
].ep_state
|= EP_HAS_STREAMS
;
3277 xhci_free_command(xhci
, config_cmd
);
3278 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3280 /* Subtract 1 for stream 0, which drivers can't use */
3281 return num_streams
- 1;
3284 /* If it didn't work, free the streams! */
3285 for (i
= 0; i
< num_eps
; i
++) {
3286 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3287 xhci_free_stream_info(xhci
, vdev
->eps
[ep_index
].stream_info
);
3288 vdev
->eps
[ep_index
].stream_info
= NULL
;
3289 /* FIXME Unset maxPstreams in endpoint context and
3290 * update deq ptr to point to normal string ring.
3292 vdev
->eps
[ep_index
].ep_state
&= ~EP_GETTING_STREAMS
;
3293 vdev
->eps
[ep_index
].ep_state
&= ~EP_HAS_STREAMS
;
3294 xhci_endpoint_zero(xhci
, vdev
, eps
[i
]);
3296 xhci_free_command(xhci
, config_cmd
);
3300 /* Transition the endpoint from using streams to being a "normal" endpoint
3303 * Modify the endpoint context state, submit a configure endpoint command,
3304 * and free all endpoint rings for streams if that completes successfully.
3306 int xhci_free_streams(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3307 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
3311 struct xhci_hcd
*xhci
;
3312 struct xhci_virt_device
*vdev
;
3313 struct xhci_command
*command
;
3314 struct xhci_input_control_ctx
*ctrl_ctx
;
3315 unsigned int ep_index
;
3316 unsigned long flags
;
3317 u32 changed_ep_bitmask
;
3319 xhci
= hcd_to_xhci(hcd
);
3320 vdev
= xhci
->devs
[udev
->slot_id
];
3322 /* Set up a configure endpoint command to remove the streams rings */
3323 spin_lock_irqsave(&xhci
->lock
, flags
);
3324 changed_ep_bitmask
= xhci_calculate_no_streams_bitmask(xhci
,
3325 udev
, eps
, num_eps
);
3326 if (changed_ep_bitmask
== 0) {
3327 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3331 /* Use the xhci_command structure from the first endpoint. We may have
3332 * allocated too many, but the driver may call xhci_free_streams() for
3333 * each endpoint it grouped into one call to xhci_alloc_streams().
3335 ep_index
= xhci_get_endpoint_index(&eps
[0]->desc
);
3336 command
= vdev
->eps
[ep_index
].stream_info
->free_streams_command
;
3337 ctrl_ctx
= xhci_get_input_control_ctx(command
->in_ctx
);
3339 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3340 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
3345 for (i
= 0; i
< num_eps
; i
++) {
3346 struct xhci_ep_ctx
*ep_ctx
;
3348 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3349 ep_ctx
= xhci_get_ep_ctx(xhci
, command
->in_ctx
, ep_index
);
3350 xhci
->devs
[udev
->slot_id
]->eps
[ep_index
].ep_state
|=
3351 EP_GETTING_NO_STREAMS
;
3353 xhci_endpoint_copy(xhci
, command
->in_ctx
,
3354 vdev
->out_ctx
, ep_index
);
3355 xhci_setup_no_streams_ep_input_ctx(ep_ctx
,
3356 &vdev
->eps
[ep_index
]);
3358 xhci_setup_input_ctx_for_config_ep(xhci
, command
->in_ctx
,
3359 vdev
->out_ctx
, ctrl_ctx
,
3360 changed_ep_bitmask
, changed_ep_bitmask
);
3361 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3363 /* Issue and wait for the configure endpoint command,
3364 * which must succeed.
3366 ret
= xhci_configure_endpoint(xhci
, udev
, command
,
3369 /* xHC rejected the configure endpoint command for some reason, so we
3370 * leave the streams rings intact.
3375 spin_lock_irqsave(&xhci
->lock
, flags
);
3376 for (i
= 0; i
< num_eps
; i
++) {
3377 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3378 xhci_free_stream_info(xhci
, vdev
->eps
[ep_index
].stream_info
);
3379 vdev
->eps
[ep_index
].stream_info
= NULL
;
3380 /* FIXME Unset maxPstreams in endpoint context and
3381 * update deq ptr to point to normal string ring.
3383 vdev
->eps
[ep_index
].ep_state
&= ~EP_GETTING_NO_STREAMS
;
3384 vdev
->eps
[ep_index
].ep_state
&= ~EP_HAS_STREAMS
;
3386 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3392 * Deletes endpoint resources for endpoints that were active before a Reset
3393 * Device command, or a Disable Slot command. The Reset Device command leaves
3394 * the control endpoint intact, whereas the Disable Slot command deletes it.
3396 * Must be called with xhci->lock held.
3398 void xhci_free_device_endpoint_resources(struct xhci_hcd
*xhci
,
3399 struct xhci_virt_device
*virt_dev
, bool drop_control_ep
)
3402 unsigned int num_dropped_eps
= 0;
3403 unsigned int drop_flags
= 0;
3405 for (i
= (drop_control_ep
? 0 : 1); i
< 31; i
++) {
3406 if (virt_dev
->eps
[i
].ring
) {
3407 drop_flags
|= 1 << i
;
3411 xhci
->num_active_eps
-= num_dropped_eps
;
3412 if (num_dropped_eps
)
3413 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
3414 "Dropped %u ep ctxs, flags = 0x%x, "
3416 num_dropped_eps
, drop_flags
,
3417 xhci
->num_active_eps
);
3421 * This submits a Reset Device Command, which will set the device state to 0,
3422 * set the device address to 0, and disable all the endpoints except the default
3423 * control endpoint. The USB core should come back and call
3424 * xhci_address_device(), and then re-set up the configuration. If this is
3425 * called because of a usb_reset_and_verify_device(), then the old alternate
3426 * settings will be re-installed through the normal bandwidth allocation
3429 * Wait for the Reset Device command to finish. Remove all structures
3430 * associated with the endpoints that were disabled. Clear the input device
3431 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
3433 * If the virt_dev to be reset does not exist or does not match the udev,
3434 * it means the device is lost, possibly due to the xHC restore error and
3435 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3436 * re-allocate the device.
3438 int xhci_discover_or_reset_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
3441 unsigned long flags
;
3442 struct xhci_hcd
*xhci
;
3443 unsigned int slot_id
;
3444 struct xhci_virt_device
*virt_dev
;
3445 struct xhci_command
*reset_device_cmd
;
3446 int last_freed_endpoint
;
3447 struct xhci_slot_ctx
*slot_ctx
;
3448 int old_active_eps
= 0;
3450 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, false, __func__
);
3453 xhci
= hcd_to_xhci(hcd
);
3454 slot_id
= udev
->slot_id
;
3455 virt_dev
= xhci
->devs
[slot_id
];
3457 xhci_dbg(xhci
, "The device to be reset with slot ID %u does "
3458 "not exist. Re-allocate the device\n", slot_id
);
3459 ret
= xhci_alloc_dev(hcd
, udev
);
3466 if (virt_dev
->tt_info
)
3467 old_active_eps
= virt_dev
->tt_info
->active_eps
;
3469 if (virt_dev
->udev
!= udev
) {
3470 /* If the virt_dev and the udev does not match, this virt_dev
3471 * may belong to another udev.
3472 * Re-allocate the device.
3474 xhci_dbg(xhci
, "The device to be reset with slot ID %u does "
3475 "not match the udev. Re-allocate the device\n",
3477 ret
= xhci_alloc_dev(hcd
, udev
);
3484 /* If device is not setup, there is no point in resetting it */
3485 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->out_ctx
);
3486 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx
->dev_state
)) ==
3487 SLOT_STATE_DISABLED
)
3490 xhci_dbg(xhci
, "Resetting device with slot ID %u\n", slot_id
);
3491 /* Allocate the command structure that holds the struct completion.
3492 * Assume we're in process context, since the normal device reset
3493 * process has to wait for the device anyway. Storage devices are
3494 * reset as part of error handling, so use GFP_NOIO instead of
3497 reset_device_cmd
= xhci_alloc_command(xhci
, false, true, GFP_NOIO
);
3498 if (!reset_device_cmd
) {
3499 xhci_dbg(xhci
, "Couldn't allocate command structure.\n");
3503 /* Attempt to submit the Reset Device command to the command ring */
3504 spin_lock_irqsave(&xhci
->lock
, flags
);
3506 ret
= xhci_queue_reset_device(xhci
, reset_device_cmd
, slot_id
);
3508 xhci_dbg(xhci
, "FIXME: allocate a command ring segment\n");
3509 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3510 goto command_cleanup
;
3512 xhci_ring_cmd_db(xhci
);
3513 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3515 /* Wait for the Reset Device command to finish */
3516 wait_for_completion(reset_device_cmd
->completion
);
3518 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3519 * unless we tried to reset a slot ID that wasn't enabled,
3520 * or the device wasn't in the addressed or configured state.
3522 ret
= reset_device_cmd
->status
;
3524 case COMP_CMD_ABORT
:
3526 xhci_warn(xhci
, "Timeout waiting for reset device command\n");
3528 goto command_cleanup
;
3529 case COMP_EBADSLT
: /* 0.95 completion code for bad slot ID */
3530 case COMP_CTX_STATE
: /* 0.96 completion code for same thing */
3531 xhci_dbg(xhci
, "Can't reset device (slot ID %u) in %s state\n",
3533 xhci_get_slot_state(xhci
, virt_dev
->out_ctx
));
3534 xhci_dbg(xhci
, "Not freeing device rings.\n");
3535 /* Don't treat this as an error. May change my mind later. */
3537 goto command_cleanup
;
3539 xhci_dbg(xhci
, "Successful reset device command.\n");
3542 if (xhci_is_vendor_info_code(xhci
, ret
))
3544 xhci_warn(xhci
, "Unknown completion code %u for "
3545 "reset device command.\n", ret
);
3547 goto command_cleanup
;
3550 /* Free up host controller endpoint resources */
3551 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
)) {
3552 spin_lock_irqsave(&xhci
->lock
, flags
);
3553 /* Don't delete the default control endpoint resources */
3554 xhci_free_device_endpoint_resources(xhci
, virt_dev
, false);
3555 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3558 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3559 last_freed_endpoint
= 1;
3560 for (i
= 1; i
< 31; ++i
) {
3561 struct xhci_virt_ep
*ep
= &virt_dev
->eps
[i
];
3563 if (ep
->ep_state
& EP_HAS_STREAMS
) {
3564 xhci_warn(xhci
, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n",
3565 xhci_get_endpoint_address(i
));
3566 xhci_free_stream_info(xhci
, ep
->stream_info
);
3567 ep
->stream_info
= NULL
;
3568 ep
->ep_state
&= ~EP_HAS_STREAMS
;
3572 xhci_free_or_cache_endpoint_ring(xhci
, virt_dev
, i
);
3573 last_freed_endpoint
= i
;
3575 if (!list_empty(&virt_dev
->eps
[i
].bw_endpoint_list
))
3576 xhci_drop_ep_from_interval_table(xhci
,
3577 &virt_dev
->eps
[i
].bw_info
,
3582 xhci_clear_endpoint_bw_info(&virt_dev
->eps
[i
].bw_info
);
3584 /* If necessary, update the number of active TTs on this root port */
3585 xhci_update_tt_active_eps(xhci
, virt_dev
, old_active_eps
);
3587 xhci_dbg(xhci
, "Output context after successful reset device cmd:\n");
3588 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, last_freed_endpoint
);
3592 xhci_free_command(xhci
, reset_device_cmd
);
3597 * At this point, the struct usb_device is about to go away, the device has
3598 * disconnected, and all traffic has been stopped and the endpoints have been
3599 * disabled. Free any HC data structures associated with that device.
3601 void xhci_free_dev(struct usb_hcd
*hcd
, struct usb_device
*udev
)
3603 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
3604 struct xhci_virt_device
*virt_dev
;
3605 unsigned long flags
;
3608 struct xhci_command
*command
;
3610 command
= xhci_alloc_command(xhci
, false, false, GFP_KERNEL
);
3614 #ifndef CONFIG_USB_DEFAULT_PERSIST
3616 * We called pm_runtime_get_noresume when the device was attached.
3617 * Decrement the counter here to allow controller to runtime suspend
3618 * if no devices remain.
3620 if (xhci
->quirks
& XHCI_RESET_ON_RESUME
)
3621 pm_runtime_put_noidle(hcd
->self
.controller
);
3624 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, true, __func__
);
3625 /* If the host is halted due to driver unload, we still need to free the
3628 if (ret
<= 0 && ret
!= -ENODEV
) {
3633 virt_dev
= xhci
->devs
[udev
->slot_id
];
3635 /* Stop any wayward timer functions (which may grab the lock) */
3636 for (i
= 0; i
< 31; ++i
) {
3637 virt_dev
->eps
[i
].ep_state
&= ~EP_HALT_PENDING
;
3638 del_timer_sync(&virt_dev
->eps
[i
].stop_cmd_timer
);
3641 spin_lock_irqsave(&xhci
->lock
, flags
);
3642 /* Don't disable the slot if the host controller is dead. */
3643 state
= readl(&xhci
->op_regs
->status
);
3644 if (state
== 0xffffffff || (xhci
->xhc_state
& XHCI_STATE_DYING
) ||
3645 (xhci
->xhc_state
& XHCI_STATE_HALTED
)) {
3646 xhci_free_virt_device(xhci
, udev
->slot_id
);
3647 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3652 if (xhci_queue_slot_control(xhci
, command
, TRB_DISABLE_SLOT
,
3654 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3655 xhci_dbg(xhci
, "FIXME: allocate a command ring segment\n");
3658 xhci_ring_cmd_db(xhci
);
3659 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3662 * Event command completion handler will free any data structures
3663 * associated with the slot. XXX Can free sleep?
3668 * Checks if we have enough host controller resources for the default control
3671 * Must be called with xhci->lock held.
3673 static int xhci_reserve_host_control_ep_resources(struct xhci_hcd
*xhci
)
3675 if (xhci
->num_active_eps
+ 1 > xhci
->limit_active_eps
) {
3676 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
3677 "Not enough ep ctxs: "
3678 "%u active, need to add 1, limit is %u.",
3679 xhci
->num_active_eps
, xhci
->limit_active_eps
);
3682 xhci
->num_active_eps
+= 1;
3683 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
3684 "Adding 1 ep ctx, %u now active.",
3685 xhci
->num_active_eps
);
3691 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3692 * timed out, or allocating memory failed. Returns 1 on success.
3694 int xhci_alloc_dev(struct usb_hcd
*hcd
, struct usb_device
*udev
)
3696 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
3697 unsigned long flags
;
3699 struct xhci_command
*command
;
3701 command
= xhci_alloc_command(xhci
, false, false, GFP_KERNEL
);
3705 /* xhci->slot_id and xhci->addr_dev are not thread-safe */
3706 mutex_lock(&xhci
->mutex
);
3707 spin_lock_irqsave(&xhci
->lock
, flags
);
3708 command
->completion
= &xhci
->addr_dev
;
3709 ret
= xhci_queue_slot_control(xhci
, command
, TRB_ENABLE_SLOT
, 0);
3711 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3712 mutex_unlock(&xhci
->mutex
);
3713 xhci_dbg(xhci
, "FIXME: allocate a command ring segment\n");
3717 xhci_ring_cmd_db(xhci
);
3718 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3720 wait_for_completion(command
->completion
);
3721 slot_id
= xhci
->slot_id
;
3722 mutex_unlock(&xhci
->mutex
);
3724 if (!slot_id
|| command
->status
!= COMP_SUCCESS
) {
3725 xhci_err(xhci
, "Error while assigning device slot ID\n");
3726 xhci_err(xhci
, "Max number of devices this xHCI host supports is %u.\n",
3728 readl(&xhci
->cap_regs
->hcs_params1
)));
3733 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
)) {
3734 spin_lock_irqsave(&xhci
->lock
, flags
);
3735 ret
= xhci_reserve_host_control_ep_resources(xhci
);
3737 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3738 xhci_warn(xhci
, "Not enough host resources, "
3739 "active endpoint contexts = %u\n",
3740 xhci
->num_active_eps
);
3743 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3745 /* Use GFP_NOIO, since this function can be called from
3746 * xhci_discover_or_reset_device(), which may be called as part of
3747 * mass storage driver error handling.
3749 if (!xhci_alloc_virt_device(xhci
, slot_id
, udev
, GFP_NOIO
)) {
3750 xhci_warn(xhci
, "Could not allocate xHCI USB device data structures\n");
3753 udev
->slot_id
= slot_id
;
3755 #ifndef CONFIG_USB_DEFAULT_PERSIST
3757 * If resetting upon resume, we can't put the controller into runtime
3758 * suspend if there is a device attached.
3760 if (xhci
->quirks
& XHCI_RESET_ON_RESUME
)
3761 pm_runtime_get_noresume(hcd
->self
.controller
);
3766 /* Is this a LS or FS device under a HS hub? */
3767 /* Hub or peripherial? */
3771 /* Disable slot, if we can do it without mem alloc */
3772 spin_lock_irqsave(&xhci
->lock
, flags
);
3773 command
->completion
= NULL
;
3774 command
->status
= 0;
3775 if (!xhci_queue_slot_control(xhci
, command
, TRB_DISABLE_SLOT
,
3777 xhci_ring_cmd_db(xhci
);
3778 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3783 * Issue an Address Device command and optionally send a corresponding
3784 * SetAddress request to the device.
3786 static int xhci_setup_device(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3787 enum xhci_setup_dev setup
)
3789 const char *act
= setup
== SETUP_CONTEXT_ONLY
? "context" : "address";
3790 unsigned long flags
;
3791 struct xhci_virt_device
*virt_dev
;
3793 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
3794 struct xhci_slot_ctx
*slot_ctx
;
3795 struct xhci_input_control_ctx
*ctrl_ctx
;
3797 struct xhci_command
*command
= NULL
;
3799 mutex_lock(&xhci
->mutex
);
3801 if (xhci
->xhc_state
) /* dying or halted */
3804 if (!udev
->slot_id
) {
3805 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3806 "Bad Slot ID %d", udev
->slot_id
);
3811 virt_dev
= xhci
->devs
[udev
->slot_id
];
3813 if (WARN_ON(!virt_dev
)) {
3815 * In plug/unplug torture test with an NEC controller,
3816 * a zero-dereference was observed once due to virt_dev = 0.
3817 * Print useful debug rather than crash if it is observed again!
3819 xhci_warn(xhci
, "Virt dev invalid for slot_id 0x%x!\n",
3825 if (setup
== SETUP_CONTEXT_ONLY
) {
3826 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->out_ctx
);
3827 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx
->dev_state
)) ==
3828 SLOT_STATE_DEFAULT
) {
3829 xhci_dbg(xhci
, "Slot already in default state\n");
3834 command
= xhci_alloc_command(xhci
, false, false, GFP_KERNEL
);
3840 command
->in_ctx
= virt_dev
->in_ctx
;
3841 command
->completion
= &xhci
->addr_dev
;
3843 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->in_ctx
);
3844 ctrl_ctx
= xhci_get_input_control_ctx(virt_dev
->in_ctx
);
3846 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
3852 * If this is the first Set Address since device plug-in or
3853 * virt_device realloaction after a resume with an xHCI power loss,
3854 * then set up the slot context.
3856 if (!slot_ctx
->dev_info
)
3857 xhci_setup_addressable_virt_dev(xhci
, udev
);
3858 /* Otherwise, update the control endpoint ring enqueue pointer. */
3860 xhci_copy_ep0_dequeue_into_input_ctx(xhci
, udev
);
3861 ctrl_ctx
->add_flags
= cpu_to_le32(SLOT_FLAG
| EP0_FLAG
);
3862 ctrl_ctx
->drop_flags
= 0;
3864 xhci_dbg(xhci
, "Slot ID %d Input Context:\n", udev
->slot_id
);
3865 xhci_dbg_ctx(xhci
, virt_dev
->in_ctx
, 2);
3866 trace_xhci_address_ctx(xhci
, virt_dev
->in_ctx
,
3867 le32_to_cpu(slot_ctx
->dev_info
) >> 27);
3869 spin_lock_irqsave(&xhci
->lock
, flags
);
3870 ret
= xhci_queue_address_device(xhci
, command
, virt_dev
->in_ctx
->dma
,
3871 udev
->slot_id
, setup
);
3873 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3874 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3875 "FIXME: allocate a command ring segment");
3878 xhci_ring_cmd_db(xhci
);
3879 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3881 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
3882 wait_for_completion(command
->completion
);
3884 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3885 * the SetAddress() "recovery interval" required by USB and aborting the
3886 * command on a timeout.
3888 switch (command
->status
) {
3889 case COMP_CMD_ABORT
:
3891 xhci_warn(xhci
, "Timeout while waiting for setup device command\n");
3894 case COMP_CTX_STATE
:
3896 xhci_err(xhci
, "Setup ERROR: setup %s command for slot %d.\n",
3897 act
, udev
->slot_id
);
3901 dev_warn(&udev
->dev
, "Device not responding to setup %s.\n", act
);
3905 dev_warn(&udev
->dev
,
3906 "ERROR: Incompatible device for setup %s command\n", act
);
3910 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3911 "Successful setup %s command", act
);
3915 "ERROR: unexpected setup %s command completion code 0x%x.\n",
3916 act
, command
->status
);
3917 xhci_dbg(xhci
, "Slot ID %d Output Context:\n", udev
->slot_id
);
3918 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 2);
3919 trace_xhci_address_ctx(xhci
, virt_dev
->out_ctx
, 1);
3925 temp_64
= xhci_read_64(xhci
, &xhci
->op_regs
->dcbaa_ptr
);
3926 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3927 "Op regs DCBAA ptr = %#016llx", temp_64
);
3928 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3929 "Slot ID %d dcbaa entry @%p = %#016llx",
3931 &xhci
->dcbaa
->dev_context_ptrs
[udev
->slot_id
],
3932 (unsigned long long)
3933 le64_to_cpu(xhci
->dcbaa
->dev_context_ptrs
[udev
->slot_id
]));
3934 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3935 "Output Context DMA address = %#08llx",
3936 (unsigned long long)virt_dev
->out_ctx
->dma
);
3937 xhci_dbg(xhci
, "Slot ID %d Input Context:\n", udev
->slot_id
);
3938 xhci_dbg_ctx(xhci
, virt_dev
->in_ctx
, 2);
3939 trace_xhci_address_ctx(xhci
, virt_dev
->in_ctx
,
3940 le32_to_cpu(slot_ctx
->dev_info
) >> 27);
3941 xhci_dbg(xhci
, "Slot ID %d Output Context:\n", udev
->slot_id
);
3942 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 2);
3944 * USB core uses address 1 for the roothubs, so we add one to the
3945 * address given back to us by the HC.
3947 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->out_ctx
);
3948 trace_xhci_address_ctx(xhci
, virt_dev
->out_ctx
,
3949 le32_to_cpu(slot_ctx
->dev_info
) >> 27);
3950 /* Zero the input context control for later use */
3951 ctrl_ctx
->add_flags
= 0;
3952 ctrl_ctx
->drop_flags
= 0;
3954 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3955 "Internal device address = %d",
3956 le32_to_cpu(slot_ctx
->dev_state
) & DEV_ADDR_MASK
);
3958 mutex_unlock(&xhci
->mutex
);
3963 int xhci_address_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
3965 return xhci_setup_device(hcd
, udev
, SETUP_CONTEXT_ADDRESS
);
3968 int xhci_enable_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
3970 return xhci_setup_device(hcd
, udev
, SETUP_CONTEXT_ONLY
);
3974 * Transfer the port index into real index in the HW port status
3975 * registers. Caculate offset between the port's PORTSC register
3976 * and port status base. Divide the number of per port register
3977 * to get the real index. The raw port number bases 1.
3979 int xhci_find_raw_port_number(struct usb_hcd
*hcd
, int port1
)
3981 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
3982 __le32 __iomem
*base_addr
= &xhci
->op_regs
->port_status_base
;
3983 __le32 __iomem
*addr
;
3986 if (hcd
->speed
< HCD_USB3
)
3987 addr
= xhci
->usb2_ports
[port1
- 1];
3989 addr
= xhci
->usb3_ports
[port1
- 1];
3991 raw_port
= (addr
- base_addr
)/NUM_PORT_REGS
+ 1;
3996 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
3997 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
3999 static int __maybe_unused
xhci_change_max_exit_latency(struct xhci_hcd
*xhci
,
4000 struct usb_device
*udev
, u16 max_exit_latency
)
4002 struct xhci_virt_device
*virt_dev
;
4003 struct xhci_command
*command
;
4004 struct xhci_input_control_ctx
*ctrl_ctx
;
4005 struct xhci_slot_ctx
*slot_ctx
;
4006 unsigned long flags
;
4009 spin_lock_irqsave(&xhci
->lock
, flags
);
4011 virt_dev
= xhci
->devs
[udev
->slot_id
];
4014 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
4015 * xHC was re-initialized. Exit latency will be set later after
4016 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
4019 if (!virt_dev
|| max_exit_latency
== virt_dev
->current_mel
) {
4020 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4024 /* Attempt to issue an Evaluate Context command to change the MEL. */
4025 command
= xhci
->lpm_command
;
4026 ctrl_ctx
= xhci_get_input_control_ctx(command
->in_ctx
);
4028 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4029 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
4034 xhci_slot_copy(xhci
, command
->in_ctx
, virt_dev
->out_ctx
);
4035 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4037 ctrl_ctx
->add_flags
|= cpu_to_le32(SLOT_FLAG
);
4038 slot_ctx
= xhci_get_slot_ctx(xhci
, command
->in_ctx
);
4039 slot_ctx
->dev_info2
&= cpu_to_le32(~((u32
) MAX_EXIT
));
4040 slot_ctx
->dev_info2
|= cpu_to_le32(max_exit_latency
);
4041 slot_ctx
->dev_state
= 0;
4043 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
4044 "Set up evaluate context for LPM MEL change.");
4045 xhci_dbg(xhci
, "Slot %u Input Context:\n", udev
->slot_id
);
4046 xhci_dbg_ctx(xhci
, command
->in_ctx
, 0);
4048 /* Issue and wait for the evaluate context command. */
4049 ret
= xhci_configure_endpoint(xhci
, udev
, command
,
4051 xhci_dbg(xhci
, "Slot %u Output Context:\n", udev
->slot_id
);
4052 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 0);
4055 spin_lock_irqsave(&xhci
->lock
, flags
);
4056 virt_dev
->current_mel
= max_exit_latency
;
4057 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4064 /* BESL to HIRD Encoding array for USB2 LPM */
4065 static int xhci_besl_encoding
[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
4066 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
4068 /* Calculate HIRD/BESL for USB2 PORTPMSC*/
4069 static int xhci_calculate_hird_besl(struct xhci_hcd
*xhci
,
4070 struct usb_device
*udev
)
4072 int u2del
, besl
, besl_host
;
4073 int besl_device
= 0;
4076 u2del
= HCS_U2_LATENCY(xhci
->hcs_params3
);
4077 field
= le32_to_cpu(udev
->bos
->ext_cap
->bmAttributes
);
4079 if (field
& USB_BESL_SUPPORT
) {
4080 for (besl_host
= 0; besl_host
< 16; besl_host
++) {
4081 if (xhci_besl_encoding
[besl_host
] >= u2del
)
4084 /* Use baseline BESL value as default */
4085 if (field
& USB_BESL_BASELINE_VALID
)
4086 besl_device
= USB_GET_BESL_BASELINE(field
);
4087 else if (field
& USB_BESL_DEEP_VALID
)
4088 besl_device
= USB_GET_BESL_DEEP(field
);
4093 besl_host
= (u2del
- 51) / 75 + 1;
4096 besl
= besl_host
+ besl_device
;
4103 /* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4104 static int xhci_calculate_usb2_hw_lpm_params(struct usb_device
*udev
)
4111 field
= le32_to_cpu(udev
->bos
->ext_cap
->bmAttributes
);
4113 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
4114 l1
= udev
->l1_params
.timeout
/ 256;
4116 /* device has preferred BESLD */
4117 if (field
& USB_BESL_DEEP_VALID
) {
4118 besld
= USB_GET_BESL_DEEP(field
);
4122 return PORT_BESLD(besld
) | PORT_L1_TIMEOUT(l1
) | PORT_HIRDM(hirdm
);
4125 int xhci_set_usb2_hardware_lpm(struct usb_hcd
*hcd
,
4126 struct usb_device
*udev
, int enable
)
4128 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
4129 __le32 __iomem
**port_array
;
4130 __le32 __iomem
*pm_addr
, *hlpm_addr
;
4131 u32 pm_val
, hlpm_val
, field
;
4132 unsigned int port_num
;
4133 unsigned long flags
;
4134 int hird
, exit_latency
;
4137 if (hcd
->speed
>= HCD_USB3
|| !xhci
->hw_lpm_support
||
4141 if (!udev
->parent
|| udev
->parent
->parent
||
4142 udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
4145 if (udev
->usb2_hw_lpm_capable
!= 1)
4148 spin_lock_irqsave(&xhci
->lock
, flags
);
4150 port_array
= xhci
->usb2_ports
;
4151 port_num
= udev
->portnum
- 1;
4152 pm_addr
= port_array
[port_num
] + PORTPMSC
;
4153 pm_val
= readl(pm_addr
);
4154 hlpm_addr
= port_array
[port_num
] + PORTHLPMC
;
4155 field
= le32_to_cpu(udev
->bos
->ext_cap
->bmAttributes
);
4157 xhci_dbg(xhci
, "%s port %d USB2 hardware LPM\n",
4158 enable
? "enable" : "disable", port_num
+ 1);
4161 /* Host supports BESL timeout instead of HIRD */
4162 if (udev
->usb2_hw_lpm_besl_capable
) {
4163 /* if device doesn't have a preferred BESL value use a
4164 * default one which works with mixed HIRD and BESL
4165 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4167 if ((field
& USB_BESL_SUPPORT
) &&
4168 (field
& USB_BESL_BASELINE_VALID
))
4169 hird
= USB_GET_BESL_BASELINE(field
);
4171 hird
= udev
->l1_params
.besl
;
4173 exit_latency
= xhci_besl_encoding
[hird
];
4174 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4176 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4177 * input context for link powermanagement evaluate
4178 * context commands. It is protected by hcd->bandwidth
4179 * mutex and is shared by all devices. We need to set
4180 * the max ext latency in USB 2 BESL LPM as well, so
4181 * use the same mutex and xhci_change_max_exit_latency()
4183 mutex_lock(hcd
->bandwidth_mutex
);
4184 ret
= xhci_change_max_exit_latency(xhci
, udev
,
4186 mutex_unlock(hcd
->bandwidth_mutex
);
4190 spin_lock_irqsave(&xhci
->lock
, flags
);
4192 hlpm_val
= xhci_calculate_usb2_hw_lpm_params(udev
);
4193 writel(hlpm_val
, hlpm_addr
);
4197 hird
= xhci_calculate_hird_besl(xhci
, udev
);
4200 pm_val
&= ~PORT_HIRD_MASK
;
4201 pm_val
|= PORT_HIRD(hird
) | PORT_RWE
| PORT_L1DS(udev
->slot_id
);
4202 writel(pm_val
, pm_addr
);
4203 pm_val
= readl(pm_addr
);
4205 writel(pm_val
, pm_addr
);
4209 pm_val
&= ~(PORT_HLE
| PORT_RWE
| PORT_HIRD_MASK
| PORT_L1DS_MASK
);
4210 writel(pm_val
, pm_addr
);
4213 if (udev
->usb2_hw_lpm_besl_capable
) {
4214 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4215 mutex_lock(hcd
->bandwidth_mutex
);
4216 xhci_change_max_exit_latency(xhci
, udev
, 0);
4217 mutex_unlock(hcd
->bandwidth_mutex
);
4222 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4226 /* check if a usb2 port supports a given extened capability protocol
4227 * only USB2 ports extended protocol capability values are cached.
4228 * Return 1 if capability is supported
4230 static int xhci_check_usb2_port_capability(struct xhci_hcd
*xhci
, int port
,
4231 unsigned capability
)
4233 u32 port_offset
, port_count
;
4236 for (i
= 0; i
< xhci
->num_ext_caps
; i
++) {
4237 if (xhci
->ext_caps
[i
] & capability
) {
4238 /* port offsets starts at 1 */
4239 port_offset
= XHCI_EXT_PORT_OFF(xhci
->ext_caps
[i
]) - 1;
4240 port_count
= XHCI_EXT_PORT_COUNT(xhci
->ext_caps
[i
]);
4241 if (port
>= port_offset
&&
4242 port
< port_offset
+ port_count
)
4249 int xhci_update_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
4251 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
4252 int portnum
= udev
->portnum
- 1;
4254 if (hcd
->speed
>= HCD_USB3
|| !xhci
->sw_lpm_support
||
4258 /* we only support lpm for non-hub device connected to root hub yet */
4259 if (!udev
->parent
|| udev
->parent
->parent
||
4260 udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
4263 if (xhci
->hw_lpm_support
== 1 &&
4264 xhci_check_usb2_port_capability(
4265 xhci
, portnum
, XHCI_HLC
)) {
4266 udev
->usb2_hw_lpm_capable
= 1;
4267 udev
->l1_params
.timeout
= XHCI_L1_TIMEOUT
;
4268 udev
->l1_params
.besl
= XHCI_DEFAULT_BESL
;
4269 if (xhci_check_usb2_port_capability(xhci
, portnum
,
4271 udev
->usb2_hw_lpm_besl_capable
= 1;
4277 /*---------------------- USB 3.0 Link PM functions ------------------------*/
4279 /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4280 static unsigned long long xhci_service_interval_to_ns(
4281 struct usb_endpoint_descriptor
*desc
)
4283 return (1ULL << (desc
->bInterval
- 1)) * 125 * 1000;
4286 static u16
xhci_get_timeout_no_hub_lpm(struct usb_device
*udev
,
4287 enum usb3_link_state state
)
4289 unsigned long long sel
;
4290 unsigned long long pel
;
4291 unsigned int max_sel_pel
;
4296 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4297 sel
= DIV_ROUND_UP(udev
->u1_params
.sel
, 1000);
4298 pel
= DIV_ROUND_UP(udev
->u1_params
.pel
, 1000);
4299 max_sel_pel
= USB3_LPM_MAX_U1_SEL_PEL
;
4303 sel
= DIV_ROUND_UP(udev
->u2_params
.sel
, 1000);
4304 pel
= DIV_ROUND_UP(udev
->u2_params
.pel
, 1000);
4305 max_sel_pel
= USB3_LPM_MAX_U2_SEL_PEL
;
4309 dev_warn(&udev
->dev
, "%s: Can't get timeout for non-U1 or U2 state.\n",
4311 return USB3_LPM_DISABLED
;
4314 if (sel
<= max_sel_pel
&& pel
<= max_sel_pel
)
4315 return USB3_LPM_DEVICE_INITIATED
;
4317 if (sel
> max_sel_pel
)
4318 dev_dbg(&udev
->dev
, "Device-initiated %s disabled "
4319 "due to long SEL %llu ms\n",
4322 dev_dbg(&udev
->dev
, "Device-initiated %s disabled "
4323 "due to long PEL %llu ms\n",
4325 return USB3_LPM_DISABLED
;
4328 /* The U1 timeout should be the maximum of the following values:
4329 * - For control endpoints, U1 system exit latency (SEL) * 3
4330 * - For bulk endpoints, U1 SEL * 5
4331 * - For interrupt endpoints:
4332 * - Notification EPs, U1 SEL * 3
4333 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4334 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4336 static unsigned long long xhci_calculate_intel_u1_timeout(
4337 struct usb_device
*udev
,
4338 struct usb_endpoint_descriptor
*desc
)
4340 unsigned long long timeout_ns
;
4344 ep_type
= usb_endpoint_type(desc
);
4346 case USB_ENDPOINT_XFER_CONTROL
:
4347 timeout_ns
= udev
->u1_params
.sel
* 3;
4349 case USB_ENDPOINT_XFER_BULK
:
4350 timeout_ns
= udev
->u1_params
.sel
* 5;
4352 case USB_ENDPOINT_XFER_INT
:
4353 intr_type
= usb_endpoint_interrupt_type(desc
);
4354 if (intr_type
== USB_ENDPOINT_INTR_NOTIFICATION
) {
4355 timeout_ns
= udev
->u1_params
.sel
* 3;
4358 /* Otherwise the calculation is the same as isoc eps */
4359 case USB_ENDPOINT_XFER_ISOC
:
4360 timeout_ns
= xhci_service_interval_to_ns(desc
);
4361 timeout_ns
= DIV_ROUND_UP_ULL(timeout_ns
* 105, 100);
4362 if (timeout_ns
< udev
->u1_params
.sel
* 2)
4363 timeout_ns
= udev
->u1_params
.sel
* 2;
4372 /* Returns the hub-encoded U1 timeout value. */
4373 static u16
xhci_calculate_u1_timeout(struct xhci_hcd
*xhci
,
4374 struct usb_device
*udev
,
4375 struct usb_endpoint_descriptor
*desc
)
4377 unsigned long long timeout_ns
;
4379 if (xhci
->quirks
& XHCI_INTEL_HOST
)
4380 timeout_ns
= xhci_calculate_intel_u1_timeout(udev
, desc
);
4382 timeout_ns
= udev
->u1_params
.sel
;
4384 /* The U1 timeout is encoded in 1us intervals.
4385 * Don't return a timeout of zero, because that's USB3_LPM_DISABLED.
4387 if (timeout_ns
== USB3_LPM_DISABLED
)
4390 timeout_ns
= DIV_ROUND_UP_ULL(timeout_ns
, 1000);
4392 /* If the necessary timeout value is bigger than what we can set in the
4393 * USB 3.0 hub, we have to disable hub-initiated U1.
4395 if (timeout_ns
<= USB3_LPM_U1_MAX_TIMEOUT
)
4397 dev_dbg(&udev
->dev
, "Hub-initiated U1 disabled "
4398 "due to long timeout %llu ms\n", timeout_ns
);
4399 return xhci_get_timeout_no_hub_lpm(udev
, USB3_LPM_U1
);
4402 /* The U2 timeout should be the maximum of:
4403 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4404 * - largest bInterval of any active periodic endpoint (to avoid going
4405 * into lower power link states between intervals).
4406 * - the U2 Exit Latency of the device
4408 static unsigned long long xhci_calculate_intel_u2_timeout(
4409 struct usb_device
*udev
,
4410 struct usb_endpoint_descriptor
*desc
)
4412 unsigned long long timeout_ns
;
4413 unsigned long long u2_del_ns
;
4415 timeout_ns
= 10 * 1000 * 1000;
4417 if ((usb_endpoint_xfer_int(desc
) || usb_endpoint_xfer_isoc(desc
)) &&
4418 (xhci_service_interval_to_ns(desc
) > timeout_ns
))
4419 timeout_ns
= xhci_service_interval_to_ns(desc
);
4421 u2_del_ns
= le16_to_cpu(udev
->bos
->ss_cap
->bU2DevExitLat
) * 1000ULL;
4422 if (u2_del_ns
> timeout_ns
)
4423 timeout_ns
= u2_del_ns
;
4428 /* Returns the hub-encoded U2 timeout value. */
4429 static u16
xhci_calculate_u2_timeout(struct xhci_hcd
*xhci
,
4430 struct usb_device
*udev
,
4431 struct usb_endpoint_descriptor
*desc
)
4433 unsigned long long timeout_ns
;
4435 if (xhci
->quirks
& XHCI_INTEL_HOST
)
4436 timeout_ns
= xhci_calculate_intel_u2_timeout(udev
, desc
);
4438 timeout_ns
= udev
->u2_params
.sel
;
4440 /* The U2 timeout is encoded in 256us intervals */
4441 timeout_ns
= DIV_ROUND_UP_ULL(timeout_ns
, 256 * 1000);
4442 /* If the necessary timeout value is bigger than what we can set in the
4443 * USB 3.0 hub, we have to disable hub-initiated U2.
4445 if (timeout_ns
<= USB3_LPM_U2_MAX_TIMEOUT
)
4447 dev_dbg(&udev
->dev
, "Hub-initiated U2 disabled "
4448 "due to long timeout %llu ms\n", timeout_ns
);
4449 return xhci_get_timeout_no_hub_lpm(udev
, USB3_LPM_U2
);
4452 static u16
xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd
*xhci
,
4453 struct usb_device
*udev
,
4454 struct usb_endpoint_descriptor
*desc
,
4455 enum usb3_link_state state
,
4458 if (state
== USB3_LPM_U1
)
4459 return xhci_calculate_u1_timeout(xhci
, udev
, desc
);
4460 else if (state
== USB3_LPM_U2
)
4461 return xhci_calculate_u2_timeout(xhci
, udev
, desc
);
4463 return USB3_LPM_DISABLED
;
4466 static int xhci_update_timeout_for_endpoint(struct xhci_hcd
*xhci
,
4467 struct usb_device
*udev
,
4468 struct usb_endpoint_descriptor
*desc
,
4469 enum usb3_link_state state
,
4474 alt_timeout
= xhci_call_host_update_timeout_for_endpoint(xhci
, udev
,
4475 desc
, state
, timeout
);
4477 /* If we found we can't enable hub-initiated LPM, or
4478 * the U1 or U2 exit latency was too high to allow
4479 * device-initiated LPM as well, just stop searching.
4481 if (alt_timeout
== USB3_LPM_DISABLED
||
4482 alt_timeout
== USB3_LPM_DEVICE_INITIATED
) {
4483 *timeout
= alt_timeout
;
4486 if (alt_timeout
> *timeout
)
4487 *timeout
= alt_timeout
;
4491 static int xhci_update_timeout_for_interface(struct xhci_hcd
*xhci
,
4492 struct usb_device
*udev
,
4493 struct usb_host_interface
*alt
,
4494 enum usb3_link_state state
,
4499 for (j
= 0; j
< alt
->desc
.bNumEndpoints
; j
++) {
4500 if (xhci_update_timeout_for_endpoint(xhci
, udev
,
4501 &alt
->endpoint
[j
].desc
, state
, timeout
))
4508 static int xhci_check_intel_tier_policy(struct usb_device
*udev
,
4509 enum usb3_link_state state
)
4511 struct usb_device
*parent
;
4512 unsigned int num_hubs
;
4514 if (state
== USB3_LPM_U2
)
4517 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4518 for (parent
= udev
->parent
, num_hubs
= 0; parent
->parent
;
4519 parent
= parent
->parent
)
4525 dev_dbg(&udev
->dev
, "Disabling U1 link state for device"
4526 " below second-tier hub.\n");
4527 dev_dbg(&udev
->dev
, "Plug device into first-tier hub "
4528 "to decrease power consumption.\n");
4532 static int xhci_check_tier_policy(struct xhci_hcd
*xhci
,
4533 struct usb_device
*udev
,
4534 enum usb3_link_state state
)
4536 if (xhci
->quirks
& XHCI_INTEL_HOST
)
4537 return xhci_check_intel_tier_policy(udev
, state
);
4542 /* Returns the U1 or U2 timeout that should be enabled.
4543 * If the tier check or timeout setting functions return with a non-zero exit
4544 * code, that means the timeout value has been finalized and we shouldn't look
4545 * at any more endpoints.
4547 static u16
xhci_calculate_lpm_timeout(struct usb_hcd
*hcd
,
4548 struct usb_device
*udev
, enum usb3_link_state state
)
4550 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
4551 struct usb_host_config
*config
;
4554 u16 timeout
= USB3_LPM_DISABLED
;
4556 if (state
== USB3_LPM_U1
)
4558 else if (state
== USB3_LPM_U2
)
4561 dev_warn(&udev
->dev
, "Can't enable unknown link state %i\n",
4566 if (xhci_check_tier_policy(xhci
, udev
, state
) < 0)
4569 /* Gather some information about the currently installed configuration
4570 * and alternate interface settings.
4572 if (xhci_update_timeout_for_endpoint(xhci
, udev
, &udev
->ep0
.desc
,
4576 config
= udev
->actconfig
;
4580 for (i
= 0; i
< config
->desc
.bNumInterfaces
; i
++) {
4581 struct usb_driver
*driver
;
4582 struct usb_interface
*intf
= config
->interface
[i
];
4587 /* Check if any currently bound drivers want hub-initiated LPM
4590 if (intf
->dev
.driver
) {
4591 driver
= to_usb_driver(intf
->dev
.driver
);
4592 if (driver
&& driver
->disable_hub_initiated_lpm
) {
4593 dev_dbg(&udev
->dev
, "Hub-initiated %s disabled "
4594 "at request of driver %s\n",
4595 state_name
, driver
->name
);
4596 return xhci_get_timeout_no_hub_lpm(udev
, state
);
4600 /* Not sure how this could happen... */
4601 if (!intf
->cur_altsetting
)
4604 if (xhci_update_timeout_for_interface(xhci
, udev
,
4605 intf
->cur_altsetting
,
4612 static int calculate_max_exit_latency(struct usb_device
*udev
,
4613 enum usb3_link_state state_changed
,
4614 u16 hub_encoded_timeout
)
4616 unsigned long long u1_mel_us
= 0;
4617 unsigned long long u2_mel_us
= 0;
4618 unsigned long long mel_us
= 0;
4624 disabling_u1
= (state_changed
== USB3_LPM_U1
&&
4625 hub_encoded_timeout
== USB3_LPM_DISABLED
);
4626 disabling_u2
= (state_changed
== USB3_LPM_U2
&&
4627 hub_encoded_timeout
== USB3_LPM_DISABLED
);
4629 enabling_u1
= (state_changed
== USB3_LPM_U1
&&
4630 hub_encoded_timeout
!= USB3_LPM_DISABLED
);
4631 enabling_u2
= (state_changed
== USB3_LPM_U2
&&
4632 hub_encoded_timeout
!= USB3_LPM_DISABLED
);
4634 /* If U1 was already enabled and we're not disabling it,
4635 * or we're going to enable U1, account for the U1 max exit latency.
4637 if ((udev
->u1_params
.timeout
!= USB3_LPM_DISABLED
&& !disabling_u1
) ||
4639 u1_mel_us
= DIV_ROUND_UP(udev
->u1_params
.mel
, 1000);
4640 if ((udev
->u2_params
.timeout
!= USB3_LPM_DISABLED
&& !disabling_u2
) ||
4642 u2_mel_us
= DIV_ROUND_UP(udev
->u2_params
.mel
, 1000);
4644 if (u1_mel_us
> u2_mel_us
)
4648 /* xHCI host controller max exit latency field is only 16 bits wide. */
4649 if (mel_us
> MAX_EXIT
) {
4650 dev_warn(&udev
->dev
, "Link PM max exit latency of %lluus "
4651 "is too big.\n", mel_us
);
4657 /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4658 int xhci_enable_usb3_lpm_timeout(struct usb_hcd
*hcd
,
4659 struct usb_device
*udev
, enum usb3_link_state state
)
4661 struct xhci_hcd
*xhci
;
4662 u16 hub_encoded_timeout
;
4666 xhci
= hcd_to_xhci(hcd
);
4667 /* The LPM timeout values are pretty host-controller specific, so don't
4668 * enable hub-initiated timeouts unless the vendor has provided
4669 * information about their timeout algorithm.
4671 if (!xhci
|| !(xhci
->quirks
& XHCI_LPM_SUPPORT
) ||
4672 !xhci
->devs
[udev
->slot_id
])
4673 return USB3_LPM_DISABLED
;
4675 hub_encoded_timeout
= xhci_calculate_lpm_timeout(hcd
, udev
, state
);
4676 mel
= calculate_max_exit_latency(udev
, state
, hub_encoded_timeout
);
4678 /* Max Exit Latency is too big, disable LPM. */
4679 hub_encoded_timeout
= USB3_LPM_DISABLED
;
4683 ret
= xhci_change_max_exit_latency(xhci
, udev
, mel
);
4686 return hub_encoded_timeout
;
4689 int xhci_disable_usb3_lpm_timeout(struct usb_hcd
*hcd
,
4690 struct usb_device
*udev
, enum usb3_link_state state
)
4692 struct xhci_hcd
*xhci
;
4695 xhci
= hcd_to_xhci(hcd
);
4696 if (!xhci
|| !(xhci
->quirks
& XHCI_LPM_SUPPORT
) ||
4697 !xhci
->devs
[udev
->slot_id
])
4700 mel
= calculate_max_exit_latency(udev
, state
, USB3_LPM_DISABLED
);
4701 return xhci_change_max_exit_latency(xhci
, udev
, mel
);
4703 #else /* CONFIG_PM */
4705 int xhci_set_usb2_hardware_lpm(struct usb_hcd
*hcd
,
4706 struct usb_device
*udev
, int enable
)
4711 int xhci_update_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
4716 int xhci_enable_usb3_lpm_timeout(struct usb_hcd
*hcd
,
4717 struct usb_device
*udev
, enum usb3_link_state state
)
4719 return USB3_LPM_DISABLED
;
4722 int xhci_disable_usb3_lpm_timeout(struct usb_hcd
*hcd
,
4723 struct usb_device
*udev
, enum usb3_link_state state
)
4727 #endif /* CONFIG_PM */
4729 /*-------------------------------------------------------------------------*/
4731 /* Once a hub descriptor is fetched for a device, we need to update the xHC's
4732 * internal data structures for the device.
4734 int xhci_update_hub_device(struct usb_hcd
*hcd
, struct usb_device
*hdev
,
4735 struct usb_tt
*tt
, gfp_t mem_flags
)
4737 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
4738 struct xhci_virt_device
*vdev
;
4739 struct xhci_command
*config_cmd
;
4740 struct xhci_input_control_ctx
*ctrl_ctx
;
4741 struct xhci_slot_ctx
*slot_ctx
;
4742 unsigned long flags
;
4743 unsigned think_time
;
4746 /* Ignore root hubs */
4750 vdev
= xhci
->devs
[hdev
->slot_id
];
4752 xhci_warn(xhci
, "Cannot update hub desc for unknown device.\n");
4755 config_cmd
= xhci_alloc_command(xhci
, true, true, mem_flags
);
4757 xhci_dbg(xhci
, "Could not allocate xHCI command structure.\n");
4760 ctrl_ctx
= xhci_get_input_control_ctx(config_cmd
->in_ctx
);
4762 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
4764 xhci_free_command(xhci
, config_cmd
);
4768 spin_lock_irqsave(&xhci
->lock
, flags
);
4769 if (hdev
->speed
== USB_SPEED_HIGH
&&
4770 xhci_alloc_tt_info(xhci
, vdev
, hdev
, tt
, GFP_ATOMIC
)) {
4771 xhci_dbg(xhci
, "Could not allocate xHCI TT structure.\n");
4772 xhci_free_command(xhci
, config_cmd
);
4773 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4777 xhci_slot_copy(xhci
, config_cmd
->in_ctx
, vdev
->out_ctx
);
4778 ctrl_ctx
->add_flags
|= cpu_to_le32(SLOT_FLAG
);
4779 slot_ctx
= xhci_get_slot_ctx(xhci
, config_cmd
->in_ctx
);
4780 slot_ctx
->dev_info
|= cpu_to_le32(DEV_HUB
);
4782 * refer to section 6.2.2: MTT should be 0 for full speed hub,
4783 * but it may be already set to 1 when setup an xHCI virtual
4784 * device, so clear it anyway.
4787 slot_ctx
->dev_info
|= cpu_to_le32(DEV_MTT
);
4788 else if (hdev
->speed
== USB_SPEED_FULL
)
4789 slot_ctx
->dev_info
&= cpu_to_le32(~DEV_MTT
);
4791 if (xhci
->hci_version
> 0x95) {
4792 xhci_dbg(xhci
, "xHCI version %x needs hub "
4793 "TT think time and number of ports\n",
4794 (unsigned int) xhci
->hci_version
);
4795 slot_ctx
->dev_info2
|= cpu_to_le32(XHCI_MAX_PORTS(hdev
->maxchild
));
4796 /* Set TT think time - convert from ns to FS bit times.
4797 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4798 * 2 = 24 FS bit times, 3 = 32 FS bit times.
4800 * xHCI 1.0: this field shall be 0 if the device is not a
4803 think_time
= tt
->think_time
;
4804 if (think_time
!= 0)
4805 think_time
= (think_time
/ 666) - 1;
4806 if (xhci
->hci_version
< 0x100 || hdev
->speed
== USB_SPEED_HIGH
)
4807 slot_ctx
->tt_info
|=
4808 cpu_to_le32(TT_THINK_TIME(think_time
));
4810 xhci_dbg(xhci
, "xHCI version %x doesn't need hub "
4811 "TT think time or number of ports\n",
4812 (unsigned int) xhci
->hci_version
);
4814 slot_ctx
->dev_state
= 0;
4815 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4817 xhci_dbg(xhci
, "Set up %s for hub device.\n",
4818 (xhci
->hci_version
> 0x95) ?
4819 "configure endpoint" : "evaluate context");
4820 xhci_dbg(xhci
, "Slot %u Input Context:\n", hdev
->slot_id
);
4821 xhci_dbg_ctx(xhci
, config_cmd
->in_ctx
, 0);
4823 /* Issue and wait for the configure endpoint or
4824 * evaluate context command.
4826 if (xhci
->hci_version
> 0x95)
4827 ret
= xhci_configure_endpoint(xhci
, hdev
, config_cmd
,
4830 ret
= xhci_configure_endpoint(xhci
, hdev
, config_cmd
,
4833 xhci_dbg(xhci
, "Slot %u Output Context:\n", hdev
->slot_id
);
4834 xhci_dbg_ctx(xhci
, vdev
->out_ctx
, 0);
4836 xhci_free_command(xhci
, config_cmd
);
4840 int xhci_get_frame(struct usb_hcd
*hcd
)
4842 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
4843 /* EHCI mods by the periodic size. Why? */
4844 return readl(&xhci
->run_regs
->microframe_index
) >> 3;
4847 int xhci_gen_setup(struct usb_hcd
*hcd
, xhci_get_quirks_t get_quirks
)
4849 struct xhci_hcd
*xhci
;
4850 struct device
*dev
= hcd
->self
.controller
;
4853 /* Accept arbitrarily long scatter-gather lists */
4854 hcd
->self
.sg_tablesize
= ~0;
4856 /* support to build packet from discontinuous buffers */
4857 hcd
->self
.no_sg_constraint
= 1;
4859 /* XHCI controllers don't stop the ep queue on short packets :| */
4860 hcd
->self
.no_stop_on_short
= 1;
4862 xhci
= hcd_to_xhci(hcd
);
4864 if (usb_hcd_is_primary_hcd(hcd
)) {
4865 xhci
->main_hcd
= hcd
;
4866 /* Mark the first roothub as being USB 2.0.
4867 * The xHCI driver will register the USB 3.0 roothub.
4869 hcd
->speed
= HCD_USB2
;
4870 hcd
->self
.root_hub
->speed
= USB_SPEED_HIGH
;
4872 * USB 2.0 roothub under xHCI has an integrated TT,
4873 * (rate matching hub) as opposed to having an OHCI/UHCI
4874 * companion controller.
4878 if (xhci
->sbrn
== 0x31) {
4879 xhci_info(xhci
, "Host supports USB 3.1 Enhanced SuperSpeed\n");
4880 hcd
->speed
= HCD_USB31
;
4882 /* xHCI private pointer was set in xhci_pci_probe for the second
4883 * registered roothub.
4888 mutex_init(&xhci
->mutex
);
4889 xhci
->cap_regs
= hcd
->regs
;
4890 xhci
->op_regs
= hcd
->regs
+
4891 HC_LENGTH(readl(&xhci
->cap_regs
->hc_capbase
));
4892 xhci
->run_regs
= hcd
->regs
+
4893 (readl(&xhci
->cap_regs
->run_regs_off
) & RTSOFF_MASK
);
4894 /* Cache read-only capability registers */
4895 xhci
->hcs_params1
= readl(&xhci
->cap_regs
->hcs_params1
);
4896 xhci
->hcs_params2
= readl(&xhci
->cap_regs
->hcs_params2
);
4897 xhci
->hcs_params3
= readl(&xhci
->cap_regs
->hcs_params3
);
4898 xhci
->hcc_params
= readl(&xhci
->cap_regs
->hc_capbase
);
4899 xhci
->hci_version
= HC_VERSION(xhci
->hcc_params
);
4900 xhci
->hcc_params
= readl(&xhci
->cap_regs
->hcc_params
);
4901 if (xhci
->hci_version
> 0x100)
4902 xhci
->hcc_params2
= readl(&xhci
->cap_regs
->hcc_params2
);
4903 xhci_print_registers(xhci
);
4905 xhci
->quirks
= quirks
;
4907 get_quirks(dev
, xhci
);
4909 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4910 * success event after a short transfer. This quirk will ignore such
4913 if (xhci
->hci_version
> 0x96)
4914 xhci
->quirks
|= XHCI_SPURIOUS_SUCCESS
;
4916 /* Make sure the HC is halted. */
4917 retval
= xhci_halt(xhci
);
4921 xhci_dbg(xhci
, "Resetting HCD\n");
4922 /* Reset the internal HC memory state and registers. */
4923 retval
= xhci_reset(xhci
);
4926 xhci_dbg(xhci
, "Reset complete\n");
4928 /* Set dma_mask and coherent_dma_mask to 64-bits,
4929 * if xHC supports 64-bit addressing */
4930 if (HCC_64BIT_ADDR(xhci
->hcc_params
) &&
4931 !dma_set_mask(dev
, DMA_BIT_MASK(64))) {
4932 xhci_dbg(xhci
, "Enabling 64-bit DMA addresses.\n");
4933 dma_set_coherent_mask(dev
, DMA_BIT_MASK(64));
4936 * This is to avoid error in cases where a 32-bit USB
4937 * controller is used on a 64-bit capable system.
4939 retval
= dma_set_mask(dev
, DMA_BIT_MASK(32));
4942 xhci_dbg(xhci
, "Enabling 32-bit DMA addresses.\n");
4943 dma_set_coherent_mask(dev
, DMA_BIT_MASK(32));
4946 xhci_dbg(xhci
, "Calling HCD init\n");
4947 /* Initialize HCD and host controller data structures. */
4948 retval
= xhci_init(hcd
);
4951 xhci_dbg(xhci
, "Called HCD init\n");
4953 xhci_info(xhci
, "hcc params 0x%08x hci version 0x%x quirks 0x%08x\n",
4954 xhci
->hcc_params
, xhci
->hci_version
, xhci
->quirks
);
4958 EXPORT_SYMBOL_GPL(xhci_gen_setup
);
4960 static const struct hc_driver xhci_hc_driver
= {
4961 .description
= "xhci-hcd",
4962 .product_desc
= "xHCI Host Controller",
4963 .hcd_priv_size
= sizeof(struct xhci_hcd
*),
4966 * generic hardware linkage
4969 .flags
= HCD_MEMORY
| HCD_USB3
| HCD_SHARED
,
4972 * basic lifecycle operations
4974 .reset
= NULL
, /* set in xhci_init_driver() */
4977 .shutdown
= xhci_shutdown
,
4980 * managing i/o requests and associated device resources
4982 .urb_enqueue
= xhci_urb_enqueue
,
4983 .urb_dequeue
= xhci_urb_dequeue
,
4984 .alloc_dev
= xhci_alloc_dev
,
4985 .free_dev
= xhci_free_dev
,
4986 .alloc_streams
= xhci_alloc_streams
,
4987 .free_streams
= xhci_free_streams
,
4988 .add_endpoint
= xhci_add_endpoint
,
4989 .drop_endpoint
= xhci_drop_endpoint
,
4990 .endpoint_reset
= xhci_endpoint_reset
,
4991 .check_bandwidth
= xhci_check_bandwidth
,
4992 .reset_bandwidth
= xhci_reset_bandwidth
,
4993 .address_device
= xhci_address_device
,
4994 .enable_device
= xhci_enable_device
,
4995 .update_hub_device
= xhci_update_hub_device
,
4996 .reset_device
= xhci_discover_or_reset_device
,
4999 * scheduling support
5001 .get_frame_number
= xhci_get_frame
,
5006 .hub_control
= xhci_hub_control
,
5007 .hub_status_data
= xhci_hub_status_data
,
5008 .bus_suspend
= xhci_bus_suspend
,
5009 .bus_resume
= xhci_bus_resume
,
5012 * call back when device connected and addressed
5014 .update_device
= xhci_update_device
,
5015 .set_usb2_hw_lpm
= xhci_set_usb2_hardware_lpm
,
5016 .enable_usb3_lpm_timeout
= xhci_enable_usb3_lpm_timeout
,
5017 .disable_usb3_lpm_timeout
= xhci_disable_usb3_lpm_timeout
,
5018 .find_raw_port_number
= xhci_find_raw_port_number
,
5021 void xhci_init_driver(struct hc_driver
*drv
,
5022 const struct xhci_driver_overrides
*over
)
5026 /* Copy the generic table to drv then apply the overrides */
5027 *drv
= xhci_hc_driver
;
5030 drv
->hcd_priv_size
+= over
->extra_priv_size
;
5032 drv
->reset
= over
->reset
;
5034 drv
->start
= over
->start
;
5037 EXPORT_SYMBOL_GPL(xhci_init_driver
);
5039 MODULE_DESCRIPTION(DRIVER_DESC
);
5040 MODULE_AUTHOR(DRIVER_AUTHOR
);
5041 MODULE_LICENSE("GPL");
5043 static int __init
xhci_hcd_init(void)
5046 * Check the compiler generated sizes of structures that must be laid
5047 * out in specific ways for hardware access.
5049 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array
) != 256*32/8);
5050 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx
) != 8*32/8);
5051 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx
) != 8*32/8);
5052 /* xhci_device_control has eight fields, and also
5053 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
5055 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx
) != 4*32/8);
5056 BUILD_BUG_ON(sizeof(union xhci_trb
) != 4*32/8);
5057 BUILD_BUG_ON(sizeof(struct xhci_erst_entry
) != 4*32/8);
5058 BUILD_BUG_ON(sizeof(struct xhci_cap_regs
) != 8*32/8);
5059 BUILD_BUG_ON(sizeof(struct xhci_intr_reg
) != 8*32/8);
5060 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
5061 BUILD_BUG_ON(sizeof(struct xhci_run_regs
) != (8+8*128)*32/8);
5066 * If an init function is provided, an exit function must also be provided
5067 * to allow module unload.
5069 static void __exit
xhci_hcd_fini(void) { }
5071 module_init(xhci_hcd_init
);
5072 module_exit(xhci_hcd_fini
);