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