Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by javen
Wed, 11 Nov 2026 08:38
[PATCH net-next v2 1/2] net: phy: realtek: add PTP clock for RTL8127a
Author: javen
Date: Wed, 11 Nov 2026 08:38
Date: Wed, 11 Nov 2026 08:38
500 lines
23180 bytes
23180 bytes
From: Javen Xu <javen_xu@realsil.com.cn>
The internal PHY of RTL8127a (RTL8261C, PHY ID 0x001cc890) contains a
PTP block with a free running clock that can be read, set, stepped and
frequency adjusted through vendor MMD registers.
Register a PTP hardware clock for it. All register accesses go through
the normal blocking MDIO path, multi register sequences are serialized
by a driver mutex. The clock is started from the system time each time
the PHY is initialized.
Signed-off-by: Javen Xu <javen_xu@realsil.com.cn>
---
drivers/net/phy/realtek/Kconfig | 11 +
drivers/net/phy/realtek/Makefile | 1 +
drivers/net/phy/realtek/realtek.h | 20 ++
drivers/net/phy/realtek/realtek_main.c | 19 +-
drivers/net/phy/realtek/realtek_ptp.c | 374 +++++++++++++++++++++++++
5 files changed, 424 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..93a8fb552834 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
+ depends on !(REALTEK_PHY=y && PTP_1588_CLOCK=m)
+ help
+ Optional PTP hardware clock support for the internal PHY of
+ RTL8127A. The clock is provided by the PHY itself and can be
+ read and adjusted through the PTP clock device, no support from
+ the MAC driver is needed.
+
endif # REALTEK_PHY
diff --git a/drivers/net/phy/realtek/Makefile b/drivers/net/phy/realtek/Makefile
index dd21cf87f2f1..450bd05b7ef9 100644
--- a/drivers/net/phy/realtek/Makefile
+++ b/drivers/net/phy/realtek/Makefile
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
realtek-y += realtek_main.o
realtek-$(CONFIG_REALTEK_PHY_HWMON) += realtek_hwmon.o
+realtek-$(CONFIG_REALTEK_PHY_PTP) += realtek_ptp.o
obj-$(CONFIG_REALTEK_PHY) += realtek.o
diff --git a/drivers/net/phy/realtek/realtek.h b/drivers/net/phy/realtek/realtek.h
index a39b44fa18a0..384bde5fff3b 100644
--- a/drivers/net/phy/realtek/realtek.h
+++ b/drivers/net/phy/realtek/realtek.h
@@ -7,4 +7,24 @@
int rtl822x_hwmon_init(struct phy_device *phydev);
+#if IS_ENABLED(CONFIG_REALTEK_PHY_PTP)
+int rtl8127a_ptp_probe(struct phy_device *phydev);
+void rtl8127a_ptp_remove(struct phy_device *phydev);
+int rtl8127a_ptp_config_init(struct phy_device *phydev);
+#else
+static inline int rtl8127a_ptp_probe(struct phy_device *phydev)
+{
+ return 0;
+}
+
+static inline void rtl8127a_ptp_remove(struct phy_device *phydev)
+{
+}
+
+static inline int rtl8127a_ptp_config_init(struct phy_device *phydev)
+{
+ return 0;
+}
+#endif
+
#endif /* REALTEK_H */
diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index 4927ee838de2..433fcce8615b 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -1839,6 +1839,21 @@ static int rtl822x_probe(struct phy_device *phydev)
return 0;
}
+static int rtl_internal_nbaset_probe(struct phy_device *phydev)
+{
+ int ret;
+
+ ret = rtl822x_probe(phydev);
+ if (ret)
+ return ret;
+
+ /* Only the internal PHY of RTL8127A has a PTP block */
+ if (phydev->phy_id == RTL_8261C)
+ return rtl8127a_ptp_probe(phydev);
+
+ return 0;
+}
+
/* RTL822x cannot access MDIO_MMD_VEND2 via MII_MMD_CTRL/MII_MMD_DATA.
* A mapping to use paged access needs to be used instead.
* All other MMD devices can be accessed as usual.
@@ -3413,7 +3428,9 @@ static struct phy_driver realtek_drvs[] = {
.match_phy_device = rtl_internal_nbaset_match_phy_device,
.name = "Realtek Internal NBASE-T PHY",
.flags = PHY_IS_INTERNAL,
- .probe = rtl822x_probe,
+ .probe = rtl_internal_nbaset_probe,
+ .remove = rtl8127a_ptp_remove,
+ .config_init = rtl8127a_ptp_config_init,
.get_features = rtl822x_get_features,
.config_aneg = rtl822x_config_aneg,
.read_status = rtl822x_read_status,
diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
new file mode 100644
index 000000000000..bd5c0d6988da
--- /dev/null
+++ b/drivers/net/phy/realtek/realtek_ptp.c
@@ -0,0 +1,374 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * PTP hardware clock support for the internal PHY of RTL8127A
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/iopoll.h>
+#include <linux/math64.h>
+#include <linux/netdevice.h>
+#include <linux/phy.h>
+#include <linux/ptp_clock_kernel.h>
+#include <linux/timekeeping.h>
+
+#include "realtek.h"
+
+#define RTL8127A_PTP_CTL 0xe400
+#define RTL8127A_PTP_CTL_EN BIT(0)
+#define RTL8127A_PTP_CTL_TYPE_3 0x003f
+#define RTL8127A_PTP_INER 0xe402
+#define RTL8127A_PTP_INSR 0xe404
+#define RTL8127A_PTP_INT_RX_TS BIT(2)
+#define RTL8127A_PTP_INT_TX_TS BIT(3)
+#define RTL8127A_PTP_SYNCE_CTL 0xe406
+#define RTL8127A_PTP_SYNCE_EN BIT(0)
+#define RTL8127A_PTP_GEN_CFG 0xe408
+#define RTL8127A_PTP_INT_CLR_MODE BIT(0)
+#define RTL8127A_PTP_CLK_CFG 0xe410
+#define RTL8127A_PTP_CLK_CFG_EXEC BIT(0)
+#define RTL8127A_PTP_CLK_CFG_MODE GENMASK(3, 1)
+/* Time registers */
+#define RTL8127A_PTP_CFG_TIME 0xe412
+#define RTL8127A_PTP_NS_LO 0x0
+#define RTL8127A_PTP_NS_HI 0x2
+#define RTL8127A_PTP_S_LO 0x4
+#define RTL8127A_PTP_S_MI 0x6
+#define RTL8127A_PTP_S_HI 0x8
+#define RTL8127A_PTP_NS_HI_MASK GENMASK(13, 0)
+#define RTL8127A_PTP_NSEC_MASK GENMASK(29, 0)
+#define RTL8127A_PTP_SEC_MASK GENMASK_ULL(47, 0)
+#define RTL8127A_PTP_RATE_HI_MASK GENMASK(21, 16)
+
+#define RTL8127A_PTP_MAX_ADJ 488281
+
+/* RTL8127A_PTP_CLK_CFG clock adjust modes */
+enum rtl8127a_ptp_clkadj_mode {
+ RTL8127A_PTP_DIRECT_READ = 4,
+ RTL8127A_PTP_DIRECT_WRITE = 6,
+ RTL8127A_PTP_INC_STEP = 8,
+ RTL8127A_PTP_DEC_STEP = 10,
+ RTL8127A_PTP_RATE_WRITE = 14,
+};
+
+struct rtl8127a_ptp {
+ struct phy_device *phydev;
+ struct ptp_clock_info info;
+ struct ptp_clock *clock;
+ struct mutex lock; /* serializes PTP register access sequences */
+};
+
+static int rtl8127a_ptp_read(struct rtl8127a_ptp *ptp, u16 reg)
+{
+ return phy_read_mmd(ptp->phydev, MDIO_MMD_VEND2, reg);
+}
+
+static int rtl8127a_ptp_write(struct rtl8127a_ptp *ptp, u16 reg, u16 val)
+{
+ return phy_write_mmd(ptp->phydev, MDIO_MMD_VEND2, reg, val);
+}
+
+/* Unlike phy_modify_mmd() always write the register back, some of the
+ * bits are commands to the PTP block.
+ */
+static int rtl8127a_ptp_modify(struct rtl8127a_ptp *ptp, u16 reg, u16 mask,
+ u16 set)
+{
+ int val = rtl8127a_ptp_read(ptp, reg);
+
+ if (val < 0)
+ return val;
+
+ return rtl8127a_ptp_write(ptp, reg, (val & ~mask) | set);
+}
+
+/* Poll until (reg & mask) is non-zero (set == true) or zero (set == false).
+ * Returns the last register value read, or a negative error code.
+ */
+static int rtl8127a_ptp_poll(struct rtl8127a_ptp *ptp, u16 reg, u16 mask,
+ bool set)
+{
+ int ret, val;
+
+ ret = read_poll_timeout(rtl8127a_ptp_read, val,
+ val < 0 || !!(val & mask) == set, 1, 1000,
+ false, ptp, reg);
+ if (val < 0)
+ ret = val;
+ if (ret) {
+ if (net_ratelimit())
+ phydev_err(ptp->phydev,
+ "PTP reg 0x%04x poll failed: %d\n", reg, ret);
+ return ret;
+ }
+
+ return val;
+}
+
+static int rtl8127a_ptp_clkadj(struct rtl8127a_ptp *ptp,
+ enum rtl8127a_ptp_clkadj_mode mode)
+{
+ int ret;
+
+ ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CLK_CFG,
+ RTL8127A_PTP_CLK_CFG_MODE,
+ RTL8127A_PTP_CLK_CFG_EXEC | mode);
+ if (ret)
+ return ret;
+
+ ret = rtl8127a_ptp_poll(ptp, RTL8127A_PTP_CLK_CFG,
+ RTL8127A_PTP_CLK_CFG_EXEC, false);
+
+ return ret < 0 ? ret : 0;
+}
+
+static int rtl8127a_ptp_read_time(struct rtl8127a_ptp *ptp, u16 base,
+ struct timespec64 *ts)
+{
+ static const u16 regs[] = {
+ RTL8127A_PTP_NS_HI, RTL8127A_PTP_NS_LO,
+ RTL8127A_PTP_S_HI, RTL8127A_PTP_S_MI, RTL8127A_PTP_S_LO,
+ };
+ int val[ARRAY_SIZE(regs)];
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(regs); i++) {
+ val[i] = rtl8127a_ptp_read(ptp, base + regs[i]);
+ if (val[i] < 0)
+ return val[i];
+ }
+
+ ts->tv_nsec = (val[0] & RTL8127A_PTP_NS_HI_MASK) << 16 | val[1];
+ ts->tv_sec = (u64)val[2] << 32 | (u64)val[3] << 16 | val[4];
+
+ return 0;
+}
+
+static int rtl8127a_ptp_write_time(struct rtl8127a_ptp *ptp,
+ const struct timespec64 *ts)
+{
+ u64 sec = ts->tv_sec & RTL8127A_PTP_SEC_MASK;
+ u32 nsec = ts->tv_nsec & RTL8127A_PTP_NSEC_MASK;
+ const struct {
+ u16 reg;
+ u16 val;
+ } regs[] = {
+ { RTL8127A_PTP_NS_LO, nsec },
+ { RTL8127A_PTP_NS_HI, nsec >> 16 },
+ { RTL8127A_PTP_S_LO, sec },
+ { RTL8127A_PTP_S_MI, sec >> 16 },
+ { RTL8127A_PTP_S_HI, sec >> 32 },
+ };
+ int i, ret;
+
+ for (i = 0; i < ARRAY_SIZE(regs); i++) {
+ ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CFG_TIME + regs[i].reg,
+ regs[i].val);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
+{
+ int ret;
+
+ mutex_lock(&ptp->lock);
+
+ ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_SYNCE_CTL,
+ RTL8127A_PTP_SYNCE_EN, 0);
+ if (ret)
+ goto out;
+
+ if (enable) {
+ ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER, 0,
+ RTL8127A_PTP_INT_RX_TS |
+ RTL8127A_PTP_INT_TX_TS);
+ if (ret)
+ goto out;
+
+ ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_GEN_CFG, 0,
+ RTL8127A_PTP_INT_CLR_MODE);
+ if (ret)
+ goto out;
+
+ ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_INSR, 0xffff);
+ if (ret)
+ goto out;
+
+ ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CTL,
+ RTL8127A_PTP_CTL_TYPE_3 | BIT(12));
+ } else {
+ ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_INER,
+ RTL8127A_PTP_INT_RX_TS |
+ RTL8127A_PTP_INT_TX_TS, 0);
+ if (ret)
+ goto out;
+
+ ret = rtl8127a_ptp_modify(ptp, RTL8127A_PTP_CTL,
+ RTL8127A_PTP_CTL_EN, 0);
+ }
+ if (ret)
+ goto out;
+
+ ret = rtl8127a_ptp_modify(ptp, 0xa640, 0, BIT(15));
+out:
+ mutex_unlock(&ptp->lock);
+
+ return ret;
+}
+
+static int rtl8127a_ptp_settime(struct rtl8127a_ptp *ptp,
+ const struct timespec64 *ts)
+{
+ int ret;
+
+ mutex_lock(&ptp->lock);
+ ret = rtl8127a_ptp_write_time(ptp, ts);
+ if (!ret)
+ ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_DIRECT_WRITE);
+ mutex_unlock(&ptp->lock);
+
+ return ret;
+}
+
+static int rtl8127a_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
+{
+ struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+ s32 ppb = scaled_ppm_to_ppb(scaled_ppm);
+ u32 rate;
+ int ret;
+
+ rate = div_u64((u64)abs(ppb) << 32, NSEC_PER_SEC);
+ if (ppb < 0)
+ rate = -rate;
+
+ mutex_lock(&ptp->lock);
+ ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CFG_TIME + RTL8127A_PTP_NS_LO,
+ rate);
+ if (ret)
+ goto out;
+
+ ret = rtl8127a_ptp_write(ptp, RTL8127A_PTP_CFG_TIME + RTL8127A_PTP_NS_HI,
+ FIELD_GET(RTL8127A_PTP_RATE_HI_MASK, rate));
+ if (ret)
+ goto out;
+
+ ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_RATE_WRITE);
+out:
+ mutex_unlock(&ptp->lock);
+
+ return ret;
+}
+
+static int rtl8127a_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
+{
+ struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+ struct timespec64 d = ns_to_timespec64(abs(delta));
+ int ret;
+
+ mutex_lock(&ptp->lock);
+ ret = rtl8127a_ptp_write_time(ptp, &d);
+ if (!ret)
+ ret = rtl8127a_ptp_clkadj(ptp, delta < 0 ?
+ RTL8127A_PTP_DEC_STEP :
+ RTL8127A_PTP_INC_STEP);
+ mutex_unlock(&ptp->lock);
+
+ return ret;
+}
+
+static int rtl8127a_ptp_gettimex64(struct ptp_clock_info *info,
+ struct timespec64 *ts,
+ struct ptp_system_timestamp *sts)
+{
+ struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+ int ret;
+
+ mutex_lock(&ptp->lock);
+ ptp_read_system_prets(sts);
+ ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_DIRECT_READ);
+ ptp_read_system_postts(sts);
+ if (!ret)
+ ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_CFG_TIME, ts);
+ mutex_unlock(&ptp->lock);
+
+ return ret;
+}
+
+static int rtl8127a_ptp_settime64(struct ptp_clock_info *info,
+ const struct timespec64 *ts)
+{
+ struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
+
+ return rtl8127a_ptp_settime(ptp, ts);
+}
+
+static const struct ptp_clock_info rtl8127a_ptp_info = {
+ .owner = THIS_MODULE,
+ .name = "RTL8127A PHC",
+ .max_adj = RTL8127A_PTP_MAX_ADJ,
+ .adjfine = rtl8127a_ptp_adjfine,
+ .adjtime = rtl8127a_ptp_adjtime,
+ .gettimex64 = rtl8127a_ptp_gettimex64,
+ .settime64 = rtl8127a_ptp_settime64,
+};
+
+int rtl8127a_ptp_config_init(struct phy_device *phydev)
+{
+ struct rtl8127a_ptp *ptp = phydev->priv;
+ struct timespec64 ts;
+
+ if (!ptp)
+ return 0;
+
+ /* (Re)start the clock from the system time whenever the PHY is
+ * (re)initialized, e.g. each time the MAC driver brings it up.
+ */
+ ktime_get_real_ts64(&ts);
+
+ return rtl8127a_ptp_settime(ptp, &ts);
+}
+
+int rtl8127a_ptp_probe(struct phy_device *phydev)
+{
+ struct device *dev = &phydev->mdio.dev;
+ struct rtl8127a_ptp *ptp;
+ int ret;
+
+ ptp = devm_kzalloc(dev, sizeof(*ptp), GFP_KERNEL);
+ if (!ptp)
+ return -ENOMEM;
+
+ ret = devm_mutex_init(dev, &ptp->lock);
+ if (ret)
+ return ret;
+
+ ptp->phydev = phydev;
+ ptp->info = rtl8127a_ptp_info;
+
+ ret = rtl8127a_ptp_hwtstamp_enable(ptp, false);
+ if (ret)
+ return ret;
+
+ ptp->clock = ptp_clock_register(&ptp->info, dev);
+ if (IS_ERR(ptp->clock))
+ return PTR_ERR(ptp->clock);
+
+ phydev->priv = ptp;
+
+ return 0;
+}
+
+void rtl8127a_ptp_remove(struct phy_device *phydev)
+{
+ struct rtl8127a_ptp *ptp = phydev->priv;
+
+ if (!ptp)
+ return;
+
+ rtl8127a_ptp_hwtstamp_enable(ptp, false);
+ ptp_clock_unregister(ptp->clock);
+}
--
2.43.0
Re: [PATCH net-next v2 1/2] net: phy: realtek: add PTP clock for RTL8127a
Author: netdev-bot+sashiko
Date: Sun, 15 Nov 2026 09:05
Date: Sun, 15 Nov 2026 09:05
307 lines
18492 bytes
18492 bytes
Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 4 · Low: 2
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_remove()
(drivers/net/phy/realtek/realtek_ptp.c:365-374) unregisters the clock…
- [Medium] The patch adds a PHC whose callbacks
(gettimex64/settime64/adjtime/adjfine) can reach the PHY's GPHY_OCP…
- [Medium] The PHC is registered at PHY probe and stays reachable from
userspace for the whole binding.
- [Medium] The PHC is seeded in .config_init, which runs from
phy_init_hw() before the rest of r8169's PHY bring-up.
- [Low] rtl8127a_ptp_hwtstamp_enable() ends with
`rtl8127a_ptp_modify(ptp, 0xa640, 0, BIT(15))` (realtek_ptp.c:216),…
- [Low] In rtl8127a_ptp_gettimex64() (realtek_ptp.c:290-293),
ptp_read_system_prets() and ptp_read_system_postts() bracket the whole…
--
net: phy: realtek: add PTP clock for RTL8127a
This patch registers a PTP hardware clock for the internal PHY of RTL8127A
(RTL8261C, PHY ID 0x001cc890). The PHY's free-running clock is read, set,
stepped and frequency-adjusted through vendor MMD registers, and a driver
mutex serializes multi-register sequences. The clock is seeded from system
time in config_init each time the PHY is initialized.
> diff --git a/drivers/net/phy/realtek/realtek_ptp.c b/drivers/net/phy/realtek/realtek_ptp.c
> new file mode 100644
> index 0000000000000..bd5c0d6988dae
> --- /dev/null
> +++ b/drivers/net/phy/realtek/realtek_ptp.c
> @@ -0,0 +1,374 @@
[ ... ]
> +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);
> +}
[Severity: Medium]
Do these accesses need more than the MDIO bus lock to stay serialized
against r8169? The PTP accessors end up on the MAC's GPHY_OCP register:
phy_read_mmd()
rtl822x_read_mmd()
rtlgen_read_mmd()
rtlgen_read_vend2()
__mdiobus_c45_read()
r8169_mdio_read_reg_c45()
r8168_phy_ocp_read()
r8169 loads the RTL8127A PHY firmware through the same register, and that
path does not take the MDIO bus lock:
rtl8169_up()
rtl8169_init_phy()
r8169_hw_phy_config()
rtl8127a_1_hw_phy_config()
r8169_apply_firmware()
rtl_fw_write_firmware()
rtl_writephy()
r8168g_mdio_write()
r8168_phy_ocp_write()
RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
/dev/ptpN exists from PHY probe onward. A gettimex64, settime64, adjtime
or adjfine call could arrive from phc2sys or a chrony PHC refclock during
interface open, the reset worker, or runtime/system resume. Its GPHY_OCP
transactions could then interleave with the firmware patch writes.
Could that lose or redirect a firmware write, or hand the other
transaction's data back to the PTP code?
The unlocked firmware loader in r8169 predates this patch. This patch adds
a path that userspace polls continuously.
[ ... ]
> +static int rtl8127a_ptp_hwtstamp_enable(struct rtl8127a_ptp *ptp, bool enable)
> +{
[ ... ]
> + if (ret)
> + goto out;
> +
> + ret = rtl8127a_ptp_modify(ptp, 0xa640, 0, BIT(15));
[Severity: Low]
This isn't a bug, but what does BIT(15) of 0xa640 control? The register is
outside the 0xe4xx PTP block and has no define or comment.
Both the enable and the disable branch reach this write. Probe and remove
only call rtl8127a_ptp_hwtstamp_enable(ptp, false), but they still set the
bit, and nothing ever clears it.
If the bit turns on some PHY-core timestamping function, does it stay on
after disable, remove() and driver unbind? The later hardware timestamping
patch in the series keeps the same unconditional write.
> +out:
> + mutex_unlock(&ptp->lock);
[ ... ]
> +static int rtl8127a_ptp_gettimex64(struct ptp_clock_info *info,
> + struct timespec64 *ts,
> + struct ptp_system_timestamp *sts)
> +{
> + struct rtl8127a_ptp *ptp = container_of(info, struct rtl8127a_ptp, info);
> + int ret;
> +
> + mutex_lock(&ptp->lock);
> + ptp_read_system_prets(sts);
> + ret = rtl8127a_ptp_clkadj(ptp, RTL8127A_PTP_DIRECT_READ);
> + ptp_read_system_postts(sts);
[Severity: Low]
Should prets/postts surround only the EXEC write that latches the counter?
rtl8127a_ptp_clkadj() first does an MDIO read of CLK_CFG inside
rtl8127a_ptp_modify(), then the EXEC write, then a read_poll_timeout()
(1us sleep, 1ms timeout) until EXEC clears.
All of that falls inside the system-time window. The window is wider than
it needs to be, and its midpoint is shifted away from the actual latch
point.
Taking prets just before the EXEC write and postts just after it, then
polling, would avoid this.
> + if (!ret)
> + ret = rtl8127a_ptp_read_time(ptp, RTL8127A_PTP_CFG_TIME, ts);
> + mutex_unlock(&ptp->lock);
[ ... ]
> +int rtl8127a_ptp_config_init(struct phy_device *phydev)
> +{
> + struct rtl8127a_ptp *ptp = phydev->priv;
> + struct timespec64 ts;
> +
> + if (!ptp)
> + return 0;
> +
> + /* (Re)start the clock from the system time whenever the PHY is
> + * (re)initialized, e.g. each time the MAC driver brings it up.
> + */
> + ktime_get_real_ts64(&ts);
> +
> + return rtl8127a_ptp_settime(ptp, &ts);
[Severity: Medium]
Does the time written here survive the rest of the r8169 PHY bring-up?
config_init runs from phy_init_hw(), before the firmware load and the soft
reset:
drivers/net/ethernet/realtek/r8169_main.c:rtl8169_init_phy() {
phy_init_hw(tp->phydev);
phy_resume(tp->phydev);
r8169_hw_phy_config(tp, tp->phydev, tp->mac_version);
...
genphy_soft_reset(tp->phydev);
}
rtl8127a_1_hw_phy_config() starts with r8169_apply_firmware(), which loads
the PHY MCU patch and polls for BMCR_RESET. It then writes a number of PHY
parameters.
If the firmware load or the BMCR soft reset resets the PTP block, the
commit message would no longer hold:
"The clock is started from the system time each time the PHY is
initialized."
The probe-time programming in rtl8127a_ptp_hwtstamp_enable() (SYNCE_CTL,
INER, CTL, 0xa640) would be lost as well.
With the later hardware timestamping patch, the PTP_CTL/INER/GEN_CFG setup
written by rtl8127a_ptp_hwtstamp_set() is never re-applied after
rtl8169_up() either (open, reset worker, resume). hwtstamp_set() only calls
rtl8127a_ptp_hwtstamp_enable() when tx_type or rx_filter change, so
re-issuing the same configuration would not restore it.
Could hardware timestamping then stop silently after a MAC reset or
resume? The code alone doesn't show whether these registers are actually
cleared; that depends on the hardware.
> +}
> +
> +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;
[ ... ]
> + ptp->clock = ptp_clock_register(&ptp->info, dev);
> + if (IS_ERR(ptp->clock))
> + return PTR_ERR(ptp->clock);
[Severity: Medium]
What happens to PHC users while the r8169 PCI function is
runtime-suspended? The clock stays registered for the whole PHY binding.
r8169 schedules a runtime suspend 10s after the interface goes down or
loses carrier:
drivers/net/ethernet/realtek/r8169_main.c:rtl8169_runtime_idle() {
...
if (!netif_running(tp->dev) || !netif_carrier_ok(tp->dev))
pm_schedule_suspend(device, 10000);
...
}
In D3hot, r8168_phy_ocp_read() returns 0xffff. r8168_phy_ocp_write() times
out and logs a rtl_loop_wait error, but r8169_mdio_write_reg_c45() still
returns 0.
Every PHC operation (clock_gettime, settime, adjtime, adjfine, and
PTP_SYS_OFFSET_EXTENDED with up to 25 samples) would then send MMIO to a
suspended device. It would busy-wait through the write timeouts and the
1ms EXEC poll and then fail with -ETIMEDOUT.
Other userspace-reachable r8169 paths guard against this: the LED ops use
pm_runtime_resume_and_get() and get_stats64 checks pm_runtime_active().
This PHY-side path has no such guard.
When the link comes back, the resume path re-seeds the clock:
rtl8169_runtime_resume()
rtl8169_up()
rtl8169_init_phy()
phy_init_hw()
rtl8127a_ptp_config_init()
That steps the PHC to CLOCK_REALTIME. A ptp4l/phc2sys session would see
errors while the cable is unplugged and then a clock jump. This is still
the case at the end of the series.
> +
> + phydev->priv = ptp;
> +
> + return 0;
> +}
> +
> +void rtl8127a_ptp_remove(struct phy_device *phydev)
> +{
> + struct rtl8127a_ptp *ptp = phydev->priv;
> +
> + if (!ptp)
> + return;
> +
> + rtl8127a_ptp_hwtstamp_enable(ptp, false);
> + ptp_clock_unregister(ptp->clock);
> +}
[Severity: Medium]
Should phydev->priv be cleared here? rtl8127a_ptp_probe() allocates ptp
with devm_kzalloc() on &phydev->mdio.dev. Devres frees it right after
remove() returns, but phydev->priv still points at it.
rtl8127a_ptp_config_init() and rtl8127a_ptp_remove() treat any non-NULL
priv as a live object. Consider this sequence:
- the realtek PHY driver is unbound through sysfs while r8169 stays
attached
- it is rebound, and the probe fails before phydev->priv = ptp
(devm_kzalloc() failure, an MDIO timeout in
rtl8127a_ptp_hwtstamp_enable(), a ptp_clock_register() failure, or a
hwmon init failure in rtl822x_probe())
phy_probe() sets phydev->drv before calling drv->probe and does not clear
it on the out_reset path. The next rtl8169_up() would then do:
rtl8169_init_phy()
phy_init_hw()
rtl8127a_ptp_config_init()
rtl8127a_ptp_settime()
mutex_lock(&ptp->lock);
Wouldn't that be a use-after-free on the devres memory? The later hardware
timestamping patch keeps the same remove() and only clears mii_ts and
default_timestamp, so the problem remains at the end of the series.
Setting phydev->priv = NULL in rtl8127a_ptp_remove() would avoid it.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261111083807.1529-1-javen_xu%40realsil.com.cn
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads