Skip to content

Commit 262bfba

Browse files
triha2workkuba-moo
authored andcommitted
net: dsa: microchip: Fix KSZ9477 set_ageing_time function
The aging count is not a simple 11-bit value but comprises a 3-bit multiplier and an 8-bit second count. The code tries to use the original multiplier which is 4 as the second count is still 300 seconds by default. Fixes: 2c119d9 ("net: dsa: microchip: add the support for set_ageing_time") Signed-off-by: Tristram Ha <[email protected]> Reviewed-by: Andrew Lunn <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 25c6a5a commit 262bfba

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

drivers/net/dsa/microchip/ksz9477.c

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* Microchip KSZ9477 switch driver main logic
44
*
5-
* Copyright (C) 2017-2019 Microchip Technology Inc.
5+
* Copyright (C) 2017-2024 Microchip Technology Inc.
66
*/
77

88
#include <linux/kernel.h>
@@ -983,26 +983,51 @@ void ksz9477_get_caps(struct ksz_device *dev, int port,
983983
int ksz9477_set_ageing_time(struct ksz_device *dev, unsigned int msecs)
984984
{
985985
u32 secs = msecs / 1000;
986-
u8 value;
987-
u8 data;
986+
u8 data, mult, value;
987+
u32 max_val;
988988
int ret;
989989

990-
value = FIELD_GET(SW_AGE_PERIOD_7_0_M, secs);
990+
#define MAX_TIMER_VAL ((1 << 8) - 1)
991991

992-
ret = ksz_write8(dev, REG_SW_LUE_CTRL_3, value);
993-
if (ret < 0)
994-
return ret;
992+
/* The aging timer comprises a 3-bit multiplier and an 8-bit second
993+
* value. Either of them cannot be zero. The maximum timer is then
994+
* 7 * 255 = 1785 seconds.
995+
*/
996+
if (!secs)
997+
secs = 1;
995998

996-
data = FIELD_GET(SW_AGE_PERIOD_10_8_M, secs);
999+
/* Return error if too large. */
1000+
else if (secs > 7 * MAX_TIMER_VAL)
1001+
return -EINVAL;
9971002

9981003
ret = ksz_read8(dev, REG_SW_LUE_CTRL_0, &value);
9991004
if (ret < 0)
10001005
return ret;
10011006

1002-
value &= ~SW_AGE_CNT_M;
1003-
value |= FIELD_PREP(SW_AGE_CNT_M, data);
1007+
/* Check whether there is need to update the multiplier. */
1008+
mult = FIELD_GET(SW_AGE_CNT_M, value);
1009+
max_val = MAX_TIMER_VAL;
1010+
if (mult > 0) {
1011+
/* Try to use the same multiplier already in the register as
1012+
* the hardware default uses multiplier 4 and 75 seconds for
1013+
* 300 seconds.
1014+
*/
1015+
max_val = DIV_ROUND_UP(secs, mult);
1016+
if (max_val > MAX_TIMER_VAL || max_val * mult != secs)
1017+
max_val = MAX_TIMER_VAL;
1018+
}
1019+
1020+
data = DIV_ROUND_UP(secs, max_val);
1021+
if (mult != data) {
1022+
value &= ~SW_AGE_CNT_M;
1023+
value |= FIELD_PREP(SW_AGE_CNT_M, data);
1024+
ret = ksz_write8(dev, REG_SW_LUE_CTRL_0, value);
1025+
if (ret < 0)
1026+
return ret;
1027+
}
10041028

1005-
return ksz_write8(dev, REG_SW_LUE_CTRL_0, value);
1029+
value = DIV_ROUND_UP(secs, data);
1030+
return ksz_write8(dev, REG_SW_LUE_CTRL_3, value);
10061031
}
10071032

10081033
void ksz9477_port_queue_split(struct ksz_device *dev, int port)

drivers/net/dsa/microchip/ksz9477_reg.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* Microchip KSZ9477 register definitions
44
*
5-
* Copyright (C) 2017-2018 Microchip Technology Inc.
5+
* Copyright (C) 2017-2024 Microchip Technology Inc.
66
*/
77

88
#ifndef __KSZ9477_REGS_H
@@ -165,8 +165,6 @@
165165
#define SW_VLAN_ENABLE BIT(7)
166166
#define SW_DROP_INVALID_VID BIT(6)
167167
#define SW_AGE_CNT_M GENMASK(5, 3)
168-
#define SW_AGE_CNT_S 3
169-
#define SW_AGE_PERIOD_10_8_M GENMASK(10, 8)
170168
#define SW_RESV_MCAST_ENABLE BIT(2)
171169
#define SW_HASH_OPTION_M 0x03
172170
#define SW_HASH_OPTION_CRC 1

0 commit comments

Comments
 (0)