Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vadim Yuzifovich" <vadim@yuzi.co>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v4 2/2] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt IN endpoints after BNA
Date: Sat, 05 Dec 2026 13:31:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-251572@kernel.org> (raw)
In-Reply-To: <20261205130225.341741-3-vadim@yuzi.co>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] usb: dwc2: stale BNAINTR in dwc2_hsotg_epint() aborts
  restarted IN transfer
- [Medium] usb: dwc2: dwc2_gadget_handle_nonisoc_bna() disables
  ineligible IN endpoint
- [Medium] usb: dwc2: stale bna_pending aborts restarted IN transfer on
  next EPDISBLD
- [Medium] usb: dwc2: broken bna_restarted tracking causes wrong TxFIFO
  flush behavior
- [Medium] usb: dwc2: latched EPDISBLD and stale bna_restarted break
  ep_stop_xfr()
- [Medium] usb: dwc2: dwc2_gadget_handle_nonisoc_bna() skips IN EP NAK
  handshake
- [Medium] usb: dwc2: XferCompl clears bna_pending before EPDISBLD
  flushes next TxFIFO
--

commit 41dd4823735d8cbadd17712a8c5f5ad25bf69eda
Author: Vadim Yuzifovich <vadim@yuzi.co>

usb: dwc2: gadget: restart descriptor DMA bulk and interrupt IN endpoints after BNA

This commit adds BNA interrupt recovery for non-EP0 bulk and interrupt IN
endpoints in descriptor DMA mode, restarting the current request when the
endpoint is disabled and the first descriptor is still host ready.

> diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
> index fdbc9677aef29..bd2e4f397f3c2 100644
> --- a/drivers/usb/dwc2/gadget.c
> +++ b/drivers/usb/dwc2/gadget.c

[ ... ]

> @@ -2790,6 +2798,111 @@ static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)

[ ... ]

> +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;
> +	}

[Severity: Medium]
Can this disable an active IN endpoint that is not eligible for a restart?

This branch writes DXEPCTL_SNAK | DXEPCTL_EPDIS before checking DXEPCTL_STALL,
desc_list, scatter-gather req.actual, descriptor 0 HREADY status, or the
DWC2_BNA_RESTARTS limit.  If DMA has already taken descriptor 0 or the
restart cap has been reached, dwc2_gadget_handle_ep_disabled() flushes the
TxFIFO and the subsequent dwc2_gadget_handle_nonisoc_bna() refuses to restart,
leaving the endpoint stopped (or bypassing DWC2_BNA_RESTARTS if EPENA still
reads set).

[Severity: Medium]
Is it safe to write DXEPCTL_SNAK | DXEPCTL_EPDIS directly without the IN
endpoint NAK-effective handshake?

In dwc2_hsotg_ep_stop_xfr(), disabling a non-stalled IN endpoint first waits
for DXEPINT_INEPNAKEFF (for dedicated TxFIFOs or periodic IN) or asserts
DCTL_SGNPINNAK and waits for GINTSTS_GINNAKEFF (for shared-FIFO bulk IN)
before setting DXEPCTL_EPDIS.  Skipping that handshake risks disrupting an
in-flight IN transfer.

In shared-FIFO mode (which dwc2_check_params() permits with g_dma_desc),
dwc2_gadget_handle_ep_disabled() then flushes shared TxFIFO 0 with
bna_restarted == 0 while other non-periodic IN endpoints such as EP0 IN were
never quiesced via DCTL_SGNPINNAK.

[ ... ]

> +	/*
> +	 * 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);

[Severity: Medium]
Does the !(ints & DXEPINT_EPDISBLD) check drop a newly latched
DXEPINT_EPDISBLD on consecutive BNAs?

dwc2_hsotg_epint() clears all bits in ints from DIEPINT at entry, so any
DXEPINT_EPDISBLD read here is a newly latched, unhandled interrupt.  If BNA #1
left EPDISBLD pending and the restarted request immediately hits BNA #2 so
that ints carries both DXEPINT_EPDISBLD and DXEPINT_BNAINTR, !(ints &
DXEPINT_EPDISBLD) forces bna_restarted to 0 despite BNA #2's new EPDISBLD,
causing the next dwc2_gadget_handle_ep_disabled() to flush the restarted
request's TxFIFO.

Separately, dwc2_hsotg_ep_sethalt() never clears bna_restarted or bna_pending,
so if a halt is set and cleared before EPDISBLD is serviced, both halted and
DXEPCTL_STALL are 0 in dwc2_gadget_handle_ep_disabled() and a stale
bna_restarted skips the TxFIFO flush.  Clearing an already-latched
DXEPINT_EPDISBLD before restarting would avoid carrying bna_restarted across
interrupts altogether.

> +
> +	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);
> +}

[Severity: Medium]
Does leaving DXEPINT_EPDISBLD latched in DIEPINT while re-enabling the
endpoint break dwc2_hsotg_ep_stop_xfr()?

If dwc2_hsotg_ep_dequeue() or dwc2_hsotg_ep_disable() runs before the pending
EPDISBLD interrupt is serviced, dwc2_hsotg_ep_stop_xfr() writes DXEPCTL_EPDIS
and polls DXEPINT_EPDISBLD, which immediately matches the stale BNA bit before
hardware finishes disabling the endpoint.  It then flushes the TxFIFO and
dwc2_hsotg_complete_request() unmaps the DMA buffer while DMA may still be
active.

dwc2_hsotg_ep_stop_xfr() also clears DXEPINT_EPDISBLD directly without
clearing bna_restarted or bna_pending, so bna_restarted stays 1 across future
requests and causes a later unrelated EPDISBLD in
dwc2_gadget_handle_ep_disabled() to skip dwc2_hsotg_txfifo_flush().

[ ... ]

> @@ -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;

[Severity: Medium]
Can a stale bna_pending survive a BNA restart and abort the restarted transfer
on the subsequent EPDISBLD interrupt?

IRQ 1: BNA with EPENA set
  dwc2_gadget_handle_nonisoc_bna()
    bna_pending = 1
    write DXEPCTL_SNAK | DXEPCTL_EPDIS

IRQ 2: BNAINTR read (EPDISBLD not latched yet)
  (hardware clears EPENA and latches EPDISBLD before DIEPCTL read)
  dwc2_gadget_handle_nonisoc_bna()
    EPENA is clear; bna_pending left at 1
    bna_restarted = 1
    dwc2_hsotg_start_req(..., true)   /* continuing=true keeps bna_pending */

IRQ 3: EPDISBLD
  dwc2_gadget_handle_ep_disabled()
    bna_restarted = 0                 /* skips TxFIFO flush */
    bna_pending == 1 -> dwc2_gadget_handle_nonisoc_bna()
      sees EPENA set -> writes SNAK | EPDIS  <-- aborts restarted transfer

Because dwc2_hsotg_start_req() only clears bna_pending when !continuing,
clearing bna_pending when dwc2_gadget_handle_nonisoc_bna() sees EPENA clear
would prevent IRQ 3 from aborting the restarted request.

[ ... ]

> @@ -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 */

[Severity: Medium]
If XferCompl arrives while bna_pending is 1, does clearing bna_pending here
allow the pending EPDISBLD to flush the next request's TxFIFO?

When dwc2_gadget_handle_nonisoc_bna() writes DXEPCTL_SNAK | DXEPCTL_EPDIS with
bna_pending = 1, bna_restarted stays 0.  If the in-flight transfer completes
before SNAK takes effect (for instance after a stale BNAINTR survived an
ep_disable()/ep_enable() cycle and hit a newly queued request),
DXEPINT_XFERCOMPL clears bna_pending and dwc2_hsotg_complete_in() starts the
next request with data in the TxFIFO.

When the pending DXEPINT_EPDISBLD then reaches
dwc2_gadget_handle_ep_disabled() in the same or next interrupt, bna_restarted
is 0, so it calls dwc2_hsotg_txfifo_flush() on the newly started request,
corrupting its data or hanging the endpoint.

[ ... ]

> @@ -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);

[Severity: Medium]
Can a stale DXEPINT_BNAINTR bit in ints abort a transfer that was just started
earlier in dwc2_hsotg_epint()?

ints is read once at the top of dwc2_hsotg_epint().  If DXEPINT_BNAINTR is set
alongside DXEPINT_EPDISBLD (with bna_pending set) or DXEPINT_XFERCOMPL, those
earlier branches already restart or start a transfer with DIEPCTL.EPENA set.

Falling through to dwc2_gadget_handle_nonisoc_bna() then sees DXEPCTL_EPENA
set from that fresh transfer and writes DXEPCTL_SNAK | DXEPCTL_EPDIS to abort
it; if DMA has already moved descriptor 0 off HREADY before the disable
completes, the subsequent EPDISBLD flushes the TxFIFO and refuses to restart,
leaving the endpoint stopped.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205130225.341741-1-vadim@yuzi.co?part=2

      reply	other threads:[~2026-12-05 13:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251572@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vadim@yuzi.co \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox