Linux USB
 help / color / mirror / Atom feed
* [PATCH v2 0/3] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002
@ 2026-11-16  7:04 Vadim Yuzifovich
  2026-11-16  7:04 ` [PATCH v2 1/3] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA Vadim Yuzifovich
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Vadim Yuzifovich @ 2026-11-16  7:04 UTC (permalink / raw)
  To: gregkh, Thinh.Nguyen
  Cc: chenyou910331, inochiama, linux-usb, sophgo, linux-kernel,
	Vadim Yuzifovich

These are the three patches offered in [1]. Chen-Yu asked for them as a
series and offered to test on a Milk-V Duo 256M.

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. The
v1 patch was wrong, as I wrote in [3]; this one is rewritten.

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.

Patch 2 starts the current request again from what has been transferred
of it. We only hit the BNA on a chain of one descriptor with nothing
transferred. A BNA on a later part of a long request, or in the middle
of a scatter-gather chain, was not exercised.

Testing, on the SG2002 with the series applied to 7.3 plus counters and
a hook that holds interrupts off for a given time.

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, 368000 commands in 120 s (3070 a
second) with 13103 BNAs, 12809 of them restarted by patch 2, and no
port reset; the other 294 were on the bulk OUT endpoint, came together
with XferCompl and found no request current. Two more such runs: one
the same, one with a port reset whose cause I did not capture. With the
read-back on, 546000 commands in 180 s, no BNA and no reset. In all
runs the endpoint was disabled when the BNA was handled, and no
endpoint-disabled interrupt came for a bulk or interrupt endpoint.

Patch 1. A tone is played into the UAC1 gadget, eight requests of 1 ms
are queued, and interrupts are held off for 3 to 40 ms every 1.6 s.
v1 does restart the endpoint, but then takes one BNA and one restart
for every packet, about 2000 interrupts a second, until the stream is
closed; the audio is mostly intact, which is why I did not see it
before. The reason is in the changelog: the core keeps the packet that
met the BNA and writes it to the first descriptor that becomes ready,
before the endpoint is enabled.

With that packet handled, the endpoint still stalled about once in 100
restarts, 5 times in some 900 holds of 9 ms: every isochronous OUT
packet dropped with OUTTKNEPDIS, bulk OUT NAKed, GINTSTS.RXFLVL stuck,
then a port reset by the host and no answer on EP0. In the traces an
OUT token had arrived while the BNA handler ran, and the handler
flushes the Rx FIFO. With the flush removed, on the same kernel, 1360
holds gave no stall.

This series as posted, plus counters: 480 holds of 9 ms, 480 BNAs
handled, 480 restarts, the stream never stopped. 400 holds of 3 to
40 ms: 361 restarts, none for 3 to 7 ms, one for 9 and 12 ms, two for
20 and 40 ms. 90 holds of 8 to 40 ms with the TEST UNIT READY load on
the mass storage function at the same time: 108 restarts, 608000
commands in 150 s, no BNA on a bulk endpoint, no port reset.

Not tested: isochronous IN, full speed, an interval above 1, and a
stream that the function stops feeding.

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 v2. The first three are from the Sashiko review of v1 [2],
the rest from the test above:
- Patch 1: handle BNA before the endpoint-disabled, OUT token and NAK
  interrupts in dwc2_hsotg_epint(), and ignore a BNA for an OUT endpoint
  that is enabled. 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 next_desc has wrapped to 0.
- Patch 1: the chain start keeps descriptor 0 host busy until the chain
  is written and the chain address is set, and deals with a packet the
  core holds from before the BNA.
- Patch 3: read descriptor 0 of an isochronous chain back as well, now
  that it is marked host ready last.
- Patch 1: the BNA handler no longer flushes the Rx FIFO.

Raised in the review and not changed:
- Patch 1, queue empty at the BNA: the endpoint stays disabled until
  the function queues again. On this core BNA repeats while the endpoint
  is disabled, and the next one restarts it.
- Patch 2, BNA handled before the core has cleared EPENA, or
  endpoint-disabled latched after the restart: in about 40000 BNAs on
  bulk endpoints the endpoint was always disabled already, and no
  endpoint-disabled interrupt followed. I do not know what the core
  guarantees. Thinh, if the restart should wait for the
  endpoint-disabled interrupt, I will do it that way.

Thinh, patch 1 is built on what I saw this core do, not on what it is
specified to do: it holds the packet that met the BNA and delivers it
to the first ready descriptor while the endpoint reads as disabled, and
it does not survive a Rx FIFO flush with OUT traffic going on. If the
databook describes the recovery differently, please say so.

[1] https://lore.kernel.org/r/CAH_=YdDCDyFBn2k18w8Ck5jrmHHZme7TuAA71hSgcAa9ZPb38Q@mail.gmail.com
[2] https://lore.kernel.org/r/20261115185335.782678-1-vadim@yuzi.co
[3] my reply to [PATCH 1/3] in [2]

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/gadget.c | 249 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 234 insertions(+), 15 deletions(-)


base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.56.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-16  7:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  7:04 [PATCH v2 0/3] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002 Vadim Yuzifovich
2026-11-16  7:04 ` [PATCH v2 1/3] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA Vadim Yuzifovich
2026-11-16  7:29   ` sashiko-bot
2026-11-16  7:04 ` [PATCH v2 2/3] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt " Vadim Yuzifovich
2026-11-16  7:13   ` sashiko-bot
2026-11-16  7:04 ` [PATCH v2 3/3] usb: dwc2: gadget: make descriptors visible before the endpoint is enabled Vadim Yuzifovich
2026-11-16  7:27   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox