* [PATCH v3 1/3] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA
2026-12-01 14:29 [PATCH v3 0/3] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002 Vadim Yuzifovich
@ 2026-12-01 14:29 ` Vadim Yuzifovich
2026-12-01 15:04 ` sashiko-bot
2026-12-01 14:29 ` [PATCH v3 2/3] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt " Vadim Yuzifovich
` (2 subsequent siblings)
3 siblings, 1 reply; 6+ messages in thread
From: Vadim Yuzifovich @ 2026-12-01 14:29 UTC (permalink / raw)
To: Thinh.Nguyen, gregkh
Cc: linux-usb, linux-kernel, chenyou910331, inochiama, sophgo
In descriptor DMA mode the core stops an isochronous endpoint when it
reaches a descriptor that is not ready (BNA), for example after an
interrupt latency longer than the queued requests cover.
dwc2_gadget_handle_isoc_bna() resets the endpoint and leaves the restart
to the next OUTTKNEPDIS. On a Sophgo SG2002 (DWC_otg 4.20a) that
interrupt does not come. The core keeps the OUT packet that met the BNA,
shows the endpoint as disabled, and tries the descriptor at the start of
the chain again and again, raising BNA each time, as fast as the handler
clears it.
With a UAC1 capture endpoint and eight requests queued, one 8 ms
interrupt latency (a line printed on the 115200 baud serial console)
caused one BNA. The endpoint stayed disabled and the BNA interrupt kept
the CPU busy until the host gave up and reset the port, which took the
other functions of the composite gadget down as well.
An endpoint-disabled interrupt after the BNA makes it worse:
dwc2_gadget_handle_ep_disabled() has an isochronous part written for
buffer DMA, which completes requests frame by frame and advances
target_frame up to the current frame. That moves target_frame off
TARGET_FRAME_INITIAL, and the OUT token handler then takes the chain for
running and never restarts it.
So restart an OUT endpoint from the BNA handler, and from the
endpoint-disabled handler when it finds the endpoint disabled with
requests queued, and do not run the buffer DMA completion in descriptor
DMA mode. IN endpoints keep restarting on the next NAK.
The restart has to deal with the packet the core holds. The core takes
the descriptor at the start of the chain the moment it is host ready,
before the endpoint is enabled, writes the packet to it and then stops:
a chain that is enabled after that takes one packet at most and raises
BNA on the next. Traced on the SG2002, a restart that just rebuilt the
chain and enabled the endpoint gave one BNA and one restart for every
packet from then on, about 2000 interrupts a second, with the audio
mostly intact; and since the chain is filled front to back, the core
took descriptor 0 while it still carried the L bit.
Therefore keep descriptor 0 host busy while the chain is filled, write
the chain address, and only then mark descriptor 0 host ready. If the
core takes it before the endpoint is enabled, wait for the packet to
land, give its request back, and build the chain once more: no packet
is waiting for that one, and it starts like the first chain of a
stream. If the packet has not landed after 100 us, warn and leave
descriptor 0 to the core: the chain is enabled as it is, the packet
completes as its first, and the BNA on the next descriptor restarts
the endpoint again. Rebuilding the chain over a descriptor the core is
still writing would lose the packet or its status. The waits are
bounded: 10 us when the core holds no packet, 110 us when it does,
once per restart.
The BNA handler must not flush the Rx FIFO for this. The FIFO is shared
by all OUT endpoints and nothing stops the host: when an OUT token
arrived while the handler ran, the flush left the core with a FIFO it
never drained again. GINTSTS.RXFLVL stayed set, every isochronous OUT
packet was dropped with OUTTKNEPDIS and no XferCompl, and bulk OUT on
the mass storage function was NAKed until its host driver timed out
and reset the port, after which EP0 did not answer. That happened about
once in 100 BNAs here. The flush does not drop the packet that met the
BNA either; the core delivers it to the new chain all the same. So
drop the flush.
A BNA can also arrive in the same interrupt as the endpoint-disabled,
OUT token or NAK interrupt that starts a chain, and since the core
keeps raising it, one is latched just before the restart and seen
after it. Handle BNA before those interrupts, and ignore a BNA for an
OUT endpoint that is enabled, so that the BNA handler never gives back
a request of a running chain or resets the endpoint state under it.
For an OUT endpoint the BNA handler gives no request back. The held
packet goes to descriptor 0 of the new chain, which belongs to the
request at the head of the queue; giving that request back first, as
dwc2 did, completed it empty and moved the packet to the next one. If
no request is queued at the BNA, or the held packet took the last one,
nothing can be started and no OUTTKNEPDIS follows, so mark the endpoint
stopped and restart it from dwc2_hsotg_ep_queue() when a request
arrives. IN endpoints keep giving the request back.
While it holds the packet, the core raises BNA again as fast as the
handler clears it. On the SG2002, with the function's requests held
back for 20 ms by a test hook, that was 1.8 million BNA interrupts in
about a second, and the work item that was to queue the next request
did not run before the host reset the port. So mask the endpoint's
interrupts while it is stopped this way, and unmask them when the chain
is started again. The BNA latched meanwhile is then seen with the
endpoint enabled and ignored.
A function driver may queue a request from the completion of another,
as f_uac1 does, and completions run while the endpoint is stopped:
those of packets that completed in the same interrupt as the BNA, and
the one of the held packet. If next_desc is 0 at that moment, the queue
call fills descriptor 0 and marks it host ready, and the core takes it
for the held packet before the chain is built. So in
dwc2_hsotg_ep_queue() keep descriptor 0 host busy while an OUT endpoint
is disabled, reset the endpoint state in the BNA handler before
anything is given back, and take the chain as not running while the
request of the held packet is given back, so that those requests only
add to the queue.
With descriptor 0 host busy the fill function does not see a chain that
is full. Stop filling when next_desc has wrapped to 0; otherwise more
requests queued than the chain has descriptors would overwrite
descriptor 0 and the ones after it.
Before the state is reset, packets that completed before the BNA but
whose XferCompl was not seen yet are given back, so that the rebuilt
chain does not reuse their requests. That is done only while a chain
runs: after a reset compl_desc points at descriptors left over from the
last chain, which may still read DMA done. dwc2_hsotg_ep_disable()
clears the stopped mark and the target frame before it kills the
requests, so that one queued from those completions neither restarts the
endpoint nor goes into its chain.
Fixes: 729cac693eec ("usb: dwc2: Change ISOC DDMA flow")
Signed-off-by: Vadim Yuzifovich <vadim@yuzi.co>
---
drivers/usb/dwc2/core.h | 5 +
drivers/usb/dwc2/gadget.c | 302 +++++++++++++++++++++++++++++++++++---
2 files changed, 289 insertions(+), 18 deletions(-)
diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index 015a5fb7842d..165ed6da348f 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -106,6 +106,10 @@ struct dwc2_hsotg_req;
* @last_load: The offset of data for the last start of request.
* @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
* @target_frame: Targeted frame num to setup next ISOC transfer
+ * @isoc_out_stopped: Descriptor DMA isochronous OUT endpoint stopped after a
+ * BNA with no request left to restart it with; its
+ * interrupts are masked and the next request queued
+ * restarts it.
* @frame_overrun: Indicates SOF number overrun in DSTS
*
* This is the driver's state for each registered endpoint, allowing it
@@ -145,6 +149,7 @@ struct dwc2_hsotg_ep {
unsigned int isochronous:1;
unsigned int send_zlp:1;
unsigned int wedged:1;
+ unsigned int isoc_out_stopped: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 6c8a9f05fff0..8ca0667b4c85 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -218,6 +218,31 @@ static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
local_irq_restore(flags);
}
+/*
+ * dwc2_gadget_isoc_out_stop - leave a descriptor DMA isochronous OUT endpoint
+ * stopped until a request is queued.
+ * @hs_ep: The endpoint, disabled after a BNA, with no request to restart it.
+ *
+ * While the core holds the packet that met the BNA it raises BNA again as
+ * fast as the handler clears it, which keeps the CPU from the function that
+ * would queue the next request. Mask the endpoint's interrupts until
+ * dwc2_gadget_isoc_out_unstop(); a BNA latched meanwhile is then seen with
+ * the new chain enabled and ignored.
+ */
+static void dwc2_gadget_isoc_out_stop(struct dwc2_hsotg_ep *hs_ep)
+{
+ hs_ep->isoc_out_stopped = 1;
+ dwc2_hsotg_ctrl_epint(hs_ep->parent, hs_ep->index, 0, 0);
+}
+
+static void dwc2_gadget_isoc_out_unstop(struct dwc2_hsotg_ep *hs_ep)
+{
+ if (!hs_ep->isoc_out_stopped)
+ return;
+ hs_ep->isoc_out_stopped = 0;
+ dwc2_hsotg_ctrl_epint(hs_ep->parent, hs_ep->index, 0, 1);
+}
+
/**
* dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
*
@@ -900,6 +925,7 @@ static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
* @hs_ep: The isochronous endpoint.
* @dma_buff: usb requests dma buffer.
* @len: usb request transfer length.
+ * @hold: leave the descriptor host busy; the caller marks it host ready.
*
* Fills next free descriptor with the data of the arrived usb request,
* frame info, sets Last and IOC bits increments next_desc. If filled
@@ -907,7 +933,8 @@ static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
* status.
*/
static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
- dma_addr_t dma_buff, unsigned int len)
+ dma_addr_t dma_buff, unsigned int len,
+ bool hold)
{
struct dwc2_dma_desc *desc;
struct dwc2_hsotg *hsotg = hs_ep->parent;
@@ -956,7 +983,8 @@ static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
}
desc->status &= ~DEV_DMA_BUFF_STS_MASK;
- desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
+ desc->status |= (hold ? DEV_DMA_BUFF_STS_HBUSY :
+ DEV_DMA_BUFF_STS_HREADY) << DEV_DMA_BUFF_STS_SHIFT;
/* Increment frame number by interval for IN */
if (hs_ep->dir_in)
@@ -970,6 +998,71 @@ static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
return 0;
}
+static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep);
+
+/*
+ * dwc2_gadget_isoc_out_held_packet - let the core finish an OUT packet it
+ * holds from before a BNA.
+ * @hs_ep: The isochronous OUT endpoint, its new chain written and descriptor 0
+ * just marked host ready, the endpoint not enabled yet.
+ *
+ * A packet that met a descriptor that was not ready stays with the core. The
+ * core raises BNA, shows the endpoint as disabled, and keeps trying the
+ * descriptor at the start of the chain. As soon as that one is host ready the
+ * core writes the packet to it, without the endpoint being enabled, and then
+ * stops: a chain enabled after that takes no further packet. So wait for the
+ * packet to land and give its request back. The caller then starts a new
+ * chain, which no packet is waiting for.
+ *
+ * If the packet has not landed within 100 us, the descriptor is left to the
+ * core: the caller enables the chain as it is, the packet completes as the
+ * first of it, and a BNA on the next descriptor restarts the endpoint again.
+ *
+ * Return: true if the core took descriptor 0 and wrote the packet to it.
+ */
+static bool dwc2_gadget_isoc_out_held_packet(struct dwc2_hsotg_ep *hs_ep)
+{
+ struct dwc2_dma_desc *desc = &hs_ep->desc_list[0];
+ u32 target_frame;
+ int i;
+
+ /* The core takes the descriptor within a few bus cycles, if at all. */
+ for (i = 0; i < 10; i++) {
+ if (READ_ONCE(desc->status) >> DEV_DMA_BUFF_STS_SHIFT !=
+ DEV_DMA_BUFF_STS_HREADY)
+ break;
+ udelay(1);
+ }
+ if (i == 10)
+ return false;
+
+ /* One packet of at most 1024 bytes: the DMA takes microseconds. */
+ for (i = 0; i < 100; i++) {
+ if (READ_ONCE(desc->status) >> DEV_DMA_BUFF_STS_SHIFT ==
+ DEV_DMA_BUFF_STS_DMADONE)
+ break;
+ udelay(1);
+ }
+ if (i == 100) {
+ dev_warn_ratelimited(hs_ep->parent->dev,
+ "%s: held packet not written in 100 us\n",
+ hs_ep->ep.name);
+ return false;
+ }
+
+ /*
+ * The completion may queue a request. Keep it out of this chain,
+ * which the caller builds again: as long as target_frame is initial,
+ * dwc2_hsotg_ep_queue() only adds the request to the queue.
+ */
+ target_frame = hs_ep->target_frame;
+ hs_ep->target_frame = TARGET_FRAME_INITIAL;
+ dwc2_gadget_complete_isoc_request_ddma(hs_ep);
+ hs_ep->target_frame = target_frame;
+
+ return true;
+}
+
/*
* dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
* @hs_ep: The isochronous endpoint.
@@ -982,6 +1075,7 @@ static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
struct dwc2_hsotg *hsotg = hs_ep->parent;
struct dwc2_hsotg_req *hs_req, *treq;
int index = hs_ep->index;
+ bool again = false;
int ret;
int i;
u32 dma_reg;
@@ -989,8 +1083,16 @@ static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
u32 ctrl;
struct dwc2_dma_desc *desc;
+restart:
if (list_empty(&hs_ep->queue)) {
hs_ep->target_frame = TARGET_FRAME_INITIAL;
+ /*
+ * The held packet took the last queued request. The endpoint
+ * is stopped with nothing to restart it, as after a BNA with
+ * an empty queue: let the next request queued restart it.
+ */
+ if (again)
+ dwc2_gadget_isoc_out_stop(hs_ep);
dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
return;
}
@@ -1003,6 +1105,12 @@ static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
<< DEV_DMA_BUFF_STS_SHIFT);
}
+ /*
+ * Descriptor 0 stays host busy until the rest of the chain is written
+ * and the chain address is set. After a BNA the core may take it the
+ * moment it is host ready, see dwc2_gadget_isoc_out_held_packet(), and
+ * must not find it as the last of a chain that is still being built.
+ */
hs_ep->next_desc = 0;
list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
dma_addr_t dma_addr = hs_req->req.dma;
@@ -1012,8 +1120,14 @@ static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
dma_addr = sg_dma_address(hs_req->req.sg);
}
ret = dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
- hs_req->req.length);
- if (ret)
+ hs_req->req.length,
+ !hs_ep->next_desc);
+ /*
+ * Stop when the chain is full and next_desc has wrapped:
+ * descriptor 0 is still host busy here, so the next call
+ * would not see the chain as full and would fill it again.
+ */
+ if (ret || !hs_ep->next_desc)
break;
}
@@ -1024,12 +1138,24 @@ static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
/* write descriptor chain address to control register */
dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
+ desc = &hs_ep->desc_list[0];
+ desc->status &= ~DEV_DMA_BUFF_STS_MASK;
+ desc->status |= DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT;
+
+ if (!hs_ep->dir_in && !again &&
+ dwc2_gadget_isoc_out_held_packet(hs_ep)) {
+ again = true;
+ goto restart;
+ }
+
+ dwc2_gadget_isoc_out_unstop(hs_ep);
ctrl = dwc2_readl(hsotg, depctl);
ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
dwc2_writel(hsotg, ctrl, depctl);
}
static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep);
+static void dwc2_gadget_restart_isoc_out_ddma(struct dwc2_hsotg_ep *hs_ep);
static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
struct dwc2_hsotg_ep *hs_ep,
struct dwc2_hsotg_req *hs_req,
@@ -1485,13 +1611,32 @@ static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
if (using_desc_dma(hs) && hs_ep->isochronous) {
if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
dma_addr_t dma_addr = hs_req->req.dma;
+ bool hold;
if (hs_req->req.num_sgs) {
WARN_ON(hs_req->req.num_sgs > 1);
dma_addr = sg_dma_address(hs_req->req.sg);
}
+
+ /*
+ * After a BNA the core retries descriptor 0 for the
+ * packet it holds. Until the BNA handler has built
+ * the chain again, a request queued from a completion
+ * must not make that descriptor host ready, see
+ * dwc2_gadget_isoc_out_held_packet().
+ */
+ hold = !hs_ep->dir_in && !hs_ep->next_desc &&
+ !(dwc2_readl(hs, DOEPCTL(hs_ep->index)) &
+ DXEPCTL_EPENA);
dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
- hs_req->req.length);
+ hs_req->req.length, hold);
+ } else if (hs_ep->isoc_out_stopped) {
+ /*
+ * A BNA stopped this OUT endpoint with nothing to
+ * restart it with. No OUTTKNEPDIS follows a BNA, so
+ * start the chain now.
+ */
+ dwc2_gadget_restart_isoc_out_ddma(hs_ep);
}
return 0;
}
@@ -2259,26 +2404,102 @@ static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
}
}
+/*
+ * dwc2_gadget_restart_isoc_out_ddma - build the chain of a disabled ISOC OUT
+ * endpoint again from its queue and enable it.
+ * @hs_ep: The isochronous OUT endpoint, in its initial state.
+ *
+ * After a BNA the core keeps raising BNA for the endpoint, as fast as the
+ * handler clears it, and no OUTTKNEPDIS comes to restart the chain. So the
+ * chain is restarted here at once.
+ */
+static void dwc2_gadget_restart_isoc_out_ddma(struct dwc2_hsotg_ep *hs_ep)
+{
+ struct dwc2_hsotg *hsotg = hs_ep->parent;
+
+ if (hs_ep->dir_in || list_empty(&hs_ep->queue) ||
+ hs_ep->target_frame != TARGET_FRAME_INITIAL ||
+ dwc2_readl(hsotg, DOEPCTL(hs_ep->index)) & DXEPCTL_EPENA)
+ return;
+
+ /*
+ * Clear the mark first: a request queued from the completion of the
+ * held packet must only be added to the queue, not start a restart of
+ * its own inside this one.
+ */
+ dwc2_gadget_isoc_out_unstop(hs_ep);
+ hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
+ dwc2_gadget_start_isoc_ddma(hs_ep);
+}
+
/*
* dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
* @hs_ep: The isochronous endpoint.
*
- * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
- * interrupt. Reset target frame and next_desc to allow to start
- * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
- * interrupt for OUT direction.
+ * Reset target frame and next_desc. For an IN endpoint, give back the request
+ * that met the BNA; the endpoint starts again on the next NAK interrupt.
+ *
+ * An OUT endpoint is restarted here, see dwc2_gadget_restart_isoc_out_ddma(),
+ * and no request is given back: the core holds the packet that met the BNA
+ * and writes it to descriptor 0 of the new chain, which belongs to the
+ * request at the head of the queue, see dwc2_gadget_isoc_out_held_packet().
+ * Giving that request back first would complete it empty, and the packet
+ * would land in the next one. With no request queued the endpoint stays
+ * stopped until dwc2_hsotg_ep_queue() restarts it.
+ *
+ * The Rx FIFO is not flushed for an OUT endpoint. It is shared by all OUT
+ * endpoints and the host goes on sending, so a flush here can hit a packet
+ * that is being received, after which the core no longer drains the FIFO.
+ * The packet that met the BNA is not dropped by a flush either: the core
+ * holds it and writes it to the first descriptor of the new chain.
*/
static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
{
struct dwc2_hsotg *hsotg = hs_ep->parent;
+ bool running;
- if (!hs_ep->dir_in)
- dwc2_flush_rx_fifo(hsotg);
- dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
+ /*
+ * The core disables an OUT endpoint that raises BNA and goes on
+ * raising BNA for it until it is enabled again. So a BNA can be
+ * latched between the moment the interrupt handler clears DOEPINT and
+ * the restart below, and is then seen with the new chain running.
+ * Leave that chain alone. If the endpoint is still about to be
+ * disabled, the endpoint-disabled interrupt or the next BNA restarts
+ * it.
+ */
+ if (!hs_ep->dir_in &&
+ dwc2_readl(hsotg, DOEPCTL(hs_ep->index)) & DXEPCTL_EPENA)
+ return;
+ /*
+ * Reset the state before the request is given back. Its completion
+ * may queue a request, and that one must go to the queue only, not
+ * to a descriptor of the chain that has stopped: for an OUT endpoint
+ * the core would take descriptor 0 for the packet it holds.
+ *
+ * Packets that completed before the BNA but whose XferCompl was not
+ * yet seen are given back first; the chain is built again from the
+ * queue below. That is done only while a chain runs: once the state
+ * has been reset, compl_desc no longer points into the chain the core
+ * used, and the descriptors there are left over from it.
+ */
+ running = hs_ep->target_frame != TARGET_FRAME_INITIAL;
hs_ep->target_frame = TARGET_FRAME_INITIAL;
+ if (!hs_ep->dir_in && running)
+ dwc2_gadget_complete_isoc_request_ddma(hs_ep);
hs_ep->next_desc = 0;
hs_ep->compl_desc = 0;
+
+ if (hs_ep->dir_in) {
+ dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep),
+ 0);
+ return;
+ }
+
+ if (list_empty(&hs_ep->queue))
+ dwc2_gadget_isoc_out_stop(hs_ep);
+ else
+ dwc2_gadget_restart_isoc_out_ddma(hs_ep);
}
/**
@@ -2902,6 +3123,36 @@ static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
if (!hs_ep->isochronous)
return;
+ /*
+ * In descriptor DMA the requests sit in the descriptor chain rather
+ * than one per frame, so the frame by frame completion below does not
+ * apply. The core disables an isochronous endpoint by itself when it
+ * meets a descriptor that was not filled in time (BNA). Put such an
+ * endpoint back in its initial state and build the chain again from
+ * the queue: at once for OUT, on the next NAK for IN. The loop below
+ * would move target_frame off TARGET_FRAME_INITIAL, which tells the
+ * token handlers that a chain runs: the endpoint then stays disabled
+ * for good, and every request queued again goes into a dead chain.
+ */
+ if (using_desc_dma(hsotg)) {
+ if (!(dwc2_readl(hsotg, epctl_reg) & DXEPCTL_EPENA)) {
+ /* As in dwc2_gadget_handle_isoc_bna(). */
+ bool running = hs_ep->target_frame !=
+ TARGET_FRAME_INITIAL;
+
+ hs_ep->target_frame = TARGET_FRAME_INITIAL;
+ if (!hs_ep->dir_in && running)
+ dwc2_gadget_complete_isoc_request_ddma(hs_ep);
+ hs_ep->next_desc = 0;
+ hs_ep->compl_desc = 0;
+ if (!hs_ep->dir_in && list_empty(&hs_ep->queue))
+ dwc2_gadget_isoc_out_stop(hs_ep);
+ else
+ dwc2_gadget_restart_isoc_out_ddma(hs_ep);
+ }
+ return;
+ }
+
if (list_empty(&hs_ep->queue)) {
dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
__func__, hs_ep);
@@ -3151,6 +3402,19 @@ static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
}
}
+ /*
+ * Handle BNA before the endpoint-disabled, OUT token and NAK
+ * interrupts that can come with it. Those handlers start a new chain
+ * for an isochronous endpoint in descriptor DMA mode, and the BNA
+ * handler must not then give back a request of the running chain and
+ * reset the endpoint state under it.
+ */
+ if (ints & DXEPINT_BNAINTR) {
+ dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
+ if (hs_ep->isochronous)
+ dwc2_gadget_handle_isoc_bna(hs_ep);
+ }
+
if (ints & DXEPINT_EPDISBLD)
dwc2_gadget_handle_ep_disabled(hs_ep);
@@ -3208,12 +3472,6 @@ 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) {
- dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
- if (hs_ep->isochronous)
- dwc2_gadget_handle_isoc_bna(hs_ep);
- }
-
if (dir_in && !hs_ep->isochronous) {
/* not sure if this is important, but we'll clear it anyway */
if (ints & DXEPINT_INTKNTXFEMP) {
@@ -4187,6 +4445,7 @@ static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
hs_ep->target_frame = TARGET_FRAME_INITIAL;
hs_ep->next_desc = 0;
hs_ep->compl_desc = 0;
+ hs_ep->isoc_out_stopped = 0;
if (dir_in) {
hs_ep->periodic = 1;
mask = dwc2_readl(hsotg, DIEPMSK);
@@ -4346,6 +4605,13 @@ static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
/* disable endpoint interrupts */
dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
+ /*
+ * A request queued from a completion below must not restart the
+ * endpoint or go into its descriptor chain.
+ */
+ hs_ep->isoc_out_stopped = 0;
+ hs_ep->target_frame = TARGET_FRAME_INITIAL;
+
/* terminate all requests with shutdown */
kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
--
2.56.1.windows.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v3 2/3] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt endpoints after BNA
2026-12-01 14:29 [PATCH v3 0/3] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002 Vadim Yuzifovich
2026-12-01 14:29 ` [PATCH v3 1/3] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA Vadim Yuzifovich
@ 2026-12-01 14:29 ` Vadim Yuzifovich
2026-12-01 14:29 ` [PATCH v3 3/3] usb: dwc2: gadget: make descriptors visible before the endpoint is enabled Vadim Yuzifovich
2026-12-01 15:04 ` [PATCH v3 0/3] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002 sashiko-bot
3 siblings, 0 replies; 6+ messages in thread
From: Vadim Yuzifovich @ 2026-12-01 14:29 UTC (permalink / raw)
To: Thinh.Nguyen, gregkh
Cc: linux-usb, linux-kernel, chenyou910331, inochiama, sophgo
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@yuzi.co>
---
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
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v3 3/3] usb: dwc2: gadget: make descriptors visible before the endpoint is enabled
2026-12-01 14:29 [PATCH v3 0/3] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002 Vadim Yuzifovich
2026-12-01 14:29 ` [PATCH v3 1/3] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA Vadim Yuzifovich
2026-12-01 14:29 ` [PATCH v3 2/3] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt " Vadim Yuzifovich
@ 2026-12-01 14:29 ` Vadim Yuzifovich
2026-12-01 15:04 ` [PATCH v3 0/3] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002 sashiko-bot
3 siblings, 0 replies; 6+ messages in thread
From: Vadim Yuzifovich @ 2026-12-01 14:29 UTC (permalink / raw)
To: Thinh.Nguyen, gregkh
Cc: linux-usb, linux-kernel, chenyou910331, inochiama, sophgo
In descriptor DMA mode the core fetches the first descriptor of a chain
as soon as the endpoint is enabled. dwc2 fills the chain in coherent
memory and then writes DxEPDMA and DxEPCTL.EPENA with writel(), whose
barrier orders the earlier stores before the MMIO write. On a Sophgo
SG2002 (T-Head C906, DWC_otg 4.20a) that is not enough: the core still
finds a descriptor that is not host ready, raises BNA and disables the
endpoint, although the descriptor reads host ready, last, short, IOC
when the interrupt is handled.
With a host sending TEST UNIT READY to a CD-ROM LUN as fast as it can
(about 2600 a second, each followed by REQUEST SENSE) and a 1080p60
H.264 encode loading the DRAM, about 0.8% of transfer starts raised BNA,
99.5% of them within 20 us of the enable (the latency of the interrupt
itself), with DxEPDMA still at the head of the chain. Without the
encode there was none in 254000 commands. BNAs per variant, measured in
turn on one boot with a diagnostic build, at about 780000 transfer
starts a minute each:
unchanged 6215, 6510
mb(), read the last descriptor, mb() 0
th.sync.s 0
udelay(2) 2
descriptor written as two word stores,
status last, dma_wmb() between 3728
Mapping coherent memory non-bufferable on the C906 did not change the
rate either. So the stores are still on their way to DRAM when the core
reads, and a load from the descriptor waits for them.
Read the last descriptor of a chain back between two full barriers
before the endpoint is enabled, for non-isochronous transfers and for
the start of an isochronous chain. An isochronous chain marks its first
descriptor host ready last, so that one is read back as well. It costs
one uncached load per transfer. With it there was no BNA in 6.2 million
transfer starts under the same load, and none in a 60 minute run at
1080p60.
A descriptor that dwc2_hsotg_ep_queue() appends to a running
isochronous chain is not read back: no enable follows it to order
against. If the core finds it late, that is a BNA, which
dwc2_gadget_handle_isoc_bna() recovers for OUT and the NAK restart
for IN.
Tested-by: Chen-Yu Yeh <chenyou910331@gmail.com> # Milk-V Duo 256M
Signed-off-by: Vadim Yuzifovich <vadim@yuzi.co>
---
drivers/usb/dwc2/gadget.c | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index 3f3de2fd4590..848a57e9bc02 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -879,6 +879,27 @@ static void dwc2_gadget_fill_nonisoc_xfer_ddma_one(struct dwc2_hsotg_ep *hs_ep,
}
}
+/*
+ * dwc2_gadget_desc_visible - make descriptors the CPU has just written
+ * visible to the core before it is told to fetch them.
+ * @desc: The descriptor written last.
+ *
+ * The core fetches the first descriptor of a chain as soon as the endpoint is
+ * enabled. writel() orders the CPU's earlier stores before that MMIO write,
+ * but on the Sophgo SG2002 (T-Head C906) the stores to coherent memory can
+ * still be on their way to DRAM when the core reads it. The core then finds a
+ * descriptor that is not host ready and raises BNA. Reading the last
+ * descriptor back between two full barriers waits for the stores to land.
+ */
+static void dwc2_gadget_desc_visible(struct dwc2_dma_desc *desc)
+{
+ /* Order the stores to the chain before the load below. */
+ mb();
+ (void)READ_ONCE(desc->status);
+ /* Finish the load before the MMIO write that enables the endpoint. */
+ mb();
+}
+
/*
* dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
* @hs_ep: The endpoint
@@ -906,6 +927,7 @@ static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
if (!ureq || !ureq->num_sgs) {
dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc,
dma_buff, len, true);
+ dwc2_gadget_desc_visible(desc - 1);
return;
}
@@ -918,6 +940,7 @@ static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
}
hs_ep->desc_count = desc_count;
+ dwc2_gadget_desc_visible(desc - 1);
}
/*
@@ -1135,12 +1158,23 @@ static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
+ /*
+ * The rest of the chain has to be visible before descriptor 0 is
+ * host ready. The queue is not empty, so at least one descriptor was
+ * filled: next_desc 0 means that the chain is full and the index
+ * wrapped.
+ */
+ i = hs_ep->next_desc ? hs_ep->next_desc - 1 :
+ MAX_DMA_DESC_NUM_HS_ISOC - 1;
+ dwc2_gadget_desc_visible(&hs_ep->desc_list[i]);
+
/* write descriptor chain address to control register */
dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
desc = &hs_ep->desc_list[0];
desc->status &= ~DEV_DMA_BUFF_STS_MASK;
desc->status |= DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT;
+ dwc2_gadget_desc_visible(desc);
if (!hs_ep->dir_in && !again &&
dwc2_gadget_isoc_out_held_packet(hs_ep)) {
--
2.56.1.windows.1
^ permalink raw reply related [flat|nested] 6+ messages in thread