* [PATCH net-next v6 0/4] net: phy: realtek: add PTP support for RTL8127a
@ 2026-12-04 2:11 javen
2026-12-04 2:11 ` [PATCH net-next v6 1/4] net: ethtool: tsconfig: allow selecting a provider javen
` (3 more replies)
0 siblings, 4 replies; 12+ messages in thread
From: javen @ 2026-12-04 2:11 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 | 14 +
drivers/net/phy/realtek/realtek_main.c | 23 +-
drivers/net/phy/realtek/realtek_ptp.c | 836 ++++++++++++++++++++++
net/ethtool/tsconfig.c | 17 +-
7 files changed, 902 insertions(+), 7 deletions(-)
create mode 100644 drivers/net/phy/realtek/realtek_ptp.c
--
2.43.0
^ permalink raw reply [flat|nested] 12+ messages in thread* [PATCH net-next v6 1/4] net: ethtool: tsconfig: allow selecting a provider 2026-12-04 2:11 [PATCH net-next v6 0/4] net: phy: realtek: add PTP support for RTL8127a javen @ 2026-12-04 2:11 ` javen 2026-12-04 2:11 ` [PATCH net-next v6 2/4] r8169: hold MDIO bus lock while loading firmware javen ` (2 subsequent siblings) 3 siblings, 0 replies; 12+ messages in thread From: javen @ 2026-12-04 2:11 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> A hwtstamp provider can only be 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 rely on dev->hwprov rather than default_timestamp. Reviewed-by: Nicolai Buchwitz <nb@tipi-net.de> Signed-off-by: Javen Xu <javen_xu@realsil.com.cn> --- Changes in v5: - new file. Changes in v6: - modify spelling mistakes --- 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] 12+ messages in thread
* [PATCH net-next v6 2/4] r8169: hold MDIO bus lock while loading firmware 2026-12-04 2:11 [PATCH net-next v6 0/4] net: phy: realtek: add PTP support for RTL8127a javen 2026-12-04 2:11 ` [PATCH net-next v6 1/4] net: ethtool: tsconfig: allow selecting a provider javen @ 2026-12-04 2:11 ` javen 2026-12-05 2:59 ` netdev-bot+sashiko 2026-12-04 2:11 ` [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen 2026-12-04 2:11 ` [PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping " javen 3 siblings, 1 reply; 12+ messages in thread From: javen @ 2026-12-04 2:11 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. Reviewed-by: Nicolai Buchwitz <nb@tipi-net.de> 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 Changes in v6: - 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..b9068957ffa4 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) 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] 12+ messages in thread
* Re: [PATCH net-next v6 2/4] r8169: hold MDIO bus lock while loading firmware 2026-12-04 2:11 ` [PATCH net-next v6 2/4] r8169: hold MDIO bus lock while loading firmware javen @ 2026-12-05 2:59 ` netdev-bot+sashiko 0 siblings, 0 replies; 12+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 2:59 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 patch "r8169: hold MDIO bus lock while loading firmware" fixes a race that is already in mainline, but the commit message… -- 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 loader accesses PHY registers directly and bypasses phylib. The lock is skipped on PHY-less parts where tp->phydev is NULL. > diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c > index f151ab6813932..b9068957ffa4f 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) off the bus meanwhile. */ > + if (tp->phydev) > + phy_lock_mdio_bus(tp->phydev); > rtl_fw_write_firmware(tp, tp->rtl_fw); [Severity: Low] Can this race already be hit in mainline? If so, should the commit carry a Fixes: tag? The "Realtek Internal NBASE-T PHY" entry uses rtl822x_probe(). When CONFIG_REALTEK_PHY_HWMON is enabled, that function registers a hwmon device for the internal PHY: drivers/net/phy/realtek/realtek_main.c:rtl822x_probe() { ... if (IS_ENABLED(CONFIG_REALTEK_PHY_HWMON) && phydev->phy_id != RTL_GENERIC_PHYID) return rtl822x_hwmon_init(phydev); ... } That code came in with: 33700ca45b7d ("net: phy: realtek: add hwmon support for temp sensor on RTL822x") A hwmon sysfs read takes only the MDIO bus lock, not RTNL: rtl822x_hwmon_read() phy_read_mmd(phydev, MDIO_MMD_VEND2, ...) r8169_mdio_read_reg_c45() r8168_phy_ocp_read() r8168_phy_ocp_read() writes the address to GPHY_OCP and then reads the data back from the same register. Before this patch, rtl_fw_write_firmware() drove GPHY_OCP through r8168g_mdio_read() and r8168g_mdio_write() without holding that lock. It runs during ifup, reset work and resume. If a sensor read overlaps the firmware load, could the PHY_READ, PHY_DATA_OR/AND and PHY_WRITE_PREVIOUS sequences read from or write to the wrong PHY register? Could hwmon also report a wrong temperature? The new comment names hwmon as the competing user. The commit message does not say that the race exists today or what users see when it hits. The later patches in the series do not add a Fixes: tag either, so kernels that already have the hwmon support (v6.14 and later) would not pick this up. Would a tag like this be appropriate? Fixes: 33700ca45b7d ("net: phy: realtek: add hwmon support for temp sensor on RTL822x") Could this also go to net on its own, instead of as part of the RTL8127a PTP series? > /* 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); [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204021116.66-1-javen_xu%40realsil.com.cn ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a 2026-12-04 2:11 [PATCH net-next v6 0/4] net: phy: realtek: add PTP support for RTL8127a javen 2026-12-04 2:11 ` [PATCH net-next v6 1/4] net: ethtool: tsconfig: allow selecting a provider javen 2026-12-04 2:11 ` [PATCH net-next v6 2/4] r8169: hold MDIO bus lock while loading firmware javen @ 2026-12-04 2:11 ` javen 2026-12-04 19:08 ` Andrew Lunn ` (2 more replies) 2026-12-04 2:11 ` [PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping " javen 3 siblings, 3 replies; 12+ messages in thread From: javen @ 2026-12-04 2:11 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, setting the time is left to userspace. 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. Changes in v6: - Drop a stale sentence from the commit message and the unused time_set - Use phy_read_mmd_poll_timeout() - Move the timestamping enable path to the timestamping patch - Use diff_by_scaled_ppm() in adjfine() to keep the full resolution - Return -EBUSY from the PHC operations while the MAC is runtime suspend --- 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 | 382 +++++++++++++++++++++++++ 5 files changed, 430 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..16ba3fe9697a --- /dev/null +++ b/drivers/net/phy/realtek/realtek_ptp.c @@ -0,0 +1,382 @@ +// 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/pm_runtime.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_INER 0xe402 +#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_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 */ +}; + +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 0 or a negative error code. + */ +static int rtl8127a_ptp_poll(struct rtl8127a_ptp *ptp, u16 reg, u16 mask, + bool set) +{ + int val; + + return phy_read_mmd_poll_timeout(ptp->phydev, MDIO_MMD_VEND2, reg, val, + !!(val & mask) == set, 1, 1000, false); +} + +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; + + return rtl8127a_ptp_poll(ptp, RTL8127A_PTP_CLK_CFG, + RTL8127A_PTP_CLK_CFG_EXEC, false); +} + +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_disable(struct rtl8127a_ptp *ptp) +{ + int ret; + + mutex_lock(&ptp->lock); + + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL, + RTL8127A_PTP_SYNCE_EN, 0); + if (ret) + goto out; + + 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; +} + +/* The PHY is accessed through the MAC, which may be runtime suspended + * while there is no link. Don't resume it for the PHC, fail instead. + * Return > 0 if a reference was token, -EINVAL if runtime PM isn't in + * use, or -EBUSY if the MAC is suspend. + */ +static int rtl8127a_ptp_pm_get(struct rtl8127a_ptp *ptp) +{ + int ret = pm_runtime_get_if_active(ptp->phydev->mdio.bus->parent); + + return ret ? ret : -EBUSY; +} + +static void rtl8127a_ptp_pm_put(struct rtl8127a_ptp *ptp, int pm) +{ + if (pm > 0) + pm_runtime_put(ptp->phydev->mdio.bus->parent); +} + +static int rtl8127a_ptp_settime(struct rtl8127a_ptp *ptp, + const struct timespec64 *ts) +{ + int pm, ret; + + pm = rtl8127a_ptp_pm_get(ptp); + if (pm == -EBUSY) + return pm; + + 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); + rtl8127a_ptp_pm_put(ptp, pm); + + 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); + int pm, ret; + u64 diff; + u32 rate; + + if (diff_by_scaled_ppm(1ULL << 32, scaled_ppm, &diff)) + rate = -(u32)diff; + else + rate = diff; + + pm = rtl8127a_ptp_pm_get(ptp); + if (pm == -EBUSY) + return pm; + + 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); + rtl8127a_ptp_pm_put(ptp, pm); + + 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 pm, ret; + + pm = rtl8127a_ptp_pm_get(ptp); + if (pm == -EBUSY) + return pm; + + 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); + rtl8127a_ptp_pm_put(ptp, pm); + + 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 pm, ret; + + pm = rtl8127a_ptp_pm_get(ptp); + if (pm == -EBUSY) + return pm; + + 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); + rtl8127a_ptp_pm_put(ptp, pm); + + 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_disable(ptp); + 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_disable(ptp); + ptp_clock_unregister(ptp->clock); + phydev->priv = NULL; +} -- 2.43.0 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a 2026-12-04 2:11 ` [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen @ 2026-12-04 19:08 ` Andrew Lunn 2026-12-04 19:15 ` Andrew Lunn 2026-12-05 2:59 ` netdev-bot+sashiko 2026-12-05 11:19 ` Nicolai Buchwitz 2 siblings, 1 reply; 12+ messages in thread From: Andrew Lunn @ 2026-12-04 19:08 UTC (permalink / raw) To: javen Cc: hkallweit1, linux, davem, edumazet, kuba, pabeni, nb, maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean > Changes in v6: > - Drop a stale sentence from the commit message and the unused time_set > - Use phy_read_mmd_poll_timeout() So you still have PHY code in the MAC driver? I had a quick look at the code. I don't see it access any MAC registers, it is all PHY. So why is this in the MAC driver? Please don't ignore what i said. New code should not violate the layering. If there is a legitimate reason for this code to be in the MAC driver, please explain, in detail, what it is. Otherwise move it into the PHY driver. Andrew --- pw-bot: cr ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a 2026-12-04 19:08 ` Andrew Lunn @ 2026-12-04 19:15 ` Andrew Lunn 0 siblings, 0 replies; 12+ messages in thread From: Andrew Lunn @ 2026-12-04 19:15 UTC (permalink / raw) To: javen Cc: hkallweit1, linux, davem, edumazet, kuba, pabeni, nb, maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean On Fri, Dec 04, 2026 at 08:08:53PM +0100, Andrew Lunn wrote: > > Changes in v6: > > - Drop a stale sentence from the commit message and the unused time_set > > - Use phy_read_mmd_poll_timeout() > > So you still have PHY code in the MAC driver? Gerr. I'm wrong here. This is in the PHY driver. Please ignore me. Sorry Andrew ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a 2026-12-04 2:11 ` [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen 2026-12-04 19:08 ` Andrew Lunn @ 2026-12-05 2:59 ` netdev-bot+sashiko 2026-12-05 11:19 ` Nicolai Buchwitz 2 siblings, 0 replies; 12+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 2:59 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 10 potential issue(s) to consider. Critical: 0 · High: 0 · Medium: 5 · 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] rtl8127a_ptp_adjfine() can program a positive frequency request as the largest negative adjustment. - [Medium] The comment on rtl8127a_ptp_pm_get() says -EINVAL means 'runtime PM isn't in use', and settime/adjfine/adjtime/gettimex64… - [Medium] PTP block state is programmed only at probe. - [Medium] Probe and remove have side effects the commit message does not describe; it says only that 'the frequency adjustment is cleared at… - [Medium] net: phy: realtek: rtl8127a_ptp_pm_put() resets r8169 runtime suspend timer - [Low] A user-visible limitation is missing from the commit message, and the Kconfig help overstates MAC independence. - [Low] rtl8127a_ptp_remove() calls rtl8127a_ptp_hwtstamp_disable(), which disables the PTP block and sets RTL8127A_PHY_RST, before… - [Low] PHC multi-register sequences (five CFG_TIME writes, then CLK_CFG EXEC, then poll, in rtl8127a_ptp_settime/adjtime/adjfine/gettimex64)… - [Low] rtl8127a_ptp_hwtstamp_disable() sets RTL8127A_PHY_RST (BIT(15) of VEND2 0xa640) with a read-modify-write that also writes back every… - [Low] net: phy: realtek: unused linux/timekeeping.h include in realtek_ptp.c -- net: phy: realtek: add PTP clock support for RTL8127a This patch registers a PTP hardware clock for the internal PHY of RTL8127A (RTL8261C). The clock is read, set, stepped and frequency adjusted through vendor MMD registers, and a driver mutex serializes the multi register sequences. At probe the PTP block is disabled and the frequency adjustment is cleared. > diff --git a/drivers/net/phy/realtek/Kconfig b/drivers/net/phy/realtek/Kconfig > index a741b34d193ed..feb9a7fd15820 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. [Severity: Low] Is the last sentence of this help text accurate? rtl8127a_ptp_pm_get() checks the runtime PM state of phydev->mdio.bus->parent, which is the MAC. Every PHC op returns -EBUSY while the MAC is runtime suspended. r8169 runtime suspends the MAC on link loss and after close: rtl_mac_link_down() pm_runtime_idle(tp_to_dev(tp)); rtl8169_runtime_idle() then schedules the suspend 10 seconds after carrier loss. So /dev/ptpN returns -EBUSY whenever the cable is unplugged or the interface has been down for a while. The series also depends on an r8169 change ("r8169: hold MDIO bus lock while loading firmware"). This limitation is only mentioned in the v6 changelog below the "---", which won't end up in the applied commit. Could the commit message describe it, and the help text be adjusted? Whether the PHC exists at all also depends on the MAC PM state when the driver binds. If the PHY driver binds while the MAC is runtime suspended, rtl8127a_ptp_hwtstamp_disable() runs against the suspended MAC with no PM check. rtl8127a_ptp_adjfine(&ptp->info, 0) then returns -EBUSY, and probe only logs "PTP support disabled". > + > endif # REALTEK_PHY [ ... ] > diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c > index a39ad18382f25..483eaf6fb6b17 100644 > --- a/drivers/net/phy/realtek/realtek_main.c > +++ b/drivers/net/phy/realtek/realtek_main.c [ ... ] > @@ -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, [Severity: Medium] What restores the PTP block state after the PHY is reset or reinitialized? The rate and the PTP control bits are only programmed in rtl8127a_ptp_probe(). adjfine writes the rate to hardware but doesn't cache it. r8169 calls rtl8169_init_phy() from every rtl8169_up(), which covers open, system resume and runtime resume: 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); On system suspend without WoL, the internal PHY may also lose power. This driver entry has no .soft_reset, no .config_init and no PTP aware .resume (it uses rtlgen_resume). If any of these events reset the PTP block, would the PHC time jump and the rate silently fall back to the hardware default? The PTP core would still report ptp->dialed_frequency for clock_adjtime(modes=0). The v3 changelog ("only start it from the system time if the counter has been reset") suggests these resets do happen. The hwtstamp_set re-check added by the next patch in the series only looks at PTP_CTL_EN, not the rate or the time. > .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..16ba3fe9697a0 > --- /dev/null > +++ b/drivers/net/phy/realtek/realtek_ptp.c > @@ -0,0 +1,382 @@ [ ... ] > +static int rtl8127a_ptp_hwtstamp_disable(struct rtl8127a_ptp *ptp) > +{ > + int ret; > + > + mutex_lock(&ptp->lock); > + > + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL, > + RTL8127A_PTP_SYNCE_EN, 0); > + if (ret) > + goto out; > + > + 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] The commit message says: The frequency adjustment is cleared at probe, setting the time is left to userspace. rtl8127a_ptp_probe() also calls rtl8127a_ptp_hwtstamp_disable(). That clears SYNCE_EN, masks the RX/TX timestamp interrupts, clears PTP_CTL_EN and sets RTL8127A_PHY_RST. Could the commit message describe these side effects too? Nothing in this patch sets PTP_CTL_EN again. In the next patch ("add hardware timestamping for RTL8127a") it is only set when timestamping is enabled, and that patch's hwtstamp_set treats a cleared CTL_EN as a PTP clock reset. If PTP_CTL_EN gates the counter, is the PHC registered here stopped? That would contradict the "free running clock" in the commit message and leave this patch non-functional on its own. In the full series, the clock would only run while timestamping is on. Because REALTEK_PHY_PTP is default y, the SyncE disable and the PHY reset bit would also hit every RTL8127A user. > + if (ret) > + goto out; > + > + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0, > + RTL8127A_PHY_RST); [Severity: Low] Is RTL8127A_PHY_RST self-clearing? It is set with a read-modify-write that also writes back every other bit it read. Nothing waits for the reset to complete, and nothing ever clears the bit. In rtl8127a_ptp_probe(), rtl8127a_ptp_adjfine(&ptp->info, 0) runs right after this. It programs CFG_TIME and polls CLK_CFG EXEC with no delay. If the bit is a self-clearing reset, can those writes land while the block is still in reset and be lost, with probe still reporting success? If it isn't self-clearing, nothing in the driver ever clears it. For comparison, genphy_soft_reset() polls BMCR_RESET until it clears. rtl8127a_ptp_remove() also issues this reset while the PHC is still registered. > +out: > + mutex_unlock(&ptp->lock); > + > + return ret; > +} > + > +/* The PHY is accessed through the MAC, which may be runtime suspended > + * while there is no link. Don't resume it for the PHC, fail instead. > + * Return > 0 if a reference was token, -EINVAL if runtime PM isn't in > + * use, or -EBUSY if the MAC is suspend. > + */ > +static int rtl8127a_ptp_pm_get(struct rtl8127a_ptp *ptp) > +{ > + int ret = pm_runtime_get_if_active(ptp->phydev->mdio.bus->parent); > + > + return ret ? ret : -EBUSY; > +} [Severity: Medium] Does -EINVAL really mean that runtime PM isn't in use? pm_runtime_get_if_active() goes through pm_runtime_get_conditional(), which returns -EINVAL whenever runtime PM is disabled, before it looks at runtime_status: drivers/base/power/runtime.c:pm_runtime_get_conditional() { ... if (dev->power.disable_depth > 0) { retval = -EINVAL; } else if (dev->power.runtime_status != RPM_ACTIVE) { ... } The PM core disables runtime PM on the MAC from device_suspend_late() until device_resume_early(). In that window r8169 is suspended, and after the noirq phase the PCI device is in D3hot. settime, adjfine, adjtime and gettimex64 only bail out on -EBUSY. In that window, wouldn't they keep doing MDIO accesses through the suspended MAC? That goes against the "fail instead" rule in the comment. A caller can plausibly land in this window. The ptp kthread worker isn't freezable, and vclocks on this PHC call the parent's getcycles64 -> gettimex64 every 2 seconds from ptp_vclock_refresh() -> ptp_vclock_sample(). rtl8127a_ptp_hwtstamp_disable() also does MDIO read-modify-writes at probe and remove without any PM check. Minor: in the comment, "token" should be "taken" and "is suspend" should be "is suspended". [ ... ] > +static int rtl8127a_ptp_settime(struct rtl8127a_ptp *ptp, > + const struct timespec64 *ts) > +{ > + int pm, ret; > + > + pm = rtl8127a_ptp_pm_get(ptp); > + if (pm == -EBUSY) > + return pm; > + > + 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); [Severity: Low] The five CFG_TIME writes, the CLK_CFG EXEC write and the EXEC poll are serialized only by ptp->lock. What stops a PHY reset or reconfiguration from landing in the middle of this sequence? rtl8169_init_phy() -> phy_init_hw() / r8169_hw_phy_config() / genphy_soft_reset() runs on every up and resume, and the firmware load also reprograms the PHY. None of those paths take ptp->lock. The PHC ops take neither phydev->lock nor RTNL. Suppose a soft reset lands between the staging writes and EXEC, or during the poll. Could the staged values be lost or the latch aborted, giving a wrong time or rate, or a poll timeout? The same applies to adjtime, adjfine and gettimex64. > + rtl8127a_ptp_pm_put(ptp, pm); > + > + 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); > + int pm, ret; > + u64 diff; > + u32 rate; > + > + if (diff_by_scaled_ppm(1ULL << 32, scaled_ppm, &diff)) > + rate = -(u32)diff; > + else > + rate = diff; [Severity: Medium] Can a positive frequency request end up programmed as the largest negative adjustment? The rate is written as a 22 bit two's complement value: bits 15:0 go to NS_LO and bits 21:16 to NS_HI. RTL8127A_PTP_MAX_ADJ is 488281, the exact limit of that field. ptp_clock_adjtime() checks only the truncated ppb against max_adj, then passes the unrounded scaled_ppm to the driver: drivers/ptp/ptp_clock.c:ptp_clock_adjtime() { ... ppb = scaled_ppm_to_ppb(tx->freq); if (ppb > ops->max_adj || ppb < -ops->max_adj) return -ERANGE; err = ops->adjfine(ops, tx->freq); ... } Every scaled_ppm from 32,000,000 to 32,000,048 gives ppb = 488281, so the check passes. diff_by_scaled_ppm(1ULL << 32, ...) then returns 0x200000 to 0x200003, which sets bit 21, the sign bit. For example, freq = 32,000,000 gives diff = 0x200000, so NS_LO = 0x0000 and NS_HI = 0x20. The driver uses the same encoding for about -488 ppm. Would clamping diff to 0x1FFFFF, or lowering max_adj to 488280, avoid this? The negative side looks fine, since floor rounding rejects anything past diff 0x1FFFFE. > + > + pm = rtl8127a_ptp_pm_get(ptp); > + if (pm == -EBUSY) > + return pm; > + > + 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)); [ ... ] > +void rtl8127a_ptp_remove(struct phy_device *phydev) > +{ > + struct rtl8127a_ptp *ptp = phydev->priv; > + > + if (!ptp) > + return; > + > + rtl8127a_ptp_hwtstamp_disable(ptp); > + ptp_clock_unregister(ptp->clock); [Severity: Low] Should the clock be unregistered before the hardware is quiesced? Until ptp_clock_unregister() runs, userspace and the vclock workers can still call the clock ops. A PHC op waiting on ptp->lock gets the mutex as soon as rtl8127a_ptp_hwtstamp_disable() drops it. It then writes CFG_TIME or the rate and triggers CLK_CFG EXEC on a block that was just disabled and put into PHY reset. Couldn't that make the poll time out, or leave the hardware partly reprogrammed after teardown? remove() also takes no runtime PM reference. An unbind while the MAC is runtime suspended would then do MDIO accesses to the suspended MAC. > + phydev->priv = NULL; > +} [ ... ] > @@ -0,0 +1,382 @@ > +// 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/pm_runtime.h> > +#include <linux/ptp_clock_kernel.h> > +#include <linux/timekeeping.h> [Severity: Low] Is linux/timekeeping.h still needed? It looks like a leftover from the earlier revisions, which seeded the clock from the system time. v5 dropped that and v6 removed the unused time_set. Nothing in this file, or in the timestamping patch that follows, uses a symbol from timekeeping.h directly: - timespec64 and ns_to_timespec64() come from linux/time64.h and linux/time.h. - ptp_read_system_prets() and ptp_read_system_postts() come from linux/ptp_clock_kernel.h, which pulls in its own dependencies. Could the include be dropped? > + > +#include "realtek.h" > + > +#define RTL8127A_PTP_CTL 0xe400 > +#define RTL8127A_PTP_CTL_EN BIT(0) > +#define RTL8127A_PTP_INER 0xe402 > +#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_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 */ > +}; > + > +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 0 or a negative error code. > + */ > +static int rtl8127a_ptp_poll(struct rtl8127a_ptp *ptp, u16 reg, u16 mask, > + bool set) > +{ > + int val; > + > + return phy_read_mmd_poll_timeout(ptp->phydev, MDIO_MMD_VEND2, reg, val, > + !!(val & mask) == set, 1, 1000, false); > +} > + > +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; > + > + return rtl8127a_ptp_poll(ptp, RTL8127A_PTP_CLK_CFG, > + RTL8127A_PTP_CLK_CFG_EXEC, false); > +} > + > +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_disable(struct rtl8127a_ptp *ptp) > +{ > + int ret; > + > + mutex_lock(&ptp->lock); > + > + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL, > + RTL8127A_PTP_SYNCE_EN, 0); > + if (ret) > + goto out; > + > + 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; > +} > + > +/* The PHY is accessed through the MAC, which may be runtime suspended > + * while there is no link. Don't resume it for the PHC, fail instead. > + * Return > 0 if a reference was token, -EINVAL if runtime PM isn't in > + * use, or -EBUSY if the MAC is suspend. > + */ > +static int rtl8127a_ptp_pm_get(struct rtl8127a_ptp *ptp) > +{ > + int ret = pm_runtime_get_if_active(ptp->phydev->mdio.bus->parent); > + > + return ret ? ret : -EBUSY; > +} > + > +static void rtl8127a_ptp_pm_put(struct rtl8127a_ptp *ptp, int pm) > +{ > + if (pm > 0) > + pm_runtime_put(ptp->phydev->mdio.bus->parent); > +} > + > +static int rtl8127a_ptp_settime(struct rtl8127a_ptp *ptp, > + const struct timespec64 *ts) > +{ > + int pm, ret; > + > + pm = rtl8127a_ptp_pm_get(ptp); > + if (pm == -EBUSY) > + return pm; > + > + 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); > + rtl8127a_ptp_pm_put(ptp, pm); > + > + 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); > + int pm, ret; > + u64 diff; > + u32 rate; > + > + if (diff_by_scaled_ppm(1ULL << 32, scaled_ppm, &diff)) > + rate = -(u32)diff; > + else > + rate = diff; > + > + pm = rtl8127a_ptp_pm_get(ptp); > + if (pm == -EBUSY) > + return pm; > + > + 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); > + rtl8127a_ptp_pm_put(ptp, pm); > + > + 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 pm, ret; > + > + pm = rtl8127a_ptp_pm_get(ptp); > + if (pm == -EBUSY) > + return pm; > + > + 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); > + rtl8127a_ptp_pm_put(ptp, pm); > + > + 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 pm, ret; > + > + pm = rtl8127a_ptp_pm_get(ptp); > + if (pm == -EBUSY) > + return pm; > + > + 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); > + rtl8127a_ptp_pm_put(ptp, pm); > + > + 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_disable(ptp); > + 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_disable(ptp); > + ptp_clock_unregister(ptp->clock); > + phydev->priv = NULL; > +} [Severity: Medium] Does this pm_runtime_put() interfere with r8169's own runtime suspend scheduling? pm_runtime_put() is __pm_runtime_idle() with RPM_GET_PUT | RPM_ASYNC. Every time the PHC drops the usage count back to 0, it queues an idle request on pm_wq. rpm_idle() allows that even while the suspend timer is armed. r8169 holds no usage count while the link is up. It also holds none during the 10 second window after carrier loss or close. The queued idle request then runs rtl8169_runtime_idle(): if (!netif_running(tp->dev) || !netif_carrier_ok(tp->dev)) pm_schedule_suspend(device, 10000); pm_schedule_suspend() calls pm_runtime_cancel_pending() and restarts dev->power.suspend_timer 10 seconds from now. So if phc2sys or chrony reads /dev/ptpN once a second while the cable is unplugged, the MAC would never reach runtime suspend. That defeats the point of returning -EBUSY instead of resuming the MAC. While the link is up, each PHC op also queues dev->power.work and wakes a kworker. A single PTP_SYS_OFFSET_EXTENDED ioctl calls rtl8127a_ptp_gettimex64() up to PTP_MAX_SAMPLES (25) times. That is up to 25 pm_wq wakeups per ioctl. The timestamping patch later in the series keeps the same helper. Would pm_runtime_put_noidle() be enough here, given that the PHC never resumes the MAC itself? One thing to consider: if the suspend timer fires while the PHC holds its reference, rpm_suspend() fails on the usage count. With _noidle nothing would retry the suspend. Is there a way to take the reference that doesn't kick the MAC's idle callback on every PHC operation? -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204021116.66-1-javen_xu%40realsil.com.cn ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a 2026-12-04 2:11 ` [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen 2026-12-04 19:08 ` Andrew Lunn 2026-12-05 2:59 ` netdev-bot+sashiko @ 2026-12-05 11:19 ` Nicolai Buchwitz 2 siblings, 0 replies; 12+ messages in thread From: Nicolai Buchwitz @ 2026-12-05 11:19 UTC (permalink / raw) To: javen Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean Hi Javen On 4.12.2026 03:11, javen wrote: > [...] > +static int rtl8127a_ptp_adjfine(struct ptp_clock_info *info, long > scaled_ppm) > +{ > + struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, > info); > + int pm, ret; > + u64 diff; > + u32 rate; > + > + if (diff_by_scaled_ppm(1ULL << 32, scaled_ppm, &diff)) > + rate = -(u32)diff; > + else > + rate = diff; Looks like my suggestion broke an edge case here, sorry! scaled_ppm = 32000000 passes the max_adj check but gives 0x200000, which sets the sign bit. max_adj = 488280 should fix it. > [...] Thanks Nicolai ^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping for RTL8127a 2026-12-04 2:11 [PATCH net-next v6 0/4] net: phy: realtek: add PTP support for RTL8127a javen ` (2 preceding siblings ...) 2026-12-04 2:11 ` [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen @ 2026-12-04 2:11 ` javen 2026-12-05 2:59 ` netdev-bot+sashiko 2026-12-05 11:07 ` Nicolai Buchwitz 3 siblings, 2 replies; 12+ messages in thread From: javen @ 2026-12-04 2:11 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 turn the hardware off if enabling it fails - Restore timestamping only link_change_notify() Changes in v6: - Add the timestamping enable path moved over from the PTP clock patch - Drop link_change_notify(), reprogram a reset PTP block in hwtstamp_set() instead - Don't report SOF_TIMESTAMPING_TX_SOFTWARE --- drivers/net/phy/realtek/Kconfig | 10 +- drivers/net/phy/realtek/realtek_ptp.c | 458 +++++++++++++++++++++++++- 2 files changed, 462 insertions(+), 6 deletions(-) diff --git a/drivers/net/phy/realtek/Kconfig b/drivers/net/phy/realtek/Kconfig index feb9a7fd1582..53ccea53fd5e 100644 --- a/drivers/net/phy/realtek/Kconfig +++ b/drivers/net/phy/realtek/Kconfig @@ -19,9 +19,11 @@ config REALTEK_PHY_PTP depends on PTP_1588_CLOCK && NETWORK_PHY_TIMESTAMPING depends on !(REALTEK_PHY=y && PTP_1588_CLOCK=m) help - Optional PTP hardware clock support for the internal PHY of - RTL8127A. The clock is provided by the PHY itself and can be - read and adjusted through the PTP clock device, no support from - the MAC driver is needed. + Optional PTP hardware clock and hardware timestamping support + for the internal PHY of RTL8127A. The PHY registers a PTP clock + and timestamps PTPv2 event messages over UDP on behalf of the + MAC, so no support from the MAC driver is needed. + The hardware keeps only one timestamp per message type and + direction, so a single PTP application on the link is assumed. endif # REALTEK_PHY diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c index 16ba3fe9697a..9f82466c2b23 100644 --- a/drivers/net/phy/realtek/realtek_ptp.c +++ b/drivers/net/phy/realtek/realtek_ptp.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0+ /* - * PTP hardware clock support for the internal PHY of RTL8127A + * PTP hardware clock and timestamping support for the internal PHY of + * RTL8127A */ #include <linux/bitfield.h> @@ -9,26 +10,42 @@ #include <linux/math64.h> #include <linux/netdevice.h> #include <linux/phy.h> +#include <linux/ptp_classify.h> #include <linux/pm_runtime.h> #include <linux/ptp_clock_kernel.h> +#include <linux/skbuff.h> #include <linux/timekeeping.h> #include "realtek.h" #define RTL8127A_PTP_CTL 0xe400 #define RTL8127A_PTP_CTL_EN BIT(0) +#define RTL8127A_PTP_CTL_TYPE_3 0x003f #define RTL8127A_PTP_INER 0xe402 +#define RTL8127A_PTP_INSR 0xe404 #define RTL8127A_PTP_INT_RX_TS BIT(2) #define RTL8127A_PTP_INT_TX_TS BIT(3) #define RTL8127A_PTP_SYNCE_CTL 0xe406 #define RTL8127A_PTP_SYNCE_EN BIT(0) +#define RTL8127A_PTP_GEN_CFG 0xe408 +#define RTL8127A_PTP_INT_CLR_MODE BIT(0) #define RTL8127A_PTP_CLK_CFG 0xe410 #define RTL8127A_PTP_CLK_CFG_EXEC BIT(0) #define RTL8127A_PTP_CLK_CFG_MODE GENMASK(3, 1) #define RTL8127A_PHY_RST_CTL 0xa640 #define RTL8127A_PHY_RST BIT(15) -/* Time registers */ +#define RTL8127A_PTP_TRX_TS_STA 0xe430 +#define RTL8127A_PTP_TRX_TS_RD BIT(0) +#define RTL8127A_PTP_TRX_TS_SEL_RX BIT(1) +#define RTL8127A_PTP_TRX_TS_MSGTYPE GENMASK(3, 2) +#define RTL8127A_PTP_TRX_TS_EXEC BIT(4) +#define RTL8127A_PTP_TX_PDLYRSP_RDY BIT(12) +#define RTL8127A_PTP_TX_PDLYREQ_RDY BIT(13) +#define RTL8127A_PTP_TX_DLYREQ_RDY BIT(14) +#define RTL8127A_PTP_TX_SYNC_RDY BIT(15) +/* Time registers, CFG_TIME for the clock, TRX_TS_TIME for packets */ #define RTL8127A_PTP_CFG_TIME 0xe412 +#define RTL8127A_PTP_TRX_TS_TIME 0xe446 #define RTL8127A_PTP_NS_LO 0x0 #define RTL8127A_PTP_NS_HI 0x2 #define RTL8127A_PTP_S_LO 0x4 @@ -40,6 +57,9 @@ #define RTL8127A_PTP_RATE_HI_MASK GENMASK(21, 16) #define RTL8127A_PTP_MAX_ADJ 488281 +#define RTL8127A_PTP_TX_TIMEOUT (15 * HZ) +#define RTL8127A_PTP_TX_POLL_FAST msecs_to_jiffies(2) +#define RTL8127A_PTP_RX_QUEUE_MAX 64 /* RTL8127A_PTP_CLK_CFG clock adjust modes */ enum rtl8127a_ptp_clkadj_mode { @@ -50,11 +70,33 @@ enum rtl8127a_ptp_clkadj_mode { RTL8127A_PTP_RATE_WRITE = 14, }; +/* ptp->state bits */ +enum rtl8127a_ptp_state { + RTL8127A_PTP_TX_IN_PROGRESS, +}; + +struct rtl8127a_ptp_skb_cb { + u8 msgtype; +}; + +#define RTL8127A_PTP_SKB_CB(skb) \ + ((struct rtl8127a_ptp_skb_cb *)(skb)->cb) + struct rtl8127a_ptp { struct phy_device *phydev; + struct mii_timestamper mii_ts; struct ptp_clock_info info; struct ptp_clock *clock; struct mutex lock; /* serializes PTP register access sequences */ + enum hwtstamp_tx_types tx_type; + enum hwtstamp_rx_filters rx_filter; + bool rx_enabled; + bool hw_enabled; + struct sk_buff_head rx_queue; + struct sk_buff *tx_skb; + unsigned long tx_start; + u8 tx_msgtype; + unsigned long state; }; static int rtl8127a_ptp_read(struct rtl8127a_ptp *ptp, u16 reg) @@ -172,6 +214,98 @@ static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp, return 0; } +/* Latch the RX or TX timestamp of the given message type into the + * RTL8127A_PTP_TRX_TS_TIME registers. + */ +static int rtl8127a_ptp_trx_ts_cmd(struct rtl8127a_ptp *ptp, u16 cmd) +{ + int ret; + + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_TRX_TS_STA, + RTL8127A_PTP_TRX_TS_SEL_RX | + RTL8127A_PTP_TRX_TS_MSGTYPE, + RTL8127A_PTP_TRX_TS_RD | cmd); + if (ret) + return ret; + + return rtl8127a_ptp_poll(ptp, RTL8127A_PTP_TRX_TS_STA, + RTL8127A_PTP_TRX_TS_RD, false); +} + +static const u16 rtl8127a_ptp_tx_rdy[] = { + [PTP_MSGTYPE_SYNC] = RTL8127A_PTP_TX_SYNC_RDY, + [PTP_MSGTYPE_DELAY_REQ] = RTL8127A_PTP_TX_DLYREQ_RDY, + [PTP_MSGTYPE_PDELAY_REQ] = RTL8127A_PTP_TX_PDLYREQ_RDY, + [PTP_MSGTYPE_PDELAY_RESP] = RTL8127A_PTP_TX_PDLYRSP_RDY, +}; + +static int rtl8127a_ptp_egress_time(struct rtl8127a_ptp *ptp, u8 msgtype, + struct timespec64 *ts) +{ + int ret; + + ret = rtl8127a_ptp_poll(ptp, RTL8127A_PTP_TRX_TS_STA, + rtl8127a_ptp_tx_rdy[msgtype], true); + if (ret) + return ret; + + ret = rtl8127a_ptp_trx_ts_cmd(ptp, + FIELD_PREP(RTL8127A_PTP_TRX_TS_MSGTYPE, + msgtype) | + RTL8127A_PTP_TRX_TS_EXEC); + if (ret) + return ret; + + return rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_TRX_TS_TIME, ts); +} + +static int __rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, + bool enable) +{ + int ret; + + lockdep_assert_held(&ptp->lock); + + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL, + RTL8127A_PTP_SYNCE_EN, 0); + if (ret) + return ret; + + if (enable) { + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER, 0, + RTL8127A_PTP_INT_RX_TS | + RTL8127A_PTP_INT_TX_TS); + if (ret) + return ret; + + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_GEN_CFG, 0, + RTL8127A_PTP_INT_CLR_MODE); + if (ret) + return ret; + + ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, 0xffff); + if (ret) + return ret; + + ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CTL, + RTL8127A_PTP_CTL_TYPE_3 | BIT(12)); + } else { + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER, + RTL8127A_PTP_INT_RX_TS | + RTL8127A_PTP_INT_TX_TS, 0); + if (ret) + return ret; + + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CTL, + RTL8127A_PTP_CTL_EN, 0); + } + if (ret) + return ret; + + return rtl8127a_ptp_modify(ptp, RTL8127A_PHY_RST_CTL, 0, + RTL8127A_PHY_RST); +} + static int rtl8127a_ptp_hwtstamp_disable(struct rtl8127a_ptp *ptp) { int ret; @@ -325,6 +459,124 @@ static int rtl8127a_ptp_settime64(struct ptp_clock_info *info, return rtl8127a_ptp_settime(ptp, ts); } +static void rtl8127a_ptp_tx_complete(struct rtl8127a_ptp *ptp, + struct skb_shared_hwtstamps *hwts) +{ + struct sk_buff *skb = ptp->tx_skb; + + ptp->tx_skb = NULL; + clear_bit_unlock(RTL8127A_PTP_TX_IN_PROGRESS, &ptp->state); + + if (hwts) + skb_complete_tx_timestamp(skb, hwts); + else + kfree_skb(skb); +} + +/* Returns true if no TX timestamp is pending anymore */ +static bool rtl8127a_ptp_tx_tstamp(struct rtl8127a_ptp *ptp) +{ + struct skb_shared_hwtstamps hwts = {}; + struct timespec64 ts; + int ret; + + /* Pairs with smp_store_release() in rtl8127a_ptp_txtstamp() */ + if (!smp_load_acquire(&ptp->tx_skb)) + return true; + + mutex_lock(&ptp->lock); + + /* rtl8127a_ptp_hwtstamp_set() may have dropped it meanwhile */ + if (!ptp->tx_skb) { + mutex_unlock(&ptp->lock); + return true; + } + + /* Give up if TX timestamping was turned off or nothing arrived */ + if (READ_ONCE(ptp->tx_type) != HWTSTAMP_TX_ON || + time_is_before_jiffies(ptp->tx_start + RTL8127A_PTP_TX_TIMEOUT)) { + rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, + RTL8127A_PTP_INT_TX_TS); + + rtl8127a_ptp_tx_complete(ptp, NULL); + mutex_unlock(&ptp->lock); + return true; + } + + ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_INSR); + if (ret < 0 || !(ret & RTL8127A_PTP_INT_TX_TS)) { + mutex_unlock(&ptp->lock); + return false; + } + + ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, + RTL8127A_PTP_INT_TX_TS); + if (!ret) + ret = rtl8127a_ptp_egress_time(ptp, ptp->tx_msgtype, &ts); + + if (ret) { + rtl8127a_ptp_tx_complete(ptp, NULL); + } else { + hwts.hwtstamp = timespec64_to_ktime(ts); + rtl8127a_ptp_tx_complete(ptp, &hwts); + } + + mutex_unlock(&ptp->lock); + + return true; +} + +/* The hardware keeps only the latest timestamp per message type and + * direction and does not record the sequenceID. A message of the same + * type received before the latch is read replaces the timestamp. + */ +static void rtl8127a_ptp_rx_tstamp(struct rtl8127a_ptp *ptp, + struct sk_buff *skb) +{ + u8 msgtype = RTL8127A_PTP_SKB_CB(skb)->msgtype; + struct timespec64 ts; + int ret; + + mutex_lock(&ptp->lock); + ret = rtl8127a_ptp_trx_ts_cmd(ptp, RTL8127A_PTP_TRX_TS_SEL_RX | + FIELD_PREP(RTL8127A_PTP_TRX_TS_MSGTYPE, + msgtype) | + RTL8127A_PTP_TRX_TS_EXEC); + if (!ret) + ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_TRX_TS_TIME, + &ts); + mutex_unlock(&ptp->lock); + + if (!ret) + skb_hwtstamps(skb)->hwtstamp = timespec64_to_ktime(ts); + + netif_rx(skb); +} + +static long rtl8127a_ptp_do_aux_work(struct ptp_clock_info *info) +{ + struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info); + struct sk_buff_head received; + struct sk_buff *skb; + unsigned long flags; + + /* Only handle the RX frames queued so far, so TX is not starved */ + __skb_queue_head_init(&received); + spin_lock_irqsave(&ptp->rx_queue.lock, flags); + skb_queue_splice_tail_init(&ptp->rx_queue, &received); + spin_unlock_irqrestore(&ptp->rx_queue.lock, flags); + + while ((skb = __skb_dequeue(&received))) + rtl8127a_ptp_rx_tstamp(ptp, skb); + + if (rtl8127a_ptp_tx_tstamp(ptp)) + return -1; + + /* The TX timestamp is normally ready right away, back off after that */ + return time_before(jiffies, ptp->tx_start + RTL8127A_PTP_TX_POLL_FAST) ? + 0 : 1; +} + static const struct ptp_clock_info rtl8127a_ptp_info = { .owner = THIS_MODULE, .name = "RTL8127A PHC", @@ -333,8 +585,193 @@ static const struct ptp_clock_info rtl8127a_ptp_info = { .adjtime = rtl8127a_ptp_adjtime, .gettimex64 = rtl8127a_ptp_gettimex64, .settime64 = rtl8127a_ptp_settime64, + .do_aux_work = rtl8127a_ptp_do_aux_work, }; +static void rtl8127a_ptp_txtstamp(struct mii_timestamper *mii_ts, + struct sk_buff *skb, int type) +{ + struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp, + mii_ts); + struct ptp_header *hdr; + u8 msgtype; + + if (READ_ONCE(ptp->tx_type) != HWTSTAMP_TX_ON || + !(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) || + (type & PTP_CLASS_VMASK) != PTP_CLASS_V2 || + (type & PTP_CLASS_PMASK) == PTP_CLASS_L2) + goto drop; + + hdr = ptp_parse_header(skb, type); + if (!hdr) + goto drop; + + /* Only event messages are timestamped by the hardware */ + msgtype = ptp_get_msgtype(hdr, type); + if (msgtype > PTP_MSGTYPE_PDELAY_RESP) + goto drop; + + /* Only one TX timestamp can be pending at a time */ + if (test_and_set_bit_lock(RTL8127A_PTP_TX_IN_PROGRESS, &ptp->state)) + goto drop; + + skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; + ptp->tx_msgtype = msgtype; + ptp->tx_start = jiffies; + /* Publish tx_msgtype and tx_start before tx_skb to the aux worker */ + smp_store_release(&ptp->tx_skb, skb); + ptp_schedule_worker(ptp->clock, 0); + return; + +drop: + kfree_skb(skb); +} + +static bool rtl8127a_ptp_rxtstamp(struct mii_timestamper *mii_ts, + struct sk_buff *skb, int type) +{ + struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp, + mii_ts); + struct ptp_header *hdr; + u8 msgtype; + + if (!READ_ONCE(ptp->rx_enabled) || + (type & PTP_CLASS_VMASK) != PTP_CLASS_V2 || + (type & PTP_CLASS_PMASK) == PTP_CLASS_L2 || + skb_queue_len_lockless(&ptp->rx_queue) >= RTL8127A_PTP_RX_QUEUE_MAX) + return false; + + hdr = ptp_parse_header(skb, type); + if (!hdr) + return false; + + /* Only event messages are timestamped by the hardware */ + msgtype = ptp_get_msgtype(hdr, type); + if (msgtype > PTP_MSGTYPE_PDELAY_RESP) + return false; + + RTL8127A_PTP_SKB_CB(skb)->msgtype = msgtype; + skb_queue_tail(&ptp->rx_queue, skb); + ptp_schedule_worker(ptp->clock, 0); + + return true; +} + +static int rtl8127a_ptp_hwtstamp_get(struct mii_timestamper *mii_ts, + struct kernel_hwtstamp_config *config) +{ + struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp, + mii_ts); + + config->tx_type = ptp->tx_type; + config->rx_filter = ptp->rx_filter; + + return 0; +} + +static int rtl8127a_ptp_hwtstamp_set(struct mii_timestamper *mii_ts, + struct kernel_hwtstamp_config *config, + struct netlink_ext_ack *extack) +{ + struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp, + mii_ts); + bool enable = false; + bool rx_enabled; + int ret = 0; + + if (config->flags) + return -EINVAL; + + switch (config->tx_type) { + case HWTSTAMP_TX_ON: + enable = true; + break; + case HWTSTAMP_TX_OFF: + break; + default: + return -ERANGE; + } + + switch (config->rx_filter) { + case HWTSTAMP_FILTER_NONE: + rx_enabled = false; + break; + case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: + case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: + case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: + config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT; + rx_enabled = true; + enable = true; + break; + default: + return -ERANGE; + } + + /* The hardware is either on or off, only reprogram it on a change */ + mutex_lock(&ptp->lock); + /* Reprogram the PTP clock in case it was reset meanwhile */ + if (enable && ptp->hw_enabled) { + ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CTL); + if (ret < 0 || !(ret & RTL8127A_PTP_CTL_EN)) + ptp->hw_enabled = false; + ret = 0; + } + + if (enable != ptp->hw_enabled) { + /* Stop timestamping before turning the hardware off */ + if (!enable) { + WRITE_ONCE(ptp->tx_type, HWTSTAMP_TX_OFF); + WRITE_ONCE(ptp->rx_enabled, false); + } + + ret = __rtl8127a_ptp_hwtstamp_enable(ptp, enable); + if (ret) { + /* The hardware state is unknown, turn timestamping + * off and have the next request reprogram it. + */ + if (enable) + __rtl8127a_ptp_hwtstamp_enable(ptp, false); + ptp->hw_enabled = false; + ptp->rx_filter = HWTSTAMP_FILTER_NONE; + WRITE_ONCE(ptp->rx_enabled, false); + WRITE_ONCE(ptp->tx_type, HWTSTAMP_TX_OFF); + goto out; + } + + ptp->hw_enabled = enable; + } + + ptp->rx_filter = config->rx_filter; + WRITE_ONCE(ptp->rx_enabled, rx_enabled); + WRITE_ONCE(ptp->tx_type, config->tx_type); +out: + /* Drop a pending TX timestamp that is no longer wanted */ + if (ptp->tx_type != HWTSTAMP_TX_ON && READ_ONCE(ptp->tx_skb)) + rtl8127a_ptp_tx_complete(ptp, NULL); + mutex_unlock(&ptp->lock); + + return ret; +} + +static int rtl8127a_ptp_ts_info(struct mii_timestamper *mii_ts, + struct kernel_ethtool_ts_info *info) +{ + struct rtl8127a_ptp *ptp = container_of(mii_ts, struct rtl8127a_ptp, + mii_ts); + + info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE | + SOF_TIMESTAMPING_RX_HARDWARE | + SOF_TIMESTAMPING_RAW_HARDWARE; + info->phc_index = ptp_clock_index(ptp->clock); + info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON); + info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | + BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | + BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) | + BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ); + + return 0; +} + int rtl8127a_ptp_probe(struct phy_device *phydev) { struct device *dev = &phydev->mdio.dev; @@ -351,6 +788,9 @@ int rtl8127a_ptp_probe(struct phy_device *phydev) ptp->phydev = phydev; ptp->info = rtl8127a_ptp_info; + ptp->tx_type = HWTSTAMP_TX_OFF; + ptp->rx_filter = HWTSTAMP_FILTER_NONE; + skb_queue_head_init(&ptp->rx_queue); ret = rtl8127a_ptp_hwtstamp_disable(ptp); if (ret) @@ -364,7 +804,14 @@ int rtl8127a_ptp_probe(struct phy_device *phydev) if (IS_ERR(ptp->clock)) return PTR_ERR(ptp->clock); + ptp->mii_ts.rxtstamp = rtl8127a_ptp_rxtstamp; + ptp->mii_ts.txtstamp = rtl8127a_ptp_txtstamp; + ptp->mii_ts.hwtstamp_set = rtl8127a_ptp_hwtstamp_set; + ptp->mii_ts.hwtstamp_get = rtl8127a_ptp_hwtstamp_get; + ptp->mii_ts.ts_info = rtl8127a_ptp_ts_info; + phydev->priv = ptp; + phydev->mii_ts = &ptp->mii_ts; return 0; } @@ -376,7 +823,14 @@ void rtl8127a_ptp_remove(struct phy_device *phydev) if (!ptp) return; + phydev->mii_ts = NULL; + rtl8127a_ptp_hwtstamp_disable(ptp); + /* Also stops the aux worker */ ptp_clock_unregister(ptp->clock); + + if (ptp->tx_skb) + rtl8127a_ptp_tx_complete(ptp, NULL); + skb_queue_purge(&ptp->rx_queue); phydev->priv = NULL; } -- 2.43.0 ^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping for RTL8127a 2026-12-04 2:11 ` [PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping " javen @ 2026-12-05 2:59 ` netdev-bot+sashiko 2026-12-05 11:07 ` Nicolai Buchwitz 1 sibling, 0 replies; 12+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 2:59 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 12 potential issue(s) to consider. Critical: 0 · High: 0 · Medium: 8 · 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). - [Medium] The driver only notices that the PTP block was reset when user space calls rtl8127a_ptp_hwtstamp_set() again with an enabling config. - [Medium] rtl8127a_ptp_hwtstamp_set() starts with `if (config->flags) return -EINVAL;`. - [Medium] rtl8127a_ptp_tx_tstamp() treats the INSR TX_TS bit as proof that the pending skb has been stamped. - [Medium] The existing PHC ops wrap every MDIO access in rtl8127a_ptp_pm_get(). - [Medium] rtl8127a_ptp_remove() sets phydev->mii_ts = NULL and immediately tears the timestamper down:… - [Medium] rtl8127a_ptp_rx_tstamp() always issues SEL_RX|MSGTYPE|EXEC and reports the TRX_TS_TIME latch as the frame's hardware timestamp. - [Medium] net: phy: realtek: aux worker accesses disabled PTP block on RX off or remove - [Medium] net: phy: realtek: rtl8127a_ptp_remove() disables hw before clock unregister - [Low] rtl8127a_ptp_probe() installs phydev->mii_ts but never sets phydev->default_timestamp. - [Low] rtl8127a_ptp_tx_tstamp() pairs its smp_load_acquire(&ptp->tx_skb) with txtstamp's smp_store_release(). - [Low] The enable path overwrites the whole RTL8127A_PTP_CTL register with RTL8127A_PTP_CTL_TYPE_3 | BIT(12). - [Low] net: phy: realtek: rtl8127a_ptp_hwtstamp_disable() duplicates enable helper -- net: phy: realtek: add hardware timestamping for RTL8127a This patch registers a mii_timestamper for the RTL8127A internal PHY, so PTPv2 event messages over UDP get hardware timestamps in both directions. Timestamps are read over MDIO from the PTP aux worker. RX skbs are queued until their timestamp is 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 16ba3fe9697a0..9f82466c2b23c 100644 > --- a/drivers/net/phy/realtek/realtek_ptp.c > +++ b/drivers/net/phy/realtek/realtek_ptp.c [ ... ] > @@ -172,6 +214,98 @@ static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp, [ ... ] > +static int __rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, > + bool enable) > +{ [Severity: Low] This isn't a bug, but the enable == false branch of __rtl8127a_ptp_hwtstamp_enable() runs the same register sequence as rtl8127a_ptp_hwtstamp_disable(): - clear SYNCE_EN in SYNCE_CTL - mask RX_TS and TX_TS in INER - clear CTL_EN in CTL - set PHY_RST in PHY_RST_CTL Could rtl8127a_ptp_hwtstamp_disable() take ptp->lock and call __rtl8127a_ptp_hwtstamp_enable(ptp, false) instead? Probe, remove and rtl8127a_ptp_hwtstamp_set() would then share one disable sequence, so a later fix to it can't end up in only one of the two copies. [ ... ] > + ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, 0xffff); > + if (ret) > + return ret; > + > + ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CTL, > + RTL8127A_PTP_CTL_TYPE_3 | BIT(12)); > + } else { > + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER, > + RTL8127A_PTP_INT_RX_TS | > + RTL8127A_PTP_INT_TX_TS, 0); > + if (ret) > + return ret; > + > + ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CTL, > + RTL8127A_PTP_CTL_EN, 0); > + } [Severity: Low] This isn't a bug, but the enable path overwrites the whole RTL8127A_PTP_CTL register with RTL8127A_PTP_CTL_TYPE_3 | BIT(12). RTL8127A_PTP_CTL_TYPE_3 is 0x003f. It quietly includes RTL8127A_PTP_CTL_EN plus bits 1-5, and BIT(12) has no name or comment. The disable path only clears RTL8127A_PTP_CTL_EN. Bits 1-5 and 12 stay set, so an off/on toggle never returns the register to a known state. Could these bits get named defines, with a note on which encapsulations they select? Without that, the software filters in rtl8127a_ptp_rxtstamp() and rtl8127a_ptp_txtstamp() can't be checked against what the hardware actually stamps. [ ... ] > @@ -325,6 +459,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) { > + mutex_unlock(&ptp->lock); > + return true; > + } [Severity: Low] The smp_load_acquire() above pairs with the smp_store_release() in rtl8127a_ptp_txtstamp(). Does that ordering still hold for this second, plain read of ptp->tx_skb under the mutex? Take this sequence while the worker is blocked on ptp->lock: worker: smp_load_acquire() sees skb A hwtstamp_set(TX OFF) drops A hwtstamp_set(TX ON) rtl8127a_ptp_txtstamp() writes tx_msgtype and tx_start, then smp_store_release(&ptp->tx_skb, B) worker: mutex_lock() pairs with hwtstamp_set()'s unlock The worker's mutex acquire then orders against hwtstamp_set(), not against rtl8127a_ptp_txtstamp(). On weakly ordered CPUs, could the plain reads return B for tx_skb but A's tx_msgtype and tx_start? That would latch the wrong message type, or time B out immediately. Would using smp_load_acquire() for this re-check close the window? rtl8127a_ptp_do_aux_work() has a similar unlocked plain read of tx_start after this function returns. It races with the plain write in rtl8127a_ptp_txtstamp(): return time_before(jiffies, ptp->tx_start + RTL8127A_PTP_TX_POLL_FAST) ? 0 : 1; KCSAN would likely flag both. > + > + /* 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] The existing PHC ops wrap their MDIO accesses in rtl8127a_ptp_pm_get(), whose comment says: /* The PHY is accessed through the MAC, which may be runtime suspended * while there is no link. Don't resume it for the PHC, fail instead. Does the aux worker need the same guard, both here and in rtl8127a_ptp_rx_tstamp()? This function polls INSR for up to RTL8127A_PTP_TX_TIMEOUT (15 s). r8169 schedules a runtime suspend 10 s after carrier loss: rtl8169_runtime_idle() if (!netif_running(tp->dev) || !netif_carrier_ok(tp->dev)) pm_schedule_suspend(device, 10000); If the cable is pulled while a TX skb is pending, the worker keeps doing MDIO accesses after the MAC has entered D3hot. In that state r8168_phy_ocp_read() can return 0xffff as valid data, because the all-ones GPHY_OCP read satisfies the flag wait: return rtl_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ? (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT; An INSR value of 0xffff looks like RTL8127A_PTP_INT_TX_TS is set, and the RDY poll in rtl8127a_ptp_egress_time() passes at once. The following writes are lost silently because r8168_phy_ocp_write() returns void. The RD-clear poll then times out, and every one of these MMIO accesses goes to a device in D3hot. > + > + 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); > + } [Severity: Medium] Can the RTL8127A_PTP_INT_TX_TS bit checked above belong to a different frame than ptp->tx_skb? That INSR bit is shared by all four event message types. Event frames the driver did not track also set it, for example: - frames whose clone rtl8127a_ptp_txtstamp() dropped because RTL8127A_PTP_TX_IN_PROGRESS was already set, e.g. after ptp4l's own tx_timestamp_timeout fired - frames sent without SKBTX_HW_TSTAMP - frames sent while tx_type was OFF but the block stayed on for RX. Switching TX from OFF to ON while hw_enabled is already true does not clear INSR. INSR is only cleared at enable time and here, and the worker returns early while tx_skb is NULL. So a stale TX_TS bit can be waiting when the next tracked skb arrives. rtl8127a_ptp_egress_time() then polls the per-type RDY bit through rtl8127a_ptp_poll(), which waits only 1 ms: return phy_read_mmd_poll_timeout(ptp->phydev, MDIO_MMD_VEND2, reg, val, !!(val & mask) == set, 1, 1000, false); If the tracked frame has not left within that 1 ms, the -ETIMEDOUT drops the skb here. The function returns true, rtl8127a_ptp_do_aux_work() stops polling, and the 15 s timeout is never reached. If the RDY bit for that type is instead still set from an earlier untracked frame, would the older frame's timestamp be latched and passed to skb_complete_tx_timestamp() as a valid 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); > +} [Severity: Medium] Can rtl8127a_ptp_rx_tstamp() latch a timestamp from a PTP block that has already been turned off? rtl8127a_ptp_tx_tstamp() re-checks ptp->tx_type under ptp->lock before it touches any register. This path has no such check. Once it holds ptp->lock, it always issues the SEL_RX latch through rtl8127a_ptp_trx_ts_cmd() and reads RTL8127A_PTP_TRX_TS_TIME. Take hwtstamp_set() turning everything off while event frames are queued: CPU1 (aux worker) CPU2 (hwtstamp_set, all off) rtl8127a_ptp_do_aux_work() splice rx_queue mutex_lock(&ptp->lock) rx_enabled = false __rtl8127a_ptp_hwtstamp_enable(false) mutex_unlock(&ptp->lock) rtl8127a_ptp_rx_tstamp() rtl8127a_ptp_trx_ts_cmd() rtl8127a_ptp_read_time() At that point the block has CTL_EN cleared and PHY_RST set. Two things can happen: - The RD-clear poll in rtl8127a_ptp_trx_ts_cmd() times out, and phy_read_mmd_poll_timeout() logs it through phydev_err(). - The poll completes, and whatever the reset block holds in the latch is stored in skb_hwtstamps() and passed to netif_rx(). That happens right after user space asked for RX timestamping to stop. hwtstamp_set() cannot reach the skbs that are already spliced into the local received list. So I think the check has to happen per skb, under ptp->lock. Would it be enough to test ptp->hw_enabled (or rx_enabled) there, and deliver the skb without a timestamp when it is off? rtl8127a_ptp_remove() has the same window. It disables the block with rtl8127a_ptp_hwtstamp_disable() before ptp_clock_unregister() stops the worker, and it leaves hw_enabled, tx_type and rx_enabled set. In that window both rtl8127a_ptp_rx_tstamp() and rtl8127a_ptp_tx_tstamp() run against the disabled block, and the tx_type re-check does not help. [Severity: Medium] Does anything here confirm that the hardware captured a new RX timestamp for this frame? The TX path at least checks INSR and a per-type RDY bit. This path always issues SEL_RX | MSGTYPE | EXEC and reports whatever is in RTL8127A_PTP_TRX_TS_TIME. __rtl8127a_ptp_hwtstamp_enable() enables RTL8127A_PTP_INT_RX_TS in INER, but the RX side never reads or clears it. Only the 0xffff write at enable time clears it, so it stays set after the first RX event. rtl8127a_ptp_rxtstamp() accepts any non-L2 PTPv2 class, including IPv4, IPv6 and VLAN variants. The hardware is programmed with the undocumented RTL8127A_PTP_CTL_TYPE_3 | BIT(12), so it is not clear that the PHY stamps all of them. For a frame the PHY did not stamp, or one received after the PTP block was reset, would this pass the previous latch contents (or zero) to netif_rx() as a valid hardware timestamp? The commit message describes a timestamp being overwritten by a later message of the same type, but not this case. [ ... ] > @@ -333,8 +585,193 @@ static const struct ptp_clock_info rtl8127a_ptp_info = { [ ... ] > +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; [Severity: Medium] Does rejecting every flag here break timestamping through a bond? net_hwtstamp_validate() only rejects flags outside HWTSTAMP_FLAG_MASK, so HWTSTAMP_FLAG_BONDED_PHC_INDEX is a valid flag. bond_hwtstamp_set() requires it and passes the same cfg down to the active slave: bond_hwtstamp_set() if (!(cfg->flags & HWTSTAMP_FLAG_BONDED_PHC_INDEX)) return -EOPNOTSUPP; ... generic_hwtstamp_set_lower(real_dev, cfg, extack) -> dev_set_hwtstamp_phylib() -> phy_hwtstamp_set() -> mii_ts->hwtstamp_set() That call then fails with -EINVAL here, so hardware timestamping on an RTL8127A PHY can never be enabled through a bond. Could this check either be dropped, since the core already validates the mask, or reject only flags & ~HWTSTAMP_FLAG_MASK? [ ... ] > + /* The hardware is either on or off, only reprogram it on a change */ > + mutex_lock(&ptp->lock); > + /* Reprogram the PTP clock in case it was reset meanwhile */ > + if (enable && ptp->hw_enabled) { > + ret = rtl8127a_ptp_read(ptp, RTL8127A_PTP_CTL); > + if (ret < 0 || !(ret & RTL8127A_PTP_CTL_EN)) > + ptp->hw_enabled = false; > + ret = 0; > + } [Severity: Medium] Is a new hwtstamp_set() call from user space the only point where a reset PTP block gets noticed? v6 dropped the config_init() and link_change_notify() restore paths. r8169 re-initializes the internal PHY on every open and on every runtime or system resume: rtl8169_up() rtl8169_init_phy() phy_init_hw() phy_resume() r8169_hw_phy_config() ... genphy_soft_reset() After such a reset, ptp->hw_enabled, tx_type and rx_enabled still say timestamping is on, and rtl8127a_ptp_hwtstamp_get() reports that cached state. rtl8127a_ptp_txtstamp() keeps taking TX clones. INSR never reports TX_TS, so each clone is held for 15 s and then dropped. rtl8127a_ptp_rxtstamp() keeps diverting RX event frames and stamping them with stale latch contents. ptp4l recovers, because it re-issues SIOCSHWTSTAMP after a link fault. Consumers that configure timestamping only once, such as chrony, would not. The commit message doesn't mention this. Also, the comment says "Reprogram the PTP clock", but the recovery path through __rtl8127a_ptp_hwtstamp_enable() only rewrites SYNCE_CTL, INER, GEN_CFG, INSR, CTL and PHY_RST_CTL. It does not restore the clock rate or time. Is the comment accurate? [ ... ] > @@ -364,7 +804,14 @@ int rtl8127a_ptp_probe(struct phy_device *phydev) > if (IS_ERR(ptp->clock)) > return PTR_ERR(ptp->clock); > > + ptp->mii_ts.rxtstamp = rtl8127a_ptp_rxtstamp; > + ptp->mii_ts.txtstamp = rtl8127a_ptp_txtstamp; > + ptp->mii_ts.hwtstamp_set = rtl8127a_ptp_hwtstamp_set; > + ptp->mii_ts.hwtstamp_get = rtl8127a_ptp_hwtstamp_get; > + ptp->mii_ts.ts_info = rtl8127a_ptp_ts_info; > + > phydev->priv = ptp; > + phydev->mii_ts = &ptp->mii_ts; [Severity: Low] This isn't a bug, but phydev->default_timestamp is never set here. The other PHY timestamping drivers (bcm-phy-ptp, dp83640, micrel, microchip_rds_ptp, mscc_ptp and nxp-c45-tja11xx) all set it. With dev->hwprov unset, phy_is_default_hwtstamp() returns false. dev_set_hwtstamp_phylib() then falls through to r8169, which has no ndo_hwtstamp_set, and returns -EOPNOTSUPP. skb_clone_tx_timestamp() and skb_defer_rx_timestamp() also skip the PHY: } else { phydev = skb->dev->phydev; if (!phy_is_default_hwtstamp(phydev)) { rcu_read_unlock(); return false; } } The earlier series commit "net: ethtool: tsconfig: allow selecting a provider" says new PHY drivers are expected to rely on dev->hwprov rather than default_timestamp, so this looks intentional. Could the commit message or the Kconfig help say that the PHY first has to be selected as the hwtstamp provider through ethtool tsconfig? The new help text only says: and timestamps PTPv2 event messages over UDP on behalf of the MAC, so no support from the MAC driver is needed. > > return 0; > } > @@ -376,7 +823,14 @@ void rtl8127a_ptp_remove(struct phy_device *phydev) > if (!ptp) > return; > > + phydev->mii_ts = NULL; > + > rtl8127a_ptp_hwtstamp_disable(ptp); > + /* Also stops the aux worker */ > ptp_clock_unregister(ptp->clock); > + > + if (ptp->tx_skb) > + rtl8127a_ptp_tx_complete(ptp, NULL); > + skb_queue_purge(&ptp->rx_queue); > phydev->priv = NULL; > } [Severity: Medium] Should ptp_clock_unregister() be called before rtl8127a_ptp_hwtstamp_disable() here? Probe uses the opposite order: it disables the block first and only then registers the clock. Here the block is disabled and PHY_RST is set while /dev/ptpN is still registered and the aux worker may still be queued or running. Until ptp_clock_unregister() cancels ptp->aux_work, rtl8127a_ptp_do_aux_work() can still process queued RX skbs and a pending TX skb. It issues latch commands and MDIO polls against a block that has just been reset. That shows up as poll timeouts logged through phydev_err(), or as timestamps read from the reset block and handed to netif_rx() during teardown. The posix clock ops can also still be called from user space in that window. Unregistering the clock first would stop the worker and the clock ops before the hardware is touched. The block could then be disabled, and tx_skb and rx_queue cleaned up as before. Is there a reason the disable has to come first? [Severity: Medium] Can rtl8127a_ptp_txtstamp() or rtl8127a_ptp_rxtstamp() still be running when this teardown happens? skb_clone_tx_timestamp() and skb_defer_rx_timestamp() load phydev->mii_ts and call into the driver with no synchronization against remove. The synchronize_net() that covered this window was dropped in v5. ptp is allocated with devm_kzalloc() and is freed right after remove() returns. For example, if root unbinds the PHY driver through sysfs while the netdev is up with the PHY selected as provider: CPU1 (xmit) CPU2 (remove) rtl8127a_ptp_txtstamp() phydev->mii_ts = NULL ptp_clock_unregister() if (ptp->tx_skb) /* NULL */ test_and_set_bit_lock() smp_store_release(&ptp->tx_skb, skb) ptp_schedule_worker(ptp->clock, 0) The clone holds a socket reference from skb_clone_sk(), so it leaks. ptp_schedule_worker() then runs against a destroyed kworker and a released ptp_clock. rtl8127a_ptp_rxtstamp(), called from NAPI, has the same problem. Its skb_queue_tail() can run after the skb_queue_purge() here, and it also schedules the destroyed worker. rtl8127a_ptp_hwtstamp_set() runs under RTNL, which remove() does not take. Could it re-enable the block after rtl8127a_ptp_hwtstamp_disable(), or take ptp->lock after devm has freed it? -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204021116.66-1-javen_xu%40realsil.com.cn ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping for RTL8127a 2026-12-04 2:11 ` [PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping " javen 2026-12-05 2:59 ` netdev-bot+sashiko @ 2026-12-05 11:07 ` Nicolai Buchwitz 1 sibling, 0 replies; 12+ messages in thread From: Nicolai Buchwitz @ 2026-12-05 11:07 UTC (permalink / raw) To: javen Cc: andrew, hkallweit1, linux, davem, edumazet, kuba, pabeni, maxime.chevallier, netdev, linux-kernel, daniel, vladimir.oltean Hi Javen On 4.12.2026 03:11, javen wrote: > [...] > +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 */ > + mutex_lock(&ptp->lock); IMHO this needs the runtime PM check too. The MAC may be suspended, e.g. when ptp4l starts before the cable is plugged in. > [...] Thanks, Nicolai ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-12-05 11:19 UTC | newest] Thread overview: 12+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-04 2:11 [PATCH net-next v6 0/4] net: phy: realtek: add PTP support for RTL8127a javen 2026-12-04 2:11 ` [PATCH net-next v6 1/4] net: ethtool: tsconfig: allow selecting a provider javen 2026-12-04 2:11 ` [PATCH net-next v6 2/4] r8169: hold MDIO bus lock while loading firmware javen 2026-12-05 2:59 ` netdev-bot+sashiko 2026-12-04 2:11 ` [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a javen 2026-12-04 19:08 ` Andrew Lunn 2026-12-04 19:15 ` Andrew Lunn 2026-12-05 2:59 ` netdev-bot+sashiko 2026-12-05 11:19 ` Nicolai Buchwitz 2026-12-04 2:11 ` [PATCH net-next v6 4/4] net: phy: realtek: add hardware timestamping " javen 2026-12-05 2:59 ` netdev-bot+sashiko 2026-12-05 11:07 ` Nicolai Buchwitz
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox