net: phy: fixes to PHY_HALTED handling
Subsystems (3)
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
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.
--- drivers/net/phy/phy.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/phy/phy.c +1 -1
@@ -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
PATCH 2/2 net: phy: ensure autoneg is configured when resuming a phydev
In reply to Anssi HannulaWhen 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
@@ -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
@@ -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
Heiner Kallweit
maintainerRe: [PATCH 2/2] net: phy: ensure autoneg is configured when resuming a phydev
Quoted 7 lines · Anssi Hannula
When a
PHY_HALTEDphydev is resumed byphy_start(), it is set toPHY_RESUMINGto 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 byphy_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_UPinstead ofPHY_RESUMINGif autonegotiation has never been configured.Signed-off-by Anssi Hannula <[email protected]>---
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@@ -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@@ -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;
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_HALTEDphydev is resumed byphy_start(), it is set toPHY_RESUMINGto 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 byphy_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 callphy_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()andphy_stop()should be extended to set the state toPHY_HALTEDonly if the PHY is in a started state, means:
don't touch the state if it's DOWN, READY, or HALTED.
In case ofphy_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=2Quoted 57 lines
Fix that by going to
PHY_UPinstead ofPHY_RESUMINGif autonegotiation has never been configured.Signed-off-by Anssi Hannula <[email protected]>---
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@@ -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@@ -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
Heiner Kallweit
maintainerRe: [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_HALTEDphydev is resumed byphy_start(), it is set toPHY_RESUMINGto 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 byphy_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 callphy_stop()even if the PHY hasn't been started yet.
Quoted 6 lines
Having run
phy_start()does not guarantee that thephy_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 nextphy_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()andphy_stop()should be extended to set the state toPHY_HALTEDonly if the PHY is in a started state, means:
don't touch the state if it's DOWN, READY, or HALTED.
In case ofphy_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 afterphy_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=2Quoted 57 lines
Fix that by going to
PHY_UPinstead ofPHY_RESUMINGif autonegotiation has never been configured.Signed-off-by Anssi Hannula <[email protected]>---
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@@ -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@@ -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;
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_HALTEDphydev is resumed byphy_start(), it is set toPHY_RESUMINGto 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 byphy_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 callphy_stop()even if the PHY hasn't been started yet.Having run
phy_start()does not guarantee that thephy_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 nextphy_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()andphy_stop()should be extended to set the state toPHY_HALTEDonly if the PHY is in a started state, means:
don't touch the state if it's DOWN, READY, or HALTED.
In case ofphy_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 afterphy_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()andphy_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=2Quoted 57 lines
Fix that by going to
PHY_UPinstead ofPHY_RESUMINGif autonegotiation has never been configured.Signed-off-by Anssi Hannula <[email protected]>---
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@@ -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@@ -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