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