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()
332 if (!ir || !ir->ir_set || imod_interval > U16_MAX * 250) in xhci_set_interrupter_moderation()
333 return -EINVAL; in xhci_set_interrupter_moderation()
335 imod = readl(&ir->ir_set->irq_control); in xhci_set_interrupter_moderation()
338 writel(imod, &ir->ir_set->irq_control); in xhci_set_interrupter_moderation()
345 struct xhci_hcd *xhci; in compliance_mode_recovery() local
351 xhci = from_timer(xhci, t, comp_mode_recovery_timer); in compliance_mode_recovery()
352 rhub = &xhci->usb3_rhub; in compliance_mode_recovery()
353 hcd = rhub->hcd; in compliance_mode_recovery()
358 for (i = 0; i < rhub->num_ports; i++) { in compliance_mode_recovery()
359 temp = readl(rhub->ports[i]->addr); in compliance_mode_recovery()
365 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in compliance_mode_recovery()
366 "Compliance mode detected->port %d", in compliance_mode_recovery()
368 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in compliance_mode_recovery()
371 if (hcd->state == HC_STATE_SUSPENDED) in compliance_mode_recovery()
378 if (xhci->port_status_u0 != ((1 << rhub->num_ports) - 1)) in compliance_mode_recovery()
379 mod_timer(&xhci->comp_mode_recovery_timer, in compliance_mode_recovery()
384 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
390 * status event is generated when entering compliance mode (per xhci spec),
393 static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci) in compliance_mode_recovery_timer_init() argument
395 xhci->port_status_u0 = 0; in compliance_mode_recovery_timer_init()
396 timer_setup(&xhci->comp_mode_recovery_timer, compliance_mode_recovery, in compliance_mode_recovery_timer_init()
398 xhci->comp_mode_recovery_timer.expires = jiffies + in compliance_mode_recovery_timer_init()
401 add_timer(&xhci->comp_mode_recovery_timer); in compliance_mode_recovery_timer_init()
402 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in compliance_mode_recovery_timer_init()
408 * USB3.0 re-driver and that need the Compliance Mode Quirk.
410 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
421 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard"))) in xhci_compliance_mode_recovery_timer_quirk_check()
433 static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci) in xhci_all_ports_seen_u0() argument
435 return (xhci->port_status_u0 == ((1 << xhci->usb3_rhub.num_ports) - 1)); in xhci_all_ports_seen_u0()
440 * Initialize memory for HCD and xHC (one-time init).
448 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_init() local
451 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init"); in xhci_init()
452 spin_lock_init(&xhci->lock); in xhci_init()
453 if (xhci->hci_version == 0x95 && link_quirk) { in xhci_init()
454 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_init()
456 xhci->quirks |= XHCI_LINK_TRB_QUIRK; in xhci_init()
458 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_init()
459 "xHCI doesn't need link TRB QUIRK"); in xhci_init()
461 retval = xhci_mem_init(xhci, GFP_KERNEL); in xhci_init()
462 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init"); in xhci_init()
466 xhci->quirks |= XHCI_COMP_MODE_QUIRK; in xhci_init()
467 compliance_mode_recovery_timer_init(xhci); in xhci_init()
473 /*-------------------------------------------------------------------------*/
475 static int xhci_run_finished(struct xhci_hcd *xhci) in xhci_run_finished() argument
477 struct xhci_interrupter *ir = xhci->interrupters[0]; in xhci_run_finished()
482 * Enable interrupts before starting the host (xhci 4.2 and 5.5.2). in xhci_run_finished()
485 spin_lock_irqsave(&xhci->lock, flags); in xhci_run_finished()
487 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Enable interrupts"); in xhci_run_finished()
488 temp = readl(&xhci->op_regs->command); in xhci_run_finished()
490 writel(temp, &xhci->op_regs->command); in xhci_run_finished()
492 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Enable primary interrupter"); in xhci_run_finished()
495 if (xhci_start(xhci)) { in xhci_run_finished()
496 xhci_halt(xhci); in xhci_run_finished()
497 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_run_finished()
498 return -ENODEV; in xhci_run_finished()
501 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING; in xhci_run_finished()
503 if (xhci->quirks & XHCI_NEC_HOST) in xhci_run_finished()
504 xhci_ring_cmd_db(xhci); in xhci_run_finished()
506 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_run_finished()
521 * Setup MSI-X vectors and enable interrupts.
527 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_run() local
528 struct xhci_interrupter *ir = xhci->interrupters[0]; in xhci_run()
529 /* Start the xHCI host controller running only after the USB 2.0 roothub in xhci_run()
533 hcd->uses_new_polling = 1; in xhci_run()
535 return xhci_run_finished(xhci); in xhci_run()
537 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run"); in xhci_run()
539 temp_64 = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); in xhci_run()
541 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run()
544 xhci_set_interrupter_moderation(ir, xhci->imod_interval); in xhci_run()
546 if (xhci->quirks & XHCI_NEC_HOST) { in xhci_run()
549 command = xhci_alloc_command(xhci, false, GFP_KERNEL); in xhci_run()
551 return -ENOMEM; in xhci_run()
553 ret = xhci_queue_vendor_command(xhci, command, 0, 0, 0, in xhci_run()
556 xhci_free_command(xhci, command); in xhci_run()
558 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_run()
561 xhci_create_dbc_dev(xhci); in xhci_run()
563 xhci_debugfs_init(xhci); in xhci_run()
565 if (xhci_has_one_roothub(xhci)) in xhci_run()
566 return xhci_run_finished(xhci); in xhci_run()
568 set_bit(HCD_FLAG_DEFER_RH_REGISTER, &hcd->flags); in xhci_run()
575 * Stop xHCI driver.
586 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_stop() local
587 struct xhci_interrupter *ir = xhci->interrupters[0]; in xhci_stop()
589 mutex_lock(&xhci->mutex); in xhci_stop()
593 mutex_unlock(&xhci->mutex); in xhci_stop()
597 xhci_remove_dbc_dev(xhci); in xhci_stop()
599 spin_lock_irq(&xhci->lock); in xhci_stop()
600 xhci->xhc_state |= XHCI_STATE_HALTED; in xhci_stop()
601 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED; in xhci_stop()
602 xhci_halt(xhci); in xhci_stop()
603 xhci_reset(xhci, XHCI_RESET_SHORT_USEC); in xhci_stop()
604 spin_unlock_irq(&xhci->lock); in xhci_stop()
607 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && in xhci_stop()
608 (!(xhci_all_ports_seen_u0(xhci)))) { in xhci_stop()
609 del_timer_sync(&xhci->comp_mode_recovery_timer); in xhci_stop()
610 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_stop()
615 if (xhci->quirks & XHCI_AMD_PLL_FIX) in xhci_stop()
618 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_stop()
620 temp = readl(&xhci->op_regs->status); in xhci_stop()
621 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status); in xhci_stop()
624 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory"); in xhci_stop()
625 xhci_mem_cleanup(xhci); in xhci_stop()
626 xhci_debugfs_exit(xhci); in xhci_stop()
627 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_stop()
628 "xhci_stop completed - status = %x", in xhci_stop()
629 readl(&xhci->op_regs->status)); in xhci_stop()
630 mutex_unlock(&xhci->mutex); in xhci_stop()
635 * Shutdown HC (not bus-specific)
645 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_shutdown() local
647 if (xhci->quirks & XHCI_SPURIOUS_REBOOT) in xhci_shutdown()
648 usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev)); in xhci_shutdown()
651 xhci_dbg(xhci, "%s: stopping usb%d port polling.\n", in xhci_shutdown()
652 __func__, hcd->self.busnum); in xhci_shutdown()
653 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_shutdown()
654 del_timer_sync(&hcd->rh_timer); in xhci_shutdown()
656 if (xhci->shared_hcd) { in xhci_shutdown()
657 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_shutdown()
658 del_timer_sync(&xhci->shared_hcd->rh_timer); in xhci_shutdown()
661 spin_lock_irq(&xhci->lock); in xhci_shutdown()
662 xhci_halt(xhci); in xhci_shutdown()
666 * firmware delay in ADL-P PCH if port are left in U3 at shutdown in xhci_shutdown()
668 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP || in xhci_shutdown()
669 xhci->quirks & XHCI_RESET_TO_DEFAULT) in xhci_shutdown()
670 xhci_reset(xhci, XHCI_RESET_SHORT_USEC); in xhci_shutdown()
672 spin_unlock_irq(&xhci->lock); in xhci_shutdown()
674 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_shutdown()
675 "xhci_shutdown completed - status = %x", in xhci_shutdown()
676 readl(&xhci->op_regs->status)); in xhci_shutdown()
681 static void xhci_save_registers(struct xhci_hcd *xhci) in xhci_save_registers() argument
686 xhci->s3.command = readl(&xhci->op_regs->command); in xhci_save_registers()
687 xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification); in xhci_save_registers()
688 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); in xhci_save_registers()
689 xhci->s3.config_reg = readl(&xhci->op_regs->config_reg); in xhci_save_registers()
693 for (i = 0; i < xhci->max_interrupters; i++) { in xhci_save_registers()
694 ir = xhci->interrupters[i]; in xhci_save_registers()
698 ir->s3_erst_size = readl(&ir->ir_set->erst_size); in xhci_save_registers()
699 ir->s3_erst_base = xhci_read_64(xhci, &ir->ir_set->erst_base); in xhci_save_registers()
700 ir->s3_erst_dequeue = xhci_read_64(xhci, &ir->ir_set->erst_dequeue); in xhci_save_registers()
701 ir->s3_irq_pending = readl(&ir->ir_set->irq_pending); in xhci_save_registers()
702 ir->s3_irq_control = readl(&ir->ir_set->irq_control); in xhci_save_registers()
706 static void xhci_restore_registers(struct xhci_hcd *xhci) in xhci_restore_registers() argument
711 writel(xhci->s3.command, &xhci->op_regs->command); in xhci_restore_registers()
712 writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification); in xhci_restore_registers()
713 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr); in xhci_restore_registers()
714 writel(xhci->s3.config_reg, &xhci->op_regs->config_reg); in xhci_restore_registers()
717 for (i = 0; i < xhci->max_interrupters; i++) { in xhci_restore_registers()
718 ir = xhci->interrupters[i]; in xhci_restore_registers()
722 writel(ir->s3_erst_size, &ir->ir_set->erst_size); in xhci_restore_registers()
723 xhci_write_64(xhci, ir->s3_erst_base, &ir->ir_set->erst_base); in xhci_restore_registers()
724 xhci_write_64(xhci, ir->s3_erst_dequeue, &ir->ir_set->erst_dequeue); in xhci_restore_registers()
725 writel(ir->s3_irq_pending, &ir->ir_set->irq_pending); in xhci_restore_registers()
726 writel(ir->s3_irq_control, &ir->ir_set->irq_control); in xhci_restore_registers()
730 static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci) in xhci_set_cmd_ring_deq() argument
735 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring); in xhci_set_cmd_ring_deq()
737 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg, in xhci_set_cmd_ring_deq()
738 xhci->cmd_ring->dequeue) & in xhci_set_cmd_ring_deq()
740 xhci->cmd_ring->cycle_state; in xhci_set_cmd_ring_deq()
741 xhci_dbg_trace(xhci, trace_xhci_dbg_init, in xhci_set_cmd_ring_deq()
744 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring); in xhci_set_cmd_ring_deq()
751 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
754 * middle of the ring (TRBs are 16-byte aligned).
756 static void xhci_clear_command_ring(struct xhci_hcd *xhci) in xhci_clear_command_ring() argument
761 ring = xhci->cmd_ring; in xhci_clear_command_ring()
762 seg = ring->deq_seg; in xhci_clear_command_ring()
764 memset(seg->trbs, 0, in xhci_clear_command_ring()
765 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1)); in xhci_clear_command_ring()
766 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &= in xhci_clear_command_ring()
768 seg = seg->next; in xhci_clear_command_ring()
769 } while (seg != ring->deq_seg); in xhci_clear_command_ring()
772 ring->deq_seg = ring->first_seg; in xhci_clear_command_ring()
773 ring->dequeue = ring->first_seg->trbs; in xhci_clear_command_ring()
774 ring->enq_seg = ring->deq_seg; in xhci_clear_command_ring()
775 ring->enqueue = ring->dequeue; in xhci_clear_command_ring()
777 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1; in xhci_clear_command_ring()
782 ring->cycle_state = 1; in xhci_clear_command_ring()
786 * Yes, this will need to be re-written after resume, but we're paranoid in xhci_clear_command_ring()
791 xhci_set_cmd_ring_deq(xhci); in xhci_clear_command_ring()
799 * Internal wake causes immediate xHCI wake after suspend. PORT_CSC write done
803 static void xhci_disable_hub_port_wake(struct xhci_hcd *xhci, in xhci_disable_hub_port_wake() argument
811 spin_lock_irqsave(&xhci->lock, flags); in xhci_disable_hub_port_wake()
813 for (i = 0; i < rhub->num_ports; i++) { in xhci_disable_hub_port_wake()
814 portsc = readl(rhub->ports[i]->addr); in xhci_disable_hub_port_wake()
827 writel(t2, rhub->ports[i]->addr); in xhci_disable_hub_port_wake()
828 xhci_dbg(xhci, "config port %d-%d wake bits, portsc: 0x%x, write: 0x%x\n", in xhci_disable_hub_port_wake()
829 rhub->hcd->self.busnum, i + 1, portsc, t2); in xhci_disable_hub_port_wake()
832 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_hub_port_wake()
835 static bool xhci_pending_portevent(struct xhci_hcd *xhci) in xhci_pending_portevent() argument
842 status = readl(&xhci->op_regs->status); in xhci_pending_portevent()
848 * being written to the Event Ring. See note in xhci 1.1 section 4.19.2. in xhci_pending_portevent()
851 port_index = xhci->usb2_rhub.num_ports; in xhci_pending_portevent()
852 ports = xhci->usb2_rhub.ports; in xhci_pending_portevent()
853 while (port_index--) { in xhci_pending_portevent()
854 portsc = readl(ports[port_index]->addr); in xhci_pending_portevent()
859 port_index = xhci->usb3_rhub.num_ports; in xhci_pending_portevent()
860 ports = xhci->usb3_rhub.ports; in xhci_pending_portevent()
861 while (port_index--) { in xhci_pending_portevent()
862 portsc = readl(ports[port_index]->addr); in xhci_pending_portevent()
871 * Stop HC (not bus-specific)
876 int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup) in xhci_suspend() argument
880 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_suspend()
884 if (!hcd->state) in xhci_suspend()
887 if (hcd->state != HC_STATE_SUSPENDED || in xhci_suspend()
888 (xhci->shared_hcd && xhci->shared_hcd->state != HC_STATE_SUSPENDED)) in xhci_suspend()
889 return -EINVAL; in xhci_suspend()
892 xhci_disable_hub_port_wake(xhci, &xhci->usb3_rhub, do_wakeup); in xhci_suspend()
893 xhci_disable_hub_port_wake(xhci, &xhci->usb2_rhub, do_wakeup); in xhci_suspend()
898 xhci_dbc_suspend(xhci); in xhci_suspend()
901 xhci_dbg(xhci, "%s: stopping usb%d port polling.\n", in xhci_suspend()
902 __func__, hcd->self.busnum); in xhci_suspend()
903 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_suspend()
904 del_timer_sync(&hcd->rh_timer); in xhci_suspend()
905 if (xhci->shared_hcd) { in xhci_suspend()
906 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_suspend()
907 del_timer_sync(&xhci->shared_hcd->rh_timer); in xhci_suspend()
910 if (xhci->quirks & XHCI_SUSPEND_DELAY) in xhci_suspend()
913 spin_lock_irq(&xhci->lock); in xhci_suspend()
914 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); in xhci_suspend()
915 if (xhci->shared_hcd) in xhci_suspend()
916 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags); in xhci_suspend()
921 command = readl(&xhci->op_regs->command); in xhci_suspend()
923 writel(command, &xhci->op_regs->command); in xhci_suspend()
926 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1; in xhci_suspend()
928 if (xhci_handshake(&xhci->op_regs->status, in xhci_suspend()
930 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n"); in xhci_suspend()
931 spin_unlock_irq(&xhci->lock); in xhci_suspend()
932 return -ETIMEDOUT; in xhci_suspend()
934 xhci_clear_command_ring(xhci); in xhci_suspend()
937 xhci_save_registers(xhci); in xhci_suspend()
940 command = readl(&xhci->op_regs->command); in xhci_suspend()
942 writel(command, &xhci->op_regs->command); in xhci_suspend()
943 xhci->broken_suspend = 0; in xhci_suspend()
944 if (xhci_handshake(&xhci->op_regs->status, in xhci_suspend()
952 * if SRE and HCE bits are not set (as per xhci in xhci_suspend()
955 res = readl(&xhci->op_regs->status); in xhci_suspend()
956 if ((xhci->quirks & XHCI_SNPS_BROKEN_SUSPEND) && in xhci_suspend()
959 xhci->broken_suspend = 1; in xhci_suspend()
961 xhci_warn(xhci, "WARN: xHC save state timeout\n"); in xhci_suspend()
962 spin_unlock_irq(&xhci->lock); in xhci_suspend()
963 return -ETIMEDOUT; in xhci_suspend()
966 spin_unlock_irq(&xhci->lock); in xhci_suspend()
969 * Deleting Compliance Mode Recovery Timer because the xHCI Host in xhci_suspend()
972 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && in xhci_suspend()
973 (!(xhci_all_ports_seen_u0(xhci)))) { in xhci_suspend()
974 del_timer_sync(&xhci->comp_mode_recovery_timer); in xhci_suspend()
975 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_suspend()
985 * start xHC (not bus-specific)
990 int xhci_resume(struct xhci_hcd *xhci, pm_message_t msg) in xhci_resume() argument
994 struct usb_hcd *hcd = xhci_to_hcd(xhci); in xhci_resume()
1001 if (!hcd->state) in xhci_resume()
1008 if (time_before(jiffies, xhci->usb2_rhub.bus_state.next_statechange) || in xhci_resume()
1009 time_before(jiffies, xhci->usb3_rhub.bus_state.next_statechange)) in xhci_resume()
1012 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); in xhci_resume()
1013 if (xhci->shared_hcd) in xhci_resume()
1014 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags); in xhci_resume()
1016 spin_lock_irq(&xhci->lock); in xhci_resume()
1018 if (hibernated || xhci->quirks & XHCI_RESET_ON_RESUME || xhci->broken_suspend) in xhci_resume()
1026 retval = xhci_handshake(&xhci->op_regs->status, in xhci_resume()
1029 xhci_warn(xhci, "Controller not ready at resume %d\n", in xhci_resume()
1031 spin_unlock_irq(&xhci->lock); in xhci_resume()
1035 xhci_restore_registers(xhci); in xhci_resume()
1037 xhci_set_cmd_ring_deq(xhci); in xhci_resume()
1040 command = readl(&xhci->op_regs->command); in xhci_resume()
1042 writel(command, &xhci->op_regs->command); in xhci_resume()
1045 * restore so setting the timeout to 100ms. Xhci specification in xhci_resume()
1048 if (xhci_handshake(&xhci->op_regs->status, in xhci_resume()
1050 xhci_warn(xhci, "WARN: xHC restore state timeout\n"); in xhci_resume()
1051 spin_unlock_irq(&xhci->lock); in xhci_resume()
1052 return -ETIMEDOUT; in xhci_resume()
1056 temp = readl(&xhci->op_regs->status); in xhci_resume()
1058 /* re-initialize the HC on Restore Error, or Host Controller Error */ in xhci_resume()
1060 !(xhci->xhc_state & XHCI_STATE_REMOVING)) { in xhci_resume()
1062 if (!xhci->broken_suspend) in xhci_resume()
1063 xhci_warn(xhci, "xHC error in resume, USBSTS 0x%x, Reinit\n", temp); in xhci_resume()
1067 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && in xhci_resume()
1068 !(xhci_all_ports_seen_u0(xhci))) { in xhci_resume()
1069 del_timer_sync(&xhci->comp_mode_recovery_timer); in xhci_resume()
1070 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_resume()
1075 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub); in xhci_resume()
1076 if (xhci->shared_hcd) in xhci_resume()
1077 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub); in xhci_resume()
1079 xhci_dbg(xhci, "Stop HCD\n"); in xhci_resume()
1080 xhci_halt(xhci); in xhci_resume()
1081 xhci_zero_64b_regs(xhci); in xhci_resume()
1082 retval = xhci_reset(xhci, XHCI_RESET_LONG_USEC); in xhci_resume()
1083 spin_unlock_irq(&xhci->lock); in xhci_resume()
1087 xhci_dbg(xhci, "// Disabling event ring interrupts\n"); in xhci_resume()
1088 temp = readl(&xhci->op_regs->status); in xhci_resume()
1089 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status); in xhci_resume()
1090 xhci_disable_interrupter(xhci->interrupters[0]); in xhci_resume()
1092 xhci_dbg(xhci, "cleaning up memory\n"); in xhci_resume()
1093 xhci_mem_cleanup(xhci); in xhci_resume()
1094 xhci_debugfs_exit(xhci); in xhci_resume()
1095 xhci_dbg(xhci, "xhci_stop completed - status = %x\n", in xhci_resume()
1096 readl(&xhci->op_regs->status)); in xhci_resume()
1102 xhci_dbg(xhci, "Initialize the xhci_hcd\n"); in xhci_resume()
1108 xhci_dbg(xhci, "Start the primary HCD\n"); in xhci_resume()
1110 if (!retval && xhci->shared_hcd) { in xhci_resume()
1111 xhci_dbg(xhci, "Start the secondary HCD\n"); in xhci_resume()
1112 retval = xhci_run(xhci->shared_hcd); in xhci_resume()
1120 hcd->state = HC_STATE_SUSPENDED; in xhci_resume()
1122 if (xhci->shared_hcd) { in xhci_resume()
1123 xhci->shared_hcd->state = HC_STATE_SUSPENDED; in xhci_resume()
1124 usb_hcd_resume_root_hub(xhci->shared_hcd); in xhci_resume()
1132 command = readl(&xhci->op_regs->command); in xhci_resume()
1134 writel(command, &xhci->op_regs->command); in xhci_resume()
1135 xhci_handshake(&xhci->op_regs->status, STS_HALT, in xhci_resume()
1147 spin_unlock_irq(&xhci->lock); in xhci_resume()
1149 xhci_dbc_resume(xhci); in xhci_resume()
1158 if (xhci->usb3_rhub.bus_state.suspended_ports || in xhci_resume()
1159 xhci->usb3_rhub.bus_state.bus_suspended) in xhci_resume()
1162 pending_portevent = xhci_pending_portevent(xhci); in xhci_resume()
1167 pending_portevent = xhci_pending_portevent(xhci); in xhci_resume()
1171 if (xhci->shared_hcd) in xhci_resume()
1172 usb_hcd_resume_root_hub(xhci->shared_hcd); in xhci_resume()
1179 * be re-initialized Always after a system resume. Ports are subject in xhci_resume()
1183 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running) in xhci_resume()
1184 compliance_mode_recovery_timer_init(xhci); in xhci_resume()
1186 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL) in xhci_resume()
1187 usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller)); in xhci_resume()
1189 /* Re-enable port polling. */ in xhci_resume()
1190 xhci_dbg(xhci, "%s: starting usb%d port polling.\n", in xhci_resume()
1191 __func__, hcd->self.busnum); in xhci_resume()
1192 if (xhci->shared_hcd) { in xhci_resume()
1193 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); in xhci_resume()
1194 usb_hcd_poll_rh_status(xhci->shared_hcd); in xhci_resume()
1196 set_bit(HCD_FLAG_POLL_RH, &hcd->flags); in xhci_resume()
1204 /*-------------------------------------------------------------------------*/
1214 buf_len = urb->transfer_buffer_length; in xhci_map_temp_buffer()
1217 dev_to_node(hcd->self.sysdev)); in xhci_map_temp_buffer()
1219 return -ENOMEM; in xhci_map_temp_buffer()
1222 sg_pcopy_to_buffer(urb->sg, urb->num_sgs, in xhci_map_temp_buffer()
1225 urb->transfer_buffer = temp; in xhci_map_temp_buffer()
1226 urb->transfer_dma = dma_map_single(hcd->self.sysdev, in xhci_map_temp_buffer()
1227 urb->transfer_buffer, in xhci_map_temp_buffer()
1228 urb->transfer_buffer_length, in xhci_map_temp_buffer()
1231 if (dma_mapping_error(hcd->self.sysdev, in xhci_map_temp_buffer()
1232 urb->transfer_dma)) { in xhci_map_temp_buffer()
1233 ret = -EAGAIN; in xhci_map_temp_buffer()
1236 urb->transfer_flags |= URB_DMA_MAP_SINGLE; in xhci_map_temp_buffer()
1253 tail_sg = urb->sg; in xhci_urb_temp_buffer_required()
1254 max_pkt = usb_endpoint_maxp(&urb->ep->desc); in xhci_urb_temp_buffer_required()
1256 if (!urb->num_sgs) in xhci_urb_temp_buffer_required()
1259 if (urb->dev->speed >= USB_SPEED_SUPER) in xhci_urb_temp_buffer_required()
1264 if (urb->transfer_buffer_length != 0 && in xhci_urb_temp_buffer_required()
1265 !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { in xhci_urb_temp_buffer_required()
1266 for_each_sg(urb->sg, sg, urb->num_sgs, i) { in xhci_urb_temp_buffer_required()
1267 len = len + sg->length; in xhci_urb_temp_buffer_required()
1268 if (i > trb_size - 2) { in xhci_urb_temp_buffer_required()
1269 len = len - tail_sg->length; in xhci_urb_temp_buffer_required()
1290 buf_len = urb->transfer_buffer_length; in xhci_unmap_temp_buf()
1293 (urb->transfer_flags & URB_DMA_MAP_SINGLE)) in xhci_unmap_temp_buf()
1294 dma_unmap_single(hcd->self.sysdev, in xhci_unmap_temp_buf()
1295 urb->transfer_dma, in xhci_unmap_temp_buf()
1296 urb->transfer_buffer_length, in xhci_unmap_temp_buf()
1300 len = sg_pcopy_from_buffer(urb->sg, urb->num_sgs, in xhci_unmap_temp_buf()
1301 urb->transfer_buffer, in xhci_unmap_temp_buf()
1307 urb->actual_length = len; in xhci_unmap_temp_buf()
1310 urb->transfer_flags &= ~URB_DMA_MAP_SINGLE; in xhci_unmap_temp_buf()
1311 kfree(urb->transfer_buffer); in xhci_unmap_temp_buf()
1312 urb->transfer_buffer = NULL; in xhci_unmap_temp_buf()
1324 struct xhci_hcd *xhci; in xhci_map_urb_for_dma() local
1326 xhci = hcd_to_xhci(hcd); in xhci_map_urb_for_dma()
1331 if (xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) { in xhci_map_urb_for_dma()
1340 struct xhci_hcd *xhci; in xhci_unmap_urb_for_dma() local
1343 xhci = hcd_to_xhci(hcd); in xhci_unmap_urb_for_dma()
1345 if (urb->num_sgs && (urb->transfer_flags & URB_DMA_MAP_SINGLE)) in xhci_unmap_urb_for_dma()
1348 if ((xhci->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK) && unmap_temp_buf) in xhci_unmap_urb_for_dma()
1355 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1359 * Index = (epnum * 2) + direction - 1,
1362 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1371 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1; in xhci_get_endpoint_index()
1377 * address from the XHCI endpoint index.
1403 return fls(added_ctxs) - 1; in xhci_last_valid_endpoint()
1407 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1412 struct xhci_hcd *xhci; in xhci_check_args() local
1416 pr_debug("xHCI %s called with invalid args\n", func); in xhci_check_args()
1417 return -EINVAL; in xhci_check_args()
1419 if (!udev->parent) { in xhci_check_args()
1420 pr_debug("xHCI %s called for root hub\n", func); in xhci_check_args()
1424 xhci = hcd_to_xhci(hcd); in xhci_check_args()
1426 if (!udev->slot_id || !xhci->devs[udev->slot_id]) { in xhci_check_args()
1427 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n", in xhci_check_args()
1429 return -EINVAL; in xhci_check_args()
1432 virt_dev = xhci->devs[udev->slot_id]; in xhci_check_args()
1433 if (virt_dev->udev != udev) { in xhci_check_args()
1434 xhci_dbg(xhci, "xHCI %s called with udev and " in xhci_check_args()
1436 return -EINVAL; in xhci_check_args()
1440 if (xhci->xhc_state & XHCI_STATE_HALTED) in xhci_check_args()
1441 return -ENODEV; in xhci_check_args()
1446 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1456 static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id, in xhci_check_maxpacket() argument
1467 out_ctx = xhci->devs[slot_id]->out_ctx; in xhci_check_maxpacket()
1468 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); in xhci_check_maxpacket()
1469 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2)); in xhci_check_maxpacket()
1470 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc); in xhci_check_maxpacket()
1472 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1474 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1477 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1478 "Max packet size in xHCI HW = %d", in xhci_check_maxpacket()
1480 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_check_maxpacket()
1484 /* FIXME: This won't work if a non-default control endpoint in xhci_check_maxpacket()
1488 command = xhci_alloc_command(xhci, true, mem_flags); in xhci_check_maxpacket()
1490 return -ENOMEM; in xhci_check_maxpacket()
1492 command->in_ctx = xhci->devs[slot_id]->in_ctx; in xhci_check_maxpacket()
1493 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_check_maxpacket()
1495 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_check_maxpacket()
1497 ret = -ENOMEM; in xhci_check_maxpacket()
1501 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx, in xhci_check_maxpacket()
1502 xhci->devs[slot_id]->out_ctx, ep_index); in xhci_check_maxpacket()
1504 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); in xhci_check_maxpacket()
1505 ep_ctx->ep_info &= cpu_to_le32(~EP_STATE_MASK);/* must clear */ in xhci_check_maxpacket()
1506 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK); in xhci_check_maxpacket()
1507 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size)); in xhci_check_maxpacket()
1509 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG); in xhci_check_maxpacket()
1510 ctrl_ctx->drop_flags = 0; in xhci_check_maxpacket()
1512 ret = xhci_configure_endpoint(xhci, urb->dev, command, in xhci_check_maxpacket()
1518 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG); in xhci_check_maxpacket()
1520 kfree(command->completion); in xhci_check_maxpacket()
1527 * non-error returns are a promise to giveback() the urb later
1532 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_urb_enqueue() local
1541 return -EINVAL; in xhci_urb_enqueue()
1542 ret = xhci_check_args(hcd, urb->dev, urb->ep, in xhci_urb_enqueue()
1545 return ret ? ret : -EINVAL; in xhci_urb_enqueue()
1547 slot_id = urb->dev->slot_id; in xhci_urb_enqueue()
1548 ep_index = xhci_get_endpoint_index(&urb->ep->desc); in xhci_urb_enqueue()
1549 ep_state = &xhci->devs[slot_id]->eps[ep_index].ep_state; in xhci_urb_enqueue()
1552 return -ESHUTDOWN; in xhci_urb_enqueue()
1554 if (xhci->devs[slot_id]->flags & VDEV_PORT_ERROR) { in xhci_urb_enqueue()
1555 xhci_dbg(xhci, "Can't queue urb, port error, link inactive\n"); in xhci_urb_enqueue()
1556 return -ENODEV; in xhci_urb_enqueue()
1559 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) in xhci_urb_enqueue()
1560 num_tds = urb->number_of_packets; in xhci_urb_enqueue()
1561 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) && in xhci_urb_enqueue()
1562 urb->transfer_buffer_length > 0 && in xhci_urb_enqueue()
1563 urb->transfer_flags & URB_ZERO_PACKET && in xhci_urb_enqueue()
1564 !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc))) in xhci_urb_enqueue()
1571 return -ENOMEM; in xhci_urb_enqueue()
1573 urb_priv->num_tds = num_tds; in xhci_urb_enqueue()
1574 urb_priv->num_tds_done = 0; in xhci_urb_enqueue()
1575 urb->hcpriv = urb_priv; in xhci_urb_enqueue()
1579 if (usb_endpoint_xfer_control(&urb->ep->desc)) { in xhci_urb_enqueue()
1583 if (urb->dev->speed == USB_SPEED_FULL) { in xhci_urb_enqueue()
1584 ret = xhci_check_maxpacket(xhci, slot_id, in xhci_urb_enqueue()
1588 urb->hcpriv = NULL; in xhci_urb_enqueue()
1594 spin_lock_irqsave(&xhci->lock, flags); in xhci_urb_enqueue()
1596 if (xhci->xhc_state & XHCI_STATE_DYING) { in xhci_urb_enqueue()
1597 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for non-responsive xHCI host.\n", in xhci_urb_enqueue()
1598 urb->ep->desc.bEndpointAddress, urb); in xhci_urb_enqueue()
1599 ret = -ESHUTDOWN; in xhci_urb_enqueue()
1603 xhci_warn(xhci, "WARN: Can't enqueue URB, ep in streams transition state %x\n", in xhci_urb_enqueue()
1605 ret = -EINVAL; in xhci_urb_enqueue()
1609 xhci_warn(xhci, "Can't enqueue URB while manually clearing toggle\n"); in xhci_urb_enqueue()
1610 ret = -EINVAL; in xhci_urb_enqueue()
1614 switch (usb_endpoint_type(&urb->ep->desc)) { in xhci_urb_enqueue()
1617 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1621 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1625 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1629 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb, in xhci_urb_enqueue()
1636 urb->hcpriv = NULL; in xhci_urb_enqueue()
1638 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_urb_enqueue()
1658 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1660 * endpoint command, as noted in the xHCI 0.95 errata.
1678 struct xhci_hcd *xhci; in xhci_urb_dequeue() local
1687 xhci = hcd_to_xhci(hcd); in xhci_urb_dequeue()
1688 spin_lock_irqsave(&xhci->lock, flags); in xhci_urb_dequeue()
1698 vdev = xhci->devs[urb->dev->slot_id]; in xhci_urb_dequeue()
1699 urb_priv = urb->hcpriv; in xhci_urb_dequeue()
1703 ep_index = xhci_get_endpoint_index(&urb->ep->desc); in xhci_urb_dequeue()
1704 ep = &vdev->eps[ep_index]; in xhci_urb_dequeue()
1705 ep_ring = xhci_urb_to_transfer_ring(xhci, urb); in xhci_urb_dequeue()
1710 temp = readl(&xhci->op_regs->status); in xhci_urb_dequeue()
1711 if (temp == ~(u32)0 || xhci->xhc_state & XHCI_STATE_DYING) { in xhci_urb_dequeue()
1712 xhci_hc_died(xhci); in xhci_urb_dequeue()
1717 * check ring is not re-allocated since URB was enqueued. If it is, then in xhci_urb_dequeue()
1721 if (!td_on_ring(&urb_priv->td[0], ep_ring)) { in xhci_urb_dequeue()
1722 xhci_err(xhci, "Canceled URB td not found on endpoint ring"); in xhci_urb_dequeue()
1723 for (i = urb_priv->num_tds_done; i < urb_priv->num_tds; i++) { in xhci_urb_dequeue()
1724 td = &urb_priv->td[i]; in xhci_urb_dequeue()
1725 if (!list_empty(&td->cancelled_td_list)) in xhci_urb_dequeue()
1726 list_del_init(&td->cancelled_td_list); in xhci_urb_dequeue()
1731 if (xhci->xhc_state & XHCI_STATE_HALTED) { in xhci_urb_dequeue()
1732 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_urb_dequeue()
1734 for (i = urb_priv->num_tds_done; in xhci_urb_dequeue()
1735 i < urb_priv->num_tds; in xhci_urb_dequeue()
1737 td = &urb_priv->td[i]; in xhci_urb_dequeue()
1738 if (!list_empty(&td->td_list)) in xhci_urb_dequeue()
1739 list_del_init(&td->td_list); in xhci_urb_dequeue()
1740 if (!list_empty(&td->cancelled_td_list)) in xhci_urb_dequeue()
1741 list_del_init(&td->cancelled_td_list); in xhci_urb_dequeue()
1746 i = urb_priv->num_tds_done; in xhci_urb_dequeue()
1747 if (i < urb_priv->num_tds) in xhci_urb_dequeue()
1748 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb, in xhci_urb_dequeue()
1751 urb, urb->dev->devpath, in xhci_urb_dequeue()
1752 urb->ep->desc.bEndpointAddress, in xhci_urb_dequeue()
1754 urb_priv->td[i].start_seg, in xhci_urb_dequeue()
1755 urb_priv->td[i].first_trb)); in xhci_urb_dequeue()
1757 for (; i < urb_priv->num_tds; i++) { in xhci_urb_dequeue()
1758 td = &urb_priv->td[i]; in xhci_urb_dequeue()
1760 if (list_empty(&td->cancelled_td_list)) { in xhci_urb_dequeue()
1761 td->cancel_status = TD_DIRTY; in xhci_urb_dequeue()
1762 list_add_tail(&td->cancelled_td_list, in xhci_urb_dequeue()
1763 &ep->cancelled_td_list); in xhci_urb_dequeue()
1768 if (ep->ep_state & (EP_STOP_CMD_PENDING | EP_HALTED | SET_DEQ_PENDING)) { in xhci_urb_dequeue()
1769 xhci_dbg(xhci, "Not queuing Stop Endpoint on slot %d ep %d in state 0x%x\n", in xhci_urb_dequeue()
1770 urb->dev->slot_id, ep_index, ep->ep_state); in xhci_urb_dequeue()
1775 if (ep->ep_state & EP_CLEARING_TT) { in xhci_urb_dequeue()
1777 xhci_dbg(xhci, "Invalidating TDs instantly on slot %d ep %d in state 0x%x\n", in xhci_urb_dequeue()
1778 urb->dev->slot_id, ep_index, ep->ep_state); in xhci_urb_dequeue()
1782 command = xhci_alloc_command(xhci, false, GFP_ATOMIC); in xhci_urb_dequeue()
1784 ret = -ENOMEM; in xhci_urb_dequeue()
1787 ep->stop_time = jiffies; in xhci_urb_dequeue()
1788 ep->ep_state |= EP_STOP_CMD_PENDING; in xhci_urb_dequeue()
1789 xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id, in xhci_urb_dequeue()
1791 xhci_ring_cmd_db(xhci); in xhci_urb_dequeue()
1794 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_urb_dequeue()
1801 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_urb_dequeue()
1802 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN); in xhci_urb_dequeue()
1817 * the xhci->devs[slot_id] structure.
1822 struct xhci_hcd *xhci; in xhci_drop_endpoint() local
1834 xhci = hcd_to_xhci(hcd); in xhci_drop_endpoint()
1835 if (xhci->xhc_state & XHCI_STATE_DYING) in xhci_drop_endpoint()
1836 return -ENODEV; in xhci_drop_endpoint()
1838 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); in xhci_drop_endpoint()
1839 drop_flag = xhci_get_endpoint_flag(&ep->desc); in xhci_drop_endpoint()
1841 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n", in xhci_drop_endpoint()
1846 in_ctx = xhci->devs[udev->slot_id]->in_ctx; in xhci_drop_endpoint()
1847 out_ctx = xhci->devs[udev->slot_id]->out_ctx; in xhci_drop_endpoint()
1850 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_drop_endpoint()
1855 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_drop_endpoint()
1856 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); in xhci_drop_endpoint()
1861 le32_to_cpu(ctrl_ctx->drop_flags) & in xhci_drop_endpoint()
1862 xhci_get_endpoint_flag(&ep->desc)) { in xhci_drop_endpoint()
1864 if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL) in xhci_drop_endpoint()
1865 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n", in xhci_drop_endpoint()
1870 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag); in xhci_drop_endpoint()
1871 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags); in xhci_drop_endpoint()
1873 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag); in xhci_drop_endpoint()
1874 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags); in xhci_drop_endpoint()
1876 xhci_debugfs_remove_endpoint(xhci, xhci->devs[udev->slot_id], ep_index); in xhci_drop_endpoint()
1878 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep); in xhci_drop_endpoint()
1880 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n", in xhci_drop_endpoint()
1881 (unsigned int) ep->desc.bEndpointAddress, in xhci_drop_endpoint()
1882 udev->slot_id, in xhci_drop_endpoint()
1900 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1905 struct xhci_hcd *xhci; in xhci_add_endpoint() local
1918 ep->hcpriv = NULL; in xhci_add_endpoint()
1921 xhci = hcd_to_xhci(hcd); in xhci_add_endpoint()
1922 if (xhci->xhc_state & XHCI_STATE_DYING) in xhci_add_endpoint()
1923 return -ENODEV; in xhci_add_endpoint()
1925 added_ctxs = xhci_get_endpoint_flag(&ep->desc); in xhci_add_endpoint()
1931 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n", in xhci_add_endpoint()
1936 virt_dev = xhci->devs[udev->slot_id]; in xhci_add_endpoint()
1937 in_ctx = virt_dev->in_ctx; in xhci_add_endpoint()
1940 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_add_endpoint()
1945 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_add_endpoint()
1949 if (virt_dev->eps[ep_index].ring && in xhci_add_endpoint()
1950 !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) { in xhci_add_endpoint()
1951 xhci_warn(xhci, "Trying to add endpoint 0x%x " in xhci_add_endpoint()
1953 (unsigned int) ep->desc.bEndpointAddress); in xhci_add_endpoint()
1954 return -EINVAL; in xhci_add_endpoint()
1960 if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) { in xhci_add_endpoint()
1961 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n", in xhci_add_endpoint()
1971 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) { in xhci_add_endpoint()
1972 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n", in xhci_add_endpoint()
1973 __func__, ep->desc.bEndpointAddress); in xhci_add_endpoint()
1974 return -ENOMEM; in xhci_add_endpoint()
1977 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs); in xhci_add_endpoint()
1978 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags); in xhci_add_endpoint()
1982 * this re-adds a new state for the endpoint from the new endpoint in xhci_add_endpoint()
1983 * descriptors. We must drop and re-add this endpoint, so we leave the in xhci_add_endpoint()
1986 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags); in xhci_add_endpoint()
1989 ep->hcpriv = udev; in xhci_add_endpoint()
1991 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index); in xhci_add_endpoint()
1994 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n", in xhci_add_endpoint()
1995 (unsigned int) ep->desc.bEndpointAddress, in xhci_add_endpoint()
1996 udev->slot_id, in xhci_add_endpoint()
2003 static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev) in xhci_zero_in_ctx() argument
2010 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); in xhci_zero_in_ctx()
2012 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_zero_in_ctx()
2022 ctrl_ctx->drop_flags = 0; in xhci_zero_in_ctx()
2023 ctrl_ctx->add_flags = 0; in xhci_zero_in_ctx()
2024 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); in xhci_zero_in_ctx()
2025 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK); in xhci_zero_in_ctx()
2027 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1)); in xhci_zero_in_ctx()
2029 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i); in xhci_zero_in_ctx()
2030 ep_ctx->ep_info = 0; in xhci_zero_in_ctx()
2031 ep_ctx->ep_info2 = 0; in xhci_zero_in_ctx()
2032 ep_ctx->deq = 0; in xhci_zero_in_ctx()
2033 ep_ctx->tx_info = 0; in xhci_zero_in_ctx()
2037 static int xhci_configure_endpoint_result(struct xhci_hcd *xhci, in xhci_configure_endpoint_result() argument
2045 xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n"); in xhci_configure_endpoint_result()
2046 ret = -ETIME; in xhci_configure_endpoint_result()
2049 dev_warn(&udev->dev, in xhci_configure_endpoint_result()
2051 ret = -ENOMEM; in xhci_configure_endpoint_result()
2056 dev_warn(&udev->dev, in xhci_configure_endpoint_result()
2058 ret = -ENOSPC; in xhci_configure_endpoint_result()
2063 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, " in xhci_configure_endpoint_result()
2066 ret = -EINVAL; in xhci_configure_endpoint_result()
2069 dev_warn(&udev->dev, in xhci_configure_endpoint_result()
2071 ret = -ENODEV; in xhci_configure_endpoint_result()
2074 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_configure_endpoint_result()
2079 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n", in xhci_configure_endpoint_result()
2081 ret = -EINVAL; in xhci_configure_endpoint_result()
2087 static int xhci_evaluate_context_result(struct xhci_hcd *xhci, in xhci_evaluate_context_result() argument
2095 xhci_warn(xhci, "Timeout while waiting for evaluate context command\n"); in xhci_evaluate_context_result()
2096 ret = -ETIME; in xhci_evaluate_context_result()
2099 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2100 "WARN: xHCI driver setup invalid evaluate context command.\n"); in xhci_evaluate_context_result()
2101 ret = -EINVAL; in xhci_evaluate_context_result()
2104 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2106 ret = -EINVAL; in xhci_evaluate_context_result()
2109 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2111 ret = -EINVAL; in xhci_evaluate_context_result()
2114 dev_warn(&udev->dev, in xhci_evaluate_context_result()
2116 ret = -ENODEV; in xhci_evaluate_context_result()
2120 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n"); in xhci_evaluate_context_result()
2121 ret = -EINVAL; in xhci_evaluate_context_result()
2124 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_evaluate_context_result()
2129 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n", in xhci_evaluate_context_result()
2131 ret = -EINVAL; in xhci_evaluate_context_result()
2137 static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci, in xhci_count_num_new_endpoints() argument
2147 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2; in xhci_count_num_new_endpoints()
2148 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2; in xhci_count_num_new_endpoints()
2154 return hweight32(valid_add_flags) - in xhci_count_num_new_endpoints()
2158 static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci, in xhci_count_num_dropped_endpoints() argument
2164 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2; in xhci_count_num_dropped_endpoints()
2165 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2; in xhci_count_num_dropped_endpoints()
2167 return hweight32(valid_drop_flags) - in xhci_count_num_dropped_endpoints()
2177 * - the first configure endpoint command drops more endpoints than it adds
2178 * - a second configure endpoint command that adds more endpoints is queued
2179 * - the first configure endpoint command fails, so the config is unchanged
2180 * - the second command may succeed, even though there isn't enough resources
2182 * Must be called with xhci->lock held.
2184 static int xhci_reserve_host_resources(struct xhci_hcd *xhci, in xhci_reserve_host_resources() argument
2189 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx); in xhci_reserve_host_resources()
2190 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) { in xhci_reserve_host_resources()
2191 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_resources()
2194 xhci->num_active_eps, added_eps, in xhci_reserve_host_resources()
2195 xhci->limit_active_eps); in xhci_reserve_host_resources()
2196 return -ENOMEM; in xhci_reserve_host_resources()
2198 xhci->num_active_eps += added_eps; in xhci_reserve_host_resources()
2199 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_resources()
2201 xhci->num_active_eps); in xhci_reserve_host_resources()
2209 * Must be called with xhci->lock held.
2211 static void xhci_free_host_resources(struct xhci_hcd *xhci, in xhci_free_host_resources() argument
2216 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx); in xhci_free_host_resources()
2217 xhci->num_active_eps -= num_failed_eps; in xhci_free_host_resources()
2218 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_free_host_resources()
2221 xhci->num_active_eps); in xhci_free_host_resources()
2228 * Must be called with xhci->lock held.
2230 static void xhci_finish_resource_reservation(struct xhci_hcd *xhci, in xhci_finish_resource_reservation() argument
2235 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx); in xhci_finish_resource_reservation()
2236 xhci->num_active_eps -= num_dropped_eps; in xhci_finish_resource_reservation()
2238 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_finish_resource_reservation()
2241 xhci->num_active_eps); in xhci_finish_resource_reservation()
2246 switch (udev->speed) { in xhci_get_block_size()
2265 if (interval_bw->overhead[LS_OVERHEAD_TYPE]) in xhci_get_largest_overhead()
2267 if (interval_bw->overhead[FS_OVERHEAD_TYPE]) in xhci_get_largest_overhead()
2276 static int xhci_check_tt_bw_table(struct xhci_hcd *xhci, in xhci_check_tt_bw_table() argument
2284 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table; in xhci_check_tt_bw_table()
2285 tt_info = virt_dev->tt_info; in xhci_check_tt_bw_table()
2292 if (old_active_eps == 0 && tt_info->active_eps != 0) { in xhci_check_tt_bw_table()
2293 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT) in xhci_check_tt_bw_table()
2294 return -ENOMEM; in xhci_check_tt_bw_table()
2306 static int xhci_check_ss_bw(struct xhci_hcd *xhci, in xhci_check_ss_bw() argument
2312 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved)) in xhci_check_ss_bw()
2313 return -ENOMEM; in xhci_check_ss_bw()
2316 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved)) in xhci_check_ss_bw()
2317 return -ENOMEM; in xhci_check_ss_bw()
2323 * This algorithm is a very conservative estimate of the worst-case scheduling
2331 * over-estimate.
2363 static int xhci_check_bw_table(struct xhci_hcd *xhci, in xhci_check_bw_table() argument
2378 if (virt_dev->udev->speed >= USB_SPEED_SUPER) in xhci_check_bw_table()
2379 return xhci_check_ss_bw(xhci, virt_dev); in xhci_check_bw_table()
2381 if (virt_dev->udev->speed == USB_SPEED_HIGH) { in xhci_check_bw_table()
2390 bw_table = virt_dev->bw_table; in xhci_check_bw_table()
2394 block_size = xhci_get_block_size(virt_dev->udev); in xhci_check_bw_table()
2399 if (virt_dev->tt_info) { in xhci_check_bw_table()
2400 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2402 virt_dev->real_port); in xhci_check_bw_table()
2403 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) { in xhci_check_bw_table()
2404 xhci_warn(xhci, "Not enough bandwidth on HS bus for " in xhci_check_bw_table()
2406 return -ENOMEM; in xhci_check_bw_table()
2408 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2410 virt_dev->tt_info->slot_id, in xhci_check_bw_table()
2411 virt_dev->tt_info->ttport); in xhci_check_bw_table()
2413 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2415 virt_dev->real_port); in xhci_check_bw_table()
2421 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) + in xhci_check_bw_table()
2422 bw_table->interval_bw[0].num_packets * in xhci_check_bw_table()
2423 xhci_get_largest_overhead(&bw_table->interval_bw[0]); in xhci_check_bw_table()
2436 bw_table->interval_bw[i].num_packets; in xhci_check_bw_table()
2441 if (list_empty(&bw_table->interval_bw[i].endpoints)) in xhci_check_bw_table()
2447 ep_entry = bw_table->interval_bw[i].endpoints.next; in xhci_check_bw_table()
2452 virt_ep->bw_info.max_packet_size, in xhci_check_bw_table()
2460 &bw_table->interval_bw[i]); in xhci_check_bw_table()
2496 xhci_warn(xhci, "Not enough bandwidth. " in xhci_check_bw_table()
2499 return -ENOMEM; in xhci_check_bw_table()
2503 * Ok, we know we have some packets left over after even-handedly in xhci_check_bw_table()
2505 * fit into, so we over-schedule and say they will be scheduled every in xhci_check_bw_table()
2511 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) { in xhci_check_bw_table()
2512 unsigned int port_index = virt_dev->real_port - 1; in xhci_check_bw_table()
2519 xhci->rh_bw[port_index].num_active_tts; in xhci_check_bw_table()
2522 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_check_bw_table()
2526 (max_bandwidth - bw_used - bw_reserved) * 100 / in xhci_check_bw_table()
2531 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n", in xhci_check_bw_table()
2533 return -ENOMEM; in xhci_check_bw_table()
2536 bw_table->bw_used = bw_used; in xhci_check_bw_table()
2554 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK); in xhci_get_ss_bw_consumed()
2556 if (ep_bw->ep_interval == 0) in xhci_get_ss_bw_consumed()
2558 (ep_bw->mult * ep_bw->num_packets * in xhci_get_ss_bw_consumed()
2560 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets * in xhci_get_ss_bw_consumed()
2562 1 << ep_bw->ep_interval); in xhci_get_ss_bw_consumed()
2566 static void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci, in xhci_drop_ep_from_interval_table() argument
2576 if (xhci_is_async_ep(ep_bw->type)) in xhci_drop_ep_from_interval_table()
2579 if (udev->speed >= USB_SPEED_SUPER) { in xhci_drop_ep_from_interval_table()
2580 if (xhci_is_sync_in_ep(ep_bw->type)) in xhci_drop_ep_from_interval_table()
2581 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -= in xhci_drop_ep_from_interval_table()
2584 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -= in xhci_drop_ep_from_interval_table()
2592 if (list_empty(&virt_ep->bw_endpoint_list)) in xhci_drop_ep_from_interval_table()
2597 if (udev->speed == USB_SPEED_HIGH) in xhci_drop_ep_from_interval_table()
2598 normalized_interval = ep_bw->ep_interval; in xhci_drop_ep_from_interval_table()
2600 normalized_interval = ep_bw->ep_interval - 3; in xhci_drop_ep_from_interval_table()
2603 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload; in xhci_drop_ep_from_interval_table()
2604 interval_bw = &bw_table->interval_bw[normalized_interval]; in xhci_drop_ep_from_interval_table()
2605 interval_bw->num_packets -= ep_bw->num_packets; in xhci_drop_ep_from_interval_table()
2606 switch (udev->speed) { in xhci_drop_ep_from_interval_table()
2608 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1; in xhci_drop_ep_from_interval_table()
2611 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1; in xhci_drop_ep_from_interval_table()
2614 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1; in xhci_drop_ep_from_interval_table()
2623 tt_info->active_eps -= 1; in xhci_drop_ep_from_interval_table()
2624 list_del_init(&virt_ep->bw_endpoint_list); in xhci_drop_ep_from_interval_table()
2627 static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci, in xhci_add_ep_to_interval_table() argument
2638 if (xhci_is_async_ep(ep_bw->type)) in xhci_add_ep_to_interval_table()
2641 if (udev->speed == USB_SPEED_SUPER) { in xhci_add_ep_to_interval_table()
2642 if (xhci_is_sync_in_ep(ep_bw->type)) in xhci_add_ep_to_interval_table()
2643 xhci->devs[udev->slot_id]->bw_table->ss_bw_in += in xhci_add_ep_to_interval_table()
2646 xhci->devs[udev->slot_id]->bw_table->ss_bw_out += in xhci_add_ep_to_interval_table()
2654 if (udev->speed == USB_SPEED_HIGH) in xhci_add_ep_to_interval_table()
2655 normalized_interval = ep_bw->ep_interval; in xhci_add_ep_to_interval_table()
2657 normalized_interval = ep_bw->ep_interval - 3; in xhci_add_ep_to_interval_table()
2660 bw_table->interval0_esit_payload += ep_bw->max_esit_payload; in xhci_add_ep_to_interval_table()
2661 interval_bw = &bw_table->interval_bw[normalized_interval]; in xhci_add_ep_to_interval_table()
2662 interval_bw->num_packets += ep_bw->num_packets; in xhci_add_ep_to_interval_table()
2663 switch (udev->speed) { in xhci_add_ep_to_interval_table()
2665 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1; in xhci_add_ep_to_interval_table()
2668 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1; in xhci_add_ep_to_interval_table()
2671 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1; in xhci_add_ep_to_interval_table()
2681 tt_info->active_eps += 1; in xhci_add_ep_to_interval_table()
2683 list_for_each_entry(smaller_ep, &interval_bw->endpoints, in xhci_add_ep_to_interval_table()
2685 if (ep_bw->max_packet_size >= in xhci_add_ep_to_interval_table()
2686 smaller_ep->bw_info.max_packet_size) { in xhci_add_ep_to_interval_table()
2688 list_add_tail(&virt_ep->bw_endpoint_list, in xhci_add_ep_to_interval_table()
2689 &smaller_ep->bw_endpoint_list); in xhci_add_ep_to_interval_table()
2694 list_add_tail(&virt_ep->bw_endpoint_list, in xhci_add_ep_to_interval_table()
2695 &interval_bw->endpoints); in xhci_add_ep_to_interval_table()
2698 void xhci_update_tt_active_eps(struct xhci_hcd *xhci, in xhci_update_tt_active_eps() argument
2703 if (!virt_dev->tt_info) in xhci_update_tt_active_eps()
2706 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1]; in xhci_update_tt_active_eps()
2708 virt_dev->tt_info->active_eps != 0) { in xhci_update_tt_active_eps()
2709 rh_bw_info->num_active_tts += 1; in xhci_update_tt_active_eps()
2710 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD; in xhci_update_tt_active_eps()
2712 virt_dev->tt_info->active_eps == 0) { in xhci_update_tt_active_eps()
2713 rh_bw_info->num_active_tts -= 1; in xhci_update_tt_active_eps()
2714 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD; in xhci_update_tt_active_eps()
2718 static int xhci_reserve_bandwidth(struct xhci_hcd *xhci, in xhci_reserve_bandwidth() argument
2727 if (virt_dev->tt_info) in xhci_reserve_bandwidth()
2728 old_active_eps = virt_dev->tt_info->active_eps; in xhci_reserve_bandwidth()
2732 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_reserve_bandwidth()
2734 return -ENOMEM; in xhci_reserve_bandwidth()
2742 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2748 xhci_drop_ep_from_interval_table(xhci, in xhci_reserve_bandwidth()
2749 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2750 virt_dev->bw_table, in xhci_reserve_bandwidth()
2751 virt_dev->udev, in xhci_reserve_bandwidth()
2752 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2753 virt_dev->tt_info); in xhci_reserve_bandwidth()
2756 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev); in xhci_reserve_bandwidth()
2760 xhci_add_ep_to_interval_table(xhci, in xhci_reserve_bandwidth()
2761 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2762 virt_dev->bw_table, in xhci_reserve_bandwidth()
2763 virt_dev->udev, in xhci_reserve_bandwidth()
2764 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2765 virt_dev->tt_info); in xhci_reserve_bandwidth()
2768 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) { in xhci_reserve_bandwidth()
2772 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps); in xhci_reserve_bandwidth()
2785 xhci_drop_ep_from_interval_table(xhci, in xhci_reserve_bandwidth()
2786 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2787 virt_dev->bw_table, in xhci_reserve_bandwidth()
2788 virt_dev->udev, in xhci_reserve_bandwidth()
2789 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2790 virt_dev->tt_info); in xhci_reserve_bandwidth()
2793 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i], in xhci_reserve_bandwidth()
2797 xhci_add_ep_to_interval_table(xhci, in xhci_reserve_bandwidth()
2798 &virt_dev->eps[i].bw_info, in xhci_reserve_bandwidth()
2799 virt_dev->bw_table, in xhci_reserve_bandwidth()
2800 virt_dev->udev, in xhci_reserve_bandwidth()
2801 &virt_dev->eps[i], in xhci_reserve_bandwidth()
2802 virt_dev->tt_info); in xhci_reserve_bandwidth()
2804 return -ENOMEM; in xhci_reserve_bandwidth()
2811 static int xhci_configure_endpoint(struct xhci_hcd *xhci, in xhci_configure_endpoint() argument
2823 return -EINVAL; in xhci_configure_endpoint()
2825 spin_lock_irqsave(&xhci->lock, flags); in xhci_configure_endpoint()
2827 if (xhci->xhc_state & XHCI_STATE_DYING) { in xhci_configure_endpoint()
2828 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2829 return -ESHUTDOWN; in xhci_configure_endpoint()
2832 virt_dev = xhci->devs[udev->slot_id]; in xhci_configure_endpoint()
2834 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_configure_endpoint()
2836 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2837 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_configure_endpoint()
2839 return -ENOMEM; in xhci_configure_endpoint()
2842 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) && in xhci_configure_endpoint()
2843 xhci_reserve_host_resources(xhci, ctrl_ctx)) { in xhci_configure_endpoint()
2844 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2845 xhci_warn(xhci, "Not enough host resources, " in xhci_configure_endpoint()
2847 xhci->num_active_eps); in xhci_configure_endpoint()
2848 return -ENOMEM; in xhci_configure_endpoint()
2850 if ((xhci->quirks & XHCI_SW_BW_CHECKING) && !ctx_change && in xhci_configure_endpoint()
2851 xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) { in xhci_configure_endpoint()
2852 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) in xhci_configure_endpoint()
2853 xhci_free_host_resources(xhci, ctrl_ctx); in xhci_configure_endpoint()
2854 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2855 xhci_warn(xhci, "Not enough bandwidth\n"); in xhci_configure_endpoint()
2856 return -ENOMEM; in xhci_configure_endpoint()
2859 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx); in xhci_configure_endpoint()
2865 ret = xhci_queue_configure_endpoint(xhci, command, in xhci_configure_endpoint()
2866 command->in_ctx->dma, in xhci_configure_endpoint()
2867 udev->slot_id, must_succeed); in xhci_configure_endpoint()
2869 ret = xhci_queue_evaluate_context(xhci, command, in xhci_configure_endpoint()
2870 command->in_ctx->dma, in xhci_configure_endpoint()
2871 udev->slot_id, must_succeed); in xhci_configure_endpoint()
2873 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) in xhci_configure_endpoint()
2874 xhci_free_host_resources(xhci, ctrl_ctx); in xhci_configure_endpoint()
2875 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2876 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_configure_endpoint()
2878 return -ENOMEM; in xhci_configure_endpoint()
2880 xhci_ring_cmd_db(xhci); in xhci_configure_endpoint()
2881 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2884 wait_for_completion(command->completion); in xhci_configure_endpoint()
2887 ret = xhci_configure_endpoint_result(xhci, udev, in xhci_configure_endpoint()
2888 &command->status); in xhci_configure_endpoint()
2890 ret = xhci_evaluate_context_result(xhci, udev, in xhci_configure_endpoint()
2891 &command->status); in xhci_configure_endpoint()
2893 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { in xhci_configure_endpoint()
2894 spin_lock_irqsave(&xhci->lock, flags); in xhci_configure_endpoint()
2899 xhci_free_host_resources(xhci, ctrl_ctx); in xhci_configure_endpoint()
2901 xhci_finish_resource_reservation(xhci, ctrl_ctx); in xhci_configure_endpoint()
2902 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_configure_endpoint()
2907 static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci, in xhci_check_bw_drop_ep_streams() argument
2910 struct xhci_virt_ep *ep = &vdev->eps[i]; in xhci_check_bw_drop_ep_streams()
2912 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_check_bw_drop_ep_streams()
2913 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n", in xhci_check_bw_drop_ep_streams()
2915 xhci_free_stream_info(xhci, ep->stream_info); in xhci_check_bw_drop_ep_streams()
2916 ep->stream_info = NULL; in xhci_check_bw_drop_ep_streams()
2917 ep->ep_state &= ~EP_HAS_STREAMS; in xhci_check_bw_drop_ep_streams()
2928 * else should be touching the xhci->devs[slot_id] structure, so we
2929 * don't need to take the xhci->lock for manipulating that.
2935 struct xhci_hcd *xhci; in xhci_check_bandwidth() local
2944 xhci = hcd_to_xhci(hcd); in xhci_check_bandwidth()
2945 if ((xhci->xhc_state & XHCI_STATE_DYING) || in xhci_check_bandwidth()
2946 (xhci->xhc_state & XHCI_STATE_REMOVING)) in xhci_check_bandwidth()
2947 return -ENODEV; in xhci_check_bandwidth()
2949 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); in xhci_check_bandwidth()
2950 virt_dev = xhci->devs[udev->slot_id]; in xhci_check_bandwidth()
2952 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_check_bandwidth()
2954 return -ENOMEM; in xhci_check_bandwidth()
2956 command->in_ctx = virt_dev->in_ctx; in xhci_check_bandwidth()
2958 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */ in xhci_check_bandwidth()
2959 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_check_bandwidth()
2961 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_check_bandwidth()
2963 ret = -ENOMEM; in xhci_check_bandwidth()
2966 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_check_bandwidth()
2967 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG); in xhci_check_bandwidth()
2968 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG)); in xhci_check_bandwidth()
2971 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) && in xhci_check_bandwidth()
2972 ctrl_ctx->drop_flags == 0) { in xhci_check_bandwidth()
2977 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); in xhci_check_bandwidth()
2978 for (i = 31; i >= 1; i--) { in xhci_check_bandwidth()
2981 if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32)) in xhci_check_bandwidth()
2982 || (ctrl_ctx->add_flags & le32) || i == 1) { in xhci_check_bandwidth()
2983 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK); in xhci_check_bandwidth()
2984 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i)); in xhci_check_bandwidth()
2989 ret = xhci_configure_endpoint(xhci, udev, command, in xhci_check_bandwidth()
2997 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) && in xhci_check_bandwidth()
2998 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) { in xhci_check_bandwidth()
2999 xhci_free_endpoint_ring(xhci, virt_dev, i); in xhci_check_bandwidth()
3000 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i); in xhci_check_bandwidth()
3003 xhci_zero_in_ctx(xhci, virt_dev); in xhci_check_bandwidth()
3009 if (!virt_dev->eps[i].new_ring) in xhci_check_bandwidth()
3014 if (virt_dev->eps[i].ring) { in xhci_check_bandwidth()
3015 xhci_free_endpoint_ring(xhci, virt_dev, i); in xhci_check_bandwidth()
3017 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i); in xhci_check_bandwidth()
3018 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring; in xhci_check_bandwidth()
3019 virt_dev->eps[i].new_ring = NULL; in xhci_check_bandwidth()
3020 xhci_debugfs_create_endpoint(xhci, virt_dev, i); in xhci_check_bandwidth()
3023 kfree(command->completion); in xhci_check_bandwidth()
3032 struct xhci_hcd *xhci; in xhci_reset_bandwidth() local
3039 xhci = hcd_to_xhci(hcd); in xhci_reset_bandwidth()
3041 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); in xhci_reset_bandwidth()
3042 virt_dev = xhci->devs[udev->slot_id]; in xhci_reset_bandwidth()
3045 if (virt_dev->eps[i].new_ring) { in xhci_reset_bandwidth()
3046 xhci_debugfs_remove_endpoint(xhci, virt_dev, i); in xhci_reset_bandwidth()
3047 xhci_ring_free(xhci, virt_dev->eps[i].new_ring); in xhci_reset_bandwidth()
3048 virt_dev->eps[i].new_ring = NULL; in xhci_reset_bandwidth()
3051 xhci_zero_in_ctx(xhci, virt_dev); in xhci_reset_bandwidth()
3055 static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci, in xhci_setup_input_ctx_for_config_ep() argument
3061 ctrl_ctx->add_flags = cpu_to_le32(add_flags); in xhci_setup_input_ctx_for_config_ep()
3062 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags); in xhci_setup_input_ctx_for_config_ep()
3063 xhci_slot_copy(xhci, in_ctx, out_ctx); in xhci_setup_input_ctx_for_config_ep()
3064 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_setup_input_ctx_for_config_ep()
3070 struct xhci_hcd *xhci; in xhci_endpoint_disable() local
3077 xhci = hcd_to_xhci(hcd); in xhci_endpoint_disable()
3079 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_disable()
3081 udev = (struct usb_device *)host_ep->hcpriv; in xhci_endpoint_disable()
3082 if (!udev || !udev->slot_id) in xhci_endpoint_disable()
3085 vdev = xhci->devs[udev->slot_id]; in xhci_endpoint_disable()
3089 ep_index = xhci_get_endpoint_index(&host_ep->desc); in xhci_endpoint_disable()
3090 ep = &vdev->eps[ep_index]; in xhci_endpoint_disable()
3093 if (ep->ep_state & EP_CLEARING_TT) { in xhci_endpoint_disable()
3094 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_disable()
3099 if (ep->ep_state) in xhci_endpoint_disable()
3100 xhci_dbg(xhci, "endpoint disable with ep_state 0x%x\n", in xhci_endpoint_disable()
3101 ep->ep_state); in xhci_endpoint_disable()
3103 host_ep->hcpriv = NULL; in xhci_endpoint_disable()
3104 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_disable()
3116 * endpoint. Refer to the additional note in xhci spcification section 4.6.8.
3122 struct xhci_hcd *xhci; in xhci_endpoint_reset() local
3133 xhci = hcd_to_xhci(hcd); in xhci_endpoint_reset()
3134 if (!host_ep->hcpriv) in xhci_endpoint_reset()
3136 udev = (struct usb_device *) host_ep->hcpriv; in xhci_endpoint_reset()
3137 vdev = xhci->devs[udev->slot_id]; in xhci_endpoint_reset()
3140 * vdev may be lost due to xHC restore error and re-initialization in xhci_endpoint_reset()
3144 if (!udev->slot_id || !vdev) in xhci_endpoint_reset()
3146 ep_index = xhci_get_endpoint_index(&host_ep->desc); in xhci_endpoint_reset()
3147 ep = &vdev->eps[ep_index]; in xhci_endpoint_reset()
3150 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3151 if (ep->ep_state & EP_HARD_CLEAR_TOGGLE) { in xhci_endpoint_reset()
3152 ep->ep_state &= ~EP_HARD_CLEAR_TOGGLE; in xhci_endpoint_reset()
3153 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3156 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3157 /* Only interrupt and bulk ep's use data toggle, USB2 spec 5.5.4-> */ in xhci_endpoint_reset()
3158 if (usb_endpoint_xfer_control(&host_ep->desc) || in xhci_endpoint_reset()
3159 usb_endpoint_xfer_isoc(&host_ep->desc)) in xhci_endpoint_reset()
3162 ep_flag = xhci_get_endpoint_flag(&host_ep->desc); in xhci_endpoint_reset()
3167 stop_cmd = xhci_alloc_command(xhci, true, GFP_NOWAIT); in xhci_endpoint_reset()
3171 cfg_cmd = xhci_alloc_command_with_ctx(xhci, true, GFP_NOWAIT); in xhci_endpoint_reset()
3175 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3178 ep->ep_state |= EP_SOFT_CLEAR_TOGGLE; in xhci_endpoint_reset()
3186 if (!list_empty(&ep->ring->td_list)) { in xhci_endpoint_reset()
3187 dev_err(&udev->dev, "EP not empty, refuse reset\n"); in xhci_endpoint_reset()
3188 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3189 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3193 err = xhci_queue_stop_endpoint(xhci, stop_cmd, udev->slot_id, in xhci_endpoint_reset()
3196 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3197 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3198 xhci_dbg(xhci, "%s: Failed to queue stop ep command, %d ", in xhci_endpoint_reset()
3203 xhci_ring_cmd_db(xhci); in xhci_endpoint_reset()
3204 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3206 wait_for_completion(stop_cmd->completion); in xhci_endpoint_reset()
3208 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3211 ctrl_ctx = xhci_get_input_control_ctx(cfg_cmd->in_ctx); in xhci_endpoint_reset()
3213 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3214 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3215 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_endpoint_reset()
3220 xhci_setup_input_ctx_for_config_ep(xhci, cfg_cmd->in_ctx, vdev->out_ctx, in xhci_endpoint_reset()
3222 xhci_endpoint_copy(xhci, cfg_cmd->in_ctx, vdev->out_ctx, ep_index); in xhci_endpoint_reset()
3224 err = xhci_queue_configure_endpoint(xhci, cfg_cmd, cfg_cmd->in_ctx->dma, in xhci_endpoint_reset()
3225 udev->slot_id, false); in xhci_endpoint_reset()
3227 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3228 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3229 xhci_dbg(xhci, "%s: Failed to queue config ep command, %d ", in xhci_endpoint_reset()
3234 xhci_ring_cmd_db(xhci); in xhci_endpoint_reset()
3235 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3237 wait_for_completion(cfg_cmd->completion); in xhci_endpoint_reset()
3239 xhci_free_command(xhci, cfg_cmd); in xhci_endpoint_reset()
3241 xhci_free_command(xhci, stop_cmd); in xhci_endpoint_reset()
3242 spin_lock_irqsave(&xhci->lock, flags); in xhci_endpoint_reset()
3243 if (ep->ep_state & EP_SOFT_CLEAR_TOGGLE) in xhci_endpoint_reset()
3244 ep->ep_state &= ~EP_SOFT_CLEAR_TOGGLE; in xhci_endpoint_reset()
3245 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_endpoint_reset()
3248 static int xhci_check_streams_endpoint(struct xhci_hcd *xhci, in xhci_check_streams_endpoint() argument
3257 return -EINVAL; in xhci_check_streams_endpoint()
3258 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__); in xhci_check_streams_endpoint()
3260 return ret ? ret : -EINVAL; in xhci_check_streams_endpoint()
3261 if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) { in xhci_check_streams_endpoint()
3262 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion" in xhci_check_streams_endpoint()
3264 ep->desc.bEndpointAddress); in xhci_check_streams_endpoint()
3265 return -EINVAL; in xhci_check_streams_endpoint()
3268 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_check_streams_endpoint()
3269 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; in xhci_check_streams_endpoint()
3272 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x " in xhci_check_streams_endpoint()
3274 ep->desc.bEndpointAddress); in xhci_check_streams_endpoint()
3275 xhci_warn(xhci, "Send email to xHCI maintainer and ask for " in xhci_check_streams_endpoint()
3277 return -EINVAL; in xhci_check_streams_endpoint()
3279 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) { in xhci_check_streams_endpoint()
3280 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk " in xhci_check_streams_endpoint()
3282 ep->desc.bEndpointAddress); in xhci_check_streams_endpoint()
3283 return -EINVAL; in xhci_check_streams_endpoint()
3288 static void xhci_calculate_streams_entries(struct xhci_hcd *xhci, in xhci_calculate_streams_entries() argument
3298 * level page entries), but that's an optional feature for xHCI host in xhci_calculate_streams_entries()
3301 max_streams = HCC_MAX_PSA(xhci->hcc_params); in xhci_calculate_streams_entries()
3303 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n", in xhci_calculate_streams_entries()
3314 static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci, in xhci_calculate_streams_and_bitmask() argument
3325 ret = xhci_check_streams_endpoint(xhci, udev, in xhci_calculate_streams_and_bitmask()
3326 eps[i], udev->slot_id); in xhci_calculate_streams_and_bitmask()
3330 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp); in xhci_calculate_streams_and_bitmask()
3331 if (max_streams < (*num_streams - 1)) { in xhci_calculate_streams_and_bitmask()
3332 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n", in xhci_calculate_streams_and_bitmask()
3333 eps[i]->desc.bEndpointAddress, in xhci_calculate_streams_and_bitmask()
3338 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc); in xhci_calculate_streams_and_bitmask()
3340 return -EINVAL; in xhci_calculate_streams_and_bitmask()
3346 static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci, in xhci_calculate_no_streams_bitmask() argument
3356 slot_id = udev->slot_id; in xhci_calculate_no_streams_bitmask()
3357 if (!xhci->devs[slot_id]) in xhci_calculate_no_streams_bitmask()
3361 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_calculate_no_streams_bitmask()
3362 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; in xhci_calculate_no_streams_bitmask()
3365 xhci_warn(xhci, "WARN Can't disable streams for " in xhci_calculate_no_streams_bitmask()
3368 eps[i]->desc.bEndpointAddress); in xhci_calculate_no_streams_bitmask()
3374 xhci_warn(xhci, "WARN Can't disable streams for " in xhci_calculate_no_streams_bitmask()
3377 eps[i]->desc.bEndpointAddress); in xhci_calculate_no_streams_bitmask()
3378 xhci_warn(xhci, "WARN xhci_free_streams() called " in xhci_calculate_no_streams_bitmask()
3379 "with non-streams endpoint\n"); in xhci_calculate_no_streams_bitmask()
3382 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc); in xhci_calculate_no_streams_bitmask()
3408 struct xhci_hcd *xhci; in xhci_alloc_streams() local
3419 return -EINVAL; in xhci_alloc_streams()
3422 * stream 0 that is reserved for xHCI usage. in xhci_alloc_streams()
3425 xhci = hcd_to_xhci(hcd); in xhci_alloc_streams()
3426 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n", in xhci_alloc_streams()
3430 if ((xhci->quirks & XHCI_BROKEN_STREAMS) || in xhci_alloc_streams()
3431 HCC_MAX_PSA(xhci->hcc_params) < 4) { in xhci_alloc_streams()
3432 xhci_dbg(xhci, "xHCI controller does not support streams.\n"); in xhci_alloc_streams()
3433 return -ENOSYS; in xhci_alloc_streams()
3436 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags); in xhci_alloc_streams()
3438 return -ENOMEM; in xhci_alloc_streams()
3440 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx); in xhci_alloc_streams()
3442 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_alloc_streams()
3444 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3445 return -ENOMEM; in xhci_alloc_streams()
3452 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_streams()
3453 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps, in xhci_alloc_streams()
3456 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3457 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3461 xhci_warn(xhci, "WARN: endpoints can't handle " in xhci_alloc_streams()
3463 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3464 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3465 return -EINVAL; in xhci_alloc_streams()
3467 vdev = xhci->devs[udev->slot_id]; in xhci_alloc_streams()
3472 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3473 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS; in xhci_alloc_streams()
3475 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3481 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs); in xhci_alloc_streams()
3482 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n", in xhci_alloc_streams()
3486 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3487 max_packet = usb_endpoint_maxp(&eps[i]->desc); in xhci_alloc_streams()
3488 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci, in xhci_alloc_streams()
3492 if (!vdev->eps[ep_index].stream_info) in xhci_alloc_streams()
3503 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3504 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index); in xhci_alloc_streams()
3506 xhci_endpoint_copy(xhci, config_cmd->in_ctx, in xhci_alloc_streams()
3507 vdev->out_ctx, ep_index); in xhci_alloc_streams()
3508 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx, in xhci_alloc_streams()
3509 vdev->eps[ep_index].stream_info); in xhci_alloc_streams()
3514 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx, in xhci_alloc_streams()
3515 vdev->out_ctx, ctrl_ctx, in xhci_alloc_streams()
3519 ret = xhci_configure_endpoint(xhci, udev, config_cmd, in xhci_alloc_streams()
3529 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_streams()
3531 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3532 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; in xhci_alloc_streams()
3533 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n", in xhci_alloc_streams()
3534 udev->slot_id, ep_index); in xhci_alloc_streams()
3535 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS; in xhci_alloc_streams()
3537 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3538 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_streams()
3541 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3542 xhci_debugfs_create_stream_files(xhci, vdev, ep_index); in xhci_alloc_streams()
3545 return num_streams - 1; in xhci_alloc_streams()
3550 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_alloc_streams()
3551 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); in xhci_alloc_streams()
3552 vdev->eps[ep_index].stream_info = NULL; in xhci_alloc_streams()
3556 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; in xhci_alloc_streams()
3557 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; in xhci_alloc_streams()
3558 xhci_endpoint_zero(xhci, vdev, eps[i]); in xhci_alloc_streams()
3560 xhci_free_command(xhci, config_cmd); in xhci_alloc_streams()
3561 return -ENOMEM; in xhci_alloc_streams()
3575 struct xhci_hcd *xhci; in xhci_free_streams() local
3583 xhci = hcd_to_xhci(hcd); in xhci_free_streams()
3584 vdev = xhci->devs[udev->slot_id]; in xhci_free_streams()
3587 spin_lock_irqsave(&xhci->lock, flags); in xhci_free_streams()
3588 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci, in xhci_free_streams()
3591 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3592 return -EINVAL; in xhci_free_streams()
3599 ep_index = xhci_get_endpoint_index(&eps[0]->desc); in xhci_free_streams()
3600 command = vdev->eps[ep_index].stream_info->free_streams_command; in xhci_free_streams()
3601 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_free_streams()
3603 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3604 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_free_streams()
3606 return -EINVAL; in xhci_free_streams()
3612 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_free_streams()
3613 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); in xhci_free_streams()
3614 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |= in xhci_free_streams()
3617 xhci_endpoint_copy(xhci, command->in_ctx, in xhci_free_streams()
3618 vdev->out_ctx, ep_index); in xhci_free_streams()
3620 &vdev->eps[ep_index]); in xhci_free_streams()
3622 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx, in xhci_free_streams()
3623 vdev->out_ctx, ctrl_ctx, in xhci_free_streams()
3625 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3630 ret = xhci_configure_endpoint(xhci, udev, command, in xhci_free_streams()
3639 spin_lock_irqsave(&xhci->lock, flags); in xhci_free_streams()
3641 ep_index = xhci_get_endpoint_index(&eps[i]->desc); in xhci_free_streams()
3642 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); in xhci_free_streams()
3643 vdev->eps[ep_index].stream_info = NULL; in xhci_free_streams()
3647 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS; in xhci_free_streams()
3648 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; in xhci_free_streams()
3650 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_streams()
3660 * Must be called with xhci->lock held.
3662 void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci, in xhci_free_device_endpoint_resources() argument
3670 if (virt_dev->eps[i].ring) { in xhci_free_device_endpoint_resources()
3675 xhci->num_active_eps -= num_dropped_eps; in xhci_free_device_endpoint_resources()
3677 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_free_device_endpoint_resources()
3681 xhci->num_active_eps); in xhci_free_device_endpoint_resources()
3690 * xhci_address_device(), and then re-set up the configuration. If this is
3692 * settings will be re-installed through the normal bandwidth allocation
3701 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3702 * re-allocate the device.
3709 struct xhci_hcd *xhci; in xhci_discover_or_reset_device() local
3719 xhci = hcd_to_xhci(hcd); in xhci_discover_or_reset_device()
3720 slot_id = udev->slot_id; in xhci_discover_or_reset_device()
3721 virt_dev = xhci->devs[slot_id]; in xhci_discover_or_reset_device()
3723 xhci_dbg(xhci, "The device to be reset with slot ID %u does " in xhci_discover_or_reset_device()
3724 "not exist. Re-allocate the device\n", slot_id); in xhci_discover_or_reset_device()
3729 return -EINVAL; in xhci_discover_or_reset_device()
3732 if (virt_dev->tt_info) in xhci_discover_or_reset_device()
3733 old_active_eps = virt_dev->tt_info->active_eps; in xhci_discover_or_reset_device()
3735 if (virt_dev->udev != udev) { in xhci_discover_or_reset_device()
3738 * Re-allocate the device. in xhci_discover_or_reset_device()
3740 xhci_dbg(xhci, "The device to be reset with slot ID %u does " in xhci_discover_or_reset_device()
3741 "not match the udev. Re-allocate the device\n", in xhci_discover_or_reset_device()
3747 return -EINVAL; in xhci_discover_or_reset_device()
3751 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_discover_or_reset_device()
3752 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) == in xhci_discover_or_reset_device()
3756 if (xhci->quirks & XHCI_ETRON_HOST) { in xhci_discover_or_reset_device()
3758 * Obtaining a new device slot to inform the xHCI host that in xhci_discover_or_reset_device()
3761 ret = xhci_disable_slot(xhci, udev->slot_id); in xhci_discover_or_reset_device()
3762 xhci_free_virt_device(xhci, udev->slot_id); in xhci_discover_or_reset_device()
3768 ret = -EINVAL; in xhci_discover_or_reset_device()
3775 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id); in xhci_discover_or_reset_device()
3782 reset_device_cmd = xhci_alloc_command(xhci, true, GFP_NOIO); in xhci_discover_or_reset_device()
3784 xhci_dbg(xhci, "Couldn't allocate command structure.\n"); in xhci_discover_or_reset_device()
3785 return -ENOMEM; in xhci_discover_or_reset_device()
3789 spin_lock_irqsave(&xhci->lock, flags); in xhci_discover_or_reset_device()
3791 ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id); in xhci_discover_or_reset_device()
3793 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); in xhci_discover_or_reset_device()
3794 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_discover_or_reset_device()
3797 xhci_ring_cmd_db(xhci); in xhci_discover_or_reset_device()
3798 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_discover_or_reset_device()
3801 wait_for_completion(reset_device_cmd->completion); in xhci_discover_or_reset_device()
3807 ret = reset_device_cmd->status; in xhci_discover_or_reset_device()
3811 xhci_warn(xhci, "Timeout waiting for reset device command\n"); in xhci_discover_or_reset_device()
3812 ret = -ETIME; in xhci_discover_or_reset_device()
3816 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n", in xhci_discover_or_reset_device()
3818 xhci_get_slot_state(xhci, virt_dev->out_ctx)); in xhci_discover_or_reset_device()
3819 xhci_dbg(xhci, "Not freeing device rings.\n"); in xhci_discover_or_reset_device()
3824 xhci_dbg(xhci, "Successful reset device command.\n"); in xhci_discover_or_reset_device()
3827 if (xhci_is_vendor_info_code(xhci, ret)) in xhci_discover_or_reset_device()
3829 xhci_warn(xhci, "Unknown completion code %u for " in xhci_discover_or_reset_device()
3831 ret = -EINVAL; in xhci_discover_or_reset_device()
3836 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { in xhci_discover_or_reset_device()
3837 spin_lock_irqsave(&xhci->lock, flags); in xhci_discover_or_reset_device()
3839 xhci_free_device_endpoint_resources(xhci, virt_dev, false); in xhci_discover_or_reset_device()
3840 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_discover_or_reset_device()
3845 struct xhci_virt_ep *ep = &virt_dev->eps[i]; in xhci_discover_or_reset_device()
3847 if (ep->ep_state & EP_HAS_STREAMS) { in xhci_discover_or_reset_device()
3848 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n", in xhci_discover_or_reset_device()
3850 xhci_free_stream_info(xhci, ep->stream_info); in xhci_discover_or_reset_device()
3851 ep->stream_info = NULL; in xhci_discover_or_reset_device()
3852 ep->ep_state &= ~EP_HAS_STREAMS; in xhci_discover_or_reset_device()
3855 if (ep->ring) { in xhci_discover_or_reset_device()
3856 xhci_debugfs_remove_endpoint(xhci, virt_dev, i); in xhci_discover_or_reset_device()
3857 xhci_free_endpoint_ring(xhci, virt_dev, i); in xhci_discover_or_reset_device()
3859 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list)) in xhci_discover_or_reset_device()
3860 xhci_drop_ep_from_interval_table(xhci, in xhci_discover_or_reset_device()
3861 &virt_dev->eps[i].bw_info, in xhci_discover_or_reset_device()
3862 virt_dev->bw_table, in xhci_discover_or_reset_device()
3864 &virt_dev->eps[i], in xhci_discover_or_reset_device()
3865 virt_dev->tt_info); in xhci_discover_or_reset_device()
3866 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info); in xhci_discover_or_reset_device()
3869 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps); in xhci_discover_or_reset_device()
3870 virt_dev->flags = 0; in xhci_discover_or_reset_device()
3874 xhci_free_command(xhci, reset_device_cmd); in xhci_discover_or_reset_device()
3885 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_free_dev() local
3896 if (xhci->quirks & XHCI_RESET_ON_RESUME) in xhci_free_dev()
3897 pm_runtime_put_noidle(hcd->self.controller); in xhci_free_dev()
3903 if (ret <= 0 && ret != -ENODEV) in xhci_free_dev()
3906 virt_dev = xhci->devs[udev->slot_id]; in xhci_free_dev()
3907 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_free_dev()
3912 virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING; in xhci_free_dev()
3913 virt_dev->udev = NULL; in xhci_free_dev()
3914 xhci_disable_slot(xhci, udev->slot_id); in xhci_free_dev()
3916 spin_lock_irqsave(&xhci->lock, flags); in xhci_free_dev()
3917 xhci_free_virt_device(xhci, udev->slot_id); in xhci_free_dev()
3918 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_free_dev()
3922 int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id) in xhci_disable_slot() argument
3929 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_disable_slot()
3931 return -ENOMEM; in xhci_disable_slot()
3933 xhci_debugfs_remove_slot(xhci, slot_id); in xhci_disable_slot()
3935 spin_lock_irqsave(&xhci->lock, flags); in xhci_disable_slot()
3937 state = readl(&xhci->op_regs->status); in xhci_disable_slot()
3938 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) || in xhci_disable_slot()
3939 (xhci->xhc_state & XHCI_STATE_HALTED)) { in xhci_disable_slot()
3940 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_slot()
3942 return -ENODEV; in xhci_disable_slot()
3945 ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT, in xhci_disable_slot()
3948 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_slot()
3952 xhci_ring_cmd_db(xhci); in xhci_disable_slot()
3953 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_disable_slot()
3955 wait_for_completion(command->completion); in xhci_disable_slot()
3957 if (command->status != COMP_SUCCESS) in xhci_disable_slot()
3958 xhci_warn(xhci, "Unsuccessful disable slot %u command, status %d\n", in xhci_disable_slot()
3959 slot_id, command->status); in xhci_disable_slot()
3961 xhci_free_command(xhci, command); in xhci_disable_slot()
3970 * Must be called with xhci->lock held.
3972 static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci) in xhci_reserve_host_control_ep_resources() argument
3974 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) { in xhci_reserve_host_control_ep_resources()
3975 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_control_ep_resources()
3978 xhci->num_active_eps, xhci->limit_active_eps); in xhci_reserve_host_control_ep_resources()
3979 return -ENOMEM; in xhci_reserve_host_control_ep_resources()
3981 xhci->num_active_eps += 1; in xhci_reserve_host_control_ep_resources()
3982 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks, in xhci_reserve_host_control_ep_resources()
3984 xhci->num_active_eps); in xhci_reserve_host_control_ep_resources()
3995 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_alloc_dev() local
4002 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_alloc_dev()
4006 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_dev()
4007 ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0); in xhci_alloc_dev()
4009 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
4010 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); in xhci_alloc_dev()
4011 xhci_free_command(xhci, command); in xhci_alloc_dev()
4014 xhci_ring_cmd_db(xhci); in xhci_alloc_dev()
4015 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
4017 wait_for_completion(command->completion); in xhci_alloc_dev()
4018 slot_id = command->slot_id; in xhci_alloc_dev()
4020 if (!slot_id || command->status != COMP_SUCCESS) { in xhci_alloc_dev()
4021 xhci_err(xhci, "Error while assigning device slot ID: %s\n", in xhci_alloc_dev()
4022 xhci_trb_comp_code_string(command->status)); in xhci_alloc_dev()
4023 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n", in xhci_alloc_dev()
4025 readl(&xhci->cap_regs->hcs_params1))); in xhci_alloc_dev()
4026 xhci_free_command(xhci, command); in xhci_alloc_dev()
4030 xhci_free_command(xhci, command); in xhci_alloc_dev()
4032 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) { in xhci_alloc_dev()
4033 spin_lock_irqsave(&xhci->lock, flags); in xhci_alloc_dev()
4034 ret = xhci_reserve_host_control_ep_resources(xhci); in xhci_alloc_dev()
4036 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
4037 xhci_warn(xhci, "Not enough host resources, " in xhci_alloc_dev()
4039 xhci->num_active_eps); in xhci_alloc_dev()
4042 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_alloc_dev()
4048 if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) { in xhci_alloc_dev()
4049 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n"); in xhci_alloc_dev()
4052 vdev = xhci->devs[slot_id]; in xhci_alloc_dev()
4053 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx); in xhci_alloc_dev()
4056 udev->slot_id = slot_id; in xhci_alloc_dev()
4058 xhci_debugfs_create_slot(xhci, slot_id); in xhci_alloc_dev()
4064 if (xhci->quirks & XHCI_RESET_ON_RESUME) in xhci_alloc_dev()
4065 pm_runtime_get_noresume(hcd->self.controller); in xhci_alloc_dev()
4072 xhci_disable_slot(xhci, udev->slot_id); in xhci_alloc_dev()
4073 xhci_free_virt_device(xhci, udev->slot_id); in xhci_alloc_dev()
4079 * xhci_setup_device - issues an Address Device command to assign a unique
4095 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_setup_device() local
4101 mutex_lock(&xhci->mutex); in xhci_setup_device()
4103 if (xhci->xhc_state) { /* dying, removing or halted */ in xhci_setup_device()
4104 ret = -ESHUTDOWN; in xhci_setup_device()
4108 if (!udev->slot_id) { in xhci_setup_device()
4109 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4110 "Bad Slot ID %d", udev->slot_id); in xhci_setup_device()
4111 ret = -EINVAL; in xhci_setup_device()
4115 virt_dev = xhci->devs[udev->slot_id]; in xhci_setup_device()
4120 * a zero-dereference was observed once due to virt_dev = 0. in xhci_setup_device()
4123 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n", in xhci_setup_device()
4124 udev->slot_id); in xhci_setup_device()
4125 ret = -EINVAL; in xhci_setup_device()
4128 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_setup_device()
4132 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) == in xhci_setup_device()
4134 xhci_dbg(xhci, "Slot already in default state\n"); in xhci_setup_device()
4139 command = xhci_alloc_command(xhci, true, GFP_KERNEL); in xhci_setup_device()
4141 ret = -ENOMEM; in xhci_setup_device()
4145 command->in_ctx = virt_dev->in_ctx; in xhci_setup_device()
4146 command->timeout_ms = timeout_ms; in xhci_setup_device()
4148 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); in xhci_setup_device()
4149 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); in xhci_setup_device()
4151 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_setup_device()
4153 ret = -EINVAL; in xhci_setup_device()
4157 * If this is the first Set Address since device plug-in or in xhci_setup_device()
4158 * virt_device realloaction after a resume with an xHCI power loss, in xhci_setup_device()
4161 if (!slot_ctx->dev_info) in xhci_setup_device()
4162 xhci_setup_addressable_virt_dev(xhci, udev); in xhci_setup_device()
4165 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev); in xhci_setup_device()
4166 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG); in xhci_setup_device()
4167 ctrl_ctx->drop_flags = 0; in xhci_setup_device()
4169 trace_xhci_address_ctx(xhci, virt_dev->in_ctx, in xhci_setup_device()
4170 le32_to_cpu(slot_ctx->dev_info) >> 27); in xhci_setup_device()
4173 spin_lock_irqsave(&xhci->lock, flags); in xhci_setup_device()
4175 ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma, in xhci_setup_device()
4176 udev->slot_id, setup); in xhci_setup_device()
4178 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_setup_device()
4179 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4183 xhci_ring_cmd_db(xhci); in xhci_setup_device()
4184 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_setup_device()
4187 wait_for_completion(command->completion); in xhci_setup_device()
4193 switch (command->status) { in xhci_setup_device()
4196 xhci_warn(xhci, "Timeout while waiting for setup device command\n"); in xhci_setup_device()
4197 ret = -ETIME; in xhci_setup_device()
4201 xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n", in xhci_setup_device()
4202 act, udev->slot_id); in xhci_setup_device()
4203 ret = -EINVAL; in xhci_setup_device()
4206 dev_warn(&udev->dev, "Device not responding to setup %s.\n", act); in xhci_setup_device()
4208 mutex_unlock(&xhci->mutex); in xhci_setup_device()
4209 ret = xhci_disable_slot(xhci, udev->slot_id); in xhci_setup_device()
4210 xhci_free_virt_device(xhci, udev->slot_id); in xhci_setup_device()
4213 xhci_setup_addressable_virt_dev(xhci, udev); in xhci_setup_device()
4215 kfree(command->completion); in xhci_setup_device()
4217 return -EPROTO; in xhci_setup_device()
4219 dev_warn(&udev->dev, in xhci_setup_device()
4221 ret = -ENODEV; in xhci_setup_device()
4224 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4228 xhci_err(xhci, in xhci_setup_device()
4230 act, command->status); in xhci_setup_device()
4231 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1); in xhci_setup_device()
4232 ret = -EINVAL; in xhci_setup_device()
4237 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); in xhci_setup_device()
4238 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4240 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4242 udev->slot_id, in xhci_setup_device()
4243 &xhci->dcbaa->dev_context_ptrs[udev->slot_id], in xhci_setup_device()
4245 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id])); in xhci_setup_device()
4246 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4248 (unsigned long long)virt_dev->out_ctx->dma); in xhci_setup_device()
4249 trace_xhci_address_ctx(xhci, virt_dev->in_ctx, in xhci_setup_device()
4250 le32_to_cpu(slot_ctx->dev_info) >> 27); in xhci_setup_device()
4255 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, in xhci_setup_device()
4256 le32_to_cpu(slot_ctx->dev_info) >> 27); in xhci_setup_device()
4258 ctrl_ctx->add_flags = 0; in xhci_setup_device()
4259 ctrl_ctx->drop_flags = 0; in xhci_setup_device()
4260 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); in xhci_setup_device()
4261 udev->devaddr = (u8)(le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); in xhci_setup_device()
4263 xhci_dbg_trace(xhci, trace_xhci_dbg_address, in xhci_setup_device()
4265 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); in xhci_setup_device()
4267 mutex_unlock(&xhci->mutex); in xhci_setup_device()
4269 kfree(command->completion); in xhci_setup_device()
4298 return rhub->ports[port1 - 1]->hw_portnum + 1; in xhci_find_raw_port_number()
4305 static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci, in xhci_change_max_exit_latency() argument
4315 command = xhci_alloc_command_with_ctx(xhci, true, GFP_KERNEL); in xhci_change_max_exit_latency()
4317 return -ENOMEM; in xhci_change_max_exit_latency()
4319 spin_lock_irqsave(&xhci->lock, flags); in xhci_change_max_exit_latency()
4321 virt_dev = xhci->devs[udev->slot_id]; in xhci_change_max_exit_latency()
4325 * xHC was re-initialized. Exit latency will be set later after in xhci_change_max_exit_latency()
4326 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated in xhci_change_max_exit_latency()
4329 if (!virt_dev || max_exit_latency == virt_dev->current_mel) { in xhci_change_max_exit_latency()
4330 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4331 xhci_free_command(xhci, command); in xhci_change_max_exit_latency()
4336 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx); in xhci_change_max_exit_latency()
4338 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4339 xhci_free_command(xhci, command); in xhci_change_max_exit_latency()
4340 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_change_max_exit_latency()
4342 return -ENOMEM; in xhci_change_max_exit_latency()
4345 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx); in xhci_change_max_exit_latency()
4346 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4348 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_change_max_exit_latency()
4349 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx); in xhci_change_max_exit_latency()
4350 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT)); in xhci_change_max_exit_latency()
4351 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency); in xhci_change_max_exit_latency()
4352 slot_ctx->dev_state = 0; in xhci_change_max_exit_latency()
4354 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change, in xhci_change_max_exit_latency()
4358 ret = xhci_configure_endpoint(xhci, udev, command, in xhci_change_max_exit_latency()
4362 spin_lock_irqsave(&xhci->lock, flags); in xhci_change_max_exit_latency()
4363 virt_dev->current_mel = max_exit_latency; in xhci_change_max_exit_latency()
4364 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_change_max_exit_latency()
4367 xhci_free_command(xhci, command); in xhci_change_max_exit_latency()
4379 static int xhci_calculate_hird_besl(struct xhci_hcd *xhci, in xhci_calculate_hird_besl() argument
4386 u2del = HCS_U2_LATENCY(xhci->hcs_params3); in xhci_calculate_hird_besl()
4387 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); in xhci_calculate_hird_besl()
4403 besl_host = (u2del - 51) / 75 + 1; in xhci_calculate_hird_besl()
4421 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); in xhci_calculate_usb2_hw_lpm_params()
4423 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */ in xhci_calculate_usb2_hw_lpm_params()
4424 l1 = udev->l1_params.timeout / 256; in xhci_calculate_usb2_hw_lpm_params()
4438 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_set_usb2_hardware_lpm() local
4447 if (xhci->quirks & XHCI_HW_LPM_DISABLE) in xhci_set_usb2_hardware_lpm()
4448 return -EPERM; in xhci_set_usb2_hardware_lpm()
4450 if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support || in xhci_set_usb2_hardware_lpm()
4451 !udev->lpm_capable) in xhci_set_usb2_hardware_lpm()
4452 return -EPERM; in xhci_set_usb2_hardware_lpm()
4454 if (!udev->parent || udev->parent->parent || in xhci_set_usb2_hardware_lpm()
4455 udev->descriptor.bDeviceClass == USB_CLASS_HUB) in xhci_set_usb2_hardware_lpm()
4456 return -EPERM; in xhci_set_usb2_hardware_lpm()
4458 if (udev->usb2_hw_lpm_capable != 1) in xhci_set_usb2_hardware_lpm()
4459 return -EPERM; in xhci_set_usb2_hardware_lpm()
4461 spin_lock_irqsave(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4463 ports = xhci->usb2_rhub.ports; in xhci_set_usb2_hardware_lpm()
4464 port_num = udev->portnum - 1; in xhci_set_usb2_hardware_lpm()
4465 pm_addr = ports[port_num]->addr + PORTPMSC; in xhci_set_usb2_hardware_lpm()
4467 hlpm_addr = ports[port_num]->addr + PORTHLPMC; in xhci_set_usb2_hardware_lpm()
4469 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n", in xhci_set_usb2_hardware_lpm()
4474 if (udev->usb2_hw_lpm_besl_capable) { in xhci_set_usb2_hardware_lpm()
4477 * systems. See XHCI_DEFAULT_BESL definition in xhci.h in xhci_set_usb2_hardware_lpm()
4479 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes); in xhci_set_usb2_hardware_lpm()
4484 hird = udev->l1_params.besl; in xhci_set_usb2_hardware_lpm()
4487 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4489 ret = xhci_change_max_exit_latency(xhci, udev, in xhci_set_usb2_hardware_lpm()
4493 spin_lock_irqsave(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4500 hird = xhci_calculate_hird_besl(xhci, udev); in xhci_set_usb2_hardware_lpm()
4504 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id); in xhci_set_usb2_hardware_lpm()
4516 if (udev->usb2_hw_lpm_besl_capable) { in xhci_set_usb2_hardware_lpm()
4517 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4518 xhci_change_max_exit_latency(xhci, udev, 0); in xhci_set_usb2_hardware_lpm()
4519 readl_poll_timeout(ports[port_num]->addr, pm_val, in xhci_set_usb2_hardware_lpm()
4526 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_set_usb2_hardware_lpm()
4534 static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port, in xhci_check_usb2_port_capability() argument
4540 for (i = 0; i < xhci->num_ext_caps; i++) { in xhci_check_usb2_port_capability()
4541 if (xhci->ext_caps[i] & capability) { in xhci_check_usb2_port_capability()
4543 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1; in xhci_check_usb2_port_capability()
4544 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]); in xhci_check_usb2_port_capability()
4555 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_update_device() local
4556 int portnum = udev->portnum - 1; in xhci_update_device()
4558 if (hcd->speed >= HCD_USB3 || !udev->lpm_capable) in xhci_update_device()
4561 /* we only support lpm for non-hub device connected to root hub yet */ in xhci_update_device()
4562 if (!udev->parent || udev->parent->parent || in xhci_update_device()
4563 udev->descriptor.bDeviceClass == USB_CLASS_HUB) in xhci_update_device()
4566 if (xhci->hw_lpm_support == 1 && in xhci_update_device()
4568 xhci, portnum, XHCI_HLC)) { in xhci_update_device()
4569 udev->usb2_hw_lpm_capable = 1; in xhci_update_device()
4570 udev->l1_params.timeout = XHCI_L1_TIMEOUT; in xhci_update_device()
4571 udev->l1_params.besl = XHCI_DEFAULT_BESL; in xhci_update_device()
4572 if (xhci_check_usb2_port_capability(xhci, portnum, in xhci_update_device()
4574 udev->usb2_hw_lpm_besl_capable = 1; in xhci_update_device()
4580 /*---------------------- USB 3.0 Link PM functions ------------------------*/
4582 /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4586 return (1ULL << (desc->bInterval - 1)) * 125 * 1000; in xhci_service_interval_to_ns()
4600 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); in xhci_get_timeout_no_hub_lpm()
4601 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); in xhci_get_timeout_no_hub_lpm()
4606 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); in xhci_get_timeout_no_hub_lpm()
4607 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); in xhci_get_timeout_no_hub_lpm()
4612 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n", in xhci_get_timeout_no_hub_lpm()
4621 dev_dbg(&udev->dev, "Device-initiated %s disabled " in xhci_get_timeout_no_hub_lpm()
4625 dev_dbg(&udev->dev, "Device-initiated %s disabled " in xhci_get_timeout_no_hub_lpm()
4632 * - For control endpoints, U1 system exit latency (SEL) * 3
4633 * - For bulk endpoints, U1 SEL * 5
4634 * - For interrupt endpoints:
4635 * - Notification EPs, U1 SEL * 3
4636 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4637 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4650 timeout_ns = udev->u1_params.sel * 3; in xhci_calculate_intel_u1_timeout()
4653 timeout_ns = udev->u1_params.sel * 5; in xhci_calculate_intel_u1_timeout()
4658 timeout_ns = udev->u1_params.sel * 3; in xhci_calculate_intel_u1_timeout()
4666 if (timeout_ns < udev->u1_params.sel * 2) in xhci_calculate_intel_u1_timeout()
4667 timeout_ns = udev->u1_params.sel * 2; in xhci_calculate_intel_u1_timeout()
4676 /* Returns the hub-encoded U1 timeout value. */
4677 static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci, in xhci_calculate_u1_timeout() argument
4685 if (xhci_service_interval_to_ns(desc) <= udev->u1_params.mel) { in xhci_calculate_u1_timeout()
4686 dev_dbg(&udev->dev, "Disable U1, ESIT shorter than exit latency\n"); in xhci_calculate_u1_timeout()
4691 if (xhci->quirks & (XHCI_INTEL_HOST | XHCI_ZHAOXIN_HOST)) in xhci_calculate_u1_timeout()
4694 timeout_ns = udev->u1_params.sel; in xhci_calculate_u1_timeout()
4705 * USB 3.0 hub, we have to disable hub-initiated U1. in xhci_calculate_u1_timeout()
4709 dev_dbg(&udev->dev, "Hub-initiated U1 disabled " in xhci_calculate_u1_timeout()
4715 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4716 * - largest bInterval of any active periodic endpoint (to avoid going
4718 * - the U2 Exit Latency of the device
4733 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL; in xhci_calculate_intel_u2_timeout()
4740 /* Returns the hub-encoded U2 timeout value. */
4741 static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci, in xhci_calculate_u2_timeout() argument
4749 if (xhci_service_interval_to_ns(desc) <= udev->u2_params.mel) { in xhci_calculate_u2_timeout()
4750 dev_dbg(&udev->dev, "Disable U2, ESIT shorter than exit latency\n"); in xhci_calculate_u2_timeout()
4755 if (xhci->quirks & (XHCI_INTEL_HOST | XHCI_ZHAOXIN_HOST)) in xhci_calculate_u2_timeout()
4758 timeout_ns = udev->u2_params.sel; in xhci_calculate_u2_timeout()
4763 * USB 3.0 hub, we have to disable hub-initiated U2. in xhci_calculate_u2_timeout()
4767 dev_dbg(&udev->dev, "Hub-initiated U2 disabled " in xhci_calculate_u2_timeout()
4772 static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci, in xhci_call_host_update_timeout_for_endpoint() argument
4779 return xhci_calculate_u1_timeout(xhci, udev, desc); in xhci_call_host_update_timeout_for_endpoint()
4781 return xhci_calculate_u2_timeout(xhci, udev, desc); in xhci_call_host_update_timeout_for_endpoint()
4786 static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci, in xhci_update_timeout_for_endpoint() argument
4794 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev, in xhci_update_timeout_for_endpoint()
4797 /* If we found we can't enable hub-initiated LPM, and in xhci_update_timeout_for_endpoint()
4799 * device-initiated LPM as well, then we will disable LPM in xhci_update_timeout_for_endpoint()
4804 return -E2BIG; in xhci_update_timeout_for_endpoint()
4811 static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci, in xhci_update_timeout_for_interface() argument
4819 for (j = 0; j < alt->desc.bNumEndpoints; j++) { in xhci_update_timeout_for_interface()
4820 if (xhci_update_timeout_for_endpoint(xhci, udev, in xhci_update_timeout_for_interface()
4821 &alt->endpoint[j].desc, state, timeout)) in xhci_update_timeout_for_interface()
4822 return -E2BIG; in xhci_update_timeout_for_interface()
4827 static int xhci_check_tier_policy(struct xhci_hcd *xhci, in xhci_check_tier_policy() argument
4831 struct usb_device *parent = udev->parent; in xhci_check_tier_policy()
4835 parent = parent->parent; in xhci_check_tier_policy()
4839 if (xhci->quirks & XHCI_INTEL_HOST && tier > 3) in xhci_check_tier_policy()
4841 if (xhci->quirks & XHCI_ZHAOXIN_HOST && tier > 2) in xhci_check_tier_policy()
4846 dev_dbg(&udev->dev, "Tier policy prevents U1/U2 LPM states for devices at tier %d\n", in xhci_check_tier_policy()
4848 return -E2BIG; in xhci_check_tier_policy()
4852 * If the tier check or timeout setting functions return with a non-zero exit
4859 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_calculate_lpm_timeout() local
4870 dev_warn(&udev->dev, "Can't enable unknown link state %i\n", in xhci_calculate_lpm_timeout()
4878 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc, in xhci_calculate_lpm_timeout()
4882 config = udev->actconfig; in xhci_calculate_lpm_timeout()
4886 for (i = 0; i < config->desc.bNumInterfaces; i++) { in xhci_calculate_lpm_timeout()
4888 struct usb_interface *intf = config->interface[i]; in xhci_calculate_lpm_timeout()
4893 /* Check if any currently bound drivers want hub-initiated LPM in xhci_calculate_lpm_timeout()
4896 if (intf->dev.driver) { in xhci_calculate_lpm_timeout()
4897 driver = to_usb_driver(intf->dev.driver); in xhci_calculate_lpm_timeout()
4898 if (driver && driver->disable_hub_initiated_lpm) { in xhci_calculate_lpm_timeout()
4899 dev_dbg(&udev->dev, "Hub-initiated %s disabled at request of driver %s\n", in xhci_calculate_lpm_timeout()
4900 state_name, driver->name); in xhci_calculate_lpm_timeout()
4909 if (!intf->cur_altsetting) in xhci_calculate_lpm_timeout()
4912 if (xhci_update_timeout_for_interface(xhci, udev, in xhci_calculate_lpm_timeout()
4913 intf->cur_altsetting, in xhci_calculate_lpm_timeout()
4945 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) || in calculate_max_exit_latency()
4947 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000); in calculate_max_exit_latency()
4948 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) || in calculate_max_exit_latency()
4950 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000); in calculate_max_exit_latency()
4954 /* xHCI host controller max exit latency field is only 16 bits wide. */ in calculate_max_exit_latency()
4956 dev_warn(&udev->dev, "Link PM max exit latency of %lluus " in calculate_max_exit_latency()
4958 return -E2BIG; in calculate_max_exit_latency()
4963 /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4967 struct xhci_hcd *xhci; in xhci_enable_usb3_lpm_timeout() local
4973 xhci = hcd_to_xhci(hcd); in xhci_enable_usb3_lpm_timeout()
4974 /* The LPM timeout values are pretty host-controller specific, so don't in xhci_enable_usb3_lpm_timeout()
4975 * enable hub-initiated timeouts unless the vendor has provided in xhci_enable_usb3_lpm_timeout()
4978 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) || in xhci_enable_usb3_lpm_timeout()
4979 !xhci->devs[udev->slot_id]) in xhci_enable_usb3_lpm_timeout()
4982 if (xhci_check_tier_policy(xhci, udev, state) < 0) in xhci_enable_usb3_lpm_timeout()
4986 if (udev->parent && !udev->parent->parent) { in xhci_enable_usb3_lpm_timeout()
4987 port = xhci->usb3_rhub.ports[udev->portnum - 1]; in xhci_enable_usb3_lpm_timeout()
4988 if (port->lpm_incapable) in xhci_enable_usb3_lpm_timeout()
5000 ret = xhci_change_max_exit_latency(xhci, udev, mel); in xhci_enable_usb3_lpm_timeout()
5009 struct xhci_hcd *xhci; in xhci_disable_usb3_lpm_timeout() local
5012 xhci = hcd_to_xhci(hcd); in xhci_disable_usb3_lpm_timeout()
5013 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) || in xhci_disable_usb3_lpm_timeout()
5014 !xhci->devs[udev->slot_id]) in xhci_disable_usb3_lpm_timeout()
5018 return xhci_change_max_exit_latency(xhci, udev, mel); in xhci_disable_usb3_lpm_timeout()
5046 /*-------------------------------------------------------------------------*/
5054 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_update_hub_device() local
5064 if (!hdev->parent) in xhci_update_hub_device()
5067 vdev = xhci->devs[hdev->slot_id]; in xhci_update_hub_device()
5069 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n"); in xhci_update_hub_device()
5070 return -EINVAL; in xhci_update_hub_device()
5073 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags); in xhci_update_hub_device()
5075 return -ENOMEM; in xhci_update_hub_device()
5077 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx); in xhci_update_hub_device()
5079 xhci_warn(xhci, "%s: Could not get input context, bad type.\n", in xhci_update_hub_device()
5081 xhci_free_command(xhci, config_cmd); in xhci_update_hub_device()
5082 return -ENOMEM; in xhci_update_hub_device()
5085 spin_lock_irqsave(&xhci->lock, flags); in xhci_update_hub_device()
5086 if (hdev->speed == USB_SPEED_HIGH && in xhci_update_hub_device()
5087 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) { in xhci_update_hub_device()
5088 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n"); in xhci_update_hub_device()
5089 xhci_free_command(xhci, config_cmd); in xhci_update_hub_device()
5090 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_update_hub_device()
5091 return -ENOMEM; in xhci_update_hub_device()
5094 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx); in xhci_update_hub_device()
5095 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); in xhci_update_hub_device()
5096 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx); in xhci_update_hub_device()
5097 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB); in xhci_update_hub_device()
5100 * but it may be already set to 1 when setup an xHCI virtual in xhci_update_hub_device()
5103 if (tt->multi) in xhci_update_hub_device()
5104 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); in xhci_update_hub_device()
5105 else if (hdev->speed == USB_SPEED_FULL) in xhci_update_hub_device()
5106 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT); in xhci_update_hub_device()
5108 if (xhci->hci_version > 0x95) { in xhci_update_hub_device()
5109 xhci_dbg(xhci, "xHCI version %x needs hub " in xhci_update_hub_device()
5111 (unsigned int) xhci->hci_version); in xhci_update_hub_device()
5112 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild)); in xhci_update_hub_device()
5113 /* Set TT think time - convert from ns to FS bit times. in xhci_update_hub_device()
5117 * xHCI 1.0: this field shall be 0 if the device is not a in xhci_update_hub_device()
5118 * High-spped hub. in xhci_update_hub_device()
5120 think_time = tt->think_time; in xhci_update_hub_device()
5122 think_time = (think_time / 666) - 1; in xhci_update_hub_device()
5123 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH) in xhci_update_hub_device()
5124 slot_ctx->tt_info |= in xhci_update_hub_device()
5127 xhci_dbg(xhci, "xHCI version %x doesn't need hub " in xhci_update_hub_device()
5129 (unsigned int) xhci->hci_version); in xhci_update_hub_device()
5131 slot_ctx->dev_state = 0; in xhci_update_hub_device()
5132 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_update_hub_device()
5134 xhci_dbg(xhci, "Set up %s for hub device.\n", in xhci_update_hub_device()
5135 (xhci->hci_version > 0x95) ? in xhci_update_hub_device()
5141 if (xhci->hci_version > 0x95) in xhci_update_hub_device()
5142 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, in xhci_update_hub_device()
5145 ret = xhci_configure_endpoint(xhci, hdev, config_cmd, in xhci_update_hub_device()
5148 xhci_free_command(xhci, config_cmd); in xhci_update_hub_device()
5155 struct xhci_hcd *xhci = hcd_to_xhci(hcd); in xhci_get_frame() local
5157 return readl(&xhci->run_regs->microframe_index) >> 3; in xhci_get_frame()
5160 static void xhci_hcd_init_usb2_data(struct xhci_hcd *xhci, struct usb_hcd *hcd) in xhci_hcd_init_usb2_data() argument
5162 xhci->usb2_rhub.hcd = hcd; in xhci_hcd_init_usb2_data()
5163 hcd->speed = HCD_USB2; in xhci_hcd_init_usb2_data()
5164 hcd->self.root_hub->speed = USB_SPEED_HIGH; in xhci_hcd_init_usb2_data()
5166 * USB 2.0 roothub under xHCI has an integrated TT, in xhci_hcd_init_usb2_data()
5170 hcd->has_tt = 1; in xhci_hcd_init_usb2_data()
5173 static void xhci_hcd_init_usb3_data(struct xhci_hcd *xhci, struct usb_hcd *hcd) in xhci_hcd_init_usb3_data() argument
5178 * Early xHCI 1.1 spec did not mention USB 3.1 capable hosts in xhci_hcd_init_usb3_data()
5180 * is a two digit BCD containig minor and sub-minor numbers. in xhci_hcd_init_usb3_data()
5181 * This was later clarified in xHCI 1.2. in xhci_hcd_init_usb3_data()
5186 if (xhci->usb3_rhub.min_rev == 0x1) in xhci_hcd_init_usb3_data()
5189 minor_rev = xhci->usb3_rhub.min_rev / 0x10; in xhci_hcd_init_usb3_data()
5193 hcd->speed = HCD_USB32; in xhci_hcd_init_usb3_data()
5194 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS; in xhci_hcd_init_usb3_data()
5195 hcd->self.root_hub->rx_lanes = 2; in xhci_hcd_init_usb3_data()
5196 hcd->self.root_hub->tx_lanes = 2; in xhci_hcd_init_usb3_data()
5197 hcd->self.root_hub->ssp_rate = USB_SSP_GEN_2x2; in xhci_hcd_init_usb3_data()
5200 hcd->speed = HCD_USB31; in xhci_hcd_init_usb3_data()
5201 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS; in xhci_hcd_init_usb3_data()
5202 hcd->self.root_hub->ssp_rate = USB_SSP_GEN_2x1; in xhci_hcd_init_usb3_data()
5205 xhci_info(xhci, "Host supports USB 3.%x %sSuperSpeed\n", in xhci_hcd_init_usb3_data()
5208 xhci->usb3_rhub.hcd = hcd; in xhci_hcd_init_usb3_data()
5213 struct xhci_hcd *xhci; in xhci_gen_setup() local
5218 struct device *dev = hcd->self.sysdev; in xhci_gen_setup()
5221 /* Accept arbitrarily long scatter-gather lists */ in xhci_gen_setup()
5222 hcd->self.sg_tablesize = ~0; in xhci_gen_setup()
5225 hcd->self.no_sg_constraint = 1; in xhci_gen_setup()
5227 /* XHCI controllers don't stop the ep queue on short packets :| */ in xhci_gen_setup()
5228 hcd->self.no_stop_on_short = 1; in xhci_gen_setup()
5230 xhci = hcd_to_xhci(hcd); in xhci_gen_setup()
5233 xhci_hcd_init_usb3_data(xhci, hcd); in xhci_gen_setup()
5237 mutex_init(&xhci->mutex); in xhci_gen_setup()
5238 xhci->main_hcd = hcd; in xhci_gen_setup()
5239 xhci->cap_regs = hcd->regs; in xhci_gen_setup()
5240 xhci->op_regs = hcd->regs + in xhci_gen_setup()
5241 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase)); in xhci_gen_setup()
5242 xhci->run_regs = hcd->regs + in xhci_gen_setup()
5243 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK); in xhci_gen_setup()
5244 /* Cache read-only capability registers */ in xhci_gen_setup()
5245 xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1); in xhci_gen_setup()
5246 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2); in xhci_gen_setup()
5247 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3); in xhci_gen_setup()
5248 xhci->hci_version = HC_VERSION(readl(&xhci->cap_regs->hc_capbase)); in xhci_gen_setup()
5249 xhci->hcc_params = readl(&xhci->cap_regs->hcc_params); in xhci_gen_setup()
5250 if (xhci->hci_version > 0x100) in xhci_gen_setup()
5251 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2); in xhci_gen_setup()
5253 /* xhci-plat or xhci-pci might have set max_interrupters already */ in xhci_gen_setup()
5254 if ((!xhci->max_interrupters) || in xhci_gen_setup()
5255 xhci->max_interrupters > HCS_MAX_INTRS(xhci->hcs_params1)) in xhci_gen_setup()
5256 xhci->max_interrupters = HCS_MAX_INTRS(xhci->hcs_params1); in xhci_gen_setup()
5258 xhci->quirks |= quirks; in xhci_gen_setup()
5261 get_quirks(dev, xhci); in xhci_gen_setup()
5263 /* In xhci controllers which follow xhci 1.0 spec gives a spurious in xhci_gen_setup()
5267 if (xhci->hci_version > 0x96) in xhci_gen_setup()
5268 xhci->quirks |= XHCI_SPURIOUS_SUCCESS; in xhci_gen_setup()
5271 retval = xhci_halt(xhci); in xhci_gen_setup()
5275 xhci_zero_64b_regs(xhci); in xhci_gen_setup()
5277 xhci_dbg(xhci, "Resetting HCD\n"); in xhci_gen_setup()
5279 retval = xhci_reset(xhci, XHCI_RESET_LONG_USEC); in xhci_gen_setup()
5282 xhci_dbg(xhci, "Reset complete\n"); in xhci_gen_setup()
5285 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0) in xhci_gen_setup()
5286 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit in xhci_gen_setup()
5288 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev, in xhci_gen_setup()
5291 if (xhci->quirks & XHCI_NO_64BIT_SUPPORT) in xhci_gen_setup()
5292 xhci->hcc_params &= ~BIT(0); in xhci_gen_setup()
5294 /* Set dma_mask and coherent_dma_mask to 64-bits, in xhci_gen_setup()
5295 * if xHC supports 64-bit addressing */ in xhci_gen_setup()
5296 if (HCC_64BIT_ADDR(xhci->hcc_params) && in xhci_gen_setup()
5298 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n"); in xhci_gen_setup()
5302 * This is to avoid error in cases where a 32-bit USB in xhci_gen_setup()
5303 * controller is used on a 64-bit capable system. in xhci_gen_setup()
5308 xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n"); in xhci_gen_setup()
5312 xhci_dbg(xhci, "Calling HCD init\n"); in xhci_gen_setup()
5317 xhci_dbg(xhci, "Called HCD init\n"); in xhci_gen_setup()
5320 xhci_hcd_init_usb3_data(xhci, hcd); in xhci_gen_setup()
5322 xhci_hcd_init_usb2_data(xhci, hcd); in xhci_gen_setup()
5324 xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%016llx\n", in xhci_gen_setup()
5325 xhci->hcc_params, xhci->hci_version, xhci->quirks); in xhci_gen_setup()
5334 struct xhci_hcd *xhci; in xhci_clear_tt_buffer_complete() local
5340 xhci = hcd_to_xhci(hcd); in xhci_clear_tt_buffer_complete()
5342 spin_lock_irqsave(&xhci->lock, flags); in xhci_clear_tt_buffer_complete()
5343 udev = (struct usb_device *)ep->hcpriv; in xhci_clear_tt_buffer_complete()
5344 slot_id = udev->slot_id; in xhci_clear_tt_buffer_complete()
5345 ep_index = xhci_get_endpoint_index(&ep->desc); in xhci_clear_tt_buffer_complete()
5347 xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_CLEARING_TT; in xhci_clear_tt_buffer_complete()
5348 xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index); in xhci_clear_tt_buffer_complete()
5349 spin_unlock_irqrestore(&xhci->lock, flags); in xhci_clear_tt_buffer_complete()
5353 .description = "xhci-hcd",
5354 .product_desc = "xHCI Host Controller",
5358 * generic hardware linkage
5424 /* Copy the generic table to drv then apply the overrides */ in xhci_init_driver()
5428 drv->hcd_priv_size += over->extra_priv_size; in xhci_init_driver()
5429 if (over->reset) in xhci_init_driver()
5430 drv->reset = over->reset; in xhci_init_driver()
5431 if (over->start) in xhci_init_driver()
5432 drv->start = over->start; in xhci_init_driver()
5433 if (over->add_endpoint) in xhci_init_driver()
5434 drv->add_endpoint = over->add_endpoint; in xhci_init_driver()
5435 if (over->drop_endpoint) in xhci_init_driver()
5436 drv->drop_endpoint = over->drop_endpoint; in xhci_init_driver()
5437 if (over->check_bandwidth) in xhci_init_driver()
5438 drv->check_bandwidth = over->check_bandwidth; in xhci_init_driver()
5439 if (over->reset_bandwidth) in xhci_init_driver()
5440 drv->reset_bandwidth = over->reset_bandwidth; in xhci_init_driver()
5441 if (over->update_hub_device) in xhci_init_driver()
5442 drv->update_hub_device = over->update_hub_device; in xhci_init_driver()
5443 if (over->hub_control) in xhci_init_driver()
5444 drv->hub_control = over->hub_control; in xhci_init_driver()
5474 return -ENODEV; in xhci_hcd_init()