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"
36 #define DRIVER_AUTHOR "Sarah Sharp"
37 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
39 #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
41 /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
42 static int link_quirk
;
43 module_param(link_quirk
, int, S_IRUGO
| S_IWUSR
);
44 MODULE_PARM_DESC(link_quirk
, "Don't clear the chain bit on a link TRB");
46 static unsigned int quirks
;
47 module_param(quirks
, uint
, S_IRUGO
);
48 MODULE_PARM_DESC(quirks
, "Bit flags for quirks to be enabled as default");
50 /* TODO: copied from ehci-hcd.c - can this be refactored? */
52 * xhci_handshake - spin reading hc until handshake completes or fails
53 * @ptr: address of hc register to be read
54 * @mask: bits to look at in result of read
55 * @done: value of those bits when handshake succeeds
56 * @usec: timeout in microseconds
58 * Returns negative errno, or zero on success
60 * Success happens when the "mask" bits have the specified value (hardware
61 * handshake done). There are two failure modes: "usec" have passed (major
62 * hardware flakeout), or the register reads as all-ones (hardware removed).
64 int xhci_handshake(void __iomem
*ptr
, u32 mask
, u32 done
, int usec
)
70 if (result
== ~(u32
)0) /* card removed */
82 * Disable interrupts and begin the xHCI halting process.
84 void xhci_quiesce(struct xhci_hcd
*xhci
)
91 halted
= readl(&xhci
->op_regs
->status
) & STS_HALT
;
95 cmd
= readl(&xhci
->op_regs
->command
);
97 writel(cmd
, &xhci
->op_regs
->command
);
101 * Force HC into halt state.
103 * Disable any IRQs and clear the run/stop bit.
104 * HC will complete any current and actively pipelined transactions, and
105 * should halt within 16 ms of the run/stop bit being cleared.
106 * Read HC Halted bit in the status register to see when the HC is finished.
108 int xhci_halt(struct xhci_hcd
*xhci
)
111 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "// Halt the HC");
114 ret
= xhci_handshake(&xhci
->op_regs
->status
,
115 STS_HALT
, STS_HALT
, XHCI_MAX_HALT_USEC
);
117 xhci
->xhc_state
|= XHCI_STATE_HALTED
;
118 xhci
->cmd_ring_state
= CMD_RING_STATE_STOPPED
;
120 xhci_warn(xhci
, "Host not halted after %u microseconds.\n",
126 * Set the run bit and wait for the host to be running.
128 static int xhci_start(struct xhci_hcd
*xhci
)
133 temp
= readl(&xhci
->op_regs
->command
);
135 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "// Turn on HC, cmd = 0x%x.",
137 writel(temp
, &xhci
->op_regs
->command
);
140 * Wait for the HCHalted Status bit to be 0 to indicate the host is
143 ret
= xhci_handshake(&xhci
->op_regs
->status
,
144 STS_HALT
, 0, XHCI_MAX_HALT_USEC
);
145 if (ret
== -ETIMEDOUT
)
146 xhci_err(xhci
, "Host took too long to start, "
147 "waited %u microseconds.\n",
150 /* clear state flags. Including dying, halted or removing */
159 * This resets pipelines, timers, counters, state machines, etc.
160 * Transactions will be terminated immediately, and operational registers
161 * will be set to their defaults.
163 int xhci_reset(struct xhci_hcd
*xhci
)
169 state
= readl(&xhci
->op_regs
->status
);
170 if ((state
& STS_HALT
) == 0) {
171 xhci_warn(xhci
, "Host controller not halted, aborting reset.\n");
175 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "// Reset the HC");
176 command
= readl(&xhci
->op_regs
->command
);
177 command
|= CMD_RESET
;
178 writel(command
, &xhci
->op_regs
->command
);
180 /* Existing Intel xHCI controllers require a delay of 1 mS,
181 * after setting the CMD_RESET bit, and before accessing any
182 * HC registers. This allows the HC to complete the
183 * reset operation and be ready for HC register access.
184 * Without this delay, the subsequent HC register access,
185 * may result in a system hang very rarely.
187 if (xhci
->quirks
& XHCI_INTEL_HOST
)
190 ret
= xhci_handshake(&xhci
->op_regs
->command
,
191 CMD_RESET
, 0, 10 * 1000 * 1000);
195 if (xhci
->quirks
& XHCI_ASMEDIA_MODIFY_FLOWCONTROL
)
196 usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
));
198 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
199 "Wait for controller to be ready for doorbell rings");
201 * xHCI cannot write to any doorbells or operational registers other
202 * than status until the "Controller Not Ready" flag is cleared.
204 ret
= xhci_handshake(&xhci
->op_regs
->status
,
205 STS_CNR
, 0, 10 * 1000 * 1000);
207 for (i
= 0; i
< 2; ++i
) {
208 xhci
->bus_state
[i
].port_c_suspend
= 0;
209 xhci
->bus_state
[i
].suspended_ports
= 0;
210 xhci
->bus_state
[i
].resuming_ports
= 0;
217 static int xhci_free_msi(struct xhci_hcd
*xhci
)
221 if (!xhci
->msix_entries
)
224 for (i
= 0; i
< xhci
->msix_count
; i
++)
225 if (xhci
->msix_entries
[i
].vector
)
226 free_irq(xhci
->msix_entries
[i
].vector
,
234 static int xhci_setup_msi(struct xhci_hcd
*xhci
)
237 struct pci_dev
*pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
239 ret
= pci_enable_msi(pdev
);
241 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
242 "failed to allocate MSI entry");
246 ret
= request_irq(pdev
->irq
, xhci_msi_irq
,
247 0, "xhci_hcd", xhci_to_hcd(xhci
));
249 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
250 "disable MSI interrupt");
251 pci_disable_msi(pdev
);
259 * free all IRQs request
261 static void xhci_free_irq(struct xhci_hcd
*xhci
)
263 struct pci_dev
*pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
266 /* return if using legacy interrupt */
267 if (xhci_to_hcd(xhci
)->irq
> 0)
270 ret
= xhci_free_msi(xhci
);
274 free_irq(pdev
->irq
, xhci_to_hcd(xhci
));
282 static int xhci_setup_msix(struct xhci_hcd
*xhci
)
285 struct usb_hcd
*hcd
= xhci_to_hcd(xhci
);
286 struct pci_dev
*pdev
= to_pci_dev(hcd
->self
.controller
);
289 * calculate number of msi-x vectors supported.
290 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
291 * with max number of interrupters based on the xhci HCSPARAMS1.
292 * - num_online_cpus: maximum msi-x vectors per CPUs core.
293 * Add additional 1 vector to ensure always available interrupt.
295 xhci
->msix_count
= min(num_online_cpus() + 1,
296 HCS_MAX_INTRS(xhci
->hcs_params1
));
299 kmalloc((sizeof(struct msix_entry
))*xhci
->msix_count
,
301 if (!xhci
->msix_entries
)
304 for (i
= 0; i
< xhci
->msix_count
; i
++) {
305 xhci
->msix_entries
[i
].entry
= i
;
306 xhci
->msix_entries
[i
].vector
= 0;
309 ret
= pci_enable_msix_exact(pdev
, xhci
->msix_entries
, xhci
->msix_count
);
311 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
312 "Failed to enable MSI-X");
316 for (i
= 0; i
< xhci
->msix_count
; i
++) {
317 ret
= request_irq(xhci
->msix_entries
[i
].vector
,
319 0, "xhci_hcd", xhci_to_hcd(xhci
));
324 hcd
->msix_enabled
= 1;
328 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "disable MSI-X interrupt");
330 pci_disable_msix(pdev
);
332 kfree(xhci
->msix_entries
);
333 xhci
->msix_entries
= NULL
;
337 /* Free any IRQs and disable MSI-X */
338 static void xhci_cleanup_msix(struct xhci_hcd
*xhci
)
340 struct usb_hcd
*hcd
= xhci_to_hcd(xhci
);
341 struct pci_dev
*pdev
= to_pci_dev(hcd
->self
.controller
);
343 if (xhci
->quirks
& XHCI_PLAT
)
348 if (xhci
->msix_entries
) {
349 pci_disable_msix(pdev
);
350 kfree(xhci
->msix_entries
);
351 xhci
->msix_entries
= NULL
;
353 pci_disable_msi(pdev
);
356 hcd
->msix_enabled
= 0;
360 static void __maybe_unused
xhci_msix_sync_irqs(struct xhci_hcd
*xhci
)
364 if (xhci
->msix_entries
) {
365 for (i
= 0; i
< xhci
->msix_count
; i
++)
366 synchronize_irq(xhci
->msix_entries
[i
].vector
);
370 static int xhci_try_enable_msi(struct usb_hcd
*hcd
)
372 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
373 struct pci_dev
*pdev
;
376 /* The xhci platform device has set up IRQs through usb_add_hcd. */
377 if (xhci
->quirks
& XHCI_PLAT
)
380 pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
382 * Some Fresco Logic host controllers advertise MSI, but fail to
383 * generate interrupts. Don't even try to enable MSI.
385 if (xhci
->quirks
& XHCI_BROKEN_MSI
)
388 /* unregister the legacy interrupt */
390 free_irq(hcd
->irq
, hcd
);
393 ret
= xhci_setup_msix(xhci
);
395 /* fall back to msi*/
396 ret
= xhci_setup_msi(xhci
);
399 /* hcd->irq is 0, we have MSI */
403 xhci_err(xhci
, "No msi-x/msi found and no IRQ in BIOS\n");
408 if (!strlen(hcd
->irq_descr
))
409 snprintf(hcd
->irq_descr
, sizeof(hcd
->irq_descr
), "%s:usb%d",
410 hcd
->driver
->description
, hcd
->self
.busnum
);
412 /* fall back to legacy interrupt*/
413 ret
= request_irq(pdev
->irq
, &usb_hcd_irq
, IRQF_SHARED
,
414 hcd
->irq_descr
, hcd
);
416 xhci_err(xhci
, "request interrupt %d failed\n",
420 hcd
->irq
= pdev
->irq
;
426 static inline int xhci_try_enable_msi(struct usb_hcd
*hcd
)
431 static inline void xhci_cleanup_msix(struct xhci_hcd
*xhci
)
435 static inline void xhci_msix_sync_irqs(struct xhci_hcd
*xhci
)
441 static void compliance_mode_recovery(unsigned long arg
)
443 struct xhci_hcd
*xhci
;
448 xhci
= (struct xhci_hcd
*)arg
;
450 for (i
= 0; i
< xhci
->num_usb3_ports
; i
++) {
451 temp
= readl(xhci
->usb3_ports
[i
]);
452 if ((temp
& PORT_PLS_MASK
) == USB_SS_PORT_LS_COMP_MOD
) {
454 * Compliance Mode Detected. Letting USB Core
455 * handle the Warm Reset
457 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
458 "Compliance mode detected->port %d",
460 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
461 "Attempting compliance mode recovery");
462 hcd
= xhci
->shared_hcd
;
464 if (hcd
->state
== HC_STATE_SUSPENDED
)
465 usb_hcd_resume_root_hub(hcd
);
467 usb_hcd_poll_rh_status(hcd
);
471 if (xhci
->port_status_u0
!= ((1 << xhci
->num_usb3_ports
)-1))
472 mod_timer(&xhci
->comp_mode_recovery_timer
,
473 jiffies
+ msecs_to_jiffies(COMP_MODE_RCVRY_MSECS
));
477 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
478 * that causes ports behind that hardware to enter compliance mode sometimes.
479 * The quirk creates a timer that polls every 2 seconds the link state of
480 * each host controller's port and recovers it by issuing a Warm reset
481 * if Compliance mode is detected, otherwise the port will become "dead" (no
482 * device connections or disconnections will be detected anymore). Becasue no
483 * status event is generated when entering compliance mode (per xhci spec),
484 * this quirk is needed on systems that have the failing hardware installed.
486 static void compliance_mode_recovery_timer_init(struct xhci_hcd
*xhci
)
488 xhci
->port_status_u0
= 0;
489 setup_timer(&xhci
->comp_mode_recovery_timer
,
490 compliance_mode_recovery
, (unsigned long)xhci
);
491 xhci
->comp_mode_recovery_timer
.expires
= jiffies
+
492 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS
);
494 add_timer(&xhci
->comp_mode_recovery_timer
);
495 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
496 "Compliance mode recovery timer initialized");
500 * This function identifies the systems that have installed the SN65LVPE502CP
501 * USB3.0 re-driver and that need the Compliance Mode Quirk.
503 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
505 static bool xhci_compliance_mode_recovery_timer_quirk_check(void)
507 const char *dmi_product_name
, *dmi_sys_vendor
;
509 dmi_product_name
= dmi_get_system_info(DMI_PRODUCT_NAME
);
510 dmi_sys_vendor
= dmi_get_system_info(DMI_SYS_VENDOR
);
511 if (!dmi_product_name
|| !dmi_sys_vendor
)
514 if (!(strstr(dmi_sys_vendor
, "Hewlett-Packard")))
517 if (strstr(dmi_product_name
, "Z420") ||
518 strstr(dmi_product_name
, "Z620") ||
519 strstr(dmi_product_name
, "Z820") ||
520 strstr(dmi_product_name
, "Z1 Workstation"))
526 static int xhci_all_ports_seen_u0(struct xhci_hcd
*xhci
)
528 return (xhci
->port_status_u0
== ((1 << xhci
->num_usb3_ports
)-1));
533 * Initialize memory for HCD and xHC (one-time init).
535 * Program the PAGESIZE register, initialize the device context array, create
536 * device contexts (?), set up a command ring segment (or two?), create event
537 * ring (one for now).
539 int xhci_init(struct usb_hcd
*hcd
)
541 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
544 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "xhci_init");
545 spin_lock_init(&xhci
->lock
);
546 if (xhci
->hci_version
== 0x95 && link_quirk
) {
547 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
548 "QUIRK: Not clearing Link TRB chain bits.");
549 xhci
->quirks
|= XHCI_LINK_TRB_QUIRK
;
551 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
552 "xHCI doesn't need link TRB QUIRK");
554 retval
= xhci_mem_init(xhci
, GFP_KERNEL
);
555 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "Finished xhci_init");
557 /* Initializing Compliance Mode Recovery Data If Needed */
558 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
559 xhci
->quirks
|= XHCI_COMP_MODE_QUIRK
;
560 compliance_mode_recovery_timer_init(xhci
);
566 /*-------------------------------------------------------------------------*/
569 static int xhci_run_finished(struct xhci_hcd
*xhci
)
571 if (xhci_start(xhci
)) {
575 xhci
->shared_hcd
->state
= HC_STATE_RUNNING
;
576 xhci
->cmd_ring_state
= CMD_RING_STATE_RUNNING
;
578 if (xhci
->quirks
& XHCI_NEC_HOST
)
579 xhci_ring_cmd_db(xhci
);
581 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
582 "Finished xhci_run for USB3 roothub");
587 * Start the HC after it was halted.
589 * This function is called by the USB core when the HC driver is added.
590 * Its opposite is xhci_stop().
592 * xhci_init() must be called once before this function can be called.
593 * Reset the HC, enable device slot contexts, program DCBAAP, and
594 * set command ring pointer and event ring pointer.
596 * Setup MSI-X vectors and enable interrupts.
598 int xhci_run(struct usb_hcd
*hcd
)
603 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
605 /* Start the xHCI host controller running only after the USB 2.0 roothub
609 hcd
->uses_new_polling
= 1;
610 if (!usb_hcd_is_primary_hcd(hcd
))
611 return xhci_run_finished(xhci
);
613 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "xhci_run");
615 ret
= xhci_try_enable_msi(hcd
);
619 xhci_dbg(xhci
, "Command ring memory map follows:\n");
620 xhci_debug_ring(xhci
, xhci
->cmd_ring
);
621 xhci_dbg_ring_ptrs(xhci
, xhci
->cmd_ring
);
622 xhci_dbg_cmd_ptrs(xhci
);
624 xhci_dbg(xhci
, "ERST memory map follows:\n");
625 xhci_dbg_erst(xhci
, &xhci
->erst
);
626 xhci_dbg(xhci
, "Event ring:\n");
627 xhci_debug_ring(xhci
, xhci
->event_ring
);
628 xhci_dbg_ring_ptrs(xhci
, xhci
->event_ring
);
629 temp_64
= xhci_read_64(xhci
, &xhci
->ir_set
->erst_dequeue
);
630 temp_64
&= ~ERST_PTR_MASK
;
631 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
632 "ERST deq = 64'h%0lx", (long unsigned int) temp_64
);
634 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
635 "// Set the interrupt modulation register");
636 temp
= readl(&xhci
->ir_set
->irq_control
);
637 temp
&= ~ER_IRQ_INTERVAL_MASK
;
639 * the increment interval is 8 times as much as that defined
640 * in xHCI spec on MTK's controller
642 temp
|= (u32
) ((xhci
->quirks
& XHCI_MTK_HOST
) ? 20 : 160);
643 writel(temp
, &xhci
->ir_set
->irq_control
);
645 /* Set the HCD state before we enable the irqs */
646 temp
= readl(&xhci
->op_regs
->command
);
648 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
649 "// Enable interrupts, cmd = 0x%x.", temp
);
650 writel(temp
, &xhci
->op_regs
->command
);
652 temp
= readl(&xhci
->ir_set
->irq_pending
);
653 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
654 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
655 xhci
->ir_set
, (unsigned int) ER_IRQ_ENABLE(temp
));
656 writel(ER_IRQ_ENABLE(temp
), &xhci
->ir_set
->irq_pending
);
657 xhci_print_ir_set(xhci
, 0);
659 if (xhci
->quirks
& XHCI_NEC_HOST
) {
660 struct xhci_command
*command
;
661 command
= xhci_alloc_command(xhci
, false, false, GFP_KERNEL
);
664 xhci_queue_vendor_command(xhci
, command
, 0, 0, 0,
665 TRB_TYPE(TRB_NEC_GET_FW
));
667 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
668 "Finished xhci_run for USB2 roothub");
671 EXPORT_SYMBOL_GPL(xhci_run
);
676 * This function is called by the USB core when the HC driver is removed.
677 * Its opposite is xhci_run().
679 * Disable device contexts, disable IRQs, and quiesce the HC.
680 * Reset the HC, finish any completed transactions, and cleanup memory.
682 void xhci_stop(struct usb_hcd
*hcd
)
685 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
687 mutex_lock(&xhci
->mutex
);
689 if (!(xhci
->xhc_state
& XHCI_STATE_HALTED
)) {
690 spin_lock_irq(&xhci
->lock
);
692 xhci
->xhc_state
|= XHCI_STATE_HALTED
;
693 xhci
->cmd_ring_state
= CMD_RING_STATE_STOPPED
;
697 spin_unlock_irq(&xhci
->lock
);
700 if (!usb_hcd_is_primary_hcd(hcd
)) {
701 mutex_unlock(&xhci
->mutex
);
705 xhci_cleanup_msix(xhci
);
707 /* Deleting Compliance Mode Recovery Timer */
708 if ((xhci
->quirks
& XHCI_COMP_MODE_QUIRK
) &&
709 (!(xhci_all_ports_seen_u0(xhci
)))) {
710 del_timer_sync(&xhci
->comp_mode_recovery_timer
);
711 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
712 "%s: compliance mode recovery timer deleted",
716 if (xhci
->quirks
& XHCI_AMD_PLL_FIX
)
719 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
720 "// Disabling event ring interrupts");
721 temp
= readl(&xhci
->op_regs
->status
);
722 writel(temp
& ~STS_EINT
, &xhci
->op_regs
->status
);
723 temp
= readl(&xhci
->ir_set
->irq_pending
);
724 writel(ER_IRQ_DISABLE(temp
), &xhci
->ir_set
->irq_pending
);
725 xhci_print_ir_set(xhci
, 0);
727 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
, "cleaning up memory");
728 xhci_mem_cleanup(xhci
);
729 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
730 "xhci_stop completed - status = %x",
731 readl(&xhci
->op_regs
->status
));
732 mutex_unlock(&xhci
->mutex
);
736 * Shutdown HC (not bus-specific)
738 * This is called when the machine is rebooting or halting. We assume that the
739 * machine will be powered off, and the HC's internal state will be reset.
740 * Don't bother to free memory.
742 * This will only ever be called with the main usb_hcd (the USB3 roothub).
744 void xhci_shutdown(struct usb_hcd
*hcd
)
746 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
748 if (xhci
->quirks
& XHCI_SPURIOUS_REBOOT
)
749 usb_disable_xhci_ports(to_pci_dev(hcd
->self
.controller
));
751 spin_lock_irq(&xhci
->lock
);
753 /* Workaround for spurious wakeups at shutdown with HSW */
754 if (xhci
->quirks
& XHCI_SPURIOUS_WAKEUP
)
756 spin_unlock_irq(&xhci
->lock
);
758 xhci_cleanup_msix(xhci
);
760 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
761 "xhci_shutdown completed - status = %x",
762 readl(&xhci
->op_regs
->status
));
764 /* Yet another workaround for spurious wakeups at shutdown with HSW */
765 if (xhci
->quirks
& XHCI_SPURIOUS_WAKEUP
)
766 pci_set_power_state(to_pci_dev(hcd
->self
.controller
), PCI_D3hot
);
770 static void xhci_save_registers(struct xhci_hcd
*xhci
)
772 xhci
->s3
.command
= readl(&xhci
->op_regs
->command
);
773 xhci
->s3
.dev_nt
= readl(&xhci
->op_regs
->dev_notification
);
774 xhci
->s3
.dcbaa_ptr
= xhci_read_64(xhci
, &xhci
->op_regs
->dcbaa_ptr
);
775 xhci
->s3
.config_reg
= readl(&xhci
->op_regs
->config_reg
);
776 xhci
->s3
.erst_size
= readl(&xhci
->ir_set
->erst_size
);
777 xhci
->s3
.erst_base
= xhci_read_64(xhci
, &xhci
->ir_set
->erst_base
);
778 xhci
->s3
.erst_dequeue
= xhci_read_64(xhci
, &xhci
->ir_set
->erst_dequeue
);
779 xhci
->s3
.irq_pending
= readl(&xhci
->ir_set
->irq_pending
);
780 xhci
->s3
.irq_control
= readl(&xhci
->ir_set
->irq_control
);
783 static void xhci_restore_registers(struct xhci_hcd
*xhci
)
785 writel(xhci
->s3
.command
, &xhci
->op_regs
->command
);
786 writel(xhci
->s3
.dev_nt
, &xhci
->op_regs
->dev_notification
);
787 xhci_write_64(xhci
, xhci
->s3
.dcbaa_ptr
, &xhci
->op_regs
->dcbaa_ptr
);
788 writel(xhci
->s3
.config_reg
, &xhci
->op_regs
->config_reg
);
789 writel(xhci
->s3
.erst_size
, &xhci
->ir_set
->erst_size
);
790 xhci_write_64(xhci
, xhci
->s3
.erst_base
, &xhci
->ir_set
->erst_base
);
791 xhci_write_64(xhci
, xhci
->s3
.erst_dequeue
, &xhci
->ir_set
->erst_dequeue
);
792 writel(xhci
->s3
.irq_pending
, &xhci
->ir_set
->irq_pending
);
793 writel(xhci
->s3
.irq_control
, &xhci
->ir_set
->irq_control
);
796 static void xhci_set_cmd_ring_deq(struct xhci_hcd
*xhci
)
800 /* step 2: initialize command ring buffer */
801 val_64
= xhci_read_64(xhci
, &xhci
->op_regs
->cmd_ring
);
802 val_64
= (val_64
& (u64
) CMD_RING_RSVD_BITS
) |
803 (xhci_trb_virt_to_dma(xhci
->cmd_ring
->deq_seg
,
804 xhci
->cmd_ring
->dequeue
) &
805 (u64
) ~CMD_RING_RSVD_BITS
) |
806 xhci
->cmd_ring
->cycle_state
;
807 xhci_dbg_trace(xhci
, trace_xhci_dbg_init
,
808 "// Setting command ring address to 0x%llx",
809 (long unsigned long) val_64
);
810 xhci_write_64(xhci
, val_64
, &xhci
->op_regs
->cmd_ring
);
814 * The whole command ring must be cleared to zero when we suspend the host.
816 * The host doesn't save the command ring pointer in the suspend well, so we
817 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
818 * aligned, because of the reserved bits in the command ring dequeue pointer
819 * register. Therefore, we can't just set the dequeue pointer back in the
820 * middle of the ring (TRBs are 16-byte aligned).
822 static void xhci_clear_command_ring(struct xhci_hcd
*xhci
)
824 struct xhci_ring
*ring
;
825 struct xhci_segment
*seg
;
827 ring
= xhci
->cmd_ring
;
831 sizeof(union xhci_trb
) * (TRBS_PER_SEGMENT
- 1));
832 seg
->trbs
[TRBS_PER_SEGMENT
- 1].link
.control
&=
833 cpu_to_le32(~TRB_CYCLE
);
835 } while (seg
!= ring
->deq_seg
);
837 /* Reset the software enqueue and dequeue pointers */
838 ring
->deq_seg
= ring
->first_seg
;
839 ring
->dequeue
= ring
->first_seg
->trbs
;
840 ring
->enq_seg
= ring
->deq_seg
;
841 ring
->enqueue
= ring
->dequeue
;
843 ring
->num_trbs_free
= ring
->num_segs
* (TRBS_PER_SEGMENT
- 1) - 1;
845 * Ring is now zeroed, so the HW should look for change of ownership
846 * when the cycle bit is set to 1.
848 ring
->cycle_state
= 1;
851 * Reset the hardware dequeue pointer.
852 * Yes, this will need to be re-written after resume, but we're paranoid
853 * and want to make sure the hardware doesn't access bogus memory
854 * because, say, the BIOS or an SMI started the host without changing
855 * the command ring pointers.
857 xhci_set_cmd_ring_deq(xhci
);
860 static void xhci_disable_port_wake_on_bits(struct xhci_hcd
*xhci
)
863 __le32 __iomem
**port_array
;
867 spin_lock_irqsave(&xhci
->lock
, flags
);
869 /* disble usb3 ports Wake bits*/
870 port_index
= xhci
->num_usb3_ports
;
871 port_array
= xhci
->usb3_ports
;
872 while (port_index
--) {
873 t1
= readl(port_array
[port_index
]);
874 t1
= xhci_port_state_to_neutral(t1
);
875 t2
= t1
& ~PORT_WAKE_BITS
;
877 writel(t2
, port_array
[port_index
]);
880 /* disble usb2 ports Wake bits*/
881 port_index
= xhci
->num_usb2_ports
;
882 port_array
= xhci
->usb2_ports
;
883 while (port_index
--) {
884 t1
= readl(port_array
[port_index
]);
885 t1
= xhci_port_state_to_neutral(t1
);
886 t2
= t1
& ~PORT_WAKE_BITS
;
888 writel(t2
, port_array
[port_index
]);
891 spin_unlock_irqrestore(&xhci
->lock
, flags
);
894 static bool xhci_pending_portevent(struct xhci_hcd
*xhci
)
896 __le32 __iomem
**port_array
;
901 status
= readl(&xhci
->op_regs
->status
);
902 if (status
& STS_EINT
)
905 * Checking STS_EINT is not enough as there is a lag between a change
906 * bit being set and the Port Status Change Event that it generated
907 * being written to the Event Ring. See note in xhci 1.1 section 4.19.2.
910 port_index
= xhci
->num_usb2_ports
;
911 port_array
= xhci
->usb2_ports
;
912 while (port_index
--) {
913 portsc
= readl(port_array
[port_index
]);
914 if (portsc
& PORT_CHANGE_MASK
||
915 (portsc
& PORT_PLS_MASK
) == XDEV_RESUME
)
918 port_index
= xhci
->num_usb3_ports
;
919 port_array
= xhci
->usb3_ports
;
920 while (port_index
--) {
921 portsc
= readl(port_array
[port_index
]);
922 if (portsc
& PORT_CHANGE_MASK
||
923 (portsc
& PORT_PLS_MASK
) == XDEV_RESUME
)
930 * Stop HC (not bus-specific)
932 * This is called when the machine transition into S3/S4 mode.
935 int xhci_suspend(struct xhci_hcd
*xhci
, bool do_wakeup
)
938 unsigned int delay
= XHCI_MAX_HALT_USEC
;
939 struct usb_hcd
*hcd
= xhci_to_hcd(xhci
);
945 if (hcd
->state
!= HC_STATE_SUSPENDED
||
946 xhci
->shared_hcd
->state
!= HC_STATE_SUSPENDED
)
949 /* Clear root port wake on bits if wakeup not allowed. */
951 xhci_disable_port_wake_on_bits(xhci
);
953 /* Don't poll the roothubs on bus suspend. */
954 xhci_dbg(xhci
, "%s: stopping port polling.\n", __func__
);
955 clear_bit(HCD_FLAG_POLL_RH
, &hcd
->flags
);
956 del_timer_sync(&hcd
->rh_timer
);
957 clear_bit(HCD_FLAG_POLL_RH
, &xhci
->shared_hcd
->flags
);
958 del_timer_sync(&xhci
->shared_hcd
->rh_timer
);
960 spin_lock_irq(&xhci
->lock
);
961 clear_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
962 clear_bit(HCD_FLAG_HW_ACCESSIBLE
, &xhci
->shared_hcd
->flags
);
963 /* step 1: stop endpoint */
964 /* skipped assuming that port suspend has done */
966 /* step 2: clear Run/Stop bit */
967 command
= readl(&xhci
->op_regs
->command
);
969 writel(command
, &xhci
->op_regs
->command
);
971 /* Some chips from Fresco Logic need an extraordinary delay */
972 delay
*= (xhci
->quirks
& XHCI_SLOW_SUSPEND
) ? 10 : 1;
974 if (xhci_handshake(&xhci
->op_regs
->status
,
975 STS_HALT
, STS_HALT
, delay
)) {
976 xhci_warn(xhci
, "WARN: xHC CMD_RUN timeout\n");
977 spin_unlock_irq(&xhci
->lock
);
980 xhci_clear_command_ring(xhci
);
982 /* step 3: save registers */
983 xhci_save_registers(xhci
);
985 /* step 4: set CSS flag */
986 command
= readl(&xhci
->op_regs
->command
);
988 writel(command
, &xhci
->op_regs
->command
);
989 if (xhci_handshake(&xhci
->op_regs
->status
,
990 STS_SAVE
, 0, 10 * 1000)) {
991 xhci_warn(xhci
, "WARN: xHC save state timeout\n");
992 spin_unlock_irq(&xhci
->lock
);
995 spin_unlock_irq(&xhci
->lock
);
998 * Deleting Compliance Mode Recovery Timer because the xHCI Host
999 * is about to be suspended.
1001 if ((xhci
->quirks
& XHCI_COMP_MODE_QUIRK
) &&
1002 (!(xhci_all_ports_seen_u0(xhci
)))) {
1003 del_timer_sync(&xhci
->comp_mode_recovery_timer
);
1004 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
1005 "%s: compliance mode recovery timer deleted",
1009 /* step 5: remove core well power */
1010 /* synchronize irq when using MSI-X */
1011 xhci_msix_sync_irqs(xhci
);
1015 EXPORT_SYMBOL_GPL(xhci_suspend
);
1018 * start xHC (not bus-specific)
1020 * This is called when the machine transition from S3/S4 mode.
1023 int xhci_resume(struct xhci_hcd
*xhci
, bool hibernated
)
1025 u32 command
, temp
= 0;
1026 struct usb_hcd
*hcd
= xhci_to_hcd(xhci
);
1027 struct usb_hcd
*secondary_hcd
;
1029 bool comp_timer_running
= false;
1034 /* Wait a bit if either of the roothubs need to settle from the
1035 * transition into bus suspend.
1037 if (time_before(jiffies
, xhci
->bus_state
[0].next_statechange
) ||
1038 time_before(jiffies
,
1039 xhci
->bus_state
[1].next_statechange
))
1042 set_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
1043 set_bit(HCD_FLAG_HW_ACCESSIBLE
, &xhci
->shared_hcd
->flags
);
1045 spin_lock_irq(&xhci
->lock
);
1046 if (xhci
->quirks
& XHCI_RESET_ON_RESUME
)
1050 /* step 1: restore register */
1051 xhci_restore_registers(xhci
);
1052 /* step 2: initialize command ring buffer */
1053 xhci_set_cmd_ring_deq(xhci
);
1054 /* step 3: restore state and start state*/
1055 /* step 3: set CRS flag */
1056 command
= readl(&xhci
->op_regs
->command
);
1058 writel(command
, &xhci
->op_regs
->command
);
1059 if (xhci_handshake(&xhci
->op_regs
->status
,
1060 STS_RESTORE
, 0, 10 * 1000)) {
1061 xhci_warn(xhci
, "WARN: xHC restore state timeout\n");
1062 spin_unlock_irq(&xhci
->lock
);
1065 temp
= readl(&xhci
->op_regs
->status
);
1068 /* If restore operation fails, re-initialize the HC during resume */
1069 if ((temp
& STS_SRE
) || hibernated
) {
1071 if ((xhci
->quirks
& XHCI_COMP_MODE_QUIRK
) &&
1072 !(xhci_all_ports_seen_u0(xhci
))) {
1073 del_timer_sync(&xhci
->comp_mode_recovery_timer
);
1074 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
1075 "Compliance Mode Recovery Timer deleted!");
1078 /* Let the USB core know _both_ roothubs lost power. */
1079 usb_root_hub_lost_power(xhci
->main_hcd
->self
.root_hub
);
1080 usb_root_hub_lost_power(xhci
->shared_hcd
->self
.root_hub
);
1082 xhci_dbg(xhci
, "Stop HCD\n");
1085 spin_unlock_irq(&xhci
->lock
);
1086 xhci_cleanup_msix(xhci
);
1088 xhci_dbg(xhci
, "// Disabling event ring interrupts\n");
1089 temp
= readl(&xhci
->op_regs
->status
);
1090 writel(temp
& ~STS_EINT
, &xhci
->op_regs
->status
);
1091 temp
= readl(&xhci
->ir_set
->irq_pending
);
1092 writel(ER_IRQ_DISABLE(temp
), &xhci
->ir_set
->irq_pending
);
1093 xhci_print_ir_set(xhci
, 0);
1095 xhci_dbg(xhci
, "cleaning up memory\n");
1096 xhci_mem_cleanup(xhci
);
1097 xhci_dbg(xhci
, "xhci_stop completed - status = %x\n",
1098 readl(&xhci
->op_regs
->status
));
1100 /* USB core calls the PCI reinit and start functions twice:
1101 * first with the primary HCD, and then with the secondary HCD.
1102 * If we don't do the same, the host will never be started.
1104 if (!usb_hcd_is_primary_hcd(hcd
))
1105 secondary_hcd
= hcd
;
1107 secondary_hcd
= xhci
->shared_hcd
;
1109 xhci_dbg(xhci
, "Initialize the xhci_hcd\n");
1110 retval
= xhci_init(hcd
->primary_hcd
);
1113 comp_timer_running
= true;
1115 xhci_dbg(xhci
, "Start the primary HCD\n");
1116 retval
= xhci_run(hcd
->primary_hcd
);
1118 xhci_dbg(xhci
, "Start the secondary HCD\n");
1119 retval
= xhci_run(secondary_hcd
);
1121 hcd
->state
= HC_STATE_SUSPENDED
;
1122 xhci
->shared_hcd
->state
= HC_STATE_SUSPENDED
;
1126 /* step 4: set Run/Stop bit */
1127 command
= readl(&xhci
->op_regs
->command
);
1129 writel(command
, &xhci
->op_regs
->command
);
1130 xhci_handshake(&xhci
->op_regs
->status
, STS_HALT
,
1133 /* step 5: walk topology and initialize portsc,
1134 * portpmsc and portli
1136 /* this is done in bus_resume */
1138 /* step 6: restart each of the previously
1139 * Running endpoints by ringing their doorbells
1142 spin_unlock_irq(&xhci
->lock
);
1146 /* Resume root hubs only when have pending events. */
1147 if (xhci_pending_portevent(xhci
)) {
1148 usb_hcd_resume_root_hub(xhci
->shared_hcd
);
1149 usb_hcd_resume_root_hub(hcd
);
1154 * If system is subject to the Quirk, Compliance Mode Timer needs to
1155 * be re-initialized Always after a system resume. Ports are subject
1156 * to suffer the Compliance Mode issue again. It doesn't matter if
1157 * ports have entered previously to U0 before system's suspension.
1159 if ((xhci
->quirks
& XHCI_COMP_MODE_QUIRK
) && !comp_timer_running
)
1160 compliance_mode_recovery_timer_init(xhci
);
1162 if (xhci
->quirks
& XHCI_ASMEDIA_MODIFY_FLOWCONTROL
)
1163 usb_asmedia_modifyflowcontrol(to_pci_dev(hcd
->self
.controller
));
1165 /* Re-enable port polling. */
1166 xhci_dbg(xhci
, "%s: starting port polling.\n", __func__
);
1167 set_bit(HCD_FLAG_POLL_RH
, &xhci
->shared_hcd
->flags
);
1168 usb_hcd_poll_rh_status(xhci
->shared_hcd
);
1169 set_bit(HCD_FLAG_POLL_RH
, &hcd
->flags
);
1170 usb_hcd_poll_rh_status(hcd
);
1174 EXPORT_SYMBOL_GPL(xhci_resume
);
1175 #endif /* CONFIG_PM */
1177 /*-------------------------------------------------------------------------*/
1180 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1181 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1182 * value to right shift 1 for the bitmask.
1184 * Index = (epnum * 2) + direction - 1,
1185 * where direction = 0 for OUT, 1 for IN.
1186 * For control endpoints, the IN index is used (OUT index is unused), so
1187 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1189 unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor
*desc
)
1192 if (usb_endpoint_xfer_control(desc
))
1193 index
= (unsigned int) (usb_endpoint_num(desc
)*2);
1195 index
= (unsigned int) (usb_endpoint_num(desc
)*2) +
1196 (usb_endpoint_dir_in(desc
) ? 1 : 0) - 1;
1200 /* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1201 * address from the XHCI endpoint index.
1203 unsigned int xhci_get_endpoint_address(unsigned int ep_index
)
1205 unsigned int number
= DIV_ROUND_UP(ep_index
, 2);
1206 unsigned int direction
= ep_index
% 2 ? USB_DIR_OUT
: USB_DIR_IN
;
1207 return direction
| number
;
1210 /* Find the flag for this endpoint (for use in the control context). Use the
1211 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1214 unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor
*desc
)
1216 return 1 << (xhci_get_endpoint_index(desc
) + 1);
1219 /* Find the flag for this endpoint (for use in the control context). Use the
1220 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1223 unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index
)
1225 return 1 << (ep_index
+ 1);
1228 /* Compute the last valid endpoint context index. Basically, this is the
1229 * endpoint index plus one. For slot contexts with more than valid endpoint,
1230 * we find the most significant bit set in the added contexts flags.
1231 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1232 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1234 unsigned int xhci_last_valid_endpoint(u32 added_ctxs
)
1236 return fls(added_ctxs
) - 1;
1239 /* Returns 1 if the arguments are OK;
1240 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1242 static int xhci_check_args(struct usb_hcd
*hcd
, struct usb_device
*udev
,
1243 struct usb_host_endpoint
*ep
, int check_ep
, bool check_virt_dev
,
1245 struct xhci_hcd
*xhci
;
1246 struct xhci_virt_device
*virt_dev
;
1248 if (!hcd
|| (check_ep
&& !ep
) || !udev
) {
1249 pr_debug("xHCI %s called with invalid args\n", func
);
1252 if (!udev
->parent
) {
1253 pr_debug("xHCI %s called for root hub\n", func
);
1257 xhci
= hcd_to_xhci(hcd
);
1258 if (check_virt_dev
) {
1259 if (!udev
->slot_id
|| !xhci
->devs
[udev
->slot_id
]) {
1260 xhci_dbg(xhci
, "xHCI %s called with unaddressed device\n",
1265 virt_dev
= xhci
->devs
[udev
->slot_id
];
1266 if (virt_dev
->udev
!= udev
) {
1267 xhci_dbg(xhci
, "xHCI %s called with udev and "
1268 "virt_dev does not match\n", func
);
1273 if (xhci
->xhc_state
& XHCI_STATE_HALTED
)
1279 static int xhci_configure_endpoint(struct xhci_hcd
*xhci
,
1280 struct usb_device
*udev
, struct xhci_command
*command
,
1281 bool ctx_change
, bool must_succeed
);
1284 * Full speed devices may have a max packet size greater than 8 bytes, but the
1285 * USB core doesn't know that until it reads the first 8 bytes of the
1286 * descriptor. If the usb_device's max packet size changes after that point,
1287 * we need to issue an evaluate context command and wait on it.
1289 static int xhci_check_maxpacket(struct xhci_hcd
*xhci
, unsigned int slot_id
,
1290 unsigned int ep_index
, struct urb
*urb
)
1292 struct xhci_container_ctx
*out_ctx
;
1293 struct xhci_input_control_ctx
*ctrl_ctx
;
1294 struct xhci_ep_ctx
*ep_ctx
;
1295 struct xhci_command
*command
;
1296 int max_packet_size
;
1297 int hw_max_packet_size
;
1300 out_ctx
= xhci
->devs
[slot_id
]->out_ctx
;
1301 ep_ctx
= xhci_get_ep_ctx(xhci
, out_ctx
, ep_index
);
1302 hw_max_packet_size
= MAX_PACKET_DECODED(le32_to_cpu(ep_ctx
->ep_info2
));
1303 max_packet_size
= usb_endpoint_maxp(&urb
->dev
->ep0
.desc
);
1304 if (hw_max_packet_size
!= max_packet_size
) {
1305 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1306 "Max Packet Size for ep 0 changed.");
1307 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1308 "Max packet size in usb_device = %d",
1310 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1311 "Max packet size in xHCI HW = %d",
1312 hw_max_packet_size
);
1313 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1314 "Issuing evaluate context command.");
1316 /* Set up the input context flags for the command */
1317 /* FIXME: This won't work if a non-default control endpoint
1318 * changes max packet sizes.
1321 command
= xhci_alloc_command(xhci
, false, true, GFP_KERNEL
);
1325 command
->in_ctx
= xhci
->devs
[slot_id
]->in_ctx
;
1326 ctrl_ctx
= xhci_get_input_control_ctx(command
->in_ctx
);
1328 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
1331 goto command_cleanup
;
1333 /* Set up the modified control endpoint 0 */
1334 xhci_endpoint_copy(xhci
, xhci
->devs
[slot_id
]->in_ctx
,
1335 xhci
->devs
[slot_id
]->out_ctx
, ep_index
);
1337 ep_ctx
= xhci_get_ep_ctx(xhci
, command
->in_ctx
, ep_index
);
1338 ep_ctx
->ep_info2
&= cpu_to_le32(~MAX_PACKET_MASK
);
1339 ep_ctx
->ep_info2
|= cpu_to_le32(MAX_PACKET(max_packet_size
));
1341 ctrl_ctx
->add_flags
= cpu_to_le32(EP0_FLAG
);
1342 ctrl_ctx
->drop_flags
= 0;
1344 xhci_dbg(xhci
, "Slot %d input context\n", slot_id
);
1345 xhci_dbg_ctx(xhci
, command
->in_ctx
, ep_index
);
1346 xhci_dbg(xhci
, "Slot %d output context\n", slot_id
);
1347 xhci_dbg_ctx(xhci
, out_ctx
, ep_index
);
1349 ret
= xhci_configure_endpoint(xhci
, urb
->dev
, command
,
1352 /* Clean up the input context for later use by bandwidth
1355 ctrl_ctx
->add_flags
= cpu_to_le32(SLOT_FLAG
);
1357 kfree(command
->completion
);
1364 * non-error returns are a promise to giveback() the urb later
1365 * we drop ownership so next owner (or urb unlink) can get it
1367 int xhci_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
, gfp_t mem_flags
)
1369 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
1370 struct xhci_td
*buffer
;
1371 unsigned long flags
;
1373 unsigned int slot_id
, ep_index
;
1374 struct urb_priv
*urb_priv
;
1377 if (!urb
|| xhci_check_args(hcd
, urb
->dev
, urb
->ep
,
1378 true, true, __func__
) <= 0)
1381 slot_id
= urb
->dev
->slot_id
;
1382 ep_index
= xhci_get_endpoint_index(&urb
->ep
->desc
);
1384 if (!HCD_HW_ACCESSIBLE(hcd
)) {
1385 if (!in_interrupt())
1386 xhci_dbg(xhci
, "urb submitted during PCI suspend\n");
1391 if (usb_endpoint_xfer_isoc(&urb
->ep
->desc
))
1392 size
= urb
->number_of_packets
;
1393 else if (usb_endpoint_is_bulk_out(&urb
->ep
->desc
) &&
1394 urb
->transfer_buffer_length
> 0 &&
1395 urb
->transfer_flags
& URB_ZERO_PACKET
&&
1396 !(urb
->transfer_buffer_length
% usb_endpoint_maxp(&urb
->ep
->desc
)))
1401 urb_priv
= kzalloc(sizeof(struct urb_priv
) +
1402 size
* sizeof(struct xhci_td
*), mem_flags
);
1406 buffer
= kzalloc(size
* sizeof(struct xhci_td
), mem_flags
);
1412 for (i
= 0; i
< size
; i
++) {
1413 urb_priv
->td
[i
] = buffer
;
1417 urb_priv
->length
= size
;
1418 urb_priv
->td_cnt
= 0;
1419 urb
->hcpriv
= urb_priv
;
1421 if (usb_endpoint_xfer_control(&urb
->ep
->desc
)) {
1422 /* Check to see if the max packet size for the default control
1423 * endpoint changed during FS device enumeration
1425 if (urb
->dev
->speed
== USB_SPEED_FULL
) {
1426 ret
= xhci_check_maxpacket(xhci
, slot_id
,
1429 xhci_urb_free_priv(urb_priv
);
1435 /* We have a spinlock and interrupts disabled, so we must pass
1436 * atomic context to this function, which may allocate memory.
1438 spin_lock_irqsave(&xhci
->lock
, flags
);
1439 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1441 ret
= xhci_queue_ctrl_tx(xhci
, GFP_ATOMIC
, urb
,
1445 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1446 } else if (usb_endpoint_xfer_bulk(&urb
->ep
->desc
)) {
1447 spin_lock_irqsave(&xhci
->lock
, flags
);
1448 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1450 if (xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
&
1451 EP_GETTING_STREAMS
) {
1452 xhci_warn(xhci
, "WARN: Can't enqueue URB while bulk ep "
1453 "is transitioning to using streams.\n");
1455 } else if (xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
&
1456 EP_GETTING_NO_STREAMS
) {
1457 xhci_warn(xhci
, "WARN: Can't enqueue URB while bulk ep "
1458 "is transitioning to "
1459 "not having streams.\n");
1462 ret
= xhci_queue_bulk_tx(xhci
, GFP_ATOMIC
, urb
,
1467 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1468 } else if (usb_endpoint_xfer_int(&urb
->ep
->desc
)) {
1469 spin_lock_irqsave(&xhci
->lock
, flags
);
1470 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1472 ret
= xhci_queue_intr_tx(xhci
, GFP_ATOMIC
, urb
,
1476 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1478 spin_lock_irqsave(&xhci
->lock
, flags
);
1479 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1481 ret
= xhci_queue_isoc_tx_prepare(xhci
, GFP_ATOMIC
, urb
,
1485 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1490 xhci_dbg(xhci
, "Ep 0x%x: URB %p submitted for "
1491 "non-responsive xHCI host.\n",
1492 urb
->ep
->desc
.bEndpointAddress
, urb
);
1495 xhci_urb_free_priv(urb_priv
);
1497 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1502 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1503 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1504 * should pick up where it left off in the TD, unless a Set Transfer Ring
1505 * Dequeue Pointer is issued.
1507 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1508 * the ring. Since the ring is a contiguous structure, they can't be physically
1509 * removed. Instead, there are two options:
1511 * 1) If the HC is in the middle of processing the URB to be canceled, we
1512 * simply move the ring's dequeue pointer past those TRBs using the Set
1513 * Transfer Ring Dequeue Pointer command. This will be the common case,
1514 * when drivers timeout on the last submitted URB and attempt to cancel.
1516 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1517 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1518 * HC will need to invalidate the any TRBs it has cached after the stop
1519 * endpoint command, as noted in the xHCI 0.95 errata.
1521 * 3) The TD may have completed by the time the Stop Endpoint Command
1522 * completes, so software needs to handle that case too.
1524 * This function should protect against the TD enqueueing code ringing the
1525 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1526 * It also needs to account for multiple cancellations on happening at the same
1527 * time for the same endpoint.
1529 * Note that this function can be called in any context, or so says
1530 * usb_hcd_unlink_urb()
1532 int xhci_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
1534 unsigned long flags
;
1537 struct xhci_hcd
*xhci
;
1538 struct urb_priv
*urb_priv
;
1540 unsigned int ep_index
;
1541 struct xhci_ring
*ep_ring
;
1542 struct xhci_virt_ep
*ep
;
1543 struct xhci_command
*command
;
1545 xhci
= hcd_to_xhci(hcd
);
1546 spin_lock_irqsave(&xhci
->lock
, flags
);
1547 /* Make sure the URB hasn't completed or been unlinked already */
1548 ret
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
1549 if (ret
|| !urb
->hcpriv
)
1551 temp
= readl(&xhci
->op_regs
->status
);
1552 if (temp
== 0xffffffff || (xhci
->xhc_state
& XHCI_STATE_HALTED
)) {
1553 xhci_dbg_trace(xhci
, trace_xhci_dbg_cancel_urb
,
1554 "HW died, freeing TD.");
1555 urb_priv
= urb
->hcpriv
;
1556 for (i
= urb_priv
->td_cnt
;
1557 i
< urb_priv
->length
&& xhci
->devs
[urb
->dev
->slot_id
];
1559 td
= urb_priv
->td
[i
];
1560 if (!list_empty(&td
->td_list
))
1561 list_del_init(&td
->td_list
);
1562 if (!list_empty(&td
->cancelled_td_list
))
1563 list_del_init(&td
->cancelled_td_list
);
1566 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
1567 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1568 usb_hcd_giveback_urb(hcd
, urb
, -ESHUTDOWN
);
1569 xhci_urb_free_priv(urb_priv
);
1573 ep_index
= xhci_get_endpoint_index(&urb
->ep
->desc
);
1574 ep
= &xhci
->devs
[urb
->dev
->slot_id
]->eps
[ep_index
];
1575 ep_ring
= xhci_urb_to_transfer_ring(xhci
, urb
);
1581 urb_priv
= urb
->hcpriv
;
1582 i
= urb_priv
->td_cnt
;
1583 if (i
< urb_priv
->length
)
1584 xhci_dbg_trace(xhci
, trace_xhci_dbg_cancel_urb
,
1585 "Cancel URB %p, dev %s, ep 0x%x, "
1586 "starting at offset 0x%llx",
1587 urb
, urb
->dev
->devpath
,
1588 urb
->ep
->desc
.bEndpointAddress
,
1589 (unsigned long long) xhci_trb_virt_to_dma(
1590 urb_priv
->td
[i
]->start_seg
,
1591 urb_priv
->td
[i
]->first_trb
));
1593 for (; i
< urb_priv
->length
; i
++) {
1594 td
= urb_priv
->td
[i
];
1595 list_add_tail(&td
->cancelled_td_list
, &ep
->cancelled_td_list
);
1598 /* Queue a stop endpoint command, but only if this is
1599 * the first cancellation to be handled.
1601 if (!(ep
->ep_state
& EP_HALT_PENDING
)) {
1602 command
= xhci_alloc_command(xhci
, false, false, GFP_ATOMIC
);
1607 ep
->ep_state
|= EP_HALT_PENDING
;
1608 ep
->stop_cmds_pending
++;
1609 ep
->stop_cmd_timer
.expires
= jiffies
+
1610 XHCI_STOP_EP_CMD_TIMEOUT
* HZ
;
1611 add_timer(&ep
->stop_cmd_timer
);
1612 xhci_queue_stop_endpoint(xhci
, command
, urb
->dev
->slot_id
,
1614 xhci_ring_cmd_db(xhci
);
1617 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1621 /* Drop an endpoint from a new bandwidth configuration for this device.
1622 * Only one call to this function is allowed per endpoint before
1623 * check_bandwidth() or reset_bandwidth() must be called.
1624 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1625 * add the endpoint to the schedule with possibly new parameters denoted by a
1626 * different endpoint descriptor in usb_host_endpoint.
1627 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1630 * The USB core will not allow URBs to be queued to an endpoint that is being
1631 * disabled, so there's no need for mutual exclusion to protect
1632 * the xhci->devs[slot_id] structure.
1634 int xhci_drop_endpoint(struct usb_hcd
*hcd
, struct usb_device
*udev
,
1635 struct usb_host_endpoint
*ep
)
1637 struct xhci_hcd
*xhci
;
1638 struct xhci_container_ctx
*in_ctx
, *out_ctx
;
1639 struct xhci_input_control_ctx
*ctrl_ctx
;
1640 unsigned int ep_index
;
1641 struct xhci_ep_ctx
*ep_ctx
;
1643 u32 new_add_flags
, new_drop_flags
;
1646 ret
= xhci_check_args(hcd
, udev
, ep
, 1, true, __func__
);
1649 xhci
= hcd_to_xhci(hcd
);
1650 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1653 xhci_dbg(xhci
, "%s called for udev %p\n", __func__
, udev
);
1654 drop_flag
= xhci_get_endpoint_flag(&ep
->desc
);
1655 if (drop_flag
== SLOT_FLAG
|| drop_flag
== EP0_FLAG
) {
1656 xhci_dbg(xhci
, "xHCI %s - can't drop slot or ep 0 %#x\n",
1657 __func__
, drop_flag
);
1661 in_ctx
= xhci
->devs
[udev
->slot_id
]->in_ctx
;
1662 out_ctx
= xhci
->devs
[udev
->slot_id
]->out_ctx
;
1663 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
1665 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
1670 ep_index
= xhci_get_endpoint_index(&ep
->desc
);
1671 ep_ctx
= xhci_get_ep_ctx(xhci
, out_ctx
, ep_index
);
1672 /* If the HC already knows the endpoint is disabled,
1673 * or the HCD has noted it is disabled, ignore this request
1675 if (((ep_ctx
->ep_info
& cpu_to_le32(EP_STATE_MASK
)) ==
1676 cpu_to_le32(EP_STATE_DISABLED
)) ||
1677 le32_to_cpu(ctrl_ctx
->drop_flags
) &
1678 xhci_get_endpoint_flag(&ep
->desc
)) {
1679 /* Do not warn when called after a usb_device_reset */
1680 if (xhci
->devs
[udev
->slot_id
]->eps
[ep_index
].ring
!= NULL
)
1681 xhci_warn(xhci
, "xHCI %s called with disabled ep %p\n",
1686 ctrl_ctx
->drop_flags
|= cpu_to_le32(drop_flag
);
1687 new_drop_flags
= le32_to_cpu(ctrl_ctx
->drop_flags
);
1689 ctrl_ctx
->add_flags
&= cpu_to_le32(~drop_flag
);
1690 new_add_flags
= le32_to_cpu(ctrl_ctx
->add_flags
);
1692 xhci_endpoint_zero(xhci
, xhci
->devs
[udev
->slot_id
], ep
);
1694 if (xhci
->quirks
& XHCI_MTK_HOST
)
1695 xhci_mtk_drop_ep_quirk(hcd
, udev
, ep
);
1697 xhci_dbg(xhci
, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
1698 (unsigned int) ep
->desc
.bEndpointAddress
,
1700 (unsigned int) new_drop_flags
,
1701 (unsigned int) new_add_flags
);
1705 /* Add an endpoint to a new possible bandwidth configuration for this device.
1706 * Only one call to this function is allowed per endpoint before
1707 * check_bandwidth() or reset_bandwidth() must be called.
1708 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1709 * add the endpoint to the schedule with possibly new parameters denoted by a
1710 * different endpoint descriptor in usb_host_endpoint.
1711 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1714 * The USB core will not allow URBs to be queued to an endpoint until the
1715 * configuration or alt setting is installed in the device, so there's no need
1716 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1718 int xhci_add_endpoint(struct usb_hcd
*hcd
, struct usb_device
*udev
,
1719 struct usb_host_endpoint
*ep
)
1721 struct xhci_hcd
*xhci
;
1722 struct xhci_container_ctx
*in_ctx
;
1723 unsigned int ep_index
;
1724 struct xhci_input_control_ctx
*ctrl_ctx
;
1726 u32 new_add_flags
, new_drop_flags
;
1727 struct xhci_virt_device
*virt_dev
;
1730 ret
= xhci_check_args(hcd
, udev
, ep
, 1, true, __func__
);
1732 /* So we won't queue a reset ep command for a root hub */
1736 xhci
= hcd_to_xhci(hcd
);
1737 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
1740 added_ctxs
= xhci_get_endpoint_flag(&ep
->desc
);
1741 if (added_ctxs
== SLOT_FLAG
|| added_ctxs
== EP0_FLAG
) {
1742 /* FIXME when we have to issue an evaluate endpoint command to
1743 * deal with ep0 max packet size changing once we get the
1746 xhci_dbg(xhci
, "xHCI %s - can't add slot or ep 0 %#x\n",
1747 __func__
, added_ctxs
);
1751 virt_dev
= xhci
->devs
[udev
->slot_id
];
1752 in_ctx
= virt_dev
->in_ctx
;
1753 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
1755 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
1760 ep_index
= xhci_get_endpoint_index(&ep
->desc
);
1761 /* If this endpoint is already in use, and the upper layers are trying
1762 * to add it again without dropping it, reject the addition.
1764 if (virt_dev
->eps
[ep_index
].ring
&&
1765 !(le32_to_cpu(ctrl_ctx
->drop_flags
) & added_ctxs
)) {
1766 xhci_warn(xhci
, "Trying to add endpoint 0x%x "
1767 "without dropping it.\n",
1768 (unsigned int) ep
->desc
.bEndpointAddress
);
1772 /* If the HCD has already noted the endpoint is enabled,
1773 * ignore this request.
1775 if (le32_to_cpu(ctrl_ctx
->add_flags
) & added_ctxs
) {
1776 xhci_warn(xhci
, "xHCI %s called with enabled ep %p\n",
1782 * Configuration and alternate setting changes must be done in
1783 * process context, not interrupt context (or so documenation
1784 * for usb_set_interface() and usb_set_configuration() claim).
1786 if (xhci_endpoint_init(xhci
, virt_dev
, udev
, ep
, GFP_NOIO
) < 0) {
1787 dev_dbg(&udev
->dev
, "%s - could not initialize ep %#x\n",
1788 __func__
, ep
->desc
.bEndpointAddress
);
1792 if (xhci
->quirks
& XHCI_MTK_HOST
) {
1793 ret
= xhci_mtk_add_ep_quirk(hcd
, udev
, ep
);
1795 xhci_free_or_cache_endpoint_ring(xhci
,
1796 virt_dev
, ep_index
);
1801 ctrl_ctx
->add_flags
|= cpu_to_le32(added_ctxs
);
1802 new_add_flags
= le32_to_cpu(ctrl_ctx
->add_flags
);
1804 /* If xhci_endpoint_disable() was called for this endpoint, but the
1805 * xHC hasn't been notified yet through the check_bandwidth() call,
1806 * this re-adds a new state for the endpoint from the new endpoint
1807 * descriptors. We must drop and re-add this endpoint, so we leave the
1810 new_drop_flags
= le32_to_cpu(ctrl_ctx
->drop_flags
);
1812 /* Store the usb_device pointer for later use */
1815 xhci_dbg(xhci
, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
1816 (unsigned int) ep
->desc
.bEndpointAddress
,
1818 (unsigned int) new_drop_flags
,
1819 (unsigned int) new_add_flags
);
1823 static void xhci_zero_in_ctx(struct xhci_hcd
*xhci
, struct xhci_virt_device
*virt_dev
)
1825 struct xhci_input_control_ctx
*ctrl_ctx
;
1826 struct xhci_ep_ctx
*ep_ctx
;
1827 struct xhci_slot_ctx
*slot_ctx
;
1830 ctrl_ctx
= xhci_get_input_control_ctx(virt_dev
->in_ctx
);
1832 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
1837 /* When a device's add flag and drop flag are zero, any subsequent
1838 * configure endpoint command will leave that endpoint's state
1839 * untouched. Make sure we don't leave any old state in the input
1840 * endpoint contexts.
1842 ctrl_ctx
->drop_flags
= 0;
1843 ctrl_ctx
->add_flags
= 0;
1844 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->in_ctx
);
1845 slot_ctx
->dev_info
&= cpu_to_le32(~LAST_CTX_MASK
);
1846 /* Endpoint 0 is always valid */
1847 slot_ctx
->dev_info
|= cpu_to_le32(LAST_CTX(1));
1848 for (i
= 1; i
< 31; ++i
) {
1849 ep_ctx
= xhci_get_ep_ctx(xhci
, virt_dev
->in_ctx
, i
);
1850 ep_ctx
->ep_info
= 0;
1851 ep_ctx
->ep_info2
= 0;
1853 ep_ctx
->tx_info
= 0;
1857 static int xhci_configure_endpoint_result(struct xhci_hcd
*xhci
,
1858 struct usb_device
*udev
, u32
*cmd_status
)
1862 switch (*cmd_status
) {
1863 case COMP_CMD_ABORT
:
1865 xhci_warn(xhci
, "Timeout while waiting for configure endpoint command\n");
1869 dev_warn(&udev
->dev
,
1870 "Not enough host controller resources for new device state.\n");
1872 /* FIXME: can we allocate more resources for the HC? */
1875 case COMP_2ND_BW_ERR
:
1876 dev_warn(&udev
->dev
,
1877 "Not enough bandwidth for new device state.\n");
1879 /* FIXME: can we go back to the old state? */
1882 /* the HCD set up something wrong */
1883 dev_warn(&udev
->dev
, "ERROR: Endpoint drop flag = 0, "
1885 "and endpoint is not disabled.\n");
1889 dev_warn(&udev
->dev
,
1890 "ERROR: Incompatible device for endpoint configure command.\n");
1894 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1895 "Successful Endpoint Configure command");
1899 xhci_err(xhci
, "ERROR: unexpected command completion code 0x%x.\n",
1907 static int xhci_evaluate_context_result(struct xhci_hcd
*xhci
,
1908 struct usb_device
*udev
, u32
*cmd_status
)
1911 struct xhci_virt_device
*virt_dev
= xhci
->devs
[udev
->slot_id
];
1913 switch (*cmd_status
) {
1914 case COMP_CMD_ABORT
:
1916 xhci_warn(xhci
, "Timeout while waiting for evaluate context command\n");
1920 dev_warn(&udev
->dev
,
1921 "WARN: xHCI driver setup invalid evaluate context command.\n");
1925 dev_warn(&udev
->dev
,
1926 "WARN: slot not enabled for evaluate context command.\n");
1929 case COMP_CTX_STATE
:
1930 dev_warn(&udev
->dev
,
1931 "WARN: invalid context state for evaluate context command.\n");
1932 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 1);
1936 dev_warn(&udev
->dev
,
1937 "ERROR: Incompatible device for evaluate context command.\n");
1941 /* Max Exit Latency too large error */
1942 dev_warn(&udev
->dev
, "WARN: Max Exit Latency too large\n");
1946 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
1947 "Successful evaluate context command");
1951 xhci_err(xhci
, "ERROR: unexpected command completion code 0x%x.\n",
1959 static u32
xhci_count_num_new_endpoints(struct xhci_hcd
*xhci
,
1960 struct xhci_input_control_ctx
*ctrl_ctx
)
1962 u32 valid_add_flags
;
1963 u32 valid_drop_flags
;
1965 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1966 * (bit 1). The default control endpoint is added during the Address
1967 * Device command and is never removed until the slot is disabled.
1969 valid_add_flags
= le32_to_cpu(ctrl_ctx
->add_flags
) >> 2;
1970 valid_drop_flags
= le32_to_cpu(ctrl_ctx
->drop_flags
) >> 2;
1972 /* Use hweight32 to count the number of ones in the add flags, or
1973 * number of endpoints added. Don't count endpoints that are changed
1974 * (both added and dropped).
1976 return hweight32(valid_add_flags
) -
1977 hweight32(valid_add_flags
& valid_drop_flags
);
1980 static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd
*xhci
,
1981 struct xhci_input_control_ctx
*ctrl_ctx
)
1983 u32 valid_add_flags
;
1984 u32 valid_drop_flags
;
1986 valid_add_flags
= le32_to_cpu(ctrl_ctx
->add_flags
) >> 2;
1987 valid_drop_flags
= le32_to_cpu(ctrl_ctx
->drop_flags
) >> 2;
1989 return hweight32(valid_drop_flags
) -
1990 hweight32(valid_add_flags
& valid_drop_flags
);
1994 * We need to reserve the new number of endpoints before the configure endpoint
1995 * command completes. We can't subtract the dropped endpoints from the number
1996 * of active endpoints until the command completes because we can oversubscribe
1997 * the host in this case:
1999 * - the first configure endpoint command drops more endpoints than it adds
2000 * - a second configure endpoint command that adds more endpoints is queued
2001 * - the first configure endpoint command fails, so the config is unchanged
2002 * - the second command may succeed, even though there isn't enough resources
2004 * Must be called with xhci->lock held.
2006 static int xhci_reserve_host_resources(struct xhci_hcd
*xhci
,
2007 struct xhci_input_control_ctx
*ctrl_ctx
)
2011 added_eps
= xhci_count_num_new_endpoints(xhci
, ctrl_ctx
);
2012 if (xhci
->num_active_eps
+ added_eps
> xhci
->limit_active_eps
) {
2013 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2014 "Not enough ep ctxs: "
2015 "%u active, need to add %u, limit is %u.",
2016 xhci
->num_active_eps
, added_eps
,
2017 xhci
->limit_active_eps
);
2020 xhci
->num_active_eps
+= added_eps
;
2021 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2022 "Adding %u ep ctxs, %u now active.", added_eps
,
2023 xhci
->num_active_eps
);
2028 * The configure endpoint was failed by the xHC for some other reason, so we
2029 * need to revert the resources that failed configuration would have used.
2031 * Must be called with xhci->lock held.
2033 static void xhci_free_host_resources(struct xhci_hcd
*xhci
,
2034 struct xhci_input_control_ctx
*ctrl_ctx
)
2038 num_failed_eps
= xhci_count_num_new_endpoints(xhci
, ctrl_ctx
);
2039 xhci
->num_active_eps
-= num_failed_eps
;
2040 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2041 "Removing %u failed ep ctxs, %u now active.",
2043 xhci
->num_active_eps
);
2047 * Now that the command has completed, clean up the active endpoint count by
2048 * subtracting out the endpoints that were dropped (but not changed).
2050 * Must be called with xhci->lock held.
2052 static void xhci_finish_resource_reservation(struct xhci_hcd
*xhci
,
2053 struct xhci_input_control_ctx
*ctrl_ctx
)
2055 u32 num_dropped_eps
;
2057 num_dropped_eps
= xhci_count_num_dropped_endpoints(xhci
, ctrl_ctx
);
2058 xhci
->num_active_eps
-= num_dropped_eps
;
2059 if (num_dropped_eps
)
2060 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2061 "Removing %u dropped ep ctxs, %u now active.",
2063 xhci
->num_active_eps
);
2066 static unsigned int xhci_get_block_size(struct usb_device
*udev
)
2068 switch (udev
->speed
) {
2070 case USB_SPEED_FULL
:
2072 case USB_SPEED_HIGH
:
2074 case USB_SPEED_SUPER
:
2075 case USB_SPEED_SUPER_PLUS
:
2077 case USB_SPEED_UNKNOWN
:
2078 case USB_SPEED_WIRELESS
:
2080 /* Should never happen */
2086 xhci_get_largest_overhead(struct xhci_interval_bw
*interval_bw
)
2088 if (interval_bw
->overhead
[LS_OVERHEAD_TYPE
])
2090 if (interval_bw
->overhead
[FS_OVERHEAD_TYPE
])
2095 /* If we are changing a LS/FS device under a HS hub,
2096 * make sure (if we are activating a new TT) that the HS bus has enough
2097 * bandwidth for this new TT.
2099 static int xhci_check_tt_bw_table(struct xhci_hcd
*xhci
,
2100 struct xhci_virt_device
*virt_dev
,
2103 struct xhci_interval_bw_table
*bw_table
;
2104 struct xhci_tt_bw_info
*tt_info
;
2106 /* Find the bandwidth table for the root port this TT is attached to. */
2107 bw_table
= &xhci
->rh_bw
[virt_dev
->real_port
- 1].bw_table
;
2108 tt_info
= virt_dev
->tt_info
;
2109 /* If this TT already had active endpoints, the bandwidth for this TT
2110 * has already been added. Removing all periodic endpoints (and thus
2111 * making the TT enactive) will only decrease the bandwidth used.
2115 if (old_active_eps
== 0 && tt_info
->active_eps
!= 0) {
2116 if (bw_table
->bw_used
+ TT_HS_OVERHEAD
> HS_BW_LIMIT
)
2120 /* Not sure why we would have no new active endpoints...
2122 * Maybe because of an Evaluate Context change for a hub update or a
2123 * control endpoint 0 max packet size change?
2124 * FIXME: skip the bandwidth calculation in that case.
2129 static int xhci_check_ss_bw(struct xhci_hcd
*xhci
,
2130 struct xhci_virt_device
*virt_dev
)
2132 unsigned int bw_reserved
;
2134 bw_reserved
= DIV_ROUND_UP(SS_BW_RESERVED
*SS_BW_LIMIT_IN
, 100);
2135 if (virt_dev
->bw_table
->ss_bw_in
> (SS_BW_LIMIT_IN
- bw_reserved
))
2138 bw_reserved
= DIV_ROUND_UP(SS_BW_RESERVED
*SS_BW_LIMIT_OUT
, 100);
2139 if (virt_dev
->bw_table
->ss_bw_out
> (SS_BW_LIMIT_OUT
- bw_reserved
))
2146 * This algorithm is a very conservative estimate of the worst-case scheduling
2147 * scenario for any one interval. The hardware dynamically schedules the
2148 * packets, so we can't tell which microframe could be the limiting factor in
2149 * the bandwidth scheduling. This only takes into account periodic endpoints.
2151 * Obviously, we can't solve an NP complete problem to find the minimum worst
2152 * case scenario. Instead, we come up with an estimate that is no less than
2153 * the worst case bandwidth used for any one microframe, but may be an
2156 * We walk the requirements for each endpoint by interval, starting with the
2157 * smallest interval, and place packets in the schedule where there is only one
2158 * possible way to schedule packets for that interval. In order to simplify
2159 * this algorithm, we record the largest max packet size for each interval, and
2160 * assume all packets will be that size.
2162 * For interval 0, we obviously must schedule all packets for each interval.
2163 * The bandwidth for interval 0 is just the amount of data to be transmitted
2164 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2165 * the number of packets).
2167 * For interval 1, we have two possible microframes to schedule those packets
2168 * in. For this algorithm, if we can schedule the same number of packets for
2169 * each possible scheduling opportunity (each microframe), we will do so. The
2170 * remaining number of packets will be saved to be transmitted in the gaps in
2171 * the next interval's scheduling sequence.
2173 * As we move those remaining packets to be scheduled with interval 2 packets,
2174 * we have to double the number of remaining packets to transmit. This is
2175 * because the intervals are actually powers of 2, and we would be transmitting
2176 * the previous interval's packets twice in this interval. We also have to be
2177 * sure that when we look at the largest max packet size for this interval, we
2178 * also look at the largest max packet size for the remaining packets and take
2179 * the greater of the two.
2181 * The algorithm continues to evenly distribute packets in each scheduling
2182 * opportunity, and push the remaining packets out, until we get to the last
2183 * interval. Then those packets and their associated overhead are just added
2184 * to the bandwidth used.
2186 static int xhci_check_bw_table(struct xhci_hcd
*xhci
,
2187 struct xhci_virt_device
*virt_dev
,
2190 unsigned int bw_reserved
;
2191 unsigned int max_bandwidth
;
2192 unsigned int bw_used
;
2193 unsigned int block_size
;
2194 struct xhci_interval_bw_table
*bw_table
;
2195 unsigned int packet_size
= 0;
2196 unsigned int overhead
= 0;
2197 unsigned int packets_transmitted
= 0;
2198 unsigned int packets_remaining
= 0;
2201 if (virt_dev
->udev
->speed
>= USB_SPEED_SUPER
)
2202 return xhci_check_ss_bw(xhci
, virt_dev
);
2204 if (virt_dev
->udev
->speed
== USB_SPEED_HIGH
) {
2205 max_bandwidth
= HS_BW_LIMIT
;
2206 /* Convert percent of bus BW reserved to blocks reserved */
2207 bw_reserved
= DIV_ROUND_UP(HS_BW_RESERVED
* max_bandwidth
, 100);
2209 max_bandwidth
= FS_BW_LIMIT
;
2210 bw_reserved
= DIV_ROUND_UP(FS_BW_RESERVED
* max_bandwidth
, 100);
2213 bw_table
= virt_dev
->bw_table
;
2214 /* We need to translate the max packet size and max ESIT payloads into
2215 * the units the hardware uses.
2217 block_size
= xhci_get_block_size(virt_dev
->udev
);
2219 /* If we are manipulating a LS/FS device under a HS hub, double check
2220 * that the HS bus has enough bandwidth if we are activing a new TT.
2222 if (virt_dev
->tt_info
) {
2223 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2224 "Recalculating BW for rootport %u",
2225 virt_dev
->real_port
);
2226 if (xhci_check_tt_bw_table(xhci
, virt_dev
, old_active_eps
)) {
2227 xhci_warn(xhci
, "Not enough bandwidth on HS bus for "
2228 "newly activated TT.\n");
2231 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2232 "Recalculating BW for TT slot %u port %u",
2233 virt_dev
->tt_info
->slot_id
,
2234 virt_dev
->tt_info
->ttport
);
2236 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2237 "Recalculating BW for rootport %u",
2238 virt_dev
->real_port
);
2241 /* Add in how much bandwidth will be used for interval zero, or the
2242 * rounded max ESIT payload + number of packets * largest overhead.
2244 bw_used
= DIV_ROUND_UP(bw_table
->interval0_esit_payload
, block_size
) +
2245 bw_table
->interval_bw
[0].num_packets
*
2246 xhci_get_largest_overhead(&bw_table
->interval_bw
[0]);
2248 for (i
= 1; i
< XHCI_MAX_INTERVAL
; i
++) {
2249 unsigned int bw_added
;
2250 unsigned int largest_mps
;
2251 unsigned int interval_overhead
;
2254 * How many packets could we transmit in this interval?
2255 * If packets didn't fit in the previous interval, we will need
2256 * to transmit that many packets twice within this interval.
2258 packets_remaining
= 2 * packets_remaining
+
2259 bw_table
->interval_bw
[i
].num_packets
;
2261 /* Find the largest max packet size of this or the previous
2264 if (list_empty(&bw_table
->interval_bw
[i
].endpoints
))
2267 struct xhci_virt_ep
*virt_ep
;
2268 struct list_head
*ep_entry
;
2270 ep_entry
= bw_table
->interval_bw
[i
].endpoints
.next
;
2271 virt_ep
= list_entry(ep_entry
,
2272 struct xhci_virt_ep
, bw_endpoint_list
);
2273 /* Convert to blocks, rounding up */
2274 largest_mps
= DIV_ROUND_UP(
2275 virt_ep
->bw_info
.max_packet_size
,
2278 if (largest_mps
> packet_size
)
2279 packet_size
= largest_mps
;
2281 /* Use the larger overhead of this or the previous interval. */
2282 interval_overhead
= xhci_get_largest_overhead(
2283 &bw_table
->interval_bw
[i
]);
2284 if (interval_overhead
> overhead
)
2285 overhead
= interval_overhead
;
2287 /* How many packets can we evenly distribute across
2288 * (1 << (i + 1)) possible scheduling opportunities?
2290 packets_transmitted
= packets_remaining
>> (i
+ 1);
2292 /* Add in the bandwidth used for those scheduled packets */
2293 bw_added
= packets_transmitted
* (overhead
+ packet_size
);
2295 /* How many packets do we have remaining to transmit? */
2296 packets_remaining
= packets_remaining
% (1 << (i
+ 1));
2298 /* What largest max packet size should those packets have? */
2299 /* If we've transmitted all packets, don't carry over the
2300 * largest packet size.
2302 if (packets_remaining
== 0) {
2305 } else if (packets_transmitted
> 0) {
2306 /* Otherwise if we do have remaining packets, and we've
2307 * scheduled some packets in this interval, take the
2308 * largest max packet size from endpoints with this
2311 packet_size
= largest_mps
;
2312 overhead
= interval_overhead
;
2314 /* Otherwise carry over packet_size and overhead from the last
2315 * time we had a remainder.
2317 bw_used
+= bw_added
;
2318 if (bw_used
> max_bandwidth
) {
2319 xhci_warn(xhci
, "Not enough bandwidth. "
2320 "Proposed: %u, Max: %u\n",
2321 bw_used
, max_bandwidth
);
2326 * Ok, we know we have some packets left over after even-handedly
2327 * scheduling interval 15. We don't know which microframes they will
2328 * fit into, so we over-schedule and say they will be scheduled every
2331 if (packets_remaining
> 0)
2332 bw_used
+= overhead
+ packet_size
;
2334 if (!virt_dev
->tt_info
&& virt_dev
->udev
->speed
== USB_SPEED_HIGH
) {
2335 unsigned int port_index
= virt_dev
->real_port
- 1;
2337 /* OK, we're manipulating a HS device attached to a
2338 * root port bandwidth domain. Include the number of active TTs
2339 * in the bandwidth used.
2341 bw_used
+= TT_HS_OVERHEAD
*
2342 xhci
->rh_bw
[port_index
].num_active_tts
;
2345 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2346 "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2347 "Available: %u " "percent",
2348 bw_used
, max_bandwidth
, bw_reserved
,
2349 (max_bandwidth
- bw_used
- bw_reserved
) * 100 /
2352 bw_used
+= bw_reserved
;
2353 if (bw_used
> max_bandwidth
) {
2354 xhci_warn(xhci
, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2355 bw_used
, max_bandwidth
);
2359 bw_table
->bw_used
= bw_used
;
2363 static bool xhci_is_async_ep(unsigned int ep_type
)
2365 return (ep_type
!= ISOC_OUT_EP
&& ep_type
!= INT_OUT_EP
&&
2366 ep_type
!= ISOC_IN_EP
&&
2367 ep_type
!= INT_IN_EP
);
2370 static bool xhci_is_sync_in_ep(unsigned int ep_type
)
2372 return (ep_type
== ISOC_IN_EP
|| ep_type
== INT_IN_EP
);
2375 static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info
*ep_bw
)
2377 unsigned int mps
= DIV_ROUND_UP(ep_bw
->max_packet_size
, SS_BLOCK
);
2379 if (ep_bw
->ep_interval
== 0)
2380 return SS_OVERHEAD_BURST
+
2381 (ep_bw
->mult
* ep_bw
->num_packets
*
2382 (SS_OVERHEAD
+ mps
));
2383 return DIV_ROUND_UP(ep_bw
->mult
* ep_bw
->num_packets
*
2384 (SS_OVERHEAD
+ mps
+ SS_OVERHEAD_BURST
),
2385 1 << ep_bw
->ep_interval
);
2389 void xhci_drop_ep_from_interval_table(struct xhci_hcd
*xhci
,
2390 struct xhci_bw_info
*ep_bw
,
2391 struct xhci_interval_bw_table
*bw_table
,
2392 struct usb_device
*udev
,
2393 struct xhci_virt_ep
*virt_ep
,
2394 struct xhci_tt_bw_info
*tt_info
)
2396 struct xhci_interval_bw
*interval_bw
;
2397 int normalized_interval
;
2399 if (xhci_is_async_ep(ep_bw
->type
))
2402 if (udev
->speed
>= USB_SPEED_SUPER
) {
2403 if (xhci_is_sync_in_ep(ep_bw
->type
))
2404 xhci
->devs
[udev
->slot_id
]->bw_table
->ss_bw_in
-=
2405 xhci_get_ss_bw_consumed(ep_bw
);
2407 xhci
->devs
[udev
->slot_id
]->bw_table
->ss_bw_out
-=
2408 xhci_get_ss_bw_consumed(ep_bw
);
2412 /* SuperSpeed endpoints never get added to intervals in the table, so
2413 * this check is only valid for HS/FS/LS devices.
2415 if (list_empty(&virt_ep
->bw_endpoint_list
))
2417 /* For LS/FS devices, we need to translate the interval expressed in
2418 * microframes to frames.
2420 if (udev
->speed
== USB_SPEED_HIGH
)
2421 normalized_interval
= ep_bw
->ep_interval
;
2423 normalized_interval
= ep_bw
->ep_interval
- 3;
2425 if (normalized_interval
== 0)
2426 bw_table
->interval0_esit_payload
-= ep_bw
->max_esit_payload
;
2427 interval_bw
= &bw_table
->interval_bw
[normalized_interval
];
2428 interval_bw
->num_packets
-= ep_bw
->num_packets
;
2429 switch (udev
->speed
) {
2431 interval_bw
->overhead
[LS_OVERHEAD_TYPE
] -= 1;
2433 case USB_SPEED_FULL
:
2434 interval_bw
->overhead
[FS_OVERHEAD_TYPE
] -= 1;
2436 case USB_SPEED_HIGH
:
2437 interval_bw
->overhead
[HS_OVERHEAD_TYPE
] -= 1;
2439 case USB_SPEED_SUPER
:
2440 case USB_SPEED_SUPER_PLUS
:
2441 case USB_SPEED_UNKNOWN
:
2442 case USB_SPEED_WIRELESS
:
2443 /* Should never happen because only LS/FS/HS endpoints will get
2444 * added to the endpoint list.
2449 tt_info
->active_eps
-= 1;
2450 list_del_init(&virt_ep
->bw_endpoint_list
);
2453 static void xhci_add_ep_to_interval_table(struct xhci_hcd
*xhci
,
2454 struct xhci_bw_info
*ep_bw
,
2455 struct xhci_interval_bw_table
*bw_table
,
2456 struct usb_device
*udev
,
2457 struct xhci_virt_ep
*virt_ep
,
2458 struct xhci_tt_bw_info
*tt_info
)
2460 struct xhci_interval_bw
*interval_bw
;
2461 struct xhci_virt_ep
*smaller_ep
;
2462 int normalized_interval
;
2464 if (xhci_is_async_ep(ep_bw
->type
))
2467 if (udev
->speed
== USB_SPEED_SUPER
) {
2468 if (xhci_is_sync_in_ep(ep_bw
->type
))
2469 xhci
->devs
[udev
->slot_id
]->bw_table
->ss_bw_in
+=
2470 xhci_get_ss_bw_consumed(ep_bw
);
2472 xhci
->devs
[udev
->slot_id
]->bw_table
->ss_bw_out
+=
2473 xhci_get_ss_bw_consumed(ep_bw
);
2477 /* For LS/FS devices, we need to translate the interval expressed in
2478 * microframes to frames.
2480 if (udev
->speed
== USB_SPEED_HIGH
)
2481 normalized_interval
= ep_bw
->ep_interval
;
2483 normalized_interval
= ep_bw
->ep_interval
- 3;
2485 if (normalized_interval
== 0)
2486 bw_table
->interval0_esit_payload
+= ep_bw
->max_esit_payload
;
2487 interval_bw
= &bw_table
->interval_bw
[normalized_interval
];
2488 interval_bw
->num_packets
+= ep_bw
->num_packets
;
2489 switch (udev
->speed
) {
2491 interval_bw
->overhead
[LS_OVERHEAD_TYPE
] += 1;
2493 case USB_SPEED_FULL
:
2494 interval_bw
->overhead
[FS_OVERHEAD_TYPE
] += 1;
2496 case USB_SPEED_HIGH
:
2497 interval_bw
->overhead
[HS_OVERHEAD_TYPE
] += 1;
2499 case USB_SPEED_SUPER
:
2500 case USB_SPEED_SUPER_PLUS
:
2501 case USB_SPEED_UNKNOWN
:
2502 case USB_SPEED_WIRELESS
:
2503 /* Should never happen because only LS/FS/HS endpoints will get
2504 * added to the endpoint list.
2510 tt_info
->active_eps
+= 1;
2511 /* Insert the endpoint into the list, largest max packet size first. */
2512 list_for_each_entry(smaller_ep
, &interval_bw
->endpoints
,
2514 if (ep_bw
->max_packet_size
>=
2515 smaller_ep
->bw_info
.max_packet_size
) {
2516 /* Add the new ep before the smaller endpoint */
2517 list_add_tail(&virt_ep
->bw_endpoint_list
,
2518 &smaller_ep
->bw_endpoint_list
);
2522 /* Add the new endpoint at the end of the list. */
2523 list_add_tail(&virt_ep
->bw_endpoint_list
,
2524 &interval_bw
->endpoints
);
2527 void xhci_update_tt_active_eps(struct xhci_hcd
*xhci
,
2528 struct xhci_virt_device
*virt_dev
,
2531 struct xhci_root_port_bw_info
*rh_bw_info
;
2532 if (!virt_dev
->tt_info
)
2535 rh_bw_info
= &xhci
->rh_bw
[virt_dev
->real_port
- 1];
2536 if (old_active_eps
== 0 &&
2537 virt_dev
->tt_info
->active_eps
!= 0) {
2538 rh_bw_info
->num_active_tts
+= 1;
2539 rh_bw_info
->bw_table
.bw_used
+= TT_HS_OVERHEAD
;
2540 } else if (old_active_eps
!= 0 &&
2541 virt_dev
->tt_info
->active_eps
== 0) {
2542 rh_bw_info
->num_active_tts
-= 1;
2543 rh_bw_info
->bw_table
.bw_used
-= TT_HS_OVERHEAD
;
2547 static int xhci_reserve_bandwidth(struct xhci_hcd
*xhci
,
2548 struct xhci_virt_device
*virt_dev
,
2549 struct xhci_container_ctx
*in_ctx
)
2551 struct xhci_bw_info ep_bw_info
[31];
2553 struct xhci_input_control_ctx
*ctrl_ctx
;
2554 int old_active_eps
= 0;
2556 if (virt_dev
->tt_info
)
2557 old_active_eps
= virt_dev
->tt_info
->active_eps
;
2559 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
2561 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
2566 for (i
= 0; i
< 31; i
++) {
2567 if (!EP_IS_ADDED(ctrl_ctx
, i
) && !EP_IS_DROPPED(ctrl_ctx
, i
))
2570 /* Make a copy of the BW info in case we need to revert this */
2571 memcpy(&ep_bw_info
[i
], &virt_dev
->eps
[i
].bw_info
,
2572 sizeof(ep_bw_info
[i
]));
2573 /* Drop the endpoint from the interval table if the endpoint is
2574 * being dropped or changed.
2576 if (EP_IS_DROPPED(ctrl_ctx
, i
))
2577 xhci_drop_ep_from_interval_table(xhci
,
2578 &virt_dev
->eps
[i
].bw_info
,
2584 /* Overwrite the information stored in the endpoints' bw_info */
2585 xhci_update_bw_info(xhci
, virt_dev
->in_ctx
, ctrl_ctx
, virt_dev
);
2586 for (i
= 0; i
< 31; i
++) {
2587 /* Add any changed or added endpoints to the interval table */
2588 if (EP_IS_ADDED(ctrl_ctx
, i
))
2589 xhci_add_ep_to_interval_table(xhci
,
2590 &virt_dev
->eps
[i
].bw_info
,
2597 if (!xhci_check_bw_table(xhci
, virt_dev
, old_active_eps
)) {
2598 /* Ok, this fits in the bandwidth we have.
2599 * Update the number of active TTs.
2601 xhci_update_tt_active_eps(xhci
, virt_dev
, old_active_eps
);
2605 /* We don't have enough bandwidth for this, revert the stored info. */
2606 for (i
= 0; i
< 31; i
++) {
2607 if (!EP_IS_ADDED(ctrl_ctx
, i
) && !EP_IS_DROPPED(ctrl_ctx
, i
))
2610 /* Drop the new copies of any added or changed endpoints from
2611 * the interval table.
2613 if (EP_IS_ADDED(ctrl_ctx
, i
)) {
2614 xhci_drop_ep_from_interval_table(xhci
,
2615 &virt_dev
->eps
[i
].bw_info
,
2621 /* Revert the endpoint back to its old information */
2622 memcpy(&virt_dev
->eps
[i
].bw_info
, &ep_bw_info
[i
],
2623 sizeof(ep_bw_info
[i
]));
2624 /* Add any changed or dropped endpoints back into the table */
2625 if (EP_IS_DROPPED(ctrl_ctx
, i
))
2626 xhci_add_ep_to_interval_table(xhci
,
2627 &virt_dev
->eps
[i
].bw_info
,
2637 /* Issue a configure endpoint command or evaluate context command
2638 * and wait for it to finish.
2640 static int xhci_configure_endpoint(struct xhci_hcd
*xhci
,
2641 struct usb_device
*udev
,
2642 struct xhci_command
*command
,
2643 bool ctx_change
, bool must_succeed
)
2646 unsigned long flags
;
2647 struct xhci_input_control_ctx
*ctrl_ctx
;
2648 struct xhci_virt_device
*virt_dev
;
2653 spin_lock_irqsave(&xhci
->lock
, flags
);
2654 virt_dev
= xhci
->devs
[udev
->slot_id
];
2656 ctrl_ctx
= xhci_get_input_control_ctx(command
->in_ctx
);
2658 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2659 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
2664 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
) &&
2665 xhci_reserve_host_resources(xhci
, ctrl_ctx
)) {
2666 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2667 xhci_warn(xhci
, "Not enough host resources, "
2668 "active endpoint contexts = %u\n",
2669 xhci
->num_active_eps
);
2672 if ((xhci
->quirks
& XHCI_SW_BW_CHECKING
) &&
2673 xhci_reserve_bandwidth(xhci
, virt_dev
, command
->in_ctx
)) {
2674 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
))
2675 xhci_free_host_resources(xhci
, ctrl_ctx
);
2676 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2677 xhci_warn(xhci
, "Not enough bandwidth\n");
2682 ret
= xhci_queue_configure_endpoint(xhci
, command
,
2683 command
->in_ctx
->dma
,
2684 udev
->slot_id
, must_succeed
);
2686 ret
= xhci_queue_evaluate_context(xhci
, command
,
2687 command
->in_ctx
->dma
,
2688 udev
->slot_id
, must_succeed
);
2690 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
))
2691 xhci_free_host_resources(xhci
, ctrl_ctx
);
2692 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2693 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
2694 "FIXME allocate a new ring segment");
2697 xhci_ring_cmd_db(xhci
);
2698 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2700 /* Wait for the configure endpoint command to complete */
2701 wait_for_completion(command
->completion
);
2704 ret
= xhci_configure_endpoint_result(xhci
, udev
,
2707 ret
= xhci_evaluate_context_result(xhci
, udev
,
2710 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
)) {
2711 spin_lock_irqsave(&xhci
->lock
, flags
);
2712 /* If the command failed, remove the reserved resources.
2713 * Otherwise, clean up the estimate to include dropped eps.
2716 xhci_free_host_resources(xhci
, ctrl_ctx
);
2718 xhci_finish_resource_reservation(xhci
, ctrl_ctx
);
2719 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2724 static void xhci_check_bw_drop_ep_streams(struct xhci_hcd
*xhci
,
2725 struct xhci_virt_device
*vdev
, int i
)
2727 struct xhci_virt_ep
*ep
= &vdev
->eps
[i
];
2729 if (ep
->ep_state
& EP_HAS_STREAMS
) {
2730 xhci_warn(xhci
, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n",
2731 xhci_get_endpoint_address(i
));
2732 xhci_free_stream_info(xhci
, ep
->stream_info
);
2733 ep
->stream_info
= NULL
;
2734 ep
->ep_state
&= ~EP_HAS_STREAMS
;
2738 /* Called after one or more calls to xhci_add_endpoint() or
2739 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2740 * to call xhci_reset_bandwidth().
2742 * Since we are in the middle of changing either configuration or
2743 * installing a new alt setting, the USB core won't allow URBs to be
2744 * enqueued for any endpoint on the old config or interface. Nothing
2745 * else should be touching the xhci->devs[slot_id] structure, so we
2746 * don't need to take the xhci->lock for manipulating that.
2748 int xhci_check_bandwidth(struct usb_hcd
*hcd
, struct usb_device
*udev
)
2752 struct xhci_hcd
*xhci
;
2753 struct xhci_virt_device
*virt_dev
;
2754 struct xhci_input_control_ctx
*ctrl_ctx
;
2755 struct xhci_slot_ctx
*slot_ctx
;
2756 struct xhci_command
*command
;
2758 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, true, __func__
);
2761 xhci
= hcd_to_xhci(hcd
);
2762 if ((xhci
->xhc_state
& XHCI_STATE_DYING
) ||
2763 (xhci
->xhc_state
& XHCI_STATE_REMOVING
))
2766 xhci_dbg(xhci
, "%s called for udev %p\n", __func__
, udev
);
2767 virt_dev
= xhci
->devs
[udev
->slot_id
];
2769 command
= xhci_alloc_command(xhci
, false, true, GFP_KERNEL
);
2773 command
->in_ctx
= virt_dev
->in_ctx
;
2775 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
2776 ctrl_ctx
= xhci_get_input_control_ctx(command
->in_ctx
);
2778 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
2781 goto command_cleanup
;
2783 ctrl_ctx
->add_flags
|= cpu_to_le32(SLOT_FLAG
);
2784 ctrl_ctx
->add_flags
&= cpu_to_le32(~EP0_FLAG
);
2785 ctrl_ctx
->drop_flags
&= cpu_to_le32(~(SLOT_FLAG
| EP0_FLAG
));
2787 /* Don't issue the command if there's no endpoints to update. */
2788 if (ctrl_ctx
->add_flags
== cpu_to_le32(SLOT_FLAG
) &&
2789 ctrl_ctx
->drop_flags
== 0) {
2791 goto command_cleanup
;
2793 /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
2794 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->in_ctx
);
2795 for (i
= 31; i
>= 1; i
--) {
2796 __le32 le32
= cpu_to_le32(BIT(i
));
2798 if ((virt_dev
->eps
[i
-1].ring
&& !(ctrl_ctx
->drop_flags
& le32
))
2799 || (ctrl_ctx
->add_flags
& le32
) || i
== 1) {
2800 slot_ctx
->dev_info
&= cpu_to_le32(~LAST_CTX_MASK
);
2801 slot_ctx
->dev_info
|= cpu_to_le32(LAST_CTX(i
));
2805 xhci_dbg(xhci
, "New Input Control Context:\n");
2806 xhci_dbg_ctx(xhci
, virt_dev
->in_ctx
,
2807 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx
->dev_info
)));
2809 ret
= xhci_configure_endpoint(xhci
, udev
, command
,
2812 /* Callee should call reset_bandwidth() */
2813 goto command_cleanup
;
2815 xhci_dbg(xhci
, "Output context after successful config ep cmd:\n");
2816 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
,
2817 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx
->dev_info
)));
2819 /* Free any rings that were dropped, but not changed. */
2820 for (i
= 1; i
< 31; ++i
) {
2821 if ((le32_to_cpu(ctrl_ctx
->drop_flags
) & (1 << (i
+ 1))) &&
2822 !(le32_to_cpu(ctrl_ctx
->add_flags
) & (1 << (i
+ 1)))) {
2823 xhci_free_or_cache_endpoint_ring(xhci
, virt_dev
, i
);
2824 xhci_check_bw_drop_ep_streams(xhci
, virt_dev
, i
);
2827 xhci_zero_in_ctx(xhci
, virt_dev
);
2829 * Install any rings for completely new endpoints or changed endpoints,
2830 * and free or cache any old rings from changed endpoints.
2832 for (i
= 1; i
< 31; ++i
) {
2833 if (!virt_dev
->eps
[i
].new_ring
)
2835 /* Only cache or free the old ring if it exists.
2836 * It may not if this is the first add of an endpoint.
2838 if (virt_dev
->eps
[i
].ring
) {
2839 xhci_free_or_cache_endpoint_ring(xhci
, virt_dev
, i
);
2841 xhci_check_bw_drop_ep_streams(xhci
, virt_dev
, i
);
2842 virt_dev
->eps
[i
].ring
= virt_dev
->eps
[i
].new_ring
;
2843 virt_dev
->eps
[i
].new_ring
= NULL
;
2846 kfree(command
->completion
);
2852 void xhci_reset_bandwidth(struct usb_hcd
*hcd
, struct usb_device
*udev
)
2854 struct xhci_hcd
*xhci
;
2855 struct xhci_virt_device
*virt_dev
;
2858 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, true, __func__
);
2861 xhci
= hcd_to_xhci(hcd
);
2863 xhci_dbg(xhci
, "%s called for udev %p\n", __func__
, udev
);
2864 virt_dev
= xhci
->devs
[udev
->slot_id
];
2865 /* Free any rings allocated for added endpoints */
2866 for (i
= 0; i
< 31; ++i
) {
2867 if (virt_dev
->eps
[i
].new_ring
) {
2868 xhci_ring_free(xhci
, virt_dev
->eps
[i
].new_ring
);
2869 virt_dev
->eps
[i
].new_ring
= NULL
;
2872 xhci_zero_in_ctx(xhci
, virt_dev
);
2875 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd
*xhci
,
2876 struct xhci_container_ctx
*in_ctx
,
2877 struct xhci_container_ctx
*out_ctx
,
2878 struct xhci_input_control_ctx
*ctrl_ctx
,
2879 u32 add_flags
, u32 drop_flags
)
2881 ctrl_ctx
->add_flags
= cpu_to_le32(add_flags
);
2882 ctrl_ctx
->drop_flags
= cpu_to_le32(drop_flags
);
2883 xhci_slot_copy(xhci
, in_ctx
, out_ctx
);
2884 ctrl_ctx
->add_flags
|= cpu_to_le32(SLOT_FLAG
);
2886 xhci_dbg(xhci
, "Input Context:\n");
2887 xhci_dbg_ctx(xhci
, in_ctx
, xhci_last_valid_endpoint(add_flags
));
2890 static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd
*xhci
,
2891 unsigned int slot_id
, unsigned int ep_index
,
2892 struct xhci_dequeue_state
*deq_state
)
2894 struct xhci_input_control_ctx
*ctrl_ctx
;
2895 struct xhci_container_ctx
*in_ctx
;
2896 struct xhci_ep_ctx
*ep_ctx
;
2900 in_ctx
= xhci
->devs
[slot_id
]->in_ctx
;
2901 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
2903 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
2908 xhci_endpoint_copy(xhci
, xhci
->devs
[slot_id
]->in_ctx
,
2909 xhci
->devs
[slot_id
]->out_ctx
, ep_index
);
2910 ep_ctx
= xhci_get_ep_ctx(xhci
, in_ctx
, ep_index
);
2911 addr
= xhci_trb_virt_to_dma(deq_state
->new_deq_seg
,
2912 deq_state
->new_deq_ptr
);
2914 xhci_warn(xhci
, "WARN Cannot submit config ep after "
2915 "reset ep command\n");
2916 xhci_warn(xhci
, "WARN deq seg = %p, deq ptr = %p\n",
2917 deq_state
->new_deq_seg
,
2918 deq_state
->new_deq_ptr
);
2921 ep_ctx
->deq
= cpu_to_le64(addr
| deq_state
->new_cycle_state
);
2923 added_ctxs
= xhci_get_endpoint_flag_from_index(ep_index
);
2924 xhci_setup_input_ctx_for_config_ep(xhci
, xhci
->devs
[slot_id
]->in_ctx
,
2925 xhci
->devs
[slot_id
]->out_ctx
, ctrl_ctx
,
2926 added_ctxs
, added_ctxs
);
2929 void xhci_cleanup_stalled_ring(struct xhci_hcd
*xhci
,
2930 unsigned int ep_index
, struct xhci_td
*td
)
2932 struct xhci_dequeue_state deq_state
;
2933 struct xhci_virt_ep
*ep
;
2934 struct usb_device
*udev
= td
->urb
->dev
;
2936 xhci_dbg_trace(xhci
, trace_xhci_dbg_reset_ep
,
2937 "Cleaning up stalled endpoint ring");
2938 ep
= &xhci
->devs
[udev
->slot_id
]->eps
[ep_index
];
2939 /* We need to move the HW's dequeue pointer past this TD,
2940 * or it will attempt to resend it on the next doorbell ring.
2942 xhci_find_new_dequeue_state(xhci
, udev
->slot_id
,
2943 ep_index
, ep
->stopped_stream
, td
, &deq_state
);
2945 if (!deq_state
.new_deq_ptr
|| !deq_state
.new_deq_seg
)
2948 /* HW with the reset endpoint quirk will use the saved dequeue state to
2949 * issue a configure endpoint command later.
2951 if (!(xhci
->quirks
& XHCI_RESET_EP_QUIRK
)) {
2952 xhci_dbg_trace(xhci
, trace_xhci_dbg_reset_ep
,
2953 "Queueing new dequeue state");
2954 xhci_queue_new_dequeue_state(xhci
, udev
->slot_id
,
2955 ep_index
, ep
->stopped_stream
, &deq_state
);
2957 /* Better hope no one uses the input context between now and the
2958 * reset endpoint completion!
2959 * XXX: No idea how this hardware will react when stream rings
2962 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
2963 "Setting up input context for "
2964 "configure endpoint command");
2965 xhci_setup_input_ctx_for_quirk(xhci
, udev
->slot_id
,
2966 ep_index
, &deq_state
);
2970 /* Called when clearing halted device. The core should have sent the control
2971 * message to clear the device halt condition. The host side of the halt should
2972 * already be cleared with a reset endpoint command issued when the STALL tx
2973 * event was received.
2975 * Context: in_interrupt
2978 void xhci_endpoint_reset(struct usb_hcd
*hcd
,
2979 struct usb_host_endpoint
*ep
)
2981 struct xhci_hcd
*xhci
;
2983 xhci
= hcd_to_xhci(hcd
);
2986 * We might need to implement the config ep cmd in xhci 4.8.1 note:
2987 * The Reset Endpoint Command may only be issued to endpoints in the
2988 * Halted state. If software wishes reset the Data Toggle or Sequence
2989 * Number of an endpoint that isn't in the Halted state, then software
2990 * may issue a Configure Endpoint Command with the Drop and Add bits set
2991 * for the target endpoint. that is in the Stopped state.
2994 /* For now just print debug to follow the situation */
2995 xhci_dbg(xhci
, "Endpoint 0x%x ep reset callback called\n",
2996 ep
->desc
.bEndpointAddress
);
2999 static int xhci_check_streams_endpoint(struct xhci_hcd
*xhci
,
3000 struct usb_device
*udev
, struct usb_host_endpoint
*ep
,
3001 unsigned int slot_id
)
3004 unsigned int ep_index
;
3005 unsigned int ep_state
;
3009 ret
= xhci_check_args(xhci_to_hcd(xhci
), udev
, ep
, 1, true, __func__
);
3012 if (usb_ss_max_streams(&ep
->ss_ep_comp
) == 0) {
3013 xhci_warn(xhci
, "WARN: SuperSpeed Endpoint Companion"
3014 " descriptor for ep 0x%x does not support streams\n",
3015 ep
->desc
.bEndpointAddress
);
3019 ep_index
= xhci_get_endpoint_index(&ep
->desc
);
3020 ep_state
= xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
;
3021 if (ep_state
& EP_HAS_STREAMS
||
3022 ep_state
& EP_GETTING_STREAMS
) {
3023 xhci_warn(xhci
, "WARN: SuperSpeed bulk endpoint 0x%x "
3024 "already has streams set up.\n",
3025 ep
->desc
.bEndpointAddress
);
3026 xhci_warn(xhci
, "Send email to xHCI maintainer and ask for "
3027 "dynamic stream context array reallocation.\n");
3030 if (!list_empty(&xhci
->devs
[slot_id
]->eps
[ep_index
].ring
->td_list
)) {
3031 xhci_warn(xhci
, "Cannot setup streams for SuperSpeed bulk "
3032 "endpoint 0x%x; URBs are pending.\n",
3033 ep
->desc
.bEndpointAddress
);
3039 static void xhci_calculate_streams_entries(struct xhci_hcd
*xhci
,
3040 unsigned int *num_streams
, unsigned int *num_stream_ctxs
)
3042 unsigned int max_streams
;
3044 /* The stream context array size must be a power of two */
3045 *num_stream_ctxs
= roundup_pow_of_two(*num_streams
);
3047 * Find out how many primary stream array entries the host controller
3048 * supports. Later we may use secondary stream arrays (similar to 2nd
3049 * level page entries), but that's an optional feature for xHCI host
3050 * controllers. xHCs must support at least 4 stream IDs.
3052 max_streams
= HCC_MAX_PSA(xhci
->hcc_params
);
3053 if (*num_stream_ctxs
> max_streams
) {
3054 xhci_dbg(xhci
, "xHCI HW only supports %u stream ctx entries.\n",
3056 *num_stream_ctxs
= max_streams
;
3057 *num_streams
= max_streams
;
3061 /* Returns an error code if one of the endpoint already has streams.
3062 * This does not change any data structures, it only checks and gathers
3065 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd
*xhci
,
3066 struct usb_device
*udev
,
3067 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
3068 unsigned int *num_streams
, u32
*changed_ep_bitmask
)
3070 unsigned int max_streams
;
3071 unsigned int endpoint_flag
;
3075 for (i
= 0; i
< num_eps
; i
++) {
3076 ret
= xhci_check_streams_endpoint(xhci
, udev
,
3077 eps
[i
], udev
->slot_id
);
3081 max_streams
= usb_ss_max_streams(&eps
[i
]->ss_ep_comp
);
3082 if (max_streams
< (*num_streams
- 1)) {
3083 xhci_dbg(xhci
, "Ep 0x%x only supports %u stream IDs.\n",
3084 eps
[i
]->desc
.bEndpointAddress
,
3086 *num_streams
= max_streams
+1;
3089 endpoint_flag
= xhci_get_endpoint_flag(&eps
[i
]->desc
);
3090 if (*changed_ep_bitmask
& endpoint_flag
)
3092 *changed_ep_bitmask
|= endpoint_flag
;
3097 static u32
xhci_calculate_no_streams_bitmask(struct xhci_hcd
*xhci
,
3098 struct usb_device
*udev
,
3099 struct usb_host_endpoint
**eps
, unsigned int num_eps
)
3101 u32 changed_ep_bitmask
= 0;
3102 unsigned int slot_id
;
3103 unsigned int ep_index
;
3104 unsigned int ep_state
;
3107 slot_id
= udev
->slot_id
;
3108 if (!xhci
->devs
[slot_id
])
3111 for (i
= 0; i
< num_eps
; i
++) {
3112 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3113 ep_state
= xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
;
3114 /* Are streams already being freed for the endpoint? */
3115 if (ep_state
& EP_GETTING_NO_STREAMS
) {
3116 xhci_warn(xhci
, "WARN Can't disable streams for "
3118 "streams are being disabled already\n",
3119 eps
[i
]->desc
.bEndpointAddress
);
3122 /* Are there actually any streams to free? */
3123 if (!(ep_state
& EP_HAS_STREAMS
) &&
3124 !(ep_state
& EP_GETTING_STREAMS
)) {
3125 xhci_warn(xhci
, "WARN Can't disable streams for "
3127 "streams are already disabled!\n",
3128 eps
[i
]->desc
.bEndpointAddress
);
3129 xhci_warn(xhci
, "WARN xhci_free_streams() called "
3130 "with non-streams endpoint\n");
3133 changed_ep_bitmask
|= xhci_get_endpoint_flag(&eps
[i
]->desc
);
3135 return changed_ep_bitmask
;
3139 * The USB device drivers use this function (through the HCD interface in USB
3140 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3141 * coordinate mass storage command queueing across multiple endpoints (basically
3142 * a stream ID == a task ID).
3144 * Setting up streams involves allocating the same size stream context array
3145 * for each endpoint and issuing a configure endpoint command for all endpoints.
3147 * Don't allow the call to succeed if one endpoint only supports one stream
3148 * (which means it doesn't support streams at all).
3150 * Drivers may get less stream IDs than they asked for, if the host controller
3151 * hardware or endpoints claim they can't support the number of requested
3154 int xhci_alloc_streams(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3155 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
3156 unsigned int num_streams
, gfp_t mem_flags
)
3159 struct xhci_hcd
*xhci
;
3160 struct xhci_virt_device
*vdev
;
3161 struct xhci_command
*config_cmd
;
3162 struct xhci_input_control_ctx
*ctrl_ctx
;
3163 unsigned int ep_index
;
3164 unsigned int num_stream_ctxs
;
3165 unsigned int max_packet
;
3166 unsigned long flags
;
3167 u32 changed_ep_bitmask
= 0;
3172 /* Add one to the number of streams requested to account for
3173 * stream 0 that is reserved for xHCI usage.
3176 xhci
= hcd_to_xhci(hcd
);
3177 xhci_dbg(xhci
, "Driver wants %u stream IDs (including stream 0).\n",
3180 /* MaxPSASize value 0 (2 streams) means streams are not supported */
3181 if ((xhci
->quirks
& XHCI_BROKEN_STREAMS
) ||
3182 HCC_MAX_PSA(xhci
->hcc_params
) < 4) {
3183 xhci_dbg(xhci
, "xHCI controller does not support streams.\n");
3187 config_cmd
= xhci_alloc_command(xhci
, true, true, mem_flags
);
3189 xhci_dbg(xhci
, "Could not allocate xHCI command structure.\n");
3192 ctrl_ctx
= xhci_get_input_control_ctx(config_cmd
->in_ctx
);
3194 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
3196 xhci_free_command(xhci
, config_cmd
);
3200 /* Check to make sure all endpoints are not already configured for
3201 * streams. While we're at it, find the maximum number of streams that
3202 * all the endpoints will support and check for duplicate endpoints.
3204 spin_lock_irqsave(&xhci
->lock
, flags
);
3205 ret
= xhci_calculate_streams_and_bitmask(xhci
, udev
, eps
,
3206 num_eps
, &num_streams
, &changed_ep_bitmask
);
3208 xhci_free_command(xhci
, config_cmd
);
3209 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3212 if (num_streams
<= 1) {
3213 xhci_warn(xhci
, "WARN: endpoints can't handle "
3214 "more than one stream.\n");
3215 xhci_free_command(xhci
, config_cmd
);
3216 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3219 vdev
= xhci
->devs
[udev
->slot_id
];
3220 /* Mark each endpoint as being in transition, so
3221 * xhci_urb_enqueue() will reject all URBs.
3223 for (i
= 0; i
< num_eps
; i
++) {
3224 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3225 vdev
->eps
[ep_index
].ep_state
|= EP_GETTING_STREAMS
;
3227 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3229 /* Setup internal data structures and allocate HW data structures for
3230 * streams (but don't install the HW structures in the input context
3231 * until we're sure all memory allocation succeeded).
3233 xhci_calculate_streams_entries(xhci
, &num_streams
, &num_stream_ctxs
);
3234 xhci_dbg(xhci
, "Need %u stream ctx entries for %u stream IDs.\n",
3235 num_stream_ctxs
, num_streams
);
3237 for (i
= 0; i
< num_eps
; i
++) {
3238 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3239 max_packet
= GET_MAX_PACKET(usb_endpoint_maxp(&eps
[i
]->desc
));
3240 vdev
->eps
[ep_index
].stream_info
= xhci_alloc_stream_info(xhci
,
3243 max_packet
, mem_flags
);
3244 if (!vdev
->eps
[ep_index
].stream_info
)
3246 /* Set maxPstreams in endpoint context and update deq ptr to
3247 * point to stream context array. FIXME
3251 /* Set up the input context for a configure endpoint command. */
3252 for (i
= 0; i
< num_eps
; i
++) {
3253 struct xhci_ep_ctx
*ep_ctx
;
3255 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3256 ep_ctx
= xhci_get_ep_ctx(xhci
, config_cmd
->in_ctx
, ep_index
);
3258 xhci_endpoint_copy(xhci
, config_cmd
->in_ctx
,
3259 vdev
->out_ctx
, ep_index
);
3260 xhci_setup_streams_ep_input_ctx(xhci
, ep_ctx
,
3261 vdev
->eps
[ep_index
].stream_info
);
3263 /* Tell the HW to drop its old copy of the endpoint context info
3264 * and add the updated copy from the input context.
3266 xhci_setup_input_ctx_for_config_ep(xhci
, config_cmd
->in_ctx
,
3267 vdev
->out_ctx
, ctrl_ctx
,
3268 changed_ep_bitmask
, changed_ep_bitmask
);
3270 /* Issue and wait for the configure endpoint command */
3271 ret
= xhci_configure_endpoint(xhci
, udev
, config_cmd
,
3274 /* xHC rejected the configure endpoint command for some reason, so we
3275 * leave the old ring intact and free our internal streams data
3281 spin_lock_irqsave(&xhci
->lock
, flags
);
3282 for (i
= 0; i
< num_eps
; i
++) {
3283 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3284 vdev
->eps
[ep_index
].ep_state
&= ~EP_GETTING_STREAMS
;
3285 xhci_dbg(xhci
, "Slot %u ep ctx %u now has streams.\n",
3286 udev
->slot_id
, ep_index
);
3287 vdev
->eps
[ep_index
].ep_state
|= EP_HAS_STREAMS
;
3289 xhci_free_command(xhci
, config_cmd
);
3290 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3292 /* Subtract 1 for stream 0, which drivers can't use */
3293 return num_streams
- 1;
3296 /* If it didn't work, free the streams! */
3297 for (i
= 0; i
< num_eps
; i
++) {
3298 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3299 xhci_free_stream_info(xhci
, vdev
->eps
[ep_index
].stream_info
);
3300 vdev
->eps
[ep_index
].stream_info
= NULL
;
3301 /* FIXME Unset maxPstreams in endpoint context and
3302 * update deq ptr to point to normal string ring.
3304 vdev
->eps
[ep_index
].ep_state
&= ~EP_GETTING_STREAMS
;
3305 vdev
->eps
[ep_index
].ep_state
&= ~EP_HAS_STREAMS
;
3306 xhci_endpoint_zero(xhci
, vdev
, eps
[i
]);
3308 xhci_free_command(xhci
, config_cmd
);
3312 /* Transition the endpoint from using streams to being a "normal" endpoint
3315 * Modify the endpoint context state, submit a configure endpoint command,
3316 * and free all endpoint rings for streams if that completes successfully.
3318 int xhci_free_streams(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3319 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
3323 struct xhci_hcd
*xhci
;
3324 struct xhci_virt_device
*vdev
;
3325 struct xhci_command
*command
;
3326 struct xhci_input_control_ctx
*ctrl_ctx
;
3327 unsigned int ep_index
;
3328 unsigned long flags
;
3329 u32 changed_ep_bitmask
;
3331 xhci
= hcd_to_xhci(hcd
);
3332 vdev
= xhci
->devs
[udev
->slot_id
];
3334 /* Set up a configure endpoint command to remove the streams rings */
3335 spin_lock_irqsave(&xhci
->lock
, flags
);
3336 changed_ep_bitmask
= xhci_calculate_no_streams_bitmask(xhci
,
3337 udev
, eps
, num_eps
);
3338 if (changed_ep_bitmask
== 0) {
3339 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3343 /* Use the xhci_command structure from the first endpoint. We may have
3344 * allocated too many, but the driver may call xhci_free_streams() for
3345 * each endpoint it grouped into one call to xhci_alloc_streams().
3347 ep_index
= xhci_get_endpoint_index(&eps
[0]->desc
);
3348 command
= vdev
->eps
[ep_index
].stream_info
->free_streams_command
;
3349 ctrl_ctx
= xhci_get_input_control_ctx(command
->in_ctx
);
3351 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3352 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
3357 for (i
= 0; i
< num_eps
; i
++) {
3358 struct xhci_ep_ctx
*ep_ctx
;
3360 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3361 ep_ctx
= xhci_get_ep_ctx(xhci
, command
->in_ctx
, ep_index
);
3362 xhci
->devs
[udev
->slot_id
]->eps
[ep_index
].ep_state
|=
3363 EP_GETTING_NO_STREAMS
;
3365 xhci_endpoint_copy(xhci
, command
->in_ctx
,
3366 vdev
->out_ctx
, ep_index
);
3367 xhci_setup_no_streams_ep_input_ctx(ep_ctx
,
3368 &vdev
->eps
[ep_index
]);
3370 xhci_setup_input_ctx_for_config_ep(xhci
, command
->in_ctx
,
3371 vdev
->out_ctx
, ctrl_ctx
,
3372 changed_ep_bitmask
, changed_ep_bitmask
);
3373 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3375 /* Issue and wait for the configure endpoint command,
3376 * which must succeed.
3378 ret
= xhci_configure_endpoint(xhci
, udev
, command
,
3381 /* xHC rejected the configure endpoint command for some reason, so we
3382 * leave the streams rings intact.
3387 spin_lock_irqsave(&xhci
->lock
, flags
);
3388 for (i
= 0; i
< num_eps
; i
++) {
3389 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
3390 xhci_free_stream_info(xhci
, vdev
->eps
[ep_index
].stream_info
);
3391 vdev
->eps
[ep_index
].stream_info
= NULL
;
3392 /* FIXME Unset maxPstreams in endpoint context and
3393 * update deq ptr to point to normal string ring.
3395 vdev
->eps
[ep_index
].ep_state
&= ~EP_GETTING_NO_STREAMS
;
3396 vdev
->eps
[ep_index
].ep_state
&= ~EP_HAS_STREAMS
;
3398 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3404 * Deletes endpoint resources for endpoints that were active before a Reset
3405 * Device command, or a Disable Slot command. The Reset Device command leaves
3406 * the control endpoint intact, whereas the Disable Slot command deletes it.
3408 * Must be called with xhci->lock held.
3410 void xhci_free_device_endpoint_resources(struct xhci_hcd
*xhci
,
3411 struct xhci_virt_device
*virt_dev
, bool drop_control_ep
)
3414 unsigned int num_dropped_eps
= 0;
3415 unsigned int drop_flags
= 0;
3417 for (i
= (drop_control_ep
? 0 : 1); i
< 31; i
++) {
3418 if (virt_dev
->eps
[i
].ring
) {
3419 drop_flags
|= 1 << i
;
3423 xhci
->num_active_eps
-= num_dropped_eps
;
3424 if (num_dropped_eps
)
3425 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
3426 "Dropped %u ep ctxs, flags = 0x%x, "
3428 num_dropped_eps
, drop_flags
,
3429 xhci
->num_active_eps
);
3433 * This submits a Reset Device Command, which will set the device state to 0,
3434 * set the device address to 0, and disable all the endpoints except the default
3435 * control endpoint. The USB core should come back and call
3436 * xhci_address_device(), and then re-set up the configuration. If this is
3437 * called because of a usb_reset_and_verify_device(), then the old alternate
3438 * settings will be re-installed through the normal bandwidth allocation
3441 * Wait for the Reset Device command to finish. Remove all structures
3442 * associated with the endpoints that were disabled. Clear the input device
3443 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
3445 * If the virt_dev to be reset does not exist or does not match the udev,
3446 * it means the device is lost, possibly due to the xHC restore error and
3447 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3448 * re-allocate the device.
3450 int xhci_discover_or_reset_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
3453 unsigned long flags
;
3454 struct xhci_hcd
*xhci
;
3455 unsigned int slot_id
;
3456 struct xhci_virt_device
*virt_dev
;
3457 struct xhci_command
*reset_device_cmd
;
3458 int last_freed_endpoint
;
3459 struct xhci_slot_ctx
*slot_ctx
;
3460 int old_active_eps
= 0;
3462 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, false, __func__
);
3465 xhci
= hcd_to_xhci(hcd
);
3466 slot_id
= udev
->slot_id
;
3467 virt_dev
= xhci
->devs
[slot_id
];
3469 xhci_dbg(xhci
, "The device to be reset with slot ID %u does "
3470 "not exist. Re-allocate the device\n", slot_id
);
3471 ret
= xhci_alloc_dev(hcd
, udev
);
3478 if (virt_dev
->tt_info
)
3479 old_active_eps
= virt_dev
->tt_info
->active_eps
;
3481 if (virt_dev
->udev
!= udev
) {
3482 /* If the virt_dev and the udev does not match, this virt_dev
3483 * may belong to another udev.
3484 * Re-allocate the device.
3486 xhci_dbg(xhci
, "The device to be reset with slot ID %u does "
3487 "not match the udev. Re-allocate the device\n",
3489 ret
= xhci_alloc_dev(hcd
, udev
);
3496 /* If device is not setup, there is no point in resetting it */
3497 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->out_ctx
);
3498 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx
->dev_state
)) ==
3499 SLOT_STATE_DISABLED
)
3502 xhci_dbg(xhci
, "Resetting device with slot ID %u\n", slot_id
);
3503 /* Allocate the command structure that holds the struct completion.
3504 * Assume we're in process context, since the normal device reset
3505 * process has to wait for the device anyway. Storage devices are
3506 * reset as part of error handling, so use GFP_NOIO instead of
3509 reset_device_cmd
= xhci_alloc_command(xhci
, false, true, GFP_NOIO
);
3510 if (!reset_device_cmd
) {
3511 xhci_dbg(xhci
, "Couldn't allocate command structure.\n");
3515 /* Attempt to submit the Reset Device command to the command ring */
3516 spin_lock_irqsave(&xhci
->lock
, flags
);
3518 ret
= xhci_queue_reset_device(xhci
, reset_device_cmd
, slot_id
);
3520 xhci_dbg(xhci
, "FIXME: allocate a command ring segment\n");
3521 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3522 goto command_cleanup
;
3524 xhci_ring_cmd_db(xhci
);
3525 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3527 /* Wait for the Reset Device command to finish */
3528 wait_for_completion(reset_device_cmd
->completion
);
3530 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3531 * unless we tried to reset a slot ID that wasn't enabled,
3532 * or the device wasn't in the addressed or configured state.
3534 ret
= reset_device_cmd
->status
;
3536 case COMP_CMD_ABORT
:
3538 xhci_warn(xhci
, "Timeout waiting for reset device command\n");
3540 goto command_cleanup
;
3541 case COMP_EBADSLT
: /* 0.95 completion code for bad slot ID */
3542 case COMP_CTX_STATE
: /* 0.96 completion code for same thing */
3543 xhci_dbg(xhci
, "Can't reset device (slot ID %u) in %s state\n",
3545 xhci_get_slot_state(xhci
, virt_dev
->out_ctx
));
3546 xhci_dbg(xhci
, "Not freeing device rings.\n");
3547 /* Don't treat this as an error. May change my mind later. */
3549 goto command_cleanup
;
3551 xhci_dbg(xhci
, "Successful reset device command.\n");
3554 if (xhci_is_vendor_info_code(xhci
, ret
))
3556 xhci_warn(xhci
, "Unknown completion code %u for "
3557 "reset device command.\n", ret
);
3559 goto command_cleanup
;
3562 /* Free up host controller endpoint resources */
3563 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
)) {
3564 spin_lock_irqsave(&xhci
->lock
, flags
);
3565 /* Don't delete the default control endpoint resources */
3566 xhci_free_device_endpoint_resources(xhci
, virt_dev
, false);
3567 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3570 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3571 last_freed_endpoint
= 1;
3572 for (i
= 1; i
< 31; ++i
) {
3573 struct xhci_virt_ep
*ep
= &virt_dev
->eps
[i
];
3575 if (ep
->ep_state
& EP_HAS_STREAMS
) {
3576 xhci_warn(xhci
, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n",
3577 xhci_get_endpoint_address(i
));
3578 xhci_free_stream_info(xhci
, ep
->stream_info
);
3579 ep
->stream_info
= NULL
;
3580 ep
->ep_state
&= ~EP_HAS_STREAMS
;
3584 xhci_free_or_cache_endpoint_ring(xhci
, virt_dev
, i
);
3585 last_freed_endpoint
= i
;
3587 if (!list_empty(&virt_dev
->eps
[i
].bw_endpoint_list
))
3588 xhci_drop_ep_from_interval_table(xhci
,
3589 &virt_dev
->eps
[i
].bw_info
,
3594 xhci_clear_endpoint_bw_info(&virt_dev
->eps
[i
].bw_info
);
3596 /* If necessary, update the number of active TTs on this root port */
3597 xhci_update_tt_active_eps(xhci
, virt_dev
, old_active_eps
);
3599 xhci_dbg(xhci
, "Output context after successful reset device cmd:\n");
3600 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, last_freed_endpoint
);
3604 xhci_free_command(xhci
, reset_device_cmd
);
3609 * At this point, the struct usb_device is about to go away, the device has
3610 * disconnected, and all traffic has been stopped and the endpoints have been
3611 * disabled. Free any HC data structures associated with that device.
3613 void xhci_free_dev(struct usb_hcd
*hcd
, struct usb_device
*udev
)
3615 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
3616 struct xhci_virt_device
*virt_dev
;
3617 unsigned long flags
;
3620 struct xhci_command
*command
;
3622 command
= xhci_alloc_command(xhci
, false, false, GFP_KERNEL
);
3626 #ifndef CONFIG_USB_DEFAULT_PERSIST
3628 * We called pm_runtime_get_noresume when the device was attached.
3629 * Decrement the counter here to allow controller to runtime suspend
3630 * if no devices remain.
3632 if (xhci
->quirks
& XHCI_RESET_ON_RESUME
)
3633 pm_runtime_put_noidle(hcd
->self
.controller
);
3636 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, true, __func__
);
3637 /* If the host is halted due to driver unload, we still need to free the
3640 if (ret
<= 0 && ret
!= -ENODEV
) {
3645 virt_dev
= xhci
->devs
[udev
->slot_id
];
3647 /* Stop any wayward timer functions (which may grab the lock) */
3648 for (i
= 0; i
< 31; ++i
) {
3649 virt_dev
->eps
[i
].ep_state
&= ~EP_HALT_PENDING
;
3650 del_timer_sync(&virt_dev
->eps
[i
].stop_cmd_timer
);
3653 spin_lock_irqsave(&xhci
->lock
, flags
);
3654 /* Don't disable the slot if the host controller is dead. */
3655 state
= readl(&xhci
->op_regs
->status
);
3656 if (state
== 0xffffffff || (xhci
->xhc_state
& XHCI_STATE_DYING
) ||
3657 (xhci
->xhc_state
& XHCI_STATE_HALTED
)) {
3658 xhci_free_virt_device(xhci
, udev
->slot_id
);
3659 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3664 if (xhci_queue_slot_control(xhci
, command
, TRB_DISABLE_SLOT
,
3666 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3667 xhci_dbg(xhci
, "FIXME: allocate a command ring segment\n");
3670 xhci_ring_cmd_db(xhci
);
3671 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3674 * Event command completion handler will free any data structures
3675 * associated with the slot. XXX Can free sleep?
3680 * Checks if we have enough host controller resources for the default control
3683 * Must be called with xhci->lock held.
3685 static int xhci_reserve_host_control_ep_resources(struct xhci_hcd
*xhci
)
3687 if (xhci
->num_active_eps
+ 1 > xhci
->limit_active_eps
) {
3688 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
3689 "Not enough ep ctxs: "
3690 "%u active, need to add 1, limit is %u.",
3691 xhci
->num_active_eps
, xhci
->limit_active_eps
);
3694 xhci
->num_active_eps
+= 1;
3695 xhci_dbg_trace(xhci
, trace_xhci_dbg_quirks
,
3696 "Adding 1 ep ctx, %u now active.",
3697 xhci
->num_active_eps
);
3703 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3704 * timed out, or allocating memory failed. Returns 1 on success.
3706 int xhci_alloc_dev(struct usb_hcd
*hcd
, struct usb_device
*udev
)
3708 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
3709 unsigned long flags
;
3711 struct xhci_command
*command
;
3713 command
= xhci_alloc_command(xhci
, false, false, GFP_KERNEL
);
3717 /* xhci->slot_id and xhci->addr_dev are not thread-safe */
3718 mutex_lock(&xhci
->mutex
);
3719 spin_lock_irqsave(&xhci
->lock
, flags
);
3720 command
->completion
= &xhci
->addr_dev
;
3721 ret
= xhci_queue_slot_control(xhci
, command
, TRB_ENABLE_SLOT
, 0);
3723 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3724 mutex_unlock(&xhci
->mutex
);
3725 xhci_dbg(xhci
, "FIXME: allocate a command ring segment\n");
3729 xhci_ring_cmd_db(xhci
);
3730 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3732 wait_for_completion(command
->completion
);
3733 slot_id
= xhci
->slot_id
;
3734 mutex_unlock(&xhci
->mutex
);
3736 if (!slot_id
|| command
->status
!= COMP_SUCCESS
) {
3737 xhci_err(xhci
, "Error while assigning device slot ID\n");
3738 xhci_err(xhci
, "Max number of devices this xHCI host supports is %u.\n",
3740 readl(&xhci
->cap_regs
->hcs_params1
)));
3745 if ((xhci
->quirks
& XHCI_EP_LIMIT_QUIRK
)) {
3746 spin_lock_irqsave(&xhci
->lock
, flags
);
3747 ret
= xhci_reserve_host_control_ep_resources(xhci
);
3749 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3750 xhci_warn(xhci
, "Not enough host resources, "
3751 "active endpoint contexts = %u\n",
3752 xhci
->num_active_eps
);
3755 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3757 /* Use GFP_NOIO, since this function can be called from
3758 * xhci_discover_or_reset_device(), which may be called as part of
3759 * mass storage driver error handling.
3761 if (!xhci_alloc_virt_device(xhci
, slot_id
, udev
, GFP_NOIO
)) {
3762 xhci_warn(xhci
, "Could not allocate xHCI USB device data structures\n");
3765 udev
->slot_id
= slot_id
;
3767 #ifndef CONFIG_USB_DEFAULT_PERSIST
3769 * If resetting upon resume, we can't put the controller into runtime
3770 * suspend if there is a device attached.
3772 if (xhci
->quirks
& XHCI_RESET_ON_RESUME
)
3773 pm_runtime_get_noresume(hcd
->self
.controller
);
3778 /* Is this a LS or FS device under a HS hub? */
3779 /* Hub or peripherial? */
3783 /* Disable slot, if we can do it without mem alloc */
3784 spin_lock_irqsave(&xhci
->lock
, flags
);
3785 command
->completion
= NULL
;
3786 command
->status
= 0;
3787 if (!xhci_queue_slot_control(xhci
, command
, TRB_DISABLE_SLOT
,
3789 xhci_ring_cmd_db(xhci
);
3790 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3795 * Issue an Address Device command and optionally send a corresponding
3796 * SetAddress request to the device.
3798 static int xhci_setup_device(struct usb_hcd
*hcd
, struct usb_device
*udev
,
3799 enum xhci_setup_dev setup
)
3801 const char *act
= setup
== SETUP_CONTEXT_ONLY
? "context" : "address";
3802 unsigned long flags
;
3803 struct xhci_virt_device
*virt_dev
;
3805 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
3806 struct xhci_slot_ctx
*slot_ctx
;
3807 struct xhci_input_control_ctx
*ctrl_ctx
;
3809 struct xhci_command
*command
= NULL
;
3811 mutex_lock(&xhci
->mutex
);
3813 if (xhci
->xhc_state
) { /* dying, removing or halted */
3818 if (!udev
->slot_id
) {
3819 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3820 "Bad Slot ID %d", udev
->slot_id
);
3825 virt_dev
= xhci
->devs
[udev
->slot_id
];
3827 if (WARN_ON(!virt_dev
)) {
3829 * In plug/unplug torture test with an NEC controller,
3830 * a zero-dereference was observed once due to virt_dev = 0.
3831 * Print useful debug rather than crash if it is observed again!
3833 xhci_warn(xhci
, "Virt dev invalid for slot_id 0x%x!\n",
3839 if (setup
== SETUP_CONTEXT_ONLY
) {
3840 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->out_ctx
);
3841 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx
->dev_state
)) ==
3842 SLOT_STATE_DEFAULT
) {
3843 xhci_dbg(xhci
, "Slot already in default state\n");
3848 command
= xhci_alloc_command(xhci
, false, false, GFP_KERNEL
);
3854 command
->in_ctx
= virt_dev
->in_ctx
;
3855 command
->completion
= &xhci
->addr_dev
;
3857 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->in_ctx
);
3858 ctrl_ctx
= xhci_get_input_control_ctx(virt_dev
->in_ctx
);
3860 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
3866 * If this is the first Set Address since device plug-in or
3867 * virt_device realloaction after a resume with an xHCI power loss,
3868 * then set up the slot context.
3870 if (!slot_ctx
->dev_info
)
3871 xhci_setup_addressable_virt_dev(xhci
, udev
);
3872 /* Otherwise, update the control endpoint ring enqueue pointer. */
3874 xhci_copy_ep0_dequeue_into_input_ctx(xhci
, udev
);
3875 ctrl_ctx
->add_flags
= cpu_to_le32(SLOT_FLAG
| EP0_FLAG
);
3876 ctrl_ctx
->drop_flags
= 0;
3878 xhci_dbg(xhci
, "Slot ID %d Input Context:\n", udev
->slot_id
);
3879 xhci_dbg_ctx(xhci
, virt_dev
->in_ctx
, 2);
3880 trace_xhci_address_ctx(xhci
, virt_dev
->in_ctx
,
3881 le32_to_cpu(slot_ctx
->dev_info
) >> 27);
3883 spin_lock_irqsave(&xhci
->lock
, flags
);
3884 ret
= xhci_queue_address_device(xhci
, command
, virt_dev
->in_ctx
->dma
,
3885 udev
->slot_id
, setup
);
3887 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3888 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3889 "FIXME: allocate a command ring segment");
3892 xhci_ring_cmd_db(xhci
);
3893 spin_unlock_irqrestore(&xhci
->lock
, flags
);
3895 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
3896 wait_for_completion(command
->completion
);
3898 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3899 * the SetAddress() "recovery interval" required by USB and aborting the
3900 * command on a timeout.
3902 switch (command
->status
) {
3903 case COMP_CMD_ABORT
:
3905 xhci_warn(xhci
, "Timeout while waiting for setup device command\n");
3908 case COMP_CTX_STATE
:
3910 xhci_err(xhci
, "Setup ERROR: setup %s command for slot %d.\n",
3911 act
, udev
->slot_id
);
3915 dev_warn(&udev
->dev
, "Device not responding to setup %s.\n", act
);
3919 dev_warn(&udev
->dev
,
3920 "ERROR: Incompatible device for setup %s command\n", act
);
3924 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3925 "Successful setup %s command", act
);
3929 "ERROR: unexpected setup %s command completion code 0x%x.\n",
3930 act
, command
->status
);
3931 xhci_dbg(xhci
, "Slot ID %d Output Context:\n", udev
->slot_id
);
3932 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 2);
3933 trace_xhci_address_ctx(xhci
, virt_dev
->out_ctx
, 1);
3939 temp_64
= xhci_read_64(xhci
, &xhci
->op_regs
->dcbaa_ptr
);
3940 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3941 "Op regs DCBAA ptr = %#016llx", temp_64
);
3942 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3943 "Slot ID %d dcbaa entry @%p = %#016llx",
3945 &xhci
->dcbaa
->dev_context_ptrs
[udev
->slot_id
],
3946 (unsigned long long)
3947 le64_to_cpu(xhci
->dcbaa
->dev_context_ptrs
[udev
->slot_id
]));
3948 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3949 "Output Context DMA address = %#08llx",
3950 (unsigned long long)virt_dev
->out_ctx
->dma
);
3951 xhci_dbg(xhci
, "Slot ID %d Input Context:\n", udev
->slot_id
);
3952 xhci_dbg_ctx(xhci
, virt_dev
->in_ctx
, 2);
3953 trace_xhci_address_ctx(xhci
, virt_dev
->in_ctx
,
3954 le32_to_cpu(slot_ctx
->dev_info
) >> 27);
3955 xhci_dbg(xhci
, "Slot ID %d Output Context:\n", udev
->slot_id
);
3956 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 2);
3958 * USB core uses address 1 for the roothubs, so we add one to the
3959 * address given back to us by the HC.
3961 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->out_ctx
);
3962 trace_xhci_address_ctx(xhci
, virt_dev
->out_ctx
,
3963 le32_to_cpu(slot_ctx
->dev_info
) >> 27);
3964 /* Zero the input context control for later use */
3965 ctrl_ctx
->add_flags
= 0;
3966 ctrl_ctx
->drop_flags
= 0;
3968 xhci_dbg_trace(xhci
, trace_xhci_dbg_address
,
3969 "Internal device address = %d",
3970 le32_to_cpu(slot_ctx
->dev_state
) & DEV_ADDR_MASK
);
3972 mutex_unlock(&xhci
->mutex
);
3977 int xhci_address_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
3979 return xhci_setup_device(hcd
, udev
, SETUP_CONTEXT_ADDRESS
);
3982 int xhci_enable_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
3984 return xhci_setup_device(hcd
, udev
, SETUP_CONTEXT_ONLY
);
3988 * Transfer the port index into real index in the HW port status
3989 * registers. Caculate offset between the port's PORTSC register
3990 * and port status base. Divide the number of per port register
3991 * to get the real index. The raw port number bases 1.
3993 int xhci_find_raw_port_number(struct usb_hcd
*hcd
, int port1
)
3995 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
3996 __le32 __iomem
*base_addr
= &xhci
->op_regs
->port_status_base
;
3997 __le32 __iomem
*addr
;
4000 if (hcd
->speed
< HCD_USB3
)
4001 addr
= xhci
->usb2_ports
[port1
- 1];
4003 addr
= xhci
->usb3_ports
[port1
- 1];
4005 raw_port
= (addr
- base_addr
)/NUM_PORT_REGS
+ 1;
4010 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
4011 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
4013 static int __maybe_unused
xhci_change_max_exit_latency(struct xhci_hcd
*xhci
,
4014 struct usb_device
*udev
, u16 max_exit_latency
)
4016 struct xhci_virt_device
*virt_dev
;
4017 struct xhci_command
*command
;
4018 struct xhci_input_control_ctx
*ctrl_ctx
;
4019 struct xhci_slot_ctx
*slot_ctx
;
4020 unsigned long flags
;
4023 spin_lock_irqsave(&xhci
->lock
, flags
);
4025 virt_dev
= xhci
->devs
[udev
->slot_id
];
4028 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
4029 * xHC was re-initialized. Exit latency will be set later after
4030 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
4033 if (!virt_dev
|| max_exit_latency
== virt_dev
->current_mel
) {
4034 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4038 /* Attempt to issue an Evaluate Context command to change the MEL. */
4039 command
= xhci
->lpm_command
;
4040 ctrl_ctx
= xhci_get_input_control_ctx(command
->in_ctx
);
4042 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4043 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
4048 xhci_slot_copy(xhci
, command
->in_ctx
, virt_dev
->out_ctx
);
4049 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4051 ctrl_ctx
->add_flags
|= cpu_to_le32(SLOT_FLAG
);
4052 slot_ctx
= xhci_get_slot_ctx(xhci
, command
->in_ctx
);
4053 slot_ctx
->dev_info2
&= cpu_to_le32(~((u32
) MAX_EXIT
));
4054 slot_ctx
->dev_info2
|= cpu_to_le32(max_exit_latency
);
4055 slot_ctx
->dev_state
= 0;
4057 xhci_dbg_trace(xhci
, trace_xhci_dbg_context_change
,
4058 "Set up evaluate context for LPM MEL change.");
4059 xhci_dbg(xhci
, "Slot %u Input Context:\n", udev
->slot_id
);
4060 xhci_dbg_ctx(xhci
, command
->in_ctx
, 0);
4062 /* Issue and wait for the evaluate context command. */
4063 ret
= xhci_configure_endpoint(xhci
, udev
, command
,
4065 xhci_dbg(xhci
, "Slot %u Output Context:\n", udev
->slot_id
);
4066 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 0);
4069 spin_lock_irqsave(&xhci
->lock
, flags
);
4070 virt_dev
->current_mel
= max_exit_latency
;
4071 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4078 /* BESL to HIRD Encoding array for USB2 LPM */
4079 static int xhci_besl_encoding
[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
4080 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
4082 /* Calculate HIRD/BESL for USB2 PORTPMSC*/
4083 static int xhci_calculate_hird_besl(struct xhci_hcd
*xhci
,
4084 struct usb_device
*udev
)
4086 int u2del
, besl
, besl_host
;
4087 int besl_device
= 0;
4090 u2del
= HCS_U2_LATENCY(xhci
->hcs_params3
);
4091 field
= le32_to_cpu(udev
->bos
->ext_cap
->bmAttributes
);
4093 if (field
& USB_BESL_SUPPORT
) {
4094 for (besl_host
= 0; besl_host
< 16; besl_host
++) {
4095 if (xhci_besl_encoding
[besl_host
] >= u2del
)
4098 /* Use baseline BESL value as default */
4099 if (field
& USB_BESL_BASELINE_VALID
)
4100 besl_device
= USB_GET_BESL_BASELINE(field
);
4101 else if (field
& USB_BESL_DEEP_VALID
)
4102 besl_device
= USB_GET_BESL_DEEP(field
);
4107 besl_host
= (u2del
- 51) / 75 + 1;
4110 besl
= besl_host
+ besl_device
;
4117 /* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4118 static int xhci_calculate_usb2_hw_lpm_params(struct usb_device
*udev
)
4125 field
= le32_to_cpu(udev
->bos
->ext_cap
->bmAttributes
);
4127 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
4128 l1
= udev
->l1_params
.timeout
/ 256;
4130 /* device has preferred BESLD */
4131 if (field
& USB_BESL_DEEP_VALID
) {
4132 besld
= USB_GET_BESL_DEEP(field
);
4136 return PORT_BESLD(besld
) | PORT_L1_TIMEOUT(l1
) | PORT_HIRDM(hirdm
);
4139 int xhci_set_usb2_hardware_lpm(struct usb_hcd
*hcd
,
4140 struct usb_device
*udev
, int enable
)
4142 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
4143 __le32 __iomem
**port_array
;
4144 __le32 __iomem
*pm_addr
, *hlpm_addr
;
4145 u32 pm_val
, hlpm_val
, field
;
4146 unsigned int port_num
;
4147 unsigned long flags
;
4148 int hird
, exit_latency
;
4151 if (hcd
->speed
>= HCD_USB3
|| !xhci
->hw_lpm_support
||
4155 if (!udev
->parent
|| udev
->parent
->parent
||
4156 udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
4159 if (udev
->usb2_hw_lpm_capable
!= 1)
4162 spin_lock_irqsave(&xhci
->lock
, flags
);
4164 port_array
= xhci
->usb2_ports
;
4165 port_num
= udev
->portnum
- 1;
4166 pm_addr
= port_array
[port_num
] + PORTPMSC
;
4167 pm_val
= readl(pm_addr
);
4168 hlpm_addr
= port_array
[port_num
] + PORTHLPMC
;
4169 field
= le32_to_cpu(udev
->bos
->ext_cap
->bmAttributes
);
4171 xhci_dbg(xhci
, "%s port %d USB2 hardware LPM\n",
4172 enable
? "enable" : "disable", port_num
+ 1);
4175 /* Host supports BESL timeout instead of HIRD */
4176 if (udev
->usb2_hw_lpm_besl_capable
) {
4177 /* if device doesn't have a preferred BESL value use a
4178 * default one which works with mixed HIRD and BESL
4179 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4181 if ((field
& USB_BESL_SUPPORT
) &&
4182 (field
& USB_BESL_BASELINE_VALID
))
4183 hird
= USB_GET_BESL_BASELINE(field
);
4185 hird
= udev
->l1_params
.besl
;
4187 exit_latency
= xhci_besl_encoding
[hird
];
4188 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4190 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4191 * input context for link powermanagement evaluate
4192 * context commands. It is protected by hcd->bandwidth
4193 * mutex and is shared by all devices. We need to set
4194 * the max ext latency in USB 2 BESL LPM as well, so
4195 * use the same mutex and xhci_change_max_exit_latency()
4197 mutex_lock(hcd
->bandwidth_mutex
);
4198 ret
= xhci_change_max_exit_latency(xhci
, udev
,
4200 mutex_unlock(hcd
->bandwidth_mutex
);
4204 spin_lock_irqsave(&xhci
->lock
, flags
);
4206 hlpm_val
= xhci_calculate_usb2_hw_lpm_params(udev
);
4207 writel(hlpm_val
, hlpm_addr
);
4211 hird
= xhci_calculate_hird_besl(xhci
, udev
);
4214 pm_val
&= ~PORT_HIRD_MASK
;
4215 pm_val
|= PORT_HIRD(hird
) | PORT_RWE
| PORT_L1DS(udev
->slot_id
);
4216 writel(pm_val
, pm_addr
);
4217 pm_val
= readl(pm_addr
);
4219 writel(pm_val
, pm_addr
);
4223 pm_val
&= ~(PORT_HLE
| PORT_RWE
| PORT_HIRD_MASK
| PORT_L1DS_MASK
);
4224 writel(pm_val
, pm_addr
);
4227 if (udev
->usb2_hw_lpm_besl_capable
) {
4228 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4229 mutex_lock(hcd
->bandwidth_mutex
);
4230 xhci_change_max_exit_latency(xhci
, udev
, 0);
4231 mutex_unlock(hcd
->bandwidth_mutex
);
4236 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4240 /* check if a usb2 port supports a given extened capability protocol
4241 * only USB2 ports extended protocol capability values are cached.
4242 * Return 1 if capability is supported
4244 static int xhci_check_usb2_port_capability(struct xhci_hcd
*xhci
, int port
,
4245 unsigned capability
)
4247 u32 port_offset
, port_count
;
4250 for (i
= 0; i
< xhci
->num_ext_caps
; i
++) {
4251 if (xhci
->ext_caps
[i
] & capability
) {
4252 /* port offsets starts at 1 */
4253 port_offset
= XHCI_EXT_PORT_OFF(xhci
->ext_caps
[i
]) - 1;
4254 port_count
= XHCI_EXT_PORT_COUNT(xhci
->ext_caps
[i
]);
4255 if (port
>= port_offset
&&
4256 port
< port_offset
+ port_count
)
4263 int xhci_update_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
4265 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
4266 int portnum
= udev
->portnum
- 1;
4268 if (hcd
->speed
>= HCD_USB3
|| !xhci
->sw_lpm_support
||
4272 /* we only support lpm for non-hub device connected to root hub yet */
4273 if (!udev
->parent
|| udev
->parent
->parent
||
4274 udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
)
4277 if (xhci
->hw_lpm_support
== 1 &&
4278 xhci_check_usb2_port_capability(
4279 xhci
, portnum
, XHCI_HLC
)) {
4280 udev
->usb2_hw_lpm_capable
= 1;
4281 udev
->l1_params
.timeout
= XHCI_L1_TIMEOUT
;
4282 udev
->l1_params
.besl
= XHCI_DEFAULT_BESL
;
4283 if (xhci_check_usb2_port_capability(xhci
, portnum
,
4285 udev
->usb2_hw_lpm_besl_capable
= 1;
4291 /*---------------------- USB 3.0 Link PM functions ------------------------*/
4293 /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4294 static unsigned long long xhci_service_interval_to_ns(
4295 struct usb_endpoint_descriptor
*desc
)
4297 return (1ULL << (desc
->bInterval
- 1)) * 125 * 1000;
4300 static u16
xhci_get_timeout_no_hub_lpm(struct usb_device
*udev
,
4301 enum usb3_link_state state
)
4303 unsigned long long sel
;
4304 unsigned long long pel
;
4305 unsigned int max_sel_pel
;
4310 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4311 sel
= DIV_ROUND_UP(udev
->u1_params
.sel
, 1000);
4312 pel
= DIV_ROUND_UP(udev
->u1_params
.pel
, 1000);
4313 max_sel_pel
= USB3_LPM_MAX_U1_SEL_PEL
;
4317 sel
= DIV_ROUND_UP(udev
->u2_params
.sel
, 1000);
4318 pel
= DIV_ROUND_UP(udev
->u2_params
.pel
, 1000);
4319 max_sel_pel
= USB3_LPM_MAX_U2_SEL_PEL
;
4323 dev_warn(&udev
->dev
, "%s: Can't get timeout for non-U1 or U2 state.\n",
4325 return USB3_LPM_DISABLED
;
4328 if (sel
<= max_sel_pel
&& pel
<= max_sel_pel
)
4329 return USB3_LPM_DEVICE_INITIATED
;
4331 if (sel
> max_sel_pel
)
4332 dev_dbg(&udev
->dev
, "Device-initiated %s disabled "
4333 "due to long SEL %llu ms\n",
4336 dev_dbg(&udev
->dev
, "Device-initiated %s disabled "
4337 "due to long PEL %llu ms\n",
4339 return USB3_LPM_DISABLED
;
4342 /* The U1 timeout should be the maximum of the following values:
4343 * - For control endpoints, U1 system exit latency (SEL) * 3
4344 * - For bulk endpoints, U1 SEL * 5
4345 * - For interrupt endpoints:
4346 * - Notification EPs, U1 SEL * 3
4347 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4348 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4350 static unsigned long long xhci_calculate_intel_u1_timeout(
4351 struct usb_device
*udev
,
4352 struct usb_endpoint_descriptor
*desc
)
4354 unsigned long long timeout_ns
;
4358 ep_type
= usb_endpoint_type(desc
);
4360 case USB_ENDPOINT_XFER_CONTROL
:
4361 timeout_ns
= udev
->u1_params
.sel
* 3;
4363 case USB_ENDPOINT_XFER_BULK
:
4364 timeout_ns
= udev
->u1_params
.sel
* 5;
4366 case USB_ENDPOINT_XFER_INT
:
4367 intr_type
= usb_endpoint_interrupt_type(desc
);
4368 if (intr_type
== USB_ENDPOINT_INTR_NOTIFICATION
) {
4369 timeout_ns
= udev
->u1_params
.sel
* 3;
4372 /* Otherwise the calculation is the same as isoc eps */
4373 case USB_ENDPOINT_XFER_ISOC
:
4374 timeout_ns
= xhci_service_interval_to_ns(desc
);
4375 timeout_ns
= DIV_ROUND_UP_ULL(timeout_ns
* 105, 100);
4376 if (timeout_ns
< udev
->u1_params
.sel
* 2)
4377 timeout_ns
= udev
->u1_params
.sel
* 2;
4386 /* Returns the hub-encoded U1 timeout value. */
4387 static u16
xhci_calculate_u1_timeout(struct xhci_hcd
*xhci
,
4388 struct usb_device
*udev
,
4389 struct usb_endpoint_descriptor
*desc
)
4391 unsigned long long timeout_ns
;
4393 if (xhci
->quirks
& XHCI_INTEL_HOST
)
4394 timeout_ns
= xhci_calculate_intel_u1_timeout(udev
, desc
);
4396 timeout_ns
= udev
->u1_params
.sel
;
4398 /* The U1 timeout is encoded in 1us intervals.
4399 * Don't return a timeout of zero, because that's USB3_LPM_DISABLED.
4401 if (timeout_ns
== USB3_LPM_DISABLED
)
4404 timeout_ns
= DIV_ROUND_UP_ULL(timeout_ns
, 1000);
4406 /* If the necessary timeout value is bigger than what we can set in the
4407 * USB 3.0 hub, we have to disable hub-initiated U1.
4409 if (timeout_ns
<= USB3_LPM_U1_MAX_TIMEOUT
)
4411 dev_dbg(&udev
->dev
, "Hub-initiated U1 disabled "
4412 "due to long timeout %llu ms\n", timeout_ns
);
4413 return xhci_get_timeout_no_hub_lpm(udev
, USB3_LPM_U1
);
4416 /* The U2 timeout should be the maximum of:
4417 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4418 * - largest bInterval of any active periodic endpoint (to avoid going
4419 * into lower power link states between intervals).
4420 * - the U2 Exit Latency of the device
4422 static unsigned long long xhci_calculate_intel_u2_timeout(
4423 struct usb_device
*udev
,
4424 struct usb_endpoint_descriptor
*desc
)
4426 unsigned long long timeout_ns
;
4427 unsigned long long u2_del_ns
;
4429 timeout_ns
= 10 * 1000 * 1000;
4431 if ((usb_endpoint_xfer_int(desc
) || usb_endpoint_xfer_isoc(desc
)) &&
4432 (xhci_service_interval_to_ns(desc
) > timeout_ns
))
4433 timeout_ns
= xhci_service_interval_to_ns(desc
);
4435 u2_del_ns
= le16_to_cpu(udev
->bos
->ss_cap
->bU2DevExitLat
) * 1000ULL;
4436 if (u2_del_ns
> timeout_ns
)
4437 timeout_ns
= u2_del_ns
;
4442 /* Returns the hub-encoded U2 timeout value. */
4443 static u16
xhci_calculate_u2_timeout(struct xhci_hcd
*xhci
,
4444 struct usb_device
*udev
,
4445 struct usb_endpoint_descriptor
*desc
)
4447 unsigned long long timeout_ns
;
4449 if (xhci
->quirks
& XHCI_INTEL_HOST
)
4450 timeout_ns
= xhci_calculate_intel_u2_timeout(udev
, desc
);
4452 timeout_ns
= udev
->u2_params
.sel
;
4454 /* The U2 timeout is encoded in 256us intervals */
4455 timeout_ns
= DIV_ROUND_UP_ULL(timeout_ns
, 256 * 1000);
4456 /* If the necessary timeout value is bigger than what we can set in the
4457 * USB 3.0 hub, we have to disable hub-initiated U2.
4459 if (timeout_ns
<= USB3_LPM_U2_MAX_TIMEOUT
)
4461 dev_dbg(&udev
->dev
, "Hub-initiated U2 disabled "
4462 "due to long timeout %llu ms\n", timeout_ns
);
4463 return xhci_get_timeout_no_hub_lpm(udev
, USB3_LPM_U2
);
4466 static u16
xhci_call_host_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
,
4472 if (state
== USB3_LPM_U1
)
4473 return xhci_calculate_u1_timeout(xhci
, udev
, desc
);
4474 else if (state
== USB3_LPM_U2
)
4475 return xhci_calculate_u2_timeout(xhci
, udev
, desc
);
4477 return USB3_LPM_DISABLED
;
4480 static int xhci_update_timeout_for_endpoint(struct xhci_hcd
*xhci
,
4481 struct usb_device
*udev
,
4482 struct usb_endpoint_descriptor
*desc
,
4483 enum usb3_link_state state
,
4488 alt_timeout
= xhci_call_host_update_timeout_for_endpoint(xhci
, udev
,
4489 desc
, state
, timeout
);
4491 /* If we found we can't enable hub-initiated LPM, or
4492 * the U1 or U2 exit latency was too high to allow
4493 * device-initiated LPM as well, just stop searching.
4495 if (alt_timeout
== USB3_LPM_DISABLED
||
4496 alt_timeout
== USB3_LPM_DEVICE_INITIATED
) {
4497 *timeout
= alt_timeout
;
4500 if (alt_timeout
> *timeout
)
4501 *timeout
= alt_timeout
;
4505 static int xhci_update_timeout_for_interface(struct xhci_hcd
*xhci
,
4506 struct usb_device
*udev
,
4507 struct usb_host_interface
*alt
,
4508 enum usb3_link_state state
,
4513 for (j
= 0; j
< alt
->desc
.bNumEndpoints
; j
++) {
4514 if (xhci_update_timeout_for_endpoint(xhci
, udev
,
4515 &alt
->endpoint
[j
].desc
, state
, timeout
))
4522 static int xhci_check_intel_tier_policy(struct usb_device
*udev
,
4523 enum usb3_link_state state
)
4525 struct usb_device
*parent
;
4526 unsigned int num_hubs
;
4528 if (state
== USB3_LPM_U2
)
4531 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4532 for (parent
= udev
->parent
, num_hubs
= 0; parent
->parent
;
4533 parent
= parent
->parent
)
4539 dev_dbg(&udev
->dev
, "Disabling U1 link state for device"
4540 " below second-tier hub.\n");
4541 dev_dbg(&udev
->dev
, "Plug device into first-tier hub "
4542 "to decrease power consumption.\n");
4546 static int xhci_check_tier_policy(struct xhci_hcd
*xhci
,
4547 struct usb_device
*udev
,
4548 enum usb3_link_state state
)
4550 if (xhci
->quirks
& XHCI_INTEL_HOST
)
4551 return xhci_check_intel_tier_policy(udev
, state
);
4556 /* Returns the U1 or U2 timeout that should be enabled.
4557 * If the tier check or timeout setting functions return with a non-zero exit
4558 * code, that means the timeout value has been finalized and we shouldn't look
4559 * at any more endpoints.
4561 static u16
xhci_calculate_lpm_timeout(struct usb_hcd
*hcd
,
4562 struct usb_device
*udev
, enum usb3_link_state state
)
4564 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
4565 struct usb_host_config
*config
;
4568 u16 timeout
= USB3_LPM_DISABLED
;
4570 if (state
== USB3_LPM_U1
)
4572 else if (state
== USB3_LPM_U2
)
4575 dev_warn(&udev
->dev
, "Can't enable unknown link state %i\n",
4580 if (xhci_check_tier_policy(xhci
, udev
, state
) < 0)
4583 /* Gather some information about the currently installed configuration
4584 * and alternate interface settings.
4586 if (xhci_update_timeout_for_endpoint(xhci
, udev
, &udev
->ep0
.desc
,
4590 config
= udev
->actconfig
;
4594 for (i
= 0; i
< config
->desc
.bNumInterfaces
; i
++) {
4595 struct usb_driver
*driver
;
4596 struct usb_interface
*intf
= config
->interface
[i
];
4601 /* Check if any currently bound drivers want hub-initiated LPM
4604 if (intf
->dev
.driver
) {
4605 driver
= to_usb_driver(intf
->dev
.driver
);
4606 if (driver
&& driver
->disable_hub_initiated_lpm
) {
4607 dev_dbg(&udev
->dev
, "Hub-initiated %s disabled "
4608 "at request of driver %s\n",
4609 state_name
, driver
->name
);
4610 return xhci_get_timeout_no_hub_lpm(udev
, state
);
4614 /* Not sure how this could happen... */
4615 if (!intf
->cur_altsetting
)
4618 if (xhci_update_timeout_for_interface(xhci
, udev
,
4619 intf
->cur_altsetting
,
4626 static int calculate_max_exit_latency(struct usb_device
*udev
,
4627 enum usb3_link_state state_changed
,
4628 u16 hub_encoded_timeout
)
4630 unsigned long long u1_mel_us
= 0;
4631 unsigned long long u2_mel_us
= 0;
4632 unsigned long long mel_us
= 0;
4638 disabling_u1
= (state_changed
== USB3_LPM_U1
&&
4639 hub_encoded_timeout
== USB3_LPM_DISABLED
);
4640 disabling_u2
= (state_changed
== USB3_LPM_U2
&&
4641 hub_encoded_timeout
== USB3_LPM_DISABLED
);
4643 enabling_u1
= (state_changed
== USB3_LPM_U1
&&
4644 hub_encoded_timeout
!= USB3_LPM_DISABLED
);
4645 enabling_u2
= (state_changed
== USB3_LPM_U2
&&
4646 hub_encoded_timeout
!= USB3_LPM_DISABLED
);
4648 /* If U1 was already enabled and we're not disabling it,
4649 * or we're going to enable U1, account for the U1 max exit latency.
4651 if ((udev
->u1_params
.timeout
!= USB3_LPM_DISABLED
&& !disabling_u1
) ||
4653 u1_mel_us
= DIV_ROUND_UP(udev
->u1_params
.mel
, 1000);
4654 if ((udev
->u2_params
.timeout
!= USB3_LPM_DISABLED
&& !disabling_u2
) ||
4656 u2_mel_us
= DIV_ROUND_UP(udev
->u2_params
.mel
, 1000);
4658 if (u1_mel_us
> u2_mel_us
)
4662 /* xHCI host controller max exit latency field is only 16 bits wide. */
4663 if (mel_us
> MAX_EXIT
) {
4664 dev_warn(&udev
->dev
, "Link PM max exit latency of %lluus "
4665 "is too big.\n", mel_us
);
4671 /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4672 int xhci_enable_usb3_lpm_timeout(struct usb_hcd
*hcd
,
4673 struct usb_device
*udev
, enum usb3_link_state state
)
4675 struct xhci_hcd
*xhci
;
4676 u16 hub_encoded_timeout
;
4680 xhci
= hcd_to_xhci(hcd
);
4681 /* The LPM timeout values are pretty host-controller specific, so don't
4682 * enable hub-initiated timeouts unless the vendor has provided
4683 * information about their timeout algorithm.
4685 if (!xhci
|| !(xhci
->quirks
& XHCI_LPM_SUPPORT
) ||
4686 !xhci
->devs
[udev
->slot_id
])
4687 return USB3_LPM_DISABLED
;
4689 hub_encoded_timeout
= xhci_calculate_lpm_timeout(hcd
, udev
, state
);
4690 mel
= calculate_max_exit_latency(udev
, state
, hub_encoded_timeout
);
4692 /* Max Exit Latency is too big, disable LPM. */
4693 hub_encoded_timeout
= USB3_LPM_DISABLED
;
4697 ret
= xhci_change_max_exit_latency(xhci
, udev
, mel
);
4700 return hub_encoded_timeout
;
4703 int xhci_disable_usb3_lpm_timeout(struct usb_hcd
*hcd
,
4704 struct usb_device
*udev
, enum usb3_link_state state
)
4706 struct xhci_hcd
*xhci
;
4709 xhci
= hcd_to_xhci(hcd
);
4710 if (!xhci
|| !(xhci
->quirks
& XHCI_LPM_SUPPORT
) ||
4711 !xhci
->devs
[udev
->slot_id
])
4714 mel
= calculate_max_exit_latency(udev
, state
, USB3_LPM_DISABLED
);
4715 return xhci_change_max_exit_latency(xhci
, udev
, mel
);
4717 #else /* CONFIG_PM */
4719 int xhci_set_usb2_hardware_lpm(struct usb_hcd
*hcd
,
4720 struct usb_device
*udev
, int enable
)
4725 int xhci_update_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
4730 int xhci_enable_usb3_lpm_timeout(struct usb_hcd
*hcd
,
4731 struct usb_device
*udev
, enum usb3_link_state state
)
4733 return USB3_LPM_DISABLED
;
4736 int xhci_disable_usb3_lpm_timeout(struct usb_hcd
*hcd
,
4737 struct usb_device
*udev
, enum usb3_link_state state
)
4741 #endif /* CONFIG_PM */
4743 /*-------------------------------------------------------------------------*/
4745 /* Once a hub descriptor is fetched for a device, we need to update the xHC's
4746 * internal data structures for the device.
4748 int xhci_update_hub_device(struct usb_hcd
*hcd
, struct usb_device
*hdev
,
4749 struct usb_tt
*tt
, gfp_t mem_flags
)
4751 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
4752 struct xhci_virt_device
*vdev
;
4753 struct xhci_command
*config_cmd
;
4754 struct xhci_input_control_ctx
*ctrl_ctx
;
4755 struct xhci_slot_ctx
*slot_ctx
;
4756 unsigned long flags
;
4757 unsigned think_time
;
4760 /* Ignore root hubs */
4764 vdev
= xhci
->devs
[hdev
->slot_id
];
4766 xhci_warn(xhci
, "Cannot update hub desc for unknown device.\n");
4769 config_cmd
= xhci_alloc_command(xhci
, true, true, mem_flags
);
4771 xhci_dbg(xhci
, "Could not allocate xHCI command structure.\n");
4774 ctrl_ctx
= xhci_get_input_control_ctx(config_cmd
->in_ctx
);
4776 xhci_warn(xhci
, "%s: Could not get input context, bad type.\n",
4778 xhci_free_command(xhci
, config_cmd
);
4782 spin_lock_irqsave(&xhci
->lock
, flags
);
4783 if (hdev
->speed
== USB_SPEED_HIGH
&&
4784 xhci_alloc_tt_info(xhci
, vdev
, hdev
, tt
, GFP_ATOMIC
)) {
4785 xhci_dbg(xhci
, "Could not allocate xHCI TT structure.\n");
4786 xhci_free_command(xhci
, config_cmd
);
4787 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4791 xhci_slot_copy(xhci
, config_cmd
->in_ctx
, vdev
->out_ctx
);
4792 ctrl_ctx
->add_flags
|= cpu_to_le32(SLOT_FLAG
);
4793 slot_ctx
= xhci_get_slot_ctx(xhci
, config_cmd
->in_ctx
);
4794 slot_ctx
->dev_info
|= cpu_to_le32(DEV_HUB
);
4796 * refer to section 6.2.2: MTT should be 0 for full speed hub,
4797 * but it may be already set to 1 when setup an xHCI virtual
4798 * device, so clear it anyway.
4801 slot_ctx
->dev_info
|= cpu_to_le32(DEV_MTT
);
4802 else if (hdev
->speed
== USB_SPEED_FULL
)
4803 slot_ctx
->dev_info
&= cpu_to_le32(~DEV_MTT
);
4805 if (xhci
->hci_version
> 0x95) {
4806 xhci_dbg(xhci
, "xHCI version %x needs hub "
4807 "TT think time and number of ports\n",
4808 (unsigned int) xhci
->hci_version
);
4809 slot_ctx
->dev_info2
|= cpu_to_le32(XHCI_MAX_PORTS(hdev
->maxchild
));
4810 /* Set TT think time - convert from ns to FS bit times.
4811 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4812 * 2 = 24 FS bit times, 3 = 32 FS bit times.
4814 * xHCI 1.0: this field shall be 0 if the device is not a
4817 think_time
= tt
->think_time
;
4818 if (think_time
!= 0)
4819 think_time
= (think_time
/ 666) - 1;
4820 if (xhci
->hci_version
< 0x100 || hdev
->speed
== USB_SPEED_HIGH
)
4821 slot_ctx
->tt_info
|=
4822 cpu_to_le32(TT_THINK_TIME(think_time
));
4824 xhci_dbg(xhci
, "xHCI version %x doesn't need hub "
4825 "TT think time or number of ports\n",
4826 (unsigned int) xhci
->hci_version
);
4828 slot_ctx
->dev_state
= 0;
4829 spin_unlock_irqrestore(&xhci
->lock
, flags
);
4831 xhci_dbg(xhci
, "Set up %s for hub device.\n",
4832 (xhci
->hci_version
> 0x95) ?
4833 "configure endpoint" : "evaluate context");
4834 xhci_dbg(xhci
, "Slot %u Input Context:\n", hdev
->slot_id
);
4835 xhci_dbg_ctx(xhci
, config_cmd
->in_ctx
, 0);
4837 /* Issue and wait for the configure endpoint or
4838 * evaluate context command.
4840 if (xhci
->hci_version
> 0x95)
4841 ret
= xhci_configure_endpoint(xhci
, hdev
, config_cmd
,
4844 ret
= xhci_configure_endpoint(xhci
, hdev
, config_cmd
,
4847 xhci_dbg(xhci
, "Slot %u Output Context:\n", hdev
->slot_id
);
4848 xhci_dbg_ctx(xhci
, vdev
->out_ctx
, 0);
4850 xhci_free_command(xhci
, config_cmd
);
4854 int xhci_get_frame(struct usb_hcd
*hcd
)
4856 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
4857 /* EHCI mods by the periodic size. Why? */
4858 return readl(&xhci
->run_regs
->microframe_index
) >> 3;
4861 int xhci_gen_setup(struct usb_hcd
*hcd
, xhci_get_quirks_t get_quirks
)
4863 struct xhci_hcd
*xhci
;
4864 struct device
*dev
= hcd
->self
.controller
;
4867 /* Accept arbitrarily long scatter-gather lists */
4868 hcd
->self
.sg_tablesize
= ~0;
4870 /* support to build packet from discontinuous buffers */
4871 hcd
->self
.no_sg_constraint
= 1;
4873 /* XHCI controllers don't stop the ep queue on short packets :| */
4874 hcd
->self
.no_stop_on_short
= 1;
4876 xhci
= hcd_to_xhci(hcd
);
4878 if (usb_hcd_is_primary_hcd(hcd
)) {
4879 xhci
->main_hcd
= hcd
;
4880 /* Mark the first roothub as being USB 2.0.
4881 * The xHCI driver will register the USB 3.0 roothub.
4883 hcd
->speed
= HCD_USB2
;
4884 hcd
->self
.root_hub
->speed
= USB_SPEED_HIGH
;
4886 * USB 2.0 roothub under xHCI has an integrated TT,
4887 * (rate matching hub) as opposed to having an OHCI/UHCI
4888 * companion controller.
4892 /* Some 3.1 hosts return sbrn 0x30, can't rely on sbrn alone */
4893 if (xhci
->sbrn
== 0x31 || xhci
->usb3_rhub
.min_rev
>= 1) {
4894 xhci_info(xhci
, "Host supports USB 3.1 Enhanced SuperSpeed\n");
4895 hcd
->speed
= HCD_USB31
;
4896 hcd
->self
.root_hub
->speed
= USB_SPEED_SUPER_PLUS
;
4898 /* xHCI private pointer was set in xhci_pci_probe for the second
4899 * registered roothub.
4904 mutex_init(&xhci
->mutex
);
4905 xhci
->cap_regs
= hcd
->regs
;
4906 xhci
->op_regs
= hcd
->regs
+
4907 HC_LENGTH(readl(&xhci
->cap_regs
->hc_capbase
));
4908 xhci
->run_regs
= hcd
->regs
+
4909 (readl(&xhci
->cap_regs
->run_regs_off
) & RTSOFF_MASK
);
4910 /* Cache read-only capability registers */
4911 xhci
->hcs_params1
= readl(&xhci
->cap_regs
->hcs_params1
);
4912 xhci
->hcs_params2
= readl(&xhci
->cap_regs
->hcs_params2
);
4913 xhci
->hcs_params3
= readl(&xhci
->cap_regs
->hcs_params3
);
4914 xhci
->hcc_params
= readl(&xhci
->cap_regs
->hc_capbase
);
4915 xhci
->hci_version
= HC_VERSION(xhci
->hcc_params
);
4916 xhci
->hcc_params
= readl(&xhci
->cap_regs
->hcc_params
);
4917 if (xhci
->hci_version
> 0x100)
4918 xhci
->hcc_params2
= readl(&xhci
->cap_regs
->hcc_params2
);
4919 xhci_print_registers(xhci
);
4921 xhci
->quirks
|= quirks
;
4923 get_quirks(dev
, xhci
);
4925 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4926 * success event after a short transfer. This quirk will ignore such
4929 if (xhci
->hci_version
> 0x96)
4930 xhci
->quirks
|= XHCI_SPURIOUS_SUCCESS
;
4932 /* Make sure the HC is halted. */
4933 retval
= xhci_halt(xhci
);
4937 xhci_dbg(xhci
, "Resetting HCD\n");
4938 /* Reset the internal HC memory state and registers. */
4939 retval
= xhci_reset(xhci
);
4942 xhci_dbg(xhci
, "Reset complete\n");
4945 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0)
4946 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit
4947 * address memory pointers actually. So, this driver clears the AC64
4948 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev,
4949 * DMA_BIT_MASK(32)) in this xhci_gen_setup().
4951 if (xhci
->quirks
& XHCI_NO_64BIT_SUPPORT
)
4952 xhci
->hcc_params
&= ~BIT(0);
4954 /* Set dma_mask and coherent_dma_mask to 64-bits,
4955 * if xHC supports 64-bit addressing */
4956 if (HCC_64BIT_ADDR(xhci
->hcc_params
) &&
4957 !dma_set_mask(dev
, DMA_BIT_MASK(64))) {
4958 xhci_dbg(xhci
, "Enabling 64-bit DMA addresses.\n");
4959 dma_set_coherent_mask(dev
, DMA_BIT_MASK(64));
4962 * This is to avoid error in cases where a 32-bit USB
4963 * controller is used on a 64-bit capable system.
4965 retval
= dma_set_mask(dev
, DMA_BIT_MASK(32));
4968 xhci_dbg(xhci
, "Enabling 32-bit DMA addresses.\n");
4969 dma_set_coherent_mask(dev
, DMA_BIT_MASK(32));
4972 xhci_dbg(xhci
, "Calling HCD init\n");
4973 /* Initialize HCD and host controller data structures. */
4974 retval
= xhci_init(hcd
);
4977 xhci_dbg(xhci
, "Called HCD init\n");
4979 xhci_info(xhci
, "hcc params 0x%08x hci version 0x%x quirks 0x%08x\n",
4980 xhci
->hcc_params
, xhci
->hci_version
, xhci
->quirks
);
4984 EXPORT_SYMBOL_GPL(xhci_gen_setup
);
4986 static const struct hc_driver xhci_hc_driver
= {
4987 .description
= "xhci-hcd",
4988 .product_desc
= "xHCI Host Controller",
4989 .hcd_priv_size
= sizeof(struct xhci_hcd
),
4992 * generic hardware linkage
4995 .flags
= HCD_MEMORY
| HCD_USB3
| HCD_SHARED
,
4998 * basic lifecycle operations
5000 .reset
= NULL
, /* set in xhci_init_driver() */
5003 .shutdown
= xhci_shutdown
,
5006 * managing i/o requests and associated device resources
5008 .urb_enqueue
= xhci_urb_enqueue
,
5009 .urb_dequeue
= xhci_urb_dequeue
,
5010 .alloc_dev
= xhci_alloc_dev
,
5011 .free_dev
= xhci_free_dev
,
5012 .alloc_streams
= xhci_alloc_streams
,
5013 .free_streams
= xhci_free_streams
,
5014 .add_endpoint
= xhci_add_endpoint
,
5015 .drop_endpoint
= xhci_drop_endpoint
,
5016 .endpoint_reset
= xhci_endpoint_reset
,
5017 .check_bandwidth
= xhci_check_bandwidth
,
5018 .reset_bandwidth
= xhci_reset_bandwidth
,
5019 .address_device
= xhci_address_device
,
5020 .enable_device
= xhci_enable_device
,
5021 .update_hub_device
= xhci_update_hub_device
,
5022 .reset_device
= xhci_discover_or_reset_device
,
5025 * scheduling support
5027 .get_frame_number
= xhci_get_frame
,
5032 .hub_control
= xhci_hub_control
,
5033 .hub_status_data
= xhci_hub_status_data
,
5034 .bus_suspend
= xhci_bus_suspend
,
5035 .bus_resume
= xhci_bus_resume
,
5038 * call back when device connected and addressed
5040 .update_device
= xhci_update_device
,
5041 .set_usb2_hw_lpm
= xhci_set_usb2_hardware_lpm
,
5042 .enable_usb3_lpm_timeout
= xhci_enable_usb3_lpm_timeout
,
5043 .disable_usb3_lpm_timeout
= xhci_disable_usb3_lpm_timeout
,
5044 .find_raw_port_number
= xhci_find_raw_port_number
,
5047 void xhci_init_driver(struct hc_driver
*drv
,
5048 const struct xhci_driver_overrides
*over
)
5052 /* Copy the generic table to drv then apply the overrides */
5053 *drv
= xhci_hc_driver
;
5056 drv
->hcd_priv_size
+= over
->extra_priv_size
;
5058 drv
->reset
= over
->reset
;
5060 drv
->start
= over
->start
;
5063 EXPORT_SYMBOL_GPL(xhci_init_driver
);
5065 MODULE_DESCRIPTION(DRIVER_DESC
);
5066 MODULE_AUTHOR(DRIVER_AUTHOR
);
5067 MODULE_LICENSE("GPL");
5069 static int __init
xhci_hcd_init(void)
5072 * Check the compiler generated sizes of structures that must be laid
5073 * out in specific ways for hardware access.
5075 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array
) != 256*32/8);
5076 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx
) != 8*32/8);
5077 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx
) != 8*32/8);
5078 /* xhci_device_control has eight fields, and also
5079 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
5081 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx
) != 4*32/8);
5082 BUILD_BUG_ON(sizeof(union xhci_trb
) != 4*32/8);
5083 BUILD_BUG_ON(sizeof(struct xhci_erst_entry
) != 4*32/8);
5084 BUILD_BUG_ON(sizeof(struct xhci_cap_regs
) != 8*32/8);
5085 BUILD_BUG_ON(sizeof(struct xhci_intr_reg
) != 8*32/8);
5086 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
5087 BUILD_BUG_ON(sizeof(struct xhci_run_regs
) != (8+8*128)*32/8);
5096 * If an init function is provided, an exit function must also be provided
5097 * to allow module unload.
5099 static void __exit
xhci_hcd_fini(void) { }
5101 module_init(xhci_hcd_init
);
5102 module_exit(xhci_hcd_fini
);