The mirror capture control register selects one destination for all ingress and egress sources. Adding a rule for another destination currently redirects existing rules. Multiple matchall filters can also share one source bit, but deleting any one of them clears that bit and can disable mirroring while other filters still need it. Reject a different destination while either source mask is occupied, and reference count each port's ingress and egress source bit under RTNL. Program a source on its first reference and clear it on its last one. Check every mirror register access and record a first reference only after programming succeeds. Select the destination before enabling the source, and log deletion failures because the callback returns no error. Use the source masks to retain the destination after a failed source deletion and allow a new destination after a failed capture-control clear. Not all supported switches receive a hardware reset during setup, so clear stale capture and source registers before accepting mirror rules. Introduce b53_mirror_init() together with its b53 and bcm_sf2 setup callers. Rebuild active sources and their destination from the reference counts when setup runs again, preserving rules across bcm_sf2 resume. Propagate initialization failures instead of accepting stale state. This depends on "net: dsa: b53: fix mirror port encoding on BCM5325 and BCM5365" for the chip-specific source and capture-port helpers. Fixes: ed3af5fd08eb ("net: dsa: b53: Add support for port mirroring") Signed-off-by: Florian Fainelli --- drivers/net/dsa/b53/b53_common.c | 168 ++++++++++++++++++++++++------- drivers/net/dsa/b53/b53_priv.h | 4 + drivers/net/dsa/bcm_sf2.c | 4 + 3 files changed, 140 insertions(+), 36 deletions(-) diff --git a/drivers/net/dsa/b53/b53_common.c b/drivers/net/dsa/b53/b53_common.c index ffbf5efd8d95..ee2d27732c0c 100644 --- a/drivers/net/dsa/b53/b53_common.c +++ b/drivers/net/dsa/b53/b53_common.c @@ -1360,6 +1360,10 @@ static int b53_setup(struct dsa_switch *ds) return ret; } + ret = b53_mirror_init(ds); + if (ret) + return ret; + /* setup default vlan for filtering mode */ pvid = b53_default_pvid(dev); vl = &dev->vlans[pvid]; @@ -2629,27 +2633,103 @@ static u16 b53_mirror_capture_port(struct b53_device *dev, int port) return port; } +int b53_mirror_init(struct dsa_switch *ds) +{ + struct b53_device *dev = ds->priv; + u16 ingress = 0, egress = 0, cap_ctl = 0; + unsigned int port; + int ret; + + /* A switch reset may leave the previous mirror configuration intact. */ + ret = b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, 0); + if (ret) + return ret; + + ret = b53_write16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, 0); + if (ret) + return ret; + + ret = b53_write16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, 0); + if (ret) + return ret; + + /* DSA retains matchall filters across suspend and resume. */ + for (port = 0; port < dev->num_ports; port++) { + if (dev->ports[port].mirror_count[true]) + ingress |= b53_mirror_port_mask(dev, port); + if (dev->ports[port].mirror_count[false]) + egress |= b53_mirror_port_mask(dev, port); + } + + if (!(ingress | egress)) + return 0; + + cap_ctl = MIRROR_EN | b53_mirror_capture_port(dev, dev->mirror_port); + ret = b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, cap_ctl); + if (ret) + return ret; + + ret = b53_write16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, ingress); + if (ret) + return ret; + + return b53_write16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, egress); +} +EXPORT_SYMBOL(b53_mirror_init); + int b53_mirror_add(struct dsa_switch *ds, int port, struct dsa_mall_mirror_tc_entry *mirror, bool ingress, struct netlink_ext_ack *extack) { struct b53_device *dev = ds->priv; - u16 reg, loc; + unsigned int *mirror_count = &dev->ports[port].mirror_count[ingress]; + u16 reg, other_reg, loc, other_loc, cap_ctl; + u16 cap_mask = b53_mirror_capture_port_mask(dev); + u16 cap_port = b53_mirror_capture_port(dev, mirror->to_local_port); + int ret; - if (ingress) - loc = B53_IG_MIR_CTL; - else - loc = B53_EG_MIR_CTL; + loc = ingress ? B53_IG_MIR_CTL : B53_EG_MIR_CTL; + other_loc = ingress ? B53_EG_MIR_CTL : B53_IG_MIR_CTL; - b53_read16(dev, B53_MGMT_PAGE, loc, ®); - reg |= b53_mirror_port_mask(dev, port); - b53_write16(dev, B53_MGMT_PAGE, loc, reg); + ret = b53_read16(dev, B53_MGMT_PAGE, loc, ®); + if (ret) + return ret; - b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, ®); - reg &= ~b53_mirror_capture_port_mask(dev); - reg |= b53_mirror_capture_port(dev, mirror->to_local_port); - reg |= MIRROR_EN; - b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg); + ret = b53_read16(dev, B53_MGMT_PAGE, other_loc, &other_reg); + if (ret) + return ret; + + ret = b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &cap_ctl); + if (ret) + return ret; + + if (((reg | other_reg) & MIRROR_MASK) && + (cap_ctl & cap_mask) != cap_port) { + NL_SET_ERR_MSG_MOD(extack, + "Only one mirroring destination is supported"); + return -EOPNOTSUPP; + } + + if (*mirror_count) { + (*mirror_count)++; + return 0; + } + + /* Select the destination before enabling the new source. */ + cap_ctl &= ~cap_mask; + cap_ctl |= cap_port; + cap_ctl |= MIRROR_EN; + ret = b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, cap_ctl); + if (ret) + return ret; + + ret = b53_write16(dev, B53_MGMT_PAGE, loc, + reg | b53_mirror_port_mask(dev, port)); + if (ret) + return ret; + + (*mirror_count)++; + dev->mirror_port = mirror->to_local_port; return 0; } @@ -2659,38 +2739,54 @@ void b53_mirror_del(struct dsa_switch *ds, int port, struct dsa_mall_mirror_tc_entry *mirror) { struct b53_device *dev = ds->priv; - bool loc_disable = false, other_loc_disable = false; - u16 reg, loc; + unsigned int *mirror_count = + &dev->ports[port].mirror_count[mirror->ingress]; + u16 reg, other_reg, loc, other_loc; + int ret; - if (mirror->ingress) - loc = B53_IG_MIR_CTL; - else - loc = B53_EG_MIR_CTL; + if (!*mirror_count) + return; + + if (--(*mirror_count)) + return; + + loc = mirror->ingress ? B53_IG_MIR_CTL : B53_EG_MIR_CTL; + other_loc = mirror->ingress ? B53_EG_MIR_CTL : B53_IG_MIR_CTL; /* Update the desired ingress/egress register */ - b53_read16(dev, B53_MGMT_PAGE, loc, ®); + ret = b53_read16(dev, B53_MGMT_PAGE, loc, ®); + if (ret) + goto err; + reg &= ~b53_mirror_port_mask(dev, port); - if (!(reg & MIRROR_MASK)) - loc_disable = true; - b53_write16(dev, B53_MGMT_PAGE, loc, reg); + ret = b53_write16(dev, B53_MGMT_PAGE, loc, reg); + if (ret) + goto err; /* Now look at the other one to know if we can disable mirroring * entirely */ - if (mirror->ingress) - b53_read16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, ®); - else - b53_read16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, ®); - if (!(reg & MIRROR_MASK)) - other_loc_disable = true; + ret = b53_read16(dev, B53_MGMT_PAGE, other_loc, &other_reg); + if (ret) + goto err; - b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, ®); - /* Both no longer have ports, let's disable mirroring */ - if (loc_disable && other_loc_disable) { - reg &= ~MIRROR_EN; - reg &= ~b53_mirror_capture_port_mask(dev); - } - b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg); + if ((reg | other_reg) & MIRROR_MASK) + return; + + ret = b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, ®); + if (ret) + goto err; + + reg &= ~(MIRROR_EN | b53_mirror_capture_port_mask(dev)); + ret = b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg); + if (ret) + goto err; + + return; + +err: + dev_err(dev->dev, "Failed to delete mirror rule for port %d: %d\n", + port, ret); } EXPORT_SYMBOL(b53_mirror_del); diff --git a/drivers/net/dsa/b53/b53_priv.h b/drivers/net/dsa/b53/b53_priv.h index c4d2a41c714f..df00a5ae46c9 100644 --- a/drivers/net/dsa/b53/b53_priv.h +++ b/drivers/net/dsa/b53/b53_priv.h @@ -119,6 +119,8 @@ struct b53_pcs { #define B53_N_PCS 2 struct b53_port { + /* References to each egress/ingress mirror source mask bit. */ + unsigned int mirror_count[2]; u16 vlan_ctl_mask; u16 pvid; struct ethtool_keee eee; @@ -170,6 +172,7 @@ struct b53_device { /* run time configuration */ bool enable_jumbo; + unsigned int mirror_port; unsigned int num_vlans; struct b53_vlan *vlans; @@ -545,6 +548,7 @@ int b53_mdb_add(struct dsa_switch *ds, int port, int b53_mdb_del(struct dsa_switch *ds, int port, const struct switchdev_obj_port_mdb *mdb, struct dsa_db db); +int b53_mirror_init(struct dsa_switch *ds); int b53_mirror_add(struct dsa_switch *ds, int port, struct dsa_mall_mirror_tc_entry *mirror, bool ingress, struct netlink_ext_ack *extack); diff --git a/drivers/net/dsa/bcm_sf2.c b/drivers/net/dsa/bcm_sf2.c index 1747a1f20c78..cbaa5de9b107 100644 --- a/drivers/net/dsa/bcm_sf2.c +++ b/drivers/net/dsa/bcm_sf2.c @@ -1061,6 +1061,10 @@ static int bcm_sf2_sw_setup(struct dsa_switch *ds) unsigned int port; int ret; + ret = b53_mirror_init(ds); + if (ret) + return ret; + /* Enable all valid ports and disable those unused */ for (port = 0; port < priv->hw_params.num_ports; port++) { /* IMP port receives special treatment */ -- 2.53.0