Commit 2b3e88ea authored by Heiner Kallweit's avatar Heiner Kallweit Committed by David S. Miller

net: phy: improve phy state checking

Add helpers phy_is_started() and __phy_is_started() to avoid open-coded
checks whether PHY has been started. To make the check easier move
PHY_HALTED before PHY_UP in enum phy_state. Further improvements:

phy_start_aneg():
Return -EBUSY and print warning if function is called from a non-started
state (DOWN, READY, HALTED). Better check because function is exported
and drivers may use it incorrectly.

phy_interrupt():
Return IRQ_NONE also if state is DOWN or READY. We should never receive
an interrupt in one of these states, but better play safe.

phy_stop():
Just return and print a warning if PHY is in a non-started state.
This warning should help to identify drivers with unbalanced calls to
phy_start() / phy_stop().

phy_state_machine():
Schedule state machine run only if PHY is in a started state.
E.g. if state is READY we don't need the state machine, it will be
started by phy_start().

v2:
- don't use __func__ within phy_warn_state
v3:
- use WARN() instead of printing error message to facilitate debugging
Signed-off-by: default avatarHeiner Kallweit <hkallweit1@gmail.com>
Reviewed-by: default avatarFlorian Fainelli <f.fainelli@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 2429f138
...@@ -543,6 +543,13 @@ int phy_start_aneg(struct phy_device *phydev) ...@@ -543,6 +543,13 @@ int phy_start_aneg(struct phy_device *phydev)
mutex_lock(&phydev->lock); mutex_lock(&phydev->lock);
if (!__phy_is_started(phydev)) {
WARN(1, "called from state %s\n",
phy_state_to_str(phydev->state));
err = -EBUSY;
goto out_unlock;
}
if (AUTONEG_DISABLE == phydev->autoneg) if (AUTONEG_DISABLE == phydev->autoneg)
phy_sanitize_settings(phydev); phy_sanitize_settings(phydev);
...@@ -553,13 +560,11 @@ int phy_start_aneg(struct phy_device *phydev) ...@@ -553,13 +560,11 @@ int phy_start_aneg(struct phy_device *phydev)
if (err < 0) if (err < 0)
goto out_unlock; goto out_unlock;
if (phydev->state != PHY_HALTED) { if (phydev->autoneg == AUTONEG_ENABLE) {
if (AUTONEG_ENABLE == phydev->autoneg) { err = phy_check_link_status(phydev);
err = phy_check_link_status(phydev); } else {
} else { phydev->state = PHY_FORCING;
phydev->state = PHY_FORCING; phydev->link_timeout = PHY_FORCE_TIMEOUT;
phydev->link_timeout = PHY_FORCE_TIMEOUT;
}
} }
out_unlock: out_unlock:
...@@ -709,7 +714,7 @@ void phy_stop_machine(struct phy_device *phydev) ...@@ -709,7 +714,7 @@ void phy_stop_machine(struct phy_device *phydev)
cancel_delayed_work_sync(&phydev->state_queue); cancel_delayed_work_sync(&phydev->state_queue);
mutex_lock(&phydev->lock); mutex_lock(&phydev->lock);
if (phydev->state > PHY_UP && phydev->state != PHY_HALTED) if (__phy_is_started(phydev))
phydev->state = PHY_UP; phydev->state = PHY_UP;
mutex_unlock(&phydev->lock); mutex_unlock(&phydev->lock);
} }
...@@ -760,7 +765,7 @@ static irqreturn_t phy_interrupt(int irq, void *phy_dat) ...@@ -760,7 +765,7 @@ static irqreturn_t phy_interrupt(int irq, void *phy_dat)
{ {
struct phy_device *phydev = phy_dat; struct phy_device *phydev = phy_dat;
if (PHY_HALTED == phydev->state) if (!phy_is_started(phydev))
return IRQ_NONE; /* It can't be ours. */ return IRQ_NONE; /* It can't be ours. */
if (phydev->drv->did_interrupt && !phydev->drv->did_interrupt(phydev)) if (phydev->drv->did_interrupt && !phydev->drv->did_interrupt(phydev))
...@@ -842,15 +847,18 @@ void phy_stop(struct phy_device *phydev) ...@@ -842,15 +847,18 @@ void phy_stop(struct phy_device *phydev)
{ {
mutex_lock(&phydev->lock); mutex_lock(&phydev->lock);
if (PHY_HALTED == phydev->state) if (!__phy_is_started(phydev)) {
goto out_unlock; WARN(1, "called from state %s\n",
phy_state_to_str(phydev->state));
mutex_unlock(&phydev->lock);
return;
}
if (phy_interrupt_is_valid(phydev)) if (phy_interrupt_is_valid(phydev))
phy_disable_interrupts(phydev); phy_disable_interrupts(phydev);
phydev->state = PHY_HALTED; phydev->state = PHY_HALTED;
out_unlock:
mutex_unlock(&phydev->lock); mutex_unlock(&phydev->lock);
phy_state_machine(&phydev->state_queue.work); phy_state_machine(&phydev->state_queue.work);
...@@ -984,7 +992,7 @@ void phy_state_machine(struct work_struct *work) ...@@ -984,7 +992,7 @@ void phy_state_machine(struct work_struct *work)
* state machine would be pointless and possibly error prone when * state machine would be pointless and possibly error prone when
* called from phy_disconnect() synchronously. * called from phy_disconnect() synchronously.
*/ */
if (phy_polling_mode(phydev) && old_state != PHY_HALTED) if (phy_polling_mode(phydev) && phy_is_started(phydev))
phy_queue_state_machine(phydev, PHY_STATE_TIME); phy_queue_state_machine(phydev, PHY_STATE_TIME);
} }
......
...@@ -319,12 +319,12 @@ struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr); ...@@ -319,12 +319,12 @@ struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
enum phy_state { enum phy_state {
PHY_DOWN = 0, PHY_DOWN = 0,
PHY_READY, PHY_READY,
PHY_HALTED,
PHY_UP, PHY_UP,
PHY_RUNNING, PHY_RUNNING,
PHY_NOLINK, PHY_NOLINK,
PHY_FORCING, PHY_FORCING,
PHY_CHANGELINK, PHY_CHANGELINK,
PHY_HALTED,
PHY_RESUMING PHY_RESUMING
}; };
...@@ -669,6 +669,28 @@ phy_lookup_setting(int speed, int duplex, const unsigned long *mask, ...@@ -669,6 +669,28 @@ phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
size_t phy_speeds(unsigned int *speeds, size_t size, size_t phy_speeds(unsigned int *speeds, size_t size,
unsigned long *mask); unsigned long *mask);
static inline bool __phy_is_started(struct phy_device *phydev)
{
WARN_ON(!mutex_is_locked(&phydev->lock));
return phydev->state >= PHY_UP;
}
/**
* phy_is_started - Convenience function to check whether PHY is started
* @phydev: The phy_device struct
*/
static inline bool phy_is_started(struct phy_device *phydev)
{
bool started;
mutex_lock(&phydev->lock);
started = __phy_is_started(phydev);
mutex_unlock(&phydev->lock);
return started;
}
void phy_resolve_aneg_linkmode(struct phy_device *phydev); void phy_resolve_aneg_linkmode(struct phy_device *phydev);
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment