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