1/* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */ 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 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License version 2 as 12 * published by the Free Software Foundation. 13 * 14 * This program is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17 * for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software Foundation, 21 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 */ 23 24#include <linux/pci.h> 25#include <linux/irq.h> 26#include <linux/log2.h> 27#include <linux/module.h> 28#include <linux/moduleparam.h> 29#include <linux/slab.h> 30 31#include "xhci.h" 32 33#define DRIVER_AUTHOR "Sarah Sharp" 34#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver" 35 36/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */ 37static int link_quirk; 38module_param(link_quirk, int, S_IRUGO | S_IWUSR); 39MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB"); 40 41/* TODO: copied from ehci-hcd.c - can this be refactored? */ 42/* 43 * handshake - spin reading hc until handshake completes or fails 44 * @ptr: address of hc register to be read 45 * @mask: bits to look at in result of read 46 * @done: value of those bits when handshake succeeds 47 * @usec: timeout in microseconds 48 * 49 * Returns negative errno, or zero on success 50 * 51 * Success happens when the "mask" bits have the specified value (hardware 52 * handshake done). There are two failure modes: "usec" have passed (major 53 * hardware flakeout), or the register reads as all-ones (hardware removed). 54 */ 55static int handshake(struct xhci_hcd *xhci, void __iomem *ptr, 56 u32 mask, u32 done, int usec) 57{ 58 u32 result; 59 60 do { 61 result = xhci_readl(xhci, ptr); 62 if (result == ~(u32)0) /* card removed */ 63 return -ENODEV; 64 result &= mask; 65 if (result == done) 66 return 0; 67 udelay(1); 68 usec--; 69 } while (usec > 0); 70 return -ETIMEDOUT; 71} 72 73/* 74 * Disable interrupts and begin the xHCI halting process. 75 */ 76void xhci_quiesce(struct xhci_hcd *xhci) 77{ 78 u32 halted; 79 u32 cmd; 80 u32 mask; 81 82 mask = ~(XHCI_IRQS); 83 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT; 84 if (!halted) 85 mask &= ~CMD_RUN; 86 87 cmd = xhci_readl(xhci, &xhci->op_regs->command); 88 cmd &= mask; 89 xhci_writel(xhci, cmd, &xhci->op_regs->command); 90} 91 92int xhci_halt(struct xhci_hcd *xhci) 93{ 94 xhci_dbg(xhci, "// Halt the HC\n"); 95 xhci_quiesce(xhci); 96 97 return handshake(xhci, &xhci->op_regs->status, 98 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC); 99} 100 101#ifdef CONFIG_BCM47XX 102int xhci_fake_doorbell(struct xhci_hcd *xhci, int slot_id) 103{ 104 unsigned int temp1, ret; 105 106 /* alloc a virt device for slot */ 107 if (!xhci_alloc_virt_device(xhci, slot_id, 0, GFP_NOIO)) { 108 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n"); 109 return 1; 110 } 111 112 /* ring fake doorbell for slot_id ep 0 */ 113 xhci_ring_ep_doorbell(xhci, slot_id, 0, 0); 114 mdelay(1); 115 116 /* read the status register to check if HSE is set or not? */ 117 temp1 = xhci_readl(xhci, &xhci->op_regs->status); 118 xhci_dbg(xhci, "op reg status = %x\n",temp1); 119 120 /* clear HSE if set */ 121 if(temp1 & STS_FATAL) { 122 xhci_dbg(xhci, "HSE problem detected\n"); 123 temp1 &= ~(0x1fff); 124 temp1 |= STS_FATAL; 125 xhci_dbg(xhci, "temp1=%x\n",temp1); 126 xhci_writel(xhci, temp1, &xhci->op_regs->status); 127 mdelay(1); 128 temp1 = xhci_readl(xhci, &xhci->op_regs->status); 129 xhci_dbg(xhci, "After clear op reg status=%x\n", temp1); 130 } 131 132 /* Free virt device */ 133 xhci_free_virt_device(xhci, slot_id); 134 135 /* Run the controller if needed */ 136 temp1 = xhci_readl(xhci, &xhci->op_regs->command); 137 if (temp1 & CMD_RUN) 138 return 0; 139 temp1 |= (CMD_RUN); 140 141 xhci_writel(xhci, temp1, &xhci->op_regs->command); 142 /* 143 * Wait for the HCHalted Status bit to be 0 to indicate the host is running. 144 */ 145 ret = handshake(xhci, &xhci->op_regs->status, 146 STS_HALT, 0, XHCI_MAX_HALT_USEC); 147 148 if (ret == -ETIMEDOUT) { 149 xhci_err(xhci, "Host took too long to start, " 150 "waited %u microseconds.\n", 151 XHCI_MAX_HALT_USEC); 152 return 1; 153 } 154 155 return 0; 156} 157#endif /* CONFIG_BCM47XX */ 158 159/* 160 * Set the run bit and wait for the host to be running. 161 */ 162int xhci_start(struct xhci_hcd *xhci) 163{ 164 u32 temp; 165 int ret; 166 167 temp = xhci_readl(xhci, &xhci->op_regs->command); 168 temp |= (CMD_RUN); 169 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n", 170 temp); 171 xhci_writel(xhci, temp, &xhci->op_regs->command); 172 173 /* 174 * Wait for the HCHalted Status bit to be 0 to indicate the host is 175 * running. 176 */ 177 ret = handshake(xhci, &xhci->op_regs->status, 178 STS_HALT, 0, XHCI_MAX_HALT_USEC); 179 if (ret == -ETIMEDOUT) 180 xhci_err(xhci, "Host took too long to start, " 181 "waited %u microseconds.\n", 182 XHCI_MAX_HALT_USEC); 183 184#ifdef CONFIG_BCM47XX 185 xhci_fake_doorbell(xhci, 1); 186#endif /* CONFIG_BCM47XX */ 187 188 return ret; 189} 190 191/* 192 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT. 193 * 194 * This resets pipelines, timers, counters, state machines, etc. 195 * Transactions will be terminated immediately, and operational registers 196 * will be set to their defaults. 197 */ 198int xhci_reset(struct xhci_hcd *xhci) 199{ 200 u32 command; 201 u32 state; 202 int ret; 203 204 state = xhci_readl(xhci, &xhci->op_regs->status); 205 if ((state & STS_HALT) == 0) { 206 xhci_warn(xhci, "Host controller not halted, aborting reset.\n"); 207 return 0; 208 } 209 210 xhci_dbg(xhci, "// Reset the HC\n"); 211 command = xhci_readl(xhci, &xhci->op_regs->command); 212 command |= CMD_RESET; 213 xhci_writel(xhci, command, &xhci->op_regs->command); 214 xhci_to_hcd(xhci)->state = HC_STATE_HALT; 215 216 ret = handshake(xhci, &xhci->op_regs->command, 217 CMD_RESET, 0, 250 * 1000); 218 if (ret) 219 return ret; 220 221 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n"); 222 /* 223 * xHCI cannot write to any doorbells or operational registers other 224 * than status until the "Controller Not Ready" flag is cleared. 225 */ 226 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000); 227} 228 229/* 230 * Free IRQs 231 * free all IRQs request 232 */ 233static void xhci_free_irq(struct xhci_hcd *xhci) 234{ 235 int i; 236 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 237 238 /* return if using legacy interrupt */ 239 if (xhci_to_hcd(xhci)->irq >= 0) 240 return; 241 242 if (xhci->msix_entries) { 243 for (i = 0; i < xhci->msix_count; i++) 244 if (xhci->msix_entries[i].vector) 245 free_irq(xhci->msix_entries[i].vector, 246 xhci_to_hcd(xhci)); 247 } else if (pdev->irq >= 0) 248 free_irq(pdev->irq, xhci_to_hcd(xhci)); 249 250 return; 251} 252 253/* 254 * Set up MSI 255 */ 256static int xhci_setup_msi(struct xhci_hcd *xhci) 257{ 258 int ret; 259 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 260 261 ret = pci_enable_msi(pdev); 262 if (ret) { 263 xhci_err(xhci, "failed to allocate MSI entry\n"); 264 return ret; 265 } 266 267 ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq, 268 0, "xhci_hcd", xhci_to_hcd(xhci)); 269 if (ret) { 270 xhci_err(xhci, "disable MSI interrupt\n"); 271 pci_disable_msi(pdev); 272 } 273 274 return ret; 275} 276 277/* 278 * Set up MSI-X 279 */ 280static int xhci_setup_msix(struct xhci_hcd *xhci) 281{ 282 int i, ret = 0; 283 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 284 285 /* 286 * calculate number of msi-x vectors supported. 287 * - HCS_MAX_INTRS: the max number of interrupts the host can handle, 288 * with max number of interrupters based on the xhci HCSPARAMS1. 289 * - num_online_cpus: maximum msi-x vectors per CPUs core. 290 * Add additional 1 vector to ensure always available interrupt. 291 */ 292 xhci->msix_count = min(num_online_cpus() + 1, 293 HCS_MAX_INTRS(xhci->hcs_params1)); 294 295 xhci->msix_entries = 296 kmalloc((sizeof(struct msix_entry))*xhci->msix_count, 297 GFP_KERNEL); 298 if (!xhci->msix_entries) { 299 xhci_err(xhci, "Failed to allocate MSI-X entries\n"); 300 return -ENOMEM; 301 } 302 303 for (i = 0; i < xhci->msix_count; i++) { 304 xhci->msix_entries[i].entry = i; 305 xhci->msix_entries[i].vector = 0; 306 } 307 308 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count); 309 if (ret) { 310 xhci_err(xhci, "Failed to enable MSI-X\n"); 311 goto free_entries; 312 } 313 314 for (i = 0; i < xhci->msix_count; i++) { 315 ret = request_irq(xhci->msix_entries[i].vector, 316 (irq_handler_t)xhci_msi_irq, 317 0, "xhci_hcd", xhci_to_hcd(xhci)); 318 if (ret) 319 goto disable_msix; 320 } 321 322 return ret; 323 324disable_msix: 325 xhci_err(xhci, "disable MSI-X interrupt\n"); 326 xhci_free_irq(xhci); 327 pci_disable_msix(pdev); 328free_entries: 329 kfree(xhci->msix_entries); 330 xhci->msix_entries = NULL; 331 return ret; 332} 333 334/* Free any IRQs and disable MSI-X */ 335static void xhci_cleanup_msix(struct xhci_hcd *xhci) 336{ 337 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 338 339 xhci_free_irq(xhci); 340 341 if (xhci->msix_entries) { 342 pci_disable_msix(pdev); 343 kfree(xhci->msix_entries); 344 xhci->msix_entries = NULL; 345 } else { 346 pci_disable_msi(pdev); 347 } 348 349 return; 350} 351 352/* 353 * Initialize memory for HCD and xHC (one-time init). 354 * 355 * Program the PAGESIZE register, initialize the device context array, create 356 * device contexts (?), set up a command ring segment (or two?), create event 357 * ring (one for now). 358 */ 359int xhci_init(struct usb_hcd *hcd) 360{ 361 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 362 int retval = 0; 363 364 xhci_dbg(xhci, "xhci_init\n"); 365 spin_lock_init(&xhci->lock); 366 if (link_quirk) { 367 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n"); 368 xhci->quirks |= XHCI_LINK_TRB_QUIRK; 369 } else { 370 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n"); 371 } 372 retval = xhci_mem_init(xhci, GFP_KERNEL); 373 xhci_dbg(xhci, "Finished xhci_init\n"); 374 375 return retval; 376} 377 378/*-------------------------------------------------------------------------*/ 379 380 381#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING 382void xhci_event_ring_work(unsigned long arg) 383{ 384 unsigned long flags; 385 int temp; 386 u64 temp_64; 387 struct xhci_hcd *xhci = (struct xhci_hcd *) arg; 388 int i, j; 389 390 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies); 391 392 spin_lock_irqsave(&xhci->lock, flags); 393 temp = xhci_readl(xhci, &xhci->op_regs->status); 394 xhci_dbg(xhci, "op reg status = 0x%x\n", temp); 395 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) { 396 xhci_dbg(xhci, "HW died, polling stopped.\n"); 397 spin_unlock_irqrestore(&xhci->lock, flags); 398 return; 399 } 400 401 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); 402 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp); 403 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled); 404 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask); 405 xhci->error_bitmask = 0; 406 xhci_dbg(xhci, "Event ring:\n"); 407 xhci_debug_segment(xhci, xhci->event_ring->deq_seg); 408 xhci_dbg_ring_ptrs(xhci, xhci->event_ring); 409 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); 410 temp_64 &= ~ERST_PTR_MASK; 411 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64); 412 xhci_dbg(xhci, "Command ring:\n"); 413 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg); 414 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring); 415 xhci_dbg_cmd_ptrs(xhci); 416 for (i = 0; i < MAX_HC_SLOTS; ++i) { 417 if (!xhci->devs[i]) 418 continue; 419 for (j = 0; j < 31; ++j) { 420 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]); 421 } 422 } 423 424 if (xhci->noops_submitted != NUM_TEST_NOOPS) 425 if (xhci_setup_one_noop(xhci)) 426 xhci_ring_cmd_db(xhci); 427 spin_unlock_irqrestore(&xhci->lock, flags); 428 429 if (!xhci->zombie) 430 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ); 431 else 432 xhci_dbg(xhci, "Quit polling the event ring.\n"); 433} 434#endif 435 436/* 437 * Start the HC after it was halted. 438 * 439 * This function is called by the USB core when the HC driver is added. 440 * Its opposite is xhci_stop(). 441 * 442 * xhci_init() must be called once before this function can be called. 443 * Reset the HC, enable device slot contexts, program DCBAAP, and 444 * set command ring pointer and event ring pointer. 445 * 446 * Setup MSI-X vectors and enable interrupts. 447 */ 448int xhci_run(struct usb_hcd *hcd) 449{ 450 u32 temp; 451 u64 temp_64; 452 u32 ret; 453 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 454 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); 455 void (*doorbell)(struct xhci_hcd *) = NULL; 456 457 hcd->uses_new_polling = 1; 458 459 xhci_dbg(xhci, "xhci_run\n"); 460 /* unregister the legacy interrupt */ 461 if (hcd->irq) 462 free_irq(hcd->irq, hcd); 463 hcd->irq = -1; 464 465 ret = xhci_setup_msix(xhci); 466 if (ret) 467 /* fall back to msi*/ 468 ret = xhci_setup_msi(xhci); 469 470 if (ret) { 471 /* fall back to legacy interrupt*/ 472 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED, 473 hcd->irq_descr, hcd); 474 if (ret) { 475 xhci_err(xhci, "request interrupt %d failed\n", 476 pdev->irq); 477 return ret; 478 } 479 hcd->irq = pdev->irq; 480 } 481 482#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING 483 init_timer(&xhci->event_ring_timer); 484 xhci->event_ring_timer.data = (unsigned long) xhci; 485 xhci->event_ring_timer.function = xhci_event_ring_work; 486 /* Poll the event ring */ 487 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ; 488 xhci->zombie = 0; 489 xhci_dbg(xhci, "Setting event ring polling timer\n"); 490 add_timer(&xhci->event_ring_timer); 491#endif 492 493 xhci_dbg(xhci, "Command ring memory map follows:\n"); 494 xhci_debug_ring(xhci, xhci->cmd_ring); 495 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring); 496 xhci_dbg_cmd_ptrs(xhci); 497 498 xhci_dbg(xhci, "ERST memory map follows:\n"); 499 xhci_dbg_erst(xhci, &xhci->erst); 500 xhci_dbg(xhci, "Event ring:\n"); 501 xhci_debug_ring(xhci, xhci->event_ring); 502 xhci_dbg_ring_ptrs(xhci, xhci->event_ring); 503 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); 504 temp_64 &= ~ERST_PTR_MASK; 505 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64); 506 507 xhci_dbg(xhci, "// Set the interrupt modulation register\n"); 508 temp = xhci_readl(xhci, &xhci->ir_set->irq_control); 509 temp &= ~ER_IRQ_INTERVAL_MASK; 510 temp |= (u32) 160; 511 xhci_writel(xhci, temp, &xhci->ir_set->irq_control); 512 513 /* Set the HCD state before we enable the irqs */ 514 hcd->state = HC_STATE_RUNNING; 515 temp = xhci_readl(xhci, &xhci->op_regs->command); 516 temp |= (CMD_EIE); 517 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n", 518 temp); 519 xhci_writel(xhci, temp, &xhci->op_regs->command); 520 521 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); 522 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n", 523 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp)); 524 xhci_writel(xhci, ER_IRQ_ENABLE(temp), 525 &xhci->ir_set->irq_pending); 526 xhci_print_ir_set(xhci, xhci->ir_set, 0); 527 528 if (NUM_TEST_NOOPS > 0) 529 doorbell = xhci_setup_one_noop(xhci); 530 if (xhci->quirks & XHCI_NEC_HOST) 531 xhci_queue_vendor_command(xhci, 0, 0, 0, 532 TRB_TYPE(TRB_NEC_GET_FW)); 533 534 if (xhci_start(xhci)) { 535 xhci_halt(xhci); 536 return -ENODEV; 537 } 538 539 if (doorbell) 540 (*doorbell)(xhci); 541 if (xhci->quirks & XHCI_NEC_HOST) 542 xhci_ring_cmd_db(xhci); 543 544 xhci_dbg(xhci, "Finished xhci_run\n"); 545 return 0; 546} 547 548/* 549 * Stop xHCI driver. 550 * 551 * This function is called by the USB core when the HC driver is removed. 552 * Its opposite is xhci_run(). 553 * 554 * Disable device contexts, disable IRQs, and quiesce the HC. 555 * Reset the HC, finish any completed transactions, and cleanup memory. 556 */ 557void xhci_stop(struct usb_hcd *hcd) 558{ 559 u32 temp; 560 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 561 562 spin_lock_irq(&xhci->lock); 563 xhci_halt(xhci); 564 xhci_reset(xhci); 565 spin_unlock_irq(&xhci->lock); 566 567 xhci_cleanup_msix(xhci); 568 569#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING 570 /* Tell the event ring poll function not to reschedule */ 571 xhci->zombie = 1; 572 del_timer_sync(&xhci->event_ring_timer); 573#endif 574 575 xhci_dbg(xhci, "// Disabling event ring interrupts\n"); 576 temp = xhci_readl(xhci, &xhci->op_regs->status); 577 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status); 578 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); 579 xhci_writel(xhci, ER_IRQ_DISABLE(temp), 580 &xhci->ir_set->irq_pending); 581 xhci_print_ir_set(xhci, xhci->ir_set, 0); 582 583 xhci_dbg(xhci, "cleaning up memory\n"); 584 xhci_mem_cleanup(xhci); 585 xhci_dbg(xhci, "xhci_stop completed - status = %x\n", 586 xhci_readl(xhci, &xhci->op_regs->status)); 587} 588 589/* 590 * Shutdown HC (not bus-specific) 591 * 592 * This is called when the machine is rebooting or halting. We assume that the 593 * machine will be powered off, and the HC's internal state will be reset. 594 * Don't bother to free memory. 595 */ 596void xhci_shutdown(struct usb_hcd *hcd) 597{ 598 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 599 600 spin_lock_irq(&xhci->lock); 601 xhci_halt(xhci); 602 spin_unlock_irq(&xhci->lock); 603 604 xhci_cleanup_msix(xhci); 605 606 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n", 607 xhci_readl(xhci, &xhci->op_regs->status)); 608} 609 610/*-------------------------------------------------------------------------*/ 611 612/** 613 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and 614 * HCDs. Find the index for an endpoint given its descriptor. Use the return 615 * value to right shift 1 for the bitmask. 616 * 617 * Index = (epnum * 2) + direction - 1, 618 * where direction = 0 for OUT, 1 for IN. 619 * For control endpoints, the IN index is used (OUT index is unused), so 620 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2) 621 */ 622unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc) 623{ 624 unsigned int index; 625 if (usb_endpoint_xfer_control(desc)) 626 index = (unsigned int) (usb_endpoint_num(desc)*2); 627 else 628 index = (unsigned int) (usb_endpoint_num(desc)*2) + 629 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1; 630 return index; 631} 632 633/* Find the flag for this endpoint (for use in the control context). Use the 634 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is 635 * bit 1, etc. 636 */ 637unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc) 638{ 639 return 1 << (xhci_get_endpoint_index(desc) + 1); 640} 641 642/* Find the flag for this endpoint (for use in the control context). Use the 643 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is 644 * bit 1, etc. 645 */ 646unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index) 647{ 648 return 1 << (ep_index + 1); 649} 650 651/* Compute the last valid endpoint context index. Basically, this is the 652 * endpoint index plus one. For slot contexts with more than valid endpoint, 653 * we find the most significant bit set in the added contexts flags. 654 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000 655 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one. 656 */ 657unsigned int xhci_last_valid_endpoint(u32 added_ctxs) 658{ 659 return fls(added_ctxs) - 1; 660} 661 662/* Returns 1 if the arguments are OK; 663 * returns 0 this is a root hub; returns -EINVAL for NULL pointers. 664 */ 665int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev, 666 struct usb_host_endpoint *ep, int check_ep, const char *func) { 667 if (!hcd || (check_ep && !ep) || !udev) { 668 printk(KERN_DEBUG "xHCI %s called with invalid args\n", 669 func); 670 return -EINVAL; 671 } 672 if (!udev->parent) { 673 printk(KERN_DEBUG "xHCI %s called for root hub\n", 674 func); 675 return 0; 676 } 677 if (!udev->slot_id) { 678 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n", 679 func); 680 return -EINVAL; 681 } 682 return 1; 683} 684 685static int xhci_configure_endpoint(struct xhci_hcd *xhci, 686 struct usb_device *udev, struct xhci_command *command, 687 bool ctx_change, bool must_succeed); 688 689/* 690 * Full speed devices may have a max packet size greater than 8 bytes, but the 691 * USB core doesn't know that until it reads the first 8 bytes of the 692 * descriptor. If the usb_device's max packet size changes after that point, 693 * we need to issue an evaluate context command and wait on it. 694 */ 695static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id, 696 unsigned int ep_index, struct urb *urb) 697{ 698 struct xhci_container_ctx *in_ctx; 699 struct xhci_container_ctx *out_ctx; 700 struct xhci_input_control_ctx *ctrl_ctx; 701 struct xhci_ep_ctx *ep_ctx; 702 int max_packet_size; 703 int hw_max_packet_size; 704 int ret = 0; 705 706 out_ctx = xhci->devs[slot_id]->out_ctx; 707 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); 708 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2); 709 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize; 710 if (hw_max_packet_size != max_packet_size) { 711 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n"); 712 xhci_dbg(xhci, "Max packet size in usb_device = %d\n", 713 max_packet_size); 714 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n", 715 hw_max_packet_size); 716 xhci_dbg(xhci, "Issuing evaluate context command.\n"); 717 718 /* Set up the modified control endpoint 0 */ 719 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx, 720 xhci->devs[slot_id]->out_ctx, ep_index); 721 in_ctx = xhci->devs[slot_id]->in_ctx; 722 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index); 723 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK; 724 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size); 725 726 /* Set up the input context flags for the command */ 727 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); 728 ctrl_ctx->add_flags = EP0_FLAG; 729 ctrl_ctx->drop_flags = 0; 730 731 xhci_dbg(xhci, "Slot %d input context\n", slot_id); 732 xhci_dbg_ctx(xhci, in_ctx, ep_index); 733 xhci_dbg(xhci, "Slot %d output context\n", slot_id); 734 xhci_dbg_ctx(xhci, out_ctx, ep_index); 735 736 ret = xhci_configure_endpoint(xhci, urb->dev, NULL, 737 true, false); 738 739 /* Clean up the input context for later use by bandwidth 740 * functions. 741 */ 742 ctrl_ctx->add_flags = SLOT_FLAG; 743 } 744 return ret; 745} 746 747/* 748 * non-error returns are a promise to giveback() the urb later 749 * we drop ownership so next owner (or urb unlink) can get it 750 */ 751int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags) 752{ 753 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 754 unsigned long flags; 755 int ret = 0; 756 unsigned int slot_id, ep_index; 757 struct urb_priv *urb_priv; 758 int size, i; 759 760 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0) 761 return -EINVAL; 762 763 slot_id = urb->dev->slot_id; 764 ep_index = xhci_get_endpoint_index(&urb->ep->desc); 765 766 if (!xhci->devs || !xhci->devs[slot_id]) { 767 if (!in_interrupt()) 768 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n"); 769 ret = -EINVAL; 770 goto exit; 771 } 772 if (!HCD_HW_ACCESSIBLE(hcd)) { 773 if (!in_interrupt()) 774 xhci_dbg(xhci, "urb submitted during PCI suspend\n"); 775 ret = -ESHUTDOWN; 776 goto exit; 777 } 778 779 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) 780 size = urb->number_of_packets; 781 else 782 size = 1; 783 784 urb_priv = kzalloc(sizeof(struct urb_priv) + 785 size * sizeof(struct xhci_td *), mem_flags); 786 if (!urb_priv) 787 return -ENOMEM; 788 789 for (i = 0; i < size; i++) { 790 urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags); 791 if (!urb_priv->td[i]) { 792 urb_priv->length = i; 793 xhci_urb_free_priv(xhci, urb_priv); 794 return -ENOMEM; 795 } 796 } 797 798 urb_priv->length = size; 799 urb_priv->td_cnt = 0; 800 urb->hcpriv = urb_priv; 801 802 if (usb_endpoint_xfer_control(&urb->ep->desc)) { 803 /* Check to see if the max packet size for the default control 804 * endpoint changed during FS device enumeration 805 */ 806 if (urb->dev->speed == USB_SPEED_FULL) { 807 ret = xhci_check_maxpacket(xhci, slot_id, 808 ep_index, urb); 809 if (ret < 0) 810 return ret; 811 } 812 813 /* We have a spinlock and interrupts disabled, so we must pass 814 * atomic context to this function, which may allocate memory. 815 */ 816 spin_lock_irqsave(&xhci->lock, flags); 817 if (xhci->xhc_state & XHCI_STATE_DYING) 818 goto dying; 819 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb, 820 slot_id, ep_index); 821 spin_unlock_irqrestore(&xhci->lock, flags); 822 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) { 823 spin_lock_irqsave(&xhci->lock, flags); 824 if (xhci->xhc_state & XHCI_STATE_DYING) 825 goto dying; 826 if (xhci->devs[slot_id]->eps[ep_index].ep_state & 827 EP_GETTING_STREAMS) { 828 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep " 829 "is transitioning to using streams.\n"); 830 ret = -EINVAL; 831 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state & 832 EP_GETTING_NO_STREAMS) { 833 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep " 834 "is transitioning to " 835 "not having streams.\n"); 836 ret = -EINVAL; 837 } else { 838 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, 839 slot_id, ep_index); 840 } 841 spin_unlock_irqrestore(&xhci->lock, flags); 842 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) { 843 spin_lock_irqsave(&xhci->lock, flags); 844 if (xhci->xhc_state & XHCI_STATE_DYING) 845 goto dying; 846 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb, 847 slot_id, ep_index); 848 spin_unlock_irqrestore(&xhci->lock, flags); 849 } else { 850 spin_lock_irqsave(&xhci->lock, flags); 851 if (xhci->xhc_state & XHCI_STATE_DYING) 852 goto dying; 853 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb, 854 slot_id, ep_index); 855 spin_unlock_irqrestore(&xhci->lock, flags); 856 } 857exit: 858 return ret; 859dying: 860 xhci_urb_free_priv(xhci, urb_priv); 861 urb->hcpriv = NULL; 862 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for " 863 "non-responsive xHCI host.\n", 864 urb->ep->desc.bEndpointAddress, urb); 865 spin_unlock_irqrestore(&xhci->lock, flags); 866 return -ESHUTDOWN; 867} 868 869/* Get the right ring for the given URB. 870 * If the endpoint supports streams, boundary check the URB's stream ID. 871 * If the endpoint doesn't support streams, return the singular endpoint ring. 872 */ 873static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci, 874 struct urb *urb) 875{ 876 unsigned int slot_id; 877 unsigned int ep_index; 878 unsigned int stream_id; 879 struct xhci_virt_ep *ep; 880 881 slot_id = urb->dev->slot_id; 882 ep_index = xhci_get_endpoint_index(&urb->ep->desc); 883 stream_id = urb->stream_id; 884 ep = &xhci->devs[slot_id]->eps[ep_index]; 885 /* Common case: no streams */ 886 if (!(ep->ep_state & EP_HAS_STREAMS)) 887 return ep->ring; 888 889 if (stream_id == 0) { 890 xhci_warn(xhci, 891 "WARN: Slot ID %u, ep index %u has streams, " 892 "but URB has no stream ID.\n", 893 slot_id, ep_index); 894 return NULL; 895 } 896 897 if (stream_id < ep->stream_info->num_streams) 898 return ep->stream_info->stream_rings[stream_id]; 899 900 xhci_warn(xhci, 901 "WARN: Slot ID %u, ep index %u has " 902 "stream IDs 1 to %u allocated, " 903 "but stream ID %u is requested.\n", 904 slot_id, ep_index, 905 ep->stream_info->num_streams - 1, 906 stream_id); 907 return NULL; 908} 909 910/* 911 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop 912 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC 913 * should pick up where it left off in the TD, unless a Set Transfer Ring 914 * Dequeue Pointer is issued. 915 * 916 * The TRBs that make up the buffers for the canceled URB will be "removed" from 917 * the ring. Since the ring is a contiguous structure, they can't be physically 918 * removed. Instead, there are two options: 919 * 920 * 1) If the HC is in the middle of processing the URB to be canceled, we 921 * simply move the ring's dequeue pointer past those TRBs using the Set 922 * Transfer Ring Dequeue Pointer command. This will be the common case, 923 * when drivers timeout on the last submitted URB and attempt to cancel. 924 * 925 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a 926 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The 927 * HC will need to invalidate the any TRBs it has cached after the stop 928 * endpoint command, as noted in the xHCI 0.95 errata. 929 * 930 * 3) The TD may have completed by the time the Stop Endpoint Command 931 * completes, so software needs to handle that case too. 932 * 933 * This function should protect against the TD enqueueing code ringing the 934 * doorbell while this code is waiting for a Stop Endpoint command to complete. 935 * It also needs to account for multiple cancellations on happening at the same 936 * time for the same endpoint. 937 * 938 * Note that this function can be called in any context, or so says 939 * usb_hcd_unlink_urb() 940 */ 941int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) 942{ 943 unsigned long flags; 944 int ret, i; 945 u32 temp; 946 struct xhci_hcd *xhci; 947 struct urb_priv *urb_priv; 948 struct xhci_td *td; 949 unsigned int ep_index; 950 struct xhci_ring *ep_ring; 951 struct xhci_virt_ep *ep; 952 953 xhci = hcd_to_xhci(hcd); 954 spin_lock_irqsave(&xhci->lock, flags); 955 /* Make sure the URB hasn't completed or been unlinked already */ 956 ret = usb_hcd_check_unlink_urb(hcd, urb, status); 957 if (ret || !urb->hcpriv) 958 goto done; 959 temp = xhci_readl(xhci, &xhci->op_regs->status); 960 if (temp == 0xffffffff) { 961 xhci_dbg(xhci, "HW died, freeing TD.\n"); 962 urb_priv = urb->hcpriv; 963 964 usb_hcd_unlink_urb_from_ep(hcd, urb); 965 spin_unlock_irqrestore(&xhci->lock, flags); 966 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN); 967 xhci_urb_free_priv(xhci, urb_priv); 968 return ret; 969 } 970 if (xhci->xhc_state & XHCI_STATE_DYING) { 971 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on " 972 "non-responsive xHCI host.\n", 973 urb->ep->desc.bEndpointAddress, urb); 974 /* Let the stop endpoint command watchdog timer (which set this 975 * state) finish cleaning up the endpoint TD lists. We must 976 * have caught it in the middle of dropping a lock and giving 977 * back an URB. 978 */ 979 goto done; 980 } 981 982 xhci_dbg(xhci, "Cancel URB %p\n", urb); 983 xhci_dbg(xhci, "Event ring:\n"); 984 xhci_debug_ring(xhci, xhci->event_ring); 985 ep_index = xhci_get_endpoint_index(&urb->ep->desc); 986 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index]; 987 ep_ring = xhci_urb_to_transfer_ring(xhci, urb); 988 if (!ep_ring) { 989 ret = -EINVAL; 990 goto done; 991 } 992 993 xhci_dbg(xhci, "Endpoint ring:\n"); 994 xhci_debug_ring(xhci, ep_ring); 995 996 urb_priv = urb->hcpriv; 997 998 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) { 999 td = urb_priv->td[i]; 1000 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list); 1001 } 1002 1003 /* Queue a stop endpoint command, but only if this is 1004 * the first cancellation to be handled. 1005 */ 1006 if (!(ep->ep_state & EP_HALT_PENDING)) { 1007 ep->ep_state |= EP_HALT_PENDING; 1008 ep->stop_cmds_pending++; 1009 ep->stop_cmd_timer.expires = jiffies + 1010 XHCI_STOP_EP_CMD_TIMEOUT * HZ; 1011 add_timer(&ep->stop_cmd_timer); 1012 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index); 1013 xhci_ring_cmd_db(xhci); 1014 } 1015done: 1016 spin_unlock_irqrestore(&xhci->lock, flags); 1017 return ret; 1018} 1019 1020/* Drop an endpoint from a new bandwidth configuration for this device. 1021 * Only one call to this function is allowed per endpoint before 1022 * check_bandwidth() or reset_bandwidth() must be called. 1023 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will 1024 * add the endpoint to the schedule with possibly new parameters denoted by a 1025 * different endpoint descriptor in usb_host_endpoint. 1026 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is 1027 * not allowed. 1028 * 1029 * The USB core will not allow URBs to be queued to an endpoint that is being 1030 * disabled, so there's no need for mutual exclusion to protect 1031 * the xhci->devs[slot_id] structure. 1032 */ 1033int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev, 1034 struct usb_host_endpoint *ep) 1035{ 1036 struct xhci_hcd *xhci; 1037 struct xhci_container_ctx *in_ctx, *out_ctx; 1038 struct xhci_input_control_ctx *ctrl_ctx; 1039 struct xhci_slot_ctx *slot_ctx; 1040 unsigned int last_ctx; 1041 unsigned int ep_index; 1042 struct xhci_ep_ctx *ep_ctx; 1043 u32 drop_flag; 1044 u32 new_add_flags, new_drop_flags, new_slot_info; 1045 int ret; 1046 1047 ret = xhci_check_args(hcd, udev, ep, 1, __func__); 1048 if (ret <= 0) 1049 return ret; 1050 xhci = hcd_to_xhci(hcd); 1051 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); 1052 1053 drop_flag = xhci_get_endpoint_flag(&ep->desc); 1054 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) { 1055 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n", 1056 __func__, drop_flag); 1057 return 0; 1058 } 1059 1060 if (!xhci->devs || !xhci->devs[udev->slot_id]) { 1061 xhci_warn(xhci, "xHCI %s called with unaddressed device\n", 1062 __func__); 1063 return -EINVAL; 1064 } 1065 1066 in_ctx = xhci->devs[udev->slot_id]->in_ctx; 1067 out_ctx = xhci->devs[udev->slot_id]->out_ctx; 1068 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); 1069 ep_index = xhci_get_endpoint_index(&ep->desc); 1070 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); 1071 /* If the HC already knows the endpoint is disabled, 1072 * or the HCD has noted it is disabled, ignore this request 1073 */ 1074 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED || 1075 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) { 1076 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n", 1077 __func__, ep); 1078 return 0; 1079 } 1080 1081 ctrl_ctx->drop_flags |= drop_flag; 1082 new_drop_flags = ctrl_ctx->drop_flags; 1083 1084 ctrl_ctx->add_flags &= ~drop_flag; 1085 new_add_flags = ctrl_ctx->add_flags; 1086 1087 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags); 1088 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx); 1089 /* Update the last valid endpoint context, if we deleted the last one */ 1090 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) { 1091 slot_ctx->dev_info &= ~LAST_CTX_MASK; 1092 slot_ctx->dev_info |= LAST_CTX(last_ctx); 1093 } 1094 new_slot_info = slot_ctx->dev_info; 1095 1096 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep); 1097 1098 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n", 1099 (unsigned int) ep->desc.bEndpointAddress, 1100 udev->slot_id, 1101 (unsigned int) new_drop_flags, 1102 (unsigned int) new_add_flags, 1103 (unsigned int) new_slot_info); 1104 return 0; 1105} 1106 1107/* Add an endpoint to a new possible bandwidth configuration for this device. 1108 * Only one call to this function is allowed per endpoint before 1109 * check_bandwidth() or reset_bandwidth() must be called. 1110 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will 1111 * add the endpoint to the schedule with possibly new parameters denoted by a 1112 * different endpoint descriptor in usb_host_endpoint. 1113 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is 1114 * not allowed. 1115 * 1116 * The USB core will not allow URBs to be queued to an endpoint until the 1117 * configuration or alt setting is installed in the device, so there's no need 1118 * for mutual exclusion to protect the xhci->devs[slot_id] structure. 1119 */ 1120int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev, 1121 struct usb_host_endpoint *ep) 1122{ 1123 struct xhci_hcd *xhci; 1124 struct xhci_container_ctx *in_ctx, *out_ctx; 1125 unsigned int ep_index; 1126 struct xhci_ep_ctx *ep_ctx; 1127 struct xhci_slot_ctx *slot_ctx; 1128 struct xhci_input_control_ctx *ctrl_ctx; 1129 u32 added_ctxs; 1130 unsigned int last_ctx; 1131 u32 new_add_flags, new_drop_flags, new_slot_info; 1132 int ret = 0; 1133 1134 ret = xhci_check_args(hcd, udev, ep, 1, __func__); 1135 if (ret <= 0) { 1136 /* So we won't queue a reset ep command for a root hub */ 1137 ep->hcpriv = NULL; 1138 return ret; 1139 } 1140 xhci = hcd_to_xhci(hcd); 1141 1142 added_ctxs = xhci_get_endpoint_flag(&ep->desc); 1143 last_ctx = xhci_last_valid_endpoint(added_ctxs); 1144 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) { 1145 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n", 1146 __func__, added_ctxs); 1147 return 0; 1148 } 1149 1150 if (!xhci->devs || !xhci->devs[udev->slot_id]) { 1151 xhci_warn(xhci, "xHCI %s called with unaddressed device\n", 1152 __func__); 1153 return -EINVAL; 1154 } 1155 1156 in_ctx = xhci->devs[udev->slot_id]->in_ctx; 1157 out_ctx = xhci->devs[udev->slot_id]->out_ctx; 1158 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); 1159 ep_index = xhci_get_endpoint_index(&ep->desc); 1160 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); 1161 /* If the HCD has already noted the endpoint is enabled, 1162 * ignore this request. 1163 */ 1164 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) { 1165 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n", 1166 __func__, ep); 1167 return 0; 1168 } 1169 1170 /* 1171 * Configuration and alternate setting changes must be done in 1172 * process context, not interrupt context (or so documenation 1173 * for usb_set_interface() and usb_set_configuration() claim). 1174 */ 1175 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id], 1176 udev, ep, GFP_NOIO) < 0) { 1177 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n", 1178 __func__, ep->desc.bEndpointAddress); 1179 return -ENOMEM; 1180 } 1181 1182 ctrl_ctx->add_flags |= added_ctxs; 1183 new_add_flags = ctrl_ctx->add_flags; 1184 1185 /* If xhci_endpoint_disable() was called for this endpoint, but the 1186 * xHC hasn't been notified yet through the check_bandwidth() call, 1187 * this re-adds a new state for the endpoint from the new endpoint 1188 * descriptors. We must drop and re-add this endpoint, so we leave the 1189 * drop flags alone. 1190 */ 1191 new_drop_flags = ctrl_ctx->drop_flags; 1192 1193 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx); 1194 /* Update the last valid endpoint context, if we just added one past */ 1195 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) { 1196 slot_ctx->dev_info &= ~LAST_CTX_MASK; 1197 slot_ctx->dev_info |= LAST_CTX(last_ctx); 1198 } 1199 new_slot_info = slot_ctx->dev_info; 1200 1201 /* Store the usb_device pointer for later use */ 1202 ep->hcpriv = udev; 1203 1204 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n", 1205 (unsigned int) ep->desc.bEndpointAddress, 1206 udev->slot_id, 1207 (unsigned int) new_drop_flags, 1208 (unsigned int) new_add_flags, 1209 (unsigned int) new_slot_info); 1210 return 0; 1211} 1212 1213static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev) 1214{ 1215 struct xhci_input_control_ctx *ctrl_ctx; 1216 struct xhci_ep_ctx *ep_ctx; 1217 struct xhci_slot_ctx *slot_ctx; 1218 int i; 1219 1220 /* When a device's add flag and drop flag are zero, any subsequent 1221 * configure endpoint command will leave that endpoint's state 1222 * untouched. Make sure we don't leave any old state in the input 1223 * endpoint contexts. 1224 */ 1225 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx); 1226 ctrl_ctx->drop_flags = 0; 1227 ctrl_ctx->add_flags = 0; 1228 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); 1229 slot_ctx->dev_info &= ~LAST_CTX_MASK; 1230 /* Endpoint 0 is always valid */ 1231 slot_ctx->dev_info |= LAST_CTX(1); 1232 for (i = 1; i < 31; ++i) { 1233 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i); 1234 ep_ctx->ep_info = 0; 1235 ep_ctx->ep_info2 = 0; 1236 ep_ctx->deq = 0; 1237 ep_ctx->tx_info = 0; 1238 } 1239} 1240 1241static int xhci_configure_endpoint_result(struct xhci_hcd *xhci, 1242 struct usb_device *udev, int *cmd_status) 1243{ 1244 int ret; 1245 1246 switch (*cmd_status) { 1247 case COMP_ENOMEM: 1248 dev_warn(&udev->dev, "Not enough host controller resources " 1249 "for new device state.\n"); 1250 ret = -ENOMEM; 1251 break; 1252 case COMP_BW_ERR: 1253 dev_warn(&udev->dev, "Not enough bandwidth " 1254 "for new device state.\n"); 1255 ret = -ENOSPC; 1256 break; 1257 case COMP_TRB_ERR: 1258 /* the HCD set up something wrong */ 1259 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, " 1260 "add flag = 1, " 1261 "and endpoint is not disabled.\n"); 1262 ret = -EINVAL; 1263 break; 1264 case COMP_SUCCESS: 1265 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n"); 1266 ret = 0; 1267 break; 1268 default: 1269 xhci_err(xhci, "ERROR: unexpected command completion " 1270 "code 0x%x.\n", *cmd_status); 1271 ret = -EINVAL; 1272 break; 1273 } 1274 return ret; 1275} 1276 1277static int xhci_evaluate_context_result(struct xhci_hcd *xhci, 1278 struct usb_device *udev, int *cmd_status) 1279{ 1280 int ret; 1281 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id]; 1282 1283 switch (*cmd_status) { 1284 case COMP_EINVAL: 1285 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate " 1286 "context command.\n"); 1287 ret = -EINVAL; 1288 break; 1289 case COMP_EBADSLT: 1290 dev_warn(&udev->dev, "WARN: slot not enabled for" 1291 "evaluate context command.\n"); 1292 case COMP_CTX_STATE: 1293 dev_warn(&udev->dev, "WARN: invalid context state for " 1294 "evaluate context command.\n"); 1295 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1); 1296 ret = -EINVAL; 1297 break; 1298 case COMP_SUCCESS: 1299 dev_dbg(&udev->dev, "Successful evaluate context command\n"); 1300 ret = 0; 1301 break; 1302 default: 1303 xhci_err(xhci, "ERROR: unexpected command completion " 1304 "code 0x%x.\n", *cmd_status); 1305 ret = -EINVAL; 1306 break; 1307 } 1308 return ret; 1309} 1310 1311/* Issue a configure endpoint command or evaluate context command 1312 * and wait for it to finish. 1313 */ 1314static int xhci_configure_endpoint(struct xhci_hcd *xhci, 1315 struct usb_device *udev, 1316 struct xhci_command *command, 1317 bool ctx_change, bool must_succeed) 1318{ 1319 int ret; 1320 int timeleft; 1321 unsigned long flags; 1322 struct xhci_container_ctx *in_ctx; 1323 struct completion *cmd_completion; 1324 int *cmd_status; 1325 struct xhci_virt_device *virt_dev; 1326 1327 spin_lock_irqsave(&xhci->lock, flags); 1328 virt_dev = xhci->devs[udev->slot_id]; 1329 if (command) { 1330 in_ctx = command->in_ctx; 1331 cmd_completion = command->completion; 1332 cmd_status = &command->status; 1333 command->command_trb = xhci->cmd_ring->enqueue; 1334 1335 /* Enqueue pointer can be left pointing to the link TRB, 1336 * we must handle that 1337 */ 1338 if ((command->command_trb->link.control & TRB_TYPE_BITMASK) 1339 == TRB_TYPE(TRB_LINK)) 1340 command->command_trb = 1341 xhci->cmd_ring->enq_seg->next->trbs; 1342 1343 list_add_tail(&command->cmd_list, &virt_dev->cmd_list); 1344 } else { 1345 in_ctx = virt_dev->in_ctx; 1346 cmd_completion = &virt_dev->cmd_completion; 1347 cmd_status = &virt_dev->cmd_status; 1348 } 1349 init_completion(cmd_completion); 1350 1351 if (!ctx_change) 1352 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma, 1353 udev->slot_id, must_succeed); 1354 else 1355 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma, 1356 udev->slot_id); 1357 if (ret < 0) { 1358 if (command) 1359 list_del(&command->cmd_list); 1360 spin_unlock_irqrestore(&xhci->lock, flags); 1361 xhci_dbg(xhci, "FIXME allocate a new ring segment\n"); 1362 return -ENOMEM; 1363 } 1364 xhci_ring_cmd_db(xhci); 1365 spin_unlock_irqrestore(&xhci->lock, flags); 1366 1367 /* Wait for the configure endpoint command to complete */ 1368 timeleft = wait_for_completion_interruptible_timeout( 1369 cmd_completion, 1370 USB_CTRL_SET_TIMEOUT); 1371 if (timeleft <= 0) { 1372 xhci_warn(xhci, "%s while waiting for %s command\n", 1373 timeleft == 0 ? "Timeout" : "Signal", 1374 ctx_change == 0 ? 1375 "configure endpoint" : 1376 "evaluate context"); 1377 return -ETIME; 1378 } 1379 1380 if (!ctx_change) 1381 return xhci_configure_endpoint_result(xhci, udev, cmd_status); 1382 return xhci_evaluate_context_result(xhci, udev, cmd_status); 1383} 1384 1385/* Called after one or more calls to xhci_add_endpoint() or 1386 * xhci_drop_endpoint(). If this call fails, the USB core is expected 1387 * to call xhci_reset_bandwidth(). 1388 * 1389 * Since we are in the middle of changing either configuration or 1390 * installing a new alt setting, the USB core won't allow URBs to be 1391 * enqueued for any endpoint on the old config or interface. Nothing 1392 * else should be touching the xhci->devs[slot_id] structure, so we 1393 * don't need to take the xhci->lock for manipulating that. 1394 */ 1395int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev) 1396{ 1397 int i; 1398 int ret = 0; 1399 struct xhci_hcd *xhci; 1400 struct xhci_virt_device *virt_dev; 1401 struct xhci_input_control_ctx *ctrl_ctx; 1402 struct xhci_slot_ctx *slot_ctx; 1403 1404 ret = xhci_check_args(hcd, udev, NULL, 0, __func__); 1405 if (ret <= 0) 1406 return ret; 1407 xhci = hcd_to_xhci(hcd); 1408 1409 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) { 1410 xhci_warn(xhci, "xHCI %s called with unaddressed device\n", 1411 __func__); 1412 return -EINVAL; 1413 } 1414 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); 1415 virt_dev = xhci->devs[udev->slot_id]; 1416 1417 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */ 1418 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx); 1419 ctrl_ctx->add_flags |= SLOT_FLAG; 1420 ctrl_ctx->add_flags &= ~EP0_FLAG; 1421 ctrl_ctx->drop_flags &= ~SLOT_FLAG; 1422 ctrl_ctx->drop_flags &= ~EP0_FLAG; 1423 xhci_dbg(xhci, "New Input Control Context:\n"); 1424 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); 1425 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 1426 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info)); 1427 1428 ret = xhci_configure_endpoint(xhci, udev, NULL, 1429 false, false); 1430 if (ret) { 1431 /* Callee should call reset_bandwidth() */ 1432 return ret; 1433 } 1434 1435 xhci_dbg(xhci, "Output context after successful config ep cmd:\n"); 1436 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1437 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info)); 1438 1439 xhci_zero_in_ctx(xhci, virt_dev); 1440 /* Install new rings and free or cache any old rings */ 1441 for (i = 1; i < 31; ++i) { 1442 if (!virt_dev->eps[i].new_ring) 1443 continue; 1444 /* Only cache or free the old ring if it exists. 1445 * It may not if this is the first add of an endpoint. 1446 */ 1447 if (virt_dev->eps[i].ring) { 1448 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i); 1449 } 1450 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring; 1451 virt_dev->eps[i].new_ring = NULL; 1452 } 1453 1454 return ret; 1455} 1456 1457void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev) 1458{ 1459 struct xhci_hcd *xhci; 1460 struct xhci_virt_device *virt_dev; 1461 int i, ret; 1462 1463 ret = xhci_check_args(hcd, udev, NULL, 0, __func__); 1464 if (ret <= 0) 1465 return; 1466 xhci = hcd_to_xhci(hcd); 1467 1468 if (!xhci->devs || !xhci->devs[udev->slot_id]) { 1469 xhci_warn(xhci, "xHCI %s called with unaddressed device\n", 1470 __func__); 1471 return; 1472 } 1473 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); 1474 virt_dev = xhci->devs[udev->slot_id]; 1475 /* Free any rings allocated for added endpoints */ 1476 for (i = 0; i < 31; ++i) { 1477 if (virt_dev->eps[i].new_ring) { 1478 xhci_ring_free(xhci, virt_dev->eps[i].new_ring); 1479 virt_dev->eps[i].new_ring = NULL; 1480 } 1481 } 1482 xhci_zero_in_ctx(xhci, virt_dev); 1483} 1484 1485static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci, 1486 struct xhci_container_ctx *in_ctx, 1487 struct xhci_container_ctx *out_ctx, 1488 u32 add_flags, u32 drop_flags) 1489{ 1490 struct xhci_input_control_ctx *ctrl_ctx; 1491 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); 1492 ctrl_ctx->add_flags = add_flags; 1493 ctrl_ctx->drop_flags = drop_flags; 1494 xhci_slot_copy(xhci, in_ctx, out_ctx); 1495 ctrl_ctx->add_flags |= SLOT_FLAG; 1496 1497 xhci_dbg(xhci, "Input Context:\n"); 1498 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags)); 1499} 1500 1501void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci, 1502 unsigned int slot_id, unsigned int ep_index, 1503 struct xhci_dequeue_state *deq_state) 1504{ 1505 struct xhci_container_ctx *in_ctx; 1506 struct xhci_ep_ctx *ep_ctx; 1507 u32 added_ctxs; 1508 dma_addr_t addr; 1509 1510 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx, 1511 xhci->devs[slot_id]->out_ctx, ep_index); 1512 in_ctx = xhci->devs[slot_id]->in_ctx; 1513 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index); 1514 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg, 1515 deq_state->new_deq_ptr); 1516 if (addr == 0) { 1517 xhci_warn(xhci, "WARN Cannot submit config ep after " 1518 "reset ep command\n"); 1519 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n", 1520 deq_state->new_deq_seg, 1521 deq_state->new_deq_ptr); 1522 return; 1523 } 1524 ep_ctx->deq = addr | deq_state->new_cycle_state; 1525 1526 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index); 1527 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx, 1528 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs); 1529} 1530 1531void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, 1532 struct usb_device *udev, unsigned int ep_index) 1533{ 1534 struct xhci_dequeue_state deq_state; 1535 struct xhci_virt_ep *ep; 1536 1537 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n"); 1538 ep = &xhci->devs[udev->slot_id]->eps[ep_index]; 1539 /* We need to move the HW's dequeue pointer past this TD, 1540 * or it will attempt to resend it on the next doorbell ring. 1541 */ 1542 xhci_find_new_dequeue_state(xhci, udev->slot_id, 1543 ep_index, ep->stopped_stream, ep->stopped_td, 1544 &deq_state); 1545 1546 /* HW with the reset endpoint quirk will use the saved dequeue state to 1547 * issue a configure endpoint command later. 1548 */ 1549 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) { 1550 xhci_dbg(xhci, "Queueing new dequeue state\n"); 1551 xhci_queue_new_dequeue_state(xhci, udev->slot_id, 1552 ep_index, ep->stopped_stream, &deq_state); 1553 } else { 1554 xhci_dbg(xhci, "Setting up input context for " 1555 "configure endpoint command\n"); 1556 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id, 1557 ep_index, &deq_state); 1558 } 1559} 1560 1561/* Deal with stalled endpoints. The core should have sent the control message 1562 * to clear the halt condition. However, we need to make the xHCI hardware 1563 * reset its sequence number, since a device will expect a sequence number of 1564 * zero after the halt condition is cleared. 1565 * Context: in_interrupt 1566 */ 1567void xhci_endpoint_reset(struct usb_hcd *hcd, 1568 struct usb_host_endpoint *ep) 1569{ 1570 struct xhci_hcd *xhci; 1571 struct usb_device *udev; 1572 unsigned int ep_index; 1573 unsigned long flags; 1574 int ret; 1575 struct xhci_virt_ep *virt_ep; 1576 1577 xhci = hcd_to_xhci(hcd); 1578 udev = (struct usb_device *) ep->hcpriv; 1579 /* Called with a root hub endpoint (or an endpoint that wasn't added 1580 * with xhci_add_endpoint() 1581 */ 1582 if (!ep->hcpriv) 1583 return; 1584 ep_index = xhci_get_endpoint_index(&ep->desc); 1585 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index]; 1586 if (!virt_ep->stopped_td) { 1587 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n", 1588 ep->desc.bEndpointAddress); 1589 return; 1590 } 1591 if (usb_endpoint_xfer_control(&ep->desc)) { 1592 xhci_dbg(xhci, "Control endpoint stall already handled.\n"); 1593 return; 1594 } 1595 1596 xhci_dbg(xhci, "Queueing reset endpoint command\n"); 1597 spin_lock_irqsave(&xhci->lock, flags); 1598 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index); 1599 /* 1600 * Can't change the ring dequeue pointer until it's transitioned to the 1601 * stopped state, which is only upon a successful reset endpoint 1602 * command. Better hope that last command worked! 1603 */ 1604 if (!ret) { 1605 xhci_cleanup_stalled_ring(xhci, udev, ep_index); 1606 kfree(virt_ep->stopped_td); 1607 xhci_ring_cmd_db(xhci); 1608 } 1609 virt_ep->stopped_td = NULL; 1610 virt_ep->stopped_trb = NULL; 1611 virt_ep->stopped_stream = 0; 1612 spin_unlock_irqrestore(&xhci->lock, flags); 1613 1614 if (ret) 1615 xhci_warn(xhci, "FIXME allocate a new ring segment\n"); 1616} 1617 1618static int xhci_check_streams_endpoint(struct xhci_hcd *xhci, 1619 struct usb_device *udev, struct usb_host_endpoint *ep, 1620 unsigned int slot_id) 1621{ 1622 int ret; 1623 unsigned int ep_index; 1624 unsigned int ep_state; 1625 1626 if (!ep) 1627 return -EINVAL; 1628 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, __func__); 1629 if (ret <= 0) 1630 return -EINVAL; 1631 if (ep->ss_ep_comp.bmAttributes == 0) { 1632 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion" 1633 " descriptor for ep 0x%x does not support streams\n", 1634 ep->desc.bEndpointAddress); 1635 return -EINVAL; 1636 } 1637 1638 ep_index = xhci_get_endpoint_index(&ep->desc); 1639 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; 1640 if (ep_state & EP_HAS_STREAMS || 1641 ep_state & EP_GETTING_STREAMS) { 1642 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x " 1643 "already has streams set up.\n", 1644 ep->desc.bEndpointAddress); 1645 xhci_warn(xhci, "Send email to xHCI maintainer and ask for " 1646 "dynamic stream context array reallocation.\n"); 1647 return -EINVAL; 1648 } 1649 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) { 1650 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk " 1651 "endpoint 0x%x; URBs are pending.\n", 1652 ep->desc.bEndpointAddress); 1653 return -EINVAL; 1654 } 1655 return 0; 1656} 1657 1658static void xhci_calculate_streams_entries(struct xhci_hcd *xhci, 1659 unsigned int *num_streams, unsigned int *num_stream_ctxs) 1660{ 1661 unsigned int max_streams; 1662 1663 /* The stream context array size must be a power of two */ 1664 *num_stream_ctxs = roundup_pow_of_two(*num_streams); 1665 /* 1666 * Find out how many primary stream array entries the host controller 1667 * supports. Later we may use secondary stream arrays (similar to 2nd 1668 * level page entries), but that's an optional feature for xHCI host 1669 * controllers. xHCs must support at least 4 stream IDs. 1670 */ 1671 max_streams = HCC_MAX_PSA(xhci->hcc_params); 1672 if (*num_stream_ctxs > max_streams) { 1673 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n", 1674 max_streams); 1675 *num_stream_ctxs = max_streams; 1676 *num_streams = max_streams; 1677 } 1678} 1679 1680/* Returns an error code if one of the endpoint already has streams. 1681 * This does not change any data structures, it only checks and gathers 1682 * information. 1683 */ 1684static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci, 1685 struct usb_device *udev, 1686 struct usb_host_endpoint **eps, unsigned int num_eps, 1687 unsigned int *num_streams, u32 *changed_ep_bitmask) 1688{ 1689 unsigned int max_streams; 1690 unsigned int endpoint_flag; 1691 int i; 1692 int ret; 1693 1694 for (i = 0; i < num_eps; i++) { 1695 ret = xhci_check_streams_endpoint(xhci, udev, 1696 eps[i], udev->slot_id); 1697 if (ret < 0) 1698 return ret; 1699 1700 max_streams = USB_SS_MAX_STREAMS( 1701 eps[i]->ss_ep_comp.bmAttributes); 1702 if (max_streams < (*num_streams - 1)) { 1703 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n", 1704 eps[i]->desc.bEndpointAddress, 1705 max_streams); 1706 *num_streams = max_streams+1; 1707 } 1708 1709 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc); 1710 if (*changed_ep_bitmask & endpoint_flag) 1711 return -EINVAL; 1712 *changed_ep_bitmask |= endpoint_flag; 1713 } 1714 return 0; 1715} 1716 1717static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci, 1718 struct usb_device *udev, 1719 struct usb_host_endpoint **eps, unsigned int num_eps) 1720{ 1721 u32 changed_ep_bitmask = 0; 1722 unsigned int slot_id; 1723 unsigned int ep_index; 1724 unsigned int ep_state; 1725 int i; 1726 1727 slot_id = udev->slot_id; 1728 if (!xhci->devs[slot_id]) 1729 return 0; 1730 1731 for (i = 0; i < num_eps; i++) { 1732 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 1733 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; 1734 /* Are streams already being freed for the endpoint? */ 1735 if (ep_state & EP_GETTING_NO_STREAMS) { 1736 xhci_warn(xhci, "WARN Can't disable streams for " 1737 "endpoint 0x%x\n, " 1738 "streams are being disabled already.", 1739 eps[i]->desc.bEndpointAddress); 1740 return 0; 1741 } 1742 /* Are there actually any streams to free? */ 1743 if (!(ep_state & EP_HAS_STREAMS) && 1744 !(ep_state & EP_GETTING_STREAMS)) { 1745 xhci_warn(xhci, "WARN Can't disable streams for " 1746 "endpoint 0x%x\n, " 1747 "streams are already disabled!", 1748 eps[i]->desc.bEndpointAddress); 1749 xhci_warn(xhci, "WARN xhci_free_streams() called " 1750 "with non-streams endpoint\n"); 1751 return 0; 1752 } 1753 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc); 1754 } 1755 return changed_ep_bitmask; 1756} 1757 1758/* 1759 * The USB device drivers use this function (though the HCD interface in USB 1760 * core) to prepare a set of bulk endpoints to use streams. Streams are used to 1761 * coordinate mass storage command queueing across multiple endpoints (basically 1762 * a stream ID == a task ID). 1763 * 1764 * Setting up streams involves allocating the same size stream context array 1765 * for each endpoint and issuing a configure endpoint command for all endpoints. 1766 * 1767 * Don't allow the call to succeed if one endpoint only supports one stream 1768 * (which means it doesn't support streams at all). 1769 * 1770 * Drivers may get less stream IDs than they asked for, if the host controller 1771 * hardware or endpoints claim they can't support the number of requested 1772 * stream IDs. 1773 */ 1774int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev, 1775 struct usb_host_endpoint **eps, unsigned int num_eps, 1776 unsigned int num_streams, gfp_t mem_flags) 1777{ 1778 int i, ret; 1779 struct xhci_hcd *xhci; 1780 struct xhci_virt_device *vdev; 1781 struct xhci_command *config_cmd; 1782 unsigned int ep_index; 1783 unsigned int num_stream_ctxs; 1784 unsigned long flags; 1785 u32 changed_ep_bitmask = 0; 1786 1787 if (!eps) 1788 return -EINVAL; 1789 1790 /* Add one to the number of streams requested to account for 1791 * stream 0 that is reserved for xHCI usage. 1792 */ 1793 num_streams += 1; 1794 xhci = hcd_to_xhci(hcd); 1795 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n", 1796 num_streams); 1797 1798 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags); 1799 if (!config_cmd) { 1800 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n"); 1801 return -ENOMEM; 1802 } 1803 1804 /* Check to make sure all endpoints are not already configured for 1805 * streams. While we're at it, find the maximum number of streams that 1806 * all the endpoints will support and check for duplicate endpoints. 1807 */ 1808 spin_lock_irqsave(&xhci->lock, flags); 1809 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps, 1810 num_eps, &num_streams, &changed_ep_bitmask); 1811 if (ret < 0) { 1812 xhci_free_command(xhci, config_cmd); 1813 spin_unlock_irqrestore(&xhci->lock, flags); 1814 return ret; 1815 } 1816 if (num_streams <= 1) { 1817 xhci_warn(xhci, "WARN: endpoints can't handle " 1818 "more than one stream.\n"); 1819 xhci_free_command(xhci, config_cmd); 1820 spin_unlock_irqrestore(&xhci->lock, flags); 1821 return -EINVAL; 1822 } 1823 vdev = xhci->devs[udev->slot_id]; 1824 /* Mark each endpoint as being in transistion, so 1825 * xhci_urb_enqueue() will reject all URBs. 1826 */ 1827 for (i = 0; i < num_eps; i++) { 1828 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 1829 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS; 1830 } 1831 spin_unlock_irqrestore(&xhci->lock, flags); 1832 1833 /* Setup internal data structures and allocate HW data structures for 1834 * streams (but don't install the HW structures in the input context 1835 * until we're sure all memory allocation succeeded). 1836 */ 1837 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs); 1838 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n", 1839 num_stream_ctxs, num_streams); 1840 1841 for (i = 0; i < num_eps; i++) { 1842 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 1843 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci, 1844 num_stream_ctxs, 1845 num_streams, mem_flags); 1846 if (!vdev->eps[ep_index].stream_info) 1847 goto cleanup; 1848 } 1849 1850 /* Set up the input context for a configure endpoint command. */ 1851 for (i = 0; i < num_eps; i++) { 1852 struct xhci_ep_ctx *ep_ctx; 1853 1854 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 1855 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index); 1856 1857 xhci_endpoint_copy(xhci, config_cmd->in_ctx, 1858 vdev->out_ctx, ep_index); 1859 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx, 1860 vdev->eps[ep_index].stream_info); 1861 } 1862 /* Tell the HW to drop its old copy of the endpoint context info 1863 * and add the updated copy from the input context. 1864 */ 1865 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx, 1866 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask); 1867 1868 /* Issue and wait for the configure endpoint command */ 1869 ret = xhci_configure_endpoint(xhci, udev, config_cmd, 1870 false, false); 1871 1872 /* xHC rejected the configure endpoint command for some reason, so we 1873 * leave the old ring intact and free our internal streams data 1874 * structure. 1875 */ 1876 if (ret < 0) 1877 goto cleanup; 1878 1879 spin_lock_irqsave(&xhci->lock, flags); 1880 for (i = 0; i < num_eps; i++) { 1881 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 1882 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; 1883 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n", 1884 udev->slot_id, ep_index); 1885 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS; 1886 } 1887 xhci_free_command(xhci, config_cmd); 1888 spin_unlock_irqrestore(&xhci->lock, flags); 1889 1890 /* Subtract 1 for stream 0, which drivers can't use */ 1891 return num_streams - 1; 1892 1893cleanup: 1894 /* If it didn't work, free the streams! */ 1895 for (i = 0; i < num_eps; i++) { 1896 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 1897 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); 1898 vdev->eps[ep_index].stream_info = NULL; 1899 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; 1900 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; 1901 xhci_endpoint_zero(xhci, vdev, eps[i]); 1902 } 1903 xhci_free_command(xhci, config_cmd); 1904 return -ENOMEM; 1905} 1906 1907/* Transition the endpoint from using streams to being a "normal" endpoint 1908 * without streams. 1909 * 1910 * Modify the endpoint context state, submit a configure endpoint command, 1911 * and free all endpoint rings for streams if that completes successfully. 1912 */ 1913int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev, 1914 struct usb_host_endpoint **eps, unsigned int num_eps, 1915 gfp_t mem_flags) 1916{ 1917 int i, ret; 1918 struct xhci_hcd *xhci; 1919 struct xhci_virt_device *vdev; 1920 struct xhci_command *command; 1921 unsigned int ep_index; 1922 unsigned long flags; 1923 u32 changed_ep_bitmask; 1924 1925 xhci = hcd_to_xhci(hcd); 1926 vdev = xhci->devs[udev->slot_id]; 1927 1928 /* Set up a configure endpoint command to remove the streams rings */ 1929 spin_lock_irqsave(&xhci->lock, flags); 1930 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci, 1931 udev, eps, num_eps); 1932 if (changed_ep_bitmask == 0) { 1933 spin_unlock_irqrestore(&xhci->lock, flags); 1934 return -EINVAL; 1935 } 1936 1937 /* Use the xhci_command structure from the first endpoint. We may have 1938 * allocated too many, but the driver may call xhci_free_streams() for 1939 * each endpoint it grouped into one call to xhci_alloc_streams(). 1940 */ 1941 ep_index = xhci_get_endpoint_index(&eps[0]->desc); 1942 command = vdev->eps[ep_index].stream_info->free_streams_command; 1943 for (i = 0; i < num_eps; i++) { 1944 struct xhci_ep_ctx *ep_ctx; 1945 1946 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 1947 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); 1948 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |= 1949 EP_GETTING_NO_STREAMS; 1950 1951 xhci_endpoint_copy(xhci, command->in_ctx, 1952 vdev->out_ctx, ep_index); 1953 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx, 1954 &vdev->eps[ep_index]); 1955 } 1956 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx, 1957 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask); 1958 spin_unlock_irqrestore(&xhci->lock, flags); 1959 1960 /* Issue and wait for the configure endpoint command, 1961 * which must succeed. 1962 */ 1963 ret = xhci_configure_endpoint(xhci, udev, command, 1964 false, true); 1965 1966 /* xHC rejected the configure endpoint command for some reason, so we 1967 * leave the streams rings intact. 1968 */ 1969 if (ret < 0) 1970 return ret; 1971 1972 spin_lock_irqsave(&xhci->lock, flags); 1973 for (i = 0; i < num_eps; i++) { 1974 ep_index = xhci_get_endpoint_index(&eps[i]->desc); 1975 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); 1976 vdev->eps[ep_index].stream_info = NULL; 1977 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS; 1978 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; 1979 } 1980 spin_unlock_irqrestore(&xhci->lock, flags); 1981 1982 return 0; 1983} 1984 1985/* 1986 * This submits a Reset Device Command, which will set the device state to 0, 1987 * set the device address to 0, and disable all the endpoints except the default 1988 * control endpoint. The USB core should come back and call 1989 * xhci_address_device(), and then re-set up the configuration. If this is 1990 * called because of a usb_reset_and_verify_device(), then the old alternate 1991 * settings will be re-installed through the normal bandwidth allocation 1992 * functions. 1993 * 1994 * Wait for the Reset Device command to finish. Remove all structures 1995 * associated with the endpoints that were disabled. Clear the input device 1996 * structure? Cache the rings? Reset the control endpoint 0 max packet size? 1997 */ 1998int xhci_reset_device(struct usb_hcd *hcd, struct usb_device *udev) 1999{ 2000 int ret, i; 2001 unsigned long flags; 2002 struct xhci_hcd *xhci; 2003 unsigned int slot_id; 2004 struct xhci_virt_device *virt_dev; 2005 struct xhci_command *reset_device_cmd; 2006 int timeleft; 2007 int last_freed_endpoint; 2008 2009 ret = xhci_check_args(hcd, udev, NULL, 0, __func__); 2010 if (ret <= 0) 2011 return ret; 2012 xhci = hcd_to_xhci(hcd); 2013 slot_id = udev->slot_id; 2014 virt_dev = xhci->devs[slot_id]; 2015 if (!virt_dev) { 2016 xhci_dbg(xhci, "%s called with invalid slot ID %u\n", 2017 __func__, slot_id); 2018 return -EINVAL; 2019 } 2020 2021 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id); 2022 /* Allocate the command structure that holds the struct completion. 2023 * Assume we're in process context, since the normal device reset 2024 * process has to wait for the device anyway. Storage devices are 2025 * reset as part of error handling, so use GFP_NOIO instead of 2026 * GFP_KERNEL. 2027 */ 2028 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO); 2029 if (!reset_device_cmd) { 2030 xhci_dbg(xhci, "Couldn't allocate command structure.\n"); 2031 return -ENOMEM; 2032 } 2033 2034 /* Attempt to submit the Reset Device command to the command ring */ 2035 spin_lock_irqsave(&xhci->lock, flags); 2036 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue; 2037 2038 /* Enqueue pointer can be left pointing to the link TRB, 2039 * we must handle that 2040 */ 2041 if ((reset_device_cmd->command_trb->link.control & TRB_TYPE_BITMASK) 2042 == TRB_TYPE(TRB_LINK)) 2043 reset_device_cmd->command_trb = 2044 xhci->cmd_ring->enq_seg->next->trbs; 2045 2046 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list); 2047 ret = xhci_queue_reset_device(xhci, slot_id); 2048 if (ret) { 2049 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); 2050 list_del(&reset_device_cmd->cmd_list); 2051 spin_unlock_irqrestore(&xhci->lock, flags); 2052 goto command_cleanup; 2053 } 2054 xhci_ring_cmd_db(xhci); 2055 spin_unlock_irqrestore(&xhci->lock, flags); 2056 2057 /* Wait for the Reset Device command to finish */ 2058 timeleft = wait_for_completion_interruptible_timeout( 2059 reset_device_cmd->completion, 2060 USB_CTRL_SET_TIMEOUT); 2061 if (timeleft <= 0) { 2062 xhci_warn(xhci, "%s while waiting for reset device command\n", 2063 timeleft == 0 ? "Timeout" : "Signal"); 2064 spin_lock_irqsave(&xhci->lock, flags); 2065 /* The timeout might have raced with the event ring handler, so 2066 * only delete from the list if the item isn't poisoned. 2067 */ 2068 if (reset_device_cmd->cmd_list.next != LIST_POISON1) 2069 list_del(&reset_device_cmd->cmd_list); 2070 spin_unlock_irqrestore(&xhci->lock, flags); 2071 ret = -ETIME; 2072 goto command_cleanup; 2073 } 2074 2075 /* The Reset Device command can't fail, according to the 0.95/0.96 spec, 2076 * unless we tried to reset a slot ID that wasn't enabled, 2077 * or the device wasn't in the addressed or configured state. 2078 */ 2079 ret = reset_device_cmd->status; 2080 switch (ret) { 2081 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */ 2082 case COMP_CTX_STATE: /* 0.96 completion code for same thing */ 2083 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n", 2084 slot_id, 2085 xhci_get_slot_state(xhci, virt_dev->out_ctx)); 2086 xhci_info(xhci, "Not freeing device rings.\n"); 2087 /* Don't treat this as an error. May change my mind later. */ 2088 ret = 0; 2089 goto command_cleanup; 2090 case COMP_SUCCESS: 2091 xhci_dbg(xhci, "Successful reset device command.\n"); 2092 break; 2093 default: 2094 if (xhci_is_vendor_info_code(xhci, ret)) 2095 break; 2096 xhci_warn(xhci, "Unknown completion code %u for " 2097 "reset device command.\n", ret); 2098 ret = -EINVAL; 2099 goto command_cleanup; 2100 } 2101 2102 /* Everything but endpoint 0 is disabled, so free or cache the rings. */ 2103 last_freed_endpoint = 1; 2104 for (i = 1; i < 31; ++i) { 2105 if (!virt_dev->eps[i].ring) 2106 continue; 2107 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i); 2108 last_freed_endpoint = i; 2109 } 2110 xhci_dbg(xhci, "Output context after successful reset device cmd:\n"); 2111 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint); 2112 ret = 0; 2113 2114command_cleanup: 2115 xhci_free_command(xhci, reset_device_cmd); 2116 return ret; 2117} 2118 2119/* 2120 * At this point, the struct usb_device is about to go away, the device has 2121 * disconnected, and all traffic has been stopped and the endpoints have been 2122 * disabled. Free any HC data structures associated with that device. 2123 */ 2124void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev) 2125{ 2126 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 2127 struct xhci_virt_device *virt_dev; 2128 unsigned long flags; 2129 u32 state; 2130 int i; 2131 2132 if (udev->slot_id == 0) 2133 return; 2134 virt_dev = xhci->devs[udev->slot_id]; 2135 if (!virt_dev) 2136 return; 2137 2138 /* Stop any wayward timer functions (which may grab the lock) */ 2139 for (i = 0; i < 31; ++i) { 2140 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING; 2141 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer); 2142 } 2143 2144 spin_lock_irqsave(&xhci->lock, flags); 2145 /* Don't disable the slot if the host controller is dead. */ 2146 state = xhci_readl(xhci, &xhci->op_regs->status); 2147 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) { 2148 xhci_free_virt_device(xhci, udev->slot_id); 2149 spin_unlock_irqrestore(&xhci->lock, flags); 2150 return; 2151 } 2152 2153 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) { 2154 spin_unlock_irqrestore(&xhci->lock, flags); 2155 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); 2156 return; 2157 } 2158 xhci_ring_cmd_db(xhci); 2159 spin_unlock_irqrestore(&xhci->lock, flags); 2160} 2161 2162/* 2163 * Returns 0 if the xHC ran out of device slots, the Enable Slot command 2164 * timed out, or allocating memory failed. Returns 1 on success. 2165 */ 2166int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev) 2167{ 2168 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 2169 unsigned long flags; 2170 int timeleft; 2171 int ret; 2172 2173 spin_lock_irqsave(&xhci->lock, flags); 2174 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0); 2175 if (ret) { 2176 spin_unlock_irqrestore(&xhci->lock, flags); 2177 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); 2178 return 0; 2179 } 2180 xhci_ring_cmd_db(xhci); 2181 spin_unlock_irqrestore(&xhci->lock, flags); 2182 2183 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev, 2184 USB_CTRL_SET_TIMEOUT); 2185 if (timeleft <= 0) { 2186 xhci_warn(xhci, "%s while waiting for a slot\n", 2187 timeleft == 0 ? "Timeout" : "Signal"); 2188 return 0; 2189 } 2190 2191 if (!xhci->slot_id) { 2192 xhci_err(xhci, "Error while assigning device slot ID\n"); 2193 return 0; 2194 } 2195 /* xhci_alloc_virt_device() does not touch rings; no need to lock. 2196 * Use GFP_NOIO, since this function can be called from 2197 * xhci_discover_or_reset_device(), which may be called as part of 2198 * mass storage driver error handling. 2199 */ 2200 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) { 2201 /* Disable slot, if we can do it without mem alloc */ 2202 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n"); 2203 spin_lock_irqsave(&xhci->lock, flags); 2204 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) 2205 xhci_ring_cmd_db(xhci); 2206 spin_unlock_irqrestore(&xhci->lock, flags); 2207 return 0; 2208 } 2209 udev->slot_id = xhci->slot_id; 2210 /* Is this a LS or FS device under a HS hub? */ 2211 /* Hub or peripherial? */ 2212 return 1; 2213} 2214 2215/* 2216 * Issue an Address Device command (which will issue a SetAddress request to 2217 * the device). 2218 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so 2219 * we should only issue and wait on one address command at the same time. 2220 * 2221 * We add one to the device address issued by the hardware because the USB core 2222 * uses address 1 for the root hubs (even though they're not really devices). 2223 */ 2224int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev) 2225{ 2226 unsigned long flags; 2227 int timeleft; 2228 struct xhci_virt_device *virt_dev; 2229 int ret = 0; 2230 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 2231 struct xhci_slot_ctx *slot_ctx; 2232 struct xhci_input_control_ctx *ctrl_ctx; 2233 u64 temp_64; 2234 2235 if (!udev->slot_id) { 2236 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id); 2237 return -EINVAL; 2238 } 2239 2240 virt_dev = xhci->devs[udev->slot_id]; 2241 2242 /* If this is a Set Address to an unconfigured device, setup ep 0 */ 2243 if (!udev->config) 2244 xhci_setup_addressable_virt_dev(xhci, udev); 2245 else 2246 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev); 2247 /* Otherwise, assume the core has the device configured how it wants */ 2248 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id); 2249 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2); 2250 2251 spin_lock_irqsave(&xhci->lock, flags); 2252 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma, 2253 udev->slot_id); 2254 if (ret) { 2255 spin_unlock_irqrestore(&xhci->lock, flags); 2256 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); 2257 return ret; 2258 } 2259 xhci_ring_cmd_db(xhci); 2260 spin_unlock_irqrestore(&xhci->lock, flags); 2261 2262 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev, 2263 USB_CTRL_SET_TIMEOUT); 2264 if (timeleft <= 0) { 2265 xhci_warn(xhci, "%s while waiting for a slot\n", 2266 timeleft == 0 ? "Timeout" : "Signal"); 2267 return -ETIME; 2268 } 2269 2270 switch (virt_dev->cmd_status) { 2271 case COMP_CTX_STATE: 2272 case COMP_EBADSLT: 2273 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n", 2274 udev->slot_id); 2275 ret = -EINVAL; 2276 break; 2277 case COMP_TX_ERR: 2278 dev_warn(&udev->dev, "Device not responding to set address.\n"); 2279 ret = -EPROTO; 2280 break; 2281 case COMP_SUCCESS: 2282 xhci_dbg(xhci, "Successful Address Device command\n"); 2283 break; 2284 default: 2285 xhci_err(xhci, "ERROR: unexpected command completion " 2286 "code 0x%x.\n", virt_dev->cmd_status); 2287 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id); 2288 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2); 2289 ret = -EINVAL; 2290 break; 2291 } 2292 if (ret) { 2293 return ret; 2294 } 2295 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); 2296 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64); 2297 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n", 2298 udev->slot_id, 2299 &xhci->dcbaa->dev_context_ptrs[udev->slot_id], 2300 (unsigned long long) 2301 xhci->dcbaa->dev_context_ptrs[udev->slot_id]); 2302 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n", 2303 (unsigned long long)virt_dev->out_ctx->dma); 2304 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id); 2305 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2); 2306 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id); 2307 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2); 2308 /* 2309 * USB core uses address 1 for the roothubs, so we add one to the 2310 * address given back to us by the HC. 2311 */ 2312 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); 2313 udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1; 2314 /* Zero the input context control for later use */ 2315 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx); 2316 ctrl_ctx->add_flags = 0; 2317 ctrl_ctx->drop_flags = 0; 2318 2319 xhci_dbg(xhci, "Device address = %d\n", udev->devnum); 2320 set_bit(udev->devnum, udev->bus->devmap.devicemap); 2321 2322 return 0; 2323} 2324 2325/* Once a hub descriptor is fetched for a device, we need to update the xHC's 2326 * internal data structures for the device. 2327 */ 2328int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev, 2329 struct usb_tt *tt, gfp_t mem_flags) 2330{ 2331 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 2332 struct xhci_virt_device *vdev; 2333 struct xhci_command *config_cmd; 2334 struct xhci_input_control_ctx *ctrl_ctx; 2335 struct xhci_slot_ctx *slot_ctx; 2336 unsigned long flags; 2337 unsigned think_time; 2338 int ret; 2339 2340 /* Ignore root hubs */ 2341 if (!hdev->parent) 2342 return 0; 2343 2344 vdev = xhci->devs[hdev->slot_id]; 2345 if (!vdev) { 2346 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n"); 2347 return -EINVAL; 2348 } 2349 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags); 2350 if (!config_cmd) { 2351 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n"); 2352 return -ENOMEM; 2353 } 2354 2355 spin_lock_irqsave(&xhci->lock, flags); 2356 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx); 2357 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx); 2358 ctrl_ctx->add_flags |= SLOT_FLAG; 2359 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx); 2360 slot_ctx->dev_info |= DEV_HUB; 2361 if (tt->multi) 2362 slot_ctx->dev_info |= DEV_MTT; 2363 if (xhci->hci_version > 0x95) { 2364 xhci_dbg(xhci, "xHCI version %x needs hub " 2365 "TT think time and number of ports\n", 2366 (unsigned int) xhci->hci_version); 2367 slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild); 2368 /* Set TT think time - convert from ns to FS bit times. 2369 * 0 = 8 FS bit times, 1 = 16 FS bit times, 2370 * 2 = 24 FS bit times, 3 = 32 FS bit times. 2371 */ 2372 think_time = tt->think_time; 2373 if (think_time != 0) 2374 think_time = (think_time / 666) - 1; 2375 slot_ctx->tt_info |= TT_THINK_TIME(think_time); 2376 } else { 2377 xhci_dbg(xhci, "xHCI version %x doesn't need hub " 2378 "TT think time or number of ports\n", 2379 (unsigned int) xhci->hci_version); 2380 } 2381 slot_ctx->dev_state = 0; 2382 spin_unlock_irqrestore(&xhci->lock, flags); 2383 2384 xhci_dbg(xhci, "Set up %s for hub device.\n", 2385 (xhci->hci_version > 0x95) ? 2386 "configure endpoint" : "evaluate context"); 2387 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id); 2388 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0); 2389 2390 /* Issue and wait for the configure endpoint or 2391 * evaluate context command. 2392 */ 2393 if (xhci->hci_version > 0x95) 2394 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, 2395 false, false); 2396 else 2397 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, 2398 true, false); 2399 2400 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id); 2401 xhci_dbg_ctx(xhci, vdev->out_ctx, 0); 2402 2403 xhci_free_command(xhci, config_cmd); 2404 return ret; 2405} 2406 2407int xhci_get_frame(struct usb_hcd *hcd) 2408{ 2409 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 2410 /* EHCI mods by the periodic size. Why? */ 2411 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3; 2412} 2413 2414MODULE_DESCRIPTION(DRIVER_DESC); 2415MODULE_AUTHOR(DRIVER_AUTHOR); 2416MODULE_LICENSE("GPL"); 2417 2418static int __init xhci_hcd_init(void) 2419{ 2420#ifdef CONFIG_PCI 2421 int retval = 0; 2422 2423 retval = xhci_register_pci(); 2424 2425 if (retval < 0) { 2426 printk(KERN_DEBUG "Problem registering PCI driver."); 2427 return retval; 2428 } 2429#endif 2430 /* 2431 * Check the compiler generated sizes of structures that must be laid 2432 * out in specific ways for hardware access. 2433 */ 2434 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8); 2435 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8); 2436 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8); 2437 /* xhci_device_control has eight fields, and also 2438 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx 2439 */ 2440 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8); 2441 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8); 2442 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8); 2443 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8); 2444 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8); 2445 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */ 2446 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8); 2447 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8); 2448 return 0; 2449} 2450module_init(xhci_hcd_init); 2451 2452static void __exit xhci_hcd_cleanup(void) 2453{ 2454#ifdef CONFIG_PCI 2455 xhci_unregister_pci(); 2456#endif 2457} 2458module_exit(xhci_hcd_cleanup); 2459