Skip to contentSkip to conversation

Fix link speed handling for SJA1105 DSA driver

PATCH Superseded Vladimir Oltean 2 participants 6 messages Jun 1, 2019

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Vladimir Oltean

author maintainer
#0 lore raw

cover Fix link speed handling for SJA1105 DSA driver

This patchset fixes two bugs in the logic handling of the enum sja1105_speed_t which caused link speeds of 10 and 100 Mbps to not be interpreted correctly and thus not be applied to the switch MACs.

Vladimir Oltean (2):
net: dsa: sja1105: Force a negative value for enum sja1105_speed_t
net: dsa: sja1105: Fix link speed not working at 100 Mbps and below

 drivers/net/dsa/sja1105/sja1105.h      | 1 +
 drivers/net/dsa/sja1105/sja1105_main.c | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH 1/2 net: dsa: sja1105: Force a negative value for enum sja1105_speed_t

The code in sja1105_adjust_port_config relies on the fact that an invalid link speed is detected by sja1105_get_speed_cfg and returned as -EINVAL. However storing this into an enum that only has positive members will cast it into an unsigned value, and it will miss the negative check.

So make the -EINVAL value part of the enum, so that it is stored as a signed number and passes the negative check.

Fixes 8aa9ebccae87 ("net: dsa: Introduce driver for NXP SJA1105 5-port L2 switch")
---
 drivers/net/dsa/sja1105/sja1105.h      | 1 +
 drivers/net/dsa/sja1105/sja1105_main.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/net/dsa/sja1105/sja1105.h +1 -0
index 1e6d0f33a663..15df37ec63bf 100644
@@ -160,6 +160,7 @@ typedef enum {
 	SJA1105_SPEED_100MBPS	= 2,
 	SJA1105_SPEED_1000MBPS	= 1,
 	SJA1105_SPEED_AUTO	= 0,
+	SJA1105_SPEED_INVALID	= -EINVAL,
 } sja1105_speed_t;
 
 int sja1105pqrs_setup_rgmii_delay(const void *ctx, int port);
drivers/net/dsa/sja1105/sja1105_main.c +1 -1
index b89d979ba213..12b1af52d84b 100644
@@ -719,7 +719,7 @@ static sja1105_speed_t sja1105_get_speed_cfg(unsigned int speed_mbps)
 	for (i = SJA1105_SPEED_AUTO; i <= SJA1105_SPEED_1000MBPS; i++)
 		if (sja1105_speed[i] == speed_mbps)
 			return i;
-	return -EINVAL;
+	return SJA1105_SPEED_INVALID;
 }
 
 /* Set link speed and enable/disable traffic I/O in the MAC configuration
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH net 1/2] net: dsa: sja1105: Force a negative value for enum sja1105_speed_t

Quoted 11 lines · Vladimir Oltean

The code in sja1105_adjust_port_config relies on the fact that an invalid link speed is detected by sja1105_get_speed_cfg and returned as -EINVAL. However storing this into an enum that only has positive members will cast it into an unsigned value, and it will miss the negative check.

So make the -EINVAL value part of the enum, so that it is stored as a signed number and passes the negative check.

Fixes 8aa9ebccae87 ("net: dsa: Introduce driver for NXP SJA1105 5-port L2 switch")

Hi Vladimir

It seems like just using a switch statement would be simpler, and more likely to be correct. And it would avoid adding SJA1105_SPEED_INVALID = -EINVAL which feels hackish.

Andrew

author maintainer
#3 lore raw

Re: [PATCH net 1/2] net: dsa: sja1105: Force a negative value for enum sja1105_speed_t

Quoted 14 lines · Andrew Lunn
Quoted 11 lines · Vladimir Oltean

The code in sja1105_adjust_port_config relies on the fact that an invalid link speed is detected by sja1105_get_speed_cfg and returned as -EINVAL. However storing this into an enum that only has positive members will cast it into an unsigned value, and it will miss the negative check.

So make the -EINVAL value part of the enum, so that it is stored as a signed number and passes the negative check.

Fixes 8aa9ebccae87 ("net: dsa: Introduce driver for NXP SJA1105 5-port L2 switch")
Quoted 7 lines

Hi Vladimir

It seems like just using a switch statement would be simpler, and more likely to be correct. And it would avoid adding SJA1105_SPEED_INVALID = -EINVAL which feels hackish.

Andrew

Hi Andrew,

You mean I should completely remove the sja1105_get_speed_cfg function? I suppose I can do that, I'm only using it in one place.

Thanks,
-Vladimir

maintainer
#4 lore raw

Re: [PATCH net 1/2] net: dsa: sja1105: Force a negative value for enum sja1105_speed_t

Quoted 21 lines · Vladimir Oltean
Quoted 20 lines · Andrew Lunn
Quoted 11 lines · Vladimir Oltean

The code in sja1105_adjust_port_config relies on the fact that an invalid link speed is detected by sja1105_get_speed_cfg and returned as -EINVAL. However storing this into an enum that only has positive members will cast it into an unsigned value, and it will miss the negative check.

So make the -EINVAL value part of the enum, so that it is stored as a signed number and passes the negative check.

Fixes 8aa9ebccae87 ("net: dsa: Introduce driver for NXP SJA1105 5-port L2 switch")

Hi Vladimir

It seems like just using a switch statement would be simpler, and more likely to be correct. And it would avoid adding SJA1105_SPEED_INVALID = -EINVAL which feels hackish.

Quoted 6 lines
Quoted 1 line

Andrew

Hi Andrew,

You mean I should completely remove the sja1105_get_speed_cfg function? I suppose I can do that, I'm only using it in one place.

I think it is often better to use simple, obviously correct code. This seems to be one example.

Andrew

author maintainer
#5 lore raw

PATCH 2/2 net: dsa: sja1105: Fix link speed not working at 100 Mbps and below

In reply to Vladimir Oltean

The hardware values for link speed are held in the sja1105_speed_t enum. However they do not increase in the order that sja1105_get_speed_cfg was iterating over them (basically from SJA1105_SPEED_AUTO - 0 - to SJA1105_SPEED_1000MBPS - 1 - skipping the other two).

Change the iteration from going through the enum values to going through the sja1105_speed array, which makes sure that all elements are visited regardless of underlying ordering.

Fixes 8aa9ebccae87 ("net: dsa: Introduce driver for NXP SJA1105 5-port L2 switch")
---
 drivers/net/dsa/sja1105/sja1105_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/dsa/sja1105/sja1105_main.c +1 -1
index 12b1af52d84b..c3eab40b0500 100644
@@ -716,7 +716,7 @@ static sja1105_speed_t sja1105_get_speed_cfg(unsigned int speed_mbps)
 {
 	int i;
 
-	for (i = SJA1105_SPEED_AUTO; i <= SJA1105_SPEED_1000MBPS; i++)
+	for (i = 0; i < ARRAY_SIZE(sja1105_speed); i++)
 		if (sja1105_speed[i] == speed_mbps)
 			return i;
 	return SJA1105_SPEED_INVALID;
Signature · 1 line
2.17.1