For a following commit, an accurate snapshot of port isolation masks is required. However, .port_pre_bridge_flags() can only veto the changes, and .port_bridge_flags() is deferred, thus the driver never knows the exact time point when flags take effect. Introduce a helper to derive such mask on the fly, and remove local cache for port flags. Signed-off-by: David Yang --- drivers/net/dsa/motorcomm/chip.c | 158 ++++++++++++++++++++----------- drivers/net/dsa/motorcomm/chip.h | 4 - 2 files changed, 105 insertions(+), 57 deletions(-) diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c index 6e57c9262c68..567ce8716acd 100644 --- a/drivers/net/dsa/motorcomm/chip.c +++ b/drivers/net/dsa/motorcomm/chip.c @@ -1322,39 +1322,112 @@ static int yt921x_isolate(struct yt921x_priv *priv, int 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, + u16 *isolation_masks) { - unsigned long targets_mask = ports_mask & ~dsa_cpu_ports(&priv->ds); + struct dsa_switch *ds = &priv->ds; + struct net_device *brport; + struct dsa_port *dp; u32 isolated_mask; + u32 hairpin_mask; u32 ctrl; - int port; - int res; isolated_mask = 0; - for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) { - struct yt921x_port *pp = &priv->ports[port]; + hairpin_mask = 0; + dsa_switch_for_each_user_port(dp, ds) { + int port = dp->index; + + if (!(ports_mask & BIT(port))) + continue; - if (pp->isolated) + brport = dsa_port_to_bridge_port(dp); + + if (br_port_flag_is_set(brport, BR_ISOLATED)) isolated_mask |= BIT(port); + if (br_port_flag_is_set(brport, BR_HAIRPIN_MODE)) + hairpin_mask |= BIT(port); } /* Block from non-cpu bridge ports ... */ - for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) { - struct yt921x_port *pp = &priv->ports[port]; + dsa_switch_for_each_user_port(dp, ds) { + int port = dp->index; + + if (!(ports_mask & BIT(port))) + continue; /* to non-bridge ports */ ctrl = ~ports_mask; /* to isolated ports when isolated */ - if (pp->isolated) + if (isolated_mask & BIT(port)) ctrl |= isolated_mask; /* to itself when non-hairpin */ - if (!pp->hairpin) + if (!(hairpin_mask & BIT(port))) ctrl |= BIT(port); else ctrl &= ~BIT(port); + isolation_masks[port] = ctrl; + } +} + +static void +yt921x_isolation_calc_ports(struct yt921x_priv *priv, u16 *ports_maskp, + u16 *isolation_masks) +{ + struct dsa_switch *ds = &priv->ds; + struct net_device *bdev; + struct dsa_port *dp; + u32 cpu_ports_mask; + u32 targets_mask; + + cpu_ports_mask = dsa_cpu_ports(ds); + targets_mask = *ports_maskp & ~cpu_ports_mask; + if (!targets_mask) + return; + + dsa_switch_for_each_user_port(dp, ds) { + u32 bridge_ports_mask; + int port = dp->index; + + if (!(targets_mask & BIT(port))) + continue; + + bdev = dsa_port_bridge_dev_get(dp); + if (!bdev) { + isolation_masks[port] = ~(BIT(port) | cpu_ports_mask); + targets_mask &= ~BIT(port); + } else { + bridge_ports_mask = dsa_bridge_ports(ds, bdev); + yt921x_isolation_calc_bridge(priv, + bridge_ports_mask | + cpu_ports_mask, + isolation_masks); + targets_mask &= ~bridge_ports_mask; + *ports_maskp |= bridge_ports_mask; + } + + if (!targets_mask) + break; + } +} + +static int +yt921x_isolation_commit(struct yt921x_priv *priv, u16 ports_mask, + const u16 *isolation_masks) +{ + struct dsa_switch *ds = &priv->ds; + struct dsa_port *dp; + int res; + + dsa_switch_for_each_user_port(dp, ds) { + int port = dp->index; + + if (!(ports_mask & BIT(port))) + continue; + res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), - ctrl); + isolation_masks[port]); if (res) return res; } @@ -1362,6 +1435,19 @@ static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask) return 0; } +static int +yt921x_isolation_refresh(struct yt921x_priv *priv, u16 ports_mask, + bool also_bridge) +{ + u16 isolation_masks[YT921X_PORT_NUM]; + u16 targets_mask = ports_mask; + + yt921x_isolation_calc_ports(priv, &targets_mask, isolation_masks); + return yt921x_isolation_commit(priv, + also_bridge ? targets_mask : ports_mask, + isolation_masks); +} + static int yt921x_bridge_leave(struct yt921x_priv *priv, int port) { int res; @@ -1378,7 +1464,7 @@ static int yt921x_bridge_leave(struct yt921x_priv *priv, int port) } static int -yt921x_bridge_join(struct yt921x_priv *priv, int port, u16 ports_mask) +yt921x_bridge_join(struct yt921x_priv *priv, int port) { int res; @@ -1386,7 +1472,7 @@ yt921x_bridge_join(struct yt921x_priv *priv, int port, u16 ports_mask) if (res) return res; - res = yt921x_bridge(priv, ports_mask); + res = yt921x_isolation_refresh(priv, BIT(port), true); if (res) return res; @@ -1397,8 +1483,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,30 +1502,10 @@ 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) { - struct dsa_switch *ds = &priv->ds; - struct dsa_port *dp = dsa_to_port(ds, port); - struct net_device *bdev; - - 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); - res = yt921x_bridge(priv, ports_mask); - if (res) - return res; - } + if ((flags.mask & BR_HAIRPIN_MODE) || (flags.mask & BR_ISOLATED)) { + res = yt921x_isolation_refresh(priv, BIT(port), true); + if (res) + return res; } return 0; @@ -1502,25 +1566,13 @@ yt921x_dsa_port_bridge_join(struct dsa_switch *ds, int port, struct netlink_ext_ack *extack) { struct yt921x_priv *priv = dsa_to_yt921x_priv(ds); - struct yt921x_port *pp = &priv->ports[port]; - struct net_device *brport_dev; - struct dsa_port *dp; - u16 ports_mask; int res; if (dsa_is_cpu_port(ds, port)) return 0; - ports_mask = dsa_bridge_ports(ds, bridge.dev); - ports_mask |= dsa_cpu_ports(ds); - - dp = dsa_to_port(ds, port); - brport_dev = dsa_port_to_bridge_port(dp); - mutex_lock(&priv->reg_lock); - pp->isolated = br_port_flag_is_set(brport_dev, BR_ISOLATED); - pp->hairpin = br_port_flag_is_set(brport_dev, BR_HAIRPIN_MODE); - res = yt921x_bridge_join(priv, port, ports_mask); + res = yt921x_bridge_join(priv, port); mutex_unlock(&priv->reg_lock); return res; diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h index d5ed580f8a78..22381009db51 100644 --- a/drivers/net/dsa/motorcomm/chip.h +++ b/drivers/net/dsa/motorcomm/chip.h @@ -457,10 +457,6 @@ struct yt921x_port { bool serdes:1; /* Link from in-band status (PHYLINK_PCS_NEG_INBAND) */ bool inband:1; - /* BR_HAIRPIN_MODE */ - bool hairpin:1; - /* BR_ISOLATED */ - bool isolated:1; unsigned short acl_cnt; -- 2.53.0