1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * xHCI host controller driver 4 * 5 * Copyright (C) 2008 Intel Corp. 6 * 7 * Author: Sarah Sharp 8 * Some code borrowed from the Linux EHCI driver. 9 */ 10 11 #include <linux/pci.h> 12 #include <linux/iopoll.h> 13 #include <linux/irq.h> 14 #include <linux/log2.h> 15 #include <linux/module.h> 16 #include <linux/moduleparam.h> 17 #include <linux/slab.h> 18 #include <linux/dmi.h> 19 #include <linux/dma-mapping.h> 20 21 #include "xhci.h" 22 #include "xhci-trace.h" 23 #include "xhci-mtk.h" 24 #include "xhci-debugfs.h" 25 #include "xhci-dbgcap.h" 26 27 #define DRIVER_AUTHOR "Sarah Sharp" 28 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver" 29 30 #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E) 31 32 /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */ 33 static int link_quirk; 34 module_param(link_quirk, int, S_IRUGO | S_IWUSR); 35 MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB"); 36 37 static unsigned long long quirks; 38 module_param(quirks, ullong, S_IRUGO); 39 MODULE_PARM_DESC(quirks, "Bit flags for quirks to be enabled as default"); 40 41 static bool td_on_ring(struct xhci_td *td, struct xhci_ring *ring) 42 { 43 struct xhci_segment *seg = ring->first_seg; 44 45 if (!td || !td->start_seg) 46 return false; 47 do { 48 if (seg == td->start_seg) 49 return true; 50 seg = seg->next; 51 } while (seg && seg != ring->first_seg); 52 53 return false; 54 } 55 56 /* 57 * xhci_handshake - spin reading hc until handshake completes or fails 58 * @ptr: address of hc register to be read 59 * @mask: bits to look at in result of read 60 * @done: value of those bits when handshake succeeds 61 * @usec: timeout in microseconds 62 * 63 * Returns negative errno, or zero on success 64 * 65 * Success happens when the "mask" bits have the specified value (hardware 66 * handshake done). There are two failure modes: "usec" have passed (major 67 * hardware flakeout), or the register reads as all-ones (hardware removed). 68 */ 69 int xhci_handshake(void __iomem *ptr, u32 mask, u32 done, int usec) 70 { 71 u32 result; 72 int ret; 73 74 ret = readl_poll_timeout_atomic(ptr, result, 75 (result & mask) == done || 76 result == U32_MAX, 77 1, usec); 78 if (result == U32_MAX) /* card removed */ 79 return -ENODEV; 80 81 return ret; 82 } 83 84 /* 85 * Disable interrupts and begin the xHCI halting process. 86 */ 87 void xhci_quiesce(struct xhci_hcd *xhci) 88 { 89 u32 halted; 90 u32 cmd; 91 u32 mask; 92 93 mask = ~(XHCI_IRQS); 94 halted = readl(&xhci->op_regs->status) & STS_HALT; 95 if (!halted) 96 mask &= ~CMD_RUN; 97 98 cmd = readl(&xhci->op_regs->command); 99 cmd &= mask; 100 writel(cmd, &xhci->op_regs->command); 101 } 102 103 /* 104 * Force HC into halt state. 105 * 106 * Disable any IRQs and clear the run/stop bit. 107 * HC will complete any current and actively pipelined transactions, and 108 * should halt within 16 ms of the run/stop bit being cleared. 109 * Read HC Halted bit in the status register to see when the HC is finished. 110 */ 111 int xhci_halt(struct xhci_hcd *xhci) 112 { 113 int ret; 114 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC"); 115 xhci_quiesce(xhci); 116 117 ret = xhci_handshake(&xhci->op_regs->status, 118 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC); 119 if (ret) { 120 xhci_warn(xhci, "Host halt failed, %d\n", ret); 121 return ret; 122 } 123 xhci->xhc_state |= XHCI_STATE_HALTED; 124 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; 125 return ret; 126 } 127 128 /* 129 * Set the run bit and wait for the host to be running. 130 */ 131 int xhci_start(struct xhci_hcd *xhci) 132 { 133 u32 temp; 134 int ret; 135 136 temp = readl(&xhci->op_regs->command); 137 temp |= (CMD_RUN); 138 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.", 139 temp); 140 writel(temp, &xhci->op_regs->command); 141 142 /* 143 * Wait for the HCHalted Status bit to be 0 to indicate the host is 144 * running. 145 */ 146 ret = xhci_handshake(&xhci->op_regs->status, 147 STS_HALT, 0, XHCI_MAX_HALT_USEC); 148 if (ret == -ETIMEDOUT) 149 xhci_err(xhci, "Host took too long to start, " 150 "waited %u microseconds.\n", 151 XHCI_MAX_HALT_USEC); 152 if (!ret) 153 /* clear state flags. Including dying, halted or removing */ 154 xhci->xhc_state = 0; 155 156 return ret; 157 } 158 159 /* 160 * Reset a halted HC. 161 * 162 * This resets pipelines, timers, counters, state machines, etc. 163 * Transactions will be terminated immediately, and operational registers 164 * will be set to their defaults. 165 */ 166 int xhci_reset(struct xhci_hcd *xhci) 167 { 168 u32 command; 169 u32 state; 170 int ret; 171 172 state = readl(&xhci->op_regs->status); 173 174 if (state == ~(u32)0) { 175 xhci_warn(xhci, "Host not accessible, reset failed.\n"); 176 return -ENODEV; 177 } 178 179 if ((state & STS_HALT) == 0) { 180 xhci_warn(xhci, "Host controller not halted, aborting reset.\n"); 181 return 0; 182 } 183 184 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC"); 185 command = readl(&xhci->op_regs->command); 186 command |= CMD_RESET; 187 writel(command, &xhci->op_regs->command); 188 189 /* Existing Intel xHCI controllers require a delay of 1 mS, 190 * after setting the CMD_RESET bit, and before accessing any 191 * HC registers. This allows the HC to complete the 192 * reset operation and be ready for HC register access. 193 * Without this delay, the subsequent HC register access, 194 * may result in a system hang very rarely. 195 */ 196 if (xhci->quirks & XHCI_INTEL_HOST) 197 udelay(1000); 198 199 ret = xhci_handshake(&xhci->op_regs->command, 200 CMD_RESET, 0, 10 * 1000 * 1000); 201 if (ret) 202 return ret; 203 204 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL) 205 usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci)->self.controller)); 206 207 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 208 "Wait for controller to be ready for doorbell rings"); 209 /* 210 * xHCI cannot write to any doorbells or operational registers other 211 * than status until the "Controller Not Ready" flag is cleared. 212 */ 213 ret = xhci_handshake(&xhci->op_regs->status, 214 STS_CNR, 0, 10 * 1000 * 1000); 215 216 xhci->usb2_rhub.bus_state.port_c_suspend = 0; 217 xhci->usb2_rhub.bus_state.suspended_ports = 0; 218 xhci->usb2_rhub.bus_state.resuming_ports = 0; 219 xhci->usb3_rhub.bus_state.port_c_suspend = 0; 220 xhci->usb3_rhub.bus_state.suspended_ports = 0; 221 xhci->usb3_rhub.bus_state.resuming_ports = 0; 222 223 return ret; 224 } 225 226 static void xhci_zero_64b_regs(struct xhci_hcd *xhci) 227 { 228 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 229 int err, i; 230 u64 val; 231 232 /* 233 * Some Renesas controllers get into a weird state if they are 234 * reset while programmed with 64bit addresses (they will preserve 235 * the top half of the address in internal, non visible 236 * registers). You end up with half the address coming from the 237 * kernel, and the other half coming from the firmware. Also, 238 * changing the programming leads to extra accesses even if the 239 * controller is supposed to be halted. The controller ends up with 240 * a fatal fault, and is then ripe for being properly reset. 241 * 242 * Special care is taken to only apply this if the device is behind 243 * an iommu. Doing anything when there is no iommu is definitely 244 * unsafe... 245 */ 246 if (!(xhci->quirks & XHCI_ZERO_64B_REGS) || !device_iommu_mapped(dev)) 247 return; 248 249 xhci_info(xhci, "Zeroing 64bit base registers, expecting fault\n"); 250 251 /* Clear HSEIE so that faults do not get signaled */ 252 val = readl(&xhci->op_regs->command); 253 val &= ~CMD_HSEIE; 254 writel(val, &xhci->op_regs->command); 255 256 /* Clear HSE (aka FATAL) */ 257 val = readl(&xhci->op_regs->status); 258 val |= STS_FATAL; 259 writel(val, &xhci->op_regs->status); 260 261 /* Now zero the registers, and brace for impact */ 262 val = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); 263 if (upper_32_bits(val)) 264 xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr); 265 val = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); 266 if (upper_32_bits(val)) 267 xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring); 268 269 for (i = 0; i < HCS_MAX_INTRS(xhci->hcs_params1); i++) { 270 struct xhci_intr_reg __iomem *ir; 271 272 ir = &xhci->run_regs->ir_set[i]; 273 val = xhci_read_64(xhci, &ir->erst_base); 274 if (upper_32_bits(val)) 275 xhci_write_64(xhci, 0, &ir->erst_base); 276 val= xhci_read_64(xhci, &ir->erst_dequeue); 277 if (upper_32_bits(val)) 278 xhci_write_64(xhci, 0, &ir->erst_dequeue); 279 } 280 281 /* Wait for the fault to appear. It will be cleared on reset */ 282 err = xhci_handshake(&xhci->op_regs->status, 283 STS_FATAL, STS_FATAL, 284 XHCI_MAX_HALT_USEC); 285 if (!err) 286 xhci_info(xhci, "Fault detected\n"); 287 } 288 289 #ifdef CONFIG_USB_PCI 290 /* 291 * Set up MSI 292 */ 293 static int xhci_setup_msi(struct xhci_hcd *xhci) 294 { 295 int ret; 296 /* 297 * TODO:Check with MSI Soc for sysdev 298 */ 299 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 300 301 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI); 302 if (ret < 0) { 303 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 304 "failed to allocate MSI entry"); 305 return ret; 306 } 307 308 ret = request_irq(pdev->irq, xhci_msi_irq, 309 0, "xhci_hcd", xhci_to_hcd(xhci)); 310 if (ret) { 311 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 312 "disable MSI interrupt"); 313 pci_free_irq_vectors(pdev); 314 } 315 316 return ret; 317 } 318 319 /* 320 * Set up MSI-X 321 */ 322 static int xhci_setup_msix(struct xhci_hcd *xhci) 323 { 324 int i, ret = 0; 325 struct usb_hcd *hcd = xhci_to_hcd(xhci); 326 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 327 328 /* 329 * calculate number of msi-x vectors supported. 330 * - HCS_MAX_INTRS: the max number of interrupts the host can handle, 331 * with max number of interrupters based on the xhci HCSPARAMS1. 332 * - num_online_cpus: maximum msi-x vectors per CPUs core. 333 * Add additional 1 vector to ensure always available interrupt. 334 */ 335 xhci->msix_count = min(num_online_cpus() + 1, 336 HCS_MAX_INTRS(xhci->hcs_params1)); 337 338 ret = pci_alloc_irq_vectors(pdev, xhci->msix_count, xhci->msix_count, 339 PCI_IRQ_MSIX); 340 if (ret < 0) { 341 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 342 "Failed to enable MSI-X"); 343 return ret; 344 } 345 346 for (i = 0; i < xhci->msix_count; i++) { 347 ret = request_irq(pci_irq_vector(pdev, i), xhci_msi_irq, 0, 348 "xhci_hcd", xhci_to_hcd(xhci)); 349 if (ret) 350 goto disable_msix; 351 } 352 353 hcd->msix_enabled = 1; 354 return ret; 355 356 disable_msix: 357 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt"); 358 while (--i >= 0) 359 free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci)); 360 pci_free_irq_vectors(pdev); 361 return ret; 362 } 363 364 /* Free any IRQs and disable MSI-X */ 365 static void xhci_cleanup_msix(struct xhci_hcd *xhci) 366 { 367 struct usb_hcd *hcd = xhci_to_hcd(xhci); 368 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 369 370 if (xhci->quirks & XHCI_PLAT) 371 return; 372 373 /* return if using legacy interrupt */ 374 if (hcd->irq > 0) 375 return; 376 377 if (hcd->msix_enabled) { 378 int i; 379 380 for (i = 0; i < xhci->msix_count; i++) 381 free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci)); 382 } else { 383 free_irq(pci_irq_vector(pdev, 0), xhci_to_hcd(xhci)); 384 } 385 386 pci_free_irq_vectors(pdev); 387 hcd->msix_enabled = 0; 388 } 389 390 static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci) 391 { 392 struct usb_hcd *hcd = xhci_to_hcd(xhci); 393 394 if (hcd->msix_enabled) { 395 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); 396 int i; 397 398 for (i = 0; i < xhci->msix_count; i++) 399 synchronize_irq(pci_irq_vector(pdev, i)); 400 } 401 } 402 403 static int xhci_try_enable_msi(struct usb_hcd *hcd) 404 { 405 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 406 struct pci_dev *pdev; 407 int ret; 408 409 /* The xhci platform device has set up IRQs through usb_add_hcd. */ 410 if (xhci->quirks & XHCI_PLAT) 411 return 0; 412 413 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 414 /* 415 * Some Fresco Logic host controllers advertise MSI, but fail to 416 * generate interrupts. Don't even try to enable MSI. 417 */ 418 if (xhci->quirks & XHCI_BROKEN_MSI) 419 goto legacy_irq; 420 421 /* unregister the legacy interrupt */ 422 if (hcd->irq) 423 free_irq(hcd->irq, hcd); 424 hcd->irq = 0; 425 426 ret = xhci_setup_msix(xhci); 427 if (ret) 428 /* fall back to msi*/ 429 ret = xhci_setup_msi(xhci); 430 431 if (!ret) { 432 hcd->msi_enabled = 1; 433 return 0; 434 } 435 436 if (!pdev->irq) { 437 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n"); 438 return -EINVAL; 439 } 440 441 legacy_irq: 442 if (!strlen(hcd->irq_descr)) 443 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d", 444 hcd->driver->description, hcd->self.busnum); 445 446 /* fall back to legacy interrupt*/ 447 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED, 448 hcd->irq_descr, hcd); 449 if (ret) { 450 xhci_err(xhci, "request interrupt %d failed\n", 451 pdev->irq); 452 return ret; 453 } 454 hcd->irq = pdev->irq; 455 return 0; 456 } 457 458 #else 459 460 static inline int xhci_try_enable_msi(struct usb_hcd *hcd) 461 { 462 return 0; 463 } 464 465 static inline void xhci_cleanup_msix(struct xhci_hcd *xhci) 466 { 467 } 468 469 static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci) 470 { 471 } 472 473 #endif 474 475 static void compliance_mode_recovery(struct timer_list *t) 476 { 477 struct xhci_hcd *xhci; 478 struct usb_hcd *hcd; 479 struct xhci_hub *rhub; 480 u32 temp; 481 int i; 482 483 xhci = from_timer(xhci, t, comp_mode_recovery_timer); 484 rhub = &xhci->usb3_rhub; 485 486 for (i = 0; i < rhub->num_ports; i++) { 487 temp = readl(rhub->ports[i]->addr); 488 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) { 489 /* 490 * Compliance Mode Detected. Letting USB Core 491 * handle the Warm Reset 492 */ 493 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 494 "Compliance mode detected->port %d", 495 i + 1); 496 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 497 "Attempting compliance mode recovery"); 498 hcd = xhci->shared_hcd; 499 500 if (hcd->state == HC_STATE_SUSPENDED) 501 usb_hcd_resume_root_hub(hcd); 502 503 usb_hcd_poll_rh_status(hcd); 504 } 505 } 506 507 if (xhci->port_status_u0 != ((1 << rhub->num_ports) - 1)) 508 mod_timer(&xhci->comp_mode_recovery_timer, 509 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS)); 510 } 511 512 /* 513 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver 514 * that causes ports behind that hardware to enter compliance mode sometimes. 515 * The quirk creates a timer that polls every 2 seconds the link state of 516 * each host controller's port and recovers it by issuing a Warm reset 517 * if Compliance mode is detected, otherwise the port will become "dead" (no 518 * device connections or disconnections will be detected anymore). Becasue no 519 * status event is generated when entering compliance mode (per xhci spec), 520 * this quirk is needed on systems that have the failing hardware installed. 521 */ 522 static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci) 523 { 524 xhci->port_status_u0 = 0; 525 timer_setup(&xhci->comp_mode_recovery_timer, compliance_mode_recovery, 526 0); 527 xhci->comp_mode_recovery_timer.expires = jiffies + 528 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS); 529 530 add_timer(&xhci->comp_mode_recovery_timer); 531 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 532 "Compliance mode recovery timer initialized"); 533 } 534 535 /* 536 * This function identifies the systems that have installed the SN65LVPE502CP 537 * USB3.0 re-driver and that need the Compliance Mode Quirk. 538 * Systems: 539 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820 540 */ 541 static bool xhci_compliance_mode_recovery_timer_quirk_check(void) 542 { 543 const char *dmi_product_name, *dmi_sys_vendor; 544 545 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME); 546 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR); 547 if (!dmi_product_name || !dmi_sys_vendor) 548 return false; 549 550 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard"))) 551 return false; 552 553 if (strstr(dmi_product_name, "Z420") || 554 strstr(dmi_product_name, "Z620") || 555 strstr(dmi_product_name, "Z820") || 556 strstr(dmi_product_name, "Z1 Workstation")) 557 return true; 558 559 return false; 560 } 561 562 static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci) 563 { 564 return (xhci->port_status_u0 == ((1 << xhci->usb3_rhub.num_ports) - 1)); 565 } 566 567 568 /* 569 * Initialize memory for HCD and xHC (one-time init). 570 * 571 * Program the PAGESIZE register, initialize the device context array, create 572 * device contexts (?), set up a command ring segment (or two?), create event 573 * ring (one for now). 574 */ 575 static int xhci_init(struct usb_hcd *hcd) 576 { 577 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 578 int retval = 0; 579 580 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init"); 581 spin_lock_init(&xhci->lock); 582 if (xhci->hci_version == 0x95 && link_quirk) { 583 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 584 "QUIRK: Not clearing Link TRB chain bits."); 585 xhci->quirks |= XHCI_LINK_TRB_QUIRK; 586 } else { 587 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 588 "xHCI doesn't need link TRB QUIRK"); 589 } 590 retval = xhci_mem_init(xhci, GFP_KERNEL); 591 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init"); 592 593 /* Initializing Compliance Mode Recovery Data If Needed */ 594 if (xhci_compliance_mode_recovery_timer_quirk_check()) { 595 xhci->quirks |= XHCI_COMP_MODE_QUIRK; 596 compliance_mode_recovery_timer_init(xhci); 597 } 598 599 return retval; 600 } 601 602 /*-------------------------------------------------------------------------*/ 603 604 605 static int xhci_run_finished(struct xhci_hcd *xhci) 606 { 607 if (xhci_start(xhci)) { 608 xhci_halt(xhci); 609 return -ENODEV; 610 } 611 xhci->shared_hcd->state = HC_STATE_RUNNING; 612 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING; 613 614 if (xhci->quirks & XHCI_NEC_HOST) 615 xhci_ring_cmd_db(xhci); 616 617 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 618 "Finished xhci_run for USB3 roothub"); 619 return 0; 620 } 621 622 /* 623 * Start the HC after it was halted. 624 * 625 * This function is called by the USB core when the HC driver is added. 626 * Its opposite is xhci_stop(). 627 * 628 * xhci_init() must be called once before this function can be called. 629 * Reset the HC, enable device slot contexts, program DCBAAP, and 630 * set command ring pointer and event ring pointer. 631 * 632 * Setup MSI-X vectors and enable interrupts. 633 */ 634 int xhci_run(struct usb_hcd *hcd) 635 { 636 u32 temp; 637 u64 temp_64; 638 int ret; 639 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 640 641 /* Start the xHCI host controller running only after the USB 2.0 roothub 642 * is setup. 643 */ 644 645 hcd->uses_new_polling = 1; 646 if (!usb_hcd_is_primary_hcd(hcd)) 647 return xhci_run_finished(xhci); 648 649 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run"); 650 651 ret = xhci_try_enable_msi(hcd); 652 if (ret) 653 return ret; 654 655 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); 656 temp_64 &= ~ERST_PTR_MASK; 657 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 658 "ERST deq = 64'h%0lx", (long unsigned int) temp_64); 659 660 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 661 "// Set the interrupt modulation register"); 662 temp = readl(&xhci->ir_set->irq_control); 663 temp &= ~ER_IRQ_INTERVAL_MASK; 664 temp |= (xhci->imod_interval / 250) & ER_IRQ_INTERVAL_MASK; 665 writel(temp, &xhci->ir_set->irq_control); 666 667 /* Set the HCD state before we enable the irqs */ 668 temp = readl(&xhci->op_regs->command); 669 temp |= (CMD_EIE); 670 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 671 "// Enable interrupts, cmd = 0x%x.", temp); 672 writel(temp, &xhci->op_regs->command); 673 674 temp = readl(&xhci->ir_set->irq_pending); 675 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 676 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending", 677 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp)); 678 writel(ER_IRQ_ENABLE(temp), &xhci->ir_set->irq_pending); 679 680 if (xhci->quirks & XHCI_NEC_HOST) { 681 struct xhci_command *command; 682 683 command = xhci_alloc_command(xhci, false, GFP_KERNEL); 684 if (!command) 685 return -ENOMEM; 686 687 ret = xhci_queue_vendor_command(xhci, command, 0, 0, 0, 688 TRB_TYPE(TRB_NEC_GET_FW)); 689 if (ret) 690 xhci_free_command(xhci, command); 691 } 692 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 693 "Finished xhci_run for USB2 roothub"); 694 695 xhci_dbc_init(xhci); 696 697 xhci_debugfs_init(xhci); 698 699 return 0; 700 } 701 EXPORT_SYMBOL_GPL(xhci_run); 702 703 /* 704 * Stop xHCI driver. 705 * 706 * This function is called by the USB core when the HC driver is removed. 707 * Its opposite is xhci_run(). 708 * 709 * Disable device contexts, disable IRQs, and quiesce the HC. 710 * Reset the HC, finish any completed transactions, and cleanup memory. 711 */ 712 static void xhci_stop(struct usb_hcd *hcd) 713 { 714 u32 temp; 715 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 716 717 mutex_lock(&xhci->mutex); 718 719 /* Only halt host and free memory after both hcds are removed */ 720 if (!usb_hcd_is_primary_hcd(hcd)) { 721 mutex_unlock(&xhci->mutex); 722 return; 723 } 724 725 xhci_dbc_exit(xhci); 726 727 spin_lock_irq(&xhci->lock); 728 xhci->xhc_state |= XHCI_STATE_HALTED; 729 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; 730 xhci_halt(xhci); 731 xhci_reset(xhci); 732 spin_unlock_irq(&xhci->lock); 733 734 xhci_cleanup_msix(xhci); 735 736 /* Deleting Compliance Mode Recovery Timer */ 737 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && 738 (!(xhci_all_ports_seen_u0(xhci)))) { 739 del_timer_sync(&xhci->comp_mode_recovery_timer); 740 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 741 "%s: compliance mode recovery timer deleted", 742 __func__); 743 } 744 745 if (xhci->quirks & XHCI_AMD_PLL_FIX) 746 usb_amd_dev_put(); 747 748 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 749 "// Disabling event ring interrupts"); 750 temp = readl(&xhci->op_regs->status); 751 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status); 752 temp = readl(&xhci->ir_set->irq_pending); 753 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending); 754 755 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory"); 756 xhci_mem_cleanup(xhci); 757 xhci_debugfs_exit(xhci); 758 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 759 "xhci_stop completed - status = %x", 760 readl(&xhci->op_regs->status)); 761 mutex_unlock(&xhci->mutex); 762 } 763 764 /* 765 * Shutdown HC (not bus-specific) 766 * 767 * This is called when the machine is rebooting or halting. We assume that the 768 * machine will be powered off, and the HC's internal state will be reset. 769 * Don't bother to free memory. 770 * 771 * This will only ever be called with the main usb_hcd (the USB3 roothub). 772 */ 773 void xhci_shutdown(struct usb_hcd *hcd) 774 { 775 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 776 777 if (xhci->quirks & XHCI_SPURIOUS_REBOOT) 778 usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev)); 779 780 spin_lock_irq(&xhci->lock); 781 xhci_halt(xhci); 782 /* Workaround for spurious wakeups at shutdown with HSW */ 783 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP) 784 xhci_reset(xhci); 785 spin_unlock_irq(&xhci->lock); 786 787 xhci_cleanup_msix(xhci); 788 789 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 790 "xhci_shutdown completed - status = %x", 791 readl(&xhci->op_regs->status)); 792 } 793 EXPORT_SYMBOL_GPL(xhci_shutdown); 794 795 #ifdef CONFIG_PM 796 static void xhci_save_registers(struct xhci_hcd *xhci) 797 { 798 xhci->s3.command = readl(&xhci->op_regs->command); 799 xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification); 800 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); 801 xhci->s3.config_reg = readl(&xhci->op_regs->config_reg); 802 xhci->s3.erst_size = readl(&xhci->ir_set->erst_size); 803 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base); 804 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); 805 xhci->s3.irq_pending = readl(&xhci->ir_set->irq_pending); 806 xhci->s3.irq_control = readl(&xhci->ir_set->irq_control); 807 } 808 809 static void xhci_restore_registers(struct xhci_hcd *xhci) 810 { 811 writel(xhci->s3.command, &xhci->op_regs->command); 812 writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification); 813 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr); 814 writel(xhci->s3.config_reg, &xhci->op_regs->config_reg); 815 writel(xhci->s3.erst_size, &xhci->ir_set->erst_size); 816 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base); 817 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue); 818 writel(xhci->s3.irq_pending, &xhci->ir_set->irq_pending); 819 writel(xhci->s3.irq_control, &xhci->ir_set->irq_control); 820 } 821 822 static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci) 823 { 824 u64 val_64; 825 826 /* step 2: initialize command ring buffer */ 827 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); 828 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) | 829 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, 830 xhci->cmd_ring->dequeue) & 831 (u64) ~CMD_RING_RSVD_BITS) | 832 xhci->cmd_ring->cycle_state; 833 xhci_dbg_trace(xhci, trace_xhci_dbg_init, 834 "// Setting command ring address to 0x%llx", 835 (long unsigned long) val_64); 836 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring); 837 } 838 839 /* 840 * The whole command ring must be cleared to zero when we suspend the host. 841 * 842 * The host doesn't save the command ring pointer in the suspend well, so we 843 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte 844 * aligned, because of the reserved bits in the command ring dequeue pointer 845 * register. Therefore, we can't just set the dequeue pointer back in the 846 * middle of the ring (TRBs are 16-byte aligned). 847 */ 848 static void xhci_clear_command_ring(struct xhci_hcd *xhci) 849 { 850 struct xhci_ring *ring; 851 struct xhci_segment *seg; 852 853 ring = xhci->cmd_ring; 854 seg = ring->deq_seg; 855 do { 856 memset(seg->trbs, 0, 857 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1)); 858 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &= 859 cpu_to_le32(~TRB_CYCLE); 860 seg = seg->next; 861 } while (seg != ring->deq_seg); 862 863 /* Reset the software enqueue and dequeue pointers */ 864 ring->deq_seg = ring->first_seg; 865 ring->dequeue = ring->first_seg->trbs; 866 ring->enq_seg = ring->deq_seg; 867 ring->enqueue = ring->dequeue; 868 869 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1; 870 /* 871 * Ring is now zeroed, so the HW should look for change of ownership 872 * when the cycle bit is set to 1. 873 */ 874 ring->cycle_state = 1; 875 876 /* 877 * Reset the hardware dequeue pointer. 878 * Yes, this will need to be re-written after resume, but we're paranoid 879 * and want to make sure the hardware doesn't access bogus memory 880 * because, say, the BIOS or an SMI started the host without changing 881 * the command ring pointers. 882 */ 883 xhci_set_cmd_ring_deq(xhci); 884 } 885 886 /* 887 * Disable port wake bits if do_wakeup is not set. 888 * 889 * Also clear a possible internal port wake state left hanging for ports that 890 * detected termination but never successfully enumerated (trained to 0U). 891 * Internal wake causes immediate xHCI wake after suspend. PORT_CSC write done 892 * at enumeration clears this wake, force one here as well for unconnected ports 893 */ 894 895 static void xhci_disable_hub_port_wake(struct xhci_hcd *xhci, 896 struct xhci_hub *rhub, 897 bool do_wakeup) 898 { 899 unsigned long flags; 900 u32 t1, t2, portsc; 901 int i; 902 903 spin_lock_irqsave(&xhci->lock, flags); 904 905 for (i = 0; i < rhub->num_ports; i++) { 906 portsc = readl(rhub->ports[i]->addr); 907 t1 = xhci_port_state_to_neutral(portsc); 908 t2 = t1; 909 910 /* clear wake bits if do_wake is not set */ 911 if (!do_wakeup) 912 t2 &= ~PORT_WAKE_BITS; 913 914 /* Don't touch csc bit if connected or connect change is set */ 915 if (!(portsc & (PORT_CSC | PORT_CONNECT))) 916 t2 |= PORT_CSC; 917 918 if (t1 != t2) { 919 writel(t2, rhub->ports[i]->addr); 920 xhci_dbg(xhci, "config port %d-%d wake bits, portsc: 0x%x, write: 0x%x\n", 921 rhub->hcd->self.busnum, i + 1, portsc, t2); 922 } 923 } 924 spin_unlock_irqrestore(&xhci->lock, flags); 925 } 926 927 static bool xhci_pending_portevent(struct xhci_hcd *xhci) 928 { 929 struct xhci_port **ports; 930 int port_index; 931 u32 status; 932 u32 portsc; 933 934 status = readl(&xhci->op_regs->status); 935 if (status & STS_EINT) 936 return true; 937 /* 938 * Checking STS_EINT is not enough as there is a lag between a change 939 * bit being set and the Port Status Change Event that it generated 940 * being written to the Event Ring. See note in xhci 1.1 section 4.19.2. 941 */ 942 943 port_index = xhci->usb2_rhub.num_ports; 944 ports = xhci->usb2_rhub.ports; 945 while (port_index--) { 946 portsc = readl(ports[port_index]->addr); 947 if (portsc & PORT_CHANGE_MASK || 948 (portsc & PORT_PLS_MASK) == XDEV_RESUME) 949 return true; 950 } 951 port_index = xhci->usb3_rhub.num_ports; 952 ports = xhci->usb3_rhub.ports; 953 while (port_index--) { 954 portsc = readl(ports[port_index]->addr); 955 if (portsc & PORT_CHANGE_MASK || 956 (portsc & PORT_PLS_MASK) == XDEV_RESUME) 957 return true; 958 } 959 return false; 960 } 961 962 /* 963 * Stop HC (not bus-specific) 964 * 965 * This is called when the machine transition into S3/S4 mode. 966 * 967 */ 968 int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup) 969 { 970 int rc = 0; 971 unsigned int delay = XHCI_MAX_HALT_USEC * 2; 972 struct usb_hcd *hcd = xhci_to_hcd(xhci); 973 u32 command; 974 u32 res; 975 976 if (!hcd->state) 977 return 0; 978 979 if (hcd->state != HC_STATE_SUSPENDED || 980 xhci->shared_hcd->state != HC_STATE_SUSPENDED) 981 return -EINVAL; 982 983 /* Clear root port wake on bits if wakeup not allowed. */ 984 xhci_disable_hub_port_wake(xhci, &xhci->usb3_rhub, do_wakeup); 985 xhci_disable_hub_port_wake(xhci, &xhci->usb2_rhub, do_wakeup); 986 987 if (!HCD_HW_ACCESSIBLE(hcd)) 988 return 0; 989 990 xhci_dbc_suspend(xhci); 991 992 /* Don't poll the roothubs on bus suspend. */ 993 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__); 994 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); 995 del_timer_sync(&hcd->rh_timer); 996 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); 997 del_timer_sync(&xhci->shared_hcd->rh_timer); 998 999 if (xhci->quirks & XHCI_SUSPEND_DELAY) 1000 usleep_range(1000, 1500); 1001 1002 spin_lock_irq(&xhci->lock); 1003 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 1004 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags); 1005 /* step 1: stop endpoint */ 1006 /* skipped assuming that port suspend has done */ 1007 1008 /* step 2: clear Run/Stop bit */ 1009 command = readl(&xhci->op_regs->command); 1010 command &= ~CMD_RUN; 1011 writel(command, &xhci->op_regs->command); 1012 1013 /* Some chips from Fresco Logic need an extraordinary delay */ 1014 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1; 1015 1016 if (xhci_handshake(&xhci->op_regs->status, 1017 STS_HALT, STS_HALT, delay)) { 1018 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n"); 1019 spin_unlock_irq(&xhci->lock); 1020 return -ETIMEDOUT; 1021 } 1022 xhci_clear_command_ring(xhci); 1023 1024 /* step 3: save registers */ 1025 xhci_save_registers(xhci); 1026 1027 /* step 4: set CSS flag */ 1028 command = readl(&xhci->op_regs->command); 1029 command |= CMD_CSS; 1030 writel(command, &xhci->op_regs->command); 1031 xhci->broken_suspend = 0; 1032 if (xhci_handshake(&xhci->op_regs->status, 1033 STS_SAVE, 0, 20 * 1000)) { 1034 /* 1035 * AMD SNPS xHC 3.0 occasionally does not clear the 1036 * SSS bit of USBSTS and when driver tries to poll 1037 * to see if the xHC clears BIT(8) which never happens 1038 * and driver assumes that controller is not responding 1039 * and times out. To workaround this, its good to check 1040 * if SRE and HCE bits are not set (as per xhci 1041 * Section 5.4.2) and bypass the timeout. 1042 */ 1043 res = readl(&xhci->op_regs->status); 1044 if ((xhci->quirks & XHCI_SNPS_BROKEN_SUSPEND) && 1045 (((res & STS_SRE) == 0) && 1046 ((res & STS_HCE) == 0))) { 1047 xhci->broken_suspend = 1; 1048 } else { 1049 xhci_warn(xhci, "WARN: xHC save state timeout\n"); 1050 spin_unlock_irq(&xhci->lock); 1051 return -ETIMEDOUT; 1052 } 1053 } 1054 spin_unlock_irq(&xhci->lock); 1055 1056 /* 1057 * Deleting Compliance Mode Recovery Timer because the xHCI Host 1058 * is about to be suspended. 1059 */ 1060 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && 1061 (!(xhci_all_ports_seen_u0(xhci)))) { 1062 del_timer_sync(&xhci->comp_mode_recovery_timer); 1063 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 1064 "%s: compliance mode recovery timer deleted", 1065 __func__); 1066 } 1067 1068 /* step 5: remove core well power */ 1069 /* synchronize irq when using MSI-X */ 1070 xhci_msix_sync_irqs(xhci); 1071 1072 return rc; 1073 } 1074 EXPORT_SYMBOL_GPL(xhci_suspend); 1075 1076 /* 1077 * start xHC (not bus-specific) 1078 * 1079 * This is called when the machine transition from S3/S4 mode. 1080 * 1081 */ 1082 int xhci_resume(struct xhci_hcd *xhci, bool hibernated) 1083 { 1084 u32 command, temp = 0; 1085 struct usb_hcd *hcd = xhci_to_hcd(xhci); 1086 struct usb_hcd *secondary_hcd; 1087 int retval = 0; 1088 bool comp_timer_running = false; 1089 bool pending_portevent = false; 1090 1091 if (!hcd->state) 1092 return 0; 1093 1094 /* Wait a bit if either of the roothubs need to settle from the 1095 * transition into bus suspend. 1096 */ 1097 1098 if (time_before(jiffies, xhci->usb2_rhub.bus_state.next_statechange) || 1099 time_before(jiffies, xhci->usb3_rhub.bus_state.next_statechange)) 1100 msleep(100); 1101 1102 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 1103 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags); 1104 1105 spin_lock_irq(&xhci->lock); 1106 if ((xhci->quirks & XHCI_RESET_ON_RESUME) || xhci->broken_suspend) 1107 hibernated = true; 1108 1109 if (!hibernated) { 1110 /* 1111 * Some controllers might lose power during suspend, so wait 1112 * for controller not ready bit to clear, just as in xHC init. 1113 */ 1114 retval = xhci_handshake(&xhci->op_regs->status, 1115 STS_CNR, 0, 10 * 1000 * 1000); 1116 if (retval) { 1117 xhci_warn(xhci, "Controller not ready at resume %d\n", 1118 retval); 1119 spin_unlock_irq(&xhci->lock); 1120 return retval; 1121 } 1122 /* step 1: restore register */ 1123 xhci_restore_registers(xhci); 1124 /* step 2: initialize command ring buffer */ 1125 xhci_set_cmd_ring_deq(xhci); 1126 /* step 3: restore state and start state*/ 1127 /* step 3: set CRS flag */ 1128 command = readl(&xhci->op_regs->command); 1129 command |= CMD_CRS; 1130 writel(command, &xhci->op_regs->command); 1131 /* 1132 * Some controllers take up to 55+ ms to complete the controller 1133 * restore so setting the timeout to 100ms. Xhci specification 1134 * doesn't mention any timeout value. 1135 */ 1136 if (xhci_handshake(&xhci->op_regs->status, 1137 STS_RESTORE, 0, 100 * 1000)) { 1138 xhci_warn(xhci, "WARN: xHC restore state timeout\n"); 1139 spin_unlock_irq(&xhci->lock); 1140 return -ETIMEDOUT; 1141 } 1142 temp = readl(&xhci->op_regs->status); 1143 } 1144 1145 /* If restore operation fails, re-initialize the HC during resume */ 1146 if ((temp & STS_SRE) || hibernated) { 1147 1148 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && 1149 !(xhci_all_ports_seen_u0(xhci))) { 1150 del_timer_sync(&xhci->comp_mode_recovery_timer); 1151 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 1152 "Compliance Mode Recovery Timer deleted!"); 1153 } 1154 1155 /* Let the USB core know _both_ roothubs lost power. */ 1156 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub); 1157 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub); 1158 1159 xhci_dbg(xhci, "Stop HCD\n"); 1160 xhci_halt(xhci); 1161 xhci_zero_64b_regs(xhci); 1162 retval = xhci_reset(xhci); 1163 spin_unlock_irq(&xhci->lock); 1164 if (retval) 1165 return retval; 1166 xhci_cleanup_msix(xhci); 1167 1168 xhci_dbg(xhci, "// Disabling event ring interrupts\n"); 1169 temp = readl(&xhci->op_regs->status); 1170 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status); 1171 temp = readl(&xhci->ir_set->irq_pending); 1172 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending); 1173 1174 xhci_dbg(xhci, "cleaning up memory\n"); 1175 xhci_mem_cleanup(xhci); 1176 xhci_debugfs_exit(xhci); 1177 xhci_dbg(xhci, "xhci_stop completed - status = %x\n", 1178 readl(&xhci->op_regs->status)); 1179 1180 /* USB core calls the PCI reinit and start functions twice: 1181 * first with the primary HCD, and then with the secondary HCD. 1182 * If we don't do the same, the host will never be started. 1183 */ 1184 if (!usb_hcd_is_primary_hcd(hcd)) 1185 secondary_hcd = hcd; 1186 else 1187 secondary_hcd = xhci->shared_hcd; 1188 1189 xhci_dbg(xhci, "Initialize the xhci_hcd\n"); 1190 retval = xhci_init(hcd->primary_hcd); 1191 if (retval) 1192 return retval; 1193 comp_timer_running = true; 1194 1195 xhci_dbg(xhci, "Start the primary HCD\n"); 1196 retval = xhci_run(hcd->primary_hcd); 1197 if (!retval) { 1198 xhci_dbg(xhci, "Start the secondary HCD\n"); 1199 retval = xhci_run(secondary_hcd); 1200 } 1201 hcd->state = HC_STATE_SUSPENDED; 1202 xhci->shared_hcd->state = HC_STATE_SUSPENDED; 1203 goto done; 1204 } 1205 1206 /* step 4: set Run/Stop bit */ 1207 command = readl(&xhci->op_regs->command); 1208 command |= CMD_RUN; 1209 writel(command, &xhci->op_regs->command); 1210 xhci_handshake(&xhci->op_regs->status, STS_HALT, 1211 0, 250 * 1000); 1212 1213 /* step 5: walk topology and initialize portsc, 1214 * portpmsc and portli 1215 */ 1216 /* this is done in bus_resume */ 1217 1218 /* step 6: restart each of the previously 1219 * Running endpoints by ringing their doorbells 1220 */ 1221 1222 spin_unlock_irq(&xhci->lock); 1223 1224 xhci_dbc_resume(xhci); 1225 1226 done: 1227 if (retval == 0) { 1228 /* 1229 * Resume roothubs only if there are pending events. 1230 * USB 3 devices resend U3 LFPS wake after a 100ms delay if 1231 * the first wake signalling failed, give it that chance. 1232 */ 1233 pending_portevent = xhci_pending_portevent(xhci); 1234 if (!pending_portevent) { 1235 msleep(120); 1236 pending_portevent = xhci_pending_portevent(xhci); 1237 } 1238 1239 if (pending_portevent) { 1240 usb_hcd_resume_root_hub(xhci->shared_hcd); 1241 usb_hcd_resume_root_hub(hcd); 1242 } 1243 } 1244 /* 1245 * If system is subject to the Quirk, Compliance Mode Timer needs to 1246 * be re-initialized Always after a system resume. Ports are subject 1247 * to suffer the Compliance Mode issue again. It doesn't matter if 1248 * ports have entered previously to U0 before system's suspension. 1249 */ 1250 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running) 1251 compliance_mode_recovery_timer_init(xhci); 1252 1253 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL) 1254 usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller)); 1255 1256 /* Re-enable port polling. */ 1257 xhci_dbg(xhci, "%s: starting port polling.\n", __func__); 1258 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); 1259 usb_hcd_poll_rh_status(xhci->shared_hcd); 1260 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); 1261 usb_hcd_poll_rh_status(hcd); 1262 1263 return retval; 1264 } 1265 EXPORT_SYMBOL_GPL(xhci_resume); 1266 #endif /* CONFIG_PM */ 1267 1268 /*-------------------------------------------------------------------------*/ 1269 1270 static int xhci_map_temp_buffer(struct usb_hcd *hcd, struct urb *urb) 1271 { 1272 void *temp; 1273 int ret = 0; 1274 unsigned int buf_len; 1275 enum dma_data_direction dir; 1276 1277 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 1278 buf_len = urb->transfer_buffer_length; 1279 1280 temp = kzalloc_node(buf_len, GFP_ATOMIC, 1281 dev_to_node(hcd->self.sysdev)); 1282 1283 if (usb_urb_dir_out(urb)) 1284 sg_pcopy_to_buffer(urb->sg, urb->num_sgs, 1285 temp, buf_len, 0); 1286 1287 urb->transfer_buffer = temp; 1288 urb->transfer_dma = dma_map_single(hcd->self.sysdev, 1289 urb->transfer_buffer, 1290 urb->transfer_buffer_length, 1291 dir); 1292 1293 if (dma_mapping_error(hcd->self.sysdev, 1294 urb->transfer_dma)) { 1295 ret = -EAGAIN; 1296 kfree(temp); 1297 } else { 1298 urb->transfer_flags |= URB_DMA_MAP_SINGLE; 1299 } 1300 1301 return ret; 1302 } 1303 1304 static bool xhci_urb_temp_buffer_required(struct usb_hcd *hcd, 1305 struct urb *urb) 1306 { 1307 bool ret = false; 1308 unsigned int i; 1309 unsigned int len = 0; 1310 unsigned int trb_size; 1311 unsigned int max_pkt; 1312 struct scatterlist *sg; 1313 struct scatterlist *tail_sg; 1314 1315 tail_sg = urb->sg; 1316 max_pkt = usb_endpoint_maxp(&urb->ep->desc); 1317 1318 if (!urb->num_sgs) 1319 return ret; 1320 1321 if (urb->dev->speed >= USB_SPEED_SUPER) 1322 trb_size = TRB_CACHE_SIZE_SS; 1323 else 1324 trb_size = TRB_CACHE_SIZE_HS; 1325 1326 if (urb->transfer_buffer_length != 0 && 1327 !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { 1328 for_each_sg(urb->sg, sg, urb->num_sgs, i) { 1329 len = len + sg->length; 1330 if (i > trb_size - 2) { 1331 len = len - tail_sg->length; 1332 if (len < max_pkt) { 1333 ret = true; 1334 break; 1335 } 1336 1337 tail_sg = sg_next(tail_sg); 1338 } 1339 } 1340 } 1341 return ret; 1342 } 1343 1344 static void xhci_unmap_temp_buf(struct usb_hcd *hcd, struct urb *urb) 1345 { 1346 unsigned int len; 1347 unsigned int buf_len; 1348 enum dma_data_direction dir; 1349 1350 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 1351 1352 buf_len = urb->transfer_buffer_length; 1353 1354 if (IS_ENABLED(CONFIG_HAS_DMA) && 1355 (urb->transfer_flags & URB_DMA_MAP_SINGLE)) 1356 dma_unmap_single(hcd->self.sysdev, 1357 urb->transfer_dma, 1358 urb->transfer_buffer_length, 1359 dir); 1360 1361 if (usb_urb_dir_in(urb)) 1362 len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, 1363 urb->transfer_buffer, 1364 buf_len, 1365 0); 1366 1367 urb->transfer_flags &= ~URB_DMA_MAP_SINGLE; 1368 kfree(urb->transfer_buffer); 1369 urb->transfer_buffer = NULL; 1370 } 1371 1372 /* 1373 * Bypass the DMA mapping if URB is suitable for Immediate Transfer (IDT), 1374 * we'll copy the actual data into the TRB address register. This is limited to 1375 * transfers up to 8 bytes on output endpoints of any kind with wMaxPacketSize 1376 * >= 8 bytes. If suitable for IDT only one Transfer TRB per TD is allowed. 1377 */ 1378 static int xhci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, 1379 gfp_t mem_flags) 1380 { 1381 struct xhci_hcd *xhci; 1382 1383 xhci = hcd_to_xhci(hcd); 1384 1385 if (xhci_urb_suitable_for_idt(urb)) 1386 return 0; 1387 1388 if (xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) { 1389 if (xhci_urb_temp_buffer_required(hcd, urb)) 1390 return xhci_map_temp_buffer(hcd, urb); 1391 } 1392 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); 1393 } 1394 1395 static void xhci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) 1396 { 1397 struct xhci_hcd *xhci; 1398 bool unmap_temp_buf = false; 1399 1400 xhci = hcd_to_xhci(hcd); 1401 1402 if (urb->num_sgs && (urb->transfer_flags & URB_DMA_MAP_SINGLE)) 1403 unmap_temp_buf = true; 1404 1405 if ((xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) && unmap_temp_buf) 1406 xhci_unmap_temp_buf(hcd, urb); 1407 else 1408 usb_hcd_unmap_urb_for_dma(hcd, urb); 1409 } 1410 1411 /** 1412 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and 1413 * HCDs. Find the index for an endpoint given its descriptor. Use the return 1414 * value to right shift 1 for the bitmask. 1415 * 1416 * Index = (epnum * 2) + direction - 1, 1417 * where direction = 0 for OUT, 1 for IN. 1418 * For control endpoints, the IN index is used (OUT index is unused), so 1419 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2) 1420 */ 1421 unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc) 1422 { 1423 unsigned int index; 1424 if (usb_endpoint_xfer_control(desc)) 1425 index = (unsigned int) (usb_endpoint_num(desc)*2); 1426 else 1427 index = (unsigned int) (usb_endpoint_num(desc)*2) + 1428 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1; 1429 return index; 1430 } 1431 1432 /* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint 1433 * address from the XHCI endpoint index. 1434 */ 1435 unsigned int xhci_get_endpoint_address(unsigned int ep_index) 1436 { 1437 unsigned int number = DIV_ROUND_UP(ep_index, 2); 1438 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN; 1439 return direction | number; 1440 } 1441 1442 /* Find the flag for this endpoint (for use in the control context). Use the 1443 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is 1444 * bit 1, etc. 1445 */ 1446 static unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc) 1447 { 1448 return 1 << (xhci_get_endpoint_index(desc) + 1); 1449 } 1450 1451 /* Compute the last valid endpoint context index. Basically, this is the 1452 * endpoint index plus one. For slot contexts with more than valid endpoint, 1453 * we find the most significant bit set in the added contexts flags. 1454 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000 1455 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one. 1456 */ 1457 unsigned int xhci_last_valid_endpoint(u32 added_ctxs) 1458 { 1459 return fls(added_ctxs) - 1; 1460 } 1461 1462 /* Returns 1 if the arguments are OK; 1463 * returns 0 this is a root hub; returns -EINVAL for NULL pointers. 1464 */ 1465 static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev, 1466 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev, 1467 const char *func) { 1468 struct xhci_hcd *xhci; 1469 struct xhci_virt_device *virt_dev; 1470 1471 if (!hcd || (check_ep && !ep) || !udev) { 1472 pr_debug("xHCI %s called with invalid args\n", func); 1473 return -EINVAL; 1474 } 1475 if (!udev->parent) { 1476 pr_debug("xHCI %s called for root hub\n", func); 1477 return 0; 1478 } 1479 1480 xhci = hcd_to_xhci(hcd); 1481 if (check_virt_dev) { 1482 if (!udev->slot_id || !xhci->devs[udev->slot_id]) { 1483 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n", 1484 func); 1485 return -EINVAL; 1486 } 1487 1488 virt_dev = xhci->devs[udev->slot_id]; 1489 if (virt_dev->udev != udev) { 1490 xhci_dbg(xhci, "xHCI %s called with udev and " 1491 "virt_dev does not match\n", func); 1492 return -EINVAL; 1493 } 1494 } 1495 1496 if (xhci->xhc_state & XHCI_STATE_HALTED) 1497 return -ENODEV; 1498 1499 return 1; 1500 } 1501 1502 static int xhci_configure_endpoint(struct xhci_hcd *xhci, 1503 struct usb_device *udev, struct xhci_command *command, 1504 bool ctx_change, bool must_succeed); 1505 1506 /* 1507 * Full speed devices may have a max packet size greater than 8 bytes, but the 1508 * USB core doesn't know that until it reads the first 8 bytes of the 1509 * descriptor. If the usb_device's max packet size changes after that point, 1510 * we need to issue an evaluate context command and wait on it. 1511 */ 1512 static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id, 1513 unsigned int ep_index, struct urb *urb) 1514 { 1515 struct xhci_container_ctx *out_ctx; 1516 struct xhci_input_control_ctx *ctrl_ctx; 1517 struct xhci_ep_ctx *ep_ctx; 1518 struct xhci_command *command; 1519 int max_packet_size; 1520 int hw_max_packet_size; 1521 int ret = 0; 1522 1523 out_ctx = xhci->devs[slot_id]->out_ctx; 1524 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); 1525 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2)); 1526 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc); 1527 if (hw_max_packet_size != max_packet_size) { 1528 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, 1529 "Max Packet Size for ep 0 changed."); 1530 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, 1531 "Max packet size in usb_device = %d", 1532 max_packet_size); 1533 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, 1534 "Max packet size in xHCI HW = %d", 1535 hw_max_packet_size); 1536 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, 1537 "Issuing evaluate context command."); 1538 1539 /* Set up the input context flags for the command */ 1540 /* FIXME: This won't work if a non-default control endpoint 1541 * changes max packet sizes. 1542 */ 1543 1544 command = xhci_alloc_command(xhci, true, GFP_KERNEL); 1545 if (!command) 1546 return -ENOMEM; 1547 1548 command->in_ctx = xhci->devs[slot_id]->in_ctx; 1549 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); 1550 if (!ctrl_ctx) { 1551 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 1552 __func__); 1553 ret = -ENOMEM; 1554 goto command_cleanup; 1555 } 1556 /* Set up the modified control endpoint 0 */ 1557 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx, 1558 xhci->devs[slot_id]->out_ctx, ep_index); 1559 1560 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); 1561 ep_ctx->ep_info &= cpu_to_le32(~EP_STATE_MASK);/* must clear */ 1562 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK); 1563 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size)); 1564 1565 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG); 1566 ctrl_ctx->drop_flags = 0; 1567 1568 ret = xhci_configure_endpoint(xhci, urb->dev, command, 1569 true, false); 1570 1571 /* Clean up the input context for later use by bandwidth 1572 * functions. 1573 */ 1574 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG); 1575 command_cleanup: 1576 kfree(command->completion); 1577 kfree(command); 1578 } 1579 return ret; 1580 } 1581 1582 /* 1583 * non-error returns are a promise to giveback() the urb later 1584 * we drop ownership so next owner (or urb unlink) can get it 1585 */ 1586 static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags) 1587 { 1588 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 1589 unsigned long flags; 1590 int ret = 0; 1591 unsigned int slot_id, ep_index; 1592 unsigned int *ep_state; 1593 struct urb_priv *urb_priv; 1594 int num_tds; 1595 1596 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, 1597 true, true, __func__) <= 0) 1598 return -EINVAL; 1599 1600 slot_id = urb->dev->slot_id; 1601 ep_index = xhci_get_endpoint_index(&urb->ep->desc); 1602 ep_state = &xhci->devs[slot_id]->eps[ep_index].ep_state; 1603 1604 if (!HCD_HW_ACCESSIBLE(hcd)) 1605 return -ESHUTDOWN; 1606 1607 if (xhci->devs[slot_id]->flags & VDEV_PORT_ERROR) { 1608 xhci_dbg(xhci, "Can't queue urb, port error, link inactive\n"); 1609 return -ENODEV; 1610 } 1611 1612 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) 1613 num_tds = urb->number_of_packets; 1614 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) && 1615 urb->transfer_buffer_length > 0 && 1616 urb->transfer_flags & URB_ZERO_PACKET && 1617 !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc))) 1618 num_tds = 2; 1619 else 1620 num_tds = 1; 1621 1622 urb_priv = kzalloc(struct_size(urb_priv, td, num_tds), mem_flags); 1623 if (!urb_priv) 1624 return -ENOMEM; 1625 1626 urb_priv->num_tds = num_tds; 1627 urb_priv->num_tds_done = 0; 1628 urb->hcpriv = urb_priv; 1629 1630 trace_xhci_urb_enqueue(urb); 1631 1632 if (usb_endpoint_xfer_control(&urb->ep->desc)) { 1633 /* Check to see if the max packet size for the default control 1634 * endpoint changed during FS device enumeration 1635 */ 1636 if (urb->dev->speed == USB_SPEED_FULL) { 1637 ret = xhci_check_maxpacket(xhci, slot_id, 1638 ep_index, urb); 1639 if (ret < 0) { 1640 xhci_urb_free_priv(urb_priv); 1641 urb->hcpriv = NULL; 1642 return ret; 1643 } 1644 } 1645 } 1646 1647 spin_lock_irqsave(&xhci->lock, flags); 1648 1649 if (xhci->xhc_state & XHCI_STATE_DYING) { 1650 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for non-responsive xHCI host.\n", 1651 urb->ep->desc.bEndpointAddress, urb); 1652 ret = -ESHUTDOWN; 1653 goto free_priv; 1654 } 1655 if (*ep_state & (EP_GETTING_STREAMS | EP_GETTING_NO_STREAMS)) { 1656 xhci_warn(xhci, "WARN: Can't enqueue URB, ep in streams transition state %x\n", 1657 *ep_state); 1658 ret = -EINVAL; 1659 goto free_priv; 1660 } 1661 if (*ep_state & EP_SOFT_CLEAR_TOGGLE) { 1662 xhci_warn(xhci, "Can't enqueue URB while manually clearing toggle\n"); 1663 ret = -EINVAL; 1664 goto free_priv; 1665 } 1666 1667 switch (usb_endpoint_type(&urb->ep->desc)) { 1668 1669 case USB_ENDPOINT_XFER_CONTROL: 1670 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb, 1671 slot_id, ep_index); 1672 break; 1673 case USB_ENDPOINT_XFER_BULK: 1674 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, 1675 slot_id, ep_index); 1676 break; 1677 case USB_ENDPOINT_XFER_INT: 1678 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb, 1679 slot_id, ep_index); 1680 break; 1681 case USB_ENDPOINT_XFER_ISOC: 1682 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb, 1683 slot_id, ep_index); 1684 } 1685 1686 if (ret) { 1687 free_priv: 1688 xhci_urb_free_priv(urb_priv); 1689 urb->hcpriv = NULL; 1690 } 1691 spin_unlock_irqrestore(&xhci->lock, flags); 1692 return ret; 1693 } 1694 1695 /* 1696 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop 1697 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC 1698 * should pick up where it left off in the TD, unless a Set Transfer Ring 1699 * Dequeue Pointer is issued. 1700 * 1701 * The TRBs that make up the buffers for the canceled URB will be "removed" from 1702 * the ring. Since the ring is a contiguous structure, they can't be physically 1703 * removed. Instead, there are two options: 1704 * 1705 * 1) If the HC is in the middle of processing the URB to be canceled, we 1706 * simply move the ring's dequeue pointer past those TRBs using the Set 1707 * Transfer Ring Dequeue Pointer command. This will be the common case, 1708 * when drivers timeout on the last submitted URB and attempt to cancel. 1709 * 1710 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a 1711 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The 1712 * HC will need to invalidate the any TRBs it has cached after the stop 1713 * endpoint command, as noted in the xHCI 0.95 errata. 1714 * 1715 * 3) The TD may have completed by the time the Stop Endpoint Command 1716 * completes, so software needs to handle that case too. 1717 * 1718 * This function should protect against the TD enqueueing code ringing the 1719 * doorbell while this code is waiting for a Stop Endpoint command to complete. 1720 * It also needs to account for multiple cancellations on happening at the same 1721 * time for the same endpoint. 1722 * 1723 * Note that this function can be called in any context, or so says 1724 * usb_hcd_unlink_urb() 1725 */ 1726 static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) 1727 { 1728 unsigned long flags; 1729 int ret, i; 1730 u32 temp; 1731 struct xhci_hcd *xhci; 1732 struct urb_priv *urb_priv; 1733 struct xhci_td *td; 1734 unsigned int ep_index; 1735 struct xhci_ring *ep_ring; 1736 struct xhci_virt_ep *ep; 1737 struct xhci_command *command; 1738 struct xhci_virt_device *vdev; 1739 1740 xhci = hcd_to_xhci(hcd); 1741 spin_lock_irqsave(&xhci->lock, flags); 1742 1743 trace_xhci_urb_dequeue(urb); 1744 1745 /* Make sure the URB hasn't completed or been unlinked already */ 1746 ret = usb_hcd_check_unlink_urb(hcd, urb, status); 1747 if (ret) 1748 goto done; 1749 1750 /* give back URB now if we can't queue it for cancel */ 1751 vdev = xhci->devs[urb->dev->slot_id]; 1752 urb_priv = urb->hcpriv; 1753 if (!vdev || !urb_priv) 1754 goto err_giveback; 1755 1756 ep_index = xhci_get_endpoint_index(&urb->ep->desc); 1757 ep = &vdev->eps[ep_index]; 1758 ep_ring = xhci_urb_to_transfer_ring(xhci, urb); 1759 if (!ep || !ep_ring) 1760 goto err_giveback; 1761 1762 /* If xHC is dead take it down and return ALL URBs in xhci_hc_died() */ 1763 temp = readl(&xhci->op_regs->status); 1764 if (temp == ~(u32)0 || xhci->xhc_state & XHCI_STATE_DYING) { 1765 xhci_hc_died(xhci); 1766 goto done; 1767 } 1768 1769 /* 1770 * check ring is not re-allocated since URB was enqueued. If it is, then 1771 * make sure none of the ring related pointers in this URB private data 1772 * are touched, such as td_list, otherwise we overwrite freed data 1773 */ 1774 if (!td_on_ring(&urb_priv->td[0], ep_ring)) { 1775 xhci_err(xhci, "Canceled URB td not found on endpoint ring"); 1776 for (i = urb_priv->num_tds_done; i < urb_priv->num_tds; i++) { 1777 td = &urb_priv->td[i]; 1778 if (!list_empty(&td->cancelled_td_list)) 1779 list_del_init(&td->cancelled_td_list); 1780 } 1781 goto err_giveback; 1782 } 1783 1784 if (xhci->xhc_state & XHCI_STATE_HALTED) { 1785 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 1786 "HC halted, freeing TD manually."); 1787 for (i = urb_priv->num_tds_done; 1788 i < urb_priv->num_tds; 1789 i++) { 1790 td = &urb_priv->td[i]; 1791 if (!list_empty(&td->td_list)) 1792 list_del_init(&td->td_list); 1793 if (!list_empty(&td->cancelled_td_list)) 1794 list_del_init(&td->cancelled_td_list); 1795 } 1796 goto err_giveback; 1797 } 1798 1799 i = urb_priv->num_tds_done; 1800 if (i < urb_priv->num_tds) 1801 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, 1802 "Cancel URB %p, dev %s, ep 0x%x, " 1803 "starting at offset 0x%llx", 1804 urb, urb->dev->devpath, 1805 urb->ep->desc.bEndpointAddress, 1806 (unsigned long long) xhci_trb_virt_to_dma( 1807 urb_priv->td[i].start_seg, 1808 urb_priv->td[i].first_trb)); 1809 1810 for (; i < urb_priv->num_tds; i++) { 1811 td = &urb_priv->td[i]; 1812 /* TD can already be on cancelled list if ep halted on it */ 1813 if (list_empty(&td->cancelled_td_list)) { 1814 td->cancel_status = TD_DIRTY; 1815 list_add_tail(&td->cancelled_td_list, 1816 &ep->cancelled_td_list); 1817 } 1818 } 1819 1820 /* Queue a stop endpoint command, but only if this is 1821 * the first cancellation to be handled. 1822 */ 1823 if (!(ep->ep_state & EP_STOP_CMD_PENDING)) { 1824 command = xhci_alloc_command(xhci, false, GFP_ATOMIC); 1825 if (!command) { 1826 ret = -ENOMEM; 1827 goto done; 1828 } 1829 ep->ep_state |= EP_STOP_CMD_PENDING; 1830 ep->stop_cmd_timer.expires = jiffies + 1831 XHCI_STOP_EP_CMD_TIMEOUT * HZ; 1832 add_timer(&ep->stop_cmd_timer); 1833 xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id, 1834 ep_index, 0); 1835 xhci_ring_cmd_db(xhci); 1836 } 1837 done: 1838 spin_unlock_irqrestore(&xhci->lock, flags); 1839 return ret; 1840 1841 err_giveback: 1842 if (urb_priv) 1843 xhci_urb_free_priv(urb_priv); 1844 usb_hcd_unlink_urb_from_ep(hcd, urb); 1845 spin_unlock_irqrestore(&xhci->lock, flags); 1846 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN); 1847 return ret; 1848 } 1849 1850 /* Drop an endpoint from a new bandwidth configuration for this device. 1851 * Only one call to this function is allowed per endpoint before 1852 * check_bandwidth() or reset_bandwidth() must be called. 1853 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will 1854 * add the endpoint to the schedule with possibly new parameters denoted by a 1855 * different endpoint descriptor in usb_host_endpoint. 1856 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is 1857 * not allowed. 1858 * 1859 * The USB core will not allow URBs to be queued to an endpoint that is being 1860 * disabled, so there's no need for mutual exclusion to protect 1861 * the xhci->devs[slot_id] structure. 1862 */ 1863 static int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev, 1864 struct usb_host_endpoint *ep) 1865 { 1866 struct xhci_hcd *xhci; 1867 struct xhci_container_ctx *in_ctx, *out_ctx; 1868 struct xhci_input_control_ctx *ctrl_ctx; 1869 unsigned int ep_index; 1870 struct xhci_ep_ctx *ep_ctx; 1871 u32 drop_flag; 1872 u32 new_add_flags, new_drop_flags; 1873 int ret; 1874 1875 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__); 1876 if (ret <= 0) 1877 return ret; 1878 xhci = hcd_to_xhci(hcd); 1879 if (xhci->xhc_state & XHCI_STATE_DYING) 1880 return -ENODEV; 1881 1882 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); 1883 drop_flag = xhci_get_endpoint_flag(&ep->desc); 1884 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) { 1885 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n", 1886 __func__, drop_flag); 1887 return 0; 1888 } 1889 1890 in_ctx = xhci->devs[udev->slot_id]->in_ctx; 1891 out_ctx = xhci->devs[udev->slot_id]->out_ctx; 1892 ctrl_ctx = xhci_get_input_control_ctx(in_ctx); 1893 if (!ctrl_ctx) { 1894 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 1895 __func__); 1896 return 0; 1897 } 1898 1899 ep_index = xhci_get_endpoint_index(&ep->desc); 1900 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); 1901 /* If the HC already knows the endpoint is disabled, 1902 * or the HCD has noted it is disabled, ignore this request 1903 */ 1904 if ((GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) || 1905 le32_to_cpu(ctrl_ctx->drop_flags) & 1906 xhci_get_endpoint_flag(&ep->desc)) { 1907 /* Do not warn when called after a usb_device_reset */ 1908 if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL) 1909 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n", 1910 __func__, ep); 1911 return 0; 1912 } 1913 1914 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag); 1915 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags); 1916 1917 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag); 1918 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags); 1919 1920 xhci_debugfs_remove_endpoint(xhci, xhci->devs[udev->slot_id], ep_index); 1921 1922 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep); 1923 1924 if (xhci->quirks & XHCI_MTK_HOST) 1925 xhci_mtk_drop_ep_quirk(hcd, udev, ep); 1926 1927 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n", 1928 (unsigned int) ep->desc.bEndpointAddress, 1929 udev->slot_id, 1930 (unsigned int) new_drop_flags, 1931 (unsigned int) new_add_flags); 1932 return 0; 1933 } 1934 1935 /* Add an endpoint to a new possible bandwidth configuration for this device. 1936 * Only one call to this function is allowed per endpoint before 1937 * check_bandwidth() or reset_bandwidth() must be called. 1938 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will 1939 * add the endpoint to the schedule with possibly new parameters denoted by a 1940 * different endpoint descriptor in usb_host_endpoint. 1941 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is 1942 * not allowed. 1943 * 1944 * The USB core will not allow URBs to be queued to an endpoint until the 1945 * configuration or alt setting is installed in the device, so there's no need 1946 * for mutual exclusion to protect the xhci->devs[slot_id] structure. 1947 */ 1948 static int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev, 1949 struct usb_host_endpoint *ep) 1950 { 1951 struct xhci_hcd *xhci; 1952 struct xhci_container_ctx *in_ctx; 1953 unsigned int ep_index; 1954 struct xhci_input_control_ctx *ctrl_ctx; 1955 struct xhci_ep_ctx *ep_ctx; 1956 u32 added_ctxs; 1957 u32 new_add_flags, new_drop_flags; 1958 struct xhci_virt_device *virt_dev; 1959 int ret = 0; 1960 1961 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__); 1962 if (ret <= 0) { 1963 /* So we won't queue a reset ep command for a root hub */ 1964 ep->hcpriv = NULL; 1965 return ret; 1966 } 1967 xhci = hcd_to_xhci(hcd); 1968 if (xhci->xhc_state & XHCI_STATE_DYING) 1969 return -ENODEV; 1970 1971 added_ctxs = xhci_get_endpoint_flag(&ep->desc); 1972 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) { 1973 /* FIXME when we have to issue an evaluate endpoint command to 1974 * deal with ep0 max packet size changing once we get the 1975 * descriptors 1976 */ 1977 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n", 1978 __func__, added_ctxs); 1979 return 0; 1980 } 1981 1982 virt_dev = xhci->devs[udev->slot_id]; 1983 in_ctx = virt_dev->in_ctx; 1984 ctrl_ctx = xhci_get_input_control_ctx(in_ctx); 1985 if (!ctrl_ctx) { 1986 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 1987 __func__); 1988 return 0; 1989 } 1990 1991 ep_index = xhci_get_endpoint_index(&ep->desc); 1992 /* If this endpoint is already in use, and the upper layers are trying 1993 * to add it again without dropping it, reject the addition. 1994 */ 1995 if (virt_dev->eps[ep_index].ring && 1996 !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) { 1997 xhci_warn(xhci, "Trying to add endpoint 0x%x " 1998 "without dropping it.\n", 1999 (unsigned int) ep->desc.bEndpointAddress); 2000 return -EINVAL; 2001 } 2002 2003 /* If the HCD has already noted the endpoint is enabled, 2004 * ignore this request. 2005 */ 2006 if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) { 2007 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n", 2008 __func__, ep); 2009 return 0; 2010 } 2011 2012 /* 2013 * Configuration and alternate setting changes must be done in 2014 * process context, not interrupt context (or so documenation 2015 * for usb_set_interface() and usb_set_configuration() claim). 2016 */ 2017 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) { 2018 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n", 2019 __func__, ep->desc.bEndpointAddress); 2020 return -ENOMEM; 2021 } 2022 2023 if (xhci->quirks & XHCI_MTK_HOST) { 2024 ret = xhci_mtk_add_ep_quirk(hcd, udev, ep); 2025 if (ret < 0) { 2026 xhci_ring_free(xhci, virt_dev->eps[ep_index].new_ring); 2027 virt_dev->eps[ep_index].new_ring = NULL; 2028 return ret; 2029 } 2030 } 2031 2032 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs); 2033 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags); 2034 2035 /* If xhci_endpoint_disable() was called for this endpoint, but the 2036 * xHC hasn't been notified yet through the check_bandwidth() call, 2037 * this re-adds a new state for the endpoint from the new endpoint 2038 * descriptors. We must drop and re-add this endpoint, so we leave the 2039 * drop flags alone. 2040 */ 2041 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags); 2042 2043 /* Store the usb_device pointer for later use */ 2044 ep->hcpriv = udev; 2045 2046 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); 2047 trace_xhci_add_endpoint(ep_ctx); 2048 2049 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n", 2050 (unsigned int) ep->desc.bEndpointAddress, 2051 udev->slot_id, 2052 (unsigned int) new_drop_flags, 2053 (unsigned int) new_add_flags); 2054 return 0; 2055 } 2056 2057 static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev) 2058 { 2059 struct xhci_input_control_ctx *ctrl_ctx; 2060 struct xhci_ep_ctx *ep_ctx; 2061 struct xhci_slot_ctx *slot_ctx; 2062 int i; 2063 2064 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); 2065 if (!ctrl_ctx) { 2066 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 2067 __func__); 2068 return; 2069 } 2070 2071 /* When a device's add flag and drop flag are zero, any subsequent 2072 * configure endpoint command will leave that endpoint's state 2073 * untouched. Make sure we don't leave any old state in the input 2074 * endpoint contexts. 2075 */ 2076 ctrl_ctx->drop_flags = 0; 2077 ctrl_ctx->add_flags = 0; 2078 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); 2079 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK); 2080 /* Endpoint 0 is always valid */ 2081 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1)); 2082 for (i = 1; i < 31; i++) { 2083 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i); 2084 ep_ctx->ep_info = 0; 2085 ep_ctx->ep_info2 = 0; 2086 ep_ctx->deq = 0; 2087 ep_ctx->tx_info = 0; 2088 } 2089 } 2090 2091 static int xhci_configure_endpoint_result(struct xhci_hcd *xhci, 2092 struct usb_device *udev, u32 *cmd_status) 2093 { 2094 int ret; 2095 2096 switch (*cmd_status) { 2097 case COMP_COMMAND_ABORTED: 2098 case COMP_COMMAND_RING_STOPPED: 2099 xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n"); 2100 ret = -ETIME; 2101 break; 2102 case COMP_RESOURCE_ERROR: 2103 dev_warn(&udev->dev, 2104 "Not enough host controller resources for new device state.\n"); 2105 ret = -ENOMEM; 2106 /* FIXME: can we allocate more resources for the HC? */ 2107 break; 2108 case COMP_BANDWIDTH_ERROR: 2109 case COMP_SECONDARY_BANDWIDTH_ERROR: 2110 dev_warn(&udev->dev, 2111 "Not enough bandwidth for new device state.\n"); 2112 ret = -ENOSPC; 2113 /* FIXME: can we go back to the old state? */ 2114 break; 2115 case COMP_TRB_ERROR: 2116 /* the HCD set up something wrong */ 2117 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, " 2118 "add flag = 1, " 2119 "and endpoint is not disabled.\n"); 2120 ret = -EINVAL; 2121 break; 2122 case COMP_INCOMPATIBLE_DEVICE_ERROR: 2123 dev_warn(&udev->dev, 2124 "ERROR: Incompatible device for endpoint configure command.\n"); 2125 ret = -ENODEV; 2126 break; 2127 case COMP_SUCCESS: 2128 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, 2129 "Successful Endpoint Configure command"); 2130 ret = 0; 2131 break; 2132 default: 2133 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n", 2134 *cmd_status); 2135 ret = -EINVAL; 2136 break; 2137 } 2138 return ret; 2139 } 2140 2141 static int xhci_evaluate_context_result(struct xhci_hcd *xhci, 2142 struct usb_device *udev, u32 *cmd_status) 2143 { 2144 int ret; 2145 2146 switch (*cmd_status) { 2147 case COMP_COMMAND_ABORTED: 2148 case COMP_COMMAND_RING_STOPPED: 2149 xhci_warn(xhci, "Timeout while waiting for evaluate context command\n"); 2150 ret = -ETIME; 2151 break; 2152 case COMP_PARAMETER_ERROR: 2153 dev_warn(&udev->dev, 2154 "WARN: xHCI driver setup invalid evaluate context command.\n"); 2155 ret = -EINVAL; 2156 break; 2157 case COMP_SLOT_NOT_ENABLED_ERROR: 2158 dev_warn(&udev->dev, 2159 "WARN: slot not enabled for evaluate context command.\n"); 2160 ret = -EINVAL; 2161 break; 2162 case COMP_CONTEXT_STATE_ERROR: 2163 dev_warn(&udev->dev, 2164 "WARN: invalid context state for evaluate context command.\n"); 2165 ret = -EINVAL; 2166 break; 2167 case COMP_INCOMPATIBLE_DEVICE_ERROR: 2168 dev_warn(&udev->dev, 2169 "ERROR: Incompatible device for evaluate context command.\n"); 2170 ret = -ENODEV; 2171 break; 2172 case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR: 2173 /* Max Exit Latency too large error */ 2174 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n"); 2175 ret = -EINVAL; 2176 break; 2177 case COMP_SUCCESS: 2178 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, 2179 "Successful evaluate context command"); 2180 ret = 0; 2181 break; 2182 default: 2183 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n", 2184 *cmd_status); 2185 ret = -EINVAL; 2186 break; 2187 } 2188 return ret; 2189 } 2190 2191 static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci, 2192 struct xhci_input_control_ctx *ctrl_ctx) 2193 { 2194 u32 valid_add_flags; 2195 u32 valid_drop_flags; 2196 2197 /* Ignore the slot flag (bit 0), and the default control endpoint flag 2198 * (bit 1). The default control endpoint is added during the Address 2199 * Device command and is never removed until the slot is disabled. 2200 */ 2201 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2; 2202 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2; 2203 2204 /* Use hweight32 to count the number of ones in the add flags, or 2205 * number of endpoints added. Don't count endpoints that are changed 2206 * (both added and dropped). 2207 */ 2208 return hweight32(valid_add_flags) - 2209 hweight32(valid_add_flags & valid_drop_flags); 2210 } 2211 2212 static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci, 2213 struct xhci_input_control_ctx *ctrl_ctx) 2214 { 2215 u32 valid_add_flags; 2216 u32 valid_drop_flags; 2217 2218 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2; 2219 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2; 2220 2221 return hweight32(valid_drop_flags) - 2222 hweight32(valid_add_flags & valid_drop_flags); 2223 } 2224 2225 /* 2226 * We need to reserve the new number of endpoints before the configure endpoint 2227 * command completes. We can't subtract the dropped endpoints from the number 2228 * of active endpoints until the command completes because we can oversubscribe 2229 * the host in this case: 2230 * 2231 * - the first configure endpoint command drops more endpoints than it adds 2232 * - a second configure endpoint command that adds more endpoints is queued 2233 * - the first configure endpoint command fails, so the config is unchanged 2234 * - the second command may succeed, even though there isn't enough resources 2235 * 2236 * Must be called with xhci->lock held. 2237 */ 2238 static int xhci_reserve_host_resources(struct xhci_hcd *xhci, 2239 struct xhci_input_control_ctx *ctrl_ctx) 2240 { 2241 u32 added_eps; 2242 2243 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx); 2244 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) { 2245 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 2246 "Not enough ep ctxs: " 2247 "%u active, need to add %u, limit is %u.", 2248 xhci->num_active_eps, added_eps, 2249 xhci->limit_active_eps); 2250 return -ENOMEM; 2251 } 2252 xhci->num_active_eps += added_eps; 2253 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 2254 "Adding %u ep ctxs, %u now active.", added_eps, 2255 xhci->num_active_eps); 2256 return 0; 2257 } 2258 2259 /* 2260 * The configure endpoint was failed by the xHC for some other reason, so we 2261 * need to revert the resources that failed configuration would have used. 2262 * 2263 * Must be called with xhci->lock held. 2264 */ 2265 static void xhci_free_host_resources(struct xhci_hcd *xhci, 2266 struct xhci_input_control_ctx *ctrl_ctx) 2267 { 2268 u32 num_failed_eps; 2269 2270 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx); 2271 xhci->num_active_eps -= num_failed_eps; 2272 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 2273 "Removing %u failed ep ctxs, %u now active.", 2274 num_failed_eps, 2275 xhci->num_active_eps); 2276 } 2277 2278 /* 2279 * Now that the command has completed, clean up the active endpoint count by 2280 * subtracting out the endpoints that were dropped (but not changed). 2281 * 2282 * Must be called with xhci->lock held. 2283 */ 2284 static void xhci_finish_resource_reservation(struct xhci_hcd *xhci, 2285 struct xhci_input_control_ctx *ctrl_ctx) 2286 { 2287 u32 num_dropped_eps; 2288 2289 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx); 2290 xhci->num_active_eps -= num_dropped_eps; 2291 if (num_dropped_eps) 2292 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 2293 "Removing %u dropped ep ctxs, %u now active.", 2294 num_dropped_eps, 2295 xhci->num_active_eps); 2296 } 2297 2298 static unsigned int xhci_get_block_size(struct usb_device *udev) 2299 { 2300 switch (udev->speed) { 2301 case USB_SPEED_LOW: 2302 case USB_SPEED_FULL: 2303 return FS_BLOCK; 2304 case USB_SPEED_HIGH: 2305 return HS_BLOCK; 2306 case USB_SPEED_SUPER: 2307 case USB_SPEED_SUPER_PLUS: 2308 return SS_BLOCK; 2309 case USB_SPEED_UNKNOWN: 2310 case USB_SPEED_WIRELESS: 2311 default: 2312 /* Should never happen */ 2313 return 1; 2314 } 2315 } 2316 2317 static unsigned int 2318 xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw) 2319 { 2320 if (interval_bw->overhead[LS_OVERHEAD_TYPE]) 2321 return LS_OVERHEAD; 2322 if (interval_bw->overhead[FS_OVERHEAD_TYPE]) 2323 return FS_OVERHEAD; 2324 return HS_OVERHEAD; 2325 } 2326 2327 /* If we are changing a LS/FS device under a HS hub, 2328 * make sure (if we are activating a new TT) that the HS bus has enough 2329 * bandwidth for this new TT. 2330 */ 2331 static int xhci_check_tt_bw_table(struct xhci_hcd *xhci, 2332 struct xhci_virt_device *virt_dev, 2333 int old_active_eps) 2334 { 2335 struct xhci_interval_bw_table *bw_table; 2336 struct xhci_tt_bw_info *tt_info; 2337 2338 /* Find the bandwidth table for the root port this TT is attached to. */ 2339 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table; 2340 tt_info = virt_dev->tt_info; 2341 /* If this TT already had active endpoints, the bandwidth for this TT 2342 * has already been added. Removing all periodic endpoints (and thus 2343 * making the TT enactive) will only decrease the bandwidth used. 2344 */ 2345 if (old_active_eps) 2346 return 0; 2347 if (old_active_eps == 0 && tt_info->active_eps != 0) { 2348 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT) 2349 return -ENOMEM; 2350 return 0; 2351 } 2352 /* Not sure why we would have no new active endpoints... 2353 * 2354 * Maybe because of an Evaluate Context change for a hub update or a 2355 * control endpoint 0 max packet size change? 2356 * FIXME: skip the bandwidth calculation in that case. 2357 */ 2358 return 0; 2359 } 2360 2361 static int xhci_check_ss_bw(struct xhci_hcd *xhci, 2362 struct xhci_virt_device *virt_dev) 2363 { 2364 unsigned int bw_reserved; 2365 2366 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100); 2367 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved)) 2368 return -ENOMEM; 2369 2370 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100); 2371 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved)) 2372 return -ENOMEM; 2373 2374 return 0; 2375 } 2376 2377 /* 2378 * This algorithm is a very conservative estimate of the worst-case scheduling 2379 * scenario for any one interval. The hardware dynamically schedules the 2380 * packets, so we can't tell which microframe could be the limiting factor in 2381 * the bandwidth scheduling. This only takes into account periodic endpoints. 2382 * 2383 * Obviously, we can't solve an NP complete problem to find the minimum worst 2384 * case scenario. Instead, we come up with an estimate that is no less than 2385 * the worst case bandwidth used for any one microframe, but may be an 2386 * over-estimate. 2387 * 2388 * We walk the requirements for each endpoint by interval, starting with the 2389 * smallest interval, and place packets in the schedule where there is only one 2390 * possible way to schedule packets for that interval. In order to simplify 2391 * this algorithm, we record the largest max packet size for each interval, and 2392 * assume all packets will be that size. 2393 * 2394 * For interval 0, we obviously must schedule all packets for each interval. 2395 * The bandwidth for interval 0 is just the amount of data to be transmitted 2396 * (the sum of all max ESIT payload sizes, plus any overhead per packet times 2397 * the number of packets). 2398 * 2399 * For interval 1, we have two possible microframes to schedule those packets 2400 * in. For this algorithm, if we can schedule the same number of packets for 2401 * each possible scheduling opportunity (each microframe), we will do so. The 2402 * remaining number of packets will be saved to be transmitted in the gaps in 2403 * the next interval's scheduling sequence. 2404 * 2405 * As we move those remaining packets to be scheduled with interval 2 packets, 2406 * we have to double the number of remaining packets to transmit. This is 2407 * because the intervals are actually powers of 2, and we would be transmitting 2408 * the previous interval's packets twice in this interval. We also have to be 2409 * sure that when we look at the largest max packet size for this interval, we 2410 * also look at the largest max packet size for the remaining packets and take 2411 * the greater of the two. 2412 * 2413 * The algorithm continues to evenly distribute packets in each scheduling 2414 * opportunity, and push the remaining packets out, until we get to the last 2415 * interval. Then those packets and their associated overhead are just added 2416 * to the bandwidth used. 2417 */ 2418 static int xhci_check_bw_table(struct xhci_hcd *xhci, 2419 struct xhci_virt_device *virt_dev, 2420 int old_active_eps) 2421 { 2422 unsigned int bw_reserved; 2423 unsigned int max_bandwidth; 2424 unsigned int bw_used; 2425 unsigned int block_size; 2426 struct xhci_interval_bw_table *bw_table; 2427 unsigned int packet_size = 0; 2428 unsigned int overhead = 0; 2429 unsigned int packets_transmitted = 0; 2430 unsigned int packets_remaining = 0; 2431 unsigned int i; 2432 2433 if (virt_dev->udev->speed >= USB_SPEED_SUPER) 2434 return xhci_check_ss_bw(xhci, virt_dev); 2435 2436 if (virt_dev->udev->speed == USB_SPEED_HIGH) { 2437 max_bandwidth = HS_BW_LIMIT; 2438 /* Convert percent of bus BW reserved to blocks reserved */ 2439 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100); 2440 } else { 2441 max_bandwidth = FS_BW_LIMIT; 2442 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100); 2443 } 2444 2445 bw_table = virt_dev->bw_table; 2446 /* We need to translate the max packet size and max ESIT payloads into 2447 * the units the hardware uses. 2448 */ 2449 block_size = xhci_get_block_size(virt_dev->udev); 2450 2451 /* If we are manipulating a LS/FS device under a HS hub, double check 2452 * that the HS bus has enough bandwidth if we are activing a new TT. 2453 */ 2454 if (virt_dev->tt_info) { 2455 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 2456 "Recalculating BW for rootport %u", 2457 virt_dev->real_port); 2458 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) { 2459 xhci_warn(xhci, "Not enough bandwidth on HS bus for " 2460 "newly activated TT.\n"); 2461 return -ENOMEM; 2462 } 2463 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 2464 "Recalculating BW for TT slot %u port %u", 2465 virt_dev->tt_info->slot_id, 2466 virt_dev->tt_info->ttport); 2467 } else { 2468 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 2469 "Recalculating BW for rootport %u", 2470 virt_dev->real_port); 2471 } 2472 2473 /* Add in how much bandwidth will be used for interval zero, or the 2474 * rounded max ESIT payload + number of packets * largest overhead. 2475 */ 2476 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) + 2477 bw_table->interval_bw[0].num_packets * 2478 xhci_get_largest_overhead(&bw_table->interval_bw[0]); 2479 2480 for (i = 1; i < XHCI_MAX_INTERVAL; i++) { 2481 unsigned int bw_added; 2482 unsigned int largest_mps; 2483 unsigned int interval_overhead; 2484 2485 /* 2486 * How many packets could we transmit in this interval? 2487 * If packets didn't fit in the previous interval, we will need 2488 * to transmit that many packets twice within this interval. 2489 */ 2490 packets_remaining = 2 * packets_remaining + 2491 bw_table->interval_bw[i].num_packets; 2492 2493 /* Find the largest max packet size of this or the previous 2494 * interval. 2495 */ 2496 if (list_empty(&bw_table->interval_bw[i].endpoints)) 2497 largest_mps = 0; 2498 else { 2499 struct xhci_virt_ep *virt_ep; 2500 struct list_head *ep_entry; 2501 2502 ep_entry = bw_table->interval_bw[i].endpoints.next; 2503 virt_ep = list_entry(ep_entry, 2504 struct xhci_virt_ep, bw_endpoint_list); 2505 /* Convert to blocks, rounding up */ 2506 largest_mps = DIV_ROUND_UP( 2507 virt_ep->bw_info.max_packet_size, 2508 block_size); 2509 } 2510 if (largest_mps > packet_size) 2511 packet_size = largest_mps; 2512 2513 /* Use the larger overhead of this or the previous interval. */ 2514 interval_overhead = xhci_get_largest_overhead( 2515 &bw_table->interval_bw[i]); 2516 if (interval_overhead > overhead) 2517 overhead = interval_overhead; 2518 2519 /* How many packets can we evenly distribute across 2520 * (1 << (i + 1)) possible scheduling opportunities? 2521 */ 2522 packets_transmitted = packets_remaining >> (i + 1); 2523 2524 /* Add in the bandwidth used for those scheduled packets */ 2525 bw_added = packets_transmitted * (overhead + packet_size); 2526 2527 /* How many packets do we have remaining to transmit? */ 2528 packets_remaining = packets_remaining % (1 << (i + 1)); 2529 2530 /* What largest max packet size should those packets have? */ 2531 /* If we've transmitted all packets, don't carry over the 2532 * largest packet size. 2533 */ 2534 if (packets_remaining == 0) { 2535 packet_size = 0; 2536 overhead = 0; 2537 } else if (packets_transmitted > 0) { 2538 /* Otherwise if we do have remaining packets, and we've 2539 * scheduled some packets in this interval, take the 2540 * largest max packet size from endpoints with this 2541 * interval. 2542 */ 2543 packet_size = largest_mps; 2544 overhead = interval_overhead; 2545 } 2546 /* Otherwise carry over packet_size and overhead from the last 2547 * time we had a remainder. 2548 */ 2549 bw_used += bw_added; 2550 if (bw_used > max_bandwidth) { 2551 xhci_warn(xhci, "Not enough bandwidth. " 2552 "Proposed: %u, Max: %u\n", 2553 bw_used, max_bandwidth); 2554 return -ENOMEM; 2555 } 2556 } 2557 /* 2558 * Ok, we know we have some packets left over after even-handedly 2559 * scheduling interval 15. We don't know which microframes they will 2560 * fit into, so we over-schedule and say they will be scheduled every 2561 * microframe. 2562 */ 2563 if (packets_remaining > 0) 2564 bw_used += overhead + packet_size; 2565 2566 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) { 2567 unsigned int port_index = virt_dev->real_port - 1; 2568 2569 /* OK, we're manipulating a HS device attached to a 2570 * root port bandwidth domain. Include the number of active TTs 2571 * in the bandwidth used. 2572 */ 2573 bw_used += TT_HS_OVERHEAD * 2574 xhci->rh_bw[port_index].num_active_tts; 2575 } 2576 2577 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 2578 "Final bandwidth: %u, Limit: %u, Reserved: %u, " 2579 "Available: %u " "percent", 2580 bw_used, max_bandwidth, bw_reserved, 2581 (max_bandwidth - bw_used - bw_reserved) * 100 / 2582 max_bandwidth); 2583 2584 bw_used += bw_reserved; 2585 if (bw_used > max_bandwidth) { 2586 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n", 2587 bw_used, max_bandwidth); 2588 return -ENOMEM; 2589 } 2590 2591 bw_table->bw_used = bw_used; 2592 return 0; 2593 } 2594 2595 static bool xhci_is_async_ep(unsigned int ep_type) 2596 { 2597 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP && 2598 ep_type != ISOC_IN_EP && 2599 ep_type != INT_IN_EP); 2600 } 2601 2602 static bool xhci_is_sync_in_ep(unsigned int ep_type) 2603 { 2604 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP); 2605 } 2606 2607 static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw) 2608 { 2609 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK); 2610 2611 if (ep_bw->ep_interval == 0) 2612 return SS_OVERHEAD_BURST + 2613 (ep_bw->mult * ep_bw->num_packets * 2614 (SS_OVERHEAD + mps)); 2615 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets * 2616 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST), 2617 1 << ep_bw->ep_interval); 2618 2619 } 2620 2621 static void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci, 2622 struct xhci_bw_info *ep_bw, 2623 struct xhci_interval_bw_table *bw_table, 2624 struct usb_device *udev, 2625 struct xhci_virt_ep *virt_ep, 2626 struct xhci_tt_bw_info *tt_info) 2627 { 2628 struct xhci_interval_bw *interval_bw; 2629 int normalized_interval; 2630 2631 if (xhci_is_async_ep(ep_bw->type)) 2632 return; 2633 2634 if (udev->speed >= USB_SPEED_SUPER) { 2635 if (xhci_is_sync_in_ep(ep_bw->type)) 2636 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -= 2637 xhci_get_ss_bw_consumed(ep_bw); 2638 else 2639 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -= 2640 xhci_get_ss_bw_consumed(ep_bw); 2641 return; 2642 } 2643 2644 /* SuperSpeed endpoints never get added to intervals in the table, so 2645 * this check is only valid for HS/FS/LS devices. 2646 */ 2647 if (list_empty(&virt_ep->bw_endpoint_list)) 2648 return; 2649 /* For LS/FS devices, we need to translate the interval expressed in 2650 * microframes to frames. 2651 */ 2652 if (udev->speed == USB_SPEED_HIGH) 2653 normalized_interval = ep_bw->ep_interval; 2654 else 2655 normalized_interval = ep_bw->ep_interval - 3; 2656 2657 if (normalized_interval == 0) 2658 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload; 2659 interval_bw = &bw_table->interval_bw[normalized_interval]; 2660 interval_bw->num_packets -= ep_bw->num_packets; 2661 switch (udev->speed) { 2662 case USB_SPEED_LOW: 2663 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1; 2664 break; 2665 case USB_SPEED_FULL: 2666 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1; 2667 break; 2668 case USB_SPEED_HIGH: 2669 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1; 2670 break; 2671 case USB_SPEED_SUPER: 2672 case USB_SPEED_SUPER_PLUS: 2673 case USB_SPEED_UNKNOWN: 2674 case USB_SPEED_WIRELESS: 2675 /* Should never happen because only LS/FS/HS endpoints will get 2676 * added to the endpoint list. 2677 */ 2678 return; 2679 } 2680 if (tt_info) 2681 tt_info->active_eps -= 1; 2682 list_del_init(&virt_ep->bw_endpoint_list); 2683 } 2684 2685 static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci, 2686 struct xhci_bw_info *ep_bw, 2687 struct xhci_interval_bw_table *bw_table, 2688 struct usb_device *udev, 2689 struct xhci_virt_ep *virt_ep, 2690 struct xhci_tt_bw_info *tt_info) 2691 { 2692 struct xhci_interval_bw *interval_bw; 2693 struct xhci_virt_ep *smaller_ep; 2694 int normalized_interval; 2695 2696 if (xhci_is_async_ep(ep_bw->type)) 2697 return; 2698 2699 if (udev->speed == USB_SPEED_SUPER) { 2700 if (xhci_is_sync_in_ep(ep_bw->type)) 2701 xhci->devs[udev->slot_id]->bw_table->ss_bw_in += 2702 xhci_get_ss_bw_consumed(ep_bw); 2703 else 2704 xhci->devs[udev->slot_id]->bw_table->ss_bw_out += 2705 xhci_get_ss_bw_consumed(ep_bw); 2706 return; 2707 } 2708 2709 /* For LS/FS devices, we need to translate the interval expressed in 2710 * microframes to frames. 2711 */ 2712 if (udev->speed == USB_SPEED_HIGH) 2713 normalized_interval = ep_bw->ep_interval; 2714 else 2715 normalized_interval = ep_bw->ep_interval - 3; 2716 2717 if (normalized_interval == 0) 2718 bw_table->interval0_esit_payload += ep_bw->max_esit_payload; 2719 interval_bw = &bw_table->interval_bw[normalized_interval]; 2720 interval_bw->num_packets += ep_bw->num_packets; 2721 switch (udev->speed) { 2722 case USB_SPEED_LOW: 2723 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1; 2724 break; 2725 case USB_SPEED_FULL: 2726 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1; 2727 break; 2728 case USB_SPEED_HIGH: 2729 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1; 2730 break; 2731 case USB_SPEED_SUPER: 2732 case USB_SPEED_SUPER_PLUS: 2733 case USB_SPEED_UNKNOWN: 2734 case USB_SPEED_WIRELESS: 2735 /* Should never happen because only LS/FS/HS endpoints will get 2736 * added to the endpoint list. 2737 */ 2738 return; 2739 } 2740 2741 if (tt_info) 2742 tt_info->active_eps += 1; 2743 /* Insert the endpoint into the list, largest max packet size first. */ 2744 list_for_each_entry(smaller_ep, &interval_bw->endpoints, 2745 bw_endpoint_list) { 2746 if (ep_bw->max_packet_size >= 2747 smaller_ep->bw_info.max_packet_size) { 2748 /* Add the new ep before the smaller endpoint */ 2749 list_add_tail(&virt_ep->bw_endpoint_list, 2750 &smaller_ep->bw_endpoint_list); 2751 return; 2752 } 2753 } 2754 /* Add the new endpoint at the end of the list. */ 2755 list_add_tail(&virt_ep->bw_endpoint_list, 2756 &interval_bw->endpoints); 2757 } 2758 2759 void xhci_update_tt_active_eps(struct xhci_hcd *xhci, 2760 struct xhci_virt_device *virt_dev, 2761 int old_active_eps) 2762 { 2763 struct xhci_root_port_bw_info *rh_bw_info; 2764 if (!virt_dev->tt_info) 2765 return; 2766 2767 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1]; 2768 if (old_active_eps == 0 && 2769 virt_dev->tt_info->active_eps != 0) { 2770 rh_bw_info->num_active_tts += 1; 2771 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD; 2772 } else if (old_active_eps != 0 && 2773 virt_dev->tt_info->active_eps == 0) { 2774 rh_bw_info->num_active_tts -= 1; 2775 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD; 2776 } 2777 } 2778 2779 static int xhci_reserve_bandwidth(struct xhci_hcd *xhci, 2780 struct xhci_virt_device *virt_dev, 2781 struct xhci_container_ctx *in_ctx) 2782 { 2783 struct xhci_bw_info ep_bw_info[31]; 2784 int i; 2785 struct xhci_input_control_ctx *ctrl_ctx; 2786 int old_active_eps = 0; 2787 2788 if (virt_dev->tt_info) 2789 old_active_eps = virt_dev->tt_info->active_eps; 2790 2791 ctrl_ctx = xhci_get_input_control_ctx(in_ctx); 2792 if (!ctrl_ctx) { 2793 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 2794 __func__); 2795 return -ENOMEM; 2796 } 2797 2798 for (i = 0; i < 31; i++) { 2799 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i)) 2800 continue; 2801 2802 /* Make a copy of the BW info in case we need to revert this */ 2803 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info, 2804 sizeof(ep_bw_info[i])); 2805 /* Drop the endpoint from the interval table if the endpoint is 2806 * being dropped or changed. 2807 */ 2808 if (EP_IS_DROPPED(ctrl_ctx, i)) 2809 xhci_drop_ep_from_interval_table(xhci, 2810 &virt_dev->eps[i].bw_info, 2811 virt_dev->bw_table, 2812 virt_dev->udev, 2813 &virt_dev->eps[i], 2814 virt_dev->tt_info); 2815 } 2816 /* Overwrite the information stored in the endpoints' bw_info */ 2817 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev); 2818 for (i = 0; i < 31; i++) { 2819 /* Add any changed or added endpoints to the interval table */ 2820 if (EP_IS_ADDED(ctrl_ctx, i)) 2821 xhci_add_ep_to_interval_table(xhci, 2822 &virt_dev->eps[i].bw_info, 2823 virt_dev->bw_table, 2824 virt_dev->udev, 2825 &virt_dev->eps[i], 2826 virt_dev->tt_info); 2827 } 2828 2829 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) { 2830 /* Ok, this fits in the bandwidth we have. 2831 * Update the number of active TTs. 2832 */ 2833 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps); 2834 return 0; 2835 } 2836 2837 /* We don't have enough bandwidth for this, revert the stored info. */ 2838 for (i = 0; i < 31; i++) { 2839 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i)) 2840 continue; 2841 2842 /* Drop the new copies of any added or changed endpoints from 2843 * the interval table. 2844 */ 2845 if (EP_IS_ADDED(ctrl_ctx, i)) { 2846 xhci_drop_ep_from_interval_table(xhci, 2847 &virt_dev->eps[i].bw_info, 2848 virt_dev->bw_table, 2849 virt_dev->udev, 2850 &virt_dev->eps[i], 2851 virt_dev->tt_info); 2852 } 2853 /* Revert the endpoint back to its old information */ 2854 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i], 2855 sizeof(ep_bw_info[i])); 2856 /* Add any changed or dropped endpoints back into the table */ 2857 if (EP_IS_DROPPED(ctrl_ctx, i)) 2858 xhci_add_ep_to_interval_table(xhci, 2859 &virt_dev->eps[i].bw_info, 2860 virt_dev->bw_table, 2861 virt_dev->udev, 2862 &virt_dev->eps[i], 2863 virt_dev->tt_info); 2864 } 2865 return -ENOMEM; 2866 } 2867 2868 2869 /* Issue a configure endpoint command or evaluate context command 2870 * and wait for it to finish. 2871 */ 2872 static int xhci_configure_endpoint(struct xhci_hcd *xhci, 2873 struct usb_device *udev, 2874 struct xhci_command *command, 2875 bool ctx_change, bool must_succeed) 2876 { 2877 int ret; 2878 unsigned long flags; 2879 struct xhci_input_control_ctx *ctrl_ctx; 2880 struct xhci_virt_device *virt_dev; 2881 struct xhci_slot_ctx *slot_ctx; 2882 2883 if (!command) 2884 return -EINVAL; 2885 2886 spin_lock_irqsave(&xhci->lock, flags); 2887 2888 if (xhci->xhc_state & XHCI_STATE_DYING) { 2889 spin_unlock_irqrestore(&xhci->lock, flags); 2890 return -ESHUTDOWN; 2891 } 2892 2893 virt_dev = xhci->devs[udev->slot_id]; 2894 2895 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); 2896 if (!ctrl_ctx) { 2897 spin_unlock_irqrestore(&xhci->lock, flags); 2898 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 2899 __func__); 2900 return -ENOMEM; 2901 } 2902 2903 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) && 2904 xhci_reserve_host_resources(xhci, ctrl_ctx)) { 2905 spin_unlock_irqrestore(&xhci->lock, flags); 2906 xhci_warn(xhci, "Not enough host resources, " 2907 "active endpoint contexts = %u\n", 2908 xhci->num_active_eps); 2909 return -ENOMEM; 2910 } 2911 if ((xhci->quirks & XHCI_SW_BW_CHECKING) && 2912 xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) { 2913 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) 2914 xhci_free_host_resources(xhci, ctrl_ctx); 2915 spin_unlock_irqrestore(&xhci->lock, flags); 2916 xhci_warn(xhci, "Not enough bandwidth\n"); 2917 return -ENOMEM; 2918 } 2919 2920 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx); 2921 2922 trace_xhci_configure_endpoint_ctrl_ctx(ctrl_ctx); 2923 trace_xhci_configure_endpoint(slot_ctx); 2924 2925 if (!ctx_change) 2926 ret = xhci_queue_configure_endpoint(xhci, command, 2927 command->in_ctx->dma, 2928 udev->slot_id, must_succeed); 2929 else 2930 ret = xhci_queue_evaluate_context(xhci, command, 2931 command->in_ctx->dma, 2932 udev->slot_id, must_succeed); 2933 if (ret < 0) { 2934 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) 2935 xhci_free_host_resources(xhci, ctrl_ctx); 2936 spin_unlock_irqrestore(&xhci->lock, flags); 2937 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, 2938 "FIXME allocate a new ring segment"); 2939 return -ENOMEM; 2940 } 2941 xhci_ring_cmd_db(xhci); 2942 spin_unlock_irqrestore(&xhci->lock, flags); 2943 2944 /* Wait for the configure endpoint command to complete */ 2945 wait_for_completion(command->completion); 2946 2947 if (!ctx_change) 2948 ret = xhci_configure_endpoint_result(xhci, udev, 2949 &command->status); 2950 else 2951 ret = xhci_evaluate_context_result(xhci, udev, 2952 &command->status); 2953 2954 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { 2955 spin_lock_irqsave(&xhci->lock, flags); 2956 /* If the command failed, remove the reserved resources. 2957 * Otherwise, clean up the estimate to include dropped eps. 2958 */ 2959 if (ret) 2960 xhci_free_host_resources(xhci, ctrl_ctx); 2961 else 2962 xhci_finish_resource_reservation(xhci, ctrl_ctx); 2963 spin_unlock_irqrestore(&xhci->lock, flags); 2964 } 2965 return ret; 2966 } 2967 2968 static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci, 2969 struct xhci_virt_device *vdev, int i) 2970 { 2971 struct xhci_virt_ep *ep = &vdev->eps[i]; 2972 2973 if (ep->ep_state & EP_HAS_STREAMS) { 2974 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n", 2975 xhci_get_endpoint_address(i)); 2976 xhci_free_stream_info(xhci, ep->stream_info); 2977 ep->stream_info = NULL; 2978 ep->ep_state &= ~EP_HAS_STREAMS; 2979 } 2980 } 2981 2982 /* Called after one or more calls to xhci_add_endpoint() or 2983 * xhci_drop_endpoint(). If this call fails, the USB core is expected 2984 * to call xhci_reset_bandwidth(). 2985 * 2986 * Since we are in the middle of changing either configuration or 2987 * installing a new alt setting, the USB core won't allow URBs to be 2988 * enqueued for any endpoint on the old config or interface. Nothing 2989 * else should be touching the xhci->devs[slot_id] structure, so we 2990 * don't need to take the xhci->lock for manipulating that. 2991 */ 2992 int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev) 2993 { 2994 int i; 2995 int ret = 0; 2996 struct xhci_hcd *xhci; 2997 struct xhci_virt_device *virt_dev; 2998 struct xhci_input_control_ctx *ctrl_ctx; 2999 struct xhci_slot_ctx *slot_ctx; 3000 struct xhci_command *command; 3001 3002 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__); 3003 if (ret <= 0) 3004 return ret; 3005 xhci = hcd_to_xhci(hcd); 3006 if ((xhci->xhc_state & XHCI_STATE_DYING) || 3007 (xhci->xhc_state & XHCI_STATE_REMOVING)) 3008 return -ENODEV; 3009 3010 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); 3011 virt_dev = xhci->devs[udev->slot_id]; 3012 3013 command = xhci_alloc_command(xhci, true, GFP_KERNEL); 3014 if (!command) 3015 return -ENOMEM; 3016 3017 command->in_ctx = virt_dev->in_ctx; 3018 3019 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */ 3020 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); 3021 if (!ctrl_ctx) { 3022 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 3023 __func__); 3024 ret = -ENOMEM; 3025 goto command_cleanup; 3026 } 3027 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); 3028 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG); 3029 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG)); 3030 3031 /* Don't issue the command if there's no endpoints to update. */ 3032 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) && 3033 ctrl_ctx->drop_flags == 0) { 3034 ret = 0; 3035 goto command_cleanup; 3036 } 3037 /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */ 3038 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); 3039 for (i = 31; i >= 1; i--) { 3040 __le32 le32 = cpu_to_le32(BIT(i)); 3041 3042 if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32)) 3043 || (ctrl_ctx->add_flags & le32) || i == 1) { 3044 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK); 3045 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i)); 3046 break; 3047 } 3048 } 3049 3050 ret = xhci_configure_endpoint(xhci, udev, command, 3051 false, false); 3052 if (ret) 3053 /* Callee should call reset_bandwidth() */ 3054 goto command_cleanup; 3055 3056 /* Free any rings that were dropped, but not changed. */ 3057 for (i = 1; i < 31; i++) { 3058 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) && 3059 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) { 3060 xhci_free_endpoint_ring(xhci, virt_dev, i); 3061 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i); 3062 } 3063 } 3064 xhci_zero_in_ctx(xhci, virt_dev); 3065 /* 3066 * Install any rings for completely new endpoints or changed endpoints, 3067 * and free any old rings from changed endpoints. 3068 */ 3069 for (i = 1; i < 31; i++) { 3070 if (!virt_dev->eps[i].new_ring) 3071 continue; 3072 /* Only free the old ring if it exists. 3073 * It may not if this is the first add of an endpoint. 3074 */ 3075 if (virt_dev->eps[i].ring) { 3076 xhci_free_endpoint_ring(xhci, virt_dev, i); 3077 } 3078 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i); 3079 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring; 3080 virt_dev->eps[i].new_ring = NULL; 3081 xhci_debugfs_create_endpoint(xhci, virt_dev, i); 3082 } 3083 command_cleanup: 3084 kfree(command->completion); 3085 kfree(command); 3086 3087 return ret; 3088 } 3089 3090 void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev) 3091 { 3092 struct xhci_hcd *xhci; 3093 struct xhci_virt_device *virt_dev; 3094 int i, ret; 3095 3096 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__); 3097 if (ret <= 0) 3098 return; 3099 xhci = hcd_to_xhci(hcd); 3100 3101 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); 3102 virt_dev = xhci->devs[udev->slot_id]; 3103 /* Free any rings allocated for added endpoints */ 3104 for (i = 0; i < 31; i++) { 3105 if (virt_dev->eps[i].new_ring) { 3106 xhci_debugfs_remove_endpoint(xhci, virt_dev, i); 3107 xhci_ring_free(xhci, virt_dev->eps[i].new_ring); 3108 virt_dev->eps[i].new_ring = NULL; 3109 } 3110 } 3111 xhci_zero_in_ctx(xhci, virt_dev); 3112 } 3113 3114 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci, 3115 struct xhci_container_ctx *in_ctx, 3116 struct xhci_container_ctx *out_ctx, 3117 struct xhci_input_control_ctx *ctrl_ctx, 3118 u32 add_flags, u32 drop_flags) 3119 { 3120 ctrl_ctx->add_flags = cpu_to_le32(add_flags); 3121 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags); 3122 xhci_slot_copy(xhci, in_ctx, out_ctx); 3123 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); 3124 } 3125 3126 static void xhci_endpoint_disable(struct usb_hcd *hcd, 3127 struct usb_host_endpoint *host_ep) 3128 { 3129 struct xhci_hcd *xhci; 3130 struct xhci_virt_device *vdev; 3131 struct xhci_virt_ep *ep; 3132 struct usb_device *udev; 3133 unsigned long flags; 3134 unsigned int ep_index; 3135 3136 xhci = hcd_to_xhci(hcd); 3137 rescan: 3138 spin_lock_irqsave(&xhci->lock, flags); 3139 3140 udev = (struct usb_device *)host_ep->hcpriv; 3141 if (!udev || !udev->slot_id) 3142 goto done; 3143 3144 vdev = xhci->devs[udev->slot_id]; 3145 if (!vdev) 3146 goto done; 3147 3148 ep_index = xhci_get_endpoint_index(&host_ep->desc); 3149 ep = &vdev->eps[ep_index]; 3150 if (!ep) 3151 goto done; 3152 3153 /* wait for hub_tt_work to finish clearing hub TT */ 3154 if (ep->ep_state & EP_CLEARING_TT) { 3155 spin_unlock_irqrestore(&xhci->lock, flags); 3156 schedule_timeout_uninterruptible(1); 3157 goto rescan; 3158 } 3159 3160 if (ep->ep_state) 3161 xhci_dbg(xhci, "endpoint disable with ep_state 0x%x\n", 3162 ep->ep_state); 3163 done: 3164 host_ep->hcpriv = NULL; 3165 spin_unlock_irqrestore(&xhci->lock, flags); 3166 } 3167 3168 /* 3169 * Called after usb core issues a clear halt control message. 3170 * The host side of the halt should already be cleared by a reset endpoint 3171 * command issued when the STALL event was received. 3172 * 3173 * The reset endpoint command may only be issued to endpoints in the halted 3174 * state. For software that wishes to reset the data toggle or sequence number 3175 * of an endpoint that isn't in the halted state this function will issue a 3176 * configure endpoint command with the Drop and Add bits set for the target 3177 * endpoint. Refer to the additional note in xhci spcification section 4.6.8. 3178 */ 3179 3180 static void xhci_endpoint_reset(struct usb_hcd *hcd, 3181 struct usb_host_endpoint *host_ep) 3182 { 3183 struct xhci_hcd *xhci; 3184 struct usb_device *udev; 3185 struct xhci_virt_device *vdev; 3186 struct xhci_virt_ep *ep; 3187 struct xhci_input_control_ctx *ctrl_ctx; 3188 struct xhci_command *stop_cmd, *cfg_cmd; 3189 unsigned int ep_index; 3190 unsigned long flags; 3191 u32 ep_flag; 3192 int err; 3193 3194 xhci = hcd_to_xhci(hcd); 3195 if (!host_ep->hcpriv) 3196 return; 3197 udev = (struct usb_device *) host_ep->hcpriv; 3198 vdev = xhci->devs[udev->slot_id]; 3199 3200 /* 3201 * vdev may be lost due to xHC restore error and re-initialization 3202 * during S3/S4 resume. A new vdev will be allocated later by 3203 * xhci_discover_or_reset_device() 3204 */ 3205 if (!udev->slot_id || !vdev) 3206 return; 3207 ep_index = xhci_get_endpoint_index(&host_ep->desc); 3208 ep = &vdev->eps[ep_index]; 3209 if (!ep) 3210 return; 3211 3212 /* Bail out if toggle is already being cleared by a endpoint reset */ 3213 if (ep->ep_state & EP_HARD_CLEAR_TOGGLE) { 3214 ep->ep_state &= ~EP_HARD_CLEAR_TOGGLE; 3215 return; 3216 } 3217 /* Only interrupt and bulk ep's use data toggle, USB2 spec 5.5.4-> */ 3218 if (usb_endpoint_xfer_control(&host_ep->desc) || 3219 usb_endpoint_xfer_isoc(&host_ep->desc)) 3220 return; 3221 3222 ep_flag = xhci_get_endpoint_flag(&host_ep->desc); 3223 3224 if (ep_flag == SLOT_FLAG || ep_flag == EP0_FLAG) 3225 return; 3226 3227 stop_cmd = xhci_alloc_command(xhci, true, GFP_NOWAIT); 3228 if (!stop_cmd) 3229 return; 3230 3231 cfg_cmd = xhci_alloc_command_with_ctx(xhci, true, GFP_NOWAIT); 3232 if (!cfg_cmd) 3233 goto cleanup; 3234 3235 spin_lock_irqsave(&xhci->lock, flags); 3236 3237 /* block queuing new trbs and ringing ep doorbell */ 3238 ep->ep_state |= EP_SOFT_CLEAR_TOGGLE; 3239 3240 /* 3241 * Make sure endpoint ring is empty before resetting the toggle/seq. 3242 * Driver is required to synchronously cancel all transfer request. 3243 * Stop the endpoint to force xHC to update the output context 3244 */ 3245 3246 if (!list_empty(&ep->ring->td_list)) { 3247 dev_err(&udev->dev, "EP not empty, refuse reset\n"); 3248 spin_unlock_irqrestore(&xhci->lock, flags); 3249 xhci_free_command(xhci, cfg_cmd); 3250 goto cleanup; 3251 } 3252 3253 err = xhci_queue_stop_endpoint(xhci, stop_cmd, udev->slot_id, 3254 ep_index, 0); 3255 if (err < 0) { 3256 spin_unlock_irqrestore(&xhci->lock, flags); 3257 xhci_free_command(xhci, cfg_cmd); 3258 xhci_dbg(xhci, "%s: Failed to queue stop ep command, %d ", 3259 __func__, err); 3260 goto cleanup; 3261 } 3262 3263 xhci_ring_cmd_db(xhci); 3264 spin_unlock_irqrestore(&xhci->lock, flags); 3265 3266 wait_for_completion(stop_cmd->completion); 3267 3268 spin_lock_irqsave(&xhci->lock, flags); 3269 3270 /* config ep command clears toggle if add and drop ep flags are set */ 3271 ctrl_ctx = xhci_get_input_control_ctx(cfg_cmd->in_ctx); 3272 xhci_setup_input_ctx_for_config_ep(xhci, cfg_cmd->in_ctx, vdev->out_ctx, 3273 ctrl_ctx, ep_flag, ep_flag); 3274 xhci_endpoint_copy(xhci, cfg_cmd->in_ctx, vdev->out_ctx, ep_index); 3275 3276 err = xhci_queue_configure_endpoint(xhci, cfg_cmd, cfg_cmd->in_ctx->dma, 3277 udev->slot_id, false); 3278 if (err < 0) { 3279 spin_unlock_irqrestore(&xhci->lock, flags); 3280 xhci_free_command(xhci, cfg_cmd); 3281 xhci_dbg(xhci, "%s: Failed to queue config ep command, %d ", 3282 __func__, err); 3283 goto cleanup; 3284 } 3285 3286 xhci_ring_cmd_db(xhci); 3287 spin_unlock_irqrestore(&xhci->lock, flags); 3288 3289 wait_for_completion(cfg_cmd->completion); 3290 3291 xhci_free_command(xhci, cfg_cmd); 3292 cleanup: 3293 xhci_free_command(xhci, stop_cmd); 3294 if (ep->ep_state & EP_SOFT_CLEAR_TOGGLE) 3295 ep->ep_state &= ~EP_SOFT_CLEAR_TOGGLE; 3296 } 3297 3298 static int xhci_check_streams_endpoint(struct xhci_hcd *xhci, 3299 struct usb_device *udev, struct usb_host_endpoint *ep, 3300 unsigned int slot_id) 3301 { 3302 int ret; 3303 unsigned int ep_index; 3304 unsigned int ep_state; 3305 3306 if (!ep) 3307 return -EINVAL; 3308 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__); 3309 if (ret <= 0) 3310 return -EINVAL; 3311 if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) { 3312 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion" 3313 " descriptor for ep 0x%x does not support streams\n", 3314 ep->desc.bEndpointAddress); 3315 return -EINVAL; 3316 } 3317 3318 ep_index = xhci_get_endpoint_index(&ep->desc); 3319 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; 3320 if (ep_state & EP_HAS_STREAMS || 3321 ep_state & EP_GETTING_STREAMS) { 3322 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x " 3323 "already has streams set up.\n", 3324 ep->desc.bEndpointAddress); 3325 xhci_warn(xhci, "Send email to xHCI maintainer and ask for " 3326 "dynamic stream context array reallocation.\n"); 3327 return -EINVAL; 3328 } 3329 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) { 3330 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk " 3331 "endpoint 0x%x; URBs are pending.\n", 3332 ep->desc.bEndpointAddress); 3333 return -EINVAL; 3334 } 3335 return 0; 3336 } 3337 3338 static void xhci_calculate_streams_entries(struct xhci_hcd *xhci, 3339 unsigned int *num_streams, unsigned int *num_stream_ctxs) 3340 { 3341 unsigned int max_streams; 3342 3343 /* The stream context array size must be a power of two */ 3344 *num_stream_ctxs = roundup_pow_of_two(*num_streams); 3345 /* 3346 * Find out how many primary stream array entries the host controller 3347 * supports. Later we may use secondary stream arrays (similar to 2nd 3348 * level page entries), but that's an optional feature for xHCI host 3349 * controllers. xHCs must support at least 4 stream IDs. 3350 */ 3351 max_streams = HCC_MAX_PSA(xhci->hcc_params); 3352 if (*num_stream_ctxs > max_streams) { 3353 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n", 3354 max_streams); 3355 *num_stream_ctxs = max_streams; 3356 *num_streams = max_streams; 3357 } 3358 } 3359 3360 /* Returns an error code if one of the endpoint already has streams. 3361 * This does not change any data structures, it only checks and gathers 3362 * information. 3363 */ 3364 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci, 3365 struct usb_device *udev, 3366 struct usb_host_endpoint **eps, unsigned int num_eps, 3367 unsigned int *num_streams, u32 *changed_ep_bitmask) 3368 { 3369 unsigned int max_streams; 3370 unsigned int endpoint_flag; 3371 int i; 3372 int ret; 3373 3374 for (i = 0; i < num_eps; i++) { 3375 ret = xhci_check_streams_endpoint(xhci, udev, 3376 eps[i], udev->slot_id); 3377 if (ret < 0) 3378 return ret; 3379 3380 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp); 3381 if (max_streams < (*num_streams - 1)) { 3382 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n", 3383 eps[i]->desc.bEndpointAddress, 3384 max_streams); 3385 *num_streams = max_streams+1; 3386 } 3387 3388 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc); 3389 if (*changed_ep_bitmask & endpoint_flag) 3390 return -EINVAL; 3391 *changed_ep_bitmask |= endpoint_flag; 3392 } 3393 return 0; 3394 } 3395 3396 static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci, 3397 struct usb_device *udev, 3398 struct usb_host_endpoint **eps, unsigned int num_eps) 3399 { 3400 u32 changed_ep_bitmask = 0; 3401 unsigned int slot_id; 3402 unsigned int ep_index; 3403 unsigned int ep_state; 3404 int i; 3405 3406 slot_id = udev->slot_id; 3407 if (!xhci->devs[slot_id]) 3408 return 0; 3409 3410 for (i = 0; i < num_eps; i++) { 3411 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3412 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; 3413 /* Are streams already being freed for the endpoint? */ 3414 if (ep_state & EP_GETTING_NO_STREAMS) { 3415 xhci_warn(xhci, "WARN Can't disable streams for " 3416 "endpoint 0x%x, " 3417 "streams are being disabled already\n", 3418 eps[i]->desc.bEndpointAddress); 3419 return 0; 3420 } 3421 /* Are there actually any streams to free? */ 3422 if (!(ep_state & EP_HAS_STREAMS) && 3423 !(ep_state & EP_GETTING_STREAMS)) { 3424 xhci_warn(xhci, "WARN Can't disable streams for " 3425 "endpoint 0x%x, " 3426 "streams are already disabled!\n", 3427 eps[i]->desc.bEndpointAddress); 3428 xhci_warn(xhci, "WARN xhci_free_streams() called " 3429 "with non-streams endpoint\n"); 3430 return 0; 3431 } 3432 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc); 3433 } 3434 return changed_ep_bitmask; 3435 } 3436 3437 /* 3438 * The USB device drivers use this function (through the HCD interface in USB 3439 * core) to prepare a set of bulk endpoints to use streams. Streams are used to 3440 * coordinate mass storage command queueing across multiple endpoints (basically 3441 * a stream ID == a task ID). 3442 * 3443 * Setting up streams involves allocating the same size stream context array 3444 * for each endpoint and issuing a configure endpoint command for all endpoints. 3445 * 3446 * Don't allow the call to succeed if one endpoint only supports one stream 3447 * (which means it doesn't support streams at all). 3448 * 3449 * Drivers may get less stream IDs than they asked for, if the host controller 3450 * hardware or endpoints claim they can't support the number of requested 3451 * stream IDs. 3452 */ 3453 static int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev, 3454 struct usb_host_endpoint **eps, unsigned int num_eps, 3455 unsigned int num_streams, gfp_t mem_flags) 3456 { 3457 int i, ret; 3458 struct xhci_hcd *xhci; 3459 struct xhci_virt_device *vdev; 3460 struct xhci_command *config_cmd; 3461 struct xhci_input_control_ctx *ctrl_ctx; 3462 unsigned int ep_index; 3463 unsigned int num_stream_ctxs; 3464 unsigned int max_packet; 3465 unsigned long flags; 3466 u32 changed_ep_bitmask = 0; 3467 3468 if (!eps) 3469 return -EINVAL; 3470 3471 /* Add one to the number of streams requested to account for 3472 * stream 0 that is reserved for xHCI usage. 3473 */ 3474 num_streams += 1; 3475 xhci = hcd_to_xhci(hcd); 3476 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n", 3477 num_streams); 3478 3479 /* MaxPSASize value 0 (2 streams) means streams are not supported */ 3480 if ((xhci->quirks & XHCI_BROKEN_STREAMS) || 3481 HCC_MAX_PSA(xhci->hcc_params) < 4) { 3482 xhci_dbg(xhci, "xHCI controller does not support streams.\n"); 3483 return -ENOSYS; 3484 } 3485 3486 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags); 3487 if (!config_cmd) 3488 return -ENOMEM; 3489 3490 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx); 3491 if (!ctrl_ctx) { 3492 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 3493 __func__); 3494 xhci_free_command(xhci, config_cmd); 3495 return -ENOMEM; 3496 } 3497 3498 /* Check to make sure all endpoints are not already configured for 3499 * streams. While we're at it, find the maximum number of streams that 3500 * all the endpoints will support and check for duplicate endpoints. 3501 */ 3502 spin_lock_irqsave(&xhci->lock, flags); 3503 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps, 3504 num_eps, &num_streams, &changed_ep_bitmask); 3505 if (ret < 0) { 3506 xhci_free_command(xhci, config_cmd); 3507 spin_unlock_irqrestore(&xhci->lock, flags); 3508 return ret; 3509 } 3510 if (num_streams <= 1) { 3511 xhci_warn(xhci, "WARN: endpoints can't handle " 3512 "more than one stream.\n"); 3513 xhci_free_command(xhci, config_cmd); 3514 spin_unlock_irqrestore(&xhci->lock, flags); 3515 return -EINVAL; 3516 } 3517 vdev = xhci->devs[udev->slot_id]; 3518 /* Mark each endpoint as being in transition, so 3519 * xhci_urb_enqueue() will reject all URBs. 3520 */ 3521 for (i = 0; i < num_eps; i++) { 3522 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3523 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS; 3524 } 3525 spin_unlock_irqrestore(&xhci->lock, flags); 3526 3527 /* Setup internal data structures and allocate HW data structures for 3528 * streams (but don't install the HW structures in the input context 3529 * until we're sure all memory allocation succeeded). 3530 */ 3531 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs); 3532 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n", 3533 num_stream_ctxs, num_streams); 3534 3535 for (i = 0; i < num_eps; i++) { 3536 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3537 max_packet = usb_endpoint_maxp(&eps[i]->desc); 3538 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci, 3539 num_stream_ctxs, 3540 num_streams, 3541 max_packet, mem_flags); 3542 if (!vdev->eps[ep_index].stream_info) 3543 goto cleanup; 3544 /* Set maxPstreams in endpoint context and update deq ptr to 3545 * point to stream context array. FIXME 3546 */ 3547 } 3548 3549 /* Set up the input context for a configure endpoint command. */ 3550 for (i = 0; i < num_eps; i++) { 3551 struct xhci_ep_ctx *ep_ctx; 3552 3553 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3554 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index); 3555 3556 xhci_endpoint_copy(xhci, config_cmd->in_ctx, 3557 vdev->out_ctx, ep_index); 3558 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx, 3559 vdev->eps[ep_index].stream_info); 3560 } 3561 /* Tell the HW to drop its old copy of the endpoint context info 3562 * and add the updated copy from the input context. 3563 */ 3564 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx, 3565 vdev->out_ctx, ctrl_ctx, 3566 changed_ep_bitmask, changed_ep_bitmask); 3567 3568 /* Issue and wait for the configure endpoint command */ 3569 ret = xhci_configure_endpoint(xhci, udev, config_cmd, 3570 false, false); 3571 3572 /* xHC rejected the configure endpoint command for some reason, so we 3573 * leave the old ring intact and free our internal streams data 3574 * structure. 3575 */ 3576 if (ret < 0) 3577 goto cleanup; 3578 3579 spin_lock_irqsave(&xhci->lock, flags); 3580 for (i = 0; i < num_eps; i++) { 3581 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3582 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; 3583 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n", 3584 udev->slot_id, ep_index); 3585 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS; 3586 } 3587 xhci_free_command(xhci, config_cmd); 3588 spin_unlock_irqrestore(&xhci->lock, flags); 3589 3590 for (i = 0; i < num_eps; i++) { 3591 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3592 xhci_debugfs_create_stream_files(xhci, vdev, ep_index); 3593 } 3594 /* Subtract 1 for stream 0, which drivers can't use */ 3595 return num_streams - 1; 3596 3597 cleanup: 3598 /* If it didn't work, free the streams! */ 3599 for (i = 0; i < num_eps; i++) { 3600 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3601 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); 3602 vdev->eps[ep_index].stream_info = NULL; 3603 /* FIXME Unset maxPstreams in endpoint context and 3604 * update deq ptr to point to normal string ring. 3605 */ 3606 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; 3607 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; 3608 xhci_endpoint_zero(xhci, vdev, eps[i]); 3609 } 3610 xhci_free_command(xhci, config_cmd); 3611 return -ENOMEM; 3612 } 3613 3614 /* Transition the endpoint from using streams to being a "normal" endpoint 3615 * without streams. 3616 * 3617 * Modify the endpoint context state, submit a configure endpoint command, 3618 * and free all endpoint rings for streams if that completes successfully. 3619 */ 3620 static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev, 3621 struct usb_host_endpoint **eps, unsigned int num_eps, 3622 gfp_t mem_flags) 3623 { 3624 int i, ret; 3625 struct xhci_hcd *xhci; 3626 struct xhci_virt_device *vdev; 3627 struct xhci_command *command; 3628 struct xhci_input_control_ctx *ctrl_ctx; 3629 unsigned int ep_index; 3630 unsigned long flags; 3631 u32 changed_ep_bitmask; 3632 3633 xhci = hcd_to_xhci(hcd); 3634 vdev = xhci->devs[udev->slot_id]; 3635 3636 /* Set up a configure endpoint command to remove the streams rings */ 3637 spin_lock_irqsave(&xhci->lock, flags); 3638 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci, 3639 udev, eps, num_eps); 3640 if (changed_ep_bitmask == 0) { 3641 spin_unlock_irqrestore(&xhci->lock, flags); 3642 return -EINVAL; 3643 } 3644 3645 /* Use the xhci_command structure from the first endpoint. We may have 3646 * allocated too many, but the driver may call xhci_free_streams() for 3647 * each endpoint it grouped into one call to xhci_alloc_streams(). 3648 */ 3649 ep_index = xhci_get_endpoint_index(&eps[0]->desc); 3650 command = vdev->eps[ep_index].stream_info->free_streams_command; 3651 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); 3652 if (!ctrl_ctx) { 3653 spin_unlock_irqrestore(&xhci->lock, flags); 3654 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 3655 __func__); 3656 return -EINVAL; 3657 } 3658 3659 for (i = 0; i < num_eps; i++) { 3660 struct xhci_ep_ctx *ep_ctx; 3661 3662 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3663 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); 3664 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |= 3665 EP_GETTING_NO_STREAMS; 3666 3667 xhci_endpoint_copy(xhci, command->in_ctx, 3668 vdev->out_ctx, ep_index); 3669 xhci_setup_no_streams_ep_input_ctx(ep_ctx, 3670 &vdev->eps[ep_index]); 3671 } 3672 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx, 3673 vdev->out_ctx, ctrl_ctx, 3674 changed_ep_bitmask, changed_ep_bitmask); 3675 spin_unlock_irqrestore(&xhci->lock, flags); 3676 3677 /* Issue and wait for the configure endpoint command, 3678 * which must succeed. 3679 */ 3680 ret = xhci_configure_endpoint(xhci, udev, command, 3681 false, true); 3682 3683 /* xHC rejected the configure endpoint command for some reason, so we 3684 * leave the streams rings intact. 3685 */ 3686 if (ret < 0) 3687 return ret; 3688 3689 spin_lock_irqsave(&xhci->lock, flags); 3690 for (i = 0; i < num_eps; i++) { 3691 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3692 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); 3693 vdev->eps[ep_index].stream_info = NULL; 3694 /* FIXME Unset maxPstreams in endpoint context and 3695 * update deq ptr to point to normal string ring. 3696 */ 3697 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS; 3698 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; 3699 } 3700 spin_unlock_irqrestore(&xhci->lock, flags); 3701 3702 return 0; 3703 } 3704 3705 /* 3706 * Deletes endpoint resources for endpoints that were active before a Reset 3707 * Device command, or a Disable Slot command. The Reset Device command leaves 3708 * the control endpoint intact, whereas the Disable Slot command deletes it. 3709 * 3710 * Must be called with xhci->lock held. 3711 */ 3712 void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci, 3713 struct xhci_virt_device *virt_dev, bool drop_control_ep) 3714 { 3715 int i; 3716 unsigned int num_dropped_eps = 0; 3717 unsigned int drop_flags = 0; 3718 3719 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) { 3720 if (virt_dev->eps[i].ring) { 3721 drop_flags |= 1 << i; 3722 num_dropped_eps++; 3723 } 3724 } 3725 xhci->num_active_eps -= num_dropped_eps; 3726 if (num_dropped_eps) 3727 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 3728 "Dropped %u ep ctxs, flags = 0x%x, " 3729 "%u now active.", 3730 num_dropped_eps, drop_flags, 3731 xhci->num_active_eps); 3732 } 3733 3734 /* 3735 * This submits a Reset Device Command, which will set the device state to 0, 3736 * set the device address to 0, and disable all the endpoints except the default 3737 * control endpoint. The USB core should come back and call 3738 * xhci_address_device(), and then re-set up the configuration. If this is 3739 * called because of a usb_reset_and_verify_device(), then the old alternate 3740 * settings will be re-installed through the normal bandwidth allocation 3741 * functions. 3742 * 3743 * Wait for the Reset Device command to finish. Remove all structures 3744 * associated with the endpoints that were disabled. Clear the input device 3745 * structure? Reset the control endpoint 0 max packet size? 3746 * 3747 * If the virt_dev to be reset does not exist or does not match the udev, 3748 * it means the device is lost, possibly due to the xHC restore error and 3749 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to 3750 * re-allocate the device. 3751 */ 3752 static int xhci_discover_or_reset_device(struct usb_hcd *hcd, 3753 struct usb_device *udev) 3754 { 3755 int ret, i; 3756 unsigned long flags; 3757 struct xhci_hcd *xhci; 3758 unsigned int slot_id; 3759 struct xhci_virt_device *virt_dev; 3760 struct xhci_command *reset_device_cmd; 3761 struct xhci_slot_ctx *slot_ctx; 3762 int old_active_eps = 0; 3763 3764 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__); 3765 if (ret <= 0) 3766 return ret; 3767 xhci = hcd_to_xhci(hcd); 3768 slot_id = udev->slot_id; 3769 virt_dev = xhci->devs[slot_id]; 3770 if (!virt_dev) { 3771 xhci_dbg(xhci, "The device to be reset with slot ID %u does " 3772 "not exist. Re-allocate the device\n", slot_id); 3773 ret = xhci_alloc_dev(hcd, udev); 3774 if (ret == 1) 3775 return 0; 3776 else 3777 return -EINVAL; 3778 } 3779 3780 if (virt_dev->tt_info) 3781 old_active_eps = virt_dev->tt_info->active_eps; 3782 3783 if (virt_dev->udev != udev) { 3784 /* If the virt_dev and the udev does not match, this virt_dev 3785 * may belong to another udev. 3786 * Re-allocate the device. 3787 */ 3788 xhci_dbg(xhci, "The device to be reset with slot ID %u does " 3789 "not match the udev. Re-allocate the device\n", 3790 slot_id); 3791 ret = xhci_alloc_dev(hcd, udev); 3792 if (ret == 1) 3793 return 0; 3794 else 3795 return -EINVAL; 3796 } 3797 3798 /* If device is not setup, there is no point in resetting it */ 3799 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 3800 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) == 3801 SLOT_STATE_DISABLED) 3802 return 0; 3803 3804 trace_xhci_discover_or_reset_device(slot_ctx); 3805 3806 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id); 3807 /* Allocate the command structure that holds the struct completion. 3808 * Assume we're in process context, since the normal device reset 3809 * process has to wait for the device anyway. Storage devices are 3810 * reset as part of error handling, so use GFP_NOIO instead of 3811 * GFP_KERNEL. 3812 */ 3813 reset_device_cmd = xhci_alloc_command(xhci, true, GFP_NOIO); 3814 if (!reset_device_cmd) { 3815 xhci_dbg(xhci, "Couldn't allocate command structure.\n"); 3816 return -ENOMEM; 3817 } 3818 3819 /* Attempt to submit the Reset Device command to the command ring */ 3820 spin_lock_irqsave(&xhci->lock, flags); 3821 3822 ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id); 3823 if (ret) { 3824 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); 3825 spin_unlock_irqrestore(&xhci->lock, flags); 3826 goto command_cleanup; 3827 } 3828 xhci_ring_cmd_db(xhci); 3829 spin_unlock_irqrestore(&xhci->lock, flags); 3830 3831 /* Wait for the Reset Device command to finish */ 3832 wait_for_completion(reset_device_cmd->completion); 3833 3834 /* The Reset Device command can't fail, according to the 0.95/0.96 spec, 3835 * unless we tried to reset a slot ID that wasn't enabled, 3836 * or the device wasn't in the addressed or configured state. 3837 */ 3838 ret = reset_device_cmd->status; 3839 switch (ret) { 3840 case COMP_COMMAND_ABORTED: 3841 case COMP_COMMAND_RING_STOPPED: 3842 xhci_warn(xhci, "Timeout waiting for reset device command\n"); 3843 ret = -ETIME; 3844 goto command_cleanup; 3845 case COMP_SLOT_NOT_ENABLED_ERROR: /* 0.95 completion for bad slot ID */ 3846 case COMP_CONTEXT_STATE_ERROR: /* 0.96 completion code for same thing */ 3847 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n", 3848 slot_id, 3849 xhci_get_slot_state(xhci, virt_dev->out_ctx)); 3850 xhci_dbg(xhci, "Not freeing device rings.\n"); 3851 /* Don't treat this as an error. May change my mind later. */ 3852 ret = 0; 3853 goto command_cleanup; 3854 case COMP_SUCCESS: 3855 xhci_dbg(xhci, "Successful reset device command.\n"); 3856 break; 3857 default: 3858 if (xhci_is_vendor_info_code(xhci, ret)) 3859 break; 3860 xhci_warn(xhci, "Unknown completion code %u for " 3861 "reset device command.\n", ret); 3862 ret = -EINVAL; 3863 goto command_cleanup; 3864 } 3865 3866 /* Free up host controller endpoint resources */ 3867 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { 3868 spin_lock_irqsave(&xhci->lock, flags); 3869 /* Don't delete the default control endpoint resources */ 3870 xhci_free_device_endpoint_resources(xhci, virt_dev, false); 3871 spin_unlock_irqrestore(&xhci->lock, flags); 3872 } 3873 3874 /* Everything but endpoint 0 is disabled, so free the rings. */ 3875 for (i = 1; i < 31; i++) { 3876 struct xhci_virt_ep *ep = &virt_dev->eps[i]; 3877 3878 if (ep->ep_state & EP_HAS_STREAMS) { 3879 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n", 3880 xhci_get_endpoint_address(i)); 3881 xhci_free_stream_info(xhci, ep->stream_info); 3882 ep->stream_info = NULL; 3883 ep->ep_state &= ~EP_HAS_STREAMS; 3884 } 3885 3886 if (ep->ring) { 3887 xhci_debugfs_remove_endpoint(xhci, virt_dev, i); 3888 xhci_free_endpoint_ring(xhci, virt_dev, i); 3889 } 3890 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list)) 3891 xhci_drop_ep_from_interval_table(xhci, 3892 &virt_dev->eps[i].bw_info, 3893 virt_dev->bw_table, 3894 udev, 3895 &virt_dev->eps[i], 3896 virt_dev->tt_info); 3897 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info); 3898 } 3899 /* If necessary, update the number of active TTs on this root port */ 3900 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps); 3901 virt_dev->flags = 0; 3902 ret = 0; 3903 3904 command_cleanup: 3905 xhci_free_command(xhci, reset_device_cmd); 3906 return ret; 3907 } 3908 3909 /* 3910 * At this point, the struct usb_device is about to go away, the device has 3911 * disconnected, and all traffic has been stopped and the endpoints have been 3912 * disabled. Free any HC data structures associated with that device. 3913 */ 3914 static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev) 3915 { 3916 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 3917 struct xhci_virt_device *virt_dev; 3918 struct xhci_slot_ctx *slot_ctx; 3919 int i, ret; 3920 3921 #ifndef CONFIG_USB_DEFAULT_PERSIST 3922 /* 3923 * We called pm_runtime_get_noresume when the device was attached. 3924 * Decrement the counter here to allow controller to runtime suspend 3925 * if no devices remain. 3926 */ 3927 if (xhci->quirks & XHCI_RESET_ON_RESUME) 3928 pm_runtime_put_noidle(hcd->self.controller); 3929 #endif 3930 3931 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__); 3932 /* If the host is halted due to driver unload, we still need to free the 3933 * device. 3934 */ 3935 if (ret <= 0 && ret != -ENODEV) 3936 return; 3937 3938 virt_dev = xhci->devs[udev->slot_id]; 3939 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 3940 trace_xhci_free_dev(slot_ctx); 3941 3942 /* Stop any wayward timer functions (which may grab the lock) */ 3943 for (i = 0; i < 31; i++) { 3944 virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING; 3945 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer); 3946 } 3947 virt_dev->udev = NULL; 3948 ret = xhci_disable_slot(xhci, udev->slot_id); 3949 if (ret) 3950 xhci_free_virt_device(xhci, udev->slot_id); 3951 } 3952 3953 int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id) 3954 { 3955 struct xhci_command *command; 3956 unsigned long flags; 3957 u32 state; 3958 int ret = 0; 3959 3960 command = xhci_alloc_command(xhci, false, GFP_KERNEL); 3961 if (!command) 3962 return -ENOMEM; 3963 3964 xhci_debugfs_remove_slot(xhci, slot_id); 3965 3966 spin_lock_irqsave(&xhci->lock, flags); 3967 /* Don't disable the slot if the host controller is dead. */ 3968 state = readl(&xhci->op_regs->status); 3969 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) || 3970 (xhci->xhc_state & XHCI_STATE_HALTED)) { 3971 spin_unlock_irqrestore(&xhci->lock, flags); 3972 kfree(command); 3973 return -ENODEV; 3974 } 3975 3976 ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT, 3977 slot_id); 3978 if (ret) { 3979 spin_unlock_irqrestore(&xhci->lock, flags); 3980 kfree(command); 3981 return ret; 3982 } 3983 xhci_ring_cmd_db(xhci); 3984 spin_unlock_irqrestore(&xhci->lock, flags); 3985 return ret; 3986 } 3987 3988 /* 3989 * Checks if we have enough host controller resources for the default control 3990 * endpoint. 3991 * 3992 * Must be called with xhci->lock held. 3993 */ 3994 static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci) 3995 { 3996 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) { 3997 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 3998 "Not enough ep ctxs: " 3999 "%u active, need to add 1, limit is %u.", 4000 xhci->num_active_eps, xhci->limit_active_eps); 4001 return -ENOMEM; 4002 } 4003 xhci->num_active_eps += 1; 4004 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 4005 "Adding 1 ep ctx, %u now active.", 4006 xhci->num_active_eps); 4007 return 0; 4008 } 4009 4010 4011 /* 4012 * Returns 0 if the xHC ran out of device slots, the Enable Slot command 4013 * timed out, or allocating memory failed. Returns 1 on success. 4014 */ 4015 int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev) 4016 { 4017 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 4018 struct xhci_virt_device *vdev; 4019 struct xhci_slot_ctx *slot_ctx; 4020 unsigned long flags; 4021 int ret, slot_id; 4022 struct xhci_command *command; 4023 4024 command = xhci_alloc_command(xhci, true, GFP_KERNEL); 4025 if (!command) 4026 return 0; 4027 4028 spin_lock_irqsave(&xhci->lock, flags); 4029 ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0); 4030 if (ret) { 4031 spin_unlock_irqrestore(&xhci->lock, flags); 4032 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); 4033 xhci_free_command(xhci, command); 4034 return 0; 4035 } 4036 xhci_ring_cmd_db(xhci); 4037 spin_unlock_irqrestore(&xhci->lock, flags); 4038 4039 wait_for_completion(command->completion); 4040 slot_id = command->slot_id; 4041 4042 if (!slot_id || command->status != COMP_SUCCESS) { 4043 xhci_err(xhci, "Error while assigning device slot ID\n"); 4044 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n", 4045 HCS_MAX_SLOTS( 4046 readl(&xhci->cap_regs->hcs_params1))); 4047 xhci_free_command(xhci, command); 4048 return 0; 4049 } 4050 4051 xhci_free_command(xhci, command); 4052 4053 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { 4054 spin_lock_irqsave(&xhci->lock, flags); 4055 ret = xhci_reserve_host_control_ep_resources(xhci); 4056 if (ret) { 4057 spin_unlock_irqrestore(&xhci->lock, flags); 4058 xhci_warn(xhci, "Not enough host resources, " 4059 "active endpoint contexts = %u\n", 4060 xhci->num_active_eps); 4061 goto disable_slot; 4062 } 4063 spin_unlock_irqrestore(&xhci->lock, flags); 4064 } 4065 /* Use GFP_NOIO, since this function can be called from 4066 * xhci_discover_or_reset_device(), which may be called as part of 4067 * mass storage driver error handling. 4068 */ 4069 if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) { 4070 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n"); 4071 goto disable_slot; 4072 } 4073 vdev = xhci->devs[slot_id]; 4074 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); 4075 trace_xhci_alloc_dev(slot_ctx); 4076 4077 udev->slot_id = slot_id; 4078 4079 xhci_debugfs_create_slot(xhci, slot_id); 4080 4081 #ifndef CONFIG_USB_DEFAULT_PERSIST 4082 /* 4083 * If resetting upon resume, we can't put the controller into runtime 4084 * suspend if there is a device attached. 4085 */ 4086 if (xhci->quirks & XHCI_RESET_ON_RESUME) 4087 pm_runtime_get_noresume(hcd->self.controller); 4088 #endif 4089 4090 /* Is this a LS or FS device under a HS hub? */ 4091 /* Hub or peripherial? */ 4092 return 1; 4093 4094 disable_slot: 4095 ret = xhci_disable_slot(xhci, udev->slot_id); 4096 if (ret) 4097 xhci_free_virt_device(xhci, udev->slot_id); 4098 4099 return 0; 4100 } 4101 4102 /* 4103 * Issue an Address Device command and optionally send a corresponding 4104 * SetAddress request to the device. 4105 */ 4106 static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev, 4107 enum xhci_setup_dev setup) 4108 { 4109 const char *act = setup == SETUP_CONTEXT_ONLY ? "context" : "address"; 4110 unsigned long flags; 4111 struct xhci_virt_device *virt_dev; 4112 int ret = 0; 4113 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 4114 struct xhci_slot_ctx *slot_ctx; 4115 struct xhci_input_control_ctx *ctrl_ctx; 4116 u64 temp_64; 4117 struct xhci_command *command = NULL; 4118 4119 mutex_lock(&xhci->mutex); 4120 4121 if (xhci->xhc_state) { /* dying, removing or halted */ 4122 ret = -ESHUTDOWN; 4123 goto out; 4124 } 4125 4126 if (!udev->slot_id) { 4127 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4128 "Bad Slot ID %d", udev->slot_id); 4129 ret = -EINVAL; 4130 goto out; 4131 } 4132 4133 virt_dev = xhci->devs[udev->slot_id]; 4134 4135 if (WARN_ON(!virt_dev)) { 4136 /* 4137 * In plug/unplug torture test with an NEC controller, 4138 * a zero-dereference was observed once due to virt_dev = 0. 4139 * Print useful debug rather than crash if it is observed again! 4140 */ 4141 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n", 4142 udev->slot_id); 4143 ret = -EINVAL; 4144 goto out; 4145 } 4146 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 4147 trace_xhci_setup_device_slot(slot_ctx); 4148 4149 if (setup == SETUP_CONTEXT_ONLY) { 4150 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) == 4151 SLOT_STATE_DEFAULT) { 4152 xhci_dbg(xhci, "Slot already in default state\n"); 4153 goto out; 4154 } 4155 } 4156 4157 command = xhci_alloc_command(xhci, true, GFP_KERNEL); 4158 if (!command) { 4159 ret = -ENOMEM; 4160 goto out; 4161 } 4162 4163 command->in_ctx = virt_dev->in_ctx; 4164 4165 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); 4166 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); 4167 if (!ctrl_ctx) { 4168 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 4169 __func__); 4170 ret = -EINVAL; 4171 goto out; 4172 } 4173 /* 4174 * If this is the first Set Address since device plug-in or 4175 * virt_device realloaction after a resume with an xHCI power loss, 4176 * then set up the slot context. 4177 */ 4178 if (!slot_ctx->dev_info) 4179 xhci_setup_addressable_virt_dev(xhci, udev); 4180 /* Otherwise, update the control endpoint ring enqueue pointer. */ 4181 else 4182 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev); 4183 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG); 4184 ctrl_ctx->drop_flags = 0; 4185 4186 trace_xhci_address_ctx(xhci, virt_dev->in_ctx, 4187 le32_to_cpu(slot_ctx->dev_info) >> 27); 4188 4189 trace_xhci_address_ctrl_ctx(ctrl_ctx); 4190 spin_lock_irqsave(&xhci->lock, flags); 4191 trace_xhci_setup_device(virt_dev); 4192 ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma, 4193 udev->slot_id, setup); 4194 if (ret) { 4195 spin_unlock_irqrestore(&xhci->lock, flags); 4196 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4197 "FIXME: allocate a command ring segment"); 4198 goto out; 4199 } 4200 xhci_ring_cmd_db(xhci); 4201 spin_unlock_irqrestore(&xhci->lock, flags); 4202 4203 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */ 4204 wait_for_completion(command->completion); 4205 4206 /* FIXME: From section 4.3.4: "Software shall be responsible for timing 4207 * the SetAddress() "recovery interval" required by USB and aborting the 4208 * command on a timeout. 4209 */ 4210 switch (command->status) { 4211 case COMP_COMMAND_ABORTED: 4212 case COMP_COMMAND_RING_STOPPED: 4213 xhci_warn(xhci, "Timeout while waiting for setup device command\n"); 4214 ret = -ETIME; 4215 break; 4216 case COMP_CONTEXT_STATE_ERROR: 4217 case COMP_SLOT_NOT_ENABLED_ERROR: 4218 xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n", 4219 act, udev->slot_id); 4220 ret = -EINVAL; 4221 break; 4222 case COMP_USB_TRANSACTION_ERROR: 4223 dev_warn(&udev->dev, "Device not responding to setup %s.\n", act); 4224 4225 mutex_unlock(&xhci->mutex); 4226 ret = xhci_disable_slot(xhci, udev->slot_id); 4227 if (!ret) 4228 xhci_alloc_dev(hcd, udev); 4229 kfree(command->completion); 4230 kfree(command); 4231 return -EPROTO; 4232 case COMP_INCOMPATIBLE_DEVICE_ERROR: 4233 dev_warn(&udev->dev, 4234 "ERROR: Incompatible device for setup %s command\n", act); 4235 ret = -ENODEV; 4236 break; 4237 case COMP_SUCCESS: 4238 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4239 "Successful setup %s command", act); 4240 break; 4241 default: 4242 xhci_err(xhci, 4243 "ERROR: unexpected setup %s command completion code 0x%x.\n", 4244 act, command->status); 4245 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1); 4246 ret = -EINVAL; 4247 break; 4248 } 4249 if (ret) 4250 goto out; 4251 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); 4252 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4253 "Op regs DCBAA ptr = %#016llx", temp_64); 4254 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4255 "Slot ID %d dcbaa entry @%p = %#016llx", 4256 udev->slot_id, 4257 &xhci->dcbaa->dev_context_ptrs[udev->slot_id], 4258 (unsigned long long) 4259 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id])); 4260 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4261 "Output Context DMA address = %#08llx", 4262 (unsigned long long)virt_dev->out_ctx->dma); 4263 trace_xhci_address_ctx(xhci, virt_dev->in_ctx, 4264 le32_to_cpu(slot_ctx->dev_info) >> 27); 4265 /* 4266 * USB core uses address 1 for the roothubs, so we add one to the 4267 * address given back to us by the HC. 4268 */ 4269 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 4270 le32_to_cpu(slot_ctx->dev_info) >> 27); 4271 /* Zero the input context control for later use */ 4272 ctrl_ctx->add_flags = 0; 4273 ctrl_ctx->drop_flags = 0; 4274 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 4275 udev->devaddr = (u8)(le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); 4276 4277 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4278 "Internal device address = %d", 4279 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); 4280 out: 4281 mutex_unlock(&xhci->mutex); 4282 if (command) { 4283 kfree(command->completion); 4284 kfree(command); 4285 } 4286 return ret; 4287 } 4288 4289 static int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev) 4290 { 4291 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ADDRESS); 4292 } 4293 4294 static int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev) 4295 { 4296 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY); 4297 } 4298 4299 /* 4300 * Transfer the port index into real index in the HW port status 4301 * registers. Caculate offset between the port's PORTSC register 4302 * and port status base. Divide the number of per port register 4303 * to get the real index. The raw port number bases 1. 4304 */ 4305 int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1) 4306 { 4307 struct xhci_hub *rhub; 4308 4309 rhub = xhci_get_rhub(hcd); 4310 return rhub->ports[port1 - 1]->hw_portnum + 1; 4311 } 4312 4313 /* 4314 * Issue an Evaluate Context command to change the Maximum Exit Latency in the 4315 * slot context. If that succeeds, store the new MEL in the xhci_virt_device. 4316 */ 4317 static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci, 4318 struct usb_device *udev, u16 max_exit_latency) 4319 { 4320 struct xhci_virt_device *virt_dev; 4321 struct xhci_command *command; 4322 struct xhci_input_control_ctx *ctrl_ctx; 4323 struct xhci_slot_ctx *slot_ctx; 4324 unsigned long flags; 4325 int ret; 4326 4327 spin_lock_irqsave(&xhci->lock, flags); 4328 4329 virt_dev = xhci->devs[udev->slot_id]; 4330 4331 /* 4332 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and 4333 * xHC was re-initialized. Exit latency will be set later after 4334 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated 4335 */ 4336 4337 if (!virt_dev || max_exit_latency == virt_dev->current_mel) { 4338 spin_unlock_irqrestore(&xhci->lock, flags); 4339 return 0; 4340 } 4341 4342 /* Attempt to issue an Evaluate Context command to change the MEL. */ 4343 command = xhci->lpm_command; 4344 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); 4345 if (!ctrl_ctx) { 4346 spin_unlock_irqrestore(&xhci->lock, flags); 4347 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 4348 __func__); 4349 return -ENOMEM; 4350 } 4351 4352 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx); 4353 spin_unlock_irqrestore(&xhci->lock, flags); 4354 4355 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); 4356 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx); 4357 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT)); 4358 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency); 4359 slot_ctx->dev_state = 0; 4360 4361 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, 4362 "Set up evaluate context for LPM MEL change."); 4363 4364 /* Issue and wait for the evaluate context command. */ 4365 ret = xhci_configure_endpoint(xhci, udev, command, 4366 true, true); 4367 4368 if (!ret) { 4369 spin_lock_irqsave(&xhci->lock, flags); 4370 virt_dev->current_mel = max_exit_latency; 4371 spin_unlock_irqrestore(&xhci->lock, flags); 4372 } 4373 return ret; 4374 } 4375 4376 #ifdef CONFIG_PM 4377 4378 /* BESL to HIRD Encoding array for USB2 LPM */ 4379 static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000, 4380 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000}; 4381 4382 /* Calculate HIRD/BESL for USB2 PORTPMSC*/ 4383 static int xhci_calculate_hird_besl(struct xhci_hcd *xhci, 4384 struct usb_device *udev) 4385 { 4386 int u2del, besl, besl_host; 4387 int besl_device = 0; 4388 u32 field; 4389 4390 u2del = HCS_U2_LATENCY(xhci->hcs_params3); 4391 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); 4392 4393 if (field & USB_BESL_SUPPORT) { 4394 for (besl_host = 0; besl_host < 16; besl_host++) { 4395 if (xhci_besl_encoding[besl_host] >= u2del) 4396 break; 4397 } 4398 /* Use baseline BESL value as default */ 4399 if (field & USB_BESL_BASELINE_VALID) 4400 besl_device = USB_GET_BESL_BASELINE(field); 4401 else if (field & USB_BESL_DEEP_VALID) 4402 besl_device = USB_GET_BESL_DEEP(field); 4403 } else { 4404 if (u2del <= 50) 4405 besl_host = 0; 4406 else 4407 besl_host = (u2del - 51) / 75 + 1; 4408 } 4409 4410 besl = besl_host + besl_device; 4411 if (besl > 15) 4412 besl = 15; 4413 4414 return besl; 4415 } 4416 4417 /* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */ 4418 static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev) 4419 { 4420 u32 field; 4421 int l1; 4422 int besld = 0; 4423 int hirdm = 0; 4424 4425 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); 4426 4427 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */ 4428 l1 = udev->l1_params.timeout / 256; 4429 4430 /* device has preferred BESLD */ 4431 if (field & USB_BESL_DEEP_VALID) { 4432 besld = USB_GET_BESL_DEEP(field); 4433 hirdm = 1; 4434 } 4435 4436 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm); 4437 } 4438 4439 static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd, 4440 struct usb_device *udev, int enable) 4441 { 4442 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 4443 struct xhci_port **ports; 4444 __le32 __iomem *pm_addr, *hlpm_addr; 4445 u32 pm_val, hlpm_val, field; 4446 unsigned int port_num; 4447 unsigned long flags; 4448 int hird, exit_latency; 4449 int ret; 4450 4451 if (xhci->quirks & XHCI_HW_LPM_DISABLE) 4452 return -EPERM; 4453 4454 if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support || 4455 !udev->lpm_capable) 4456 return -EPERM; 4457 4458 if (!udev->parent || udev->parent->parent || 4459 udev->descriptor.bDeviceClass == USB_CLASS_HUB) 4460 return -EPERM; 4461 4462 if (udev->usb2_hw_lpm_capable != 1) 4463 return -EPERM; 4464 4465 spin_lock_irqsave(&xhci->lock, flags); 4466 4467 ports = xhci->usb2_rhub.ports; 4468 port_num = udev->portnum - 1; 4469 pm_addr = ports[port_num]->addr + PORTPMSC; 4470 pm_val = readl(pm_addr); 4471 hlpm_addr = ports[port_num]->addr + PORTHLPMC; 4472 4473 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n", 4474 enable ? "enable" : "disable", port_num + 1); 4475 4476 if (enable) { 4477 /* Host supports BESL timeout instead of HIRD */ 4478 if (udev->usb2_hw_lpm_besl_capable) { 4479 /* if device doesn't have a preferred BESL value use a 4480 * default one which works with mixed HIRD and BESL 4481 * systems. See XHCI_DEFAULT_BESL definition in xhci.h 4482 */ 4483 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); 4484 if ((field & USB_BESL_SUPPORT) && 4485 (field & USB_BESL_BASELINE_VALID)) 4486 hird = USB_GET_BESL_BASELINE(field); 4487 else 4488 hird = udev->l1_params.besl; 4489 4490 exit_latency = xhci_besl_encoding[hird]; 4491 spin_unlock_irqrestore(&xhci->lock, flags); 4492 4493 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx 4494 * input context for link powermanagement evaluate 4495 * context commands. It is protected by hcd->bandwidth 4496 * mutex and is shared by all devices. We need to set 4497 * the max ext latency in USB 2 BESL LPM as well, so 4498 * use the same mutex and xhci_change_max_exit_latency() 4499 */ 4500 mutex_lock(hcd->bandwidth_mutex); 4501 ret = xhci_change_max_exit_latency(xhci, udev, 4502 exit_latency); 4503 mutex_unlock(hcd->bandwidth_mutex); 4504 4505 if (ret < 0) 4506 return ret; 4507 spin_lock_irqsave(&xhci->lock, flags); 4508 4509 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev); 4510 writel(hlpm_val, hlpm_addr); 4511 /* flush write */ 4512 readl(hlpm_addr); 4513 } else { 4514 hird = xhci_calculate_hird_besl(xhci, udev); 4515 } 4516 4517 pm_val &= ~PORT_HIRD_MASK; 4518 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id); 4519 writel(pm_val, pm_addr); 4520 pm_val = readl(pm_addr); 4521 pm_val |= PORT_HLE; 4522 writel(pm_val, pm_addr); 4523 /* flush write */ 4524 readl(pm_addr); 4525 } else { 4526 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK); 4527 writel(pm_val, pm_addr); 4528 /* flush write */ 4529 readl(pm_addr); 4530 if (udev->usb2_hw_lpm_besl_capable) { 4531 spin_unlock_irqrestore(&xhci->lock, flags); 4532 mutex_lock(hcd->bandwidth_mutex); 4533 xhci_change_max_exit_latency(xhci, udev, 0); 4534 mutex_unlock(hcd->bandwidth_mutex); 4535 readl_poll_timeout(ports[port_num]->addr, pm_val, 4536 (pm_val & PORT_PLS_MASK) == XDEV_U0, 4537 100, 10000); 4538 return 0; 4539 } 4540 } 4541 4542 spin_unlock_irqrestore(&xhci->lock, flags); 4543 return 0; 4544 } 4545 4546 /* check if a usb2 port supports a given extened capability protocol 4547 * only USB2 ports extended protocol capability values are cached. 4548 * Return 1 if capability is supported 4549 */ 4550 static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port, 4551 unsigned capability) 4552 { 4553 u32 port_offset, port_count; 4554 int i; 4555 4556 for (i = 0; i < xhci->num_ext_caps; i++) { 4557 if (xhci->ext_caps[i] & capability) { 4558 /* port offsets starts at 1 */ 4559 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1; 4560 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]); 4561 if (port >= port_offset && 4562 port < port_offset + port_count) 4563 return 1; 4564 } 4565 } 4566 return 0; 4567 } 4568 4569 static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev) 4570 { 4571 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 4572 int portnum = udev->portnum - 1; 4573 4574 if (hcd->speed >= HCD_USB3 || !udev->lpm_capable) 4575 return 0; 4576 4577 /* we only support lpm for non-hub device connected to root hub yet */ 4578 if (!udev->parent || udev->parent->parent || 4579 udev->descriptor.bDeviceClass == USB_CLASS_HUB) 4580 return 0; 4581 4582 if (xhci->hw_lpm_support == 1 && 4583 xhci_check_usb2_port_capability( 4584 xhci, portnum, XHCI_HLC)) { 4585 udev->usb2_hw_lpm_capable = 1; 4586 udev->l1_params.timeout = XHCI_L1_TIMEOUT; 4587 udev->l1_params.besl = XHCI_DEFAULT_BESL; 4588 if (xhci_check_usb2_port_capability(xhci, portnum, 4589 XHCI_BLC)) 4590 udev->usb2_hw_lpm_besl_capable = 1; 4591 } 4592 4593 return 0; 4594 } 4595 4596 /*---------------------- USB 3.0 Link PM functions ------------------------*/ 4597 4598 /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */ 4599 static unsigned long long xhci_service_interval_to_ns( 4600 struct usb_endpoint_descriptor *desc) 4601 { 4602 return (1ULL << (desc->bInterval - 1)) * 125 * 1000; 4603 } 4604 4605 static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev, 4606 enum usb3_link_state state) 4607 { 4608 unsigned long long sel; 4609 unsigned long long pel; 4610 unsigned int max_sel_pel; 4611 char *state_name; 4612 4613 switch (state) { 4614 case USB3_LPM_U1: 4615 /* Convert SEL and PEL stored in nanoseconds to microseconds */ 4616 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 4617 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); 4618 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL; 4619 state_name = "U1"; 4620 break; 4621 case USB3_LPM_U2: 4622 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 4623 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); 4624 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL; 4625 state_name = "U2"; 4626 break; 4627 default: 4628 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n", 4629 __func__); 4630 return USB3_LPM_DISABLED; 4631 } 4632 4633 if (sel <= max_sel_pel && pel <= max_sel_pel) 4634 return USB3_LPM_DEVICE_INITIATED; 4635 4636 if (sel > max_sel_pel) 4637 dev_dbg(&udev->dev, "Device-initiated %s disabled " 4638 "due to long SEL %llu ms\n", 4639 state_name, sel); 4640 else 4641 dev_dbg(&udev->dev, "Device-initiated %s disabled " 4642 "due to long PEL %llu ms\n", 4643 state_name, pel); 4644 return USB3_LPM_DISABLED; 4645 } 4646 4647 /* The U1 timeout should be the maximum of the following values: 4648 * - For control endpoints, U1 system exit latency (SEL) * 3 4649 * - For bulk endpoints, U1 SEL * 5 4650 * - For interrupt endpoints: 4651 * - Notification EPs, U1 SEL * 3 4652 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2) 4653 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2) 4654 */ 4655 static unsigned long long xhci_calculate_intel_u1_timeout( 4656 struct usb_device *udev, 4657 struct usb_endpoint_descriptor *desc) 4658 { 4659 unsigned long long timeout_ns; 4660 int ep_type; 4661 int intr_type; 4662 4663 ep_type = usb_endpoint_type(desc); 4664 switch (ep_type) { 4665 case USB_ENDPOINT_XFER_CONTROL: 4666 timeout_ns = udev->u1_params.sel * 3; 4667 break; 4668 case USB_ENDPOINT_XFER_BULK: 4669 timeout_ns = udev->u1_params.sel * 5; 4670 break; 4671 case USB_ENDPOINT_XFER_INT: 4672 intr_type = usb_endpoint_interrupt_type(desc); 4673 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) { 4674 timeout_ns = udev->u1_params.sel * 3; 4675 break; 4676 } 4677 /* Otherwise the calculation is the same as isoc eps */ 4678 fallthrough; 4679 case USB_ENDPOINT_XFER_ISOC: 4680 timeout_ns = xhci_service_interval_to_ns(desc); 4681 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100); 4682 if (timeout_ns < udev->u1_params.sel * 2) 4683 timeout_ns = udev->u1_params.sel * 2; 4684 break; 4685 default: 4686 return 0; 4687 } 4688 4689 return timeout_ns; 4690 } 4691 4692 /* Returns the hub-encoded U1 timeout value. */ 4693 static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci, 4694 struct usb_device *udev, 4695 struct usb_endpoint_descriptor *desc) 4696 { 4697 unsigned long long timeout_ns; 4698 4699 if (xhci->quirks & XHCI_INTEL_HOST) 4700 timeout_ns = xhci_calculate_intel_u1_timeout(udev, desc); 4701 else 4702 timeout_ns = udev->u1_params.sel; 4703 4704 /* Prevent U1 if service interval is shorter than U1 exit latency */ 4705 if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) { 4706 if (xhci_service_interval_to_ns(desc) <= timeout_ns) { 4707 dev_dbg(&udev->dev, "Disable U1, ESIT shorter than exit latency\n"); 4708 return USB3_LPM_DISABLED; 4709 } 4710 } 4711 4712 /* The U1 timeout is encoded in 1us intervals. 4713 * Don't return a timeout of zero, because that's USB3_LPM_DISABLED. 4714 */ 4715 if (timeout_ns == USB3_LPM_DISABLED) 4716 timeout_ns = 1; 4717 else 4718 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000); 4719 4720 /* If the necessary timeout value is bigger than what we can set in the 4721 * USB 3.0 hub, we have to disable hub-initiated U1. 4722 */ 4723 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT) 4724 return timeout_ns; 4725 dev_dbg(&udev->dev, "Hub-initiated U1 disabled " 4726 "due to long timeout %llu ms\n", timeout_ns); 4727 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1); 4728 } 4729 4730 /* The U2 timeout should be the maximum of: 4731 * - 10 ms (to avoid the bandwidth impact on the scheduler) 4732 * - largest bInterval of any active periodic endpoint (to avoid going 4733 * into lower power link states between intervals). 4734 * - the U2 Exit Latency of the device 4735 */ 4736 static unsigned long long xhci_calculate_intel_u2_timeout( 4737 struct usb_device *udev, 4738 struct usb_endpoint_descriptor *desc) 4739 { 4740 unsigned long long timeout_ns; 4741 unsigned long long u2_del_ns; 4742 4743 timeout_ns = 10 * 1000 * 1000; 4744 4745 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) && 4746 (xhci_service_interval_to_ns(desc) > timeout_ns)) 4747 timeout_ns = xhci_service_interval_to_ns(desc); 4748 4749 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL; 4750 if (u2_del_ns > timeout_ns) 4751 timeout_ns = u2_del_ns; 4752 4753 return timeout_ns; 4754 } 4755 4756 /* Returns the hub-encoded U2 timeout value. */ 4757 static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci, 4758 struct usb_device *udev, 4759 struct usb_endpoint_descriptor *desc) 4760 { 4761 unsigned long long timeout_ns; 4762 4763 if (xhci->quirks & XHCI_INTEL_HOST) 4764 timeout_ns = xhci_calculate_intel_u2_timeout(udev, desc); 4765 else 4766 timeout_ns = udev->u2_params.sel; 4767 4768 /* Prevent U2 if service interval is shorter than U2 exit latency */ 4769 if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) { 4770 if (xhci_service_interval_to_ns(desc) <= timeout_ns) { 4771 dev_dbg(&udev->dev, "Disable U2, ESIT shorter than exit latency\n"); 4772 return USB3_LPM_DISABLED; 4773 } 4774 } 4775 4776 /* The U2 timeout is encoded in 256us intervals */ 4777 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000); 4778 /* If the necessary timeout value is bigger than what we can set in the 4779 * USB 3.0 hub, we have to disable hub-initiated U2. 4780 */ 4781 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT) 4782 return timeout_ns; 4783 dev_dbg(&udev->dev, "Hub-initiated U2 disabled " 4784 "due to long timeout %llu ms\n", timeout_ns); 4785 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2); 4786 } 4787 4788 static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci, 4789 struct usb_device *udev, 4790 struct usb_endpoint_descriptor *desc, 4791 enum usb3_link_state state, 4792 u16 *timeout) 4793 { 4794 if (state == USB3_LPM_U1) 4795 return xhci_calculate_u1_timeout(xhci, udev, desc); 4796 else if (state == USB3_LPM_U2) 4797 return xhci_calculate_u2_timeout(xhci, udev, desc); 4798 4799 return USB3_LPM_DISABLED; 4800 } 4801 4802 static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci, 4803 struct usb_device *udev, 4804 struct usb_endpoint_descriptor *desc, 4805 enum usb3_link_state state, 4806 u16 *timeout) 4807 { 4808 u16 alt_timeout; 4809 4810 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev, 4811 desc, state, timeout); 4812 4813 /* If we found we can't enable hub-initiated LPM, and 4814 * the U1 or U2 exit latency was too high to allow 4815 * device-initiated LPM as well, then we will disable LPM 4816 * for this device, so stop searching any further. 4817 */ 4818 if (alt_timeout == USB3_LPM_DISABLED) { 4819 *timeout = alt_timeout; 4820 return -E2BIG; 4821 } 4822 if (alt_timeout > *timeout) 4823 *timeout = alt_timeout; 4824 return 0; 4825 } 4826 4827 static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci, 4828 struct usb_device *udev, 4829 struct usb_host_interface *alt, 4830 enum usb3_link_state state, 4831 u16 *timeout) 4832 { 4833 int j; 4834 4835 for (j = 0; j < alt->desc.bNumEndpoints; j++) { 4836 if (xhci_update_timeout_for_endpoint(xhci, udev, 4837 &alt->endpoint[j].desc, state, timeout)) 4838 return -E2BIG; 4839 continue; 4840 } 4841 return 0; 4842 } 4843 4844 static int xhci_check_intel_tier_policy(struct usb_device *udev, 4845 enum usb3_link_state state) 4846 { 4847 struct usb_device *parent; 4848 unsigned int num_hubs; 4849 4850 if (state == USB3_LPM_U2) 4851 return 0; 4852 4853 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */ 4854 for (parent = udev->parent, num_hubs = 0; parent->parent; 4855 parent = parent->parent) 4856 num_hubs++; 4857 4858 if (num_hubs < 2) 4859 return 0; 4860 4861 dev_dbg(&udev->dev, "Disabling U1 link state for device" 4862 " below second-tier hub.\n"); 4863 dev_dbg(&udev->dev, "Plug device into first-tier hub " 4864 "to decrease power consumption.\n"); 4865 return -E2BIG; 4866 } 4867 4868 static int xhci_check_tier_policy(struct xhci_hcd *xhci, 4869 struct usb_device *udev, 4870 enum usb3_link_state state) 4871 { 4872 if (xhci->quirks & XHCI_INTEL_HOST) 4873 return xhci_check_intel_tier_policy(udev, state); 4874 else 4875 return 0; 4876 } 4877 4878 /* Returns the U1 or U2 timeout that should be enabled. 4879 * If the tier check or timeout setting functions return with a non-zero exit 4880 * code, that means the timeout value has been finalized and we shouldn't look 4881 * at any more endpoints. 4882 */ 4883 static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd, 4884 struct usb_device *udev, enum usb3_link_state state) 4885 { 4886 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 4887 struct usb_host_config *config; 4888 char *state_name; 4889 int i; 4890 u16 timeout = USB3_LPM_DISABLED; 4891 4892 if (state == USB3_LPM_U1) 4893 state_name = "U1"; 4894 else if (state == USB3_LPM_U2) 4895 state_name = "U2"; 4896 else { 4897 dev_warn(&udev->dev, "Can't enable unknown link state %i\n", 4898 state); 4899 return timeout; 4900 } 4901 4902 if (xhci_check_tier_policy(xhci, udev, state) < 0) 4903 return timeout; 4904 4905 /* Gather some information about the currently installed configuration 4906 * and alternate interface settings. 4907 */ 4908 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc, 4909 state, &timeout)) 4910 return timeout; 4911 4912 config = udev->actconfig; 4913 if (!config) 4914 return timeout; 4915 4916 for (i = 0; i < config->desc.bNumInterfaces; i++) { 4917 struct usb_driver *driver; 4918 struct usb_interface *intf = config->interface[i]; 4919 4920 if (!intf) 4921 continue; 4922 4923 /* Check if any currently bound drivers want hub-initiated LPM 4924 * disabled. 4925 */ 4926 if (intf->dev.driver) { 4927 driver = to_usb_driver(intf->dev.driver); 4928 if (driver && driver->disable_hub_initiated_lpm) { 4929 dev_dbg(&udev->dev, "Hub-initiated %s disabled at request of driver %s\n", 4930 state_name, driver->name); 4931 timeout = xhci_get_timeout_no_hub_lpm(udev, 4932 state); 4933 if (timeout == USB3_LPM_DISABLED) 4934 return timeout; 4935 } 4936 } 4937 4938 /* Not sure how this could happen... */ 4939 if (!intf->cur_altsetting) 4940 continue; 4941 4942 if (xhci_update_timeout_for_interface(xhci, udev, 4943 intf->cur_altsetting, 4944 state, &timeout)) 4945 return timeout; 4946 } 4947 return timeout; 4948 } 4949 4950 static int calculate_max_exit_latency(struct usb_device *udev, 4951 enum usb3_link_state state_changed, 4952 u16 hub_encoded_timeout) 4953 { 4954 unsigned long long u1_mel_us = 0; 4955 unsigned long long u2_mel_us = 0; 4956 unsigned long long mel_us = 0; 4957 bool disabling_u1; 4958 bool disabling_u2; 4959 bool enabling_u1; 4960 bool enabling_u2; 4961 4962 disabling_u1 = (state_changed == USB3_LPM_U1 && 4963 hub_encoded_timeout == USB3_LPM_DISABLED); 4964 disabling_u2 = (state_changed == USB3_LPM_U2 && 4965 hub_encoded_timeout == USB3_LPM_DISABLED); 4966 4967 enabling_u1 = (state_changed == USB3_LPM_U1 && 4968 hub_encoded_timeout != USB3_LPM_DISABLED); 4969 enabling_u2 = (state_changed == USB3_LPM_U2 && 4970 hub_encoded_timeout != USB3_LPM_DISABLED); 4971 4972 /* If U1 was already enabled and we're not disabling it, 4973 * or we're going to enable U1, account for the U1 max exit latency. 4974 */ 4975 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) || 4976 enabling_u1) 4977 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000); 4978 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) || 4979 enabling_u2) 4980 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000); 4981 4982 if (u1_mel_us > u2_mel_us) 4983 mel_us = u1_mel_us; 4984 else 4985 mel_us = u2_mel_us; 4986 /* xHCI host controller max exit latency field is only 16 bits wide. */ 4987 if (mel_us > MAX_EXIT) { 4988 dev_warn(&udev->dev, "Link PM max exit latency of %lluus " 4989 "is too big.\n", mel_us); 4990 return -E2BIG; 4991 } 4992 return mel_us; 4993 } 4994 4995 /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */ 4996 static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd, 4997 struct usb_device *udev, enum usb3_link_state state) 4998 { 4999 struct xhci_hcd *xhci; 5000 u16 hub_encoded_timeout; 5001 int mel; 5002 int ret; 5003 5004 xhci = hcd_to_xhci(hcd); 5005 /* The LPM timeout values are pretty host-controller specific, so don't 5006 * enable hub-initiated timeouts unless the vendor has provided 5007 * information about their timeout algorithm. 5008 */ 5009 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) || 5010 !xhci->devs[udev->slot_id]) 5011 return USB3_LPM_DISABLED; 5012 5013 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state); 5014 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout); 5015 if (mel < 0) { 5016 /* Max Exit Latency is too big, disable LPM. */ 5017 hub_encoded_timeout = USB3_LPM_DISABLED; 5018 mel = 0; 5019 } 5020 5021 ret = xhci_change_max_exit_latency(xhci, udev, mel); 5022 if (ret) 5023 return ret; 5024 return hub_encoded_timeout; 5025 } 5026 5027 static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd, 5028 struct usb_device *udev, enum usb3_link_state state) 5029 { 5030 struct xhci_hcd *xhci; 5031 u16 mel; 5032 5033 xhci = hcd_to_xhci(hcd); 5034 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) || 5035 !xhci->devs[udev->slot_id]) 5036 return 0; 5037 5038 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED); 5039 return xhci_change_max_exit_latency(xhci, udev, mel); 5040 } 5041 #else /* CONFIG_PM */ 5042 5043 static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd, 5044 struct usb_device *udev, int enable) 5045 { 5046 return 0; 5047 } 5048 5049 static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev) 5050 { 5051 return 0; 5052 } 5053 5054 static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd, 5055 struct usb_device *udev, enum usb3_link_state state) 5056 { 5057 return USB3_LPM_DISABLED; 5058 } 5059 5060 static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd, 5061 struct usb_device *udev, enum usb3_link_state state) 5062 { 5063 return 0; 5064 } 5065 #endif /* CONFIG_PM */ 5066 5067 /*-------------------------------------------------------------------------*/ 5068 5069 /* Once a hub descriptor is fetched for a device, we need to update the xHC's 5070 * internal data structures for the device. 5071 */ 5072 static int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev, 5073 struct usb_tt *tt, gfp_t mem_flags) 5074 { 5075 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 5076 struct xhci_virt_device *vdev; 5077 struct xhci_command *config_cmd; 5078 struct xhci_input_control_ctx *ctrl_ctx; 5079 struct xhci_slot_ctx *slot_ctx; 5080 unsigned long flags; 5081 unsigned think_time; 5082 int ret; 5083 5084 /* Ignore root hubs */ 5085 if (!hdev->parent) 5086 return 0; 5087 5088 vdev = xhci->devs[hdev->slot_id]; 5089 if (!vdev) { 5090 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n"); 5091 return -EINVAL; 5092 } 5093 5094 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags); 5095 if (!config_cmd) 5096 return -ENOMEM; 5097 5098 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx); 5099 if (!ctrl_ctx) { 5100 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 5101 __func__); 5102 xhci_free_command(xhci, config_cmd); 5103 return -ENOMEM; 5104 } 5105 5106 spin_lock_irqsave(&xhci->lock, flags); 5107 if (hdev->speed == USB_SPEED_HIGH && 5108 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) { 5109 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n"); 5110 xhci_free_command(xhci, config_cmd); 5111 spin_unlock_irqrestore(&xhci->lock, flags); 5112 return -ENOMEM; 5113 } 5114 5115 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx); 5116 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); 5117 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx); 5118 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB); 5119 /* 5120 * refer to section 6.2.2: MTT should be 0 for full speed hub, 5121 * but it may be already set to 1 when setup an xHCI virtual 5122 * device, so clear it anyway. 5123 */ 5124 if (tt->multi) 5125 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); 5126 else if (hdev->speed == USB_SPEED_FULL) 5127 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT); 5128 5129 if (xhci->hci_version > 0x95) { 5130 xhci_dbg(xhci, "xHCI version %x needs hub " 5131 "TT think time and number of ports\n", 5132 (unsigned int) xhci->hci_version); 5133 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild)); 5134 /* Set TT think time - convert from ns to FS bit times. 5135 * 0 = 8 FS bit times, 1 = 16 FS bit times, 5136 * 2 = 24 FS bit times, 3 = 32 FS bit times. 5137 * 5138 * xHCI 1.0: this field shall be 0 if the device is not a 5139 * High-spped hub. 5140 */ 5141 think_time = tt->think_time; 5142 if (think_time != 0) 5143 think_time = (think_time / 666) - 1; 5144 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH) 5145 slot_ctx->tt_info |= 5146 cpu_to_le32(TT_THINK_TIME(think_time)); 5147 } else { 5148 xhci_dbg(xhci, "xHCI version %x doesn't need hub " 5149 "TT think time or number of ports\n", 5150 (unsigned int) xhci->hci_version); 5151 } 5152 slot_ctx->dev_state = 0; 5153 spin_unlock_irqrestore(&xhci->lock, flags); 5154 5155 xhci_dbg(xhci, "Set up %s for hub device.\n", 5156 (xhci->hci_version > 0x95) ? 5157 "configure endpoint" : "evaluate context"); 5158 5159 /* Issue and wait for the configure endpoint or 5160 * evaluate context command. 5161 */ 5162 if (xhci->hci_version > 0x95) 5163 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, 5164 false, false); 5165 else 5166 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, 5167 true, false); 5168 5169 xhci_free_command(xhci, config_cmd); 5170 return ret; 5171 } 5172 5173 static int xhci_get_frame(struct usb_hcd *hcd) 5174 { 5175 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 5176 /* EHCI mods by the periodic size. Why? */ 5177 return readl(&xhci->run_regs->microframe_index) >> 3; 5178 } 5179 5180 int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks) 5181 { 5182 struct xhci_hcd *xhci; 5183 /* 5184 * TODO: Check with DWC3 clients for sysdev according to 5185 * quirks 5186 */ 5187 struct device *dev = hcd->self.sysdev; 5188 unsigned int minor_rev; 5189 int retval; 5190 5191 /* Accept arbitrarily long scatter-gather lists */ 5192 hcd->self.sg_tablesize = ~0; 5193 5194 /* support to build packet from discontinuous buffers */ 5195 hcd->self.no_sg_constraint = 1; 5196 5197 /* XHCI controllers don't stop the ep queue on short packets :| */ 5198 hcd->self.no_stop_on_short = 1; 5199 5200 xhci = hcd_to_xhci(hcd); 5201 5202 if (usb_hcd_is_primary_hcd(hcd)) { 5203 xhci->main_hcd = hcd; 5204 xhci->usb2_rhub.hcd = hcd; 5205 /* Mark the first roothub as being USB 2.0. 5206 * The xHCI driver will register the USB 3.0 roothub. 5207 */ 5208 hcd->speed = HCD_USB2; 5209 hcd->self.root_hub->speed = USB_SPEED_HIGH; 5210 /* 5211 * USB 2.0 roothub under xHCI has an integrated TT, 5212 * (rate matching hub) as opposed to having an OHCI/UHCI 5213 * companion controller. 5214 */ 5215 hcd->has_tt = 1; 5216 } else { 5217 /* 5218 * Early xHCI 1.1 spec did not mention USB 3.1 capable hosts 5219 * should return 0x31 for sbrn, or that the minor revision 5220 * is a two digit BCD containig minor and sub-minor numbers. 5221 * This was later clarified in xHCI 1.2. 5222 * 5223 * Some USB 3.1 capable hosts therefore have sbrn 0x30, and 5224 * minor revision set to 0x1 instead of 0x10. 5225 */ 5226 if (xhci->usb3_rhub.min_rev == 0x1) 5227 minor_rev = 1; 5228 else 5229 minor_rev = xhci->usb3_rhub.min_rev / 0x10; 5230 5231 switch (minor_rev) { 5232 case 2: 5233 hcd->speed = HCD_USB32; 5234 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS; 5235 hcd->self.root_hub->rx_lanes = 2; 5236 hcd->self.root_hub->tx_lanes = 2; 5237 break; 5238 case 1: 5239 hcd->speed = HCD_USB31; 5240 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS; 5241 break; 5242 } 5243 xhci_info(xhci, "Host supports USB 3.%x %sSuperSpeed\n", 5244 minor_rev, 5245 minor_rev ? "Enhanced " : ""); 5246 5247 xhci->usb3_rhub.hcd = hcd; 5248 /* xHCI private pointer was set in xhci_pci_probe for the second 5249 * registered roothub. 5250 */ 5251 return 0; 5252 } 5253 5254 mutex_init(&xhci->mutex); 5255 xhci->cap_regs = hcd->regs; 5256 xhci->op_regs = hcd->regs + 5257 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)); 5258 xhci->run_regs = hcd->regs + 5259 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK); 5260 /* Cache read-only capability registers */ 5261 xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1); 5262 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2); 5263 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3); 5264 xhci->hcc_params = readl(&xhci->cap_regs->hc_capbase); 5265 xhci->hci_version = HC_VERSION(xhci->hcc_params); 5266 xhci->hcc_params = readl(&xhci->cap_regs->hcc_params); 5267 if (xhci->hci_version > 0x100) 5268 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2); 5269 5270 xhci->quirks |= quirks; 5271 5272 get_quirks(dev, xhci); 5273 5274 /* In xhci controllers which follow xhci 1.0 spec gives a spurious 5275 * success event after a short transfer. This quirk will ignore such 5276 * spurious event. 5277 */ 5278 if (xhci->hci_version > 0x96) 5279 xhci->quirks |= XHCI_SPURIOUS_SUCCESS; 5280 5281 /* Make sure the HC is halted. */ 5282 retval = xhci_halt(xhci); 5283 if (retval) 5284 return retval; 5285 5286 xhci_zero_64b_regs(xhci); 5287 5288 xhci_dbg(xhci, "Resetting HCD\n"); 5289 /* Reset the internal HC memory state and registers. */ 5290 retval = xhci_reset(xhci); 5291 if (retval) 5292 return retval; 5293 xhci_dbg(xhci, "Reset complete\n"); 5294 5295 /* 5296 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0) 5297 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit 5298 * address memory pointers actually. So, this driver clears the AC64 5299 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev, 5300 * DMA_BIT_MASK(32)) in this xhci_gen_setup(). 5301 */ 5302 if (xhci->quirks & XHCI_NO_64BIT_SUPPORT) 5303 xhci->hcc_params &= ~BIT(0); 5304 5305 /* Set dma_mask and coherent_dma_mask to 64-bits, 5306 * if xHC supports 64-bit addressing */ 5307 if (HCC_64BIT_ADDR(xhci->hcc_params) && 5308 !dma_set_mask(dev, DMA_BIT_MASK(64))) { 5309 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n"); 5310 dma_set_coherent_mask(dev, DMA_BIT_MASK(64)); 5311 } else { 5312 /* 5313 * This is to avoid error in cases where a 32-bit USB 5314 * controller is used on a 64-bit capable system. 5315 */ 5316 retval = dma_set_mask(dev, DMA_BIT_MASK(32)); 5317 if (retval) 5318 return retval; 5319 xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n"); 5320 dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); 5321 } 5322 5323 xhci_dbg(xhci, "Calling HCD init\n"); 5324 /* Initialize HCD and host controller data structures. */ 5325 retval = xhci_init(hcd); 5326 if (retval) 5327 return retval; 5328 xhci_dbg(xhci, "Called HCD init\n"); 5329 5330 xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%016llx\n", 5331 xhci->hcc_params, xhci->hci_version, xhci->quirks); 5332 5333 return 0; 5334 } 5335 EXPORT_SYMBOL_GPL(xhci_gen_setup); 5336 5337 static void xhci_clear_tt_buffer_complete(struct usb_hcd *hcd, 5338 struct usb_host_endpoint *ep) 5339 { 5340 struct xhci_hcd *xhci; 5341 struct usb_device *udev; 5342 unsigned int slot_id; 5343 unsigned int ep_index; 5344 unsigned long flags; 5345 5346 xhci = hcd_to_xhci(hcd); 5347 5348 spin_lock_irqsave(&xhci->lock, flags); 5349 udev = (struct usb_device *)ep->hcpriv; 5350 slot_id = udev->slot_id; 5351 ep_index = xhci_get_endpoint_index(&ep->desc); 5352 5353 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_CLEARING_TT; 5354 xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index); 5355 spin_unlock_irqrestore(&xhci->lock, flags); 5356 } 5357 5358 static const struct hc_driver xhci_hc_driver = { 5359 .description = "xhci-hcd", 5360 .product_desc = "xHCI Host Controller", 5361 .hcd_priv_size = sizeof(struct xhci_hcd), 5362 5363 /* 5364 * generic hardware linkage 5365 */ 5366 .irq = xhci_irq, 5367 .flags = HCD_MEMORY | HCD_DMA | HCD_USB3 | HCD_SHARED | 5368 HCD_BH, 5369 5370 /* 5371 * basic lifecycle operations 5372 */ 5373 .reset = NULL, /* set in xhci_init_driver() */ 5374 .start = xhci_run, 5375 .stop = xhci_stop, 5376 .shutdown = xhci_shutdown, 5377 5378 /* 5379 * managing i/o requests and associated device resources 5380 */ 5381 .map_urb_for_dma = xhci_map_urb_for_dma, 5382 .unmap_urb_for_dma = xhci_unmap_urb_for_dma, 5383 .urb_enqueue = xhci_urb_enqueue, 5384 .urb_dequeue = xhci_urb_dequeue, 5385 .alloc_dev = xhci_alloc_dev, 5386 .free_dev = xhci_free_dev, 5387 .alloc_streams = xhci_alloc_streams, 5388 .free_streams = xhci_free_streams, 5389 .add_endpoint = xhci_add_endpoint, 5390 .drop_endpoint = xhci_drop_endpoint, 5391 .endpoint_disable = xhci_endpoint_disable, 5392 .endpoint_reset = xhci_endpoint_reset, 5393 .check_bandwidth = xhci_check_bandwidth, 5394 .reset_bandwidth = xhci_reset_bandwidth, 5395 .address_device = xhci_address_device, 5396 .enable_device = xhci_enable_device, 5397 .update_hub_device = xhci_update_hub_device, 5398 .reset_device = xhci_discover_or_reset_device, 5399 5400 /* 5401 * scheduling support 5402 */ 5403 .get_frame_number = xhci_get_frame, 5404 5405 /* 5406 * root hub support 5407 */ 5408 .hub_control = xhci_hub_control, 5409 .hub_status_data = xhci_hub_status_data, 5410 .bus_suspend = xhci_bus_suspend, 5411 .bus_resume = xhci_bus_resume, 5412 .get_resuming_ports = xhci_get_resuming_ports, 5413 5414 /* 5415 * call back when device connected and addressed 5416 */ 5417 .update_device = xhci_update_device, 5418 .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm, 5419 .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout, 5420 .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout, 5421 .find_raw_port_number = xhci_find_raw_port_number, 5422 .clear_tt_buffer_complete = xhci_clear_tt_buffer_complete, 5423 }; 5424 5425 void xhci_init_driver(struct hc_driver *drv, 5426 const struct xhci_driver_overrides *over) 5427 { 5428 BUG_ON(!over); 5429 5430 /* Copy the generic table to drv then apply the overrides */ 5431 *drv = xhci_hc_driver; 5432 5433 if (over) { 5434 drv->hcd_priv_size += over->extra_priv_size; 5435 if (over->reset) 5436 drv->reset = over->reset; 5437 if (over->start) 5438 drv->start = over->start; 5439 if (over->check_bandwidth) 5440 drv->check_bandwidth = over->check_bandwidth; 5441 if (over->reset_bandwidth) 5442 drv->reset_bandwidth = over->reset_bandwidth; 5443 } 5444 } 5445 EXPORT_SYMBOL_GPL(xhci_init_driver); 5446 5447 MODULE_DESCRIPTION(DRIVER_DESC); 5448 MODULE_AUTHOR(DRIVER_AUTHOR); 5449 MODULE_LICENSE("GPL"); 5450 5451 static int __init xhci_hcd_init(void) 5452 { 5453 /* 5454 * Check the compiler generated sizes of structures that must be laid 5455 * out in specific ways for hardware access. 5456 */ 5457 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8); 5458 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8); 5459 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8); 5460 /* xhci_device_control has eight fields, and also 5461 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx 5462 */ 5463 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8); 5464 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8); 5465 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8); 5466 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 8*32/8); 5467 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8); 5468 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */ 5469 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8); 5470 5471 if (usb_disabled()) 5472 return -ENODEV; 5473 5474 xhci_debugfs_create_root(); 5475 5476 return 0; 5477 } 5478 5479 /* 5480 * If an init function is provided, an exit function must also be provided 5481 * to allow module unload. 5482 */ 5483 static void __exit xhci_hcd_fini(void) 5484 { 5485 xhci_debugfs_remove_root(); 5486 } 5487 5488 module_init(xhci_hcd_init); 5489 module_exit(xhci_hcd_fini); 5490