diff mbox series

[v11,4/4] usb: host: enable USB offload during system sleep

Message ID 20250408035833.844821-5-guanyulin@google.com (mailing list archive)
State New
Headers show
Series Support system sleep with offloaded usb transfers | expand

Commit Message

Guan-Yu Lin April 8, 2025, 3:57 a.m. UTC
Sharing a USB controller with another entity via xhci-sideband driver
creates power management complexities. To prevent the USB controller
from being inadvertently deactivated while in use by the other entity, a
usage-count based mechanism is implemented. This allows the system to
manage power effectively, ensuring the controller remains available
whenever needed.
In order to maintain full functionality of an offloaded USB devices,
several changes are made within the suspend flow of such devices:
- skip usb_suspend_device() so that the port/hub are still active for
  USB transfers via offloaded path.
- not suspending the endpoints which are used by USB interfaces marked
  with needs_remote_wakeup. Namely, skip usb_suspend_interface() and
  usb_hcd_flush_endpoint() on associated USB interfaces. This reserves a
  pending interrupt urb during system suspend for handling the interrupt
  transfer, which is necessary since remote wakeup doesn't apply in the
  offloaded USB devices when controller is still active.
- not flushing the endpoints of actively offloaded USB devices. Given
  that the USB devices is used by another entity, unilaterally flush the
  endpoint might lead to unexpected behavior on another entity.
- not suspending the xhci controller. This is done by skipping the
  suspend/resume callbacks in the xhci platform driver.

Signed-off-by: Guan-Yu Lin <guanyulin@google.com>
---
 drivers/usb/core/driver.c    | 43 +++++++++++++++++++++++++++++++-----
 drivers/usb/host/xhci-plat.c | 19 ++++++++++++++++
 drivers/usb/host/xhci-plat.h |  1 +
 include/linux/usb.h          |  2 ++
 4 files changed, 59 insertions(+), 6 deletions(-)

Comments

Alan Stern April 9, 2025, 3:17 p.m. UTC | #1
On Tue, Apr 08, 2025 at 03:57:24AM +0000, Guan-Yu Lin wrote:
> Sharing a USB controller with another entity via xhci-sideband driver
> creates power management complexities. To prevent the USB controller
> from being inadvertently deactivated while in use by the other entity, a
> usage-count based mechanism is implemented. This allows the system to
> manage power effectively, ensuring the controller remains available
> whenever needed.
> In order to maintain full functionality of an offloaded USB devices,
> several changes are made within the suspend flow of such devices:
> - skip usb_suspend_device() so that the port/hub are still active for
>   USB transfers via offloaded path.
> - not suspending the endpoints which are used by USB interfaces marked
>   with needs_remote_wakeup. Namely, skip usb_suspend_interface() and
>   usb_hcd_flush_endpoint() on associated USB interfaces. This reserves a
>   pending interrupt urb during system suspend for handling the interrupt
>   transfer, which is necessary since remote wakeup doesn't apply in the
>   offloaded USB devices when controller is still active.
> - not flushing the endpoints of actively offloaded USB devices. Given
>   that the USB devices is used by another entity, unilaterally flush the
>   endpoint might lead to unexpected behavior on another entity.
> - not suspending the xhci controller. This is done by skipping the
>   suspend/resume callbacks in the xhci platform driver.
> 
> Signed-off-by: Guan-Yu Lin <guanyulin@google.com>
> ---
>  drivers/usb/core/driver.c    | 43 +++++++++++++++++++++++++++++++-----
>  drivers/usb/host/xhci-plat.c | 19 ++++++++++++++++
>  drivers/usb/host/xhci-plat.h |  1 +
>  include/linux/usb.h          |  2 ++
>  4 files changed, 59 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
> index 316526a05250..59b33e5e9a87 100644
> --- a/drivers/usb/core/driver.c
> +++ b/drivers/usb/core/driver.c
> @@ -1420,11 +1420,25 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
>  			udev->state == USB_STATE_SUSPENDED)
>  		goto done;
>  
> +	if (msg.event == PM_EVENT_SUSPEND && usb_offload_check(udev)) {
> +		dev_dbg(&udev->dev, "device offload active, skip suspend.\n");
> +		udev->offload_at_suspend = 1;
> +	}
> +
>  	/* Suspend all the interfaces and then udev itself */
>  	if (udev->actconfig) {
>  		n = udev->actconfig->desc.bNumInterfaces;
>  		for (i = n - 1; i >= 0; --i) {
>  			intf = udev->actconfig->interface[i];
> +			/*
> +			 * Don't suspend interfaces with remote wakeup while the controller is
> +			 * active. This preserves pending interrupt urbs, allowing interrupt
> +			 * events to be handled during system suspend.
> +			 */

Here and below, please try to limit the line lengths to below 80 
columns, even in comments.

Alan Stern
Greg Kroah-Hartman April 11, 2025, 2:02 p.m. UTC | #2
On Tue, Apr 08, 2025 at 03:57:24AM +0000, Guan-Yu Lin wrote:
> @@ -736,6 +737,7 @@ struct usb_device {
>  	unsigned use_generic_driver:1;
>  
>  	refcount_t offload_usage;
> +	unsigned offload_at_suspend:1;

Please take a second and think about how the memory is now layed out
here with your new addition.  The tool 'pahole' is your friend...

thanks,

greg k-h
Guan-Yu Lin April 16, 2025, 9:26 a.m. UTC | #3
On Wed, Apr 9, 2025 at 11:17 PM Alan Stern <stern@rowland.harvard.edu> wrote:
>
> On Tue, Apr 08, 2025 at 03:57:24AM +0000, Guan-Yu Lin wrote:
> > +                     /*
> > +                      * Don't suspend interfaces with remote wakeup while the controller is
> > +                      * active. This preserves pending interrupt urbs, allowing interrupt
> > +                      * events to be handled during system suspend.
> > +                      */
>
> Here and below, please try to limit the line lengths to below 80
> columns, even in comments.
>
> Alan Stern

Thanks for the reminder. I'll address it in the next patch.

Regards,
Guan-Yu
Guan-Yu Lin April 16, 2025, 10:05 a.m. UTC | #4
On Fri, Apr 11, 2025 at 10:02 PM Greg KH <gregkh@linuxfoundation.org> wrote:
>
> On Tue, Apr 08, 2025 at 03:57:24AM +0000, Guan-Yu Lin wrote:
> > @@ -736,6 +737,7 @@ struct usb_device {
> >       unsigned use_generic_driver:1;
> >
> >       refcount_t offload_usage;
> > +     unsigned offload_at_suspend:1;
>
> Please take a second and think about how the memory is now layed out
> here with your new addition.  The tool 'pahole' is your friend...
>
> thanks,
>
> greg k-h

Thanks for providing this helpful information. I'll reconstruct the
struct to minimize the effect of new additions.

Regards,
Guan-Yu
diff mbox series

Patch

diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 316526a05250..59b33e5e9a87 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -1420,11 +1420,25 @@  static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
 			udev->state == USB_STATE_SUSPENDED)
 		goto done;
 
+	if (msg.event == PM_EVENT_SUSPEND && usb_offload_check(udev)) {
+		dev_dbg(&udev->dev, "device offload active, skip suspend.\n");
+		udev->offload_at_suspend = 1;
+	}
+
 	/* Suspend all the interfaces and then udev itself */
 	if (udev->actconfig) {
 		n = udev->actconfig->desc.bNumInterfaces;
 		for (i = n - 1; i >= 0; --i) {
 			intf = udev->actconfig->interface[i];
+			/*
+			 * Don't suspend interfaces with remote wakeup while the controller is
+			 * active. This preserves pending interrupt urbs, allowing interrupt
+			 * events to be handled during system suspend.
+			 */
+			if (udev->offload_at_suspend && intf->needs_remote_wakeup) {
+				dev_dbg(&intf->dev, "active interface on offloaded devices\n");
+				continue;
+			}
 			status = usb_suspend_interface(udev, intf, msg);
 
 			/* Ignore errors during system sleep transitions */
@@ -1435,7 +1449,8 @@  static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
 		}
 	}
 	if (status == 0) {
-		status = usb_suspend_device(udev, msg);
+		if (!udev->offload_at_suspend)
+			status = usb_suspend_device(udev, msg);
 
 		/*
 		 * Ignore errors from non-root-hub devices during
@@ -1480,9 +1495,11 @@  static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
 	 */
 	} else {
 		udev->can_submit = 0;
-		for (i = 0; i < 16; ++i) {
-			usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
-			usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
+		if (!udev->offload_at_suspend) {
+			for (i = 0; i < 16; ++i) {
+				usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
+				usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
+			}
 		}
 	}
 
@@ -1524,17 +1541,31 @@  static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
 	udev->can_submit = 1;
 
 	/* Resume the device */
-	if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
-		status = usb_resume_device(udev, msg);
+	if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume) {
+		if (!udev->offload_at_suspend)
+			status = usb_resume_device(udev, msg);
+		else
+			dev_dbg(&udev->dev, "device offload active, skip resume.\n");
+	}
 
 	/* Resume the interfaces */
 	if (status == 0 && udev->actconfig) {
 		for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
 			intf = udev->actconfig->interface[i];
+			/*
+			 * Interfaces with remote wakeup aren't suspended while the controller is
+			 * active. This preserves pending interrupt urbs, allowing interrupt
+			 * events to be handled during system suspend.
+			 */
+			if (udev->offload_at_suspend && intf->needs_remote_wakeup) {
+				dev_dbg(&intf->dev, "active interface on offloaded devices\n");
+				continue;
+			}
 			usb_resume_interface(udev, intf, msg,
 					udev->reset_resume);
 		}
 	}
+	udev->offload_at_suspend = 0;
 	usb_mark_last_busy(udev);
 
  done:
diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
index 9843d3ad5cf4..598f1b974347 100644
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -20,6 +20,7 @@ 
 #include <linux/acpi.h>
 #include <linux/usb/of.h>
 #include <linux/reset.h>
+#include <linux/usb/xhci-sideband.h>
 
 #include "xhci.h"
 #include "xhci-plat.h"
@@ -483,6 +484,15 @@  static int xhci_plat_suspend_common(struct device *dev)
 
 static int xhci_plat_suspend(struct device *dev)
 {
+	struct usb_hcd	*hcd = dev_get_drvdata(dev);
+	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
+
+	if (xhci_sideband_check(hcd)) {
+		priv->sideband_at_suspend = 1;
+		dev_dbg(dev, "sideband instance active, skip suspend.\n");
+		return 0;
+	}
+
 	return xhci_plat_suspend_common(dev);
 }
 
@@ -535,6 +545,15 @@  static int xhci_plat_resume_common(struct device *dev, bool power_lost)
 
 static int xhci_plat_resume(struct device *dev)
 {
+	struct usb_hcd	*hcd = dev_get_drvdata(dev);
+	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
+
+	if (priv->sideband_at_suspend) {
+		priv->sideband_at_suspend = 0;
+		dev_dbg(dev, "sideband instance active, skip resume.\n");
+		return 0;
+	}
+
 	return xhci_plat_resume_common(dev, false);
 }
 
diff --git a/drivers/usb/host/xhci-plat.h b/drivers/usb/host/xhci-plat.h
index fe4f95e690fa..cd07b22adc60 100644
--- a/drivers/usb/host/xhci-plat.h
+++ b/drivers/usb/host/xhci-plat.h
@@ -15,6 +15,7 @@  struct usb_hcd;
 struct xhci_plat_priv {
 	const char *firmware_name;
 	unsigned long long quirks;
+	unsigned sideband_at_suspend:1;
 	bool power_lost;
 	void (*plat_start)(struct usb_hcd *);
 	int (*init_quirk)(struct usb_hcd *);
diff --git a/include/linux/usb.h b/include/linux/usb.h
index f3a4064c729c..bc3a84870907 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -648,6 +648,7 @@  struct usb3_lpm_parameters {
  *	Will be used as wValue for SetIsochDelay requests.
  * @use_generic_driver: ask driver core to reprobe using the generic driver.
  * @offload_usage: number of offload activities happening on this usb device.
+ * @offload_at_suspend: offload activities during suspend is enabled.
  *
  * Notes:
  * Usbcore drivers should not set usbdev->state directly.  Instead use
@@ -736,6 +737,7 @@  struct usb_device {
 	unsigned use_generic_driver:1;
 
 	refcount_t offload_usage;
+	unsigned offload_at_suspend:1;
 };
 
 #define to_usb_device(__dev)	container_of_const(__dev, struct usb_device, dev)