Skip to contentSkip to conversation

net: dsa: yt921x: Add support for Motorcomm YT921x

PATCH Superseded David Yang 5 participants 12 messages Sep 13, 2025 – Sep 18, 2025

Subsystems (5)

v9 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

David Yang

author maintainer
#0 lore raw

v9 cover net: dsa: yt921x: Add support for Motorcomm YT921x

Motorcomm YT921x is a series of ethernet switches developed by Shanghai Motorcomm Electronic Technology, including:

- YT9215S / YT9215RB / YT9215SC: 5 GbE phys
- YT9213NB / YT9214NB: 2 GbE phys
- YT9218N / YT9218MB: 8 GbE phys

and up to 2 serdes interfaces.

This patch adds basic support for a working DSA switch.

v8: https://lore.kernel.org/r/[email protected]
- rework register polling
- rework mib reading
- other suggested code style changes
v7: https://lore.kernel.org/r/[email protected]
- simplify locking scheme
v6: https://lore.kernel.org/r/[email protected]
- handle unforwarded packets in tag driver
- move register and struct definitions to header file
- rework register abstraction and implement a driver lock
- implement *_stats and use a periodic work to fetch MIB
- remove EEPROM dump
- remove sysfs attr and other debug leftovers
- remove ds->user_mii_bus assignment
- run selftests and fix any errors found
v5: https://lore.kernel.org/r/[email protected]
- use enum for reg in dt binding
- fix phylink_mac_ops in the driver
- fix coding style
v4: https://lore.kernel.org/r/[email protected]
- remove switchid from dt binding
- remove hsr from tag driver
- use ratelimited log in tag driver
v3: https://lore.kernel.org/r/[email protected]
- fix words and warnings in dt binding
- remove unnecessary dev_warn_ratelimited and u64_from_u32
- remove lag and mst
- check for mdio results and fix a unlocked write in conduit_state_change
v2: https://lore.kernel.org/r/[email protected]
- fix words in dt binding
- add support for lag and mst
v1: https://lore.kernel.org/r/[email protected]
- fix coding style
- add dt binding
- add support for fdb, vlan and bridge

David Yang (3):
dt-bindings: net: dsa: yt921x: Add Motorcomm YT921x switch support
net: dsa: tag_yt921x: add support for Motorcomm YT921x tags
net: dsa: yt921x: Add support for Motorcomm YT921x

 .../bindings/net/dsa/motorcomm,yt921x.yaml    |  169 +
 drivers/net/dsa/Kconfig                       |    7 +
 drivers/net/dsa/Makefile                      |    1 +
 drivers/net/dsa/yt921x.c                      | 2950 +++++++++++++++++
 drivers/net/dsa/yt921x.h                      |  506 +++
 include/net/dsa.h                             |    2 +
 include/uapi/linux/if_ether.h                 |    1 +
 net/dsa/Kconfig                               |    6 +
 net/dsa/Makefile                              |    1 +
 net/dsa/tag_yt921x.c                          |  141 +
 10 files changed, 3784 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/dsa/motorcomm,yt921x.yaml
 create mode 100644 drivers/net/dsa/yt921x.c
 create mode 100644 drivers/net/dsa/yt921x.h
 create mode 100644 net/dsa/tag_yt921x.c
Signature · 1 line
2.51.0
author maintainer
#1 lore raw

PATCH v9 1/3 dt-bindings: net: dsa: yt921x: Add Motorcomm YT921x switch support

The Motorcomm YT921x series is a family of Ethernet switches with up to 8 internal GbE PHYs and up to 2 GMACs.

---
 .../bindings/net/dsa/motorcomm,yt921x.yaml    | 169 ++++++++++++++++++
 1 file changed, 169 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/dsa/motorcomm,yt921x.yaml
Documentation/devicetree/bindings/net/dsa/motorcomm,yt921x.yaml new file +169 -0
new file mode 100644
index 000000000000..7648aad073eb
@@ -0,0 +1,169 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/dsa/motorcomm,yt921x.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Motorcomm YT921x Ethernet switch family
+
+maintainers:
+  - David Yang <[email protected]>
+
+description: |
+  The Motorcomm YT921x series is a family of Ethernet switches with up to 8
+  internal GbE PHYs and up to 2 GMACs, including:
+
+    - YT9215S / YT9215RB / YT9215SC: 5 GbE PHYs (Port 0-4) + 2 GMACs (Port 8-9)
+    - YT9213NB: 2 GbE PHYs (Port 1/3) + 1 GMAC (Port 9)
+    - YT9214NB: 2 GbE PHYs (Port 1/3) + 2 GMACs (Port 8-9)
+    - YT9218N: 8 GbE PHYs (Port 0-7)
+    - YT9218MB: 8 GbE PHYs (Port 0-7) + 2 GMACs (Port 8-9)
+
+  Any port can be used as the CPU port.
+
+properties:
+  compatible:
+    const: motorcomm,yt9215
+
+  reg:
+    enum: [0x0, 0x1d]
+
+  reset-gpios:
+    maxItems: 1
+
+  mdio:
+    $ref: /schemas/net/mdio.yaml#
+    unevaluatedProperties: false
+    description:
+      Internal MDIO bus for the internal GbE PHYs. PHY 0-7 are used for Port
+      0-7 respectively.
+
+  mdio-external:
+    $ref: /schemas/net/mdio.yaml#
+    unevaluatedProperties: false
+    description:
+      External MDIO bus to access external components. External PHYs for GMACs
+      (Port 8-9) are expected to be connected to the external MDIO bus in
+      vendor's reference design, but that is not a hard limitation from the
+      chip.
+
+required:
+  - compatible
+  - reg
+
+allOf:
+  - $ref: dsa.yaml#/$defs/ethernet-ports
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    mdio {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        switch@1d {
+            compatible = "motorcomm,yt9215";
+            /* default 0x1d, alternate 0x0 */
+            reg = <0x1d>;
+            reset-gpios = <&tlmm 39 GPIO_ACTIVE_LOW>;
+
+            mdio {
+                #address-cells = <1>;
+                #size-cells = <0>;
+
+                sw_phy0: phy@0 {
+                    reg = <0x0>;
+                };
+
+                sw_phy1: phy@1 {
+                    reg = <0x1>;
+                };
+
+                sw_phy2: phy@2 {
+                    reg = <0x2>;
+                };
+
+                sw_phy3: phy@3 {
+                    reg = <0x3>;
+                };
+
+                sw_phy4: phy@4 {
+                    reg = <0x4>;
+                };
+            };
+
+            mdio-external {
+                #address-cells = <1>;
+                #size-cells = <0>;
+
+                phy1: phy@b {
+                    reg = <0xb>;
+                };
+            };
+
+            ethernet-ports {
+                #address-cells = <1>;
+                #size-cells = <0>;
+
+                ethernet-port@0 {
+                    reg = <0>;
+                    label = "lan1";
+                    phy-mode = "internal";
+                    phy-handle = <&sw_phy0>;
+                };
+
+                ethernet-port@1 {
+                    reg = <1>;
+                    label = "lan2";
+                    phy-mode = "internal";
+                    phy-handle = <&sw_phy1>;
+                };
+
+                ethernet-port@2 {
+                    reg = <2>;
+                    label = "lan3";
+                    phy-mode = "internal";
+                    phy-handle = <&sw_phy2>;
+                };
+
+                ethernet-port@3 {
+                    reg = <3>;
+                    label = "lan4";
+                    phy-mode = "internal";
+                    phy-handle = <&sw_phy3>;
+                };
+
+                ethernet-port@4 {
+                    reg = <4>;
+                    label = "lan5";
+                    phy-mode = "internal";
+                    phy-handle = <&sw_phy4>;
+                };
+
+                /* CPU port */
+                ethernet-port@8 {
+                    reg = <8>;
+                    phy-mode = "sgmii";
+                    ethernet = <&eth0>;
+
+                    fixed-link {
+                        speed = <1000>;
+                        full-duplex;
+                        pause;
+                        asym-pause;
+                    };
+                };
+
+                /* if external phy is connected to a MAC */
+                ethernet-port@9 {
+                    reg = <9>;
+                    label = "wan";
+                    phy-mode = "rgmii-id";
+                    phy-handle = <&phy1>;
+                };
+            };
+        };
+    };
Signature · 1 line
2.51.0
author maintainer
#2 lore raw

PATCH v9 2/3 net: dsa: tag_yt921x: add support for Motorcomm YT921x tags

In reply to David Yang

Add support for Motorcomm YT921x tags, which includes a proper configurable ethertype field (default to 0x9988).

---
 include/net/dsa.h             |   2 +
 include/uapi/linux/if_ether.h |   1 +
 net/dsa/Kconfig               |   6 ++
 net/dsa/Makefile              |   1 +
 net/dsa/tag_yt921x.c          | 141 ++++++++++++++++++++++++++++++++++
 5 files changed, 151 insertions(+)
 create mode 100644 net/dsa/tag_yt921x.c
include/net/dsa.h +2 -0
index d73ea0880066..67762fdaf3c7 100644
@@ -55,6 +55,7 @@ struct tc_action;
 #define DSA_TAG_PROTO_LAN937X_VALUE		27
 #define DSA_TAG_PROTO_VSC73XX_8021Q_VALUE	28
 #define DSA_TAG_PROTO_BRCM_LEGACY_FCS_VALUE	29
+#define DSA_TAG_PROTO_YT921X_VALUE		30
 
 enum dsa_tag_protocol {
 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
@@ -87,6 +88,7 @@ enum dsa_tag_protocol {
 	DSA_TAG_PROTO_RZN1_A5PSW	= DSA_TAG_PROTO_RZN1_A5PSW_VALUE,
 	DSA_TAG_PROTO_LAN937X		= DSA_TAG_PROTO_LAN937X_VALUE,
 	DSA_TAG_PROTO_VSC73XX_8021Q	= DSA_TAG_PROTO_VSC73XX_8021Q_VALUE,
+	DSA_TAG_PROTO_YT921X		= DSA_TAG_PROTO_YT921X_VALUE,
 };
 
 struct dsa_switch;
include/uapi/linux/if_ether.h +1 -0
index 69e0457eb200..cfd200c87e5e 100644
@@ -114,6 +114,7 @@
 #define ETH_P_QINQ1	0x9100		/* deprecated QinQ VLAN [ NOT AN OFFICIALLY REGISTERED ID ] */
 #define ETH_P_QINQ2	0x9200		/* deprecated QinQ VLAN [ NOT AN OFFICIALLY REGISTERED ID ] */
 #define ETH_P_QINQ3	0x9300		/* deprecated QinQ VLAN [ NOT AN OFFICIALLY REGISTERED ID ] */
+#define ETH_P_YT921X	0x9988		/* Motorcomm YT921x DSA [ NOT AN OFFICIALLY REGISTERED ID ] */
 #define ETH_P_EDSA	0xDADA		/* Ethertype DSA [ NOT AN OFFICIALLY REGISTERED ID ] */
 #define ETH_P_DSA_8021Q	0xDADB		/* Fake VLAN Header for DSA [ NOT AN OFFICIALLY REGISTERED ID ] */
 #define ETH_P_DSA_A5PSW	0xE001		/* A5PSW Tag Value [ NOT AN OFFICIALLY REGISTERED ID ] */
net/dsa/Kconfig +6 -0
index 869cbe57162f..6b94028b1fcc 100644
@@ -190,4 +190,10 @@ config NET_DSA_TAG_XRS700X
 	  Say Y or M if you want to enable support for tagging frames for
 	  Arrow SpeedChips XRS700x switches that use a single byte tag trailer.
 
+config NET_DSA_TAG_YT921X
+	tristate "Tag driver for Motorcomm YT921x switches"
+	help
+	  Say Y or M if you want to enable support for tagging frames for
+	  Motorcomm YT921x switches.
+
 endif
net/dsa/Makefile +1 -0
index 555c07cfeb71..4b011a1d5c87 100644
@@ -39,6 +39,7 @@ obj-$(CONFIG_NET_DSA_TAG_SJA1105) += tag_sja1105.o
 obj-$(CONFIG_NET_DSA_TAG_TRAILER) += tag_trailer.o
 obj-$(CONFIG_NET_DSA_TAG_VSC73XX_8021Q) += tag_vsc73xx_8021q.o
 obj-$(CONFIG_NET_DSA_TAG_XRS700X) += tag_xrs700x.o
+obj-$(CONFIG_NET_DSA_TAG_YT921X) += tag_yt921x.o
 
 # for tracing framework to find trace.h
 CFLAGS_trace.o := -I$(src)
net/dsa/tag_yt921x.c new file +141 -0

141 lines not on this page · raw message

Signature · 1 line
2.51.0
author maintainer
#3 lore raw

PATCH v9 3/3 net: dsa: yt921x: Add support for Motorcomm YT921x

In reply to David Yang

Motorcomm YT921x is a series of ethernet switches developed by Shanghai Motorcomm Electronic Technology, including:

- YT9215S / YT9215RB / YT9215SC: 5 GbE PHYs
- YT9213NB / YT9214NB: 2 GbE PHYs
- YT9218N / YT9218MB: 8 GbE PHYs

and up to 2 GMACs.

Driver verified on a stock wireless router with IPQ5018 + YT9215S.

---
 drivers/net/dsa/Kconfig  |    7 +
 drivers/net/dsa/Makefile |    1 +
 drivers/net/dsa/yt921x.c | 2950 ++++++++++++++++++++++++++++++++++++++
 drivers/net/dsa/yt921x.h |  506 +++++++
 4 files changed, 3464 insertions(+)
 create mode 100644 drivers/net/dsa/yt921x.c
 create mode 100644 drivers/net/dsa/yt921x.h
drivers/net/dsa/Kconfig +7 -0
index 4d9af691b989..7eb301fd987d 100644
@@ -154,4 +154,11 @@ config NET_DSA_VITESSE_VSC73XX_PLATFORM
 	  This enables support for the Vitesse VSC7385, VSC7388, VSC7395
 	  and VSC7398 SparX integrated ethernet switches, connected over
 	  a CPU-attached address bus and work in memory-mapped I/O mode.
+
+config NET_DSA_YT921X
+	tristate "Motorcomm YT9215 ethernet switch chip support"
+	select NET_DSA_TAG_YT921X
+	help
+	  This enables support for the Motorcomm YT9215 ethernet switch
+	  chip.
 endmenu
drivers/net/dsa/Makefile +1 -0
index c0a534fe6eaf..f5567747681e 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_NET_DSA_SMSC_LAN9303_MDIO) += lan9303_mdio.o
 obj-$(CONFIG_NET_DSA_VITESSE_VSC73XX) += vitesse-vsc73xx-core.o
 obj-$(CONFIG_NET_DSA_VITESSE_VSC73XX_PLATFORM) += vitesse-vsc73xx-platform.o
 obj-$(CONFIG_NET_DSA_VITESSE_VSC73XX_SPI) += vitesse-vsc73xx-spi.o
+obj-$(CONFIG_NET_DSA_YT921X) += yt921x.o
 obj-y				+= b53/
 obj-y				+= hirschmann/
 obj-y				+= lantiq/
drivers/net/dsa/yt921x.c new file +2950 -0

2,950 lines not on this page · raw message

drivers/net/dsa/yt921x.h new file +506 -0

32 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.51.0

Part of the diff is not shown here. View the raw message.

reviewer
#4 lore raw
Quoted 12 lines · David Yang

Motorcomm YT921x is a series of ethernet switches developed by Shanghai Motorcomm Electronic Technology, including:

- YT9215S / YT9215RB / YT9215SC: 5 GbE PHYs
- YT9213NB / YT9214NB: 2 GbE PHYs
- YT9218N / YT9218MB: 8 GbE PHYs

and up to 2 GMACs.

Driver verified on a stock wireless router with IPQ5018 + YT9215S.

...

Quoted 42 lines
drivers/net/dsa/yt921x.c new file +42 -0
+static int
+yt921x_port_config(struct yt921x_priv *priv, int port, unsigned int mode,
+		   phy_interface_t interface)
+{
+	struct device *dev = to_device(priv);
+	u32 mask;
+	u32 ctrl;
+	int res;
+
+	if (!yt921x_port_is_external(port)) {
+		if (interface != PHY_INTERFACE_MODE_INTERNAL) {
+			dev_err(dev, "Wrong mode %d on port %d\n",
+				interface, port);
+			return -EINVAL;
+		}
+		return 0;
+	}
+
+	switch (interface) {
+	/* SGMII */
+	case PHY_INTERFACE_MODE_SGMII:
+	case PHY_INTERFACE_MODE_100BASEX:
+	case PHY_INTERFACE_MODE_1000BASEX:
+	case PHY_INTERFACE_MODE_2500BASEX:
+		mask = YT921X_SGMII_CTRL_PORTn(port);
+		res = yt921x_reg_set_bits(priv, YT921X_SGMII_CTRL, mask);
+		if (res)
+			return res;
+
+		mask = YT921X_XMII_CTRL_PORTn(port);
+		res = yt921x_reg_clear_bits(priv, YT921X_XMII_CTRL, mask);
+		if (res)
+			return res;
+
+		mask = YT921X_SGMII_MODE_M;
+		switch (interface) {
+		case PHY_INTERFACE_MODE_SGMII:
+			ctrl = YT921X_SGMII_MODE_SGMII_PHY;
+			break;
+		case PHY_INTERFACE_MODE_100BASEX:
+			ctrl = YT921X_SGMII_MODE_100BASEX;
+			break;

drivers/net/dsa/yt921x.c line 2357

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+		case PHY_INTERFACE_MODE_1000BASEX:
+			ctrl = YT921X_SGMII_MODE_1000BASEX;
+			break;
+		case PHY_INTERFACE_MODE_2500BASEX:
+			ctrl = YT921X_SGMII_MODE_2500BASEX;
+			break;
+		default:
+			WARN_ON(1);
+			break;
+		}

Hi David,

If the default case is reached above then ctrl is used uninitialised below.

Flagged by Clang 21.2.1

Quoted 14 lines
drivers/net/dsa/yt921x.c new file +14 -0
+		res = yt921x_reg_update_bits(priv, YT921X_SGMIIn(port),
+					     mask, ctrl);
+		if (res)
+			return res;
+
+		break;
+	/* add XMII support here */
+	default:
+		WARN_ON(1);
+		break;
+	}
+
+	return 0;
+}

...

#5 lore raw
In reply to David Yang
Quoted 28 lines · David Yang
drivers/net/dsa/yt921x.c new file +28 -0
+static int
+yt921x_port_config(struct yt921x_priv *priv, int port, unsigned int mode,
+		   phy_interface_t interface)
+{
+	struct device *dev = to_device(priv);
+	u32 mask;
+	u32 ctrl;
+	int res;
+
+	if (!yt921x_port_is_external(port)) {
+		if (interface != PHY_INTERFACE_MODE_INTERNAL) {
+			dev_err(dev, "Wrong mode %d on port %d\n",
+				interface, port);
+			return -EINVAL;
+		}
+		return 0;
+	}
+
+	switch (interface) {
+	/* SGMII */
+	case PHY_INTERFACE_MODE_SGMII:
+	case PHY_INTERFACE_MODE_100BASEX:
+	case PHY_INTERFACE_MODE_1000BASEX:
+	case PHY_INTERFACE_MODE_2500BASEX:
+		mask = YT921X_SGMII_CTRL_PORTn(port);
+		res = yt921x_reg_set_bits(priv, YT921X_SGMII_CTRL, mask);
+		if (res)
+			return res;

drivers/net/dsa/yt921x.c line 2343

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+
+		mask = YT921X_XMII_CTRL_PORTn(port);
+		res = yt921x_reg_clear_bits(priv, YT921X_XMII_CTRL, mask);
+		if (res)
+			return res;
+
+		mask = YT921X_SGMII_MODE_M;
+		switch (interface) {
+		case PHY_INTERFACE_MODE_SGMII:
+			ctrl = YT921X_SGMII_MODE_SGMII_PHY;

Does this mean that "SGMII" here means you are sending SGMII speed/duplex to a host MAC (in other words, immitating a PHY) ?

If yes, then I think we want PHY_INTERFACE_MODE_REVSGMII added.

drivers/net/dsa/yt921x.c line 2382

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+static void
+yt921x_phylink_mac_link_down(struct phylink_config *config, unsigned int mode,
+			     phy_interface_t interface)
+{
+	struct dsa_port *dp = dsa_phylink_to_port(config);
+	struct dsa_switch *ds = dp->ds;
+	struct yt921x_priv *priv = to_yt921x_priv(ds);
+	struct device *dev = to_device(priv);
+	int port = dp->index;
+	int res;

Reverse christmas tree ordering please, and do we need "struct device *dev..." because ds->dev should be this same device. Maybe:

	struct dsa_port *dp = dsa_phylink_to_port(config);
	struct yt921x_priv *priv = to_yt921x_priv(dp->ds);
	int port = dp->index;
	int res;

drivers/net/dsa/yt921x.c line 2392

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+
+	cancel_delayed_work(&priv->ports[port].mib_read);
+
+	mutex_lock(&priv->reg_lock);
+	res = yt921x_port_down(priv, port);
+	mutex_unlock(&priv->reg_lock);
+
+	if (res)
+		dev_err(dev, "Failed to %s port %d: %i\n", "bring down",
+			port, res);

Then:

		dev_err(dp->ds->dev, ...

Same comment for the other phylink methods.

Thanks.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
maintainer
#6 lore raw
In reply to David Yang
Quoted 21 lines
drivers/net/dsa/yt921x.c new file +21 -0
+static int
+yt921x_port_up(struct yt921x_priv *priv, int port, unsigned int mode,
+	       phy_interface_t interface, int speed, int duplex,
+	       bool tx_pause, bool rx_pause)
+{
+	struct device *dev = to_device(priv);
+	u32 mask;
+	u32 ctrl;
+	int res;
+
+	switch (speed) {
+	case SPEED_10:
+		ctrl = YT921X_PORT_SPEED_10;
+		break;
+	case SPEED_100:
+		ctrl = YT921X_PORT_SPEED_100;
+		break;
+	case SPEED_1000:
+		ctrl = YT921X_PORT_SPEED_1000;
+		break;
+	case SPEED_10000:

drivers/net/dsa/yt921x.c line 2219

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+		ctrl = YT921X_PORT_SPEED_10000;
+		break;
+	case SPEED_2500:
+		ctrl = YT921X_PORT_SPEED_2500;
+		break;
+	default:
+		dev_err(dev, "Unsupported speed %d\n", speed);
+		/* compile complains about uninitialized variable */
+		ctrl = 0;
+		break;

If this should not happen, it is better to return -EINVAL.

I would also suggest sorting these numerically. 10G after 2.5G.

Quoted 23 lines
drivers/net/dsa/yt921x.c new file +23 -0
+	}
+	if (duplex == DUPLEX_FULL)
+		ctrl |= YT921X_PORT_DUPLEX_FULL;
+	if (tx_pause)
+		ctrl |= YT921X_PORT_TX_PAUSE;
+	if (rx_pause)
+		ctrl |= YT921X_PORT_RX_PAUSE;
+	ctrl |= YT921X_PORT_RX_MAC_EN | YT921X_PORT_TX_MAC_EN;
+	res = yt921x_reg_write(priv, YT921X_PORTn_CTRL(port), ctrl);
+	if (res)
+		return res;
+
+	if (yt921x_port_is_external(port)) {
+		mask = YT921X_SGMII_SPEED_M;
+		switch (speed) {
+		case SPEED_10:
+			ctrl = YT921X_SGMII_SPEED_10;
+			break;
+		case SPEED_100:
+			ctrl = YT921X_SGMII_SPEED_100;
+			break;
+		case SPEED_1000:
+			ctrl = YT921X_SGMII_SPEED_1000;

drivers/net/dsa/yt921x.c line 2252

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+			break;
+		case SPEED_10000:
+			ctrl = YT921X_SGMII_SPEED_10000;
+			break;
+		case SPEED_2500:
+			ctrl = YT921X_SGMII_SPEED_2500;
+			break;
+		default:
+			ctrl = 0;
+			break;

Same here.

Quoted 31 lines
drivers/net/dsa/yt921x.c new file +31 -0
+		}
+		mask |= YT921X_SGMII_DUPLEX_FULL;
+		if (duplex == DUPLEX_FULL)
+			ctrl |= YT921X_SGMII_DUPLEX_FULL;
+		mask |= YT921X_SGMII_TX_PAUSE;
+		if (tx_pause)
+			ctrl |= YT921X_SGMII_TX_PAUSE;
+		mask |= YT921X_SGMII_RX_PAUSE;
+		if (rx_pause)
+			ctrl |= YT921X_SGMII_RX_PAUSE;
+		mask |= YT921X_SGMII_LINK;
+		ctrl |= YT921X_SGMII_LINK;
+		res = yt921x_reg_update_bits(priv, YT921X_SGMIIn(port),
+					     mask, ctrl);
+		if (res)
+			return res;
+
+		mask = YT921X_XMII_LINK;
+		res = yt921x_reg_set_bits(priv, YT921X_XMIIn(port), mask);
+		if (res)
+			return res;
+
+		switch (speed) {
+		case SPEED_10:
+			ctrl = YT921X_MDIO_POLLING_SPEED_10;
+			break;
+		case SPEED_100:
+			ctrl = YT921X_MDIO_POLLING_SPEED_100;
+			break;
+		case SPEED_1000:
+			ctrl = YT921X_MDIO_POLLING_SPEED_1000;

drivers/net/dsa/yt921x.c line 2293

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+			break;
+		case SPEED_10000:
+			ctrl = YT921X_MDIO_POLLING_SPEED_10000;
+			break;
+		case SPEED_2500:
+			ctrl = YT921X_MDIO_POLLING_SPEED_2500;
+			break;
+		default:
+			ctrl = 0;
+			break;

and again.

Quoted 14 lines
drivers/net/dsa/yt921x.c new file +14 -0
+static int
+yt921x_port_config(struct yt921x_priv *priv, int port, unsigned int mode,
+		   phy_interface_t interface)
+{
+	struct device *dev = to_device(priv);
+	u32 mask;
+	u32 ctrl;
+	int res;
+
+	if (!yt921x_port_is_external(port)) {
+		if (interface != PHY_INTERFACE_MODE_INTERNAL) {
+			dev_err(dev, "Wrong mode %d on port %d\n",
+				interface, port);
+			return -EINVAL;

drivers/net/dsa/yt921x.c line 2329

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+		}
+		return 0;
+	}
+
+	switch (interface) {
+	/* SGMII */
+	case PHY_INTERFACE_MODE_SGMII:
+	case PHY_INTERFACE_MODE_100BASEX:
+	case PHY_INTERFACE_MODE_1000BASEX:
+	case PHY_INTERFACE_MODE_2500BASEX:

This comment is wrong. Only the first is SGMII.

drivers/net/dsa/yt921x.c line 2384

Quoted 12 lines
drivers/net/dsa/yt921x.c new file +12 -0
+static void
+yt921x_phylink_mac_link_down(struct phylink_config *config, unsigned int mode,
+			     phy_interface_t interface)
+{
+	struct dsa_port *dp = dsa_phylink_to_port(config);
+	struct dsa_switch *ds = dp->ds;
+	struct yt921x_priv *priv = to_yt921x_priv(ds);
+	struct device *dev = to_device(priv);
+	int port = dp->index;
+	int res;
+
+	cancel_delayed_work(&priv->ports[port].mib_read);

Should that be cancel_delayed_work_sync() ? Does it matter if the work is running on a different CPU?

Quoted 25 lines
drivers/net/dsa/yt921x.c new file +25 -0
+static void
+yt921x_dsa_phylink_get_caps(struct dsa_switch *ds, int port,
+			    struct phylink_config *config)
+{
+	struct yt921x_priv *priv = to_yt921x_priv(ds);
+	const struct yt921x_info *info = priv->info;
+
+	config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
+				   MAC_10 | MAC_100 | MAC_1000;
+
+	if ((info->internal_mask & BIT(port)) != 0) {
+		/* Port 10 for MCU should probably go here too. But since that
+		 * is untested yet, turn it down for the moment by letting it
+		 * fall to the default branch.
+		 */
+		__set_bit(PHY_INTERFACE_MODE_INTERNAL,
+			  config->supported_interfaces);
+	} else if ((info->external_mask & BIT(port)) != 0) {
+		/* TODO: external ports may support SGMII only, XMII only, or
+		 * SGMII + XMII depending on the chip. However, we can't get
+		 * the accurate config table due to lack of document, thus
+		 * we simply declare SGMII + XMII and rely on the correctness
+		 * of devicetree for now.
+		 */
+

drivers/net/dsa/yt921x.c line 2474

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+		/* SGMII */
+		__set_bit(PHY_INTERFACE_MODE_SGMII,
+			  config->supported_interfaces);
+		__set_bit(PHY_INTERFACE_MODE_100BASEX,
+			  config->supported_interfaces);
+		__set_bit(PHY_INTERFACE_MODE_1000BASEX,
+			  config->supported_interfaces);
+		__set_bit(PHY_INTERFACE_MODE_2500BASEX,
+			  config->supported_interfaces);
+		config->mac_capabilities |= MAC_2500FD;

And again

Quoted 39 lines
drivers/net/dsa/yt921x.c new file +39 -0
+static int yt921x_chip_detect(struct yt921x_priv *priv)
+{
+	struct device *dev = to_device(priv);
+	const struct yt921x_info *info;
+	u8 extmode;
+	u32 chipid;
+	u32 major;
+	u32 mode;
+	int res;
+
+	res = yt921x_reg_read(priv, YT921X_CHIP_ID, &chipid);
+	if (res)
+		return res;
+
+	major = FIELD_GET(YT921X_CHIP_ID_MAJOR, chipid);
+
+	for (info = yt921x_infos; info->name; info++)
+		if (info->major == major)
+			goto found_major;
+
+	dev_err(dev, "Unexpected chipid 0x%x\n", chipid);
+	return -ENODEV;
+
+found_major:
+	res = yt921x_reg_read(priv, YT921X_CHIP_MODE, &mode);
+	if (res)
+		return res;
+	res = yt921x_edata_read(priv, YT921X_EDATA_EXTMODE, &extmode);
+	if (res)
+		return res;
+
+	for (; info->name; info++)
+		if (info->major == major && info->mode == mode &&
+		    info->extmode == extmode)
+			goto found_chip;
+
+	dev_err(dev, "Unsupported chipid 0x%x with chipmode 0x%x 0x%x\n",
+		chipid, mode, extmode);
+	return -ENODEV;

drivers/net/dsa/yt921x.c line 2619

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+
+found_chip:
+	/* Print chipid here since we are interested in lower 16 bits */
+	dev_info(dev,
+		 "Motorcomm %s ethernet switch, chipid: 0x%x, "
+		 "chipmode: 0x%x 0x%x\n",
+		 info->name, chipid, mode, extmode);
+
+	priv->info = info;
+	return 0;

The use of gotos here is backwards to normal. They are pretty much only used in Linux to jump to the end to do cleanup on error. I don't know of any other driver which uses goto on success. Please change this.

drivers/net/dsa/yt921x.c line 2771

Quoted 3 lines
drivers/net/dsa/yt921x.c new file +3 -0
+	/* Register the internal mdio bus. Nodes for internal ports should have
+	 * proper phy-handle pointing to their PHYs. Not enabling the internal
+	 * bus is possible, though pretty wired, if internal ports are not used.

weird, not wired.

Andrew

---
pw-bot: cr

maintainer
#7 lore raw
In reply to David Yang
Quoted 49 lines · David Yang
drivers/net/dsa/yt921x.c new file +49 -0
+static int yt921x_reg_read(struct yt921x_priv *priv, u32 reg, u32 *valp)
+{
+	WARN_ON(!mutex_is_locked(&priv->reg_lock));
+
+	return priv->reg_ops->read(priv->reg_ctx, reg, valp);
+}
+
+static int yt921x_reg_write(struct yt921x_priv *priv, u32 reg, u32 val)
+{
+	WARN_ON(!mutex_is_locked(&priv->reg_lock));
+
+	return priv->reg_ops->write(priv->reg_ctx, reg, val);
+}
+
+static int
+yt921x_reg_wait(struct yt921x_priv *priv, u32 reg, u32 mask, u32 *valp)
+{
+	u32 val;
+	int res;
+
+	res = read_poll_timeout(yt921x_reg_read, res,
+				res || (val & mask) == *valp,
+				YT921X_POLL_SLEEP_US, YT921X_POLL_TIMEOUT_US,
+				false, priv, reg, &val);
+	if (res)
+		return res;
+
+	*valp = val;
+	return 0;
+}
+
+static void yt921x_reg_mdio_verify(u32 reg, u16 val, bool lo)
+{
+	const char *desc;
+
+	switch (val) {
+	case 0xfade:
+		desc = "which is likely from a non-existent register";
+		break;
+	case 0xdead:
+		desc = "which is likely a data race condition";
+		break;
+	default:
+		return;
+	}
+
+	/* Skip registers which are likely to have any valid values */
+	switch (reg) {
+	case YT921X_MAC_ADDR_HI2:

drivers/net/dsa/yt921x.c line 242

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	case YT921X_MAC_ADDR_LO4:
+	case YT921X_FDB_OUT0:
+	case YT921X_FDB_OUT1:
+		return;
+	}
+
+	pr_warn("%s: Read 0x%x at 0x%x %s32, %s; "
+		"consider reporting a bug if this happens again\n",
+		__func__, val, reg, lo ? "lo" : "hi", desc);
+}

Do you believe that having this mdio_verify() function here will help anyone? Per your previous response to Andrew, these seem some personal deductions during debugging, with no clear reasoning as to why these values would be indicative of these causes.
https://lore.kernel.org/netdev/CAAXyoMPLRHfSUGboC4SO+gBD0TdHq19fNs7AK3W2ZQnHT48gyA@mail.gmail.com/

What about YT921X_FDB_IN0? It shouldn't be so hard for 0xfade or 0xdead to appear in the upper or lower 16 bits of this register.

Rather than trying to find more examples where searching for bogus values leads to false positives, I'm trying to convince you to drop the function :)

drivers/net/dsa/yt921x.c line 349

Quoted 13 lines
drivers/net/dsa/yt921x.c new file +13 -0
+
+static int yt921x_reg_mdio_read(void *context, u32 reg, u32 *valp)
+{
+	struct yt921x_reg_mdio *mdio = context;
+	struct mii_bus *bus = mdio->bus;
+	int addr = mdio->addr;
+	u32 reg_addr;
+	u32 reg_data;
+	u32 val;
+	int res;
+
+	/* Hold the mdio bus lock to avoid (un)locking for 4 times */
+	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);

Andrew, are you satisfied with this lock? Perhaps I missed some part of the conversation, but didn't you say "leave the mdio lock alone"?
https://lore.kernel.org/netdev/[email protected]/

We also have priv->reg_lock here, not sure what is supposed to cover what.

Also, I didn't get the chance to intervene in this discussion:
https://lore.kernel.org/netdev/[email protected]/

David, I hope you won't get lost in apparently conflicting feedback, so I'll just try to lay out some basic facts and it's up to you to choose something that is consistent and makes sense for this driver. Sorry for the long text, but this submission seems confused in terms of locking.

Generally you need a lock when:
- you make an operation which alters the global state (like reset) in such a way that it would impact concurrent execution threads trying to do something.
But: if reset isn't concurrent with anything else going on, locking is pointless.
- you operate on a set of registers that must be accessed in an atomic sequence (SMI read/write, indirect access to a table, multi-word access)
- you operate on the same register or larger resource from multiple places
- you traverse a list at the same time as adding or removing an element AND
there is no higher level lock to reliably serialize the different calling threads.

The last part is important, because most DSA and phylink methods are called with rtnl_lock() held - a mutex which is not going away any time soon. I think only port_fdb_add() and port_fdb_del() are called unlocked, and need driver author analysis in terms of concurrency.

It should be said that many consider mutex_init() followed in quick succession by mutex_lock(), with nothing in between that would result in concurrency (spawn a new thread, request an IRQ, register an object to make it visible to user API, etc), to be an anti-pattern.
If you're the only one who can acquire the lock, you don't need it.

For example: you acquire priv->reg_lock during ds->ops->setup(), when there is no concurrency. You are encouraged to look through net/dsa/ and follow the code paths and locks that are used (and sprinkle a few ASSERT_RTNL() calls for instrumenting the various dsa_switch_ops methods).

Or another case, I would be surprised if the internal PHYs shared any state with the port forwarding isolation registers. So in principle, I don't see why yt921x_mbus_int_read() and yt921x_bridge_join() would serialize on the priv->reg_lock.

You do lock the bus->mdio_lock for the MDIO reg_ops, and a lock at that level (although not the MDIO bus lock) seems perfectly adequate and justifiable. That should be sufficient.

I think Andrew, when recommending the mv88e6xxx approach, was suggesting to _replace_ the reg_ops lock with the top-level priv->reg_lock. Not to have both.

Quoted 30 lines
drivers/net/dsa/yt921x.c new file +30 -0
+
+	reg_addr = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_ADDR |
+		   YT921X_SMI_READ;
+	res = __mdiobus_write(bus, addr, reg_addr, (u16)(reg >> 16));
+	if (res)
+		goto end;
+	res = __mdiobus_write(bus, addr, reg_addr, (u16)reg);
+	if (res)
+		goto end;
+
+	reg_data = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_DATA |
+		   YT921X_SMI_READ;
+	res = __mdiobus_read(bus, addr, reg_data);
+	if (res < 0)
+		goto end;
+	yt921x_reg_mdio_verify(reg, res, false);
+	val = (u16)res;
+	res = __mdiobus_read(bus, addr, reg_data);
+	if (res < 0)
+		goto end;
+	yt921x_reg_mdio_verify(reg, res, true);
+	val = (val << 16) | (u16)res;
+
+	*valp = val;
+	res = 0;
+
+end:
+	mutex_unlock(&bus->mdio_lock);
+	return res;
+}

drivers/net/dsa/yt921x.c

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+
+/* Read and handle overflow of 32bit MIBs. MIB buffer must be zeroed at start.
+ *
+ * Do not hold yt921x_priv::reg_lock or yt921x_port::stats_lock.
+ */
+static int yt921x_read_mib(struct yt921x_priv *priv, int port)
+{
+	struct yt921x_port *pp = &priv->ports[port];
+	struct device *dev = to_device(priv);
+	struct yt921x_mib buf;

312 bytes for a variable on the kernel stack seems a bit excessive, especially from the yt921x_dsa_get_ethtool_stats() call path. You might get -Wframe-larger-than warnings on certain architectures.

You can allocate a bounce buffer somewhere in the port or switch structure.

drivers/net/dsa/yt921x.c line 712

Quoted 12 lines
drivers/net/dsa/yt921x.c new file +12 -0
+	u64 rx_frames;
+	u64 tx_frames;
+	int res = 0;
+
+	/* Don't read registers while holding stats_lock.
+	 *
+	 * Also, read out old values first to avoid false positives on overflow
+	 * detection.
+	 */
+	spin_lock(&pp->stats_lock);
+	buf = pp->mib;
+	spin_unlock(&pp->stats_lock);

What is the point of copying pp->mib here? Doesn't the loop below overwrite each buf element?

Quoted 89 lines
drivers/net/dsa/yt921x.c new file +89 -0
+
+	mutex_lock(&priv->reg_lock);
+	for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
+		const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
+		u32 reg = YT921X_MIBn_DATA0(port) + desc->offset;
+		u64 *valp = &((u64 *)&buf)[i];
+		u32 val;
+
+		res = yt921x_reg_read(priv, reg, &val);
+		if (res)
+			break;
+
+		if (desc->size <= 1) {
+			if (val < (u32)*valp)
+				/* overflow */
+				*valp += (u64)U32_MAX + 1;
+			*valp &= ~U32_MAX;
+			*valp |= val;
+		} else {
+			u32 val1;
+
+			res = yt921x_reg_read(priv, reg + 4, &val1);
+			if (res)
+				break;
+
+			*valp = ((u64)val << 32) | val1;
+		}
+	}
+	mutex_unlock(&priv->reg_lock);
+
+	if (res) {
+		dev_err(dev, "Failed to %s port %d: %i\n", "read stats for",
+			port, res);
+		return res;
+	}
+
+	rx_frames = buf.rx_64byte + buf.rx_65_127byte +
+		    buf.rx_128_255byte + buf.rx_256_511byte +
+		    buf.rx_512_1023byte + buf.rx_1024_1518byte + buf.rx_jumbo;
+	tx_frames = buf.tx_64byte + buf.tx_65_127byte +
+		    buf.tx_128_255byte + buf.tx_256_511byte +
+		    buf.tx_512_1023byte + buf.tx_1024_1518byte + buf.tx_jumbo;
+
+	spin_lock(&pp->stats_lock);
+	pp->mib = buf;
+	pp->rx_frames = rx_frames;
+	pp->tx_frames = tx_frames;
+	spin_unlock(&pp->stats_lock);
+
+	return 0;
+}
+
+static const struct ethtool_rmon_hist_range yt921x_rmon_ranges[] = {
+	{ 0, 64 },
+	{ 65, 127 },
+	{ 128, 255 },
+	{ 256, 511 },
+	{ 512, 1023 },
+	{ 1024, 1518 },
+	{ 1519, YT921X_FRAME_SIZE_MAX },
+	{}
+};
+
+static void
+yt921x_dsa_get_rmon_stats(struct dsa_switch *ds, int port,
+			  struct ethtool_rmon_stats *rmon_stats,
+			  const struct ethtool_rmon_hist_range **ranges)
+{
+	struct yt921x_priv *priv = to_yt921x_priv(ds);
+	struct yt921x_port *pp = &priv->ports[port];
+	struct yt921x_mib *mib = &pp->mib;
+
+	*ranges = yt921x_rmon_ranges;
+
+	spin_lock(&pp->stats_lock);
+
+	rmon_stats->undersize_pkts = mib->rx_undersize_errors;
+	rmon_stats->oversize_pkts = mib->rx_oversize_errors;
+	rmon_stats->fragments = mib->rx_alignment_errors;
+	/* rmon_stats->jabbers */
+
+	rmon_stats->hist[0] = mib->rx_64byte;
+	rmon_stats->hist[1] = mib->rx_65_127byte;
+	rmon_stats->hist[2] = mib->rx_128_255byte;
+	rmon_stats->hist[3] = mib->rx_256_511byte;
+	rmon_stats->hist[4] = mib->rx_512_1023byte;
+	rmon_stats->hist[5] = mib->rx_1024_1518byte;
+	rmon_stats->hist[6] = mib->rx_jumbo;
+

drivers/net/dsa/yt921x.c line 822

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	rmon_stats->hist_tx[0] = mib->tx_64byte;
+	rmon_stats->hist_tx[1] = mib->tx_65_127byte;
+	rmon_stats->hist_tx[2] = mib->tx_128_255byte;
+	rmon_stats->hist_tx[3] = mib->tx_256_511byte;
+	rmon_stats->hist_tx[4] = mib->tx_512_1023byte;
+	rmon_stats->hist_tx[5] = mib->tx_1024_1518byte;
+	rmon_stats->hist_tx[6] = mib->tx_jumbo;
+
+	spin_unlock(&pp->stats_lock);
+}

The stats methods with non-atomic contexts can sleep, so you can give them updated counters at the time of call rather than up to 3 seconds old.

You can look at ocelot_port_stats_run() - and I'm sure a lot other implementations too - for some ideas.

drivers/net/dsa/yt921x.c line 1041

Quoted 4 lines
drivers/net/dsa/yt921x.c new file +4 -0
+static bool yt921x_dsa_support_eee(struct dsa_switch *ds, int port)
+{
+	return true;
+}

Replace with dsa_supports_eee().

drivers/net/dsa/yt921x.c line 1659

Quoted 12 lines
drivers/net/dsa/yt921x.c new file +12 -0
+static int
+yt921x_vlan_filtering(struct yt921x_priv *priv, int port, bool vlan_filtering)
+{
+	struct dsa_switch *ds = &priv->ds;
+	struct dsa_port *dp = dsa_to_port(ds, port);
+	u16 pvid;
+	u32 mask;
+	u32 ctrl;
+	int res;
+
+	mask = YT921X_PORT_VLAN_CTRL_CVID_M;
+	if (!vlan_filtering || !dp->bridge) {

dsa_port_bridge_dev_get()... everywhere

Quoted 6 lines
drivers/net/dsa/yt921x.c new file +6 -0
+		ctrl = YT921X_PORT_VLAN_CTRL_CVID(YT921X_VID_UNWARE);
+	} else {
+		br_vlan_get_pvid(dp->bridge->dev, &pvid);
+		ctrl = YT921X_PORT_VLAN_CTRL_CVID(pvid);
+	}
+	res = yt921x_reg_update_bits(priv, YT921X_PORTn_VLAN_CTRL(port),

drivers/net/dsa/yt921x.c line 1675

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+				     mask, ctrl);
+	if (res)
+		return res;
+
+	mask = YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_TAGGED |
+	       YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
+	ctrl = 0;
+	if (vlan_filtering) {
+		/* Do not drop tagged frames here; let VLAN_IGR_FILTER do it */
+		if (!pvid)

if (vlan_filtering && !pvid)

Quoted 31 lines
drivers/net/dsa/yt921x.c new file +31 -0
+			ctrl |= YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
+	}
+	res = yt921x_reg_update_bits(priv, YT921X_PORTn_VLAN_CTRL1(port),
+				     mask, ctrl);
+	if (res)
+		return res;
+
+	res = yt921x_reg_toggle_bits(priv, YT921X_VLAN_IGR_FILTER,
+				     YT921X_VLAN_IGR_FILTER_PORTn(port),
+				     vlan_filtering);
+	if (res)
+		return res;
+
+	/* Turn on / off VLAN awareness */
+	mask = YT921X_PORT_IGR_TPIDn_CTAG_M;
+	if (!vlan_filtering)
+		ctrl = 0;
+	else
+		ctrl = YT921X_PORT_IGR_TPIDn_CTAG(0);
+	res = yt921x_reg_update_bits(priv, YT921X_PORTn_IGR_TPID(port),
+				     mask, ctrl);
+	if (res)
+		return res;
+
+	return 0;
+}
+
+static int
+yt921x_pvid_set(struct yt921x_priv *priv, int port, u16 vid)
+{
+	struct dsa_switch *ds = &priv->ds;

drivers/net/dsa/yt921x.c line 1778

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	struct dsa_port *dp = dsa_to_port(ds, port);
+	bool vlan_filtering;
+	u32 mask;
+	u32 ctrl;
+	int res;
+
+	vlan_filtering = dsa_port_is_vlan_filtering(dp);
+
+	if (vlan_filtering) {
+		mask = YT921X_PORT_VLAN_CTRL_CVID_M;

YT921X_PORT_VLAN_CTRL_CVID_M is modified from 4 code paths, can you condense things a little bit (call the same function, with authoritative logic, from all required places)?

Quoted 15 lines
drivers/net/dsa/yt921x.c new file +15 -0
+		ctrl = YT921X_PORT_VLAN_CTRL_CVID(vid);
+		res = yt921x_reg_update_bits(priv, YT921X_PORTn_VLAN_CTRL(port),
+					     mask, ctrl);
+		if (res)
+			return res;
+	}
+
+	mask = YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
+	res = yt921x_reg_clear_bits(priv, YT921X_PORTn_VLAN_CTRL1(port), mask);
+	if (res)
+		return res;
+
+	return 0;
+}
+

drivers/net/dsa/yt921x.c line 1803

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+static int
+yt921x_dsa_port_vlan_filtering(struct dsa_switch *ds, int port,
+			       bool vlan_filtering,
+			       struct netlink_ext_ack *extack)
+{
+	struct yt921x_priv *priv = to_yt921x_priv(ds);
+	int res;
+
+	if ((priv->cpu_ports_mask & BIT(port)) != 0)
+		return 0;

port_vlan_filtering() only gets called for user ports.

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+
+	mutex_lock(&priv->reg_lock);
+	res = yt921x_vlan_filtering(priv, port, vlan_filtering);
+	mutex_unlock(&priv->reg_lock);
+
+	return res;
+}
+
+static int
+yt921x_dsa_port_vlan_add(struct dsa_switch *ds, int port,

drivers/net/dsa/yt921x.c line 1854

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+			 const struct switchdev_obj_port_vlan *vlan,
+			 struct netlink_ext_ack *extack)
+{
+	struct yt921x_priv *priv = to_yt921x_priv(ds);
+	u16 vid = vlan->vid;
+	u16 pvid;
+	int res;
+
+	if ((priv->cpu_ports_mask & BIT(port)) != 0)
+		return 0;

The CPU port is VLAN-unaware by default and the driver leaves it that way, correct?

Quoted 32 lines
drivers/net/dsa/yt921x.c new file +32 -0
+
+	mutex_lock(&priv->reg_lock);
+	do {
+		struct dsa_port *dp = dsa_to_port(ds, port);
+
+		res = yt921x_vlan_add(priv, port, vid,
+				      (vlan->flags &
+				       BRIDGE_VLAN_INFO_UNTAGGED) != 0);
+		if (res)
+			break;
+
+		if (dp->bridge) {
+			if ((vlan->flags & BRIDGE_VLAN_INFO_PVID) != 0) {
+				res = yt921x_pvid_set(priv, port, vid);
+			} else {
+				br_vlan_get_pvid(dp->bridge->dev, &pvid);
+				if (pvid == vid)
+					res = yt921x_pvid_clear(priv, port);
+			}
+		}
+	} while (0);
+	mutex_unlock(&priv->reg_lock);
+
+	return res;
+}
+
+static int yt921x_userport_bridge(struct yt921x_priv *priv, int port)
+{
+	u32 mask;
+	int res;
+
+	mask = YT921X_PORT_LEARN_DIS;

drivers/net/dsa/yt921x.c line 1933

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	res = yt921x_reg_clear_bits(priv, YT921X_PORTn_LEARN(port), mask);
+	if (res)
+		return res;
+
+	res = yt921x_fdb_flush_port(priv, port, true);
+	if (res)
+		return res;
+
+	return 0;
+}

See what the following code paths do:

dsa_port_bridge_join()
-> dsa_port_switchdev_sync_attrs()

dsa_port_bridge_leave()
-> dsa_port_switchdev_unsync_attrs()
-> dsa_port_clear_brport_flags()
-> dsa_port_set_state_now(BR_STATE_FORWARDING)
-> dsa_port_reset_vlan_filtering()

del_nbp()
-> br_stp_disable_port()
-> br_set_state(BR_STATE_DISABLED)

      -> ...
         -> dsa_user_port_attr_set(SWITCHDEV_ATTR_ID_PORT_STP_STATE)
            -> dsa_port_fast_age()

to get an impression of the operations you are doing twice on bridge join/leave. You only need to manage the initial port state, then DSA should move you from one operating mode to another.

Quoted 47 lines
drivers/net/dsa/yt921x.c new file +47 -0
+static int yt921x_chip_setup(struct yt921x_priv *priv)
+{
+	struct dsa_switch *ds = &priv->ds;
+	unsigned long cpu_ports_mask;
+	u64 ctrl64;
+	u32 ctrl;
+	int port;
+	int res;
+
+	/* Enable DSA */
+	priv->cpu_ports_mask = dsa_cpu_ports(ds);
+
+	ctrl = YT921X_EXT_CPU_PORT_TAG_EN | YT921X_EXT_CPU_PORT_PORT_EN |
+	       YT921X_EXT_CPU_PORT_PORT(__ffs(priv->cpu_ports_mask));
+	res = yt921x_reg_write(priv, YT921X_EXT_CPU_PORT, ctrl);
+	if (res)
+		return res;
+
+	/* Enable and clear MIB */
+	res = yt921x_reg_set_bits(priv, YT921X_FUNC, YT921X_FUNC_MIB);
+	if (res)
+		return res;
+
+	ctrl = YT921X_MIB_CTRL_CLEAN | YT921X_MIB_CTRL_ALL_PORT;
+	res = yt921x_reg_write(priv, YT921X_MIB_CTRL, ctrl);
+	if (res)
+		return res;
+
+	/* Setup software switch */
+	ctrl = YT921X_CPU_COPY_TO_EXT_CPU;
+	res = yt921x_reg_write(priv, YT921X_CPU_COPY, ctrl);
+	if (res)
+		return res;
+
+	ctrl = GENMASK(10, 0);
+	res = yt921x_reg_write(priv, YT921X_FILTER_UNK_UCAST, ctrl);
+	if (res)
+		return res;
+	res = yt921x_reg_write(priv, YT921X_FILTER_UNK_MCAST, ctrl);
+	if (res)
+		return res;
+
+	/* YT921x does not support native DSA port bridging, so we use port
+	 * isolation to emulate it. However, be especially careful that port
+	 * isolation takes _after_ FDB lookups, i.e. if an FDB entry (from
+	 * another bridge) is matched and the destination port (in another
+	 * bridge) is blocked, the packet will be dropped instead of flooding to

drivers/net/dsa/yt921x.c line 2722

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	 * the "bridged" ports, thus we need to handle those packets by
+	 * software.
+	 *
+	 * If there is no more than one bridge, we might be able to drop them
+	 * directly given some conditions are met, but for now we trap them in
+	 * all cases.
+	 */
+	ctrl = 0;
+	for (int i = 0; i < YT921X_PORT_NUM; i++)
+		ctrl |= YT921X_ACT_UNK_ACTn_TRAP(i);

YT921X_ACT_UNK_ACTn_TRAP traps packets with FDB misses to the CPU, correct? How does that address the described problem, where an FDB entry is found, but the destination port is isolated from the source?

Quoted 43 lines
drivers/net/dsa/yt921x.c new file +43 -0
+	/* Except for CPU ports, if any packets are sent via CPU ports without
+	 * tag, they should be dropped.
+	 */
+	cpu_ports_mask = priv->cpu_ports_mask;
+	for_each_set_bit(port, &cpu_ports_mask, YT921X_PORT_NUM) {
+		ctrl &= ~YT921X_ACT_UNK_ACTn_M(port);
+		ctrl |= YT921X_ACT_UNK_ACTn_DROP(port);
+	}
+	res = yt921x_reg_write(priv, YT921X_ACT_UNK_UCAST, ctrl);
+	if (res)
+		return res;
+	res = yt921x_reg_write(priv, YT921X_ACT_UNK_MCAST, ctrl);
+	if (res)
+		return res;
+
+	/* Tagged VID 0 should be treated as untagged, which confuses the
+	 * hardware a lot
+	 */
+	ctrl64 = YT921X_VLAN_CTRL_LEARN_DIS | YT921X_VLAN_CTRL_PORTS_M;
+	res = yt921x_reg64_write(priv, YT921X_VLANn_CTRL(0), ctrl64);
+	if (res)
+		return res;
+
+	/* Miscellaneous */
+	res = yt921x_reg_set_bits(priv, YT921X_SENSOR, YT921X_SENSOR_TEMP);
+	if (res)
+		return res;
+
+	return 0;
+}
+
+static int yt921x_dsa_setup(struct dsa_switch *ds)
+{
+	struct yt921x_priv *priv = to_yt921x_priv(ds);
+	struct device *dev = to_device(priv);
+	struct device_node *np = dev->of_node;
+	struct device_node *child;
+	int res;
+
+	mutex_lock(&priv->reg_lock);
+	res = yt921x_chip_reset(priv);
+	mutex_unlock(&priv->reg_lock);
+

drivers/net/dsa/yt921x.c line 2775

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	if (res)
+		return res;
+
+	/* Register the internal mdio bus. Nodes for internal ports should have
+	 * proper phy-handle pointing to their PHYs. Not enabling the internal
+	 * bus is possible, though pretty wired, if internal ports are not used.
+	 */
+	child = of_get_child_by_name(np, "mdio");
+	if (!child) {
+		priv->mbus_int = NULL;

You probably don't need this branch, priv is allocated with devm_kzalloc() and thus, priv->mbus_int is NULL without any effort.

drivers/net/dsa/yt921x.c line 2786

Quoted 11 lines
drivers/net/dsa/yt921x.c new file +11 -0
+	} else {
+		res = yt921x_mbus_int_init(priv, child);
+		of_node_put(child);
+		if (res)
+			return res;
+	}
+
+	/* External mdio bus is optional */
+	child = of_get_child_by_name(np, "mdio-external");
+	if (!child) {
+		priv->mbus_ext = NULL;

Same.

Quoted 24 lines
drivers/net/dsa/yt921x.c new file +24 -0
+	} else {
+		res = yt921x_mbus_ext_init(priv, child);
+		of_node_put(child);
+		if (res)
+			return res;
+
+		dev_err(dev, "Untested external mdio bus\n");
+		return -ENODEV;
+	}
+
+	mutex_lock(&priv->reg_lock);
+	res = yt921x_chip_setup(priv);
+	mutex_unlock(&priv->reg_lock);
+
+	if (res)
+		return res;
+
+	return 0;
+}
+
+static void yt921x_mdio_remove(struct mdio_device *mdiodev)
+{
+	struct yt921x_priv *priv = mdiodev_get_drvdata(mdiodev);
+

drivers/net/dsa/yt921x.c line 2880

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	if (!priv)
+		return;
+
+	for (size_t i = ARRAY_SIZE(priv->ports); i-- > 0; ) {
+		struct yt921x_port *pp = &priv->ports[i];
+
+		cancel_delayed_work_sync(&pp->mib_read);
+	}
+
+	mutex_destroy(&priv->reg_lock);

This seems destroyed way too early, dsa_unregister_switch() triggers a bunch of driver methods, so you need to destroy it after it finishes.

Quoted 3 lines
drivers/net/dsa/yt921x.c new file +3 -0
+
+	dsa_unregister_switch(&priv->ds);
+}
maintainer
#8 lore raw
Quoted 6 lines
Quoted 6 lines
drivers/net/dsa/yt921x.c new file +6 -0
+static int yt921x_reg_mdio_read(void *context, u32 reg, u32 *valp)
+{
+	struct yt921x_reg_mdio *mdio = context;
+	struct mii_bus *bus = mdio->bus;
+	int addr = mdio->addr;
+	u32 reg_addr;
Quoted 8 lines

drivers/net/dsa/yt921x.c line 349

Quoted 6 lines
drivers/net/dsa/yt921x.c new file +6 -0
+	u32 reg_data;
+	u32 val;
+	int res;
+
+	/* Hold the mdio bus lock to avoid (un)locking for 4 times */
+	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);

Andrew, are you satisfied with this lock?

This is O.K. You snipped too much context. As the comment says, the code is about to do 4 MDIO bus transactions. Each will take and release the lock. By taking it now, and then using the unlocked version for read/write, it will make it a tiny bit faster. The time to do the bus transaction will however dominate.

Quoted 2 lines

Perhaps I missed some part of
the conversation, but didn't you say "leave the mdio lock alone"?

Yes, i did, but then the mdio lock was being abused as a DSA driver lock. The DSA driver now has its own lock. So what we see above is purely an optimisation, not a locking scheme.

Andrew

maintainer
#9 lore raw
Quoted 21 lines · Andrew Lunn
Quoted 6 lines
Quoted 6 lines
drivers/net/dsa/yt921x.c new file +6 -0
+static int yt921x_reg_mdio_read(void *context, u32 reg, u32 *valp)
+{
+	struct yt921x_reg_mdio *mdio = context;
+	struct mii_bus *bus = mdio->bus;
+	int addr = mdio->addr;
+	u32 reg_addr;
Quoted 8 lines

drivers/net/dsa/yt921x.c line 349

Quoted 6 lines
drivers/net/dsa/yt921x.c new file +6 -0
+	u32 reg_data;
+	u32 val;
+	int res;
+
+	/* Hold the mdio bus lock to avoid (un)locking for 4 times */
+	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);

Andrew, are you satisfied with this lock?

This is O.K. You snipped too much context. As the comment says, the code is about to do 4 MDIO bus transactions. Each will take and release the lock. By taking it now, and then using the unlocked version for read/write, it will make it a tiny bit faster. The time to do the bus transaction will however dominate.

Quoted 8 lines
Quoted 2 lines

Perhaps I missed some part of
the conversation, but didn't you say "leave the mdio lock alone"?

Yes, i did, but then the mdio lock was being abused as a DSA driver lock. The DSA driver now has its own lock. So what we see above is purely an optimisation, not a locking scheme.

Andrew

Ok, I misunderstood the reason for your comment to leave the MDIO lock alone. Acquiring &bus->mdio_lock I can agree with, regardless of whether the "driver lock" exists. I'm currently reviewing drivers/mfd/airoha-an8855.c and it handles this the same way.

I'm not sure that a "driver lock" is something that drivers need. In this case it creates a lot of red tape. Function yt921x_dsa_X() takes the driver lock and calls function yt921x_X() which does the work. IMO that's part of what gives "vendor crap" drivers their name, when there's no reason behind it.

maintainer
#10 lore raw
Quoted 5 lines

I'm not sure that a "driver lock" is something that drivers need. In this case it creates a lot of red tape. Function yt921x_dsa_X() takes the driver lock and calls function yt921x_X() which does the work. IMO that's part of what gives "vendor crap" drivers their name, when there's no reason behind it.

As you said, some methods are protected by RTNL. But not all. And it is hard to know which are not. A driver lock is KISS, and easy to get right, easy to see is right, and easy to prove is right. Locking can be hard, so KISS is good.

Andrew

author maintainer
#11 lore raw
In reply to Vladimir Oltean

On Wed, Sep 17, 2025 at 7:17 AM Vladimir Oltean <[email protected]> wrote:
...

Quoted 15 lines
Quoted 15 lines
drivers/net/dsa/yt921x.c new file +15 -0
+
+     mutex_lock(&priv->reg_lock);
+     res = yt921x_vlan_filtering(priv, port, vlan_filtering);
+     mutex_unlock(&priv->reg_lock);
+
+     return res;
+}
+
+static int
+yt921x_dsa_port_vlan_add(struct dsa_switch *ds, int port,
+                      const struct switchdev_obj_port_vlan *vlan,
+                      struct netlink_ext_ack *extack)
+{
+     struct yt921x_priv *priv = to_yt921x_priv(ds);
+     u16 vid = vlan->vid;
Quoted 9 lines

drivers/net/dsa/yt921x.c line 1822

Quoted 5 lines
drivers/net/dsa/yt921x.c new file +5 -0
+     u16 pvid;
+     int res;
+
+     if ((priv->cpu_ports_mask & BIT(port)) != 0)
+             return 0;

The CPU port is VLAN-unaware by default and the driver leaves it that way, correct?

Yes. Actually flows via CPU port are entirely up to the tag, and we actively isolate it from any other ports (see port_setup()) so packets without tag are automatically dropped.

...

Quoted 52 lines
Quoted 52 lines
drivers/net/dsa/yt921x.c new file +51 -0
+static int yt921x_chip_setup(struct yt921x_priv *priv)
+{
+     struct dsa_switch *ds = &priv->ds;
+     unsigned long cpu_ports_mask;
+     u64 ctrl64;
+     u32 ctrl;
+     int port;
+     int res;
+
+     /* Enable DSA */
+     priv->cpu_ports_mask = dsa_cpu_ports(ds);
+
+     ctrl = YT921X_EXT_CPU_PORT_TAG_EN | YT921X_EXT_CPU_PORT_PORT_EN |
+            YT921X_EXT_CPU_PORT_PORT(__ffs(priv->cpu_ports_mask));
+     res = yt921x_reg_write(priv, YT921X_EXT_CPU_PORT, ctrl);
+     if (res)
+             return res;
+
+     /* Enable and clear MIB */
+     res = yt921x_reg_set_bits(priv, YT921X_FUNC, YT921X_FUNC_MIB);
+     if (res)
+             return res;
+
+     ctrl = YT921X_MIB_CTRL_CLEAN | YT921X_MIB_CTRL_ALL_PORT;
+     res = yt921x_reg_write(priv, YT921X_MIB_CTRL, ctrl);
+     if (res)
+             return res;
+
+     /* Setup software switch */
+     ctrl = YT921X_CPU_COPY_TO_EXT_CPU;
+     res = yt921x_reg_write(priv, YT921X_CPU_COPY, ctrl);
+     if (res)
+             return res;
+
+     ctrl = GENMASK(10, 0);
+     res = yt921x_reg_write(priv, YT921X_FILTER_UNK_UCAST, ctrl);
+     if (res)
+             return res;
+     res = yt921x_reg_write(priv, YT921X_FILTER_UNK_MCAST, ctrl);
+     if (res)
+             return res;
+
+     /* YT921x does not support native DSA port bridging, so we use port
+      * isolation to emulate it. However, be especially careful that port
+      * isolation takes _after_ FDB lookups, i.e. if an FDB entry (from
+      * another bridge) is matched and the destination port (in another
+      * bridge) is blocked, the packet will be dropped instead of flooding to
+      * the "bridged" ports, thus we need to handle those packets by
+      * software.
+      *
+      * If there is no more than one bridge, we might be able to drop them
Quoted 8 lines

drivers/net/dsa/yt921x.c line 2722

Quoted 6 lines
drivers/net/dsa/yt921x.c new file +6 -0
+      * directly given some conditions are met, but for now we trap them in
+      * all cases.
+      */
+     ctrl = 0;
+     for (int i = 0; i < YT921X_PORT_NUM; i++)
+             ctrl |= YT921X_ACT_UNK_ACTn_TRAP(i);

YT921X_ACT_UNK_ACTn_TRAP traps packets with FDB misses to the CPU, correct?

Yes, and

Quoted 2 lines

How does that address the described problem, where an FDB entry is found, but the destination port is isolated from the source?

also in this case.

...