Linux USB
 help / color / mirror / Atom feed
* [PATCH v4 0/2] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002
@ 2026-12-05 13:02 Vadim Yuzifovich
  2026-12-05 13:02 ` [PATCH v4 1/2] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA Vadim Yuzifovich
  2026-12-05 13:02 ` [PATCH v4 2/2] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt IN " Vadim Yuzifovich
  0 siblings, 2 replies; 5+ messages in thread
From: Vadim Yuzifovich @ 2026-12-05 13:02 UTC (permalink / raw)
  To: Thinh.Nguyen, gregkh
  Cc: linux-usb, linux-kernel, chenyou910331, inochiama, sophgo

These are patches 1 and 2 of the three offered in [1], on top of
usb-next. Chen-Yu asked for them as a series and has tested v2 on a
Milk-V Duo 256M, see his reply in [3]. Patch 3 of v3 is left out of v4,
see below.

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.
In mainline, dwc2_set_cv1800_params() turns gadget DMA off on these
SoCs, so none of this runs there as it is; see the end of this letter
for how to try it.

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 IN 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 of v3 made descriptors visible to the core before the endpoint
is enabled: on this SoC the core fetched a descriptor before the CPU's
stores to it had reached DRAM, which is why bulk endpoints took BNAs at
all. It is a CV18XX/SG200X workaround, and mainline cannot reach it
while dwc2_set_cv1800_params() turns gadget DMA off. It will come back
with a patch that enables gadget DMA on these SoCs, after I have asked
the RISC-V side about the C906 fence behaviour that looks like its root
cause. Testers on an SG2002 still need it to avoid bulk BNAs under load;
it is v3 patch 3 [5].

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. v4 restarts from the BNA handler
then. When the endpoint still reads enabled, it sets SNAK and EPDIS
and restarts from the endpoint-disabled interrupt that brings. If it
should always go that way, I will do it so.

Patch 2 has a Fixes tag but no Cc: stable, since it is built on patch 1
and would not apply without it.

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; with patches 1 and 2, or all three, no run wedged. Patches 2 and
3 were not exercised there, since no bulk BNA happened without the
series either. v4 has not been run there; Chen-Yu, a run of v4 would be
welcome.

Testing here, on the SG2002 with v4 applied to our 7.4-rc5 tree, with
v3 patch 3, 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. A tone is played into the UAC1 gadget, eight requests of 1 ms
are queued, and the host sends TEST UNIT READY to the mass storage
function and GET_STATUS on EP0 in a loop throughout.
- 50 rounds of holding back every request for 20 ms, then 50 rounds of
  giving them back one at a time: 250 stops with nothing queued, each
  drained (one packet each time, 23 us at most), and 250 restarts.
- 200 interrupt holds of 3 to 40 ms, 1.6 s apart: 304 restarts, in each
  of which the core had taken descriptor 0 for the held packet; BNAs
  latched before a restart were seen and ignored afterwards; no BNA was
  seen with the endpoint enabled at any other time.
- 5 holds of 3 s with nothing queued: the stream stopped and resumed.
- During these, 82609 TEST UNIT READY commands and 48676 control
  requests, none failed; the slowest took 45 ms and 40 ms. Without the
  NAK and drain in patch 1, one 3 s hold made a TEST UNIT READY take
  3 s and two control requests time out.
- 450 holds of 3 s with nothing queued during which the host closed the
  stream, so that the endpoint was disabled while stopped, at 0.2 to
  4.2 s into the hold; the stream was opened again after each.
- A USB reset from the host, three times, and an unbind and bind of the
  gadget on the board, each while the endpoint was stopped: the gadget
  enumerated again and the stream ran.
- SET_FEATURE(ENDPOINT_HALT) and CLEAR_FEATURE on the mass storage bulk
  IN endpoint, three times: the endpoint reported halted and then not,
  and the mass storage function answered afterwards.
- With a test hook that skips the drain of a stopping endpoint, so that
  it still held its packets, an unbind and bind of the gadget on the
  board during a hold, 15 times: the drain in dwc2_hsotg_ep_disable()
  took the packets each time, the gadget enumerated again, and TEST
  UNIT READY, control requests and the stream worked right after. The
  OUT endpoints disabled before it timed out waiting for the global OUT
  NAK, which the held packets keep from taking effect (100 us each,
  with a warning). The previous revision of v4 left those packets held
  in that test, and every core reset after it then timed out waiting
  for the AHB master to go idle, until a reboot.
The stream ran at about 1009 completions a second after each part, and
there was no port reset. The holds, the USB resets and 5 of the unbinds
with packets held ran on v4 as posted. The stream closes and the halt
test ran on earlier revisions of v4, which differed in the paths that
an endpoint disable takes; 40 of the stream closes were run on the
revision before this one, which differs from it only in the review
fixes listed last below.

Once, in a test of an earlier revision of v4, a counter of
isochronous OUT XferCompl interrupts went up by about 6.7 million in
about 10 s, right as the host closed the stream at the end of a hold,
and then stopped by itself; the board stayed responsive and nothing was
logged. It did not happen again in 516 more tries of that test, 490
of them on later revisions and 450 of those with every XferCompl that
finds no completed descriptor traced; none were run on v4 as posted.
I do not know its cause; the test hook that holds the requests back,
which f_uac1 may free while the hook still has them, is a suspect, but
that is not shown.

Patch 2 was not exercised by v4 on the board: the bulk BNAs only came
with a 1080p60 encode loading the DRAM, which was not running, and the
v3 patch 3 that the board runs closes the window they came through. Its
v3 results are below. Since v3 it restarts IN endpoints only, restarts
a scatter-gather request only from its start, disables an endpoint
that still reads enabled and restarts it from the endpoint-disabled
interrupt, skips the TxFIFO flush of a restarted endpoint on a later
endpoint-disabled interrupt, warns about a BNA that leaves the endpoint
stopped, and gives up after three restarts; none of those changes has
been exercised.

Patches 2 and 3 in v3, under the load described in v3 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: 546000 commands in 180 s, no BNA and no reset.

Not tested, or known and left as they are:
- 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 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.
- a bulk OUT BNA: patch 2 leaves those alone, as before the series.
- a drain that hits its bound or times out: every drain here took one
  packet and finished.
- the core may take descriptor 0 between the last poll of a wait and
  the store that marks it host busy again, or while a request queued in
  the window before a BNA is put on it; one packet, or its status, is
  lost then. Closing that needs the core to say whether it holds a
  packet, which I have not found.
- isochronous IN, full speed, an interval above 1. Isochronous IN
  behaves as before the series.
- any other core version or SoC, and SMP: the SG2002 has one core. The
  disable count in patch 1 is for SMP and could not be exercised here.

To try it on a CV18XX/SG200X board, gadget DMA has to be allowed, since
dwc2_set_cv1800_params() turns it off, and v3 patch 3 [5] applied:

  --- 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->g_dma_desc = false;
   	p->speed = DWC2_SPEED_PARAM_HIGH;

"usb: dwc2: Disable gadget descriptor DMA for Sophgo CV18XX/SG200X",
now in usb-next, only silences the probe warning while g_dma is off and
does not conflict with 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 v4. The first three answer the four findings of the Sashiko
review of v3 [4], the rest come from reviews of v4 before sending it,
two of them local Sashiko runs, and from the tests above:
- Patch 1: dwc2_hsotg_ep_queue() no longer restarts a stopped endpoint;
  it only unmasks it, and the BNA or OUTTKNEPDIS that follows restarts
  it from the interrupt handler. So no request completes inside
  usb_ep_queue(), and the endpoint is not unmasked before the held
  packet has been given back.
- Patch 1: mark a descriptor of an OUT endpoint host busy again when its
  request is given back, so that the completion loop cannot take a
  descriptor left as DMA done from the last lap of the ring.
- Patch 1: stop if the endpoint was disabled while the lock was dropped
  for a giveback. v4 uses a count that dwc2_hsotg_ep_disable() bumps
  rather than USBACTEP, so that an endpoint disabled and enabled again
  meanwhile is seen too.
- Patch 1: ignore a BNA seen with the endpoint enabled only right after
  a restart, until the next interrupt of the endpoint without a BNA.
  Any other such BNA resets the state as before the series and leaves
  the restart to the endpoint-disabled or OUT token interrupt.
- Patch 1: wait for a held packet only on the first start after a BNA,
  not on every stream start.
- Patch 1: NAK an endpoint that is stopped with no request queued, and
  write the packets it holds to a scratch buffer until the core takes
  no more; dwc2_hsotg_ep_disable() does the same for an endpoint left
  that way. Before, they stayed at the head of the Rx FIFO, and EP0 and
  bulk OUT stalled until the function queued a request again. The drain
  waits 200 us at most with interrupts off; what is left is drained on
  the next BNA. A descriptor the core took for a dropped packet but has
  not written is not reused for a request until it has.
- Patch 1: dwc2_hsotg_epint() stops after a handler that dropped the
  lock if the endpoint was disabled meanwhile, instead of acting on the
  interrupts it read before.
- Patch 1: an OUT token interrupt that finds the chain taken for running
  but the endpoint disabled restarts it as after a BNA.
- Patch 1: write descriptor status words with one store. Host ready is
  0, so a status built up in place could read host ready too early.
- Patch 1: leave isochronous IN as it was: its BNA is handled where and
  how it was, the endpoint-disabled handler keeps its per-frame
  completion for it, and its descriptors are not written back.
- Patch 2: restart IN endpoints only, and a scatter-gather request only
  from its start. A bulk OUT endpoint may hold an acknowledged packet as
  the isochronous one does, and a restart could race with its write.
- Patch 2: restart a request at most three times between completions.
  Warn about an actual restart and about a BNA that leaves the endpoint
  stopped with its request current; other BNAs stay at debug level.
- Patch 2: do not flush the TxFIFO of a restarted endpoint on an
  endpoint-disabled interrupt latched for its BNA and not handled yet;
  a halted or stalled endpoint is always flushed. Fixes tag added.
- Patch 1: the drain in dwc2_hsotg_ep_disable() may wait up to 2 ms
  rather than 200 us, since no BNA comes to drain on once the endpoint
  is gone, and warns if packets are left. A Rx FIFO flush would not
  release them, see patch 1.
- Patch 1: forget the BNA state, including a descriptor 0 left with the
  core by a drain that timed out, in dwc2_hsotg_ep_disable() and
  dwc2_hsotg_ep_enable(), and bump the disable count in
  dwc2_hsotg_disconnect() too.
- Patch 1: the endpoint-disabled handler leaves an endpoint the BNA
  handler has just stopped alone, so the waits with interrupts off are
  not repeated in one interrupt: about 310 us at most.
- Patch 1: a drain that times out, or finds descriptor 0 still with the
  core, leaves the stopped endpoint unmasked, so that the BNA of the
  next held packet drains on; at most four times in a row.
- Patch 2: the TxFIFO flush mark is cleared only by the
  endpoint-disabled handler and the reset paths, not by an XferCompl or
  the start of the next request in the same interrupt.
- Patch 2: a BNA seen with the endpoint still enabled sets SNAK and
  EPDIS, so that the endpoint-disabled interrupt the restart waits for
  is sure to come.
- Patch 3 left out, see above. Chen-Yu's Tested-by on it goes with it.
- Rebased on usb-next.
- Patch 1 is long; if you prefer it split (the Rx FIFO flush removal,
  the endpoint-disabled change, the restart and held packet, the NAK and
  drain, the disable count, the single-store status writes, the stop at
  the wrap of next_desc), I will split it, and the same for cutting the
  changelog of patch 2 down.

Changes in v3 [4]. The first, second, fourth and fifth are from the
Sashiko review of v2 [3], the rest from my own review and tests:
- 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
  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 [3]:
- Patch 1: rewritten. v1 [2] was wrong, see my reply to it. 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] the offer: https://lore.kernel.org/r/CAH_=YdDCDyFBn2k18w8Ck5jrmHHZme7TuAA71hSgcAa9ZPb38Q@mail.gmail.com
[2] v1: https://lore.kernel.org/r/20261115185335.782678-1-vadim@yuzi.co
[3] v2: https://lore.kernel.org/r/20261116070433.2813951-1-vadim@yuzi.co
[4] v3: https://lore.kernel.org/r/20261201142920.49830-1-vadim@yuzi.co
[5] v3 patch 3: https://lore.kernel.org/r/20261201142920.49830-4-vadim@yuzi.co

Vadim Yuzifovich (2):
  usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints
    after BNA
  usb: dwc2: gadget: restart descriptor DMA bulk and interrupt IN
    endpoints after BNA

 drivers/usb/dwc2/core.h   |  41 ++
 drivers/usb/dwc2/gadget.c | 845 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 847 insertions(+), 39 deletions(-)


base-commit: 9357829ac604c28510b71b154cb6630af81226ff
-- 
2.56.1.windows.1


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

end of thread, other threads:[~2026-12-05 13:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 13:02 [PATCH v4 0/2] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002 Vadim Yuzifovich
2026-12-05 13:02 ` [PATCH v4 1/2] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA Vadim Yuzifovich
2026-12-05 13:31   ` sashiko-bot
2026-12-05 13:02 ` [PATCH v4 2/2] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt IN " Vadim Yuzifovich
2026-12-05 13:31   ` sashiko-bot

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