🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #26653

[PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping for RTL8127a

#26653
From: javen
Date: Fri, 04 Dec 2026 02:11
639 lines
28862 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()

Changes in v5:
 - Drop default_timestamp
 - Drop synchronize_net() from remove()
 - Splice the RX queue in the aux worker so TX is not starved
 - Drop a pending TX skb in hwtstamp_set() when TX is turned off
 - Try to turn the hardware off if enabling it fails
 - Restore timestamping only link_change_notify()

Changes in v6:
 - Add the timestamping enable path moved over from the PTP clock patch
 - Drop link_change_notify(), reprogram a reset PTP block in
   hwtstamp_set() instead
 - Don't report SOF_TIMESTAMPING_TX_SOFTWARE
---
 drivers/net/phy/realtek/Kconfig       |  10 +-
 drivers/net/phy/realtek/realtek_ptp.c | 458 +++++++++++++++++++++++++-
 2 files changed, 462 insertions(+), 6 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 16ba3fe9697a..9f82466c2b23 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,26 +10,42 @@
 #include <linux/math64.h>
 #include <linux/netdevice.h>
 #include <linux/phy.h>
+#include <linux/ptp_classify.h>
 #include <linux/pm_runtime.h>
 #include <linux/ptp_clock_kernel.h>
+#include <linux/skbuff.h>
 #include <linux/timekeeping.h>
 
 #include "realtek.h"
 
 #define RTL8127A_PTP_CTL		0xe400
 #define	RTL8127A_PTP_CTL_EN		BIT(0)
+#define	RTL8127A_PTP_CTL_TYPE_3		0x003f
 #define RTL8127A_PTP_INER		0xe402
+#define RTL8127A_PTP_INSR		0xe404
 #define	RTL8127A_PTP_INT_RX_TS		BIT(2)
 #define	RTL8127A_PTP_INT_TX_TS		BIT(3)
 #define RTL8127A_PTP_SYNCE_CTL		0xe406
 #define	RTL8127A_PTP_SYNCE_EN		BIT(0)
+#define RTL8127A_PTP_GEN_CFG		0xe408
+#define	RTL8127A_PTP_INT_CLR_MODE	BIT(0)
 #define RTL8127A_PTP_CLK_CFG		0xe410
 #define	RTL8127A_PTP_CLK_CFG_EXEC	BIT(0)
 #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
@@ -40,6 +57,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 {
@@ -50,11 +70,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;
 };
 
 static int rtl8127a_ptp_read(struct rtl8127a_ptp *ptp, u16 reg)
@@ -172,6 +214,98 @@ static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
 	return 0;
 }
 
+/* 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;
+
+	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;
+
+	return rtl8127a_ptp_poll(ptp, RTL8127A_PTP_TRX_TS_STA,
+				 RTL8127A_PTP_TRX_TS_RD, false);
+}
+
+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)
+		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)
+		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)
+			return ret;
+
+		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_GEN_CFG, 0,
+					  RTL8127A_PTP_INT_CLR_MODE);
+		if (ret)
+			return ret;
+
+		ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, 0xffff);
+		if (ret)
+			return ret;
+
+		ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CTL,
+					 RTL8127A_PTP_CTL_TYPE_3 | BIT(12));
+	} else {
+		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER,
+					  RTL8127A_PTP_INT_RX_TS |
+					  RTL8127A_PTP_INT_TX_TS, 0);
+		if (ret)
+			return ret;
+
+		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CTL,
+					  RTL8127A_PTP_CTL_EN, 0);
+	}
+	if (ret)
+		return ret;
+
+	return rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
+				   RTL8127A_PHY_RST);
+}
+
 static int rtl8127a_ptp_hwtstamp_disable(struct rtl8127a_ptp *ptp)
 {
 	int ret;
@@ -325,6 +459,124 @@ 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);
+
+	/* rtl8127a_ptp_hwtstamp_set() may have dropped it meanwhile */
+	if (!ptp->tx_skb) {
+		mutex_unlock(&ptp->lock);
+		return true;
+	}
+
+	/* 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);
+
+		rtl8127a_ptp_tx_complete(ptp, NULL);
+		mutex_unlock(&ptp->lock);
+		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);
+
+	if (ret) {
+		rtl8127a_ptp_tx_complete(ptp, NULL);
+	} else {
+		hwts.hwtstamp = timespec64_to_ktime(ts);
+		rtl8127a_ptp_tx_complete(ptp, &hwts);
+	}
+
+	mutex_unlock(&ptp->lock);
+
+	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_head received;
+	struct sk_buff *skb;
+	unsigned long flags;
+
+	/* Only handle the RX frames queued so far, so TX is not starved */
+	__skb_queue_head_init(&received);
+	spin_lock_irqsave(&ptp->rx_queue.lock, flags);
+	skb_queue_splice_tail_init(&ptp->rx_queue, &received);
+	spin_unlock_irqrestore(&ptp->rx_queue.lock, flags);
+
+	while ((skb = __skb_dequeue(&received)))
+		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",
@@ -333,8 +585,193 @@ 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,
 };
 
+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);
+	/* Reprogram the PTP clock in case it was reset meanwhile */
+	if (enable && ptp->hw_enabled) {
+		ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CTL);
+		if (ret < 0 || !(ret & RTL8127A_PTP_CTL_EN))
+			ptp->hw_enabled = false;
+		ret = 0;
+	}
+
+	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.
+			 */
+			if (enable)
+				__rtl8127a_ptp_hwtstamp_enable(ptp, false);
+			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:
+	/* Drop a pending TX timestamp that is no longer wanted */
+	if (ptp->tx_type != HWTSTAMP_TX_ON && READ_ONCE(ptp->tx_skb))
+		rtl8127a_ptp_tx_complete(ptp, NULL);
+	mutex_unlock(&ptp->lock);
+
+	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_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;
@@ -351,6 +788,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_disable(ptp);
 	if (ret)
@@ -364,7 +804,14 @@ 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;
 
 	return 0;
 }
@@ -376,7 +823,14 @@ void rtl8127a_ptp_remove(struct phy_device *phydev)
 	if (!ptp)
 		return;
 
+	phydev->mii_ts = NULL;
+
 	rtl8127a_ptp_hwtstamp_disable(ptp);
+	/* 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
Technical Details
Message-ID: <20261204021116.66-5-javen_xu@realsil.com.cn>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!newsfeed.endofthelinebbs.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376965-linux-kernel-netdev=dewy.ru
References: <20261204021116.66-1-javen_xu@realsil.com.cn>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!newsfeed.endofthelinebbs.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-376965-linux-kernel-netdev=dewy.ru
From: javen_xu@realsil.com.cn (javen)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping for RTL8127a
Date: Fri, 4 Dec 2026 10:11:16 +0800
Organization: Usenet Network
Message-ID: <20261204021116.66-5-javen_xu@realsil.com.cn>
References: <20261204021116.66-1-javen_xu@realsil.com.cn>
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2653733"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:oCZHalUSNVlupHe49sPjRIwZsHk= sha256:SNs22/T9ER1bFUBQXwtXKo3zFZUTJOOZ1tjYY9uXJ1c=
	sha1:Bav6XtYYIPqEHRQpmIqZViWQbp0= sha256:is5f3k3KZoQnSN37R0uMXof1Ak8SUytK+DR6xYXqb68=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id A9B16117AA8; Fri,  4 Dec 2026 05:13:35 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.298
X-Spam-Status: No, score=-3.298 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.001,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=realsil.com.cn
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id Ybl3bm2lZSl5;
	Fri,  4 Dec 2026 05:13:32 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-376965-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=realsil.com.cn
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 7E34F117AA2; Fri,  4 Dec 2026 05:13:24 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 35AF82AA00; Fri,  4 Dec 2026 02:13:07 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796350383;
	b=mvLuiep44aNF9rzBDPmr/LKTLsKOm6M+kmgELrUeN/0+sEQ6AJzdXNgqc2ACn9Nxaedm
	 kL59wnTzSuBsLv9F6eIqw5t0e1i2Tga14+a1aE/JrLkkhspAKJa17nbY/ktH34Oo4L6Rv
	 gZ0YHgX/4+KR9qbSXRPnM6/hZggmqt/OEeJXhTJwSho2aRGKiBfGYsOtw5bnvL+MryzFV
	 QDZ+nEDzNfctNqpVKEl/AO1nMyQ3MlEBoVvegnFOc7YLHwAJaJF9vs7+GrzJsTkN2mIaB
	 6RfZc2pyZE3ZfUCmwzMcjivqapQg9x6n14sbiZBhw2Qg5uoaQ33bGeBhdk/OjRMOw+A==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796350383;
	h=X-Original-To:Received:DMARC-Filter:X-SpamFilter-By:DKIM-Signature:
	 Received:Received:Received:From:To:CC:Subject:Date:Message-ID:
	 X-Mailer:In-Reply-To:References:X-Mailing-List:List-Id:List-Post:
	 List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
	 MIME-Version:Content-Transfer-Encoding:Content-Type;
	bh=8fFVz6h7Hs4VycOCANVNBmnGHvzpcLinfpQ294+FiUs=;
	b=u/Rp6tqJFC3Yh0E9MMzMwikFZzAOVjtC8il7PG/nxuKZWrW6yIVPuqt1c5/6fWFJRMfI
	 CH1qDf94P77CulB+xhbtMshR/MGLk/E+Ar9Z2uTM75M5CktmJ6hzTJyIWmU27ktxeArKZ
	 /Sud07z+/tC6Hf99SSEHE8gQO/YA0i/DVBaYSJVvBqu0g37S51uROY7P4GApF5r3rPcBh
	 8SaAQvCxRhlXOJEZynDCE2ZTxZE9yJ/FsZJrpOJTd3nHmjs0DggWORUUTIwYIouu8/FlP
	 Qj5HIsH6wzpEV4IiJJC2ugW+0xfqjxOUdEFv/th+KFapwTIErP1ugGTjmy5RIg4M3bA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=realsil.com.cn header.i=@realsil.com.cn header.a=rsa-sha256 header.s=dkim header.b=mYSLT0fX;
	dmarc=pass header.from=realsil.com.cn;
	spf=pass smtp.mailfrom=realsil.com.cn;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 2A7F8606A685;
	Fri,  4 Dec 2026 02:13:03 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=realsil.com.cn header.i=@realsil.com.cn header.a=rsa-sha256 header.s=dkim header.b=mYSLT0fX
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=211.75.126.72
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796350377;
	b=JyiCK+25/ATyczuKshggYJQ3H7IYLczGFbFSmMuRWQiaIJWd/jn97O5Cdz/vxIJmPEBZ
	 7o4xbO1ZuPIbaz/6697O5pV8PuBfjL5QPwnMCNhTnzQFlGm3aL04SWw9h0Fycjy282QUv
	 dnWeDT7qEV1vl/GbSqzUguHR4A4V2YTLo99LcNrGbifNXdIqO7OurWf8Lbr4AgSBd0xy6
	 7/aDZQpm2yTGv7nVMckzrxGY1vcXcnZuZzZ06NyLYJSJa71pDV6MQPPqXH5P2OhRAbt0N
	 1i67IJ8qyLS1FkGsQxRX1/GzYt/mYrXg8v4lubFkWVFXHo3GUPpQUB8OfxSEPgmGEQw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796350377;
	h=DMARC-Filter:X-SpamFilter-By:DKIM-Signature:Received:Received:
	 Received:From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:
	 References:MIME-Version:Content-Transfer-Encoding:Content-Type;
	bh=8fFVz6h7Hs4VycOCANVNBmnGHvzpcLinfpQ294+FiUs=;
	b=W6LpI9NJxLg4UMCD1Wom7fTM47W9dYReSjphkHGX1j1a9ZniWxJOS3cuxEq4D9bk8S5z
	 b5B4fPgqkKellxgr4rScpS42/sC2uVM9XcbTJC6Uh80USg+YmR4ZTQAL3ESLp6wzIEfHr
	 Vb1LDztZKSYIPsGzLQFfhOv8g3P6pLHMxIrMRd7ermQ6vtw89rTyC0JVaPWoFzzw54+ji
	 lxRRUvq4LGEojmam2Y0HDv50W1nDehe8GUSnPDIV5BE0+YGP51gwZg0ibHhrtLuIOSHDf
	 SdLXLxpXrTsy5E24JAob2Eot+drGctrdjWZbBeG1pdu4ii6h5bI2nFTfjzc0mWlezbg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=realsil.com.cn;
	spf=pass smtp.mailfrom=realsil.com.cn;
	dkim=pass header.d=realsil.com.cn header.i=@realsil.com.cn header.a=rsa-sha256 header.s=dkim header.b=mYSLT0fX;
	arc=none smtp.remote-ip=211.75.126.72
X-Received: from rtits2.realtek.com.tw (rtits2.realtek.com [211.75.126.72])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74431606A338;
	Fri,  4 Dec 2026 02:12:55 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 74431606A338
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=realsil.com.cn
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=realsil.com.cn
X-SpamFilter-By: ArmorX SpamTrap 5.80 with qID 6B42BJNE02474063, This message is accepted by code: ctloc85258
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=realsil.com.cn;
	s=dkim; t=1796350280;
	bh=8fFVz6h7Hs4VycOCANVNBmnGHvzpcLinfpQ294+FiUs=;
	h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version:Content-Transfer-Encoding:Content-Type;
	b=mYSLT0fX9WtGq+8J7tMXaRGALOxwz06ygrMm03glCtx9Ml/h3WVHcBMz6MfWjc6PE
	 YF9U3JphOp2hzlD2kXdtkPiXqt3AHdpTMGsXjcB/Vh/l6iwv7OGG6TyTNIvfjN+xyw
	 xYrG1bQXT0YNHW3sHsjtwGuTeXrroc/ECCQkqVooVbiHDqjfediWOkSL0yZfrExIvk
	 +gkWkvs+BlUcbeqtfJbVt4OqtLCikrn+NSyqpI3oVAuHcMc8ZE20xW9+evYdpziMOV
	 TRYOdHW2jjhK6KJgjMC0YnYhgjXxkZfWes9MfJXrP77gqfhq0Awlb4lApIY8ssOyRR
	 iAAYCSXwdEdBg==
X-Received: from RS-EX-MBS1.realsil.com.cn ([172.29.17.101])
	by rtits2.realtek.com.tw (8.15.2/3.29/5.94) with ESMTPS id 6B42BJNE02474063
	(version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL);
	Fri, 4 Dec 2026 10:11:19 +0800
X-Received: from RS-EX-MBS2.realsil.com.cn (172.29.17.102) by
 RS-EX-MBS1.realsil.com.cn (172.29.17.101) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 15.2.2562.56; Fri, 4 Dec 2026 10:11:18 +0800
X-Received: from 172.29.37.154 (172.29.37.152) by RS-EX-MBS2.realsil.com.cn
 (172.29.17.102) with Microsoft SMTP Server id 15.2.2562.56 via Frontend
 Transport; Fri, 4 Dec 2026 10:11:18 +0800
X-CC: <netdev@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<daniel@makrotopia.org>, <vladimir.oltean@nxp.com>, Javen Xu
	<javen_xu@realsil.com.cn>
X-Mailer: git-send-email 2.50.1.windows.1
X-In-Reply-To: <20261204021116.66-1-javen_xu@realsil.com.cn>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list