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