mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 12/15] usb: xhci: Better error handling in abort_td()
Date: Mon, 19 Feb 2024 14:38:32 +0100	[thread overview]
Message-ID: <20240219133835.3886399-13-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240219133835.3886399-1-a.fatoum@pengutronix.de>

This ports U-Boot commit 2526cd993272966606cb64b1898343e6963fb1d9:

| Author:     Hector Martin <marcan@marcan.st>
| AuthorDate: Sun Oct 29 15:37:39 2023 +0900
|
| usb: xhci: Better error handling in abort_td()
|
| 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.
|
| 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.
|
| Signed-off-by: Hector Martin <marcan@marcan.st>

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/usb/host/xhci-ring.c | 36 +++++++++++++++++++++++-------------
 drivers/usb/host/xhci.h      |  2 ++
 2 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index d3f2018422cc..23357a882538 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -470,7 +470,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)
@@ -549,29 +550,38 @@ 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;
 	dma_addr_t addr;
 	u32 field;
 
 	xhci_queue_command(ctrl, 0, udev->slot_id, ep_index, TRB_STOP_RING);
 
-	event = xhci_wait_for_event(ctrl, TRB_TRANSFER, XHCI_TIMEOUT_DEFAULT);
+	event = xhci_wait_for_event(ctrl, TRB_NONE, XHCI_TIMEOUT_DEFAULT);
 	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, XHCI_TIMEOUT_DEFAULT);
-	if (!event)
-		return;
+		event = xhci_wait_for_event(ctrl, TRB_COMPLETION, XHCI_TIMEOUT_DEFAULT);
+		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 {
+		dev_warn(ctrl->dev, "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);
+	        event->event_cmd.status)) != COMP_SUCCESS);
 	xhci_acknowledge_event(ctrl);
 
 	addr = xhci_trb_virt_to_dma(ring->enq_seg, ring->enqueue);
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 4a08f3f9e0d2..37e8cee843cf 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -906,6 +906,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 */
-- 
2.39.2




  parent reply	other threads:[~2024-02-19 13:46 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-19 13:38 [PATCH 00/15] usb: xhci: pull in fixes from U-Boot v2024.01 Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 01/15] usb: xhci: usb: xhci: avoid type conversion of void * Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 02/15] usb: xhci: add various debugging prints Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 03/15] usb: xhci: call xhci_flush_cache where appropriate Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 04/15] usb: xhci: use macros for formatting values Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 05/15] usb: xhci: Add missing endian conversions (cpu_to_leXX / leXX_to_cpu) Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 06/15] usb: xhci: Add missing xhci_readl() Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 07/15] usb: xhci: don't use xhci_writeq for normal SDRAM Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 08/15] usb: xhci: support non-1:1 mapped xHCI Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 09/15] usb: xhci: reset endpoint on USB stall Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 10/15] usb: xhci: Fix root hub descriptor Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 11/15] usb: xhci: Guard all calls to xhci_wait_for_event Ahmad Fatoum
2024-02-19 13:38 ` Ahmad Fatoum [this message]
2024-02-19 13:38 ` [PATCH 13/15] usb: xhci: Allow context state errors when halting an endpoint Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 14/15] usb: xhci: Recover from halted bulk endpoints Ahmad Fatoum
2024-02-19 13:38 ` [PATCH 15/15] usb: xhci: Do not panic on event timeouts Ahmad Fatoum
2024-02-20 11:07 ` [PATCH 00/15] usb: xhci: pull in fixes from U-Boot v2024.01 Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240219133835.3886399-13-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox