Skip to contentSkip to conversation

net: dsa: yt921x: Add support for Motorcomm YT921x

PATCH Superseded David Yang 2 participants 7 messages Sep 12, 2025

Subsystems (5)

v8 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

David Yang

author maintainer
#0 lore raw

v8 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.

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                      | 2944 +++++++++++++++++
 drivers/net/dsa/yt921x.h                      |  587 ++++
 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                          |  138 +
 10 files changed, 3856 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.50.1
author maintainer
#1 lore raw

PATCH v8 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.50.1
author maintainer
#2 lore raw

PATCH v8 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          | 138 ++++++++++++++++++++++++++++++++++
 5 files changed, 148 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 +138 -0

138 lines not on this page · raw message

Signature · 1 line
2.50.1
author maintainer
#3 lore raw

PATCH v8 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 | 2944 ++++++++++++++++++++++++++++++++++++++
 drivers/net/dsa/yt921x.h |  587 ++++++++
 4 files changed, 3539 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 +2944 -0

2,944 lines not on this page · raw message

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

38 lines not on this page · raw message

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

Signature · 1 line
2.50.1

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

maintainer
#4 lore raw

drivers/net/dsa/yt921x.c line 294

Quoted 11 lines
drivers/net/dsa/yt921x.c new file +11 -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;

Where did these two values come from? Are they documented in the datasheet?

Quoted 6 lines
drivers/net/dsa/yt921x.c new file +6 -0
+	default:
+		return;
+	}
+
+	/* Skip registers which are likely to have any valid values */
+	switch (reg) {

drivers/net/dsa/yt921x.c line 310

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	case YT921X_MAC_ADDR_HI2:
+	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);

You probably have a warning from checkpatch about pr_warn. Ideally you want to give an indication which device has triggered this, making use of a struct device. You might want to include that in context.

drivers/net/dsa/yt921x.c line 456

Quoted 9 lines
drivers/net/dsa/yt921x.c new file +9 -0
+static int
+yt921x_intif_read(struct yt921x_priv *priv, int port, int reg, u16 *valp)
+{
+	if ((u16)val != val)
+		dev_err(dev,
+			"%s: port %d, reg 0x%x: Expected u16, got 0x%08x\n",
+			__func__, port, reg, val);
+	*valp = (u16)val;
+	return 0;

You don't treat this as an error, you don't return -EIO or -EPROTO etc. So maybe this should be dev_info() or dev_dbg().

drivers/net/dsa/yt921x.c line 512

Quoted 8 lines
drivers/net/dsa/yt921x.c new file +8 -0
+static int
+yt921x_mbus_int_write(struct mii_bus *mbus, int port, int reg, u16 data)
+{
+	struct yt921x_priv *priv = mbus->priv;
+	int res;
+
+	if (port >= YT921X_PORT_NUM)
+		return 0;

-ENODEV.

drivers/net/dsa/yt921x.c line 543

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+yt921x_mbus_int_init(struct yt921x_priv *priv, struct device_node *mnp)
+{
+	struct device *dev = to_device(priv);
+	struct mii_bus *mbus;
+	int res;
+
+	if (!mnp)
+		res = devm_mdiobus_register(dev, mbus);
+	else
+		res = devm_of_mdiobus_register(dev, mbus, mnp);

You can call devm_of_mdiobus_register() with a NULL pointer for the OF, and it will do the correct thing.

Quoted 9 lines
drivers/net/dsa/yt921x.c new file +9 -0
+static int yt921x_extif_wait(struct yt921x_priv *priv)
+{
+	u32 val;
+	int res;
+
+	res = yt921x_reg_read(priv, YT921X_EXT_MBUS_OP, &val);
+	if (res)
+		return res;
+	if ((val & YT921X_MBUS_OP_START) != 0) {

drivers/net/dsa/yt921x.c line 570

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+		res = read_poll_timeout(yt921x_reg_read, res,
+					(val & YT921X_MBUS_OP_START) == 0,
+					YT921X_POLL_SLEEP_US,
+					YT921X_POLL_TIMEOUT_US,
+					true, priv, YT921X_EXT_MBUS_OP, &val);
+		if (res)
+			return res;
+	}
+
+	return 0;

In mv88e6xxx, we have the generic mv88e6xxx_wait_mask() and on top of that mv88e6xxx_wait_bit(). That allows us to have register specific wait functions as one liners. Please consider something similar.

drivers/net/dsa/yt921x.c line 700

Quoted 2 lines
drivers/net/dsa/yt921x.c new file +2 -0
+static int yt921x_mib_read(struct yt921x_priv *priv, int port, void *data)
+{

As far as i can see, data is always a pointer to struct yt921x_mib_raw. I would be better to not have the void in the middle. It also makes it clearer what assumption you are making about the layout of that structure.

Quoted 9 lines
drivers/net/dsa/yt921x.c new file +9 -0
+	unsigned char *buf = data;
+	int res = 0;
+
+	for (size_t i = 0; i < sizeof(struct yt921x_mib_raw);
+	     i += sizeof(u32)) {
+		res = yt921x_reg_read(priv, YT921X_MIBn_DATA0(port) + i,
+				      (u32 *)&buf[i]);
+		if (res)
+			break;

drivers/net/dsa/yt921x.c line 719

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	}
+	return res;
+}
+
+static void yt921x_poll_mib(struct work_struct *work)
+{
+	struct yt921x_port *pp = container_of_const(work, struct yt921x_port,
+						    mib_read.work);
+	struct yt921x_priv *priv = (void *)(pp - pp->index) -
+				   offsetof(struct yt921x_priv, ports);

Can you make container_of() work for this?

Quoted 24 lines
drivers/net/dsa/yt921x.c new file +24 -0
+	unsigned long delay = YT921X_STATS_INTERVAL_JIFFIES;
+	struct device *dev = to_device(priv);
+	struct yt921x_mib *mib = &pp->mib;
+	struct yt921x_mib_raw raw;
+	int port = pp->index;
+	int res;
+
+	yt921x_reg_lock(priv);
+	res = yt921x_mib_read(priv, port, &raw);
+	yt921x_reg_unlock(priv);
+
+	if (res) {
+		dev_err(dev, "Failed to %s port %d: %i\n", "read stats for",
+			port, res);
+		delay *= 4;
+		goto end;
+	}
+
+	spin_lock(&pp->stats_lock);
+
+	/* Handle overflow of 32bit MIBs */
+	for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
+		const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
+		u32 *rawp = (u32 *)((u8 *)&raw + desc->offset);

drivers/net/dsa/yt921x.c line 753

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+		u64 *valp = &((u64 *)mib)[i];
+		u64 newval;
+
+		if (desc->size > 1) {
+			newval = ((u64)rawp[0] << 32) | rawp[1];
+		} else {
+			newval = (*valp & ~(u64)U32_MAX) | *rawp;
+			if (*rawp < (u32)*valp)
+				newval += (u64)1 << 32;
+		}

There are way too many casts here. Think about your types, and how you can remove some of these casts. In general, casts are bad, and should be avoided where possible.

Quoted 11 lines
drivers/net/dsa/yt921x.c new file +10 -0
+static void
+yt921x_dsa_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
+{
+	struct yt921x_priv *priv = to_yt921x_priv(ds);
+	struct yt921x_port *pp = &priv->ports[port];
+	struct yt921x_mib *mib = &pp->mib;
+	size_t j;
+
+	spin_lock(&pp->stats_lock);
+

drivers/net/dsa/yt921x.c line 807

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	j = 0;
+	for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
+		const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
+
+		if (!desc->unstructured)
+			continue;
+
+		data[j] = ((u64 *)mib)[i];
+		j++;
+	}

ethtool APIs are called in a context where you can block. So it would be good to updated the statistics first before copying them. You just need to think about your locking in case the worker is running.

drivers/net/dsa/yt921x.c line 819

Quoted 8 lines
drivers/net/dsa/yt921x.c new file +8 -0
+static int yt921x_dsa_get_sset_count(struct dsa_switch *ds, int port, int sset)
+{
+	int cnt;
+
+	if (sset != ETH_SS_STATS)
+		return 0;
+
+	cnt = 0;

Please do the zeroing above when you declare the local variable.

Quoted 11 lines
drivers/net/dsa/yt921x.c new file +10 -0
+	for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
+		const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
+
+		if (desc->unstructured)
+			cnt++;
+	}
+
+	return cnt;
+}
 
+static int

drivers/net/dsa/yt921x.c line 1018

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +9 -0
+yt921x_set_eee(struct yt921x_priv *priv, int port, struct ethtool_keee *e)
+{
 
+	/* Enable / disable port EEE */
+	res = yt921x_reg_toggle_bits(priv, YT921X_EEE_CTRL,
+				     YT921X_EEE_CTRL_ENn(port), enable);
+	if (res)
+		return res;
+	res = yt921x_reg_toggle_bits(priv, YT921X_EEEn_VAL(port),
+				     YT921X_EEE_VAL_DATA, enable);

How do these two different registers differ? Why are there two of them? Maybe add a comment to explain this.

drivers/net/dsa/yt921x.c line 1029

Quoted 5 lines
drivers/net/dsa/yt921x.c new file +5 -0
+static bool yt921x_dsa_support_eee(struct dsa_switch *ds, int port)
+{
+	struct yt921x_priv *priv = to_yt921x_priv(ds);
+
+	return (priv->pon_strap_cap & YT921X_PON_STRAP_EEE) != 0;

What does the strapping actually tell you?

Quoted 33 lines
drivers/net/dsa/yt921x.c new file +33 -0
+static int
+yt921x_dsa_port_mirror_add(struct dsa_switch *ds, int port,
+			   struct dsa_mall_mirror_tc_entry *mirror,
+			   bool ingress, struct netlink_ext_ack *extack)
+{
+	struct yt921x_priv *priv = to_yt921x_priv(ds);
+	u32 ctrl;
+	u32 val;
+	int res;
+
+	yt921x_reg_lock(priv);
+	do {
+		u32 srcs;
+		u32 dst;
+
+		if (ingress)
+			srcs = YT921X_MIRROR_IGR_PORTn(port);
+		else
+			srcs = YT921X_MIRROR_EGR_PORTn(port);
+		dst = YT921X_MIRROR_PORT(mirror->to_local_port);
+
+		res = yt921x_reg_read(priv, YT921X_MIRROR, &val);
+		if (res)
+			break;
+
+		/* other mirror tasks & different dst port -> conflict */
+		if ((val & ~srcs & (YT921X_MIRROR_EGR_PORTS_M |
+				    YT921X_MIRROR_IGR_PORTS_M)) != 0 &&
+		    (val & YT921X_MIRROR_PORT_M) != dst) {
+			NL_SET_ERR_MSG_MOD(extack,
+					   "Sniffer port is already configured,"
+					   " delete existing rules & retry");
+			res = -EBUSY;

drivers/net/dsa/yt921x.c line 1140

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+			break;
+		}
+
+		ctrl = val & ~YT921X_MIRROR_PORT_M;
+		ctrl |= srcs;
+		ctrl |= dst;
+
+		if (ctrl != val)
+			res = yt921x_reg_write(priv, YT921X_MIRROR, ctrl);
+	} while (0);

What does a while (0) loop bring you here?

Andrew

---
pw-bot: cr

author maintainer
#5 lore raw
Quoted 15 lines · Andrew Lunn

drivers/net/dsa/yt921x.c line 294

Quoted 12 lines
drivers/net/dsa/yt921x.c new file +11 -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;

Where did these two values come from? Are they documented in the datasheet?

I don't have the comprehensive datasheet and it (along with other parts) is not mentioned in the documents I have, so I dug them out by testing.

Quoted 12 lines
Quoted 12 lines
drivers/net/dsa/yt921x.c new file +12 -0
+     default:
+             return;
+     }
+
+     /* Skip registers which are likely to have any valid values */
+     switch (reg) {
+     case YT921X_MAC_ADDR_HI2:
+     case YT921X_MAC_ADDR_LO4:
+     case YT921X_FDB_OUT0:
+     case YT921X_FDB_OUT1:
+             return;
+     }
Quoted 9 lines

drivers/net/dsa/yt921x.c line 310

Quoted 4 lines
drivers/net/dsa/yt921x.c new file +4 -0
+
+     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);

You probably have a warning from checkpatch about pr_warn. Ideally you want to give an indication which device has triggered this, making use of a struct device. You might want to include that in context.

I don't want to introduce priv struct layout into register implementations, which makes unnecessary dependency between functions. The warning will not appear if no bugs exist, and it can still be easily identified by its function name.

Quoted 13 lines

drivers/net/dsa/yt921x.c line 456

Quoted 9 lines
drivers/net/dsa/yt921x.c new file +9 -0
+static int
+yt921x_intif_read(struct yt921x_priv *priv, int port, int reg, u16 *valp)
+{
+     if ((u16)val != val)
+             dev_err(dev,
+                     "%s: port %d, reg 0x%x: Expected u16, got 0x%08x\n",
+                     __func__, port, reg, val);
+     *valp = (u16)val;
+     return 0;

You don't treat this as an error, you don't return -EIO or -EPROTO etc. So maybe this should be dev_info() or dev_dbg().

Quoted 11 lines

drivers/net/dsa/yt921x.c line 512

Quoted 8 lines
drivers/net/dsa/yt921x.c new file +8 -0
+static int
+yt921x_mbus_int_write(struct mii_bus *mbus, int port, int reg, u16 data)
+{
+     struct yt921x_priv *priv = mbus->priv;
+     int res;
+
+     if (port >= YT921X_PORT_NUM)
+             return 0;

-ENODEV.

mdio-tools complains a lot when returning an error code. Also that is what dsa_user_phy_write() returns for a non-existing port.

Quoted 59 lines

drivers/net/dsa/yt921x.c line 543

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+yt921x_mbus_int_init(struct yt921x_priv *priv, struct device_node *mnp)
+{
+     struct device *dev = to_device(priv);
+     struct mii_bus *mbus;
+     int res;
+
+     if (!mnp)
+             res = devm_mdiobus_register(dev, mbus);
+     else
+             res = devm_of_mdiobus_register(dev, mbus, mnp);

You can call devm_of_mdiobus_register() with a NULL pointer for the OF, and it will do the correct thing.

Quoted 9 lines
drivers/net/dsa/yt921x.c new file +9 -0
+static int yt921x_extif_wait(struct yt921x_priv *priv)
+{
+     u32 val;
+     int res;
+
+     res = yt921x_reg_read(priv, YT921X_EXT_MBUS_OP, &val);
+     if (res)
+             return res;
+     if ((val & YT921X_MBUS_OP_START) != 0) {

drivers/net/dsa/yt921x.c line 570

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+             res = read_poll_timeout(yt921x_reg_read, res,
+                                     (val & YT921X_MBUS_OP_START) == 0,
+                                     YT921X_POLL_SLEEP_US,
+                                     YT921X_POLL_TIMEOUT_US,
+                                     true, priv, YT921X_EXT_MBUS_OP, &val);
+             if (res)
+                     return res;
+     }
+
+     return 0;

In mv88e6xxx, we have the generic mv88e6xxx_wait_mask() and on top of that mv88e6xxx_wait_bit(). That allows us to have register specific wait functions as one liners. Please consider something similar.

drivers/net/dsa/yt921x.c line 700

Quoted 2 lines
drivers/net/dsa/yt921x.c new file +2 -0
+static int yt921x_mib_read(struct yt921x_priv *priv, int port, void *data)
+{

As far as i can see, data is always a pointer to struct yt921x_mib_raw. I would be better to not have the void in the middle. It also makes it clearer what assumption you are making about the layout of that structure.

Quoted 13 lines
drivers/net/dsa/yt921x.c new file +13 -0
+     unsigned char *buf = data;
+     int res = 0;
+
+     for (size_t i = 0; i < sizeof(struct yt921x_mib_raw);
+          i += sizeof(u32)) {
+             res = yt921x_reg_read(priv, YT921X_MIBn_DATA0(port) + i,
+                                   (u32 *)&buf[i]);
+             if (res)
+                     break;
+     }
+     return res;
+}
+
Quoted 9 lines

drivers/net/dsa/yt921x.c line 719

Quoted 6 lines
drivers/net/dsa/yt921x.c new file +6 -0
+static void yt921x_poll_mib(struct work_struct *work)
+{
+     struct yt921x_port *pp = container_of_const(work, struct yt921x_port,
+                                                 mib_read.work);
+     struct yt921x_priv *priv = (void *)(pp - pp->index) -
+                                offsetof(struct yt921x_priv, ports);

Can you make container_of() work for this?

Impossible, also see ar9331_sw_port_to_priv().

Quoted 30 lines
Quoted 30 lines
drivers/net/dsa/yt921x.c new file +30 -0
+     unsigned long delay = YT921X_STATS_INTERVAL_JIFFIES;
+     struct device *dev = to_device(priv);
+     struct yt921x_mib *mib = &pp->mib;
+     struct yt921x_mib_raw raw;
+     int port = pp->index;
+     int res;
+
+     yt921x_reg_lock(priv);
+     res = yt921x_mib_read(priv, port, &raw);
+     yt921x_reg_unlock(priv);
+
+     if (res) {
+             dev_err(dev, "Failed to %s port %d: %i\n", "read stats for",
+                     port, res);
+             delay *= 4;
+             goto end;
+     }
+
+     spin_lock(&pp->stats_lock);
+
+     /* Handle overflow of 32bit MIBs */
+     for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
+             const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
+             u32 *rawp = (u32 *)((u8 *)&raw + desc->offset);
+             u64 *valp = &((u64 *)mib)[i];
+             u64 newval;
+
+             if (desc->size > 1) {
+                     newval = ((u64)rawp[0] << 32) | rawp[1];
+             } else {
Quoted 9 lines

drivers/net/dsa/yt921x.c line 753

Quoted 4 lines
drivers/net/dsa/yt921x.c new file +4 -0
+                     newval = (*valp & ~(u64)U32_MAX) | *rawp;
+                     if (*rawp < (u32)*valp)
+                             newval += (u64)1 << 32;
+             }

There are way too many casts here. Think about your types, and how you can remove some of these casts. In general, casts are bad, and should be avoided where possible.

Some casts are necessary for shifting operations, otherwise an error will pop out for shift overflow.

Others are just synonyms of val & 0xffffffff. I'd consider casts are neater than magic numbers.

Quoted 59 lines
Quoted 11 lines
drivers/net/dsa/yt921x.c new file +10 -0
+static void
+yt921x_dsa_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
+{
+     struct yt921x_priv *priv = to_yt921x_priv(ds);
+     struct yt921x_port *pp = &priv->ports[port];
+     struct yt921x_mib *mib = &pp->mib;
+     size_t j;
+
+     spin_lock(&pp->stats_lock);
+

drivers/net/dsa/yt921x.c line 807

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +10 -0
+     j = 0;
+     for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
+             const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
+
+             if (!desc->unstructured)
+                     continue;
+
+             data[j] = ((u64 *)mib)[i];
+             j++;
+     }

ethtool APIs are called in a context where you can block. So it would be good to updated the statistics first before copying them. You just need to think about your locking in case the worker is running.

drivers/net/dsa/yt921x.c line 819

Quoted 8 lines
drivers/net/dsa/yt921x.c new file +8 -0
+static int yt921x_dsa_get_sset_count(struct dsa_switch *ds, int port, int sset)
+{
+     int cnt;
+
+     if (sset != ETH_SS_STATS)
+             return 0;
+
+     cnt = 0;

Please do the zeroing above when you declare the local variable.

Quoted 11 lines
drivers/net/dsa/yt921x.c new file +10 -0
+     for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
+             const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
+
+             if (desc->unstructured)
+                     cnt++;
+     }
+
+     return cnt;
+}
 
+static int

drivers/net/dsa/yt921x.c line 1018

Quoted 10 lines
drivers/net/dsa/yt921x.c new file +9 -0
+yt921x_set_eee(struct yt921x_priv *priv, int port, struct ethtool_keee *e)
+{
 
+     /* Enable / disable port EEE */
+     res = yt921x_reg_toggle_bits(priv, YT921X_EEE_CTRL,
+                                  YT921X_EEE_CTRL_ENn(port), enable);
+     if (res)
+             return res;
+     res = yt921x_reg_toggle_bits(priv, YT921X_EEEn_VAL(port),
+                                  YT921X_EEE_VAL_DATA, enable);
Quoted 3 lines

How do these two different registers differ? Why are there two of them? Maybe add a comment to explain this.

Datasheet gives no explanation here too, so this is a carbon copy of the sample code. I'm also confused too.

Quoted 8 lines

drivers/net/dsa/yt921x.c line 1029

Quoted 5 lines
drivers/net/dsa/yt921x.c new file +5 -0
+static bool yt921x_dsa_support_eee(struct dsa_switch *ds, int port)
+{
+     struct yt921x_priv *priv = to_yt921x_priv(ds);
+
+     return (priv->pon_strap_cap & YT921X_PON_STRAP_EEE) != 0;

What does the strapping actually tell you?

Whether EEE capability is present.

Quoted 37 lines
Quoted 37 lines
drivers/net/dsa/yt921x.c new file +37 -0
+static int
+yt921x_dsa_port_mirror_add(struct dsa_switch *ds, int port,
+                        struct dsa_mall_mirror_tc_entry *mirror,
+                        bool ingress, struct netlink_ext_ack *extack)
+{
+     struct yt921x_priv *priv = to_yt921x_priv(ds);
+     u32 ctrl;
+     u32 val;
+     int res;
+
+     yt921x_reg_lock(priv);
+     do {
+             u32 srcs;
+             u32 dst;
+
+             if (ingress)
+                     srcs = YT921X_MIRROR_IGR_PORTn(port);
+             else
+                     srcs = YT921X_MIRROR_EGR_PORTn(port);
+             dst = YT921X_MIRROR_PORT(mirror->to_local_port);
+
+             res = yt921x_reg_read(priv, YT921X_MIRROR, &val);
+             if (res)
+                     break;
+
+             /* other mirror tasks & different dst port -> conflict */
+             if ((val & ~srcs & (YT921X_MIRROR_EGR_PORTS_M |
+                                 YT921X_MIRROR_IGR_PORTS_M)) != 0 &&
+                 (val & YT921X_MIRROR_PORT_M) != dst) {
+                     NL_SET_ERR_MSG_MOD(extack,
+                                        "Sniffer port is already configured,"
+                                        " delete existing rules & retry");
+                     res = -EBUSY;
+                     break;
+             }
+
+             ctrl = val & ~YT921X_MIRROR_PORT_M;
Quoted 9 lines

drivers/net/dsa/yt921x.c line 1140

Quoted 6 lines
drivers/net/dsa/yt921x.c new file +6 -0
+             ctrl |= srcs;
+             ctrl |= dst;
+
+             if (ctrl != val)
+                     res = yt921x_reg_write(priv, YT921X_MIRROR, ctrl);
+     } while (0);

What does a while (0) loop bring you here?

break statement instead of goto err.

Quoted 5 lines

Andrew

---
pw-bot: cr

maintainer
#6 lore raw
Quoted 12 lines
Quoted 11 lines

drivers/net/dsa/yt921x.c line 512

Quoted 8 lines
drivers/net/dsa/yt921x.c new file +8 -0
+static int
+yt921x_mbus_int_write(struct mii_bus *mbus, int port, int reg, u16 data)
+{
+     struct yt921x_priv *priv = mbus->priv;
+     int res;
+
+     if (port >= YT921X_PORT_NUM)
+             return 0;

-ENODEV.

Quoted 1 line

mdio-tools complains a lot when returning an error code.

It should. There is no device there, so its an error.

Quoted 2 lines

Also that is
what dsa_user_phy_write() returns for a non-existing port.

I would say that is not ideal. If you are trying to write to a PHY which does not exist, you want to know about it.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
drivers/net/dsa/yt921x.c new file +16 -0
+     unsigned long delay = YT921X_STATS_INTERVAL_JIFFIES;
+     struct device *dev = to_device(priv);
+     struct yt921x_mib *mib = &pp->mib;
+     struct yt921x_mib_raw raw;
+     int port = pp->index;
+     int res;
+
+     yt921x_reg_lock(priv);
+     res = yt921x_mib_read(priv, port, &raw);
+     yt921x_reg_unlock(priv);
+
+     if (res) {
+             dev_err(dev, "Failed to %s port %d: %i\n", "read stats for",
+                     port, res);
+             delay *= 4;
+             goto end;
Quoted 8 lines
Quoted 8 lines

drivers/net/dsa/yt921x.c line 743

Quoted 8 lines
drivers/net/dsa/yt921x.c new file +8 -0
+     }
+
+     spin_lock(&pp->stats_lock);
+
+     /* Handle overflow of 32bit MIBs */
+     for (size_t i = 0; i < ARRAY_SIZE(yt921x_mib_descs); i++) {
+             const struct yt921x_mib_desc *desc = &yt921x_mib_descs[i];
+             u32 *rawp = (u32 *)((u8 *)&raw + desc->offset);

One thing which might helper is make yt921x_mib_raw and a yt921x_mib union. List all the fields, but also have a u32 array. You might also consider a u64 array, so you don't need to construct a u64 from two u32. But you need to check alignment.

Quoted 10 lines
Quoted 8 lines

drivers/net/dsa/yt921x.c line 1029

Quoted 5 lines
drivers/net/dsa/yt921x.c new file +5 -0
+static bool yt921x_dsa_support_eee(struct dsa_switch *ds, int port)
+{
+     struct yt921x_priv *priv = to_yt921x_priv(ds);
+
+     return (priv->pon_strap_cap & YT921X_PON_STRAP_EEE) != 0;

What does the strapping actually tell you?

Whether EEE capability is present.

This is really a strapping? A resistor pulling the pin high/low?

The silicon itself should know if it has EEE support or not. Strapping is normally used for configuration, what are the reset defaults. So i suspect this strapping says the design of the board would like EEE on/off by default, not that the silicon supports EEE. And the user should have the choice to enable/disable EEE, independent of the board designs defaults.

Quoted 4 lines
Quoted 2 lines

What does a while (0) loop bring you here?

break statement instead of goto err.

Which you only seem to do once, here, and nowhere else. And look at other drivers, how many use a while (0) loop like this?

goto err is the standard way to do this.

Andrew