Skip to contentSkip to conversation

net: phy: Make phy_interface_is_sgmii|rgmii a switch statement

PATCH Superseded Nishanth Menon 3 participants 9 messages Apr 13, 2023 – Apr 25, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover net: phy: Make phy_interface_is_sgmii|rgmii a switch statement

Originally reported by Tom[1], turned out to be that recent commit 75d28899e3e9 ("net: phy: Synchronize PHY interface modes with Linux") reordered the enum definitions which in turn broke the range checks.

we are left with two options:
a) check against explicit values to help reuse as much as possible and let compiler optimize where applicable
or
b) be very explicit in phy drivers and drop these helpers.

I have chosen to go with (a) approach.

Tested on am64x, though the dp83867 is used elsewhere as well.

Nishanth Menon (2):
net: phy: Make phy_interface_is_sgmii a switch statement
net: phy: Make phy_interface_is_rgmii a switch statement

 include/phy.h | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
Signature · 1 line
2.40.0
#1 lore raw

PATCH 1/2 net: phy: Make phy_interface_is_sgmii a switch statement

Recent commit 75d28899e3e9 ("net: phy: Synchronize PHY interface modes with Linux") reordered the enum definitions. This caused the range of enums that this api was checking to go bad.

While it is possible for the phy drivers to practically use the enum's directly, drivers such as dp83867 use this helper to manage the configuration of the phy correctly.

---
 include/phy.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/phy.h +10 -2
index a837fed72352..1c4dc23bc5ba 100644
@@ -373,8 +373,16 @@ static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
  */
 static inline bool phy_interface_is_sgmii(struct phy_device *phydev)
 {
-	return phydev->interface >= PHY_INTERFACE_MODE_SGMII &&
-		phydev->interface <= PHY_INTERFACE_MODE_QSGMII;
+	switch (phydev->interface) {
+	case PHY_INTERFACE_MODE_SGMII:
+	case PHY_INTERFACE_MODE_QUSGMII:
+	case PHY_INTERFACE_MODE_USXGMII:
+	case PHY_INTERFACE_MODE_QSGMII:
+		return 1;
+	default:
+		fallthrough;
+	}
+	return 0;
 }
 
 bool phy_interface_is_ncsi(void);
Signature · 1 line
2.40.0
#2 lore raw

Re: [PATCH 1/2] net: phy: Make phy_interface_is_sgmii a switch statement

Quoted 22 lines · Nishanth Menon

Recent commit 75d28899e3e9 ("net: phy: Synchronize PHY interface modes with Linux") reordered the enum definitions. This caused the range of enums that this api was checking to go bad.

While it is possible for the phy drivers to practically use the enum's directly, drivers such as dp83867 use this helper to manage the configuration of the phy correctly.

---
 include/phy.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/phy.h +0 -0
index a837fed72352..1c4dc23bc5ba 100644
@@ -373,8 +373,16 @@ static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
  */
 static inline bool phy_interface_is_sgmii(struct phy_device *phydev)
 {

include/phy.h static inline bool phy_interface_is_rgmii(struct phy_device *phydev) · line 383

Quoted 10 lines
include/phy.h +8 -2
-	return phydev->interface >= PHY_INTERFACE_MODE_SGMII &&
-		phydev->interface <= PHY_INTERFACE_MODE_QSGMII;
+	switch (phydev->interface) {
+	case PHY_INTERFACE_MODE_SGMII:
+	case PHY_INTERFACE_MODE_QUSGMII:
+	case PHY_INTERFACE_MODE_USXGMII:
+	case PHY_INTERFACE_MODE_QSGMII:
+		return 1;
+	default:
+		fallthrough;

Why not just put the return 0; statement here instead of fallthrough and drop it from after the switch statement?

Quoted 3 lines
include/phy.h +2 -0
+	}
+	return 0;
 }
#3 lore raw

Re: [PATCH 1/2] net: phy: Make phy_interface_is_sgmii a switch statement

Quoted 28 lines · Marek Behún
Quoted 27 lines · Nishanth Menon

Recent commit 75d28899e3e9 ("net: phy: Synchronize PHY interface modes with Linux") reordered the enum definitions. This caused the range of enums that this api was checking to go bad.

While it is possible for the phy drivers to practically use the enum's directly, drivers such as dp83867 use this helper to manage the configuration of the phy correctly.

---
 include/phy.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/phy.h +3 -2
index a837fed72352..1c4dc23bc5ba 100644
@@ -373,8 +373,16 @@ static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
  */
 static inline bool phy_interface_is_sgmii(struct phy_device *phydev)
 {
-	return phydev->interface >= PHY_INTERFACE_MODE_SGMII &&
-		phydev->interface <= PHY_INTERFACE_MODE_QSGMII;
+	switch (phydev->interface) {
+	case PHY_INTERFACE_MODE_SGMII:
+	case PHY_INTERFACE_MODE_QUSGMII:
Quoted 8 lines

include/phy.h static inline bool phy_interface_is_rgmii(struct phy_device *phydev) · line 383

Quoted 5 lines
include/phy.h +5 -0
+	case PHY_INTERFACE_MODE_USXGMII:
+	case PHY_INTERFACE_MODE_QSGMII:
+		return 1;
+	default:
+		fallthrough;

Why not just put the return 0; statement here instead of fallthrough and drop it from after the switch statement?

Just dropping the default also will work, though it does leave something un-handled. handling the default on the other hand allows for additional code (meh?) to be added in default.. But really what? I'd rather drop the default and fall through to save on a couple of lines.. if that is'nt creating confusion..

Quoted 3 lines
Quoted 3 lines
include/phy.h +2 -0
+	}
+	return 0;
 }
Signature · 3 lines
Regards,
Nishanth Menon
Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3  1A34 DDB5 849D 1736 249D
#4 lore raw

Re: [PATCH 1/2] net: phy: Make phy_interface_is_sgmii a switch statement

Quoted 38 lines · Nishanth Menon
Quoted 28 lines · Marek Behún
Quoted 27 lines · Nishanth Menon

Recent commit 75d28899e3e9 ("net: phy: Synchronize PHY interface modes with Linux") reordered the enum definitions. This caused the range of enums that this api was checking to go bad.

While it is possible for the phy drivers to practically use the enum's directly, drivers such as dp83867 use this helper to manage the configuration of the phy correctly.

---
 include/phy.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/phy.h +3 -2
index a837fed72352..1c4dc23bc5ba 100644
@@ -373,8 +373,16 @@ static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
  */
 static inline bool phy_interface_is_sgmii(struct phy_device *phydev)
 {
-	return phydev->interface >= PHY_INTERFACE_MODE_SGMII &&
-		phydev->interface <= PHY_INTERFACE_MODE_QSGMII;
+	switch (phydev->interface) {
+	case PHY_INTERFACE_MODE_SGMII:
+	case PHY_INTERFACE_MODE_QUSGMII:
Quoted 8 lines

include/phy.h static inline bool phy_interface_is_rgmii(struct phy_device *phydev) · line 383

Quoted 5 lines
include/phy.h +5 -0
+	case PHY_INTERFACE_MODE_USXGMII:
+	case PHY_INTERFACE_MODE_QSGMII:
+		return 1;
+	default:
+		fallthrough;

Why not just put the return 0; statement here instead of fallthrough and drop it from after the switch statement?

Quoted 5 lines

Just dropping the default also will work, though it does leave something un-handled. handling the default on the other hand allows for additional code (meh?) to be added in default.. But really what? I'd rather drop the default and fall through to save on a couple of lines.. if that is'nt creating confusion..

You can't drop the default if you use a switch statement and not enumerating all enumerator entries.

So you either have to use

	switch (x) {
	case A:
		return 1;
	case B:
		return 2;
	default:
		return 3;
	}

or

	switch (x) {
	case A:
		return 1;
	case B:
		return 2;
	default:
		break; /* or fallthourgh */
	}
	return 3;

The first one is shorter one line. It is also used in this way already.

IMO it makes more sense.

But keep in mind, this is just a nit-pick. I don't actually care that much. I just think it makes more sense this way. If all possibilities are handled by return statements inside the switch, you don't need to have another return in top level of the function.

Look for example at Linux' include/linux/phy.h function phy_modes():
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/phy.h?h=v6.3-rc6#n216

Marek

Quoted 9 lines
Quoted 4 lines
Quoted 4 lines
include/phy.h +2 -0
+	}
+	return 0;
 }
Signature · 3 lines
Regards,
Nishanth Menon
Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3  1A34 DDB5 849D 1736 249D
#5 lore raw

Re: [PATCH 1/2] net: phy: Make phy_interface_is_sgmii a switch statement

Quoted 71 lines · Marek Behún
Quoted 38 lines · Nishanth Menon
Quoted 28 lines · Marek Behún
Quoted 27 lines · Nishanth Menon

Recent commit 75d28899e3e9 ("net: phy: Synchronize PHY interface modes with Linux") reordered the enum definitions. This caused the range of enums that this api was checking to go bad.

While it is possible for the phy drivers to practically use the enum's directly, drivers such as dp83867 use this helper to manage the configuration of the phy correctly.

---
 include/phy.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/phy.h +3 -2
index a837fed72352..1c4dc23bc5ba 100644
@@ -373,8 +373,16 @@ static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
  */
 static inline bool phy_interface_is_sgmii(struct phy_device *phydev)
 {
-	return phydev->interface >= PHY_INTERFACE_MODE_SGMII &&
-		phydev->interface <= PHY_INTERFACE_MODE_QSGMII;
+	switch (phydev->interface) {
+	case PHY_INTERFACE_MODE_SGMII:
+	case PHY_INTERFACE_MODE_QUSGMII:
Quoted 8 lines

include/phy.h static inline bool phy_interface_is_rgmii(struct phy_device *phydev) · line 383

Quoted 5 lines
include/phy.h +5 -0
+	case PHY_INTERFACE_MODE_USXGMII:
+	case PHY_INTERFACE_MODE_QSGMII:
+		return 1;
+	default:
+		fallthrough;

Why not just put the return 0; statement here instead of fallthrough and drop it from after the switch statement?

Quoted 5 lines

Just dropping the default also will work, though it does leave something un-handled. handling the default on the other hand allows for additional code (meh?) to be added in default.. But really what? I'd rather drop the default and fall through to save on a couple of lines.. if that is'nt creating confusion..

You can't drop the default if you use a switch statement and not enumerating all enumerator entries.

So you either have to use

	switch (x) {
	case A:
		return 1;
	case B:
		return 2;
	default:
		return 3;
	}

or

	switch (x) {
	case A:
		return 1;
	case B:
		return 2;
	default:
		break; /* or fallthourgh */
	}
	return 3;
Quoted 8 lines

The first one is shorter one line. It is also used in this way already.

IMO it makes more sense.

But keep in mind, this is just a nit-pick. I don't actually care that much. I just think it makes more sense this way. If all possibilities are handled by return statements inside the switch, you don't need to have another return in top level of the function.

Sigh, remnants of a few years of misra misery :( - yep, dropping..

new rev incoming..

Quoted 4 lines

Look for example at Linux' include/linux/phy.h function phy_modes():
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/phy.h?h=v6.3-rc6#n216

Signature · 3 lines
Regards,
Nishanth Menon
Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3  1A34 DDB5 849D 1736 249D
#6 lore raw

PATCH 2/2 net: phy: Make phy_interface_is_rgmii a switch statement

In reply to Nishanth Menon

Recent commit 75d28899e3e9 ("net: phy: Synchronize PHY interface modes with Linux") reordered the enum definitions. This exposed a problem in range checking functions to identify the interface type. Though this specific api was'nt impacted (all the RGMII definitions remained within range), this experience should be used to never to have to face this kind of challenge again.

While it is possible for the phy drivers to practically use the enum's directly, drivers such as dp83867, dp83869, marvell, micrel_ksz90x1 etc use the same.

---
 include/phy.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/phy.h +10 -2
index 1c4dc23bc5ba..812694cf4a81 100644
@@ -361,8 +361,16 @@ int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id);
  */
 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
 {
-	return phydev->interface >= PHY_INTERFACE_MODE_RGMII &&
-		phydev->interface <= PHY_INTERFACE_MODE_RGMII_TXID;
+	switch (phydev->interface) {
+	case PHY_INTERFACE_MODE_RGMII:
+	case PHY_INTERFACE_MODE_RGMII_ID:
+	case PHY_INTERFACE_MODE_RGMII_RXID:
+	case PHY_INTERFACE_MODE_RGMII_TXID:
+		return 1;
+	default:
+		fallthrough;
+	}
+	return 0;
 }
 
 /**
Signature · 1 line
2.40.0
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] net: phy: Make phy_interface_is_rgmii a switch statement

Quoted 42 lines · Nishanth Menon

Recent commit 75d28899e3e9 ("net: phy: Synchronize PHY interface modes with Linux") reordered the enum definitions. This exposed a problem in range checking functions to identify the interface type. Though this specific api was'nt impacted (all the RGMII definitions remained within range), this experience should be used to never to have to face this kind of challenge again.

While it is possible for the phy drivers to practically use the enum's directly, drivers such as dp83867, dp83869, marvell, micrel_ksz90x1 etc use the same.

---
 include/phy.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/phy.h +10 -3
index 1c4dc23bc5ba..812694cf4a81 100644
@@ -361,8 +361,16 @@ int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id);
  */
 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
 {
-       return phydev->interface >= PHY_INTERFACE_MODE_RGMII &&
-               phydev->interface <= PHY_INTERFACE_MODE_RGMII_TXID;
+       switch (phydev->interface) {
+       case PHY_INTERFACE_MODE_RGMII:
+       case PHY_INTERFACE_MODE_RGMII_ID:
+       case PHY_INTERFACE_MODE_RGMII_RXID:
+       case PHY_INTERFACE_MODE_RGMII_TXID:
+               return 1;
+       default:
+               fallthrough;
+       }
+       return 0;
 }
 
 /**
--
Quoted 2 lines

2.40.0

#8 lore raw
In reply to Nishanth Menon
Quoted 12 lines · Nishanth Menon

Originally reported by Tom[1], turned out to be that recent commit 75d28899e3e9 ("net: phy: Synchronize PHY interface modes with Linux") reordered the enum definitions which in turn broke the range checks.

we are left with two options:
a) check against explicit values to help reuse as much as possible and let compiler optimize where applicable
or
b) be very explicit in phy drivers and drop these helpers.

I have chosen to go with (a) approach.

Quoted 12 lines

Tested on am64x, though the dp83867 is used elsewhere as well.

Nishanth Menon (2):
net: phy: Make phy_interface_is_sgmii a switch statement
net: phy: Make phy_interface_is_rgmii a switch statement

 include/phy.h | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
Signature · 1 line
2.40.0

Oops.. forgot to provide the link of the report:

[1] https://libera.irclog.whitequark.org/u-boot/2023-04-07#34130437;

Signature · 3 lines
Regards,
Nishanth Menon
Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3  1A34 DDB5 849D 1736 249D