Re: [PATCH net-next v6 3/4] net: phy: realtek: add PTP clock support for RTL8127a
From: netdev-bot+sashiko
Date: Fri Dec 04 2026 - 21:59:20 EST
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