Skip to contentSkip to conversation

net: phy: fixes to PHY_HALTED handling

PATCH In review Anssi Hannula 2 participants 7 messages Dec 1, 2018 – Dec 4, 2018

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

cover net: phy: fixes to PHY_HALTED handling

Hi all,

Here are a couple of fixes for PHY_HALTED/PHY_RESUMING handling.

On a related note, it feels a bit strange that AFAICS phydevs will only be put to powerdown state (via PHY_HALTED) after the network interface has been brought up and down once. If the ethernet interface is never brought up, the phydev remains powered on (in PHY_READY).

This is because the phydev is only put to PHY_HALTED from phy_stop() and phy_error(), and phy_stop() seems to be normally called only from .ndo_stop().

But I didn't touch that now as I wasn't sure what is the intent there.

Anssi Hannula (2):
net: phy: suspend phydev on PHY_HALTED even if there is no link
net: phy: ensure autoneg is configured when resuming a phydev

 drivers/net/phy/phy.c | 13 +++++++++++--
 include/linux/phy.h   |  2 ++
 2 files changed, 13 insertions(+), 2 deletions(-)
Signature · 1 line
Anssi Hannula / Bitwise Oy
#1 lore raw

PATCH 1/2 net: phy: suspend phydev on PHY_HALTED even if there is no link

When the phydev is put to PHY_HALTED (by e.g. phy_stop()) the state machine suspends the phydev to save power.

However, this is wrongly inside a phydev->link check, causing the phydev not to be suspended if there was no link at the time of stopping it.

Fix that by setting do_suspend regardless of whether there was a link or not.

Fixes be9dad1f9f26 ("net: phy: suspend phydev when going to HALTED")
Cc Sebastian Hesselbarth <[email protected]>
---
 drivers/net/phy/phy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/phy/phy.c +1 -1
index 376a0d8a2b61..d46858694db9 100644
@@ -956,8 +956,8 @@ void phy_state_machine(struct work_struct *work)
 		if (phydev->link) {
 			phydev->link = 0;
 			phy_link_down(phydev, true);
-			do_suspend = true;
 		}
+		do_suspend = true;
 		break;
 	}
 
Signature · 1 line
2.17.2
#2 lore raw

PATCH 2/2 net: phy: ensure autoneg is configured when resuming a phydev

In reply to Anssi Hannula

When a PHY_HALTED phydev is resumed by phy_start(), it is set to PHY_RESUMING to wait for the link to come up.

However, if the phydev was put to PHY_HALTED (by e.g. phy_stop()) before autonegotiation was ever started by phy_state_machine(), autonegotiation remains unconfigured, i.e. phy_config_aneg() is never called.

Fix that by going to PHY_UP instead of PHY_RESUMING if autonegotiation has never been configured.

---

This doesn't feel as clean is I'd like to, though. Maybe there is a better way?

 drivers/net/phy/phy.c | 11 ++++++++++-
 include/linux/phy.h   |  2 ++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/net/phy/phy.c +10 -1
index d46858694db9..7a650cce7599 100644
@@ -553,6 +553,8 @@ int phy_start_aneg(struct phy_device *phydev)
 	if (err < 0)
 		goto out_unlock;
 
+	phydev->autoneg_configured = 1;
+
 	if (phydev->state != PHY_HALTED) {
 		if (AUTONEG_ENABLE == phydev->autoneg) {
 			err = phy_check_link_status(phydev);
@@ -893,7 +895,14 @@ void phy_start(struct phy_device *phydev)
 				break;
 		}
 
-		phydev->state = PHY_RESUMING;
+		/* if autoneg/forcing was never configured, go back to PHY_UP
+		 * to make that happen
+		 */
+		if (!phydev->autoneg_configured)
+			phydev->state = PHY_UP;
+		else
+			phydev->state = PHY_RESUMING;
+
 		break;
 	default:
 		break;
include/linux/phy.h +2 -0
index 8f927246acdb..b306d5ed9d09 100644
@@ -389,6 +389,8 @@ struct phy_device {
 	unsigned loopback_enabled:1;
 
 	unsigned autoneg:1;
+	/* autoneg has been configured at least once */
+	unsigned autoneg_configured:1;
 	/* The most recently read link state */
 	unsigned link:1;
 
Signature · 1 line
2.17.2
maintainer
#3 lore raw

Re: [PATCH 2/2] net: phy: ensure autoneg is configured when resuming a phydev

Quoted 7 lines · Anssi Hannula

When a PHY_HALTED phydev is resumed by phy_start(), it is set to PHY_RESUMING to wait for the link to come up.

However, if the phydev was put to PHY_HALTED (by e.g. phy_stop()) before autonegotiation was ever started by phy_state_machine(), autonegotiation remains unconfigured, i.e. phy_config_aneg() is never called.

phy_stop() should only be called once the PHY was started. But it's right that we don't have full control over it because misbehaving drivers may call phy_stop() even if the PHY hasn't been started yet.

I think phy_error() and phy_stop() should be extended to set the state to PHY_HALTED only if the PHY is in a started state, means:
don't touch the state if it's DOWN, READY, or HALTED.
In case of phy_error() it's more a precaution, because it's used within phylib only and AFAICS it can be triggered from a started state only.

So yes, there is a theoretical issue. But as you wrote already, I think there's a better way to deal with it.

For checking whether PHY is in a started state you could use the helper which is being discussed here:
https://marc.info/?l=linux-netdev&m=154360368104946&w=2

Quoted 57 lines

Fix that by going to PHY_UP instead of PHY_RESUMING if autonegotiation has never been configured.

---

This doesn't feel as clean is I'd like to, though. Maybe there is a better way?

 drivers/net/phy/phy.c | 11 ++++++++++-
 include/linux/phy.h   |  2 ++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/net/phy/phy.c +10 -1
index d46858694db9..7a650cce7599 100644
@@ -553,6 +553,8 @@ int phy_start_aneg(struct phy_device *phydev)
 	if (err < 0)
 		goto out_unlock;
 
+	phydev->autoneg_configured = 1;
+
 	if (phydev->state != PHY_HALTED) {
 		if (AUTONEG_ENABLE == phydev->autoneg) {
 			err = phy_check_link_status(phydev);
@@ -893,7 +895,14 @@ void phy_start(struct phy_device *phydev)
 				break;
 		}
 
-		phydev->state = PHY_RESUMING;
+		/* if autoneg/forcing was never configured, go back to PHY_UP
+		 * to make that happen
+		 */
+		if (!phydev->autoneg_configured)
+			phydev->state = PHY_UP;
+		else
+			phydev->state = PHY_RESUMING;
+
 		break;
 	default:
 		break;
include/linux/phy.h +2 -0
index 8f927246acdb..b306d5ed9d09 100644
@@ -389,6 +389,8 @@ struct phy_device {
 	unsigned loopback_enabled:1;
 
 	unsigned autoneg:1;
+	/* autoneg has been configured at least once */
+	unsigned autoneg_configured:1;
 	/* The most recently read link state */
 	unsigned link:1;
 
#4 lore raw

Re: [PATCH 2/2] net: phy: ensure autoneg is configured when resuming a phydev

Quoted 11 lines · Heiner Kallweit
Quoted 7 lines · Anssi Hannula

When a PHY_HALTED phydev is resumed by phy_start(), it is set to PHY_RESUMING to wait for the link to come up.

However, if the phydev was put to PHY_HALTED (by e.g. phy_stop()) before autonegotiation was ever started by phy_state_machine(), autonegotiation remains unconfigured, i.e. phy_config_aneg() is never called.

phy_stop() should only be called once the PHY was started. But it's right that we don't have full control over it because misbehaving drivers may call phy_stop() even if the PHY hasn't been started yet.

Having run phy_start() does not guarantee that the phy_state_machine() has been run at least once, though.

I was able to reproduce the issue by calling phy_start(); phy_stop(). Then on the next phy_start() autoneg is not configured.

Quoted 5 lines

I think phy_error() and phy_stop() should be extended to set the state to PHY_HALTED only if the PHY is in a started state, means:
don't touch the state if it's DOWN, READY, or HALTED.
In case of phy_error() it's more a precaution, because it's used within phylib only and AFAICS it can be triggered from a started state only.

This wouldn't fix the issue that occurs when phy_stop() is called right after phy_start(), though, as PHY is in UP state then.

Quoted 66 lines

So yes, there is a theoretical issue. But as you wrote already, I think there's a better way to deal with it.

For checking whether PHY is in a started state you could use the helper which is being discussed here:
https://marc.info/?l=linux-netdev&m=154360368104946&w=2

Quoted 57 lines

Fix that by going to PHY_UP instead of PHY_RESUMING if autonegotiation has never been configured.

---

This doesn't feel as clean is I'd like to, though. Maybe there is a better way?

 drivers/net/phy/phy.c | 11 ++++++++++-
 include/linux/phy.h   |  2 ++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/net/phy/phy.c +10 -1
index d46858694db9..7a650cce7599 100644
@@ -553,6 +553,8 @@ int phy_start_aneg(struct phy_device *phydev)
 	if (err < 0)
 		goto out_unlock;
 
+	phydev->autoneg_configured = 1;
+
 	if (phydev->state != PHY_HALTED) {
 		if (AUTONEG_ENABLE == phydev->autoneg) {
 			err = phy_check_link_status(phydev);
@@ -893,7 +895,14 @@ void phy_start(struct phy_device *phydev)
 				break;
 		}
 
-		phydev->state = PHY_RESUMING;
+		/* if autoneg/forcing was never configured, go back to PHY_UP
+		 * to make that happen
+		 */
+		if (!phydev->autoneg_configured)
+			phydev->state = PHY_UP;
+		else
+			phydev->state = PHY_RESUMING;
+
 		break;
 	default:
 		break;
include/linux/phy.h +2 -0
index 8f927246acdb..b306d5ed9d09 100644
@@ -389,6 +389,8 @@ struct phy_device {
 	unsigned loopback_enabled:1;
 
 	unsigned autoneg:1;
+	/* autoneg has been configured at least once */
+	unsigned autoneg_configured:1;
 	/* The most recently read link state */
 	unsigned link:1;
 
Signature · 1 line
Anssi Hannula / Bitwise Oy
maintainer
#5 lore raw

Re: [PATCH 2/2] net: phy: ensure autoneg is configured when resuming a phydev

Quoted 13 lines · Anssi Hannula
Quoted 11 lines · Heiner Kallweit
Quoted 7 lines · Anssi Hannula

When a PHY_HALTED phydev is resumed by phy_start(), it is set to PHY_RESUMING to wait for the link to come up.

However, if the phydev was put to PHY_HALTED (by e.g. phy_stop()) before autonegotiation was ever started by phy_state_machine(), autonegotiation remains unconfigured, i.e. phy_config_aneg() is never called.

phy_stop() should only be called once the PHY was started. But it's right that we don't have full control over it because misbehaving drivers may call phy_stop() even if the PHY hasn't been started yet.

Quoted 6 lines

Having run phy_start() does not guarantee that the phy_state_machine() has been run at least once, though.

I was able to reproduce the issue by calling phy_start(); phy_stop(). Then on the next phy_start() autoneg is not configured.

Right, phy_start() schedules the state machine run, so there is a small window where this can happen. Did you experience this in any real-life scenario?

Quoted 9 lines
Quoted 5 lines

I think phy_error() and phy_stop() should be extended to set the state to PHY_HALTED only if the PHY is in a started state, means:
don't touch the state if it's DOWN, READY, or HALTED.
In case of phy_error() it's more a precaution, because it's used within phylib only and AFAICS it can be triggered from a started state only.

This wouldn't fix the issue that occurs when phy_stop() is called right after phy_start(), though, as PHY is in UP state then.

After having removed state AN I was thinking already whether we really need to have separate states READY and HALTED. I think we wouldn't have this scenario if phy_stop() and phy_error() would set the state to READY. Merging the two states requires some upfront work, but I have some ideas in the back of my mind.
Overall this should be cleaner than the proposed workaround.

Quoted 67 lines
Quoted 66 lines

So yes, there is a theoretical issue. But as you wrote already, I think there's a better way to deal with it.

For checking whether PHY is in a started state you could use the helper which is being discussed here:
https://marc.info/?l=linux-netdev&m=154360368104946&w=2

Quoted 57 lines

Fix that by going to PHY_UP instead of PHY_RESUMING if autonegotiation has never been configured.

---

This doesn't feel as clean is I'd like to, though. Maybe there is a better way?

 drivers/net/phy/phy.c | 11 ++++++++++-
 include/linux/phy.h   |  2 ++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/net/phy/phy.c +10 -1
index d46858694db9..7a650cce7599 100644
@@ -553,6 +553,8 @@ int phy_start_aneg(struct phy_device *phydev)
 	if (err < 0)
 		goto out_unlock;
 
+	phydev->autoneg_configured = 1;
+
 	if (phydev->state != PHY_HALTED) {
 		if (AUTONEG_ENABLE == phydev->autoneg) {
 			err = phy_check_link_status(phydev);
@@ -893,7 +895,14 @@ void phy_start(struct phy_device *phydev)
 				break;
 		}
 
-		phydev->state = PHY_RESUMING;
+		/* if autoneg/forcing was never configured, go back to PHY_UP
+		 * to make that happen
+		 */
+		if (!phydev->autoneg_configured)
+			phydev->state = PHY_UP;
+		else
+			phydev->state = PHY_RESUMING;
+
 		break;
 	default:
 		break;
include/linux/phy.h +2 -0
index 8f927246acdb..b306d5ed9d09 100644
@@ -389,6 +389,8 @@ struct phy_device {
 	unsigned loopback_enabled:1;
 
 	unsigned autoneg:1;
+	/* autoneg has been configured at least once */
+	unsigned autoneg_configured:1;
 	/* The most recently read link state */
 	unsigned link:1;
 
#6 lore raw

Re: [PATCH 2/2] net: phy: ensure autoneg is configured when resuming a phydev

Quoted 16 lines · Heiner Kallweit
Quoted 15 lines · Anssi Hannula
Quoted 11 lines · Heiner Kallweit
Quoted 7 lines · Anssi Hannula

When a PHY_HALTED phydev is resumed by phy_start(), it is set to PHY_RESUMING to wait for the link to come up.

However, if the phydev was put to PHY_HALTED (by e.g. phy_stop()) before autonegotiation was ever started by phy_state_machine(), autonegotiation remains unconfigured, i.e. phy_config_aneg() is never called.

phy_stop() should only be called once the PHY was started. But it's right that we don't have full control over it because misbehaving drivers may call phy_stop() even if the PHY hasn't been started yet.

Having run phy_start() does not guarantee that the phy_state_machine() has been run at least once, though.

Quoted 6 lines
Quoted 3 lines

I was able to reproduce the issue by calling phy_start(); phy_stop(). Then on the next phy_start() autoneg is not configured.

Right, phy_start() schedules the state machine run, so there is a small window where this can happen. Did you experience this in any real-life scenario?

No, I encountered this while tinkering with phy_start/stop() to look how they interact with suspending phydevs as I wanted to suspend unused ones.

So I don't think there is any rush to fix this one.

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

I think phy_error() and phy_stop() should be extended to set the state to PHY_HALTED only if the PHY is in a started state, means:
don't touch the state if it's DOWN, READY, or HALTED.
In case of phy_error() it's more a precaution, because it's used within phylib only and AFAICS it can be triggered from a started state only.

This wouldn't fix the issue that occurs when phy_stop() is called right after phy_start(), though, as PHY is in UP state then.

Quoted 6 lines

After having removed state AN I was thinking already whether we really need to have separate states READY and HALTED. I think we wouldn't have this scenario if phy_stop() and phy_error() would set the state to READY. Merging the two states requires some upfront work, but I have some ideas in the back of my mind.
Overall this should be cleaner than the proposed workaround.

Yeah, sounds better indeed.

Quoted 65 lines
Quoted 65 lines
Quoted 65 lines

So yes, there is a theoretical issue. But as you wrote already, I think there's a better way to deal with it.

For checking whether PHY is in a started state you could use the helper which is being discussed here:
https://marc.info/?l=linux-netdev&m=154360368104946&w=2

Quoted 57 lines

Fix that by going to PHY_UP instead of PHY_RESUMING if autonegotiation has never been configured.

---

This doesn't feel as clean is I'd like to, though. Maybe there is a better way?

 drivers/net/phy/phy.c | 11 ++++++++++-
 include/linux/phy.h   |  2 ++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/net/phy/phy.c +10 -1
index d46858694db9..7a650cce7599 100644
@@ -553,6 +553,8 @@ int phy_start_aneg(struct phy_device *phydev)
 	if (err < 0)
 		goto out_unlock;
 
+	phydev->autoneg_configured = 1;
+
 	if (phydev->state != PHY_HALTED) {
 		if (AUTONEG_ENABLE == phydev->autoneg) {
 			err = phy_check_link_status(phydev);
@@ -893,7 +895,14 @@ void phy_start(struct phy_device *phydev)
 				break;
 		}
 
-		phydev->state = PHY_RESUMING;
+		/* if autoneg/forcing was never configured, go back to PHY_UP
+		 * to make that happen
+		 */
+		if (!phydev->autoneg_configured)
+			phydev->state = PHY_UP;
+		else
+			phydev->state = PHY_RESUMING;
+
 		break;
 	default:
 		break;
include/linux/phy.h +2 -0
index 8f927246acdb..b306d5ed9d09 100644
@@ -389,6 +389,8 @@ struct phy_device {
 	unsigned loopback_enabled:1;
 
 	unsigned autoneg:1;
+	/* autoneg has been configured at least once */
+	unsigned autoneg_configured:1;
 	/* The most recently read link state */
 	unsigned link:1;
 
Signature · 1 line
Anssi Hannula / Bitwise Oy