Fix link speed handling for SJA1105 DSA driver
Vladimir Oltean
maintainercover 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
Vladimir Oltean
maintainerPATCH 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.
--- 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
@@ -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
@@ -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
Vladimir Oltean
maintainerPATCH 2/2 net: dsa: sja1105: Fix link speed not working at 100 Mbps and below
In reply to Vladimir OlteanThe 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.
--- drivers/net/dsa/sja1105/sja1105_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/dsa/sja1105/sja1105_main.c +1 -1
@@ -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
Andrew Lunn
maintainerRe: [PATCH net 1/2] net: dsa: sja1105: Force a negative value for enum sja1105_speed_t
In reply to Vladimir OlteanQuoted 11 lines · Vladimir Oltean
The code in
sja1105_adjust_port_configrelies on the fact that an invalid link speed is detected bysja1105_get_speed_cfgand 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
-EINVALvalue 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")Signed-off-by Vladimir Oltean <[email protected]>
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
Vladimir Oltean
maintainerRe: [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_configrelies on the fact that an invalid link speed is detected bysja1105_get_speed_cfgand 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
-EINVALvalue 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")Signed-off-by Vladimir Oltean <[email protected]>
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=-EINVALwhich 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
Andrew Lunn
maintainerRe: [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_configrelies on the fact that an invalid link speed is detected bysja1105_get_speed_cfgand 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
-EINVALvalue 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")Signed-off-by Vladimir Oltean <[email protected]>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=-EINVALwhich feels hackish.
Quoted 6 lines
Quoted 1 line
Andrew
Hi Andrew,
You mean I should completely remove the
sja1105_get_speed_cfgfunction? 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