Re: [PATCH net-next v5 3/4] net: phy: realtek: add PTP clock support for RTL8127a

From: netdev-bot+sashiko

Date: Sat Dec 05 2026 - 05:33:04 EST


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

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

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

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

--

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

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

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

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

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

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

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

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

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

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

The v5 changelog says this behaviour was dropped:

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

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

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

bool time_set;

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

Could the sentence and the field be dropped?

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

[ ... ]

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

[ ... ]

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

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

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

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

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

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

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

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

rtl8127a_ptp_probe() goes straight on to the next step:

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

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

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

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

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

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

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

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

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

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

The same question applies to these sequences:

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

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

[ ... ]

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

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

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

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

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

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

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

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

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

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