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 09/15] usb: xhci: reset endpoint on USB stall
Date: Mon, 19 Feb 2024 14:38:29 +0100	[thread overview]
Message-ID: <20240219133835.3886399-10-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240219133835.3886399-1-a.fatoum@pengutronix.de>

This ports U-Boot commit d5daa02d8d9e7c403a3339db1966e8413e64e408:

| Author:     Stefan Agner <stefan@agner.ch>
| AuthorDate: Mon Sep 27 14:42:58 2021 +0200
|
| usb: xhci: reset endpoint on USB stall
|
| 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>

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/usb/host/xhci-ring.c | 40 ++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index c645285da7fc..056613445330 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -500,6 +500,42 @@ 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, unsigned int timeout_ms)
+{
+	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;
+	u64 addr;
+	u32 field;
+
+	dev_info(&udev->dev, "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, timeout_ms);
+	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);
+
+	addr = xhci_trb_virt_to_dma(ring->enq_seg,
+		(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, timeout_ms);
+	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);
+	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
@@ -979,6 +1015,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, timeout_ms);
+		return -EPIPE;
+	}
 
 	/* Invalidate buffer to make it available to usb-core */
 	if (length > 0)
-- 
2.39.2




  parent reply	other threads:[~2024-02-19 13:39 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 ` Ahmad Fatoum [this message]
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 ` [PATCH 12/15] usb: xhci: Better error handling in abort_td() Ahmad Fatoum
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-10-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