Netdev List
 help / color / mirror / Atom feed
From: David Yang <mmyangfl@gmail.com>
To: netdev@vger.kernel.org
Cc: David Yang <mmyangfl@gmail.com>, Andrew Lunn <andrew@lunn.ch>,
	Vladimir Oltean <olteanv@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Subject: [PATCH net-next v4 3/6] net: dsa: motorcomm: Derive port isolation masks on the fly
Date: Fri,  4 Dec 2026 18:12:54 +0800	[thread overview]
Message-ID: <20261204101300.3680838-4-mmyangfl@gmail.com> (raw)
In-Reply-To: <20261204101300.3680838-1-mmyangfl@gmail.com>

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 <mmyangfl@gmail.com>
---
 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


  parent reply	other threads:[~2026-12-04 10:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 10:12 [PATCH net-next v4 0/6] net: dsa: motorcomm: Reject isolated redirect targets David Yang
2026-12-04 10:12 ` [PATCH net-next v4 1/6] net: dsa: motorcomm: Remove local cpu_ports_mask David Yang
2026-12-04 10:12 ` [PATCH net-next v4 2/6] net: dsa: motorcomm: Do not block hairpins on standalone ports David Yang
2026-12-04 10:12 ` David Yang [this message]
2026-12-04 10:12 ` [PATCH net-next v4 4/6] net: dsa: motorcomm: Reject isolated redirect targets David Yang
2026-12-04 10:12 ` [PATCH net-next v4 5/6] net: dsa: motorcomm: Reject bridge flags that break redirect rules David Yang
2026-12-04 10:12 ` [PATCH net-next v4 6/6] net: dsa: motorcomm: Recompute redirect rules on bridge changes David Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204101300.3680838-4-mmyangfl@gmail.com \
    --to=mmyangfl@gmail.com \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=olteanv@gmail.com \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox