Skip to content

Commit 2b3e88e

Browse files
hkallweitdavem330
authored andcommitted
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: Heiner Kallweit <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 2429f13 commit 2b3e88e

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

drivers/net/phy/phy.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,13 @@ int phy_start_aneg(struct phy_device *phydev)
543543

544544
mutex_lock(&phydev->lock);
545545

546+
if (!__phy_is_started(phydev)) {
547+
WARN(1, "called from state %s\n",
548+
phy_state_to_str(phydev->state));
549+
err = -EBUSY;
550+
goto out_unlock;
551+
}
552+
546553
if (AUTONEG_DISABLE == phydev->autoneg)
547554
phy_sanitize_settings(phydev);
548555

@@ -553,13 +560,11 @@ int phy_start_aneg(struct phy_device *phydev)
553560
if (err < 0)
554561
goto out_unlock;
555562

556-
if (phydev->state != PHY_HALTED) {
557-
if (AUTONEG_ENABLE == phydev->autoneg) {
558-
err = phy_check_link_status(phydev);
559-
} else {
560-
phydev->state = PHY_FORCING;
561-
phydev->link_timeout = PHY_FORCE_TIMEOUT;
562-
}
563+
if (phydev->autoneg == AUTONEG_ENABLE) {
564+
err = phy_check_link_status(phydev);
565+
} else {
566+
phydev->state = PHY_FORCING;
567+
phydev->link_timeout = PHY_FORCE_TIMEOUT;
563568
}
564569

565570
out_unlock:
@@ -709,7 +714,7 @@ void phy_stop_machine(struct phy_device *phydev)
709714
cancel_delayed_work_sync(&phydev->state_queue);
710715

711716
mutex_lock(&phydev->lock);
712-
if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
717+
if (__phy_is_started(phydev))
713718
phydev->state = PHY_UP;
714719
mutex_unlock(&phydev->lock);
715720
}
@@ -760,7 +765,7 @@ static irqreturn_t phy_interrupt(int irq, void *phy_dat)
760765
{
761766
struct phy_device *phydev = phy_dat;
762767

763-
if (PHY_HALTED == phydev->state)
768+
if (!phy_is_started(phydev))
764769
return IRQ_NONE; /* It can't be ours. */
765770

766771
if (phydev->drv->did_interrupt && !phydev->drv->did_interrupt(phydev))
@@ -842,15 +847,18 @@ void phy_stop(struct phy_device *phydev)
842847
{
843848
mutex_lock(&phydev->lock);
844849

845-
if (PHY_HALTED == phydev->state)
846-
goto out_unlock;
850+
if (!__phy_is_started(phydev)) {
851+
WARN(1, "called from state %s\n",
852+
phy_state_to_str(phydev->state));
853+
mutex_unlock(&phydev->lock);
854+
return;
855+
}
847856

848857
if (phy_interrupt_is_valid(phydev))
849858
phy_disable_interrupts(phydev);
850859

851860
phydev->state = PHY_HALTED;
852861

853-
out_unlock:
854862
mutex_unlock(&phydev->lock);
855863

856864
phy_state_machine(&phydev->state_queue.work);
@@ -984,7 +992,7 @@ void phy_state_machine(struct work_struct *work)
984992
* state machine would be pointless and possibly error prone when
985993
* called from phy_disconnect() synchronously.
986994
*/
987-
if (phy_polling_mode(phydev) && old_state != PHY_HALTED)
995+
if (phy_polling_mode(phydev) && phy_is_started(phydev))
988996
phy_queue_state_machine(phydev, PHY_STATE_TIME);
989997
}
990998

include/linux/phy.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,12 @@ struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
319319
enum phy_state {
320320
PHY_DOWN = 0,
321321
PHY_READY,
322+
PHY_HALTED,
322323
PHY_UP,
323324
PHY_RUNNING,
324325
PHY_NOLINK,
325326
PHY_FORCING,
326327
PHY_CHANGELINK,
327-
PHY_HALTED,
328328
PHY_RESUMING
329329
};
330330

@@ -669,6 +669,28 @@ phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
669669
size_t phy_speeds(unsigned int *speeds, size_t size,
670670
unsigned long *mask);
671671

672+
static inline bool __phy_is_started(struct phy_device *phydev)
673+
{
674+
WARN_ON(!mutex_is_locked(&phydev->lock));
675+
676+
return phydev->state >= PHY_UP;
677+
}
678+
679+
/**
680+
* phy_is_started - Convenience function to check whether PHY is started
681+
* @phydev: The phy_device struct
682+
*/
683+
static inline bool phy_is_started(struct phy_device *phydev)
684+
{
685+
bool started;
686+
687+
mutex_lock(&phydev->lock);
688+
started = __phy_is_started(phydev);
689+
mutex_unlock(&phydev->lock);
690+
691+
return started;
692+
}
693+
672694
void phy_resolve_aneg_linkmode(struct phy_device *phydev);
673695

674696
/**

0 commit comments

Comments
 (0)