Skip to contentSkip to conversation

AT8031 PHY timestamping support

RFC PATCH Superseded Michael Walle 4 participants 12 messages Feb 25, 2020 – Feb 26, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover AT8031 PHY timestamping support

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 ;) The code basically works but there are three major caveats:

(1) The reading of timestamps via MDIO sometimes return wrong values. What I see is that a part of the timestamp corresponds to the new timestamp while another part still contains old values. Thus at the moment, I'm reading the registers twice. I don't know if the reading actually affects the update of the timestamp or the different timing (my MDIO bus is rather slow, so reading the timestamp a second time take some amount of time; but I've also tested with some delays and it didn't had any effects). There is also no possibility to read the timestamp atomically :(
(2) It seems to be the case that the PHY generates an interrupt on every PTP message, eg. even if it is not an event message meaning that a new timestamp is ready. Thus we might read the timestamp too often.
(3) Sometimes the TX timestamp is missing. It seems that in this case the PHY doesn't generate an interrupt. If you check for any RX_PTP/TX_PTP interrupt pending and then read both timestamps (remember that get_rxts/get_txts checks that the timestamp has actually changed) it seems to work though.

if (mask & (AT8031_INTR_RX_PTP | AT8031_INTR_TX_PTP)) {
at8031_get_rxts(phydev); at8031_get_txts(phydev);
}

Please note that the patch doesn't contain the code above. Replacing the IRQ handling with the code make PTP actually work, but I'm not satisfied with that solution, esp. reading the timestamps multiple times over MDIO. So currently I'm stuck and unfortunately, I'm not able to get support from Atheros/our FAE.

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]/

-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     | 879 ++++++++++++++++++++++++++++++++++-
 drivers/net/phy/phy.c        |  15 +
 drivers/net/phy/phy_device.c |   6 +-
 include/linux/phy.h          |   2 +
 5 files changed, 892 insertions(+), 27 deletions(-)
Signature · 1 line
2.20.1
#6 lore raw

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

Add PHY timestamping to the Atheros AR8031 PHY.

---
 drivers/net/phy/Kconfig  |  17 +
 drivers/net/phy/at803x.c | 879 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 871 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 +854 -25

969 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw

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

In reply to Michael Walle

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.c        | 15 +++++++++++++++
 drivers/net/phy/phy_device.c |  6 ++++--
 include/linux/phy.h          |  2 ++
 3 files changed, 21 insertions(+), 2 deletions(-)
drivers/net/phy/phy.c +15 -0
index d76e038cf2cb..f25aacbcf1d9 100644
@@ -942,6 +942,21 @@ void phy_mac_interrupt(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(phy_mac_interrupt);
 
+/**
+ * phy_drv_interrupt - PHY drivers says the link has changed
+ * @phydev: phy_device struct with changed link
+ *
+ * The PHY driver may implement his own interrupt handler. It will call this
+ * function to notify us about a link change. Trigger the state machine and
+ * work a work queue.
+ */
+void phy_drv_interrupt(struct phy_device *phydev)
+{
+	/* Trigger a state machine change */
+	phy_trigger_machine(phydev);
+}
+EXPORT_SYMBOL(phy_drv_interrupt);
+
 static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
 {
 	linkmode_zero(advertising);
drivers/net/phy/phy_device.c +4 -2
index 6a5056e0ae77..6d8c94e61251 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 +2 -0
index c570e162e05e..46f73b94fd60 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 */
@@ -1235,6 +1236,7 @@ int phy_drivers_register(struct phy_driver *new_driver, int n,
 void phy_state_machine(struct work_struct *work);
 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
 void phy_mac_interrupt(struct phy_device *phydev);
+void phy_drv_interrupt(struct phy_device *phydev);
 void phy_start_machine(struct phy_device *phydev);
 void phy_stop_machine(struct phy_device *phydev);
 void phy_ethtool_ksettings_get(struct phy_device *phydev,
Signature · 1 line
2.20.1
maintainer
#7 lore raw
In reply to Michael Walle
Quoted 6 lines · Michael Walle

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 ;) The code basically works but there are three major caveats:

Quoted 9 lines

(1) The reading of timestamps via MDIO sometimes return wrong values. What I see is that a part of the timestamp corresponds to the new timestamp while another part still contains old values. Thus at the moment, I'm reading the registers twice. I don't know if the reading actually affects the update of the timestamp or the different timing (my MDIO bus is rather slow, so reading the timestamp a second time take some amount of time; but I've also tested with some delays and it didn't had any effects). There is also no possibility to read the timestamp atomically :(

Hi Michael

That sounds fundamentally broken. Which would be odd. Sometimes there is a way to take a snapshot of the value. Reading the first word could trigger this snapshot. Or the last word, or some status register. One would hope the datasheet would talk about this.

Andrew

#8 lore raw

Am 26. Februar 2020 00:50:40 MEZ schrieb Andrew Lunn <[email protected]>:

Quoted 24 lines · Michael Walle
Quoted 1 line

This patchset is the current state of my work for adding PHY

timestamping

Quoted 1 line

support. I just wanted to post this to the mailinglist before I never

do

Quoted 2 lines

it. Maybe its a starting point for other people. That being said, I wouldn't mind comments ;) The code basically works but there are

three

Quoted 3 lines

major caveats:

(1) The reading of timestamps via MDIO sometimes return wrong

values. What

Quoted 1 line

I see is that a part of the timestamp corresponds to the new

timestamp

Quoted 1 line

while another part still contains old values. Thus at the moment,

I'm

Quoted 2 lines

reading the registers twice. I don't know if the reading actually affects the update of the timestamp or the different timing (my

MDIO

Quoted 1 line

bus is rather slow, so reading the timestamp a second time take

some

Quoted 2 lines

amount of time; but I've also tested with some delays and it didn't had any effects). There is also no possibility to read the

timestamp

Quoted 8 lines
Quoted 1 line
	 atomically :(

Hi Michael

That sounds fundamentally broken. Which would be odd. Sometimes there is a way to take a snapshot of the value. Reading the first word could trigger this snapshot. Or the last word, or some status register. One would hope the datasheet would talk about this.

Hi Andrew

This might be the case, but the datasheet (some older revision can be found on the internet, maybe you find something) doesn't mention it. Nor does the PTP "guide" (I don't know the exact name, I'd have to check at work) of this PHY. Besides the timestamp there's also the sequence number and the source port id which would need to be read atomically together with the timestamp.

I might give it a try reading the whole tx or rx block (sequenceId, sourcePortId, timestamp) sequentially.

-michael

#9 lore raw
Quoted 2 lines · Michael Walle

Am 26. Februar 2020 00:50:40 MEZ schrieb Andrew Lunn <[email protected]>:

Quoted 1 line

That sounds fundamentally broken.

Right. It can't work unless the PHY latches the time stamp.

Quoted 6 lines

This might be the case, but the datasheet (some older revision can be found on the internet, maybe you find something) doesn't mention it. Nor does the PTP "guide" (I don't know the exact name, I'd have to check at work) of this PHY. Besides the timestamp there's also the sequence number and the source port id which would need to be read atomically together with the timestamp.

Maybe the part is not intended to be used at all in this way?

AFAICT, PHYs like this are meant to feed a "PTP frame detected" pulse into the time stamping unit on the attached MAC. The interrupt serves to allow the SW to gather the matching fields from the frame.

Thanks,
Richard

maintainer
#2 lore raw

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

In reply to Michael Walle
Quoted 4 lines · Michael Walle

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

Quoted 9 lines

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().

We have phy_driver callback handle_interrupt for custom interrupt handlers. Any specific reason why you can't use it for your purposes?

Quoted 13 lines
---
 drivers/net/phy/phy.c        | 15 +++++++++++++++
 drivers/net/phy/phy_device.c |  6 ++++--
 include/linux/phy.h          |  2 ++
 3 files changed, 21 insertions(+), 2 deletions(-)
drivers/net/phy/phy.c +0 -0
index d76e038cf2cb..f25aacbcf1d9 100644
@@ -942,6 +942,21 @@ void phy_mac_interrupt(struct phy_device *phydev)
 }

drivers/net/phy/phy.c void phy_mac_interrupt(struct phy_device *phydev) · line 952

Quoted 10 lines
drivers/net/phy/phy.c +8 -0
 EXPORT_SYMBOL(phy_mac_interrupt);
 
+/**
+ * phy_drv_interrupt - PHY drivers says the link has changed
+ * @phydev: phy_device struct with changed link
+ *
+ * The PHY driver may implement his own interrupt handler. It will call this
+ * function to notify us about a link change. Trigger the state machine and
+ * work a work queue.
+ */

This function would duplicate phy_mac_interrupt().

Quoted 15 lines
drivers/net/phy/phy.c +7 -0
+void phy_drv_interrupt(struct phy_device *phydev)
+{
+	/* Trigger a state machine change */
+	phy_trigger_machine(phydev);
+}
+EXPORT_SYMBOL(phy_drv_interrupt);
+
 static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
 {
 	linkmode_zero(advertising);
drivers/net/phy/phy_device.c +0 -0
index 6a5056e0ae77..6d8c94e61251 100644

drivers/net/phy/phy_device.c int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, · line 970

Quoted 7 lines
drivers/net/phy/phy_device.c +2 -1
@@ -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);

Here most likely a ! is missing. because as-is you would break current phylib interrupt mode. Where in the PHY driver (in which callback) do you want to register your own interrupt handler?

Quoted 33 lines
drivers/net/phy/phy_device.c +2 -1
 	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 +2 -0
index c570e162e05e..46f73b94fd60 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 */
@@ -1235,6 +1236,7 @@ int phy_drivers_register(struct phy_driver *new_driver, int n,
 void phy_state_machine(struct work_struct *work);
 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
 void phy_mac_interrupt(struct phy_device *phydev);
+void phy_drv_interrupt(struct phy_device *phydev);
 void phy_start_machine(struct phy_device *phydev);
 void phy_stop_machine(struct phy_device *phydev);
 void phy_ethtool_ksettings_get(struct phy_device *phydev,
#3 lore raw

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

Am 2020-02-26 08:27, schrieb Heiner Kallweit:

Quoted 10 lines · Michael Walle
Quoted 9 lines

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.

Quoted 7 lines
Quoted 4 lines

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().

We have phy_driver callback handle_interrupt for custom interrupt handlers. Any specific reason why you can't use it for your purposes?

Yes, as mentioned above this wont work for PHYs which has a clear-on-read status register, because you may loose interrupts between handle_interrupt() and phy_clear_interrupt().

See also

https://lore.kernel.org/netdev/[email protected]/

And esp. Russell reply. I tried using handle_interrupt() but it won't work unless you make the ack_interrupt a NOOP. but even then it won't work because the ack_interrupt is also used during setup etc.

Quoted 15 lines
Quoted 15 lines
---
 drivers/net/phy/phy.c        | 15 +++++++++++++++
 drivers/net/phy/phy_device.c |  6 ++++--
 include/linux/phy.h          |  2 ++
 3 files changed, 21 insertions(+), 2 deletions(-)
drivers/net/phy/phy.c +0 -0
index d76e038cf2cb..f25aacbcf1d9 100644
@@ -942,6 +942,21 @@ void phy_mac_interrupt(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(phy_mac_interrupt);
 
Quoted 12 lines
Quoted 10 lines
drivers/net/phy/phy.c +5 -0
+/**
+ * phy_drv_interrupt - PHY drivers says the link has changed
+ * @phydev: phy_device struct with changed link
+ *
+ * The PHY driver may implement his own interrupt handler. It will 

call this
+ * function to notify us about a link change. Trigger the state machine and
+ * work a work queue.
+ */

This function would duplicate phy_mac_interrupt().

Yes I wasn't sure, if I should just call that function of if it might change in the future. Also the description doesn't fit. Renaming the function sounded also bad, because its an exported symbol.

I'm open to suggestions ;)

Quoted 20 lines
Quoted 20 lines
drivers/net/phy/phy.c +7 -0
+void phy_drv_interrupt(struct phy_device *phydev)
+{
+	/* Trigger a state machine change */
+	phy_trigger_machine(phydev);
+}
+EXPORT_SYMBOL(phy_drv_interrupt);
+
 static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 

eee_adv)
{

drivers/net/phy/phy.c +0 -0
 	linkmode_zero(advertising);
drivers/net/phy/phy_device.c +0 -0
index 6a5056e0ae77..6d8c94e61251 100644
@@ -965,7 +965,8 @@ int phy_connect_direct(struct net_device *dev, 
 struct phy_device *phydev,
 		return rc;
 
Quoted 8 lines

drivers/net/phy/phy_device.c int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, · line 970

Quoted 5 lines
drivers/net/phy/phy_device.c +2 -1
 	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);

Here most likely a ! is missing. because as-is you would break current phylib interrupt mode.

whoops you're right. nice catch.

Quoted 2 lines

Where in the PHY driver (in which
callback) do you want to register your own interrupt handler?

In the _probe() see patch 2/2.

-michael

Quoted 35 lines
Quoted 35 lines
drivers/net/phy/phy_device.c +2 -1
 	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 +2 -0
index c570e162e05e..46f73b94fd60 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 */
@@ -1235,6 +1236,7 @@ int phy_drivers_register(struct phy_driver 
 *new_driver, int n,
 void phy_state_machine(struct work_struct *work);
 void phy_queue_state_machine(struct phy_device *phydev, unsigned long 
 jiffies);
 void phy_mac_interrupt(struct phy_device *phydev);
+void phy_drv_interrupt(struct phy_device *phydev);
 void phy_start_machine(struct phy_device *phydev);

void phy_stop_machine(struct phy_device *phydev); void phy_ethtool_ksettings_get(struct phy_device *phydev,

#10 lore raw
In reply to Richard Cochran

Am 2020-02-26 03:54, schrieb Richard Cochran:

Quoted 5 lines · Michael Walle
Quoted 2 lines

Am 26. Februar 2020 00:50:40 MEZ schrieb Andrew Lunn <[email protected]>:

Quoted 1 line

That sounds fundamentally broken.

Right. It can't work unless the PHY latches the time stamp.

To make things worse, it only has one slot for RX and one slot for TX timestamps.

Quoted 7 lines
Quoted 6 lines

This might be the case, but the datasheet (some older revision can be found on the internet, maybe you find something) doesn't mention it. Nor does the PTP "guide" (I don't know the exact name, I'd have to check at work) of this PHY. Besides the timestamp there's also the sequence number and the source port id which would need to be read atomically together with the timestamp.

Quoted 5 lines

Maybe the part is not intended to be used at all in this way?

AFAICT, PHYs like this are meant to feed a "PTP frame detected" pulse into the time stamping unit on the attached MAC. The interrupt serves to allow the SW to gather the matching fields from the frame.

But then there would need to be such a hardware pin, correct? Unless you'd misuse the INT# for it. Also, why should the PHY then have a PHC which can be adjusted.

Quoted 9 lines
Quoted 4 lines

That sounds fundamentally broken. Which would be odd. Sometimes there is a way to take a snapshot of the value. Reading the first word could trigger this snapshot. Or the last word, or some status register. One would hope the datasheet would talk about this.

This might be the case, but the datasheet (some older revision can be found on the internet, maybe you find something) doesn't mention it. Nor does the PTP "guide" (I don't know the exact name, I'd have to check at work).

BTW, the name of the document is "AR8031 1588v2 Precision Time Protocol, Application Note, 80-Y0618-15 Rev. A", which describes a use case where the RTC (ie PHC) is in the AR8031. I don't argue that the PHY is not broken, only that Atheros at least intended to have that use case.

-michael

#11 lore raw
Quoted 3 lines · Michael Walle

But then there would need to be such a hardware pin, correct? Unless you'd misuse the INT# for it. Also, why should the PHY then have a PHC which can be adjusted.

I see. Oh well. I'll just have to remember the AR8031 on my list of "ptp hardware to be avoided".

:(

Richard

maintainer
#4 lore raw

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

In reply to Michael Walle
Quoted 23 lines · Michael Walle

Am 2020-02-26 08:27, schrieb Heiner Kallweit:

Quoted 10 lines · Michael Walle
Quoted 9 lines

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.

Quoted 7 lines
Quoted 4 lines

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().

We have phy_driver callback handle_interrupt for custom interrupt handlers. Any specific reason why you can't use it for your purposes?

Yes, as mentioned above this wont work for PHYs which has a clear-on-read status register, because you may loose interrupts between handle_interrupt() and phy_clear_interrupt().

Quoted 8 lines

See also

https://lore.kernel.org/netdev/[email protected]/

And esp. Russell reply. I tried using handle_interrupt() but it won't work unless you make the ack_interrupt a NOOP. but even then it won't work because the ack_interrupt is also used during setup etc.

Right, now I remember .. So far we have only one user of the handle_interrupt callback. Following a proposal from the quoted discussion, can you base your patch on the following and check?
Note: Even though you implement handle_interrupt, you still have to implement ack_interrupt too.

---
 drivers/net/phy/mscc.c | 3 ++-
 drivers/net/phy/phy.c  | 4 ++--
 include/linux/phy.h    | 4 +++-
 3 files changed, 7 insertions(+), 4 deletions(-)
drivers/net/phy/mscc.c +2 -1
index 937ac7da2..20b9d3ef5 100644
@@ -2868,7 +2868,8 @@ static int vsc8584_handle_interrupt(struct phy_device *phydev)
 #endif
 
 	phy_mac_interrupt(phydev);
-	return 0;
+
+	return vsc85xx_ack_interrupt(phydev);
 }
 
 static int vsc85xx_config_init(struct phy_device *phydev)
drivers/net/phy/phy.c +2 -2
index d76e038cf..de52f0e82 100644
@@ -725,10 +725,10 @@ static irqreturn_t phy_interrupt(int irq, void *phy_dat)
 	} else {
 		/* reschedule state queue work to run as soon as possible */
 		phy_trigger_machine(phydev);
+		if (phy_clear_interrupt(phydev))
+			goto phy_err;
 	}
 
-	if (phy_clear_interrupt(phydev))
-		goto phy_err;
 	return IRQ_HANDLED;
 
 phy_err:
include/linux/phy.h +3 -1
index 80f8b2158..9e2895ee4 100644
@@ -560,7 +560,9 @@ struct phy_driver {
 	 */
 	int (*did_interrupt)(struct phy_device *phydev);
 
-	/* Override default interrupt handling */
+	/* Override default interrupt handling. Handler has to ensure
+	 * that interrupt is ack'ed.
+	 */
 	int (*handle_interrupt)(struct phy_device *phydev);
 
 	/* Clears up any memory if needed */
Signature · 1 line
2.25.1
#5 lore raw

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

Am 2020-02-26 22:17, schrieb Heiner Kallweit:

Quoted 32 lines · Michael Walle
Quoted 23 lines

Am 2020-02-26 08:27, schrieb Heiner Kallweit:

Quoted 10 lines · Michael Walle
Quoted 9 lines

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.

Quoted 7 lines
Quoted 4 lines

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().

We have phy_driver callback handle_interrupt for custom interrupt handlers. Any specific reason why you can't use it for your purposes?

Yes, as mentioned above this wont work for PHYs which has a clear-on-read status register, because you may loose interrupts between handle_interrupt() and phy_clear_interrupt().

Quoted 8 lines

See also

https://lore.kernel.org/netdev/[email protected]/

And esp. Russell reply. I tried using handle_interrupt() but it won't work unless you make the ack_interrupt a NOOP. but even then it won't work because the ack_interrupt is also used during setup etc.

Quoted 5 lines

Right, now I remember .. So far we have only one user of the handle_interrupt callback. Following a proposal from the quoted discussion, can you base your patch on the following and check?
Note: Even though you implement handle_interrupt, you still have to implement ack_interrupt too.

I guess that should work, I can give that a try. But I'm also preparing support for the BCM54140, a quad PHY. I guess we have the same problem with did_interrupt(). Eg. to tell if there actually was an interrupt from that PHY you have to read the status register which clears the interrupt. So handle_interrupt() will left with no actual interrupt pending bits. I've had a look at how the vsc8584 does it as it also uses did_interrupt() together with handle_interrupt() and it looks like it only works because handle_interrupt() doesn't actually read the pending bits again. Also I guess there is a chance of missing interrupts between vsc8584_did_interrupt() and vsc85xx_ack_interrupt() which both clears interrupts. Although I'm not sure if that matters for the current implementation.

-michael

Quoted 56 lines

---
 drivers/net/phy/mscc.c | 3 ++-
 drivers/net/phy/phy.c  | 4 ++--
 include/linux/phy.h    | 4 +++-
 3 files changed, 7 insertions(+), 4 deletions(-)
drivers/net/phy/mscc.c +2 -1
index 937ac7da2..20b9d3ef5 100644
@@ -2868,7 +2868,8 @@ static int vsc8584_handle_interrupt(struct
 phy_device *phydev)
 #endif
 
 	phy_mac_interrupt(phydev);
-	return 0;
+
+	return vsc85xx_ack_interrupt(phydev);
 }
 

static int vsc85xx_config_init(struct phy_device *phydev)

drivers/net/phy/phy.c +2 -2
index d76e038cf..de52f0e82 100644
@@ -725,10 +725,10 @@ static irqreturn_t phy_interrupt(int irq, void 
 *phy_dat)
 	} else {
 		/* reschedule state queue work to run as soon as possible */
 		phy_trigger_machine(phydev);
+		if (phy_clear_interrupt(phydev))
+			goto phy_err;
 	}
 
-	if (phy_clear_interrupt(phydev))
-		goto phy_err;
 	return IRQ_HANDLED;
 

phy_err:

include/linux/phy.h +3 -1
index 80f8b2158..9e2895ee4 100644
@@ -560,7 +560,9 @@ struct phy_driver {
 	 */
 	int (*did_interrupt)(struct phy_device *phydev);
 
-	/* Override default interrupt handling */
+	/* Override default interrupt handling. Handler has to ensure
+	 * that interrupt is ack'ed.
+	 */
 	int (*handle_interrupt)(struct phy_device *phydev);
 
 	/* Clears up any memory if needed */