Linux-mediatek Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: jackbb_wu@compal.com
Cc: loic.poulain@oss.qualcomm.com, ryazanov.s.a@gmail.com,
	johannes@sipsolutions.net, andrew+netdev@lunn.ch,
	davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com,
	wen-zhi.huang@mediatek.com, shi-wei.yeh@mediatek.com,
	Minano.tseng@mediatek.com, matthias.bgg@gmail.com,
	angelogioacchino.delregno@collabora.com, horms@kernel.org,
	corbet@lwn.net, skhan@linuxfoundation.org, robert_yu@compal.com,
	Jeff_Chang@compal.com, edumazet@kernel.org,
	linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-mediatek@lists.infradead.org, linux-doc@vger.kernel.org
Subject: Re: [PATCH net-next v14 5/6] net: wwan: t9xx: Add FSM thread
Date: Thu, 03 Dec 2026 09:46:41 +0000	[thread overview]
Message-ID: <179629120173.3146.3576973350427912504@kernel.org> (raw)
In-Reply-To: <20261202-t9xx_driver_v1-v14-5-08ebc870ea94@compal.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 4 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] If the bare STARTUP from the IDLE report cannot be queued, the
  earlier boot's HS2_DONE bits stay set.
- [Medium] The stale-HS2 check in mtk_fsm_startup_act() is a check-then-
  act race.
- [Medium] The free paths mask a queue's L2 QUEUE_ERROR/XFER_DONE sources
  but never clear status latched while they were masked, and the alloc…
- [Medium] net: wwan: t9xx: TX queue stop timeout stalls TRB thread when
  ring is full
- [Low] Some comments describe a TX queue whose stop failed as down for
  good, but the code can bring it back.

--

net: wwan: t9xx: Add FSM thread

This adds an FSM kthread that serializes device and OS events. It drives
the HS1/HS2/HS3 runtime feature handshake for the MD and SAP sides, and it
moves port manager and CLDMA bring-up/teardown onto FSM state transitions.
It also registers the CLDMA IRQ, adds err_work for queue error recovery,
and reorders HIF teardown around a new mtk_cldma_quiesce().

> diff --git a/drivers/net/wwan/t9xx/mtk_fsm.c b/drivers/net/wwan/t9xx/mtk_fsm.c
> new file mode 100644
> index 0000000000000..06921251cbfb4
> --- /dev/null
> +++ b/drivers/net/wwan/t9xx/mtk_fsm.c
> @@ -0,0 +1,1029 @@

[ ... ]

> +static void mtk_fsm_idle_evt_handler(struct mtk_md_dev *mdev,
> +				     u32 dev_state, struct mtk_md_fsm *fsm)
> +{
> +	u32 dev_cfg = dev_state >> DEVICE_CFG_SHIFT & DEVICE_CFG_REGION_MASK;
> +	int hs_id;
> +
> +	if (dev_cfg == DEV_CFG_MD_ONLY)
> +		WRITE_ONCE(fsm->hs_done_flag, FSM_F_MD_HS_START | FSM_F_MD_HS2_DONE);
> +	else
> +		WRITE_ONCE(fsm->hs_done_flag, FSM_HS_START_MASK | FSM_HS2_DONE_MASK);
> +
> +	/* Only an IDLE reached from an earlier stage starts a new boot's
> +	 * handshake round; another report within IDLE queues nothing.
> +	 */
> +	if ((fsm->last_dev_state & REGION_BITMASK) != DEV_STAGE_IDLE) {
> +		WRITE_ONCE(fsm->boot_seq, fsm->boot_seq + 1);
> +		if (mtk_fsm_evt_submit(mdev, FSM_EVT_STARTUP, FSM_F_DFLT, NULL, 0))
> +			dev_err(mdev->dev, "Failed to submit STARTUP evt\n");
> +	}
> +
> +	/* The notification carrying this state is one-shot, so there is no
> +	 * retry to wait for: open the handshake channels whether or not the
> +	 * STARTUP above was queued.  A bare HS1 drives ON->BOOTUP as well.
> +	 */
> +	for (hs_id = 0; hs_id < HS_ID_MAX; hs_id++)
> +		mtk_fsm_unmask_ext_evt(fsm, fsm->hs_info[hs_id].mhccif_ch);
> +}

[Severity: Medium]
If mtk_fsm_evt_submit() fails here, for example because kzalloc_obj()
returns NULL, what clears the HS2_DONE bits recorded for the earlier boot?

For a new boot, the only code that drops them is the bare STARTUP branch
of mtk_fsm_startup_act():

    if (fsm->state != FSM_STATE_BOOTUP)
        mtk_fsm_switch_state(fsm, to_state, event);
    else
        fsm->fsm_flag &= ~FSM_HS2_DONE_MASK;

A new HS1 clears only its own side's bit:

    fsm->fsm_flag &= ~hs_info->fsm_flag_hs2;

Take an MD+SAP device in BOOTUP. SAP has finished HS1/HS2 and MD has sent
HS1. The modem reboots and reports IDLE, and the STARTUP allocation fails.
boot_seq is still bumped and both channels are still unmasked.

The new MD HS1 and HS2 then complete and set MD_HS2_DONE. The READY test
passes because SAP_HS2_DONE is still set from the earlier boot:

    hs_done_flag = READ_ONCE(fsm->hs_done_flag);
    if (((fsm->fsm_flag | event->fsm_flag) & hs_done_flag) == hs_done_flag) {

When the new boot's SAP HS1 arrives, the FSM is in READY and the HS1 is
refused with -EPROTO. Would the SAP control path then stay down while the
FSM reports READY?

This also seems to contradict the commit message:

    Before READY, the IDLE report of the new boot drops the HS2 results
    recorded so far

Could the HS2_DONE bits be dropped in a way that does not depend on the
event allocation succeeding?

[ ... ]

> +static int mtk_fsm_startup_act(struct mtk_md_fsm *fsm, struct mtk_fsm_evt *event)
> +{

[ ... ]

> +	} else if (event->fsm_flag & FSM_HS2_DONE_MASK) {
> +		/* This side's HS1 was handled before the device last reported
> +		 * a new boot, so the HS2 answers the earlier boot's handshake.
> +		 */
> +		if (hs_info->boot_seq != READ_ONCE(fsm->boot_seq)) {
> +			dev_warn(mdev->dev, "HS2 of an earlier boot (hs%d), dropping\n",
> +				 hs_info->id);
> +			dev_kfree_skb(skb);
> +			return 0;
> +		}
> +		if (fsm->fsm_flag & event->fsm_flag) {
> +			dev_warn(mdev->dev, "Duplicate HS2 (hs%d), dropping\n", hs_info->id);
> +			dev_kfree_skb(skb);
> +			return 0;
> +		}
> +		ret = mtk_fsm_parse_hs2_msg(fsm, hs_info, skb);
> +		if (!ret)
> +			ret = mtk_fsm_send_hs3_msg(hs_info, skb);
> +		dev_kfree_skb(skb);
> +		if (ret)
> +			goto hs_err;
> +		fsm->fsm_flag |= event->fsm_flag;
> +	}
> +
> +	hs_done_flag = READ_ONCE(fsm->hs_done_flag);
> +	if (((fsm->fsm_flag | event->fsm_flag) & hs_done_flag) == hs_done_flag) {
> +		to_state = FSM_STATE_READY;
> +		mtk_fsm_switch_state(fsm, to_state, NULL);
> +	}

[Severity: Medium]
What keeps fsm->boot_seq stable between the boot_seq check and the READY
switch below?

mtk_fsm_idle_evt_handler() bumps boot_seq from the MHCCIF work and shares
no lock with the FSM thread. The new boot's IDLE report can land after the
check but before mtk_fsm_switch_state(..., READY). In that case the
earlier boot's HS2 is still parsed, HS3 goes to the rebooted device, and
READY is reached.

The bare STARTUP and new HS1 events queued for the new boot then hit:

    if (fsm->state != FSM_STATE_ON && fsm->state != FSM_STATE_BOOTUP) {
        ret = -EPROTO;
        goto free_skb;
    }

so the modem stays down until the device is rebound.

The only exception the commit message lists is an HS2 of the earlier boot
"that completes READY before the new boot reports IDLE". Is the window
where the report arrives during HS2 handling meant to be covered by that
too?

[ ... ]

> diff --git a/drivers/net/wwan/t9xx/pcie/mtk_cldma.c b/drivers/net/wwan/t9xx/pcie/mtk_cldma.c
> index f5ede24fcd14e..0f070072f5d00 100644
> --- a/drivers/net/wwan/t9xx/pcie/mtk_cldma.c
> +++ b/drivers/net/wwan/t9xx/pcie/mtk_cldma.c

[ ... ]

> @@ -461,20 +649,28 @@ static void mtk_cldma_rearm_queues(struct cldma_drv_info *drv_info)
>  		if (txq) {
>  			spin_lock(&txq->ring_lock);
>  			req = txq->req_pool + mtk_cldma_tx_start_idx(txq);
> -			mtk_cldma_setup_start_addr(drv_info, DIR_TX, i,
> -						   req->gpd_dma_addr);
> -			if (READ_ONCE(txq->tx_started)) {
> -				/* An XFER_DONE may have been lost in the reset;
> -				 * without this free_idx never advances.
> +			if (txq->is_stopping) {
> +				/* Its stop failed: its requests are kept and
> +				 * the queue stays down until it is freed.
>  				 */

[ ... ]

>  			mtk_cldma_unmask_intr(drv_info, DIR_TX, i, QUEUE_ERROR);
>  			mtk_cldma_unmask_intr(drv_info, DIR_TX, i, QUEUE_XFER_DONE);
> -			if (READ_ONCE(txq->tx_started))
> +			if (READ_ONCE(txq->tx_started) && !txq->is_stopping)
>  				mtk_cldma_start_queue(drv_info, DIR_TX, i);
>  			spin_unlock(&txq->ring_lock);

[Severity: Low]
This isn't a bug, but the comment says the queue stays down until it is
freed. A few lines below, QUEUE_ERROR and QUEUE_XFER_DONE are unmasked for
this txq as well.

After a re-arm, a new QUEUE_ERROR goes
mtk_cldma_isr()->mtk_cldma_err_work()->mtk_cldma_err_tx_one(), which stops
the queue again. If that stop succeeds, it flushes the kept requests with
-EPIPE and runs:

    txq->is_stopping = false;

>From then on mtk_cldma_submit_tx() and mtk_cldma_start_xfer() accept
requests again.

Two other comments also describe this state as permanent: the struct txq
comment in mtk_cldma.h ("from then on if a stop of this queue failed") and
the one in mtk_cldma_tx() ("the stop is not retried").

Should these comments say that a re-arm followed by a new error can bring
the queue back? Or is the unmask here not meant for a queue with
is_stopping set?

[ ... ]

> @@ -528,12 +724,32 @@ static void mtk_cldma_txq_free(struct cldma_drv_info *drv_info, u32 txqno)
>  	}
>  	txq->tx_started = false;
>  
> +	/* Mask before waiting on anything: a QUEUE_ERROR latched after the
> +	 * flushes below would outlive this queue and be acted on for the
> +	 * next occupant of the same queue number.
> +	 */
> +	mtk_cldma_mask_intr(drv_info, DIR_TX, txqno, QUEUE_XFER_DONE);
> +	mtk_cldma_mask_intr(drv_info, DIR_TX, txqno, QUEUE_ERROR);
>  	irq_id = mtk_pci_get_virq_id(mdev, drv_info->pci_ext_irq_id);
>  	synchronize_irq(irq_id);

[ ... ]

> +	synchronize_irq(irq_id);
> +	atomic_andnot(BIT(txqno), &drv_info->tx_err_qs);
> +	flush_work(&drv_info->err_work);

[Severity: Medium]
Does masking QUEUE_ERROR here keep a late error away from the next
occupant, or does it only postpone the error to the next unmask?

mtk_cldma_get_intr_status() computes the pending bits as status & ~mask:

    *tx_sta = (*tx_sta) & (~tx_mask);

That suggests the L2 status register keeps latching bits for masked
sources. The only code that clears QUEUE_ERROR status is mtk_cldma_isr(),
and only for unmasked bits it has just seen. The atomic_andnot() and
flush_work() here clear only the software copy.

On reopen, mtk_cldma_txq_alloc() unmasks without first clearing the
queue's status:

    mtk_cldma_unmask_intr(drv_info, DIR_TX, txq->txqno, QUEUE_ERROR);

mtk_cldma_dev_init() only clears the PCIe L1 bit. mtk_cldma_rxq_free() and
mtk_cldma_rxq_alloc() follow the same pattern on RX.

Here is one sequence. mtk_cldma_err_tx_one() fails to stop the queue, so
QUEUE_ERROR stays masked, and the queue raises the error again. A later
close then stops the queue without a reset, which would leave that bit
latched.

When the channel is reopened, the stale error would fire at unmask.
mtk_cldma_err_tx_one() would then stop the new queue and complete its HWO
descriptors with -EPIPE. If that happens after the first HS1 kick, which
the device does not retry, would the handshake fail?

On RX, the stale error would set need_restart and stop/restart the newly
armed ring.

Should the free or alloc paths clear the queue's QUEUE_ERROR (and
XFER_DONE) status before unmasking?

[ ... ]

> @@ -960,15 +1308,100 @@ static void mtk_cldma_txq_flush(struct cldma_drv_info *drv_info,
>  		trb->status = status;
>  		trb->trb_complete(skb);
>  
> -		/* The service may already have been torn down; completing the
> -		 * TRB is still correct, there is just nobody left to wake.
> +		/* The service may never have been started; completing the TRB
> +		 * is still correct, there is just nobody left to wake.
>  		 */
> -		srv = trans->trb_srv[0];
>  		if (was_starved && srv)
>  			wake_up(&srv->trb_waitq);
>  	}
>  }
>  
> +static void mtk_cldma_err_tx_one(struct cldma_drv_info *drv_info, u32 qno)
> +{
> +	struct txq *txq;
> +	int ret;
> +
> +	/* pairs with smp_store_release() in txq_alloc */
> +	txq = smp_load_acquire(&drv_info->txq[qno]);
> +	if (!txq)
> +		return;
> +
> +	/* is_stopping keeps submit_tx from publishing into the ring between
> +	 * the stop and the flush.  rearm_lock: not while start_xfer is
> +	 * between its check and the kick, and held to the end so that two
> +	 * stops of this queue run one after the other and the flag is left
> +	 * as the later one found the queue.
> +	 */
> +	mutex_lock(&drv_info->rearm_lock);
> +	spin_lock(&txq->ring_lock);
> +	txq->is_stopping = true;
> +	spin_unlock(&txq->ring_lock);
> +
> +	ret = mtk_cldma_stop_queue(drv_info, DIR_TX, qno);
> +	/* -ENODEV: the link is dead, nothing is walking the ring */
> +	if (ret && ret != -ENODEV) {
> +		/* the device may still be walking the ring: unmapping its
> +		 * buffers here would leave it reading unmapped memory.
> +		 * is_stopping stays set so nothing submits to it again;
> +		 * QUEUE_ERROR stays masked until an instance re-arm.
> +		 */
> +		dev_err(drv_info->mdev->dev,
> +			"TX queue %u stop failed (%d), keeping its requests\n",
> +			qno, ret);
> +		mutex_unlock(&drv_info->rearm_lock);
> +		return;
> +	}
> +
> +	spin_lock(&txq->ring_lock);
> +	WRITE_ONCE(txq->tx_started, false);
> +	spin_unlock(&txq->ring_lock);
> +	mtk_cldma_txq_flush(drv_info, txq, -EPIPE);
> +
> +	spin_lock(&txq->ring_lock);
> +	txq->is_stopping = false;
> +	spin_unlock(&txq->ring_lock);
> +	mutex_unlock(&drv_info->rearm_lock);
> +	/* handled: re-arm what the ISR left masked */
> +	mtk_cldma_unmask_intr(drv_info, DIR_TX, qno, QUEUE_ERROR);
> +}
> +
> +/* Handed to the worker because it owns free_idx and is the only context
> + * allowed to program an RX queue; a stopped queue raises no further
> + * interrupt, so nothing else would re-arm it.
> + */
> +static void mtk_cldma_err_rx_one(struct cldma_drv_info *drv_info, u32 qno)
> +{
> +	struct rxq *rxq;
> +
> +	/* pairs with smp_store_release() in rxq_alloc */
> +	rxq = smp_load_acquire(&drv_info->rxq[qno]);
> +	if (!rxq)
> +		return;
> +
> +	atomic_set(&rxq->need_restart, 1);
> +	queue_work(drv_info->wq, &rxq->rx_done_work);
> +}
> +
> +/* The latched bits are consumed up front, so an early exit on one direction
> + * must not swallow the other's error.
> + */
> +static void mtk_cldma_err_work(struct work_struct *work)
> +{
> +	struct cldma_drv_info *drv_info = container_of(work, struct cldma_drv_info, err_work);
> +	u32 tx_err, rx_err;
> +	int i;
> +
> +	tx_err = atomic_xchg(&drv_info->tx_err_qs, 0);
> +	rx_err = atomic_xchg(&drv_info->rx_err_qs, 0);
> +
> +	for (i = 0; i < HW_QUE_NUM; i++) {
> +		if (tx_err & BIT(i))
> +			mtk_cldma_err_tx_one(drv_info, i);
> +		if (rx_err & BIT(i))
> +			mtk_cldma_err_rx_one(drv_info, i);
> +	}
> +}
> +
>  static int mtk_cldma_tx(struct cldma_dev *cd, struct sk_buff *skb)
>  {
>  	struct trb *trb = (struct trb *)skb->cb;

[Severity: Medium]
If mtk_cldma_stop_queue() fails here while the ring is full, can the
TX requests for this queue ever be completed?

is_stopping stays set and the flush is skipped. Unless the device
still completes descriptors, txq->req_budget stays at 0.

The new is_stopping check in mtk_cldma_submit_tx() sits ahead of the
req_budget check, so that the trb thread gets -EPIPE rather than
-EAGAIN. However, mtk_cldma_get_tx_budget() still returns req_budget
without looking at is_stopping.

mtk_ctrl_chs_is_busy_or_empty() only dispatches a TRB_CMD_TX at the
head of skb_list when mtk_cldma_get_tx_budget() is non-zero. With a
zero budget it keeps reporting the queue as busy, so
mtk_ctrl_trb_thread() never calls mtk_ctrl_trb_handler() for it.

That means mtk_cldma_submit_tx() is never reached. The queued TX skbs
and any new ones are never completed with -EPIPE. Nothing on this
stop-failure path wakes srv->trb_waitq either.

A blocking writer in mtk_port_send_data() would then sleep in
wait_event_interruptible() until it is signalled or the device is
removed. The commit message instead promises that "a queue whose stop
times out keeps its requests and refuses new ones".

Should mtk_cldma_get_tx_budget() report a queue with is_stopping set
as dispatchable? It already reads under ring_lock, which the locking
comment on struct txq allows for this flag. And should the
stop-failure path wake the trb thread, so that requests already
queued are failed instead of left waiting?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261202-t9xx_driver_v1-v14-0-08ebc870ea94%40compal.com


  reply	other threads:[~2026-12-03  9:46 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  8:58 [PATCH net-next v14 0/6] net: wwan: t9xx: Add MediaTek T9XX WWAN driver Jack Wu via B4 Relay
2026-12-02  8:59 ` [PATCH net-next v14 1/6] net: wwan: t9xx: Add PCIe core Jack Wu via B4 Relay
2026-12-03  9:46   ` netdev-bot+sashiko
2026-12-02  8:59 ` [PATCH net-next v14 2/6] net: wwan: t9xx: Add control plane transaction layer framework Jack Wu via B4 Relay
2026-12-02  8:59 ` [PATCH net-next v14 3/6] net: wwan: t9xx: Add control DMA interface Jack Wu via B4 Relay
2026-12-03  9:46   ` netdev-bot+sashiko
2026-12-02  8:59 ` [PATCH net-next v14 4/6] net: wwan: t9xx: Add control port Jack Wu via B4 Relay
2026-12-03  9:46   ` netdev-bot+sashiko
2026-12-02  8:59 ` [PATCH net-next v14 5/6] net: wwan: t9xx: Add FSM thread Jack Wu via B4 Relay
2026-12-03  9:46   ` netdev-bot+sashiko [this message]
2026-12-02  8:59 ` [PATCH net-next v14 6/6] net: wwan: t9xx: Add AT & MBIM WWAN ports Jack Wu via B4 Relay
2026-12-03  9:46   ` netdev-bot+sashiko

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=179629120173.3146.3576973350427912504@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=Jeff_Chang@compal.com \
    --cc=Minano.tseng@mediatek.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jackbb_wu@compal.com \
    --cc=johannes@sipsolutions.net \
    --cc=kuba@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=loic.poulain@oss.qualcomm.com \
    --cc=matthias.bgg@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=robert_yu@compal.com \
    --cc=ryazanov.s.a@gmail.com \
    --cc=shi-wei.yeh@mediatek.com \
    --cc=skhan@linuxfoundation.org \
    --cc=wen-zhi.huang@mediatek.com \
    /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