1 /* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */
3 * xHCI host controller driver
5 * Copyright (C) 2008 Intel Corp.
8 * Some code borrowed from the Linux EHCI driver.
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.
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
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.
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>
33 #define DRIVER_AUTHOR "Sarah Sharp"
34 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
36 /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
37 static int link_quirk
;
38 module_param(link_quirk
, int, S_IRUGO
| S_IWUSR
);
39 MODULE_PARM_DESC(link_quirk
, "Don't clear the chain bit on a link TRB");
41 /* a workaround for Seagate or WD USB 3.0 HDD */
43 module_param(usb2mode
, int, S_IRUGO
| S_IWUSR
);
44 MODULE_PARM_DESC(usb2mode
, "set this to enable USB2");
46 /* TODO: copied from ehci-hcd.c - can this be refactored? */
48 * handshake - spin reading hc until handshake completes or fails
49 * @ptr: address of hc register to be read
50 * @mask: bits to look at in result of read
51 * @done: value of those bits when handshake succeeds
52 * @usec: timeout in microseconds
54 * Returns negative errno, or zero on success
56 * Success happens when the "mask" bits have the specified value (hardware
57 * handshake done). There are two failure modes: "usec" have passed (major
58 * hardware flakeout), or the register reads as all-ones (hardware removed).
60 static int handshake(struct xhci_hcd
*xhci
, void __iomem
*ptr
,
61 u32 mask
, u32 done
, int usec
)
66 result
= xhci_readl(xhci
, ptr
);
67 if (result
== ~(u32
)0) /* card removed */
79 * Disable interrupts and begin the xHCI halting process.
81 void xhci_quiesce(struct xhci_hcd
*xhci
)
88 halted
= xhci_readl(xhci
, &xhci
->op_regs
->status
) & STS_HALT
;
92 cmd
= xhci_readl(xhci
, &xhci
->op_regs
->command
);
94 xhci_writel(xhci
, cmd
, &xhci
->op_regs
->command
);
97 int xhci_halt(struct xhci_hcd
*xhci
)
99 xhci_dbg(xhci
, "// Halt the HC\n");
102 return handshake(xhci
, &xhci
->op_regs
->status
,
103 STS_HALT
, STS_HALT
, XHCI_MAX_HALT_USEC
);
106 #ifdef CONFIG_BCM47XX
107 int xhci_fake_doorbell(struct xhci_hcd
*xhci
, int slot_id
)
109 unsigned int temp1
, ret
;
111 /* alloc a virt device for slot */
112 if (!xhci_alloc_virt_device(xhci
, slot_id
, 0, GFP_NOIO
)) {
113 xhci_warn(xhci
, "Could not allocate xHCI USB device data structures\n");
117 /* ring fake doorbell for slot_id ep 0 */
118 xhci_ring_ep_doorbell(xhci
, slot_id
, 0, 0);
121 /* read the status register to check if HSE is set or not? */
122 temp1
= xhci_readl(xhci
, &xhci
->op_regs
->status
);
123 xhci_dbg(xhci
, "op reg status = %x\n",temp1
);
125 /* clear HSE if set */
126 if(temp1
& STS_FATAL
) {
127 xhci_dbg(xhci
, "HSE problem detected\n");
130 xhci_dbg(xhci
, "temp1=%x\n",temp1
);
131 xhci_writel(xhci
, temp1
, &xhci
->op_regs
->status
);
133 temp1
= xhci_readl(xhci
, &xhci
->op_regs
->status
);
134 xhci_dbg(xhci
, "After clear op reg status=%x\n", temp1
);
137 /* Free virt device */
138 xhci_free_virt_device(xhci
, slot_id
);
140 /* Run the controller if needed */
141 temp1
= xhci_readl(xhci
, &xhci
->op_regs
->command
);
146 xhci_writel(xhci
, temp1
, &xhci
->op_regs
->command
);
148 * Wait for the HCHalted Status bit to be 0 to indicate the host is running.
150 ret
= handshake(xhci
, &xhci
->op_regs
->status
,
151 STS_HALT
, 0, XHCI_MAX_HALT_USEC
);
153 if (ret
== -ETIMEDOUT
) {
154 xhci_err(xhci
, "Host took too long to start, "
155 "waited %u microseconds.\n",
162 #endif /* CONFIG_BCM47XX */
165 * Set the run bit and wait for the host to be running.
167 int xhci_start(struct xhci_hcd
*xhci
)
172 temp
= xhci_readl(xhci
, &xhci
->op_regs
->command
);
174 xhci_dbg(xhci
, "// Turn on HC, cmd = 0x%x.\n",
176 xhci_writel(xhci
, temp
, &xhci
->op_regs
->command
);
179 * Wait for the HCHalted Status bit to be 0 to indicate the host is
182 ret
= handshake(xhci
, &xhci
->op_regs
->status
,
183 STS_HALT
, 0, XHCI_MAX_HALT_USEC
);
184 if (ret
== -ETIMEDOUT
)
185 xhci_err(xhci
, "Host took too long to start, "
186 "waited %u microseconds.\n",
189 #ifdef CONFIG_BCM47XX
190 xhci_fake_doorbell(xhci
, 1);
191 #endif /* CONFIG_BCM47XX */
197 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
199 * This resets pipelines, timers, counters, state machines, etc.
200 * Transactions will be terminated immediately, and operational registers
201 * will be set to their defaults.
203 int xhci_reset(struct xhci_hcd
*xhci
)
209 state
= xhci_readl(xhci
, &xhci
->op_regs
->status
);
210 if ((state
& STS_HALT
) == 0) {
211 xhci_warn(xhci
, "Host controller not halted, aborting reset.\n");
215 xhci_dbg(xhci
, "// Reset the HC\n");
216 command
= xhci_readl(xhci
, &xhci
->op_regs
->command
);
217 command
|= CMD_RESET
;
218 xhci_writel(xhci
, command
, &xhci
->op_regs
->command
);
219 xhci_to_hcd(xhci
)->state
= HC_STATE_HALT
;
221 ret
= handshake(xhci
, &xhci
->op_regs
->command
,
222 CMD_RESET
, 0, 250 * 1000);
226 xhci_dbg(xhci
, "Wait for controller to be ready for doorbell rings\n");
228 * xHCI cannot write to any doorbells or operational registers other
229 * than status until the "Controller Not Ready" flag is cleared.
231 return handshake(xhci
, &xhci
->op_regs
->status
, STS_CNR
, 0, 250 * 1000);
236 * free all IRQs request
238 static void xhci_free_irq(struct xhci_hcd
*xhci
)
241 struct pci_dev
*pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
243 /* return if using legacy interrupt */
244 if (xhci_to_hcd(xhci
)->irq
>= 0)
247 if (xhci
->msix_entries
) {
248 for (i
= 0; i
< xhci
->msix_count
; i
++)
249 if (xhci
->msix_entries
[i
].vector
)
250 free_irq(xhci
->msix_entries
[i
].vector
,
252 } else if (pdev
->irq
>= 0)
253 free_irq(pdev
->irq
, xhci_to_hcd(xhci
));
261 static int xhci_setup_msi(struct xhci_hcd
*xhci
)
264 struct pci_dev
*pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
266 ret
= pci_enable_msi(pdev
);
268 xhci_err(xhci
, "failed to allocate MSI entry\n");
272 ret
= request_irq(pdev
->irq
, (irq_handler_t
)xhci_msi_irq
,
273 0, "xhci_hcd", xhci_to_hcd(xhci
));
275 xhci_err(xhci
, "disable MSI interrupt\n");
276 pci_disable_msi(pdev
);
285 static int xhci_setup_msix(struct xhci_hcd
*xhci
)
288 struct pci_dev
*pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
291 * calculate number of msi-x vectors supported.
292 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
293 * with max number of interrupters based on the xhci HCSPARAMS1.
294 * - num_online_cpus: maximum msi-x vectors per CPUs core.
295 * Add additional 1 vector to ensure always available interrupt.
297 xhci
->msix_count
= min(num_online_cpus() + 1,
298 HCS_MAX_INTRS(xhci
->hcs_params1
));
301 kmalloc((sizeof(struct msix_entry
))*xhci
->msix_count
,
303 if (!xhci
->msix_entries
) {
304 xhci_err(xhci
, "Failed to allocate MSI-X entries\n");
308 for (i
= 0; i
< xhci
->msix_count
; i
++) {
309 xhci
->msix_entries
[i
].entry
= i
;
310 xhci
->msix_entries
[i
].vector
= 0;
313 ret
= pci_enable_msix(pdev
, xhci
->msix_entries
, xhci
->msix_count
);
315 xhci_err(xhci
, "Failed to enable MSI-X\n");
319 for (i
= 0; i
< xhci
->msix_count
; i
++) {
320 ret
= request_irq(xhci
->msix_entries
[i
].vector
,
321 (irq_handler_t
)xhci_msi_irq
,
322 0, "xhci_hcd", xhci_to_hcd(xhci
));
330 xhci_err(xhci
, "disable MSI-X interrupt\n");
332 pci_disable_msix(pdev
);
334 kfree(xhci
->msix_entries
);
335 xhci
->msix_entries
= NULL
;
339 /* Free any IRQs and disable MSI-X */
340 static void xhci_cleanup_msix(struct xhci_hcd
*xhci
)
342 struct pci_dev
*pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
346 if (xhci
->msix_entries
) {
347 pci_disable_msix(pdev
);
348 kfree(xhci
->msix_entries
);
349 xhci
->msix_entries
= NULL
;
351 pci_disable_msi(pdev
);
358 * Initialize memory for HCD and xHC (one-time init).
360 * Program the PAGESIZE register, initialize the device context array, create
361 * device contexts (?), set up a command ring segment (or two?), create event
362 * ring (one for now).
364 int xhci_init(struct usb_hcd
*hcd
)
366 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
369 xhci_dbg(xhci
, "xhci_init\n");
370 spin_lock_init(&xhci
->lock
);
372 xhci_dbg(xhci
, "QUIRK: Not clearing Link TRB chain bits.\n");
373 xhci
->quirks
|= XHCI_LINK_TRB_QUIRK
;
375 xhci_dbg(xhci
, "xHCI doesn't need link TRB QUIRK\n");
377 retval
= xhci_mem_init(xhci
, GFP_KERNEL
);
378 xhci_dbg(xhci
, "Finished xhci_init\n");
383 /*-------------------------------------------------------------------------*/
386 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
387 void xhci_event_ring_work(unsigned long arg
)
392 struct xhci_hcd
*xhci
= (struct xhci_hcd
*) arg
;
395 xhci_dbg(xhci
, "Poll event ring: %lu\n", jiffies
);
397 spin_lock_irqsave(&xhci
->lock
, flags
);
398 temp
= xhci_readl(xhci
, &xhci
->op_regs
->status
);
399 xhci_dbg(xhci
, "op reg status = 0x%x\n", temp
);
400 if (temp
== 0xffffffff || (xhci
->xhc_state
& XHCI_STATE_DYING
)) {
401 xhci_dbg(xhci
, "HW died, polling stopped.\n");
402 spin_unlock_irqrestore(&xhci
->lock
, flags
);
406 temp
= xhci_readl(xhci
, &xhci
->ir_set
->irq_pending
);
407 xhci_dbg(xhci
, "ir_set 0 pending = 0x%x\n", temp
);
408 xhci_dbg(xhci
, "No-op commands handled = %d\n", xhci
->noops_handled
);
409 xhci_dbg(xhci
, "HC error bitmask = 0x%x\n", xhci
->error_bitmask
);
410 xhci
->error_bitmask
= 0;
411 xhci_dbg(xhci
, "Event ring:\n");
412 xhci_debug_segment(xhci
, xhci
->event_ring
->deq_seg
);
413 xhci_dbg_ring_ptrs(xhci
, xhci
->event_ring
);
414 temp_64
= xhci_read_64(xhci
, &xhci
->ir_set
->erst_dequeue
);
415 temp_64
&= ~ERST_PTR_MASK
;
416 xhci_dbg(xhci
, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64
);
417 xhci_dbg(xhci
, "Command ring:\n");
418 xhci_debug_segment(xhci
, xhci
->cmd_ring
->deq_seg
);
419 xhci_dbg_ring_ptrs(xhci
, xhci
->cmd_ring
);
420 xhci_dbg_cmd_ptrs(xhci
);
421 for (i
= 0; i
< MAX_HC_SLOTS
; ++i
) {
424 for (j
= 0; j
< 31; ++j
) {
425 xhci_dbg_ep_rings(xhci
, i
, j
, &xhci
->devs
[i
]->eps
[j
]);
429 if (xhci
->noops_submitted
!= NUM_TEST_NOOPS
)
430 if (xhci_setup_one_noop(xhci
))
431 xhci_ring_cmd_db(xhci
);
432 spin_unlock_irqrestore(&xhci
->lock
, flags
);
435 mod_timer(&xhci
->event_ring_timer
, jiffies
+ POLL_TIMEOUT
* HZ
);
437 xhci_dbg(xhci
, "Quit polling the event ring.\n");
442 * Start the HC after it was halted.
444 * This function is called by the USB core when the HC driver is added.
445 * Its opposite is xhci_stop().
447 * xhci_init() must be called once before this function can be called.
448 * Reset the HC, enable device slot contexts, program DCBAAP, and
449 * set command ring pointer and event ring pointer.
451 * Setup MSI-X vectors and enable interrupts.
453 int xhci_run(struct usb_hcd
*hcd
)
458 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
459 struct pci_dev
*pdev
= to_pci_dev(xhci_to_hcd(xhci
)->self
.controller
);
460 void (*doorbell
)(struct xhci_hcd
*) = NULL
;
462 hcd
->uses_new_polling
= 1;
464 xhci_dbg(xhci
, "xhci_run\n");
465 /* unregister the legacy interrupt */
467 free_irq(hcd
->irq
, hcd
);
470 ret
= xhci_setup_msix(xhci
);
472 /* fall back to msi*/
473 ret
= xhci_setup_msi(xhci
);
476 /* fall back to legacy interrupt*/
477 ret
= request_irq(pdev
->irq
, &usb_hcd_irq
, IRQF_SHARED
,
478 hcd
->irq_descr
, hcd
);
480 xhci_err(xhci
, "request interrupt %d failed\n",
484 hcd
->irq
= pdev
->irq
;
487 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
488 init_timer(&xhci
->event_ring_timer
);
489 xhci
->event_ring_timer
.data
= (unsigned long) xhci
;
490 xhci
->event_ring_timer
.function
= xhci_event_ring_work
;
491 /* Poll the event ring */
492 xhci
->event_ring_timer
.expires
= jiffies
+ POLL_TIMEOUT
* HZ
;
494 xhci_dbg(xhci
, "Setting event ring polling timer\n");
495 add_timer(&xhci
->event_ring_timer
);
498 xhci_dbg(xhci
, "Command ring memory map follows:\n");
499 xhci_debug_ring(xhci
, xhci
->cmd_ring
);
500 xhci_dbg_ring_ptrs(xhci
, xhci
->cmd_ring
);
501 xhci_dbg_cmd_ptrs(xhci
);
503 xhci_dbg(xhci
, "ERST memory map follows:\n");
504 xhci_dbg_erst(xhci
, &xhci
->erst
);
505 xhci_dbg(xhci
, "Event ring:\n");
506 xhci_debug_ring(xhci
, xhci
->event_ring
);
507 xhci_dbg_ring_ptrs(xhci
, xhci
->event_ring
);
508 temp_64
= xhci_read_64(xhci
, &xhci
->ir_set
->erst_dequeue
);
509 temp_64
&= ~ERST_PTR_MASK
;
510 xhci_dbg(xhci
, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64
);
512 xhci_dbg(xhci
, "// Set the interrupt modulation register\n");
513 temp
= xhci_readl(xhci
, &xhci
->ir_set
->irq_control
);
514 temp
&= ~ER_IRQ_INTERVAL_MASK
;
516 xhci_writel(xhci
, temp
, &xhci
->ir_set
->irq_control
);
518 /* Set the HCD state before we enable the irqs */
519 hcd
->state
= HC_STATE_RUNNING
;
520 temp
= xhci_readl(xhci
, &xhci
->op_regs
->command
);
522 xhci_dbg(xhci
, "// Enable interrupts, cmd = 0x%x.\n",
524 xhci_writel(xhci
, temp
, &xhci
->op_regs
->command
);
526 temp
= xhci_readl(xhci
, &xhci
->ir_set
->irq_pending
);
527 xhci_dbg(xhci
, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
528 xhci
->ir_set
, (unsigned int) ER_IRQ_ENABLE(temp
));
529 xhci_writel(xhci
, ER_IRQ_ENABLE(temp
),
530 &xhci
->ir_set
->irq_pending
);
531 xhci_print_ir_set(xhci
, xhci
->ir_set
, 0);
533 if (NUM_TEST_NOOPS
> 0)
534 doorbell
= xhci_setup_one_noop(xhci
);
535 if (xhci
->quirks
& XHCI_NEC_HOST
)
536 xhci_queue_vendor_command(xhci
, 0, 0, 0,
537 TRB_TYPE(TRB_NEC_GET_FW
));
539 if (xhci_start(xhci
)) {
546 if (xhci
->quirks
& XHCI_NEC_HOST
)
547 xhci_ring_cmd_db(xhci
);
549 xhci_dbg(xhci
, "Finished xhci_run\n");
556 * This function is called by the USB core when the HC driver is removed.
557 * Its opposite is xhci_run().
559 * Disable device contexts, disable IRQs, and quiesce the HC.
560 * Reset the HC, finish any completed transactions, and cleanup memory.
562 void xhci_stop(struct usb_hcd
*hcd
)
565 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
567 spin_lock_irq(&xhci
->lock
);
570 spin_unlock_irq(&xhci
->lock
);
572 xhci_cleanup_msix(xhci
);
574 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
575 /* Tell the event ring poll function not to reschedule */
577 del_timer_sync(&xhci
->event_ring_timer
);
580 xhci_dbg(xhci
, "// Disabling event ring interrupts\n");
581 temp
= xhci_readl(xhci
, &xhci
->op_regs
->status
);
582 xhci_writel(xhci
, temp
& ~STS_EINT
, &xhci
->op_regs
->status
);
583 temp
= xhci_readl(xhci
, &xhci
->ir_set
->irq_pending
);
584 xhci_writel(xhci
, ER_IRQ_DISABLE(temp
),
585 &xhci
->ir_set
->irq_pending
);
586 xhci_print_ir_set(xhci
, xhci
->ir_set
, 0);
588 xhci_dbg(xhci
, "cleaning up memory\n");
589 xhci_mem_cleanup(xhci
);
590 xhci_dbg(xhci
, "xhci_stop completed - status = %x\n",
591 xhci_readl(xhci
, &xhci
->op_regs
->status
));
595 * Shutdown HC (not bus-specific)
597 * This is called when the machine is rebooting or halting. We assume that the
598 * machine will be powered off, and the HC's internal state will be reset.
599 * Don't bother to free memory.
601 void xhci_shutdown(struct usb_hcd
*hcd
)
603 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
605 spin_lock_irq(&xhci
->lock
);
607 spin_unlock_irq(&xhci
->lock
);
609 xhci_cleanup_msix(xhci
);
611 xhci_dbg(xhci
, "xhci_shutdown completed - status = %x\n",
612 xhci_readl(xhci
, &xhci
->op_regs
->status
));
615 /*-------------------------------------------------------------------------*/
618 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
619 * HCDs. Find the index for an endpoint given its descriptor. Use the return
620 * value to right shift 1 for the bitmask.
622 * Index = (epnum * 2) + direction - 1,
623 * where direction = 0 for OUT, 1 for IN.
624 * For control endpoints, the IN index is used (OUT index is unused), so
625 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
627 unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor
*desc
)
630 if (usb_endpoint_xfer_control(desc
))
631 index
= (unsigned int) (usb_endpoint_num(desc
)*2);
633 index
= (unsigned int) (usb_endpoint_num(desc
)*2) +
634 (usb_endpoint_dir_in(desc
) ? 1 : 0) - 1;
638 /* Find the flag for this endpoint (for use in the control context). Use the
639 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
642 unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor
*desc
)
644 return 1 << (xhci_get_endpoint_index(desc
) + 1);
647 /* Find the flag for this endpoint (for use in the control context). Use the
648 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
651 unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index
)
653 return 1 << (ep_index
+ 1);
656 /* Compute the last valid endpoint context index. Basically, this is the
657 * endpoint index plus one. For slot contexts with more than valid endpoint,
658 * we find the most significant bit set in the added contexts flags.
659 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
660 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
662 unsigned int xhci_last_valid_endpoint(u32 added_ctxs
)
664 return fls(added_ctxs
) - 1;
667 /* Returns 1 if the arguments are OK;
668 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
670 int xhci_check_args(struct usb_hcd
*hcd
, struct usb_device
*udev
,
671 struct usb_host_endpoint
*ep
, int check_ep
, const char *func
) {
672 if (!hcd
|| (check_ep
&& !ep
) || !udev
) {
673 printk(KERN_DEBUG
"xHCI %s called with invalid args\n",
678 printk(KERN_DEBUG
"xHCI %s called for root hub\n",
682 if (!udev
->slot_id
) {
683 printk(KERN_DEBUG
"xHCI %s called with unaddressed device\n",
690 static int xhci_configure_endpoint(struct xhci_hcd
*xhci
,
691 struct usb_device
*udev
, struct xhci_command
*command
,
692 bool ctx_change
, bool must_succeed
);
695 * Full speed devices may have a max packet size greater than 8 bytes, but the
696 * USB core doesn't know that until it reads the first 8 bytes of the
697 * descriptor. If the usb_device's max packet size changes after that point,
698 * we need to issue an evaluate context command and wait on it.
700 static int xhci_check_maxpacket(struct xhci_hcd
*xhci
, unsigned int slot_id
,
701 unsigned int ep_index
, struct urb
*urb
)
703 struct xhci_container_ctx
*in_ctx
;
704 struct xhci_container_ctx
*out_ctx
;
705 struct xhci_input_control_ctx
*ctrl_ctx
;
706 struct xhci_ep_ctx
*ep_ctx
;
708 int hw_max_packet_size
;
711 out_ctx
= xhci
->devs
[slot_id
]->out_ctx
;
712 ep_ctx
= xhci_get_ep_ctx(xhci
, out_ctx
, ep_index
);
713 hw_max_packet_size
= MAX_PACKET_DECODED(ep_ctx
->ep_info2
);
714 max_packet_size
= urb
->dev
->ep0
.desc
.wMaxPacketSize
;
715 if (hw_max_packet_size
!= max_packet_size
) {
716 xhci_dbg(xhci
, "Max Packet Size for ep 0 changed.\n");
717 xhci_dbg(xhci
, "Max packet size in usb_device = %d\n",
719 xhci_dbg(xhci
, "Max packet size in xHCI HW = %d\n",
721 xhci_dbg(xhci
, "Issuing evaluate context command.\n");
723 /* Set up the modified control endpoint 0 */
724 xhci_endpoint_copy(xhci
, xhci
->devs
[slot_id
]->in_ctx
,
725 xhci
->devs
[slot_id
]->out_ctx
, ep_index
);
726 in_ctx
= xhci
->devs
[slot_id
]->in_ctx
;
727 ep_ctx
= xhci_get_ep_ctx(xhci
, in_ctx
, ep_index
);
728 ep_ctx
->ep_info2
&= ~MAX_PACKET_MASK
;
729 ep_ctx
->ep_info2
|= MAX_PACKET(max_packet_size
);
731 /* Set up the input context flags for the command */
732 ctrl_ctx
= xhci_get_input_control_ctx(xhci
, in_ctx
);
733 ctrl_ctx
->add_flags
= EP0_FLAG
;
734 ctrl_ctx
->drop_flags
= 0;
736 xhci_dbg(xhci
, "Slot %d input context\n", slot_id
);
737 xhci_dbg_ctx(xhci
, in_ctx
, ep_index
);
738 xhci_dbg(xhci
, "Slot %d output context\n", slot_id
);
739 xhci_dbg_ctx(xhci
, out_ctx
, ep_index
);
741 ret
= xhci_configure_endpoint(xhci
, urb
->dev
, NULL
,
744 /* Clean up the input context for later use by bandwidth
747 ctrl_ctx
->add_flags
= SLOT_FLAG
;
753 * non-error returns are a promise to giveback() the urb later
754 * we drop ownership so next owner (or urb unlink) can get it
756 int xhci_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
, gfp_t mem_flags
)
758 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
761 unsigned int slot_id
, ep_index
;
762 struct urb_priv
*urb_priv
;
765 if (!urb
|| xhci_check_args(hcd
, urb
->dev
, urb
->ep
, true, __func__
) <= 0)
768 slot_id
= urb
->dev
->slot_id
;
769 ep_index
= xhci_get_endpoint_index(&urb
->ep
->desc
);
771 if (!xhci
->devs
|| !xhci
->devs
[slot_id
]) {
773 dev_warn(&urb
->dev
->dev
, "WARN: urb submitted for dev with no Slot ID\n");
777 if (!HCD_HW_ACCESSIBLE(hcd
)) {
779 xhci_dbg(xhci
, "urb submitted during PCI suspend\n");
784 if (usb_endpoint_xfer_isoc(&urb
->ep
->desc
))
785 size
= urb
->number_of_packets
;
789 urb_priv
= kzalloc(sizeof(struct urb_priv
) +
790 size
* sizeof(struct xhci_td
*), mem_flags
);
794 for (i
= 0; i
< size
; i
++) {
795 urb_priv
->td
[i
] = kzalloc(sizeof(struct xhci_td
), mem_flags
);
796 if (!urb_priv
->td
[i
]) {
797 urb_priv
->length
= i
;
798 xhci_urb_free_priv(xhci
, urb_priv
);
803 urb_priv
->length
= size
;
804 urb_priv
->td_cnt
= 0;
805 urb
->hcpriv
= urb_priv
;
807 if (usb_endpoint_xfer_control(&urb
->ep
->desc
)) {
808 /* Check to see if the max packet size for the default control
809 * endpoint changed during FS device enumeration
811 if (urb
->dev
->speed
== USB_SPEED_FULL
) {
812 ret
= xhci_check_maxpacket(xhci
, slot_id
,
818 /* We have a spinlock and interrupts disabled, so we must pass
819 * atomic context to this function, which may allocate memory.
821 spin_lock_irqsave(&xhci
->lock
, flags
);
822 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
824 ret
= xhci_queue_ctrl_tx(xhci
, GFP_ATOMIC
, urb
,
826 spin_unlock_irqrestore(&xhci
->lock
, flags
);
827 } else if (usb_endpoint_xfer_bulk(&urb
->ep
->desc
)) {
828 spin_lock_irqsave(&xhci
->lock
, flags
);
829 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
831 if (xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
&
832 EP_GETTING_STREAMS
) {
833 xhci_warn(xhci
, "WARN: Can't enqueue URB while bulk ep "
834 "is transitioning to using streams.\n");
836 } else if (xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
&
837 EP_GETTING_NO_STREAMS
) {
838 xhci_warn(xhci
, "WARN: Can't enqueue URB while bulk ep "
839 "is transitioning to "
840 "not having streams.\n");
843 ret
= xhci_queue_bulk_tx(xhci
, GFP_ATOMIC
, urb
,
846 spin_unlock_irqrestore(&xhci
->lock
, flags
);
847 } else if (usb_endpoint_xfer_int(&urb
->ep
->desc
)) {
848 spin_lock_irqsave(&xhci
->lock
, flags
);
849 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
851 ret
= xhci_queue_intr_tx(xhci
, GFP_ATOMIC
, urb
,
853 spin_unlock_irqrestore(&xhci
->lock
, flags
);
855 spin_lock_irqsave(&xhci
->lock
, flags
);
856 if (xhci
->xhc_state
& XHCI_STATE_DYING
)
858 ret
= xhci_queue_isoc_tx_prepare(xhci
, GFP_ATOMIC
, urb
,
860 spin_unlock_irqrestore(&xhci
->lock
, flags
);
865 xhci_urb_free_priv(xhci
, urb_priv
);
867 xhci_dbg(xhci
, "Ep 0x%x: URB %p submitted for "
868 "non-responsive xHCI host.\n",
869 urb
->ep
->desc
.bEndpointAddress
, urb
);
870 spin_unlock_irqrestore(&xhci
->lock
, flags
);
874 /* Get the right ring for the given URB.
875 * If the endpoint supports streams, boundary check the URB's stream ID.
876 * If the endpoint doesn't support streams, return the singular endpoint ring.
878 static struct xhci_ring
*xhci_urb_to_transfer_ring(struct xhci_hcd
*xhci
,
881 unsigned int slot_id
;
882 unsigned int ep_index
;
883 unsigned int stream_id
;
884 struct xhci_virt_ep
*ep
;
886 slot_id
= urb
->dev
->slot_id
;
887 ep_index
= xhci_get_endpoint_index(&urb
->ep
->desc
);
888 stream_id
= urb
->stream_id
;
889 ep
= &xhci
->devs
[slot_id
]->eps
[ep_index
];
890 /* Common case: no streams */
891 if (!(ep
->ep_state
& EP_HAS_STREAMS
))
894 if (stream_id
== 0) {
896 "WARN: Slot ID %u, ep index %u has streams, "
897 "but URB has no stream ID.\n",
902 if (stream_id
< ep
->stream_info
->num_streams
)
903 return ep
->stream_info
->stream_rings
[stream_id
];
906 "WARN: Slot ID %u, ep index %u has "
907 "stream IDs 1 to %u allocated, "
908 "but stream ID %u is requested.\n",
910 ep
->stream_info
->num_streams
- 1,
916 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
917 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
918 * should pick up where it left off in the TD, unless a Set Transfer Ring
919 * Dequeue Pointer is issued.
921 * The TRBs that make up the buffers for the canceled URB will be "removed" from
922 * the ring. Since the ring is a contiguous structure, they can't be physically
923 * removed. Instead, there are two options:
925 * 1) If the HC is in the middle of processing the URB to be canceled, we
926 * simply move the ring's dequeue pointer past those TRBs using the Set
927 * Transfer Ring Dequeue Pointer command. This will be the common case,
928 * when drivers timeout on the last submitted URB and attempt to cancel.
930 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
931 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
932 * HC will need to invalidate the any TRBs it has cached after the stop
933 * endpoint command, as noted in the xHCI 0.95 errata.
935 * 3) The TD may have completed by the time the Stop Endpoint Command
936 * completes, so software needs to handle that case too.
938 * This function should protect against the TD enqueueing code ringing the
939 * doorbell while this code is waiting for a Stop Endpoint command to complete.
940 * It also needs to account for multiple cancellations on happening at the same
941 * time for the same endpoint.
943 * Note that this function can be called in any context, or so says
944 * usb_hcd_unlink_urb()
946 int xhci_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
951 struct xhci_hcd
*xhci
;
952 struct urb_priv
*urb_priv
;
954 unsigned int ep_index
;
955 struct xhci_ring
*ep_ring
;
956 struct xhci_virt_ep
*ep
;
958 xhci
= hcd_to_xhci(hcd
);
959 spin_lock_irqsave(&xhci
->lock
, flags
);
960 /* Make sure the URB hasn't completed or been unlinked already */
961 ret
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
962 if (ret
|| !urb
->hcpriv
)
964 temp
= xhci_readl(xhci
, &xhci
->op_regs
->status
);
965 if (temp
== 0xffffffff) {
966 xhci_dbg(xhci
, "HW died, freeing TD.\n");
967 urb_priv
= urb
->hcpriv
;
969 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
970 spin_unlock_irqrestore(&xhci
->lock
, flags
);
971 usb_hcd_giveback_urb(xhci_to_hcd(xhci
), urb
, -ESHUTDOWN
);
972 xhci_urb_free_priv(xhci
, urb_priv
);
975 if (xhci
->xhc_state
& XHCI_STATE_DYING
) {
976 xhci_dbg(xhci
, "Ep 0x%x: URB %p to be canceled on "
977 "non-responsive xHCI host.\n",
978 urb
->ep
->desc
.bEndpointAddress
, urb
);
979 /* Let the stop endpoint command watchdog timer (which set this
980 * state) finish cleaning up the endpoint TD lists. We must
981 * have caught it in the middle of dropping a lock and giving
987 xhci_dbg(xhci
, "Cancel URB %p\n", urb
);
988 xhci_dbg(xhci
, "Event ring:\n");
989 xhci_debug_ring(xhci
, xhci
->event_ring
);
990 ep_index
= xhci_get_endpoint_index(&urb
->ep
->desc
);
991 ep
= &xhci
->devs
[urb
->dev
->slot_id
]->eps
[ep_index
];
992 ep_ring
= xhci_urb_to_transfer_ring(xhci
, urb
);
998 xhci_dbg(xhci
, "Endpoint ring:\n");
999 xhci_debug_ring(xhci
, ep_ring
);
1001 urb_priv
= urb
->hcpriv
;
1003 for (i
= urb_priv
->td_cnt
; i
< urb_priv
->length
; i
++) {
1004 td
= urb_priv
->td
[i
];
1005 list_add_tail(&td
->cancelled_td_list
, &ep
->cancelled_td_list
);
1008 /* Queue a stop endpoint command, but only if this is
1009 * the first cancellation to be handled.
1011 if (!(ep
->ep_state
& EP_HALT_PENDING
)) {
1012 ep
->ep_state
|= EP_HALT_PENDING
;
1013 ep
->stop_cmds_pending
++;
1014 ep
->stop_cmd_timer
.expires
= jiffies
+
1015 XHCI_STOP_EP_CMD_TIMEOUT
* HZ
;
1016 add_timer(&ep
->stop_cmd_timer
);
1017 xhci_queue_stop_endpoint(xhci
, urb
->dev
->slot_id
, ep_index
);
1018 xhci_ring_cmd_db(xhci
);
1021 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1025 /* Drop an endpoint from a new bandwidth configuration for this device.
1026 * Only one call to this function is allowed per endpoint before
1027 * check_bandwidth() or reset_bandwidth() must be called.
1028 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1029 * add the endpoint to the schedule with possibly new parameters denoted by a
1030 * different endpoint descriptor in usb_host_endpoint.
1031 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1034 * The USB core will not allow URBs to be queued to an endpoint that is being
1035 * disabled, so there's no need for mutual exclusion to protect
1036 * the xhci->devs[slot_id] structure.
1038 int xhci_drop_endpoint(struct usb_hcd
*hcd
, struct usb_device
*udev
,
1039 struct usb_host_endpoint
*ep
)
1041 struct xhci_hcd
*xhci
;
1042 struct xhci_container_ctx
*in_ctx
, *out_ctx
;
1043 struct xhci_input_control_ctx
*ctrl_ctx
;
1044 struct xhci_slot_ctx
*slot_ctx
;
1045 unsigned int last_ctx
;
1046 unsigned int ep_index
;
1047 struct xhci_ep_ctx
*ep_ctx
;
1049 u32 new_add_flags
, new_drop_flags
, new_slot_info
;
1052 ret
= xhci_check_args(hcd
, udev
, ep
, 1, __func__
);
1055 xhci
= hcd_to_xhci(hcd
);
1056 xhci_dbg(xhci
, "%s called for udev %p\n", __func__
, udev
);
1058 drop_flag
= xhci_get_endpoint_flag(&ep
->desc
);
1059 if (drop_flag
== SLOT_FLAG
|| drop_flag
== EP0_FLAG
) {
1060 xhci_dbg(xhci
, "xHCI %s - can't drop slot or ep 0 %#x\n",
1061 __func__
, drop_flag
);
1065 if (!xhci
->devs
|| !xhci
->devs
[udev
->slot_id
]) {
1066 xhci_warn(xhci
, "xHCI %s called with unaddressed device\n",
1071 in_ctx
= xhci
->devs
[udev
->slot_id
]->in_ctx
;
1072 out_ctx
= xhci
->devs
[udev
->slot_id
]->out_ctx
;
1073 ctrl_ctx
= xhci_get_input_control_ctx(xhci
, in_ctx
);
1074 ep_index
= xhci_get_endpoint_index(&ep
->desc
);
1075 ep_ctx
= xhci_get_ep_ctx(xhci
, out_ctx
, ep_index
);
1076 /* If the HC already knows the endpoint is disabled,
1077 * or the HCD has noted it is disabled, ignore this request
1079 if ((ep_ctx
->ep_info
& EP_STATE_MASK
) == EP_STATE_DISABLED
||
1080 ctrl_ctx
->drop_flags
& xhci_get_endpoint_flag(&ep
->desc
)) {
1081 xhci_warn(xhci
, "xHCI %s called with disabled ep %p\n",
1086 ctrl_ctx
->drop_flags
|= drop_flag
;
1087 new_drop_flags
= ctrl_ctx
->drop_flags
;
1089 ctrl_ctx
->add_flags
&= ~drop_flag
;
1090 new_add_flags
= ctrl_ctx
->add_flags
;
1092 last_ctx
= xhci_last_valid_endpoint(ctrl_ctx
->add_flags
);
1093 slot_ctx
= xhci_get_slot_ctx(xhci
, in_ctx
);
1094 /* Update the last valid endpoint context, if we deleted the last one */
1095 if ((slot_ctx
->dev_info
& LAST_CTX_MASK
) > LAST_CTX(last_ctx
)) {
1096 slot_ctx
->dev_info
&= ~LAST_CTX_MASK
;
1097 slot_ctx
->dev_info
|= LAST_CTX(last_ctx
);
1099 new_slot_info
= slot_ctx
->dev_info
;
1101 xhci_endpoint_zero(xhci
, xhci
->devs
[udev
->slot_id
], ep
);
1103 xhci_dbg(xhci
, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1104 (unsigned int) ep
->desc
.bEndpointAddress
,
1106 (unsigned int) new_drop_flags
,
1107 (unsigned int) new_add_flags
,
1108 (unsigned int) new_slot_info
);
1112 /* Add an endpoint to a new possible bandwidth configuration for this device.
1113 * Only one call to this function is allowed per endpoint before
1114 * check_bandwidth() or reset_bandwidth() must be called.
1115 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1116 * add the endpoint to the schedule with possibly new parameters denoted by a
1117 * different endpoint descriptor in usb_host_endpoint.
1118 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1121 * The USB core will not allow URBs to be queued to an endpoint until the
1122 * configuration or alt setting is installed in the device, so there's no need
1123 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1125 int xhci_add_endpoint(struct usb_hcd
*hcd
, struct usb_device
*udev
,
1126 struct usb_host_endpoint
*ep
)
1128 struct xhci_hcd
*xhci
;
1129 struct xhci_container_ctx
*in_ctx
, *out_ctx
;
1130 unsigned int ep_index
;
1131 struct xhci_ep_ctx
*ep_ctx
;
1132 struct xhci_slot_ctx
*slot_ctx
;
1133 struct xhci_input_control_ctx
*ctrl_ctx
;
1135 unsigned int last_ctx
;
1136 u32 new_add_flags
, new_drop_flags
, new_slot_info
;
1139 ret
= xhci_check_args(hcd
, udev
, ep
, 1, __func__
);
1141 /* So we won't queue a reset ep command for a root hub */
1145 xhci
= hcd_to_xhci(hcd
);
1147 added_ctxs
= xhci_get_endpoint_flag(&ep
->desc
);
1148 last_ctx
= xhci_last_valid_endpoint(added_ctxs
);
1149 if (added_ctxs
== SLOT_FLAG
|| added_ctxs
== EP0_FLAG
) {
1150 xhci_dbg(xhci
, "xHCI %s - can't add slot or ep 0 %#x\n",
1151 __func__
, added_ctxs
);
1155 if (!xhci
->devs
|| !xhci
->devs
[udev
->slot_id
]) {
1156 xhci_warn(xhci
, "xHCI %s called with unaddressed device\n",
1161 in_ctx
= xhci
->devs
[udev
->slot_id
]->in_ctx
;
1162 out_ctx
= xhci
->devs
[udev
->slot_id
]->out_ctx
;
1163 ctrl_ctx
= xhci_get_input_control_ctx(xhci
, in_ctx
);
1164 ep_index
= xhci_get_endpoint_index(&ep
->desc
);
1165 ep_ctx
= xhci_get_ep_ctx(xhci
, out_ctx
, ep_index
);
1166 /* If the HCD has already noted the endpoint is enabled,
1167 * ignore this request.
1169 if (ctrl_ctx
->add_flags
& xhci_get_endpoint_flag(&ep
->desc
)) {
1170 xhci_warn(xhci
, "xHCI %s called with enabled ep %p\n",
1176 * Configuration and alternate setting changes must be done in
1177 * process context, not interrupt context (or so documenation
1178 * for usb_set_interface() and usb_set_configuration() claim).
1180 if (xhci_endpoint_init(xhci
, xhci
->devs
[udev
->slot_id
],
1181 udev
, ep
, GFP_NOIO
) < 0) {
1182 dev_dbg(&udev
->dev
, "%s - could not initialize ep %#x\n",
1183 __func__
, ep
->desc
.bEndpointAddress
);
1187 ctrl_ctx
->add_flags
|= added_ctxs
;
1188 new_add_flags
= ctrl_ctx
->add_flags
;
1190 /* If xhci_endpoint_disable() was called for this endpoint, but the
1191 * xHC hasn't been notified yet through the check_bandwidth() call,
1192 * this re-adds a new state for the endpoint from the new endpoint
1193 * descriptors. We must drop and re-add this endpoint, so we leave the
1196 new_drop_flags
= ctrl_ctx
->drop_flags
;
1198 slot_ctx
= xhci_get_slot_ctx(xhci
, in_ctx
);
1199 /* Update the last valid endpoint context, if we just added one past */
1200 if ((slot_ctx
->dev_info
& LAST_CTX_MASK
) < LAST_CTX(last_ctx
)) {
1201 slot_ctx
->dev_info
&= ~LAST_CTX_MASK
;
1202 slot_ctx
->dev_info
|= LAST_CTX(last_ctx
);
1204 new_slot_info
= slot_ctx
->dev_info
;
1206 /* Store the usb_device pointer for later use */
1209 xhci_dbg(xhci
, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1210 (unsigned int) ep
->desc
.bEndpointAddress
,
1212 (unsigned int) new_drop_flags
,
1213 (unsigned int) new_add_flags
,
1214 (unsigned int) new_slot_info
);
1218 static void xhci_zero_in_ctx(struct xhci_hcd
*xhci
, struct xhci_virt_device
*virt_dev
)
1220 struct xhci_input_control_ctx
*ctrl_ctx
;
1221 struct xhci_ep_ctx
*ep_ctx
;
1222 struct xhci_slot_ctx
*slot_ctx
;
1225 /* When a device's add flag and drop flag are zero, any subsequent
1226 * configure endpoint command will leave that endpoint's state
1227 * untouched. Make sure we don't leave any old state in the input
1228 * endpoint contexts.
1230 ctrl_ctx
= xhci_get_input_control_ctx(xhci
, virt_dev
->in_ctx
);
1231 ctrl_ctx
->drop_flags
= 0;
1232 ctrl_ctx
->add_flags
= 0;
1233 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->in_ctx
);
1234 slot_ctx
->dev_info
&= ~LAST_CTX_MASK
;
1235 /* Endpoint 0 is always valid */
1236 slot_ctx
->dev_info
|= LAST_CTX(1);
1237 for (i
= 1; i
< 31; ++i
) {
1238 ep_ctx
= xhci_get_ep_ctx(xhci
, virt_dev
->in_ctx
, i
);
1239 ep_ctx
->ep_info
= 0;
1240 ep_ctx
->ep_info2
= 0;
1242 ep_ctx
->tx_info
= 0;
1246 static int xhci_configure_endpoint_result(struct xhci_hcd
*xhci
,
1247 struct usb_device
*udev
, int *cmd_status
)
1251 switch (*cmd_status
) {
1253 dev_warn(&udev
->dev
, "Not enough host controller resources "
1254 "for new device state.\n");
1258 dev_warn(&udev
->dev
, "Not enough bandwidth "
1259 "for new device state.\n");
1263 /* the HCD set up something wrong */
1264 dev_warn(&udev
->dev
, "ERROR: Endpoint drop flag = 0, "
1266 "and endpoint is not disabled.\n");
1270 dev_dbg(&udev
->dev
, "Successful Endpoint Configure command\n");
1274 xhci_err(xhci
, "ERROR: unexpected command completion "
1275 "code 0x%x.\n", *cmd_status
);
1282 static int xhci_evaluate_context_result(struct xhci_hcd
*xhci
,
1283 struct usb_device
*udev
, int *cmd_status
)
1286 struct xhci_virt_device
*virt_dev
= xhci
->devs
[udev
->slot_id
];
1288 switch (*cmd_status
) {
1290 dev_warn(&udev
->dev
, "WARN: xHCI driver setup invalid evaluate "
1291 "context command.\n");
1295 dev_warn(&udev
->dev
, "WARN: slot not enabled for"
1296 "evaluate context command.\n");
1297 case COMP_CTX_STATE
:
1298 dev_warn(&udev
->dev
, "WARN: invalid context state for "
1299 "evaluate context command.\n");
1300 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 1);
1304 dev_dbg(&udev
->dev
, "Successful evaluate context command\n");
1308 xhci_err(xhci
, "ERROR: unexpected command completion "
1309 "code 0x%x.\n", *cmd_status
);
1316 /* Issue a configure endpoint command or evaluate context command
1317 * and wait for it to finish.
1319 static int xhci_configure_endpoint(struct xhci_hcd
*xhci
,
1320 struct usb_device
*udev
,
1321 struct xhci_command
*command
,
1322 bool ctx_change
, bool must_succeed
)
1326 unsigned long flags
;
1327 struct xhci_container_ctx
*in_ctx
;
1328 struct completion
*cmd_completion
;
1330 struct xhci_virt_device
*virt_dev
;
1332 spin_lock_irqsave(&xhci
->lock
, flags
);
1333 virt_dev
= xhci
->devs
[udev
->slot_id
];
1335 in_ctx
= command
->in_ctx
;
1336 cmd_completion
= command
->completion
;
1337 cmd_status
= &command
->status
;
1338 command
->command_trb
= xhci
->cmd_ring
->enqueue
;
1340 /* Enqueue pointer can be left pointing to the link TRB,
1341 * we must handle that
1343 if ((command
->command_trb
->link
.control
& TRB_TYPE_BITMASK
)
1344 == TRB_TYPE(TRB_LINK
))
1345 command
->command_trb
=
1346 xhci
->cmd_ring
->enq_seg
->next
->trbs
;
1348 list_add_tail(&command
->cmd_list
, &virt_dev
->cmd_list
);
1350 in_ctx
= virt_dev
->in_ctx
;
1351 cmd_completion
= &virt_dev
->cmd_completion
;
1352 cmd_status
= &virt_dev
->cmd_status
;
1354 init_completion(cmd_completion
);
1357 ret
= xhci_queue_configure_endpoint(xhci
, in_ctx
->dma
,
1358 udev
->slot_id
, must_succeed
);
1360 ret
= xhci_queue_evaluate_context(xhci
, in_ctx
->dma
,
1364 list_del(&command
->cmd_list
);
1365 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1366 xhci_dbg(xhci
, "FIXME allocate a new ring segment\n");
1369 xhci_ring_cmd_db(xhci
);
1370 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1372 /* Wait for the configure endpoint command to complete */
1373 timeleft
= wait_for_completion_interruptible_timeout(
1375 USB_CTRL_SET_TIMEOUT
);
1376 if (timeleft
<= 0) {
1377 xhci_warn(xhci
, "%s while waiting for %s command\n",
1378 timeleft
== 0 ? "Timeout" : "Signal",
1380 "configure endpoint" :
1381 "evaluate context");
1386 return xhci_configure_endpoint_result(xhci
, udev
, cmd_status
);
1387 return xhci_evaluate_context_result(xhci
, udev
, cmd_status
);
1390 /* Called after one or more calls to xhci_add_endpoint() or
1391 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1392 * to call xhci_reset_bandwidth().
1394 * Since we are in the middle of changing either configuration or
1395 * installing a new alt setting, the USB core won't allow URBs to be
1396 * enqueued for any endpoint on the old config or interface. Nothing
1397 * else should be touching the xhci->devs[slot_id] structure, so we
1398 * don't need to take the xhci->lock for manipulating that.
1400 int xhci_check_bandwidth(struct usb_hcd
*hcd
, struct usb_device
*udev
)
1404 struct xhci_hcd
*xhci
;
1405 struct xhci_virt_device
*virt_dev
;
1406 struct xhci_input_control_ctx
*ctrl_ctx
;
1407 struct xhci_slot_ctx
*slot_ctx
;
1409 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, __func__
);
1412 xhci
= hcd_to_xhci(hcd
);
1414 if (!udev
->slot_id
|| !xhci
->devs
|| !xhci
->devs
[udev
->slot_id
]) {
1415 xhci_warn(xhci
, "xHCI %s called with unaddressed device\n",
1419 xhci_dbg(xhci
, "%s called for udev %p\n", __func__
, udev
);
1420 virt_dev
= xhci
->devs
[udev
->slot_id
];
1422 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
1423 ctrl_ctx
= xhci_get_input_control_ctx(xhci
, virt_dev
->in_ctx
);
1424 ctrl_ctx
->add_flags
|= SLOT_FLAG
;
1425 ctrl_ctx
->add_flags
&= ~EP0_FLAG
;
1426 ctrl_ctx
->drop_flags
&= ~SLOT_FLAG
;
1427 ctrl_ctx
->drop_flags
&= ~EP0_FLAG
;
1428 xhci_dbg(xhci
, "New Input Control Context:\n");
1429 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->in_ctx
);
1430 xhci_dbg_ctx(xhci
, virt_dev
->in_ctx
,
1431 LAST_CTX_TO_EP_NUM(slot_ctx
->dev_info
));
1433 ret
= xhci_configure_endpoint(xhci
, udev
, NULL
,
1436 /* Callee should call reset_bandwidth() */
1440 xhci_dbg(xhci
, "Output context after successful config ep cmd:\n");
1441 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
,
1442 LAST_CTX_TO_EP_NUM(slot_ctx
->dev_info
));
1444 xhci_zero_in_ctx(xhci
, virt_dev
);
1445 /* Install new rings and free or cache any old rings */
1446 for (i
= 1; i
< 31; ++i
) {
1447 if (!virt_dev
->eps
[i
].new_ring
)
1449 /* Only cache or free the old ring if it exists.
1450 * It may not if this is the first add of an endpoint.
1452 if (virt_dev
->eps
[i
].ring
) {
1453 xhci_free_or_cache_endpoint_ring(xhci
, virt_dev
, i
);
1455 virt_dev
->eps
[i
].ring
= virt_dev
->eps
[i
].new_ring
;
1456 virt_dev
->eps
[i
].new_ring
= NULL
;
1462 void xhci_reset_bandwidth(struct usb_hcd
*hcd
, struct usb_device
*udev
)
1464 struct xhci_hcd
*xhci
;
1465 struct xhci_virt_device
*virt_dev
;
1468 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, __func__
);
1471 xhci
= hcd_to_xhci(hcd
);
1473 if (!xhci
->devs
|| !xhci
->devs
[udev
->slot_id
]) {
1474 xhci_warn(xhci
, "xHCI %s called with unaddressed device\n",
1478 xhci_dbg(xhci
, "%s called for udev %p\n", __func__
, udev
);
1479 virt_dev
= xhci
->devs
[udev
->slot_id
];
1480 /* Free any rings allocated for added endpoints */
1481 for (i
= 0; i
< 31; ++i
) {
1482 if (virt_dev
->eps
[i
].new_ring
) {
1483 xhci_ring_free(xhci
, virt_dev
->eps
[i
].new_ring
);
1484 virt_dev
->eps
[i
].new_ring
= NULL
;
1487 xhci_zero_in_ctx(xhci
, virt_dev
);
1490 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd
*xhci
,
1491 struct xhci_container_ctx
*in_ctx
,
1492 struct xhci_container_ctx
*out_ctx
,
1493 u32 add_flags
, u32 drop_flags
)
1495 struct xhci_input_control_ctx
*ctrl_ctx
;
1496 ctrl_ctx
= xhci_get_input_control_ctx(xhci
, in_ctx
);
1497 ctrl_ctx
->add_flags
= add_flags
;
1498 ctrl_ctx
->drop_flags
= drop_flags
;
1499 xhci_slot_copy(xhci
, in_ctx
, out_ctx
);
1500 ctrl_ctx
->add_flags
|= SLOT_FLAG
;
1502 xhci_dbg(xhci
, "Input Context:\n");
1503 xhci_dbg_ctx(xhci
, in_ctx
, xhci_last_valid_endpoint(add_flags
));
1506 void xhci_setup_input_ctx_for_quirk(struct xhci_hcd
*xhci
,
1507 unsigned int slot_id
, unsigned int ep_index
,
1508 struct xhci_dequeue_state
*deq_state
)
1510 struct xhci_container_ctx
*in_ctx
;
1511 struct xhci_ep_ctx
*ep_ctx
;
1515 xhci_endpoint_copy(xhci
, xhci
->devs
[slot_id
]->in_ctx
,
1516 xhci
->devs
[slot_id
]->out_ctx
, ep_index
);
1517 in_ctx
= xhci
->devs
[slot_id
]->in_ctx
;
1518 ep_ctx
= xhci_get_ep_ctx(xhci
, in_ctx
, ep_index
);
1519 addr
= xhci_trb_virt_to_dma(deq_state
->new_deq_seg
,
1520 deq_state
->new_deq_ptr
);
1522 xhci_warn(xhci
, "WARN Cannot submit config ep after "
1523 "reset ep command\n");
1524 xhci_warn(xhci
, "WARN deq seg = %p, deq ptr = %p\n",
1525 deq_state
->new_deq_seg
,
1526 deq_state
->new_deq_ptr
);
1529 ep_ctx
->deq
= addr
| deq_state
->new_cycle_state
;
1531 added_ctxs
= xhci_get_endpoint_flag_from_index(ep_index
);
1532 xhci_setup_input_ctx_for_config_ep(xhci
, xhci
->devs
[slot_id
]->in_ctx
,
1533 xhci
->devs
[slot_id
]->out_ctx
, added_ctxs
, added_ctxs
);
1536 void xhci_cleanup_stalled_ring(struct xhci_hcd
*xhci
,
1537 struct usb_device
*udev
, unsigned int ep_index
)
1539 struct xhci_dequeue_state deq_state
;
1540 struct xhci_virt_ep
*ep
;
1542 xhci_dbg(xhci
, "Cleaning up stalled endpoint ring\n");
1543 ep
= &xhci
->devs
[udev
->slot_id
]->eps
[ep_index
];
1544 /* We need to move the HW's dequeue pointer past this TD,
1545 * or it will attempt to resend it on the next doorbell ring.
1547 xhci_find_new_dequeue_state(xhci
, udev
->slot_id
,
1548 ep_index
, ep
->stopped_stream
, ep
->stopped_td
,
1551 /* HW with the reset endpoint quirk will use the saved dequeue state to
1552 * issue a configure endpoint command later.
1554 if (!(xhci
->quirks
& XHCI_RESET_EP_QUIRK
)) {
1555 xhci_dbg(xhci
, "Queueing new dequeue state\n");
1556 xhci_queue_new_dequeue_state(xhci
, udev
->slot_id
,
1557 ep_index
, ep
->stopped_stream
, &deq_state
);
1559 xhci_dbg(xhci
, "Setting up input context for "
1560 "configure endpoint command\n");
1561 xhci_setup_input_ctx_for_quirk(xhci
, udev
->slot_id
,
1562 ep_index
, &deq_state
);
1566 /* Deal with stalled endpoints. The core should have sent the control message
1567 * to clear the halt condition. However, we need to make the xHCI hardware
1568 * reset its sequence number, since a device will expect a sequence number of
1569 * zero after the halt condition is cleared.
1570 * Context: in_interrupt
1572 void xhci_endpoint_reset(struct usb_hcd
*hcd
,
1573 struct usb_host_endpoint
*ep
)
1575 struct xhci_hcd
*xhci
;
1576 struct usb_device
*udev
;
1577 unsigned int ep_index
;
1578 unsigned long flags
;
1580 struct xhci_virt_ep
*virt_ep
;
1582 xhci
= hcd_to_xhci(hcd
);
1583 udev
= (struct usb_device
*) ep
->hcpriv
;
1584 /* Called with a root hub endpoint (or an endpoint that wasn't added
1585 * with xhci_add_endpoint()
1589 ep_index
= xhci_get_endpoint_index(&ep
->desc
);
1590 virt_ep
= &xhci
->devs
[udev
->slot_id
]->eps
[ep_index
];
1591 if (!virt_ep
->stopped_td
) {
1592 xhci_dbg(xhci
, "Endpoint 0x%x not halted, refusing to reset.\n",
1593 ep
->desc
.bEndpointAddress
);
1596 if (usb_endpoint_xfer_control(&ep
->desc
)) {
1597 xhci_dbg(xhci
, "Control endpoint stall already handled.\n");
1601 xhci_dbg(xhci
, "Queueing reset endpoint command\n");
1602 spin_lock_irqsave(&xhci
->lock
, flags
);
1603 ret
= xhci_queue_reset_ep(xhci
, udev
->slot_id
, ep_index
);
1605 * Can't change the ring dequeue pointer until it's transitioned to the
1606 * stopped state, which is only upon a successful reset endpoint
1607 * command. Better hope that last command worked!
1610 xhci_cleanup_stalled_ring(xhci
, udev
, ep_index
);
1611 kfree(virt_ep
->stopped_td
);
1612 xhci_ring_cmd_db(xhci
);
1614 virt_ep
->stopped_td
= NULL
;
1615 virt_ep
->stopped_trb
= NULL
;
1616 virt_ep
->stopped_stream
= 0;
1617 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1620 xhci_warn(xhci
, "FIXME allocate a new ring segment\n");
1623 static int xhci_check_streams_endpoint(struct xhci_hcd
*xhci
,
1624 struct usb_device
*udev
, struct usb_host_endpoint
*ep
,
1625 unsigned int slot_id
)
1628 unsigned int ep_index
;
1629 unsigned int ep_state
;
1633 ret
= xhci_check_args(xhci_to_hcd(xhci
), udev
, ep
, 1, __func__
);
1636 if (ep
->ss_ep_comp
.bmAttributes
== 0) {
1637 xhci_warn(xhci
, "WARN: SuperSpeed Endpoint Companion"
1638 " descriptor for ep 0x%x does not support streams\n",
1639 ep
->desc
.bEndpointAddress
);
1643 ep_index
= xhci_get_endpoint_index(&ep
->desc
);
1644 ep_state
= xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
;
1645 if (ep_state
& EP_HAS_STREAMS
||
1646 ep_state
& EP_GETTING_STREAMS
) {
1647 xhci_warn(xhci
, "WARN: SuperSpeed bulk endpoint 0x%x "
1648 "already has streams set up.\n",
1649 ep
->desc
.bEndpointAddress
);
1650 xhci_warn(xhci
, "Send email to xHCI maintainer and ask for "
1651 "dynamic stream context array reallocation.\n");
1654 if (!list_empty(&xhci
->devs
[slot_id
]->eps
[ep_index
].ring
->td_list
)) {
1655 xhci_warn(xhci
, "Cannot setup streams for SuperSpeed bulk "
1656 "endpoint 0x%x; URBs are pending.\n",
1657 ep
->desc
.bEndpointAddress
);
1663 static void xhci_calculate_streams_entries(struct xhci_hcd
*xhci
,
1664 unsigned int *num_streams
, unsigned int *num_stream_ctxs
)
1666 unsigned int max_streams
;
1668 /* The stream context array size must be a power of two */
1669 *num_stream_ctxs
= roundup_pow_of_two(*num_streams
);
1671 * Find out how many primary stream array entries the host controller
1672 * supports. Later we may use secondary stream arrays (similar to 2nd
1673 * level page entries), but that's an optional feature for xHCI host
1674 * controllers. xHCs must support at least 4 stream IDs.
1676 max_streams
= HCC_MAX_PSA(xhci
->hcc_params
);
1677 if (*num_stream_ctxs
> max_streams
) {
1678 xhci_dbg(xhci
, "xHCI HW only supports %u stream ctx entries.\n",
1680 *num_stream_ctxs
= max_streams
;
1681 *num_streams
= max_streams
;
1685 /* Returns an error code if one of the endpoint already has streams.
1686 * This does not change any data structures, it only checks and gathers
1689 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd
*xhci
,
1690 struct usb_device
*udev
,
1691 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
1692 unsigned int *num_streams
, u32
*changed_ep_bitmask
)
1694 unsigned int max_streams
;
1695 unsigned int endpoint_flag
;
1699 for (i
= 0; i
< num_eps
; i
++) {
1700 ret
= xhci_check_streams_endpoint(xhci
, udev
,
1701 eps
[i
], udev
->slot_id
);
1705 max_streams
= USB_SS_MAX_STREAMS(
1706 eps
[i
]->ss_ep_comp
.bmAttributes
);
1707 if (max_streams
< (*num_streams
- 1)) {
1708 xhci_dbg(xhci
, "Ep 0x%x only supports %u stream IDs.\n",
1709 eps
[i
]->desc
.bEndpointAddress
,
1711 *num_streams
= max_streams
+1;
1714 endpoint_flag
= xhci_get_endpoint_flag(&eps
[i
]->desc
);
1715 if (*changed_ep_bitmask
& endpoint_flag
)
1717 *changed_ep_bitmask
|= endpoint_flag
;
1722 static u32
xhci_calculate_no_streams_bitmask(struct xhci_hcd
*xhci
,
1723 struct usb_device
*udev
,
1724 struct usb_host_endpoint
**eps
, unsigned int num_eps
)
1726 u32 changed_ep_bitmask
= 0;
1727 unsigned int slot_id
;
1728 unsigned int ep_index
;
1729 unsigned int ep_state
;
1732 slot_id
= udev
->slot_id
;
1733 if (!xhci
->devs
[slot_id
])
1736 for (i
= 0; i
< num_eps
; i
++) {
1737 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
1738 ep_state
= xhci
->devs
[slot_id
]->eps
[ep_index
].ep_state
;
1739 /* Are streams already being freed for the endpoint? */
1740 if (ep_state
& EP_GETTING_NO_STREAMS
) {
1741 xhci_warn(xhci
, "WARN Can't disable streams for "
1743 "streams are being disabled already.",
1744 eps
[i
]->desc
.bEndpointAddress
);
1747 /* Are there actually any streams to free? */
1748 if (!(ep_state
& EP_HAS_STREAMS
) &&
1749 !(ep_state
& EP_GETTING_STREAMS
)) {
1750 xhci_warn(xhci
, "WARN Can't disable streams for "
1752 "streams are already disabled!",
1753 eps
[i
]->desc
.bEndpointAddress
);
1754 xhci_warn(xhci
, "WARN xhci_free_streams() called "
1755 "with non-streams endpoint\n");
1758 changed_ep_bitmask
|= xhci_get_endpoint_flag(&eps
[i
]->desc
);
1760 return changed_ep_bitmask
;
1764 * The USB device drivers use this function (though the HCD interface in USB
1765 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
1766 * coordinate mass storage command queueing across multiple endpoints (basically
1767 * a stream ID == a task ID).
1769 * Setting up streams involves allocating the same size stream context array
1770 * for each endpoint and issuing a configure endpoint command for all endpoints.
1772 * Don't allow the call to succeed if one endpoint only supports one stream
1773 * (which means it doesn't support streams at all).
1775 * Drivers may get less stream IDs than they asked for, if the host controller
1776 * hardware or endpoints claim they can't support the number of requested
1779 int xhci_alloc_streams(struct usb_hcd
*hcd
, struct usb_device
*udev
,
1780 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
1781 unsigned int num_streams
, gfp_t mem_flags
)
1784 struct xhci_hcd
*xhci
;
1785 struct xhci_virt_device
*vdev
;
1786 struct xhci_command
*config_cmd
;
1787 unsigned int ep_index
;
1788 unsigned int num_stream_ctxs
;
1789 unsigned long flags
;
1790 u32 changed_ep_bitmask
= 0;
1795 /* Add one to the number of streams requested to account for
1796 * stream 0 that is reserved for xHCI usage.
1799 xhci
= hcd_to_xhci(hcd
);
1800 xhci_dbg(xhci
, "Driver wants %u stream IDs (including stream 0).\n",
1803 config_cmd
= xhci_alloc_command(xhci
, true, true, mem_flags
);
1805 xhci_dbg(xhci
, "Could not allocate xHCI command structure.\n");
1809 /* Check to make sure all endpoints are not already configured for
1810 * streams. While we're at it, find the maximum number of streams that
1811 * all the endpoints will support and check for duplicate endpoints.
1813 spin_lock_irqsave(&xhci
->lock
, flags
);
1814 ret
= xhci_calculate_streams_and_bitmask(xhci
, udev
, eps
,
1815 num_eps
, &num_streams
, &changed_ep_bitmask
);
1817 xhci_free_command(xhci
, config_cmd
);
1818 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1821 if (num_streams
<= 1) {
1822 xhci_warn(xhci
, "WARN: endpoints can't handle "
1823 "more than one stream.\n");
1824 xhci_free_command(xhci
, config_cmd
);
1825 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1828 vdev
= xhci
->devs
[udev
->slot_id
];
1829 /* Mark each endpoint as being in transistion, so
1830 * xhci_urb_enqueue() will reject all URBs.
1832 for (i
= 0; i
< num_eps
; i
++) {
1833 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
1834 vdev
->eps
[ep_index
].ep_state
|= EP_GETTING_STREAMS
;
1836 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1838 /* Setup internal data structures and allocate HW data structures for
1839 * streams (but don't install the HW structures in the input context
1840 * until we're sure all memory allocation succeeded).
1842 xhci_calculate_streams_entries(xhci
, &num_streams
, &num_stream_ctxs
);
1843 xhci_dbg(xhci
, "Need %u stream ctx entries for %u stream IDs.\n",
1844 num_stream_ctxs
, num_streams
);
1846 for (i
= 0; i
< num_eps
; i
++) {
1847 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
1848 vdev
->eps
[ep_index
].stream_info
= xhci_alloc_stream_info(xhci
,
1850 num_streams
, mem_flags
);
1851 if (!vdev
->eps
[ep_index
].stream_info
)
1855 /* Set up the input context for a configure endpoint command. */
1856 for (i
= 0; i
< num_eps
; i
++) {
1857 struct xhci_ep_ctx
*ep_ctx
;
1859 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
1860 ep_ctx
= xhci_get_ep_ctx(xhci
, config_cmd
->in_ctx
, ep_index
);
1862 xhci_endpoint_copy(xhci
, config_cmd
->in_ctx
,
1863 vdev
->out_ctx
, ep_index
);
1864 xhci_setup_streams_ep_input_ctx(xhci
, ep_ctx
,
1865 vdev
->eps
[ep_index
].stream_info
);
1867 /* Tell the HW to drop its old copy of the endpoint context info
1868 * and add the updated copy from the input context.
1870 xhci_setup_input_ctx_for_config_ep(xhci
, config_cmd
->in_ctx
,
1871 vdev
->out_ctx
, changed_ep_bitmask
, changed_ep_bitmask
);
1873 /* Issue and wait for the configure endpoint command */
1874 ret
= xhci_configure_endpoint(xhci
, udev
, config_cmd
,
1877 /* xHC rejected the configure endpoint command for some reason, so we
1878 * leave the old ring intact and free our internal streams data
1884 spin_lock_irqsave(&xhci
->lock
, flags
);
1885 for (i
= 0; i
< num_eps
; i
++) {
1886 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
1887 vdev
->eps
[ep_index
].ep_state
&= ~EP_GETTING_STREAMS
;
1888 xhci_dbg(xhci
, "Slot %u ep ctx %u now has streams.\n",
1889 udev
->slot_id
, ep_index
);
1890 vdev
->eps
[ep_index
].ep_state
|= EP_HAS_STREAMS
;
1892 xhci_free_command(xhci
, config_cmd
);
1893 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1895 /* Subtract 1 for stream 0, which drivers can't use */
1896 return num_streams
- 1;
1899 /* If it didn't work, free the streams! */
1900 for (i
= 0; i
< num_eps
; i
++) {
1901 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
1902 xhci_free_stream_info(xhci
, vdev
->eps
[ep_index
].stream_info
);
1903 vdev
->eps
[ep_index
].stream_info
= NULL
;
1904 vdev
->eps
[ep_index
].ep_state
&= ~EP_GETTING_STREAMS
;
1905 vdev
->eps
[ep_index
].ep_state
&= ~EP_HAS_STREAMS
;
1906 xhci_endpoint_zero(xhci
, vdev
, eps
[i
]);
1908 xhci_free_command(xhci
, config_cmd
);
1912 /* Transition the endpoint from using streams to being a "normal" endpoint
1915 * Modify the endpoint context state, submit a configure endpoint command,
1916 * and free all endpoint rings for streams if that completes successfully.
1918 int xhci_free_streams(struct usb_hcd
*hcd
, struct usb_device
*udev
,
1919 struct usb_host_endpoint
**eps
, unsigned int num_eps
,
1923 struct xhci_hcd
*xhci
;
1924 struct xhci_virt_device
*vdev
;
1925 struct xhci_command
*command
;
1926 unsigned int ep_index
;
1927 unsigned long flags
;
1928 u32 changed_ep_bitmask
;
1930 xhci
= hcd_to_xhci(hcd
);
1931 vdev
= xhci
->devs
[udev
->slot_id
];
1933 /* Set up a configure endpoint command to remove the streams rings */
1934 spin_lock_irqsave(&xhci
->lock
, flags
);
1935 changed_ep_bitmask
= xhci_calculate_no_streams_bitmask(xhci
,
1936 udev
, eps
, num_eps
);
1937 if (changed_ep_bitmask
== 0) {
1938 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1942 /* Use the xhci_command structure from the first endpoint. We may have
1943 * allocated too many, but the driver may call xhci_free_streams() for
1944 * each endpoint it grouped into one call to xhci_alloc_streams().
1946 ep_index
= xhci_get_endpoint_index(&eps
[0]->desc
);
1947 command
= vdev
->eps
[ep_index
].stream_info
->free_streams_command
;
1948 for (i
= 0; i
< num_eps
; i
++) {
1949 struct xhci_ep_ctx
*ep_ctx
;
1951 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
1952 ep_ctx
= xhci_get_ep_ctx(xhci
, command
->in_ctx
, ep_index
);
1953 xhci
->devs
[udev
->slot_id
]->eps
[ep_index
].ep_state
|=
1954 EP_GETTING_NO_STREAMS
;
1956 xhci_endpoint_copy(xhci
, command
->in_ctx
,
1957 vdev
->out_ctx
, ep_index
);
1958 xhci_setup_no_streams_ep_input_ctx(xhci
, ep_ctx
,
1959 &vdev
->eps
[ep_index
]);
1961 xhci_setup_input_ctx_for_config_ep(xhci
, command
->in_ctx
,
1962 vdev
->out_ctx
, changed_ep_bitmask
, changed_ep_bitmask
);
1963 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1965 /* Issue and wait for the configure endpoint command,
1966 * which must succeed.
1968 ret
= xhci_configure_endpoint(xhci
, udev
, command
,
1971 /* xHC rejected the configure endpoint command for some reason, so we
1972 * leave the streams rings intact.
1977 spin_lock_irqsave(&xhci
->lock
, flags
);
1978 for (i
= 0; i
< num_eps
; i
++) {
1979 ep_index
= xhci_get_endpoint_index(&eps
[i
]->desc
);
1980 xhci_free_stream_info(xhci
, vdev
->eps
[ep_index
].stream_info
);
1981 vdev
->eps
[ep_index
].stream_info
= NULL
;
1982 vdev
->eps
[ep_index
].ep_state
&= ~EP_GETTING_NO_STREAMS
;
1983 vdev
->eps
[ep_index
].ep_state
&= ~EP_HAS_STREAMS
;
1985 spin_unlock_irqrestore(&xhci
->lock
, flags
);
1991 * This submits a Reset Device Command, which will set the device state to 0,
1992 * set the device address to 0, and disable all the endpoints except the default
1993 * control endpoint. The USB core should come back and call
1994 * xhci_address_device(), and then re-set up the configuration. If this is
1995 * called because of a usb_reset_and_verify_device(), then the old alternate
1996 * settings will be re-installed through the normal bandwidth allocation
1999 * Wait for the Reset Device command to finish. Remove all structures
2000 * associated with the endpoints that were disabled. Clear the input device
2001 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
2003 int xhci_reset_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
2006 unsigned long flags
;
2007 struct xhci_hcd
*xhci
;
2008 unsigned int slot_id
;
2009 struct xhci_virt_device
*virt_dev
;
2010 struct xhci_command
*reset_device_cmd
;
2012 int last_freed_endpoint
;
2014 ret
= xhci_check_args(hcd
, udev
, NULL
, 0, __func__
);
2017 xhci
= hcd_to_xhci(hcd
);
2018 slot_id
= udev
->slot_id
;
2019 virt_dev
= xhci
->devs
[slot_id
];
2021 xhci_dbg(xhci
, "%s called with invalid slot ID %u\n",
2026 xhci_dbg(xhci
, "Resetting device with slot ID %u\n", slot_id
);
2027 /* Allocate the command structure that holds the struct completion.
2028 * Assume we're in process context, since the normal device reset
2029 * process has to wait for the device anyway. Storage devices are
2030 * reset as part of error handling, so use GFP_NOIO instead of
2033 reset_device_cmd
= xhci_alloc_command(xhci
, false, true, GFP_NOIO
);
2034 if (!reset_device_cmd
) {
2035 xhci_dbg(xhci
, "Couldn't allocate command structure.\n");
2039 /* Attempt to submit the Reset Device command to the command ring */
2040 spin_lock_irqsave(&xhci
->lock
, flags
);
2041 reset_device_cmd
->command_trb
= xhci
->cmd_ring
->enqueue
;
2043 /* Enqueue pointer can be left pointing to the link TRB,
2044 * we must handle that
2046 if ((reset_device_cmd
->command_trb
->link
.control
& TRB_TYPE_BITMASK
)
2047 == TRB_TYPE(TRB_LINK
))
2048 reset_device_cmd
->command_trb
=
2049 xhci
->cmd_ring
->enq_seg
->next
->trbs
;
2051 list_add_tail(&reset_device_cmd
->cmd_list
, &virt_dev
->cmd_list
);
2052 ret
= xhci_queue_reset_device(xhci
, slot_id
);
2054 xhci_dbg(xhci
, "FIXME: allocate a command ring segment\n");
2055 list_del(&reset_device_cmd
->cmd_list
);
2056 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2057 goto command_cleanup
;
2059 xhci_ring_cmd_db(xhci
);
2060 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2062 /* Wait for the Reset Device command to finish */
2063 timeleft
= wait_for_completion_interruptible_timeout(
2064 reset_device_cmd
->completion
,
2065 USB_CTRL_SET_TIMEOUT
);
2066 if (timeleft
<= 0) {
2067 xhci_warn(xhci
, "%s while waiting for reset device command\n",
2068 timeleft
== 0 ? "Timeout" : "Signal");
2069 spin_lock_irqsave(&xhci
->lock
, flags
);
2070 /* The timeout might have raced with the event ring handler, so
2071 * only delete from the list if the item isn't poisoned.
2073 if (reset_device_cmd
->cmd_list
.next
!= LIST_POISON1
)
2074 list_del(&reset_device_cmd
->cmd_list
);
2075 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2077 goto command_cleanup
;
2080 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
2081 * unless we tried to reset a slot ID that wasn't enabled,
2082 * or the device wasn't in the addressed or configured state.
2084 ret
= reset_device_cmd
->status
;
2086 case COMP_EBADSLT
: /* 0.95 completion code for bad slot ID */
2087 case COMP_CTX_STATE
: /* 0.96 completion code for same thing */
2088 xhci_info(xhci
, "Can't reset device (slot ID %u) in %s state\n",
2090 xhci_get_slot_state(xhci
, virt_dev
->out_ctx
));
2091 xhci_info(xhci
, "Not freeing device rings.\n");
2092 /* Don't treat this as an error. May change my mind later. */
2094 goto command_cleanup
;
2096 xhci_dbg(xhci
, "Successful reset device command.\n");
2099 if (xhci_is_vendor_info_code(xhci
, ret
))
2101 xhci_warn(xhci
, "Unknown completion code %u for "
2102 "reset device command.\n", ret
);
2104 goto command_cleanup
;
2107 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
2108 last_freed_endpoint
= 1;
2109 for (i
= 1; i
< 31; ++i
) {
2110 if (!virt_dev
->eps
[i
].ring
)
2112 xhci_free_or_cache_endpoint_ring(xhci
, virt_dev
, i
);
2113 last_freed_endpoint
= i
;
2115 xhci_dbg(xhci
, "Output context after successful reset device cmd:\n");
2116 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, last_freed_endpoint
);
2120 xhci_free_command(xhci
, reset_device_cmd
);
2125 * At this point, the struct usb_device is about to go away, the device has
2126 * disconnected, and all traffic has been stopped and the endpoints have been
2127 * disabled. Free any HC data structures associated with that device.
2129 void xhci_free_dev(struct usb_hcd
*hcd
, struct usb_device
*udev
)
2131 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
2132 struct xhci_virt_device
*virt_dev
;
2133 unsigned long flags
;
2137 if (udev
->slot_id
== 0)
2139 virt_dev
= xhci
->devs
[udev
->slot_id
];
2143 /* Stop any wayward timer functions (which may grab the lock) */
2144 for (i
= 0; i
< 31; ++i
) {
2145 virt_dev
->eps
[i
].ep_state
&= ~EP_HALT_PENDING
;
2146 del_timer_sync(&virt_dev
->eps
[i
].stop_cmd_timer
);
2149 spin_lock_irqsave(&xhci
->lock
, flags
);
2150 /* Don't disable the slot if the host controller is dead. */
2151 state
= xhci_readl(xhci
, &xhci
->op_regs
->status
);
2152 if (state
== 0xffffffff || (xhci
->xhc_state
& XHCI_STATE_DYING
)) {
2153 xhci_free_virt_device(xhci
, udev
->slot_id
);
2154 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2158 if (xhci_queue_slot_control(xhci
, TRB_DISABLE_SLOT
, udev
->slot_id
)) {
2159 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2160 xhci_dbg(xhci
, "FIXME: allocate a command ring segment\n");
2163 xhci_ring_cmd_db(xhci
);
2164 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2168 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
2169 * timed out, or allocating memory failed. Returns 1 on success.
2171 int xhci_alloc_dev(struct usb_hcd
*hcd
, struct usb_device
*udev
)
2173 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
2174 unsigned long flags
;
2178 spin_lock_irqsave(&xhci
->lock
, flags
);
2179 ret
= xhci_queue_slot_control(xhci
, TRB_ENABLE_SLOT
, 0);
2181 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2182 xhci_dbg(xhci
, "FIXME: allocate a command ring segment\n");
2185 xhci_ring_cmd_db(xhci
);
2186 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2188 timeleft
= wait_for_completion_interruptible_timeout(&xhci
->addr_dev
,
2189 USB_CTRL_SET_TIMEOUT
);
2190 if (timeleft
<= 0) {
2191 xhci_warn(xhci
, "%s while waiting for a slot\n",
2192 timeleft
== 0 ? "Timeout" : "Signal");
2196 if (!xhci
->slot_id
) {
2197 xhci_err(xhci
, "Error while assigning device slot ID\n");
2200 /* xhci_alloc_virt_device() does not touch rings; no need to lock.
2201 * Use GFP_NOIO, since this function can be called from
2202 * xhci_discover_or_reset_device(), which may be called as part of
2203 * mass storage driver error handling.
2205 if (!xhci_alloc_virt_device(xhci
, xhci
->slot_id
, udev
, GFP_NOIO
)) {
2206 /* Disable slot, if we can do it without mem alloc */
2207 xhci_warn(xhci
, "Could not allocate xHCI USB device data structures\n");
2208 spin_lock_irqsave(&xhci
->lock
, flags
);
2209 if (!xhci_queue_slot_control(xhci
, TRB_DISABLE_SLOT
, udev
->slot_id
))
2210 xhci_ring_cmd_db(xhci
);
2211 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2214 udev
->slot_id
= xhci
->slot_id
;
2215 /* Is this a LS or FS device under a HS hub? */
2216 /* Hub or peripherial? */
2221 * Issue an Address Device command (which will issue a SetAddress request to
2223 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
2224 * we should only issue and wait on one address command at the same time.
2226 * We add one to the device address issued by the hardware because the USB core
2227 * uses address 1 for the root hubs (even though they're not really devices).
2229 int xhci_address_device(struct usb_hcd
*hcd
, struct usb_device
*udev
)
2231 unsigned long flags
;
2233 struct xhci_virt_device
*virt_dev
;
2235 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
2236 struct xhci_slot_ctx
*slot_ctx
;
2237 struct xhci_input_control_ctx
*ctrl_ctx
;
2240 if (!udev
->slot_id
) {
2241 xhci_dbg(xhci
, "Bad Slot ID %d\n", udev
->slot_id
);
2245 virt_dev
= xhci
->devs
[udev
->slot_id
];
2247 /* If this is a Set Address to an unconfigured device, setup ep 0 */
2249 xhci_setup_addressable_virt_dev(xhci
, udev
);
2251 xhci_copy_ep0_dequeue_into_input_ctx(xhci
, udev
);
2252 /* Otherwise, assume the core has the device configured how it wants */
2253 xhci_dbg(xhci
, "Slot ID %d Input Context:\n", udev
->slot_id
);
2254 xhci_dbg_ctx(xhci
, virt_dev
->in_ctx
, 2);
2256 spin_lock_irqsave(&xhci
->lock
, flags
);
2257 ret
= xhci_queue_address_device(xhci
, virt_dev
->in_ctx
->dma
,
2260 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2261 xhci_dbg(xhci
, "FIXME: allocate a command ring segment\n");
2264 xhci_ring_cmd_db(xhci
);
2265 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2267 timeleft
= wait_for_completion_interruptible_timeout(&xhci
->addr_dev
,
2268 USB_CTRL_SET_TIMEOUT
);
2269 if (timeleft
<= 0) {
2270 xhci_warn(xhci
, "%s while waiting for a slot\n",
2271 timeleft
== 0 ? "Timeout" : "Signal");
2275 switch (virt_dev
->cmd_status
) {
2276 case COMP_CTX_STATE
:
2278 xhci_err(xhci
, "Setup ERROR: address device command for slot %d.\n",
2283 dev_warn(&udev
->dev
, "Device not responding to set address.\n");
2287 xhci_dbg(xhci
, "Successful Address Device command\n");
2290 xhci_err(xhci
, "ERROR: unexpected command completion "
2291 "code 0x%x.\n", virt_dev
->cmd_status
);
2292 xhci_dbg(xhci
, "Slot ID %d Output Context:\n", udev
->slot_id
);
2293 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 2);
2300 temp_64
= xhci_read_64(xhci
, &xhci
->op_regs
->dcbaa_ptr
);
2301 xhci_dbg(xhci
, "Op regs DCBAA ptr = %#016llx\n", temp_64
);
2302 xhci_dbg(xhci
, "Slot ID %d dcbaa entry @%p = %#016llx\n",
2304 &xhci
->dcbaa
->dev_context_ptrs
[udev
->slot_id
],
2305 (unsigned long long)
2306 xhci
->dcbaa
->dev_context_ptrs
[udev
->slot_id
]);
2307 xhci_dbg(xhci
, "Output Context DMA address = %#08llx\n",
2308 (unsigned long long)virt_dev
->out_ctx
->dma
);
2309 xhci_dbg(xhci
, "Slot ID %d Input Context:\n", udev
->slot_id
);
2310 xhci_dbg_ctx(xhci
, virt_dev
->in_ctx
, 2);
2311 xhci_dbg(xhci
, "Slot ID %d Output Context:\n", udev
->slot_id
);
2312 xhci_dbg_ctx(xhci
, virt_dev
->out_ctx
, 2);
2314 * USB core uses address 1 for the roothubs, so we add one to the
2315 * address given back to us by the HC.
2317 slot_ctx
= xhci_get_slot_ctx(xhci
, virt_dev
->out_ctx
);
2318 udev
->devnum
= (slot_ctx
->dev_state
& DEV_ADDR_MASK
) + 1;
2319 /* Zero the input context control for later use */
2320 ctrl_ctx
= xhci_get_input_control_ctx(xhci
, virt_dev
->in_ctx
);
2321 ctrl_ctx
->add_flags
= 0;
2322 ctrl_ctx
->drop_flags
= 0;
2324 xhci_dbg(xhci
, "Device address = %d\n", udev
->devnum
);
2325 set_bit(udev
->devnum
, udev
->bus
->devmap
.devicemap
);
2330 /* Once a hub descriptor is fetched for a device, we need to update the xHC's
2331 * internal data structures for the device.
2333 int xhci_update_hub_device(struct usb_hcd
*hcd
, struct usb_device
*hdev
,
2334 struct usb_tt
*tt
, gfp_t mem_flags
)
2336 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
2337 struct xhci_virt_device
*vdev
;
2338 struct xhci_command
*config_cmd
;
2339 struct xhci_input_control_ctx
*ctrl_ctx
;
2340 struct xhci_slot_ctx
*slot_ctx
;
2341 unsigned long flags
;
2342 unsigned think_time
;
2345 /* Ignore root hubs */
2349 vdev
= xhci
->devs
[hdev
->slot_id
];
2351 xhci_warn(xhci
, "Cannot update hub desc for unknown device.\n");
2354 config_cmd
= xhci_alloc_command(xhci
, true, true, mem_flags
);
2356 xhci_dbg(xhci
, "Could not allocate xHCI command structure.\n");
2360 spin_lock_irqsave(&xhci
->lock
, flags
);
2361 xhci_slot_copy(xhci
, config_cmd
->in_ctx
, vdev
->out_ctx
);
2362 ctrl_ctx
= xhci_get_input_control_ctx(xhci
, config_cmd
->in_ctx
);
2363 ctrl_ctx
->add_flags
|= SLOT_FLAG
;
2364 slot_ctx
= xhci_get_slot_ctx(xhci
, config_cmd
->in_ctx
);
2365 slot_ctx
->dev_info
|= DEV_HUB
;
2367 slot_ctx
->dev_info
|= DEV_MTT
;
2368 if (xhci
->hci_version
> 0x95) {
2369 xhci_dbg(xhci
, "xHCI version %x needs hub "
2370 "TT think time and number of ports\n",
2371 (unsigned int) xhci
->hci_version
);
2372 slot_ctx
->dev_info2
|= XHCI_MAX_PORTS(hdev
->maxchild
);
2373 /* Set TT think time - convert from ns to FS bit times.
2374 * 0 = 8 FS bit times, 1 = 16 FS bit times,
2375 * 2 = 24 FS bit times, 3 = 32 FS bit times.
2377 think_time
= tt
->think_time
;
2378 if (think_time
!= 0)
2379 think_time
= (think_time
/ 666) - 1;
2380 slot_ctx
->tt_info
|= TT_THINK_TIME(think_time
);
2382 xhci_dbg(xhci
, "xHCI version %x doesn't need hub "
2383 "TT think time or number of ports\n",
2384 (unsigned int) xhci
->hci_version
);
2386 slot_ctx
->dev_state
= 0;
2387 spin_unlock_irqrestore(&xhci
->lock
, flags
);
2389 xhci_dbg(xhci
, "Set up %s for hub device.\n",
2390 (xhci
->hci_version
> 0x95) ?
2391 "configure endpoint" : "evaluate context");
2392 xhci_dbg(xhci
, "Slot %u Input Context:\n", hdev
->slot_id
);
2393 xhci_dbg_ctx(xhci
, config_cmd
->in_ctx
, 0);
2395 /* Issue and wait for the configure endpoint or
2396 * evaluate context command.
2398 if (xhci
->hci_version
> 0x95)
2399 ret
= xhci_configure_endpoint(xhci
, hdev
, config_cmd
,
2402 ret
= xhci_configure_endpoint(xhci
, hdev
, config_cmd
,
2405 xhci_dbg(xhci
, "Slot %u Output Context:\n", hdev
->slot_id
);
2406 xhci_dbg_ctx(xhci
, vdev
->out_ctx
, 0);
2408 xhci_free_command(xhci
, config_cmd
);
2412 int xhci_get_frame(struct usb_hcd
*hcd
)
2414 struct xhci_hcd
*xhci
= hcd_to_xhci(hcd
);
2415 /* EHCI mods by the periodic size. Why? */
2416 return xhci_readl(xhci
, &xhci
->run_regs
->microframe_index
) >> 3;
2419 MODULE_DESCRIPTION(DRIVER_DESC
);
2420 MODULE_AUTHOR(DRIVER_AUTHOR
);
2421 MODULE_LICENSE("GPL");
2423 static int __init
xhci_hcd_init(void)
2428 retval
= xhci_register_pci();
2431 printk(KERN_DEBUG
"Problem registering PCI driver.");
2436 * Check the compiler generated sizes of structures that must be laid
2437 * out in specific ways for hardware access.
2439 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array
) != 256*32/8);
2440 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx
) != 8*32/8);
2441 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx
) != 8*32/8);
2442 /* xhci_device_control has eight fields, and also
2443 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
2445 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx
) != 4*32/8);
2446 BUILD_BUG_ON(sizeof(union xhci_trb
) != 4*32/8);
2447 BUILD_BUG_ON(sizeof(struct xhci_erst_entry
) != 4*32/8);
2448 BUILD_BUG_ON(sizeof(struct xhci_cap_regs
) != 7*32/8);
2449 BUILD_BUG_ON(sizeof(struct xhci_intr_reg
) != 8*32/8);
2450 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
2451 BUILD_BUG_ON(sizeof(struct xhci_run_regs
) != (8+8*128)*32/8);
2452 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array
) != 256*32/8);
2455 module_init(xhci_hcd_init
);
2457 static void __exit
xhci_hcd_cleanup(void)
2460 xhci_unregister_pci();
2463 module_exit(xhci_hcd_cleanup
);