All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: xhci: reset endpoint on USB stall
@ 2021-09-27 12:42 Stefan Agner
  2021-09-27 15:14 ` Marek Vasut
  2022-02-16  7:53 ` Bin Meng
  0 siblings, 2 replies; 7+ messages in thread
From: Stefan Agner @ 2021-09-27 12:42 UTC (permalink / raw)
  To: u-boot, bmeng.cn; +Cc: mike.hoogstraten, Stefan Agner, Marek Vasut

There are devices which cause a USB stall when trying to read strings.
Specifically Arduino Mega R3 stalls when trying to read the product
string.

The stall currently remains unhandled, and subsequent retries submit new
transfers on a stopped endpoint which ultimately cause a crash in
abort_td():
WARN halted endpoint, queueing URB anyway.
XHCI control transfer timed out, aborting...
Unexpected XHCI event TRB, skipping... (3affe040 00000000 13000000 02008401)
BUG at drivers/usb/host/xhci-ring.c:505/abort_td()!
BUG!
resetting ...

Linux seems to be able to recover from the stall by issuing a
TRB_RESET_EP command.

Introduce reset_ep() which issues a TRB_RESET_EP followed by setting the
transfer ring dequeue pointer via TRB_SET_DEQ. This allows to properly
recover from a USB stall error and continue communicating with the USB
device.

Signed-off-by: Stefan Agner <stefan@agner.ch>
---
This has been reported on the ML by Mike Hoogstraten a while ago:
https://www.mail-archive.com/u-boot@lists.denx.de/msg390604.html

FWIW, for the Arduino Mega R3 I also opened an issue:
https://github.com/arduino/ArduinoCore-avr/issues/431

--
Stefan

 drivers/usb/host/xhci-ring.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 35bd5cd29e..1a9dff3b6c 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -481,6 +481,33 @@ union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected)
 	BUG();
 }
 
+/*
+ * Send reset endpoint command for given endpoint. This recovers from a
+ * halted endpoint (e.g. due to a stall error).
+ */
+static void reset_ep(struct usb_device *udev, int ep_index)
+{
+	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
+	struct xhci_ring *ring =  ctrl->devs[udev->slot_id]->eps[ep_index].ring;
+	union xhci_trb *event;
+	u32 field;
+
+	printf("Resetting EP %d...\n", ep_index);
+	xhci_queue_command(ctrl, NULL, udev->slot_id, ep_index, TRB_RESET_EP);
+	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
+	field = le32_to_cpu(event->trans_event.flags);
+	BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
+	xhci_acknowledge_event(ctrl);
+
+	xhci_queue_command(ctrl, (void *)((uintptr_t)ring->enqueue |
+		ring->cycle_state), udev->slot_id, ep_index, TRB_SET_DEQ);
+	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
+	BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
+		!= udev->slot_id || GET_COMP_CODE(le32_to_cpu(
+		event->event_cmd.status)) != COMP_SUCCESS);
+	xhci_acknowledge_event(ctrl);
+}
+
 /*
  * Stops transfer processing for an endpoint and throws away all unprocessed
  * TRBs by setting the xHC's dequeue pointer to our enqueue pointer. The next
@@ -928,6 +955,10 @@ int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe,
 
 	record_transfer_result(udev, event, length);
 	xhci_acknowledge_event(ctrl);
+	if (udev->status == USB_ST_STALLED) {
+		reset_ep(udev, ep_index);
+		return -EPIPE;
+	}
 
 	/* Invalidate buffer to make it available to usb-core */
 	if (length > 0)
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] usb: xhci: reset endpoint on USB stall
  2021-09-27 12:42 [PATCH] usb: xhci: reset endpoint on USB stall Stefan Agner
@ 2021-09-27 15:14 ` Marek Vasut
  2022-01-04 19:48   ` Stefan Agner
  2022-02-16  7:53 ` Bin Meng
  1 sibling, 1 reply; 7+ messages in thread
From: Marek Vasut @ 2021-09-27 15:14 UTC (permalink / raw)
  To: Stefan Agner, u-boot, bmeng.cn; +Cc: mike.hoogstraten

On 9/27/21 2:42 PM, Stefan Agner wrote:
> There are devices which cause a USB stall when trying to read strings.
> Specifically Arduino Mega R3 stalls when trying to read the product
> string.
> 
> The stall currently remains unhandled, and subsequent retries submit new
> transfers on a stopped endpoint which ultimately cause a crash in
> abort_td():
> WARN halted endpoint, queueing URB anyway.
> XHCI control transfer timed out, aborting...
> Unexpected XHCI event TRB, skipping... (3affe040 00000000 13000000 02008401)
> BUG at drivers/usb/host/xhci-ring.c:505/abort_td()!
> BUG!
> resetting ...
> 
> Linux seems to be able to recover from the stall by issuing a
> TRB_RESET_EP command.
> 
> Introduce reset_ep() which issues a TRB_RESET_EP followed by setting the
> transfer ring dequeue pointer via TRB_SET_DEQ. This allows to properly
> recover from a USB stall error and continue communicating with the USB
> device.
> 
> Signed-off-by: Stefan Agner <stefan@agner.ch>

I hope to get AB/RB from Bin here, then it can go into this release I think.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] usb: xhci: reset endpoint on USB stall
  2021-09-27 15:14 ` Marek Vasut
@ 2022-01-04 19:48   ` Stefan Agner
  2022-01-05  1:21     ` Bin Meng
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Agner @ 2022-01-04 19:48 UTC (permalink / raw)
  To: bmeng.cn; +Cc: u-boot, bmeng.cn, mike.hoogstraten, Marek Vasut

Bin Meng,

On 2021-09-27 17:14, Marek Vasut wrote:
> On 9/27/21 2:42 PM, Stefan Agner wrote:
>> There are devices which cause a USB stall when trying to read strings.
>> Specifically Arduino Mega R3 stalls when trying to read the product
>> string.
>>
>> The stall currently remains unhandled, and subsequent retries submit new
>> transfers on a stopped endpoint which ultimately cause a crash in
>> abort_td():
>> WARN halted endpoint, queueing URB anyway.
>> XHCI control transfer timed out, aborting...
>> Unexpected XHCI event TRB, skipping... (3affe040 00000000 13000000 02008401)
>> BUG at drivers/usb/host/xhci-ring.c:505/abort_td()!
>> BUG!
>> resetting ...
>>
>> Linux seems to be able to recover from the stall by issuing a
>> TRB_RESET_EP command.
>>
>> Introduce reset_ep() which issues a TRB_RESET_EP followed by setting the
>> transfer ring dequeue pointer via TRB_SET_DEQ. This allows to properly
>> recover from a USB stall error and continue communicating with the USB
>> device.
>>
>> Signed-off-by: Stefan Agner <stefan@agner.ch>
> 
> I hope to get AB/RB from Bin here, then it can go into this release I think.

Any chance you could have a look at this to get it into this release :)

--
Stefan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] usb: xhci: reset endpoint on USB stall
  2022-01-04 19:48   ` Stefan Agner
@ 2022-01-05  1:21     ` Bin Meng
  2022-01-24 14:23       ` Stefan Agner
  0 siblings, 1 reply; 7+ messages in thread
From: Bin Meng @ 2022-01-05  1:21 UTC (permalink / raw)
  To: Stefan Agner; +Cc: U-Boot Mailing List, mike.hoogstraten, Marek Vasut

Hi Stefan,

On Wed, Jan 5, 2022 at 3:48 AM Stefan Agner <stefan@agner.ch> wrote:
>
> Bin Meng,
>
> On 2021-09-27 17:14, Marek Vasut wrote:
> > On 9/27/21 2:42 PM, Stefan Agner wrote:
> >> There are devices which cause a USB stall when trying to read strings.
> >> Specifically Arduino Mega R3 stalls when trying to read the product
> >> string.
> >>
> >> The stall currently remains unhandled, and subsequent retries submit new
> >> transfers on a stopped endpoint which ultimately cause a crash in
> >> abort_td():
> >> WARN halted endpoint, queueing URB anyway.
> >> XHCI control transfer timed out, aborting...
> >> Unexpected XHCI event TRB, skipping... (3affe040 00000000 13000000 02008401)
> >> BUG at drivers/usb/host/xhci-ring.c:505/abort_td()!
> >> BUG!
> >> resetting ...
> >>
> >> Linux seems to be able to recover from the stall by issuing a
> >> TRB_RESET_EP command.
> >>
> >> Introduce reset_ep() which issues a TRB_RESET_EP followed by setting the
> >> transfer ring dequeue pointer via TRB_SET_DEQ. This allows to properly
> >> recover from a USB stall error and continue communicating with the USB
> >> device.
> >>
> >> Signed-off-by: Stefan Agner <stefan@agner.ch>
> >
> > I hope to get AB/RB from Bin here, then it can go into this release I think.
>
> Any chance you could have a look at this to get it into this release :)
>

Sorry I missed this. I suspect it's too late for 2022.01 to include
such big changes :(

Regards,
Bin

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] usb: xhci: reset endpoint on USB stall
  2022-01-05  1:21     ` Bin Meng
@ 2022-01-24 14:23       ` Stefan Agner
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Agner @ 2022-01-24 14:23 UTC (permalink / raw)
  To: Bin Meng; +Cc: U-Boot Mailing List, mike.hoogstraten, Marek Vasut

Hi Bin,

On 2022-01-05 02:21, Bin Meng wrote:
> Hi Stefan,
> 
> On Wed, Jan 5, 2022 at 3:48 AM Stefan Agner <stefan@agner.ch> wrote:
>>
>> Bin Meng,
>>
>> On 2021-09-27 17:14, Marek Vasut wrote:
>> > On 9/27/21 2:42 PM, Stefan Agner wrote:
>> >> There are devices which cause a USB stall when trying to read strings.
>> >> Specifically Arduino Mega R3 stalls when trying to read the product
>> >> string.
>> >>
>> >> The stall currently remains unhandled, and subsequent retries submit new
>> >> transfers on a stopped endpoint which ultimately cause a crash in
>> >> abort_td():
>> >> WARN halted endpoint, queueing URB anyway.
>> >> XHCI control transfer timed out, aborting...
>> >> Unexpected XHCI event TRB, skipping... (3affe040 00000000 13000000 02008401)
>> >> BUG at drivers/usb/host/xhci-ring.c:505/abort_td()!
>> >> BUG!
>> >> resetting ...
>> >>
>> >> Linux seems to be able to recover from the stall by issuing a
>> >> TRB_RESET_EP command.
>> >>
>> >> Introduce reset_ep() which issues a TRB_RESET_EP followed by setting the
>> >> transfer ring dequeue pointer via TRB_SET_DEQ. This allows to properly
>> >> recover from a USB stall error and continue communicating with the USB
>> >> device.
>> >>
>> >> Signed-off-by: Stefan Agner <stefan@agner.ch>
>> >
>> > I hope to get AB/RB from Bin here, then it can go into this release I think.
>>
>> Any chance you could have a look at this to get it into this release :)
>>
> 
> Sorry I missed this. I suspect it's too late for 2022.01 to include
> such big changes :(

I understand. Now that 2022.01 is out, could you have a look at this?

--
Stefan

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] usb: xhci: reset endpoint on USB stall
  2021-09-27 12:42 [PATCH] usb: xhci: reset endpoint on USB stall Stefan Agner
  2021-09-27 15:14 ` Marek Vasut
@ 2022-02-16  7:53 ` Bin Meng
  2022-02-16 15:53   ` Marek Vasut
  1 sibling, 1 reply; 7+ messages in thread
From: Bin Meng @ 2022-02-16  7:53 UTC (permalink / raw)
  To: Stefan Agner; +Cc: U-Boot Mailing List, Mike Hoogstraten, Marek Vasut

On Mon, Sep 27, 2021 at 8:43 PM Stefan Agner <stefan@agner.ch> wrote:
>
> There are devices which cause a USB stall when trying to read strings.
> Specifically Arduino Mega R3 stalls when trying to read the product
> string.
>
> The stall currently remains unhandled, and subsequent retries submit new
> transfers on a stopped endpoint which ultimately cause a crash in
> abort_td():
> WARN halted endpoint, queueing URB anyway.
> XHCI control transfer timed out, aborting...
> Unexpected XHCI event TRB, skipping... (3affe040 00000000 13000000 02008401)
> BUG at drivers/usb/host/xhci-ring.c:505/abort_td()!
> BUG!
> resetting ...
>
> Linux seems to be able to recover from the stall by issuing a
> TRB_RESET_EP command.
>
> Introduce reset_ep() which issues a TRB_RESET_EP followed by setting the
> transfer ring dequeue pointer via TRB_SET_DEQ. This allows to properly
> recover from a USB stall error and continue communicating with the USB
> device.
>
> Signed-off-by: Stefan Agner <stefan@agner.ch>
> ---
> This has been reported on the ML by Mike Hoogstraten a while ago:
> https://www.mail-archive.com/u-boot@lists.denx.de/msg390604.html
>
> FWIW, for the Arduino Mega R3 I also opened an issue:
> https://github.com/arduino/ArduinoCore-avr/issues/431
>
> --
> Stefan
>
>  drivers/usb/host/xhci-ring.c | 31 +++++++++++++++++++++++++++++++
>  1 file changed, 31 insertions(+)
>

Sorry for the delay!

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] usb: xhci: reset endpoint on USB stall
  2022-02-16  7:53 ` Bin Meng
@ 2022-02-16 15:53   ` Marek Vasut
  0 siblings, 0 replies; 7+ messages in thread
From: Marek Vasut @ 2022-02-16 15:53 UTC (permalink / raw)
  To: Bin Meng, Stefan Agner; +Cc: U-Boot Mailing List, Mike Hoogstraten

On 2/16/22 08:53, Bin Meng wrote:
> On Mon, Sep 27, 2021 at 8:43 PM Stefan Agner <stefan@agner.ch> wrote:
>>
>> There are devices which cause a USB stall when trying to read strings.
>> Specifically Arduino Mega R3 stalls when trying to read the product
>> string.
>>
>> The stall currently remains unhandled, and subsequent retries submit new
>> transfers on a stopped endpoint which ultimately cause a crash in
>> abort_td():
>> WARN halted endpoint, queueing URB anyway.
>> XHCI control transfer timed out, aborting...
>> Unexpected XHCI event TRB, skipping... (3affe040 00000000 13000000 02008401)
>> BUG at drivers/usb/host/xhci-ring.c:505/abort_td()!
>> BUG!
>> resetting ...
>>
>> Linux seems to be able to recover from the stall by issuing a
>> TRB_RESET_EP command.
>>
>> Introduce reset_ep() which issues a TRB_RESET_EP followed by setting the
>> transfer ring dequeue pointer via TRB_SET_DEQ. This allows to properly
>> recover from a USB stall error and continue communicating with the USB
>> device.
>>
>> Signed-off-by: Stefan Agner <stefan@agner.ch>

Applied, thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-02-16 15:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-27 12:42 [PATCH] usb: xhci: reset endpoint on USB stall Stefan Agner
2021-09-27 15:14 ` Marek Vasut
2022-01-04 19:48   ` Stefan Agner
2022-01-05  1:21     ` Bin Meng
2022-01-24 14:23       ` Stefan Agner
2022-02-16  7:53 ` Bin Meng
2022-02-16 15:53   ` Marek Vasut

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.