Skip to contentSkip to conversation

AT8031 PHY timestamping support

RFC PATCH In review Michael Walle 2 participants 7 messages Feb 28, 2020 – Mar 1, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover AT8031 PHY timestamping support

I've put some additional time into figuring out how the PHY works since my last RFC. My previous comments on the interrupt handling was caused by my broken setup.

This patchset is the current state of my work for adding PHY timestamping support. I just wanted to post this to the mailinglist before I never do it. Maybe its a starting point for other people. That being said, I wouldn't mind comments ;) Also I like to share my findings about the PHY. The PHY has three major caveats which IMHO makes it really hard to work properly.

(1) The PHY doesn't support atomic reading of the (timestamp, messageType, sequenceId) tuple. The workaround is to read the timestamp again and check if it has changed. Actually, you'd have to read the complete tuple again.
(2) The PHY generates an interrupt on every PTP packet not only on event messages. Thus the interrupt handler may read the capture registers and then determine that nothing has changed. This also means we have to remember the last read timestamp. I make the assumption that the timestamp is unique.
(3) The biggest drawback is that the PHY only provide one set of RX and TX capture registers. It is possible that the timestamp will change when another PTP event message is received while we are still reading the timestamp of the previously received packet.

Example A
The driver basically works when there is low PTP traffic. Eg. the following works pretty good.

ptp4l -m -2 -i eth0 -f ptp.cfg -s

ptp.cfg:
[global]
tx_timestamp_timeout 100

Example B
But if you're using a P2P clock with peer delay requests this whole thing falls apart because of caveat (3). You'll often see messages like
received SYNC without timestamp
or
received PDELAY_RESP without timestamp
in linuxptp. Sometimes it working for some time and then it starts to loosing packets. I suspect this depends on how the PDELAY messages are interleaved with the SYNC message. If there is not enough time to until the next event message is received either of these two messages won't have a timestamp.

ptp4l -m -f gPTP.cfg -i eth0 -s

gPTP.cfg is the one from stock linuxptp with tx_timestamp_timeout set to 100.

The PHY also supports appending the timestamp to the actual ethernet frame, but this seems to only work when the PHY is connected via RGMII. I've never get it to work with a SGMII connection.

The first patch might actually be useful outside of this series. See also
https://lore.kernel.org/netdev/[email protected]/

Changes since RFC v1:
net: phy: let the driver register its own IRQ handler
- fixed mistake for calling phy_request_interrupt(). Thanks Heiner.
- removed phy_drv_interrupt(). just calling phy_mac_interrupt()

net: phy: at803x: add PTP support for AR8031
- moved rereading the timestamp out of at8031_read_ts() to at8031_get_rxts()/at8031_get_txts().
- call phy_mac_interrupt() instead of phy_drv_interrupt()

Please note that Heiner suggested that I should use handle_interrupt() instead of registering my own handler. I've still included my old patch here because the discussion is still ongoing and the proposed fix is only working for this use case. See also
https://lore.kernel.org/netdev/[email protected]/

-michael

Michael Walle (2):
net: phy: let the driver register its own IRQ handler
net: phy: at803x: add PTP support for AR8031

 drivers/net/phy/Kconfig      |  17 +
 drivers/net/phy/at803x.c     | 855 ++++++++++++++++++++++++++++++++++-
 drivers/net/phy/phy_device.c |   6 +-
 include/linux/phy.h          |   1 +
 4 files changed, 852 insertions(+), 27 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

RFC PATCH v2 1/2 net: phy: let the driver register its own IRQ handler

There are more and more PHY drivers which has more than just the PHY link change interrupts. For example, temperature thresholds or PTP interrupts.

At the moment it is not possible to correctly handle interrupts for PHYs which has a clear-on-read interrupt status register. It is also likely that the current approach of the phylib isn't working for all PHYs out there.

Therefore, this patch let the PHY driver register its own interrupt handler. To notify the phylib about a link change, the interrupt handler has to call the new function phy_drv_interrupt().

---
 drivers/net/phy/phy_device.c | 6 ++++--
 include/linux/phy.h          | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/net/phy/phy_device.c +4 -2
index 6a5056e0ae77..a30a5472fa46 100644
@@ -965,7 +965,8 @@ int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
 		return rc;
 
 	phy_prepare_link(phydev, handler);
-	if (phy_interrupt_is_valid(phydev))
+	if (phy_interrupt_is_valid(phydev)
+	    && !(phydev->drv->flags & PHY_HAS_OWN_IRQ_HANDLER))
 		phy_request_interrupt(phydev);
 
 	return 0;
@@ -2411,7 +2412,8 @@ EXPORT_SYMBOL(phy_validate_pause);
 
 static bool phy_drv_supports_irq(struct phy_driver *phydrv)
 {
-	return phydrv->config_intr && phydrv->ack_interrupt;
+	return ((phydrv->config_intr && phydrv->ack_interrupt)
+		|| phydrv->flags & PHY_HAS_OWN_IRQ_HANDLER);
 }
 
 /**
include/linux/phy.h +1 -0
index c570e162e05e..9cadc8ae4e87 100644
@@ -75,6 +75,7 @@ extern const int phy_10gbit_features_array[1];
 
 #define PHY_IS_INTERNAL		0x00000001
 #define PHY_RST_AFTER_CLK_EN	0x00000002
+#define PHY_HAS_OWN_IRQ_HANDLER	0x00000004
 #define MDIO_DEVICE_IS_PHY	0x80000000
 
 /* Interface Mode definitions */
Signature · 1 line
2.20.1
#2 lore raw

RFC PATCH v2 2/2 net: phy: at803x: add PTP support for AR8031

In reply to Michael Walle

Add PHY timestamping to the Atheros AR8031 PHY.

---
 drivers/net/phy/Kconfig  |  17 +
 drivers/net/phy/at803x.c | 855 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 847 insertions(+), 25 deletions(-)
drivers/net/phy/Kconfig +17 -0
index 9dabe03a668c..f3d4655af436 100644
@@ -462,6 +462,23 @@ config AT803X_PHY
 	help
 	  Currently supports the AR8030, AR8031, AR8033 and AR8035 model
 
+if AT803X_PHY
+config AT8031_PHY_TIMESTAMPING
+	bool "Enable PHY timestamping support"
+	depends on NETWORK_PHY_TIMESTAMPING
+	depends on PTP_1588_CLOCK
+	help
+	  Enable the IEEE1588 features for the AR8031 PHY.
+
+	  This driver adds support for using the AR8031 as a PTP
+	  clock. This clock is only useful if your PTP programs are
+	  getting hardware time stamps on the PTP Ethernet packets
+	  using the SO_TIMESTAMPING API.
+
+	  In order for this to work, your MAC driver must also
+	  implement the skb_tx_timestamp() function.
+endif
+
 config QSEMI_PHY
 	tristate "Quality Semiconductor PHYs"
 	---help---
drivers/net/phy/at803x.c +830 -25

945 lines not on this page · raw message

Signature · 1 line
2.20.1
#3 lore raw

Re: [RFC PATCH v2 2/2] net: phy: at803x: add PTP support for AR8031

drivers/net/phy/at803x.c static int at803x_parse_dt(struct phy_device *phydev) · line 824

Quoted 4 lines · Michael Walle
drivers/net/phy/at803x.c +4 -0
+static int at8031_rtc_adjust(struct phy_device *phydev, s64 delta)
+{
+	struct timespec64 ts = ns_to_timespec64(delta);
+	int ret;

Here the 'ts' is written in multiple steps,

Quoted 33 lines
drivers/net/phy/at803x.c +33 -0
+	ret = phy_write_mmd(phydev, MDIO_MMD_PCS,
+			    AT8031_MMD3_RTC_OFFSET_SEC_2,
+			    (ts.tv_sec >> 32) & 0xffff);
+	if (ret)
+		return ret;
+
+	ret = phy_write_mmd(phydev, MDIO_MMD_PCS,
+			    AT8031_MMD3_RTC_OFFSET_SEC_1,
+			    (ts.tv_sec >> 16) & 0xffff);
+	if (ret)
+		return ret;
+
+	ret = phy_write_mmd(phydev, MDIO_MMD_PCS,
+			    AT8031_MMD3_RTC_OFFSET_SEC_0,
+			    ts.tv_sec & 0xffff);
+	if (ret)
+		return ret;
+
+	ret = phy_write_mmd(phydev, MDIO_MMD_PCS,
+			    AT8031_MMD3_RTC_OFFSET_NSEC_1,
+			    (ts.tv_nsec >> 16) & 0xffff);
+	if (ret)
+		return ret;
+
+	ret = phy_write_mmd(phydev, MDIO_MMD_PCS,
+			    AT8031_MMD3_RTC_OFFSET_NSEC_0,
+			    ts.tv_nsec & 0xffff);
+	if (ret)
+		return ret;
+
+	return phy_write_mmd(phydev, MDIO_MMD_PCS, AT8031_MMD3_RTC_ADJUST,
+			     AT8031_RTC_ADJUST);
+}

...

drivers/net/phy/at803x.c static int at803x_parse_dt(struct phy_device *phydev) · line 938

Quoted 8 lines
drivers/net/phy/at803x.c +8 -0
+static int at8031_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
+{
+	struct at803x_priv *priv =
+		container_of(ptp, struct at803x_priv, ptp_info);
+	struct phy_device *phydev = priv->phydev;
+
+	return at8031_rtc_adjust(phydev, delta);
+}

... and here there is no locking. You would need a mutex here and elsewhere to prevent multiple readers/writers from accessing the device registers asynchronously.

(I know this is a just a RFC and that there are bigger problems with the HW, but just saying.)

Thanks,
Richard

#4 lore raw
In reply to Michael Walle
Quoted 4 lines · Michael Walle

(1) The PHY doesn't support atomic reading of the (timestamp, messageType, sequenceId) tuple. The workaround is to read the timestamp again and check if it has changed. Actually, you'd have to read the complete tuple again.

This HW is broken by design :(

Quoted 10 lines

But if you're using a P2P clock with peer delay requests this whole thing falls apart because of caveat (3). You'll often see messages like
received SYNC without timestamp
or
received PDELAY_RESP without timestamp
in linuxptp. Sometimes it working for some time and then it starts to loosing packets. I suspect this depends on how the PDELAY messages are interleaved with the SYNC message. If there is not enough time to until the next event message is received either of these two messages won't have a timestamp.

And even the case where a Sync and a DelayResp arrive at nearly the same time will fail.

Quoted 3 lines

The PHY also supports appending the timestamp to the actual ethernet frame, but this seems to only work when the PHY is connected via RGMII. I've never get it to work with a SGMII connection.

This is the way to go. I would try to get the vendor's help in making this work.

Thanks,
Richard

#5 lore raw

Hi Richard,

Am 2020-02-28 19:15, schrieb Richard Cochran:

Quoted 7 lines · Michael Walle
Quoted 4 lines

(1) The PHY doesn't support atomic reading of the (timestamp, messageType, sequenceId) tuple. The workaround is to read the timestamp again and check if it has changed. Actually, you'd have to read the complete tuple again.

This HW is broken by design :(

Yeah, I know. And actually I don't think I'll pursue this further. Like I said, I just wanted to my current work. Maybe it will be useful in the future who knows.

Quoted 14 lines
Quoted 10 lines

But if you're using a P2P clock with peer delay requests this whole thing falls apart because of caveat (3). You'll often see messages like
received SYNC without timestamp
or
received PDELAY_RESP without timestamp
in linuxptp. Sometimes it working for some time and then it starts to loosing packets. I suspect this depends on how the PDELAY messages are interleaved with the SYNC message. If there is not enough time to until the next event message is received either of these two messages won't have a timestamp.

And even the case where a Sync and a DelayResp arrive at nearly the same time will fail.

Quoted 6 lines
Quoted 3 lines

The PHY also supports appending the timestamp to the actual ethernet frame, but this seems to only work when the PHY is connected via RGMII. I've never get it to work with a SGMII connection.

This is the way to go. I would try to get the vendor's help in making this work.

Like I said, our FAE is pretty unresponsive. But I'll at least try to find out if my guess is correct (that it only works with RGMII). But even then, how should the outgoing timestamping work. There are two possibilities:

(1) According to the datasheet, the PHY will attach the TX timestamp to the corresponding RX packet; whatever that means. Lets assume there is such a "corresponding packet", then we would be at the mercy of
the
peer to actually send such a packet, let alone in a timely manner.
(2) Mixing both methods. Use attached timestamps for RX packets, read
the
timestamp via PHY registers for TX packets. Theoretically, we could control how the packets are send and make sure, we fetch the TX timestamp before sending another PTP packet. But well.. sounds
really
hacky to me.

-michael

#6 lore raw
Quoted 4 lines · Michael Walle

Yeah, I know. And actually I don't think I'll pursue this further. Like I said, I just wanted to my current work. Maybe it will be useful in the future who knows.

I appreciate your publishing this work. It is a good starting place. Maybe the vendor will wake up and help this along. One can always hope.

Quoted 8 lines

Like I said, our FAE is pretty unresponsive. But I'll at least try to find out if my guess is correct (that it only works with RGMII). But even then, how should the outgoing timestamping work. There are two possibilities:

(1) According to the datasheet, the PHY will attach the TX timestamp to the corresponding RX packet; whatever that means. Lets assume there is such a "corresponding packet", then we would be at the mercy of the peer to actually send such a packet, let alone in a timely manner.

I see. Mysterious. For a Sync frame, I can't think of any "corresponding RX packet".

Quoted 5 lines

(2) Mixing both methods. Use attached timestamps for RX packets, read the timestamp via PHY registers for TX packets. Theoretically, we could control how the packets are send and make sure, we fetch the TX timestamp before sending another PTP packet. But well.. sounds really hacky to me.

It would not be that bad. Some of the MAC cards can only buffer one Tx time stamp, and for this reason, the ptp4l program always fetches the time stamp immediately after sending.

Thanks,
Richard