mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / usb / host / xhci.c
blobea185eaeae284e5a575381f19634fa2a4f5bf9e2
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 | XHCI_STATE_DYING);
148 return ret;
152 * Reset a halted HC.
154 * This resets pipelines, timers, counters, state machines, etc.
155 * Transactions will be terminated immediately, and operational registers
156 * will be set to their defaults.
158 int xhci_reset(struct xhci_hcd *xhci)
160 u32 command;
161 u32 state;
162 int ret, i;
164 state = xhci_readl(xhci, &xhci->op_regs->status);
165 if ((state & STS_HALT) == 0) {
166 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
167 return 0;
170 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC");
171 command = xhci_readl(xhci, &xhci->op_regs->command);
172 command |= CMD_RESET;
173 xhci_writel(xhci, command, &xhci->op_regs->command);
175 ret = xhci_handshake(xhci, &xhci->op_regs->command,
176 CMD_RESET, 0, 10 * 1000 * 1000);
177 if (ret)
178 return ret;
180 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
181 "Wait for controller to be ready for doorbell rings");
183 * xHCI cannot write to any doorbells or operational registers other
184 * than status until the "Controller Not Ready" flag is cleared.
186 ret = xhci_handshake(xhci, &xhci->op_regs->status,
187 STS_CNR, 0, 10 * 1000 * 1000);
189 for (i = 0; i < 2; ++i) {
190 xhci->bus_state[i].port_c_suspend = 0;
191 xhci->bus_state[i].suspended_ports = 0;
192 xhci->bus_state[i].resuming_ports = 0;
195 return ret;
198 #ifdef CONFIG_PCI
199 static int xhci_free_msi(struct xhci_hcd *xhci)
201 int i;
203 if (!xhci->msix_entries)
204 return -EINVAL;
206 for (i = 0; i < xhci->msix_count; i++)
207 if (xhci->msix_entries[i].vector)
208 free_irq(xhci->msix_entries[i].vector,
209 xhci_to_hcd(xhci));
210 return 0;
214 * Set up MSI
216 static int xhci_setup_msi(struct xhci_hcd *xhci)
218 int ret;
219 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
221 ret = pci_enable_msi(pdev);
222 if (ret) {
223 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
224 "failed to allocate MSI entry");
225 return ret;
228 ret = request_irq(pdev->irq, xhci_msi_irq,
229 0, "xhci_hcd", xhci_to_hcd(xhci));
230 if (ret) {
231 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
232 "disable MSI interrupt");
233 pci_disable_msi(pdev);
236 return ret;
240 * Free IRQs
241 * free all IRQs request
243 static void xhci_free_irq(struct xhci_hcd *xhci)
245 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
246 int ret;
248 /* return if using legacy interrupt */
249 if (xhci_to_hcd(xhci)->irq > 0)
250 return;
252 ret = xhci_free_msi(xhci);
253 if (!ret)
254 return;
255 if (pdev->irq > 0)
256 free_irq(pdev->irq, xhci_to_hcd(xhci));
258 return;
262 * Set up MSI-X
264 static int xhci_setup_msix(struct xhci_hcd *xhci)
266 int i, ret = 0;
267 struct usb_hcd *hcd = xhci_to_hcd(xhci);
268 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
271 * calculate number of msi-x vectors supported.
272 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
273 * with max number of interrupters based on the xhci HCSPARAMS1.
274 * - num_online_cpus: maximum msi-x vectors per CPUs core.
275 * Add additional 1 vector to ensure always available interrupt.
277 xhci->msix_count = min(num_online_cpus() + 1,
278 HCS_MAX_INTRS(xhci->hcs_params1));
280 xhci->msix_entries =
281 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
282 GFP_KERNEL);
283 if (!xhci->msix_entries) {
284 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
285 return -ENOMEM;
288 for (i = 0; i < xhci->msix_count; i++) {
289 xhci->msix_entries[i].entry = i;
290 xhci->msix_entries[i].vector = 0;
293 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
294 if (ret) {
295 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
296 "Failed to enable MSI-X");
297 goto free_entries;
300 for (i = 0; i < xhci->msix_count; i++) {
301 ret = request_irq(xhci->msix_entries[i].vector,
302 xhci_msi_irq,
303 0, "xhci_hcd", xhci_to_hcd(xhci));
304 if (ret)
305 goto disable_msix;
308 hcd->msix_enabled = 1;
309 return ret;
311 disable_msix:
312 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt");
313 xhci_free_irq(xhci);
314 pci_disable_msix(pdev);
315 free_entries:
316 kfree(xhci->msix_entries);
317 xhci->msix_entries = NULL;
318 return ret;
321 /* Free any IRQs and disable MSI-X */
322 static void xhci_cleanup_msix(struct xhci_hcd *xhci)
324 struct usb_hcd *hcd = xhci_to_hcd(xhci);
325 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
327 if (xhci->quirks & XHCI_PLAT)
328 return;
330 xhci_free_irq(xhci);
332 if (xhci->msix_entries) {
333 pci_disable_msix(pdev);
334 kfree(xhci->msix_entries);
335 xhci->msix_entries = NULL;
336 } else {
337 pci_disable_msi(pdev);
340 hcd->msix_enabled = 0;
341 return;
344 static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
346 int i;
348 if (xhci->msix_entries) {
349 for (i = 0; i < xhci->msix_count; i++)
350 synchronize_irq(xhci->msix_entries[i].vector);
354 static int xhci_try_enable_msi(struct usb_hcd *hcd)
356 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
357 struct pci_dev *pdev;
358 int ret;
360 /* The xhci platform device has set up IRQs through usb_add_hcd. */
361 if (xhci->quirks & XHCI_PLAT)
362 return 0;
364 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
366 * Some Fresco Logic host controllers advertise MSI, but fail to
367 * generate interrupts. Don't even try to enable MSI.
369 if (xhci->quirks & XHCI_BROKEN_MSI)
370 goto legacy_irq;
372 /* unregister the legacy interrupt */
373 if (hcd->irq)
374 free_irq(hcd->irq, hcd);
375 hcd->irq = 0;
377 ret = xhci_setup_msix(xhci);
378 if (ret)
379 /* fall back to msi*/
380 ret = xhci_setup_msi(xhci);
382 if (!ret)
383 /* hcd->irq is 0, we have MSI */
384 return 0;
386 if (!pdev->irq) {
387 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
388 return -EINVAL;
391 legacy_irq:
392 /* fall back to legacy interrupt*/
393 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
394 hcd->irq_descr, hcd);
395 if (ret) {
396 xhci_err(xhci, "request interrupt %d failed\n",
397 pdev->irq);
398 return ret;
400 hcd->irq = pdev->irq;
401 return 0;
404 #else
406 static inline int xhci_try_enable_msi(struct usb_hcd *hcd)
408 return 0;
411 static inline void xhci_cleanup_msix(struct xhci_hcd *xhci)
415 static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
419 #endif
421 static void compliance_mode_recovery(unsigned long arg)
423 struct xhci_hcd *xhci;
424 struct usb_hcd *hcd;
425 u32 temp;
426 int i;
428 xhci = (struct xhci_hcd *)arg;
430 for (i = 0; i < xhci->num_usb3_ports; i++) {
431 temp = xhci_readl(xhci, xhci->usb3_ports[i]);
432 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
434 * Compliance Mode Detected. Letting USB Core
435 * handle the Warm Reset
437 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
438 "Compliance mode detected->port %d",
439 i + 1);
440 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
441 "Attempting compliance mode recovery");
442 hcd = xhci->shared_hcd;
444 if (hcd->state == HC_STATE_SUSPENDED)
445 usb_hcd_resume_root_hub(hcd);
447 usb_hcd_poll_rh_status(hcd);
451 if (xhci->port_status_u0 != ((1 << xhci->num_usb3_ports)-1))
452 mod_timer(&xhci->comp_mode_recovery_timer,
453 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
457 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
458 * that causes ports behind that hardware to enter compliance mode sometimes.
459 * The quirk creates a timer that polls every 2 seconds the link state of
460 * each host controller's port and recovers it by issuing a Warm reset
461 * if Compliance mode is detected, otherwise the port will become "dead" (no
462 * device connections or disconnections will be detected anymore). Becasue no
463 * status event is generated when entering compliance mode (per xhci spec),
464 * this quirk is needed on systems that have the failing hardware installed.
466 static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
468 xhci->port_status_u0 = 0;
469 init_timer(&xhci->comp_mode_recovery_timer);
471 xhci->comp_mode_recovery_timer.data = (unsigned long) xhci;
472 xhci->comp_mode_recovery_timer.function = compliance_mode_recovery;
473 xhci->comp_mode_recovery_timer.expires = jiffies +
474 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
476 set_timer_slack(&xhci->comp_mode_recovery_timer,
477 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
478 add_timer(&xhci->comp_mode_recovery_timer);
479 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
480 "Compliance mode recovery timer initialized");
484 * This function identifies the systems that have installed the SN65LVPE502CP
485 * USB3.0 re-driver and that need the Compliance Mode Quirk.
486 * Systems:
487 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
489 bool xhci_compliance_mode_recovery_timer_quirk_check(void)
491 const char *dmi_product_name, *dmi_sys_vendor;
493 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
494 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
495 if (!dmi_product_name || !dmi_sys_vendor)
496 return false;
498 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
499 return false;
501 if (strstr(dmi_product_name, "Z420") ||
502 strstr(dmi_product_name, "Z620") ||
503 strstr(dmi_product_name, "Z820") ||
504 strstr(dmi_product_name, "Z1 Workstation"))
505 return true;
507 return false;
510 static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
512 return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
517 * Initialize memory for HCD and xHC (one-time init).
519 * Program the PAGESIZE register, initialize the device context array, create
520 * device contexts (?), set up a command ring segment (or two?), create event
521 * ring (one for now).
523 int xhci_init(struct usb_hcd *hcd)
525 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
526 int retval = 0;
528 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init");
529 spin_lock_init(&xhci->lock);
530 if (xhci->hci_version == 0x95 && link_quirk) {
531 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
532 "QUIRK: Not clearing Link TRB chain bits.");
533 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
534 } else {
535 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
536 "xHCI doesn't need link TRB QUIRK");
538 retval = xhci_mem_init(xhci, GFP_KERNEL);
539 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init");
541 /* Initializing Compliance Mode Recovery Data If Needed */
542 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
543 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
544 compliance_mode_recovery_timer_init(xhci);
547 return retval;
550 /*-------------------------------------------------------------------------*/
553 static int xhci_run_finished(struct xhci_hcd *xhci)
555 if (xhci_start(xhci)) {
556 xhci_halt(xhci);
557 return -ENODEV;
559 xhci->shared_hcd->state = HC_STATE_RUNNING;
560 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
562 if (xhci->quirks & XHCI_NEC_HOST)
563 xhci_ring_cmd_db(xhci);
565 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
566 "Finished xhci_run for USB3 roothub");
567 return 0;
571 * Start the HC after it was halted.
573 * This function is called by the USB core when the HC driver is added.
574 * Its opposite is xhci_stop().
576 * xhci_init() must be called once before this function can be called.
577 * Reset the HC, enable device slot contexts, program DCBAAP, and
578 * set command ring pointer and event ring pointer.
580 * Setup MSI-X vectors and enable interrupts.
582 int xhci_run(struct usb_hcd *hcd)
584 u32 temp;
585 u64 temp_64;
586 int ret;
587 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
589 /* Start the xHCI host controller running only after the USB 2.0 roothub
590 * is setup.
593 hcd->uses_new_polling = 1;
594 if (!usb_hcd_is_primary_hcd(hcd))
595 return xhci_run_finished(xhci);
597 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");
599 ret = xhci_try_enable_msi(hcd);
600 if (ret)
601 return ret;
603 xhci_dbg(xhci, "Command ring memory map follows:\n");
604 xhci_debug_ring(xhci, xhci->cmd_ring);
605 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
606 xhci_dbg_cmd_ptrs(xhci);
608 xhci_dbg(xhci, "ERST memory map follows:\n");
609 xhci_dbg_erst(xhci, &xhci->erst);
610 xhci_dbg(xhci, "Event ring:\n");
611 xhci_debug_ring(xhci, xhci->event_ring);
612 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
613 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
614 temp_64 &= ~ERST_PTR_MASK;
615 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
616 "ERST deq = 64'h%0lx", (long unsigned int) temp_64);
618 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
619 "// Set the interrupt modulation register");
620 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
621 temp &= ~ER_IRQ_INTERVAL_MASK;
622 temp |= (u32) 160;
623 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
625 /* Set the HCD state before we enable the irqs */
626 temp = xhci_readl(xhci, &xhci->op_regs->command);
627 temp |= (CMD_EIE);
628 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
629 "// Enable interrupts, cmd = 0x%x.", temp);
630 xhci_writel(xhci, temp, &xhci->op_regs->command);
632 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
633 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
634 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
635 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
636 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
637 &xhci->ir_set->irq_pending);
638 xhci_print_ir_set(xhci, 0);
640 if (xhci->quirks & XHCI_NEC_HOST)
641 xhci_queue_vendor_command(xhci, 0, 0, 0,
642 TRB_TYPE(TRB_NEC_GET_FW));
644 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
645 "Finished xhci_run for USB2 roothub");
646 return 0;
649 static void xhci_only_stop_hcd(struct usb_hcd *hcd)
651 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
653 spin_lock_irq(&xhci->lock);
654 xhci_halt(xhci);
656 /* The shared_hcd is going to be deallocated shortly (the USB core only
657 * calls this function when allocation fails in usb_add_hcd(), or
658 * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
660 xhci->shared_hcd = NULL;
661 spin_unlock_irq(&xhci->lock);
665 * Stop xHCI driver.
667 * This function is called by the USB core when the HC driver is removed.
668 * Its opposite is xhci_run().
670 * Disable device contexts, disable IRQs, and quiesce the HC.
671 * Reset the HC, finish any completed transactions, and cleanup memory.
673 void xhci_stop(struct usb_hcd *hcd)
675 u32 temp;
676 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
678 if (!usb_hcd_is_primary_hcd(hcd)) {
679 xhci_only_stop_hcd(xhci->shared_hcd);
680 return;
683 spin_lock_irq(&xhci->lock);
684 /* Make sure the xHC is halted for a USB3 roothub
685 * (xhci_stop() could be called as part of failed init).
687 xhci_halt(xhci);
688 xhci_reset(xhci);
689 spin_unlock_irq(&xhci->lock);
691 xhci_cleanup_msix(xhci);
693 /* Deleting Compliance Mode Recovery Timer */
694 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
695 (!(xhci_all_ports_seen_u0(xhci)))) {
696 del_timer_sync(&xhci->comp_mode_recovery_timer);
697 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
698 "%s: compliance mode recovery timer deleted",
699 __func__);
702 if (xhci->quirks & XHCI_AMD_PLL_FIX)
703 usb_amd_dev_put();
705 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
706 "// Disabling event ring interrupts");
707 temp = xhci_readl(xhci, &xhci->op_regs->status);
708 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
709 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
710 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
711 &xhci->ir_set->irq_pending);
712 xhci_print_ir_set(xhci, 0);
714 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
715 xhci_mem_cleanup(xhci);
716 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
717 "xhci_stop completed - status = %x",
718 xhci_readl(xhci, &xhci->op_regs->status));
722 * Shutdown HC (not bus-specific)
724 * This is called when the machine is rebooting or halting. We assume that the
725 * machine will be powered off, and the HC's internal state will be reset.
726 * Don't bother to free memory.
728 * This will only ever be called with the main usb_hcd (the USB3 roothub).
730 void xhci_shutdown(struct usb_hcd *hcd)
732 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
734 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
735 usb_disable_xhci_ports(to_pci_dev(hcd->self.controller));
737 spin_lock_irq(&xhci->lock);
738 xhci_halt(xhci);
739 /* Workaround for spurious wakeups at shutdown with HSW */
740 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
741 xhci_reset(xhci);
742 spin_unlock_irq(&xhci->lock);
744 xhci_cleanup_msix(xhci);
746 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
747 "xhci_shutdown completed - status = %x",
748 xhci_readl(xhci, &xhci->op_regs->status));
750 /* Yet another workaround for spurious wakeups at shutdown with HSW */
751 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
752 pci_set_power_state(to_pci_dev(hcd->self.controller), PCI_D3hot);
755 #ifdef CONFIG_PM
756 static void xhci_save_registers(struct xhci_hcd *xhci)
758 xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
759 xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
760 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
761 xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
762 xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
763 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
764 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
765 xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
766 xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
769 static void xhci_restore_registers(struct xhci_hcd *xhci)
771 xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
772 xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
773 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
774 xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
775 xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
776 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
777 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
778 xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
779 xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
782 static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
784 u64 val_64;
786 /* step 2: initialize command ring buffer */
787 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
788 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
789 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
790 xhci->cmd_ring->dequeue) &
791 (u64) ~CMD_RING_RSVD_BITS) |
792 xhci->cmd_ring->cycle_state;
793 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
794 "// Setting command ring address to 0x%llx",
795 (long unsigned long) val_64);
796 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
800 * The whole command ring must be cleared to zero when we suspend the host.
802 * The host doesn't save the command ring pointer in the suspend well, so we
803 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
804 * aligned, because of the reserved bits in the command ring dequeue pointer
805 * register. Therefore, we can't just set the dequeue pointer back in the
806 * middle of the ring (TRBs are 16-byte aligned).
808 static void xhci_clear_command_ring(struct xhci_hcd *xhci)
810 struct xhci_ring *ring;
811 struct xhci_segment *seg;
813 ring = xhci->cmd_ring;
814 seg = ring->deq_seg;
815 do {
816 memset(seg->trbs, 0,
817 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
818 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
819 cpu_to_le32(~TRB_CYCLE);
820 seg = seg->next;
821 } while (seg != ring->deq_seg);
823 /* Reset the software enqueue and dequeue pointers */
824 ring->deq_seg = ring->first_seg;
825 ring->dequeue = ring->first_seg->trbs;
826 ring->enq_seg = ring->deq_seg;
827 ring->enqueue = ring->dequeue;
829 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
831 * Ring is now zeroed, so the HW should look for change of ownership
832 * when the cycle bit is set to 1.
834 ring->cycle_state = 1;
837 * Reset the hardware dequeue pointer.
838 * Yes, this will need to be re-written after resume, but we're paranoid
839 * and want to make sure the hardware doesn't access bogus memory
840 * because, say, the BIOS or an SMI started the host without changing
841 * the command ring pointers.
843 xhci_set_cmd_ring_deq(xhci);
846 static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
848 int port_index;
849 __le32 __iomem **port_array;
850 unsigned long flags;
851 u32 t1, t2;
853 spin_lock_irqsave(&xhci->lock, flags);
855 /* disble usb3 ports Wake bits*/
856 port_index = xhci->num_usb3_ports;
857 port_array = xhci->usb3_ports;
858 while (port_index--) {
859 t1 = readl(port_array[port_index]);
860 t1 = xhci_port_state_to_neutral(t1);
861 t2 = t1 & ~PORT_WAKE_BITS;
862 if (t1 != t2)
863 writel(t2, port_array[port_index]);
866 /* disble usb2 ports Wake bits*/
867 port_index = xhci->num_usb2_ports;
868 port_array = xhci->usb2_ports;
869 while (port_index--) {
870 t1 = readl(port_array[port_index]);
871 t1 = xhci_port_state_to_neutral(t1);
872 t2 = t1 & ~PORT_WAKE_BITS;
873 if (t1 != t2)
874 writel(t2, port_array[port_index]);
877 spin_unlock_irqrestore(&xhci->lock, flags);
881 * Stop HC (not bus-specific)
883 * This is called when the machine transition into S3/S4 mode.
886 int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
888 int rc = 0;
889 unsigned int delay = XHCI_MAX_HALT_USEC;
890 struct usb_hcd *hcd = xhci_to_hcd(xhci);
891 u32 command;
893 if (hcd->state != HC_STATE_SUSPENDED ||
894 xhci->shared_hcd->state != HC_STATE_SUSPENDED)
895 return -EINVAL;
897 /* Clear root port wake on bits if wakeup not allowed. */
898 if (!do_wakeup)
899 xhci_disable_port_wake_on_bits(xhci);
901 /* Don't poll the roothubs on bus suspend. */
902 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
903 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
904 del_timer_sync(&hcd->rh_timer);
906 spin_lock_irq(&xhci->lock);
907 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
908 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
909 /* step 1: stop endpoint */
910 /* skipped assuming that port suspend has done */
912 /* step 2: clear Run/Stop bit */
913 command = xhci_readl(xhci, &xhci->op_regs->command);
914 command &= ~CMD_RUN;
915 xhci_writel(xhci, command, &xhci->op_regs->command);
917 /* Some chips from Fresco Logic need an extraordinary delay */
918 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
920 if (xhci_handshake(xhci, &xhci->op_regs->status,
921 STS_HALT, STS_HALT, delay)) {
922 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
923 spin_unlock_irq(&xhci->lock);
924 return -ETIMEDOUT;
926 xhci_clear_command_ring(xhci);
928 /* step 3: save registers */
929 xhci_save_registers(xhci);
931 /* step 4: set CSS flag */
932 command = xhci_readl(xhci, &xhci->op_regs->command);
933 command |= CMD_CSS;
934 xhci_writel(xhci, command, &xhci->op_regs->command);
935 if (xhci_handshake(xhci, &xhci->op_regs->status,
936 STS_SAVE, 0, 10 * 1000)) {
937 xhci_warn(xhci, "WARN: xHC save state timeout\n");
938 spin_unlock_irq(&xhci->lock);
939 return -ETIMEDOUT;
941 spin_unlock_irq(&xhci->lock);
944 * Deleting Compliance Mode Recovery Timer because the xHCI Host
945 * is about to be suspended.
947 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
948 (!(xhci_all_ports_seen_u0(xhci)))) {
949 del_timer_sync(&xhci->comp_mode_recovery_timer);
950 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
951 "%s: compliance mode recovery timer deleted",
952 __func__);
955 /* step 5: remove core well power */
956 /* synchronize irq when using MSI-X */
957 xhci_msix_sync_irqs(xhci);
959 return rc;
963 * start xHC (not bus-specific)
965 * This is called when the machine transition from S3/S4 mode.
968 int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
970 u32 command, temp = 0, status;
971 struct usb_hcd *hcd = xhci_to_hcd(xhci);
972 struct usb_hcd *secondary_hcd;
973 int retval = 0;
974 bool comp_timer_running = false;
976 /* Wait a bit if either of the roothubs need to settle from the
977 * transition into bus suspend.
979 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
980 time_before(jiffies,
981 xhci->bus_state[1].next_statechange))
982 msleep(100);
984 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
985 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
987 spin_lock_irq(&xhci->lock);
988 if (xhci->quirks & XHCI_RESET_ON_RESUME)
989 hibernated = true;
991 if (!hibernated) {
992 /* step 1: restore register */
993 xhci_restore_registers(xhci);
994 /* step 2: initialize command ring buffer */
995 xhci_set_cmd_ring_deq(xhci);
996 /* step 3: restore state and start state*/
997 /* step 3: set CRS flag */
998 command = xhci_readl(xhci, &xhci->op_regs->command);
999 command |= CMD_CRS;
1000 xhci_writel(xhci, command, &xhci->op_regs->command);
1001 if (xhci_handshake(xhci, &xhci->op_regs->status,
1002 STS_RESTORE, 0, 10 * 1000)) {
1003 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
1004 spin_unlock_irq(&xhci->lock);
1005 return -ETIMEDOUT;
1007 temp = xhci_readl(xhci, &xhci->op_regs->status);
1010 /* If restore operation fails, re-initialize the HC during resume */
1011 if ((temp & STS_SRE) || hibernated) {
1013 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1014 !(xhci_all_ports_seen_u0(xhci))) {
1015 del_timer_sync(&xhci->comp_mode_recovery_timer);
1016 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1017 "Compliance Mode Recovery Timer deleted!");
1020 /* Let the USB core know _both_ roothubs lost power. */
1021 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1022 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
1024 xhci_dbg(xhci, "Stop HCD\n");
1025 xhci_halt(xhci);
1026 xhci_reset(xhci);
1027 spin_unlock_irq(&xhci->lock);
1028 xhci_cleanup_msix(xhci);
1030 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
1031 temp = xhci_readl(xhci, &xhci->op_regs->status);
1032 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
1033 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
1034 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
1035 &xhci->ir_set->irq_pending);
1036 xhci_print_ir_set(xhci, 0);
1038 xhci_dbg(xhci, "cleaning up memory\n");
1039 xhci_mem_cleanup(xhci);
1040 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
1041 xhci_readl(xhci, &xhci->op_regs->status));
1043 /* USB core calls the PCI reinit and start functions twice:
1044 * first with the primary HCD, and then with the secondary HCD.
1045 * If we don't do the same, the host will never be started.
1047 if (!usb_hcd_is_primary_hcd(hcd))
1048 secondary_hcd = hcd;
1049 else
1050 secondary_hcd = xhci->shared_hcd;
1052 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1053 retval = xhci_init(hcd->primary_hcd);
1054 if (retval)
1055 return retval;
1056 comp_timer_running = true;
1058 xhci_dbg(xhci, "Start the primary HCD\n");
1059 retval = xhci_run(hcd->primary_hcd);
1060 if (!retval) {
1061 xhci_dbg(xhci, "Start the secondary HCD\n");
1062 retval = xhci_run(secondary_hcd);
1064 hcd->state = HC_STATE_SUSPENDED;
1065 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
1066 goto done;
1069 /* step 4: set Run/Stop bit */
1070 command = xhci_readl(xhci, &xhci->op_regs->command);
1071 command |= CMD_RUN;
1072 xhci_writel(xhci, command, &xhci->op_regs->command);
1073 xhci_handshake(xhci, &xhci->op_regs->status, STS_HALT,
1074 0, 250 * 1000);
1076 /* step 5: walk topology and initialize portsc,
1077 * portpmsc and portli
1079 /* this is done in bus_resume */
1081 /* step 6: restart each of the previously
1082 * Running endpoints by ringing their doorbells
1085 spin_unlock_irq(&xhci->lock);
1087 done:
1088 if (retval == 0) {
1089 /* Resume root hubs only when have pending events. */
1090 status = readl(&xhci->op_regs->status);
1091 if (status & STS_EINT) {
1092 usb_hcd_resume_root_hub(hcd);
1093 usb_hcd_resume_root_hub(xhci->shared_hcd);
1098 * If system is subject to the Quirk, Compliance Mode Timer needs to
1099 * be re-initialized Always after a system resume. Ports are subject
1100 * to suffer the Compliance Mode issue again. It doesn't matter if
1101 * ports have entered previously to U0 before system's suspension.
1103 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
1104 compliance_mode_recovery_timer_init(xhci);
1106 /* Re-enable port polling. */
1107 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
1108 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1109 usb_hcd_poll_rh_status(hcd);
1111 return retval;
1113 #endif /* CONFIG_PM */
1115 /*-------------------------------------------------------------------------*/
1118 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1119 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1120 * value to right shift 1 for the bitmask.
1122 * Index = (epnum * 2) + direction - 1,
1123 * where direction = 0 for OUT, 1 for IN.
1124 * For control endpoints, the IN index is used (OUT index is unused), so
1125 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1127 unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1129 unsigned int index;
1130 if (usb_endpoint_xfer_control(desc))
1131 index = (unsigned int) (usb_endpoint_num(desc)*2);
1132 else
1133 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1134 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1135 return index;
1138 /* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1139 * address from the XHCI endpoint index.
1141 unsigned int xhci_get_endpoint_address(unsigned int ep_index)
1143 unsigned int number = DIV_ROUND_UP(ep_index, 2);
1144 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
1145 return direction | number;
1148 /* Find the flag for this endpoint (for use in the control context). Use the
1149 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1150 * bit 1, etc.
1152 unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
1154 return 1 << (xhci_get_endpoint_index(desc) + 1);
1157 /* Find the flag for this endpoint (for use in the control context). Use the
1158 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1159 * bit 1, etc.
1161 unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
1163 return 1 << (ep_index + 1);
1166 /* Compute the last valid endpoint context index. Basically, this is the
1167 * endpoint index plus one. For slot contexts with more than valid endpoint,
1168 * we find the most significant bit set in the added contexts flags.
1169 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1170 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1172 unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
1174 return fls(added_ctxs) - 1;
1177 /* Returns 1 if the arguments are OK;
1178 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1180 static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
1181 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1182 const char *func) {
1183 struct xhci_hcd *xhci;
1184 struct xhci_virt_device *virt_dev;
1186 if (!hcd || (check_ep && !ep) || !udev) {
1187 pr_debug("xHCI %s called with invalid args\n", func);
1188 return -EINVAL;
1190 if (!udev->parent) {
1191 pr_debug("xHCI %s called for root hub\n", func);
1192 return 0;
1195 xhci = hcd_to_xhci(hcd);
1196 if (check_virt_dev) {
1197 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
1198 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
1199 func);
1200 return -EINVAL;
1203 virt_dev = xhci->devs[udev->slot_id];
1204 if (virt_dev->udev != udev) {
1205 xhci_dbg(xhci, "xHCI %s called with udev and "
1206 "virt_dev does not match\n", func);
1207 return -EINVAL;
1211 if (xhci->xhc_state & XHCI_STATE_HALTED)
1212 return -ENODEV;
1214 return 1;
1217 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1218 struct usb_device *udev, struct xhci_command *command,
1219 bool ctx_change, bool must_succeed);
1222 * Full speed devices may have a max packet size greater than 8 bytes, but the
1223 * USB core doesn't know that until it reads the first 8 bytes of the
1224 * descriptor. If the usb_device's max packet size changes after that point,
1225 * we need to issue an evaluate context command and wait on it.
1227 static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1228 unsigned int ep_index, struct urb *urb)
1230 struct xhci_container_ctx *in_ctx;
1231 struct xhci_container_ctx *out_ctx;
1232 struct xhci_input_control_ctx *ctrl_ctx;
1233 struct xhci_ep_ctx *ep_ctx;
1234 int max_packet_size;
1235 int hw_max_packet_size;
1236 int ret = 0;
1238 out_ctx = xhci->devs[slot_id]->out_ctx;
1239 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1240 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
1241 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
1242 if (hw_max_packet_size != max_packet_size) {
1243 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1244 "Max Packet Size for ep 0 changed.");
1245 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1246 "Max packet size in usb_device = %d",
1247 max_packet_size);
1248 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1249 "Max packet size in xHCI HW = %d",
1250 hw_max_packet_size);
1251 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1252 "Issuing evaluate context command.");
1254 /* Set up the input context flags for the command */
1255 /* FIXME: This won't work if a non-default control endpoint
1256 * changes max packet sizes.
1258 in_ctx = xhci->devs[slot_id]->in_ctx;
1259 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1260 if (!ctrl_ctx) {
1261 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1262 __func__);
1263 return -ENOMEM;
1265 /* Set up the modified control endpoint 0 */
1266 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1267 xhci->devs[slot_id]->out_ctx, ep_index);
1269 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1270 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1271 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1273 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
1274 ctrl_ctx->drop_flags = 0;
1276 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
1277 xhci_dbg_ctx(xhci, in_ctx, ep_index);
1278 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
1279 xhci_dbg_ctx(xhci, out_ctx, ep_index);
1281 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
1282 true, false);
1284 /* Clean up the input context for later use by bandwidth
1285 * functions.
1287 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
1289 return ret;
1293 * non-error returns are a promise to giveback() the urb later
1294 * we drop ownership so next owner (or urb unlink) can get it
1296 int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1298 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1299 struct xhci_td *buffer;
1300 unsigned long flags;
1301 int ret = 0;
1302 unsigned int slot_id, ep_index;
1303 struct urb_priv *urb_priv;
1304 int size, i;
1306 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1307 true, true, __func__) <= 0)
1308 return -EINVAL;
1310 slot_id = urb->dev->slot_id;
1311 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1313 if (!HCD_HW_ACCESSIBLE(hcd)) {
1314 if (!in_interrupt())
1315 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1316 ret = -ESHUTDOWN;
1317 goto exit;
1320 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1321 size = urb->number_of_packets;
1322 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
1323 urb->transfer_buffer_length > 0 &&
1324 urb->transfer_flags & URB_ZERO_PACKET &&
1325 !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
1326 size = 2;
1327 else
1328 size = 1;
1330 urb_priv = kzalloc(sizeof(struct urb_priv) +
1331 size * sizeof(struct xhci_td *), mem_flags);
1332 if (!urb_priv)
1333 return -ENOMEM;
1335 buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
1336 if (!buffer) {
1337 kfree(urb_priv);
1338 return -ENOMEM;
1341 for (i = 0; i < size; i++) {
1342 urb_priv->td[i] = buffer;
1343 buffer++;
1346 urb_priv->length = size;
1347 urb_priv->td_cnt = 0;
1348 urb->hcpriv = urb_priv;
1350 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1351 /* Check to see if the max packet size for the default control
1352 * endpoint changed during FS device enumeration
1354 if (urb->dev->speed == USB_SPEED_FULL) {
1355 ret = xhci_check_maxpacket(xhci, slot_id,
1356 ep_index, urb);
1357 if (ret < 0) {
1358 xhci_urb_free_priv(xhci, urb_priv);
1359 urb->hcpriv = NULL;
1360 return ret;
1364 /* We have a spinlock and interrupts disabled, so we must pass
1365 * atomic context to this function, which may allocate memory.
1367 spin_lock_irqsave(&xhci->lock, flags);
1368 if (xhci->xhc_state & XHCI_STATE_DYING)
1369 goto dying;
1370 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
1371 slot_id, ep_index);
1372 if (ret)
1373 goto free_priv;
1374 spin_unlock_irqrestore(&xhci->lock, flags);
1375 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1376 spin_lock_irqsave(&xhci->lock, flags);
1377 if (xhci->xhc_state & XHCI_STATE_DYING)
1378 goto dying;
1379 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1380 EP_GETTING_STREAMS) {
1381 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1382 "is transitioning to using streams.\n");
1383 ret = -EINVAL;
1384 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1385 EP_GETTING_NO_STREAMS) {
1386 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1387 "is transitioning to "
1388 "not having streams.\n");
1389 ret = -EINVAL;
1390 } else {
1391 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1392 slot_id, ep_index);
1394 if (ret)
1395 goto free_priv;
1396 spin_unlock_irqrestore(&xhci->lock, flags);
1397 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1398 spin_lock_irqsave(&xhci->lock, flags);
1399 if (xhci->xhc_state & XHCI_STATE_DYING)
1400 goto dying;
1401 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1402 slot_id, ep_index);
1403 if (ret)
1404 goto free_priv;
1405 spin_unlock_irqrestore(&xhci->lock, flags);
1406 } else {
1407 spin_lock_irqsave(&xhci->lock, flags);
1408 if (xhci->xhc_state & XHCI_STATE_DYING)
1409 goto dying;
1410 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1411 slot_id, ep_index);
1412 if (ret)
1413 goto free_priv;
1414 spin_unlock_irqrestore(&xhci->lock, flags);
1416 exit:
1417 return ret;
1418 dying:
1419 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1420 "non-responsive xHCI host.\n",
1421 urb->ep->desc.bEndpointAddress, urb);
1422 ret = -ESHUTDOWN;
1423 free_priv:
1424 xhci_urb_free_priv(xhci, urb_priv);
1425 urb->hcpriv = NULL;
1426 spin_unlock_irqrestore(&xhci->lock, flags);
1427 return ret;
1430 /* Get the right ring for the given URB.
1431 * If the endpoint supports streams, boundary check the URB's stream ID.
1432 * If the endpoint doesn't support streams, return the singular endpoint ring.
1434 static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1435 struct urb *urb)
1437 unsigned int slot_id;
1438 unsigned int ep_index;
1439 unsigned int stream_id;
1440 struct xhci_virt_ep *ep;
1442 slot_id = urb->dev->slot_id;
1443 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1444 stream_id = urb->stream_id;
1445 ep = &xhci->devs[slot_id]->eps[ep_index];
1446 /* Common case: no streams */
1447 if (!(ep->ep_state & EP_HAS_STREAMS))
1448 return ep->ring;
1450 if (stream_id == 0) {
1451 xhci_warn(xhci,
1452 "WARN: Slot ID %u, ep index %u has streams, "
1453 "but URB has no stream ID.\n",
1454 slot_id, ep_index);
1455 return NULL;
1458 if (stream_id < ep->stream_info->num_streams)
1459 return ep->stream_info->stream_rings[stream_id];
1461 xhci_warn(xhci,
1462 "WARN: Slot ID %u, ep index %u has "
1463 "stream IDs 1 to %u allocated, "
1464 "but stream ID %u is requested.\n",
1465 slot_id, ep_index,
1466 ep->stream_info->num_streams - 1,
1467 stream_id);
1468 return NULL;
1472 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1473 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1474 * should pick up where it left off in the TD, unless a Set Transfer Ring
1475 * Dequeue Pointer is issued.
1477 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1478 * the ring. Since the ring is a contiguous structure, they can't be physically
1479 * removed. Instead, there are two options:
1481 * 1) If the HC is in the middle of processing the URB to be canceled, we
1482 * simply move the ring's dequeue pointer past those TRBs using the Set
1483 * Transfer Ring Dequeue Pointer command. This will be the common case,
1484 * when drivers timeout on the last submitted URB and attempt to cancel.
1486 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1487 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1488 * HC will need to invalidate the any TRBs it has cached after the stop
1489 * endpoint command, as noted in the xHCI 0.95 errata.
1491 * 3) The TD may have completed by the time the Stop Endpoint Command
1492 * completes, so software needs to handle that case too.
1494 * This function should protect against the TD enqueueing code ringing the
1495 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1496 * It also needs to account for multiple cancellations on happening at the same
1497 * time for the same endpoint.
1499 * Note that this function can be called in any context, or so says
1500 * usb_hcd_unlink_urb()
1502 int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1504 unsigned long flags;
1505 int ret, i;
1506 u32 temp;
1507 struct xhci_hcd *xhci;
1508 struct urb_priv *urb_priv;
1509 struct xhci_td *td;
1510 unsigned int ep_index;
1511 struct xhci_ring *ep_ring;
1512 struct xhci_virt_ep *ep;
1514 xhci = hcd_to_xhci(hcd);
1515 spin_lock_irqsave(&xhci->lock, flags);
1516 /* Make sure the URB hasn't completed or been unlinked already */
1517 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1518 if (ret || !urb->hcpriv)
1519 goto done;
1520 temp = xhci_readl(xhci, &xhci->op_regs->status);
1521 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
1522 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1523 "HW died, freeing TD.");
1524 urb_priv = urb->hcpriv;
1525 for (i = urb_priv->td_cnt;
1526 i < urb_priv->length && xhci->devs[urb->dev->slot_id];
1527 i++) {
1528 td = urb_priv->td[i];
1529 if (!list_empty(&td->td_list))
1530 list_del_init(&td->td_list);
1531 if (!list_empty(&td->cancelled_td_list))
1532 list_del_init(&td->cancelled_td_list);
1535 usb_hcd_unlink_urb_from_ep(hcd, urb);
1536 spin_unlock_irqrestore(&xhci->lock, flags);
1537 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
1538 xhci_urb_free_priv(xhci, urb_priv);
1539 return ret;
1541 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
1542 (xhci->xhc_state & XHCI_STATE_HALTED)) {
1543 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1544 "Ep 0x%x: URB %p to be canceled on "
1545 "non-responsive xHCI host.",
1546 urb->ep->desc.bEndpointAddress, urb);
1547 /* Let the stop endpoint command watchdog timer (which set this
1548 * state) finish cleaning up the endpoint TD lists. We must
1549 * have caught it in the middle of dropping a lock and giving
1550 * back an URB.
1552 goto done;
1555 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1556 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
1557 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1558 if (!ep_ring) {
1559 ret = -EINVAL;
1560 goto done;
1563 urb_priv = urb->hcpriv;
1564 i = urb_priv->td_cnt;
1565 if (i < urb_priv->length)
1566 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1567 "Cancel URB %p, dev %s, ep 0x%x, "
1568 "starting at offset 0x%llx",
1569 urb, urb->dev->devpath,
1570 urb->ep->desc.bEndpointAddress,
1571 (unsigned long long) xhci_trb_virt_to_dma(
1572 urb_priv->td[i]->start_seg,
1573 urb_priv->td[i]->first_trb));
1575 for (; i < urb_priv->length; i++) {
1576 td = urb_priv->td[i];
1577 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1580 /* Queue a stop endpoint command, but only if this is
1581 * the first cancellation to be handled.
1583 if (!(ep->ep_state & EP_HALT_PENDING)) {
1584 ep->ep_state |= EP_HALT_PENDING;
1585 ep->stop_cmds_pending++;
1586 ep->stop_cmd_timer.expires = jiffies +
1587 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1588 add_timer(&ep->stop_cmd_timer);
1589 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
1590 xhci_ring_cmd_db(xhci);
1592 done:
1593 spin_unlock_irqrestore(&xhci->lock, flags);
1594 return ret;
1597 /* Drop an endpoint from a new bandwidth configuration for this device.
1598 * Only one call to this function is allowed per endpoint before
1599 * check_bandwidth() or reset_bandwidth() must be called.
1600 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1601 * add the endpoint to the schedule with possibly new parameters denoted by a
1602 * different endpoint descriptor in usb_host_endpoint.
1603 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1604 * not allowed.
1606 * The USB core will not allow URBs to be queued to an endpoint that is being
1607 * disabled, so there's no need for mutual exclusion to protect
1608 * the xhci->devs[slot_id] structure.
1610 int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1611 struct usb_host_endpoint *ep)
1613 struct xhci_hcd *xhci;
1614 struct xhci_container_ctx *in_ctx, *out_ctx;
1615 struct xhci_input_control_ctx *ctrl_ctx;
1616 struct xhci_slot_ctx *slot_ctx;
1617 unsigned int last_ctx;
1618 unsigned int ep_index;
1619 struct xhci_ep_ctx *ep_ctx;
1620 u32 drop_flag;
1621 u32 new_add_flags, new_drop_flags, new_slot_info;
1622 int ret;
1624 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
1625 if (ret <= 0)
1626 return ret;
1627 xhci = hcd_to_xhci(hcd);
1628 if (xhci->xhc_state & XHCI_STATE_DYING)
1629 return -ENODEV;
1631 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1632 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1633 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1634 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1635 __func__, drop_flag);
1636 return 0;
1639 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
1640 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1641 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1642 if (!ctrl_ctx) {
1643 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1644 __func__);
1645 return 0;
1648 ep_index = xhci_get_endpoint_index(&ep->desc);
1649 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1650 /* If the HC already knows the endpoint is disabled,
1651 * or the HCD has noted it is disabled, ignore this request
1653 if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1654 cpu_to_le32(EP_STATE_DISABLED)) ||
1655 le32_to_cpu(ctrl_ctx->drop_flags) &
1656 xhci_get_endpoint_flag(&ep->desc)) {
1657 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1658 __func__, ep);
1659 return 0;
1662 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1663 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1665 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1666 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1668 last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
1669 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1670 /* Update the last valid endpoint context, if we deleted the last one */
1671 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
1672 LAST_CTX(last_ctx)) {
1673 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1674 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
1676 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
1678 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1680 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1681 (unsigned int) ep->desc.bEndpointAddress,
1682 udev->slot_id,
1683 (unsigned int) new_drop_flags,
1684 (unsigned int) new_add_flags,
1685 (unsigned int) new_slot_info);
1686 return 0;
1689 /* Add an endpoint to a new possible bandwidth configuration for this device.
1690 * Only one call to this function is allowed per endpoint before
1691 * check_bandwidth() or reset_bandwidth() must be called.
1692 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1693 * add the endpoint to the schedule with possibly new parameters denoted by a
1694 * different endpoint descriptor in usb_host_endpoint.
1695 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1696 * not allowed.
1698 * The USB core will not allow URBs to be queued to an endpoint until the
1699 * configuration or alt setting is installed in the device, so there's no need
1700 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1702 int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1703 struct usb_host_endpoint *ep)
1705 struct xhci_hcd *xhci;
1706 struct xhci_container_ctx *in_ctx, *out_ctx;
1707 unsigned int ep_index;
1708 struct xhci_slot_ctx *slot_ctx;
1709 struct xhci_input_control_ctx *ctrl_ctx;
1710 u32 added_ctxs;
1711 unsigned int last_ctx;
1712 u32 new_add_flags, new_drop_flags, new_slot_info;
1713 struct xhci_virt_device *virt_dev;
1714 int ret = 0;
1716 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
1717 if (ret <= 0) {
1718 /* So we won't queue a reset ep command for a root hub */
1719 ep->hcpriv = NULL;
1720 return ret;
1722 xhci = hcd_to_xhci(hcd);
1723 if (xhci->xhc_state & XHCI_STATE_DYING)
1724 return -ENODEV;
1726 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1727 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1728 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1729 /* FIXME when we have to issue an evaluate endpoint command to
1730 * deal with ep0 max packet size changing once we get the
1731 * descriptors
1733 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1734 __func__, added_ctxs);
1735 return 0;
1738 virt_dev = xhci->devs[udev->slot_id];
1739 in_ctx = virt_dev->in_ctx;
1740 out_ctx = virt_dev->out_ctx;
1741 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1742 if (!ctrl_ctx) {
1743 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1744 __func__);
1745 return 0;
1748 ep_index = xhci_get_endpoint_index(&ep->desc);
1749 /* If this endpoint is already in use, and the upper layers are trying
1750 * to add it again without dropping it, reject the addition.
1752 if (virt_dev->eps[ep_index].ring &&
1753 !(le32_to_cpu(ctrl_ctx->drop_flags) &
1754 xhci_get_endpoint_flag(&ep->desc))) {
1755 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1756 "without dropping it.\n",
1757 (unsigned int) ep->desc.bEndpointAddress);
1758 return -EINVAL;
1761 /* If the HCD has already noted the endpoint is enabled,
1762 * ignore this request.
1764 if (le32_to_cpu(ctrl_ctx->add_flags) &
1765 xhci_get_endpoint_flag(&ep->desc)) {
1766 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1767 __func__, ep);
1768 return 0;
1772 * Configuration and alternate setting changes must be done in
1773 * process context, not interrupt context (or so documenation
1774 * for usb_set_interface() and usb_set_configuration() claim).
1776 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
1777 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1778 __func__, ep->desc.bEndpointAddress);
1779 return -ENOMEM;
1782 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1783 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1785 /* If xhci_endpoint_disable() was called for this endpoint, but the
1786 * xHC hasn't been notified yet through the check_bandwidth() call,
1787 * this re-adds a new state for the endpoint from the new endpoint
1788 * descriptors. We must drop and re-add this endpoint, so we leave the
1789 * drop flags alone.
1791 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1793 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1794 /* Update the last valid endpoint context, if we just added one past */
1795 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
1796 LAST_CTX(last_ctx)) {
1797 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1798 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
1800 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
1802 /* Store the usb_device pointer for later use */
1803 ep->hcpriv = udev;
1805 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1806 (unsigned int) ep->desc.bEndpointAddress,
1807 udev->slot_id,
1808 (unsigned int) new_drop_flags,
1809 (unsigned int) new_add_flags,
1810 (unsigned int) new_slot_info);
1811 return 0;
1814 static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
1816 struct xhci_input_control_ctx *ctrl_ctx;
1817 struct xhci_ep_ctx *ep_ctx;
1818 struct xhci_slot_ctx *slot_ctx;
1819 int i;
1821 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1822 if (!ctrl_ctx) {
1823 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1824 __func__);
1825 return;
1828 /* When a device's add flag and drop flag are zero, any subsequent
1829 * configure endpoint command will leave that endpoint's state
1830 * untouched. Make sure we don't leave any old state in the input
1831 * endpoint contexts.
1833 ctrl_ctx->drop_flags = 0;
1834 ctrl_ctx->add_flags = 0;
1835 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1836 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1837 /* Endpoint 0 is always valid */
1838 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
1839 for (i = 1; i < 31; ++i) {
1840 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
1841 ep_ctx->ep_info = 0;
1842 ep_ctx->ep_info2 = 0;
1843 ep_ctx->deq = 0;
1844 ep_ctx->tx_info = 0;
1848 static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
1849 struct usb_device *udev, u32 *cmd_status)
1851 int ret;
1853 switch (*cmd_status) {
1854 case COMP_ENOMEM:
1855 dev_warn(&udev->dev, "Not enough host controller resources "
1856 "for new device state.\n");
1857 ret = -ENOMEM;
1858 /* FIXME: can we allocate more resources for the HC? */
1859 break;
1860 case COMP_BW_ERR:
1861 case COMP_2ND_BW_ERR:
1862 dev_warn(&udev->dev, "Not enough bandwidth "
1863 "for new device state.\n");
1864 ret = -ENOSPC;
1865 /* FIXME: can we go back to the old state? */
1866 break;
1867 case COMP_TRB_ERR:
1868 /* the HCD set up something wrong */
1869 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1870 "add flag = 1, "
1871 "and endpoint is not disabled.\n");
1872 ret = -EINVAL;
1873 break;
1874 case COMP_DEV_ERR:
1875 dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
1876 "configure command.\n");
1877 ret = -ENODEV;
1878 break;
1879 case COMP_SUCCESS:
1880 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1881 "Successful Endpoint Configure command");
1882 ret = 0;
1883 break;
1884 default:
1885 xhci_err(xhci, "ERROR: unexpected command completion "
1886 "code 0x%x.\n", *cmd_status);
1887 ret = -EINVAL;
1888 break;
1890 return ret;
1893 static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
1894 struct usb_device *udev, u32 *cmd_status)
1896 int ret;
1897 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
1899 switch (*cmd_status) {
1900 case COMP_EINVAL:
1901 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1902 "context command.\n");
1903 ret = -EINVAL;
1904 break;
1905 case COMP_EBADSLT:
1906 dev_warn(&udev->dev, "WARN: slot not enabled for"
1907 "evaluate context command.\n");
1908 ret = -EINVAL;
1909 break;
1910 case COMP_CTX_STATE:
1911 dev_warn(&udev->dev, "WARN: invalid context state for "
1912 "evaluate context command.\n");
1913 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1914 ret = -EINVAL;
1915 break;
1916 case COMP_DEV_ERR:
1917 dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
1918 "context command.\n");
1919 ret = -ENODEV;
1920 break;
1921 case COMP_MEL_ERR:
1922 /* Max Exit Latency too large error */
1923 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1924 ret = -EINVAL;
1925 break;
1926 case COMP_SUCCESS:
1927 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1928 "Successful evaluate context command");
1929 ret = 0;
1930 break;
1931 default:
1932 xhci_err(xhci, "ERROR: unexpected command completion "
1933 "code 0x%x.\n", *cmd_status);
1934 ret = -EINVAL;
1935 break;
1937 return ret;
1940 static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
1941 struct xhci_input_control_ctx *ctrl_ctx)
1943 u32 valid_add_flags;
1944 u32 valid_drop_flags;
1946 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1947 * (bit 1). The default control endpoint is added during the Address
1948 * Device command and is never removed until the slot is disabled.
1950 valid_add_flags = ctrl_ctx->add_flags >> 2;
1951 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1953 /* Use hweight32 to count the number of ones in the add flags, or
1954 * number of endpoints added. Don't count endpoints that are changed
1955 * (both added and dropped).
1957 return hweight32(valid_add_flags) -
1958 hweight32(valid_add_flags & valid_drop_flags);
1961 static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
1962 struct xhci_input_control_ctx *ctrl_ctx)
1964 u32 valid_add_flags;
1965 u32 valid_drop_flags;
1967 valid_add_flags = ctrl_ctx->add_flags >> 2;
1968 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1970 return hweight32(valid_drop_flags) -
1971 hweight32(valid_add_flags & valid_drop_flags);
1975 * We need to reserve the new number of endpoints before the configure endpoint
1976 * command completes. We can't subtract the dropped endpoints from the number
1977 * of active endpoints until the command completes because we can oversubscribe
1978 * the host in this case:
1980 * - the first configure endpoint command drops more endpoints than it adds
1981 * - a second configure endpoint command that adds more endpoints is queued
1982 * - the first configure endpoint command fails, so the config is unchanged
1983 * - the second command may succeed, even though there isn't enough resources
1985 * Must be called with xhci->lock held.
1987 static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
1988 struct xhci_input_control_ctx *ctrl_ctx)
1990 u32 added_eps;
1992 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
1993 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
1994 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1995 "Not enough ep ctxs: "
1996 "%u active, need to add %u, limit is %u.",
1997 xhci->num_active_eps, added_eps,
1998 xhci->limit_active_eps);
1999 return -ENOMEM;
2001 xhci->num_active_eps += added_eps;
2002 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2003 "Adding %u ep ctxs, %u now active.", added_eps,
2004 xhci->num_active_eps);
2005 return 0;
2009 * The configure endpoint was failed by the xHC for some other reason, so we
2010 * need to revert the resources that failed configuration would have used.
2012 * Must be called with xhci->lock held.
2014 static void xhci_free_host_resources(struct xhci_hcd *xhci,
2015 struct xhci_input_control_ctx *ctrl_ctx)
2017 u32 num_failed_eps;
2019 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
2020 xhci->num_active_eps -= num_failed_eps;
2021 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2022 "Removing %u failed ep ctxs, %u now active.",
2023 num_failed_eps,
2024 xhci->num_active_eps);
2028 * Now that the command has completed, clean up the active endpoint count by
2029 * subtracting out the endpoints that were dropped (but not changed).
2031 * Must be called with xhci->lock held.
2033 static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
2034 struct xhci_input_control_ctx *ctrl_ctx)
2036 u32 num_dropped_eps;
2038 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
2039 xhci->num_active_eps -= num_dropped_eps;
2040 if (num_dropped_eps)
2041 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2042 "Removing %u dropped ep ctxs, %u now active.",
2043 num_dropped_eps,
2044 xhci->num_active_eps);
2047 static unsigned int xhci_get_block_size(struct usb_device *udev)
2049 switch (udev->speed) {
2050 case USB_SPEED_LOW:
2051 case USB_SPEED_FULL:
2052 return FS_BLOCK;
2053 case USB_SPEED_HIGH:
2054 return HS_BLOCK;
2055 case USB_SPEED_SUPER:
2056 case USB_SPEED_SUPER_PLUS:
2057 return SS_BLOCK;
2058 case USB_SPEED_UNKNOWN:
2059 case USB_SPEED_WIRELESS:
2060 default:
2061 /* Should never happen */
2062 return 1;
2066 static unsigned int
2067 xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
2069 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2070 return LS_OVERHEAD;
2071 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2072 return FS_OVERHEAD;
2073 return HS_OVERHEAD;
2076 /* If we are changing a LS/FS device under a HS hub,
2077 * make sure (if we are activating a new TT) that the HS bus has enough
2078 * bandwidth for this new TT.
2080 static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2081 struct xhci_virt_device *virt_dev,
2082 int old_active_eps)
2084 struct xhci_interval_bw_table *bw_table;
2085 struct xhci_tt_bw_info *tt_info;
2087 /* Find the bandwidth table for the root port this TT is attached to. */
2088 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2089 tt_info = virt_dev->tt_info;
2090 /* If this TT already had active endpoints, the bandwidth for this TT
2091 * has already been added. Removing all periodic endpoints (and thus
2092 * making the TT enactive) will only decrease the bandwidth used.
2094 if (old_active_eps)
2095 return 0;
2096 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2097 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2098 return -ENOMEM;
2099 return 0;
2101 /* Not sure why we would have no new active endpoints...
2103 * Maybe because of an Evaluate Context change for a hub update or a
2104 * control endpoint 0 max packet size change?
2105 * FIXME: skip the bandwidth calculation in that case.
2107 return 0;
2110 static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2111 struct xhci_virt_device *virt_dev)
2113 unsigned int bw_reserved;
2115 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2116 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2117 return -ENOMEM;
2119 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2120 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2121 return -ENOMEM;
2123 return 0;
2127 * This algorithm is a very conservative estimate of the worst-case scheduling
2128 * scenario for any one interval. The hardware dynamically schedules the
2129 * packets, so we can't tell which microframe could be the limiting factor in
2130 * the bandwidth scheduling. This only takes into account periodic endpoints.
2132 * Obviously, we can't solve an NP complete problem to find the minimum worst
2133 * case scenario. Instead, we come up with an estimate that is no less than
2134 * the worst case bandwidth used for any one microframe, but may be an
2135 * over-estimate.
2137 * We walk the requirements for each endpoint by interval, starting with the
2138 * smallest interval, and place packets in the schedule where there is only one
2139 * possible way to schedule packets for that interval. In order to simplify
2140 * this algorithm, we record the largest max packet size for each interval, and
2141 * assume all packets will be that size.
2143 * For interval 0, we obviously must schedule all packets for each interval.
2144 * The bandwidth for interval 0 is just the amount of data to be transmitted
2145 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2146 * the number of packets).
2148 * For interval 1, we have two possible microframes to schedule those packets
2149 * in. For this algorithm, if we can schedule the same number of packets for
2150 * each possible scheduling opportunity (each microframe), we will do so. The
2151 * remaining number of packets will be saved to be transmitted in the gaps in
2152 * the next interval's scheduling sequence.
2154 * As we move those remaining packets to be scheduled with interval 2 packets,
2155 * we have to double the number of remaining packets to transmit. This is
2156 * because the intervals are actually powers of 2, and we would be transmitting
2157 * the previous interval's packets twice in this interval. We also have to be
2158 * sure that when we look at the largest max packet size for this interval, we
2159 * also look at the largest max packet size for the remaining packets and take
2160 * the greater of the two.
2162 * The algorithm continues to evenly distribute packets in each scheduling
2163 * opportunity, and push the remaining packets out, until we get to the last
2164 * interval. Then those packets and their associated overhead are just added
2165 * to the bandwidth used.
2167 static int xhci_check_bw_table(struct xhci_hcd *xhci,
2168 struct xhci_virt_device *virt_dev,
2169 int old_active_eps)
2171 unsigned int bw_reserved;
2172 unsigned int max_bandwidth;
2173 unsigned int bw_used;
2174 unsigned int block_size;
2175 struct xhci_interval_bw_table *bw_table;
2176 unsigned int packet_size = 0;
2177 unsigned int overhead = 0;
2178 unsigned int packets_transmitted = 0;
2179 unsigned int packets_remaining = 0;
2180 unsigned int i;
2182 if (virt_dev->udev->speed >= USB_SPEED_SUPER)
2183 return xhci_check_ss_bw(xhci, virt_dev);
2185 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2186 max_bandwidth = HS_BW_LIMIT;
2187 /* Convert percent of bus BW reserved to blocks reserved */
2188 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2189 } else {
2190 max_bandwidth = FS_BW_LIMIT;
2191 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2194 bw_table = virt_dev->bw_table;
2195 /* We need to translate the max packet size and max ESIT payloads into
2196 * the units the hardware uses.
2198 block_size = xhci_get_block_size(virt_dev->udev);
2200 /* If we are manipulating a LS/FS device under a HS hub, double check
2201 * that the HS bus has enough bandwidth if we are activing a new TT.
2203 if (virt_dev->tt_info) {
2204 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2205 "Recalculating BW for rootport %u",
2206 virt_dev->real_port);
2207 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2208 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2209 "newly activated TT.\n");
2210 return -ENOMEM;
2212 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2213 "Recalculating BW for TT slot %u port %u",
2214 virt_dev->tt_info->slot_id,
2215 virt_dev->tt_info->ttport);
2216 } else {
2217 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2218 "Recalculating BW for rootport %u",
2219 virt_dev->real_port);
2222 /* Add in how much bandwidth will be used for interval zero, or the
2223 * rounded max ESIT payload + number of packets * largest overhead.
2225 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2226 bw_table->interval_bw[0].num_packets *
2227 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2229 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2230 unsigned int bw_added;
2231 unsigned int largest_mps;
2232 unsigned int interval_overhead;
2235 * How many packets could we transmit in this interval?
2236 * If packets didn't fit in the previous interval, we will need
2237 * to transmit that many packets twice within this interval.
2239 packets_remaining = 2 * packets_remaining +
2240 bw_table->interval_bw[i].num_packets;
2242 /* Find the largest max packet size of this or the previous
2243 * interval.
2245 if (list_empty(&bw_table->interval_bw[i].endpoints))
2246 largest_mps = 0;
2247 else {
2248 struct xhci_virt_ep *virt_ep;
2249 struct list_head *ep_entry;
2251 ep_entry = bw_table->interval_bw[i].endpoints.next;
2252 virt_ep = list_entry(ep_entry,
2253 struct xhci_virt_ep, bw_endpoint_list);
2254 /* Convert to blocks, rounding up */
2255 largest_mps = DIV_ROUND_UP(
2256 virt_ep->bw_info.max_packet_size,
2257 block_size);
2259 if (largest_mps > packet_size)
2260 packet_size = largest_mps;
2262 /* Use the larger overhead of this or the previous interval. */
2263 interval_overhead = xhci_get_largest_overhead(
2264 &bw_table->interval_bw[i]);
2265 if (interval_overhead > overhead)
2266 overhead = interval_overhead;
2268 /* How many packets can we evenly distribute across
2269 * (1 << (i + 1)) possible scheduling opportunities?
2271 packets_transmitted = packets_remaining >> (i + 1);
2273 /* Add in the bandwidth used for those scheduled packets */
2274 bw_added = packets_transmitted * (overhead + packet_size);
2276 /* How many packets do we have remaining to transmit? */
2277 packets_remaining = packets_remaining % (1 << (i + 1));
2279 /* What largest max packet size should those packets have? */
2280 /* If we've transmitted all packets, don't carry over the
2281 * largest packet size.
2283 if (packets_remaining == 0) {
2284 packet_size = 0;
2285 overhead = 0;
2286 } else if (packets_transmitted > 0) {
2287 /* Otherwise if we do have remaining packets, and we've
2288 * scheduled some packets in this interval, take the
2289 * largest max packet size from endpoints with this
2290 * interval.
2292 packet_size = largest_mps;
2293 overhead = interval_overhead;
2295 /* Otherwise carry over packet_size and overhead from the last
2296 * time we had a remainder.
2298 bw_used += bw_added;
2299 if (bw_used > max_bandwidth) {
2300 xhci_warn(xhci, "Not enough bandwidth. "
2301 "Proposed: %u, Max: %u\n",
2302 bw_used, max_bandwidth);
2303 return -ENOMEM;
2307 * Ok, we know we have some packets left over after even-handedly
2308 * scheduling interval 15. We don't know which microframes they will
2309 * fit into, so we over-schedule and say they will be scheduled every
2310 * microframe.
2312 if (packets_remaining > 0)
2313 bw_used += overhead + packet_size;
2315 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2316 unsigned int port_index = virt_dev->real_port - 1;
2318 /* OK, we're manipulating a HS device attached to a
2319 * root port bandwidth domain. Include the number of active TTs
2320 * in the bandwidth used.
2322 bw_used += TT_HS_OVERHEAD *
2323 xhci->rh_bw[port_index].num_active_tts;
2326 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2327 "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2328 "Available: %u " "percent",
2329 bw_used, max_bandwidth, bw_reserved,
2330 (max_bandwidth - bw_used - bw_reserved) * 100 /
2331 max_bandwidth);
2333 bw_used += bw_reserved;
2334 if (bw_used > max_bandwidth) {
2335 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2336 bw_used, max_bandwidth);
2337 return -ENOMEM;
2340 bw_table->bw_used = bw_used;
2341 return 0;
2344 static bool xhci_is_async_ep(unsigned int ep_type)
2346 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2347 ep_type != ISOC_IN_EP &&
2348 ep_type != INT_IN_EP);
2351 static bool xhci_is_sync_in_ep(unsigned int ep_type)
2353 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
2356 static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2358 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2360 if (ep_bw->ep_interval == 0)
2361 return SS_OVERHEAD_BURST +
2362 (ep_bw->mult * ep_bw->num_packets *
2363 (SS_OVERHEAD + mps));
2364 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2365 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2366 1 << ep_bw->ep_interval);
2370 void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2371 struct xhci_bw_info *ep_bw,
2372 struct xhci_interval_bw_table *bw_table,
2373 struct usb_device *udev,
2374 struct xhci_virt_ep *virt_ep,
2375 struct xhci_tt_bw_info *tt_info)
2377 struct xhci_interval_bw *interval_bw;
2378 int normalized_interval;
2380 if (xhci_is_async_ep(ep_bw->type))
2381 return;
2383 if (udev->speed >= USB_SPEED_SUPER) {
2384 if (xhci_is_sync_in_ep(ep_bw->type))
2385 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2386 xhci_get_ss_bw_consumed(ep_bw);
2387 else
2388 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2389 xhci_get_ss_bw_consumed(ep_bw);
2390 return;
2393 /* SuperSpeed endpoints never get added to intervals in the table, so
2394 * this check is only valid for HS/FS/LS devices.
2396 if (list_empty(&virt_ep->bw_endpoint_list))
2397 return;
2398 /* For LS/FS devices, we need to translate the interval expressed in
2399 * microframes to frames.
2401 if (udev->speed == USB_SPEED_HIGH)
2402 normalized_interval = ep_bw->ep_interval;
2403 else
2404 normalized_interval = ep_bw->ep_interval - 3;
2406 if (normalized_interval == 0)
2407 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2408 interval_bw = &bw_table->interval_bw[normalized_interval];
2409 interval_bw->num_packets -= ep_bw->num_packets;
2410 switch (udev->speed) {
2411 case USB_SPEED_LOW:
2412 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2413 break;
2414 case USB_SPEED_FULL:
2415 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2416 break;
2417 case USB_SPEED_HIGH:
2418 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2419 break;
2420 case USB_SPEED_SUPER:
2421 case USB_SPEED_SUPER_PLUS:
2422 case USB_SPEED_UNKNOWN:
2423 case USB_SPEED_WIRELESS:
2424 /* Should never happen because only LS/FS/HS endpoints will get
2425 * added to the endpoint list.
2427 return;
2429 if (tt_info)
2430 tt_info->active_eps -= 1;
2431 list_del_init(&virt_ep->bw_endpoint_list);
2434 static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2435 struct xhci_bw_info *ep_bw,
2436 struct xhci_interval_bw_table *bw_table,
2437 struct usb_device *udev,
2438 struct xhci_virt_ep *virt_ep,
2439 struct xhci_tt_bw_info *tt_info)
2441 struct xhci_interval_bw *interval_bw;
2442 struct xhci_virt_ep *smaller_ep;
2443 int normalized_interval;
2445 if (xhci_is_async_ep(ep_bw->type))
2446 return;
2448 if (udev->speed == USB_SPEED_SUPER) {
2449 if (xhci_is_sync_in_ep(ep_bw->type))
2450 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2451 xhci_get_ss_bw_consumed(ep_bw);
2452 else
2453 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2454 xhci_get_ss_bw_consumed(ep_bw);
2455 return;
2458 /* For LS/FS devices, we need to translate the interval expressed in
2459 * microframes to frames.
2461 if (udev->speed == USB_SPEED_HIGH)
2462 normalized_interval = ep_bw->ep_interval;
2463 else
2464 normalized_interval = ep_bw->ep_interval - 3;
2466 if (normalized_interval == 0)
2467 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2468 interval_bw = &bw_table->interval_bw[normalized_interval];
2469 interval_bw->num_packets += ep_bw->num_packets;
2470 switch (udev->speed) {
2471 case USB_SPEED_LOW:
2472 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2473 break;
2474 case USB_SPEED_FULL:
2475 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2476 break;
2477 case USB_SPEED_HIGH:
2478 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2479 break;
2480 case USB_SPEED_SUPER:
2481 case USB_SPEED_SUPER_PLUS:
2482 case USB_SPEED_UNKNOWN:
2483 case USB_SPEED_WIRELESS:
2484 /* Should never happen because only LS/FS/HS endpoints will get
2485 * added to the endpoint list.
2487 return;
2490 if (tt_info)
2491 tt_info->active_eps += 1;
2492 /* Insert the endpoint into the list, largest max packet size first. */
2493 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2494 bw_endpoint_list) {
2495 if (ep_bw->max_packet_size >=
2496 smaller_ep->bw_info.max_packet_size) {
2497 /* Add the new ep before the smaller endpoint */
2498 list_add_tail(&virt_ep->bw_endpoint_list,
2499 &smaller_ep->bw_endpoint_list);
2500 return;
2503 /* Add the new endpoint at the end of the list. */
2504 list_add_tail(&virt_ep->bw_endpoint_list,
2505 &interval_bw->endpoints);
2508 void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2509 struct xhci_virt_device *virt_dev,
2510 int old_active_eps)
2512 struct xhci_root_port_bw_info *rh_bw_info;
2513 if (!virt_dev->tt_info)
2514 return;
2516 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2517 if (old_active_eps == 0 &&
2518 virt_dev->tt_info->active_eps != 0) {
2519 rh_bw_info->num_active_tts += 1;
2520 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
2521 } else if (old_active_eps != 0 &&
2522 virt_dev->tt_info->active_eps == 0) {
2523 rh_bw_info->num_active_tts -= 1;
2524 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
2528 static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2529 struct xhci_virt_device *virt_dev,
2530 struct xhci_container_ctx *in_ctx)
2532 struct xhci_bw_info ep_bw_info[31];
2533 int i;
2534 struct xhci_input_control_ctx *ctrl_ctx;
2535 int old_active_eps = 0;
2537 if (virt_dev->tt_info)
2538 old_active_eps = virt_dev->tt_info->active_eps;
2540 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2541 if (!ctrl_ctx) {
2542 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2543 __func__);
2544 return -ENOMEM;
2547 for (i = 0; i < 31; i++) {
2548 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2549 continue;
2551 /* Make a copy of the BW info in case we need to revert this */
2552 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2553 sizeof(ep_bw_info[i]));
2554 /* Drop the endpoint from the interval table if the endpoint is
2555 * being dropped or changed.
2557 if (EP_IS_DROPPED(ctrl_ctx, i))
2558 xhci_drop_ep_from_interval_table(xhci,
2559 &virt_dev->eps[i].bw_info,
2560 virt_dev->bw_table,
2561 virt_dev->udev,
2562 &virt_dev->eps[i],
2563 virt_dev->tt_info);
2565 /* Overwrite the information stored in the endpoints' bw_info */
2566 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2567 for (i = 0; i < 31; i++) {
2568 /* Add any changed or added endpoints to the interval table */
2569 if (EP_IS_ADDED(ctrl_ctx, i))
2570 xhci_add_ep_to_interval_table(xhci,
2571 &virt_dev->eps[i].bw_info,
2572 virt_dev->bw_table,
2573 virt_dev->udev,
2574 &virt_dev->eps[i],
2575 virt_dev->tt_info);
2578 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2579 /* Ok, this fits in the bandwidth we have.
2580 * Update the number of active TTs.
2582 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2583 return 0;
2586 /* We don't have enough bandwidth for this, revert the stored info. */
2587 for (i = 0; i < 31; i++) {
2588 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2589 continue;
2591 /* Drop the new copies of any added or changed endpoints from
2592 * the interval table.
2594 if (EP_IS_ADDED(ctrl_ctx, i)) {
2595 xhci_drop_ep_from_interval_table(xhci,
2596 &virt_dev->eps[i].bw_info,
2597 virt_dev->bw_table,
2598 virt_dev->udev,
2599 &virt_dev->eps[i],
2600 virt_dev->tt_info);
2602 /* Revert the endpoint back to its old information */
2603 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2604 sizeof(ep_bw_info[i]));
2605 /* Add any changed or dropped endpoints back into the table */
2606 if (EP_IS_DROPPED(ctrl_ctx, i))
2607 xhci_add_ep_to_interval_table(xhci,
2608 &virt_dev->eps[i].bw_info,
2609 virt_dev->bw_table,
2610 virt_dev->udev,
2611 &virt_dev->eps[i],
2612 virt_dev->tt_info);
2614 return -ENOMEM;
2618 /* Issue a configure endpoint command or evaluate context command
2619 * and wait for it to finish.
2621 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
2622 struct usb_device *udev,
2623 struct xhci_command *command,
2624 bool ctx_change, bool must_succeed)
2626 int ret;
2627 int timeleft;
2628 unsigned long flags;
2629 struct xhci_container_ctx *in_ctx;
2630 struct xhci_input_control_ctx *ctrl_ctx;
2631 struct completion *cmd_completion;
2632 u32 *cmd_status;
2633 struct xhci_virt_device *virt_dev;
2634 union xhci_trb *cmd_trb;
2636 spin_lock_irqsave(&xhci->lock, flags);
2637 virt_dev = xhci->devs[udev->slot_id];
2639 if (command)
2640 in_ctx = command->in_ctx;
2641 else
2642 in_ctx = virt_dev->in_ctx;
2643 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2644 if (!ctrl_ctx) {
2645 spin_unlock_irqrestore(&xhci->lock, flags);
2646 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2647 __func__);
2648 return -ENOMEM;
2651 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
2652 xhci_reserve_host_resources(xhci, ctrl_ctx)) {
2653 spin_unlock_irqrestore(&xhci->lock, flags);
2654 xhci_warn(xhci, "Not enough host resources, "
2655 "active endpoint contexts = %u\n",
2656 xhci->num_active_eps);
2657 return -ENOMEM;
2659 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
2660 xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
2661 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2662 xhci_free_host_resources(xhci, ctrl_ctx);
2663 spin_unlock_irqrestore(&xhci->lock, flags);
2664 xhci_warn(xhci, "Not enough bandwidth\n");
2665 return -ENOMEM;
2668 if (command) {
2669 cmd_completion = command->completion;
2670 cmd_status = &command->status;
2671 command->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
2672 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
2673 } else {
2674 cmd_completion = &virt_dev->cmd_completion;
2675 cmd_status = &virt_dev->cmd_status;
2677 init_completion(cmd_completion);
2679 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
2680 if (!ctx_change)
2681 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
2682 udev->slot_id, must_succeed);
2683 else
2684 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
2685 udev->slot_id, must_succeed);
2686 if (ret < 0) {
2687 if (command)
2688 list_del(&command->cmd_list);
2689 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2690 xhci_free_host_resources(xhci, ctrl_ctx);
2691 spin_unlock_irqrestore(&xhci->lock, flags);
2692 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2693 "FIXME allocate a new ring segment");
2694 return -ENOMEM;
2696 xhci_ring_cmd_db(xhci);
2697 spin_unlock_irqrestore(&xhci->lock, flags);
2699 /* Wait for the configure endpoint command to complete */
2700 timeleft = wait_for_completion_interruptible_timeout(
2701 cmd_completion,
2702 XHCI_CMD_DEFAULT_TIMEOUT);
2703 if (timeleft <= 0) {
2704 xhci_warn(xhci, "%s while waiting for %s command\n",
2705 timeleft == 0 ? "Timeout" : "Signal",
2706 ctx_change == 0 ?
2707 "configure endpoint" :
2708 "evaluate context");
2709 /* cancel the configure endpoint command */
2710 ret = xhci_cancel_cmd(xhci, command, cmd_trb);
2711 if (ret < 0)
2712 return ret;
2713 return -ETIME;
2716 if (!ctx_change)
2717 ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
2718 else
2719 ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
2721 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2722 spin_lock_irqsave(&xhci->lock, flags);
2723 /* If the command failed, remove the reserved resources.
2724 * Otherwise, clean up the estimate to include dropped eps.
2726 if (ret)
2727 xhci_free_host_resources(xhci, ctrl_ctx);
2728 else
2729 xhci_finish_resource_reservation(xhci, ctrl_ctx);
2730 spin_unlock_irqrestore(&xhci->lock, flags);
2732 return ret;
2735 /* Called after one or more calls to xhci_add_endpoint() or
2736 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2737 * to call xhci_reset_bandwidth().
2739 * Since we are in the middle of changing either configuration or
2740 * installing a new alt setting, the USB core won't allow URBs to be
2741 * enqueued for any endpoint on the old config or interface. Nothing
2742 * else should be touching the xhci->devs[slot_id] structure, so we
2743 * don't need to take the xhci->lock for manipulating that.
2745 int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2747 int i;
2748 int ret = 0;
2749 struct xhci_hcd *xhci;
2750 struct xhci_virt_device *virt_dev;
2751 struct xhci_input_control_ctx *ctrl_ctx;
2752 struct xhci_slot_ctx *slot_ctx;
2754 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
2755 if (ret <= 0)
2756 return ret;
2757 xhci = hcd_to_xhci(hcd);
2758 if (xhci->xhc_state & XHCI_STATE_DYING)
2759 return -ENODEV;
2761 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
2762 virt_dev = xhci->devs[udev->slot_id];
2764 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
2765 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2766 if (!ctrl_ctx) {
2767 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2768 __func__);
2769 return -ENOMEM;
2771 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2772 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2773 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
2775 /* Don't issue the command if there's no endpoints to update. */
2776 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
2777 ctrl_ctx->drop_flags == 0)
2778 return 0;
2780 xhci_dbg(xhci, "New Input Control Context:\n");
2781 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2782 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
2783 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
2785 ret = xhci_configure_endpoint(xhci, udev, NULL,
2786 false, false);
2787 if (ret) {
2788 /* Callee should call reset_bandwidth() */
2789 return ret;
2792 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
2793 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
2794 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
2796 /* Free any rings that were dropped, but not changed. */
2797 for (i = 1; i < 31; ++i) {
2798 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
2799 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
2800 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2802 xhci_zero_in_ctx(xhci, virt_dev);
2804 * Install any rings for completely new endpoints or changed endpoints,
2805 * and free or cache any old rings from changed endpoints.
2807 for (i = 1; i < 31; ++i) {
2808 if (!virt_dev->eps[i].new_ring)
2809 continue;
2810 /* Only cache or free the old ring if it exists.
2811 * It may not if this is the first add of an endpoint.
2813 if (virt_dev->eps[i].ring) {
2814 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2816 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2817 virt_dev->eps[i].new_ring = NULL;
2820 return ret;
2823 void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2825 struct xhci_hcd *xhci;
2826 struct xhci_virt_device *virt_dev;
2827 int i, ret;
2829 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
2830 if (ret <= 0)
2831 return;
2832 xhci = hcd_to_xhci(hcd);
2834 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
2835 virt_dev = xhci->devs[udev->slot_id];
2836 /* Free any rings allocated for added endpoints */
2837 for (i = 0; i < 31; ++i) {
2838 if (virt_dev->eps[i].new_ring) {
2839 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2840 virt_dev->eps[i].new_ring = NULL;
2843 xhci_zero_in_ctx(xhci, virt_dev);
2846 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
2847 struct xhci_container_ctx *in_ctx,
2848 struct xhci_container_ctx *out_ctx,
2849 struct xhci_input_control_ctx *ctrl_ctx,
2850 u32 add_flags, u32 drop_flags)
2852 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2853 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
2854 xhci_slot_copy(xhci, in_ctx, out_ctx);
2855 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2857 xhci_dbg(xhci, "Input Context:\n");
2858 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
2861 static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
2862 unsigned int slot_id, unsigned int ep_index,
2863 struct xhci_dequeue_state *deq_state)
2865 struct xhci_input_control_ctx *ctrl_ctx;
2866 struct xhci_container_ctx *in_ctx;
2867 struct xhci_ep_ctx *ep_ctx;
2868 u32 added_ctxs;
2869 dma_addr_t addr;
2871 in_ctx = xhci->devs[slot_id]->in_ctx;
2872 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2873 if (!ctrl_ctx) {
2874 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2875 __func__);
2876 return;
2879 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2880 xhci->devs[slot_id]->out_ctx, ep_index);
2881 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2882 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2883 deq_state->new_deq_ptr);
2884 if (addr == 0) {
2885 xhci_warn(xhci, "WARN Cannot submit config ep after "
2886 "reset ep command\n");
2887 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2888 deq_state->new_deq_seg,
2889 deq_state->new_deq_ptr);
2890 return;
2892 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
2894 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
2895 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
2896 xhci->devs[slot_id]->out_ctx, ctrl_ctx,
2897 added_ctxs, added_ctxs);
2900 void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
2901 struct usb_device *udev, unsigned int ep_index)
2903 struct xhci_dequeue_state deq_state;
2904 struct xhci_virt_ep *ep;
2906 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2907 "Cleaning up stalled endpoint ring");
2908 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
2909 /* We need to move the HW's dequeue pointer past this TD,
2910 * or it will attempt to resend it on the next doorbell ring.
2912 xhci_find_new_dequeue_state(xhci, udev->slot_id,
2913 ep_index, ep->stopped_stream, ep->stopped_td,
2914 &deq_state);
2916 if (!deq_state.new_deq_ptr || !deq_state.new_deq_seg)
2917 return;
2919 /* HW with the reset endpoint quirk will use the saved dequeue state to
2920 * issue a configure endpoint command later.
2922 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
2923 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2924 "Queueing new dequeue state");
2925 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
2926 ep_index, ep->stopped_stream, &deq_state);
2927 } else {
2928 /* Better hope no one uses the input context between now and the
2929 * reset endpoint completion!
2930 * XXX: No idea how this hardware will react when stream rings
2931 * are enabled.
2933 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2934 "Setting up input context for "
2935 "configure endpoint command");
2936 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2937 ep_index, &deq_state);
2941 /* Called when clearing halted device. The core should have sent the control
2942 * message to clear the device halt condition. The host side of the halt should
2943 * already be cleared with a reset endpoint command issued when the STALL tx
2944 * event was received.
2946 * Context: in_interrupt
2949 void xhci_endpoint_reset(struct usb_hcd *hcd,
2950 struct usb_host_endpoint *ep)
2952 struct xhci_hcd *xhci;
2954 xhci = hcd_to_xhci(hcd);
2957 * We might need to implement the config ep cmd in xhci 4.8.1 note:
2958 * The Reset Endpoint Command may only be issued to endpoints in the
2959 * Halted state. If software wishes reset the Data Toggle or Sequence
2960 * Number of an endpoint that isn't in the Halted state, then software
2961 * may issue a Configure Endpoint Command with the Drop and Add bits set
2962 * for the target endpoint. that is in the Stopped state.
2965 /* For now just print debug to follow the situation */
2966 xhci_dbg(xhci, "Endpoint 0x%x ep reset callback called\n",
2967 ep->desc.bEndpointAddress);
2970 static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2971 struct usb_device *udev, struct usb_host_endpoint *ep,
2972 unsigned int slot_id)
2974 int ret;
2975 unsigned int ep_index;
2976 unsigned int ep_state;
2978 if (!ep)
2979 return -EINVAL;
2980 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
2981 if (ret <= 0)
2982 return -EINVAL;
2983 if (ep->ss_ep_comp.bmAttributes == 0) {
2984 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2985 " descriptor for ep 0x%x does not support streams\n",
2986 ep->desc.bEndpointAddress);
2987 return -EINVAL;
2990 ep_index = xhci_get_endpoint_index(&ep->desc);
2991 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
2992 if (ep_state & EP_HAS_STREAMS ||
2993 ep_state & EP_GETTING_STREAMS) {
2994 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
2995 "already has streams set up.\n",
2996 ep->desc.bEndpointAddress);
2997 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
2998 "dynamic stream context array reallocation.\n");
2999 return -EINVAL;
3001 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
3002 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
3003 "endpoint 0x%x; URBs are pending.\n",
3004 ep->desc.bEndpointAddress);
3005 return -EINVAL;
3007 return 0;
3010 static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
3011 unsigned int *num_streams, unsigned int *num_stream_ctxs)
3013 unsigned int max_streams;
3015 /* The stream context array size must be a power of two */
3016 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
3018 * Find out how many primary stream array entries the host controller
3019 * supports. Later we may use secondary stream arrays (similar to 2nd
3020 * level page entries), but that's an optional feature for xHCI host
3021 * controllers. xHCs must support at least 4 stream IDs.
3023 max_streams = HCC_MAX_PSA(xhci->hcc_params);
3024 if (*num_stream_ctxs > max_streams) {
3025 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
3026 max_streams);
3027 *num_stream_ctxs = max_streams;
3028 *num_streams = max_streams;
3032 /* Returns an error code if one of the endpoint already has streams.
3033 * This does not change any data structures, it only checks and gathers
3034 * information.
3036 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
3037 struct usb_device *udev,
3038 struct usb_host_endpoint **eps, unsigned int num_eps,
3039 unsigned int *num_streams, u32 *changed_ep_bitmask)
3041 unsigned int max_streams;
3042 unsigned int endpoint_flag;
3043 int i;
3044 int ret;
3046 for (i = 0; i < num_eps; i++) {
3047 ret = xhci_check_streams_endpoint(xhci, udev,
3048 eps[i], udev->slot_id);
3049 if (ret < 0)
3050 return ret;
3052 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
3053 if (max_streams < (*num_streams - 1)) {
3054 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3055 eps[i]->desc.bEndpointAddress,
3056 max_streams);
3057 *num_streams = max_streams+1;
3060 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3061 if (*changed_ep_bitmask & endpoint_flag)
3062 return -EINVAL;
3063 *changed_ep_bitmask |= endpoint_flag;
3065 return 0;
3068 static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3069 struct usb_device *udev,
3070 struct usb_host_endpoint **eps, unsigned int num_eps)
3072 u32 changed_ep_bitmask = 0;
3073 unsigned int slot_id;
3074 unsigned int ep_index;
3075 unsigned int ep_state;
3076 int i;
3078 slot_id = udev->slot_id;
3079 if (!xhci->devs[slot_id])
3080 return 0;
3082 for (i = 0; i < num_eps; i++) {
3083 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3084 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3085 /* Are streams already being freed for the endpoint? */
3086 if (ep_state & EP_GETTING_NO_STREAMS) {
3087 xhci_warn(xhci, "WARN Can't disable streams for "
3088 "endpoint 0x%x, "
3089 "streams are being disabled already\n",
3090 eps[i]->desc.bEndpointAddress);
3091 return 0;
3093 /* Are there actually any streams to free? */
3094 if (!(ep_state & EP_HAS_STREAMS) &&
3095 !(ep_state & EP_GETTING_STREAMS)) {
3096 xhci_warn(xhci, "WARN Can't disable streams for "
3097 "endpoint 0x%x, "
3098 "streams are already disabled!\n",
3099 eps[i]->desc.bEndpointAddress);
3100 xhci_warn(xhci, "WARN xhci_free_streams() called "
3101 "with non-streams endpoint\n");
3102 return 0;
3104 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3106 return changed_ep_bitmask;
3110 * The USB device drivers use this function (though the HCD interface in USB
3111 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3112 * coordinate mass storage command queueing across multiple endpoints (basically
3113 * a stream ID == a task ID).
3115 * Setting up streams involves allocating the same size stream context array
3116 * for each endpoint and issuing a configure endpoint command for all endpoints.
3118 * Don't allow the call to succeed if one endpoint only supports one stream
3119 * (which means it doesn't support streams at all).
3121 * Drivers may get less stream IDs than they asked for, if the host controller
3122 * hardware or endpoints claim they can't support the number of requested
3123 * stream IDs.
3125 int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3126 struct usb_host_endpoint **eps, unsigned int num_eps,
3127 unsigned int num_streams, gfp_t mem_flags)
3129 int i, ret;
3130 struct xhci_hcd *xhci;
3131 struct xhci_virt_device *vdev;
3132 struct xhci_command *config_cmd;
3133 struct xhci_input_control_ctx *ctrl_ctx;
3134 unsigned int ep_index;
3135 unsigned int num_stream_ctxs;
3136 unsigned long flags;
3137 u32 changed_ep_bitmask = 0;
3139 if (!eps)
3140 return -EINVAL;
3142 /* Add one to the number of streams requested to account for
3143 * stream 0 that is reserved for xHCI usage.
3145 num_streams += 1;
3146 xhci = hcd_to_xhci(hcd);
3147 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3148 num_streams);
3150 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
3151 if (!config_cmd) {
3152 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
3153 return -ENOMEM;
3155 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
3156 if (!ctrl_ctx) {
3157 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3158 __func__);
3159 xhci_free_command(xhci, config_cmd);
3160 return -ENOMEM;
3163 /* Check to make sure all endpoints are not already configured for
3164 * streams. While we're at it, find the maximum number of streams that
3165 * all the endpoints will support and check for duplicate endpoints.
3167 spin_lock_irqsave(&xhci->lock, flags);
3168 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3169 num_eps, &num_streams, &changed_ep_bitmask);
3170 if (ret < 0) {
3171 xhci_free_command(xhci, config_cmd);
3172 spin_unlock_irqrestore(&xhci->lock, flags);
3173 return ret;
3175 if (num_streams <= 1) {
3176 xhci_warn(xhci, "WARN: endpoints can't handle "
3177 "more than one stream.\n");
3178 xhci_free_command(xhci, config_cmd);
3179 spin_unlock_irqrestore(&xhci->lock, flags);
3180 return -EINVAL;
3182 vdev = xhci->devs[udev->slot_id];
3183 /* Mark each endpoint as being in transition, so
3184 * xhci_urb_enqueue() will reject all URBs.
3186 for (i = 0; i < num_eps; i++) {
3187 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3188 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3190 spin_unlock_irqrestore(&xhci->lock, flags);
3192 /* Setup internal data structures and allocate HW data structures for
3193 * streams (but don't install the HW structures in the input context
3194 * until we're sure all memory allocation succeeded).
3196 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3197 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3198 num_stream_ctxs, num_streams);
3200 for (i = 0; i < num_eps; i++) {
3201 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3202 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3203 num_stream_ctxs,
3204 num_streams, mem_flags);
3205 if (!vdev->eps[ep_index].stream_info)
3206 goto cleanup;
3207 /* Set maxPstreams in endpoint context and update deq ptr to
3208 * point to stream context array. FIXME
3212 /* Set up the input context for a configure endpoint command. */
3213 for (i = 0; i < num_eps; i++) {
3214 struct xhci_ep_ctx *ep_ctx;
3216 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3217 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3219 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3220 vdev->out_ctx, ep_index);
3221 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3222 vdev->eps[ep_index].stream_info);
3224 /* Tell the HW to drop its old copy of the endpoint context info
3225 * and add the updated copy from the input context.
3227 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
3228 vdev->out_ctx, ctrl_ctx,
3229 changed_ep_bitmask, changed_ep_bitmask);
3231 /* Issue and wait for the configure endpoint command */
3232 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3233 false, false);
3235 /* xHC rejected the configure endpoint command for some reason, so we
3236 * leave the old ring intact and free our internal streams data
3237 * structure.
3239 if (ret < 0)
3240 goto cleanup;
3242 spin_lock_irqsave(&xhci->lock, flags);
3243 for (i = 0; i < num_eps; i++) {
3244 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3245 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3246 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3247 udev->slot_id, ep_index);
3248 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3250 xhci_free_command(xhci, config_cmd);
3251 spin_unlock_irqrestore(&xhci->lock, flags);
3253 /* Subtract 1 for stream 0, which drivers can't use */
3254 return num_streams - 1;
3256 cleanup:
3257 /* If it didn't work, free the streams! */
3258 for (i = 0; i < num_eps; i++) {
3259 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3260 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
3261 vdev->eps[ep_index].stream_info = NULL;
3262 /* FIXME Unset maxPstreams in endpoint context and
3263 * update deq ptr to point to normal string ring.
3265 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3266 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3267 xhci_endpoint_zero(xhci, vdev, eps[i]);
3269 xhci_free_command(xhci, config_cmd);
3270 return -ENOMEM;
3273 /* Transition the endpoint from using streams to being a "normal" endpoint
3274 * without streams.
3276 * Modify the endpoint context state, submit a configure endpoint command,
3277 * and free all endpoint rings for streams if that completes successfully.
3279 int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
3280 struct usb_host_endpoint **eps, unsigned int num_eps,
3281 gfp_t mem_flags)
3283 int i, ret;
3284 struct xhci_hcd *xhci;
3285 struct xhci_virt_device *vdev;
3286 struct xhci_command *command;
3287 struct xhci_input_control_ctx *ctrl_ctx;
3288 unsigned int ep_index;
3289 unsigned long flags;
3290 u32 changed_ep_bitmask;
3292 xhci = hcd_to_xhci(hcd);
3293 vdev = xhci->devs[udev->slot_id];
3295 /* Set up a configure endpoint command to remove the streams rings */
3296 spin_lock_irqsave(&xhci->lock, flags);
3297 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3298 udev, eps, num_eps);
3299 if (changed_ep_bitmask == 0) {
3300 spin_unlock_irqrestore(&xhci->lock, flags);
3301 return -EINVAL;
3304 /* Use the xhci_command structure from the first endpoint. We may have
3305 * allocated too many, but the driver may call xhci_free_streams() for
3306 * each endpoint it grouped into one call to xhci_alloc_streams().
3308 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3309 command = vdev->eps[ep_index].stream_info->free_streams_command;
3310 ctrl_ctx = xhci_get_input_control_ctx(xhci, command->in_ctx);
3311 if (!ctrl_ctx) {
3312 spin_unlock_irqrestore(&xhci->lock, flags);
3313 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3314 __func__);
3315 return -EINVAL;
3318 for (i = 0; i < num_eps; i++) {
3319 struct xhci_ep_ctx *ep_ctx;
3321 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3322 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3323 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3324 EP_GETTING_NO_STREAMS;
3326 xhci_endpoint_copy(xhci, command->in_ctx,
3327 vdev->out_ctx, ep_index);
3328 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
3329 &vdev->eps[ep_index]);
3331 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
3332 vdev->out_ctx, ctrl_ctx,
3333 changed_ep_bitmask, changed_ep_bitmask);
3334 spin_unlock_irqrestore(&xhci->lock, flags);
3336 /* Issue and wait for the configure endpoint command,
3337 * which must succeed.
3339 ret = xhci_configure_endpoint(xhci, udev, command,
3340 false, true);
3342 /* xHC rejected the configure endpoint command for some reason, so we
3343 * leave the streams rings intact.
3345 if (ret < 0)
3346 return ret;
3348 spin_lock_irqsave(&xhci->lock, flags);
3349 for (i = 0; i < num_eps; i++) {
3350 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3351 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
3352 vdev->eps[ep_index].stream_info = NULL;
3353 /* FIXME Unset maxPstreams in endpoint context and
3354 * update deq ptr to point to normal string ring.
3356 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3357 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3359 spin_unlock_irqrestore(&xhci->lock, flags);
3361 return 0;
3365 * Deletes endpoint resources for endpoints that were active before a Reset
3366 * Device command, or a Disable Slot command. The Reset Device command leaves
3367 * the control endpoint intact, whereas the Disable Slot command deletes it.
3369 * Must be called with xhci->lock held.
3371 void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3372 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3374 int i;
3375 unsigned int num_dropped_eps = 0;
3376 unsigned int drop_flags = 0;
3378 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3379 if (virt_dev->eps[i].ring) {
3380 drop_flags |= 1 << i;
3381 num_dropped_eps++;
3384 xhci->num_active_eps -= num_dropped_eps;
3385 if (num_dropped_eps)
3386 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3387 "Dropped %u ep ctxs, flags = 0x%x, "
3388 "%u now active.",
3389 num_dropped_eps, drop_flags,
3390 xhci->num_active_eps);
3394 * This submits a Reset Device Command, which will set the device state to 0,
3395 * set the device address to 0, and disable all the endpoints except the default
3396 * control endpoint. The USB core should come back and call
3397 * xhci_address_device(), and then re-set up the configuration. If this is
3398 * called because of a usb_reset_and_verify_device(), then the old alternate
3399 * settings will be re-installed through the normal bandwidth allocation
3400 * functions.
3402 * Wait for the Reset Device command to finish. Remove all structures
3403 * associated with the endpoints that were disabled. Clear the input device
3404 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
3406 * If the virt_dev to be reset does not exist or does not match the udev,
3407 * it means the device is lost, possibly due to the xHC restore error and
3408 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3409 * re-allocate the device.
3411 int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
3413 int ret, i;
3414 unsigned long flags;
3415 struct xhci_hcd *xhci;
3416 unsigned int slot_id;
3417 struct xhci_virt_device *virt_dev;
3418 struct xhci_command *reset_device_cmd;
3419 int timeleft;
3420 int last_freed_endpoint;
3421 struct xhci_slot_ctx *slot_ctx;
3422 int old_active_eps = 0;
3424 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
3425 if (ret <= 0)
3426 return ret;
3427 xhci = hcd_to_xhci(hcd);
3428 slot_id = udev->slot_id;
3429 virt_dev = xhci->devs[slot_id];
3430 if (!virt_dev) {
3431 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3432 "not exist. Re-allocate the device\n", slot_id);
3433 ret = xhci_alloc_dev(hcd, udev);
3434 if (ret == 1)
3435 return 0;
3436 else
3437 return -EINVAL;
3440 if (virt_dev->tt_info)
3441 old_active_eps = virt_dev->tt_info->active_eps;
3443 if (virt_dev->udev != udev) {
3444 /* If the virt_dev and the udev does not match, this virt_dev
3445 * may belong to another udev.
3446 * Re-allocate the device.
3448 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3449 "not match the udev. Re-allocate the device\n",
3450 slot_id);
3451 ret = xhci_alloc_dev(hcd, udev);
3452 if (ret == 1)
3453 return 0;
3454 else
3455 return -EINVAL;
3458 /* If device is not setup, there is no point in resetting it */
3459 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3460 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3461 SLOT_STATE_DISABLED)
3462 return 0;
3464 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3465 /* Allocate the command structure that holds the struct completion.
3466 * Assume we're in process context, since the normal device reset
3467 * process has to wait for the device anyway. Storage devices are
3468 * reset as part of error handling, so use GFP_NOIO instead of
3469 * GFP_KERNEL.
3471 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
3472 if (!reset_device_cmd) {
3473 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3474 return -ENOMEM;
3477 /* Attempt to submit the Reset Device command to the command ring */
3478 spin_lock_irqsave(&xhci->lock, flags);
3479 reset_device_cmd->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
3481 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
3482 ret = xhci_queue_reset_device(xhci, slot_id);
3483 if (ret) {
3484 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3485 list_del(&reset_device_cmd->cmd_list);
3486 spin_unlock_irqrestore(&xhci->lock, flags);
3487 goto command_cleanup;
3489 xhci_ring_cmd_db(xhci);
3490 spin_unlock_irqrestore(&xhci->lock, flags);
3492 /* Wait for the Reset Device command to finish */
3493 timeleft = wait_for_completion_interruptible_timeout(
3494 reset_device_cmd->completion,
3495 USB_CTRL_SET_TIMEOUT);
3496 if (timeleft <= 0) {
3497 xhci_warn(xhci, "%s while waiting for reset device command\n",
3498 timeleft == 0 ? "Timeout" : "Signal");
3499 spin_lock_irqsave(&xhci->lock, flags);
3500 /* The timeout might have raced with the event ring handler, so
3501 * only delete from the list if the item isn't poisoned.
3503 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
3504 list_del(&reset_device_cmd->cmd_list);
3505 spin_unlock_irqrestore(&xhci->lock, flags);
3506 ret = -ETIME;
3507 goto command_cleanup;
3510 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3511 * unless we tried to reset a slot ID that wasn't enabled,
3512 * or the device wasn't in the addressed or configured state.
3514 ret = reset_device_cmd->status;
3515 switch (ret) {
3516 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
3517 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
3518 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n",
3519 slot_id,
3520 xhci_get_slot_state(xhci, virt_dev->out_ctx));
3521 xhci_dbg(xhci, "Not freeing device rings.\n");
3522 /* Don't treat this as an error. May change my mind later. */
3523 ret = 0;
3524 goto command_cleanup;
3525 case COMP_SUCCESS:
3526 xhci_dbg(xhci, "Successful reset device command.\n");
3527 break;
3528 default:
3529 if (xhci_is_vendor_info_code(xhci, ret))
3530 break;
3531 xhci_warn(xhci, "Unknown completion code %u for "
3532 "reset device command.\n", ret);
3533 ret = -EINVAL;
3534 goto command_cleanup;
3537 /* Free up host controller endpoint resources */
3538 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3539 spin_lock_irqsave(&xhci->lock, flags);
3540 /* Don't delete the default control endpoint resources */
3541 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3542 spin_unlock_irqrestore(&xhci->lock, flags);
3545 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3546 last_freed_endpoint = 1;
3547 for (i = 1; i < 31; ++i) {
3548 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3550 if (ep->ep_state & EP_HAS_STREAMS) {
3551 xhci_free_stream_info(xhci, ep->stream_info);
3552 ep->stream_info = NULL;
3553 ep->ep_state &= ~EP_HAS_STREAMS;
3556 if (ep->ring) {
3557 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
3558 last_freed_endpoint = i;
3560 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3561 xhci_drop_ep_from_interval_table(xhci,
3562 &virt_dev->eps[i].bw_info,
3563 virt_dev->bw_table,
3564 udev,
3565 &virt_dev->eps[i],
3566 virt_dev->tt_info);
3567 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
3569 /* If necessary, update the number of active TTs on this root port */
3570 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3572 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
3573 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
3574 ret = 0;
3576 command_cleanup:
3577 xhci_free_command(xhci, reset_device_cmd);
3578 return ret;
3582 * At this point, the struct usb_device is about to go away, the device has
3583 * disconnected, and all traffic has been stopped and the endpoints have been
3584 * disabled. Free any HC data structures associated with that device.
3586 void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
3588 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3589 struct xhci_virt_device *virt_dev;
3590 unsigned long flags;
3591 u32 state;
3592 int i, ret;
3594 #ifndef CONFIG_USB_DEFAULT_PERSIST
3596 * We called pm_runtime_get_noresume when the device was attached.
3597 * Decrement the counter here to allow controller to runtime suspend
3598 * if no devices remain.
3600 if (xhci->quirks & XHCI_RESET_ON_RESUME)
3601 pm_runtime_put_noidle(hcd->self.controller);
3602 #endif
3604 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
3605 /* If the host is halted due to driver unload, we still need to free the
3606 * device.
3608 if (ret <= 0 && ret != -ENODEV)
3609 return;
3611 virt_dev = xhci->devs[udev->slot_id];
3613 /* Stop any wayward timer functions (which may grab the lock) */
3614 for (i = 0; i < 31; ++i) {
3615 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
3616 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3619 if (udev->usb2_hw_lpm_enabled) {
3620 xhci_set_usb2_hardware_lpm(hcd, udev, 0);
3621 udev->usb2_hw_lpm_enabled = 0;
3624 spin_lock_irqsave(&xhci->lock, flags);
3625 /* Don't disable the slot if the host controller is dead. */
3626 state = xhci_readl(xhci, &xhci->op_regs->status);
3627 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3628 (xhci->xhc_state & XHCI_STATE_HALTED)) {
3629 xhci_free_virt_device(xhci, udev->slot_id);
3630 spin_unlock_irqrestore(&xhci->lock, flags);
3631 return;
3634 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
3635 spin_unlock_irqrestore(&xhci->lock, flags);
3636 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3637 return;
3639 xhci_ring_cmd_db(xhci);
3640 spin_unlock_irqrestore(&xhci->lock, flags);
3642 * Event command completion handler will free any data structures
3643 * associated with the slot. XXX Can free sleep?
3648 * Checks if we have enough host controller resources for the default control
3649 * endpoint.
3651 * Must be called with xhci->lock held.
3653 static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3655 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
3656 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3657 "Not enough ep ctxs: "
3658 "%u active, need to add 1, limit is %u.",
3659 xhci->num_active_eps, xhci->limit_active_eps);
3660 return -ENOMEM;
3662 xhci->num_active_eps += 1;
3663 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3664 "Adding 1 ep ctx, %u now active.",
3665 xhci->num_active_eps);
3666 return 0;
3671 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3672 * timed out, or allocating memory failed. Returns 1 on success.
3674 int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3676 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3677 unsigned long flags;
3678 int timeleft;
3679 int ret;
3680 union xhci_trb *cmd_trb;
3682 spin_lock_irqsave(&xhci->lock, flags);
3683 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
3684 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
3685 if (ret) {
3686 spin_unlock_irqrestore(&xhci->lock, flags);
3687 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3688 return 0;
3690 xhci_ring_cmd_db(xhci);
3691 spin_unlock_irqrestore(&xhci->lock, flags);
3693 /* XXX: how much time for xHC slot assignment? */
3694 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
3695 XHCI_CMD_DEFAULT_TIMEOUT);
3696 if (timeleft <= 0) {
3697 xhci_warn(xhci, "%s while waiting for a slot\n",
3698 timeleft == 0 ? "Timeout" : "Signal");
3699 /* cancel the enable slot request */
3700 return xhci_cancel_cmd(xhci, NULL, cmd_trb);
3703 if (!xhci->slot_id) {
3704 xhci_err(xhci, "Error while assigning device slot ID\n");
3705 return 0;
3708 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3709 spin_lock_irqsave(&xhci->lock, flags);
3710 ret = xhci_reserve_host_control_ep_resources(xhci);
3711 if (ret) {
3712 spin_unlock_irqrestore(&xhci->lock, flags);
3713 xhci_warn(xhci, "Not enough host resources, "
3714 "active endpoint contexts = %u\n",
3715 xhci->num_active_eps);
3716 goto disable_slot;
3718 spin_unlock_irqrestore(&xhci->lock, flags);
3720 /* Use GFP_NOIO, since this function can be called from
3721 * xhci_discover_or_reset_device(), which may be called as part of
3722 * mass storage driver error handling.
3724 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
3725 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
3726 goto disable_slot;
3728 udev->slot_id = xhci->slot_id;
3730 #ifndef CONFIG_USB_DEFAULT_PERSIST
3732 * If resetting upon resume, we can't put the controller into runtime
3733 * suspend if there is a device attached.
3735 if (xhci->quirks & XHCI_RESET_ON_RESUME)
3736 pm_runtime_get_noresume(hcd->self.controller);
3737 #endif
3739 /* Is this a LS or FS device under a HS hub? */
3740 /* Hub or peripherial? */
3741 return 1;
3743 disable_slot:
3744 /* Disable slot, if we can do it without mem alloc */
3745 spin_lock_irqsave(&xhci->lock, flags);
3746 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
3747 xhci_ring_cmd_db(xhci);
3748 spin_unlock_irqrestore(&xhci->lock, flags);
3749 return 0;
3753 * Issue an Address Device command (which will issue a SetAddress request to
3754 * the device).
3755 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
3756 * we should only issue and wait on one address command at the same time.
3758 * We add one to the device address issued by the hardware because the USB core
3759 * uses address 1 for the root hubs (even though they're not really devices).
3761 int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
3763 unsigned long flags;
3764 int timeleft;
3765 struct xhci_virt_device *virt_dev;
3766 int ret = 0;
3767 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3768 struct xhci_slot_ctx *slot_ctx;
3769 struct xhci_input_control_ctx *ctrl_ctx;
3770 u64 temp_64;
3771 union xhci_trb *cmd_trb;
3773 if (!udev->slot_id) {
3774 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3775 "Bad Slot ID %d", udev->slot_id);
3776 return -EINVAL;
3779 virt_dev = xhci->devs[udev->slot_id];
3781 if (WARN_ON(!virt_dev)) {
3783 * In plug/unplug torture test with an NEC controller,
3784 * a zero-dereference was observed once due to virt_dev = 0.
3785 * Print useful debug rather than crash if it is observed again!
3787 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3788 udev->slot_id);
3789 return -EINVAL;
3792 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
3793 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
3794 if (!ctrl_ctx) {
3795 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3796 __func__);
3797 return -EINVAL;
3800 * If this is the first Set Address since device plug-in or
3801 * virt_device realloaction after a resume with an xHCI power loss,
3802 * then set up the slot context.
3804 if (!slot_ctx->dev_info)
3805 xhci_setup_addressable_virt_dev(xhci, udev);
3806 /* Otherwise, update the control endpoint ring enqueue pointer. */
3807 else
3808 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
3809 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3810 ctrl_ctx->drop_flags = 0;
3812 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
3813 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
3814 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
3815 slot_ctx->dev_info >> 27);
3817 spin_lock_irqsave(&xhci->lock, flags);
3818 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
3819 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
3820 udev->slot_id);
3821 if (ret) {
3822 spin_unlock_irqrestore(&xhci->lock, flags);
3823 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3824 "FIXME: allocate a command ring segment");
3825 return ret;
3827 xhci_ring_cmd_db(xhci);
3828 spin_unlock_irqrestore(&xhci->lock, flags);
3830 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
3831 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
3832 XHCI_CMD_DEFAULT_TIMEOUT);
3833 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3834 * the SetAddress() "recovery interval" required by USB and aborting the
3835 * command on a timeout.
3837 if (timeleft <= 0) {
3838 xhci_warn(xhci, "%s while waiting for address device command\n",
3839 timeleft == 0 ? "Timeout" : "Signal");
3840 /* cancel the address device command */
3841 ret = xhci_cancel_cmd(xhci, NULL, cmd_trb);
3842 if (ret < 0)
3843 return ret;
3844 return -ETIME;
3847 switch (virt_dev->cmd_status) {
3848 case COMP_CTX_STATE:
3849 case COMP_EBADSLT:
3850 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
3851 udev->slot_id);
3852 ret = -EINVAL;
3853 break;
3854 case COMP_TX_ERR:
3855 dev_warn(&udev->dev, "Device not responding to set address.\n");
3856 ret = -EPROTO;
3857 break;
3858 case COMP_DEV_ERR:
3859 dev_warn(&udev->dev, "ERROR: Incompatible device for address "
3860 "device command.\n");
3861 ret = -ENODEV;
3862 break;
3863 case COMP_SUCCESS:
3864 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3865 "Successful Address Device command");
3866 break;
3867 default:
3868 xhci_err(xhci, "ERROR: unexpected command completion "
3869 "code 0x%x.\n", virt_dev->cmd_status);
3870 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
3871 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
3872 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
3873 ret = -EINVAL;
3874 break;
3876 if (ret) {
3877 return ret;
3879 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
3880 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3881 "Op regs DCBAA ptr = %#016llx", temp_64);
3882 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3883 "Slot ID %d dcbaa entry @%p = %#016llx",
3884 udev->slot_id,
3885 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3886 (unsigned long long)
3887 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
3888 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3889 "Output Context DMA address = %#08llx",
3890 (unsigned long long)virt_dev->out_ctx->dma);
3891 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
3892 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
3893 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
3894 slot_ctx->dev_info >> 27);
3895 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
3896 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
3898 * USB core uses address 1 for the roothubs, so we add one to the
3899 * address given back to us by the HC.
3901 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3902 trace_xhci_address_ctx(xhci, virt_dev->out_ctx,
3903 slot_ctx->dev_info >> 27);
3904 /* Use kernel assigned address for devices; store xHC assigned
3905 * address locally. */
3906 virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
3907 + 1;
3908 /* Zero the input context control for later use */
3909 ctrl_ctx->add_flags = 0;
3910 ctrl_ctx->drop_flags = 0;
3912 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3913 "Internal device address = %d", virt_dev->address);
3915 return 0;
3919 * Transfer the port index into real index in the HW port status
3920 * registers. Caculate offset between the port's PORTSC register
3921 * and port status base. Divide the number of per port register
3922 * to get the real index. The raw port number bases 1.
3924 int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
3926 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3927 __le32 __iomem *base_addr = &xhci->op_regs->port_status_base;
3928 __le32 __iomem *addr;
3929 int raw_port;
3931 if (hcd->speed != HCD_USB3)
3932 addr = xhci->usb2_ports[port1 - 1];
3933 else
3934 addr = xhci->usb3_ports[port1 - 1];
3936 raw_port = (addr - base_addr)/NUM_PORT_REGS + 1;
3937 return raw_port;
3941 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
3942 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
3944 static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
3945 struct usb_device *udev, u16 max_exit_latency)
3947 struct xhci_virt_device *virt_dev;
3948 struct xhci_command *command;
3949 struct xhci_input_control_ctx *ctrl_ctx;
3950 struct xhci_slot_ctx *slot_ctx;
3951 unsigned long flags;
3952 int ret;
3954 spin_lock_irqsave(&xhci->lock, flags);
3956 virt_dev = xhci->devs[udev->slot_id];
3959 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
3960 * xHC was re-initialized. Exit latency will be set later after
3961 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
3964 if (!virt_dev || max_exit_latency == virt_dev->current_mel) {
3965 spin_unlock_irqrestore(&xhci->lock, flags);
3966 return 0;
3969 /* Attempt to issue an Evaluate Context command to change the MEL. */
3970 command = xhci->lpm_command;
3971 ctrl_ctx = xhci_get_input_control_ctx(xhci, command->in_ctx);
3972 if (!ctrl_ctx) {
3973 spin_unlock_irqrestore(&xhci->lock, flags);
3974 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3975 __func__);
3976 return -ENOMEM;
3979 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
3980 spin_unlock_irqrestore(&xhci->lock, flags);
3982 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
3983 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
3984 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
3985 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
3987 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
3988 "Set up evaluate context for LPM MEL change.");
3989 xhci_dbg(xhci, "Slot %u Input Context:\n", udev->slot_id);
3990 xhci_dbg_ctx(xhci, command->in_ctx, 0);
3992 /* Issue and wait for the evaluate context command. */
3993 ret = xhci_configure_endpoint(xhci, udev, command,
3994 true, true);
3995 xhci_dbg(xhci, "Slot %u Output Context:\n", udev->slot_id);
3996 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 0);
3998 if (!ret) {
3999 spin_lock_irqsave(&xhci->lock, flags);
4000 virt_dev->current_mel = max_exit_latency;
4001 spin_unlock_irqrestore(&xhci->lock, flags);
4003 return ret;
4006 #ifdef CONFIG_PM_RUNTIME
4008 /* BESL to HIRD Encoding array for USB2 LPM */
4009 static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
4010 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
4012 /* Calculate HIRD/BESL for USB2 PORTPMSC*/
4013 static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
4014 struct usb_device *udev)
4016 int u2del, besl, besl_host;
4017 int besl_device = 0;
4018 u32 field;
4020 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
4021 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4023 if (field & USB_BESL_SUPPORT) {
4024 for (besl_host = 0; besl_host < 16; besl_host++) {
4025 if (xhci_besl_encoding[besl_host] >= u2del)
4026 break;
4028 /* Use baseline BESL value as default */
4029 if (field & USB_BESL_BASELINE_VALID)
4030 besl_device = USB_GET_BESL_BASELINE(field);
4031 else if (field & USB_BESL_DEEP_VALID)
4032 besl_device = USB_GET_BESL_DEEP(field);
4033 } else {
4034 if (u2del <= 50)
4035 besl_host = 0;
4036 else
4037 besl_host = (u2del - 51) / 75 + 1;
4040 besl = besl_host + besl_device;
4041 if (besl > 15)
4042 besl = 15;
4044 return besl;
4047 /* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4048 static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
4050 u32 field;
4051 int l1;
4052 int besld = 0;
4053 int hirdm = 0;
4055 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4057 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
4058 l1 = udev->l1_params.timeout / 256;
4060 /* device has preferred BESLD */
4061 if (field & USB_BESL_DEEP_VALID) {
4062 besld = USB_GET_BESL_DEEP(field);
4063 hirdm = 1;
4066 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
4069 int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4070 struct usb_device *udev, int enable)
4072 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4073 __le32 __iomem **port_array;
4074 __le32 __iomem *pm_addr, *hlpm_addr;
4075 u32 pm_val, hlpm_val, field;
4076 unsigned int port_num;
4077 unsigned long flags;
4078 int hird, exit_latency;
4079 int ret;
4081 if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
4082 !udev->lpm_capable)
4083 return -EPERM;
4085 if (!udev->parent || udev->parent->parent ||
4086 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4087 return -EPERM;
4089 if (udev->usb2_hw_lpm_capable != 1)
4090 return -EPERM;
4092 spin_lock_irqsave(&xhci->lock, flags);
4094 port_array = xhci->usb2_ports;
4095 port_num = udev->portnum - 1;
4096 pm_addr = port_array[port_num] + PORTPMSC;
4097 pm_val = xhci_readl(xhci, pm_addr);
4098 hlpm_addr = port_array[port_num] + PORTHLPMC;
4099 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4101 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
4102 enable ? "enable" : "disable", port_num);
4104 if (enable) {
4105 /* Host supports BESL timeout instead of HIRD */
4106 if (udev->usb2_hw_lpm_besl_capable) {
4107 /* if device doesn't have a preferred BESL value use a
4108 * default one which works with mixed HIRD and BESL
4109 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4111 if ((field & USB_BESL_SUPPORT) &&
4112 (field & USB_BESL_BASELINE_VALID))
4113 hird = USB_GET_BESL_BASELINE(field);
4114 else
4115 hird = udev->l1_params.besl;
4117 exit_latency = xhci_besl_encoding[hird];
4118 spin_unlock_irqrestore(&xhci->lock, flags);
4120 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4121 * input context for link powermanagement evaluate
4122 * context commands. It is protected by hcd->bandwidth
4123 * mutex and is shared by all devices. We need to set
4124 * the max ext latency in USB 2 BESL LPM as well, so
4125 * use the same mutex and xhci_change_max_exit_latency()
4127 mutex_lock(hcd->bandwidth_mutex);
4128 ret = xhci_change_max_exit_latency(xhci, udev,
4129 exit_latency);
4130 mutex_unlock(hcd->bandwidth_mutex);
4132 if (ret < 0)
4133 return ret;
4134 spin_lock_irqsave(&xhci->lock, flags);
4136 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
4137 xhci_writel(xhci, hlpm_val, hlpm_addr);
4138 /* flush write */
4139 xhci_readl(xhci, hlpm_addr);
4140 } else {
4141 hird = xhci_calculate_hird_besl(xhci, udev);
4144 pm_val &= ~PORT_HIRD_MASK;
4145 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id);
4146 xhci_writel(xhci, pm_val, pm_addr);
4147 pm_val = xhci_readl(xhci, pm_addr);
4148 pm_val |= PORT_HLE;
4149 xhci_writel(xhci, pm_val, pm_addr);
4150 /* flush write */
4151 xhci_readl(xhci, pm_addr);
4152 } else {
4153 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK);
4154 xhci_writel(xhci, pm_val, pm_addr);
4155 /* flush write */
4156 xhci_readl(xhci, pm_addr);
4157 if (udev->usb2_hw_lpm_besl_capable) {
4158 spin_unlock_irqrestore(&xhci->lock, flags);
4159 mutex_lock(hcd->bandwidth_mutex);
4160 xhci_change_max_exit_latency(xhci, udev, 0);
4161 mutex_unlock(hcd->bandwidth_mutex);
4162 return 0;
4166 spin_unlock_irqrestore(&xhci->lock, flags);
4167 return 0;
4170 /* check if a usb2 port supports a given extened capability protocol
4171 * only USB2 ports extended protocol capability values are cached.
4172 * Return 1 if capability is supported
4174 static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
4175 unsigned capability)
4177 u32 port_offset, port_count;
4178 int i;
4180 for (i = 0; i < xhci->num_ext_caps; i++) {
4181 if (xhci->ext_caps[i] & capability) {
4182 /* port offsets starts at 1 */
4183 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
4184 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
4185 if (port >= port_offset &&
4186 port < port_offset + port_count)
4187 return 1;
4190 return 0;
4193 int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4195 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4196 int portnum = udev->portnum - 1;
4198 if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
4199 !udev->lpm_capable)
4200 return 0;
4202 /* we only support lpm for non-hub device connected to root hub yet */
4203 if (!udev->parent || udev->parent->parent ||
4204 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4205 return 0;
4207 if (xhci->hw_lpm_support == 1 &&
4208 xhci_check_usb2_port_capability(
4209 xhci, portnum, XHCI_HLC)) {
4210 udev->usb2_hw_lpm_capable = 1;
4211 udev->l1_params.timeout = XHCI_L1_TIMEOUT;
4212 udev->l1_params.besl = XHCI_DEFAULT_BESL;
4213 if (xhci_check_usb2_port_capability(xhci, portnum,
4214 XHCI_BLC))
4215 udev->usb2_hw_lpm_besl_capable = 1;
4218 return 0;
4221 #else
4223 int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4224 struct usb_device *udev, int enable)
4226 return 0;
4229 int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4231 return 0;
4234 #endif /* CONFIG_PM_RUNTIME */
4236 /*---------------------- USB 3.0 Link PM functions ------------------------*/
4238 #ifdef CONFIG_PM
4239 /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4240 static unsigned long long xhci_service_interval_to_ns(
4241 struct usb_endpoint_descriptor *desc)
4243 return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
4246 static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
4247 enum usb3_link_state state)
4249 unsigned long long sel;
4250 unsigned long long pel;
4251 unsigned int max_sel_pel;
4252 char *state_name;
4254 switch (state) {
4255 case USB3_LPM_U1:
4256 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4257 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4258 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
4259 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
4260 state_name = "U1";
4261 break;
4262 case USB3_LPM_U2:
4263 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4264 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
4265 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
4266 state_name = "U2";
4267 break;
4268 default:
4269 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
4270 __func__);
4271 return USB3_LPM_DISABLED;
4274 if (sel <= max_sel_pel && pel <= max_sel_pel)
4275 return USB3_LPM_DEVICE_INITIATED;
4277 if (sel > max_sel_pel)
4278 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4279 "due to long SEL %llu ms\n",
4280 state_name, sel);
4281 else
4282 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4283 "due to long PEL %llu ms\n",
4284 state_name, pel);
4285 return USB3_LPM_DISABLED;
4288 /* Returns the hub-encoded U1 timeout value.
4289 * The U1 timeout should be the maximum of the following values:
4290 * - For control endpoints, U1 system exit latency (SEL) * 3
4291 * - For bulk endpoints, U1 SEL * 5
4292 * - For interrupt endpoints:
4293 * - Notification EPs, U1 SEL * 3
4294 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4295 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4297 static u16 xhci_calculate_intel_u1_timeout(struct usb_device *udev,
4298 struct usb_endpoint_descriptor *desc)
4300 unsigned long long timeout_ns;
4301 int ep_type;
4302 int intr_type;
4304 ep_type = usb_endpoint_type(desc);
4305 switch (ep_type) {
4306 case USB_ENDPOINT_XFER_CONTROL:
4307 timeout_ns = udev->u1_params.sel * 3;
4308 break;
4309 case USB_ENDPOINT_XFER_BULK:
4310 timeout_ns = udev->u1_params.sel * 5;
4311 break;
4312 case USB_ENDPOINT_XFER_INT:
4313 intr_type = usb_endpoint_interrupt_type(desc);
4314 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
4315 timeout_ns = udev->u1_params.sel * 3;
4316 break;
4318 /* Otherwise the calculation is the same as isoc eps */
4319 case USB_ENDPOINT_XFER_ISOC:
4320 timeout_ns = xhci_service_interval_to_ns(desc);
4321 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
4322 if (timeout_ns < udev->u1_params.sel * 2)
4323 timeout_ns = udev->u1_params.sel * 2;
4324 break;
4325 default:
4326 return 0;
4329 /* The U1 timeout is encoded in 1us intervals. */
4330 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
4331 /* Don't return a timeout of zero, because that's USB3_LPM_DISABLED. */
4332 if (timeout_ns == USB3_LPM_DISABLED)
4333 timeout_ns++;
4335 /* If the necessary timeout value is bigger than what we can set in the
4336 * USB 3.0 hub, we have to disable hub-initiated U1.
4338 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
4339 return timeout_ns;
4340 dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
4341 "due to long timeout %llu ms\n", timeout_ns);
4342 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
4345 /* Returns the hub-encoded U2 timeout value.
4346 * The U2 timeout should be the maximum of:
4347 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4348 * - largest bInterval of any active periodic endpoint (to avoid going
4349 * into lower power link states between intervals).
4350 * - the U2 Exit Latency of the device
4352 static u16 xhci_calculate_intel_u2_timeout(struct usb_device *udev,
4353 struct usb_endpoint_descriptor *desc)
4355 unsigned long long timeout_ns;
4356 unsigned long long u2_del_ns;
4358 timeout_ns = 10 * 1000 * 1000;
4360 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
4361 (xhci_service_interval_to_ns(desc) > timeout_ns))
4362 timeout_ns = xhci_service_interval_to_ns(desc);
4364 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
4365 if (u2_del_ns > timeout_ns)
4366 timeout_ns = u2_del_ns;
4368 /* The U2 timeout is encoded in 256us intervals */
4369 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
4370 /* If the necessary timeout value is bigger than what we can set in the
4371 * USB 3.0 hub, we have to disable hub-initiated U2.
4373 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4374 return timeout_ns;
4375 dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4376 "due to long timeout %llu ms\n", timeout_ns);
4377 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4380 static u16 xhci_call_host_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 if (state == USB3_LPM_U1) {
4387 if (xhci->quirks & XHCI_INTEL_HOST)
4388 return xhci_calculate_intel_u1_timeout(udev, desc);
4389 } else {
4390 if (xhci->quirks & XHCI_INTEL_HOST)
4391 return xhci_calculate_intel_u2_timeout(udev, desc);
4394 return USB3_LPM_DISABLED;
4397 static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4398 struct usb_device *udev,
4399 struct usb_endpoint_descriptor *desc,
4400 enum usb3_link_state state,
4401 u16 *timeout)
4403 u16 alt_timeout;
4405 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4406 desc, state, timeout);
4408 /* If we found we can't enable hub-initiated LPM, or
4409 * the U1 or U2 exit latency was too high to allow
4410 * device-initiated LPM as well, just stop searching.
4412 if (alt_timeout == USB3_LPM_DISABLED ||
4413 alt_timeout == USB3_LPM_DEVICE_INITIATED) {
4414 *timeout = alt_timeout;
4415 return -E2BIG;
4417 if (alt_timeout > *timeout)
4418 *timeout = alt_timeout;
4419 return 0;
4422 static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4423 struct usb_device *udev,
4424 struct usb_host_interface *alt,
4425 enum usb3_link_state state,
4426 u16 *timeout)
4428 int j;
4430 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4431 if (xhci_update_timeout_for_endpoint(xhci, udev,
4432 &alt->endpoint[j].desc, state, timeout))
4433 return -E2BIG;
4434 continue;
4436 return 0;
4439 static int xhci_check_intel_tier_policy(struct usb_device *udev,
4440 enum usb3_link_state state)
4442 struct usb_device *parent;
4443 unsigned int num_hubs;
4445 if (state == USB3_LPM_U2)
4446 return 0;
4448 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4449 for (parent = udev->parent, num_hubs = 0; parent->parent;
4450 parent = parent->parent)
4451 num_hubs++;
4453 if (num_hubs < 2)
4454 return 0;
4456 dev_dbg(&udev->dev, "Disabling U1 link state for device"
4457 " below second-tier hub.\n");
4458 dev_dbg(&udev->dev, "Plug device into first-tier hub "
4459 "to decrease power consumption.\n");
4460 return -E2BIG;
4463 static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4464 struct usb_device *udev,
4465 enum usb3_link_state state)
4467 if (xhci->quirks & XHCI_INTEL_HOST)
4468 return xhci_check_intel_tier_policy(udev, state);
4469 return -EINVAL;
4472 /* Returns the U1 or U2 timeout that should be enabled.
4473 * If the tier check or timeout setting functions return with a non-zero exit
4474 * code, that means the timeout value has been finalized and we shouldn't look
4475 * at any more endpoints.
4477 static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4478 struct usb_device *udev, enum usb3_link_state state)
4480 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4481 struct usb_host_config *config;
4482 char *state_name;
4483 int i;
4484 u16 timeout = USB3_LPM_DISABLED;
4486 if (state == USB3_LPM_U1)
4487 state_name = "U1";
4488 else if (state == USB3_LPM_U2)
4489 state_name = "U2";
4490 else {
4491 dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4492 state);
4493 return timeout;
4496 if (xhci_check_tier_policy(xhci, udev, state) < 0)
4497 return timeout;
4499 /* Gather some information about the currently installed configuration
4500 * and alternate interface settings.
4502 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4503 state, &timeout))
4504 return timeout;
4506 config = udev->actconfig;
4507 if (!config)
4508 return timeout;
4510 for (i = 0; i < USB_MAXINTERFACES; i++) {
4511 struct usb_driver *driver;
4512 struct usb_interface *intf = config->interface[i];
4514 if (!intf)
4515 continue;
4517 /* Check if any currently bound drivers want hub-initiated LPM
4518 * disabled.
4520 if (intf->dev.driver) {
4521 driver = to_usb_driver(intf->dev.driver);
4522 if (driver && driver->disable_hub_initiated_lpm) {
4523 dev_dbg(&udev->dev, "Hub-initiated %s disabled "
4524 "at request of driver %s\n",
4525 state_name, driver->name);
4526 return xhci_get_timeout_no_hub_lpm(udev, state);
4530 /* Not sure how this could happen... */
4531 if (!intf->cur_altsetting)
4532 continue;
4534 if (xhci_update_timeout_for_interface(xhci, udev,
4535 intf->cur_altsetting,
4536 state, &timeout))
4537 return timeout;
4539 return timeout;
4542 static int calculate_max_exit_latency(struct usb_device *udev,
4543 enum usb3_link_state state_changed,
4544 u16 hub_encoded_timeout)
4546 unsigned long long u1_mel_us = 0;
4547 unsigned long long u2_mel_us = 0;
4548 unsigned long long mel_us = 0;
4549 bool disabling_u1;
4550 bool disabling_u2;
4551 bool enabling_u1;
4552 bool enabling_u2;
4554 disabling_u1 = (state_changed == USB3_LPM_U1 &&
4555 hub_encoded_timeout == USB3_LPM_DISABLED);
4556 disabling_u2 = (state_changed == USB3_LPM_U2 &&
4557 hub_encoded_timeout == USB3_LPM_DISABLED);
4559 enabling_u1 = (state_changed == USB3_LPM_U1 &&
4560 hub_encoded_timeout != USB3_LPM_DISABLED);
4561 enabling_u2 = (state_changed == USB3_LPM_U2 &&
4562 hub_encoded_timeout != USB3_LPM_DISABLED);
4564 /* If U1 was already enabled and we're not disabling it,
4565 * or we're going to enable U1, account for the U1 max exit latency.
4567 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4568 enabling_u1)
4569 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4570 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4571 enabling_u2)
4572 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4574 if (u1_mel_us > u2_mel_us)
4575 mel_us = u1_mel_us;
4576 else
4577 mel_us = u2_mel_us;
4578 /* xHCI host controller max exit latency field is only 16 bits wide. */
4579 if (mel_us > MAX_EXIT) {
4580 dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4581 "is too big.\n", mel_us);
4582 return -E2BIG;
4584 return mel_us;
4587 /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4588 int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4589 struct usb_device *udev, enum usb3_link_state state)
4591 struct xhci_hcd *xhci;
4592 u16 hub_encoded_timeout;
4593 int mel;
4594 int ret;
4596 xhci = hcd_to_xhci(hcd);
4597 /* The LPM timeout values are pretty host-controller specific, so don't
4598 * enable hub-initiated timeouts unless the vendor has provided
4599 * information about their timeout algorithm.
4601 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4602 !xhci->devs[udev->slot_id])
4603 return USB3_LPM_DISABLED;
4605 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
4606 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
4607 if (mel < 0) {
4608 /* Max Exit Latency is too big, disable LPM. */
4609 hub_encoded_timeout = USB3_LPM_DISABLED;
4610 mel = 0;
4613 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4614 if (ret)
4615 return ret;
4616 return hub_encoded_timeout;
4619 int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4620 struct usb_device *udev, enum usb3_link_state state)
4622 struct xhci_hcd *xhci;
4623 u16 mel;
4624 int ret;
4626 xhci = hcd_to_xhci(hcd);
4627 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4628 !xhci->devs[udev->slot_id])
4629 return 0;
4631 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
4632 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4633 if (ret)
4634 return ret;
4635 return 0;
4637 #else /* CONFIG_PM */
4639 int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4640 struct usb_device *udev, enum usb3_link_state state)
4642 return USB3_LPM_DISABLED;
4645 int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4646 struct usb_device *udev, enum usb3_link_state state)
4648 return 0;
4650 #endif /* CONFIG_PM */
4652 /*-------------------------------------------------------------------------*/
4654 /* Once a hub descriptor is fetched for a device, we need to update the xHC's
4655 * internal data structures for the device.
4657 int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
4658 struct usb_tt *tt, gfp_t mem_flags)
4660 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4661 struct xhci_virt_device *vdev;
4662 struct xhci_command *config_cmd;
4663 struct xhci_input_control_ctx *ctrl_ctx;
4664 struct xhci_slot_ctx *slot_ctx;
4665 unsigned long flags;
4666 unsigned think_time;
4667 int ret;
4669 /* Ignore root hubs */
4670 if (!hdev->parent)
4671 return 0;
4673 vdev = xhci->devs[hdev->slot_id];
4674 if (!vdev) {
4675 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4676 return -EINVAL;
4678 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
4679 if (!config_cmd) {
4680 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
4681 return -ENOMEM;
4683 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
4684 if (!ctrl_ctx) {
4685 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4686 __func__);
4687 xhci_free_command(xhci, config_cmd);
4688 return -ENOMEM;
4691 spin_lock_irqsave(&xhci->lock, flags);
4692 if (hdev->speed == USB_SPEED_HIGH &&
4693 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4694 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4695 xhci_free_command(xhci, config_cmd);
4696 spin_unlock_irqrestore(&xhci->lock, flags);
4697 return -ENOMEM;
4700 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
4701 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
4702 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
4703 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
4705 * refer to section 6.2.2: MTT should be 0 for full speed hub,
4706 * but it may be already set to 1 when setup an xHCI virtual
4707 * device, so clear it anyway.
4709 if (tt->multi)
4710 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
4711 else if (hdev->speed == USB_SPEED_FULL)
4712 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
4714 if (xhci->hci_version > 0x95) {
4715 xhci_dbg(xhci, "xHCI version %x needs hub "
4716 "TT think time and number of ports\n",
4717 (unsigned int) xhci->hci_version);
4718 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
4719 /* Set TT think time - convert from ns to FS bit times.
4720 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4721 * 2 = 24 FS bit times, 3 = 32 FS bit times.
4723 * xHCI 1.0: this field shall be 0 if the device is not a
4724 * High-spped hub.
4726 think_time = tt->think_time;
4727 if (think_time != 0)
4728 think_time = (think_time / 666) - 1;
4729 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4730 slot_ctx->tt_info |=
4731 cpu_to_le32(TT_THINK_TIME(think_time));
4732 } else {
4733 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4734 "TT think time or number of ports\n",
4735 (unsigned int) xhci->hci_version);
4737 slot_ctx->dev_state = 0;
4738 spin_unlock_irqrestore(&xhci->lock, flags);
4740 xhci_dbg(xhci, "Set up %s for hub device.\n",
4741 (xhci->hci_version > 0x95) ?
4742 "configure endpoint" : "evaluate context");
4743 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
4744 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
4746 /* Issue and wait for the configure endpoint or
4747 * evaluate context command.
4749 if (xhci->hci_version > 0x95)
4750 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4751 false, false);
4752 else
4753 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4754 true, false);
4756 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
4757 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
4759 xhci_free_command(xhci, config_cmd);
4760 return ret;
4763 int xhci_get_frame(struct usb_hcd *hcd)
4765 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4766 /* EHCI mods by the periodic size. Why? */
4767 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
4770 int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4772 struct xhci_hcd *xhci;
4773 struct device *dev = hcd->self.controller;
4774 int retval;
4776 /* Accept arbitrarily long scatter-gather lists */
4777 hcd->self.sg_tablesize = ~0;
4779 /* support to build packet from discontinuous buffers */
4780 hcd->self.no_sg_constraint = 1;
4782 /* XHCI controllers don't stop the ep queue on short packets :| */
4783 hcd->self.no_stop_on_short = 1;
4785 if (usb_hcd_is_primary_hcd(hcd)) {
4786 xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
4787 if (!xhci)
4788 return -ENOMEM;
4789 *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
4790 xhci->main_hcd = hcd;
4791 /* Mark the first roothub as being USB 2.0.
4792 * The xHCI driver will register the USB 3.0 roothub.
4794 hcd->speed = HCD_USB2;
4795 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4797 * USB 2.0 roothub under xHCI has an integrated TT,
4798 * (rate matching hub) as opposed to having an OHCI/UHCI
4799 * companion controller.
4801 hcd->has_tt = 1;
4802 } else {
4803 /* xHCI private pointer was set in xhci_pci_probe for the second
4804 * registered roothub.
4806 return 0;
4809 xhci->cap_regs = hcd->regs;
4810 xhci->op_regs = hcd->regs +
4811 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
4812 xhci->run_regs = hcd->regs +
4813 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
4814 /* Cache read-only capability registers */
4815 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
4816 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
4817 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
4818 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
4819 xhci->hci_version = HC_VERSION(xhci->hcc_params);
4820 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4821 xhci_print_registers(xhci);
4823 get_quirks(dev, xhci);
4825 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4826 * success event after a short transfer. This quirk will ignore such
4827 * spurious event.
4829 if (xhci->hci_version > 0x96)
4830 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
4832 /* Make sure the HC is halted. */
4833 retval = xhci_halt(xhci);
4834 if (retval)
4835 goto error;
4837 xhci_dbg(xhci, "Resetting HCD\n");
4838 /* Reset the internal HC memory state and registers. */
4839 retval = xhci_reset(xhci);
4840 if (retval)
4841 goto error;
4842 xhci_dbg(xhci, "Reset complete\n");
4844 /* Set dma_mask and coherent_dma_mask to 64-bits,
4845 * if xHC supports 64-bit addressing */
4846 if (HCC_64BIT_ADDR(xhci->hcc_params) &&
4847 !dma_set_mask(dev, DMA_BIT_MASK(64))) {
4848 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4849 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
4852 xhci_dbg(xhci, "Calling HCD init\n");
4853 /* Initialize HCD and host controller data structures. */
4854 retval = xhci_init(hcd);
4855 if (retval)
4856 goto error;
4857 xhci_dbg(xhci, "Called HCD init\n");
4858 return 0;
4859 error:
4860 kfree(xhci);
4861 return retval;
4864 MODULE_DESCRIPTION(DRIVER_DESC);
4865 MODULE_AUTHOR(DRIVER_AUTHOR);
4866 MODULE_LICENSE("GPL");
4868 static int __init xhci_hcd_init(void)
4870 int retval;
4872 if (usb_disabled())
4873 return -ENODEV;
4875 retval = xhci_register_pci();
4876 if (retval < 0) {
4877 pr_debug("Problem registering PCI driver.\n");
4878 return retval;
4880 retval = xhci_register_plat();
4881 if (retval < 0) {
4882 pr_debug("Problem registering platform driver.\n");
4883 goto unreg_pci;
4886 * Check the compiler generated sizes of structures that must be laid
4887 * out in specific ways for hardware access.
4889 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
4890 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
4891 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
4892 /* xhci_device_control has eight fields, and also
4893 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
4895 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
4896 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
4897 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
4898 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
4899 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
4900 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
4901 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
4903 return 0;
4904 unreg_pci:
4905 xhci_unregister_pci();
4906 return retval;
4908 module_init(xhci_hcd_init);
4910 static void __exit xhci_hcd_cleanup(void)
4912 xhci_unregister_pci();
4913 xhci_unregister_plat();
4915 module_exit(xhci_hcd_cleanup);