Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a
@ 2026-12-01  9:44 javen
  2026-12-01  9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
                   ` (3 more replies)
  0 siblings, 4 replies; 24+ messages in thread
From: javen @ 2026-12-01  9:44 UTC (permalink / raw)
  To: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier
  Cc: netdev, linux-kernel, daniel, vladimir.oltean, Javen

From: Javen <javen_xu@realsil.com.cn>

The PTP block of the RTL8127A resides in its internal PHY (RTL8261C).
Therefore, this series adds PTP support to the Realtek PHY driver
instead of the r8169 MAC driver.

Javen (1):
  net: ethtool: tsconfig: allow selecting a provider

Javen Xu (3):
  r8169: hold MDIO bus lock while loading firmware
  net: phy: realtek: add PTP clock support for RTL8127a
  net: phy: realtek: add hardware timestamping for RTL8127a

 drivers/net/ethernet/realtek/r8169_main.c |   5 +
 drivers/net/phy/realtek/Kconfig           |  13 +
 drivers/net/phy/realtek/Makefile          |   1 +
 drivers/net/phy/realtek/realtek.h         |  19 +
 drivers/net/phy/realtek/realtek_main.c    |  24 +-
 drivers/net/phy/realtek/realtek_ptp.c     | 807 ++++++++++++++++++++++
 net/ethtool/tsconfig.c                    |  17 +-
 7 files changed, 879 insertions(+), 7 deletions(-)
 create mode 100644 drivers/net/phy/realtek/realtek_ptp.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 24+ messages in thread

* [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider
  2026-12-01  9:44 [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a javen
@ 2026-12-01  9:44 ` javen
  2026-12-01 10:06   ` Nicolai Buchwitz
                     ` (2 more replies)
  2026-12-01  9:44 ` [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware javen
                   ` (2 subsequent siblings)
  3 siblings, 3 replies; 24+ messages in thread
From: javen @ 2026-12-01  9:44 UTC (permalink / raw)
  To: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier
  Cc: netdev, linux-kernel, daniel, vladimir.oltean, Javen

From: Javen <javen_xu@realsil.com.cn>

A hwtstamp provider can obly ne selected with ETHTOOL_MSG_TSCONFIG_SET
when the device already has an active time stamping source: a MAC
implementing ndo_hwtstamp_set(), a PHY flagged as default_timestamp, or
a previously selected dev->hwprov.

This patch makes it possible to select a PHY time stamper on a MAC
without time stamping support. New phy drivers are expected to reply on
dev->hwprov rather than default_timestamp.

Signed-off-by: Javen <javen_xu@realsil.com.cn>
---
Changes in v5:
 - new file.
---
 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,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware
  2026-12-01  9:44 [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a javen
  2026-12-01  9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
@ 2026-12-01  9:44 ` javen
  2026-12-01 10:18   ` Nicolai Buchwitz
                     ` (2 more replies)
  2026-12-01  9:44 ` [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen
  2026-12-01  9:44 ` [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping " javen
  3 siblings, 3 replies; 24+ messages in thread
From: javen @ 2026-12-01  9:44 UTC (permalink / raw)
  To: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier
  Cc: netdev, linux-kernel, daniel, vladimir.oltean, Javen Xu

From: Javen Xu <javen_xu@realsil.com.cn>

rtl_fw_write_firmware() accesses PHY registers directly, bypassing
phylib. Hold MDIO bus lock meanwhile, so other phylib users cannot
interleave their accesses with the firmware sequence.

Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
---
Changes in v3:
 - New patch

Changes in v4:
 - Skip the bus lock on PHY-less RTL8116af
 - Restore tp->ocp_base before releasing the bus lock

Changes in v5:
 - no changes
---
 drivers/net/ethernet/realtek/r8169_main.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index f151ab681393..b289ff6c3064 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -2686,9 +2686,14 @@ void r8169_apply_firmware(struct rtl8169_private *tp)
 
 	/* TODO: release firmware if rtl_fw_write_firmware signals failure. */
 	if (tp->rtl_fw) {
+		/* Keep phylib users (hwmon, PTP) off the bus meanwhile. */
+		if (tp->phydev)
+			phy_lock_mdio_bus(tp->phydev);
 		rtl_fw_write_firmware(tp, tp->rtl_fw);
 		/* At least one firmware doesn't reset tp->ocp_base. */
 		tp->ocp_base = OCP_STD_PHY_BASE;
+		if (tp->phydev)
+			phy_unlock_mdio_bus(tp->phydev);
 
 		/* PHY soft reset may still be in progress */
 		if (tp->phydev)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a
  2026-12-01  9:44 [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a javen
  2026-12-01  9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
  2026-12-01  9:44 ` [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware javen
@ 2026-12-01  9:44 ` javen
  2026-12-01 10:23   ` Nicolai Buchwitz
                     ` (2 more replies)
  2026-12-01  9:44 ` [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping " javen
  3 siblings, 3 replies; 24+ messages in thread
From: javen @ 2026-12-01  9:44 UTC (permalink / raw)
  To: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier
  Cc: netdev, linux-kernel, daniel, vladimir.oltean, Javen Xu

From: Javen Xu <javen_xu@realsil.com.cn>

The internal PHY of RTL8127A (RTL8261C, PHY ID 0x001cc890) contains a
PTP block with a free running clock that can be read, set, stepped and
frequency adjusted through vendor MMD registers.

Register a PTP hardware clock for it. All register accesses go through
the normal blocking MDIO path, multi register sequences are serialized
by a driver mutex. The frequency adjustment is cleared at probe. The
clock is set from the system time when the link comes up for the first
time after a PHY init or link loss.

Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
---
Changes in v3:
 - Keep the clock running across PHY re-initializations, only start it
   from the system time if the counter has been reset
 - Only bracket the latching EXEC write with the system timestamps in
   gettimex64()
 - Clear phydev->priv in remove()
 - Add defines for the PHY reset register
 - Depend on NETWORK_PHY_TIMESTAMPING from this patch on

Changes in v4:
 - Set the clock from the system time on link up, after a PHY init or
   link loss
 - Don't fail the PHY probe if PTP setup fails

Changes in v5:
 - Don't set the clock from the system time on link up, leave the time
   to userspace.
---
 drivers/net/phy/realtek/Kconfig        |  11 +
 drivers/net/phy/realtek/Makefile       |   1 +
 drivers/net/phy/realtek/realtek.h      |  14 +
 drivers/net/phy/realtek/realtek_main.c |  23 +-
 drivers/net/phy/realtek/realtek_ptp.c  | 380 +++++++++++++++++++++++++
 5 files changed, 428 insertions(+), 1 deletion(-)
 create mode 100644 drivers/net/phy/realtek/realtek_ptp.c

diff --git a/drivers/net/phy/realtek/Kconfig b/drivers/net/phy/realtek/Kconfig
index a741b34d193e..feb9a7fd1582 100644
--- a/drivers/net/phy/realtek/Kconfig
+++ b/drivers/net/phy/realtek/Kconfig
@@ -13,4 +13,15 @@ config REALTEK_PHY_HWMON
 	help
 	  Optional hwmon support for the temperature sensor
 
+config REALTEK_PHY_PTP
+	bool "PTP support for Realtek PHYs"
+	default y
+	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.
+
 endif # REALTEK_PHY
diff --git a/drivers/net/phy/realtek/Makefile b/drivers/net/phy/realtek/Makefile
index dd21cf87f2f1..450bd05b7ef9 100644
--- a/drivers/net/phy/realtek/Makefile
+++ b/drivers/net/phy/realtek/Makefile
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 realtek-y			+= realtek_main.o
 realtek-$(CONFIG_REALTEK_PHY_HWMON) += realtek_hwmon.o
+realtek-$(CONFIG_REALTEK_PHY_PTP) += realtek_ptp.o
 obj-$(CONFIG_REALTEK_PHY)	+= realtek.o
diff --git a/drivers/net/phy/realtek/realtek.h b/drivers/net/phy/realtek/realtek.h
index a39b44fa18a0..d1dc79c396e5 100644
--- a/drivers/net/phy/realtek/realtek.h
+++ b/drivers/net/phy/realtek/realtek.h
@@ -7,4 +7,18 @@
 
 int rtl822x_hwmon_init(struct phy_device *phydev);
 
+#if IS_ENABLED(CONFIG_REALTEK_PHY_PTP)
+int rtl8127a_ptp_probe(struct phy_device *phydev);
+void rtl8127a_ptp_remove(struct phy_device *phydev);
+#else
+static inline int rtl8127a_ptp_probe(struct phy_device *phydev)
+{
+	return 0;
+}
+
+static inline void rtl8127a_ptp_remove(struct phy_device *phydev)
+{
+}
+#endif
+
 #endif /* REALTEK_H */
diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index a39ad18382f2..483eaf6fb6b1 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -1839,6 +1839,26 @@ static int rtl822x_probe(struct phy_device *phydev)
 	return 0;
 }
 
+static int rtl_internal_nbaset_probe(struct phy_device *phydev)
+{
+	int ret;
+
+	ret = rtl822x_probe(phydev);
+	if (ret)
+		return ret;
+
+	/* Only the internal PHY of RTL8127A has a PTP block. PTP is
+	 * optional, keep the PHY usable without it.
+	 */
+	if (phydev->phy_id == RTL_8261C) {
+		ret = rtl8127a_ptp_probe(phydev);
+		if (ret)
+			phydev_warn(phydev, "PTP support disabled: %d\n", ret);
+	}
+
+	return 0;
+}
+
 /* RTL822x cannot access MDIO_MMD_VEND2 via MII_MMD_CTRL/MII_MMD_DATA.
  * A mapping to use paged access needs to be used instead.
  * All other MMD devices can be accessed as usual.
@@ -3413,7 +3433,8 @@ static struct phy_driver realtek_drvs[] = {
 		.match_phy_device = rtl_internal_nbaset_match_phy_device,
 		.name		= "Realtek Internal NBASE-T PHY",
 		.flags		= PHY_IS_INTERNAL,
-		.probe		= rtl822x_probe,
+		.probe		= rtl_internal_nbaset_probe,
+		.remove		= rtl8127a_ptp_remove,
 		.get_features	= rtl822x_get_features,
 		.config_aneg	= rtl822x_config_aneg,
 		.read_status	= rtl822x_read_status,
diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
new file mode 100644
index 000000000000..72d0663a90c2
--- /dev/null
+++ b/drivers/net/phy/realtek/realtek_ptp.c
@@ -0,0 +1,380 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * PTP hardware clock support for the internal PHY of RTL8127A
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/iopoll.h>
+#include <linux/math64.h>
+#include <linux/netdevice.h>
+#include <linux/phy.h>
+#include <linux/ptp_clock_kernel.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_CFG_TIME		0xe412
+#define	RTL8127A_PTP_NS_LO		0x0
+#define	RTL8127A_PTP_NS_HI		0x2
+#define	RTL8127A_PTP_S_LO		0x4
+#define	RTL8127A_PTP_S_MI		0x6
+#define	RTL8127A_PTP_S_HI		0x8
+#define	RTL8127A_PTP_NS_HI_MASK		GENMASK(13, 0)
+#define	RTL8127A_PTP_NSEC_MASK		GENMASK(29, 0)
+#define	RTL8127A_PTP_SEC_MASK		GENMASK_ULL(47, 0)
+#define	RTL8127A_PTP_RATE_HI_MASK	GENMASK(21, 16)
+
+#define RTL8127A_PTP_MAX_ADJ		488281
+
+/* RTL8127A_PTP_CLK_CFG clock adjust modes */
+enum rtl8127a_ptp_clkadj_mode {
+	RTL8127A_PTP_DIRECT_READ	= 4,
+	RTL8127A_PTP_DIRECT_WRITE	= 6,
+	RTL8127A_PTP_INC_STEP		= 8,
+	RTL8127A_PTP_DEC_STEP		= 10,
+	RTL8127A_PTP_RATE_WRITE		= 14,
+};
+
+struct rtl8127a_ptp {
+	struct phy_device *phydev;
+	struct ptp_clock_info info;
+	struct ptp_clock *clock;
+	struct mutex lock;	/* serializes PTP register access sequences */
+	bool time_set;
+};
+
+static int rtl8127a_ptp_read(struct rtl8127a_ptp *ptp, u16 reg)
+{
+	return phy_read_mmd(ptp->phydev, MDIO_MMD_VEND2, reg);
+}
+
+static int rtl8127a_ptp_write(struct rtl8127a_ptp *ptp, u16 reg, u16 val)
+{
+	return phy_write_mmd(ptp->phydev, MDIO_MMD_VEND2, reg, val);
+}
+
+/* Unlike phy_modify_mmd() always write the register back, some of the
+ * bits are commands to the PTP block.
+ */
+static int rtl8127a_ptp_modify(struct rtl8127a_ptp *ptp, u16 reg, u16 mask,
+			       u16 set)
+{
+	int val = rtl8127a_ptp_read(ptp, reg);
+
+	if (val < 0)
+		return val;
+
+	return rtl8127a_ptp_write(ptp, reg, (val & ~mask) | set);
+}
+
+/* Poll until (reg & mask) is non-zero (set == true) or zero (set == false).
+ * Returns the last register value read, or a negative error code.
+ */
+static int rtl8127a_ptp_poll(struct rtl8127a_ptp *ptp, u16 reg, u16 mask,
+			     bool set)
+{
+	int ret, val;
+
+	ret = read_poll_timeout(rtl8127a_ptp_read, val,
+				val < 0 || !!(val & mask) == set, 1, 1000,
+				false, ptp, reg);
+	if (val < 0)
+		ret = val;
+	if (ret) {
+		if (net_ratelimit())
+			phydev_err(ptp->phydev,
+				   "PTP reg 0x%04x poll failed: %d\n", reg, ret);
+		return ret;
+	}
+
+	return val;
+}
+
+static int rtl8127a_ptp_clkadj_sts(struct rtl8127a_ptp *ptp,
+				   enum rtl8127a_ptp_clkadj_mode mode,
+				   struct ptp_system_timestamp *sts)
+{
+	int val, ret;
+
+	val = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CLK_CFG);
+	if (val < 0)
+		return val;
+
+	val &= ~RTL8127A_PTP_CLK_CFG_MODE;
+	val |= RTL8127A_PTP_CLK_CFG_EXEC | mode;
+
+	/* Only the exec write latches the counter. */
+	ptp_read_system_prets(sts);
+	ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CLK_CFG, val);
+	ptp_read_system_postts(sts);
+	if (ret)
+		return ret;
+
+	ret = rtl8127a_ptp_poll(ptp, RTL8127A_PTP_CLK_CFG,
+				RTL8127A_PTP_CLK_CFG_EXEC, false);
+
+	return ret < 0 ? ret : 0;
+}
+
+static int rtl8127a_ptp_clkadj(struct rtl8127a_ptp *ptp,
+			       enum rtl8127a_ptp_clkadj_mode mode)
+{
+	return rtl8127a_ptp_clkadj_sts(ptp, mode, NULL);
+}
+
+static int rtl8127a_ptp_read_time(struct rtl8127a_ptp *ptp, u16 base,
+				  struct timespec64 *ts)
+{
+	static const u16 regs[] = {
+		RTL8127A_PTP_NS_HI, RTL8127A_PTP_NS_LO,
+		RTL8127A_PTP_S_HI, RTL8127A_PTP_S_MI, RTL8127A_PTP_S_LO,
+	};
+	int val[ARRAY_SIZE(regs)];
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(regs); i++) {
+		val[i] = rtl8127a_ptp_read(ptp, base + regs[i]);
+		if (val[i] < 0)
+			return val[i];
+	}
+
+	ts->tv_nsec = (val[0] & RTL8127A_PTP_NS_HI_MASK) << 16 | val[1];
+	ts->tv_sec = (u64)val[2] << 32 | (u64)val[3] << 16 | val[4];
+
+	return 0;
+}
+
+static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
+				   const struct timespec64 *ts)
+{
+	u64 sec = ts->tv_sec & RTL8127A_PTP_SEC_MASK;
+	u32 nsec = ts->tv_nsec & RTL8127A_PTP_NSEC_MASK;
+	const struct {
+		u16 reg;
+		u16 val;
+	} regs[] = {
+		{ RTL8127A_PTP_NS_LO, nsec },
+		{ RTL8127A_PTP_NS_HI, nsec >> 16 },
+		{ RTL8127A_PTP_S_LO, sec },
+		{ RTL8127A_PTP_S_MI, sec >> 16 },
+		{ RTL8127A_PTP_S_HI, sec >> 32 },
+	};
+	int i, ret;
+
+	for (i = 0; i < ARRAY_SIZE(regs); i++) {
+		ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CFG_TIME + regs[i].reg,
+					 regs[i].val);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
+{
+	int ret;
+
+	mutex_lock(&ptp->lock);
+
+	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL,
+				  RTL8127A_PTP_SYNCE_EN, 0);
+	if (ret)
+		goto out;
+
+	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;
+
+		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_GEN_CFG, 0,
+					  RTL8127A_PTP_INT_CLR_MODE);
+		if (ret)
+			goto out;
+
+		ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, 0xffff);
+		if (ret)
+			goto out;
+
+		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)
+			goto out;
+
+		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CTL,
+					  RTL8127A_PTP_CTL_EN, 0);
+	}
+	if (ret)
+		goto out;
+
+	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
+				  RTL8127A_PHY_RST);
+out:
+	mutex_unlock(&ptp->lock);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_settime(struct rtl8127a_ptp *ptp,
+				const struct timespec64 *ts)
+{
+	int ret;
+
+	mutex_lock(&ptp->lock);
+	ret = rtl8127a_ptp_write_time(ptp, ts);
+	if (!ret)
+		ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_DIRECT_WRITE);
+	mutex_unlock(&ptp->lock);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
+{
+	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+	s32 ppb = scaled_ppm_to_ppb(scaled_ppm);
+	u32 rate;
+	int ret;
+
+	rate = div_u64((u64)abs(ppb) << 32, NSEC_PER_SEC);
+	if (ppb < 0)
+		rate = -rate;
+
+	mutex_lock(&ptp->lock);
+	ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CFG_TIME + RTL8127A_PTP_NS_LO,
+				 rate);
+	if (ret)
+		goto out;
+
+	ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CFG_TIME + RTL8127A_PTP_NS_HI,
+				 FIELD_GET(RTL8127A_PTP_RATE_HI_MASK, rate));
+	if (ret)
+		goto out;
+
+	ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_RATE_WRITE);
+out:
+	mutex_unlock(&ptp->lock);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
+{
+	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+	struct timespec64 d = ns_to_timespec64(abs(delta));
+	int ret;
+
+	mutex_lock(&ptp->lock);
+	ret = rtl8127a_ptp_write_time(ptp, &d);
+	if (!ret)
+		ret = rtl8127a_ptp_clkadj(ptp, delta < 0 ?
+						RTL8127A_PTP_DEC_STEP :
+						RTL8127A_PTP_INC_STEP);
+	mutex_unlock(&ptp->lock);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_gettimex64(struct ptp_clock_info *info,
+				   struct timespec64 *ts,
+				   struct ptp_system_timestamp *sts)
+{
+	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+	int ret;
+
+	mutex_lock(&ptp->lock);
+	ret = rtl8127a_ptp_clkadj_sts(ptp, RTL8127A_PTP_DIRECT_READ, sts);
+	if (!ret)
+		ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_CFG_TIME, ts);
+	mutex_unlock(&ptp->lock);
+
+	return ret;
+}
+
+static int rtl8127a_ptp_settime64(struct ptp_clock_info *info,
+				  const struct timespec64 *ts)
+{
+	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+
+	return rtl8127a_ptp_settime(ptp, ts);
+}
+
+static const struct ptp_clock_info rtl8127a_ptp_info = {
+	.owner		= THIS_MODULE,
+	.name		= "RTL8127A PHC",
+	.max_adj	= RTL8127A_PTP_MAX_ADJ,
+	.adjfine	= rtl8127a_ptp_adjfine,
+	.adjtime	= rtl8127a_ptp_adjtime,
+	.gettimex64	= rtl8127a_ptp_gettimex64,
+	.settime64	= rtl8127a_ptp_settime64,
+};
+
+int rtl8127a_ptp_probe(struct phy_device *phydev)
+{
+	struct device *dev = &phydev->mdio.dev;
+	struct rtl8127a_ptp *ptp;
+	int ret;
+
+	ptp = devm_kzalloc(dev, sizeof(*ptp), GFP_KERNEL);
+	if (!ptp)
+		return -ENOMEM;
+
+	ret = devm_mutex_init(dev, &ptp->lock);
+	if (ret)
+		return ret;
+
+	ptp->phydev = phydev;
+	ptp->info = rtl8127a_ptp_info;
+
+	ret = rtl8127a_ptp_hwtstamp_enable(ptp, false);
+	if (ret)
+		return ret;
+
+	ret = rtl8127a_ptp_adjfine(&ptp->info, 0);
+	if (ret)
+		return ret;
+
+	ptp->clock = ptp_clock_register(&ptp->info, dev);
+	if (IS_ERR(ptp->clock))
+		return PTR_ERR(ptp->clock);
+
+	phydev->priv = ptp;
+
+	return 0;
+}
+
+void rtl8127a_ptp_remove(struct phy_device *phydev)
+{
+	struct rtl8127a_ptp *ptp = phydev->priv;
+
+	if (!ptp)
+		return;
+
+	rtl8127a_ptp_hwtstamp_enable(ptp, false);
+	ptp_clock_unregister(ptp->clock);
+	phydev->priv = NULL;
+}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping for RTL8127a
  2026-12-01  9:44 [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a javen
                   ` (2 preceding siblings ...)
  2026-12-01  9:44 ` [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen
@ 2026-12-01  9:44 ` javen
  2026-12-01 10:36   ` Nicolai Buchwitz
  2026-12-05 10:32   ` netdev-bot+sashiko
  3 siblings, 2 replies; 24+ messages in thread
From: javen @ 2026-12-01  9:44 UTC (permalink / raw)
  To: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier
  Cc: netdev, linux-kernel, daniel, vladimir.oltean, Javen Xu

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 trun the hardware off if enabling it fails
 - Restore timestamping only link_change_notify()
---
 drivers/net/phy/realtek/Kconfig        |  10 +-
 drivers/net/phy/realtek/realtek.h      |   5 +
 drivers/net/phy/realtek/realtek_main.c |   1 +
 drivers/net/phy/realtek/realtek_ptp.c  | 453 ++++++++++++++++++++++++-
 4 files changed, 452 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.h b/drivers/net/phy/realtek/realtek.h
index d1dc79c396e5..47f84d911bc1 100644
--- a/drivers/net/phy/realtek/realtek.h
+++ b/drivers/net/phy/realtek/realtek.h
@@ -10,6 +10,7 @@ int rtl822x_hwmon_init(struct phy_device *phydev);
 #if IS_ENABLED(CONFIG_REALTEK_PHY_PTP)
 int rtl8127a_ptp_probe(struct phy_device *phydev);
 void rtl8127a_ptp_remove(struct phy_device *phydev);
+void rtl8127a_ptp_link_change_notify(struct phy_device *phydev);
 #else
 static inline int rtl8127a_ptp_probe(struct phy_device *phydev)
 {
@@ -19,6 +20,10 @@ static inline int rtl8127a_ptp_probe(struct phy_device *phydev)
 static inline void rtl8127a_ptp_remove(struct phy_device *phydev)
 {
 }
+
+static inline void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
+{
+}
 #endif
 
 #endif /* REALTEK_H */
diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index 483eaf6fb6b1..704401117c3e 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -3444,6 +3444,7 @@ static struct phy_driver realtek_drvs[] = {
 		.write_page	= rtl821x_write_page,
 		.read_mmd	= rtl822x_read_mmd,
 		.write_mmd	= rtl822x_write_mmd,
+		.link_change_notify = rtl8127a_ptp_link_change_notify,
 	}, {
 		PHY_ID_MATCH_EXACT(0x001ccad0),
 		.name		= "RTL8224 2.5Gbps PHY",
diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
index 72d0663a90c2..eff057e14c10 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,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",
@@ -331,8 +542,207 @@ 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,
 };
 
+void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
+{
+	struct rtl8127a_ptp *ptp = phydev->priv;
+	int ret = 0;
+
+	if (!ptp || !phydev->link)
+		return;
+
+	mutex_lock(&ptp->lock);
+	/* 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);
+	}
+	mutex_unlock(&ptp->lock);
+
+	if (ret < 0)
+		phydev_warn(phydev, "failed to restore timestamping: %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.
+			 */
+			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_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;
@@ -349,6 +759,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)
@@ -362,7 +775,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;
 }
@@ -374,7 +794,14 @@ void rtl8127a_ptp_remove(struct phy_device *phydev)
 	if (!ptp)
 		return;
 
+	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


^ permalink raw reply related	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider
  2026-12-01  9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
@ 2026-12-01 10:06   ` Nicolai Buchwitz
  2026-12-01 10:34   ` Maxime Chevallier
  2026-12-05 10:32   ` netdev-bot+sashiko
  2 siblings, 0 replies; 24+ messages in thread
From: Nicolai Buchwitz @ 2026-12-01 10:06 UTC (permalink / raw)
  To: javen
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

Hi Javen

On 1.12.2026 10:44, javen wrote:
> From: Javen <javen_xu@realsil.com.cn>
> 
> A hwtstamp provider can obly ne selected with ETHTOOL_MSG_TSCONFIG_SET

nit: obly ne -> only be

> when the device already has an active time stamping source: a MAC
> implementing ndo_hwtstamp_set(), a PHY flagged as default_timestamp, or
> a previously selected dev->hwprov.
> 
> This patch makes it possible to select a PHY time stamper on a MAC
> without time stamping support. New phy drivers are expected to reply on

nit: reply on -> rely on

> dev->hwprov rather than default_timestamp.
> 
> Signed-off-by: Javen <javen_xu@realsil.com.cn>

Please use your full name in the trailer and From (as in the other 
patches).

> [...]

With the above fixed:

Reviewed-by: Nicolai Buchwitz <nb@tipi-net.de>

Thanks,
Nicolai

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware
  2026-12-01  9:44 ` [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware javen
@ 2026-12-01 10:18   ` Nicolai Buchwitz
  2026-12-02 13:17   ` Andrew Lunn
  2026-12-05 10:32   ` netdev-bot+sashiko
  2 siblings, 0 replies; 24+ messages in thread
From: Nicolai Buchwitz @ 2026-12-01 10:18 UTC (permalink / raw)
  To: javen
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

Hi Javen

On 1.12.2026 10:44, javen wrote:

> [...]

> diff --git a/drivers/net/ethernet/realtek/r8169_main.c 
> b/drivers/net/ethernet/realtek/r8169_main.c
> index f151ab681393..b289ff6c3064 100644
> --- a/drivers/net/ethernet/realtek/r8169_main.c
> +++ b/drivers/net/ethernet/realtek/r8169_main.c
> @@ -2686,9 +2686,14 @@ void r8169_apply_firmware(struct rtl8169_private 
> *tp)
> 
>  	/* TODO: release firmware if rtl_fw_write_firmware signals failure. 
> */
>  	if (tp->rtl_fw) {
> +		/* Keep phylib users (hwmon, PTP) off the bus meanwhile. */

nit: PTP doesn't exist yet at this point, maybe just drop it.

> [...]

Reviewed-by: Nicolai Buchwitz <nb@tipi-net.de>

Thanks,
Nicolai

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a
  2026-12-01  9:44 ` [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen
@ 2026-12-01 10:23   ` Nicolai Buchwitz
  2026-12-02  8:55     ` Javen
  2026-12-02 13:21   ` Andrew Lunn
  2026-12-05 10:32   ` netdev-bot+sashiko
  2 siblings, 1 reply; 24+ messages in thread
From: Nicolai Buchwitz @ 2026-12-01 10:23 UTC (permalink / raw)
  To: javen
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

Hi Javen

On 1.12.2026 10:44, javen wrote:
> From: Javen Xu <javen_xu@realsil.com.cn>
> 
> The internal PHY of RTL8127A (RTL8261C, PHY ID 0x001cc890) contains a
> PTP block with a free running clock that can be read, set, stepped and
> frequency adjusted through vendor MMD registers.
> 
> Register a PTP hardware clock for it. All register accesses go through
> the normal blocking MDIO path, multi register sequences are serialized
> by a driver mutex. The frequency adjustment is cleared at probe. The
> clock is set from the system time when the link comes up for the first
> time after a PHY init or link loss.

The code doesn't do this anymore, and time_set is unused. Please drop
both.

> 
> Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
> ---
> Changes in v3:
>  - Keep the clock running across PHY re-initializations, only start it
>    from the system time if the counter has been reset
>  - Only bracket the latching EXEC write with the system timestamps in
>    gettimex64()
>  - Clear phydev->priv in remove()
>  - Add defines for the PHY reset register
>  - Depend on NETWORK_PHY_TIMESTAMPING from this patch on
> 
> Changes in v4:
>  - Set the clock from the system time on link up, after a PHY init or
>    link loss
>  - Don't fail the PHY probe if PTP setup fails
> 
> Changes in v5:
>  - Don't set the clock from the system time on link up, leave the time
>    to userspace.
> ---

> [...]

> +static int rtl8127a_ptp_poll(struct rtl8127a_ptp *ptp, u16 reg, u16 
> mask,
> +			     bool set)
> +{
> +	int ret, val;
> +
> +	ret = read_poll_timeout(rtl8127a_ptp_read, val,
> +				val < 0 || !!(val & mask) == set, 1, 1000,
> +				false, ptp, reg);

Could use phy_read_mmd_poll_timeout() ?

> [...]

> +static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool 
> enable)
> +{
> +	int ret;
> +
> +	mutex_lock(&ptp->lock);
> +
> +	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL,
> +				  RTL8127A_PTP_SYNCE_EN, 0);
> +	if (ret)
> +		goto out;
> +
> +	if (enable) {

Unused until 4/4, maybe move it there ?

> [...]

> +static int rtl8127a_ptp_adjfine(struct ptp_clock_info *info, long 
> scaled_ppm)
> +{
> +	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, 
> info);
> +	s32 ppb = scaled_ppm_to_ppb(scaled_ppm);

diff_by_scaled_ppm() would keep the full resolution.

> [...]

> +static int rtl8127a_ptp_gettimex64(struct ptp_clock_info *info,
> +				   struct timespec64 *ts,
> +				   struct ptp_system_timestamp *sts)
> +{

What happens when r8169 is runtime suspended? Nothing on this path
resumes it.

> [...]

Thanks,
Nicolai

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider
  2026-12-01  9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
  2026-12-01 10:06   ` Nicolai Buchwitz
@ 2026-12-01 10:34   ` Maxime Chevallier
  2026-12-01 10:44     ` Nicolai Buchwitz
  2026-12-05 10:32   ` netdev-bot+sashiko
  2 siblings, 1 reply; 24+ messages in thread
From: Maxime Chevallier @ 2026-12-01 10:34 UTC (permalink / raw)
  To: javen, andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni,
	nb
  Cc: netdev, linux-kernel, daniel, vladimir.oltean

Hi,

On 12/1/26 10:44, javen wrote:
> From: Javen <javen_xu@realsil.com.cn>
> 
> A hwtstamp provider can obly ne selected with ETHTOOL_MSG_TSCONFIG_SET
> when the device already has an active time stamping source: a MAC
> implementing ndo_hwtstamp_set(), a PHY flagged as default_timestamp, or
> a previously selected dev->hwprov.
> 
> This patch makes it possible to select a PHY time stamper on a MAC
> without time stamping support. New phy drivers are expected to reply on
> dev->hwprov rather than default_timestamp.
> 
> Signed-off-by: Javen <javen_xu@realsil.com.cn>
> ---
> Changes in v5:
>  - new file.
> ---
>  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) ||

Isn't "phy_has_hwtstsamp() enough here ? We just need to check if there's some
timestamper available, the PHY's "default_timestamp" field shouldn't
matter, no ?

I may be missing something though,

Maxime


^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping for RTL8127a
  2026-12-01  9:44 ` [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping " javen
@ 2026-12-01 10:36   ` Nicolai Buchwitz
  2026-12-02  8:30     ` Javen
  2026-12-05 10:32   ` netdev-bot+sashiko
  1 sibling, 1 reply; 24+ messages in thread
From: Nicolai Buchwitz @ 2026-12-01 10:36 UTC (permalink / raw)
  To: javen
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

Hi Javen

On 1.12.2026 10:44, javen wrote:
> 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>
> ---

> [...]

> +void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
> +{
> +	struct rtl8127a_ptp *ptp = phydev->priv;
> +	int ret = 0;
> +
> +	if (!ptp || !phydev->link)
> +		return;
> +
> +	mutex_lock(&ptp->lock);
> +	/* 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);

phy_detach() clears dev->hwprov on down/up, so the PHY is no longer the
selected after that. Should this still turn timestamping back on?

> [...]

> +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 |

Please drop TX_SOFTWARE, that's for the MAC to report.

> [...]

Thanks,
Nicolai

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider
  2026-12-01 10:34   ` Maxime Chevallier
@ 2026-12-01 10:44     ` Nicolai Buchwitz
  0 siblings, 0 replies; 24+ messages in thread
From: Nicolai Buchwitz @ 2026-12-01 10:44 UTC (permalink / raw)
  To: Maxime Chevallier
  Cc: javen, andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni,
	netdev, linux-kernel, daniel, vladimir.oltean

Hi Maxime

On 1.12.2026 11:34, Maxime Chevallier wrote:

> [...]

>> -	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) ||
> 
> Isn't "phy_has_hwtstsamp() enough here ? We just need to check if 
> there's some
> timestamper available, the PHY's "default_timestamp" field shouldn't
> matter, no ?

has_hwts also decides below whether the current source gets disabled
first. With phy_has_hwtstamp() that disable would go to the MAC path
and fail, as r8169 has no ndo_hwtstamp_set?

It means "something is active", so maybe hwts_active would be a better 
name.

> [...]

Thanks,
Nicolai

^ permalink raw reply	[flat|nested] 24+ messages in thread

* RE: [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping for RTL8127a
  2026-12-01 10:36   ` Nicolai Buchwitz
@ 2026-12-02  8:30     ` Javen
  0 siblings, 0 replies; 24+ messages in thread
From: Javen @ 2026-12-02  8:30 UTC (permalink / raw)
  To: Nicolai Buchwitz
  Cc: andrew@lunn.ch, hkallweit1@gmail.com, linux@armlinux.org.uk,
	davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, maxime.chevallier@bootlin.com,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	daniel@makrotopia.org, vladimir.oltean@nxp.com

>
>Hi Javen
>
>On 1.12.2026 10:44, javen wrote:
>> 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>
>> ---
>
>> [...]
>
>> +void rtl8127a_ptp_link_change_notify(struct phy_device *phydev) {
>> +     struct rtl8127a_ptp *ptp = phydev->priv;
>> +     int ret = 0;
>> +
>> +     if (!ptp || !phydev->link)
>> +             return;
>> +
>> +     mutex_lock(&ptp->lock);
>> +     /* 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);
>
>phy_detach() clears dev->hwprov on down/up, so the PHY is no longer the
>selected after that. Should this still turn timestamping back on?
>
>> [...]
>
>> +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 |
>
>Please drop TX_SOFTWARE, that's for the MAC to report.
>
>> [...]
>

I will drop this in v6.

Thanks,
Javen

^ permalink raw reply	[flat|nested] 24+ messages in thread

* RE: [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a
  2026-12-01 10:23   ` Nicolai Buchwitz
@ 2026-12-02  8:55     ` Javen
  2026-12-02 12:20       ` Nicolai Buchwitz
  0 siblings, 1 reply; 24+ messages in thread
From: Javen @ 2026-12-02  8:55 UTC (permalink / raw)
  To: Nicolai Buchwitz
  Cc: andrew@lunn.ch, hkallweit1@gmail.com, linux@armlinux.org.uk,
	davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, maxime.chevallier@bootlin.com,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	daniel@makrotopia.org, vladimir.oltean@nxp.com

Hi Nicolai,

>
>Hi Javen
>
>On 1.12.2026 10:44, javen wrote:
>> From: Javen Xu <javen_xu@realsil.com.cn>
>>
>> The internal PHY of RTL8127A (RTL8261C, PHY ID 0x001cc890) contains a
>> PTP block with a free running clock that can be read, set, stepped and
>> frequency adjusted through vendor MMD registers.
>>
>> Register a PTP hardware clock for it. All register accesses go through
>> the normal blocking MDIO path, multi register sequences are serialized
>> by a driver mutex. The frequency adjustment is cleared at probe. The
>> clock is set from the system time when the link comes up for the first
>> time after a PHY init or link loss.
>
>The code doesn't do this anymore, and time_set is unused. Please drop both.
>
>>
>> Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
>> ---
>> Changes in v3:
>>  - Keep the clock running across PHY re-initializations, only start it
>>    from the system time if the counter has been reset
>>  - Only bracket the latching EXEC write with the system timestamps in
>>    gettimex64()
>>  - Clear phydev->priv in remove()
>>  - Add defines for the PHY reset register
>>  - Depend on NETWORK_PHY_TIMESTAMPING from this patch on
>>
>> Changes in v4:
>>  - Set the clock from the system time on link up, after a PHY init or
>>    link loss
>>  - Don't fail the PHY probe if PTP setup fails
>>
>> Changes in v5:
>>  - Don't set the clock from the system time on link up, leave the time
>>    to userspace.
>> ---
>
>> [...]
>
>> +static int rtl8127a_ptp_poll(struct rtl8127a_ptp *ptp, u16 reg, u16
>> mask,
>> +                          bool set)
>> +{
>> +     int ret, val;
>> +
>> +     ret = read_poll_timeout(rtl8127a_ptp_read, val,
>> +                             val < 0 || !!(val & mask) == set, 1, 1000,
>> +                             false, ptp, reg);
>
>Could use phy_read_mmd_poll_timeout() ?
>
>> [...]
>
>> +static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp,
>> +bool
>> enable)
>> +{
>> +     int ret;
>> +
>> +     mutex_lock(&ptp->lock);
>> +
>> +     ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL,
>> +                               RTL8127A_PTP_SYNCE_EN, 0);
>> +     if (ret)
>> +             goto out;
>> +
>> +     if (enable) {
>
>Unused until 4/4, maybe move it there ?
>
>> [...]
>
>> +static int rtl8127a_ptp_adjfine(struct ptp_clock_info *info, long
>> scaled_ppm)
>> +{
>> +     struct rtl8127a_ptp *ptp = container_of(info, struct
>> +rtl8127a_ptp,
>> info);
>> +     s32 ppb = scaled_ppm_to_ppb(scaled_ppm);
>
>diff_by_scaled_ppm() would keep the full resolution.
>
>> [...]
>
>> +static int rtl8127a_ptp_gettimex64(struct ptp_clock_info *info,
>> +                                struct timespec64 *ts,
>> +                                struct ptp_system_timestamp *sts) {
>
>What happens when r8169 is runtime suspended? Nothing on this path
>resumes it.

I think we could resume the MAC around the PHC callbacks with pm_runtime_resume_and_get(phydev->mdio.bus->parent), or only take a reference with pm_runtime_get_if_active() and return -EBUSY while the MAC is suspended, so the PHC doesn't wake up the NIC (and re-init the PHY) while there is no link.
Which one do you prefer?

Thanks,
Javen

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a
  2026-12-02  8:55     ` Javen
@ 2026-12-02 12:20       ` Nicolai Buchwitz
  0 siblings, 0 replies; 24+ messages in thread
From: Nicolai Buchwitz @ 2026-12-02 12:20 UTC (permalink / raw)
  To: Javen
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

Hi Javen

On 2.12.2026 09:55, Javen wrote:

> [...]

>> What happens when r8169 is runtime suspended? Nothing on this path
>> resumes it.
> 
> I think we could resume the MAC around the PHC callbacks with 
> pm_runtime_resume_and_get(phydev->mdio.bus->parent), or only take a 
> reference with pm_runtime_get_if_active() and return -EBUSY while the 
> MAC is suspended, so the PHC doesn't wake up the NIC (and re-init the 
> PHY) while there is no link.
> Which one do you prefer?

I'd lean towards the second one.

Waking the NIC on every clock read would probably keep it from 
suspending
while phc2sys runs, and without link there isn't much to sync. But I'm 
not
sure, hwmon has the same issue, so maybe Heiner prefers fixing it on the
r8169 side ?

> [...]

Regards
Nicolai

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware
  2026-12-01  9:44 ` [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware javen
  2026-12-01 10:18   ` Nicolai Buchwitz
@ 2026-12-02 13:17   ` Andrew Lunn
  2026-12-03  2:07     ` Javen
  2026-12-05 10:32   ` netdev-bot+sashiko
  2 siblings, 1 reply; 24+ messages in thread
From: Andrew Lunn @ 2026-12-02 13:17 UTC (permalink / raw)
  To: javen
  Cc: hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

On Tue, Dec 01, 2026 at 05:44:52PM +0800, javen wrote:
> From: Javen Xu <javen_xu@realsil.com.cn>
> 
> rtl_fw_write_firmware() accesses PHY registers directly, bypassing
> phylib. Hold MDIO bus lock meanwhile, so other phylib users cannot
> interleave their accesses with the firmware sequence.

More signs you really do need to clean up this mess and use
phylink/phylib correctly.

Why is the MAC driver downloading firmware into the PHY? The PHY
driver should do this. All the locking issues then go away. 

	Andrew

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a
  2026-12-01  9:44 ` [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen
  2026-12-01 10:23   ` Nicolai Buchwitz
@ 2026-12-02 13:21   ` Andrew Lunn
  2026-12-05 10:32   ` netdev-bot+sashiko
  2 siblings, 0 replies; 24+ messages in thread
From: Andrew Lunn @ 2026-12-02 13:21 UTC (permalink / raw)
  To: javen
  Cc: hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

> +static int rtl_internal_nbaset_probe(struct phy_device *phydev)
> +{
> +	int ret;
> +
> +	ret = rtl822x_probe(phydev);
> +	if (ret)
> +		return ret;
> +
> +	/* Only the internal PHY of RTL8127A has a PTP block. PTP is
> +	 * optional, keep the PHY usable without it.
> +	 */

As you point out here, the PTP block is in the PHY.

> +static int rtl8127a_ptp_read(struct rtl8127a_ptp *ptp, u16 reg)
> +{
> +	return phy_read_mmd(ptp->phydev, MDIO_MMD_VEND2, reg);
> +}

And you are using PHY APIs to access it.

All this code needs to move into the PHY driver.

I can accept old code violating layering, but new could should not.

NACK.

       Andrew

^ permalink raw reply	[flat|nested] 24+ messages in thread

* RE: [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware
  2026-12-02 13:17   ` Andrew Lunn
@ 2026-12-03  2:07     ` Javen
  2026-12-03  2:37       ` Andrew Lunn
  0 siblings, 1 reply; 24+ messages in thread
From: Javen @ 2026-12-03  2:07 UTC (permalink / raw)
  To: Andrew Lunn
  Cc: hkallweit1@gmail.com, linux@armlinux.org.uk, davem@davemloft.net,
	edumazet@google.com, kuba@kernel.org, pabeni@redhat.com,
	nb@tipi-net.de, maxime.chevallier@bootlin.com,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	daniel@makrotopia.org, vladimir.oltean@nxp.com

>
>On Tue, Dec 01, 2026 at 05:44:52PM +0800, javen wrote:
>> From: Javen Xu <javen_xu@realsil.com.cn>
>>
>> rtl_fw_write_firmware() accesses PHY registers directly, bypassing
>> phylib. Hold MDIO bus lock meanwhile, so other phylib users cannot
>> interleave their accesses with the firmware sequence.
>
>More signs you really do need to clean up this mess and use phylink/phylib
>correctly.
>
>Why is the MAC driver downloading firmware into the PHY? The PHY driver
>should do this. All the locking issues then go away.
>

I agree. But most of Realtek nics are single chip, mac and phy are highly integrated. Some of the chips share the same PHY ID, but they have different mac version which should be applied different firmware. In this case, I think firmware here can not be moved into PHY driver for the bad design.
Just as we discusses in r8169 phylink patch.
Link: https://lore.kernel.org/netdev/7473edee9a67407c81b357492e9452c3@realsil.com.cn/

Thanks,
Javen

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware
  2026-12-03  2:07     ` Javen
@ 2026-12-03  2:37       ` Andrew Lunn
  2026-12-03  3:04         ` Javen
  0 siblings, 1 reply; 24+ messages in thread
From: Andrew Lunn @ 2026-12-03  2:37 UTC (permalink / raw)
  To: Javen
  Cc: hkallweit1@gmail.com, linux@armlinux.org.uk, davem@davemloft.net,
	edumazet@google.com, kuba@kernel.org, pabeni@redhat.com,
	nb@tipi-net.de, maxime.chevallier@bootlin.com,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	daniel@makrotopia.org, vladimir.oltean@nxp.com

> I agree. But most of Realtek nics are single chip, mac and phy are
> highly integrated. Some of the chips share the same PHY ID, but they
> have different mac version which should be applied different
> firmware. In this case, I think firmware here can not be moved into
> PHY driver for the bad design.

We have dealt with bad vendor design before, where ID 2 and 3 are not
sufficient, or not even filled in.

Look at the Marvell mv88e6xxx switch driver. Some generations of its
integrated PHYs have the OUI part, but the model is 0. The data sheet
even says this was intentional!?!?. So the MDIO driver, which knows
the switch ID, traps reads to ID2, and if it finds the ID value is not
reasonable, it replaces it with the ID of the switch. The Marvell PHY
driver then knows what sort of PHY it is dealing with.

Please don't use bad design as an excuse for layering violations.

If there are other design issues, just describe them, and we can
figure out a workaround.

But step by step you need to move all PHY code out of the MAC driver
and into the PHY driver. And i will not accept any new code which
touches PHY registers from the MAC driver.

	Andrew

^ permalink raw reply	[flat|nested] 24+ messages in thread

* RE: [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware
  2026-12-03  2:37       ` Andrew Lunn
@ 2026-12-03  3:04         ` Javen
  2026-12-04 19:23           ` Andrew Lunn
  0 siblings, 1 reply; 24+ messages in thread
From: Javen @ 2026-12-03  3:04 UTC (permalink / raw)
  To: Andrew Lunn
  Cc: hkallweit1@gmail.com, linux@armlinux.org.uk, davem@davemloft.net,
	edumazet@google.com, kuba@kernel.org, pabeni@redhat.com,
	nb@tipi-net.de, maxime.chevallier@bootlin.com,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	daniel@makrotopia.org, vladimir.oltean@nxp.com

>
>> I agree. But most of Realtek nics are single chip, mac and phy are
>> highly integrated. Some of the chips share the same PHY ID, but they
>> have different mac version which should be applied different firmware.
>> In this case, I think firmware here can not be moved into PHY driver
>> for the bad design.
>
>We have dealt with bad vendor design before, where ID 2 and 3 are not
>sufficient, or not even filled in.
>
>Look at the Marvell mv88e6xxx switch driver. Some generations of its
>integrated PHYs have the OUI part, but the model is 0. The data sheet even
>says this was intentional!?!?. So the MDIO driver, which knows the switch ID,
>traps reads to ID2, and if it finds the ID value is not reasonable, it replaces it
>with the ID of the switch. The Marvell PHY driver then knows what sort of PHY
>it is dealing with.
>
>Please don't use bad design as an excuse for layering violations.
>
>If there are other design issues, just describe them, and we can figure out a
>workaround.
>
>But step by step you need to move all PHY code out of the MAC driver and into
>the PHY driver. And i will not accept any new code which touches PHY registers
>from the MAC driver.
>

Understand.

But for single chip, the firmware contains mixed configurations for both MAC and PHY. It has strict timing requirements. During execution, it uses PHY_MDIO_CHG to dynamically switch the access target. The sequence is interleaved and cannot be split to into two separated phases.

How do you suggest we handle this issue? Any suggestion will be helpful.

Thanks,
Javen

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware
  2026-12-03  3:04         ` Javen
@ 2026-12-04 19:23           ` Andrew Lunn
  0 siblings, 0 replies; 24+ messages in thread
From: Andrew Lunn @ 2026-12-04 19:23 UTC (permalink / raw)
  To: Javen
  Cc: hkallweit1@gmail.com, linux@armlinux.org.uk, davem@davemloft.net,
	edumazet@google.com, kuba@kernel.org, pabeni@redhat.com,
	nb@tipi-net.de, maxime.chevallier@bootlin.com,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	daniel@makrotopia.org, vladimir.oltean@nxp.com

> But for single chip, the firmware contains mixed configurations for
> both MAC and PHY. It has strict timing requirements. During
> execution, it uses PHY_MDIO_CHG to dynamically switch the access
> target. The sequence is interleaved and cannot be split to into two
> separated phases.

I can understand the sequence is interleaved in the firmware file. But
what it not clear to me is why you cannot do the split at runtime? The
PHY ignores all requests to the MAC. The MAC ignores all requests to
the PHY.

What goes wrong when you do split it?

     Andrew

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider
  2026-12-01  9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
  2026-12-01 10:06   ` Nicolai Buchwitz
  2026-12-01 10:34   ` Maxime Chevallier
@ 2026-12-05 10:32   ` netdev-bot+sashiko
  2 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 10:32 UTC (permalink / raw)
  To: javen_xu
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message for "net: ethtool: tsconfig: allow selecting a
  provider" is inaccurate and leaves out user-visible effects.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but this patch
  makes it reachable on more devices: dev->hwprov->phydev can be left…

--

net: ethtool: tsconfig: allow selecting a provider

This patch relaxes the early check in ethnl_set_tsconfig(). A request that
carries ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER is now accepted even when the
device has no ndo_hwtstamp_set(), no default PHY time stamper and no
previously selected dev->hwprov. In that case the call that disables the
current time stamping config is skipped before the new provider is
installed.

> A hwtstamp provider can obly ne selected with ETHTOOL_MSG_TSCONFIG_SET
> when the device already has an active time stamping source: a MAC
> implementing ndo_hwtstamp_set(), a PHY flagged as default_timestamp, or
> a previously selected dev->hwprov.
>
> This patch makes it possible to select a PHY time stamper on a MAC
> without time stamping support. New phy drivers are expected to reply on
> dev->hwprov rather than default_timestamp.

[ ... ]

> Changes in v5:
>  - new file.

[Severity: Low]
This isn't a bug, but there are some typos and inaccuracies here:

  "obly ne selected" should be "only be selected".
  "expected to reply on" should be "expected to rely on".
  "new file" is wrong, since the patch doesn't add a file.

The subject "allow selecting a provider" sounds as if provider selection
wasn't possible before. What is actually new is that a provider can be
selected on a device with no hw timestamping source at all. Could the
subject say that?

The message sets a policy that PHY drivers should rely on dev->hwprov
rather than default_timestamp. Should it also say what this means for the
legacy interfaces?

Take a MAC without ndo_hwtstamp_set, such as r8169, with no hwprov selected
and default_timestamp false. In that case dev_set_hwtstamp_phylib() ends up
with phy_ts == false:

net/core/dev_ioctl.c:dev_set_hwtstamp_phylib() {
    ...
	if (!phy_ts || dev->see_all_hwtstamp_requests) {
		if (!ops->ndo_hwtstamp_set)
			return -EOPNOTSUPP;
    ...
}

On the same device:

  dev_get_hwtstamp_phylib() also returns -EOPNOTSUPP.
  __ethtool_get_ts_info() reports only the MAC's get_ts_info.

So users of SIOCSHWTSTAMP, SIOCGHWTSTAMP and ETHTOOL_GET_TS_INFO, such as
ptp4l, can't find or use the PHY time stamper until something selects it
with ETHTOOL_MSG_TSCONFIG_SET. The RTL8127a PHY driver later in the series
doesn't set default_timestamp, so it is affected. The kernel-doc of
phy_is_default_hwtstamp() may also be a good place to document this
policy.

The errno that userspace sees also changes. Before this patch, a
TSCONFIG_SET with a provider on a device without hw timestamping failed
right away with -EOPNOTSUPP. It now goes through
tsconfig_set_hwprov_from_desc():

net/ethtool/tsconfig.c:tsconfig_set_hwprov_from_desc() {
    ...
	ret = ethtool_net_get_ts_info_by_phc(dev, &ts_info, hwprov_desc);
	if (!ret && dev->netdev_ops->ndo_hwtstamp_set) {
		/* Found */
		source = HWTSTAMP_SOURCE_NETDEV;
	} else {
		phy = ethtool_phy_get_ts_info_by_phc(dev, &ts_info, hwprov_desc);
		if (IS_ERR(phy)) {
			if (PTR_ERR(phy) == -ENODEV)
				NL_SET_ERR_MSG_ATTR(info->extack,
						    tb[ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER],
						    "phc not in this net device topology");
    ...
}

This path can return -ENODEV with "phc not in this net device topology" in
three cases:

  The PHC is not a topology PHY.
  The qualifier is not PRECISE.
  The PHC is the MAC's own, as reported by get_ts_info, but the MAC lacks
  ndo_hwtstamp_set.

In the last case the message is misleading. Should the commit message
mention the errno change? Is this extack text right for the MAC PHC case?

> diff --git a/net/ethtool/tsconfig.c b/net/ethtool/tsconfig.c
> index 2db0e7ba8b9f5..9c983666a1b02 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,

[ ... ]

> -	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;

[Severity: High]
This isn't a bug introduced by this patch, but the patch makes it reachable
on more devices. PHY-only provider selection now works on MACs without hw
timestamping. Can dev->hwprov->phydev then be left pointing at a freed SFP
module PHY on those devices too?

tsconfig_set_hwprov_from_desc() stores the phy_device pointer without
taking a reference:

	hwprov->phydev = phy;

That phy comes from ethtool_phy_get_ts_info_by_phc(). It walks every PHY in
dev->link_topo, including a module PHY behind a PHY-driven SFP cage.

The only teardown code that clears a PHY-backed dev->hwprov is in
phy_detach():

drivers/net/phy/phy_device.c:phy_detach() {
    ...
		if (hwprov && hwprov->phydev == phydev) {
			rcu_assign_pointer(dev->hwprov, NULL);
			kfree_rcu(hwprov, rcu_head);
		}
    ...
}

A module PHY behind a PHY-driven cage is never attached with
phy_attach_direct(). When the module is removed, this is the path:

sfp_sm_phy_detach()
  sfp_remove_phy()
    phy_sfp_disconnect_phy()
      phy_link_topo_del_phy(dev, phy);
  phy_device_remove(sfp->mod_phy);
  phy_device_free(sfp->mod_phy);

Nothing on that path checks or clears dev->hwprov. Afterwards,
skb_clone_tx_timestamp() and skb_defer_rx_timestamp() would load
hwprov->phydev, read phydev->mii_ts, and call mii_ts->txtstamp() or
mii_ts->rxtstamp() through freed memory. dev_set_hwtstamp_phylib() and
dev_get_hwtstamp_phylib() dereference the same stale pointer.

Before this patch, selecting such a PHY needed a MAC with ndo_hwtstamp_set
or another hw timestamping source. Does anything clear dev->hwprov when the
SFP module PHY is removed from the link topology?

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201094454.1150-1-javen_xu%40realsil.com.cn

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware
  2026-12-01  9:44 ` [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware javen
  2026-12-01 10:18   ` Nicolai Buchwitz
  2026-12-02 13:17   ` Andrew Lunn
@ 2026-12-05 10:32   ` netdev-bot+sashiko
  2 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 10:32 UTC (permalink / raw)
  To: javen_xu
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message reads as preventive hardening, but the patch
  fixes a race that already exists before this series.

--

r8169: hold MDIO bus lock while loading firmware

This patch takes the MDIO bus lock around rtl_fw_write_firmware() in
r8169_apply_firmware(). Other phylib users then cannot interleave their
PHY accesses with the firmware sequence. The lock is skipped when there
is no phydev, and tp->ocp_base is restored before the lock is released.

> rtl_fw_write_firmware() accesses PHY registers directly, bypassing
> phylib. Hold MDIO bus lock meanwhile, so other phylib users cannot
> interleave their accesses with the firmware sequence.

[Severity: Low]
The code change looks right. However, the message reads like
preventive hardening. Isn't this race already reachable before this
series?

Before this patch, r8169_apply_firmware() ran the firmware sequence
without the MDIO bus lock:

r8169_apply_firmware()
  rtl_fw_write_firmware()
    rtl_writephy() / rtl_readphy()
      r8168g_mdio_write() / r8168g_mdio_read()
        r8168_phy_ocp_write() / r8168_phy_ocp_read()

r8168_phy_ocp_read() writes, polls and then reads the shared GPHY_OCP
register. That is not atomic:

    RTL_W32(tp, GPHY_OCP, reg << 15);

    return rtl_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
        (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;

Firmware writes to register 0x1f also change the shared tp->ocp_base.

In the same tree, rtl822x_probe() in drivers/net/phy/realtek/ already
calls rtl822x_hwmon_init() for the internal NBASE-T PHYs (RTL_8221B,
RTL_8251B, RTL_8261C and 0x001cc841). A read of the 0444 temp1_input or
temp1_max file follows this path:

rtl822x_hwmon_read()
  phy_read_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSRR)
    rtl822x_read_mmd()
      r8169_mdio_read_reg_c45()
        r8168_phy_ocp_read()

That path holds only the MDIO bus lock.

Suppose the hwmon file is read while rtl_open(), the reset work or
resume runs this chain:

rtl8169_init_phy()
  r8169_hw_phy_config()
    rtl8125b_hw_phy_config() (or the 8125d/8126a/8127a variants)
      r8169_apply_firmware()

Can the two OCP access sequences interleave? That could corrupt a
firmware write, or a firmware PHY_READ could return the hwmon register
value, and the PHY would be left with a broken firmware patch.

If so, could the commit message say what goes wrong without the lock?
Could it also carry a Fixes: tag for the commit that added hwmon
support for these PHYs, so the patch is routed to net and stable?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201094454.1150-1-javen_xu%40realsil.com.cn

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a
  2026-12-01  9:44 ` [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen
  2026-12-01 10:23   ` Nicolai Buchwitz
  2026-12-02 13:21   ` Andrew Lunn
@ 2026-12-05 10:32   ` netdev-bot+sashiko
  2 siblings, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 10:32 UTC (permalink / raw)
  To: javen_xu
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

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

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

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

- [Medium] The commit message does not match the code.
- [Medium] The multi-register PHC sequences are serialized only by the
  private ptp->lock: rtl8127a_ptp_settime (5 CFG_TIME writes, then…
- [Medium] The PHC is registered at PHY probe (rtl_internal_nbaset_probe
  -> rtl8127a_ptp_probe -> ptp_clock_register).
- [Medium] rtl8127a_ptp_hwtstamp_enable() ends by setting
  RTL8127A_PHY_RST (BIT(15) of RTL8127A_PHY_RST_CTL, VEND2 0xa640) with…
- [Medium] rtl8127a_ptp_probe() calls rtl8127a_ptp_hwtstamp_enable(ptp,
  false), which clears RTL8127A_PTP_CTL_EN (PTP_CTL bit 0) and…
- [Low] On every driver bind and unbind, rtl8127a_ptp_probe() and
  rtl8127a_ptp_remove() call rtl8127a_ptp_hwtstamp_enable(ptp, false).

--

net: phy: realtek: add PTP clock support for RTL8127a

This patch registers a PTP hardware clock for the PTP block in the
internal PHY of RTL8127A (RTL8261C). The clock is read, set, stepped and
frequency adjusted through vendor MMD registers. A driver mutex
serializes the multi register sequences.

> Register a PTP hardware clock for it. All register accesses go through
> the normal blocking MDIO path, multi register sequences are serialized
> by a driver mutex. The frequency adjustment is cleared at probe. The

[Severity: Low]
Should the message also say what else happens at probe and remove?

Both rtl8127a_ptp_probe() and rtl8127a_ptp_remove() call
rtl8127a_ptp_hwtstamp_enable(ptp, false). That helper clears
RTL8127A_PTP_SYNCE_EN, the RX/TX timestamp interrupt enables and
RTL8127A_PTP_CTL_EN. It then always sets RTL8127A_PHY_RST:

	ret = rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
				  RTL8127A_PHY_RST);

So binding or unbinding the PHY driver now asserts what the v3 changelog
calls the PHY reset bit, and it turns off the PTP block enable. The
message doesn't describe either.

> clock is set from the system time when the link comes up for the first
> time after a PHY init or link loss.

[Severity: Medium]
Does this sentence still match the code? Nothing in this patch sets the
PHC from the system time.

The "Realtek Internal NBASE-T PHY" entry in realtek_drvs[] only gains
.probe and .remove, and there is no link_change_notify hook. The only
system time reads in realtek_ptp.c are the gettimex64 pre/post
timestamps.

The v5 changelog says this behaviour was dropped:

  Don't set the clock from the system time on link up, leave the time
  to userspace.

It looks like the commit body wasn't updated to match.

There is also a leftover field in struct rtl8127a_ptp that nothing reads
or writes:

	bool time_set;

The later "net: phy: realtek: add hardware timestamping for RTL8127a"
patch adds a link_change_notify. That hook only restores timestamping,
and it doesn't use time_set either.

Could the sentence and the field be dropped?

> diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
> new file mode 100644
> index 0000000000000..72d0663a90c20
> --- /dev/null
> +++ b/drivers/net/phy/realtek/realtek_ptp.c
> @@ -0,0 +1,380 @@

[ ... ]

> +static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
> +{

[ ... ]

> +	} else {
> +		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER,
> +					  RTL8127A_PTP_INT_RX_TS |
> +					  RTL8127A_PTP_INT_TX_TS, 0);
> +		if (ret)
> +			goto out;
> +
> +		ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CTL,
> +					  RTL8127A_PTP_CTL_EN, 0);
> +	}

[Severity: Medium]
Does the PTP counter keep running while RTL8127A_PTP_CTL_EN is cleared?

rtl8127a_ptp_probe() takes this disable path before calling
ptp_clock_register(), and nothing in this patch sets CTL_EN again. Later
in the series it is only set while hardware timestamping is enabled
through hwtstamp_set.

The enable path writes RTL8127A_PTP_CTL_TYPE_3 (0x003f), which includes
bit 0. That makes CTL_EN look like the master enable of the PTP block.

If CTL_EN gates the counter, the registered PHC would be frozen whenever
timestamping is off. gettimex64 would return a stale value, and
adjtime, adjfine and settime64 would act on a disabled block.

If the counter and the CLK_CFG engine are known to work with CTL_EN
cleared, could a comment say so?

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

[Severity: Medium]
Should there be a wait after setting RTL8127A_PHY_RST? This sets bit 15
of VEND2 0xa640 and returns at once, with no delay and no poll for the
bit to self-clear.

rtl8127a_ptp_probe() goes straight on to the next step:

	ret = rtl8127a_ptp_hwtstamp_enable(ptp, false);
	...
	ret = rtl8127a_ptp_adjfine(&ptp->info, 0);

adjfine writes CFG_TIME, triggers RATE_WRITE through CLK_CFG.EXEC, and
takes EXEC reading back as 0 to mean completion.

If this bit starts a self-clearing reset, can those writes land while the
block is still in reset, and the EXEC poll pass on a reset value of 0?
If the bit is level-sensitive instead, what deasserts it?

Other drivers that set PHY reset bits poll for completion, for example
smsc911x (PMT_CTRL_PHY_RST_), lan78xx (PMT_CTL_PHY_RST_) and igc
(IGC_PHY_RST_COMP). Later in the series this helper is also called from
hwtstamp_set and link_change_notify, still without a wait.

> +static int rtl8127a_ptp_settime(struct rtl8127a_ptp *ptp,
> +				const struct timespec64 *ts)
> +{
> +	int ret;
> +
> +	mutex_lock(&ptp->lock);
> +	ret = rtl8127a_ptp_write_time(ptp, ts);
> +	if (!ret)
> +		ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_DIRECT_WRITE);
> +	mutex_unlock(&ptp->lock);
> +
> +	return ret;
> +}

[Severity: Medium]
Is ptp->lock enough to keep the staging writes and the EXEC write
together? It only serializes against other PTP callbacks. Each
phy_read_mmd()/phy_write_mmd() holds the MDIO bus lock for one access
only.

r8169 re-initializes this same PHY without taking ptp->lock:

rtl8169_up()
  rtl8169_init_phy()
    phy_init_hw()
    r8169_hw_phy_config()
    genphy_soft_reset()

rtl8169_up() runs from ndo_open, from the reset work after a TX timeout
and from resume. PHC ioctls and clock_adjtime() run in process context
without RTNL or phydev->lock.

Suppose that re-init lands between the CFG_TIME writes and the CLK_CFG
EXEC write, and clears the staging registers while the counter keeps
running. Could EXEC then apply the wrong time and still return success?
Or could the EXEC poll time out?

The same question applies to these sequences:

  - the rate LO/HI writes in rtl8127a_ptp_adjfine()
  - rtl8127a_ptp_adjtime()
  - the EXEC-then-read sequence in rtl8127a_ptp_gettimex64()

The PTP block does seem to be affected by PHY re-init. The v3 changelog
mentions the counter being reset across re-initialization. The next
patch in the series also adds link_change_notify code commented "Restore
timestamping in case the PTP block was reset".

[ ... ]

> +static int rtl8127a_ptp_gettimex64(struct ptp_clock_info *info,
> +				   struct timespec64 *ts,
> +				   struct ptp_system_timestamp *sts)
> +{
> +	struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
> +	int ret;
> +
> +	mutex_lock(&ptp->lock);
> +	ret = rtl8127a_ptp_clkadj_sts(ptp, RTL8127A_PTP_DIRECT_READ, sts);
> +	if (!ret)
> +		ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_CFG_TIME, ts);
> +	mutex_unlock(&ptp->lock);
> +
> +	return ret;
> +}

[Severity: Medium]
What happens to the PHC callbacks while the NIC is runtime suspended?
The clock is registered at PHY probe and stays visible through /dev/ptpN
for as long as the driver is bound, including while the interface is
down.

The callbacks go straight to MMIO on the PCI function without taking a
runtime PM reference:

phy_read_mmd()
  r8169_mdio_read_reg()
    rtl_readphy()
      r8168_phy_ocp_read()

r8169 schedules runtime suspend 10 seconds after the interface goes down
or loses carrier:

drivers/net/ethernet/realtek/r8169_main.c:rtl8169_runtime_idle() {
    ...
	if (!netif_running(tp->dev) || !netif_carrier_ok(tp->dev))
		pm_schedule_suspend(device, 10000);
    ...
}

Once the function is in D3hot, CLK_CFG would read back as 0xffff and the
EXEC bit would never clear. Every PHC operation would then fail with
-ETIMEDOUT after logging OCP and poll errors. On some platforms, MMIO to
a D3hot function can also raise AER errors.

r8169 also powers the PHY down with phy_suspend() while the PHC stays
usable. There is no resume hook to restore a rate written by adjfine.

r8169's own LED helpers, such as rtl8168_led_mod_ctrl(), wrap their
register access in pm_runtime_resume_and_get(). Does the PHC path need
the same, or at least a check that the PHY is powered?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201094454.1150-1-javen_xu%40realsil.com.cn

^ permalink raw reply	[flat|nested] 24+ messages in thread

* Re: [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping for RTL8127a
  2026-12-01  9:44 ` [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping " javen
  2026-12-01 10:36   ` Nicolai Buchwitz
@ 2026-12-05 10:32   ` netdev-bot+sashiko
  1 sibling, 0 replies; 24+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 10:32 UTC (permalink / raw)
  To: javen_xu
  Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, nb,
	maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean

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

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

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() (lines 790-807) unpublishes the
  mii_timestamper without synchronization and leaves the core's hwprov…
- [Medium] When the core is in its default configuration, the new
  mii_timestamper never receives any traffic or configuration requests.
- [Medium] rtl8127a_ptp_rx_tstamp() (lines 490-511) latches the per-type
  RX timestamp (SEL_RX|MSGTYPE|EXEC), reads TRX_TS_TIME and writes the…
- [Medium] In the TX path, rtl8127a_ptp_tx_tstamp() and
  rtl8127a_ptp_egress_time() take INSR.INT_TX_TS together with the per-…
- [Medium] INSR has a single INT_TX_TS bit for all four TX event message
  types, while TRX_TS_STA has a separate TX_*_RDY bit per type (bits…
- [Low] In rtl8127a_ptp_hwtstamp_set() (lines 697-708), when
  __rtl8127a_ptp_hwtstamp_enable() fails, the error path sets…
- [Low] The single TX slot is handed from rtl8127a_ptp_txtstamp() to the
  aux worker without a lock, and only the first consumer read pairs with…
- [Low] rtl8127a_ptp_trx_ts_cmd() (lines 237-240) does a read-modify-
  write of RTL8127A_PTP_TRX_TS_STA through rtl8127a_ptp_modify().
- [Low] rtl8127a_ptp_link_change_notify() (lines 548-567) treats a
  cleared CTL_EN on link-up, while hw_enabled is set, as meaning 'the…

--

net: phy: realtek: add hardware timestamping for RTL8127a

This patch registers a mii_timestamper for the RTL8127a internal PHY so
that it can timestamp PTPv2 event messages over UDP. The timestamps are
read over MDIO from the PTP aux worker. RX frames are queued until their
timestamp has been read, and only one TX timestamp can be pending at a
time.

> diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
> index 72d0663a90c20..eff057e14c101 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: Low]
TRX_TS_STA mixes command bits (RD, SEL_RX, MSGTYPE, EXEC) with the
TX_*_RDY status flags in bits 12-15. The mask passed to
rtl8127a_ptp_modify() clears only SEL_RX and MSGTYPE. Won't any RDY bits
that are set when the register is read be written back together with the
latch command?

If those flags are write-1-to-clear, consider an RX latch from
rtl8127a_ptp_rx_tstamp() while a tracked TX timestamp is pending, for
example a Sync received while a Delay_Req is outstanding. The RX latch
could clear the TX RDY bit. The 1 ms RDY poll in
rtl8127a_ptp_egress_time() would then fail and the TX timestamp would be
dropped.

If the flags are read-only, how do they ever get cleared?

Should the status bits be masked out of the write-back, or should their
clearing semantics be handled explicitly?

[ ... ]

> @@ -323,6 +416,124 @@ 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)
> +{
> +	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) {

[Severity: Low]
Only the first lockless check pairs with the smp_store_release() in
rtl8127a_ptp_txtstamp(). This re-check under the mutex is a plain load.
Can it see a different skb than the one acquired above, without ordering
the later loads of tx_start and tx_msgtype?

  aux worker:     smp_load_acquire() sees skb1, blocks on ptp->lock
  hwtstamp_set(): TX_OFF, completes skb1, clears TX_IN_PROGRESS
  hwtstamp_set(): TX_ON
  txtstamp():     on another CPU, arms skb2 via smp_store_release()
  aux worker:     takes ptp->lock, plain load sees skb2

mutex_lock() orders the worker only against the previous lock holder,
not against txtstamp(). On weakly ordered CPUs the reads of tx_start and
tx_msgtype could still return skb1's values. A stale tx_msgtype would
latch the wrong message type, and a stale tx_start could time skb2 out
early.

rtl8127a_ptp_do_aux_work() also reads ptp->tx_start without READ_ONCE()
while txtstamp() may be writing it, which KCSAN would report as a data
race.

Would smp_load_acquire() for this re-check, plus READ_ONCE() and
WRITE_ONCE() for tx_start, be enough?

> +		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;
> +	}

[Severity: Medium]
Could INT_TX_TS here, and the per-type TX_*_RDY bit polled in
rtl8127a_ptp_egress_time(), belong to an earlier frame rather than the
tracked one?

Neither bit, nor the latch, is reset when rtl8127a_ptp_txtstamp() starts
tracking a new skb. txtstamp() also schedules the worker with delay 0
from the xmit path, before the frame has reached the PHY.

The hardware presumably stamps every PTPv2 UDP event frame it transmits,
including frames the driver does not track:

- frames txtstamp() dropped because TX_IN_PROGRESS was already set
- frames sent without SKBTX_HW_TSTAMP
- a tracked frame that timed out or was dropped on TX_OFF and went out
  later (the branch above only acks INT_TX_TS)
- frames that went out after the worker's INSR ack

Each of these could leave INT_TX_TS asserted and a RDY bit set over an
old latch value. The next tracked frame of that type would pass this
check at once and the RDY poll would succeed immediately.
skb_complete_tx_timestamp() would then report the earlier frame's egress
time.

Any unprivileged local process can send a UDP datagram to port 319 with
a PTPv2 event header. Could that inject wrong TX timestamps into a
privileged PTP daemon? The Kconfig note that "a single PTP application
on the link is assumed" doesn't seem to cover this case.

> +
> +	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);

[Severity: Medium]
INSR has a single INT_TX_TS bit for all four event message types, while
TRX_TS_STA has one RDY bit per type. Suppose INT_TX_TS was raised by a
different message type than the tracked one. This code acks it, and
rtl8127a_ptp_egress_time() then polls the tracked type's RDY bit for only
the 1 ms budget of rtl8127a_ptp_poll().

The tracked frame could still be queued in the MAC, for example behind a
busy TX ring or because of pause frames. Won't the poll then return
-ETIMEDOUT, log a "poll failed" error, and drop the skb, even though
RTL8127A_PTP_TX_TIMEOUT has not expired?

The timestamp the hardware produces later would then stay in the latch
and RDY bit as stale state for the next request.

Should "INT_TX_TS set but RDY clear" be treated as not ready yet (return
false) instead of as a final failure?

[ ... ]

> +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]
Does anything here confirm that the hardware actually captured a new
timestamp for this frame? The latched value is written into
skb_hwtstamps() unconditionally.

__rtl8127a_ptp_hwtstamp_enable() enables INT_RX_TS in INER, but nothing
ever reads or acknowledges it in INSR. Once it fires, it stays set. The
TX path at least checks INSR and a per-type RDY bit.

There are a couple of ways a stale value could end up in the latch.

rtl8127a_ptp_rxtstamp() rejects only PTP_CLASS_L2, so IPv6 and
VLAN-tagged UDP PTPv2 frames are queued. It isn't clear that the
CTL_TYPE_3 | BIT(12) configuration stamps those frames.

rxtstamp() also checks rx_enabled without the lock before queueing the
skb. rtl8127a_ptp_hwtstamp_set(FILTER_NONE) can then clear CTL_EN and
pulse PHY_RST. This function latches from the disabled block without
rechecking hw_enabled or rx_enabled under ptp->lock.

In each case the frame would be delivered with an earlier frame's
timestamp, or a value read after the reset, as a valid hardware
timestamp. The commit message only says that a later message of the
same type can overwrite the latch.

Can freshness be checked here, or should the limitation be documented?

[ ... ]

> @@ -331,8 +542,207 @@ static const struct ptp_clock_info rtl8127a_ptp_info = {

[ ... ]

> +void rtl8127a_ptp_link_change_notify(struct phy_device *phydev)
> +{
> +	struct rtl8127a_ptp *ptp = phydev->priv;
> +	int ret = 0;
> +
> +	if (!ptp || !phydev->link)
> +		return;
> +
> +	mutex_lock(&ptp->lock);
> +	/* 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);

[Severity: Low]
If a cleared CTL_EN means the PTP block was reset, could the same reset
also have cleared the clock state?

Only the timestamp enable sequence is rerun here. The rate programmed by
rtl8127a_ptp_adjfine() through RTL8127A_PTP_RATE_WRITE, and the time set
by settime/adjtime, are not restored. The driver also keeps no copy of
the last rate.

A tx_skb pending across the reset is not invalidated either, so it only
finishes at the 15 second timeout.

Would the PHC then run at the nominal rate while the PTP stack assumes
its last adjustment still applies?

[ ... ]

> +	/* 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.
> +			 */
> +			if (enable)
> +				__rtl8127a_ptp_hwtstamp_enable(ptp, false);
> +			ptp->hw_enabled = false;

[Severity: Low]
Can hw_enabled be set to false here while the hardware is still on?

- If enable is false and the disable sequence fails part-way (for
  example after the SYNCE_CTL or INER modify), CTL_EN is never cleared.
- If enable is true and this rollback call fails, its return value is
  ignored.

In both cases the INER RX/TX interrupt enables and CTL_EN could stay
set. The registers are only reprogrammed when enable != ptp->hw_enabled,
so a later TX_OFF/FILTER_NONE request makes no register access at all.

The block would keep timestamping with its interrupts enabled while the
driver reports timestamping as off.

[ ... ]

> @@ -362,7 +775,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;

[Severity: Medium]
phydev->default_timestamp is not set, so phy_is_default_hwtstamp()
returns false. Is this timestamper used at all in the default
configuration?

net/core/dev_ioctl.c:dev_set_hwtstamp_phylib() {
    ...
	phy_ts = phy_is_default_hwtstamp(dev->phydev);
    ...
	if (!phy_ts || dev->see_all_hwtstamp_requests) {
		if (!ops->ndo_hwtstamp_set)
			return -EOPNOTSUPP;
    ...
}

r8169 has no ndo_hwtstamp_set/get. That leads to three problems:

- SIOCSHWTSTAMP from ptp4l would return -EOPNOTSUPP.
- __ethtool_get_ts_info() would report only software timestamping, with
  phc_index -1.
- skb_clone_tx_timestamp() and skb_defer_rx_timestamp() return early,
  so rtl8127a_ptp_txtstamp() and rtl8127a_ptp_rxtstamp() are never
  called.

Timestamping only works once user space selects the PHY as the hwtstamp
provider with ETHTOOL_MSG_TSCONFIG_SET. bcm-phy-ptp, dp83640, micrel,
microchip_rds_ptp, mscc_ptp and nxp-c45-tja11xx all set
default_timestamp = true.

The v5 changelog says default_timestamp was dropped on purpose. However,
the new Kconfig help says the PHY timestamps "on behalf of the MAC, so
no support from the MAC driver is needed", and the commit message makes
a similar claim.

Should default_timestamp be set, or should the need to select the
provider explicitly be documented?

> @@ -374,7 +794,14 @@ void rtl8127a_ptp_remove(struct phy_device *phydev)
>  	if (!ptp)
>  		return;
>  
> +	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;
>  }

[Severity: High]
Does anything wait for rtl8127a_ptp_rxtstamp() or rtl8127a_ptp_txtstamp()
calls that are already in flight before this teardown?

skb_defer_rx_timestamp() and skb_clone_tx_timestamp() load
phydev->mii_ts and call through it. A CPU that loaded the pointer before
the plain store above can still be running the callbacks:

- skb_queue_tail() after skb_queue_purge() leaks the skb.
- smp_store_release(&ptp->tx_skb) after the tx_skb check here leaks the
  clone and its socket reference.
- ptp_schedule_worker(ptp->clock, 0) after ptp_clock_unregister() has
  called kthread_destroy_worker() would use a freed kworker.
- Once remove returns, devres frees struct rtl8127a_ptp, so later
  accesses to ptp->state, tx_type or rx_queue.lock would be
  use-after-free.

v4 had synchronize_net() here, and v5 removed it.

rtl8127a_ptp_tx_complete() is also called here without ptp->lock. Could
a concurrent rtl8127a_ptp_hwtstamp_set() that turns TX off see the same
tx_skb and free it a second time?

Separately, phy_remove() calls drv->remove() without calling
phy_detach(), and only phy_detach() clears dev->hwprov. After a sysfs
unbind of the PHY driver while the netdev is up, hwprov would still
select this phydev. The hwprov branch of skb_defer_rx_timestamp() does
this:

	mii_ts = phydev->mii_ts;
	if (likely(mii_ts->rxtstamp))
		return mii_ts->rxtstamp(mii_ts, skb, type);

There is no NULL check. Wouldn't the next PTP packet received from any
peer then dereference NULL in softirq context?

The missing NULL check in the core predates this patch (dp83640 clears
mii_ts the same way). However, explicit hwprov selection is the only way
to use this driver's timestamping, so the crash is directly reachable
here.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201094454.1150-1-javen_xu%40realsil.com.cn

^ permalink raw reply	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2026-12-05 10:32 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  9:44 [PATCH net-next v5 0/4] net: phy: realtek: add PTP support for RTL8127a javen
2026-12-01  9:44 ` [PATCH net-next v5 1/4] net: ethtool: tsconfig: allow selecting a provider javen
2026-12-01 10:06   ` Nicolai Buchwitz
2026-12-01 10:34   ` Maxime Chevallier
2026-12-01 10:44     ` Nicolai Buchwitz
2026-12-05 10:32   ` netdev-bot+sashiko
2026-12-01  9:44 ` [PATCH net-next v5 2/4] r8169: hold MDIO bus lock while loading firmware javen
2026-12-01 10:18   ` Nicolai Buchwitz
2026-12-02 13:17   ` Andrew Lunn
2026-12-03  2:07     ` Javen
2026-12-03  2:37       ` Andrew Lunn
2026-12-03  3:04         ` Javen
2026-12-04 19:23           ` Andrew Lunn
2026-12-05 10:32   ` netdev-bot+sashiko
2026-12-01  9:44 ` [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen
2026-12-01 10:23   ` Nicolai Buchwitz
2026-12-02  8:55     ` Javen
2026-12-02 12:20       ` Nicolai Buchwitz
2026-12-02 13:21   ` Andrew Lunn
2026-12-05 10:32   ` netdev-bot+sashiko
2026-12-01  9:44 ` [PATCH net-next v5 4/4] net: phy: realtek: add hardware timestamping " javen
2026-12-01 10:36   ` Nicolai Buchwitz
2026-12-02  8:30     ` Javen
2026-12-05 10:32   ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox