Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next 0/5] net: dsa: lan9645x: add VLAN and bridge offload
@ 2026-11-24  8:57 Jens Emil Schulz Østergaard
  2026-11-24  8:57 ` [PATCH net-next 1/5] net: dsa: lan9645x: add the analyzer event register Jens Emil Schulz Østergaard
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-24  8:57 UTC (permalink / raw)
  To: UNGLinuxDriver, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Russell King
  Cc: linux-kernel, netdev, Jens Emil Schulz Østergaard

This series adds bridge offload to the LAN9645x DSA driver.

The MAC table is keyed on (MAC, VID), so at most one VLAN-aware bridge
can be offloaded. VLAN-unaware bridges are kept apart by classifying
their ports to a reserved VID per bridge, so up to 8 bridges can be
offloaded in total, at most one of them VLAN-aware.

With FDB isolation and the FDB and MDB callbacks, DSA enables unicast
and multicast filtering on standalone ports. Host addresses are
installed as MAC table entries towards the CPU, and unknown unicast and
unregistered multicast only reach the CPU while a port is promiscuous or
allmulti. The flood masks are global, so one such port lets that traffic
from every port reach the CPU.

Patch 1 adds the ANA_ANEVENTS register to the generated register macros.

Patch 2 adds VLAN support for VLAN-aware and VLAN-unaware bridges. VIDs
4032-4095 are reserved for internal use. Standalone ports use VID 0, and
the ports of VLAN-unaware bridge n use VID 4096 - n, so each
VLAN-unaware bridge gets its own FDB domain in the (MAC, VID) keyed MAC
table.

Patch 3 adds the MAC table and the FDB callbacks, with FDB isolation and
assisted learning on the CPU port.

Patch 4 adds the MDB callbacks. Each L2 multicast group uses one of the
general purpose PGIDs as its port mask. IP multicast groups carry the
port mask in the MAC table entry.

Patch 5 adds bridge offload. We support offload of 8 unaware bridges, and
at most 1 VLAN-aware bridge.

Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>
---
Jens Emil Schulz Østergaard (5):
      net: dsa: lan9645x: add the analyzer event register
      net: dsa: lan9645x: add vlan support
      net: dsa: lan9645x: add mac table integration
      net: dsa: lan9645x: add mdb management
      net: dsa: lan9645x: add bridge support

 drivers/net/dsa/microchip/lan9645x/Makefile        |   3 +
 drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c  | 267 ++++++++++
 drivers/net/dsa/microchip/lan9645x/lan9645x_main.c | 564 ++++++++++++++++++++-
 drivers/net/dsa/microchip/lan9645x/lan9645x_main.h | 114 +++++
 drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c  | 256 ++++++++++
 drivers/net/dsa/microchip/lan9645x/lan9645x_port.c |  14 +
 drivers/net/dsa/microchip/lan9645x/lan9645x_regs.h | 196 +++++++
 drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c | 429 ++++++++++++++++
 8 files changed, 1832 insertions(+), 11 deletions(-)
---
base-commit: eb6d6c80d35449c27471b1874f13066c09a8964d
change-id: 20261118-dsa-lan9645x-add-vlan-and-bridge-offload-9b28e691f883

Best regards,
-- 
Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH net-next 1/5] net: dsa: lan9645x: add the analyzer event register
  2026-11-24  8:57 [PATCH net-next 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
@ 2026-11-24  8:57 ` Jens Emil Schulz Østergaard
  2026-11-24  8:57 ` [PATCH net-next 2/5] net: dsa: lan9645x: add vlan support Jens Emil Schulz Østergaard
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-24  8:57 UTC (permalink / raw)
  To: UNGLinuxDriver, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Russell King
  Cc: linux-kernel, netdev, Jens Emil Schulz Østergaard

Add ANA_ANEVENTS to the autogenerated register macros. The MAC table
code uses its CPU_LEARN_FAILED sticky bit to detect a CPU learn that
did not find a free bucket in the hashed row.

Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>
---
 drivers/net/dsa/microchip/lan9645x/lan9645x_regs.h | 196 +++++++++++++++++++++
 1 file changed, 196 insertions(+)

diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_regs.h b/drivers/net/dsa/microchip/lan9645x/lan9645x_regs.h
index 0414c7239103..531dfb789a02 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_regs.h
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_regs.h
@@ -83,6 +83,202 @@ enum lan9645x_target {
 #define ANA_ANAGEFIL_VID_VAL_GET(x)\
 	FIELD_GET(ANA_ANAGEFIL_VID_VAL, x)
 
+/*      ANA:ANA:ANEVENTS */
+#define ANA_ANEVENTS              __REG(TARGET_ANA,\
+					0, 1, 27136, 0, 1, 284, 20, 0, 1, 4)
+
+#define ANA_ANEVENTS_CHANGE2SW                   BIT(31)
+#define ANA_ANEVENTS_CHANGE2SW_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_CHANGE2SW, x)
+#define ANA_ANEVENTS_CHANGE2SW_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_CHANGE2SW, x)
+
+#define ANA_ANEVENTS_LLCT_PORT_ERR               BIT(30)
+#define ANA_ANEVENTS_LLCT_PORT_ERR_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_LLCT_PORT_ERR, x)
+#define ANA_ANEVENTS_LLCT_PORT_ERR_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_LLCT_PORT_ERR, x)
+
+#define ANA_ANEVENTS_RT_FWD                      BIT(29)
+#define ANA_ANEVENTS_RT_FWD_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_RT_FWD, x)
+#define ANA_ANEVENTS_RT_FWD_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_RT_FWD, x)
+
+#define ANA_ANEVENTS_FLOOD_DISCARD               BIT(28)
+#define ANA_ANEVENTS_FLOOD_DISCARD_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_FLOOD_DISCARD, x)
+#define ANA_ANEVENTS_FLOOD_DISCARD_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_FLOOD_DISCARD, x)
+
+#define ANA_ANEVENTS_MSTI_DROP                   BIT(27)
+#define ANA_ANEVENTS_MSTI_DROP_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_MSTI_DROP, x)
+#define ANA_ANEVENTS_MSTI_DROP_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_MSTI_DROP, x)
+
+#define ANA_ANEVENTS_ACLKILL                     BIT(26)
+#define ANA_ANEVENTS_ACLKILL_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_ACLKILL, x)
+#define ANA_ANEVENTS_ACLKILL_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_ACLKILL, x)
+
+#define ANA_ANEVENTS_ACLUSED                     BIT(25)
+#define ANA_ANEVENTS_ACLUSED_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_ACLUSED, x)
+#define ANA_ANEVENTS_ACLUSED_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_ACLUSED, x)
+
+#define ANA_ANEVENTS_AUTOAGE                     BIT(24)
+#define ANA_ANEVENTS_AUTOAGE_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_AUTOAGE, x)
+#define ANA_ANEVENTS_AUTOAGE_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_AUTOAGE, x)
+
+#define ANA_ANEVENTS_LEARN_DROP                  BIT(23)
+#define ANA_ANEVENTS_LEARN_DROP_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_LEARN_DROP, x)
+#define ANA_ANEVENTS_LEARN_DROP_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_LEARN_DROP, x)
+
+#define ANA_ANEVENTS_AGED_ENTRY                  BIT(22)
+#define ANA_ANEVENTS_AGED_ENTRY_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_AGED_ENTRY, x)
+#define ANA_ANEVENTS_AGED_ENTRY_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_AGED_ENTRY, x)
+
+#define ANA_ANEVENTS_CPU_LEARN_FAILED            BIT(21)
+#define ANA_ANEVENTS_CPU_LEARN_FAILED_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_CPU_LEARN_FAILED, x)
+#define ANA_ANEVENTS_CPU_LEARN_FAILED_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_CPU_LEARN_FAILED, x)
+
+#define ANA_ANEVENTS_AUTO_LEARN_FAILED           BIT(20)
+#define ANA_ANEVENTS_AUTO_LEARN_FAILED_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_AUTO_LEARN_FAILED, x)
+#define ANA_ANEVENTS_AUTO_LEARN_FAILED_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_AUTO_LEARN_FAILED, x)
+
+#define ANA_ANEVENTS_LEARN_REMOVE                BIT(19)
+#define ANA_ANEVENTS_LEARN_REMOVE_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_LEARN_REMOVE, x)
+#define ANA_ANEVENTS_LEARN_REMOVE_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_LEARN_REMOVE, x)
+
+#define ANA_ANEVENTS_AUTO_LEARNED                BIT(18)
+#define ANA_ANEVENTS_AUTO_LEARNED_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_AUTO_LEARNED, x)
+#define ANA_ANEVENTS_AUTO_LEARNED_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_AUTO_LEARNED, x)
+
+#define ANA_ANEVENTS_AUTO_MOVED                  BIT(17)
+#define ANA_ANEVENTS_AUTO_MOVED_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_AUTO_MOVED, x)
+#define ANA_ANEVENTS_AUTO_MOVED_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_AUTO_MOVED, x)
+
+#define ANA_ANEVENTS_DROPPED                     BIT(16)
+#define ANA_ANEVENTS_DROPPED_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_DROPPED, x)
+#define ANA_ANEVENTS_DROPPED_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_DROPPED, x)
+
+#define ANA_ANEVENTS_CLASSIFIED_DROP             BIT(15)
+#define ANA_ANEVENTS_CLASSIFIED_DROP_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_CLASSIFIED_DROP, x)
+#define ANA_ANEVENTS_CLASSIFIED_DROP_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_CLASSIFIED_DROP, x)
+
+#define ANA_ANEVENTS_CLASSIFIED_COPY             BIT(14)
+#define ANA_ANEVENTS_CLASSIFIED_COPY_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_CLASSIFIED_COPY, x)
+#define ANA_ANEVENTS_CLASSIFIED_COPY_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_CLASSIFIED_COPY, x)
+
+#define ANA_ANEVENTS_VLAN_DISCARD                BIT(13)
+#define ANA_ANEVENTS_VLAN_DISCARD_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_VLAN_DISCARD, x)
+#define ANA_ANEVENTS_VLAN_DISCARD_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_VLAN_DISCARD, x)
+
+#define ANA_ANEVENTS_FWD_DISCARD                 BIT(12)
+#define ANA_ANEVENTS_FWD_DISCARD_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_FWD_DISCARD, x)
+#define ANA_ANEVENTS_FWD_DISCARD_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_FWD_DISCARD, x)
+
+#define ANA_ANEVENTS_MULTICAST_FLOOD             BIT(11)
+#define ANA_ANEVENTS_MULTICAST_FLOOD_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_MULTICAST_FLOOD, x)
+#define ANA_ANEVENTS_MULTICAST_FLOOD_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_MULTICAST_FLOOD, x)
+
+#define ANA_ANEVENTS_UNICAST_FLOOD               BIT(10)
+#define ANA_ANEVENTS_UNICAST_FLOOD_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_UNICAST_FLOOD, x)
+#define ANA_ANEVENTS_UNICAST_FLOOD_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_UNICAST_FLOOD, x)
+
+#define ANA_ANEVENTS_DEST_KNOWN                  BIT(9)
+#define ANA_ANEVENTS_DEST_KNOWN_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_DEST_KNOWN, x)
+#define ANA_ANEVENTS_DEST_KNOWN_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_DEST_KNOWN, x)
+
+#define ANA_ANEVENTS_BUCKET3_MATCH               BIT(8)
+#define ANA_ANEVENTS_BUCKET3_MATCH_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_BUCKET3_MATCH, x)
+#define ANA_ANEVENTS_BUCKET3_MATCH_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_BUCKET3_MATCH, x)
+
+#define ANA_ANEVENTS_BUCKET2_MATCH               BIT(7)
+#define ANA_ANEVENTS_BUCKET2_MATCH_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_BUCKET2_MATCH, x)
+#define ANA_ANEVENTS_BUCKET2_MATCH_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_BUCKET2_MATCH, x)
+
+#define ANA_ANEVENTS_BUCKET1_MATCH               BIT(6)
+#define ANA_ANEVENTS_BUCKET1_MATCH_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_BUCKET1_MATCH, x)
+#define ANA_ANEVENTS_BUCKET1_MATCH_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_BUCKET1_MATCH, x)
+
+#define ANA_ANEVENTS_BUCKET0_MATCH               BIT(5)
+#define ANA_ANEVENTS_BUCKET0_MATCH_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_BUCKET0_MATCH, x)
+#define ANA_ANEVENTS_BUCKET0_MATCH_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_BUCKET0_MATCH, x)
+
+#define ANA_ANEVENTS_CPU_OPERATION               BIT(4)
+#define ANA_ANEVENTS_CPU_OPERATION_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_CPU_OPERATION, x)
+#define ANA_ANEVENTS_CPU_OPERATION_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_CPU_OPERATION, x)
+
+#define ANA_ANEVENTS_DMAC_LOOKUP                 BIT(3)
+#define ANA_ANEVENTS_DMAC_LOOKUP_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_DMAC_LOOKUP, x)
+#define ANA_ANEVENTS_DMAC_LOOKUP_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_DMAC_LOOKUP, x)
+
+#define ANA_ANEVENTS_SMAC_LOOKUP                 BIT(2)
+#define ANA_ANEVENTS_SMAC_LOOKUP_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_SMAC_LOOKUP, x)
+#define ANA_ANEVENTS_SMAC_LOOKUP_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_SMAC_LOOKUP, x)
+
+#define ANA_ANEVENTS_SEQ_GEN_ERR_0               BIT(1)
+#define ANA_ANEVENTS_SEQ_GEN_ERR_0_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_SEQ_GEN_ERR_0, x)
+#define ANA_ANEVENTS_SEQ_GEN_ERR_0_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_SEQ_GEN_ERR_0, x)
+
+#define ANA_ANEVENTS_SEQ_GEN_ERR_1               BIT(0)
+#define ANA_ANEVENTS_SEQ_GEN_ERR_1_SET(x)\
+	FIELD_PREP(ANA_ANEVENTS_SEQ_GEN_ERR_1, x)
+#define ANA_ANEVENTS_SEQ_GEN_ERR_1_GET(x)\
+	FIELD_GET(ANA_ANEVENTS_SEQ_GEN_ERR_1, x)
+
 /*      ANA:ANA:AUTOAGE */
 #define ANA_AUTOAGE               __REG(TARGET_ANA,\
 					0, 1, 27136, 0, 1, 284, 44, 0, 1, 4)

-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH net-next 2/5] net: dsa: lan9645x: add vlan support
  2026-11-24  8:57 [PATCH net-next 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
  2026-11-24  8:57 ` [PATCH net-next 1/5] net: dsa: lan9645x: add the analyzer event register Jens Emil Schulz Østergaard
@ 2026-11-24  8:57 ` Jens Emil Schulz Østergaard
  2026-11-24  8:57 ` [PATCH net-next 3/5] net: dsa: lan9645x: add mac table integration Jens Emil Schulz Østergaard
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-24  8:57 UTC (permalink / raw)
  To: UNGLinuxDriver, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Russell King
  Cc: linux-kernel, netdev, Jens Emil Schulz Østergaard

Add VLAN support for VLAN-aware and VLAN-unaware bridges.

VIDs 4032-4095 are reserved for internal use. VID 0 (HOST_PVID) is used
internally as the port VLAN for standalone (non-bridged) ports so their
FDB domain is isolated from bridged traffic.

The ports of VLAN-unaware bridges use VIDs in the reserved range to
isolate forwarding domains. The MAC table is keyed on (MAC, VID), so we
can not support multiple VLAN-aware bridges. A bridged port is a
member of its bridge's VID only while it is VLAN-unaware, so frames
tagged with a reserved VID are dropped on VLAN-aware ports.

The VLAN table and the per port VLAN configuration are serialized with
fwd_domain_lock, since .port_setup runs without rtnl held.

There are two chip limitations. First, only one egress-untagged VLAN is
supported on a port that also has tagged VLANs. The rewriter can exempt
a single PORT_VID from tagging, so adding a second untagged VLAN
alongside tagged ones is rejected with -EBUSY.

Second, both TPIDs are recognized as VLAN tags unconditionally. There is
no per port control to classify one TPID as tagged while treating the
other as untagged customer data.

S-tag and C-tag are always treated as interchangeable. Full C-component
conformance would need S-tagged frames treated as untagged by a VLAN
aware bridge, dropped on a port without a pvid and classified to the
pvid on a port with one.

Of those, only the drop is expressible at this time, through
ANA_DROP_CFG.DROP_S_TAGGED_ENA. The reclassification needs the VCAP to
match on tag type and overwrite the classified VID, see
ocelot_update_vlan_reclassify_rule().

We do not program ANA_DROP_CFG.DROP_S_TAGGED_ENA, since dropping without
reclassifying is less consistent than treating both tag types alike.

Only bridge vlan_protocol 802.1Q is offloaded. Enabling VLAN filtering
on an 802.1ad bridge is refused, which also covers joining a VLAN-aware
802.1ad bridge. DSA does not pass SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL
to drivers, so a protocol change on a bridge that is already VLAN-aware
is not seen.

Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>
---
 drivers/net/dsa/microchip/lan9645x/Makefile        |   1 +
 drivers/net/dsa/microchip/lan9645x/lan9645x_main.c |  78 ++++
 drivers/net/dsa/microchip/lan9645x/lan9645x_main.h |  45 +++
 drivers/net/dsa/microchip/lan9645x/lan9645x_port.c |   6 +
 drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c | 420 +++++++++++++++++++++
 5 files changed, 550 insertions(+)

diff --git a/drivers/net/dsa/microchip/lan9645x/Makefile b/drivers/net/dsa/microchip/lan9645x/Makefile
index 7cc0ae0ada40..e049114b3563 100644
--- a/drivers/net/dsa/microchip/lan9645x/Makefile
+++ b/drivers/net/dsa/microchip/lan9645x/Makefile
@@ -6,3 +6,4 @@ mchp-lan9645x-objs := \
 	lan9645x_npi.o \
 	lan9645x_phylink.o \
 	lan9645x_port.o \
+	lan9645x_vlan.o \
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
index 0eee05034458..e6c10399154b 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
@@ -71,6 +71,7 @@ static void lan9645x_teardown(struct dsa_switch *ds)
 {
 	struct lan9645x *lan9645x = ds->priv;
 
+	mutex_destroy(&lan9645x->fwd_domain_lock);
 	lan9645x_npi_port_deinit(lan9645x, lan9645x->npi);
 }
 
@@ -159,6 +160,11 @@ static int lan9645x_setup(struct dsa_switch *ds)
 		return err;
 	}
 
+	mutex_init(&lan9645x->fwd_domain_lock);
+	err = lan9645x_vlan_init(lan9645x);
+	if (err)
+		goto err_mutex;
+
 	/* Link Aggregation Mode: NETDEV_LAG_HASH_L2 */
 	lan_wr(ANA_AGGR_CFG_AC_SMAC_ENA |
 	       ANA_AGGR_CFG_AC_DMAC_ENA,
@@ -287,6 +293,11 @@ static int lan9645x_setup(struct dsa_switch *ds)
 		 lan9645x->num_phys_ports - lan9645x->num_port_dis);
 
 	return 0;
+
+err_mutex:
+	mutex_destroy(&lan9645x->fwd_domain_lock);
+	lan9645x_npi_port_deinit(lan9645x, lan9645x->npi);
+	return err;
 }
 
 static void lan9645x_port_phylink_get_caps(struct dsa_switch *ds, int port,
@@ -295,6 +306,68 @@ static void lan9645x_port_phylink_get_caps(struct dsa_switch *ds, int port,
 	lan9645x_phylink_get_caps(ds->priv, port, config);
 }
 
+static int lan9645x_port_vlan_filtering(struct dsa_switch *ds, int port,
+					bool enabled,
+					struct netlink_ext_ack *extack)
+{
+	struct net_device *br = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
+	struct lan9645x *lan9645x = ds->priv;
+	struct lan9645x_port *p;
+
+	if (enabled && !lan9645x_bridge_vlan_proto_ok(br)) {
+		NL_SET_ERR_MSG_MOD(extack,
+				   "Only 802.1Q VLAN-aware bridges are supported");
+		return -EINVAL;
+	}
+
+	/* DSA core does not call this for the CPU port */
+	p = lan9645x_to_port(lan9645x, port);
+	mutex_lock(&lan9645x->fwd_domain_lock);
+	p->vlan_aware = enabled;
+	if (lan9645x_port_is_bridged(p))
+		lan9645x_vlan_port_set_bridge_vid(p, !enabled);
+	lan9645x_vlan_port_apply(p);
+	mutex_unlock(&lan9645x->fwd_domain_lock);
+
+	return 0;
+}
+
+static int lan9645x_port_vlan_add(struct dsa_switch *ds, int port,
+				  const struct switchdev_obj_port_vlan *vlan,
+				  struct netlink_ext_ack *extack)
+{
+	struct lan9645x *lan9645x = ds->priv;
+	struct lan9645x_port *p;
+	bool pvid, untagged;
+	int err;
+
+	p = lan9645x_to_port(lan9645x, port);
+	pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
+	untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
+
+	mutex_lock(&lan9645x->fwd_domain_lock);
+	err = lan9645x_vlan_port_add_vlan(p, vlan->vid, pvid, untagged, extack);
+	mutex_unlock(&lan9645x->fwd_domain_lock);
+
+	return err;
+}
+
+static int lan9645x_port_vlan_del(struct dsa_switch *ds, int port,
+				  const struct switchdev_obj_port_vlan *vlan)
+{
+	struct lan9645x *lan9645x = ds->priv;
+	struct lan9645x_port *p;
+	int err;
+
+	p = lan9645x_to_port(lan9645x, port);
+
+	mutex_lock(&lan9645x->fwd_domain_lock);
+	err = lan9645x_vlan_port_del_vlan(p, vlan->vid);
+	mutex_unlock(&lan9645x->fwd_domain_lock);
+
+	return err;
+}
+
 static const struct dsa_switch_ops lan9645x_switch_ops = {
 	.get_tag_protocol		= lan9645x_get_tag_protocol,
 
@@ -308,6 +381,11 @@ static const struct dsa_switch_ops lan9645x_switch_ops = {
 	/* MTU  */
 	.port_change_mtu		= lan9645x_change_mtu,
 	.port_max_mtu			= lan9645x_get_max_mtu,
+
+	/* VLAN integration */
+	.port_vlan_filtering		= lan9645x_port_vlan_filtering,
+	.port_vlan_add			= lan9645x_port_vlan_add,
+	.port_vlan_del			= lan9645x_port_vlan_del,
 };
 
 static int lan9645x_request_target_regmaps(struct lan9645x *lan9645x)
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
index 7b62c826cc7e..ea093a293328 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
@@ -7,6 +7,7 @@
 
 #include <linux/dsa/lan9645x.h>
 #include <linux/if_bridge.h>
+#include <linux/if_vlan.h>
 #include <linux/regmap.h>
 #include <net/dsa.h>
 
@@ -37,6 +38,11 @@
  */
 #define QSYS_Q_RSRV			95
 
+/* Reserved VLAN IDs. */
+#define HOST_PVID			0
+#define VLAN_RSV_RANGE_START		4032
+#define VLAN_MAX			(VLAN_RSV_RANGE_START - 1)
+
 /* Port Group Identifiers (PGID) are port-masks applied to all frames.
  *
  * The forwarding engine outputs two masks for the forwarding decision, DEST and
@@ -159,6 +165,11 @@ enum lan9645x_vlan_port_tag {
 	LAN9645X_TAG_ALL = 3,
 };
 
+struct lan9645x_vlan {
+	u32 portmask: 10, /* ports 0-8 + CPU port module */
+	    untagged: 9; /* ports 0-8 */
+};
+
 struct lan9645x {
 	struct device *dev;
 	struct dsa_switch *ds;
@@ -170,7 +181,13 @@ struct lan9645x {
 	u8 num_phys_ports;
 	struct lan9645x_port **ports;
 
+	/* lock forwarding configuration and vlan table */
+	struct mutex fwd_domain_lock;
+
 	int num_port_dis;
+
+	/* VLAN entries */
+	struct lan9645x_vlan vlans[VLAN_N_VID];
 };
 
 struct lan9645x_port {
@@ -178,6 +195,10 @@ struct lan9645x_port {
 
 	u8 chip_port;
 
+	unsigned int bridge_num;
+	bool vlan_aware;
+	u16 pvid;
+
 	bool rx_internal_delay;
 	bool tx_internal_delay;
 };
@@ -226,6 +247,19 @@ static inline struct lan9645x_port *lan9645x_to_port(struct lan9645x *lan9645x,
 	return lan9645x->ports[port];
 }
 
+static inline bool lan9645x_port_is_bridged(struct lan9645x_port *p)
+{
+	return p->bridge_num;
+}
+
+static inline bool lan9645x_bridge_vlan_proto_ok(const struct net_device *br)
+{
+	u16 proto;
+
+	return !br || !br_vlan_enabled(br) || br_vlan_get_proto(br, &proto) ||
+	       proto == ETH_P_8021Q;
+}
+
 static inline struct regmap *lan_tgt2rmap(struct lan9645x *lan9645x,
 					  enum lan9645x_target t, int tinst)
 {
@@ -303,4 +337,15 @@ void lan9645x_phylink_get_caps(struct lan9645x *lan9645x, int port,
 			       struct phylink_config *c);
 void lan9645x_phylink_port_down(struct lan9645x *lan9645x, int port);
 
+/* VLAN lan9645x_vlan.c */
+int lan9645x_vlan_init(struct lan9645x *lan9645x);
+u16 lan9645x_vlan_unaware_pvid(unsigned int bridge_num);
+void lan9645x_vlan_port_apply(struct lan9645x_port *p);
+void lan9645x_vlan_port_set_bridge_vid(struct lan9645x_port *p, bool member);
+int lan9645x_vlan_port_add_vlan(struct lan9645x_port *p, u16 vid, bool pvid,
+				bool untagged,
+				struct netlink_ext_ack *extack);
+int lan9645x_vlan_port_del_vlan(struct lan9645x_port *p, u16 vid);
+void lan9645x_vlan_set_hostmode(struct lan9645x_port *p);
+
 #endif /* __LAN9645X_MAIN_H__ */
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_port.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_port.c
index 4d2d922bacba..2571380f2c02 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_port.c
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_port.c
@@ -162,5 +162,11 @@ int lan9645x_port_setup(struct dsa_switch *ds, int port)
 		ANA_PORT_CFG_PORTID_VAL,
 		lan9645x, ANA_PORT_CFG(p->chip_port));
 
+	if (p->chip_port != lan9645x->npi) {
+		mutex_lock(&lan9645x->fwd_domain_lock);
+		lan9645x_vlan_set_hostmode(p);
+		mutex_unlock(&lan9645x->fwd_domain_lock);
+	}
+
 	return 0;
 }
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c
new file mode 100644
index 000000000000..3ad9afc27b45
--- /dev/null
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c
@@ -0,0 +1,420 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Copyright (C) 2026 Microchip Technology Inc.
+ */
+
+#include "lan9645x_main.h"
+
+#define VLANACCESS_CMD_IDLE		0
+#define VLANACCESS_CMD_READ		1
+#define VLANACCESS_CMD_WRITE		2
+#define VLANACCESS_CMD_INIT		3
+
+struct lan9645x_vlan_port_info {
+	int untagged;
+	int tagged;
+	u16 untagged_vid;
+};
+
+/* Calculate VLAN state of a port, across all VLANS. */
+static void lan9645x_vlan_port_get_info(struct lan9645x *lan9645x, int port,
+					struct lan9645x_vlan_port_info *info)
+{
+	u16 vid;
+
+	info->untagged = 0;
+	info->tagged = 0;
+	info->untagged_vid = 0;
+
+	for (vid = 1; vid <= VLAN_MAX; vid++) {
+		struct lan9645x_vlan *v = &lan9645x->vlans[vid];
+
+		if (!(v->portmask & BIT(port)))
+			continue;
+
+		if (v->untagged & BIT(port)) {
+			info->untagged++;
+			info->untagged_vid = vid;
+		} else {
+			info->tagged++;
+		}
+
+		/* VLAN composition is invalid, so break early. */
+		if (info->untagged > 1 && info->tagged)
+			break;
+	}
+}
+
+static int lan9645x_vlan_wait_for_completion(struct lan9645x *lan9645x)
+{
+	u32 val;
+
+	return lan9645x_rd_poll_timeout(lan9645x, ANA_VLANACCESS, val,
+					ANA_VLANACCESS_VLAN_TBL_CMD_GET(val) ==
+					VLANACCESS_CMD_IDLE);
+}
+
+static int lan9645x_vlan_hw_wr(struct lan9645x *lan9645x, u16 vid)
+{
+	struct lan9645x_vlan *v = &lan9645x->vlans[vid];
+	bool cpu_dis;
+	u32 val;
+	int err;
+
+	cpu_dis = !(v->portmask & BIT(lan9645x->num_phys_ports));
+	val = ANA_VLANTIDX_VLAN_PGID_CPU_DIS_SET(cpu_dis) |
+	      ANA_VLANTIDX_V_INDEX_SET(vid);
+
+	lan_wr(val, lan9645x, ANA_VLANTIDX);
+	lan_wr(ANA_VLAN_PORT_MASK_VLAN_PORT_MASK_SET(v->portmask),
+	       lan9645x, ANA_VLAN_PORT_MASK);
+	lan_wr(ANA_VLANACCESS_VLAN_TBL_CMD_SET(VLANACCESS_CMD_WRITE),
+	       lan9645x, ANA_VLANACCESS);
+
+	/* The VLAN access engine completes in a fixed ~1us vs the polling
+	 * timeout of 100_000 us. A timeout here therefore likely means the
+	 * register bus itself is dead, not that the VLAN op failed. There is no
+	 * meaningful recovery at runtime, so this function logs via dev_err()
+	 * and runtime callers discard the return value. Only
+	 * lan9645x_vlan_init() treats this as fatal so that probe fails early
+	 * on a broken bus.
+	 */
+	err = lan9645x_vlan_wait_for_completion(lan9645x);
+	if (err)
+		dev_err(lan9645x->dev, "VLAN set mask failed\n");
+
+	return err;
+}
+
+u16 lan9645x_vlan_unaware_pvid(unsigned int bridge_num)
+{
+	return bridge_num ? VLAN_N_VID - bridge_num : HOST_PVID;
+}
+
+static u16 lan9645x_vlan_port_get_pvid(struct lan9645x_port *port)
+{
+	if (lan9645x_port_is_bridged(port) && port->vlan_aware)
+		return port->pvid;
+	else
+		return lan9645x_vlan_unaware_pvid(port->bridge_num);
+}
+
+/* Dynamically choose the egress tagging mode based on the port vlan state:
+ *
+ * Standalone:
+ * TAG_NO_PVID_NO_UNAWARE with PORT_VID=HOST_PVID. Frames classified to
+ * HOST_PVID leave untagged, and a frame with another classified VID, such as a
+ * frame mirrored from a VLAN-aware port, keeps its tag.
+ *
+ * Bridged, VLAN-aware:
+ *  - N untagged, 0 tagged: TAG_DISABLED
+ *  - 1 untagged, N tagged: TAG_NO_PVID_NO_UNAWARE
+ *  - 0 untagged, N tagged: TAG_NO_UNAWARE
+ *
+ * Bridged, VLAN-unaware:
+ *   TAG_DISABLED
+ */
+static void
+lan9645x_vlan_port_apply_egress(struct lan9645x_port *p,
+				struct lan9645x_vlan_port_info *info)
+{
+	struct lan9645x *lan9645x = p->lan9645x;
+	enum lan9645x_vlan_port_tag tag_cfg;
+	u16 port_vid = HOST_PVID;
+
+	if (!lan9645x_port_is_bridged(p)) {
+		tag_cfg = LAN9645X_TAG_NO_PVID_NO_UNAWARE;
+	} else if (p->vlan_aware) {
+		struct lan9645x_vlan_port_info _info;
+
+		if (!info) {
+			lan9645x_vlan_port_get_info(lan9645x, p->chip_port,
+						    &_info);
+			info = &_info;
+		}
+
+		if (info->untagged == 1 && info->tagged) {
+			tag_cfg = LAN9645X_TAG_NO_PVID_NO_UNAWARE;
+			port_vid = info->untagged_vid;
+		} else if (info->untagged) {
+			tag_cfg = LAN9645X_TAG_DISABLED;
+		} else {
+			tag_cfg = LAN9645X_TAG_NO_UNAWARE;
+		}
+	} else {
+		tag_cfg = LAN9645X_TAG_DISABLED;
+	}
+
+	/* TAG_TPID_CFG encoding:
+	 *
+	 * 0: Use 0x8100.
+	 * 1: Use 0x88A8.
+	 * 2: Use custom value from PORT_VLAN_CFG.PORT_TPID.
+	 * 3: Use PORT_VLAN_CFG.PORT_TPID, unless ingress tag was a C-tag
+	 *    (EtherType = 0x8100)
+	 *
+	 * Use 3 and PORT_VLAN_CFG.PORT_TPID=0x88a8 to ensure stags are not
+	 * rewritten to ctags on egress.
+	 */
+	lan_rmw(REW_TAG_CFG_TAG_TPID_CFG_SET(3) |
+		REW_TAG_CFG_TAG_CFG_SET(tag_cfg),
+		REW_TAG_CFG_TAG_TPID_CFG |
+		REW_TAG_CFG_TAG_CFG,
+		lan9645x, REW_TAG_CFG(p->chip_port));
+
+	lan_rmw(REW_PORT_VLAN_CFG_PORT_TPID_SET(ETH_P_8021AD) |
+		REW_PORT_VLAN_CFG_PORT_VID_SET(port_vid),
+		REW_PORT_VLAN_CFG_PORT_TPID |
+		REW_PORT_VLAN_CFG_PORT_VID,
+		lan9645x, REW_PORT_VLAN_CFG(p->chip_port));
+}
+
+static void lan9645x_vlan_port_apply_ingress(struct lan9645x_port *p)
+{
+	bool aware = lan9645x_port_is_bridged(p) && p->vlan_aware;
+	struct lan9645x *lan9645x = p->lan9645x;
+	u16 pvid;
+	u32 val;
+
+	pvid = lan9645x_vlan_port_get_pvid(p);
+
+	/* Default vlan to classify for untagged frames (may be zero), and set
+	 * their tag type to C-tag.
+	 */
+	val = ANA_VLAN_CFG_VLAN_VID_SET(pvid) |
+	      ANA_VLAN_CFG_VLAN_TAG_TYPE_SET(0);
+	if (aware)
+		val |= ANA_VLAN_CFG_VLAN_AWARE_ENA_SET(1) |
+		       ANA_VLAN_CFG_VLAN_POP_CNT_SET(1);
+
+	lan_rmw(val,
+		ANA_VLAN_CFG_VLAN_VID |
+		ANA_VLAN_CFG_VLAN_AWARE_ENA |
+		ANA_VLAN_CFG_VLAN_POP_CNT |
+		ANA_VLAN_CFG_VLAN_TAG_TYPE,
+		lan9645x, ANA_VLAN_CFG(p->chip_port));
+
+	val = 0;
+	if (aware && !pvid)
+		/* If port is vlan-aware and tagged, drop untagged and priority
+		 * tagged frames.
+		 */
+		val = ANA_DROP_CFG_DROP_UNTAGGED_ENA_SET(1) |
+		      ANA_DROP_CFG_DROP_PRIO_S_TAGGED_ENA_SET(1) |
+		      ANA_DROP_CFG_DROP_PRIO_C_TAGGED_ENA_SET(1);
+
+	lan_rmw(val,
+		ANA_DROP_CFG_DROP_UNTAGGED_ENA |
+		ANA_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
+		ANA_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
+		lan9645x, ANA_DROP_CFG(p->chip_port));
+}
+
+void lan9645x_vlan_port_apply(struct lan9645x_port *p)
+{
+	lockdep_assert_held(&p->lan9645x->fwd_domain_lock);
+
+	lan9645x_vlan_port_apply_ingress(p);
+	lan9645x_vlan_port_apply_egress(p, NULL);
+}
+
+static struct lan9645x_vlan *lan9645x_vlan_port_modify(struct lan9645x_port *p,
+						       u16 vid, bool pvid,
+						       bool untagged)
+{
+	struct lan9645x_vlan *v = &p->lan9645x->vlans[vid];
+
+	if (untagged)
+		v->untagged |= BIT(p->chip_port);
+	else
+		v->untagged &= ~BIT(p->chip_port);
+
+	if (pvid)
+		p->pvid = vid;
+	else if (p->pvid == vid)
+		p->pvid = 0;
+
+	return v;
+}
+
+static int lan9645x_vlan_cpu_add(struct lan9645x_port *p, u16 vid)
+{
+	struct lan9645x_vlan *v = &p->lan9645x->vlans[vid];
+
+	v->portmask |= BIT(p->lan9645x->num_phys_ports) | BIT(p->chip_port);
+	lan9645x_vlan_hw_wr(p->lan9645x, vid);
+
+	return 0;
+}
+
+int lan9645x_vlan_port_add_vlan(struct lan9645x_port *p, u16 vid, bool pvid,
+				bool untagged, struct netlink_ext_ack *extack)
+{
+	struct lan9645x *lan9645x = p->lan9645x;
+	struct lan9645x_vlan_port_info info;
+	struct lan9645x_vlan old_vlan;
+	struct lan9645x_vlan *v;
+	u16 old_pvid;
+
+	lockdep_assert_held(&lan9645x->fwd_domain_lock);
+
+	/* Kernel VLAN core adds vid 0, which collides with our HOST_PVID.
+	 * Hardware needs no entry for it: basic VLAN classification replaces a
+	 * VID of 0 with VLAN_CFG.VLAN_VID, keeping the frame's PCP and DEI, so
+	 * a priority tagged frame is classified to the port pvid. Ports with no
+	 * pvid instead drop it, see lan9645x_vlan_port_apply_ingress().
+	 */
+	if (!vid)
+		return 0;
+
+	if (vid > VLAN_MAX) {
+		NL_SET_ERR_MSG_MOD(extack, "VLAN range 4032-4095 reserved");
+		return -EINVAL;
+	}
+
+	if (p->chip_port == lan9645x->npi)
+		return lan9645x_vlan_cpu_add(p, vid);
+
+	old_vlan = lan9645x->vlans[vid];
+	old_pvid = p->pvid;
+
+	v = lan9645x_vlan_port_modify(p, vid, pvid, untagged);
+	v->portmask |= BIT(p->chip_port);
+
+	lan9645x_vlan_port_get_info(lan9645x, p->chip_port, &info);
+
+	if (info.untagged > 1 && info.tagged) {
+		*v = old_vlan;
+		p->pvid = old_pvid;
+		if (untagged)
+			NL_SET_ERR_MSG_MOD(extack,
+					   "Port with egress-tagged VLANs cannot have more than one egress-untagged VLAN");
+		else
+			NL_SET_ERR_MSG_MOD(extack,
+					   "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
+		return -EBUSY;
+	}
+
+	lan9645x_vlan_hw_wr(lan9645x, vid);
+	lan9645x_vlan_port_apply_ingress(p);
+	lan9645x_vlan_port_apply_egress(p, &info);
+
+	return 0;
+}
+
+static int lan9645x_vlan_cpu_del(struct lan9645x_port *p, u16 vid)
+{
+	struct lan9645x_vlan *v = &p->lan9645x->vlans[vid];
+
+	v->portmask &= ~(BIT(p->lan9645x->num_phys_ports) |
+			 BIT(p->chip_port));
+	lan9645x_vlan_hw_wr(p->lan9645x, vid);
+
+	return 0;
+}
+
+int lan9645x_vlan_port_del_vlan(struct lan9645x_port *p, u16 vid)
+{
+	struct lan9645x *lan9645x = p->lan9645x;
+	struct lan9645x_vlan *v;
+
+	lockdep_assert_held(&lan9645x->fwd_domain_lock);
+
+	if (!vid)
+		return 0;
+
+	if (vid > VLAN_MAX)
+		return -EINVAL;
+
+	if (p->chip_port == lan9645x->npi)
+		return lan9645x_vlan_cpu_del(p, vid);
+
+	v = lan9645x_vlan_port_modify(p, vid, false, false);
+	v->portmask &= ~BIT(p->chip_port);
+	lan9645x_vlan_hw_wr(lan9645x, vid);
+	lan9645x_vlan_port_apply(p);
+
+	return 0;
+}
+
+void lan9645x_vlan_set_hostmode(struct lan9645x_port *p)
+{
+	lockdep_assert_held(&p->lan9645x->fwd_domain_lock);
+
+	p->lan9645x->vlans[HOST_PVID].portmask |= BIT(p->chip_port);
+	lan9645x_vlan_hw_wr(p->lan9645x, HOST_PVID);
+	lan9645x_vlan_port_apply(p);
+}
+
+void lan9645x_vlan_port_set_bridge_vid(struct lan9645x_port *p, bool member)
+{
+	u16 vid = lan9645x_vlan_unaware_pvid(p->bridge_num);
+	struct lan9645x *lan9645x = p->lan9645x;
+
+	lockdep_assert_held(&lan9645x->fwd_domain_lock);
+
+	if (member)
+		lan9645x->vlans[vid].portmask |= BIT(p->chip_port);
+	else
+		lan9645x->vlans[vid].portmask &= ~BIT(p->chip_port);
+
+	lan9645x_vlan_hw_wr(lan9645x, vid);
+}
+
+int lan9645x_vlan_init(struct lan9645x *lan9645x)
+{
+	u32 all_phys_ports, all_ports;
+	unsigned int bridge_num;
+	struct dsa_port *dp;
+	u16 vid;
+	int err;
+
+	all_phys_ports = GENMASK(lan9645x->num_phys_ports - 1, 0);
+	all_ports = all_phys_ports | BIT(lan9645x->num_phys_ports);
+
+	/* Clear VLAN table, by default all ports are members of all VLANS */
+	lan_wr(ANA_VLANACCESS_VLAN_TBL_CMD_SET(VLANACCESS_CMD_INIT),
+	       lan9645x, ANA_VLANACCESS);
+
+	err = lan9645x_vlan_wait_for_completion(lan9645x);
+	if (err) {
+		dev_err(lan9645x->dev, "VLAN clear table failed\n");
+		return err;
+	}
+
+	for (vid = 1; vid < VLAN_N_VID; vid++) {
+		err = lan9645x_vlan_hw_wr(lan9645x, vid);
+		if (err)
+			return err;
+	}
+
+	/* Set all the ports + cpu to be part of HOST_PVID */
+	lan9645x->vlans[HOST_PVID].portmask = all_ports;
+	err = lan9645x_vlan_hw_wr(lan9645x, HOST_PVID);
+	if (err)
+		return err;
+
+	for (bridge_num = 1; bridge_num <= lan9645x->num_phys_ports;
+	     bridge_num++) {
+		vid = lan9645x_vlan_unaware_pvid(bridge_num);
+		lan9645x->vlans[vid].portmask = BIT(lan9645x->num_phys_ports);
+		err = lan9645x_vlan_hw_wr(lan9645x, vid);
+		if (err)
+			return err;
+	}
+
+	/* Configure the CPU port module to be vlan aware */
+	lan_wr(ANA_VLAN_CFG_VLAN_VID_SET(HOST_PVID) |
+	       ANA_VLAN_CFG_VLAN_AWARE_ENA_SET(1) |
+	       ANA_VLAN_CFG_VLAN_POP_CNT_SET(1),
+	       lan9645x, ANA_VLAN_CFG(lan9645x->num_phys_ports));
+
+	/* Set vlan ingress filter mask to all ports */
+	lan_wr(all_ports, lan9645x, ANA_VLANMASK);
+
+	dsa_switch_for_each_user_port(dp, lan9645x->ds) {
+		lan_wr(0, lan9645x, REW_PORT_VLAN_CFG(dp->index));
+		lan_wr(0, lan9645x, REW_TAG_CFG(dp->index));
+	}
+
+	return 0;
+}

-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH net-next 3/5] net: dsa: lan9645x: add mac table integration
  2026-11-24  8:57 [PATCH net-next 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
  2026-11-24  8:57 ` [PATCH net-next 1/5] net: dsa: lan9645x: add the analyzer event register Jens Emil Schulz Østergaard
  2026-11-24  8:57 ` [PATCH net-next 2/5] net: dsa: lan9645x: add vlan support Jens Emil Schulz Østergaard
@ 2026-11-24  8:57 ` Jens Emil Schulz Østergaard
  2026-11-25  8:58   ` netdev-bot+sashiko
  2026-11-24  8:57 ` [PATCH net-next 4/5] net: dsa: lan9645x: add mdb management Jens Emil Schulz Østergaard
  2026-11-24  8:57 ` [PATCH net-next 5/5] net: dsa: lan9645x: add bridge support Jens Emil Schulz Østergaard
  4 siblings, 1 reply; 10+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-24  8:57 UTC (permalink / raw)
  To: UNGLinuxDriver, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Russell King
  Cc: linux-kernel, netdev, Jens Emil Schulz Østergaard

Add MAC table support, and dsa fdb callback integration. The mactable is
keyed on (MAC,VID) and each row has 4 buckets. A mac table entry
typically points to a PGID index, the first 9 of which represent a front
port.

Enable FDB isolation and assisted learning on the CPU port. FDB entries
without a VID are installed in the reserved VID of their bridge, or in
HOST_PVID for standalone ports. The FDB dump reports locked entries as
static.

A CPU learn into a row whose four buckets all hold locked or IP
multicast entries is dropped by the hardware, and only flagged in
ANA_ANEVENTS.CPU_LEARN_FAILED. Report it as -ENOSPC.

Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>
---
 drivers/net/dsa/microchip/lan9645x/Makefile        |   1 +
 drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c  | 267 +++++++++++++++++++++
 drivers/net/dsa/microchip/lan9645x/lan9645x_main.c | 108 +++++++++
 drivers/net/dsa/microchip/lan9645x/lan9645x_main.h |  42 ++++
 4 files changed, 418 insertions(+)

diff --git a/drivers/net/dsa/microchip/lan9645x/Makefile b/drivers/net/dsa/microchip/lan9645x/Makefile
index e049114b3563..70815edca5b9 100644
--- a/drivers/net/dsa/microchip/lan9645x/Makefile
+++ b/drivers/net/dsa/microchip/lan9645x/Makefile
@@ -2,6 +2,7 @@
 obj-$(CONFIG_NET_DSA_MICROCHIP_LAN9645X) += mchp-lan9645x.o
 
 mchp-lan9645x-objs := \
+	lan9645x_mac.o \
 	lan9645x_main.o \
 	lan9645x_npi.o \
 	lan9645x_phylink.o \
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c
new file mode 100644
index 000000000000..9da03d0369f5
--- /dev/null
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c
@@ -0,0 +1,267 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Copyright (C) 2026 Microchip Technology Inc.
+ */
+
+#include "lan9645x_main.h"
+
+#define CMD_IDLE		0
+#define CMD_LEARN		1
+#define CMD_FORGET		2
+#define CMD_AGE			3
+#define CMD_GET_NEXT		4
+#define CMD_INIT		5
+#define CMD_READ		6
+#define CMD_WRITE		7
+#define CMD_SYNC_GET_NEXT	8
+
+/* The chip has 2048 * 4 = 8192 total entries in the mac table */
+#define MACT_ROWS		(FIELD_MAX(ANA_MACTINDX_M_INDEX) + 1)
+#define MACT_BUCKETS		(FIELD_MAX(ANA_MACTINDX_BUCKET) + 1)
+#define MACT_ENTRIES		(MACT_ROWS * MACT_BUCKETS)
+#define MACT_GET_NEXT_MAX	(MACT_ENTRIES + 1)
+
+static int lan9645x_mac_wait_for_completion(struct lan9645x *lan9645x,
+					    u32 *maca)
+{
+	u32 val = 0;
+	int err;
+
+	lockdep_assert_held(&lan9645x->mact_lock);
+
+	err = lan9645x_rd_poll_timeout(lan9645x, ANA_MACACCESS, val,
+				       ANA_MACACCESS_MAC_TABLE_CMD_GET(val) ==
+				       CMD_IDLE);
+	if (err)
+		return err;
+
+	if (maca)
+		*maca = val;
+
+	return 0;
+}
+
+static void lan9645x_mac_select(struct lan9645x *lan9645x,
+				const unsigned char mac[ETH_ALEN],
+				unsigned int vid)
+{
+	u64 maddr = ether_addr_to_u64(mac);
+
+	lockdep_assert_held(&lan9645x->mact_lock);
+
+	lan_wr(ANA_MACHDATA_VID_SET(vid) |
+	       ANA_MACHDATA_MACHDATA_SET(upper_32_bits(maddr)),
+	       lan9645x,
+	       ANA_MACHDATA);
+
+	lan_wr(lower_32_bits(maddr),
+	       lan9645x,
+	       ANA_MACLDATA);
+}
+
+static int __lan9645x_mact_forget(struct lan9645x *lan9645x,
+				  const unsigned char mac[ETH_ALEN],
+				  unsigned int vid,
+				  enum macaccess_entry_type type)
+{
+	lockdep_assert_held(&lan9645x->mact_lock);
+
+	lan9645x_mac_select(lan9645x, mac, vid);
+
+	lan_wr(ANA_MACACCESS_ENTRYTYPE_SET(type) |
+	       ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_FORGET),
+	       lan9645x,
+	       ANA_MACACCESS);
+
+	return lan9645x_mac_wait_for_completion(lan9645x, NULL);
+}
+
+int lan9645x_mact_forget(struct lan9645x *lan9645x,
+			 const unsigned char mac[ETH_ALEN], unsigned int vid,
+			 enum macaccess_entry_type type)
+{
+	int err;
+
+	mutex_lock(&lan9645x->mact_lock);
+	err = __lan9645x_mact_forget(lan9645x, mac, vid, type);
+	mutex_unlock(&lan9645x->mact_lock);
+
+	return err;
+}
+
+static int __lan9645x_mact_learn(struct lan9645x *lan9645x, int port,
+				 const unsigned char mac[ETH_ALEN],
+				 unsigned int vid,
+				 enum macaccess_entry_type type,
+				 bool cpu_copy)
+{
+	int err;
+
+	lockdep_assert_held(&lan9645x->mact_lock);
+
+	lan9645x_mac_select(lan9645x, mac, vid);
+
+	lan_wr(ANA_MACACCESS_VALID_SET(1) |
+	       ANA_MACACCESS_DEST_IDX_SET(port) |
+	       ANA_MACACCESS_MAC_CPU_COPY_SET(cpu_copy) |
+	       ANA_MACACCESS_ENTRYTYPE_SET(type) |
+	       ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_LEARN),
+	       lan9645x, ANA_MACACCESS);
+
+	err = lan9645x_mac_wait_for_completion(lan9645x, NULL);
+	if (err)
+		return err;
+
+	if (ANA_ANEVENTS_CPU_LEARN_FAILED_GET(lan_rd(lan9645x, ANA_ANEVENTS))) {
+		lan_wr(ANA_ANEVENTS_CPU_LEARN_FAILED, lan9645x, ANA_ANEVENTS);
+		return -ENOSPC;
+	}
+
+	return 0;
+}
+
+int lan9645x_mact_learn(struct lan9645x *lan9645x, int port,
+			const unsigned char mac[ETH_ALEN], unsigned int vid,
+			enum macaccess_entry_type type, bool cpu_copy)
+{
+	int err;
+
+	mutex_lock(&lan9645x->mact_lock);
+	err = __lan9645x_mact_learn(lan9645x, port, mac, vid, type, cpu_copy);
+	mutex_unlock(&lan9645x->mact_lock);
+
+	return err;
+}
+
+int lan9645x_mact_flush(struct lan9645x *lan9645x, int port)
+{
+	int err;
+
+	mutex_lock(&lan9645x->mact_lock);
+	/* MAC table entries with dst index matching port are aged on scan. */
+	lan_wr(ANA_ANAGEFIL_PID_EN_SET(1) |
+	       ANA_ANAGEFIL_PID_VAL_SET(port),
+	       lan9645x, ANA_ANAGEFIL);
+
+	/* Flushing requires two scans. First sets AGE_FLAG=1, second removes
+	 * entries with AGE_FLAG=1.
+	 */
+	lan_wr(ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_AGE),
+	       lan9645x,
+	       ANA_MACACCESS);
+
+	err = lan9645x_mac_wait_for_completion(lan9645x, NULL);
+	if (err)
+		goto mact_unlock;
+
+	lan_wr(ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_AGE),
+	       lan9645x,
+	       ANA_MACACCESS);
+
+	err = lan9645x_mac_wait_for_completion(lan9645x, NULL);
+
+mact_unlock:
+	lan_wr(0, lan9645x, ANA_ANAGEFIL);
+	mutex_unlock(&lan9645x->mact_lock);
+	return err;
+}
+
+int lan9645x_mac_init(struct lan9645x *lan9645x)
+{
+	u32 val;
+	int err;
+
+	/* Clear the MAC table */
+	lan_wr(ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_INIT), lan9645x,
+	       ANA_MACACCESS);
+
+	err = lan9645x_rd_poll_timeout(lan9645x, ANA_MACACCESS, val,
+				       ANA_MACACCESS_MAC_TABLE_CMD_GET(val) ==
+				       CMD_IDLE);
+	if (err) {
+		dev_err(lan9645x->dev, "MAC table clear timeout\n");
+		return err;
+	}
+
+	lan_wr(ANA_ANEVENTS_CPU_LEARN_FAILED, lan9645x, ANA_ANEVENTS);
+
+	mutex_init(&lan9645x->mact_lock);
+	return 0;
+}
+
+void lan9645x_mac_deinit(struct lan9645x *lan9645x)
+{
+	mutex_destroy(&lan9645x->mact_lock);
+}
+
+int lan9645x_mact_dsa_dump(struct lan9645x *lan9645x, int port,
+			   dsa_fdb_dump_cb_t *cb, void *data)
+{
+	u8 mac[ETH_ALEN] __aligned(2);
+	u32 mach, macl, maca;
+	int err = 0;
+	u64 addr;
+	int iter;
+	u16 vid;
+	u8 type;
+
+	mutex_lock(&lan9645x->mact_lock);
+
+	lan_wr(ANA_ANAGEFIL_PID_EN_SET(1) |
+	       ANA_ANAGEFIL_PID_VAL_SET(port),
+	       lan9645x, ANA_ANAGEFIL);
+
+	lan_wr(0, lan9645x, ANA_MACHDATA);
+	lan_wr(0, lan9645x, ANA_MACLDATA);
+
+	type = ENTRYTYPE_NORMAL;
+
+	for (iter = 0; iter < MACT_GET_NEXT_MAX; iter++) {
+		/* CMD_GET_NEXT returns the smallest entry ordered above the one
+		 * left in the registers.
+		 * We rely on mach, macl and type being set correctly in
+		 * the registers from previous round, vis a vis the GET_NEXT
+		 * semantics, so locking entire loop is important.
+		 */
+		lan_wr(ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_GET_NEXT) |
+		       ANA_MACACCESS_ENTRYTYPE_SET(type),
+		       lan9645x, ANA_MACACCESS);
+
+		err = lan9645x_mac_wait_for_completion(lan9645x, &maca);
+		if (err)
+			break;
+
+		if (ANA_MACACCESS_VALID_GET(maca) == 0)
+			break;
+
+		type = ANA_MACACCESS_ENTRYTYPE_GET(maca);
+		mach = lan_rd(lan9645x, ANA_MACHDATA);
+		macl = lan_rd(lan9645x, ANA_MACLDATA);
+
+		if (ANA_MACACCESS_DEST_IDX_GET(maca) == port &&
+		    (type == ENTRYTYPE_NORMAL || type == ENTRYTYPE_LOCKED)) {
+			addr = (u64)ANA_MACHDATA_MACHDATA_GET(mach) << 32 |
+			       macl;
+			u64_to_ether_addr(addr, mac);
+			vid = ANA_MACHDATA_VID_GET(mach);
+			if (vid > VLAN_MAX)
+				vid = 0;
+
+			err = cb(mac, vid, type == ENTRYTYPE_LOCKED, data);
+			if (err)
+				break;
+		}
+	}
+
+	if (iter == MACT_GET_NEXT_MAX) {
+		dev_err(lan9645x->dev,
+			"MAC table walk on port %d did not terminate\n", port);
+		err = -EIO;
+	}
+
+	/* Remove aging filters */
+	lan_wr(0, lan9645x, ANA_ANAGEFIL);
+
+	mutex_unlock(&lan9645x->mact_lock);
+
+	return err;
+}
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
index e6c10399154b..904b51a55d93 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
@@ -71,6 +71,7 @@ static void lan9645x_teardown(struct dsa_switch *ds)
 {
 	struct lan9645x *lan9645x = ds->priv;
 
+	lan9645x_mac_deinit(lan9645x);
 	mutex_destroy(&lan9645x->fwd_domain_lock);
 	lan9645x_npi_port_deinit(lan9645x, lan9645x->npi);
 }
@@ -162,6 +163,9 @@ static int lan9645x_setup(struct dsa_switch *ds)
 
 	mutex_init(&lan9645x->fwd_domain_lock);
 	err = lan9645x_vlan_init(lan9645x);
+	if (err)
+		goto err_mutex;
+	err = lan9645x_mac_init(lan9645x);
 	if (err)
 		goto err_mutex;
 
@@ -287,6 +291,8 @@ static int lan9645x_setup(struct dsa_switch *ds)
 	       ANA_PGID(PGID_BC));
 
 	ds->mtu_enforcement_ingress = true;
+	ds->assisted_learning_on_cpu_port = true;
+	ds->fdb_isolation = true;
 
 	dev_info(lan9645x->dev,
 		 "SKU features: max_ports=%d\n",
@@ -368,6 +374,102 @@ static int lan9645x_port_vlan_del(struct dsa_switch *ds, int port,
 	return err;
 }
 
+static void lan9645x_port_fast_age(struct dsa_switch *ds, int port)
+{
+	int err;
+
+	err = lan9645x_mact_flush(ds->priv, port);
+	if (err)
+		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
+			port, ERR_PTR(err));
+}
+
+static int lan9645x_fdb_dump(struct dsa_switch *ds, int port,
+			     dsa_fdb_dump_cb_t *cb, void *data)
+{
+	return lan9645x_mact_dsa_dump(ds->priv, port, cb, data);
+}
+
+static u16 lan9645x_fdb_vid(u16 vid, const struct dsa_db *db)
+{
+	if (vid)
+		return vid;
+
+	return lan9645x_vlan_unaware_pvid(db->type == DSA_DB_BRIDGE ?
+					  db->bridge.num : 0);
+}
+
+static bool lan9645x_db_equal(const struct dsa_db *a, const struct dsa_db *b)
+{
+	if (a->type != b->type)
+		return false;
+
+	switch (a->type) {
+	case DSA_DB_PORT:
+		return a->dp == b->dp;
+	case DSA_DB_LAG:
+		return a->lag.dev == b->lag.dev;
+	case DSA_DB_BRIDGE:
+		return a->bridge.num == b->bridge.num;
+	}
+
+	return false;
+}
+
+static bool lan9645x_host_addr_shared(struct dsa_port *dp,
+				      const struct list_head *addrs,
+				      const unsigned char *addr, u16 vid,
+				      const struct dsa_db *db)
+{
+	u16 hw_vid = lan9645x_fdb_vid(vid, db);
+	struct dsa_mac_addr *a;
+
+	lockdep_assert_held(&dp->addr_lists_lock);
+
+	list_for_each_entry(a, addrs, list) {
+		if (ether_addr_equal(a->addr, addr) &&
+		    !lan9645x_db_equal(&a->db, db) &&
+		    lan9645x_fdb_vid(a->vid, &a->db) == hw_vid)
+			return true;
+	}
+
+	return false;
+}
+
+static int lan9645x_fdb_add(struct dsa_switch *ds, int port,
+			    const unsigned char *addr, u16 vid,
+			    struct dsa_db db)
+{
+	struct dsa_port *dp = dsa_to_port(ds, port);
+	struct lan9645x *lan9645x = ds->priv;
+	int dest;
+
+	if (dsa_port_is_cpu(dp) &&
+	    lan9645x_host_addr_shared(dp, &dp->fdbs, addr, vid, &db))
+		return 0;
+
+	dest = dsa_port_is_cpu(dp) ? PGID_CPU : port;
+
+	return lan9645x_mact_learn(lan9645x, dest, addr,
+				   lan9645x_fdb_vid(vid, &db),
+				   ENTRYTYPE_LOCKED, false);
+}
+
+static int lan9645x_fdb_del(struct dsa_switch *ds, int port,
+			    const unsigned char *addr, u16 vid,
+			    struct dsa_db db)
+{
+	struct dsa_port *dp = dsa_to_port(ds, port);
+	struct lan9645x *lan9645x = ds->priv;
+
+	if (dsa_port_is_cpu(dp) &&
+	    lan9645x_host_addr_shared(dp, &dp->fdbs, addr, vid, &db))
+		return 0;
+
+	return lan9645x_mact_forget(lan9645x, addr, lan9645x_fdb_vid(vid, &db),
+				    ENTRYTYPE_LOCKED);
+}
+
 static const struct dsa_switch_ops lan9645x_switch_ops = {
 	.get_tag_protocol		= lan9645x_get_tag_protocol,
 
@@ -386,6 +488,12 @@ static const struct dsa_switch_ops lan9645x_switch_ops = {
 	.port_vlan_filtering		= lan9645x_port_vlan_filtering,
 	.port_vlan_add			= lan9645x_port_vlan_add,
 	.port_vlan_del			= lan9645x_port_vlan_del,
+
+	/* MAC table integration */
+	.port_fast_age			= lan9645x_port_fast_age,
+	.port_fdb_dump			= lan9645x_fdb_dump,
+	.port_fdb_add			= lan9645x_fdb_add,
+	.port_fdb_del			= lan9645x_fdb_del,
 };
 
 static int lan9645x_request_target_regmaps(struct lan9645x *lan9645x)
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
index ea093a293328..1bf9f49dc231 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
@@ -170,6 +170,19 @@ struct lan9645x_vlan {
 	    untagged: 9; /* ports 0-8 */
 };
 
+/* MAC table entry types.
+ * ENTRYTYPE_NORMAL is subject to aging.
+ * ENTRYTYPE_LOCKED is not subject to aging.
+ * ENTRYTYPE_MACV4 is not subject to aging. For IPv4 multicast.
+ * ENTRYTYPE_MACV6 is not subject to aging. For IPv6 multicast.
+ */
+enum macaccess_entry_type {
+	ENTRYTYPE_NORMAL = 0,
+	ENTRYTYPE_LOCKED,
+	ENTRYTYPE_MACV4,
+	ENTRYTYPE_MACV6,
+};
+
 struct lan9645x {
 	struct device *dev;
 	struct dsa_switch *ds;
@@ -183,6 +196,7 @@ struct lan9645x {
 
 	/* lock forwarding configuration and vlan table */
 	struct mutex fwd_domain_lock;
+	struct mutex mact_lock; /* serialize mac table register access */
 
 	int num_port_dis;
 
@@ -285,6 +299,21 @@ static inline u32 lan_rel_addr(enum lan9645x_target t, int tinst, int tcnt,
 			      rcnt, rwidth);
 }
 
+static inline u32 lan_rd(struct lan9645x *lan9645x, enum lan9645x_target t,
+			 int tinst, int tcnt, int gbase, int ginst,
+			 int gcnt, int gwidth, int raddr, int rinst,
+			 int rcnt, int rwidth)
+{
+	u32 addr, val = 0;
+
+	addr = lan_rel_addr(t, tinst, tcnt, gbase, ginst, gcnt, gwidth,
+			    raddr, rinst, rcnt, rwidth);
+
+	WARN_ON_ONCE(regmap_read(lan_tgt2rmap(lan9645x, t, tinst), addr, &val));
+
+	return val;
+}
+
 static inline struct regmap *lan_rmap(struct lan9645x *lan9645x,
 				      enum lan9645x_target t, int tinst,
 				      int tcnt, int gbase, int ginst,
@@ -348,4 +377,17 @@ int lan9645x_vlan_port_add_vlan(struct lan9645x_port *p, u16 vid, bool pvid,
 int lan9645x_vlan_port_del_vlan(struct lan9645x_port *p, u16 vid);
 void lan9645x_vlan_set_hostmode(struct lan9645x_port *p);
 
+/* MAC table: lan9645x_mac.c */
+int lan9645x_mact_flush(struct lan9645x *lan9645x, int port);
+int lan9645x_mact_learn(struct lan9645x *lan9645x, int port,
+			const unsigned char mac[ETH_ALEN], unsigned int vid,
+			enum macaccess_entry_type type, bool cpu_copy);
+int lan9645x_mact_forget(struct lan9645x *lan9645x,
+			 const unsigned char mac[ETH_ALEN], unsigned int vid,
+			 enum macaccess_entry_type type);
+int lan9645x_mac_init(struct lan9645x *lan9645x);
+void lan9645x_mac_deinit(struct lan9645x *lan9645x);
+int lan9645x_mact_dsa_dump(struct lan9645x *lan9645x, int port,
+			   dsa_fdb_dump_cb_t *cb, void *data);
+
 #endif /* __LAN9645X_MAIN_H__ */

-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH net-next 4/5] net: dsa: lan9645x: add mdb management
  2026-11-24  8:57 [PATCH net-next 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
                   ` (2 preceding siblings ...)
  2026-11-24  8:57 ` [PATCH net-next 3/5] net: dsa: lan9645x: add mac table integration Jens Emil Schulz Østergaard
@ 2026-11-24  8:57 ` Jens Emil Schulz Østergaard
  2026-11-25  8:58   ` netdev-bot+sashiko
  2026-11-24  8:57 ` [PATCH net-next 5/5] net: dsa: lan9645x: add bridge support Jens Emil Schulz Østergaard
  4 siblings, 1 reply; 10+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-24  8:57 UTC (permalink / raw)
  To: UNGLinuxDriver, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Russell King
  Cc: linux-kernel, netdev, Jens Emil Schulz Østergaard

Add support for dsa mdb callbacks. L2 multicast and IP multicast is
handled differently. IP multicast stores the port group mask inline in
the mac table, and host membership in the MAC_CPU_COPY bit of the
entry. L2 multicast points at a PGID, whose destination mask is the port
group, including the CPU port module.

Each L2 multicast group owns one of the general purpose PGIDs. When they
are exhausted, adding a new group fails with -ENOSPC and the group is
not offloaded.

Removing a port from a group always succeeds. The
switchdev_port_obj_del_deferred() path only logs an error, while
br_switchdev_mdb_replay() abandons the remaining entries after the first
failure, so failing one delete during a bridge leave would leave the
other groups programmed. A failed MAC table command is logged instead.

IGMP and MLD frames are redirected to the CPU, and IPv4 multicast
control frames are copied to it, so that the bridge can snoop them.

Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>
---
 drivers/net/dsa/microchip/lan9645x/Makefile        |   1 +
 drivers/net/dsa/microchip/lan9645x/lan9645x_main.c |  61 +++++
 drivers/net/dsa/microchip/lan9645x/lan9645x_main.h |  20 ++
 drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c  | 256 +++++++++++++++++++++
 drivers/net/dsa/microchip/lan9645x/lan9645x_port.c |   8 +
 5 files changed, 346 insertions(+)

diff --git a/drivers/net/dsa/microchip/lan9645x/Makefile b/drivers/net/dsa/microchip/lan9645x/Makefile
index 70815edca5b9..2413d11fe849 100644
--- a/drivers/net/dsa/microchip/lan9645x/Makefile
+++ b/drivers/net/dsa/microchip/lan9645x/Makefile
@@ -4,6 +4,7 @@ obj-$(CONFIG_NET_DSA_MICROCHIP_LAN9645X) += mchp-lan9645x.o
 mchp-lan9645x-objs := \
 	lan9645x_mac.o \
 	lan9645x_main.o \
+	lan9645x_mdb.o \
 	lan9645x_npi.o \
 	lan9645x_phylink.o \
 	lan9645x_port.o \
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
index 904b51a55d93..de432772f639 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
@@ -71,6 +71,7 @@ static void lan9645x_teardown(struct dsa_switch *ds)
 {
 	struct lan9645x *lan9645x = ds->priv;
 
+	lan9645x_mdb_deinit(lan9645x);
 	lan9645x_mac_deinit(lan9645x);
 	mutex_destroy(&lan9645x->fwd_domain_lock);
 	lan9645x_npi_port_deinit(lan9645x, lan9645x->npi);
@@ -168,6 +169,7 @@ static int lan9645x_setup(struct dsa_switch *ds)
 	err = lan9645x_mac_init(lan9645x);
 	if (err)
 		goto err_mutex;
+	lan9645x_mdb_init(lan9645x);
 
 	/* Link Aggregation Mode: NETDEV_LAG_HASH_L2 */
 	lan_wr(ANA_AGGR_CFG_AC_SMAC_ENA |
@@ -255,6 +257,17 @@ static int lan9645x_setup(struct dsa_switch *ds)
 			lan9645x, ANA_CPUQ_8021_CFG(i));
 	}
 
+	/* Use CPU queues to communicate frame classification to the CPU */
+	lan_rmw(ANA_CPUQ_CFG_CPUQ_MAC_COPY_SET(LAN9645X_CPUQ_DEF) |
+		ANA_CPUQ_CFG_CPUQ_IGMP_SET(LAN9645X_CPUQ_TRAP) |
+		ANA_CPUQ_CFG_CPUQ_MLD_SET(LAN9645X_CPUQ_TRAP) |
+		ANA_CPUQ_CFG_CPUQ_IPMC_CTRL_SET(LAN9645X_CPUQ_COPY),
+		ANA_CPUQ_CFG_CPUQ_MAC_COPY |
+		ANA_CPUQ_CFG_CPUQ_IGMP |
+		ANA_CPUQ_CFG_CPUQ_MLD |
+		ANA_CPUQ_CFG_CPUQ_IPMC_CTRL,
+		lan9645x, ANA_CPUQ_CFG);
+
 	/* Reserve 1664 bytes, 26 buffer cells, per (port, prio) for source
 	 * tracking (resource 0, indices 0..95) and destination tracking
 	 * (resource 2, indices 512..607). These are access watermarks, not
@@ -470,6 +483,50 @@ static int lan9645x_fdb_del(struct dsa_switch *ds, int port,
 				    ENTRYTYPE_LOCKED);
 }
 
+static int lan9645x_port_mdb_add(struct dsa_switch *ds, int port,
+				 const struct switchdev_obj_port_mdb *mdb,
+				 struct dsa_db db)
+{
+	struct dsa_port *dp = dsa_to_port(ds, port);
+	struct lan9645x *lan9645x = ds->priv;
+
+	dev_dbg(lan9645x->dev, "port=%d addr=%pM vid=%u\n", port, mdb->addr,
+		mdb->vid);
+
+	if (dsa_port_is_cpu(dp) &&
+	    lan9645x_host_addr_shared(dp, &dp->mdbs, mdb->addr, mdb->vid, &db))
+		return 0;
+
+	if (port == lan9645x->npi)
+		port = lan9645x->num_phys_ports;
+
+	return lan9645x_mdb_add(lan9645x, port, mdb->addr,
+				lan9645x_fdb_vid(mdb->vid, &db));
+}
+
+static int lan9645x_port_mdb_del(struct dsa_switch *ds, int port,
+				 const struct switchdev_obj_port_mdb *mdb,
+				 struct dsa_db db)
+{
+	struct dsa_port *dp = dsa_to_port(ds, port);
+	struct lan9645x *lan9645x = ds->priv;
+
+	dev_dbg(lan9645x->dev, "port=%d addr=%pM vid=%u\n", port, mdb->addr,
+		mdb->vid);
+
+	if (dsa_port_is_cpu(dp) &&
+	    lan9645x_host_addr_shared(dp, &dp->mdbs, mdb->addr, mdb->vid, &db))
+		return 0;
+
+	if (port == lan9645x->npi)
+		port = lan9645x->num_phys_ports;
+
+	lan9645x_mdb_del(lan9645x, port, mdb->addr,
+			 lan9645x_fdb_vid(mdb->vid, &db));
+
+	return 0;
+}
+
 static const struct dsa_switch_ops lan9645x_switch_ops = {
 	.get_tag_protocol		= lan9645x_get_tag_protocol,
 
@@ -494,6 +551,10 @@ static const struct dsa_switch_ops lan9645x_switch_ops = {
 	.port_fdb_dump			= lan9645x_fdb_dump,
 	.port_fdb_add			= lan9645x_fdb_add,
 	.port_fdb_del			= lan9645x_fdb_del,
+
+	/* Multicast database */
+	.port_mdb_add			= lan9645x_port_mdb_add,
+	.port_mdb_del			= lan9645x_port_mdb_del,
 };
 
 static int lan9645x_request_target_regmaps(struct lan9645x *lan9645x)
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
index 1bf9f49dc231..caad6871a657 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
@@ -85,6 +85,10 @@
 #define PGID_AGGR			64
 #define PGID_SRC			80
 
+/* General purpose PGIDs. PGID 57 is reserved for a later blackhole PGID. */
+#define PGID_GP_START			NUM_PHYS_PORTS
+#define PGID_GP_END			(PGID_AGGR - 7)
+
 /* Reserved PGIDs. */
 #define PGID_CPU			(PGID_AGGR - 6)
 #define PGID_UC				(PGID_AGGR - 5)
@@ -198,6 +202,14 @@ struct lan9645x {
 	struct mutex fwd_domain_lock;
 	struct mutex mact_lock; /* serialize mac table register access */
 
+	/* Multicast Forwarding Database */
+	struct list_head mdb_entries;
+	DECLARE_BITMAP(mdb_pgids, PGID_GP_END);
+	/* lock for mdb_entries and mdb_pgids. Must be taken before mact_lock
+	 * if both are taken.
+	 */
+	struct mutex mdb_lock;
+
 	int num_port_dis;
 
 	/* VLAN entries */
@@ -390,4 +402,12 @@ void lan9645x_mac_deinit(struct lan9645x *lan9645x);
 int lan9645x_mact_dsa_dump(struct lan9645x *lan9645x, int port,
 			   dsa_fdb_dump_cb_t *cb, void *data);
 
+/* Multicast Database lan9645x_mdb.c */
+int lan9645x_mdb_add(struct lan9645x *lan9645x, int chip_port,
+		     const unsigned char addr[ETH_ALEN], u16 vid);
+void lan9645x_mdb_del(struct lan9645x *lan9645x, int chip_port,
+		      const unsigned char addr[ETH_ALEN], u16 vid);
+void lan9645x_mdb_init(struct lan9645x *lan9645x);
+void lan9645x_mdb_deinit(struct lan9645x *lan9645x);
+
 #endif /* __LAN9645X_MAIN_H__ */
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c
new file mode 100644
index 000000000000..6864b89d5a2d
--- /dev/null
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c
@@ -0,0 +1,256 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Copyright (C) 2026 Microchip Technology Inc.
+ */
+
+#include "lan9645x_main.h"
+
+/* IPv4/IPv6 types the mac entry dest_idx is not used for forwarding. The
+ * datasheet recommends using 0 as the dummy index.
+ */
+#define IP_ENTRY_PGID		0
+
+struct lan9645x_mdb_entry {
+	struct list_head list;
+	unsigned char mac[ETH_ALEN];
+	u16 vid;
+	u16 ports;
+	int pgid;
+	enum macaccess_entry_type type;
+};
+
+void lan9645x_mdb_init(struct lan9645x *lan9645x)
+{
+	INIT_LIST_HEAD(&lan9645x->mdb_entries);
+	bitmap_zero(lan9645x->mdb_pgids, PGID_GP_END);
+	mutex_init(&lan9645x->mdb_lock);
+}
+
+static enum macaccess_entry_type lan9645x_mdb_classify(const unsigned char *mac)
+{
+	if (mac[0] == 0x01 && mac[1] == 0x00 && mac[2] == 0x5e)
+		return ENTRYTYPE_MACV4;
+	if (mac[0] == 0x33 && mac[1] == 0x33)
+		return ENTRYTYPE_MACV6;
+	return ENTRYTYPE_LOCKED;
+}
+
+static struct lan9645x_mdb_entry *
+lan9645x_mdb_entry_lookup(struct lan9645x *lan9645x, const unsigned char *mac,
+			  u16 vid)
+{
+	struct lan9645x_mdb_entry *mdb;
+
+	lockdep_assert_held(&lan9645x->mdb_lock);
+
+	list_for_each_entry(mdb, &lan9645x->mdb_entries, list) {
+		if (ether_addr_equal(mdb->mac, mac) && mdb->vid == vid)
+			return mdb;
+	}
+
+	return NULL;
+}
+
+static void lan9645x_mdb_encode_mac(unsigned char *dst,
+				    const unsigned char *mac, u16 ports,
+				    enum macaccess_entry_type type)
+{
+	ether_addr_copy(dst, mac);
+
+	/* The HW encodes the portmask in the high bits of the mac for ip
+	 * multicast entries, to save on the limited PGID resources.
+	 *
+	 * IPv4 Multicast DMAC: 0x01005Exxxxxx
+	 * IPv6 Multicast DMAC: 0x3333xxxxxxxx
+	 *
+	 * which gives us 24 or 16 bits to encode the portmask.
+	 */
+	if (type == ENTRYTYPE_MACV4) {
+		dst[0] = 0;
+		dst[1] = ports >> 8;
+		dst[2] = ports & 0xff;
+	} else if (type == ENTRYTYPE_MACV6) {
+		dst[0] = ports >> 8;
+		dst[1] = ports & 0xff;
+	}
+}
+
+static int lan9645x_mdb_write_ports(struct lan9645x *lan9645x,
+				    struct lan9645x_mdb_entry *mdb_entry,
+				    u16 ports)
+{
+	unsigned char mac[ETH_ALEN] __aligned(2);
+	bool cpu_copy;
+
+	lockdep_assert_held(&lan9645x->mdb_lock);
+
+	if (mdb_entry->type == ENTRYTYPE_LOCKED) {
+		lan_rmw(ANA_PGID_PGID_SET(ports), ANA_PGID_PGID, lan9645x,
+			ANA_PGID(mdb_entry->pgid));
+		return 0;
+	}
+
+	lan9645x_mdb_encode_mac(mac, mdb_entry->mac, ports, mdb_entry->type);
+	cpu_copy = !!(ports & BIT(lan9645x->num_phys_ports));
+
+	/* For IP multicast, the hardware lookup uses the DMAC
+	 * (01:00:5E:.. / 33:33:..) as the (mac, vid) key, not the encoded mac.
+	 * Therefore, this CMD_LEARN will atomically rewrite the existing
+	 * hardware entry. We intentionally do not do a forget before learn
+	 * sequence, as that would not be atomic, and leave a forwarding gap.
+	 */
+	return lan9645x_mact_learn(lan9645x, IP_ENTRY_PGID, mac, mdb_entry->vid,
+				   mdb_entry->type, cpu_copy);
+}
+
+static int lan9645x_mdb_entry_create(struct lan9645x *lan9645x, int chip_port,
+				     const unsigned char addr[ETH_ALEN],
+				     u16 vid)
+{
+	struct lan9645x_mdb_entry *mdb_entry;
+	int err;
+
+	lockdep_assert_held(&lan9645x->mdb_lock);
+
+	mdb_entry = kzalloc_obj(*mdb_entry);
+	if (!mdb_entry)
+		return -ENOMEM;
+
+	ether_addr_copy(mdb_entry->mac, addr);
+	mdb_entry->vid = vid;
+	mdb_entry->ports = BIT(chip_port);
+	mdb_entry->pgid = IP_ENTRY_PGID;
+	mdb_entry->type = lan9645x_mdb_classify(addr);
+
+	if (mdb_entry->type == ENTRYTYPE_LOCKED) {
+		mdb_entry->pgid = find_next_zero_bit(lan9645x->mdb_pgids,
+						     PGID_GP_END,
+						     PGID_GP_START);
+		if (mdb_entry->pgid >= PGID_GP_END) {
+			kfree(mdb_entry);
+			return -ENOSPC;
+		}
+	}
+
+	err = lan9645x_mdb_write_ports(lan9645x, mdb_entry, mdb_entry->ports);
+	if (!err && mdb_entry->type == ENTRYTYPE_LOCKED)
+		err = lan9645x_mact_learn(lan9645x, mdb_entry->pgid, addr, vid,
+					  mdb_entry->type, false);
+	if (err) {
+		kfree(mdb_entry);
+		return err;
+	}
+
+	if (mdb_entry->type == ENTRYTYPE_LOCKED)
+		set_bit(mdb_entry->pgid, lan9645x->mdb_pgids);
+
+	list_add_tail(&mdb_entry->list, &lan9645x->mdb_entries);
+
+	return 0;
+}
+
+static void lan9645x_mdb_entry_destroy(struct lan9645x *lan9645x,
+				       struct lan9645x_mdb_entry *mdb_entry)
+{
+	int err;
+
+	lockdep_assert_held(&lan9645x->mdb_lock);
+
+	/* The encoded bytes are not part of the (mac, vid) key used for
+	 * lookups in the mactable, for entries of type 2
+	 * (IPv4 Multicast) and type 3 (IPv6 Multicast). For these types
+	 * the mac used in the key is the 'real' mac e.g.
+	 *
+	 * Type 2 (IPv4 multicast):
+	 * KEY_MAC = 0x01005E000000 | MACLDATA[23:0]
+	 *
+	 * Type 3 (IPv6 multicast):
+	 * KEY_MAC = 0x333300000000 | MACLDATA[31:0]
+	 *
+	 * This holds for both the datapath and the CPU access path.
+	 * Therefore, it is not necessary to encode the mac before a
+	 * CMD_FORGET, because the bytes it changes are unused for the
+	 * lookup.
+	 */
+	err = lan9645x_mact_forget(lan9645x, mdb_entry->mac, mdb_entry->vid,
+				   mdb_entry->type);
+	if (err)
+		dev_err(lan9645x->dev, "Forgetting %pM vid %u returned %pe\n",
+			mdb_entry->mac, mdb_entry->vid, ERR_PTR(err));
+	else if (mdb_entry->type == ENTRYTYPE_LOCKED)
+		clear_bit(mdb_entry->pgid, lan9645x->mdb_pgids);
+
+	list_del(&mdb_entry->list);
+	kfree(mdb_entry);
+}
+
+int lan9645x_mdb_add(struct lan9645x *lan9645x, int chip_port,
+		     const unsigned char addr[ETH_ALEN], u16 vid)
+{
+	struct lan9645x_mdb_entry *mdb_entry;
+	u16 new_ports;
+	int err = 0;
+
+	mutex_lock(&lan9645x->mdb_lock);
+
+	mdb_entry = lan9645x_mdb_entry_lookup(lan9645x, addr, vid);
+	if (!mdb_entry) {
+		err = lan9645x_mdb_entry_create(lan9645x, chip_port, addr, vid);
+		goto out;
+	}
+
+	if (mdb_entry->ports & BIT(chip_port))
+		goto out;
+
+	new_ports = mdb_entry->ports | BIT(chip_port);
+	err = lan9645x_mdb_write_ports(lan9645x, mdb_entry, new_ports);
+	if (!err)
+		mdb_entry->ports = new_ports;
+
+out:
+	mutex_unlock(&lan9645x->mdb_lock);
+	return err;
+}
+
+void lan9645x_mdb_del(struct lan9645x *lan9645x, int chip_port,
+		      const unsigned char addr[ETH_ALEN], u16 vid)
+{
+	struct lan9645x_mdb_entry *mdb_entry;
+	int err;
+
+	mutex_lock(&lan9645x->mdb_lock);
+
+	mdb_entry = lan9645x_mdb_entry_lookup(lan9645x, addr, vid);
+	if (!mdb_entry || !(mdb_entry->ports & BIT(chip_port)))
+		goto out;
+
+	mdb_entry->ports &= ~BIT(chip_port);
+
+	if (!mdb_entry->ports) {
+		lan9645x_mdb_entry_destroy(lan9645x, mdb_entry);
+		goto out;
+	}
+
+	err = lan9645x_mdb_write_ports(lan9645x, mdb_entry, mdb_entry->ports);
+	if (err)
+		dev_err(lan9645x->dev,
+			"Removing port %d from %pM vid %u returned %pe\n",
+			chip_port, mdb_entry->mac, mdb_entry->vid,
+			ERR_PTR(err));
+
+out:
+	mutex_unlock(&lan9645x->mdb_lock);
+}
+
+void lan9645x_mdb_deinit(struct lan9645x *lan9645x)
+{
+	struct lan9645x_mdb_entry *mdb, *tmp;
+
+	mutex_lock(&lan9645x->mdb_lock);
+	list_for_each_entry_safe(mdb, tmp, &lan9645x->mdb_entries, list) {
+		list_del(&mdb->list);
+		kfree(mdb);
+	}
+	mutex_unlock(&lan9645x->mdb_lock);
+
+	mutex_destroy(&lan9645x->mdb_lock);
+}
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_port.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_port.c
index 2571380f2c02..edcedd7723c9 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_port.c
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_port.c
@@ -166,6 +166,14 @@ int lan9645x_port_setup(struct dsa_switch *ds, int port)
 		mutex_lock(&lan9645x->fwd_domain_lock);
 		lan9645x_vlan_set_hostmode(p);
 		mutex_unlock(&lan9645x->fwd_domain_lock);
+
+		lan_rmw(ANA_CPU_FWD_CFG_IGMP_REDIR_ENA_SET(1) |
+			ANA_CPU_FWD_CFG_MLD_REDIR_ENA_SET(1) |
+			ANA_CPU_FWD_CFG_IPMC_CTRL_COPY_ENA_SET(1),
+			ANA_CPU_FWD_CFG_IGMP_REDIR_ENA |
+			ANA_CPU_FWD_CFG_MLD_REDIR_ENA |
+			ANA_CPU_FWD_CFG_IPMC_CTRL_COPY_ENA,
+			lan9645x, ANA_CPU_FWD_CFG(p->chip_port));
 	}
 
 	return 0;

-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH net-next 5/5] net: dsa: lan9645x: add bridge support
  2026-11-24  8:57 [PATCH net-next 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
                   ` (3 preceding siblings ...)
  2026-11-24  8:57 ` [PATCH net-next 4/5] net: dsa: lan9645x: add mdb management Jens Emil Schulz Østergaard
@ 2026-11-24  8:57 ` Jens Emil Schulz Østergaard
  2026-11-25  8:58   ` netdev-bot+sashiko
  4 siblings, 1 reply; 10+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-24  8:57 UTC (permalink / raw)
  To: UNGLinuxDriver, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Russell King
  Cc: linux-kernel, netdev, Jens Emil Schulz Østergaard

Add support for hardware offloading of the bridge. Up to 8 bridges can
be offloaded, and each VLAN-unaware bridge uses its own reserved VID.
The source port masks only forward between forwarding ports of the same
bridge.

At most one bridge can be VLAN-aware, since the MAC table has no other
way to keep the FDB domains of two bridges with the same VIDs apart.
Joining a second VLAN-aware bridge or enabling VLAN filtering on a
second bridge is refused.

The CPU port module is no longer in the unicast and multicast flood
masks by default. It is added through .port_set_host_flood while any
port is promiscuous or allmulti. The flood masks are global and not per
ingress port, so one port requesting host flooding makes unknown unicast
or unregistered multicast from every port reach the CPU.

Mac entry ageing can be configured with a 2s granularity, and a
requested time is rounded down. An ANA_AUTOAGE.AGE_PERIOD of 0 stops the
analyzer from ageing at all, which is the opposite of the software
bridge with an ageing time of 0. So ds->ageing_time_min and
ds->ageing_time_max let the DSA core reject unsupported configurations.

Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>
---
 drivers/net/dsa/microchip/lan9645x/lan9645x_main.c | 317 ++++++++++++++++++++-
 drivers/net/dsa/microchip/lan9645x/lan9645x_main.h |   7 +
 drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c |   9 +
 3 files changed, 322 insertions(+), 11 deletions(-)

diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
index de432772f639..a78410efb973 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
@@ -285,19 +285,15 @@ static int lan9645x_setup(struct dsa_switch *ds)
 
 	lan9645x_port_cpu_init(lan9645x);
 
-	/* Multicast to cpu and all front ports */
-	lan_wr(BIT(lan9645x->num_phys_ports) | front_ports, lan9645x,
-	       ANA_PGID(PGID_MC));
+	/* Multicast to all front ports */
+	lan_wr(front_ports, lan9645x, ANA_PGID(PGID_MC));
 
-	/* IP multicast to cpu and all front ports */
-	lan_wr(BIT(lan9645x->num_phys_ports) | front_ports, lan9645x,
-	       ANA_PGID(PGID_MCIPV4));
-	lan_wr(BIT(lan9645x->num_phys_ports) | front_ports, lan9645x,
-	       ANA_PGID(PGID_MCIPV6));
+	/* IP multicast to all front ports */
+	lan_wr(front_ports, lan9645x, ANA_PGID(PGID_MCIPV4));
+	lan_wr(front_ports, lan9645x, ANA_PGID(PGID_MCIPV6));
 
-	/* Unicast to cpu and all front ports */
-	lan_wr(BIT(lan9645x->num_phys_ports) | front_ports, lan9645x,
-	       ANA_PGID(PGID_UC));
+	/* Unicast to all front ports */
+	lan_wr(front_ports, lan9645x, ANA_PGID(PGID_UC));
 
 	/* Broadcast to cpu and all front ports */
 	lan_wr(BIT(lan9645x->num_phys_ports) | front_ports, lan9645x,
@@ -306,6 +302,20 @@ static int lan9645x_setup(struct dsa_switch *ds)
 	ds->mtu_enforcement_ingress = true;
 	ds->assisted_learning_on_cpu_port = true;
 	ds->fdb_isolation = true;
+	ds->max_num_bridges = ds->num_ports;
+
+	/* ANA_AUTOAGE_AGE_PERIOD is a seconds-based field and entries are
+	 * aged after 2 * AGE_PERIOD, giving (2 * FIELD_MAX) seconds of
+	 * maximum aging and 2 seconds of minimum aging.
+	 *
+	 * An AGE_PERIOD of 0 stops the analyzer from aging at all, which is
+	 * the opposite of what a bridge ageing_time of 0 means. The bridge
+	 * expires every dynamic entry immediately in that case and floods, so
+	 * reject it rather than keep learned addresses forever.
+	 */
+	ds->ageing_time_min = 2U * MSEC_PER_SEC;
+	ds->ageing_time_max = 2U * MSEC_PER_SEC *
+			      FIELD_MAX(ANA_AUTOAGE_AGE_PERIOD);
 
 	dev_info(lan9645x->dev,
 		 "SKU features: max_ports=%d\n",
@@ -325,6 +335,275 @@ static void lan9645x_port_phylink_get_caps(struct dsa_switch *ds, int port,
 	lan9645x_phylink_get_caps(ds->priv, port, config);
 }
 
+static int lan9645x_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
+{
+	struct lan9645x *lan9645x = ds->priv;
+
+	/* Entry must suffer two aging scans before it is removed, so it is
+	 * aged after 2*AGE_PERIOD, and the unit is in seconds.
+	 */
+	lan_rmw(ANA_AUTOAGE_AGE_PERIOD_SET(msecs / (2 * MSEC_PER_SEC)),
+		ANA_AUTOAGE_AGE_PERIOD,
+		lan9645x, ANA_AUTOAGE);
+
+	return 0;
+}
+
+static int lan9645x_port_pre_bridge_flags(struct dsa_switch *ds, int port,
+					  struct switchdev_brport_flags flags,
+					  struct netlink_ext_ack *extack)
+{
+	if (flags.mask &
+	    ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD))
+		return -EINVAL;
+
+	return 0;
+}
+
+static void lan9645x_port_pgid_set(struct lan9645x *lan9645x, u16 pgid,
+				   int chip_port, bool enabled)
+{
+	u32 reg_msk, port_msk;
+
+	port_msk = ANA_PGID_PGID_SET(enabled ? BIT(chip_port) : 0);
+	reg_msk = ANA_PGID_PGID_SET(BIT(chip_port));
+
+	lan_rmw(port_msk, reg_msk, lan9645x, ANA_PGID(pgid));
+}
+
+static void lan9645x_port_apply_learning(struct lan9645x_port *p)
+{
+	struct lan9645x *lan9645x = p->lan9645x;
+	bool ena;
+
+	lockdep_assert_held(&lan9645x->fwd_domain_lock);
+
+	ena = lan9645x_port_is_bridged(p) && p->learn_ena &&
+	      (p->stp_state == BR_STATE_LEARNING ||
+	       p->stp_state == BR_STATE_FORWARDING);
+
+	lan_rmw(ANA_PORT_CFG_LEARN_ENA_SET(ena), ANA_PORT_CFG_LEARN_ENA,
+		lan9645x, ANA_PORT_CFG(p->chip_port));
+}
+
+static void lan9645x_port_set_learning(struct lan9645x *lan9645x, int port,
+				       bool enabled)
+{
+	struct lan9645x_port *p = lan9645x_to_port(lan9645x, port);
+
+	mutex_lock(&lan9645x->fwd_domain_lock);
+	p->learn_ena = enabled;
+	lan9645x_port_apply_learning(p);
+	mutex_unlock(&lan9645x->fwd_domain_lock);
+}
+
+static int lan9645x_port_bridge_flags(struct dsa_switch *ds, int port,
+				      struct switchdev_brport_flags f,
+				      struct netlink_ext_ack *extack)
+{
+	struct lan9645x *lan9645x = ds->priv;
+
+	/* DSA core does not call this for the CPU port */
+	if (f.mask & BR_LEARNING)
+		lan9645x_port_set_learning(lan9645x, port,
+					   !!(f.val & BR_LEARNING));
+
+	if (f.mask & BR_FLOOD)
+		lan9645x_port_pgid_set(lan9645x, PGID_UC, port,
+				       !!(f.val & BR_FLOOD));
+
+	if (f.mask & BR_MCAST_FLOOD) {
+		bool ena = !!(f.val & BR_MCAST_FLOOD);
+
+		lan9645x_port_pgid_set(lan9645x, PGID_MC, port, ena);
+		lan9645x_port_pgid_set(lan9645x, PGID_MCIPV4, port, ena);
+		lan9645x_port_pgid_set(lan9645x, PGID_MCIPV6, port, ena);
+	}
+
+	if (f.mask & BR_BCAST_FLOOD)
+		lan9645x_port_pgid_set(lan9645x, PGID_BC, port,
+				       !!(f.val & BR_BCAST_FLOOD));
+
+	return 0;
+}
+
+static u32 lan9645x_port_bridge_fwd_mask(struct lan9645x *lan9645x,
+					 struct lan9645x_port *p)
+{
+	struct lan9645x_port *q;
+	struct dsa_port *dp;
+	u32 mask = 0;
+
+	dsa_switch_for_each_user_port(dp, lan9645x->ds) {
+		q = lan9645x_to_port(lan9645x, dp->index);
+		if (q != p && q->bridge_num == p->bridge_num)
+			mask |= BIT(dp->index);
+	}
+
+	return mask & lan9645x->bridge_fwd_mask;
+}
+
+static void lan9645x_update_fwd_mask(struct lan9645x *lan9645x)
+{
+	struct lan9645x_port *p;
+	struct dsa_port *dp;
+
+	lockdep_assert_held(&lan9645x->fwd_domain_lock);
+
+	/* Updates the source port PGIDs, making sure frames from p
+	 * are only forwarded to ports q != p, where q is relevant to forward
+	 */
+	dsa_switch_for_each_available_port(dp, lan9645x->ds) {
+		u32 mask = 0;
+
+		p = lan9645x_to_port(lan9645x, dp->index);
+
+		if (lan9645x_port_is_bridged(p) &&
+		    (lan9645x->bridge_fwd_mask & BIT(dp->index)))
+			mask = lan9645x_port_bridge_fwd_mask(lan9645x, p);
+
+		lan_wr(ANA_PGID_PGID_SET(mask), lan9645x,
+		       ANA_PGID(PGID_SRC + dp->index));
+	}
+}
+
+static void lan9645x_port_set_host_flood(struct dsa_switch *ds, int port,
+					 bool uc, bool mc)
+{
+	struct lan9645x *lan9645x = ds->priv;
+	int cpu = lan9645x->num_phys_ports;
+	bool uc_ena, mc_ena;
+
+	mutex_lock(&lan9645x->fwd_domain_lock);
+
+	if (uc)
+		lan9645x->host_flood_uc_mask |= BIT(port);
+	else
+		lan9645x->host_flood_uc_mask &= ~BIT(port);
+
+	if (mc)
+		lan9645x->host_flood_mc_mask |= BIT(port);
+	else
+		lan9645x->host_flood_mc_mask &= ~BIT(port);
+
+	uc_ena = !!lan9645x->host_flood_uc_mask;
+	mc_ena = !!lan9645x->host_flood_mc_mask;
+
+	lan9645x_port_pgid_set(lan9645x, PGID_UC, cpu, uc_ena);
+	lan9645x_port_pgid_set(lan9645x, PGID_MC, cpu, mc_ena);
+	lan9645x_port_pgid_set(lan9645x, PGID_MCIPV4, cpu, mc_ena);
+	lan9645x_port_pgid_set(lan9645x, PGID_MCIPV6, cpu, mc_ena);
+
+	mutex_unlock(&lan9645x->fwd_domain_lock);
+}
+
+static int lan9645x_single_vlan_aware_bridge(struct dsa_switch *ds,
+					     const struct net_device *br,
+					     struct netlink_ext_ack *extack)
+{
+	struct net_device *other;
+	struct dsa_port *dp;
+
+	dsa_switch_for_each_user_port(dp, ds) {
+		other = dsa_port_bridge_dev_get(dp);
+		if (other && other != br && br_vlan_enabled(other)) {
+			NL_SET_ERR_MSG_MOD(extack,
+					   "Only one VLAN-aware bridge is supported");
+			return -EBUSY;
+		}
+	}
+
+	return 0;
+}
+
+static int lan9645x_port_bridge_join(struct dsa_switch *ds, int port,
+				     struct dsa_bridge bridge,
+				     bool *tx_fwd_offload,
+				     struct netlink_ext_ack *extack)
+{
+	struct lan9645x *lan9645x = ds->priv;
+	struct lan9645x_port *p;
+	int err;
+
+	if (!lan9645x_bridge_vlan_proto_ok(bridge.dev)) {
+		NL_SET_ERR_MSG_MOD(extack,
+				   "Only 802.1Q VLAN-aware bridges are supported");
+		return -EINVAL;
+	}
+
+	if (br_vlan_enabled(bridge.dev)) {
+		err = lan9645x_single_vlan_aware_bridge(ds, bridge.dev, extack);
+		if (err)
+			return err;
+	}
+
+	p = lan9645x_to_port(lan9645x, port);
+
+	mutex_lock(&lan9645x->fwd_domain_lock);
+
+	p->bridge_num = bridge.num;
+	lan9645x_vlan_port_set_bridge_vid(p, !p->vlan_aware);
+	/* DSA sets the bridge port flags before the STP state. Start disabled,
+	 * so the inherited BR_LEARNING does not enable learning in between.
+	 */
+	lan9645x->bridge_fwd_mask &= ~BIT(p->chip_port);
+	p->stp_state = BR_STATE_DISABLED;
+	lan9645x_vlan_clear_hostmode(p);
+
+	mutex_unlock(&lan9645x->fwd_domain_lock);
+
+	/* Forwarding is updated by .port_stp_state_set(), which the core
+	 * invokes after this returns:
+	 * dsa_port_bridge_join() -> dsa_port_switchdev_sync_attrs() ->
+	 * dsa_port_set_state()
+	 */
+
+	return 0;
+}
+
+static void lan9645x_port_bridge_stp_state_set(struct dsa_switch *ds, int port,
+					       u8 state)
+{
+	struct lan9645x *lan9645x;
+	struct lan9645x_port *p;
+
+	lan9645x = ds->priv;
+	p = lan9645x_to_port(lan9645x, port);
+
+	mutex_lock(&lan9645x->fwd_domain_lock);
+
+	p->stp_state = state;
+
+	if (state == BR_STATE_FORWARDING)
+		lan9645x->bridge_fwd_mask |= BIT(p->chip_port);
+	else
+		lan9645x->bridge_fwd_mask &= ~BIT(p->chip_port);
+
+	lan9645x_port_apply_learning(p);
+	lan9645x_update_fwd_mask(lan9645x);
+	mutex_unlock(&lan9645x->fwd_domain_lock);
+}
+
+static void lan9645x_port_bridge_leave(struct dsa_switch *ds, int port,
+				       struct dsa_bridge bridge)
+{
+	struct lan9645x *lan9645x = ds->priv;
+	struct lan9645x_port *p;
+
+	p = lan9645x_to_port(lan9645x, port);
+
+	mutex_lock(&lan9645x->fwd_domain_lock);
+
+	lan9645x_vlan_port_set_bridge_vid(p, false);
+	p->bridge_num = 0;
+
+	lan9645x_vlan_set_hostmode(p);
+	lan9645x_port_apply_learning(p);
+	lan9645x_update_fwd_mask(lan9645x);
+
+	mutex_unlock(&lan9645x->fwd_domain_lock);
+}
+
 static int lan9645x_port_vlan_filtering(struct dsa_switch *ds, int port,
 					bool enabled,
 					struct netlink_ext_ack *extack)
@@ -332,6 +611,7 @@ static int lan9645x_port_vlan_filtering(struct dsa_switch *ds, int port,
 	struct net_device *br = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
 	struct lan9645x *lan9645x = ds->priv;
 	struct lan9645x_port *p;
+	int err;
 
 	if (enabled && !lan9645x_bridge_vlan_proto_ok(br)) {
 		NL_SET_ERR_MSG_MOD(extack,
@@ -339,6 +619,12 @@ static int lan9645x_port_vlan_filtering(struct dsa_switch *ds, int port,
 		return -EINVAL;
 	}
 
+	if (enabled && br) {
+		err = lan9645x_single_vlan_aware_bridge(ds, br, extack);
+		if (err)
+			return err;
+	}
+
 	/* DSA core does not call this for the CPU port */
 	p = lan9645x_to_port(lan9645x, port);
 	mutex_lock(&lan9645x->fwd_domain_lock);
@@ -541,6 +827,15 @@ static const struct dsa_switch_ops lan9645x_switch_ops = {
 	.port_change_mtu		= lan9645x_change_mtu,
 	.port_max_mtu			= lan9645x_get_max_mtu,
 
+	/* Bridge integration */
+	.set_ageing_time		= lan9645x_set_ageing_time,
+	.port_pre_bridge_flags		= lan9645x_port_pre_bridge_flags,
+	.port_bridge_flags		= lan9645x_port_bridge_flags,
+	.port_bridge_join		= lan9645x_port_bridge_join,
+	.port_bridge_leave		= lan9645x_port_bridge_leave,
+	.port_stp_state_set		= lan9645x_port_bridge_stp_state_set,
+	.port_set_host_flood		= lan9645x_port_set_host_flood,
+
 	/* VLAN integration */
 	.port_vlan_filtering		= lan9645x_port_vlan_filtering,
 	.port_vlan_add			= lan9645x_port_vlan_add,
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
index caad6871a657..90cde5e2ecda 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h
@@ -192,12 +192,16 @@ struct lan9645x {
 	struct dsa_switch *ds;
 	struct regmap *rmap[NUM_TARGETS];
 
+	u16 host_flood_uc_mask;
+	u16 host_flood_mc_mask;
+
 	/* NPI chip_port */
 	int npi;
 
 	u8 num_phys_ports;
 	struct lan9645x_port **ports;
 
+	u16 bridge_fwd_mask; /* Mask for ports in STP forwarding state */
 	/* lock forwarding configuration and vlan table */
 	struct mutex fwd_domain_lock;
 	struct mutex mact_lock; /* serialize mac table register access */
@@ -220,6 +224,8 @@ struct lan9645x_port {
 	struct lan9645x *lan9645x;
 
 	u8 chip_port;
+	u8 stp_state;
+	bool learn_ena;
 
 	unsigned int bridge_num;
 	bool vlan_aware;
@@ -388,6 +394,7 @@ int lan9645x_vlan_port_add_vlan(struct lan9645x_port *p, u16 vid, bool pvid,
 				struct netlink_ext_ack *extack);
 int lan9645x_vlan_port_del_vlan(struct lan9645x_port *p, u16 vid);
 void lan9645x_vlan_set_hostmode(struct lan9645x_port *p);
+void lan9645x_vlan_clear_hostmode(struct lan9645x_port *p);
 
 /* MAC table: lan9645x_mac.c */
 int lan9645x_mact_flush(struct lan9645x *lan9645x, int port);
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c
index 3ad9afc27b45..0aa711b68825 100644
--- a/drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c
@@ -360,6 +360,15 @@ void lan9645x_vlan_port_set_bridge_vid(struct lan9645x_port *p, bool member)
 	lan9645x_vlan_hw_wr(lan9645x, vid);
 }
 
+void lan9645x_vlan_clear_hostmode(struct lan9645x_port *p)
+{
+	lockdep_assert_held(&p->lan9645x->fwd_domain_lock);
+
+	p->lan9645x->vlans[HOST_PVID].portmask &= ~BIT(p->chip_port);
+	lan9645x_vlan_hw_wr(p->lan9645x, HOST_PVID);
+	lan9645x_vlan_port_apply(p);
+}
+
 int lan9645x_vlan_init(struct lan9645x *lan9645x)
 {
 	u32 all_phys_ports, all_ports;

-- 
2.52.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH net-next 3/5] net: dsa: lan9645x: add mac table integration
  2026-11-24  8:57 ` [PATCH net-next 3/5] net: dsa: lan9645x: add mac table integration Jens Emil Schulz Østergaard
@ 2026-11-25  8:58   ` netdev-bot+sashiko
  0 siblings, 0 replies; 10+ messages in thread
From: netdev-bot+sashiko @ 2026-11-25  8:58 UTC (permalink / raw)
  To: jensemil.schulzostergaard
  Cc: UNGLinuxDriver, andrew, olteanv, davem, edumazet, kuba, pabeni,
	linux, linux-kernel, netdev

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] __lan9645x_mact_learn() in
  drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c decides whether a…

--

net: dsa: lan9645x: add mac table integration

This commit adds MAC table support to the lan9645x DSA driver and wires
up port_fast_age, port_fdb_dump, port_fdb_add and port_fdb_del. It also
enables FDB isolation and assisted learning on the CPU port. A CPU learn
failure flagged in ANA_ANEVENTS.CPU_LEARN_FAILED is reported as -ENOSPC.

> diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c
> new file mode 100644
> index 0000000000000..9da03d0369f5d
> --- /dev/null
> +++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c

[ ... ]

> +static int __lan9645x_mact_learn(struct lan9645x *lan9645x, int port,
> +				 const unsigned char mac[ETH_ALEN],
> +				 unsigned int vid,
> +				 enum macaccess_entry_type type,
> +				 bool cpu_copy)
> +{
> +	int err;
> +
> +	lockdep_assert_held(&lan9645x->mact_lock);
> +
> +	lan9645x_mac_select(lan9645x, mac, vid);
> +
> +	lan_wr(ANA_MACACCESS_VALID_SET(1) |
> +	       ANA_MACACCESS_DEST_IDX_SET(port) |
> +	       ANA_MACACCESS_MAC_CPU_COPY_SET(cpu_copy) |
> +	       ANA_MACACCESS_ENTRYTYPE_SET(type) |
> +	       ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_LEARN),
> +	       lan9645x, ANA_MACACCESS);
> +
> +	err = lan9645x_mac_wait_for_completion(lan9645x, NULL);
> +	if (err)
> +		return err;
> +
> +	if (ANA_ANEVENTS_CPU_LEARN_FAILED_GET(lan_rd(lan9645x, ANA_ANEVENTS))) {
> +		lan_wr(ANA_ANEVENTS_CPU_LEARN_FAILED, lan9645x, ANA_ANEVENTS);
> +		return -ENOSPC;
> +	}

[Severity: Low]
Could a stale CPU_LEARN_FAILED bit cause a spurious -ENOSPC here?

CPU_LEARN_FAILED is a sticky write-one-to-clear bit. The only
unconditional clear is in lan9645x_mac_init(), after CMD_INIT. The only
other clear is on this failure path. Nothing clears the bit before
CMD_LEARN is written to ANA_MACACCESS.

Two cases can leave the bit set:

  - An earlier learn takes the early return because
    lan9645x_mac_wait_for_completion() timed out or got a read error.
    That skips the ANEVENTS check. If the learn later finishes in
    hardware and fails, the bit stays set.

  - The lan_rd() or lan_wr() of ANA_ANEVENTS fails. Both only
    WARN_ON_ONCE on regmap errors.

The next, unrelated learn under mact_lock would then see the stale bit,
clear it and return -ENOSPC. The hardware will still have installed the
new LOCKED entry.

For host addresses on the CPU port, the chain is:

  lan9645x_fdb_add()
    lan9645x_mact_learn()
      __lan9645x_mact_learn()  -> -ENOSPC

Because of that error, dsa_port_do_fdb_add() does not record the
address. A later delete gets -ENOENT from dsa_mac_addr_find() and never
calls .port_fdb_del. The LOCKED entry that points at PGID_CPU would stay
in the MAC table. It never ages out and it takes one of the four buckets
in its row.

The mdb code added later in the series ("net: dsa: lan9645x: add mdb
management") goes through the same lan9645x_mact_learn() path.

Would it help to write ANA_ANEVENTS_CPU_LEARN_FAILED just before issuing
CMD_LEARN? Then the bit would only reflect the current command.

> +
> +	return 0;
> +}

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124-dsa-lan9645x-add-vlan-and-bridge-offload-v1-0-b930a1dae700%40microchip.com

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH net-next 4/5] net: dsa: lan9645x: add mdb management
  2026-11-24  8:57 ` [PATCH net-next 4/5] net: dsa: lan9645x: add mdb management Jens Emil Schulz Østergaard
@ 2026-11-25  8:58   ` netdev-bot+sashiko
  2026-11-25 13:13     ` Jens Emil Schulz Østergaard
  0 siblings, 1 reply; 10+ messages in thread
From: netdev-bot+sashiko @ 2026-11-25  8:58 UTC (permalink / raw)
  To: jensemil.schulzostergaard
  Cc: UNGLinuxDriver, andrew, olteanv, davem, edumazet, kuba, pabeni,
	linux, linux-kernel, netdev

Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] A user port's membership in a hardware MDB entry is a single bit
  (lan9645x_mdb_entry->ports), keyed only by (MAC, hw_vid), with no per-…
- [Medium] In drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c, the
  hardware and software MDB state can disagree after a failed MAC-table…
- [Medium] lan9645x_port_mdb_add() and lan9645x_port_mdb_del()
  (drivers/net/dsa/microchip/lan9645x/lan9645x_main.c) pass mdb->vid…

--

net: dsa: lan9645x: add mdb management

This commit adds DSA .port_mdb_add/.port_mdb_del support to the lan9645x
switch. IP multicast groups store the port mask inline in the MAC table
entry. Each L2 multicast group takes one of the general purpose PGIDs.
IGMP/MLD frames are redirected to the CPU so the bridge can snoop them.

> diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
> index 904b51a55d937..de432772f6399 100644
> --- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
> +++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c

[ ... ]

> @@ -470,6 +483,50 @@ static int lan9645x_fdb_del(struct dsa_switch *ds, int port,
>  				    ENTRYTYPE_LOCKED);
>  }
>  
> +static int lan9645x_port_mdb_add(struct dsa_switch *ds, int port,
> +				 const struct switchdev_obj_port_mdb *mdb,
> +				 struct dsa_db db)
> +{

[ ... ]

> +	if (port == lan9645x->npi)
> +		port = lan9645x->num_phys_ports;
> +
> +	return lan9645x_mdb_add(lan9645x, port, mdb->addr,
> +				lan9645x_fdb_vid(mdb->vid, &db));

[Severity: Medium]
Should mdb->vid be checked against the reserved VID range before it is
used here?

lan9645x_fdb_vid() returns any non-zero VID unchanged, so the value goes
straight into the MAC table. The driver's VLAN path rejects this range in
lan9645x_vlan_port_add_vlan() ("VLAN range 4032-4095 reserved"). That
range holds the internal VIDs of VLAN-unaware bridges:

lan9645x_vlan_unaware_pvid() {
    ...
    return bridge_num ? VLAN_N_VID - bridge_num : HOST_PVID;
}

so bridge_num 2..9 use VIDs 4094..4087.

The bridge MDB netlink path only rejects vid >= 4095. When per-VLAN
multicast snooping is off, br_mdb_config_init() does not require the VLAN
to exist. Take this command:

    bridge mdb add dev brA port swpA grp 239.1.1.1 permanent vid 4094

It would install (01:00:5e:01:01:01, 4094) with portmask {swpA} inside
the internal VLAN of VLAN-unaware bridge #2. Bridge #2 frames for that
group would then match this entry instead of being flooded. Bridge #2's
PGID_SRC mask excludes swpA, so those frames are dropped. A host MDB entry
gives a CPU-only entry with the same effect on bridge #2's front ports.

DSA user ports can be placed in different network namespaces. Could one
bridge disrupt multicast in another bridge's isolated forwarding domain
this way?

The same applies to lan9645x_port_mdb_del(). The existing FDB path also
lacks this check, but this patch adds a new user-controlled path into the
reserved VIDs. The user-port path is reachable once the later "add bridge
support" patch in the series lands, and this code is unchanged there.

> diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c
> new file mode 100644
> index 0000000000000..6864b89d5a2d4
> --- /dev/null
> +++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c
> @@ -0,0 +1,256 @@

[ ... ]

> +static void lan9645x_mdb_entry_destroy(struct lan9645x *lan9645x,
> +				       struct lan9645x_mdb_entry *mdb_entry)
> +{

[ ... ]

> +	err = lan9645x_mact_forget(lan9645x, mdb_entry->mac, mdb_entry->vid,
> +				   mdb_entry->type);
> +	if (err)
> +		dev_err(lan9645x->dev, "Forgetting %pM vid %u returned %pe\n",
> +			mdb_entry->mac, mdb_entry->vid, ERR_PTR(err));
> +	else if (mdb_entry->type == ENTRYTYPE_LOCKED)
> +		clear_bit(mdb_entry->pgid, lan9645x->mdb_pgids);
> +
> +	list_del(&mdb_entry->list);
> +	kfree(mdb_entry);
> +}

[Severity: Medium]
What happens to the hardware entry when lan9645x_mact_forget() fails
here?

lan9645x_mdb_del() has already cleared the port from mdb_entry->ports.
When the mask reaches zero, it calls lan9645x_mdb_entry_destroy() without
first writing the narrowed mask to hardware:

lan9645x_mdb_del() {
    ...
    mdb_entry->ports &= ~BIT(chip_port);

    if (!mdb_entry->ports) {
        lan9645x_mdb_entry_destroy(lan9645x, mdb_entry);
        goto out;
    }
    ...
}

If the forget then times out, or the regmap read in
lan9645x_rd_poll_timeout() fails, the software entry is still freed but
the MAC table entry stays valid:

- For an L2 group, its ANA_PGID mask still contains the departing port.
- For an IPv4/IPv6 group, the encoded portmask and MAC_CPU_COPY still
  include it.

Wouldn't traffic keep being forwarded to a port the bridge has already
removed?

The PGID bit also stays set in mdb_pgids, but the only object that knew
about it has been freed. Can that PGID ever be reclaimed? A re-add of the
same (mac, vid) misses the lookup and allocates a new PGID. Each such
failure then seems to lose one of the general purpose PGIDs until the next
setup.

For L2 groups, would it help to write a zero ANA_PGID mask before the
forget? That is a plain register write and does not depend on the MAC
table command, so any leftover entry would forward nowhere.

Two related cases.

In lan9645x_mdb_del(), when other ports remain, ports is updated before
the CMD_LEARN rewrite for IP groups. If lan9645x_mdb_write_ports() fails,
software no longer lists the port but hardware still does.

In lan9645x_mdb_entry_create(), ANA_PGID(pgid) is programmed before
CMD_LEARN:

    err = lan9645x_mdb_write_ports(lan9645x, mdb_entry, mdb_entry->ports);
    if (!err && mdb_entry->type == ENTRYTYPE_LOCKED)
        err = lan9645x_mact_learn(lan9645x, mdb_entry->pgid, addr, vid,
                                  mdb_entry->type, false);

An -ETIMEDOUT from the learn only means the command did not reach IDLE in
time. It does not mean the learn was dropped. The PGID is left unreserved
while its mask is live. Could a later reuse of that PGID redirect an
orphaned entry?

The commit message says:

    A failed MAC table command is logged instead.

Does that fully describe the stale forwarding and the lost PGID above?
This code is unchanged through the end of the series.

> +int lan9645x_mdb_add(struct lan9645x *lan9645x, int chip_port,
> +		     const unsigned char addr[ETH_ALEN], u16 vid)
> +{

[ ... ]

> +	mdb_entry = lan9645x_mdb_entry_lookup(lan9645x, addr, vid);
> +	if (!mdb_entry) {
> +		err = lan9645x_mdb_entry_create(lan9645x, chip_port, addr, vid);
> +		goto out;
> +	}
> +
> +	if (mdb_entry->ports & BIT(chip_port))
> +		goto out;

[Severity: High]
Can two IP groups that map to the same multicast MAC on one port break
each other here?

br_switchdev_mdb_populate() converts IP groups to MAC addresses with
ip_eth_mc_map() and ipv6_eth_mc_map(). For example, 224.1.1.1, 225.1.1.1
and 239.129.1.1 all become 01:00:5e:01:01:01. For IPv6, groups with the
same low 32 bits share a 33:33 MAC.

DSA does not refcount MDB entries on user ports:

net/dsa/switch.c:dsa_port_do_mdb_del() {
    ...
    /* No need to bother with refcounting for user ports */
    if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp))) {
        err = ds->ops->port_mdb_del(ds, port, mdb, db);
    ...
}

Suppose port P joins group A and then group B with the same MAC. The
second lan9645x_mdb_add() returns 0 here without recording anything. When
A is deleted, lan9645x_mdb_del() clears P from the shared entry and
rewrites or destroys it, even though B is still joined on P in the bridge.

Forwarded frames carry offload_fwd_mark, so the software bridge does not
make up for this, and P stops receiving B in hardware.

With IGMP/MLD snooping, joins and leaves come from remote hosts. A host on
P's segment could join and then leave 224.1.1.1 and stop delivery of
239.1.1.1 to a listener on P. The listener's later reports only refresh
the existing bridge port group. They do not send a new switchdev add.

Does the (mac, vid) entry need a per-port reference count to handle this?

The user-port path becomes reachable once the later "net: dsa: lan9645x:
add bridge support" patch adds .port_bridge_join, and this code is
unchanged there.

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124-dsa-lan9645x-add-vlan-and-bridge-offload-v1-0-b930a1dae700%40microchip.com

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH net-next 5/5] net: dsa: lan9645x: add bridge support
  2026-11-24  8:57 ` [PATCH net-next 5/5] net: dsa: lan9645x: add bridge support Jens Emil Schulz Østergaard
@ 2026-11-25  8:58   ` netdev-bot+sashiko
  0 siblings, 0 replies; 10+ messages in thread
From: netdev-bot+sashiko @ 2026-11-25  8:58 UTC (permalink / raw)
  To: jensemil.schulzostergaard
  Cc: UNGLinuxDriver, andrew, olteanv, davem, edumazet, kuba, pabeni,
	linux, linux-kernel, netdev

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] lan9645x_port_bridge_join()
  (drivers/net/dsa/microchip/lan9645x/lan9645x_main.c:544) stores…
- [Medium] lan9645x_fdb_vid()
  (drivers/net/dsa/microchip/lan9645x/lan9645x_main.c:692) only switches…

--

net: dsa: lan9645x: add bridge support

This adds hardware bridge offload to the lan9645x DSA driver. Each
VLAN-unaware bridge gets its own reserved VID, and at most one bridge can
be VLAN-aware. The CPU port is no longer in the default unicast and
multicast flood masks; .port_set_host_flood adds it instead. The patch
also adds ageing time configuration with min/max limits.

> diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
> index de432772f6399..a78410efb9730 100644
> --- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
> +++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c

[ ... ]

> @@ -306,6 +302,20 @@ static int lan9645x_setup(struct dsa_switch *ds)
>  	ds->mtu_enforcement_ingress = true;
>  	ds->assisted_learning_on_cpu_port = true;
>  	ds->fdb_isolation = true;
> +	ds->max_num_bridges = ds->num_ports;

[ ... ]

> +static int lan9645x_port_bridge_join(struct dsa_switch *ds, int port,
> +				     struct dsa_bridge bridge,
> +				     bool *tx_fwd_offload,
> +				     struct netlink_ext_ack *extack)
> +{

[ ... ]

> +	p = lan9645x_to_port(lan9645x, port);
> +
> +	mutex_lock(&lan9645x->fwd_domain_lock);
> +
> +	p->bridge_num = bridge.num;
> +	lan9645x_vlan_port_set_bridge_vid(p, !p->vlan_aware);

[Severity: Medium]
Can bridge.num be larger than num_phys_ports here?

lan9645x_vlan_init() only adds the CPU port module to the reserved VIDs
for bridge numbers 1..num_phys_ports:

    for (bridge_num = 1; bridge_num <= lan9645x->num_phys_ports;
         bridge_num++) {
        vid = lan9645x_vlan_unaware_pvid(bridge_num);
        lan9645x->vlans[vid].portmask = BIT(lan9645x->num_phys_ports);
        ...
    }

Setting ds->max_num_bridges = ds->num_ports does not appear to limit the
number the DSA core passes in. In net/dsa/dsa.c, dsa_bridge_num_get()
first calls dsa_bridge_num_find(). That function searches every tree in
dsa_tree_list and returns an existing number without comparing it to max.
The "bridge_num > max" check only runs when a new number is allocated.

Take a bridge that first gets a number N >= 10 from a switch in another
DSA tree with a larger max_num_bridges (mv88e6xxx, sja1105 or vsc73xx).
If a lan9645x port then joins the same bridge:

lan9645x_port_bridge_join()
  p->bridge_num = N
  lan9645x_vlan_port_set_bridge_vid(p, true)
    vlans[VLAN_N_VID - N].portmask |= BIT(port)
    lan9645x_vlan_hw_wr()
      cpu_dis = !(v->portmask & BIT(lan9645x->num_phys_ports));  -> 1

ANA_PGID_CFG_OBEY_VLAN is set for the destination PGIDs. Would that mean
no frame classified to this VID is ever copied to the CPU? That would
cover broadcast, host FDB/MDB entries pointing at PGID_CPU, and host
flooding. The host stack and software forwarding to the other tree's
ports would then stop working for that bridge.

Should the join reject bridge.num > num_phys_ports, or should the
reserved VID be set up when it is first used?

[Severity: Medium]
Several bridges can now be offloaded at once. Does lan9645x_fdb_vid()
still keep the FDB of a VLAN-unaware bridge separate from the FDB of the
VLAN-aware bridge?

It only maps vid == 0 to the bridge's reserved VID:

drivers/net/dsa/microchip/lan9645x/lan9645x_main.c:lan9645x_fdb_vid() {
	if (vid)
		return vid;

	return lan9645x_vlan_unaware_pvid(db->type == DSA_DB_BRIDGE ?
					  db->bridge.num : 0);
}

Take "bridge fdb add M dev swpB master static" on a port of
VLAN-unaware bridge B. br_fdb_add() adds M@0, and also M@vid for every
VLAN in the port's VLAN group. That includes the default pvid 1, which
exists even with vlan_filtering=0:

    list_for_each_entry(v, &vg->vlan_list, vlist) {
        if (!br_vlan_should_use(v))
            continue;
        err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v,
                           nfea_tb, notified, extack);

DSA passes vid 1 through unchanged, so lan9645x_fdb_add() calls
lan9645x_mact_learn(lan9645x, port, M, 1, ENTRYTYPE_LOCKED).

B never uses that entry, because its ports classify to the reserved VID.
Now suppose VLAN-aware bridge A uses VID 1. Would frames to M in A hit
this locked entry pointing at swpB and be dropped by A's PGID_SRC mask?
The locked entry would also stop M from being learned on A's real port.

A later fdb_del from B would remove the same hardware entry. That could
also delete A's own static M@VID1 entry. lan9645x_host_addr_shared()
only handles collisions between CPU host addresses, not entries for front
ports.

Before this patch there was no .port_bridge_join, so bridge-DB FDB
entries never reached this helper. The commit message says the reserved
VIDs keep the FDB domains apart, but these entries would still overlap.

Should non-zero VIDs from a VLAN-unaware bridge's DB also be mapped to
its reserved VID, or skipped?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124-dsa-lan9645x-add-vlan-and-bridge-offload-v1-0-b930a1dae700%40microchip.com

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH net-next 4/5] net: dsa: lan9645x: add mdb management
  2026-11-25  8:58   ` netdev-bot+sashiko
@ 2026-11-25 13:13     ` Jens Emil Schulz Østergaard
  0 siblings, 0 replies; 10+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-25 13:13 UTC (permalink / raw)
  To: netdev-bot+sashiko, jensemil.schulzostergaard
  Cc: UNGLinuxDriver, andrew, olteanv, davem, edumazet, kuba, pabeni,
	linux, linux-kernel, netdev

On Wed Nov 25, 2026 at 9:58 AM CET, netdev-bot+sashiko wrote:
> Thank you for your contribution! Sashiko AI review found 3 potential
> issue(s) to consider.
>
> Critical: 0 · High: 1 · Medium: 2 · Low: 0
>
> If you believe none of the issues are legitimate, please reply to each
> issue of Medium severity or higher and explain why the AI is wrong.
> If any of them are legitimate and you plan to respin, please let patchwork
> know by including "pw-bot: cr" as a separate line at the end of your reply
> (one such reply per series is enough).
>
> - [High] A user port's membership in a hardware MDB entry is a single bit
>   (lan9645x_mdb_entry->ports), keyed only by (MAC, hw_vid), with no per-…
> - [Medium] In drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c, the
>   hardware and software MDB state can disagree after a failed MAC-table…
> - [Medium] lan9645x_port_mdb_add() and lan9645x_port_mdb_del()
>   (drivers/net/dsa/microchip/lan9645x/lan9645x_main.c) pass mdb->vid…
>
> --
>
> net: dsa: lan9645x: add mdb management
>
> This commit adds DSA .port_mdb_add/.port_mdb_del support to the lan9645x
> switch. IP multicast groups store the port mask inline in the MAC table
> entry. Each L2 multicast group takes one of the general purpose PGIDs.
> IGMP/MLD frames are redirected to the CPU so the bridge can snoop them.
>
>> diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
>> index 904b51a55d937..de432772f6399 100644
>> --- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
>> +++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
>
> [ ... ]
>
>> @@ -470,6 +483,50 @@ static int lan9645x_fdb_del(struct dsa_switch *ds, int port,
>>  				    ENTRYTYPE_LOCKED);
>>  }
>>  
>> +static int lan9645x_port_mdb_add(struct dsa_switch *ds, int port,
>> +				 const struct switchdev_obj_port_mdb *mdb,
>> +				 struct dsa_db db)
>> +{
>
> [ ... ]
>
>> +	if (port == lan9645x->npi)
>> +		port = lan9645x->num_phys_ports;
>> +
>> +	return lan9645x_mdb_add(lan9645x, port, mdb->addr,
>> +				lan9645x_fdb_vid(mdb->vid, &db));
>
> [Severity: Medium]
> Should mdb->vid be checked against the reserved VID range before it is
> used here?
>

Yes, correct. Without per-VLAN snooping the bridge does not require the
VLAN for an MDB entry, so a VID in 4032-4095 reaches the driver. v2
refuses it in .port_mdb_add. FDB entries are not affected: br_fdb_add()
requires the VLAN on the port, and .port_vlan_add refuses the reserved
range on user ports and on the CPU port.

>> diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c
>> new file mode 100644
>> index 0000000000000..6864b89d5a2d4
>> --- /dev/null
>> +++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_mdb.c
>> @@ -0,0 +1,256 @@
>
> [ ... ]
>
>> +static void lan9645x_mdb_entry_destroy(struct lan9645x *lan9645x,
>> +				       struct lan9645x_mdb_entry *mdb_entry)
>> +{
>
> [ ... ]
>
>> +	err = lan9645x_mact_forget(lan9645x, mdb_entry->mac, mdb_entry->vid,
>> +				   mdb_entry->type);
>> +	if (err)
>> +		dev_err(lan9645x->dev, "Forgetting %pM vid %u returned %pe\n",
>> +			mdb_entry->mac, mdb_entry->vid, ERR_PTR(err));
>> +	else if (mdb_entry->type == ENTRYTYPE_LOCKED)
>> +		clear_bit(mdb_entry->pgid, lan9645x->mdb_pgids);
>> +
>> +	list_del(&mdb_entry->list);
>> +	kfree(mdb_entry);
>> +}
>
> [Severity: Medium]
> What happens to the hardware entry when lan9645x_mact_forget() fails
> here?
>
> lan9645x_mdb_del() has already cleared the port from mdb_entry->ports.
> When the mask reaches zero, it calls lan9645x_mdb_entry_destroy() without
> first writing the narrowed mask to hardware:
>
> lan9645x_mdb_del() {
>     ...
>     mdb_entry->ports &= ~BIT(chip_port);
>
>     if (!mdb_entry->ports) {
>         lan9645x_mdb_entry_destroy(lan9645x, mdb_entry);
>         goto out;
>     }
>     ...
> }
>
> If the forget then times out, or the regmap read in
> lan9645x_rd_poll_timeout() fails, the software entry is still freed but
> the MAC table entry stays valid:
>
> - For an L2 group, its ANA_PGID mask still contains the departing port.
> - For an IPv4/IPv6 group, the encoded portmask and MAC_CPU_COPY still
>   include it.
>
> Wouldn't traffic keep being forwarded to a port the bridge has already
> removed?
>
> The PGID bit also stays set in mdb_pgids, but the only object that knew
> about it has been freed. Can that PGID ever be reclaimed? A re-add of the
> same (mac, vid) misses the lookup and allocates a new PGID. Each such
> failure then seems to lose one of the general purpose PGIDs until the next
> setup.
>
> For L2 groups, would it help to write a zero ANA_PGID mask before the
> forget? That is a plain register write and does not depend on the MAC
> table command, so any leftover entry would forward nowhere.
>
> Two related cases.
>
> In lan9645x_mdb_del(), when other ports remain, ports is updated before
> the CMD_LEARN rewrite for IP groups. If lan9645x_mdb_write_ports() fails,
> software no longer lists the port but hardware still does.
>
> In lan9645x_mdb_entry_create(), ANA_PGID(pgid) is programmed before
> CMD_LEARN:
>
>     err = lan9645x_mdb_write_ports(lan9645x, mdb_entry, mdb_entry->ports);
>     if (!err && mdb_entry->type == ENTRYTYPE_LOCKED)
>         err = lan9645x_mact_learn(lan9645x, mdb_entry->pgid, addr, vid,
>                                   mdb_entry->type, false);
>
> An -ETIMEDOUT from the learn only means the command did not reach IDLE in
> time. It does not mean the learn was dropped. The PGID is left unreserved
> while its mask is live. Could a later reuse of that PGID redirect an
> orphaned entry?
>
> The commit message says:
>
>     A failed MAC table command is logged instead.
>
> Does that fully describe the stale forwarding and the lost PGID above?
> This code is unchanged through the end of the series.
>

This is intended. The MAC table finishes a command in a bounded number of
cycles, far below the poll timeout, so the MAC table command only fails when the
register bus is broken.

We do not try to stay defensive against a broken bus. Doing so would require us
to keep track of failed hw ops and retry them either periodically or on
subsequent add/del callbacks. I do not see other drivers doing something like
this.

The delete path has no useful way to report an error here:
switchdev_port_obj_del_deferred() only logs the error, br_switchdev_mdb_replay()
stops at the first failed entry, and nothing retries a failed delete. Therefore,
we just log the error.

The PGID stays reserved on purpose, because the stale mac entry may still point
at it, and reusing it would redirect that entry to another group. A later add of
the same group relearns the mac entry. Writing an empty mask before the forget
would blackhole the group, which has never been requested, so it is better to
continue forwarding as previously requested, until the group gets another add.

The two related cases are the same. When the narrowed mask of an IP group
cannot be written, the port is still dropped from the software state, since the
bridge does not retry the delete and the group could otherwise never reach zero
ports.

The next add or delete of the group writes the full mask again. In
lan9645x_mdb_entry_create() the only learn failure with a working bus is -ENOSPC
from ANA_ANEVENTS.CPU_LEARN_FAILED, which means the hardware dropped the learn,
so no entry points at the PGID and it can be reused.

>> +int lan9645x_mdb_add(struct lan9645x *lan9645x, int chip_port,
>> +		     const unsigned char addr[ETH_ALEN], u16 vid)
>> +{
>
> [ ... ]
>
>> +	mdb_entry = lan9645x_mdb_entry_lookup(lan9645x, addr, vid);
>> +	if (!mdb_entry) {
>> +		err = lan9645x_mdb_entry_create(lan9645x, chip_port, addr, vid);
>> +		goto out;
>> +	}
>> +
>> +	if (mdb_entry->ports & BIT(chip_port))
>> +		goto out;
>
> [Severity: High]
> Can two IP groups that map to the same multicast MAC on one port break
> each other here?
>
> br_switchdev_mdb_populate() converts IP groups to MAC addresses with
> ip_eth_mc_map() and ipv6_eth_mc_map(). For example, 224.1.1.1, 225.1.1.1
> and 239.129.1.1 all become 01:00:5e:01:01:01. For IPv6, groups with the
> same low 32 bits share a 33:33 MAC.
>
> DSA does not refcount MDB entries on user ports:
>
> net/dsa/switch.c:dsa_port_do_mdb_del() {
>     ...
>     /* No need to bother with refcounting for user ports */
>     if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp))) {
>         err = ds->ops->port_mdb_del(ds, port, mdb, db);
>     ...
> }
>
> Suppose port P joins group A and then group B with the same MAC. The
> second lan9645x_mdb_add() returns 0 here without recording anything. When
> A is deleted, lan9645x_mdb_del() clears P from the shared entry and
> rewrites or destroys it, even though B is still joined on P in the bridge.
>
> Forwarded frames carry offload_fwd_mark, so the software bridge does not
> make up for this, and P stops receiving B in hardware.
>
> With IGMP/MLD snooping, joins and leaves come from remote hosts. A host on
> P's segment could join and then leave 224.1.1.1 and stop delivery of
> 239.1.1.1 to a listener on P. The listener's later reports only refresh
> the existing bridge port group. They do not send a new switchdev add.
>
> Does the (mac, vid) entry need a per-port reference count to handle this?
>
> The user-port path becomes reachable once the later "net: dsa: lan9645x:
> add bridge support" patch adds .port_bridge_join, and this code is
> unchanged there.
>
> [ ... ]

This aliasing problem is right, but I believe this is preexisting, and that the
fix needs to be in the bridge core.

A reference count in the driver cannot be balanced. The switchdev MDB
object only carries the group MAC and VID, and the bridge also sends an
add for a port group that already exists: on bridge mdb replace
(br_mdb_replace_group_sg() and br_mdb_replace_group_star_g()), when an
IGMPv3/MLDv2 report changes the source list or filter mode, and when the
group timer moves a group to INCLUDE mode. The delete is sent once per
port group. These two sequences

  bridge mdb add dev br0 port swp0 grp 239.1.1.1 permanent
  bridge mdb add dev br0 port swp0 grp 224.129.1.1 permanent
  bridge mdb del dev br0 port swp0 grp 224.129.1.1 permanent

  bridge mdb add dev br0 port swp0 grp 239.1.1.1 permanent
  bridge mdb replace dev br0 port swp0 grp 239.1.1.1 permanent
  bridge mdb del dev br0 port swp0 grp 239.1.1.1 permanent

both reach the driver as add, add, del of 01:00:5e:01:01:01. After the first,
swp0 must stay in the entry for 239.1.1.1. After the second, the bridge no
longer has the group and swp0 must leave it. The driver cannot tell them apart,
since the IP group is not in the object. DSA does not count user port MDB
entries either, and ocelot, lan966x and mv88e6xxx behave the same way.

Fixing the aliasing needs a bridge change: either notify switchdev only when a
port group is created, so that adds and deletes balance and a per-port count
works, or pass the group address in the object, so that drivers can track port
groups themselves.

pw-bot: cr

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-11-25 13:13 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  8:57 [PATCH net-next 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
2026-11-24  8:57 ` [PATCH net-next 1/5] net: dsa: lan9645x: add the analyzer event register Jens Emil Schulz Østergaard
2026-11-24  8:57 ` [PATCH net-next 2/5] net: dsa: lan9645x: add vlan support Jens Emil Schulz Østergaard
2026-11-24  8:57 ` [PATCH net-next 3/5] net: dsa: lan9645x: add mac table integration Jens Emil Schulz Østergaard
2026-11-25  8:58   ` netdev-bot+sashiko
2026-11-24  8:57 ` [PATCH net-next 4/5] net: dsa: lan9645x: add mdb management Jens Emil Schulz Østergaard
2026-11-25  8:58   ` netdev-bot+sashiko
2026-11-25 13:13     ` Jens Emil Schulz Østergaard
2026-11-24  8:57 ` [PATCH net-next 5/5] net: dsa: lan9645x: add bridge support Jens Emil Schulz Østergaard
2026-11-25  8:58   ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox