* [PATCH net-next v2 0/5] net: dsa: lan9645x: add VLAN and bridge offload
@ 2026-11-27 9:24 Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 1/5] net: dsa: lan9645x: add the analyzer event register Jens Emil Schulz Østergaard
` (4 more replies)
0 siblings, 5 replies; 8+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-27 9:24 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. The vlan_filtering setting of a bridge
cannot be changed while switch ports are offloaded.
Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>
---
Changes in v2:
- patches 2 and 5: Move the 802.1ad check from .port_vlan_filtering to
.port_bridge_join.
- patch 3: Clear ANA_ANEVENTS.CPU_LEARN_FAILED before each CPU learn, so
the result only reflects that learn.
- patch 4: Do not offload MDB entries in the reserved VIDs 4032-4095.
- patch 5: Refuse a bridge number above max_num_bridges in
.port_bridge_join, DSA can reuse a number allocated by another tree.
- patch 5: Refuse vlan_filtering changes on a bridge with switch ports,
and do not install FDB and MDB entries with a VID of a VLAN-unaware
bridge, which broke FDB isolation.
- Link to v1: https://lore.kernel.org/r/20261124-dsa-lan9645x-add-vlan-and-bridge-offload-v1-0-b930a1dae700@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 | 264 +++++++++
drivers/net/dsa/microchip/lan9645x/lan9645x_main.c | 635 ++++++++++++++++++++-
drivers/net/dsa/microchip/lan9645x/lan9645x_main.h | 106 ++++
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, 1892 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] 8+ messages in thread
* [PATCH net-next v2 1/5] net: dsa: lan9645x: add the analyzer event register
2026-11-27 9:24 [PATCH net-next v2 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
@ 2026-11-27 9:24 ` Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 2/5] net: dsa: lan9645x: add vlan support Jens Emil Schulz Østergaard
` (3 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-27 9:24 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 [flat|nested] 8+ messages in thread
* [PATCH net-next v2 2/5] net: dsa: lan9645x: add vlan support
2026-11-27 9:24 [PATCH net-next v2 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 1/5] net: dsa: lan9645x: add the analyzer event register Jens Emil Schulz Østergaard
@ 2026-11-27 9:24 ` Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 3/5] net: dsa: lan9645x: add mac table integration Jens Emil Schulz Østergaard
` (2 subsequent siblings)
4 siblings, 0 replies; 8+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-27 9:24 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.
Signed-off-by: Jens Emil Schulz Østergaard <jensemil.schulzostergaard@microchip.com>
---
Changes in v2:
- Move the 802.1ad check to .port_bridge_join in the bridge patch.
---
drivers/net/dsa/microchip/lan9645x/Makefile | 1 +
drivers/net/dsa/microchip/lan9645x/lan9645x_main.c | 71 ++++
drivers/net/dsa/microchip/lan9645x/lan9645x_main.h | 37 ++
drivers/net/dsa/microchip/lan9645x/lan9645x_port.c | 6 +
drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c | 420 +++++++++++++++++++++
5 files changed, 535 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..b48359095574 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,61 @@ 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 lan9645x *lan9645x = ds->priv;
+ struct lan9645x_port *p;
+
+ /* 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 +374,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..613cfcbccd44 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,11 @@ 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 struct regmap *lan_tgt2rmap(struct lan9645x *lan9645x,
enum lan9645x_target t, int tinst)
{
@@ -303,4 +329,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 [flat|nested] 8+ messages in thread
* [PATCH net-next v2 3/5] net: dsa: lan9645x: add mac table integration
2026-11-27 9:24 [PATCH net-next v2 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 1/5] net: dsa: lan9645x: add the analyzer event register Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 2/5] net: dsa: lan9645x: add vlan support Jens Emil Schulz Østergaard
@ 2026-11-27 9:24 ` Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 4/5] net: dsa: lan9645x: add mdb management Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 5/5] net: dsa: lan9645x: add bridge support Jens Emil Schulz Østergaard
4 siblings, 0 replies; 8+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-27 9:24 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>
---
Changes in v2:
- Clear ANA_ANEVENTS.CPU_LEARN_FAILED before each CPU learn instead of
after a failed one and in lan9645x_mac_init().
---
drivers/net/dsa/microchip/lan9645x/Makefile | 1 +
drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c | 264 +++++++++++++++++++++
drivers/net/dsa/microchip/lan9645x/lan9645x_main.c | 118 +++++++++
drivers/net/dsa/microchip/lan9645x/lan9645x_main.h | 42 ++++
4 files changed, 425 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..21676cc84bc9
--- /dev/null
+++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c
@@ -0,0 +1,264 @@
+// 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_ANEVENTS_CPU_LEARN_FAILED, lan9645x, ANA_ANEVENTS);
+
+ 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)))
+ 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;
+ }
+
+ 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 b48359095574..c425153b4701 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",
@@ -361,6 +367,112 @@ 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;
+}
+
+/* All host addresses point at the CPU, so host addresses of different
+ * databases with the same address and hardware VID are one MAC table
+ * entry. DSA only counts references within one database, so keep the
+ * entry while another database still uses it. dsa_fdb_present_in_other_db()
+ * compares the VID given by the bridge, not the hardware VID. VID 0 is
+ * installed in a different reserved VID for each VLAN-unaware bridge, so
+ * it would merge their host addresses. It also only compares databases of
+ * the same type, which misses a VLAN upper of a port and a bridge in the
+ * same VLAN.
+ */
+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,
@@ -379,6 +491,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 613cfcbccd44..eb8752e5da7c 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;
@@ -277,6 +291,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,
@@ -340,4 +369,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 [flat|nested] 8+ messages in thread
* [PATCH net-next v2 4/5] net: dsa: lan9645x: add mdb management
2026-11-27 9:24 [PATCH net-next v2 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
` (2 preceding siblings ...)
2026-11-27 9:24 ` [PATCH net-next v2 3/5] net: dsa: lan9645x: add mac table integration Jens Emil Schulz Østergaard
@ 2026-11-27 9:24 ` Jens Emil Schulz Østergaard
2026-11-28 9:30 ` netdev-bot+sashiko
2026-11-27 9:24 ` [PATCH net-next v2 5/5] net: dsa: lan9645x: add bridge support Jens Emil Schulz Østergaard
4 siblings, 1 reply; 8+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-27 9:24 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>
---
Changes in v2:
- Do not offload MDB entries in the reserved VIDs 4032-4095.
---
drivers/net/dsa/microchip/lan9645x/Makefile | 1 +
drivers/net/dsa/microchip/lan9645x/lan9645x_main.c | 68 ++++++
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, 353 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 c425153b4701..e840de1c918b 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
@@ -473,6 +486,57 @@ 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);
+
+ /* The bridge accepts MDB entries in a VID that is not configured, so
+ * the reserved range can reach here. The FDB case is handled by
+ * lan9645x_vlan_port_add_vlan(), since FDB entries need the VLAN.
+ */
+ if (mdb->vid > VLAN_MAX)
+ return -EOPNOTSUPP;
+
+ 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,
@@ -497,6 +561,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 eb8752e5da7c..c5b85cb79f83 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 */
@@ -382,4 +394,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 [flat|nested] 8+ messages in thread
* [PATCH net-next v2 5/5] net: dsa: lan9645x: add bridge support
2026-11-27 9:24 [PATCH net-next v2 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
` (3 preceding siblings ...)
2026-11-27 9:24 ` [PATCH net-next v2 4/5] net: dsa: lan9645x: add mdb management Jens Emil Schulz Østergaard
@ 2026-11-27 9:24 ` Jens Emil Schulz Østergaard
2026-11-28 9:30 ` netdev-bot+sashiko
4 siblings, 1 reply; 8+ messages in thread
From: Jens Emil Schulz Østergaard @ 2026-11-27 9:24 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.
At most one bridge can be VLAN-aware, since the MAC table is keyed
on (MAC,VID). Only bridge vlan_protocol 802.1Q is offloaded, and joining
a VLAN-aware 802.1ad bridge is refused. 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.
FDB and MDB entries, learned entries and the VLAN membership of a port
all depend on the vlan_filtering setting of its bridge, and the bridge
does not notify them again when the setting changes. So a port takes the
setting of its bridge when it joins, and a change is refused while the
port is in the bridge. The join does not rely on .port_vlan_filtering,
which DSA skips, or tries to undo, around a failed join.
A bridge FDB or MDB entry with a VID is never used by a VLAN-unaware
bridge, whose ports classify frames to its reserved VID, and whose FDB
and MDB lookups use VID 0. The bridge still adds such entries,
1) For every VLAN in the bridge VLAN group of the port, which holds the
default pvid even without vlan_filtering.
2) For its local addresses
3) An MDB entry can be added with any VID.
Installing these in the MAC table would break FDB isolation between a
VLAN-unaware and a VLAN-aware bridge, so they are accepted without being
installed.
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>
---
Changes in v2:
- Refuse a bridge number above max_num_bridges in
lan9645x_port_bridge_join().
- Refuse vlan_filtering changes while switch ports are in the bridge.
- Do not install FDB and MDB entries with a VID of a VLAN-unaware
bridge.
- Move the 802.1ad check from .port_vlan_filtering to
.port_bridge_join.
- Take the VLAN awareness of a port from its bridge in
lan9645x_port_bridge_join().
---
drivers/net/dsa/microchip/lan9645x/lan9645x_main.c | 386 ++++++++++++++++++++-
drivers/net/dsa/microchip/lan9645x/lan9645x_main.h | 7 +
drivers/net/dsa/microchip/lan9645x/lan9645x_vlan.c | 9 +
3 files changed, 387 insertions(+), 15 deletions(-)
diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
index e840de1c918b..94b46082cf5c 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,290 @@ 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 bool lan9645x_bridge_vlan_proto_ok(const struct net_device *br)
+{
+ u16 proto;
+
+ return !br_vlan_enabled(br) || br_vlan_get_proto(br, &proto) ||
+ proto == ETH_P_8021Q;
+}
+
+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 (bridge.num > ds->max_num_bridges) {
+ NL_SET_ERR_MSG_MOD(extack,
+ "Range of offloadable bridges exceeded");
+ return -EOPNOTSUPP;
+ }
+
+ 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;
+ p->vlan_aware = br_vlan_enabled(bridge.dev);
+ 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)
@@ -334,10 +628,18 @@ static int lan9645x_port_vlan_filtering(struct dsa_switch *ds, int port,
/* DSA core does not call this for the CPU port */
p = lan9645x_to_port(lan9645x, port);
+
+ if (lan9645x_port_is_bridged(p)) {
+ if (enabled == p->vlan_aware)
+ return 0;
+
+ NL_SET_ERR_MSG_MOD(extack,
+ "VLAN filtering cannot be changed on a bridge with offloaded ports");
+ return -EBUSY;
+ }
+
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);
@@ -422,6 +724,37 @@ static bool lan9645x_db_equal(const struct dsa_db *a, const struct dsa_db *b)
return false;
}
+/* A bridge FDB or MDB entry with a VID is never used while the bridge is
+ * VLAN-unaware. The bridge still adds such entries, for every VLAN in the
+ * bridge VLAN group of the port and for its local addresses. In the MAC
+ * table they would land in the VLAN of the VLAN-aware bridge and break FDB
+ * isolation. vlan_filtering cannot change while a switch port is in the
+ * bridge, and DSA deletes the entries of a port before it leaves, so a
+ * delete skips exactly the entries that the add skipped.
+ */
+static bool lan9645x_db_vid_unused(struct lan9645x *lan9645x, u16 vid,
+ const struct dsa_db *db)
+{
+ struct lan9645x_port *p;
+ struct dsa_port *dp;
+ bool unused = false;
+
+ if (!vid || db->type != DSA_DB_BRIDGE)
+ return false;
+
+ mutex_lock(&lan9645x->fwd_domain_lock);
+ dsa_switch_for_each_user_port(dp, lan9645x->ds) {
+ p = lan9645x_to_port(lan9645x, dp->index);
+ if (p->bridge_num == db->bridge.num) {
+ unused = !p->vlan_aware;
+ break;
+ }
+ }
+ mutex_unlock(&lan9645x->fwd_domain_lock);
+
+ return unused;
+}
+
/* All host addresses point at the CPU, so host addresses of different
* databases with the same address and hardware VID are one MAC table
* entry. DSA only counts references within one database, so keep the
@@ -430,7 +763,8 @@ static bool lan9645x_db_equal(const struct dsa_db *a, const struct dsa_db *b)
* installed in a different reserved VID for each VLAN-unaware bridge, so
* it would merge their host addresses. It also only compares databases of
* the same type, which misses a VLAN upper of a port and a bridge in the
- * same VLAN.
+ * same VLAN. Entries that are not installed, see lan9645x_db_vid_unused(),
+ * do not count.
*/
static bool lan9645x_host_addr_shared(struct dsa_port *dp,
const struct list_head *addrs,
@@ -445,7 +779,8 @@ static bool lan9645x_host_addr_shared(struct dsa_port *dp,
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)
+ lan9645x_fdb_vid(a->vid, &a->db) == hw_vid &&
+ !lan9645x_db_vid_unused(dp->ds->priv, a->vid, &a->db))
return true;
}
@@ -460,6 +795,9 @@ static int lan9645x_fdb_add(struct dsa_switch *ds, int port,
struct lan9645x *lan9645x = ds->priv;
int dest;
+ if (lan9645x_db_vid_unused(lan9645x, vid, &db))
+ return 0;
+
if (dsa_port_is_cpu(dp) &&
lan9645x_host_addr_shared(dp, &dp->fdbs, addr, vid, &db))
return 0;
@@ -478,6 +816,9 @@ static int lan9645x_fdb_del(struct dsa_switch *ds, int port,
struct dsa_port *dp = dsa_to_port(ds, port);
struct lan9645x *lan9645x = ds->priv;
+ if (lan9645x_db_vid_unused(lan9645x, vid, &db))
+ return 0;
+
if (dsa_port_is_cpu(dp) &&
lan9645x_host_addr_shared(dp, &dp->fdbs, addr, vid, &db))
return 0;
@@ -503,6 +844,9 @@ static int lan9645x_port_mdb_add(struct dsa_switch *ds, int port,
if (mdb->vid > VLAN_MAX)
return -EOPNOTSUPP;
+ if (lan9645x_db_vid_unused(lan9645x, mdb->vid, &db))
+ return 0;
+
if (dsa_port_is_cpu(dp) &&
lan9645x_host_addr_shared(dp, &dp->mdbs, mdb->addr, mdb->vid, &db))
return 0;
@@ -524,6 +868,9 @@ static int lan9645x_port_mdb_del(struct dsa_switch *ds, int port,
dev_dbg(lan9645x->dev, "port=%d addr=%pM vid=%u\n", port, mdb->addr,
mdb->vid);
+ if (lan9645x_db_vid_unused(lan9645x, mdb->vid, &db))
+ return 0;
+
if (dsa_port_is_cpu(dp) &&
lan9645x_host_addr_shared(dp, &dp->mdbs, mdb->addr, mdb->vid, &db))
return 0;
@@ -551,6 +898,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 c5b85cb79f83..4b9b17535f51 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;
@@ -380,6 +386,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 [flat|nested] 8+ messages in thread
* Re: [PATCH net-next v2 4/5] net: dsa: lan9645x: add mdb management
2026-11-27 9:24 ` [PATCH net-next v2 4/5] net: dsa: lan9645x: add mdb management Jens Emil Schulz Østergaard
@ 2026-11-28 9:30 ` netdev-bot+sashiko
0 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 9:30 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 5 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 3
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] Per-port MDB membership is a single bit per (MAC, hw VID),
with no refcount for IP groups that map to the same multicast MAC.
- [Medium] When the general-purpose PGIDs (48 PGIDs, 9..56, shared by all
bridges and the standalone host MDB entries) are used up,…
- [Low] When the last member leaves, lan9645x_mdb_del() calls
lan9645x_mdb_entry_destroy().
- [Low] L2 (non-IP) MDB groups are written to the MAC table as
ENTRYTYPE_LOCKED at key (addr, hw_vid) with DEST_IDX=pgid.
- [Low] For IPv4/IPv6 MDB entries, lan9645x_mdb_write_ports() passes the
full `ports` mask, including BIT(num_phys_ports) (the CPU port module,…
--
net: dsa: lan9645x: add mdb management
This adds the DSA port_mdb_add/port_mdb_del callbacks for lan9645x. IP
multicast groups store the port mask inline in the MAC table entry. Each
L2 multicast group allocates one general purpose PGID. IGMP/MLD frames
are redirected to the CPU, and IPv4 multicast control frames are copied
to it.
> 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
[ ... ]
> +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));
[Severity: Low]
Should the CPU bit be masked out of ports before it is encoded inline
here?
As written, lan9645x_mdb_encode_mac() writes BIT(num_phys_ports) into the
inline destination bytes, and MAC_CPU_COPY is also set from the same bit.
The commit message says IP host membership is carried in MAC_CPU_COPY.
L2 groups reach the CPU through the PGID CPU bit, and that path is gated
per VLAN. ANA_PGID_CFG.OBEY_VLAN is set, and lan9645x_vlan_hw_wr() sets
VLAN_PGID_CPU_DIS when the CPU is not in the VLAN portmask. IP entries
use the dummy DEST_IDX 0, so they don't get that gating.
The lan966x driver keeps the CPU out of the inline mask. It only sets
cpu_copy when the CPU is a member of the VLAN. DSA doesn't add user-port
VLANs to the CPU port, so a host MDB entry can exist in a VID the CPU
isn't a member of. One example is a static "bridge mdb add dev br0 port
br0 ... vid V" where br0 isn't in V.
In that VID, would IP host groups still be copied to the CPU while L2
host groups are not? This is inferred from lan966x. I couldn't confirm
from the LAN9645x documentation what inline bit 9 means, or whether
MAC_CPU_COPY follows VLAN membership.
> +
> + /* 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)
> +{
[ ... ]
> + 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;
> + }
> + }
[Severity: Medium]
Does returning -ENOSPC here make a bridge join fail, instead of just
leaving the group unoffloaded?
The commit message says:
When they are exhausted, adding a new group fails with -ENOSPC and the
group is not offloaded.
lan9645x_port_mdb_add() returns this code unchanged, but
br_switchdev_mdb_replay() only tolerates -EOPNOTSUPP:
net/bridge/br_switchdev.c:br_switchdev_mdb_replay() {
...
if (err == -EOPNOTSUPP)
err = 0;
if (err)
goto out_free_mdb;
...
}
The error then goes through nbp_switchdev_sync_objs() to
br_switchdev_port_offload(). That function fails the offload without
unsyncing the entries that were already replayed.
The 48 general purpose PGIDs are shared by all bridges and by the
standalone host entries. Once they are all in use, would "ip link set
swpN master br0" fail entirely? Would the PGIDs and port bits of the
entries replayed before the failure stay programmed in the driver?
lan9645x_port_mdb_add() already returns -EOPNOTSUPP for the reserved VID
case. Could PGID exhaustion, and the -ENOSPC that lan9645x_mact_learn()
returns on CPU_LEARN_FAILED, be handled the same way?
This becomes reachable once "net: dsa: lan9645x: add bridge support"
adds .port_bridge_join later in the series. That commit doesn't change
this error handling.
> +
> + 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);
[Severity: Low]
Can this collide with a static FDB entry for the same multicast MAC and
VID?
lan9645x_fdb_add() also learns ENTRYTYPE_LOCKED at (addr, hw_vid), with
DEST_IDX set to a port:
drivers/net/dsa/microchip/lan9645x/lan9645x_main.c:lan9645x_fdb_add() {
...
return lan9645x_mact_learn(lan9645x, dest, addr,
lan9645x_fdb_vid(vid, &db),
ENTRYTYPE_LOCKED, false);
}
br_fdb_add() only rejects all-zero MACs, so a command like "bridge fdb
add 01:xx:... dev swpN master static vid V" reaches it.
Whichever of the FDB entry and the L2 MDB entry is learned last
overwrites the other's destination. Deleting either one sends CMD_FORGET
for the shared key, which also removes the other's hardware entry.
For ENTRYTYPE_LOCKED, lan9645x_mdb_write_ports() only rewrites ANA_PGID
and never re-learns the MAC entry. Doesn't that leave the MDB state out
of sync with the hardware until the group is deleted and recreated?
[ ... ]
> +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: Low]
What happens to the hardware entry if lan9645x_mact_forget() fails here?
lan9645x_mdb_del() clears the port from mdb_entry->ports and calls this
function. It does not first write an empty ANA_PGID(pgid) or an empty
inline mask.
The forget can fail with a bus error, or with -ETIMEDOUT from
lan9645x_rd_poll_timeout() in __lan9645x_mact_forget(). In that case the
MAC table entry stays valid and keeps forwarding to the departed port
through the old mask. The tracking entry is freed anyway.
For L2 groups the PGID bit is intentionally left set, so that PGID is
lost permanently. A later re-add allocates a new PGID and CMD_LEARN
repoints the same MAC slot at it. The old PGID is then orphaned, and the
pool has only 48.
Would clearing ANA_PGID(pgid) with a register write before CMD_FORGET
stop the stale forwarding? That would also make it safe to release the
PGID whatever the forget returns.
The partial removal path in lan9645x_mdb_del() has a similar ordering
problem. mdb_entry->ports is updated before lan9645x_mdb_write_ports().
For IP entries that call is a CMD_LEARN, and if it fails the old inline
port set stays in hardware.
> +
> +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;
[Severity: Medium]
Does this need a per-port refcount for IP groups that map to the same
multicast MAC?
The bridge sends one SWITCHDEV_OBJ_ID_PORT_MDB object per IP group.
br_switchdev_mdb_populate() turns each group into a MAC address with
ip_eth_mc_map()/ipv6_eth_mc_map(). For example, 224.1.1.1 and 225.1.1.1
both become 01:00:5e:01:01:01.
DSA doesn't refcount these for 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);
...
}
Take two receivers behind one port that join 224.1.1.1 and 225.1.1.1.
The second join is a no-op here.
When one of them leaves or times out, lan9645x_mdb_del() does:
mdb_entry->ports &= ~BIT(chip_port);
It then either rewrites the mask without the port or destroys the entry.
Would the port then stop receiving the group it is still a member of?
Bridge refreshes of the remaining group don't re-notify switchdev, so
the loss would last until the bridge recreates that port group.
This becomes reachable with .port_bridge_join from "net: dsa: lan9645x:
add bridge support", which doesn't change lan9645x_mdb.c.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127-dsa-lan9645x-add-vlan-and-bridge-offload-v2-0-84adcd919dac%40microchip.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH net-next v2 5/5] net: dsa: lan9645x: add bridge support
2026-11-27 9:24 ` [PATCH net-next v2 5/5] net: dsa: lan9645x: add bridge support Jens Emil Schulz Østergaard
@ 2026-11-28 9:30 ` netdev-bot+sashiko
0 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 9:30 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: 1 · Low: 1
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] The 802.1Q-only restriction for VLAN-aware bridges is checked
only when a port joins, in lan9645x_port_bridge_join() through…
- [Low] In lan9645x_port_mdb_add(), the existing `mdb->vid > VLAN_MAX`
check (returns -EOPNOTSUPP) runs before the new…
--
net: dsa: lan9645x: add bridge support
This adds hardware bridge offload to the lan9645x DSA driver. Up to 8
bridges are supported, each VLAN-unaware bridge gets its own reserved VID,
and at most one bridge can be VLAN-aware. The patch adds STP, learning,
flooding, host flood and ageing support, and refuses vlan_filtering
changes while ports are bridged. It also does not install FDB or MDB
entries that carry a VID on a VLAN-unaware bridge.
> diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
> index e840de1c918ba..94b46082cf5c4 100644
> --- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
> +++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c
[ ... ]
> +static bool lan9645x_bridge_vlan_proto_ok(const struct net_device *br)
> +{
> + u16 proto;
> +
> + return !br_vlan_enabled(br) || br_vlan_get_proto(br, &proto) ||
> + proto == ETH_P_8021Q;
> +}
[ ... ]
> +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)
> +{
[ ... ]
> + if (!lan9645x_bridge_vlan_proto_ok(bridge.dev)) {
> + NL_SET_ERR_MSG_MOD(extack,
> + "Only 802.1Q VLAN-aware bridges are supported");
> + return -EINVAL;
> + }
[Severity: Medium]
Is checking the protocol only in lan9645x_port_bridge_join() enough to
keep the hardware and the software bridge in agreement?
Once a port has joined, a protocol change goes through
__br_vlan_set_proto() in net/bridge/br_vlan.c:
err = switchdev_port_attr_set(br->dev, &attr, extack);
if (err && err != -EOPNOTSUPP)
return err;
The attribute is sent with SWITCHDEV_F_SKIP_EOPNOTSUPP. net/dsa/ has no
handler for SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL, so a command like
"ip link set br0 type bridge vlan_protocol 802.1ad" succeeds on a
VLAN-aware bridge that already has lan9645x ports.
After that, the hardware still treats 0x8100 tags as VLAN tags, but the
software bridge uses 802.1ad. Take a 0x8100-tagged VID 20 frame that
arrives on a port with pvid 10, where the port is also a member of VLAN
20. The hardware forwards it in VLAN 20, but the bridge policy would
classify it to VLAN 10.
The commit message mentions this limitation:
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.
It also says "Only bridge vlan_protocol 802.1Q is offloaded", but that
restriction can be bypassed after the join. Should the DSA core either
pass this attribute to drivers or refuse the change while ports are
offloaded?
[ ... ]
> @@ -503,6 +844,9 @@ static int lan9645x_port_mdb_add(struct dsa_switch *ds, int port,
> if (mdb->vid > VLAN_MAX)
> return -EOPNOTSUPP;
>
> + if (lan9645x_db_vid_unused(lan9645x, mdb->vid, &db))
> + return 0;
> +
[Severity: Low]
Should lan9645x_db_vid_unused() be checked before the VLAN_MAX check
here?
On a VLAN-unaware bridge, an MDB entry with a VID in 4032..4094 gets
-EOPNOTSUPP on add. lan9645x_port_mdb_del() has no range check and
returns 0 through lan9645x_db_vid_unused().
That does not match the commit message, which says "3) An MDB entry can
be added with any VID" and "they are accepted without being installed".
It also does not match the comment above lan9645x_db_vid_unused():
so a delete skips exactly the entries that the add skipped.
br_mdb_add() does not check the VID against the VLAN group when
vlan_filtering is off, so a command like this reaches the driver:
bridge mdb add dev br0 port br0 grp 01:00:5e:01:01:01 permanent vid 4040
For a host MDB the add fails, so dsa_port_do_mdb_add() never adds a
dsa_mac_addr to cpu_dp->mdbs. The later delete then fails in
dsa_port_do_mdb_del() with -ENOENT, and switchdev_port_obj_del_deferred()
logs it as an error.
The hardware ends up the same either way, since the entry is never
installed.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127-dsa-lan9645x-add-vlan-and-bridge-offload-v2-0-84adcd919dac%40microchip.com
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-28 9:30 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 9:24 [PATCH net-next v2 0/5] net: dsa: lan9645x: add VLAN and bridge offload Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 1/5] net: dsa: lan9645x: add the analyzer event register Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 2/5] net: dsa: lan9645x: add vlan support Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 3/5] net: dsa: lan9645x: add mac table integration Jens Emil Schulz Østergaard
2026-11-27 9:24 ` [PATCH net-next v2 4/5] net: dsa: lan9645x: add mdb management Jens Emil Schulz Østergaard
2026-11-28 9:30 ` netdev-bot+sashiko
2026-11-27 9:24 ` [PATCH net-next v2 5/5] net: dsa: lan9645x: add bridge support Jens Emil Schulz Østergaard
2026-11-28 9:30 ` 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
all inboxes | Powered by JetHome®