Skip to contentSkip to conversation

usb: dwc3: gadget: Prevent EP resource conflicts during StartTransfer

PATCH In review Selvarasu Ganesan 2 participants 22 messages Feb 27 – Oct 14 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20260227121338epcas5p4baebb406db37f07223545b2f85751bf2@epcas5p4.samsung.com

#1 lore raw

PATCH v3 1/1 usb: dwc3: gadget: Prevent EP resource conflicts during StartTransfer

The below “No resource for ep” warning appears when a StartTransfer command is issued for bulk or interrupt endpoints in dwc3_gadget_ep_enable while a previous StartTransfer on the same endpoint is still in progress. The gadget functions drivers can invoke usb_ep_enable (which triggers a new StartTransfer command) before the earlier transfer has completed. Because the previous StartTransfer is still active, dwc3_gadget_ep_disable can skip the required EndTransfer due to DWC3_EP_DELAY_STOP, leading to the endpoint resources are busy for previous StartTransfer and warning ("No resource for ep") from dwc3 driver.

Additionally, a race condition exists between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue() when manipulating dep->flags. When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() runs in that window, it may set the DWC3_EP_TRANSFER_STARTED flag as part of dwc3_send_gadget_ep_cmd(). When ep_disable resumes, it unconditionally clears all flags except those explicitly masked, potentially clearing DWC3_EP_TRANSFER_STARTED even though a new transfer has started. This leads to "No resource for ep" warnings on subsequent StartTransfer attempts.

The underlying framework issue is that usb_ep_disable() is expected to complete pending requests before returning, but is allowed to be called from interrupt context where sleeping to wait for completion is not possible.

As temporary workarounds for this framework limitation:

1. In __dwc3_gadget_ep_enable(), add a check for the DWC3_EP_TRANSFER_STARTED flag before issuing a new StartTransfer. This prevents a second StartTransfer on an already busy endpoint, eliminating the resource conflict.

2. In __dwc3_gadget_ep_disable(), preserve the DWC3_EP_TRANSFER_STARTED flag when masking dep->flags if it is actually set, preventing the race with dwc3_gadget_ep_queue() from corrupting the flag state.

These changes eliminate the "No resource for ep" warnings and potential kernel panics caused by panic_on_warn.

dwc3 13200000.dwc3: No resource for ep1out

WARNING: CPU: 0 PID: 700 at drivers/usb/dwc3/gadget.c:398 dwc3_send_gadget_ep_cmd+0x2f8/0x76c
Call trace:
dwc3_send_gadget_ep_cmd+0x2f8/0x76c
__dwc3_gadget_ep_enable+0x490/0x7c0
dwc3_gadget_ep_enable+0x6c/0xe4
usb_ep_enable+0x5c/0x15c
mp_eth_stop+0xd4/0x11c
__dev_close_many+0x160/0x1c8
__dev_change_flags+0xfc/0x220
dev_change_flags+0x24/0x70
devinet_ioctl+0x434/0x524
inet_ioctl+0xa8/0x224
sock_do_ioctl+0x74/0x128
sock_ioctl+0x3bc/0x468
__arm64_sys_ioctl+0xa8/0xe4
invoke_syscall+0x58/0x10c
el0_svc_common+0xa8/0xdc
do_el0_svc+0x1c/0x28
el0_svc+0x38/0x88
el0t_64_sync_handler+0x70/0xbc
el0t_64_sync+0x1a8/0x1ac

---

Note: No Fixes tag is added because this is a workaround for the gadget framework issue where the gadget framework calls usb_ep_disable() in interrupt context without ensuring endpoint flushing completes. A proper fix requires refactoring the framework to make sure usb_ep_disable is invoked in process context.

Changes in v3:
- Revised the commit message to detail the real gadget framework issue pointed out by the reviewer.
- Merged the two fixes for the same ep wringing into one patch.
Link to v2: https://lore.kernel.org/linux-usb/[email protected]/

Changes in v2:
- Removed change-id.
- Updated commit message.
Link to v1: https://lore.kernel.org/linux-usb/[email protected]/

---
 drivers/usb/dwc3/gadget.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +20 -2
index 0a688904ce8c5..3af1bbfe3d92b 100644
@@ -971,8 +971,9 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
 	 * Issue StartTransfer here with no-op TRB so we can always rely on No
 	 * Response Update Transfer command.
 	 */
-	if (usb_endpoint_xfer_bulk(desc) ||
-			usb_endpoint_xfer_int(desc)) {
+	if ((usb_endpoint_xfer_bulk(desc) ||
+			usb_endpoint_xfer_int(desc)) &&
+			!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
 		struct dwc3_gadget_ep_cmd_params params;
 		struct dwc3_trb	*trb;
 		dma_addr_t trb_dma;
@@ -1096,6 +1097,23 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
 	 */
 	if (dep->flags & DWC3_EP_DELAY_STOP)
 		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+	/*
+	 * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(),
+	 * the dwc->lock is temporarily released. If dwc3_gadget_ep_queue()
+	 * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as
+	 * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+	 * unconditionally in the mask operation, which could overwrite the
+	 * concurrent modification.
+	 *
+	 * As a workaround for the interrupt context constraint where we cannot
+	 * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED
+	 * flag if it is set, avoiding resource conflicts until the framework
+	 * is fixed to properly synchronize endpoint lifecycle management.
+	 */
+	if (dep->flags & DWC3_EP_TRANSFER_STARTED)
+		mask |= DWC3_EP_TRANSFER_STARTED;
+
 	dep->flags &= mask;
 
 	/* Clear out the ep descriptors for non-ep0 */
Signature · 1 line
2.34.1
maintainer Acked-by on v3 1/1
#2 lore raw
Quoted 131 lines · Selvarasu Ganesan

The below “No resource for ep” warning appears when a StartTransfer command is issued for bulk or interrupt endpoints in dwc3_gadget_ep_enable while a previous StartTransfer on the same endpoint is still in progress. The gadget functions drivers can invoke usb_ep_enable (which triggers a new StartTransfer command) before the earlier transfer has completed. Because the previous StartTransfer is still active, dwc3_gadget_ep_disable can skip the required EndTransfer due to DWC3_EP_DELAY_STOP, leading to the endpoint resources are busy for previous StartTransfer and warning ("No resource for ep") from dwc3 driver.

Additionally, a race condition exists between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue() when manipulating dep->flags. When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() runs in that window, it may set the DWC3_EP_TRANSFER_STARTED flag as part of dwc3_send_gadget_ep_cmd(). When ep_disable resumes, it unconditionally clears all flags except those explicitly masked, potentially clearing DWC3_EP_TRANSFER_STARTED even though a new transfer has started. This leads to "No resource for ep" warnings on subsequent StartTransfer attempts.

The underlying framework issue is that usb_ep_disable() is expected to complete pending requests before returning, but is allowed to be called from interrupt context where sleeping to wait for completion is not possible.

As temporary workarounds for this framework limitation:

1. In __dwc3_gadget_ep_enable(), add a check for the DWC3_EP_TRANSFER_STARTED flag before issuing a new StartTransfer. This prevents a second StartTransfer on an already busy endpoint, eliminating the resource conflict.

2. In __dwc3_gadget_ep_disable(), preserve the DWC3_EP_TRANSFER_STARTED flag when masking dep->flags if it is actually set, preventing the race with dwc3_gadget_ep_queue() from corrupting the flag state.

These changes eliminate the "No resource for ep" warnings and potential kernel panics caused by panic_on_warn.

dwc3 13200000.dwc3: No resource for ep1out

WARNING: CPU: 0 PID: 700 at drivers/usb/dwc3/gadget.c:398 dwc3_send_gadget_ep_cmd+0x2f8/0x76c
Call trace:
dwc3_send_gadget_ep_cmd+0x2f8/0x76c
__dwc3_gadget_ep_enable+0x490/0x7c0
dwc3_gadget_ep_enable+0x6c/0xe4
usb_ep_enable+0x5c/0x15c
mp_eth_stop+0xd4/0x11c
__dev_close_many+0x160/0x1c8
__dev_change_flags+0xfc/0x220
dev_change_flags+0x24/0x70
devinet_ioctl+0x434/0x524
inet_ioctl+0xa8/0x224
sock_do_ioctl+0x74/0x128
sock_ioctl+0x3bc/0x468
__arm64_sys_ioctl+0xa8/0xe4
invoke_syscall+0x58/0x10c
el0_svc_common+0xa8/0xdc
do_el0_svc+0x1c/0x28
el0_svc+0x38/0x88
el0t_64_sync_handler+0x70/0xbc
el0t_64_sync+0x1a8/0x1ac

---

Note: No Fixes tag is added because this is a workaround for the gadget framework issue where the gadget framework calls usb_ep_disable() in interrupt context without ensuring endpoint flushing completes. A proper fix requires refactoring the framework to make sure usb_ep_disable is invoked in process context.

Changes in v3:
- Revised the commit message to detail the real gadget framework issue
pointed out by the reviewer.
- Merged the two fixes for the same ep wringing into one patch.
Link to v2: https://urldefense.com/v3/__https://lore.kernel.org/linux-usb/[email protected]/__;!!A4F2R9G_pg!cQzQQ5kAWF6CE5hQe7VqFdnaxqwzsTB1ZGNT1GvCH28GoB_nESZR5Y2jtxdZBls6wBIM4OtpvG4dSaylvNC3qbh547k$

Changes in v2:
- Removed change-id.
- Updated commit message.
Link to v1: https://urldefense.com/v3/__https://lore.kernel.org/linux-usb/[email protected]/__;!!A4F2R9G_pg!cQzQQ5kAWF6CE5hQe7VqFdnaxqwzsTB1ZGNT1GvCH28GoB_nESZR5Y2jtxdZBls6wBIM4OtpvG4dSaylvNC38z-CRD4$

---
 drivers/usb/dwc3/gadget.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +20 -2
index 0a688904ce8c5..3af1bbfe3d92b 100644
@@ -971,8 +971,9 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
 	 * Issue StartTransfer here with no-op TRB so we can always rely on No
 	 * Response Update Transfer command.
 	 */
-	if (usb_endpoint_xfer_bulk(desc) ||
-			usb_endpoint_xfer_int(desc)) {
+	if ((usb_endpoint_xfer_bulk(desc) ||
+			usb_endpoint_xfer_int(desc)) &&
+			!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
 		struct dwc3_gadget_ep_cmd_params params;
 		struct dwc3_trb	*trb;
 		dma_addr_t trb_dma;
@@ -1096,6 +1097,23 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
 	 */
 	if (dep->flags & DWC3_EP_DELAY_STOP)
 		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+	/*
+	 * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(),
+	 * the dwc->lock is temporarily released. If dwc3_gadget_ep_queue()
+	 * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as
+	 * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+	 * unconditionally in the mask operation, which could overwrite the
+	 * concurrent modification.
+	 *
+	 * As a workaround for the interrupt context constraint where we cannot
+	 * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED
+	 * flag if it is set, avoiding resource conflicts until the framework
+	 * is fixed to properly synchronize endpoint lifecycle management.
+	 */
+	if (dep->flags & DWC3_EP_TRANSFER_STARTED)
+		mask |= DWC3_EP_TRANSFER_STARTED;
+
 	dep->flags &= mask;
 
 	/* Clear out the ep descriptors for non-ep0 */
Signature · 1 line
2.34.1

Thanks!
Thinh

maintainer
#3 lore raw
Quoted 128 lines · Thinh Nguyen
Quoted 127 lines · Selvarasu Ganesan

The below “No resource for ep” warning appears when a StartTransfer command is issued for bulk or interrupt endpoints in dwc3_gadget_ep_enable while a previous StartTransfer on the same endpoint is still in progress. The gadget functions drivers can invoke usb_ep_enable (which triggers a new StartTransfer command) before the earlier transfer has completed. Because the previous StartTransfer is still active, dwc3_gadget_ep_disable can skip the required EndTransfer due to DWC3_EP_DELAY_STOP, leading to the endpoint resources are busy for previous StartTransfer and warning ("No resource for ep") from dwc3 driver.

Additionally, a race condition exists between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue() when manipulating dep->flags. When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() runs in that window, it may set the DWC3_EP_TRANSFER_STARTED flag as part of dwc3_send_gadget_ep_cmd(). When ep_disable resumes, it unconditionally clears all flags except those explicitly masked, potentially clearing DWC3_EP_TRANSFER_STARTED even though a new transfer has started. This leads to "No resource for ep" warnings on subsequent StartTransfer attempts.

The underlying framework issue is that usb_ep_disable() is expected to complete pending requests before returning, but is allowed to be called from interrupt context where sleeping to wait for completion is not possible.

As temporary workarounds for this framework limitation:

1. In __dwc3_gadget_ep_enable(), add a check for the DWC3_EP_TRANSFER_STARTED flag before issuing a new StartTransfer. This prevents a second StartTransfer on an already busy endpoint, eliminating the resource conflict.

2. In __dwc3_gadget_ep_disable(), preserve the DWC3_EP_TRANSFER_STARTED flag when masking dep->flags if it is actually set, preventing the race with dwc3_gadget_ep_queue() from corrupting the flag state.

These changes eliminate the "No resource for ep" warnings and potential kernel panics caused by panic_on_warn.

dwc3 13200000.dwc3: No resource for ep1out

WARNING: CPU: 0 PID: 700 at drivers/usb/dwc3/gadget.c:398 dwc3_send_gadget_ep_cmd+0x2f8/0x76c
Call trace:
dwc3_send_gadget_ep_cmd+0x2f8/0x76c
__dwc3_gadget_ep_enable+0x490/0x7c0
dwc3_gadget_ep_enable+0x6c/0xe4
usb_ep_enable+0x5c/0x15c
mp_eth_stop+0xd4/0x11c
__dev_close_many+0x160/0x1c8
__dev_change_flags+0xfc/0x220
dev_change_flags+0x24/0x70
devinet_ioctl+0x434/0x524
inet_ioctl+0xa8/0x224
sock_do_ioctl+0x74/0x128
sock_ioctl+0x3bc/0x468
__arm64_sys_ioctl+0xa8/0xe4
invoke_syscall+0x58/0x10c
el0_svc_common+0xa8/0xdc
do_el0_svc+0x1c/0x28
el0_svc+0x38/0x88
el0t_64_sync_handler+0x70/0xbc
el0t_64_sync+0x1a8/0x1ac

---

Note: No Fixes tag is added because this is a workaround for the gadget framework issue where the gadget framework calls usb_ep_disable() in interrupt context without ensuring endpoint flushing completes. A proper fix requires refactoring the framework to make sure usb_ep_disable is invoked in process context.

Changes in v3:
- Revised the commit message to detail the real gadget framework issue
pointed out by the reviewer.
- Merged the two fixes for the same ep wringing into one patch.
Link to v2: https://urldefense.com/v3/__https://lore.kernel.org/linux-usb/[email protected]/__;!!A4F2R9G_pg!cQzQQ5kAWF6CE5hQe7VqFdnaxqwzsTB1ZGNT1GvCH28GoB_nESZR5Y2jtxdZBls6wBIM4OtpvG4dSaylvNC3qbh547k$

Changes in v2:
- Removed change-id.
- Updated commit message.
Link to v1: https://urldefense.com/v3/__https://lore.kernel.org/linux-usb/[email protected]/__;!!A4F2R9G_pg!cQzQQ5kAWF6CE5hQe7VqFdnaxqwzsTB1ZGNT1GvCH28GoB_nESZR5Y2jtxdZBls6wBIM4OtpvG4dSaylvNC38z-CRD4$

---
 drivers/usb/dwc3/gadget.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +20 -2
index 0a688904ce8c5..3af1bbfe3d92b 100644
@@ -971,8 +971,9 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
 	 * Issue StartTransfer here with no-op TRB so we can always rely on No
 	 * Response Update Transfer command.
 	 */
-	if (usb_endpoint_xfer_bulk(desc) ||
-			usb_endpoint_xfer_int(desc)) {
+	if ((usb_endpoint_xfer_bulk(desc) ||
+			usb_endpoint_xfer_int(desc)) &&
+			!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
 		struct dwc3_gadget_ep_cmd_params params;
 		struct dwc3_trb	*trb;
 		dma_addr_t trb_dma;
@@ -1096,6 +1097,23 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
 	 */
 	if (dep->flags & DWC3_EP_DELAY_STOP)
 		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+	/*
+	 * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(),
+	 * the dwc->lock is temporarily released. If dwc3_gadget_ep_queue()
+	 * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as
+	 * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+	 * unconditionally in the mask operation, which could overwrite the
+	 * concurrent modification.
+	 *
+	 * As a workaround for the interrupt context constraint where we cannot
+	 * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED
+	 * flag if it is set, avoiding resource conflicts until the framework
+	 * is fixed to properly synchronize endpoint lifecycle management.
+	 */
+	if (dep->flags & DWC3_EP_TRANSFER_STARTED)
+		mask |= DWC3_EP_TRANSFER_STARTED;
+
 	dep->flags &= mask;
 
Quoted 7 lines
Quoted 4 lines
drivers/usb/dwc3/gadget.c +0 -0
 	/* Clear out the ep descriptors for non-ep0 */
Signature · 1 line
2.34.1

Oh wait, don't pick this patch up yet.

This will cause a regression for UAS device. When switching alt-setting interface for BOT to UASP, the device needs to issue a Start Transfer command.

This workaround won't work. Can we fix the usb_ep_disable() interface and rework this instead?

BR,
Thinh

#4 lore raw
Quoted 141 lines · Thinh Nguyen
Quoted 128 lines · Thinh Nguyen
Quoted 127 lines · Selvarasu Ganesan

The below “No resource for ep” warning appears when a StartTransfer command is issued for bulk or interrupt endpoints in dwc3_gadget_ep_enable while a previous StartTransfer on the same endpoint is still in progress. The gadget functions drivers can invoke usb_ep_enable (which triggers a new StartTransfer command) before the earlier transfer has completed. Because the previous StartTransfer is still active, dwc3_gadget_ep_disable can skip the required EndTransfer due to DWC3_EP_DELAY_STOP, leading to the endpoint resources are busy for previous StartTransfer and warning ("No resource for ep") from dwc3 driver.

Additionally, a race condition exists between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue() when manipulating dep->flags. When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() runs in that window, it may set the DWC3_EP_TRANSFER_STARTED flag as part of dwc3_send_gadget_ep_cmd(). When ep_disable resumes, it unconditionally clears all flags except those explicitly masked, potentially clearing DWC3_EP_TRANSFER_STARTED even though a new transfer has started. This leads to "No resource for ep" warnings on subsequent StartTransfer attempts.

The underlying framework issue is that usb_ep_disable() is expected to complete pending requests before returning, but is allowed to be called from interrupt context where sleeping to wait for completion is not possible.

As temporary workarounds for this framework limitation:

1. In __dwc3_gadget_ep_enable(), add a check for the DWC3_EP_TRANSFER_STARTED flag before issuing a new StartTransfer. This prevents a second StartTransfer on an already busy endpoint, eliminating the resource conflict.

2. In __dwc3_gadget_ep_disable(), preserve the DWC3_EP_TRANSFER_STARTED flag when masking dep->flags if it is actually set, preventing the race with dwc3_gadget_ep_queue() from corrupting the flag state.

These changes eliminate the "No resource for ep" warnings and potential kernel panics caused by panic_on_warn.

dwc3 13200000.dwc3: No resource for ep1out

WARNING: CPU: 0 PID: 700 at drivers/usb/dwc3/gadget.c:398 dwc3_send_gadget_ep_cmd+0x2f8/0x76c
Call trace:
dwc3_send_gadget_ep_cmd+0x2f8/0x76c
__dwc3_gadget_ep_enable+0x490/0x7c0
dwc3_gadget_ep_enable+0x6c/0xe4
usb_ep_enable+0x5c/0x15c
mp_eth_stop+0xd4/0x11c
__dev_close_many+0x160/0x1c8
__dev_change_flags+0xfc/0x220
dev_change_flags+0x24/0x70
devinet_ioctl+0x434/0x524
inet_ioctl+0xa8/0x224
sock_do_ioctl+0x74/0x128
sock_ioctl+0x3bc/0x468
__arm64_sys_ioctl+0xa8/0xe4
invoke_syscall+0x58/0x10c
el0_svc_common+0xa8/0xdc
do_el0_svc+0x1c/0x28
el0_svc+0x38/0x88
el0t_64_sync_handler+0x70/0xbc
el0t_64_sync+0x1a8/0x1ac

---

Note: No Fixes tag is added because this is a workaround for the gadget framework issue where the gadget framework calls usb_ep_disable() in interrupt context without ensuring endpoint flushing completes. A proper fix requires refactoring the framework to make sure usb_ep_disable is invoked in process context.

Changes in v3:
- Revised the commit message to detail the real gadget framework issue
pointed out by the reviewer.
- Merged the two fixes for the same ep wringing into one patch.
Link to v2: https://urldefense.com/v3/__https://lore.kernel.org/linux-usb/[email protected]/__;!!A4F2R9G_pg!cQzQQ5kAWF6CE5hQe7VqFdnaxqwzsTB1ZGNT1GvCH28GoB_nESZR5Y2jtxdZBls6wBIM4OtpvG4dSaylvNC3qbh547k$

Changes in v2:
- Removed change-id.
- Updated commit message.
Link to v1: https://urldefense.com/v3/__https://lore.kernel.org/linux-usb/[email protected]/__;!!A4F2R9G_pg!cQzQQ5kAWF6CE5hQe7VqFdnaxqwzsTB1ZGNT1GvCH28GoB_nESZR5Y2jtxdZBls6wBIM4OtpvG4dSaylvNC38z-CRD4$

---
  drivers/usb/dwc3/gadget.c | 22 ++++++++++++++++++++--
  1 file changed, 20 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +20 -2
index 0a688904ce8c5..3af1bbfe3d92b 100644
@@ -971,8 +971,9 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
  	 * Issue StartTransfer here with no-op TRB so we can always rely on No
  	 * Response Update Transfer command.
  	 */
-	if (usb_endpoint_xfer_bulk(desc) ||
-			usb_endpoint_xfer_int(desc)) {
+	if ((usb_endpoint_xfer_bulk(desc) ||
+			usb_endpoint_xfer_int(desc)) &&
+			!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
  		struct dwc3_gadget_ep_cmd_params params;
  		struct dwc3_trb	*trb;
  		dma_addr_t trb_dma;
@@ -1096,6 +1097,23 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  	 */
  	if (dep->flags & DWC3_EP_DELAY_STOP)
  		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+	/*
+	 * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(),
+	 * the dwc->lock is temporarily released. If dwc3_gadget_ep_queue()
+	 * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as
+	 * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+	 * unconditionally in the mask operation, which could overwrite the
+	 * concurrent modification.
+	 *
+	 * As a workaround for the interrupt context constraint where we cannot
+	 * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED
+	 * flag if it is set, avoiding resource conflicts until the framework
+	 * is fixed to properly synchronize endpoint lifecycle management.
+	 */
+	if (dep->flags & DWC3_EP_TRANSFER_STARTED)
+		mask |= DWC3_EP_TRANSFER_STARTED;
+
  	dep->flags &= mask;
 
Quoted 6 lines
Quoted 4 lines
drivers/usb/dwc3/gadget.c +0 -0
  	/* Clear out the ep descriptors for non-ep0 */
Signature · 1 line
2.34.1

Oh wait, don't pick this patch up yet.

This will cause a regression for UAS device. When switching alt-setting interface for BOT to UASP, the device needs to issue a Start Transfer command.

Quoted 5 lines

This workaround won't work. Can we fix the usb_ep_disable() interface and rework this instead?

BR,
Thinh

Hi Thinh,

We’re trying to see how this change could cause a regression for UAS devices.
Could you explain why the workaround might be a problem for UAS? Are you concerned that it could miss a valid StartTransfer when a previous transfer finishes later than expected as part of ep_disable?

If we don’t use this temporary fix, the driver can still report “EP resource busy” when an earlier StartTransfer hasn’t finished before ep_disable returns. That can happen when a UAS device needs to start a new transfer during ep_enable while the prior transfer is still pending.

The patch simply blocks a second StartTransfer when the same endpoint already has a transfer in progress to prevent a “EP resource busy” issue.

And it can cause a new StartTransfer to be issued later from ep_queue while the starttransfer that should have been started during ep_enable is skipped.

Thanks,
Selva

maintainer
#5 lore raw
Quoted 149 lines · Selvarasu Ganesan
Quoted 141 lines · Thinh Nguyen
Quoted 128 lines · Thinh Nguyen
Quoted 127 lines · Selvarasu Ganesan

The below “No resource for ep” warning appears when a StartTransfer command is issued for bulk or interrupt endpoints in dwc3_gadget_ep_enable while a previous StartTransfer on the same endpoint is still in progress. The gadget functions drivers can invoke usb_ep_enable (which triggers a new StartTransfer command) before the earlier transfer has completed. Because the previous StartTransfer is still active, dwc3_gadget_ep_disable can skip the required EndTransfer due to DWC3_EP_DELAY_STOP, leading to the endpoint resources are busy for previous StartTransfer and warning ("No resource for ep") from dwc3 driver.

Additionally, a race condition exists between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue() when manipulating dep->flags. When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() runs in that window, it may set the DWC3_EP_TRANSFER_STARTED flag as part of dwc3_send_gadget_ep_cmd(). When ep_disable resumes, it unconditionally clears all flags except those explicitly masked, potentially clearing DWC3_EP_TRANSFER_STARTED even though a new transfer has started. This leads to "No resource for ep" warnings on subsequent StartTransfer attempts.

The underlying framework issue is that usb_ep_disable() is expected to complete pending requests before returning, but is allowed to be called from interrupt context where sleeping to wait for completion is not possible.

As temporary workarounds for this framework limitation:

1. In __dwc3_gadget_ep_enable(), add a check for the DWC3_EP_TRANSFER_STARTED flag before issuing a new StartTransfer. This prevents a second StartTransfer on an already busy endpoint, eliminating the resource conflict.

2. In __dwc3_gadget_ep_disable(), preserve the DWC3_EP_TRANSFER_STARTED flag when masking dep->flags if it is actually set, preventing the race with dwc3_gadget_ep_queue() from corrupting the flag state.

These changes eliminate the "No resource for ep" warnings and potential kernel panics caused by panic_on_warn.

dwc3 13200000.dwc3: No resource for ep1out

WARNING: CPU: 0 PID: 700 at drivers/usb/dwc3/gadget.c:398 dwc3_send_gadget_ep_cmd+0x2f8/0x76c
Call trace:
dwc3_send_gadget_ep_cmd+0x2f8/0x76c
__dwc3_gadget_ep_enable+0x490/0x7c0
dwc3_gadget_ep_enable+0x6c/0xe4
usb_ep_enable+0x5c/0x15c
mp_eth_stop+0xd4/0x11c
__dev_close_many+0x160/0x1c8
__dev_change_flags+0xfc/0x220
dev_change_flags+0x24/0x70
devinet_ioctl+0x434/0x524
inet_ioctl+0xa8/0x224
sock_do_ioctl+0x74/0x128
sock_ioctl+0x3bc/0x468
__arm64_sys_ioctl+0xa8/0xe4
invoke_syscall+0x58/0x10c
el0_svc_common+0xa8/0xdc
do_el0_svc+0x1c/0x28
el0_svc+0x38/0x88
el0t_64_sync_handler+0x70/0xbc
el0t_64_sync+0x1a8/0x1ac

---

Note: No Fixes tag is added because this is a workaround for the gadget framework issue where the gadget framework calls usb_ep_disable() in interrupt context without ensuring endpoint flushing completes. A proper fix requires refactoring the framework to make sure usb_ep_disable is invoked in process context.

Changes in v3:
- Revised the commit message to detail the real gadget framework issue
pointed out by the reviewer.
- Merged the two fixes for the same ep wringing into one patch.
Link to v2: https://urldefense.com/v3/__https://lore.kernel.org/linux-usb/[email protected]/__;!!A4F2R9G_pg!cQzQQ5kAWF6CE5hQe7VqFdnaxqwzsTB1ZGNT1GvCH28GoB_nESZR5Y2jtxdZBls6wBIM4OtpvG4dSaylvNC3qbh547k$

Changes in v2:
- Removed change-id.
- Updated commit message.
Link to v1: https://urldefense.com/v3/__https://lore.kernel.org/linux-usb/[email protected]/__;!!A4F2R9G_pg!cQzQQ5kAWF6CE5hQe7VqFdnaxqwzsTB1ZGNT1GvCH28GoB_nESZR5Y2jtxdZBls6wBIM4OtpvG4dSaylvNC38z-CRD4$

---
  drivers/usb/dwc3/gadget.c | 22 ++++++++++++++++++++--
  1 file changed, 20 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +20 -2
index 0a688904ce8c5..3af1bbfe3d92b 100644
@@ -971,8 +971,9 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
  	 * Issue StartTransfer here with no-op TRB so we can always rely on No
  	 * Response Update Transfer command.
  	 */
-	if (usb_endpoint_xfer_bulk(desc) ||
-			usb_endpoint_xfer_int(desc)) {
+	if ((usb_endpoint_xfer_bulk(desc) ||
+			usb_endpoint_xfer_int(desc)) &&
+			!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
  		struct dwc3_gadget_ep_cmd_params params;
  		struct dwc3_trb	*trb;
  		dma_addr_t trb_dma;
@@ -1096,6 +1097,23 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  	 */
  	if (dep->flags & DWC3_EP_DELAY_STOP)
  		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+	/*
+	 * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(),
+	 * the dwc->lock is temporarily released. If dwc3_gadget_ep_queue()
+	 * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as
+	 * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+	 * unconditionally in the mask operation, which could overwrite the
+	 * concurrent modification.
+	 *
+	 * As a workaround for the interrupt context constraint where we cannot
+	 * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED
+	 * flag if it is set, avoiding resource conflicts until the framework
+	 * is fixed to properly synchronize endpoint lifecycle management.
+	 */
+	if (dep->flags & DWC3_EP_TRANSFER_STARTED)
+		mask |= DWC3_EP_TRANSFER_STARTED;
+
  	dep->flags &= mask;
 
Quoted 6 lines
Quoted 4 lines
drivers/usb/dwc3/gadget.c +0 -0
  	/* Clear out the ep descriptors for non-ep0 */
Signature · 1 line
2.34.1

Oh wait, don't pick this patch up yet.

This will cause a regression for UAS device. When switching alt-setting interface for BOT to UASP, the device needs to issue a Start Transfer command.

Quoted 5 lines

This workaround won't work. Can we fix the usb_ep_disable() interface and rework this instead?

BR,
Thinh

Quoted 7 lines

Hi Thinh,

We’re trying to see how this change could cause a regression for UAS devices.
Could you explain why the workaround might be a problem for UAS? Are you concerned that it could miss a valid StartTransfer when a previous transfer finishes later than expected as part of ep_disable?

In UAS, the device controller uses the first PRIME to synchronize with the host to determine whether the Start Transfer command can initiate the stream. After configuring an endpoint, if we issue the StartTransfer command too late, then the device controller may not initiate the transfer (sending ERDY), and host will not know when to start the transfer.

So we have this workaround that we would have the device Start and Stop the endpoint immediately just to arm the endpoint for UASP transfers. In the newer IPs, this workaround may not be needed.

Quoted 7 lines

If we don’t use this temporary fix, the driver can still report “EP resource busy” when an earlier StartTransfer hasn’t finished before ep_disable returns. That can happen when a UAS device needs to start a new transfer during ep_enable while the prior transfer is still pending.

Quoted 7 lines

The patch simply blocks a second StartTransfer when the same endpoint already has a transfer in progress to prevent a “EP resource busy” issue.

And it can cause a new StartTransfer to be issued later from ep_queue while the starttransfer that should have been started during ep_enable is skipped.

Also, if the gadget driver just uses usb_ep_disable() to handle the teardown instead of proactively dequeuing all the active requests, then the DWC3_EP_TRANSFER_STARTED flag will still be cleared immediately on usb_ep_disable() and we will still run into this issue again.

Another workaround is to have the dwc3 driver retry the command after a small delay if there's no resource error report. Only do dev_WARN after a few times of the same failure.

Ideally, we should fix the usb_ep_disable() and have the composite framework properly handle the "wait" for completion.

BR,
Thinh

#6 lore raw
Quoted 155 lines · Thinh Nguyen
Quoted 147 lines · Selvarasu Ganesan
Quoted 141 lines · Thinh Nguyen
Quoted 128 lines · Thinh Nguyen
Quoted 127 lines · Selvarasu Ganesan

The below “No resource for ep” warning appears when a StartTransfer command is issued for bulk or interrupt endpoints in dwc3_gadget_ep_enable while a previous StartTransfer on the same endpoint is still in progress. The gadget functions drivers can invoke usb_ep_enable (which triggers a new StartTransfer command) before the earlier transfer has completed. Because the previous StartTransfer is still active, dwc3_gadget_ep_disable can skip the required EndTransfer due to DWC3_EP_DELAY_STOP, leading to the endpoint resources are busy for previous StartTransfer and warning ("No resource for ep") from dwc3 driver.

Additionally, a race condition exists between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue() when manipulating dep->flags. When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() runs in that window, it may set the DWC3_EP_TRANSFER_STARTED flag as part of dwc3_send_gadget_ep_cmd(). When ep_disable resumes, it unconditionally clears all flags except those explicitly masked, potentially clearing DWC3_EP_TRANSFER_STARTED even though a new transfer has started. This leads to "No resource for ep" warnings on subsequent StartTransfer attempts.

The underlying framework issue is that usb_ep_disable() is expected to complete pending requests before returning, but is allowed to be called from interrupt context where sleeping to wait for completion is not possible.

As temporary workarounds for this framework limitation:

1. In __dwc3_gadget_ep_enable(), add a check for the DWC3_EP_TRANSFER_STARTED flag before issuing a new StartTransfer. This prevents a second StartTransfer on an already busy endpoint, eliminating the resource conflict.

2. In __dwc3_gadget_ep_disable(), preserve the DWC3_EP_TRANSFER_STARTED flag when masking dep->flags if it is actually set, preventing the race with dwc3_gadget_ep_queue() from corrupting the flag state.

These changes eliminate the "No resource for ep" warnings and potential kernel panics caused by panic_on_warn.

dwc3 13200000.dwc3: No resource for ep1out

WARNING: CPU: 0 PID: 700 at drivers/usb/dwc3/gadget.c:398 dwc3_send_gadget_ep_cmd+0x2f8/0x76c
Call trace:
dwc3_send_gadget_ep_cmd+0x2f8/0x76c
__dwc3_gadget_ep_enable+0x490/0x7c0
dwc3_gadget_ep_enable+0x6c/0xe4
usb_ep_enable+0x5c/0x15c
mp_eth_stop+0xd4/0x11c
__dev_close_many+0x160/0x1c8
__dev_change_flags+0xfc/0x220
dev_change_flags+0x24/0x70
devinet_ioctl+0x434/0x524
inet_ioctl+0xa8/0x224
sock_do_ioctl+0x74/0x128
sock_ioctl+0x3bc/0x468
__arm64_sys_ioctl+0xa8/0xe4
invoke_syscall+0x58/0x10c
el0_svc_common+0xa8/0xdc
do_el0_svc+0x1c/0x28
el0_svc+0x38/0x88
el0t_64_sync_handler+0x70/0xbc
el0t_64_sync+0x1a8/0x1ac

---

Note: No Fixes tag is added because this is a workaround for the gadget framework issue where the gadget framework calls usb_ep_disable() in interrupt context without ensuring endpoint flushing completes. A proper fix requires refactoring the framework to make sure usb_ep_disable is invoked in process context.

Changes in v3:
- Revised the commit message to detail the real gadget framework issue
pointed out by the reviewer.
- Merged the two fixes for the same ep wringing into one patch.
Link to v2: https://protect2.fireeye.com/v1/url?k=5535d0f4-344e7a7d-55345bbb-74fe48600034-c617d7b77912682d&q=1&e=2a6508e6-6363-4d6e-b6ab-ce6c77832c8d&u=https%3A%2F%2Flore.kernel.org%2Flinux-usb%2F20251117155920.643-1-selvarasu.g%40samsung.com%2F

Changes in v2:
- Removed change-id.
- Updated commit message.
Link to v1: https://protect2.fireeye.com/v1/url?k=c8daed0d-a9a14784-c8db6642-74fe48600034-8488506d5854e40d&q=1&e=2a6508e6-6363-4d6e-b6ab-ce6c77832c8d&u=https%3A%2F%2Flore.kernel.org%2Flinux-usb%2F20251117152812.622-1-selvarasu.g%40samsung.com%2F

---
   drivers/usb/dwc3/gadget.c | 22 ++++++++++++++++++++--
   1 file changed, 20 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +20 -2
index 0a688904ce8c5..3af1bbfe3d92b 100644
@@ -971,8 +971,9 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
   	 * Issue StartTransfer here with no-op TRB so we can always rely on No
   	 * Response Update Transfer command.
   	 */
-	if (usb_endpoint_xfer_bulk(desc) ||
-			usb_endpoint_xfer_int(desc)) {
+	if ((usb_endpoint_xfer_bulk(desc) ||
+			usb_endpoint_xfer_int(desc)) &&
+			!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
   		struct dwc3_gadget_ep_cmd_params params;
   		struct dwc3_trb	*trb;
   		dma_addr_t trb_dma;
@@ -1096,6 +1097,23 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
   	 */
   	if (dep->flags & DWC3_EP_DELAY_STOP)
   		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+	/*
+	 * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(),
+	 * the dwc->lock is temporarily released. If dwc3_gadget_ep_queue()
+	 * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as
+	 * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+	 * unconditionally in the mask operation, which could overwrite the
+	 * concurrent modification.
+	 *
+	 * As a workaround for the interrupt context constraint where we cannot
+	 * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED
+	 * flag if it is set, avoiding resource conflicts until the framework
+	 * is fixed to properly synchronize endpoint lifecycle management.
+	 */
+	if (dep->flags & DWC3_EP_TRANSFER_STARTED)
+		mask |= DWC3_EP_TRANSFER_STARTED;
+
   	dep->flags &= mask;
 
Quoted 6 lines
Quoted 4 lines
drivers/usb/dwc3/gadget.c +0 -0
   	/* Clear out the ep descriptors for non-ep0 */
Signature · 1 line
2.34.1

Oh wait, don't pick this patch up yet.

This will cause a regression for UAS device. When switching alt-setting interface for BOT to UASP, the device needs to issue a Start Transfer command.

Quoted 5 lines

This workaround won't work. Can we fix the usb_ep_disable() interface and rework this instead?

BR,
Thinh

Quoted 7 lines

Hi Thinh,

We’re trying to see how this change could cause a regression for UAS devices.
Could you explain why the workaround might be a problem for UAS? Are you concerned that it could miss a valid StartTransfer when a previous transfer finishes later than expected as part of ep_disable?

Quoted 6 lines

In UAS, the device controller uses the first PRIME to synchronize with the host to determine whether the Start Transfer command can initiate the stream. After configuring an endpoint, if we issue the StartTransfer command too late, then the device controller may not initiate the transfer (sending ERDY), and host will not know when to start the transfer.

HI Thinh,

Sorry for the delayed response. We're seeing this issue a lot in Exynos platform, so we want to get it fixed.

Thanks for the explanation about UASP in last comment.

We agree that for UASP, the new StartTransfer sent in ep_enable() is what makes the controller send ERDY for the host's first PRIME. The old patch skipped that StartTransfer that was wrong. If it is skipped and no request is queued right away, the controller never sends ERDY for the first PRIME and the UAS device possible hangs as you mentioned.

The new patch is simpler, we won't skip it and just trigger it once the inflight end transfer is done.

Please see the proposed sequence,

  1. usb_ep_disable() --> EndTransfer deferred due to pending control
transfer data/status stages (DWC3_EP_DELAY_STOP), old transfer still active in HW.
  2. usb_ep_enable() on the same endpoint --> instead of issuing the
new StartTransfer, set DWC3_EP_PENDING_START_TRANSFER (New flag) as below,

  /* __dwc3_gadget_ep_enable() */
  if (dep->flags & (DWC3_EP_DELAY_STOP | DWC3_EP_END_TRANSFER_PENDING | DWC3_EP_TRANSFER_STARTED))
      dep->flags |= DWC3_EP_PENDING_START_TRANSFER;
  else
      ret = dwc3_gadget_ep_start_noop_transfer(dep);   /* unchanged path */

  3. Control request finishes --> next SETUP --> existing delayed stop
retry in dwc3_ep0_out_start() sends the EndTransfer for all delayed stop endpoints.
  4. EndTransfer completion and trigger a previously skipped
StartTransfer in ep_enable by checking DWC3_EP_PENDING_START_TRANSFER.

  /* dwc3_gadget_endpoint_command_complete() */ if (dep->flags & DWC3_EP_PENDING_START_TRANSFER) { dep->flags &= ~DWC3_EP_PENDING_START_TRANSFER; dwc3_gadget_ep_start_noop_transfer(dep); }

So the endpoint gets the same start/stop sequence you described, only sent later, it goes out as soon as the EndTransfer finishes. So this is high possible of happens before the host's first PRIME arrives. ERDY is still sent, no UAS regression.

The below testing log is for the reference. You can see the timestamp where pending start transfer triggered.

[ 3273.597476]  Entry __dwc3_gadget_ep_disable
[ 3273.597481]  dwc3_remove_requests ep1out skip stop transfer due to 
DWC3_EP_DELAY_STOP
[ 3273.597510]  Entry __dwc3_gadget_ep_enable 1045 dep->name =ep1out 
dep->flags =e009
[ 3273.597590]  dwc3_gadget_endpoint_command_complete 3857 dep->name 

=ep1out dep->flags =4009 --> Triggered skipped Start transfer when endpoint command completion is done.

Thanks,
Selva

Quoted 31 lines

So we have this workaround that we would have the device Start and Stop the endpoint immediately just to arm the endpoint for UASP transfers. In the newer IPs, this workaround may not be needed.

Quoted 6 lines

If we don’t use this temporary fix, the driver can still report “EP resource busy” when an earlier StartTransfer hasn’t finished before ep_disable returns. That can happen when a UAS device needs to start a new transfer during ep_enable while the prior transfer is still pending.

Quoted 7 lines

The patch simply blocks a second StartTransfer when the same endpoint already has a transfer in progress to prevent a “EP resource busy” issue.

And it can cause a new StartTransfer to be issued later from ep_queue while the starttransfer that should have been started during ep_enable is skipped.

Also, if the gadget driver just uses usb_ep_disable() to handle the teardown instead of proactively dequeuing all the active requests, then the DWC3_EP_TRANSFER_STARTED flag will still be cleared immediately on usb_ep_disable() and we will still run into this issue again.

Another workaround is to have the dwc3 driver retry the command after a small delay if there's no resource error report. Only do dev_WARN after a few times of the same failure.

Ideally, we should fix the usb_ep_disable() and have the composite framework properly handle the "wait" for completion.

BR,
Thinh

#7 lore raw
Quoted 260 lines · Selvarasu Ganesan
Quoted 190 lines · Thinh Nguyen
Quoted 181 lines · Selvarasu Ganesan
Quoted 175 lines · Thinh Nguyen
Quoted 158 lines · Thinh Nguyen
Quoted 157 lines · Selvarasu Ganesan

The below “No resource for ep” warning appears when a StartTransfer command is issued for bulk or interrupt endpoints in dwc3_gadget_ep_enable while a previous StartTransfer on the same endpoint is still in progress. The gadget functions drivers can invoke
usb_ep_enable (which triggers a new StartTransfer command) before the
earlier transfer has completed. Because the previous StartTransfer is
still active, dwc3_gadget_ep_disable can skip the required EndTransfer due to DWC3_EP_DELAY_STOP, leading to the endpoint resources are busy for previous StartTransfer and warning ("No resource
for ep") from dwc3 driver.

Additionally, a race condition exists between dwc3_gadget_ep_disable()
and dwc3_gadget_ep_queue() when manipulating dep->flags. When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), the dwc->lock is
temporarily released. If dwc3_gadget_ep_queue() runs in that window, it
may set the DWC3_EP_TRANSFER_STARTED flag as part of dwc3_send_gadget_ep_cmd(). When ep_disable resumes, it unconditionally
clears all flags except those explicitly masked, potentially clearing
DWC3_EP_TRANSFER_STARTED even though a new transfer has started. This
leads to "No resource for ep" warnings on subsequent StartTransfer attempts.

The underlying framework issue is that usb_ep_disable() is expected to
complete pending requests before returning, but is allowed to be called
from interrupt context where sleeping to wait for completion is not possible.

As temporary workarounds for this framework limitation:

1. In __dwc3_gadget_ep_enable(), add a check for the DWC3_EP_TRANSFER_STARTED flag before issuing a new StartTransfer.
     This prevents a second StartTransfer on an already busy endpoint,
     eliminating the resource conflict.

2. In __dwc3_gadget_ep_disable(), preserve the DWC3_EP_TRANSFER_STARTED
     flag when masking dep->flags if it is actually set, preventing the
     race with dwc3_gadget_ep_queue() from corrupting the flag state.

These changes eliminate the "No resource for ep" warnings and potential
kernel panics caused by panic_on_warn.

dwc3 13200000.dwc3: No resource for ep1out

WARNING: CPU: 0 PID: 700 at drivers/usb/dwc3/gadget.c:398 
dwc3_send_gadget_ep_cmd+0x2f8/0x76c
Call trace:
dwc3_send_gadget_ep_cmd+0x2f8/0x76c
__dwc3_gadget_ep_enable+0x490/0x7c0
dwc3_gadget_ep_enable+0x6c/0xe4
usb_ep_enable+0x5c/0x15c
mp_eth_stop+0xd4/0x11c
__dev_close_many+0x160/0x1c8
__dev_change_flags+0xfc/0x220
dev_change_flags+0x24/0x70
devinet_ioctl+0x434/0x524
inet_ioctl+0xa8/0x224
sock_do_ioctl+0x74/0x128
sock_ioctl+0x3bc/0x468
__arm64_sys_ioctl+0xa8/0xe4
invoke_syscall+0x58/0x10c
el0_svc_common+0xa8/0xdc
do_el0_svc+0x1c/0x28
el0_svc+0x38/0x88
el0t_64_sync_handler+0x70/0xbc
el0t_64_sync+0x1a8/0x1ac

---

Note: No Fixes tag is added because this is a workaround for the gadget framework issue where the gadget framework calls usb_ep_disable()
in interrupt context without ensuring endpoint flushing completes. A proper fix requires refactoring the framework to make sure usb_ep_disable is invoked in process context.

Changes in v3:
   - Revised the commit message to detail the real gadget framework issue
     pointed out by the reviewer.
   - Merged the two fixes for the same ep wringing into one patch. Link to v2:
https://protect2.fireeye.com/v1/url?k=5535d0f4-344e7a7d-55345bbb-74fe48600034-c617d7b77912682d&q=1&e=2a6508e6-6363-4d6e-b6ab-ce6c77832c8d&u=https%3A%2F%2Flore.kernel.org%2Flinux-usb%2F20251117155920.643-1-selvarasu.g%40samsung.com%2F

Changes in v2:
- Removed change-id.
- Updated commit message.
Link to v1:
https://protect2.fireeye.com/v1/url?k=c8daed0d-a9a14784-c8db6642-74fe48600034-8488506d5854e40d&q=1&e=2a6508e6-6363-4d6e-b6ab-ce6c77832c8d&u=https%3A%2F%2Flore.kernel.org%2Flinux-usb%2F20251117152812.622-1-selvarasu.g%40samsung.com%2F
---
   drivers/usb/dwc3/gadget.c | 22 ++++++++++++++++++++--

   1 file changed, 20 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +0 -0
index 0a688904ce8c5..3af1bbfe3d92b 100644
@@ -971,8 +971,9 @@ static int __dwc3_gadget_ep_enable(struct 
 dwc3_ep *dep, unsigned int action)
         * Issue StartTransfer here with no-op TRB so we can 

always rely on No
        * Response Update Transfer command.
        */
-    if (usb_endpoint_xfer_bulk(desc) ||
-            usb_endpoint_xfer_int(desc)) {
+    if ((usb_endpoint_xfer_bulk(desc) ||
+            usb_endpoint_xfer_int(desc)) &&
+            !(dep->flags & DWC3_EP_TRANSFER_STARTED)) { struct dwc3_gadget_ep_cmd_params params; struct dwc3_trb    *trb;
           dma_addr_t trb_dma;

drivers/usb/dwc3/gadget.c +5 -0
@@ -1096,6 +1097,23 @@ static int __dwc3_gadget_ep_disable(struct 
 dwc3_ep *dep)
         */
        if (dep->flags & DWC3_EP_DELAY_STOP)
            mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+    /*
+     * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(),
+     * the dwc->lock is temporarily released. If 
 dwc3_gadget_ep_queue()
+     * runs in that window it may set the 

DWC3_EP_TRANSFER_STARTED flag as
+     * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+     * unconditionally in the mask operation, which could overwrite the
+     * concurrent modification.
+     *
+     * As a workaround for the interrupt context constraint where we cannot
+     * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED
+     * flag if it is set, avoiding resource conflicts until the framework
+     * is fixed to properly synchronize endpoint lifecycle management.
+     */
+    if (dep->flags & DWC3_EP_TRANSFER_STARTED)

Quoted 9 lines
Quoted 7 lines

+        mask |= DWC3_EP_TRANSFER_STARTED;
+
       dep->flags &= mask;
          /* Clear out the ep descriptors for non-ep0 */

Signature · 1 line
2.34.1

Oh wait, don't pick this patch up yet.

This will cause a regression for UAS device. When switching alt-setting
interface for BOT to UASP, the device needs to issue a Start Transfer command.

Quoted 5 lines

This workaround won't work. Can we fix the usb_ep_disable() interface and rework this instead?

BR,
Thinh

Quoted 8 lines

Hi Thinh,

We’re trying to see how this change could cause a regression for UAS devices.
Could you explain why the workaround might be a problem for UAS? Are you
concerned that it could miss a valid StartTransfer when a previous transfer finishes later than expected as part of ep_disable?

Quoted 6 lines

In UAS, the device controller uses the first PRIME to synchronize with the host to determine whether the Start Transfer command can initiate the stream. After configuring an endpoint, if we issue the StartTransfer command too late, then the device controller may not initiate the transfer (sending ERDY), and host will not know when to start the transfer.

HI Thinh,

Sorry for the delayed response. We're seeing this issue a lot in Exynos platform, so we want to get it fixed.

Thanks for the explanation about UASP in last comment.

We agree that for UASP, the new StartTransfer sent in ep_enable() is what makes the controller send ERDY for the host's first PRIME. The old patch skipped that StartTransfer that was wrong. If it is skipped and no request is queued right away, the controller never sends ERDY for the first PRIME and the UAS device possible hangs as you mentioned.

The new patch is simpler, we won't skip it and just trigger it once the inflight end transfer is done.

Please see the proposed sequence,

  1. usb_ep_disable() --> EndTransfer deferred due to pending control transfer data/status stages (DWC3_EP_DELAY_STOP), old transfer still active in HW.
  2. usb_ep_enable() on the same endpoint --> instead of issuing the new StartTransfer, set DWC3_EP_PENDING_START_TRANSFER (New flag) as below,

  /* __dwc3_gadget_ep_enable() */
  if (dep->flags & (DWC3_EP_DELAY_STOP | DWC3_EP_END_TRANSFER_PENDING | DWC3_EP_TRANSFER_STARTED)) dep->flags |= DWC3_EP_PENDING_START_TRANSFER;
  else
      ret = dwc3_gadget_ep_start_noop_transfer(dep);   /* unchanged path */

  3. Control request finishes --> next SETUP --> existing delayed stop retry in dwc3_ep0_out_start() sends the EndTransfer for all delayed stop endpoints.
  4. EndTransfer completion and trigger a previously skipped StartTransfer in ep_enable by checking DWC3_EP_PENDING_START_TRANSFER.

  /* dwc3_gadget_endpoint_command_complete() */ if (dep->flags & DWC3_EP_PENDING_START_TRANSFER) { dep->flags &= ~DWC3_EP_PENDING_START_TRANSFER; dwc3_gadget_ep_start_noop_transfer(dep); }

So the endpoint gets the same start/stop sequence you described, only sent later, it goes out as soon as the EndTransfer finishes. So this is high possible of happens before the host's first PRIME arrives. ERDY is still sent, no UAS regression.

The below testing log is for the reference. You can see the timestamp where pending start transfer triggered.

[ 3273.597476]  Entry __dwc3_gadget_ep_disable
[ 3273.597481]  dwc3_remove_requests ep1out skip stop transfer due to 
DWC3_EP_DELAY_STOP
[ 3273.597510]  Entry __dwc3_gadget_ep_enable 1045 dep->name =ep1out 
dep->flags =e009
[ 3273.597590]  dwc3_gadget_endpoint_command_complete 3857 dep->name 
Quoted 7 lines

=ep1out dep->flags =4009 --> Triggered skipped Start transfer when endpoint command completion is done.

Thanks,
Selva

The below patch is for this proposed sequence,

drivers/usb/dwc3/gadget.c +0 -0
index fa0f16ffafef..40fe930a6c2c 100644
@@ -906,6 +906,72 @@ static int dwc3_gadget_resize_tx_fifos(struct 

dwc3_ep *dep)
        return 0;
 }

+/**
+ * dwc3_gadget_ep_start_noop_transfer - start a no-op transfer on the endpoint
+ * @dep: bulk or interrupt endpoint
+ *
+ * Issue StartTransfer here with no-op TRB so we can always rely on No
+ * Response Update Transfer command.
+ *
+ * Caller should take care of locking. The endpoint must be enabled and must
+ * not have an active transfer.
+ */
+static int dwc3_gadget_ep_start_noop_transfer(struct dwc3_ep *dep) +{
+       struct dwc3_gadget_ep_cmd_params params;
+       struct dwc3_trb *trb;
+       struct dwc3             *dwc = dep->dwc;
+       dma_addr_t trb_dma;
+       u32 cmd;
+       int ret;
+
+       memset(&params, 0, sizeof(params));
+       trb = &dep->trb_pool[0];
+       trb_dma = dwc3_trb_dma_offset(dep, trb);
+
+       params.param0 = upper_32_bits(trb_dma);
+       params.param1 = lower_32_bits(trb_dma);
+
+       cmd = DWC3_DEPCMD_STARTTRANSFER;
+
+       ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
+       if (ret < 0)
+               return ret;
+
+       if (dep->stream_capable) {
+               /*
+                * For streams, at start, there maybe a race where the +                * host primes the endpoint before the function driver +                * queues a request to initiate a stream. In that case, +                * the controller will not see the prime to generate the +                * ERDY and start stream. To workaround this, issue a +                * no-op TRB as normal, but end it immediately. As a +                * result, when the function driver queues the request, +                * the next START_TRANSFER command will cause the +                * controller to generate an ERDY to initiate the +                * stream.
+                */
+               dwc3_stop_active_transfer(dep, true, true);
+
+               /*
+                * All stream eps will reinitiate stream on NoStream +                * rejection until we can determine that the host can +                * prime after the first transfer.
+                *
+                * However, if the controller is capable of
+                * TXF_FLUSH_BYPASS, then IN direction endpoints will +                * automatically restart the stream without the driver +                * initiation.
+                */
+               if (!dep->direction ||
+                   !(dwc->hwparams.hwparams9 &
+                     DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
+                       dep->flags |= DWC3_EP_FORCE_RESTART_STREAM; +       }
+
+       return 0;
+}
+
 /**
  * __dwc3_gadget_ep_enable - initializes a hw endpoint
  * @dep: endpoint to be initialized
@@ -973,52 +1039,26 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
         */
        if (usb_endpoint_xfer_bulk(desc) || usb_endpoint_xfer_int(desc)) {
-               struct dwc3_gadget_ep_cmd_params params;
-               struct dwc3_trb *trb;
-               dma_addr_t trb_dma;
-               u32 cmd;
-
-               memset(&params, 0, sizeof(params));
-               trb = &dep->trb_pool[0];
-               trb_dma = dwc3_trb_dma_offset(dep, trb);
-
-               params.param0 = upper_32_bits(trb_dma);
-               params.param1 = lower_32_bits(trb_dma);
-
-               cmd = DWC3_DEPCMD_STARTTRANSFER;
-
-               ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params); -               if (ret < 0)
-                       return ret;
-
-               if (dep->stream_capable) {
+               if (dep->flags & (DWC3_EP_DELAY_STOP |
+                                 DWC3_EP_END_TRANSFER_PENDING | +                                 DWC3_EP_TRANSFER_STARTED)) {
                        /*
-                        * For streams, at start, there maybe a race where the
-                        * host primes the endpoint before the function driver
-                        * queues a request to initiate a stream. In that case,
-                        * the controller will not see the prime to generate the
-                        * ERDY and start stream. To workaround this, issue a
-                        * no-op TRB as normal, but end it immediately. As a -                        * result, when the function driver queues the request,
-                        * the next START_TRANSFER command will cause the -                        * controller to generate an ERDY to initiate the -                        * stream.
-                        */
-                       dwc3_stop_active_transfer(dep, true, true); -
-                       /*
-                        * All stream eps will reinitiate stream on NoStream -                        * rejection.
-                        *
-                        * However, if the controller is capable of -                        * TXF_FLUSH_BYPASS, then IN direction endpoints will
-                        * automatically restart the stream without the driver
-                        * initiation.
+                        * The previous transfer has not been ended in +                        * hardware yet: the prior usb_ep_disable() left a +                        * deferred (DWC3_EP_DELAY_STOP) or in-flight +                        * (DWC3_EP_END_TRANSFER_PENDING) EndTransfer +                        * command.  Issuing StartTransfer now would be +                        * rejected with "No resource" because the endpoint +                        * still has an active transfer.  Defer the no-op +                        * StartTransfer; it will be replayed from +                        * dwc3_gadget_endpoint_command_complete() once the +                        * EndTransfer has completed.
                         */
-                       if (!dep->direction ||
-                           !(dwc->hwparams.hwparams9 &
-  DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
-                               dep->flags |= DWC3_EP_FORCE_RESTART_STREAM; +                       dep->flags |= DWC3_EP_PENDING_START_TRANSFER; +               } else {
+                       ret = dwc3_gadget_ep_start_noop_transfer(dep); +                       if (ret < 0)
+                               return ret;
                }
        }

@@ -1096,6 +1136,23 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
         */
        if (dep->flags & DWC3_EP_DELAY_STOP)
                mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+       /*
+        * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), +        * the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() +        * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as
+        * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+        * unconditionally in the mask operation, which could overwrite the +        * concurrent modification.
+        *
+        * As a workaround for the interrupt context constraint where we cannot
+        * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED +        * flag if it is set, avoiding resource conflicts until the framework
+        * is fixed to properly synchronize endpoint lifecycle management. +        */
+       if (dep->flags & DWC3_EP_TRANSFER_STARTED)
+               mask |= DWC3_EP_TRANSFER_STARTED;
+
        dep->flags &= mask;

        /* Clear out the ep descriptors for non-ep0 */
@@ -3861,6 +3918,22 @@ static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
                        dwc3_ep0_send_delayed_status(dwc); }

+       /*
+        * __dwc3_gadget_ep_enable() deferred the no-op StartTransfer while +        * this EndTransfer was pending (the endpoint was re-enabled before +        * the deferred EndTransfer of a prior usb_ep_disable() completed). +        * Now that the previous transfer has been ended in hardware, issue +        * the no-op StartTransfer to (re)arm the endpoint, mirroring the +        * original usb_ep_enable() path.  If requests were queued in the +        * meantime, the delayed-start kick below issues an UpdateTransfer +        * with the first request's TRB, exactly like the normal +        * enable-then-queue sequence.
+        */
+       if (dep->flags & DWC3_EP_PENDING_START_TRANSFER) {
+               dep->flags &= ~DWC3_EP_PENDING_START_TRANSFER; +               dwc3_gadget_ep_start_noop_transfer(dep);
+       }
+
        if ((dep->flags & DWC3_EP_DELAY_START) && !usb_endpoint_xfer_isoc(dep->endpoint.desc)) __dwc3_gadget_kick_transfer(dep);

Thanks,
Selva

Quoted 32 lines
Quoted 32 lines

So we have this workaround that we would have the device Start and Stop the endpoint immediately just to arm the endpoint for UASP transfers. In the newer IPs, this workaround may not be needed.

Quoted 6 lines

If we don’t use this temporary fix, the driver can still report “EP resource busy” when an earlier StartTransfer hasn’t finished before ep_disable returns. That can happen when a UAS device needs to start a new transfer during ep_enable while the prior transfer is still pending.

Quoted 8 lines

The patch simply blocks a second StartTransfer when the same endpoint already has a transfer in progress to prevent a “EP resource busy” issue.

And it can cause a new StartTransfer to be issued later from ep_queue while the starttransfer that should have been started during ep_enable is skipped.

Also, if the gadget driver just uses usb_ep_disable() to handle the teardown instead of proactively dequeuing all the active requests, then the DWC3_EP_TRANSFER_STARTED flag will still be cleared immediately on usb_ep_disable() and we will still run into this issue again.

Another workaround is to have the dwc3 driver retry the command after a small delay if there's no resource error report. Only do dev_WARN after a few times of the same failure.

Ideally, we should fix the usb_ep_disable() and have the composite framework properly handle the "wait" for completion.

BR,
Thinh

maintainer
#8 lore raw
In reply to Selvarasu Ganesan
Quoted 217 lines · Selvarasu Ganesan
Quoted 155 lines · Thinh Nguyen
Quoted 147 lines · Selvarasu Ganesan
Quoted 141 lines · Thinh Nguyen
Quoted 128 lines · Thinh Nguyen
Quoted 127 lines · Selvarasu Ganesan

The below “No resource for ep” warning appears when a StartTransfer command is issued for bulk or interrupt endpoints in dwc3_gadget_ep_enable while a previous StartTransfer on the same endpoint is still in progress. The gadget functions drivers can invoke usb_ep_enable (which triggers a new StartTransfer command) before the earlier transfer has completed. Because the previous StartTransfer is still active, dwc3_gadget_ep_disable can skip the required EndTransfer due to DWC3_EP_DELAY_STOP, leading to the endpoint resources are busy for previous StartTransfer and warning ("No resource for ep") from dwc3 driver.

Additionally, a race condition exists between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue() when manipulating dep->flags. When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() runs in that window, it may set the DWC3_EP_TRANSFER_STARTED flag as part of dwc3_send_gadget_ep_cmd(). When ep_disable resumes, it unconditionally clears all flags except those explicitly masked, potentially clearing DWC3_EP_TRANSFER_STARTED even though a new transfer has started. This leads to "No resource for ep" warnings on subsequent StartTransfer attempts.

The underlying framework issue is that usb_ep_disable() is expected to complete pending requests before returning, but is allowed to be called from interrupt context where sleeping to wait for completion is not possible.

As temporary workarounds for this framework limitation:

1. In __dwc3_gadget_ep_enable(), add a check for the DWC3_EP_TRANSFER_STARTED flag before issuing a new StartTransfer. This prevents a second StartTransfer on an already busy endpoint, eliminating the resource conflict.

2. In __dwc3_gadget_ep_disable(), preserve the DWC3_EP_TRANSFER_STARTED flag when masking dep->flags if it is actually set, preventing the race with dwc3_gadget_ep_queue() from corrupting the flag state.

These changes eliminate the "No resource for ep" warnings and potential kernel panics caused by panic_on_warn.

dwc3 13200000.dwc3: No resource for ep1out

WARNING: CPU: 0 PID: 700 at drivers/usb/dwc3/gadget.c:398 dwc3_send_gadget_ep_cmd+0x2f8/0x76c
Call trace:
dwc3_send_gadget_ep_cmd+0x2f8/0x76c
__dwc3_gadget_ep_enable+0x490/0x7c0
dwc3_gadget_ep_enable+0x6c/0xe4
usb_ep_enable+0x5c/0x15c
mp_eth_stop+0xd4/0x11c
__dev_close_many+0x160/0x1c8
__dev_change_flags+0xfc/0x220
dev_change_flags+0x24/0x70
devinet_ioctl+0x434/0x524
inet_ioctl+0xa8/0x224
sock_do_ioctl+0x74/0x128
sock_ioctl+0x3bc/0x468
__arm64_sys_ioctl+0xa8/0xe4
invoke_syscall+0x58/0x10c
el0_svc_common+0xa8/0xdc
do_el0_svc+0x1c/0x28
el0_svc+0x38/0x88
el0t_64_sync_handler+0x70/0xbc
el0t_64_sync+0x1a8/0x1ac

---

Note: No Fixes tag is added because this is a workaround for the gadget framework issue where the gadget framework calls usb_ep_disable() in interrupt context without ensuring endpoint flushing completes. A proper fix requires refactoring the framework to make sure usb_ep_disable is invoked in process context.

Changes in v3:
- Revised the commit message to detail the real gadget framework issue
pointed out by the reviewer.
- Merged the two fixes for the same ep wringing into one patch.
Link to v2: https://urldefense.com/v3/__https://protect2.fireeye.com/v1/url?k=5535d0f4-344e7a7d-55345bbb-74fe48600034-c617d7b77912682d&q=1&e=2a6508e6-6363-4d6e-b6ab-ce6c77832c8d&u=https*3A*2F*2Flore.kernel.org*2Flinux-usb*2F20251117155920.643-1-selvarasu.g*40samsung.com*2F__;JSUlJSUlJQ!!A4F2R9G_pg!dR0yufG2VvgMnkC0ZJ4eK3I5v7C5q2ry8MYAYSNdWhMNM2zPkO35fGBJJHPS-GhG4y9hzmrCcO16V1ASehHBYxvxQWQ$

Changes in v2:
- Removed change-id.
- Updated commit message.
Link to v1: https://urldefense.com/v3/__https://protect2.fireeye.com/v1/url?k=c8daed0d-a9a14784-c8db6642-74fe48600034-8488506d5854e40d&q=1&e=2a6508e6-6363-4d6e-b6ab-ce6c77832c8d&u=https*3A*2F*2Flore.kernel.org*2Flinux-usb*2F20251117152812.622-1-selvarasu.g*40samsung.com*2F__;JSUlJSUlJQ!!A4F2R9G_pg!dR0yufG2VvgMnkC0ZJ4eK3I5v7C5q2ry8MYAYSNdWhMNM2zPkO35fGBJJHPS-GhG4y9hzmrCcO16V1ASehHB1Y4qk_k$

---
   drivers/usb/dwc3/gadget.c | 22 ++++++++++++++++++++--
   1 file changed, 20 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +20 -2
index 0a688904ce8c5..3af1bbfe3d92b 100644
@@ -971,8 +971,9 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
   	 * Issue StartTransfer here with no-op TRB so we can always rely on No
   	 * Response Update Transfer command.
   	 */
-	if (usb_endpoint_xfer_bulk(desc) ||
-			usb_endpoint_xfer_int(desc)) {
+	if ((usb_endpoint_xfer_bulk(desc) ||
+			usb_endpoint_xfer_int(desc)) &&
+			!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
   		struct dwc3_gadget_ep_cmd_params params;
   		struct dwc3_trb	*trb;
   		dma_addr_t trb_dma;
@@ -1096,6 +1097,23 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
   	 */
   	if (dep->flags & DWC3_EP_DELAY_STOP)
   		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+	/*
+	 * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(),
+	 * the dwc->lock is temporarily released. If dwc3_gadget_ep_queue()
+	 * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as
+	 * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+	 * unconditionally in the mask operation, which could overwrite the
+	 * concurrent modification.
+	 *
+	 * As a workaround for the interrupt context constraint where we cannot
+	 * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED
+	 * flag if it is set, avoiding resource conflicts until the framework
+	 * is fixed to properly synchronize endpoint lifecycle management.
+	 */
+	if (dep->flags & DWC3_EP_TRANSFER_STARTED)
+		mask |= DWC3_EP_TRANSFER_STARTED;
+
   	dep->flags &= mask;
 
Quoted 6 lines
Quoted 4 lines
drivers/usb/dwc3/gadget.c +0 -0
   	/* Clear out the ep descriptors for non-ep0 */
Signature · 1 line
2.34.1

Oh wait, don't pick this patch up yet.

This will cause a regression for UAS device. When switching alt-setting interface for BOT to UASP, the device needs to issue a Start Transfer command.

Quoted 5 lines

This workaround won't work. Can we fix the usb_ep_disable() interface and rework this instead?

BR,
Thinh

Quoted 7 lines

Hi Thinh,

We’re trying to see how this change could cause a regression for UAS devices.
Could you explain why the workaround might be a problem for UAS? Are you concerned that it could miss a valid StartTransfer when a previous transfer finishes later than expected as part of ep_disable?

Quoted 6 lines

In UAS, the device controller uses the first PRIME to synchronize with the host to determine whether the Start Transfer command can initiate the stream. After configuring an endpoint, if we issue the StartTransfer command too late, then the device controller may not initiate the transfer (sending ERDY), and host will not know when to start the transfer.

HI Thinh,

Sorry for the delayed response. We're seeing this issue a lot in Exynos platform, so we want to get it fixed.

Thanks for the explanation about UASP in last comment.

We agree that for UASP, the new StartTransfer sent in ep_enable() is what makes the controller send ERDY for the host's first PRIME. The old patch skipped that StartTransfer that was wrong. If it is skipped and no request is queued right away, the controller never sends ERDY for the first PRIME and the UAS device possible hangs as you mentioned.

The new patch is simpler, we won't skip it and just trigger it once the inflight end transfer is done.

Please see the proposed sequence,

  1. usb_ep_disable() --> EndTransfer deferred due to pending control
transfer data/status stages (DWC3_EP_DELAY_STOP), old transfer still active in HW.
  2. usb_ep_enable() on the same endpoint --> instead of issuing the
new StartTransfer, set DWC3_EP_PENDING_START_TRANSFER (New flag) as below,

  /* __dwc3_gadget_ep_enable() */
  if (dep->flags & (DWC3_EP_DELAY_STOP | DWC3_EP_END_TRANSFER_PENDING | DWC3_EP_TRANSFER_STARTED))
      dep->flags |= DWC3_EP_PENDING_START_TRANSFER;
  else
      ret = dwc3_gadget_ep_start_noop_transfer(dep);   /* unchanged path */

  3. Control request finishes --> next SETUP --> existing delayed stop
retry in dwc3_ep0_out_start() sends the EndTransfer for all delayed stop endpoints.
  4. EndTransfer completion and trigger a previously skipped
StartTransfer in ep_enable by checking DWC3_EP_PENDING_START_TRANSFER.

  /* dwc3_gadget_endpoint_command_complete() */ if (dep->flags & DWC3_EP_PENDING_START_TRANSFER) { dep->flags &= ~DWC3_EP_PENDING_START_TRANSFER; dwc3_gadget_ep_start_noop_transfer(dep); }

So the endpoint gets the same start/stop sequence you described, only sent later, it goes out as soon as the EndTransfer finishes. So this is high possible of happens before the host's first PRIME arrives. ERDY is still sent, no UAS regression.

The below testing log is for the reference. You can see the timestamp where pending start transfer triggered.

Quoted 9 lines
[ 3273.597476]  Entry __dwc3_gadget_ep_disable
[ 3273.597481]  dwc3_remove_requests ep1out skip stop transfer due to 
DWC3_EP_DELAY_STOP
[ 3273.597510]  Entry __dwc3_gadget_ep_enable 1045 dep->name =ep1out 
dep->flags =e009
[ 3273.597590]  dwc3_gadget_endpoint_command_complete 3857 dep->name 

=ep1out dep->flags =4009 --> Triggered skipped Start transfer when endpoint command completion is done.

Hi Selvarasu,

I just wanted to point out that dwc3_gadget_ep_start_noop_transfer() should only be needed for stream endpoints. I'll take a closer look at the rest of the changes later this week.

Regarding the original comment about avoiding the wait for command completion due to the No Response Update Transfer command does not provide any meaningful performance benefit since it only applies to the initial Start Transfer command.

Thanks,
Thinh

#9 lore raw
Quoted 225 lines · Thinh Nguyen
Quoted 215 lines · Selvarasu Ganesan
Quoted 155 lines · Thinh Nguyen
Quoted 147 lines · Selvarasu Ganesan
Quoted 141 lines · Thinh Nguyen
Quoted 128 lines · Thinh Nguyen
Quoted 127 lines · Selvarasu Ganesan

The below “No resource for ep” warning appears when a StartTransfer command is issued for bulk or interrupt endpoints in dwc3_gadget_ep_enable while a previous StartTransfer on the same endpoint is still in progress. The gadget functions drivers can invoke usb_ep_enable (which triggers a new StartTransfer command) before the earlier transfer has completed. Because the previous StartTransfer is still active, dwc3_gadget_ep_disable can skip the required EndTransfer due to DWC3_EP_DELAY_STOP, leading to the endpoint resources are busy for previous StartTransfer and warning ("No resource for ep") from dwc3 driver.

Additionally, a race condition exists between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue() when manipulating dep->flags. When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() runs in that window, it may set the DWC3_EP_TRANSFER_STARTED flag as part of dwc3_send_gadget_ep_cmd(). When ep_disable resumes, it unconditionally clears all flags except those explicitly masked, potentially clearing DWC3_EP_TRANSFER_STARTED even though a new transfer has started. This leads to "No resource for ep" warnings on subsequent StartTransfer attempts.

The underlying framework issue is that usb_ep_disable() is expected to complete pending requests before returning, but is allowed to be called from interrupt context where sleeping to wait for completion is not possible.

As temporary workarounds for this framework limitation:

1. In __dwc3_gadget_ep_enable(), add a check for the DWC3_EP_TRANSFER_STARTED flag before issuing a new StartTransfer. This prevents a second StartTransfer on an already busy endpoint, eliminating the resource conflict.

2. In __dwc3_gadget_ep_disable(), preserve the DWC3_EP_TRANSFER_STARTED flag when masking dep->flags if it is actually set, preventing the race with dwc3_gadget_ep_queue() from corrupting the flag state.

These changes eliminate the "No resource for ep" warnings and potential kernel panics caused by panic_on_warn.

dwc3 13200000.dwc3: No resource for ep1out

WARNING: CPU: 0 PID: 700 at drivers/usb/dwc3/gadget.c:398 dwc3_send_gadget_ep_cmd+0x2f8/0x76c
Call trace:
dwc3_send_gadget_ep_cmd+0x2f8/0x76c
__dwc3_gadget_ep_enable+0x490/0x7c0
dwc3_gadget_ep_enable+0x6c/0xe4
usb_ep_enable+0x5c/0x15c
mp_eth_stop+0xd4/0x11c
__dev_close_many+0x160/0x1c8
__dev_change_flags+0xfc/0x220
dev_change_flags+0x24/0x70
devinet_ioctl+0x434/0x524
inet_ioctl+0xa8/0x224
sock_do_ioctl+0x74/0x128
sock_ioctl+0x3bc/0x468
__arm64_sys_ioctl+0xa8/0xe4
invoke_syscall+0x58/0x10c
el0_svc_common+0xa8/0xdc
do_el0_svc+0x1c/0x28
el0_svc+0x38/0x88
el0t_64_sync_handler+0x70/0xbc
el0t_64_sync+0x1a8/0x1ac

---

Note: No Fixes tag is added because this is a workaround for the gadget framework issue where the gadget framework calls usb_ep_disable() in interrupt context without ensuring endpoint flushing completes. A proper fix requires refactoring the framework to make sure usb_ep_disable is invoked in process context.

Changes in v3:
- Revised the commit message to detail the real gadget framework issue
pointed out by the reviewer.
- Merged the two fixes for the same ep wringing into one patch.
Link to v2: https://protect2.fireeye.com/v1/url?k=dd1a51eb-82a25949-dd1bdaa4-000babff88b5-867f0f86789e003a&q=1&e=b9b4dadf-c903-42b6-bd70-3d09d4ad7c04&u=https%3A%2F%2Flore.kernel.org%2Flinux-usb%2F20251117155920.643-1-selvarasu.g%40samsung.com%2F

Changes in v2:
- Removed change-id.
- Updated commit message.
Link to v1: https://protect2.fireeye.com/v1/url?k=a7e00d4e-f85805ec-a7e18601-000babff88b5-fe0fb8c41d582e73&q=1&e=b9b4dadf-c903-42b6-bd70-3d09d4ad7c04&u=https%3A%2F%2Flore.kernel.org%2Flinux-usb%2F20251117152812.622-1-selvarasu.g%40samsung.com%2F

---
    drivers/usb/dwc3/gadget.c | 22 ++++++++++++++++++++--
    1 file changed, 20 insertions(+), 2 deletions(-)
drivers/usb/dwc3/gadget.c +20 -2
index 0a688904ce8c5..3af1bbfe3d92b 100644
@@ -971,8 +971,9 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
    	 * Issue StartTransfer here with no-op TRB so we can always rely on No
    	 * Response Update Transfer command.
    	 */
-	if (usb_endpoint_xfer_bulk(desc) ||
-			usb_endpoint_xfer_int(desc)) {
+	if ((usb_endpoint_xfer_bulk(desc) ||
+			usb_endpoint_xfer_int(desc)) &&
+			!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
    		struct dwc3_gadget_ep_cmd_params params;
    		struct dwc3_trb	*trb;
    		dma_addr_t trb_dma;
@@ -1096,6 +1097,23 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
    	 */
    	if (dep->flags & DWC3_EP_DELAY_STOP)
    		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+	/*
+	 * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(),
+	 * the dwc->lock is temporarily released. If dwc3_gadget_ep_queue()
+	 * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as
+	 * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+	 * unconditionally in the mask operation, which could overwrite the
+	 * concurrent modification.
+	 *
+	 * As a workaround for the interrupt context constraint where we cannot
+	 * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED
+	 * flag if it is set, avoiding resource conflicts until the framework
+	 * is fixed to properly synchronize endpoint lifecycle management.
+	 */
+	if (dep->flags & DWC3_EP_TRANSFER_STARTED)
+		mask |= DWC3_EP_TRANSFER_STARTED;
+
    	dep->flags &= mask;
 
Quoted 6 lines
Quoted 4 lines
drivers/usb/dwc3/gadget.c +0 -0
    	/* Clear out the ep descriptors for non-ep0 */
Signature · 1 line
2.34.1

Oh wait, don't pick this patch up yet.

This will cause a regression for UAS device. When switching alt-setting interface for BOT to UASP, the device needs to issue a Start Transfer command.

Quoted 5 lines

This workaround won't work. Can we fix the usb_ep_disable() interface and rework this instead?

BR,
Thinh

Quoted 7 lines

Hi Thinh,

We’re trying to see how this change could cause a regression for UAS devices.
Could you explain why the workaround might be a problem for UAS? Are you concerned that it could miss a valid StartTransfer when a previous transfer finishes later than expected as part of ep_disable?

Quoted 6 lines

In UAS, the device controller uses the first PRIME to synchronize with the host to determine whether the Start Transfer command can initiate the stream. After configuring an endpoint, if we issue the StartTransfer command too late, then the device controller may not initiate the transfer (sending ERDY), and host will not know when to start the transfer.

HI Thinh,

Sorry for the delayed response. We're seeing this issue a lot in Exynos platform, so we want to get it fixed.

Thanks for the explanation about UASP in last comment.

We agree that for UASP, the new StartTransfer sent in ep_enable() is what makes the controller send ERDY for the host's first PRIME. The old patch skipped that StartTransfer that was wrong. If it is skipped and no request is queued right away, the controller never sends ERDY for the first PRIME and the UAS device possible hangs as you mentioned.

The new patch is simpler, we won't skip it and just trigger it once the inflight end transfer is done.

Please see the proposed sequence,

  1. usb_ep_disable() --> EndTransfer deferred due to pending control
transfer data/status stages (DWC3_EP_DELAY_STOP), old transfer still active in HW.
  2. usb_ep_enable() on the same endpoint --> instead of issuing the
new StartTransfer, set DWC3_EP_PENDING_START_TRANSFER (New flag) as below,

    /* __dwc3_gadget_ep_enable() */
    if (dep->flags & (DWC3_EP_DELAY_STOP |
                      DWC3_EP_END_TRANSFER_PENDING |
                      DWC3_EP_TRANSFER_STARTED))
        dep->flags |= DWC3_EP_PENDING_START_TRANSFER;
    else
        ret = dwc3_gadget_ep_start_noop_transfer(dep);   /* unchanged path */

  3. Control request finishes --> next SETUP --> existing delayed stop
retry in dwc3_ep0_out_start() sends the EndTransfer for all delayed stop endpoints.
  4. EndTransfer completion and trigger a previously skipped
StartTransfer in ep_enable by checking DWC3_EP_PENDING_START_TRANSFER.

    /* dwc3_gadget_endpoint_command_complete() */
    if (dep->flags & DWC3_EP_PENDING_START_TRANSFER) {
        dep->flags &= ~DWC3_EP_PENDING_START_TRANSFER;
        dwc3_gadget_ep_start_noop_transfer(dep);
    }

So the endpoint gets the same start/stop sequence you described, only sent later, it goes out as soon as the EndTransfer finishes. So this is high possible of happens before the host's first PRIME arrives. ERDY is still sent, no UAS regression.

The below testing log is for the reference. You can see the timestamp where pending start transfer triggered.

Quoted 9 lines
[ 3273.597476]  Entry __dwc3_gadget_ep_disable
[ 3273.597481]  dwc3_remove_requests ep1out skip stop transfer due to
DWC3_EP_DELAY_STOP
[ 3273.597510]  Entry __dwc3_gadget_ep_enable 1045 dep->name =ep1out
dep->flags =e009
[ 3273.597590]  dwc3_gadget_endpoint_command_complete 3857 dep->name

=ep1out dep->flags =4009 --> Triggered skipped Start transfer when endpoint command completion is done.

Quoted 5 lines

Hi Selvarasu,

I just wanted to point out that dwc3_gadget_ep_start_noop_transfer() should only be needed for stream endpoints. I'll take a closer look at the rest of the changes later this week.

Hi Thinh,

Thanks for you feedback.

Agreed, The dwc3_gadget_ep_start_noop_transfer() should only be needed for stream endpoints.

Thanks,
Selva

Quoted 8 lines

Regarding the original comment about avoiding the wait for command completion due to the No Response Update Transfer command does not provide any meaningful performance benefit since it only applies to the initial Start Transfer command.

Thanks,
Thinh

maintainer
#10 lore raw
Quoted 70 lines · Selvarasu Ganesan
Quoted 63 lines · Thinh Nguyen
Quoted 53 lines · Selvarasu Ganesan

HI Thinh,

Sorry for the delayed response. We're seeing this issue a lot in Exynos platform, so we want to get it fixed.

Thanks for the explanation about UASP in last comment.

We agree that for UASP, the new StartTransfer sent in ep_enable() is what makes the controller send ERDY for the host's first PRIME. The old patch skipped that StartTransfer that was wrong. If it is skipped and no request is queued right away, the controller never sends ERDY for the first PRIME and the UAS device possible hangs as you mentioned.

The new patch is simpler, we won't skip it and just trigger it once the inflight end transfer is done.

Please see the proposed sequence,

  1. usb_ep_disable() --> EndTransfer deferred due to pending control
transfer data/status stages (DWC3_EP_DELAY_STOP), old transfer still active in HW.
  2. usb_ep_enable() on the same endpoint --> instead of issuing the
new StartTransfer, set DWC3_EP_PENDING_START_TRANSFER (New flag) as below,

    /* __dwc3_gadget_ep_enable() */
    if (dep->flags & (DWC3_EP_DELAY_STOP |
                      DWC3_EP_END_TRANSFER_PENDING |
                      DWC3_EP_TRANSFER_STARTED))
        dep->flags |= DWC3_EP_PENDING_START_TRANSFER;
    else
        ret = dwc3_gadget_ep_start_noop_transfer(dep);   /* unchanged path */

  3. Control request finishes --> next SETUP --> existing delayed stop
retry in dwc3_ep0_out_start() sends the EndTransfer for all delayed stop endpoints.
  4. EndTransfer completion and trigger a previously skipped
StartTransfer in ep_enable by checking DWC3_EP_PENDING_START_TRANSFER.

    /* dwc3_gadget_endpoint_command_complete() */
    if (dep->flags & DWC3_EP_PENDING_START_TRANSFER) {
        dep->flags &= ~DWC3_EP_PENDING_START_TRANSFER;
        dwc3_gadget_ep_start_noop_transfer(dep);
    }

So the endpoint gets the same start/stop sequence you described, only sent later, it goes out as soon as the EndTransfer finishes. So this is high possible of happens before the host's first PRIME arrives. ERDY is still sent, no UAS regression.

The below testing log is for the reference. You can see the timestamp where pending start transfer triggered.

Quoted 9 lines
[ 3273.597476]  Entry __dwc3_gadget_ep_disable
[ 3273.597481]  dwc3_remove_requests ep1out skip stop transfer due to
DWC3_EP_DELAY_STOP
[ 3273.597510]  Entry __dwc3_gadget_ep_enable 1045 dep->name =ep1out
dep->flags =e009
[ 3273.597590]  dwc3_gadget_endpoint_command_complete 3857 dep->name

=ep1out dep->flags =4009 --> Triggered skipped Start transfer when endpoint command completion is done.

Quoted 5 lines

Hi Selvarasu,

I just wanted to point out that dwc3_gadget_ep_start_noop_transfer() should only be needed for stream endpoints. I'll take a closer look at the rest of the changes later this week.

Quoted 7 lines

Hi Thinh,

Thanks for you feedback.

Agreed, The dwc3_gadget_ep_start_noop_transfer() should only be needed for stream endpoints.

Hi,

Can you try the below instead.

Thanks,
Thinh

drivers/usb/dwc3/core.h +3 -1
index 608daeb7ef10..093627a6a409 100644
@@ -770,6 +770,7 @@ struct dwc3_ep {
 #define DWC3_EP_TXFIFO_RESIZED		BIT(12)
 #define DWC3_EP_DELAY_STOP             BIT(13)
 #define DWC3_EP_RESOURCE_ALLOCATED	BIT(14)
+#define DWC3_EP_PENDING_NOOP_START	BIT(15)
 
 	/* This last one is specific to EP0 */
 #define DWC3_EP0_DIR_IN			BIT(31)
@@ -1676,7 +1677,8 @@ int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
 		u32 param);
 void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc);
-void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status);
+void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status,
+			  bool interrupt);
 #else
 static inline int dwc3_gadget_init(struct dwc3 *dwc)
 { return 0; }
drivers/usb/dwc3/ep0.c +1 -1
index 310b5ffb236a..ff80c3a0c983 100644
@@ -306,7 +306,7 @@ void dwc3_ep0_out_start(struct dwc3 *dwc)
 		if (dwc->connected)
 			dwc3_stop_active_transfer(dwc3_ep, false, true);
 		else
-			dwc3_remove_requests(dwc, dwc3_ep, -ESHUTDOWN);
+			dwc3_remove_requests(dwc, dwc3_ep, -ESHUTDOWN, false);
 	}
 }
 
drivers/usb/dwc3/gadget.c +87 -55

199 lines not on this page · raw message

#11 lore raw
Quoted 291 lines · Thinh Nguyen
Quoted 68 lines · Selvarasu Ganesan
Quoted 62 lines · Thinh Nguyen
Quoted 52 lines · Selvarasu Ganesan

HI Thinh,

Sorry for the delayed response. We're seeing this issue a lot in Exynos platform, so we want to get it fixed.

Thanks for the explanation about UASP in last comment.

We agree that for UASP, the new StartTransfer sent in ep_enable() is what makes the controller send ERDY for the host's first PRIME. The old patch skipped that StartTransfer that was wrong. If it is skipped and no request is queued right away, the controller never sends ERDY for the first PRIME and the UAS device possible hangs as you mentioned.

The new patch is simpler, we won't skip it and just trigger it once the inflight end transfer is done.

Please see the proposed sequence,

  1. usb_ep_disable() --> EndTransfer deferred due to pending control
transfer data/status stages (DWC3_EP_DELAY_STOP), old transfer still active in HW.
  2. usb_ep_enable() on the same endpoint --> instead of issuing the
new StartTransfer, set DWC3_EP_PENDING_START_TRANSFER (New flag) as below,

     /* __dwc3_gadget_ep_enable() */
     if (dep->flags & (DWC3_EP_DELAY_STOP |
                       DWC3_EP_END_TRANSFER_PENDING |
                       DWC3_EP_TRANSFER_STARTED))
         dep->flags |= DWC3_EP_PENDING_START_TRANSFER;
     else
         ret = dwc3_gadget_ep_start_noop_transfer(dep);   /* unchanged path */

  3. Control request finishes --> next SETUP --> existing delayed stop
retry in dwc3_ep0_out_start() sends the EndTransfer for all delayed stop endpoints.
  4. EndTransfer completion and trigger a previously skipped
StartTransfer in ep_enable by checking DWC3_EP_PENDING_START_TRANSFER.

     /* dwc3_gadget_endpoint_command_complete() */
     if (dep->flags & DWC3_EP_PENDING_START_TRANSFER) {
         dep->flags &= ~DWC3_EP_PENDING_START_TRANSFER;
         dwc3_gadget_ep_start_noop_transfer(dep);
     }

So the endpoint gets the same start/stop sequence you described, only sent later, it goes out as soon as the EndTransfer finishes. So this is high possible of happens before the host's first PRIME arrives. ERDY is still sent, no UAS regression.

The below testing log is for the reference. You can see the timestamp where pending start transfer triggered.

Quoted 9 lines
[ 3273.597476]  Entry __dwc3_gadget_ep_disable
[ 3273.597481]  dwc3_remove_requests ep1out skip stop transfer due to
DWC3_EP_DELAY_STOP
[ 3273.597510]  Entry __dwc3_gadget_ep_enable 1045 dep->name =ep1out
dep->flags =e009
[ 3273.597590]  dwc3_gadget_endpoint_command_complete 3857 dep->name

=ep1out dep->flags =4009 --> Triggered skipped Start transfer when endpoint command completion is done.

Quoted 5 lines

Hi Selvarasu,

I just wanted to point out that dwc3_gadget_ep_start_noop_transfer() should only be needed for stream endpoints. I'll take a closer look at the rest of the changes later this week.

Quoted 7 lines

Hi Thinh,

Thanks for you feedback.

Agreed, The dwc3_gadget_ep_start_noop_transfer() should only be needed for stream endpoints.

Hi,

Can you try the below instead.

Thanks,
Thinh

drivers/usb/dwc3/core.h +3 -1
index 608daeb7ef10..093627a6a409 100644
@@ -770,6 +770,7 @@ struct dwc3_ep {
  #define DWC3_EP_TXFIFO_RESIZED		BIT(12)
  #define DWC3_EP_DELAY_STOP             BIT(13)
  #define DWC3_EP_RESOURCE_ALLOCATED	BIT(14)
+#define DWC3_EP_PENDING_NOOP_START	BIT(15)
 
  	/* This last one is specific to EP0 */
  #define DWC3_EP0_DIR_IN			BIT(31)
@@ -1676,7 +1677,8 @@ int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
  int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
  		u32 param);
  void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc);
-void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status);
+void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status,
+			  bool interrupt);
  #else
  static inline int dwc3_gadget_init(struct dwc3 *dwc)
  { return 0; }
drivers/usb/dwc3/ep0.c +1 -1
index 310b5ffb236a..ff80c3a0c983 100644
@@ -306,7 +306,7 @@ void dwc3_ep0_out_start(struct dwc3 *dwc)
  		if (dwc->connected)
  			dwc3_stop_active_transfer(dwc3_ep, false, true);
  		else
-			dwc3_remove_requests(dwc, dwc3_ep, -ESHUTDOWN);
+			dwc3_remove_requests(dwc, dwc3_ep, -ESHUTDOWN, false);
  	}
  }
 
drivers/usb/dwc3/gadget.c +74 -51
index f245e66cd13d..4381a9f60f60 100644
@@ -906,6 +906,54 @@ static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
  	return 0;
  }
 
+static int dwc3_gadget_ep_start_noop_transfer(struct dwc3_ep *dep)
+{
+	struct dwc3_gadget_ep_cmd_params params;
+	struct dwc3		*dwc = dep->dwc;
+	struct dwc3_trb		*trb;
+	dma_addr_t		trb_dma;
+	u32			cmd;
+	int			ret;
+
+	memset(&params, 0, sizeof(params));
+	trb = &dep->trb_pool[0];
+	trb_dma = dwc3_trb_dma_offset(dep, trb);
+
+	params.param0 = upper_32_bits(trb_dma);
+	params.param1 = lower_32_bits(trb_dma);
+
+	cmd = DWC3_DEPCMD_STARTTRANSFER;
+
+	ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * For streams, at start, there maybe a race where the host primes the
+	 * endpoint before the function driver queues a request to initiate a
+	 * stream. In that case, the controller will not see the prime to
+	 * generate the ERDY and start stream. To workaround this, issue a
+	 * no-op TRB as normal, but end it immediately. As a result, when the
+	 * function driver queues the request, the next START_TRANSFER command
+	 * will cause the controller to generate an ERDY to initiate the
+	 * stream.
+	 */
+	dwc3_stop_active_transfer(dep, false, true);
+
+	/*
+	 * All stream eps will reinitiate stream on NoStream rejection.
+	 *
+	 * However, if the controller is capable of TXF_FLUSH_BYPASS, then IN
+	 * direction endpoints will automatically restart the stream without
+	 * the driver initiation.
+	 */
+	if (!dep->direction ||
+	    !(dwc->hwparams.hwparams9 & DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
+		dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
+
+	return 0;
+}
+
  /**
   * __dwc3_gadget_ep_enable - initializes a hw endpoint
   * @dep: endpoint to be initialized
@@ -971,54 +1019,15 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
  	 * Issue StartTransfer here with no-op TRB so we can always rely on No
  	 * Response Update Transfer command.
  	 */
-	if (usb_endpoint_xfer_bulk(desc) ||
-			usb_endpoint_xfer_int(desc)) {
-		struct dwc3_gadget_ep_cmd_params params;
-		struct dwc3_trb	*trb;
-		dma_addr_t trb_dma;
-		u32 cmd;
-
-		memset(&params, 0, sizeof(params));
-		trb = &dep->trb_pool[0];
-		trb_dma = dwc3_trb_dma_offset(dep, trb);
-
-		params.param0 = upper_32_bits(trb_dma);
-		params.param1 = lower_32_bits(trb_dma);
-
-		cmd = DWC3_DEPCMD_STARTTRANSFER;
-
-		ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
-		if (ret < 0)
-			return ret;
-
-		if (dep->stream_capable) {
-			/*
-			 * For streams, at start, there maybe a race where the
-			 * host primes the endpoint before the function driver
-			 * queues a request to initiate a stream. In that case,
-			 * the controller will not see the prime to generate the
-			 * ERDY and start stream. To workaround this, issue a
-			 * no-op TRB as normal, but end it immediately. As a
-			 * result, when the function driver queues the request,
-			 * the next START_TRANSFER command will cause the
-			 * controller to generate an ERDY to initiate the
-			 * stream.
-			 */
-			dwc3_stop_active_transfer(dep, false, true);
-
-			/*
-			 * All stream eps will reinitiate stream on NoStream
-			 * rejection.
-			 *
-			 * However, if the controller is capable of
-			 * TXF_FLUSH_BYPASS, then IN direction endpoints will
-			 * automatically restart the stream without the driver
-			 * initiation.
-			 */
-			if (!dep->direction ||
-			    !(dwc->hwparams.hwparams9 &
-			      DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
-				dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
+	if (dep->stream_capable) {
+		/* Defer if a previous transfer is still being retired */
+		if (dep->flags & (DWC3_EP_END_TRANSFER_PENDING |
+				  DWC3_EP_DELAY_STOP)) {
+			dep->flags |= DWC3_EP_PENDING_NOOP_START;
+		} else {
+			ret = dwc3_gadget_ep_start_noop_transfer(dep);
+			if (ret < 0)
+				return ret;
  		}
  	}
 
@@ -1028,16 +1037,21 @@ static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
  	return 0;
  }
 
-void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status)
+void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status,
+			  bool interrupt)
  {
  	struct dwc3_request		*req;
 
-	dwc3_stop_active_transfer(dep, false, false);
+	dwc3_stop_active_transfer(dep, false, interrupt);
 
  	/* If endxfer is delayed, avoid unmapping requests */
  	if (dep->flags & DWC3_EP_DELAY_STOP)
  		return;
 
+	/* Give the controller time to stop accessing the request buffers */
+	if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
+		mdelay(1);
+
  	/* - giveback all requests to gadget driver */
  	while (!list_empty(&dep->started_list)) {
  		req = next_request(&dep->started_list);
@@ -1084,7 +1098,7 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  	reg &= ~DWC3_DALEPENA_EP(dep->number);
  	dwc3_writel(dwc, DWC3_DALEPENA, reg);
 
-	dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
+	dwc3_remove_requests(dwc, dep, -ESHUTDOWN, true);
 
  	dep->stream_capable = false;
  	dep->type = 0;
@@ -1096,6 +1110,15 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  	 */
  	if (dep->flags & DWC3_EP_DELAY_STOP)
  		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+	/*
+	 * The End Transfer command is still in progress.  Do not clear the
+	 * flags, so that the ep is only rearmed once the command completes.
+	 */
+	if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
+		mask |= (DWC3_EP_END_TRANSFER_PENDING |
+			 DWC3_EP_TRANSFER_STARTED);
+
  	dep->flags &= mask;
 
  	/* Clear out the ep descriptors for non-ep0 */

drivers/usb/dwc3/gadget.c static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int · line 1818

Quoted 5 lines
drivers/usb/dwc3/gadget.c +1 -1
@@ -1792,9 +1815,9 @@ static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int
 
  	dep->resource_index = 0;
 
-	if (!interrupt)
+	if (!interrupt || ret)

Hi Thinh,

Thanks for your code changes. The given code changes look good to me and are working as expected.

There is one more concern about an uncovered endpoint resource failure in some corner cases where END TRANSFER timeout is observed (ret = -110).

The sequence is explained below,

Step 1:
 __dwc3_gadget_ep_set_halt(dep, value=0) ->dwc3_stop_active_transfer(dep, true, true) ->END(e.g, ep2out, resource_index=7) issued, but timeout occurs -> resource_index cleared to 0 ->dwc3_send_clear_stall_ep_cmd(dep)
    -> failed to clear STALL on ep2out

Step 2:
__dwc3_gadget_ep_set_halt(dep, value=0) ->Re triggered clear stall for same EP
 -> dwc3_stop_active_transfer(dep, true, true)
   -> END(ep2out, resource_index=0) issued (wrong resource!) -> Command succeeds, DWC3_EP_TRANSFER_STARTED cleared in
completion handler

Step 3:
usb_ep_queue()
 -> dwc3_gadget_ep_queue()
   -> __dwc3_gadget_kick_transfer()
     -> starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED)  -> starting=0 -> Issues STARTTRANSFER (because DWC3_EP_TRANSFER_STARTED is not
set)
         -> Hardware rejects with NO_RESOURCE (resource 7 still held)

Could you please give your suggestions on this issue case?

Thanks,

Selva

Quoted 33 lines
drivers/usb/dwc3/gadget.c +12 -3
  		dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
-	else if (!ret)
+	else
  		dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
 
  	dep->flags &= ~DWC3_EP_DELAY_STOP;
@@ -2523,7 +2546,7 @@ static void dwc3_stop_active_transfers(struct dwc3 *dwc)
  		if (!dep)
  			continue;
 
-		dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
+		dwc3_remove_requests(dwc, dep, -ESHUTDOWN, false);
  	}
  }
 
@@ -3867,7 +3890,16 @@ static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
  			dwc3_ep0_send_delayed_status(dwc);
  	}
 
-	if ((dep->flags & DWC3_EP_DELAY_START) &&
+	if (dep->flags & DWC3_EP_PENDING_NOOP_START) {
+		dep->flags &= ~DWC3_EP_PENDING_NOOP_START;
+
+		/* A delayed kick will arm the stream with a real TRB instead */
+		if (!(dep->flags & DWC3_EP_DELAY_START) &&
+		    (dep->flags & DWC3_EP_ENABLED) && dep->endpoint.desc)
+			dwc3_gadget_ep_start_noop_transfer(dep);
+	}
+
+	if ((dep->flags & DWC3_EP_DELAY_START) && dep->endpoint.desc &&
  	    !usb_endpoint_xfer_isoc(dep->endpoint.desc))
  		__dwc3_gadget_kick_transfer(dep);
 
maintainer
#12 lore raw
Quoted 15 lines · Selvarasu Ganesan

Review static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int · line 1818

Quoted 8 lines

/* Clear out the ep descriptors for non-ep0 */

(quoted diff) +1 -1
@@ -1792,9 +1815,9 @@ static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int
 
  	dep->resource_index = 0;
 
-	if (!interrupt)
+	if (!interrupt || ret)

Hi Thinh,

Thanks for your code changes. The given code changes look good to me and are working as expected.

Quoted 8 lines

There is one more concern about an uncovered endpoint resource failure in some corner cases where END TRANSFER timeout is observed (ret = -110).

The sequence is explained below,

Step 1:
 __dwc3_gadget_ep_set_halt(dep, value=0)
  ->dwc3_stop_active_transfer(dep, true, true)

Looks like you still do ForceRM=true. Can you apply this patch:

b58e6200450d ("usb: dwc3: clear forceRM when issuing EndTransfer")

Quoted 24 lines

    ->END(e.g, ep2out, resource_index=7) issued, but timeout occurs -> resource_index cleared to 0 ->dwc3_send_clear_stall_ep_cmd(dep)
    -> failed to clear STALL on ep2out

Step 2:
__dwc3_gadget_ep_set_halt(dep, value=0) ->Re triggered clear stall for same EP
 -> dwc3_stop_active_transfer(dep, true, true)
   -> END(ep2out, resource_index=0) issued (wrong resource!) -> Command succeeds, DWC3_EP_TRANSFER_STARTED cleared in
completion handler

Step 3:
usb_ep_queue()
 -> dwc3_gadget_ep_queue()
   -> __dwc3_gadget_kick_transfer()
     -> starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED)  -> starting=0 -> Issues STARTTRANSFER (because DWC3_EP_TRANSFER_STARTED is not
set)
         -> Hardware rejects with NO_RESOURCE (resource 7 still held)

Quoted 2 lines

Could you please give your suggestions on this issue case?

Thanks for testing. Can you check whether the End Transfer command ever completes with the endpoint completion event after the -ETIMEDOUT error? Try with the below.

Thanks,
Thinh

drivers/usb/dwc3/gadget.c +2 -1
index 1f973e546219..bac0328423cd 100644
@@ -1819,7 +1819,11 @@ static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int
 
 	dep->resource_index = 0;
 
-	if (!interrupt || ret)
+	if (!interrupt || (ret && ret != -ETIMEDOUT))
 		dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
 	else
 		dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
@@ -3895,6 +3899,8 @@ static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
 	if (dep->stream_capable)
 		dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
 
+	dep->flags &= ~DWC3_EP_DELAY_STOP;
 	dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
 	dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
 	dwc3_gadget_ep_cleanup_cancelled_requests(dep);
#13 lore raw
Quoted 17 lines · Thinh Nguyen
Quoted 16 lines · Selvarasu Ganesan

drivers/usb/dwc3/gadget.c static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int · line 1818

Quoted 7 lines
   	/* Clear out the ep descriptors for non-ep0 */
drivers/usb/dwc3/gadget.c +1 -1
@@ -1792,9 +1815,9 @@ static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int
 
   	dep->resource_index = 0;
 
-	if (!interrupt)
+	if (!interrupt || ret)

Hi Thinh,

Thanks for your code changes. The given code changes look good to me and are working as expected.

There is one more concern about an uncovered endpoint resource failure in some corner cases where END TRANSFER timeout is observed (ret = -110).

Quoted 8 lines
Quoted 5 lines

The sequence is explained below,

Step 1:
 __dwc3_gadget_ep_set_halt(dep, value=0)
  ->dwc3_stop_active_transfer(dep, true, true)

Looks like you still do ForceRM=true. Can you apply this patch:

b58e6200450d ("usb: dwc3: clear forceRM when issuing EndTransfer")

We have this fix in our dwc3 driver code. but still observing EP end transfer timeout.

Quoted 25 lines
Quoted 25 lines

    ->END(e.g, ep2out, resource_index=7) issued, but timeout occurs -> resource_index cleared to 0 ->dwc3_send_clear_stall_ep_cmd(dep)
    -> failed to clear STALL on ep2out

Step 2:
__dwc3_gadget_ep_set_halt(dep, value=0) ->Re triggered clear stall for same EP
 -> dwc3_stop_active_transfer(dep, true, true)
   -> END(ep2out, resource_index=0) issued (wrong resource!) -> Command succeeds, DWC3_EP_TRANSFER_STARTED cleared in
completion handler

Step 3:
usb_ep_queue()
 -> dwc3_gadget_ep_queue()
   -> __dwc3_gadget_kick_transfer()
     -> starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED)  -> starting=0 -> Issues STARTTRANSFER (because DWC3_EP_TRANSFER_STARTED is not
set)
         -> Hardware rejects with NO_RESOURCE (resource 7 still held)

Quoted 4 lines
Quoted 2 lines

Could you please give your suggestions on this issue case?

Thanks for testing. Can you check whether the End Transfer command ever completes with the endpoint completion event after the -ETIMEDOUT error?

No, the endpoint completion event is not seen after the -ETIMEDOUT error.

The proposed fix works well for the __dwc3_gadget_ep_set_halt sequence, where DWC3_EP_END_TRANSFER_PENDING must be set to prevent dwc3_ep_queue from starting a new transfer during a EP transfer timeout.

But, this is unnecessary for __dwc3_gadget_ep_disable. Since there's no way to clear the pending flag if the interrupt is missed and no dwc3_ep_queue calls occur until the EP is re-enabled, preserving DWC3_EP_END_TRANSFER_PENDING here provides no benefit.
So, the below changes is not necessary in ep disable,

@@ -1096,6 +1110,15 @@ static int __dwc3_gadget_ep_disable(struct
dwc3_ep *dep) */
if (dep->flags & DWC3_EP_DELAY_STOP)
mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+ + /* + * The End Transfer command is still in progress. Do not clear the + * flags, so that the ep is only rearmed once the command completes. + */ + if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) + mask |= (DWC3_EP_END_TRANSFER_PENDING | + DWC3_EP_TRANSFER_STARTED); +

Instead, keep our suggestion changes that prevent the manipulation of dep->flags due to the race condition between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue(), since this race observing in long run rndis test.

+ + /* + * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), +
* the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() + * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as + * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag + * unconditionally in the mask operation, which could overwrite the + * concurrent modification. + * + * As a workaround for the interrupt context constraint where we cannot + * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED + * flag if it is set, avoiding resource conflicts until the framework + * is fixed to properly synchronize endpoint lifecycle management. + */ + if (dep->flags & DWC3_EP_TRANSFER_STARTED) + mask |= DWC3_EP_TRANSFER_STARTED; +

Thanks,
Selva

Quoted 26 lines

Try with the below.

Thanks,
Thinh

drivers/usb/dwc3/gadget.c +2 -1
index 1f973e546219..bac0328423cd 100644
@@ -1819,7 +1819,11 @@ static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int
 
  	dep->resource_index = 0;
 
-	if (!interrupt || ret)
+	if (!interrupt || (ret && ret != -ETIMEDOUT))
  		dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
  	else
  		dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
@@ -3895,6 +3899,8 @@ static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
  	if (dep->stream_capable)
  		dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
 
+	dep->flags &= ~DWC3_EP_DELAY_STOP;
  	dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
  	dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
  	dwc3_gadget_ep_cleanup_cancelled_requests(dep);
#14 lore raw
Quoted 97 lines · Selvarasu Ganesan
Quoted 16 lines · Thinh Nguyen
Quoted 15 lines · Selvarasu Ganesan

drivers/usb/dwc3/gadget.c static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int · line 1818

Quoted 7 lines
    	/* Clear out the ep descriptors for non-ep0 */
drivers/usb/dwc3/gadget.c +1 -1
@@ -1792,9 +1815,9 @@ static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int
 
    	dep->resource_index = 0;
 
-	if (!interrupt)
+	if (!interrupt || ret)

Hi Thinh,

Thanks for your code changes. The given code changes look good to me and are working as expected.

There is one more concern about an uncovered endpoint resource failure in some corner cases where END TRANSFER timeout is observed (ret = -110).

Quoted 8 lines
Quoted 5 lines

The sequence is explained below,

Step 1:

    __dwc3_gadget_ep_set_halt(dep, value=0)
     ->dwc3_stop_active_transfer(dep, true, true)

Looks like you still do ForceRM=true. Can you apply this patch:

b58e6200450d ("usb: dwc3: clear forceRM when issuing EndTransfer")

We have this fix in our dwc3 driver code. but still observing EP end transfer timeout.

Quoted 24 lines
Quoted 24 lines
       ->END(e.g, ep2out, resource_index=7) issued, but timeout occurs
        -> resource_index cleared to 0
     ->dwc3_send_clear_stall_ep_cmd(dep)
       -> failed to clear STALL on ep2out

Step 2:
__dwc3_gadget_ep_set_halt(dep, value=0) ->Re triggered clear stall for same EP
 -> dwc3_stop_active_transfer(dep, true, true)
   -> END(ep2out, resource_index=0) issued (wrong resource!) -> Command succeeds, DWC3_EP_TRANSFER_STARTED cleared in
completion handler

Step 3:
usb_ep_queue()
 -> dwc3_gadget_ep_queue()
   -> __dwc3_gadget_kick_transfer()
     -> starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED)  -> starting=0 -> Issues STARTTRANSFER (because DWC3_EP_TRANSFER_STARTED is not
set)
         -> Hardware rejects with NO_RESOURCE (resource 7 still held)

Quoted 4 lines
Quoted 2 lines

Could you please give your suggestions on this issue case?

Thanks for testing. Can you check whether the End Transfer command ever completes with the endpoint completion event after the -ETIMEDOUT error?

No, the endpoint completion event is not seen after the -ETIMEDOUT error.

The proposed fix works well for the __dwc3_gadget_ep_set_halt sequence, where DWC3_EP_END_TRANSFER_PENDING must be set to prevent dwc3_ep_queue from starting a new transfer during a EP transfer timeout.

But, this is unnecessary for __dwc3_gadget_ep_disable. Since there's no way to clear the pending flag if the interrupt is missed and no dwc3_ep_queue calls occur until the EP is re-enabled, preserving DWC3_EP_END_TRANSFER_PENDING here provides no benefit.
So, the below changes is not necessary in ep disable,

(quoted diff) +0 -0
@@ -1096,6 +1110,15 @@ static int __dwc3_gadget_ep_disable(struct

dwc3_ep *dep) */
if (dep->flags & DWC3_EP_DELAY_STOP)
mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+ + /* + * The End Transfer command is still in progress. Do not clear the + * flags, so that the ep is only rearmed once the command completes. + */ + if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) + mask |= (DWC3_EP_END_TRANSFER_PENDING | + DWC3_EP_TRANSFER_STARTED); +

Instead, keep our suggestion changes that prevent the manipulation of dep->flags due to the race condition between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue(), since this race observing in long run rndis test.

+ + /* + * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), +
* the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() + * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as + * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag + * unconditionally in the mask operation, which could overwrite the + * concurrent modification. + * + * As a workaround for the interrupt context constraint where we cannot + * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED + * flag if it is set, avoiding resource conflicts until the framework + * is fixed to properly synchronize endpoint lifecycle management. + */ + if (dep->flags & DWC3_EP_TRANSFER_STARTED) + mask |= DWC3_EP_TRANSFER_STARTED; +

Quoted 2 lines

Thanks,
Selva

Sorry for the format issue. The updated answers as below,

No, the endpoint completion event is not seen after the -ETIMEDOUT error.

The proposed fix works well for the __dwc3_gadget_ep_set_halt sequence, where DWC3_EP_END_TRANSFER_PENDING must be set to prevent dwc3_ep_queue from starting a new transfer during a EP transfer timeout.

But, this is unnecessary for __dwc3_gadget_ep_disable. Since there's no way to clear the pending flag if the interrupt is missed and no dwc3_ep_queue calls occur until the EP is re-enabled, preserving DWC3_EP_END_TRANSFER_PENDING here provides no benefit.

So, the below changes is not necessary in ep disable,

@@ -1096,6 +1144,15 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
         */
        if (dep->flags & DWC3_EP_DELAY_STOP)
                mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+       /*
+        * The End Transfer command is still in progress.  Do not clear the +        * flags, so that the ep is only rearmed once the command completes. +        */
+       if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
+               mask |= (DWC3_EP_END_TRANSFER_PENDING |
+ DWC3_EP_TRANSFER_STARTED);
+

Instead, keep our suggestion changes that prevent the manipulation of dep->flags due to the race condition between dwc3_gadget_ep_disable() and dwc3_gadget_ep_queue(), since this race observing in long run rndis test.

+
+       /*
+        * When dwc3_gadget_ep_disable() calls dwc3_gadget_giveback(), +        * the dwc->lock is temporarily released. If dwc3_gadget_ep_queue() +        * runs in that window it may set the DWC3_EP_TRANSFER_STARTED flag as
+        * part of dwc3_send_gadget_ep_cmd. The original code cleared the flag
+        * unconditionally in the mask operation, which could overwrite the +        * concurrent modification.
+        *
+        * As a workaround for the interrupt context constraint where we cannot
+        * wait for endpoint flushing, preserve the DWC3_EP_TRANSFER_STARTED +        * flag if it is set, avoiding resource conflicts until the framework
+        * is fixed to properly synchronize endpoint lifecycle management. +        */
+       if (dep->flags & DWC3_EP_TRANSFER_STARTED)
+               mask |= DWC3_EP_TRANSFER_STARTED;
+

Thanks,
Selva

Quoted 26 lines
Quoted 26 lines

Try with the below.

Thanks,
Thinh

drivers/usb/dwc3/gadget.c +2 -1
index 1f973e546219..bac0328423cd 100644
@@ -1819,7 +1819,11 @@ static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool int
 
   	dep->resource_index = 0;
 
-	if (!interrupt || ret)
+	if (!interrupt || (ret && ret != -ETIMEDOUT))
   		dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
   	else
   		dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
@@ -3895,6 +3899,8 @@ static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
   	if (dep->stream_capable)
   		dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
 
+	dep->flags &= ~DWC3_EP_DELAY_STOP;
   	dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
   	dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
   	dwc3_gadget_ep_cleanup_cancelled_requests(dep);
maintainer
#15 lore raw
Quoted 9 lines · Selvarasu Ganesan

Sorry for the format issue. The updated answers as below,

No, the endpoint completion event is not seen after the -ETIMEDOUT error.

The proposed fix works well for the __dwc3_gadget_ep_set_halt sequence, where DWC3_EP_END_TRANSFER_PENDING must be set to prevent dwc3_ep_queue from starting a new transfer during a EP transfer timeout.

Quoted 7 lines

But, this is unnecessary for __dwc3_gadget_ep_disable. Since there's no way to clear the pending flag if the interrupt is missed and no dwc3_ep_queue calls occur until the EP is re-enabled, preserving DWC3_EP_END_TRANSFER_PENDING here provides no benefit.

So, the below changes is not necessary in ep disable,

We still need to keep DWC3_EP_END_TRANSFER_PENDING in ep_disable. That's for the normal case where the End Transfer completes after ep_disable returns, which is the original issue. If the command never completes, the endpoint resource is stuck regardless. Clearing the flag only lead to a NO_RESOURCE error later.

As for the dwc3_gadget_ep_queue() race during giveback, keeping DWC3_EP_TRANSFER_STARTED isn't right. We should reject the queue when the endpoint is disabled. This should be a separate patch:

drivers/usb/dwc3/gadget.c +4 -1
index ee837235630a..eb6666b7bb98 100644
@@ -1084,6 +1084,8 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
 	reg &= ~DWC3_DALEPENA_EP(dep->number);
 	dwc3_writel(dwc, DWC3_DALEPENA, reg);
 
+	dep->flags &= ~DWC3_EP_ENABLED;
+
 	dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
 
 	dep->stream_capable = false;
@@ -1990,7 +1992,8 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
 {
 	struct dwc3		*dwc = dep->dwc;
 
-	if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
+	if (!(dep->flags & DWC3_EP_ENABLED) || !dep->endpoint.desc ||
+	    !dwc->pullups_connected || !dwc->connected) {
 		dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n",
 				dep->name);
 		return -ESHUTDOWN;

The End Transfer command not completing is also a separate issue. I asked you to check whether the command would eventually complete with the additional code, and it appears it doesn't.

Can you provide some more info on your setup:
* IP and version
* connected speed
* endpoint direction (is it always OUT?)
* Is there any active transfer
* tracepoints

Thanks,
Thinh

#16 lore raw
Quoted 14 lines · Thinh Nguyen
Quoted 13 lines · Selvarasu Ganesan

Sorry for the format issue. The updated answers as below,

No, the endpoint completion event is not seen after the -ETIMEDOUT error.

The proposed fix works well for the __dwc3_gadget_ep_set_halt sequence, where DWC3_EP_END_TRANSFER_PENDING must be set to prevent dwc3_ep_queue from starting a new transfer during a EP transfer timeout.

But, this is unnecessary for __dwc3_gadget_ep_disable. Since there's no way to clear the pending flag if the interrupt is missed and no dwc3_ep_queue calls occur until the EP is re-enabled, preserving DWC3_EP_END_TRANSFER_PENDING here provides no benefit.

Quoted 7 lines
Quoted 2 lines

So, the below changes is not necessary in ep disable,

We still need to keep DWC3_EP_END_TRANSFER_PENDING in ep_disable. That's for the normal case where the End Transfer completes after ep_disable returns, which is the original issue. If the command never completes, the endpoint resource is stuck regardless. Clearing the flag only lead to a NO_RESOURCE error later.

Agreed.

Quoted 4 lines

As for the dwc3_gadget_ep_queue() race during giveback, keeping DWC3_EP_TRANSFER_STARTED isn't right. We should reject the queue when the endpoint is disabled. This should be a separate patch:

Agreed.

Quoted 16 lines
drivers/usb/dwc3/gadget.c +2 -0
index ee837235630a..eb6666b7bb98 100644
@@ -1084,6 +1084,8 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
  	reg &= ~DWC3_DALEPENA_EP(dep->number);
  	dwc3_writel(dwc, DWC3_DALEPENA, reg);
 
+	dep->flags &= ~DWC3_EP_ENABLED;
+
  	dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
 
  	dep->stream_capable = false;

drivers/usb/dwc3/gadget.c static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) · line 1999

Quoted 9 lines
drivers/usb/dwc3/gadget.c +2 -1
@@ -1990,7 +1992,8 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
  {
  	struct dwc3		*dwc = dep->dwc;
 
-	if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
+	if (!(dep->flags & DWC3_EP_ENABLED) || !dep->endpoint.desc ||
+	    !dwc->pullups_connected || !dwc->connected) {
  		dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n",
  				dep->name);
  		return -ESHUTDOWN;

Thanks for suggestion. We will test this patch to confirm no resource issue in race condition.

Quoted 3 lines

The End Transfer command not completing is also a separate issue. I asked you to check whether the command would eventually complete with the additional code, and it appears it doesn't.

 We have confirmed that the completion event is never seen, once the
timeout occurs for end transfer. It appears the command is indeed hanging in the hardware, as you suspected.

Quoted 7 lines

Can you provide some more info on your setup:
* IP and version
* connected speed
* endpoint direction (is it always OUT?)
* Is there any active transfer
* tracepoints

* IP and version : 0xc120:   0x33313130
* connected speed : HS mode
* endpoint direction (is it always OUT?) : Yes its always OUT
* Is there any active transfer : No, There is evidences from the log to say there is a active transfer.
* tracepoints: There is no dwc3 traces for this issue as of now due to its low reproduction rate in the customer's setup.

Thanks,
Selva

Quoted 2 lines

Thanks,
Thinh

maintainer
#17 lore raw
Quoted 17 lines · Selvarasu Ganesan
Quoted 4 lines

The End Transfer command not completing is also a separate issue. I asked you to check whether the command would eventually complete with the additional code, and it appears it doesn't.

 We have confirmed that the completion event is never seen, once the
timeout occurs for end transfer. It appears the command is indeed hanging in the hardware, as you suspected.

Quoted 7 lines

Can you provide some more info on your setup:
* IP and version
* connected speed
* endpoint direction (is it always OUT?)
* Is there any active transfer
* tracepoints

Quoted 1 line

* IP and version : 0xc120:   0x33313130

and version (0xc1a0)?

Quoted 4 lines

* connected speed : HS mode
* endpoint direction (is it always OUT?) : Yes its always OUT
* Is there any active transfer : No, There is evidences from the log to say there is a active transfer.

The host may have sent data that would not appear in the log until completion. Are there any active transfer on other endpoints?

Quoted 3 lines

* tracepoints: There is no dwc3 traces for this issue as of now due to its low reproduction rate in the customer's setup.

I think you may need to file a case with our support team and provide additional debug info such as scan dump to investigate this further.

Thanks,
Thinh

#18 lore raw
Quoted 15 lines · Thinh Nguyen
Quoted 13 lines · Selvarasu Ganesan
Quoted 3 lines

The End Transfer command not completing is also a separate issue. I asked you to check whether the command would eventually complete with the additional code, and it appears it doesn't.

 We have confirmed that the completion event is never seen, once the
timeout occurs for end transfer. It appears the command is indeed hanging in the hardware, as you suspected.

Quoted 6 lines

Can you provide some more info on your setup:
* IP and version
* connected speed
* endpoint direction (is it always OUT?)
* Is there any active transfer
* tracepoints

Quoted 2 lines
Quoted 1 line

* IP and version : 0xc120:   0x33313130

and version (0xc1a0)?

VER_NUMBER = 0x3231302a
VER_TYPE = 0x67612a2a

Quoted 7 lines
Quoted 5 lines

* connected speed : HS mode
* endpoint direction (is it always OUT?) : Yes its always OUT
* Is there any active transfer : No, There is evidences from the log to say there is a active transfer.

The host may have sent data that would not appear in the log until completion. Are there any active transfer on other endpoints?

Yes, we can see active transfer on other endpoints.

Quoted 6 lines
Quoted 4 lines

* tracepoints: There is no dwc3 traces for this issue as of now due to its low reproduction rate in the customer's setup.

I think you may need to file a case with our support team and provide additional debug info such as scan dump to investigate this further.

Sure We will file a case,
I agree that the ENDTRANSFER timeout may be a controller level issue that we can debug separately.  But, there is also a software bug here. Let see the in the below error case, "dep->resource_index" is reset to "0" unconditionally, even when the first ENDTRANSFER times out. A subsequent ENDTRANSFER with resource index "0" may succeed and incorrectly clear the pending and transfer started flags, making the driver assume the previous transfer has completed. Since the original transfer was never properly terminated, the endpoint may not recover, resulting in a no-resources error on the next STARTTRANSFER.

Therefore, we need to fix this software state tracking issue independently of the controller timeout.

Step 1:
__dwc3_gadget_ep_set_halt(dep, value=0)
 ->dwc3_stop_active_transfer(dep, true, true) ->END(e.g, ep2out, resource_index=7) issued, but timeout occurs -> resource_index cleared to 0
->dwc3_send_clear_stall_ep_cmd(dep)
 -> failed to clear STALL on ep2out

Step 2:
__dwc3_gadget_ep_set_halt(dep, value=0) ->Re triggered clear stall for same EP
 -> dwc3_stop_active_transfer(dep, true, true)
   -> END(ep2out, resource_index=0) issued (wrong resource!) -> Command succeeds, DWC3_EP_TRANSFER_STARTED cleared in completion
handler

Step 3:
usb_ep_queue()
-> dwc3_gadget_ep_queue()
 -> __dwc3_gadget_kick_transfer()
  -> starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED) -> starting=0 -> Issues STARTTRANSFER (because DWC3_EP_TRANSFER_STARTED is not set) -> Hardware rejects with NO_RESOURCE (resource 7 still held)

We are fine with the following suggestions fix to prevent resource issue in timeout case,

drivers/usb/dwc3/gadget.c +0 -0
index 1f973e546219..bac0328423cd 100644
@@ -1819,7 +1819,11 @@ static int __dwc3_stop_active_transfer(struct 

dwc3_ep *dep, bool force, bool int

     dep->resource_index = 0;

-    if (!interrupt || ret)
+    if (!interrupt || (ret && ret != -ETIMEDOUT))

         dep->flags &= ~DWC3_EP_TRANSFER_STARTED; else
         dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
@@ -3895,6 +3899,8 @@ static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
     if (dep->stream_capable)
         dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;

+    dep->flags &= ~DWC3_EP_DELAY_STOP;
     dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; dep->flags &= ~DWC3_EP_TRANSFER_STARTED; dwc3_gadget_ep_cleanup_cancelled_requests(dep);

But regarding below changes in ep_disable that you suggested, is this really required? It seems this could cause a bug during an endtransfer timeout, if command completion never happens, the PENDING flag won't be cleared, making recovery impossible.

@@ -1096,6 +1110,15 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
      */
     if (dep->flags & DWC3_EP_DELAY_STOP)
         mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+    /*
+     * The End Transfer command is still in progress.  Do not clear the +     * flags, so that the ep is only rearmed once the command completes. +     */
+    if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
+        mask |= (DWC3_EP_END_TRANSFER_PENDING |
+             DWC3_EP_TRANSFER_STARTED);

As you mentioned Clearing the flag (DWC3_EP_END_TRANSFER_PENDING) in ep_disble only lead to a NO_RESOURCE error later. Yes i agree, but this can prevent by below new suggestion change, so above changes (do not clear DWC3_EP_END_TRANSFER_PENDING in ep_disable) is not required.

drivers/usb/dwc3/gadget.c index renamed from drivers/usb/dwc3/gadget.c +0 -0

ee837235630a..eb6666b7bb98 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -1084,6 +1084,8 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) reg &= ~DWC3_DALEPENA_EP(dep->number); dwc3_writel(dwc, DWC3_DALEPENA, reg); + dep->flags &= ~DWC3_EP_ENABLED; + dwc3_remove_requests(dwc, dep, -ESHUTDOWN); dep->stream_capable = false; @@ -1990,7 +1992,8 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) { struct dwc3 *dwc = dep->dwc; - if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) { + if (!(dep->flags & DWC3_EP_ENABLED) || !dep->endpoint.desc || + !dwc->pullups_connected || !dwc->connected) { dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n", dep->name); return -ESHUTDOWN;

Thanks,
Selva

Quoted 3 lines

Thanks,
Thinh

maintainer
#19 lore raw
Quoted 28 lines · Selvarasu Ganesan
Quoted 15 lines · Thinh Nguyen
Quoted 13 lines · Selvarasu Ganesan
Quoted 3 lines

The End Transfer command not completing is also a separate issue. I asked you to check whether the command would eventually complete with the additional code, and it appears it doesn't.

 We have confirmed that the completion event is never seen, once the
timeout occurs for end transfer. It appears the command is indeed hanging in the hardware, as you suspected.

Quoted 6 lines

Can you provide some more info on your setup:
* IP and version
* connected speed
* endpoint direction (is it always OUT?)
* Is there any active transfer
* tracepoints

Quoted 2 lines
Quoted 1 line

* IP and version : 0xc120:   0x33313130

and version (0xc1a0)?

VER_NUMBER = 0x3231302a
VER_TYPE = 0x67612a2a

Quoted 5 lines
Quoted 5 lines

* connected speed : HS mode
* endpoint direction (is it always OUT?) : Yes its always OUT
* Is there any active transfer : No, There is evidences from the log to say there is a active transfer.

Quoted 5 lines
Quoted 2 lines

The host may have sent data that would not appear in the log until completion. Are there any active transfer on other endpoints?

Yes, we can see active transfer on other endpoints.

Was there another SETUP packet aborting the ClearFeature(ep_halt) while this is going on?

Quoted 77 lines
Quoted 7 lines
Quoted 5 lines

* tracepoints: There is no dwc3 traces for this issue as of now due to its low reproduction rate in the customer's setup.

I think you may need to file a case with our support team and provide additional debug info such as scan dump to investigate this further.

Sure We will file a case,
I agree that the ENDTRANSFER timeout may be a controller level issue that we can debug separately.  But, there is also a software bug here. Let see the in the below error case, "dep->resource_index" is reset to "0" unconditionally, even when the first ENDTRANSFER times out. A subsequent ENDTRANSFER with resource index "0" may succeed and incorrectly clear the pending and transfer started flags, making the driver assume the previous transfer has completed. Since the original transfer was never properly terminated, the endpoint may not recover, resulting in a no-resources error on the next STARTTRANSFER.

Therefore, we need to fix this software state tracking issue independently of the controller timeout.

Step 1:
__dwc3_gadget_ep_set_halt(dep, value=0)
 ->dwc3_stop_active_transfer(dep, true, true) ->END(e.g, ep2out, resource_index=7) issued, but timeout occurs -> resource_index cleared to 0
->dwc3_send_clear_stall_ep_cmd(dep)
 -> failed to clear STALL on ep2out

Step 2:
__dwc3_gadget_ep_set_halt(dep, value=0) ->Re triggered clear stall for same EP
 -> dwc3_stop_active_transfer(dep, true, true)
   -> END(ep2out, resource_index=0) issued (wrong resource!) -> Command succeeds, DWC3_EP_TRANSFER_STARTED cleared in completion
handler

Step 3:
usb_ep_queue()
-> dwc3_gadget_ep_queue()
 -> __dwc3_gadget_kick_transfer()
  -> starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED) -> starting=0 -> Issues STARTTRANSFER (because DWC3_EP_TRANSFER_STARTED is not set) -> Hardware rejects with NO_RESOURCE (resource 7 still held)

We are fine with the following suggestions fix to prevent resource issue in timeout case,

drivers/usb/dwc3/gadget.c +0 -0
index 1f973e546219..bac0328423cd 100644
@@ -1819,7 +1819,11 @@ static int __dwc3_stop_active_transfer(struct 

dwc3_ep *dep, bool force, bool int

     dep->resource_index = 0;

-    if (!interrupt || ret)
+    if (!interrupt || (ret && ret != -ETIMEDOUT))

         dep->flags &= ~DWC3_EP_TRANSFER_STARTED; else
         dep->flags |= DWC3_EP_END_TRANSFER_PENDING;

drivers/usb/dwc3/gadget.c +1 -0
@@ -3895,6 +3899,8 @@ static void 
 dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
      if (dep->stream_capable)
          dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
 
+    dep->flags &= ~DWC3_EP_DELAY_STOP;
      dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
      dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
      dwc3_gadget_ep_cleanup_cancelled_requests(dep);
Quoted 4 lines

But regarding below changes in ep_disable that you suggested, is this really required? It seems this could cause a bug during an endtransfer timeout, if command completion never happens, the PENDING flag won't be cleared, making recovery impossible.

Yes, it's required. The suggested patch now enables IOC for ep_disable. So we need to track its completion with DWC3_EP_END_TRANSFER_PENDING. This handles the case where the End Transfer completes after ep_disable returns. If the function driver re-enables and queues to the endpoint before then, the driver would issue Start Transfer while the resource is still held. That's the original issue.

If the End Transfer never completes, the endpoint is stuck. Clearing the flag doesn't recover it. The dwc3_schedule_err_recovery() is now in mainline. We can use that as recovery for -ETIMEDOUT.

Looking at the recovery function now, we also need to reset the dep->flags on recovery which I missed when reviewing the change. I'll send a patch for this.

Quoted 16 lines
drivers/usb/dwc3/gadget.c +8 -0
@@ -1096,6 +1110,15 @@ static int __dwc3_gadget_ep_disable(struct 
 dwc3_ep *dep)
       */
      if (dep->flags & DWC3_EP_DELAY_STOP)
          mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+    /*
+     * The End Transfer command is still in progress.  Do not clear the
+     * flags, so that the ep is only rearmed once the command completes.
+     */
+    if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
+        mask |= (DWC3_EP_END_TRANSFER_PENDING |
+             DWC3_EP_TRANSFER_STARTED);
 
 
Quoted 4 lines

As you mentioned Clearing the flag (DWC3_EP_END_TRANSFER_PENDING) in ep_disble only lead to a NO_RESOURCE error later. Yes i agree, but this can prevent by below new suggestion change, so above changes (do not clear DWC3_EP_END_TRANSFER_PENDING in ep_disable) is not required.

The ep_queue change only covers the giveback window inside ep_disable, not this case.

Quoted 6 lines
drivers/usb/dwc3/gadget.c index renamed from drivers/usb/dwc3/gadget.c +0 -0

ee837235630a..eb6666b7bb98 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -1084,6 +1084,8 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) reg &= ~DWC3_DALEPENA_EP(dep->number); dwc3_writel(dwc, DWC3_DALEPENA, reg); +

Quoted 9 lines

dep->flags &= ~DWC3_EP_ENABLED; + dwc3_remove_requests(dwc, dep, -ESHUTDOWN); dep->stream_capable = false; @@ -1990,7 +1992,8 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) { struct dwc3 *dwc = dep->dwc; - if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) { + if (!(dep->flags & DWC3_EP_ENABLED) || !dep->endpoint.desc || + !dwc->pullups_connected || !dwc->connected) { dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n", dep->name); return -ESHUTDOWN;

(Can you help fix your mail client? I had to unmangle the diff above to know what it was)

Thanks,
Thinh

#20 lore raw
Quoted 25 lines · Thinh Nguyen
Quoted 24 lines · Selvarasu Ganesan
Quoted 8 lines · Thinh Nguyen
Quoted 7 lines · Selvarasu Ganesan
Quoted 3 lines

The End Transfer command not completing is also a separate issue. I asked you to check whether the command would eventually complete with the additional code, and it appears it doesn't.

 We have confirmed that the completion event is never seen, once the
timeout occurs for end transfer. It appears the command is indeed hanging in the hardware, as you suspected.

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines

Can you provide some more info on your setup:
* IP and version
* connected speed
* endpoint direction (is it always OUT?)
* Is there any active transfer
* tracepoints

* IP and version : 0xc120:   0x33313130

and version (0xc1a0)?

VER_NUMBER = 0x3231302a
VER_TYPE = 0x67612a2a

Quoted 4 lines
Quoted 4 lines

* connected speed : HS mode
* endpoint direction (is it always OUT?) : Yes its always OUT
* Is there any active transfer : No, There is evidences from the log to say there is a active transfer.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

The host may have sent data that would not appear in the log until completion. Are there any active transfer on other endpoints?

Yes, we can see active transfer on other endpoints.

Was there another SETUP packet aborting the ClearFeature(ep_halt) while this is going on?

We dont see any  evidence from the log to say SETUP packet aborting, anyway we will add a debug logs to confirm this one.

Quoted 86 lines
Quoted 75 lines
Quoted 6 lines
Quoted 4 lines

* tracepoints: There is no dwc3 traces for this issue as of now due to its low reproduction rate in the customer's setup.

I think you may need to file a case with our support team and provide additional debug info such as scan dump to investigate this further.

Sure We will file a case,
I agree that the ENDTRANSFER timeout may be a controller level issue that we can debug separately.  But, there is also a software bug here. Let see the in the below error case, "dep->resource_index" is reset to "0" unconditionally, even when the first ENDTRANSFER times out. A subsequent ENDTRANSFER with resource index "0" may succeed and incorrectly clear the pending and transfer started flags, making the driver assume the previous transfer has completed. Since the original transfer was never properly terminated, the endpoint may not recover, resulting in a no-resources error on the next STARTTRANSFER.

Therefore, we need to fix this software state tracking issue independently of the controller timeout.

Step 1:
__dwc3_gadget_ep_set_halt(dep, value=0)
 ->dwc3_stop_active_transfer(dep, true, true) ->END(e.g, ep2out, resource_index=7) issued, but timeout occurs -> resource_index cleared to 0
->dwc3_send_clear_stall_ep_cmd(dep)
 -> failed to clear STALL on ep2out

Step 2:
__dwc3_gadget_ep_set_halt(dep, value=0) ->Re triggered clear stall for same EP
 -> dwc3_stop_active_transfer(dep, true, true)
   -> END(ep2out, resource_index=0) issued (wrong resource!) -> Command succeeds, DWC3_EP_TRANSFER_STARTED cleared in completion
handler

Step 3:
usb_ep_queue()
-> dwc3_gadget_ep_queue()
 -> __dwc3_gadget_kick_transfer()
  -> starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED) -> starting=0 -> Issues STARTTRANSFER (because DWC3_EP_TRANSFER_STARTED is not set) -> Hardware rejects with NO_RESOURCE (resource 7 still held)

We are fine with the following suggestions fix to prevent resource issue in timeout case,

drivers/usb/dwc3/gadget.c +0 -0
index 1f973e546219..bac0328423cd 100644
@@ -1819,7 +1819,11 @@ static int __dwc3_stop_active_transfer(struct

dwc3_ep *dep, bool force, bool int

       dep->resource_index = 0;
-    if (!interrupt || ret)
+    if (!interrupt || (ret && ret != -ETIMEDOUT))

         dep->flags &= ~DWC3_EP_TRANSFER_STARTED; else
         dep->flags |= DWC3_EP_END_TRANSFER_PENDING;

drivers/usb/dwc3/gadget.c +1 -0
@@ -3895,6 +3899,8 @@ static void
 dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
       if (dep->stream_capable)
           dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
 
+    dep->flags &= ~DWC3_EP_DELAY_STOP;
       dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
       dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
       dwc3_gadget_ep_cleanup_cancelled_requests(dep);
Quoted 4 lines

But regarding below changes in ep_disable that you suggested, is this really required? It seems this could cause a bug during an endtransfer timeout, if command completion never happens, the PENDING flag won't be cleared, making recovery impossible.

Yes, it's required. The suggested patch now enables IOC for ep_disable. So we need to track its completion with DWC3_EP_END_TRANSFER_PENDING. This handles the case where the End Transfer completes after ep_disable returns. If the function driver re-enables and queues to the endpoint before then, the driver would issue Start Transfer while the resource is still held. That's the original issue.

Quoted 2 lines

If the End Transfer never completes, the endpoint is stuck. Clearing the flag doesn't recover it. The dwc3_schedule_err_recovery() is now in

I mean 'not recover here' is the failure to clear the DWC3_EP_END_TRANSFER_PENDING flag upon cable reconnection, caused by a missing command completion from the prior connection.

Sure we can use dwc3_schedule_err_recovery(), and thanks for the suggestion.

Quoted 5 lines

mainline. We can use that as recovery for -ETIMEDOUT.

Looking at the recovery function now, we also need to reset the dep->flags on recovery which I missed when reviewing the change. I'll send a patch for this.

Great, Thank you.

Quoted 16 lines
Quoted 16 lines
drivers/usb/dwc3/gadget.c +8 -0
@@ -1096,6 +1110,15 @@ static int __dwc3_gadget_ep_disable(struct
 dwc3_ep *dep)
        */
       if (dep->flags & DWC3_EP_DELAY_STOP)
           mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
+
+    /*
+     * The End Transfer command is still in progress.  Do not clear the
+     * flags, so that the ep is only rearmed once the command completes.
+     */
+    if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
+        mask |= (DWC3_EP_END_TRANSFER_PENDING |
+             DWC3_EP_TRANSFER_STARTED);
 
 
Quoted 6 lines
Quoted 4 lines

As you mentioned Clearing the flag (DWC3_EP_END_TRANSFER_PENDING) in ep_disble only lead to a NO_RESOURCE error later. Yes i agree, but this can prevent by below new suggestion change, so above changes (do not clear DWC3_EP_END_TRANSFER_PENDING in ep_disable) is not required.

The ep_queue change only covers the giveback window inside ep_disable, not this case.

agree.

Quoted 17 lines
Quoted 6 lines
drivers/usb/dwc3/gadget.c index renamed from drivers/usb/dwc3/gadget.c +0 -0

ee837235630a..eb6666b7bb98 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -1084,6 +1084,8 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) reg &= ~DWC3_DALEPENA_EP(dep->number); dwc3_writel(dwc, DWC3_DALEPENA, reg); +

Quoted 9 lines

dep->flags &= ~DWC3_EP_ENABLED; + dwc3_remove_requests(dwc, dep, -ESHUTDOWN); dep->stream_capable = false; @@ -1990,7 +1992,8 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) { struct dwc3 *dwc = dep->dwc; - if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) { + if (!(dep->flags & DWC3_EP_ENABLED) || !dep->endpoint.desc || + !dwc->pullups_connected || !dwc->connected) { dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n", dep->name); return -ESHUTDOWN;

(Can you help fix your mail client? I had to unmangle the diff above to know what it was)

drivers/usb/dwc3/gadget.c +0 -0
index ee837235630a..eb6666b7bb98 100644
@@ -1084,6 +1084,8 @@ static int __dwc3_gadget_ep_disable(struct dwc3_ep 

*dep)
     reg &= ~DWC3_DALEPENA_EP(dep->number);
     dwc3_writel(dwc, DWC3_DALEPENA, reg);

+    dep->flags &= ~DWC3_EP_ENABLED;
+
     dwc3_remove_requests(dwc, dep, -ESHUTDOWN);

     dep->stream_capable = false;
@@ -1990,7 +1992,8 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
 {
     struct dwc3        *dwc = dep->dwc;

-    if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
+    if (!(dep->flags & DWC3_EP_ENABLED) || !dep->endpoint.desc || +        !dwc->pullups_connected || !dwc->connected) {
         dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n", dep->name);
         return -ESHUTDOWN;

Quoted 3 lines

Thanks,
Thinh

maintainer
#21 lore raw
Quoted 4 lines · Selvarasu Ganesan

I mean 'not recover here' is the failure to clear the DWC3_EP_END_TRANSFER_PENDING flag upon cable reconnection, caused by a missing command completion from the prior connection.

Right. Resetting the endpoint flags on controller restart will cover that.

Quoted 3 lines

Sure we can use dwc3_schedule_err_recovery(), and thanks for the suggestion.

I can prepare the fix patches based on our discussion. It would be great if you can test them.

Thanks,
Thinh

#22 lore raw
Quoted 11 lines · Thinh Nguyen
Quoted 3 lines · Selvarasu Ganesan

I mean 'not recover here' is the failure to clear the DWC3_EP_END_TRANSFER_PENDING flag upon cable reconnection, caused by a missing command completion from the prior connection.

Right. Resetting the endpoint flags on controller restart will cover that.

Quoted 2 lines

Sure we can use dwc3_schedule_err_recovery(), and thanks for the suggestion.

I can prepare the fix patches based on our discussion. It would be great if you can test them.

Sure, Thanks for your support.

Thanks,
Selva

Quoted 3 lines

Thanks,
Thinh