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