The mirror destination check can reject a first rule when a bootloader or previous kernel left mirroring enabled. Not all supported switches receive a hardware reset during setup. Unchecked register accesses can also corrupt mirror state or report an unsuccessful rule installation as successful. Reference counting must not record a first source reference until hardware programming has succeeded. Clear capture control and both source-control registers during b53 and bcm_sf2 setup, then rebuild the source masks and capture destination from the mirror reference counts, propagating initialization failures. Since bcm_sf2 runs setup again on resume, this also restores active mirror rules after suspend. Check all mirror register reads and writes and only increment the source reference count after successful programming. Log deletion failures because the DSA callback cannot return an error. Use the source masks to decide whether a destination is occupied. This preserves the destination when a failed deletion leaves sources enabled, and permits a new destination after the last source was removed even if the final capture-control clear failed. Program the capture destination before the new source so a failed destination write cannot leave a new source enabled. Clearing the inactive capture-port field is cleanup. This builds on "net: dsa: b53: reject conflicting mirror destinations" and "net: dsa: b53: reference count shared mirror source bits", and must be applied after them. Fixes: ed3af5fd08eb ("net: dsa: b53: Add support for port mirroring") Signed-off-by: Florian Fainelli --- drivers/net/dsa/b53/b53_common.c | 91 ++++++++++++++++++++------------ drivers/net/dsa/bcm_sf2.c | 4 ++ 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/drivers/net/dsa/b53/b53_common.c b/drivers/net/dsa/b53/b53_common.c index 98696bce368f..deb331b3629a 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]; @@ -2654,14 +2658,25 @@ int b53_mirror_add(struct dsa_switch *ds, int port, { struct b53_device *dev = ds->priv; unsigned int *mirror_count = &dev->ports[port].mirror_count[ingress]; - u16 reg, loc, cap_ctl; + u16 reg, other_reg, loc, other_loc, cap_ctl; int ret; + loc = ingress ? B53_IG_MIR_CTL : B53_EG_MIR_CTL; + other_loc = ingress ? B53_EG_MIR_CTL : B53_IG_MIR_CTL; + + ret = b53_read16(dev, B53_MGMT_PAGE, loc, ®); + if (ret) + return ret; + + 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 ((cap_ctl & MIRROR_EN) && + if (((reg | other_reg) & MIRROR_MASK) && (cap_ctl & CAP_PORT_MASK) != mirror->to_local_port) { NL_SET_ERR_MSG_MOD(extack, "Only one mirroring destination is supported"); @@ -2673,19 +2688,17 @@ int b53_mirror_add(struct dsa_switch *ds, int port, return 0; } - if (ingress) - loc = B53_IG_MIR_CTL; - else - loc = B53_EG_MIR_CTL; - - b53_read16(dev, B53_MGMT_PAGE, loc, ®); - reg |= BIT(port); - b53_write16(dev, B53_MGMT_PAGE, loc, reg); - + /* Select the destination before enabling the new source. */ cap_ctl &= ~CAP_PORT_MASK; cap_ctl |= mirror->to_local_port; cap_ctl |= MIRROR_EN; - b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, cap_ctl); + 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 | BIT(port)); + if (ret) + return ret; (*mirror_count)++; dev->mirror_port = mirror->to_local_port; @@ -2700,8 +2713,8 @@ void b53_mirror_del(struct dsa_switch *ds, int port, struct b53_device *dev = ds->priv; unsigned int *mirror_count = &dev->ports[port].mirror_count[mirror->ingress]; - bool loc_disable = false, other_loc_disable = false; - u16 reg, loc; + u16 reg, other_reg, loc, other_loc; + int ret; if (!*mirror_count) return; @@ -2709,33 +2722,43 @@ void b53_mirror_del(struct dsa_switch *ds, int port, if (--(*mirror_count)) return; - if (mirror->ingress) - loc = B53_IG_MIR_CTL; - else - loc = B53_EG_MIR_CTL; + 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 &= ~BIT(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; - - 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 | CAP_PORT_MASK); - b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg); + ret = b53_read16(dev, B53_MGMT_PAGE, other_loc, &other_reg); + if (ret) + goto err; + + 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 | CAP_PORT_MASK); + 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/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