* [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support
@ 2026-03-31 23:00 Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 01/10] net: dsa: tag_rtl8_4: update format description Luiz Angelo Daros de Luca
` (9 more replies)
0 siblings, 10 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
This series introduces bridge offloading, FDB management, and VLAN support
for the Realtek rtl8365mb DSA switch driver. The primary goal is to
enable hardware frame forwarding between bridge ports, reducing CPU
overhead and providing advanced features like VLAN and FDB isolation.
Some of these patches are based on original work by Alvin Šipraga,
subsequently adapted and updated for the current net-next state.
---
I attempted to reach Alvin for review of the final version but was
unable to establish contact. Any regressions in this version are my
responsibility.
Patch 04 introduces FIELD_WIDTH() in include/linux/bitfield.h. Although
not strictly part of the driver itself, it is required by subsequent
patches. I am happy to split this into a separate submission if
preferred.
To: Andrew Lunn <andrew@lunn.ch>
To: Vladimir Oltean <olteanv@gmail.com>
To: David S. Miller <davem@davemloft.net>
To: Eric Dumazet <edumazet@google.com>
To: Jakub Kicinski <kuba@kernel.org>
To: Paolo Abeni <pabeni@redhat.com>
To: Simon Horman <horms@kernel.org>
To: Linus Walleij <linusw@kernel.org>
To: Alvin Šipraga <alsi@bang-olufsen.dk>
To: Yury Norov <yury.norov@gmail.com>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Russell King <linux@armlinux.org.uk>
Cc: netdev@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
Alvin Šipraga (8):
net: dsa: tag_rtl8_4: update format description
net: dsa: realtek: rtl8365mb: set STP state to disabled early
net: dsa: realtek: rtl8365mb: prepare for multiple source files
net: dsa: realtek: rtl8365mb: add table lookup interface
net: dsa: realtek: rtl8365mb: add VLAN support
net: dsa: realtek: rtl8365mb: add port_bridge_{join,leave}
net: dsa: realtek: rtl8365mb: add FDB support
net: dsa: realtek: rtl8365mb: add bridge port flags
Luiz Angelo Daros de Luca (2):
bitfield.h: add FIELD_WIDTH()
net: dsa: tag_rtl8_4: set KEEP flag
drivers/net/dsa/realtek/Makefile | 4 +
drivers/net/dsa/realtek/rtl8365mb_l2.c | 465 ++++++++++++
drivers/net/dsa/realtek/rtl8365mb_l2.h | 59 ++
.../dsa/realtek/{rtl8365mb.c => rtl8365mb_main.c} | 711 +++++++++++++++++-
drivers/net/dsa/realtek/rtl8365mb_table.c | 255 +++++++
drivers/net/dsa/realtek/rtl8365mb_table.h | 133 ++++
drivers/net/dsa/realtek/rtl8365mb_vlan.c | 805 +++++++++++++++++++++
drivers/net/dsa/realtek/rtl8365mb_vlan.h | 30 +
include/linux/bitfield.h | 13 +
net/dsa/tag_rtl8_4.c | 21 +-
10 files changed, 2481 insertions(+), 15 deletions(-)
---
base-commit: 1a8dd88469bf742fd5eda91cd8e0f720a983ec5a
change-id: 20260323-realtek_forward-1bac3a77c664
Best regards,
--
Luiz Angelo Daros de Luca <luizluca@gmail.com>
^ permalink raw reply [flat|nested] 24+ messages in thread
* [net-next PATCH 01/10] net: dsa: tag_rtl8_4: update format description
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
@ 2026-03-31 23:00 ` Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 02/10] net: dsa: realtek: rtl8365mb: set STP state to disabled early Luiz Angelo Daros de Luca
` (8 subsequent siblings)
9 siblings, 0 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
From: Alvin Šipraga <alsi@bang-olufsen.dk>
Document the updated tag layout fields (EFID, VSEL/VIDX) and clarify
which bits are set/cleared when emitting tags.
Co-developed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
net/dsa/tag_rtl8_4.c | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/net/dsa/tag_rtl8_4.c b/net/dsa/tag_rtl8_4.c
index 2464545da4d2..b7ed39c5419f 100644
--- a/net/dsa/tag_rtl8_4.c
+++ b/net/dsa/tag_rtl8_4.c
@@ -17,8 +17,8 @@
* | (8-bit) | (8-bit) |
* | Protocol [0x04] | REASON | b
* |-----------------------------------+-----------------------------------| y
- * | (1) | (1) | (2) | (1) | (3) | (1) | (1) | (1) | (5) | t
- * | FID_EN | X | FID | PRI_EN | PRI | KEEP | X | LEARN_DIS | X | e
+ * | (1) | (3) | (1) | (3) | (1) | (1) | (1) | (5) | t
+ * | EFID_EN | EFID | PRI_EN | PRI | KEEP | VSEL | LEARN_DIS | VIDX | e
* |-----------------------------------+-----------------------------------| s
* | (1) | (15-bit) | |
* | ALLOW | TX/RX | v
@@ -32,19 +32,22 @@
* EtherType | note that Realtek uses the same EtherType for
* | other incompatible tag formats (e.g. tag_rtl4_a.c)
* Protocol | 0x04: indicates that this tag conforms to this format
- * X | reserved
* ------------+-------------
* REASON | reason for forwarding packet to CPU
* | 0: packet was forwarded or flooded to CPU
* | 80: packet was trapped to CPU
- * FID_EN | 1: packet has an FID
- * | 0: no FID
- * FID | FID of packet (if FID_EN=1)
+ * EFID_EN | 1: packet has an EFID
+ * | 0: no EFID
+ * EFID | Extended filter ID (EFID) of packet (if EFID_EN=1)
* PRI_EN | 1: force priority of packet
* | 0: don't force priority
* PRI | priority of packet (if PRI_EN=1)
* KEEP | preserve packet VLAN tag format
+ * VSEL | 0: switch should classify packet according to VLAN tag
+ * | 1: switch should classify packet according to VLAN membership
+ * | configuration with index VIDX
* LEARN_DIS | don't learn the source MAC address of the packet
+ * VIDX | index of a VLAN membership configuration to use with VSEL
* ALLOW | 1: treat TX/RX field as an allowance port mask, meaning the
* | packet may only be forwarded to ports specified in the
* | mask
@@ -111,7 +114,7 @@ static void rtl8_4_write_tag(struct sk_buff *skb, struct net_device *dev,
/* Set Protocol; zero REASON */
tag16[1] = htons(FIELD_PREP(RTL8_4_PROTOCOL, RTL8_4_PROTOCOL_RTL8365MB));
- /* Zero FID_EN, FID, PRI_EN, PRI, KEEP; set LEARN_DIS */
+ /* Zero EFID_EN, EFID, PRI_EN, PRI, VSEL, VIDX, KEEP; set LEARN_DIS */
tag16[2] = htons(FIELD_PREP(RTL8_4_LEARN_DIS, 1));
/* Zero ALLOW; set RX (CPU->switch) forwarding port mask */
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [net-next PATCH 02/10] net: dsa: realtek: rtl8365mb: set STP state to disabled early
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 01/10] net: dsa: tag_rtl8_4: update format description Luiz Angelo Daros de Luca
@ 2026-03-31 23:00 ` Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 03/10] net: dsa: realtek: rtl8365mb: prepare for multiple source files Luiz Angelo Daros de Luca
` (7 subsequent siblings)
9 siblings, 0 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
From: Alvin Šipraga <alsi@bang-olufsen.dk>
Since we are going around and manipulating a number of port settings
during setup, it's safer to assume that the port is disabled to begin
with. This is purely defensive.
Co-developed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
drivers/net/dsa/realtek/rtl8365mb.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/drivers/net/dsa/realtek/rtl8365mb.c b/drivers/net/dsa/realtek/rtl8365mb.c
index 31fa94dac627..530ff5503543 100644
--- a/drivers/net/dsa/realtek/rtl8365mb.c
+++ b/drivers/net/dsa/realtek/rtl8365mb.c
@@ -1989,6 +1989,12 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
if (dsa_is_unused_port(ds, i))
continue;
+ /* Set the initial STP state of all ports to DISABLED, otherwise
+ * ports will still forward frames to the CPU despite being
+ * administratively down by default.
+ */
+ rtl8365mb_port_stp_state_set(ds, i, BR_STATE_DISABLED);
+
/* Forward only to the CPU */
ret = rtl8365mb_port_set_isolation(priv, i, cpu->mask);
if (ret)
@@ -1999,12 +2005,6 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
if (ret)
goto out_teardown_irq;
- /* Set the initial STP state of all ports to DISABLED, otherwise
- * ports will still forward frames to the CPU despite being
- * administratively down by default.
- */
- rtl8365mb_port_stp_state_set(ds, i, BR_STATE_DISABLED);
-
/* Set up per-port private data */
p->priv = priv;
p->index = i;
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [net-next PATCH 03/10] net: dsa: realtek: rtl8365mb: prepare for multiple source files
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 01/10] net: dsa: tag_rtl8_4: update format description Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 02/10] net: dsa: realtek: rtl8365mb: set STP state to disabled early Luiz Angelo Daros de Luca
@ 2026-03-31 23:00 ` Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH() Luiz Angelo Daros de Luca
` (6 subsequent siblings)
9 siblings, 0 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
From: Alvin Šipraga <alsi@bang-olufsen.dk>
Rename rtl8365mb.c to rtl8365mb_main.c in preparation for subsequent
commits which add additional source files to the driver.
The trailing backslash in the Makefile is deliberate. It allows for new
files to be added without clobbering git history.
Co-developed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
drivers/net/dsa/realtek/Makefile | 1 +
drivers/net/dsa/realtek/{rtl8365mb.c => rtl8365mb_main.c} | 0
2 files changed, 1 insertion(+)
diff --git a/drivers/net/dsa/realtek/Makefile b/drivers/net/dsa/realtek/Makefile
index 17367bcba496..3f986e04912f 100644
--- a/drivers/net/dsa/realtek/Makefile
+++ b/drivers/net/dsa/realtek/Makefile
@@ -16,3 +16,4 @@ ifdef CONFIG_NET_DSA_REALTEK_RTL8366RB_LEDS
rtl8366-objs += rtl8366rb-leds.o
endif
obj-$(CONFIG_NET_DSA_REALTEK_RTL8365MB) += rtl8365mb.o
+rtl8365mb-objs := rtl8365mb_main.o \
diff --git a/drivers/net/dsa/realtek/rtl8365mb.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
similarity index 100%
rename from drivers/net/dsa/realtek/rtl8365mb.c
rename to drivers/net/dsa/realtek/rtl8365mb_main.c
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
` (2 preceding siblings ...)
2026-03-31 23:00 ` [net-next PATCH 03/10] net: dsa: realtek: rtl8365mb: prepare for multiple source files Luiz Angelo Daros de Luca
@ 2026-03-31 23:00 ` Luiz Angelo Daros de Luca
2026-04-01 2:15 ` Yury Norov
2026-03-31 23:00 ` [net-next PATCH 05/10] net: dsa: realtek: rtl8365mb: add table lookup interface Luiz Angelo Daros de Luca
` (5 subsequent siblings)
9 siblings, 1 reply; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
Some hardware stores logical fields split across multiple registers,
requiring partial values to be reassembled using shifts and masks.
Add FIELD_WIDTH(), a helper that returns the number of contiguous bits
covered by a bitfield mask. This allows callers to determine how much a
partial value needs to be shifted when reconstructing a field.
This complements FIELD_MAX() and FIELD_FIT(), and avoids reimplementing
bit counting logic in drivers.
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
include/linux/bitfield.h | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index 54aeeef1f0ec..8d6c1c309c3b 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -111,6 +111,19 @@
(typeof(_mask))((_mask) >> __bf_shf(_mask)); \
})
+/**
+ * FIELD_WIDTH() - return the width of a bitfield
+ * @_mask: shifted mask defining the field's length and position
+ *
+ * Returns the number of contiguous bits covered by @_mask.
+ * This corresponds to the bit width of FIELD_MAX(@_mask).
+ */
+#define FIELD_WIDTH(_mask) \
+ ({ \
+ __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_WIDTH: "); \
+ __bf_shf(~FIELD_MAX(_mask)); \
+ })
+
/**
* FIELD_FIT() - check if value fits in the field
* @_mask: shifted mask defining the field's length and position
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [net-next PATCH 05/10] net: dsa: realtek: rtl8365mb: add table lookup interface
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
` (3 preceding siblings ...)
2026-03-31 23:00 ` [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH() Luiz Angelo Daros de Luca
@ 2026-03-31 23:00 ` Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support Luiz Angelo Daros de Luca
` (4 subsequent siblings)
9 siblings, 0 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
From: Alvin Šipraga <alsi@bang-olufsen.dk>
Add a generic table lookup interface to centralize access to
the RTL8365MB internal tables.
This interface abstracts the low-level table access logic and
will be used by subsequent commits to implement FDB and VLAN
operations.
Co-developed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
drivers/net/dsa/realtek/Makefile | 1 +
drivers/net/dsa/realtek/rtl8365mb_table.c | 255 ++++++++++++++++++++++++++++++
drivers/net/dsa/realtek/rtl8365mb_table.h | 133 ++++++++++++++++
3 files changed, 389 insertions(+)
diff --git a/drivers/net/dsa/realtek/Makefile b/drivers/net/dsa/realtek/Makefile
index 3f986e04912f..99654c4c5a3d 100644
--- a/drivers/net/dsa/realtek/Makefile
+++ b/drivers/net/dsa/realtek/Makefile
@@ -17,3 +17,4 @@ rtl8366-objs += rtl8366rb-leds.o
endif
obj-$(CONFIG_NET_DSA_REALTEK_RTL8365MB) += rtl8365mb.o
rtl8365mb-objs := rtl8365mb_main.o \
+ rtl8365mb_table.o \
diff --git a/drivers/net/dsa/realtek/rtl8365mb_table.c b/drivers/net/dsa/realtek/rtl8365mb_table.c
new file mode 100644
index 000000000000..e706ea2ccb85
--- /dev/null
+++ b/drivers/net/dsa/realtek/rtl8365mb_table.c
@@ -0,0 +1,255 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Look-up table query interface for the rtl8365mb switch family
+ *
+ * Copyright (C) 2022 Alvin Šipraga <alsi@bang-olufsen.dk>
+ */
+
+#include "rtl8365mb_table.h"
+#include <linux/regmap.h>
+
+/* Table access control register */
+#define RTL8365MB_TABLE_CTRL_REG 0x0500
+/* Should be one of rtl8365mb_table enum members */
+#define RTL8365MB_TABLE_CTRL_TABLE_MASK GENMASK(2, 0)
+/* Should be one of rtl8365mb_table_op enum members */
+#define RTL8365MB_TABLE_CTRL_OP_MASK GENMASK(3, 3)
+/* Should be one of rtl8365mb_table_l2_method enum members */
+#define RTL8365MB_TABLE_CTRL_METHOD_MASK GENMASK(7, 4)
+/* NOTE: PORT_MASK is only 4 bit, which suggests that port-based
+ * look-up of the L2 table only works for physical port addresses
+ * 0~4. It could be that the Realtek driver is out-of-date and
+ * actually the mask is something like 0xFF00, but this is
+ * unconfirmed.
+ */
+#define RTL8365MB_TABLE_CTRL_PORT_MASK GENMASK(11, 8)
+
+/* Table access address register */
+#define RTL8365MB_TABLE_ACCESS_ADDR_REG 0x0501
+#define RTL8365MB_TABLE_ADDR_MASK GENMASK(13, 0)
+
+/* Table status register */
+#define RTL8365MB_TABLE_STATUS_REG 0x0502
+#define RTL8365MB_TABLE_STATUS_ADDRESS_MASK GENMASK(10, 0)
+/* set for L3, unset for L2 */
+#define RTL8365MB_TABLE_STATUS_ADDR_TYPE_MASK GENMASK(11, 11)
+#define RTL8365MB_TABLE_STATUS_HIT_STATUS_MASK GENMASK(12, 12)
+#define RTL8365MB_TABLE_STATUS_BUSY_FLAG_MASK GENMASK(13, 13)
+#define RTL8365MB_TABLE_STATUS_ADDRESS_EXT_MASK GENMASK(14, 14)
+
+/* Table read/write registers */
+#define RTL8365MB_TABLE_WRITE_BASE 0x0510
+#define RTL8365MB_TABLE_WRITE_REG(_x) \
+ (RTL8365MB_TABLE_WRITE_BASE + (_x))
+#define RTL8365MB_TABLE_READ_BASE 0x0520
+#define RTL8365MB_TABLE_READ_REG(_x) \
+ (RTL8365MB_TABLE_READ_BASE + (_x))
+#define RTL8365MB_TABLE_ENTRY_MAX_SIZE 10
+#define RTL8365MB_TABLE_10TH_DATA_MASK GENMASK(3, 0)
+#define RTL8365MB_TABLE_WRITE_10TH_REG \
+ RTL8365MB_TABLE_WRITE_REG(RTL8365MB_TABLE_ENTRY_MAX_SIZE - 1)
+
+static int rtl8365mb_table_poll_busy(struct realtek_priv *priv)
+{
+ u32 val;
+
+ return regmap_read_poll_timeout(priv->map_nolock,
+ RTL8365MB_TABLE_STATUS_REG, val,
+ !FIELD_GET(RTL8365MB_TABLE_STATUS_BUSY_FLAG_MASK, val),
+ 10, 100);
+}
+
+int rtl8365mb_table_query(struct realtek_priv *priv,
+ enum rtl8365mb_table table,
+ enum rtl8365mb_table_op op, u16 *addr,
+ enum rtl8365mb_table_l2_method method,
+ u16 port, u16 *data, size_t size)
+{
+ bool addr_as_input = true;
+ bool write_data = false;
+ int ret = 0;
+ u32 cmd;
+ u32 val;
+ u32 hit;
+
+ if (!addr) {
+ dev_err(priv->dev, "%s: addr is NULL\n", __func__);
+ return -EINVAL;
+ }
+
+ if (!data) {
+ dev_err(priv->dev, "%s: data is NULL\n", __func__);
+ return -EINVAL;
+ }
+
+ if (size > RTL8365MB_TABLE_ENTRY_MAX_SIZE) {
+ dev_err(priv->dev, "%s: size too big: %zu\n", __func__, size);
+ return -E2BIG;
+ }
+
+ if (size == 0) {
+ dev_err(priv->dev, "%s: size is 0\n", __func__);
+ return -EINVAL;
+ }
+
+ if (!FIELD_FIT(RTL8365MB_TABLE_CTRL_TABLE_MASK, table)) {
+ dev_err(priv->dev, "%s: table %d does not fit in MASK\n",
+ __func__, table);
+ return -EINVAL;
+ }
+
+ /* Prepare target table and operation (read or write) */
+ cmd = 0;
+ cmd |= FIELD_PREP(RTL8365MB_TABLE_CTRL_TABLE_MASK, table);
+ cmd |= FIELD_PREP(RTL8365MB_TABLE_CTRL_OP_MASK, op);
+ if (op == RTL8365MB_TABLE_OP_READ && table == RTL8365MB_TABLE_L2) {
+ if (!FIELD_FIT(RTL8365MB_TABLE_CTRL_METHOD_MASK, method))
+ return -EINVAL;
+
+ cmd |= FIELD_PREP(RTL8365MB_TABLE_CTRL_METHOD_MASK, method);
+ switch (method) {
+ case RTL8365MB_TABLE_L2_METHOD_MAC:
+ /*
+ * Method MAC requires as input the same L2 table format
+ * you'll get as result. However, it might only use mac
+ * address and FID/VID fields.
+ */
+ write_data = true;
+
+ /* METHOD_MAC does not use addr as input, but may return
+ * the matched index.
+ */
+ addr_as_input = false;
+
+ break;
+ case RTL8365MB_TABLE_L2_METHOD_ADDR:
+ case RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT:
+ case RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC:
+ case RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_MC:
+ break;
+ case RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC_PORT:
+ if (!FIELD_FIT(RTL8365MB_TABLE_CTRL_PORT_MASK, port))
+ return -EINVAL;
+
+ cmd |= FIELD_PREP(RTL8365MB_TABLE_CTRL_PORT_MASK, port);
+ break;
+ default:
+ return -EINVAL;
+ }
+ } else if (op == RTL8365MB_TABLE_OP_WRITE) {
+ write_data = true;
+
+ /* Writing to L2 does not use addr as input, as the table index
+ * is derived from key fields.
+ */
+ if (table == RTL8365MB_TABLE_L2)
+ addr_as_input = false;
+ }
+
+ /* Validate addr only when used as an input */
+ if (addr_as_input) {
+ if (!FIELD_FIT(RTL8365MB_TABLE_ADDR_MASK, *addr)) {
+ dev_err(priv->dev, "%s: addr %u does not fit in MASK\n",
+ __func__, *addr);
+ return -EINVAL;
+ }
+ }
+
+ /* To prevent concurrent access to the look-up tables, take the regmap
+ * lock manually and access via the map_nolock regmap.
+ */
+ mutex_lock(&priv->map_lock);
+
+ /* Write entry data if writing to the table (or L2_METHOD_MAC) */
+ if (write_data) {
+ /* bulk write data up to 9th byte */
+ ret = regmap_bulk_write(priv->map_nolock,
+ RTL8365MB_TABLE_WRITE_BASE,
+ data,
+ min_t(size_t, size,
+ RTL8365MB_TABLE_ENTRY_MAX_SIZE -
+ 1));
+ if (ret)
+ goto out;
+
+ /* 10th register uses only 4 less significant bits */
+ if (size == RTL8365MB_TABLE_ENTRY_MAX_SIZE) {
+ val = FIELD_PREP(RTL8365MB_TABLE_10TH_DATA_MASK,
+ data[size - 1]);
+ ret = regmap_update_bits(priv->map_nolock,
+ RTL8365MB_TABLE_WRITE_10TH_REG,
+ RTL8365MB_TABLE_10TH_DATA_MASK,
+ val);
+ }
+
+ if (ret)
+ goto out;
+ }
+
+ /* Write address (if needed) */
+ if (addr_as_input) {
+ ret = regmap_write(priv->map_nolock,
+ RTL8365MB_TABLE_ACCESS_ADDR_REG,
+ FIELD_PREP(RTL8365MB_TABLE_ADDR_MASK,
+ *addr));
+ if (ret)
+ goto out;
+ }
+
+ /* Execute */
+ ret = regmap_write(priv->map_nolock, RTL8365MB_TABLE_CTRL_REG, cmd);
+ if (ret)
+ goto out;
+
+ /* Poll for completion */
+ ret = rtl8365mb_table_poll_busy(priv);
+ if (ret)
+ goto out;
+
+ /* For both reads and writes to the L2 table, check status */
+ if (table == RTL8365MB_TABLE_L2) {
+ ret = regmap_read(priv->map_nolock, RTL8365MB_TABLE_STATUS_REG,
+ &val);
+ if (ret)
+ goto out;
+
+ /* Did the query find an entry? */
+ hit = FIELD_GET(RTL8365MB_TABLE_STATUS_HIT_STATUS_MASK, val);
+ if (!hit) {
+ ret = -ENOENT;
+ goto out;
+ }
+
+ /* If so, extract the address */
+ *addr = 0;
+ *addr |= FIELD_GET(RTL8365MB_TABLE_STATUS_ADDRESS_MASK, val);
+ *addr |= FIELD_GET(RTL8365MB_TABLE_STATUS_ADDRESS_EXT_MASK, val)
+ << 11;
+ /* only set if it is a L3 address */
+ *addr |= FIELD_GET(RTL8365MB_TABLE_STATUS_ADDR_TYPE_MASK, val)
+ << 12;
+ }
+
+ /* Finally, get the table entry if we were reading */
+ if (op == RTL8365MB_TABLE_OP_READ) {
+ ret = regmap_bulk_read(priv->map_nolock,
+ RTL8365MB_TABLE_READ_BASE,
+ data, size);
+
+ /* For the biggest table entries, the uppermost table
+ * entry register has space for only one nibble. Mask
+ * out the remainder bits. Empirically I saw nothing
+ * wrong with omitting this mask, but it may prevent
+ * unwanted behaviour. FYI.
+ */
+ if (size == RTL8365MB_TABLE_ENTRY_MAX_SIZE) {
+ val = FIELD_GET(RTL8365MB_TABLE_10TH_DATA_MASK,
+ data[size - 1]);
+ data[size - 1] = val;
+ }
+ }
+
+out:
+ mutex_unlock(&priv->map_lock);
+
+ return ret;
+}
diff --git a/drivers/net/dsa/realtek/rtl8365mb_table.h b/drivers/net/dsa/realtek/rtl8365mb_table.h
new file mode 100644
index 000000000000..0b1a89bd81f1
--- /dev/null
+++ b/drivers/net/dsa/realtek/rtl8365mb_table.h
@@ -0,0 +1,133 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Look-up table query interface for the rtl8365mb switch family
+ *
+ * Copyright (C) 2022 Alvin Šipraga <alsi@bang-olufsen.dk>
+ */
+
+#ifndef _REALTEK_RTL8365MB_TABLE_H
+#define _REALTEK_RTL8365MB_TABLE_H
+
+#include <linux/if_ether.h>
+#include <linux/types.h>
+
+#include "realtek.h"
+
+/**
+ * struct rtl8365mb_table - available switch tables
+ *
+ * @RTL8365MB_TABLE_ACL_RULE - ACL rules
+ * @RTL8365MB_TABLE_ACL_ACTION - ACL actions
+ * @RTL8365MB_TABLE_CVLAN - VLAN4k configurations
+ * @RTL8365MB_TABLE_L2 - filtering database (2K hash table)
+ * @RTL8365MB_TABLE_IGMP_GROUP - IGMP group database (readonly)
+ *
+ * NOTE: Don't change the enum values. They must concur with the field
+ * described by @RTL8365MB_TABLE_CTRL_TABLE_MASK.
+ */
+enum rtl8365mb_table {
+ RTL8365MB_TABLE_ACL_RULE = 1,
+ RTL8365MB_TABLE_ACL_ACTION = 2,
+ RTL8365MB_TABLE_CVLAN = 3,
+ RTL8365MB_TABLE_L2 = 4,
+ RTL8365MB_TABLE_IGMP_GROUP = 5,
+};
+
+/**
+ * enum rtl8365mb_table_op - table query operation
+ *
+ * @RTL8365MB_TABLE_OP_READ: read an entry from the target table
+ * @RTL8365MB_TABLE_OP_WRITE: write an entry to the target table
+ *
+ * NOTE: Don't change the enum values. They must concur with the field
+ * described by @RTL8365MB_TABLE_CTRL_OP_MASK.
+ */
+enum rtl8365mb_table_op {
+ RTL8365MB_TABLE_OP_READ = 0,
+ RTL8365MB_TABLE_OP_WRITE = 1,
+};
+
+/**
+ * enum rtl8365mb_table_l2_method - look-up method for read queries of L2 table
+ *
+ * @RTL8365MB_TABLE_L2_METHOD_MAC: look-up by source MAC address and FID (or
+ * VID)
+ * @RTL8365MB_TABLE_L2_METHOD_ADDR: look-up by entry address
+ * @RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT: look-up next entry starting from the
+ * supplied address
+ * @RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC: same as ADDR_NEXT but search only
+ * unicast addresses
+ * @RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_MC: same as ADDR_NEXT but search only
+ * multicast addresses
+ * @RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC_PORT: same as ADDR_NEXT_UC but
+ * search only entries with matching source port
+ *
+ * NOTE: Don't change the enum values. They must concur with the field
+ * described by @RTL8365MB_TABLE_CTRL_METHOD_MASK
+ */
+enum rtl8365mb_table_l2_method {
+ RTL8365MB_TABLE_L2_METHOD_MAC = 0,
+ RTL8365MB_TABLE_L2_METHOD_ADDR = 1,
+ RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT = 2,
+ RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC = 3,
+ RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_MC = 4,
+ /*
+ * RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_MC_L3 = 5,
+ * RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_MC_L2L3 = 6,
+ */
+ RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC_PORT = 7,
+};
+
+/**
+ * rtl8365mb_table_query() - read from or write to a switch table
+ * @priv: driver context
+ * @table: target table, see &enum rtl8365mb_table
+ * @op: read or write operation, see &enum rtl8365mb_table_op
+ * @addr: table address. For indexed tables, this selects the entry to access.
+ * For L2 read queries, it is ignored as input for MAC-based lookup
+ * methods and used as input for address-based lookup methods. On
+ * successful L2 queries, it is updated with the matched entry address.
+ * @method: L2 table lookup method, see &enum rtl8365mb_table_l2_method.
+ * Ignored for non-L2 tables.
+ * @port: for L2 read queries using method
+ * %RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC_PORT, restrict the search
+ * to entries associated with this source port. Ignored otherwise.
+ * @data: data buffer used to read from or write to the table. For L2 MAC
+ * lookups, this buffer provides the lookup key and receives the
+ * matched entry contents on success.
+ * @size: size of @data in 16-bit words
+ *
+ * This function provides unified access to the internal tables of the switch.
+ * All tables except the L2 table are simple indexed tables, where @addr
+ * selects the entry and @op determines whether the access is a read or a
+ * write operation.
+ *
+ * The L2 table is a hash table and supports multiple lookup methods. For
+ * %RTL8365MB_TABLE_L2_METHOD_MAC, an entry is searched based on the MAC
+ * address and FID/VID fields provided in @data, using the same format as
+ * an L2 table entry. Address-based methods either read a specific entry
+ * (%RTL8365MB_TABLE_L2_METHOD_ADDR) or iterate over valid entries starting
+ * from @addr (%RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT and variants). When using
+ * %RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC_PORT, only entries associated with
+ * the specified @port are considered.
+ *
+ * On successful L2 lookups, @addr is updated with the matched table address
+ * and @data contains the corresponding table entry. If no matching entry
+ * is found, -ENOENT is returned.
+ *
+ * The contents of @data are used as input when writing to tables or when
+ * specifying the lookup key for L2 MAC searches, and as output for all
+ * successful read operations. If an error occurs, the contents of @addr
+ * and @data are undefined.
+ *
+ * @size must match the size of the target table entry, expressed in 16-bit
+ * words. This function only validates that it is non-zero and fits in the
+ * available register space.
+ *
+ */
+int rtl8365mb_table_query(struct realtek_priv *priv,
+ enum rtl8365mb_table table,
+ enum rtl8365mb_table_op op, u16 *addr,
+ enum rtl8365mb_table_l2_method method,
+ u16 port, u16 *data, size_t size);
+
+#endif /* _REALTEK_RTL8365MB_TABLE_H */
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
` (4 preceding siblings ...)
2026-03-31 23:00 ` [net-next PATCH 05/10] net: dsa: realtek: rtl8365mb: add table lookup interface Luiz Angelo Daros de Luca
@ 2026-03-31 23:00 ` Luiz Angelo Daros de Luca
2026-04-01 2:27 ` Yury Norov
2026-03-31 23:00 ` [net-next PATCH 07/10] net: dsa: realtek: rtl8365mb: add port_bridge_{join,leave} Luiz Angelo Daros de Luca
` (3 subsequent siblings)
9 siblings, 1 reply; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
From: Alvin Šipraga <alsi@bang-olufsen.dk>
Realtek RTL8365MB switches (a.k.a. RTL8367C family) use two different
structures for VLANs:
- VLAN4K: A full table with 4096 entries defining port membership and
tagging.
- VLANMC: A smaller table with 32 entries used primarily for PVID
assignment.
In this hardware, a port's PVID must point to an index in the VLANMC
table rather than a VID directly. Since the VLANMC table is limited to
32 entries, the driver implements a dynamic allocation scheme to
maximize resource usage:
- VLAN4K is treated by the driver as the source of truth for membership.
- A VLANMC entry is only allocated when a port is configured to use a
specific VID as its PVID.
- VLANMC entries are deleted when no longer needed as a PVID by any port.
Although VLANMC has a members field, the switch only checks membership
in the VLAN4K table. However, when a corresponding VLAN entry also exists
in VLANMC, this driver keeps both membership configurations in sync.
VLANMC index 0, although a valid entry, is reserved in this driver as a
neutral PVID value for ports not using a specific PVID.
In the subsequent RTL8367D switch family, VLANMC table was
removed and PVID assignment was delegated to a dedicated set of
registers.
All ports start isolated, forwarding exclusively to CPU ports, and
with VLAN transparent, ignoring VLAN membership. Once a member in a
bridge, the port isolation is expanded to include the bridge members.
When that bridge enables VLAN filtering, the VLAN transparent feature is
disabled, letting the switch filter based on VLAN setup.
Co-developed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
drivers/net/dsa/realtek/Makefile | 1 +
drivers/net/dsa/realtek/rtl8365mb_main.c | 256 ++++++++++
drivers/net/dsa/realtek/rtl8365mb_vlan.c | 805 +++++++++++++++++++++++++++++++
drivers/net/dsa/realtek/rtl8365mb_vlan.h | 30 ++
4 files changed, 1092 insertions(+)
diff --git a/drivers/net/dsa/realtek/Makefile b/drivers/net/dsa/realtek/Makefile
index 99654c4c5a3d..b7fc4e852fd8 100644
--- a/drivers/net/dsa/realtek/Makefile
+++ b/drivers/net/dsa/realtek/Makefile
@@ -18,3 +18,4 @@ endif
obj-$(CONFIG_NET_DSA_REALTEK_RTL8365MB) += rtl8365mb.o
rtl8365mb-objs := rtl8365mb_main.o \
rtl8365mb_table.o \
+ rtl8365mb_vlan.o \
diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
index 530ff5503543..c604bd744d38 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_main.c
+++ b/drivers/net/dsa/realtek/rtl8365mb_main.c
@@ -104,6 +104,7 @@
#include "realtek-smi.h"
#include "realtek-mdio.h"
#include "rtl83xx.h"
+#include "rtl8365mb_vlan.h"
/* Family-specific data and limits */
#define RTL8365MB_PHYADDRMAX 7
@@ -292,6 +293,67 @@
#define RTL8365MB_MSTI_CTRL_PORT_STATE_MASK(_physport) \
(0x3 << RTL8365MB_MSTI_CTRL_PORT_STATE_OFFSET((_physport)))
+/* Miscellaneous port configuration register, incl. VLAN egress mode */
+#define RTL8365MB_PORT_MISC_CFG_REG_BASE 0x000E
+#define RTL8365MB_PORT_MISC_CFG_REG(_p) \
+ (RTL8365MB_PORT_MISC_CFG_REG_BASE + ((_p) << 5))
+#define RTL8365MB_PORT_MISC_CFG_SMALL_TAG_IPG_MASK 0x8000
+#define RTL8365MB_PORT_MISC_CFG_TX_ITFSP_MODE_MASK 0x4000
+#define RTL8365MB_PORT_MISC_CFG_FLOWCTRL_INDEP_MASK 0x2000
+#define RTL8365MB_PORT_MISC_CFG_DOT1Q_REMARK_ENABLE_MASK 0x1000
+#define RTL8365MB_PORT_MISC_CFG_INGRESSBW_FLOWCTRL_MASK 0x0800
+#define RTL8365MB_PORT_MISC_CFG_INGRESSBW_IFG_MASK 0x0400
+#define RTL8365MB_PORT_MISC_CFG_RX_SPC_MASK 0x0200
+#define RTL8365MB_PORT_MISC_CFG_CRC_SKIP_MASK 0x0100
+#define RTL8365MB_PORT_MISC_CFG_PKTGEN_TX_FIRST_MASK 0x0080
+#define RTL8365MB_PORT_MISC_CFG_MAC_LOOPBACK_MASK 0x0040
+/* See &rtl8365mb_vlan_egress_mode */
+#define RTL8365MB_PORT_MISC_CFG_VLAN_EGRESS_MODE_MASK 0x0030
+#define RTL8365MB_PORT_MISC_CFG_CONGESTION_SUSTAIN_TIME_MASK 0x000F
+
+/**
+ * enum rtl8365mb_vlan_egress_mode - port VLAN engress mode
+ * @RTL8365MB_VLAN_EGRESS_MODE_ORIGINAL: follow untag mask in VLAN4k table entry
+ * @RTL8365MB_VLAN_EGRESS_MODE_KEEP: the VLAN tag format of egressed packets
+ * will remain the same as their ingressed format, but the priority and VID
+ * fields may be altered
+ * @RTL8365MB_VLAN_EGRESS_MODE_PRI_TAG: always egress with priority tag
+ * @RTL8365MB_VLAN_EGRESS_MODE_REAL_KEEP: the VLAN tag format of egressed
+ * packets will remain the same as their ingressed format, and neither the
+ * priority nor VID fields can be altered
+ */
+enum rtl8365mb_vlan_egress_mode {
+ RTL8365MB_VLAN_EGRESS_MODE_ORIGINAL = 0,
+ RTL8365MB_VLAN_EGRESS_MODE_KEEP = 1,
+ RTL8365MB_VLAN_EGRESS_MODE_PRI_TAG = 2,
+ RTL8365MB_VLAN_EGRESS_MODE_REAL_KEEP = 3,
+};
+
+/* VLAN control register */
+#define RTL8365MB_VLAN_CTRL_REG 0x07A8
+#define RTL8365MB_VLAN_CTRL_EN_MASK 0x0001
+
+/* VLAN ingress filter register */
+#define RTL8365MB_VLAN_INGRESS_REG 0x07A9
+#define RTL8365MB_VLAN_INGRESS_MASK GENMASK(10, 0)
+#define RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_OFFSET(_p) (_p)
+#define RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_MASK(_p) BIT(_p)
+
+/* VLAN "transparent" setting registers */
+#define RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG_BASE 0x09D0
+#define RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG(_p) \
+ (RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG_BASE + (_p))
+
+/* Frame type filtering registers */
+#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_BASE 0x07aa
+#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_REG(port) \
+ (RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_BASE + ((port) >> 3))
+/* required as FIELD_PREP cannot use non-constant masks */
+#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_MASK(port) \
+ (0x3 << RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_OFFSET(port))
+#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_OFFSET(port) \
+ (((port) & 0x7) << 1)
+
/* MIB counter value registers */
#define RTL8365MB_MIB_COUNTER_BASE 0x1000
#define RTL8365MB_MIB_COUNTER_REG(_x) (RTL8365MB_MIB_COUNTER_BASE + (_x))
@@ -1196,6 +1258,183 @@ static void rtl8365mb_port_stp_state_set(struct dsa_switch *ds, int port,
val << RTL8365MB_MSTI_CTRL_PORT_STATE_OFFSET(port));
}
+static int rtl8365mb_port_set_transparent(struct realtek_priv *priv,
+ int igr_port, int egr_port,
+ bool enable)
+{
+ dev_dbg(priv->dev, "%s transparent VLAN from %d to %d\n",
+ enable ? "Enable" : "Disable", igr_port, egr_port);
+
+ /* "Transparent" between the two ports means that packets forwarded by
+ * igr_port and egressed on egr_port will not be filtered by the usual
+ * VLAN membership settings.
+ */
+ return regmap_update_bits(priv->map,
+ RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG(egr_port),
+ BIT(igr_port), enable ? BIT(igr_port) : 0);
+}
+
+static int rtl8365mb_port_set_ingress_filtering(struct realtek_priv *priv,
+ int port, bool enable)
+{
+ /* Ingress filtering enabled: Discard VLAN-tagged frames if the port is
+ * not a member of the VLAN with which the packet is associated.
+ * Untagged packets will also be discarded unless the port has a PVID
+ * programmed. Priority-tagged frames are treated as untagged frames.
+ *
+ * Ingress filtering disabled: Accept all tagged and untagged frames.
+ */
+ return regmap_update_bits(priv->map, RTL8365MB_VLAN_INGRESS_REG,
+ RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_MASK(port),
+ enable ?
+ RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_MASK(port) :
+ 0);
+}
+
+static int rtl8365mb_port_vlan_filtering(struct dsa_switch *ds, int port,
+ bool vlan_filtering,
+ struct netlink_ext_ack *extack)
+{
+ struct realtek_priv *priv = ds->priv;
+ struct dsa_port *dp;
+ int ret;
+
+ dev_dbg(priv->dev, "port %d: %s VLAN filtering\n", port,
+ vlan_filtering ? "enable" : "disable");
+
+ /* (En|dis)able incoming packets filter, i.e. ignore VLAN membership */
+ dsa_switch_for_each_available_port(dp, ds) {
+ /* after considering port isolation, if not filtering
+ * allow forwarding from port to dp->index ignoring
+ * VLAN membership.
+ */
+ ret = rtl8365mb_port_set_transparent(priv, port, dp->index,
+ !vlan_filtering);
+ if (ret)
+ return ret;
+ }
+
+ /* If the port is not in the member set, the frame will be dropped */
+ return rtl8365mb_port_set_ingress_filtering(priv, port,
+ vlan_filtering);
+}
+
+static int rtl8365mb_port_vlan_add(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan,
+ struct netlink_ext_ack *extack)
+{
+ bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
+ bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
+ struct realtek_priv *priv = ds->priv;
+ int ret;
+
+ dev_dbg(priv->dev, "add VLAN %d on port %d, %s, %s\n",
+ vlan->vid, port, untagged ? "untagged" : "tagged",
+ pvid ? "PVID" : "no PVID");
+ /* add port to vlan4k. It knows nothing about PVID */
+ ret = rtl8365mb_vlan_4k_port_add(ds, port, vlan, extack);
+ if (ret)
+ return ret;
+
+ /* VlanMC knows nothing about untagged but it is required for PVID */
+ ret = rtl8365mb_vlan_mc_port_add(ds, port, vlan, extack);
+ if (ret)
+ goto undo_vlan_4k;
+
+ /* Set PVID if needed */
+ if (pvid) {
+ ret = rtl8365mb_vlan_pvid_port_add(ds, port, vlan, extack);
+ if (ret)
+ goto undo_vlan_mc;
+ }
+
+ return 0;
+
+undo_vlan_mc:
+ (void)rtl8365mb_vlan_mc_port_del(ds, port, vlan);
+
+undo_vlan_4k:
+ (void)rtl8365mb_vlan_4k_port_del(ds, port, vlan);
+ return ret;
+}
+
+static int rtl8365mb_port_vlan_del(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan)
+{
+ bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
+ bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
+ struct realtek_priv *priv = ds->priv;
+ int ret1, ret2, ret3;
+
+ dev_dbg(priv->dev, "del VLAN %d on port %d, %s, %s\n",
+ vlan->vid, port, untagged ? "untagged" : "tagged",
+ pvid ? "PVID" : "no PVID");
+
+ ret1 = rtl8365mb_vlan_pvid_port_del(ds, port, vlan);
+ ret2 = rtl8365mb_vlan_mc_port_del(ds, port, vlan);
+ ret3 = rtl8365mb_vlan_4k_port_del(ds, port, vlan);
+
+ return ret1 ?: ret2 ?: ret3;
+}
+
+static int
+rtl8365mb_port_set_vlan_egress_mode(struct realtek_priv *priv, int port,
+ enum rtl8365mb_vlan_egress_mode mode)
+{
+ u32 val;
+
+ val = FIELD_PREP(RTL8365MB_PORT_MISC_CFG_VLAN_EGRESS_MODE_MASK, mode);
+ return regmap_update_bits(priv->map,
+ RTL8365MB_PORT_MISC_CFG_REG(port),
+ RTL8365MB_PORT_MISC_CFG_VLAN_EGRESS_MODE_MASK, val);
+}
+
+/* VLAN support is always enabled in the switch.
+ *
+ * All ports starts with vlan-unaware state, letting non-bridge port forward
+ * to CPU.
+ *
+ */
+static int rtl8365mb_vlan_setup(struct dsa_switch *ds)
+{
+ struct realtek_priv *priv = ds->priv;
+ enum rtl8365mb_vlan_egress_mode mode;
+ struct dsa_port *dp;
+ int ret;
+
+ dsa_switch_for_each_user_port(dp, ds) {
+ /* Disable vlan-filtering for all ports */
+ ret = rtl8365mb_port_vlan_filtering(ds, dp->index, false, NULL);
+ if (ret) {
+ dev_err(priv->dev,
+ "Failed to disable vlan filtering on port %d\n",
+ dp->index);
+ return ret;
+ }
+
+ /* The switch default is RTL8365MB_VLAN_EGRESS_MODE_REAL_KEEP,
+ * that forwards the packet as it was received. However,
+ * different untag settings will require the switch to update
+ * the tag.
+ */
+ mode = RTL8365MB_VLAN_EGRESS_MODE_ORIGINAL;
+ ret = rtl8365mb_port_set_vlan_egress_mode(priv, dp->index,
+ mode);
+ if (ret) {
+ dev_err(priv->dev,
+ "Failed to set port %d egress mode\n",
+ dp->index);
+ return ret;
+ }
+ }
+
+ /* VLAN is always enabled. */
+ ret = regmap_update_bits(priv->map, RTL8365MB_VLAN_CTRL_REG,
+ RTL8365MB_VLAN_CTRL_EN_MASK,
+ FIELD_PREP(RTL8365MB_VLAN_CTRL_EN_MASK, 1));
+ return ret;
+}
+
static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port,
bool enable)
{
@@ -2014,6 +2253,20 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
if (ret)
goto out_teardown_irq;
+ /* vlan config will only be effective for ports with vlan filtering */
+ ds->configure_vlan_while_not_filtering = true;
+ /* Set up VLAN */
+ ret = rtl8365mb_vlan_setup(ds);
+ if (ret)
+ goto out_teardown_irq;
+
+ /* Set maximum packet length to 1536 bytes */
+ ret = regmap_update_bits(priv->map, RTL8365MB_CFG0_MAX_LEN_REG,
+ RTL8365MB_CFG0_MAX_LEN_MASK,
+ FIELD_PREP(RTL8365MB_CFG0_MAX_LEN_MASK, 1536));
+ if (ret)
+ goto out_teardown_irq;
+
ret = rtl83xx_setup_user_mdio(ds);
if (ret) {
dev_err(priv->dev, "could not set up MDIO bus\n");
@@ -2124,6 +2377,9 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
.teardown = rtl8365mb_teardown,
.phylink_get_caps = rtl8365mb_phylink_get_caps,
.port_stp_state_set = rtl8365mb_port_stp_state_set,
+ .port_vlan_add = rtl8365mb_port_vlan_add,
+ .port_vlan_del = rtl8365mb_port_vlan_del,
+ .port_vlan_filtering = rtl8365mb_port_vlan_filtering,
.get_strings = rtl8365mb_get_strings,
.get_ethtool_stats = rtl8365mb_get_ethtool_stats,
.get_sset_count = rtl8365mb_get_sset_count,
diff --git a/drivers/net/dsa/realtek/rtl8365mb_vlan.c b/drivers/net/dsa/realtek/rtl8365mb_vlan.c
new file mode 100644
index 000000000000..1ac36c06dcf7
--- /dev/null
+++ b/drivers/net/dsa/realtek/rtl8365mb_vlan.c
@@ -0,0 +1,805 @@
+// SPDX-License-Identifier: GPL-2.0
+/* VLAN configuration interface for the rtl8365mb switch family
+ *
+ * Copyright (C) 2022 Alvin Šipraga <alsi@bang-olufsen.dk>
+ *
+ * VLAN configuration takes place in two separate domains of the switch: the
+ * VLAN4k table and the VLAN membership configuration (MC) database. While the
+ * VLAN4k table is exhaustive and can be fully populated with 4096 VLAN
+ * configurations, the same does not hold for the VLAN membership configuration
+ * database, which is limited to 32 entries.
+ *
+ * The switch will normally only use the VLAN4k table when making forwarding
+ * decisions. The VLAN membership configuration database is a vestigial ASIC
+ * design and is only used for a few specific features in the rtl8365mb
+ * family. This means that the limit of 32 entries should not hinder us in
+ * programming a huge number of VLANs into the switch.
+ *
+ * One necessary use of the VLAN membership configuration database is for the
+ * programming of a port-based VLAN ID (PVID). The PVID is programmed on a
+ * per-port basis via register field, which refers to a specific VLAN membership
+ * configuration via an index 0~31. In order to maintain coherent behaviour on a
+ * port with a PVID, it is necessary to keep the VLAN configuration synchronized
+ * between the VLAN4k table and the VLAN membership configuration database.
+ *
+ * Since VLAN membership configs are a scarce resource, it will only be used
+ * when strictly needed (i.e. a VLAN with members using PVID). Otherwise, the
+ * VLAN4k will be enough.
+ *
+ * With some exceptions, the entries in both the VLAN4k table and the VLAN
+ * membership configuration database offer the same configuration options. The
+ * differences are as follows:
+ *
+ * 1. VLAN4k entries can specify whether to use Independent or Shared VLAN
+ * Learning (IVL or SVL respectively). VLAN membership config entries
+ * cannot. This underscores the fact that VLAN membership configs are not
+ * involved in the learning process of the ASIC.
+ *
+ * 2. VLAN membership config entries use an "enhanced VLAN ID" (efid), which has
+ * a range 0~8191 compared with the standard 0~4095 range of the VLAN4k
+ * table. This underscores the fact that VLAN membership configs can be used
+ * to group ports on a layer beyond the standard VLAN configuration, which
+ * may be useful for ACL rules which specify alternative forwarding
+ * decisions.
+ *
+ * VLANMC index 0 is reserved as a neutral PVID, used for vlan-unaware ports.
+ *
+ */
+
+#include "rtl8365mb_vlan.h"
+#include "rtl8365mb_table.h"
+#include <linux/if_bridge.h>
+#include <linux/regmap.h>
+
+/* CVLAN (i.e. VLAN4k) table entry layout, u16[3] */
+#define RTL8365MB_CVLAN_ENTRY_SIZE 3 /* 48-bits */
+#define RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK GENMASK(7, 0)
+#define RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK GENMASK(15, 8)
+#define RTL8365MB_CVLAN_ENTRY_D1_FID_MASK GENMASK(3, 0)
+#define RTL8365MB_CVLAN_ENTRY_D1_VBPEN_MASK GENMASK(4, 4)
+#define RTL8365MB_CVLAN_ENTRY_D1_VBPRI_MASK GENMASK(7, 5)
+#define RTL8365MB_CVLAN_ENTRY_D1_ENVLANPOL_MASK GENMASK(8, 8)
+#define RTL8365MB_CVLAN_ENTRY_D1_METERIDX_MASK GENMASK(13, 9)
+#define RTL8365MB_CVLAN_ENTRY_D1_IVL_SVL_MASK GENMASK(14, 14)
+/* extends RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK */
+#define RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK GENMASK(2, 0)
+/* extends RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK */
+#define RTL8365MB_CVLAN_ENTRY_D2_UNTAG_EXT_MASK GENMASK(5, 3)
+/* extends RTL8365MB_CVLAN_ENTRY_D1_METERIDX_MASK */
+#define RTL8365MB_CVLAN_ENTRY_D2_METERIDX_EXT_MASK GENMASK(6, 6)
+
+/* VLAN member configuration registers 0~31, u16[3] */
+#define RTL8365MB_VLAN_MC_BASE 0x0728
+#define RTL8365MB_VLAN_MC_ENTRY_SIZE 4 /* 64-bit */
+#define RTL8365MB_VLAN_MC_REG(index) \
+ (RTL8365MB_VLAN_MC_BASE + \
+ (RTL8365MB_VLAN_MC_ENTRY_SIZE * (index)))
+#define RTL8365MB_VLAN_MC_D0_MBR_MASK GENMASK(10, 0)
+#define RTL8365MB_VLAN_MC_D1_FID_MASK GENMASK(3, 0)
+
+#define RTL8365MB_VLAN_MC_D2_VBPEN_MASK GENMASK(0, 0)
+#define RTL8365MB_VLAN_MC_D2_VBPRI_MASK GENMASK(3, 1)
+#define RTL8365MB_VLAN_MC_D2_ENVLANPOL_MASK GENMASK(4, 4)
+#define RTL8365MB_VLAN_MC_D2_METERIDX_MASK GENMASK(10, 5)
+#define RTL8365MB_VLAN_MC_D3_EVID_MASK GENMASK(12, 0)
+
+/* Some limits for VLAN4k/VLAN membership config entries */
+#define RTL8365MB_PRIORITYMAX 7
+#define RTL8365MB_FIDMAX 15
+#define RTL8365MB_METERMAX 63
+#define RTL8365MB_VLAN_MCMAX 31
+
+/* RTL8367S supports 4k vlans (vid<=4095) and 32 enhanced vlans
+ * for VIDs up to 8191
+ */
+#define RTL8365MB_MAX_4K_VID 0x0FFF /* 4095 */
+#define RTL8365MB_MAX_MC_VID 0x1FFF /* 8191 */
+
+ /* Port-based VID registers 0~5 - each one holds an MC index for two ports */
+#define RTL8365MB_VLAN_PVID_CTRL_BASE 0x0700
+#define RTL8365MB_VLAN_PVID_CTRL_REG(_p) \
+ (RTL8365MB_VLAN_PVID_CTRL_BASE + ((_p) >> 1))
+#define RTL8365MB_VLAN_PVID_CTRL_PORT0_MCIDX_MASK 0x001F
+#define RTL8365MB_VLAN_PVID_CTRL_PORT1_MCIDX_MASK 0x1F00
+#define RTL8365MB_VLAN_PVID_CTRL_PORT_MCIDX_OFFSET(_p) \
+ (((_p) & 1) << 3)
+#define RTL8365MB_VLAN_PVID_CTRL_PORT_MCIDX_MASK(_p) \
+ (0x1F << RTL8365MB_VLAN_PVID_CTRL_PORT_MCIDX_OFFSET(_p))
+
+/* Frame type filtering registers */
+#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_BASE 0x07aa
+#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_REG(port) \
+ (RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_BASE + ((port) >> 3))
+/* required as FIELD_PREP cannot use non-constant masks */
+#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_MASK(port) \
+ (0x3 << RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_OFFSET(port))
+#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_OFFSET(port) \
+ (((port) & 0x7) << 1)
+
+/**
+ * struct rtl8365mb_vlan4k - VLAN4k table entry
+ * @vid: VLAN ID (0~4095)
+ * @member: port mask of ports in this VLAN
+ * @untag: port mask of ports which untag on egress
+ * @fid: filter ID - only used with SVL (unused)
+ * @priority: priority classification (unused)
+ * @priority_en: enable priority (unused)
+ * @policing_en: enable policing (unused)
+ * @ivl_en: enable IVL instead of default SVL
+ * @meteridx: metering index (unused)
+ *
+ * This structure is used to get/set entries in the VLAN4k table. The
+ * VLAN4k table dictates the VLAN configuration for the switch for the
+ * vast majority of features.
+ */
+struct rtl8365mb_vlan4k {
+ u16 vid;
+ u16 member;
+ u16 untag;
+ u8 fid : 4;
+ u8 priority : 3;
+ u8 priority_en : 1;
+ u8 policing_en : 1;
+ u8 ivl_en : 1;
+ u8 meteridx : 6;
+};
+
+/**
+ * struct rtl8365mb_vlanmc - VLAN membership config
+ * @evid: Enhanced VLAN ID (0~8191)
+ * @member: port mask of ports in this VLAN
+ * @fid: filter ID - only used with SVL (unused)
+ * @priority: priority classification (unused)
+ * @priority_en: enable priority (unused)
+ * @policing_en: enable policing (unused)
+ * @meteridx: metering index (unused)
+ *
+ * This structure is used to get/set entries in the VLAN membership
+ * configuration database. This feature is largely vestigial, but
+ * still needed for at least the following features:
+ * - PVID configuration
+ * - ACL configuration
+ * - selection of VLAN by the CPU tag when VSEL=1, although the switch
+ * can also select VLAN based on the VLAN tag if VSEL=0
+ *
+ * This is a low-level structure and it is recommended to interface with
+ * the VLAN membership config database via &struct rtl8365mb_vlanmc_entry.
+ */
+struct rtl8365mb_vlanmc {
+ u16 evid;
+ u16 member;
+ u8 fid : 4;
+ u8 priority : 3;
+ u8 priority_en : 1;
+ u8 policing_en : 1;
+ u8 meteridx : 6;
+};
+
+enum rtl8365mb_frame_ingress {
+ RTL8365MB_FRAME_TYPE_ANY_FRAME = 0,
+ RTL8365MB_FRAME_TYPE_TAGGED_ONLY,
+ RTL8365MB_FRAME_TYPE_UNTAGGED_ONLY,
+};
+
+static int rtl8365mb_vlan_4k_read(struct realtek_priv *priv, u16 vid,
+ struct rtl8365mb_vlan4k *vlan4k)
+{
+ u16 data[RTL8365MB_CVLAN_ENTRY_SIZE];
+ int ret;
+
+ ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_CVLAN,
+ RTL8365MB_TABLE_OP_READ, &vid, 0, 0,
+ data, ARRAY_SIZE(data));
+ if (ret)
+ return ret;
+
+ /* Unpack table entry */
+ memset(vlan4k, 0, sizeof(*vlan4k));
+ vlan4k->vid = vid;
+ vlan4k->member =
+ FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, data[0]) |
+ (FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2])
+ << FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK));
+ vlan4k->untag =
+ FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK, data[0]) |
+ (FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_UNTAG_EXT_MASK, data[2])
+ << FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK));
+ vlan4k->fid = FIELD_GET(RTL8365MB_CVLAN_ENTRY_D1_FID_MASK, data[1]);
+ vlan4k->priority_en =
+ FIELD_GET(RTL8365MB_CVLAN_ENTRY_D1_VBPEN_MASK, data[1]);
+ vlan4k->priority =
+ FIELD_GET(RTL8365MB_CVLAN_ENTRY_D1_VBPRI_MASK, data[1]);
+ vlan4k->policing_en =
+ FIELD_GET(RTL8365MB_CVLAN_ENTRY_D1_ENVLANPOL_MASK, data[1]);
+ vlan4k->meteridx =
+ FIELD_GET(RTL8365MB_CVLAN_ENTRY_D1_METERIDX_MASK, data[1]) |
+ (FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_METERIDX_EXT_MASK, data[2])
+ << FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D1_METERIDX_MASK));
+ vlan4k->ivl_en =
+ FIELD_GET(RTL8365MB_CVLAN_ENTRY_D1_IVL_SVL_MASK, data[1]);
+
+ return 0;
+}
+
+static int rtl8365mb_vlan_4k_write(struct realtek_priv *priv,
+ const struct rtl8365mb_vlan4k *vlan4k)
+{
+ u16 data[RTL8365MB_CVLAN_ENTRY_SIZE] = { 0 };
+ u16 val;
+ u16 vid;
+
+ if (vlan4k->fid > RTL8365MB_FIDMAX ||
+ vlan4k->priority > RTL8365MB_PRIORITYMAX ||
+ vlan4k->meteridx > RTL8365MB_METERMAX)
+ return -EINVAL;
+
+ /* Pack table entry value */
+ data[0] |=
+ FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, vlan4k->member);
+ data[0] |=
+ FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK, vlan4k->untag);
+
+ data[1] |= FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D1_FID_MASK, vlan4k->fid);
+ data[1] |= FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D1_VBPEN_MASK,
+ vlan4k->priority_en);
+ data[1] |= FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D1_VBPRI_MASK,
+ vlan4k->priority);
+ data[1] |= FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D1_ENVLANPOL_MASK,
+ vlan4k->policing_en);
+ data[1] |= FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D1_METERIDX_MASK,
+ vlan4k->meteridx);
+ data[1] |= FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D1_IVL_SVL_MASK,
+ vlan4k->ivl_en);
+
+ data[2] |= FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK,
+ vlan4k->member >>
+ FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK));
+ data[2] |= FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D2_UNTAG_EXT_MASK,
+ vlan4k->untag >>
+ FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK));
+ val = FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D2_METERIDX_EXT_MASK,
+ vlan4k->meteridx >>
+ FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D1_METERIDX_MASK));
+ data[2] |= val;
+
+ vid = vlan4k->vid;
+ return rtl8365mb_table_query(priv, RTL8365MB_TABLE_CVLAN,
+ RTL8365MB_TABLE_OP_WRITE, &vid, 0, 0,
+ data, ARRAY_SIZE(data));
+}
+
+#define RTL_VLAN_ERR(msg) \
+ do { \
+ const char *__msg = (msg); \
+ \
+ if (extack) \
+ NL_SET_ERR_MSG_FMT_MOD(extack, "%s", __msg); \
+ dev_err(priv->dev, "%s", __msg); \
+ } while (0)
+
+static int
+rtl8365mb_vlan_4k_port_set(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan,
+ struct netlink_ext_ack *extack,
+ bool include)
+{
+ struct realtek_priv *priv = ds->priv;
+ struct rtl8365mb_vlan4k vlan4k = {0};
+ int ret;
+
+ dev_dbg(priv->dev, "%s VLAN %d 4K on port %d\n",
+ include ? "add" : "del",
+ vlan->vid, port);
+
+ if (vlan->vid > RTL8365MB_MAX_4K_VID) {
+ RTL_VLAN_ERR("VLAN ID greater than "
+ __stringify(RTL8365MB_MAX_4K_VID));
+ return -EINVAL;
+ }
+
+ ret = rtl8365mb_vlan_4k_read(priv, vlan->vid, &vlan4k);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to read VLAN 4k table");
+ return ret;
+ }
+
+ if (include)
+ vlan4k.member |= BIT(port);
+ else
+ vlan4k.member &= ~BIT(port);
+
+ if (include && (vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED))
+ vlan4k.untag |= BIT(port);
+ else
+ vlan4k.untag &= ~BIT(port);
+ vlan4k.ivl_en = true; /* always use Independent VLAN Learning */
+
+ ret = rtl8365mb_vlan_4k_write(priv, &vlan4k);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to write VLAN 4k table");
+ return ret;
+ }
+
+ return 0;
+}
+
+int rtl8365mb_vlan_4k_port_add(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan,
+ struct netlink_ext_ack *extack)
+{
+ return rtl8365mb_vlan_4k_port_set(ds, port, vlan, extack, true);
+}
+
+int rtl8365mb_vlan_4k_port_del(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan)
+{
+ return rtl8365mb_vlan_4k_port_set(ds, port, vlan, NULL, false);
+}
+
+/**
+ * rtl8365mb_vlan_mc_find() - find VLANMC index by VID or the first free index
+ *
+ * @priv: realtek_priv pointer
+ * @vid: VLAN ID
+ * @index: found index
+ * @first_free: found free index
+ *
+ * If a VLAN MC entry using @vid was found, @index will return the matched index
+ * and @first_free is undefined. If not found, @index will return 0 and
+ * @first_free will return the first found free index in VLAN MC or 0 if the
+ * table is full.
+ *
+ * Although 0 is a valid VLAN MC index, it is reserved for ports without PVID,
+ * including standalone, non-member ports.
+ *
+ * Both @index and @first_free will be in the * 1..@RTL8365MB_VLAN_MCMAX range.
+ *
+ * Return: Returns 0 on success, a negative error on failure.
+ *
+ */
+static int rtl8365mb_vlan_mc_find(struct realtek_priv *priv, u16 vid,
+ u8 *index, u8 *first_free)
+{
+ u32 vlan_entry_d3;
+ u8 vlanmc_idx;
+ u16 evid;
+ int ret;
+
+ if (!index)
+ return -EINVAL;
+ if (!first_free)
+ return -EINVAL;
+ if (!vid)
+ return -EINVAL;
+
+ *index = 0;
+ *first_free = 0;
+
+ /* look for existing entry or an empty one */
+ /* vlanmc index 0 is reserved as a neutral PVID value.
+ * Non-PVID ports can still reach the CPU via VLAN
+ * transparent mode.
+ **/
+ for (vlanmc_idx = 1; vlanmc_idx <= RTL8365MB_VLAN_MCMAX; vlanmc_idx++) {
+ /* just read the 4th word, where the evid is */
+ ret = regmap_read(priv->map,
+ RTL8365MB_VLAN_MC_REG(vlanmc_idx) + 3,
+ &vlan_entry_d3);
+ if (ret)
+ return ret;
+
+ evid = FIELD_GET(RTL8365MB_VLAN_MC_D3_EVID_MASK, vlan_entry_d3);
+
+ if (evid == vid) {
+ *index = vlanmc_idx;
+ return 0;
+ }
+
+ if (evid == 0x0 && *first_free < 1)
+ *first_free = vlanmc_idx;
+ }
+ return 0;
+}
+
+static int rtl8365mb_vlan_port_set_pvid(struct realtek_priv *priv,
+ int port, u16 vlanmc_idx)
+{
+ int ret;
+ u32 val;
+
+ val = vlanmc_idx << RTL8365MB_VLAN_PVID_CTRL_PORT_MCIDX_OFFSET(port);
+ ret = regmap_update_bits(priv->map,
+ RTL8365MB_VLAN_PVID_CTRL_REG(port),
+ RTL8365MB_VLAN_PVID_CTRL_PORT_MCIDX_MASK(port),
+ val);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
+static int rtl8365mb_vlan_port_get_pvid(struct realtek_priv *priv,
+ int port, u8 *vlanmc_idx)
+{
+ u32 data;
+ int ret;
+
+ ret = regmap_read(priv->map, RTL8365MB_VLAN_PVID_CTRL_REG(port), &data);
+ if (ret)
+ return ret;
+
+ *vlanmc_idx = (data & RTL8365MB_VLAN_PVID_CTRL_PORT_MCIDX_MASK(port))
+ >> RTL8365MB_VLAN_PVID_CTRL_PORT_MCIDX_OFFSET(port);
+
+ return 0;
+}
+
+/**
+ * rtl8365mb_vlan_mc_pvid_members() - Get a bitmap of vlan PVID members
+ *
+ * @ds: DSA switch
+ * @vlanmc_idx: the index of a VLAN in VLAN MC table
+ * @members: the returned bitmap of members that have PVID status
+ *
+ * This function iterates over DSA ports and creates a bitmap representation of
+ * those ports that have PVID pointing to this VLAN (identified by its table
+ * index and not VID). If you need to get the table index from VID, see
+ * rtl8365mb_vlan_mc_find()
+ *
+ * Return: Returns 0 on success, a negative error on failure.
+ **/
+static int rtl8365mb_vlan_mc_pvid_members(struct dsa_switch *ds,
+ u8 vlanmc_idx, u16 *members)
+{
+ struct realtek_priv *priv = ds->priv;
+ struct dsa_port *dp;
+ u8 _vlanmc_idx;
+ int ret;
+
+ if (!members)
+ return -EINVAL;
+
+ *members = 0;
+
+ dsa_switch_for_each_port(dp, ds) {
+ ret = rtl8365mb_vlan_port_get_pvid(priv, dp->index,
+ &_vlanmc_idx);
+ if (ret)
+ return ret;
+
+ if (_vlanmc_idx == vlanmc_idx)
+ *members |= BIT(dp->index);
+ }
+
+ return 0;
+}
+
+static int
+rtl8365mb_vlan_port_set_framefilter(struct realtek_priv *priv,
+ int port,
+ enum rtl8365mb_frame_ingress accepted_frame)
+{
+ /* Even if ACCEPT_FRAME_TYPE_ANY, the switch will still check if the
+ * port is a member of vlan PVID
+ */
+ accepted_frame = accepted_frame
+ << RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_OFFSET(port);
+
+ return regmap_update_bits(priv->map,
+ RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_REG(port),
+ RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_MASK(port),
+ accepted_frame);
+}
+
+static int rtl8365mb_vlan_mc_read(struct realtek_priv *priv, u32 index,
+ struct rtl8365mb_vlanmc *vlanmc)
+{
+ u16 data[RTL8365MB_VLAN_MC_ENTRY_SIZE];
+ int ret;
+
+ if (index > RTL8365MB_VLAN_MCMAX)
+ return -EINVAL;
+
+ ret = regmap_bulk_read(priv->map, RTL8365MB_VLAN_MC_REG(index), &data,
+ RTL8365MB_VLAN_MC_ENTRY_SIZE);
+ if (ret)
+ return ret;
+
+ vlanmc->member = FIELD_GET(RTL8365MB_VLAN_MC_D0_MBR_MASK, data[0]);
+ vlanmc->fid = FIELD_GET(RTL8365MB_VLAN_MC_D1_FID_MASK, data[1]);
+ vlanmc->priority = FIELD_GET(RTL8365MB_VLAN_MC_D2_VBPRI_MASK, data[2]);
+ vlanmc->evid = FIELD_GET(RTL8365MB_VLAN_MC_D3_EVID_MASK, data[3]);
+
+ return 0;
+}
+
+int rtl8365mb_vlan_pvid_port_add(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan,
+ struct netlink_ext_ack *extack)
+{
+ bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
+ enum rtl8365mb_frame_ingress accepted_frame;
+ struct realtek_priv *priv = ds->priv;
+ u8 _unused_first_free_idx;
+ u8 vlanmc_idx;
+ int ret;
+
+ if (!pvid)
+ return 0;
+
+ /* look for existing entry */
+ ret = rtl8365mb_vlan_mc_find(priv, vlan->vid, &vlanmc_idx,
+ &_unused_first_free_idx);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to find a VLAN MC table index");
+ return ret;
+ }
+
+ if (!vlanmc_idx) {
+ RTL_VLAN_ERR("VLAN should already exist in VLAN MC");
+ return ret;
+ }
+
+ ret = rtl8365mb_vlan_port_set_pvid(priv, port, vlanmc_idx);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to set port PVID");
+ return ret;
+ }
+
+ /* Changing accept frame is what enables PVID (if not enabled before) */
+ accepted_frame = RTL8365MB_FRAME_TYPE_ANY_FRAME;
+ ret = rtl8365mb_vlan_port_set_framefilter(priv, port, accepted_frame);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to set port frame filter");
+ return ret;
+ }
+
+ return 0;
+}
+
+int rtl8365mb_vlan_pvid_port_del(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan)
+{
+ enum rtl8365mb_frame_ingress accepted_frame;
+ struct netlink_ext_ack *extack = NULL;
+ struct realtek_priv *priv = ds->priv;
+ struct rtl8365mb_vlanmc vlanmc = {0};
+ u8 vlanmc_idx;
+ int ret;
+
+ ret = rtl8365mb_vlan_port_get_pvid(priv, port, &vlanmc_idx);
+ if (ret)
+ return ret;
+
+ /* Port is not using PVID. Nothing to remove. */
+ if (!vlanmc_idx)
+ return 0;
+
+ ret = rtl8365mb_vlan_mc_read(priv, vlanmc_idx, &vlanmc);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to read VLAN MC table");
+ return ret;
+ }
+
+ /* We are leaving a non PVID vlan, Nothing to remove. */
+ if (vlanmc.evid != vlan->vid)
+ return 0;
+
+ /* Changing accept frame is what really removes PVID */
+ accepted_frame = RTL8365MB_FRAME_TYPE_TAGGED_ONLY;
+ ret = rtl8365mb_vlan_port_set_framefilter(priv, port, accepted_frame);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to set port frame filter");
+ return ret;
+ }
+
+ ret = rtl8365mb_vlan_port_set_pvid(priv, port, 0);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to set port PVID to 0");
+ return ret;
+ }
+
+ return 0;
+}
+
+static int rtl8365mb_vlan_mc_write(struct realtek_priv *priv, u32 index,
+ const struct rtl8365mb_vlanmc *vlanmc)
+{
+ u16 data[4] = { 0 };
+ int ret;
+
+ if (index > RTL8365MB_VLAN_MCMAX ||
+ vlanmc->fid > RTL8365MB_FIDMAX ||
+ vlanmc->priority > RTL8365MB_PRIORITYMAX ||
+ vlanmc->meteridx > RTL8365MB_METERMAX)
+ return -EINVAL;
+
+ data[0] |= FIELD_PREP(RTL8365MB_VLAN_MC_D0_MBR_MASK, vlanmc->member);
+ data[1] |= FIELD_PREP(RTL8365MB_VLAN_MC_D1_FID_MASK, vlanmc->fid);
+ data[2] |= FIELD_PREP(RTL8365MB_VLAN_MC_D2_METERIDX_MASK,
+ vlanmc->meteridx);
+ data[2] |= FIELD_PREP(RTL8365MB_VLAN_MC_D2_ENVLANPOL_MASK,
+ vlanmc->policing_en);
+ data[2] |=
+ FIELD_PREP(RTL8365MB_VLAN_MC_D2_VBPRI_MASK, vlanmc->priority);
+ data[2] |= FIELD_PREP(RTL8365MB_VLAN_MC_D2_VBPEN_MASK,
+ vlanmc->priority_en);
+ data[3] |= FIELD_PREP(RTL8365MB_VLAN_MC_D3_EVID_MASK, vlanmc->evid);
+
+ ret = regmap_bulk_write(priv->map, RTL8365MB_VLAN_MC_REG(index), &data,
+ RTL8365MB_VLAN_MC_ENTRY_SIZE);
+
+ return ret;
+}
+
+static int rtl8365mb_vlan_mc_erase(struct realtek_priv *priv, u32 index)
+{
+ u16 data[4] = { 0 };
+ int ret;
+
+ if (index > RTL8365MB_VLAN_MCMAX)
+ return -EINVAL;
+
+ ret = regmap_bulk_write(priv->map, RTL8365MB_VLAN_MC_REG(index), &data,
+ RTL8365MB_VLAN_MC_ENTRY_SIZE);
+
+ return ret;
+}
+
+/** rtl8365mb_vlanmc_port_set() - include or exclude a port from vlanMC
+ * @ds: dsa switch
+ * @port: the port number
+ * @vlan: the vlan to include/exclude @port
+ * @extack: optional extack to return errors
+ * @include: whether to include or exclude @port
+ *
+ * This function is used to include/exclude ports to the vlanMC table.
+ *
+ * VlanMC stands for VLAN membership config and it is used exclusively for
+ * PVID. If @vlan members are not using PVID, this function will either
+ * remove or not create a new vlanMC entry.
+ *
+ * vlanMC members are kept in sync with vlan4k, although the switch only
+ * checks membership in vlan4k table.
+ *
+ * Port PVID and accepted frame type are updated as well.
+ *
+ * Return: Returns 0 on success, a negative error on failure.
+ *
+ */
+static
+int rtl8365mb_vlan_mc_port_set(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan,
+ struct netlink_ext_ack *extack,
+ bool include)
+{
+ bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
+ struct realtek_priv *priv = ds->priv;
+ struct rtl8365mb_vlan4k vlan4k = {0};
+ struct rtl8365mb_vlanmc vlanmc = {0};
+ u16 pvid_members = 0;
+ u8 first_unused = 0;
+ u8 vlanmc_idx = 0;
+ int ret;
+
+ dev_dbg(priv->dev, "%s VLAN %d MC on port %d\n",
+ include ? "add" : "del",
+ vlan->vid, port);
+
+ if (vlan->vid > RTL8365MB_MAX_MC_VID) {
+ RTL_VLAN_ERR("VLAN ID greater than "
+ __stringify(RTL8365MB_MAX_MC_VID));
+ return -EINVAL;
+ }
+
+ /* look for existing entry or an empty slot */
+ ret = rtl8365mb_vlan_mc_find(priv, vlan->vid, &vlanmc_idx,
+ &first_unused);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to find a VLAN MC table index");
+ return ret;
+ }
+
+ if (vlanmc_idx) {
+ ret = rtl8365mb_vlan_mc_read(priv, vlanmc_idx, &vlanmc);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to read VLAN MC table");
+ return ret;
+ }
+ } else if (include) {
+ /* for now, vlan_mc is only required for PVID. Defer allocation
+ * until at least one port uses PVID.
+ */
+ if (!pvid) {
+ dev_dbg(priv->dev,
+ "Not creating VlanMC for vlan %d until a port uses PVID (%d does not)\n",
+ vlan->vid, port);
+ return 0;
+ }
+
+ if (!first_unused) {
+ RTL_VLAN_ERR("All VLAN MC entries ("
+ __stringify(RTL8365MB_VLAN_MCMAX + 1)
+ ") are in use.");
+ return -E2BIG;
+ }
+
+ /* Retrieve vlan4k members as we might have deferred VlanMC
+ * before.
+ */
+ if (vlan->vid <= RTL8365MB_MAX_4K_VID) {
+ ret = rtl8365mb_vlan_4k_read(priv, vlan->vid, &vlan4k);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to read VLAN 4k table");
+ return ret;
+ }
+ }
+
+ vlanmc_idx = first_unused;
+ vlanmc.evid = vlan->vid;
+
+ /* for new vlan_mc, sync current vlan4k members,
+ * although only vlan4k members matter.
+ */
+ vlanmc.member |= vlan4k.member;
+ } else /* excluding and VLANMC not found */ {
+ return 0;
+ }
+
+ ret = rtl8365mb_vlan_mc_pvid_members(ds, vlanmc_idx,
+ &pvid_members);
+ if (ret) {
+ RTL_VLAN_ERR("Failed to read VLANMC PVID members");
+ return ret;
+ }
+ dev_dbg(priv->dev,
+ "VLAN %d (idx: %d) PVID curr members: %08x\n",
+ vlan->vid, vlanmc_idx, pvid_members);
+
+ /* here we either have an existing VLANMC (with PVID members) or the
+ * added port is using this VLAN as PVID
+ */
+ if (include) {
+ vlanmc.member |= BIT(port);
+ if (pvid)
+ pvid_members |= BIT(port);
+ } else {
+ vlanmc.member &= ~BIT(port);
+ pvid_members &= ~BIT(port);
+ }
+
+ /* just like we don't need to create a VLAN_MC when there is no port
+ * using it as PVID, we can erase it when there is no more port using
+ * it as PVID.
+ */
+ if (!pvid_members) {
+ dev_dbg(priv->dev,
+ "Clearing VlanMC index %d previously used by VID %d\n",
+ vlanmc_idx, vlan->vid);
+ ret = rtl8365mb_vlan_mc_erase(priv, vlanmc_idx);
+ } else {
+ dev_dbg(priv->dev,
+ "Saving VlanMC index %d with VID %d\n",
+ vlanmc_idx, vlan->vid);
+ ret = rtl8365mb_vlan_mc_write(priv, vlanmc_idx, &vlanmc);
+ }
+ if (ret) {
+ RTL_VLAN_ERR("Failed to write vlan MC entry");
+ return ret;
+ }
+
+ return 0;
+}
+
+int rtl8365mb_vlan_mc_port_add(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan,
+ struct netlink_ext_ack *extack)
+{
+ return rtl8365mb_vlan_mc_port_set(ds, port, vlan, extack, true);
+}
+
+int rtl8365mb_vlan_mc_port_del(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan)
+{
+ return rtl8365mb_vlan_mc_port_set(ds, port, vlan, NULL, false);
+}
diff --git a/drivers/net/dsa/realtek/rtl8365mb_vlan.h b/drivers/net/dsa/realtek/rtl8365mb_vlan.h
new file mode 100644
index 000000000000..bd0cf4c804c8
--- /dev/null
+++ b/drivers/net/dsa/realtek/rtl8365mb_vlan.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* VLAN configuration interface for the rtl8365mb switch family
+ *
+ * Copyright (C) 2022 Alvin Šipraga <alsi@bang-olufsen.dk>
+ *
+ */
+
+#ifndef _REALTEK_RTL8365MB_VLAN_H
+#define _REALTEK_RTL8365MB_VLAN_H
+
+#include <linux/types.h>
+
+#include "realtek.h"
+
+int rtl8365mb_vlan_4k_port_add(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan,
+ struct netlink_ext_ack *extack);
+int rtl8365mb_vlan_4k_port_del(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan);
+int rtl8365mb_vlan_mc_port_add(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan,
+ struct netlink_ext_ack *extack);
+int rtl8365mb_vlan_mc_port_del(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan);
+int rtl8365mb_vlan_pvid_port_add(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan,
+ struct netlink_ext_ack *extack);
+int rtl8365mb_vlan_pvid_port_del(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_vlan *vlan);
+#endif /* _REALTEK_RTL8365MB_VLAN_H */
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [net-next PATCH 07/10] net: dsa: realtek: rtl8365mb: add port_bridge_{join,leave}
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
` (5 preceding siblings ...)
2026-03-31 23:00 ` [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support Luiz Angelo Daros de Luca
@ 2026-03-31 23:00 ` Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 08/10] net: dsa: realtek: rtl8365mb: add FDB support Luiz Angelo Daros de Luca
` (2 subsequent siblings)
9 siblings, 0 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
From: Alvin Šipraga <alsi@bang-olufsen.dk>
Implement hardware offloading of bridge functionality. This is achieved
by using the per-port isolation registers, which contain a forwarding
port mask. The switch will refuse to forward packets ingressed on a
given port to a port which is not in its forwarding mask.
For each bridge that is offloaded, use the DSA-provided bridge number
for the Extended Filtering ID (EFID). When using Independent VLAN
Learning (IVL), the forwarding database is keyed with the tuple
{VID, MAC, EFID}. There are 8 EFIDs available (0~7), but we reserve the
default EFID 0 for standalone ports where learning is disabled. This
fits nicely because DSA indexes the bridge number starting from 1.
Because of the limited number of EFIDs, we have to set the
max_num_bridges property of our switch to 7: we can't offload more than
that or we will fail to offer IVL as at least two bridges would end up
having to share an EFID.
Co-developed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
drivers/net/dsa/realtek/rtl8365mb_main.c | 123 ++++++++++++++++++++++++++++++-
1 file changed, 122 insertions(+), 1 deletion(-)
diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
index c604bd744d38..c955864f17b3 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_main.c
+++ b/drivers/net/dsa/realtek/rtl8365mb_main.c
@@ -285,6 +285,15 @@
(RTL8365MB_PORT_ISOLATION_REG_BASE + (_physport))
#define RTL8365MB_PORT_ISOLATION_MASK 0x07FF
+/* Extended filter ID registers - used to key forwarding database with IVL */
+#define RTL8365MB_EFID_MASK GENMASK(2, 0)
+#define RTL8365MB_PORT_EFID_REG_BASE 0x0A32
+#define RTL8365MB_PORT_EFID_REG(_p) \
+ (RTL8365MB_PORT_EFID_REG_BASE + ((_p) >> 2))
+#define RTL8365MB_PORT_EFID_OFFSET(_p) (((_p) & 0x3) << 2)
+#define RTL8365MB_PORT_EFID_MASK(_p) \
+ (RTL8365MB_EFID_MASK << RTL8365MB_PORT_EFID_OFFSET(_p))
+
/* MSTP port state registers - indexed by tree instance */
#define RTL8365MB_MSTI_CTRL_BASE 0x0A00
#define RTL8365MB_MSTI_CTRL_REG(_msti, _physport) \
@@ -1447,10 +1456,117 @@ static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port,
enable ? RTL8365MB_LEARN_LIMIT_MAX : 0);
}
+static int rtl8365mb_port_set_efid(struct realtek_priv *priv, int port,
+ u32 efid)
+{
+ return regmap_update_bits(priv->map, RTL8365MB_PORT_EFID_REG(port),
+ RTL8365MB_PORT_EFID_MASK(port),
+ efid << RTL8365MB_PORT_EFID_OFFSET(port));
+}
+
+/* Port isolation manipulation functions.
+ *
+ * The port isolation register controls the forwarding mask of a given
+ * port. The switch will not forward packets ingressed on a given port
+ * to ports which are not enabled in its forwarding mask.
+ *
+ * The port forwarding mask has the highest priority in forwarding
+ * decisions. The only exception to this rule is when the switch
+ * receives a packet on its CPU port with ALLOW=0. In that case the TX
+ * field of the CPU tag will override the forwarding port mask.
+ */
static int rtl8365mb_port_set_isolation(struct realtek_priv *priv, int port,
u32 mask)
{
- return regmap_write(priv->map, RTL8365MB_PORT_ISOLATION_REG(port), mask);
+ return regmap_write(priv->map, RTL8365MB_PORT_ISOLATION_REG(port),
+ mask);
+}
+
+static int rtl8365mb_port_add_isolation(struct realtek_priv *priv, int port,
+ u32 mask)
+{
+ return regmap_update_bits(priv->map, RTL8365MB_PORT_ISOLATION_REG(port),
+ mask, mask);
+}
+
+static int rtl8365mb_port_remove_isolation(struct realtek_priv *priv, int port,
+ u32 mask)
+{
+ return regmap_update_bits(priv->map, RTL8365MB_PORT_ISOLATION_REG(port),
+ mask, 0);
+}
+
+static int rtl8365mb_port_bridge_join(struct dsa_switch *ds, int port,
+ struct dsa_bridge bridge,
+ bool *tx_forward_offload,
+ struct netlink_ext_ack *extack)
+{
+ struct realtek_priv *priv = ds->priv;
+ u32 mask = 0;
+ int ret;
+ int i;
+
+ /* Add this port to the isolation group of every other port
+ * offloading this bridge.
+ */
+ for (i = 0; i < priv->num_ports; i++) {
+ /* Handle this port after */
+ if (i == port)
+ continue;
+
+ /* Skip ports that are not in this bridge */
+ if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
+ continue;
+
+ ret = rtl8365mb_port_add_isolation(priv, i, BIT(port));
+ if (ret)
+ return ret;
+
+ mask |= BIT(i);
+ }
+
+ /* Add those ports to the isolation group of this port */
+ ret = rtl8365mb_port_add_isolation(priv, port, mask);
+ if (ret)
+ return ret;
+
+ /* Use the bridge number as the EFID for this port */
+ ret = rtl8365mb_port_set_efid(priv, port, bridge.num);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
+static void rtl8365mb_port_bridge_leave(struct dsa_switch *ds, int port,
+ struct dsa_bridge bridge)
+{
+ struct realtek_priv *priv = ds->priv;
+ u32 mask = 0;
+ int i;
+
+ /* Remove this port from the isolation group of every other
+ * port offloading this bridge.
+ */
+ for (i = 0; i < priv->num_ports; i++) {
+ /* Handle this port after */
+ if (i == port)
+ continue;
+
+ /* Skip ports that are not in this bridge */
+ if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
+ continue;
+
+ rtl8365mb_port_remove_isolation(priv, i, BIT(port));
+
+ mask |= BIT(i);
+ }
+
+ /* Remove those ports from the isolation group of this port */
+ rtl8365mb_port_remove_isolation(priv, port, mask);
+
+ /* Revert to the default EFID 0 for standalone mode */
+ rtl8365mb_port_set_efid(priv, port, 0);
}
static int rtl8365mb_mib_counter_read(struct realtek_priv *priv, int port,
@@ -2253,6 +2369,9 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
if (ret)
goto out_teardown_irq;
+ /* The EFID is 3 bits, but EFID 0 is reserved for standalone ports */
+ ds->max_num_bridges = FIELD_MAX(RTL8365MB_EFID_MASK);
+
/* vlan config will only be effective for ports with vlan filtering */
ds->configure_vlan_while_not_filtering = true;
/* Set up VLAN */
@@ -2376,6 +2495,8 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
.setup = rtl8365mb_setup,
.teardown = rtl8365mb_teardown,
.phylink_get_caps = rtl8365mb_phylink_get_caps,
+ .port_bridge_join = rtl8365mb_port_bridge_join,
+ .port_bridge_leave = rtl8365mb_port_bridge_leave,
.port_stp_state_set = rtl8365mb_port_stp_state_set,
.port_vlan_add = rtl8365mb_port_vlan_add,
.port_vlan_del = rtl8365mb_port_vlan_del,
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [net-next PATCH 08/10] net: dsa: realtek: rtl8365mb: add FDB support
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
` (6 preceding siblings ...)
2026-03-31 23:00 ` [net-next PATCH 07/10] net: dsa: realtek: rtl8365mb: add port_bridge_{join,leave} Luiz Angelo Daros de Luca
@ 2026-03-31 23:00 ` Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 09/10] net: dsa: realtek: rtl8365mb: add bridge port flags Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 10/10] net: dsa: tag_rtl8_4: set KEEP flag Luiz Angelo Daros de Luca
9 siblings, 0 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
From: Alvin Šipraga <alsi@bang-olufsen.dk>
Add support for forwarding database operations, including unicast and
multicast entry handling as well as fast aging support.
The driver implements Independent VLAN Learning (IVL) by keying the
forwarding database with the {VID, MAC, EFID} tuple. The Extended
Filtering ID (EFID) is 3 bits wide, providing 8 unique filtering
domains. Since EFID 0 is reserved for standalone ports where learning is
disabled, the hardware is limited to offloading a maximum of 7 bridges.
The driver implements only the subset of L2 table accessors needed to
support these DSA operations. Other hardware capabilities are left
untouched and can be added incrementally if needed.
Co-developed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
drivers/net/dsa/realtek/Makefile | 1 +
drivers/net/dsa/realtek/rtl8365mb_l2.c | 465 +++++++++++++++++++++++++++++++
drivers/net/dsa/realtek/rtl8365mb_l2.h | 59 ++++
drivers/net/dsa/realtek/rtl8365mb_main.c | 197 ++++++++++++-
4 files changed, 721 insertions(+), 1 deletion(-)
diff --git a/drivers/net/dsa/realtek/Makefile b/drivers/net/dsa/realtek/Makefile
index b7fc4e852fd8..6c329e046d0b 100644
--- a/drivers/net/dsa/realtek/Makefile
+++ b/drivers/net/dsa/realtek/Makefile
@@ -19,3 +19,4 @@ obj-$(CONFIG_NET_DSA_REALTEK_RTL8365MB) += rtl8365mb.o
rtl8365mb-objs := rtl8365mb_main.o \
rtl8365mb_table.o \
rtl8365mb_vlan.o \
+ rtl8365mb_l2.o \
diff --git a/drivers/net/dsa/realtek/rtl8365mb_l2.c b/drivers/net/dsa/realtek/rtl8365mb_l2.c
new file mode 100644
index 000000000000..be5825f8fe7b
--- /dev/null
+++ b/drivers/net/dsa/realtek/rtl8365mb_l2.c
@@ -0,0 +1,465 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Forwarding and multicast database interface for the rtl8365mb switch family
+ *
+ * Copyright (C) 2022 Alvin Šipraga <alsi@bang-olufsen.dk>
+ */
+
+#include <linux/etherdevice.h>
+
+#include "rtl8365mb_l2.h"
+#include "rtl8365mb_table.h"
+#include <linux/regmap.h>
+
+#define RTL8365MB_L2_ENTRY_SIZE 6
+
+#define RTL8365MB_L2_UC_D0_MAC5_MASK GENMASK(7, 0)
+#define RTL8365MB_L2_UC_D0_MAC4_MASK GENMASK(15, 8)
+#define RTL8365MB_L2_UC_D1_MAC3_MASK GENMASK(7, 0)
+#define RTL8365MB_L2_UC_D1_MAC2_MASK GENMASK(15, 8)
+#define RTL8365MB_L2_UC_D2_MAC1_MASK GENMASK(7, 0)
+#define RTL8365MB_L2_UC_D2_MAC0_MASK GENMASK(15, 8)
+#define RTL8365MB_L2_UC_D3_VID_MASK GENMASK(11, 0)
+#define RTL8365MB_L2_UC_D3_IVL_MASK GENMASK(13, 13)
+#define RTL8365MB_L2_UC_D3_PORT_EXT_MASK GENMASK(15, 15)
+#define RTL8365MB_L2_UC_D4_EFID_MASK GENMASK(2, 0)
+#define RTL8365MB_L2_UC_D4_FID_MASK GENMASK(6, 3)
+#define RTL8365MB_L2_UC_D4_SA_PRI_MASK GENMASK(7, 7)
+#define RTL8365MB_L2_UC_D4_PORT_MASK GENMASK(10, 8)
+#define RTL8365MB_L2_UC_D4_AGE_MASK GENMASK(13, 11)
+#define RTL8365MB_L2_UC_D4_AUTH_MASK GENMASK(14, 14)
+#define RTL8365MB_L2_UC_D4_SA_BLOCK_MASK GENMASK(15, 15)
+#define RTL8365MB_L2_UC_D5_DA_BLOCK_MASK GENMASK(0, 0)
+#define RTL8365MB_L2_UC_D5_PRIORITY_MASK GENMASK(3, 1)
+#define RTL8365MB_L2_UC_D5_FWD_PRI_MASK GENMASK(4, 4)
+#define RTL8365MB_L2_UC_D5_STATIC_MASK GENMASK(5, 5)
+
+#define RTL8365MB_L2_MC_MAC5_MASK GENMASK(7, 0) /* D0 */
+#define RTL8365MB_L2_MC_MAC4_MASK GENMASK(15, 8) /* D0 */
+#define RTL8365MB_L2_MC_MAC3_MASK GENMASK(7, 0) /* D1 */
+#define RTL8365MB_L2_MC_MAC2_MASK GENMASK(15, 8) /* D1 */
+#define RTL8365MB_L2_MC_MAC1_MASK GENMASK(7, 0) /* D2 */
+#define RTL8365MB_L2_MC_MAC0_MASK GENMASK(15, 8) /* D2 */
+#define RTL8365MB_L2_MC_VID_MASK GENMASK(11, 0) /* D3 */
+#define RTL8365MB_L2_MC_IVL_MASK GENMASK(13, 13) /* D3 */
+#define RTL8365MB_L2_MC_MBR_EXT1_MASK GENMASK(15, 14) /* D3 */
+
+#define RTL8365MB_L2_MC_MBR_MASK GENMASK(7, 0) /* D4 */
+#define RTL8365MB_L2_MC_IGMPIDX_MASK GENMASK(15, 8) /* D4 */
+
+#define RTL8365MB_L2_MC_IGMP_ASIC_MASK GENMASK(0, 0) /* D5 */
+#define RTL8365MB_L2_MC_PRIORITY_MASK GENMASK(3, 1) /* D5 */
+#define RTL8365MB_L2_MC_FWD_PRI_MASK GENMASK(4, 4) /* D5 */
+#define RTL8365MB_L2_MC_STATIC_MASK GENMASK(5, 5) /* D5 */
+#define RTL8365MB_L2_MC_MBR_EXT2_MASK GENMASK(7, 7) /* D5 */
+
+/* Port flush command registers - writing a 1 to the port's MASK bit will
+ * initiate the flush procedure. Completion is signalled when the corresponding
+ * BUSY bit is 0.
+ */
+#define RTL8365MB_L2_FLUSH_PORT_REG 0x0A36
+#define RTL8365MB_L2_FLUSH_PORT_MASK_MASK GENMASK(7, 0)
+#define RTL8365MB_L2_FLUSH_PORT_BUSY_MASK GENMASK(15, 8)
+
+#define RTL8365MB_L2_FLUSH_PORT_EXT_REG 0x0A35
+#define RTL8365MB_L2_FLUSH_PORT_EXT_MASK_MASK GENMASK(2, 0)
+#define RTL8365MB_L2_FLUSH_PORT_EXT_BUSY_MASK GENMASK(5, 3)
+
+#define RTL8365MB_L2_FLUSH_CTRL1_REG 0x0A37
+#define RTL8365MB_L2_FLUSH_CTRL1_VID_MASK GENMASK(11, 0)
+#define RTL8365MB_L2_FLUSH_CTRL1_FID_MASK GENMASK(15, 12)
+
+#define RTL8365MB_L2_FLUSH_CTRL2_REG 0x0A38
+#define RTL8365MB_L2_FLUSH_CTRL2_MODE_MASK GENMASK(1, 0)
+#define RTL8365MB_L2_FLUSH_CTRL2_MODE_PORT 0
+#define RTL8365MB_L2_FLUSH_CTRL2_MODE_PORT_VID 1
+#define RTL8365MB_L2_FLUSH_CTRL2_MODE_PORT_FID 2
+#define RTL8365MB_L2_FLUSH_CTRL2_TYPE_MASK GENMASK(2, 2)
+#define RTL8365MB_L2_FLUSH_CTRL2_TYPE_DYNAMIC 0
+#define RTL8365MB_L2_FLUSH_CTRL2_TYPE_BOTH 0
+
+/* This flushes the entire LUT, reading it back it will turn 0 when the
+ * operation is complete
+ */
+#define RTL8365MB_L2_FLUSH_CTRL3_REG 0x0A39
+#define RTL8365MB_L2_FLUSH_CTRL3_MASK GENMASK(0, 0)
+
+struct rtl8365mb_l2_mc_key {
+ u8 mac_addr[ETH_ALEN];
+ union {
+ u16 vid; /* IVL */
+ u16 fid; /* SVL */
+ };
+ bool ivl;
+};
+
+struct rtl8365mb_l2_mc {
+ struct rtl8365mb_l2_mc_key key;
+ u16 member;
+ u8 priority;
+ u8 igmpidx;
+
+ bool is_static;
+ bool fwd_pri;
+ bool igmp_asic;
+};
+
+static void rtl8365mb_l2_data_to_uc(const u16 *data, struct rtl8365mb_l2_uc *uc)
+{
+ uc->key.mac_addr[5] = FIELD_GET(RTL8365MB_L2_UC_D0_MAC5_MASK, data[0]);
+ uc->key.mac_addr[4] = FIELD_GET(RTL8365MB_L2_UC_D0_MAC4_MASK, data[0]);
+ uc->key.mac_addr[3] = FIELD_GET(RTL8365MB_L2_UC_D1_MAC3_MASK, data[1]);
+ uc->key.mac_addr[2] = FIELD_GET(RTL8365MB_L2_UC_D1_MAC2_MASK, data[1]);
+ uc->key.mac_addr[1] = FIELD_GET(RTL8365MB_L2_UC_D2_MAC1_MASK, data[2]);
+ uc->key.mac_addr[0] = FIELD_GET(RTL8365MB_L2_UC_D2_MAC0_MASK, data[2]);
+ uc->key.efid = FIELD_GET(RTL8365MB_L2_UC_D4_EFID_MASK, data[4]);
+ uc->key.vid = FIELD_GET(RTL8365MB_L2_UC_D3_VID_MASK, data[3]);
+ uc->key.ivl = FIELD_GET(RTL8365MB_L2_UC_D3_IVL_MASK, data[3]);
+ uc->key.fid = FIELD_GET(RTL8365MB_L2_UC_D4_FID_MASK, data[4]);
+ uc->age = FIELD_GET(RTL8365MB_L2_UC_D4_AGE_MASK, data[4]);
+ uc->auth = FIELD_GET(RTL8365MB_L2_UC_D4_AUTH_MASK, data[4]);
+ uc->port = FIELD_GET(RTL8365MB_L2_UC_D4_PORT_MASK, data[4]) |
+ (FIELD_GET(RTL8365MB_L2_UC_D3_PORT_EXT_MASK, data[3]) << 3);
+ uc->sa_pri = FIELD_GET(RTL8365MB_L2_UC_D4_SA_PRI_MASK, data[4]);
+ uc->fwd_pri = FIELD_GET(RTL8365MB_L2_UC_D5_FWD_PRI_MASK, data[5]);
+ uc->sa_block = FIELD_GET(RTL8365MB_L2_UC_D4_SA_BLOCK_MASK, data[4]);
+ uc->da_block = FIELD_GET(RTL8365MB_L2_UC_D5_DA_BLOCK_MASK, data[5]);
+ uc->priority = FIELD_GET(RTL8365MB_L2_UC_D5_PRIORITY_MASK, data[5]);
+ uc->is_static = FIELD_GET(RTL8365MB_L2_UC_D5_STATIC_MASK, data[5]);
+}
+
+static void rtl8365mb_l2_uc_to_data(const struct rtl8365mb_l2_uc *uc, u16 *data)
+{
+ memset(data, 0, RTL8365MB_L2_ENTRY_SIZE * 2);
+ data[0] |=
+ FIELD_PREP(RTL8365MB_L2_UC_D0_MAC5_MASK, uc->key.mac_addr[5]);
+ data[0] |=
+ FIELD_PREP(RTL8365MB_L2_UC_D0_MAC4_MASK, uc->key.mac_addr[4]);
+ data[1] |=
+ FIELD_PREP(RTL8365MB_L2_UC_D1_MAC3_MASK, uc->key.mac_addr[3]);
+ data[1] |=
+ FIELD_PREP(RTL8365MB_L2_UC_D1_MAC2_MASK, uc->key.mac_addr[2]);
+ data[2] |=
+ FIELD_PREP(RTL8365MB_L2_UC_D2_MAC1_MASK, uc->key.mac_addr[1]);
+ data[2] |=
+ FIELD_PREP(RTL8365MB_L2_UC_D2_MAC0_MASK, uc->key.mac_addr[0]);
+ data[3] |= FIELD_PREP(RTL8365MB_L2_UC_D3_VID_MASK, uc->key.vid);
+ data[3] |= FIELD_PREP(RTL8365MB_L2_UC_D3_IVL_MASK, uc->key.ivl);
+ data[3] |= FIELD_PREP(RTL8365MB_L2_UC_D3_PORT_EXT_MASK, uc->port >> 3);
+ data[4] |= FIELD_PREP(RTL8365MB_L2_UC_D4_FID_MASK, uc->key.fid);
+ data[4] |= FIELD_PREP(RTL8365MB_L2_UC_D4_EFID_MASK, uc->key.efid);
+ data[4] |= FIELD_PREP(RTL8365MB_L2_UC_D4_AGE_MASK, uc->age);
+ data[4] |= FIELD_PREP(RTL8365MB_L2_UC_D4_AUTH_MASK, uc->auth);
+ data[4] |= FIELD_PREP(RTL8365MB_L2_UC_D4_PORT_MASK, uc->port);
+ data[4] |= FIELD_PREP(RTL8365MB_L2_UC_D4_SA_PRI_MASK, uc->sa_pri);
+ data[4] |= FIELD_PREP(RTL8365MB_L2_UC_D4_SA_BLOCK_MASK, uc->sa_block);
+ data[5] |= FIELD_PREP(RTL8365MB_L2_UC_D5_FWD_PRI_MASK, uc->fwd_pri);
+ data[5] |= FIELD_PREP(RTL8365MB_L2_UC_D5_DA_BLOCK_MASK, uc->da_block);
+ data[5] |= FIELD_PREP(RTL8365MB_L2_UC_D5_PRIORITY_MASK, uc->priority);
+ data[5] |= FIELD_PREP(RTL8365MB_L2_UC_D5_STATIC_MASK, uc->is_static);
+}
+
+static void rtl8365mb_l2_data_to_mc(const u16 *data, struct rtl8365mb_l2_mc *mc)
+{
+ mc->key.mac_addr[5] = FIELD_GET(RTL8365MB_L2_MC_MAC5_MASK, data[0]);
+ mc->key.mac_addr[4] = FIELD_GET(RTL8365MB_L2_MC_MAC4_MASK, data[0]);
+ mc->key.mac_addr[3] = FIELD_GET(RTL8365MB_L2_MC_MAC3_MASK, data[1]);
+ mc->key.mac_addr[2] = FIELD_GET(RTL8365MB_L2_MC_MAC2_MASK, data[1]);
+ mc->key.mac_addr[1] = FIELD_GET(RTL8365MB_L2_MC_MAC1_MASK, data[2]);
+ mc->key.mac_addr[0] = FIELD_GET(RTL8365MB_L2_MC_MAC0_MASK, data[2]);
+ mc->key.vid = FIELD_GET(RTL8365MB_L2_MC_VID_MASK, data[3]);
+ mc->key.ivl = FIELD_GET(RTL8365MB_L2_MC_IVL_MASK, data[3]);
+ mc->priority = FIELD_GET(RTL8365MB_L2_MC_PRIORITY_MASK, data[5]);
+ mc->fwd_pri = FIELD_GET(RTL8365MB_L2_MC_FWD_PRI_MASK, data[5]);
+ mc->is_static = FIELD_GET(RTL8365MB_L2_MC_STATIC_MASK, data[5]);
+ mc->member = FIELD_GET(RTL8365MB_L2_MC_MBR_MASK, data[4]) |
+ (FIELD_GET(RTL8365MB_L2_MC_MBR_EXT1_MASK, data[3]) << 8) |
+ (FIELD_GET(RTL8365MB_L2_MC_MBR_EXT2_MASK, data[5]) << 8);
+ mc->igmpidx = FIELD_GET(RTL8365MB_L2_MC_IGMPIDX_MASK, data[4]);
+ mc->igmp_asic = FIELD_GET(RTL8365MB_L2_MC_IGMP_ASIC_MASK, data[5]);
+}
+
+static void rtl8365mb_l2_mc_to_data(const struct rtl8365mb_l2_mc *mc, u16 *data)
+{
+ memset(data, 0, 12);
+ data[0] |= FIELD_PREP(RTL8365MB_L2_MC_MAC5_MASK, mc->key.mac_addr[5]);
+ data[0] |= FIELD_PREP(RTL8365MB_L2_MC_MAC4_MASK, mc->key.mac_addr[4]);
+ data[1] |= FIELD_PREP(RTL8365MB_L2_MC_MAC3_MASK, mc->key.mac_addr[3]);
+ data[1] |= FIELD_PREP(RTL8365MB_L2_MC_MAC2_MASK, mc->key.mac_addr[2]);
+ data[2] |= FIELD_PREP(RTL8365MB_L2_MC_MAC1_MASK, mc->key.mac_addr[1]);
+ data[2] |= FIELD_PREP(RTL8365MB_L2_MC_MAC0_MASK, mc->key.mac_addr[0]);
+ data[3] |= FIELD_PREP(RTL8365MB_L2_MC_VID_MASK, mc->key.vid);
+ data[3] |= FIELD_PREP(RTL8365MB_L2_MC_IVL_MASK, mc->key.ivl);
+ data[3] |= FIELD_PREP(RTL8365MB_L2_MC_MBR_EXT1_MASK, mc->member >> 8);
+ data[4] |= FIELD_PREP(RTL8365MB_L2_MC_MBR_MASK, mc->member);
+ data[4] |= FIELD_PREP(RTL8365MB_L2_MC_IGMPIDX_MASK, mc->igmpidx);
+ data[5] |= FIELD_PREP(RTL8365MB_L2_MC_IGMP_ASIC_MASK, mc->igmp_asic);
+ data[5] |= FIELD_PREP(RTL8365MB_L2_MC_PRIORITY_MASK, mc->priority);
+ data[5] |= FIELD_PREP(RTL8365MB_L2_MC_FWD_PRI_MASK, mc->fwd_pri);
+ data[5] |= FIELD_PREP(RTL8365MB_L2_MC_STATIC_MASK, mc->is_static);
+ data[5] |= FIELD_PREP(RTL8365MB_L2_MC_MBR_EXT2_MASK, mc->member >> 10);
+}
+
+/**
+ * rtl8365mb_l2_get_next_uc() - get the next Unicast L2 entry
+ *
+ * @priv: realtek_priv pointer
+ * @addr: as input, the table index to start the walk
+ * as output, the found table index
+ * @port: restrict the walk on entries related to port
+ * @uc: returned L2 Unicast table entry
+ *
+ * This function get the next unicast L2 table entry starting from @addr
+ * and checking exclusively entries related to @port. If no more entries
+ * were found, the output @addr will be lower than the input @addr and @uc
+ * will not be overwritten.
+ *
+ * Return: Returns 0 on success, a negative error on failure.
+ **/
+int rtl8365mb_l2_get_next_uc(struct realtek_priv *priv, u16 *addr, u16 port,
+ struct rtl8365mb_l2_uc *uc)
+{
+ u16 data[RTL8365MB_L2_ENTRY_SIZE] = { 0 };
+ int ret;
+
+ ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
+ RTL8365MB_TABLE_OP_READ, addr,
+ RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC_PORT,
+ port, data, RTL8365MB_L2_ENTRY_SIZE);
+ if (ret)
+ return ret;
+
+ rtl8365mb_l2_data_to_uc(data, uc);
+
+ return 0;
+}
+
+int rtl8365mb_l2_add_uc(struct realtek_priv *priv, u16 port,
+ const unsigned char mac_addr[static ETH_ALEN],
+ u16 efid, u16 vid)
+{
+ u16 data[RTL8365MB_L2_ENTRY_SIZE] = { 0 };
+ struct rtl8365mb_l2_uc uc = { 0 };
+ u16 addr;
+ int ret;
+
+ memcpy(uc.key.mac_addr, mac_addr, ETH_ALEN);
+ uc.key.efid = efid;
+ uc.key.ivl = true;
+ uc.key.vid = vid;
+ uc.port = port;
+ /* do not let HW decrease age */
+ uc.is_static = true;
+ /* age greater than 0 adds/updates entries */
+ uc.age = 1;
+ rtl8365mb_l2_uc_to_data(&uc, data);
+
+ /* add the new entry or update an existing one */
+ ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
+ RTL8365MB_TABLE_OP_WRITE, &addr,
+ 0, 0,
+ data, RTL8365MB_L2_ENTRY_SIZE);
+ /* addr will hold the table index, but it is not used here */
+ if (ret == -ENOENT) {
+ /* -ENOENT means the just added entry was not found (and @addr
+ * does not hold the table index. Although any error will be
+ * treated equally by the caller, assume that the missing entry
+ * means the table is full (tested in real HW).
+ */
+ return -ENOSPC;
+ }
+ return ret;
+}
+
+int rtl8365mb_l2_del_uc(struct realtek_priv *priv, u16 port,
+ const unsigned char mac_addr[static ETH_ALEN],
+ u16 efid, u16 vid)
+{
+ u16 data[RTL8365MB_L2_ENTRY_SIZE] = { 0 };
+ struct rtl8365mb_l2_uc uc = { 0 };
+ u16 addr;
+ int ret;
+
+ memcpy(uc.key.mac_addr, mac_addr, ETH_ALEN);
+ uc.key.efid = efid;
+ uc.key.ivl = true;
+ uc.key.vid = vid;
+ /* age 0 deletes the entry */
+ uc.age = 0;
+ rtl8365mb_l2_uc_to_data(&uc, data);
+
+ /* it looks like the switch will always add/update the entry,
+ * even when age is 0 or uc.key did not match an existing entry,
+ * just to immediately drop it because age is zero. You can still
+ * get the added/updated address from @addr
+ */
+ ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
+ RTL8365MB_TABLE_OP_WRITE, &addr,
+ 0, 0,
+ data, RTL8365MB_L2_ENTRY_SIZE);
+ /* addr will hold the table index, but it is not used here */
+ return ret;
+}
+
+int rtl8365mb_l2_flush(struct realtek_priv *priv, int port, u16 vid)
+{
+ int mode = vid ? RTL8365MB_L2_FLUSH_CTRL2_MODE_PORT_VID :
+ RTL8365MB_L2_FLUSH_CTRL2_MODE_PORT;
+ u32 val, mask;
+ int ret;
+
+ mutex_lock(&priv->map_lock);
+
+ /* Configure flushing mode; only flush dynamic entries */
+ ret = regmap_write(priv->map_nolock, RTL8365MB_L2_FLUSH_CTRL2_REG,
+ FIELD_PREP(RTL8365MB_L2_FLUSH_CTRL2_MODE_MASK,
+ mode) |
+ FIELD_PREP(RTL8365MB_L2_FLUSH_CTRL2_TYPE_MASK,
+ RTL8365MB_L2_FLUSH_CTRL2_TYPE_DYNAMIC));
+ if (ret)
+ goto out;
+
+ ret = regmap_write(priv->map_nolock, RTL8365MB_L2_FLUSH_CTRL1_REG,
+ FIELD_PREP(RTL8365MB_L2_FLUSH_CTRL1_VID_MASK, vid));
+
+ if (ret)
+ goto out;
+ /* Now issue the flush command and wait for its completion. There are
+ * two registers for this purpose, and which one to use depends on the
+ * port number. The _EXT register is for ports 8 or higher.
+ */
+ if (port < 8) {
+ val = FIELD_PREP(RTL8365MB_L2_FLUSH_PORT_MASK_MASK,
+ BIT(port) & 0xFF);
+ ret = regmap_write(priv->map_nolock,
+ RTL8365MB_L2_FLUSH_PORT_REG, val);
+ if (ret)
+ goto out;
+
+ mask = FIELD_PREP(RTL8365MB_L2_FLUSH_PORT_BUSY_MASK,
+ BIT(port) & 0xFF);
+ ret = regmap_read_poll_timeout(priv->map_nolock,
+ RTL8365MB_L2_FLUSH_PORT_REG,
+ val, !(val & mask), 10, 100);
+ if (ret)
+ goto out;
+ } else {
+ val = FIELD_PREP(RTL8365MB_L2_FLUSH_PORT_EXT_MASK_MASK,
+ BIT(port) >> 8);
+ ret = regmap_write(priv->map_nolock,
+ RTL8365MB_L2_FLUSH_PORT_EXT_REG, val);
+ if (ret)
+ goto out;
+
+ mask = FIELD_PREP(RTL8365MB_L2_FLUSH_PORT_EXT_BUSY_MASK,
+ BIT(port) >> 8);
+ ret = regmap_read_poll_timeout(priv->map_nolock,
+ RTL8365MB_L2_FLUSH_PORT_EXT_REG,
+ val, !(val & mask), 10, 100);
+ if (ret)
+ goto out;
+ }
+
+out:
+ mutex_unlock(&priv->map_lock);
+
+ return ret;
+}
+
+int rtl8365mb_l2_add_mc(struct realtek_priv *priv, u16 port,
+ const unsigned char mac_addr[static ETH_ALEN],
+ u16 vid)
+{
+ u16 data[RTL8365MB_L2_ENTRY_SIZE] = { 0 };
+ struct rtl8365mb_l2_mc mc = { 0 };
+ u16 addr;
+ int ret;
+
+ memcpy(mc.key.mac_addr, mac_addr, ETH_ALEN);
+ mc.key.vid = vid;
+ mc.key.ivl = true;
+ /* Already set the port and is_static, although not used in OP_READ,
+ * data will be ready for OP_WRITE if it is a new entry.
+ */
+ mc.member |= BIT(port);
+ mc.is_static = 1;
+ rtl8365mb_l2_mc_to_data(&mc, data);
+
+ /* First look for an existing entry (to get existing port members) */
+ ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
+ RTL8365MB_TABLE_OP_READ, &addr,
+ RTL8365MB_TABLE_L2_METHOD_MAC, 0,
+ data, RTL8365MB_L2_ENTRY_SIZE);
+ if (!ret) {
+ /* There is already an entry... */
+ rtl8365mb_l2_data_to_mc(data, &mc);
+ /* the port must be added as a member */
+ mc.member |= BIT(port);
+ rtl8365mb_l2_mc_to_data(&mc, data);
+ } else if (ret == -ENOENT) {
+ /* New entry, no need to update data again as it already
+ * includes the member
+ */
+ } else {
+ return ret;
+ }
+
+ /* add the new entry or update an existing one */
+ ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
+ RTL8365MB_TABLE_OP_WRITE, &addr,
+ 0, 0,
+ data, RTL8365MB_L2_ENTRY_SIZE);
+ /* addr will hold the table index, but it is not used here */
+ if (ret == -ENOENT) {
+ /* -ENOENT means the just added entry was not found (and @addr
+ * does not hold the table index. Although any error will be
+ * treated equally by the caller, assume that the missing entry
+ * means the table is full (tested in real HW).
+ */
+ return -ENOSPC;
+ }
+
+ return ret;
+}
+
+int rtl8365mb_l2_del_mc(struct realtek_priv *priv, u16 port,
+ const unsigned char mac_addr[static ETH_ALEN],
+ u16 vid)
+{
+ u16 data[RTL8365MB_L2_ENTRY_SIZE] = { 0 };
+ struct rtl8365mb_l2_mc mc = { 0 };
+ u16 addr;
+ int ret;
+
+ memcpy(mc.key.mac_addr, mac_addr, ETH_ALEN);
+ mc.key.vid = vid;
+ mc.key.ivl = true;
+ rtl8365mb_l2_mc_to_data(&mc, data);
+
+ /* First look for an existing entry (to get existing port members) */
+ ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
+ RTL8365MB_TABLE_OP_READ, &addr,
+ RTL8365MB_TABLE_L2_METHOD_MAC, 0,
+ data, RTL8365MB_L2_ENTRY_SIZE);
+ if (ret)
+ /* Any error, including -ENOENT is unexpected */
+ return ret;
+
+ rtl8365mb_l2_data_to_mc(data, &mc);
+ /* the port must be removed as a member */
+ mc.member &= ~BIT(port);
+ if (!mc.member) {
+ /* With no members, zero all non-key fields to delete the
+ * entry. However is_static is everything else we wrote.
+ * (and probably all that is needed by the HW)
+ */
+ mc.is_static = 0;
+ }
+ rtl8365mb_l2_mc_to_data(&mc, data);
+
+ /* update the existing entry. */
+ ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
+ RTL8365MB_TABLE_OP_WRITE, &addr,
+ 0, 0,
+ data, RTL8365MB_L2_ENTRY_SIZE);
+ return ret;
+}
diff --git a/drivers/net/dsa/realtek/rtl8365mb_l2.h b/drivers/net/dsa/realtek/rtl8365mb_l2.h
new file mode 100644
index 000000000000..b60d8617969d
--- /dev/null
+++ b/drivers/net/dsa/realtek/rtl8365mb_l2.h
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Forwarding and multicast database interface for the rtl8365mb switch family
+ *
+ * Copyright (C) 2022 Alvin Šipraga <alsi@bang-olufsen.dk>
+ */
+
+#ifndef _REALTEK_RTL8365MB_L2_H
+#define _REALTEK_RTL8365MB_L2_H
+
+#include <linux/if_ether.h>
+#include <linux/types.h>
+
+#include "realtek.h"
+
+/* It's valid for all family but RTL8370B, which has 4160 */
+#define RTL8365MB_LEARN_LIMIT_MAX 2112
+
+struct rtl8365mb_l2_uc_key {
+ u8 mac_addr[ETH_ALEN];
+ union {
+ u16 vid; /* IVL */
+ u16 fid; /* SVL */
+ };
+ bool ivl;
+ u16 efid;
+};
+
+struct rtl8365mb_l2_uc {
+ struct rtl8365mb_l2_uc_key key;
+ u8 port;
+ u8 age;
+ u8 priority;
+
+ bool sa_block;
+ bool da_block;
+ bool auth;
+ bool is_static;
+ bool sa_pri;
+ bool fwd_pri;
+};
+
+int rtl8365mb_l2_get_next_uc(struct realtek_priv *priv, u16 *addr, u16 port,
+ struct rtl8365mb_l2_uc *uc);
+int rtl8365mb_l2_add_uc(struct realtek_priv *priv, u16 port,
+ const unsigned char addr[static ETH_ALEN],
+ u16 efid, u16 vid);
+int rtl8365mb_l2_del_uc(struct realtek_priv *priv, u16 port,
+ const unsigned char addr[static ETH_ALEN],
+ u16 efid, u16 vid);
+int rtl8365mb_l2_flush(struct realtek_priv *priv, int port, u16 vid);
+
+int rtl8365mb_l2_add_mc(struct realtek_priv *priv, u16 port,
+ const unsigned char mac_addr[static ETH_ALEN],
+ u16 vid);
+int rtl8365mb_l2_del_mc(struct realtek_priv *priv, u16 port,
+ const unsigned char mac_addr[static ETH_ALEN],
+ u16 vid);
+
+#endif /* _REALTEK_RTL8365MB_L2_H */
diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
index c955864f17b3..0767b2704ad7 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_main.c
+++ b/drivers/net/dsa/realtek/rtl8365mb_main.c
@@ -104,6 +104,7 @@
#include "realtek-smi.h"
#include "realtek-mdio.h"
#include "rtl83xx.h"
+#include "rtl8365mb_l2.h"
#include "rtl8365mb_vlan.h"
/* Family-specific data and limits */
@@ -112,7 +113,6 @@
#define RTL8365MB_PHYREGMAX (RTL8365MB_NUM_PHYREGS - 1)
#define RTL8365MB_MAX_NUM_PORTS 11
#define RTL8365MB_MAX_NUM_EXTINTS 3
-#define RTL8365MB_LEARN_LIMIT_MAX 2112
/* Chip identification registers */
#define RTL8365MB_CHIP_ID_REG 0x1300
@@ -705,6 +705,7 @@ struct rtl8365mb_port {
* @chip_info: chip-specific info about the attached switch
* @cpu: CPU tagging and CPU port configuration for this chip
* @mib_lock: prevent concurrent reads of MIB counters
+ * @l2_lock: prevent concurrent access to L2 look-up table
* @ports: per-port data
*
* Private data for this driver.
@@ -715,6 +716,15 @@ struct rtl8365mb {
const struct rtl8365mb_chip_info *chip_info;
struct rtl8365mb_cpu cpu;
struct mutex mib_lock;
+ /* l2_lock is used to prevent concurrent modifications of L2 table
+ * entries while another function is reading it. l2_(add,del)_mc
+ * is an example that first read current table entry and then
+ * create/update it. l2_(add|del)_uc uses a single table op and,
+ * internally, it might not need this lock. However, altering FDB
+ * may still collide, as well as l2_flush, with fdb_dump iterating
+ * over FDB.
+ */
+ struct mutex l2_lock;
struct rtl8365mb_port ports[RTL8365MB_MAX_NUM_PORTS];
};
@@ -1267,6 +1277,22 @@ static void rtl8365mb_port_stp_state_set(struct dsa_switch *ds, int port,
val << RTL8365MB_MSTI_CTRL_PORT_STATE_OFFSET(port));
}
+static void rtl8365mb_port_fast_age(struct dsa_switch *ds, int port)
+{
+ struct realtek_priv *priv = ds->priv;
+ struct rtl8365mb *mb = priv->chip_data;
+ int ret;
+
+ dev_dbg(priv->dev, "fast_age port %d\n", port);
+
+ mutex_lock(&mb->l2_lock);
+ ret = rtl8365mb_l2_flush(priv, port, 0);
+ mutex_unlock(&mb->l2_lock);
+ if (ret)
+ dev_err(priv->dev, "failed to fast age on port %d: %d\n", port,
+ ret);
+}
+
static int rtl8365mb_port_set_transparent(struct realtek_priv *priv,
int igr_port, int egr_port,
bool enable)
@@ -1444,6 +1470,165 @@ static int rtl8365mb_vlan_setup(struct dsa_switch *ds)
return ret;
}
+static int rtl8365mb_port_fdb_add(struct dsa_switch *ds, int port,
+ const unsigned char *addr, u16 vid,
+ struct dsa_db db)
+{
+ struct realtek_priv *priv = ds->priv;
+ struct rtl8365mb *mb;
+ int efid;
+ int ret;
+
+ mb = priv->chip_data;
+
+ if (db.type != DSA_DB_PORT && db.type != DSA_DB_BRIDGE)
+ return -EOPNOTSUPP;
+
+ /*
+ * DSA_DB_BRIDGE ports use bridge number [1..N] as EFID, while
+ * DSA_DB_PORT use the default EFID (0), not used by any bridge.
+ */
+ efid = db.type == DSA_DB_BRIDGE ? db.bridge.num : 0;
+
+ dev_dbg(priv->dev, "fdb_add port %d addr %pM efid %d vid %d\n",
+ port, addr, efid, vid);
+
+ mutex_lock(&mb->l2_lock);
+ ret = rtl8365mb_l2_add_uc(priv, port, addr, efid, vid);
+ mutex_unlock(&mb->l2_lock);
+
+ if (ret)
+ dev_err(priv->dev, "fdb_add ERROR %pe\n", ERR_PTR(ret));
+ return ret;
+}
+
+static int rtl8365mb_port_fdb_del(struct dsa_switch *ds, int port,
+ const unsigned char *addr, u16 vid,
+ struct dsa_db db)
+{
+ struct realtek_priv *priv = ds->priv;
+ struct rtl8365mb *mb;
+ int efid;
+ int ret;
+
+ mb = priv->chip_data;
+
+ if (db.type != DSA_DB_PORT && db.type != DSA_DB_BRIDGE)
+ return -EOPNOTSUPP;
+
+ /*
+ * DSA_DB_BRIDGE ports use bridge number [1..N] as EFID, while
+ * DSA_DB_PORT use the default EFID (0), not used by any bridge.
+ */
+ efid = db.type == DSA_DB_BRIDGE ? db.bridge.num : 0;
+
+ dev_dbg(priv->dev, "fdb_del port %d addr %pM efid %d vid %d\n",
+ port, addr, efid, vid);
+
+ mutex_lock(&mb->l2_lock);
+ ret = rtl8365mb_l2_del_uc(priv, port, addr, efid, vid);
+ mutex_unlock(&mb->l2_lock);
+
+ if (ret)
+ dev_err(priv->dev, "fdb_del ERROR %pe\n", ERR_PTR(ret));
+ return ret;
+}
+
+static int rtl8365mb_port_fdb_dump(struct dsa_switch *ds, int port,
+ dsa_fdb_dump_cb_t *cb, void *data)
+{
+ struct realtek_priv *priv = ds->priv;
+ struct rtl8365mb_l2_uc uc = {0};
+ u16 start_addr, addr = 0;
+ struct rtl8365mb *mb;
+ int ret = 0;
+
+ mb = priv->chip_data;
+
+ mutex_lock(&mb->l2_lock);
+ while (true) {
+ start_addr = addr;
+
+ dev_dbg(priv->dev, "l2_get_next_uc, addr:%d, port:%d\n",
+ addr, port);
+ ret = rtl8365mb_l2_get_next_uc(priv, &addr, port, &uc);
+ dev_dbg(priv->dev,
+ "l2_get_next_uc addr:%d mac:%pM vid:%d static:%d ret:%pe\n",
+ addr, uc.key.mac_addr, uc.key.vid, uc.is_static,
+ ERR_PTR(ret));
+
+ /* table is empty for that port */
+ if (ret == -ENOENT)
+ break;
+ if (ret)
+ break;
+
+ /* overflow: reached the end */
+ if (addr < start_addr)
+ break;
+
+ cb(uc.key.mac_addr, uc.key.vid, uc.is_static, data);
+
+ addr++;
+
+ /* Avoid -ETIMEDOUT in rtl8365mb_l2_get_next_uc() */
+ if (addr > RTL8365MB_LEARN_LIMIT_MAX)
+ break;
+ }
+ mutex_unlock(&mb->l2_lock);
+
+ return ret;
+}
+
+static int rtl8365mb_port_mdb_add(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_mdb *mdb,
+ struct dsa_db db)
+{
+ struct realtek_priv *priv = ds->priv;
+ struct rtl8365mb *mb;
+ int ret;
+
+ mb = priv->chip_data;
+
+ if (db.type != DSA_DB_PORT && db.type != DSA_DB_BRIDGE)
+ return -EOPNOTSUPP;
+
+ dev_dbg(priv->dev, "mdb_add port %d addr %pM vid %d\n",
+ port, mdb->addr, mdb->vid);
+
+ mutex_lock(&mb->l2_lock);
+ ret = rtl8365mb_l2_add_mc(priv, port, mdb->addr, mdb->vid);
+ mutex_unlock(&mb->l2_lock);
+
+ if (ret)
+ dev_err(priv->dev, "mdb_add ERROR %pe\n", ERR_PTR(ret));
+ return ret;
+}
+
+static int rtl8365mb_port_mdb_del(struct dsa_switch *ds, int port,
+ const struct switchdev_obj_port_mdb *mdb,
+ struct dsa_db db)
+{
+ struct realtek_priv *priv = ds->priv;
+ struct rtl8365mb *mb;
+ int ret;
+
+ mb = priv->chip_data;
+
+ if (db.type != DSA_DB_PORT && db.type != DSA_DB_BRIDGE)
+ return -EOPNOTSUPP;
+
+ dev_dbg(priv->dev, "mdb_del port %d addr %pM vid %d\n",
+ port, mdb->addr, mdb->vid);
+
+ mutex_lock(&mb->l2_lock);
+ ret = rtl8365mb_l2_del_mc(priv, port, mdb->addr, mdb->vid);
+ mutex_unlock(&mb->l2_lock);
+
+ if (ret)
+ dev_err(priv->dev, "mdb_del ERROR %pe\n", ERR_PTR(ret));
+ return ret;
+}
static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port,
bool enable)
{
@@ -2305,6 +2490,8 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
mb = priv->chip_data;
cpu = &mb->cpu;
+ mutex_init(&mb->l2_lock);
+
ret = rtl8365mb_reset_chip(priv);
if (ret) {
dev_err(priv->dev, "failed to reset chip: %d\n", ret);
@@ -2369,6 +2556,8 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
if (ret)
goto out_teardown_irq;
+ ds->assisted_learning_on_cpu_port = true;
+ ds->fdb_isolation = true;
/* The EFID is 3 bits, but EFID 0 is reserved for standalone ports */
ds->max_num_bridges = FIELD_MAX(RTL8365MB_EFID_MASK);
@@ -2498,6 +2687,12 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
.port_bridge_join = rtl8365mb_port_bridge_join,
.port_bridge_leave = rtl8365mb_port_bridge_leave,
.port_stp_state_set = rtl8365mb_port_stp_state_set,
+ .port_fast_age = rtl8365mb_port_fast_age,
+ .port_fdb_add = rtl8365mb_port_fdb_add,
+ .port_fdb_del = rtl8365mb_port_fdb_del,
+ .port_fdb_dump = rtl8365mb_port_fdb_dump,
+ .port_mdb_add = rtl8365mb_port_mdb_add,
+ .port_mdb_del = rtl8365mb_port_mdb_del,
.port_vlan_add = rtl8365mb_port_vlan_add,
.port_vlan_del = rtl8365mb_port_vlan_del,
.port_vlan_filtering = rtl8365mb_port_vlan_filtering,
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [net-next PATCH 09/10] net: dsa: realtek: rtl8365mb: add bridge port flags
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
` (7 preceding siblings ...)
2026-03-31 23:00 ` [net-next PATCH 08/10] net: dsa: realtek: rtl8365mb: add FDB support Luiz Angelo Daros de Luca
@ 2026-03-31 23:00 ` Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 10/10] net: dsa: tag_rtl8_4: set KEEP flag Luiz Angelo Daros de Luca
9 siblings, 0 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
From: Alvin Šipraga <alsi@bang-olufsen.dk>
Add bridge port flags for:
- BR_LEARNING
- BR_FLOOD
- BR_MCAST_FLOOD
- BR_BCAST_FLOOD
Co-developed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
drivers/net/dsa/realtek/rtl8365mb_main.c | 125 +++++++++++++++++++++++++++++++
1 file changed, 125 insertions(+)
diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
index 0767b2704ad7..b26335c5b27e 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_main.c
+++ b/drivers/net/dsa/realtek/rtl8365mb_main.c
@@ -302,6 +302,21 @@
#define RTL8365MB_MSTI_CTRL_PORT_STATE_MASK(_physport) \
(0x3 << RTL8365MB_MSTI_CTRL_PORT_STATE_OFFSET((_physport)))
+/* Unknown unicast DA flooding port mask */
+#define RTL8365MB_UNKNOWN_UNICAST_FLOODING_PMASK_REG 0x0890
+#define RTL8365MB_UNKNOWN_UNICAST_FLOODING_PMASK_MASK 0x07FF
+
+/* Unknown multicast DA flooding port mask */
+#define RTL8365MB_UNKNOWN_MULTICAST_FLOODING_PMASK_REG 0x0891
+#define RTL8365MB_UNKNOWN_MULTICAST_FLOODING_PMASK_MASK 0x07FF
+
+/* Broadcast flooding port mask */
+#define RTL8365MB_UNKNOWN_BROADCAST_FLOODING_PMASK_REG 0x0892
+#define RTL8365MB_UNKNOWN_BROADCAST_FLOODING_PMASK_MASK 0x07FF
+
+#define RTL8365MB_SUPPORTED_BRIDGE_FLAGS \
+ (BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD)
+
/* Miscellaneous port configuration register, incl. VLAN egress mode */
#define RTL8365MB_PORT_MISC_CFG_REG_BASE 0x000E
#define RTL8365MB_PORT_MISC_CFG_REG(_p) \
@@ -1641,6 +1656,97 @@ static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port,
enable ? RTL8365MB_LEARN_LIMIT_MAX : 0);
}
+static int rtl8365mb_port_set_ucast_flood(struct realtek_priv *priv, int port,
+ bool enable)
+{
+ /* Frames with unknown unicast DA will be flooded to a programmable
+ * port mask that by default includes all ports. Add or remove
+ * the specified port from this port mask accordingly.
+ */
+ return regmap_update_bits(priv->map,
+ RTL8365MB_UNKNOWN_UNICAST_FLOODING_PMASK_REG,
+ BIT(port), enable ? BIT(port) : 0);
+}
+
+static int rtl8365mb_port_set_mcast_flood(struct realtek_priv *priv, int port,
+ bool enable)
+{
+ return regmap_update_bits(priv->map,
+ RTL8365MB_UNKNOWN_MULTICAST_FLOODING_PMASK_REG,
+ BIT(port), enable ? BIT(port) : 0);
+}
+
+static int rtl8365mb_port_set_bcast_flood(struct realtek_priv *priv, int port,
+ bool enable)
+{
+ return regmap_update_bits(priv->map,
+ RTL8365MB_UNKNOWN_BROADCAST_FLOODING_PMASK_REG,
+ BIT(port), enable ? BIT(port) : 0);
+}
+
+static int rtl8365mb_port_pre_bridge_flags(struct dsa_switch *ds, int port,
+ struct switchdev_brport_flags flags,
+ struct netlink_ext_ack *extack)
+{
+ struct realtek_priv *priv = ds->priv;
+
+ dev_dbg(priv->dev, "pre_bridge_flags port:%d flags:%lx supported:%lx",
+ port, flags.mask, RTL8365MB_SUPPORTED_BRIDGE_FLAGS);
+
+ if (flags.mask & ~RTL8365MB_SUPPORTED_BRIDGE_FLAGS)
+ return -EINVAL;
+
+ return 0;
+}
+
+static int rtl8365mb_port_bridge_flags(struct dsa_switch *ds, int port,
+ struct switchdev_brport_flags flags,
+ struct netlink_ext_ack *exack)
+{
+ struct realtek_priv *priv = ds->priv;
+ int ret;
+
+ dev_dbg(priv->dev, "port_bridge_flags port:%d flags:%lx supported:%lx",
+ port, flags.mask, RTL8365MB_SUPPORTED_BRIDGE_FLAGS);
+
+ if (flags.mask & BR_LEARNING) {
+ bool learning_en = !!(flags.val & BR_LEARNING);
+
+ ret = rtl8365mb_port_set_learning(priv, port, learning_en);
+ if (ret)
+ return ret;
+ }
+
+ if (flags.mask & BR_FLOOD) {
+ bool ucast_flood_en = !!(flags.val & BR_FLOOD);
+
+ ret = rtl8365mb_port_set_ucast_flood(priv, port,
+ ucast_flood_en);
+ if (ret)
+ return ret;
+ }
+
+ if (flags.mask & BR_MCAST_FLOOD) {
+ bool mcast_flood_en = !!(flags.val & BR_MCAST_FLOOD);
+
+ ret = rtl8365mb_port_set_mcast_flood(priv, port,
+ mcast_flood_en);
+ if (ret)
+ return ret;
+ }
+
+ if (flags.mask & BR_BCAST_FLOOD) {
+ bool bcast_flood_en = !!(flags.val & BR_BCAST_FLOOD);
+
+ ret = rtl8365mb_port_set_bcast_flood(priv, port,
+ bcast_flood_en);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
static int rtl8365mb_port_set_efid(struct realtek_priv *priv, int port,
u32 efid)
{
@@ -1691,6 +1797,8 @@ static int rtl8365mb_port_bridge_join(struct dsa_switch *ds, int port,
int ret;
int i;
+ dev_dbg(priv->dev, "bridge %d join port %d\n", port, bridge.num);
+
/* Add this port to the isolation group of every other port
* offloading this bridge.
*/
@@ -1730,6 +1838,8 @@ static void rtl8365mb_port_bridge_leave(struct dsa_switch *ds, int port,
u32 mask = 0;
int i;
+ dev_dbg(priv->dev, "bridge %d leave port %d\n", port, bridge.num);
+
/* Remove this port from the isolation group of every other
* port offloading this bridge.
*/
@@ -2547,6 +2657,19 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
if (ret)
goto out_teardown_irq;
+ /* Enable all types of flooding */
+ ret = rtl8365mb_port_set_ucast_flood(priv, i, true);
+ if (ret)
+ goto out_teardown_irq;
+
+ ret = rtl8365mb_port_set_mcast_flood(priv, i, true);
+ if (ret)
+ goto out_teardown_irq;
+
+ ret = rtl8365mb_port_set_bcast_flood(priv, i, true);
+ if (ret)
+ goto out_teardown_irq;
+
/* Set up per-port private data */
p->priv = priv;
p->index = i;
@@ -2686,6 +2809,8 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
.phylink_get_caps = rtl8365mb_phylink_get_caps,
.port_bridge_join = rtl8365mb_port_bridge_join,
.port_bridge_leave = rtl8365mb_port_bridge_leave,
+ .port_pre_bridge_flags = rtl8365mb_port_pre_bridge_flags,
+ .port_bridge_flags = rtl8365mb_port_bridge_flags,
.port_stp_state_set = rtl8365mb_port_stp_state_set,
.port_fast_age = rtl8365mb_port_fast_age,
.port_fdb_add = rtl8365mb_port_fdb_add,
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* [net-next PATCH 10/10] net: dsa: tag_rtl8_4: set KEEP flag
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
` (8 preceding siblings ...)
2026-03-31 23:00 ` [net-next PATCH 09/10] net: dsa: realtek: rtl8365mb: add bridge port flags Luiz Angelo Daros de Luca
@ 2026-03-31 23:00 ` Luiz Angelo Daros de Luca
9 siblings, 0 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-03-31 23:00 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King
Cc: netdev, linux-kernel, Luiz Angelo Daros de Luca
KEEP=1 is needed because we should respect the format of the packet as
the kernel sends it to us. Unless tx forward offloading is used, the
kernel is giving us the packet exactly as it should leave the specified
port on the wire. Until now this was not needed because the ports were
always functioning in a standalone mode in a VLAN-unaware way, so the
switch would not tag or untag frames anyway. But arguably it should have
been KEEP=1 all along.
Co-developed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
---
net/dsa/tag_rtl8_4.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/net/dsa/tag_rtl8_4.c b/net/dsa/tag_rtl8_4.c
index b7ed39c5419f..852c6b88079a 100644
--- a/net/dsa/tag_rtl8_4.c
+++ b/net/dsa/tag_rtl8_4.c
@@ -99,6 +99,7 @@
#define RTL8_4_REASON_TRAP 80
#define RTL8_4_LEARN_DIS BIT(5)
+#define RTL8_4_KEEP BIT(7)
#define RTL8_4_TX GENMASK(3, 0)
#define RTL8_4_RX GENMASK(10, 0)
@@ -114,8 +115,9 @@ static void rtl8_4_write_tag(struct sk_buff *skb, struct net_device *dev,
/* Set Protocol; zero REASON */
tag16[1] = htons(FIELD_PREP(RTL8_4_PROTOCOL, RTL8_4_PROTOCOL_RTL8365MB));
- /* Zero EFID_EN, EFID, PRI_EN, PRI, VSEL, VIDX, KEEP; set LEARN_DIS */
- tag16[2] = htons(FIELD_PREP(RTL8_4_LEARN_DIS, 1));
+ /* Zero EFID_EN, EFID, PRI_EN, PRI, VSEL, VIDX; set KEEP, LEARN_DIS */
+ tag16[2] = htons(FIELD_PREP(RTL8_4_LEARN_DIS, 1) |
+ FIELD_PREP(RTL8_4_KEEP, 1));
/* Zero ALLOW; set RX (CPU->switch) forwarding port mask */
tag16[3] = htons(FIELD_PREP(RTL8_4_RX, dsa_xmit_port_mask(skb, dev)));
--
2.53.0
^ permalink raw reply related [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-03-31 23:00 ` [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH() Luiz Angelo Daros de Luca
@ 2026-04-01 2:15 ` Yury Norov
2026-04-02 4:00 ` Luiz Angelo Daros de Luca
0 siblings, 1 reply; 24+ messages in thread
From: Yury Norov @ 2026-04-01 2:15 UTC (permalink / raw)
To: Luiz Angelo Daros de Luca
Cc: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King,
netdev, linux-kernel
On Tue, Mar 31, 2026 at 08:00:04PM -0300, Luiz Angelo Daros de Luca wrote:
> Some hardware stores logical fields split across multiple registers,
> requiring partial values to be reassembled using shifts and masks.
>
> Add FIELD_WIDTH(), a helper that returns the number of contiguous bits
> covered by a bitfield mask. This allows callers to determine how much a
> partial value needs to be shifted when reconstructing a field.
>
> This complements FIELD_MAX() and FIELD_FIT(), and avoids reimplementing
> bit counting logic in drivers.
>
> Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
> ---
> include/linux/bitfield.h | 13 +++++++++++++
> 1 file changed, 13 insertions(+)
>
> diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
> index 54aeeef1f0ec..8d6c1c309c3b 100644
> --- a/include/linux/bitfield.h
> +++ b/include/linux/bitfield.h
> @@ -111,6 +111,19 @@
> (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
> })
>
> +/**
> + * FIELD_WIDTH() - return the width of a bitfield
> + * @_mask: shifted mask defining the field's length and position
> + *
> + * Returns the number of contiguous bits covered by @_mask.
> + * This corresponds to the bit width of FIELD_MAX(@_mask).
> + */
> +#define FIELD_WIDTH(_mask) \
Please no underscored names unless necessary.
> + ({ \
> + __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_WIDTH: "); \
> + __bf_shf(~FIELD_MAX(_mask)); \
> + })
I believe, this should be:
#define FIELD_WIDTH(mask) ({ \
__BF_FIELD_CHECK_MASK(mask, 0ULL, "FIELD_WIDTH: "); \
HWEIGHT(mask); \
})
Thanks,
Yury
> +
> /**
> * FIELD_FIT() - check if value fits in the field
> * @_mask: shifted mask defining the field's length and position
>
> --
> 2.53.0
>
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
2026-03-31 23:00 ` [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support Luiz Angelo Daros de Luca
@ 2026-04-01 2:27 ` Yury Norov
2026-04-02 2:45 ` Luiz Angelo Daros de Luca
0 siblings, 1 reply; 24+ messages in thread
From: Yury Norov @ 2026-04-01 2:27 UTC (permalink / raw)
To: Luiz Angelo Daros de Luca
Cc: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King,
netdev, linux-kernel
On Tue, Mar 31, 2026 at 08:00:06PM -0300, Luiz Angelo Daros de Luca wrote:
> Walleij <linusw@kernel.org>,
> =?utf-8?q?Alvin_=C5=A0ipraga?= <alsi@bang-olufsen.dk>,
> Yury Norov <yury.norov@gmail.com>,
> Rasmus Villemoes <linux@rasmusvillemoes.dk>,
> Russell King <linux@armlinux.org.uk>
> Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
> Luiz Angelo Daros de Luca <luizluca@gmail.com>
> X-Mailer: b4 0.15.1
> Status: O
> Content-Length: 40249
> Lines: 1202
>
> From: Alvin Šipraga <alsi@bang-olufsen.dk>
>
> Realtek RTL8365MB switches (a.k.a. RTL8367C family) use two different
> structures for VLANs:
>
> - VLAN4K: A full table with 4096 entries defining port membership and
> tagging.
> - VLANMC: A smaller table with 32 entries used primarily for PVID
> assignment.
>
> In this hardware, a port's PVID must point to an index in the VLANMC
> table rather than a VID directly. Since the VLANMC table is limited to
> 32 entries, the driver implements a dynamic allocation scheme to
> maximize resource usage:
>
> - VLAN4K is treated by the driver as the source of truth for membership.
> - A VLANMC entry is only allocated when a port is configured to use a
> specific VID as its PVID.
> - VLANMC entries are deleted when no longer needed as a PVID by any port.
>
> Although VLANMC has a members field, the switch only checks membership
> in the VLAN4K table. However, when a corresponding VLAN entry also exists
> in VLANMC, this driver keeps both membership configurations in sync.
>
> VLANMC index 0, although a valid entry, is reserved in this driver as a
> neutral PVID value for ports not using a specific PVID.
>
> In the subsequent RTL8367D switch family, VLANMC table was
> removed and PVID assignment was delegated to a dedicated set of
> registers.
>
> All ports start isolated, forwarding exclusively to CPU ports, and
> with VLAN transparent, ignoring VLAN membership. Once a member in a
> bridge, the port isolation is expanded to include the bridge members.
> When that bridge enables VLAN filtering, the VLAN transparent feature is
> disabled, letting the switch filter based on VLAN setup.
>
> Co-developed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
> Signed-off-by: Alvin Šipraga <alsi@bang-olufsen.dk>
> Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
> ---
> drivers/net/dsa/realtek/Makefile | 1 +
> drivers/net/dsa/realtek/rtl8365mb_main.c | 256 ++++++++++
> drivers/net/dsa/realtek/rtl8365mb_vlan.c | 805 +++++++++++++++++++++++++++++++
> drivers/net/dsa/realtek/rtl8365mb_vlan.h | 30 ++
> 4 files changed, 1092 insertions(+)
>
> diff --git a/drivers/net/dsa/realtek/Makefile b/drivers/net/dsa/realtek/Makefile
> index 99654c4c5a3d..b7fc4e852fd8 100644
> --- a/drivers/net/dsa/realtek/Makefile
> +++ b/drivers/net/dsa/realtek/Makefile
> @@ -18,3 +18,4 @@ endif
> obj-$(CONFIG_NET_DSA_REALTEK_RTL8365MB) += rtl8365mb.o
> rtl8365mb-objs := rtl8365mb_main.o \
> rtl8365mb_table.o \
> + rtl8365mb_vlan.o \
> diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
> index 530ff5503543..c604bd744d38 100644
> --- a/drivers/net/dsa/realtek/rtl8365mb_main.c
> +++ b/drivers/net/dsa/realtek/rtl8365mb_main.c
> @@ -104,6 +104,7 @@
> #include "realtek-smi.h"
> #include "realtek-mdio.h"
> #include "rtl83xx.h"
> +#include "rtl8365mb_vlan.h"
>
> /* Family-specific data and limits */
> #define RTL8365MB_PHYADDRMAX 7
> @@ -292,6 +293,67 @@
> #define RTL8365MB_MSTI_CTRL_PORT_STATE_MASK(_physport) \
> (0x3 << RTL8365MB_MSTI_CTRL_PORT_STATE_OFFSET((_physport)))
>
> +/* Miscellaneous port configuration register, incl. VLAN egress mode */
> +#define RTL8365MB_PORT_MISC_CFG_REG_BASE 0x000E
> +#define RTL8365MB_PORT_MISC_CFG_REG(_p) \
> + (RTL8365MB_PORT_MISC_CFG_REG_BASE + ((_p) << 5))
> +#define RTL8365MB_PORT_MISC_CFG_SMALL_TAG_IPG_MASK 0x8000
> +#define RTL8365MB_PORT_MISC_CFG_TX_ITFSP_MODE_MASK 0x4000
> +#define RTL8365MB_PORT_MISC_CFG_FLOWCTRL_INDEP_MASK 0x2000
> +#define RTL8365MB_PORT_MISC_CFG_DOT1Q_REMARK_ENABLE_MASK 0x1000
> +#define RTL8365MB_PORT_MISC_CFG_INGRESSBW_FLOWCTRL_MASK 0x0800
> +#define RTL8365MB_PORT_MISC_CFG_INGRESSBW_IFG_MASK 0x0400
> +#define RTL8365MB_PORT_MISC_CFG_RX_SPC_MASK 0x0200
> +#define RTL8365MB_PORT_MISC_CFG_CRC_SKIP_MASK 0x0100
> +#define RTL8365MB_PORT_MISC_CFG_PKTGEN_TX_FIRST_MASK 0x0080
> +#define RTL8365MB_PORT_MISC_CFG_MAC_LOOPBACK_MASK 0x0040
> +/* See &rtl8365mb_vlan_egress_mode */
> +#define RTL8365MB_PORT_MISC_CFG_VLAN_EGRESS_MODE_MASK 0x0030
> +#define RTL8365MB_PORT_MISC_CFG_CONGESTION_SUSTAIN_TIME_MASK 0x000F
> +
> +/**
> + * enum rtl8365mb_vlan_egress_mode - port VLAN engress mode
> + * @RTL8365MB_VLAN_EGRESS_MODE_ORIGINAL: follow untag mask in VLAN4k table entry
> + * @RTL8365MB_VLAN_EGRESS_MODE_KEEP: the VLAN tag format of egressed packets
> + * will remain the same as their ingressed format, but the priority and VID
> + * fields may be altered
> + * @RTL8365MB_VLAN_EGRESS_MODE_PRI_TAG: always egress with priority tag
> + * @RTL8365MB_VLAN_EGRESS_MODE_REAL_KEEP: the VLAN tag format of egressed
> + * packets will remain the same as their ingressed format, and neither the
> + * priority nor VID fields can be altered
> + */
> +enum rtl8365mb_vlan_egress_mode {
> + RTL8365MB_VLAN_EGRESS_MODE_ORIGINAL = 0,
> + RTL8365MB_VLAN_EGRESS_MODE_KEEP = 1,
> + RTL8365MB_VLAN_EGRESS_MODE_PRI_TAG = 2,
> + RTL8365MB_VLAN_EGRESS_MODE_REAL_KEEP = 3,
> +};
> +
> +/* VLAN control register */
> +#define RTL8365MB_VLAN_CTRL_REG 0x07A8
> +#define RTL8365MB_VLAN_CTRL_EN_MASK 0x0001
> +
> +/* VLAN ingress filter register */
> +#define RTL8365MB_VLAN_INGRESS_REG 0x07A9
> +#define RTL8365MB_VLAN_INGRESS_MASK GENMASK(10, 0)
> +#define RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_OFFSET(_p) (_p)
> +#define RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_MASK(_p) BIT(_p)
> +
> +/* VLAN "transparent" setting registers */
> +#define RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG_BASE 0x09D0
> +#define RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG(_p) \
> + (RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG_BASE + (_p))
> +
> +/* Frame type filtering registers */
> +#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_BASE 0x07aa
> +#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_REG(port) \
> + (RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_BASE + ((port) >> 3))
> +/* required as FIELD_PREP cannot use non-constant masks */
> +#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_MASK(port) \
> + (0x3 << RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_OFFSET(port))
> +#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_OFFSET(port) \
> + (((port) & 0x7) << 1)
> +
> /* MIB counter value registers */
> #define RTL8365MB_MIB_COUNTER_BASE 0x1000
> #define RTL8365MB_MIB_COUNTER_REG(_x) (RTL8365MB_MIB_COUNTER_BASE + (_x))
> @@ -1196,6 +1258,183 @@ static void rtl8365mb_port_stp_state_set(struct dsa_switch *ds, int port,
> val << RTL8365MB_MSTI_CTRL_PORT_STATE_OFFSET(port));
> }
>
> +static int rtl8365mb_port_set_transparent(struct realtek_priv *priv,
> + int igr_port, int egr_port,
> + bool enable)
> +{
> + dev_dbg(priv->dev, "%s transparent VLAN from %d to %d\n",
> + enable ? "Enable" : "Disable", igr_port, egr_port);
> +
> + /* "Transparent" between the two ports means that packets forwarded by
> + * igr_port and egressed on egr_port will not be filtered by the usual
> + * VLAN membership settings.
> + */
> + return regmap_update_bits(priv->map,
> + RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG(egr_port),
> + BIT(igr_port), enable ? BIT(igr_port) : 0);
> +}
> +
> +static int rtl8365mb_port_set_ingress_filtering(struct realtek_priv *priv,
> + int port, bool enable)
> +{
> + /* Ingress filtering enabled: Discard VLAN-tagged frames if the port is
> + * not a member of the VLAN with which the packet is associated.
> + * Untagged packets will also be discarded unless the port has a PVID
> + * programmed. Priority-tagged frames are treated as untagged frames.
> + *
> + * Ingress filtering disabled: Accept all tagged and untagged frames.
> + */
> + return regmap_update_bits(priv->map, RTL8365MB_VLAN_INGRESS_REG,
> + RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_MASK(port),
> + enable ?
> + RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_MASK(port) :
> + 0);
> +}
> +
> +static int rtl8365mb_port_vlan_filtering(struct dsa_switch *ds, int port,
> + bool vlan_filtering,
> + struct netlink_ext_ack *extack)
> +{
> + struct realtek_priv *priv = ds->priv;
> + struct dsa_port *dp;
> + int ret;
> +
> + dev_dbg(priv->dev, "port %d: %s VLAN filtering\n", port,
> + vlan_filtering ? "enable" : "disable");
> +
> + /* (En|dis)able incoming packets filter, i.e. ignore VLAN membership */
> + dsa_switch_for_each_available_port(dp, ds) {
> + /* after considering port isolation, if not filtering
> + * allow forwarding from port to dp->index ignoring
> + * VLAN membership.
> + */
> + ret = rtl8365mb_port_set_transparent(priv, port, dp->index,
> + !vlan_filtering);
> + if (ret)
> + return ret;
> + }
> +
> + /* If the port is not in the member set, the frame will be dropped */
> + return rtl8365mb_port_set_ingress_filtering(priv, port,
> + vlan_filtering);
> +}
> +
> +static int rtl8365mb_port_vlan_add(struct dsa_switch *ds, int port,
> + const struct switchdev_obj_port_vlan *vlan,
> + struct netlink_ext_ack *extack)
> +{
> + bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
> + bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
> + struct realtek_priv *priv = ds->priv;
> + int ret;
> +
> + dev_dbg(priv->dev, "add VLAN %d on port %d, %s, %s\n",
> + vlan->vid, port, untagged ? "untagged" : "tagged",
> + pvid ? "PVID" : "no PVID");
> + /* add port to vlan4k. It knows nothing about PVID */
> + ret = rtl8365mb_vlan_4k_port_add(ds, port, vlan, extack);
> + if (ret)
> + return ret;
> +
> + /* VlanMC knows nothing about untagged but it is required for PVID */
> + ret = rtl8365mb_vlan_mc_port_add(ds, port, vlan, extack);
> + if (ret)
> + goto undo_vlan_4k;
> +
> + /* Set PVID if needed */
> + if (pvid) {
> + ret = rtl8365mb_vlan_pvid_port_add(ds, port, vlan, extack);
> + if (ret)
> + goto undo_vlan_mc;
> + }
> +
> + return 0;
> +
> +undo_vlan_mc:
> + (void)rtl8365mb_vlan_mc_port_del(ds, port, vlan);
> +
> +undo_vlan_4k:
> + (void)rtl8365mb_vlan_4k_port_del(ds, port, vlan);
> + return ret;
> +}
> +
> +static int rtl8365mb_port_vlan_del(struct dsa_switch *ds, int port,
> + const struct switchdev_obj_port_vlan *vlan)
> +{
> + bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
> + bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
> + struct realtek_priv *priv = ds->priv;
> + int ret1, ret2, ret3;
> +
> + dev_dbg(priv->dev, "del VLAN %d on port %d, %s, %s\n",
> + vlan->vid, port, untagged ? "untagged" : "tagged",
> + pvid ? "PVID" : "no PVID");
> +
> + ret1 = rtl8365mb_vlan_pvid_port_del(ds, port, vlan);
> + ret2 = rtl8365mb_vlan_mc_port_del(ds, port, vlan);
> + ret3 = rtl8365mb_vlan_4k_port_del(ds, port, vlan);
> +
> + return ret1 ?: ret2 ?: ret3;
> +}
> +
> +static int
> +rtl8365mb_port_set_vlan_egress_mode(struct realtek_priv *priv, int port,
> + enum rtl8365mb_vlan_egress_mode mode)
> +{
> + u32 val;
> +
> + val = FIELD_PREP(RTL8365MB_PORT_MISC_CFG_VLAN_EGRESS_MODE_MASK, mode);
> + return regmap_update_bits(priv->map,
> + RTL8365MB_PORT_MISC_CFG_REG(port),
> + RTL8365MB_PORT_MISC_CFG_VLAN_EGRESS_MODE_MASK, val);
> +}
> +
> +/* VLAN support is always enabled in the switch.
> + *
> + * All ports starts with vlan-unaware state, letting non-bridge port forward
> + * to CPU.
> + *
> + */
> +static int rtl8365mb_vlan_setup(struct dsa_switch *ds)
> +{
> + struct realtek_priv *priv = ds->priv;
> + enum rtl8365mb_vlan_egress_mode mode;
> + struct dsa_port *dp;
> + int ret;
> +
> + dsa_switch_for_each_user_port(dp, ds) {
> + /* Disable vlan-filtering for all ports */
> + ret = rtl8365mb_port_vlan_filtering(ds, dp->index, false, NULL);
> + if (ret) {
> + dev_err(priv->dev,
> + "Failed to disable vlan filtering on port %d\n",
> + dp->index);
> + return ret;
> + }
> +
> + /* The switch default is RTL8365MB_VLAN_EGRESS_MODE_REAL_KEEP,
> + * that forwards the packet as it was received. However,
> + * different untag settings will require the switch to update
> + * the tag.
> + */
> + mode = RTL8365MB_VLAN_EGRESS_MODE_ORIGINAL;
> + ret = rtl8365mb_port_set_vlan_egress_mode(priv, dp->index,
> + mode);
> + if (ret) {
> + dev_err(priv->dev,
> + "Failed to set port %d egress mode\n",
> + dp->index);
> + return ret;
> + }
> + }
> +
> + /* VLAN is always enabled. */
> + ret = regmap_update_bits(priv->map, RTL8365MB_VLAN_CTRL_REG,
> + RTL8365MB_VLAN_CTRL_EN_MASK,
> + FIELD_PREP(RTL8365MB_VLAN_CTRL_EN_MASK, 1));
> + return ret;
> +}
> +
> static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port,
> bool enable)
> {
> @@ -2014,6 +2253,20 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
> if (ret)
> goto out_teardown_irq;
>
> + /* vlan config will only be effective for ports with vlan filtering */
> + ds->configure_vlan_while_not_filtering = true;
> + /* Set up VLAN */
> + ret = rtl8365mb_vlan_setup(ds);
> + if (ret)
> + goto out_teardown_irq;
> +
> + /* Set maximum packet length to 1536 bytes */
> + ret = regmap_update_bits(priv->map, RTL8365MB_CFG0_MAX_LEN_REG,
> + RTL8365MB_CFG0_MAX_LEN_MASK,
> + FIELD_PREP(RTL8365MB_CFG0_MAX_LEN_MASK, 1536));
> + if (ret)
> + goto out_teardown_irq;
> +
> ret = rtl83xx_setup_user_mdio(ds);
> if (ret) {
> dev_err(priv->dev, "could not set up MDIO bus\n");
> @@ -2124,6 +2377,9 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
> .teardown = rtl8365mb_teardown,
> .phylink_get_caps = rtl8365mb_phylink_get_caps,
> .port_stp_state_set = rtl8365mb_port_stp_state_set,
> + .port_vlan_add = rtl8365mb_port_vlan_add,
> + .port_vlan_del = rtl8365mb_port_vlan_del,
> + .port_vlan_filtering = rtl8365mb_port_vlan_filtering,
> .get_strings = rtl8365mb_get_strings,
> .get_ethtool_stats = rtl8365mb_get_ethtool_stats,
> .get_sset_count = rtl8365mb_get_sset_count,
> diff --git a/drivers/net/dsa/realtek/rtl8365mb_vlan.c b/drivers/net/dsa/realtek/rtl8365mb_vlan.c
> new file mode 100644
> index 000000000000..1ac36c06dcf7
> --- /dev/null
> +++ b/drivers/net/dsa/realtek/rtl8365mb_vlan.c
> @@ -0,0 +1,805 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* VLAN configuration interface for the rtl8365mb switch family
> + *
> + * Copyright (C) 2022 Alvin Šipraga <alsi@bang-olufsen.dk>
> + *
> + * VLAN configuration takes place in two separate domains of the switch: the
> + * VLAN4k table and the VLAN membership configuration (MC) database. While the
> + * VLAN4k table is exhaustive and can be fully populated with 4096 VLAN
> + * configurations, the same does not hold for the VLAN membership configuration
> + * database, which is limited to 32 entries.
> + *
> + * The switch will normally only use the VLAN4k table when making forwarding
> + * decisions. The VLAN membership configuration database is a vestigial ASIC
> + * design and is only used for a few specific features in the rtl8365mb
> + * family. This means that the limit of 32 entries should not hinder us in
> + * programming a huge number of VLANs into the switch.
> + *
> + * One necessary use of the VLAN membership configuration database is for the
> + * programming of a port-based VLAN ID (PVID). The PVID is programmed on a
> + * per-port basis via register field, which refers to a specific VLAN membership
> + * configuration via an index 0~31. In order to maintain coherent behaviour on a
> + * port with a PVID, it is necessary to keep the VLAN configuration synchronized
> + * between the VLAN4k table and the VLAN membership configuration database.
> + *
> + * Since VLAN membership configs are a scarce resource, it will only be used
> + * when strictly needed (i.e. a VLAN with members using PVID). Otherwise, the
> + * VLAN4k will be enough.
> + *
> + * With some exceptions, the entries in both the VLAN4k table and the VLAN
> + * membership configuration database offer the same configuration options. The
> + * differences are as follows:
> + *
> + * 1. VLAN4k entries can specify whether to use Independent or Shared VLAN
> + * Learning (IVL or SVL respectively). VLAN membership config entries
> + * cannot. This underscores the fact that VLAN membership configs are not
> + * involved in the learning process of the ASIC.
> + *
> + * 2. VLAN membership config entries use an "enhanced VLAN ID" (efid), which has
> + * a range 0~8191 compared with the standard 0~4095 range of the VLAN4k
> + * table. This underscores the fact that VLAN membership configs can be used
> + * to group ports on a layer beyond the standard VLAN configuration, which
> + * may be useful for ACL rules which specify alternative forwarding
> + * decisions.
> + *
> + * VLANMC index 0 is reserved as a neutral PVID, used for vlan-unaware ports.
> + *
> + */
> +
> +#include "rtl8365mb_vlan.h"
> +#include "rtl8365mb_table.h"
> +#include <linux/if_bridge.h>
> +#include <linux/regmap.h>
> +
> +/* CVLAN (i.e. VLAN4k) table entry layout, u16[3] */
> +#define RTL8365MB_CVLAN_ENTRY_SIZE 3 /* 48-bits */
> +#define RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK GENMASK(7, 0)
> +#define RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK GENMASK(15, 8)
> +#define RTL8365MB_CVLAN_ENTRY_D1_FID_MASK GENMASK(3, 0)
> +#define RTL8365MB_CVLAN_ENTRY_D1_VBPEN_MASK GENMASK(4, 4)
> +#define RTL8365MB_CVLAN_ENTRY_D1_VBPRI_MASK GENMASK(7, 5)
> +#define RTL8365MB_CVLAN_ENTRY_D1_ENVLANPOL_MASK GENMASK(8, 8)
> +#define RTL8365MB_CVLAN_ENTRY_D1_METERIDX_MASK GENMASK(13, 9)
> +#define RTL8365MB_CVLAN_ENTRY_D1_IVL_SVL_MASK GENMASK(14, 14)
> +/* extends RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK */
> +#define RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK GENMASK(2, 0)
> +/* extends RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK */
> +#define RTL8365MB_CVLAN_ENTRY_D2_UNTAG_EXT_MASK GENMASK(5, 3)
> +/* extends RTL8365MB_CVLAN_ENTRY_D1_METERIDX_MASK */
> +#define RTL8365MB_CVLAN_ENTRY_D2_METERIDX_EXT_MASK GENMASK(6, 6)
> +
> +/* VLAN member configuration registers 0~31, u16[3] */
> +#define RTL8365MB_VLAN_MC_BASE 0x0728
> +#define RTL8365MB_VLAN_MC_ENTRY_SIZE 4 /* 64-bit */
> +#define RTL8365MB_VLAN_MC_REG(index) \
> + (RTL8365MB_VLAN_MC_BASE + \
> + (RTL8365MB_VLAN_MC_ENTRY_SIZE * (index)))
> +#define RTL8365MB_VLAN_MC_D0_MBR_MASK GENMASK(10, 0)
> +#define RTL8365MB_VLAN_MC_D1_FID_MASK GENMASK(3, 0)
> +
> +#define RTL8365MB_VLAN_MC_D2_VBPEN_MASK GENMASK(0, 0)
> +#define RTL8365MB_VLAN_MC_D2_VBPRI_MASK GENMASK(3, 1)
> +#define RTL8365MB_VLAN_MC_D2_ENVLANPOL_MASK GENMASK(4, 4)
> +#define RTL8365MB_VLAN_MC_D2_METERIDX_MASK GENMASK(10, 5)
> +#define RTL8365MB_VLAN_MC_D3_EVID_MASK GENMASK(12, 0)
> +
> +/* Some limits for VLAN4k/VLAN membership config entries */
> +#define RTL8365MB_PRIORITYMAX 7
> +#define RTL8365MB_FIDMAX 15
> +#define RTL8365MB_METERMAX 63
> +#define RTL8365MB_VLAN_MCMAX 31
> +
> +/* RTL8367S supports 4k vlans (vid<=4095) and 32 enhanced vlans
> + * for VIDs up to 8191
> + */
> +#define RTL8365MB_MAX_4K_VID 0x0FFF /* 4095 */
> +#define RTL8365MB_MAX_MC_VID 0x1FFF /* 8191 */
> +
> + /* Port-based VID registers 0~5 - each one holds an MC index for two ports */
> +#define RTL8365MB_VLAN_PVID_CTRL_BASE 0x0700
> +#define RTL8365MB_VLAN_PVID_CTRL_REG(_p) \
> + (RTL8365MB_VLAN_PVID_CTRL_BASE + ((_p) >> 1))
> +#define RTL8365MB_VLAN_PVID_CTRL_PORT0_MCIDX_MASK 0x001F
> +#define RTL8365MB_VLAN_PVID_CTRL_PORT1_MCIDX_MASK 0x1F00
> +#define RTL8365MB_VLAN_PVID_CTRL_PORT_MCIDX_OFFSET(_p) \
> + (((_p) & 1) << 3)
> +#define RTL8365MB_VLAN_PVID_CTRL_PORT_MCIDX_MASK(_p) \
> + (0x1F << RTL8365MB_VLAN_PVID_CTRL_PORT_MCIDX_OFFSET(_p))
> +
> +/* Frame type filtering registers */
> +#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_BASE 0x07aa
> +#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_REG(port) \
> + (RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_BASE + ((port) >> 3))
> +/* required as FIELD_PREP cannot use non-constant masks */
> +#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_MASK(port) \
> + (0x3 << RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_OFFSET(port))
> +#define RTL8365MB_VLAN_ACCEPT_FRAME_TYPE_OFFSET(port) \
> + (((port) & 0x7) << 1)
> +
> +/**
> + * struct rtl8365mb_vlan4k - VLAN4k table entry
> + * @vid: VLAN ID (0~4095)
> + * @member: port mask of ports in this VLAN
> + * @untag: port mask of ports which untag on egress
> + * @fid: filter ID - only used with SVL (unused)
> + * @priority: priority classification (unused)
> + * @priority_en: enable priority (unused)
> + * @policing_en: enable policing (unused)
> + * @ivl_en: enable IVL instead of default SVL
> + * @meteridx: metering index (unused)
> + *
> + * This structure is used to get/set entries in the VLAN4k table. The
> + * VLAN4k table dictates the VLAN configuration for the switch for the
> + * vast majority of features.
> + */
> +struct rtl8365mb_vlan4k {
> + u16 vid;
> + u16 member;
> + u16 untag;
> + u8 fid : 4;
> + u8 priority : 3;
> + u8 priority_en : 1;
> + u8 policing_en : 1;
> + u8 ivl_en : 1;
> + u8 meteridx : 6;
> +};
> +
> +/**
> + * struct rtl8365mb_vlanmc - VLAN membership config
> + * @evid: Enhanced VLAN ID (0~8191)
> + * @member: port mask of ports in this VLAN
> + * @fid: filter ID - only used with SVL (unused)
> + * @priority: priority classification (unused)
> + * @priority_en: enable priority (unused)
> + * @policing_en: enable policing (unused)
> + * @meteridx: metering index (unused)
> + *
> + * This structure is used to get/set entries in the VLAN membership
> + * configuration database. This feature is largely vestigial, but
> + * still needed for at least the following features:
> + * - PVID configuration
> + * - ACL configuration
> + * - selection of VLAN by the CPU tag when VSEL=1, although the switch
> + * can also select VLAN based on the VLAN tag if VSEL=0
> + *
> + * This is a low-level structure and it is recommended to interface with
> + * the VLAN membership config database via &struct rtl8365mb_vlanmc_entry.
> + */
> +struct rtl8365mb_vlanmc {
> + u16 evid;
> + u16 member;
> + u8 fid : 4;
> + u8 priority : 3;
> + u8 priority_en : 1;
> + u8 policing_en : 1;
> + u8 meteridx : 6;
> +};
> +
> +enum rtl8365mb_frame_ingress {
> + RTL8365MB_FRAME_TYPE_ANY_FRAME = 0,
> + RTL8365MB_FRAME_TYPE_TAGGED_ONLY,
> + RTL8365MB_FRAME_TYPE_UNTAGGED_ONLY,
> +};
> +
> +static int rtl8365mb_vlan_4k_read(struct realtek_priv *priv, u16 vid,
> + struct rtl8365mb_vlan4k *vlan4k)
> +{
> + u16 data[RTL8365MB_CVLAN_ENTRY_SIZE];
> + int ret;
> +
> + ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_CVLAN,
> + RTL8365MB_TABLE_OP_READ, &vid, 0, 0,
> + data, ARRAY_SIZE(data));
> + if (ret)
> + return ret;
> +
> + /* Unpack table entry */
> + memset(vlan4k, 0, sizeof(*vlan4k));
> + vlan4k->vid = vid;
> + vlan4k->member =
> + FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, data[0]) |
> + (FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2])
> + << FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK));
This FIELD_GET() << FIELD_WIDTH() resembles FIELD_PREP(), except that
you skip some checks. Is that intentional?
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
2026-04-01 2:27 ` Yury Norov
@ 2026-04-02 2:45 ` Luiz Angelo Daros de Luca
2026-04-02 14:22 ` Yury Norov
0 siblings, 1 reply; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-04-02 2:45 UTC (permalink / raw)
To: Yury Norov
Cc: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King,
netdev, linux-kernel
> > + vlan4k->vid = vid;
> > + vlan4k->member =
> > + FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, data[0]) |
> > + (FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2])
> > + << FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK));
>
> This FIELD_GET() << FIELD_WIDTH() resembles FIELD_PREP(), except that
> you skip some checks. Is that intentional?
While it resembles FIELD_PREP(), the usage of FIELD_GET() <<
FIELD_WIDTH() is intentional and semantically distinct from
FIELD_PREP(). While FIELD_PREP() prepares a raw value to be written up
into a register mask, we are performing a read/unpack operation here:
extracting bits from hardware registers to populate the driver's
internal logical structure.
Using FIELD_PREP() for this would require defining a 'fake' software
mask (e.g., GENMASK(10, 8)) that does not correspond to any physical
register, which would be more confusing to future maintainers than the
current explicit reassembly.
The 11-bit member field is split across two non-contiguous 16-bit
words in the CVLAN entry:
- data[0] (D0): Contains the primary bits [7:0] via
RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK.
- data[2] (D2): Contains the extension bits [10:8] via
RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK.
This split layout suggests the hardware was originally designed for 8
ports and later expanded to 11, placing the extra bits in the
previously unused space in data[2].
Even if we concatenate them into a single 48-bit value, a single
bitfield macro would not handle the non-contiguous mask.
Regards,
Luiz
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-04-01 2:15 ` Yury Norov
@ 2026-04-02 4:00 ` Luiz Angelo Daros de Luca
2026-04-02 9:27 ` David Laight
2026-04-02 13:45 ` Yury Norov
0 siblings, 2 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-04-02 4:00 UTC (permalink / raw)
To: Yury Norov
Cc: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King,
netdev, linux-kernel
> > +/**
> > + * FIELD_WIDTH() - return the width of a bitfield
> > + * @_mask: shifted mask defining the field's length and position
> > + *
> > + * Returns the number of contiguous bits covered by @_mask.
> > + * This corresponds to the bit width of FIELD_MAX(@_mask).
> > + */
> > +#define FIELD_WIDTH(_mask) \
>
> Please no underscored names unless necessary.
I used _mask to maintain consistency with the existing public macros
in this file, such as FIELD_GET, FIELD_PREP, FIELD_MAX, and FIELD_FIT.
All of them use the underscore prefix for parameters. Should I diverge
from them?
> > + ({ \
> > + __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_WIDTH: "); \
> > + __bf_shf(~FIELD_MAX(_mask)); \
> > + })
>
> I believe, this should be:
>
> #define FIELD_WIDTH(mask) ({ \
> __BF_FIELD_CHECK_MASK(mask, 0ULL, "FIELD_WIDTH: "); \
> HWEIGHT(mask); \
> })
HWEIGHT() is indeed much cleaner. However, to keep bitfield.h
self-contained and avoid adding more includes, I'll try
__builtin_popcountll() in a similar way __builtin_ffsll is already
used.
I also noticed the suggestion to use __BF_FIELD_CHECK_MASK instead of
__BF_FIELD_CHECK. Both FIELD_MAX and FIELD_FIT currently use the full
__BF_FIELD_CHECK with 0ULL as a dummy register to ensure the mask fits
within the header's supported limits. If __BF_FIELD_CHECK_MASK is
preferred for FIELD_WIDTH, I can certainly use it, but should we also
update FIELD_MAX and FIELD_FIT for consistency?
I intend to send a v2 with the following implementation:
#define __bf_shf(x) (__builtin_ffsll(x) - 1)
+#define __bf_hweight(x) __builtin_popcountll(x)
#define __scalar_type_to_unsigned_cases(type) \
unsigned type: (unsigned type)0, \
@@ -111,6 +112,19 @@
(typeof(_mask))((_mask) >> __bf_shf(_mask)); \
})
+/**
+ * FIELD_WIDTH() - return the width of a bitfield
+ * @_mask: shifted mask defining the field's length and position
+ *
+ * Returns the number of contiguous bits covered by @_mask.
+ * This corresponds to the bit width of FIELD_MAX(@_mask).
+ */
+#define FIELD_WIDTH(_mask) \
+ ({ \
+ __BF_FIELD_CHECK_MASK(_mask, 0ULL, "FIELD_WIDTH: "); \
+ (typeof(_mask))__bf_hweight(_mask); \
+ })
+
Regards,
Luiz
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-04-02 4:00 ` Luiz Angelo Daros de Luca
@ 2026-04-02 9:27 ` David Laight
2026-04-02 13:52 ` Yury Norov
2026-04-02 13:45 ` Yury Norov
1 sibling, 1 reply; 24+ messages in thread
From: David Laight @ 2026-04-02 9:27 UTC (permalink / raw)
To: Luiz Angelo Daros de Luca
Cc: Yury Norov, Andrew Lunn, Vladimir Oltean, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Linus Walleij, Alvin Šipraga, Yury Norov, Rasmus Villemoes,
Russell King, netdev, linux-kernel
On Thu, 2 Apr 2026 01:00:20 -0300
Luiz Angelo Daros de Luca <luizluca@gmail.com> wrote:
> > > +/**
> > > + * FIELD_WIDTH() - return the width of a bitfield
> > > + * @_mask: shifted mask defining the field's length and position
> > > + *
> > > + * Returns the number of contiguous bits covered by @_mask.
> > > + * This corresponds to the bit width of FIELD_MAX(@_mask).
> > > + */
> > > +#define FIELD_WIDTH(_mask) \
> >
> > Please no underscored names unless necessary.
>
> I used _mask to maintain consistency with the existing public macros
> in this file, such as FIELD_GET, FIELD_PREP, FIELD_MAX, and FIELD_FIT.
> All of them use the underscore prefix for parameters. Should I diverge
> from them?
>
> > > + ({ \
> > > + __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_WIDTH: "); \
> > > + __bf_shf(~FIELD_MAX(_mask)); \
> > > + })
> >
> > I believe, this should be:
> >
> > #define FIELD_WIDTH(mask) ({ \
> > __BF_FIELD_CHECK_MASK(mask, 0ULL, "FIELD_WIDTH: "); \
> > HWEIGHT(mask); \
> > })
>
> HWEIGHT() is indeed much cleaner. However, to keep bitfield.h
> self-contained and avoid adding more includes, I'll try
> __builtin_popcountll() in a similar way __builtin_ffsll is already
> used.
>
> I also noticed the suggestion to use __BF_FIELD_CHECK_MASK instead of
> __BF_FIELD_CHECK. Both FIELD_MAX and FIELD_FIT currently use the full
> __BF_FIELD_CHECK with 0ULL as a dummy register to ensure the mask fits
> within the header's supported limits. If __BF_FIELD_CHECK_MASK is
> preferred for FIELD_WIDTH, I can certainly use it, but should we also
> update FIELD_MAX and FIELD_FIT for consistency?
All of the calls with the 0ULL placeholder (especially for the register)
should really be removed.
Last time I looked there where some calls that only had placeholders.
They just bloat the pre-processor output and slow down compilation.
>
> I intend to send a v2 with the following implementation:
>
> #define __bf_shf(x) (__builtin_ffsll(x) - 1)
> +#define __bf_hweight(x) __builtin_popcountll(x)
>
> #define __scalar_type_to_unsigned_cases(type) \
> unsigned type: (unsigned type)0, \
> @@ -111,6 +112,19 @@
> (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
> })
>
> +/**
> + * FIELD_WIDTH() - return the width of a bitfield
> + * @_mask: shifted mask defining the field's length and position
> + *
> + * Returns the number of contiguous bits covered by @_mask.
> + * This corresponds to the bit width of FIELD_MAX(@_mask).
> + */
> +#define FIELD_WIDTH(_mask) \
> + ({ \
You ought to have:
auto _fw_mask = mask;
here. While _mask has to be a constant, if it comes from GENMASK()
it is very long.
> + __BF_FIELD_CHECK_MASK(_mask, 0ULL, "FIELD_WIDTH: "); \
> + (typeof(_mask))__bf_hweight(_mask); \
Why the cast of the result?
They are everywhere in that file, and many are pointless.
But there is no point adding another one.
I'm not even sure you need the extra define.
Just use __builtin_popcountll().
David
> + })
> +
>
> Regards,
>
> Luiz
>
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-04-02 4:00 ` Luiz Angelo Daros de Luca
2026-04-02 9:27 ` David Laight
@ 2026-04-02 13:45 ` Yury Norov
1 sibling, 0 replies; 24+ messages in thread
From: Yury Norov @ 2026-04-02 13:45 UTC (permalink / raw)
To: Luiz Angelo Daros de Luca
Cc: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King,
netdev, linux-kernel
On Thu, Apr 02, 2026 at 01:00:20AM -0300, Luiz Angelo Daros de Luca wrote:
> > > +/**
> > > + * FIELD_WIDTH() - return the width of a bitfield
> > > + * @_mask: shifted mask defining the field's length and position
> > > + *
> > > + * Returns the number of contiguous bits covered by @_mask.
> > > + * This corresponds to the bit width of FIELD_MAX(@_mask).
> > > + */
> > > +#define FIELD_WIDTH(_mask) \
> >
> > Please no underscored names unless necessary.
>
> I used _mask to maintain consistency with the existing public macros
> in this file, such as FIELD_GET, FIELD_PREP, FIELD_MAX, and FIELD_FIT.
> All of them use the underscore prefix for parameters. Should I diverge
> from them?
There's no consistency, but if you look at the recently added code,
you'll find it doesn't add those underscored prefixes. If you want
them, it's OK. Just explain what the hell do they mean?
> > > + ({ \
> > > + __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_WIDTH: "); \
> > > + __bf_shf(~FIELD_MAX(_mask)); \
> > > + })
> >
> > I believe, this should be:
> >
> > #define FIELD_WIDTH(mask) ({ \
> > __BF_FIELD_CHECK_MASK(mask, 0ULL, "FIELD_WIDTH: "); \
> > HWEIGHT(mask); \
> > })
>
> HWEIGHT() is indeed much cleaner. However, to keep bitfield.h
> self-contained and avoid adding more includes, I'll try
> __builtin_popcountll() in a similar way __builtin_ffsll is already
> used.
>
> I also noticed the suggestion to use __BF_FIELD_CHECK_MASK instead of
> __BF_FIELD_CHECK. Both FIELD_MAX and FIELD_FIT currently use the full
> __BF_FIELD_CHECK with 0ULL as a dummy register to ensure the mask fits
> within the header's supported limits. If __BF_FIELD_CHECK_MASK is
> preferred for FIELD_WIDTH, I can certainly use it, but should we also
> update FIELD_MAX and FIELD_FIT for consistency?
>
> I intend to send a v2 with the following implementation:
>
> #define __bf_shf(x) (__builtin_ffsll(x) - 1)
> +#define __bf_hweight(x) __builtin_popcountll(x)
So, we've got only 2 __bf_xxx() functions here:
#define __bf_shf(x) (__builtin_ffsll(x) - 1)
#define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x))
They both do something with the builtins. Your __bf_hweight() is a
pure redefinition. What for do you need it? Why not just use that
compiler-provided popcount() that everybody knows?
> #define __scalar_type_to_unsigned_cases(type) \
> unsigned type: (unsigned type)0, \
> @@ -111,6 +112,19 @@
> (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
> })
>
> +/**
> + * FIELD_WIDTH() - return the width of a bitfield
> + * @_mask: shifted mask defining the field's length and position
> + *
> + * Returns the number of contiguous bits covered by @_mask.
> + * This corresponds to the bit width of FIELD_MAX(@_mask).
> + */
> +#define FIELD_WIDTH(_mask) \
> + ({ \
> + __BF_FIELD_CHECK_MASK(_mask, 0ULL, "FIELD_WIDTH: "); \
> + (typeof(_mask))__bf_hweight(_mask); \
No need to typecast the result.
> + })
> +
>
> Regards,
>
> Luiz
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-04-02 9:27 ` David Laight
@ 2026-04-02 13:52 ` Yury Norov
2026-04-02 22:21 ` David Laight
0 siblings, 1 reply; 24+ messages in thread
From: Yury Norov @ 2026-04-02 13:52 UTC (permalink / raw)
To: David Laight
Cc: Luiz Angelo Daros de Luca, Andrew Lunn, Vladimir Oltean,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Linus Walleij, Alvin Šipraga, Yury Norov,
Rasmus Villemoes, Russell King, netdev, linux-kernel
On Thu, Apr 02, 2026 at 10:27:17AM +0100, David Laight wrote:
> On Thu, 2 Apr 2026 01:00:20 -0300
> Luiz Angelo Daros de Luca <luizluca@gmail.com> wrote:
>
> > > > +/**
> > > > + * FIELD_WIDTH() - return the width of a bitfield
> > > > + * @_mask: shifted mask defining the field's length and position
> > > > + *
> > > > + * Returns the number of contiguous bits covered by @_mask.
> > > > + * This corresponds to the bit width of FIELD_MAX(@_mask).
> > > > + */
> > > > +#define FIELD_WIDTH(_mask) \
> > >
> > > Please no underscored names unless necessary.
> >
> > I used _mask to maintain consistency with the existing public macros
> > in this file, such as FIELD_GET, FIELD_PREP, FIELD_MAX, and FIELD_FIT.
> > All of them use the underscore prefix for parameters. Should I diverge
> > from them?
> >
> > > > + ({ \
> > > > + __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_WIDTH: "); \
> > > > + __bf_shf(~FIELD_MAX(_mask)); \
> > > > + })
> > >
> > > I believe, this should be:
> > >
> > > #define FIELD_WIDTH(mask) ({ \
> > > __BF_FIELD_CHECK_MASK(mask, 0ULL, "FIELD_WIDTH: "); \
> > > HWEIGHT(mask); \
> > > })
> >
> > HWEIGHT() is indeed much cleaner. However, to keep bitfield.h
> > self-contained and avoid adding more includes, I'll try
> > __builtin_popcountll() in a similar way __builtin_ffsll is already
> > used.
> >
> > I also noticed the suggestion to use __BF_FIELD_CHECK_MASK instead of
> > __BF_FIELD_CHECK. Both FIELD_MAX and FIELD_FIT currently use the full
> > __BF_FIELD_CHECK with 0ULL as a dummy register to ensure the mask fits
> > within the header's supported limits. If __BF_FIELD_CHECK_MASK is
> > preferred for FIELD_WIDTH, I can certainly use it, but should we also
> > update FIELD_MAX and FIELD_FIT for consistency?
>
> All of the calls with the 0ULL placeholder (especially for the register)
> should really be removed.
> Last time I looked there where some calls that only had placeholders.
> They just bloat the pre-processor output and slow down compilation.
Have you any numbers? Can you send a patch?
> > I intend to send a v2 with the following implementation:
> >
> > #define __bf_shf(x) (__builtin_ffsll(x) - 1)
> > +#define __bf_hweight(x) __builtin_popcountll(x)
> >
> > #define __scalar_type_to_unsigned_cases(type) \
> > unsigned type: (unsigned type)0, \
> > @@ -111,6 +112,19 @@
> > (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
> > })
> >
> > +/**
> > + * FIELD_WIDTH() - return the width of a bitfield
> > + * @_mask: shifted mask defining the field's length and position
> > + *
> > + * Returns the number of contiguous bits covered by @_mask.
> > + * This corresponds to the bit width of FIELD_MAX(@_mask).
> > + */
> > +#define FIELD_WIDTH(_mask) \
> > + ({ \
>
> You ought to have:
> auto _fw_mask = mask;
> here. While _mask has to be a constant, if it comes from GENMASK()
> it is very long.
Yes, but what this _fw means? Here it could be just auto __mask = mask.
That is what the underscores are used.
> > + __BF_FIELD_CHECK_MASK(_mask, 0ULL, "FIELD_WIDTH: "); \
> > + (typeof(_mask))__bf_hweight(_mask); \
>
> Why the cast of the result?
> They are everywhere in that file, and many are pointless.
> But there is no point adding another one.
>
> I'm not even sure you need the extra define.
> Just use __builtin_popcountll().
For __BF_FIELD_CHECK_MASK() check, I guess.
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
2026-04-02 2:45 ` Luiz Angelo Daros de Luca
@ 2026-04-02 14:22 ` Yury Norov
0 siblings, 0 replies; 24+ messages in thread
From: Yury Norov @ 2026-04-02 14:22 UTC (permalink / raw)
To: Luiz Angelo Daros de Luca
Cc: Andrew Lunn, Vladimir Oltean, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Linus Walleij,
Alvin Šipraga, Yury Norov, Rasmus Villemoes, Russell King,
netdev, linux-kernel
On Wed, Apr 01, 2026 at 11:45:18PM -0300, Luiz Angelo Daros de Luca wrote:
> > > + vlan4k->vid = vid;
> > > + vlan4k->member =
> > > + FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, data[0]) |
> > > + (FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2])
> > > + << FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK));
> >
> > This FIELD_GET() << FIELD_WIDTH() resembles FIELD_PREP(), except that
> > you skip some checks. Is that intentional?
>
> While it resembles FIELD_PREP(), the usage of FIELD_GET() <<
> FIELD_WIDTH() is intentional and semantically distinct from
> FIELD_PREP(). While FIELD_PREP() prepares a raw value to be written up
> into a register mask, we are performing a read/unpack operation here:
> extracting bits from hardware registers to populate the driver's
> internal logical structure.
>
> Using FIELD_PREP() for this would require defining a 'fake' software
> mask (e.g., GENMASK(10, 8)) that does not correspond to any physical
> register, which would be more confusing to future maintainers than the
> current explicit reassembly.
Not sure. You can add a nice comment on top of those GENMASKS(),
explaining what actually is happening there. Long term it always
better to avoid opencoded shifts. It's proven by decades of coding.
All that FIELD() machinery is aimed to avoid shifts and other
opencoded bit manipulations, because they are proven to be a constant
source of errors. And now you use that FIELD_WIDTH() exactly for that.
The alternative would look like:
lo = FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, data[0]);
hi = FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2]);
vlan4k->member = FIELD_PREP(RTL8365MB_CVLAN_ENTRY_MBR_MASK_LO, lo) |
FIELD_PREP(RTL8365MB_CVLAN_ENTRY_MBR_EXT_MASK_HI, hi);
I'm not going to teach you how to wright your driver, but you see -
it's 4 lines vs 4 lines, and no opencoded bit ops. And I'm pretty sure
you'll find plenty examples in the kernel where people construct
registers from pieces this way.
Thanks,
Yury
> The 11-bit member field is split across two non-contiguous 16-bit
> words in the CVLAN entry:
> - data[0] (D0): Contains the primary bits [7:0] via
> RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK.
> - data[2] (D2): Contains the extension bits [10:8] via
> RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK.
>
> This split layout suggests the hardware was originally designed for 8
> ports and later expanded to 11, placing the extra bits in the
> previously unused space in data[2].
> Even if we concatenate them into a single 48-bit value, a single
> bitfield macro would not handle the non-contiguous mask.
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-04-02 13:52 ` Yury Norov
@ 2026-04-02 22:21 ` David Laight
2026-04-03 14:09 ` Luiz Angelo Daros de Luca
0 siblings, 1 reply; 24+ messages in thread
From: David Laight @ 2026-04-02 22:21 UTC (permalink / raw)
To: Yury Norov
Cc: Luiz Angelo Daros de Luca, Andrew Lunn, Vladimir Oltean,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Linus Walleij, Alvin Šipraga, Yury Norov,
Rasmus Villemoes, Russell King, netdev, linux-kernel
On Thu, 2 Apr 2026 09:52:49 -0400
Yury Norov <ynorov@nvidia.com> wrote:
> On Thu, Apr 02, 2026 at 10:27:17AM +0100, David Laight wrote:
> > On Thu, 2 Apr 2026 01:00:20 -0300
> > Luiz Angelo Daros de Luca <luizluca@gmail.com> wrote:
> >
> > > > > +/**
> > > > > + * FIELD_WIDTH() - return the width of a bitfield
> > > > > + * @_mask: shifted mask defining the field's length and position
> > > > > + *
> > > > > + * Returns the number of contiguous bits covered by @_mask.
> > > > > + * This corresponds to the bit width of FIELD_MAX(@_mask).
> > > > > + */
> > > > > +#define FIELD_WIDTH(_mask) \
> > > >
> > > > Please no underscored names unless necessary.
> > >
> > > I used _mask to maintain consistency with the existing public macros
> > > in this file, such as FIELD_GET, FIELD_PREP, FIELD_MAX, and FIELD_FIT.
> > > All of them use the underscore prefix for parameters. Should I diverge
> > > from them?
> > >
> > > > > + ({ \
> > > > > + __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_WIDTH: "); \
> > > > > + __bf_shf(~FIELD_MAX(_mask)); \
> > > > > + })
> > > >
> > > > I believe, this should be:
> > > >
> > > > #define FIELD_WIDTH(mask) ({ \
> > > > __BF_FIELD_CHECK_MASK(mask, 0ULL, "FIELD_WIDTH: "); \
> > > > HWEIGHT(mask); \
> > > > })
> > >
> > > HWEIGHT() is indeed much cleaner. However, to keep bitfield.h
> > > self-contained and avoid adding more includes, I'll try
> > > __builtin_popcountll() in a similar way __builtin_ffsll is already
> > > used.
> > >
> > > I also noticed the suggestion to use __BF_FIELD_CHECK_MASK instead of
> > > __BF_FIELD_CHECK. Both FIELD_MAX and FIELD_FIT currently use the full
> > > __BF_FIELD_CHECK with 0ULL as a dummy register to ensure the mask fits
> > > within the header's supported limits. If __BF_FIELD_CHECK_MASK is
> > > preferred for FIELD_WIDTH, I can certainly use it, but should we also
> > > update FIELD_MAX and FIELD_FIT for consistency?
> >
> > All of the calls with the 0ULL placeholder (especially for the register)
> > should really be removed.
> > Last time I looked there where some calls that only had placeholders.
> > They just bloat the pre-processor output and slow down compilation.
>
> Have you any numbers? Can you send a patch?
It was all in a patch I send a while back.
Basically GENMASK() is a couple of hundred bytes and FIELD_PREP(GENMASK())
hits a few thousand due to the number of times mask (in particular)
get expanded.
The problem was bit-fields...
>
> > > I intend to send a v2 with the following implementation:
> > >
> > > #define __bf_shf(x) (__builtin_ffsll(x) - 1)
> > > +#define __bf_hweight(x) __builtin_popcountll(x)
> > >
> > > #define __scalar_type_to_unsigned_cases(type) \
> > > unsigned type: (unsigned type)0, \
> > > @@ -111,6 +112,19 @@
> > > (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
> > > })
> > >
> > > +/**
> > > + * FIELD_WIDTH() - return the width of a bitfield
> > > + * @_mask: shifted mask defining the field's length and position
> > > + *
> > > + * Returns the number of contiguous bits covered by @_mask.
> > > + * This corresponds to the bit width of FIELD_MAX(@_mask).
> > > + */
> > > +#define FIELD_WIDTH(_mask) \
> > > + ({ \
> >
> > You ought to have:
> > auto _fw_mask = mask;
> > here. While _mask has to be a constant, if it comes from GENMASK()
> > it is very long.
>
> Yes, but what this _fw means? Here it could be just auto __mask = mask.
> That is what the underscores are used.
The problems arise when the expansions get nested.
Using unique names is compex (and unreadable).
Provided only different defines are nested it is enough to use
different names in each one.
>
> > > + __BF_FIELD_CHECK_MASK(_mask, 0ULL, "FIELD_WIDTH: "); \
> > > + (typeof(_mask))__bf_hweight(_mask); \
> >
> > Why the cast of the result?
> > They are everywhere in that file, and many are pointless.
> > But there is no point adding another one.
> >
> > I'm not even sure you need the extra define.
> > Just use __builtin_popcountll().
>
> For __BF_FIELD_CHECK_MASK() check, I guess.
I meant why have __bf_hweight().
David
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-04-02 22:21 ` David Laight
@ 2026-04-03 14:09 ` Luiz Angelo Daros de Luca
2026-04-03 16:18 ` Yury Norov
` (2 more replies)
0 siblings, 3 replies; 24+ messages in thread
From: Luiz Angelo Daros de Luca @ 2026-04-03 14:09 UTC (permalink / raw)
To: David Laight
Cc: Yury Norov, Andrew Lunn, Vladimir Oltean, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Linus Walleij, Alvin Šipraga, Yury Norov, Rasmus Villemoes,
Russell King, netdev, linux-kernel
Thanks David and Yuri,
This patch was originally intended to reconstruct a value split across
multiple registers. Originally:
vlan4k->member =
FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, data[0]) |
(FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2])
<< FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK));
However, as Yuri pointed out, I can use FIELD_PREP() to align the bits
into the final struct member without manual bitops. While less common,
this pattern is used elsewhere (e.g., drivers/clk/clk-lmk04832.c). The
updated approach would be:
vlan4k->member =
FIELD_PREP(RTL8365MB_CVLAN_MBR_LO_MASK,
FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK,
data[0])) |
FIELD_PREP(RTL8365MB_CVLAN_MBR_HI_MASK,
FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2]));
If this use of FIELD_PREP is preferred, I will drop this FIELD_WIDTH
patch from the series as it would no longer have any users.
Regards,
Luiz
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-04-03 14:09 ` Luiz Angelo Daros de Luca
@ 2026-04-03 16:18 ` Yury Norov
2026-04-04 14:54 ` David Laight
2026-04-04 15:12 ` David Laight
2 siblings, 0 replies; 24+ messages in thread
From: Yury Norov @ 2026-04-03 16:18 UTC (permalink / raw)
To: Luiz Angelo Daros de Luca
Cc: David Laight, Andrew Lunn, Vladimir Oltean, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Linus Walleij, Alvin Šipraga, Yury Norov, Rasmus Villemoes,
Russell King, netdev, linux-kernel
On Fri, Apr 03, 2026 at 11:09:30AM -0300, Luiz Angelo Daros de Luca wrote:
> Thanks David and Yuri,
>
> This patch was originally intended to reconstruct a value split across
> multiple registers. Originally:
>
> vlan4k->member =
> FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, data[0]) |
> (FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2])
> << FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK));
>
> However, as Yuri pointed out, I can use FIELD_PREP() to align the bits
> into the final struct member without manual bitops. While less common,
> this pattern is used elsewhere (e.g., drivers/clk/clk-lmk04832.c). The
> updated approach would be:
>
> vlan4k->member =
> FIELD_PREP(RTL8365MB_CVLAN_MBR_LO_MASK,
> FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK,
> data[0])) |
> FIELD_PREP(RTL8365MB_CVLAN_MBR_HI_MASK,
>
> FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2]));
>
> If this use of FIELD_PREP is preferred, I will drop this FIELD_WIDTH
> patch from the series as it would no longer have any users.
Then, for relevant patches:
Suggested-by: Yury Norov <ynorov@nvidia.com> # for split fields handling
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-04-03 14:09 ` Luiz Angelo Daros de Luca
2026-04-03 16:18 ` Yury Norov
@ 2026-04-04 14:54 ` David Laight
2026-04-04 15:12 ` David Laight
2 siblings, 0 replies; 24+ messages in thread
From: David Laight @ 2026-04-04 14:54 UTC (permalink / raw)
To: Luiz Angelo Daros de Luca
Cc: Yury Norov, Andrew Lunn, Vladimir Oltean, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Linus Walleij, Alvin Šipraga, Yury Norov, Rasmus Villemoes,
Russell King, netdev, linux-kernel
On Fri, 3 Apr 2026 11:09:30 -0300
Luiz Angelo Daros de Luca <luizluca@gmail.com> wrote:
> Thanks David and Yuri,
>
> This patch was originally intended to reconstruct a value split across
> multiple registers. Originally:
>
> vlan4k->member =
> FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, data[0]) |
> (FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2])
> << FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK));
>
> However, as Yuri pointed out, I can use FIELD_PREP() to align the bits
> into the final struct member without manual bitops. While less common,
> this pattern is used elsewhere (e.g., drivers/clk/clk-lmk04832.c). The
> updated approach would be:
>
> vlan4k->member =
> FIELD_PREP(RTL8365MB_CVLAN_MBR_LO_MASK,
> FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK,
> data[0])) |
> FIELD_PREP(RTL8365MB_CVLAN_MBR_HI_MASK,
>
> FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2]));
Q: does the line make any sense to someone reading the code?
I'd also generate the .i file and look at output first.
You might find the line doesn't fit on your screen.
David
>
> If this use of FIELD_PREP is preferred, I will drop this FIELD_WIDTH
> patch from the series as it would no longer have any users.
>
> Regards,
>
> Luiz
^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
2026-04-03 14:09 ` Luiz Angelo Daros de Luca
2026-04-03 16:18 ` Yury Norov
2026-04-04 14:54 ` David Laight
@ 2026-04-04 15:12 ` David Laight
2 siblings, 0 replies; 24+ messages in thread
From: David Laight @ 2026-04-04 15:12 UTC (permalink / raw)
To: Luiz Angelo Daros de Luca
Cc: Yury Norov, Andrew Lunn, Vladimir Oltean, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman,
Linus Walleij, Alvin Šipraga, Yury Norov, Rasmus Villemoes,
Russell King, netdev, linux-kernel
On Fri, 3 Apr 2026 11:09:30 -0300
Luiz Angelo Daros de Luca <luizluca@gmail.com> wrote:
> Thanks David and Yuri,
>
> This patch was originally intended to reconstruct a value split across
> multiple registers. Originally:
>
> vlan4k->member =
> FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, data[0]) |
> (FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2])
> << FIELD_WIDTH(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK));
That calculation isn't even really correct.
It is only right if the low bit of the last mask is set.
I'm not a big fan of FIELD_GET/PREP - they have their use, but get
overused.
David
>
> However, as Yuri pointed out, I can use FIELD_PREP() to align the bits
> into the final struct member without manual bitops. While less common,
> this pattern is used elsewhere (e.g., drivers/clk/clk-lmk04832.c). The
> updated approach would be:
>
> vlan4k->member =
> FIELD_PREP(RTL8365MB_CVLAN_MBR_LO_MASK,
> FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK,
> data[0])) |
> FIELD_PREP(RTL8365MB_CVLAN_MBR_HI_MASK,
>
> FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2]));
>
> If this use of FIELD_PREP is preferred, I will drop this FIELD_WIDTH
> patch from the series as it would no longer have any users.
>
> Regards,
>
> Luiz
>
^ permalink raw reply [flat|nested] 24+ messages in thread
end of thread, other threads:[~2026-04-04 15:12 UTC | newest]
Thread overview: 24+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-31 23:00 [net-next PATCH 00/10] net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 01/10] net: dsa: tag_rtl8_4: update format description Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 02/10] net: dsa: realtek: rtl8365mb: set STP state to disabled early Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 03/10] net: dsa: realtek: rtl8365mb: prepare for multiple source files Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH() Luiz Angelo Daros de Luca
2026-04-01 2:15 ` Yury Norov
2026-04-02 4:00 ` Luiz Angelo Daros de Luca
2026-04-02 9:27 ` David Laight
2026-04-02 13:52 ` Yury Norov
2026-04-02 22:21 ` David Laight
2026-04-03 14:09 ` Luiz Angelo Daros de Luca
2026-04-03 16:18 ` Yury Norov
2026-04-04 14:54 ` David Laight
2026-04-04 15:12 ` David Laight
2026-04-02 13:45 ` Yury Norov
2026-03-31 23:00 ` [net-next PATCH 05/10] net: dsa: realtek: rtl8365mb: add table lookup interface Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support Luiz Angelo Daros de Luca
2026-04-01 2:27 ` Yury Norov
2026-04-02 2:45 ` Luiz Angelo Daros de Luca
2026-04-02 14:22 ` Yury Norov
2026-03-31 23:00 ` [net-next PATCH 07/10] net: dsa: realtek: rtl8365mb: add port_bridge_{join,leave} Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 08/10] net: dsa: realtek: rtl8365mb: add FDB support Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 09/10] net: dsa: realtek: rtl8365mb: add bridge port flags Luiz Angelo Daros de Luca
2026-03-31 23:00 ` [net-next PATCH 10/10] net: dsa: tag_rtl8_4: set KEEP flag Luiz Angelo Daros de Luca
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox