* [PATCH net-next v3 0/3] net: phy: realtek: add PTP support for RTL8127a
@ 2026-11-17 6:01 javen
2026-11-17 6:01 ` [PATCH net-next v3 1/3] r8169: hold MDIO bus lock while loading firmware javen
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: javen @ 2026-11-17 6:01 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 resides in its internal PHY (RTL8261C).
Therefore, this series adds PTP support to the Realtek PHY driver
instead of the r8169 MAC driver.
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 | 3 +
drivers/net/phy/realtek/Kconfig | 11 +
drivers/net/phy/realtek/Makefile | 1 +
drivers/net/phy/realtek/realtek.h | 20 +
drivers/net/phy/realtek/realtek_main.c | 19 +-
drivers/net/phy/realtek/realtek_ptp.c | 782 ++++++++++++++++++++++
6 files changed, 835 insertions(+), 1 deletion(-)
create mode 100644 drivers/net/phy/realtek/realtek_ptp.c
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH net-next v3 1/3] r8169: hold MDIO bus lock while loading firmware
2026-11-17 6:01 [PATCH net-next v3 0/3] net: phy: realtek: add PTP support for RTL8127a javen
@ 2026-11-17 6:01 ` javen
2026-11-21 6:39 ` netdev-bot+sashiko
2026-11-17 6:01 ` [PATCH net-next v3 2/3] net: phy: realtek: add PTP clock support for RTL8127a javen
2026-11-17 6:01 ` [PATCH net-next v3 3/3] net: phy: realtek: add hardware timestamping " javen
2 siblings, 1 reply; 7+ messages in thread
From: javen @ 2026-11-17 6:01 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
---
drivers/net/ethernet/realtek/r8169_main.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index f151ab681393..34ea4640467c 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -2686,7 +2686,10 @@ 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. */
+ phy_lock_mdio_bus(tp->phydev);
rtl_fw_write_firmware(tp, tp->rtl_fw);
+ phy_unlock_mdio_bus(tp->phydev);
/* At least one firmware doesn't reset tp->ocp_base. */
tp->ocp_base = OCP_STD_PHY_BASE;
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH net-next v3 2/3] net: phy: realtek: add PTP clock support for RTL8127a
2026-11-17 6:01 [PATCH net-next v3 0/3] net: phy: realtek: add PTP support for RTL8127a javen
2026-11-17 6:01 ` [PATCH net-next v3 1/3] r8169: hold MDIO bus lock while loading firmware javen
@ 2026-11-17 6:01 ` javen
2026-11-21 6:39 ` netdev-bot+sashiko
2026-11-17 6:01 ` [PATCH net-next v3 3/3] net: phy: realtek: add hardware timestamping " javen
2 siblings, 1 reply; 7+ messages in thread
From: javen @ 2026-11-17 6:01 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 clock is started from the system tinme unless it
is already running, so a PHY re-initialization does not make it jump.
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
---
drivers/net/phy/realtek/Kconfig | 11 +
drivers/net/phy/realtek/Makefile | 1 +
drivers/net/phy/realtek/realtek.h | 20 ++
drivers/net/phy/realtek/realtek_main.c | 19 +-
drivers/net/phy/realtek/realtek_ptp.c | 401 +++++++++++++++++++++++++
5 files changed, 451 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..384bde5fff3b 100644
--- a/drivers/net/phy/realtek/realtek.h
+++ b/drivers/net/phy/realtek/realtek.h
@@ -7,4 +7,24 @@
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);
+int rtl8127a_ptp_config_init(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)
+{
+}
+
+static inline int rtl8127a_ptp_config_init(struct phy_device *phydev)
+{
+ return 0;
+}
+#endif
+
#endif /* REALTEK_H */
diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index ca2d515b77b3..e21d10e57a13 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -1839,6 +1839,21 @@ 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 */
+ if (phydev->phy_id == RTL_8261C)
+ return rtl8127a_ptp_probe(phydev);
+
+ 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 +3428,9 @@ 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,
+ .config_init = rtl8127a_ptp_config_init,
.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..9622571249f4
--- /dev/null
+++ b/drivers/net/phy/realtek/realtek_ptp.c
@@ -0,0 +1,401 @@
+// 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_PTP_RESET_SEC 10
+#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 */
+};
+
+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_config_init(struct phy_device *phydev)
+{
+ struct rtl8127a_ptp *ptp = phydev->priv;
+ struct timespec64 ts;
+ int ret;
+
+ if (!ptp)
+ return 0;
+
+ /* Keep the clock running across a PHY re-init, a running PTP
+ * client would otherwise see it jump. Only start it from the
+ * system time if the counter has been reset.
+ */
+ ret = rtl8127a_ptp_gettimex64(&ptp->info, &ts, NULL);
+ if (ret)
+ return ret;
+
+ if (ts.tv_sec >= RTL8127A_PTP_RESET_SEC)
+ return 0;
+
+ ktime_get_real_ts64(&ts);
+
+ return rtl8127a_ptp_settime(ptp, &ts);
+}
+
+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;
+
+ 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] 7+ messages in thread
* [PATCH net-next v3 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
2026-11-17 6:01 [PATCH net-next v3 0/3] net: phy: realtek: add PTP support for RTL8127a javen
2026-11-17 6:01 ` [PATCH net-next v3 1/3] r8169: hold MDIO bus lock while loading firmware javen
2026-11-17 6:01 ` [PATCH net-next v3 2/3] net: phy: realtek: add PTP clock support for RTL8127a javen
@ 2026-11-17 6:01 ` javen
2026-11-21 6:39 ` netdev-bot+sashiko
2 siblings, 1 reply; 7+ messages in thread
From: javen @ 2026-11-17 6:01 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
---
drivers/net/phy/realtek/Kconfig | 8 +-
drivers/net/phy/realtek/realtek_ptp.c | 391 +++++++++++++++++++++++++-
2 files changed, 390 insertions(+), 9 deletions(-)
diff --git a/drivers/net/phy/realtek/Kconfig b/drivers/net/phy/realtek/Kconfig
index feb9a7fd1582..8a928de89287 100644
--- a/drivers/net/phy/realtek/Kconfig
+++ b/drivers/net/phy/realtek/Kconfig
@@ -19,9 +19,9 @@ 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.
endif # REALTEK_PHY
diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
index 9622571249f4..6caa7d112584 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"
@@ -31,8 +34,18 @@
#define RTL8127A_PTP_RESET_SEC 10
#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
@@ -44,6 +57,9 @@
#define RTL8127A_PTP_RATE_HI_MASK GENMASK(21, 16)
#define RTL8127A_PTP_MAX_ADJ 488281
+#define RTL8127A_PTP_TX_TIMEOUT (15 * HZ)
+#define RTL8127A_PTP_TX_POLL_FAST msecs_to_jiffies(2)
+#define RTL8127A_PTP_RX_QUEUE_MAX 64
/* RTL8127A_PTP_CLK_CFG clock adjust modes */
enum rtl8127a_ptp_clkadj_mode {
@@ -54,11 +70,33 @@ enum rtl8127a_ptp_clkadj_mode {
RTL8127A_PTP_RATE_WRITE = 14,
};
+/* ptp->state bits */
+enum rtl8127a_ptp_state {
+ RTL8127A_PTP_TX_IN_PROGRESS,
+};
+
+struct rtl8127a_ptp_skb_cb {
+ u8 msgtype;
+};
+
+#define RTL8127A_PTP_SKB_CB(skb) \
+ ((struct rtl8127a_ptp_skb_cb *)(skb)->cb)
+
struct rtl8127a_ptp {
struct phy_device *phydev;
+ struct mii_timestamper mii_ts;
struct ptp_clock_info info;
struct ptp_clock *clock;
struct mutex lock; /* serializes PTP register access sequences */
+ enum hwtstamp_tx_types tx_type;
+ enum hwtstamp_rx_filters rx_filter;
+ bool rx_enabled;
+ bool hw_enabled;
+ struct sk_buff_head rx_queue;
+ struct sk_buff *tx_skb;
+ unsigned long tx_start;
+ u8 tx_msgtype;
+ unsigned long state;
};
static int rtl8127a_ptp_read(struct rtl8127a_ptp *ptp, u16 reg)
@@ -189,6 +227,53 @@ static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
return 0;
}
+/* Latch the RX or TX timestamp of the given message type into the
+ * RTL8127A_PTP_TRX_TS_TIME registers.
+ */
+static int rtl8127a_ptp_trx_ts_cmd(struct rtl8127a_ptp *ptp, u16 cmd)
+{
+ int ret;
+
+ ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_TRX_TS_STA,
+ RTL8127A_PTP_TRX_TS_SEL_RX |
+ RTL8127A_PTP_TRX_TS_MSGTYPE,
+ RTL8127A_PTP_TRX_TS_RD | cmd);
+ if (ret)
+ return ret;
+
+ 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;
@@ -323,6 +408,107 @@ static int rtl8127a_ptp_settime64(struct ptp_clock_info *info,
return rtl8127a_ptp_settime(ptp, ts);
}
+static void rtl8127a_ptp_tx_complete(struct rtl8127a_ptp *ptp,
+ struct skb_shared_hwtstamps *hwts)
+{
+ struct sk_buff *skb = ptp->tx_skb;
+
+ ptp->tx_skb = NULL;
+ clear_bit_unlock(RTL8127A_PTP_TX_IN_PROGRESS, &ptp->state);
+
+ if (hwts)
+ skb_complete_tx_timestamp(skb, hwts);
+ else
+ kfree_skb(skb);
+}
+
+/* Returns true if no TX timestamp is pending anymore */
+static bool rtl8127a_ptp_tx_tstamp(struct rtl8127a_ptp *ptp)
+{
+ struct skb_shared_hwtstamps hwts = {};
+ struct timespec64 ts;
+ int ret;
+
+ /* Pairs with smp_store_release() in rtl8127a_ptp_txtstamp() */
+ if (!smp_load_acquire(&ptp->tx_skb))
+ return true;
+
+ mutex_lock(&ptp->lock);
+
+ /* Give up if TX timestamping was turned off or nothing arrived */
+ if (READ_ONCE(ptp->tx_type) != HWTSTAMP_TX_ON ||
+ time_is_before_jiffies(ptp->tx_start + RTL8127A_PTP_TX_TIMEOUT)) {
+ rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR,
+ RTL8127A_PTP_INT_TX_TS);
+ mutex_unlock(&ptp->lock);
+
+ rtl8127a_ptp_tx_complete(ptp, NULL);
+ return true;
+ }
+
+ ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_INSR);
+ if (ret < 0 || !(ret & RTL8127A_PTP_INT_TX_TS)) {
+ mutex_unlock(&ptp->lock);
+ return false;
+ }
+
+ ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR,
+ RTL8127A_PTP_INT_TX_TS);
+ if (!ret)
+ ret = rtl8127a_ptp_egress_time(ptp, ptp->tx_msgtype, &ts);
+
+ mutex_unlock(&ptp->lock);
+
+ if (ret) {
+ rtl8127a_ptp_tx_complete(ptp, NULL);
+ return true;
+ }
+
+ hwts.hwtstamp = timespec64_to_ktime(ts);
+ rtl8127a_ptp_tx_complete(ptp, &hwts);
+
+ return true;
+}
+
+static void rtl8127a_ptp_rx_tstamp(struct rtl8127a_ptp *ptp,
+ struct sk_buff *skb)
+{
+ u8 msgtype = RTL8127A_PTP_SKB_CB(skb)->msgtype;
+ struct timespec64 ts;
+ int ret;
+
+ mutex_lock(&ptp->lock);
+ ret = rtl8127a_ptp_trx_ts_cmd(ptp, RTL8127A_PTP_TRX_TS_SEL_RX |
+ FIELD_PREP(RTL8127A_PTP_TRX_TS_MSGTYPE,
+ msgtype) |
+ RTL8127A_PTP_TRX_TS_EXEC);
+ if (!ret)
+ ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_TRX_TS_TIME,
+ &ts);
+ mutex_unlock(&ptp->lock);
+
+ if (!ret)
+ skb_hwtstamps(skb)->hwtstamp = timespec64_to_ktime(ts);
+
+ netif_rx(skb);
+}
+
+static long rtl8127a_ptp_do_aux_work(struct ptp_clock_info *info)
+{
+ struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+ struct sk_buff *skb;
+
+ while ((skb = skb_dequeue(&ptp->rx_queue)))
+ rtl8127a_ptp_rx_tstamp(ptp, skb);
+
+ if (rtl8127a_ptp_tx_tstamp(ptp))
+ return -1;
+
+ /* The TX timestamp is normally ready right away, back off after that */
+ return time_before(jiffies, ptp->tx_start + RTL8127A_PTP_TX_POLL_FAST) ?
+ 0 : 1;
+}
+
static const struct ptp_clock_info rtl8127a_ptp_info = {
.owner = THIS_MODULE,
.name = "RTL8127A PHC",
@@ -331,8 +517,172 @@ static const struct ptp_clock_info rtl8127a_ptp_info = {
.adjtime = rtl8127a_ptp_adjtime,
.gettimex64 = rtl8127a_ptp_gettimex64,
.settime64 = rtl8127a_ptp_settime64,
+ .do_aux_work = rtl8127a_ptp_do_aux_work,
};
+static void rtl8127a_ptp_txtstamp(struct mii_timestamper *mii_ts,
+ struct sk_buff *skb, int type)
+{
+ struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+ mii_ts);
+ struct ptp_header *hdr;
+ u8 msgtype;
+
+ if (READ_ONCE(ptp->tx_type) != HWTSTAMP_TX_ON ||
+ !(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ||
+ (type & PTP_CLASS_VMASK) != PTP_CLASS_V2 ||
+ (type & PTP_CLASS_PMASK) == PTP_CLASS_L2)
+ goto drop;
+
+ hdr = ptp_parse_header(skb, type);
+ if (!hdr)
+ goto drop;
+
+ /* Only event messages are timestamped by the hardware */
+ msgtype = ptp_get_msgtype(hdr, type);
+ if (msgtype > PTP_MSGTYPE_PDELAY_RESP)
+ goto drop;
+
+ /* Only one TX timestamp can be pending at a time */
+ if (test_and_set_bit_lock(RTL8127A_PTP_TX_IN_PROGRESS, &ptp->state))
+ goto drop;
+
+ skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
+ ptp->tx_msgtype = msgtype;
+ ptp->tx_start = jiffies;
+ /* Publish tx_msgtype and tx_start before tx_skb to the aux worker */
+ smp_store_release(&ptp->tx_skb, skb);
+ ptp_schedule_worker(ptp->clock, 0);
+ return;
+
+drop:
+ kfree_skb(skb);
+}
+
+static bool rtl8127a_ptp_rxtstamp(struct mii_timestamper *mii_ts,
+ struct sk_buff *skb, int type)
+{
+ struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+ mii_ts);
+ struct ptp_header *hdr;
+ u8 msgtype;
+
+ if (!READ_ONCE(ptp->rx_enabled) ||
+ (type & PTP_CLASS_VMASK) != PTP_CLASS_V2 ||
+ (type & PTP_CLASS_PMASK) == PTP_CLASS_L2 ||
+ skb_queue_len_lockless(&ptp->rx_queue) >= RTL8127A_PTP_RX_QUEUE_MAX)
+ return false;
+
+ hdr = ptp_parse_header(skb, type);
+ if (!hdr)
+ return false;
+
+ /* Only event messages are timestamped by the hardware */
+ msgtype = ptp_get_msgtype(hdr, type);
+ if (msgtype > PTP_MSGTYPE_PDELAY_RESP)
+ return false;
+
+ RTL8127A_PTP_SKB_CB(skb)->msgtype = msgtype;
+ skb_queue_tail(&ptp->rx_queue, skb);
+ ptp_schedule_worker(ptp->clock, 0);
+
+ return true;
+}
+
+static int rtl8127a_ptp_hwtstamp_get(struct mii_timestamper *mii_ts,
+ struct kernel_hwtstamp_config *config)
+{
+ struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+ mii_ts);
+
+ config->tx_type = ptp->tx_type;
+ config->rx_filter = ptp->rx_filter;
+
+ return 0;
+}
+
+static int rtl8127a_ptp_hwtstamp_set(struct mii_timestamper *mii_ts,
+ struct kernel_hwtstamp_config *config,
+ struct netlink_ext_ack *extack)
+{
+ struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp,
+ mii_ts);
+ bool enable = false;
+ bool rx_enabled;
+ int ret;
+
+ 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 */
+ 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)
+ return ret;
+
+ 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);
+ /* Let the aux worker drop a TX timestamp that is no longer wanted */
+ ptp_schedule_worker(ptp->clock, 0);
+
+ return 0;
+}
+
+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_config_init(struct phy_device *phydev)
{
struct rtl8127a_ptp *ptp = phydev->priv;
@@ -350,12 +700,24 @@ int rtl8127a_ptp_config_init(struct phy_device *phydev)
if (ret)
return ret;
- if (ts.tv_sec >= RTL8127A_PTP_RESET_SEC)
+ if (ts.tv_sec < RTL8127A_PTP_RESET_SEC) {
+ ktime_get_real_ts64(&ts);
+ ret = rtl8127a_ptp_settime(ptp, &ts);
+ if (ret)
+ return ret;
+ }
+
+ /* Restore timestamping in case the PTP block was reset */
+ if (!ptp->hw_enabled)
return 0;
- ktime_get_real_ts64(&ts);
+ ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CTL);
+ if (ret < 0)
+ return ret;
+ if (!(ret & RTL8127A_PTP_CTL_EN))
+ return rtl8127a_ptp_hwtstamp_enable(ptp, true);
- return rtl8127a_ptp_settime(ptp, &ts);
+ return 0;
}
int rtl8127a_ptp_probe(struct phy_device *phydev)
@@ -374,6 +736,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)
@@ -383,7 +748,15 @@ int rtl8127a_ptp_probe(struct phy_device *phydev)
if (IS_ERR(ptp->clock))
return PTR_ERR(ptp->clock);
+ ptp->mii_ts.rxtstamp = rtl8127a_ptp_rxtstamp;
+ ptp->mii_ts.txtstamp = rtl8127a_ptp_txtstamp;
+ ptp->mii_ts.hwtstamp_set = rtl8127a_ptp_hwtstamp_set;
+ ptp->mii_ts.hwtstamp_get = rtl8127a_ptp_hwtstamp_get;
+ ptp->mii_ts.ts_info = rtl8127a_ptp_ts_info;
+
phydev->priv = ptp;
+ phydev->mii_ts = &ptp->mii_ts;
+ phydev->default_timestamp = true;
return 0;
}
@@ -395,7 +768,15 @@ void rtl8127a_ptp_remove(struct phy_device *phydev)
if (!ptp)
return;
+ phydev->mii_ts = NULL;
+ phydev->default_timestamp = false;
+
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] 7+ messages in thread
* Re: [PATCH net-next v3 1/3] r8169: hold MDIO bus lock while loading firmware
2026-11-17 6:01 ` [PATCH net-next v3 1/3] r8169: hold MDIO bus lock while loading firmware javen
@ 2026-11-21 6:39 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-11-21 6:39 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 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 0
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] NULL pointer dereference in r8169_apply_firmware()
(drivers/net/ethernet/realtek/r8169_main.c) on PHY-less RTL8116AF…
- [Medium] r8169_apply_firmware() drops mdio_lock (line 2692) before it
restores the PHY page shadow `tp->ocp_base = OCP_STD_PHY_BASE` (line…
--
r8169: hold MDIO bus lock while loading firmware
This patch takes the MDIO bus lock around rtl_fw_write_firmware() in
r8169_apply_firmware(). The firmware sequence accesses PHY registers
directly and bypasses phylib, so the lock is meant to stop other phylib
users from interleaving their accesses with that sequence.
> diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
> index f151ab6813932..34ea4640467cf 100644
> --- a/drivers/net/ethernet/realtek/r8169_main.c
> +++ b/drivers/net/ethernet/realtek/r8169_main.c
> @@ -2686,7 +2686,10 @@ 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. */
> + phy_lock_mdio_bus(tp->phydev);
[Severity: High]
Can tp->phydev be NULL here?
phy_lock_mdio_bus() dereferences phydev->mdio.bus->mdio_lock and does not
check for NULL. Further down, the same function already guards its phylib
call:
/* PHY soft reset may still be in progress */
if (tp->phydev)
phy_read_poll_timeout(tp->phydev, MII_BMCR, val, ...
That check came in with commit dab515216deff5 ("r8169: add support for
RTL8116af"). It suggests this function is expected to run with rtl_fw set
and phydev NULL on 8116AF.
On an RTL8116AF (RTL_GIGA_MAC_VER_52), rtl_get_sfp_mode() returns
RTL_SFP_8168_AF. rtl_init_one() then skips the MDIO registration:
if (tp->sfp_mode == RTL_SFP_NONE) {
rc = r8169_mdio_register(tp);
so tp->phydev is never set.
The VER_52 chip entry has FIRMWARE_8168FP_3 as its firmware name.
rtl_open() calls rtl_request_firmware() whether or not a PHY exists, so
tp->rtl_fw is set whenever that firmware file is installed.
rtl_hw_start_8117() checks tp->phydev before its own PHY access, but it
calls the firmware helper unconditionally:
/* firmware is for MAC only */
r8169_apply_firmware(tp);
So interface bring-up appears to reach the new lock call with a NULL
phydev:
rtl_open()
rtl8169_up()
rtl_reset_work()
rtl_hw_start()
rtl_hw_start_8117()
r8169_apply_firmware()
phy_lock_mdio_bus(NULL)
Runtime resume and the rtl_task reset path get there too.
Would it work to take and drop the bus lock only when tp->phydev is
non-NULL? Without a phydev there are no phylib bus users to keep out.
> rtl_fw_write_firmware(tp, tp->rtl_fw);
> + phy_unlock_mdio_bus(tp->phydev);
> /* At least one firmware doesn't reset tp->ocp_base. */
> tp->ocp_base = OCP_STD_PHY_BASE;
[Severity: Medium]
Should the tp->ocp_base restore move above phy_unlock_mdio_bus()?
Right now the bus lock is released before the page shadow left by the
firmware is reset. That makes the restore an unlocked write to state that
the Clause 22 bus ops depend on.
The firmware can change tp->ocp_base in two ways:
- r8168g_mdio_write() with reg 0x1f
- mac_mcu_write() with reg 0x1f after PHY_MDIO_CHG, which leaves a MAC
OCP base in it
The comment above the restore says at least one firmware doesn't reset
the value.
Every C22 access builds its address from that shadow:
r8168g_mdio_read() {
...
if (tp->ocp_base != OCP_STD_PHY_BASE)
reg -= 0x10;
return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
}
A C22 phylib user can be blocked on mdio_lock while this runs. One example
is the PHY state machine running read_status after a link change, during
the rtl_task reset or runtime resume. That user can wake as soon as
phy_unlock_mdio_bus() runs and do its BMSR/BMCR access before the restore.
The access would then go to a firmware vendor page or a MAC OCP derived
address.
Paged accesses have a worse case. phy_select_page() reads reg 0x1f and
gets the stale page from the shadow, and phy_restore_page() writes it back
later. If the unlocked restore runs between those two calls, the access
meant for the selected page goes to the standard page instead. The stale
page is then written back, and later C22 accesses keep going to the wrong
place.
This only affects C22 users. The hwmon and PTP users go through C45 with
absolute OCP addressing.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117060120.1175-1-javen_xu%40realsil.com.cn
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net-next v3 2/3] net: phy: realtek: add PTP clock support for RTL8127a
2026-11-17 6:01 ` [PATCH net-next v3 2/3] net: phy: realtek: add PTP clock support for RTL8127a javen
@ 2026-11-21 6:39 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-11-21 6:39 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 7 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 5
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 says the clock 'is started from the system
time unless it is already running', and the comment in…
- [Medium] The PHC is registered at PHY probe and stays available through
/dev/ptpN at all times, but none of its callbacks (gettimex64,…
- [Low] The commit message only says a PTP hardware clock is registered.
- [Low] The Kconfig help calls the PTP clock 'optional', but a failure in
the PTP block now breaks basic PHY operation.
- [Low] rtl8127a_ptp_config_init() assumes that nothing after the PHY
driver's config_init resets the PTP counter.
- [Low] rtl8127a_ptp_hwtstamp_enable() ends by setting BIT(15) of VEND2
0xa640, which the patch defines as RTL8127A_PHY_RST (lines 234-235).
- [Low] The driver never sets the hardware rate-adjust state to a known
value.
--
net: phy: realtek: add PTP clock support for RTL8127a
This adds a PTP hardware clock for the PTP block in the RTL8127A internal
PHY (RTL8261C). The clock is read, set, stepped and frequency adjusted
through vendor MMD registers, and the patch hooks into the internal NBASE-T
PHY driver's probe, remove and config_init. The clock is seeded from the
system time in config_init when the counter looks like it has been reset.
> diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
> index ca2d515b77b36..e21d10e57a130 100644
> --- a/drivers/net/phy/realtek/realtek_main.c
> +++ b/drivers/net/phy/realtek/realtek_main.c
[ ... ]
> @@ -3413,7 +3428,9 @@ 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,
> + .config_init = rtl8127a_ptp_config_init,
[Severity: Low]
This driver entry had no config_init before. After this change, the only
way config_init can fail is an error from the PTP block. The Kconfig help
calls the PTP clock optional, but rtl8127a_ptp_config_init() returns any
error from rtl8127a_ptp_gettimex64() or rtl8127a_ptp_settime(). That
includes -ETIMEDOUT from rtl8127a_ptp_poll(), which gives
RTL8127A_PTP_CLK_CFG_EXEC only 1ms to clear.
config_init is reached through phy_init_hw() on attach:
rtl_open()
r8169_phy_connect()
phylink_connect_phy()
phy_attach_direct()
phy_init_hw()
rtl8127a_ptp_config_init()
rtl8127a_ptp_gettimex64()
rtl8127a_ptp_clkadj_sts()
rtl8127a_ptp_poll()
It is also reached on resume, through mdio_bus_phy_resume() -> phy_init_hw().
Would a transient PTP block failure then stop the interface from being
opened or resumed?
In the same way, rtl_internal_nbaset_probe() returns the error from
rtl8127a_ptp_probe() as is. So an MDIO error in the disable sequence, or a
ptp_clock_register() failure, fails the whole PHY probe. Other PHY drivers
commonly fail probe when PTP registration fails, so the config_init path is
the bigger concern.
Could these paths warn and continue without a usable PHC instead?
> .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 0000000000000..9622571249f45
> --- /dev/null
> +++ b/drivers/net/phy/realtek/realtek_ptp.c
> @@ -0,0 +1,401 @@
[ ... ]
> +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);
[Severity: Medium]
Can this run while the RTL8127A PCI function is runtime suspended?
The PHC is registered at PHY probe and stays reachable through /dev/ptpN.
None of gettimex64, settime64, adjtime or adjfine make sure the MAC is
powered first. The register accesses end up as plain MMIO:
phy_read_mmd()
rtl822x_read_mmd()
rtlgen_read_vend2()
__mdiobus_c45_read()
r8169_mdio_read_reg_c45()
r8168_phy_ocp_read()
r8169 runtime suspends the function 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);
...
}
In D3hot, r8168_phy_ocp_read() would return 0xffff as data.
r8168_phy_ocp_write() is void, so after logging a timeout it drops the write
and the caller sees success. The EXEC poll below then times out on
CLK_CFG == 0xffff. Every PHC operation would fail with -ETIMEDOUT and add log
noise while the NIC is idle.
The device_link that phy_attach_direct() creates makes the PCI device the
consumer and the PHY the supplier, so PHC accesses do not resume the PCI
function. For comparison, r8169's own LED accessors wrap their MMIO in
pm_runtime_resume_and_get().
This needs runtime PM to be allowed for the device (power/control=auto, as
TLP or powertop --auto-tune set it). The existing realtek hwmon support for
this PHY has the same exposure, but the PTP path is new in this patch.
> + if (val < 0)
> + return val;
> +
> + val &= ~RTL8127A_PTP_CLK_CFG_MODE;
> + val |= RTL8127A_PTP_CLK_CFG_EXEC | mode;
[ ... ]
> +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;
[ ... ]
> + } 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);
[Severity: Low]
rtl8127a_ptp_probe() and rtl8127a_ptp_remove() both call
rtl8127a_ptp_hwtstamp_enable(ptp, false). That path clears
RTL8127A_PTP_SYNCE_EN, the INER RX/TX bits and RTL8127A_PTP_CTL_EN, and then
sets RTL8127A_PHY_RST here.
So every bind and unbind of the RTL8127A internal PHY now resets the PHY and
turns off SyncE. Is that intended?
The commit message only talks about registering a PTP clock. The v3
changelog only mentions adding defines for the PHY reset register.
If firmware or a PXE/UEFI environment brought the link up or enabled SyncE,
that state would now be lost at PHY probe rather than at ifup.
The follow-up patch "net: phy: realtek: add hardware timestamping for
RTL8127a" keeps this sequence and also runs it whenever timestamping is
toggled. Its commit message doesn't mention the reset either.
Could the commit message describe and justify the PHY reset and the SyncE
disable?
[Severity: Low]
The reset is asserted with a single read-modify-write of
RTL8127A_PHY_RST_CTL, and the function then returns. Nothing deasserts
RTL8127A_PHY_RST or waits for it to self-clear, the way genphy_soft_reset()
polls BMCR_RESET.
In rtl8127a_ptp_probe(), ptp_clock_register() comes right after this call
and exposes the PTP registers to userspace. r8169 also keeps accessing PHY
registers after PHY probe.
If the bit self-clears but takes a noticeable amount of time, could accesses
made during the reset be lost or see the pre-reset counter? That covers PHC
operations and the counter read in rtl8127a_ptp_config_init().
If the bit is level-sensitive, what releases it?
> +out:
> + mutex_unlock(&ptp->lock);
> +
> + return ret;
> +}
[ ... ]
> +int rtl8127a_ptp_config_init(struct phy_device *phydev)
> +{
> + struct rtl8127a_ptp *ptp = phydev->priv;
> + struct timespec64 ts;
> + int ret;
> +
> + if (!ptp)
> + return 0;
> +
> + /* Keep the clock running across a PHY re-init, a running PTP
> + * client would otherwise see it jump. Only start it from the
> + * system time if the counter has been reset.
> + */
> + ret = rtl8127a_ptp_gettimex64(&ptp->info, &ts, NULL);
> + if (ret)
> + return ret;
> +
> + if (ts.tv_sec >= RTL8127A_PTP_RESET_SEC)
> + return 0;
[Severity: Medium]
Does this check do what the commit message says? The commit message says:
The clock is started from the system tinme unless it is already running
The comment above says the clock is only started from the system time if the
counter has been reset.
The check tests neither of those. It tests whether the free-running counter
has been running for less than 10 seconds since its last reset.
config_init first runs from phy_init_hw() at ifup:
rtl_open()
r8169_phy_connect()
phylink_connect_phy()
...
phy_init_hw()
rtl8127a_ptp_config_init()
The counter starts at power-on, or at the PHY reset that
rtl8127a_ptp_probe() triggers during r8169 probe. More than 10 seconds
before the first ifup is normal after boot, and with a delayed or manual
ifup. In that case, wouldn't the PHC never be set and keep reporting
seconds since reset, close to 1970?
Also, rtl8169_init_phy() calls phy_init_hw() before r8169_hw_phy_config()
and genphy_soft_reset():
drivers/net/ethernet/realtek/r8169_main.c:rtl8169_init_phy() {
phy_init_hw(tp->phydev);
phy_resume(tp->phydev);
r8169_hw_phy_config(tp, tp->phydev, tp->mac_version);
...
genphy_soft_reset(tp->phydev);
}
Any counter reset caused by those later steps happens after this check, so
it would go unnoticed unless another config_init follows within 10 seconds.
The heuristic can also overwrite a clock that a user deliberately set below
10 seconds.
Is there a more reliable way to tell that the counter was reset than its
current value?
> +
> + ktime_get_real_ts64(&ts);
> +
> + return rtl8127a_ptp_settime(ptp, &ts);
[Severity: Low]
This seeds the PHC on the assumption that nothing after the PHY driver's
config_init resets the PTP counter. Does that hold with r8169?
As shown above, rtl8169_init_phy() runs phy_init_hw() before
r8169_hw_phy_config(). For RTL8127A, r8169_hw_phy_config() calls
rtl8127a_1_hw_phy_config(), which starts by loading the PHY MCU firmware:
drivers/net/ethernet/realtek/r8169_phy_config.c:rtl8127a_1_hw_phy_config() {
r8169_apply_firmware(tp);
rtl8168g_enable_gphy_10m(phydev);
...
}
genphy_soft_reset() runs after that, in rtl8169_init_phy().
Suppose either the firmware load or the BMCR soft reset clears the PTP
block. Then the value written here would be wiped on every open. The PHC
would run from about 0 until some later re-init happens to see tv_sec < 10.
Whether either step resets the counter depends on the hardware and can't be
confirmed from the code. The ordering between the two drivers isn't
documented, and the PHY driver has no hook that runs after the MAC driver's
PHY setup.
> +}
> +
> +int rtl8127a_ptp_probe(struct phy_device *phydev)
> +{
[ ... ]
> + ptp->phydev = phydev;
> + ptp->info = rtl8127a_ptp_info;
> +
> + ret = rtl8127a_ptp_hwtstamp_enable(ptp, false);
> + if (ret)
> + return ret;
> +
> + ptp->clock = ptp_clock_register(&ptp->info, dev);
[Severity: Low]
Should the rate-adjust registers be set to a known value before the clock is
registered?
rtl8127a_ptp_adjfine() is the only code that writes the rate (CFG_TIME
NS_LO/NS_HI, latched with RTL8127A_PTP_RATE_WRITE). Probe doesn't write a
zero rate before ptp_clock_register(), and rtl8127a_ptp_remove() doesn't
clear it.
The driver already assumes that PTP block state survives a PHY re-init. If
the latched rate also survives the probe-time reset, a rebind leaves the
hardware running at the last offset the servo applied. Examples are rmmod
and modprobe of realtek with the interface down, or a kexec or warm reboot.
That offset can be up to max_adj, about 488 ppm. The new ptp_clock would
meanwhile report a dialed frequency of 0.
Whether the rate register survives the PHY reset isn't verified. linuxptp
writes the frequency it reads back at startup, which hides most of the
effect. Writing a zero rate at probe would avoid the mismatch.
> + if (IS_ERR(ptp->clock))
> + return PTR_ERR(ptp->clock);
> +
> + phydev->priv = ptp;
> +
> + return 0;
> +}
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117060120.1175-1-javen_xu%40realsil.com.cn
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net-next v3 3/3] net: phy: realtek: add hardware timestamping for RTL8127a
2026-11-17 6:01 ` [PATCH net-next v3 3/3] net: phy: realtek: add hardware timestamping " javen
@ 2026-11-21 6:39 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-11-21 6:39 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 8 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 7 · Low: 0
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() (realtek_ptp.c:764-782) does not quiesce
rxtstamp() or txtstamp() callers that are already running before it…
- [Medium] RX hardware timestamps can be silently wrong.
- [Medium] rtl8127a_ptp_hwtstamp_set() leaves the cached state
inconsistent when turning timestamping off fails.
- [Medium] The TX path decides that the tracked skb's timestamp is ready
using status bits that frames it does not track can also set.
- [Medium] ptp->hw_enabled is a check-then-act value with no lock shared
between its writer and its reader.
- [Medium] Senders other than the PTP daemon can control or corrupt this
driver's hardware timestamps.
- [Medium] rtl8127a_ptp_hwtstamp_enable() always finishes by setting bit
15 of VEND2 0xa640 (RTL8127A_PHY_RST) with a read-modify-write (lines…
- [Medium] rtl8127a_ptp_trx_ts_cmd() (lines 233-248) uses
rtl8127a_ptp_modify() on RTL8127A_PTP_TRX_TS_STA with a mask of only…
--
net: phy: realtek: add hardware timestamping for RTL8127a
This commit registers a mii_timestamper for the RTL8127A internal PHY, so
the PHY can provide hardware timestamps for PTPv2 event messages over UDP
without MAC driver support. Timestamps are read over MDIO from the PTP aux
worker. RX skbs are queued until their timestamp has been read, and a
single pending TX skb is completed once the hardware reports its
timestamp.
> diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
> index 9622571249f45..6caa7d1125840 100644
> --- a/drivers/net/phy/realtek/realtek_ptp.c
> +++ b/drivers/net/phy/realtek/realtek_ptp.c
[ ... ]
> @@ -189,6 +227,53 @@ static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
> return 0;
> }
>
> +/* Latch the RX or TX timestamp of the given message type into the
> + * RTL8127A_PTP_TRX_TS_TIME registers.
> + */
> +static int rtl8127a_ptp_trx_ts_cmd(struct rtl8127a_ptp *ptp, u16 cmd)
> +{
> + int ret;
> +
> + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_TRX_TS_STA,
> + RTL8127A_PTP_TRX_TS_SEL_RX |
> + RTL8127A_PTP_TRX_TS_MSGTYPE,
> + RTL8127A_PTP_TRX_TS_RD | cmd);
[Severity: Medium]
rtl8127a_ptp_modify() reads TRX_TS_STA and writes back every bit outside
SEL_RX | MSGTYPE. That includes the TX_PDLYRSP_RDY, TX_PDLYREQ_RDY,
TX_DLYREQ_RDY and TX_SYNC_RDY status bits (15:12).
INSR in the same block is write-1-to-clear. If these RDY bits work the
same way, wouldn't every latch command clear the ready flags of any
pending TX timestamp?
rtl8127a_ptp_do_aux_work() drains rx_queue before it services the TX skb:
while ((skb = skb_dequeue(&ptp->rx_queue)))
rtl8127a_ptp_rx_tstamp(ptp, skb);
if (rtl8127a_ptp_tx_tstamp(ptp))
So a master that sends Sync while receiving Delay_Req could lose SYNC_RDY
to the RX latch. rtl8127a_ptp_egress_time() would then time out in its
1 ms poll and drop the TX skb.
The tree does not document how the RDY bits behave, so this depends on
the hardware.
> + if (ret)
> + return ret;
[ ... ]
> @@ -323,6 +408,107 @@ 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;
[ ... ]
> + ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_INSR);
> + if (ret < 0 || !(ret & RTL8127A_PTP_INT_TX_TS)) {
> + mutex_unlock(&ptp->lock);
> + return false;
> + }
> +
> + ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR,
> + RTL8127A_PTP_INT_TX_TS);
> + if (!ret)
> + ret = rtl8127a_ptp_egress_time(ptp, ptp->tx_msgtype, &ts);
[Severity: Medium]
Could INSR TX_TS and the per-type RDY bit be left over from a frame the
driver never tracked?
The commit message says the PHY timestamps every PTPv2 UDP event message
on TX. So these bits are also set by frames that rtl8127a_ptp_txtstamp()
drops:
- frames without SKBTX_HW_TSTAMP (any socket sending to UDP port 319)
- frames sent with HWTSTAMP_TX_OFF while the hardware is on for RX
- frames sent while RTL8127A_PTP_TX_IN_PROGRESS is held
- a late frame that goes out after the 15 second timeout path has
cleared INSR
When no tx_skb is pending, the early return above leaves that status in
place, so it is still set when the next skb is armed. The TRX_TS_STA RDY
bits are never cleared explicitly.
rtl8127a_ptp_txtstamp() schedules the worker with delay 0, which is
normally before the new frame is on the wire. If the stale frame was of
the same type, the RDY poll in rtl8127a_ptp_egress_time() succeeds right
away. Wouldn't the previous frame's timestamp then go to
skb_complete_tx_timestamp() for the new skb?
If the stale TX_TS came from a different type, the 1 ms RDY poll can run
out before the tracked frame is sent, and the skb is dropped. Once that
frame goes out it sets INSR and RDY again. The next frame of that type
would then read a timestamp from one sync interval earlier.
[ ... ]
> +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]
Can this report another frame's timestamp as a valid hardware timestamp?
The PHY latched this value when the frame hit the wire. The frame then
went through the MAC, NAPI, rtl8127a_ptp_rxtstamp() and rx_queue before
this read. Any later event message of the same type in that window
overwrites the latch, and this skb gets the later frame's time. Examples
are a Delay_Req from a second client, a Sync from another master, or any
PTPv2 frame sent to UDP port 319.
When two skbs with the same msgtype are in rx_queue together, the first
one always gets the second one's timestamp, and both end up with the
same value.
There also doesn't seem to be a freshness check.
rtl8127a_ptp_hwtstamp_enable() unmasks the RX status:
ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER, 0,
RTL8127A_PTP_INT_RX_TS |
RTL8127A_PTP_INT_TX_TS);
but nothing reads or acknowledges INSR RX_TS to confirm that a capture
happened.
rtl8127a_ptp_rxtstamp() also accepts every non-L2 PTPv2 class, including
IPv6 and VLAN tagged frames. Does the mode programmed in
rtl8127a_ptp_hwtstamp_enable() (RTL8127A_PTP_CTL_TYPE_3 | BIT(12))
timestamp those? If not, those skbs would get a stale or reset latch
value.
The commit message says:
and an RX timestamp may be overwritten by a later message of the
same type before it has been read.
It doesn't say that the overwritten value is still delivered as a valid
timestamp.
The v3 notes also list "Document the per message type limitation". There
doesn't appear to be a matching code comment, and the new Kconfig help
text has no caveat:
and timestamps PTPv2 event messages over UDP on behalf of the
MAC, so no support from the MAC driver is needed.
[ ... ]
> @@ -331,8 +517,172 @@ static const struct ptp_clock_info rtl8127a_ptp_info = {
[ ... ]
> +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;
[ ... ]
> + /* 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;
[Severity: Medium]
Can a sender other than the PTP daemon hold this single slot?
SOF_TIMESTAMPING_TX_HARDWARE needs no capability, and the classifier only
checks the UDP destination port (319). An unprivileged local user could
keep RTL8127A_PTP_TX_IN_PROGRESS set by repeatedly sending PTPv2
formatted packets to port 319 with TX hardware timestamping requested.
ptp4l's own event messages would then take the drop path and get no TX
timestamp.
If the stack classifies a frame as PTP but the PHY doesn't timestamp it,
the slot stays held for the whole RTL8127A_PTP_TX_TIMEOUT (15 seconds).
Timestamps are also matched to skbs by message type only, both in
rtl8127a_ptp_tx_tstamp()->rtl8127a_ptp_egress_time() and in
rtl8127a_ptp_rx_tstamp(). There is no sequenceId match and no check
against tx_start. So any same-type frame that hits the wire before the
worker reads the latch replaces the value. On TX that can be a plain
UDP/319 packet from a local user. On RX it can be a frame from any L2
host.
Other PHY timestamping drivers such as dp83640, mscc, nxp-c45,
bcm-phy-ptp and lan8814 match on sequenceId. Is there another way on
this hardware to reject a latched value that doesn't belong to the skb?
[ ... ]
> +static int rtl8127a_ptp_hwtstamp_set(struct mii_timestamper *mii_ts,
> + struct kernel_hwtstamp_config *config,
> + struct netlink_ext_ack *extack)
> +{
[ ... ]
> + /* The hardware is either on or off, only reprogram it on a change */
> + 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)
> + return ret;
> +
> + ptp->hw_enabled = enable;
> + }
[Severity: Medium]
What happens to the cached state if rtl8127a_ptp_hwtstamp_enable(ptp,
false) fails here?
tx_type and rx_enabled have already been cleared, and the error return
doesn't restore them. ptp->rx_filter and ptp->hw_enabled keep their old
values.
rtl8127a_ptp_hwtstamp_get() would then report HWTSTAMP_TX_OFF together
with the old rx_filter, for example HWTSTAMP_FILTER_PTP_V2_L4_EVENT.
Meanwhile rtl8127a_ptp_rxtstamp() drops every packet because rx_enabled
is false.
hw_enabled also stays true. A later request for HWTSTAMP_TX_ON with
HWTSTAMP_FILTER_PTP_V2_L4_EVENT then sees enable == hw_enabled and skips
rtl8127a_ptp_hwtstamp_enable(ptp, true).
The hardware may be partly disabled at that point: INER cleared, or
CTL_EN cleared if the failure happened at the PHY_RST_CTL step. TX
requests would hit the 15 second timeout, and RX timestamps would be
invalid until a later config_init() happens to see CTL_EN clear.
rtl8127a_ptp_hwtstamp_enable() does several MDIO read-modify-writes
(SYNCE_CTL, INER, CTL, PHY_RST_CTL), and any of them can fail.
The v3 notes say "Only update the cached config after programming the
hardware". Should tx_type and rx_enabled be restored on this error path?
[ ... ]
> @@ -350,12 +700,24 @@ int rtl8127a_ptp_config_init(struct phy_device *phydev)
> if (ret)
> return ret;
>
> - if (ts.tv_sec >= RTL8127A_PTP_RESET_SEC)
> + if (ts.tv_sec < RTL8127A_PTP_RESET_SEC) {
> + ktime_get_real_ts64(&ts);
> + ret = rtl8127a_ptp_settime(ptp, &ts);
> + if (ret)
> + return ret;
> + }
> +
> + /* Restore timestamping in case the PTP block was reset */
> + if (!ptp->hw_enabled)
> return 0;
[Severity: Medium]
What serializes this hw_enabled check against
rtl8127a_ptp_hwtstamp_set()?
rtl8127a_ptp_hwtstamp_set() runs under rtnl. It programs the hardware
inside ptp->lock, then stores ptp->hw_enabled outside any lock.
This path reads hw_enabled and CTL and may call
rtl8127a_ptp_hwtstamp_enable(ptp, true). It holds neither rtnl nor
ptp->lock across that decision.
r8169 reaches this function without rtnl on resume:
rtl8169_runtime_resume()
rtl8169_up()
rtl8169_init_phy()
phy_init_hw()
rtl8127a_ptp_config_init()
The phylib SIOCSHWTSTAMP path also takes no runtime PM reference.
On disable: hwtstamp_set() clears CTL_EN and drops ptp->lock. If
config_init reads hw_enabled before false is stored, it sees EN clear
and re-enables the hardware. The hardware then stays on while software
says off.
On enable: hwtstamp_set() programs the hardware, then the resume path
re-inits the PHY. config_init still reads hw_enabled == false and skips
the restore. The PTP block stays off while tx_type and rx_enabled say
on. Sending the same config again doesn't reprogram it, because it
matches hw_enabled.
>
> - ktime_get_real_ts64(&ts);
> + ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CTL);
> + if (ret < 0)
> + return ret;
> + if (!(ret & RTL8127A_PTP_CTL_EN))
> + return rtl8127a_ptp_hwtstamp_enable(ptp, true);
[Severity: Medium]
Both the enable and disable branches of rtl8127a_ptp_hwtstamp_enable()
end with:
ret = rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0,
RTL8127A_PHY_RST);
and the function never waits for the bit to self-clear.
Before this patch it was only called with enable == false, from probe
and remove. Now it also runs:
- from rtl8127a_ptp_hwtstamp_set() on every on/off change, such as each
ptp4l start or stop
- from here inside phy_init_hw(), right before phylib continues
configuring the PHY
If bit 15 of 0xa640 really is a PHY or datapath reset, as the name
suggests, does toggling timestamping drop and retrain the link behind
phylib's back?
Could the next MDIO accesses, from phylib's config_aneg/read_status or
from the aux worker, land while the reset is still in progress? For
comparison, genphy_soft_reset() waits for BMCR_RESET to clear.
The tree doesn't document what this bit does, so it may turn out to be a
harmless strobe.
[ ... ]
> @@ -395,7 +768,15 @@ void rtl8127a_ptp_remove(struct phy_device *phydev)
> if (!ptp)
> return;
>
> + phydev->mii_ts = NULL;
> + phydev->default_timestamp = false;
> +
> 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]
Can rtl8127a_ptp_rxtstamp() or rtl8127a_ptp_txtstamp() still be running
on another CPU when this teardown frees what they use?
skb_defer_rx_timestamp() and skb_clone_tx_timestamp() drop
rcu_read_lock() before they read phydev->mii_ts and call the callbacks.
There is no synchronize_net() between clearing mii_ts here and
ptp_clock_unregister() destroying the aux kworker.
A NAPI or xmit context already inside the callbacks could run this
after the skb_queue_purge():
skb_queue_tail(&ptp->rx_queue, skb);
ptp_schedule_worker(ptp->clock, 0);
or this after the tx_skb check:
smp_store_release(&ptp->tx_skb, skb);
ptp_schedule_worker(ptp->clock, 0);
Wouldn't that leak the skb and the socket reference taken by
skb_clone_sk()? It would also schedule work on a destroyed clock and
later touch ptp after devm has freed it.
There is also a case that doesn't need a race. phy_remove() calls
drv->remove() without phy_detach(), so root can unbind the PHY driver
through sysfs while the netdev is up.
If this PHY was selected as the hwtstamp provider through tsconfig,
dev->hwprov is only cleared in phy_detach(). The next PTP packet then
takes the hwprov branch of skb_defer_rx_timestamp():
net/core/timestamping.c:skb_defer_rx_timestamp() {
...
mii_ts = phydev->mii_ts;
if (likely(mii_ts->rxtstamp))
return mii_ts->rxtstamp(mii_ts, skb, type);
...
}
Does that dereference the now NULL mii_ts in softirq context?
Before this patch the PHY had no mii_timestamper, so this patch
introduces the problem.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117060120.1175-1-javen_xu%40realsil.com.cn
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-21 6:39 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 6:01 [PATCH net-next v3 0/3] net: phy: realtek: add PTP support for RTL8127a javen
2026-11-17 6:01 ` [PATCH net-next v3 1/3] r8169: hold MDIO bus lock while loading firmware javen
2026-11-21 6:39 ` netdev-bot+sashiko
2026-11-17 6:01 ` [PATCH net-next v3 2/3] net: phy: realtek: add PTP clock support for RTL8127a javen
2026-11-21 6:39 ` netdev-bot+sashiko
2026-11-17 6:01 ` [PATCH net-next v3 3/3] net: phy: realtek: add hardware timestamping " javen
2026-11-21 6:39 ` 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