[PATCH v3 0/3] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002
From: Vadim Yuzifovich
Date: Tue Dec 01 2026 - 09:29:39 EST
These are the three patches offered in [1]. Chen-Yu asked for them as a
series and has tested v2 on a Milk-V Duo 256M, see his reply in [3].
We run a Sophgo SG2002 (T-Head C906, DWC_otg 4.20a) as a peripheral with
g_dma and g_dma_desc both on, as a composite gadget: mass storage, HID,
ACM and UAC1. Descriptor DMA works there, but only with these changes.
Patch 1: an isochronous OUT endpoint that takes a BNA is never restarted,
and the BNA interrupt then repeats until the host resets the port.
Patch 2: a bulk or interrupt endpoint that takes a BNA is only logged at
debug level. It stays disabled with its request current, and the
function waits until the host times out.
Patch 3: why bulk endpoints took BNAs at all on this SoC. The core
fetches the descriptor before the CPU's stores to it have reached DRAM,
although writel() orders them before the enable. Reading the last
descriptor back between two full barriers closes the window. The
measurements are in the patch.
Patches 1 and 2 look generic to dwc2 descriptor DMA. Patch 3 is the one
I am least sure where to put. The effect was seen on the C906 only, and
the read-back costs one uncached load per transfer on every platform. If
it should sit behind a quirk or a core parameter, or belongs in the
RISC-V T-Head errata code instead of dwc2, I will redo it that way.
Thinh, patch 1 is built on what I saw this core do, not on what it is
specified to do: after a BNA on an isochronous OUT endpoint it holds
the packet, shows the endpoint as disabled, and delivers the packet to
descriptor 0 as soon as that is host ready, enabled or not; and it does
not survive a Rx FIFO flush with OUT traffic going on. If the databook
describes the recovery differently, please say so. The same for patch
2: in about 40000 BNAs on bulk endpoints in the v1 and v2 tests the
endpoint was disabled already when the BNA was handled, and no
endpoint-disabled interrupt followed. If the restart should wait for
that interrupt, I will do it that way.
Testing by Chen-Yu on v2, on a Duo 256M with 7.4-rc3, a composite
of ECM, mass storage and UAC1, and a console line every 2 s as the
interrupt hold: without the series the UAC1 stream wedged in 3 of 4
runs, once so that only a reboot brought the gadget back; with patches
1 and 2, or all three, no run wedged, including a 30 minute run with
all functions loaded. Patches 2 and 3 were not exercised there, since
no bulk BNA happened without the series either. Patch 3 has not changed
since v2 and carries his Tested-by. Patches 1 and 2 have changed, so I
left it off them; Chen-Yu, a run of v3 would be welcome.
Testing here, on the SG2002 with the series applied to 7.3 plus
counters and two test hooks: one holds interrupts off for a given time,
the other holds back the requests the function queues on the
isochronous OUT endpoint and gives them back one at a time or all
together.
Patch 1, as posted. A tone is played into the UAC1 gadget, eight
requests of 1 ms are queued.
- 200 interrupt holds of 3 to 40 ms, 1.6 s apart: in every restart the
core had taken descriptor 0 for the held packet; the stream never
stopped.
- 50 rounds of holding back every request for 20 ms, so that the queue
runs empty and the endpoint takes a BNA with nothing to restart it:
50 stops, 50 restarts from dwc2_hsotg_ep_queue(), at most 12 BNA
interrupts per round.
- 50 rounds of giving the requests back one at a time, 5 ms apart: the
held packet took the last queued request 100 times, and each time the
next request queued restarted the endpoint.
- 100 holds of 9 ms with req_number 300: the chain stopped filling at
256 descriptors at the start of the stream and ran on.
No port reset in any of them. The form of patch 1 without the last four
patch 1 changes listed below also went through 300 holds of 9 ms, 400 of 3 to 40 ms
and 150 holds while the host closed and opened the stream 30 times, and
an earlier form, which still gave a request back on an OUT BNA, 480
holds of 9 ms with a 1080p60 H.264 encode running.
When the queue runs empty, dwc2_gadget_complete_isoc_request_ddma()
still warns "ISOC EP queue empty" once or twice: after the last request
it reads the next slot of the ring, which still reads DMA done from the
previous lap. That is as before the series and harmless; I left it
alone.
Patches 2 and 3, under the load described in patch 3 (TEST UNIT READY
as fast as the host can, a 1080p60 H.264 encode running), with the
read-back of patch 3 switched off: 402000 commands in 120 s (3347 a
second), 14452 BNAs on the bulk endpoints, 12269 of them restarted by
patch 2, no port reset. The warning is rate limited; the 30 that were
logged as not restarted were all on the bulk OUT endpoint, came together
with XferCompl, and the descriptor read DMA done. With the read-back
on (v2 run, that code is unchanged): 546000 commands in 180 s, no BNA
and no reset.
Not tested:
- a BNA restart with more than 256 requests queued. As before the
series, requests beyond the 256 descriptors are not put in the chain
when it is started.
- a function that queues from process context under load other than the
test hook's work item.
- a BNA in the middle of a bulk chain of several descriptors, or of a
scatter-gather chain. We only hit the BNA on a chain of one
descriptor.
- isochronous IN, full speed, an interval above 1.
- any other core version or SoC, and SMP: the SG2002 has one core.
To try it on a CV18XX/SG200X board, gadget DMA has to be allowed, since
dwc2_set_cv1800_params() turns it off:
--- a/drivers/usb/dwc2/params.c
+++ b/drivers/usb/dwc2/params.c
@@ static void dwc2_set_cv1800_params(struct dwc2_hsotg *hsotg)
p->host_dma = false;
- p->g_dma = false;
p->speed = DWC2_SPEED_PARAM_HIGH;
and "usb: dwc2: Disable gadget descriptor DMA for Sophgo CV18XX/SG200X"
must not be applied. That change is not part of this series. Whether
DMA should become the default on these SoCs is a separate question, and
host DMA was not tested.
An AI coding assistant (Claude) was used in the debugging sessions and
in drafting the code and the changelogs. The measurements were made on
real hardware, and I have reviewed the patches.
Changes in v3. The first, second, fourth and fifth are from the
Sashiko review of v2 [3], the rest from my own review and the tests
above:
- Patch 1: stop filling the isochronous chain when next_desc wraps to
0. In v2 more than 256 queued requests overwrote descriptor 0, which
is kept host busy at that point.
- Patch 1: a request queued from a completion while the endpoint is
stopped after a BNA no longer makes descriptor 0 host ready before
the chain is built again.
- Patch 2: restart only while the first descriptor of the chain is
still host ready, so that a BNA further down a chain of several
descriptors does not transfer the start of it twice.
- Patch 1: for an OUT endpoint the BNA handler gives no request back.
The held packet lands in the request at the head of the queue, so v2
completed that one empty and put the packet into the next.
- Patch 1: an OUT endpoint left with no request after a BNA, because
none was queued or the held packet took the last one, is restarted
by dwc2_hsotg_ep_queue() when a request arrives. In v2 it stayed
disabled until a later BNA, or for good once the held packet had
been taken.
- Patch 1: if the held packet has not landed within 100 us, warn and
leave descriptor 0 to the core instead of building the chain over it.
- Patch 1: mask the endpoint's interrupts while it is stopped with no
request queued. The core raises BNA as fast as the handler clears it
while it holds the packet: 1.8 million interrupts in about a second
in the test above without the mask, and the host reset the port.
- Patch 1: clear the stopped mark before a restart, so that a request
queued from the held packet's completion cannot start a second
restart inside the first.
- Patch 1: give back descriptors that completed before a BNA or an
endpoint disable while the chain still ran, and clear the stopped
state in dwc2_hsotg_ep_disable() before the requests are killed.
- Patch 3: say in the changelog that a descriptor appended to a running
isochronous chain is not read back.
Changes in v2 [2]:
- Patch 1: rewritten. v1 was wrong, see my reply to it in [2]. Handle
BNA before the endpoint-disabled, OUT token and NAK interrupts, ignore
a BNA for an OUT endpoint that is enabled, keep descriptor 0 host busy
until the chain is written, deal with the packet the core holds, do
not flush the Rx FIFO. Fixes tag added.
- Patch 2: restart the request from req.actual instead of from its
beginning.
- Patch 3: read the last descriptor back also when the isochronous
chain is full, and read descriptor 0 of an isochronous chain back.
[1] https://lore.kernel.org/r/CAH_=YdDCDyFBn2k18w8Ck5jrmHHZme7TuAA71hSgcAa9ZPb38Q@xxxxxxxxxxxxxx
[2] https://lore.kernel.org/r/20261115185335.782678-1-vadim@xxxxxxx
[3] https://lore.kernel.org/r/20261116070433.2813951-1-vadim@xxxxxxx
Vadim Yuzifovich (3):
usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints
after BNA
usb: dwc2: gadget: restart descriptor DMA bulk and interrupt endpoints
after BNA
usb: dwc2: gadget: make descriptors visible before the endpoint is
enabled
drivers/usb/dwc2/core.h | 5 +
drivers/usb/dwc2/gadget.c | 389 ++++++++++++++++++++++++++++++++++++--
2 files changed, 377 insertions(+), 17 deletions(-)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.56.1.windows.1