[PATCH v3 2/3] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt endpoints after BNA

From: Vadim Yuzifovich

Date: Tue Dec 01 2026 - 09:29:49 EST


In descriptor DMA mode the core raises BNA and disables an endpoint when
the descriptor it fetches is not marked host ready. For isochronous
endpoints dwc2 handles that; for bulk and interrupt endpoints it only
logs the interrupt at debug level. Nothing was transferred and no
XferCompl follows, so the request stays current, the requests queued
behind it never start, and the function driver waits for a completion
that never comes.

On a Sophgo SG2002 (DWC_otg 4.20a) with a video pipeline loading the
DRAM this happened every minute or two to the bulk IN endpoint of
f_mass_storage: debugfs showed the endpoint disabled with an 18-byte
REQUEST SENSE reply current and nothing done, the 13-byte CSW queued
behind it, and f_mass_storage asleep waiting for its buffer. The host's
command timed out after 30 s and it reset the port. Read in the BNA
handler, the descriptor is 0x0e000012: host ready, last, short,
interrupt on completion, 18 bytes. The chain is written completely
before the endpoint is enabled, so the core fetched the descriptor
before the CPU's stores to it reached memory. The next patch closes
that window on this SoC, but a BNA on such an endpoint should not leave
it dead on any platform.

When a bulk or interrupt endpoint other than EP0 raises BNA, is disabled
and not halted, start its current request again, which fills the
descriptor anew. Start it from what has been transferred of it, the way
the next part of a request longer than one chain is started, so that a
BNA on a later part does not send or fill the start of the buffer
again. Report the BNA with a rate limited warning instead of
at debug level: unlike on an isochronous endpoint it is not expected.

Do this only while the first descriptor of the chain still reads host
ready. If the core has taken it, a part of the chain has been
transferred that req.actual does not include yet, and starting again
would transfer that part twice. Such a BNA is only reported.

Signed-off-by: Vadim Yuzifovich <vadim@xxxxxxx>
---
drivers/usb/dwc2/gadget.c | 55 +++++++++++++++++++++++++++++++++++++++
1 file changed, 55 insertions(+)

diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index 8ca0667b4c85..3f3de2fd4590 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -2502,6 +2502,52 @@ static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
dwc2_gadget_restart_isoc_out_ddma(hs_ep);
}

+/*
+ * dwc2_gadget_handle_nonisoc_bna - handle BNA interrupt for bulk and
+ * interrupt endpoints in descriptor DMA mode.
+ * @hs_ep: The endpoint, not EP0 and not isochronous.
+ * @ints: The endpoint interrupts that came with the BNA, for the log.
+ *
+ * The core raises BNA and disables the endpoint when the descriptor it
+ * fetches is not marked host ready. The chain of a bulk or interrupt
+ * endpoint is complete before the endpoint is enabled, and afterwards the
+ * descriptor reads host ready, so the core read it before the CPU's writes
+ * to it reached memory. Nothing was transferred and no XferCompl
+ * follows: the request stays current, the ones queued behind it never
+ * start, and the function waits until the host gives up and resets the
+ * port. Start the request again from what has been transferred of it, as
+ * for the next part of a request longer than one chain; that fills the
+ * descriptor anew.
+ *
+ * This is done only while the first descriptor of the chain is still host
+ * ready. If the core has taken it, a part of the chain has been transferred
+ * that the request does not account for yet, and starting again would
+ * transfer that part twice.
+ */
+static void dwc2_gadget_handle_nonisoc_bna(struct dwc2_hsotg_ep *hs_ep,
+ u32 ints)
+{
+ struct dwc2_hsotg *hsotg = hs_ep->parent;
+ struct dwc2_hsotg_req *hs_req = hs_ep->req;
+ u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
+ DOEPCTL(hs_ep->index);
+ u32 epctl = dwc2_readl(hsotg, epctl_reg);
+ u32 status = hs_ep->desc_list ? READ_ONCE(hs_ep->desc_list[0].status) : 0;
+ bool restart = hs_req && !hs_ep->halted && hs_ep->desc_list &&
+ !(epctl & (DXEPCTL_EPENA | DXEPCTL_STALL)) &&
+ status >> DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_HREADY;
+
+ dev_warn_ratelimited(hsotg->dev,
+ "%s: BNA, DxEPINT %08x DxEPCTL %08x descriptor %08x, %s\n",
+ hs_ep->name, ints, epctl, status,
+ restart ? "request restarted" : "not restarted");
+
+ if (!restart)
+ return;
+
+ dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
+}
+
/**
* dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
* @hsotg: The device state.
@@ -3472,6 +3518,15 @@ static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
if (ints & DXEPINT_BACK2BACKSETUP)
dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);

+ /*
+ * A bulk or interrupt endpoint is restarted here, after the
+ * endpoint-disabled handler above has flushed the TxFIFO of an IN
+ * endpoint.
+ */
+ if ((ints & DXEPINT_BNAINTR) && !hs_ep->isochronous &&
+ using_desc_dma(hsotg) && idx)
+ dwc2_gadget_handle_nonisoc_bna(hs_ep, ints);
+
if (dir_in && !hs_ep->isochronous) {
/* not sure if this is important, but we'll clear it anyway */
if (ints & DXEPINT_INTKNTXFEMP) {
--
2.56.1.windows.1