Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

xhci: disable U3 suspended ports in S4 hibernate poweroff_late stage

Disable U3 suspended ports in hibernate S4 poweroff_late for systems
with XHCI_RESET_TO_DEFAULT quirk, if wakeup is not enabled.

This reduces the number of self-powered usb devices from surviving in
U3 suspended state into next reboot.

Bootloader/firmware on these systems can't handle usb ports in U3, and
will timeout, causing extra delay during reboot/restore from S4.

Add pci_poweroff_late() callback to struct usb_hcd to get this done at
the correct stage in hibernate.

Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
Link: https://lore.kernel.org/r/20221130091944.2171610-5-mathias.nyman@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Mathias Nyman and committed by
Greg Kroah-Hartman
c3bbacd6 705c333a

+68
+13
drivers/usb/core/hcd-pci.c
··· 558 558 return retval; 559 559 } 560 560 561 + static int hcd_pci_poweroff_late(struct device *dev) 562 + { 563 + struct pci_dev *pci_dev = to_pci_dev(dev); 564 + struct usb_hcd *hcd = pci_get_drvdata(pci_dev); 565 + 566 + if (hcd->driver->pci_poweroff_late && !HCD_DEAD(hcd)) 567 + return hcd->driver->pci_poweroff_late(hcd, device_may_wakeup(dev)); 568 + 569 + return 0; 570 + } 571 + 561 572 static int hcd_pci_resume_noirq(struct device *dev) 562 573 { 563 574 powermac_set_asic(to_pci_dev(dev), 1); ··· 589 578 590 579 #define hcd_pci_suspend NULL 591 580 #define hcd_pci_suspend_noirq NULL 581 + #define hcd_pci_poweroff_late NULL 592 582 #define hcd_pci_resume_noirq NULL 593 583 #define hcd_pci_resume NULL 594 584 #define hcd_pci_restore NULL ··· 627 615 .thaw_noirq = NULL, 628 616 .thaw = hcd_pci_resume, 629 617 .poweroff = hcd_pci_suspend, 618 + .poweroff_late = hcd_pci_poweroff_late, 630 619 .poweroff_noirq = hcd_pci_suspend_noirq, 631 620 .restore_noirq = hcd_pci_resume_noirq, 632 621 .restore = hcd_pci_restore,
+52
drivers/usb/host/xhci-pci.c
··· 622 622 return retval; 623 623 } 624 624 625 + static int xhci_pci_poweroff_late(struct usb_hcd *hcd, bool do_wakeup) 626 + { 627 + struct xhci_hcd *xhci = hcd_to_xhci(hcd); 628 + struct xhci_port *port; 629 + struct usb_device *udev; 630 + unsigned int slot_id; 631 + u32 portsc; 632 + int i; 633 + 634 + /* 635 + * Systems with XHCI_RESET_TO_DEFAULT quirk have boot firmware that 636 + * cause significant boot delay if usb ports are in suspended U3 state 637 + * during boot. Some USB devices survive in U3 state over S4 hibernate 638 + * 639 + * Disable ports that are in U3 if remote wake is not enabled for either 640 + * host controller or connected device 641 + */ 642 + 643 + if (!(xhci->quirks & XHCI_RESET_TO_DEFAULT)) 644 + return 0; 645 + 646 + for (i = 0; i < HCS_MAX_PORTS(xhci->hcs_params1); i++) { 647 + port = &xhci->hw_ports[i]; 648 + portsc = readl(port->addr); 649 + 650 + if ((portsc & PORT_PLS_MASK) != XDEV_U3) 651 + continue; 652 + 653 + slot_id = xhci_find_slot_id_by_port(port->rhub->hcd, xhci, 654 + port->hcd_portnum + 1); 655 + if (!slot_id || !xhci->devs[slot_id]) { 656 + xhci_err(xhci, "No dev for slot_id %d for port %d-%d in U3\n", 657 + slot_id, port->rhub->hcd->self.busnum, port->hcd_portnum + 1); 658 + continue; 659 + } 660 + 661 + udev = xhci->devs[slot_id]->udev; 662 + 663 + /* if wakeup is enabled then don't disable the port */ 664 + if (udev->do_remote_wakeup && do_wakeup) 665 + continue; 666 + 667 + xhci_dbg(xhci, "port %d-%d in U3 without wakeup, disable it\n", 668 + port->rhub->hcd->self.busnum, port->hcd_portnum + 1); 669 + portsc = xhci_port_state_to_neutral(portsc); 670 + writel(portsc | PORT_PE, port->addr); 671 + } 672 + 673 + return 0; 674 + } 675 + 625 676 static void xhci_pci_shutdown(struct usb_hcd *hcd) 626 677 { 627 678 struct xhci_hcd *xhci = hcd_to_xhci(hcd); ··· 740 689 #ifdef CONFIG_PM 741 690 xhci_pci_hc_driver.pci_suspend = xhci_pci_suspend; 742 691 xhci_pci_hc_driver.pci_resume = xhci_pci_resume; 692 + xhci_pci_hc_driver.pci_poweroff_late = xhci_pci_poweroff_late; 743 693 xhci_pci_hc_driver.shutdown = xhci_pci_shutdown; 744 694 #endif 745 695 return pci_register_driver(&xhci_pci_driver);
+3
include/linux/usb/hcd.h
··· 269 269 /* called after entering D0 (etc), before resuming the hub */ 270 270 int (*pci_resume)(struct usb_hcd *hcd, bool hibernated); 271 271 272 + /* called just before hibernate final D3 state, allows host to poweroff parts */ 273 + int (*pci_poweroff_late)(struct usb_hcd *hcd, bool do_wakeup); 274 + 272 275 /* cleanly make HCD stop writing memory and doing I/O */ 273 276 void (*stop) (struct usb_hcd *hcd); 274 277