Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Johan Alvarado <contact@c127.dev>
To: Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Eric Dumazet <edumazet@kernel.org>,
	"David S . Miller" <davem@davemloft.net>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	Maxime Chevallier <maxime.chevallier@bootlin.com>,
	Yao Zi <me@ziyao.cc>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>,
	Alexandre Torgue <alexandre.torgue@foss.st.com>,
	netdev@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com,
	linux-arm-kernel@lists.infradead.org,
	Johan Alvarado <contact@c127.dev>,
	Pengfei Zhang <zhangfeionline@gmail.com>
Subject: [PATCH net-next 2/3] net: stmmac: add an opt-in MSI vector mask for NAPI
Date: Fri,  4 Dec 2026 15:31:29 -0500	[thread overview]
Message-ID: <20261204203130.287919-3-contact@c127.dev> (raw)
In-Reply-To: <20261204203130.287919-1-contact@c127.dev>

With one MSI per DMA channel, stmmac masks the channel interrupt in
DMA_CHAN_INTR_ENA during NAPI. On the YT6801, an RI or TI set while
masked raises no interrupt on unmask. The event waits for an unrelated
interrupt or the TX coalesce timer.

Add STMMAC_FLAG_NAPI_MASK_MSI_VEC. When it and STMMAC_FLAG_MULTI_MSI_EN
are set, the RX and TX MSI handlers disable their vector with
disable_irq_nosync() and the poll enables it again. The DMA channel
interrupt stays enabled, so the irq core resends events raised during
the poll on enable_irq(). The vendor driver does the same.

The channel records which vector it disabled, and a completing poll
undoes only its own mask. The TX timer and the shared interrupt keep
the DMA mask. stmmac_enable_all_dma_irq() and
stmmac_enable_rx/tx_queue() re-enable a vector left disabled by
napi_disable(). Freeing the interrupts clears the record.

The resend uses irq_retrigger() or CONFIG_HARDIRQS_SW_RESEND, which
arm64 and x86 select. Setting IRQ_DISABLE_UNLAZY instead would add two
PCIe writes per NAPI cycle.

The default is unchanged. The vector mask is untested on dwmac-intel
and dwmac-loongson.

Signed-off-by: Johan Alvarado <contact@c127.dev>
---
 drivers/net/ethernet/stmicro/stmmac/stmmac.h  |   3 +
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 105 ++++++++++++++++--
 include/linux/stmmac.h                        |   1 +
 3 files changed, 101 insertions(+), 8 deletions(-)

diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac.h b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
index fcc395321816..b3eb4b0d8491 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac.h
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac.h
@@ -146,6 +146,9 @@ struct stmmac_channel {
 	struct stmmac_priv *priv_data;
 	spinlock_t lock;
 	u32 index;
+	/* RX/TX MSI vector disabled for NAPI, STMMAC_FLAG_NAPI_MASK_MSI_VEC */
+	bool rx_vec_masked;
+	bool tx_vec_masked;
 };
 
 struct stmmac_fpe_cfg {
diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
index b3524d46863d..b34942ad08ba 100644
--- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
+++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
@@ -2592,6 +2592,78 @@ static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
 	stmmac_stop_tx(priv, priv->ioaddr, chan);
 }
 
+/* Mask a channel interrupt for NAPI. With STMMAC_FLAG_NAPI_MASK_MSI_VEC
+ * and per-channel MSIs, the MSI vector is disabled instead of the DMA
+ * channel interrupt, so an event raised meanwhile is resent by the irq
+ * core (lazy disable) when the vector is enabled again. Called with
+ * ch->lock held.
+ */
+static void stmmac_napi_mask_irq(struct stmmac_priv *priv, u32 chan,
+				 u32 dir)
+{
+	struct stmmac_channel *ch = &priv->channel[chan];
+
+	if ((priv->plat->flags & STMMAC_FLAG_NAPI_MASK_MSI_VEC) &&
+	    (priv->plat->flags & STMMAC_FLAG_MULTI_MSI_EN)) {
+		if (dir == DMA_DIR_RX && !ch->rx_vec_masked) {
+			ch->rx_vec_masked = true;
+			disable_irq_nosync(priv->msi->rx_irq[chan]);
+			return;
+		}
+		if (dir == DMA_DIR_TX && !ch->tx_vec_masked) {
+			ch->tx_vec_masked = true;
+			disable_irq_nosync(priv->msi->tx_irq[chan]);
+			return;
+		}
+	}
+
+	stmmac_disable_dma_irq(priv, priv->ioaddr, chan, dir != DMA_DIR_TX,
+			       dir != DMA_DIR_RX);
+}
+
+/* Enable the MSI vectors stmmac_napi_mask_irq() disabled for @rx/@tx and
+ * clear @rx/@tx for them. Called with ch->lock held.
+ */
+static void stmmac_napi_unmask_vec(struct stmmac_priv *priv, u32 chan,
+				   bool *rx, bool *tx)
+{
+	struct stmmac_channel *ch = &priv->channel[chan];
+
+	if (*rx && ch->rx_vec_masked) {
+		ch->rx_vec_masked = false;
+		enable_irq(priv->msi->rx_irq[chan]);
+		*rx = false;
+	}
+	if (*tx && ch->tx_vec_masked) {
+		ch->tx_vec_masked = false;
+		enable_irq(priv->msi->tx_irq[chan]);
+		*tx = false;
+	}
+}
+
+/* Undo stmmac_napi_mask_irq() or a DMA mask from the other NAPI
+ * schedulers when a poll completes. Called with ch->lock held.
+ */
+static void stmmac_napi_unmask_irq(struct stmmac_priv *priv, u32 chan,
+				   bool rx, bool tx)
+{
+	stmmac_napi_unmask_vec(priv, chan, &rx, &tx);
+	if (rx || tx)
+		stmmac_enable_dma_irq(priv, priv->ioaddr, chan, rx, tx);
+}
+
+/* Enable the channel interrupts when a queue (re)starts. A NAPI stopped
+ * by napi_disable() may have left its vector disabled.
+ */
+static void stmmac_start_chan_irq(struct stmmac_priv *priv, u32 chan,
+				  bool rx, bool tx)
+{
+	bool vrx = rx, vtx = tx;
+
+	stmmac_napi_unmask_vec(priv, chan, &vrx, &vtx);
+	stmmac_enable_dma_irq(priv, priv->ioaddr, chan, rx, tx);
+}
+
 static void stmmac_enable_all_dma_irq(struct stmmac_priv *priv)
 {
 	u8 rx_channels_count = priv->plat->rx_queues_to_use;
@@ -2604,7 +2676,7 @@ static void stmmac_enable_all_dma_irq(struct stmmac_priv *priv)
 		unsigned long flags;
 
 		spin_lock_irqsave(&ch->lock, flags);
-		stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
+		stmmac_start_chan_irq(priv, chan, true, true);
 		spin_unlock_irqrestore(&ch->lock, flags);
 	}
 }
@@ -3179,7 +3251,11 @@ static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan, u32 dir)
 	if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) {
 		if (napi_schedule_prep(rx_napi)) {
 			spin_lock_irqsave(&ch->lock, flags);
-			stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
+			if (dir == DMA_DIR_RX)
+				stmmac_napi_mask_irq(priv, chan, dir);
+			else
+				stmmac_disable_dma_irq(priv, priv->ioaddr, chan,
+						       1, 0);
 			spin_unlock_irqrestore(&ch->lock, flags);
 			__napi_schedule(rx_napi);
 		}
@@ -3188,7 +3264,11 @@ static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan, u32 dir)
 	if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) {
 		if (napi_schedule_prep(tx_napi)) {
 			spin_lock_irqsave(&ch->lock, flags);
-			stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
+			if (dir == DMA_DIR_TX)
+				stmmac_napi_mask_irq(priv, chan, dir);
+			else
+				stmmac_disable_dma_irq(priv, priv->ioaddr, chan,
+						       0, 1);
 			spin_unlock_irqrestore(&ch->lock, flags);
 			__napi_schedule(tx_napi);
 		}
@@ -3875,6 +3955,15 @@ static void stmmac_free_irq(struct net_device *dev,
 	struct stmmac_msi *msi = priv->msi;
 	int j;
 
+	/* NAPI and the TX timers are stopped here, so no lock is needed.
+	 * With MULTI_MSI_EN the vectors are not shared, so request_irq()
+	 * sets their disable depth back to 0.
+	 */
+	for (j = 0; j < ARRAY_SIZE(priv->channel); j++) {
+		priv->channel[j].rx_vec_masked = false;
+		priv->channel[j].tx_vec_masked = false;
+	}
+
 	switch (irq_err) {
 	case REQ_IRQ_ERR_ALL:
 		irq_idx = priv->plat->tx_queues_to_use;
@@ -6217,7 +6306,7 @@ static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget)
 		unsigned long flags;
 
 		spin_lock_irqsave(&ch->lock, flags);
-		stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
+		stmmac_napi_unmask_irq(priv, chan, true, false);
 		spin_unlock_irqrestore(&ch->lock, flags);
 	}
 
@@ -6246,7 +6335,7 @@ static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget)
 		unsigned long flags;
 
 		spin_lock_irqsave(&ch->lock, flags);
-		stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
+		stmmac_napi_unmask_irq(priv, chan, false, true);
 		spin_unlock_irqrestore(&ch->lock, flags);
 	}
 
@@ -6299,7 +6388,7 @@ static int stmmac_napi_poll_rxtx(struct napi_struct *napi, int budget)
 		/* Both RX and TX work done are complete,
 		 * so enable both RX & TX IRQs.
 		 */
-		stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 1);
+		stmmac_napi_unmask_irq(priv, chan, true, true);
 		spin_unlock_irqrestore(&ch->lock, flags);
 	}
 
@@ -7303,7 +7392,7 @@ void stmmac_enable_rx_queue(struct stmmac_priv *priv, u32 queue)
 	stmmac_start_rx_dma(priv, queue);
 
 	spin_lock_irqsave(&ch->lock, flags);
-	stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 1, 0);
+	stmmac_start_chan_irq(priv, queue, true, false);
 	spin_unlock_irqrestore(&ch->lock, flags);
 }
 
@@ -7354,7 +7443,7 @@ void stmmac_enable_tx_queue(struct stmmac_priv *priv, u32 queue)
 	stmmac_start_tx_dma(priv, queue);
 
 	spin_lock_irqsave(&ch->lock, flags);
-	stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 0, 1);
+	stmmac_start_chan_irq(priv, queue, false, true);
 	spin_unlock_irqrestore(&ch->lock, flags);
 }
 
diff --git a/include/linux/stmmac.h b/include/linux/stmmac.h
index 00be2df63d22..37e3f5c001b0 100644
--- a/include/linux/stmmac.h
+++ b/include/linux/stmmac.h
@@ -211,6 +211,7 @@ enum dwmac_core_type {
 #define STMMAC_FLAG_HWTSTAMP_CORRECT_LATENCY	BIT(14)
 #define STMMAC_FLAG_KEEP_PREAMBLE_BEFORE_SFD	BIT(15)
 #define STMMAC_FLAG_SERDES_SUPPORTS_2500M	BIT(16)
+#define STMMAC_FLAG_NAPI_MASK_MSI_VEC		BIT(17)
 
 struct mac_device_info;
 
-- 
2.56.1



  parent reply	other threads:[~2026-12-04 20:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 20:31 [PATCH net-next 0/3] net: stmmac: reduce DMA interrupt latency on YT6801 Johan Alvarado
2026-12-04 20:31 ` [PATCH net-next 1/3] net: stmmac: dwmac4: do not treat TBU as a TX event Johan Alvarado
2026-12-04 20:31 ` Johan Alvarado [this message]
2026-12-04 20:31 ` [PATCH net-next 3/3] net: stmmac: motorcomm: mask the MSI vector during NAPI Johan Alvarado

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=20261204203130.287919-3-contact@c127.dev \
    --to=contact@c127.dev \
    --cc=alexandre.torgue@foss.st.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=maxime.chevallier@bootlin.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=me@ziyao.cc \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=zhangfeionline@gmail.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