[PATCH v4 1/2] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA

From: Vadim Yuzifovich

Date: Sat Dec 05 2026 - 08:02:43 EST


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 for an
isochronous OUT endpoint in descriptor DMA mode. Isochronous IN
endpoints are left as they are; they 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. A stream that starts on its first OUT
token has no packet held and does not wait.

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 the BNA of an isochronous OUT endpoint before those interrupts,
and ignore a BNA that is seen with the endpoint enabled again after such
a restart, so that the BNA handler never gives back a request of a
running chain or resets the endpoint state under it. Such a BNA is seen
in the next interrupt of the endpoint, so the next one without a BNA
ends that. A BNA seen with the endpoint enabled at any other time, as a
core that clears EPENA only after raising BNA could give, resets the
endpoint state as before this patch and leaves the restart to the
endpoint-disabled or OUT token interrupt. An OUT token interrupt that
finds the chain taken for running but the endpoint disabled restarts it
as after a BNA, so that a BNA swallowed as one latched before a restart
cannot leave the endpoint dead either.

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. 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 in dwc2_hsotg_ep_queue() when a
request arrives. The BNA the core raises for the packet it holds, or the
OUTTKNEPDIS of the next OUT token, then restarts the endpoint from the
interrupt handler. A restart from the queue call itself would give back
the request of the held packet inside usb_ep_queue().

Masking does not release the packet the core holds. It stays at the head
of the Rx FIFO, which all OUT endpoints share, and the core takes no
other OUT packet from the FIFO until it has written that one somewhere.
A disabled isochronous OUT endpoint also goes on taking the packets the
host sends into the FIFO. On the SG2002, with the requests held back for
3 s, TEST UNIT READY to the mass storage function took 3 s and control
requests on EP0 timed out. So NAK the endpoint when it is stopped, which
makes the core drop the packets that arrive after that, and point
descriptor 0 at a scratch buffer of the endpoint, again and again, until
the core no longer takes it. The packets written there are dropped: no
request was there to take them. With the endpoint NAKed no packet is
added. The drain runs with interrupts off, so it stops after 200 us in
all; if packets are left then, the endpoint stays marked as after a BNA
and its interrupts are left unmasked, and the BNA the core raises for
the next packet it holds drains on. If the core has taken descriptor 0
for a packet but not written it within 100 us, the descriptor stays
with the core: it is not filled for a request, and the chain is not
started, until the core has written it, since the request would
otherwise be given back with the dropped packet. The endpoint is left
unmasked then too, so that the BNA of the packet after it drains on
once the descriptor is written; that is done at most four times in a
row, so that a descriptor the core never writes back cannot turn into a
BNA on every interrupt.
dwc2_hsotg_ep_disable() drains an endpoint still marked as after a BNA,
so that the host closing the stream does not leave packets in front of
EP0. Once the endpoint is gone no BNA comes to drain on, so that drain
may wait up to 2 ms, and a warning says if packets are left even then.
A Rx FIFO flush would not release them: it waits for the AHB master to
go idle, and the core's DMA stays busy while it holds a packet with no
descriptor to write it to. On the SG2002, with a test hook leaving the
packets held through an unbind of the gadget, every core reset after
that found GRSTCTL.AHBIDLE clear until a reboot. The restart clears the
NAK when it enables the endpoint. With that, TEST UNIT READY took at
most 46 ms during the holds and no control request failed. The scratch
buffer is allocated in dwc2_hsotg_ep_enable() before the descriptor
list, so that its failure does not reach the error path that frees a
list left from an earlier enable of the endpoint.

The waits for a held packet run with interrupts off. Each takes 10 us
when the core holds no packet and the time of one packet's DMA when it
does, at most 110 us. One runs on the first start after a BNA; the
drain of a stopped endpoint waits at most 200 us in all. One interrupt
waits at most about 310 us, when the held packet takes the last request
and the endpoint is stopped and drained right after: the
endpoint-disabled interrupt that can come with the BNA leaves an
endpoint that the BNA handler stopped alone. dwc2_hsotg_ep_disable()
may wait up to 2 ms for its drain. On the SG2002 the longest drain
measured took 25 us, for one packet. A core that does
not hold the packet pays 10 us on each start after a BNA and each stop.

The core may read the status word of a descriptor at any time while it
holds a packet, and host ready is 0. So the status words this patch
writes, and those dwc2_gadget_fill_isoc_desc() and the chain reset in
dwc2_gadget_start_isoc_ddma() write, are written with one store, not
built up in place.

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. dwc2_hsotg_ep_disable() also bumps a
count in the endpoint. Code that dropped the lock for a giveback checks
it and stops if the endpoint was disabled meanwhile, and maybe enabled
again, instead of going on with the old chain or restarting the
endpoint. dwc2_hsotg_epint() checks it too after each handler that may
give a request back, so that it does not act on interrupts it read for
an endpoint that has been disabled since. dwc2_hsotg_disconnect() bumps
it as well before it kills the requests. dwc2_hsotg_ep_disable() and
dwc2_hsotg_ep_enable() also forget the BNA state of the endpoint,
including a descriptor 0 that a drain left with the core, which after a
reset may never read DMA done.

dwc2_gadget_complete_isoc_request_ddma() leaves a completed descriptor
marked DMA done and stops at the first descriptor that is not. Once the
ring has wrapped, every descriptor that has not been filled again reads
DMA done. That was harmless while every request queued from a completion
filled the next descriptor, but while the endpoint state is reset such a
request goes to the queue only, and the loop would take the stale
descriptor after the last real completion for that request, complete it,
and go on as long as the function queues another. So mark the descriptor
of an OUT endpoint host busy again when its request is given back.

Fixes: 729cac693eec ("usb: dwc2: Change ISOC DDMA flow")
Signed-off-by: Vadim Yuzifovich <vadim@xxxxxxx>
---
drivers/usb/dwc2/core.h | 31 ++
drivers/usb/dwc2/gadget.c | 696 ++++++++++++++++++++++++++++++++++++--
2 files changed, 691 insertions(+), 36 deletions(-)

diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index a25af51928fc..d7bed6c98ec2 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -106,6 +106,28 @@ 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, unless a drain left packets
+ * behind; a request queued unmasks them, and the next
+ * BNA or OUTTKNEPDIS restarts the endpoint.
+ * @isoc_out_bna: Descriptor DMA isochronous OUT endpoint stopped by a BNA and
+ * not started since; the core may hold the packet that met
+ * the BNA.
+ * @isoc_out_restarted: Descriptor DMA isochronous OUT endpoint enabled again
+ * after a BNA; a BNA latched before that is still to
+ * be seen.
+ * @isoc_out_drain_busy: Descriptor 0 of a descriptor DMA isochronous OUT
+ * endpoint was taken by the core for a packet to drop
+ * and has not been written back yet.
+ * @isoc_out_drain_tries: Times a stopped descriptor DMA isochronous OUT
+ * endpoint was left unmasked because its drain could
+ * not finish; see dwc2_gadget_isoc_out_drain_retry().
+ * @disable_count: Bumped by dwc2_hsotg_ep_disable(), to tell whether the
+ * endpoint was disabled while the lock was dropped.
+ * @isoc_out_drain: Buffer for a descriptor DMA isochronous OUT endpoint to
+ * take a packet into that no request is queued for.
+ * @isoc_out_drain_dma: The DMA address of @isoc_out_drain.
* @frame_overrun: Indicates SOF number overrun in DSTS
*
* This is the driver's state for each registered endpoint, allowing it
@@ -145,6 +167,12 @@ 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 isoc_out_bna:1;
+ unsigned int isoc_out_restarted:1;
+ unsigned int isoc_out_drain_busy:1;
+ unsigned int isoc_out_drain_tries:3;
+ unsigned int disable_count;
unsigned int target_frame;
#define TARGET_FRAME_INITIAL 0xFFFFFFFF
bool frame_overrun;
@@ -153,6 +181,9 @@ struct dwc2_hsotg_ep {
struct dwc2_dma_desc *desc_list;
u8 desc_count;

+ void *isoc_out_drain;
+ dma_addr_t isoc_out_drain_dma;
+
unsigned int next_desc;
unsigned int compl_desc;

diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index 7ef704568e09..fdbc9677aef2 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -218,6 +218,53 @@ 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 a
+ * request is queued, see dwc2_gadget_isoc_out_unstop().
+ *
+ * A disabled isochronous OUT endpoint still takes the packets the host sends
+ * into the Rx FIFO, which all OUT endpoints share, and in DMA mode nothing
+ * takes them out of it: control transfers on EP0 and bulk OUT transfers stall
+ * behind them. So NAK the endpoint as well, which makes the core drop its
+ * packets; the restart clears the NAK when it enables the endpoint.
+ */
+static void dwc2_gadget_isoc_out_stop(struct dwc2_hsotg_ep *hs_ep)
+{
+ struct dwc2_hsotg *hsotg = hs_ep->parent;
+ u32 epctl_reg = DOEPCTL(hs_ep->index);
+
+ hs_ep->isoc_out_stopped = 1;
+ dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, 0, 0);
+ dwc2_writel(hsotg, dwc2_readl(hsotg, epctl_reg) | DXEPCTL_SNAK,
+ epctl_reg);
+}
+
+/*
+ * dwc2_gadget_isoc_out_unstop - let the interrupt handler restart a stopped
+ * descriptor DMA isochronous OUT endpoint.
+ * @hs_ep: The endpoint, stopped by dwc2_gadget_isoc_out_stop(), with a
+ * request queued.
+ *
+ * The endpoint is not restarted here: the restart gives back the request that
+ * receives the packet the core holds, and a request must not complete inside
+ * usb_ep_queue(). Unmasking is enough. The core raises BNA for the endpoint
+ * while it holds a packet and OUTTKNEPDIS for the next OUT token otherwise,
+ * and either handler builds the chain again from the queue.
+ */
+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 +947,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,12 +955,14 @@ 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;
u32 index;
u32 mask = 0;
+ u32 sts;
u8 pid = 0;

dwc2_gadget_get_desc_params(hs_ep, &mask);
@@ -934,29 +984,31 @@ static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
__func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);

- desc->status = 0;
- desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
-
desc->buf = dma_buff;
- desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
- ((len << DEV_DMA_NBYTES_SHIFT) & mask));
+ sts = DEV_DMA_L | DEV_DMA_IOC | ((len << DEV_DMA_NBYTES_SHIFT) & mask);

if (hs_ep->dir_in) {
if (len)
pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket);
else
pid = 1;
- desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) &
- DEV_DMA_ISOC_PID_MASK) |
- ((len % hs_ep->ep.maxpacket) ?
- DEV_DMA_SHORT : 0) |
- ((hs_ep->target_frame <<
- DEV_DMA_ISOC_FRNUM_SHIFT) &
- DEV_DMA_ISOC_FRNUM_MASK);
+ sts |= ((pid << DEV_DMA_ISOC_PID_SHIFT) &
+ DEV_DMA_ISOC_PID_MASK) |
+ ((len % hs_ep->ep.maxpacket) ? DEV_DMA_SHORT : 0) |
+ ((hs_ep->target_frame << DEV_DMA_ISOC_FRNUM_SHIFT) &
+ DEV_DMA_ISOC_FRNUM_MASK);
}

- desc->status &= ~DEV_DMA_BUFF_STS_MASK;
- desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
+ /*
+ * Write the status word once. The core may be polling the descriptor,
+ * see dwc2_gadget_isoc_out_held_packet(), and the buffer status
+ * "host ready" is 0: a status built up in place would read host ready
+ * with the length or flags of a descriptor still being filled.
+ */
+ sts |= (hold ? DEV_DMA_BUFF_STS_HBUSY :
+ DEV_DMA_BUFF_STS_HREADY) << DEV_DMA_BUFF_STS_SHIFT;
+ dma_wmb();
+ WRITE_ONCE(desc->status, sts);

/* Increment frame number by interval for IN */
if (hs_ep->dir_in)
@@ -970,6 +1022,262 @@ 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_wait_packet - wait for the core to write a packet it
+ * holds to descriptor 0, just marked host ready.
+ * @hs_ep: The isochronous OUT endpoint, not enabled.
+ * @waited: Incremented by the microseconds waited.
+ *
+ * The core takes the descriptor within a few bus cycles if it holds a
+ * packet, and the DMA of one packet of at most 1024 bytes takes
+ * microseconds. So this waits 10 us when no packet is held, and at most
+ * 110 us when one is.
+ *
+ * Return: 1 if the packet was written, 0 if the core did not take the
+ * descriptor, or -ETIMEDOUT if it took it but has not written it yet.
+ */
+static int dwc2_gadget_isoc_out_wait_packet(struct dwc2_hsotg_ep *hs_ep,
+ unsigned int *waited)
+{
+ struct dwc2_dma_desc *desc = &hs_ep->desc_list[0];
+ int i;
+
+ for (i = 0; i < 10; i++) {
+ if (READ_ONCE(desc->status) >> DEV_DMA_BUFF_STS_SHIFT !=
+ DEV_DMA_BUFF_STS_HREADY)
+ break;
+ udelay(1);
+ }
+ *waited += i;
+ if (i == 10)
+ return 0;
+
+ for (i = 0; i < 100; i++) {
+ if (READ_ONCE(desc->status) >> DEV_DMA_BUFF_STS_SHIFT ==
+ DEV_DMA_BUFF_STS_DMADONE)
+ break;
+ udelay(1);
+ }
+ *waited += i;
+ if (i < 100)
+ return 1;
+
+ dev_warn_ratelimited(hs_ep->parent->dev,
+ "%s: held packet not written in 100 us\n",
+ hs_ep->ep.name);
+ return -ETIMEDOUT;
+}
+
+/*
+ * 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.
+ *
+ * Only called for the first start after a BNA; a stream that starts on its
+ * first OUT token has no packet held.
+ *
+ * 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)
+{
+ unsigned int count = hs_ep->disable_count;
+ unsigned int waited = 0;
+ u32 target_frame;
+
+ if (dwc2_gadget_isoc_out_wait_packet(hs_ep, &waited) <= 0)
+ 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);
+ /* Unless dwc2_hsotg_ep_disable() ran meanwhile, see the caller. */
+ if (hs_ep->disable_count == count)
+ hs_ep->target_frame = target_frame;
+
+ return true;
+}
+
+#define DWC2_ISOC_OUT_DRAIN_SIZE 1024
+/* Time one drain may spend waiting for packets, with interrupts off. */
+#define DWC2_ISOC_OUT_DRAIN_US 200
+/* The same when the endpoint is disabled, and no BNA comes to drain on. */
+#define DWC2_ISOC_OUT_DRAIN_DISABLE_US 2000
+/* Times a drain that could not finish leaves the endpoint to the next BNA. */
+#define DWC2_ISOC_OUT_DRAIN_TRIES 4
+
+/*
+ * dwc2_gadget_isoc_out_drain_settled - see whether descriptor 0 is free again
+ * after a drain that timed out.
+ * @hs_ep: The isochronous OUT endpoint.
+ *
+ * When the core took descriptor 0 for a packet to drop but had not written it
+ * within the wait, it still owns the descriptor, which points at the scratch
+ * buffer. It must not be filled for a request before the core has written it:
+ * the request would be given back with the dropped packet.
+ *
+ * Return: true if descriptor 0 may be written.
+ */
+static bool dwc2_gadget_isoc_out_drain_settled(struct dwc2_hsotg_ep *hs_ep)
+{
+ struct dwc2_dma_desc *desc = &hs_ep->desc_list[0];
+
+ if (!hs_ep->isoc_out_drain_busy)
+ return true;
+ if (READ_ONCE(desc->status) >> DEV_DMA_BUFF_STS_SHIFT !=
+ DEV_DMA_BUFF_STS_DMADONE)
+ return false;
+ WRITE_ONCE(desc->status,
+ DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
+ hs_ep->isoc_out_drain_busy = 0;
+ hs_ep->isoc_out_drain_tries = 0;
+ return true;
+}
+
+/*
+ * dwc2_gadget_isoc_out_drain_retry - leave a stopped isochronous OUT endpoint
+ * whose drain could not finish open to the next BNA.
+ * @hs_ep: The isochronous OUT endpoint.
+ *
+ * Once the core has written descriptor 0, it raises BNA for the next packet it
+ * holds, and the handler drains on. A descriptor the core never writes back
+ * would turn that into a BNA on every interrupt, so the endpoint is left
+ * unmasked only DWC2_ISOC_OUT_DRAIN_TRIES times in a row; after that it stays
+ * masked until a request is queued or the endpoint is disabled.
+ */
+static void dwc2_gadget_isoc_out_drain_retry(struct dwc2_hsotg_ep *hs_ep)
+{
+ if (!hs_ep->isoc_out_stopped)
+ return;
+ if (hs_ep->isoc_out_drain_tries >= DWC2_ISOC_OUT_DRAIN_TRIES) {
+ dev_warn_ratelimited(hs_ep->parent->dev,
+ "%s: descriptor 0 not written back, endpoint left stopped\n",
+ hs_ep->ep.name);
+ return;
+ }
+ hs_ep->isoc_out_drain_tries++;
+ dwc2_hsotg_ctrl_epint(hs_ep->parent, hs_ep->index, 0, 1);
+}
+
+/*
+ * dwc2_gadget_isoc_out_drain - take the packets that a stopped isochronous
+ * OUT endpoint holds into a buffer of the driver's own.
+ * @hs_ep: The isochronous OUT endpoint, disabled after a BNA with no request
+ * to restart it with, and NAKed.
+ * @budget: The time in microseconds the drain may wait in all.
+ *
+ * The packet that met the BNA stays at the head of the Rx FIFO, which all OUT
+ * endpoints share, and so may the ones that arrived before the NAK took
+ * effect. Until the core has written them somewhere, no other OUT packet is
+ * taken from the FIFO: control transfers on EP0 and bulk OUT transfers stall
+ * until a request is queued on this endpoint, which a function that has
+ * stopped streaming may not do for a long time. So point descriptor 0 at a
+ * scratch buffer, let the core write a held packet there as it does for the
+ * request of one, see dwc2_gadget_isoc_out_held_packet(), and drop it, until
+ * the core takes the descriptor no more. No request was there to take them.
+ *
+ * This runs with interrupts off, so it stops waiting after @budget in all,
+ * DWC2_ISOC_OUT_DRAIN_US from the interrupt handler. If packets are left then,
+ * the endpoint stays
+ * marked as after a BNA, and for a stopped endpoint its interrupts are left
+ * unmasked: the core raises BNA again for the packet it holds, and the
+ * handler drains on. If the core took a descriptor but has not written it,
+ * the descriptor stays with the core, see
+ * dwc2_gadget_isoc_out_drain_settled(); the endpoint is left unmasked then
+ * too, so that the BNA of the packet after it drains on once it is written,
+ * see dwc2_gadget_isoc_out_drain_retry().
+ *
+ * Descriptor 0 is host busy again afterwards, so that the completion loop
+ * does not take a packet for a request queued later.
+ */
+static void dwc2_gadget_isoc_out_drain(struct dwc2_hsotg_ep *hs_ep,
+ unsigned int budget)
+{
+ struct dwc2_hsotg *hsotg = hs_ep->parent;
+ struct dwc2_dma_desc *desc = &hs_ep->desc_list[0];
+ u32 len = min_t(u32, hs_ep->ep.maxpacket, DWC2_ISOC_OUT_DRAIN_SIZE);
+ u32 hbusy = DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT;
+ unsigned int waited = 0;
+ u32 mask = 0;
+ int ret;
+
+ if (!hs_ep->isoc_out_drain)
+ return;
+ if (!dwc2_gadget_isoc_out_drain_settled(hs_ep)) {
+ dwc2_gadget_isoc_out_drain_retry(hs_ep);
+ return;
+ }
+
+ dwc2_gadget_get_desc_params(hs_ep, &mask);
+ /* A packet may take 110 us: do not start one past the budget. */
+ while (waited + 110 <= budget) {
+ desc->buf = hs_ep->isoc_out_drain_dma;
+ WRITE_ONCE(desc->status, hbusy | DEV_DMA_L |
+ ((len << DEV_DMA_NBYTES_SHIFT) & mask));
+ dwc2_writel(hsotg, hs_ep->desc_list_dma, DOEPDMA(hs_ep->index));
+ dma_wmb();
+ WRITE_ONCE(desc->status, (desc->status & ~DEV_DMA_BUFF_STS_MASK) |
+ DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
+
+ ret = dwc2_gadget_isoc_out_wait_packet(hs_ep, &waited);
+ if (ret < 0) {
+ hs_ep->isoc_out_drain_busy = 1;
+ dwc2_gadget_isoc_out_drain_retry(hs_ep);
+ return;
+ }
+ WRITE_ONCE(desc->status, hbusy);
+ if (!ret) {
+ hs_ep->isoc_out_bna = 0;
+ return;
+ }
+ }
+
+ if (hs_ep->isoc_out_stopped)
+ dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, 0, 1);
+}
+
+/*
+ * dwc2_gadget_bna_state_reset - forget the BNA state of an endpoint that goes
+ * away.
+ * @hs_ep: The endpoint.
+ *
+ * Descriptor 0 of an isochronous OUT endpoint that a drain left with the core
+ * is taken back as well: after the endpoint is disabled, or the core reset,
+ * it may never read DMA done, and dwc2_gadget_start_isoc_ddma() would wait
+ * for that for good. dwc2_hsotg_disconnect() leaves the state alone, so that
+ * dwc2_hsotg_ep_disable() still drains an endpoint that holds packets.
+ */
+static void dwc2_gadget_bna_state_reset(struct dwc2_hsotg_ep *hs_ep)
+{
+ hs_ep->isoc_out_stopped = 0;
+ hs_ep->isoc_out_bna = 0;
+ hs_ep->isoc_out_restarted = 0;
+ if (hs_ep->isoc_out_drain_busy && hs_ep->desc_list)
+ WRITE_ONCE(hs_ep->desc_list[0].status,
+ DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
+ hs_ep->isoc_out_drain_busy = 0;
+ hs_ep->isoc_out_drain_tries = 0;
+}
+
/*
* dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
* @hs_ep: The isochronous endpoint.
@@ -982,6 +1290,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,20 +1298,45 @@ 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);
+ dwc2_gadget_isoc_out_drain(hs_ep, DWC2_ISOC_OUT_DRAIN_US);
+ }
dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
return;
}

+ /*
+ * Descriptor 0 still belongs to the core after a drain that timed
+ * out. Try again on a later OUT token or BNA interrupt.
+ */
+ if (!hs_ep->dir_in && !dwc2_gadget_isoc_out_drain_settled(hs_ep)) {
+ hs_ep->target_frame = TARGET_FRAME_INITIAL;
+ return;
+ }
+
/* Initialize descriptor chain by Host Busy status */
for (i = 0; i < MAX_DMA_DESC_NUM_HS_ISOC; i++) {
desc = &hs_ep->desc_list[i];
- desc->status = 0;
- desc->status |= (DEV_DMA_BUFF_STS_HBUSY
- << DEV_DMA_BUFF_STS_SHIFT);
+ /* One store: a status of 0 would read host ready. */
+ WRITE_ONCE(desc->status,
+ DEV_DMA_BUFF_STS_HBUSY << 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 +1346,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,6 +1364,32 @@ 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];
+ dma_wmb();
+ WRITE_ONCE(desc->status, (desc->status & ~DEV_DMA_BUFF_STS_MASK) |
+ DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
+
+ if (!hs_ep->dir_in && hs_ep->isoc_out_bna && !again) {
+ unsigned int count = hs_ep->disable_count;
+
+ if (dwc2_gadget_isoc_out_held_packet(hs_ep)) {
+ /*
+ * dwc2_hsotg_ep_disable() may have run while the lock
+ * was dropped for the giveback, and the endpoint may
+ * have been enabled again since.
+ */
+ if (hs_ep->disable_count != count)
+ return;
+ again = true;
+ goto restart;
+ }
+ }
+
+ if (!hs_ep->dir_in) {
+ hs_ep->isoc_out_restarted = hs_ep->isoc_out_bna;
+ hs_ep->isoc_out_bna = 0;
+ }
+
ctrl = dwc2_readl(hsotg, depctl);
ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
dwc2_writel(hsotg, ctrl, depctl);
@@ -1485,13 +1851,31 @@ 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.
+ */
+ dwc2_gadget_isoc_out_unstop(hs_ep);
}
return 0;
}
@@ -2214,6 +2598,7 @@ static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
struct dwc2_hsotg *hsotg = hs_ep->parent;
struct dwc2_hsotg_req *hs_req;
struct usb_request *ureq;
+ unsigned int count = hs_ep->disable_count;
u32 desc_sts;
u32 mask;

@@ -2250,8 +2635,29 @@ static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
DEV_DMA_ISOC_FRNUM_SHIFT;
}

+ /*
+ * Mark the descriptor of an OUT endpoint host busy again.
+ * Left as DMA done, it would be taken for a new completion
+ * once the ring has wrapped and the loop reached it before
+ * the descriptor was filled again: a request queued while the
+ * endpoint is being restarted after a BNA goes to the queue
+ * only.
+ */
+ if (!hs_ep->dir_in)
+ WRITE_ONCE(hs_ep->desc_list[hs_ep->compl_desc].status,
+ DEV_DMA_BUFF_STS_HBUSY <<
+ DEV_DMA_BUFF_STS_SHIFT);
+
dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);

+ /*
+ * The lock was dropped for the giveback. If the endpoint was
+ * disabled meanwhile, and maybe enabled again, the chain this
+ * loop walks is gone.
+ */
+ if (hs_ep->disable_count != count)
+ return;
+
hs_ep->compl_desc++;
if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_HS_ISOC - 1))
hs_ep->compl_desc = 0;
@@ -2260,25 +2666,128 @@ static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
}

/*
- * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
- * @hs_ep: The isochronous endpoint.
+ * 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.
*
- * 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.
+ * 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_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
+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)
- dwc2_flush_rx_fifo(hsotg);
- dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
+ if (list_empty(&hs_ep->queue) ||
+ hs_ep->target_frame != TARGET_FRAME_INITIAL ||
+ dwc2_readl(hsotg, DOEPCTL(hs_ep->index)) & DXEPCTL_EPENA)
+ return;
+
+ hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
+ dwc2_gadget_start_isoc_ddma(hs_ep);
+}
+
+/*
+ * dwc2_gadget_isoc_out_bna_reset - put a descriptor DMA isochronous OUT
+ * endpoint that the core stopped back in its initial state.
+ * @hs_ep: The isochronous OUT endpoint.
+ * @start: Build the chain again from the queue and enable it, or mark the
+ * endpoint stopped if nothing is queued.
+ *
+ * The state is reset before anything is given back. A completion may queue a
+ * request, and that one must go to the queue only, not to a descriptor of the
+ * chain that has stopped: the core would take descriptor 0 for the packet it
+ * holds, see dwc2_gadget_isoc_out_held_packet().
+ *
+ * Packets that completed before the BNA but whose XferCompl was not yet seen
+ * are given back first, so that the new chain does not reuse their requests.
+ * 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.
+ */
+static void dwc2_gadget_isoc_out_bna_reset(struct dwc2_hsotg_ep *hs_ep,
+ bool start)
+{
+ unsigned int count = hs_ep->disable_count;
+ bool running = hs_ep->target_frame != TARGET_FRAME_INITIAL;

+ hs_ep->isoc_out_restarted = 0;
hs_ep->target_frame = TARGET_FRAME_INITIAL;
+ if (running) {
+ dwc2_gadget_complete_isoc_request_ddma(hs_ep);
+ /* Disabled while the lock was dropped for a giveback. */
+ if (hs_ep->disable_count != count)
+ return;
+ }
hs_ep->next_desc = 0;
hs_ep->compl_desc = 0;
+ hs_ep->isoc_out_bna = 1;
+
+ if (!start)
+ return;
+ if (list_empty(&hs_ep->queue)) {
+ dwc2_gadget_isoc_out_stop(hs_ep);
+ dwc2_gadget_isoc_out_drain(hs_ep, DWC2_ISOC_OUT_DRAIN_US);
+ } else {
+ dwc2_gadget_restart_isoc_out_ddma(hs_ep);
+ }
+}
+
+/*
+ * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
+ * @hs_ep: The isochronous endpoint.
+ *
+ * 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 one is, see dwc2_gadget_isoc_out_unstop().
+ *
+ * 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 start = true;
+
+ if (hs_ep->dir_in) {
+ dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep),
+ 0);
+ hs_ep->target_frame = TARGET_FRAME_INITIAL;
+ hs_ep->next_desc = 0;
+ hs_ep->compl_desc = 0;
+ return;
+ }
+
+ /*
+ * 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, and is then seen with the new chain running. Leave
+ * that chain alone.
+ *
+ * A BNA seen with the endpoint enabled and no restart before it means
+ * that the core has not cleared EPENA yet. Reset the state as for any
+ * BNA, but leave the restart to the endpoint-disabled or OUT token
+ * interrupt that follows once it has.
+ */
+ if (dwc2_readl(hsotg, DOEPCTL(hs_ep->index)) & DXEPCTL_EPENA) {
+ if (hs_ep->isoc_out_restarted) {
+ hs_ep->isoc_out_restarted = 0;
+ return;
+ }
+ start = false;
+ }
+
+ dwc2_gadget_isoc_out_bna_reset(hs_ep, start);
}

/**
@@ -2902,6 +3411,27 @@ static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
if (!hs_ep->isochronous)
return;

+ /*
+ * In descriptor DMA the requests of an OUT endpoint 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
+ * OUT 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, as the BNA handler does.
+ * The loop below would move target_frame off TARGET_FRAME_INITIAL,
+ * which tells the OUT token handler that a chain runs: the endpoint
+ * then stays disabled for good, and every request queued again goes
+ * into a dead chain. If the BNA handler has restarted the endpoint
+ * already, it is enabled and left alone; if it stopped it for lack of a
+ * request, it is left stopped.
+ */
+ if (using_desc_dma(hsotg) && !dir_in) {
+ if (!(dwc2_readl(hsotg, epctl_reg) & DXEPCTL_EPENA) &&
+ !hs_ep->isoc_out_stopped)
+ dwc2_gadget_isoc_out_bna_reset(hs_ep, true);
+ return;
+ }
+
if (list_empty(&hs_ep->queue)) {
dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
__func__, hs_ep);
@@ -2947,6 +3477,15 @@ static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
/* Start first ISO Out */
ep->target_frame = hsotg->frame_number;
dwc2_gadget_start_isoc_ddma(ep);
+ } else if (!(dwc2_readl(hsotg, DOEPCTL(ep->index)) &
+ DXEPCTL_EPENA)) {
+ /*
+ * The chain is taken for running, but the endpoint
+ * is disabled: the core stopped it on a BNA that was
+ * not handled as one, for example one seen with the
+ * endpoint still enabled. Restart it as after a BNA.
+ */
+ dwc2_gadget_isoc_out_bna_reset(ep, true);
}
return;
}
@@ -3091,6 +3630,7 @@ static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
+ unsigned int count;
u32 ints;

ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
@@ -3107,6 +3647,13 @@ static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
__func__, idx, dir_in ? "in" : "out", ints);

+ /*
+ * The handlers below may drop the lock to give a request back. If the
+ * endpoint was disabled meanwhile, the interrupts read above belong to
+ * an endpoint that is gone.
+ */
+ count = hs_ep->disable_count;
+
/* Don't process XferCompl interrupt if it is a setup packet */
if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
ints &= ~DXEPINT_XFERCOMPL;
@@ -3149,10 +3696,35 @@ static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
if (!hs_ep->isochronous || !(ints & DXEPINT_OUTTKNEPDIS))
dwc2_hsotg_handle_outdone(hsotg, idx);
}
+ if (hs_ep->disable_count != count)
+ return;
+ }
+
+ /*
+ * Handle BNA of an isochronous OUT endpoint before the
+ * endpoint-disabled and OUT token interrupts that can come with it.
+ * Those handlers start a new chain in descriptor DMA mode, and the BNA
+ * handler must not then reset the endpoint state under it.
+ */
+ if ((ints & DXEPINT_BNAINTR) && hs_ep->isochronous && !dir_in) {
+ dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
+ dwc2_gadget_handle_isoc_bna(hs_ep);
+ if (hs_ep->disable_count != count)
+ return;
+ } else if (hs_ep->isochronous && !dir_in) {
+ /*
+ * A BNA latched before a restart is seen in the next interrupt
+ * of the endpoint. This one has none, so any BNA from now on
+ * is a new one.
+ */
+ hs_ep->isoc_out_restarted = 0;
}

- if (ints & DXEPINT_EPDISBLD)
+ if (ints & DXEPINT_EPDISBLD) {
dwc2_gadget_handle_ep_disabled(hs_ep);
+ if (hs_ep->disable_count != count)
+ return;
+ }

if (ints & DXEPINT_OUTTKNEPDIS)
dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
@@ -3208,7 +3780,7 @@ 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) {
+ 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);
@@ -3377,14 +3949,22 @@ void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
dwc2_clear_bit(hsotg, GOTGCTL, GOTGCTL_DEVHNPEN);
}

- /* all endpoints should be shutdown */
+ /*
+ * all endpoints should be shutdown. Bump the disable count first, see
+ * dwc2_hsotg_ep_disable(): a giveback that dropped the lock must not
+ * go on with an endpoint of a gadget that goes away.
+ */
for (ep = 0; ep < hsotg->num_of_eps; ep++) {
- if (hsotg->eps_in[ep])
+ if (hsotg->eps_in[ep]) {
+ hsotg->eps_in[ep]->disable_count++;
kill_all_requests(hsotg, hsotg->eps_in[ep],
-ESHUTDOWN);
- if (hsotg->eps_out[ep])
+ }
+ if (hsotg->eps_out[ep]) {
+ hsotg->eps_out[ep]->disable_count++;
kill_all_requests(hsotg, hsotg->eps_out[ep],
-ESHUTDOWN);
+ }
}

call_gadget(hsotg, disconnect);
@@ -4161,6 +4741,21 @@ static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
else
desc_num = MAX_DMA_DESC_NUM_GENERIC;

+ /*
+ * See dwc2_gadget_isoc_out_drain(). Allocated first: the error path
+ * below frees a descriptor list it finds, which may be one from an
+ * earlier enable of the endpoint.
+ */
+ if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
+ !dir_in && !hs_ep->isoc_out_drain) {
+ hs_ep->isoc_out_drain =
+ dmam_alloc_coherent(hsotg->dev, DWC2_ISOC_OUT_DRAIN_SIZE,
+ &hs_ep->isoc_out_drain_dma,
+ GFP_ATOMIC);
+ if (!hs_ep->isoc_out_drain)
+ return -ENOMEM;
+ }
+
/* Allocate DMA descriptor chain for non-ctrl endpoints */
if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
@@ -4202,6 +4797,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;
+ dwc2_gadget_bna_state_reset(hs_ep);
if (dir_in) {
hs_ep->periodic = 1;
mask = dwc2_readl(hsotg, DIEPMSK);
@@ -4348,6 +4944,25 @@ static int dwc2_hsotg_ep_disable(struct usb_ep *ep)

ctrl = dwc2_readl(hsotg, epctrl_reg);

+ /*
+ * An isochronous OUT endpoint disabled by a BNA may still hold packets
+ * at the head of the Rx FIFO, which EP0 shares, see
+ * dwc2_gadget_isoc_out_drain(). Once the endpoint is gone no BNA comes
+ * to drain on, so give the drain more time here. A Rx FIFO flush does
+ * not help: the core holds the packet for a DMA write that waits for
+ * a descriptor, and the flush waits for the AHB master to go idle.
+ */
+ if (using_desc_dma(hsotg) && hs_ep->isochronous && !dir_in &&
+ hs_ep->isoc_out_bna && !(ctrl & DXEPCTL_EPENA)) {
+ dwc2_writel(hsotg, ctrl | DXEPCTL_SNAK, epctrl_reg);
+ dwc2_gadget_isoc_out_drain(hs_ep,
+ DWC2_ISOC_OUT_DRAIN_DISABLE_US);
+ if (hs_ep->isoc_out_bna)
+ dev_warn(hsotg->dev,
+ "%s: packets left in the Rx FIFO at disable\n",
+ hs_ep->ep.name);
+ }
+
if (ctrl & DXEPCTL_EPENA)
dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);

@@ -4361,6 +4976,15 @@ 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. The count tells a
+ * giveback that dropped the lock that the endpoint went away.
+ */
+ dwc2_gadget_bna_state_reset(hs_ep);
+ hs_ep->target_frame = TARGET_FRAME_INITIAL;
+ hs_ep->disable_count++;
+
/* terminate all requests with shutdown */
kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);

--
2.56.1.windows.1