Introduce XHCI_FULL_RESET_ON_REMOVE quirk for DWC3
Subsystems (4)
v2 cover Introduce XHCI_FULL_RESET_ON_REMOVE quirk for DWC3
Commit 6ccb83d6c497 ("usb: xhci: Implement xhci_handshake_check_state() helper") introduced an optimization to xhci_reset() during xhci removal, allowing it to bail out early without waiting for the reset to complete.
This behavior can cause issues on SNPS DWC3 USB controller with dual-role capability. When the DWC3 controller exits host mode and removes xhci while a reset is still in progress, and then tries to configure its hardware for device mode, the ongoing reset leads to register access issues; specifically, all register reads returns 0. These issues extend beyond the xhci register space (which is expected during a reset) and affect the entire DWC3 IP block, causing the DWC3 device mode to malfunction.
To fix this, introduce XHCI_FULL_RESET_ON_REMOVE quirk andd enable it on DWC3. This ensures xhci_reset() completes its full handshake before proceeding.
---
Changes in v2:
- no code change
- add Fixes tag and cc stable kernel
Link to v1: https://lore.kernel.org/r/[email protected]/
---
Roy Luo (2):
xhci: Add a quirk for full reset on removal
usb: dwc3: Force full reset on xhci removal
drivers/usb/dwc3/host.c | 5 ++++- drivers/usb/host/xhci-plat.c | 3 +++ drivers/usb/host/xhci.c | 8 +++++++- drivers/usb/host/xhci.h | 1 + 4 files changed, 15 insertions(+), 2 deletions(-)
base-commit: c94d59a126cb9a8d1f71e3e044363d654dcd7af8
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
PATCH v2 1/2 xhci: Add a quirk for full reset on removal
Commit 6ccb83d6c497 ("usb: xhci: Implement xhci_handshake_check_state() helper") introduced an optimization to xhci_reset() during xhci removal, allowing it to bail out early without waiting for the reset to complete.
This behavior can cause issues on SNPS DWC3 USB controller with dual-role capability. When the DWC3 controller exits host mode and removes xhci while a reset is still in progress, and then tries to configure its hardware for device mode, the ongoing reset leads to register access issues; specifically, all register reads returns 0. These issues extend beyond the xhci register space (which is expected during a reset) and affect the entire DWC3 IP block, causing the DWC3 device mode to malfunction.
To address this, introduce the XHCI_FULL_RESET_ON_REMOVE quirk. When this quirk is set, xhci_reset() always completes its reset handshake, ensuring the controller is in a fully reset state before proceeding.
--- drivers/usb/host/xhci-plat.c | 3 +++ drivers/usb/host/xhci.c | 8 +++++++- drivers/usb/host/xhci.h | 1 + 3 files changed, 11 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-plat.c +3 -0
@@ -265,6 +265,9 @@ int xhci_plat_probe(struct platform_device *pdev, struct device *sysdev, const sif (device_property_read_bool (tmpdev,"xhci-skip-phy-init-quirk" )) xhci->quirks |=XHCI_SKIP_PHY_INIT ; +if (device_property_read_bool (tmpdev,"xhci-full-reset-on-remove-quirk" )) + xhci->quirks |=XHCI_FULL_RESET_ON_REMOVE ; +device_property_read_u32 (tmpdev,"imod-interval-ns" , &xhci->imod_interval); }
drivers/usb/host/xhci.c +7 -1
@@ -198,6 +198,7 @@ int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)u32 command;u32 state;int ret; +unsigned int exit_state; state =readl (&xhci->op_regs->status);
@@ -226,8 +227,13 @@ int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)if (xhci->quirks &XHCI_INTEL_HOST )udelay (1000 ); +if (xhci->quirks &XHCI_FULL_RESET_ON_REMOVE ) + exit_state =0 ; +else + exit_state =XHCI_STATE_REMOVING ; + ret =xhci_handshake_check_state (xhci, &xhci->op_regs->command, -CMD_RESET ,0 , timeout_us,XHCI_STATE_REMOVING ); +CMD_RESET ,0 , timeout_us, exit_state);if (ret)return ret;
drivers/usb/host/xhci.h +1 -0
@@ -1637,6 +1637,7 @@ struct xhci_hcd {#define XHCI_WRITE_64_HI_LO BIT_ULL(47)#define XHCI_CDNS_SCTX_QUIRK BIT_ULL(48)#define XHCI_ETRON_HOST BIT_ULL(49) +#define XHCI_FULL_RESET_ON_REMOVE BIT_ULL(50)unsigned int num_active_eps;unsigned int limit_active_eps;
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Thinh Nguyen
maintainerRe: [PATCH v2 1/2] xhci: Add a quirk for full reset on removal
Quoted 53 lines · Roy Luo
Commit
6ccb83d6c497("usb: xhci: Implementxhci_handshake_check_state()helper") introduced an optimization toxhci_reset()during xhci removal, allowing it to bail out early without waiting for the reset to complete.This behavior can cause issues on SNPS DWC3 USB controller with dual-role capability. When the DWC3 controller exits host mode and removes xhci while a reset is still in progress, and then tries to configure its hardware for device mode, the ongoing reset leads to register access issues; specifically, all register reads returns 0. These issues extend beyond the xhci register space (which is expected during a reset) and affect the entire DWC3 IP block, causing the DWC3 device mode to malfunction.
To address this, introduce the
XHCI_FULL_RESET_ON_REMOVEquirk. When this quirk is set,xhci_reset()always completes its reset handshake, ensuring the controller is in a fully reset state before proceeding.Fixes 6ccb83d6c497 ("usb: xhci: Implement xhci_handshake_check_state() helper")Signed-off-by Roy Luo <[email protected]>--- drivers/usb/host/xhci-plat.c | 3 +++ drivers/usb/host/xhci.c | 8 +++++++- drivers/usb/host/xhci.h | 1 + 3 files changed, 11 insertions(+), 1 deletion(-)drivers/usb/host/xhci-plat.c +3
-0@@ -265,6 +265,9 @@ int xhci_plat_probe(struct platform_device *pdev, struct device *sysdev, const sif (device_property_read_bool (tmpdev,"xhci-skip-phy-init-quirk" )) xhci->quirks |=XHCI_SKIP_PHY_INIT ; +if (device_property_read_bool (tmpdev,"xhci-full-reset-on-remove-quirk" )) + xhci->quirks |=XHCI_FULL_RESET_ON_REMOVE ; +device_property_read_u32 (tmpdev,"imod-interval-ns" , &xhci->imod_interval); }drivers/usb/host/xhci.c +1
-0@@ -198,6 +198,7 @@ int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)u32 command;u32 state;int ret; +unsigned int exit_state; state =readl (&xhci->op_regs->status);
drivers/usb/host/xhci.c int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us) · line 231
Quoted 5 lines
drivers/usb/host/xhci.c +2
-0@@ -226,8 +227,13 @@ int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)if (xhci->quirks &XHCI_INTEL_HOST )udelay (1000 ); +if (xhci->quirks &XHCI_FULL_RESET_ON_REMOVE ) + exit_state =0 ;
There's no state 0. Checking against that is odd. Couldn't we just use xhci_handshake() equivalent instead?
In any case, this is basically a revert of this change:6ccb83d6c497 ("usb: xhci: Implement xhci_handshake_check_state() helper")
Can't we just revert or fix the above patch that causes a regression?
Thanks,
Thinh
Quoted 24 lines
drivers/usb/host/xhci.c +4
-1+else + exit_state =XHCI_STATE_REMOVING ; + ret =xhci_handshake_check_state (xhci, &xhci->op_regs->command, -CMD_RESET ,0 , timeout_us,XHCI_STATE_REMOVING ); +CMD_RESET ,0 , timeout_us, exit_state);if (ret)return ret;drivers/usb/host/xhci.h +1
-0@@ -1637,6 +1637,7 @@ struct xhci_hcd {#define XHCI_WRITE_64_HI_LO BIT_ULL(47)#define XHCI_CDNS_SCTX_QUIRK BIT_ULL(48)#define XHCI_ETRON_HOST BIT_ULL(49) +#define XHCI_FULL_RESET_ON_REMOVE BIT_ULL(50)unsigned int num_active_eps;unsigned int limit_active_eps;Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog
Re: [PATCH v2 1/2] xhci: Add a quirk for full reset on removal
Quoted 5 lines · Thinh Nguyen
In any case, this is basically a revert of this change:
6ccb83d6c497("usb: xhci: Implementxhci_handshake_check_state()helper")Can't we just revert or fix the above patch that causes a regression?
Also note that 6ccb83d6c497 claimed to fix actual problems, so disabling it on selected hardware could bring the old bug back:
Quoted 6 lines
In some situations where xhci removal happens parallel to
xhci_handshake, we encounter a scenario where thexhci_handshakecan't succeed, and it polls until timeout.If
xhci_handshakeruns until timeout it can on some platforms result in a long wait which might lead to a watchdog timeout.
But on the other hand, xhci_handshake() has long timeouts because the handshakes themselves can take a surprisingly long time (and sometimes still succeed), so any reliance on handshake completing before timeout is frankly a bug in itself.
Regards,
Michal
Re: [PATCH v2 1/2] xhci: Add a quirk for full reset on removal
Quoted 2 lines
There's no state 0. Checking against that is odd. Couldn't we just use
xhci_handshake()equivalent instead?
Ok, I will change it in the next version.
Quoted 17 lines · Michał Pecio
Quoted 5 lines · Thinh Nguyen
In any case, this is basically a revert of this change:
6ccb83d6c497("usb: xhci: Implementxhci_handshake_check_state()helper")Can't we just revert or fix the above patch that causes a regression?
Also note that
6ccb83d6c497claimed to fix actual problems, so disabling it on selected hardware could bring the old bug back:Quoted 6 lines
In some situations where xhci removal happens parallel to
xhci_handshake, we encounter a scenario where thexhci_handshakecan't succeed, and it polls until timeout.If
xhci_handshakeruns until timeout it can on some platforms result in a long wait which might lead to a watchdog timeout.
On top of this, xhci_handshake_check_state(XHCI_STATE_REMOVING) is also used elsewhere like xhci_abort_cmd_ring(), so a simple revert is off the table. Commit 6ccb83d6c497 did not specify which platform and in what circumstance would xhci handshake timeout, adding a quirk for DWC3 seems to be the better option here.
Quoted 5 lines
But on the other hand,
xhci_handshake()has long timeouts because the handshakes themselves can take a surprisingly long time (and sometimes still succeed), so any reliance on handshake completing before timeout is frankly a bug in itself.
This patch simply honors the contract between the software and hardware, allowing the handshake to complete. It doesn't assume the handshake will finish on time. If it times out, then it times out and returns a failure.
Thanks,
Roy
Thinh Nguyen
maintainerRe: [PATCH v2 1/2] xhci: Add a quirk for full reset on removal
Hi Roy, Michał,
Quoted 21 lines · Roy Luo
Quoted 2 lines
There's no state 0. Checking against that is odd. Couldn't we just use
xhci_handshake()equivalent instead?Ok, I will change it in the next version.
Quoted 15 lines · Michał Pecio
Quoted 5 lines · Thinh Nguyen
In any case, this is basically a revert of this change:
6ccb83d6c497("usb: xhci: Implementxhci_handshake_check_state()helper")Can't we just revert or fix the above patch that causes a regression?
Also note that
6ccb83d6c497claimed to fix actual problems, so disabling it on selected hardware could bring the old bug back:Quoted 4 lines
In some situations where xhci removal happens parallel to
xhci_handshake, we encounter a scenario where thexhci_handshakecan't succeed, and it polls until timeout.
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
If
xhci_handshakeruns until timeout it can on some platforms result in a long wait which might lead to a watchdog timeout.On top of this,
xhci_handshake_check_state(XHCI_STATE_REMOVING)is also used elsewhere likexhci_abort_cmd_ring(), so a simple revert is off the table. Commit6ccb83d6c497did not specify which platform and in what circumstance would xhci handshake timeout, adding a quirk for DWC3 seems to be the better option here.
Regarding the commit 6ccb83d6c497, I'm assuming Udipto made the change for Qcom platforms. Hi @Udipto, if you're reading this, please confirm.
Many of the Qcom platforms are using dwc3 controller. The change you made here are affecting all the dwc3 DRD controllers, which has a good chance to also impact the Qcom platforms.
Quoted 11 lines
Quoted 5 lines
But on the other hand,
xhci_handshake()has long timeouts because the handshakes themselves can take a surprisingly long time (and sometimes still succeed), so any reliance on handshake completing before timeout is frankly a bug in itself.This patch simply honors the contract between the software and hardware, allowing the handshake to complete. It doesn't assume the handshake will finish on time. If it times out, then it times out and returns a failure.
As Michał pointed out, disregarding the xhci handshake timeout is not proper. The change 6ccb83d6c497 seems to workaround some different watchdog warning timeout instead of resolving the actual issue. The watchdog timeout should not be less than the handshake timeout here.
BR,
Thinh
Re: [PATCH v2 1/2] xhci: Add a quirk for full reset on removal
Quoted 54 lines · Thinh Nguyen
Hi Roy, Michał,
Quoted 21 lines · Roy Luo
Quoted 2 lines
There's no state 0. Checking against that is odd. Couldn't we just use
xhci_handshake()equivalent instead?Ok, I will change it in the next version.
Quoted 15 lines · Michał Pecio
Quoted 5 lines · Thinh Nguyen
In any case, this is basically a revert of this change:
6ccb83d6c497("usb: xhci: Implementxhci_handshake_check_state()helper")Can't we just revert or fix the above patch that causes a regression?
Also note that
6ccb83d6c497claimed to fix actual problems, so disabling it on selected hardware could bring the old bug back:Quoted 4 lines
In some situations where xhci removal happens parallel to
xhci_handshake, we encounter a scenario where thexhci_handshakecan't succeed, and it polls until timeout.Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
If
xhci_handshakeruns until timeout it can on some platforms result in a long wait which might lead to a watchdog timeout.On top of this,
xhci_handshake_check_state(XHCI_STATE_REMOVING)is also used elsewhere likexhci_abort_cmd_ring(), so a simple revert is off the table. Commit6ccb83d6c497did not specify which platform and in what circumstance would xhci handshake timeout, adding a quirk for DWC3 seems to be the better option here.Regarding the commit
6ccb83d6c497, I'm assuming Udipto made the change for Qcom platforms. Hi @Udipto, if you're reading this, please confirm.Many of the Qcom platforms are using dwc3 controller. The change you made here are affecting all the dwc3 DRD controllers, which has a good chance to also impact the Qcom platforms.
Quoted 11 lines
Quoted 5 lines
But on the other hand,
xhci_handshake()has long timeouts because the handshakes themselves can take a surprisingly long time (and sometimes still succeed), so any reliance on handshake completing before timeout is frankly a bug in itself.This patch simply honors the contract between the software and hardware, allowing the handshake to complete. It doesn't assume the handshake will finish on time. If it times out, then it times out and returns a failure.
Quoted 5 lines
As Michał pointed out, disregarding the xhci handshake timeout is not proper. The change
6ccb83d6c497seems to workaround some different watchdog warning timeout instead of resolving the actual issue. The watchdog timeout should not be less than the handshake timeout here.
This makes sense, I will send out a revert of 6ccb83d6c497 then.
Thanks,
Roy
Re: [PATCH v2 1/2] xhci: Add a quirk for full reset on removal
In reply to Thinh NguyenQuoted 11 lines · Thinh Nguyen
Quoted 10 lines
Quoted 4 lines
But on the other hand,
xhci_handshake()has long timeouts because the handshakes themselves can take a surprisingly long time (and sometimes still succeed), so any reliance on handshake completing before timeout is frankly a bug in itself.This patch simply honors the contract between the software and hardware, allowing the handshake to complete. It doesn't assume the handshake will finish on time. If it times out, then it times out and returns a failure.
Quoted 4 lines
As Michał pointed out, disregarding the xhci handshake timeout is not proper. The change
6ccb83d6c497seems to workaround some different watchdog warning timeout instead of resolving the actual issue. The watchdog timeout should not be less than the handshake timeout here.
There is certainly one real problem, which has likely existed since
forever: some of those handshakes cause system-wide freezes. I haven't investigated it thoroughly, but I suspect the main culprit is the one in xhci_abort_cmd_ring(), which holds the spinlock for a few seconds if the xHC is particularly slow to complete the abort. This probably causes xhci_irq() to spin and disrupt other IRQs.
I encounter it sometimes with ASMedia controllers, but I guess anyone can simulate it by inserting artificial delays near xhci_handshake().
Regards,
Michal
PATCH v2 2/2 usb: dwc3: Force full reset on xhci removal
In reply to Roy LuoDuring an xhci host controller reset (via USBCMD.HCRST), reading DWC3 registers can return zero instead of their actual values. This applies not only to registers within the xhci memory space but also those in the broader DWC3 IP block.
By default, the xhci driver doesn't wait for the reset handshake to complete during teardown. This can cause problems when the DWC3 controller is operating as a dual role device and is switching from host to device mode, the invalid register read caused by ongoing HCRST could lead to gadget mode startup failures and unintended register overwrites.
To mitigate this, enable xhci-full-reset-on-remove-quirk to ensure that xhci_reset() completes its full reset handshake during xhci removal.
--- drivers/usb/dwc3/host.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/usb/dwc3/host.c +4 -1
@@ -126,7 +126,7 @@ static int dwc3_host_get_irq(struct dwc3 *dwc)int dwc3_host_init (struct dwc3 *dwc) { -struct property_entry props[6 ]; +struct property_entry props[7 ];struct platform_device *xhci;int ret, irq;int prop_idx =0 ;
@@ -182,6 +182,9 @@ int dwc3_host_init(struct dwc3 *dwc) if (DWC3_VER_IS_WITHIN(DWC3, ANY, 300A)) props[prop_idx++] =PROPERTY_ENTRY_BOOL ("quirk-broken-port-ped" ); +if (dwc->dr_mode ==USB_DR_MODE_OTG ) + props[prop_idx++] =PROPERTY_ENTRY_BOOL ("xhci-full-reset-on-remove-quirk" ); +if (prop_idx) { ret =device_create_managed_software_node (&xhci->dev, props,NULL );if (ret) {
Signature · 1 line
2.49.0.1112.g889b7c5bd8-goog