[PATCH net v4 6/8] net: dsa: b53: reference count shared mirror source bits
From: Florian Fainelli
Date: Mon Nov 09 2026 - 22:54:15 EST
Multiple tc matchall filters can use the same source port, direction and
capture destination, but the hardware represents that source with one
mask bit. Removing any one filter currently clears the bit even when
another filter still needs it, and can also disable mirroring globally.
Track references to each port's ingress and egress source bits. Program
a bit for the first reference and retain it until the last reference is
removed. Keep the two directions independent and ignore a deletion with
no matching reference to avoid underflow. The tc callbacks serialize this
accounting under RTNL.
Fixes: ed3af5fd08eb ("net: dsa: b53: Add support for port mirroring")
Signed-off-by: Florian Fainelli <florian.fainelli@xxxxxxxxxxxx>
---
drivers/net/dsa/b53/b53_common.c | 61 ++++++++++++++++++++++++++++++++
drivers/net/dsa/b53/b53_priv.h | 3 ++
drivers/net/dsa/bcm_sf2.c | 6 ++--
3 files changed, 68 insertions(+), 2 deletions(-)
diff --git a/drivers/net/dsa/b53/b53_common.c b/drivers/net/dsa/b53/b53_common.c
index 6b4a94a97d66..7f5bae290eaa 100644
--- a/drivers/net/dsa/b53/b53_common.c
+++ b/drivers/net/dsa/b53/b53_common.c
@@ -2604,11 +2604,56 @@ enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port,
}
EXPORT_SYMBOL(b53_get_tag_protocol);
+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 |= BIT(port);
+ if (dev->ports[port].mirror_count[false])
+ egress |= BIT(port);
+ }
+
+ if (!(ingress | egress))
+ return 0;
+
+ cap_ctl = MIRROR_EN | 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;
+ unsigned int *mirror_count = &dev->ports[port].mirror_count[ingress];
u16 reg, loc, cap_ctl;
int ret;
@@ -2623,6 +2668,11 @@ int b53_mirror_add(struct dsa_switch *ds, int port,
return -EOPNOTSUPP;
}
+ if (*mirror_count) {
+ (*mirror_count)++;
+ return 0;
+ }
+
if (ingress)
loc = B53_IG_MIR_CTL;
else
@@ -2637,6 +2687,9 @@ int b53_mirror_add(struct dsa_switch *ds, int port,
cap_ctl |= MIRROR_EN;
b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, cap_ctl);
+ (*mirror_count)++;
+ dev->mirror_port = mirror->to_local_port;
+
return 0;
}
EXPORT_SYMBOL(b53_mirror_add);
@@ -2645,9 +2698,17 @@ void b53_mirror_del(struct dsa_switch *ds, int port,
struct dsa_mall_mirror_tc_entry *mirror)
{
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;
+ if (!*mirror_count)
+ return;
+
+ if (--(*mirror_count))
+ return;
+
if (mirror->ingress)
loc = B53_IG_MIR_CTL;
else
diff --git a/drivers/net/dsa/b53/b53_priv.h b/drivers/net/dsa/b53/b53_priv.h
index c4d2a41c714f..d55fba9bcb1d 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;
diff --git a/drivers/net/dsa/bcm_sf2.c b/drivers/net/dsa/bcm_sf2.c
index 1c846f6d7802..6570279cc040 100644
--- a/drivers/net/dsa/bcm_sf2.c
+++ b/drivers/net/dsa/bcm_sf2.c
@@ -996,9 +996,11 @@ static int bcm_sf2_sw_resume(struct dsa_switch *ds)
if (priv->hw_params.num_gphy == 1)
bcm_sf2_gphy_enable_set(ds, true);
- ds->ops->setup(ds);
+ ret = ds->ops->setup(ds);
+ if (ret && !priv->wol_ports_mask)
+ clk_disable_unprepare(priv->clk);
- return 0;
+ return ret;
}
static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
--
2.53.0