public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Nicolai Buchwitz <nb@tipi-net.de>
To: netdev@vger.kernel.org
Cc: andrew+netdev@lunn.ch, claudiu.beznea@tuxon.dev,
	davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	nicolas.ferre@microchip.com, pabeni@redhat.com,
	linux@armlinux.org.uk, Nicolai Buchwitz <nb@tipi-net.de>
Subject: [PATCH net-next 3/5] net: cadence: macb: implement EEE TX LPI support
Date: Mon, 23 Feb 2026 10:04:10 +0100	[thread overview]
Message-ID: <20260223090412.44070-4-nb@tipi-net.de> (raw)
In-Reply-To: <20260223090412.44070-1-nb@tipi-net.de>

Implement software-managed TX Low Power Idle (LPI) for the Cadence GEM
MAC as part of IEEE 802.3az Energy Efficient Ethernet support.

The GEM MAC has no built-in idle timer - the TXLPIEN bit (NCR bit 19)
immediately asserts LPI and blocks all TX while set. The MAC does not
auto-wake for transmit. Per Microchip GMAC documentation (section
40.6.19): "It is best to use firmware to control LPI."

This patch implements a software idle timer using delayed_work:
- On TX completion with an empty ring, schedule LPI entry after a
  configurable idle timeout (default 250ms). The work function
  verifies all TX queues are truly idle before entering LPI to
  prevent entering LPI while traffic is still active.
- On TX start, wake from LPI by clearing TXLPIEN, cancelling any
  pending re-entry, and waiting 50us for the PHY to exit LPI
  (conservative vs IEEE 802.3az Tw_sys of ~17us/~30us)
- On link up, check EEE negotiation via phy_init_eee() and defer
  first LPI entry by 1 second per IEEE 802.3az requirements
- On link down, immediately cancel pending work and clear TXLPIEN

The timer value is configurable at runtime via ethtool --set-eee
tx-timer.

The implementation is gated on MACB_CAPS_EEE so platforms must
explicitly opt in via their macb_config.

Signed-off-by: Nicolai Buchwitz <nb@tipi-net.de>
---
 drivers/net/ethernet/cadence/macb.h      |   6 ++
 drivers/net/ethernet/cadence/macb_main.c | 120 ++++++++++++++++++++++-
 2 files changed, 124 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/cadence/macb.h b/drivers/net/ethernet/cadence/macb.h
index 95317965080c..d132ad95ee61 100644
--- a/drivers/net/ethernet/cadence/macb.h
+++ b/drivers/net/ethernet/cadence/macb.h
@@ -1358,6 +1358,12 @@ struct macb {
 
 	struct macb_ptp_info	*ptp_info;	/* macb-ptp interface */
 
+	/* EEE / LPI state */
+	bool			eee_active;
+	bool			tx_lpi_enabled;
+	struct delayed_work	tx_lpi_work;
+	unsigned int		tx_lpi_timer_ms; /* idle timeout before LPI */
+
 	struct phy		*phy;
 
 	spinlock_t tsu_clk_lock; /* gem tsu clock locking */
diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
index 43cd013bb70e..d4a81f3a7b9a 100644
--- a/drivers/net/ethernet/cadence/macb_main.c
+++ b/drivers/net/ethernet/cadence/macb_main.c
@@ -589,6 +589,86 @@ static const struct phylink_pcs_ops macb_phylink_pcs_ops = {
 	.pcs_config = macb_pcs_config,
 };
 
+/* Default TX LPI idle timeout in milliseconds.
+ * The MAC will enter LPI after this period of TX inactivity.
+ */
+#define MACB_TX_LPI_TIMER_DEFAULT_MS	250
+
+/* PHY wake time from LPI in microseconds.
+ * IEEE 802.3az: Tw_sys is ~17us for 1000BASE-T, ~30us for 100BASE-TX.
+ * Use a conservative value to ensure the PHY has fully exited LPI.
+ */
+#define MACB_TX_LPI_WAKE_TIME_US	50
+
+static void macb_tx_lpi_set(struct macb *bp, bool enable)
+{
+	unsigned long flags;
+	u32 ncr;
+
+	spin_lock_irqsave(&bp->lock, flags);
+
+	ncr = macb_readl(bp, NCR);
+	if (enable)
+		ncr |= GEM_BIT(TXLPIEN);
+	else
+		ncr &= ~GEM_BIT(TXLPIEN);
+	macb_writel(bp, NCR, ncr);
+
+	bp->tx_lpi_enabled = enable;
+
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	netdev_dbg(bp->dev, "EEE TX LPI %s\n",
+		   enable ? "enabled" : "disabled");
+}
+
+/* Schedule LPI re-entry after TX idle timeout */
+static inline void macb_tx_lpi_schedule(struct macb *bp)
+{
+	if (!bp->eee_active)
+		return;
+
+	mod_delayed_work(system_wq, &bp->tx_lpi_work,
+			 msecs_to_jiffies(bp->tx_lpi_timer_ms));
+}
+
+static void macb_tx_lpi_work_fn(struct work_struct *work)
+{
+	struct macb *bp = container_of(work, struct macb, tx_lpi_work.work);
+	unsigned int q;
+
+	if (!bp->eee_active)
+		return;
+
+	/* Only enter LPI if all TX queues are truly idle. The timer may
+	 * have been scheduled when one queue drained but traffic resumed
+	 * before the timer fired.
+	 */
+	for (q = 0; q < bp->num_queues; q++) {
+		if (bp->queues[q].tx_head != bp->queues[q].tx_tail) {
+			/* TX still active, reschedule and check again later */
+			macb_tx_lpi_schedule(bp);
+			return;
+		}
+	}
+
+	macb_tx_lpi_set(bp, true);
+}
+
+/* Called from TX path to wake from LPI before transmitting */
+static inline void macb_tx_lpi_wake(struct macb *bp)
+{
+	if (!bp->tx_lpi_enabled)
+		return;
+
+	macb_tx_lpi_set(bp, false);
+	/* Cancel any pending re-entry */
+	cancel_delayed_work(&bp->tx_lpi_work);
+
+	/* Wait for PHY to exit LPI before transmitting */
+	udelay(MACB_TX_LPI_WAKE_TIME_US);
+}
+
 static void macb_mac_config(struct phylink_config *config, unsigned int mode,
 			    const struct phylink_link_state *state)
 {
@@ -661,10 +741,16 @@ static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
 			queue_writel(queue, IDR,
 				     bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
 
-	/* Disable Rx and Tx */
-	ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
+	/* Cancel any pending LPI entry */
+	cancel_delayed_work(&bp->tx_lpi_work);
+
+	/* Disable TX LPI, Rx, and Tx */
+	ctrl = macb_readl(bp, NCR) & ~(GEM_BIT(TXLPIEN) | MACB_BIT(RE) | MACB_BIT(TE));
 	macb_writel(bp, NCR, ctrl);
 
+	bp->eee_active = false;
+	bp->tx_lpi_enabled = false;
+
 	netif_tx_stop_all_queues(ndev);
 }
 
@@ -732,6 +818,19 @@ static void macb_mac_link_up(struct phylink_config *config,
 	macb_writel(bp, NCR, ctrl | MACB_BIT(RE) | MACB_BIT(TE));
 
 	netif_tx_wake_all_queues(ndev);
+
+	/* EEE: check if link partner negotiated EEE.
+	 * Per IEEE 802.3az / Microchip GMAC docs: LPI must not be
+	 * requested until the link has been up for at least 1 second.
+	 */
+	if (phy && (bp->caps & MACB_CAPS_EEE)) {
+		bp->eee_active = phy_init_eee(phy, false) >= 0 &&
+				 phy->enable_tx_lpi;
+		netdev_dbg(ndev, "EEE: active=%d\n", bp->eee_active);
+		if (bp->eee_active)
+			schedule_delayed_work(&bp->tx_lpi_work,
+					      msecs_to_jiffies(1000));
+	}
 }
 
 static struct phylink_pcs *macb_mac_select_pcs(struct phylink_config *config,
@@ -1242,6 +1341,11 @@ static int macb_tx_complete(struct macb_queue *queue, int budget)
 	    CIRC_CNT(queue->tx_head, queue->tx_tail,
 		     bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
 		netif_wake_subqueue(bp->dev, queue_index);
+
+	/* Schedule LPI re-entry when TX ring is drained */
+	if (queue->tx_head == queue->tx_tail)
+		macb_tx_lpi_schedule(bp);
+
 	spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
 
 	return packets;
@@ -2270,6 +2374,10 @@ static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
 	bool is_lso;
 	netdev_tx_t ret = NETDEV_TX_OK;
 
+	/* Wake from LPI before transmitting */
+	if (unlikely(bp->tx_lpi_enabled))
+		macb_tx_lpi_wake(bp);
+
 	if (macb_clear_csum(skb)) {
 		dev_kfree_skb_any(skb);
 		return ret;
@@ -2973,6 +3081,9 @@ static int macb_open(struct net_device *dev)
 	if (err)
 		goto phy_off;
 
+	if ((bp->caps & MACB_CAPS_EEE) && dev->phydev)
+		phy_support_eee(dev->phydev);
+
 	netif_tx_start_all_queues(dev);
 
 	if (bp->ptp_info)
@@ -3004,6 +3115,8 @@ static int macb_close(struct net_device *dev)
 
 	netif_tx_stop_all_queues(dev);
 
+	cancel_delayed_work_sync(&bp->tx_lpi_work);
+
 	for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
 		napi_disable(&queue->napi_rx);
 		napi_disable(&queue->napi_tx);
@@ -5616,6 +5729,8 @@ static int macb_probe(struct platform_device *pdev)
 	}
 
 	INIT_WORK(&bp->hresp_err_bh_work, macb_hresp_error_task);
+	INIT_DELAYED_WORK(&bp->tx_lpi_work, macb_tx_lpi_work_fn);
+	bp->tx_lpi_timer_ms = MACB_TX_LPI_TIMER_DEFAULT_MS;
 
 	netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
 		    macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
@@ -5659,6 +5774,7 @@ static void macb_remove(struct platform_device *pdev)
 		mdiobus_free(bp->mii_bus);
 
 		device_set_wakeup_enable(&bp->pdev->dev, 0);
+		cancel_delayed_work_sync(&bp->tx_lpi_work);
 		cancel_work_sync(&bp->hresp_err_bh_work);
 		pm_runtime_disable(&pdev->dev);
 		pm_runtime_dont_use_autosuspend(&pdev->dev);
-- 
2.39.5


  parent reply	other threads:[~2026-02-23  9:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-23  9:04 [PATCH net-next 0/5] net: cadence: macb: add IEEE 802.3az EEE support Nicolai Buchwitz
2026-02-23  9:04 ` [PATCH net-next 1/5] net: cadence: macb: add EEE register definitions and capability flag Nicolai Buchwitz
2026-02-23  9:04 ` [PATCH net-next 2/5] net: cadence: macb: add EEE LPI statistics counters Nicolai Buchwitz
2026-02-23  9:04 ` Nicolai Buchwitz [this message]
2026-02-23 10:07   ` [PATCH net-next 3/5] net: cadence: macb: implement EEE TX LPI support Russell King (Oracle)
2026-02-23 14:54     ` nb
2026-02-23  9:04 ` [PATCH net-next 4/5] net: cadence: macb: add ethtool EEE support Nicolai Buchwitz
2026-02-23  9:04 ` [PATCH net-next 5/5] net: cadence: macb: enable EEE for Raspberry Pi RP1 Nicolai Buchwitz
2026-02-23  9:14 ` [PATCH net-next 0/5] net: cadence: macb: add IEEE 802.3az EEE support Russell King (Oracle)

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=20260223090412.44070-4-nb@tipi-net.de \
    --to=nb@tipi-net.de \
    --cc=andrew+netdev@lunn.ch \
    --cc=claudiu.beznea@tuxon.dev \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=netdev@vger.kernel.org \
    --cc=nicolas.ferre@microchip.com \
    --cc=pabeni@redhat.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