Skip to contentSkip to conversation

introduce mii_bmcr_encode_fixed()

PATCH Applied Russell King (Oracle) 3 participants 6 messages Jun 18, 2022 – Jun 19, 2022

Subsystems (4)
lore
Share & tools
lore

Russell King (Oracle)

author reviewer
#0 lore raw

cover introduce mii_bmcr_encode_fixed()

Hi,

While converting the mv88e6xxx driver to phylink pcs, it has been noticed that we've started to have repeated cases where we convert a speed and duplex to a BMCR value.

Rather than open coding this in multiple locations, let's provide a helper for this - in linux/mii.h. This helper not only takes care of the standard 10, 100 and 1000Mbps encodings, but also includes 2500Mbps (which is the same as 1000Mbps) for those users who require that encoding as well. Unknown speeds will be encoded to 10Mbps, and non-full duplexes will be encoded as half duplex.

This series converts the existing users to the new helper, and the mv88e6xxx conversion will add further users in the 6352 and 639x PCS code.

 drivers/net/pcs/pcs-xpcs.c   | 18 +-----------------
 drivers/net/phy/marvell.c    | 10 ++--------
 drivers/net/phy/phy_device.c | 18 +++---------------
 include/linux/mii.h          | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 41 insertions(+), 40 deletions(-)
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
#1 lore raw

PATCH 1/4 net: mii: add mii_bmcr_encode_fixed()

Add a function to encode a fixed speed/duplex to a BMCR value.

---
 include/linux/mii.h | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
include/linux/mii.h +35 -0
index 5ee13083cec7..d5a959ce4877 100644
@@ -545,4 +545,39 @@ static inline u8 mii_resolve_flowctrl_fdx(u16 lcladv, u16 rmtadv)
 	return cap;
 }
 
+/**
+ * mii_bmcr_encode_fixed - encode fixed speed/duplex settings to a BMCR value
+ * @speed: a SPEED_* value
+ * @duplex: a DUPLEX_* value
+ *
+ * Encode the speed and duplex to a BMCR value. 2500, 1000, 100 and 10 Mbps are
+ * supported. 2500Mbps is encoded to 1000Mbps. Other speeds are encoded as 10
+ * Mbps. Unknown duplex values are encoded to half-duplex.
+ */
+static inline u16 mii_bmcr_encode_fixed(int speed, int duplex)
+{
+	u16 bmcr;
+
+	switch (speed) {
+	case SPEED_2500:
+	case SPEED_1000:
+		bmcr = BMCR_SPEED1000;
+		break;
+
+	case SPEED_100:
+		bmcr = BMCR_SPEED100;
+		break;
+
+	case SPEED_10:
+	default:
+		bmcr = BMCR_SPEED10;
+		break;
+	}
+
+	if (duplex == DUPLEX_FULL)
+		bmcr |= BMCR_FULLDPLX;
+
+	return bmcr;
+}
+
 #endif /* __LINUX_MII_H__ */
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/4 net: phy: use mii_bmcr_encode_fixed()

In reply to Russell King (Oracle)

phylib can make use of the newly introduced mii_bmcr_encode_fixed() macro, so let's convert it over.

---
 drivers/net/phy/phy_device.c | 18 +++---------------
 1 file changed, 3 insertions(+), 15 deletions(-)
drivers/net/phy/phy_device.c +3 -15
index 431a8719c635..7885bceff773 100644
@@ -2001,18 +2001,12 @@ EXPORT_SYMBOL(genphy_config_eee_advert);
  */
 int genphy_setup_forced(struct phy_device *phydev)
 {
-	u16 ctl = 0;
+	u16 ctl;
 
 	phydev->pause = 0;
 	phydev->asym_pause = 0;
 
-	if (SPEED_1000 == phydev->speed)
-		ctl |= BMCR_SPEED1000;
-	else if (SPEED_100 == phydev->speed)
-		ctl |= BMCR_SPEED100;
-
-	if (DUPLEX_FULL == phydev->duplex)
-		ctl |= BMCR_FULLDPLX;
+	ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
 
 	return phy_modify(phydev, MII_BMCR,
 			  ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
@@ -2614,13 +2608,7 @@ int genphy_loopback(struct phy_device *phydev, bool enable)
 		u16 val, ctl = BMCR_LOOPBACK;
 		int ret;
 
-		if (phydev->speed == SPEED_1000)
-			ctl |= BMCR_SPEED1000;
-		else if (phydev->speed == SPEED_100)
-			ctl |= BMCR_SPEED100;
-
-		if (phydev->duplex == DUPLEX_FULL)
-			ctl |= BMCR_FULLDPLX;
+		ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
 
 		phy_modify(phydev, MII_BMCR, ~0, ctl);
 
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/4 net: phy: marvell: use mii_bmcr_encode_fixed()

In reply to Russell King (Oracle)

Make use of the newly introduced mii_bmcr_encode_fixed() to get the BMCR value when setting loopback mode for the 88e1510.

---
 drivers/net/phy/marvell.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/net/phy/marvell.c +2 -8
index d777c8851ed6..a714150f5e8c 100644
@@ -1991,15 +1991,9 @@ static int m88e1510_loopback(struct phy_device *phydev, bool enable)
 	int err;
 
 	if (enable) {
-		u16 bmcr_ctl = 0, mscr2_ctl = 0;
+		u16 bmcr_ctl, mscr2_ctl = 0;
 
-		if (phydev->speed == SPEED_1000)
-			bmcr_ctl = BMCR_SPEED1000;
-		else if (phydev->speed == SPEED_100)
-			bmcr_ctl = BMCR_SPEED100;
-
-		if (phydev->duplex == DUPLEX_FULL)
-			bmcr_ctl |= BMCR_FULLDPLX;
+		bmcr_ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
 
 		err = phy_write(phydev, MII_BMCR, bmcr_ctl);
 		if (err < 0)
Signature · 1 line
2.30.2
#4 lore raw

PATCH 4/4 net: pcs: pcs-xpcs: use mii_bmcr_encode_fixed()

In reply to Russell King (Oracle)

Use the newly introduced mii_bmcr_encode_fixed() for the xpcs driver.

---
 drivers/net/pcs/pcs-xpcs.c | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)
drivers/net/pcs/pcs-xpcs.c +1 -17
index a5d520e34ea3..ab0af1d2531f 100644
@@ -1081,23 +1081,7 @@ static void xpcs_link_up_sgmii(struct dw_xpcs *xpcs, unsigned int mode,
 	if (phylink_autoneg_inband(mode))
 		return;
 
-	switch (speed) {
-	case SPEED_1000:
-		val = BMCR_SPEED1000;
-		break;
-	case SPEED_100:
-		val = BMCR_SPEED100;
-		break;
-	case SPEED_10:
-		val = BMCR_SPEED10;
-		break;
-	default:
-		return;
-	}
-
-	if (duplex == DUPLEX_FULL)
-		val |= BMCR_FULLDPLX;
-
+	val = mii_bmcr_encode_fixed(speed, duplex);
 	ret = xpcs_write(xpcs, MDIO_MMD_VEND2, MDIO_CTRL1, val);
 	if (ret)
 		pr_err("%s: xpcs_write returned %pe\n", __func__, ERR_PTR(ret));
Signature · 1 line
2.30.2
#5 lore raw
In reply to Russell King (Oracle)
This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Quoted 6 lines · On Sat, 18 Jun 2022 11:00:18 +0100 you wrote:

Hi,

While converting the mv88e6xxx driver to phylink pcs, it has been noticed that we've started to have repeated cases where we convert a speed and duplex to a BMCR value.

Quoted 8 lines

Rather than open coding this in multiple locations, let's provide a helper for this - in linux/mii.h. This helper not only takes care of the standard 10, 100 and 1000Mbps encodings, but also includes 2500Mbps (which is the same as 1000Mbps) for those users who require that encoding as well. Unknown speeds will be encoded to 10Mbps, and non-full duplexes will be encoded as half duplex.

[...]

Here is the summary with links:
- [net-next,1/4] net: mii: add mii_bmcr_encode_fixed()

    https://git.kernel.org/netdev/net-next/c/bdb6cfe7512f
  - [net-next,2/4] net: phy: use mii_bmcr_encode_fixed()
    https://git.kernel.org/netdev/net-next/c/f28a602b285e
  - [net-next,3/4] net: phy: marvell: use mii_bmcr_encode_fixed()
    https://git.kernel.org/netdev/net-next/c/e62dbaff4bc2
  - [net-next,4/4] net: pcs: pcs-xpcs: use mii_bmcr_encode_fixed()
    https://git.kernel.org/netdev/net-next/c/449b7a15200a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html