Skip to contentSkip to conversation

net: phy: improve handling link_change_notify callback

PATCH In review Heiner Kallweit 8 participants 10 messages Mar 3, 2019 – Mar 15, 2019

Subsystems (4)
lore
Share & tools
lore

Heiner Kallweit

author maintainer
#0 lore raw

PATCH 1/1 net: phy: improve handling link_change_notify callback

Currently the Phy driver's link_change_notify callback is called whenever the state machine is run (every second if polling), no matter whether the state changed or not. This isn't needed and may confuse users considering the name of the callback. Therefore let's change the behavior and call this callback only in case of an actual state change.

This requires changes to the at803x and rockchip drivers. at803x can be simplified so that it reacts on a state change to PHY_NOLINK only.
The rockchip driver can also be much simplified. We simply re-init the AFE/DSP registers whenever we change to PHY_RUNNING and speed is 100Mbps. This causes very small overhead because we do this even if the speed was 100Mbps already. But this is neglectable and I think justified by the much simpler code.

Changes are compile-tested only.

---
 drivers/net/phy/at803x.c   | 26 +++++++++-----------------
 drivers/net/phy/phy.c      |  8 ++++----
 drivers/net/phy/rockchip.c | 31 ++-----------------------------
 3 files changed, 15 insertions(+), 50 deletions(-)
drivers/net/phy/at803x.c +9 -17
index f3e96191e..f315ab468 100644
@@ -324,8 +324,6 @@ static int at803x_config_intr(struct phy_device *phydev)
 
 static void at803x_link_change_notify(struct phy_device *phydev)
 {
-	struct at803x_priv *priv = phydev->priv;
-
 	/*
 	 * Conduct a hardware reset for AT8030 every time a link loss is
 	 * signalled. This is necessary to circumvent a hardware bug that
@@ -333,25 +331,19 @@ static void at803x_link_change_notify(struct phy_device *phydev)
 	 * in the FIFO. In such cases, the FIFO enters an error mode it
 	 * cannot recover from by software.
 	 */
-	if (phydev->state == PHY_NOLINK) {
-		if (phydev->mdio.reset && !priv->phy_reset) {
-			struct at803x_context context;
+	if (phydev->state == PHY_NOLINK && phydev->mdio.reset) {
+		struct at803x_context context;
 
-			at803x_context_save(phydev, &context);
+		at803x_context_save(phydev, &context);
 
-			phy_device_reset(phydev, 1);
-			msleep(1);
-			phy_device_reset(phydev, 0);
-			msleep(1);
+		phy_device_reset(phydev, 1);
+		msleep(1);
+		phy_device_reset(phydev, 0);
+		msleep(1);
 
-			at803x_context_restore(phydev, &context);
+		at803x_context_restore(phydev, &context);
 
-			phydev_dbg(phydev, "%s(): phy was reset\n",
-				   __func__);
-			priv->phy_reset = true;
-		}
-	} else {
-		priv->phy_reset = false;
+		phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
 	}
 }
 
drivers/net/phy/phy.c +4 -4
index 3745220c5..5938c5acf 100644
@@ -891,9 +891,6 @@ void phy_state_machine(struct work_struct *work)
 
 	old_state = phydev->state;
 
-	if (phydev->drv && phydev->drv->link_change_notify)
-		phydev->drv->link_change_notify(phydev);
-
 	switch (phydev->state) {
 	case PHY_DOWN:
 	case PHY_READY:
@@ -940,10 +937,13 @@ void phy_state_machine(struct work_struct *work)
 	if (err < 0)
 		phy_error(phydev);
 
-	if (old_state != phydev->state)
+	if (old_state != phydev->state) {
 		phydev_dbg(phydev, "PHY state change %s -> %s\n",
 			   phy_state_to_str(old_state),
 			   phy_state_to_str(phydev->state));
+		if (phydev->drv && phydev->drv->link_change_notify)
+			phydev->drv->link_change_notify(phydev);
+	}
 
 	/* Only re-schedule a PHY state machine change if we are polling the
 	 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
drivers/net/phy/rockchip.c +2 -29
index 95abf7072..9053b1d01 100644
@@ -104,41 +104,14 @@ static int rockchip_integrated_phy_config_init(struct phy_device *phydev)
 
 static void rockchip_link_change_notify(struct phy_device *phydev)
 {
-	int speed = SPEED_10;
-
-	if (phydev->autoneg == AUTONEG_ENABLE) {
-		int reg = phy_read(phydev, MII_SPECIAL_CONTROL_STATUS);
-
-		if (reg < 0) {
-			phydev_err(phydev, "phy_read err: %d.\n", reg);
-			return;
-		}
-
-		if (reg & MII_SPEED_100)
-			speed = SPEED_100;
-		else if (reg & MII_SPEED_10)
-			speed = SPEED_10;
-	} else {
-		int bmcr = phy_read(phydev, MII_BMCR);
-
-		if (bmcr < 0) {
-			phydev_err(phydev, "phy_read err: %d.\n", bmcr);
-			return;
-		}
-
-		if (bmcr & BMCR_SPEED100)
-			speed = SPEED_100;
-		else
-			speed = SPEED_10;
-	}
-
 	/*
 	 * If mode switch happens from 10BT to 100BT, all DSP/AFE
 	 * registers are set to default values. So any AFE/DSP
 	 * registers have to be re-initialized in this case.
 	 */
-	if ((phydev->speed == SPEED_10) && (speed == SPEED_100)) {
+	if (phydev->state == PHY_RUNNING && phydev->speed == SPEED_100) {
 		int ret = rockchip_integrated_phy_analog_init(phydev);
+
 		if (ret)
 			phydev_err(phydev, "rockchip_integrated_phy_analog_init err: %d.\n",
 				   ret);
Signature · 1 line
2.21.0
maintainer
#1 lore raw

From: Heiner Kallweit <[email protected]>
Date: Sun, 3 Mar 2019 19:58:57 +0100

Quoted 16 lines

Currently the Phy driver's link_change_notify callback is called whenever the state machine is run (every second if polling), no matter whether the state changed or not. This isn't needed and may confuse users considering the name of the callback. Therefore let's change the behavior and call this callback only in case of an actual state change.

This requires changes to the at803x and rockchip drivers. at803x can be simplified so that it reacts on a state change to PHY_NOLINK only.
The rockchip driver can also be much simplified. We simply re-init the AFE/DSP registers whenever we change to PHY_RUNNING and speed is 100Mbps. This causes very small overhead because we do this even if the speed was 100Mbps already. But this is neglectable and I think justified by the much simpler code.

Quoted 3 lines

Changes are compile-tested only.

Someone please review this.

maintainer
#2 lore raw
Quoted 19 lines · David Miller

From: Heiner Kallweit <[email protected]>
Date: Sun, 3 Mar 2019 19:58:57 +0100

Quoted 16 lines

Currently the Phy driver's link_change_notify callback is called whenever the state machine is run (every second if polling), no matter whether the state changed or not. This isn't needed and may confuse users considering the name of the callback. Therefore let's change the behavior and call this callback only in case of an actual state change.

This requires changes to the at803x and rockchip drivers. at803x can be simplified so that it reacts on a state change to PHY_NOLINK only.
The rockchip driver can also be much simplified. We simply re-init the AFE/DSP registers whenever we change to PHY_RUNNING and speed is 100Mbps. This causes very small overhead because we do this even if the speed was 100Mbps already. But this is neglectable and I think justified by the much simpler code.

Quoted 5 lines
Quoted 3 lines

Changes are compile-tested only.

Someone please review this.

Hi David

We should probably wait for a Tested-by: from Daniel Mack <[email protected]> and David Wu <[email protected]>, or equivalent.

Andrew

maintainer
#4 lore raw
In reply to Andrew Lunn

Hi Andrew, Heiner,

Am Montag, 4. März 2019, 21:06:30 CET schrieb Andrew Lunn:

Quoted 24 lines · David Miller
Quoted 23 lines

From: Heiner Kallweit <[email protected]>
Date: Sun, 3 Mar 2019 19:58:57 +0100

Quoted 19 lines

Currently the Phy driver's link_change_notify callback is called whenever the state machine is run (every second if polling), no matter whether the state changed or not. This isn't needed and may confuse users considering the name of the callback. Therefore let's change the behavior and call this callback only in case of an actual state change.

This requires changes to the at803x and rockchip drivers. at803x can be simplified so that it reacts on a state change to PHY_NOLINK only.
The rockchip driver can also be much simplified. We simply re-init the AFE/DSP registers whenever we change to PHY_RUNNING and speed is 100Mbps. This causes very small overhead because we do this even if the speed was 100Mbps already. But this is neglectable and I think justified by the much simpler code.

Changes are compile-tested only.

Quoted 7 lines
Quoted 1 line

Someone please review this.

Hi David

We should probably wait for a Tested-by: from Daniel Mack <[email protected]> and David Wu <[email protected]>, or equivalent.

We should probably add them to the list of recipients if we want tests from them, which I've done now.

@David: patch in question that changes the Rockchip eth-phy is
https://patchwork.kernel.org/patch/10837217/

Sadly I don't have matching hardware to test this myself.

Heiko

#5 lore raw
Quoted 35 lines · Heiko Stuebner

Hi Andrew, Heiner,

Am Montag, 4. März 2019, 21:06:30 CET schrieb Andrew Lunn:

Quoted 24 lines · David Miller
Quoted 23 lines

From: Heiner Kallweit <[email protected]>
Date: Sun, 3 Mar 2019 19:58:57 +0100

Quoted 19 lines

Currently the Phy driver's link_change_notify callback is called whenever the state machine is run (every second if polling), no matter whether the state changed or not. This isn't needed and may confuse users considering the name of the callback. Therefore let's change the behavior and call this callback only in case of an actual state change.

This requires changes to the at803x and rockchip drivers. at803x can be simplified so that it reacts on a state change to PHY_NOLINK only.
The rockchip driver can also be much simplified. We simply re-init the AFE/DSP registers whenever we change to PHY_RUNNING and speed is 100Mbps. This causes very small overhead because we do this even if the speed was 100Mbps already. But this is neglectable and I think justified by the much simpler code.

Changes are compile-tested only.

Quoted 7 lines
Quoted 1 line

Someone please review this.

Hi David

We should probably wait for a Tested-by: from Daniel Mack <[email protected]> and David Wu <[email protected]>, or equivalent.

Quoted 7 lines

We should probably add them to the list of recipients if we want tests from them, which I've done now.

@David: patch in question that changes the Rockchip eth-phy is
https://patchwork.kernel.org/patch/10837217/

Sadly I don't have matching hardware to test this myself.

Thanks for considering me, but I don't have access to that hardware either right now.

The changes look straight forward to me however, so just go ahead an apply this. Once I get back to that setup, I'll report in case of a regression.

Thanks,
Dainel

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#6 lore raw
Quoted 42 lines · Daniel Mack
Quoted 41 lines · Heiko Stuebner

Hi Andrew, Heiner,

Am Montag, 4. März 2019, 21:06:30 CET schrieb Andrew Lunn:

Quoted 24 lines · David Miller
Quoted 23 lines

From: Heiner Kallweit <[email protected]>
Date: Sun, 3 Mar 2019 19:58:57 +0100

Quoted 19 lines

Currently the Phy driver's link_change_notify callback is called whenever the state machine is run (every second if polling), no matter whether the state changed or not. This isn't needed and may confuse users considering the name of the callback. Therefore let's change the behavior and call this callback only in case of an actual state change.

This requires changes to the at803x and rockchip drivers. at803x can be simplified so that it reacts on a state change to PHY_NOLINK only.
The rockchip driver can also be much simplified. We simply re-init the AFE/DSP registers whenever we change to PHY_RUNNING and speed is 100Mbps. This causes very small overhead because we do this even if the speed was 100Mbps already. But this is neglectable and I think justified by the much simpler code.

Changes are compile-tested only.

Quoted 7 lines
Quoted 1 line

Someone please review this.

Hi David

We should probably wait for a Tested-by: from Daniel Mack <[email protected]> and David Wu <[email protected]>, or equivalent.

We should probably add them to the list of recipients if we want tests from them, which I've done now.

@David: patch in question that changes the Rockchip eth-phy is
https://patchwork.kernel.org/patch/10837217/

Quoted 8 lines
Quoted 1 line

Sadly I don't have matching hardware to test this myself.

Thanks for considering me, but I don't have access to that hardware either right now.

The changes look straight forward to me however, so just go ahead an apply this. Once I get back to that setup, I'll report in case of a regression.

SolidRun Hummingboards and Cubox-i use AR8035 with the FEC driver, so would be ideal to test - unfortunately I'm up to my eyeballs trying to diagnose Armada 8040 comphy issues so can't test at the moment.

Signature · 3 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up
According to speedtest.net: 11.9Mbps down 500kbps up
#7 lore raw
In reply to Daniel Mack
Quoted 36 lines · Daniel Mack
Quoted 35 lines · Heiko Stuebner

Hi Andrew, Heiner,

Am Montag, 4. März 2019, 21:06:30 CET schrieb Andrew Lunn:

Quoted 31 lines · David Miller
Quoted 19 lines

From: Heiner Kallweit <[email protected]>
Date: Sun, 3 Mar 2019 19:58:57 +0100

Quoted 16 lines

Currently the Phy driver's link_change_notify callback is called whenever the state machine is run (every second if polling), no matter whether the state changed or not. This isn't needed and may confuse users considering the name of the callback. Therefore let's change the behavior and call this callback only in case of an actual state change.

This requires changes to the at803x and rockchip drivers. at803x can be simplified so that it reacts on a state change to PHY_NOLINK only.
The rockchip driver can also be much simplified. We simply re-init the AFE/DSP registers whenever we change to PHY_RUNNING and speed is 100Mbps. This causes very small overhead because we do this even if the speed was 100Mbps already. But this is neglectable and I think justified by the much simpler code.

Quoted 5 lines
Quoted 3 lines

Changes are compile-tested only.

Someone please review this.

Hi David

We should probably wait for a Tested-by: from Daniel Mack <[email protected]> and David Wu <[email protected]>, or equivalent.

Quoted 5 lines
Quoted 5 lines

We should probably add them to the list of recipients if we want tests from them, which I've done now.

@David: patch in question that changes the Rockchip eth-phy is
https://patchwork.kernel.org/patch/10837217/

I think i can find a hardware to test this.

Quoted 6 lines
Quoted 2 lines

Sadly I don't have matching hardware to test this myself.

Thanks for considering me, but I don't have access to that hardware either right now.

Quoted 10 lines

The changes look straight forward to me however, so just go ahead an apply this. Once I get back to that setup, I'll report in case of a regression.

Thanks,
Dainel

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#8 lore raw
In reply to Heiner Kallweit

From: Heiner Kallweit <[email protected]>
Date: Sun, 3 Mar 2019 19:58:57 +0100

Quoted 16 lines

Currently the Phy driver's link_change_notify callback is called whenever the state machine is run (every second if polling), no matter whether the state changed or not. This isn't needed and may confuse users considering the name of the callback. Therefore let's change the behavior and call this callback only in case of an actual state change.

This requires changes to the at803x and rockchip drivers. at803x can be simplified so that it reacts on a state change to PHY_NOLINK only.
The rockchip driver can also be much simplified. We simply re-init the AFE/DSP registers whenever we change to PHY_RUNNING and speed is 100Mbps. This causes very small overhead because we do this even if the speed was 100Mbps already. But this is neglectable and I think justified by the much simpler code.

Quoted 3 lines

Changes are compile-tested only.

This is still rotting... I've marked it as deferred in patchwork.

This is the kind of patch review and testing scenerio that simply drives me crazy as a maintainer.

Patches should never rot.

#9 lore raw
In reply to Heiner Kallweit
Quoted 16 lines · Heiner Kallweit

Currently the Phy driver's link_change_notify callback is called whenever the state machine is run (every second if polling), no matter whether the state changed or not. This isn't needed and may confuse users considering the name of the callback. Therefore let's change the behavior and call this callback only in case of an actual state change.

This requires changes to the at803x and rockchip drivers. at803x can be simplified so that it reacts on a state change to PHY_NOLINK only.
The rockchip driver can also be much simplified. We simply re-init the AFE/DSP registers whenever we change to PHY_RUNNING and speed is 100Mbps. This causes very small overhead because we do this even if the speed was 100Mbps already. But this is neglectable and I think justified by the much simpler code.

Quoted 3 lines

Changes are compile-tested only.

While you are it, we should probably audit all drivers making use of PHYLIB and see whether the logic to compare the previous link with the current link parameters is still necessary, and if it is, we should consider moving that to the core PHYLIB.

Quoted 132 lines
---
 drivers/net/phy/at803x.c   | 26 +++++++++-----------------
 drivers/net/phy/phy.c      |  8 ++++----
 drivers/net/phy/rockchip.c | 31 ++-----------------------------
 3 files changed, 15 insertions(+), 50 deletions(-)
drivers/net/phy/at803x.c +9 -17
index f3e96191e..f315ab468 100644
@@ -324,8 +324,6 @@ static int at803x_config_intr(struct phy_device *phydev)
 
 static void at803x_link_change_notify(struct phy_device *phydev)
 {
-	struct at803x_priv *priv = phydev->priv;
-
 	/*
 	 * Conduct a hardware reset for AT8030 every time a link loss is
 	 * signalled. This is necessary to circumvent a hardware bug that
@@ -333,25 +331,19 @@ static void at803x_link_change_notify(struct phy_device *phydev)
 	 * in the FIFO. In such cases, the FIFO enters an error mode it
 	 * cannot recover from by software.
 	 */
-	if (phydev->state == PHY_NOLINK) {
-		if (phydev->mdio.reset && !priv->phy_reset) {
-			struct at803x_context context;
+	if (phydev->state == PHY_NOLINK && phydev->mdio.reset) {
+		struct at803x_context context;
 
-			at803x_context_save(phydev, &context);
+		at803x_context_save(phydev, &context);
 
-			phy_device_reset(phydev, 1);
-			msleep(1);
-			phy_device_reset(phydev, 0);
-			msleep(1);
+		phy_device_reset(phydev, 1);
+		msleep(1);
+		phy_device_reset(phydev, 0);
+		msleep(1);
 
-			at803x_context_restore(phydev, &context);
+		at803x_context_restore(phydev, &context);
 
-			phydev_dbg(phydev, "%s(): phy was reset\n",
-				   __func__);
-			priv->phy_reset = true;
-		}
-	} else {
-		priv->phy_reset = false;
+		phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
 	}
 }
 
drivers/net/phy/phy.c +4 -4
index 3745220c5..5938c5acf 100644
@@ -891,9 +891,6 @@ void phy_state_machine(struct work_struct *work)
 
 	old_state = phydev->state;
 
-	if (phydev->drv && phydev->drv->link_change_notify)
-		phydev->drv->link_change_notify(phydev);
-
 	switch (phydev->state) {
 	case PHY_DOWN:
 	case PHY_READY:
@@ -940,10 +937,13 @@ void phy_state_machine(struct work_struct *work)
 	if (err < 0)
 		phy_error(phydev);
 
-	if (old_state != phydev->state)
+	if (old_state != phydev->state) {
 		phydev_dbg(phydev, "PHY state change %s -> %s\n",
 			   phy_state_to_str(old_state),
 			   phy_state_to_str(phydev->state));
+		if (phydev->drv && phydev->drv->link_change_notify)
+			phydev->drv->link_change_notify(phydev);
+	}
 
 	/* Only re-schedule a PHY state machine change if we are polling the
 	 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
drivers/net/phy/rockchip.c +2 -29
index 95abf7072..9053b1d01 100644
@@ -104,41 +104,14 @@ static int rockchip_integrated_phy_config_init(struct phy_device *phydev)
 
 static void rockchip_link_change_notify(struct phy_device *phydev)
 {
-	int speed = SPEED_10;
-
-	if (phydev->autoneg == AUTONEG_ENABLE) {
-		int reg = phy_read(phydev, MII_SPECIAL_CONTROL_STATUS);
-
-		if (reg < 0) {
-			phydev_err(phydev, "phy_read err: %d.\n", reg);
-			return;
-		}
-
-		if (reg & MII_SPEED_100)
-			speed = SPEED_100;
-		else if (reg & MII_SPEED_10)
-			speed = SPEED_10;
-	} else {
-		int bmcr = phy_read(phydev, MII_BMCR);
-
-		if (bmcr < 0) {
-			phydev_err(phydev, "phy_read err: %d.\n", bmcr);
-			return;
-		}
-
-		if (bmcr & BMCR_SPEED100)
-			speed = SPEED_100;
-		else
-			speed = SPEED_10;
-	}
-
 	/*
 	 * If mode switch happens from 10BT to 100BT, all DSP/AFE
 	 * registers are set to default values. So any AFE/DSP
 	 * registers have to be re-initialized in this case.
 	 */
-	if ((phydev->speed == SPEED_10) && (speed == SPEED_100)) {
+	if (phydev->state == PHY_RUNNING && phydev->speed == SPEED_100) {
 		int ret = rockchip_integrated_phy_analog_init(phydev);
+
 		if (ret)
 			phydev_err(phydev, "rockchip_integrated_phy_analog_init err: %d.\n",
 				   ret);
Signature · 1 line
Florian