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 unsigned int ep_index; 3789 unsigned long flags; 3790 u32 changed_ep_bitmask; 3791 3792 xhci = hcd_to_xhci(hcd); 3793 vdev = xhci->devs[udev->slot_id]; 3794 3795 /* Set up a configure endpoint command to remove the streams rings */ 3796 spin_lock_irqsave(&xhci->lock, flags); 3797 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci, 3798 udev, eps, num_eps); 3799 if (changed_ep_bitmask == 0) { 3800 spin_unlock_irqrestore(&xhci->lock, flags); 3801 return -EINVAL; 3802 } 3803 3804 /* Use the xhci_command structure from the first endpoint. We may have 3805 * allocated too many, but the driver may call xhci_free_streams() for 3806 * each endpoint it grouped into one call to xhci_alloc_streams(). 3807 */ 3808 ep_index = xhci_get_endpoint_index(&eps[0]->desc); 3809 command = vdev->eps[ep_index].stream_info->free_streams_command; 3810 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); 3811 if (!ctrl_ctx) { 3812 spin_unlock_irqrestore(&xhci->lock, flags); 3813 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 3814 __func__); 3815 return -EINVAL; 3816 } 3817 3818 for (i = 0; i < num_eps; i++) { 3819 struct xhci_ep_ctx *ep_ctx; 3820 3821 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3822 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); 3823 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |= 3824 EP_GETTING_NO_STREAMS; 3825 3826 xhci_endpoint_copy(xhci, command->in_ctx, 3827 vdev->out_ctx, ep_index); 3828 xhci_setup_no_streams_ep_input_ctx(ep_ctx, 3829 &vdev->eps[ep_index]); 3830 } 3831 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx, 3832 vdev->out_ctx, ctrl_ctx, 3833 changed_ep_bitmask, changed_ep_bitmask); 3834 spin_unlock_irqrestore(&xhci->lock, flags); 3835 3836 /* Issue and wait for the configure endpoint command, 3837 * which must succeed. 3838 */ 3839 ret = xhci_configure_endpoint(xhci, udev, command, 3840 false, true); 3841 3842 /* xHC rejected the configure endpoint command for some reason, so we 3843 * leave the streams rings intact. 3844 */ 3845 if (ret < 0) 3846 return ret; 3847 3848 spin_lock_irqsave(&xhci->lock, flags); 3849 for (i = 0; i < num_eps; i++) { 3850 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 3851 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); 3852 vdev->eps[ep_index].stream_info = NULL; 3853 /* FIXME Unset maxPstreams in endpoint context and 3854 * update deq ptr to point to normal string ring. 3855 */ 3856 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS; 3857 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; 3858 } 3859 spin_unlock_irqrestore(&xhci->lock, flags); 3860 3861 return 0; 3862 } 3863 3864 /* 3865 * Deletes endpoint resources for endpoints that were active before a Reset 3866 * Device command, or a Disable Slot command. The Reset Device command leaves 3867 * the control endpoint intact, whereas the Disable Slot command deletes it. 3868 * 3869 * Must be called with xhci->lock held. 3870 */ 3871 void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci, 3872 struct xhci_virt_device *virt_dev, bool drop_control_ep) 3873 { 3874 int i; 3875 unsigned int num_dropped_eps = 0; 3876 unsigned int drop_flags = 0; 3877 3878 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) { 3879 if (virt_dev->eps[i].ring) { 3880 drop_flags |= 1 << i; 3881 num_dropped_eps++; 3882 } 3883 } 3884 xhci->num_active_eps -= num_dropped_eps; 3885 if (num_dropped_eps) 3886 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 3887 "Dropped %u ep ctxs, flags = 0x%x, " 3888 "%u now active.", 3889 num_dropped_eps, drop_flags, 3890 xhci->num_active_eps); 3891 } 3892 3893 static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev); 3894 3895 /* 3896 * This submits a Reset Device Command, which will set the device state to 0, 3897 * set the device address to 0, and disable all the endpoints except the default 3898 * control endpoint. The USB core should come back and call 3899 * xhci_address_device(), and then re-set up the configuration. If this is 3900 * called because of a usb_reset_and_verify_device(), then the old alternate 3901 * settings will be re-installed through the normal bandwidth allocation 3902 * functions. 3903 * 3904 * Wait for the Reset Device command to finish. Remove all structures 3905 * associated with the endpoints that were disabled. Clear the input device 3906 * structure? Reset the control endpoint 0 max packet size? 3907 * 3908 * If the virt_dev to be reset does not exist or does not match the udev, 3909 * it means the device is lost, possibly due to the xHC restore error and 3910 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to 3911 * re-allocate the device. 3912 */ 3913 static int xhci_discover_or_reset_device(struct usb_hcd *hcd, 3914 struct usb_device *udev) 3915 { 3916 int ret, i; 3917 unsigned long flags; 3918 struct xhci_hcd *xhci; 3919 unsigned int slot_id; 3920 struct xhci_virt_device *virt_dev; 3921 struct xhci_command *reset_device_cmd; 3922 struct xhci_slot_ctx *slot_ctx; 3923 int old_active_eps = 0; 3924 3925 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__); 3926 if (ret <= 0) 3927 return ret; 3928 xhci = hcd_to_xhci(hcd); 3929 slot_id = udev->slot_id; 3930 virt_dev = xhci->devs[slot_id]; 3931 if (!virt_dev) { 3932 xhci_dbg(xhci, "The device to be reset with slot ID %u does " 3933 "not exist. Re-allocate the device\n", slot_id); 3934 ret = xhci_alloc_dev(hcd, udev); 3935 if (ret == 1) 3936 return 0; 3937 else 3938 return -EINVAL; 3939 } 3940 3941 if (virt_dev->tt_info) 3942 old_active_eps = virt_dev->tt_info->active_eps; 3943 3944 if (virt_dev->udev != udev) { 3945 /* If the virt_dev and the udev does not match, this virt_dev 3946 * may belong to another udev. 3947 * Re-allocate the device. 3948 */ 3949 xhci_dbg(xhci, "The device to be reset with slot ID %u does " 3950 "not match the udev. Re-allocate the device\n", 3951 slot_id); 3952 ret = xhci_alloc_dev(hcd, udev); 3953 if (ret == 1) 3954 return 0; 3955 else 3956 return -EINVAL; 3957 } 3958 3959 /* If device is not setup, there is no point in resetting it */ 3960 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 3961 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) == 3962 SLOT_STATE_DISABLED) 3963 return 0; 3964 3965 if (xhci->quirks & XHCI_ETRON_HOST) { 3966 /* 3967 * Obtaining a new device slot to inform the xHCI host that 3968 * the USB device has been reset. 3969 */ 3970 ret = xhci_disable_and_free_slot(xhci, udev->slot_id); 3971 if (!ret) { 3972 ret = xhci_alloc_dev(hcd, udev); 3973 if (ret == 1) 3974 ret = 0; 3975 else 3976 ret = -EINVAL; 3977 } 3978 return ret; 3979 } 3980 3981 trace_xhci_discover_or_reset_device(slot_ctx); 3982 3983 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id); 3984 /* Allocate the command structure that holds the struct completion. 3985 * Assume we're in process context, since the normal device reset 3986 * process has to wait for the device anyway. Storage devices are 3987 * reset as part of error handling, so use GFP_NOIO instead of 3988 * GFP_KERNEL. 3989 */ 3990 reset_device_cmd = xhci_alloc_command(xhci, true, GFP_NOIO); 3991 if (!reset_device_cmd) { 3992 xhci_dbg(xhci, "Couldn't allocate command structure.\n"); 3993 return -ENOMEM; 3994 } 3995 3996 /* Attempt to submit the Reset Device command to the command ring */ 3997 spin_lock_irqsave(&xhci->lock, flags); 3998 3999 ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id); 4000 if (ret) { 4001 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); 4002 spin_unlock_irqrestore(&xhci->lock, flags); 4003 goto command_cleanup; 4004 } 4005 xhci_ring_cmd_db(xhci); 4006 spin_unlock_irqrestore(&xhci->lock, flags); 4007 4008 /* Wait for the Reset Device command to finish */ 4009 wait_for_completion(reset_device_cmd->completion); 4010 4011 /* The Reset Device command can't fail, according to the 0.95/0.96 spec, 4012 * unless we tried to reset a slot ID that wasn't enabled, 4013 * or the device wasn't in the addressed or configured state. 4014 */ 4015 ret = reset_device_cmd->status; 4016 switch (ret) { 4017 case COMP_COMMAND_ABORTED: 4018 case COMP_COMMAND_RING_STOPPED: 4019 xhci_warn(xhci, "Timeout waiting for reset device command\n"); 4020 ret = -ETIME; 4021 goto command_cleanup; 4022 case COMP_SLOT_NOT_ENABLED_ERROR: /* 0.95 completion for bad slot ID */ 4023 case COMP_CONTEXT_STATE_ERROR: /* 0.96 completion code for same thing */ 4024 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n", 4025 slot_id, 4026 xhci_get_slot_state(xhci, virt_dev->out_ctx)); 4027 xhci_dbg(xhci, "Not freeing device rings.\n"); 4028 /* Don't treat this as an error. May change my mind later. */ 4029 virt_dev->flags = 0; 4030 ret = 0; 4031 goto command_cleanup; 4032 case COMP_SUCCESS: 4033 xhci_dbg(xhci, "Successful reset device command.\n"); 4034 break; 4035 default: 4036 if (xhci_is_vendor_info_code(xhci, ret)) 4037 break; 4038 xhci_warn(xhci, "Unknown completion code %u for " 4039 "reset device command.\n", ret); 4040 ret = -EINVAL; 4041 goto command_cleanup; 4042 } 4043 4044 /* Free up host controller endpoint resources */ 4045 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { 4046 spin_lock_irqsave(&xhci->lock, flags); 4047 /* Don't delete the default control endpoint resources */ 4048 xhci_free_device_endpoint_resources(xhci, virt_dev, false); 4049 spin_unlock_irqrestore(&xhci->lock, flags); 4050 } 4051 4052 /* Everything but endpoint 0 is disabled, so free the rings. */ 4053 for (i = 1; i < 31; i++) { 4054 struct xhci_virt_ep *ep = &virt_dev->eps[i]; 4055 4056 if (ep->ep_state & EP_HAS_STREAMS) { 4057 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n", 4058 xhci_get_endpoint_address(i)); 4059 xhci_free_stream_info(xhci, ep->stream_info); 4060 ep->stream_info = NULL; 4061 ep->ep_state &= ~EP_HAS_STREAMS; 4062 } 4063 4064 if (ep->ring) { 4065 if (ep->sideband) 4066 xhci_sideband_notify_ep_ring_free(ep->sideband, i); 4067 xhci_debugfs_remove_endpoint(xhci, virt_dev, i); 4068 xhci_free_endpoint_ring(xhci, virt_dev, i); 4069 } 4070 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list)) 4071 xhci_drop_ep_from_interval_table(xhci, 4072 &virt_dev->eps[i].bw_info, 4073 virt_dev->bw_table, 4074 udev, 4075 &virt_dev->eps[i], 4076 virt_dev->tt_info); 4077 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info); 4078 } 4079 /* If necessary, update the number of active TTs on this root port */ 4080 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps); 4081 virt_dev->flags = 0; 4082 ret = 0; 4083 4084 command_cleanup: 4085 xhci_free_command(xhci, reset_device_cmd); 4086 return ret; 4087 } 4088 4089 /* 4090 * At this point, the struct usb_device is about to go away, the device has 4091 * disconnected, and all traffic has been stopped and the endpoints have been 4092 * disabled. Free any HC data structures associated with that device. 4093 */ 4094 static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev) 4095 { 4096 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 4097 struct xhci_virt_device *virt_dev; 4098 struct xhci_slot_ctx *slot_ctx; 4099 unsigned long flags; 4100 int i, ret; 4101 4102 /* 4103 * We called pm_runtime_get_noresume when the device was attached. 4104 * Decrement the counter here to allow controller to runtime suspend 4105 * if no devices remain. 4106 */ 4107 if (xhci->quirks & XHCI_RESET_ON_RESUME) 4108 pm_runtime_put_noidle(hcd->self.controller); 4109 4110 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__); 4111 /* If the host is halted due to driver unload, we still need to free the 4112 * device. 4113 */ 4114 if (ret <= 0 && ret != -ENODEV) 4115 return; 4116 4117 virt_dev = xhci->devs[udev->slot_id]; 4118 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 4119 trace_xhci_free_dev(slot_ctx); 4120 4121 /* Stop any wayward timer functions (which may grab the lock) */ 4122 for (i = 0; i < 31; i++) 4123 virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING; 4124 virt_dev->udev = NULL; 4125 xhci_disable_slot(xhci, udev->slot_id); 4126 4127 spin_lock_irqsave(&xhci->lock, flags); 4128 xhci_free_virt_device(xhci, virt_dev, udev->slot_id); 4129 spin_unlock_irqrestore(&xhci->lock, flags); 4130 4131 } 4132 4133 int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id) 4134 { 4135 struct xhci_command *command; 4136 unsigned long flags; 4137 u32 state; 4138 int ret; 4139 4140 command = xhci_alloc_command(xhci, true, GFP_KERNEL); 4141 if (!command) 4142 return -ENOMEM; 4143 4144 xhci_debugfs_remove_slot(xhci, slot_id); 4145 4146 spin_lock_irqsave(&xhci->lock, flags); 4147 /* Don't disable the slot if the host controller is dead. */ 4148 state = readl(&xhci->op_regs->status); 4149 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) || 4150 (xhci->xhc_state & XHCI_STATE_HALTED)) { 4151 spin_unlock_irqrestore(&xhci->lock, flags); 4152 xhci_free_command(xhci, command); 4153 return -ENODEV; 4154 } 4155 4156 ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT, 4157 slot_id); 4158 if (ret) { 4159 spin_unlock_irqrestore(&xhci->lock, flags); 4160 xhci_free_command(xhci, command); 4161 return ret; 4162 } 4163 xhci_ring_cmd_db(xhci); 4164 spin_unlock_irqrestore(&xhci->lock, flags); 4165 4166 wait_for_completion(command->completion); 4167 4168 if (command->status != COMP_SUCCESS) 4169 xhci_warn(xhci, "Unsuccessful disable slot %u command, status %d\n", 4170 slot_id, command->status); 4171 4172 xhci_free_command(xhci, command); 4173 4174 return 0; 4175 } 4176 4177 int xhci_disable_and_free_slot(struct xhci_hcd *xhci, u32 slot_id) 4178 { 4179 struct xhci_virt_device *vdev = xhci->devs[slot_id]; 4180 int ret; 4181 4182 ret = xhci_disable_slot(xhci, slot_id); 4183 xhci_free_virt_device(xhci, vdev, slot_id); 4184 return ret; 4185 } 4186 4187 /* 4188 * Checks if we have enough host controller resources for the default control 4189 * endpoint. 4190 * 4191 * Must be called with xhci->lock held. 4192 */ 4193 static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci) 4194 { 4195 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) { 4196 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 4197 "Not enough ep ctxs: " 4198 "%u active, need to add 1, limit is %u.", 4199 xhci->num_active_eps, xhci->limit_active_eps); 4200 return -ENOMEM; 4201 } 4202 xhci->num_active_eps += 1; 4203 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, 4204 "Adding 1 ep ctx, %u now active.", 4205 xhci->num_active_eps); 4206 return 0; 4207 } 4208 4209 4210 /* 4211 * Returns 0 if the xHC ran out of device slots, the Enable Slot command 4212 * timed out, or allocating memory failed. Returns 1 on success. 4213 */ 4214 int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev) 4215 { 4216 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 4217 struct xhci_virt_device *vdev; 4218 struct xhci_slot_ctx *slot_ctx; 4219 unsigned long flags; 4220 int ret, slot_id; 4221 struct xhci_command *command; 4222 4223 command = xhci_alloc_command(xhci, true, GFP_KERNEL); 4224 if (!command) 4225 return 0; 4226 4227 spin_lock_irqsave(&xhci->lock, flags); 4228 ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0); 4229 if (ret) { 4230 spin_unlock_irqrestore(&xhci->lock, flags); 4231 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); 4232 xhci_free_command(xhci, command); 4233 return 0; 4234 } 4235 xhci_ring_cmd_db(xhci); 4236 spin_unlock_irqrestore(&xhci->lock, flags); 4237 4238 wait_for_completion(command->completion); 4239 slot_id = command->slot_id; 4240 4241 if (!slot_id || command->status != COMP_SUCCESS) { 4242 xhci_err(xhci, "Error while assigning device slot ID: %s\n", 4243 xhci_trb_comp_code_string(command->status)); 4244 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n", 4245 xhci->max_slots); 4246 xhci_free_command(xhci, command); 4247 return 0; 4248 } 4249 4250 xhci_free_command(xhci, command); 4251 4252 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { 4253 spin_lock_irqsave(&xhci->lock, flags); 4254 ret = xhci_reserve_host_control_ep_resources(xhci); 4255 if (ret) { 4256 spin_unlock_irqrestore(&xhci->lock, flags); 4257 xhci_warn(xhci, "Not enough host resources, " 4258 "active endpoint contexts = %u\n", 4259 xhci->num_active_eps); 4260 goto disable_slot; 4261 } 4262 spin_unlock_irqrestore(&xhci->lock, flags); 4263 } 4264 /* Use GFP_NOIO, since this function can be called from 4265 * xhci_discover_or_reset_device(), which may be called as part of 4266 * mass storage driver error handling. 4267 */ 4268 if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) { 4269 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n"); 4270 goto disable_slot; 4271 } 4272 vdev = xhci->devs[slot_id]; 4273 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); 4274 trace_xhci_alloc_dev(slot_ctx); 4275 4276 udev->slot_id = slot_id; 4277 4278 xhci_debugfs_create_slot(xhci, slot_id); 4279 4280 /* 4281 * If resetting upon resume, we can't put the controller into runtime 4282 * suspend if there is a device attached. 4283 */ 4284 if (xhci->quirks & XHCI_RESET_ON_RESUME) 4285 pm_runtime_get_noresume(hcd->self.controller); 4286 4287 /* Is this a LS or FS device under a HS hub? */ 4288 /* Hub or peripherial? */ 4289 return 1; 4290 4291 disable_slot: 4292 xhci_disable_and_free_slot(xhci, udev->slot_id); 4293 4294 return 0; 4295 } 4296 4297 /** 4298 * xhci_setup_device - issues an Address Device command to assign a unique 4299 * USB bus address. 4300 * @hcd: USB host controller data structure. 4301 * @udev: USB dev structure representing the connected device. 4302 * @setup: Enum specifying setup mode: address only or with context. 4303 * @timeout_ms: Max wait time (ms) for the command operation to complete. 4304 * 4305 * Return: 0 if successful; otherwise, negative error code. 4306 */ 4307 static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev, 4308 enum xhci_setup_dev setup, unsigned int timeout_ms) 4309 { 4310 const char *act = setup == SETUP_CONTEXT_ONLY ? "context" : "address"; 4311 unsigned long flags; 4312 struct xhci_virt_device *virt_dev; 4313 int ret = 0; 4314 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 4315 struct xhci_slot_ctx *slot_ctx; 4316 struct xhci_input_control_ctx *ctrl_ctx; 4317 u64 temp_64; 4318 struct xhci_command *command = NULL; 4319 4320 mutex_lock(&xhci->mutex); 4321 4322 if (xhci->xhc_state) { /* dying, removing or halted */ 4323 ret = -ESHUTDOWN; 4324 goto out; 4325 } 4326 4327 if (!udev->slot_id) { 4328 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4329 "Bad Slot ID %d", udev->slot_id); 4330 ret = -EINVAL; 4331 goto out; 4332 } 4333 4334 virt_dev = xhci->devs[udev->slot_id]; 4335 4336 if (WARN_ON(!virt_dev)) { 4337 /* 4338 * In plug/unplug torture test with an NEC controller, 4339 * a zero-dereference was observed once due to virt_dev = 0. 4340 * Print useful debug rather than crash if it is observed again! 4341 */ 4342 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n", 4343 udev->slot_id); 4344 ret = -EINVAL; 4345 goto out; 4346 } 4347 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 4348 trace_xhci_setup_device_slot(slot_ctx); 4349 4350 if (setup == SETUP_CONTEXT_ONLY) { 4351 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) == 4352 SLOT_STATE_DEFAULT) { 4353 xhci_dbg(xhci, "Slot already in default state\n"); 4354 goto out; 4355 } 4356 } 4357 4358 command = xhci_alloc_command(xhci, true, GFP_KERNEL); 4359 if (!command) { 4360 ret = -ENOMEM; 4361 goto out; 4362 } 4363 4364 command->in_ctx = virt_dev->in_ctx; 4365 command->timeout_ms = timeout_ms; 4366 4367 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); 4368 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); 4369 if (!ctrl_ctx) { 4370 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 4371 __func__); 4372 ret = -EINVAL; 4373 goto out; 4374 } 4375 /* 4376 * If this is the first Set Address since device plug-in or 4377 * virt_device realloaction after a resume with an xHCI power loss, 4378 * then set up the slot context. 4379 */ 4380 if (!slot_ctx->dev_info) 4381 xhci_setup_addressable_virt_dev(xhci, udev); 4382 /* Otherwise, update the control endpoint ring enqueue pointer. */ 4383 else 4384 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev); 4385 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG); 4386 ctrl_ctx->drop_flags = 0; 4387 4388 trace_xhci_address_ctx(xhci, virt_dev->in_ctx); 4389 4390 trace_xhci_address_ctrl_ctx(ctrl_ctx); 4391 spin_lock_irqsave(&xhci->lock, flags); 4392 trace_xhci_setup_device(virt_dev); 4393 ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma, 4394 udev->slot_id, setup); 4395 if (ret) { 4396 spin_unlock_irqrestore(&xhci->lock, flags); 4397 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4398 "FIXME: allocate a command ring segment"); 4399 goto out; 4400 } 4401 xhci_ring_cmd_db(xhci); 4402 spin_unlock_irqrestore(&xhci->lock, flags); 4403 4404 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */ 4405 wait_for_completion(command->completion); 4406 4407 /* FIXME: From section 4.3.4: "Software shall be responsible for timing 4408 * the SetAddress() "recovery interval" required by USB and aborting the 4409 * command on a timeout. 4410 */ 4411 switch (command->status) { 4412 case COMP_COMMAND_ABORTED: 4413 case COMP_COMMAND_RING_STOPPED: 4414 xhci_warn(xhci, "Timeout while waiting for setup device command\n"); 4415 ret = -ETIME; 4416 break; 4417 case COMP_CONTEXT_STATE_ERROR: 4418 case COMP_SLOT_NOT_ENABLED_ERROR: 4419 xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n", 4420 act, udev->slot_id); 4421 ret = -EINVAL; 4422 break; 4423 case COMP_USB_TRANSACTION_ERROR: 4424 dev_warn(&udev->dev, "Device not responding to setup %s.\n", act); 4425 4426 mutex_unlock(&xhci->mutex); 4427 ret = xhci_disable_and_free_slot(xhci, udev->slot_id); 4428 if (!ret) { 4429 if (xhci_alloc_dev(hcd, udev) == 1) 4430 xhci_setup_addressable_virt_dev(xhci, udev); 4431 } 4432 kfree(command->completion); 4433 kfree(command); 4434 return -EPROTO; 4435 case COMP_INCOMPATIBLE_DEVICE_ERROR: 4436 dev_warn(&udev->dev, 4437 "ERROR: Incompatible device for setup %s command\n", act); 4438 ret = -ENODEV; 4439 break; 4440 case COMP_SUCCESS: 4441 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4442 "Successful setup %s command", act); 4443 break; 4444 default: 4445 xhci_err(xhci, 4446 "ERROR: unexpected setup %s command completion code 0x%x.\n", 4447 act, command->status); 4448 trace_xhci_address_ctx(xhci, virt_dev->out_ctx); 4449 ret = -EINVAL; 4450 break; 4451 } 4452 if (ret) 4453 goto out; 4454 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); 4455 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4456 "Op regs DCBAA ptr = %#016llx", temp_64); 4457 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4458 "Slot ID %d dcbaa entry @%p = %#016llx", 4459 udev->slot_id, 4460 &xhci->dcbaa.ctx_array[udev->slot_id], 4461 (unsigned long long) 4462 le64_to_cpu(xhci->dcbaa.ctx_array[udev->slot_id])); 4463 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4464 "Output Context DMA address = %#08llx", 4465 (unsigned long long)virt_dev->out_ctx->dma); 4466 trace_xhci_address_ctx(xhci, virt_dev->in_ctx); 4467 /* 4468 * USB core uses address 1 for the roothubs, so we add one to the 4469 * address given back to us by the HC. 4470 */ 4471 trace_xhci_address_ctx(xhci, virt_dev->out_ctx); 4472 /* Zero the input context control for later use */ 4473 ctrl_ctx->add_flags = 0; 4474 ctrl_ctx->drop_flags = 0; 4475 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 4476 udev->devaddr = (u8)(le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); 4477 4478 xhci_dbg_trace(xhci, trace_xhci_dbg_address, 4479 "Internal device address = %d", 4480 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); 4481 out: 4482 mutex_unlock(&xhci->mutex); 4483 if (command) { 4484 kfree(command->completion); 4485 kfree(command); 4486 } 4487 return ret; 4488 } 4489 4490 static int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev, 4491 unsigned int timeout_ms) 4492 { 4493 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ADDRESS, timeout_ms); 4494 } 4495 4496 static int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev) 4497 { 4498 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY, 4499 XHCI_CMD_DEFAULT_TIMEOUT); 4500 } 4501 4502 /* 4503 * Transfer the port index into real index in the HW port status 4504 * registers. Caculate offset between the port's PORTSC register 4505 * and port status base. Divide the number of per port register 4506 * to get the real index. The raw port number bases 1. 4507 */ 4508 int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1) 4509 { 4510 struct xhci_hub *rhub; 4511 4512 rhub = xhci_get_rhub(hcd); 4513 return rhub->ports[port1 - 1]->hw_portnum + 1; 4514 } 4515 4516 /* 4517 * Issue an Evaluate Context command to change the Maximum Exit Latency in the 4518 * slot context. If that succeeds, store the new MEL in the xhci_virt_device. 4519 */ 4520 static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci, 4521 struct usb_device *udev, u16 max_exit_latency) 4522 { 4523 struct xhci_virt_device *virt_dev; 4524 struct xhci_command *command; 4525 struct xhci_input_control_ctx *ctrl_ctx; 4526 struct xhci_slot_ctx *slot_ctx; 4527 unsigned long flags; 4528 int ret; 4529 4530 command = xhci_alloc_command_with_ctx(xhci, true, GFP_KERNEL); 4531 if (!command) 4532 return -ENOMEM; 4533 4534 spin_lock_irqsave(&xhci->lock, flags); 4535 4536 virt_dev = xhci->devs[udev->slot_id]; 4537 4538 /* 4539 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and 4540 * xHC was re-initialized. Exit latency will be set later after 4541 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated 4542 */ 4543 4544 if (!virt_dev || max_exit_latency == virt_dev->current_mel) { 4545 spin_unlock_irqrestore(&xhci->lock, flags); 4546 xhci_free_command(xhci, command); 4547 return 0; 4548 } 4549 4550 /* Attempt to issue an Evaluate Context command to change the MEL. */ 4551 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); 4552 if (!ctrl_ctx) { 4553 spin_unlock_irqrestore(&xhci->lock, flags); 4554 xhci_free_command(xhci, command); 4555 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 4556 __func__); 4557 return -ENOMEM; 4558 } 4559 4560 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx); 4561 spin_unlock_irqrestore(&xhci->lock, flags); 4562 4563 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); 4564 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx); 4565 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT)); 4566 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency); 4567 slot_ctx->dev_state = 0; 4568 4569 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, 4570 "Set up evaluate context for LPM MEL change."); 4571 4572 /* Issue and wait for the evaluate context command. */ 4573 ret = xhci_configure_endpoint(xhci, udev, command, 4574 true, true); 4575 4576 if (!ret) { 4577 spin_lock_irqsave(&xhci->lock, flags); 4578 virt_dev->current_mel = max_exit_latency; 4579 spin_unlock_irqrestore(&xhci->lock, flags); 4580 } 4581 4582 xhci_free_command(xhci, command); 4583 4584 return ret; 4585 } 4586 4587 #ifdef CONFIG_PM 4588 4589 /* BESL to HIRD Encoding array for USB2 LPM */ 4590 static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000, 4591 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000}; 4592 4593 /* Calculate HIRD/BESL for USB2 PORTPMSC*/ 4594 static int xhci_calculate_hird_besl(struct xhci_hcd *xhci, 4595 struct usb_device *udev) 4596 { 4597 int u2del, besl, besl_host; 4598 int besl_device = 0; 4599 u32 field; 4600 4601 u2del = HCS_U2_LATENCY(xhci->hcs_params3); 4602 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); 4603 4604 if (field & USB_BESL_SUPPORT) { 4605 for (besl_host = 0; besl_host < 16; besl_host++) { 4606 if (xhci_besl_encoding[besl_host] >= u2del) 4607 break; 4608 } 4609 /* Use baseline BESL value as default */ 4610 if (field & USB_BESL_BASELINE_VALID) 4611 besl_device = USB_GET_BESL_BASELINE(field); 4612 else if (field & USB_BESL_DEEP_VALID) 4613 besl_device = USB_GET_BESL_DEEP(field); 4614 } else { 4615 if (u2del <= 50) 4616 besl_host = 0; 4617 else 4618 besl_host = (u2del - 51) / 75 + 1; 4619 } 4620 4621 besl = besl_host + besl_device; 4622 if (besl > 15) 4623 besl = 15; 4624 4625 return besl; 4626 } 4627 4628 /* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */ 4629 static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev) 4630 { 4631 u32 field; 4632 int l1; 4633 int besld = 0; 4634 int hirdm = 0; 4635 4636 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); 4637 4638 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */ 4639 l1 = udev->l1_params.timeout / 256; 4640 4641 /* device has preferred BESLD */ 4642 if (field & USB_BESL_DEEP_VALID) { 4643 besld = USB_GET_BESL_DEEP(field); 4644 hirdm = 1; 4645 } 4646 4647 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm); 4648 } 4649 4650 static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd, 4651 struct usb_device *udev, int enable) 4652 { 4653 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 4654 struct xhci_port **ports; 4655 struct xhci_port_regs __iomem *port_reg; 4656 u32 pm_val, hlpm_val, field; 4657 unsigned int port_num; 4658 unsigned long flags; 4659 int hird, exit_latency; 4660 int ret; 4661 4662 if (xhci->quirks & XHCI_HW_LPM_DISABLE) 4663 return -EPERM; 4664 4665 if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support || 4666 !udev->lpm_capable) 4667 return -EPERM; 4668 4669 if (!udev->parent || udev->parent->parent || 4670 udev->descriptor.bDeviceClass == USB_CLASS_HUB) 4671 return -EPERM; 4672 4673 if (udev->usb2_hw_lpm_capable != 1) 4674 return -EPERM; 4675 4676 spin_lock_irqsave(&xhci->lock, flags); 4677 4678 ports = xhci->usb2_rhub.ports; 4679 port_num = udev->portnum - 1; 4680 port_reg = ports[port_num]->port_reg; 4681 pm_val = readl(&port_reg->portpmsc); 4682 4683 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n", 4684 str_enable_disable(enable), port_num + 1); 4685 4686 if (enable) { 4687 /* Host supports BESL timeout instead of HIRD */ 4688 if (udev->usb2_hw_lpm_besl_capable) { 4689 /* if device doesn't have a preferred BESL value use a 4690 * default one which works with mixed HIRD and BESL 4691 * systems. See XHCI_DEFAULT_BESL definition in xhci.h 4692 */ 4693 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); 4694 if ((field & USB_BESL_SUPPORT) && 4695 (field & USB_BESL_BASELINE_VALID)) 4696 hird = USB_GET_BESL_BASELINE(field); 4697 else 4698 hird = udev->l1_params.besl; 4699 4700 exit_latency = xhci_besl_encoding[hird]; 4701 spin_unlock_irqrestore(&xhci->lock, flags); 4702 4703 ret = xhci_change_max_exit_latency(xhci, udev, 4704 exit_latency); 4705 if (ret < 0) 4706 return ret; 4707 spin_lock_irqsave(&xhci->lock, flags); 4708 4709 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev); 4710 writel(hlpm_val, &port_reg->porthlmpc); 4711 /* flush write */ 4712 readl(&port_reg->porthlmpc); 4713 } else { 4714 hird = xhci_calculate_hird_besl(xhci, udev); 4715 } 4716 4717 pm_val &= ~PORT_HIRD_MASK; 4718 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id); 4719 writel(pm_val, &port_reg->portpmsc); 4720 pm_val = readl(&port_reg->portpmsc); 4721 pm_val |= PORT_HLE; 4722 writel(pm_val, &port_reg->portpmsc); 4723 /* flush write */ 4724 readl(&port_reg->portpmsc); 4725 } else { 4726 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK); 4727 writel(pm_val, &port_reg->portpmsc); 4728 /* flush write */ 4729 readl(&port_reg->portpmsc); 4730 if (udev->usb2_hw_lpm_besl_capable) { 4731 spin_unlock_irqrestore(&xhci->lock, flags); 4732 xhci_change_max_exit_latency(xhci, udev, 0); 4733 readl_poll_timeout(&ports[port_num]->port_reg->portsc, pm_val, 4734 (pm_val & PORT_PLS_MASK) == XDEV_U0, 4735 100, 10000); 4736 return 0; 4737 } 4738 } 4739 4740 spin_unlock_irqrestore(&xhci->lock, flags); 4741 return 0; 4742 } 4743 4744 static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev) 4745 { 4746 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 4747 struct xhci_port *port; 4748 u32 capability; 4749 4750 /* Check if USB3 device at root port is tunneled over USB4 */ 4751 if (hcd->speed >= HCD_USB3 && !udev->parent->parent) { 4752 port = xhci->usb3_rhub.ports[udev->portnum - 1]; 4753 4754 udev->tunnel_mode = xhci_port_is_tunneled(xhci, port); 4755 if (udev->tunnel_mode == USB_LINK_UNKNOWN) 4756 dev_dbg(&udev->dev, "link tunnel state unknown\n"); 4757 else if (udev->tunnel_mode == USB_LINK_TUNNELED) 4758 dev_dbg(&udev->dev, "tunneled over USB4 link\n"); 4759 else if (udev->tunnel_mode == USB_LINK_NATIVE) 4760 dev_dbg(&udev->dev, "native USB 3.x link\n"); 4761 return 0; 4762 } 4763 4764 if (hcd->speed >= HCD_USB3 || !udev->lpm_capable || !xhci->hw_lpm_support) 4765 return 0; 4766 4767 /* we only support lpm for non-hub device connected to root hub yet */ 4768 if (!udev->parent || udev->parent->parent || 4769 udev->descriptor.bDeviceClass == USB_CLASS_HUB) 4770 return 0; 4771 4772 port = xhci->usb2_rhub.ports[udev->portnum - 1]; 4773 capability = port->port_cap->protocol_caps; 4774 4775 if (capability & XHCI_HLC) { 4776 udev->usb2_hw_lpm_capable = 1; 4777 udev->l1_params.timeout = XHCI_L1_TIMEOUT; 4778 udev->l1_params.besl = XHCI_DEFAULT_BESL; 4779 if (capability & XHCI_BLC) 4780 udev->usb2_hw_lpm_besl_capable = 1; 4781 } 4782 4783 return 0; 4784 } 4785 4786 /*---------------------- USB 3.0 Link PM functions ------------------------*/ 4787 4788 /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */ 4789 static unsigned long long xhci_service_interval_to_ns( 4790 struct usb_endpoint_descriptor *desc) 4791 { 4792 return (1ULL << (desc->bInterval - 1)) * 125 * 1000; 4793 } 4794 4795 static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev, 4796 enum usb3_link_state state) 4797 { 4798 unsigned long long sel; 4799 unsigned long long pel; 4800 unsigned int max_sel_pel; 4801 char *state_name; 4802 4803 switch (state) { 4804 case USB3_LPM_U1: 4805 /* Convert SEL and PEL stored in nanoseconds to microseconds */ 4806 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 4807 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); 4808 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL; 4809 state_name = "U1"; 4810 break; 4811 case USB3_LPM_U2: 4812 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 4813 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); 4814 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL; 4815 state_name = "U2"; 4816 break; 4817 default: 4818 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n", 4819 __func__); 4820 return USB3_LPM_DISABLED; 4821 } 4822 4823 if (sel <= max_sel_pel && pel <= max_sel_pel) 4824 return USB3_LPM_DEVICE_INITIATED; 4825 4826 if (sel > max_sel_pel) 4827 dev_dbg(&udev->dev, "Device-initiated %s disabled " 4828 "due to long SEL %llu ms\n", 4829 state_name, sel); 4830 else 4831 dev_dbg(&udev->dev, "Device-initiated %s disabled " 4832 "due to long PEL %llu ms\n", 4833 state_name, pel); 4834 return USB3_LPM_DISABLED; 4835 } 4836 4837 /* The U1 timeout should be the maximum of the following values: 4838 * - For control endpoints, U1 system exit latency (SEL) * 3 4839 * - For bulk endpoints, U1 SEL * 5 4840 * - For interrupt endpoints: 4841 * - Notification EPs, U1 SEL * 3 4842 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2) 4843 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2) 4844 */ 4845 static unsigned long long xhci_calculate_intel_u1_timeout( 4846 struct usb_device *udev, 4847 struct usb_endpoint_descriptor *desc) 4848 { 4849 unsigned long long timeout_ns; 4850 int ep_type; 4851 int intr_type; 4852 4853 ep_type = usb_endpoint_type(desc); 4854 switch (ep_type) { 4855 case USB_ENDPOINT_XFER_CONTROL: 4856 timeout_ns = udev->u1_params.sel * 3; 4857 break; 4858 case USB_ENDPOINT_XFER_BULK: 4859 timeout_ns = udev->u1_params.sel * 5; 4860 break; 4861 case USB_ENDPOINT_XFER_INT: 4862 intr_type = usb_endpoint_interrupt_type(desc); 4863 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) { 4864 timeout_ns = udev->u1_params.sel * 3; 4865 break; 4866 } 4867 /* Otherwise the calculation is the same as isoc eps */ 4868 fallthrough; 4869 case USB_ENDPOINT_XFER_ISOC: 4870 timeout_ns = xhci_service_interval_to_ns(desc); 4871 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100); 4872 if (timeout_ns < udev->u1_params.sel * 2) 4873 timeout_ns = udev->u1_params.sel * 2; 4874 break; 4875 default: 4876 return 0; 4877 } 4878 4879 return timeout_ns; 4880 } 4881 4882 /* Returns the hub-encoded U1 timeout value. */ 4883 static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci, 4884 struct usb_device *udev, 4885 struct usb_endpoint_descriptor *desc) 4886 { 4887 unsigned long long timeout_ns; 4888 4889 /* Prevent U1 if service interval is shorter than U1 exit latency */ 4890 if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) { 4891 if (xhci_service_interval_to_ns(desc) <= udev->u1_params.mel) { 4892 dev_dbg(&udev->dev, "Disable U1, ESIT shorter than exit latency\n"); 4893 return USB3_LPM_DISABLED; 4894 } 4895 } 4896 4897 if (xhci->quirks & (XHCI_INTEL_HOST | XHCI_ZHAOXIN_HOST)) 4898 timeout_ns = xhci_calculate_intel_u1_timeout(udev, desc); 4899 else 4900 timeout_ns = udev->u1_params.sel; 4901 4902 /* The U1 timeout is encoded in 1us intervals. 4903 * Don't return a timeout of zero, because that's USB3_LPM_DISABLED. 4904 */ 4905 if (timeout_ns == USB3_LPM_DISABLED) 4906 timeout_ns = 1; 4907 else 4908 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000); 4909 4910 /* If the necessary timeout value is bigger than what we can set in the 4911 * USB 3.0 hub, we have to disable hub-initiated U1. 4912 */ 4913 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT) 4914 return timeout_ns; 4915 dev_dbg(&udev->dev, "Hub-initiated U1 disabled due to long timeout %lluus\n", 4916 timeout_ns); 4917 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1); 4918 } 4919 4920 /* The U2 timeout should be the maximum of: 4921 * - 10 ms (to avoid the bandwidth impact on the scheduler) 4922 * - largest bInterval of any active periodic endpoint (to avoid going 4923 * into lower power link states between intervals). 4924 * - the U2 Exit Latency of the device 4925 */ 4926 static unsigned long long xhci_calculate_intel_u2_timeout( 4927 struct usb_device *udev, 4928 struct usb_endpoint_descriptor *desc) 4929 { 4930 unsigned long long timeout_ns; 4931 unsigned long long u2_del_ns; 4932 4933 timeout_ns = 10 * 1000 * 1000; 4934 4935 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) && 4936 (xhci_service_interval_to_ns(desc) > timeout_ns)) 4937 timeout_ns = xhci_service_interval_to_ns(desc); 4938 4939 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL; 4940 if (u2_del_ns > timeout_ns) 4941 timeout_ns = u2_del_ns; 4942 4943 return timeout_ns; 4944 } 4945 4946 /* Returns the hub-encoded U2 timeout value. */ 4947 static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci, 4948 struct usb_device *udev, 4949 struct usb_endpoint_descriptor *desc) 4950 { 4951 unsigned long long timeout_ns; 4952 4953 /* Prevent U2 if service interval is shorter than U2 exit latency */ 4954 if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) { 4955 if (xhci_service_interval_to_ns(desc) <= udev->u2_params.mel) { 4956 dev_dbg(&udev->dev, "Disable U2, ESIT shorter than exit latency\n"); 4957 return USB3_LPM_DISABLED; 4958 } 4959 } 4960 4961 if (xhci->quirks & (XHCI_INTEL_HOST | XHCI_ZHAOXIN_HOST)) 4962 timeout_ns = xhci_calculate_intel_u2_timeout(udev, desc); 4963 else 4964 timeout_ns = udev->u2_params.sel; 4965 4966 /* The U2 timeout is encoded in 256us intervals */ 4967 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000); 4968 /* If the necessary timeout value is bigger than what we can set in the 4969 * USB 3.0 hub, we have to disable hub-initiated U2. 4970 */ 4971 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT) 4972 return timeout_ns; 4973 dev_dbg(&udev->dev, "Hub-initiated U2 disabled due to long timeout %lluus\n", 4974 timeout_ns * 256); 4975 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2); 4976 } 4977 4978 static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci, 4979 struct usb_device *udev, 4980 struct usb_endpoint_descriptor *desc, 4981 enum usb3_link_state state, 4982 u16 *timeout) 4983 { 4984 if (state == USB3_LPM_U1) 4985 return xhci_calculate_u1_timeout(xhci, udev, desc); 4986 else if (state == USB3_LPM_U2) 4987 return xhci_calculate_u2_timeout(xhci, udev, desc); 4988 4989 return USB3_LPM_DISABLED; 4990 } 4991 4992 static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci, 4993 struct usb_device *udev, 4994 struct usb_endpoint_descriptor *desc, 4995 enum usb3_link_state state, 4996 u16 *timeout) 4997 { 4998 u16 alt_timeout; 4999 5000 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev, 5001 desc, state, timeout); 5002 5003 /* If we found we can't enable hub-initiated LPM, and 5004 * the U1 or U2 exit latency was too high to allow 5005 * device-initiated LPM as well, then we will disable LPM 5006 * for this device, so stop searching any further. 5007 */ 5008 if (alt_timeout == USB3_LPM_DISABLED) { 5009 *timeout = alt_timeout; 5010 return -E2BIG; 5011 } 5012 if (alt_timeout > *timeout) 5013 *timeout = alt_timeout; 5014 return 0; 5015 } 5016 5017 static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci, 5018 struct usb_device *udev, 5019 struct usb_host_interface *alt, 5020 enum usb3_link_state state, 5021 u16 *timeout) 5022 { 5023 int j; 5024 5025 for (j = 0; j < alt->desc.bNumEndpoints; j++) { 5026 if (xhci_update_timeout_for_endpoint(xhci, udev, 5027 &alt->endpoint[j].desc, state, timeout)) 5028 return -E2BIG; 5029 } 5030 return 0; 5031 } 5032 5033 static int xhci_check_tier_policy(struct xhci_hcd *xhci, 5034 struct usb_device *udev, 5035 enum usb3_link_state state) 5036 { 5037 struct usb_device *parent = udev->parent; 5038 int tier = 1; /* roothub is tier1 */ 5039 5040 while (parent) { 5041 parent = parent->parent; 5042 tier++; 5043 } 5044 5045 if (xhci->quirks & XHCI_INTEL_HOST && tier > 3) 5046 goto fail; 5047 if (xhci->quirks & XHCI_ZHAOXIN_HOST && tier > 2) 5048 goto fail; 5049 5050 return 0; 5051 fail: 5052 dev_dbg(&udev->dev, "Tier policy prevents U1/U2 LPM states for devices at tier %d\n", 5053 tier); 5054 return -E2BIG; 5055 } 5056 5057 /* Returns the U1 or U2 timeout that should be enabled. 5058 * If the tier check or timeout setting functions return with a non-zero exit 5059 * code, that means the timeout value has been finalized and we shouldn't look 5060 * at any more endpoints. 5061 */ 5062 static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd, 5063 struct usb_device *udev, enum usb3_link_state state) 5064 { 5065 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 5066 struct usb_host_config *config; 5067 char *state_name; 5068 int i; 5069 u16 timeout = USB3_LPM_DISABLED; 5070 5071 if (state == USB3_LPM_U1) 5072 state_name = "U1"; 5073 else if (state == USB3_LPM_U2) 5074 state_name = "U2"; 5075 else { 5076 dev_warn(&udev->dev, "Can't enable unknown link state %i\n", 5077 state); 5078 return timeout; 5079 } 5080 5081 /* Gather some information about the currently installed configuration 5082 * and alternate interface settings. 5083 */ 5084 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc, 5085 state, &timeout)) 5086 return timeout; 5087 5088 config = udev->actconfig; 5089 if (!config) 5090 return timeout; 5091 5092 for (i = 0; i < config->desc.bNumInterfaces; i++) { 5093 struct usb_driver *driver; 5094 struct usb_interface *intf = config->interface[i]; 5095 5096 if (!intf) 5097 continue; 5098 5099 /* Check if any currently bound drivers want hub-initiated LPM 5100 * disabled. 5101 */ 5102 if (intf->dev.driver) { 5103 driver = to_usb_driver(intf->dev.driver); 5104 if (driver && driver->disable_hub_initiated_lpm) { 5105 dev_dbg(&udev->dev, "Hub-initiated %s disabled at request of driver %s\n", 5106 state_name, driver->name); 5107 timeout = xhci_get_timeout_no_hub_lpm(udev, 5108 state); 5109 if (timeout == USB3_LPM_DISABLED) 5110 return timeout; 5111 } 5112 } 5113 5114 /* Not sure how this could happen... */ 5115 if (!intf->cur_altsetting) 5116 continue; 5117 5118 if (xhci_update_timeout_for_interface(xhci, udev, 5119 intf->cur_altsetting, 5120 state, &timeout)) 5121 return timeout; 5122 } 5123 return timeout; 5124 } 5125 5126 static int calculate_max_exit_latency(struct usb_device *udev, 5127 enum usb3_link_state state_changed, 5128 u16 hub_encoded_timeout) 5129 { 5130 unsigned long long u1_mel_us = 0; 5131 unsigned long long u2_mel_us = 0; 5132 unsigned long long mel_us = 0; 5133 bool disabling_u1; 5134 bool disabling_u2; 5135 bool enabling_u1; 5136 bool enabling_u2; 5137 5138 disabling_u1 = (state_changed == USB3_LPM_U1 && 5139 hub_encoded_timeout == USB3_LPM_DISABLED); 5140 disabling_u2 = (state_changed == USB3_LPM_U2 && 5141 hub_encoded_timeout == USB3_LPM_DISABLED); 5142 5143 enabling_u1 = (state_changed == USB3_LPM_U1 && 5144 hub_encoded_timeout != USB3_LPM_DISABLED); 5145 enabling_u2 = (state_changed == USB3_LPM_U2 && 5146 hub_encoded_timeout != USB3_LPM_DISABLED); 5147 5148 /* If U1 was already enabled and we're not disabling it, 5149 * or we're going to enable U1, account for the U1 max exit latency. 5150 */ 5151 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) || 5152 enabling_u1) 5153 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000); 5154 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) || 5155 enabling_u2) 5156 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000); 5157 5158 mel_us = max(u1_mel_us, u2_mel_us); 5159 5160 /* xHCI host controller max exit latency field is only 16 bits wide. */ 5161 if (mel_us > MAX_EXIT) { 5162 dev_warn(&udev->dev, "Link PM max exit latency of %lluus " 5163 "is too big.\n", mel_us); 5164 return -E2BIG; 5165 } 5166 return mel_us; 5167 } 5168 5169 /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */ 5170 static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd, 5171 struct usb_device *udev, enum usb3_link_state state) 5172 { 5173 struct xhci_hcd *xhci; 5174 struct xhci_port *port; 5175 u16 hub_encoded_timeout; 5176 int mel; 5177 int ret; 5178 5179 xhci = hcd_to_xhci(hcd); 5180 /* The LPM timeout values are pretty host-controller specific, so don't 5181 * enable hub-initiated timeouts unless the vendor has provided 5182 * information about their timeout algorithm. 5183 */ 5184 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) || 5185 !xhci->devs[udev->slot_id]) 5186 return USB3_LPM_DISABLED; 5187 5188 if (xhci_check_tier_policy(xhci, udev, state) < 0) 5189 return USB3_LPM_DISABLED; 5190 5191 /* If connected to root port then check port can handle lpm */ 5192 if (udev->parent && !udev->parent->parent) { 5193 port = xhci->usb3_rhub.ports[udev->portnum - 1]; 5194 if (port->lpm_incapable) 5195 return USB3_LPM_DISABLED; 5196 } 5197 5198 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state); 5199 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout); 5200 if (mel < 0) { 5201 /* Max Exit Latency is too big, disable LPM. */ 5202 hub_encoded_timeout = USB3_LPM_DISABLED; 5203 mel = 0; 5204 } 5205 5206 ret = xhci_change_max_exit_latency(xhci, udev, mel); 5207 if (ret) 5208 return ret; 5209 return hub_encoded_timeout; 5210 } 5211 5212 static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd, 5213 struct usb_device *udev, enum usb3_link_state state) 5214 { 5215 struct xhci_hcd *xhci; 5216 u16 mel; 5217 5218 xhci = hcd_to_xhci(hcd); 5219 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) || 5220 !xhci->devs[udev->slot_id]) 5221 return 0; 5222 5223 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED); 5224 return xhci_change_max_exit_latency(xhci, udev, mel); 5225 } 5226 #else /* CONFIG_PM */ 5227 5228 static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd, 5229 struct usb_device *udev, int enable) 5230 { 5231 return 0; 5232 } 5233 5234 static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev) 5235 { 5236 return 0; 5237 } 5238 5239 static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd, 5240 struct usb_device *udev, enum usb3_link_state state) 5241 { 5242 return USB3_LPM_DISABLED; 5243 } 5244 5245 static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd, 5246 struct usb_device *udev, enum usb3_link_state state) 5247 { 5248 return 0; 5249 } 5250 #endif /* CONFIG_PM */ 5251 5252 /*-------------------------------------------------------------------------*/ 5253 5254 /* Once a hub descriptor is fetched for a device, we need to update the xHC's 5255 * internal data structures for the device. 5256 */ 5257 int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev, 5258 struct usb_tt *tt, gfp_t mem_flags) 5259 { 5260 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 5261 struct xhci_virt_device *vdev; 5262 struct xhci_command *config_cmd; 5263 struct xhci_input_control_ctx *ctrl_ctx; 5264 struct xhci_slot_ctx *slot_ctx; 5265 unsigned long flags; 5266 unsigned think_time; 5267 int ret; 5268 5269 /* Ignore root hubs */ 5270 if (!hdev->parent) 5271 return 0; 5272 5273 vdev = xhci->devs[hdev->slot_id]; 5274 if (!vdev) { 5275 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n"); 5276 return -EINVAL; 5277 } 5278 5279 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags); 5280 if (!config_cmd) 5281 return -ENOMEM; 5282 5283 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx); 5284 if (!ctrl_ctx) { 5285 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", 5286 __func__); 5287 xhci_free_command(xhci, config_cmd); 5288 return -ENOMEM; 5289 } 5290 5291 spin_lock_irqsave(&xhci->lock, flags); 5292 if (hdev->speed == USB_SPEED_HIGH && 5293 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) { 5294 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n"); 5295 xhci_free_command(xhci, config_cmd); 5296 spin_unlock_irqrestore(&xhci->lock, flags); 5297 return -ENOMEM; 5298 } 5299 5300 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx); 5301 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); 5302 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx); 5303 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB); 5304 /* 5305 * refer to section 6.2.2: MTT should be 0 for full speed hub, 5306 * but it may be already set to 1 when setup an xHCI virtual 5307 * device, so clear it anyway. 5308 */ 5309 if (tt->multi) 5310 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); 5311 else if (hdev->speed == USB_SPEED_FULL) 5312 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT); 5313 5314 if (xhci->hci_version > 0x95) { 5315 xhci_dbg(xhci, "xHCI version %x needs hub " 5316 "TT think time and number of ports\n", 5317 (unsigned int) xhci->hci_version); 5318 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild)); 5319 /* Set TT think time - convert from ns to FS bit times. 5320 * 0 = 8 FS bit times, 1 = 16 FS bit times, 5321 * 2 = 24 FS bit times, 3 = 32 FS bit times. 5322 * 5323 * xHCI 1.0: this field shall be 0 if the device is not a 5324 * High-spped hub. 5325 */ 5326 think_time = tt->think_time; 5327 if (think_time != 0) 5328 think_time = (think_time / 666) - 1; 5329 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH) 5330 slot_ctx->tt_info |= 5331 cpu_to_le32(TT_THINK_TIME(think_time)); 5332 } else { 5333 xhci_dbg(xhci, "xHCI version %x doesn't need hub " 5334 "TT think time or number of ports\n", 5335 (unsigned int) xhci->hci_version); 5336 } 5337 slot_ctx->dev_state = 0; 5338 spin_unlock_irqrestore(&xhci->lock, flags); 5339 5340 xhci_dbg(xhci, "Set up %s for hub device.\n", 5341 (xhci->hci_version > 0x95) ? 5342 "configure endpoint" : "evaluate context"); 5343 5344 /* Issue and wait for the configure endpoint or 5345 * evaluate context command. 5346 */ 5347 if (xhci->hci_version > 0x95) 5348 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, 5349 false, false); 5350 else 5351 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, 5352 true, false); 5353 5354 xhci_free_command(xhci, config_cmd); 5355 return ret; 5356 } 5357 EXPORT_SYMBOL_GPL(xhci_update_hub_device); 5358 5359 static int xhci_get_frame(struct usb_hcd *hcd) 5360 { 5361 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 5362 /* EHCI mods by the periodic size. Why? */ 5363 return readl(&xhci->run_regs->microframe_index) >> 3; 5364 } 5365 5366 static void xhci_hcd_init_usb2_data(struct xhci_hcd *xhci, struct usb_hcd *hcd) 5367 { 5368 xhci->usb2_rhub.hcd = hcd; 5369 hcd->speed = HCD_USB2; 5370 hcd->self.root_hub->speed = USB_SPEED_HIGH; 5371 /* 5372 * USB 2.0 roothub under xHCI has an integrated TT, 5373 * (rate matching hub) as opposed to having an OHCI/UHCI 5374 * companion controller. 5375 */ 5376 hcd->has_tt = 1; 5377 } 5378 5379 static void xhci_hcd_init_usb3_data(struct xhci_hcd *xhci, struct usb_hcd *hcd) 5380 { 5381 unsigned int minor_rev; 5382 5383 /* 5384 * Early xHCI 1.1 spec did not mention USB 3.1 capable hosts 5385 * should return 0x31 for sbrn, or that the minor revision 5386 * is a two digit BCD containig minor and sub-minor numbers. 5387 * This was later clarified in xHCI 1.2. 5388 * 5389 * Some USB 3.1 capable hosts therefore have sbrn 0x30, and 5390 * minor revision set to 0x1 instead of 0x10. 5391 */ 5392 if (xhci->usb3_rhub.min_rev == 0x1) 5393 minor_rev = 1; 5394 else 5395 minor_rev = xhci->usb3_rhub.min_rev / 0x10; 5396 5397 switch (minor_rev) { 5398 case 2: 5399 hcd->speed = HCD_USB32; 5400 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS; 5401 hcd->self.root_hub->rx_lanes = 2; 5402 hcd->self.root_hub->tx_lanes = 2; 5403 hcd->self.root_hub->ssp_rate = USB_SSP_GEN_2x2; 5404 break; 5405 case 1: 5406 hcd->speed = HCD_USB31; 5407 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS; 5408 hcd->self.root_hub->ssp_rate = USB_SSP_GEN_2x1; 5409 break; 5410 } 5411 xhci_info(xhci, "Host supports USB 3.%x %sSuperSpeed\n", 5412 minor_rev, minor_rev ? "Enhanced " : ""); 5413 5414 xhci->usb3_rhub.hcd = hcd; 5415 } 5416 5417 int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks) 5418 { 5419 struct xhci_hcd *xhci; 5420 /* 5421 * TODO: Check with DWC3 clients for sysdev according to 5422 * quirks 5423 */ 5424 struct device *dev = hcd->self.sysdev; 5425 int retval; 5426 u32 hcs_params1; 5427 5428 /* Accept arbitrarily long scatter-gather lists */ 5429 hcd->self.sg_tablesize = ~0; 5430 5431 /* support to build packet from discontinuous buffers */ 5432 hcd->self.no_sg_constraint = 1; 5433 5434 /* XHCI controllers don't stop the ep queue on short packets :| */ 5435 hcd->self.no_stop_on_short = 1; 5436 5437 xhci = hcd_to_xhci(hcd); 5438 5439 if (!usb_hcd_is_primary_hcd(hcd)) { 5440 xhci_hcd_init_usb3_data(xhci, hcd); 5441 return 0; 5442 } 5443 5444 mutex_init(&xhci->mutex); 5445 xhci->main_hcd = hcd; 5446 xhci->cap_regs = hcd->regs; 5447 xhci->op_regs = hcd->regs + 5448 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)); 5449 xhci->run_regs = hcd->regs + 5450 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK); 5451 /* Cache read-only capability registers */ 5452 hcs_params1 = readl(&xhci->cap_regs->hcs_params1); 5453 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2); 5454 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3); 5455 xhci->hci_version = HC_VERSION(readl(&xhci->cap_regs->hc_capbase)); 5456 xhci->hcc_params = readl(&xhci->cap_regs->hcc_params); 5457 if (xhci->hci_version > 0x100) 5458 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2); 5459 5460 xhci->max_slots = min(HCS_MAX_SLOTS(hcs_params1), MAX_HC_SLOTS); 5461 xhci->max_ports = min(HCS_MAX_PORTS(hcs_params1), MAX_HC_PORTS); 5462 /* xhci-plat or xhci-pci might have set max_interrupters already */ 5463 if (!xhci->max_interrupters) 5464 xhci->max_interrupters = min(HCS_MAX_INTRS(hcs_params1), MAX_HC_INTRS); 5465 else if (xhci->max_interrupters > HCS_MAX_INTRS(hcs_params1)) 5466 xhci->max_interrupters = HCS_MAX_INTRS(hcs_params1); 5467 5468 xhci->quirks |= quirks; 5469 5470 if (get_quirks) 5471 get_quirks(dev, xhci); 5472 5473 /* In xhci controllers which follow xhci 1.0 spec gives a spurious 5474 * success event after a short transfer. This quirk will ignore such 5475 * spurious event. 5476 */ 5477 if (xhci->hci_version > 0x96) 5478 xhci->quirks |= XHCI_SPURIOUS_SUCCESS; 5479 5480 if (xhci->hci_version == 0x95 && link_quirk) { 5481 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits"); 5482 xhci->quirks |= XHCI_LINK_TRB_QUIRK; 5483 } 5484 5485 /* Make sure the HC is halted. */ 5486 retval = xhci_halt(xhci); 5487 if (retval) 5488 return retval; 5489 5490 xhci_zero_64b_regs(xhci); 5491 5492 xhci_dbg(xhci, "Resetting HCD\n"); 5493 /* Reset the internal HC memory state and registers. */ 5494 retval = xhci_reset(xhci, XHCI_RESET_LONG_USEC); 5495 if (retval) 5496 return retval; 5497 xhci_dbg(xhci, "Reset complete\n"); 5498 5499 /* 5500 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0) 5501 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit 5502 * address memory pointers actually. So, this driver clears the AC64 5503 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev, 5504 * DMA_BIT_MASK(32)) in this xhci_gen_setup(). 5505 */ 5506 if (xhci->quirks & XHCI_NO_64BIT_SUPPORT) 5507 xhci->hcc_params &= ~BIT(0); 5508 5509 /* Set dma_mask and coherent_dma_mask to 64-bits, 5510 * if xHC supports 64-bit addressing */ 5511 if ((xhci->hcc_params & HCC_64BIT_ADDR) && 5512 !dma_set_mask(dev, DMA_BIT_MASK(64))) { 5513 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n"); 5514 dma_set_coherent_mask(dev, DMA_BIT_MASK(64)); 5515 } else { 5516 /* 5517 * This is to avoid error in cases where a 32-bit USB 5518 * controller is used on a 64-bit capable system. 5519 */ 5520 retval = dma_set_mask(dev, DMA_BIT_MASK(32)); 5521 if (retval) 5522 return retval; 5523 xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n"); 5524 dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); 5525 } 5526 5527 spin_lock_init(&xhci->lock); 5528 INIT_LIST_HEAD(&xhci->cmd_list); 5529 INIT_DELAYED_WORK(&xhci->cmd_timer, xhci_handle_command_timeout); 5530 init_completion(&xhci->cmd_ring_stop_completion); 5531 xhci_hcd_page_size(xhci); 5532 5533 /* Allocate xHCI data structures */ 5534 retval = xhci_mem_init(xhci, GFP_KERNEL); 5535 if (retval) 5536 return retval; 5537 5538 /* Initialize HCD and host controller data structures */ 5539 xhci_init(hcd); 5540 5541 if (xhci_hcd_is_usb3(hcd)) 5542 xhci_hcd_init_usb3_data(xhci, hcd); 5543 else 5544 xhci_hcd_init_usb2_data(xhci, hcd); 5545 5546 xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%016llx\n", 5547 xhci->hcc_params, xhci->hci_version, xhci->quirks); 5548 5549 return 0; 5550 } 5551 EXPORT_SYMBOL_GPL(xhci_gen_setup); 5552 5553 static void xhci_clear_tt_buffer_complete(struct usb_hcd *hcd, 5554 struct usb_host_endpoint *ep) 5555 { 5556 struct xhci_hcd *xhci; 5557 struct usb_device *udev; 5558 unsigned int slot_id; 5559 unsigned int ep_index; 5560 unsigned long flags; 5561 5562 xhci = hcd_to_xhci(hcd); 5563 5564 spin_lock_irqsave(&xhci->lock, flags); 5565 udev = (struct usb_device *)ep->hcpriv; 5566 slot_id = udev->slot_id; 5567 ep_index = xhci_get_endpoint_index(&ep->desc); 5568 5569 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_CLEARING_TT; 5570 xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index); 5571 spin_unlock_irqrestore(&xhci->lock, flags); 5572 } 5573 5574 static const struct hc_driver xhci_hc_driver = { 5575 .description = "xhci-hcd", 5576 .product_desc = "xHCI Host Controller", 5577 .hcd_priv_size = sizeof(struct xhci_hcd), 5578 5579 /* 5580 * generic hardware linkage 5581 */ 5582 .irq = xhci_irq, 5583 .flags = HCD_MEMORY | HCD_DMA | HCD_USB3 | HCD_SHARED | 5584 HCD_BH, 5585 5586 /* 5587 * basic lifecycle operations 5588 */ 5589 .reset = NULL, /* set in xhci_init_driver() */ 5590 .start = xhci_run, 5591 .stop = xhci_stop, 5592 .shutdown = xhci_shutdown, 5593 5594 /* 5595 * managing i/o requests and associated device resources 5596 */ 5597 .map_urb_for_dma = xhci_map_urb_for_dma, 5598 .unmap_urb_for_dma = xhci_unmap_urb_for_dma, 5599 .urb_enqueue = xhci_urb_enqueue, 5600 .urb_dequeue = xhci_urb_dequeue, 5601 .alloc_dev = xhci_alloc_dev, 5602 .free_dev = xhci_free_dev, 5603 .alloc_streams = xhci_alloc_streams, 5604 .free_streams = xhci_free_streams, 5605 .add_endpoint = xhci_add_endpoint, 5606 .drop_endpoint = xhci_drop_endpoint, 5607 .endpoint_disable = xhci_endpoint_disable, 5608 .endpoint_reset = xhci_endpoint_reset, 5609 .check_bandwidth = xhci_check_bandwidth, 5610 .reset_bandwidth = xhci_reset_bandwidth, 5611 .address_device = xhci_address_device, 5612 .enable_device = xhci_enable_device, 5613 .update_hub_device = xhci_update_hub_device, 5614 .reset_device = xhci_discover_or_reset_device, 5615 5616 /* 5617 * scheduling support 5618 */ 5619 .get_frame_number = xhci_get_frame, 5620 5621 /* 5622 * root hub support 5623 */ 5624 .hub_control = xhci_hub_control, 5625 .hub_status_data = xhci_hub_status_data, 5626 .bus_suspend = xhci_bus_suspend, 5627 .bus_resume = xhci_bus_resume, 5628 .get_resuming_ports = xhci_get_resuming_ports, 5629 5630 /* 5631 * call back when device connected and addressed 5632 */ 5633 .update_device = xhci_update_device, 5634 .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm, 5635 .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout, 5636 .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout, 5637 .find_raw_port_number = xhci_find_raw_port_number, 5638 .clear_tt_buffer_complete = xhci_clear_tt_buffer_complete, 5639 }; 5640 5641 void xhci_init_driver(struct hc_driver *drv, 5642 const struct xhci_driver_overrides *over) 5643 { 5644 BUG_ON(!over); 5645 5646 /* Copy the generic table to drv then apply the overrides */ 5647 *drv = xhci_hc_driver; 5648 5649 if (over) { 5650 drv->hcd_priv_size += over->extra_priv_size; 5651 if (over->reset) 5652 drv->reset = over->reset; 5653 if (over->start) 5654 drv->start = over->start; 5655 if (over->add_endpoint) 5656 drv->add_endpoint = over->add_endpoint; 5657 if (over->drop_endpoint) 5658 drv->drop_endpoint = over->drop_endpoint; 5659 if (over->check_bandwidth) 5660 drv->check_bandwidth = over->check_bandwidth; 5661 if (over->reset_bandwidth) 5662 drv->reset_bandwidth = over->reset_bandwidth; 5663 if (over->update_hub_device) 5664 drv->update_hub_device = over->update_hub_device; 5665 if (over->hub_control) 5666 drv->hub_control = over->hub_control; 5667 } 5668 } 5669 EXPORT_SYMBOL_GPL(xhci_init_driver); 5670 5671 MODULE_DESCRIPTION(DRIVER_DESC); 5672 MODULE_AUTHOR(DRIVER_AUTHOR); 5673 MODULE_LICENSE("GPL"); 5674 5675 static int __init xhci_hcd_init(void) 5676 { 5677 /* 5678 * Check the compiler generated sizes of structures that must be laid 5679 * out in specific ways for hardware access. 5680 */ 5681 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8); 5682 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8); 5683 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8); 5684 /* xhci_device_control has eight fields, and also 5685 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx 5686 */ 5687 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8); 5688 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8); 5689 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8); 5690 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 8*32/8); 5691 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8); 5692 /* xhci_run_regs has eight fields and embeds 1024 xhci_intr_regs */ 5693 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*1024)*32/8); 5694 5695 if (usb_disabled()) 5696 return -ENODEV; 5697 5698 xhci_debugfs_create_root(); 5699 xhci_dbc_init(); 5700 5701 return 0; 5702 } 5703 5704 /* 5705 * If an init function is provided, an exit function must also be provided 5706 * to allow module unload. 5707 */ 5708 static void __exit xhci_hcd_fini(void) 5709 { 5710 xhci_debugfs_remove_root(); 5711 xhci_dbc_exit(); 5712 } 5713 5714 module_init(xhci_hcd_init); 5715 module_exit(xhci_hcd_fini); 5716