Skip to contentSkip to conversation

net: phy: improve starting PHY

PATCH In review Heiner Kallweit 3 participants 7 messages Jan 23, 2019 – Jan 25, 2019

Subsystems (4)
lore
Share & tools
lore

Heiner Kallweit

author maintainer
#0 lore raw

v3 cover net: phy: improve starting PHY

This patch series improves few aspects of starting the PHY.

v2:
- improve a warning in patch 4
v3:
- extend commit message for patch 2

Heiner Kallweit (4):
net: phy: start state machine in phy_start only
net: phy: warn if phy_start is called from invalid state
net: phy: start interrupts in phy_start
net: phy: change phy_start_interrupts to phy_request_interrupt

 drivers/net/phy/phy.c        | 64 ++++++++++++++++++------------------
 drivers/net/phy/phy_device.c |  5 ++-
 drivers/net/phy/phylink.c    |  5 ++-
 include/linux/phy.h          |  2 +-
 4 files changed, 37 insertions(+), 39 deletions(-)
Signature · 1 line
2.20.1
author maintainer
#1 lore raw

PATCH v3 1/4 net: phy: start state machine in phy_start only

The state machine is a no-op before phy_start() has been called. Therefore let's enable it in phy_start() only. In phy_start() let's call phy_start_machine() instead of phy_trigger_machine(). phy_start_machine is an alias for phy_trigger_machine but it makes clearer that we start the state machine here instead of just triggering a run.

---
 drivers/net/phy/phy.c        | 2 +-
 drivers/net/phy/phy_device.c | 1 -
 drivers/net/phy/phylink.c    | 1 -
 3 files changed, 1 insertion(+), 3 deletions(-)
drivers/net/phy/phy.c +1 -1
index 745a705a5..3df6aadc5 100644
@@ -883,7 +883,7 @@ void phy_start(struct phy_device *phydev)
 	}
 	mutex_unlock(&phydev->lock);
 
-	phy_trigger_machine(phydev);
+	phy_start_machine(phydev);
 }
 EXPORT_SYMBOL(phy_start);
 
drivers/net/phy/phy_device.c +0 -1
index e0ceecbed..3e284d596 100644
@@ -944,7 +944,6 @@ int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
 		return rc;
 
 	phy_prepare_link(phydev, handler);
-	phy_start_machine(phydev);
 	if (phydev->irq > 0)
 		phy_start_interrupts(phydev);
 
drivers/net/phy/phylink.c +0 -1
index e7becc737..e9b8f1037 100644
@@ -679,7 +679,6 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
 		   __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
 		   __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
 
-	phy_start_machine(phy);
 	if (phy->irq > 0)
 		phy_start_interrupts(phy);
 
Signature · 1 line
2.20.1
author maintainer
#2 lore raw

PATCH v3 2/4 net: phy: warn if phy_start is called from invalid state

In reply to Heiner Kallweit

phy_start() should be called from states PHY_READY or PHY_HALTED only. Check for this to detect misbehaving drivers. Also the state machine should be started only when being called from one of the valid states.

Some more background:
For all invalid states phy_start() basically was a no-op. All it did was triggering a state machine run, but for all "running" states the poll loop was active anyway. And if called from PHY_DOWN, the state machine does nothing.

v3:
- extended commit message

---
 drivers/net/phy/phy.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/net/phy/phy.c +9 -2
index 3df6aadc5..fd928979b 100644
@@ -861,9 +861,16 @@ void phy_start(struct phy_device *phydev)
 
 	mutex_lock(&phydev->lock);
 
+	if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
+		WARN(1, "called from state %s\n",
+		     phy_state_to_str(phydev->state));
+		goto out;
+	}
+
 	switch (phydev->state) {
 	case PHY_READY:
 		phydev->state = PHY_UP;
+		phy_start_machine(phydev);
 		break;
 	case PHY_HALTED:
 		/* if phy was suspended, bring the physical link up again */
@@ -877,13 +884,13 @@ void phy_start(struct phy_device *phydev)
 		}
 
 		phydev->state = PHY_RESUMING;
+		phy_start_machine(phydev);
 		break;
 	default:
 		break;
 	}
+out:
 	mutex_unlock(&phydev->lock);
-
-	phy_start_machine(phydev);
 }
 EXPORT_SYMBOL(phy_start);
 
Signature · 1 line
2.20.1
author maintainer
#3 lore raw

PATCH v3 3/4 net: phy: start interrupts in phy_start

In reply to Heiner Kallweit

Interrupts don't have to be enabled before calling phy_start(). Therefore let's enable them in phy_start(). In a subsequent step we'll remove enabling interrupts from phy_connect_direct().

---
 drivers/net/phy/phy.c | 34 ++++++++++++++--------------------
 1 file changed, 14 insertions(+), 20 deletions(-)
drivers/net/phy/phy.c +14 -20
index fd928979b..30ba650bb 100644
@@ -857,7 +857,7 @@ EXPORT_SYMBOL(phy_stop);
  */
 void phy_start(struct phy_device *phydev)
 {
-	int err = 0;
+	int err;
 
 	mutex_lock(&phydev->lock);
 
@@ -867,28 +867,22 @@ void phy_start(struct phy_device *phydev)
 		goto out;
 	}
 
-	switch (phydev->state) {
-	case PHY_READY:
-		phydev->state = PHY_UP;
-		phy_start_machine(phydev);
-		break;
-	case PHY_HALTED:
-		/* if phy was suspended, bring the physical link up again */
-		__phy_resume(phydev);
+	/* if phy was suspended, bring the physical link up again */
+	__phy_resume(phydev);
 
-		/* make sure interrupts are re-enabled for the PHY */
-		if (phy_interrupt_is_valid(phydev)) {
-			err = phy_enable_interrupts(phydev);
-			if (err < 0)
-				break;
-		}
+	/* make sure interrupts are enabled for the PHY */
+	if (phy_interrupt_is_valid(phydev)) {
+		err = phy_enable_interrupts(phydev);
+		if (err < 0)
+			goto out;
+	}
 
+	if (phydev->state == PHY_READY)
+		phydev->state = PHY_UP;
+	else
 		phydev->state = PHY_RESUMING;
-		phy_start_machine(phydev);
-		break;
-	default:
-		break;
-	}
+
+	phy_start_machine(phydev);
 out:
 	mutex_unlock(&phydev->lock);
 }
Signature · 1 line
2.20.1
author maintainer
#4 lore raw

PATCH v3 4/4 net: phy: change phy_start_interrupts to phy_request_interrupt

In reply to Heiner Kallweit

Now that we enable the interrupts in phy_start() we don't have to do it before. Therefore remove enabling interrupts from phy_start_interrupts() and rename this function to reflect the changed functionality.

v2:
- improve warning to clearly state that we fall back to polling

---
 drivers/net/phy/phy.c        | 23 +++++++++++------------
 drivers/net/phy/phy_device.c |  4 ++--
 drivers/net/phy/phylink.c    |  4 ++--
 include/linux/phy.h          |  2 +-
 4 files changed, 16 insertions(+), 17 deletions(-)
drivers/net/phy/phy.c +11 -12
index 30ba650bb..58ce3dac2 100644
@@ -790,28 +790,27 @@ static int phy_enable_interrupts(struct phy_device *phydev)
 }
 
 /**
- * phy_start_interrupts - request and enable interrupts for a PHY device
+ * phy_request_interrupt - request interrupt for a PHY device
  * @phydev: target phy_device struct
  *
  * Description: Request the interrupt for the given PHY.
  *   If this fails, then we set irq to PHY_POLL.
- *   Otherwise, we enable the interrupts in the PHY.
  *   This should only be called with a valid IRQ number.
- *   Returns 0 on success or < 0 on error.
  */
-int phy_start_interrupts(struct phy_device *phydev)
+void phy_request_interrupt(struct phy_device *phydev)
 {
-	if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
-				 IRQF_ONESHOT | IRQF_SHARED,
-				 phydev_name(phydev), phydev) < 0) {
-		phydev_warn(phydev, "Can't get IRQ %d\n", phydev->irq);
+	int err;
+
+	err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
+				   IRQF_ONESHOT | IRQF_SHARED,
+				   phydev_name(phydev), phydev);
+	if (err) {
+		phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
+			    err, phydev->irq);
 		phydev->irq = PHY_POLL;
-		return 0;
 	}
-
-	return phy_enable_interrupts(phydev);
 }
-EXPORT_SYMBOL(phy_start_interrupts);
+EXPORT_SYMBOL(phy_request_interrupt);
 
 /**
  * phy_stop - Bring down the PHY link, and stop checking the status
drivers/net/phy/phy_device.c +2 -2
index 3e284d596..dd3a2302f 100644
@@ -944,8 +944,8 @@ int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
 		return rc;
 
 	phy_prepare_link(phydev, handler);
-	if (phydev->irq > 0)
-		phy_start_interrupts(phydev);
+	if (phy_interrupt_is_valid(phydev))
+		phy_request_interrupt(phydev);
 
 	return 0;
 }
drivers/net/phy/phylink.c +2 -2
index e9b8f1037..1ac832ba0 100644
@@ -679,8 +679,8 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy)
 		   __ETHTOOL_LINK_MODE_MASK_NBITS, pl->supported,
 		   __ETHTOOL_LINK_MODE_MASK_NBITS, phy->advertising);
 
-	if (phy->irq > 0)
-		phy_start_interrupts(phy);
+	if (phy_interrupt_is_valid(phy))
+		phy_request_interrupt(phy);
 
 	return 0;
 }
include/linux/phy.h +1 -1
index 1fa7c367b..d0055adbc 100644
@@ -1045,7 +1045,7 @@ void phy_ethtool_ksettings_get(struct phy_device *phydev,
 int phy_ethtool_ksettings_set(struct phy_device *phydev,
 			      const struct ethtool_link_ksettings *cmd);
 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
-int phy_start_interrupts(struct phy_device *phydev);
+void phy_request_interrupt(struct phy_device *phydev);
 void phy_print_status(struct phy_device *phydev);
 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode);
Signature · 1 line
2.20.1
Tested-by on the v3 series
#5 lore raw
In reply to Heiner Kallweit
Quoted 13 lines · Heiner Kallweit

This patch series improves few aspects of starting the PHY.

v2:
- improve a warning in patch 4
v3:
- extend commit message for patch 2

Heiner Kallweit (4):
net: phy: start state machine in phy_start only
net: phy: warn if phy_start is called from invalid state
net: phy: start interrupts in phy_start
net: phy: change phy_start_interrupts to phy_request_interrupt

Quoted 5 lines
 drivers/net/phy/phy.c        | 64 ++++++++++++++++++------------------
 drivers/net/phy/phy_device.c |  5 ++-
 drivers/net/phy/phylink.c    |  5 ++-
 include/linux/phy.h          |  2 +-
 4 files changed, 37 insertions(+), 39 deletions(-)

Changes works well on AMD xgbe hardware.

maintainer
#6 lore raw
In reply to Heiner Kallweit

From: Heiner Kallweit <[email protected]>
Date: Wed, 23 Jan 2019 07:25:38 +0100

Quoted 6 lines

This patch series improves few aspects of starting the PHY.

v2:
- improve a warning in patch 4
v3:
- extend commit message for patch 2

Series applied, thanks Heiner.