[PATCH net-next v8 09/11] net: dsa: realtek: rtl8365mb: refactor L2 MAC handling
From: Mieczyslaw Nalewaj
Date: Tue Nov 03 2026 - 18:36:40 EST
Move the L2 MAC pack/unpack helpers above the family C serializers
and use them there instead of open-coding the byte packing, since the
layout of the MAC address words is the same for both families. The
now unused per-field MAC masks are dropped.
Rename the family C serializers to rtl8365mb_c_l2_*() and keep
rtl8365mb_l2_{data_to_uc,uc_to_data,data_to_mc,mc_to_data}() as thin
dispatchers taking priv, so that a following patch can add the family
D layout without touching the family C code again.
No functional change.
Signed-off-by: Mieczyslaw Nalewaj <namiltd@xxxxxxxxx>
---
drivers/net/dsa/realtek/rtl8365mb_l2.c | 117 +++++++++++++------------
1 file changed, 62 insertions(+), 55 deletions(-)
diff --git a/drivers/net/dsa/realtek/rtl8365mb_l2.c b/drivers/net/dsa/realtek/rtl8365mb_l2.c
index 0494d8a..01cf2d5 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_l2.c
+++ b/drivers/net/dsa/realtek/rtl8365mb_l2.c
@@ -12,12 +12,6 @@
#define RTL8365MB_L2_ENTRY_SIZE 6
-#define RTL8365MB_L2_UC_D0_MAC5_MSK GENMASK(7, 0)
-#define RTL8365MB_L2_UC_D0_MAC4_MSK GENMASK(15, 8)
-#define RTL8365MB_L2_UC_D1_MAC3_MSK GENMASK(7, 0)
-#define RTL8365MB_L2_UC_D1_MAC2_MSK GENMASK(15, 8)
-#define RTL8365MB_L2_UC_D2_MAC1_MSK GENMASK(7, 0)
-#define RTL8365MB_L2_UC_D2_MAC0_MSK GENMASK(15, 8)
#define RTL8365MB_L2_UC_D3_VID_MSK GENMASK(11, 0)
#define RTL8365MB_L2_UC_D3_IVL_MSK GENMASK(13, 13)
#define RTL8365MB_L2_UC_D3_PORT_EXT_MSK GENMASK(15, 15)
@@ -36,12 +30,6 @@
#define RTL8365MB_L2_UC_D5_FWD_PRI_MSK GENMASK(4, 4)
#define RTL8365MB_L2_UC_D5_STATIC_MSK GENMASK(5, 5)
-#define RTL8365MB_L2_MC_D0_MAC5_MSK GENMASK(7, 0)
-#define RTL8365MB_L2_MC_D0_MAC4_MSK GENMASK(15, 8)
-#define RTL8365MB_L2_MC_D1_MAC3_MSK GENMASK(7, 0)
-#define RTL8365MB_L2_MC_D1_MAC2_MSK GENMASK(15, 8)
-#define RTL8365MB_L2_MC_D2_MAC1_MSK GENMASK(7, 0)
-#define RTL8365MB_L2_MC_D2_MAC0_MSK GENMASK(15, 8)
#define RTL8365MB_L2_MC_D3_VID_MSK GENMASK(11, 0)
#define RTL8365MB_L2_MC_D3_IVL_MSK GENMASK(13, 13)
#define RTL8365MB_L2_MC_D3_MBR_HI1_MSK GENMASK(15, 14)
@@ -131,16 +119,29 @@ struct rtl8365mb_l2_mc {
bool igmp_asic;
};
-static void rtl8365mb_l2_data_to_uc(const u16 *data, struct rtl8365mb_l2_uc *uc)
+static void rtl8365mb_l2_mac_to_data(const u8 *m, u16 *data)
+{
+ data[0] = (m[4] << 8) | m[5];
+ data[1] = (m[2] << 8) | m[3];
+ data[2] = (m[0] << 8) | m[1];
+}
+
+static void rtl8365mb_l2_data_to_mac(const u16 *data, u8 *m)
+{
+ m[5] = data[0] & 0xff;
+ m[4] = data[0] >> 8;
+ m[3] = data[1] & 0xff;
+ m[2] = data[1] >> 8;
+ m[1] = data[2] & 0xff;
+ m[0] = data[2] >> 8;
+}
+
+static void rtl8365mb_c_l2_data_to_uc(const u16 *data,
+ struct rtl8365mb_l2_uc *uc)
{
u32 val;
- uc->key.mac_addr[5] = FIELD_GET(RTL8365MB_L2_UC_D0_MAC5_MSK, data[0]);
- uc->key.mac_addr[4] = FIELD_GET(RTL8365MB_L2_UC_D0_MAC4_MSK, data[0]);
- uc->key.mac_addr[3] = FIELD_GET(RTL8365MB_L2_UC_D1_MAC3_MSK, data[1]);
- uc->key.mac_addr[2] = FIELD_GET(RTL8365MB_L2_UC_D1_MAC2_MSK, data[1]);
- uc->key.mac_addr[1] = FIELD_GET(RTL8365MB_L2_UC_D2_MAC1_MSK, data[2]);
- uc->key.mac_addr[0] = FIELD_GET(RTL8365MB_L2_UC_D2_MAC0_MSK, data[2]);
+ rtl8365mb_l2_data_to_mac(data, uc->key.mac_addr);
uc->key.efid = FIELD_GET(RTL8365MB_L2_UC_D4_EFID_MSK, data[4]);
uc->key.vid = FIELD_GET(RTL8365MB_L2_UC_D3_VID_MSK, data[3]);
uc->key.ivl = FIELD_GET(RTL8365MB_L2_UC_D3_IVL_MSK, data[3]);
@@ -161,23 +162,13 @@ static void rtl8365mb_l2_data_to_uc(const u16 *data, struct rtl8365mb_l2_uc *uc)
uc->is_static = FIELD_GET(RTL8365MB_L2_UC_D5_STATIC_MSK, data[5]);
}
-static void rtl8365mb_l2_uc_to_data(const struct rtl8365mb_l2_uc *uc, u16 *data)
+static void rtl8365mb_c_l2_uc_to_data(const struct rtl8365mb_l2_uc *uc,
+ u16 *data)
{
u32 val;
memset(data, 0, RTL8365MB_L2_ENTRY_SIZE * 2);
- data[0] |=
- FIELD_PREP(RTL8365MB_L2_UC_D0_MAC5_MSK, uc->key.mac_addr[5]);
- data[0] |=
- FIELD_PREP(RTL8365MB_L2_UC_D0_MAC4_MSK, uc->key.mac_addr[4]);
- data[1] |=
- FIELD_PREP(RTL8365MB_L2_UC_D1_MAC3_MSK, uc->key.mac_addr[3]);
- data[1] |=
- FIELD_PREP(RTL8365MB_L2_UC_D1_MAC2_MSK, uc->key.mac_addr[2]);
- data[2] |=
- FIELD_PREP(RTL8365MB_L2_UC_D2_MAC1_MSK, uc->key.mac_addr[1]);
- data[2] |=
- FIELD_PREP(RTL8365MB_L2_UC_D2_MAC0_MSK, uc->key.mac_addr[0]);
+ rtl8365mb_l2_mac_to_data(uc->key.mac_addr, data);
data[3] |= FIELD_PREP(RTL8365MB_L2_UC_D3_VID_MSK, uc->key.vid);
data[3] |= FIELD_PREP(RTL8365MB_L2_UC_D3_IVL_MSK, uc->key.ivl);
@@ -200,16 +191,12 @@ static void rtl8365mb_l2_uc_to_data(const struct rtl8365mb_l2_uc *uc, u16 *data)
data[5] |= FIELD_PREP(RTL8365MB_L2_UC_D5_STATIC_MSK, uc->is_static);
}
-static void rtl8365mb_l2_data_to_mc(const u16 *data, struct rtl8365mb_l2_mc *mc)
+static void rtl8365mb_c_l2_data_to_mc(const u16 *data,
+ struct rtl8365mb_l2_mc *mc)
{
u32 val;
- mc->key.mac_addr[5] = FIELD_GET(RTL8365MB_L2_MC_D0_MAC5_MSK, data[0]);
- mc->key.mac_addr[4] = FIELD_GET(RTL8365MB_L2_MC_D0_MAC4_MSK, data[0]);
- mc->key.mac_addr[3] = FIELD_GET(RTL8365MB_L2_MC_D1_MAC3_MSK, data[1]);
- mc->key.mac_addr[2] = FIELD_GET(RTL8365MB_L2_MC_D1_MAC2_MSK, data[1]);
- mc->key.mac_addr[1] = FIELD_GET(RTL8365MB_L2_MC_D2_MAC1_MSK, data[2]);
- mc->key.mac_addr[0] = FIELD_GET(RTL8365MB_L2_MC_D2_MAC0_MSK, data[2]);
+ rtl8365mb_l2_data_to_mac(data, mc->key.mac_addr);
/* key.vid,key.fid shares the same memory space */
mc->key.vid = FIELD_GET(RTL8365MB_L2_MC_D3_VID_MSK, data[3]);
mc->key.ivl = FIELD_GET(RTL8365MB_L2_MC_D3_IVL_MSK, data[3]);
@@ -228,17 +215,13 @@ static void rtl8365mb_l2_data_to_mc(const u16 *data, struct rtl8365mb_l2_mc *mc)
mc->igmp_asic = FIELD_GET(RTL8365MB_L2_MC_D5_IGMP_ASIC_MSK, data[5]);
}
-static void rtl8365mb_l2_mc_to_data(const struct rtl8365mb_l2_mc *mc, u16 *data)
+static void rtl8365mb_c_l2_mc_to_data(const struct rtl8365mb_l2_mc *mc,
+ u16 *data)
{
u32 val;
memset(data, 0, RTL8365MB_L2_ENTRY_SIZE * 2);
- data[0] |= FIELD_PREP(RTL8365MB_L2_MC_D0_MAC5_MSK, mc->key.mac_addr[5]);
- data[0] |= FIELD_PREP(RTL8365MB_L2_MC_D0_MAC4_MSK, mc->key.mac_addr[4]);
- data[1] |= FIELD_PREP(RTL8365MB_L2_MC_D1_MAC3_MSK, mc->key.mac_addr[3]);
- data[1] |= FIELD_PREP(RTL8365MB_L2_MC_D1_MAC2_MSK, mc->key.mac_addr[2]);
- data[2] |= FIELD_PREP(RTL8365MB_L2_MC_D2_MAC1_MSK, mc->key.mac_addr[1]);
- data[2] |= FIELD_PREP(RTL8365MB_L2_MC_D2_MAC0_MSK, mc->key.mac_addr[0]);
+ rtl8365mb_l2_mac_to_data(mc->key.mac_addr, data);
data[3] |= FIELD_PREP(RTL8365MB_L2_MC_D3_VID_MSK, mc->key.vid);
data[3] |= FIELD_PREP(RTL8365MB_L2_MC_D3_IVL_MSK, mc->key.ivl);
@@ -258,6 +241,30 @@ static void rtl8365mb_l2_mc_to_data(const struct rtl8365mb_l2_mc *mc, u16 *data)
data[5] |= FIELD_PREP(RTL8365MB_L2_MC_D5_MBR_HI2_MSK, val);
}
+static void rtl8365mb_l2_data_to_uc(struct realtek_priv *priv, const u16 *data,
+ struct rtl8365mb_l2_uc *uc)
+{
+ rtl8365mb_c_l2_data_to_uc(data, uc);
+}
+
+static void rtl8365mb_l2_uc_to_data(struct realtek_priv *priv,
+ const struct rtl8365mb_l2_uc *uc, u16 *data)
+{
+ rtl8365mb_c_l2_uc_to_data(uc, data);
+}
+
+static void rtl8365mb_l2_data_to_mc(struct realtek_priv *priv, const u16 *data,
+ struct rtl8365mb_l2_mc *mc)
+{
+ rtl8365mb_c_l2_data_to_mc(data, mc);
+}
+
+static void rtl8365mb_l2_mc_to_data(struct realtek_priv *priv,
+ const struct rtl8365mb_l2_mc *mc, u16 *data)
+{
+ rtl8365mb_c_l2_mc_to_data(mc, data);
+}
+
/*
* rtl8365mb_l2_get_next_uc() - get the next Unicast L2 entry
* @priv: realtek_priv pointer
@@ -293,7 +300,7 @@ int rtl8365mb_l2_get_next_uc(struct realtek_priv *priv, u16 *addr, int port,
if (ret)
return ret;
- rtl8365mb_l2_data_to_uc(data, &uc);
+ rtl8365mb_l2_data_to_uc(priv, data, &uc);
ether_addr_copy(entry->mac_addr, uc.key.mac_addr);
entry->vid = uc.key.vid;
@@ -326,7 +333,7 @@ int rtl8365mb_l2_add_uc(struct realtek_priv *priv, int port,
uc.is_static = true;
/* age greater than 0 adds/updates entries */
uc.age = 1;
- rtl8365mb_l2_uc_to_data(&uc, data);
+ rtl8365mb_l2_uc_to_data(priv, &uc, data);
/* add the new entry or update an existing one */
ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
@@ -358,7 +365,7 @@ int rtl8365mb_l2_del_uc(struct realtek_priv *priv, int port,
uc.key.vid = vid;
/* age 0 deletes the entry */
uc.age = 0;
- rtl8365mb_l2_uc_to_data(&uc, data);
+ rtl8365mb_l2_uc_to_data(priv, &uc, data);
/* it looks like the switch will always add/update the entry,
* even when age is 0 or uc.key did not match an existing entry,
@@ -463,7 +470,7 @@ int rtl8365mb_l2_add_mc(struct realtek_priv *priv, int port,
*/
mc.member |= BIT(port);
mc.is_static = 1;
- rtl8365mb_l2_mc_to_data(&mc, data);
+ rtl8365mb_l2_mc_to_data(priv, &mc, data);
/* First look for an existing entry (to get existing port members) */
ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
@@ -472,7 +479,7 @@ int rtl8365mb_l2_add_mc(struct realtek_priv *priv, int port,
data, RTL8365MB_L2_ENTRY_SIZE);
if (!ret) {
/* There is already an entry... */
- rtl8365mb_l2_data_to_mc(data, &mc);
+ rtl8365mb_l2_data_to_mc(priv, data, &mc);
dev_dbg(priv->dev,
"%s: found %pM addr=%d member=0x%x igmpidx=0x%x %s\n",
__func__, mac_addr, addr, mc.member, mc.igmpidx,
@@ -487,7 +494,7 @@ int rtl8365mb_l2_add_mc(struct realtek_priv *priv, int port,
mc.is_static = 1;
}
- rtl8365mb_l2_mc_to_data(&mc, data);
+ rtl8365mb_l2_mc_to_data(priv, &mc, data);
} else if (ret == -ENOENT) {
/* New entry, no need to update data again as it already
* includes the member.
@@ -529,7 +536,7 @@ int rtl8365mb_l2_del_mc(struct realtek_priv *priv, int port,
memcpy(mc.key.mac_addr, mac_addr, ETH_ALEN);
mc.key.vid = vid;
mc.key.ivl = true;
- rtl8365mb_l2_mc_to_data(&mc, data);
+ rtl8365mb_l2_mc_to_data(priv, &mc, data);
/* First look for an existing entry (to get existing port members) */
ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
@@ -547,7 +554,7 @@ int rtl8365mb_l2_del_mc(struct realtek_priv *priv, int port,
/* Return on any other error */
return ret;
- rtl8365mb_l2_data_to_mc(data, &mc);
+ rtl8365mb_l2_data_to_mc(priv, data, &mc);
dev_dbg(priv->dev,
"%s: found %pM addr=%d member=0x%x igmpidx=0x%x %s\n",
__func__, mac_addr, addr, mc.member, mc.igmpidx,
@@ -565,7 +572,7 @@ int rtl8365mb_l2_del_mc(struct realtek_priv *priv, int port,
mc.fwd_pri = 0;
mc.igmp_asic = 0;
}
- rtl8365mb_l2_mc_to_data(&mc, data);
+ rtl8365mb_l2_mc_to_data(priv, &mc, data);
/* update the existing entry. */
ret = rtl8365mb_table_query(priv, RTL8365MB_TABLE_L2,
--
2.53.0