net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support
Subsystems (5)
Luiz Angelo Daros de Luca
maintainercover net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support
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 <[email protected]>
To: Vladimir Oltean <[email protected]>
To: David S. Miller <[email protected]>
To: Eric Dumazet <[email protected]>
To: Jakub Kicinski <[email protected]>
To: Paolo Abeni <[email protected]>
To: Simon Horman <[email protected]>
To: Linus Walleij <[email protected]>
To: Alvin Šipraga <[email protected]>
To: Yury Norov <[email protected]>
To: Rasmus Villemoes <[email protected]>
To: Russell King <[email protected]>
---
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,
Signature · 1 line
Luiz Angelo Daros de Luca <[email protected]>
Luiz Angelo Daros de Luca
maintainerPATCH 1/10 net: dsa: tag_rtl8_4: update format description
From: Alvin Šipraga <[email protected]>
Document the updated tag layout fields (EFID, VSEL/VIDX) and clarify which bits are set/cleared when emitting tags.
--- net/dsa/tag_rtl8_4.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
net/dsa/tag_rtl8_4.c +10 -7
@@ -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 */
Signature · 1 line
2.53.0
Re: [net-next PATCH 01/10] net: dsa: tag_rtl8_4: update format description
Quoted 8 lines · Luiz Angelo Daros de Luca
From: Alvin Šipraga <[email protected]>
Document the updated tag layout fields (EFID, VSEL/VIDX) and clarify which bits are set/cleared when emitting tags.
Co-developed-by Alvin Šipraga <[email protected]>Signed-off-by Alvin Šipraga <[email protected]>Signed-off-by Luiz Angelo Daros de Luca <[email protected]>
Yours,
Linus Walleij
Luiz Angelo Daros de Luca
maintainerPATCH 2/10 net: dsa: realtek: rtl8365mb: set STP state to disabled early
In reply to Luiz Angelo Daros de LucaFrom: Alvin Šipraga <[email protected]>
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.
--- drivers/net/dsa/realtek/rtl8365mb.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/dsa/realtek/rtl8365mb.c +6 -6
@@ -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;
Signature · 1 line
2.53.0
Luiz Angelo Daros de Luca
maintainerPATCH 3/10 net: dsa: realtek: rtl8365mb: prepare for multiple source files
In reply to Luiz Angelo Daros de LucaFrom: Alvin Šipraga <[email protected]>
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.
---
drivers/net/dsa/realtek/Makefile | 1 +
drivers/net/dsa/realtek/{rtl8365mb.c => rtl8365mb_main.c} | 0
2 files changed, 1 insertion(+)drivers/net/dsa/realtek/Makefile +1 -0
@@ -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 \
drivers/net/dsa/realtek/rtl8365mb_main.c renamed from drivers/net/dsa/realtek/rtl8365mb.c +0 -0
Signature · 1 line
2.53.0
Re: [net-next PATCH 03/10] net: dsa: realtek: rtl8365mb: prepare for multiple source files
Quoted 11 lines · Luiz Angelo Daros de Luca
From: Alvin Šipraga <[email protected]>
Rename
rtl8365mb.ctortl8365mb_main.cin preparation for subsequent commits which add additional source files to the driver.The trailing backslash in the
Makefileis deliberate. It allows for new files to be added without clobbering git history.Co-developed-by Alvin Šipraga <[email protected]>Signed-off-by Alvin Šipraga <[email protected]>Signed-off-by Luiz Angelo Daros de Luca <[email protected]>
I can't really see from the patch if the line below the backslash is empty and that is why this bisects fine, but if this compiles fine then OK.
Yours,
Linus Walleij
Luiz Angelo Daros de Luca
maintainerPATCH 4/10 bitfield.h: add FIELD_WIDTH()
In reply to Luiz Angelo Daros de LucaSome 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.
--- include/linux/bitfield.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/linux/bitfield.h +13 -0
@@ -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
Signature · 1 line
2.53.0
Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
Quoted 21 lines · 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()andFIELD_FIT(), and avoids reimplementing bit counting logic in drivers.Signed-off-by Luiz Angelo Daros de Luca <[email protected]>--- include/linux/bitfield.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)include/linux/bitfield.h +0
-0@@ -111,6 +111,19 @@ (typeof (_mask))((_mask) >>__bf_shf (_mask)); \
include/linux/bitfield.h line 121
Quoted 10 lines
include/linux/bitfield.h +8
-0}) +/** +* 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.
include/linux/bitfield.h line 125
Quoted 4 lines
include/linux/bitfield.h +4
-0+ ({ \ +__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
Quoted 8 lines
include/linux/bitfield.h +1
-0+/** * FIELD_FIT() - check if value fits in the field * @_mask: shifted mask defining the field's length and position Signature · 2 lines
-- 2.53.0
Luiz Angelo Daros de Luca
maintainerRe: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
Quoted 10 lines
include/linux/bitfield.h line 121
Quoted 8 lines
include/linux/bitfield.h +8
-0+/** +* 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?
Quoted 11 lines
include/linux/bitfield.h line 125
Quoted 4 lines
include/linux/bitfield.h +4
-0+ ({ \ +__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
Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
Quoted 33 lines · On Thu, 2 Apr 2026 01:00:20 -0300 Luiz Angelo Daros de Luca…
Quoted 10 lines
include/linux/bitfield.h line 121
Quoted 8 lines
include/linux/bitfield.h +8
-0+/** +* 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
_maskto maintain consistency with the existing public macros in this file, such asFIELD_GET,FIELD_PREP,FIELD_MAX, andFIELD_FIT. All of them use the underscore prefix for parameters. Should I diverge from them?Quoted 11 lines
include/linux/bitfield.h line 125
Quoted 4 lines
include/linux/bitfield.h +4
-0+ ({ \ +__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 keepbitfield.hself-contained and avoid adding more includes, I'll try__builtin_popcountll()in a similar way__builtin_ffsllis already used.
Quoted 6 lines
I also noticed the suggestion to use
__BF_FIELD_CHECK_MASKinstead of__BF_FIELD_CHECK. BothFIELD_MAXandFIELD_FITcurrently use the full__BF_FIELD_CHECKwith 0ULL as a dummy register to ensure the mask fits within the header's supported limits. If__BF_FIELD_CHECK_MASKis preferred forFIELD_WIDTH, I can certainly use it, but should we also updateFIELD_MAXandFIELD_FITfor 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.
Quoted 11 lines
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, \include/linux/bitfield.h +0
-0@@ -111,6 +112,19 @@ (typeof (_mask))((_mask) >>__bf_shf (_mask)); \ })
include/linux/bitfield.h line 123
Quoted 10 lines
include/linux/bitfield.h +9
-0+/** +* 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.
Review line 125
Quoted 2 lines
(quoted diff) +2
-0+ __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
Quoted 7 lines
(quoted diff) +2
-0+ }) + Regards,Luiz
Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
Quoted 43 lines · David Laight
Quoted 33 lines · On Thu, 2 Apr 2026 01:00:20 -0300 Luiz Angelo Daros de Luca…
Quoted 10 lines
include/linux/bitfield.h line 121
Quoted 8 lines
include/linux/bitfield.h +8
-0+/** +* 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
_maskto maintain consistency with the existing public macros in this file, such asFIELD_GET,FIELD_PREP,FIELD_MAX, andFIELD_FIT. All of them use the underscore prefix for parameters. Should I diverge from them?Quoted 11 lines
include/linux/bitfield.h line 125
Quoted 4 lines
include/linux/bitfield.h +4
-0+ ({ \ +__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 keepbitfield.hself-contained and avoid adding more includes, I'll try__builtin_popcountll()in a similar way__builtin_ffsllis already used.Quoted 6 lines
I also noticed the suggestion to use
__BF_FIELD_CHECK_MASKinstead of__BF_FIELD_CHECK. BothFIELD_MAXandFIELD_FITcurrently use the full__BF_FIELD_CHECKwith 0ULL as a dummy register to ensure the mask fits within the header's supported limits. If__BF_FIELD_CHECK_MASKis preferred forFIELD_WIDTH, I can certainly use it, but should we also updateFIELD_MAXandFIELD_FITfor consistency?
Quoted 4 lines
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?
Quoted 22 lines
Quoted 10 lines
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, \include/linux/bitfield.h +0
-0@@ -111,6 +112,19 @@ (typeof (_mask))((_mask) >>__bf_shf (_mask)); \ })include/linux/bitfield.h line 123
Quoted 10 lines
include/linux/bitfield.h +9
-0+/** +* 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:
Quoted 3 lines
auto _fw_mask = mask;here. While
_maskhas to be a constant, if it comes fromGENMASK()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.
Quoted 9 lines
Review line 125
Quoted 2 lines
(quoted diff) +2
-0+ __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.
Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
Quoted 44 lines · On Thu, 2 Apr 2026 09:52:49 -0400 Yury Norov <[email protected]> wrote:
Quoted 43 lines · David Laight
Quoted 39 lines · On Thu, 2 Apr 2026 01:00:20 -0300 Luiz Angelo Daros de Luca…
Quoted 10 lines
include/linux/bitfield.h line 121
Quoted 8 lines
include/linux/bitfield.h +8
-0+/** +* 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
_maskto maintain consistency with the existing public macros in this file, such asFIELD_GET,FIELD_PREP,FIELD_MAX, andFIELD_FIT. All of them use the underscore prefix for parameters. Should I diverge from them?Quoted 11 lines
include/linux/bitfield.h line 125
Quoted 4 lines
include/linux/bitfield.h +4
-0+ ({ \ +__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 keepbitfield.hself-contained and avoid adding more includes, I'll try__builtin_popcountll()in a similar way__builtin_ffsllis already used.I also noticed the suggestion to use
__BF_FIELD_CHECK_MASKinstead of__BF_FIELD_CHECK. BothFIELD_MAXandFIELD_FITcurrently use the full__BF_FIELD_CHECKwith 0ULL as a dummy register to ensure the mask fits within the header's supported limits. If__BF_FIELD_CHECK_MASKis preferred forFIELD_WIDTH, I can certainly use it, but should we also updateFIELD_MAXandFIELD_FITfor consistency?
Quoted 6 lines
Quoted 4 lines
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...
Quoted 27 lines
Quoted 23 lines
Quoted 11 lines
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, \include/linux/bitfield.h +0
-0@@ -111,6 +112,19 @@ (typeof (_mask))((_mask) >>__bf_shf (_mask)); \ })include/linux/bitfield.h line 123
Quoted 10 lines
include/linux/bitfield.h +9
-0+/** +* 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:
Quoted 3 lines
auto _fw_mask = mask;here. While
_maskhas to be a constant, if it comes fromGENMASK()it is very long.
Quoted 2 lines
Yes, but what this
_fwmeans? 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.
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
(quoted diff) +2
-0+ __BF_FIELD_CHECK_MASK(_mask, 0ULL, "FIELD_WIDTH: "); \ + (typeof(_mask))__bf_hweight(_mask); \
Quoted 8 lines
Quoted 6 lines
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
Luiz Angelo Daros de Luca
maintainerRe: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
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
Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
Quoted 21 lines · Luiz Angelo Daros de Luca
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,
Quoted 4 lines
FIELD_GET(RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK, data[2]));If this use of
FIELD_PREPis preferred, I will drop thisFIELD_WIDTHpatch from the series as it would no longer have any users.
Then, for relevant patches:
Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
In reply to Luiz Angelo Daros de LucaQuoted 15 lines · On Fri, 3 Apr 2026 11:09:30 -0300 Luiz Angelo Daros de Luca…
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:
Quoted 7 lines
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
Quoted 7 lines
If this use of
FIELD_PREPis preferred, I will drop thisFIELD_WIDTHpatch from the series as it would no longer have any users.Regards,
Luiz
Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
In reply to Luiz Angelo Daros de LucaQuoted 9 lines · On Fri, 3 Apr 2026 11:09:30 -0300 Luiz Angelo Daros de Luca…
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
Quoted 21 lines
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_PREPis preferred, I will drop thisFIELD_WIDTHpatch from the series as it would no longer have any users.Regards,
Luiz
Re: [net-next PATCH 04/10] bitfield.h: add FIELD_WIDTH()
In reply to Luiz Angelo Daros de LucaQuoted 11 lines · Luiz Angelo Daros de Luca
Quoted 10 lines
include/linux/bitfield.h line 121
Quoted 8 lines
include/linux/bitfield.h +8
-0+/** +* 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.
Quoted 4 lines
I used
_maskto maintain consistency with the existing public macros in this file, such asFIELD_GET,FIELD_PREP,FIELD_MAX, andFIELD_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?
Quoted 24 lines
Quoted 11 lines
include/linux/bitfield.h line 125
Quoted 4 lines
include/linux/bitfield.h +4
-0+ ({ \ +__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 keepbitfield.hself-contained and avoid adding more includes, I'll try__builtin_popcountll()in a similar way__builtin_ffsllis already used.I also noticed the suggestion to use
__BF_FIELD_CHECK_MASKinstead of__BF_FIELD_CHECK. BothFIELD_MAXandFIELD_FITcurrently use the full__BF_FIELD_CHECKwith 0ULL as a dummy register to ensure the mask fits within the header's supported limits. If__BF_FIELD_CHECK_MASKis preferred forFIELD_WIDTH, I can certainly use it, but should we also updateFIELD_MAXandFIELD_FITfor consistency?
Quoted 4 lines
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?
Quoted 7 lines
#define __scalar_type_to_unsigned_cases(type) \ unsigned type: (unsigned type)0, \include/linux/bitfield.h +1
-0@@ -111,6 +112,19 @@ (typeof (_mask))((_mask) >>__bf_shf (_mask)); \ }) +/**
include/linux/bitfield.h line 125
Quoted 10 lines
include/linux/bitfield.h +10
-0+* 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.
Quoted 6 lines
(quoted diff) +2
-0+ }) + Regards,Luiz
Luiz Angelo Daros de Luca
maintainerPATCH 5/10 net: dsa: realtek: rtl8365mb: add table lookup interface
In reply to Luiz Angelo Daros de LucaFrom: Alvin Šipraga <[email protected]>
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.
--- 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(+)
drivers/net/dsa/realtek/Makefile +1 -0
@@ -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 \
drivers/net/dsa/realtek/rtl8365mb_table.c new file +255 -0
255 lines not on this page · raw message
drivers/net/dsa/realtek/rtl8365mb_table.h new file +133 -0
133 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [net-next PATCH 05/10] net: dsa: realtek: rtl8365mb: add table lookup interface
Quoted 5 lines · Luiz Angelo Daros de Luca
From: Alvin Šipraga <[email protected]>
Add a generic table lookup interface to centralize access to the RTL8365MB internal tables.
Quoted 7 lines
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 <[email protected]>Signed-off-by Alvin Šipraga <[email protected]>Signed-off-by Luiz Angelo Daros de Luca <[email protected]>
Yours,
Linus Walleij
Luiz Angelo Daros de Luca
maintainerPATCH 6/10 net: dsa: realtek: rtl8365mb: add VLAN support
In reply to Luiz Angelo Daros de LucaFrom: Alvin Šipraga <[email protected]>
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.
--- 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(+)
drivers/net/dsa/realtek/Makefile +1 -0
@@ -18,3 +18,4 @@ endif obj-$(CONFIG_NET_DSA_REALTEK_RTL8365MB) += rtl8365mb.o rtl8365mb-objs := rtl8365mb_main.o \ rtl8365mb_table.o \ + rtl8365mb_vlan.o \
drivers/net/dsa/realtek/rtl8365mb_main.c +256 -0
286 lines not on this page · raw message
drivers/net/dsa/realtek/rtl8365mb_vlan.c new file +805 -0
805 lines not on this page · raw message
drivers/net/dsa/realtek/rtl8365mb_vlan.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* VLAN configuration interface for the rtl8365mb switch family +* +* Copyright (C) 2022 Alvin Šipraga <[email protected]> +* +*/ + +#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 */
Signature · 1 line
2.53.0
Re: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
Quoted 561 lines · Luiz Angelo Daros de Luca
Walleij <[email protected]>,
=?utf-8?q?Alvin_=C5=A0ipraga?= <[email protected]>, Yury Norov <[email protected]>,
Rasmus Villemoes <[email protected]>, Russell King <[email protected]>
Cc: [email protected], [email protected],
Luiz Angelo Daros de Luca <[email protected]>
X-Mailer: b4 0.15.1
Status: O
Content-Length: 40249
Lines: 1202From: Alvin Šipraga <[email protected]>
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 <[email protected]>Signed-off-by Alvin Šipraga <[email protected]>Signed-off-by Luiz Angelo Daros de Luca <[email protected]>--- 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(+)drivers/net/dsa/realtek/Makefile +1
-0@@ -18,3 +18,4 @@ endif obj-$(CONFIG_NET_DSA_REALTEK_RTL8365MB) += rtl8365mb.o rtl8365mb-objs := rtl8365mb_main.o \ rtl8365mb_table.o \ + rtl8365mb_vlan.o \drivers/net/dsa/realtek/rtl8365mb_main.c +256
-0@@ -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,drivers/net/dsa/realtek/rtl8365mb_vlan.c new file +192
-0@@ -0,0 +1,805 @@ +// SPDX-License-Identifier: GPL-2.0 +/* VLAN configuration interface for the rtl8365mb switch family +* +* Copyright (C) 2022 Alvin Šipraga <[email protected]> +* +* 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));
drivers/net/dsa/realtek/rtl8365mb_vlan.c line 202
Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb_vlan.c new file +10
-0+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?
Luiz Angelo Daros de Luca
maintainerRe: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
Quoted 8 lines
drivers/net/dsa/realtek/rtl8365mb_vlan.c line 202
Quoted 5 lines
drivers/net/dsa/realtek/rtl8365mb_vlan.c new file +5
-0+ 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()resemblesFIELD_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
Re: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
Quoted 16 lines · Luiz Angelo Daros de Luca
Quoted 8 lines
drivers/net/dsa/realtek/rtl8365mb_vlan.c line 202
Quoted 5 lines
drivers/net/dsa/realtek/rtl8365mb_vlan.c new file +5
-0+ 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()resemblesFIELD_PREP(), except that you skip some checks. Is that intentional?While it resembles
FIELD_PREP(), the usage ofFIELD_GET()<<FIELD_WIDTH()is intentional and semantically distinct fromFIELD_PREP(). WhileFIELD_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.
Quoted 4 lines
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
Quoted 12 lines
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] viaRTL8365MB_CVLAN_ENTRY_D0_MBR_MASK.
- data[2] (D2): Contains the extension bits [10:8] viaRTL8365MB_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.
Linus Walleij
maintainerRe: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
In reply to Luiz Angelo Daros de LucaHi Luiz, Alvin,
thanks for your patch!
Quoted 37 lines · Luiz Angelo Daros de Luca
From: Alvin Šipraga <[email protected]>
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.
Quoted 3 lines
Co-developed-by Alvin Šipraga <[email protected]>Signed-off-by Alvin Šipraga <[email protected]>Signed-off-by Luiz Angelo Daros de Luca <[email protected]>
This patch ignores the existing abstractions for generic RTL8366 VLAN handling in rtl8366-core.c and realtek.h, which is actually compiled and bundled into the same object with the RTL8365MB driver.
It provides rtl8366_vlan_add(), rtl8366_vlan_del(), rtl8366_enable_vlan(), rtl8366_enable_vlan4k(), rtl8366_set_pvid(), rtl8366_set_vlan().
The RTL8366RB notably has the same 32 PVID thing and the same 4K VLAN table.
I understand that it is always easier to develop and test something for "just this one chip", but I predictably have to be the one to ask if this can be done in the shared code.
You need to either:
- Take a second sweep and refactor to share the existing VLAN handling with RTL8366RB (preferred).
- Explain why RTL8365MB is so very different that it cannot share code with RTL8366RB, then proceed to move all code from rtl8366-core.c into the RTL8366RB driver, change prefix of functions and generally make it local so it doesn't bloat the RTL8365MB code before adding this.
Yours,
Linus Walleij
Luiz Angelo Daros de Luca
maintainerRe: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
Hi Linus,
Thanks for the review, I took some time to evaluate the possibility of reusing rtl8366-core.
Quoted 3 lines
This patch ignores the existing abstractions for generic RTL8366 VLAN handling in
rtl8366-core.candrealtek.h, which is actually compiled and bundled into the same object with the RTL8365MB driver.
While rtl8366-core is currently only used by RTL8366, I understand the goal of sharing VLAN handling across these drivers. However, RTL8365MB differs from RTL8366RB in how VLANs and PVIDs are implemented.
1) In RTL8366, VLAN4K, VLANMC and PVID are programmed together, and the helpers in rtl8366-core assume this 1:1 relationship. In RTL8365MB, the VLANMC is exclusively used as an indirect way to set the port VID used in the PVID (port pvid points to vlan MC table index and vlan MC entry has the VID number). In this patch, the VLANMC is conditionally allocated only when a port uses it as PVID and deallocated when no ports use it. VLAN MC membership is not checked by the HW but I still keep it in sync when it is allocated.
As VLANMC, at least in RTL8365MB, is effectively only used for PVID, I'll put all vlan MC methods behind public PVID methods in the following v2. There is no need to expose them for now. For the next RTL8367D family, VLANMC is gone and there are dedicated registers to set the PVID VID number for each port.
2) In RTL8365MB, vlan is always enabled, controlling filtering by port (.port_vlan_filtering), while in RTL8366, it is enabled by vlan_add.
3) The abstraction doesn't set the framefilter in RTL8366 nor does it clear PVID status. I'm using framefilter as a way to disable PVID, dropping untagged frames, together with setting PVID VLAN MC index to 0 and keeping an empty (vid=0) vlan config at that index.
Without clearing the PVID state in RTL8366, removing a PVID VLAN member and re-adding it as a non-PVID member will leave it configured as PVID.
4) I don't know how IVL would be handled in RTL8366... but that's a topic for another patch.
Quoted 3 lines
I understand that it is always easier to develop and test something for "just this one chip", but I predictably have to be the one to ask if this can be done in the shared code.
I agree there is room for sharing logic in the future, both for newer areas (table access, VLAN, L2) and existing ones (phy_read/write), but this likely requires rethinking the abstraction and harmonizing the assumptions on each driver first. It is possible that the hardware behavior between RTL8366 and RTL8365MB is more similar than what the current drivers suggest. However, the existing rtl8366-core abstraction does not model this in a way that can be directly reused here. Aligning both drivers would likely require reworking that abstraction rather than adapting the RTL8365MB implementation to fit it.
This could be explored in the future as a separate effort as it changes how RTL8366RB driver behave. For now, I would prefer to keep this implementation local to RTL8365MB and then follow up with a separate effort to reconcile both drivers and extract shared logic. Some of that work will likely become clearer while adding RTL8367D family support.
Regards,
Luiz
Linus Walleij
maintainerRe: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
Quoted 2 lines · Luiz Angelo Daros de Luca
Thanks for the review, I took some time to evaluate the possibility of reusing rtl8366-core.
Thanks Luiz.
Even if we end up splitting out VLAN, I think my comments on the other things that can be shared are still valid as well.
Quoted 3 lines
While rtl8366-core is currently only used by RTL8366, I understand the goal of sharing VLAN handling across these drivers. However, RTL8365MB differs from RTL8366RB in how VLANs and PVIDs are implemented.
To give the whole picture, RTL8365MB wasn't even in the picture when this abstraction was created.
The ASICs the driver was supposed to handle (and which were/is used by OpenWrt) were:
- RTL8366RB
- RTL8366S
- RTL8367
The abstraction was part of the outoftree "swconfig" driver for a long time before being migrated to DSA.
I haven't yet wrapped my head around if the RTL8367 (no extra letters) is more RTL8366RB-ish or more RTL8365MB-ish... take a look at the old code if you can figure it out from register maps etc:
https://github.com/openwrt/openwrt/tree/main/target/linux/generic/files/drivers/net/phy
(maybe Gabor knows, put him on cc)
If the VLAN is very dissimilar we can just as well have two different implementations.
Quoted 18 lines
1) In RTL8366, VLAN4K, VLANMC and PVID are programmed together, and the helpers in rtl8366-core assume this 1:1 relationship. In RTL8365MB, the VLANMC is exclusively used as an indirect way to set the port VID used in the PVID (port pvid points to vlan MC table index and vlan MC entry has the VID number). In this patch, the VLANMC is conditionally allocated only when a port uses it as PVID and deallocated when no ports use it. VLAN MC membership is not checked by the HW but I still keep it in sync when it is allocated.
As VLANMC, at least in RTL8365MB, is effectively only used for PVID, I'll put all vlan MC methods behind public PVID methods in the following v2. There is no need to expose them for now. For the next RTL8367D family, VLANMC is gone and there are dedicated registers to set the PVID VID number for each port.
2) In RTL8365MB, vlan is always enabled, controlling filtering by port (.port_vlan_filtering), while in RTL8366, it is enabled by
vlan_add.
Quoted 8 lines
3) The abstraction doesn't set the framefilter in RTL8366 nor does it clear PVID status. I'm using framefilter as a way to disable PVID, dropping untagged frames, together with setting PVID VLAN MC index to 0 and keeping an empty (vid=0) vlan config at that index.
Without clearing the PVID state in RTL8366, removing a PVID VLAN member and re-adding it as a non-PVID member will leave it configured as PVID.
OK seems we should split up the VLAN handling if there is nothing to share in the abstraction here. We don't do abstraction for abstractions sake.
I'd say make a patch moving all functions from realtek_ops that can't be reused into the rtl8366rb driver and make them static.
But probably keep the shared core for other stuff. Though I guess it might be empty then until we add more stuff :/
Quoted 2 lines
4) I don't know how IVL would be handled in RTL8366... but that's a topic for another patch.
OTOMH it seems half of it is generic but it's a helicopter view. There is no question the ASICs are related, we just don't know by how much...
Quoted 8 lines
It is possible that the hardware
behavior between RTL8366 and RTL8365MB is more similar than what the current drivers suggest. However, the existing rtl8366-core abstraction does not model this in a way that can be directly reused here. Aligning both drivers would likely require reworking that abstraction rather than adapting the RTL8365MB implementation to fit it.
Quoted 6 lines
This could be explored in the future as a separate effort as it changes how RTL8366RB driver behave. For now, I would prefer to keep this implementation local to RTL8365MB and then follow up with a separate effort to reconcile both drivers and extract shared logic. Some of that work will likely become clearer while adding RTL8367D family support.
The reason I push back a bit is that any kind of things that are "fix later" tend to never actually happen in my experience. Either we add the abstraction now or never.
For VLAN I'm pretty much convinced that there is not much to share. For port separation/isolation and possibly FDB I'm not as convinced.
Yours,
Linus Walleij
Luiz Angelo Daros de Luca
maintainerRe: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
Quoted 2 lines
Even if we end up splitting out VLAN, I think my comments on the other things that can be shared are still valid as well.
Agree. It is generally more effective to refactor and share code once the target models are well-understood, rather than aiming at a moving target.
Quoted 6 lines
Quoted 3 lines
While rtl8366-core is currently only used by RTL8366, I understand the goal of sharing VLAN handling across these drivers. However, RTL8365MB differs from RTL8366RB in how VLANs and PVIDs are implemented.
To give the whole picture, RTL8365MB wasn't even in the picture when this abstraction was created.
The logic is indeed similar in several places, but the overhead of creating abstractions or indirect calls for relatively simple operations might outweigh the benefits of reuse at this stage.
Quoted 6 lines
The ASICs the driver was supposed to handle (and which were/is used by OpenWrt) were:
- RTL8366RB
- RTL8366S
- RTL8367
Quoted 8 lines
The abstraction was part of the outoftree "swconfig" driver for a long time before being migrated to DSA.
I haven't yet wrapped my head around if the RTL8367 (no extra letters) is more RTL8366RB-ish or more RTL8365MB-ish... take a look at the old code if you can figure it out from register maps etc:
https://github.com/openwrt/openwrt/tree/main/target/linux/generic/files/drivers/net/phy
(maybe Gabor knows, put him on cc)
I have experimented with the RTL8367R (found in the TP-Link TL-WR2543ND), which appears to belong to the RTL8367B family. From what I’ve observed, it behaves more like the RTL8365MB. However, my visibility is limited to a small subset of registers used in U-Boot. I haven't been able to confirm if the switch supports a CPU tag or how to enable it, as the registers used for the RTL8365MB did not produce the same results.
Quoted 6 lines
OK seems we should split up the VLAN handling if there is nothing to share in the abstraction here. We don't do abstraction for abstractions sake.
I'd say make a patch moving all functions from
realtek_opsthat can't be reused into the rtl8366rb driver and make them static.
Merging rtl8366-core.c into rtl8366rb.c would likely simplify the implementation significantly and remove unnecessary indirection.
Quoted 2 lines
But probably keep the shared core for other stuff. Though I guess it might be empty then until we add more stuff :/
Not empty as we already have some functions from rtl83xx.c :-)
Quoted 10 lines
Quoted 6 lines
This could be explored in the future as a separate effort as it changes how RTL8366RB driver behave. For now, I would prefer to keep this implementation local to RTL8365MB and then follow up with a separate effort to reconcile both drivers and extract shared logic. Some of that work will likely become clearer while adding RTL8367D family support.
The reason I push back a bit is that any kind of things that are "fix later" tend to never actually happen in my experience. Either we add the abstraction now or never.
I understand the concern regarding "technical debt." However, forcing code sharing now carries the risk of regressions on legacy hardware that is difficult to test. Keeping the implementations distinct for now ensures stability for these older devices while we refine the RTL8365MB support.
Quoted 3 lines
For VLAN I'm pretty much convinced that there is not much to share. For port separation/isolation and possibly FDB I'm not as convinced.
It remains difficult to determine the best path for sharing port isolation or FDB logic without a clearer picture of the future rtl8366rb FDB code. Specifically, differences in IVL logic might make shared abstractions more complex than they initially appear.
Quoted 2 lines
Yours,
Linus Walleij
Regards,
Luiz
Luiz Angelo Daros de Luca
maintainerRe: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
Hi Linus,
Quoted 5 lines
Quoted 5 lines
I haven't yet wrapped my head around if the RTL8367 (no extra letters) is more RTL8366RB-ish or more RTL8365MB-ish... take a look at the old code if you can figure it out from register maps etc:
https://github.com/openwrt/openwrt/tree/main/target/linux/generic/files/drivers/net/phy
(maybe Gabor knows, put him on cc)
Regarding the RTL8367 (no extra letters), the RTL8367R is indeed a member of that original family, rather than the "B" family as I previously suggested. It uses Port 9 for the CPU, which is absent in RTL8367B. Architecturally, it is definitely more RTL8365MB-ish.
The main challenge with the base RTL8367 is the lack of a public API. Most vendors support it via binary managers (ASUS) or proprietary kernel modules (TP-Link). The only available references I’ve found are the OpenWrt swconfig driver you mentioned and some U-Boot initialization code. I do have the rtl8367{b,c,d} APIs. While the rtl8367b seems close to the original RTL8367, it has fewer ports. Does anyone happen to have access to the original RTL8367 API documentation?
In any case, I’ve successfully reverse-engineered the table access functions and format, which are similar to the rtl8367b, to the point where I have the rtl8365mb driver running on an RTL8367R. I also managed to enable CPU tagging, and it is currently functional using the rtl8_4 tag. The code still needs some cleanup, support for additional features (like LEDs) and a lot of tests, but it’s looking promising. Adding support for the RTL8367 variant should make adding RTL8367B or RTL8367D support much easier in the future.
Regards,
Luiz
Linus Walleij
maintainerRe: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
Quoted 8 lines · Luiz Angelo Daros de Luca
I’ve successfully reverse-engineered the table access functions and format, which are similar to the rtl8367b, to the point where I have the rtl8365mb driver running on an RTL8367R. I also managed to enable CPU tagging, and it is currently functional using the
rtl8_4tag. The code still needs some cleanup, support for additional features (like LEDs) and a lot of tests, but it’s looking promising. Adding support for the RTL8367 variant should make adding RTL8367B or RTL8367D support much easier in the future.
Wow that was an unexpected development from a stray question from my side!
Your attention to detail is much appreciated, as this will help migration away from swconfig in OpenWrt.
I had the same issue: all code I've written for the RTL8366RB family is based on code dumps and no public documentation. Sometimes I wonder if Realtek even documented it or just made the proprietary driver and shipped without any documentation.
Yours,
Linus Walleij
Re: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
In reply to Luiz Angelo Daros de LucaHi Luiz, Linus,
Sorry for the late reply.
Quoted 11 lines
Quoted 6 lines
Quoted 6 lines
I haven't yet wrapped my head around if the RTL8367 (no extra letters) is more RTL8366RB-ish or more RTL8365MB-ish... take a look at the old code if you can figure it out from register maps etc:
https://github.com/openwrt/openwrt/tree/main/target/linux/generic/files/drivers/net/phy
(maybe Gabor knows, put him on cc)Regarding the RTL8367 (no extra letters), the RTL8367R is indeed a member of that original family, rather than the "B" family as I previously suggested. It uses Port 9 for the CPU, which is absent in RTL8367B. Architecturally, it is definitely more RTL8365MB-ish.
I wrote the rtl8367 driver for the RTL8367R and RTL8367M switches. I have no datasheet for those chips, but there are some basic information about their features [1].
The driver was based on various code found in GPL sources of the TP-Link TL-WR2543ND and ASUS RT-N56U devices. Those sources were using the RTL8370 specific API for these chips.
It was not clear that the two models really belongs to the RTL8370 family or simply the vendors were using the RTL8370 specific code as a base, so I have used RTL8367 prefix in the swconfig driver. Probably, it would have been better to keep the RTL8370 prefix to avoid confusion.
Quoted 9 lines
The main challenge with the base RTL8367 is the lack of a public API. Most vendors support it via binary managers (ASUS) or proprietary kernel modules (TP-Link). The only available references I’ve found are the OpenWrt swconfig driver you mentioned and some U-Boot initialization code. I do have the rtl8367{b,c,d} APIs. While the rtl8367b seems close to the original RTL8367, it has fewer ports. Does anyone happen to have access to the original RTL8367 API documentation?
I have no documentation, but the RTL8370 API source can be found at various places [2], [3].
To be honest, I don't remember all the details, but I hope that this helps.
Regards,
Gabor
[1] http://semitron.co.kr/product/product.php?lcate=001&mcate=005&scate=004&uid=47&PHPSESSID=ec195486d91ed175f698567462571180
[2] https://github.com/cgoder/openwrt_rtk/tree/master/rtk_openwrt_sdk/target/linux/rtkmipsel/files/drivers/net/rtl819x/RTL8370_RTL8367_API
[3] https://github.com/andy-padavan/rt-n56u/tree/master/uboot/mips/uboot-5.x.x.x/drivers/rtl8367/api_8370
Luiz Angelo Daros de Luca
maintainerRe: [net-next PATCH 06/10] net: dsa: realtek: rtl8365mb: add VLAN support
Hi Gabor, Linus,
(Sorry for the late answer. I left it as a draft and forgot to send it)
Quoted 3 lines
The driver was based on various code found in GPL sources of the TP-Link TL-WR2543ND and ASUS RT-N56U devices. Those sources were using the RTL8370 specific API for these chips.
The GPL code from both TL-WR2543ND and ASUS RT-N56U devices were a dead end. However,
the mention of "RTL8370" simply changed everything. I was searching for a RTL8367 API.
Quoted 4 lines
It was not clear that the two models really belongs to the RTL8370 family or simply the vendors were using the RTL8370 specific code as a base, so I have used RTL8367 prefix in the swconfig driver. Probably, it would have been better to keep the RTL8370 prefix to avoid confusion.
Maybe. I don't know if the RTL8367 family exists. I'm adapting the rtl8365mb driver to work with multiple switch generations, from RTL8370/RTL8367 until RTL8367D. I simply internally renamed RTL8370/RTL8367 to RTL8367A as I can reference it as Family-A (and B, C, D...).
Quoted 11 lines
Quoted 8 lines
The main challenge with the base RTL8367 is the lack of a public API. Most vendors support it via binary managers (ASUS) or proprietary kernel modules (TP-Link). The only available references I’ve found are the OpenWrt swconfig driver you mentioned and some U-Boot initialization code. I do have the rtl8367{b,c,d} APIs. While the rtl8367b seems close to the original RTL8367, it has fewer ports. Does anyone happen to have access to the original RTL8367 API documentation?
I have no documentation, but the RTL8370 API source can be found at various places [2], [3].
I did have it checked out in my dev dir for years but the missing piece was to match RTL8367R with RTL8370.
Quoted 1 line
To be honest, I don't remember all the details, but I hope that this helps.
It helped a lot.
I have a mostly working rtl8365mb version that runs on RTL8367R. I still need more tests with FDB but I think it will be possible to replace the swconfig rtl8367.c with DSA rtl8365mb. I am still looking for a RTL8367B and RTL8367D device to complete the family.
Regards,
Luiz
Luiz Angelo Daros de Luca
maintainerPATCH 7/10 net: dsa: realtek: rtl8365mb: add port_bridge_{join,leave}
In reply to Luiz Angelo Daros de LucaFrom: Alvin Šipraga <[email protected]>
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.
--- drivers/net/dsa/realtek/rtl8365mb_main.c | 123 ++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-)
drivers/net/dsa/realtek/rtl8365mb_main.c +122 -1
@@ -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,
Signature · 1 line
2.53.0
Linus Walleij
maintainerRe: [net-next PATCH 07/10] net: dsa: realtek: rtl8365mb: add port_bridge_{join,leave}
Hi Luiz, Alvin,
thanks for your patch!
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port, · line 1493
Quoted 6 lines · Luiz Angelo Daros de Luca
static int rtl8365mb_port_set_isolation(struct
realtek_priv*priv, int port,u32 mask)drivers/net/dsa/realtek/rtl8365mb_main.c +4
-0+static int rtl8365mb_port_add_isolation(struct realtek_priv *priv, int port, + u32 mask) +static int rtl8365mb_port_remove_isolation(struct realtek_priv *priv, int port, + u32 mask)
This is smart!
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port, · line 1542
Quoted 6 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +6
-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) +static void rtl8365mb_port_bridge_leave(struct dsa_switch *ds, int port, + struct dsa_bridge bridge)
The content of these functions are very similar to rtl8366rb_port_bridge_join() and rtl8366rb_port_bridge_leave().
What about moving this code to the shared library rtl8366-core as rtl8366_port_bridge_join() and rtl8366_port_bridge_leave() and add per-variant callbacks for ->port_set_isolation() ->port_add_isolation() and ->port_remove_isolation() to struct realtek_ops and share this with rtl8366rb by refactoring rtl8366rb.c to use the same callbacks.
Using shared code will help us to avoid having to fix the same bugs in more than one place.
I don't know about this EFID stuff in rtl8365mb_port_set_efid() (maybe RTL8366RB needs something like that too) but certainly that can be an optional callback in struct realtek_ops for now.
Yours,
Linus Walleij
Luiz Angelo Daros de Luca
maintainerRe: [net-next PATCH 07/10] net: dsa: realtek: rtl8365mb: add port_bridge_{join,leave}
Quoted 9 lines
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port, · line 1542
Quoted 6 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +6
-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) +static void rtl8365mb_port_bridge_leave(struct dsa_switch *ds, int port, + struct dsa_bridge bridge)The content of these functions are very similar to
rtl8366rb_port_bridge_join()andrtl8366rb_port_bridge_leave().
It is likely that these functions are a variant of those or they share a common origin.
Quoted 6 lines
What about moving this code to the shared library rtl8366-core as
rtl8366_port_bridge_join()andrtl8366_port_bridge_leave()and add per-variant callbacks for ->port_set_isolation() ->port_add_isolation() and ->port_remove_isolation() to structrealtek_opsand share this with rtl8366rb by refactoringrtl8366rb.cto use the same callbacks.
I agree that moving this logic to rtl83xx.c and introducing the necessary abstractions makes sense. However, I would prefer not to refactor rtl8366rb.c within this specific patchset to keep the scope manageable.
I will also use dsa_switch_for_each_user_port, as dsa_port_offloads_bridge() already ensures that CPU ports are skipped and it aligns with RTL8366RB logic.
Quoted 6 lines
Using shared code will help us to avoid having to fix the same bugs in more than one place.
I don't know about this EFID stuff in
rtl8365mb_port_set_efid()(maybe RTL8366RB needs something like that too) but certainly that can be an optional callback in structrealtek_opsfor now.
I will keep this as an optional callback for now. If it eventually becomes clear that the RTL8366RB requires it, we can update the logic later to make the callback mandatory.
Quoted 2 lines
Yours,
Linus Walleij
Regards,
Luiz
Luiz Angelo Daros de Luca
maintainerPATCH 8/10 net: dsa: realtek: rtl8365mb: add FDB support
In reply to Luiz Angelo Daros de LucaFrom: Alvin Šipraga <[email protected]>
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.
--- 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(-)
drivers/net/dsa/realtek/Makefile +1 -0
@@ -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 \
drivers/net/dsa/realtek/rtl8365mb_l2.c new file +465 -0
465 lines not on this page · raw message
drivers/net/dsa/realtek/rtl8365mb_l2.h new file +59 -0
59 lines not on this page · raw message
drivers/net/dsa/realtek/rtl8365mb_main.c +196 -1
251 lines not on this page · raw message
Signature · 1 line
2.53.0
Linus Walleij
maintainerRe: [net-next PATCH 08/10] net: dsa: realtek: rtl8365mb: add FDB support
Hi Luiz, Alvin,
thanks for your patch!
I actually at one point started to implement FDB accessors to the RTL8366RB before I got sidetracked (for the same reasons of being able to do hardware offloading), here is how far I got before I got distracted:
https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-integrator.git/patch/?id=9941b129f91e92423803d6e2869022ffa66e9654
You can see that it also has an L2 table albeit with 4 entries rather than 6, and an additional "CAM" (content adressable memory...) table that I don't quite understand.
Quoted 9 lines · Luiz Angelo Daros de Luca
drivers/net/dsa/realtek/rtl8365mb_l2.h new file +9
-0+int rtl8365mb_l2_del_mc(struct realtek_priv *priv, u16 port, + const unsigned char mac_addr[static ETH_ALEN], + u16 vid) +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,
drivers/net/dsa/realtek/rtl8365mb_l2.h line 57
Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb_l2.h new file +10
-0+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);
I think it would make sense to add these to struct realtek_ops and
make them available for all RTL8366 variants.
Possibly it would even make sense to move rtl8365mb_table_query() to struct realtek_ops under the name .table_query and all of these helpers to rtl8366-core.c as rtl8366_l2_del_mc() etc just calling the per-chip table query variant. I'm not sure but you can perhaps see what makes most sense?
Quoted 1 line
+ struct mutex
l2_lock;
Move to struct realtek_priv, every chip will need an l2 lock.
Quoted 5 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +5
-0+static void rtl8365mb_port_fast_age(struct dsa_switch *ds, int port) +static int rtl8365mb_port_fdb_add(struct dsa_switch *ds, int port, + const unsigned char *addr, u16 vid, + struct dsa_db db) +static int rtl8365mb_port_fdb_del(struct dsa_switch *ds, int port,
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_vlan_setup(struct dsa_switch *ds) · line 1610
Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +10
-0+ const unsigned char *addr, u16 vid, + struct dsa_db db) +static int rtl8365mb_port_fdb_dump(struct dsa_switch *ds, int port, + dsa_fdb_dump_cb_t *cb, void *data) +static int rtl8365mb_port_mdb_add(struct dsa_switch *ds, int port, + const struct switchdev_obj_port_mdb *mdb, + struct dsa_db db) +static int rtl8365mb_port_mdb_del(struct dsa_switch *ds, int port, + const struct switchdev_obj_port_mdb *mdb, + struct dsa_db db)
I think you should rename all of these rtl8366_port_* and move them to rtl8366-core.c. They are generic.
drivers/net/dsa/realtek/rtl8365mb_main.c static const struct dsa_switch_ops rtl8365mb_switch_ops = { · line 2690
Quoted 5 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +1
-0@@ -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_fast_age = rtl8366_port_fast_age,
Etc.
Curiously I already have a hack for fast aging in the rtl8366rb driver, but using the FDB directly to do that directly in L2 is definitely better!
Yours,
Linus Walleij
Luiz Angelo Daros de Luca
maintainerRe: [net-next PATCH 08/10] net: dsa: realtek: rtl8365mb: add FDB support
Quoted 8 lines
I actually at one point started to implement FDB accessors to the RTL8366RB before I got sidetracked (for the same reasons of being able to do hardware offloading), here is how far I got before I got distracted:
https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-integrator.git/patch/?id=9941b129f91e92423803d6e2869022ffa66e9654You can see that it also has an L2 table albeit with 4 entries rather than 6, and an additional "CAM" (content adressable memory...) table that I don't quite understand.
I will look into the CAM table info in rtl8367c family.
Quoted 17 lines
Quoted 14 lines · Luiz Angelo Daros de Luca
drivers/net/dsa/realtek/rtl8365mb_l2.h new file +14
-0+int rtl8365mb_l2_del_mc(struct realtek_priv *priv, u16 port, + const unsigned char mac_addr[static ETH_ALEN], + u16 vid) +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,
Quoted 8 lines
drivers/net/dsa/realtek/rtl8365mb_l2.h line 57
Quoted 5 lines
drivers/net/dsa/realtek/rtl8365mb_l2.h new file +5
-0+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);I think it would make sense to add these to struct
realtek_opsand
make them available for all RTL8366 variants.
Agreed. I will add these to struct realtek_ops. I'll need to create a generic replacement for struct rtl8365mb_l2_uc, but the rest of the migration should be straightforward. For now, the efid argument can simply be ignored by the RTL8366 implementation.
Quoted 5 lines
Possibly it would even make sense to move
rtl8365mb_table_query()to structrealtek_opsunder the name .table_query and all of these helpers tortl8366-core.casrtl8366_l2_del_mc()etc just calling the per-chip table query variant. I'm not sure but you can perhaps see what makes most sense?
I suspect rtl8365mb_table_query() might be too complex to abstract effectively without first seeing how the RTL8366 handles table lookups, as hardware-specific behaviors can vary significantly here. I suggest we implement it for the RTL8366 first; once we have a reference point, we can discuss the best way to consolidate the logic.
Quoted 3 lines
Quoted 1 line
+ struct mutex
l2_lock;Move to struct
realtek_priv, every chip will need an l2 lock.
Sure.
Quoted 10 lines
Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +10
-0+static void rtl8365mb_port_fast_age(struct dsa_switch *ds, int port) +static int rtl8365mb_port_fdb_add(struct dsa_switch *ds, int port, + const unsigned char *addr, u16 vid, + struct dsa_db db) +static int rtl8365mb_port_fdb_del(struct dsa_switch *ds, int port, + const unsigned char *addr, u16 vid, + struct dsa_db db) +static int rtl8365mb_port_fdb_dump(struct dsa_switch *ds, int port, + dsa_fdb_dump_cb_t *cb, void *data) +static int rtl8365mb_port_mdb_add(struct dsa_switch *ds, int port,
Quoted 8 lines
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_vlan_setup(struct dsa_switch *ds) · line 1610
Quoted 5 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +5
-0+const struct switchdev_obj_port_mdb *mdb, + struct dsa_db db) +static int rtl8365mb_port_mdb_del(struct dsa_switch *ds, int port, +const struct switchdev_obj_port_mdb *mdb, +struct dsa_db db)I think you should rename all of these rtl8366_port_* and move them to
rtl8366-core.c. They are generic.
Yes. Once rtl8365mb_l2_* is moved to realtek_ops, these functions become generic wrappers and fit perfectly in rtl83xx.c.
Quoted 7 lines
drivers/net/dsa/realtek/rtl8365mb_main.c static const struct dsa_switch_ops rtl8365mb_switch_ops = { · line 2690
Quoted 5 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +1
-0@@ -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_fast_age =
rtl8366_port_fast_age,
I will update the ops to use the new generics.
Regards,
Luiz
Luiz Angelo Daros de Luca
maintainerPATCH 9/10 net: dsa: realtek: rtl8365mb: add bridge port flags
In reply to Luiz Angelo Daros de LucaFrom: Alvin Šipraga <[email protected]>
Add bridge port flags for:
- BR_LEARNING
- BR_FLOOD
- BR_MCAST_FLOOD
- BR_BCAST_FLOOD
--- drivers/net/dsa/realtek/rtl8365mb_main.c | 125 +++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+)
drivers/net/dsa/realtek/rtl8365mb_main.c +125 -0
@@ -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,
Signature · 1 line
2.53.0
Linus Walleij
maintainerRe: [net-next PATCH 09/10] net: dsa: realtek: rtl8365mb: add bridge port flags
Hi Luiz, Alvin,
Quoted 11 lines · Luiz Angelo Daros de Luca
From: Alvin Šipraga <[email protected]>
Add bridge port flags for:
-BR_LEARNING
-BR_FLOOD
-BR_MCAST_FLOOD
-BR_BCAST_FLOODCo-developed-by Alvin Šipraga <[email protected]>Signed-off-by Alvin Šipraga <[email protected]>Signed-off-by Luiz Angelo Daros de Luca <[email protected]>
I have been working on a patch for RTL8366RB for this as well:
https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-integrator.git/patch/?id=0d58a9d74f363b969c06a8ac8446e6eef5511795
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port, · line 1680
Quoted 6 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +6
-0+static int rtl8365mb_port_set_ucast_flood(struct realtek_priv *priv, int port, + bool enable) +static int rtl8365mb_port_set_mcast_flood(struct realtek_priv *priv, int port, + bool enable) +static int rtl8365mb_port_set_bcast_flood(struct realtek_priv *priv, int port, + bool enable)
Those are small and just writing registers so I guess they can be kept local, but I think they should be accessed indirectly through
struct realtek_ops .rtl8366_port_set_ucast_flood() etc.
I can just make a simple patch adding the same three callbacks for RTL8366RB and then it's done for both chips.
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port, · line 1704
Quoted 6 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +6
-0+static int rtl8365mb_port_pre_bridge_flags(struct dsa_switch *ds, int port, + struct switchdev_brport_flags flags, + struct netlink_ext_ack *extack) +static int rtl8365mb_port_bridge_flags(struct dsa_switch *ds, int port, + struct switchdev_brport_flags flags, + struct netlink_ext_ack *exack)
Move this one to rtl8366-core.c under the name rtl8366_port_[pre_]bridge_flags() and use the abstract accessors to do the register writes.
Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +2
-0@@ -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. */
drivers/net/dsa/realtek/rtl8365mb_main.c static void rtl8365mb_port_bridge_leave(struct dsa_switch *ds, int port, · line 1845
Quoted 8 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +2
-0@@ -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. */
These debug prints seem unrelated to this patch?
Quoted 6 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +2
-0@@ -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);
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_setup(struct dsa_switch *ds) · line 2671
Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +10
-0+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;
Create rtl8366_setup_flood_control() in realtek-core.c and add these three calls as a helper using the rtl8366_* generic calls there. It will be done the same on all chips.
Yours,
Linus Walleij
Luiz Angelo Daros de Luca
maintainerRe: [net-next PATCH 09/10] net: dsa: realtek: rtl8365mb: add bridge port flags
Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port, · line 1680
Quoted 6 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +6
-0+static int rtl8365mb_port_set_ucast_flood(struct realtek_priv *priv, int port, + bool enable) +static int rtl8365mb_port_set_mcast_flood(struct realtek_priv *priv, int port, + bool enable) +static int rtl8365mb_port_set_bcast_flood(struct realtek_priv *priv, int port, + bool enable)Those are small and just writing registers so I guess they can be kept local, but I think they should be accessed indirectly through
structrealtek_ops.rtl8366_port_set_ucast_flood() etc.
Sure.
Quoted 6 lines
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port, · line 1704
Quoted 3 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +3
-0+static int rtl8365mb_port_bridge_flags (struct dsa_switch *ds,int port, +struct switchdev_brport_flags flags, +struct netlink_ext_ack *exack)Move this one to
rtl8366-core.cunder the name rtl8366_port_[pre_]bridge_flags() and use the abstract accessors to do the register writes.
OK
Quoted 13 lines
Quoted 13 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +2
-0@@ -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;
Quoted 7 lines
drivers/net/dsa/realtek/rtl8365mb_main.c static void rtl8365mb_port_bridge_leave(struct dsa_switch *ds, int port, · line 1845
Quoted 5 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +2
-0+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. */ These debug prints seem unrelated to this patch?
Yes, sorry. I'll move them.
Quoted 5 lines
Quoted 5 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +0
-0@@ -2547,6 +2657,19 @@ static int rtl8365mb_setup(struct dsa_switch *ds)if (ret) goto out_teardown_irq;
Quoted 16 lines
drivers/net/dsa/realtek/rtl8365mb_main.c static int rtl8365mb_setup(struct dsa_switch *ds) · line 2671
Quoted 12 lines
drivers/net/dsa/realtek/rtl8365mb_main.c +12
-0+/* 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;Create
rtl8366_setup_flood_control()inrealtek-core.cand add these three calls as a helper using the rtl8366_* generic calls there. It will be done the same on all chips.
OK. I think
rtl83xx_setup_port_flood_control(struct realtek_priv *priv, int port);
might be better as we can avoid looping again over the ports.
Regards,
Luiz
Luiz Angelo Daros de Luca
maintainerPATCH 10/10 net: dsa: tag_rtl8_4: set KEEP flag
In reply to Luiz Angelo Daros de LucaKEEP=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.
--- net/dsa/tag_rtl8_4.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/dsa/tag_rtl8_4.c +4 -2
@@ -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)));
Signature · 1 line
2.53.0
Re: [net-next PATCH 10/10] net: dsa: tag_rtl8_4: set KEEP flag
Quoted 11 lines · 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 <[email protected]>Signed-off-by Alvin Šipraga <[email protected]>Signed-off-by Luiz Angelo Daros de Luca <[email protected]>
I think you could just send this and patch 1 separately so they get merged first as they are completely self-contained and uncontroversial and touch the same code.
Yours,
Linus Walleij