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