diff mbox series

[v2] usb: dwc3: gadget: check drained isoc ep

Message ID 20240307-dwc3-gadget-complete-irq-v2-1-8c5e9b35f7b9@pengutronix.de
State New
Headers show
Series [v2] usb: dwc3: gadget: check drained isoc ep | expand

Commit Message

Michael Grzeschik April 2, 2024, 9:51 p.m. UTC
To avoid a potential underrun of an currently drained transfer
we add a check for that scenario in the dwc3_gadget_endpoint_trbs_complete
function. In the case of an empty trb ring, we call the stop_transfer
cmd and avoid the underrun to occur.

Signed-off-by: Michael Grzeschik <m.grzeschik@pengutronix.de>
---
Changes in v2:
- dropped patch "usb: dwc3: gadget: reclaim the whole started list when request was missed"
- fixed patch "usb: dwc3: gadget: check drained isoc ep"
- dropped patch "usb: dwc3: gadget: check the whole started queue for missed requests in complete"
- Link to v1: https://lore.kernel.org/r/20240307-dwc3-gadget-complete-irq-v1-0-4fe9ac0ba2b7@pengutronix.de
---
 drivers/usb/dwc3/gadget.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)


---
base-commit: 5bab5dc780c9ed0c69fc2f828015532acf4a7848
change-id: 20240307-dwc3-gadget-complete-irq-1a8ffa347fd1

Best regards,

Comments

Thinh Nguyen April 4, 2024, 12:29 a.m. UTC | #1
On Tue, Apr 02, 2024, Thinh Nguyen wrote:
> On Tue, Apr 02, 2024, Thinh Nguyen wrote:
> > My concern here is for the case where transfer_in_flight == true and
> 
> I mean transfer_in_flight == false
> 
> > list_empty(started_list) == false. That means that the requests in the
> > started_list are completed but are not given back to the gadget driver.
> > 
> > Since they remained in the started_list, they will be resubmitted again
> > on the next usb_ep_queue. We may send duplicate transfers right?

Actually, since the requests are completed, the HWO bits are cleared,
nothing is submitted and no duplicate. But since the requests are not
given back yet from the started_list, then the next Start_Transfer
command will begin with the TRB address of the completed request
(HWO=0), the controller may not process the next TRBs. Have you tested
this scenario?

> > 
> > You can try to cleanup requests in the started_list, but you need to be
> > careful to make sure you're not out of sync with the transfer completion
> > events and new requests from gadget driver.
> > 

Was the problem you encounter due to no_interrupt settings where the
it was set to the last request of the uvc data pump?

if that's the case, can UVC function driver make sure to not set
no_interrupt to the last request of the data pump from the UVC?

Thanks,
Thinh
Michael Grzeschik April 23, 2024, 1:37 p.m. UTC | #2
Hi Thinh,

On Thu, Apr 04, 2024 at 12:29:14AM +0000, Thinh Nguyen wrote:
>On Tue, Apr 02, 2024, Thinh Nguyen wrote:
>> On Tue, Apr 02, 2024, Thinh Nguyen wrote:
>> > My concern here is for the case where transfer_in_flight == true and
>>
>> I mean transfer_in_flight == false
>>
>> > list_empty(started_list) == false. That means that the requests in the
>> > started_list are completed but are not given back to the gadget driver.
>> >
>> > Since they remained in the started_list, they will be resubmitted again
>> > on the next usb_ep_queue. We may send duplicate transfers right?
>
>Actually, since the requests are completed, the HWO bits are cleared,
>nothing is submitted and no duplicate. But since the requests are not
>given back yet from the started_list, then the next Start_Transfer
>command will begin with the TRB address of the completed request
>(HWO=0), the controller may not process the next TRBs. Have you tested
>this scenario?
>
>> >
>> > You can try to cleanup requests in the started_list, but you need to be
>> > careful to make sure you're not out of sync with the transfer completion
>> > events and new requests from gadget driver.
>> >
>
>Was the problem you encounter due to no_interrupt settings where the
>it was set to the last request of the uvc data pump?
>
>if that's the case, can UVC function driver make sure to not set
>no_interrupt to the last request of the data pump from the UVC?

Actually no. What I want to do is to ensure that the dwc3 stream
is stopped when the hardware was drained. Which is a valid point
in my case. Since we are actually potentially enqueueing new request
in the complete handler, be it zero length or real transfers.

Calling kick_transfer on an drained hw will absolutely run into
missed isocs if the irq thread was called late. We saw this on real hardware,
where another irq_thread was scheduled with the same priority as the
dwc3 irq_thread but was running so long that the HW was running dry in
between the hw irq and the actual dwc3_irq_thread run.

Michael
diff mbox series

Patch

diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 4df2661f66751..3e9c67799259a 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -3582,13 +3582,26 @@  static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
 	if (!dep->endpoint.desc)
 		return no_started_trb;
 
-	if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
-		list_empty(&dep->started_list) &&
-		(list_empty(&dep->pending_list) || status == -EXDEV))
-		dwc3_stop_active_transfer(dep, true, true);
-	else if (dwc3_gadget_ep_should_continue(dep))
+	if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
+		/* It is possible that the interrupt thread was delayed
+		 * by scheduling in the system, and therefor the HW has
+		 * already run dry. In that case the last trb in the
+		 * queue is already handled by the hw. By checking the
+		 * HWO bit we know to restart the whole transfer. The
+		 * condition to appear is more likely if not every req
+		 * has the IOC bit set and therefor does not trigger the
+		 * interrupt thread frequently.
+		 */
+		struct dwc3_trb *trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
+		unsigned int transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
+
+		if ((list_empty(&dep->started_list) || !transfer_in_flight) &&
+		    (list_empty(&dep->pending_list) || status == -EXDEV))
+			dwc3_stop_active_transfer(dep, true, true);
+	} else if (dwc3_gadget_ep_should_continue(dep)) {
 		if (__dwc3_gadget_kick_transfer(dep) == 0)
 			no_started_trb = false;
+	}
 
 out:
 	/*