Skip to contentSkip to conversation

net: phy: PHY reset consolidation

PATCH Superseded Florian Fainelli 2 participants 12 messages Dec 6, 2013

Subsystems (7)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover net: phy: PHY reset consolidation

Hi all,

Over the past few months there have been various attempts at doing "wild" PHY resets eventually bypassing the PHY state machine and causing various sort of issues whenever auto-negotiation as not enabled.

This patchset resumes the work Kyle Moffett started here:
https://lkml.org/lkml/2011/10/20/301

Once Sebastian's phy_resume/suspend patchet is in, we can also make sure we consolidate PHY resets after resuming from S2/S3 suspend modes.

Florian Fainelli (9):
net: phy: report link partner features through ethtool
net: phy: use phy_init_hw instead of open-coding it
net: greth: use phy_read_status()
net: bfin_mac: do not reset PHY after phy_start()
net: phy: consolidate PHY reset in phy_init_hw()
net: mv643xx_eth: use phy_init_hw to reset PHY
net: pxa168_eth: use phy_init_hw for PHY reset
net: tc35815: use phy_init_hw for PHY reset
net: sh_eth: do not issue a wild PHY reset through BMCR

 Documentation/networking/phy.txt           |  3 +-
 drivers/net/ethernet/adi/bfin_mac.c        |  1 -
 drivers/net/ethernet/aeroflex/greth.c      |  2 +-
 drivers/net/ethernet/marvell/mv643xx_eth.c | 21 +---------
 drivers/net/ethernet/marvell/pxa168_eth.c  | 19 +--------
 drivers/net/ethernet/renesas/sh_eth.c      |  5 ++-
 drivers/net/ethernet/toshiba/tc35815.c     | 15 ++------
 drivers/net/phy/phy.c                      | 11 +++---
 drivers/net/phy/phy_device.c               | 62 +++++++++++++++++++++++++++++-
 include/linux/phy.h                        |  5 ++-
 10 files changed, 82 insertions(+), 62 deletions(-)
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 1/9 net: phy: report link partner features through ethtool

The PHY library already reads the MII_STAT1000 and MII_LPA registers in genphy_read_status(), so extend it to also populate the PHY device link partner advertised features such that we can feed this back into ethtool when asked for it in phy_ethtool_gset().

---
 drivers/net/phy/phy.c        | 1 +
 drivers/net/phy/phy_device.c | 6 ++++++
 include/linux/phy.h          | 5 +++--
 3 files changed, 10 insertions(+), 2 deletions(-)
drivers/net/phy/phy.c +1 -0
index 36c6994..05cb8fe 100644
@@ -289,6 +289,7 @@ int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
 	cmd->supported = phydev->supported;
 
 	cmd->advertising = phydev->advertising;
+	cmd->lp_advertising = phydev->lp_advertising;
 
 	ethtool_cmd_speed_set(cmd, phydev->speed);
 	cmd->duplex = phydev->duplex;
drivers/net/phy/phy_device.c +6 -0
index d6447b3..6db3659 100644
@@ -839,6 +839,8 @@ int genphy_read_status(struct phy_device *phydev)
 	if (err)
 		return err;
 
+	phydev->lp_advertising = 0;
+
 	if (AUTONEG_ENABLE == phydev->autoneg) {
 		if (phydev->supported & (SUPPORTED_1000baseT_Half
 					| SUPPORTED_1000baseT_Full)) {
@@ -852,6 +854,8 @@ int genphy_read_status(struct phy_device *phydev)
 			if (adv < 0)
 				return adv;
 
+			phydev->lp_advertising =
+				mii_stat1000_to_ethtool_lpa_t(lpagb);
 			lpagb &= adv << 2;
 		}
 
@@ -860,6 +864,8 @@ int genphy_read_status(struct phy_device *phydev)
 		if (lpa < 0)
 			return lpa;
 
+		phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
+
 		adv = phy_read(phydev, MII_ADVERTISE);
 
 		if (adv < 0)
include/linux/phy.h +3 -2
index 48a4dc3..cc2c8bb 100644
@@ -278,8 +278,8 @@ struct phy_c45_device_ids {
  * adjust_state: Callback for the enet driver to respond to
  * changes in the state machine.
  *
- * speed, duplex, pause, supported, advertising, and
- * autoneg are used like in mii_if_info
+ * speed, duplex, pause, supported, advertising, lp_advertising,
+ * and autoneg are used like in mii_if_info
  *
  * interrupts currently only supports enabled or disabled,
  * but could be changed in the future to support enabling
@@ -331,6 +331,7 @@ struct phy_device {
 	/* See mii.h for more info */
 	u32 supported;
 	u32 advertising;
+	u32 lp_advertising;
 
 	int autoneg;
 
Signature · 1 line
1.8.3.2
#2 lore raw

PATCH 2/9 net: phy: use phy_init_hw instead of open-coding it

In reply to Florian Fainelli

Use phy_init_hw() instead of open-coding it in phy_mii_ioctl(), this improves consistenty and makes sure that we will not duplicate the same routine somewhere else.

---
 drivers/net/phy/phy.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/net/phy/phy.c +2 -5
index 05cb8fe..5d7101b 100644
@@ -361,11 +361,8 @@ int phy_mii_ioctl(struct phy_device *phydev,
 			      mii_data->reg_num, val);
 
 		if (mii_data->reg_num == MII_BMCR &&
-		    val & BMCR_RESET &&
-		    phydev->drv->config_init) {
-			phy_scan_fixups(phydev);
-			phydev->drv->config_init(phydev);
-		}
+		    val & BMCR_RESET)
+			phy_init_hw(phydev);
 		break;
 
 	case SIOCSHWTSTAMP:
Signature · 1 line
1.8.3.2
#3 lore raw

PATCH 3/9 net: greth: use phy_read_status()

In reply to Florian Fainelli

In case the greth driver is bound to anything but the Generic PHY driver or the PHY has a special read_status callback implemented, unexpected things will happen. Make sure we that we use phy_read_status() which does the proper abstraction of calling the driver specific read_status() callback for a given PHY.

---
 drivers/net/ethernet/aeroflex/greth.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/aeroflex/greth.c +1 -1
index e066945..b20cbf0 100644
@@ -1361,7 +1361,7 @@ static int greth_mdio_init(struct greth_private *greth)
 		timeout = jiffies + 6*HZ;
 		while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
 		}
-		genphy_read_status(greth->phy);
+		phy_read_status(greth->phy);
 		greth_link_change(greth->netdev);
 	}
 
Signature · 1 line
1.8.3.2
#4 lore raw

PATCH 4/9 net: bfin_mac: do not reset PHY after phy_start()

In reply to Florian Fainelli

The PHY is already reset during driver probing, and this manual reset after calling phy_start() will wipe out board-specific PHY fixups and driver specific configuration initialization. Remove that explicit PHY reset.

---
 drivers/net/ethernet/adi/bfin_mac.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/ethernet/adi/bfin_mac.c +0 -1
index 75fb1d2..2ce382e 100644
@@ -1544,7 +1544,6 @@ static int bfin_mac_open(struct net_device *dev)
 		return ret;
 
 	phy_start(lp->phydev);
-	phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
 	setup_system_regs(dev);
 	setup_mac_addr(dev->dev_addr);
 
Signature · 1 line
1.8.3.2
#5 lore raw

PATCH 5/9 net: phy: consolidate PHY reset in phy_init_hw()

In reply to Florian Fainelli

There are quite a lot of drivers touching a PHY device MII_BMCR register to reset the PHY without taking care of:

1) ensuring that BMCR_RESET is cleared after a given timeout
2) the PHY state machine resuming to the proper state and re-applying potentially changed settings such as auto-negotiation

Introduce phy_poll_reset() which will take care of polling the MII_BMCR for the BMCR_RESET bit to be cleared after a given timeout or return a timeout error code.

In order to make sure the PHY is in a correct state, phy_init_hw() first issues a software reset through MII_BMCR and then applies any fixups.

---
 Documentation/networking/phy.txt |  3 ++-
 drivers/net/phy/phy.c            |  5 ++--
 drivers/net/phy/phy_device.c     | 56 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 60 insertions(+), 4 deletions(-)
Documentation/networking/phy.txt +2 -1
index d5b1a39..ebf2707 100644
@@ -255,7 +255,8 @@ Writing a PHY driver
 
    config_init: configures PHY into a sane state after a reset.
      For instance, a Davicom PHY requires descrambling disabled.
-   probe: Does any setup needed by the driver
+   probe: Allocate phy->priv, optionally refuse to bind.
+   PHY may not have been reset or had fixups run yet.
    suspend/resume: power management
    config_aneg: Changes the speed/duplex/negotiation settings
    read_status: Reads the current speed/duplex/negotiation settings
drivers/net/phy/phy.c +3 -2
index 5d7101b..e3dd691 100644
@@ -318,6 +318,7 @@ int phy_mii_ioctl(struct phy_device *phydev,
 {
 	struct mii_ioctl_data *mii_data = if_mii(ifr);
 	u16 val = mii_data->val_in;
+	int ret = 0;
 
 	switch (cmd) {
 	case SIOCGMIIPHY:
@@ -362,7 +363,7 @@ int phy_mii_ioctl(struct phy_device *phydev,
 
 		if (mii_data->reg_num == MII_BMCR &&
 		    val & BMCR_RESET)
-			phy_init_hw(phydev);
+			ret = phy_init_hw(phydev);
 		break;
 
 	case SIOCSHWTSTAMP:
@@ -374,7 +375,7 @@ int phy_mii_ioctl(struct phy_device *phydev,
 		return -EOPNOTSUPP;
 	}
 
-	return 0;
+	return ret;
 }
 EXPORT_SYMBOL(phy_mii_ioctl);
 
drivers/net/phy/phy_device.c +55 -1

80 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#6 lore raw

PATCH 6/9 net: mv643xx_eth: use phy_init_hw to reset PHY

In reply to Florian Fainelli

Instead of open-coding a PHY reset through the MII BMCR register, use phy_init_hw() which does that for us and will also make sure that PHY fixups are applied if required.

---
 drivers/net/ethernet/marvell/mv643xx_eth.c | 21 +--------------------
 1 file changed, 1 insertion(+), 20 deletions(-)
drivers/net/ethernet/marvell/mv643xx_eth.c +1 -20
index 61088a6..8d9bb6b 100644
@@ -2067,23 +2067,6 @@ static inline void oom_timer_wrapper(unsigned long data)
 	napi_schedule(&mp->napi);
 }
 
-static void phy_reset(struct mv643xx_eth_private *mp)
-{
-	int data;
-
-	data = phy_read(mp->phy, MII_BMCR);
-	if (data < 0)
-		return;
-
-	data |= BMCR_RESET;
-	if (phy_write(mp->phy, MII_BMCR, data) < 0)
-		return;
-
-	do {
-		data = phy_read(mp->phy, MII_BMCR);
-	} while (data >= 0 && data & BMCR_RESET);
-}
-
 static void port_start(struct mv643xx_eth_private *mp)
 {
 	u32 pscr;
@@ -2096,7 +2079,7 @@ static void port_start(struct mv643xx_eth_private *mp)
 		struct ethtool_cmd cmd;
 
 		mv643xx_eth_get_settings(mp->dev, &cmd);
-		phy_reset(mp);
+		phy_init_hw(mp);
 		mv643xx_eth_set_settings(mp->dev, &cmd);
 	}
 
@@ -2764,8 +2747,6 @@ static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
 {
 	struct phy_device *phy = mp->phy;
 
-	phy_reset(mp);
-
 	if (speed == 0) {
 		phy->autoneg = AUTONEG_ENABLE;
 		phy->speed = 0;
Signature · 1 line
1.8.3.2
#7 lore raw

Re: [PATCH 6/9] net: mv643xx_eth: use phy_init_hw to reset PHY

Hello.

Quoted 3 lines · Florian Fainelli

Instead of open-coding a PHY reset through the MII BMCR register, use phy_init_hw() which does that for us and will also make sure that PHY fixups are applied if required.

You failed to mention that you stopped resetting PHY in phy_init()...

Quoted 9 lines
---
  drivers/net/ethernet/marvell/mv643xx_eth.c | 21 +--------------------
  1 file changed, 1 insertion(+), 20 deletions(-)
drivers/net/ethernet/marvell/mv643xx_eth.c +0 -0
index 61088a6..8d9bb6b 100644

[...]

drivers/net/ethernet/marvell/mv643xx_eth.c static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex) · line 2752

Quoted 10 lines
drivers/net/ethernet/marvell/mv643xx_eth.c +0 -2
@@ -2764,8 +2747,6 @@ static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
  {
  	struct phy_device *phy = mp->phy;
 
-	phy_reset(mp);
-
  	if (speed == 0) {
  		phy->autoneg = AUTONEG_ENABLE;
  		phy->speed = 0;

WBR, Sergei

#8 lore raw

PATCH 7/9 net: pxa168_eth: use phy_init_hw for PHY reset

In reply to Florian Fainelli

Instead of open-coding a PHY reset through the MII BMCR register, use phy_init_hw() which does this for us and ensures that PHY device fixups are also applied.

---
 drivers/net/ethernet/marvell/pxa168_eth.c | 19 +------------------
 1 file changed, 1 insertion(+), 18 deletions(-)
drivers/net/ethernet/marvell/pxa168_eth.c +1 -18
index fff6246..b3642a0 100644
@@ -321,23 +321,6 @@ static void ethernet_phy_set_addr(struct pxa168_eth_private *pep, int phy_addr)
 	wrl(pep, PHY_ADDRESS, reg_data);
 }
 
-static void ethernet_phy_reset(struct pxa168_eth_private *pep)
-{
-	int data;
-
-	data = phy_read(pep->phy, MII_BMCR);
-	if (data < 0)
-		return;
-
-	data |= BMCR_RESET;
-	if (phy_write(pep->phy, MII_BMCR, data) < 0)
-		return;
-
-	do {
-		data = phy_read(pep->phy, MII_BMCR);
-	} while (data >= 0 && data & BMCR_RESET);
-}
-
 static void rxq_refill(struct net_device *dev)
 {
 	struct pxa168_eth_private *pep = netdev_priv(dev);
@@ -646,7 +629,7 @@ static void eth_port_start(struct net_device *dev)
 		struct ethtool_cmd cmd;
 
 		pxa168_get_settings(pep->dev, &cmd);
-		ethernet_phy_reset(pep);
+		phy_init_hw(pep);
 		pxa168_set_settings(pep->dev, &cmd);
 	}
 
Signature · 1 line
1.8.3.2
#9 lore raw

Re: [PATCH 7/9] net: pxa168_eth: use phy_init_hw for PHY reset

2013/12/5 Florian Fainelli <[email protected]>:

Quoted 23 lines

Instead of open-coding a PHY reset through the MII BMCR register, use phy_init_hw() which does this for us and ensures that PHY device fixups are also applied.

---
 drivers/net/ethernet/marvell/pxa168_eth.c | 19 +------------------
 1 file changed, 1 insertion(+), 18 deletions(-)
drivers/net/ethernet/marvell/pxa168_eth.c +0 -6
index fff6246..b3642a0 100644
@@ -321,23 +321,6 @@ static void ethernet_phy_set_addr(struct pxa168_eth_private *pep, int phy_addr)
        wrl(pep, PHY_ADDRESS, reg_data);
 }
 
-static void ethernet_phy_reset(struct pxa168_eth_private *pep)
-{
-       int data;
-
-       data = phy_read(pep->phy, MII_BMCR);
-       if (data < 0)

drivers/net/ethernet/marvell/pxa168_eth.c static void ethernet_phy_set_addr(struct pxa168_eth_private *pep, int phy_addr) · line 339

Quoted 10 lines
drivers/net/ethernet/marvell/pxa168_eth.c +0 -10
-               return;
-
-       data |= BMCR_RESET;
-       if (phy_write(pep->phy, MII_BMCR, data) < 0)
-               return;
-
-       do {
-               data = phy_read(pep->phy, MII_BMCR);
-       } while (data >= 0 && data & BMCR_RESET);
-}

I forgot a reference to ethernet_phy_reset() somewhere later in the driver. Will wait for comments until I issue a respin of these patches.

Signature · 1 line
Florian
#10 lore raw

PATCH 8/9 net: tc35815: use phy_init_hw for PHY reset

In reply to Florian Fainelli

Instead of open-coding the PHY reset through MII BMCR, use phy_init_hw() which does that for us and also makes sure that any PHY specific fixups are applied.

---
 drivers/net/ethernet/toshiba/tc35815.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/net/ethernet/toshiba/tc35815.c +4 -11
index 1322546..f6b3212 100644
@@ -1170,19 +1170,12 @@ static int tc35815_tx_full(struct net_device *dev)
 static void tc35815_restart(struct net_device *dev)
 {
 	struct tc35815_local *lp = netdev_priv(dev);
+	int ret;
 
 	if (lp->phy_dev) {
-		int timeout;
-
-		phy_write(lp->phy_dev, MII_BMCR, BMCR_RESET);
-		timeout = 100;
-		while (--timeout) {
-			if (!(phy_read(lp->phy_dev, MII_BMCR) & BMCR_RESET))
-				break;
-			udelay(1);
-		}
-		if (!timeout)
-			printk(KERN_ERR "%s: BMCR reset failed.\n", dev->name);
+		ret = phy_init_hw(lp->phy_dev);
+		if (ret)
+			printk(KERN_ERR "%s: PHY init failed.\n", dev->name);
 	}
 
 	spin_lock_bh(&lp->rx_lock);
Signature · 1 line
1.8.3.2
#11 lore raw

PATCH 9/9 net: sh_eth: do not issue a wild PHY reset through BMCR

In reply to Florian Fainelli

The sh_eth driver issues an uncontrolled PHY reset through the MII register BMCR but fails to wait for the reset to complete, and will also implicitely wipe out all possible PHY fixups applied. Use phy_init_hw() which remedies both problems.

---
 drivers/net/ethernet/renesas/sh_eth.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/ethernet/renesas/sh_eth.c +4 -1
index d256ce1..6761dde 100644
@@ -1704,7 +1704,10 @@ static int sh_eth_phy_start(struct net_device *ndev)
 		return ret;
 
 	/* reset phy - this also wakes it from PDOWN */
-	phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
+	ret = phy_init_hw(mdp->phydev);
+	if (ret)
+		return ret;
+
 	phy_start(mdp->phydev);
 
 	return 0;
Signature · 1 line
1.8.3.2