2024-01-18 09:01:29

by Horatiu Vultur

[permalink] [raw]
Subject: [PATCH net v2 0/2] net: micrel: Fixes for PHC for lan8814

There are different issues with the PHC of lan8814.
First issue is similar with the one on lan8841 which it has enabled by
default the check regarding minorVersionPTP in the PTP header frame.
So all the frames compliant to 8021AS will not be timestamped.
The second issue is with setting and getting the time of the PHC. The
issue is seen only when setting a time which is bigger than 2^32.

v1->v2:
- replace hardcoded values with macros for PTP version

Horatiu Vultur (2):
net: micrel: Fix PTP frame parsing for lan8814
net: micrel: Fix set/get PHC time for lan8814

drivers/net/phy/micrel.c | 72 ++++++++++++++++++++++------------------
1 file changed, 40 insertions(+), 32 deletions(-)

--
2.34.1



2024-01-18 09:01:48

by Horatiu Vultur

[permalink] [raw]
Subject: [PATCH net v2 1/2] net: micrel: Fix PTP frame parsing for lan8814

The HW has the capability to check each frame if it is a PTP frame,
which domain it is, which ptp frame type it is, different ip address in
the frame. And if one of these checks fail then the frame is not
timestamp. Most of these checks were disabled except checking the field
minorVersionPTP inside the PTP header. Meaning that once a partner sends
a frame compliant to 8021AS which has minorVersionPTP set to 1, then the
frame was not timestamp because the HW expected by default a value of 0
in minorVersionPTP. This is exactly the same issue as on lan8841.
Fix this issue by removing this check so the userspace can decide on this.

Fixes: ece19502834d ("net: phy: micrel: 1588 support for LAN8814 phy")
Signed-off-by: Horatiu Vultur <[email protected]>
Reviewed-by: Maxime Chevallier <[email protected]>
Reviewed-by: Divya Koppera <[email protected]>
---
drivers/net/phy/micrel.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/drivers/net/phy/micrel.c b/drivers/net/phy/micrel.c
index bf4053431dcb3..43520ac0f4e00 100644
--- a/drivers/net/phy/micrel.c
+++ b/drivers/net/phy/micrel.c
@@ -120,6 +120,11 @@
*/
#define LAN8814_1PPM_FORMAT 17179

+#define PTP_RX_VERSION 0x0248
+#define PTP_TX_VERSION 0x0288
+#define PTP_MAX_VERSION(x) (((x) & GENMASK(7, 0)) << 8)
+#define PTP_MIN_VERSION(x) ((x) & GENMASK(7, 0))
+
#define PTP_RX_MOD 0x024F
#define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
#define PTP_RX_TIMESTAMP_EN 0x024D
@@ -3150,6 +3155,12 @@ static void lan8814_ptp_init(struct phy_device *phydev)
lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0);
lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0);

+ /* Disable checking for minorVersionPTP field */
+ lanphy_write_page_reg(phydev, 5, PTP_RX_VERSION,
+ PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
+ lanphy_write_page_reg(phydev, 5, PTP_TX_VERSION,
+ PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
+
skb_queue_head_init(&ptp_priv->tx_queue);
skb_queue_head_init(&ptp_priv->rx_queue);
INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
--
2.34.1


2024-01-18 09:02:09

by Horatiu Vultur

[permalink] [raw]
Subject: [PATCH net v2 2/2] net: micrel: Fix set/get PHC time for lan8814

When setting or getting PHC time, the higher bits of the second time (>32
bits) they were ignored. Meaning that setting some time in the future like
year 2150, it was failing to set this.

The issue can be reproduced like this:

# phc_ctl /dev/ptp1 set 10000000000
phc_ctl[118.619]: set clock time to 4294967295.000000000 or Sun Feb 7 06:28:15 2106

# phc_ctl /dev/ptp1 get
phc_ctl[120.858]: clock time is 1.238620924 or Thu Jan 1 00:00:01 1970

Fixes: ece19502834d ("net: phy: micrel: 1588 support for LAN8814 phy")
Signed-off-by: Horatiu Vultur <[email protected]>
Reviewed-by: Maxime Chevallier <[email protected]>
Reviewed-by: Divya Koppera <[email protected]>
---
drivers/net/phy/micrel.c | 61 +++++++++++++++++++---------------------
1 file changed, 29 insertions(+), 32 deletions(-)

diff --git a/drivers/net/phy/micrel.c b/drivers/net/phy/micrel.c
index 43520ac0f4e00..0ceba62d55c08 100644
--- a/drivers/net/phy/micrel.c
+++ b/drivers/net/phy/micrel.c
@@ -154,11 +154,13 @@
#define PTP_CMD_CTL_PTP_LTC_STEP_SEC_ BIT(5)
#define PTP_CMD_CTL_PTP_LTC_STEP_NSEC_ BIT(6)

+#define PTP_CLOCK_SET_SEC_HI 0x0205
#define PTP_CLOCK_SET_SEC_MID 0x0206
#define PTP_CLOCK_SET_SEC_LO 0x0207
#define PTP_CLOCK_SET_NS_HI 0x0208
#define PTP_CLOCK_SET_NS_LO 0x0209

+#define PTP_CLOCK_READ_SEC_HI 0x0229
#define PTP_CLOCK_READ_SEC_MID 0x022A
#define PTP_CLOCK_READ_SEC_LO 0x022B
#define PTP_CLOCK_READ_NS_HI 0x022C
@@ -2592,35 +2594,31 @@ static bool lan8814_rxtstamp(struct mii_timestamper *mii_ts, struct sk_buff *skb
}

static void lan8814_ptp_clock_set(struct phy_device *phydev,
- u32 seconds, u32 nano_seconds)
+ time64_t sec, u32 nsec)
{
- u32 sec_low, sec_high, nsec_low, nsec_high;
-
- sec_low = seconds & 0xffff;
- sec_high = (seconds >> 16) & 0xffff;
- nsec_low = nano_seconds & 0xffff;
- nsec_high = (nano_seconds >> 16) & 0x3fff;
-
- lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, sec_low);
- lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, sec_high);
- lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, nsec_low);
- lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, nsec_high);
+ lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_LO, lower_16_bits(sec));
+ lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_MID, upper_16_bits(sec));
+ lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_SEC_HI, upper_32_bits(sec));
+ lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_LO, lower_16_bits(nsec));
+ lanphy_write_page_reg(phydev, 4, PTP_CLOCK_SET_NS_HI, upper_16_bits(nsec));

lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
}

static void lan8814_ptp_clock_get(struct phy_device *phydev,
- u32 *seconds, u32 *nano_seconds)
+ time64_t *sec, u32 *nsec)
{
lanphy_write_page_reg(phydev, 4, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);

- *seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID);
- *seconds = (*seconds << 16) |
- lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO);
+ *sec = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_HI);
+ *sec <<= 16;
+ *sec |= lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_MID);
+ *sec <<= 16;
+ *sec |= lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_SEC_LO);

- *nano_seconds = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI);
- *nano_seconds = ((*nano_seconds & 0x3fff) << 16) |
- lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO);
+ *nsec = lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_HI);
+ *nsec <<= 16;
+ *nsec |= lanphy_read_page_reg(phydev, 4, PTP_CLOCK_READ_NS_LO);
}

static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
@@ -2630,7 +2628,7 @@ static int lan8814_ptpci_gettime64(struct ptp_clock_info *ptpci,
ptp_clock_info);
struct phy_device *phydev = shared->phydev;
u32 nano_seconds;
- u32 seconds;
+ time64_t seconds;

mutex_lock(&shared->shared_lock);
lan8814_ptp_clock_get(phydev, &seconds, &nano_seconds);
@@ -2660,38 +2658,37 @@ static void lan8814_ptp_clock_step(struct phy_device *phydev,
{
u32 nano_seconds_step;
u64 abs_time_step_ns;
- u32 unsigned_seconds;
+ time64_t set_seconds;
u32 nano_seconds;
u32 remainder;
s32 seconds;

if (time_step_ns > 15000000000LL) {
/* convert to clock set */
- lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
- unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
- &remainder);
+ lan8814_ptp_clock_get(phydev, &set_seconds, &nano_seconds);
+ set_seconds += div_u64_rem(time_step_ns, 1000000000LL,
+ &remainder);
nano_seconds += remainder;
if (nano_seconds >= 1000000000) {
- unsigned_seconds++;
+ set_seconds++;
nano_seconds -= 1000000000;
}
- lan8814_ptp_clock_set(phydev, unsigned_seconds, nano_seconds);
+ lan8814_ptp_clock_set(phydev, set_seconds, nano_seconds);
return;
} else if (time_step_ns < -15000000000LL) {
/* convert to clock set */
time_step_ns = -time_step_ns;

- lan8814_ptp_clock_get(phydev, &unsigned_seconds, &nano_seconds);
- unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
- &remainder);
+ lan8814_ptp_clock_get(phydev, &set_seconds, &nano_seconds);
+ set_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
+ &remainder);
nano_seconds_step = remainder;
if (nano_seconds < nano_seconds_step) {
- unsigned_seconds--;
+ set_seconds--;
nano_seconds += 1000000000;
}
nano_seconds -= nano_seconds_step;
- lan8814_ptp_clock_set(phydev, unsigned_seconds,
- nano_seconds);
+ lan8814_ptp_clock_set(phydev, set_seconds, nano_seconds);
return;
}

--
2.34.1


2024-01-18 13:52:39

by Andrew Lunn

[permalink] [raw]
Subject: Re: [PATCH net v2 2/2] net: micrel: Fix set/get PHC time for lan8814

On Thu, Jan 18, 2024 at 09:59:16AM +0100, Horatiu Vultur wrote:
> When setting or getting PHC time, the higher bits of the second time (>32
> bits) they were ignored. Meaning that setting some time in the future like
> year 2150, it was failing to set this.

Stable rules say:

It must either fix a real bug that bothers people...

Do we have users of this device in 2150?

Maybe submit this for net-next?

Andrew

2024-01-18 15:28:56

by Horatiu Vultur

[permalink] [raw]
Subject: Re: [PATCH net v2 2/2] net: micrel: Fix set/get PHC time for lan8814

The 01/18/2024 14:52, Andrew Lunn wrote:

Hi Andrew,

>
> On Thu, Jan 18, 2024 at 09:59:16AM +0100, Horatiu Vultur wrote:
> > When setting or getting PHC time, the higher bits of the second time (>32
> > bits) they were ignored. Meaning that setting some time in the future like
> > year 2150, it was failing to set this.
>
> Stable rules say:
>
> It must either fix a real bug that bothers people...
>
> Do we have users of this device in 2150?

No, and it is not like a customer raise this issue because they have
some special cases. It was me who discover it by accident and I
found it annoying not being able to do something that is that simple.
I mean, I just want to set the time and then be able to read it back.

>
> Maybe submit this for net-next?

Anyway, I don't have strong feelings about this, if it goes to net or
net-next, I just want to fix this at some point :)

>
> Andrew

--
/Horatiu

2024-01-18 17:00:44

by Andrew Lunn

[permalink] [raw]
Subject: Re: [PATCH net v2 2/2] net: micrel: Fix set/get PHC time for lan8814

> > Maybe submit this for net-next?
>
> Anyway, I don't have strong feelings about this, if it goes to net or
> net-next, I just want to fix this at some point :)

Please submit to net-next. I think the ML bot which picks out patches
to backport is likely to see the work Fix in the subject and decided
to backport it anyway. But its not our problem if the bot breaks the
stable rules.

Is there any danger of regressions? Could the higher word actually
have a value for some reason today, which is being ignored. Would this
change then jump the time forward?

Andrew

2024-01-19 08:21:38

by Horatiu Vultur

[permalink] [raw]
Subject: Re: [PATCH net v2 2/2] net: micrel: Fix set/get PHC time for lan8814

The 01/18/2024 18:00, Andrew Lunn wrote:
>
> > > Maybe submit this for net-next?
> >
> > Anyway, I don't have strong feelings about this, if it goes to net or
> > net-next, I just want to fix this at some point :)
>
> Please submit to net-next. I think the ML bot which picks out patches
> to backport is likely to see the work Fix in the subject and decided
> to backport it anyway. But its not our problem if the bot breaks the
> stable rules.

Yes, I will do that.

>
> Is there any danger of regressions? Could the higher word actually
> have a value for some reason today, which is being ignored. Would this
> change then jump the time forward?

I am not seeing any danger of regressions.
By default the higher word has a value of 0, and doesn't have any
special functionality to jump forward or backwards. It just contains
upper 16 bits of the second [47:32].
I have seen only on register PTP_LTC_STEP_ADJ_HI that the most significant
bit can signify if to jump forward or backwards.

>
> Andrew

--
/Horatiu

2024-01-19 13:07:33

by Andrew Lunn

[permalink] [raw]
Subject: Re: [PATCH net v2 2/2] net: micrel: Fix set/get PHC time for lan8814

On Thu, Jan 18, 2024 at 09:59:16AM +0100, Horatiu Vultur wrote:
> When setting or getting PHC time, the higher bits of the second time (>32
> bits) they were ignored. Meaning that setting some time in the future like
> year 2150, it was failing to set this.
>
> The issue can be reproduced like this:
>
> # phc_ctl /dev/ptp1 set 10000000000
> phc_ctl[118.619]: set clock time to 4294967295.000000000 or Sun Feb 7 06:28:15 2106
>
> # phc_ctl /dev/ptp1 get
> phc_ctl[120.858]: clock time is 1.238620924 or Thu Jan 1 00:00:01 1970
>
> Fixes: ece19502834d ("net: phy: micrel: 1588 support for LAN8814 phy")
> Signed-off-by: Horatiu Vultur <[email protected]>
> Reviewed-by: Maxime Chevallier <[email protected]>
> Reviewed-by: Divya Koppera <[email protected]>

When submitted to net-next:

Reviewed-by: Andrew Lunn <[email protected]>

Andrew

2024-01-19 20:02:05

by Simon Horman

[permalink] [raw]
Subject: Re: [PATCH net v2 1/2] net: micrel: Fix PTP frame parsing for lan8814

On Thu, Jan 18, 2024 at 09:59:15AM +0100, Horatiu Vultur wrote:
> The HW has the capability to check each frame if it is a PTP frame,
> which domain it is, which ptp frame type it is, different ip address in
> the frame. And if one of these checks fail then the frame is not
> timestamp. Most of these checks were disabled except checking the field
> minorVersionPTP inside the PTP header. Meaning that once a partner sends
> a frame compliant to 8021AS which has minorVersionPTP set to 1, then the
> frame was not timestamp because the HW expected by default a value of 0
> in minorVersionPTP. This is exactly the same issue as on lan8841.
> Fix this issue by removing this check so the userspace can decide on this.
>
> Fixes: ece19502834d ("net: phy: micrel: 1588 support for LAN8814 phy")
> Signed-off-by: Horatiu Vultur <[email protected]>
> Reviewed-by: Maxime Chevallier <[email protected]>
> Reviewed-by: Divya Koppera <[email protected]>
> ---
> drivers/net/phy/micrel.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/drivers/net/phy/micrel.c b/drivers/net/phy/micrel.c
> index bf4053431dcb3..43520ac0f4e00 100644
> --- a/drivers/net/phy/micrel.c
> +++ b/drivers/net/phy/micrel.c
> @@ -120,6 +120,11 @@
> */
> #define LAN8814_1PPM_FORMAT 17179
>
> +#define PTP_RX_VERSION 0x0248
> +#define PTP_TX_VERSION 0x0288
> +#define PTP_MAX_VERSION(x) (((x) & GENMASK(7, 0)) << 8)
> +#define PTP_MIN_VERSION(x) ((x) & GENMASK(7, 0))

FWIIW, these macros feel like open-coded versions of FIELD_PREP to me.

> +
> #define PTP_RX_MOD 0x024F
> #define PTP_RX_MOD_BAD_UDPV4_CHKSUM_FORCE_FCS_DIS_ BIT(3)
> #define PTP_RX_TIMESTAMP_EN 0x024D
> @@ -3150,6 +3155,12 @@ static void lan8814_ptp_init(struct phy_device *phydev)
> lanphy_write_page_reg(phydev, 5, PTP_TX_PARSE_IP_ADDR_EN, 0);
> lanphy_write_page_reg(phydev, 5, PTP_RX_PARSE_IP_ADDR_EN, 0);
>
> + /* Disable checking for minorVersionPTP field */
> + lanphy_write_page_reg(phydev, 5, PTP_RX_VERSION,
> + PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
> + lanphy_write_page_reg(phydev, 5, PTP_TX_VERSION,
> + PTP_MAX_VERSION(0xff) | PTP_MIN_VERSION(0x0));
> +
> skb_queue_head_init(&ptp_priv->tx_queue);
> skb_queue_head_init(&ptp_priv->rx_queue);
> INIT_LIST_HEAD(&ptp_priv->rx_ts_list);
> --
> 2.34.1
>