Skip to contentSkip to conversation

USB fixes: xHCI error handling

PATCH Superseded Hector Martin 7 participants 25 messages Oct 26, 2023 – Nov 22, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover USB fixes: xHCI error handling

This series is the first of a few bundles of USB fixes we have been carrying downstream on the Asahi U-Boot branch for a few months.

Most importantly, this related set of patches makes xHCI error/stall recovery more robust (or work at all in some cases). There are also a couple patches fixing other xHCI bugs and adding better debug logs.

I believe this should fix this Fedora bug too:

https://bugzilla.redhat.com/show_bug.cgi?id=2244305

---
Hector Martin (8):
usb: xhci: Guard all calls to xhci_wait_for_event
usb: xhci: Better error handling in abort_td()
usb: xhci: Allow context state errors when halting an endpoint
usb: xhci: Recover from halted non-control endpoints
usb: xhci: Fail on attempt to queue TRBs to a halted endpoint
usb: xhci: Do not panic on event timeouts
usb: xhci: Fix DMA address calculation in queue_trb
usb: xhci: Add more debugging

 drivers/usb/host/xhci-ring.c | 100 +++++++++++++++++++++++++++++++++++--------
 drivers/usb/host/xhci.c      |   9 ++++
 include/usb/xhci.h           |   2 +
 3 files changed, 92 insertions(+), 19 deletions(-)

---
base-commit: fb428b61819444b9337075f49c72f326f5d12085
change-id: 20231027-usb-fixes-1-83bfc7013012

Best regards,

Signature · 1 line
Hector Martin <[email protected]>
#1 lore raw

PATCH 1/8 usb: xhci: Guard all calls to xhci_wait_for_event

xhci_wait_for_event returns NULL on timeout, so the caller always has to check for that. This addresses the immediate explosions in this part of the code, but not the original cause.

---
 drivers/usb/host/xhci-ring.c | 15 ++++++++++++++-
 drivers/usb/host/xhci.c      |  9 +++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-ring.c +14 -1
index c8260cbdf94b..aaf128ff9317 100644
@@ -511,7 +511,8 @@ static void reset_ep(struct usb_device *udev, int ep_index)
 	printf("Resetting EP %d...\n", ep_index);
 	xhci_queue_command(ctrl, 0, udev->slot_id, ep_index, TRB_RESET_EP);
 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
-	field = le32_to_cpu(event->trans_event.flags);
+	if (!event)
+		return;
 	BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
 	xhci_acknowledge_event(ctrl);
 
@@ -519,6 +520,9 @@ static void reset_ep(struct usb_device *udev, int ep_index)
 		(void *)((uintptr_t)ring->enqueue | ring->cycle_state));
 	xhci_queue_command(ctrl, addr, udev->slot_id, ep_index, TRB_SET_DEQ);
 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
+	if (!event)
+		return;
+
 	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);
@@ -544,6 +548,9 @@ static void abort_td(struct usb_device *udev, int ep_index)
 	xhci_queue_command(ctrl, 0, udev->slot_id, ep_index, TRB_STOP_RING);
 
 	event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
+	if (!event)
+		return;
+
 	field = le32_to_cpu(event->trans_event.flags);
 	BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
 	BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
@@ -552,6 +559,9 @@ static void abort_td(struct usb_device *udev, int ep_index)
 	xhci_acknowledge_event(ctrl);
 
 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
+	if (!event)
+		return;
+
 	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);
@@ -561,6 +571,9 @@ static void abort_td(struct usb_device *udev, int ep_index)
 		(void *)((uintptr_t)ring->enqueue | ring->cycle_state));
 	xhci_queue_command(ctrl, addr, udev->slot_id, ep_index, TRB_SET_DEQ);
 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
+	if (!event)
+		return;
+
 	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);
drivers/usb/host/xhci.c +9 -0
index 5cacf0769ec7..d13cbff9b372 100644
@@ -451,6 +451,9 @@ static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
 	xhci_queue_command(ctrl, in_ctx->dma, udev->slot_id, 0,
 			   ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
+	if (!event)
+		return -ETIMEDOUT;
+
 	BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
 		!= udev->slot_id);
 
@@ -647,6 +650,9 @@ static int xhci_address_device(struct usb_device *udev, int root_portnr)
 	xhci_queue_command(ctrl, virt_dev->in_ctx->dma,
 			   slot_id, 0, TRB_ADDR_DEV);
 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
+	if (!event)
+		return -ETIMEDOUT;
+
 	BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
 
 	switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
@@ -722,6 +728,9 @@ static int _xhci_alloc_device(struct usb_device *udev)
 
 	xhci_queue_command(ctrl, 0, 0, 0, TRB_ENABLE_SLOT);
 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
+	if (!event)
+		return -ETIMEDOUT;
+
 	BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
 		!= COMP_SUCCESS);
 
Signature · 1 line
2.41.0
#2 lore raw

PATCH 1/1 fixup! usb: xhci: Guard all calls to xhci_wait_for_event

Gah, I should've paid more attention to that rebase. Here's a dumb fixup for this patch. I'll squash it into a v2 if needed alongside any other changes, or if it looks good feel free to apply/squash it in directly.

---
 drivers/usb/host/xhci-ring.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/host/xhci-ring.c +1 -0
index e2bd2e999a8e..7f2507be0cf0 100644
@@ -532,6 +532,7 @@ static void reset_ep(struct usb_device *udev, int ep_index)
 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
 	if (!event)
 		return;
+	field = le32_to_cpu(event->trans_event.flags);
 	BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
 	xhci_acknowledge_event(ctrl);
 
Signature · 1 line
2.41.0
Reviewed-by on v1 1/1
#3 lore raw

Re: [PATCH] fixup! usb: xhci: Guard all calls to xhci_wait_for_event

Quoted 14 lines · Hector Martin

Gah, I should've paid more attention to that rebase. Here's a dumb fixup for this patch. I'll squash it into a v2 if needed alongside any other changes, or if it looks good feel free to apply/squash it in directly.

---
  drivers/usb/host/xhci-ring.c | 1 +
  1 file changed, 1 insertion(+)
drivers/usb/host/xhci-ring.c +0 -0
index e2bd2e999a8e..7f2507be0cf0 100644

drivers/usb/host/xhci-ring.c static void reset_ep(struct usb_device *udev, int ep_index) · line 537

Quoted 6 lines
drivers/usb/host/xhci-ring.c +1 -0
@@ -532,6 +532,7 @@ static void reset_ep(struct usb_device *udev, int ep_index)
  	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  	if (!event)
  		return;
+	field = le32_to_cpu(event->trans_event.flags);
  	BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
  	xhci_acknowledge_event(ctrl);

Please squash, and add

Also, +CC Minda,

there has been a similar fix to this one but with much more information about the failure, see:

[PATCH v1] usb: xhci: Check return value of wait for TRB_TRANSFER event

I think it would be useful to somehow include that information, so it wouldn't be lost.

#4 lore raw

Re: [PATCH] fixup! usb: xhci: Guard all calls to xhci_wait_for_event

Quoted 28 lines · Marek Vasut
Quoted 14 lines · Hector Martin

Gah, I should've paid more attention to that rebase. Here's a dumb fixup for this patch. I'll squash it into a v2 if needed alongside any other changes, or if it looks good feel free to apply/squash it in directly.

---
  drivers/usb/host/xhci-ring.c | 1 +
  1 file changed, 1 insertion(+)
drivers/usb/host/xhci-ring.c +0 -0
index e2bd2e999a8e..7f2507be0cf0 100644

drivers/usb/host/xhci-ring.c static void reset_ep(struct usb_device *udev, int ep_index) · line 537

Quoted 6 lines
drivers/usb/host/xhci-ring.c +1 -0
@@ -532,6 +532,7 @@ static void reset_ep(struct usb_device *udev, int ep_index)
  	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
  	if (!event)
  		return;
+	field = le32_to_cpu(event->trans_event.flags);
  	BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
  	xhci_acknowledge_event(ctrl);

Please squash, and add

Also, +CC Minda,

Quoted 7 lines

there has been a similar fix to this one but with much more information about the failure, see:

[PATCH v1] usb: xhci: Check return value of wait for TRB_TRANSFER event

I think it would be useful to somehow include that information, so it wouldn't be lost.

The root cause for *that* failure is what I fix in patch #5. From that thread:

Quoted 1 line

scanning bus xhci_pci for devices... WARN halted endpoint, queueing

URB anyway.

Without #5 and without #1, that situation then leads to fireworks.

A bunch of things are broken, which is why this is a series and not a single patch. I have more fixes to timeout handling, mass storage, etc. coming up after this. I fixed most of this in one long day of trying random USB devices, so it's not so much subtle super specific problems I could document the crashes for as just wide-ranging problems in the u-boot USB stack. None of this is particularly hard to repro if you just try a bunch of normal consumer USB devices, including things like USB HDDs which take time to spin-up leading to timeouts etc. The crash dumps aren't particularly useful, it's the subtleties of the xHCI interactions that are, but for that you need to add and enable a lot more debugging (patch #8).

I think the main reason all this stuff is broken and we're finding out now is that u-boot has traditionally been used in embedded devices with fairly narrow use cases for USB, and now we're running it on workstation-grade laptops and desktops and people are plugging in all kinds of normal USB devices and expecting their bootloader not to freak out with them (even though most of the time it doesn't need to talk to them). It's really easy to run into this stuff when that's your use case.

- Hector

#5 lore raw

Re: [PATCH] fixup! usb: xhci: Guard all calls to xhci_wait_for_event

Quoted 45 lines · Hector Martin
Quoted 28 lines · Marek Vasut
Quoted 14 lines · Hector Martin

Gah, I should've paid more attention to that rebase. Here's a dumb fixup for this patch. I'll squash it into a v2 if needed alongside any other changes, or if it looks good feel free to apply/squash it in directly.

---
   drivers/usb/host/xhci-ring.c | 1 +
   1 file changed, 1 insertion(+)
drivers/usb/host/xhci-ring.c +0 -0
index e2bd2e999a8e..7f2507be0cf0 100644

drivers/usb/host/xhci-ring.c static void reset_ep(struct usb_device *udev, int ep_index) · line 537

Quoted 6 lines
drivers/usb/host/xhci-ring.c +1 -0
@@ -532,6 +532,7 @@ static void reset_ep(struct usb_device *udev, int ep_index)
   	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
   	if (!event)
   		return;
+	field = le32_to_cpu(event->trans_event.flags);
   	BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
   	xhci_acknowledge_event(ctrl);

Please squash, and add

Also, +CC Minda,

Quoted 7 lines

there has been a similar fix to this one but with much more information about the failure, see:

[PATCH v1] usb: xhci: Check return value of wait for TRB_TRANSFER event

I think it would be useful to somehow include that information, so it wouldn't be lost.

The root cause for *that* failure is what I fix in patch #5. From that thread:

Quoted 1 line

scanning bus xhci_pci for devices... WARN halted endpoint, queueing

URB anyway.

Without #5 and without #1, that situation then leads to fireworks.

Quoted 8 lines

A bunch of things are broken, which is why this is a series and not a single patch. I have more fixes to timeout handling, mass storage, etc. coming up after this. I fixed most of this in one long day of trying random USB devices, so it's not so much subtle super specific problems I could document the crashes for as just wide-ranging problems in the u-boot USB stack. None of this is particularly hard to repro if you just try a bunch of normal consumer USB devices, including things like USB HDDs which take time to spin-up leading to timeouts etc.

I think majority of users I can think of use USB mass storage devices, like USB pen drives, not so much HDDs as far as I can tell.

Quoted 4 lines

The crash dumps
aren't particularly useful, it's the subtleties of the xHCI interactions that are, but for that you need to add and enable a lot more debugging (patch #8).

The crash dumps are more for posterity, when someone searches for a problem they see. Search engines tend to pick those up and it might point those people in the right direction.

Also, it is good to include information about what triggered the crash, e.g. which USB device on which hardware, in case this is needed in the future.

Quoted 3 lines

I think the main reason all this stuff is broken and we're finding out now is that u-boot has traditionally been used in embedded devices with fairly narrow use cases for USB

Yes

Quoted 5 lines

, and now we're running it on
workstation-grade laptops and desktops and people are plugging in all kinds of normal USB devices and expecting their bootloader not to freak out with them (even though most of the time it doesn't need to talk to them). It's really easy to run into this stuff when that's your use case.

It is really appreciated to see people fix things like this stuff, thanks !

#6 lore raw

Re: [PATCH] fixup! usb: xhci: Guard all calls to xhci_wait_for_event

Quoted 56 lines · Marek Vasut
Quoted 45 lines · Hector Martin
Quoted 28 lines · Marek Vasut
Quoted 14 lines · Hector Martin

Gah, I should've paid more attention to that rebase. Here's a dumb fixup for this patch. I'll squash it into a v2 if needed alongside any other changes, or if it looks good feel free to apply/squash it in directly.

---
   drivers/usb/host/xhci-ring.c | 1 +
   1 file changed, 1 insertion(+)
drivers/usb/host/xhci-ring.c +0 -0
index e2bd2e999a8e..7f2507be0cf0 100644

drivers/usb/host/xhci-ring.c static void reset_ep(struct usb_device *udev, int ep_index) · line 537

Quoted 6 lines
drivers/usb/host/xhci-ring.c +1 -0
@@ -532,6 +532,7 @@ static void reset_ep(struct usb_device *udev, int ep_index)
    event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
    if (!event)
            return;
+   field = le32_to_cpu(event->trans_event.flags);
    BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
    xhci_acknowledge_event(ctrl);

Please squash, and add

Also, +CC Minda,

Quoted 7 lines

there has been a similar fix to this one but with much more information about the failure, see:

[PATCH v1] usb: xhci: Check return value of wait for TRB_TRANSFER event

I think it would be useful to somehow include that information, so it wouldn't be lost.

The root cause for *that* failure is what I fix in patch #5. From that thread:

Quoted 1 line

scanning bus xhci_pci for devices... WARN halted endpoint, queueing

URB anyway.

Without #5 and without #1, that situation then leads to fireworks.

Quoted 8 lines

A bunch of things are broken, which is why this is a series and not a single patch. I have more fixes to timeout handling, mass storage, etc. coming up after this. I fixed most of this in one long day of trying random USB devices, so it's not so much subtle super specific problems I could document the crashes for as just wide-ranging problems in the u-boot USB stack. None of this is particularly hard to repro if you just try a bunch of normal consumer USB devices, including things like USB HDDs which take time to spin-up leading to timeouts etc.

Quoted 2 lines

I think majority of users I can think of use USB mass storage devices, like USB pen drives, not so much HDDs as far as I can tell.

We see a bunch of spinning rust users in Fedora, these sorts of devices are used by a bunch of people that want to run up cheap home NAS devices so from experience I'd say while not usual to be USB sticks and some form of solid state storage, spinning disk isn't unusual.

Quoted 26 lines
Quoted 4 lines

The crash dumps
aren't particularly useful, it's the subtleties of the xHCI interactions that are, but for that you need to add and enable a lot more debugging (patch #8).

The crash dumps are more for posterity, when someone searches for a problem they see. Search engines tend to pick those up and it might point those people in the right direction.

Also, it is good to include information about what triggered the crash, e.g. which USB device on which hardware, in case this is needed in the future.

Quoted 3 lines

I think the main reason all this stuff is broken and we're finding out now is that u-boot has traditionally been used in embedded devices with fairly narrow use cases for USB

Yes

Quoted 5 lines

, and now we're running it on
workstation-grade laptops and desktops and people are plugging in all kinds of normal USB devices and expecting their bootloader not to freak out with them (even though most of the time it doesn't need to talk to them). It's really easy to run into this stuff when that's your use case.

It is really appreciated to see people fix things like this stuff, thanks !

#7 lore raw

Re: [PATCH] fixup! usb: xhci: Guard all calls to xhci_wait_for_event

Quoted 58 lines · Peter Robinson
Quoted 56 lines · Marek Vasut
Quoted 53 lines · Hector Martin
Quoted 28 lines · Marek Vasut
Quoted 14 lines · Hector Martin

Gah, I should've paid more attention to that rebase. Here's a dumb fixup for this patch. I'll squash it into a v2 if needed alongside any other changes, or if it looks good feel free to apply/squash it in directly.

---
   drivers/usb/host/xhci-ring.c | 1 +
   1 file changed, 1 insertion(+)
drivers/usb/host/xhci-ring.c +0 -0
index e2bd2e999a8e..7f2507be0cf0 100644

drivers/usb/host/xhci-ring.c static void reset_ep(struct usb_device *udev, int ep_index) · line 537

Quoted 6 lines
drivers/usb/host/xhci-ring.c +1 -0
@@ -532,6 +532,7 @@ static void reset_ep(struct usb_device *udev, int ep_index)
    event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
    if (!event)
            return;
+   field = le32_to_cpu(event->trans_event.flags);
    BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
    xhci_acknowledge_event(ctrl);

Please squash, and add

Also, +CC Minda,

Quoted 7 lines

there has been a similar fix to this one but with much more information about the failure, see:

[PATCH v1] usb: xhci: Check return value of wait for TRB_TRANSFER event

I think it would be useful to somehow include that information, so it wouldn't be lost.

The root cause for *that* failure is what I fix in patch #5. From that thread:

Quoted 1 line

scanning bus xhci_pci for devices... WARN halted endpoint, queueing

URB anyway.

Without #5 and without #1, that situation then leads to fireworks.

A bunch of things are broken, which is why this is a series and not a single patch. I have more fixes to timeout handling, mass storage, etc. coming up after this. I fixed most of this in one long day of trying random USB devices, so it's not so much subtle super specific problems I could document the crashes for as just wide-ranging problems in the u-boot USB stack. None of this is particularly hard to repro if you just try a bunch of normal consumer USB devices, including things like USB HDDs which take time to spin-up leading to timeouts etc.

Quoted 8 lines
Quoted 2 lines

I think majority of users I can think of use USB mass storage devices, like USB pen drives, not so much HDDs as far as I can tell.

We see a bunch of spinning rust users in Fedora, these sorts of devices are used by a bunch of people that want to run up cheap home NAS devices so from experience I'd say while not usual to be USB sticks and some form of solid state storage, spinning disk isn't unusual.

What Peter said. A very common use case, even more so than USB flash drives, is for the consumers to plug in a USB HDD to their routers or home NAS, as a cheap and quick solution. I've seen this type of timeout failure happen quite often with large >= 3TB HDDs in USB enclosure.

All the best,
Tony

Quoted 27 lines
Quoted 27 lines
Quoted 5 lines

The crash dumps
aren't particularly useful, it's the subtleties of the xHCI interactions that are, but for that you need to add and enable a lot more debugging (patch #8).

The crash dumps are more for posterity, when someone searches for a problem they see. Search engines tend to pick those up and it might point those people in the right direction.

Also, it is good to include information about what triggered the crash, e.g. which USB device on which hardware, in case this is needed in the future.

Quoted 3 lines

I think the main reason all this stuff is broken and we're finding out now is that u-boot has traditionally been used in embedded devices with fairly narrow use cases for USB

Yes

Quoted 5 lines

, and now we're running it on
workstation-grade laptops and desktops and people are plugging in all kinds of normal USB devices and expecting their bootloader not to freak out with them (even though most of the time it doesn't need to talk to them). It's really easy to run into this stuff when that's your use case.

It is really appreciated to see people fix things like this stuff, thanks !

#8 lore raw

PATCH 2/8 usb: xhci: Better error handling in abort_td()

In reply to Hector Martin

If the xHC has a problem with our STOP ENDPOINT command, it is likely to return a completion directly instead of first a transfer event for the in-progress transfer. Handle that more gracefully.

Right now we still BUG() on the error code, but at least we don't end up timing out on the event and ending up with unexpected event errors.

---
 drivers/usb/host/xhci-ring.c | 34 ++++++++++++++++++++++------------
 include/usb/xhci.h           |  2 ++
 2 files changed, 24 insertions(+), 12 deletions(-)
drivers/usb/host/xhci-ring.c +22 -12
index aaf128ff9317..d08bb8e2bfba 100644
@@ -466,7 +466,8 @@ union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected)
 			continue;
 
 		type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
-		if (type == expected)
+		if (type == expected ||
+		    (expected == TRB_NONE && type != TRB_PORT_STATUS))
 			return event;
 
 		if (type == TRB_PORT_STATUS)
@@ -542,27 +543,36 @@ static void abort_td(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;
+	trb_type type;
 	u64 addr;
 	u32 field;
 
 	xhci_queue_command(ctrl, 0, udev->slot_id, ep_index, TRB_STOP_RING);
 
-	event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
+	event = xhci_wait_for_event(ctrl, TRB_NONE);
 	if (!event)
 		return;
 
-	field = le32_to_cpu(event->trans_event.flags);
-	BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
-	BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
-	BUG_ON(GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len
-		!= COMP_STOP)));
-	xhci_acknowledge_event(ctrl);
+	type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
+	if (type == TRB_TRANSFER) {
+		field = le32_to_cpu(event->trans_event.flags);
+		BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
+		BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
+		BUG_ON(GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len
+			!= COMP_STOP)));
+		xhci_acknowledge_event(ctrl);
 
-	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
-	if (!event)
-		return;
+		event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
+		if (!event)
+			return;
+		type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
 
-	BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
+	} else {
+		printf("abort_td: Expected a TRB_TRANSFER TRB first\n");
+	}
+
+	BUG_ON(type != TRB_COMPLETION ||
+		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);
include/usb/xhci.h +2 -0
index 4a4ac10229ac..04d16a256bbd 100644
@@ -901,6 +901,8 @@ union xhci_trb {
 
 /* TRB type IDs */
 typedef enum {
+	/* reserved, used as a software sentinel */
+	TRB_NONE = 0,
 	/* bulk, interrupt, isoc scatter/gather, and control data stage */
 	TRB_NORMAL = 1,
 	/* setup stage for control transfers */
Signature · 1 line
2.41.0
#9 lore raw

Re: [PATCH 2/8] usb: xhci: Better error handling in abort_td()

Quoted 8 lines · Hector Martin

If the xHC has a problem with our STOP ENDPOINT command, it is likely to return a completion directly instead of first a transfer event for the in-progress transfer. Handle that more gracefully.

Right now we still BUG() on the error code, but at least we don't end up timing out on the event and ending up with unexpected event errors.

I'll defer the rest of the review to Bin , he just knows better when it comes to xHCI . Please ping me if things are stuck for too long however.

#10 lore raw

PATCH 3/8 usb: xhci: Allow context state errors when halting an endpoint

In reply to Hector Martin

There is a race where an endpoint may halt by itself while we are trying to halt it, which results in a context state error. See xHCI 4.6.9 which mentions this case.

This also avoids BUGging when we attempt to stop an endpoint which was already stopped to begin with, which is probably a bug elsewhere but not a good reason to crash.

---
 drivers/usb/host/xhci-ring.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-ring.c +4 -2
index d08bb8e2bfba..5c2d16b58589 100644
@@ -543,6 +543,7 @@ static void abort_td(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;
+	xhci_comp_code comp;
 	trb_type type;
 	u64 addr;
 	u32 field;
@@ -571,10 +572,11 @@ static void abort_td(struct usb_device *udev, int ep_index)
 		printf("abort_td: Expected a TRB_TRANSFER TRB first\n");
 	}
 
+	comp = GET_COMP_CODE(le32_to_cpu(event->event_cmd.status));
 	BUG_ON(type != TRB_COMPLETION ||
 		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);
+		!= udev->slot_id || (comp != COMP_SUCCESS && comp
+		!= COMP_CTX_STATE));
 	xhci_acknowledge_event(ctrl);
 
 	addr = xhci_trb_virt_to_dma(ring->enq_seg,
Signature · 1 line
2.41.0
#11 lore raw

PATCH 4/8 usb: xhci: Recover from halted non-control endpoints

In reply to Hector Martin

There is currently no codepath to recover from this case. In principle we could require that the upper layer do this explicitly, but let's just do it in xHCI when the next bulk transfer is started, since that reasonably implies whatever caused the problem has been dealt with.

---
 drivers/usb/host/xhci-ring.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/usb/host/xhci-ring.c +8 -0
index 5c2d16b58589..60f2cf72dffa 100644
@@ -669,6 +669,14 @@ int xhci_bulk_tx(struct usb_device *udev, unsigned long pipe,
 
 	ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index);
 
+	/*
+	 * If the endpoint was halted due to a prior error, resume it before
+	 * the next transfer. It is the responsibility of the upper layer to
+	 * have dealt with whatever caused the error.
+	 */
+	if ((le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK) == EP_STATE_HALTED)
+		reset_ep(udev, ep_index);
+
 	ring = virt_dev->eps[ep_index].ring;
 	/*
 	 * How much data is (potentially) left before the 64KB boundary?
Signature · 1 line
2.41.0
#12 lore raw

PATCH 5/8 usb: xhci: Fail on attempt to queue TRBs to a halted endpoint

In reply to Hector Martin

This isn't going to work, don't pretend it will and then end up timing out.

---
 drivers/usb/host/xhci-ring.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-ring.c +2 -1
index 60f2cf72dffa..3ef8c2f14ccc 100644
@@ -243,7 +243,8 @@ static int prepare_ring(struct xhci_ctrl *ctrl, struct xhci_ring *ep_ring,
 		puts("WARN waiting for error on ep to be cleared\n");
 		return -EINVAL;
 	case EP_STATE_HALTED:
-		puts("WARN halted endpoint, queueing URB anyway.\n");
+		puts("WARN endpoint is halted\n");
+		return -EINVAL;
 	case EP_STATE_STOPPED:
 	case EP_STATE_RUNNING:
 		debug("EP STATE RUNNING.\n");
Signature · 1 line
2.41.0
#13 lore raw

PATCH 6/8 usb: xhci: Do not panic on event timeouts

In reply to Hector Martin

Now that we always check the return value, just return NULL on timeouts. We can still log the error since this is a problem, but it's not reason to panic.

---
 drivers/usb/host/xhci-ring.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/usb/host/xhci-ring.c +3 -2
index 3ef8c2f14ccc..b95f20642943 100644
@@ -494,8 +494,9 @@ union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected)
 	if (expected == TRB_TRANSFER)
 		return NULL;
 
-	printf("XHCI timeout on event type %d... cannot recover.\n", expected);
-	BUG();
+	printf("XHCI timeout on event type %d...\n", expected);
+
+	return NULL;
 }
 
 /*
Signature · 1 line
2.41.0
#14 lore raw

PATCH 7/8 usb: xhci: Fix DMA address calculation in queue_trb

In reply to Hector Martin

We need to get the DMA address before incrementing the pointer, as that might move us onto another segment.

---
 drivers/usb/host/xhci-ring.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/host/xhci-ring.c +5 -1
index b95f20642943..b9518e4a6743 100644
@@ -202,18 +202,22 @@ static dma_addr_t queue_trb(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
 			    bool more_trbs_coming, unsigned int *trb_fields)
 {
 	struct xhci_generic_trb *trb;
+	dma_addr_t addr;
 	int i;
 
 	trb = &ring->enqueue->generic;
 
+
 	for (i = 0; i < 4; i++)
 		trb->field[i] = cpu_to_le32(trb_fields[i]);
 
 	xhci_flush_cache((uintptr_t)trb, sizeof(struct xhci_generic_trb));
 
+	addr = xhci_trb_virt_to_dma(ring->enq_seg, (union xhci_trb *)trb);
+
 	inc_enq(ctrl, ring, more_trbs_coming);
 
-	return xhci_trb_virt_to_dma(ring->enq_seg, (union xhci_trb *)trb);
+	return addr;
 }
 
 /**
Signature · 1 line
2.41.0
#15 lore raw

PATCH 8/8 usb: xhci: Add more debugging

In reply to Hector Martin

A bunch of miscellaneous debug messages to aid in working out USB issues.

---
 drivers/usb/host/xhci-ring.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
drivers/usb/host/xhci-ring.c +26 -3
index b9518e4a6743..e2bd2e999a8e 100644
@@ -215,6 +215,9 @@ static dma_addr_t queue_trb(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
 
 	addr = xhci_trb_virt_to_dma(ring->enq_seg, (union xhci_trb *)trb);
 
+	debug("trb @ %llx: %08x %08x %08x %08x\n", addr,
+	      trb_fields[0], trb_fields[1], trb_fields[2], trb_fields[3]);
+
 	inc_enq(ctrl, ring, more_trbs_coming);
 
 	return addr;
@@ -297,6 +300,8 @@ void xhci_queue_command(struct xhci_ctrl *ctrl, dma_addr_t addr, u32 slot_id,
 {
 	u32 fields[4];
 
+	debug("CMD: %llx 0x%x 0x%x %d\n", addr, slot_id, ep_index, cmd);
+
 	BUG_ON(prepare_ring(ctrl, ctrl->cmd_ring, EP_STATE_RUNNING));
 
 	fields[0] = lower_32_bits(addr);
@@ -472,8 +477,14 @@ union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected)
 
 		type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
 		if (type == expected ||
-		    (expected == TRB_NONE && type != TRB_PORT_STATUS))
+		    (expected == TRB_NONE && type != TRB_PORT_STATUS)) {
+			debug("Event: %08x %08x %08x %08x\n",
+			      le32_to_cpu(event->generic.field[0]),
+			      le32_to_cpu(event->generic.field[1]),
+			      le32_to_cpu(event->generic.field[2]),
+			      le32_to_cpu(event->generic.field[3]));
 			return event;
+		}
 
 		if (type == TRB_PORT_STATUS)
 		/* TODO: remove this once enumeration has been reworked */
@@ -485,8 +496,9 @@ union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected)
 				le32_to_cpu(event->generic.field[2])) !=
 								COMP_SUCCESS);
 		else
-			printf("Unexpected XHCI event TRB, skipping... "
+			printf("Unexpected XHCI event TRB, expected %d... "
 				"(%08x %08x %08x %08x)\n",
+				expected,
 				le32_to_cpu(event->generic.field[0]),
 				le32_to_cpu(event->generic.field[1]),
 				le32_to_cpu(event->generic.field[2]),
@@ -601,10 +613,13 @@ static void abort_td(struct usb_device *udev, int ep_index)
 static void record_transfer_result(struct usb_device *udev,
 				   union xhci_trb *event, int length)
 {
+	xhci_comp_code code = GET_COMP_CODE(
+		le32_to_cpu(event->trans_event.transfer_len));
+
 	udev->act_len = min(length, length -
 		(int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len)));
 
-	switch (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))) {
+	switch (code) {
 	case COMP_SUCCESS:
 		BUG_ON(udev->act_len != length);
 		/* fallthrough */
@@ -612,16 +627,23 @@ static void record_transfer_result(struct usb_device *udev,
 		udev->status = 0;
 		break;
 	case COMP_STALL:
+		debug("Xfer STALL\n");
 		udev->status = USB_ST_STALLED;
 		break;
 	case COMP_DB_ERR:
+		debug("Xfer DB_ERR\n");
+		udev->status = USB_ST_BUF_ERR;
+		break;
 	case COMP_TRB_ERR:
+		debug("Xfer TRB_ERR\n");
 		udev->status = USB_ST_BUF_ERR;
 		break;
 	case COMP_BABBLE:
+		debug("Xfer BABBLE\n");
 		udev->status = USB_ST_BABBLE_DET;
 		break;
 	default:
+		debug("Xfer error: %d\n", code);
 		udev->status = 0x80;  /* USB_ST_TOO_LAZY_TO_MAKE_A_NEW_MACRO */
 	}
 }
@@ -1015,6 +1037,7 @@ 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) {
+		debug("EP %d stalled\n", ep_index);
 		reset_ep(udev, ep_index);
 		return -EPIPE;
 	}
Signature · 1 line
2.41.0
Reviewed-by on the v1 series
#16 lore raw
In reply to Hector Martin
Quoted 24 lines · Hector Martin

This series is the first of a few bundles of USB fixes we have been carrying downstream on the Asahi U-Boot branch for a few months.

Most importantly, this related set of patches makes xHCI error/stall recovery more robust (or work at all in some cases). There are also a couple patches fixing other xHCI bugs and adding better debug logs.

I believe this should fix this Fedora bug too:

https://bugzilla.redhat.com/show_bug.cgi?id=2244305

---
Hector Martin (8):
usb: xhci: Guard all calls to xhci_wait_for_event
usb: xhci: Better error handling in abort_td()
usb: xhci: Allow context state errors when halting an endpoint
usb: xhci: Recover from halted non-control endpoints
usb: xhci: Fail on attempt to queue TRBs to a halted endpoint
usb: xhci: Do not panic on event timeouts
usb: xhci: Fix DMA address calculation in queue_trb
usb: xhci: Add more debugging

Quoted 8 lines
 drivers/usb/host/xhci-ring.c | 100 +++++++++++++++++++++++++++++++++++--------
 drivers/usb/host/xhci.c      |   9 ++++
 include/usb/xhci.h           |   2 +
 3 files changed, 92 insertions(+), 19 deletions(-)

---
base-commit: fb428b61819444b9337075f49c72f326f5d12085
change-id: 20231027-usb-fixes-1-83bfc7013012

The series looks reasonable and has worked quite well in Fedora Asahi.

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!
Reviewed-by
#17 lore raw
Quoted 35 lines · Neal Gompa
Quoted 24 lines · Hector Martin

This series is the first of a few bundles of USB fixes we have been carrying downstream on the Asahi U-Boot branch for a few months.

Most importantly, this related set of patches makes xHCI error/stall recovery more robust (or work at all in some cases). There are also a couple patches fixing other xHCI bugs and adding better debug logs.

I believe this should fix this Fedora bug too:

https://bugzilla.redhat.com/show_bug.cgi?id=2244305

---
Hector Martin (8):
usb: xhci: Guard all calls to xhci_wait_for_event
usb: xhci: Better error handling in abort_td()
usb: xhci: Allow context state errors when halting an endpoint
usb: xhci: Recover from halted non-control endpoints
usb: xhci: Fail on attempt to queue TRBs to a halted endpoint
usb: xhci: Do not panic on event timeouts
usb: xhci: Fix DMA address calculation in queue_trb
usb: xhci: Add more debugging

Quoted 8 lines
 drivers/usb/host/xhci-ring.c | 100 +++++++++++++++++++++++++++++++++++--------
 drivers/usb/host/xhci.c      |   9 ++++
 include/usb/xhci.h           |   2 +
 3 files changed, 92 insertions(+), 19 deletions(-)

---
base-commit: fb428b61819444b9337075f49c72f326f5d12085
change-id: 20231027-usb-fixes-1-83bfc7013012

Quoted 4 lines

The series looks reasonable and has worked quite well in Fedora Asahi.

Resending now that I'm subscribed to the U-Boot mailing list...

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!
#18 lore raw
In reply to Hector Martin
Quoted 10 lines · Hector Martin

This series is the first of a few bundles of USB fixes we have been carrying downstream on the Asahi U-Boot branch for a few months.

Most importantly, this related set of patches makes xHCI error/stall recovery more robust (or work at all in some cases). There are also a couple patches fixing other xHCI bugs and adding better debug logs.

I believe this should fix this Fedora bug too:

https://bugzilla.redhat.com/show_bug.cgi?id=2244305

Was there ever a V2 of these patches I might've missed ?

#19 lore raw
Quoted 9 lines · Marek Vasut
Quoted 7 lines · Hector Martin

This series is the first of a few bundles of USB fixes we have been carrying downstream on the Asahi U-Boot branch for a few months.

Most importantly, this related set of patches makes xHCI error/stall recovery more robust (or work at all in some cases). There are also a couple patches fixing other xHCI bugs and adding better debug logs.

Quoted 5 lines
Quoted 3 lines

I believe this should fix this Fedora bug too:

https://bugzilla.redhat.com/show_bug.cgi?id=2244305

Was there ever a V2 of these patches I might've missed ?

Is it this one?
https://patchwork.ozlabs.org/project/uboot/list/?series=379807

#20 lore raw
Quoted 10 lines · Shantur Rathore
Quoted 9 lines · Marek Vasut
Quoted 7 lines · Hector Martin

This series is the first of a few bundles of USB fixes we have been carrying downstream on the Asahi U-Boot branch for a few months.

Most importantly, this related set of patches makes xHCI error/stall recovery more robust (or work at all in some cases). There are also a couple patches fixing other xHCI bugs and adding better debug logs.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I believe this should fix this Fedora bug too:

https://bugzilla.redhat.com/show_bug.cgi?id=2244305

Was there ever a V2 of these patches I might've missed ?

Is it this one?
https://patchwork.ozlabs.org/project/uboot/list/?series=379807

I think so, thanks.

And uh, my question therefore it, is there a V3 which addresses the 3/8 and 8/8 comment ?

#21 lore raw
Quoted 17 lines · Marek Vasut
Quoted 16 lines · Shantur Rathore
Quoted 14 lines · Marek Vasut
Quoted 10 lines · Hector Martin

This series is the first of a few bundles of USB fixes we have been carrying downstream on the Asahi U-Boot branch for a few months.

Most importantly, this related set of patches makes xHCI error/stall recovery more robust (or work at all in some cases). There are also a couple patches fixing other xHCI bugs and adding better debug logs.

I believe this should fix this Fedora bug too:

https://bugzilla.redhat.com/show_bug.cgi?id=2244305

Was there ever a V2 of these patches I might've missed ?

Quoted 7 lines
Quoted 2 lines

Is it this one?
https://patchwork.ozlabs.org/project/uboot/list/?series=379807

I think so, thanks.

And uh, my question therefore it, is there a V3 which addresses the 3/8 and 8/8 comment ?

Not yet, no. Sorry, I probably won't have time to work on this in a while, currently busy with other stuff.

- Hector

#22 lore raw
Quoted 23 lines · Hector Martin
Quoted 20 lines · Marek Vasut
Quoted 18 lines · Shantur Rathore
Quoted 9 lines · Marek Vasut
Quoted 7 lines · Hector Martin

This series is the first of a few bundles of USB fixes we have been carrying downstream on the Asahi U-Boot branch for a few months.

Most importantly, this related set of patches makes xHCI error/stall recovery more robust (or work at all in some cases). There are also a couple patches fixing other xHCI bugs and adding better debug logs.

Quoted 5 lines
Quoted 3 lines

I believe this should fix this Fedora bug too:

https://bugzilla.redhat.com/show_bug.cgi?id=2244305

Was there ever a V2 of these patches I might've missed ?

Is it this one?
https://patchwork.ozlabs.org/project/uboot/list/?series=379807

Quoted 7 lines
Quoted 4 lines

I think so, thanks.

And uh, my question therefore it, is there a V3 which addresses the 3/8 and 8/8 comment ?

Not yet, no. Sorry, I probably won't have time to work on this in a while, currently busy with other stuff.

I can probably fix the patches up myself if that is fine with you, I'd really like to get these fixes into the release soon. Would that be OK with you ?

#23 lore raw
Quoted 29 lines · Marek Vasut
Quoted 28 lines · Hector Martin
Quoted 24 lines · Marek Vasut
Quoted 10 lines · Shantur Rathore
Quoted 9 lines · Marek Vasut
Quoted 7 lines · Hector Martin

This series is the first of a few bundles of USB fixes we have been carrying downstream on the Asahi U-Boot branch for a few months.

Most importantly, this related set of patches makes xHCI error/stall recovery more robust (or work at all in some cases). There are also a couple patches fixing other xHCI bugs and adding better debug logs.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I believe this should fix this Fedora bug too:

https://bugzilla.redhat.com/show_bug.cgi?id=2244305

Was there ever a V2 of these patches I might've missed ?

Is it this one?
https://patchwork.ozlabs.org/project/uboot/list/?series=379807

I think so, thanks.

And uh, my question therefore it, is there a V3 which addresses the 3/8 and 8/8 comment ?

Quoted 7 lines
Quoted 2 lines

Not yet, no. Sorry, I probably won't have time to work on this in a while, currently busy with other stuff.

I can probably fix the patches up myself if that is fine with you, I'd really like to get these fixes into the release soon. Would that be OK with you ?

Of course, I would appreciate that :)

- Hector

#24 lore raw
Quoted 35 lines · Hector Martin
Quoted 32 lines · Marek Vasut
Quoted 30 lines · Hector Martin
Quoted 17 lines · Marek Vasut
Quoted 16 lines · Shantur Rathore
Quoted 14 lines · Marek Vasut
Quoted 10 lines · Hector Martin

This series is the first of a few bundles of USB fixes we have been carrying downstream on the Asahi U-Boot branch for a few months.

Most importantly, this related set of patches makes xHCI error/stall recovery more robust (or work at all in some cases). There are also a couple patches fixing other xHCI bugs and adding better debug logs.

I believe this should fix this Fedora bug too:

https://bugzilla.redhat.com/show_bug.cgi?id=2244305

Was there ever a V2 of these patches I might've missed ?

Quoted 7 lines
Quoted 2 lines

Is it this one?
https://patchwork.ozlabs.org/project/uboot/list/?series=379807

I think so, thanks.

And uh, my question therefore it, is there a V3 which addresses the 3/8 and 8/8 comment ?

Not yet, no. Sorry, I probably won't have time to work on this in a while, currently busy with other stuff.

Quoted 6 lines
Quoted 4 lines

I can probably fix the patches up myself if that is fine with you, I'd really like to get these fixes into the release soon. Would that be OK with you ?

Of course, I would appreciate that :)

I picked a subset to usb/master and fixed a subset, for the rest I really do need your input on those patches.