From: Mieczyslaw Nalewaj <namiltd@yahoo.com>
To: linusw@kernel.org, andrew@lunn.ch, olteanv@gmail.com,
kuba@kernel.org, davem@davemloft.net, edumazet@google.com,
pabeni@redhat.com, linux@armlinux.org.uk, luizluca@gmail.com,
maxime.chevallier@bootlin.com, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org, alvin.sipraga@analog.com,
gusfmora@gmail.com, kuncy7@gmail.com, fabianotassotti@gmail.com,
gabonpivovich@gmail.com, contact@c127.dev
Subject: [PATCH net-next v13 07/14] net: dsa: realtek: rtl8365mb: set and get vlan 4k for family D
Date: Sun, 22 Nov 2026 10:43:28 +0100 [thread overview]
Message-ID: <0552fff4-ac3d-4e2b-8d66-610d0e661e11@yahoo.com> (raw)
In-Reply-To: <3f1826f8-3096-40dd-a278-c70fce4852c8@yahoo.com>
The hardware structure of 4K VLAN table entries has changed between
silicon families. Family D's CVLAN entry is two 16-bit words rather
than three: there is no third word extending member/untag past bit 7,
and no priority/meter fields at all. The FID field is restricted to a
narrower 2-bit width. The chip itself supports both IVL and SVL.
IVL_EN (bit 3, versus bit 14 on family C) follows vlan4k->ivl_en. On
family D, rtl8365mb_vlan_4k_port_set() enables it while any user port
has VLAN filtering enabled (dsa_port_is_vlan_filtering()), so that
static and learned FDB/MDB entries use the same key. Family C keeps IVL
always enabled, as before.
When a vlan_filtering change flips this switch-wide state,
rtl8365mb_d_vlan_4k_refresh_ivl() updates IVL_EN across the active 4K
entries, so existing VLANs, including those programmed before filtering
was enabled, switch between SVL and IVL immediately. Changes that leave
the state as it is do not touch the table.
The update visits only the VIDs in use, tracked in the vlan_4k_used
bitmap in struct realtek_priv. rtl8365mb_vlan_4k_port_set() keeps it up
to date under vlan_lock, so a toggle does not read all 4094 entries over
the slow management bus (about 5 s per toggle on an RTL8367S-VB; 20-30 ms
with the bitmap). The bitmap covers the entries written by this driver:
an entry that was already in the table before the driver loaded is not
visited. An entry that loses its last member is written back with IVL_EN
cleared, as in its reset state, so that it is not left in IVL after it
drops out of the bitmap. The rewrite itself is a separate function,
rtl8365mb_d_vlan_4k_set_ivl(), so that an error path can restore the
previous state.
Reusing the three-word family C layout on family D would read a third
word that does not exist as a distinct table word on family D's die.
Whatever data happens to be in that register would be folded into
member/untag bits [10:8] on read, and written back on every
read-modify-write cycle in rtl8365mb_vlan_4k_port_set(). Give family D
its own two-word pack/unpack instead: add rtl8365mb_d_vlan_4k_read() and
rtl8365mb_d_vlan_4k_write(). The shared code reaches them through the
vlan_4k_read and vlan_4k_write pointers in struct rtl8365mb_family_info,
so the pack/unpack code needs no is_d() check.
The two-word layout matches the vendor switch API for this chip
family, which writes only 0x0510/0x0511 for a CVLAN entry.
Nothing points to them until the family D info is added together with
the chip table entry in the last patch. Until then they are not static
and are declared in rtl8365mb_vlan.h, so that the build does not warn
about unused functions.
Tested-by: Gustavo Foletto Mora <gusfmora@gmail.com>
Signed-off-by: Mieczyslaw Nalewaj <namiltd@yahoo.com>
---
drivers/net/dsa/realtek/realtek.h | 7 +
drivers/net/dsa/realtek/rtl8365mb_main.c | 13 ++
drivers/net/dsa/realtek/rtl8365mb_vlan.c | 184 ++++++++++++++++++++++-
drivers/net/dsa/realtek/rtl8365mb_vlan.h | 9 ++
4 files changed, 211 insertions(+), 2 deletions(-)
diff --git a/drivers/net/dsa/realtek/realtek.h b/drivers/net/dsa/realtek/realtek.h
index dacb780..c166719 100644
--- a/drivers/net/dsa/realtek/realtek.h
+++ b/drivers/net/dsa/realtek/realtek.h
@@ -8,6 +8,8 @@
#ifndef _REALTEK_H
#define _REALTEK_H
+#include <linux/bitmap.h>
+#include <linux/if_vlan.h>
#include <linux/phy.h>
#include <linux/platform_device.h>
#include <linux/gpio/consumer.h>
@@ -65,6 +67,11 @@ struct realtek_priv {
* deleting port VLAN memberships and PVID configurations.
*/
struct mutex vlan_lock;
+ /* VIDs with a non-empty 4K VLAN entry, protected by vlan_lock. Lets
+ * a switch-wide update visit only the entries in use instead of
+ * reading all 4094 over the slow management bus.
+ */
+ DECLARE_BITMAP(vlan_4k_used, VLAN_N_VID);
/* l2_lock is used to prevent concurrent modifications of L2 table
* entries while another function is reading it. l2_(add,del)_mc
* is an example that first read current table entry and then
diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
index ed7078e..2269ac9 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_main.c
+++ b/drivers/net/dsa/realtek/rtl8365mb_main.c
@@ -2305,8 +2305,21 @@ static int rtl8365mb_port_vlan_filtering(struct dsa_switch *ds, int port,
if (ret)
goto undo_set_framefilter;
+ if (rtl8365mb_is_d(priv)) {
+ mutex_lock(&priv->vlan_lock);
+ ret = rtl8365mb_d_vlan_4k_refresh_ivl(ds, port, vlan_filtering);
+ mutex_unlock(&priv->vlan_lock);
+ if (ret)
+ goto undo_egress_mode;
+ }
+
return ret;
+undo_egress_mode:
+ /* The previous state was the opposite of the new one, see below */
+ (void)rtl8365mb_port_set_vlan_egress_mode(priv, port,
+ vlan_filtering ? RTL8365MB_VLAN_EGRESS_MODE_REAL_KEEP :
+ RTL8365MB_VLAN_EGRESS_MODE_ORIGINAL);
undo_set_framefilter:
if (prev_accepted_frame != accepted_frame)
rtl8365mb_vlan_port_set_framefilter(priv, port,
diff --git a/drivers/net/dsa/realtek/rtl8365mb_vlan.c b/drivers/net/dsa/realtek/rtl8365mb_vlan.c
index 566c6bc..b807835 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_vlan.c
+++ b/drivers/net/dsa/realtek/rtl8365mb_vlan.c
@@ -50,11 +50,15 @@
#include "rtl8365mb_table.h"
#include "rtl8365mb.h"
#include <linux/if_bridge.h>
+#include <linux/if_vlan.h>
#include <linux/lockdep.h>
#include <linux/regmap.h>
-/* CVLAN (i.e. VLAN4k) table entry layout, u16[3] */
+/* CVLAN (i.e. VLAN4k) table entry layout: u16[3] on family C, u16[2] on
+ * family D
+ */
#define RTL8365MB_C_CVLAN_ENTRY_SIZE 3 /* 48-bits */
+#define RTL8365MB_D_CVLAN_ENTRY_SIZE 2 /* 32-bits */
#define RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK GENMASK(7, 0)
#define RTL8365MB_CVLAN_MBR_LO_MASK GENMASK(7, 0)
#define RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK GENMASK(15, 8)
@@ -66,6 +70,10 @@
#define RTL8365MB_CVLAN_ENTRY_D1_METERIDX_MASK GENMASK(13, 9)
#define RTL8365MB_CVLAN_METERIDX_LO_MASK GENMASK(4, 0)
#define RTL8365MB_CVLAN_ENTRY_D1_IVL_SVL_MASK GENMASK(14, 14)
+/* Family D CVLAN entry, word 1 (family D has no word 2) */
+#define RTL8365MB_D_CVLAN_ENTRY_D1_IVL_EN_MASK GENMASK(3, 3)
+/* FID_MSTI in the vendor API: the same bits hold the FID or the MSTI */
+#define RTL8365MB_D_CVLAN_ENTRY_D1_FID_MASK GENMASK(1, 0)
/* extends RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK */
#define RTL8365MB_CVLAN_ENTRY_D2_MBR_EXT_MASK GENMASK(2, 0)
#define RTL8365MB_CVLAN_MBR_HI_MASK GENMASK(10, 8)
@@ -231,6 +239,45 @@ int rtl8365mb_c_vlan_4k_read(struct realtek_priv *priv, u16 vid,
return 0;
}
+int rtl8365mb_d_vlan_4k_read(struct realtek_priv *priv, u16 vid,
+ struct rtl8365mb_vlan4k *vlan4k)
+{
+ u16 data[RTL8365MB_D_CVLAN_ENTRY_SIZE];
+ int val;
+ int ret;
+
+ ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_CVLAN,
+ RTL8365MB_TABLE_OP_READ, &vid, 0, 0,
+ data, RTL8365MB_D_CVLAN_ENTRY_SIZE);
+ if (ret)
+ return ret;
+
+ /* Unpack table entry */
+ memset(vlan4k, 0, sizeof(*vlan4k));
+ vlan4k->vid = vid;
+
+ /* member/untag: d0[7:0]/d0[15:8] on both families. Family C
+ * extends these into data[2] bits [2:0]/[5:3] for its 9th-11th
+ * ports; family D's die has only 8 ports and no third table
+ * word, so data[2] does not exist there and must not be read.
+ */
+ val = FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, data[0]);
+ vlan4k->member = FIELD_PREP(RTL8365MB_CVLAN_MBR_LO_MASK, val);
+
+ val = FIELD_GET(RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK, data[0]);
+ vlan4k->untag = FIELD_PREP(RTL8365MB_CVLAN_UNTAG_LO_MASK, val);
+
+ vlan4k->fid = FIELD_GET(RTL8365MB_D_CVLAN_ENTRY_D1_FID_MASK,
+ data[1]);
+ /* Family D has no priority/meter fields in this entry -
+ * left zeroed by the memset() above.
+ */
+ vlan4k->ivl_en = FIELD_GET(RTL8365MB_D_CVLAN_ENTRY_D1_IVL_EN_MASK,
+ data[1]);
+
+ return 0;
+}
+
int rtl8365mb_c_vlan_4k_write(struct realtek_priv *priv,
const struct rtl8365mb_vlan4k *vlan4k)
{
@@ -277,6 +324,124 @@ int rtl8365mb_c_vlan_4k_write(struct realtek_priv *priv,
data, ARRAY_SIZE(data));
}
+int rtl8365mb_d_vlan_4k_write(struct realtek_priv *priv,
+ const struct rtl8365mb_vlan4k *vlan4k)
+{
+ /* Bit 2 of word 1 is SVLAN_CHK_IVL_SVL in the vendor API; like
+ * the other bits not described above, it is written as zero, as
+ * the vendor API does for this chip family.
+ */
+ u16 data[RTL8365MB_D_CVLAN_ENTRY_SIZE] = { 0 };
+ u16 vid;
+ int val;
+
+ /* Pack table entry value */
+ val = FIELD_GET(RTL8365MB_CVLAN_MBR_LO_MASK, vlan4k->member);
+ data[0] |= FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D0_MBR_MASK, val);
+
+ val = FIELD_GET(RTL8365MB_CVLAN_UNTAG_LO_MASK, vlan4k->untag);
+ data[0] |= FIELD_PREP(RTL8365MB_CVLAN_ENTRY_D0_UNTAG_MASK, val);
+
+ data[1] |= FIELD_PREP(RTL8365MB_D_CVLAN_ENTRY_D1_IVL_EN_MASK,
+ vlan4k->ivl_en);
+ data[1] |= FIELD_PREP(RTL8365MB_D_CVLAN_ENTRY_D1_FID_MASK,
+ vlan4k->fid);
+ /* No priority/meter/member-untag-extension fields exist in
+ * family D's 2-word entry - data[1] and data[0] above are
+ * the whole entry, and data[2] is not part of it at all.
+ */
+
+ vid = vlan4k->vid;
+ return rtl8365mb_table_query(priv, RTL8365MB_TABLE_CVLAN,
+ RTL8365MB_TABLE_OP_WRITE, &vid, 0, 0,
+ data, RTL8365MB_D_CVLAN_ENTRY_SIZE);
+}
+
+/* Family D has no EFID, so a VLAN-unaware bridge must learn in SVL (IVL=0)
+ * to match static FDB/MDB entries programmed with VID 0. Use IVL only while
+ * at least one user port is VLAN-filtering. @port/@filtering override
+ * the state of the port currently being changed, as DSA updates
+ * dp->vlan_filtering only after the driver callback returns.
+ */
+bool rtl8365mb_d_want_ivl(struct dsa_switch *ds, int port, bool filtering)
+{
+ struct dsa_port *dp;
+
+ dsa_switch_for_each_user_port(dp, ds) {
+ if (dp->index == port ? filtering :
+ dsa_port_is_vlan_filtering(dp))
+ return true;
+ }
+
+ return false;
+}
+
+/* Set IVL_EN of every 4K entry in use to @want_ivl. Callers must use it only
+ * for a change of the switch-wide state (see
+ * rtl8365mb_d_vlan_4k_refresh_ivl()), so that every entry in use holds
+ * !@want_ivl on entry. Must be called with &priv->vlan_lock held.
+ */
+int rtl8365mb_d_vlan_4k_set_ivl(struct dsa_switch *ds, bool want_ivl)
+{
+ const struct rtl8365mb_family_info *fi;
+ struct realtek_priv *priv = ds->priv;
+ struct rtl8365mb_vlan4k vlan4k;
+ unsigned int failed;
+ unsigned int vid;
+ int ret;
+
+ lockdep_assert_held(&priv->vlan_lock);
+
+ fi = rtl8365mb_get_family_info(priv);
+
+ for_each_set_bit(vid, priv->vlan_4k_used, VLAN_N_VID) {
+ ret = fi->vlan_4k_read(priv, vid, &vlan4k);
+ if (ret)
+ goto rollback;
+ if (!vlan4k.member || vlan4k.ivl_en == want_ivl)
+ continue;
+
+ vlan4k.ivl_en = want_ivl;
+ ret = fi->vlan_4k_write(priv, &vlan4k);
+ if (ret)
+ goto rollback;
+ }
+
+ return 0;
+
+rollback:
+ /* Best effort: restore the entries rewritten so far. IVL_EN is
+ * switch-wide, so before the scan every entry in use had the old
+ * value (!want_ivl) and every entry below @failed that now has
+ * want_ivl was rewritten above.
+ */
+ failed = vid;
+ for_each_set_bit(vid, priv->vlan_4k_used, failed) {
+ if (fi->vlan_4k_read(priv, vid, &vlan4k))
+ continue;
+ if (!vlan4k.member || vlan4k.ivl_en != want_ivl)
+ continue;
+ vlan4k.ivl_en = !want_ivl;
+ fi->vlan_4k_write(priv, &vlan4k);
+ }
+ return ret;
+}
+
+/* Must be called with &priv->vlan_lock held. */
+int rtl8365mb_d_vlan_4k_refresh_ivl(struct dsa_switch *ds, int port,
+ bool filtering)
+{
+ bool want_ivl = rtl8365mb_d_want_ivl(ds, port, filtering);
+
+ /* The scan visits every entry in use, so do it only if this
+ * change flips the switch-wide state, not for each port of a bridge.
+ */
+ if (want_ivl == rtl8365mb_d_want_ivl(ds, -1, false))
+ return 0;
+
+ return rtl8365mb_d_vlan_4k_set_ivl(ds, want_ivl);
+}
+
static int
rtl8365mb_vlan_4k_port_set(struct dsa_switch *ds, int port,
const struct switchdev_obj_port_vlan *vlan,
@@ -314,7 +479,15 @@ rtl8365mb_vlan_4k_port_set(struct dsa_switch *ds, int port,
vlan4k.untag |= BIT(port);
else
vlan4k.untag &= ~BIT(port);
- vlan4k.ivl_en = true; /* always use Independent VLAN Learning */
+
+ if (!rtl8365mb_is_d(priv))
+ vlan4k.ivl_en = true;
+ else
+ /* An emptied entry goes back to its reset state, so that
+ * it is not left in IVL after it leaves vlan_4k_used.
+ */
+ vlan4k.ivl_en = vlan4k.member &&
+ rtl8365mb_d_want_ivl(ds, -1, false);
ret = fi->vlan_4k_write(priv, &vlan4k);
if (ret) {
@@ -322,6 +495,13 @@ rtl8365mb_vlan_4k_port_set(struct dsa_switch *ds, int port,
return ret;
}
+ if (rtl8365mb_is_d(priv)) {
+ if (vlan4k.member)
+ set_bit(vlan->vid, priv->vlan_4k_used);
+ else
+ clear_bit(vlan->vid, priv->vlan_4k_used);
+ }
+
return 0;
}
diff --git a/drivers/net/dsa/realtek/rtl8365mb_vlan.h b/drivers/net/dsa/realtek/rtl8365mb_vlan.h
index 009b127..f6230eb 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_vlan.h
+++ b/drivers/net/dsa/realtek/rtl8365mb_vlan.h
@@ -24,6 +24,10 @@ int rtl8365mb_c_vlan_4k_read(struct realtek_priv *priv, u16 vid,
struct rtl8365mb_vlan4k *vlan4k);
int rtl8365mb_c_vlan_4k_write(struct realtek_priv *priv,
const struct rtl8365mb_vlan4k *vlan4k);
+int rtl8365mb_d_vlan_4k_read(struct realtek_priv *priv, u16 vid,
+ struct rtl8365mb_vlan4k *vlan4k);
+int rtl8365mb_d_vlan_4k_write(struct realtek_priv *priv,
+ const struct rtl8365mb_vlan4k *vlan4k);
int rtl8365mb_vlan_port_get_pvid(struct realtek_priv *priv, int port,
u16 *pvid);
@@ -43,4 +47,9 @@ int rtl8365mb_vlan_4k_port_del(struct dsa_switch *ds, int port,
int rtl8365mb_vlan_pvid_port_set(struct dsa_switch *ds, int port, u16 vid,
struct netlink_ext_ack *extack);
int rtl8365mb_vlan_pvid_port_clear(struct dsa_switch *ds, int port, u16 vid);
+
+bool rtl8365mb_d_want_ivl(struct dsa_switch *ds, int port, bool filtering);
+int rtl8365mb_d_vlan_4k_set_ivl(struct dsa_switch *ds, bool want_ivl);
+int rtl8365mb_d_vlan_4k_refresh_ivl(struct dsa_switch *ds, int port,
+ bool filtering);
#endif /* _REALTEK_RTL8365MB_VLAN_H */
--
2.53.0
next prev parent reply other threads:[~2026-11-22 9:43 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <3f1826f8-3096-40dd-a278-c70fce4852c8.ref@yahoo.com>
2026-11-22 9:35 ` [PATCH net-next v13 00/14] net: dsa: realtek: rtl8365mb: add support for RTL8367S-VB Mieczyslaw Nalewaj
2026-11-22 9:36 ` [PATCH net-next v13 01/14] dt-bindings: net: dsa: realtek: add RTL8367S-VB Mieczyslaw Nalewaj
2026-11-22 9:37 ` [PATCH net-next v13 02/14] net: dsa: realtek: rtl8365mb: add chip family infrastructure Mieczyslaw Nalewaj
2026-11-22 9:38 ` [PATCH net-next v13 03/14] net: dsa: realtek: rtl8365mb: refactor forcemode handling Mieczyslaw Nalewaj
2026-11-22 9:39 ` [PATCH net-next v13 04/14] net: dsa: realtek: rtl8365mb: set speed for family D Mieczyslaw Nalewaj
2026-11-22 9:41 ` [PATCH net-next v13 05/14] net: dsa: realtek: rtl8365mb: set RGMII mode " Mieczyslaw Nalewaj
2026-11-22 9:42 ` [PATCH net-next v13 06/14] net: dsa: realtek: rtl8365mb: refactor VLAN 4k handling Mieczyslaw Nalewaj
2026-11-22 9:43 ` Mieczyslaw Nalewaj [this message]
2026-11-22 9:44 ` [PATCH net-next v13 08/14] net: dsa: realtek: rtl8365mb: refactor PVID handling Mieczyslaw Nalewaj
2026-11-22 9:45 ` [PATCH net-next v13 09/14] net: dsa: realtek: rtl8365mb: use raw VID for PVID on family D Mieczyslaw Nalewaj
2026-11-22 9:46 ` [PATCH net-next v13 10/14] net: dsa: realtek: rtl8365mb: add family D SDS13 PCS support Mieczyslaw Nalewaj
2026-11-22 9:47 ` [PATCH net-next v13 11/14] net: dsa: realtek: rtl8365mb: refactor L2 MAC handling Mieczyslaw Nalewaj
2026-11-22 9:48 ` [PATCH net-next v13 12/14] net: dsa: realtek: rtl8365mb: make L2/LUT access work on family D Mieczyslaw Nalewaj
2026-11-22 9:49 ` [PATCH net-next v13 13/14] net: dsa: realtek: rtl8365mb: use PCP numbering " Mieczyslaw Nalewaj
2026-11-22 9:50 ` [PATCH net-next v13 14/14] net: dsa: realtek: rtl8365mb: add support for RTL8367S-VB Mieczyslaw Nalewaj
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0552fff4-ac3d-4e2b-8d66-610d0e661e11@yahoo.com \
--to=namiltd@yahoo.com \
--cc=alvin.sipraga@analog.com \
--cc=andrew@lunn.ch \
--cc=contact@c127.dev \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=fabianotassotti@gmail.com \
--cc=gabonpivovich@gmail.com \
--cc=gusfmora@gmail.com \
--cc=kuba@kernel.org \
--cc=kuncy7@gmail.com \
--cc=linusw@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=luizluca@gmail.com \
--cc=maxime.chevallier@bootlin.com \
--cc=netdev@vger.kernel.org \
--cc=olteanv@gmail.com \
--cc=pabeni@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox