[PATCH v4 2/2] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt IN endpoints after BNA
From: Vadim Yuzifovich
Date: Sat Dec 05 2026 - 08:02:46 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. On this SoC that window can
be closed separately, by reading the descriptor back before the endpoint
is enabled, but a BNA on such an endpoint should not leave it dead on
any platform.
When a bulk or interrupt IN 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 the start of the buffer again. A
scatter-gather request is only started again from its beginning, since
its chain is always built from the first entry of the list. Report a
restart with a rate limited warning, since unlike on an isochronous
endpoint it is not expected. A BNA that leaves a request current on a
disabled endpoint and is not restarted is reported with a rate limited
warning as well, since the endpoint stays stopped until the request is
dequeued; any other BNA stays at debug level, as before.
If the endpoint still reads enabled when the BNA is handled, as on a
core that clears EPENA only after raising BNA, set SNAK and EPDIS, so
that an endpoint-disabled interrupt is sure to come, and restart from
that.
Restart a request at most three times between its start and an
XferCompl. A BNA that comes back after that points at something a
restart does not fix, such as a descriptor the core cannot read, and
restarting on would keep the CPU in the interrupt handler. Report it as
an error and leave the endpoint as before this patch.
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, and so is a
BNA on an OUT endpoint: after a BNA an isochronous OUT endpoint holds
the packet it has acknowledged and writes it to the first descriptor as
soon as that is host ready (see "usb: dwc2: gadget: restart descriptor
DMA isochronous OUT endpoints after BNA"). If a bulk OUT
endpoint does the same, filling that descriptor anew would race with the
write and could lose the packet. All the BNAs seen here that a restart
recovered were on bulk IN.
In the BNAs seen here the endpoint was disabled already when the BNA was
handled, and no endpoint-disabled interrupt followed. As a precaution,
an endpoint-disabled interrupt that comes after the restart does not
flush the TxFIFO of the endpoint, which then holds the data of the
restarted request. The restart marks the endpoint for that only when an
endpoint-disabled interrupt for the BNA is latched and not handled yet.
Only the endpoint-disabled handler and the reset paths clear the mark,
not an XferCompl or the start of the next request in the same
interrupt, which would let the flush hit the request started after the
restarted one. The TxFIFO of a halted or stalled endpoint is always
flushed, so that a halt set and cleared by the host still gets the
flush.
BNA interrupts of IN endpoints are only enabled since commit
37981e00503f ("usb: dwc2: Enable BNA interrupt for IN endpoints"). The
commit in the Fixes tag is the one that started these endpoints in
descriptor DMA mode without handling a BNA.
Fixes: aa3e8bc81311 ("usb: dwc2: gadget: DDMA transfer start and complete")
Signed-off-by: Vadim Yuzifovich <vadim@xxxxxxx>
---
drivers/usb/dwc2/core.h | 10 +++
drivers/usb/dwc2/gadget.c | 151 +++++++++++++++++++++++++++++++++++++-
2 files changed, 157 insertions(+), 4 deletions(-)
diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index d7bed6c98ec2..9c35b6f76348 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -128,6 +128,13 @@ struct dwc2_hsotg_req;
* @isoc_out_drain: Buffer for a descriptor DMA isochronous OUT endpoint to
* take a packet into that no request is queued for.
* @isoc_out_drain_dma: The DMA address of @isoc_out_drain.
+ * @bna_restarts: Restarts of the current request of a bulk or interrupt IN
+ * endpoint after a BNA in descriptor DMA mode.
+ * @bna_pending: A BNA of a bulk or interrupt IN endpoint was seen with the
+ * endpoint still enabled; it is restarted on the endpoint
+ * disabled interrupt.
+ * @bna_restarted: A bulk or interrupt IN endpoint was restarted after a BNA;
+ * its TxFIFO holds the data of the restarted request.
* @frame_overrun: Indicates SOF number overrun in DSTS
*
* This is the driver's state for each registered endpoint, allowing it
@@ -173,6 +180,9 @@ struct dwc2_hsotg_ep {
unsigned int isoc_out_drain_busy:1;
unsigned int isoc_out_drain_tries:3;
unsigned int disable_count;
+ u8 bna_restarts;
+ unsigned int bna_pending:1;
+ unsigned int bna_restarted:1;
unsigned int target_frame;
#define TARGET_FRAME_INITIAL 0xFFFFFFFF
bool frame_overrun;
diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index fdbc9677aef2..bd2e4f397f3c 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -1276,6 +1276,8 @@ static void dwc2_gadget_bna_state_reset(struct dwc2_hsotg_ep *hs_ep)
DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
hs_ep->isoc_out_drain_busy = 0;
hs_ep->isoc_out_drain_tries = 0;
+ hs_ep->bna_pending = 0;
+ hs_ep->bna_restarted = 0;
}
/*
@@ -1441,6 +1443,12 @@ static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
}
}
+ /* See dwc2_gadget_handle_nonisoc_bna() */
+ if (!continuing) {
+ hs_ep->bna_restarts = 0;
+ hs_ep->bna_pending = 0;
+ }
+
dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
@@ -2790,6 +2798,111 @@ static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
dwc2_gadget_isoc_out_bna_reset(hs_ep, start);
}
+#define DWC2_BNA_RESTARTS 3
+
+/*
+ * 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. A scatter-gather request is only started again
+ * from its beginning: dwc2_gadget_config_nonisoc_xfer_ddma() builds its chain
+ * from the first entry of the list.
+ *
+ * It is done for IN endpoints only. An OUT endpoint that raised BNA may hold
+ * a packet it has acknowledged already and write it to the first descriptor
+ * as soon as that is host ready, as an isochronous OUT endpoint does, see
+ * dwc2_gadget_isoc_out_held_packet(). Filling the descriptor anew could then
+ * race with that write and lose the packet. A BNA on an OUT endpoint is only
+ * reported.
+ *
+ * A BNA seen while the endpoint is still enabled disables the endpoint, and
+ * the restart runs from the endpoint-disabled interrupt that this is sure to
+ * bring. A BNA that leaves a request
+ * current and is not restarted is reported as a warning: the endpoint stays
+ * stopped until the request is dequeued.
+ *
+ * A request is started again at most DWC2_BNA_RESTARTS times, counted from
+ * its start or its last XferCompl. A BNA that keeps coming back points at
+ * something a restart does not fix, and restarting on would keep the CPU in
+ * the interrupt handler.
+ */
+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;
+ /* A request is current, and nothing else will move the endpoint on. */
+ bool stuck = hs_req && !hs_ep->halted &&
+ !(epctl & (DXEPCTL_EPENA | DXEPCTL_STALL));
+ bool restart = stuck && hs_ep->dir_in && hs_ep->desc_list &&
+ (!hs_req->req.num_sgs || !hs_req->req.actual) &&
+ status >> DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_HREADY;
+
+ /*
+ * Not disabled yet. Disable it, so that an endpoint-disabled
+ * interrupt is sure to come, and restart from there, see
+ * dwc2_gadget_handle_ep_disabled().
+ */
+ if (hs_ep->dir_in && hs_req && !hs_ep->halted &&
+ (epctl & DXEPCTL_EPENA)) {
+ hs_ep->bna_pending = 1;
+ dwc2_writel(hsotg, epctl | DXEPCTL_SNAK | DXEPCTL_EPDIS,
+ epctl_reg);
+ return;
+ }
+
+ if (!restart) {
+ if (stuck)
+ dev_warn_ratelimited(hsotg->dev,
+ "%s: BNA, DxEPINT %08x DxEPCTL %08x descriptor %08x, not restarted, endpoint stopped\n",
+ hs_ep->name, ints, epctl, status);
+ else
+ dev_dbg(hsotg->dev,
+ "%s: BNA, DxEPINT %08x DxEPCTL %08x descriptor %08x\n",
+ hs_ep->name, ints, epctl, status);
+ return;
+ }
+
+ if (hs_ep->bna_restarts >= DWC2_BNA_RESTARTS) {
+ dev_err_ratelimited(hsotg->dev,
+ "%s: BNA after %u restarts, descriptor %08x, not restarted\n",
+ hs_ep->name, hs_ep->bna_restarts, status);
+ return;
+ }
+ hs_ep->bna_restarts++;
+ /*
+ * An endpoint-disabled interrupt latched for this BNA and not handled
+ * yet must not flush the data of the restarted request.
+ */
+ hs_ep->bna_restarted = !(ints & DXEPINT_EPDISBLD) &&
+ (dwc2_readl(hsotg, DIEPINT(hs_ep->index)) & DXEPINT_EPDISBLD);
+
+ dev_warn_ratelimited(hsotg->dev,
+ "%s: BNA, DxEPINT %08x DxEPCTL %08x descriptor %08x, request restarted\n",
+ hs_ep->name, ints, epctl, status);
+ 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.
@@ -3392,7 +3505,15 @@ static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
if (dir_in) {
int epctl = dwc2_readl(hsotg, epctl_reg);
- dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
+ /*
+ * A bulk or interrupt endpoint restarted after a BNA, see
+ * dwc2_gadget_handle_nonisoc_bna(), has the data of the
+ * restarted request in its TxFIFO already.
+ */
+ if (!hs_ep->bna_restarted || hs_ep->halted ||
+ (epctl & DXEPCTL_STALL))
+ dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
+ hs_ep->bna_restarted = 0;
if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
int dctl = dwc2_readl(hsotg, DCTL);
@@ -3408,6 +3529,12 @@ static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
}
}
+ /* See dwc2_gadget_handle_nonisoc_bna() */
+ if (hs_ep->bna_pending) {
+ hs_ep->bna_pending = 0;
+ dwc2_gadget_handle_nonisoc_bna(hs_ep, DXEPINT_EPDISBLD);
+ }
+
if (!hs_ep->isochronous)
return;
@@ -3674,6 +3801,14 @@ static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
__func__, dwc2_readl(hsotg, epctl_reg),
dwc2_readl(hsotg, epsiz_reg));
+ /*
+ * See dwc2_gadget_handle_nonisoc_bna(). bna_restarted is left
+ * to the endpoint-disabled handler below, which may still have
+ * an interrupt of the BNA to handle.
+ */
+ hs_ep->bna_restarts = 0;
+ hs_ep->bna_pending = 0;
+
/* In DDMA handle isochronous requests separately */
if (using_desc_dma(hsotg) && hs_ep->isochronous) {
dwc2_gadget_complete_isoc_request_ddma(hs_ep);
@@ -3780,12 +3915,20 @@ 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__);
- if ((ints & DXEPINT_BNAINTR) && (!hs_ep->isochronous || dir_in)) {
+ if ((ints & DXEPINT_BNAINTR) && hs_ep->isochronous && dir_in) {
dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
- if (hs_ep->isochronous)
- dwc2_gadget_handle_isoc_bna(hs_ep);
+ dwc2_gadget_handle_isoc_bna(hs_ep);
}
+ /*
+ * 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