All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Petar Stepanovic" <pstepanovic@axiado.com>
Cc: linux-usb@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v8 6/6] usb: gadget: udc: crg: Add core driver and build integration
Date: Fri, 04 Dec 2026 02:07:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-248944@kernel.org> (raw)
In-Reply-To: <20261203-axiado-ax3000-usb-device-controller-v8-6-d5ff04febd4d@axiado.com>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] usb: crg: missing recovery/stop checks after lock drop in event
  ring
- [High] usb: crg: suspend re-arms DMA and resume skips hardware state
  restore
- [Medium] usb: gadget: crg: premature usb_ep.enabled clear in
  crg_udc_recover_link()
- [Medium] usb: gadget: crg: bounce copy and IRQ-off unmap in
  crg_udc_req_done()
- [Medium] usb: gadget: crg: hardcoded 1 ms IMOD in
  crg_udc_init_event_ring()
- [Medium] usb: gadget: crg: crg_gadget_irq_init() pairs IRQF_SHARED
  with IRQF_ONESHOT
- [Low] usb: gadget: crg: crg_udc_reset() busy-waits in sleepable
  callers
- [Low] usb: gadget: crg: data race on max_speed in crg_udc_set_speed()
--

commit b35e22194ff20279fce619046adf96ac69e8667a
Author: Petar Stepanovic <pstepanovic@axiado.com>

usb: gadget: udc: crg: Add core driver and build integration

This commit adds the core probe, remove, shutdown, PM, IRQ, and recovery
logic for the Axiado AX3000/AX3005 UDC driver in crg-udc-core.c and wires
the crg directory into Kconfig and Makefile.

> diff --git a/drivers/usb/gadget/udc/crg/Makefile b/drivers/usb/gadget/udc/crg/Makefile
> new file mode 100644
> index 0000000000000..96b209fb59fc3
> --- /dev/null
> +++ b/drivers/usb/gadget/udc/crg/Makefile
> @@ -0,0 +1,9 @@
[ ... ]
> +crg-udc-y			:= crg-udc-core.o crg-udc-ring.o crg-udc-ep0.o crg-udc-gadget.o

[Severity: Medium]
Does crg-udc-gadget.c need to bounce-copy every non-EP0 transfer and run
DMA unmap, RX memcpy(), and page freeing under udc_lock with IRQs disabled?

On every usb_ep_queue(), crg_udc_bounce_map_pages() allocates order-0 pages
one by one (with __GFP_ZERO on RX), copies every TX payload on the CPU, and
rejects pre-mapped requests with -EOPNOTSUPP.  On completion,
crg_udc_req_done() calls crg_udc_unmap_request() before dropping udc_lock,
running dma_unmap_sgtable(), full-buffer RX memcpy(), and per-page
__free_page() with local IRQs off.

This causes heavy allocator churn, SuperSpeed throughput loss, and long
IRQ-off stalls on every transfer.  Detaching udc_req->bounce under udc_lock
and unmapping/copying/freeing it after unlocking would avoid the IRQ-off
stall, and mapping DMA-capable gadget buffers directly would avoid the
unconditional bounce copy.

> diff --git a/drivers/usb/gadget/udc/crg/crg-udc-core.c b/drivers/usb/gadget/udc/crg/crg-udc-core.c
> new file mode 100644
> index 0000000000000..ed601ad2a23cf
> --- /dev/null
> +++ b/drivers/usb/gadget/udc/crg/crg-udc-core.c
[ ... ]
> +	/* Use completed recovery or leave a pending reset to the worker. */
> +	ret = crg_udc->suspend_recovery_result;
> +	if (ret == -EINPROGRESS || (ret && !crg_udc->external_reset_pending))
> +		ret = crg_udc_recover(crg_udc, &flags, notify_disconnect);
[ ... ]
> +	if (!crg_udc->gadget_stopping) {
> +		if (crg_udc->shutting_down || crg_udc->resetting)
> +			ret = -ESHUTDOWN;
> +		else if (crg_udc->gadget_driver && crg_udc->pullup_on)
> +			ret = crg_udc_set_pullup(crg_udc, true);
> +	}

[Severity: High]
Does crg_udc_resume() fail to restore hardware state after hibernation
restore or power-gated suspend?

DEFINE_SIMPLE_DEV_PM_OPS() binds crg_udc_resume() to .restore as well as
.resume.  In crg_udc_suspend(), calling crg_udc_recover() immediately
reprograms DCBAP and event rings (setting hw_dma_armed = true) and stages
CRG_CMD_INIT_EP0 right before sleep, without disabling VBUS detect, the
PHY, or the clock.

After register context loss, crg_udc_resume() asserts CRG_U3DC_CTRL_RUN via
crg_udc_set_pullup() on zeroed registers (losing device-role mode, VBUS
detect, max speed, DCBAP, event ring pointers, and the staged INIT_EP0),
and crg_udc_finish_staged_ep0() reads cmd_control == 0 and reports success.
Quiescing the controller in crg_udc_suspend() and rebuilding its register
state in crg_udc_resume() would handle power loss cleanly.

[ ... ]

> +/* Program the speed limit while the controller is stopped. */
> +int crg_udc_program_speed(struct crg_gadget_dev *crg_udc)
> +{
> +	u32 cfg, speed;
> +
> +	switch (crg_udc->max_speed) {

[Severity: Low]
Can crg_udc_program_speed() race with crg_udc_set_speed() on
crg_udc->max_speed?

CPU0                                CPU1
crg_udc_suspend() [no gadget]
  crg_udc_recover() -> timeout
  external_reset_pending = true
  crg_udc_kick_reset_work()
  return err (suspend aborted)
dpm_complete()
crg_udc_recovery_work()             gadget_bind_driver()
  crg_udc_recover_external()          usb_gadget_udc_set_speed()
    spin_lock(&udc_lock)                crg_udc_set_speed()
    crg_udc_program_speed()               crg_udc->max_speed = ...
      read crg_udc->max_speed             <-- data race

crg_udc_set_speed() in crg-udc-gadget.c writes crg_udc->max_speed holding
only device_lock(&gadget->dev), without taking crg_udc->udc_lock.
Acquiring crg_udc->udc_lock in crg_udc_set_speed() avoids the data race.

[ ... ]

> +	for (i = 0; i < CRG_RING_NUM; i++) {
> +		ret = crg_udc_init_event_ring(crg_udc, i);
> +		if (ret)
> +			return ret;
> +	}

[Severity: Medium]
Is the hardcoded 1 ms interrupt moderation interval in
crg_udc_init_event_ring() intentional?

On every probe, reset, and recovery, crg_udc_init_event_ring() in
drivers/usb/gadget/udc/crg/crg-udc-ring.c writes 4000 (4000 * 250 ns =
1 ms, or 8 High-Speed/SuperSpeed microframes) to &uicr->imod.  That caps
event interrupts at 1000/s, limiting synchronous bulk IOPS, adding up to 1 ms
RTT latency to USB Ethernet gadgets, and introducing up to 8-microframe
jitter on isochronous endpoints.

[ ... ]

> +			ret = crg_udc_process_event_ring(crg_udc, i, &flags);
> +			/*
> +			 * The event handlers drop udc_lock, so a reset may have
> +			 * started (resetting) or already completed and rebuilt
> +			 * the rings (generation) while they ran. Either way
> +			 * tmp_status and the checks below are stale.
> +			 */
> +			if (unlikely(crg_udc->resetting || crg_udc->suspended ||
> +				     crg_udc->shutting_down ||
> +				     generation != crg_udc->udc_event[i].generation)) {
> +				spin_unlock_irqrestore(&crg_udc->udc_lock, flags);
> +				return;
> +			}

[Severity: High]
Can crg_udc_process_event_ring() and crg_gadget_handle_interrupt() keep
handling events after crg_udc_reinit() or teardown has taken ownership?

CPU0 (irq_thread)                   CPU1 (port_retry_work)
crg_gadget_handle_interrupt()
  crg_udc_process_event_ring()
    [after 32 events]
    spin_unlock(&udc_lock)
    cond_resched()                  crg_udc_handle_port_status()
                                      crg_udc_reinit()
                                        controller_recovering = true
                                        device_state = RECONNECTING
                                        crg_udc_ep_nuke()
                                          spin_unlock(&udc_lock)
    spin_lock(&udc_lock)
    [resetting == false,
     generation unchanged]
    crg_udc_handle_event()          <-- runs during recovery

Unlike the entry guard in crg_gadget_handle_interrupt(), the post-ring
check omits controller_recovering and gadget_stopping.  Inside
crg_udc_process_event_ring() in crg-udc-ring.c, the post-relock checks only
test generation and resetting, omitting controller_recovering,
gadget_stopping, shutting_down, and suspended (and still writing
uicr->erdphi/erdplo after breaking on suspended).

Because crg_udc_reinit() sets controller_recovering without setting
resetting or bumping udc_event[0].generation, the IRQ thread can re-enter
crg_udc_handle_event() mid-reinit, completing stale EP0 transfers in
crg_udc_handle_xfer_event() or W1C-clearing PORTSC bits during reinit.

[ ... ]

> +	/*
> +	 * IRQF_ONESHOT keeps this (level-triggered) line masked until
> +	 * crg_udc_irq_thread() has cleared the status bits, which is the
> +	 * correct pairing for IRQF_SHARED with a threaded handler.
> +	 */
> +	ret = devm_request_threaded_irq(&pdev->dev, irq, crg_udc_common_irq,
> +					crg_udc_irq_thread,
> +					IRQF_SHARED | IRQF_ONESHOT, "crg_udc",
> +					crg_udc);

[Severity: Medium]
Will pairing IRQF_SHARED with IRQF_ONESHOT break probe on shared interrupt
lines and starve sibling devices?

__setup_irq() returns -EBUSY when shared handlers disagree on IRQF_ONESHOT,
so sharing this line with a standard non-oneshot handler such as xhci-plat
fails probe.  Even if a sibling accepts oneshot, keeping the irqchip line
masked until crg_udc_irq_thread() finishes starves sibling devices across
cond_resched() yields in crg_udc_process_event_ring() and multi-millisecond
command and reset polls.

If shared IRQ support is intended, mask CRG_U3DC_CTRL_IRQ_MASK in
crg_udc_common_irq() under irq_lock, unmask it when
crg_gadget_handle_interrupt() finishes, and drop IRQF_ONESHOT; otherwise
drop IRQF_SHARED.

[ ... ]

> +static int crg_udc_reset(struct crg_gadget_dev *crg_udc, unsigned long *flags,
> +			 bool preserve_enabled)
> +{
> +	int ret;
> +
> +	crg_udc_set_irq_mask(crg_udc, 0);
> +	crg_udc_reset_session(crg_udc);
> +	spin_unlock_irqrestore(&crg_udc->udc_lock, *flags);
> +	ret = crg_udc_soft_reset(crg_udc, true);
> +	spin_lock_irqsave(&crg_udc->udc_lock, *flags);

[Severity: Low]
Does crg_udc_reset() need to busy-wait in sleepable callers?

Hardcoding atomic = true makes crg_udc_soft_reset() spin in
readl_poll_timeout_atomic() for up to 20 ms total even though it already
supports sleeping polls and every caller except the crg_udc_ep_disable()
fallback (including crg_udc_probe(), crg_udc_suspend(), workqueues, and
crg_udc_irq_thread() on cable disconnects) runs in sleepable context.  In
the SCHED_FIFO IRQ thread, busy-waiting blocks lower-priority tasks on that
CPU for the duration of the reset.

[ ... ]

> +	/* Keep enabled state visible if recovery fails and disconnect is owed. */
> +	ret = crg_udc_recover(crg_udc, flags, notify_disconnect || notify_reset ||
> +			      crg_udc->disconnect_pending);
[ ... ]
> +	/* A successful recovery without a new callback owns endpoint cleanup. */
> +	if (!notify_reset && !crg_udc->disconnect_pending)
> +		for (i = 2; i < CRG_NUM_EP_CX; i++)
> +			crg_udc_ep_clear_enabled(&crg_udc->udc_ep[i]);

[Severity: Medium]
Can crg_udc_recover_link() clear usb_ep.enabled on all data endpoints while
driver->disconnect() is still running?

If usb_ep_disable() hits a hardware timeout during driver->disconnect(),
crg_udc_ep_disable() calls crg_udc_recover_endpoints() ->
crg_udc_recover_link() with disconnect_callback_active set.  Because both
crg_udc_disconnect_needed() and crg_udc_recover_endpoints() suppress new
notifications while disconnect_callback_active is true, notify_disconnect,
notify_reset, and disconnect_pending are all false, causing both
crg_udc_recover() and the post-recovery loop to clear usb_ep.enabled across
all data endpoints.

Remaining endpoints or composite functions whose disable() callbacks gate
teardown on ep->enabled (such as ncm_disable() or ecm_disable()) then
silently skip cleanup, leaving gadget ports attached and leaking request or
descriptor state.  Preserving usb_ep.enabled while
disconnect_callback_active is set and clearing disabled endpoints after
driver->disconnect() returns in crg_udc_notify_disconnect() avoids this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-axiado-ax3000-usb-device-controller-v8-0-d5ff04febd4d@axiado.com?part=6

  reply	other threads:[~2026-12-04  2:07 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  1:31 [PATCH v8 0/6] Axiado AX3000 and AX3005 USB Device Controller (UDC) Petar Stepanovic
2026-12-04  1:31 ` [PATCH v8 1/6] dt-bindings: usb: gadget: udc: add Axiado AX3000 and AX3005 UDC Petar Stepanovic
2026-12-04  1:31 ` [PATCH v8 2/6] usb: gadget: udc: crg: Add hardware definitions and structs Petar Stepanovic
2026-12-04  1:53   ` sashiko-bot
2026-12-04  1:31 ` [PATCH v8 3/6] usb: gadget: udc: crg: Add ring and transfer management Petar Stepanovic
2026-12-04  1:53   ` sashiko-bot
2026-12-04  1:31 ` [PATCH v8 4/6] usb: gadget: udc: crg: Add endpoint 0 handling Petar Stepanovic
2026-12-04  2:03   ` sashiko-bot
2026-12-04  1:31 ` [PATCH v8 5/6] usb: gadget: udc: crg: Add gadget and endpoint operations Petar Stepanovic
2026-12-04  2:03   ` sashiko-bot
2026-12-04  1:31 ` [PATCH v8 6/6] usb: gadget: udc: crg: Add core driver and build integration Petar Stepanovic
2026-12-04  2:07   ` sashiko-bot [this message]
2026-12-04  2:07 ` [PATCH v8 0/6] Axiado AX3000 and AX3005 USB Device Controller (UDC) sashiko-bot

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-248944@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=pstepanovic@axiado.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.