Replace the hardcoded YT921X_PORT_NUM macro with the per-series max_ports field in port validation and in the internal mii_bus phy_mask setup. This removes family-specific constants from the common code path and simplifies adding new switch families with different port counts. Validate max_ports against the size of priv->ports (and the u32 width of phy_mask) before using it in GENMASK(). No functional change for existing YT921X devices, as their max_ports equals YT921X_PORT_NUM. Reviewed-by: Andrew Lunn Signed-off-by: Kyle Switch --- drivers/net/dsa/motorcomm/mdio_bus.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/net/dsa/motorcomm/mdio_bus.c b/drivers/net/dsa/motorcomm/mdio_bus.c index a4062e6b0f00..a06e2cc94a3e 100644 --- a/drivers/net/dsa/motorcomm/mdio_bus.c +++ b/drivers/net/dsa/motorcomm/mdio_bus.c @@ -113,7 +113,7 @@ static int yt921x_mbus_int_read(struct mii_bus *mbus, int port, int reg) u16 val; int res; - if (port >= YT921X_PORT_NUM) + if (port >= priv->series->max_ports) return U16_MAX; mutex_lock(&priv->reg_lock); @@ -131,7 +131,7 @@ yt921x_mbus_int_write(struct mii_bus *mbus, int port, int reg, u16 data) struct yt921x_priv *priv = mbus->priv; int res; - if (port >= YT921X_PORT_NUM) + if (port >= priv->series->max_ports) return -ENODEV; mutex_lock(&priv->reg_lock); @@ -155,6 +155,7 @@ int yt921x_mbus_int_init(struct yt921x_priv *priv, struct device_node *mnp) { struct device *dev = yt921x_priv_to_device(priv); struct mii_bus *mbus; + u32 max_ports; int res; mbus = mdiobus_alloc(); @@ -167,7 +168,12 @@ int yt921x_mbus_int_init(struct yt921x_priv *priv, struct device_node *mnp) mbus->read = yt921x_mbus_int_read; mbus->write = yt921x_mbus_int_write; mbus->parent = dev; - mbus->phy_mask = (u32)~GENMASK(YT921X_PORT_NUM - 1, 0); + max_ports = priv->series->max_ports; + if (max_ports == 0 || max_ports > ARRAY_SIZE(priv->ports)) { + dev_err(dev, "Invalid max_ports: %u\n", max_ports); + return -EINVAL; + } + mbus->phy_mask = (u32)~GENMASK(max_ports - 1, 0); res = of_mdiobus_register(mbus, mnp); if (res) { -- 2.25.1