Skip to contentSkip to conversation

Rework "xhci: clear root port wake on bits if controller isn't wake-up capable"

PATCH Superseded Lu Baolu 2 participants 6 messages Nov 4, 2014 – Nov 5, 2014

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Lu Baolu

author
#0 lore raw

v2 cover Rework "xhci: clear root port wake on bits if controller isn't wake-up capable"

This serie of patch reworks commit ff8cbf250b448aac35589f6075082c3fcad8a8fe. This has been discussed at http://www.spinics.net/lists/linux-usb/msg114986.html
It also includes a patch to fix a comment in drivers/usb/host/xhci.h.

Changes in v2:
* Should not be a quirk.
* Should be applied to all xhci controllers.

Lu Baolu (3):
usb: xhci: Revert "xhci: clear root port wake on bits if controller
isn't wake-up capable"
usb: xhci: This reworks ff8cbf250b448aac35589f6075082c3fcad8a8fe
usb: xhci: fix comment for PORT_DEV_REMOVE

 drivers/usb/host/xhci-hub.c |  5 +----
 drivers/usb/host/xhci-pci.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/host/xhci.h     |  8 +++++++-
 3 files changed, 50 insertions(+), 5 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v2 1/3 usb: xhci: Revert "xhci: clear root port wake on bits if controller isn't wake-up capable"

This reverts commit ff8cbf250b448aac35589f6075082c3fcad8a8fe.

Commit ff8cbf250b448aac35589f6075082c3fcad8a8fe triggers the bug logged at

https://bugzilla.kernel.org/show_bug.cgi?id=85701

---
 drivers/usb/host/xhci-hub.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/usb/host/xhci-hub.c +1 -4
index 696160d..388cfd8 100644
@@ -22,7 +22,6 @@
 
 
 #include <linux/slab.h>
-#include <linux/device.h>
 #include <asm/unaligned.h>
 
 #include "xhci.h"
@@ -1149,9 +1148,7 @@ int xhci_bus_suspend(struct usb_hcd *hcd)
 		 * including the USB 3.0 roothub, but only if CONFIG_PM_RUNTIME
 		 * is enabled, so also enable remote wake here.
 		 */
-		if (hcd->self.root_hub->do_remote_wakeup
-				&& device_may_wakeup(hcd->self.controller)) {
-
+		if (hcd->self.root_hub->do_remote_wakeup) {
 			if (t1 & PORT_CONNECT) {
 				t2 |= PORT_WKOC_E | PORT_WKDISC_E;
 				t2 &= ~PORT_WKCONN_E;
Signature · 1 line
1.9.1
author
#2 lore raw

PATCH v2 2/3 usb: xhci: This reworks ff8cbf250b448aac35589f6075082c3fcad8a8fe

In reply to Lu Baolu

xhci: clear root port wake on bits if controller isn't wake-up capable

When system is being suspended, if host device is not wakeup capable, xhci_suspend() needs to clear all root port wake on bits. Otherwise, some platforms may generate spurious wakeup, even if PCI PME# is dis- abled.

---
 drivers/usb/host/xhci.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
drivers/usb/host/xhci.c +42 -0
index 2a5d45b..cd57aae 100644
@@ -35,6 +35,8 @@
 #define DRIVER_AUTHOR "Sarah Sharp"
 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
 
+#define	PORT_WAKE_BITS	(PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
+
 /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
 static int link_quirk;
 module_param(link_quirk, int, S_IRUGO | S_IWUSR);
@@ -851,6 +853,42 @@ static void xhci_clear_command_ring(struct xhci_hcd *xhci)
 	xhci_set_cmd_ring_deq(xhci);
 }
 
+static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
+{
+	int port_index;
+	__le32 __iomem **port_array;
+	unsigned long flags;
+	u32 t1, t2;
+
+	spin_lock_irqsave(&xhci->lock, flags);
+
+	/* disble usb3 ports Wake bits*/
+	port_index = xhci->num_usb3_ports;
+	port_array = xhci->usb3_ports;
+	while (port_index--) {
+		t1 = readl(port_array[port_index]);
+		t2 = xhci_port_state_to_neutral(t1);
+		t2 &= ~PORT_WAKE_BITS;
+		t1 = xhci_port_state_to_neutral(t1);
+		if (t1 != t2)
+			writel(t2, port_array[port_index]);
+	}
+
+	/* disble usb2 ports Wake bits*/
+	port_index = xhci->num_usb2_ports;
+	port_array = xhci->usb2_ports;
+	while (port_index--) {
+		t1 = readl(port_array[port_index]);
+		t2 = xhci_port_state_to_neutral(t1);
+		t2 &= ~PORT_WAKE_BITS;
+		t1 = xhci_port_state_to_neutral(t1);
+		if (t1 != t2)
+			writel(t2, port_array[port_index]);
+	}
+
+	spin_unlock_irqrestore(&xhci->lock, flags);
+}
+
 /*
  * Stop HC (not bus-specific)
  *
@@ -868,6 +906,10 @@ int xhci_suspend(struct xhci_hcd *xhci)
 			xhci->shared_hcd->state != HC_STATE_SUSPENDED)
 		return -EINVAL;
 
+	/* Clear root port wake on bits if not wakeup capable. */
+	if (!device_may_wakeup(hcd->self.controller))
+		xhci_disable_port_wake_on_bits(xhci);
+
 	/* Don't poll the roothubs on bus suspend. */
 	xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
 	clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
Signature · 1 line
1.9.1
#3 lore raw

Re: [PATCH v2 2/3] usb: xhci: This reworks ff8cbf250b448aac35589f6075082c3fcad8a8fe

Quoted 69 lines · Lu Baolu

xhci: clear root port wake on bits if controller isn't wake-up capable

When system is being suspended, if host device is not wakeup capable, xhci_suspend() needs to clear all root port wake on bits. Otherwise, some platforms may generate spurious wakeup, even if PCI PME# is dis- abled.

---
 drivers/usb/host/xhci.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
drivers/usb/host/xhci.c +38 -0
index 2a5d45b..cd57aae 100644
@@ -35,6 +35,8 @@
 #define DRIVER_AUTHOR "Sarah Sharp"
 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
 
+#define	PORT_WAKE_BITS	(PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
+
 /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
 static int link_quirk;
 module_param(link_quirk, int, S_IRUGO | S_IWUSR);
@@ -851,6 +853,42 @@ static void xhci_clear_command_ring(struct xhci_hcd *xhci)
 	xhci_set_cmd_ring_deq(xhci);
 }
 
+static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
+{
+	int port_index;
+	__le32 __iomem **port_array;
+	unsigned long flags;
+	u32 t1, t2;
+
+	spin_lock_irqsave(&xhci->lock, flags);
+
+	/* disble usb3 ports Wake bits*/
+	port_index = xhci->num_usb3_ports;
+	port_array = xhci->usb3_ports;
+	while (port_index--) {
+		t1 = readl(port_array[port_index]);
+		t2 = xhci_port_state_to_neutral(t1);
+		t2 &= ~PORT_WAKE_BITS;
+		t1 = xhci_port_state_to_neutral(t1);
+		if (t1 != t2)
+			writel(t2, port_array[port_index]);
+	}
+
+	/* disble usb2 ports Wake bits*/
+	port_index = xhci->num_usb2_ports;
+	port_array = xhci->usb2_ports;
+	while (port_index--) {
+		t1 = readl(port_array[port_index]);
+		t2 = xhci_port_state_to_neutral(t1);
+		t2 &= ~PORT_WAKE_BITS;
+		t1 = xhci_port_state_to_neutral(t1);
+		if (t1 != t2)
+			writel(t2, port_array[port_index]);
+	}
+
+	spin_unlock_irqrestore(&xhci->lock, flags);
+}
+
 /*
  * Stop HC (not bus-specific)
  *

drivers/usb/host/xhci.c int xhci_suspend(struct xhci_hcd *xhci) · line 915

Quoted 10 lines
drivers/usb/host/xhci.c +4 -0
@@ -868,6 +906,10 @@ int xhci_suspend(struct xhci_hcd *xhci)
 			xhci->shared_hcd->state != HC_STATE_SUSPENDED)
 		return -EINVAL;
 
+	/* Clear root port wake on bits if not wakeup capable. */
+	if (!device_may_wakeup(hcd->self.controller))
+		xhci_disable_port_wake_on_bits(xhci);
+
 	/* Don't poll the roothubs on bus suspend. */
 	xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
 	clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);

This is better but still wrong. Remember, this same code gets called for system suspend _and_ for runtime suspend. During runtime suspend, wakeup is always supposed to be turned on, even if device_may_wakeup() is false. That's because device_may_wakeup() refers only to system suspend. What you need to test is the do_wakeup flag, which should be passed into xhci_suspend() by xhci_pci_suspend() and xhci_plat_suspend().

Another problem is in the patch description and the comments. If device_may_wakeup() returns false, it doesn't mean the controller isn't wakeup-capable -- it means the controller isn't _allowed_ to wake up the system. Those are two different things.

Finally, the code in xhci_disable_port_wake_on_bits() looks a little peculiar -- I'm not sure about all those calls to xhci_port_state_to_neutral(). But I'm not an expert on that; Mathias will have to advise on it.

Alan Stern

author
#4 lore raw

Re: [PATCH v2 2/3] usb: xhci: This reworks ff8cbf250b448aac35589f6075082c3fcad8a8fe

Quoted 74 lines · Alan Stern
Quoted 72 lines · Lu Baolu

xhci: clear root port wake on bits if controller isn't wake-up capable

When system is being suspended, if host device is not wakeup capable, xhci_suspend() needs to clear all root port wake on bits. Otherwise, some platforms may generate spurious wakeup, even if PCI PME# is dis- abled.

---
  drivers/usb/host/xhci.c | 42 ++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 42 insertions(+)
drivers/usb/host/xhci.c +38 -0
index 2a5d45b..cd57aae 100644
@@ -35,6 +35,8 @@
  #define DRIVER_AUTHOR "Sarah Sharp"
  #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
 
+#define	PORT_WAKE_BITS	(PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
+
  /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
  static int link_quirk;
  module_param(link_quirk, int, S_IRUGO | S_IWUSR);
@@ -851,6 +853,42 @@ static void xhci_clear_command_ring(struct xhci_hcd *xhci)
  	xhci_set_cmd_ring_deq(xhci);
  }
 
+static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
+{
+	int port_index;
+	__le32 __iomem **port_array;
+	unsigned long flags;
+	u32 t1, t2;
+
+	spin_lock_irqsave(&xhci->lock, flags);
+
+	/* disble usb3 ports Wake bits*/
+	port_index = xhci->num_usb3_ports;
+	port_array = xhci->usb3_ports;
+	while (port_index--) {
+		t1 = readl(port_array[port_index]);
+		t2 = xhci_port_state_to_neutral(t1);
+		t2 &= ~PORT_WAKE_BITS;
+		t1 = xhci_port_state_to_neutral(t1);
+		if (t1 != t2)
+			writel(t2, port_array[port_index]);
+	}
+
+	/* disble usb2 ports Wake bits*/
+	port_index = xhci->num_usb2_ports;
+	port_array = xhci->usb2_ports;
+	while (port_index--) {
+		t1 = readl(port_array[port_index]);
+		t2 = xhci_port_state_to_neutral(t1);
+		t2 &= ~PORT_WAKE_BITS;
+		t1 = xhci_port_state_to_neutral(t1);
+		if (t1 != t2)
+			writel(t2, port_array[port_index]);
+	}
+
+	spin_unlock_irqrestore(&xhci->lock, flags);
+}
+
  /*
   * Stop HC (not bus-specific)
   *
@@ -868,6 +906,10 @@ int xhci_suspend(struct xhci_hcd *xhci)
  			xhci->shared_hcd->state != HC_STATE_SUSPENDED)
  		return -EINVAL;
 
Quoted 14 lines

drivers/usb/host/xhci.c int xhci_suspend(struct xhci_hcd *xhci) · line 915

Quoted 7 lines
drivers/usb/host/xhci.c +4 -0
+	/* Clear root port wake on bits if not wakeup capable. */
+	if (!device_may_wakeup(hcd->self.controller))
+		xhci_disable_port_wake_on_bits(xhci);
+
  	/* Don't poll the roothubs on bus suspend. */
  	xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
  	clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);

This is better but still wrong. Remember, this same code gets called for system suspend _and_ for runtime suspend. During runtime suspend, wakeup is always supposed to be turned on, even if device_may_wakeup() is false. That's because device_may_wakeup() refers only to system suspend. What you need to test is the do_wakeup flag, which should be passed into xhci_suspend() by xhci_pci_suspend() and xhci_plat_suspend().

Yes, it should cover runtime suspend as well. Thanks for the comments. I will resend the patch.

Quoted 4 lines

Another problem is in the patch description and the comments. If device_may_wakeup() returns false, it doesn't mean the controller isn't wakeup-capable -- it means the controller isn't _allowed_ to wake up the system. Those are two different things.

Accept, I will change this in new patch version.

Quoted 5 lines

Finally, the code in xhci_disable_port_wake_on_bits() looks a little peculiar -- I'm not sure about all those calls to xhci_port_state_to_neutral(). But I'm not an expert on that; Mathias will have to advise on it.

This part of code was written with reference to code in xhci-hub.c.

Comments of xhci_port_state_to_neutral():
/*
* Given a port state, this function returns a value that would result
in the
* port being in the same state, if the value was written to the port
status
* control register.
* Save Read Only (RO) bits and save read/write bits where
* writing a 0 clears the bit and writing a 1 sets the bit (RWS).
* For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no
effect.
*/

This calculation is used to avoid side effect of changing other bit fields.

Thanks,
-baolu

Quoted 3 lines

Alan Stern

author
#5 lore raw

PATCH v2 3/3 usb: xhci: fix comment for PORT_DEV_REMOVE

In reply to Lu Baolu

According to xHCI specification, PORT_DEV_REMOVE(bit 30) in PORTSC true means "Device is non-removable".

---
 drivers/usb/host/xhci.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/host/xhci.h +1 -1
index df76d64..66c168a9 100644
@@ -358,7 +358,7 @@ struct xhci_op_regs {
 /* wake on over-current (enable) */
 #define PORT_WKOC_E	(1 << 27)
 /* bits 28:29 reserved */
-/* true: device is removable - for USB 3.0 roothub emulation */
+/* true: device is non-removable - for USB 3.0 roothub emulation */
 #define PORT_DEV_REMOVE	(1 << 30)
 /* Initiate a warm port reset - complete when PORT_WRC is '1' */
 #define PORT_WR		(1 << 31)
Signature · 1 line
1.9.1