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

1 // SPDX-License-Identifier: GPL-2.0
3 * xHCI host controller driver
21 #include <linux/dma-mapping.h>
23 #include "xhci.h"
24 #include "xhci-trace.h"
25 #include "xhci-debugfs.h"
26 #include "xhci-dbgcap.h"
44 struct xhci_segment *seg = ring->first_seg; in td_on_ring()
46 if (!td || !td->start_seg) in td_on_ring()
49 if (seg == td->start_seg) in td_on_ring()
51 seg = seg->next; in td_on_ring()
52 } while (seg && seg != ring->first_seg); in td_on_ring()
58 * xhci_handshake - spin reading hc until handshake completes or fails
68 * hardware flakeout), or the register reads as all-ones (hardware removed).
80 return -ENODEV; in xhci_handshake()
86 * Disable interrupts and begin the xHCI halting process.
88 void xhci_quiesce(struct xhci_hcd *xhci) in xhci_quiesce() argument
95 halted = readl(&xhci->op_regs->status) & STS_HALT; in xhci_quiesce()
99 cmd = readl(&xhci->op_regs->command); in xhci_quiesce()
101 writel(cmd, &xhci->op_regs->command); in xhci_quiesce()
112 int xhci_halt(struct xhci_hcd *xhci) in xhci_halt() argument
116 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC"); in xhci_halt()
117 xhci_quiesce(xhci); in xhci_halt()
119 ret = xhci_handshake(&xhci->op_regs->status, in xhci_halt()
122 xhci_warn(xhci, "Host halt failed, %d\n", ret); in xhci_halt()
126 xhci->xhc_state |= XHCI_STATE_HALTED; in xhci_halt()
127 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; in xhci_halt()
135 int xhci_start(struct xhci_hcd *xhci) in xhci_start() argument
140 temp = readl(&xhci->op_regs->command); in xhci_start()
142 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.", in xhci_start()
144 writel(temp, &xhci->op_regs->command); in xhci_start()
150 ret = xhci_handshake(&xhci->op_regs->status, in xhci_start()
152 if (ret == -ETIMEDOUT) in xhci_start()
153 xhci_err(xhci, "Host took too long to start, " in xhci_start()
158 xhci->xhc_state = 0; in xhci_start()
159 xhci->run_graceperiod = jiffies + msecs_to_jiffies(500); in xhci_start()
172 int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us) in xhci_reset() argument
178 state = readl(&xhci->op_regs->status); in xhci_reset()
181 xhci_warn(xhci, "Host not accessible, reset failed.\n"); in xhci_reset()
182 return -ENODEV; in xhci_reset()
186 xhci_warn(xhci, "Host controller not halted, aborting reset.\n"); in xhci_reset()
190 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC"); in xhci_reset()
191 command = readl(&xhci->op_regs->command); in xhci_reset()
193 writel(command, &xhci->op_regs->command); in xhci_reset()
195 /* Existing Intel xHCI controllers require a delay of 1 mS, in xhci_reset()
202 if (xhci->quirks & XHCI_INTEL_HOST) in xhci_reset()
205 ret = xhci_handshake(&xhci->op_regs->command, CMD_RESET, 0, timeout_us); in xhci_reset()
209 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL) in xhci_reset()
210 usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci)->self.controller)); in xhci_reset()
212 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_reset()
215 * xHCI cannot write to any doorbells or operational registers other in xhci_reset()
218 ret = xhci_handshake(&xhci->op_regs->status, STS_CNR, 0, timeout_us); in xhci_reset()
220 xhci->usb2_rhub.bus_state.port_c_suspend = 0; in xhci_reset()
221 xhci->usb2_rhub.bus_state.suspended_ports = 0; in xhci_reset()
222 xhci->usb2_rhub.bus_state.resuming_ports = 0; in xhci_reset()
223 xhci->usb3_rhub.bus_state.port_c_suspend = 0; in xhci_reset()
224 xhci->usb3_rhub.bus_state.suspended_ports = 0; in xhci_reset()
225 xhci->usb3_rhub.bus_state.resuming_ports = 0; in xhci_reset()
230 static void xhci_zero_64b_regs(struct xhci_hcd *xhci) in xhci_zero_64b_regs() argument
232 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; in xhci_zero_64b_regs()
253 if (!(xhci->quirks & XHCI_ZERO_64B_REGS) || !domain || in xhci_zero_64b_regs()
254 domain->type == IOMMU_DOMAIN_IDENTITY) in xhci_zero_64b_regs()
257 xhci_info(xhci, "Zeroing 64bit base registers, expecting fault\n"); in xhci_zero_64b_regs()
260 val = readl(&xhci->op_regs->command); in xhci_zero_64b_regs()
262 writel(val, &xhci->op_regs->command); in xhci_zero_64b_regs()
265 val = readl(&xhci->op_regs->status); in xhci_zero_64b_regs()
267 writel(val, &xhci->op_regs->status); in xhci_zero_64b_regs()
270 val = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); in xhci_zero_64b_regs()
272 xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr); in xhci_zero_64b_regs()
273 val = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); in xhci_zero_64b_regs()
275 xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring); in xhci_zero_64b_regs()
277 intrs = min_t(u32, HCS_MAX_INTRS(xhci->hcs_params1), in xhci_zero_64b_regs()
278 ARRAY_SIZE(xhci->run_regs->ir_set)); in xhci_zero_64b_regs()
283 ir = &xhci->run_regs->ir_set[i]; in xhci_zero_64b_regs()
284 val = xhci_read_64(xhci, &ir->erst_base); in xhci_zero_64b_regs()
286 xhci_write_64(xhci, 0, &ir->erst_base); in xhci_zero_64b_regs()
287 val= xhci_read_64(xhci, &ir->erst_dequeue); in xhci_zero_64b_regs()
289 xhci_write_64(xhci, 0, &ir->erst_dequeue); in xhci_zero_64b_regs()
293 err = xhci_handshake(&xhci->op_regs->status, in xhci_zero_64b_regs()
297 xhci_info(xhci, "Fault detected\n"); in xhci_zero_64b_regs()
304 if (!ir || !ir->ir_set) in xhci_enable_interrupter()
305 return -EINVAL; in xhci_enable_interrupter()
307 iman = readl(&ir->ir_set->irq_pending); in xhci_enable_interrupter()
308 writel(ER_IRQ_ENABLE(iman), &ir->ir_set->irq_pending); in xhci_enable_interrupter()
317 if (!ir || !ir->ir_set) in xhci_disable_interrupter()
318 return -EINVAL; in xhci_disable_interrupter()
320 iman = readl(&ir->ir_set->irq_pending); in xhci_disable_interrupter()
321 writel(ER_IRQ_DISABLE(iman), &ir->ir_set->irq_pending); in xhci_disable_interrupter()
328 struct xhci_hcd *xhci; in compliance_mode_recovery() local
334 xhci = from_timer(xhci, t, comp_mode_recovery_timer); in compliance_mode_recovery()
335 rhub = &xhci->usb3_rhub; in compliance_mode_recovery()
336 hcd = rhub->hcd; in compliance_mode_recovery()
341 for (i = 0; i < rhub->num_ports; i++) { in compliance_mode_recovery()
342 temp = readl(rhub->ports[i]->addr); in compliance_mode_recovery()
348 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in compliance_mode_recovery()
349 "Compliance mode detected->port %d", in compliance_mode_recovery()
351 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in compliance_mode_recovery()
354 if (hcd->state == HC_STATE_SUSPENDED) in compliance_mode_recovery()
361 if (xhci->port_status_u0 != ((1 << rhub->num_ports) - 1)) in compliance_mode_recovery()
362 mod_timer(&xhci->comp_mode_recovery_timer, in compliance_mode_recovery()
367 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
373 * status event is generated when entering compliance mode (per xhci spec),
376 static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci) in compliance_mode_recovery_timer_init() argument
378 xhci->port_status_u0 = 0; in compliance_mode_recovery_timer_init()
379 timer_setup(&xhci->comp_mode_recovery_timer, compliance_mode_recovery, in compliance_mode_recovery_timer_init()
381 xhci->comp_mode_recovery_timer.expires = jiffies + in compliance_mode_recovery_timer_init()
384 add_timer(&xhci->comp_mode_recovery_timer); in compliance_mode_recovery_timer_init()
385 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in compliance_mode_recovery_timer_init()
391 * USB3.0 re-driver and that need the Compliance Mode Quirk.
393 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
404 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard"))) in xhci_compliance_mode_recovery_timer_quirk_check()
416 static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci) in xhci_all_ports_seen_u0() argument
418 return (xhci->port_status_u0 == ((1 << xhci->usb3_rhub.num_ports) - 1)); in xhci_all_ports_seen_u0()
423 * Initialize memory for HCD and xHC (one-time init).
431 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_init() local
434 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init"); in xhci_init()
435 spin_lock_init(&xhci->lock); in xhci_init()
436 if (xhci->hci_version == 0x95 && link_quirk) { in xhci_init()
437 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_init()
439 xhci->quirks |= XHCI_LINK_TRB_QUIRK; in xhci_init()
441 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_init()
442 "xHCI doesn't need link TRB QUIRK"); in xhci_init()
444 retval = xhci_mem_init(xhci, GFP_KERNEL); in xhci_init()
445 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init"); in xhci_init()
449 xhci->quirks |= XHCI_COMP_MODE_QUIRK; in xhci_init()
450 compliance_mode_recovery_timer_init(xhci); in xhci_init()
456 /*-------------------------------------------------------------------------*/
458 static int xhci_run_finished(struct xhci_hcd *xhci) in xhci_run_finished() argument
460 struct xhci_interrupter *ir = xhci->interrupter; in xhci_run_finished()
465 * Enable interrupts before starting the host (xhci 4.2 and 5.5.2). in xhci_run_finished()
468 spin_lock_irqsave(&xhci->lock, flags); in xhci_run_finished()
470 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Enable interrupts"); in xhci_run_finished()
471 temp = readl(&xhci->op_regs->command); in xhci_run_finished()
473 writel(temp, &xhci->op_regs->command); in xhci_run_finished()
475 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Enable primary interrupter"); in xhci_run_finished()
478 if (xhci_start(xhci)) { in xhci_run_finished()
479 xhci_halt(xhci); in xhci_run_finished()
480 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_run_finished()
481 return -ENODEV; in xhci_run_finished()
484 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING; in xhci_run_finished()
486 if (xhci->quirks & XHCI_NEC_HOST) in xhci_run_finished()
487 xhci_ring_cmd_db(xhci); in xhci_run_finished()
489 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_run_finished()
504 * Setup MSI-X vectors and enable interrupts.
511 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_run() local
512 struct xhci_interrupter *ir = xhci->interrupter; in xhci_run()
513 /* Start the xHCI host controller running only after the USB 2.0 roothub in xhci_run()
517 hcd->uses_new_polling = 1; in xhci_run()
519 return xhci_run_finished(xhci); in xhci_run()
521 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run"); in xhci_run()
523 temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); in xhci_run()
525 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run()
528 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run()
530 temp = readl(&ir->ir_set->irq_control); in xhci_run()
532 temp |= (xhci->imod_interval / 250) & ER_IRQ_INTERVAL_MASK; in xhci_run()
533 writel(temp, &ir->ir_set->irq_control); in xhci_run()
535 if (xhci->quirks & XHCI_NEC_HOST) { in xhci_run()
538 command = xhci_alloc_command(xhci, false, GFP_KERNEL); in xhci_run()
540 return -ENOMEM; in xhci_run()
542 ret = xhci_queue_vendor_command(xhci, command, 0, 0, 0, in xhci_run()
545 xhci_free_command(xhci, command); in xhci_run()
547 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run()
550 xhci_create_dbc_dev(xhci); in xhci_run()
552 xhci_debugfs_init(xhci); in xhci_run()
554 if (xhci_has_one_roothub(xhci)) in xhci_run()
555 return xhci_run_finished(xhci); in xhci_run()
557 set_bit(HCD_FLAG_DEFER_RH_REGISTER, &hcd->flags); in xhci_run()
564 * Stop xHCI driver.
575 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_stop() local
576 struct xhci_interrupter *ir = xhci->interrupter; in xhci_stop()
578 mutex_lock(&xhci->mutex); in xhci_stop()
582 mutex_unlock(&xhci->mutex); in xhci_stop()
586 xhci_remove_dbc_dev(xhci); in xhci_stop()
588 spin_lock_irq(&xhci->lock); in xhci_stop()
589 xhci->xhc_state |= XHCI_STATE_HALTED; in xhci_stop()
590 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; in xhci_stop()
591 xhci_halt(xhci); in xhci_stop()
592 xhci_reset(xhci, XHCI_RESET_SHORT_USEC); in xhci_stop()
593 spin_unlock_irq(&xhci->lock); in xhci_stop()
596 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && in xhci_stop()
597 (!(xhci_all_ports_seen_u0(xhci)))) { in xhci_stop()
598 del_timer_sync(&xhci->comp_mode_recovery_timer); in xhci_stop()
599 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_stop()
604 if (xhci->quirks & XHCI_AMD_PLL_FIX) in xhci_stop()
607 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_stop()
609 temp = readl(&xhci->op_regs->status); in xhci_stop()
610 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status); in xhci_stop()
613 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory"); in xhci_stop()
614 xhci_mem_cleanup(xhci); in xhci_stop()
615 xhci_debugfs_exit(xhci); in xhci_stop()
616 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_stop()
617 "xhci_stop completed - status = %x", in xhci_stop()
618 readl(&xhci->op_regs->status)); in xhci_stop()
619 mutex_unlock(&xhci->mutex); in xhci_stop()
624 * Shutdown HC (not bus-specific)
634 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_shutdown() local
636 if (xhci->quirks & XHCI_SPURIOUS_REBOOT) in xhci_shutdown()
637 usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev)); in xhci_shutdown()
640 xhci_dbg(xhci, "%s: stopping usb%d port polling.\n", in xhci_shutdown()
641 __func__, hcd->self.busnum); in xhci_shutdown()
642 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_shutdown()
643 del_timer_sync(&hcd->rh_timer); in xhci_shutdown()
645 if (xhci->shared_hcd) { in xhci_shutdown()
646 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_shutdown()
647 del_timer_sync(&xhci->shared_hcd->rh_timer); in xhci_shutdown()
650 spin_lock_irq(&xhci->lock); in xhci_shutdown()
651 xhci_halt(xhci); in xhci_shutdown()
655 * firmware delay in ADL-P PCH if port are left in U3 at shutdown in xhci_shutdown()
657 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP || in xhci_shutdown()
658 xhci->quirks & XHCI_RESET_TO_DEFAULT) in xhci_shutdown()
659 xhci_reset(xhci, XHCI_RESET_SHORT_USEC); in xhci_shutdown()
661 spin_unlock_irq(&xhci->lock); in xhci_shutdown()
663 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_shutdown()
664 "xhci_shutdown completed - status = %x", in xhci_shutdown()
665 readl(&xhci->op_regs->status)); in xhci_shutdown()
670 static void xhci_save_registers(struct xhci_hcd *xhci) in xhci_save_registers() argument
672 struct xhci_interrupter *ir = xhci->interrupter; in xhci_save_registers()
674 xhci->s3.command = readl(&xhci->op_regs->command); in xhci_save_registers()
675 xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification); in xhci_save_registers()
676 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); in xhci_save_registers()
677 xhci->s3.config_reg = readl(&xhci->op_regs->config_reg); in xhci_save_registers()
682 ir->s3_erst_size = readl(&ir->ir_set->erst_size); in xhci_save_registers()
683 ir->s3_erst_base = xhci_read_64(xhci, &ir->ir_set->erst_base); in xhci_save_registers()
684 ir->s3_erst_dequeue = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); in xhci_save_registers()
685 ir->s3_irq_pending = readl(&ir->ir_set->irq_pending); in xhci_save_registers()
686 ir->s3_irq_control = readl(&ir->ir_set->irq_control); in xhci_save_registers()
689 static void xhci_restore_registers(struct xhci_hcd *xhci) in xhci_restore_registers() argument
691 struct xhci_interrupter *ir = xhci->interrupter; in xhci_restore_registers()
693 writel(xhci->s3.command, &xhci->op_regs->command); in xhci_restore_registers()
694 writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification); in xhci_restore_registers()
695 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr); in xhci_restore_registers()
696 writel(xhci->s3.config_reg, &xhci->op_regs->config_reg); in xhci_restore_registers()
697 writel(ir->s3_erst_size, &ir->ir_set->erst_size); in xhci_restore_registers()
698 xhci_write_64(xhci, ir->s3_erst_base, &ir->ir_set->erst_base); in xhci_restore_registers()
699 xhci_write_64(xhci, ir->s3_erst_dequeue, &ir->ir_set->erst_dequeue); in xhci_restore_registers()
700 writel(ir->s3_irq_pending, &ir->ir_set->irq_pending); in xhci_restore_registers()
701 writel(ir->s3_irq_control, &ir->ir_set->irq_control); in xhci_restore_registers()
704 static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci) in xhci_set_cmd_ring_deq() argument
709 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); in xhci_set_cmd_ring_deq()
711 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, in xhci_set_cmd_ring_deq()
712 xhci->cmd_ring->dequeue) & in xhci_set_cmd_ring_deq()
714 xhci->cmd_ring->cycle_state; in xhci_set_cmd_ring_deq()
715 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_set_cmd_ring_deq()
718 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring); in xhci_set_cmd_ring_deq()
725 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
728 * middle of the ring (TRBs are 16-byte aligned).
730 static void xhci_clear_command_ring(struct xhci_hcd *xhci) in xhci_clear_command_ring() argument
735 ring = xhci->cmd_ring; in xhci_clear_command_ring()
736 seg = ring->deq_seg; in xhci_clear_command_ring()
738 memset(seg->trbs, 0, in xhci_clear_command_ring()
739 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1)); in xhci_clear_command_ring()
740 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &= in xhci_clear_command_ring()
742 seg = seg->next; in xhci_clear_command_ring()
743 } while (seg != ring->deq_seg); in xhci_clear_command_ring()
746 ring->deq_seg = ring->first_seg; in xhci_clear_command_ring()
747 ring->dequeue = ring->first_seg->trbs; in xhci_clear_command_ring()
748 ring->enq_seg = ring->deq_seg; in xhci_clear_command_ring()
749 ring->enqueue = ring->dequeue; in xhci_clear_command_ring()
751 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1; in xhci_clear_command_ring()
756 ring->cycle_state = 1; in xhci_clear_command_ring()
760 * Yes, this will need to be re-written after resume, but we're paranoid in xhci_clear_command_ring()
765 xhci_set_cmd_ring_deq(xhci); in xhci_clear_command_ring()
773 * Internal wake causes immediate xHCI wake after suspend. PORT_CSC write done
777 static void xhci_disable_hub_port_wake(struct xhci_hcd *xhci, in xhci_disable_hub_port_wake() argument
785 spin_lock_irqsave(&xhci->lock, flags); in xhci_disable_hub_port_wake()
787 for (i = 0; i < rhub->num_ports; i++) { in xhci_disable_hub_port_wake()
788 portsc = readl(rhub->ports[i]->addr); in xhci_disable_hub_port_wake()
801 writel(t2, rhub->ports[i]->addr); in xhci_disable_hub_port_wake()
802 xhci_dbg(xhci, "config port %d-%d wake bits, portsc: 0x%x, write: 0x%x\n", in xhci_disable_hub_port_wake()
803 rhub->hcd->self.busnum, i + 1, portsc, t2); in xhci_disable_hub_port_wake()
806 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_hub_port_wake()
809 static bool xhci_pending_portevent(struct xhci_hcd *xhci) in xhci_pending_portevent() argument
816 status = readl(&xhci->op_regs->status); in xhci_pending_portevent()
822 * being written to the Event Ring. See note in xhci 1.1 section 4.19.2. in xhci_pending_portevent()
825 port_index = xhci->usb2_rhub.num_ports; in xhci_pending_portevent()
826 ports = xhci->usb2_rhub.ports; in xhci_pending_portevent()
827 while (port_index--) { in xhci_pending_portevent()
828 portsc = readl(ports[port_index]->addr); in xhci_pending_portevent()
833 port_index = xhci->usb3_rhub.num_ports; in xhci_pending_portevent()
834 ports = xhci->usb3_rhub.ports; in xhci_pending_portevent()
835 while (port_index--) { in xhci_pending_portevent()
836 portsc = readl(ports[port_index]->addr); in xhci_pending_portevent()
845 * Stop HC (not bus-specific)
850 int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup) in xhci_suspend() argument
854 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_suspend()
858 if (!hcd->state) in xhci_suspend()
861 if (hcd->state != HC_STATE_SUSPENDED || in xhci_suspend()
862 (xhci->shared_hcd && xhci->shared_hcd->state != HC_STATE_SUSPENDED)) in xhci_suspend()
863 return -EINVAL; in xhci_suspend()
866 xhci_disable_hub_port_wake(xhci, &xhci->usb3_rhub, do_wakeup); in xhci_suspend()
867 xhci_disable_hub_port_wake(xhci, &xhci->usb2_rhub, do_wakeup); in xhci_suspend()
872 xhci_dbc_suspend(xhci); in xhci_suspend()
875 xhci_dbg(xhci, "%s: stopping usb%d port polling.\n", in xhci_suspend()
876 __func__, hcd->self.busnum); in xhci_suspend()
877 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_suspend()
878 del_timer_sync(&hcd->rh_timer); in xhci_suspend()
879 if (xhci->shared_hcd) { in xhci_suspend()
880 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_suspend()
881 del_timer_sync(&xhci->shared_hcd->rh_timer); in xhci_suspend()
884 if (xhci->quirks & XHCI_SUSPEND_DELAY) in xhci_suspend()
887 spin_lock_irq(&xhci->lock); in xhci_suspend()
888 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); in xhci_suspend()
889 if (xhci->shared_hcd) in xhci_suspend()
890 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags); in xhci_suspend()
895 command = readl(&xhci->op_regs->command); in xhci_suspend()
897 writel(command, &xhci->op_regs->command); in xhci_suspend()
900 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1; in xhci_suspend()
902 if (xhci_handshake(&xhci->op_regs->status, in xhci_suspend()
904 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n"); in xhci_suspend()
905 spin_unlock_irq(&xhci->lock); in xhci_suspend()
906 return -ETIMEDOUT; in xhci_suspend()
908 xhci_clear_command_ring(xhci); in xhci_suspend()
911 xhci_save_registers(xhci); in xhci_suspend()
914 command = readl(&xhci->op_regs->command); in xhci_suspend()
916 writel(command, &xhci->op_regs->command); in xhci_suspend()
917 xhci->broken_suspend = 0; in xhci_suspend()
918 if (xhci_handshake(&xhci->op_regs->status, in xhci_suspend()
926 * if SRE and HCE bits are not set (as per xhci in xhci_suspend()
929 res = readl(&xhci->op_regs->status); in xhci_suspend()
930 if ((xhci->quirks & XHCI_SNPS_BROKEN_SUSPEND) && in xhci_suspend()
933 xhci->broken_suspend = 1; in xhci_suspend()
935 xhci_warn(xhci, "WARN: xHC save state timeout\n"); in xhci_suspend()
936 spin_unlock_irq(&xhci->lock); in xhci_suspend()
937 return -ETIMEDOUT; in xhci_suspend()
940 spin_unlock_irq(&xhci->lock); in xhci_suspend()
943 * Deleting Compliance Mode Recovery Timer because the xHCI Host in xhci_suspend()
946 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && in xhci_suspend()
947 (!(xhci_all_ports_seen_u0(xhci)))) { in xhci_suspend()
948 del_timer_sync(&xhci->comp_mode_recovery_timer); in xhci_suspend()
949 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_suspend()
959 * start xHC (not bus-specific)
964 int xhci_resume(struct xhci_hcd *xhci, pm_message_t msg) in xhci_resume() argument
968 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_resume()
975 if (!hcd->state) in xhci_resume()
982 if (time_before(jiffies, xhci->usb2_rhub.bus_state.next_statechange) || in xhci_resume()
983 time_before(jiffies, xhci->usb3_rhub.bus_state.next_statechange)) in xhci_resume()
986 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); in xhci_resume()
987 if (xhci->shared_hcd) in xhci_resume()
988 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags); in xhci_resume()
990 spin_lock_irq(&xhci->lock); in xhci_resume()
992 if (hibernated || xhci->quirks & XHCI_RESET_ON_RESUME || xhci->broken_suspend) in xhci_resume()
1000 retval = xhci_handshake(&xhci->op_regs->status, in xhci_resume()
1003 xhci_warn(xhci, "Controller not ready at resume %d\n", in xhci_resume()
1005 spin_unlock_irq(&xhci->lock); in xhci_resume()
1009 xhci_restore_registers(xhci); in xhci_resume()
1011 xhci_set_cmd_ring_deq(xhci); in xhci_resume()
1014 command = readl(&xhci->op_regs->command); in xhci_resume()
1016 writel(command, &xhci->op_regs->command); in xhci_resume()
1019 * restore so setting the timeout to 100ms. Xhci specification in xhci_resume()
1022 if (xhci_handshake(&xhci->op_regs->status, in xhci_resume()
1024 xhci_warn(xhci, "WARN: xHC restore state timeout\n"); in xhci_resume()
1025 spin_unlock_irq(&xhci->lock); in xhci_resume()
1026 return -ETIMEDOUT; in xhci_resume()
1030 temp = readl(&xhci->op_regs->status); in xhci_resume()
1032 /* re-initialize the HC on Restore Error, or Host Controller Error */ in xhci_resume()
1034 !(xhci->xhc_state & XHCI_STATE_REMOVING)) { in xhci_resume()
1036 if (!xhci->broken_suspend) in xhci_resume()
1037 xhci_warn(xhci, "xHC error in resume, USBSTS 0x%x, Reinit\n", temp); in xhci_resume()
1041 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && in xhci_resume()
1042 !(xhci_all_ports_seen_u0(xhci))) { in xhci_resume()
1043 del_timer_sync(&xhci->comp_mode_recovery_timer); in xhci_resume()
1044 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_resume()
1049 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub); in xhci_resume()
1050 if (xhci->shared_hcd) in xhci_resume()
1051 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub); in xhci_resume()
1053 xhci_dbg(xhci, "Stop HCD\n"); in xhci_resume()
1054 xhci_halt(xhci); in xhci_resume()
1055 xhci_zero_64b_regs(xhci); in xhci_resume()
1056 retval = xhci_reset(xhci, XHCI_RESET_LONG_USEC); in xhci_resume()
1057 spin_unlock_irq(&xhci->lock); in xhci_resume()
1061 xhci_dbg(xhci, "// Disabling event ring interrupts\n"); in xhci_resume()
1062 temp = readl(&xhci->op_regs->status); in xhci_resume()
1063 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status); in xhci_resume()
1064 xhci_disable_interrupter(xhci->interrupter); in xhci_resume()
1066 xhci_dbg(xhci, "cleaning up memory\n"); in xhci_resume()
1067 xhci_mem_cleanup(xhci); in xhci_resume()
1068 xhci_debugfs_exit(xhci); in xhci_resume()
1069 xhci_dbg(xhci, "xhci_stop completed - status = %x\n", in xhci_resume()
1070 readl(&xhci->op_regs->status)); in xhci_resume()
1076 xhci_dbg(xhci, "Initialize the xhci_hcd\n"); in xhci_resume()
1082 xhci_dbg(xhci, "Start the primary HCD\n"); in xhci_resume()
1084 if (!retval && xhci->shared_hcd) { in xhci_resume()
1085 xhci_dbg(xhci, "Start the secondary HCD\n"); in xhci_resume()
1086 retval = xhci_run(xhci->shared_hcd); in xhci_resume()
1094 hcd->state = HC_STATE_SUSPENDED; in xhci_resume()
1096 if (xhci->shared_hcd) { in xhci_resume()
1097 xhci->shared_hcd->state = HC_STATE_SUSPENDED; in xhci_resume()
1098 usb_hcd_resume_root_hub(xhci->shared_hcd); in xhci_resume()
1106 command = readl(&xhci->op_regs->command); in xhci_resume()
1108 writel(command, &xhci->op_regs->command); in xhci_resume()
1109 xhci_handshake(&xhci->op_regs->status, STS_HALT, in xhci_resume()
1121 spin_unlock_irq(&xhci->lock); in xhci_resume()
1123 xhci_dbc_resume(xhci); in xhci_resume()
1132 if (xhci->usb3_rhub.bus_state.suspended_ports || in xhci_resume()
1133 xhci->usb3_rhub.bus_state.bus_suspended) in xhci_resume()
1136 pending_portevent = xhci_pending_portevent(xhci); in xhci_resume()
1141 pending_portevent = xhci_pending_portevent(xhci); in xhci_resume()
1145 if (xhci->shared_hcd) in xhci_resume()
1146 usb_hcd_resume_root_hub(xhci->shared_hcd); in xhci_resume()
1153 * be re-initialized Always after a system resume. Ports are subject in xhci_resume()
1157 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running) in xhci_resume()
1158 compliance_mode_recovery_timer_init(xhci); in xhci_resume()
1160 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL) in xhci_resume()
1161 usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller)); in xhci_resume()
1163 /* Re-enable port polling. */ in xhci_resume()
1164 xhci_dbg(xhci, "%s: starting usb%d port polling.\n", in xhci_resume()
1165 __func__, hcd->self.busnum); in xhci_resume()
1166 if (xhci->shared_hcd) { in xhci_resume()
1167 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_resume()
1168 usb_hcd_poll_rh_status(xhci->shared_hcd); in xhci_resume()
1170 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_resume()
1178 /*-------------------------------------------------------------------------*/
1188 buf_len = urb->transfer_buffer_length; in xhci_map_temp_buffer()
1191 dev_to_node(hcd->self.sysdev)); in xhci_map_temp_buffer()
1193 return -ENOMEM; in xhci_map_temp_buffer()
1196 sg_pcopy_to_buffer(urb->sg, urb->num_sgs, in xhci_map_temp_buffer()
1199 urb->transfer_buffer = temp; in xhci_map_temp_buffer()
1200 urb->transfer_dma = dma_map_single(hcd->self.sysdev, in xhci_map_temp_buffer()
1201 urb->transfer_buffer, in xhci_map_temp_buffer()
1202 urb->transfer_buffer_length, in xhci_map_temp_buffer()
1205 if (dma_mapping_error(hcd->self.sysdev, in xhci_map_temp_buffer()
1206 urb->transfer_dma)) { in xhci_map_temp_buffer()
1207 ret = -EAGAIN; in xhci_map_temp_buffer()
1210 urb->transfer_flags |= URB_DMA_MAP_SINGLE; in xhci_map_temp_buffer()
1227 tail_sg = urb->sg; in xhci_urb_temp_buffer_required()
1228 max_pkt = usb_endpoint_maxp(&urb->ep->desc); in xhci_urb_temp_buffer_required()
1230 if (!urb->num_sgs) in xhci_urb_temp_buffer_required()
1233 if (urb->dev->speed >= USB_SPEED_SUPER) in xhci_urb_temp_buffer_required()
1238 if (urb->transfer_buffer_length != 0 && in xhci_urb_temp_buffer_required()
1239 !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { in xhci_urb_temp_buffer_required()
1240 for_each_sg(urb->sg, sg, urb->num_sgs, i) { in xhci_urb_temp_buffer_required()
1241 len = len + sg->length; in xhci_urb_temp_buffer_required()
1242 if (i > trb_size - 2) { in xhci_urb_temp_buffer_required()
1243 len = len - tail_sg->length; in xhci_urb_temp_buffer_required()
1264 buf_len = urb->transfer_buffer_length; in xhci_unmap_temp_buf()
1267 (urb->transfer_flags & URB_DMA_MAP_SINGLE)) in xhci_unmap_temp_buf()
1268 dma_unmap_single(hcd->self.sysdev, in xhci_unmap_temp_buf()
1269 urb->transfer_dma, in xhci_unmap_temp_buf()
1270 urb->transfer_buffer_length, in xhci_unmap_temp_buf()
1274 len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, in xhci_unmap_temp_buf()
1275 urb->transfer_buffer, in xhci_unmap_temp_buf()
1281 urb->actual_length = len; in xhci_unmap_temp_buf()
1284 urb->transfer_flags &= ~URB_DMA_MAP_SINGLE; in xhci_unmap_temp_buf()
1285 kfree(urb->transfer_buffer); in xhci_unmap_temp_buf()
1286 urb->transfer_buffer = NULL; in xhci_unmap_temp_buf()
1298 struct xhci_hcd *xhci; in xhci_map_urb_for_dma() local
1300 xhci = hcd_to_xhci(hcd); in xhci_map_urb_for_dma()
1305 if (xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) { in xhci_map_urb_for_dma()
1314 struct xhci_hcd *xhci; in xhci_unmap_urb_for_dma() local
1317 xhci = hcd_to_xhci(hcd); in xhci_unmap_urb_for_dma()
1319 if (urb->num_sgs && (urb->transfer_flags & URB_DMA_MAP_SINGLE)) in xhci_unmap_urb_for_dma()
1322 if ((xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) && unmap_temp_buf) in xhci_unmap_urb_for_dma()
1329 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1333 * Index = (epnum * 2) + direction - 1,
1336 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1345 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1; in xhci_get_endpoint_index()
1351 * address from the XHCI endpoint index.
1377 return fls(added_ctxs) - 1; in xhci_last_valid_endpoint()
1381 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1386 struct xhci_hcd *xhci; in xhci_check_args() local
1390 pr_debug("xHCI %s called with invalid args\n", func); in xhci_check_args()
1391 return -EINVAL; in xhci_check_args()
1393 if (!udev->parent) { in xhci_check_args()
1394 pr_debug("xHCI %s called for root hub\n", func); in xhci_check_args()
1398 xhci = hcd_to_xhci(hcd); in xhci_check_args()
1400 if (!udev->slot_id || !xhci->devs[udev->slot_id]) { in xhci_check_args()
1401 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n", in xhci_check_args()
1403 return -EINVAL; in xhci_check_args()
1406 virt_dev = xhci->devs[udev->slot_id]; in xhci_check_args()
1407 if (virt_dev->udev != udev) { in xhci_check_args()
1408 xhci_dbg(xhci, "xHCI %s called with udev and " in xhci_check_args()
1410 return -EINVAL; in xhci_check_args()
1414 if (xhci->xhc_state & XHCI_STATE_HALTED) in xhci_check_args()
1415 return -ENODEV; in xhci_check_args()
1420 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1430 static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id, in xhci_check_maxpacket() argument
1441 out_ctx = xhci->devs[slot_id]->out_ctx; in xhci_check_maxpacket()
1442 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); in xhci_check_maxpacket()
1443 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2)); in xhci_check_maxpacket()
1444 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc); in xhci_check_maxpacket()
1446 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1448 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1451 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1452 "Max packet size in xHCI HW = %d", in xhci_check_maxpacket()
1454 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1458 /* FIXME: This won't work if a non-default control endpoint in xhci_check_maxpacket()
1462 command = xhci_alloc_command(xhci, true, mem_flags); in xhci_check_maxpacket()
1464 return -ENOMEM; in xhci_check_maxpacket()
1466 command->in_ctx = xhci->devs[slot_id]->in_ctx; in xhci_check_maxpacket()
1467 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_check_maxpacket()
1469 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_check_maxpacket()
1471 ret = -ENOMEM; in xhci_check_maxpacket()
1475 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx, in xhci_check_maxpacket()
1476 xhci->devs[slot_id]->out_ctx, ep_index); in xhci_check_maxpacket()
1478 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); in xhci_check_maxpacket()
1479 ep_ctx->ep_info &= cpu_to_le32(~EP_STATE_MASK);/* must clear */ in xhci_check_maxpacket()
1480 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK); in xhci_check_maxpacket()
1481 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size)); in xhci_check_maxpacket()
1483 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG); in xhci_check_maxpacket()
1484 ctrl_ctx->drop_flags = 0; in xhci_check_maxpacket()
1486 ret = xhci_configure_endpoint(xhci, urb->dev, command, in xhci_check_maxpacket()
1492 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG); in xhci_check_maxpacket()
1494 kfree(command->completion); in xhci_check_maxpacket()
1501 * non-error returns are a promise to giveback() the urb later
1506 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_urb_enqueue() local
1515 return -EINVAL; in xhci_urb_enqueue()
1516 ret = xhci_check_args(hcd, urb->dev, urb->ep, in xhci_urb_enqueue()
1519 return ret ? ret : -EINVAL; in xhci_urb_enqueue()
1521 slot_id = urb->dev->slot_id; in xhci_urb_enqueue()
1522 ep_index = xhci_get_endpoint_index(&urb->ep->desc); in xhci_urb_enqueue()
1523 ep_state = &xhci->devs[slot_id]->eps[ep_index].ep_state; in xhci_urb_enqueue()
1526 return -ESHUTDOWN; in xhci_urb_enqueue()
1528 if (xhci->devs[slot_id]->flags & VDEV_PORT_ERROR) { in xhci_urb_enqueue()
1529 xhci_dbg(xhci, "Can't queue urb, port error, link inactive\n"); in xhci_urb_enqueue()
1530 return -ENODEV; in xhci_urb_enqueue()
1533 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) in xhci_urb_enqueue()
1534 num_tds = urb->number_of_packets; in xhci_urb_enqueue()
1535 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) && in xhci_urb_enqueue()
1536 urb->transfer_buffer_length > 0 && in xhci_urb_enqueue()
1537 urb->transfer_flags & URB_ZERO_PACKET && in xhci_urb_enqueue()
1538 !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc))) in xhci_urb_enqueue()
1545 return -ENOMEM; in xhci_urb_enqueue()
1547 urb_priv->num_tds = num_tds; in xhci_urb_enqueue()
1548 urb_priv->num_tds_done = 0; in xhci_urb_enqueue()
1549 urb->hcpriv = urb_priv; in xhci_urb_enqueue()
1553 if (usb_endpoint_xfer_control(&urb->ep->desc)) { in xhci_urb_enqueue()
1557 if (urb->dev->speed == USB_SPEED_FULL) { in xhci_urb_enqueue()
1558 ret = xhci_check_maxpacket(xhci, slot_id, in xhci_urb_enqueue()
1562 urb->hcpriv = NULL; in xhci_urb_enqueue()
1568 spin_lock_irqsave(&xhci->lock, flags); in xhci_urb_enqueue()
1570 if (xhci->xhc_state & XHCI_STATE_DYING) { in xhci_urb_enqueue()
1571 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for non-responsive xHCI host.\n", in xhci_urb_enqueue()
1572 urb->ep->desc.bEndpointAddress, urb); in xhci_urb_enqueue()
1573 ret = -ESHUTDOWN; in xhci_urb_enqueue()
1577 xhci_warn(xhci, "WARN: Can't enqueue URB, ep in streams transition state %x\n", in xhci_urb_enqueue()
1579 ret = -EINVAL; in xhci_urb_enqueue()
1583 xhci_warn(xhci, "Can't enqueue URB while manually clearing toggle\n"); in xhci_urb_enqueue()
1584 ret = -EINVAL; in xhci_urb_enqueue()
1588 switch (usb_endpoint_type(&urb->ep->desc)) { in xhci_urb_enqueue()
1591 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1595 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1599 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1603 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1610 urb->hcpriv = NULL; in xhci_urb_enqueue()
1612 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_urb_enqueue()
1632 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1634 * endpoint command, as noted in the xHCI 0.95 errata.
1652 struct xhci_hcd *xhci; in xhci_urb_dequeue() local
1661 xhci = hcd_to_xhci(hcd); in xhci_urb_dequeue()
1662 spin_lock_irqsave(&xhci->lock, flags); in xhci_urb_dequeue()
1672 vdev = xhci->devs[urb->dev->slot_id]; in xhci_urb_dequeue()
1673 urb_priv = urb->hcpriv; in xhci_urb_dequeue()
1677 ep_index = xhci_get_endpoint_index(&urb->ep->desc); in xhci_urb_dequeue()
1678 ep = &vdev->eps[ep_index]; in xhci_urb_dequeue()
1679 ep_ring = xhci_urb_to_transfer_ring(xhci, urb); in xhci_urb_dequeue()
1684 temp = readl(&xhci->op_regs->status); in xhci_urb_dequeue()
1685 if (temp == ~(u32)0 || xhci->xhc_state & XHCI_STATE_DYING) { in xhci_urb_dequeue()
1686 xhci_hc_died(xhci); in xhci_urb_dequeue()
1691 * check ring is not re-allocated since URB was enqueued. If it is, then in xhci_urb_dequeue()
1695 if (!td_on_ring(&urb_priv->td[0], ep_ring)) { in xhci_urb_dequeue()
1696 xhci_err(xhci, "Canceled URB td not found on endpoint ring"); in xhci_urb_dequeue()
1697 for (i = urb_priv->num_tds_done; i < urb_priv->num_tds; i++) { in xhci_urb_dequeue()
1698 td = &urb_priv->td[i]; in xhci_urb_dequeue()
1699 if (!list_empty(&td->cancelled_td_list)) in xhci_urb_dequeue()
1700 list_del_init(&td->cancelled_td_list); in xhci_urb_dequeue()
1705 if (xhci->xhc_state & XHCI_STATE_HALTED) { in xhci_urb_dequeue()
1706 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_urb_dequeue()
1708 for (i = urb_priv->num_tds_done; in xhci_urb_dequeue()
1709 i < urb_priv->num_tds; in xhci_urb_dequeue()
1711 td = &urb_priv->td[i]; in xhci_urb_dequeue()
1712 if (!list_empty(&td->td_list)) in xhci_urb_dequeue()
1713 list_del_init(&td->td_list); in xhci_urb_dequeue()
1714 if (!list_empty(&td->cancelled_td_list)) in xhci_urb_dequeue()
1715 list_del_init(&td->cancelled_td_list); in xhci_urb_dequeue()
1720 i = urb_priv->num_tds_done; in xhci_urb_dequeue()
1721 if (i < urb_priv->num_tds) in xhci_urb_dequeue()
1722 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_urb_dequeue()
1725 urb, urb->dev->devpath, in xhci_urb_dequeue()
1726 urb->ep->desc.bEndpointAddress, in xhci_urb_dequeue()
1728 urb_priv->td[i].start_seg, in xhci_urb_dequeue()
1729 urb_priv->td[i].first_trb)); in xhci_urb_dequeue()
1731 for (; i < urb_priv->num_tds; i++) { in xhci_urb_dequeue()
1732 td = &urb_priv->td[i]; in xhci_urb_dequeue()
1734 if (list_empty(&td->cancelled_td_list)) { in xhci_urb_dequeue()
1735 td->cancel_status = TD_DIRTY; in xhci_urb_dequeue()
1736 list_add_tail(&td->cancelled_td_list, in xhci_urb_dequeue()
1737 &ep->cancelled_td_list); in xhci_urb_dequeue()
1742 if (ep->ep_state & (EP_STOP_CMD_PENDING | EP_HALTED | SET_DEQ_PENDING)) { in xhci_urb_dequeue()
1743 xhci_dbg(xhci, "Not queuing Stop Endpoint on slot %d ep %d in state 0x%x\n", in xhci_urb_dequeue()
1744 urb->dev->slot_id, ep_index, ep->ep_state); in xhci_urb_dequeue()
1749 if (ep->ep_state & EP_CLEARING_TT) { in xhci_urb_dequeue()
1751 xhci_dbg(xhci, "Invalidating TDs instantly on slot %d ep %d in state 0x%x\n", in xhci_urb_dequeue()
1752 urb->dev->slot_id, ep_index, ep->ep_state); in xhci_urb_dequeue()
1756 command = xhci_alloc_command(xhci, false, GFP_ATOMIC); in xhci_urb_dequeue()
1758 ret = -ENOMEM; in xhci_urb_dequeue()
1761 ep->stop_time = jiffies; in xhci_urb_dequeue()
1762 ep->ep_state |= EP_STOP_CMD_PENDING; in xhci_urb_dequeue()
1763 xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id, in xhci_urb_dequeue()
1765 xhci_ring_cmd_db(xhci); in xhci_urb_dequeue()
1768 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_urb_dequeue()
1775 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_urb_dequeue()
1776 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN); in xhci_urb_dequeue()
1791 * the xhci->devs[slot_id] structure.
1796 struct xhci_hcd *xhci; in xhci_drop_endpoint() local
1808 xhci = hcd_to_xhci(hcd); in xhci_drop_endpoint()
1809 if (xhci->xhc_state & XHCI_STATE_DYING) in xhci_drop_endpoint()
1810 return -ENODEV; in xhci_drop_endpoint()
1812 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); in xhci_drop_endpoint()
1813 drop_flag = xhci_get_endpoint_flag(&ep->desc); in xhci_drop_endpoint()
1815 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n", in xhci_drop_endpoint()
1820 in_ctx = xhci->devs[udev->slot_id]->in_ctx; in xhci_drop_endpoint()
1821 out_ctx = xhci->devs[udev->slot_id]->out_ctx; in xhci_drop_endpoint()
1824 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_drop_endpoint()
1829 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_drop_endpoint()
1830 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); in xhci_drop_endpoint()
1835 le32_to_cpu(ctrl_ctx->drop_flags) & in xhci_drop_endpoint()
1836 xhci_get_endpoint_flag(&ep->desc)) { in xhci_drop_endpoint()
1838 if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL) in xhci_drop_endpoint()
1839 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n", in xhci_drop_endpoint()
1844 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag); in xhci_drop_endpoint()
1845 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags); in xhci_drop_endpoint()
1847 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag); in xhci_drop_endpoint()
1848 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags); in xhci_drop_endpoint()
1850 xhci_debugfs_remove_endpoint(xhci, xhci->devs[udev->slot_id], ep_index); in xhci_drop_endpoint()
1852 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep); in xhci_drop_endpoint()
1854 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n", in xhci_drop_endpoint()
1855 (unsigned int) ep->desc.bEndpointAddress, in xhci_drop_endpoint()
1856 udev->slot_id, in xhci_drop_endpoint()
1874 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1879 struct xhci_hcd *xhci; in xhci_add_endpoint() local
1892 ep->hcpriv = NULL; in xhci_add_endpoint()
1895 xhci = hcd_to_xhci(hcd); in xhci_add_endpoint()
1896 if (xhci->xhc_state & XHCI_STATE_DYING) in xhci_add_endpoint()
1897 return -ENODEV; in xhci_add_endpoint()
1899 added_ctxs = xhci_get_endpoint_flag(&ep->desc); in xhci_add_endpoint()
1905 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n", in xhci_add_endpoint()
1910 virt_dev = xhci->devs[udev->slot_id]; in xhci_add_endpoint()
1911 in_ctx = virt_dev->in_ctx; in xhci_add_endpoint()
1914 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_add_endpoint()
1919 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_add_endpoint()
1923 if (virt_dev->eps[ep_index].ring && in xhci_add_endpoint()
1924 !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) { in xhci_add_endpoint()
1925 xhci_warn(xhci, "Trying to add endpoint 0x%x " in xhci_add_endpoint()
1927 (unsigned int) ep->desc.bEndpointAddress); in xhci_add_endpoint()
1928 return -EINVAL; in xhci_add_endpoint()
1934 if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) { in xhci_add_endpoint()
1935 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n", in xhci_add_endpoint()
1945 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) { in xhci_add_endpoint()
1946 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n", in xhci_add_endpoint()
1947 __func__, ep->desc.bEndpointAddress); in xhci_add_endpoint()
1948 return -ENOMEM; in xhci_add_endpoint()
1951 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs); in xhci_add_endpoint()
1952 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags); in xhci_add_endpoint()
1956 * this re-adds a new state for the endpoint from the new endpoint in xhci_add_endpoint()
1957 * descriptors. We must drop and re-add this endpoint, so we leave the in xhci_add_endpoint()
1960 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags); in xhci_add_endpoint()
1963 ep->hcpriv = udev; in xhci_add_endpoint()
1965 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); in xhci_add_endpoint()
1968 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n", in xhci_add_endpoint()
1969 (unsigned int) ep->desc.bEndpointAddress, in xhci_add_endpoint()
1970 udev->slot_id, in xhci_add_endpoint()
1977 static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev) in xhci_zero_in_ctx() argument
1984 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); in xhci_zero_in_ctx()
1986 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_zero_in_ctx()
1996 ctrl_ctx->drop_flags = 0; in xhci_zero_in_ctx()
1997 ctrl_ctx->add_flags = 0; in xhci_zero_in_ctx()
1998 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); in xhci_zero_in_ctx()
1999 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK); in xhci_zero_in_ctx()
2001 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1)); in xhci_zero_in_ctx()
2003 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i); in xhci_zero_in_ctx()
2004 ep_ctx->ep_info = 0; in xhci_zero_in_ctx()
2005 ep_ctx->ep_info2 = 0; in xhci_zero_in_ctx()
2006 ep_ctx->deq = 0; in xhci_zero_in_ctx()
2007 ep_ctx->tx_info = 0; in xhci_zero_in_ctx()
2011 static int xhci_configure_endpoint_result(struct xhci_hcd *xhci, in xhci_configure_endpoint_result() argument
2019 xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n"); in xhci_configure_endpoint_result()
2020 ret = -ETIME; in xhci_configure_endpoint_result()
2023 dev_warn(&udev->dev, in xhci_configure_endpoint_result()
2025 ret = -ENOMEM; in xhci_configure_endpoint_result()
2030 dev_warn(&udev->dev, in xhci_configure_endpoint_result()
2032 ret = -ENOSPC; in xhci_configure_endpoint_result()
2037 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, " in xhci_configure_endpoint_result()
2040 ret = -EINVAL; in xhci_configure_endpoint_result()
2043 dev_warn(&udev->dev, in xhci_configure_endpoint_result()
2045 ret = -ENODEV; in xhci_configure_endpoint_result()
2048 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_configure_endpoint_result()
2053 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n", in xhci_configure_endpoint_result()
2055 ret = -EINVAL; in xhci_configure_endpoint_result()
2061 static int xhci_evaluate_context_result(struct xhci_hcd *xhci, in xhci_evaluate_context_result() argument
2069 xhci_warn(xhci, "Timeout while waiting for evaluate context command\n"); in xhci_evaluate_context_result()
2070 ret = -ETIME; in xhci_evaluate_context_result()
2073 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2074 "WARN: xHCI driver setup invalid evaluate context command.\n"); in xhci_evaluate_context_result()
2075 ret = -EINVAL; in xhci_evaluate_context_result()
2078 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2080 ret = -EINVAL; in xhci_evaluate_context_result()
2083 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2085 ret = -EINVAL; in xhci_evaluate_context_result()
2088 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2090 ret = -ENODEV; in xhci_evaluate_context_result()
2094 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n"); in xhci_evaluate_context_result()
2095 ret = -EINVAL; in xhci_evaluate_context_result()
2098 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_evaluate_context_result()
2103 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n", in xhci_evaluate_context_result()
2105 ret = -EINVAL; in xhci_evaluate_context_result()
2111 static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci, in xhci_count_num_new_endpoints() argument
2121 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2; in xhci_count_num_new_endpoints()
2122 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2; in xhci_count_num_new_endpoints()
2128 return hweight32(valid_add_flags) - in xhci_count_num_new_endpoints()
2132 static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci, in xhci_count_num_dropped_endpoints() argument
2138 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2; in xhci_count_num_dropped_endpoints()
2139 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2; in xhci_count_num_dropped_endpoints()
2141 return hweight32(valid_drop_flags) - in xhci_count_num_dropped_endpoints()
2151 * - the first configure endpoint command drops more endpoints than it adds
2152 * - a second configure endpoint command that adds more endpoints is queued
2153 * - the first configure endpoint command fails, so the config is unchanged
2154 * - the second command may succeed, even though there isn't enough resources
2156 * Must be called with xhci->lock held.
2158 static int xhci_reserve_host_resources(struct xhci_hcd *xhci, in xhci_reserve_host_resources() argument
2163 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx); in xhci_reserve_host_resources()
2164 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) { in xhci_reserve_host_resources()
2165 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_resources()
2168 xhci->num_active_eps, added_eps, in xhci_reserve_host_resources()
2169 xhci->limit_active_eps); in xhci_reserve_host_resources()
2170 return -ENOMEM; in xhci_reserve_host_resources()
2172 xhci->num_active_eps += added_eps; in xhci_reserve_host_resources()
2173 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_resources()
2175 xhci->num_active_eps); in xhci_reserve_host_resources()
2183 * Must be called with xhci->lock held.
2185 static void xhci_free_host_resources(struct xhci_hcd *xhci, in xhci_free_host_resources() argument
2190 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx); in xhci_free_host_resources()
2191 xhci->num_active_eps -= num_failed_eps; in xhci_free_host_resources()
2192 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_free_host_resources()
2195 xhci->num_active_eps); in xhci_free_host_resources()
2202 * Must be called with xhci->lock held.
2204 static void xhci_finish_resource_reservation(struct xhci_hcd *xhci, in xhci_finish_resource_reservation() argument
2209 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx); in xhci_finish_resource_reservation()
2210 xhci->num_active_eps -= num_dropped_eps; in xhci_finish_resource_reservation()
2212 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_finish_resource_reservation()
2215 xhci->num_active_eps); in xhci_finish_resource_reservation()
2220 switch (udev->speed) { in xhci_get_block_size()
2239 if (interval_bw->overhead[LS_OVERHEAD_TYPE]) in xhci_get_largest_overhead()
2241 if (interval_bw->overhead[FS_OVERHEAD_TYPE]) in xhci_get_largest_overhead()
2250 static int xhci_check_tt_bw_table(struct xhci_hcd *xhci, in xhci_check_tt_bw_table() argument
2258 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table; in xhci_check_tt_bw_table()
2259 tt_info = virt_dev->tt_info; in xhci_check_tt_bw_table()
2266 if (old_active_eps == 0 && tt_info->active_eps != 0) { in xhci_check_tt_bw_table()
2267 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT) in xhci_check_tt_bw_table()
2268 return -ENOMEM; in xhci_check_tt_bw_table()
2280 static int xhci_check_ss_bw(struct xhci_hcd *xhci, in xhci_check_ss_bw() argument
2286 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved)) in xhci_check_ss_bw()
2287 return -ENOMEM; in xhci_check_ss_bw()
2290 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved)) in xhci_check_ss_bw()
2291 return -ENOMEM; in xhci_check_ss_bw()
2297 * This algorithm is a very conservative estimate of the worst-case scheduling
2305 * over-estimate.
2337 static int xhci_check_bw_table(struct xhci_hcd *xhci, in xhci_check_bw_table() argument
2352 if (virt_dev->udev->speed >= USB_SPEED_SUPER) in xhci_check_bw_table()
2353 return xhci_check_ss_bw(xhci, virt_dev); in xhci_check_bw_table()
2355 if (virt_dev->udev->speed == USB_SPEED_HIGH) { in xhci_check_bw_table()
2364 bw_table = virt_dev->bw_table; in xhci_check_bw_table()
2368 block_size = xhci_get_block_size(virt_dev->udev); in xhci_check_bw_table()
2373 if (virt_dev->tt_info) { in xhci_check_bw_table()
2374 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2376 virt_dev->real_port); in xhci_check_bw_table()
2377 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) { in xhci_check_bw_table()
2378 xhci_warn(xhci, "Not enough bandwidth on HS bus for " in xhci_check_bw_table()
2380 return -ENOMEM; in xhci_check_bw_table()
2382 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2384 virt_dev->tt_info->slot_id, in xhci_check_bw_table()
2385 virt_dev->tt_info->ttport); in xhci_check_bw_table()
2387 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2389 virt_dev->real_port); in xhci_check_bw_table()
2395 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) + in xhci_check_bw_table()
2396 bw_table->interval_bw[0].num_packets * in xhci_check_bw_table()
2397 xhci_get_largest_overhead(&bw_table->interval_bw[0]); in xhci_check_bw_table()
2410 bw_table->interval_bw[i].num_packets; in xhci_check_bw_table()
2415 if (list_empty(&bw_table->interval_bw[i].endpoints)) in xhci_check_bw_table()
2421 ep_entry = bw_table->interval_bw[i].endpoints.next; in xhci_check_bw_table()
2426 virt_ep->bw_info.max_packet_size, in xhci_check_bw_table()
2434 &bw_table->interval_bw[i]); in xhci_check_bw_table()
2470 xhci_warn(xhci, "Not enough bandwidth. " in xhci_check_bw_table()
2473 return -ENOMEM; in xhci_check_bw_table()
2477 * Ok, we know we have some packets left over after even-handedly in xhci_check_bw_table()
2479 * fit into, so we over-schedule and say they will be scheduled every in xhci_check_bw_table()
2485 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) { in xhci_check_bw_table()
2486 unsigned int port_index = virt_dev->real_port - 1; in xhci_check_bw_table()
2493 xhci->rh_bw[port_index].num_active_tts; in xhci_check_bw_table()
2496 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2500 (max_bandwidth - bw_used - bw_reserved) * 100 / in xhci_check_bw_table()
2505 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n", in xhci_check_bw_table()
2507 return -ENOMEM; in xhci_check_bw_table()
2510 bw_table->bw_used = bw_used; in xhci_check_bw_table()
2528 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK); in xhci_get_ss_bw_consumed()
2530 if (ep_bw->ep_interval == 0) in xhci_get_ss_bw_consumed()
2532 (ep_bw->mult * ep_bw->num_packets * in xhci_get_ss_bw_consumed()
2534 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets * in xhci_get_ss_bw_consumed()
2536 1 << ep_bw->ep_interval); in xhci_get_ss_bw_consumed()
2540 static void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci, in xhci_drop_ep_from_interval_table() argument
2550 if (xhci_is_async_ep(ep_bw->type)) in xhci_drop_ep_from_interval_table()
2553 if (udev->speed >= USB_SPEED_SUPER) { in xhci_drop_ep_from_interval_table()
2554 if (xhci_is_sync_in_ep(ep_bw->type)) in xhci_drop_ep_from_interval_table()
2555 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -= in xhci_drop_ep_from_interval_table()
2558 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -= in xhci_drop_ep_from_interval_table()
2566 if (list_empty(&virt_ep->bw_endpoint_list)) in xhci_drop_ep_from_interval_table()
2571 if (udev->speed == USB_SPEED_HIGH) in xhci_drop_ep_from_interval_table()
2572 normalized_interval = ep_bw->ep_interval; in xhci_drop_ep_from_interval_table()
2574 normalized_interval = ep_bw->ep_interval - 3; in xhci_drop_ep_from_interval_table()
2577 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload; in xhci_drop_ep_from_interval_table()
2578 interval_bw = &bw_table->interval_bw[normalized_interval]; in xhci_drop_ep_from_interval_table()
2579 interval_bw->num_packets -= ep_bw->num_packets; in xhci_drop_ep_from_interval_table()
2580 switch (udev->speed) { in xhci_drop_ep_from_interval_table()
2582 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1; in xhci_drop_ep_from_interval_table()
2585 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1; in xhci_drop_ep_from_interval_table()
2588 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1; in xhci_drop_ep_from_interval_table()
2597 tt_info->active_eps -= 1; in xhci_drop_ep_from_interval_table()
2598 list_del_init(&virt_ep->bw_endpoint_list); in xhci_drop_ep_from_interval_table()
2601 static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci, in xhci_add_ep_to_interval_table() argument
2612 if (xhci_is_async_ep(ep_bw->type)) in xhci_add_ep_to_interval_table()
2615 if (udev->speed == USB_SPEED_SUPER) { in xhci_add_ep_to_interval_table()
2616 if (xhci_is_sync_in_ep(ep_bw->type)) in xhci_add_ep_to_interval_table()
2617 xhci->devs[udev->slot_id]->bw_table->ss_bw_in += in xhci_add_ep_to_interval_table()
2620 xhci->devs[udev->slot_id]->bw_table->ss_bw_out += in xhci_add_ep_to_interval_table()
2628 if (udev->speed == USB_SPEED_HIGH) in xhci_add_ep_to_interval_table()
2629 normalized_interval = ep_bw->ep_interval; in xhci_add_ep_to_interval_table()
2631 normalized_interval = ep_bw->ep_interval - 3; in xhci_add_ep_to_interval_table()
2634 bw_table->interval0_esit_payload += ep_bw->max_esit_payload; in xhci_add_ep_to_interval_table()
2635 interval_bw = &bw_table->interval_bw[normalized_interval]; in xhci_add_ep_to_interval_table()
2636 interval_bw->num_packets += ep_bw->num_packets; in xhci_add_ep_to_interval_table()
2637 switch (udev->speed) { in xhci_add_ep_to_interval_table()
2639 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1; in xhci_add_ep_to_interval_table()
2642 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1; in xhci_add_ep_to_interval_table()
2645 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1; in xhci_add_ep_to_interval_table()
2655 tt_info->active_eps += 1; in xhci_add_ep_to_interval_table()
2657 list_for_each_entry(smaller_ep, &interval_bw->endpoints, in xhci_add_ep_to_interval_table()
2659 if (ep_bw->max_packet_size >= in xhci_add_ep_to_interval_table()
2660 smaller_ep->bw_info.max_packet_size) { in xhci_add_ep_to_interval_table()
2662 list_add_tail(&virt_ep->bw_endpoint_list, in xhci_add_ep_to_interval_table()
2663 &smaller_ep->bw_endpoint_list); in xhci_add_ep_to_interval_table()
2668 list_add_tail(&virt_ep->bw_endpoint_list, in xhci_add_ep_to_interval_table()
2669 &interval_bw->endpoints); in xhci_add_ep_to_interval_table()
2672 void xhci_update_tt_active_eps(struct xhci_hcd *xhci, in xhci_update_tt_active_eps() argument
2677 if (!virt_dev->tt_info) in xhci_update_tt_active_eps()
2680 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1]; in xhci_update_tt_active_eps()
2682 virt_dev->tt_info->active_eps != 0) { in xhci_update_tt_active_eps()
2683 rh_bw_info->num_active_tts += 1; in xhci_update_tt_active_eps()
2684 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD; in xhci_update_tt_active_eps()
2686 virt_dev->tt_info->active_eps == 0) { in xhci_update_tt_active_eps()
2687 rh_bw_info->num_active_tts -= 1; in xhci_update_tt_active_eps()
2688 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD; in xhci_update_tt_active_eps()
2692 static int xhci_reserve_bandwidth(struct xhci_hcd *xhci, in xhci_reserve_bandwidth() argument
2701 if (virt_dev->tt_info) in xhci_reserve_bandwidth()
2702 old_active_eps = virt_dev->tt_info->active_eps; in xhci_reserve_bandwidth()
2706 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_reserve_bandwidth()
2708 return -ENOMEM; in xhci_reserve_bandwidth()
2716 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2722 xhci_drop_ep_from_interval_table(xhci, in xhci_reserve_bandwidth()
2723 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2724 virt_dev->bw_table, in xhci_reserve_bandwidth()
2725 virt_dev->udev, in xhci_reserve_bandwidth()
2726 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2727 virt_dev->tt_info); in xhci_reserve_bandwidth()
2730 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev); in xhci_reserve_bandwidth()
2734 xhci_add_ep_to_interval_table(xhci, in xhci_reserve_bandwidth()
2735 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2736 virt_dev->bw_table, in xhci_reserve_bandwidth()
2737 virt_dev->udev, in xhci_reserve_bandwidth()
2738 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2739 virt_dev->tt_info); in xhci_reserve_bandwidth()
2742 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) { in xhci_reserve_bandwidth()
2746 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps); in xhci_reserve_bandwidth()
2759 xhci_drop_ep_from_interval_table(xhci, in xhci_reserve_bandwidth()
2760 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2761 virt_dev->bw_table, in xhci_reserve_bandwidth()
2762 virt_dev->udev, in xhci_reserve_bandwidth()
2763 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2764 virt_dev->tt_info); in xhci_reserve_bandwidth()
2767 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i], in xhci_reserve_bandwidth()
2771 xhci_add_ep_to_interval_table(xhci, in xhci_reserve_bandwidth()
2772 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2773 virt_dev->bw_table, in xhci_reserve_bandwidth()
2774 virt_dev->udev, in xhci_reserve_bandwidth()
2775 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2776 virt_dev->tt_info); in xhci_reserve_bandwidth()
2778 return -ENOMEM; in xhci_reserve_bandwidth()
2785 static int xhci_configure_endpoint(struct xhci_hcd *xhci, in xhci_configure_endpoint() argument
2797 return -EINVAL; in xhci_configure_endpoint()
2799 spin_lock_irqsave(&xhci->lock, flags); in xhci_configure_endpoint()
2801 if (xhci->xhc_state & XHCI_STATE_DYING) { in xhci_configure_endpoint()
2802 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2803 return -ESHUTDOWN; in xhci_configure_endpoint()
2806 virt_dev = xhci->devs[udev->slot_id]; in xhci_configure_endpoint()
2808 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_configure_endpoint()
2810 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2811 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_configure_endpoint()
2813 return -ENOMEM; in xhci_configure_endpoint()
2816 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) && in xhci_configure_endpoint()
2817 xhci_reserve_host_resources(xhci, ctrl_ctx)) { in xhci_configure_endpoint()
2818 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2819 xhci_warn(xhci, "Not enough host resources, " in xhci_configure_endpoint()
2821 xhci->num_active_eps); in xhci_configure_endpoint()
2822 return -ENOMEM; in xhci_configure_endpoint()
2824 if ((xhci->quirks & XHCI_SW_BW_CHECKING) && !ctx_change && in xhci_configure_endpoint()
2825 xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) { in xhci_configure_endpoint()
2826 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) in xhci_configure_endpoint()
2827 xhci_free_host_resources(xhci, ctrl_ctx); in xhci_configure_endpoint()
2828 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2829 xhci_warn(xhci, "Not enough bandwidth\n"); in xhci_configure_endpoint()
2830 return -ENOMEM; in xhci_configure_endpoint()
2833 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx); in xhci_configure_endpoint()
2839 ret = xhci_queue_configure_endpoint(xhci, command, in xhci_configure_endpoint()
2840 command->in_ctx->dma, in xhci_configure_endpoint()
2841 udev->slot_id, must_succeed); in xhci_configure_endpoint()
2843 ret = xhci_queue_evaluate_context(xhci, command, in xhci_configure_endpoint()
2844 command->in_ctx->dma, in xhci_configure_endpoint()
2845 udev->slot_id, must_succeed); in xhci_configure_endpoint()
2847 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) in xhci_configure_endpoint()
2848 xhci_free_host_resources(xhci, ctrl_ctx); in xhci_configure_endpoint()
2849 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2850 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_configure_endpoint()
2852 return -ENOMEM; in xhci_configure_endpoint()
2854 xhci_ring_cmd_db(xhci); in xhci_configure_endpoint()
2855 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2858 wait_for_completion(command->completion); in xhci_configure_endpoint()
2861 ret = xhci_configure_endpoint_result(xhci, udev, in xhci_configure_endpoint()
2862 &command->status); in xhci_configure_endpoint()
2864 ret = xhci_evaluate_context_result(xhci, udev, in xhci_configure_endpoint()
2865 &command->status); in xhci_configure_endpoint()
2867 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { in xhci_configure_endpoint()
2868 spin_lock_irqsave(&xhci->lock, flags); in xhci_configure_endpoint()
2873 xhci_free_host_resources(xhci, ctrl_ctx); in xhci_configure_endpoint()
2875 xhci_finish_resource_reservation(xhci, ctrl_ctx); in xhci_configure_endpoint()
2876 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2881 static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci, in xhci_check_bw_drop_ep_streams() argument
2884 struct xhci_virt_ep *ep = &vdev->eps[i]; in xhci_check_bw_drop_ep_streams()
2886 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_check_bw_drop_ep_streams()
2887 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n", in xhci_check_bw_drop_ep_streams()
2889 xhci_free_stream_info(xhci, ep->stream_info); in xhci_check_bw_drop_ep_streams()
2890 ep->stream_info = NULL; in xhci_check_bw_drop_ep_streams()
2891 ep->ep_state &= ~EP_HAS_STREAMS; in xhci_check_bw_drop_ep_streams()
2902 * else should be touching the xhci->devs[slot_id] structure, so we
2903 * don't need to take the xhci->lock for manipulating that.
2909 struct xhci_hcd *xhci; in xhci_check_bandwidth() local
2918 xhci = hcd_to_xhci(hcd); in xhci_check_bandwidth()
2919 if ((xhci->xhc_state & XHCI_STATE_DYING) || in xhci_check_bandwidth()
2920 (xhci->xhc_state & XHCI_STATE_REMOVING)) in xhci_check_bandwidth()
2921 return -ENODEV; in xhci_check_bandwidth()
2923 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); in xhci_check_bandwidth()
2924 virt_dev = xhci->devs[udev->slot_id]; in xhci_check_bandwidth()
2926 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_check_bandwidth()
2928 return -ENOMEM; in xhci_check_bandwidth()
2930 command->in_ctx = virt_dev->in_ctx; in xhci_check_bandwidth()
2932 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */ in xhci_check_bandwidth()
2933 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_check_bandwidth()
2935 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_check_bandwidth()
2937 ret = -ENOMEM; in xhci_check_bandwidth()
2940 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_check_bandwidth()
2941 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG); in xhci_check_bandwidth()
2942 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG)); in xhci_check_bandwidth()
2945 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) && in xhci_check_bandwidth()
2946 ctrl_ctx->drop_flags == 0) { in xhci_check_bandwidth()
2951 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); in xhci_check_bandwidth()
2952 for (i = 31; i >= 1; i--) { in xhci_check_bandwidth()
2955 if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32)) in xhci_check_bandwidth()
2956 || (ctrl_ctx->add_flags & le32) || i == 1) { in xhci_check_bandwidth()
2957 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK); in xhci_check_bandwidth()
2958 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i)); in xhci_check_bandwidth()
2963 ret = xhci_configure_endpoint(xhci, udev, command, in xhci_check_bandwidth()
2971 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) && in xhci_check_bandwidth()
2972 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) { in xhci_check_bandwidth()
2973 xhci_free_endpoint_ring(xhci, virt_dev, i); in xhci_check_bandwidth()
2974 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i); in xhci_check_bandwidth()
2977 xhci_zero_in_ctx(xhci, virt_dev); in xhci_check_bandwidth()
2983 if (!virt_dev->eps[i].new_ring) in xhci_check_bandwidth()
2988 if (virt_dev->eps[i].ring) { in xhci_check_bandwidth()
2989 xhci_free_endpoint_ring(xhci, virt_dev, i); in xhci_check_bandwidth()
2991 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i); in xhci_check_bandwidth()
2992 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring; in xhci_check_bandwidth()
2993 virt_dev->eps[i].new_ring = NULL; in xhci_check_bandwidth()
2994 xhci_debugfs_create_endpoint(xhci, virt_dev, i); in xhci_check_bandwidth()
2997 kfree(command->completion); in xhci_check_bandwidth()
3006 struct xhci_hcd *xhci; in xhci_reset_bandwidth() local
3013 xhci = hcd_to_xhci(hcd); in xhci_reset_bandwidth()
3015 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); in xhci_reset_bandwidth()
3016 virt_dev = xhci->devs[udev->slot_id]; in xhci_reset_bandwidth()
3019 if (virt_dev->eps[i].new_ring) { in xhci_reset_bandwidth()
3020 xhci_debugfs_remove_endpoint(xhci, virt_dev, i); in xhci_reset_bandwidth()
3021 xhci_ring_free(xhci, virt_dev->eps[i].new_ring); in xhci_reset_bandwidth()
3022 virt_dev->eps[i].new_ring = NULL; in xhci_reset_bandwidth()
3025 xhci_zero_in_ctx(xhci, virt_dev); in xhci_reset_bandwidth()
3029 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci, in xhci_setup_input_ctx_for_config_ep() argument
3035 ctrl_ctx->add_flags = cpu_to_le32(add_flags); in xhci_setup_input_ctx_for_config_ep()
3036 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags); in xhci_setup_input_ctx_for_config_ep()
3037 xhci_slot_copy(xhci, in_ctx, out_ctx); in xhci_setup_input_ctx_for_config_ep()
3038 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_setup_input_ctx_for_config_ep()
3044 struct xhci_hcd *xhci; in xhci_endpoint_disable() local
3051 xhci = hcd_to_xhci(hcd); in xhci_endpoint_disable()
3053 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_disable()
3055 udev = (struct usb_device *)host_ep->hcpriv; in xhci_endpoint_disable()
3056 if (!udev || !udev->slot_id) in xhci_endpoint_disable()
3059 vdev = xhci->devs[udev->slot_id]; in xhci_endpoint_disable()
3063 ep_index = xhci_get_endpoint_index(&host_ep->desc); in xhci_endpoint_disable()
3064 ep = &vdev->eps[ep_index]; in xhci_endpoint_disable()
3067 if (ep->ep_state & EP_CLEARING_TT) { in xhci_endpoint_disable()
3068 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_disable()
3073 if (ep->ep_state) in xhci_endpoint_disable()
3074 xhci_dbg(xhci, "endpoint disable with ep_state 0x%x\n", in xhci_endpoint_disable()
3075 ep->ep_state); in xhci_endpoint_disable()
3077 host_ep->hcpriv = NULL; in xhci_endpoint_disable()
3078 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_disable()
3090 * endpoint. Refer to the additional note in xhci spcification section 4.6.8.
3096 struct xhci_hcd *xhci; in xhci_endpoint_reset() local
3107 xhci = hcd_to_xhci(hcd); in xhci_endpoint_reset()
3108 if (!host_ep->hcpriv) in xhci_endpoint_reset()
3110 udev = (struct usb_device *) host_ep->hcpriv; in xhci_endpoint_reset()
3111 vdev = xhci->devs[udev->slot_id]; in xhci_endpoint_reset()
3114 * vdev may be lost due to xHC restore error and re-initialization in xhci_endpoint_reset()
3118 if (!udev->slot_id || !vdev) in xhci_endpoint_reset()
3120 ep_index = xhci_get_endpoint_index(&host_ep->desc); in xhci_endpoint_reset()
3121 ep = &vdev->eps[ep_index]; in xhci_endpoint_reset()
3124 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3125 if (ep->ep_state & EP_HARD_CLEAR_TOGGLE) { in xhci_endpoint_reset()
3126 ep->ep_state &= ~EP_HARD_CLEAR_TOGGLE; in xhci_endpoint_reset()
3127 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3130 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3131 /* Only interrupt and bulk ep's use data toggle, USB2 spec 5.5.4-> */ in xhci_endpoint_reset()
3132 if (usb_endpoint_xfer_control(&host_ep->desc) || in xhci_endpoint_reset()
3133 usb_endpoint_xfer_isoc(&host_ep->desc)) in xhci_endpoint_reset()
3136 ep_flag = xhci_get_endpoint_flag(&host_ep->desc); in xhci_endpoint_reset()
3141 stop_cmd = xhci_alloc_command(xhci, true, GFP_NOWAIT); in xhci_endpoint_reset()
3145 cfg_cmd = xhci_alloc_command_with_ctx(xhci, true, GFP_NOWAIT); in xhci_endpoint_reset()
3149 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3152 ep->ep_state |= EP_SOFT_CLEAR_TOGGLE; in xhci_endpoint_reset()
3160 if (!list_empty(&ep->ring->td_list)) { in xhci_endpoint_reset()
3161 dev_err(&udev->dev, "EP not empty, refuse reset\n"); in xhci_endpoint_reset()
3162 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3163 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3167 err = xhci_queue_stop_endpoint(xhci, stop_cmd, udev->slot_id, in xhci_endpoint_reset()
3170 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3171 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3172 xhci_dbg(xhci, "%s: Failed to queue stop ep command, %d ", in xhci_endpoint_reset()
3177 xhci_ring_cmd_db(xhci); in xhci_endpoint_reset()
3178 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3180 wait_for_completion(stop_cmd->completion); in xhci_endpoint_reset()
3182 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3185 ctrl_ctx = xhci_get_input_control_ctx(cfg_cmd->in_ctx); in xhci_endpoint_reset()
3187 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3188 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3189 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_endpoint_reset()
3194 xhci_setup_input_ctx_for_config_ep(xhci, cfg_cmd->in_ctx, vdev->out_ctx, in xhci_endpoint_reset()
3196 xhci_endpoint_copy(xhci, cfg_cmd->in_ctx, vdev->out_ctx, ep_index); in xhci_endpoint_reset()
3198 err = xhci_queue_configure_endpoint(xhci, cfg_cmd, cfg_cmd->in_ctx->dma, in xhci_endpoint_reset()
3199 udev->slot_id, false); in xhci_endpoint_reset()
3201 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3202 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3203 xhci_dbg(xhci, "%s: Failed to queue config ep command, %d ", in xhci_endpoint_reset()
3208 xhci_ring_cmd_db(xhci); in xhci_endpoint_reset()
3209 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3211 wait_for_completion(cfg_cmd->completion); in xhci_endpoint_reset()
3213 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3215 xhci_free_command(xhci, stop_cmd); in xhci_endpoint_reset()
3216 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3217 if (ep->ep_state & EP_SOFT_CLEAR_TOGGLE) in xhci_endpoint_reset()
3218 ep->ep_state &= ~EP_SOFT_CLEAR_TOGGLE; in xhci_endpoint_reset()
3219 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3222 static int xhci_check_streams_endpoint(struct xhci_hcd *xhci, in xhci_check_streams_endpoint() argument
3231 return -EINVAL; in xhci_check_streams_endpoint()
3232 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__); in xhci_check_streams_endpoint()
3234 return ret ? ret : -EINVAL; in xhci_check_streams_endpoint()
3235 if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) { in xhci_check_streams_endpoint()
3236 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion" in xhci_check_streams_endpoint()
3238 ep->desc.bEndpointAddress); in xhci_check_streams_endpoint()
3239 return -EINVAL; in xhci_check_streams_endpoint()
3242 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_check_streams_endpoint()
3243 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; in xhci_check_streams_endpoint()
3246 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x " in xhci_check_streams_endpoint()
3248 ep->desc.bEndpointAddress); in xhci_check_streams_endpoint()
3249 xhci_warn(xhci, "Send email to xHCI maintainer and ask for " in xhci_check_streams_endpoint()
3251 return -EINVAL; in xhci_check_streams_endpoint()
3253 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) { in xhci_check_streams_endpoint()
3254 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk " in xhci_check_streams_endpoint()
3256 ep->desc.bEndpointAddress); in xhci_check_streams_endpoint()
3257 return -EINVAL; in xhci_check_streams_endpoint()
3262 static void xhci_calculate_streams_entries(struct xhci_hcd *xhci, in xhci_calculate_streams_entries() argument
3272 * level page entries), but that's an optional feature for xHCI host in xhci_calculate_streams_entries()
3275 max_streams = HCC_MAX_PSA(xhci->hcc_params); in xhci_calculate_streams_entries()
3277 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n", in xhci_calculate_streams_entries()
3288 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci, in xhci_calculate_streams_and_bitmask() argument
3299 ret = xhci_check_streams_endpoint(xhci, udev, in xhci_calculate_streams_and_bitmask()
3300 eps[i], udev->slot_id); in xhci_calculate_streams_and_bitmask()
3304 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp); in xhci_calculate_streams_and_bitmask()
3305 if (max_streams < (*num_streams - 1)) { in xhci_calculate_streams_and_bitmask()
3306 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n", in xhci_calculate_streams_and_bitmask()
3307 eps[i]->desc.bEndpointAddress, in xhci_calculate_streams_and_bitmask()
3312 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc); in xhci_calculate_streams_and_bitmask()
3314 return -EINVAL; in xhci_calculate_streams_and_bitmask()
3320 static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci, in xhci_calculate_no_streams_bitmask() argument
3330 slot_id = udev->slot_id; in xhci_calculate_no_streams_bitmask()
3331 if (!xhci->devs[slot_id]) in xhci_calculate_no_streams_bitmask()
3335 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_calculate_no_streams_bitmask()
3336 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; in xhci_calculate_no_streams_bitmask()
3339 xhci_warn(xhci, "WARN Can't disable streams for " in xhci_calculate_no_streams_bitmask()
3342 eps[i]->desc.bEndpointAddress); in xhci_calculate_no_streams_bitmask()
3348 xhci_warn(xhci, "WARN Can't disable streams for " in xhci_calculate_no_streams_bitmask()
3351 eps[i]->desc.bEndpointAddress); in xhci_calculate_no_streams_bitmask()
3352 xhci_warn(xhci, "WARN xhci_free_streams() called " in xhci_calculate_no_streams_bitmask()
3353 "with non-streams endpoint\n"); in xhci_calculate_no_streams_bitmask()
3356 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc); in xhci_calculate_no_streams_bitmask()
3382 struct xhci_hcd *xhci; in xhci_alloc_streams() local
3393 return -EINVAL; in xhci_alloc_streams()
3396 * stream 0 that is reserved for xHCI usage. in xhci_alloc_streams()
3399 xhci = hcd_to_xhci(hcd); in xhci_alloc_streams()
3400 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n", in xhci_alloc_streams()
3404 if ((xhci->quirks & XHCI_BROKEN_STREAMS) || in xhci_alloc_streams()
3405 HCC_MAX_PSA(xhci->hcc_params) < 4) { in xhci_alloc_streams()
3406 xhci_dbg(xhci, "xHCI controller does not support streams.\n"); in xhci_alloc_streams()
3407 return -ENOSYS; in xhci_alloc_streams()
3410 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags); in xhci_alloc_streams()
3412 return -ENOMEM; in xhci_alloc_streams()
3414 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx); in xhci_alloc_streams()
3416 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_alloc_streams()
3418 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3419 return -ENOMEM; in xhci_alloc_streams()
3426 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_streams()
3427 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps, in xhci_alloc_streams()
3430 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3431 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3435 xhci_warn(xhci, "WARN: endpoints can't handle " in xhci_alloc_streams()
3437 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3438 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3439 return -EINVAL; in xhci_alloc_streams()
3441 vdev = xhci->devs[udev->slot_id]; in xhci_alloc_streams()
3446 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3447 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS; in xhci_alloc_streams()
3449 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3455 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs); in xhci_alloc_streams()
3456 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n", in xhci_alloc_streams()
3460 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3461 max_packet = usb_endpoint_maxp(&eps[i]->desc); in xhci_alloc_streams()
3462 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci, in xhci_alloc_streams()
3466 if (!vdev->eps[ep_index].stream_info) in xhci_alloc_streams()
3477 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3478 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index); in xhci_alloc_streams()
3480 xhci_endpoint_copy(xhci, config_cmd->in_ctx, in xhci_alloc_streams()
3481 vdev->out_ctx, ep_index); in xhci_alloc_streams()
3482 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx, in xhci_alloc_streams()
3483 vdev->eps[ep_index].stream_info); in xhci_alloc_streams()
3488 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx, in xhci_alloc_streams()
3489 vdev->out_ctx, ctrl_ctx, in xhci_alloc_streams()
3493 ret = xhci_configure_endpoint(xhci, udev, config_cmd, in xhci_alloc_streams()
3503 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_streams()
3505 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3506 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; in xhci_alloc_streams()
3507 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n", in xhci_alloc_streams()
3508 udev->slot_id, ep_index); in xhci_alloc_streams()
3509 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS; in xhci_alloc_streams()
3511 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3512 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3515 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3516 xhci_debugfs_create_stream_files(xhci, vdev, ep_index); in xhci_alloc_streams()
3519 return num_streams - 1; in xhci_alloc_streams()
3524 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3525 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); in xhci_alloc_streams()
3526 vdev->eps[ep_index].stream_info = NULL; in xhci_alloc_streams()
3530 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; in xhci_alloc_streams()
3531 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; in xhci_alloc_streams()
3532 xhci_endpoint_zero(xhci, vdev, eps[i]); in xhci_alloc_streams()
3534 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3535 return -ENOMEM; in xhci_alloc_streams()
3549 struct xhci_hcd *xhci; in xhci_free_streams() local
3557 xhci = hcd_to_xhci(hcd); in xhci_free_streams()
3558 vdev = xhci->devs[udev->slot_id]; in xhci_free_streams()
3561 spin_lock_irqsave(&xhci->lock, flags); in xhci_free_streams()
3562 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci, in xhci_free_streams()
3565 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3566 return -EINVAL; in xhci_free_streams()
3573 ep_index = xhci_get_endpoint_index(&eps[0]->desc); in xhci_free_streams()
3574 command = vdev->eps[ep_index].stream_info->free_streams_command; in xhci_free_streams()
3575 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_free_streams()
3577 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3578 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_free_streams()
3580 return -EINVAL; in xhci_free_streams()
3586 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_free_streams()
3587 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); in xhci_free_streams()
3588 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |= in xhci_free_streams()
3591 xhci_endpoint_copy(xhci, command->in_ctx, in xhci_free_streams()
3592 vdev->out_ctx, ep_index); in xhci_free_streams()
3594 &vdev->eps[ep_index]); in xhci_free_streams()
3596 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx, in xhci_free_streams()
3597 vdev->out_ctx, ctrl_ctx, in xhci_free_streams()
3599 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3604 ret = xhci_configure_endpoint(xhci, udev, command, in xhci_free_streams()
3613 spin_lock_irqsave(&xhci->lock, flags); in xhci_free_streams()
3615 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_free_streams()
3616 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); in xhci_free_streams()
3617 vdev->eps[ep_index].stream_info = NULL; in xhci_free_streams()
3621 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS; in xhci_free_streams()
3622 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; in xhci_free_streams()
3624 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3634 * Must be called with xhci->lock held.
3636 void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci, in xhci_free_device_endpoint_resources() argument
3644 if (virt_dev->eps[i].ring) { in xhci_free_device_endpoint_resources()
3649 xhci->num_active_eps -= num_dropped_eps; in xhci_free_device_endpoint_resources()
3651 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_free_device_endpoint_resources()
3655 xhci->num_active_eps); in xhci_free_device_endpoint_resources()
3664 * xhci_address_device(), and then re-set up the configuration. If this is
3666 * settings will be re-installed through the normal bandwidth allocation
3675 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3676 * re-allocate the device.
3683 struct xhci_hcd *xhci; in xhci_discover_or_reset_device() local
3693 xhci = hcd_to_xhci(hcd); in xhci_discover_or_reset_device()
3694 slot_id = udev->slot_id; in xhci_discover_or_reset_device()
3695 virt_dev = xhci->devs[slot_id]; in xhci_discover_or_reset_device()
3697 xhci_dbg(xhci, "The device to be reset with slot ID %u does " in xhci_discover_or_reset_device()
3698 "not exist. Re-allocate the device\n", slot_id); in xhci_discover_or_reset_device()
3703 return -EINVAL; in xhci_discover_or_reset_device()
3706 if (virt_dev->tt_info) in xhci_discover_or_reset_device()
3707 old_active_eps = virt_dev->tt_info->active_eps; in xhci_discover_or_reset_device()
3709 if (virt_dev->udev != udev) { in xhci_discover_or_reset_device()
3712 * Re-allocate the device. in xhci_discover_or_reset_device()
3714 xhci_dbg(xhci, "The device to be reset with slot ID %u does " in xhci_discover_or_reset_device()
3715 "not match the udev. Re-allocate the device\n", in xhci_discover_or_reset_device()
3721 return -EINVAL; in xhci_discover_or_reset_device()
3725 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_discover_or_reset_device()
3726 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) == in xhci_discover_or_reset_device()
3730 if (xhci->quirks & XHCI_ETRON_HOST) { in xhci_discover_or_reset_device()
3732 * Obtaining a new device slot to inform the xHCI host that in xhci_discover_or_reset_device()
3735 ret = xhci_disable_slot(xhci, udev->slot_id); in xhci_discover_or_reset_device()
3736 xhci_free_virt_device(xhci, udev->slot_id); in xhci_discover_or_reset_device()
3742 ret = -EINVAL; in xhci_discover_or_reset_device()
3749 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id); in xhci_discover_or_reset_device()
3756 reset_device_cmd = xhci_alloc_command(xhci, true, GFP_NOIO); in xhci_discover_or_reset_device()
3758 xhci_dbg(xhci, "Couldn't allocate command structure.\n"); in xhci_discover_or_reset_device()
3759 return -ENOMEM; in xhci_discover_or_reset_device()
3763 spin_lock_irqsave(&xhci->lock, flags); in xhci_discover_or_reset_device()
3765 ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id); in xhci_discover_or_reset_device()
3767 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); in xhci_discover_or_reset_device()
3768 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_discover_or_reset_device()
3771 xhci_ring_cmd_db(xhci); in xhci_discover_or_reset_device()
3772 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_discover_or_reset_device()
3775 wait_for_completion(reset_device_cmd->completion); in xhci_discover_or_reset_device()
3781 ret = reset_device_cmd->status; in xhci_discover_or_reset_device()
3785 xhci_warn(xhci, "Timeout waiting for reset device command\n"); in xhci_discover_or_reset_device()
3786 ret = -ETIME; in xhci_discover_or_reset_device()
3790 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n", in xhci_discover_or_reset_device()
3792 xhci_get_slot_state(xhci, virt_dev->out_ctx)); in xhci_discover_or_reset_device()
3793 xhci_dbg(xhci, "Not freeing device rings.\n"); in xhci_discover_or_reset_device()
3798 xhci_dbg(xhci, "Successful reset device command.\n"); in xhci_discover_or_reset_device()
3801 if (xhci_is_vendor_info_code(xhci, ret)) in xhci_discover_or_reset_device()
3803 xhci_warn(xhci, "Unknown completion code %u for " in xhci_discover_or_reset_device()
3805 ret = -EINVAL; in xhci_discover_or_reset_device()
3810 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { in xhci_discover_or_reset_device()
3811 spin_lock_irqsave(&xhci->lock, flags); in xhci_discover_or_reset_device()
3813 xhci_free_device_endpoint_resources(xhci, virt_dev, false); in xhci_discover_or_reset_device()
3814 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_discover_or_reset_device()
3819 struct xhci_virt_ep *ep = &virt_dev->eps[i]; in xhci_discover_or_reset_device()
3821 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_discover_or_reset_device()
3822 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n", in xhci_discover_or_reset_device()
3824 xhci_free_stream_info(xhci, ep->stream_info); in xhci_discover_or_reset_device()
3825 ep->stream_info = NULL; in xhci_discover_or_reset_device()
3826 ep->ep_state &= ~EP_HAS_STREAMS; in xhci_discover_or_reset_device()
3829 if (ep->ring) { in xhci_discover_or_reset_device()
3830 xhci_debugfs_remove_endpoint(xhci, virt_dev, i); in xhci_discover_or_reset_device()
3831 xhci_free_endpoint_ring(xhci, virt_dev, i); in xhci_discover_or_reset_device()
3833 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list)) in xhci_discover_or_reset_device()
3834 xhci_drop_ep_from_interval_table(xhci, in xhci_discover_or_reset_device()
3835 &virt_dev->eps[i].bw_info, in xhci_discover_or_reset_device()
3836 virt_dev->bw_table, in xhci_discover_or_reset_device()
3838 &virt_dev->eps[i], in xhci_discover_or_reset_device()
3839 virt_dev->tt_info); in xhci_discover_or_reset_device()
3840 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info); in xhci_discover_or_reset_device()
3843 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps); in xhci_discover_or_reset_device()
3844 virt_dev->flags = 0; in xhci_discover_or_reset_device()
3848 xhci_free_command(xhci, reset_device_cmd); in xhci_discover_or_reset_device()
3859 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_free_dev() local
3870 if (xhci->quirks & XHCI_RESET_ON_RESUME) in xhci_free_dev()
3871 pm_runtime_put_noidle(hcd->self.controller); in xhci_free_dev()
3877 if (ret <= 0 && ret != -ENODEV) in xhci_free_dev()
3880 virt_dev = xhci->devs[udev->slot_id]; in xhci_free_dev()
3881 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_free_dev()
3886 virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING; in xhci_free_dev()
3887 virt_dev->udev = NULL; in xhci_free_dev()
3888 xhci_disable_slot(xhci, udev->slot_id); in xhci_free_dev()
3890 spin_lock_irqsave(&xhci->lock, flags); in xhci_free_dev()
3891 xhci_free_virt_device(xhci, udev->slot_id); in xhci_free_dev()
3892 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_dev()
3896 int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id) in xhci_disable_slot() argument
3903 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_disable_slot()
3905 return -ENOMEM; in xhci_disable_slot()
3907 xhci_debugfs_remove_slot(xhci, slot_id); in xhci_disable_slot()
3909 spin_lock_irqsave(&xhci->lock, flags); in xhci_disable_slot()
3911 state = readl(&xhci->op_regs->status); in xhci_disable_slot()
3912 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) || in xhci_disable_slot()
3913 (xhci->xhc_state & XHCI_STATE_HALTED)) { in xhci_disable_slot()
3914 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_slot()
3916 return -ENODEV; in xhci_disable_slot()
3919 ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT, in xhci_disable_slot()
3922 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_slot()
3926 xhci_ring_cmd_db(xhci); in xhci_disable_slot()
3927 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_slot()
3929 wait_for_completion(command->completion); in xhci_disable_slot()
3931 if (command->status != COMP_SUCCESS) in xhci_disable_slot()
3932 xhci_warn(xhci, "Unsuccessful disable slot %u command, status %d\n", in xhci_disable_slot()
3933 slot_id, command->status); in xhci_disable_slot()
3935 xhci_free_command(xhci, command); in xhci_disable_slot()
3944 * Must be called with xhci->lock held.
3946 static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci) in xhci_reserve_host_control_ep_resources() argument
3948 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) { in xhci_reserve_host_control_ep_resources()
3949 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_control_ep_resources()
3952 xhci->num_active_eps, xhci->limit_active_eps); in xhci_reserve_host_control_ep_resources()
3953 return -ENOMEM; in xhci_reserve_host_control_ep_resources()
3955 xhci->num_active_eps += 1; in xhci_reserve_host_control_ep_resources()
3956 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_control_ep_resources()
3958 xhci->num_active_eps); in xhci_reserve_host_control_ep_resources()
3969 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_alloc_dev() local
3976 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_alloc_dev()
3980 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_dev()
3981 ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0); in xhci_alloc_dev()
3983 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
3984 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); in xhci_alloc_dev()
3985 xhci_free_command(xhci, command); in xhci_alloc_dev()
3988 xhci_ring_cmd_db(xhci); in xhci_alloc_dev()
3989 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
3991 wait_for_completion(command->completion); in xhci_alloc_dev()
3992 slot_id = command->slot_id; in xhci_alloc_dev()
3994 if (!slot_id || command->status != COMP_SUCCESS) { in xhci_alloc_dev()
3995 xhci_err(xhci, "Error while assigning device slot ID: %s\n", in xhci_alloc_dev()
3996 xhci_trb_comp_code_string(command->status)); in xhci_alloc_dev()
3997 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n", in xhci_alloc_dev()
3999 readl(&xhci->cap_regs->hcs_params1))); in xhci_alloc_dev()
4000 xhci_free_command(xhci, command); in xhci_alloc_dev()
4004 xhci_free_command(xhci, command); in xhci_alloc_dev()
4006 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { in xhci_alloc_dev()
4007 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_dev()
4008 ret = xhci_reserve_host_control_ep_resources(xhci); in xhci_alloc_dev()
4010 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
4011 xhci_warn(xhci, "Not enough host resources, " in xhci_alloc_dev()
4013 xhci->num_active_eps); in xhci_alloc_dev()
4016 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
4022 if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) { in xhci_alloc_dev()
4023 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n"); in xhci_alloc_dev()
4026 vdev = xhci->devs[slot_id]; in xhci_alloc_dev()
4027 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); in xhci_alloc_dev()
4030 udev->slot_id = slot_id; in xhci_alloc_dev()
4032 xhci_debugfs_create_slot(xhci, slot_id); in xhci_alloc_dev()
4038 if (xhci->quirks & XHCI_RESET_ON_RESUME) in xhci_alloc_dev()
4039 pm_runtime_get_noresume(hcd->self.controller); in xhci_alloc_dev()
4046 xhci_disable_slot(xhci, udev->slot_id); in xhci_alloc_dev()
4047 xhci_free_virt_device(xhci, udev->slot_id); in xhci_alloc_dev()
4053 * xhci_setup_device - issues an Address Device command to assign a unique
4069 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_setup_device() local
4075 mutex_lock(&xhci->mutex); in xhci_setup_device()
4077 if (xhci->xhc_state) { /* dying, removing or halted */ in xhci_setup_device()
4078 ret = -ESHUTDOWN; in xhci_setup_device()
4082 if (!udev->slot_id) { in xhci_setup_device()
4083 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4084 "Bad Slot ID %d", udev->slot_id); in xhci_setup_device()
4085 ret = -EINVAL; in xhci_setup_device()
4089 virt_dev = xhci->devs[udev->slot_id]; in xhci_setup_device()
4094 * a zero-dereference was observed once due to virt_dev = 0. in xhci_setup_device()
4097 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n", in xhci_setup_device()
4098 udev->slot_id); in xhci_setup_device()
4099 ret = -EINVAL; in xhci_setup_device()
4102 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_setup_device()
4106 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) == in xhci_setup_device()
4108 xhci_dbg(xhci, "Slot already in default state\n"); in xhci_setup_device()
4113 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_setup_device()
4115 ret = -ENOMEM; in xhci_setup_device()
4119 command->in_ctx = virt_dev->in_ctx; in xhci_setup_device()
4120 command->timeout_ms = timeout_ms; in xhci_setup_device()
4122 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); in xhci_setup_device()
4123 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); in xhci_setup_device()
4125 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_setup_device()
4127 ret = -EINVAL; in xhci_setup_device()
4131 * If this is the first Set Address since device plug-in or in xhci_setup_device()
4132 * virt_device realloaction after a resume with an xHCI power loss, in xhci_setup_device()
4135 if (!slot_ctx->dev_info) in xhci_setup_device()
4136 xhci_setup_addressable_virt_dev(xhci, udev); in xhci_setup_device()
4139 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev); in xhci_setup_device()
4140 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG); in xhci_setup_device()
4141 ctrl_ctx->drop_flags = 0; in xhci_setup_device()
4143 trace_xhci_address_ctx(xhci, virt_dev->in_ctx, in xhci_setup_device()
4144 le32_to_cpu(slot_ctx->dev_info) >> 27); in xhci_setup_device()
4147 spin_lock_irqsave(&xhci->lock, flags); in xhci_setup_device()
4149 ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma, in xhci_setup_device()
4150 udev->slot_id, setup); in xhci_setup_device()
4152 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_setup_device()
4153 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4157 xhci_ring_cmd_db(xhci); in xhci_setup_device()
4158 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_setup_device()
4161 wait_for_completion(command->completion); in xhci_setup_device()
4167 switch (command->status) { in xhci_setup_device()
4170 xhci_warn(xhci, "Timeout while waiting for setup device command\n"); in xhci_setup_device()
4171 ret = -ETIME; in xhci_setup_device()
4175 xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n", in xhci_setup_device()
4176 act, udev->slot_id); in xhci_setup_device()
4177 ret = -EINVAL; in xhci_setup_device()
4180 dev_warn(&udev->dev, "Device not responding to setup %s.\n", act); in xhci_setup_device()
4182 mutex_unlock(&xhci->mutex); in xhci_setup_device()
4183 ret = xhci_disable_slot(xhci, udev->slot_id); in xhci_setup_device()
4184 xhci_free_virt_device(xhci, udev->slot_id); in xhci_setup_device()
4187 xhci_setup_addressable_virt_dev(xhci, udev); in xhci_setup_device()
4189 kfree(command->completion); in xhci_setup_device()
4191 return -EPROTO; in xhci_setup_device()
4193 dev_warn(&udev->dev, in xhci_setup_device()
4195 ret = -ENODEV; in xhci_setup_device()
4198 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4202 xhci_err(xhci, in xhci_setup_device()
4204 act, command->status); in xhci_setup_device()
4205 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1); in xhci_setup_device()
4206 ret = -EINVAL; in xhci_setup_device()
4211 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); in xhci_setup_device()
4212 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4214 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4216 udev->slot_id, in xhci_setup_device()
4217 &xhci->dcbaa->dev_context_ptrs[udev->slot_id], in xhci_setup_device()
4219 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id])); in xhci_setup_device()
4220 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4222 (unsigned long long)virt_dev->out_ctx->dma); in xhci_setup_device()
4223 trace_xhci_address_ctx(xhci, virt_dev->in_ctx, in xhci_setup_device()
4224 le32_to_cpu(slot_ctx->dev_info) >> 27); in xhci_setup_device()
4229 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, in xhci_setup_device()
4230 le32_to_cpu(slot_ctx->dev_info) >> 27); in xhci_setup_device()
4232 ctrl_ctx->add_flags = 0; in xhci_setup_device()
4233 ctrl_ctx->drop_flags = 0; in xhci_setup_device()
4234 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_setup_device()
4235 udev->devaddr = (u8)(le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); in xhci_setup_device()
4237 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4239 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); in xhci_setup_device()
4241 mutex_unlock(&xhci->mutex); in xhci_setup_device()
4243 kfree(command->completion); in xhci_setup_device()
4272 return rhub->ports[port1 - 1]->hw_portnum + 1; in xhci_find_raw_port_number()
4279 static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci, in xhci_change_max_exit_latency() argument
4289 command = xhci_alloc_command_with_ctx(xhci, true, GFP_KERNEL); in xhci_change_max_exit_latency()
4291 return -ENOMEM; in xhci_change_max_exit_latency()
4293 spin_lock_irqsave(&xhci->lock, flags); in xhci_change_max_exit_latency()
4295 virt_dev = xhci->devs[udev->slot_id]; in xhci_change_max_exit_latency()
4299 * xHC was re-initialized. Exit latency will be set later after in xhci_change_max_exit_latency()
4300 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated in xhci_change_max_exit_latency()
4303 if (!virt_dev || max_exit_latency == virt_dev->current_mel) { in xhci_change_max_exit_latency()
4304 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4305 xhci_free_command(xhci, command); in xhci_change_max_exit_latency()
4310 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_change_max_exit_latency()
4312 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4313 xhci_free_command(xhci, command); in xhci_change_max_exit_latency()
4314 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_change_max_exit_latency()
4316 return -ENOMEM; in xhci_change_max_exit_latency()
4319 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx); in xhci_change_max_exit_latency()
4320 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4322 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_change_max_exit_latency()
4323 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx); in xhci_change_max_exit_latency()
4324 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT)); in xhci_change_max_exit_latency()
4325 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency); in xhci_change_max_exit_latency()
4326 slot_ctx->dev_state = 0; in xhci_change_max_exit_latency()
4328 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_change_max_exit_latency()
4332 ret = xhci_configure_endpoint(xhci, udev, command, in xhci_change_max_exit_latency()
4336 spin_lock_irqsave(&xhci->lock, flags); in xhci_change_max_exit_latency()
4337 virt_dev->current_mel = max_exit_latency; in xhci_change_max_exit_latency()
4338 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4341 xhci_free_command(xhci, command); in xhci_change_max_exit_latency()
4353 static int xhci_calculate_hird_besl(struct xhci_hcd *xhci, in xhci_calculate_hird_besl() argument
4360 u2del = HCS_U2_LATENCY(xhci->hcs_params3); in xhci_calculate_hird_besl()
4361 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); in xhci_calculate_hird_besl()
4377 besl_host = (u2del - 51) / 75 + 1; in xhci_calculate_hird_besl()
4395 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); in xhci_calculate_usb2_hw_lpm_params()
4397 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */ in xhci_calculate_usb2_hw_lpm_params()
4398 l1 = udev->l1_params.timeout / 256; in xhci_calculate_usb2_hw_lpm_params()
4412 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_set_usb2_hardware_lpm() local
4421 if (xhci->quirks & XHCI_HW_LPM_DISABLE) in xhci_set_usb2_hardware_lpm()
4422 return -EPERM; in xhci_set_usb2_hardware_lpm()
4424 if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support || in xhci_set_usb2_hardware_lpm()
4425 !udev->lpm_capable) in xhci_set_usb2_hardware_lpm()
4426 return -EPERM; in xhci_set_usb2_hardware_lpm()
4428 if (!udev->parent || udev->parent->parent || in xhci_set_usb2_hardware_lpm()
4429 udev->descriptor.bDeviceClass == USB_CLASS_HUB) in xhci_set_usb2_hardware_lpm()
4430 return -EPERM; in xhci_set_usb2_hardware_lpm()
4432 if (udev->usb2_hw_lpm_capable != 1) in xhci_set_usb2_hardware_lpm()
4433 return -EPERM; in xhci_set_usb2_hardware_lpm()
4435 spin_lock_irqsave(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4437 ports = xhci->usb2_rhub.ports; in xhci_set_usb2_hardware_lpm()
4438 port_num = udev->portnum - 1; in xhci_set_usb2_hardware_lpm()
4439 pm_addr = ports[port_num]->addr + PORTPMSC; in xhci_set_usb2_hardware_lpm()
4441 hlpm_addr = ports[port_num]->addr + PORTHLPMC; in xhci_set_usb2_hardware_lpm()
4443 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n", in xhci_set_usb2_hardware_lpm()
4448 if (udev->usb2_hw_lpm_besl_capable) { in xhci_set_usb2_hardware_lpm()
4451 * systems. See XHCI_DEFAULT_BESL definition in xhci.h in xhci_set_usb2_hardware_lpm()
4453 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); in xhci_set_usb2_hardware_lpm()
4458 hird = udev->l1_params.besl; in xhci_set_usb2_hardware_lpm()
4461 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4463 ret = xhci_change_max_exit_latency(xhci, udev, in xhci_set_usb2_hardware_lpm()
4467 spin_lock_irqsave(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4474 hird = xhci_calculate_hird_besl(xhci, udev); in xhci_set_usb2_hardware_lpm()
4478 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id); in xhci_set_usb2_hardware_lpm()
4490 if (udev->usb2_hw_lpm_besl_capable) { in xhci_set_usb2_hardware_lpm()
4491 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4492 xhci_change_max_exit_latency(xhci, udev, 0); in xhci_set_usb2_hardware_lpm()
4493 readl_poll_timeout(ports[port_num]->addr, pm_val, in xhci_set_usb2_hardware_lpm()
4500 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4508 static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port, in xhci_check_usb2_port_capability() argument
4514 for (i = 0; i < xhci->num_ext_caps; i++) { in xhci_check_usb2_port_capability()
4515 if (xhci->ext_caps[i] & capability) { in xhci_check_usb2_port_capability()
4517 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1; in xhci_check_usb2_port_capability()
4518 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]); in xhci_check_usb2_port_capability()
4529 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_update_device() local
4530 int portnum = udev->portnum - 1; in xhci_update_device()
4532 if (hcd->speed >= HCD_USB3 || !udev->lpm_capable) in xhci_update_device()
4535 /* we only support lpm for non-hub device connected to root hub yet */ in xhci_update_device()
4536 if (!udev->parent || udev->parent->parent || in xhci_update_device()
4537 udev->descriptor.bDeviceClass == USB_CLASS_HUB) in xhci_update_device()
4540 if (xhci->hw_lpm_support == 1 && in xhci_update_device()
4542 xhci, portnum, XHCI_HLC)) { in xhci_update_device()
4543 udev->usb2_hw_lpm_capable = 1; in xhci_update_device()
4544 udev->l1_params.timeout = XHCI_L1_TIMEOUT; in xhci_update_device()
4545 udev->l1_params.besl = XHCI_DEFAULT_BESL; in xhci_update_device()
4546 if (xhci_check_usb2_port_capability(xhci, portnum, in xhci_update_device()
4548 udev->usb2_hw_lpm_besl_capable = 1; in xhci_update_device()
4554 /*---------------------- USB 3.0 Link PM functions ------------------------*/
4556 /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4560 return (1ULL << (desc->bInterval - 1)) * 125 * 1000; in xhci_service_interval_to_ns()
4574 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); in xhci_get_timeout_no_hub_lpm()
4575 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); in xhci_get_timeout_no_hub_lpm()
4580 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); in xhci_get_timeout_no_hub_lpm()
4581 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); in xhci_get_timeout_no_hub_lpm()
4586 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n", in xhci_get_timeout_no_hub_lpm()
4595 dev_dbg(&udev->dev, "Device-initiated %s disabled " in xhci_get_timeout_no_hub_lpm()
4599 dev_dbg(&udev->dev, "Device-initiated %s disabled " in xhci_get_timeout_no_hub_lpm()
4606 * - For control endpoints, U1 system exit latency (SEL) * 3
4607 * - For bulk endpoints, U1 SEL * 5
4608 * - For interrupt endpoints:
4609 * - Notification EPs, U1 SEL * 3
4610 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4611 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4624 timeout_ns = udev->u1_params.sel * 3; in xhci_calculate_intel_u1_timeout()
4627 timeout_ns = udev->u1_params.sel * 5; in xhci_calculate_intel_u1_timeout()
4632 timeout_ns = udev->u1_params.sel * 3; in xhci_calculate_intel_u1_timeout()
4640 if (timeout_ns < udev->u1_params.sel * 2) in xhci_calculate_intel_u1_timeout()
4641 timeout_ns = udev->u1_params.sel * 2; in xhci_calculate_intel_u1_timeout()
4650 /* Returns the hub-encoded U1 timeout value. */
4651 static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci, in xhci_calculate_u1_timeout() argument
4659 if (xhci_service_interval_to_ns(desc) <= udev->u1_params.mel) { in xhci_calculate_u1_timeout()
4660 dev_dbg(&udev->dev, "Disable U1, ESIT shorter than exit latency\n"); in xhci_calculate_u1_timeout()
4665 if (xhci->quirks & (XHCI_INTEL_HOST | XHCI_ZHAOXIN_HOST)) in xhci_calculate_u1_timeout()
4668 timeout_ns = udev->u1_params.sel; in xhci_calculate_u1_timeout()
4679 * USB 3.0 hub, we have to disable hub-initiated U1. in xhci_calculate_u1_timeout()
4683 dev_dbg(&udev->dev, "Hub-initiated U1 disabled " in xhci_calculate_u1_timeout()
4689 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4690 * - largest bInterval of any active periodic endpoint (to avoid going
4692 * - the U2 Exit Latency of the device
4707 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL; in xhci_calculate_intel_u2_timeout()
4714 /* Returns the hub-encoded U2 timeout value. */
4715 static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci, in xhci_calculate_u2_timeout() argument
4723 if (xhci_service_interval_to_ns(desc) <= udev->u2_params.mel) { in xhci_calculate_u2_timeout()
4724 dev_dbg(&udev->dev, "Disable U2, ESIT shorter than exit latency\n"); in xhci_calculate_u2_timeout()
4729 if (xhci->quirks & (XHCI_INTEL_HOST | XHCI_ZHAOXIN_HOST)) in xhci_calculate_u2_timeout()
4732 timeout_ns = udev->u2_params.sel; in xhci_calculate_u2_timeout()
4737 * USB 3.0 hub, we have to disable hub-initiated U2. in xhci_calculate_u2_timeout()
4741 dev_dbg(&udev->dev, "Hub-initiated U2 disabled " in xhci_calculate_u2_timeout()
4746 static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci, in xhci_call_host_update_timeout_for_endpoint() argument
4753 return xhci_calculate_u1_timeout(xhci, udev, desc); in xhci_call_host_update_timeout_for_endpoint()
4755 return xhci_calculate_u2_timeout(xhci, udev, desc); in xhci_call_host_update_timeout_for_endpoint()
4760 static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci, in xhci_update_timeout_for_endpoint() argument
4768 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev, in xhci_update_timeout_for_endpoint()
4771 /* If we found we can't enable hub-initiated LPM, and in xhci_update_timeout_for_endpoint()
4773 * device-initiated LPM as well, then we will disable LPM in xhci_update_timeout_for_endpoint()
4778 return -E2BIG; in xhci_update_timeout_for_endpoint()
4785 static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci, in xhci_update_timeout_for_interface() argument
4793 for (j = 0; j < alt->desc.bNumEndpoints; j++) { in xhci_update_timeout_for_interface()
4794 if (xhci_update_timeout_for_endpoint(xhci, udev, in xhci_update_timeout_for_interface()
4795 &alt->endpoint[j].desc, state, timeout)) in xhci_update_timeout_for_interface()
4796 return -E2BIG; in xhci_update_timeout_for_interface()
4801 static int xhci_check_tier_policy(struct xhci_hcd *xhci, in xhci_check_tier_policy() argument
4805 struct usb_device *parent = udev->parent; in xhci_check_tier_policy()
4809 parent = parent->parent; in xhci_check_tier_policy()
4813 if (xhci->quirks & XHCI_INTEL_HOST && tier > 3) in xhci_check_tier_policy()
4815 if (xhci->quirks & XHCI_ZHAOXIN_HOST && tier > 2) in xhci_check_tier_policy()
4820 dev_dbg(&udev->dev, "Tier policy prevents U1/U2 LPM states for devices at tier %d\n", in xhci_check_tier_policy()
4822 return -E2BIG; in xhci_check_tier_policy()
4826 * If the tier check or timeout setting functions return with a non-zero exit
4833 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_calculate_lpm_timeout() local
4844 dev_warn(&udev->dev, "Can't enable unknown link state %i\n", in xhci_calculate_lpm_timeout()
4852 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc, in xhci_calculate_lpm_timeout()
4856 config = udev->actconfig; in xhci_calculate_lpm_timeout()
4860 for (i = 0; i < config->desc.bNumInterfaces; i++) { in xhci_calculate_lpm_timeout()
4862 struct usb_interface *intf = config->interface[i]; in xhci_calculate_lpm_timeout()
4867 /* Check if any currently bound drivers want hub-initiated LPM in xhci_calculate_lpm_timeout()
4870 if (intf->dev.driver) { in xhci_calculate_lpm_timeout()
4871 driver = to_usb_driver(intf->dev.driver); in xhci_calculate_lpm_timeout()
4872 if (driver && driver->disable_hub_initiated_lpm) { in xhci_calculate_lpm_timeout()
4873 dev_dbg(&udev->dev, "Hub-initiated %s disabled at request of driver %s\n", in xhci_calculate_lpm_timeout()
4874 state_name, driver->name); in xhci_calculate_lpm_timeout()
4883 if (!intf->cur_altsetting) in xhci_calculate_lpm_timeout()
4886 if (xhci_update_timeout_for_interface(xhci, udev, in xhci_calculate_lpm_timeout()
4887 intf->cur_altsetting, in xhci_calculate_lpm_timeout()
4919 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) || in calculate_max_exit_latency()
4921 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000); in calculate_max_exit_latency()
4922 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) || in calculate_max_exit_latency()
4924 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000); in calculate_max_exit_latency()
4928 /* xHCI host controller max exit latency field is only 16 bits wide. */ in calculate_max_exit_latency()
4930 dev_warn(&udev->dev, "Link PM max exit latency of %lluus " in calculate_max_exit_latency()
4932 return -E2BIG; in calculate_max_exit_latency()
4937 /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4941 struct xhci_hcd *xhci; in xhci_enable_usb3_lpm_timeout() local
4947 xhci = hcd_to_xhci(hcd); in xhci_enable_usb3_lpm_timeout()
4948 /* The LPM timeout values are pretty host-controller specific, so don't in xhci_enable_usb3_lpm_timeout()
4949 * enable hub-initiated timeouts unless the vendor has provided in xhci_enable_usb3_lpm_timeout()
4952 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) || in xhci_enable_usb3_lpm_timeout()
4953 !xhci->devs[udev->slot_id]) in xhci_enable_usb3_lpm_timeout()
4956 if (xhci_check_tier_policy(xhci, udev, state) < 0) in xhci_enable_usb3_lpm_timeout()
4960 if (udev->parent && !udev->parent->parent) { in xhci_enable_usb3_lpm_timeout()
4961 port = xhci->usb3_rhub.ports[udev->portnum - 1]; in xhci_enable_usb3_lpm_timeout()
4962 if (port->lpm_incapable) in xhci_enable_usb3_lpm_timeout()
4974 ret = xhci_change_max_exit_latency(xhci, udev, mel); in xhci_enable_usb3_lpm_timeout()
4983 struct xhci_hcd *xhci; in xhci_disable_usb3_lpm_timeout() local
4986 xhci = hcd_to_xhci(hcd); in xhci_disable_usb3_lpm_timeout()
4987 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) || in xhci_disable_usb3_lpm_timeout()
4988 !xhci->devs[udev->slot_id]) in xhci_disable_usb3_lpm_timeout()
4992 return xhci_change_max_exit_latency(xhci, udev, mel); in xhci_disable_usb3_lpm_timeout()
5020 /*-------------------------------------------------------------------------*/
5028 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_update_hub_device() local
5038 if (!hdev->parent) in xhci_update_hub_device()
5041 vdev = xhci->devs[hdev->slot_id]; in xhci_update_hub_device()
5043 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n"); in xhci_update_hub_device()
5044 return -EINVAL; in xhci_update_hub_device()
5047 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags); in xhci_update_hub_device()
5049 return -ENOMEM; in xhci_update_hub_device()
5051 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx); in xhci_update_hub_device()
5053 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_update_hub_device()
5055 xhci_free_command(xhci, config_cmd); in xhci_update_hub_device()
5056 return -ENOMEM; in xhci_update_hub_device()
5059 spin_lock_irqsave(&xhci->lock, flags); in xhci_update_hub_device()
5060 if (hdev->speed == USB_SPEED_HIGH && in xhci_update_hub_device()
5061 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) { in xhci_update_hub_device()
5062 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n"); in xhci_update_hub_device()
5063 xhci_free_command(xhci, config_cmd); in xhci_update_hub_device()
5064 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_update_hub_device()
5065 return -ENOMEM; in xhci_update_hub_device()
5068 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx); in xhci_update_hub_device()
5069 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_update_hub_device()
5070 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx); in xhci_update_hub_device()
5071 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB); in xhci_update_hub_device()
5074 * but it may be already set to 1 when setup an xHCI virtual in xhci_update_hub_device()
5077 if (tt->multi) in xhci_update_hub_device()
5078 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); in xhci_update_hub_device()
5079 else if (hdev->speed == USB_SPEED_FULL) in xhci_update_hub_device()
5080 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT); in xhci_update_hub_device()
5082 if (xhci->hci_version > 0x95) { in xhci_update_hub_device()
5083 xhci_dbg(xhci, "xHCI version %x needs hub " in xhci_update_hub_device()
5085 (unsigned int) xhci->hci_version); in xhci_update_hub_device()
5086 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild)); in xhci_update_hub_device()
5087 /* Set TT think time - convert from ns to FS bit times. in xhci_update_hub_device()
5091 * xHCI 1.0: this field shall be 0 if the device is not a in xhci_update_hub_device()
5092 * High-spped hub. in xhci_update_hub_device()
5094 think_time = tt->think_time; in xhci_update_hub_device()
5096 think_time = (think_time / 666) - 1; in xhci_update_hub_device()
5097 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH) in xhci_update_hub_device()
5098 slot_ctx->tt_info |= in xhci_update_hub_device()
5101 xhci_dbg(xhci, "xHCI version %x doesn't need hub " in xhci_update_hub_device()
5103 (unsigned int) xhci->hci_version); in xhci_update_hub_device()
5105 slot_ctx->dev_state = 0; in xhci_update_hub_device()
5106 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_update_hub_device()
5108 xhci_dbg(xhci, "Set up %s for hub device.\n", in xhci_update_hub_device()
5109 (xhci->hci_version > 0x95) ? in xhci_update_hub_device()
5115 if (xhci->hci_version > 0x95) in xhci_update_hub_device()
5116 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, in xhci_update_hub_device()
5119 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, in xhci_update_hub_device()
5122 xhci_free_command(xhci, config_cmd); in xhci_update_hub_device()
5129 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_get_frame() local
5131 return readl(&xhci->run_regs->microframe_index) >> 3; in xhci_get_frame()
5134 static void xhci_hcd_init_usb2_data(struct xhci_hcd *xhci, struct usb_hcd *hcd) in xhci_hcd_init_usb2_data() argument
5136 xhci->usb2_rhub.hcd = hcd; in xhci_hcd_init_usb2_data()
5137 hcd->speed = HCD_USB2; in xhci_hcd_init_usb2_data()
5138 hcd->self.root_hub->speed = USB_SPEED_HIGH; in xhci_hcd_init_usb2_data()
5140 * USB 2.0 roothub under xHCI has an integrated TT, in xhci_hcd_init_usb2_data()
5144 hcd->has_tt = 1; in xhci_hcd_init_usb2_data()
5147 static void xhci_hcd_init_usb3_data(struct xhci_hcd *xhci, struct usb_hcd *hcd) in xhci_hcd_init_usb3_data() argument
5152 * Early xHCI 1.1 spec did not mention USB 3.1 capable hosts in xhci_hcd_init_usb3_data()
5154 * is a two digit BCD containig minor and sub-minor numbers. in xhci_hcd_init_usb3_data()
5155 * This was later clarified in xHCI 1.2. in xhci_hcd_init_usb3_data()
5160 if (xhci->usb3_rhub.min_rev == 0x1) in xhci_hcd_init_usb3_data()
5163 minor_rev = xhci->usb3_rhub.min_rev / 0x10; in xhci_hcd_init_usb3_data()
5167 hcd->speed = HCD_USB32; in xhci_hcd_init_usb3_data()
5168 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS; in xhci_hcd_init_usb3_data()
5169 hcd->self.root_hub->rx_lanes = 2; in xhci_hcd_init_usb3_data()
5170 hcd->self.root_hub->tx_lanes = 2; in xhci_hcd_init_usb3_data()
5171 hcd->self.root_hub->ssp_rate = USB_SSP_GEN_2x2; in xhci_hcd_init_usb3_data()
5174 hcd->speed = HCD_USB31; in xhci_hcd_init_usb3_data()
5175 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS; in xhci_hcd_init_usb3_data()
5176 hcd->self.root_hub->ssp_rate = USB_SSP_GEN_2x1; in xhci_hcd_init_usb3_data()
5179 xhci_info(xhci, "Host supports USB 3.%x %sSuperSpeed\n", in xhci_hcd_init_usb3_data()
5182 xhci->usb3_rhub.hcd = hcd; in xhci_hcd_init_usb3_data()
5187 struct xhci_hcd *xhci; in xhci_gen_setup() local
5192 struct device *dev = hcd->self.sysdev; in xhci_gen_setup()
5195 /* Accept arbitrarily long scatter-gather lists */ in xhci_gen_setup()
5196 hcd->self.sg_tablesize = ~0; in xhci_gen_setup()
5199 hcd->self.no_sg_constraint = 1; in xhci_gen_setup()
5201 /* XHCI controllers don't stop the ep queue on short packets :| */ in xhci_gen_setup()
5202 hcd->self.no_stop_on_short = 1; in xhci_gen_setup()
5204 xhci = hcd_to_xhci(hcd); in xhci_gen_setup()
5207 xhci_hcd_init_usb3_data(xhci, hcd); in xhci_gen_setup()
5211 mutex_init(&xhci->mutex); in xhci_gen_setup()
5212 xhci->main_hcd = hcd; in xhci_gen_setup()
5213 xhci->cap_regs = hcd->regs; in xhci_gen_setup()
5214 xhci->op_regs = hcd->regs + in xhci_gen_setup()
5215 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)); in xhci_gen_setup()
5216 xhci->run_regs = hcd->regs + in xhci_gen_setup()
5217 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK); in xhci_gen_setup()
5218 /* Cache read-only capability registers */ in xhci_gen_setup()
5219 xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1); in xhci_gen_setup()
5220 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2); in xhci_gen_setup()
5221 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3); in xhci_gen_setup()
5222 xhci->hci_version = HC_VERSION(readl(&xhci->cap_regs->hc_capbase)); in xhci_gen_setup()
5223 xhci->hcc_params = readl(&xhci->cap_regs->hcc_params); in xhci_gen_setup()
5224 if (xhci->hci_version > 0x100) in xhci_gen_setup()
5225 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2); in xhci_gen_setup()
5227 /* xhci-plat or xhci-pci might have set max_interrupters already */ in xhci_gen_setup()
5228 if ((!xhci->max_interrupters) || in xhci_gen_setup()
5229 xhci->max_interrupters > HCS_MAX_INTRS(xhci->hcs_params1)) in xhci_gen_setup()
5230 xhci->max_interrupters = HCS_MAX_INTRS(xhci->hcs_params1); in xhci_gen_setup()
5232 xhci->quirks |= quirks; in xhci_gen_setup()
5235 get_quirks(dev, xhci); in xhci_gen_setup()
5237 /* In xhci controllers which follow xhci 1.0 spec gives a spurious in xhci_gen_setup()
5241 if (xhci->hci_version > 0x96) in xhci_gen_setup()
5242 xhci->quirks |= XHCI_SPURIOUS_SUCCESS; in xhci_gen_setup()
5245 retval = xhci_halt(xhci); in xhci_gen_setup()
5249 xhci_zero_64b_regs(xhci); in xhci_gen_setup()
5251 xhci_dbg(xhci, "Resetting HCD\n"); in xhci_gen_setup()
5253 retval = xhci_reset(xhci, XHCI_RESET_LONG_USEC); in xhci_gen_setup()
5256 xhci_dbg(xhci, "Reset complete\n"); in xhci_gen_setup()
5259 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0) in xhci_gen_setup()
5260 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit in xhci_gen_setup()
5262 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev, in xhci_gen_setup()
5265 if (xhci->quirks & XHCI_NO_64BIT_SUPPORT) in xhci_gen_setup()
5266 xhci->hcc_params &= ~BIT(0); in xhci_gen_setup()
5268 /* Set dma_mask and coherent_dma_mask to 64-bits, in xhci_gen_setup()
5269 * if xHC supports 64-bit addressing */ in xhci_gen_setup()
5270 if (HCC_64BIT_ADDR(xhci->hcc_params) && in xhci_gen_setup()
5272 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n"); in xhci_gen_setup()
5276 * This is to avoid error in cases where a 32-bit USB in xhci_gen_setup()
5277 * controller is used on a 64-bit capable system. in xhci_gen_setup()
5282 xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n"); in xhci_gen_setup()
5286 xhci_dbg(xhci, "Calling HCD init\n"); in xhci_gen_setup()
5291 xhci_dbg(xhci, "Called HCD init\n"); in xhci_gen_setup()
5294 xhci_hcd_init_usb3_data(xhci, hcd); in xhci_gen_setup()
5296 xhci_hcd_init_usb2_data(xhci, hcd); in xhci_gen_setup()
5298 xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%016llx\n", in xhci_gen_setup()
5299 xhci->hcc_params, xhci->hci_version, xhci->quirks); in xhci_gen_setup()
5308 struct xhci_hcd *xhci; in xhci_clear_tt_buffer_complete() local
5314 xhci = hcd_to_xhci(hcd); in xhci_clear_tt_buffer_complete()
5316 spin_lock_irqsave(&xhci->lock, flags); in xhci_clear_tt_buffer_complete()
5317 udev = (struct usb_device *)ep->hcpriv; in xhci_clear_tt_buffer_complete()
5318 slot_id = udev->slot_id; in xhci_clear_tt_buffer_complete()
5319 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_clear_tt_buffer_complete()
5321 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_CLEARING_TT; in xhci_clear_tt_buffer_complete()
5322 xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index); in xhci_clear_tt_buffer_complete()
5323 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_clear_tt_buffer_complete()
5327 .description = "xhci-hcd",
5328 .product_desc = "xHCI Host Controller",
5332 * generic hardware linkage
5398 /* Copy the generic table to drv then apply the overrides */ in xhci_init_driver()
5402 drv->hcd_priv_size += over->extra_priv_size; in xhci_init_driver()
5403 if (over->reset) in xhci_init_driver()
5404 drv->reset = over->reset; in xhci_init_driver()
5405 if (over->start) in xhci_init_driver()
5406 drv->start = over->start; in xhci_init_driver()
5407 if (over->add_endpoint) in xhci_init_driver()
5408 drv->add_endpoint = over->add_endpoint; in xhci_init_driver()
5409 if (over->drop_endpoint) in xhci_init_driver()
5410 drv->drop_endpoint = over->drop_endpoint; in xhci_init_driver()
5411 if (over->check_bandwidth) in xhci_init_driver()
5412 drv->check_bandwidth = over->check_bandwidth; in xhci_init_driver()
5413 if (over->reset_bandwidth) in xhci_init_driver()
5414 drv->reset_bandwidth = over->reset_bandwidth; in xhci_init_driver()
5415 if (over->update_hub_device) in xhci_init_driver()
5416 drv->update_hub_device = over->update_hub_device; in xhci_init_driver()
5417 if (over->hub_control) in xhci_init_driver()
5418 drv->hub_control = over->hub_control; in xhci_init_driver()
5448 return -ENODEV; in xhci_hcd_init()