For a following commit, an accurate snapshot of port isolation masks is required. However, the bridge core invokes .port_pre_bridge_flags() synchronously in an atomic context, and userspace already sees such changes applied before .port_bridge_flags() is called, causing the stale snapshot to be used before the deferred call. Calculate first, so that part can be made atomic, then apply later. Signed-off-by: David Yang --- drivers/net/dsa/motorcomm/chip.c | 90 ++++++++++++++++++-------------- drivers/net/dsa/motorcomm/chip.h | 4 ++ 2 files changed, 56 insertions(+), 38 deletions(-) diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c index 49c9055c7512..6c6a87207bd5 100644 --- a/drivers/net/dsa/motorcomm/chip.c +++ b/drivers/net/dsa/motorcomm/chip.c @@ -1256,12 +1256,14 @@ yt921x_dsa_port_vlan_add(struct dsa_switch *ds, int port, static int yt921x_userport_standalone(struct yt921x_priv *priv, int port) { + struct yt921x_port *pp = &priv->ports[port]; struct dsa_switch *ds = &priv->ds; u32 mask; u32 ctrl; int res; ctrl = ~dsa_cpu_ports(ds); + pp->isolation_mask = ctrl; res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), ctrl); if (res) return res; @@ -1300,35 +1302,22 @@ static int yt921x_userport_bridge(struct yt921x_priv *priv, int port) return 0; } -static int yt921x_isolate(struct yt921x_priv *priv, int port) +static void yt921x_isolation_calc_standalone(struct yt921x_priv *priv, int port) { - u32 mask; - int res; - - mask = BIT(port); - for (int i = 0; i < YT921X_PORT_NUM; i++) { - if (dsa_is_cpu_port(&priv->ds, i) || i == port) - continue; - - res = yt921x_reg_set_bits(priv, YT921X_PORTn_ISOLATION(i), - mask); - if (res) - return res; - } - - return 0; + for (int i = 0; i < YT921X_PORT_NUM; i++) + priv->ports[i].isolation_mask |= BIT(port); } /* Make sure to include the CPU port in ports_mask, or your bridge will * not have it. */ -static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask) +static void +yt921x_isolation_calc_bridge(struct yt921x_priv *priv, u16 ports_mask) { unsigned long targets_mask = ports_mask & ~dsa_cpu_ports(&priv->ds); u32 isolated_mask; u32 ctrl; int port; - int res; isolated_mask = 0; for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) { @@ -1353,11 +1342,21 @@ static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask) else ctrl &= ~BIT(port); - res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), - ctrl); - if (res) - return res; + pp->isolation_mask = ctrl; } +} + +static int yt921x_isolation_commit(struct yt921x_priv *priv, u16 ports_mask) +{ + int res; + + for (int i = 0; i < YT921X_PORT_NUM; i++) + if (BIT(i) & ports_mask) { + res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(i), + priv->ports[i].isolation_mask); + if (res) + return res; + } return 0; } @@ -1370,7 +1369,8 @@ static int yt921x_bridge_leave(struct yt921x_priv *priv, int port) if (res) return res; - res = yt921x_isolate(priv, port); + yt921x_isolation_calc_standalone(priv, port); + res = yt921x_isolation_commit(priv, ~0); if (res) return res; @@ -1386,7 +1386,8 @@ yt921x_bridge_join(struct yt921x_priv *priv, int port, u16 ports_mask) if (res) return res; - res = yt921x_bridge(priv, ports_mask); + yt921x_isolation_calc_bridge(priv, ports_mask); + res = yt921x_isolation_commit(priv, ports_mask); if (res) return res; @@ -1397,8 +1398,6 @@ static int yt921x_bridge_flags(struct yt921x_priv *priv, int port, struct switchdev_brport_flags flags) { - struct yt921x_port *pp = &priv->ports[port]; - bool do_flush; u32 mask; int res; @@ -1418,16 +1417,7 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port, /* BR_BCAST_FLOOD: we can filter bcast, but cannot trap them */ - do_flush = false; - if (flags.mask & BR_HAIRPIN_MODE) { - pp->hairpin = flags.val & BR_HAIRPIN_MODE; - do_flush = true; - } - if (flags.mask & BR_ISOLATED) { - pp->isolated = flags.val & BR_ISOLATED; - do_flush = true; - } - if (do_flush) { + if ((flags.mask & BR_HAIRPIN_MODE) || (flags.mask & BR_ISOLATED)) { struct dsa_switch *ds = &priv->ds; struct dsa_port *dp = dsa_to_port(ds, port); struct net_device *bdev; @@ -1437,8 +1427,7 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port, u32 ports_mask; ports_mask = dsa_bridge_ports(ds, bdev); - ports_mask |= dsa_cpu_ports(ds); - res = yt921x_bridge(priv, ports_mask); + res = yt921x_isolation_commit(priv, ports_mask); if (res) return res; } @@ -1452,9 +1441,32 @@ yt921x_dsa_port_pre_bridge_flags(struct dsa_switch *ds, int port, struct switchdev_brport_flags flags, struct netlink_ext_ack *extack) { + struct yt921x_priv *priv = dsa_to_yt921x_priv(ds); + struct yt921x_port *pp = &priv->ports[port]; + if (flags.mask & ~(BR_HAIRPIN_MODE | BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_ISOLATED)) return -EINVAL; + + if ((flags.mask & BR_HAIRPIN_MODE) || (flags.mask & BR_ISOLATED)) { + struct dsa_port *dp = dsa_to_port(ds, port); + struct net_device *bdev; + + if (flags.mask & BR_HAIRPIN_MODE) + pp->hairpin = flags.val & BR_HAIRPIN_MODE; + if (flags.mask & BR_ISOLATED) + pp->isolated = flags.val & BR_ISOLATED; + + bdev = dsa_port_bridge_dev_get(dp); + if (bdev) { + u32 ports_mask; + + ports_mask = dsa_bridge_ports(ds, bdev); + ports_mask |= dsa_cpu_ports(ds); + yt921x_isolation_calc_bridge(priv, ports_mask); + } + } + return 0; } @@ -2046,6 +2058,7 @@ yt921x_dsa_phylink_get_caps(struct dsa_switch *ds, int port, static int yt921x_port_setup(struct yt921x_priv *priv, int port) { + struct yt921x_port *pp = &priv->ports[port]; struct dsa_switch *ds = &priv->ds; u32 ctrl; int res; @@ -2067,6 +2080,7 @@ static int yt921x_port_setup(struct yt921x_priv *priv, int port) * without tag). */ ctrl = ~(u32)0; + pp->isolation_mask = ctrl; res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), ctrl); if (res) diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h index d5ed580f8a78..203371888c45 100644 --- a/drivers/net/dsa/motorcomm/chip.h +++ b/drivers/net/dsa/motorcomm/chip.h @@ -457,11 +457,15 @@ struct yt921x_port { bool serdes:1; /* Link from in-band status (PHYLINK_PCS_NEG_INBAND) */ bool inband:1; + bool:0; + /* BR_HAIRPIN_MODE */ bool hairpin:1; /* BR_ISOLATED */ bool isolated:1; + u16 isolation_mask; + unsigned short acl_cnt; #if IS_ENABLED(CONFIG_NET_DSA_YT921X_LEDS) -- 2.53.0