Lines Matching +full:generic +full:- +full:xhci

1 // SPDX-License-Identifier: GPL-2.0
3 * xHCI host controller driver
19 #include <linux/dma-mapping.h>
21 #include "xhci.h"
22 #include "xhci-trace.h"
23 #include "xhci-debugfs.h"
24 #include "xhci-dbgcap.h"
42 struct xhci_segment *seg = ring->first_seg; in td_on_ring()
44 if (!td || !td->start_seg) in td_on_ring()
47 if (seg == td->start_seg) in td_on_ring()
49 seg = seg->next; in td_on_ring()
50 } while (seg && seg != ring->first_seg); in td_on_ring()
56 * xhci_handshake - spin reading hc until handshake completes or fails
66 * hardware flakeout), or the register reads as all-ones (hardware removed).
78 return -ENODEV; in xhci_handshake()
84 * Disable interrupts and begin the xHCI halting process.
86 void xhci_quiesce(struct xhci_hcd *xhci) in xhci_quiesce() argument
93 halted = readl(&xhci->op_regs->status) & STS_HALT; in xhci_quiesce()
97 cmd = readl(&xhci->op_regs->command); in xhci_quiesce()
99 writel(cmd, &xhci->op_regs->command); in xhci_quiesce()
110 int xhci_halt(struct xhci_hcd *xhci) in xhci_halt() argument
113 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC"); in xhci_halt()
114 xhci_quiesce(xhci); in xhci_halt()
116 ret = xhci_handshake(&xhci->op_regs->status, in xhci_halt()
119 xhci_warn(xhci, "Host halt failed, %d\n", ret); in xhci_halt()
122 xhci->xhc_state |= XHCI_STATE_HALTED; in xhci_halt()
123 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; in xhci_halt()
130 int xhci_start(struct xhci_hcd *xhci) in xhci_start() argument
135 temp = readl(&xhci->op_regs->command); in xhci_start()
137 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.", in xhci_start()
139 writel(temp, &xhci->op_regs->command); in xhci_start()
145 ret = xhci_handshake(&xhci->op_regs->status, in xhci_start()
147 if (ret == -ETIMEDOUT) in xhci_start()
148 xhci_err(xhci, "Host took too long to start, " in xhci_start()
153 xhci->xhc_state = 0; in xhci_start()
165 int xhci_reset(struct xhci_hcd *xhci) in xhci_reset() argument
171 state = readl(&xhci->op_regs->status); in xhci_reset()
174 xhci_warn(xhci, "Host not accessible, reset failed.\n"); in xhci_reset()
175 return -ENODEV; in xhci_reset()
179 xhci_warn(xhci, "Host controller not halted, aborting reset.\n"); in xhci_reset()
183 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC"); in xhci_reset()
184 command = readl(&xhci->op_regs->command); in xhci_reset()
186 writel(command, &xhci->op_regs->command); in xhci_reset()
188 /* Existing Intel xHCI controllers require a delay of 1 mS, in xhci_reset()
195 if (xhci->quirks & XHCI_INTEL_HOST) in xhci_reset()
198 ret = xhci_handshake(&xhci->op_regs->command, in xhci_reset()
203 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL) in xhci_reset()
204 usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci)->self.controller)); in xhci_reset()
206 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_reset()
209 * xHCI cannot write to any doorbells or operational registers other in xhci_reset()
212 ret = xhci_handshake(&xhci->op_regs->status, in xhci_reset()
215 xhci->usb2_rhub.bus_state.port_c_suspend = 0; in xhci_reset()
216 xhci->usb2_rhub.bus_state.suspended_ports = 0; in xhci_reset()
217 xhci->usb2_rhub.bus_state.resuming_ports = 0; in xhci_reset()
218 xhci->usb3_rhub.bus_state.port_c_suspend = 0; in xhci_reset()
219 xhci->usb3_rhub.bus_state.suspended_ports = 0; in xhci_reset()
220 xhci->usb3_rhub.bus_state.resuming_ports = 0; in xhci_reset()
225 static void xhci_zero_64b_regs(struct xhci_hcd *xhci) in xhci_zero_64b_regs() argument
227 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_zero_64b_regs()
246 if (!(xhci->quirks & XHCI_ZERO_64B_REGS) || !device_iommu_mapped(dev)) in xhci_zero_64b_regs()
249 xhci_info(xhci, "Zeroing 64bit base registers, expecting fault\n"); in xhci_zero_64b_regs()
252 val = readl(&xhci->op_regs->command); in xhci_zero_64b_regs()
254 writel(val, &xhci->op_regs->command); in xhci_zero_64b_regs()
257 val = readl(&xhci->op_regs->status); in xhci_zero_64b_regs()
259 writel(val, &xhci->op_regs->status); in xhci_zero_64b_regs()
262 val = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); in xhci_zero_64b_regs()
264 xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr); in xhci_zero_64b_regs()
265 val = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); in xhci_zero_64b_regs()
267 xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring); in xhci_zero_64b_regs()
269 intrs = min_t(u32, HCS_MAX_INTRS(xhci->hcs_params1), in xhci_zero_64b_regs()
270 ARRAY_SIZE(xhci->run_regs->ir_set)); in xhci_zero_64b_regs()
275 ir = &xhci->run_regs->ir_set[i]; in xhci_zero_64b_regs()
276 val = xhci_read_64(xhci, &ir->erst_base); in xhci_zero_64b_regs()
278 xhci_write_64(xhci, 0, &ir->erst_base); in xhci_zero_64b_regs()
279 val= xhci_read_64(xhci, &ir->erst_dequeue); in xhci_zero_64b_regs()
281 xhci_write_64(xhci, 0, &ir->erst_dequeue); in xhci_zero_64b_regs()
285 err = xhci_handshake(&xhci->op_regs->status, in xhci_zero_64b_regs()
289 xhci_info(xhci, "Fault detected\n"); in xhci_zero_64b_regs()
296 static int xhci_setup_msi(struct xhci_hcd *xhci) in xhci_setup_msi() argument
302 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); in xhci_setup_msi()
306 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_setup_msi()
311 ret = request_irq(pdev->irq, xhci_msi_irq, in xhci_setup_msi()
312 0, "xhci_hcd", xhci_to_hcd(xhci)); in xhci_setup_msi()
314 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_setup_msi()
323 * Set up MSI-X
325 static int xhci_setup_msix(struct xhci_hcd *xhci) in xhci_setup_msix() argument
328 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_setup_msix()
329 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); in xhci_setup_msix()
332 * calculate number of msi-x vectors supported. in xhci_setup_msix()
333 * - HCS_MAX_INTRS: the max number of interrupts the host can handle, in xhci_setup_msix()
334 * with max number of interrupters based on the xhci HCSPARAMS1. in xhci_setup_msix()
335 * - num_online_cpus: maximum msi-x vectors per CPUs core. in xhci_setup_msix()
338 xhci->msix_count = min(num_online_cpus() + 1, in xhci_setup_msix()
339 HCS_MAX_INTRS(xhci->hcs_params1)); in xhci_setup_msix()
341 ret = pci_alloc_irq_vectors(pdev, xhci->msix_count, xhci->msix_count, in xhci_setup_msix()
344 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_setup_msix()
345 "Failed to enable MSI-X"); in xhci_setup_msix()
349 for (i = 0; i < xhci->msix_count; i++) { in xhci_setup_msix()
351 "xhci_hcd", xhci_to_hcd(xhci)); in xhci_setup_msix()
356 hcd->msix_enabled = 1; in xhci_setup_msix()
360 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt"); in xhci_setup_msix()
361 while (--i >= 0) in xhci_setup_msix()
362 free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci)); in xhci_setup_msix()
367 /* Free any IRQs and disable MSI-X */
368 static void xhci_cleanup_msix(struct xhci_hcd *xhci) in xhci_cleanup_msix() argument
370 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_cleanup_msix()
371 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); in xhci_cleanup_msix()
373 if (xhci->quirks & XHCI_PLAT) in xhci_cleanup_msix()
377 if (hcd->irq > 0) in xhci_cleanup_msix()
380 if (hcd->msix_enabled) { in xhci_cleanup_msix()
383 for (i = 0; i < xhci->msix_count; i++) in xhci_cleanup_msix()
384 free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci)); in xhci_cleanup_msix()
386 free_irq(pci_irq_vector(pdev, 0), xhci_to_hcd(xhci)); in xhci_cleanup_msix()
390 hcd->msix_enabled = 0; in xhci_cleanup_msix()
393 static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci) in xhci_msix_sync_irqs() argument
395 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_msix_sync_irqs()
397 if (hcd->msix_enabled) { in xhci_msix_sync_irqs()
398 struct pci_dev *pdev = to_pci_dev(hcd->self.controller); in xhci_msix_sync_irqs()
401 for (i = 0; i < xhci->msix_count; i++) in xhci_msix_sync_irqs()
408 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_try_enable_msi() local
412 /* The xhci platform device has set up IRQs through usb_add_hcd. */ in xhci_try_enable_msi()
413 if (xhci->quirks & XHCI_PLAT) in xhci_try_enable_msi()
416 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); in xhci_try_enable_msi()
421 if (xhci->quirks & XHCI_BROKEN_MSI) in xhci_try_enable_msi()
425 if (hcd->irq) in xhci_try_enable_msi()
426 free_irq(hcd->irq, hcd); in xhci_try_enable_msi()
427 hcd->irq = 0; in xhci_try_enable_msi()
429 ret = xhci_setup_msix(xhci); in xhci_try_enable_msi()
432 ret = xhci_setup_msi(xhci); in xhci_try_enable_msi()
435 hcd->msi_enabled = 1; in xhci_try_enable_msi()
439 if (!pdev->irq) { in xhci_try_enable_msi()
440 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n"); in xhci_try_enable_msi()
441 return -EINVAL; in xhci_try_enable_msi()
445 if (!strlen(hcd->irq_descr)) in xhci_try_enable_msi()
446 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d", in xhci_try_enable_msi()
447 hcd->driver->description, hcd->self.busnum); in xhci_try_enable_msi()
450 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED, in xhci_try_enable_msi()
451 hcd->irq_descr, hcd); in xhci_try_enable_msi()
453 xhci_err(xhci, "request interrupt %d failed\n", in xhci_try_enable_msi()
454 pdev->irq); in xhci_try_enable_msi()
457 hcd->irq = pdev->irq; in xhci_try_enable_msi()
468 static inline void xhci_cleanup_msix(struct xhci_hcd *xhci) in xhci_cleanup_msix() argument
472 static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci) in xhci_msix_sync_irqs() argument
480 struct xhci_hcd *xhci; in compliance_mode_recovery() local
486 xhci = from_timer(xhci, t, comp_mode_recovery_timer); in compliance_mode_recovery()
487 rhub = &xhci->usb3_rhub; in compliance_mode_recovery()
489 for (i = 0; i < rhub->num_ports; i++) { in compliance_mode_recovery()
490 temp = readl(rhub->ports[i]->addr); in compliance_mode_recovery()
496 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in compliance_mode_recovery()
497 "Compliance mode detected->port %d", in compliance_mode_recovery()
499 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in compliance_mode_recovery()
501 hcd = xhci->shared_hcd; in compliance_mode_recovery()
503 if (hcd->state == HC_STATE_SUSPENDED) in compliance_mode_recovery()
510 if (xhci->port_status_u0 != ((1 << rhub->num_ports) - 1)) in compliance_mode_recovery()
511 mod_timer(&xhci->comp_mode_recovery_timer, in compliance_mode_recovery()
516 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
522 * status event is generated when entering compliance mode (per xhci spec),
525 static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci) in compliance_mode_recovery_timer_init() argument
527 xhci->port_status_u0 = 0; in compliance_mode_recovery_timer_init()
528 timer_setup(&xhci->comp_mode_recovery_timer, compliance_mode_recovery, in compliance_mode_recovery_timer_init()
530 xhci->comp_mode_recovery_timer.expires = jiffies + in compliance_mode_recovery_timer_init()
533 add_timer(&xhci->comp_mode_recovery_timer); in compliance_mode_recovery_timer_init()
534 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in compliance_mode_recovery_timer_init()
540 * USB3.0 re-driver and that need the Compliance Mode Quirk.
542 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
553 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard"))) in xhci_compliance_mode_recovery_timer_quirk_check()
565 static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci) in xhci_all_ports_seen_u0() argument
567 return (xhci->port_status_u0 == ((1 << xhci->usb3_rhub.num_ports) - 1)); in xhci_all_ports_seen_u0()
572 * Initialize memory for HCD and xHC (one-time init).
580 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_init() local
583 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init"); in xhci_init()
584 spin_lock_init(&xhci->lock); in xhci_init()
585 if (xhci->hci_version == 0x95 && link_quirk) { in xhci_init()
586 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_init()
588 xhci->quirks |= XHCI_LINK_TRB_QUIRK; in xhci_init()
590 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_init()
591 "xHCI doesn't need link TRB QUIRK"); in xhci_init()
593 retval = xhci_mem_init(xhci, GFP_KERNEL); in xhci_init()
594 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init"); in xhci_init()
598 xhci->quirks |= XHCI_COMP_MODE_QUIRK; in xhci_init()
599 compliance_mode_recovery_timer_init(xhci); in xhci_init()
605 /*-------------------------------------------------------------------------*/
608 static int xhci_run_finished(struct xhci_hcd *xhci) in xhci_run_finished() argument
610 if (xhci_start(xhci)) { in xhci_run_finished()
611 xhci_halt(xhci); in xhci_run_finished()
612 return -ENODEV; in xhci_run_finished()
614 xhci->shared_hcd->state = HC_STATE_RUNNING; in xhci_run_finished()
615 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING; in xhci_run_finished()
617 if (xhci->quirks & XHCI_NEC_HOST) in xhci_run_finished()
618 xhci_ring_cmd_db(xhci); in xhci_run_finished()
620 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run_finished()
635 * Setup MSI-X vectors and enable interrupts.
642 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_run() local
644 /* Start the xHCI host controller running only after the USB 2.0 roothub in xhci_run()
648 hcd->uses_new_polling = 1; in xhci_run()
650 return xhci_run_finished(xhci); in xhci_run()
652 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run"); in xhci_run()
658 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); in xhci_run()
660 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run()
663 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run()
665 temp = readl(&xhci->ir_set->irq_control); in xhci_run()
667 temp |= (xhci->imod_interval / 250) & ER_IRQ_INTERVAL_MASK; in xhci_run()
668 writel(temp, &xhci->ir_set->irq_control); in xhci_run()
671 temp = readl(&xhci->op_regs->command); in xhci_run()
673 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run()
675 writel(temp, &xhci->op_regs->command); in xhci_run()
677 temp = readl(&xhci->ir_set->irq_pending); in xhci_run()
678 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run()
680 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp)); in xhci_run()
681 writel(ER_IRQ_ENABLE(temp), &xhci->ir_set->irq_pending); in xhci_run()
683 if (xhci->quirks & XHCI_NEC_HOST) { in xhci_run()
686 command = xhci_alloc_command(xhci, false, GFP_KERNEL); in xhci_run()
688 return -ENOMEM; in xhci_run()
690 ret = xhci_queue_vendor_command(xhci, command, 0, 0, 0, in xhci_run()
693 xhci_free_command(xhci, command); in xhci_run()
695 set_bit(HCD_FLAG_DEFER_RH_REGISTER, &hcd->flags); in xhci_run()
696 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run()
699 xhci_dbc_init(xhci); in xhci_run()
701 xhci_debugfs_init(xhci); in xhci_run()
708 * Stop xHCI driver.
719 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_stop() local
721 mutex_lock(&xhci->mutex); in xhci_stop()
725 mutex_unlock(&xhci->mutex); in xhci_stop()
729 xhci_dbc_exit(xhci); in xhci_stop()
731 spin_lock_irq(&xhci->lock); in xhci_stop()
732 xhci->xhc_state |= XHCI_STATE_HALTED; in xhci_stop()
733 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; in xhci_stop()
734 xhci_halt(xhci); in xhci_stop()
735 xhci_reset(xhci); in xhci_stop()
736 spin_unlock_irq(&xhci->lock); in xhci_stop()
738 xhci_cleanup_msix(xhci); in xhci_stop()
741 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && in xhci_stop()
742 (!(xhci_all_ports_seen_u0(xhci)))) { in xhci_stop()
743 del_timer_sync(&xhci->comp_mode_recovery_timer); in xhci_stop()
744 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_stop()
749 if (xhci->quirks & XHCI_AMD_PLL_FIX) in xhci_stop()
752 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_stop()
754 temp = readl(&xhci->op_regs->status); in xhci_stop()
755 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status); in xhci_stop()
756 temp = readl(&xhci->ir_set->irq_pending); in xhci_stop()
757 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending); in xhci_stop()
759 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory"); in xhci_stop()
760 xhci_mem_cleanup(xhci); in xhci_stop()
761 xhci_debugfs_exit(xhci); in xhci_stop()
762 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_stop()
763 "xhci_stop completed - status = %x", in xhci_stop()
764 readl(&xhci->op_regs->status)); in xhci_stop()
765 mutex_unlock(&xhci->mutex); in xhci_stop()
769 * Shutdown HC (not bus-specific)
779 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_shutdown() local
781 if (xhci->quirks & XHCI_SPURIOUS_REBOOT) in xhci_shutdown()
782 usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev)); in xhci_shutdown()
784 spin_lock_irq(&xhci->lock); in xhci_shutdown()
785 xhci_halt(xhci); in xhci_shutdown()
787 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP) in xhci_shutdown()
788 xhci_reset(xhci); in xhci_shutdown()
789 spin_unlock_irq(&xhci->lock); in xhci_shutdown()
791 xhci_cleanup_msix(xhci); in xhci_shutdown()
793 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_shutdown()
794 "xhci_shutdown completed - status = %x", in xhci_shutdown()
795 readl(&xhci->op_regs->status)); in xhci_shutdown()
800 static void xhci_save_registers(struct xhci_hcd *xhci) in xhci_save_registers() argument
802 xhci->s3.command = readl(&xhci->op_regs->command); in xhci_save_registers()
803 xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification); in xhci_save_registers()
804 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); in xhci_save_registers()
805 xhci->s3.config_reg = readl(&xhci->op_regs->config_reg); in xhci_save_registers()
806 xhci->s3.erst_size = readl(&xhci->ir_set->erst_size); in xhci_save_registers()
807 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base); in xhci_save_registers()
808 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); in xhci_save_registers()
809 xhci->s3.irq_pending = readl(&xhci->ir_set->irq_pending); in xhci_save_registers()
810 xhci->s3.irq_control = readl(&xhci->ir_set->irq_control); in xhci_save_registers()
813 static void xhci_restore_registers(struct xhci_hcd *xhci) in xhci_restore_registers() argument
815 writel(xhci->s3.command, &xhci->op_regs->command); in xhci_restore_registers()
816 writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification); in xhci_restore_registers()
817 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr); in xhci_restore_registers()
818 writel(xhci->s3.config_reg, &xhci->op_regs->config_reg); in xhci_restore_registers()
819 writel(xhci->s3.erst_size, &xhci->ir_set->erst_size); in xhci_restore_registers()
820 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base); in xhci_restore_registers()
821 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue); in xhci_restore_registers()
822 writel(xhci->s3.irq_pending, &xhci->ir_set->irq_pending); in xhci_restore_registers()
823 writel(xhci->s3.irq_control, &xhci->ir_set->irq_control); in xhci_restore_registers()
826 static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci) in xhci_set_cmd_ring_deq() argument
831 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); in xhci_set_cmd_ring_deq()
833 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, in xhci_set_cmd_ring_deq()
834 xhci->cmd_ring->dequeue) & in xhci_set_cmd_ring_deq()
836 xhci->cmd_ring->cycle_state; in xhci_set_cmd_ring_deq()
837 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_set_cmd_ring_deq()
840 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring); in xhci_set_cmd_ring_deq()
847 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
850 * middle of the ring (TRBs are 16-byte aligned).
852 static void xhci_clear_command_ring(struct xhci_hcd *xhci) in xhci_clear_command_ring() argument
857 ring = xhci->cmd_ring; in xhci_clear_command_ring()
858 seg = ring->deq_seg; in xhci_clear_command_ring()
860 memset(seg->trbs, 0, in xhci_clear_command_ring()
861 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1)); in xhci_clear_command_ring()
862 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &= in xhci_clear_command_ring()
864 seg = seg->next; in xhci_clear_command_ring()
865 } while (seg != ring->deq_seg); in xhci_clear_command_ring()
868 ring->deq_seg = ring->first_seg; in xhci_clear_command_ring()
869 ring->dequeue = ring->first_seg->trbs; in xhci_clear_command_ring()
870 ring->enq_seg = ring->deq_seg; in xhci_clear_command_ring()
871 ring->enqueue = ring->dequeue; in xhci_clear_command_ring()
873 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1; in xhci_clear_command_ring()
878 ring->cycle_state = 1; in xhci_clear_command_ring()
882 * Yes, this will need to be re-written after resume, but we're paranoid in xhci_clear_command_ring()
887 xhci_set_cmd_ring_deq(xhci); in xhci_clear_command_ring()
895 * Internal wake causes immediate xHCI wake after suspend. PORT_CSC write done
899 static void xhci_disable_hub_port_wake(struct xhci_hcd *xhci, in xhci_disable_hub_port_wake() argument
907 spin_lock_irqsave(&xhci->lock, flags); in xhci_disable_hub_port_wake()
909 for (i = 0; i < rhub->num_ports; i++) { in xhci_disable_hub_port_wake()
910 portsc = readl(rhub->ports[i]->addr); in xhci_disable_hub_port_wake()
923 writel(t2, rhub->ports[i]->addr); in xhci_disable_hub_port_wake()
924 xhci_dbg(xhci, "config port %d-%d wake bits, portsc: 0x%x, write: 0x%x\n", in xhci_disable_hub_port_wake()
925 rhub->hcd->self.busnum, i + 1, portsc, t2); in xhci_disable_hub_port_wake()
928 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_hub_port_wake()
931 static bool xhci_pending_portevent(struct xhci_hcd *xhci) in xhci_pending_portevent() argument
938 status = readl(&xhci->op_regs->status); in xhci_pending_portevent()
944 * being written to the Event Ring. See note in xhci 1.1 section 4.19.2. in xhci_pending_portevent()
947 port_index = xhci->usb2_rhub.num_ports; in xhci_pending_portevent()
948 ports = xhci->usb2_rhub.ports; in xhci_pending_portevent()
949 while (port_index--) { in xhci_pending_portevent()
950 portsc = readl(ports[port_index]->addr); in xhci_pending_portevent()
955 port_index = xhci->usb3_rhub.num_ports; in xhci_pending_portevent()
956 ports = xhci->usb3_rhub.ports; in xhci_pending_portevent()
957 while (port_index--) { in xhci_pending_portevent()
958 portsc = readl(ports[port_index]->addr); in xhci_pending_portevent()
967 * Stop HC (not bus-specific)
972 int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup) in xhci_suspend() argument
976 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_suspend()
980 if (!hcd->state) in xhci_suspend()
983 if (hcd->state != HC_STATE_SUSPENDED || in xhci_suspend()
984 xhci->shared_hcd->state != HC_STATE_SUSPENDED) in xhci_suspend()
985 return -EINVAL; in xhci_suspend()
988 xhci_disable_hub_port_wake(xhci, &xhci->usb3_rhub, do_wakeup); in xhci_suspend()
989 xhci_disable_hub_port_wake(xhci, &xhci->usb2_rhub, do_wakeup); in xhci_suspend()
994 xhci_dbc_suspend(xhci); in xhci_suspend()
997 xhci_dbg(xhci, "%s: stopping usb%d port polling.\n", in xhci_suspend()
998 __func__, hcd->self.busnum); in xhci_suspend()
999 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_suspend()
1000 del_timer_sync(&hcd->rh_timer); in xhci_suspend()
1001 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_suspend()
1002 del_timer_sync(&xhci->shared_hcd->rh_timer); in xhci_suspend()
1004 if (xhci->quirks & XHCI_SUSPEND_DELAY) in xhci_suspend()
1007 spin_lock_irq(&xhci->lock); in xhci_suspend()
1008 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); in xhci_suspend()
1009 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags); in xhci_suspend()
1014 command = readl(&xhci->op_regs->command); in xhci_suspend()
1016 writel(command, &xhci->op_regs->command); in xhci_suspend()
1019 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1; in xhci_suspend()
1021 if (xhci_handshake(&xhci->op_regs->status, in xhci_suspend()
1023 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n"); in xhci_suspend()
1024 spin_unlock_irq(&xhci->lock); in xhci_suspend()
1025 return -ETIMEDOUT; in xhci_suspend()
1027 xhci_clear_command_ring(xhci); in xhci_suspend()
1030 xhci_save_registers(xhci); in xhci_suspend()
1033 command = readl(&xhci->op_regs->command); in xhci_suspend()
1035 writel(command, &xhci->op_regs->command); in xhci_suspend()
1036 xhci->broken_suspend = 0; in xhci_suspend()
1037 if (xhci_handshake(&xhci->op_regs->status, in xhci_suspend()
1045 * if SRE and HCE bits are not set (as per xhci in xhci_suspend()
1048 res = readl(&xhci->op_regs->status); in xhci_suspend()
1049 if ((xhci->quirks & XHCI_SNPS_BROKEN_SUSPEND) && in xhci_suspend()
1052 xhci->broken_suspend = 1; in xhci_suspend()
1054 xhci_warn(xhci, "WARN: xHC save state timeout\n"); in xhci_suspend()
1055 spin_unlock_irq(&xhci->lock); in xhci_suspend()
1056 return -ETIMEDOUT; in xhci_suspend()
1059 spin_unlock_irq(&xhci->lock); in xhci_suspend()
1062 * Deleting Compliance Mode Recovery Timer because the xHCI Host in xhci_suspend()
1065 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && in xhci_suspend()
1066 (!(xhci_all_ports_seen_u0(xhci)))) { in xhci_suspend()
1067 del_timer_sync(&xhci->comp_mode_recovery_timer); in xhci_suspend()
1068 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_suspend()
1074 /* synchronize irq when using MSI-X */ in xhci_suspend()
1075 xhci_msix_sync_irqs(xhci); in xhci_suspend()
1082 * start xHC (not bus-specific)
1087 int xhci_resume(struct xhci_hcd *xhci, bool hibernated) in xhci_resume() argument
1090 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_resume()
1096 if (!hcd->state) in xhci_resume()
1103 if (time_before(jiffies, xhci->usb2_rhub.bus_state.next_statechange) || in xhci_resume()
1104 time_before(jiffies, xhci->usb3_rhub.bus_state.next_statechange)) in xhci_resume()
1107 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); in xhci_resume()
1108 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags); in xhci_resume()
1110 spin_lock_irq(&xhci->lock); in xhci_resume()
1111 if ((xhci->quirks & XHCI_RESET_ON_RESUME) || xhci->broken_suspend) in xhci_resume()
1119 retval = xhci_handshake(&xhci->op_regs->status, in xhci_resume()
1122 xhci_warn(xhci, "Controller not ready at resume %d\n", in xhci_resume()
1124 spin_unlock_irq(&xhci->lock); in xhci_resume()
1128 xhci_restore_registers(xhci); in xhci_resume()
1130 xhci_set_cmd_ring_deq(xhci); in xhci_resume()
1133 command = readl(&xhci->op_regs->command); in xhci_resume()
1135 writel(command, &xhci->op_regs->command); in xhci_resume()
1138 * restore so setting the timeout to 100ms. Xhci specification in xhci_resume()
1141 if (xhci_handshake(&xhci->op_regs->status, in xhci_resume()
1143 xhci_warn(xhci, "WARN: xHC restore state timeout\n"); in xhci_resume()
1144 spin_unlock_irq(&xhci->lock); in xhci_resume()
1145 return -ETIMEDOUT; in xhci_resume()
1147 temp = readl(&xhci->op_regs->status); in xhci_resume()
1150 /* If restore operation fails, re-initialize the HC during resume */ in xhci_resume()
1153 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && in xhci_resume()
1154 !(xhci_all_ports_seen_u0(xhci))) { in xhci_resume()
1155 del_timer_sync(&xhci->comp_mode_recovery_timer); in xhci_resume()
1156 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_resume()
1161 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub); in xhci_resume()
1162 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub); in xhci_resume()
1164 xhci_dbg(xhci, "Stop HCD\n"); in xhci_resume()
1165 xhci_halt(xhci); in xhci_resume()
1166 xhci_zero_64b_regs(xhci); in xhci_resume()
1167 retval = xhci_reset(xhci); in xhci_resume()
1168 spin_unlock_irq(&xhci->lock); in xhci_resume()
1171 xhci_cleanup_msix(xhci); in xhci_resume()
1173 xhci_dbg(xhci, "// Disabling event ring interrupts\n"); in xhci_resume()
1174 temp = readl(&xhci->op_regs->status); in xhci_resume()
1175 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status); in xhci_resume()
1176 temp = readl(&xhci->ir_set->irq_pending); in xhci_resume()
1177 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending); in xhci_resume()
1179 xhci_dbg(xhci, "cleaning up memory\n"); in xhci_resume()
1180 xhci_mem_cleanup(xhci); in xhci_resume()
1181 xhci_debugfs_exit(xhci); in xhci_resume()
1182 xhci_dbg(xhci, "xhci_stop completed - status = %x\n", in xhci_resume()
1183 readl(&xhci->op_regs->status)); in xhci_resume()
1192 secondary_hcd = xhci->shared_hcd; in xhci_resume()
1194 xhci_dbg(xhci, "Initialize the xhci_hcd\n"); in xhci_resume()
1195 retval = xhci_init(hcd->primary_hcd); in xhci_resume()
1200 xhci_dbg(xhci, "Start the primary HCD\n"); in xhci_resume()
1201 retval = xhci_run(hcd->primary_hcd); in xhci_resume()
1203 xhci_dbg(xhci, "Start the secondary HCD\n"); in xhci_resume()
1206 hcd->state = HC_STATE_SUSPENDED; in xhci_resume()
1207 xhci->shared_hcd->state = HC_STATE_SUSPENDED; in xhci_resume()
1212 command = readl(&xhci->op_regs->command); in xhci_resume()
1214 writel(command, &xhci->op_regs->command); in xhci_resume()
1215 xhci_handshake(&xhci->op_regs->status, STS_HALT, in xhci_resume()
1227 spin_unlock_irq(&xhci->lock); in xhci_resume()
1229 xhci_dbc_resume(xhci); in xhci_resume()
1238 pending_portevent = xhci_pending_portevent(xhci); in xhci_resume()
1241 pending_portevent = xhci_pending_portevent(xhci); in xhci_resume()
1245 usb_hcd_resume_root_hub(xhci->shared_hcd); in xhci_resume()
1251 * be re-initialized Always after a system resume. Ports are subject in xhci_resume()
1255 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running) in xhci_resume()
1256 compliance_mode_recovery_timer_init(xhci); in xhci_resume()
1258 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL) in xhci_resume()
1259 usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller)); in xhci_resume()
1261 /* Re-enable port polling. */ in xhci_resume()
1262 xhci_dbg(xhci, "%s: starting usb%d port polling.\n", in xhci_resume()
1263 __func__, hcd->self.busnum); in xhci_resume()
1264 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_resume()
1265 usb_hcd_poll_rh_status(xhci->shared_hcd); in xhci_resume()
1266 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_resume()
1274 /*-------------------------------------------------------------------------*/
1284 buf_len = urb->transfer_buffer_length; in xhci_map_temp_buffer()
1287 dev_to_node(hcd->self.sysdev)); in xhci_map_temp_buffer()
1290 sg_pcopy_to_buffer(urb->sg, urb->num_sgs, in xhci_map_temp_buffer()
1293 urb->transfer_buffer = temp; in xhci_map_temp_buffer()
1294 urb->transfer_dma = dma_map_single(hcd->self.sysdev, in xhci_map_temp_buffer()
1295 urb->transfer_buffer, in xhci_map_temp_buffer()
1296 urb->transfer_buffer_length, in xhci_map_temp_buffer()
1299 if (dma_mapping_error(hcd->self.sysdev, in xhci_map_temp_buffer()
1300 urb->transfer_dma)) { in xhci_map_temp_buffer()
1301 ret = -EAGAIN; in xhci_map_temp_buffer()
1304 urb->transfer_flags |= URB_DMA_MAP_SINGLE; in xhci_map_temp_buffer()
1321 tail_sg = urb->sg; in xhci_urb_temp_buffer_required()
1322 max_pkt = usb_endpoint_maxp(&urb->ep->desc); in xhci_urb_temp_buffer_required()
1324 if (!urb->num_sgs) in xhci_urb_temp_buffer_required()
1327 if (urb->dev->speed >= USB_SPEED_SUPER) in xhci_urb_temp_buffer_required()
1332 if (urb->transfer_buffer_length != 0 && in xhci_urb_temp_buffer_required()
1333 !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { in xhci_urb_temp_buffer_required()
1334 for_each_sg(urb->sg, sg, urb->num_sgs, i) { in xhci_urb_temp_buffer_required()
1335 len = len + sg->length; in xhci_urb_temp_buffer_required()
1336 if (i > trb_size - 2) { in xhci_urb_temp_buffer_required()
1337 len = len - tail_sg->length; in xhci_urb_temp_buffer_required()
1358 buf_len = urb->transfer_buffer_length; in xhci_unmap_temp_buf()
1361 (urb->transfer_flags & URB_DMA_MAP_SINGLE)) in xhci_unmap_temp_buf()
1362 dma_unmap_single(hcd->self.sysdev, in xhci_unmap_temp_buf()
1363 urb->transfer_dma, in xhci_unmap_temp_buf()
1364 urb->transfer_buffer_length, in xhci_unmap_temp_buf()
1368 len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, in xhci_unmap_temp_buf()
1369 urb->transfer_buffer, in xhci_unmap_temp_buf()
1375 urb->actual_length = len; in xhci_unmap_temp_buf()
1378 urb->transfer_flags &= ~URB_DMA_MAP_SINGLE; in xhci_unmap_temp_buf()
1379 kfree(urb->transfer_buffer); in xhci_unmap_temp_buf()
1380 urb->transfer_buffer = NULL; in xhci_unmap_temp_buf()
1392 struct xhci_hcd *xhci; in xhci_map_urb_for_dma() local
1394 xhci = hcd_to_xhci(hcd); in xhci_map_urb_for_dma()
1399 if (xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) { in xhci_map_urb_for_dma()
1408 struct xhci_hcd *xhci; in xhci_unmap_urb_for_dma() local
1411 xhci = hcd_to_xhci(hcd); in xhci_unmap_urb_for_dma()
1413 if (urb->num_sgs && (urb->transfer_flags & URB_DMA_MAP_SINGLE)) in xhci_unmap_urb_for_dma()
1416 if ((xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) && unmap_temp_buf) in xhci_unmap_urb_for_dma()
1423 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1427 * Index = (epnum * 2) + direction - 1,
1430 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1439 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1; in xhci_get_endpoint_index()
1445 * address from the XHCI endpoint index.
1471 return fls(added_ctxs) - 1; in xhci_last_valid_endpoint()
1475 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1480 struct xhci_hcd *xhci; in xhci_check_args() local
1484 pr_debug("xHCI %s called with invalid args\n", func); in xhci_check_args()
1485 return -EINVAL; in xhci_check_args()
1487 if (!udev->parent) { in xhci_check_args()
1488 pr_debug("xHCI %s called for root hub\n", func); in xhci_check_args()
1492 xhci = hcd_to_xhci(hcd); in xhci_check_args()
1494 if (!udev->slot_id || !xhci->devs[udev->slot_id]) { in xhci_check_args()
1495 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n", in xhci_check_args()
1497 return -EINVAL; in xhci_check_args()
1500 virt_dev = xhci->devs[udev->slot_id]; in xhci_check_args()
1501 if (virt_dev->udev != udev) { in xhci_check_args()
1502 xhci_dbg(xhci, "xHCI %s called with udev and " in xhci_check_args()
1504 return -EINVAL; in xhci_check_args()
1508 if (xhci->xhc_state & XHCI_STATE_HALTED) in xhci_check_args()
1509 return -ENODEV; in xhci_check_args()
1514 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1524 static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id, in xhci_check_maxpacket() argument
1535 out_ctx = xhci->devs[slot_id]->out_ctx; in xhci_check_maxpacket()
1536 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); in xhci_check_maxpacket()
1537 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2)); in xhci_check_maxpacket()
1538 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc); in xhci_check_maxpacket()
1540 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1542 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1545 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1546 "Max packet size in xHCI HW = %d", in xhci_check_maxpacket()
1548 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1552 /* FIXME: This won't work if a non-default control endpoint in xhci_check_maxpacket()
1556 command = xhci_alloc_command(xhci, true, mem_flags); in xhci_check_maxpacket()
1558 return -ENOMEM; in xhci_check_maxpacket()
1560 command->in_ctx = xhci->devs[slot_id]->in_ctx; in xhci_check_maxpacket()
1561 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_check_maxpacket()
1563 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_check_maxpacket()
1565 ret = -ENOMEM; in xhci_check_maxpacket()
1569 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx, in xhci_check_maxpacket()
1570 xhci->devs[slot_id]->out_ctx, ep_index); in xhci_check_maxpacket()
1572 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); in xhci_check_maxpacket()
1573 ep_ctx->ep_info &= cpu_to_le32(~EP_STATE_MASK);/* must clear */ in xhci_check_maxpacket()
1574 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK); in xhci_check_maxpacket()
1575 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size)); in xhci_check_maxpacket()
1577 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG); in xhci_check_maxpacket()
1578 ctrl_ctx->drop_flags = 0; in xhci_check_maxpacket()
1580 ret = xhci_configure_endpoint(xhci, urb->dev, command, in xhci_check_maxpacket()
1586 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG); in xhci_check_maxpacket()
1588 kfree(command->completion); in xhci_check_maxpacket()
1595 * non-error returns are a promise to giveback() the urb later
1600 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_urb_enqueue() local
1608 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, in xhci_urb_enqueue()
1610 return -EINVAL; in xhci_urb_enqueue()
1612 slot_id = urb->dev->slot_id; in xhci_urb_enqueue()
1613 ep_index = xhci_get_endpoint_index(&urb->ep->desc); in xhci_urb_enqueue()
1614 ep_state = &xhci->devs[slot_id]->eps[ep_index].ep_state; in xhci_urb_enqueue()
1617 return -ESHUTDOWN; in xhci_urb_enqueue()
1619 if (xhci->devs[slot_id]->flags & VDEV_PORT_ERROR) { in xhci_urb_enqueue()
1620 xhci_dbg(xhci, "Can't queue urb, port error, link inactive\n"); in xhci_urb_enqueue()
1621 return -ENODEV; in xhci_urb_enqueue()
1624 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) in xhci_urb_enqueue()
1625 num_tds = urb->number_of_packets; in xhci_urb_enqueue()
1626 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) && in xhci_urb_enqueue()
1627 urb->transfer_buffer_length > 0 && in xhci_urb_enqueue()
1628 urb->transfer_flags & URB_ZERO_PACKET && in xhci_urb_enqueue()
1629 !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc))) in xhci_urb_enqueue()
1636 return -ENOMEM; in xhci_urb_enqueue()
1638 urb_priv->num_tds = num_tds; in xhci_urb_enqueue()
1639 urb_priv->num_tds_done = 0; in xhci_urb_enqueue()
1640 urb->hcpriv = urb_priv; in xhci_urb_enqueue()
1644 if (usb_endpoint_xfer_control(&urb->ep->desc)) { in xhci_urb_enqueue()
1648 if (urb->dev->speed == USB_SPEED_FULL) { in xhci_urb_enqueue()
1649 ret = xhci_check_maxpacket(xhci, slot_id, in xhci_urb_enqueue()
1653 urb->hcpriv = NULL; in xhci_urb_enqueue()
1659 spin_lock_irqsave(&xhci->lock, flags); in xhci_urb_enqueue()
1661 if (xhci->xhc_state & XHCI_STATE_DYING) { in xhci_urb_enqueue()
1662 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for non-responsive xHCI host.\n", in xhci_urb_enqueue()
1663 urb->ep->desc.bEndpointAddress, urb); in xhci_urb_enqueue()
1664 ret = -ESHUTDOWN; in xhci_urb_enqueue()
1668 xhci_warn(xhci, "WARN: Can't enqueue URB, ep in streams transition state %x\n", in xhci_urb_enqueue()
1670 ret = -EINVAL; in xhci_urb_enqueue()
1674 xhci_warn(xhci, "Can't enqueue URB while manually clearing toggle\n"); in xhci_urb_enqueue()
1675 ret = -EINVAL; in xhci_urb_enqueue()
1679 switch (usb_endpoint_type(&urb->ep->desc)) { in xhci_urb_enqueue()
1682 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1686 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1690 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1694 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1701 urb->hcpriv = NULL; in xhci_urb_enqueue()
1703 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_urb_enqueue()
1723 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1725 * endpoint command, as noted in the xHCI 0.95 errata.
1743 struct xhci_hcd *xhci; in xhci_urb_dequeue() local
1752 xhci = hcd_to_xhci(hcd); in xhci_urb_dequeue()
1753 spin_lock_irqsave(&xhci->lock, flags); in xhci_urb_dequeue()
1763 vdev = xhci->devs[urb->dev->slot_id]; in xhci_urb_dequeue()
1764 urb_priv = urb->hcpriv; in xhci_urb_dequeue()
1768 ep_index = xhci_get_endpoint_index(&urb->ep->desc); in xhci_urb_dequeue()
1769 ep = &vdev->eps[ep_index]; in xhci_urb_dequeue()
1770 ep_ring = xhci_urb_to_transfer_ring(xhci, urb); in xhci_urb_dequeue()
1775 temp = readl(&xhci->op_regs->status); in xhci_urb_dequeue()
1776 if (temp == ~(u32)0 || xhci->xhc_state & XHCI_STATE_DYING) { in xhci_urb_dequeue()
1777 xhci_hc_died(xhci); in xhci_urb_dequeue()
1782 * check ring is not re-allocated since URB was enqueued. If it is, then in xhci_urb_dequeue()
1786 if (!td_on_ring(&urb_priv->td[0], ep_ring)) { in xhci_urb_dequeue()
1787 xhci_err(xhci, "Canceled URB td not found on endpoint ring"); in xhci_urb_dequeue()
1788 for (i = urb_priv->num_tds_done; i < urb_priv->num_tds; i++) { in xhci_urb_dequeue()
1789 td = &urb_priv->td[i]; in xhci_urb_dequeue()
1790 if (!list_empty(&td->cancelled_td_list)) in xhci_urb_dequeue()
1791 list_del_init(&td->cancelled_td_list); in xhci_urb_dequeue()
1796 if (xhci->xhc_state & XHCI_STATE_HALTED) { in xhci_urb_dequeue()
1797 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_urb_dequeue()
1799 for (i = urb_priv->num_tds_done; in xhci_urb_dequeue()
1800 i < urb_priv->num_tds; in xhci_urb_dequeue()
1802 td = &urb_priv->td[i]; in xhci_urb_dequeue()
1803 if (!list_empty(&td->td_list)) in xhci_urb_dequeue()
1804 list_del_init(&td->td_list); in xhci_urb_dequeue()
1805 if (!list_empty(&td->cancelled_td_list)) in xhci_urb_dequeue()
1806 list_del_init(&td->cancelled_td_list); in xhci_urb_dequeue()
1811 i = urb_priv->num_tds_done; in xhci_urb_dequeue()
1812 if (i < urb_priv->num_tds) in xhci_urb_dequeue()
1813 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_urb_dequeue()
1816 urb, urb->dev->devpath, in xhci_urb_dequeue()
1817 urb->ep->desc.bEndpointAddress, in xhci_urb_dequeue()
1819 urb_priv->td[i].start_seg, in xhci_urb_dequeue()
1820 urb_priv->td[i].first_trb)); in xhci_urb_dequeue()
1822 for (; i < urb_priv->num_tds; i++) { in xhci_urb_dequeue()
1823 td = &urb_priv->td[i]; in xhci_urb_dequeue()
1825 if (list_empty(&td->cancelled_td_list)) { in xhci_urb_dequeue()
1826 td->cancel_status = TD_DIRTY; in xhci_urb_dequeue()
1827 list_add_tail(&td->cancelled_td_list, in xhci_urb_dequeue()
1828 &ep->cancelled_td_list); in xhci_urb_dequeue()
1835 if (!(ep->ep_state & EP_STOP_CMD_PENDING)) { in xhci_urb_dequeue()
1836 command = xhci_alloc_command(xhci, false, GFP_ATOMIC); in xhci_urb_dequeue()
1838 ret = -ENOMEM; in xhci_urb_dequeue()
1841 ep->ep_state |= EP_STOP_CMD_PENDING; in xhci_urb_dequeue()
1842 ep->stop_cmd_timer.expires = jiffies + in xhci_urb_dequeue()
1844 add_timer(&ep->stop_cmd_timer); in xhci_urb_dequeue()
1845 xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id, in xhci_urb_dequeue()
1847 xhci_ring_cmd_db(xhci); in xhci_urb_dequeue()
1850 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_urb_dequeue()
1857 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_urb_dequeue()
1858 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN); in xhci_urb_dequeue()
1873 * the xhci->devs[slot_id] structure.
1878 struct xhci_hcd *xhci; in xhci_drop_endpoint() local
1890 xhci = hcd_to_xhci(hcd); in xhci_drop_endpoint()
1891 if (xhci->xhc_state & XHCI_STATE_DYING) in xhci_drop_endpoint()
1892 return -ENODEV; in xhci_drop_endpoint()
1894 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); in xhci_drop_endpoint()
1895 drop_flag = xhci_get_endpoint_flag(&ep->desc); in xhci_drop_endpoint()
1897 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n", in xhci_drop_endpoint()
1902 in_ctx = xhci->devs[udev->slot_id]->in_ctx; in xhci_drop_endpoint()
1903 out_ctx = xhci->devs[udev->slot_id]->out_ctx; in xhci_drop_endpoint()
1906 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_drop_endpoint()
1911 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_drop_endpoint()
1912 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); in xhci_drop_endpoint()
1917 le32_to_cpu(ctrl_ctx->drop_flags) & in xhci_drop_endpoint()
1918 xhci_get_endpoint_flag(&ep->desc)) { in xhci_drop_endpoint()
1920 if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL) in xhci_drop_endpoint()
1921 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n", in xhci_drop_endpoint()
1926 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag); in xhci_drop_endpoint()
1927 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags); in xhci_drop_endpoint()
1929 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag); in xhci_drop_endpoint()
1930 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags); in xhci_drop_endpoint()
1932 xhci_debugfs_remove_endpoint(xhci, xhci->devs[udev->slot_id], ep_index); in xhci_drop_endpoint()
1934 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep); in xhci_drop_endpoint()
1936 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n", in xhci_drop_endpoint()
1937 (unsigned int) ep->desc.bEndpointAddress, in xhci_drop_endpoint()
1938 udev->slot_id, in xhci_drop_endpoint()
1956 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1961 struct xhci_hcd *xhci; in xhci_add_endpoint() local
1974 ep->hcpriv = NULL; in xhci_add_endpoint()
1977 xhci = hcd_to_xhci(hcd); in xhci_add_endpoint()
1978 if (xhci->xhc_state & XHCI_STATE_DYING) in xhci_add_endpoint()
1979 return -ENODEV; in xhci_add_endpoint()
1981 added_ctxs = xhci_get_endpoint_flag(&ep->desc); in xhci_add_endpoint()
1987 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n", in xhci_add_endpoint()
1992 virt_dev = xhci->devs[udev->slot_id]; in xhci_add_endpoint()
1993 in_ctx = virt_dev->in_ctx; in xhci_add_endpoint()
1996 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_add_endpoint()
2001 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_add_endpoint()
2005 if (virt_dev->eps[ep_index].ring && in xhci_add_endpoint()
2006 !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) { in xhci_add_endpoint()
2007 xhci_warn(xhci, "Trying to add endpoint 0x%x " in xhci_add_endpoint()
2009 (unsigned int) ep->desc.bEndpointAddress); in xhci_add_endpoint()
2010 return -EINVAL; in xhci_add_endpoint()
2016 if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) { in xhci_add_endpoint()
2017 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n", in xhci_add_endpoint()
2027 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) { in xhci_add_endpoint()
2028 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n", in xhci_add_endpoint()
2029 __func__, ep->desc.bEndpointAddress); in xhci_add_endpoint()
2030 return -ENOMEM; in xhci_add_endpoint()
2033 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs); in xhci_add_endpoint()
2034 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags); in xhci_add_endpoint()
2038 * this re-adds a new state for the endpoint from the new endpoint in xhci_add_endpoint()
2039 * descriptors. We must drop and re-add this endpoint, so we leave the in xhci_add_endpoint()
2042 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags); in xhci_add_endpoint()
2045 ep->hcpriv = udev; in xhci_add_endpoint()
2047 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); in xhci_add_endpoint()
2050 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n", in xhci_add_endpoint()
2051 (unsigned int) ep->desc.bEndpointAddress, in xhci_add_endpoint()
2052 udev->slot_id, in xhci_add_endpoint()
2059 static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev) in xhci_zero_in_ctx() argument
2066 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); in xhci_zero_in_ctx()
2068 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_zero_in_ctx()
2078 ctrl_ctx->drop_flags = 0; in xhci_zero_in_ctx()
2079 ctrl_ctx->add_flags = 0; in xhci_zero_in_ctx()
2080 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); in xhci_zero_in_ctx()
2081 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK); in xhci_zero_in_ctx()
2083 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1)); in xhci_zero_in_ctx()
2085 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i); in xhci_zero_in_ctx()
2086 ep_ctx->ep_info = 0; in xhci_zero_in_ctx()
2087 ep_ctx->ep_info2 = 0; in xhci_zero_in_ctx()
2088 ep_ctx->deq = 0; in xhci_zero_in_ctx()
2089 ep_ctx->tx_info = 0; in xhci_zero_in_ctx()
2093 static int xhci_configure_endpoint_result(struct xhci_hcd *xhci, in xhci_configure_endpoint_result() argument
2101 xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n"); in xhci_configure_endpoint_result()
2102 ret = -ETIME; in xhci_configure_endpoint_result()
2105 dev_warn(&udev->dev, in xhci_configure_endpoint_result()
2107 ret = -ENOMEM; in xhci_configure_endpoint_result()
2112 dev_warn(&udev->dev, in xhci_configure_endpoint_result()
2114 ret = -ENOSPC; in xhci_configure_endpoint_result()
2119 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, " in xhci_configure_endpoint_result()
2122 ret = -EINVAL; in xhci_configure_endpoint_result()
2125 dev_warn(&udev->dev, in xhci_configure_endpoint_result()
2127 ret = -ENODEV; in xhci_configure_endpoint_result()
2130 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_configure_endpoint_result()
2135 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n", in xhci_configure_endpoint_result()
2137 ret = -EINVAL; in xhci_configure_endpoint_result()
2143 static int xhci_evaluate_context_result(struct xhci_hcd *xhci, in xhci_evaluate_context_result() argument
2151 xhci_warn(xhci, "Timeout while waiting for evaluate context command\n"); in xhci_evaluate_context_result()
2152 ret = -ETIME; in xhci_evaluate_context_result()
2155 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2156 "WARN: xHCI driver setup invalid evaluate context command.\n"); in xhci_evaluate_context_result()
2157 ret = -EINVAL; in xhci_evaluate_context_result()
2160 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2162 ret = -EINVAL; in xhci_evaluate_context_result()
2165 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2167 ret = -EINVAL; in xhci_evaluate_context_result()
2170 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2172 ret = -ENODEV; in xhci_evaluate_context_result()
2176 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n"); in xhci_evaluate_context_result()
2177 ret = -EINVAL; in xhci_evaluate_context_result()
2180 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_evaluate_context_result()
2185 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n", in xhci_evaluate_context_result()
2187 ret = -EINVAL; in xhci_evaluate_context_result()
2193 static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci, in xhci_count_num_new_endpoints() argument
2203 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2; in xhci_count_num_new_endpoints()
2204 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2; in xhci_count_num_new_endpoints()
2210 return hweight32(valid_add_flags) - in xhci_count_num_new_endpoints()
2214 static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci, in xhci_count_num_dropped_endpoints() argument
2220 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2; in xhci_count_num_dropped_endpoints()
2221 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2; in xhci_count_num_dropped_endpoints()
2223 return hweight32(valid_drop_flags) - in xhci_count_num_dropped_endpoints()
2233 * - the first configure endpoint command drops more endpoints than it adds
2234 * - a second configure endpoint command that adds more endpoints is queued
2235 * - the first configure endpoint command fails, so the config is unchanged
2236 * - the second command may succeed, even though there isn't enough resources
2238 * Must be called with xhci->lock held.
2240 static int xhci_reserve_host_resources(struct xhci_hcd *xhci, in xhci_reserve_host_resources() argument
2245 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx); in xhci_reserve_host_resources()
2246 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) { in xhci_reserve_host_resources()
2247 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_resources()
2250 xhci->num_active_eps, added_eps, in xhci_reserve_host_resources()
2251 xhci->limit_active_eps); in xhci_reserve_host_resources()
2252 return -ENOMEM; in xhci_reserve_host_resources()
2254 xhci->num_active_eps += added_eps; in xhci_reserve_host_resources()
2255 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_resources()
2257 xhci->num_active_eps); in xhci_reserve_host_resources()
2265 * Must be called with xhci->lock held.
2267 static void xhci_free_host_resources(struct xhci_hcd *xhci, in xhci_free_host_resources() argument
2272 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx); in xhci_free_host_resources()
2273 xhci->num_active_eps -= num_failed_eps; in xhci_free_host_resources()
2274 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_free_host_resources()
2277 xhci->num_active_eps); in xhci_free_host_resources()
2284 * Must be called with xhci->lock held.
2286 static void xhci_finish_resource_reservation(struct xhci_hcd *xhci, in xhci_finish_resource_reservation() argument
2291 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx); in xhci_finish_resource_reservation()
2292 xhci->num_active_eps -= num_dropped_eps; in xhci_finish_resource_reservation()
2294 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_finish_resource_reservation()
2297 xhci->num_active_eps); in xhci_finish_resource_reservation()
2302 switch (udev->speed) { in xhci_get_block_size()
2322 if (interval_bw->overhead[LS_OVERHEAD_TYPE]) in xhci_get_largest_overhead()
2324 if (interval_bw->overhead[FS_OVERHEAD_TYPE]) in xhci_get_largest_overhead()
2333 static int xhci_check_tt_bw_table(struct xhci_hcd *xhci, in xhci_check_tt_bw_table() argument
2341 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table; in xhci_check_tt_bw_table()
2342 tt_info = virt_dev->tt_info; in xhci_check_tt_bw_table()
2349 if (old_active_eps == 0 && tt_info->active_eps != 0) { in xhci_check_tt_bw_table()
2350 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT) in xhci_check_tt_bw_table()
2351 return -ENOMEM; in xhci_check_tt_bw_table()
2363 static int xhci_check_ss_bw(struct xhci_hcd *xhci, in xhci_check_ss_bw() argument
2369 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved)) in xhci_check_ss_bw()
2370 return -ENOMEM; in xhci_check_ss_bw()
2373 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved)) in xhci_check_ss_bw()
2374 return -ENOMEM; in xhci_check_ss_bw()
2380 * This algorithm is a very conservative estimate of the worst-case scheduling
2388 * over-estimate.
2420 static int xhci_check_bw_table(struct xhci_hcd *xhci, in xhci_check_bw_table() argument
2435 if (virt_dev->udev->speed >= USB_SPEED_SUPER) in xhci_check_bw_table()
2436 return xhci_check_ss_bw(xhci, virt_dev); in xhci_check_bw_table()
2438 if (virt_dev->udev->speed == USB_SPEED_HIGH) { in xhci_check_bw_table()
2447 bw_table = virt_dev->bw_table; in xhci_check_bw_table()
2451 block_size = xhci_get_block_size(virt_dev->udev); in xhci_check_bw_table()
2456 if (virt_dev->tt_info) { in xhci_check_bw_table()
2457 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2459 virt_dev->real_port); in xhci_check_bw_table()
2460 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) { in xhci_check_bw_table()
2461 xhci_warn(xhci, "Not enough bandwidth on HS bus for " in xhci_check_bw_table()
2463 return -ENOMEM; in xhci_check_bw_table()
2465 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2467 virt_dev->tt_info->slot_id, in xhci_check_bw_table()
2468 virt_dev->tt_info->ttport); in xhci_check_bw_table()
2470 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2472 virt_dev->real_port); in xhci_check_bw_table()
2478 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) + in xhci_check_bw_table()
2479 bw_table->interval_bw[0].num_packets * in xhci_check_bw_table()
2480 xhci_get_largest_overhead(&bw_table->interval_bw[0]); in xhci_check_bw_table()
2493 bw_table->interval_bw[i].num_packets; in xhci_check_bw_table()
2498 if (list_empty(&bw_table->interval_bw[i].endpoints)) in xhci_check_bw_table()
2504 ep_entry = bw_table->interval_bw[i].endpoints.next; in xhci_check_bw_table()
2509 virt_ep->bw_info.max_packet_size, in xhci_check_bw_table()
2517 &bw_table->interval_bw[i]); in xhci_check_bw_table()
2553 xhci_warn(xhci, "Not enough bandwidth. " in xhci_check_bw_table()
2556 return -ENOMEM; in xhci_check_bw_table()
2560 * Ok, we know we have some packets left over after even-handedly in xhci_check_bw_table()
2562 * fit into, so we over-schedule and say they will be scheduled every in xhci_check_bw_table()
2568 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) { in xhci_check_bw_table()
2569 unsigned int port_index = virt_dev->real_port - 1; in xhci_check_bw_table()
2576 xhci->rh_bw[port_index].num_active_tts; in xhci_check_bw_table()
2579 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2583 (max_bandwidth - bw_used - bw_reserved) * 100 / in xhci_check_bw_table()
2588 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n", in xhci_check_bw_table()
2590 return -ENOMEM; in xhci_check_bw_table()
2593 bw_table->bw_used = bw_used; in xhci_check_bw_table()
2611 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK); in xhci_get_ss_bw_consumed()
2613 if (ep_bw->ep_interval == 0) in xhci_get_ss_bw_consumed()
2615 (ep_bw->mult * ep_bw->num_packets * in xhci_get_ss_bw_consumed()
2617 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets * in xhci_get_ss_bw_consumed()
2619 1 << ep_bw->ep_interval); in xhci_get_ss_bw_consumed()
2623 static void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci, in xhci_drop_ep_from_interval_table() argument
2633 if (xhci_is_async_ep(ep_bw->type)) in xhci_drop_ep_from_interval_table()
2636 if (udev->speed >= USB_SPEED_SUPER) { in xhci_drop_ep_from_interval_table()
2637 if (xhci_is_sync_in_ep(ep_bw->type)) in xhci_drop_ep_from_interval_table()
2638 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -= in xhci_drop_ep_from_interval_table()
2641 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -= in xhci_drop_ep_from_interval_table()
2649 if (list_empty(&virt_ep->bw_endpoint_list)) in xhci_drop_ep_from_interval_table()
2654 if (udev->speed == USB_SPEED_HIGH) in xhci_drop_ep_from_interval_table()
2655 normalized_interval = ep_bw->ep_interval; in xhci_drop_ep_from_interval_table()
2657 normalized_interval = ep_bw->ep_interval - 3; in xhci_drop_ep_from_interval_table()
2660 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload; in xhci_drop_ep_from_interval_table()
2661 interval_bw = &bw_table->interval_bw[normalized_interval]; in xhci_drop_ep_from_interval_table()
2662 interval_bw->num_packets -= ep_bw->num_packets; in xhci_drop_ep_from_interval_table()
2663 switch (udev->speed) { in xhci_drop_ep_from_interval_table()
2665 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1; in xhci_drop_ep_from_interval_table()
2668 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1; in xhci_drop_ep_from_interval_table()
2671 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1; in xhci_drop_ep_from_interval_table()
2683 tt_info->active_eps -= 1; in xhci_drop_ep_from_interval_table()
2684 list_del_init(&virt_ep->bw_endpoint_list); in xhci_drop_ep_from_interval_table()
2687 static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci, in xhci_add_ep_to_interval_table() argument
2698 if (xhci_is_async_ep(ep_bw->type)) in xhci_add_ep_to_interval_table()
2701 if (udev->speed == USB_SPEED_SUPER) { in xhci_add_ep_to_interval_table()
2702 if (xhci_is_sync_in_ep(ep_bw->type)) in xhci_add_ep_to_interval_table()
2703 xhci->devs[udev->slot_id]->bw_table->ss_bw_in += in xhci_add_ep_to_interval_table()
2706 xhci->devs[udev->slot_id]->bw_table->ss_bw_out += in xhci_add_ep_to_interval_table()
2714 if (udev->speed == USB_SPEED_HIGH) in xhci_add_ep_to_interval_table()
2715 normalized_interval = ep_bw->ep_interval; in xhci_add_ep_to_interval_table()
2717 normalized_interval = ep_bw->ep_interval - 3; in xhci_add_ep_to_interval_table()
2720 bw_table->interval0_esit_payload += ep_bw->max_esit_payload; in xhci_add_ep_to_interval_table()
2721 interval_bw = &bw_table->interval_bw[normalized_interval]; in xhci_add_ep_to_interval_table()
2722 interval_bw->num_packets += ep_bw->num_packets; in xhci_add_ep_to_interval_table()
2723 switch (udev->speed) { in xhci_add_ep_to_interval_table()
2725 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1; in xhci_add_ep_to_interval_table()
2728 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1; in xhci_add_ep_to_interval_table()
2731 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1; in xhci_add_ep_to_interval_table()
2744 tt_info->active_eps += 1; in xhci_add_ep_to_interval_table()
2746 list_for_each_entry(smaller_ep, &interval_bw->endpoints, in xhci_add_ep_to_interval_table()
2748 if (ep_bw->max_packet_size >= in xhci_add_ep_to_interval_table()
2749 smaller_ep->bw_info.max_packet_size) { in xhci_add_ep_to_interval_table()
2751 list_add_tail(&virt_ep->bw_endpoint_list, in xhci_add_ep_to_interval_table()
2752 &smaller_ep->bw_endpoint_list); in xhci_add_ep_to_interval_table()
2757 list_add_tail(&virt_ep->bw_endpoint_list, in xhci_add_ep_to_interval_table()
2758 &interval_bw->endpoints); in xhci_add_ep_to_interval_table()
2761 void xhci_update_tt_active_eps(struct xhci_hcd *xhci, in xhci_update_tt_active_eps() argument
2766 if (!virt_dev->tt_info) in xhci_update_tt_active_eps()
2769 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1]; in xhci_update_tt_active_eps()
2771 virt_dev->tt_info->active_eps != 0) { in xhci_update_tt_active_eps()
2772 rh_bw_info->num_active_tts += 1; in xhci_update_tt_active_eps()
2773 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD; in xhci_update_tt_active_eps()
2775 virt_dev->tt_info->active_eps == 0) { in xhci_update_tt_active_eps()
2776 rh_bw_info->num_active_tts -= 1; in xhci_update_tt_active_eps()
2777 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD; in xhci_update_tt_active_eps()
2781 static int xhci_reserve_bandwidth(struct xhci_hcd *xhci, in xhci_reserve_bandwidth() argument
2790 if (virt_dev->tt_info) in xhci_reserve_bandwidth()
2791 old_active_eps = virt_dev->tt_info->active_eps; in xhci_reserve_bandwidth()
2795 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_reserve_bandwidth()
2797 return -ENOMEM; in xhci_reserve_bandwidth()
2805 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2811 xhci_drop_ep_from_interval_table(xhci, in xhci_reserve_bandwidth()
2812 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2813 virt_dev->bw_table, in xhci_reserve_bandwidth()
2814 virt_dev->udev, in xhci_reserve_bandwidth()
2815 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2816 virt_dev->tt_info); in xhci_reserve_bandwidth()
2819 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev); in xhci_reserve_bandwidth()
2823 xhci_add_ep_to_interval_table(xhci, in xhci_reserve_bandwidth()
2824 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2825 virt_dev->bw_table, in xhci_reserve_bandwidth()
2826 virt_dev->udev, in xhci_reserve_bandwidth()
2827 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2828 virt_dev->tt_info); in xhci_reserve_bandwidth()
2831 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) { in xhci_reserve_bandwidth()
2835 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps); in xhci_reserve_bandwidth()
2848 xhci_drop_ep_from_interval_table(xhci, in xhci_reserve_bandwidth()
2849 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2850 virt_dev->bw_table, in xhci_reserve_bandwidth()
2851 virt_dev->udev, in xhci_reserve_bandwidth()
2852 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2853 virt_dev->tt_info); in xhci_reserve_bandwidth()
2856 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i], in xhci_reserve_bandwidth()
2860 xhci_add_ep_to_interval_table(xhci, in xhci_reserve_bandwidth()
2861 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2862 virt_dev->bw_table, in xhci_reserve_bandwidth()
2863 virt_dev->udev, in xhci_reserve_bandwidth()
2864 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2865 virt_dev->tt_info); in xhci_reserve_bandwidth()
2867 return -ENOMEM; in xhci_reserve_bandwidth()
2874 static int xhci_configure_endpoint(struct xhci_hcd *xhci, in xhci_configure_endpoint() argument
2886 return -EINVAL; in xhci_configure_endpoint()
2888 spin_lock_irqsave(&xhci->lock, flags); in xhci_configure_endpoint()
2890 if (xhci->xhc_state & XHCI_STATE_DYING) { in xhci_configure_endpoint()
2891 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2892 return -ESHUTDOWN; in xhci_configure_endpoint()
2895 virt_dev = xhci->devs[udev->slot_id]; in xhci_configure_endpoint()
2897 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_configure_endpoint()
2899 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2900 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_configure_endpoint()
2902 return -ENOMEM; in xhci_configure_endpoint()
2905 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) && in xhci_configure_endpoint()
2906 xhci_reserve_host_resources(xhci, ctrl_ctx)) { in xhci_configure_endpoint()
2907 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2908 xhci_warn(xhci, "Not enough host resources, " in xhci_configure_endpoint()
2910 xhci->num_active_eps); in xhci_configure_endpoint()
2911 return -ENOMEM; in xhci_configure_endpoint()
2913 if ((xhci->quirks & XHCI_SW_BW_CHECKING) && in xhci_configure_endpoint()
2914 xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) { in xhci_configure_endpoint()
2915 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) in xhci_configure_endpoint()
2916 xhci_free_host_resources(xhci, ctrl_ctx); in xhci_configure_endpoint()
2917 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2918 xhci_warn(xhci, "Not enough bandwidth\n"); in xhci_configure_endpoint()
2919 return -ENOMEM; in xhci_configure_endpoint()
2922 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx); in xhci_configure_endpoint()
2928 ret = xhci_queue_configure_endpoint(xhci, command, in xhci_configure_endpoint()
2929 command->in_ctx->dma, in xhci_configure_endpoint()
2930 udev->slot_id, must_succeed); in xhci_configure_endpoint()
2932 ret = xhci_queue_evaluate_context(xhci, command, in xhci_configure_endpoint()
2933 command->in_ctx->dma, in xhci_configure_endpoint()
2934 udev->slot_id, must_succeed); in xhci_configure_endpoint()
2936 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) in xhci_configure_endpoint()
2937 xhci_free_host_resources(xhci, ctrl_ctx); in xhci_configure_endpoint()
2938 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2939 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_configure_endpoint()
2941 return -ENOMEM; in xhci_configure_endpoint()
2943 xhci_ring_cmd_db(xhci); in xhci_configure_endpoint()
2944 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2947 wait_for_completion(command->completion); in xhci_configure_endpoint()
2950 ret = xhci_configure_endpoint_result(xhci, udev, in xhci_configure_endpoint()
2951 &command->status); in xhci_configure_endpoint()
2953 ret = xhci_evaluate_context_result(xhci, udev, in xhci_configure_endpoint()
2954 &command->status); in xhci_configure_endpoint()
2956 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { in xhci_configure_endpoint()
2957 spin_lock_irqsave(&xhci->lock, flags); in xhci_configure_endpoint()
2962 xhci_free_host_resources(xhci, ctrl_ctx); in xhci_configure_endpoint()
2964 xhci_finish_resource_reservation(xhci, ctrl_ctx); in xhci_configure_endpoint()
2965 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2970 static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci, in xhci_check_bw_drop_ep_streams() argument
2973 struct xhci_virt_ep *ep = &vdev->eps[i]; in xhci_check_bw_drop_ep_streams()
2975 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_check_bw_drop_ep_streams()
2976 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n", in xhci_check_bw_drop_ep_streams()
2978 xhci_free_stream_info(xhci, ep->stream_info); in xhci_check_bw_drop_ep_streams()
2979 ep->stream_info = NULL; in xhci_check_bw_drop_ep_streams()
2980 ep->ep_state &= ~EP_HAS_STREAMS; in xhci_check_bw_drop_ep_streams()
2991 * else should be touching the xhci->devs[slot_id] structure, so we
2992 * don't need to take the xhci->lock for manipulating that.
2998 struct xhci_hcd *xhci; in xhci_check_bandwidth() local
3007 xhci = hcd_to_xhci(hcd); in xhci_check_bandwidth()
3008 if ((xhci->xhc_state & XHCI_STATE_DYING) || in xhci_check_bandwidth()
3009 (xhci->xhc_state & XHCI_STATE_REMOVING)) in xhci_check_bandwidth()
3010 return -ENODEV; in xhci_check_bandwidth()
3012 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); in xhci_check_bandwidth()
3013 virt_dev = xhci->devs[udev->slot_id]; in xhci_check_bandwidth()
3015 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_check_bandwidth()
3017 return -ENOMEM; in xhci_check_bandwidth()
3019 command->in_ctx = virt_dev->in_ctx; in xhci_check_bandwidth()
3021 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */ in xhci_check_bandwidth()
3022 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_check_bandwidth()
3024 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_check_bandwidth()
3026 ret = -ENOMEM; in xhci_check_bandwidth()
3029 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_check_bandwidth()
3030 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG); in xhci_check_bandwidth()
3031 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG)); in xhci_check_bandwidth()
3034 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) && in xhci_check_bandwidth()
3035 ctrl_ctx->drop_flags == 0) { in xhci_check_bandwidth()
3040 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); in xhci_check_bandwidth()
3041 for (i = 31; i >= 1; i--) { in xhci_check_bandwidth()
3044 if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32)) in xhci_check_bandwidth()
3045 || (ctrl_ctx->add_flags & le32) || i == 1) { in xhci_check_bandwidth()
3046 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK); in xhci_check_bandwidth()
3047 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i)); in xhci_check_bandwidth()
3052 ret = xhci_configure_endpoint(xhci, udev, command, in xhci_check_bandwidth()
3060 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) && in xhci_check_bandwidth()
3061 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) { in xhci_check_bandwidth()
3062 xhci_free_endpoint_ring(xhci, virt_dev, i); in xhci_check_bandwidth()
3063 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i); in xhci_check_bandwidth()
3066 xhci_zero_in_ctx(xhci, virt_dev); in xhci_check_bandwidth()
3072 if (!virt_dev->eps[i].new_ring) in xhci_check_bandwidth()
3077 if (virt_dev->eps[i].ring) { in xhci_check_bandwidth()
3078 xhci_free_endpoint_ring(xhci, virt_dev, i); in xhci_check_bandwidth()
3080 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i); in xhci_check_bandwidth()
3081 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring; in xhci_check_bandwidth()
3082 virt_dev->eps[i].new_ring = NULL; in xhci_check_bandwidth()
3083 xhci_debugfs_create_endpoint(xhci, virt_dev, i); in xhci_check_bandwidth()
3086 kfree(command->completion); in xhci_check_bandwidth()
3095 struct xhci_hcd *xhci; in xhci_reset_bandwidth() local
3102 xhci = hcd_to_xhci(hcd); in xhci_reset_bandwidth()
3104 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); in xhci_reset_bandwidth()
3105 virt_dev = xhci->devs[udev->slot_id]; in xhci_reset_bandwidth()
3108 if (virt_dev->eps[i].new_ring) { in xhci_reset_bandwidth()
3109 xhci_debugfs_remove_endpoint(xhci, virt_dev, i); in xhci_reset_bandwidth()
3110 xhci_ring_free(xhci, virt_dev->eps[i].new_ring); in xhci_reset_bandwidth()
3111 virt_dev->eps[i].new_ring = NULL; in xhci_reset_bandwidth()
3114 xhci_zero_in_ctx(xhci, virt_dev); in xhci_reset_bandwidth()
3118 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci, in xhci_setup_input_ctx_for_config_ep() argument
3124 ctrl_ctx->add_flags = cpu_to_le32(add_flags); in xhci_setup_input_ctx_for_config_ep()
3125 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags); in xhci_setup_input_ctx_for_config_ep()
3126 xhci_slot_copy(xhci, in_ctx, out_ctx); in xhci_setup_input_ctx_for_config_ep()
3127 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_setup_input_ctx_for_config_ep()
3133 struct xhci_hcd *xhci; in xhci_endpoint_disable() local
3140 xhci = hcd_to_xhci(hcd); in xhci_endpoint_disable()
3142 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_disable()
3144 udev = (struct usb_device *)host_ep->hcpriv; in xhci_endpoint_disable()
3145 if (!udev || !udev->slot_id) in xhci_endpoint_disable()
3148 vdev = xhci->devs[udev->slot_id]; in xhci_endpoint_disable()
3152 ep_index = xhci_get_endpoint_index(&host_ep->desc); in xhci_endpoint_disable()
3153 ep = &vdev->eps[ep_index]; in xhci_endpoint_disable()
3158 if (ep->ep_state & EP_CLEARING_TT) { in xhci_endpoint_disable()
3159 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_disable()
3164 if (ep->ep_state) in xhci_endpoint_disable()
3165 xhci_dbg(xhci, "endpoint disable with ep_state 0x%x\n", in xhci_endpoint_disable()
3166 ep->ep_state); in xhci_endpoint_disable()
3168 host_ep->hcpriv = NULL; in xhci_endpoint_disable()
3169 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_disable()
3181 * endpoint. Refer to the additional note in xhci spcification section 4.6.8.
3187 struct xhci_hcd *xhci; in xhci_endpoint_reset() local
3198 xhci = hcd_to_xhci(hcd); in xhci_endpoint_reset()
3199 if (!host_ep->hcpriv) in xhci_endpoint_reset()
3201 udev = (struct usb_device *) host_ep->hcpriv; in xhci_endpoint_reset()
3202 vdev = xhci->devs[udev->slot_id]; in xhci_endpoint_reset()
3205 * vdev may be lost due to xHC restore error and re-initialization in xhci_endpoint_reset()
3209 if (!udev->slot_id || !vdev) in xhci_endpoint_reset()
3211 ep_index = xhci_get_endpoint_index(&host_ep->desc); in xhci_endpoint_reset()
3212 ep = &vdev->eps[ep_index]; in xhci_endpoint_reset()
3217 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3218 if (ep->ep_state & EP_HARD_CLEAR_TOGGLE) { in xhci_endpoint_reset()
3219 ep->ep_state &= ~EP_HARD_CLEAR_TOGGLE; in xhci_endpoint_reset()
3220 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3223 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3224 /* Only interrupt and bulk ep's use data toggle, USB2 spec 5.5.4-> */ in xhci_endpoint_reset()
3225 if (usb_endpoint_xfer_control(&host_ep->desc) || in xhci_endpoint_reset()
3226 usb_endpoint_xfer_isoc(&host_ep->desc)) in xhci_endpoint_reset()
3229 ep_flag = xhci_get_endpoint_flag(&host_ep->desc); in xhci_endpoint_reset()
3234 stop_cmd = xhci_alloc_command(xhci, true, GFP_NOWAIT); in xhci_endpoint_reset()
3238 cfg_cmd = xhci_alloc_command_with_ctx(xhci, true, GFP_NOWAIT); in xhci_endpoint_reset()
3242 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3245 ep->ep_state |= EP_SOFT_CLEAR_TOGGLE; in xhci_endpoint_reset()
3253 if (!list_empty(&ep->ring->td_list)) { in xhci_endpoint_reset()
3254 dev_err(&udev->dev, "EP not empty, refuse reset\n"); in xhci_endpoint_reset()
3255 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3256 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3260 err = xhci_queue_stop_endpoint(xhci, stop_cmd, udev->slot_id, in xhci_endpoint_reset()
3263 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3264 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3265 xhci_dbg(xhci, "%s: Failed to queue stop ep command, %d ", in xhci_endpoint_reset()
3270 xhci_ring_cmd_db(xhci); in xhci_endpoint_reset()
3271 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3273 wait_for_completion(stop_cmd->completion); in xhci_endpoint_reset()
3275 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3278 ctrl_ctx = xhci_get_input_control_ctx(cfg_cmd->in_ctx); in xhci_endpoint_reset()
3280 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3281 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3282 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_endpoint_reset()
3287 xhci_setup_input_ctx_for_config_ep(xhci, cfg_cmd->in_ctx, vdev->out_ctx, in xhci_endpoint_reset()
3289 xhci_endpoint_copy(xhci, cfg_cmd->in_ctx, vdev->out_ctx, ep_index); in xhci_endpoint_reset()
3291 err = xhci_queue_configure_endpoint(xhci, cfg_cmd, cfg_cmd->in_ctx->dma, in xhci_endpoint_reset()
3292 udev->slot_id, false); in xhci_endpoint_reset()
3294 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3295 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3296 xhci_dbg(xhci, "%s: Failed to queue config ep command, %d ", in xhci_endpoint_reset()
3301 xhci_ring_cmd_db(xhci); in xhci_endpoint_reset()
3302 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3304 wait_for_completion(cfg_cmd->completion); in xhci_endpoint_reset()
3306 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3308 xhci_free_command(xhci, stop_cmd); in xhci_endpoint_reset()
3309 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3310 if (ep->ep_state & EP_SOFT_CLEAR_TOGGLE) in xhci_endpoint_reset()
3311 ep->ep_state &= ~EP_SOFT_CLEAR_TOGGLE; in xhci_endpoint_reset()
3312 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3315 static int xhci_check_streams_endpoint(struct xhci_hcd *xhci, in xhci_check_streams_endpoint() argument
3324 return -EINVAL; in xhci_check_streams_endpoint()
3325 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__); in xhci_check_streams_endpoint()
3327 return -EINVAL; in xhci_check_streams_endpoint()
3328 if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) { in xhci_check_streams_endpoint()
3329 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion" in xhci_check_streams_endpoint()
3331 ep->desc.bEndpointAddress); in xhci_check_streams_endpoint()
3332 return -EINVAL; in xhci_check_streams_endpoint()
3335 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_check_streams_endpoint()
3336 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; in xhci_check_streams_endpoint()
3339 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x " in xhci_check_streams_endpoint()
3341 ep->desc.bEndpointAddress); in xhci_check_streams_endpoint()
3342 xhci_warn(xhci, "Send email to xHCI maintainer and ask for " in xhci_check_streams_endpoint()
3344 return -EINVAL; in xhci_check_streams_endpoint()
3346 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) { in xhci_check_streams_endpoint()
3347 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk " in xhci_check_streams_endpoint()
3349 ep->desc.bEndpointAddress); in xhci_check_streams_endpoint()
3350 return -EINVAL; in xhci_check_streams_endpoint()
3355 static void xhci_calculate_streams_entries(struct xhci_hcd *xhci, in xhci_calculate_streams_entries() argument
3365 * level page entries), but that's an optional feature for xHCI host in xhci_calculate_streams_entries()
3368 max_streams = HCC_MAX_PSA(xhci->hcc_params); in xhci_calculate_streams_entries()
3370 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n", in xhci_calculate_streams_entries()
3381 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci, in xhci_calculate_streams_and_bitmask() argument
3392 ret = xhci_check_streams_endpoint(xhci, udev, in xhci_calculate_streams_and_bitmask()
3393 eps[i], udev->slot_id); in xhci_calculate_streams_and_bitmask()
3397 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp); in xhci_calculate_streams_and_bitmask()
3398 if (max_streams < (*num_streams - 1)) { in xhci_calculate_streams_and_bitmask()
3399 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n", in xhci_calculate_streams_and_bitmask()
3400 eps[i]->desc.bEndpointAddress, in xhci_calculate_streams_and_bitmask()
3405 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc); in xhci_calculate_streams_and_bitmask()
3407 return -EINVAL; in xhci_calculate_streams_and_bitmask()
3413 static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci, in xhci_calculate_no_streams_bitmask() argument
3423 slot_id = udev->slot_id; in xhci_calculate_no_streams_bitmask()
3424 if (!xhci->devs[slot_id]) in xhci_calculate_no_streams_bitmask()
3428 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_calculate_no_streams_bitmask()
3429 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; in xhci_calculate_no_streams_bitmask()
3432 xhci_warn(xhci, "WARN Can't disable streams for " in xhci_calculate_no_streams_bitmask()
3435 eps[i]->desc.bEndpointAddress); in xhci_calculate_no_streams_bitmask()
3441 xhci_warn(xhci, "WARN Can't disable streams for " in xhci_calculate_no_streams_bitmask()
3444 eps[i]->desc.bEndpointAddress); in xhci_calculate_no_streams_bitmask()
3445 xhci_warn(xhci, "WARN xhci_free_streams() called " in xhci_calculate_no_streams_bitmask()
3446 "with non-streams endpoint\n"); in xhci_calculate_no_streams_bitmask()
3449 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc); in xhci_calculate_no_streams_bitmask()
3475 struct xhci_hcd *xhci; in xhci_alloc_streams() local
3486 return -EINVAL; in xhci_alloc_streams()
3489 * stream 0 that is reserved for xHCI usage. in xhci_alloc_streams()
3492 xhci = hcd_to_xhci(hcd); in xhci_alloc_streams()
3493 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n", in xhci_alloc_streams()
3497 if ((xhci->quirks & XHCI_BROKEN_STREAMS) || in xhci_alloc_streams()
3498 HCC_MAX_PSA(xhci->hcc_params) < 4) { in xhci_alloc_streams()
3499 xhci_dbg(xhci, "xHCI controller does not support streams.\n"); in xhci_alloc_streams()
3500 return -ENOSYS; in xhci_alloc_streams()
3503 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags); in xhci_alloc_streams()
3505 return -ENOMEM; in xhci_alloc_streams()
3507 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx); in xhci_alloc_streams()
3509 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_alloc_streams()
3511 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3512 return -ENOMEM; in xhci_alloc_streams()
3519 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_streams()
3520 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps, in xhci_alloc_streams()
3523 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3524 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3528 xhci_warn(xhci, "WARN: endpoints can't handle " in xhci_alloc_streams()
3530 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3531 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3532 return -EINVAL; in xhci_alloc_streams()
3534 vdev = xhci->devs[udev->slot_id]; in xhci_alloc_streams()
3539 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3540 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS; in xhci_alloc_streams()
3542 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3548 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs); in xhci_alloc_streams()
3549 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n", in xhci_alloc_streams()
3553 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3554 max_packet = usb_endpoint_maxp(&eps[i]->desc); in xhci_alloc_streams()
3555 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci, in xhci_alloc_streams()
3559 if (!vdev->eps[ep_index].stream_info) in xhci_alloc_streams()
3570 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3571 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index); in xhci_alloc_streams()
3573 xhci_endpoint_copy(xhci, config_cmd->in_ctx, in xhci_alloc_streams()
3574 vdev->out_ctx, ep_index); in xhci_alloc_streams()
3575 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx, in xhci_alloc_streams()
3576 vdev->eps[ep_index].stream_info); in xhci_alloc_streams()
3581 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx, in xhci_alloc_streams()
3582 vdev->out_ctx, ctrl_ctx, in xhci_alloc_streams()
3586 ret = xhci_configure_endpoint(xhci, udev, config_cmd, in xhci_alloc_streams()
3596 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_streams()
3598 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3599 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; in xhci_alloc_streams()
3600 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n", in xhci_alloc_streams()
3601 udev->slot_id, ep_index); in xhci_alloc_streams()
3602 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS; in xhci_alloc_streams()
3604 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3605 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3608 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3609 xhci_debugfs_create_stream_files(xhci, vdev, ep_index); in xhci_alloc_streams()
3612 return num_streams - 1; in xhci_alloc_streams()
3617 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3618 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); in xhci_alloc_streams()
3619 vdev->eps[ep_index].stream_info = NULL; in xhci_alloc_streams()
3623 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; in xhci_alloc_streams()
3624 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; in xhci_alloc_streams()
3625 xhci_endpoint_zero(xhci, vdev, eps[i]); in xhci_alloc_streams()
3627 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3628 return -ENOMEM; in xhci_alloc_streams()
3642 struct xhci_hcd *xhci; in xhci_free_streams() local
3650 xhci = hcd_to_xhci(hcd); in xhci_free_streams()
3651 vdev = xhci->devs[udev->slot_id]; in xhci_free_streams()
3654 spin_lock_irqsave(&xhci->lock, flags); in xhci_free_streams()
3655 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci, in xhci_free_streams()
3658 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3659 return -EINVAL; in xhci_free_streams()
3666 ep_index = xhci_get_endpoint_index(&eps[0]->desc); in xhci_free_streams()
3667 command = vdev->eps[ep_index].stream_info->free_streams_command; in xhci_free_streams()
3668 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_free_streams()
3670 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3671 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_free_streams()
3673 return -EINVAL; in xhci_free_streams()
3679 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_free_streams()
3680 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); in xhci_free_streams()
3681 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |= in xhci_free_streams()
3684 xhci_endpoint_copy(xhci, command->in_ctx, in xhci_free_streams()
3685 vdev->out_ctx, ep_index); in xhci_free_streams()
3687 &vdev->eps[ep_index]); in xhci_free_streams()
3689 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx, in xhci_free_streams()
3690 vdev->out_ctx, ctrl_ctx, in xhci_free_streams()
3692 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3697 ret = xhci_configure_endpoint(xhci, udev, command, in xhci_free_streams()
3706 spin_lock_irqsave(&xhci->lock, flags); in xhci_free_streams()
3708 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_free_streams()
3709 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); in xhci_free_streams()
3710 vdev->eps[ep_index].stream_info = NULL; in xhci_free_streams()
3714 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS; in xhci_free_streams()
3715 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; in xhci_free_streams()
3717 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3727 * Must be called with xhci->lock held.
3729 void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci, in xhci_free_device_endpoint_resources() argument
3737 if (virt_dev->eps[i].ring) { in xhci_free_device_endpoint_resources()
3742 xhci->num_active_eps -= num_dropped_eps; in xhci_free_device_endpoint_resources()
3744 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_free_device_endpoint_resources()
3748 xhci->num_active_eps); in xhci_free_device_endpoint_resources()
3755 * xhci_address_device(), and then re-set up the configuration. If this is
3757 * settings will be re-installed through the normal bandwidth allocation
3766 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3767 * re-allocate the device.
3774 struct xhci_hcd *xhci; in xhci_discover_or_reset_device() local
3784 xhci = hcd_to_xhci(hcd); in xhci_discover_or_reset_device()
3785 slot_id = udev->slot_id; in xhci_discover_or_reset_device()
3786 virt_dev = xhci->devs[slot_id]; in xhci_discover_or_reset_device()
3788 xhci_dbg(xhci, "The device to be reset with slot ID %u does " in xhci_discover_or_reset_device()
3789 "not exist. Re-allocate the device\n", slot_id); in xhci_discover_or_reset_device()
3794 return -EINVAL; in xhci_discover_or_reset_device()
3797 if (virt_dev->tt_info) in xhci_discover_or_reset_device()
3798 old_active_eps = virt_dev->tt_info->active_eps; in xhci_discover_or_reset_device()
3800 if (virt_dev->udev != udev) { in xhci_discover_or_reset_device()
3803 * Re-allocate the device. in xhci_discover_or_reset_device()
3805 xhci_dbg(xhci, "The device to be reset with slot ID %u does " in xhci_discover_or_reset_device()
3806 "not match the udev. Re-allocate the device\n", in xhci_discover_or_reset_device()
3812 return -EINVAL; in xhci_discover_or_reset_device()
3816 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_discover_or_reset_device()
3817 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) == in xhci_discover_or_reset_device()
3823 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id); in xhci_discover_or_reset_device()
3830 reset_device_cmd = xhci_alloc_command(xhci, true, GFP_NOIO); in xhci_discover_or_reset_device()
3832 xhci_dbg(xhci, "Couldn't allocate command structure.\n"); in xhci_discover_or_reset_device()
3833 return -ENOMEM; in xhci_discover_or_reset_device()
3837 spin_lock_irqsave(&xhci->lock, flags); in xhci_discover_or_reset_device()
3839 ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id); in xhci_discover_or_reset_device()
3841 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); in xhci_discover_or_reset_device()
3842 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_discover_or_reset_device()
3845 xhci_ring_cmd_db(xhci); in xhci_discover_or_reset_device()
3846 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_discover_or_reset_device()
3849 wait_for_completion(reset_device_cmd->completion); in xhci_discover_or_reset_device()
3855 ret = reset_device_cmd->status; in xhci_discover_or_reset_device()
3859 xhci_warn(xhci, "Timeout waiting for reset device command\n"); in xhci_discover_or_reset_device()
3860 ret = -ETIME; in xhci_discover_or_reset_device()
3864 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n", in xhci_discover_or_reset_device()
3866 xhci_get_slot_state(xhci, virt_dev->out_ctx)); in xhci_discover_or_reset_device()
3867 xhci_dbg(xhci, "Not freeing device rings.\n"); in xhci_discover_or_reset_device()
3872 xhci_dbg(xhci, "Successful reset device command.\n"); in xhci_discover_or_reset_device()
3875 if (xhci_is_vendor_info_code(xhci, ret)) in xhci_discover_or_reset_device()
3877 xhci_warn(xhci, "Unknown completion code %u for " in xhci_discover_or_reset_device()
3879 ret = -EINVAL; in xhci_discover_or_reset_device()
3884 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { in xhci_discover_or_reset_device()
3885 spin_lock_irqsave(&xhci->lock, flags); in xhci_discover_or_reset_device()
3887 xhci_free_device_endpoint_resources(xhci, virt_dev, false); in xhci_discover_or_reset_device()
3888 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_discover_or_reset_device()
3893 struct xhci_virt_ep *ep = &virt_dev->eps[i]; in xhci_discover_or_reset_device()
3895 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_discover_or_reset_device()
3896 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n", in xhci_discover_or_reset_device()
3898 xhci_free_stream_info(xhci, ep->stream_info); in xhci_discover_or_reset_device()
3899 ep->stream_info = NULL; in xhci_discover_or_reset_device()
3900 ep->ep_state &= ~EP_HAS_STREAMS; in xhci_discover_or_reset_device()
3903 if (ep->ring) { in xhci_discover_or_reset_device()
3904 xhci_debugfs_remove_endpoint(xhci, virt_dev, i); in xhci_discover_or_reset_device()
3905 xhci_free_endpoint_ring(xhci, virt_dev, i); in xhci_discover_or_reset_device()
3907 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list)) in xhci_discover_or_reset_device()
3908 xhci_drop_ep_from_interval_table(xhci, in xhci_discover_or_reset_device()
3909 &virt_dev->eps[i].bw_info, in xhci_discover_or_reset_device()
3910 virt_dev->bw_table, in xhci_discover_or_reset_device()
3912 &virt_dev->eps[i], in xhci_discover_or_reset_device()
3913 virt_dev->tt_info); in xhci_discover_or_reset_device()
3914 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info); in xhci_discover_or_reset_device()
3917 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps); in xhci_discover_or_reset_device()
3918 virt_dev->flags = 0; in xhci_discover_or_reset_device()
3922 xhci_free_command(xhci, reset_device_cmd); in xhci_discover_or_reset_device()
3933 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_free_dev() local
3944 if (xhci->quirks & XHCI_RESET_ON_RESUME) in xhci_free_dev()
3945 pm_runtime_put_noidle(hcd->self.controller); in xhci_free_dev()
3952 if (ret <= 0 && ret != -ENODEV) in xhci_free_dev()
3955 virt_dev = xhci->devs[udev->slot_id]; in xhci_free_dev()
3956 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_free_dev()
3961 virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING; in xhci_free_dev()
3962 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer); in xhci_free_dev()
3964 virt_dev->udev = NULL; in xhci_free_dev()
3965 ret = xhci_disable_slot(xhci, udev->slot_id); in xhci_free_dev()
3967 xhci_free_virt_device(xhci, udev->slot_id); in xhci_free_dev()
3970 int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id) in xhci_disable_slot() argument
3977 command = xhci_alloc_command(xhci, false, GFP_KERNEL); in xhci_disable_slot()
3979 return -ENOMEM; in xhci_disable_slot()
3981 xhci_debugfs_remove_slot(xhci, slot_id); in xhci_disable_slot()
3983 spin_lock_irqsave(&xhci->lock, flags); in xhci_disable_slot()
3985 state = readl(&xhci->op_regs->status); in xhci_disable_slot()
3986 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) || in xhci_disable_slot()
3987 (xhci->xhc_state & XHCI_STATE_HALTED)) { in xhci_disable_slot()
3988 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_slot()
3990 return -ENODEV; in xhci_disable_slot()
3993 ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT, in xhci_disable_slot()
3996 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_slot()
4000 xhci_ring_cmd_db(xhci); in xhci_disable_slot()
4001 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_slot()
4009 * Must be called with xhci->lock held.
4011 static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci) in xhci_reserve_host_control_ep_resources() argument
4013 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) { in xhci_reserve_host_control_ep_resources()
4014 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_control_ep_resources()
4017 xhci->num_active_eps, xhci->limit_active_eps); in xhci_reserve_host_control_ep_resources()
4018 return -ENOMEM; in xhci_reserve_host_control_ep_resources()
4020 xhci->num_active_eps += 1; in xhci_reserve_host_control_ep_resources()
4021 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_control_ep_resources()
4023 xhci->num_active_eps); in xhci_reserve_host_control_ep_resources()
4034 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_alloc_dev() local
4041 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_alloc_dev()
4045 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_dev()
4046 ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0); in xhci_alloc_dev()
4048 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
4049 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); in xhci_alloc_dev()
4050 xhci_free_command(xhci, command); in xhci_alloc_dev()
4053 xhci_ring_cmd_db(xhci); in xhci_alloc_dev()
4054 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
4056 wait_for_completion(command->completion); in xhci_alloc_dev()
4057 slot_id = command->slot_id; in xhci_alloc_dev()
4059 if (!slot_id || command->status != COMP_SUCCESS) { in xhci_alloc_dev()
4060 xhci_err(xhci, "Error while assigning device slot ID\n"); in xhci_alloc_dev()
4061 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n", in xhci_alloc_dev()
4063 readl(&xhci->cap_regs->hcs_params1))); in xhci_alloc_dev()
4064 xhci_free_command(xhci, command); in xhci_alloc_dev()
4068 xhci_free_command(xhci, command); in xhci_alloc_dev()
4070 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { in xhci_alloc_dev()
4071 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_dev()
4072 ret = xhci_reserve_host_control_ep_resources(xhci); in xhci_alloc_dev()
4074 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
4075 xhci_warn(xhci, "Not enough host resources, " in xhci_alloc_dev()
4077 xhci->num_active_eps); in xhci_alloc_dev()
4080 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
4086 if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) { in xhci_alloc_dev()
4087 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n"); in xhci_alloc_dev()
4090 vdev = xhci->devs[slot_id]; in xhci_alloc_dev()
4091 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); in xhci_alloc_dev()
4094 udev->slot_id = slot_id; in xhci_alloc_dev()
4096 xhci_debugfs_create_slot(xhci, slot_id); in xhci_alloc_dev()
4103 if (xhci->quirks & XHCI_RESET_ON_RESUME) in xhci_alloc_dev()
4104 pm_runtime_get_noresume(hcd->self.controller); in xhci_alloc_dev()
4112 ret = xhci_disable_slot(xhci, udev->slot_id); in xhci_alloc_dev()
4114 xhci_free_virt_device(xhci, udev->slot_id); in xhci_alloc_dev()
4130 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_setup_device() local
4136 mutex_lock(&xhci->mutex); in xhci_setup_device()
4138 if (xhci->xhc_state) { /* dying, removing or halted */ in xhci_setup_device()
4139 ret = -ESHUTDOWN; in xhci_setup_device()
4143 if (!udev->slot_id) { in xhci_setup_device()
4144 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4145 "Bad Slot ID %d", udev->slot_id); in xhci_setup_device()
4146 ret = -EINVAL; in xhci_setup_device()
4150 virt_dev = xhci->devs[udev->slot_id]; in xhci_setup_device()
4155 * a zero-dereference was observed once due to virt_dev = 0. in xhci_setup_device()
4158 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n", in xhci_setup_device()
4159 udev->slot_id); in xhci_setup_device()
4160 ret = -EINVAL; in xhci_setup_device()
4163 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_setup_device()
4167 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) == in xhci_setup_device()
4169 xhci_dbg(xhci, "Slot already in default state\n"); in xhci_setup_device()
4174 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_setup_device()
4176 ret = -ENOMEM; in xhci_setup_device()
4180 command->in_ctx = virt_dev->in_ctx; in xhci_setup_device()
4182 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); in xhci_setup_device()
4183 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); in xhci_setup_device()
4185 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_setup_device()
4187 ret = -EINVAL; in xhci_setup_device()
4191 * If this is the first Set Address since device plug-in or in xhci_setup_device()
4192 * virt_device realloaction after a resume with an xHCI power loss, in xhci_setup_device()
4195 if (!slot_ctx->dev_info) in xhci_setup_device()
4196 xhci_setup_addressable_virt_dev(xhci, udev); in xhci_setup_device()
4199 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev); in xhci_setup_device()
4200 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG); in xhci_setup_device()
4201 ctrl_ctx->drop_flags = 0; in xhci_setup_device()
4203 trace_xhci_address_ctx(xhci, virt_dev->in_ctx, in xhci_setup_device()
4204 le32_to_cpu(slot_ctx->dev_info) >> 27); in xhci_setup_device()
4207 spin_lock_irqsave(&xhci->lock, flags); in xhci_setup_device()
4209 ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma, in xhci_setup_device()
4210 udev->slot_id, setup); in xhci_setup_device()
4212 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_setup_device()
4213 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4217 xhci_ring_cmd_db(xhci); in xhci_setup_device()
4218 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_setup_device()
4221 wait_for_completion(command->completion); in xhci_setup_device()
4227 switch (command->status) { in xhci_setup_device()
4230 xhci_warn(xhci, "Timeout while waiting for setup device command\n"); in xhci_setup_device()
4231 ret = -ETIME; in xhci_setup_device()
4235 xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n", in xhci_setup_device()
4236 act, udev->slot_id); in xhci_setup_device()
4237 ret = -EINVAL; in xhci_setup_device()
4240 dev_warn(&udev->dev, "Device not responding to setup %s.\n", act); in xhci_setup_device()
4242 mutex_unlock(&xhci->mutex); in xhci_setup_device()
4243 ret = xhci_disable_slot(xhci, udev->slot_id); in xhci_setup_device()
4246 kfree(command->completion); in xhci_setup_device()
4248 return -EPROTO; in xhci_setup_device()
4250 dev_warn(&udev->dev, in xhci_setup_device()
4252 ret = -ENODEV; in xhci_setup_device()
4255 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4259 xhci_err(xhci, in xhci_setup_device()
4261 act, command->status); in xhci_setup_device()
4262 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1); in xhci_setup_device()
4263 ret = -EINVAL; in xhci_setup_device()
4268 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); in xhci_setup_device()
4269 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4271 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4273 udev->slot_id, in xhci_setup_device()
4274 &xhci->dcbaa->dev_context_ptrs[udev->slot_id], in xhci_setup_device()
4276 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id])); in xhci_setup_device()
4277 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4279 (unsigned long long)virt_dev->out_ctx->dma); in xhci_setup_device()
4280 trace_xhci_address_ctx(xhci, virt_dev->in_ctx, in xhci_setup_device()
4281 le32_to_cpu(slot_ctx->dev_info) >> 27); in xhci_setup_device()
4286 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, in xhci_setup_device()
4287 le32_to_cpu(slot_ctx->dev_info) >> 27); in xhci_setup_device()
4289 ctrl_ctx->add_flags = 0; in xhci_setup_device()
4290 ctrl_ctx->drop_flags = 0; in xhci_setup_device()
4291 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_setup_device()
4292 udev->devaddr = (u8)(le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); in xhci_setup_device()
4294 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4296 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); in xhci_setup_device()
4298 mutex_unlock(&xhci->mutex); in xhci_setup_device()
4300 kfree(command->completion); in xhci_setup_device()
4327 return rhub->ports[port1 - 1]->hw_portnum + 1; in xhci_find_raw_port_number()
4334 static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci, in xhci_change_max_exit_latency() argument
4344 spin_lock_irqsave(&xhci->lock, flags); in xhci_change_max_exit_latency()
4346 virt_dev = xhci->devs[udev->slot_id]; in xhci_change_max_exit_latency()
4350 * xHC was re-initialized. Exit latency will be set later after in xhci_change_max_exit_latency()
4351 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated in xhci_change_max_exit_latency()
4354 if (!virt_dev || max_exit_latency == virt_dev->current_mel) { in xhci_change_max_exit_latency()
4355 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4360 command = xhci->lpm_command; in xhci_change_max_exit_latency()
4361 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_change_max_exit_latency()
4363 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4364 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_change_max_exit_latency()
4366 return -ENOMEM; in xhci_change_max_exit_latency()
4369 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx); in xhci_change_max_exit_latency()
4370 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4372 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_change_max_exit_latency()
4373 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx); in xhci_change_max_exit_latency()
4374 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT)); in xhci_change_max_exit_latency()
4375 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency); in xhci_change_max_exit_latency()
4376 slot_ctx->dev_state = 0; in xhci_change_max_exit_latency()
4378 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_change_max_exit_latency()
4382 ret = xhci_configure_endpoint(xhci, udev, command, in xhci_change_max_exit_latency()
4386 spin_lock_irqsave(&xhci->lock, flags); in xhci_change_max_exit_latency()
4387 virt_dev->current_mel = max_exit_latency; in xhci_change_max_exit_latency()
4388 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4400 static int xhci_calculate_hird_besl(struct xhci_hcd *xhci, in xhci_calculate_hird_besl() argument
4407 u2del = HCS_U2_LATENCY(xhci->hcs_params3); in xhci_calculate_hird_besl()
4408 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); in xhci_calculate_hird_besl()
4424 besl_host = (u2del - 51) / 75 + 1; in xhci_calculate_hird_besl()
4442 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); in xhci_calculate_usb2_hw_lpm_params()
4444 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */ in xhci_calculate_usb2_hw_lpm_params()
4445 l1 = udev->l1_params.timeout / 256; in xhci_calculate_usb2_hw_lpm_params()
4459 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_set_usb2_hardware_lpm() local
4468 if (xhci->quirks & XHCI_HW_LPM_DISABLE) in xhci_set_usb2_hardware_lpm()
4469 return -EPERM; in xhci_set_usb2_hardware_lpm()
4471 if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support || in xhci_set_usb2_hardware_lpm()
4472 !udev->lpm_capable) in xhci_set_usb2_hardware_lpm()
4473 return -EPERM; in xhci_set_usb2_hardware_lpm()
4475 if (!udev->parent || udev->parent->parent || in xhci_set_usb2_hardware_lpm()
4476 udev->descriptor.bDeviceClass == USB_CLASS_HUB) in xhci_set_usb2_hardware_lpm()
4477 return -EPERM; in xhci_set_usb2_hardware_lpm()
4479 if (udev->usb2_hw_lpm_capable != 1) in xhci_set_usb2_hardware_lpm()
4480 return -EPERM; in xhci_set_usb2_hardware_lpm()
4482 spin_lock_irqsave(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4484 ports = xhci->usb2_rhub.ports; in xhci_set_usb2_hardware_lpm()
4485 port_num = udev->portnum - 1; in xhci_set_usb2_hardware_lpm()
4486 pm_addr = ports[port_num]->addr + PORTPMSC; in xhci_set_usb2_hardware_lpm()
4488 hlpm_addr = ports[port_num]->addr + PORTHLPMC; in xhci_set_usb2_hardware_lpm()
4490 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n", in xhci_set_usb2_hardware_lpm()
4495 if (udev->usb2_hw_lpm_besl_capable) { in xhci_set_usb2_hardware_lpm()
4498 * systems. See XHCI_DEFAULT_BESL definition in xhci.h in xhci_set_usb2_hardware_lpm()
4500 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); in xhci_set_usb2_hardware_lpm()
4505 hird = udev->l1_params.besl; in xhci_set_usb2_hardware_lpm()
4508 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4510 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx in xhci_set_usb2_hardware_lpm()
4512 * context commands. It is protected by hcd->bandwidth in xhci_set_usb2_hardware_lpm()
4517 mutex_lock(hcd->bandwidth_mutex); in xhci_set_usb2_hardware_lpm()
4518 ret = xhci_change_max_exit_latency(xhci, udev, in xhci_set_usb2_hardware_lpm()
4520 mutex_unlock(hcd->bandwidth_mutex); in xhci_set_usb2_hardware_lpm()
4524 spin_lock_irqsave(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4531 hird = xhci_calculate_hird_besl(xhci, udev); in xhci_set_usb2_hardware_lpm()
4535 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id); in xhci_set_usb2_hardware_lpm()
4547 if (udev->usb2_hw_lpm_besl_capable) { in xhci_set_usb2_hardware_lpm()
4548 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4549 mutex_lock(hcd->bandwidth_mutex); in xhci_set_usb2_hardware_lpm()
4550 xhci_change_max_exit_latency(xhci, udev, 0); in xhci_set_usb2_hardware_lpm()
4551 mutex_unlock(hcd->bandwidth_mutex); in xhci_set_usb2_hardware_lpm()
4552 readl_poll_timeout(ports[port_num]->addr, pm_val, in xhci_set_usb2_hardware_lpm()
4559 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4567 static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port, in xhci_check_usb2_port_capability() argument
4573 for (i = 0; i < xhci->num_ext_caps; i++) { in xhci_check_usb2_port_capability()
4574 if (xhci->ext_caps[i] & capability) { in xhci_check_usb2_port_capability()
4576 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1; in xhci_check_usb2_port_capability()
4577 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]); in xhci_check_usb2_port_capability()
4588 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_update_device() local
4589 int portnum = udev->portnum - 1; in xhci_update_device()
4591 if (hcd->speed >= HCD_USB3 || !udev->lpm_capable) in xhci_update_device()
4594 /* we only support lpm for non-hub device connected to root hub yet */ in xhci_update_device()
4595 if (!udev->parent || udev->parent->parent || in xhci_update_device()
4596 udev->descriptor.bDeviceClass == USB_CLASS_HUB) in xhci_update_device()
4599 if (xhci->hw_lpm_support == 1 && in xhci_update_device()
4601 xhci, portnum, XHCI_HLC)) { in xhci_update_device()
4602 udev->usb2_hw_lpm_capable = 1; in xhci_update_device()
4603 udev->l1_params.timeout = XHCI_L1_TIMEOUT; in xhci_update_device()
4604 udev->l1_params.besl = XHCI_DEFAULT_BESL; in xhci_update_device()
4605 if (xhci_check_usb2_port_capability(xhci, portnum, in xhci_update_device()
4607 udev->usb2_hw_lpm_besl_capable = 1; in xhci_update_device()
4613 /*---------------------- USB 3.0 Link PM functions ------------------------*/
4615 /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4619 return (1ULL << (desc->bInterval - 1)) * 125 * 1000; in xhci_service_interval_to_ns()
4633 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); in xhci_get_timeout_no_hub_lpm()
4634 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); in xhci_get_timeout_no_hub_lpm()
4639 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); in xhci_get_timeout_no_hub_lpm()
4640 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); in xhci_get_timeout_no_hub_lpm()
4645 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n", in xhci_get_timeout_no_hub_lpm()
4654 dev_dbg(&udev->dev, "Device-initiated %s disabled " in xhci_get_timeout_no_hub_lpm()
4658 dev_dbg(&udev->dev, "Device-initiated %s disabled " in xhci_get_timeout_no_hub_lpm()
4665 * - For control endpoints, U1 system exit latency (SEL) * 3
4666 * - For bulk endpoints, U1 SEL * 5
4667 * - For interrupt endpoints:
4668 * - Notification EPs, U1 SEL * 3
4669 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4670 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4683 timeout_ns = udev->u1_params.sel * 3; in xhci_calculate_intel_u1_timeout()
4686 timeout_ns = udev->u1_params.sel * 5; in xhci_calculate_intel_u1_timeout()
4691 timeout_ns = udev->u1_params.sel * 3; in xhci_calculate_intel_u1_timeout()
4699 if (timeout_ns < udev->u1_params.sel * 2) in xhci_calculate_intel_u1_timeout()
4700 timeout_ns = udev->u1_params.sel * 2; in xhci_calculate_intel_u1_timeout()
4709 /* Returns the hub-encoded U1 timeout value. */
4710 static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci, in xhci_calculate_u1_timeout() argument
4718 if (xhci_service_interval_to_ns(desc) <= udev->u1_params.mel) { in xhci_calculate_u1_timeout()
4719 dev_dbg(&udev->dev, "Disable U1, ESIT shorter than exit latency\n"); in xhci_calculate_u1_timeout()
4724 if (xhci->quirks & XHCI_INTEL_HOST) in xhci_calculate_u1_timeout()
4727 timeout_ns = udev->u1_params.sel; in xhci_calculate_u1_timeout()
4738 * USB 3.0 hub, we have to disable hub-initiated U1. in xhci_calculate_u1_timeout()
4742 dev_dbg(&udev->dev, "Hub-initiated U1 disabled " in xhci_calculate_u1_timeout()
4748 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4749 * - largest bInterval of any active periodic endpoint (to avoid going
4751 * - the U2 Exit Latency of the device
4766 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL; in xhci_calculate_intel_u2_timeout()
4773 /* Returns the hub-encoded U2 timeout value. */
4774 static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci, in xhci_calculate_u2_timeout() argument
4782 if (xhci_service_interval_to_ns(desc) <= udev->u2_params.mel) { in xhci_calculate_u2_timeout()
4783 dev_dbg(&udev->dev, "Disable U2, ESIT shorter than exit latency\n"); in xhci_calculate_u2_timeout()
4788 if (xhci->quirks & XHCI_INTEL_HOST) in xhci_calculate_u2_timeout()
4791 timeout_ns = udev->u2_params.sel; in xhci_calculate_u2_timeout()
4796 * USB 3.0 hub, we have to disable hub-initiated U2. in xhci_calculate_u2_timeout()
4800 dev_dbg(&udev->dev, "Hub-initiated U2 disabled " in xhci_calculate_u2_timeout()
4805 static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci, in xhci_call_host_update_timeout_for_endpoint() argument
4812 return xhci_calculate_u1_timeout(xhci, udev, desc); in xhci_call_host_update_timeout_for_endpoint()
4814 return xhci_calculate_u2_timeout(xhci, udev, desc); in xhci_call_host_update_timeout_for_endpoint()
4819 static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci, in xhci_update_timeout_for_endpoint() argument
4827 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev, in xhci_update_timeout_for_endpoint()
4830 /* If we found we can't enable hub-initiated LPM, and in xhci_update_timeout_for_endpoint()
4832 * device-initiated LPM as well, then we will disable LPM in xhci_update_timeout_for_endpoint()
4837 return -E2BIG; in xhci_update_timeout_for_endpoint()
4844 static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci, in xhci_update_timeout_for_interface() argument
4852 for (j = 0; j < alt->desc.bNumEndpoints; j++) { in xhci_update_timeout_for_interface()
4853 if (xhci_update_timeout_for_endpoint(xhci, udev, in xhci_update_timeout_for_interface()
4854 &alt->endpoint[j].desc, state, timeout)) in xhci_update_timeout_for_interface()
4855 return -E2BIG; in xhci_update_timeout_for_interface()
4870 for (parent = udev->parent, num_hubs = 0; parent->parent; in xhci_check_intel_tier_policy()
4871 parent = parent->parent) in xhci_check_intel_tier_policy()
4877 dev_dbg(&udev->dev, "Disabling U1 link state for device" in xhci_check_intel_tier_policy()
4878 " below second-tier hub.\n"); in xhci_check_intel_tier_policy()
4879 dev_dbg(&udev->dev, "Plug device into first-tier hub " in xhci_check_intel_tier_policy()
4881 return -E2BIG; in xhci_check_intel_tier_policy()
4884 static int xhci_check_tier_policy(struct xhci_hcd *xhci, in xhci_check_tier_policy() argument
4888 if (xhci->quirks & XHCI_INTEL_HOST) in xhci_check_tier_policy()
4895 * If the tier check or timeout setting functions return with a non-zero exit
4902 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_calculate_lpm_timeout() local
4913 dev_warn(&udev->dev, "Can't enable unknown link state %i\n", in xhci_calculate_lpm_timeout()
4918 if (xhci_check_tier_policy(xhci, udev, state) < 0) in xhci_calculate_lpm_timeout()
4924 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc, in xhci_calculate_lpm_timeout()
4928 config = udev->actconfig; in xhci_calculate_lpm_timeout()
4932 for (i = 0; i < config->desc.bNumInterfaces; i++) { in xhci_calculate_lpm_timeout()
4934 struct usb_interface *intf = config->interface[i]; in xhci_calculate_lpm_timeout()
4939 /* Check if any currently bound drivers want hub-initiated LPM in xhci_calculate_lpm_timeout()
4942 if (intf->dev.driver) { in xhci_calculate_lpm_timeout()
4943 driver = to_usb_driver(intf->dev.driver); in xhci_calculate_lpm_timeout()
4944 if (driver && driver->disable_hub_initiated_lpm) { in xhci_calculate_lpm_timeout()
4945 dev_dbg(&udev->dev, "Hub-initiated %s disabled at request of driver %s\n", in xhci_calculate_lpm_timeout()
4946 state_name, driver->name); in xhci_calculate_lpm_timeout()
4955 if (!intf->cur_altsetting) in xhci_calculate_lpm_timeout()
4958 if (xhci_update_timeout_for_interface(xhci, udev, in xhci_calculate_lpm_timeout()
4959 intf->cur_altsetting, in xhci_calculate_lpm_timeout()
4991 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) || in calculate_max_exit_latency()
4993 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000); in calculate_max_exit_latency()
4994 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) || in calculate_max_exit_latency()
4996 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000); in calculate_max_exit_latency()
5002 /* xHCI host controller max exit latency field is only 16 bits wide. */ in calculate_max_exit_latency()
5004 dev_warn(&udev->dev, "Link PM max exit latency of %lluus " in calculate_max_exit_latency()
5006 return -E2BIG; in calculate_max_exit_latency()
5011 /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
5015 struct xhci_hcd *xhci; in xhci_enable_usb3_lpm_timeout() local
5020 xhci = hcd_to_xhci(hcd); in xhci_enable_usb3_lpm_timeout()
5021 /* The LPM timeout values are pretty host-controller specific, so don't in xhci_enable_usb3_lpm_timeout()
5022 * enable hub-initiated timeouts unless the vendor has provided in xhci_enable_usb3_lpm_timeout()
5025 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) || in xhci_enable_usb3_lpm_timeout()
5026 !xhci->devs[udev->slot_id]) in xhci_enable_usb3_lpm_timeout()
5037 ret = xhci_change_max_exit_latency(xhci, udev, mel); in xhci_enable_usb3_lpm_timeout()
5046 struct xhci_hcd *xhci; in xhci_disable_usb3_lpm_timeout() local
5049 xhci = hcd_to_xhci(hcd); in xhci_disable_usb3_lpm_timeout()
5050 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) || in xhci_disable_usb3_lpm_timeout()
5051 !xhci->devs[udev->slot_id]) in xhci_disable_usb3_lpm_timeout()
5055 return xhci_change_max_exit_latency(xhci, udev, mel); in xhci_disable_usb3_lpm_timeout()
5083 /*-------------------------------------------------------------------------*/
5091 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_update_hub_device() local
5101 if (!hdev->parent) in xhci_update_hub_device()
5104 vdev = xhci->devs[hdev->slot_id]; in xhci_update_hub_device()
5106 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n"); in xhci_update_hub_device()
5107 return -EINVAL; in xhci_update_hub_device()
5110 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags); in xhci_update_hub_device()
5112 return -ENOMEM; in xhci_update_hub_device()
5114 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx); in xhci_update_hub_device()
5116 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_update_hub_device()
5118 xhci_free_command(xhci, config_cmd); in xhci_update_hub_device()
5119 return -ENOMEM; in xhci_update_hub_device()
5122 spin_lock_irqsave(&xhci->lock, flags); in xhci_update_hub_device()
5123 if (hdev->speed == USB_SPEED_HIGH && in xhci_update_hub_device()
5124 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) { in xhci_update_hub_device()
5125 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n"); in xhci_update_hub_device()
5126 xhci_free_command(xhci, config_cmd); in xhci_update_hub_device()
5127 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_update_hub_device()
5128 return -ENOMEM; in xhci_update_hub_device()
5131 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx); in xhci_update_hub_device()
5132 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_update_hub_device()
5133 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx); in xhci_update_hub_device()
5134 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB); in xhci_update_hub_device()
5137 * but it may be already set to 1 when setup an xHCI virtual in xhci_update_hub_device()
5140 if (tt->multi) in xhci_update_hub_device()
5141 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); in xhci_update_hub_device()
5142 else if (hdev->speed == USB_SPEED_FULL) in xhci_update_hub_device()
5143 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT); in xhci_update_hub_device()
5145 if (xhci->hci_version > 0x95) { in xhci_update_hub_device()
5146 xhci_dbg(xhci, "xHCI version %x needs hub " in xhci_update_hub_device()
5148 (unsigned int) xhci->hci_version); in xhci_update_hub_device()
5149 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild)); in xhci_update_hub_device()
5150 /* Set TT think time - convert from ns to FS bit times. in xhci_update_hub_device()
5154 * xHCI 1.0: this field shall be 0 if the device is not a in xhci_update_hub_device()
5155 * High-spped hub. in xhci_update_hub_device()
5157 think_time = tt->think_time; in xhci_update_hub_device()
5159 think_time = (think_time / 666) - 1; in xhci_update_hub_device()
5160 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH) in xhci_update_hub_device()
5161 slot_ctx->tt_info |= in xhci_update_hub_device()
5164 xhci_dbg(xhci, "xHCI version %x doesn't need hub " in xhci_update_hub_device()
5166 (unsigned int) xhci->hci_version); in xhci_update_hub_device()
5168 slot_ctx->dev_state = 0; in xhci_update_hub_device()
5169 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_update_hub_device()
5171 xhci_dbg(xhci, "Set up %s for hub device.\n", in xhci_update_hub_device()
5172 (xhci->hci_version > 0x95) ? in xhci_update_hub_device()
5178 if (xhci->hci_version > 0x95) in xhci_update_hub_device()
5179 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, in xhci_update_hub_device()
5182 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, in xhci_update_hub_device()
5185 xhci_free_command(xhci, config_cmd); in xhci_update_hub_device()
5191 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_get_frame() local
5193 return readl(&xhci->run_regs->microframe_index) >> 3; in xhci_get_frame()
5198 struct xhci_hcd *xhci; in xhci_gen_setup() local
5203 struct device *dev = hcd->self.sysdev; in xhci_gen_setup()
5207 /* Accept arbitrarily long scatter-gather lists */ in xhci_gen_setup()
5208 hcd->self.sg_tablesize = ~0; in xhci_gen_setup()
5211 hcd->self.no_sg_constraint = 1; in xhci_gen_setup()
5213 /* XHCI controllers don't stop the ep queue on short packets :| */ in xhci_gen_setup()
5214 hcd->self.no_stop_on_short = 1; in xhci_gen_setup()
5216 xhci = hcd_to_xhci(hcd); in xhci_gen_setup()
5219 xhci->main_hcd = hcd; in xhci_gen_setup()
5220 xhci->usb2_rhub.hcd = hcd; in xhci_gen_setup()
5222 * The xHCI driver will register the USB 3.0 roothub. in xhci_gen_setup()
5224 hcd->speed = HCD_USB2; in xhci_gen_setup()
5225 hcd->self.root_hub->speed = USB_SPEED_HIGH; in xhci_gen_setup()
5227 * USB 2.0 roothub under xHCI has an integrated TT, in xhci_gen_setup()
5231 hcd->has_tt = 1; in xhci_gen_setup()
5234 * Early xHCI 1.1 spec did not mention USB 3.1 capable hosts in xhci_gen_setup()
5236 * is a two digit BCD containig minor and sub-minor numbers. in xhci_gen_setup()
5237 * This was later clarified in xHCI 1.2. in xhci_gen_setup()
5242 if (xhci->usb3_rhub.min_rev == 0x1) in xhci_gen_setup()
5245 minor_rev = xhci->usb3_rhub.min_rev / 0x10; in xhci_gen_setup()
5249 hcd->speed = HCD_USB32; in xhci_gen_setup()
5250 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS; in xhci_gen_setup()
5251 hcd->self.root_hub->rx_lanes = 2; in xhci_gen_setup()
5252 hcd->self.root_hub->tx_lanes = 2; in xhci_gen_setup()
5253 hcd->self.root_hub->ssp_rate = USB_SSP_GEN_2x2; in xhci_gen_setup()
5256 hcd->speed = HCD_USB31; in xhci_gen_setup()
5257 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS; in xhci_gen_setup()
5258 hcd->self.root_hub->ssp_rate = USB_SSP_GEN_2x1; in xhci_gen_setup()
5261 xhci_info(xhci, "Host supports USB 3.%x %sSuperSpeed\n", in xhci_gen_setup()
5265 xhci->usb3_rhub.hcd = hcd; in xhci_gen_setup()
5266 /* xHCI private pointer was set in xhci_pci_probe for the second in xhci_gen_setup()
5272 mutex_init(&xhci->mutex); in xhci_gen_setup()
5273 xhci->cap_regs = hcd->regs; in xhci_gen_setup()
5274 xhci->op_regs = hcd->regs + in xhci_gen_setup()
5275 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)); in xhci_gen_setup()
5276 xhci->run_regs = hcd->regs + in xhci_gen_setup()
5277 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK); in xhci_gen_setup()
5278 /* Cache read-only capability registers */ in xhci_gen_setup()
5279 xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1); in xhci_gen_setup()
5280 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2); in xhci_gen_setup()
5281 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3); in xhci_gen_setup()
5282 xhci->hcc_params = readl(&xhci->cap_regs->hc_capbase); in xhci_gen_setup()
5283 xhci->hci_version = HC_VERSION(xhci->hcc_params); in xhci_gen_setup()
5284 xhci->hcc_params = readl(&xhci->cap_regs->hcc_params); in xhci_gen_setup()
5285 if (xhci->hci_version > 0x100) in xhci_gen_setup()
5286 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2); in xhci_gen_setup()
5288 xhci->quirks |= quirks; in xhci_gen_setup()
5290 get_quirks(dev, xhci); in xhci_gen_setup()
5292 /* In xhci controllers which follow xhci 1.0 spec gives a spurious in xhci_gen_setup()
5296 if (xhci->hci_version > 0x96) in xhci_gen_setup()
5297 xhci->quirks |= XHCI_SPURIOUS_SUCCESS; in xhci_gen_setup()
5300 retval = xhci_halt(xhci); in xhci_gen_setup()
5304 xhci_zero_64b_regs(xhci); in xhci_gen_setup()
5306 xhci_dbg(xhci, "Resetting HCD\n"); in xhci_gen_setup()
5308 retval = xhci_reset(xhci); in xhci_gen_setup()
5311 xhci_dbg(xhci, "Reset complete\n"); in xhci_gen_setup()
5314 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0) in xhci_gen_setup()
5315 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit in xhci_gen_setup()
5317 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev, in xhci_gen_setup()
5320 if (xhci->quirks & XHCI_NO_64BIT_SUPPORT) in xhci_gen_setup()
5321 xhci->hcc_params &= ~BIT(0); in xhci_gen_setup()
5323 /* Set dma_mask and coherent_dma_mask to 64-bits, in xhci_gen_setup()
5324 * if xHC supports 64-bit addressing */ in xhci_gen_setup()
5325 if (HCC_64BIT_ADDR(xhci->hcc_params) && in xhci_gen_setup()
5327 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n"); in xhci_gen_setup()
5331 * This is to avoid error in cases where a 32-bit USB in xhci_gen_setup()
5332 * controller is used on a 64-bit capable system. in xhci_gen_setup()
5337 xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n"); in xhci_gen_setup()
5341 xhci_dbg(xhci, "Calling HCD init\n"); in xhci_gen_setup()
5346 xhci_dbg(xhci, "Called HCD init\n"); in xhci_gen_setup()
5348 xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%016llx\n", in xhci_gen_setup()
5349 xhci->hcc_params, xhci->hci_version, xhci->quirks); in xhci_gen_setup()
5358 struct xhci_hcd *xhci; in xhci_clear_tt_buffer_complete() local
5364 xhci = hcd_to_xhci(hcd); in xhci_clear_tt_buffer_complete()
5366 spin_lock_irqsave(&xhci->lock, flags); in xhci_clear_tt_buffer_complete()
5367 udev = (struct usb_device *)ep->hcpriv; in xhci_clear_tt_buffer_complete()
5368 slot_id = udev->slot_id; in xhci_clear_tt_buffer_complete()
5369 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_clear_tt_buffer_complete()
5371 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_CLEARING_TT; in xhci_clear_tt_buffer_complete()
5372 xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index); in xhci_clear_tt_buffer_complete()
5373 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_clear_tt_buffer_complete()
5377 .description = "xhci-hcd",
5378 .product_desc = "xHCI Host Controller",
5382 * generic hardware linkage
5448 /* Copy the generic table to drv then apply the overrides */ in xhci_init_driver()
5452 drv->hcd_priv_size += over->extra_priv_size; in xhci_init_driver()
5453 if (over->reset) in xhci_init_driver()
5454 drv->reset = over->reset; in xhci_init_driver()
5455 if (over->start) in xhci_init_driver()
5456 drv->start = over->start; in xhci_init_driver()
5457 if (over->add_endpoint) in xhci_init_driver()
5458 drv->add_endpoint = over->add_endpoint; in xhci_init_driver()
5459 if (over->drop_endpoint) in xhci_init_driver()
5460 drv->drop_endpoint = over->drop_endpoint; in xhci_init_driver()
5461 if (over->check_bandwidth) in xhci_init_driver()
5462 drv->check_bandwidth = over->check_bandwidth; in xhci_init_driver()
5463 if (over->reset_bandwidth) in xhci_init_driver()
5464 drv->reset_bandwidth = over->reset_bandwidth; in xhci_init_driver()
5494 return -ENODEV; in xhci_hcd_init()