🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
10 messages
10 total messages Started by javen Wed, 25 Nov 2026 09:02
[PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
#22403
Author: javen
Date: Wed, 25 Nov 2026 09:02
629 lines
28253 bytes
From: Javen Xu <javen_xu@realsil.com.cn>

The PTP block of the RTL8127a internal PHY also timestamps PTPv2 event
messages over UDP in both directions. Register a mii_timestamper for it,
so the PHY can provide hardware timestamps without any support from the
MAC driver.

The timestamps are read out over MDIO, which may sleep, so this is done
from the PTP aux worker. Received packets are queued until their
timestamp has been read, transmitted packets are completed once the
hardware reports their timestamp.

The hardware only keeps the most recent timestamp per message type and
direction and does not record the sequenceId. Hence only one TX
timestamp can be pending at a time, it is dropped if nothing arrives
within 15 seconds, and an RX timestamp may be overwritten by a later
message of the same type before it has been read.

Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
---
Changes in v3:
 - Only take TX skbs which requested a hardware timestamp and only
   PTPv2 event messages
 - Latch the TX timestamp of the recorded message type, fail on timeout
   instead of falling back to Sync
 - Bound the RX queue, ignore PTPv1
 - Only update the cached config after programming the hardware, only
   reprogram the hardware on an on/off change
 - Drop a pending TX skb when TX timestamping is turned off
 - Back off in the aux worker instead of busy polling
 - Restore timestamping in config_init()
 - Document the per message type limitation
 - Move the NETWORK_PHY_TIMESTAMPING dependency to patch 1
 - Only support PTPv2 over UDP, drop the L2 and any other layer filters

Changes in v4:
 - Wait for in-flight rxtstamp()/txtstamp() in remove()
 - clear mii_ts after synchronize_net()
---
 drivers/net/phy/realtek/Kconfig       |  10 +-
 drivers/net/phy/realtek/realtek_ptp.c | 427 +++++++++++++++++++++++++-
 2 files changed, 420 insertions(+), 17 deletions(-)

diff --git a/drivers/net/phy/realtek/Kconfig b/drivers/net/phy/realtek/Kconfig
index feb9a7fd1582..53ccea53fd5e 100644
--- a/drivers/net/phy/realtek/Kconfig
+++ b/drivers/net/phy/realtek/Kconfig
@@ -19,9 +19,11 @@ config REALTEK_PHY_PTP
 	depends on PTP_1588_CLOCK && NETWORK_PHY_TIMESTAMPING
 	depends on !(REALTEK_PHY=y && PTP_1588_CLOCK=m)
 	help
-	  Optional PTP hardware clock support for the internal PHY of
-	  RTL8127A. The clock is provided by the PHY itself and can be
-	  read and adjusted through the PTP clock device, no support from
-	  the MAC driver is needed.
+	  Optional PTP hardware clock and hardware timestamping support
+	  for the internal PHY of RTL8127A. The PHY registers a PTP clock
+	  and timestamps PTPv2 event messages over UDP on behalf of the
+	  MAC, so no support from the MAC driver is needed.
+	  The hardware keeps only one timestamp per message type and
+	  direction, so a single PTP application on the link is assumed.
 
 endif # REALTEK_PHY
diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
index 83ee125517b0..18a2d7800f11 100644
--- a/drivers/net/phy/realtek/realtek_ptp.c
+++ b/drivers/net/phy/realtek/realtek_ptp.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
- * PTP hardware clock support for the internal PHY of RTL8127A
+ * PTP hardware clock and timestamping support for the internal PHY of
+ * RTL8127A
  */
 
 #include <linux/bitfield.h>
@@ -9,7 +10,9 @@
 #include <linux/math64.h>
 #include <linux/netdevice.h>
 #include <linux/phy.h>
+#include <linux/ptp_classify.h>
 #include <linux/ptp_clock_kernel.h>
+#include <linux/skbuff.h>
 #include <linux/timekeeping.h>
 
 #include "realtek.h"
@@ -30,8 +33,18 @@
 #define	RTL8127A_PTP_CLK_CFG_MODE	GENMASK(3, 1)
 #define RTL8127A_PHY_RST_CTL		0xa640
 #define RTL8127A_PHY_RST		BIT(15)
-/* Time registers */
+#define RTL8127A_PTP_TRX_TS_STA		0xe430
+#define	RTL8127A_PTP_TRX_TS_RD		BIT(0)
+#define	RTL8127A_PTP_TRX_TS_SEL_RX	BIT(1)
+#define	RTL8127A_PTP_TRX_TS_MSGTYPE	GENMASK(3, 2)
+#define	RTL8127A_PTP_TRX_TS_EXEC	BIT(4)
+#define	RTL8127A_PTP_TX_PDLYRSP_RDY	BIT(12)
+#define	RTL8127A_PTP_TX_PDLYREQ_RDY	BIT(13)
+#define	RTL8127A_PTP_TX_DLYREQ_RDY	BIT(14)
+#define	RTL8127A_PTP_TX_SYNC_RDY	BIT(15)
+/* Time registers, CFG_TIME for the clock, TRX_TS_TIME for packets */
 #define RTL8127A_PTP_CFG_TIME		0xe412
+#define RTL8127A_PTP_TRX_TS_TIME	0xe446
 #define	RTL8127A_PTP_NS_LO		0x0
 #define	RTL8127A_PTP_NS_HI		0x2
 #define	RTL8127A_PTP_S_LO		0x4
@@ -43,6 +56,9 @@
 #define	RTL8127A_PTP_RATE_HI_MASK	GENMASK(21, 16)
 
 #define RTL8127A_PTP_MAX_ADJ		488281
+#define RTL8127A_PTP_TX_TIMEOUT		(15 * HZ)
+#define RTL8127A_PTP_TX_POLL_FAST	msecs_to_jiffies(2)
+#define RTL8127A_PTP_RX_QUEUE_MAX	64
 
 /* RTL8127A_PTP_CLK_CFG clock adjust modes */
 enum rtl8127a_ptp_clkadj_mode {
@@ -53,11 +69,33 @@ enum rtl8127a_ptp_clkadj_mode {
 	RTL8127A_PTP_RATE_WRITE		= 14,
 };
 
+/* ptp->state bits */
+enum rtl8127a_ptp_state {
+	RTL8127A_PTP_TX_IN_PROGRESS,
+};
+
+struct rtl8127a_ptp_skb_cb {
+	u8 msgtype;
+};
+
+#define RTL8127A_PTP_SKB_CB(skb) \
+	((struct rtl8127a_ptp_skb_cb *)(skb)->cb)
+
 struct rtl8127a_ptp {
 	struct phy_device *phydev;
+	struct mii_timestamper mii_ts;
 	struct ptp_clock_info info;
 	struct ptp_clock *clock;
 	struct mutex lock;	/* serializes PTP register access sequences */
+	enum hwtstamp_tx_types tx_type;
+	enum hwtstamp_rx_filters rx_filter;
+	bool rx_enabled;
+	bool hw_enabled;
+	struct sk_buff_head rx_queue;
+	struct sk_buff *tx_skb;
+	unsigned long tx_start;
+	u8 tx_msgtype;
+	unsigned long state;
 	bool time_set;
 };
 
@@ -189,32 +227,80 @@ static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
 	return 0;
 }
 
-static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
+/* Latch the RX or TX timestamp of the given message type into the
+ * RTL8127A_PTP_TRX_TS_TIME registers.
+ */
+static int rtl8127a_ptp_trx_ts_cmd(struct rtl8127a_ptp *ptp, u16 cmd)
 {
 	int ret;
 
-	mutex_lock(&ptp->lock);
+	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_TRX_TS_STA,
+				  RTL8127A_PTP_TRX_TS_SEL_RX |
+				  RTL8127A_PTP_TRX_TS_MSGTYPE,
+				  RTL8127A_PTP_TRX_TS_RD | cmd);
+	if (ret)
+		return ret;
+
+	ret = rtl8127a_ptp_poll(ptp, RTL8127A_PTP_TRX_TS_STA,
+				RTL8127A_PTP_TRX_TS_RD, false);
+
+	return ret < 0 ? ret : 0;
+}
+
+static const u16 rtl8127a_ptp_tx_rdy[] = {
+	[PTP_MSGTYPE_SYNC]		= RTL8127A_PTP_TX_SYNC_RDY,
+	[PTP_MSGTYPE_DELAY_REQ]		= RTL8127A_PTP_TX_DLYREQ_RDY,
+	[PTP_MSGTYPE_PDELAY_REQ]	= RTL8127A_PTP_TX_PDLYREQ_RDY,
+	[PTP_MSGTYPE_PDELAY_RESP]	= RTL8127A_PTP_TX_PDLYRSP_RDY,
+};
+
+static int rtl8127a_ptp_egress_time(struct rtl8127a_ptp *ptp, u8 msgtype,
+				    struct timespec64 *ts)
+{
+	int ret;
+
+	ret = rtl8127a_ptp_poll(ptp, RTL8127A_PTP_TRX_TS_STA,
+				rtl8127a_ptp_tx_rdy[msgtype], true);
+	if (ret < 0)
+		return ret;
+
+	ret = rtl8127a_ptp_trx_ts_cmd(ptp,
+				      FIELD_PREP(RTL8127A_PTP_TRX_TS_MSGTYPE,
+						 msgtype) |
+				      RTL8127A_PTP_TRX_TS_EXEC);
+	if (ret)
+		return ret;
+
+	return rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_TRX_TS_TIME, ts);
+}
+
+static int __rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp,
+					  bool enable)
+{
+	int ret;
+
+	lockdep_assert_held(&ptp->lock);
 
 	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL,
 				  RTL8127A_PTP_SYNCE_EN, 0);
 	if (ret)
-		goto out;
+		return ret;
 
 	if (enable) {
 		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER, 0,
 					  RTL8127A_PTP_INT_RX_TS |
 					  RTL8127A_PTP_INT_TX_TS);
 		if (ret)
-			goto out;
+			return ret;
 
 		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_GEN_CFG, 0,
 					  RTL8127A_PTP_INT_CLR_MODE);
 		if (ret)
-			goto out;
+			return ret;
 
 		ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, 0xffff);
 		if (ret)
-			goto out;
+			return ret;
 
 		ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CTL,
 					 RTL8127A_PTP_CTL_TYPE_3 | BIT(12));
@@ -223,17 +309,24 @@ static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
 					  RTL8127A_PTP_INT_RX_TS |
 					  RTL8127A_PTP_INT_TX_TS, 0);
 		if (ret)
-			goto out;
+			return ret;
 
 		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CTL,
 					  RTL8127A_PTP_CTL_EN, 0);
 	}
 	if (ret)
-		goto out;
+		return ret;
 
-	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
-				  RTL8127A_PHY_RST);
-out:
+	return rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
+				   RTL8127A_PHY_RST);
+}
+
+static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
+{
+	int ret;
+
+	mutex_lock(&ptp->lock);
+	ret = __rtl8127a_ptp_hwtstamp_enable(ptp, enable);
 	mutex_unlock(&ptp->lock);
 
 	return ret;
@@ -323,6 +416,111 @@ static int rtl8127a_ptp_settime64(struct ptp_clock_info *info,
 	return rtl8127a_ptp_settime(ptp, ts);
 }
 
+static void rtl8127a_ptp_tx_complete(struct rtl8127a_ptp *ptp,
+				     struct skb_shared_hwtstamps *hwts)
+{
+	struct sk_buff *skb = ptp->tx_skb;
+
+	ptp->tx_skb = NULL;
+	clear_bit_unlock(RTL8127A_PTP_TX_IN_PROGRESS, &ptp->state);
+
+	if (hwts)
+		skb_complete_tx_timestamp(skb, hwts);
+	else
+		kfree_skb(skb);
+}
+
+/* Returns true if no TX timestamp is pending anymore */
+static bool rtl8127a_ptp_tx_tstamp(struct rtl8127a_ptp *ptp)
+{
+	struct skb_shared_hwtstamps hwts = {};
+	struct timespec64 ts;
+	int ret;
+
+	/* Pairs with smp_store_release() in rtl8127a_ptp_txtstamp() */
+	if (!smp_load_acquire(&ptp->tx_skb))
+		return true;
+
+	mutex_lock(&ptp->lock);
+
+	/* Give up if TX timestamping was turned off or nothing arrived */
+	if (READ_ONCE(ptp->tx_type) != HWTSTAMP_TX_ON ||
+	    time_is_before_jiffies(ptp->tx_start + RTL8127A_PTP_TX_TIMEOUT)) {
+		rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR,
+				   RTL8127A_PTP_INT_TX_TS);
+		mutex_unlock(&ptp->lock);
+
+		rtl8127a_ptp_tx_complete(ptp, NULL);
+		return true;
+	}
+
+	ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_INSR);
+	if (ret < 0 || !(ret & RTL8127A_PTP_INT_TX_TS)) {
+		mutex_unlock(&ptp->lock);
+		return false;
+	}
+
+	ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR,
+				 RTL8127A_PTP_INT_TX_TS);
+	if (!ret)
+		ret = rtl8127a_ptp_egress_time(ptp, ptp->tx_msgtype, &ts);
+
+	mutex_unlock(&ptp->lock);
+
+	if (ret) {
+		rtl8127a_ptp_tx_complete(ptp, NULL);
+		return true;
+	}
+
+	hwts.hwtstamp = timespec64_to_ktime(ts);
+	rtl8127a_ptp_tx_complete(ptp, &hwts);
+
+	return true;
+}
+
+/* The hardware keeps only the latest timestamp per message type and
+ * direction and does not record the sequenceID. A message of the same
+ * type received before the latch is read replaces the timestamp.
+ */
+static void rtl8127a_ptp_rx_tstamp(struct rtl8127a_ptp *ptp,
+				   struct sk_buff *skb)
+{
+	u8 msgtype = RTL8127A_PTP_SKB_CB(skb)->msgtype;
+	struct timespec64 ts;
+	int ret;
+
+	mutex_lock(&ptp->lock);
+	ret = rtl8127a_ptp_trx_ts_cmd(ptp, RTL8127A_PTP_TRX_TS_SEL_RX |
+					   FIELD_PREP(RTL8127A_PTP_TRX_TS_MSGTYPE,
+						      msgtype) |
+					   RTL8127A_PTP_TRX_TS_EXEC);
+	if (!ret)
+		ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_TRX_TS_TIME,
+					     &ts);
+	mutex_unlock(&ptp->lock);
+
+	if (!ret)
+		skb_hwtstamps(skb)->hwtstamp = timespec64_to_ktime(ts);
+
+	netif_rx(skb);
+}
+
+static long rtl8127a_ptp_do_aux_work(struct ptp_clock_info *info)
+{
+	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+	struct sk_buff *skb;
+
+	while ((skb = skb_dequeue(&ptp->rx_queue)))
+		rtl8127a_ptp_rx_tstamp(ptp, skb);
+
+	if (rtl8127a_ptp_tx_tstamp(ptp))
+		return -1;
+
+	/* The TX timestamp is normally ready right away, back off after that */
+	return time_before(jiffies, ptp->tx_start + RTL8127A_PTP_TX_POLL_FAST) ?
+	       0 : 1;
+}
+
 static const struct ptp_clock_info rtl8127a_ptp_info = {
 	.owner		= THIS_MODULE,
 	.name		= "RTL8127A PHC",
@@ -331,6 +529,7 @@ static const struct ptp_clock_info rtl8127a_ptp_info = {
 	.adjtime	= rtl8127a_ptp_adjtime,
 	.gettimex64	= rtl8127a_ptp_gettimex64,
 	.settime64	= rtl8127a_ptp_settime64,
+	.do_aux_work	= rtl8127a_ptp_do_aux_work,
 };
 
 int rtl8127a_ptp_config_init(struct phy_device *phydev)
@@ -374,6 +573,13 @@ void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
 			goto out;
 		ptp->time_set = true;
 	}
+
+	/* Restore timestamping in case the PTP block was reset */
+	if (ptp->hw_enabled) {
+		ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CTL);
+		if (ret >= 0 && !(ret & RTL8127A_PTP_CTL_EN))
+			ret = __rtl8127a_ptp_hwtstamp_enable(ptp, true);
+	}
 out:
 	mutex_unlock(&ptp->lock);
 
@@ -381,6 +587,180 @@ void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
 		phydev_warn(phydev, "failed to set PTP time: %d\n", ret);
 }
 
+static void rtl8127a_ptp_txtstamp(struct mii_timestamper *mii_ts,
+				  struct sk_buff *skb, int type)
+{
+	struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+						mii_ts);
+	struct ptp_header *hdr;
+	u8 msgtype;
+
+	if (READ_ONCE(ptp->tx_type) != HWTSTAMP_TX_ON ||
+	    !(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ||
+	    (type & PTP_CLASS_VMASK) != PTP_CLASS_V2 ||
+	    (type & PTP_CLASS_PMASK) == PTP_CLASS_L2)
+		goto drop;
+
+	hdr = ptp_parse_header(skb, type);
+	if (!hdr)
+		goto drop;
+
+	/* Only event messages are timestamped by the hardware */
+	msgtype = ptp_get_msgtype(hdr, type);
+	if (msgtype > PTP_MSGTYPE_PDELAY_RESP)
+		goto drop;
+
+	/* Only one TX timestamp can be pending at a time */
+	if (test_and_set_bit_lock(RTL8127A_PTP_TX_IN_PROGRESS, &ptp->state))
+		goto drop;
+
+	skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
+	ptp->tx_msgtype = msgtype;
+	ptp->tx_start = jiffies;
+	/* Publish tx_msgtype and tx_start before tx_skb to the aux worker */
+	smp_store_release(&ptp->tx_skb, skb);
+	ptp_schedule_worker(ptp->clock, 0);
+	return;
+
+drop:
+	kfree_skb(skb);
+}
+
+static bool rtl8127a_ptp_rxtstamp(struct mii_timestamper *mii_ts,
+				  struct sk_buff *skb, int type)
+{
+	struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+						mii_ts);
+	struct ptp_header *hdr;
+	u8 msgtype;
+
+	if (!READ_ONCE(ptp->rx_enabled) ||
+	    (type & PTP_CLASS_VMASK) != PTP_CLASS_V2 ||
+	    (type & PTP_CLASS_PMASK) == PTP_CLASS_L2 ||
+	    skb_queue_len_lockless(&ptp->rx_queue) >= RTL8127A_PTP_RX_QUEUE_MAX)
+		return false;
+
+	hdr = ptp_parse_header(skb, type);
+	if (!hdr)
+		return false;
+
+	/* Only event messages are timestamped by the hardware */
+	msgtype = ptp_get_msgtype(hdr, type);
+	if (msgtype > PTP_MSGTYPE_PDELAY_RESP)
+		return false;
+
+	RTL8127A_PTP_SKB_CB(skb)->msgtype = msgtype;
+	skb_queue_tail(&ptp->rx_queue, skb);
+	ptp_schedule_worker(ptp->clock, 0);
+
+	return true;
+}
+
+static int rtl8127a_ptp_hwtstamp_get(struct mii_timestamper *mii_ts,
+				     struct kernel_hwtstamp_config *config)
+{
+	struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+						mii_ts);
+
+	config->tx_type = ptp->tx_type;
+	config->rx_filter = ptp->rx_filter;
+
+	return 0;
+}
+
+static int rtl8127a_ptp_hwtstamp_set(struct mii_timestamper *mii_ts,
+				     struct kernel_hwtstamp_config *config,
+				     struct netlink_ext_ack *extack)
+{
+	struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+						mii_ts);
+	bool enable = false;
+	bool rx_enabled;
+	int ret = 0;
+
+	if (config->flags)
+		return -EINVAL;
+
+	switch (config->tx_type) {
+	case HWTSTAMP_TX_ON:
+		enable = true;
+		break;
+	case HWTSTAMP_TX_OFF:
+		break;
+	default:
+		return -ERANGE;
+	}
+
+	switch (config->rx_filter) {
+	case HWTSTAMP_FILTER_NONE:
+		rx_enabled = false;
+		break;
+	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
+	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
+	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
+		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
+		rx_enabled = true;
+		enable = true;
+		break;
+	default:
+		return -ERANGE;
+	}
+
+	/* The hardware is either on or off, only reprogram it on a change */
+	mutex_lock(&ptp->lock);
+	if (enable != ptp->hw_enabled) {
+		/* Stop timestamping before turning the hardware off */
+		if (!enable) {
+			WRITE_ONCE(ptp->tx_type, HWTSTAMP_TX_OFF);
+			WRITE_ONCE(ptp->rx_enabled, false);
+		}
+
+		ret = __rtl8127a_ptp_hwtstamp_enable(ptp, enable);
+		if (ret) {
+			/* The hardware state is unknown, turn timestamping
+			 * off and have the next request reprogram it.
+			 */
+			ptp->hw_enabled = false;
+			ptp->rx_filter = HWTSTAMP_FILTER_NONE;
+			WRITE_ONCE(ptp->rx_enabled, false);
+			WRITE_ONCE(ptp->tx_type, HWTSTAMP_TX_OFF);
+			goto out;
+		}
+
+		ptp->hw_enabled = enable;
+	}
+
+	ptp->rx_filter = config->rx_filter;
+	WRITE_ONCE(ptp->rx_enabled, rx_enabled);
+	WRITE_ONCE(ptp->tx_type, config->tx_type);
+out:
+	mutex_unlock(&ptp->lock);
+	/* Let the aux worker drop a TX timestamp that is no longer wanted */
+	ptp_schedule_worker(ptp->clock, 0);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_ts_info(struct mii_timestamper *mii_ts,
+				struct kernel_ethtool_ts_info *info)
+{
+	struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+						mii_ts);
+
+	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
+				SOF_TIMESTAMPING_TX_HARDWARE |
+				SOF_TIMESTAMPING_RX_HARDWARE |
+				SOF_TIMESTAMPING_RAW_HARDWARE;
+	info->phc_index = ptp_clock_index(ptp->clock);
+	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
+	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
+			   BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
+			   BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
+			   BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
+
+	return 0;
+}
+
 int rtl8127a_ptp_probe(struct phy_device *phydev)
 {
 	struct device *dev = &phydev->mdio.dev;
@@ -397,6 +777,9 @@ int rtl8127a_ptp_probe(struct phy_device *phydev)
 
 	ptp->phydev = phydev;
 	ptp->info = rtl8127a_ptp_info;
+	ptp->tx_type = HWTSTAMP_TX_OFF;
+	ptp->rx_filter = HWTSTAMP_FILTER_NONE;
+	skb_queue_head_init(&ptp->rx_queue);
 
 	ret = rtl8127a_ptp_hwtstamp_enable(ptp, false);
 	if (ret)
@@ -410,7 +793,15 @@ int rtl8127a_ptp_probe(struct phy_device *phydev)
 	if (IS_ERR(ptp->clock))
 		return PTR_ERR(ptp->clock);
 
+	ptp->mii_ts.rxtstamp = rtl8127a_ptp_rxtstamp;
+	ptp->mii_ts.txtstamp = rtl8127a_ptp_txtstamp;
+	ptp->mii_ts.hwtstamp_set = rtl8127a_ptp_hwtstamp_set;
+	ptp->mii_ts.hwtstamp_get = rtl8127a_ptp_hwtstamp_get;
+	ptp->mii_ts.ts_info = rtl8127a_ptp_ts_info;
+
 	phydev->priv = ptp;
+	phydev->mii_ts = &ptp->mii_ts;
+	phydev->default_timestamp = true;
 
 	return 0;
 }
@@ -422,7 +813,17 @@ void rtl8127a_ptp_remove(struct phy_device *phydev)
 	if (!ptp)
 		return;
 
+	phydev->default_timestamp = false;
+	/* Wait for rxtstamp()/txtstamp() callers that still see mii_ts. */
+	synchronize_net();
+	phydev->mii_ts = NULL;
+
 	rtl8127a_ptp_hwtstamp_enable(ptp, false);
+	/* Also stops the aux worker */
 	ptp_clock_unregister(ptp->clock);
+
+	if (ptp->tx_skb)
+		rtl8127a_ptp_tx_complete(ptp, NULL);
+	skb_queue_purge(&ptp->rx_queue);
 	phydev->priv = NULL;
 }
-- 
2.43.0
Re: [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
#22442
Author: Vladimir Oltean
Date: Wed, 25 Nov 2026 09:43
6 lines
16665 bytes
On Wed, Nov 25, 2026 at 05:02:16PM +0800, javen wrote:
> +	phydev->default_timestamp = true;

Enabling new instances of phydev->default_timestamp is not an option
that you have. Did you research why it exists?
RE: [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
#22906
Author: Javen
Date: Thu, 26 Nov 2026 06:13
21 lines
10569 bytes
>
>
>On Wed, Nov 25, 2026 at 05:02:16PM +0800, javen wrote:
>> +     phydev->default_timestamp = true;
>
>Enabling new instances of phydev->default_timestamp is not an option that
>you have. Did you research why it exists?

Hi Vladimir,

Thanks for review. I understand that phydev->default_timestamp was introduced to preserve legacy behavior. However, RTL8127A doesn't support mac ptp. The PHY is the only hardware timestamping block available on this link. Without setting default_timestamp = true, the current core framework simply breaks for this PHY-only chip.
How would you prefer handling such hardware without mac ptp? Any suggestion would be great helpful.

Thanks,
Javen
Re: [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
#23095
Author: Andrew Lunn
Date: Thu, 26 Nov 2026 14:08
32 lines
9965 bytes
On Thu, Nov 26, 2026 at 06:13:55AM +0000, Javen wrote:
> >
> >
> >On Wed, Nov 25, 2026 at 05:02:16PM +0800, javen wrote:
> >> +     phydev->default_timestamp = true;
> >
> >Enabling new instances of phydev->default_timestamp is not an option that
> >you have. Did you research why it exists?
> 
> Hi Vladimir,
> 

> Thanks for review. I understand that phydev->default_timestamp was
> introduced to preserve legacy behavior. However, RTL8127A doesn't
> support mac ptp. The PHY is the only hardware timestamping block
> available on this link. Without setting default_timestamp = true,
> the current core framework simply breaks for this PHY-only chip.
> How would you prefer handling such hardware without mac ptp? Any
> suggestion would be great helpful.

That sounds like something is broken somewhere. This should only come
into effect when there are two stampers available, e.g. a Marvell PHY
connected to a Marvell Switch. As far as i remember, the PHY one is
partially broken, so you want to use the MAC one, if available.

However, if there is no choice, i would not expect default_timestamp
to do anything.

Please spend some time to understand what is going on here.

       Andrew
Re: [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
#24112
Author: netdev-bot+sashiko
Date: Sun, 29 Nov 2026 09:50
477 lines
25729 bytes
Thank you for your contribution! Sashiko AI review found 10 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 6 · Low: 3

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).

- [High] rtl8127a_ptp_remove() relies on clearing
  phydev->default_timestamp and then calling synchronize_net() to keep…
- [Medium] rtl8127a_ptp_hwtstamp_set(), rtl8127a_ptp_hwtstamp_get() and
  rtl8127a_ptp_ts_info() are reached through…
- [Medium] rtl8127a_ptp_tx_tstamp() takes INSR.INT_TX_TS as proof that
  the tracked tx_skb has been timestamped.
- [Medium] rtl8127a_ptp_do_aux_work() drains rx_queue with `while ((skb =
  skb_dequeue(&ptp->rx_queue)))` before it looks at the pending TX…
- [Medium] rtl8127a_ptp_trx_ts_cmd() does a read-modify-write of
  RTL8127A_PTP_TRX_TS_STA through rtl8127a_ptp_modify().
- [Medium] rtl8127a_ptp_rx_tstamp() issues the RX latch for the skb's
  message type and attaches whatever TRX_TS_TIME holds as…
- [Medium] net: phy: realtek: pending tx_skb kept on TX timestamp toggle
  in rtl8127a_ptp_hwtstamp_set()
- [Low] When __rtl8127a_ptp_hwtstamp_enable() fails partway,
  rtl8127a_ptp_hwtstamp_set() records hw_enabled=false, rx_filter=NONE…
- [Low] The v3 changelog for this patch says "Restore timestamping in
  config_init()", but rtl8127a_ptp_config_init() only clears time_set.
- [Low] The enable sequence in __rtl8127a_ptp_hwtstamp_enable() ends by
  setting RTL8127A_PHY_RST (0xa640 bit 15) through rtl8127a_ptp_modify().

--

net: phy: realtek: add hardware timestamping for RTL8127a

This patch registers a mii_timestamper for the RTL8127a internal PHY.
With it, the PHY timestamps PTPv2 event messages over UDP without any
support from the MAC. Timestamps are read over MDIO from the PTP aux
worker: RX skbs are queued until their timestamp is latched, and only
one TX skb is tracked at a time.

> diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
> index 83ee125517b00..18a2d7800f116 100644
> --- a/drivers/net/phy/realtek/realtek_ptp.c
> +++ b/drivers/net/phy/realtek/realtek_ptp.c

[ ... ]

> @@ -189,32 +227,80 @@ static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
>  	return 0;
>  }
>  
> -static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
> +/* Latch the RX or TX timestamp of the given message type into the
> + * RTL8127A_PTP_TRX_TS_TIME registers.
> + */
> +static int rtl8127a_ptp_trx_ts_cmd(struct rtl8127a_ptp *ptp, u16 cmd)
>  {
>  	int ret;
>  
> -	mutex_lock(&ptp->lock);
> +	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_TRX_TS_STA,
> +				  RTL8127A_PTP_TRX_TS_SEL_RX |
> +				  RTL8127A_PTP_TRX_TS_MSGTYPE,
> +				  RTL8127A_PTP_TRX_TS_RD | cmd);

[Severity: Medium]
This read-modify-write masks only SEL_RX and MSGTYPE. Every other bit
read from TRX_TS_STA is written back as it was. That includes EXEC and
the TX ready bits RTL8127A_PTP_TX_PDLYRSP_RDY through
RTL8127A_PTP_TX_SYNC_RDY (bits 12-15).

If those RDY bits are write-1-to-clear, could an RX latch from the drain
loop in rtl8127a_ptp_do_aux_work() clear the RDY bit the pending tx_skb
is waiting for?

rtl8127a_ptp_do_aux_work()
    rtl8127a_ptp_rx_tstamp()
        rtl8127a_ptp_trx_ts_cmd()     <- writes TX_*_RDY back
    rtl8127a_ptp_tx_tstamp()
        rtl8127a_ptp_egress_time()
            rtl8127a_ptp_poll(..., rtl8127a_ptp_tx_rdy[msgtype], true)

In that case the RDY poll would time out after 1ms and the TX timestamp
would be dropped. One example: an E2E slave receives other slaves'
multicast Delay_Req while its own Delay_Req timestamp is still pending.

[ ... ]

> @@ -223,17 +309,24 @@ static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)

[ ... ]

>  	if (ret)
> -		goto out;
> +		return ret;
>  
> -	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
> -				  RTL8127A_PHY_RST);
> -out:
> +	return rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
> +				   RTL8127A_PHY_RST);
> +}

[Severity: Low]
This sets RTL8127A_PHY_RST but never deasserts it or polls for it to
self-clear. CLK_CFG_EXEC and TRX_TS_RD, by contrast, are polled.

Before this patch, the sequence ran only for disable, at probe and
remove. Now the enable variant also runs at runtime:

  - from rtl8127a_ptp_hwtstamp_set() on every off to on change
  - from rtl8127a_ptp_link_change_notify() right after link-up

What does this bit do? If it resets the PHY or the PTP datapath, can
the link that just came up bounce? If it is level-triggered, does the
block stay in reset?

The aux worker may also issue TRX_TS_STA commands right after this,
with no settle delay.

[ ... ]

> @@ -323,6 +416,111 @@ static int rtl8127a_ptp_settime64(struct ptp_clock_info *info,

[ ... ]

> +/* Returns true if no TX timestamp is pending anymore */
> +static bool rtl8127a_ptp_tx_tstamp(struct rtl8127a_ptp *ptp)
> +{

[ ... ]

> +	ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_INSR);
> +	if (ret < 0 || !(ret & RTL8127A_PTP_INT_TX_TS)) {
> +		mutex_unlock(&ptp->lock);
> +		return false;
> +	}
> +
> +	ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR,
> +				 RTL8127A_PTP_INT_TX_TS);
> +	if (!ret)
> +		ret = rtl8127a_ptp_egress_time(ptp, ptp->tx_msgtype, &ts);

[Severity: Medium]
Can INSR.INT_TX_TS be stale at this point?

All TX event messages share this bit, and it is acknowledged only while
a tx_skb is tracked. rtl8127a_ptp_txtstamp() does not clear INSR or the
TX_*_RDY bits when it starts tracking a new skb. So the bit can still be
set from any event frame the PHY timestamped while nothing was tracked,
for example:

  - frames sent while the hardware is on only because an RX filter was
    set (rtl8127a_ptp_hwtstamp_set() sets enable = true for any RX
    filter, even with HWTSTAMP_TX_OFF)
  - frames without SKBTX_HW_TSTAMP
  - frames dropped in rtl8127a_ptp_txtstamp() because
    RTL8127A_PTP_TX_IN_PROGRESS was already held

If TX is switched on later while RX is already on, enable equals
hw_enabled. The INSR write of 0xffff in __rtl8127a_ptp_hwtstamp_enable()
is then skipped.

r8169 calls skb_tx_timestamp() before ringing the doorbell, and
rtl8127a_ptp_txtstamp() schedules the worker with zero delay. The worker
can therefore see the stale INT_TX_TS before the tracked frame is on the
wire, and go straight into the 1ms RDY poll in
rtl8127a_ptp_egress_time().

If the RDY bit for this message type is still set from an earlier frame,
does this latch the earlier frame's timestamp and report it for this
skb? Without a sequenceId, nothing would catch the mismatch.

If the RDY bit is not set, the skb is freed without a timestamp whenever
the frame takes more than 1ms to go out.

[ ... ]

> +static void rtl8127a_ptp_rx_tstamp(struct rtl8127a_ptp *ptp,
> +				   struct sk_buff *skb)
> +{
> +	u8 msgtype = RTL8127A_PTP_SKB_CB(skb)->msgtype;
> +	struct timespec64 ts;
> +	int ret;
> +
> +	mutex_lock(&ptp->lock);
> +	ret = rtl8127a_ptp_trx_ts_cmd(ptp, RTL8127A_PTP_TRX_TS_SEL_RX |
> +					   FIELD_PREP(RTL8127A_PTP_TRX_TS_MSGTYPE,
> +						      msgtype) |
> +					   RTL8127A_PTP_TRX_TS_EXEC);
> +	if (!ret)
> +		ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_TRX_TS_TIME,
> +					     &ts);
> +	mutex_unlock(&ptp->lock);
> +
> +	if (!ret)
> +		skb_hwtstamps(skb)->hwtstamp = timespec64_to_ktime(ts);

[Severity: Medium]
Is there a check anywhere that the hardware captured a timestamp for
this frame?

The TX path checks INSR.INT_TX_TS and the per-type RDY bit. Here,
whatever TRX_TS_TIME holds is attached as a valid hardware timestamp.
__rtl8127a_ptp_hwtstamp_enable() unmasks INT_RX_TS in INER with
INT_CLR_MODE set, but nothing reads or acknowledges INT_RX_TS.

rtl8127a_ptp_rxtstamp() accepts every non-L2 PTPv2 event frame. That
includes:

  - any IPv4 or IPv6 variant covered by HWTSTAMP_FILTER_PTP_V2_L4_EVENT
  - frames received right after enable
  - frames received after a PTP block reset, before
    rtl8127a_ptp_link_change_notify() restores it

If the hardware did not timestamp such a frame, could the frame be
delivered with an earlier frame's timestamp, or with zero?

The commit message documents only overwrites by a later message of the
same type.

> +
> +	netif_rx(skb);
> +}
> +
> +static long rtl8127a_ptp_do_aux_work(struct ptp_clock_info *info)
> +{
> +	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
> +	struct sk_buff *skb;
> +
> +	while ((skb = skb_dequeue(&ptp->rx_queue)))
> +		rtl8127a_ptp_rx_tstamp(ptp, skb);
> +
> +	if (rtl8127a_ptp_tx_tstamp(ptp))
> +		return -1;

[Severity: Medium]
Can this loop starve the pending TX timestamp?

Each rtl8127a_ptp_rx_tstamp() call does about nine MDIO accesses,
including a poll of up to 1ms. While rx_enabled is set,
rtl8127a_ptp_rxtstamp() queues any PTPv2 UDP event message from any
sender. RTL8127A_PTP_RX_QUEUE_MAX bounds memory, not how long this loop
runs.

A remote host can send event messages to UDP port 319 faster than the
worker services them, and then rx_queue never drains. In that case:

  - rtl8127a_ptp_tx_tstamp() is never reached
  - the pending TX skb is dropped after RTL8127A_PTP_TX_TIMEOUT
  - the PTP kthread stays busy for as long as the traffic continues

Other drivers, such as mv88e6xxx, splice the queue and process only a
bounded batch per invocation. Would that approach work here?

[ ... ]

> @@ -374,6 +573,13 @@ void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
>  			goto out;
>  		ptp->time_set = true;
>  	}
> +
> +	/* Restore timestamping in case the PTP block was reset */
> +	if (ptp->hw_enabled) {
> +		ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CTL);
> +		if (ret >= 0 && !(ret & RTL8127A_PTP_CTL_EN))
> +			ret = __rtl8127a_ptp_hwtstamp_enable(ptp, true);
> +	}
>  out:
>  	mutex_unlock(&ptp->lock);
>  
> @@ -381,6 +587,180 @@ void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
>  		phydev_warn(phydev, "failed to set PTP time: %d\n", ret);
>  }

[Severity: Low]
The v3 changelog says:

  - Restore timestamping in config_init()

However, rtl8127a_ptp_config_init() only clears time_set. The restore is
here in rtl8127a_ptp_link_change_notify(). It runs only on link-up, and
it is skipped when setting the clock time fails and the code jumps to
out.

If the CTL read or __rtl8127a_ptp_hwtstamp_enable() fails here, the
error is logged as "failed to set PTP time". Should a failure to restore
timestamping get its own message, separate from a clock-set failure?

[ ... ]

> +	/* The hardware is either on or off, only reprogram it on a change */
> +	mutex_lock(&ptp->lock);
> +	if (enable != ptp->hw_enabled) {
> +		/* Stop timestamping before turning the hardware off */
> +		if (!enable) {
> +			WRITE_ONCE(ptp->tx_type, HWTSTAMP_TX_OFF);
> +			WRITE_ONCE(ptp->rx_enabled, false);
> +		}
> +
> +		ret = __rtl8127a_ptp_hwtstamp_enable(ptp, enable);
> +		if (ret) {
> +			/* The hardware state is unknown, turn timestamping
> +			 * off and have the next request reprogram it.
> +			 */
> +			ptp->hw_enabled = false;

[Severity: Low]
Does the next request actually reprogram the hardware?

__rtl8127a_ptp_hwtstamp_enable() returns early on any error, with no
unwind. r8168_phy_ocp_read() can return -ETIMEDOUT, so for example the
PHY_RST_CTL modify can fail after CTL has been written with CTL_EN set.

A later HWTSTAMP_TX_OFF / HWTSTAMP_FILTER_NONE request then computes
enable == false == hw_enabled and never touches the hardware.
rtl8127a_ptp_link_change_notify() also skips the restore, because
hw_enabled is false.

Can the PTP block stay enabled and keep raising INSR status while
software reports timestamping as off?

> +			ptp->rx_filter = HWTSTAMP_FILTER_NONE;
> +			WRITE_ONCE(ptp->rx_enabled, false);
> +			WRITE_ONCE(ptp->tx_type, HWTSTAMP_TX_OFF);
> +			goto out;
> +		}
> +
> +		ptp->hw_enabled = enable;
> +	}
> +
> +	ptp->rx_filter = config->rx_filter;
> +	WRITE_ONCE(ptp->rx_enabled, rx_enabled);
> +	WRITE_ONCE(ptp->tx_type, config->tx_type);
> +out:
> +	mutex_unlock(&ptp->lock);
> +	/* Let the aux worker drop a TX timestamp that is no longer wanted */
> +	ptp_schedule_worker(ptp->clock, 0);

[Severity: Medium]
Does a quick off/on toggle leave the old tx_skb pending?

When TX timestamping is turned off, rtl8127a_ptp_hwtstamp_set() only
writes HWTSTAMP_TX_OFF to ptp->tx_type and kicks the aux worker. The
pending ptp->tx_skb is dropped only if rtl8127a_ptp_tx_tstamp() still
sees tx_type != HWTSTAMP_TX_ON once it holds ptp->lock.

A second request can set HWTSTAMP_TX_ON again before that happens. The
worker may still be draining rx_queue over MDIO, or the kthread may not
have run yet. In that case the worker sees HWTSTAMP_TX_ON and keeps the
skb from the earlier session. A PTP daemon restart is one way to hit
this.

>From then on:

  - RTL8127A_PTP_TX_IN_PROGRESS stays set, so rtl8127a_ptp_txtstamp()
    drops every new TX timestamp request until RTL8127A_PTP_TX_TIMEOUT
    expires up to 15 seconds later
  - tx_msgtype and tx_start still belong to the old skb. If one of the
    dropped frames has the same message type and raises
    INSR.INT_TX_TS, rtl8127a_ptp_egress_time() latches that frame's
    timestamp and rtl8127a_ptp_tx_complete() reports it on the stale
    skb

If the off request also turned the hardware off, the following on
request goes through __rtl8127a_ptp_hwtstamp_enable() again, which
clears INSR. The old frame's timestamp is then gone for good, and only
the timeout or a wrong match can release the skb.

Would it be better to release the pending skb from
rtl8127a_ptp_hwtstamp_set() itself when TX is turned off? Another
option is to record a generation along with tx_skb and have the worker
compare it under ptp->lock. Note that the worker currently checks
tx_skb before it takes ptp->lock, so it would need to recheck tx_skb
once the lock is held.

[Severity: Medium]
What stops this callback from racing with rtl8127a_ptp_remove()?

Three callbacks are reached through RTNL or netdev ops lock callers:

  - rtl8127a_ptp_hwtstamp_set() via __phy_hwtstamp_set()
  - rtl8127a_ptp_hwtstamp_get() via __phy_hwtstamp_get()
  - rtl8127a_ptp_ts_info() via the ethtool tsinfo path

Those callers check phydev->mii_ts only once. On a sysfs unbind,
phy_remove() calls rtl8127a_ptp_remove() without holding either lock.
synchronize_net() does not wait for these process-context callers.

For example:

CPU1 (unbind)                    CPU2 (SIOCSHWTSTAMP)
rtl8127a_ptp_remove()
 rtl8127a_ptp_hwtstamp_enable()
  mutex_lock(&ptp->lock)
                                 rtl8127a_ptp_hwtstamp_set()
                                  mutex_lock(&ptp->lock) waits
  mutex_unlock(&ptp->lock)
 ptp_clock_unregister()
                                  sees hw_enabled == false
                                  __rtl8127a_ptp_hwtstamp_enable(ptp, true)
                                  ptp_schedule_worker(ptp->clock, 0)

remove() never updates hw_enabled, so CPU2 turns the hardware back on.
CPU2 then schedules work on the kthread worker that
ptp_clock_unregister() already destroyed.

rtl8127a_ptp_ts_info() can likewise call ptp_clock_index() on an
unregistered clock. Once remove() returns, all three callbacks would be
running on devm-freed ptp memory.

[ ... ]

> @@ -422,7 +813,17 @@ void rtl8127a_ptp_remove(struct phy_device *phydev)
>  	if (!ptp)
>  		return;
>  
> +	phydev->default_timestamp = false;
> +	/* Wait for rxtstamp()/txtstamp() callers that still see mii_ts. */
> +	synchronize_net();
> +	phydev->mii_ts = NULL;

[Severity: High]
Does clearing default_timestamp also stop callers that come in through
dev->hwprov?

If ethtool tsconfig selected the PHY as the hwtstamp provider, then
skb_defer_rx_timestamp() and skb_clone_tx_timestamp() take the phydev
from hwprov. They never call phy_is_default_hwtstamp():

net/core/timestamping.c:skb_defer_rx_timestamp() {
    ...
	hwprov = rcu_dereference(skb->dev->hwprov);
	if (hwprov) {
		...
		phydev = hwprov->phydev;
	} else {
		phydev = skb->dev->phydev;
		if (!phy_is_default_hwtstamp(phydev)) {
		...
	}
	rcu_read_unlock();
    ...
	mii_ts = phydev->mii_ts;
	if (likely(mii_ts->rxtstamp))
		return mii_ts->rxtstamp(mii_ts, skb, type);
    ...
}

dev->hwprov is cleared only in phy_detach(), and phy_remove() calls the
driver's remove() without detaching. Suppose the PHY driver is unbound
through sysfs while the netdev is still attached. Would the next PTP
event packet received then dereference a NULL mii_ts here?

A reader that loaded the old mii_ts before it was cleared can still
call rtl8127a_ptp_rxtstamp() or rtl8127a_ptp_txtstamp(). remove() never
clears rx_enabled or tx_type, so:

  - the skb could be queued to rx_queue, or stored in tx_skb, after
    skb_queue_purge()
  - ptp_schedule_worker() would run on the kthread worker that
    ptp_clock_unregister() destroyed
  - the ptp struct itself is devm memory and is freed after remove()
    returns

The v4 changelog says "Wait for in-flight rxtstamp()/txtstamp() in
remove()". However, skb_defer_rx_timestamp() and skb_clone_tx_timestamp()
dereference mii_ts outside the RCU section, and nothing waits after
mii_ts is cleared.

Can the hwprov path be covered as well?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125090217.1198-1-javen_xu%40realsil.com.cn
RE: [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
#24346
Author: Javen
Date: Mon, 30 Nov 2026 03:35
203 lines
16998 bytes
>
>On Thu, Nov 26, 2026 at 06:13:55AM +0000, Javen wrote:
>> >
>> >
>> >On Wed, Nov 25, 2026 at 05:02:16PM +0800, javen wrote:
>> >> +     phydev->default_timestamp = true;
>> >
>> >Enabling new instances of phydev->default_timestamp is not an option
>> >that you have. Did you research why it exists?
>>
>> Hi Vladimir,
>>
>
>> Thanks for review. I understand that phydev->default_timestamp was
>> introduced to preserve legacy behavior. However, RTL8127A doesn't
>> support mac ptp. The PHY is the only hardware timestamping block
>> available on this link. Without setting default_timestamp = true, the
>> current core framework simply breaks for this PHY-only chip.
>> How would you prefer handling such hardware without mac ptp? Any
>> suggestion would be great helpful.
>
>That sounds like something is broken somewhere. This should only come into
>effect when there are two stampers available, e.g. a Marvell PHY connected to
>a Marvell Switch. As far as i remember, the PHY one is partially broken, so you
>want to use the MAC one, if available.
>
>However, if there is no choice, i would not expect default_timestamp to do
>anything.
>
>Please spend some time to understand what is going on here.

Hi Andrew,

I think the problem is in how the core picks the default timestamp.

When both mac and phy support ptp timestamp, and the user has not selected a provider(dev->hwprov is NULL), the core uses the mac. It uses the phy only if the phy driver sets default_timestamp, which is kept for the old behaviour.

static inline bool phy_is_default_hwtstamp(struct phy_device *phydev)
{
	return phy_has_hwtstamp(phydev) && phydev->default_timestamp;
}

But this does not check if the MAC support timestamp. RTL8127 is the PHY only. Without default_timestamp, the core still picks the MAC, and the hardware timestamping does not work.

   - skb_clone_tx_timestamp() and skb_defer_rx_timestamp() return early,
     so no packet reaches the PHY
   - ethtool -T reports software timestamping only
   - SIOCSHWTSTAMP and SIOCGHWTSTAMP return -EOPNOTSUPP
   - tsconfig-set also returns -EOPNOTSUPP, so the user cannot select
     the PHY by hand either

I think that when mac cannot support timestamp but phy support, phy should be the default timestamp. I have prepared patch below. If the mac has hwtstamp NDOs, nothing changes.

diff --git a/net/core/dev.h b/net/core/dev.h
index 4e9e8cd15697..7bc3c65c249d 100644
--- a/net/core/dev.h
+++ b/net/core/dev.h
@@ -418,5 +418,6 @@ int dev_set_hwtstamp_phylib(struct net_device *dev,
 int dev_get_hwtstamp_phylib(struct net_device *dev,
 			    struct kernel_hwtstamp_config *cfg);
 int net_hwtstamp_validate(const struct kernel_hwtstamp_config *cfg);
+bool dev_phy_is_default_hwtstamp(struct net_device *dev);
 
 #endif
diff --git a/net/core/dev_ioctl.c b/net/core/dev_ioctl.c
index f6029f60c1dc..db345fe13744 100644
--- a/net/core/dev_ioctl.c
+++ b/net/core/dev_ioctl.c
@@ -32,6 +32,16 @@ static int dev_ifname(struct net *net, struct ifreq *ifr)
 	return netdev_get_name(net, ifr->ifr_name, ifr->ifr_ifindex);
 }
 
+bool dev_phy_is_default_hwtstamp(struct net_device *dev)
+{
+	struct phy_device *phydev = dev->phydev;
+
+	if (phy_is_default_hwtstamp(phydev))
+		return true;
+
+	return phy_has_hwtstamp(phydev) && !dev->netdev_ops->ndo_hwtstamp_set;
+}
+
 /*
  *	Perform a SIOCGIFCONF call. This structure will change
  *	size eventually, and there is nothing I can do about it.
@@ -274,7 +284,7 @@ int dev_get_hwtstamp_phylib(struct net_device *dev,
 		return -EOPNOTSUPP;
 	}
 
-	if (phy_is_default_hwtstamp(dev->phydev))
+	if (dev_phy_is_default_hwtstamp(dev))
 		return phy_hwtstamp_get(dev->phydev, cfg);
 
 	if (!dev->netdev_ops->ndo_hwtstamp_get)
@@ -351,7 +361,7 @@ int dev_set_hwtstamp_phylib(struct net_device *dev,
 
 		cfg->qualifier = hwprov->desc.qualifier;
 	} else {
-		phy_ts = phy_is_default_hwtstamp(dev->phydev);
+		phy_ts = dev_phy_is_default_hwtstamp(dev);
 		if (phy_ts)
 			phydev = dev->phydev;
 	}
diff --git a/net/core/timestamping.c b/net/core/timestamping.c
index a50a7ef49ae8..4dcf803b0bb3 100644
--- a/net/core/timestamping.c
+++ b/net/core/timestamping.c
@@ -10,6 +10,7 @@
 #include <linux/skbuff.h>
 #include <linux/export.h>
 #include <linux/ptp_clock_kernel.h>
+#include "dev.h"
 
 static unsigned int classify(const struct sk_buff *skb)
 {
@@ -43,7 +44,7 @@ void skb_clone_tx_timestamp(struct sk_buff *skb)
 		phydev = hwprov->phydev;
 	} else {
 		phydev = skb->dev->phydev;
-		if (!phy_is_default_hwtstamp(phydev)) {
+		if (!dev_phy_is_default_hwtstamp(skb->dev)) {
 			rcu_read_unlock();
 			return;
 		}
@@ -86,7 +87,7 @@ bool skb_defer_rx_timestamp(struct sk_buff *skb)
 		phydev = hwprov->phydev;
 	} else {
 		phydev = skb->dev->phydev;
-		if (!phy_is_default_hwtstamp(phydev)) {
+		if (!dev_phy_is_default_hwtstamp(skb->dev)) {
 			rcu_read_unlock();
 			return false;
 		}
diff --git a/net/ethtool/common.c b/net/ethtool/common.c
index ec67fbd6bb86..b15446daef58 100644
--- a/net/ethtool/common.c
+++ b/net/ethtool/common.c
@@ -1089,7 +1089,7 @@ int __ethtool_get_ts_info(struct net_device *dev,
 		struct phy_device *phydev = dev->phydev;
 
 		ethtool_init_tsinfo(info);
-		if (phy_is_default_hwtstamp(phydev) &&
+		if (dev_phy_is_default_hwtstamp(dev) &&
 		    phy_has_tsinfo(phydev)) {
 			err = phy_ts_info(phydev, info);
 			/* Report the phc source only if we have a real
diff --git a/net/ethtool/tsconfig.c b/net/ethtool/tsconfig.c
index 2db0e7ba8b9f..b099329ada06 100644
--- a/net/ethtool/tsconfig.c
+++ b/net/ethtool/tsconfig.c
@@ -44,7 +44,7 @@ static int tsconfig_prepare_data(const struct ethnl_req_info *req_base,
 	int ret;
 
 	if (!dev->netdev_ops->ndo_hwtstamp_get &&
-	    !phy_is_default_hwtstamp(dev->phydev) &&
+	    !dev_phy_is_default_hwtstamp(dev) &&
 	    !netdev_ops_lock_dereference(dev->hwprov, dev))
 		return -EOPNOTSUPP;
 
@@ -306,7 +306,7 @@ static int ethnl_set_tsconfig(struct ethnl_req_info *req_base,
 		return -ENODEV;
 
 	if (!dev->netdev_ops->ndo_hwtstamp_set &&
-	    !phy_is_default_hwtstamp(dev->phydev) &&
+	    !dev_phy_is_default_hwtstamp(dev) &&
 	    !netdev_ops_lock_dereference(dev->hwprov, dev))
 		return -EOPNOTSUPP;
 
-- 
2.43.0

I tested it on RTL8127A with net-next. With this patch, and with default_timestamp removed from the Realtek PHY driver, ptp4l works with PHY hardware timestamps.

I also have a question about the approach. Would you prefer:

   1) a new helper in net/core, as in the patch above, or
   2) changing phy_is_default_hwtstamp() itself?

For 2), phy_is_default_hwtstamp() only gets the phydev, so it would have to check the MAC through phydev->attached_dev. Then include/linux/phy.h would need to include linux/netdevice.h, which it does not do today.

Any advice would be appreciated.

Thanks,
Javen
>
>       Andrew
Re: [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
#24889
Author: Andrew Lunn
Date: Tue, 01 Dec 2026 01:25
15 lines
9464 bytes
> When both mac and phy support ptp timestamp, and the user has not
> selected a provider(dev->hwprov is NULL), the core uses the mac.

We try to keep policy out of the kernel. It is policy to decide which
stamper to use, so it should be userspace which decides and sets
dev->hwprov. This is however a new feature, so we need to ensure it
does not cause regressions with older code. So that is what this
phy_is_default_hwtstamp() is about. The Marvell stampers have been
around a while.

Since yours is a new device, you should be using dev->hwprov. I guess
it is a reasonably simple patch to ptp4l.

   Andrew
RE: [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
#24982
Author: Javen
Date: Tue, 01 Dec 2026 05:50
95 lines
13423 bytes
>
>> When both mac and phy support ptp timestamp, and the user has not
>> selected a provider(dev->hwprov is NULL), the core uses the mac.
>
>We try to keep policy out of the kernel. It is policy to decide which stamper to
>use, so it should be userspace which decides and sets
>dev->hwprov. This is however a new feature, so we need to ensure it
>does not cause regressions with older code. So that is what this
>phy_is_default_hwtstamp() is about. The Marvell stampers have been around
>a while.
>
>Since yours is a new device, you should be using dev->hwprov. I guess it is a
>reasonably simple patch to ptp4l.

Hi Andrew,

When I tried this on RTL8127A, I found that the provider cannot be selected at all. r8169 has no ndo_hwtstamp_set(), so after dropping default_timestamp the netdev has no active time stamping source, and TSCONFIG_SET fails:

sudo tools/net/ynl/pyynl/cli.py \
--spec Documentation/netlink/specs/ethtool.yaml --do tsconfig-set \
--json '{"header":{"dev-name":"enp2s0"},"hwtstamp-provider":{"index":0,"qualifier":0}}'

There are two places in ethnl_set_tsconfig() that block it:
1. The entry check returns -EOPNOTSUPP before the provider attribute is even parsed, since there is no ndo_hwtstamp_set(), no default PHY and no hwprov yet.
2. Before switching the provider, the current source is disabled with a zero config through dev_set_hwtstamp_phylib(). With no hwprov and no default PHY it goes to the MAC path and returns -EOPNOTSUPP, as there is nothing to disable.

With the change below, the request is only let through when a provider is given.

net/ethtool/tsconfig.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/net/ethtool/tsconfig.c b/net/ethtool/tsconfig.c
index 2db0e7ba8b9f..9c983666a1b0 100644
--- a/net/ethtool/tsconfig.c
+++ b/net/ethtool/tsconfig.c
@@ -300,14 +300,16 @@ static int ethnl_set_tsconfig(struct ethnl_req_info *req_base,
 	struct hwtstamp_provider *hwprov = NULL;
 	struct net_device *dev = req_base->dev;
 	struct nlattr **tb = info->attrs;
+	bool has_hwts;
 	int ret;
 
 	if (!netif_device_present(dev))
 		return -ENODEV;
 
-	if (!dev->netdev_ops->ndo_hwtstamp_set &&
-	    !phy_is_default_hwtstamp(dev->phydev) &&
-	    !netdev_ops_lock_dereference(dev->hwprov, dev))
+	has_hwts = dev->netdev_ops->ndo_hwtstamp_set ||
+		   phy_is_default_hwtstamp(dev->phydev) ||
+		   netdev_ops_lock_dereference(dev->hwprov, dev);
+	if (!has_hwts && !tb[ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER])
 		return -EOPNOTSUPP;
 
 	if (tb[ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER]) {
@@ -411,9 +413,12 @@ static int ethnl_set_tsconfig(struct ethnl_req_info *req_base,
 		/* Disable current time stamping if we try to enable
 		 * another one
 		 */
-		ret = dev_set_hwtstamp_phylib(dev, &zero_config, info->extack);
-		if (ret < 0)
-			goto err_free_hwprov;
+		if (has_hwts) {
+			ret = dev_set_hwtstamp_phylib(dev, &zero_config,
+						      info->extack);
+			if (ret < 0)
+				goto err_free_hwprov;
+		}
 
 		/* Change the selected hwtstamp source */
 		__hwprov = rcu_replace_pointer(dev->hwprov, hwprov,

Is this the right way to fix it? If so, I will add it as the first patch of v5.

Looking forward to your advice.

Thanks,
Javen
>
>   Andrew
Re: [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
#25091
Author: Nicolai Buchwitz
Date: Tue, 01 Dec 2026 08:50
83 lines
11638 bytes
Hi Javen

On 1.12.2026 06:50, Javen wrote:

> [...]

> There are two places in ethnl_set_tsconfig() that block it:
> 1. The entry check returns -EOPNOTSUPP before the provider attribute is 
> even parsed, since there is no ndo_hwtstamp_set(), no default PHY and 
> no hwprov yet.
> 2. Before switching the provider, the current source is disabled with a 
> zero config through dev_set_hwtstamp_phylib(). With no hwprov and no 
> default PHY it goes to the MAC path and returns -EOPNOTSUPP, as there 
> is nothing to disable.
> 
> With the change below, the request is only let through when a provider 
> is given.
> 
> net/ethtool/tsconfig.c | 17 +++++++++++------
>  1 file changed, 11 insertions(+), 6 deletions(-)
> 
> diff --git a/net/ethtool/tsconfig.c b/net/ethtool/tsconfig.c
> index 2db0e7ba8b9f..9c983666a1b0 100644
> --- a/net/ethtool/tsconfig.c
> +++ b/net/ethtool/tsconfig.c
> @@ -300,14 +300,16 @@ static int ethnl_set_tsconfig(struct 
> ethnl_req_info *req_base,
>  	struct hwtstamp_provider *hwprov = NULL;
>  	struct net_device *dev = req_base->dev;
>  	struct nlattr **tb = info->attrs;
> +	bool has_hwts;
>  	int ret;
> 
>  	if (!netif_device_present(dev))
>  		return -ENODEV;
> 
> -	if (!dev->netdev_ops->ndo_hwtstamp_set &&
> -	    !phy_is_default_hwtstamp(dev->phydev) &&
> -	    !netdev_ops_lock_dereference(dev->hwprov, dev))
> +	has_hwts = dev->netdev_ops->ndo_hwtstamp_set ||
> +		   phy_is_default_hwtstamp(dev->phydev) ||
> +		   netdev_ops_lock_dereference(dev->hwprov, dev);
> +	if (!has_hwts && !tb[ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER])
>  		return -EOPNOTSUPP;

Looks right. My gate (036778a4fad1) only covered the default PHY, and
tsconfig_set_hwprov_from_desc() already refuses a NETDEV source without
ndo_hwtstamp_set, so this can only select a PHY.

> 
>  	if (tb[ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER]) {
> @@ -411,9 +413,12 @@ static int ethnl_set_tsconfig(struct 
> ethnl_req_info *req_base,
>  		/* Disable current time stamping if we try to enable
>  		 * another one
>  		 */
> -		ret = dev_set_hwtstamp_phylib(dev, &zero_config, info->extack);
> -		if (ret < 0)
> -			goto err_free_hwprov;
> +		if (has_hwts) {
> +			ret = dev_set_hwtstamp_phylib(dev, &zero_config,
> +						      info->extack);
> +			if (ret < 0)
> +				goto err_free_hwprov;
> +		}
> 
>  		/* Change the selected hwtstamp source */
>  		__hwprov = rcu_replace_pointer(dev->hwprov, hwprov,
> 
> Is this the right way to fix it? If so, I will add it as the first 
> patch of v5.

IMHO yes.

Note that rtl8169_close() disconnects the PHY, and phy_detach() clears
dev->hwprov. So user space has to select the PHY again after every
down/up.

> [...]

Thanks,
Nicolai
RE: [PATCH net-next v4 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
#25120
Author: Javen
Date: Tue, 01 Dec 2026 09:17
90 lines
13436 bytes
Hi Nicolai,
>
>Hi Javen
>
>On 1.12.2026 06:50, Javen wrote:
>
>> [...]
>
>> There are two places in ethnl_set_tsconfig() that block it:
>> 1. The entry check returns -EOPNOTSUPP before the provider attribute
>> is even parsed, since there is no ndo_hwtstamp_set(), no default PHY
>> and no hwprov yet.
>> 2. Before switching the provider, the current source is disabled with
>> a zero config through dev_set_hwtstamp_phylib(). With no hwprov and no
>> default PHY it goes to the MAC path and returns -EOPNOTSUPP, as there
>> is nothing to disable.
>>
>> With the change below, the request is only let through when a provider
>> is given.
>>
>> net/ethtool/tsconfig.c | 17 +++++++++++------
>>  1 file changed, 11 insertions(+), 6 deletions(-)
>>
>> diff --git a/net/ethtool/tsconfig.c b/net/ethtool/tsconfig.c index
>> 2db0e7ba8b9f..9c983666a1b0 100644
>> --- a/net/ethtool/tsconfig.c
>> +++ b/net/ethtool/tsconfig.c
>> @@ -300,14 +300,16 @@ static int ethnl_set_tsconfig(struct
>> ethnl_req_info *req_base,
>>       struct hwtstamp_provider *hwprov = NULL;
>>       struct net_device *dev = req_base->dev;
>>       struct nlattr **tb = info->attrs;
>> +     bool has_hwts;
>>       int ret;
>>
>>       if (!netif_device_present(dev))
>>               return -ENODEV;
>>
>> -     if (!dev->netdev_ops->ndo_hwtstamp_set &&
>> -         !phy_is_default_hwtstamp(dev->phydev) &&
>> -         !netdev_ops_lock_dereference(dev->hwprov, dev))
>> +     has_hwts = dev->netdev_ops->ndo_hwtstamp_set ||
>> +                phy_is_default_hwtstamp(dev->phydev) ||
>> +                netdev_ops_lock_dereference(dev->hwprov, dev);
>> +     if (!has_hwts && !tb[ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER])
>>               return -EOPNOTSUPP;
>
>Looks right. My gate (036778a4fad1) only covered the default PHY, and
>tsconfig_set_hwprov_from_desc() already refuses a NETDEV source without
>ndo_hwtstamp_set, so this can only select a PHY.
>
>>
>>       if (tb[ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER]) { @@ -411,9
>> +413,12 @@ static int ethnl_set_tsconfig(struct ethnl_req_info
>> *req_base,
>>               /* Disable current time stamping if we try to enable
>>                * another one
>>                */
>> -             ret = dev_set_hwtstamp_phylib(dev, &zero_config, info->extack);
>> -             if (ret < 0)
>> -                     goto err_free_hwprov;
>> +             if (has_hwts) {
>> +                     ret = dev_set_hwtstamp_phylib(dev, &zero_config,
>> +                                                   info->extack);
>> +                     if (ret < 0)
>> +                             goto err_free_hwprov;
>> +             }
>>
>>               /* Change the selected hwtstamp source */
>>               __hwprov = rcu_replace_pointer(dev->hwprov, hwprov,
>>
>> Is this the right way to fix it? If so, I will add it as the first
>> patch of v5.
>
>IMHO yes.
>
>Note that rtl8169_close() disconnects the PHY, and phy_detach() clears
>dev->hwprov. So user space has to select the PHY again after every
>down/up.
>

Thanks for review.

I will add this patch as the first patch of v5.
And thanks for the reminder. I plan to resolve this issue in ptp4l.

Thanks,
Javen
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads