Linux 3.12.39
[linux/fpc-iii.git] / drivers / usb / host / xhci.c
blobe0ccc95c91e2fa6cee98f116f1d2c7021507f949
1 /*
2 * xHCI host controller driver
4 * Copyright (C) 2008 Intel Corp.
6 * Author: Sarah Sharp
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
16 * for more details.
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>
32 #include "xhci.h"
33 #include "xhci-trace.h"
35 #define DRIVER_AUTHOR "Sarah Sharp"
36 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
38 #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
40 /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
41 static int link_quirk;
42 module_param(link_quirk, int, S_IRUGO | S_IWUSR);
43 MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
45 /* TODO: copied from ehci-hcd.c - can this be refactored? */
47 * xhci_handshake - spin reading hc until handshake completes or fails
48 * @ptr: address of hc register to be read
49 * @mask: bits to look at in result of read
50 * @done: value of those bits when handshake succeeds
51 * @usec: timeout in microseconds
53 * Returns negative errno, or zero on success
55 * Success happens when the "mask" bits have the specified value (hardware
56 * handshake done). There are two failure modes: "usec" have passed (major
57 * hardware flakeout), or the register reads as all-ones (hardware removed).
59 int xhci_handshake(struct xhci_hcd *xhci, void __iomem *ptr,
60 u32 mask, u32 done, int usec)
62 u32 result;
64 do {
65 result = xhci_readl(xhci, ptr);
66 if (result == ~(u32)0) /* card removed */
67 return -ENODEV;
68 result &= mask;
69 if (result == done)
70 return 0;
71 udelay(1);
72 usec--;
73 } while (usec > 0);
74 return -ETIMEDOUT;
78 * Disable interrupts and begin the xHCI halting process.
80 void xhci_quiesce(struct xhci_hcd *xhci)
82 u32 halted;
83 u32 cmd;
84 u32 mask;
86 mask = ~(XHCI_IRQS);
87 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
88 if (!halted)
89 mask &= ~CMD_RUN;
91 cmd = xhci_readl(xhci, &xhci->op_regs->command);
92 cmd &= mask;
93 xhci_writel(xhci, cmd, &xhci->op_regs->command);
97 * Force HC into halt state.
99 * Disable any IRQs and clear the run/stop bit.
100 * HC will complete any current and actively pipelined transactions, and
101 * should halt within 16 ms of the run/stop bit being cleared.
102 * Read HC Halted bit in the status register to see when the HC is finished.
104 int xhci_halt(struct xhci_hcd *xhci)
106 int ret;
107 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC");
108 xhci_quiesce(xhci);
110 ret = xhci_handshake(xhci, &xhci->op_regs->status,
111 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
112 if (!ret) {
113 xhci->xhc_state |= XHCI_STATE_HALTED;
114 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
115 } else
116 xhci_warn(xhci, "Host not halted after %u microseconds.\n",
117 XHCI_MAX_HALT_USEC);
118 return ret;
122 * Set the run bit and wait for the host to be running.
124 static int xhci_start(struct xhci_hcd *xhci)
126 u32 temp;
127 int ret;
129 temp = xhci_readl(xhci, &xhci->op_regs->command);
130 temp |= (CMD_RUN);
131 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.",
132 temp);
133 xhci_writel(xhci, temp, &xhci->op_regs->command);
136 * Wait for the HCHalted Status bit to be 0 to indicate the host is
137 * running.
139 ret = xhci_handshake(xhci, &xhci->op_regs->status,
140 STS_HALT, 0, XHCI_MAX_HALT_USEC);
141 if (ret == -ETIMEDOUT)
142 xhci_err(xhci, "Host took too long to start, "
143 "waited %u microseconds.\n",
144 XHCI_MAX_HALT_USEC);
145 if (!ret)
146 xhci->xhc_state &= ~XHCI_STATE_HALTED;
147 return ret;
151 * Reset a halted HC.
153 * This resets pipelines, timers, counters, state machines, etc.
154 * Transactions will be terminated immediately, and operational registers
155 * will be set to their defaults.
157 int xhci_reset(struct xhci_hcd *xhci)
159 u32 command;
160 u32 state;
161 int ret, i;
163 state = xhci_readl(xhci, &xhci->op_regs->status);
164 if ((state & STS_HALT) == 0) {
165 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
166 return 0;
169 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC");
170 command = xhci_readl(xhci, &xhci->op_regs->command);
171 command |= CMD_RESET;
172 xhci_writel(xhci, command, &xhci->op_regs->command);
174 ret = xhci_handshake(xhci, &xhci->op_regs->command,
175 CMD_RESET, 0, 10 * 1000 * 1000);
176 if (ret)
177 return ret;
179 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
180 "Wait for controller to be ready for doorbell rings");
182 * xHCI cannot write to any doorbells or operational registers other
183 * than status until the "Controller Not Ready" flag is cleared.
185 ret = xhci_handshake(xhci, &xhci->op_regs->status,
186 STS_CNR, 0, 10 * 1000 * 1000);
188 for (i = 0; i < 2; ++i) {
189 xhci->bus_state[i].port_c_suspend = 0;
190 xhci->bus_state[i].suspended_ports = 0;
191 xhci->bus_state[i].resuming_ports = 0;
194 return ret;
197 #ifdef CONFIG_PCI
198 static int xhci_free_msi(struct xhci_hcd *xhci)
200 int i;
202 if (!xhci->msix_entries)
203 return -EINVAL;
205 for (i = 0; i < xhci->msix_count; i++)
206 if (xhci->msix_entries[i].vector)
207 free_irq(xhci->msix_entries[i].vector,
208 xhci_to_hcd(xhci));
209 return 0;
213 * Set up MSI
215 static int xhci_setup_msi(struct xhci_hcd *xhci)
217 int ret;
218 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
220 ret = pci_enable_msi(pdev);
221 if (ret) {
222 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
223 "failed to allocate MSI entry");
224 return ret;
227 ret = request_irq(pdev->irq, xhci_msi_irq,
228 0, "xhci_hcd", xhci_to_hcd(xhci));
229 if (ret) {
230 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
231 "disable MSI interrupt");
232 pci_disable_msi(pdev);
235 return ret;
239 * Free IRQs
240 * free all IRQs request
242 static void xhci_free_irq(struct xhci_hcd *xhci)
244 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
245 int ret;
247 /* return if using legacy interrupt */
248 if (xhci_to_hcd(xhci)->irq > 0)
249 return;
251 ret = xhci_free_msi(xhci);
252 if (!ret)
253 return;
254 if (pdev->irq > 0)
255 free_irq(pdev->irq, xhci_to_hcd(xhci));
257 return;
261 * Set up MSI-X
263 static int xhci_setup_msix(struct xhci_hcd *xhci)
265 int i, ret = 0;
266 struct usb_hcd *hcd = xhci_to_hcd(xhci);
267 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
270 * calculate number of msi-x vectors supported.
271 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
272 * with max number of interrupters based on the xhci HCSPARAMS1.
273 * - num_online_cpus: maximum msi-x vectors per CPUs core.
274 * Add additional 1 vector to ensure always available interrupt.
276 xhci->msix_count = min(num_online_cpus() + 1,
277 HCS_MAX_INTRS(xhci->hcs_params1));
279 xhci->msix_entries =
280 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
281 GFP_KERNEL);
282 if (!xhci->msix_entries) {
283 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
284 return -ENOMEM;
287 for (i = 0; i < xhci->msix_count; i++) {
288 xhci->msix_entries[i].entry = i;
289 xhci->msix_entries[i].vector = 0;
292 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
293 if (ret) {
294 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
295 "Failed to enable MSI-X");
296 goto free_entries;
299 for (i = 0; i < xhci->msix_count; i++) {
300 ret = request_irq(xhci->msix_entries[i].vector,
301 xhci_msi_irq,
302 0, "xhci_hcd", xhci_to_hcd(xhci));
303 if (ret)
304 goto disable_msix;
307 hcd->msix_enabled = 1;
308 return ret;
310 disable_msix:
311 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt");
312 xhci_free_irq(xhci);
313 pci_disable_msix(pdev);
314 free_entries:
315 kfree(xhci->msix_entries);
316 xhci->msix_entries = NULL;
317 return ret;
320 /* Free any IRQs and disable MSI-X */
321 static void xhci_cleanup_msix(struct xhci_hcd *xhci)
323 struct usb_hcd *hcd = xhci_to_hcd(xhci);
324 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
326 if (xhci->quirks & XHCI_PLAT)
327 return;
329 xhci_free_irq(xhci);
331 if (xhci->msix_entries) {
332 pci_disable_msix(pdev);
333 kfree(xhci->msix_entries);
334 xhci->msix_entries = NULL;
335 } else {
336 pci_disable_msi(pdev);
339 hcd->msix_enabled = 0;
340 return;
343 static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
345 int i;
347 if (xhci->msix_entries) {
348 for (i = 0; i < xhci->msix_count; i++)
349 synchronize_irq(xhci->msix_entries[i].vector);
353 static int xhci_try_enable_msi(struct usb_hcd *hcd)
355 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
356 struct pci_dev *pdev;
357 int ret;
359 /* The xhci platform device has set up IRQs through usb_add_hcd. */
360 if (xhci->quirks & XHCI_PLAT)
361 return 0;
363 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
365 * Some Fresco Logic host controllers advertise MSI, but fail to
366 * generate interrupts. Don't even try to enable MSI.
368 if (xhci->quirks & XHCI_BROKEN_MSI)
369 goto legacy_irq;
371 /* unregister the legacy interrupt */
372 if (hcd->irq)
373 free_irq(hcd->irq, hcd);
374 hcd->irq = 0;
376 ret = xhci_setup_msix(xhci);
377 if (ret)
378 /* fall back to msi*/
379 ret = xhci_setup_msi(xhci);
381 if (!ret)
382 /* hcd->irq is 0, we have MSI */
383 return 0;
385 if (!pdev->irq) {
386 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
387 return -EINVAL;
390 legacy_irq:
391 /* fall back to legacy interrupt*/
392 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
393 hcd->irq_descr, hcd);
394 if (ret) {
395 xhci_err(xhci, "request interrupt %d failed\n",
396 pdev->irq);
397 return ret;
399 hcd->irq = pdev->irq;
400 return 0;
403 #else
405 static inline int xhci_try_enable_msi(struct usb_hcd *hcd)
407 return 0;
410 static inline void xhci_cleanup_msix(struct xhci_hcd *xhci)
414 static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
418 #endif
420 static void compliance_mode_recovery(unsigned long arg)
422 struct xhci_hcd *xhci;
423 struct usb_hcd *hcd;
424 u32 temp;
425 int i;
427 xhci = (struct xhci_hcd *)arg;
429 for (i = 0; i < xhci->num_usb3_ports; i++) {
430 temp = xhci_readl(xhci, xhci->usb3_ports[i]);
431 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
433 * Compliance Mode Detected. Letting USB Core
434 * handle the Warm Reset
436 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
437 "Compliance mode detected->port %d",
438 i + 1);
439 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
440 "Attempting compliance mode recovery");
441 hcd = xhci->shared_hcd;
443 if (hcd->state == HC_STATE_SUSPENDED)
444 usb_hcd_resume_root_hub(hcd);
446 usb_hcd_poll_rh_status(hcd);
450 if (xhci->port_status_u0 != ((1 << xhci->num_usb3_ports)-1))
451 mod_timer(&xhci->comp_mode_recovery_timer,
452 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
456 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
457 * that causes ports behind that hardware to enter compliance mode sometimes.
458 * The quirk creates a timer that polls every 2 seconds the link state of
459 * each host controller's port and recovers it by issuing a Warm reset
460 * if Compliance mode is detected, otherwise the port will become "dead" (no
461 * device connections or disconnections will be detected anymore). Becasue no
462 * status event is generated when entering compliance mode (per xhci spec),
463 * this quirk is needed on systems that have the failing hardware installed.
465 static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
467 xhci->port_status_u0 = 0;
468 init_timer(&xhci->comp_mode_recovery_timer);
470 xhci->comp_mode_recovery_timer.data = (unsigned long) xhci;
471 xhci->comp_mode_recovery_timer.function = compliance_mode_recovery;
472 xhci->comp_mode_recovery_timer.expires = jiffies +
473 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
475 set_timer_slack(&xhci->comp_mode_recovery_timer,
476 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
477 add_timer(&xhci->comp_mode_recovery_timer);
478 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
479 "Compliance mode recovery timer initialized");
483 * This function identifies the systems that have installed the SN65LVPE502CP
484 * USB3.0 re-driver and that need the Compliance Mode Quirk.
485 * Systems:
486 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
488 bool xhci_compliance_mode_recovery_timer_quirk_check(void)
490 const char *dmi_product_name, *dmi_sys_vendor;
492 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
493 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
494 if (!dmi_product_name || !dmi_sys_vendor)
495 return false;
497 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
498 return false;
500 if (strstr(dmi_product_name, "Z420") ||
501 strstr(dmi_product_name, "Z620") ||
502 strstr(dmi_product_name, "Z820") ||
503 strstr(dmi_product_name, "Z1 Workstation"))
504 return true;
506 return false;
509 static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
511 return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
516 * Initialize memory for HCD and xHC (one-time init).
518 * Program the PAGESIZE register, initialize the device context array, create
519 * device contexts (?), set up a command ring segment (or two?), create event
520 * ring (one for now).
522 int xhci_init(struct usb_hcd *hcd)
524 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
525 int retval = 0;
527 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init");
528 spin_lock_init(&xhci->lock);
529 if (xhci->hci_version == 0x95 && link_quirk) {
530 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
531 "QUIRK: Not clearing Link TRB chain bits.");
532 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
533 } else {
534 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
535 "xHCI doesn't need link TRB QUIRK");
537 retval = xhci_mem_init(xhci, GFP_KERNEL);
538 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init");
540 /* Initializing Compliance Mode Recovery Data If Needed */
541 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
542 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
543 compliance_mode_recovery_timer_init(xhci);
546 return retval;
549 /*-------------------------------------------------------------------------*/
552 static int xhci_run_finished(struct xhci_hcd *xhci)
554 if (xhci_start(xhci)) {
555 xhci_halt(xhci);
556 return -ENODEV;
558 xhci->shared_hcd->state = HC_STATE_RUNNING;
559 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
561 if (xhci->quirks & XHCI_NEC_HOST)
562 xhci_ring_cmd_db(xhci);
564 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
565 "Finished xhci_run for USB3 roothub");
566 return 0;
570 * Start the HC after it was halted.
572 * This function is called by the USB core when the HC driver is added.
573 * Its opposite is xhci_stop().
575 * xhci_init() must be called once before this function can be called.
576 * Reset the HC, enable device slot contexts, program DCBAAP, and
577 * set command ring pointer and event ring pointer.
579 * Setup MSI-X vectors and enable interrupts.
581 int xhci_run(struct usb_hcd *hcd)
583 u32 temp;
584 u64 temp_64;
585 int ret;
586 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
588 /* Start the xHCI host controller running only after the USB 2.0 roothub
589 * is setup.
592 hcd->uses_new_polling = 1;
593 if (!usb_hcd_is_primary_hcd(hcd))
594 return xhci_run_finished(xhci);
596 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");
598 ret = xhci_try_enable_msi(hcd);
599 if (ret)
600 return ret;
602 xhci_dbg(xhci, "Command ring memory map follows:\n");
603 xhci_debug_ring(xhci, xhci->cmd_ring);
604 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
605 xhci_dbg_cmd_ptrs(xhci);
607 xhci_dbg(xhci, "ERST memory map follows:\n");
608 xhci_dbg_erst(xhci, &xhci->erst);
609 xhci_dbg(xhci, "Event ring:\n");
610 xhci_debug_ring(xhci, xhci->event_ring);
611 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
612 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
613 temp_64 &= ~ERST_PTR_MASK;
614 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
615 "ERST deq = 64'h%0lx", (long unsigned int) temp_64);
617 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
618 "// Set the interrupt modulation register");
619 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
620 temp &= ~ER_IRQ_INTERVAL_MASK;
621 temp |= (u32) 160;
622 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
624 /* Set the HCD state before we enable the irqs */
625 temp = xhci_readl(xhci, &xhci->op_regs->command);
626 temp |= (CMD_EIE);
627 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
628 "// Enable interrupts, cmd = 0x%x.", temp);
629 xhci_writel(xhci, temp, &xhci->op_regs->command);
631 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
632 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
633 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
634 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
635 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
636 &xhci->ir_set->irq_pending);
637 xhci_print_ir_set(xhci, 0);
639 if (xhci->quirks & XHCI_NEC_HOST)
640 xhci_queue_vendor_command(xhci, 0, 0, 0,
641 TRB_TYPE(TRB_NEC_GET_FW));
643 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
644 "Finished xhci_run for USB2 roothub");
645 return 0;
648 static void xhci_only_stop_hcd(struct usb_hcd *hcd)
650 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
652 spin_lock_irq(&xhci->lock);
653 xhci_halt(xhci);
655 /* The shared_hcd is going to be deallocated shortly (the USB core only
656 * calls this function when allocation fails in usb_add_hcd(), or
657 * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
659 xhci->shared_hcd = NULL;
660 spin_unlock_irq(&xhci->lock);
664 * Stop xHCI driver.
666 * This function is called by the USB core when the HC driver is removed.
667 * Its opposite is xhci_run().
669 * Disable device contexts, disable IRQs, and quiesce the HC.
670 * Reset the HC, finish any completed transactions, and cleanup memory.
672 void xhci_stop(struct usb_hcd *hcd)
674 u32 temp;
675 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
677 if (!usb_hcd_is_primary_hcd(hcd)) {
678 xhci_only_stop_hcd(xhci->shared_hcd);
679 return;
682 spin_lock_irq(&xhci->lock);
683 /* Make sure the xHC is halted for a USB3 roothub
684 * (xhci_stop() could be called as part of failed init).
686 xhci_halt(xhci);
687 xhci_reset(xhci);
688 spin_unlock_irq(&xhci->lock);
690 xhci_cleanup_msix(xhci);
692 /* Deleting Compliance Mode Recovery Timer */
693 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
694 (!(xhci_all_ports_seen_u0(xhci)))) {
695 del_timer_sync(&xhci->comp_mode_recovery_timer);
696 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
697 "%s: compliance mode recovery timer deleted",
698 __func__);
701 if (xhci->quirks & XHCI_AMD_PLL_FIX)
702 usb_amd_dev_put();
704 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
705 "// Disabling event ring interrupts");
706 temp = xhci_readl(xhci, &xhci->op_regs->status);
707 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
708 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
709 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
710 &xhci->ir_set->irq_pending);
711 xhci_print_ir_set(xhci, 0);
713 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
714 xhci_mem_cleanup(xhci);
715 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
716 "xhci_stop completed - status = %x",
717 xhci_readl(xhci, &xhci->op_regs->status));
721 * Shutdown HC (not bus-specific)
723 * This is called when the machine is rebooting or halting. We assume that the
724 * machine will be powered off, and the HC's internal state will be reset.
725 * Don't bother to free memory.
727 * This will only ever be called with the main usb_hcd (the USB3 roothub).
729 void xhci_shutdown(struct usb_hcd *hcd)
731 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
733 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
734 usb_disable_xhci_ports(to_pci_dev(hcd->self.controller));
736 spin_lock_irq(&xhci->lock);
737 xhci_halt(xhci);
738 /* Workaround for spurious wakeups at shutdown with HSW */
739 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
740 xhci_reset(xhci);
741 spin_unlock_irq(&xhci->lock);
743 xhci_cleanup_msix(xhci);
745 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
746 "xhci_shutdown completed - status = %x",
747 xhci_readl(xhci, &xhci->op_regs->status));
749 /* Yet another workaround for spurious wakeups at shutdown with HSW */
750 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
751 pci_set_power_state(to_pci_dev(hcd->self.controller), PCI_D3hot);
754 #ifdef CONFIG_PM
755 static void xhci_save_registers(struct xhci_hcd *xhci)
757 xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
758 xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
759 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
760 xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
761 xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
762 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
763 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
764 xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
765 xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
768 static void xhci_restore_registers(struct xhci_hcd *xhci)
770 xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
771 xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
772 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
773 xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
774 xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
775 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
776 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
777 xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
778 xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
781 static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
783 u64 val_64;
785 /* step 2: initialize command ring buffer */
786 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
787 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
788 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
789 xhci->cmd_ring->dequeue) &
790 (u64) ~CMD_RING_RSVD_BITS) |
791 xhci->cmd_ring->cycle_state;
792 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
793 "// Setting command ring address to 0x%llx",
794 (long unsigned long) val_64);
795 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
799 * The whole command ring must be cleared to zero when we suspend the host.
801 * The host doesn't save the command ring pointer in the suspend well, so we
802 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
803 * aligned, because of the reserved bits in the command ring dequeue pointer
804 * register. Therefore, we can't just set the dequeue pointer back in the
805 * middle of the ring (TRBs are 16-byte aligned).
807 static void xhci_clear_command_ring(struct xhci_hcd *xhci)
809 struct xhci_ring *ring;
810 struct xhci_segment *seg;
812 ring = xhci->cmd_ring;
813 seg = ring->deq_seg;
814 do {
815 memset(seg->trbs, 0,
816 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
817 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
818 cpu_to_le32(~TRB_CYCLE);
819 seg = seg->next;
820 } while (seg != ring->deq_seg);
822 /* Reset the software enqueue and dequeue pointers */
823 ring->deq_seg = ring->first_seg;
824 ring->dequeue = ring->first_seg->trbs;
825 ring->enq_seg = ring->deq_seg;
826 ring->enqueue = ring->dequeue;
828 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
830 * Ring is now zeroed, so the HW should look for change of ownership
831 * when the cycle bit is set to 1.
833 ring->cycle_state = 1;
836 * Reset the hardware dequeue pointer.
837 * Yes, this will need to be re-written after resume, but we're paranoid
838 * and want to make sure the hardware doesn't access bogus memory
839 * because, say, the BIOS or an SMI started the host without changing
840 * the command ring pointers.
842 xhci_set_cmd_ring_deq(xhci);
845 static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
847 int port_index;
848 __le32 __iomem **port_array;
849 unsigned long flags;
850 u32 t1, t2;
852 spin_lock_irqsave(&xhci->lock, flags);
854 /* disble usb3 ports Wake bits*/
855 port_index = xhci->num_usb3_ports;
856 port_array = xhci->usb3_ports;
857 while (port_index--) {
858 t1 = readl(port_array[port_index]);
859 t1 = xhci_port_state_to_neutral(t1);
860 t2 = t1 & ~PORT_WAKE_BITS;
861 if (t1 != t2)
862 writel(t2, port_array[port_index]);
865 /* disble usb2 ports Wake bits*/
866 port_index = xhci->num_usb2_ports;
867 port_array = xhci->usb2_ports;
868 while (port_index--) {
869 t1 = readl(port_array[port_index]);
870 t1 = xhci_port_state_to_neutral(t1);
871 t2 = t1 & ~PORT_WAKE_BITS;
872 if (t1 != t2)
873 writel(t2, port_array[port_index]);
876 spin_unlock_irqrestore(&xhci->lock, flags);
880 * Stop HC (not bus-specific)
882 * This is called when the machine transition into S3/S4 mode.
885 int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
887 int rc = 0;
888 unsigned int delay = XHCI_MAX_HALT_USEC;
889 struct usb_hcd *hcd = xhci_to_hcd(xhci);
890 u32 command;
892 if (hcd->state != HC_STATE_SUSPENDED ||
893 xhci->shared_hcd->state != HC_STATE_SUSPENDED)
894 return -EINVAL;
896 /* Clear root port wake on bits if wakeup not allowed. */
897 if (!do_wakeup)
898 xhci_disable_port_wake_on_bits(xhci);
900 /* Don't poll the roothubs on bus suspend. */
901 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
902 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
903 del_timer_sync(&hcd->rh_timer);
905 spin_lock_irq(&xhci->lock);
906 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
907 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
908 /* step 1: stop endpoint */
909 /* skipped assuming that port suspend has done */
911 /* step 2: clear Run/Stop bit */
912 command = xhci_readl(xhci, &xhci->op_regs->command);
913 command &= ~CMD_RUN;
914 xhci_writel(xhci, command, &xhci->op_regs->command);
916 /* Some chips from Fresco Logic need an extraordinary delay */
917 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
919 if (xhci_handshake(xhci, &xhci->op_regs->status,
920 STS_HALT, STS_HALT, delay)) {
921 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
922 spin_unlock_irq(&xhci->lock);
923 return -ETIMEDOUT;
925 xhci_clear_command_ring(xhci);
927 /* step 3: save registers */
928 xhci_save_registers(xhci);
930 /* step 4: set CSS flag */
931 command = xhci_readl(xhci, &xhci->op_regs->command);
932 command |= CMD_CSS;
933 xhci_writel(xhci, command, &xhci->op_regs->command);
934 if (xhci_handshake(xhci, &xhci->op_regs->status,
935 STS_SAVE, 0, 10 * 1000)) {
936 xhci_warn(xhci, "WARN: xHC save state timeout\n");
937 spin_unlock_irq(&xhci->lock);
938 return -ETIMEDOUT;
940 spin_unlock_irq(&xhci->lock);
943 * Deleting Compliance Mode Recovery Timer because the xHCI Host
944 * is about to be suspended.
946 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
947 (!(xhci_all_ports_seen_u0(xhci)))) {
948 del_timer_sync(&xhci->comp_mode_recovery_timer);
949 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
950 "%s: compliance mode recovery timer deleted",
951 __func__);
954 /* step 5: remove core well power */
955 /* synchronize irq when using MSI-X */
956 xhci_msix_sync_irqs(xhci);
958 return rc;
962 * start xHC (not bus-specific)
964 * This is called when the machine transition from S3/S4 mode.
967 int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
969 u32 command, temp = 0, status;
970 struct usb_hcd *hcd = xhci_to_hcd(xhci);
971 struct usb_hcd *secondary_hcd;
972 int retval = 0;
973 bool comp_timer_running = false;
975 /* Wait a bit if either of the roothubs need to settle from the
976 * transition into bus suspend.
978 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
979 time_before(jiffies,
980 xhci->bus_state[1].next_statechange))
981 msleep(100);
983 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
984 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
986 spin_lock_irq(&xhci->lock);
987 if (xhci->quirks & XHCI_RESET_ON_RESUME)
988 hibernated = true;
990 if (!hibernated) {
991 /* step 1: restore register */
992 xhci_restore_registers(xhci);
993 /* step 2: initialize command ring buffer */
994 xhci_set_cmd_ring_deq(xhci);
995 /* step 3: restore state and start state*/
996 /* step 3: set CRS flag */
997 command = xhci_readl(xhci, &xhci->op_regs->command);
998 command |= CMD_CRS;
999 xhci_writel(xhci, command, &xhci->op_regs->command);
1000 if (xhci_handshake(xhci, &xhci->op_regs->status,
1001 STS_RESTORE, 0, 10 * 1000)) {
1002 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
1003 spin_unlock_irq(&xhci->lock);
1004 return -ETIMEDOUT;
1006 temp = xhci_readl(xhci, &xhci->op_regs->status);
1009 /* If restore operation fails, re-initialize the HC during resume */
1010 if ((temp & STS_SRE) || hibernated) {
1012 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1013 !(xhci_all_ports_seen_u0(xhci))) {
1014 del_timer_sync(&xhci->comp_mode_recovery_timer);
1015 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1016 "Compliance Mode Recovery Timer deleted!");
1019 /* Let the USB core know _both_ roothubs lost power. */
1020 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1021 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
1023 xhci_dbg(xhci, "Stop HCD\n");
1024 xhci_halt(xhci);
1025 xhci_reset(xhci);
1026 spin_unlock_irq(&xhci->lock);
1027 xhci_cleanup_msix(xhci);
1029 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
1030 temp = xhci_readl(xhci, &xhci->op_regs->status);
1031 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
1032 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
1033 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
1034 &xhci->ir_set->irq_pending);
1035 xhci_print_ir_set(xhci, 0);
1037 xhci_dbg(xhci, "cleaning up memory\n");
1038 xhci_mem_cleanup(xhci);
1039 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
1040 xhci_readl(xhci, &xhci->op_regs->status));
1042 /* USB core calls the PCI reinit and start functions twice:
1043 * first with the primary HCD, and then with the secondary HCD.
1044 * If we don't do the same, the host will never be started.
1046 if (!usb_hcd_is_primary_hcd(hcd))
1047 secondary_hcd = hcd;
1048 else
1049 secondary_hcd = xhci->shared_hcd;
1051 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1052 retval = xhci_init(hcd->primary_hcd);
1053 if (retval)
1054 return retval;
1055 comp_timer_running = true;
1057 xhci_dbg(xhci, "Start the primary HCD\n");
1058 retval = xhci_run(hcd->primary_hcd);
1059 if (!retval) {
1060 xhci_dbg(xhci, "Start the secondary HCD\n");
1061 retval = xhci_run(secondary_hcd);
1063 hcd->state = HC_STATE_SUSPENDED;
1064 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
1065 goto done;
1068 /* step 4: set Run/Stop bit */
1069 command = xhci_readl(xhci, &xhci->op_regs->command);
1070 command |= CMD_RUN;
1071 xhci_writel(xhci, command, &xhci->op_regs->command);
1072 xhci_handshake(xhci, &xhci->op_regs->status, STS_HALT,
1073 0, 250 * 1000);
1075 /* step 5: walk topology and initialize portsc,
1076 * portpmsc and portli
1078 /* this is done in bus_resume */
1080 /* step 6: restart each of the previously
1081 * Running endpoints by ringing their doorbells
1084 spin_unlock_irq(&xhci->lock);
1086 done:
1087 if (retval == 0) {
1088 /* Resume root hubs only when have pending events. */
1089 status = readl(&xhci->op_regs->status);
1090 if (status & STS_EINT) {
1091 usb_hcd_resume_root_hub(hcd);
1092 usb_hcd_resume_root_hub(xhci->shared_hcd);
1097 * If system is subject to the Quirk, Compliance Mode Timer needs to
1098 * be re-initialized Always after a system resume. Ports are subject
1099 * to suffer the Compliance Mode issue again. It doesn't matter if
1100 * ports have entered previously to U0 before system's suspension.
1102 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
1103 compliance_mode_recovery_timer_init(xhci);
1105 /* Re-enable port polling. */
1106 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
1107 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1108 usb_hcd_poll_rh_status(hcd);
1110 return retval;
1112 #endif /* CONFIG_PM */
1114 /*-------------------------------------------------------------------------*/
1117 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1118 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1119 * value to right shift 1 for the bitmask.
1121 * Index = (epnum * 2) + direction - 1,
1122 * where direction = 0 for OUT, 1 for IN.
1123 * For control endpoints, the IN index is used (OUT index is unused), so
1124 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1126 unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1128 unsigned int index;
1129 if (usb_endpoint_xfer_control(desc))
1130 index = (unsigned int) (usb_endpoint_num(desc)*2);
1131 else
1132 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1133 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1134 return index;
1137 /* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1138 * address from the XHCI endpoint index.
1140 unsigned int xhci_get_endpoint_address(unsigned int ep_index)
1142 unsigned int number = DIV_ROUND_UP(ep_index, 2);
1143 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
1144 return direction | number;
1147 /* Find the flag for this endpoint (for use in the control context). Use the
1148 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1149 * bit 1, etc.
1151 unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
1153 return 1 << (xhci_get_endpoint_index(desc) + 1);
1156 /* Find the flag for this endpoint (for use in the control context). Use the
1157 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1158 * bit 1, etc.
1160 unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
1162 return 1 << (ep_index + 1);
1165 /* Compute the last valid endpoint context index. Basically, this is the
1166 * endpoint index plus one. For slot contexts with more than valid endpoint,
1167 * we find the most significant bit set in the added contexts flags.
1168 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1169 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1171 unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
1173 return fls(added_ctxs) - 1;
1176 /* Returns 1 if the arguments are OK;
1177 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1179 static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
1180 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1181 const char *func) {
1182 struct xhci_hcd *xhci;
1183 struct xhci_virt_device *virt_dev;
1185 if (!hcd || (check_ep && !ep) || !udev) {
1186 pr_debug("xHCI %s called with invalid args\n", func);
1187 return -EINVAL;
1189 if (!udev->parent) {
1190 pr_debug("xHCI %s called for root hub\n", func);
1191 return 0;
1194 xhci = hcd_to_xhci(hcd);
1195 if (check_virt_dev) {
1196 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
1197 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
1198 func);
1199 return -EINVAL;
1202 virt_dev = xhci->devs[udev->slot_id];
1203 if (virt_dev->udev != udev) {
1204 xhci_dbg(xhci, "xHCI %s called with udev and "
1205 "virt_dev does not match\n", func);
1206 return -EINVAL;
1210 if (xhci->xhc_state & XHCI_STATE_HALTED)
1211 return -ENODEV;
1213 return 1;
1216 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1217 struct usb_device *udev, struct xhci_command *command,
1218 bool ctx_change, bool must_succeed);
1221 * Full speed devices may have a max packet size greater than 8 bytes, but the
1222 * USB core doesn't know that until it reads the first 8 bytes of the
1223 * descriptor. If the usb_device's max packet size changes after that point,
1224 * we need to issue an evaluate context command and wait on it.
1226 static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1227 unsigned int ep_index, struct urb *urb)
1229 struct xhci_container_ctx *in_ctx;
1230 struct xhci_container_ctx *out_ctx;
1231 struct xhci_input_control_ctx *ctrl_ctx;
1232 struct xhci_ep_ctx *ep_ctx;
1233 int max_packet_size;
1234 int hw_max_packet_size;
1235 int ret = 0;
1237 out_ctx = xhci->devs[slot_id]->out_ctx;
1238 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1239 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
1240 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
1241 if (hw_max_packet_size != max_packet_size) {
1242 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1243 "Max Packet Size for ep 0 changed.");
1244 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1245 "Max packet size in usb_device = %d",
1246 max_packet_size);
1247 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1248 "Max packet size in xHCI HW = %d",
1249 hw_max_packet_size);
1250 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1251 "Issuing evaluate context command.");
1253 /* Set up the input context flags for the command */
1254 /* FIXME: This won't work if a non-default control endpoint
1255 * changes max packet sizes.
1257 in_ctx = xhci->devs[slot_id]->in_ctx;
1258 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1259 if (!ctrl_ctx) {
1260 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1261 __func__);
1262 return -ENOMEM;
1264 /* Set up the modified control endpoint 0 */
1265 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1266 xhci->devs[slot_id]->out_ctx, ep_index);
1268 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1269 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1270 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1272 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
1273 ctrl_ctx->drop_flags = 0;
1275 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
1276 xhci_dbg_ctx(xhci, in_ctx, ep_index);
1277 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
1278 xhci_dbg_ctx(xhci, out_ctx, ep_index);
1280 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
1281 true, false);
1283 /* Clean up the input context for later use by bandwidth
1284 * functions.
1286 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
1288 return ret;
1292 * non-error returns are a promise to giveback() the urb later
1293 * we drop ownership so next owner (or urb unlink) can get it
1295 int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1297 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1298 struct xhci_td *buffer;
1299 unsigned long flags;
1300 int ret = 0;
1301 unsigned int slot_id, ep_index;
1302 struct urb_priv *urb_priv;
1303 int size, i;
1305 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1306 true, true, __func__) <= 0)
1307 return -EINVAL;
1309 slot_id = urb->dev->slot_id;
1310 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1312 if (!HCD_HW_ACCESSIBLE(hcd)) {
1313 if (!in_interrupt())
1314 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1315 ret = -ESHUTDOWN;
1316 goto exit;
1319 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1320 size = urb->number_of_packets;
1321 else
1322 size = 1;
1324 urb_priv = kzalloc(sizeof(struct urb_priv) +
1325 size * sizeof(struct xhci_td *), mem_flags);
1326 if (!urb_priv)
1327 return -ENOMEM;
1329 buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
1330 if (!buffer) {
1331 kfree(urb_priv);
1332 return -ENOMEM;
1335 for (i = 0; i < size; i++) {
1336 urb_priv->td[i] = buffer;
1337 buffer++;
1340 urb_priv->length = size;
1341 urb_priv->td_cnt = 0;
1342 urb->hcpriv = urb_priv;
1344 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1345 /* Check to see if the max packet size for the default control
1346 * endpoint changed during FS device enumeration
1348 if (urb->dev->speed == USB_SPEED_FULL) {
1349 ret = xhci_check_maxpacket(xhci, slot_id,
1350 ep_index, urb);
1351 if (ret < 0) {
1352 xhci_urb_free_priv(xhci, urb_priv);
1353 urb->hcpriv = NULL;
1354 return ret;
1358 /* We have a spinlock and interrupts disabled, so we must pass
1359 * atomic context to this function, which may allocate memory.
1361 spin_lock_irqsave(&xhci->lock, flags);
1362 if (xhci->xhc_state & XHCI_STATE_DYING)
1363 goto dying;
1364 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
1365 slot_id, ep_index);
1366 if (ret)
1367 goto free_priv;
1368 spin_unlock_irqrestore(&xhci->lock, flags);
1369 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1370 spin_lock_irqsave(&xhci->lock, flags);
1371 if (xhci->xhc_state & XHCI_STATE_DYING)
1372 goto dying;
1373 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1374 EP_GETTING_STREAMS) {
1375 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1376 "is transitioning to using streams.\n");
1377 ret = -EINVAL;
1378 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1379 EP_GETTING_NO_STREAMS) {
1380 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1381 "is transitioning to "
1382 "not having streams.\n");
1383 ret = -EINVAL;
1384 } else {
1385 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1386 slot_id, ep_index);
1388 if (ret)
1389 goto free_priv;
1390 spin_unlock_irqrestore(&xhci->lock, flags);
1391 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1392 spin_lock_irqsave(&xhci->lock, flags);
1393 if (xhci->xhc_state & XHCI_STATE_DYING)
1394 goto dying;
1395 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1396 slot_id, ep_index);
1397 if (ret)
1398 goto free_priv;
1399 spin_unlock_irqrestore(&xhci->lock, flags);
1400 } else {
1401 spin_lock_irqsave(&xhci->lock, flags);
1402 if (xhci->xhc_state & XHCI_STATE_DYING)
1403 goto dying;
1404 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1405 slot_id, ep_index);
1406 if (ret)
1407 goto free_priv;
1408 spin_unlock_irqrestore(&xhci->lock, flags);
1410 exit:
1411 return ret;
1412 dying:
1413 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1414 "non-responsive xHCI host.\n",
1415 urb->ep->desc.bEndpointAddress, urb);
1416 ret = -ESHUTDOWN;
1417 free_priv:
1418 xhci_urb_free_priv(xhci, urb_priv);
1419 urb->hcpriv = NULL;
1420 spin_unlock_irqrestore(&xhci->lock, flags);
1421 return ret;
1424 /* Get the right ring for the given URB.
1425 * If the endpoint supports streams, boundary check the URB's stream ID.
1426 * If the endpoint doesn't support streams, return the singular endpoint ring.
1428 static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1429 struct urb *urb)
1431 unsigned int slot_id;
1432 unsigned int ep_index;
1433 unsigned int stream_id;
1434 struct xhci_virt_ep *ep;
1436 slot_id = urb->dev->slot_id;
1437 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1438 stream_id = urb->stream_id;
1439 ep = &xhci->devs[slot_id]->eps[ep_index];
1440 /* Common case: no streams */
1441 if (!(ep->ep_state & EP_HAS_STREAMS))
1442 return ep->ring;
1444 if (stream_id == 0) {
1445 xhci_warn(xhci,
1446 "WARN: Slot ID %u, ep index %u has streams, "
1447 "but URB has no stream ID.\n",
1448 slot_id, ep_index);
1449 return NULL;
1452 if (stream_id < ep->stream_info->num_streams)
1453 return ep->stream_info->stream_rings[stream_id];
1455 xhci_warn(xhci,
1456 "WARN: Slot ID %u, ep index %u has "
1457 "stream IDs 1 to %u allocated, "
1458 "but stream ID %u is requested.\n",
1459 slot_id, ep_index,
1460 ep->stream_info->num_streams - 1,
1461 stream_id);
1462 return NULL;
1466 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1467 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1468 * should pick up where it left off in the TD, unless a Set Transfer Ring
1469 * Dequeue Pointer is issued.
1471 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1472 * the ring. Since the ring is a contiguous structure, they can't be physically
1473 * removed. Instead, there are two options:
1475 * 1) If the HC is in the middle of processing the URB to be canceled, we
1476 * simply move the ring's dequeue pointer past those TRBs using the Set
1477 * Transfer Ring Dequeue Pointer command. This will be the common case,
1478 * when drivers timeout on the last submitted URB and attempt to cancel.
1480 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1481 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1482 * HC will need to invalidate the any TRBs it has cached after the stop
1483 * endpoint command, as noted in the xHCI 0.95 errata.
1485 * 3) The TD may have completed by the time the Stop Endpoint Command
1486 * completes, so software needs to handle that case too.
1488 * This function should protect against the TD enqueueing code ringing the
1489 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1490 * It also needs to account for multiple cancellations on happening at the same
1491 * time for the same endpoint.
1493 * Note that this function can be called in any context, or so says
1494 * usb_hcd_unlink_urb()
1496 int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1498 unsigned long flags;
1499 int ret, i;
1500 u32 temp;
1501 struct xhci_hcd *xhci;
1502 struct urb_priv *urb_priv;
1503 struct xhci_td *td;
1504 unsigned int ep_index;
1505 struct xhci_ring *ep_ring;
1506 struct xhci_virt_ep *ep;
1508 xhci = hcd_to_xhci(hcd);
1509 spin_lock_irqsave(&xhci->lock, flags);
1510 /* Make sure the URB hasn't completed or been unlinked already */
1511 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1512 if (ret || !urb->hcpriv)
1513 goto done;
1514 temp = xhci_readl(xhci, &xhci->op_regs->status);
1515 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
1516 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1517 "HW died, freeing TD.");
1518 urb_priv = urb->hcpriv;
1519 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1520 td = urb_priv->td[i];
1521 if (!list_empty(&td->td_list))
1522 list_del_init(&td->td_list);
1523 if (!list_empty(&td->cancelled_td_list))
1524 list_del_init(&td->cancelled_td_list);
1527 usb_hcd_unlink_urb_from_ep(hcd, urb);
1528 spin_unlock_irqrestore(&xhci->lock, flags);
1529 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
1530 xhci_urb_free_priv(xhci, urb_priv);
1531 return ret;
1533 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
1534 (xhci->xhc_state & XHCI_STATE_HALTED)) {
1535 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1536 "Ep 0x%x: URB %p to be canceled on "
1537 "non-responsive xHCI host.",
1538 urb->ep->desc.bEndpointAddress, urb);
1539 /* Let the stop endpoint command watchdog timer (which set this
1540 * state) finish cleaning up the endpoint TD lists. We must
1541 * have caught it in the middle of dropping a lock and giving
1542 * back an URB.
1544 goto done;
1547 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1548 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
1549 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1550 if (!ep_ring) {
1551 ret = -EINVAL;
1552 goto done;
1555 urb_priv = urb->hcpriv;
1556 i = urb_priv->td_cnt;
1557 if (i < urb_priv->length)
1558 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1559 "Cancel URB %p, dev %s, ep 0x%x, "
1560 "starting at offset 0x%llx",
1561 urb, urb->dev->devpath,
1562 urb->ep->desc.bEndpointAddress,
1563 (unsigned long long) xhci_trb_virt_to_dma(
1564 urb_priv->td[i]->start_seg,
1565 urb_priv->td[i]->first_trb));
1567 for (; i < urb_priv->length; i++) {
1568 td = urb_priv->td[i];
1569 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1572 /* Queue a stop endpoint command, but only if this is
1573 * the first cancellation to be handled.
1575 if (!(ep->ep_state & EP_HALT_PENDING)) {
1576 ep->ep_state |= EP_HALT_PENDING;
1577 ep->stop_cmds_pending++;
1578 ep->stop_cmd_timer.expires = jiffies +
1579 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1580 add_timer(&ep->stop_cmd_timer);
1581 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
1582 xhci_ring_cmd_db(xhci);
1584 done:
1585 spin_unlock_irqrestore(&xhci->lock, flags);
1586 return ret;
1589 /* Drop an endpoint from a new bandwidth configuration for this device.
1590 * Only one call to this function is allowed per endpoint before
1591 * check_bandwidth() or reset_bandwidth() must be called.
1592 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1593 * add the endpoint to the schedule with possibly new parameters denoted by a
1594 * different endpoint descriptor in usb_host_endpoint.
1595 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1596 * not allowed.
1598 * The USB core will not allow URBs to be queued to an endpoint that is being
1599 * disabled, so there's no need for mutual exclusion to protect
1600 * the xhci->devs[slot_id] structure.
1602 int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1603 struct usb_host_endpoint *ep)
1605 struct xhci_hcd *xhci;
1606 struct xhci_container_ctx *in_ctx, *out_ctx;
1607 struct xhci_input_control_ctx *ctrl_ctx;
1608 struct xhci_slot_ctx *slot_ctx;
1609 unsigned int last_ctx;
1610 unsigned int ep_index;
1611 struct xhci_ep_ctx *ep_ctx;
1612 u32 drop_flag;
1613 u32 new_add_flags, new_drop_flags, new_slot_info;
1614 int ret;
1616 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
1617 if (ret <= 0)
1618 return ret;
1619 xhci = hcd_to_xhci(hcd);
1620 if (xhci->xhc_state & XHCI_STATE_DYING)
1621 return -ENODEV;
1623 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1624 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1625 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1626 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1627 __func__, drop_flag);
1628 return 0;
1631 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
1632 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1633 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1634 if (!ctrl_ctx) {
1635 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1636 __func__);
1637 return 0;
1640 ep_index = xhci_get_endpoint_index(&ep->desc);
1641 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1642 /* If the HC already knows the endpoint is disabled,
1643 * or the HCD has noted it is disabled, ignore this request
1645 if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1646 cpu_to_le32(EP_STATE_DISABLED)) ||
1647 le32_to_cpu(ctrl_ctx->drop_flags) &
1648 xhci_get_endpoint_flag(&ep->desc)) {
1649 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1650 __func__, ep);
1651 return 0;
1654 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1655 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1657 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1658 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1660 last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
1661 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1662 /* Update the last valid endpoint context, if we deleted the last one */
1663 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
1664 LAST_CTX(last_ctx)) {
1665 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1666 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
1668 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
1670 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1672 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1673 (unsigned int) ep->desc.bEndpointAddress,
1674 udev->slot_id,
1675 (unsigned int) new_drop_flags,
1676 (unsigned int) new_add_flags,
1677 (unsigned int) new_slot_info);
1678 return 0;
1681 /* Add an endpoint to a new possible bandwidth configuration for this device.
1682 * Only one call to this function is allowed per endpoint before
1683 * check_bandwidth() or reset_bandwidth() must be called.
1684 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1685 * add the endpoint to the schedule with possibly new parameters denoted by a
1686 * different endpoint descriptor in usb_host_endpoint.
1687 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1688 * not allowed.
1690 * The USB core will not allow URBs to be queued to an endpoint until the
1691 * configuration or alt setting is installed in the device, so there's no need
1692 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1694 int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1695 struct usb_host_endpoint *ep)
1697 struct xhci_hcd *xhci;
1698 struct xhci_container_ctx *in_ctx, *out_ctx;
1699 unsigned int ep_index;
1700 struct xhci_slot_ctx *slot_ctx;
1701 struct xhci_input_control_ctx *ctrl_ctx;
1702 u32 added_ctxs;
1703 unsigned int last_ctx;
1704 u32 new_add_flags, new_drop_flags, new_slot_info;
1705 struct xhci_virt_device *virt_dev;
1706 int ret = 0;
1708 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
1709 if (ret <= 0) {
1710 /* So we won't queue a reset ep command for a root hub */
1711 ep->hcpriv = NULL;
1712 return ret;
1714 xhci = hcd_to_xhci(hcd);
1715 if (xhci->xhc_state & XHCI_STATE_DYING)
1716 return -ENODEV;
1718 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1719 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1720 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1721 /* FIXME when we have to issue an evaluate endpoint command to
1722 * deal with ep0 max packet size changing once we get the
1723 * descriptors
1725 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1726 __func__, added_ctxs);
1727 return 0;
1730 virt_dev = xhci->devs[udev->slot_id];
1731 in_ctx = virt_dev->in_ctx;
1732 out_ctx = virt_dev->out_ctx;
1733 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1734 if (!ctrl_ctx) {
1735 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1736 __func__);
1737 return 0;
1740 ep_index = xhci_get_endpoint_index(&ep->desc);
1741 /* If this endpoint is already in use, and the upper layers are trying
1742 * to add it again without dropping it, reject the addition.
1744 if (virt_dev->eps[ep_index].ring &&
1745 !(le32_to_cpu(ctrl_ctx->drop_flags) &
1746 xhci_get_endpoint_flag(&ep->desc))) {
1747 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1748 "without dropping it.\n",
1749 (unsigned int) ep->desc.bEndpointAddress);
1750 return -EINVAL;
1753 /* If the HCD has already noted the endpoint is enabled,
1754 * ignore this request.
1756 if (le32_to_cpu(ctrl_ctx->add_flags) &
1757 xhci_get_endpoint_flag(&ep->desc)) {
1758 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1759 __func__, ep);
1760 return 0;
1764 * Configuration and alternate setting changes must be done in
1765 * process context, not interrupt context (or so documenation
1766 * for usb_set_interface() and usb_set_configuration() claim).
1768 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
1769 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1770 __func__, ep->desc.bEndpointAddress);
1771 return -ENOMEM;
1774 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1775 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1777 /* If xhci_endpoint_disable() was called for this endpoint, but the
1778 * xHC hasn't been notified yet through the check_bandwidth() call,
1779 * this re-adds a new state for the endpoint from the new endpoint
1780 * descriptors. We must drop and re-add this endpoint, so we leave the
1781 * drop flags alone.
1783 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1785 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1786 /* Update the last valid endpoint context, if we just added one past */
1787 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
1788 LAST_CTX(last_ctx)) {
1789 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1790 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
1792 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
1794 /* Store the usb_device pointer for later use */
1795 ep->hcpriv = udev;
1797 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1798 (unsigned int) ep->desc.bEndpointAddress,
1799 udev->slot_id,
1800 (unsigned int) new_drop_flags,
1801 (unsigned int) new_add_flags,
1802 (unsigned int) new_slot_info);
1803 return 0;
1806 static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
1808 struct xhci_input_control_ctx *ctrl_ctx;
1809 struct xhci_ep_ctx *ep_ctx;
1810 struct xhci_slot_ctx *slot_ctx;
1811 int i;
1813 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1814 if (!ctrl_ctx) {
1815 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1816 __func__);
1817 return;
1820 /* When a device's add flag and drop flag are zero, any subsequent
1821 * configure endpoint command will leave that endpoint's state
1822 * untouched. Make sure we don't leave any old state in the input
1823 * endpoint contexts.
1825 ctrl_ctx->drop_flags = 0;
1826 ctrl_ctx->add_flags = 0;
1827 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1828 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1829 /* Endpoint 0 is always valid */
1830 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
1831 for (i = 1; i < 31; ++i) {
1832 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
1833 ep_ctx->ep_info = 0;
1834 ep_ctx->ep_info2 = 0;
1835 ep_ctx->deq = 0;
1836 ep_ctx->tx_info = 0;
1840 static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
1841 struct usb_device *udev, u32 *cmd_status)
1843 int ret;
1845 switch (*cmd_status) {
1846 case COMP_ENOMEM:
1847 dev_warn(&udev->dev, "Not enough host controller resources "
1848 "for new device state.\n");
1849 ret = -ENOMEM;
1850 /* FIXME: can we allocate more resources for the HC? */
1851 break;
1852 case COMP_BW_ERR:
1853 case COMP_2ND_BW_ERR:
1854 dev_warn(&udev->dev, "Not enough bandwidth "
1855 "for new device state.\n");
1856 ret = -ENOSPC;
1857 /* FIXME: can we go back to the old state? */
1858 break;
1859 case COMP_TRB_ERR:
1860 /* the HCD set up something wrong */
1861 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1862 "add flag = 1, "
1863 "and endpoint is not disabled.\n");
1864 ret = -EINVAL;
1865 break;
1866 case COMP_DEV_ERR:
1867 dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
1868 "configure command.\n");
1869 ret = -ENODEV;
1870 break;
1871 case COMP_SUCCESS:
1872 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1873 "Successful Endpoint Configure command");
1874 ret = 0;
1875 break;
1876 default:
1877 xhci_err(xhci, "ERROR: unexpected command completion "
1878 "code 0x%x.\n", *cmd_status);
1879 ret = -EINVAL;
1880 break;
1882 return ret;
1885 static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
1886 struct usb_device *udev, u32 *cmd_status)
1888 int ret;
1889 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
1891 switch (*cmd_status) {
1892 case COMP_EINVAL:
1893 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1894 "context command.\n");
1895 ret = -EINVAL;
1896 break;
1897 case COMP_EBADSLT:
1898 dev_warn(&udev->dev, "WARN: slot not enabled for"
1899 "evaluate context command.\n");
1900 ret = -EINVAL;
1901 break;
1902 case COMP_CTX_STATE:
1903 dev_warn(&udev->dev, "WARN: invalid context state for "
1904 "evaluate context command.\n");
1905 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1906 ret = -EINVAL;
1907 break;
1908 case COMP_DEV_ERR:
1909 dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
1910 "context command.\n");
1911 ret = -ENODEV;
1912 break;
1913 case COMP_MEL_ERR:
1914 /* Max Exit Latency too large error */
1915 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1916 ret = -EINVAL;
1917 break;
1918 case COMP_SUCCESS:
1919 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1920 "Successful evaluate context command");
1921 ret = 0;
1922 break;
1923 default:
1924 xhci_err(xhci, "ERROR: unexpected command completion "
1925 "code 0x%x.\n", *cmd_status);
1926 ret = -EINVAL;
1927 break;
1929 return ret;
1932 static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
1933 struct xhci_input_control_ctx *ctrl_ctx)
1935 u32 valid_add_flags;
1936 u32 valid_drop_flags;
1938 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1939 * (bit 1). The default control endpoint is added during the Address
1940 * Device command and is never removed until the slot is disabled.
1942 valid_add_flags = ctrl_ctx->add_flags >> 2;
1943 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1945 /* Use hweight32 to count the number of ones in the add flags, or
1946 * number of endpoints added. Don't count endpoints that are changed
1947 * (both added and dropped).
1949 return hweight32(valid_add_flags) -
1950 hweight32(valid_add_flags & valid_drop_flags);
1953 static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
1954 struct xhci_input_control_ctx *ctrl_ctx)
1956 u32 valid_add_flags;
1957 u32 valid_drop_flags;
1959 valid_add_flags = ctrl_ctx->add_flags >> 2;
1960 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1962 return hweight32(valid_drop_flags) -
1963 hweight32(valid_add_flags & valid_drop_flags);
1967 * We need to reserve the new number of endpoints before the configure endpoint
1968 * command completes. We can't subtract the dropped endpoints from the number
1969 * of active endpoints until the command completes because we can oversubscribe
1970 * the host in this case:
1972 * - the first configure endpoint command drops more endpoints than it adds
1973 * - a second configure endpoint command that adds more endpoints is queued
1974 * - the first configure endpoint command fails, so the config is unchanged
1975 * - the second command may succeed, even though there isn't enough resources
1977 * Must be called with xhci->lock held.
1979 static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
1980 struct xhci_input_control_ctx *ctrl_ctx)
1982 u32 added_eps;
1984 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
1985 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
1986 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1987 "Not enough ep ctxs: "
1988 "%u active, need to add %u, limit is %u.",
1989 xhci->num_active_eps, added_eps,
1990 xhci->limit_active_eps);
1991 return -ENOMEM;
1993 xhci->num_active_eps += added_eps;
1994 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1995 "Adding %u ep ctxs, %u now active.", added_eps,
1996 xhci->num_active_eps);
1997 return 0;
2001 * The configure endpoint was failed by the xHC for some other reason, so we
2002 * need to revert the resources that failed configuration would have used.
2004 * Must be called with xhci->lock held.
2006 static void xhci_free_host_resources(struct xhci_hcd *xhci,
2007 struct xhci_input_control_ctx *ctrl_ctx)
2009 u32 num_failed_eps;
2011 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
2012 xhci->num_active_eps -= num_failed_eps;
2013 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2014 "Removing %u failed ep ctxs, %u now active.",
2015 num_failed_eps,
2016 xhci->num_active_eps);
2020 * Now that the command has completed, clean up the active endpoint count by
2021 * subtracting out the endpoints that were dropped (but not changed).
2023 * Must be called with xhci->lock held.
2025 static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
2026 struct xhci_input_control_ctx *ctrl_ctx)
2028 u32 num_dropped_eps;
2030 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
2031 xhci->num_active_eps -= num_dropped_eps;
2032 if (num_dropped_eps)
2033 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2034 "Removing %u dropped ep ctxs, %u now active.",
2035 num_dropped_eps,
2036 xhci->num_active_eps);
2039 static unsigned int xhci_get_block_size(struct usb_device *udev)
2041 switch (udev->speed) {
2042 case USB_SPEED_LOW:
2043 case USB_SPEED_FULL:
2044 return FS_BLOCK;
2045 case USB_SPEED_HIGH:
2046 return HS_BLOCK;
2047 case USB_SPEED_SUPER:
2048 return SS_BLOCK;
2049 case USB_SPEED_UNKNOWN:
2050 case USB_SPEED_WIRELESS:
2051 default:
2052 /* Should never happen */
2053 return 1;
2057 static unsigned int
2058 xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
2060 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2061 return LS_OVERHEAD;
2062 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2063 return FS_OVERHEAD;
2064 return HS_OVERHEAD;
2067 /* If we are changing a LS/FS device under a HS hub,
2068 * make sure (if we are activating a new TT) that the HS bus has enough
2069 * bandwidth for this new TT.
2071 static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2072 struct xhci_virt_device *virt_dev,
2073 int old_active_eps)
2075 struct xhci_interval_bw_table *bw_table;
2076 struct xhci_tt_bw_info *tt_info;
2078 /* Find the bandwidth table for the root port this TT is attached to. */
2079 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2080 tt_info = virt_dev->tt_info;
2081 /* If this TT already had active endpoints, the bandwidth for this TT
2082 * has already been added. Removing all periodic endpoints (and thus
2083 * making the TT enactive) will only decrease the bandwidth used.
2085 if (old_active_eps)
2086 return 0;
2087 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2088 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2089 return -ENOMEM;
2090 return 0;
2092 /* Not sure why we would have no new active endpoints...
2094 * Maybe because of an Evaluate Context change for a hub update or a
2095 * control endpoint 0 max packet size change?
2096 * FIXME: skip the bandwidth calculation in that case.
2098 return 0;
2101 static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2102 struct xhci_virt_device *virt_dev)
2104 unsigned int bw_reserved;
2106 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2107 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2108 return -ENOMEM;
2110 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2111 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2112 return -ENOMEM;
2114 return 0;
2118 * This algorithm is a very conservative estimate of the worst-case scheduling
2119 * scenario for any one interval. The hardware dynamically schedules the
2120 * packets, so we can't tell which microframe could be the limiting factor in
2121 * the bandwidth scheduling. This only takes into account periodic endpoints.
2123 * Obviously, we can't solve an NP complete problem to find the minimum worst
2124 * case scenario. Instead, we come up with an estimate that is no less than
2125 * the worst case bandwidth used for any one microframe, but may be an
2126 * over-estimate.
2128 * We walk the requirements for each endpoint by interval, starting with the
2129 * smallest interval, and place packets in the schedule where there is only one
2130 * possible way to schedule packets for that interval. In order to simplify
2131 * this algorithm, we record the largest max packet size for each interval, and
2132 * assume all packets will be that size.
2134 * For interval 0, we obviously must schedule all packets for each interval.
2135 * The bandwidth for interval 0 is just the amount of data to be transmitted
2136 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2137 * the number of packets).
2139 * For interval 1, we have two possible microframes to schedule those packets
2140 * in. For this algorithm, if we can schedule the same number of packets for
2141 * each possible scheduling opportunity (each microframe), we will do so. The
2142 * remaining number of packets will be saved to be transmitted in the gaps in
2143 * the next interval's scheduling sequence.
2145 * As we move those remaining packets to be scheduled with interval 2 packets,
2146 * we have to double the number of remaining packets to transmit. This is
2147 * because the intervals are actually powers of 2, and we would be transmitting
2148 * the previous interval's packets twice in this interval. We also have to be
2149 * sure that when we look at the largest max packet size for this interval, we
2150 * also look at the largest max packet size for the remaining packets and take
2151 * the greater of the two.
2153 * The algorithm continues to evenly distribute packets in each scheduling
2154 * opportunity, and push the remaining packets out, until we get to the last
2155 * interval. Then those packets and their associated overhead are just added
2156 * to the bandwidth used.
2158 static int xhci_check_bw_table(struct xhci_hcd *xhci,
2159 struct xhci_virt_device *virt_dev,
2160 int old_active_eps)
2162 unsigned int bw_reserved;
2163 unsigned int max_bandwidth;
2164 unsigned int bw_used;
2165 unsigned int block_size;
2166 struct xhci_interval_bw_table *bw_table;
2167 unsigned int packet_size = 0;
2168 unsigned int overhead = 0;
2169 unsigned int packets_transmitted = 0;
2170 unsigned int packets_remaining = 0;
2171 unsigned int i;
2173 if (virt_dev->udev->speed == USB_SPEED_SUPER)
2174 return xhci_check_ss_bw(xhci, virt_dev);
2176 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2177 max_bandwidth = HS_BW_LIMIT;
2178 /* Convert percent of bus BW reserved to blocks reserved */
2179 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2180 } else {
2181 max_bandwidth = FS_BW_LIMIT;
2182 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2185 bw_table = virt_dev->bw_table;
2186 /* We need to translate the max packet size and max ESIT payloads into
2187 * the units the hardware uses.
2189 block_size = xhci_get_block_size(virt_dev->udev);
2191 /* If we are manipulating a LS/FS device under a HS hub, double check
2192 * that the HS bus has enough bandwidth if we are activing a new TT.
2194 if (virt_dev->tt_info) {
2195 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2196 "Recalculating BW for rootport %u",
2197 virt_dev->real_port);
2198 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2199 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2200 "newly activated TT.\n");
2201 return -ENOMEM;
2203 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2204 "Recalculating BW for TT slot %u port %u",
2205 virt_dev->tt_info->slot_id,
2206 virt_dev->tt_info->ttport);
2207 } else {
2208 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2209 "Recalculating BW for rootport %u",
2210 virt_dev->real_port);
2213 /* Add in how much bandwidth will be used for interval zero, or the
2214 * rounded max ESIT payload + number of packets * largest overhead.
2216 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2217 bw_table->interval_bw[0].num_packets *
2218 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2220 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2221 unsigned int bw_added;
2222 unsigned int largest_mps;
2223 unsigned int interval_overhead;
2226 * How many packets could we transmit in this interval?
2227 * If packets didn't fit in the previous interval, we will need
2228 * to transmit that many packets twice within this interval.
2230 packets_remaining = 2 * packets_remaining +
2231 bw_table->interval_bw[i].num_packets;
2233 /* Find the largest max packet size of this or the previous
2234 * interval.
2236 if (list_empty(&bw_table->interval_bw[i].endpoints))
2237 largest_mps = 0;
2238 else {
2239 struct xhci_virt_ep *virt_ep;
2240 struct list_head *ep_entry;
2242 ep_entry = bw_table->interval_bw[i].endpoints.next;
2243 virt_ep = list_entry(ep_entry,
2244 struct xhci_virt_ep, bw_endpoint_list);
2245 /* Convert to blocks, rounding up */
2246 largest_mps = DIV_ROUND_UP(
2247 virt_ep->bw_info.max_packet_size,
2248 block_size);
2250 if (largest_mps > packet_size)
2251 packet_size = largest_mps;
2253 /* Use the larger overhead of this or the previous interval. */
2254 interval_overhead = xhci_get_largest_overhead(
2255 &bw_table->interval_bw[i]);
2256 if (interval_overhead > overhead)
2257 overhead = interval_overhead;
2259 /* How many packets can we evenly distribute across
2260 * (1 << (i + 1)) possible scheduling opportunities?
2262 packets_transmitted = packets_remaining >> (i + 1);
2264 /* Add in the bandwidth used for those scheduled packets */
2265 bw_added = packets_transmitted * (overhead + packet_size);
2267 /* How many packets do we have remaining to transmit? */
2268 packets_remaining = packets_remaining % (1 << (i + 1));
2270 /* What largest max packet size should those packets have? */
2271 /* If we've transmitted all packets, don't carry over the
2272 * largest packet size.
2274 if (packets_remaining == 0) {
2275 packet_size = 0;
2276 overhead = 0;
2277 } else if (packets_transmitted > 0) {
2278 /* Otherwise if we do have remaining packets, and we've
2279 * scheduled some packets in this interval, take the
2280 * largest max packet size from endpoints with this
2281 * interval.
2283 packet_size = largest_mps;
2284 overhead = interval_overhead;
2286 /* Otherwise carry over packet_size and overhead from the last
2287 * time we had a remainder.
2289 bw_used += bw_added;
2290 if (bw_used > max_bandwidth) {
2291 xhci_warn(xhci, "Not enough bandwidth. "
2292 "Proposed: %u, Max: %u\n",
2293 bw_used, max_bandwidth);
2294 return -ENOMEM;
2298 * Ok, we know we have some packets left over after even-handedly
2299 * scheduling interval 15. We don't know which microframes they will
2300 * fit into, so we over-schedule and say they will be scheduled every
2301 * microframe.
2303 if (packets_remaining > 0)
2304 bw_used += overhead + packet_size;
2306 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2307 unsigned int port_index = virt_dev->real_port - 1;
2309 /* OK, we're manipulating a HS device attached to a
2310 * root port bandwidth domain. Include the number of active TTs
2311 * in the bandwidth used.
2313 bw_used += TT_HS_OVERHEAD *
2314 xhci->rh_bw[port_index].num_active_tts;
2317 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2318 "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2319 "Available: %u " "percent",
2320 bw_used, max_bandwidth, bw_reserved,
2321 (max_bandwidth - bw_used - bw_reserved) * 100 /
2322 max_bandwidth);
2324 bw_used += bw_reserved;
2325 if (bw_used > max_bandwidth) {
2326 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2327 bw_used, max_bandwidth);
2328 return -ENOMEM;
2331 bw_table->bw_used = bw_used;
2332 return 0;
2335 static bool xhci_is_async_ep(unsigned int ep_type)
2337 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2338 ep_type != ISOC_IN_EP &&
2339 ep_type != INT_IN_EP);
2342 static bool xhci_is_sync_in_ep(unsigned int ep_type)
2344 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
2347 static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2349 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2351 if (ep_bw->ep_interval == 0)
2352 return SS_OVERHEAD_BURST +
2353 (ep_bw->mult * ep_bw->num_packets *
2354 (SS_OVERHEAD + mps));
2355 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2356 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2357 1 << ep_bw->ep_interval);
2361 void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2362 struct xhci_bw_info *ep_bw,
2363 struct xhci_interval_bw_table *bw_table,
2364 struct usb_device *udev,
2365 struct xhci_virt_ep *virt_ep,
2366 struct xhci_tt_bw_info *tt_info)
2368 struct xhci_interval_bw *interval_bw;
2369 int normalized_interval;
2371 if (xhci_is_async_ep(ep_bw->type))
2372 return;
2374 if (udev->speed == USB_SPEED_SUPER) {
2375 if (xhci_is_sync_in_ep(ep_bw->type))
2376 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2377 xhci_get_ss_bw_consumed(ep_bw);
2378 else
2379 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2380 xhci_get_ss_bw_consumed(ep_bw);
2381 return;
2384 /* SuperSpeed endpoints never get added to intervals in the table, so
2385 * this check is only valid for HS/FS/LS devices.
2387 if (list_empty(&virt_ep->bw_endpoint_list))
2388 return;
2389 /* For LS/FS devices, we need to translate the interval expressed in
2390 * microframes to frames.
2392 if (udev->speed == USB_SPEED_HIGH)
2393 normalized_interval = ep_bw->ep_interval;
2394 else
2395 normalized_interval = ep_bw->ep_interval - 3;
2397 if (normalized_interval == 0)
2398 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2399 interval_bw = &bw_table->interval_bw[normalized_interval];
2400 interval_bw->num_packets -= ep_bw->num_packets;
2401 switch (udev->speed) {
2402 case USB_SPEED_LOW:
2403 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2404 break;
2405 case USB_SPEED_FULL:
2406 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2407 break;
2408 case USB_SPEED_HIGH:
2409 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2410 break;
2411 case USB_SPEED_SUPER:
2412 case USB_SPEED_UNKNOWN:
2413 case USB_SPEED_WIRELESS:
2414 /* Should never happen because only LS/FS/HS endpoints will get
2415 * added to the endpoint list.
2417 return;
2419 if (tt_info)
2420 tt_info->active_eps -= 1;
2421 list_del_init(&virt_ep->bw_endpoint_list);
2424 static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2425 struct xhci_bw_info *ep_bw,
2426 struct xhci_interval_bw_table *bw_table,
2427 struct usb_device *udev,
2428 struct xhci_virt_ep *virt_ep,
2429 struct xhci_tt_bw_info *tt_info)
2431 struct xhci_interval_bw *interval_bw;
2432 struct xhci_virt_ep *smaller_ep;
2433 int normalized_interval;
2435 if (xhci_is_async_ep(ep_bw->type))
2436 return;
2438 if (udev->speed == USB_SPEED_SUPER) {
2439 if (xhci_is_sync_in_ep(ep_bw->type))
2440 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2441 xhci_get_ss_bw_consumed(ep_bw);
2442 else
2443 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2444 xhci_get_ss_bw_consumed(ep_bw);
2445 return;
2448 /* For LS/FS devices, we need to translate the interval expressed in
2449 * microframes to frames.
2451 if (udev->speed == USB_SPEED_HIGH)
2452 normalized_interval = ep_bw->ep_interval;
2453 else
2454 normalized_interval = ep_bw->ep_interval - 3;
2456 if (normalized_interval == 0)
2457 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2458 interval_bw = &bw_table->interval_bw[normalized_interval];
2459 interval_bw->num_packets += ep_bw->num_packets;
2460 switch (udev->speed) {
2461 case USB_SPEED_LOW:
2462 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2463 break;
2464 case USB_SPEED_FULL:
2465 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2466 break;
2467 case USB_SPEED_HIGH:
2468 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2469 break;
2470 case USB_SPEED_SUPER:
2471 case USB_SPEED_UNKNOWN:
2472 case USB_SPEED_WIRELESS:
2473 /* Should never happen because only LS/FS/HS endpoints will get
2474 * added to the endpoint list.
2476 return;
2479 if (tt_info)
2480 tt_info->active_eps += 1;
2481 /* Insert the endpoint into the list, largest max packet size first. */
2482 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2483 bw_endpoint_list) {
2484 if (ep_bw->max_packet_size >=
2485 smaller_ep->bw_info.max_packet_size) {
2486 /* Add the new ep before the smaller endpoint */
2487 list_add_tail(&virt_ep->bw_endpoint_list,
2488 &smaller_ep->bw_endpoint_list);
2489 return;
2492 /* Add the new endpoint at the end of the list. */
2493 list_add_tail(&virt_ep->bw_endpoint_list,
2494 &interval_bw->endpoints);
2497 void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2498 struct xhci_virt_device *virt_dev,
2499 int old_active_eps)
2501 struct xhci_root_port_bw_info *rh_bw_info;
2502 if (!virt_dev->tt_info)
2503 return;
2505 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2506 if (old_active_eps == 0 &&
2507 virt_dev->tt_info->active_eps != 0) {
2508 rh_bw_info->num_active_tts += 1;
2509 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
2510 } else if (old_active_eps != 0 &&
2511 virt_dev->tt_info->active_eps == 0) {
2512 rh_bw_info->num_active_tts -= 1;
2513 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
2517 static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2518 struct xhci_virt_device *virt_dev,
2519 struct xhci_container_ctx *in_ctx)
2521 struct xhci_bw_info ep_bw_info[31];
2522 int i;
2523 struct xhci_input_control_ctx *ctrl_ctx;
2524 int old_active_eps = 0;
2526 if (virt_dev->tt_info)
2527 old_active_eps = virt_dev->tt_info->active_eps;
2529 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2530 if (!ctrl_ctx) {
2531 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2532 __func__);
2533 return -ENOMEM;
2536 for (i = 0; i < 31; i++) {
2537 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2538 continue;
2540 /* Make a copy of the BW info in case we need to revert this */
2541 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2542 sizeof(ep_bw_info[i]));
2543 /* Drop the endpoint from the interval table if the endpoint is
2544 * being dropped or changed.
2546 if (EP_IS_DROPPED(ctrl_ctx, i))
2547 xhci_drop_ep_from_interval_table(xhci,
2548 &virt_dev->eps[i].bw_info,
2549 virt_dev->bw_table,
2550 virt_dev->udev,
2551 &virt_dev->eps[i],
2552 virt_dev->tt_info);
2554 /* Overwrite the information stored in the endpoints' bw_info */
2555 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2556 for (i = 0; i < 31; i++) {
2557 /* Add any changed or added endpoints to the interval table */
2558 if (EP_IS_ADDED(ctrl_ctx, i))
2559 xhci_add_ep_to_interval_table(xhci,
2560 &virt_dev->eps[i].bw_info,
2561 virt_dev->bw_table,
2562 virt_dev->udev,
2563 &virt_dev->eps[i],
2564 virt_dev->tt_info);
2567 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2568 /* Ok, this fits in the bandwidth we have.
2569 * Update the number of active TTs.
2571 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2572 return 0;
2575 /* We don't have enough bandwidth for this, revert the stored info. */
2576 for (i = 0; i < 31; i++) {
2577 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2578 continue;
2580 /* Drop the new copies of any added or changed endpoints from
2581 * the interval table.
2583 if (EP_IS_ADDED(ctrl_ctx, i)) {
2584 xhci_drop_ep_from_interval_table(xhci,
2585 &virt_dev->eps[i].bw_info,
2586 virt_dev->bw_table,
2587 virt_dev->udev,
2588 &virt_dev->eps[i],
2589 virt_dev->tt_info);
2591 /* Revert the endpoint back to its old information */
2592 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2593 sizeof(ep_bw_info[i]));
2594 /* Add any changed or dropped endpoints back into the table */
2595 if (EP_IS_DROPPED(ctrl_ctx, i))
2596 xhci_add_ep_to_interval_table(xhci,
2597 &virt_dev->eps[i].bw_info,
2598 virt_dev->bw_table,
2599 virt_dev->udev,
2600 &virt_dev->eps[i],
2601 virt_dev->tt_info);
2603 return -ENOMEM;
2607 /* Issue a configure endpoint command or evaluate context command
2608 * and wait for it to finish.
2610 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
2611 struct usb_device *udev,
2612 struct xhci_command *command,
2613 bool ctx_change, bool must_succeed)
2615 int ret;
2616 int timeleft;
2617 unsigned long flags;
2618 struct xhci_container_ctx *in_ctx;
2619 struct xhci_input_control_ctx *ctrl_ctx;
2620 struct completion *cmd_completion;
2621 u32 *cmd_status;
2622 struct xhci_virt_device *virt_dev;
2623 union xhci_trb *cmd_trb;
2625 spin_lock_irqsave(&xhci->lock, flags);
2626 virt_dev = xhci->devs[udev->slot_id];
2628 if (command)
2629 in_ctx = command->in_ctx;
2630 else
2631 in_ctx = virt_dev->in_ctx;
2632 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2633 if (!ctrl_ctx) {
2634 spin_unlock_irqrestore(&xhci->lock, flags);
2635 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2636 __func__);
2637 return -ENOMEM;
2640 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
2641 xhci_reserve_host_resources(xhci, ctrl_ctx)) {
2642 spin_unlock_irqrestore(&xhci->lock, flags);
2643 xhci_warn(xhci, "Not enough host resources, "
2644 "active endpoint contexts = %u\n",
2645 xhci->num_active_eps);
2646 return -ENOMEM;
2648 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
2649 xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
2650 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2651 xhci_free_host_resources(xhci, ctrl_ctx);
2652 spin_unlock_irqrestore(&xhci->lock, flags);
2653 xhci_warn(xhci, "Not enough bandwidth\n");
2654 return -ENOMEM;
2657 if (command) {
2658 cmd_completion = command->completion;
2659 cmd_status = &command->status;
2660 command->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
2661 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
2662 } else {
2663 cmd_completion = &virt_dev->cmd_completion;
2664 cmd_status = &virt_dev->cmd_status;
2666 init_completion(cmd_completion);
2668 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
2669 if (!ctx_change)
2670 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
2671 udev->slot_id, must_succeed);
2672 else
2673 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
2674 udev->slot_id, must_succeed);
2675 if (ret < 0) {
2676 if (command)
2677 list_del(&command->cmd_list);
2678 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2679 xhci_free_host_resources(xhci, ctrl_ctx);
2680 spin_unlock_irqrestore(&xhci->lock, flags);
2681 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2682 "FIXME allocate a new ring segment");
2683 return -ENOMEM;
2685 xhci_ring_cmd_db(xhci);
2686 spin_unlock_irqrestore(&xhci->lock, flags);
2688 /* Wait for the configure endpoint command to complete */
2689 timeleft = wait_for_completion_interruptible_timeout(
2690 cmd_completion,
2691 XHCI_CMD_DEFAULT_TIMEOUT);
2692 if (timeleft <= 0) {
2693 xhci_warn(xhci, "%s while waiting for %s command\n",
2694 timeleft == 0 ? "Timeout" : "Signal",
2695 ctx_change == 0 ?
2696 "configure endpoint" :
2697 "evaluate context");
2698 /* cancel the configure endpoint command */
2699 ret = xhci_cancel_cmd(xhci, command, cmd_trb);
2700 if (ret < 0)
2701 return ret;
2702 return -ETIME;
2705 if (!ctx_change)
2706 ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
2707 else
2708 ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
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.
2715 if (ret)
2716 xhci_free_host_resources(xhci, ctrl_ctx);
2717 else
2718 xhci_finish_resource_reservation(xhci, ctrl_ctx);
2719 spin_unlock_irqrestore(&xhci->lock, flags);
2721 return ret;
2724 /* Called after one or more calls to xhci_add_endpoint() or
2725 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2726 * to call xhci_reset_bandwidth().
2728 * Since we are in the middle of changing either configuration or
2729 * installing a new alt setting, the USB core won't allow URBs to be
2730 * enqueued for any endpoint on the old config or interface. Nothing
2731 * else should be touching the xhci->devs[slot_id] structure, so we
2732 * don't need to take the xhci->lock for manipulating that.
2734 int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2736 int i;
2737 int ret = 0;
2738 struct xhci_hcd *xhci;
2739 struct xhci_virt_device *virt_dev;
2740 struct xhci_input_control_ctx *ctrl_ctx;
2741 struct xhci_slot_ctx *slot_ctx;
2743 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
2744 if (ret <= 0)
2745 return ret;
2746 xhci = hcd_to_xhci(hcd);
2747 if (xhci->xhc_state & XHCI_STATE_DYING)
2748 return -ENODEV;
2750 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
2751 virt_dev = xhci->devs[udev->slot_id];
2753 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
2754 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2755 if (!ctrl_ctx) {
2756 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2757 __func__);
2758 return -ENOMEM;
2760 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2761 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2762 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
2764 /* Don't issue the command if there's no endpoints to update. */
2765 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
2766 ctrl_ctx->drop_flags == 0)
2767 return 0;
2769 xhci_dbg(xhci, "New Input Control Context:\n");
2770 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2771 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
2772 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
2774 ret = xhci_configure_endpoint(xhci, udev, NULL,
2775 false, false);
2776 if (ret) {
2777 /* Callee should call reset_bandwidth() */
2778 return ret;
2781 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
2782 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
2783 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
2785 /* Free any rings that were dropped, but not changed. */
2786 for (i = 1; i < 31; ++i) {
2787 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
2788 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
2789 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2791 xhci_zero_in_ctx(xhci, virt_dev);
2793 * Install any rings for completely new endpoints or changed endpoints,
2794 * and free or cache any old rings from changed endpoints.
2796 for (i = 1; i < 31; ++i) {
2797 if (!virt_dev->eps[i].new_ring)
2798 continue;
2799 /* Only cache or free the old ring if it exists.
2800 * It may not if this is the first add of an endpoint.
2802 if (virt_dev->eps[i].ring) {
2803 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2805 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2806 virt_dev->eps[i].new_ring = NULL;
2809 return ret;
2812 void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2814 struct xhci_hcd *xhci;
2815 struct xhci_virt_device *virt_dev;
2816 int i, ret;
2818 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
2819 if (ret <= 0)
2820 return;
2821 xhci = hcd_to_xhci(hcd);
2823 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
2824 virt_dev = xhci->devs[udev->slot_id];
2825 /* Free any rings allocated for added endpoints */
2826 for (i = 0; i < 31; ++i) {
2827 if (virt_dev->eps[i].new_ring) {
2828 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2829 virt_dev->eps[i].new_ring = NULL;
2832 xhci_zero_in_ctx(xhci, virt_dev);
2835 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
2836 struct xhci_container_ctx *in_ctx,
2837 struct xhci_container_ctx *out_ctx,
2838 struct xhci_input_control_ctx *ctrl_ctx,
2839 u32 add_flags, u32 drop_flags)
2841 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2842 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
2843 xhci_slot_copy(xhci, in_ctx, out_ctx);
2844 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2846 xhci_dbg(xhci, "Input Context:\n");
2847 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
2850 static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
2851 unsigned int slot_id, unsigned int ep_index,
2852 struct xhci_dequeue_state *deq_state)
2854 struct xhci_input_control_ctx *ctrl_ctx;
2855 struct xhci_container_ctx *in_ctx;
2856 struct xhci_ep_ctx *ep_ctx;
2857 u32 added_ctxs;
2858 dma_addr_t addr;
2860 in_ctx = xhci->devs[slot_id]->in_ctx;
2861 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2862 if (!ctrl_ctx) {
2863 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2864 __func__);
2865 return;
2868 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2869 xhci->devs[slot_id]->out_ctx, ep_index);
2870 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2871 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2872 deq_state->new_deq_ptr);
2873 if (addr == 0) {
2874 xhci_warn(xhci, "WARN Cannot submit config ep after "
2875 "reset ep command\n");
2876 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2877 deq_state->new_deq_seg,
2878 deq_state->new_deq_ptr);
2879 return;
2881 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
2883 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
2884 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
2885 xhci->devs[slot_id]->out_ctx, ctrl_ctx,
2886 added_ctxs, added_ctxs);
2889 void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
2890 struct usb_device *udev, unsigned int ep_index)
2892 struct xhci_dequeue_state deq_state;
2893 struct xhci_virt_ep *ep;
2895 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2896 "Cleaning up stalled endpoint ring");
2897 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
2898 /* We need to move the HW's dequeue pointer past this TD,
2899 * or it will attempt to resend it on the next doorbell ring.
2901 xhci_find_new_dequeue_state(xhci, udev->slot_id,
2902 ep_index, ep->stopped_stream, ep->stopped_td,
2903 &deq_state);
2905 /* HW with the reset endpoint quirk will use the saved dequeue state to
2906 * issue a configure endpoint command later.
2908 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
2909 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2910 "Queueing new dequeue state");
2911 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
2912 ep_index, ep->stopped_stream, &deq_state);
2913 } else {
2914 /* Better hope no one uses the input context between now and the
2915 * reset endpoint completion!
2916 * XXX: No idea how this hardware will react when stream rings
2917 * are enabled.
2919 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2920 "Setting up input context for "
2921 "configure endpoint command");
2922 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2923 ep_index, &deq_state);
2927 /* Called when clearing halted device. The core should have sent the control
2928 * message to clear the device halt condition. The host side of the halt should
2929 * already be cleared with a reset endpoint command issued when the STALL tx
2930 * event was received.
2932 * Context: in_interrupt
2935 void xhci_endpoint_reset(struct usb_hcd *hcd,
2936 struct usb_host_endpoint *ep)
2938 struct xhci_hcd *xhci;
2940 xhci = hcd_to_xhci(hcd);
2943 * We might need to implement the config ep cmd in xhci 4.8.1 note:
2944 * The Reset Endpoint Command may only be issued to endpoints in the
2945 * Halted state. If software wishes reset the Data Toggle or Sequence
2946 * Number of an endpoint that isn't in the Halted state, then software
2947 * may issue a Configure Endpoint Command with the Drop and Add bits set
2948 * for the target endpoint. that is in the Stopped state.
2951 /* For now just print debug to follow the situation */
2952 xhci_dbg(xhci, "Endpoint 0x%x ep reset callback called\n",
2953 ep->desc.bEndpointAddress);
2956 static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2957 struct usb_device *udev, struct usb_host_endpoint *ep,
2958 unsigned int slot_id)
2960 int ret;
2961 unsigned int ep_index;
2962 unsigned int ep_state;
2964 if (!ep)
2965 return -EINVAL;
2966 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
2967 if (ret <= 0)
2968 return -EINVAL;
2969 if (ep->ss_ep_comp.bmAttributes == 0) {
2970 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2971 " descriptor for ep 0x%x does not support streams\n",
2972 ep->desc.bEndpointAddress);
2973 return -EINVAL;
2976 ep_index = xhci_get_endpoint_index(&ep->desc);
2977 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
2978 if (ep_state & EP_HAS_STREAMS ||
2979 ep_state & EP_GETTING_STREAMS) {
2980 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
2981 "already has streams set up.\n",
2982 ep->desc.bEndpointAddress);
2983 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
2984 "dynamic stream context array reallocation.\n");
2985 return -EINVAL;
2987 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
2988 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
2989 "endpoint 0x%x; URBs are pending.\n",
2990 ep->desc.bEndpointAddress);
2991 return -EINVAL;
2993 return 0;
2996 static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
2997 unsigned int *num_streams, unsigned int *num_stream_ctxs)
2999 unsigned int max_streams;
3001 /* The stream context array size must be a power of two */
3002 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
3004 * Find out how many primary stream array entries the host controller
3005 * supports. Later we may use secondary stream arrays (similar to 2nd
3006 * level page entries), but that's an optional feature for xHCI host
3007 * controllers. xHCs must support at least 4 stream IDs.
3009 max_streams = HCC_MAX_PSA(xhci->hcc_params);
3010 if (*num_stream_ctxs > max_streams) {
3011 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
3012 max_streams);
3013 *num_stream_ctxs = max_streams;
3014 *num_streams = max_streams;
3018 /* Returns an error code if one of the endpoint already has streams.
3019 * This does not change any data structures, it only checks and gathers
3020 * information.
3022 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
3023 struct usb_device *udev,
3024 struct usb_host_endpoint **eps, unsigned int num_eps,
3025 unsigned int *num_streams, u32 *changed_ep_bitmask)
3027 unsigned int max_streams;
3028 unsigned int endpoint_flag;
3029 int i;
3030 int ret;
3032 for (i = 0; i < num_eps; i++) {
3033 ret = xhci_check_streams_endpoint(xhci, udev,
3034 eps[i], udev->slot_id);
3035 if (ret < 0)
3036 return ret;
3038 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
3039 if (max_streams < (*num_streams - 1)) {
3040 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3041 eps[i]->desc.bEndpointAddress,
3042 max_streams);
3043 *num_streams = max_streams+1;
3046 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3047 if (*changed_ep_bitmask & endpoint_flag)
3048 return -EINVAL;
3049 *changed_ep_bitmask |= endpoint_flag;
3051 return 0;
3054 static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3055 struct usb_device *udev,
3056 struct usb_host_endpoint **eps, unsigned int num_eps)
3058 u32 changed_ep_bitmask = 0;
3059 unsigned int slot_id;
3060 unsigned int ep_index;
3061 unsigned int ep_state;
3062 int i;
3064 slot_id = udev->slot_id;
3065 if (!xhci->devs[slot_id])
3066 return 0;
3068 for (i = 0; i < num_eps; i++) {
3069 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3070 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3071 /* Are streams already being freed for the endpoint? */
3072 if (ep_state & EP_GETTING_NO_STREAMS) {
3073 xhci_warn(xhci, "WARN Can't disable streams for "
3074 "endpoint 0x%x, "
3075 "streams are being disabled already\n",
3076 eps[i]->desc.bEndpointAddress);
3077 return 0;
3079 /* Are there actually any streams to free? */
3080 if (!(ep_state & EP_HAS_STREAMS) &&
3081 !(ep_state & EP_GETTING_STREAMS)) {
3082 xhci_warn(xhci, "WARN Can't disable streams for "
3083 "endpoint 0x%x, "
3084 "streams are already disabled!\n",
3085 eps[i]->desc.bEndpointAddress);
3086 xhci_warn(xhci, "WARN xhci_free_streams() called "
3087 "with non-streams endpoint\n");
3088 return 0;
3090 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3092 return changed_ep_bitmask;
3096 * The USB device drivers use this function (though the HCD interface in USB
3097 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3098 * coordinate mass storage command queueing across multiple endpoints (basically
3099 * a stream ID == a task ID).
3101 * Setting up streams involves allocating the same size stream context array
3102 * for each endpoint and issuing a configure endpoint command for all endpoints.
3104 * Don't allow the call to succeed if one endpoint only supports one stream
3105 * (which means it doesn't support streams at all).
3107 * Drivers may get less stream IDs than they asked for, if the host controller
3108 * hardware or endpoints claim they can't support the number of requested
3109 * stream IDs.
3111 int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3112 struct usb_host_endpoint **eps, unsigned int num_eps,
3113 unsigned int num_streams, gfp_t mem_flags)
3115 int i, ret;
3116 struct xhci_hcd *xhci;
3117 struct xhci_virt_device *vdev;
3118 struct xhci_command *config_cmd;
3119 struct xhci_input_control_ctx *ctrl_ctx;
3120 unsigned int ep_index;
3121 unsigned int num_stream_ctxs;
3122 unsigned long flags;
3123 u32 changed_ep_bitmask = 0;
3125 if (!eps)
3126 return -EINVAL;
3128 /* Add one to the number of streams requested to account for
3129 * stream 0 that is reserved for xHCI usage.
3131 num_streams += 1;
3132 xhci = hcd_to_xhci(hcd);
3133 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3134 num_streams);
3136 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
3137 if (!config_cmd) {
3138 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
3139 return -ENOMEM;
3141 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
3142 if (!ctrl_ctx) {
3143 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3144 __func__);
3145 xhci_free_command(xhci, config_cmd);
3146 return -ENOMEM;
3149 /* Check to make sure all endpoints are not already configured for
3150 * streams. While we're at it, find the maximum number of streams that
3151 * all the endpoints will support and check for duplicate endpoints.
3153 spin_lock_irqsave(&xhci->lock, flags);
3154 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3155 num_eps, &num_streams, &changed_ep_bitmask);
3156 if (ret < 0) {
3157 xhci_free_command(xhci, config_cmd);
3158 spin_unlock_irqrestore(&xhci->lock, flags);
3159 return ret;
3161 if (num_streams <= 1) {
3162 xhci_warn(xhci, "WARN: endpoints can't handle "
3163 "more than one stream.\n");
3164 xhci_free_command(xhci, config_cmd);
3165 spin_unlock_irqrestore(&xhci->lock, flags);
3166 return -EINVAL;
3168 vdev = xhci->devs[udev->slot_id];
3169 /* Mark each endpoint as being in transition, so
3170 * xhci_urb_enqueue() will reject all URBs.
3172 for (i = 0; i < num_eps; i++) {
3173 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3174 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3176 spin_unlock_irqrestore(&xhci->lock, flags);
3178 /* Setup internal data structures and allocate HW data structures for
3179 * streams (but don't install the HW structures in the input context
3180 * until we're sure all memory allocation succeeded).
3182 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3183 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3184 num_stream_ctxs, num_streams);
3186 for (i = 0; i < num_eps; i++) {
3187 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3188 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3189 num_stream_ctxs,
3190 num_streams, mem_flags);
3191 if (!vdev->eps[ep_index].stream_info)
3192 goto cleanup;
3193 /* Set maxPstreams in endpoint context and update deq ptr to
3194 * point to stream context array. FIXME
3198 /* Set up the input context for a configure endpoint command. */
3199 for (i = 0; i < num_eps; i++) {
3200 struct xhci_ep_ctx *ep_ctx;
3202 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3203 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3205 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3206 vdev->out_ctx, ep_index);
3207 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3208 vdev->eps[ep_index].stream_info);
3210 /* Tell the HW to drop its old copy of the endpoint context info
3211 * and add the updated copy from the input context.
3213 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
3214 vdev->out_ctx, ctrl_ctx,
3215 changed_ep_bitmask, changed_ep_bitmask);
3217 /* Issue and wait for the configure endpoint command */
3218 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3219 false, false);
3221 /* xHC rejected the configure endpoint command for some reason, so we
3222 * leave the old ring intact and free our internal streams data
3223 * structure.
3225 if (ret < 0)
3226 goto cleanup;
3228 spin_lock_irqsave(&xhci->lock, flags);
3229 for (i = 0; i < num_eps; i++) {
3230 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3231 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3232 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3233 udev->slot_id, ep_index);
3234 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3236 xhci_free_command(xhci, config_cmd);
3237 spin_unlock_irqrestore(&xhci->lock, flags);
3239 /* Subtract 1 for stream 0, which drivers can't use */
3240 return num_streams - 1;
3242 cleanup:
3243 /* If it didn't work, free the streams! */
3244 for (i = 0; i < num_eps; i++) {
3245 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3246 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
3247 vdev->eps[ep_index].stream_info = NULL;
3248 /* FIXME Unset maxPstreams in endpoint context and
3249 * update deq ptr to point to normal string ring.
3251 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3252 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3253 xhci_endpoint_zero(xhci, vdev, eps[i]);
3255 xhci_free_command(xhci, config_cmd);
3256 return -ENOMEM;
3259 /* Transition the endpoint from using streams to being a "normal" endpoint
3260 * without streams.
3262 * Modify the endpoint context state, submit a configure endpoint command,
3263 * and free all endpoint rings for streams if that completes successfully.
3265 int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
3266 struct usb_host_endpoint **eps, unsigned int num_eps,
3267 gfp_t mem_flags)
3269 int i, ret;
3270 struct xhci_hcd *xhci;
3271 struct xhci_virt_device *vdev;
3272 struct xhci_command *command;
3273 struct xhci_input_control_ctx *ctrl_ctx;
3274 unsigned int ep_index;
3275 unsigned long flags;
3276 u32 changed_ep_bitmask;
3278 xhci = hcd_to_xhci(hcd);
3279 vdev = xhci->devs[udev->slot_id];
3281 /* Set up a configure endpoint command to remove the streams rings */
3282 spin_lock_irqsave(&xhci->lock, flags);
3283 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3284 udev, eps, num_eps);
3285 if (changed_ep_bitmask == 0) {
3286 spin_unlock_irqrestore(&xhci->lock, flags);
3287 return -EINVAL;
3290 /* Use the xhci_command structure from the first endpoint. We may have
3291 * allocated too many, but the driver may call xhci_free_streams() for
3292 * each endpoint it grouped into one call to xhci_alloc_streams().
3294 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3295 command = vdev->eps[ep_index].stream_info->free_streams_command;
3296 ctrl_ctx = xhci_get_input_control_ctx(xhci, command->in_ctx);
3297 if (!ctrl_ctx) {
3298 spin_unlock_irqrestore(&xhci->lock, flags);
3299 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3300 __func__);
3301 return -EINVAL;
3304 for (i = 0; i < num_eps; i++) {
3305 struct xhci_ep_ctx *ep_ctx;
3307 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3308 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3309 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3310 EP_GETTING_NO_STREAMS;
3312 xhci_endpoint_copy(xhci, command->in_ctx,
3313 vdev->out_ctx, ep_index);
3314 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
3315 &vdev->eps[ep_index]);
3317 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
3318 vdev->out_ctx, ctrl_ctx,
3319 changed_ep_bitmask, changed_ep_bitmask);
3320 spin_unlock_irqrestore(&xhci->lock, flags);
3322 /* Issue and wait for the configure endpoint command,
3323 * which must succeed.
3325 ret = xhci_configure_endpoint(xhci, udev, command,
3326 false, true);
3328 /* xHC rejected the configure endpoint command for some reason, so we
3329 * leave the streams rings intact.
3331 if (ret < 0)
3332 return ret;
3334 spin_lock_irqsave(&xhci->lock, flags);
3335 for (i = 0; i < num_eps; i++) {
3336 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3337 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
3338 vdev->eps[ep_index].stream_info = NULL;
3339 /* FIXME Unset maxPstreams in endpoint context and
3340 * update deq ptr to point to normal string ring.
3342 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3343 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3345 spin_unlock_irqrestore(&xhci->lock, flags);
3347 return 0;
3351 * Deletes endpoint resources for endpoints that were active before a Reset
3352 * Device command, or a Disable Slot command. The Reset Device command leaves
3353 * the control endpoint intact, whereas the Disable Slot command deletes it.
3355 * Must be called with xhci->lock held.
3357 void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3358 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3360 int i;
3361 unsigned int num_dropped_eps = 0;
3362 unsigned int drop_flags = 0;
3364 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3365 if (virt_dev->eps[i].ring) {
3366 drop_flags |= 1 << i;
3367 num_dropped_eps++;
3370 xhci->num_active_eps -= num_dropped_eps;
3371 if (num_dropped_eps)
3372 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3373 "Dropped %u ep ctxs, flags = 0x%x, "
3374 "%u now active.",
3375 num_dropped_eps, drop_flags,
3376 xhci->num_active_eps);
3380 * This submits a Reset Device Command, which will set the device state to 0,
3381 * set the device address to 0, and disable all the endpoints except the default
3382 * control endpoint. The USB core should come back and call
3383 * xhci_address_device(), and then re-set up the configuration. If this is
3384 * called because of a usb_reset_and_verify_device(), then the old alternate
3385 * settings will be re-installed through the normal bandwidth allocation
3386 * functions.
3388 * Wait for the Reset Device command to finish. Remove all structures
3389 * associated with the endpoints that were disabled. Clear the input device
3390 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
3392 * If the virt_dev to be reset does not exist or does not match the udev,
3393 * it means the device is lost, possibly due to the xHC restore error and
3394 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3395 * re-allocate the device.
3397 int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
3399 int ret, i;
3400 unsigned long flags;
3401 struct xhci_hcd *xhci;
3402 unsigned int slot_id;
3403 struct xhci_virt_device *virt_dev;
3404 struct xhci_command *reset_device_cmd;
3405 int timeleft;
3406 int last_freed_endpoint;
3407 struct xhci_slot_ctx *slot_ctx;
3408 int old_active_eps = 0;
3410 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
3411 if (ret <= 0)
3412 return ret;
3413 xhci = hcd_to_xhci(hcd);
3414 slot_id = udev->slot_id;
3415 virt_dev = xhci->devs[slot_id];
3416 if (!virt_dev) {
3417 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3418 "not exist. Re-allocate the device\n", slot_id);
3419 ret = xhci_alloc_dev(hcd, udev);
3420 if (ret == 1)
3421 return 0;
3422 else
3423 return -EINVAL;
3426 if (virt_dev->udev != udev) {
3427 /* If the virt_dev and the udev does not match, this virt_dev
3428 * may belong to another udev.
3429 * Re-allocate the device.
3431 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3432 "not match the udev. Re-allocate the device\n",
3433 slot_id);
3434 ret = xhci_alloc_dev(hcd, udev);
3435 if (ret == 1)
3436 return 0;
3437 else
3438 return -EINVAL;
3441 /* If device is not setup, there is no point in resetting it */
3442 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3443 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3444 SLOT_STATE_DISABLED)
3445 return 0;
3447 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3448 /* Allocate the command structure that holds the struct completion.
3449 * Assume we're in process context, since the normal device reset
3450 * process has to wait for the device anyway. Storage devices are
3451 * reset as part of error handling, so use GFP_NOIO instead of
3452 * GFP_KERNEL.
3454 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
3455 if (!reset_device_cmd) {
3456 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3457 return -ENOMEM;
3460 /* Attempt to submit the Reset Device command to the command ring */
3461 spin_lock_irqsave(&xhci->lock, flags);
3462 reset_device_cmd->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
3464 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
3465 ret = xhci_queue_reset_device(xhci, slot_id);
3466 if (ret) {
3467 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3468 list_del(&reset_device_cmd->cmd_list);
3469 spin_unlock_irqrestore(&xhci->lock, flags);
3470 goto command_cleanup;
3472 xhci_ring_cmd_db(xhci);
3473 spin_unlock_irqrestore(&xhci->lock, flags);
3475 /* Wait for the Reset Device command to finish */
3476 timeleft = wait_for_completion_interruptible_timeout(
3477 reset_device_cmd->completion,
3478 USB_CTRL_SET_TIMEOUT);
3479 if (timeleft <= 0) {
3480 xhci_warn(xhci, "%s while waiting for reset device command\n",
3481 timeleft == 0 ? "Timeout" : "Signal");
3482 spin_lock_irqsave(&xhci->lock, flags);
3483 /* The timeout might have raced with the event ring handler, so
3484 * only delete from the list if the item isn't poisoned.
3486 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
3487 list_del(&reset_device_cmd->cmd_list);
3488 spin_unlock_irqrestore(&xhci->lock, flags);
3489 ret = -ETIME;
3490 goto command_cleanup;
3493 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3494 * unless we tried to reset a slot ID that wasn't enabled,
3495 * or the device wasn't in the addressed or configured state.
3497 ret = reset_device_cmd->status;
3498 switch (ret) {
3499 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
3500 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
3501 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n",
3502 slot_id,
3503 xhci_get_slot_state(xhci, virt_dev->out_ctx));
3504 xhci_dbg(xhci, "Not freeing device rings.\n");
3505 /* Don't treat this as an error. May change my mind later. */
3506 ret = 0;
3507 goto command_cleanup;
3508 case COMP_SUCCESS:
3509 xhci_dbg(xhci, "Successful reset device command.\n");
3510 break;
3511 default:
3512 if (xhci_is_vendor_info_code(xhci, ret))
3513 break;
3514 xhci_warn(xhci, "Unknown completion code %u for "
3515 "reset device command.\n", ret);
3516 ret = -EINVAL;
3517 goto command_cleanup;
3520 /* Free up host controller endpoint resources */
3521 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3522 spin_lock_irqsave(&xhci->lock, flags);
3523 /* Don't delete the default control endpoint resources */
3524 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3525 spin_unlock_irqrestore(&xhci->lock, flags);
3528 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3529 last_freed_endpoint = 1;
3530 for (i = 1; i < 31; ++i) {
3531 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3533 if (ep->ep_state & EP_HAS_STREAMS) {
3534 xhci_free_stream_info(xhci, ep->stream_info);
3535 ep->stream_info = NULL;
3536 ep->ep_state &= ~EP_HAS_STREAMS;
3539 if (ep->ring) {
3540 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
3541 last_freed_endpoint = i;
3543 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3544 xhci_drop_ep_from_interval_table(xhci,
3545 &virt_dev->eps[i].bw_info,
3546 virt_dev->bw_table,
3547 udev,
3548 &virt_dev->eps[i],
3549 virt_dev->tt_info);
3550 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
3552 /* If necessary, update the number of active TTs on this root port */
3553 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3555 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
3556 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
3557 ret = 0;
3559 command_cleanup:
3560 xhci_free_command(xhci, reset_device_cmd);
3561 return ret;
3565 * At this point, the struct usb_device is about to go away, the device has
3566 * disconnected, and all traffic has been stopped and the endpoints have been
3567 * disabled. Free any HC data structures associated with that device.
3569 void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
3571 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3572 struct xhci_virt_device *virt_dev;
3573 unsigned long flags;
3574 u32 state;
3575 int i, ret;
3577 #ifndef CONFIG_USB_DEFAULT_PERSIST
3579 * We called pm_runtime_get_noresume when the device was attached.
3580 * Decrement the counter here to allow controller to runtime suspend
3581 * if no devices remain.
3583 if (xhci->quirks & XHCI_RESET_ON_RESUME)
3584 pm_runtime_put_noidle(hcd->self.controller);
3585 #endif
3587 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
3588 /* If the host is halted due to driver unload, we still need to free the
3589 * device.
3591 if (ret <= 0 && ret != -ENODEV)
3592 return;
3594 virt_dev = xhci->devs[udev->slot_id];
3596 /* Stop any wayward timer functions (which may grab the lock) */
3597 for (i = 0; i < 31; ++i) {
3598 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
3599 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3602 if (udev->usb2_hw_lpm_enabled) {
3603 xhci_set_usb2_hardware_lpm(hcd, udev, 0);
3604 udev->usb2_hw_lpm_enabled = 0;
3607 spin_lock_irqsave(&xhci->lock, flags);
3608 /* Don't disable the slot if the host controller is dead. */
3609 state = xhci_readl(xhci, &xhci->op_regs->status);
3610 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3611 (xhci->xhc_state & XHCI_STATE_HALTED)) {
3612 xhci_free_virt_device(xhci, udev->slot_id);
3613 spin_unlock_irqrestore(&xhci->lock, flags);
3614 return;
3617 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
3618 spin_unlock_irqrestore(&xhci->lock, flags);
3619 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3620 return;
3622 xhci_ring_cmd_db(xhci);
3623 spin_unlock_irqrestore(&xhci->lock, flags);
3625 * Event command completion handler will free any data structures
3626 * associated with the slot. XXX Can free sleep?
3631 * Checks if we have enough host controller resources for the default control
3632 * endpoint.
3634 * Must be called with xhci->lock held.
3636 static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3638 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
3639 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3640 "Not enough ep ctxs: "
3641 "%u active, need to add 1, limit is %u.",
3642 xhci->num_active_eps, xhci->limit_active_eps);
3643 return -ENOMEM;
3645 xhci->num_active_eps += 1;
3646 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3647 "Adding 1 ep ctx, %u now active.",
3648 xhci->num_active_eps);
3649 return 0;
3654 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3655 * timed out, or allocating memory failed. Returns 1 on success.
3657 int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3659 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3660 unsigned long flags;
3661 int timeleft;
3662 int ret;
3663 union xhci_trb *cmd_trb;
3665 spin_lock_irqsave(&xhci->lock, flags);
3666 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
3667 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
3668 if (ret) {
3669 spin_unlock_irqrestore(&xhci->lock, flags);
3670 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3671 return 0;
3673 xhci_ring_cmd_db(xhci);
3674 spin_unlock_irqrestore(&xhci->lock, flags);
3676 /* XXX: how much time for xHC slot assignment? */
3677 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
3678 XHCI_CMD_DEFAULT_TIMEOUT);
3679 if (timeleft <= 0) {
3680 xhci_warn(xhci, "%s while waiting for a slot\n",
3681 timeleft == 0 ? "Timeout" : "Signal");
3682 /* cancel the enable slot request */
3683 return xhci_cancel_cmd(xhci, NULL, cmd_trb);
3686 if (!xhci->slot_id) {
3687 xhci_err(xhci, "Error while assigning device slot ID\n");
3688 return 0;
3691 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3692 spin_lock_irqsave(&xhci->lock, flags);
3693 ret = xhci_reserve_host_control_ep_resources(xhci);
3694 if (ret) {
3695 spin_unlock_irqrestore(&xhci->lock, flags);
3696 xhci_warn(xhci, "Not enough host resources, "
3697 "active endpoint contexts = %u\n",
3698 xhci->num_active_eps);
3699 goto disable_slot;
3701 spin_unlock_irqrestore(&xhci->lock, flags);
3703 /* Use GFP_NOIO, since this function can be called from
3704 * xhci_discover_or_reset_device(), which may be called as part of
3705 * mass storage driver error handling.
3707 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
3708 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
3709 goto disable_slot;
3711 udev->slot_id = xhci->slot_id;
3713 #ifndef CONFIG_USB_DEFAULT_PERSIST
3715 * If resetting upon resume, we can't put the controller into runtime
3716 * suspend if there is a device attached.
3718 if (xhci->quirks & XHCI_RESET_ON_RESUME)
3719 pm_runtime_get_noresume(hcd->self.controller);
3720 #endif
3722 /* Is this a LS or FS device under a HS hub? */
3723 /* Hub or peripherial? */
3724 return 1;
3726 disable_slot:
3727 /* Disable slot, if we can do it without mem alloc */
3728 spin_lock_irqsave(&xhci->lock, flags);
3729 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
3730 xhci_ring_cmd_db(xhci);
3731 spin_unlock_irqrestore(&xhci->lock, flags);
3732 return 0;
3736 * Issue an Address Device command (which will issue a SetAddress request to
3737 * the device).
3738 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
3739 * we should only issue and wait on one address command at the same time.
3741 * We add one to the device address issued by the hardware because the USB core
3742 * uses address 1 for the root hubs (even though they're not really devices).
3744 int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
3746 unsigned long flags;
3747 int timeleft;
3748 struct xhci_virt_device *virt_dev;
3749 int ret = 0;
3750 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3751 struct xhci_slot_ctx *slot_ctx;
3752 struct xhci_input_control_ctx *ctrl_ctx;
3753 u64 temp_64;
3754 union xhci_trb *cmd_trb;
3756 if (!udev->slot_id) {
3757 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3758 "Bad Slot ID %d", udev->slot_id);
3759 return -EINVAL;
3762 virt_dev = xhci->devs[udev->slot_id];
3764 if (WARN_ON(!virt_dev)) {
3766 * In plug/unplug torture test with an NEC controller,
3767 * a zero-dereference was observed once due to virt_dev = 0.
3768 * Print useful debug rather than crash if it is observed again!
3770 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3771 udev->slot_id);
3772 return -EINVAL;
3775 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
3776 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
3777 if (!ctrl_ctx) {
3778 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3779 __func__);
3780 return -EINVAL;
3783 * If this is the first Set Address since device plug-in or
3784 * virt_device realloaction after a resume with an xHCI power loss,
3785 * then set up the slot context.
3787 if (!slot_ctx->dev_info)
3788 xhci_setup_addressable_virt_dev(xhci, udev);
3789 /* Otherwise, update the control endpoint ring enqueue pointer. */
3790 else
3791 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
3792 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3793 ctrl_ctx->drop_flags = 0;
3795 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
3796 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
3797 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
3798 slot_ctx->dev_info >> 27);
3800 spin_lock_irqsave(&xhci->lock, flags);
3801 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
3802 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
3803 udev->slot_id);
3804 if (ret) {
3805 spin_unlock_irqrestore(&xhci->lock, flags);
3806 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3807 "FIXME: allocate a command ring segment");
3808 return ret;
3810 xhci_ring_cmd_db(xhci);
3811 spin_unlock_irqrestore(&xhci->lock, flags);
3813 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
3814 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
3815 XHCI_CMD_DEFAULT_TIMEOUT);
3816 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3817 * the SetAddress() "recovery interval" required by USB and aborting the
3818 * command on a timeout.
3820 if (timeleft <= 0) {
3821 xhci_warn(xhci, "%s while waiting for address device command\n",
3822 timeleft == 0 ? "Timeout" : "Signal");
3823 /* cancel the address device command */
3824 ret = xhci_cancel_cmd(xhci, NULL, cmd_trb);
3825 if (ret < 0)
3826 return ret;
3827 return -ETIME;
3830 switch (virt_dev->cmd_status) {
3831 case COMP_CTX_STATE:
3832 case COMP_EBADSLT:
3833 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
3834 udev->slot_id);
3835 ret = -EINVAL;
3836 break;
3837 case COMP_TX_ERR:
3838 dev_warn(&udev->dev, "Device not responding to set address.\n");
3839 ret = -EPROTO;
3840 break;
3841 case COMP_DEV_ERR:
3842 dev_warn(&udev->dev, "ERROR: Incompatible device for address "
3843 "device command.\n");
3844 ret = -ENODEV;
3845 break;
3846 case COMP_SUCCESS:
3847 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3848 "Successful Address Device command");
3849 break;
3850 default:
3851 xhci_err(xhci, "ERROR: unexpected command completion "
3852 "code 0x%x.\n", virt_dev->cmd_status);
3853 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
3854 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
3855 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
3856 ret = -EINVAL;
3857 break;
3859 if (ret) {
3860 return ret;
3862 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
3863 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3864 "Op regs DCBAA ptr = %#016llx", temp_64);
3865 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3866 "Slot ID %d dcbaa entry @%p = %#016llx",
3867 udev->slot_id,
3868 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3869 (unsigned long long)
3870 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
3871 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3872 "Output Context DMA address = %#08llx",
3873 (unsigned long long)virt_dev->out_ctx->dma);
3874 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
3875 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
3876 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
3877 slot_ctx->dev_info >> 27);
3878 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
3879 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
3881 * USB core uses address 1 for the roothubs, so we add one to the
3882 * address given back to us by the HC.
3884 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3885 trace_xhci_address_ctx(xhci, virt_dev->out_ctx,
3886 slot_ctx->dev_info >> 27);
3887 /* Use kernel assigned address for devices; store xHC assigned
3888 * address locally. */
3889 virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
3890 + 1;
3891 /* Zero the input context control for later use */
3892 ctrl_ctx->add_flags = 0;
3893 ctrl_ctx->drop_flags = 0;
3895 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3896 "Internal device address = %d", virt_dev->address);
3898 return 0;
3902 * Transfer the port index into real index in the HW port status
3903 * registers. Caculate offset between the port's PORTSC register
3904 * and port status base. Divide the number of per port register
3905 * to get the real index. The raw port number bases 1.
3907 int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
3909 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3910 __le32 __iomem *base_addr = &xhci->op_regs->port_status_base;
3911 __le32 __iomem *addr;
3912 int raw_port;
3914 if (hcd->speed != HCD_USB3)
3915 addr = xhci->usb2_ports[port1 - 1];
3916 else
3917 addr = xhci->usb3_ports[port1 - 1];
3919 raw_port = (addr - base_addr)/NUM_PORT_REGS + 1;
3920 return raw_port;
3924 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
3925 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
3927 static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
3928 struct usb_device *udev, u16 max_exit_latency)
3930 struct xhci_virt_device *virt_dev;
3931 struct xhci_command *command;
3932 struct xhci_input_control_ctx *ctrl_ctx;
3933 struct xhci_slot_ctx *slot_ctx;
3934 unsigned long flags;
3935 int ret;
3937 spin_lock_irqsave(&xhci->lock, flags);
3939 virt_dev = xhci->devs[udev->slot_id];
3942 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
3943 * xHC was re-initialized. Exit latency will be set later after
3944 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
3947 if (!virt_dev || max_exit_latency == virt_dev->current_mel) {
3948 spin_unlock_irqrestore(&xhci->lock, flags);
3949 return 0;
3952 /* Attempt to issue an Evaluate Context command to change the MEL. */
3953 command = xhci->lpm_command;
3954 ctrl_ctx = xhci_get_input_control_ctx(xhci, command->in_ctx);
3955 if (!ctrl_ctx) {
3956 spin_unlock_irqrestore(&xhci->lock, flags);
3957 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3958 __func__);
3959 return -ENOMEM;
3962 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
3963 spin_unlock_irqrestore(&xhci->lock, flags);
3965 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
3966 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
3967 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
3968 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
3970 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
3971 "Set up evaluate context for LPM MEL change.");
3972 xhci_dbg(xhci, "Slot %u Input Context:\n", udev->slot_id);
3973 xhci_dbg_ctx(xhci, command->in_ctx, 0);
3975 /* Issue and wait for the evaluate context command. */
3976 ret = xhci_configure_endpoint(xhci, udev, command,
3977 true, true);
3978 xhci_dbg(xhci, "Slot %u Output Context:\n", udev->slot_id);
3979 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 0);
3981 if (!ret) {
3982 spin_lock_irqsave(&xhci->lock, flags);
3983 virt_dev->current_mel = max_exit_latency;
3984 spin_unlock_irqrestore(&xhci->lock, flags);
3986 return ret;
3989 #ifdef CONFIG_PM_RUNTIME
3991 /* BESL to HIRD Encoding array for USB2 LPM */
3992 static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
3993 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
3995 /* Calculate HIRD/BESL for USB2 PORTPMSC*/
3996 static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
3997 struct usb_device *udev)
3999 int u2del, besl, besl_host;
4000 int besl_device = 0;
4001 u32 field;
4003 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
4004 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4006 if (field & USB_BESL_SUPPORT) {
4007 for (besl_host = 0; besl_host < 16; besl_host++) {
4008 if (xhci_besl_encoding[besl_host] >= u2del)
4009 break;
4011 /* Use baseline BESL value as default */
4012 if (field & USB_BESL_BASELINE_VALID)
4013 besl_device = USB_GET_BESL_BASELINE(field);
4014 else if (field & USB_BESL_DEEP_VALID)
4015 besl_device = USB_GET_BESL_DEEP(field);
4016 } else {
4017 if (u2del <= 50)
4018 besl_host = 0;
4019 else
4020 besl_host = (u2del - 51) / 75 + 1;
4023 besl = besl_host + besl_device;
4024 if (besl > 15)
4025 besl = 15;
4027 return besl;
4030 /* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4031 static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
4033 u32 field;
4034 int l1;
4035 int besld = 0;
4036 int hirdm = 0;
4038 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4040 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
4041 l1 = udev->l1_params.timeout / 256;
4043 /* device has preferred BESLD */
4044 if (field & USB_BESL_DEEP_VALID) {
4045 besld = USB_GET_BESL_DEEP(field);
4046 hirdm = 1;
4049 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
4052 int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4053 struct usb_device *udev, int enable)
4055 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4056 __le32 __iomem **port_array;
4057 __le32 __iomem *pm_addr, *hlpm_addr;
4058 u32 pm_val, hlpm_val, field;
4059 unsigned int port_num;
4060 unsigned long flags;
4061 int hird, exit_latency;
4062 int ret;
4064 if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
4065 !udev->lpm_capable)
4066 return -EPERM;
4068 if (!udev->parent || udev->parent->parent ||
4069 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4070 return -EPERM;
4072 if (udev->usb2_hw_lpm_capable != 1)
4073 return -EPERM;
4075 spin_lock_irqsave(&xhci->lock, flags);
4077 port_array = xhci->usb2_ports;
4078 port_num = udev->portnum - 1;
4079 pm_addr = port_array[port_num] + PORTPMSC;
4080 pm_val = xhci_readl(xhci, pm_addr);
4081 hlpm_addr = port_array[port_num] + PORTHLPMC;
4082 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4084 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
4085 enable ? "enable" : "disable", port_num);
4087 if (enable) {
4088 /* Host supports BESL timeout instead of HIRD */
4089 if (udev->usb2_hw_lpm_besl_capable) {
4090 /* if device doesn't have a preferred BESL value use a
4091 * default one which works with mixed HIRD and BESL
4092 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4094 if ((field & USB_BESL_SUPPORT) &&
4095 (field & USB_BESL_BASELINE_VALID))
4096 hird = USB_GET_BESL_BASELINE(field);
4097 else
4098 hird = udev->l1_params.besl;
4100 exit_latency = xhci_besl_encoding[hird];
4101 spin_unlock_irqrestore(&xhci->lock, flags);
4103 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4104 * input context for link powermanagement evaluate
4105 * context commands. It is protected by hcd->bandwidth
4106 * mutex and is shared by all devices. We need to set
4107 * the max ext latency in USB 2 BESL LPM as well, so
4108 * use the same mutex and xhci_change_max_exit_latency()
4110 mutex_lock(hcd->bandwidth_mutex);
4111 ret = xhci_change_max_exit_latency(xhci, udev,
4112 exit_latency);
4113 mutex_unlock(hcd->bandwidth_mutex);
4115 if (ret < 0)
4116 return ret;
4117 spin_lock_irqsave(&xhci->lock, flags);
4119 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
4120 xhci_writel(xhci, hlpm_val, hlpm_addr);
4121 /* flush write */
4122 xhci_readl(xhci, hlpm_addr);
4123 } else {
4124 hird = xhci_calculate_hird_besl(xhci, udev);
4127 pm_val &= ~PORT_HIRD_MASK;
4128 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id);
4129 xhci_writel(xhci, pm_val, pm_addr);
4130 pm_val = xhci_readl(xhci, pm_addr);
4131 pm_val |= PORT_HLE;
4132 xhci_writel(xhci, pm_val, pm_addr);
4133 /* flush write */
4134 xhci_readl(xhci, pm_addr);
4135 } else {
4136 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK);
4137 xhci_writel(xhci, pm_val, pm_addr);
4138 /* flush write */
4139 xhci_readl(xhci, pm_addr);
4140 if (udev->usb2_hw_lpm_besl_capable) {
4141 spin_unlock_irqrestore(&xhci->lock, flags);
4142 mutex_lock(hcd->bandwidth_mutex);
4143 xhci_change_max_exit_latency(xhci, udev, 0);
4144 mutex_unlock(hcd->bandwidth_mutex);
4145 return 0;
4149 spin_unlock_irqrestore(&xhci->lock, flags);
4150 return 0;
4153 /* check if a usb2 port supports a given extened capability protocol
4154 * only USB2 ports extended protocol capability values are cached.
4155 * Return 1 if capability is supported
4157 static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
4158 unsigned capability)
4160 u32 port_offset, port_count;
4161 int i;
4163 for (i = 0; i < xhci->num_ext_caps; i++) {
4164 if (xhci->ext_caps[i] & capability) {
4165 /* port offsets starts at 1 */
4166 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
4167 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
4168 if (port >= port_offset &&
4169 port < port_offset + port_count)
4170 return 1;
4173 return 0;
4176 int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4178 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4179 int portnum = udev->portnum - 1;
4181 if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
4182 !udev->lpm_capable)
4183 return 0;
4185 /* we only support lpm for non-hub device connected to root hub yet */
4186 if (!udev->parent || udev->parent->parent ||
4187 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4188 return 0;
4190 if (xhci->hw_lpm_support == 1 &&
4191 xhci_check_usb2_port_capability(
4192 xhci, portnum, XHCI_HLC)) {
4193 udev->usb2_hw_lpm_capable = 1;
4194 udev->l1_params.timeout = XHCI_L1_TIMEOUT;
4195 udev->l1_params.besl = XHCI_DEFAULT_BESL;
4196 if (xhci_check_usb2_port_capability(xhci, portnum,
4197 XHCI_BLC))
4198 udev->usb2_hw_lpm_besl_capable = 1;
4201 return 0;
4204 #else
4206 int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4207 struct usb_device *udev, int enable)
4209 return 0;
4212 int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4214 return 0;
4217 #endif /* CONFIG_PM_RUNTIME */
4219 /*---------------------- USB 3.0 Link PM functions ------------------------*/
4221 #ifdef CONFIG_PM
4222 /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4223 static unsigned long long xhci_service_interval_to_ns(
4224 struct usb_endpoint_descriptor *desc)
4226 return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
4229 static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
4230 enum usb3_link_state state)
4232 unsigned long long sel;
4233 unsigned long long pel;
4234 unsigned int max_sel_pel;
4235 char *state_name;
4237 switch (state) {
4238 case USB3_LPM_U1:
4239 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4240 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4241 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
4242 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
4243 state_name = "U1";
4244 break;
4245 case USB3_LPM_U2:
4246 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4247 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
4248 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
4249 state_name = "U2";
4250 break;
4251 default:
4252 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
4253 __func__);
4254 return USB3_LPM_DISABLED;
4257 if (sel <= max_sel_pel && pel <= max_sel_pel)
4258 return USB3_LPM_DEVICE_INITIATED;
4260 if (sel > max_sel_pel)
4261 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4262 "due to long SEL %llu ms\n",
4263 state_name, sel);
4264 else
4265 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4266 "due to long PEL %llu ms\n",
4267 state_name, pel);
4268 return USB3_LPM_DISABLED;
4271 /* Returns the hub-encoded U1 timeout value.
4272 * The U1 timeout should be the maximum of the following values:
4273 * - For control endpoints, U1 system exit latency (SEL) * 3
4274 * - For bulk endpoints, U1 SEL * 5
4275 * - For interrupt endpoints:
4276 * - Notification EPs, U1 SEL * 3
4277 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4278 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4280 static u16 xhci_calculate_intel_u1_timeout(struct usb_device *udev,
4281 struct usb_endpoint_descriptor *desc)
4283 unsigned long long timeout_ns;
4284 int ep_type;
4285 int intr_type;
4287 ep_type = usb_endpoint_type(desc);
4288 switch (ep_type) {
4289 case USB_ENDPOINT_XFER_CONTROL:
4290 timeout_ns = udev->u1_params.sel * 3;
4291 break;
4292 case USB_ENDPOINT_XFER_BULK:
4293 timeout_ns = udev->u1_params.sel * 5;
4294 break;
4295 case USB_ENDPOINT_XFER_INT:
4296 intr_type = usb_endpoint_interrupt_type(desc);
4297 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
4298 timeout_ns = udev->u1_params.sel * 3;
4299 break;
4301 /* Otherwise the calculation is the same as isoc eps */
4302 case USB_ENDPOINT_XFER_ISOC:
4303 timeout_ns = xhci_service_interval_to_ns(desc);
4304 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
4305 if (timeout_ns < udev->u1_params.sel * 2)
4306 timeout_ns = udev->u1_params.sel * 2;
4307 break;
4308 default:
4309 return 0;
4312 /* The U1 timeout is encoded in 1us intervals. */
4313 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
4314 /* Don't return a timeout of zero, because that's USB3_LPM_DISABLED. */
4315 if (timeout_ns == USB3_LPM_DISABLED)
4316 timeout_ns++;
4318 /* If the necessary timeout value is bigger than what we can set in the
4319 * USB 3.0 hub, we have to disable hub-initiated U1.
4321 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
4322 return timeout_ns;
4323 dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
4324 "due to long timeout %llu ms\n", timeout_ns);
4325 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
4328 /* Returns the hub-encoded U2 timeout value.
4329 * The U2 timeout should be the maximum of:
4330 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4331 * - largest bInterval of any active periodic endpoint (to avoid going
4332 * into lower power link states between intervals).
4333 * - the U2 Exit Latency of the device
4335 static u16 xhci_calculate_intel_u2_timeout(struct usb_device *udev,
4336 struct usb_endpoint_descriptor *desc)
4338 unsigned long long timeout_ns;
4339 unsigned long long u2_del_ns;
4341 timeout_ns = 10 * 1000 * 1000;
4343 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
4344 (xhci_service_interval_to_ns(desc) > timeout_ns))
4345 timeout_ns = xhci_service_interval_to_ns(desc);
4347 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
4348 if (u2_del_ns > timeout_ns)
4349 timeout_ns = u2_del_ns;
4351 /* The U2 timeout is encoded in 256us intervals */
4352 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
4353 /* If the necessary timeout value is bigger than what we can set in the
4354 * USB 3.0 hub, we have to disable hub-initiated U2.
4356 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4357 return timeout_ns;
4358 dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4359 "due to long timeout %llu ms\n", timeout_ns);
4360 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4363 static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4364 struct usb_device *udev,
4365 struct usb_endpoint_descriptor *desc,
4366 enum usb3_link_state state,
4367 u16 *timeout)
4369 if (state == USB3_LPM_U1) {
4370 if (xhci->quirks & XHCI_INTEL_HOST)
4371 return xhci_calculate_intel_u1_timeout(udev, desc);
4372 } else {
4373 if (xhci->quirks & XHCI_INTEL_HOST)
4374 return xhci_calculate_intel_u2_timeout(udev, desc);
4377 return USB3_LPM_DISABLED;
4380 static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4381 struct usb_device *udev,
4382 struct usb_endpoint_descriptor *desc,
4383 enum usb3_link_state state,
4384 u16 *timeout)
4386 u16 alt_timeout;
4388 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4389 desc, state, timeout);
4391 /* If we found we can't enable hub-initiated LPM, or
4392 * the U1 or U2 exit latency was too high to allow
4393 * device-initiated LPM as well, just stop searching.
4395 if (alt_timeout == USB3_LPM_DISABLED ||
4396 alt_timeout == USB3_LPM_DEVICE_INITIATED) {
4397 *timeout = alt_timeout;
4398 return -E2BIG;
4400 if (alt_timeout > *timeout)
4401 *timeout = alt_timeout;
4402 return 0;
4405 static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4406 struct usb_device *udev,
4407 struct usb_host_interface *alt,
4408 enum usb3_link_state state,
4409 u16 *timeout)
4411 int j;
4413 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4414 if (xhci_update_timeout_for_endpoint(xhci, udev,
4415 &alt->endpoint[j].desc, state, timeout))
4416 return -E2BIG;
4417 continue;
4419 return 0;
4422 static int xhci_check_intel_tier_policy(struct usb_device *udev,
4423 enum usb3_link_state state)
4425 struct usb_device *parent;
4426 unsigned int num_hubs;
4428 if (state == USB3_LPM_U2)
4429 return 0;
4431 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4432 for (parent = udev->parent, num_hubs = 0; parent->parent;
4433 parent = parent->parent)
4434 num_hubs++;
4436 if (num_hubs < 2)
4437 return 0;
4439 dev_dbg(&udev->dev, "Disabling U1 link state for device"
4440 " below second-tier hub.\n");
4441 dev_dbg(&udev->dev, "Plug device into first-tier hub "
4442 "to decrease power consumption.\n");
4443 return -E2BIG;
4446 static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4447 struct usb_device *udev,
4448 enum usb3_link_state state)
4450 if (xhci->quirks & XHCI_INTEL_HOST)
4451 return xhci_check_intel_tier_policy(udev, state);
4452 return -EINVAL;
4455 /* Returns the U1 or U2 timeout that should be enabled.
4456 * If the tier check or timeout setting functions return with a non-zero exit
4457 * code, that means the timeout value has been finalized and we shouldn't look
4458 * at any more endpoints.
4460 static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4461 struct usb_device *udev, enum usb3_link_state state)
4463 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4464 struct usb_host_config *config;
4465 char *state_name;
4466 int i;
4467 u16 timeout = USB3_LPM_DISABLED;
4469 if (state == USB3_LPM_U1)
4470 state_name = "U1";
4471 else if (state == USB3_LPM_U2)
4472 state_name = "U2";
4473 else {
4474 dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4475 state);
4476 return timeout;
4479 if (xhci_check_tier_policy(xhci, udev, state) < 0)
4480 return timeout;
4482 /* Gather some information about the currently installed configuration
4483 * and alternate interface settings.
4485 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4486 state, &timeout))
4487 return timeout;
4489 config = udev->actconfig;
4490 if (!config)
4491 return timeout;
4493 for (i = 0; i < USB_MAXINTERFACES; i++) {
4494 struct usb_driver *driver;
4495 struct usb_interface *intf = config->interface[i];
4497 if (!intf)
4498 continue;
4500 /* Check if any currently bound drivers want hub-initiated LPM
4501 * disabled.
4503 if (intf->dev.driver) {
4504 driver = to_usb_driver(intf->dev.driver);
4505 if (driver && driver->disable_hub_initiated_lpm) {
4506 dev_dbg(&udev->dev, "Hub-initiated %s disabled "
4507 "at request of driver %s\n",
4508 state_name, driver->name);
4509 return xhci_get_timeout_no_hub_lpm(udev, state);
4513 /* Not sure how this could happen... */
4514 if (!intf->cur_altsetting)
4515 continue;
4517 if (xhci_update_timeout_for_interface(xhci, udev,
4518 intf->cur_altsetting,
4519 state, &timeout))
4520 return timeout;
4522 return timeout;
4525 static int calculate_max_exit_latency(struct usb_device *udev,
4526 enum usb3_link_state state_changed,
4527 u16 hub_encoded_timeout)
4529 unsigned long long u1_mel_us = 0;
4530 unsigned long long u2_mel_us = 0;
4531 unsigned long long mel_us = 0;
4532 bool disabling_u1;
4533 bool disabling_u2;
4534 bool enabling_u1;
4535 bool enabling_u2;
4537 disabling_u1 = (state_changed == USB3_LPM_U1 &&
4538 hub_encoded_timeout == USB3_LPM_DISABLED);
4539 disabling_u2 = (state_changed == USB3_LPM_U2 &&
4540 hub_encoded_timeout == USB3_LPM_DISABLED);
4542 enabling_u1 = (state_changed == USB3_LPM_U1 &&
4543 hub_encoded_timeout != USB3_LPM_DISABLED);
4544 enabling_u2 = (state_changed == USB3_LPM_U2 &&
4545 hub_encoded_timeout != USB3_LPM_DISABLED);
4547 /* If U1 was already enabled and we're not disabling it,
4548 * or we're going to enable U1, account for the U1 max exit latency.
4550 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4551 enabling_u1)
4552 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4553 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4554 enabling_u2)
4555 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4557 if (u1_mel_us > u2_mel_us)
4558 mel_us = u1_mel_us;
4559 else
4560 mel_us = u2_mel_us;
4561 /* xHCI host controller max exit latency field is only 16 bits wide. */
4562 if (mel_us > MAX_EXIT) {
4563 dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4564 "is too big.\n", mel_us);
4565 return -E2BIG;
4567 return mel_us;
4570 /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4571 int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4572 struct usb_device *udev, enum usb3_link_state state)
4574 struct xhci_hcd *xhci;
4575 u16 hub_encoded_timeout;
4576 int mel;
4577 int ret;
4579 xhci = hcd_to_xhci(hcd);
4580 /* The LPM timeout values are pretty host-controller specific, so don't
4581 * enable hub-initiated timeouts unless the vendor has provided
4582 * information about their timeout algorithm.
4584 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4585 !xhci->devs[udev->slot_id])
4586 return USB3_LPM_DISABLED;
4588 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
4589 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
4590 if (mel < 0) {
4591 /* Max Exit Latency is too big, disable LPM. */
4592 hub_encoded_timeout = USB3_LPM_DISABLED;
4593 mel = 0;
4596 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4597 if (ret)
4598 return ret;
4599 return hub_encoded_timeout;
4602 int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4603 struct usb_device *udev, enum usb3_link_state state)
4605 struct xhci_hcd *xhci;
4606 u16 mel;
4607 int ret;
4609 xhci = hcd_to_xhci(hcd);
4610 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4611 !xhci->devs[udev->slot_id])
4612 return 0;
4614 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
4615 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4616 if (ret)
4617 return ret;
4618 return 0;
4620 #else /* CONFIG_PM */
4622 int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4623 struct usb_device *udev, enum usb3_link_state state)
4625 return USB3_LPM_DISABLED;
4628 int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4629 struct usb_device *udev, enum usb3_link_state state)
4631 return 0;
4633 #endif /* CONFIG_PM */
4635 /*-------------------------------------------------------------------------*/
4637 /* Once a hub descriptor is fetched for a device, we need to update the xHC's
4638 * internal data structures for the device.
4640 int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
4641 struct usb_tt *tt, gfp_t mem_flags)
4643 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4644 struct xhci_virt_device *vdev;
4645 struct xhci_command *config_cmd;
4646 struct xhci_input_control_ctx *ctrl_ctx;
4647 struct xhci_slot_ctx *slot_ctx;
4648 unsigned long flags;
4649 unsigned think_time;
4650 int ret;
4652 /* Ignore root hubs */
4653 if (!hdev->parent)
4654 return 0;
4656 vdev = xhci->devs[hdev->slot_id];
4657 if (!vdev) {
4658 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4659 return -EINVAL;
4661 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
4662 if (!config_cmd) {
4663 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
4664 return -ENOMEM;
4666 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
4667 if (!ctrl_ctx) {
4668 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4669 __func__);
4670 xhci_free_command(xhci, config_cmd);
4671 return -ENOMEM;
4674 spin_lock_irqsave(&xhci->lock, flags);
4675 if (hdev->speed == USB_SPEED_HIGH &&
4676 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4677 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4678 xhci_free_command(xhci, config_cmd);
4679 spin_unlock_irqrestore(&xhci->lock, flags);
4680 return -ENOMEM;
4683 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
4684 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
4685 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
4686 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
4687 if (tt->multi)
4688 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
4689 if (xhci->hci_version > 0x95) {
4690 xhci_dbg(xhci, "xHCI version %x needs hub "
4691 "TT think time and number of ports\n",
4692 (unsigned int) xhci->hci_version);
4693 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
4694 /* Set TT think time - convert from ns to FS bit times.
4695 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4696 * 2 = 24 FS bit times, 3 = 32 FS bit times.
4698 * xHCI 1.0: this field shall be 0 if the device is not a
4699 * High-spped hub.
4701 think_time = tt->think_time;
4702 if (think_time != 0)
4703 think_time = (think_time / 666) - 1;
4704 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4705 slot_ctx->tt_info |=
4706 cpu_to_le32(TT_THINK_TIME(think_time));
4707 } else {
4708 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4709 "TT think time or number of ports\n",
4710 (unsigned int) xhci->hci_version);
4712 slot_ctx->dev_state = 0;
4713 spin_unlock_irqrestore(&xhci->lock, flags);
4715 xhci_dbg(xhci, "Set up %s for hub device.\n",
4716 (xhci->hci_version > 0x95) ?
4717 "configure endpoint" : "evaluate context");
4718 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
4719 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
4721 /* Issue and wait for the configure endpoint or
4722 * evaluate context command.
4724 if (xhci->hci_version > 0x95)
4725 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4726 false, false);
4727 else
4728 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4729 true, false);
4731 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
4732 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
4734 xhci_free_command(xhci, config_cmd);
4735 return ret;
4738 int xhci_get_frame(struct usb_hcd *hcd)
4740 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4741 /* EHCI mods by the periodic size. Why? */
4742 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
4745 int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4747 struct xhci_hcd *xhci;
4748 struct device *dev = hcd->self.controller;
4749 int retval;
4751 /* Accept arbitrarily long scatter-gather lists */
4752 hcd->self.sg_tablesize = ~0;
4754 /* support to build packet from discontinuous buffers */
4755 hcd->self.no_sg_constraint = 1;
4757 /* XHCI controllers don't stop the ep queue on short packets :| */
4758 hcd->self.no_stop_on_short = 1;
4760 if (usb_hcd_is_primary_hcd(hcd)) {
4761 xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
4762 if (!xhci)
4763 return -ENOMEM;
4764 *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
4765 xhci->main_hcd = hcd;
4766 /* Mark the first roothub as being USB 2.0.
4767 * The xHCI driver will register the USB 3.0 roothub.
4769 hcd->speed = HCD_USB2;
4770 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4772 * USB 2.0 roothub under xHCI has an integrated TT,
4773 * (rate matching hub) as opposed to having an OHCI/UHCI
4774 * companion controller.
4776 hcd->has_tt = 1;
4777 } else {
4778 /* xHCI private pointer was set in xhci_pci_probe for the second
4779 * registered roothub.
4781 return 0;
4784 xhci->cap_regs = hcd->regs;
4785 xhci->op_regs = hcd->regs +
4786 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
4787 xhci->run_regs = hcd->regs +
4788 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
4789 /* Cache read-only capability registers */
4790 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
4791 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
4792 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
4793 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
4794 xhci->hci_version = HC_VERSION(xhci->hcc_params);
4795 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4796 xhci_print_registers(xhci);
4798 get_quirks(dev, xhci);
4800 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4801 * success event after a short transfer. This quirk will ignore such
4802 * spurious event.
4804 if (xhci->hci_version > 0x96)
4805 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
4807 /* Make sure the HC is halted. */
4808 retval = xhci_halt(xhci);
4809 if (retval)
4810 goto error;
4812 xhci_dbg(xhci, "Resetting HCD\n");
4813 /* Reset the internal HC memory state and registers. */
4814 retval = xhci_reset(xhci);
4815 if (retval)
4816 goto error;
4817 xhci_dbg(xhci, "Reset complete\n");
4819 /* Set dma_mask and coherent_dma_mask to 64-bits,
4820 * if xHC supports 64-bit addressing */
4821 if (HCC_64BIT_ADDR(xhci->hcc_params) &&
4822 !dma_set_mask(dev, DMA_BIT_MASK(64))) {
4823 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4824 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
4827 xhci_dbg(xhci, "Calling HCD init\n");
4828 /* Initialize HCD and host controller data structures. */
4829 retval = xhci_init(hcd);
4830 if (retval)
4831 goto error;
4832 xhci_dbg(xhci, "Called HCD init\n");
4833 return 0;
4834 error:
4835 kfree(xhci);
4836 return retval;
4839 MODULE_DESCRIPTION(DRIVER_DESC);
4840 MODULE_AUTHOR(DRIVER_AUTHOR);
4841 MODULE_LICENSE("GPL");
4843 static int __init xhci_hcd_init(void)
4845 int retval;
4847 retval = xhci_register_pci();
4848 if (retval < 0) {
4849 pr_debug("Problem registering PCI driver.\n");
4850 return retval;
4852 retval = xhci_register_plat();
4853 if (retval < 0) {
4854 pr_debug("Problem registering platform driver.\n");
4855 goto unreg_pci;
4858 * Check the compiler generated sizes of structures that must be laid
4859 * out in specific ways for hardware access.
4861 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
4862 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
4863 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
4864 /* xhci_device_control has eight fields, and also
4865 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
4867 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
4868 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
4869 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
4870 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
4871 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
4872 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
4873 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
4874 return 0;
4875 unreg_pci:
4876 xhci_unregister_pci();
4877 return retval;
4879 module_init(xhci_hcd_init);
4881 static void __exit xhci_hcd_cleanup(void)
4883 xhci_unregister_pci();
4884 xhci_unregister_plat();
4886 module_exit(xhci_hcd_cleanup);