Skip to contentSkip to conversation

net: dsa: realtek: rtl8365mb: bridge offloading and VLAN support

PATCH Superseded Luiz Angelo Daros de Luca 3 participants 14 messages Jun 1 – Jun 5

Subsystems (4)

v11 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Luiz Angelo Daros de Luca

author maintainer
#0 lore raw

v11 cover 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.

Changes in v11:
* Patch 2 (unsupported topologies):
- Moved the check for DSA links to an early pre-flight phase before configuring any ports.
- Omitted "Link" from the missing upstream port error message. If
cascading is supported in the future, that message will be updated
as well.
* Patch 3 (DSA helpers):
- Reworded the commit message to clarify that unused ports are
explicitly blocked.
- Reworded the commit message to explain why all IRQ mappings need to
be cleaned during teardown, even for unused ports.
* Patch 6 (VLAN):
- Added the same VID==0 guard for the vlan port del path.
- Reworded the VID==0 error message, mentioning it is reserved by the
driver and not "unsupported by the HW".
- Link to v10: https://patch.msgid.link/[email protected]

Changes in v10:
* Patch 2->2,3 (DSA helpers):
- Split the unsupported topology checks into a new preparation patch.
* Patch 4->5 (Table lookup):
- Fix port mask size in the table lookup command.
* Patch 5->6 (VLAN support):
- Only remove a failed rtl8365mb_vlan_pvid_port_set() from
VlanMC if it wasn't already a member.
* Patch 6->7 (Bridge Join/Leave):
- Bridge join should only enable learning after efid was set. If not, a learned MAC could be leaked to EFID 0.
- Link to v9: https://patch.msgid.link/[email protected]

Changes in v9:
* Patch 2 (DSA helpers):
- Switch to dsa_switch_for_each_port() in the irq_setup error path.
* Patch 4 (Table lookup):
- Convert kdoc to standard comments.
- Fix doc description for L2 write operation return value and
data/addr on errors.
- Removed misleading comment on RTL8365MB_TABLE_CTRL_PORT_MASK.
* Patch 5 (VLAN support):
- Do PVID first in port_vlan_add() to avoid reverting vlan4k changes
if it fails.
- Do not use dsa_switch_for_each_port_continue_reverse() after the
loop ends (use-after-iterator).
- Simplify PVID helpers to receive VID instead of switchdev vlan.
- Avoid redundant writes in the PVID error path.
- Convert kdoc to standard comments.
* Patch 6 (Bridge Join/Leave):
- Do not use dsa_switch_for_each_port_continue_reverse() after the
loop ends (use-after-iterator).
- Link to v8: https://patch.msgid.link/[email protected]

Changes in v8:
* Patch 4 (Table lookup):
- Revert -ENOSPC suggestion from Sashiko. It is better for the caller
to handle the semantic meaning of table query failures, as write operations are also used for deletion where -ENOSPC makes no sense. Physical lookup/hit failures in table_query() now simply return -ENOENT.
* Patch 5 (VLAN):
- Removed unused attribution to variable dp.
* Patch 6 (Bridge Join/Leave):
- Migrate the .port_set_learning callback definition from the bridge
flags patch to ensure MAC learning can be explicitly enabled on
bridge join and disabled on bridge leave, preserving bisectability.
* Patch 7 (FDB):
- Reroute fdb_add/del to l2_add_mc/l2_del_mc if the MAC address is multicast to prevent bit corruption in the unified LUT.
- Silence -ENOENT (and -ENOSPC on write-based deletion) when fdb_del
tries to delete a missing entry, as it could have expired in the meantime. Repeated deletion calls are already filtered out by the
upper layers before reaching the driver.
- Improved multicast deletion debug message to include the MAC address.
* Patch 8 (Bridge Flags)
- .port_set_learning was moved to patch 6
- Link to v7: https://patch.msgid.link/[email protected]

Changes in v7:
* Patch 2 (DSA helpers):
- Switch from dsa_switch_for_each_available_port() to dsa_switch_for_each_port() in irq_teardown() to properly dispose
of unused ports' mappings.
- Reorder the !cpu->enable check in setup() to run before chip configuration.
- Add a detailed comment explaining the limitation of the unsupported cascading layout.
- Expand the commit message to document the pre-existing IRQ handler
guard and the new -EINVAL probe failure mode.
* Patch 5 (VLAN):
- Replace br_vlan_get_pvid() with rtl8365mb_vlan_port_get_pvid(), which uses the HW info directly.
- Rename the existing internal static helper to rtl8365mb_vlan_port_get_pvid_idx() to cleanly export the new PVID retrieval helper.
- Add a logical guard check in rtl8365mb_vlan_pvid_port_add() to prevent accidental deletion of VLAN MC entries when reconfiguring an identical PVID.
- Removed spurious rtl8365mb_vlan.c.orig file.
* Patch 7 (L2):
- Clarify CPU port EFID and assisted learning comment in fdb_add().
- Comment that EFID computation in mdb_add/del is for debugging only.
* Patch 8 (bridge flags):
- Added missing '\n' in rtl8365mb_port_pre_bridge_flags() dbg message.
- Link to v6: https://patch.msgid.link/[email protected]

Changes in v6:
* General:
- Added missing Reviewed-by tags across the series.
- Removed redundant parentheses around the dp macro evaluation ((dp)).
* Patch 1 (err_ptr):
- Updated the commit message to document the use of dev_err_probe().
- Fixed a minor typo (failed map IRQ -> failed to map IRQ).
* Patch 2 (DSA helpers):
- Fixed a space typo (Cascading(DSA link) -> Cascading (DSA link)).
- Added a safety check to abort configuration early with -EINVAL
if no upstream (CPU/Link) port is active (!cpu->enable).
- Fixed error handling inside port configuration loop that was not
using out_teardown_irq.
* Patch 5 (VLAN):
- Added a vlan_lock mutex to protect read-modify-write ops in vlan4k
and vlanMC, used in vlan_port_add/del.
- Set framefilter to tagged-only only if vlan filtering is enabled and
PVID is not active (checked both while changing vlan_filtering or
adding a PVID port).
- Added rtl8365mb_vlan_mc_get_pvid() helper function.
- Added rtl8365mb_vlan_port_get_framefilter() and made rtl8365mb_vlan_port_set_framefilter() and enum
rtl8365mb_frame_ingress public.
- Explicitly reject requests for VLAN 0 with -EOPNOTSUPP.
- Fixed string formatting bounds for the vlanMC overflow.
- rtl8365mb_vlan_pvid_port_{add,del} error handling now attempts to
return to the previous PVID state.
- rtl8365mb_vlan_pvid_port_del with vlan NULL (without informing the
VID) is not needed anymore.
- Added kdocs to public functions
* Patch 7 (L2):
- Removed check for -ENOENT on L2 write as the table access already
returns the expected error.
- Increased L2 polling timeout to 10000us, matching the table polling.
- Improved l2_{add/del}_mc debug messages while strictly respecting
the 80-column limit.
- If an unexpected dynamic multicast group exists while adding a new
port, promote that group to static.
- Improved comment about how HW deletes a multicast group.
- Link to v5: https://patch.msgid.link/[email protected]

Changes in v5:
* General:
- Removed Alvin from Co-Developed-By and added myself.
* Patch 1 (err_ptr):
- Fixed error message on failed IRQ mapping
* Patch 2 (DSA helpers):
- Fixed missing irq teardown when DSA link port was detected.
- Added missing \n in error message when DSA link port is detected.
* Patch 4 (Table lookup):
- Fixed RTL8365MB_TABLE_ADDR_MASK mask
- Added a check for busy table access before preparing and issuing
a new command (protecting from a failed previous access)
- Increased table timeout from 100us to 10000us
- Changed -ENOENT to -ENOSPC when writing to L2 failed
- Moved RTL8365MB_TABLE_ENTRY_MAX_SIZE to table.h (as kdoc mentions it)
* Patch 5 (VLAN):
- Removed duplicated filtering registers macros
- Removed wrong comment on undo_transparent
- Forced the removal of a possible PVID state if an added port
is not PVID
- Added vlanmc->priority_en to VlanMC reading, although always zero
- Use VlanMC member just for tracking ports using PVID and do not
try to keep it in sync with Vlan4k. rtl8365mb_vlan_mc_pvid_members()
is not needed anymore.
- Changed rtl8365mb_vlan_mc_port_set signature to receive vid and pvid
instead of the vlan struct to avoid null pointer dereference
* Patch 7 (L2):
- Removed addr from rtl8365mb_l2_del_mc when -ENOENT is returned by the table_access as it is undefined (noticed by sashiko)
- Added RTL8365MB_L2_TABLE_SIZE and use it instead of RTL8365MB_LEARN_LIMIT_MAX when needed
- Extended RTL8365MB_LEARN_LIMIT_MAX comment to cite that the exception RTL8370B might not even be a RTL8367C member
- Moved l2_lock init to rtl83xx.c as it is a field in a common struct.
- Fixed fdb_dump kdoc, now returning OK when the table is empty
- Expaned L2 commit to cite unicast/multicast table key
differences
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
* General:
- Fixed comments/kdoc wording identified by Sashiko.
* Patch 1 (err_prt):
- Added missing %pe/ERR_PTR conversion identified by Sashiko.
* Patch 2 (dsa macros):
- Reorganize port setup initialization, collecting ports in upports_mask and downports_mask.
- Include a fail fast during setup if DSA links ports are detected.
* Patch 4 (table):
- Added missing return value check for regmap_bulk_read in table access (spotted by Sashiko).
* Patch 5 (vlan):
- Added undo steps to rtl8365mb_port_vlan_filtering (noticed by
Sashiko).
- Removed residual double max packet configuation (noticed by Sashiko).
- Added meteridx and policing_en to rtl8365mb_vlan_mc_read, although
always written as zero in rtl8365mb_vlan_mc_write() (noticed by
Sashiko).
- Explicitly comment that VlanMC[0] uses EVID==0 (Sashiko was worried).
- Always undo previous PVID setting before applying a new one. It might exhaust VlanMC entries if the user keeps changing PVID without removing the port from the previous PVID VLAN (bug detected by Sashiko).
- Fixed a bug where a missing VLAN MC entry during PVID assignment would return success instead of an error (reportd by Sashiko).
* Patch 6 (bridge join/leave):
- Added error unwinding path (undo isolation and EFID) in rtl83xx_port_bridge_join (noticed by Sashiko).
* Patch 7 (FDB support):
- Added RTL8365MB_L2_ENTRY_SIZE macro.
- Aixed RTL8365MB_L2_FLUSH_CTRL2_TYPE_BOTH definition (noticed by
Sashiko).
- Removed the union in rtl8365mb_l2_uc_key. Each field is independent
now (bug detected by by Sashiko).
- Fixed mc->member calculation. Now it uses bitfield to rebuild the
Value (bug detected by Sashiko).
- Return mdb_del without errors if entry is missing.
- Improve debug messages for l2 operations.
- Added comment about ds->assisted_learning_on_cpu_port and
fdb_isolation in the commit message (asked by Sashiko)
- Renamed some _MASK macros to _MSK to reduce the name length.
** rtl83xx_port_fdb_dump fixes:
- Initialize entry as null (noticed by Sashiko)
- Check return of cb() (bugfix reported by Sashiko)
- Fix address overflow (noticed by Sashiko)
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Fixed kernel-doc warnings
- Removed unnecessary defensive checks
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Added patch to use ERR_PTR()
- Dropped bitfield patch. Use FIELD_PREP instead. Suggested by Yury Norov
- tag_rtl8_4 patches were submitted on its own series (already accepted)
- Dropped rtl8365mb_vlan_mc_port_{add,del}(). rtl8365mb_vlan_mc_port_set is now called directly from PVID methods.
- Reordered methods in rtl8365mb_vlan.c
- Use dsa_switch_for_each_user_port() instead of simple for in bridge port join/leave
- PVID check now uses dsa_switch_for_each_available_port instead of dsa_switch_for_each_port
- Set EFID of user ports to 0 at setup(), although it is the expected state after reset
- STP patch was dropped and replaced by a more extensive one that disables all ports (including unused ones) before setting CPU and user ports. It also extended the CPU port isolation to include all user ports.
- Refactored bridge, FDB, and MDB port operations into the common rtl83xx module, introducing new realtek_ops callbacks to abstract the hardware access
- Collected Reviewed-by and Suggested-by tags
- Link to v1:
https://patch.msgid.link/[email protected]

To: Linus Walleij <[email protected]>
To: Alvin Šipraga <[email protected]>
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: Russell King <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
Alvin Šipraga (5):
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

Luiz Angelo Daros de Luca (4):
net: dsa: realtek: rtl8365mb: use ERR_PTR
net: dsa: realtek: rtl8365mb: reject unsupported topologies
net: dsa: realtek: rtl8365mb: use dsa helpers for port iteration
net: dsa: realtek: rtl8365mb: add bridge port flags

 drivers/net/dsa/realtek/Makefile                   |   4 +
 drivers/net/dsa/realtek/realtek.h                  |  47 +
 drivers/net/dsa/realtek/rtl8365mb_l2.c             | 566 ++++++++++++
 drivers/net/dsa/realtek/rtl8365mb_l2.h             |  32 +
 .../dsa/realtek/{rtl8365mb.c => rtl8365mb_main.c}  | 684 +++++++++++++--
 drivers/net/dsa/realtek/rtl8365mb_table.c          | 214 +++++
 drivers/net/dsa/realtek/rtl8365mb_table.h          | 138 +++
 drivers/net/dsa/realtek/rtl8365mb_vlan.c           | 955 +++++++++++++++++++++
 drivers/net/dsa/realtek/rtl8365mb_vlan.h           |  39 +
 drivers/net/dsa/realtek/rtl83xx.c                  | 569 ++++++++++++
 drivers/net/dsa/realtek/rtl83xx.h                  |  27 +
 11 files changed, 3203 insertions(+), 72 deletions(-)

---
base-commit: 627ac78f2741e2ebd2225e2e953b6964a8a9182f
change-id: 20260323-realtek_forward-1bac3a77c664

Best regards,

Signature · 1 line
Luiz Angelo Daros de Luca <[email protected]>
author maintainer
#1 lore raw

PATCH v11 1/9 net: dsa: realtek: rtl8365mb: use ERR_PTR

Convert numeric error codes into human-readable strings by using %pe together with ERR_PTR() in dev_err() messages. Also use dev_err_probe() instead of checking for -EPROBE_DEFER.

---
 drivers/net/dsa/realtek/rtl8365mb.c | 51 +++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 22 deletions(-)
drivers/net/dsa/realtek/rtl8365mb.c +29 -22
index 0da048da533a..2637884fe472 100644
@@ -803,8 +803,8 @@ static int rtl8365mb_phy_read(struct realtek_priv *priv, int phy, int regnum)
 	ret = rtl8365mb_phy_ocp_read(priv, phy, ocp_addr, &val);
 	if (ret) {
 		dev_err(priv->dev,
-			"failed to read PHY%d reg %02x @ %04x, ret %d\n", phy,
-			regnum, ocp_addr, ret);
+			"failed to read PHY%d reg %02x @ %04x, ret %pe\n", phy,
+			regnum, ocp_addr, ERR_PTR(ret));
 		return ret;
 	}
 
@@ -831,8 +831,8 @@ static int rtl8365mb_phy_write(struct realtek_priv *priv, int phy, int regnum,
 	ret = rtl8365mb_phy_ocp_write(priv, phy, ocp_addr, val);
 	if (ret) {
 		dev_err(priv->dev,
-			"failed to write PHY%d reg %02x @ %04x, ret %d\n", phy,
-			regnum, ocp_addr, ret);
+			"failed to write PHY%d reg %02x @ %04x, ret %pe\n", phy,
+			regnum, ocp_addr, ERR_PTR(ret));
 		return ret;
 	}
 
@@ -1082,8 +1082,8 @@ static void rtl8365mb_phylink_mac_config(struct phylink_config *config,
 		ret = rtl8365mb_ext_config_rgmii(priv, port, state->interface);
 		if (ret)
 			dev_err(priv->dev,
-				"failed to configure RGMII mode on port %d: %d\n",
-				port, ret);
+				"failed to configure RGMII mode on port %d: %pe\n",
+				port, ERR_PTR(ret));
 		return;
 	}
 
@@ -1112,8 +1112,8 @@ static void rtl8365mb_phylink_mac_link_down(struct phylink_config *config,
 						     false, false);
 		if (ret)
 			dev_err(priv->dev,
-				"failed to reset forced mode on port %d: %d\n",
-				port, ret);
+				"failed to reset forced mode on port %d: %pe\n",
+				port, ERR_PTR(ret));
 
 		return;
 	}
@@ -1143,8 +1143,8 @@ static void rtl8365mb_phylink_mac_link_up(struct phylink_config *config,
 						     rx_pause);
 		if (ret)
 			dev_err(priv->dev,
-				"failed to force mode on port %d: %d\n", port,
-				ret);
+				"failed to force mode on port %d: %pe\n", port,
+				ERR_PTR(ret));
 
 		return;
 	}
@@ -1299,8 +1299,8 @@ static void rtl8365mb_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *da
 						 mib->length, &data[i]);
 		if (ret) {
 			dev_err(priv->dev,
-				"failed to read port %d counters: %d\n", port,
-				ret);
+				"failed to read port %d counters: %pe\n", port,
+				ERR_PTR(ret));
 			break;
 		}
 	}
@@ -1652,7 +1652,8 @@ static irqreturn_t rtl8365mb_irq(int irq, void *data)
 	return IRQ_HANDLED;
 
 out_error:
-	dev_err(priv->dev, "failed to read interrupt status: %d\n", ret);
+	dev_err(priv->dev, "failed to read interrupt status: %pe\n",
+		ERR_PTR(ret));
 
 out_none:
 	return IRQ_NONE;
@@ -1725,10 +1726,13 @@ static int rtl8365mb_irq_setup(struct realtek_priv *priv)
 	/* rtl8365mb IRQs cascade off this one */
 	irq = of_irq_get(intc, 0);
 	if (irq <= 0) {
-		if (irq != -EPROBE_DEFER)
-			dev_err(priv->dev, "failed to get parent irq: %d\n",
-				irq);
-		ret = irq ? irq : -EINVAL;
+		if (!irq) {
+			dev_err(priv->dev, "failed to map IRQ\n");
+			ret = -EINVAL;
+		} else {
+			ret = dev_err_probe(priv->dev, irq,
+					    "failed to get parent irq\n");
+		}
 		goto out_put_node;
 	}
 
@@ -1790,7 +1794,8 @@ static int rtl8365mb_irq_setup(struct realtek_priv *priv)
 	ret = request_threaded_irq(irq, NULL, rtl8365mb_irq, IRQF_ONESHOT,
 				   "rtl8365mb", priv);
 	if (ret) {
-		dev_err(priv->dev, "failed to request irq: %d\n", ret);
+		dev_err(priv->dev, "failed to request irq: %pe\n",
+			ERR_PTR(ret));
 		goto out_remove_irqdomain;
 	}
 
@@ -1966,14 +1971,16 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 
 	ret = rtl8365mb_reset_chip(priv);
 	if (ret) {
-		dev_err(priv->dev, "failed to reset chip: %d\n", ret);
+		dev_err(priv->dev, "failed to reset chip: %pe\n",
+			ERR_PTR(ret));
 		goto out_error;
 	}
 
 	/* Configure switch to vendor-defined initial state */
 	ret = rtl8365mb_switch_init(priv);
 	if (ret) {
-		dev_err(priv->dev, "failed to initialize switch: %d\n", ret);
+		dev_err(priv->dev, "failed to initialize switch: %pe\n",
+			ERR_PTR(ret));
 		goto out_error;
 	}
 
@@ -2091,8 +2098,8 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 
 	ret = rtl8365mb_get_chip_id_and_ver(priv->map, &chip_id, &chip_ver);
 	if (ret) {
-		dev_err(priv->dev, "failed to read chip id and version: %d\n",
-			ret);
+		dev_err(priv->dev, "failed to read chip id and version: %pe\n",
+			ERR_PTR(ret));
 		return ret;
 	}
 
Signature · 1 line
2.54.0
author maintainer
#2 lore raw

PATCH v11 2/9 net: dsa: realtek: rtl8365mb: reject unsupported topologies

In reply to Luiz Angelo Daros de Luca

Explicitly enforce the presence of a CPU port (-EINVAL) and reject DSA cascade links (-EOPNOTSUPP) during setup to prevent silent failures.

These topologies were already non-functional. Without a CPU port, the driver does not activate CPU tagging. Additionally, the switch hardware was not designed to be cascaded, and DSA links never worked because CPU tagging is not enabled for them.

---
 drivers/net/dsa/realtek/rtl8365mb.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/net/dsa/realtek/rtl8365mb.c +21 -0
index 2637884fe472..37e1d7654b1d 100644
@@ -1991,6 +1991,20 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 	else if (ret)
 		dev_info(priv->dev, "no interrupt support\n");
 
+	for (i = 0; i < priv->num_ports; i++) {
+		/* Cascading (DSA links) is not supported yet.
+		 * Historically, the driver has always been broken
+		 * without a dedicated CPU port because CPU tagging
+		 * would be disabled, rendering the switch entirely
+		 * non-functional for DSA operations.
+		 */
+		if (dsa_is_dsa_port(ds, i)) {
+			dev_err(priv->dev, "Cascading (DSA link) not supported\n");
+			ret = -EOPNOTSUPP;
+			goto out_teardown_irq;
+		}
+	}
+
 	/* Configure CPU tagging */
 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
 		cpu->mask |= BIT(cpu_dp->index);
@@ -1999,6 +2013,13 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 			cpu->trap_port = cpu_dp->index;
 	}
 	cpu->enable = cpu->mask > 0;
+
+	if (!cpu->enable) {
+		dev_err(priv->dev, "no CPU port defined\n");
+		ret = -EINVAL;
+		goto out_teardown_irq;
+	}
+
 	ret = rtl8365mb_cpu_config(priv);
 	if (ret)
 		goto out_teardown_irq;
Signature · 1 line
2.54.0
maintainer Reviewed-by on v11 2/9
#3 lore raw

Re: [net-next PATCH v11 2/9] net: dsa: realtek: rtl8365mb: reject unsupported topologies

Quoted 10 lines · Luiz Angelo Daros de Luca

Explicitly enforce the presence of a CPU port (-EINVAL) and reject DSA cascade links (-EOPNOTSUPP) during setup to prevent silent failures.

These topologies were already non-functional. Without a CPU port, the driver does not activate CPU tagging. Additionally, the switch hardware was not designed to be cascaded, and DSA links never worked because CPU tagging is not enabled for them.

Yours,
Linus Walleij

author maintainer
#4 lore raw

PATCH v11 3/9 net: dsa: realtek: rtl8365mb: use dsa helpers for port iteration

In reply to Luiz Angelo Daros de Luca

Convert open-coded port iteration loops to use the DSA helpers and restructure rtl8365mb_setup() into clear blocking, user, and CPU port phases.

As part of this refactoring, unused ports are explicitly placed into a blocked, isolated state with learning disabled, ensuring safe default hardware behavior. The driver also does not allocate a virtual IRQ mapping for unused ports. To accommodate this, a guard check is added to the interrupt handler (rtl8365mb_irq) to safely skip ports without a valid IRQ mapping. The irq domain teardown, however, does clean all ports as external PHYs may still map the IRQ.

Furthermore, since the new initialization loop starts with all ports administratively isolated by default, CPU port forwarding and isolation masks are explicitly configured at the end of the setup phase to prevent egress traffic from being blocked.

---
 drivers/net/dsa/realtek/rtl8365mb.c | 152 +++++++++++++++++++++++-------------
 1 file changed, 96 insertions(+), 56 deletions(-)
drivers/net/dsa/realtek/rtl8365mb.c +96 -56
index 37e1d7654b1d..8460f36df4e5 100644
@@ -1554,18 +1554,15 @@ static void rtl8365mb_stats_setup(struct realtek_priv *priv)
 {
 	struct rtl8365mb *mb = priv->chip_data;
 	struct dsa_switch *ds = &priv->ds;
-	int i;
+	struct dsa_port *dp;
 
 	/* Per-chip global mutex to protect MIB counter access, since doing
 	 * so requires accessing a series of registers in a particular order.
 	 */
 	mutex_init(&mb->mib_lock);
 
-	for (i = 0; i < priv->num_ports; i++) {
-		struct rtl8365mb_port *p = &mb->ports[i];
-
-		if (dsa_is_unused_port(ds, i))
-			continue;
+	dsa_switch_for_each_available_port(dp, ds) {
+		struct rtl8365mb_port *p = &mb->ports[dp->index];
 
 		/* Per-port spinlock to protect the stats64 data */
 		spin_lock_init(&p->stats_lock);
@@ -1581,13 +1578,10 @@ static void rtl8365mb_stats_teardown(struct realtek_priv *priv)
 {
 	struct rtl8365mb *mb = priv->chip_data;
 	struct dsa_switch *ds = &priv->ds;
-	int i;
-
-	for (i = 0; i < priv->num_ports; i++) {
-		struct rtl8365mb_port *p = &mb->ports[i];
+	struct dsa_port *dp;
 
-		if (dsa_is_unused_port(ds, i))
-			continue;
+	dsa_switch_for_each_available_port(dp, ds) {
+		struct rtl8365mb_port *p = &mb->ports[dp->index];
 
 		cancel_delayed_work_sync(&p->mib_work);
 	}
@@ -1646,6 +1640,9 @@ static irqreturn_t rtl8365mb_irq(int irq, void *data)
 	for_each_set_bit(line, &line_changes, priv->num_ports) {
 		int child_irq = irq_find_mapping(priv->irqdomain, line);
 
+		if (!child_irq)
+			continue;
+
 		handle_nested_irq(child_irq);
 	}
 
@@ -1709,13 +1706,14 @@ static int rtl8365mb_irq_disable(struct realtek_priv *priv)
 static int rtl8365mb_irq_setup(struct realtek_priv *priv)
 {
 	struct rtl8365mb *mb = priv->chip_data;
+	struct dsa_switch *ds = &priv->ds;
 	struct device_node *intc;
+	struct dsa_port *dp;
 	u32 irq_trig;
 	int virq;
 	int irq;
 	u32 val;
 	int ret;
-	int i;
 
 	intc = of_get_child_by_name(priv->dev->of_node, "interrupt-controller");
 	if (!intc) {
@@ -1744,8 +1742,8 @@ static int rtl8365mb_irq_setup(struct realtek_priv *priv)
 		goto out_put_node;
 	}
 
-	for (i = 0; i < priv->num_ports; i++) {
-		virq = irq_create_mapping(priv->irqdomain, i);
+	dsa_switch_for_each_available_port(dp, ds) {
+		virq = irq_create_mapping(priv->irqdomain, dp->index);
 		if (!virq) {
 			dev_err(priv->dev,
 				"failed to create irq domain mapping\n");
@@ -1815,9 +1813,11 @@ static int rtl8365mb_irq_setup(struct realtek_priv *priv)
 	mb->irq = 0;
 
 out_remove_irqdomain:
-	for (i = 0; i < priv->num_ports; i++) {
-		virq = irq_find_mapping(priv->irqdomain, i);
-		irq_dispose_mapping(virq);
+	dsa_switch_for_each_port(dp, ds) {
+		virq = irq_find_mapping(priv->irqdomain, dp->index);
+
+		if (virq)
+			irq_dispose_mapping(virq);
 	}
 
 	irq_domain_remove(priv->irqdomain);
@@ -1832,8 +1832,9 @@ static int rtl8365mb_irq_setup(struct realtek_priv *priv)
 static void rtl8365mb_irq_teardown(struct realtek_priv *priv)
 {
 	struct rtl8365mb *mb = priv->chip_data;
+	struct dsa_switch *ds = &priv->ds;
+	struct dsa_port *dp;
 	int virq;
-	int i;
 
 	if (mb->irq) {
 		free_irq(mb->irq, priv);
@@ -1841,9 +1842,15 @@ static void rtl8365mb_irq_teardown(struct realtek_priv *priv)
 	}
 
 	if (priv->irqdomain) {
-		for (i = 0; i < priv->num_ports; i++) {
-			virq = irq_find_mapping(priv->irqdomain, i);
-			irq_dispose_mapping(virq);
+		/* Unused ports with a linked PHY still have an active IRQ
+		 * mapping that must be disposed of during teardown. Loop
+		 * through all ports.
+		 */
+		dsa_switch_for_each_port(dp, ds) {
+			virq = irq_find_mapping(priv->irqdomain, dp->index);
+
+			if (virq)
+				irq_dispose_mapping(virq);
 		}
 
 		irq_domain_remove(priv->irqdomain);
@@ -1961,10 +1968,11 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 {
 	struct realtek_priv *priv = ds->priv;
 	struct rtl8365mb_cpu *cpu;
-	struct dsa_port *cpu_dp;
+	u32 downports_mask = 0;
+	u32 upports_mask = 0;
 	struct rtl8365mb *mb;
+	struct dsa_port *dp;
 	int ret;
-	int i;
 
 	mb = priv->chip_data;
 	cpu = &mb->cpu;
@@ -1991,67 +1999,99 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 	else if (ret)
 		dev_info(priv->dev, "no interrupt support\n");
 
-	for (i = 0; i < priv->num_ports; i++) {
+	dsa_switch_for_each_port(dp, ds) {
 		/* Cascading (DSA links) is not supported yet.
 		 * Historically, the driver has always been broken
 		 * without a dedicated CPU port because CPU tagging
 		 * would be disabled, rendering the switch entirely
 		 * non-functional for DSA operations.
 		 */
-		if (dsa_is_dsa_port(ds, i)) {
+		if (dsa_port_is_dsa(dp)) {
 			dev_err(priv->dev, "Cascading (DSA link) not supported\n");
 			ret = -EOPNOTSUPP;
 			goto out_teardown_irq;
 		}
 	}
 
-	/* Configure CPU tagging */
-	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
-		cpu->mask |= BIT(cpu_dp->index);
+	/* Start with all ports blocked, including unused ports */
+	dsa_switch_for_each_port(dp, ds) {
+		struct rtl8365mb_port *p = &mb->ports[dp->index];
 
-		if (cpu->trap_port == RTL8365MB_MAX_NUM_PORTS)
-			cpu->trap_port = cpu_dp->index;
-	}
-	cpu->enable = cpu->mask > 0;
+		/* 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, dp->index, BR_STATE_DISABLED);
 
-	if (!cpu->enable) {
-		dev_err(priv->dev, "no CPU port defined\n");
-		ret = -EINVAL;
-		goto out_teardown_irq;
-	}
+		/* Start with all port completely isolated */
+		ret = rtl8365mb_port_set_isolation(priv, dp->index, 0);
+		if (ret)
+			goto out_teardown_irq;
 
-	ret = rtl8365mb_cpu_config(priv);
-	if (ret)
-		goto out_teardown_irq;
+		/* Disable learning */
+		ret = rtl8365mb_port_set_learning(priv, dp->index, false);
+		if (ret)
+			goto out_teardown_irq;
 
-	/* Configure ports */
-	for (i = 0; i < priv->num_ports; i++) {
-		struct rtl8365mb_port *p = &mb->ports[i];
+		/* Set up per-port private data */
+		p->priv = priv;
+		p->index = dp->index;
 
-		if (dsa_is_unused_port(ds, i))
+		/* Collect CPU ports. If we support cascade switches, it should
+		 * also include the upstream DSA ports.
+		 */
+		if (!dsa_port_is_cpu(dp))
+			continue;
+
+		upports_mask |= BIT(dp->index);
+	}
+
+	/* Configure user ports */
+	dsa_switch_for_each_port(dp, ds) {
+		if (!dsa_port_is_user(dp))
 			continue;
 
 		/* Forward only to the CPU */
-		ret = rtl8365mb_port_set_isolation(priv, i, cpu->mask);
+		ret = rtl8365mb_port_set_isolation(priv, dp->index,
+						   upports_mask);
 		if (ret)
 			goto out_teardown_irq;
 
-		/* Disable learning */
-		ret = rtl8365mb_port_set_learning(priv, i, false);
+		/* If we support cascade switches, it should also include the
+		 * downstream DSA ports.
+		 */
+		downports_mask |= BIT(dp->index);
+	}
+
+	/* Configure CPU tagging */
+	/* If we support cascade switches, it should also include the upstream
+	 * DSA ports.
+	 */
+	dsa_switch_for_each_cpu_port(dp, ds) {
+		/* Use the first CPU port as trap_port */
+		if (cpu->trap_port == RTL8365MB_MAX_NUM_PORTS)
+			cpu->trap_port = dp->index;
+
+		/* Forward to all user ports */
+		ret = rtl8365mb_port_set_isolation(priv, dp->index,
+						   downports_mask);
 		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);
+	cpu->mask = upports_mask;
+	cpu->enable = cpu->mask > 0;
 
-		/* Set up per-port private data */
-		p->priv = priv;
-		p->index = i;
+	if (!cpu->enable) {
+		dev_err(priv->dev, "no CPU port defined\n");
+		ret = -EINVAL;
+		goto out_teardown_irq;
 	}
 
+	ret = rtl8365mb_cpu_config(priv);
+	if (ret)
+		goto out_teardown_irq;
+
 	ret = rtl8365mb_port_change_mtu(ds, cpu->trap_port, ETH_DATA_LEN);
 	if (ret)
 		goto out_teardown_irq;
Signature · 1 line
2.54.0
author maintainer
#5 lore raw

PATCH v11 4/9 net: dsa: realtek: rtl8365mb: prepare for multiple source files

In reply to Luiz Angelo Daros de Luca

From: 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
index 17367bcba496..3f986e04912f 100644
@@ -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
similarity index 100%
rename from drivers/net/dsa/realtek/rtl8365mb.c
rename to drivers/net/dsa/realtek/rtl8365mb_main.c
Signature · 1 line
2.54.0
maintainer
#6 lore raw

Re: [net-next PATCH v11 4/9] net: dsa: realtek: rtl8365mb: prepare for multiple source files

Quoted 2 lines · On Sun, 31 May 2026 23:53:19 -0300 Luiz Angelo Daros de Luca wrote:

The trailing backslash in the Makefile is deliberate. It allows for new files to be added without clobbering git history.

FWIW maybe just my personal preference but I'd add a trailing comment for this, IOW:

rtl8365mb-objs := rtl8365mb_main.o \

# end of rtl8365mb-objs

This way it's less likely someone will add a line here without realizing it will become part of the previous statement.

author maintainer
#7 lore raw

PATCH v11 5/9 net: dsa: realtek: rtl8365mb: add table lookup interface

In reply to Luiz Angelo Daros de Luca

From: 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 | 214 ++++++++++++++++++++++++++++++
 drivers/net/dsa/realtek/rtl8365mb_table.h | 138 +++++++++++++++++++
 3 files changed, 353 insertions(+)
drivers/net/dsa/realtek/Makefile +1 -0
index 3f986e04912f..99654c4c5a3d 100644
@@ -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 +214 -0

214 lines not on this page · raw message

drivers/net/dsa/realtek/rtl8365mb_table.h new file +138 -0

138 lines not on this page · raw message

Signature · 1 line
2.54.0
author maintainer
#8 lore raw

PATCH v11 6/9 net: dsa: realtek: rtl8365mb: add VLAN support

In reply to 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. This driver will use VLANMC members field as way to track which ports are using that entry as PVID.

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.

The use of FIELD_PREP for reconstructing LO/HI values was suggested by Yury Norov.

Fix for vlan_setup and vlan_filtering was suggested by Abdulkader Alrezej.

---
 drivers/net/dsa/realtek/Makefile         |   1 +
 drivers/net/dsa/realtek/realtek.h        |   5 +
 drivers/net/dsa/realtek/rtl8365mb_main.c | 325 +++++++++++
 drivers/net/dsa/realtek/rtl8365mb_vlan.c | 955 +++++++++++++++++++++++++++++++
 drivers/net/dsa/realtek/rtl8365mb_vlan.h |  39 ++
 drivers/net/dsa/realtek/rtl83xx.c        |   1 +
 6 files changed, 1326 insertions(+)
drivers/net/dsa/realtek/Makefile +1 -0
index 99654c4c5a3d..b7fc4e852fd8 100644
@@ -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/realtek.h +5 -0
index c03485a80d93..b9c4cbdd72fb 100644
@@ -54,6 +54,11 @@ struct realtek_priv {
 	struct regmap		*map;
 	struct regmap		*map_nolock;
 	struct mutex		map_lock;
+	/* vlan_lock protects against concurrent Read-Modify-Write operations
+	 * on the global VLAN 4K and VLANMC tables, such as when adding or
+	 * deleting port VLAN memberships and PVID configurations.
+	 */
+	struct mutex		vlan_lock;
 	struct mii_bus		*user_mii_bus;
 	struct mii_bus		*bus;
 	int			mdio_addr;
drivers/net/dsa/realtek/rtl8365mb_main.c +325 -0

355 lines not on this page · raw message

drivers/net/dsa/realtek/rtl8365mb_vlan.c new file +955 -0

955 lines not on this page · raw message

drivers/net/dsa/realtek/rtl8365mb_vlan.h new file +39 -0
new file mode 100644
index 000000000000..27a526b61873
@@ -0,0 +1,39 @@
+/* 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"
+
+enum rtl8365mb_frame_ingress {
+	RTL8365MB_FRAME_TYPE_ANY_FRAME = 0,
+	RTL8365MB_FRAME_TYPE_TAGGED_ONLY,
+	RTL8365MB_FRAME_TYPE_UNTAGGED_ONLY,
+};
+
+int rtl8365mb_vlan_port_get_pvid(struct realtek_priv *priv, int port,
+				 u16 *pvid);
+int
+rtl8365mb_vlan_port_get_framefilter(struct realtek_priv *priv,
+				    int port,
+				    enum rtl8365mb_frame_ingress *frame_type);
+int
+rtl8365mb_vlan_port_set_framefilter(struct realtek_priv *priv,
+				    int port,
+				    enum rtl8365mb_frame_ingress frame_type);
+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_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);
+#endif /* _REALTEK_RTL8365MB_VLAN_H */
drivers/net/dsa/realtek/rtl83xx.c +1 -0
index 2b9bd4462714..93bc47dfe7f7 100644
@@ -155,6 +155,7 @@ rtl83xx_probe(struct device *dev,
 		return ERR_PTR(-ENOMEM);
 
 	mutex_init(&priv->map_lock);
+	mutex_init(&priv->vlan_lock);
 
 	rc.lock_arg = priv;
 	priv->map = devm_regmap_init(dev, NULL, priv, &rc);
Signature · 1 line
2.54.0
maintainer
#9 lore raw

Re: [net-next PATCH v11 6/9] net: dsa: realtek: rtl8365mb: add VLAN support

drivers/net/dsa/realtek/rtl8365mb_vlan.c line 286

Quoted 8 lines · On Sun, 31 May 2026 23:53:21 -0300 Luiz Angelo Daros de Luca wrote:
drivers/net/dsa/realtek/rtl8365mb_vlan.c new file +8 -0
+#define RTL_VLAN_ERR(msg)						\
+	do {								\
+		const char *__msg = (msg);				\
+									\
+		if (extack)						\
+			NL_SET_ERR_MSG_FMT_MOD(extack, "%s", __msg);	\
+		dev_err(priv->dev, "%s", __msg);			\
+	} while (0)

Please don't duplicate errors. The errors are either due to something being wrong / unsupported in the request in which case they should go via extack. Or they are unexpected system errors (IO errors, timeouts and such) in which case just return errno and print to the logs so that the admin has a permanent record.

There's a tracepoint in NL_SET_ERR_MSG() if you want to sniff user errors.

Signature · 1 line
pw-bot: cr
author maintainer
#10 lore raw

Re: [net-next PATCH v11 6/9] net: dsa: realtek: rtl8365mb: add VLAN support

Quoted 10 lines · On Sun, 31 May 2026 23:53:21 -0300 Luiz Angelo Daros de Luca wrote:

drivers/net/dsa/realtek/rtl8365mb_vlan.c line 286

Quoted 8 lines
drivers/net/dsa/realtek/rtl8365mb_vlan.c new file +8 -0
+#define RTL_VLAN_ERR(msg)                                            \
+     do {                                                            \
+             const char *__msg = (msg);                              \
+                                                                     \
+             if (extack)                                             \
+                     NL_SET_ERR_MSG_FMT_MOD(extack, "%s", __msg);    \
+             dev_err(priv->dev, "%s", __msg);                        \
+     } while (0)
Quoted 8 lines

Please don't duplicate errors. The errors are either due to something being wrong / unsupported in the request in which case they should go via extack. Or they are unexpected system errors (IO errors, timeouts and such) in which case just return errno and print to the logs so that the admin has a permanent record.

There's a tracepoint in NL_SET_ERR_MSG() if you want to sniff user errors.

Hi Jakub,

Apologies, I missed your review comment just before hitting send on v12. I will spin a v13 shortly to address this.

You are entirely right. The macro was originally a crude way to channel all errors into a single source for automated testing. However, separating user validation (extack) from actual hardware/system logs (dev_err) makes much more sense. I will remove the macro and split the error paths properly in the next revision.

Thanks,

Regards,

Luiz

author maintainer
#11 lore raw

PATCH v11 7/9 net: dsa: realtek: rtl8365mb: add port_bridge_{join,leave}

In reply to Luiz Angelo Daros de Luca

From: 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/realtek.h        |   7 ++
 drivers/net/dsa/realtek/rtl8365mb_main.c |  58 +++++++++++-
 drivers/net/dsa/realtek/rtl83xx.c        | 150 +++++++++++++++++++++++++++++++
 drivers/net/dsa/realtek/rtl83xx.h        |   7 ++
 4 files changed, 221 insertions(+), 1 deletion(-)
drivers/net/dsa/realtek/realtek.h +7 -0
index b9c4cbdd72fb..c271a15a9ae2 100644
@@ -112,6 +112,13 @@ struct realtek_ops {
 	int	(*enable_vlan)(struct realtek_priv *priv, bool enable);
 	int	(*enable_vlan4k)(struct realtek_priv *priv, bool enable);
 	int	(*enable_port)(struct realtek_priv *priv, int port, bool enable);
+	int	(*port_add_isolation)(struct realtek_priv *priv, int port,
+				      u32 mask);
+	int	(*port_remove_isolation)(struct realtek_priv *priv, int port,
+					 u32 mask);
+	int	(*port_set_efid)(struct realtek_priv *priv, int port, u32 efid);
+	int	(*port_set_learning)(struct realtek_priv *priv, int port,
+				     bool enable);
 	int	(*phy_read)(struct realtek_priv *priv, int phy, int regnum);
 	int	(*phy_write)(struct realtek_priv *priv, int phy, int regnum,
 			     u16 val);
drivers/net/dsa/realtek/rtl8365mb_main.c +57 -1

97 lines not on this page · raw message

drivers/net/dsa/realtek/rtl83xx.c +150 -0

156 lines not on this page · raw message

drivers/net/dsa/realtek/rtl83xx.h +7 -0
index c8a0ff8fd75e..2481a1aaa226 100644
@@ -21,4 +21,11 @@ void rtl83xx_remove(struct realtek_priv *priv);
 void rtl83xx_reset_assert(struct realtek_priv *priv);
 void rtl83xx_reset_deassert(struct realtek_priv *priv);
 
+int rtl83xx_port_bridge_join(struct dsa_switch *ds, int port,
+			     struct dsa_bridge bridge,
+			     bool *tx_forward_offload,
+			     struct netlink_ext_ack *extack);
+void rtl83xx_port_bridge_leave(struct dsa_switch *ds, int port,
+			       struct dsa_bridge bridge);
+
 #endif /* _RTL83XX_H */
Signature · 1 line
2.54.0
author maintainer
#12 lore raw

PATCH v11 8/9 net: dsa: realtek: rtl8365mb: add FDB support

In reply to Luiz Angelo Daros de Luca

From: Alvin Šipraga <[email protected]>

Implement support for FDB and MDB management for the RTL8365MB series switches.

The hardware supports IVL by keying the unicast forwarding database with the {MAC, VID, EFID} tuple. The Extended Filtering ID (EFID) is 3 bits wide, providing 8 unique filtering domains. This driver reserves EFID 0 for standalone ports, effectively limiting the hardware offload to a maximum of 7 bridges. The multicast database uses a {MAC, VID} key, with ports from different bridges sharing the same multicast group.

Introduce a mutex lock (l2_lock) to protect concurrent L2 table updates.

Add support for forwarding database operations, including unicast and multicast entry handling as well as fast aging support.

Set DSA switch flags assisted_learning_on_cpu_port and fdb_isolation.

---
 drivers/net/dsa/realtek/Makefile         |   1 +
 drivers/net/dsa/realtek/realtek.h        |  29 ++
 drivers/net/dsa/realtek/rtl8365mb_l2.c   | 566 +++++++++++++++++++++++++++++++
 drivers/net/dsa/realtek/rtl8365mb_l2.h   |  32 ++
 drivers/net/dsa/realtek/rtl8365mb_main.c |  25 +-
 drivers/net/dsa/realtek/rtl83xx.c        | 317 +++++++++++++++++
 drivers/net/dsa/realtek/rtl83xx.h        |  16 +
 7 files changed, 985 insertions(+), 1 deletion(-)
drivers/net/dsa/realtek/Makefile +1 -0
index b7fc4e852fd8..6c329e046d0b 100644
@@ -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/realtek.h +29 -0
index c271a15a9ae2..426c9d345e6e 100644
@@ -45,6 +45,12 @@ struct rtl8366_vlan_4k {
 	u8	fid;
 };
 
+struct realtek_fdb_entry {
+	u8 mac_addr[ETH_ALEN];
+	u16 vid;
+	bool is_static;
+};
+
 struct realtek_priv {
 	struct device		*dev;
 	struct reset_control    *reset_ctl;
@@ -59,6 +65,15 @@ struct realtek_priv {
 	 * deleting port VLAN memberships and PVID configurations.
 	 */
 	struct mutex		vlan_lock;
+	/* l2_lock is used to prevent concurrent modifications of L2 table
+	 * entries while another function is reading it. l2_(add,del)_mc
+	 * is an example that first read current table entry and then
+	 * create/update it. l2_(add|del)_uc uses a single table op and,
+	 * internally, it might not need this lock. However, altering FDB
+	 * may still collide, as well as l2_flush, with fdb_dump iterating
+	 * over FDB.
+	 */
+	struct mutex		l2_lock;
 	struct mii_bus		*user_mii_bus;
 	struct mii_bus		*bus;
 	int			mdio_addr;
@@ -119,6 +134,19 @@ struct realtek_ops {
 	int	(*port_set_efid)(struct realtek_priv *priv, int port, u32 efid);
 	int	(*port_set_learning)(struct realtek_priv *priv, int port,
 				     bool enable);
+	int	(*l2_add_uc)(struct realtek_priv *priv, int port,
+			     const unsigned char addr[ETH_ALEN],
+			     u16 efid, u16 vid);
+	int	(*l2_del_uc)(struct realtek_priv *priv, int port,
+			     const unsigned char addr[ETH_ALEN],
+			     u16 efid, u16 vid);
+	int	(*l2_get_next_uc)(struct realtek_priv *priv, u16 *addr,
+				  int port, struct realtek_fdb_entry *entry);
+	int	(*l2_add_mc)(struct realtek_priv *priv, int port,
+			     const unsigned char addr[ETH_ALEN], u16 vid);
+	int	(*l2_del_mc)(struct realtek_priv *priv, int port,
+			     const unsigned char addr[ETH_ALEN], u16 vid);
+	int	(*l2_flush)(struct realtek_priv *priv, int port, u16 vid);
 	int	(*phy_read)(struct realtek_priv *priv, int phy, int regnum);
 	int	(*phy_write)(struct realtek_priv *priv, int phy, int regnum,
 			     u16 val);
@@ -131,6 +159,7 @@ struct realtek_variant {
 	unsigned int clk_delay;
 	u8 cmd_read;
 	u8 cmd_write;
+	u16 l2_table_size;
 	size_t chip_data_sz;
 };
 
drivers/net/dsa/realtek/rtl8365mb_l2.c new file +566 -0

566 lines not on this page · raw message

drivers/net/dsa/realtek/rtl8365mb_l2.h new file +32 -0
new file mode 100644
index 000000000000..9470cf059ce5
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Forwarding and multicast database interface for the rtl8365mb switch family
+ *
+ * Copyright (C) 2022 Alvin Šipraga <[email protected]>
+ */
+
+#ifndef _REALTEK_RTL8365MB_L2_H
+#define _REALTEK_RTL8365MB_L2_H
+
+#include <linux/if_ether.h>
+#include <linux/types.h>
+
+#include "realtek.h"
+
+int rtl8365mb_l2_get_next_uc(struct realtek_priv *priv, u16 *addr, int port,
+			     struct realtek_fdb_entry *entry);
+int rtl8365mb_l2_add_uc(struct realtek_priv *priv, int port,
+			const unsigned char addr[static ETH_ALEN],
+			u16 efid, u16 vid);
+int rtl8365mb_l2_del_uc(struct realtek_priv *priv, int 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, int port,
+			const unsigned char mac_addr[static ETH_ALEN],
+			u16 vid);
+int rtl8365mb_l2_del_mc(struct realtek_priv *priv, int port,
+			const unsigned char mac_addr[static ETH_ALEN],
+			u16 vid);
+
+#endif /* _REALTEK_RTL8365MB_L2_H */
drivers/net/dsa/realtek/rtl8365mb_main.c +24 -1
index c170c7f23ca8..75317e47e32d 100644
@@ -104,6 +104,7 @@
 #include "realtek-smi.h"
 #include "realtek-mdio.h"
 #include "rtl83xx.h"
+#include "rtl8365mb_l2.h"
 #include "rtl8365mb_vlan.h"
 
 /* Family-specific data and limits */
@@ -111,8 +112,15 @@
 #define RTL8365MB_NUM_PHYREGS		32
 #define RTL8365MB_PHYREGMAX		(RTL8365MB_NUM_PHYREGS - 1)
 #define RTL8365MB_MAX_NUM_PORTS		11
-#define RTL8365MB_MAX_NUM_EXTINTS	3
+/* Valid for the whole family except RTL8370B, which has 4160 entries.
+ * RTL8370B is mentioned in vendor code but it might not even belong
+ * to the same RTL8367C family.
+ */
 #define RTL8365MB_LEARN_LIMIT_MAX	2112
+#define RTL8365MB_MAX_NUM_EXTINTS	3
+
+/* The LUT table size matches the maximum learning limit */
+#define RTL8365MB_L2_TABLE_SIZE		RTL8365MB_LEARN_LIMIT_MAX
 
 /* Chip identification registers */
 #define RTL8365MB_CHIP_ID_REG		0x1300
@@ -2459,6 +2467,8 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 	if (ret)
 		goto out_teardown_irq;
 
+	ds->assisted_learning_on_cpu_port = true;
+	ds->fdb_isolation = true;
 	/* The EFID is 3 bits, but EFID 0 is reserved for standalone ports */
 	ds->max_num_bridges = FIELD_MAX(RTL8365MB_EFID_MASK);
 	ds->configure_vlan_while_not_filtering = true;
@@ -2580,6 +2590,12 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
 	.port_bridge_join = rtl83xx_port_bridge_join,
 	.port_bridge_leave = rtl83xx_port_bridge_leave,
 	.port_stp_state_set = rtl8365mb_port_stp_state_set,
+	.port_fast_age = rtl83xx_port_fast_age,
+	.port_fdb_add = rtl83xx_port_fdb_add,
+	.port_fdb_del = rtl83xx_port_fdb_del,
+	.port_fdb_dump = rtl83xx_port_fdb_dump,
+	.port_mdb_add = rtl83xx_port_mdb_add,
+	.port_mdb_del = rtl83xx_port_mdb_del,
 	.port_vlan_add = rtl8365mb_port_vlan_add,
 	.port_vlan_del = rtl8365mb_port_vlan_del,
 	.port_vlan_filtering = rtl8365mb_port_vlan_filtering,
@@ -2602,6 +2618,12 @@ static const struct realtek_ops rtl8365mb_ops = {
 	.port_remove_isolation = rtl8365mb_port_remove_isolation,
 	.port_set_efid = rtl8365mb_port_set_efid,
 	.port_set_learning = rtl8365mb_port_set_learning,
+	.l2_add_uc = rtl8365mb_l2_add_uc,
+	.l2_del_uc = rtl8365mb_l2_del_uc,
+	.l2_get_next_uc = rtl8365mb_l2_get_next_uc,
+	.l2_add_mc = rtl8365mb_l2_add_mc,
+	.l2_del_mc = rtl8365mb_l2_del_mc,
+	.l2_flush = rtl8365mb_l2_flush,
 	.phy_read = rtl8365mb_phy_read,
 	.phy_write = rtl8365mb_phy_write,
 };
@@ -2613,6 +2635,7 @@ const struct realtek_variant rtl8365mb_variant = {
 	.clk_delay = 10,
 	.cmd_read = 0xb9,
 	.cmd_write = 0xb8,
+	.l2_table_size = RTL8365MB_L2_TABLE_SIZE,
 	.chip_data_sz = sizeof(struct rtl8365mb),
 };
 
drivers/net/dsa/realtek/rtl83xx.c +317 -0

335 lines not on this page · raw message

drivers/net/dsa/realtek/rtl83xx.h +16 -0
index 2481a1aaa226..dcb819fe567f 100644
@@ -28,4 +28,20 @@ int rtl83xx_port_bridge_join(struct dsa_switch *ds, int port,
 void rtl83xx_port_bridge_leave(struct dsa_switch *ds, int port,
 			       struct dsa_bridge bridge);
 
+void rtl83xx_port_fast_age(struct dsa_switch *ds, int port);
+int rtl83xx_port_fdb_add(struct dsa_switch *ds, int port,
+			 const unsigned char *addr, u16 vid,
+			 struct dsa_db db);
+int rtl83xx_port_fdb_del(struct dsa_switch *ds, int port,
+			 const unsigned char *addr, u16 vid,
+			 struct dsa_db db);
+int rtl83xx_port_fdb_dump(struct dsa_switch *ds, int port,
+			  dsa_fdb_dump_cb_t *cb, void *data);
+int rtl83xx_port_mdb_add(struct dsa_switch *ds, int port,
+			 const struct switchdev_obj_port_mdb *mdb,
+			 struct dsa_db db);
+int rtl83xx_port_mdb_del(struct dsa_switch *ds, int port,
+			 const struct switchdev_obj_port_mdb *mdb,
+			 struct dsa_db db);
+
 #endif /* _RTL83XX_H */
Signature · 1 line
2.54.0
author maintainer
#13 lore raw

PATCH v11 9/9 net: dsa: realtek: rtl8365mb: add bridge port flags

In reply to Luiz Angelo Daros de Luca

Implement support for bridge port flags to control learning and flooding behavior. This patch maps hardware functionalities to the following bridge flags:

- BR_LEARNING
- BR_FLOOD
- BR_MCAST_FLOOD
- BR_BCAST_FLOOD

By default, all flooding types are enabled during port setup to ensure standard bridge behavior.

---
 drivers/net/dsa/realtek/realtek.h        |   6 ++
 drivers/net/dsa/realtek/rtl8365mb_main.c |  68 +++++++++++++++++++++
 drivers/net/dsa/realtek/rtl83xx.c        | 101 +++++++++++++++++++++++++++++++
 drivers/net/dsa/realtek/rtl83xx.h        |   4 ++
 4 files changed, 179 insertions(+)
drivers/net/dsa/realtek/realtek.h +6 -0
index 426c9d345e6e..452e4e6dd157 100644
@@ -134,6 +134,12 @@ struct realtek_ops {
 	int	(*port_set_efid)(struct realtek_priv *priv, int port, u32 efid);
 	int	(*port_set_learning)(struct realtek_priv *priv, int port,
 				     bool enable);
+	int	(*port_set_ucast_flood)(struct realtek_priv *priv, int port,
+					bool enable);
+	int	(*port_set_mcast_flood)(struct realtek_priv *priv, int port,
+					bool enable);
+	int	(*port_set_bcast_flood)(struct realtek_priv *priv, int port,
+					bool enable);
 	int	(*l2_add_uc)(struct realtek_priv *priv, int port,
 			     const unsigned char addr[ETH_ALEN],
 			     u16 efid, u16 vid);
drivers/net/dsa/realtek/rtl8365mb_main.c +68 -0

98 lines not on this page · raw message

drivers/net/dsa/realtek/rtl83xx.c +101 -0

113 lines not on this page · raw message

drivers/net/dsa/realtek/rtl83xx.h +4 -0
index dcb819fe567f..d86447121276 100644
@@ -27,6 +27,10 @@ int rtl83xx_port_bridge_join(struct dsa_switch *ds, int port,
 			     struct netlink_ext_ack *extack);
 void rtl83xx_port_bridge_leave(struct dsa_switch *ds, int port,
 			       struct dsa_bridge bridge);
+int rtl83xx_port_bridge_flags(struct dsa_switch *ds, int port,
+			      struct switchdev_brport_flags flags,
+			      struct netlink_ext_ack *extack);
+int rtl83xx_setup_port_flood_control(struct realtek_priv *priv, int port);
 
 void rtl83xx_port_fast_age(struct dsa_switch *ds, int port);
 int rtl83xx_port_fdb_add(struct dsa_switch *ds, int port,
Signature · 1 line
2.54.0