From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380395; b=wddBq0AcF1KlMz6CzqoWOTT864o73jwfgQkq07NtwtJLOmgeTgH/EAxFVK+uNSwcfLQ9 THChsId14E5x5Ubx+wvjsPg1WkNdWNfuxIgybpqJh5lvdhKNR+DYaEOERO5C2YuRtyId+ YPsl0f8J010ZcgpP60QMA5258gSY/CUb0RBDcvhkNeXuEwFQwkYYVhAZNhJVjXofqZgFL appA7LxbW+YNMEQdJYoMP3C/9AEC3oukNgSzfaWSc0tEW/pbYrGnyf+HirbtirpbUGvb6 RhEq7AkjVIvCJaU8dFVJkeYziZK/doT2YmgP4t4vmU2+IpFpdWezw2lLCA6drXITi8g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380395; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=o5hM92okXvBS7fUIxG9n94nSOatAndmPevWs/k/gIO8=; b=CjpnaZOxDP1fwWkzPh1B1Dme3omVgw6+UYBwWcKdZnLebGBAzv/G2cS3aSGIXYm7Pwet iTNj4MFga7V7IcjVP6cMS7oJatZhtbl8N6PmNpnmqqHIxqMVlPIxvvoanbS5ReziVPlzh EizH6PPNGvZ4Tk41AttJJPoT9hJL/YTGddD6KrtfYnDVcuj6FwPmbfAylJXdOhV8oPj/K f0iDod/IYM/AiambKr5jKRh8IgTY2FxYssKA6yUen6ctuLYGf3Adm/5PFEbZqg6OsNKYr K6J111OwgerDlosV5nNioX3dT8i6PtnxbtsJn6XZr9B0SgegrjKe3Q5jRZEAKX7Kl9Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=ygvSakOC; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E2F2E606A32D; Fri, 4 Dec 2026 10:33:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=ygvSakOC Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E76B1F000C2; Fri, 4 Dec 2026 10:33:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380394; bh=o5hM92okXvBS7fUIxG9n94nSOatAndmPevWs/k/gIO8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ygvSakOClXHgPRgEuwxEWGzjBbT8VxmwhdIrnuAf//lHHG5agt6cmsRH9PF9828pf Cej6KKVruVbGWId3G4SQzF74Aa1g0NFAk58grgGoVy47Vrqe/CtAW/yUg3HzdKwAS8 +yj0hc6gtK2U41sEflPSGQel99R/XZ2Nx7dPmmF4= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Florian Fainelli , Joseph Telaak , Willem de Bruijn , Sasha Levin Subject: [PATCH 7.3 253/742] net: dsa: b53: fix mirror destination and source state tracking Date: Fri, 4 Dec 2026 19:01:57 +0900 Message-ID: <20261204100641.779598635@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Florian Fainelli [ Upstream commit 320bc9b8e2bc8a5fc75537082529b534ace633e3 ] 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 Tested-by: Joseph Telaak Signed-off-by: Willem de Bruijn Signed-off-by: Sasha Levin --- 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 7638bf95b5ab2..5bdf9cf147b35 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 c4d2a41c714f6..df00a5ae46c9d 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 f3b56ea87b07b..babf5b142ad85 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