[PATCH net-next v2 1/3] net: dsa: microchip: ksz9477: apply a shorter ageing time at once

From: Oleksij Rempel

Date: Sun Nov 15 2026 - 06:00:29 EST


Pulse Fast Aging when the programmed ageing time gets shorter or the Age
Count gets smaller.

The ageing time is Age Count times Age Period. An entry stores the Age
Count when it is updated, as the datasheet says. The datasheet does not
say when a new Age Period applies.

Without the pulse, stale entries outlived an STP topology change. In my
STP tests the switch ran at 300 s as 4 x 75 s, its reset value. With
forward_delay 4 s, the bridge asks for an ageing time of 8 s. Read back
right after the write, the register showed 8 s. In three tests, an
entry learned before the change still stayed for more than a minute. I
think the switch uses the new Age Period only after the running one
ends.

The pulse is also needed when the ageing time grows but the Age Count
shrinks. From 8 s (4 x 2 s) to 31 s (1 x 31 s), an entry stayed about
63 s in one test.

On a KSZ9893 the pulse removed the dynamic entries on the one user port
I tested. The new period then started right away.

I found this in STP tests on a KSZ9893. I tested the fix there with
kernel STP and with STP off. With mstpd I checked only the programmed
ageing time, not the FDB.

Fixes: 2c119d9982b1 ("net: dsa: microchip: add the support for set_ageing_time")
Signed-off-by: Oleksij Rempel <o.rempel@xxxxxxxxxxxxxx>
---
Changes in v2:
- Fix the tense in the comment (Andrew)
- Add new_period, and compute old_secs next to new_secs
- Rephrase the comments and the commit message
- Add a Fixes tag
---
drivers/net/dsa/microchip/ksz9477.c | 41 ++++++++++++++++++++++++++++++++-----
1 file changed, 36 insertions(+), 5 deletions(-)

diff --git a/drivers/net/dsa/microchip/ksz9477.c b/drivers/net/dsa/microchip/ksz9477.c
index 8d4647e23cda153bc68255a44c44e0d1d92469f9..a9d701f0404a706e6ad2a6faa2f57dbe28d383c2 100644
--- a/drivers/net/dsa/microchip/ksz9477.c
+++ b/drivers/net/dsa/microchip/ksz9477.c
@@ -1289,13 +1289,16 @@ static void ksz9477_phylink_get_caps(struct dsa_switch *ds, int port,
ksz_phylink_get_caps(ds, port, config);
}

+/* The datasheet gives no pulse length. A 2 ms pulse worked on a KSZ9893. */
+#define KSZ9477_FAST_AGE_PULSE_US (2 * USEC_PER_MSEC)
+
static int ksz9477_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
{
+ u8 data, mult, value, old_period, new_period;
struct ksz_device *dev = ds->priv;
+ u32 old_secs, new_secs, max_val;
u32 secs = msecs / 1000;
- u8 data, mult, value;
- u32 max_val;
- int ret;
+ int ret, err;

#define MAX_TIMER_VAL ((1 << 8) - 1)

@@ -1314,6 +1317,10 @@ static int ksz9477_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
if (ret < 0)
return ret;

+ ret = ksz_read8(dev, REG_SW_LUE_CTRL_3, &old_period);
+ if (ret < 0)
+ return ret;
+
/* Check whether there is need to update the multiplier. */
mult = FIELD_GET(SW_AGE_CNT_M, value);
max_val = MAX_TIMER_VAL;
@@ -1336,8 +1343,32 @@ static int ksz9477_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
return ret;
}

- value = DIV_ROUND_UP(secs, data);
- return ksz_write8(dev, REG_SW_LUE_CTRL_3, value);
+ new_period = DIV_ROUND_UP(secs, data);
+ ret = ksz_write8(dev, REG_SW_LUE_CTRL_3, new_period);
+ if (ret < 0)
+ return ret;
+
+ /* data is the new Age Count and mult is the old one. An entry
+ * stores the Age Count when it is updated. The datasheet does not
+ * say when a new Age Period applies. On a KSZ9893, entries outlived
+ * the new ageing time after it got shorter, and after the Age Count
+ * got smaller. Pulse Fast Aging in both cases. On a KSZ9893 the
+ * pulse removes the dynamic entries (checked on one user port) and
+ * starts the new period.
+ */
+ old_secs = mult * old_period;
+ new_secs = data * new_period;
+ if (new_secs >= old_secs && data >= mult)
+ return 0;
+
+ ret = ksz_rmw8(dev, REG_SW_LUE_CTRL_1, SW_FAST_AGING, SW_FAST_AGING);
+ if (!ret)
+ fsleep(KSZ9477_FAST_AGE_PULSE_US);
+
+ /* Clear the bit even after an error. */
+ err = ksz_rmw8(dev, REG_SW_LUE_CTRL_1, SW_FAST_AGING, 0);
+
+ return ret ?: err;
}

void ksz9477_port_queue_split(struct ksz_device *dev, int port)

--
2.47.3