Skip to contentSkip to conversation

net: dsa: realtek: MDIO interface and RTL8367S

PATCH Superseded Luiz Angelo Daros de Luca 9 participants 88 messages Jan 5, 2022 – Feb 1, 2022

Subsystems (6)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Luiz Angelo Daros de Luca

author maintainer
#0 lore raw

v4 cover net: dsa: realtek: MDIO interface and RTL8367S

The old realtek-smi driver was linking subdrivers into a single realtek-smi.ko After this series, each subdriver will be an independent module required by either realtek-smi (platform driver) or the new realtek-mdio (mdio driver). Both interface drivers (SMI or MDIO) are independent, and they might even work side-by-side, although it will be difficult to find such device. The subdriver can be individually selected but only at buildtime, saving some storage space for custom embedded systems.

Existing realtek-smi devices continue to work untouched during the tests. The realtek-smi was moved into a realtek subdirectory, but it normally does not break things.

I couldn't identify a fixed relation between port numbers (0..9) and external interfaces (0..2), and I'm not sure if it is fixed for each chip version or a device configuration. Until there is more info about it, there is a new port property "realtek,ext-int" that can inform the external interface.

The rtl8365mb might now handle multiple CPU ports and extint ports not used as CPU ports. RTL8367S has an SGMII external interface, but my test device (TP-Link Archer C5v4) uses only the second RGMII interface. We need a test device with more external ports to test these features. The driver still cannot handle SGMII ports.

The rtl8365mb was tested with a MDIO-connected RTL8367S (TP-Link Acher C5v4) and a SMI-connected RTL8365MB-VC switch (Asus RT-AC88U)

The rtl8366rb subdriver was not tested with this patch series, but it was only slightly touched. It would be nice to test it, especially in an MDIO-connected switch.

Best,

Luiz

Changelog:

v1-v2)
- formatting fixes
- dropped the rtl8365mb->rtl8367c rename
- other suggestions

v2-v3)
* realtek-mdio.c:
- cleanup realtek-mdio.c (BUG_ON, comments and includes)
- check devm_regmap_init return code
- removed realtek,rtl8366s string from realtek-mdio
* realtek-smi.c:
- removed void* type cast
* rtl8365mb.c:
- using macros to identify EXT interfaces
- rename some extra extport->extint cases
- allow extint as non cpu (not tested)
- allow multple cpu ports (not tested)
- dropped cpu info from struct rtl8365mb
* dropped dt-bindings changes (dealing outside this series)
* formatting issues fixed

v3-v4)
* fix cover message numbering 0/13 -> 0/11
* use static for realtek_mdio_read_reg
- Reported-by: kernel test robot <[email protected]>
* use dsa_switch_for_each_cpu_port
* mention realtek_smi_{variant,ops} to realtek_{variant,ops} in commit message

author maintainer
#1 lore raw

PATCH v4 1/11 net: dsa: realtek-smi: move to subdirectory

---
 MAINTAINERS                                   |  3 +--
 drivers/net/dsa/Kconfig                       | 12 +----------
 drivers/net/dsa/Makefile                      |  3 +--
 drivers/net/dsa/realtek/Kconfig               | 20 +++++++++++++++++++
 drivers/net/dsa/realtek/Makefile              |  3 +++
 .../net/dsa/{ => realtek}/realtek-smi-core.c  |  0
 .../net/dsa/{ => realtek}/realtek-smi-core.h  |  0
 drivers/net/dsa/{ => realtek}/rtl8365mb.c     |  0
 drivers/net/dsa/{ => realtek}/rtl8366.c       |  0
 drivers/net/dsa/{ => realtek}/rtl8366rb.c     |  0
 10 files changed, 26 insertions(+), 15 deletions(-)
 create mode 100644 drivers/net/dsa/realtek/Kconfig
 create mode 100644 drivers/net/dsa/realtek/Makefile
 rename drivers/net/dsa/{ => realtek}/realtek-smi-core.c (100%)
 rename drivers/net/dsa/{ => realtek}/realtek-smi-core.h (100%)
 rename drivers/net/dsa/{ => realtek}/rtl8365mb.c (100%)
 rename drivers/net/dsa/{ => realtek}/rtl8366.c (100%)
 rename drivers/net/dsa/{ => realtek}/rtl8366rb.c (100%)
MAINTAINERS +1 -2
index 0b5fdb517c76..fc63d1e46798 100644
@@ -16161,8 +16161,7 @@ REALTEK RTL83xx SMI DSA ROUTER CHIPS
 M:	Linus Walleij <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/net/dsa/realtek-smi.txt
-F:	drivers/net/dsa/realtek-smi*
-F:	drivers/net/dsa/rtl83*
+F:	drivers/net/dsa/realtek/*
 
 REALTEK WIRELESS DRIVER (rtlwifi family)
 M:	Ping-Ke Shih <[email protected]>
drivers/net/dsa/Kconfig +1 -11
index 7b1457a6e327..1251caf0f638 100644
@@ -67,17 +67,7 @@ config NET_DSA_QCA8K
 	  This enables support for the Qualcomm Atheros QCA8K Ethernet
 	  switch chips.
 
-config NET_DSA_REALTEK_SMI
-	tristate "Realtek SMI Ethernet switch family support"
-	select NET_DSA_TAG_RTL4_A
-	select NET_DSA_TAG_RTL8_4
-	select FIXED_PHY
-	select IRQ_DOMAIN
-	select REALTEK_PHY
-	select REGMAP
-	help
-	  This enables support for the Realtek SMI-based switch
-	  chips, currently only RTL8366RB.
+source "drivers/net/dsa/realtek/Kconfig"
 
 config NET_DSA_SMSC_LAN9303
 	tristate
drivers/net/dsa/Makefile +1 -2
index 8da1569a34e6..e73838c12256 100644
@@ -9,8 +9,6 @@ obj-$(CONFIG_NET_DSA_LANTIQ_GSWIP) += lantiq_gswip.o
 obj-$(CONFIG_NET_DSA_MT7530)	+= mt7530.o
 obj-$(CONFIG_NET_DSA_MV88E6060) += mv88e6060.o
 obj-$(CONFIG_NET_DSA_QCA8K)	+= qca8k.o
-obj-$(CONFIG_NET_DSA_REALTEK_SMI) += realtek-smi.o
-realtek-smi-objs		:= realtek-smi-core.o rtl8366.o rtl8366rb.o rtl8365mb.o
 obj-$(CONFIG_NET_DSA_SMSC_LAN9303) += lan9303-core.o
 obj-$(CONFIG_NET_DSA_SMSC_LAN9303_I2C) += lan9303_i2c.o
 obj-$(CONFIG_NET_DSA_SMSC_LAN9303_MDIO) += lan9303_mdio.o
@@ -23,5 +21,6 @@ obj-y				+= microchip/
 obj-y				+= mv88e6xxx/
 obj-y				+= ocelot/
 obj-y				+= qca/
+obj-y				+= realtek/
 obj-y				+= sja1105/
 obj-y				+= xrs700x/
drivers/net/dsa/realtek/Kconfig new file +20 -0
new file mode 100644
index 000000000000..1c62212fb0ec
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: GPL-2.0-only
+menuconfig NET_DSA_REALTEK
+	tristate "Realtek Ethernet switch family support"
+	depends on NET_DSA
+	select NET_DSA_TAG_RTL4_A
+	select NET_DSA_TAG_RTL8_4
+	select FIXED_PHY
+	select IRQ_DOMAIN
+	select REALTEK_PHY
+	select REGMAP
+	help
+	  Select to enable support for Realtek Ethernet switch chips.
+
+config NET_DSA_REALTEK_SMI
+	tristate "Realtek SMI connected switch driver"
+	depends on NET_DSA_REALTEK
+	default y
+	help
+	  Select to enable support for registering switches connected
+	  through SMI.
drivers/net/dsa/realtek/Makefile new file +3 -0
new file mode 100644
index 000000000000..323b921bfce0
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_NET_DSA_REALTEK_SMI) 	+= realtek-smi.o
+realtek-smi-objs			:= realtek-smi-core.o rtl8366.o rtl8366rb.o rtl8365mb.o
drivers/net/dsa/realtek/realtek-smi-core.c renamed from drivers/net/dsa/realtek-smi-core.c +0 -0
similarity index 100%
rename from drivers/net/dsa/realtek-smi-core.c
rename to drivers/net/dsa/realtek/realtek-smi-core.c
drivers/net/dsa/realtek/realtek-smi-core.h renamed from drivers/net/dsa/realtek-smi-core.h +0 -0
similarity index 100%
rename from drivers/net/dsa/realtek-smi-core.h
rename to drivers/net/dsa/realtek/realtek-smi-core.h
drivers/net/dsa/realtek/rtl8365mb.c renamed from drivers/net/dsa/rtl8365mb.c +0 -0
similarity index 100%
rename from drivers/net/dsa/rtl8365mb.c
rename to drivers/net/dsa/realtek/rtl8365mb.c
drivers/net/dsa/realtek/rtl8366.c renamed from drivers/net/dsa/rtl8366.c +0 -0
similarity index 100%
rename from drivers/net/dsa/rtl8366.c
rename to drivers/net/dsa/realtek/rtl8366.c
drivers/net/dsa/realtek/rtl8366rb.c renamed from drivers/net/dsa/rtl8366rb.c +0 -0
similarity index 100%
rename from drivers/net/dsa/rtl8366rb.c
rename to drivers/net/dsa/realtek/rtl8366rb.c
Signature · 1 line
2.34.0
author maintainer
#2 lore raw

PATCH v4 2/11 net: dsa: realtek: rename realtek_smi to realtek_priv

In reply to Luiz Angelo Daros de Luca

In preparation to adding other interfaces, the private data structure was renamed to priv. Also, realtek_smi_variant and realtek_smi_ops were renamed to realtek_variant and realtek_ops as those structs are not SMI specific.

---
 drivers/net/dsa/realtek/realtek-smi-core.c    | 316 +++++++-------
 .../realtek/{realtek-smi-core.h => realtek.h} |  68 +--
 drivers/net/dsa/realtek/rtl8365mb.c           | 394 ++++++++---------
 drivers/net/dsa/realtek/rtl8366.c             | 164 +++----
 drivers/net/dsa/realtek/rtl8366rb.c           | 402 +++++++++---------
 5 files changed, 672 insertions(+), 672 deletions(-)
 rename drivers/net/dsa/realtek/{realtek-smi-core.h => realtek.h} (57%)
drivers/net/dsa/realtek/realtek-smi-core.c +158 -158
index aae46ada8d83..7dfd86a99c24 100644
@@ -40,105 +40,105 @@
 #include <linux/bitops.h>
 #include <linux/if_bridge.h>
 
-#include "realtek-smi-core.h"
+#include "realtek.h"
 
 #define REALTEK_SMI_ACK_RETRY_COUNT		5
 #define REALTEK_SMI_HW_STOP_DELAY		25	/* msecs */
 #define REALTEK_SMI_HW_START_DELAY		100	/* msecs */
 
-static inline void realtek_smi_clk_delay(struct realtek_smi *smi)
+static inline void realtek_smi_clk_delay(struct realtek_priv *priv)
 {
-	ndelay(smi->clk_delay);
+	ndelay(priv->clk_delay);
 }
 
-static void realtek_smi_start(struct realtek_smi *smi)
+static void realtek_smi_start(struct realtek_priv *priv)
 {
 	/* Set GPIO pins to output mode, with initial state:
 	 * SCK = 0, SDA = 1
 	 */
-	gpiod_direction_output(smi->mdc, 0);
-	gpiod_direction_output(smi->mdio, 1);
-	realtek_smi_clk_delay(smi);
+	gpiod_direction_output(priv->mdc, 0);
+	gpiod_direction_output(priv->mdio, 1);
+	realtek_smi_clk_delay(priv);
 
 	/* CLK 1: 0 -> 1, 1 -> 0 */
-	gpiod_set_value(smi->mdc, 1);
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdc, 0);
-	realtek_smi_clk_delay(smi);
+	gpiod_set_value(priv->mdc, 1);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdc, 0);
+	realtek_smi_clk_delay(priv);
 
 	/* CLK 2: */
-	gpiod_set_value(smi->mdc, 1);
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdio, 0);
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdc, 0);
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdio, 1);
+	gpiod_set_value(priv->mdc, 1);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdio, 0);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdc, 0);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdio, 1);
 }
 
-static void realtek_smi_stop(struct realtek_smi *smi)
+static void realtek_smi_stop(struct realtek_priv *priv)
 {
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdio, 0);
-	gpiod_set_value(smi->mdc, 1);
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdio, 1);
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdc, 1);
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdc, 0);
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdc, 1);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdio, 0);
+	gpiod_set_value(priv->mdc, 1);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdio, 1);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdc, 1);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdc, 0);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdc, 1);
 
 	/* Add a click */
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdc, 0);
-	realtek_smi_clk_delay(smi);
-	gpiod_set_value(smi->mdc, 1);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdc, 0);
+	realtek_smi_clk_delay(priv);
+	gpiod_set_value(priv->mdc, 1);
 
 	/* Set GPIO pins to input mode */
-	gpiod_direction_input(smi->mdio);
-	gpiod_direction_input(smi->mdc);
+	gpiod_direction_input(priv->mdio);
+	gpiod_direction_input(priv->mdc);
 }
 
-static void realtek_smi_write_bits(struct realtek_smi *smi, u32 data, u32 len)
+static void realtek_smi_write_bits(struct realtek_priv *priv, u32 data, u32 len)
 {
 	for (; len > 0; len--) {
-		realtek_smi_clk_delay(smi);
+		realtek_smi_clk_delay(priv);
 
 		/* Prepare data */
-		gpiod_set_value(smi->mdio, !!(data & (1 << (len - 1))));
-		realtek_smi_clk_delay(smi);
+		gpiod_set_value(priv->mdio, !!(data & (1 << (len - 1))));
+		realtek_smi_clk_delay(priv);
 
 		/* Clocking */
-		gpiod_set_value(smi->mdc, 1);
-		realtek_smi_clk_delay(smi);
-		gpiod_set_value(smi->mdc, 0);
+		gpiod_set_value(priv->mdc, 1);
+		realtek_smi_clk_delay(priv);
+		gpiod_set_value(priv->mdc, 0);
 	}
 }
 
-static void realtek_smi_read_bits(struct realtek_smi *smi, u32 len, u32 *data)
+static void realtek_smi_read_bits(struct realtek_priv *priv, u32 len, u32 *data)
 {
-	gpiod_direction_input(smi->mdio);
+	gpiod_direction_input(priv->mdio);
 
 	for (*data = 0; len > 0; len--) {
 		u32 u;
 
-		realtek_smi_clk_delay(smi);
+		realtek_smi_clk_delay(priv);
 
 		/* Clocking */
-		gpiod_set_value(smi->mdc, 1);
-		realtek_smi_clk_delay(smi);
-		u = !!gpiod_get_value(smi->mdio);
-		gpiod_set_value(smi->mdc, 0);
+		gpiod_set_value(priv->mdc, 1);
+		realtek_smi_clk_delay(priv);
+		u = !!gpiod_get_value(priv->mdio);
+		gpiod_set_value(priv->mdc, 0);
 
 		*data |= (u << (len - 1));
 	}
 
-	gpiod_direction_output(smi->mdio, 0);
+	gpiod_direction_output(priv->mdio, 0);
 }
 
-static int realtek_smi_wait_for_ack(struct realtek_smi *smi)
+static int realtek_smi_wait_for_ack(struct realtek_priv *priv)
 {
 	int retry_cnt;
 
@@ -146,12 +146,12 @@ static int realtek_smi_wait_for_ack(struct realtek_smi *smi)
 	do {
 		u32 ack;
 
-		realtek_smi_read_bits(smi, 1, &ack);
+		realtek_smi_read_bits(priv, 1, &ack);
 		if (ack == 0)
 			break;
 
 		if (++retry_cnt > REALTEK_SMI_ACK_RETRY_COUNT) {
-			dev_err(smi->dev, "ACK timeout\n");
+			dev_err(priv->dev, "ACK timeout\n");
 			return -ETIMEDOUT;
 		}
 	} while (1);
@@ -159,131 +159,131 @@ static int realtek_smi_wait_for_ack(struct realtek_smi *smi)
 	return 0;
 }
 
-static int realtek_smi_write_byte(struct realtek_smi *smi, u8 data)
+static int realtek_smi_write_byte(struct realtek_priv *priv, u8 data)
 {
-	realtek_smi_write_bits(smi, data, 8);
-	return realtek_smi_wait_for_ack(smi);
+	realtek_smi_write_bits(priv, data, 8);
+	return realtek_smi_wait_for_ack(priv);
 }
 
-static int realtek_smi_write_byte_noack(struct realtek_smi *smi, u8 data)
+static int realtek_smi_write_byte_noack(struct realtek_priv *priv, u8 data)
 {
-	realtek_smi_write_bits(smi, data, 8);
+	realtek_smi_write_bits(priv, data, 8);
 	return 0;
 }
 
-static int realtek_smi_read_byte0(struct realtek_smi *smi, u8 *data)
+static int realtek_smi_read_byte0(struct realtek_priv *priv, u8 *data)
 {
 	u32 t;
 
 	/* Read data */
-	realtek_smi_read_bits(smi, 8, &t);
+	realtek_smi_read_bits(priv, 8, &t);
 	*data = (t & 0xff);
 
 	/* Send an ACK */
-	realtek_smi_write_bits(smi, 0x00, 1);
+	realtek_smi_write_bits(priv, 0x00, 1);
 
 	return 0;
 }
 
-static int realtek_smi_read_byte1(struct realtek_smi *smi, u8 *data)
+static int realtek_smi_read_byte1(struct realtek_priv *priv, u8 *data)
 {
 	u32 t;
 
 	/* Read data */
-	realtek_smi_read_bits(smi, 8, &t);
+	realtek_smi_read_bits(priv, 8, &t);
 	*data = (t & 0xff);
 
 	/* Send an ACK */
-	realtek_smi_write_bits(smi, 0x01, 1);
+	realtek_smi_write_bits(priv, 0x01, 1);
 
 	return 0;
 }
 
-static int realtek_smi_read_reg(struct realtek_smi *smi, u32 addr, u32 *data)
+static int realtek_smi_read_reg(struct realtek_priv *priv, u32 addr, u32 *data)
 {
 	unsigned long flags;
 	u8 lo = 0;
 	u8 hi = 0;
 	int ret;
 
-	spin_lock_irqsave(&smi->lock, flags);
+	spin_lock_irqsave(&priv->lock, flags);
 
-	realtek_smi_start(smi);
+	realtek_smi_start(priv);
 
 	/* Send READ command */
-	ret = realtek_smi_write_byte(smi, smi->cmd_read);
+	ret = realtek_smi_write_byte(priv, priv->cmd_read);
 	if (ret)
 		goto out;
 
 	/* Set ADDR[7:0] */
-	ret = realtek_smi_write_byte(smi, addr & 0xff);
+	ret = realtek_smi_write_byte(priv, addr & 0xff);
 	if (ret)
 		goto out;
 
 	/* Set ADDR[15:8] */
-	ret = realtek_smi_write_byte(smi, addr >> 8);
+	ret = realtek_smi_write_byte(priv, addr >> 8);
 	if (ret)
 		goto out;
 
 	/* Read DATA[7:0] */
-	realtek_smi_read_byte0(smi, &lo);
+	realtek_smi_read_byte0(priv, &lo);
 	/* Read DATA[15:8] */
-	realtek_smi_read_byte1(smi, &hi);
+	realtek_smi_read_byte1(priv, &hi);
 
 	*data = ((u32)lo) | (((u32)hi) << 8);
 
 	ret = 0;
 
  out:
-	realtek_smi_stop(smi);
-	spin_unlock_irqrestore(&smi->lock, flags);
+	realtek_smi_stop(priv);
+	spin_unlock_irqrestore(&priv->lock, flags);
 
 	return ret;
 }
 
-static int realtek_smi_write_reg(struct realtek_smi *smi,
+static int realtek_smi_write_reg(struct realtek_priv *priv,
 				 u32 addr, u32 data, bool ack)
 {
 	unsigned long flags;
 	int ret;
 
-	spin_lock_irqsave(&smi->lock, flags);
+	spin_lock_irqsave(&priv->lock, flags);
 
-	realtek_smi_start(smi);
+	realtek_smi_start(priv);
 
 	/* Send WRITE command */
-	ret = realtek_smi_write_byte(smi, smi->cmd_write);
+	ret = realtek_smi_write_byte(priv, priv->cmd_write);
 	if (ret)
 		goto out;
 
 	/* Set ADDR[7:0] */
-	ret = realtek_smi_write_byte(smi, addr & 0xff);
+	ret = realtek_smi_write_byte(priv, addr & 0xff);
 	if (ret)
 		goto out;
 
 	/* Set ADDR[15:8] */
-	ret = realtek_smi_write_byte(smi, addr >> 8);
+	ret = realtek_smi_write_byte(priv, addr >> 8);
 	if (ret)
 		goto out;
 
 	/* Write DATA[7:0] */
-	ret = realtek_smi_write_byte(smi, data & 0xff);
+	ret = realtek_smi_write_byte(priv, data & 0xff);
 	if (ret)
 		goto out;
 
 	/* Write DATA[15:8] */
 	if (ack)
-		ret = realtek_smi_write_byte(smi, data >> 8);
+		ret = realtek_smi_write_byte(priv, data >> 8);
 	else
-		ret = realtek_smi_write_byte_noack(smi, data >> 8);
+		ret = realtek_smi_write_byte_noack(priv, data >> 8);
 	if (ret)
 		goto out;
 
 	ret = 0;
 
  out:
-	realtek_smi_stop(smi);
-	spin_unlock_irqrestore(&smi->lock, flags);
+	realtek_smi_stop(priv);
+	spin_unlock_irqrestore(&priv->lock, flags);
 
 	return ret;
 }
@@ -292,10 +292,10 @@ static int realtek_smi_write_reg(struct realtek_smi *smi,
  * is when issueing soft reset. Since the device reset as soon as we write
  * that bit, no ACK will come back for natural reasons.
  */
-int realtek_smi_write_reg_noack(struct realtek_smi *smi, u32 addr,
+int realtek_smi_write_reg_noack(struct realtek_priv *priv, u32 addr,
 				u32 data)
 {
-	return realtek_smi_write_reg(smi, addr, data, false);
+	return realtek_smi_write_reg(priv, addr, data, false);
 }
 EXPORT_SYMBOL_GPL(realtek_smi_write_reg_noack);
 
@@ -303,16 +303,16 @@ EXPORT_SYMBOL_GPL(realtek_smi_write_reg_noack);
 
 static int realtek_smi_write(void *ctx, u32 reg, u32 val)
 {
-	struct realtek_smi *smi = ctx;
+	struct realtek_priv *priv = ctx;
 
-	return realtek_smi_write_reg(smi, reg, val, true);
+	return realtek_smi_write_reg(priv, reg, val, true);
 }
 
 static int realtek_smi_read(void *ctx, u32 reg, u32 *val)
 {
-	struct realtek_smi *smi = ctx;
+	struct realtek_priv *priv = ctx;
 
-	return realtek_smi_read_reg(smi, reg, val);
+	return realtek_smi_read_reg(priv, reg, val);
 }
 
 static const struct regmap_config realtek_smi_mdio_regmap_config = {
@@ -329,49 +329,49 @@ static const struct regmap_config realtek_smi_mdio_regmap_config = {
 
 static int realtek_smi_mdio_read(struct mii_bus *bus, int addr, int regnum)
 {
-	struct realtek_smi *smi = bus->priv;
+	struct realtek_priv *priv = bus->priv;
 
-	return smi->ops->phy_read(smi, addr, regnum);
+	return priv->ops->phy_read(priv, addr, regnum);
 }
 
 static int realtek_smi_mdio_write(struct mii_bus *bus, int addr, int regnum,
 				  u16 val)
 {
-	struct realtek_smi *smi = bus->priv;
+	struct realtek_priv *priv = bus->priv;
 
-	return smi->ops->phy_write(smi, addr, regnum, val);
+	return priv->ops->phy_write(priv, addr, regnum, val);
 }
 
-int realtek_smi_setup_mdio(struct realtek_smi *smi)
+int realtek_smi_setup_mdio(struct realtek_priv *priv)
 {
 	struct device_node *mdio_np;
 	int ret;
 
-	mdio_np = of_get_compatible_child(smi->dev->of_node, "realtek,smi-mdio");
+	mdio_np = of_get_compatible_child(priv->dev->of_node, "realtek,smi-mdio");
 	if (!mdio_np) {
-		dev_err(smi->dev, "no MDIO bus node\n");
+		dev_err(priv->dev, "no MDIO bus node\n");
 		return -ENODEV;
 	}
 
-	smi->slave_mii_bus = devm_mdiobus_alloc(smi->dev);
-	if (!smi->slave_mii_bus) {
+	priv->slave_mii_bus = devm_mdiobus_alloc(priv->dev);
+	if (!priv->slave_mii_bus) {
 		ret = -ENOMEM;
 		goto err_put_node;
 	}
-	smi->slave_mii_bus->priv = smi;
-	smi->slave_mii_bus->name = "SMI slave MII";
-	smi->slave_mii_bus->read = realtek_smi_mdio_read;
-	smi->slave_mii_bus->write = realtek_smi_mdio_write;
-	snprintf(smi->slave_mii_bus->id, MII_BUS_ID_SIZE, "SMI-%d",
-		 smi->ds->index);
-	smi->slave_mii_bus->dev.of_node = mdio_np;
-	smi->slave_mii_bus->parent = smi->dev;
-	smi->ds->slave_mii_bus = smi->slave_mii_bus;
-
-	ret = devm_of_mdiobus_register(smi->dev, smi->slave_mii_bus, mdio_np);
+	priv->slave_mii_bus->priv = priv;
+	priv->slave_mii_bus->name = "SMI slave MII";
+	priv->slave_mii_bus->read = realtek_smi_mdio_read;
+	priv->slave_mii_bus->write = realtek_smi_mdio_write;
+	snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "SMI-%d",
+		 priv->ds->index);
+	priv->slave_mii_bus->dev.of_node = mdio_np;
+	priv->slave_mii_bus->parent = priv->dev;
+	priv->ds->slave_mii_bus = priv->slave_mii_bus;
+
+	ret = devm_of_mdiobus_register(priv->dev, priv->slave_mii_bus, mdio_np);
 	if (ret) {
-		dev_err(smi->dev, "unable to register MDIO bus %s\n",
-			smi->slave_mii_bus->id);
+		dev_err(priv->dev, "unable to register MDIO bus %s\n",
+			priv->slave_mii_bus->id);
 		goto err_put_node;
 	}
 
@@ -385,76 +385,76 @@ int realtek_smi_setup_mdio(struct realtek_smi *smi)
 
 static int realtek_smi_probe(struct platform_device *pdev)
 {
-	const struct realtek_smi_variant *var;
+	const struct realtek_variant *var;
 	struct device *dev = &pdev->dev;
-	struct realtek_smi *smi;
+	struct realtek_priv *priv;
 	struct device_node *np;
 	int ret;
 
 	var = of_device_get_match_data(dev);
 	np = dev->of_node;
 
-	smi = devm_kzalloc(dev, sizeof(*smi) + var->chip_data_sz, GFP_KERNEL);
-	if (!smi)
+	priv = devm_kzalloc(dev, sizeof(*priv) + var->chip_data_sz, GFP_KERNEL);
+	if (!priv)
 		return -ENOMEM;
-	smi->chip_data = (void *)smi + sizeof(*smi);
-	smi->map = devm_regmap_init(dev, NULL, smi,
-				    &realtek_smi_mdio_regmap_config);
-	if (IS_ERR(smi->map)) {
-		ret = PTR_ERR(smi->map);
+	priv->chip_data = (void *)priv + sizeof(*priv);
+	priv->map = devm_regmap_init(dev, NULL, priv,
+				     &realtek_smi_mdio_regmap_config);
+	if (IS_ERR(priv->map)) {
+		ret = PTR_ERR(priv->map);
 		dev_err(dev, "regmap init failed: %d\n", ret);
 		return ret;
 	}
 
 	/* Link forward and backward */
-	smi->dev = dev;
-	smi->clk_delay = var->clk_delay;
-	smi->cmd_read = var->cmd_read;
-	smi->cmd_write = var->cmd_write;
-	smi->ops = var->ops;
+	priv->dev = dev;
+	priv->clk_delay = var->clk_delay;
+	priv->cmd_read = var->cmd_read;
+	priv->cmd_write = var->cmd_write;
+	priv->ops = var->ops;
 
-	dev_set_drvdata(dev, smi);
-	spin_lock_init(&smi->lock);
+	dev_set_drvdata(dev, priv);
+	spin_lock_init(&priv->lock);
 
 	/* TODO: if power is software controlled, set up any regulators here */
 
 	/* Assert then deassert RESET */
-	smi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
-	if (IS_ERR(smi->reset)) {
+	priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
+	if (IS_ERR(priv->reset)) {
 		dev_err(dev, "failed to get RESET GPIO\n");
-		return PTR_ERR(smi->reset);
+		return PTR_ERR(priv->reset);
 	}
 	msleep(REALTEK_SMI_HW_STOP_DELAY);
-	gpiod_set_value(smi->reset, 0);
+	gpiod_set_value(priv->reset, 0);
 	msleep(REALTEK_SMI_HW_START_DELAY);
 	dev_info(dev, "deasserted RESET\n");
 
 	/* Fetch MDIO pins */
-	smi->mdc = devm_gpiod_get_optional(dev, "mdc", GPIOD_OUT_LOW);
-	if (IS_ERR(smi->mdc))
-		return PTR_ERR(smi->mdc);
-	smi->mdio = devm_gpiod_get_optional(dev, "mdio", GPIOD_OUT_LOW);
-	if (IS_ERR(smi->mdio))
-		return PTR_ERR(smi->mdio);
+	priv->mdc = devm_gpiod_get_optional(dev, "mdc", GPIOD_OUT_LOW);
+	if (IS_ERR(priv->mdc))
+		return PTR_ERR(priv->mdc);
+	priv->mdio = devm_gpiod_get_optional(dev, "mdio", GPIOD_OUT_LOW);
+	if (IS_ERR(priv->mdio))
+		return PTR_ERR(priv->mdio);
 
-	smi->leds_disabled = of_property_read_bool(np, "realtek,disable-leds");
+	priv->leds_disabled = of_property_read_bool(np, "realtek,disable-leds");
 
-	ret = smi->ops->detect(smi);
+	ret = priv->ops->detect(priv);
 	if (ret) {
 		dev_err(dev, "unable to detect switch\n");
 		return ret;
 	}
 
-	smi->ds = devm_kzalloc(dev, sizeof(*smi->ds), GFP_KERNEL);
-	if (!smi->ds)
+	priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
+	if (!priv->ds)
 		return -ENOMEM;
 
-	smi->ds->dev = dev;
-	smi->ds->num_ports = smi->num_ports;
-	smi->ds->priv = smi;
+	priv->ds->dev = dev;
+	priv->ds->num_ports = priv->num_ports;
+	priv->ds->priv = priv;
 
-	smi->ds->ops = var->ds_ops;
-	ret = dsa_register_switch(smi->ds);
+	priv->ds->ops = var->ds_ops;
+	ret = dsa_register_switch(priv->ds);
 	if (ret) {
 		dev_err_probe(dev, ret, "unable to register switch\n");
 		return ret;
@@ -464,15 +464,15 @@ static int realtek_smi_probe(struct platform_device *pdev)
 
 static int realtek_smi_remove(struct platform_device *pdev)
 {
-	struct realtek_smi *smi = platform_get_drvdata(pdev);
+	struct realtek_priv *priv = platform_get_drvdata(pdev);
 
-	if (!smi)
+	if (!priv)
 		return 0;
 
-	dsa_unregister_switch(smi->ds);
-	if (smi->slave_mii_bus)
-		of_node_put(smi->slave_mii_bus->dev.of_node);
-	gpiod_set_value(smi->reset, 1);
+	dsa_unregister_switch(priv->ds);
+	if (priv->slave_mii_bus)
+		of_node_put(priv->slave_mii_bus->dev.of_node);
+	gpiod_set_value(priv->reset, 1);
 
 	platform_set_drvdata(pdev, NULL);
 
@@ -481,12 +481,12 @@ static int realtek_smi_remove(struct platform_device *pdev)
 
 static void realtek_smi_shutdown(struct platform_device *pdev)
 {
-	struct realtek_smi *smi = platform_get_drvdata(pdev);
+	struct realtek_priv *priv = platform_get_drvdata(pdev);
 
-	if (!smi)
+	if (!priv)
 		return;
 
-	dsa_switch_shutdown(smi->ds);
+	dsa_switch_shutdown(priv->ds);
 
 	platform_set_drvdata(pdev, NULL);
 }
drivers/net/dsa/realtek/realtek.h renamed from drivers/net/dsa/realtek/realtek-smi-core.h +34 -34

121 lines not on this page · raw message

drivers/net/dsa/realtek/rtl8365mb.c +197 -197

1,160 lines not on this page · raw message

drivers/net/dsa/realtek/rtl8366.c +82 -82

452 lines not on this page · raw message

drivers/net/dsa/realtek/rtl8366rb.c +201 -201

683 lines not on this page · raw message

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

Signature · 1 line
2.34.0

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

maintainer
#3 lore raw

Re: [PATCH net-next v4 02/11] net: dsa: realtek: rename realtek_smi to realtek_priv

Quoted 4 lines · On Wed, 5 Jan 2022 00:15:06 -0300 Luiz Angelo Daros de Luca wrote:

/**
- * struct realtek_smi_ops - vtable for the per-SMI-chiptype operations
+ * struct realtek_ops - vtable for the per-SMI-chiptype operations
* @detect: detects the chiptype

This kdoc is missing decriptions for a lot of members.

Would you mind adding a patch to the front of the series to resolve all kdoc issues in realtek-smi-core.h AKA realtek.h so that we don't get the errors re-reported each time the file is renamed in the series? Since the kdoc doesn't add much here I think you can just replace the /** with a /* for now, or remove it completely.

Quoted 46 lines

*/

drivers/net/dsa/realtek/realtek.h renamed from drivers/net/dsa/realtek/realtek-smi-core.h +18 -18
-struct realtek_smi_ops {
-	int	(*detect)(struct realtek_smi *smi);
-	int	(*reset_chip)(struct realtek_smi *smi);
-	int	(*setup)(struct realtek_smi *smi);
-	void	(*cleanup)(struct realtek_smi *smi);
-	int	(*get_mib_counter)(struct realtek_smi *smi,
+struct realtek_ops {
+	int	(*detect)(struct realtek_priv *priv);
+	int	(*reset_chip)(struct realtek_priv *priv);
+	int	(*setup)(struct realtek_priv *priv);
+	void	(*cleanup)(struct realtek_priv *priv);
+	int	(*get_mib_counter)(struct realtek_priv *priv,
 				   int port,
 				   struct rtl8366_mib_counter *mib,
 				   u64 *mibvalue);
-	int	(*get_vlan_mc)(struct realtek_smi *smi, u32 index,
+	int	(*get_vlan_mc)(struct realtek_priv *priv, u32 index,
 			       struct rtl8366_vlan_mc *vlanmc);
-	int	(*set_vlan_mc)(struct realtek_smi *smi, u32 index,
+	int	(*set_vlan_mc)(struct realtek_priv *priv, u32 index,
 			       const struct rtl8366_vlan_mc *vlanmc);
-	int	(*get_vlan_4k)(struct realtek_smi *smi, u32 vid,
+	int	(*get_vlan_4k)(struct realtek_priv *priv, u32 vid,
 			       struct rtl8366_vlan_4k *vlan4k);
-	int	(*set_vlan_4k)(struct realtek_smi *smi,
+	int	(*set_vlan_4k)(struct realtek_priv *priv,
 			       const struct rtl8366_vlan_4k *vlan4k);
-	int	(*get_mc_index)(struct realtek_smi *smi, int port, int *val);
-	int	(*set_mc_index)(struct realtek_smi *smi, int port, int index);
-	bool	(*is_vlan_valid)(struct realtek_smi *smi, unsigned int vlan);
-	int	(*enable_vlan)(struct realtek_smi *smi, bool enable);
-	int	(*enable_vlan4k)(struct realtek_smi *smi, bool enable);
-	int	(*enable_port)(struct realtek_smi *smi, int port, bool enable);
-	int	(*phy_read)(struct realtek_smi *smi, int phy, int regnum);
-	int	(*phy_write)(struct realtek_smi *smi, int phy, int regnum,
+	int	(*get_mc_index)(struct realtek_priv *priv, int port, int *val);
+	int	(*set_mc_index)(struct realtek_priv *priv, int port, int index);
+	bool	(*is_vlan_valid)(struct realtek_priv *priv, unsigned int vlan);
+	int	(*enable_vlan)(struct realtek_priv *priv, bool enable);
+	int	(*enable_vlan4k)(struct realtek_priv *priv, bool enable);
+	int	(*enable_port)(struct realtek_priv *priv, int port, bool enable);
+	int	(*phy_read)(struct realtek_priv *priv, int phy, int regnum);
+	int	(*phy_write)(struct realtek_priv *priv, int phy, int regnum,
 			     u16 val);
 };
Reviewed-by on v4 2/11
#4 lore raw

Re: [PATCH net-next v4 02/11] net: dsa: realtek: rename realtek_smi to realtek_priv

In reply to Luiz Angelo Daros de Luca
Quoted 8 lines · Luiz Angelo Daros de Luca

In preparation to adding other interfaces, the private data structure was renamed to priv. Also, realtek_smi_variant and realtek_smi_ops were renamed to realtek_variant and realtek_ops as those structs are not SMI specific.

Quoted 8 lines
---
 drivers/net/dsa/realtek/realtek-smi-core.c    | 316 +++++++-------
 .../realtek/{realtek-smi-core.h => realtek.h} |  68 +--
 drivers/net/dsa/realtek/rtl8365mb.c           | 394 ++++++++---------
 drivers/net/dsa/realtek/rtl8366.c             | 164 +++----
 drivers/net/dsa/realtek/rtl8366rb.c           | 402 +++++++++---------
 5 files changed, 672 insertions(+), 672 deletions(-)
 rename drivers/net/dsa/realtek/{realtek-smi-core.h => realtek.h} (57%)
Reviewed-by on v4 2/11
#5 lore raw

Re: [PATCH net-next v4 02/11] net: dsa: realtek: rename realtek_smi to realtek_priv

In reply to Luiz Angelo Daros de Luca
Quoted 8 lines · Luiz Angelo Daros de Luca

In preparation to adding other interfaces, the private data structure was renamed to priv. Also, realtek_smi_variant and realtek_smi_ops were renamed to realtek_variant and realtek_ops as those structs are not SMI specific.

Fine by me:

Yours,
Linus Walleij

maintainer Reviewed-by on v4 2/11
#6 lore raw

Re: [PATCH net-next v4 02/11] net: dsa: realtek: rename realtek_smi to realtek_priv

In reply to Luiz Angelo Daros de Luca
Quoted 9 lines · Luiz Angelo Daros de Luca

In preparation to adding other interfaces, the private data structure was renamed to priv. Also, realtek_smi_variant and realtek_smi_ops were renamed to realtek_variant and realtek_ops as those structs are not SMI specific.

---

author maintainer
#7 lore raw

PATCH v4 3/11 net: dsa: realtek: remove direct calls to realtek-smi

In reply to Luiz Angelo Daros de Luca

Remove the only two direct calls from subdrivers to realtek-smi. Now they are called from realtek_priv. Subdrivers can now be linked independently from realtek-smi.

---
 drivers/net/dsa/realtek/realtek-smi-core.c | 15 +++++++++------
 drivers/net/dsa/realtek/realtek.h          |  7 ++-----
 drivers/net/dsa/realtek/rtl8365mb.c        | 12 +++++++-----
 drivers/net/dsa/realtek/rtl8366rb.c        | 12 +++++++-----
 4 files changed, 25 insertions(+), 21 deletions(-)
drivers/net/dsa/realtek/realtek-smi-core.c +9 -6
index 7dfd86a99c24..a917801385c9 100644
@@ -292,12 +292,11 @@ static int realtek_smi_write_reg(struct realtek_priv *priv,
  * is when issueing soft reset. Since the device reset as soon as we write
  * that bit, no ACK will come back for natural reasons.
  */
-int realtek_smi_write_reg_noack(struct realtek_priv *priv, u32 addr,
-				u32 data)
+static int realtek_smi_write_reg_noack(struct realtek_priv *priv, u32 addr,
+				       u32 data)
 {
 	return realtek_smi_write_reg(priv, addr, data, false);
 }
-EXPORT_SYMBOL_GPL(realtek_smi_write_reg_noack);
 
 /* Regmap accessors */
 
@@ -342,8 +341,9 @@ static int realtek_smi_mdio_write(struct mii_bus *bus, int addr, int regnum,
 	return priv->ops->phy_write(priv, addr, regnum, val);
 }
 
-int realtek_smi_setup_mdio(struct realtek_priv *priv)
+static int realtek_smi_setup_mdio(struct dsa_switch *ds)
 {
+	struct realtek_priv *priv =  ds->priv;
 	struct device_node *mdio_np;
 	int ret;
 
@@ -363,10 +363,10 @@ int realtek_smi_setup_mdio(struct realtek_priv *priv)
 	priv->slave_mii_bus->read = realtek_smi_mdio_read;
 	priv->slave_mii_bus->write = realtek_smi_mdio_write;
 	snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "SMI-%d",
-		 priv->ds->index);
+		 ds->index);
 	priv->slave_mii_bus->dev.of_node = mdio_np;
 	priv->slave_mii_bus->parent = priv->dev;
-	priv->ds->slave_mii_bus = priv->slave_mii_bus;
+	ds->slave_mii_bus = priv->slave_mii_bus;
 
 	ret = devm_of_mdiobus_register(priv->dev, priv->slave_mii_bus, mdio_np);
 	if (ret) {
@@ -413,6 +413,9 @@ static int realtek_smi_probe(struct platform_device *pdev)
 	priv->cmd_write = var->cmd_write;
 	priv->ops = var->ops;
 
+	priv->setup_interface = realtek_smi_setup_mdio;
+	priv->write_reg_noack = realtek_smi_write_reg_noack;
+
 	dev_set_drvdata(dev, priv);
 	spin_lock_init(&priv->lock);
 
drivers/net/dsa/realtek/realtek.h +2 -5
index 177fff90b8a6..58814de563a2 100644
@@ -66,6 +66,8 @@ struct realtek_priv {
 	struct rtl8366_mib_counter *mib_counters;
 
 	const struct realtek_ops *ops;
+	int			(*setup_interface)(struct dsa_switch *ds);
+	int			(*write_reg_noack)(struct realtek_priv *priv, u32 addr, u32 data);
 
 	int			vlan_enabled;
 	int			vlan4k_enabled;
@@ -115,11 +117,6 @@ struct realtek_variant {
 	size_t chip_data_sz;
 };
 
-/* SMI core calls */
-int realtek_smi_write_reg_noack(struct realtek_priv *priv, u32 addr,
-				u32 data);
-int realtek_smi_setup_mdio(struct realtek_priv *priv);
-
 /* RTL8366 library helpers */
 int rtl8366_mc_is_used(struct realtek_priv *priv, int mc_index, int *used);
 int rtl8366_set_vlan(struct realtek_priv *priv, int vid, u32 member,
drivers/net/dsa/realtek/rtl8365mb.c +7 -5
index 6b8797ba80c6..5fb453b5f650 100644
@@ -1767,7 +1767,7 @@ static int rtl8365mb_reset_chip(struct realtek_priv *priv)
 {
 	u32 val;
 
-	realtek_smi_write_reg_noack(priv, RTL8365MB_CHIP_RESET_REG,
+	priv->write_reg_noack(priv, RTL8365MB_CHIP_RESET_REG,
 				    FIELD_PREP(RTL8365MB_CHIP_RESET_HW_MASK,
 					       1));
 
@@ -1849,10 +1849,12 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 	if (ret)
 		goto out_teardown_irq;
 
-	ret = realtek_smi_setup_mdio(priv);
-	if (ret) {
-		dev_err(priv->dev, "could not set up MDIO bus\n");
-		goto out_teardown_irq;
+	if (priv->setup_interface) {
+		ret = priv->setup_interface(ds);
+		if (ret) {
+			dev_err(priv->dev, "could not set up MDIO bus\n");
+			goto out_teardown_irq;
+		}
 	}
 
 	/* Start statistics counter polling */
drivers/net/dsa/realtek/rtl8366rb.c +7 -5
index 241e6b6ebea5..34e371084d6d 100644
@@ -1030,10 +1030,12 @@ static int rtl8366rb_setup(struct dsa_switch *ds)
 	if (ret)
 		dev_info(priv->dev, "no interrupt support\n");
 
-	ret = realtek_smi_setup_mdio(priv);
-	if (ret) {
-		dev_info(priv->dev, "could not set up MDIO bus\n");
-		return -ENODEV;
+	if (priv->setup_interface) {
+		ret = priv->setup_interface(ds);
+		if (ret) {
+			dev_err(priv->dev, "could not set up MDIO bus\n");
+			return -ENODEV;
+		}
 	}
 
 	return 0;
@@ -1705,7 +1707,7 @@ static int rtl8366rb_reset_chip(struct realtek_priv *priv)
 	u32 val;
 	int ret;
 
-	realtek_smi_write_reg_noack(priv, RTL8366RB_RESET_CTRL_REG,
+	priv->write_reg_noack(priv, RTL8366RB_RESET_CTRL_REG,
 				    RTL8366RB_CHIP_CTRL_RESET_HW);
 	do {
 		usleep_range(20000, 25000);
Signature · 1 line
2.34.0
Reviewed-by on v4 3/11
#9 lore raw

Re: [PATCH net-next v4 03/11] net: dsa: realtek: remove direct calls to realtek-smi

In reply to Luiz Angelo Daros de Luca
Quoted 5 lines · Luiz Angelo Daros de Luca

Remove the only two direct calls from subdrivers to realtek-smi. Now they are called from realtek_priv. Subdrivers can now be linked independently from realtek-smi.

Yours,
Linus Walleij

Reviewed-by on v4 3/11
#10 lore raw

Re: [PATCH net-next v4 03/11] net: dsa: realtek: remove direct calls to realtek-smi

In reply to Luiz Angelo Daros de Luca
Quoted 6 lines · Luiz Angelo Daros de Luca

Remove the only two direct calls from subdrivers to realtek-smi. Now they are called from realtek_priv. Subdrivers can now be linked independently from realtek-smi.

---

[snip]

Just a nit below.

drivers/net/dsa/realtek/rtl8366rb.c static int rtl8366rb_reset_chip(struct realtek_priv *priv) · line 1711

Quoted 8 lines

return 0;

drivers/net/dsa/realtek/rtl8366rb.c +1 -1
@@ -1705,7 +1707,7 @@ static int rtl8366rb_reset_chip(struct realtek_priv *priv)
  	u32 val;
  	int ret;
 
-	realtek_smi_write_reg_noack(priv, RTL8366RB_RESET_CTRL_REG,
+	priv->write_reg_noack(priv, RTL8366RB_RESET_CTRL_REG,
  				    RTL8366RB_CHIP_CTRL_RESET_HW);

Only if you need to spin a new version, the second line should start at the character after the opening parenthesis on the previous line.

Signature · 1 line
Florian
author maintainer
#11 lore raw

PATCH v4 4/11 net: dsa: realtek: convert subdrivers into modules

In reply to Luiz Angelo Daros de Luca

Preparing for multiple interfaces support, the drivers must be independent of realtek-smi.

---
 drivers/net/dsa/realtek/Kconfig               | 20 +++++++++++++++++--
 drivers/net/dsa/realtek/Makefile              |  4 +++-
 .../{realtek-smi-core.c => realtek-smi.c}     |  6 ++++++
 drivers/net/dsa/realtek/rtl8365mb.c           |  4 ++++
 .../dsa/realtek/{rtl8366.c => rtl8366-core.c} |  0
 drivers/net/dsa/realtek/rtl8366rb.c           |  4 ++++
 6 files changed, 35 insertions(+), 3 deletions(-)
 rename drivers/net/dsa/realtek/{realtek-smi-core.c => realtek-smi.c} (97%)
 rename drivers/net/dsa/realtek/{rtl8366.c => rtl8366-core.c} (100%)
drivers/net/dsa/realtek/Kconfig +18 -2
index 1c62212fb0ec..cd1aa95b7bf0 100644
@@ -2,8 +2,6 @@
 menuconfig NET_DSA_REALTEK
 	tristate "Realtek Ethernet switch family support"
 	depends on NET_DSA
-	select NET_DSA_TAG_RTL4_A
-	select NET_DSA_TAG_RTL8_4
 	select FIXED_PHY
 	select IRQ_DOMAIN
 	select REALTEK_PHY
@@ -18,3 +16,21 @@ config NET_DSA_REALTEK_SMI
 	help
 	  Select to enable support for registering switches connected
 	  through SMI.
+
+config NET_DSA_REALTEK_RTL8365MB
+	tristate "Realtek RTL8365MB switch subdriver"
+	default y
+	depends on NET_DSA_REALTEK
+	depends on NET_DSA_REALTEK_SMI
+	select NET_DSA_TAG_RTL8_4
+	help
+	  Select to enable support for Realtek RTL8365MB
+
+config NET_DSA_REALTEK_RTL8366RB
+	tristate "Realtek RTL8366RB switch subdriver"
+	default y
+	depends on NET_DSA_REALTEK
+	depends on NET_DSA_REALTEK_SMI
+	select NET_DSA_TAG_RTL4_A
+	help
+	  Select to enable support for Realtek RTL8366RB
drivers/net/dsa/realtek/Makefile +3 -1
index 323b921bfce0..8b5a4abcedd3 100644
@@ -1,3 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_NET_DSA_REALTEK_SMI) 	+= realtek-smi.o
-realtek-smi-objs			:= realtek-smi-core.o rtl8366.o rtl8366rb.o rtl8365mb.o
+obj-$(CONFIG_NET_DSA_REALTEK_RTL8366RB) += rtl8366.o
+rtl8366-objs 				:= rtl8366-core.o rtl8366rb.o
+obj-$(CONFIG_NET_DSA_REALTEK_RTL8365MB) += rtl8365mb.o
drivers/net/dsa/realtek/realtek-smi.c renamed from drivers/net/dsa/realtek/realtek-smi-core.c +6 -0
similarity index 97%
rename from drivers/net/dsa/realtek/realtek-smi-core.c
rename to drivers/net/dsa/realtek/realtek-smi.c
index a917801385c9..5514fe81d64f 100644
@@ -495,19 +495,23 @@ static void realtek_smi_shutdown(struct platform_device *pdev)
 }
 
 static const struct of_device_id realtek_smi_of_match[] = {
+#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8366RB)
 	{
 		.compatible = "realtek,rtl8366rb",
 		.data = &rtl8366rb_variant,
 	},
+#endif
 	{
 		/* FIXME: add support for RTL8366S and more */
 		.compatible = "realtek,rtl8366s",
 		.data = NULL,
 	},
+#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8365MB)
 	{
 		.compatible = "realtek,rtl8365mb",
 		.data = &rtl8365mb_variant,
 	},
+#endif
 	{ /* sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, realtek_smi_of_match);
@@ -523,4 +527,6 @@ static struct platform_driver realtek_smi_driver = {
 };
 module_platform_driver(realtek_smi_driver);
 
+MODULE_AUTHOR("Linus Walleij <[email protected]>");
+MODULE_DESCRIPTION("Driver for Realtek ethernet switch connected via SMI interface");
 MODULE_LICENSE("GPL");
drivers/net/dsa/realtek/rtl8365mb.c +4 -0
index 5fb453b5f650..b52bb987027c 100644
@@ -1987,3 +1987,7 @@ const struct realtek_variant rtl8365mb_variant = {
 	.chip_data_sz = sizeof(struct rtl8365mb),
 };
 EXPORT_SYMBOL_GPL(rtl8365mb_variant);
+
+MODULE_AUTHOR("Alvin Šipraga <[email protected]>");
+MODULE_DESCRIPTION("Driver for RTL8365MB-VC ethernet switch");
+MODULE_LICENSE("GPL");
drivers/net/dsa/realtek/rtl8366-core.c renamed from drivers/net/dsa/realtek/rtl8366.c +0 -0
similarity index 100%
rename from drivers/net/dsa/realtek/rtl8366.c
rename to drivers/net/dsa/realtek/rtl8366-core.c
drivers/net/dsa/realtek/rtl8366rb.c +4 -0
index 34e371084d6d..ff607608dead 100644
@@ -1814,3 +1814,7 @@ const struct realtek_variant rtl8366rb_variant = {
 	.chip_data_sz = sizeof(struct rtl8366rb),
 };
 EXPORT_SYMBOL_GPL(rtl8366rb_variant);
+
+MODULE_AUTHOR("Linus Walleij <[email protected]>");
+MODULE_DESCRIPTION("Driver for RTL8366RB ethernet switch");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.34.0
Reviewed-by on v4 4/11
#12 lore raw

Re: [PATCH net-next v4 04/11] net: dsa: realtek: convert subdrivers into modules

Quoted 6 lines · Luiz Angelo Daros de Luca

Preparing for multiple interfaces support, the drivers must be independent of realtek-smi.

Quoted 69 lines
---
 drivers/net/dsa/realtek/Kconfig               | 20 +++++++++++++++++--
 drivers/net/dsa/realtek/Makefile              |  4 +++-
 .../{realtek-smi-core.c => realtek-smi.c}     |  6 ++++++
 drivers/net/dsa/realtek/rtl8365mb.c           |  4 ++++
 .../dsa/realtek/{rtl8366.c => rtl8366-core.c} |  0
 drivers/net/dsa/realtek/rtl8366rb.c           |  4 ++++
 6 files changed, 35 insertions(+), 3 deletions(-)
 rename drivers/net/dsa/realtek/{realtek-smi-core.c => realtek-smi.c} (97%)
 rename drivers/net/dsa/realtek/{rtl8366.c => rtl8366-core.c} (100%)
drivers/net/dsa/realtek/Kconfig +18 -2
index 1c62212fb0ec..cd1aa95b7bf0 100644
@@ -2,8 +2,6 @@
 menuconfig NET_DSA_REALTEK
 	tristate "Realtek Ethernet switch family support"
 	depends on NET_DSA
-	select NET_DSA_TAG_RTL4_A
-	select NET_DSA_TAG_RTL8_4
 	select FIXED_PHY
 	select IRQ_DOMAIN
 	select REALTEK_PHY
@@ -18,3 +16,21 @@ config NET_DSA_REALTEK_SMI
 	help
 	  Select to enable support for registering switches connected
 	  through SMI.
+
+config NET_DSA_REALTEK_RTL8365MB
+	tristate "Realtek RTL8365MB switch subdriver"
+	default y
+	depends on NET_DSA_REALTEK
+	depends on NET_DSA_REALTEK_SMI
+	select NET_DSA_TAG_RTL8_4
+	help
+	  Select to enable support for Realtek RTL8365MB
+
+config NET_DSA_REALTEK_RTL8366RB
+	tristate "Realtek RTL8366RB switch subdriver"
+	default y
+	depends on NET_DSA_REALTEK
+	depends on NET_DSA_REALTEK_SMI
+	select NET_DSA_TAG_RTL4_A
+	help
+	  Select to enable support for Realtek RTL8366RB
drivers/net/dsa/realtek/Makefile +3 -1
index 323b921bfce0..8b5a4abcedd3 100644
@@ -1,3 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_NET_DSA_REALTEK_SMI) 	+= realtek-smi.o
-realtek-smi-objs			:= realtek-smi-core.o rtl8366.o rtl8366rb.o rtl8365mb.o
+obj-$(CONFIG_NET_DSA_REALTEK_RTL8366RB) += rtl8366.o
+rtl8366-objs 				:= rtl8366-core.o rtl8366rb.o
+obj-$(CONFIG_NET_DSA_REALTEK_RTL8365MB) += rtl8365mb.o
drivers/net/dsa/realtek/realtek-smi.c renamed from drivers/net/dsa/realtek/realtek-smi-core.c +1 -0
similarity index 97%
rename from drivers/net/dsa/realtek/realtek-smi-core.c
rename to drivers/net/dsa/realtek/realtek-smi.c
index a917801385c9..5514fe81d64f 100644
@@ -495,19 +495,23 @@ static void realtek_smi_shutdown(struct platform_device *pdev)
 }
 
 static const struct of_device_id realtek_smi_of_match[] = {
+#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8366RB)

drivers/net/dsa/realtek/realtek-smi.c static void realtek_smi_shutdown(struct platform_device *pdev) · line 508

Quoted 10 lines
drivers/net/dsa/realtek/realtek-smi.c renamed from drivers/net/dsa/realtek/realtek-smi-core.c +1 -0
 	{
 		.compatible = "realtek,rtl8366rb",
 		.data = &rtl8366rb_variant,
 	},
+#endif
 	{
 		/* FIXME: add support for RTL8366S and more */
 		.compatible = "realtek,rtl8366s",
 		.data = NULL,
 	},

If you send a v5, maybe add a patch to remove this. It is going to crash anyway so I am sure nobody uses it.

Quoted 44 lines
drivers/net/dsa/realtek/realtek-smi.c renamed from drivers/net/dsa/realtek/realtek-smi-core.c +4 -0
+#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8365MB)
 	{
 		.compatible = "realtek,rtl8365mb",
 		.data = &rtl8365mb_variant,
 	},
+#endif
 	{ /* sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, realtek_smi_of_match);
@@ -523,4 +527,6 @@ static struct platform_driver realtek_smi_driver = {
 };
 module_platform_driver(realtek_smi_driver);
 
+MODULE_AUTHOR("Linus Walleij <[email protected]>");
+MODULE_DESCRIPTION("Driver for Realtek ethernet switch connected via SMI interface");
 MODULE_LICENSE("GPL");
drivers/net/dsa/realtek/rtl8365mb.c +4 -0
index 5fb453b5f650..b52bb987027c 100644
@@ -1987,3 +1987,7 @@ const struct realtek_variant rtl8365mb_variant = {
 	.chip_data_sz = sizeof(struct rtl8365mb),
 };
 EXPORT_SYMBOL_GPL(rtl8365mb_variant);
+
+MODULE_AUTHOR("Alvin Šipraga <[email protected]>");
+MODULE_DESCRIPTION("Driver for RTL8365MB-VC ethernet switch");
+MODULE_LICENSE("GPL");
drivers/net/dsa/realtek/rtl8366-core.c renamed from drivers/net/dsa/realtek/rtl8366.c +0 -0
similarity index 100%
rename from drivers/net/dsa/realtek/rtl8366.c
rename to drivers/net/dsa/realtek/rtl8366-core.c
drivers/net/dsa/realtek/rtl8366rb.c +4 -0
index 34e371084d6d..ff607608dead 100644
@@ -1814,3 +1814,7 @@ const struct realtek_variant rtl8366rb_variant = {
 	.chip_data_sz = sizeof(struct rtl8366rb),
 };
 EXPORT_SYMBOL_GPL(rtl8366rb_variant);
+
+MODULE_AUTHOR("Linus Walleij <[email protected]>");
+MODULE_DESCRIPTION("Driver for RTL8366RB ethernet switch");
+MODULE_LICENSE("GPL");
Reviewed-by on v4 4/11
#13 lore raw

Re: [PATCH net-next v4 04/11] net: dsa: realtek: convert subdrivers into modules

In reply to Luiz Angelo Daros de Luca
Quoted 6 lines · Luiz Angelo Daros de Luca

Preparing for multiple interfaces support, the drivers must be independent of realtek-smi.

Signature · 1 line
Florian
author maintainer
#14 lore raw

PATCH v4 5/11 net: dsa: realtek: use phy_read in ds->ops

In reply to Luiz Angelo Daros de Luca

The ds->ops->phy_read will only be used if the ds->slave_mii_bus was not initialized. Calling realtek_smi_setup_mdio will create a ds->slave_mii_bus, making ds->ops->phy_read dormant.

Using ds->ops->phy_read will allow switches connected through non-SMI interfaces (like mdio) to let ds allocate slave_mii_bus and reuse the same code.

---
 drivers/net/dsa/realtek/realtek-smi.c |  6 ++++--
 drivers/net/dsa/realtek/realtek.h     |  3 ---
 drivers/net/dsa/realtek/rtl8365mb.c   | 10 ++++++----
 drivers/net/dsa/realtek/rtl8366rb.c   | 10 ++++++----
 4 files changed, 16 insertions(+), 13 deletions(-)
drivers/net/dsa/realtek/realtek-smi.c +4 -2
index 5514fe81d64f..1f024e2520a6 100644
@@ -329,16 +329,18 @@ static const struct regmap_config realtek_smi_mdio_regmap_config = {
 static int realtek_smi_mdio_read(struct mii_bus *bus, int addr, int regnum)
 {
 	struct realtek_priv *priv = bus->priv;
+	struct dsa_switch *ds = priv->ds;
 
-	return priv->ops->phy_read(priv, addr, regnum);
+	return ds->ops->phy_read(ds, addr, regnum);
 }
 
 static int realtek_smi_mdio_write(struct mii_bus *bus, int addr, int regnum,
 				  u16 val)
 {
 	struct realtek_priv *priv = bus->priv;
+	struct dsa_switch *ds = priv->ds;
 
-	return priv->ops->phy_write(priv, addr, regnum, val);
+	return ds->ops->phy_write(ds, addr, regnum, val);
 }
 
 static int realtek_smi_setup_mdio(struct dsa_switch *ds)
drivers/net/dsa/realtek/realtek.h +0 -3
index 58814de563a2..a03de15c4a94 100644
@@ -103,9 +103,6 @@ struct realtek_ops {
 	int	(*enable_vlan)(struct realtek_priv *priv, bool enable);
 	int	(*enable_vlan4k)(struct realtek_priv *priv, bool enable);
 	int	(*enable_port)(struct realtek_priv *priv, int port, bool enable);
-	int	(*phy_read)(struct realtek_priv *priv, int phy, int regnum);
-	int	(*phy_write)(struct realtek_priv *priv, int phy, int regnum,
-			     u16 val);
 };
 
 struct realtek_variant {
drivers/net/dsa/realtek/rtl8365mb.c +6 -4
index b52bb987027c..11a985900c57 100644
@@ -674,8 +674,9 @@ static int rtl8365mb_phy_ocp_write(struct realtek_priv *priv, int phy,
 	return 0;
 }
 
-static int rtl8365mb_phy_read(struct realtek_priv *priv, int phy, int regnum)
+static int rtl8365mb_phy_read(struct dsa_switch *ds, int phy, int regnum)
 {
+	struct realtek_priv *priv = ds->priv;
 	u32 ocp_addr;
 	u16 val;
 	int ret;
@@ -702,9 +703,10 @@ static int rtl8365mb_phy_read(struct realtek_priv *priv, int phy, int regnum)
 	return val;
 }
 
-static int rtl8365mb_phy_write(struct realtek_priv *priv, int phy, int regnum,
+static int rtl8365mb_phy_write(struct dsa_switch *ds, int phy, int regnum,
 			       u16 val)
 {
+	struct realtek_priv *priv = (struct realtek_priv *)ds->priv;
 	u32 ocp_addr;
 	int ret;
 
@@ -1958,6 +1960,8 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
 	.get_tag_protocol = rtl8365mb_get_tag_protocol,
 	.setup = rtl8365mb_setup,
 	.teardown = rtl8365mb_teardown,
+	.phy_read = rtl8365mb_phy_read,
+	.phy_write = rtl8365mb_phy_write,
 	.phylink_validate = rtl8365mb_phylink_validate,
 	.phylink_mac_config = rtl8365mb_phylink_mac_config,
 	.phylink_mac_link_down = rtl8365mb_phylink_mac_link_down,
@@ -1974,8 +1978,6 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
 
 static const struct realtek_ops rtl8365mb_ops = {
 	.detect = rtl8365mb_detect,
-	.phy_read = rtl8365mb_phy_read,
-	.phy_write = rtl8365mb_phy_write,
 };
 
 const struct realtek_variant rtl8365mb_variant = {
drivers/net/dsa/realtek/rtl8366rb.c +6 -4
index ff607608dead..4576f9b797c5 100644
@@ -1641,8 +1641,9 @@ static int rtl8366rb_enable_vlan4k(struct realtek_priv *priv, bool enable)
 				  enable ? RTL8366RB_SGCR_EN_VLAN_4KTB : 0);
 }
 
-static int rtl8366rb_phy_read(struct realtek_priv *priv, int phy, int regnum)
+static int rtl8366rb_phy_read(struct dsa_switch *ds, int phy, int regnum)
 {
+	struct realtek_priv *priv = ds->priv;
 	u32 val;
 	u32 reg;
 	int ret;
@@ -1675,9 +1676,10 @@ static int rtl8366rb_phy_read(struct realtek_priv *priv, int phy, int regnum)
 	return val;
 }
 
-static int rtl8366rb_phy_write(struct realtek_priv *priv, int phy, int regnum,
+static int rtl8366rb_phy_write(struct dsa_switch *ds, int phy, int regnum,
 			       u16 val)
 {
+	struct realtek_priv *priv = ds->priv;
 	u32 reg;
 	int ret;
 
@@ -1769,6 +1771,8 @@ static int rtl8366rb_detect(struct realtek_priv *priv)
 static const struct dsa_switch_ops rtl8366rb_switch_ops = {
 	.get_tag_protocol = rtl8366_get_tag_protocol,
 	.setup = rtl8366rb_setup,
+	.phy_read = rtl8366rb_phy_read,
+	.phy_write = rtl8366rb_phy_write,
 	.phylink_mac_link_up = rtl8366rb_mac_link_up,
 	.phylink_mac_link_down = rtl8366rb_mac_link_down,
 	.get_strings = rtl8366_get_strings,
@@ -1801,8 +1805,6 @@ static const struct realtek_ops rtl8366rb_ops = {
 	.is_vlan_valid	= rtl8366rb_is_vlan_valid,
 	.enable_vlan	= rtl8366rb_enable_vlan,
 	.enable_vlan4k	= rtl8366rb_enable_vlan4k,
-	.phy_read	= rtl8366rb_phy_read,
-	.phy_write	= rtl8366rb_phy_write,
 };
 
 const struct realtek_variant rtl8366rb_variant = {
Signature · 1 line
2.34.0
Reviewed-by on v4 5/11
#15 lore raw

Re: [PATCH net-next v4 05/11] net: dsa: realtek: use phy_read in ds->ops

Quoted 11 lines · Luiz Angelo Daros de Luca

The ds->ops->phy_read will only be used if the ds->slave_mii_bus was not initialized. Calling realtek_smi_setup_mdio will create a ds->slave_mii_bus, making ds->ops->phy_read dormant.

Using ds->ops->phy_read will allow switches connected through non-SMI interfaces (like mdio) to let ds allocate slave_mii_bus and reuse the same code.

Quoted 136 lines
---
 drivers/net/dsa/realtek/realtek-smi.c |  6 ++++--
 drivers/net/dsa/realtek/realtek.h     |  3 ---
 drivers/net/dsa/realtek/rtl8365mb.c   | 10 ++++++----
 drivers/net/dsa/realtek/rtl8366rb.c   | 10 ++++++----
 4 files changed, 16 insertions(+), 13 deletions(-)
drivers/net/dsa/realtek/realtek-smi.c +4 -2
index 5514fe81d64f..1f024e2520a6 100644
@@ -329,16 +329,18 @@ static const struct regmap_config realtek_smi_mdio_regmap_config = {
 static int realtek_smi_mdio_read(struct mii_bus *bus, int addr, int regnum)
 {
 	struct realtek_priv *priv = bus->priv;
+	struct dsa_switch *ds = priv->ds;
 
-	return priv->ops->phy_read(priv, addr, regnum);
+	return ds->ops->phy_read(ds, addr, regnum);
 }
 
 static int realtek_smi_mdio_write(struct mii_bus *bus, int addr, int regnum,
 				  u16 val)
 {
 	struct realtek_priv *priv = bus->priv;
+	struct dsa_switch *ds = priv->ds;
 
-	return priv->ops->phy_write(priv, addr, regnum, val);
+	return ds->ops->phy_write(ds, addr, regnum, val);
 }
 
 static int realtek_smi_setup_mdio(struct dsa_switch *ds)
drivers/net/dsa/realtek/realtek.h +0 -3
index 58814de563a2..a03de15c4a94 100644
@@ -103,9 +103,6 @@ struct realtek_ops {
 	int	(*enable_vlan)(struct realtek_priv *priv, bool enable);
 	int	(*enable_vlan4k)(struct realtek_priv *priv, bool enable);
 	int	(*enable_port)(struct realtek_priv *priv, int port, bool enable);
-	int	(*phy_read)(struct realtek_priv *priv, int phy, int regnum);
-	int	(*phy_write)(struct realtek_priv *priv, int phy, int regnum,
-			     u16 val);
 };
 
 struct realtek_variant {
drivers/net/dsa/realtek/rtl8365mb.c +6 -4
index b52bb987027c..11a985900c57 100644
@@ -674,8 +674,9 @@ static int rtl8365mb_phy_ocp_write(struct realtek_priv *priv, int phy,
 	return 0;
 }
 
-static int rtl8365mb_phy_read(struct realtek_priv *priv, int phy, int regnum)
+static int rtl8365mb_phy_read(struct dsa_switch *ds, int phy, int regnum)
 {
+	struct realtek_priv *priv = ds->priv;
 	u32 ocp_addr;
 	u16 val;
 	int ret;
@@ -702,9 +703,10 @@ static int rtl8365mb_phy_read(struct realtek_priv *priv, int phy, int regnum)
 	return val;
 }
 
-static int rtl8365mb_phy_write(struct realtek_priv *priv, int phy, int regnum,
+static int rtl8365mb_phy_write(struct dsa_switch *ds, int phy, int regnum,
 			       u16 val)
 {
+	struct realtek_priv *priv = (struct realtek_priv *)ds->priv;
 	u32 ocp_addr;
 	int ret;
 
@@ -1958,6 +1960,8 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
 	.get_tag_protocol = rtl8365mb_get_tag_protocol,
 	.setup = rtl8365mb_setup,
 	.teardown = rtl8365mb_teardown,
+	.phy_read = rtl8365mb_phy_read,
+	.phy_write = rtl8365mb_phy_write,
 	.phylink_validate = rtl8365mb_phylink_validate,
 	.phylink_mac_config = rtl8365mb_phylink_mac_config,
 	.phylink_mac_link_down = rtl8365mb_phylink_mac_link_down,
@@ -1974,8 +1978,6 @@ static const struct dsa_switch_ops rtl8365mb_switch_ops = {
 
 static const struct realtek_ops rtl8365mb_ops = {
 	.detect = rtl8365mb_detect,
-	.phy_read = rtl8365mb_phy_read,
-	.phy_write = rtl8365mb_phy_write,
 };
 
 const struct realtek_variant rtl8365mb_variant = {
drivers/net/dsa/realtek/rtl8366rb.c +6 -4
index ff607608dead..4576f9b797c5 100644
@@ -1641,8 +1641,9 @@ static int rtl8366rb_enable_vlan4k(struct realtek_priv *priv, bool enable)
 				  enable ? RTL8366RB_SGCR_EN_VLAN_4KTB : 0);
 }
 
-static int rtl8366rb_phy_read(struct realtek_priv *priv, int phy, int regnum)
+static int rtl8366rb_phy_read(struct dsa_switch *ds, int phy, int regnum)
 {
+	struct realtek_priv *priv = ds->priv;
 	u32 val;
 	u32 reg;
 	int ret;
@@ -1675,9 +1676,10 @@ static int rtl8366rb_phy_read(struct realtek_priv *priv, int phy, int regnum)
 	return val;
 }
 
-static int rtl8366rb_phy_write(struct realtek_priv *priv, int phy, int regnum,
+static int rtl8366rb_phy_write(struct dsa_switch *ds, int phy, int regnum,
 			       u16 val)
 {
+	struct realtek_priv *priv = ds->priv;
 	u32 reg;
 	int ret;
 
@@ -1769,6 +1771,8 @@ static int rtl8366rb_detect(struct realtek_priv *priv)
 static const struct dsa_switch_ops rtl8366rb_switch_ops = {
 	.get_tag_protocol = rtl8366_get_tag_protocol,
 	.setup = rtl8366rb_setup,
+	.phy_read = rtl8366rb_phy_read,
+	.phy_write = rtl8366rb_phy_write,
 	.phylink_mac_link_up = rtl8366rb_mac_link_up,
 	.phylink_mac_link_down = rtl8366rb_mac_link_down,
 	.get_strings = rtl8366_get_strings,
@@ -1801,8 +1805,6 @@ static const struct realtek_ops rtl8366rb_ops = {
 	.is_vlan_valid	= rtl8366rb_is_vlan_valid,
 	.enable_vlan	= rtl8366rb_enable_vlan,
 	.enable_vlan4k	= rtl8366rb_enable_vlan4k,
-	.phy_read	= rtl8366rb_phy_read,
-	.phy_write	= rtl8366rb_phy_write,
 };
 
 const struct realtek_variant rtl8366rb_variant = {
#16 lore raw

Re: [PATCH net-next v4 05/11] net: dsa: realtek: use phy_read in ds->ops

In reply to Luiz Angelo Daros de Luca
Quoted 11 lines · Luiz Angelo Daros de Luca

The ds->ops->phy_read will only be used if the ds->slave_mii_bus was not initialized. Calling realtek_smi_setup_mdio will create a ds->slave_mii_bus, making ds->ops->phy_read dormant.

Using ds->ops->phy_read will allow switches connected through non-SMI interfaces (like mdio) to let ds allocate slave_mii_bus and reuse the same code.

Humm assigning dsa_switch_ops::phy_read will force DSA into tearing down the MDIO bus in dsa_switch_teardown() instead of letting your driver do it and since realtek-smi-core.c uses devm_mdiobus_unregister(), it is not clear to me what is going to happen but it sounds like a double free might happen?

It seems more prudent to me to leave existing code.

Signature · 1 line
Florian
author maintainer
#17 lore raw

Re: [PATCH net-next v4 05/11] net: dsa: realtek: use phy_read in ds->ops

Quoted 13 lines · Luiz Angelo Daros de Luca
Quoted 11 lines

The ds->ops->phy_read will only be used if the ds->slave_mii_bus was not initialized. Calling realtek_smi_setup_mdio will create a ds->slave_mii_bus, making ds->ops->phy_read dormant.

Using ds->ops->phy_read will allow switches connected through non-SMI interfaces (like mdio) to let ds allocate slave_mii_bus and reuse the same code.

Quoted 5 lines

Humm assigning dsa_switch_ops::phy_read will force DSA into tearing down the MDIO bus in dsa_switch_teardown() instead of letting your driver do it and since realtek-smi-core.c uses devm_mdiobus_unregister(), it is not clear to me what is going to happen but it sounds like a double free might happen?

Thanks, Florian. You should be correct. It might call mdiobus_unregister() and mdiobus_free() twice, once inside the dsa code and another one by the devm (if I understood how devm functions work).

The issue is that the dsa switch is assuming that if slave_mii is allocated and ds->ops->phy_read is defined, it has allocated the slave_mii by itself and it should clean up the slave_mii during teardown.
That assumption came from commit 5135e96a3dd2f4555ae6981c3155a62bcf3227f6 "So I can only guess that no driver that implements ds->ops->phy_read also allocates and registers ds->slave_mii_bus itself.". If that is true, the condition during dsa_switch_setup() is not correct.

During dsa_switch_setup(), if it does not fail, I know that ds->slave_mii_bus will be allocated, either by ds->ops->setup() or by itself.

dsa_switch_setup() {
        ....
        ds->ops->setup()
        ....
        if (!ds->slave_mii_bus && ds->ops->phy_read) {
              ...allocate and register ds->slave_mii_bus...
        }
}

During the teardown, ds->slave_mii_bus will always be true (if not cleaning from an error before it was allocated). So, the test is really about having ds->ops->phy_read.

dsa_switch_teardown() {
        ...
        if (ds->slave_mii_bus && ds->ops->phy_read) {
             ...unregister and free ds->slave_mii_bus...
        }
        ...
        ds->ops->teardown();
        ...
}

As ds->ops->teardown() is called after slave_mii_bus is gone, there is no opportunity for ds->ops to clean the mii_slave_bus it might have allocated.
It does not make sense for me to have those two "if" conditions working together. It should be either:

dsa_switch_setup() {
        ....
        ds->ops->setup()
        ....
        if (ds->ops->phy_read) {
              if (ds->slave_mii_bus)
                    error("ds->ops->phy_read is set, I should be the
one allocating ds->slave_mii_bus!")
              ...allocate and register ds->slave_mii_bus...
        }
}

if "no driver that implements ds->ops->phy_read also allocates and registers ds->slave_mii_bus itself" or:

dsa_switch_teardown() {
        ...
        if (ds->slave_mii_bus && "slave_mii_bus was allocated by myself") {
             ...unregister and free ds->slave_mii_bus...
        }
        ds->ops->teardown();
        ...
}

if ds->ops->phy_read value should not tell if ds->slave_mii_bus should be cleaned by the DSA switch.

I would selfishly hope the correct one was the second option because it would make my code much cleaner. If not, that's a complex issue to solve without lots of duplications: realtek-smi drivers should not have ds->ops->phy_read defined while realtek-mdio requires it. I'll need to duplicate dsa_switch_ops for each subdriver only to unset phy_read and also duplicate realtek_variant for each interface only to reference that different dsa_switch_ops.

BTW, the realtek-smi calls of_node_put(priv->slave_mii_bus->dev.of_node) during shutdown while other dsa drivers do not seem to care. Wouldn't devm controls be enough for cleaning that mii_bus?
Even if not, wouldn't the ds->ops->teardown be the correct place for that cleanup and not realtek_smi_remove()?

Quoted 1 line

It seems more prudent to me to leave existing code.

As I mentioned, It would require a good amount of duplications. But I'll do what needs to be done.

Regards,

Luiz

maintainer
#18 lore raw

Re: [PATCH net-next v4 05/11] net: dsa: realtek: use phy_read in ds->ops

Quoted 9 lines

Thanks, Florian. You should be correct. It might call mdiobus_unregister() and mdiobus_free() twice, once inside the dsa code and another one by the devm (if I understood how devm functions work).

The issue is that the dsa switch is assuming that if slave_mii is allocated and ds->ops->phy_read is defined, it has allocated the slave_mii by itself and it should clean up the slave_mii during teardown.

Correct. Either the DSA core takes care of the mdiobus and uses the phy_read and phy_write ops, or the driver internally registers its own mdiobus, and phy_read and phy_write ops are not implemented. The core is not designed to mix those together.

Quoted 10 lines

if ds->ops->phy_read value should not tell if ds->slave_mii_bus should be cleaned by the DSA switch.

I would selfishly hope the correct one was the second option because it would make my code much cleaner. If not, that's a complex issue to solve without lots of duplications: realtek-smi drivers should not have ds->ops->phy_read defined while realtek-mdio requires it. I'll need to duplicate dsa_switch_ops for each subdriver only to unset phy_read and also duplicate realtek_variant for each interface only to reference that different dsa_switch_ops.

One option would be to provide a dummy mdiobus driver for realtek-mdio, which simply passes the access through to the existing MDIO bus.

Andrew

author maintainer
#19 lore raw

Re: [PATCH net-next v4 05/11] net: dsa: realtek: use phy_read in ds->ops

---

Quoted 26 lines
Quoted 9 lines

Thanks, Florian. You should be correct. It might call mdiobus_unregister() and mdiobus_free() twice, once inside the dsa code and another one by the devm (if I understood how devm functions work).

The issue is that the dsa switch is assuming that if slave_mii is allocated and ds->ops->phy_read is defined, it has allocated the slave_mii by itself and it should clean up the slave_mii during teardown.

Correct. Either the DSA core takes care of the mdiobus and uses the phy_read and phy_write ops, or the driver internally registers its own mdiobus, and phy_read and phy_write ops are not implemented. The core is not designed to mix those together.

Quoted 10 lines

if ds->ops->phy_read value should not tell if ds->slave_mii_bus should be cleaned by the DSA switch.

I would selfishly hope the correct one was the second option because it would make my code much cleaner. If not, that's a complex issue to solve without lots of duplications: realtek-smi drivers should not have ds->ops->phy_read defined while realtek-mdio requires it. I'll need to duplicate dsa_switch_ops for each subdriver only to unset phy_read and also duplicate realtek_variant for each interface only to reference that different dsa_switch_ops.

Quoted 5 lines

One option would be to provide a dummy mdiobus driver for realtek-mdio, which simply passes the access through to the existing MDIO bus.

Andrew

Ok, thanks for the clarification, Andrew. In the end, I simply duplicated ds_ops and wrote a wrap funcion.
Much less painful than I anticipated.

Should I submit a patch to make dsa work like this then?

dsa_switch_setup() {
        ....
        ds->ops->setup()
        ....
        if (ds->ops->phy_read) {
              if (ds->slave_mii_bus)
                    error("ds->ops->phy_read is set, I should be the
one allocating ds->slave_mii_bus!")
              ...allocate and register ds->slave_mii_bus...
        }
}

I think it is better to fail when there is an invalid setup than to expect the dsa driver to behave correctly.

Regards,

Luiz

maintainer
#20 lore raw

Re: [PATCH net-next v4 05/11] net: dsa: realtek: use phy_read in ds->ops

Quoted 13 lines

Should I submit a patch to make dsa work like this then?

dsa_switch_setup() {
        ....
        ds->ops->setup()
        ....
        if (ds->ops->phy_read) {
              if (ds->slave_mii_bus)
                    error("ds->ops->phy_read is set, I should be the
one allocating ds->slave_mii_bus!")
              ...allocate and register ds->slave_mii_bus...
        }
}

You could add a WARN_ON(ds->ops->phy_read && ds->ops->phy_read);

Andrew

author maintainer
#21 lore raw

PATCH v4 6/11 net: dsa: realtek: add new mdio interface for drivers

In reply to Luiz Angelo Daros de Luca

This driver is a mdio_driver instead of a platform driver (like realtek-smi).

---
 drivers/net/dsa/realtek/Kconfig        |  11 +-
 drivers/net/dsa/realtek/Makefile       |   1 +
 drivers/net/dsa/realtek/realtek-mdio.c | 221 +++++++++++++++++++++++++
 drivers/net/dsa/realtek/realtek.h      |   2 +
 4 files changed, 233 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/dsa/realtek/realtek-mdio.c
drivers/net/dsa/realtek/Kconfig +9 -2
index cd1aa95b7bf0..73b26171fade 100644
@@ -9,6 +9,13 @@ menuconfig NET_DSA_REALTEK
 	help
 	  Select to enable support for Realtek Ethernet switch chips.
 
+config NET_DSA_REALTEK_MDIO
+	tristate "Realtek MDIO connected switch driver"
+	depends on NET_DSA_REALTEK
+	default y
+	help
+	  Select to enable support for registering switches configured
+	  through MDIO.
 config NET_DSA_REALTEK_SMI
 	tristate "Realtek SMI connected switch driver"
 	depends on NET_DSA_REALTEK
@@ -21,7 +28,7 @@ config NET_DSA_REALTEK_RTL8365MB
 	tristate "Realtek RTL8365MB switch subdriver"
 	default y
 	depends on NET_DSA_REALTEK
-	depends on NET_DSA_REALTEK_SMI
+	depends on NET_DSA_REALTEK_SMI || NET_DSA_REALTEK_MDIO
 	select NET_DSA_TAG_RTL8_4
 	help
 	  Select to enable support for Realtek RTL8365MB
@@ -30,7 +37,7 @@ config NET_DSA_REALTEK_RTL8366RB
 	tristate "Realtek RTL8366RB switch subdriver"
 	default y
 	depends on NET_DSA_REALTEK
-	depends on NET_DSA_REALTEK_SMI
+	depends on NET_DSA_REALTEK_SMI || NET_DSA_REALTEK_MDIO
 	select NET_DSA_TAG_RTL4_A
 	help
 	  Select to enable support for Realtek RTL8366RB
drivers/net/dsa/realtek/Makefile +1 -0
index 8b5a4abcedd3..0aab57252a7c 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_NET_DSA_REALTEK_MDIO) 	+= realtek-mdio.o
 obj-$(CONFIG_NET_DSA_REALTEK_SMI) 	+= realtek-smi.o
 obj-$(CONFIG_NET_DSA_REALTEK_RTL8366RB) += rtl8366.o
 rtl8366-objs 				:= rtl8366-core.o rtl8366rb.o
drivers/net/dsa/realtek/realtek-mdio.c new file +221 -0

221 lines not on this page · raw message

drivers/net/dsa/realtek/realtek.h +2 -0
index a03de15c4a94..97274273cb3b 100644
@@ -50,6 +50,8 @@ struct realtek_priv {
 	struct gpio_desc	*mdio;
 	struct regmap		*map;
 	struct mii_bus		*slave_mii_bus;
+	struct mii_bus		*bus;
+	int			phy_id;
 
 	unsigned int		clk_delay;
 	u8			cmd_read;
Signature · 1 line
2.34.0
#22 lore raw

Re: [PATCH net-next v4 06/11] net: dsa: realtek: add new mdio interface for drivers

Quoted 5 lines · Luiz Angelo Daros de Luca

This driver is a mdio_driver instead of a platform driver (like realtek-smi).

This looks good but I still wonder about the START_OPs, see below.

Quoted 13 lines
---
 drivers/net/dsa/realtek/Kconfig        |  11 +-
 drivers/net/dsa/realtek/Makefile       |   1 +
 drivers/net/dsa/realtek/realtek-mdio.c | 221 +++++++++++++++++++++++++
 drivers/net/dsa/realtek/realtek.h      |   2 +
 4 files changed, 233 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/dsa/realtek/realtek-mdio.c
drivers/net/dsa/realtek/Kconfig +0 -0
index cd1aa95b7bf0..73b26171fade 100644

drivers/net/dsa/realtek/Kconfig menuconfig NET_DSA_REALTEK · line 18

Quoted 10 lines
drivers/net/dsa/realtek/Kconfig +7 -0
@@ -9,6 +9,13 @@ menuconfig NET_DSA_REALTEK
 	help
 	  Select to enable support for Realtek Ethernet switch chips.
 
+config NET_DSA_REALTEK_MDIO
+	tristate "Realtek MDIO connected switch driver"
+	depends on NET_DSA_REALTEK
+	default y
+	help
+	  Select to enable support for registering switches configured
+	  through MDIO.

Missing newline here

Quoted 76 lines
drivers/net/dsa/realtek/Kconfig +2 -2
 config NET_DSA_REALTEK_SMI
 	tristate "Realtek SMI connected switch driver"
 	depends on NET_DSA_REALTEK
@@ -21,7 +28,7 @@ config NET_DSA_REALTEK_RTL8365MB
 	tristate "Realtek RTL8365MB switch subdriver"
 	default y
 	depends on NET_DSA_REALTEK
-	depends on NET_DSA_REALTEK_SMI
+	depends on NET_DSA_REALTEK_SMI || NET_DSA_REALTEK_MDIO
 	select NET_DSA_TAG_RTL8_4
 	help
 	  Select to enable support for Realtek RTL8365MB
@@ -30,7 +37,7 @@ config NET_DSA_REALTEK_RTL8366RB
 	tristate "Realtek RTL8366RB switch subdriver"
 	default y
 	depends on NET_DSA_REALTEK
-	depends on NET_DSA_REALTEK_SMI
+	depends on NET_DSA_REALTEK_SMI || NET_DSA_REALTEK_MDIO
 	select NET_DSA_TAG_RTL4_A
 	help
 	  Select to enable support for Realtek RTL8366RB
drivers/net/dsa/realtek/Makefile +1 -0
index 8b5a4abcedd3..0aab57252a7c 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_NET_DSA_REALTEK_MDIO) 	+= realtek-mdio.o
 obj-$(CONFIG_NET_DSA_REALTEK_SMI) 	+= realtek-smi.o
 obj-$(CONFIG_NET_DSA_REALTEK_RTL8366RB) += rtl8366.o
 rtl8366-objs 				:= rtl8366-core.o rtl8366rb.o
drivers/net/dsa/realtek/realtek-mdio.c new file +39 -0
new file mode 100644
index 000000000000..b505f4d3c5f0
@@ -0,0 +1,221 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Realtek MDIO interface driver
+ *
+ * ASICs we intend to support with this driver:
+ *
+ * RTL8366   - The original version, apparently
+ * RTL8369   - Similar enough to have the same datsheet as RTL8366
+ * RTL8366RB - Probably reads out "RTL8366 revision B", has a quite
+ *             different register layout from the other two
+ * RTL8366S  - Is this "RTL8366 super"?
+ * RTL8367   - Has an OpenWRT driver as well
+ * RTL8368S  - Seems to be an alternative name for RTL8366RB
+ * RTL8370   - Also uses SMI
+ *
+ * Copyright (C) 2017 Linus Walleij <[email protected]>
+ * Copyright (C) 2010 Antti Seppälä <[email protected]>
+ * Copyright (C) 2010 Roman Yeryomin <[email protected]>
+ * Copyright (C) 2011 Colin Leitner <[email protected]>
+ * Copyright (C) 2009-2010 Gabor Juhos <[email protected]>
+ */
+
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/regmap.h>
+
+#include "realtek.h"
+
+/* Read/write via mdiobus */
+#define REALTEK_MDIO_CTRL0_REG		31
+#define REALTEK_MDIO_START_REG		29
+#define REALTEK_MDIO_CTRL1_REG		21
+#define REALTEK_MDIO_ADDRESS_REG	23
+#define REALTEK_MDIO_DATA_WRITE_REG	24
+#define REALTEK_MDIO_DATA_READ_REG	25
+
+#define REALTEK_MDIO_START_OP		0xFFFF
+#define REALTEK_MDIO_ADDR_OP		0x000E
+#define REALTEK_MDIO_READ_OP		0x0001
+#define REALTEK_MDIO_WRITE_OP		0x0003

drivers/net/dsa/realtek/realtek-mdio.c line 49

Quoted 10 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +10 -0
+
+static int realtek_mdio_read_reg(struct realtek_priv *priv, u32 addr, u32 *data)
+{
+	u32 phy_id = priv->phy_id;
+	struct mii_bus *bus = priv->bus;
+
+	mutex_lock(&bus->mdio_lock);
+
+	bus->write(bus, phy_id, REALTEK_MDIO_CTRL0_REG, REALTEK_MDIO_ADDR_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);

Hm, I thought you said it works without these START_OPs? Or just without the first one?

I don't know if it matters but I would suggest removing all START_OPs and seeing if things still work. If so then it's probably best to omit this because it is not present in the vendor driver, and nobody seems to know what it's for. I agree it could be something for older models but if it works on your model then let's leave it out :-)

Quoted 185 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +172 -0
+	bus->write(bus, phy_id, REALTEK_MDIO_ADDRESS_REG, addr);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_CTRL1_REG, REALTEK_MDIO_READ_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	*data = bus->read(bus, phy_id, REALTEK_MDIO_DATA_READ_REG);
+
+	mutex_unlock(&bus->mdio_lock);
+
+	return 0;
+}
+
+static int realtek_mdio_write_reg(struct realtek_priv *priv, u32 addr, u32 data)
+{
+	u32 phy_id = priv->phy_id;
+	struct mii_bus *bus = priv->bus;
+
+	mutex_lock(&bus->mdio_lock);
+
+	bus->write(bus, phy_id, REALTEK_MDIO_CTRL0_REG, REALTEK_MDIO_ADDR_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_ADDRESS_REG, addr);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_DATA_WRITE_REG, data);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_CTRL1_REG, REALTEK_MDIO_WRITE_OP);
+
+	mutex_unlock(&bus->mdio_lock);
+
+	return 0;
+}
+
+/* Regmap accessors */
+
+static int realtek_mdio_write(void *ctx, u32 reg, u32 val)
+{
+	struct realtek_priv *priv = ctx;
+
+	return realtek_mdio_write_reg(priv, reg, val);
+}
+
+static int realtek_mdio_read(void *ctx, u32 reg, u32 *val)
+{
+	struct realtek_priv *priv = ctx;
+
+	return realtek_mdio_read_reg(priv, reg, val);
+}
+
+static const struct regmap_config realtek_mdio_regmap_config = {
+	.reg_bits = 10, /* A4..A0 R4..R0 */
+	.val_bits = 16,
+	.reg_stride = 1,
+	/* PHY regs are at 0x8000 */
+	.max_register = 0xffff,
+	.reg_format_endian = REGMAP_ENDIAN_BIG,
+	.reg_read = realtek_mdio_read,
+	.reg_write = realtek_mdio_write,
+	.cache_type = REGCACHE_NONE,
+};
+
+static int realtek_mdio_probe(struct mdio_device *mdiodev)
+{
+	struct realtek_priv *priv;
+	struct device *dev = &mdiodev->dev;
+	const struct realtek_variant *var;
+	int ret;
+	struct device_node *np;
+
+	var = of_device_get_match_data(dev);
+	priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->map = devm_regmap_init(dev, NULL, priv, &realtek_mdio_regmap_config);
+	if (IS_ERR(priv->map)) {
+		ret = PTR_ERR(priv->map);
+		dev_err(dev, "regmap init failed: %d\n", ret);
+		return ret;
+	}
+
+	priv->phy_id = mdiodev->addr;
+	priv->bus = mdiodev->bus;
+	priv->dev = &mdiodev->dev;
+	priv->chip_data = (void *)priv + sizeof(*priv);
+
+	priv->clk_delay = var->clk_delay;
+	priv->cmd_read = var->cmd_read;
+	priv->cmd_write = var->cmd_write;
+	priv->ops = var->ops;
+
+	priv->write_reg_noack = realtek_mdio_write_reg;
+
+	np = dev->of_node;
+
+	dev_set_drvdata(dev, priv);
+
+	/* TODO: if power is software controlled, set up any regulators here */
+	priv->leds_disabled = of_property_read_bool(np, "realtek,disable-leds");
+
+	ret = priv->ops->detect(priv);
+	if (ret) {
+		dev_err(dev, "unable to detect switch\n");
+		return ret;
+	}
+
+	priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
+	if (!priv->ds)
+		return -ENOMEM;
+
+	priv->ds->dev = dev;
+	priv->ds->num_ports = priv->num_ports;
+	priv->ds->priv = priv;
+	priv->ds->ops = var->ds_ops;
+
+	ret = dsa_register_switch(priv->ds);
+	if (ret) {
+		dev_err(priv->dev, "unable to register switch ret = %d\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+
+static void realtek_mdio_remove(struct mdio_device *mdiodev)
+{
+	struct realtek_priv *priv = dev_get_drvdata(&mdiodev->dev);
+
+	if (!priv)
+		return;
+
+	dsa_unregister_switch(priv->ds);
+
+	dev_set_drvdata(&mdiodev->dev, NULL);
+}
+
+static void realtek_mdio_shutdown(struct mdio_device *mdiodev)
+{
+	struct realtek_priv *priv = dev_get_drvdata(&mdiodev->dev);
+
+	if (!priv)
+		return;
+
+	dsa_switch_shutdown(priv->ds);
+
+	dev_set_drvdata(&mdiodev->dev, NULL);
+}
+
+static const struct of_device_id realtek_mdio_of_match[] = {
+#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8366RB)
+	{ .compatible = "realtek,rtl8366rb", .data = &rtl8366rb_variant, },
+#endif
+#if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8365MB)
+	{ .compatible = "realtek,rtl8365mb", .data = &rtl8365mb_variant, },
+#endif
+	{ /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, realtek_mdio_of_match);
+
+static struct mdio_driver realtek_mdio_driver = {
+	.mdiodrv.driver = {
+		.name = "realtek-mdio",
+		.of_match_table = of_match_ptr(realtek_mdio_of_match),
+	},
+	.probe  = realtek_mdio_probe,
+	.remove = realtek_mdio_remove,
+	.shutdown = realtek_mdio_shutdown,
+};
+
+mdio_module_driver(realtek_mdio_driver);
+
+MODULE_AUTHOR("Luiz Angelo Daros de Luca <[email protected]>");
+MODULE_DESCRIPTION("Driver for Realtek ethernet switch connected via MDIO interface");
+MODULE_LICENSE("GPL");
drivers/net/dsa/realtek/realtek.h +2 -0
index a03de15c4a94..97274273cb3b 100644
@@ -50,6 +50,8 @@ struct realtek_priv {
 	struct gpio_desc	*mdio;
 	struct regmap		*map;
 	struct mii_bus		*slave_mii_bus;
+	struct mii_bus		*bus;
+	int			phy_id;
 
 	unsigned int		clk_delay;
 	u8			cmd_read;
#23 lore raw

Re: [PATCH net-next v4 06/11] net: dsa: realtek: add new mdio interface for drivers

In reply to Luiz Angelo Daros de Luca
Quoted 18 lines · Luiz Angelo Daros de Luca

This driver is a mdio_driver instead of a platform driver (like realtek-smi).

---
  drivers/net/dsa/realtek/Kconfig        |  11 +-
  drivers/net/dsa/realtek/Makefile       |   1 +
  drivers/net/dsa/realtek/realtek-mdio.c | 221 +++++++++++++++++++++++++
  drivers/net/dsa/realtek/realtek.h      |   2 +
  4 files changed, 233 insertions(+), 2 deletions(-)

create mode 100644 drivers/net/dsa/realtek/realtek-mdio.c

drivers/net/dsa/realtek/Kconfig +0 -0
index cd1aa95b7bf0..73b26171fade 100644

drivers/net/dsa/realtek/Kconfig menuconfig NET_DSA_REALTEK · line 15

Quoted 7 lines
drivers/net/dsa/realtek/Kconfig +4 -0
@@ -9,6 +9,13 @@ menuconfig NET_DSA_REALTEK
  	help
  	  Select to enable support for Realtek Ethernet switch chips.
 
+config NET_DSA_REALTEK_MDIO
+	tristate "Realtek MDIO connected switch driver"
+	depends on NET_DSA_REALTEK
+	default y

I suppose this is fine since we depend on NET_DSA_REALTEK.

[snip]

Quoted 4 lines
drivers/net/dsa/realtek/Kconfig +4 -0
+static int realtek_mdio_read_reg(struct realtek_priv *priv, u32 addr, u32 *data)
+{
+	u32 phy_id = priv->phy_id;
+	struct mii_bus *bus = priv->bus;

drivers/net/dsa/realtek/Kconfig menuconfig NET_DSA_REALTEK

Quoted 10 lines
drivers/net/dsa/realtek/Kconfig +10 -0
+
+	mutex_lock(&bus->mdio_lock);
+
+	bus->write(bus, phy_id, REALTEK_MDIO_CTRL0_REG, REALTEK_MDIO_ADDR_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_ADDRESS_REG, addr);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_CTRL1_REG, REALTEK_MDIO_READ_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	*data = bus->read(bus, phy_id, REALTEK_MDIO_DATA_READ_REG);

Do you have no way to return an error for instance, if you read from a non-existent PHY device on the MDIO bus, -EIO would be expected for instance. If the data returned is 0xffff that ought to be enough.

Quoted 6 lines
drivers/net/dsa/realtek/Kconfig +6 -0
+
+	mutex_unlock(&bus->mdio_lock);
+
+	return 0;
+}
+

drivers/net/dsa/realtek/Kconfig

Quoted 10 lines
drivers/net/dsa/realtek/Kconfig +10 -0
+static int realtek_mdio_write_reg(struct realtek_priv *priv, u32 addr, u32 data)
+{
+	u32 phy_id = priv->phy_id;
+	struct mii_bus *bus = priv->bus;
+
+	mutex_lock(&bus->mdio_lock);
+
+	bus->write(bus, phy_id, REALTEK_MDIO_CTRL0_REG, REALTEK_MDIO_ADDR_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_ADDRESS_REG, addr);

This repeats between read and writes, might be worth a helper function.

Quoted 15 lines
drivers/net/dsa/realtek/Kconfig +15 -0
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_DATA_WRITE_REG, data);
+	bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+	bus->write(bus, phy_id, REALTEK_MDIO_CTRL1_REG, REALTEK_MDIO_WRITE_OP);
+
+	mutex_unlock(&bus->mdio_lock);
+
+	return 0;
+}
+
+/* Regmap accessors */
+
+static int realtek_mdio_write(void *ctx, u32 reg, u32 val)
+{
+	struct realtek_priv *priv = ctx;

drivers/net/dsa/realtek/Kconfig

Quoted 10 lines
drivers/net/dsa/realtek/Kconfig +10 -0
+
+	return realtek_mdio_write_reg(priv, reg, val);
+}
+
+static int realtek_mdio_read(void *ctx, u32 reg, u32 *val)
+{
+	struct realtek_priv *priv = ctx;
+
+	return realtek_mdio_read_reg(priv, reg, val);
+}

Do you see a value for this function as oppposed to inlining the bodies of realtek_mdio_read_reg and realtek_mdio_write_reg directly into these two functions?

Quoted 12 lines
drivers/net/dsa/realtek/Kconfig +12 -0
+
+static const struct regmap_config realtek_mdio_regmap_config = {
+	.reg_bits = 10, /* A4..A0 R4..R0 */
+	.val_bits = 16,
+	.reg_stride = 1,
+	/* PHY regs are at 0x8000 */
+	.max_register = 0xffff,
+	.reg_format_endian = REGMAP_ENDIAN_BIG,
+	.reg_read = realtek_mdio_read,
+	.reg_write = realtek_mdio_write,
+	.cache_type = REGCACHE_NONE,
+};

drivers/net/dsa/realtek/Kconfig

Quoted 10 lines
drivers/net/dsa/realtek/Kconfig +10 -0
+
+static int realtek_mdio_probe(struct mdio_device *mdiodev)
+{
+	struct realtek_priv *priv;
+	struct device *dev = &mdiodev->dev;
+	const struct realtek_variant *var;
+	int ret;
+	struct device_node *np;
+
+	var = of_device_get_match_data(dev);

Don't you have to check that var is non-NULL just in case?

drivers/net/dsa/realtek/Kconfig

Quoted 12 lines
drivers/net/dsa/realtek/Kconfig +12 -0
+	priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
+	priv->map = devm_regmap_init(dev, NULL, priv, &realtek_mdio_regmap_config);
+	if (IS_ERR(priv->map)) {
+		ret = PTR_ERR(priv->map);
+		dev_err(dev, "regmap init failed: %d\n", ret);
+		return ret;
+	}
+
+	priv->phy_id = mdiodev->addr;

Please use a more descriptive variable name such as mdio_addr or something like that. I know that phy_id is typically used but it could also mean a 32-bit PHY unique identifier, which a MDIO device does not have typically.

Looks fine otherwise.

Signature · 1 line
Florian
author maintainer Applied
#24 lore raw

Re: [PATCH net-next v4 06/11] net: dsa: realtek: add new mdio interface for drivers

Quoted 10 lines
Quoted 10 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +10 -0
+static int realtek_mdio_read_reg(struct realtek_priv *priv, u32 addr, u32 *data)
+{
+     u32 phy_id = priv->phy_id;
+     struct mii_bus *bus = priv->bus;
+
+     mutex_lock(&bus->mdio_lock);
+
+     bus->write(bus, phy_id, REALTEK_MDIO_CTRL0_REG, REALTEK_MDIO_ADDR_OP);
+     bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+     bus->write(bus, phy_id, REALTEK_MDIO_ADDRESS_REG, addr);
Quoted 8 lines

drivers/net/dsa/realtek/realtek-mdio.c line 54

Quoted 4 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +4 -0
+     bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+     bus->write(bus, phy_id, REALTEK_MDIO_CTRL1_REG, REALTEK_MDIO_READ_OP);
+     bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+     *data = bus->read(bus, phy_id, REALTEK_MDIO_DATA_READ_REG);

Do you have no way to return an error for instance, if you read from a non-existent PHY device on the MDIO bus, -EIO would be expected for instance. If the data returned is 0xffff that ought to be enough.

I'll check for error (non zero for write, negative for read) and return that value

Quoted 10 lines
Quoted 10 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +10 -0
+
+     mutex_unlock(&bus->mdio_lock);
+
+     return 0;
+}
+
+static int realtek_mdio_write_reg(struct realtek_priv *priv, u32 addr, u32 data)
+{
+     u32 phy_id = priv->phy_id;
+     struct mii_bus *bus = priv->bus;
Quoted 8 lines

drivers/net/dsa/realtek/realtek-mdio.c line 50

Quoted 6 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +6 -0
+
+     mutex_lock(&bus->mdio_lock);
+
+     bus->write(bus, phy_id, REALTEK_MDIO_CTRL0_REG, REALTEK_MDIO_ADDR_OP);
+     bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+     bus->write(bus, phy_id, REALTEK_MDIO_ADDRESS_REG, addr);

This repeats between read and writes, might be worth a helper function.

Without the REALTEK_MDIO_START_OP Alvin asked, it is not worth it anymore.

Quoted 22 lines
Quoted 22 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +21 -0
+     bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+     bus->write(bus, phy_id, REALTEK_MDIO_DATA_WRITE_REG, data);
+     bus->write(bus, phy_id, REALTEK_MDIO_START_REG, REALTEK_MDIO_START_OP);
+     bus->write(bus, phy_id, REALTEK_MDIO_CTRL1_REG, REALTEK_MDIO_WRITE_OP);
+
+     mutex_unlock(&bus->mdio_lock);
+
+     return 0;
+}
+
+/* Regmap accessors */
+
+static int realtek_mdio_write(void *ctx, u32 reg, u32 val)
+{
+     struct realtek_priv *priv = ctx;
+
+     return realtek_mdio_write_reg(priv, reg, val);
+}
+
+static int realtek_mdio_read(void *ctx, u32 reg, u32 *val)
+{
Quoted 9 lines

drivers/net/dsa/realtek/realtek-mdio.c line 95

Quoted 4 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +4 -0
+     struct realtek_priv *priv = ctx;
+
+     return realtek_mdio_read_reg(priv, reg, val);
+}

Do you see a value for this function as oppposed to inlining the bodies of realtek_mdio_read_reg and realtek_mdio_write_reg directly into these two functions?

I merged them. I also changed the write_reg_noack signature to match regmap->write_reg, so I can use it without a wrapper.

Quoted 16 lines
Quoted 16 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +16 -0
+
+static const struct regmap_config realtek_mdio_regmap_config = {
+     .reg_bits = 10, /* A4..A0 R4..R0 */
+     .val_bits = 16,
+     .reg_stride = 1,
+     /* PHY regs are at 0x8000 */
+     .max_register = 0xffff,
+     .reg_format_endian = REGMAP_ENDIAN_BIG,
+     .reg_read = realtek_mdio_read,
+     .reg_write = realtek_mdio_write,
+     .cache_type = REGCACHE_NONE,
+};
+
+static int realtek_mdio_probe(struct mdio_device *mdiodev)
+{
+     struct realtek_priv *priv;
Quoted 8 lines

drivers/net/dsa/realtek/realtek-mdio.c line 117

Quoted 6 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +6 -0
+     struct device *dev = &mdiodev->dev;
+     const struct realtek_variant *var;
+     int ret;
+     struct device_node *np;
+
+     var = of_device_get_match_data(dev);

Don't you have to check that var is non-NULL just in case?

I'll add that check but it is not likely to happen.

Quoted 10 lines
Quoted 10 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +9 -0
+     priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
+     if (!priv)
+             return -ENOMEM;
+
+     priv->map = devm_regmap_init(dev, NULL, priv, &realtek_mdio_regmap_config);
+     if (IS_ERR(priv->map)) {
+             ret = PTR_ERR(priv->map);
+             dev_err(dev, "regmap init failed: %d\n", ret);
+             return ret;
Quoted 8 lines

drivers/net/dsa/realtek/realtek-mdio.c line 129

Quoted 3 lines
drivers/net/dsa/realtek/realtek-mdio.c new file +3 -0
+     }
+
+     priv->phy_id = mdiodev->addr;

Please use a more descriptive variable name such as mdio_addr or something like that. I know that phy_id is typically used but it could also mean a 32-bit PHY unique identifier, which a MDIO device does not have typically.

Renamed to mdio_addr. As you said, I just used what is typically used but you know best.

Quoted 2 lines

Looks fine otherwise.

Thanks, Florian.

Luiz

author maintainer
#25 lore raw

PATCH v4 7/11 net: dsa: realtek: rtl8365mb: rename extport to extint, add "realtek,ext-int"

In reply to Luiz Angelo Daros de Luca

"extport" 0, 1, 2 was used to reference external ports (ext0, ext1, ext2). Meanwhile, port 0..9 is used as switch ports, including external ports. "extport" was renamed to extint to make it clear it does not mean the port number but the external interface number.

The macros that map extint numbers to registers addresses now use inline ifs instead of binary arithmetic.

"extint" was hardcoded to 1. However, some chips have multiple external interfaces. It's not right to assume the CPU port uses extint 1 nor that all extint are CPU ports. Now the association between the port and the external interface can be defined with a device-tree port property "realtek,ext-int".

This patch still does not allow multiple CPU ports nor extint as a non CPU port.

---
 drivers/net/dsa/realtek/rtl8365mb.c | 135 ++++++++++++++++++----------
 1 file changed, 88 insertions(+), 47 deletions(-)
drivers/net/dsa/realtek/rtl8365mb.c +88 -47
index 11a985900c57..e115129cd5cd 100644
@@ -191,7 +191,13 @@
 /* The PHY OCP addresses of PHY registers 0~31 start here */
 #define RTL8365MB_PHY_OCP_ADDR_PHYREG_BASE		0xA400
 
-/* EXT port interface mode values - used in DIGITAL_INTERFACE_SELECT */
+/* EXT interface numbers */
+#define RTL8365MB_NOT_EXT			-1
+#define RTL8365MB_EXT0				 0
+#define RTL8365MB_EXT1				 1
+#define RTL8365MB_EXT2				 2
+
+/* EXT interface port mode values - used in DIGITAL_INTERFACE_SELECT */
 #define RTL8365MB_EXT_PORT_MODE_DISABLE		0
 #define RTL8365MB_EXT_PORT_MODE_RGMII		1
 #define RTL8365MB_EXT_PORT_MODE_MII_MAC		2
@@ -207,39 +213,44 @@
 #define RTL8365MB_EXT_PORT_MODE_1000X		12
 #define RTL8365MB_EXT_PORT_MODE_100FX		13
 
-/* EXT port interface mode configuration registers 0~1 */
-#define RTL8365MB_DIGITAL_INTERFACE_SELECT_REG0		0x1305
-#define RTL8365MB_DIGITAL_INTERFACE_SELECT_REG1		0x13C3
-#define RTL8365MB_DIGITAL_INTERFACE_SELECT_REG(_extport)   \
-		(RTL8365MB_DIGITAL_INTERFACE_SELECT_REG0 + \
-		 ((_extport) >> 1) * (0x13C3 - 0x1305))
-#define   RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_MASK(_extport) \
-		(0xF << (((_extport) % 2)))
-#define   RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_OFFSET(_extport) \
-		(((_extport) % 2) * 4)
-
-/* EXT port RGMII TX/RX delay configuration registers 1~2 */
-#define RTL8365MB_EXT_RGMXF_REG1		0x1307
-#define RTL8365MB_EXT_RGMXF_REG2		0x13C5
-#define RTL8365MB_EXT_RGMXF_REG(_extport)   \
-		(RTL8365MB_EXT_RGMXF_REG1 + \
-		 (((_extport) >> 1) * (0x13C5 - 0x1307)))
+/* EXT interface mode configuration registers 0~1 */
+#define RTL8365MB_DIGITAL_INTERFACE_SELECT_REG0		0x1305 /* EXT1 */
+#define RTL8365MB_DIGITAL_INTERFACE_SELECT_REG1		0x13C3 /* EXT2 */
+#define RTL8365MB_DIGITAL_INTERFACE_SELECT_REG(_extint) \
+		((_extint) == 1 ? RTL8365MB_DIGITAL_INTERFACE_SELECT_REG0 : \
+		 (_extint) == 2 ? RTL8365MB_DIGITAL_INTERFACE_SELECT_REG1 : \
+		 0x0)
+#define   RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_MASK(_extint) \
+		(0xF << (((_extint) % 2)))
+#define   RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_OFFSET(_extint) \
+		(((_extint) % 2) * 4)
+
+/* EXT interface RGMII TX/RX delay configuration registers 0~2 */
+#define RTL8365MB_EXT_RGMXF_REG0		0x1306 /* EXT0 */
+#define RTL8365MB_EXT_RGMXF_REG1		0x1307 /* EXT1 */
+#define RTL8365MB_EXT_RGMXF_REG2		0x13C5 /* EXT2 */
+#define RTL8365MB_EXT_RGMXF_REG(_extint) \
+		((_extint) == 0 ? RTL8365MB_EXT_RGMXF_REG0 : \
+		 (_extint) == 1 ? RTL8365MB_EXT_RGMXF_REG1 : \
+		 (_extint) == 2 ? RTL8365MB_EXT_RGMXF_REG2 : \
+		 0x0)
 #define   RTL8365MB_EXT_RGMXF_RXDELAY_MASK	0x0007
 #define   RTL8365MB_EXT_RGMXF_TXDELAY_MASK	0x0008
 
-/* External port speed values - used in DIGITAL_INTERFACE_FORCE */
+/* External interface port speed values - used in DIGITAL_INTERFACE_FORCE */
 #define RTL8365MB_PORT_SPEED_10M	0
 #define RTL8365MB_PORT_SPEED_100M	1
 #define RTL8365MB_PORT_SPEED_1000M	2
 
-/* EXT port force configuration registers 0~2 */
-#define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG0			0x1310
-#define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG1			0x1311
-#define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG2			0x13C4
-#define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG(_extport)   \
-		(RTL8365MB_DIGITAL_INTERFACE_FORCE_REG0 + \
-		 ((_extport) & 0x1) +                     \
-		 ((((_extport) >> 1) & 0x1) * (0x13C4 - 0x1310)))
+/* EXT interface force configuration registers 0~2 */
+#define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG0		0x1310 /* EXT0 */
+#define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG1		0x1311 /* EXT1 */
+#define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG2		0x13C4 /* EXT2 */
+#define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG(_extint) \
+		((_extint) == 0 ? RTL8365MB_DIGITAL_INTERFACE_FORCE_REG0 : \
+		 (_extint) == 1 ? RTL8365MB_DIGITAL_INTERFACE_FORCE_REG1 : \
+		 (_extint) == 2 ? RTL8365MB_DIGITAL_INTERFACE_FORCE_REG2 : \
+		 0x0)
 #define   RTL8365MB_DIGITAL_INTERFACE_FORCE_EN_MASK		0x1000
 #define   RTL8365MB_DIGITAL_INTERFACE_FORCE_NWAY_MASK		0x0080
 #define   RTL8365MB_DIGITAL_INTERFACE_FORCE_TXPAUSE_MASK	0x0040
@@ -522,6 +533,7 @@ struct rtl8365mb_cpu {
  *         access via rtl8365mb_get_stats64
  * @stats_lock: protect the stats structure during read/update
  * @mib_work: delayed work for polling MIB counters
+ * @ext_int: the external interface port related to this port, RTL8365MB_NOT_EXT(-1) if none
  */
 struct rtl8365mb_port {
 	struct realtek_priv *priv;
@@ -529,6 +541,7 @@ struct rtl8365mb_port {
 	struct rtnl_link_stats64 stats;
 	spinlock_t stats_lock;
 	struct delayed_work mib_work;
+	int ext_int;
 };
 
 /**
@@ -742,24 +755,28 @@ rtl8365mb_get_tag_protocol(struct dsa_switch *ds, int port,
 static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port,
 				      phy_interface_t interface)
 {
+	struct rtl8365mb_port *p;
 	struct device_node *dn;
+	struct rtl8365mb *mb;
 	struct dsa_port *dp;
 	int tx_delay = 0;
 	int rx_delay = 0;
-	int ext_port;
+	int ext_int;
 	u32 val;
 	int ret;
 
-	if (port == priv->cpu_port) {
-		ext_port = 1;
-	} else {
-		dev_err(priv->dev, "only one EXT port is currently supported\n");
+	if (port != priv->cpu_port) {
+		dev_err(priv->dev, "only one EXT interface is currently supported\n");
 		return -EINVAL;
 	}
 
 	dp = dsa_to_port(priv->ds, port);
 	dn = dp->dn;
 
+	mb = priv->chip_data;
+	p = &mb->ports[port];
+	ext_int = p->ext_int;
+
 	/* Set the RGMII TX/RX delay
 	 *
 	 * The Realtek vendor driver indicates the following possible
@@ -789,7 +806,7 @@ static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port,
 			tx_delay = val / 2;
 		else
 			dev_warn(priv->dev,
-				 "EXT port TX delay must be 0 or 2 ns\n");
+				 "EXT interface TX delay must be 0 or 2 ns\n");
 	}
 
 	if (!of_property_read_u32(dn, "rx-internal-delay-ps", &val)) {
@@ -799,11 +816,11 @@ static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port,
 			rx_delay = val;
 		else
 			dev_warn(priv->dev,
-				 "EXT port RX delay must be 0 to 2.1 ns\n");
+				 "EXT interface RX delay must be 0 to 2.1 ns\n");
 	}
 
 	ret = regmap_update_bits(
-		priv->map, RTL8365MB_EXT_RGMXF_REG(ext_port),
+		priv->map, RTL8365MB_EXT_RGMXF_REG(ext_int),
 		RTL8365MB_EXT_RGMXF_TXDELAY_MASK |
 			RTL8365MB_EXT_RGMXF_RXDELAY_MASK,
 		FIELD_PREP(RTL8365MB_EXT_RGMXF_TXDELAY_MASK, tx_delay) |
@@ -812,11 +829,11 @@ static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port,
 		return ret;
 
 	ret = regmap_update_bits(
-		priv->map, RTL8365MB_DIGITAL_INTERFACE_SELECT_REG(ext_port),
-		RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_MASK(ext_port),
+		priv->map, RTL8365MB_DIGITAL_INTERFACE_SELECT_REG(ext_int),
+		RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_MASK(ext_int),
 		RTL8365MB_EXT_PORT_MODE_RGMII
 			<< RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_OFFSET(
-				   ext_port));
+				   ext_int));
 	if (ret)
 		return ret;
 
@@ -827,22 +844,26 @@ static int rtl8365mb_ext_config_forcemode(struct realtek_priv *priv, int port,
 					  bool link, int speed, int duplex,
 					  bool tx_pause, bool rx_pause)
 {
+	struct rtl8365mb_port *p;
+	struct rtl8365mb *mb;
 	u32 r_tx_pause;
 	u32 r_rx_pause;
 	u32 r_duplex;
 	u32 r_speed;
 	u32 r_link;
-	int ext_port;
+	int ext_int;
 	int val;
 	int ret;
 
-	if (port == priv->cpu_port) {
-		ext_port = 1;
-	} else {
-		dev_err(priv->dev, "only one EXT port is currently supported\n");
+	if (port != priv->cpu_port) {
+		dev_err(priv->dev, "only one EXT interface is currently supported\n");
 		return -EINVAL;
 	}
 
+	mb = priv->chip_data;
+	p = &mb->ports[port];
+	ext_int = p->ext_int;
+
 	if (link) {
 		/* Force the link up with the desired configuration */
 		r_link = 1;
@@ -889,7 +910,7 @@ static int rtl8365mb_ext_config_forcemode(struct realtek_priv *priv, int port,
 			 r_duplex) |
 	      FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_SPEED_MASK, r_speed);
 	ret = regmap_write(priv->map,
-			   RTL8365MB_DIGITAL_INTERFACE_FORCE_REG(ext_port),
+			   RTL8365MB_DIGITAL_INTERFACE_FORCE_REG(ext_int),
 			   val);
 	if (ret)
 		return ret;
@@ -1819,13 +1840,13 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 	/* Configure ports */
 	for (i = 0; i < priv->num_ports; i++) {
 		struct rtl8365mb_port *p = &mb->ports[i];
+		struct device_node *dn;
+		u32 val;
 
 		if (dsa_is_unused_port(priv->ds, i))
 			continue;
 
-		/* Set up per-port private data */
-		p->priv = priv;
-		p->index = i;
+		dn = dsa_to_port(priv->ds, i)->dn;
 
 		/* Forward only to the CPU */
 		ret = rtl8365mb_port_set_isolation(priv, i, BIT(priv->cpu_port));
@@ -1842,6 +1863,26 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 		 * administratively down by default.
 		 */
 		rtl8365mb_port_stp_state_set(priv->ds, i, BR_STATE_DISABLED);
+
+		/* Set up per-port private data */
+		p->priv = priv;
+		p->index = i;
+
+		if (!of_property_read_u32(dn, "realtek,ext-int", &val)) {
+			if (val < 0 || val > 2) {
+				dev_err(priv->dev,
+					"realtek,ext-int must be between 0 and 2\n");
+				return -EINVAL;
+			}
+
+			p->ext_int = val;
+		} else {
+			if (dsa_is_cpu_port(priv->ds, i))
+				/* Default for compatibility with older device trees */
+				p->ext_int = RTL8365MB_EXT1;
+			else
+				p->ext_int = RTL8365MB_NOT_EXT;
+		}
 	}
 
 	/* Set maximum packet length to 1536 bytes */
Signature · 1 line
2.34.0
Reviewed-by on v4 7/11
#26 lore raw

Re: [PATCH net-next v4 07/11] net: dsa: realtek: rtl8365mb: rename extport to extint, add "realtek,ext-int"

Quoted 15 lines · Luiz Angelo Daros de Luca

"extport" 0, 1, 2 was used to reference external ports (ext0, ext1, ext2). Meanwhile, port 0..9 is used as switch ports, including external ports. "extport" was renamed to extint to make it clear it does not mean the port number but the external interface number.

The macros that map extint numbers to registers addresses now use inline ifs instead of binary arithmetic.

"extint" was hardcoded to 1. However, some chips have multiple external interfaces. It's not right to assume the CPU port uses extint 1 nor that all extint are CPU ports. Now the association between the port and the external interface can be defined with a device-tree port property "realtek,ext-int".

Quoted 5 lines

This patch still does not allow multiple CPU ports nor extint as a non CPU port.

Quoted 3 lines
---
 drivers/net/dsa/realtek/rtl8365mb.c | 135 ++++++++++++++++++----------
 1 file changed, 88 insertions(+), 47 deletions(-)
Reviewed-by on v4 7/11
#27 lore raw

Re: [PATCH net-next v4 07/11] net: dsa: realtek: rtl8365mb: rename extport to extint, add "realtek,ext-int"

In reply to Luiz Angelo Daros de Luca
Quoted 15 lines · Luiz Angelo Daros de Luca

"extport" 0, 1, 2 was used to reference external ports (ext0, ext1, ext2). Meanwhile, port 0..9 is used as switch ports, including external ports. "extport" was renamed to extint to make it clear it does not mean the port number but the external interface number.

The macros that map extint numbers to registers addresses now use inline ifs instead of binary arithmetic.

"extint" was hardcoded to 1. However, some chips have multiple external interfaces. It's not right to assume the CPU port uses extint 1 nor that all extint are CPU ports. Now the association between the port and the external interface can be defined with a device-tree port property "realtek,ext-int".

Quoted 5 lines

This patch still does not allow multiple CPU ports nor extint as a non CPU port.

Signature · 1 line
Florian
author maintainer
#28 lore raw

PATCH v4 8/11 net: dsa: realtek: rtl8365mb: use GENMASK(n-1,0) instead of BIT(n)-1

In reply to Luiz Angelo Daros de Luca
---
 drivers/net/dsa/realtek/rtl8365mb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/dsa/realtek/rtl8365mb.c +1 -1
index e115129cd5cd..b22f50a9d1ef 100644
@@ -1973,7 +1973,7 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 		mb->priv = priv;
 		mb->chip_id = chip_id;
 		mb->chip_ver = chip_ver;
-		mb->port_mask = BIT(priv->num_ports) - 1;
+		mb->port_mask = GENMASK(priv->num_ports - 1, 0);
 		mb->learn_limit_max = RTL8365MB_LEARN_LIMIT_MAX_8365MB_VC;
 		mb->jam_table = rtl8365mb_init_jam_8365mb_vc;
 		mb->jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc);
Signature · 1 line
2.34.0
Reviewed-by on v4 8/11
#29 lore raw

Re: [PATCH net-next v4 08/11] net: dsa: realtek: rtl8365mb: use GENMASK(n-1,0) instead of BIT(n)-1

Quoted 3 lines · Luiz Angelo Daros de Luca
Quoted 17 lines
---
 drivers/net/dsa/realtek/rtl8365mb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/dsa/realtek/rtl8365mb.c +1 -1
index e115129cd5cd..b22f50a9d1ef 100644
@@ -1973,7 +1973,7 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 		mb->priv = priv;
 		mb->chip_id = chip_id;
 		mb->chip_ver = chip_ver;
-		mb->port_mask = BIT(priv->num_ports) - 1;
+		mb->port_mask = GENMASK(priv->num_ports - 1, 0);
 		mb->learn_limit_max = RTL8365MB_LEARN_LIMIT_MAX_8365MB_VC;
 		mb->jam_table = rtl8365mb_init_jam_8365mb_vc;
 		mb->jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc);
author maintainer
#31 lore raw

PATCH v4 9/11 net: dsa: realtek: rtl8365mb: use DSA CPU port

In reply to Luiz Angelo Daros de Luca

Instead of a fixed CPU port, assume that DSA is correct.

---
 drivers/net/dsa/realtek/rtl8365mb.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
drivers/net/dsa/realtek/rtl8365mb.c +14 -9
index b22f50a9d1ef..adc72f0844ae 100644
@@ -103,14 +103,13 @@
 
 /* Chip-specific data and limits */
 #define RTL8365MB_CHIP_ID_8365MB_VC		0x6367
-#define RTL8365MB_CPU_PORT_NUM_8365MB_VC	6
 #define RTL8365MB_LEARN_LIMIT_MAX_8365MB_VC	2112
 
 /* Family-specific data and limits */
 #define RTL8365MB_PHYADDRMAX	7
 #define RTL8365MB_NUM_PHYREGS	32
 #define RTL8365MB_PHYREGMAX	(RTL8365MB_NUM_PHYREGS - 1)
-#define RTL8365MB_MAX_NUM_PORTS	(RTL8365MB_CPU_PORT_NUM_8365MB_VC + 1)
+#define RTL8365MB_MAX_NUM_PORTS  7
 
 /* Chip identification registers */
 #define RTL8365MB_CHIP_ID_REG		0x1300
@@ -1806,6 +1805,8 @@ static int rtl8365mb_reset_chip(struct realtek_priv *priv)
 static int rtl8365mb_setup(struct dsa_switch *ds)
 {
 	struct realtek_priv *priv = ds->priv;
+	struct rtl8365mb_cpu cpu;
+	struct dsa_port *cpu_dp;
 	struct rtl8365mb *mb;
 	int ret;
 	int i;
@@ -1833,9 +1834,16 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 		dev_info(priv->dev, "no interrupt support\n");
 
 	/* Configure CPU tagging */
-	ret = rtl8365mb_cpu_config(priv);
-	if (ret)
-		goto out_teardown_irq;
+	dsa_switch_for_each_cpu_port(cpu_dp, priv->ds) {
+		priv->cpu_port = cpu_dp->index;
+		mb->cpu.mask = BIT(priv->cpu_port);
+		mb->cpu.trap_port = priv->cpu_port;
+		ret = rtl8365mb_cpu_config(priv);
+		if (ret)
+			goto out_teardown_irq;
+
+		break;
+	}
 
 	/* Configure ports */
 	for (i = 0; i < priv->num_ports; i++) {
@@ -1967,8 +1975,7 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 			 "found an RTL8365MB-VC switch (ver=0x%04x)\n",
 			 chip_ver);
 
-		priv->cpu_port = RTL8365MB_CPU_PORT_NUM_8365MB_VC;
-		priv->num_ports = priv->cpu_port + 1;
+		priv->num_ports = RTL8365MB_MAX_NUM_PORTS;
 
 		mb->priv = priv;
 		mb->chip_id = chip_id;
@@ -1979,8 +1986,6 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 		mb->jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc);
 
 		mb->cpu.enable = 1;
-		mb->cpu.mask = BIT(priv->cpu_port);
-		mb->cpu.trap_port = priv->cpu_port;
 		mb->cpu.insert = RTL8365MB_CPU_INSERT_TO_ALL;
 		mb->cpu.position = RTL8365MB_CPU_POS_AFTER_SA;
 		mb->cpu.rx_length = RTL8365MB_CPU_RXLEN_64BYTES;
Signature · 1 line
2.34.0
maintainer
#32 lore raw

Re: [PATCH net-next v4 09/11] net: dsa: realtek: rtl8365mb: use DSA CPU port

Quoted 4 lines · On Wed, 5 Jan 2022 00:15:13 -0300 Luiz Angelo Daros de Luca wrote:

Instead of a fixed CPU port, assume that DSA is correct.

Transient build warning here:

drivers/net/dsa/realtek/rtl8365mb.c:1808:23: warning: unused variable 'cpu' [-Wunused-variable]

        struct rtl8365mb_cpu cpu;
                             ^
Reviewed-by on v4 9/11
#33 lore raw

Re: [PATCH net-next v4 09/11] net: dsa: realtek: rtl8365mb: use DSA CPU port

In reply to Luiz Angelo Daros de Luca
Quoted 4 lines · Luiz Angelo Daros de Luca

Instead of a fixed CPU port, assume that DSA is correct.

With the warning fixed:

Quoted 25 lines
---
 drivers/net/dsa/realtek/rtl8365mb.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
drivers/net/dsa/realtek/rtl8365mb.c +1 -2
index b22f50a9d1ef..adc72f0844ae 100644
@@ -103,14 +103,13 @@
 
 /* Chip-specific data and limits */
 #define RTL8365MB_CHIP_ID_8365MB_VC		0x6367
-#define RTL8365MB_CPU_PORT_NUM_8365MB_VC	6
 #define RTL8365MB_LEARN_LIMIT_MAX_8365MB_VC	2112
 
 /* Family-specific data and limits */
 #define RTL8365MB_PHYADDRMAX	7
 #define RTL8365MB_NUM_PHYREGS	32
 #define RTL8365MB_PHYREGMAX	(RTL8365MB_NUM_PHYREGS - 1)
-#define RTL8365MB_MAX_NUM_PORTS	(RTL8365MB_CPU_PORT_NUM_8365MB_VC + 1)
+#define RTL8365MB_MAX_NUM_PORTS  7
 
 /* Chip identification registers */
 #define RTL8365MB_CHIP_ID_REG		0x1300

drivers/net/dsa/realtek/rtl8365mb.c static int rtl8365mb_reset_chip(struct realtek_priv *priv) · line 1808

Quoted 4 lines
drivers/net/dsa/realtek/rtl8365mb.c +1 -0
@@ -1806,6 +1805,8 @@ static int rtl8365mb_reset_chip(struct realtek_priv *priv)
 static int rtl8365mb_setup(struct dsa_switch *ds)
 {
 	struct realtek_priv *priv = ds->priv;
+	struct rtl8365mb_cpu cpu;

Unused?

Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb.c +1 -2
+	struct dsa_port *cpu_dp;
 	struct rtl8365mb *mb;
 	int ret;
 	int i;
@@ -1833,9 +1834,16 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 		dev_info(priv->dev, "no interrupt support\n");
 
 	/* Configure CPU tagging */
-	ret = rtl8365mb_cpu_config(priv);
-	if (ret)

drivers/net/dsa/realtek/rtl8365mb.c static int rtl8365mb_setup(struct dsa_switch *ds) · line 1845

Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb.c +9 -1
-		goto out_teardown_irq;
+	dsa_switch_for_each_cpu_port(cpu_dp, priv->ds) {
+		priv->cpu_port = cpu_dp->index;
+		mb->cpu.mask = BIT(priv->cpu_port);
+		mb->cpu.trap_port = priv->cpu_port;
+		ret = rtl8365mb_cpu_config(priv);
+		if (ret)
+			goto out_teardown_irq;
+
+		break;

Maybe a comment like /* Currently, only one CPU port is supported */?

Quoted 23 lines
drivers/net/dsa/realtek/rtl8365mb.c +2 -4
+	}
 
 	/* Configure ports */
 	for (i = 0; i < priv->num_ports; i++) {
@@ -1967,8 +1975,7 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 			 "found an RTL8365MB-VC switch (ver=0x%04x)\n",
 			 chip_ver);
 
-		priv->cpu_port = RTL8365MB_CPU_PORT_NUM_8365MB_VC;
-		priv->num_ports = priv->cpu_port + 1;
+		priv->num_ports = RTL8365MB_MAX_NUM_PORTS;
 
 		mb->priv = priv;
 		mb->chip_id = chip_id;
@@ -1979,8 +1986,6 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 		mb->jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc);
 
 		mb->cpu.enable = 1;
-		mb->cpu.mask = BIT(priv->cpu_port);
-		mb->cpu.trap_port = priv->cpu_port;
 		mb->cpu.insert = RTL8365MB_CPU_INSERT_TO_ALL;
 		mb->cpu.position = RTL8365MB_CPU_POS_AFTER_SA;
 		mb->cpu.rx_length = RTL8365MB_CPU_RXLEN_64BYTES;
author maintainer
#35 lore raw

PATCH v4 10/11 net: dsa: realtek: rtl8365mb: add RTL8367S support

In reply to Luiz Angelo Daros de Luca

Realtek's RTL8367S, a 5+2 port 10/100/1000M Ethernet switch. It shares the same driver family (RTL8367C) with other models as the RTL8365MB-VC. Its compatible string is "realtek,rtl8367s".

It was tested only with MDIO interface (realtek-mdio), although it might work out-of-the-box with SMI interface (using realtek-smi).

This patch was based on an unpublished patch from Alvin Šipraga <[email protected]>.

---
 drivers/net/dsa/realtek/Kconfig        |  2 +-
 drivers/net/dsa/realtek/realtek-mdio.c |  1 +
 drivers/net/dsa/realtek/realtek-smi.c  |  4 +++
 drivers/net/dsa/realtek/rtl8365mb.c    | 38 +++++++++++++++++++-------
 4 files changed, 34 insertions(+), 11 deletions(-)
drivers/net/dsa/realtek/Kconfig +1 -1
index 73b26171fade..d0d6b5ba4bdd 100644
@@ -31,7 +31,7 @@ config NET_DSA_REALTEK_RTL8365MB
 	depends on NET_DSA_REALTEK_SMI || NET_DSA_REALTEK_MDIO
 	select NET_DSA_TAG_RTL8_4
 	help
-	  Select to enable support for Realtek RTL8365MB
+	  Select to enable support for Realtek RTL8365MB-VC and RTL8367S.
 
 config NET_DSA_REALTEK_RTL8366RB
 	tristate "Realtek RTL8366RB switch subdriver"
drivers/net/dsa/realtek/realtek-mdio.c +1 -0
index b505f4d3c5f0..2bed65981c7d 100644
@@ -199,6 +199,7 @@ static const struct of_device_id realtek_mdio_of_match[] = {
 #endif
 #if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8365MB)
 	{ .compatible = "realtek,rtl8365mb", .data = &rtl8365mb_variant, },
+	{ .compatible = "realtek,rtl8367s", .data = &rtl8365mb_variant, },
 #endif
 	{ /* sentinel */ },
 };
drivers/net/dsa/realtek/realtek-smi.c +4 -0
index 1f024e2520a6..c330a4d8ebf0 100644
@@ -513,6 +513,10 @@ static const struct of_device_id realtek_smi_of_match[] = {
 		.compatible = "realtek,rtl8365mb",
 		.data = &rtl8365mb_variant,
 	},
+	{
+		.compatible = "realtek,rtl8367s",
+		.data = &rtl8365mb_variant,
+	},
 #endif
 	{ /* sentinel */ },
 };
drivers/net/dsa/realtek/rtl8365mb.c +28 -10
index adc72f0844ae..59e08b192c06 100644
@@ -102,14 +102,19 @@
 #include "realtek.h"
 
 /* Chip-specific data and limits */
-#define RTL8365MB_CHIP_ID_8365MB_VC		0x6367
-#define RTL8365MB_LEARN_LIMIT_MAX_8365MB_VC	2112
+#define RTL8365MB_CHIP_ID_8365MB_VC	0x6367
+#define RTL8365MB_CHIP_VER_8365MB_VC	0x0040
+
+#define RTL8365MB_CHIP_ID_8367S		0x6367
+#define RTL8365MB_CHIP_VER_8367S	0x00A0
 
 /* Family-specific data and limits */
-#define RTL8365MB_PHYADDRMAX	7
-#define RTL8365MB_NUM_PHYREGS	32
-#define RTL8365MB_PHYREGMAX	(RTL8365MB_NUM_PHYREGS - 1)
-#define RTL8365MB_MAX_NUM_PORTS  7
+#define RTL8365MB_PHYADDRMAX		7
+#define RTL8365MB_NUM_PHYREGS		32
+#define RTL8365MB_PHYREGMAX		(RTL8365MB_NUM_PHYREGS - 1)
+/* RTL8370MB and RTL8310SR, possibly suportable by this driver, have 10 ports */
+#define RTL8365MB_MAX_NUM_PORTS		10
+#define RTL8365MB_LEARN_LIMIT_MAX	2112
 
 /* Chip identification registers */
 #define RTL8365MB_CHIP_ID_REG		0x1300
@@ -1971,9 +1976,22 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 
 	switch (chip_id) {
 	case RTL8365MB_CHIP_ID_8365MB_VC:
-		dev_info(priv->dev,
-			 "found an RTL8365MB-VC switch (ver=0x%04x)\n",
-			 chip_ver);
+		switch (chip_ver) {
+		case RTL8365MB_CHIP_VER_8365MB_VC:
+			dev_info(priv->dev,
+				 "found an RTL8365MB-VC switch (ver=0x%04x)\n",
+				 chip_ver);
+			break;
+		case RTL8365MB_CHIP_VER_8367S:
+			dev_info(priv->dev,
+				 "found an RTL8367S switch (ver=0x%04x)\n",
+				 chip_ver);
+			break;
+		default:
+			dev_err(priv->dev, "unrecognized switch version (ver=0x%04x)",
+				chip_ver);
+			return -ENODEV;
+		}
 
 		priv->num_ports = RTL8365MB_MAX_NUM_PORTS;
 
@@ -1981,7 +1999,7 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 		mb->chip_id = chip_id;
 		mb->chip_ver = chip_ver;
 		mb->port_mask = GENMASK(priv->num_ports - 1, 0);
-		mb->learn_limit_max = RTL8365MB_LEARN_LIMIT_MAX_8365MB_VC;
+		mb->learn_limit_max = RTL8365MB_LEARN_LIMIT_MAX;
 		mb->jam_table = rtl8365mb_init_jam_8365mb_vc;
 		mb->jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc);
 
Signature · 1 line
2.34.0
Reviewed-by on v4 10/11
#36 lore raw

Re: [PATCH net-next v4 10/11] net: dsa: realtek: rtl8365mb: add RTL8367S support

Quoted 4 lines · Luiz Angelo Daros de Luca

Realtek's RTL8367S, a 5+2 port 10/100/1000M Ethernet switch. It shares the same driver family (RTL8367C) with other models as the RTL8365MB-VC. Its compatible string is "realtek,rtl8367s".

Quoted 8 lines

It was tested only with MDIO interface (realtek-mdio), although it might work out-of-the-box with SMI interface (using realtek-smi).

This patch was based on an unpublished patch from Alvin Šipraga <[email protected]>.

Reviewed-by on v4 10/11
#37 lore raw

Re: [PATCH net-next v4 10/11] net: dsa: realtek: rtl8365mb: add RTL8367S support

In reply to Luiz Angelo Daros de Luca
Quoted 4 lines · Luiz Angelo Daros de Luca

Realtek's RTL8367S, a 5+2 port 10/100/1000M Ethernet switch. It shares the same driver family (RTL8367C) with other models as the RTL8365MB-VC. Its compatible string is "realtek,rtl8367s".

Quoted 8 lines

It was tested only with MDIO interface (realtek-mdio), although it might work out-of-the-box with SMI interface (using realtek-smi).

This patch was based on an unpublished patch from Alvin Šipraga <[email protected]>.

Signature · 1 line
Florian
author maintainer
#38 lore raw

PATCH v4 11/11 net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

In reply to Luiz Angelo Daros de Luca

Now CPU port is not limited to a single port. Also, extint can be used as non-cpu ports, as long as it defines relatek,ext-int. The last cpu port will be used as trap_port.

The CPU information was dropped from chip data as it was not used outside setup. The only other place it was used is when it wrongly checks for CPU port when it should check for extint.

realtek_priv->cpu_port is now only used by rtl8366rb.c

---
 drivers/net/dsa/realtek/rtl8365mb.c | 53 +++++++++++++++--------------
 1 file changed, 27 insertions(+), 26 deletions(-)
drivers/net/dsa/realtek/rtl8365mb.c +27 -26
index 59e08b192c06..6a00a162b2ac 100644
@@ -556,7 +556,6 @@ struct rtl8365mb_port {
  * @chip_ver: chip silicon revision
  * @port_mask: mask of all ports
  * @learn_limit_max: maximum number of L2 addresses the chip can learn
- * @cpu: CPU tagging and CPU port configuration for this chip
  * @mib_lock: prevent concurrent reads of MIB counters
  * @ports: per-port data
  * @jam_table: chip-specific initialization jam table
@@ -571,7 +570,6 @@ struct rtl8365mb {
 	u32 chip_ver;
 	u32 port_mask;
 	u32 learn_limit_max;
-	struct rtl8365mb_cpu cpu;
 	struct mutex mib_lock;
 	struct rtl8365mb_port ports[RTL8365MB_MAX_NUM_PORTS];
 	const struct rtl8365mb_jam_tbl_entry *jam_table;
@@ -769,17 +767,20 @@ static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port,
 	u32 val;
 	int ret;
 
-	if (port != priv->cpu_port) {
-		dev_err(priv->dev, "only one EXT interface is currently supported\n");
+	mb = priv->chip_data;
+	p = &mb->ports[port];
+	ext_int = p->ext_int;
+
+	if (ext_int == RTL8365MB_NOT_EXT) {
+		dev_err(priv->dev,
+			"Port %d is not identified as extenal interface.\n",
+			port);
 		return -EINVAL;
 	}
 
 	dp = dsa_to_port(priv->ds, port);
 	dn = dp->dn;
 
-	mb = priv->chip_data;
-	p = &mb->ports[port];
-	ext_int = p->ext_int;
 
 	/* Set the RGMII TX/RX delay
 	 *
@@ -859,15 +860,17 @@ static int rtl8365mb_ext_config_forcemode(struct realtek_priv *priv, int port,
 	int val;
 	int ret;
 
-	if (port != priv->cpu_port) {
-		dev_err(priv->dev, "only one EXT interface is currently supported\n");
-		return -EINVAL;
-	}
-
 	mb = priv->chip_data;
 	p = &mb->ports[port];
 	ext_int = p->ext_int;
 
+	if (ext_int == RTL8365MB_NOT_EXT) {
+		dev_err(priv->dev,
+			"Port %d is not identified as extenal interface.\n",
+			port);
+		return -EINVAL;
+	}
+
 	if (link) {
 		/* Force the link up with the desired configuration */
 		r_link = 1;
@@ -1734,10 +1737,8 @@ static void rtl8365mb_irq_teardown(struct realtek_priv *priv)
 	}
 }
 
-static int rtl8365mb_cpu_config(struct realtek_priv *priv)
+static int rtl8365mb_cpu_config(struct realtek_priv *priv, struct rtl8365mb_cpu *cpu)
 {
-	struct rtl8365mb *mb = priv->chip_data;
-	struct rtl8365mb_cpu *cpu = &mb->cpu;
 	u32 val;
 	int ret;
 
@@ -1839,11 +1840,17 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 		dev_info(priv->dev, "no interrupt support\n");
 
 	/* Configure CPU tagging */
+	cpu.mask = 0;
 	dsa_switch_for_each_cpu_port(cpu_dp, priv->ds) {
-		priv->cpu_port = cpu_dp->index;
-		mb->cpu.mask = BIT(priv->cpu_port);
-		mb->cpu.trap_port = priv->cpu_port;
-		ret = rtl8365mb_cpu_config(priv);
+		cpu.enable = 1;
+		cpu.insert = RTL8365MB_CPU_INSERT_TO_ALL;
+		cpu.position = RTL8365MB_CPU_POS_AFTER_SA;
+		cpu.rx_length = RTL8365MB_CPU_RXLEN_64BYTES;
+		cpu.format = RTL8365MB_CPU_FORMAT_8BYTES;
+		cpu.trap_port = cpu_dp->index;
+		cpu.mask |= BIT(cpu_dp->index);
+
+		ret = rtl8365mb_cpu_config(priv, &cpu);
 		if (ret)
 			goto out_teardown_irq;
 
@@ -1862,7 +1869,7 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 		dn = dsa_to_port(priv->ds, i)->dn;
 
 		/* Forward only to the CPU */
-		ret = rtl8365mb_port_set_isolation(priv, i, BIT(priv->cpu_port));
+		ret = rtl8365mb_port_set_isolation(priv, i, cpu.mask);
 		if (ret)
 			goto out_teardown_irq;
 
@@ -2003,12 +2010,6 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 		mb->jam_table = rtl8365mb_init_jam_8365mb_vc;
 		mb->jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc);
 
-		mb->cpu.enable = 1;
-		mb->cpu.insert = RTL8365MB_CPU_INSERT_TO_ALL;
-		mb->cpu.position = RTL8365MB_CPU_POS_AFTER_SA;
-		mb->cpu.rx_length = RTL8365MB_CPU_RXLEN_64BYTES;
-		mb->cpu.format = RTL8365MB_CPU_FORMAT_8BYTES;
-
 		break;
 	default:
 		dev_err(priv->dev,
Signature · 1 line
2.34.0
#39 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 9 lines · Luiz Angelo Daros de Luca

Now CPU port is not limited to a single port. Also, extint can be used as non-cpu ports, as long as it defines relatek,ext-int. The last cpu port will be used as trap_port.

The CPU information was dropped from chip data as it was not used outside setup. The only other place it was used is when it wrongly checks for CPU port when it should check for extint.

realtek_priv->cpu_port is now only used by rtl8366rb.c

Great work with this series! If I understood correctly from your last emails, you weren't actually able to test this due to hardware constraints. While I think this change is not going to introduce any surprises, I think you should still mention that it is not tested.

Some more comments below but in general the change makes sense to me.

Quoted 29 lines
---
 drivers/net/dsa/realtek/rtl8365mb.c | 53 +++++++++++++++--------------
 1 file changed, 27 insertions(+), 26 deletions(-)
drivers/net/dsa/realtek/rtl8365mb.c +0 -2
index 59e08b192c06..6a00a162b2ac 100644
@@ -556,7 +556,6 @@ struct rtl8365mb_port {
  * @chip_ver: chip silicon revision
  * @port_mask: mask of all ports
  * @learn_limit_max: maximum number of L2 addresses the chip can learn
- * @cpu: CPU tagging and CPU port configuration for this chip
  * @mib_lock: prevent concurrent reads of MIB counters
  * @ports: per-port data
  * @jam_table: chip-specific initialization jam table
@@ -571,7 +570,6 @@ struct rtl8365mb {
 	u32 chip_ver;
 	u32 port_mask;
 	u32 learn_limit_max;
-	struct rtl8365mb_cpu cpu;
 	struct mutex mib_lock;
 	struct rtl8365mb_port ports[RTL8365MB_MAX_NUM_PORTS];
 	const struct rtl8365mb_jam_tbl_entry *jam_table;
@@ -769,17 +767,20 @@ static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port,
 	u32 val;
 	int ret;

drivers/net/dsa/realtek/rtl8365mb.c static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port, · line 776

Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb.c +7 -2
 
-	if (port != priv->cpu_port) {
-		dev_err(priv->dev, "only one EXT interface is currently supported\n");
+	mb = priv->chip_data;
+	p = &mb->ports[port];
+	ext_int = p->ext_int;
+
+	if (ext_int == RTL8365MB_NOT_EXT) {
+		dev_err(priv->dev,
+			"Port %d is not identified as extenal interface.\n",

Maybe just a warning?
also: s/as extenal/as an external/

Quoted 19 lines
drivers/net/dsa/realtek/rtl8365mb.c +1 -5
+			port);
 		return -EINVAL;
 	}
 
 	dp = dsa_to_port(priv->ds, port);
 	dn = dp->dn;
 
-	mb = priv->chip_data;
-	p = &mb->ports[port];
-	ext_int = p->ext_int;
 
 	/* Set the RGMII TX/RX delay
 	 *
@@ -859,15 +860,17 @@ static int rtl8365mb_ext_config_forcemode(struct realtek_priv *priv, int port,
 	int val;
 	int ret;
 
-	if (port != priv->cpu_port) {
-		dev_err(priv->dev, "only one EXT interface is currently supported\n");

drivers/net/dsa/realtek/rtl8365mb.c static int rtl8365mb_ext_config_forcemode(struct realtek_priv *priv, int port, · line 869

Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb.c +3 -3
-		return -EINVAL;
-	}
-
 	mb = priv->chip_data;
 	p = &mb->ports[port];
 	ext_int = p->ext_int;
 
+	if (ext_int == RTL8365MB_NOT_EXT) {
+		dev_err(priv->dev,
+			"Port %d is not identified as extenal interface.\n",

ditto

drivers/net/dsa/realtek/rtl8365mb.c static void rtl8365mb_irq_teardown(struct realtek_priv *priv) · line 1740

Quoted 13 lines
drivers/net/dsa/realtek/rtl8365mb.c +5 -1
+			port);
+		return -EINVAL;
+	}
+
 	if (link) {
 		/* Force the link up with the desired configuration */
 		r_link = 1;
@@ -1734,10 +1737,8 @@ static void rtl8365mb_irq_teardown(struct realtek_priv *priv)
 	}
 }
 
-static int rtl8365mb_cpu_config(struct realtek_priv *priv)
+static int rtl8365mb_cpu_config(struct realtek_priv *priv, struct rtl8365mb_cpu *cpu)

const struct rtl8365mb_cpu?

drivers/net/dsa/realtek/rtl8365mb.c static int rtl8365mb_setup(struct dsa_switch *ds) · line 1843

Quoted 11 lines
drivers/net/dsa/realtek/rtl8365mb.c +1 -2
 {
-	struct rtl8365mb *mb = priv->chip_data;
-	struct rtl8365mb_cpu *cpu = &mb->cpu;
 	u32 val;
 	int ret;
 
@@ -1839,11 +1840,17 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 		dev_info(priv->dev, "no interrupt support\n");
 
 	/* Configure CPU tagging */
+	cpu.mask = 0;

I guess the unused cpu variable in the earlier patch belongs in this one, in which case you can just initialize it = { 0 } so that you don't need to explicitly set cpu.mask = 0.

drivers/net/dsa/realtek/rtl8365mb.c static int rtl8365mb_setup(struct dsa_switch *ds) · line 1850

Quoted 11 lines
drivers/net/dsa/realtek/rtl8365mb.c +6 -4
 	dsa_switch_for_each_cpu_port(cpu_dp, priv->ds) {
-		priv->cpu_port = cpu_dp->index;
-		mb->cpu.mask = BIT(priv->cpu_port);
-		mb->cpu.trap_port = priv->cpu_port;
-		ret = rtl8365mb_cpu_config(priv);
+		cpu.enable = 1;
+		cpu.insert = RTL8365MB_CPU_INSERT_TO_ALL;
+		cpu.position = RTL8365MB_CPU_POS_AFTER_SA;
+		cpu.rx_length = RTL8365MB_CPU_RXLEN_64BYTES;
+		cpu.format = RTL8365MB_CPU_FORMAT_8BYTES;
+		cpu.trap_port = cpu_dp->index;

If you are going to do this, perhaps it's better specified as a device tree property like the external interface index? Making the "last" CPU port the trap port is not incorrect, but it seems quite arbitrary.

drivers/net/dsa/realtek/rtl8365mb.c static int rtl8365mb_setup(struct dsa_switch *ds) · line 1853

Quoted 3 lines
drivers/net/dsa/realtek/rtl8365mb.c +3 -0
+		cpu.mask |= BIT(cpu_dp->index);
+
+		ret = rtl8365mb_cpu_config(priv, &cpu);

Shouldn't this go outside the loop to avoid potentially calling it twice in a row?

Quoted 25 lines
drivers/net/dsa/realtek/rtl8365mb.c +1 -7
 		if (ret)
 			goto out_teardown_irq;
 
@@ -1862,7 +1869,7 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 		dn = dsa_to_port(priv->ds, i)->dn;
 
 		/* Forward only to the CPU */
-		ret = rtl8365mb_port_set_isolation(priv, i, BIT(priv->cpu_port));
+		ret = rtl8365mb_port_set_isolation(priv, i, cpu.mask);
 		if (ret)
 			goto out_teardown_irq;
 
@@ -2003,12 +2010,6 @@ static int rtl8365mb_detect(struct realtek_priv *priv)
 		mb->jam_table = rtl8365mb_init_jam_8365mb_vc;
 		mb->jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc);
 
-		mb->cpu.enable = 1;
-		mb->cpu.insert = RTL8365MB_CPU_INSERT_TO_ALL;
-		mb->cpu.position = RTL8365MB_CPU_POS_AFTER_SA;
-		mb->cpu.rx_length = RTL8365MB_CPU_RXLEN_64BYTES;
-		mb->cpu.format = RTL8365MB_CPU_FORMAT_8BYTES;
-
 		break;
 	default:
 		dev_err(priv->dev,
#40 lore raw

Aw: Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Hi,

i have 2 devices here i currently try this series.

1x Bananapi R64 v0.1 (mt7622 SOC) with rtl8367s (sgmii+rgmii) - configured to use extport 2 in rgmii mode 1x Bananapi R2 Pro v0 (rk3568 SOC) with rtl8367RB (rgmii+rgmii) - configured to use extport 1 in rgmii mode

on both devices i get mdio running after additional reset in probe and ports are recognizing link up (got the real port-reg-mapping)

on r64 i get pings working but tcp (ssh, http) seems not working. on r2pro i cannot get even ping working (but rk3568 gmac seems to come up).

but i'm not deep enough in driver coding to find out whats wrong not having technical documents for checking registers to values needed.

so i need support from anyone to test it further, but devices are here ;)

regards Frank

Quoted 6 lines

Gesendet: Montag, 10. Januar 2022 um 14:39 Uhr
Von: "Alvin Šipraga" <[email protected]>
Great work with this series! If I understood correctly from your last emails, you weren't actually able to test this due to hardware constraints. While I think this change is not going to introduce any surprises, I think you should still mention that it is not tested.

#41 lore raw

Re: Aw: Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 7 lines · Frank Wunderlich

Hi,

i have 2 devices here i currently try this series.

1x Bananapi R64 v0.1 (mt7622 SOC) with rtl8367s (sgmii+rgmii) - configured to use extport 2 in rgmii mode 1x Bananapi R2 Pro v0 (rk3568 SOC) with rtl8367RB (rgmii+rgmii) - configured to use extport 1 in rgmii mode

Quoted 6 lines

on both devices i get mdio running after additional reset in probe and ports are recognizing link up (got the real port-reg-mapping)

on r64 i get pings working but tcp (ssh, http) seems not working. on r2pro i cannot get even ping working (but rk3568 gmac seems to come up).

Luiz, any comments regarding this? I suppose if the chip ID/revision is the same for both 67S and 67RB, they should work pretty much the same, right?

Quoted 2 lines

but i'm not deep enough in driver coding to find out whats wrong not having technical documents for checking registers to values needed.

Ping working but TCP not working is a bit strange. You could check the output of ethtool -S and see if that meets your expectations. If you have a relatively modern ethtool you can also append --all-groups to the comment to get a more standard output.

You can also try adjusting the RGMII TX/RX delay and pause settings - that might help for the R2 where you aren't getting any packets through.

#42 lore raw

Aw: Re: Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Hi,

Quoted 6 lines

Gesendet: Dienstag, 11. Januar 2022 um 19:17 Uhr
Von: "Alvin Šipraga" <[email protected]>

Luiz, any comments regarding this? I suppose if the chip ID/revision is the same for both 67S and 67RB, they should work pretty much the same, right?

my phy driver is same for both devices and afaik only do different RX/TX delays. With the chip-rev-patch 0x0020 i can init the switch, but have no technical documentation except the phy driver code.

Quoted 4 lines

Ping working but TCP not working is a bit strange. You could check the output of ethtool -S and see if that meets your expectations. If you have a relatively modern ethtool you can also append --all-groups to the comment to get a more standard output.

as far as i see in tcpdump (suggested by luiz) on target it is a checksum error where checksum is always 0x8382 (maybe some kind of fixed tag).

16:39:07.994825 IP (tos 0x10, ttl 64, id 54002, offset 0, flags [DF], proto TCP (6), length 60)

    192.168.1.2.43284 > 192.168.1.1.22: Flags [S], cksum 0x8382 (incorrect -> 0xa6f6), seq 3231275121, win 64240, options [mss 1460,sackOK,TS val 1615921214 ecr 0,nop,wscale 7], length 0
16:39:12.154790 IP (tos 0x10, ttl 64, id 54003, offset 0, flags [DF], proto TCP (6), length 60)
    192.168.1.2.43284 > 192.168.1.1.22: Flags [S], cksum 0x8382 (incorrect -> 0x96b6), seq 3231275121, win 64240, options [mss 1460,sackOK,TS val 1615925374 ecr 0,nop,wscale 7], length 0
Quoted 3 lines

You can also try adjusting the RGMII TX/RX delay and pause settings - that might help for the R2 where you aren't getting any packets through.

r2pro i got working by setting both delays to 0 as phy-driver does the same (after some calculation).

on r64 this is a bit more tricky, because the phy driver uses tx=1 and rx=3 with this calculation for reg-value

regData = (regData & 0xFFF0) | ((txDelay << 3) & 0x0008) | (rxDelay & 0x0007);

but in dts i need the values in picosends (?) and here i do not know how to calculate them

regards Frank

#43 lore raw

Re: Aw: Re: Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 20 lines · Frank Wunderlich

Hi,

Quoted 6 lines

Gesendet: Dienstag, 11. Januar 2022 um 19:17 Uhr
Von: "Alvin Šipraga" <[email protected]>

Luiz, any comments regarding this? I suppose if the chip ID/revision is the same for both 67S and 67RB, they should work pretty much the same, right?

my phy driver is same for both devices and afaik only do different RX/TX delays. With the chip-rev-patch 0x0020 i can init the switch, but have no technical documentation except the phy driver code.

Quoted 4 lines

Ping working but TCP not working is a bit strange. You could check the output of ethtool -S and see if that meets your expectations. If you have a relatively modern ethtool you can also append --all-groups to the comment to get a more standard output.

as far as i see in tcpdump (suggested by luiz) on target it is a checksum error where checksum is always 0x8382 (maybe some kind of fixed tag).

Quoted 8 lines

16:39:07.994825 IP (tos 0x10, ttl 64, id 54002, offset 0, flags [DF], proto TCP (6), length 60)

    192.168.1.2.43284 > 192.168.1.1.22: Flags [S], cksum 0x8382
(incorrect -> 0xa6f6), seq 3231275121, win 64240, options [mss
1460,sackOK,TS val 1615921214 ecr 0,nop,wscale 7], length 0
16:39:12.154790 IP (tos 0x10, ttl 64, id 54003, offset 0, flags [DF], proto TCP (6), length 60)
    192.168.1.2.43284 > 192.168.1.1.22: Flags [S], cksum 0x8382

(incorrect -> 0x96b6), seq 3231275121, win 64240, options [mss 1460,sackOK,TS val 1615925374 ecr 0,nop,wscale 7], length 0

That's weird, I must admit I do not recognize this issue at all. Try dumping the whole packet with -x and maybe you can see what kind of data you are getting.

Quoted 5 lines
Quoted 4 lines

You can also try adjusting the RGMII TX/RX delay and pause settings - that might help for the R2 where you aren't getting any packets through.

Quoted 8 lines

r2pro i got working by setting both delays to 0 as phy-driver does the same (after some calculation).

on r64 this is a bit more tricky, because the phy driver uses tx=1 and rx=3 with this calculation for reg-value

regData = (regData & 0xFFF0) | ((txDelay << 3) & 0x0008) | (rxDelay & 0x0007);

but in dts i need the values in picosends (?) and here i do not know how to calculate them

Try:

    tx-internal-delay-ps = <2000>;
    rx-internal-delay-ps = <1000>;

This should correspond to internal values tx=1 and rx=3.

Kind regards,
Alvin

#44 lore raw

Aw: Re: Re: Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Hi,

the problem is checksum offloading on the gmac (soc-side)

root@bpi-r64:~# ethtool -k eth1 | grep checksum                                                                                                  
rx-checksumming: on                                                                                                                              
tx-checksumming: on                                                                                                                              
        tx-checksum-ipv4: on    #<<<<<<<<<<<<<                           
        tx-checksum-ip-generic: off [fixed]                                                                                                      
        tx-checksum-ipv6: on    #<<<<<<<<<<<<<                          
        tx-checksum-fcoe-crc: off [fixed]                                                                                                        
        tx-checksum-sctp: off [fixed]

in my case i tried ipv4....and after disabling the offload i get a connection

root@bpi-r64:~# ethtool -K eth1 rx off tx off                                                                                                    
Actual changes:                                                                                                                                  
tx-checksum-ipv4: off                                                                                                                            
tx-checksum-ipv6: off                                                                                                                            
tx-tcp-segmentation: off [not requested]                                                                                                         
tx-tcp6-segmentation: off [not requested]                                                                                                        
rx-checksum: off                                                                                                                                 
root@bpi-r64:~# telnet 192.168.1.1 22                                                                                                            
Trying 192.168.1.1...                                                                                                                            
Connected to 192.168.1.1.                                                                                                                        
Escape character is '^]'.                                                                                                                        
SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.3                                                                                                          
^C

regards Frank

Quoted 37 lines

Gesendet: Donnerstag, 13. Januar 2022 um 13:37 Uhr
Von: "Alvin Šipraga" <[email protected]>
An: "Frank Wunderlich" <[email protected]>
Cc: "Luiz Angelo Daros de Luca" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>
Betreff: Re: Aw: Re: Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 20 lines · Frank Wunderlich

Hi,

Quoted 6 lines

Gesendet: Dienstag, 11. Januar 2022 um 19:17 Uhr
Von: "Alvin Šipraga" <[email protected]>

Luiz, any comments regarding this? I suppose if the chip ID/revision is the same for both 67S and 67RB, they should work pretty much the same, right?

my phy driver is same for both devices and afaik only do different RX/TX delays. With the chip-rev-patch 0x0020 i can init the switch, but have no technical documentation except the phy driver code.

Quoted 4 lines

Ping working but TCP not working is a bit strange. You could check the output of ethtool -S and see if that meets your expectations. If you have a relatively modern ethtool you can also append --all-groups to the comment to get a more standard output.

as far as i see in tcpdump (suggested by luiz) on target it is a checksum error where checksum is always 0x8382 (maybe some kind of fixed tag).

Quoted 8 lines

16:39:07.994825 IP (tos 0x10, ttl 64, id 54002, offset 0, flags [DF], proto TCP (6), length 60)

    192.168.1.2.43284 > 192.168.1.1.22: Flags [S], cksum 0x8382
(incorrect -> 0xa6f6), seq 3231275121, win 64240, options [mss
1460,sackOK,TS val 1615921214 ecr 0,nop,wscale 7], length 0
16:39:12.154790 IP (tos 0x10, ttl 64, id 54003, offset 0, flags [DF], proto TCP (6), length 60)
    192.168.1.2.43284 > 192.168.1.1.22: Flags [S], cksum 0x8382

(incorrect -> 0x96b6), seq 3231275121, win 64240, options [mss 1460,sackOK,TS val 1615925374 ecr 0,nop,wscale 7], length 0

Quoted 3 lines

That's weird, I must admit I do not recognize this issue at all. Try dumping the whole packet with -x and maybe you can see what kind of data you are getting.

2 example packets from tcpdump (if you still want to see it)

$ sudo tcpdump -i enx00131100063c -vvv -nn -x
tcpdump: listening on enx00131100063c, link-type EN10MB (Ethernet), capture size 262144 bytes

16:43:50.297259 IP (tos 0x10, ttl 64, id 19802, offset 0, flags [DF], proto TCP (6), length 60)

    192.168.1.2.38278 > 192.168.1.1.22: Flags [S], cksum 0x8382 (incorrect -> 0xb704), seq 2565260294, win 64240, options [mss 1460,sackOK,TS val 2917954112 ecr 0,nop,wscale 7], length 0
	0x0000:  4510 003c 4d5a 4000 4006 69fe c0a8 0102
	0x0010:  c0a8 0101 9586 0016 98e6 c406 0000 0000
	0x0020:  a002 faf0 8382 0000 0204 05b4 0402 080a
	0x0030:  adec 7240 0000 0000 0103 0307
16:43:51.324255 IP (tos 0x10, ttl 64, id 19803, offset 0, flags [DF], proto TCP (6), length 60)
    192.168.1.2.38278 > 192.168.1.1.22: Flags [S], cksum 0x8382 (incorrect -> 0xb300), seq 2565260294, win 64240, options [mss 1460,sackOK,TS val 2917955140 ecr 0,nop,wscale 7], length 0
	0x0000:  4510 003c 4d5b 4000 4006 69fd c0a8 0102
	0x0010:  c0a8 0101 9586 0016 98e6 c406 0000 0000
	0x0020:  a002 faf0 8382 0000 0204 05b4 0402 080a
	0x0030:  adec 7644 0000 0000 0103 0307
Quoted 13 lines
Quoted 4 lines
Quoted 3 lines

You can also try adjusting the RGMII TX/RX delay and pause settings - that might help for the R2 where you aren't getting any packets through.

Quoted 8 lines

r2pro i got working by setting both delays to 0 as phy-driver does the same (after some calculation).

on r64 this is a bit more tricky, because the phy driver uses tx=1 and rx=3 with this calculation for reg-value

regData = (regData & 0xFFF0) | ((txDelay << 3) & 0x0008) | (rxDelay & 0x0007);

but in dts i need the values in picosends (?) and here i do not know how to calculate them

Quoted 6 lines

Try:

    tx-internal-delay-ps = <2000>;
    rx-internal-delay-ps = <1000>;

This should correspond to internal values tx=1 and rx=3.

thanks i've found out and used tx=2000 and rx=900 (your 1000 is rounded to 3), but only disabling checksum-offloading fixed the problem. need to look how to make it persistent.

Afaik switch driver does not do any Checksum-handling so problem lies in the SOC ethernet driver (here i guess the mtk_soc_eth.c for mt7622). maybe i find an option to disable the offloading in dts because boards with mt7531 switch working. maybe DSA-Tag handling can be changed, but this is no breaking point from my POV.

regards Frank

author maintainer
#45 lore raw

Re: Re: Re: Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 1 line

the problem is checksum offloading on the gmac (soc-side)

I suggested it might be checksum problem because I'm also affected. In my case, I have an mt7620a SoC connected to the rtl8367s switch. The OS offloads checksum to HW but the mt7620a cannot calculate the checksum with the (EtherType) Realtek CPU Tag in place. I'll try to move the CPU tag to test if the mt7620a will then digest the frame correctly.

Regards,

#46 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 8 lines · Luiz Angelo Daros de Luca
Quoted 1 line

the problem is checksum offloading on the gmac (soc-side)

I suggested it might be checksum problem because I'm also affected. In my case, I have an mt7620a SoC connected to the rtl8367s switch. The OS offloads checksum to HW but the mt7620a cannot calculate the checksum with the (EtherType) Realtek CPU Tag in place. I'll try to move the CPU tag to test if the mt7620a will then digest the frame correctly.

You have two choices:

    enum rtl8365mb_cpu_position {
            RTL8365MB_CPU_POS_AFTER_SA = 0,
            RTL8365MB_CPU_POS_BEFORE_CRC = 1,
    };

I hardcoded it to AFTER_SA but if you find that this solves the problem for some MACs then it might be worth adding a device tree property for this to make it configurable. Of course remember to keep it backward-compatible, and add a note to future travellers in the bindings that this might solve checksum errors :-)

Kind regards,
Alvin

maintainer
#47 lore raw

Re: Re: Re: Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

In reply to Luiz Angelo Daros de Luca
Quoted 8 lines · Luiz Angelo Daros de Luca
Quoted 1 line

the problem is checksum offloading on the gmac (soc-side)

I suggested it might be checksum problem because I'm also affected. In my case, I have an mt7620a SoC connected to the rtl8367s switch. The OS offloads checksum to HW but the mt7620a cannot calculate the checksum with the (EtherType) Realtek CPU Tag in place. I'll try to move the CPU tag to test if the mt7620a will then digest the frame correctly.

Some MAC hardware you can tell it where the ether type value is in the frame. This is often used to skip over the VLAN header, but it can also be used to skip DSA headers. Check the datasheet for the hardware and see if there is anything like that.

Andrew

maintainer
#48 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 10 lines · Andrew Lunn
Quoted 8 lines · Luiz Angelo Daros de Luca
Quoted 1 line

the problem is checksum offloading on the gmac (soc-side)

I suggested it might be checksum problem because I'm also affected. In my case, I have an mt7620a SoC connected to the rtl8367s switch. The OS offloads checksum to HW but the mt7620a cannot calculate the checksum with the (EtherType) Realtek CPU Tag in place. I'll try to move the CPU tag to test if the mt7620a will then digest the frame correctly.

Quoted 6 lines

Some MAC hardware you can tell it where the ether type value is in the frame. This is often used to skip over the VLAN header, but it can also be used to skip DSA headers. Check the datasheet for the hardware and see if there is anything like that.

Andrew

And what is the problem if the hardware cannot calculate the checksum with an unknown EtherType? Is it the DSA master that drops the packets in hardware? What is the reported error counter?

author maintainer
#49 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 3 lines

And what is the problem if the hardware cannot calculate the checksum with an unknown EtherType? Is it the DSA master that drops the packets in hardware? What is the reported error counter?

No, the issue is with outgoing packets and nothing is dropped inside the DSA device.

If the OS is configured to offload (I'm using OpenWrt.), it will send a packet with the wrong checksum expecting that the HW will fix that. After DSA is brought up, the OS is still expecting the HW to calculate the checksums. However, with the EtherType DSA tag from a , it cannot understand it anymore, leaving the checksum as is. The DSA switch (Realtek) passes the packet to the network and the other end receives a broken packet. Maybe if the DSA knew that the CPU Ethernet HW cannot handle that DSA tag, it could disable checksums by default. But it is difficult to foresee how each offload HW will digest each type of CPU tag.

Is the kernel enabling checksum by default when the driver reports it is supported? If so, it would be nice to somehow disable offloading with some kind of device-tree dsa cpu port property.

Regards,

Luiz

maintainer
#50 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 6 lines · Luiz Angelo Daros de Luca
Quoted 3 lines

And what is the problem if the hardware cannot calculate the checksum with an unknown EtherType? Is it the DSA master that drops the packets in hardware? What is the reported error counter?

No, the issue is with outgoing packets and nothing is dropped inside the DSA device.

Ah, sorry, I missed that.

Quoted 11 lines

If the OS is configured to offload (I'm using OpenWrt.), it will send a packet with the wrong checksum expecting that the HW will fix that. After DSA is brought up, the OS is still expecting the HW to calculate the checksums. However, with the EtherType DSA tag from a , it cannot understand it anymore, leaving the checksum as is. The DSA switch (Realtek) passes the packet to the network and the other end receives a broken packet. Maybe if the DSA knew that the CPU Ethernet HW cannot handle that DSA tag, it could disable checksums by default. But it is difficult to foresee how each offload HW will digest each type of CPU tag.

Quoted 3 lines

Is the kernel enabling checksum by default when the driver reports it is supported? If so, it would be nice to somehow disable offloading with some kind of device-tree dsa cpu port property.

:) device tree properties are not the fix for everything!

I think I know what the problem is. But I'd need to know what the driver for the DSA master is, to confirm. To be precise, what I'd like to check is the value of master->vlan_features.

author maintainer
#51 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 1 line

:) device tree properties are not the fix for everything!

I'm still getting used to it ;-)

In this thread, Alvin suggested adding a new property to define which port will be used as trap_port instead of using the last CPU port. Should I try something different?

        switch1 {
               compatible = "realtek,rtl8367s";
               reg = <29>;

               realtek,trap-port = <&port7>;

               ports {
                        ....
                        port7: port@7 {
                            ...
                       };
        };

Should I do something differently?

Quoted 3 lines

I think I know what the problem is. But I'd need to know what the driver for the DSA master is, to confirm. To be precise, what I'd like to check is the value of master->vlan_features.

Here it is 0x1099513266227 (I hope). Oh, this DSA driver still does not implement vlan nor bridge offload. Maybe it would matter.

Regards,

Luiz

#52 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 21 lines · Luiz Angelo Daros de Luca
Quoted 1 line

:) device tree properties are not the fix for everything!

I'm still getting used to it ;-)

In this thread, Alvin suggested adding a new property to define which port will be used as trap_port instead of using the last CPU port. Should I try something different?

         switch1 {
                compatible = "realtek,rtl8367s";
                reg = <29>;

                realtek,trap-port = <&port7>;

                ports {
                         ....
                         port7: port@7 {
                             ...
                        };
         };
Quoted 8 lines

Should I do something differently?

Quoted 3 lines

I think I know what the problem is. But I'd need to know what the driver for the DSA master is, to confirm. To be precise, what I'd like to check is the value of master->vlan_features.

Here it is 0x1099513266227 (I hope). Oh, this DSA driver still does not implement vlan nor bridge offload. Maybe it would matter.

Are we talking about an in tree driver? If so which is it?

Signature · 1 line
Florian
author maintainer
#53 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 1 line

Are we talking about an in tree driver? If so which is it?

Yes, the one the patch touches: rtl8365mb.

My device uses a mt7620a SoC and traffic passes through its mt7530 switch with vlan disabled before reaching the realtek switch. It still loads a swconfig driver but I think it might work without one. I just didn't stop to try it yet.

#54 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 3 lines · Luiz Angelo Daros de Luca
Quoted 1 line

Are we talking about an in tree driver? If so which is it?

Yes, the one the patch touches: rtl8365mb.

I meant the DSA master network device, but you answered that, it uses a mt7260a SoC, but there is no Ethernet driver upstream for it yet?

git grep ralink,mt7620-gsw * Documentation/devicetree/bindings/net/mediatek,mt7620-gsw.txt: compatible = "ralink,mt7620-gsw";

Quoted 4 lines

My device uses a mt7620a SoC and traffic passes through its mt7530 switch with vlan disabled before reaching the realtek switch. It still loads a swconfig driver but I think it might work without one.

Ah so you have a cascade of switches here, that could confuse your Ethernet MAC. Do you have a knob to adjust where to calculate the checksum from, say a L2 or L3 offset for instance?

Signature · 1 line
Florian
author maintainer
#55 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 12 lines
Quoted 3 lines
Quoted 1 line

Are we talking about an in tree driver? If so which is it?

Yes, the one the patch touches: rtl8365mb.

I meant the DSA master network device, but you answered that, it uses a mt7260a SoC, but there is no Ethernet driver upstream for it yet?

git grep ralink,mt7620-gsw * Documentation/devicetree/bindings/net/mediatek,mt7620-gsw.txt:
compatible = "ralink,mt7620-gsw";

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

My device uses a mt7620a SoC and traffic passes through its mt7530 switch with vlan disabled before reaching the realtek switch. It still loads a swconfig driver but I think it might work without one.

Ah so you have a cascade of switches here, that could confuse your Ethernet MAC. Do you have a knob to adjust where to calculate the checksum from, say a L2 or L3 offset for instance?

Not that I could find in any docs. I just found registers to set it on and off. However, Realtek supports two locations for the CPU tag. I'll try the RTL8365MB_CPU_POS_BEFORE_CRC and hope the checksum will work as expected. But I might leave that test for a moment after this series is solved.

Quoted 2 lines

--
Florian

#56 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

In reply to Florian Fainelli
Quoted 13 lines · Florian Fainelli
Quoted 3 lines · Luiz Angelo Daros de Luca
Quoted 1 line

Are we talking about an in tree driver? If so which is it?

Yes, the one the patch touches: rtl8365mb.

I meant the DSA master network device, but you answered that, it uses a mt7260a SoC, but there is no Ethernet driver upstream for it yet?

git grep ralink,mt7620-gsw * Documentation/devicetree/bindings/net/mediatek,mt7620-gsw.txt: compatible = "ralink,mt7620-gsw";

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

My device uses a mt7620a SoC and traffic passes through its mt7530 switch with vlan disabled before reaching the realtek switch. It still loads a swconfig driver but I think it might work without one.

Ah so you have a cascade of switches here, that could confuse your Ethernet MAC. Do you have a knob to adjust where to calculate the checksum from, say a L2 or L3 offset for instance?

The company I currently work for has got their own mt7621a board with an external rtl8367s switch.

According to Documentation/devicetree/bindings/net/dsa/mt7530.txt I can either connect the rtl switch directly to the second GMAC of the mt7621 SoC or to MT7530's GMAC5 to create a cascade.

I've been running gregkh/staging staging-next branch but I can't seem to have traffic flow on the RGMII2 bus which is shared by the 2nd GMAC of the SoC, MT7530's GMAC5 and an external phy (rtl switch in this case).

None of the documented configurations work:
PHY0/4 <-> 2nd GMAC
External phy <-> 2nd GMAC
External phy <-> MT7530's GMAC5

Arınç

maintainer
#57 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

In reply to Luiz Angelo Daros de Luca
Quoted 21 lines · Luiz Angelo Daros de Luca
Quoted 1 line

:) device tree properties are not the fix for everything!

I'm still getting used to it ;-)

In this thread, Alvin suggested adding a new property to define which port will be used as trap_port instead of using the last CPU port. Should I try something different?

        switch1 {
               compatible = "realtek,rtl8367s";
               reg = <29>;

               realtek,trap-port = <&port7>;

               ports {
                        ....
                        port7: port@7 {
                            ...
                       };
        };
Quoted 1 line

Should I do something differently?

To clarify, I don't know what a trap_port is. I just saw this description in rtl8365mb.c:

* @trap_port: forward trapped frames to this port

but I still don't know to which packets does this configuration apply (where are the packet traps installed, and for what kind of packets).

Speculating here, but it appears quite arbitrary, and I'd guess also broken, to make the trap_port the last CPU port. Is this also part of the things which you didn't really test? See commit 8d5f7954b7c8 ("net:
dsa: felix: break at first CPU port during init and teardown") for a similar issue with this. When there are multiple 'ethernet = <&phandle>' properties in the device tree, DSA makes the owners of all those phandles a DSA master, and all those switch ports as CPU ports. But out of all those CPU ports, only the first one is an active CPU port. The others have no dp->cpu_dp pointing to them.
See dsa_tree_setup_default_cpu() -> dsa_tree_find_first_cpu(). Even when DSA gets full-blown support for multiple CPU ports, I think it's safe to say that this default will remain the way it is: a single CPU port will be active to begin with: the first one. Given that fact (and depending on what you need to do with the trap_port info exactly), it might be broken to set as the trap port a CPU port that isn't used. Stuff like dsa_port_host_fdb_add()/dsa_port_host_fdb_del() will be broken, because they rely on the dp->cpu_dp association, and dp->cpu_dp->index will be != trap_port.

Quoted 5 lines
Quoted 3 lines

I think I know what the problem is. But I'd need to know what the driver for the DSA master is, to confirm. To be precise, what I'd like to check is the value of master->vlan_features.

Here it is 0x1099513266227 (I hope).

That's quite an extraordinary set of vlan_features. In that number, I notice BIT(2) is set, which corresponds to __UNUSED_NETIF_F_1. So it probably isn't correctly printed.

This is what I would have liked to see:

net/dsa/slave.c +1 -0
index 22241afcac81..b41f1b414c69 100644
@@ -1909,6 +1909,7 @@ void dsa_slave_setup_tagger(struct net_device *slave)
 	p->xmit = cpu_dp->tag_ops->xmit;
 
 	slave->features = master->vlan_features | NETIF_F_HW_TC;
+	netdev_err(slave, "master %s vlan_features 0x%llx\n", master->name, master->vlan_features);
 	slave->hw_features |= NETIF_F_HW_TC;
 	slave->features |= NETIF_F_LLTX;
 	if (slave->needed_tailroom)

And I don't think you fully answered Florian's questions either, really. Can we see the a link to the code of the Ethernet controller whose role is to be a host port (DSA master) for the rtl8365mb switch? If that DSA master is a DSA switch itself, could you please unroll the chain all the way with more links to drivers? No matter whether upstream or downstream, just what you use.

I hate to guess, but since both you and Arınç have mentioned the mt7620a/mt7621 SoCs, I'd guess that the top-most DSA driver in both cases is "mediatek,eth-mac" (drivers/net/ethernet/mediatek/mtk_eth_soc.c). If so, this would confirm my suspicions, since it sets its vlan_features to include NETIF_F_IP_CSUM and NETIF_F_IPV6_CSUM. Please confirm that master->vlan_features contains these 2 bits.

Quoted 2 lines

Oh, this DSA driver still does not implement vlan nor bridge offload. Maybe it would matter.

It doesn't matter. The vlan_features is a confusing name for what it really does here. I'll explain in a bit once you clarify the other things I asked for.

author maintainer
#58 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 21 lines
Quoted 19 lines

I'm still getting used to it ;-)

In this thread, Alvin suggested adding a new property to define which port will be used as trap_port instead of using the last CPU port. Should I try something different?

        switch1 {
               compatible = "realtek,rtl8367s";
               reg = <29>;

               realtek,trap-port = <&port7>;

               ports {
                        ....
                        port7: port@7 {
                            ...
                       };
        };
Quoted 1 line

Should I do something differently?

Quoted 7 lines

To clarify, I don't know what a trap_port is. I just saw this description in rtl8365mb.c:

* @trap_port: forward trapped frames to this port

but I still don't know to which packets does this configuration apply (where are the packet traps installed, and for what kind of packets).

Thank you, Vladimir.

trap_port seems to be where the switch will send any packet captured from LAN ports. There are a couple of situations it will be used like:
1) untagged or unmatched vlan packets (if configured to do so)
2) some multicasting packets (Reserved Multicast Address), for some cases like capturing STP or LACP
3) IGMP and 802.1X EAPOL packets
4) Switch ACL rules that could match a packet and send it to the trap port.

In my early tests, I only saw some IGMP packets trapped to CPU. I also do not know how important they are.

Quoted 10 lines

Speculating here, but it appears quite arbitrary, and I'd guess also broken, to make the trap_port the last CPU port. Is this also part of the things which you didn't really test? See commit 8d5f7954b7c8 ("net:
dsa: felix: break at first CPU port during init and teardown") for a similar issue with this. When there are multiple 'ethernet = <&phandle>' properties in the device tree, DSA makes the owners of all those phandles a DSA master, and all those switch ports as CPU ports. But out of all those CPU ports, only the first one is an active CPU port. The others have no dp->cpu_dp pointing to them.
See dsa_tree_setup_default_cpu() -> dsa_tree_find_first_cpu().

Quoted 8 lines

Even when DSA gets full-blown support for multiple CPU ports, I think it's safe to say that this default will remain the way it is: a single CPU port will be active to begin with: the first one. Given that fact (and depending on what you need to do with the trap_port info exactly), it might be broken to set as the trap port a CPU port that isn't used. Stuff like dsa_port_host_fdb_add()/dsa_port_host_fdb_del() will be broken, because they rely on the dp->cpu_dp association, and dp->cpu_dp->index will be != trap_port.

Although it would be interesting to have some sniffed traffic sent to a second CPU port, I agree it might break more things than it will help. Until multiple CPU ports can be used as first-class citizens, I'll simply force it to be the first CPU port.

The multiple CPU port is not a target but a byproduct of removing the assumption that "CPU port" is equal to "external interface port". The real change is to allow an external interface to be configured, even if it is not the CPU port, as it could be used to stack a second switch.
I'll leave the multiple CPU as a note in the commit message and not the subject. It was wrong to emphasize that.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

I think I know what the problem is. But I'd need to know what the driver for the DSA master is, to confirm. To be precise, what I'd like to check is the value of master->vlan_features.

Here it is 0x1099513266227 (I hope).

That's quite an extraordinary set of vlan_features. In that number, I notice BIT(2) is set, which corresponds to __UNUSED_NETIF_F_1. So it probably isn't correctly printed.

Oh my... I printed it as an unsigned decimal. Sorry.

Quoted 8 lines

This is what I would have liked to see:

net/dsa/slave.c +0 -0
index 22241afcac81..b41f1b414c69 100644

net/dsa/slave.c void dsa_slave_setup_tagger(struct net_device *slave) · line 1915

Quoted 7 lines
net/dsa/slave.c +1 -0
@@ -1909,6 +1909,7 @@ void dsa_slave_setup_tagger(struct net_device *slave)
        p->xmit = cpu_dp->tag_ops->xmit;
 
        slave->features = master->vlan_features | NETIF_F_HW_TC;
+       netdev_err(slave, "master %s vlan_features 0x%llx\n", master->name, master->vlan_features);
        slave->hw_features |= NETIF_F_HW_TC;
        slave->features |= NETIF_F_LLTX;
        if (slave->needed_tailroom)

0x10000190033. If I got it right:

NETIF_F_SG_BIT
NETIF_F_IP_CSUM_BIT
NETIF_F_IPV6_CSUM_BIT
NETIF_F_HIGHDMA_BIT
NETIF_F_GSO_SHIFT
NETIF_F_TSO_MANGLEID_BIT
NETIF_F_TSO6_BIT
NETIF_F_RXCSUM_BIT

Quoted 3 lines

And I don't think you fully answered Florian's questions either, really. Can we see the a link to the code of the Ethernet controller whose role is to be a host port (DSA master) for the rtl8365mb switch?

The code is from the OpenWrt tree.
https://github.com/openwrt/openwrt/tree/master/target/linux/ramips/files/drivers/net/ethernet/ralink

I only patched it to accept Jumbo Frames (it was dropping incoming packets with MTU 1508)
https://patchwork.ozlabs.org/project/openwrt/list/?series=279773

Quoted 4 lines

If that DSA
master is a DSA switch itself, could you please unroll the chain all the way with more links to drivers? No matter whether upstream or downstream, just what you use.

OpenWrt (soc mt7620a) eth0 (mtk_eth_soc) connected to internal SoC MT7530 switch port 6 (, mediatek,mt7620-gsw).
MT7530 port 5 connected to RTL8367S port 7 (RGMII).

The internal SoC switch is behaving as an unmanaged switch, with no vlans. It would be just extra overhead to have it working as a DSA switch, specially
as those two switches tags are not compatible. I still have the swconfig driver installed but I was only using it for some debugging (checking metrics). I think that the state the bootloader leaves that switchis enough to make it forward packets to the Realtek switch. In device-tree conf, I'm directly using that eth0 as the CPU port.

Quoted 2 lines

I hate to guess, but since both you and Arınç have mentioned the mt7620a/mt7621 SoCs,

Sorry for the incomplete answer. If it helps, this is my device
https://github.com/luizluca/openwrt/blob/tplink_c5v4_dsa/target/linux/ramips/dts/mt7620a_tplink_archer-c5-v4.dts

I try to keep my remote branch updated, although it has some dirty changes:
https://github.com/luizluca/openwrt/tree/tplink_c5v4_dsa

Quoted 1 line

I'd guess that the top-most DSA driver in both cases is "mediatek,eth-mac" (drivers/net/ethernet/mediatek/mtk_eth_soc.c).

Not in my case. The driver I use also supports mt7621 but the upstream driver skipped the mt7620a support.

Quoted 3 lines

If so, this would confirm my suspicions, since it sets its vlan_features to include NETIF_F_IP_CSUM and NETIF_F_IPV6_CSUM. Please confirm that master->vlan_features contains these 2 bits.

Yes.

Quoted 6 lines
Quoted 2 lines

Oh, this DSA driver still does not implement vlan nor bridge offload. Maybe it would matter.

It doesn't matter. The vlan_features is a confusing name for what it really does here. I'll explain in a bit once you clarify the other things I asked for.

That is good news as we can deal with it independently. I wish to focus on that afterwards.

Regards,

Luiz

maintainer
#59 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 16 lines · Luiz Angelo Daros de Luca

The code is from the OpenWrt tree.
https://github.com/openwrt/openwrt/tree/master/target/linux/ramips/files/drivers/net/ethernet/ralink

I only patched it to accept Jumbo Frames (it was dropping incoming packets with MTU 1508)
https://patchwork.ozlabs.org/project/openwrt/list/?series=279773

Quoted 4 lines

If that DSA
master is a DSA switch itself, could you please unroll the chain all the way with more links to drivers? No matter whether upstream or downstream, just what you use.

OpenWrt (soc mt7620a) eth0 (mtk_eth_soc) connected to internal SoC MT7530 switch port 6 (, mediatek,mt7620-gsw).
MT7530 port 5 connected to RTL8367S port 7 (RGMII).

Quoted 8 lines

The internal SoC switch is behaving as an unmanaged switch, with no vlans. It would be just extra overhead to have it working as a DSA switch, specially
as those two switches tags are not compatible. I still have the swconfig driver installed but I was only using it for some debugging (checking metrics). I think that the state the bootloader leaves that switchis enough to make it forward packets to the Realtek switch. In device-tree conf, I'm directly using that eth0 as the CPU port.

There could be value in managing the internal switch with DSA too, for example in a situation like this:

 +-------------------------------------------------+
 |  SoC                                            |
 |                                                 |
 |  +----------------+--------+---------------+    |
 |  |                |        |               |    |
 |  | Internal       |        |               |    |
 |  |  switch        +--------+               |    |
 |  | (dsa,member = <0 0>;)                   |    |
 |  | +-------+ +-------+ +-------+ +-------+ |    |
 |  | |       | |       | |       | |       | |    |
 |  | | sw0p0 | | sw0p1 | | sw0p2 | | sw0p3 | |    |
 |  | |       | |       | |       | |       | |    |
 +--+-+-------+-+-------+-+-------+-+-------+-+----+
 +----+--------+------------------+
 |    |        |                  |
 |    +--------+                  |
 | External switch                |
 | (dsa,member = <1 0>;)          |
 |  +-------+ +-------+ +-------+ |
 |  |       | |       | |       | |
 |  | sw1p0 | | sw1p1 | | sw1p2 | |
 |  |       | |       | |       | |
 +--+-------+-+-------+-+-------+-+

where you'd create a bridge spanning all of sw0p1, sw0p2, sw0p3, sw1p0, sw1p1, sw1p2. Forwarding between the internal and the external switch is done in software, and that deals with the "impedance matching" between the tagging protocols too - first the packet is stripped of the DSA tag of the ingress switch, then the DSA tag of the egress switch is added. With a transparent internal switch (no driver), ports sw0p1, sw0p2, sw0p3 are dead, since if you'd connect them to a PHY, they'd spit out DSA-tagged packets from the external switch.

Quoted 11 lines
Quoted 2 lines

I hate to guess, but since both you and Arınç have mentioned the mt7620a/mt7621 SoCs,

Sorry for the incomplete answer. If it helps, this is my device
https://github.com/luizluca/openwrt/blob/tplink_c5v4_dsa/target/linux/ramips/dts/mt7620a_tplink_archer-c5-v4.dts

I try to keep my remote branch updated, although it has some dirty changes:
https://github.com/luizluca/openwrt/tree/tplink_c5v4_dsa

Quoted 1 line

I'd guess that the top-most DSA driver in both cases is "mediatek,eth-mac" (drivers/net/ethernet/mediatek/mtk_eth_soc.c).

Quoted 8 lines

Not in my case. The driver I use also supports mt7621 but the upstream driver skipped the mt7620a support.

Quoted 3 lines

If so, this would confirm my suspicions, since it sets its vlan_features to include NETIF_F_IP_CSUM and NETIF_F_IPV6_CSUM. Please confirm that master->vlan_features contains these 2 bits.

Yes.

Ok. See the discussion with Lino Sanfilippo here:
https://lore.kernel.org/netdev/YPAzZXaC%[email protected]/
Basically, the moving parts of this mechanism are:

- when the DSA master doesn't understand DSA tags, the transmit checksums must be calculated in software.

- this is already supported, we just need to make sure that the DSA slave->features does not include any checksum offload bits (NETIF_F_HW_CSUM, NETIF_F_IP_CSUM, NETIF_F_IPV6_CSUM), otherwise that will be delegated to the device driver. The place that checks that condition and calculates the checksum in software is validate_xmit_skb() -> skb_csum_hwoffload_help().

- the checksum is evaluated on the skb before the DSA tag is even inserted, and is preserved when DSA inserts the header, and is therefore still correct by the time the skb reaches the DSA master driver. A DSA-unaware master doesn't have to do anything for this packet, the IP header checksum will still be correct despite the hardware not recognizing the IP header.

- the way DSA populates slave->features is by inheriting master->vlan_features (vlan_features means "netdev features which are inheritable by VLAN upper interfaces"). This directly makes or breaks what happens in validate_xmit_skb() on a DSA slave interface.

- the problem occurs when the DSA master puts checksum offload bits in both dev->features and dev->vlan_features. The master thinks this
means: "I can offload IP checksumming for myself and for VLAN upper interfaces (I can recognize the IP header past the VLAN header)." Little does it know that DSA assumes this means it can also offload checksumming in the presence of switch tags.

So just stop inheriting NETIF_F_HW_CSUM and friends from master->vlan_features, right?

Well, you can't help but wonder a bit how come it's 2022 and we could still have an obvious omission like that? And at the same time: but why does the mt7530 DSA driver work with the same DSA master, but not rtl8365mb? The answer to both, I think, is "some DSA masters do understand a particular DSA switch tag, particularly the one from the same vendor". So if we stop inheriting the checksum offload bits from vlan_features, we introduce a performance regression for those.

We should instead ask the DSA master "for this DSA tagging protocol, what netdev features can DSA inherit"? Because of the variability per tagging protocol, this probably needs to be done through a new netdev operation, I don't know of any cleaner way.
The complicated part is that we'd need to correctly identify the pairs of DSA master drivers and tagging protocols where some features can be safely inherited. Then, it's not too clear whether we want this new ndo to cover other functionality as well, or if netdev features are enough.

So the sad news for you is that this is pretty much "net-next" material, even if it fixes what is essentially a design shortcoming. If we're quick, we could start doing this right as net-next reopens, and that would give other developers maximum opportunity to fix up the performance regressions caused by lack of TX checksumming.

Quoted 13 lines
Quoted 6 lines
Quoted 2 lines

Oh, this DSA driver still does not implement vlan nor bridge offload. Maybe it would matter.

It doesn't matter. The vlan_features is a confusing name for what it really does here. I'll explain in a bit once you clarify the other things I asked for.

That is good news as we can deal with it independently. I wish to focus on that afterwards.

Regards,

Luiz

author maintainer
#60 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 37 lines
Quoted 8 lines

The internal SoC switch is behaving as an unmanaged switch, with no vlans. It would be just extra overhead to have it working as a DSA switch, specially
as those two switches tags are not compatible. I still have the swconfig driver installed but I was only using it for some debugging (checking metrics). I think that the state the bootloader leaves that switchis enough to make it forward packets to the Realtek switch. In device-tree conf, I'm directly using that eth0 as the CPU port.

There could be value in managing the internal switch with DSA too, for example in a situation like this:

 +-------------------------------------------------+
 |  SoC                                            |
 |                                                 |
 |  +----------------+--------+---------------+    |
 |  |                |        |               |    |
 |  | Internal       |        |               |    |
 |  |  switch        +--------+               |    |
 |  | (dsa,member = <0 0>;)                   |    |
 |  | +-------+ +-------+ +-------+ +-------+ |    |
 |  | |       | |       | |       | |       | |    |
 |  | | sw0p0 | | sw0p1 | | sw0p2 | | sw0p3 | |    |
 |  | |       | |       | |       | |       | |    |
 +--+-+-------+-+-------+-+-------+-+-------+-+----+
 +----+--------+------------------+
 |    |        |                  |
 |    +--------+                  |
 | External switch                |
 | (dsa,member = <1 0>;)          |
 |  +-------+ +-------+ +-------+ |
 |  |       | |       | |       | |
 |  | sw1p0 | | sw1p1 | | sw1p2 | |
 |  |       | |       | |       | |
 +--+-------+-+-------+-+-------+-+
Quoted 8 lines

where you'd create a bridge spanning all of sw0p1, sw0p2, sw0p3, sw1p0, sw1p1, sw1p2. Forwarding between the internal and the external switch is done in software, and that deals with the "impedance matching" between the tagging protocols too - first the packet is stripped of the DSA tag of the ingress switch, then the DSA tag of the egress switch is added. With a transparent internal switch (no driver), ports sw0p1, sw0p2, sw0p3 are dead, since if you'd connect them to a PHY, they'd spit out DSA-tagged packets from the external switch.

Oh, any other internal switch ports are physically not in use. Those ports are 10/100. I think that in my device, some of its pins are even used as GPIO.
And the offload issue will remain as the HW will not be able to offload the second layer of DSA tag.

Quoted 64 lines
Quoted 11 lines
Quoted 2 lines

I hate to guess, but since both you and Arınç have mentioned the mt7620a/mt7621 SoCs,

Sorry for the incomplete answer. If it helps, this is my device
https://github.com/luizluca/openwrt/blob/tplink_c5v4_dsa/target/linux/ramips/dts/mt7620a_tplink_archer-c5-v4.dts

I try to keep my remote branch updated, although it has some dirty changes:
https://github.com/luizluca/openwrt/tree/tplink_c5v4_dsa

Quoted 1 line

I'd guess that the top-most DSA driver in both cases is "mediatek,eth-mac" (drivers/net/ethernet/mediatek/mtk_eth_soc.c).

Quoted 8 lines

Not in my case. The driver I use also supports mt7621 but the upstream driver skipped the mt7620a support.

Quoted 3 lines

If so, this would confirm my suspicions, since it sets its vlan_features to include NETIF_F_IP_CSUM and NETIF_F_IPV6_CSUM. Please confirm that master->vlan_features contains these 2 bits.

Yes.

Ok. See the discussion with Lino Sanfilippo here:
https://lore.kernel.org/netdev/YPAzZXaC%[email protected]/
Basically, the moving parts of this mechanism are:

- when the DSA master doesn't understand DSA tags, the transmit checksums must be calculated in software.

- this is already supported, we just need to make sure that the DSA slave->features does not include any checksum offload bits (NETIF_F_HW_CSUM, NETIF_F_IP_CSUM, NETIF_F_IPV6_CSUM), otherwise that will be delegated to the device driver. The place that checks that condition and calculates the checksum in software is validate_xmit_skb() -> skb_csum_hwoffload_help().

- the checksum is evaluated on the skb before the DSA tag is even inserted, and is preserved when DSA inserts the header, and is therefore still correct by the time the skb reaches the DSA master driver. A DSA-unaware master doesn't have to do anything for this packet, the IP header checksum will still be correct despite the hardware not recognizing the IP header.

- the way DSA populates slave->features is by inheriting master->vlan_features (vlan_features means "netdev features which are inheritable by VLAN upper interfaces"). This directly makes or breaks what happens in validate_xmit_skb() on a DSA slave interface.

- the problem occurs when the DSA master puts checksum offload bits in both dev->features and dev->vlan_features. The master thinks this
means: "I can offload IP checksumming for myself and for VLAN upper interfaces (I can recognize the IP header past the VLAN header)." Little does it know that DSA assumes this means it can also offload checksumming in the presence of switch tags.

So just stop inheriting NETIF_F_HW_CSUM and friends from master->vlan_features, right?

Well, you can't help but wonder a bit how come it's 2022 and we could still have an obvious omission like that? And at the same time: but why does the mt7530 DSA driver work with the same DSA master, but not rtl8365mb? The answer to both, I think, is "some DSA masters do understand a particular DSA switch tag, particularly the one from the same vendor". So if we stop inheriting the checksum offload bits from vlan_features, we introduce a performance regression for those.

Quoted 8 lines

We should instead ask the DSA master "for this DSA tagging protocol, what netdev features can DSA inherit"? Because of the variability per tagging protocol, this probably needs to be done through a new netdev operation, I don't know of any cleaner way.
The complicated part is that we'd need to correctly identify the pairs of DSA master drivers and tagging protocols where some features can be safely inherited. Then, it's not too clear whether we want this new ndo to cover other functionality as well, or if netdev features are enough.

I'm new to DSA but I think that a solution like that might not scale well. For every possible master network driver, it needs to know if its offload feature will handle every different tag. Imagining that both new offload HW and new switch tags will still appear in the kernel, it might be untreatable.

I know dsa properties are not the solution for everything (and I'm still adapting to where that border is) but, in this case, it is a device specific arrangement between the ethernet device and the switch. Wouldn't it be better to allow the
one writing the device-tree description inform if a master feature cannot be copied to slave devices?

I checked DSA doc again and it says:

"Since tagging protocols in category 1 and 2 break software (and most often also hardware) packet dissection on the DSA master, features such as RPS (Receive Packet Steering) on the DSA master would be broken. The DSA framework deals with this by hooking into the flow dissector and shifting the offset at which the IP header is to be found in the tagged frame as seen by the DSA master. This behavior is automatic based on the overhead value of the tagging protocol. If not all packets are of equal size, the tagger can implement the flow_dissect method of the struct dsa_device_ops and override this default behavior by specifying the correct offset incurred by each individual RX packet. Tail taggers do not cause issues to the flow dissector."

It makes me think that it is the master network driver that does not implement that IP header location shift. Anyway, I believe it also depends on HW capabilities to inform that shift, right?

I'm trying to think as a DSA newbie (which is exactly what I am). Differently from an isolated ethernet driver, with DSA, the system does have control of "something" after the offload should be applied:
the dsa switch. Can't we have a generic way to send a packet to the switch and make it bounce back to the CPU (passing through the offload engine)? Would it work if I set the destination port as the CPU port? This way, we could simply detect if the offload worked and disable those features that did not work. It could work with a generic implementation or, if needed, a specialized optional ds_switch_ops function just to setup that temporary lookback forwarding rule.

Quoted 5 lines

So the sad news for you is that this is pretty much "net-next" material, even if it fixes what is essentially a design shortcoming. If we're quick, we could start doing this right as net-next reopens, and that would give other developers maximum opportunity to fix up the performance regressions caused by lack of TX checksumming.

No problem. I'm already playing the long game. I'm just trying to fix a device I own using my free time and I don't have any manager with impossible deadlines.
However, any solution with a performance regression would break the kernel API. I would rather add a new device-tree option :-)

Quoted 13 lines
Quoted 13 lines
Quoted 6 lines
Quoted 2 lines

Oh, this DSA driver still does not implement vlan nor bridge offload. Maybe it would matter.

It doesn't matter. The vlan_features is a confusing name for what it really does here. I'll explain in a bit once you clarify the other things I asked for.

That is good news as we can deal with it independently. I wish to focus on that afterwards.

Regards,

Luiz

maintainer
#61 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 3 lines · Luiz Angelo Daros de Luca

I'm new to DSA but I think that a solution like that might not scale well. For every possible master network driver, it needs to know if its offload feature will handle every different tag.

Correct, with the sensible default being that no checksum offloading in the presence of DSA tags is supported.

Quoted 2 lines

Imagining that both new offload HW and new switch tags will still appear in the kernel, it might be untreatable.

You don't see DSA masters understanding DSA tagging protocols every day, I think you're overstating this. We'd have to cover Marvell-on-Marvell, Broadcom-on-Broadcom, Mediatek-on-Mediatek, and the rest will have to add their support when they add the hardware.

Quoted 6 lines

I know dsa properties are not the solution for everything (and I'm still adapting to where that border is) but, in this case, it is a device specific arrangement between the ethernet device and the switch. Wouldn't it be better to allow the
one writing the device-tree description inform if a master feature cannot be copied to slave devices?

Assuming an ultra-generic Ethernet controller with advanced soft parser capabilities, you'd have to teach it the format of each DSA tagging protocol you intend it to understand anyway, so this doesn't appear the kind of thing best described in the device tree, since it may easily be out of sync with what the driver is able to tell the hardware to do.

Quoted 20 lines

I checked DSA doc again and it says:

"Since tagging protocols in category 1 and 2 break software (and most often also hardware) packet dissection on the DSA master, features such as RPS (Receive Packet Steering) on the DSA master would be broken. The DSA framework deals with this by hooking into the flow dissector and shifting the offset at which the IP header is to be found in the tagged frame as seen by the DSA master. This behavior is automatic based on the overhead value of the tagging protocol. If not all packets are of equal size, the tagger can implement the flow_dissect method of the struct dsa_device_ops and override this default behavior by specifying the correct offset incurred by each individual RX packet. Tail taggers do not cause issues to the flow dissector."

It makes me think that it is the master network driver that does not implement that IP header location shift. Anyway, I believe it also depends on HW capabilities to inform that shift, right?

Quoted 10 lines

I'm trying to think as a DSA newbie (which is exactly what I am). Differently from an isolated ethernet driver, with DSA, the system does have control of "something" after the offload should be applied:
the dsa switch. Can't we have a generic way to send a packet to the switch and make it bounce back to the CPU (passing through the offload engine)? Would it work if I set the destination port as the CPU port? This way, we could simply detect if the offload worked and disable those features that did not work. It could work with a generic implementation or, if needed, a specialized optional ds_switch_ops function just to setup that temporary lookback forwarding rule.

To be clear, do you consider this simpler than an ndo operation that returns true or false if a certain DSA master can offload a certain netdev feature in the presence of a certain DSA tag?

Ignoring the fact that there are subtly different ways in which various hardware manufacturers implement packet injection from the CPU (and this is reflected in the various struct dsa_device_ops :: xmit operation), plus the fact that dsa_device_ops :: xmit takes a slave net_device as argument, for which there is none to represent the CPU port. These points mean that you'd need to implement a separate, hardware-specific loopback xmit for each tagging protocol. But again, ignoring that for a second.

When would be a good time to probe for DSA master features? The DSA master might be down when DSA switches probe. What should we do with packets sent on a DSA port until we've finished probing for DSA master capabilities?

Quoted 11 lines
Quoted 5 lines

So the sad news for you is that this is pretty much "net-next" material, even if it fixes what is essentially a design shortcoming. If we're quick, we could start doing this right as net-next reopens, and that would give other developers maximum opportunity to fix up the performance regressions caused by lack of TX checksumming.

No problem. I'm already playing the long game. I'm just trying to fix a device I own using my free time and I don't have any manager with impossible deadlines.
However, any solution with a performance regression would break the kernel API. I would rather add a new device-tree option :-)

Feel free to do whatever you want in OpenWRT, but as a general rule of thumb, if something can be solved without involving the device tree, then involving the device tree is probably the wrong approach.

maintainer
#62 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 43 lines · On Mon, 24 Jan 2022 17:31:47 +0200 Vladimir Oltean wrote:
Quoted 19 lines

I checked DSA doc again and it says:

"Since tagging protocols in category 1 and 2 break software (and most often also hardware) packet dissection on the DSA master, features such as RPS (Receive Packet Steering) on the DSA master would be broken. The DSA framework deals with this by hooking into the flow dissector and shifting the offset at which the IP header is to be found in the tagged frame as seen by the DSA master. This behavior is automatic based on the overhead value of the tagging protocol. If not all packets are of equal size, the tagger can implement the flow_dissect method of the struct dsa_device_ops and override this default behavior by specifying the correct offset incurred by each individual RX packet. Tail taggers do not cause issues to the flow dissector."

It makes me think that it is the master network driver that does not implement that IP header location shift. Anyway, I believe it also depends on HW capabilities to inform that shift, right?

Quoted 10 lines

I'm trying to think as a DSA newbie (which is exactly what I am). Differently from an isolated ethernet driver, with DSA, the system does have control of "something" after the offload should be applied:
the dsa switch. Can't we have a generic way to send a packet to the switch and make it bounce back to the CPU (passing through the offload engine)? Would it work if I set the destination port as the CPU port? This way, we could simply detect if the offload worked and disable those features that did not work. It could work with a generic implementation or, if needed, a specialized optional ds_switch_ops function just to setup that temporary lookback forwarding rule.

To be clear, do you consider this simpler than an ndo operation that returns true or false if a certain DSA master can offload a certain netdev feature in the presence of a certain DSA tag?

Ignoring the fact that there are subtly different ways in which various hardware manufacturers implement packet injection from the CPU (and this is reflected in the various struct dsa_device_ops :: xmit operation), plus the fact that dsa_device_ops :: xmit takes a slave net_device as argument, for which there is none to represent the CPU port. These points mean that you'd need to implement a separate, hardware-specific loopback xmit for each tagging protocol. But again, ignoring that for a second.

Quoted 4 lines

When would be a good time to probe for DSA master features? The DSA master might be down when DSA switches probe. What should we do with packets sent on a DSA port until we've finished probing for DSA master capabilities?

I thought for drivers setting the legacy NETIF_F_IP*_CSUM feature it's driver's responsibility to validate the geometry of the packet will work with the parser the device has. Or at least I think that's what Tom was pushing for when he was cleaning up the checksumming last (and wrote the long comment on the subject in skbuff.h).

maintainer
#63 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 5 lines · Jakub Kicinski

I thought for drivers setting the legacy NETIF_F_IP*_CSUM feature it's driver's responsibility to validate the geometry of the packet will work with the parser the device has. Or at least I think that's what Tom was pushing for when he was cleaning up the checksumming last (and wrote the long comment on the subject in skbuff.h).

Sorry Jakub, I don't understand what you mean to say when applied to the context discussed here?

#64 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 9 lines · Vladimir Oltean
Quoted 5 lines · Jakub Kicinski

I thought for drivers setting the legacy NETIF_F_IP*_CSUM feature it's driver's responsibility to validate the geometry of the packet will work with the parser the device has. Or at least I think that's what Tom was pushing for when he was cleaning up the checksumming last (and wrote the long comment on the subject in skbuff.h).

Sorry Jakub, I don't understand what you mean to say when applied to the context discussed here?

I believe what Jakub meant to say is that if a DSA conduit device driver advertises any of the NETIF_F_IP*_CSUM feature bits, then the driver's transmit path has the responsibility of checking that the payload being transmitted has a chance of being checksummed properly by the hardware. The problem here is not so much the geometry itself (linear or not, number/size of fragments, etc.) as much as the placement of the L2/L3 headers usually.

DSA conduit network device drivers do not have the ability today to determine what type of DSA tagging is being applied onto the DSA master but they do know whether DSA tagging is in use or not which may be enough to be overly compatible.

It is not clear to me whether we can solve this generically within the DSA framework or even if this is desirable, but once we have identified a problematic association of DSA tagger and DSA conduit, we can always have the DSA conduit driver do something like:

if (netdev_uses_dsa(dev))
skb_checksum_help()

or have a fix_features callback which does reject the enabling of NETIF_F_IP*_CSUM if netdev_uses_dsa() becomes true.

Signature · 1 line
Florian
maintainer
#65 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 28 lines · Florian Fainelli
Quoted 9 lines · Vladimir Oltean
Quoted 5 lines · Jakub Kicinski

I thought for drivers setting the legacy NETIF_F_IP*_CSUM feature it's driver's responsibility to validate the geometry of the packet will work with the parser the device has. Or at least I think that's what Tom was pushing for when he was cleaning up the checksumming last (and wrote the long comment on the subject in skbuff.h).

Sorry Jakub, I don't understand what you mean to say when applied to the context discussed here?

I believe what Jakub meant to say is that if a DSA conduit device driver advertises any of the NETIF_F_IP*_CSUM feature bits, then the driver's transmit path has the responsibility of checking that the payload being transmitted has a chance of being checksummed properly by the hardware. The problem here is not so much the geometry itself (linear or not, number/size of fragments, etc.) as much as the placement of the L2/L3 headers usually.

DSA conduit network device drivers do not have the ability today to determine what type of DSA tagging is being applied onto the DSA master but they do know whether DSA tagging is in use or not which may be enough to be overly compatible.

It is not clear to me whether we can solve this generically within the DSA framework or even if this is desirable, but once we have identified a problematic association of DSA tagger and DSA conduit, we can always have the DSA conduit driver do something like:

Quoted 5 lines

if (netdev_uses_dsa(dev))
skb_checksum_help()

or have a fix_features callback which does reject the enabling of NETIF_F_IP*_CSUM if netdev_uses_dsa() becomes true.

Yes, but as you point out, the DSA master driver doesn't know what header/trailer format it's dealing with. We could use netdev_uses_dsa() as a very rough approximation, and that might work when we know that the particular Ethernet controller is used only in conjunction with a single type of DSA switch [from the same vendor], but I think we're just delaying the inevitable, which is to treat the case where an Ethernet controller can be a DSA master for more than one switch type, and it understands some protocols but not others.
Also, scattering "if (netdev_uses_dsa(dev)) skb_checksum_help()" in DSA-unaware drivers (the common case) seems like the improper approach. We might end up seeing this pattern quite a lot, so DSA-unaware drivers won't be DSA-unaware any longer.
It's still possible I'm misunderstanding something...

#66 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 35 lines · Vladimir Oltean
Quoted 28 lines · Florian Fainelli
Quoted 9 lines · Vladimir Oltean
Quoted 5 lines · Jakub Kicinski

I thought for drivers setting the legacy NETIF_F_IP*_CSUM feature it's driver's responsibility to validate the geometry of the packet will work with the parser the device has. Or at least I think that's what Tom was pushing for when he was cleaning up the checksumming last (and wrote the long comment on the subject in skbuff.h).

Sorry Jakub, I don't understand what you mean to say when applied to the context discussed here?

I believe what Jakub meant to say is that if a DSA conduit device driver advertises any of the NETIF_F_IP*_CSUM feature bits, then the driver's transmit path has the responsibility of checking that the payload being transmitted has a chance of being checksummed properly by the hardware. The problem here is not so much the geometry itself (linear or not, number/size of fragments, etc.) as much as the placement of the L2/L3 headers usually.

DSA conduit network device drivers do not have the ability today to determine what type of DSA tagging is being applied onto the DSA master but they do know whether DSA tagging is in use or not which may be enough to be overly compatible.

It is not clear to me whether we can solve this generically within the DSA framework or even if this is desirable, but once we have identified a problematic association of DSA tagger and DSA conduit, we can always have the DSA conduit driver do something like:

Quoted 5 lines

if (netdev_uses_dsa(dev))
skb_checksum_help()

or have a fix_features callback which does reject the enabling of NETIF_F_IP*_CSUM if netdev_uses_dsa() becomes true.

Quoted 13 lines

Yes, but as you point out, the DSA master driver doesn't know what header/trailer format it's dealing with. We could use netdev_uses_dsa() as a very rough approximation, and that might work when we know that the particular Ethernet controller is used only in conjunction with a single type of DSA switch [from the same vendor], but I think we're just delaying the inevitable, which is to treat the case where an Ethernet controller can be a DSA master for more than one switch type, and it understands some protocols but not others.
Also, scattering "if (netdev_uses_dsa(dev)) skb_checksum_help()" in DSA-unaware drivers (the common case) seems like the improper approach. We might end up seeing this pattern quite a lot, so DSA-unaware drivers won't be DSA-unaware any longer.
It's still possible I'm misunderstanding something...

I don't think you are, and my crude proposal was just so we have it working, and then we can think about having it work fast.

A long time (but in the same galaxy) DSA used to set skb->protocol to the value of the DSA tagging protocol used (say ETH_P_EDSA), long before they were all consolidated within ETH_P_XDSA, but this would be breaking any checksum setting up that looks at skb->protocol to determine if it is IP, IPv6 or else, so in a way it might have done what we wanted it to do, but this was mostly by accident.

The tagger on transmit can definitively tell us via an out of band signaling what type of tagging protocol is being used and where it is located within the packet if necessary, and I suppose we can then update the DSA conduit in order to not ask the HW to checksum if this is deemed problematic. Doing that for every single packet transmitted however may not be very efficient given that usually we set-up one tagging protocol, then we set-up another one (possibly), but it won't change on a packet by packet basis. So maybe what we need to do is at the time we "connect" the tagger we inform the DSA master that from there on, all that is supposed to go through that interface will look that way, along with a description of the tagger offset and length?

Signature · 1 line
Florian
maintainer
#67 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

In reply to Vladimir Oltean
Quoted 11 lines · On Mon, 24 Jan 2022 19:21:58 +0200 Vladimir Oltean wrote:
Quoted 10 lines · Florian Fainelli
Quoted 2 lines · Vladimir Oltean

Sorry Jakub, I don't understand what you mean to say when applied to the context discussed here?

I believe what Jakub meant to say is that if a DSA conduit device driver advertises any of the NETIF_F_IP*_CSUM feature bits, then the driver's transmit path has the responsibility of checking that the payload being transmitted has a chance of being checksummed properly by the hardware. The problem here is not so much the geometry itself (linear or not, number/size of fragments, etc.) as much as the placement of the L2/L3 headers usually.

Sorry I used "geometry" loosely.

What I meant is simply that if the driver uses NETIF_F_IP*_CSUM it should parse the packet before it hands it off to the HW.

There is infinity of protocols users can come up with, while the device parser is very much finite, so it's only practical to check compliance with the HW parser in the driver. The reverse approach of adding per-protocol caps is a dead end IMO. And we should not bloat the stack when NETIF_F_HW_CSUM exists and the memo that parsing packets on Tx is bad b/c of protocol ossification went out a decade ago.

Quoted 16 lines
Quoted 10 lines

DSA conduit network device drivers do not have the ability today to determine what type of DSA tagging is being applied onto the DSA master but they do know whether DSA tagging is in use or not which may be enough to be overly compatible.

It is not clear to me whether we can solve this generically within the DSA framework or even if this is desirable, but once we have identified a problematic association of DSA tagger and DSA conduit, we can always have the DSA conduit driver do something like:

Quoted 5 lines

if (netdev_uses_dsa(dev))
skb_checksum_help()

or have a fix_features callback which does reject the enabling of NETIF_F_IP*_CSUM if netdev_uses_dsa() becomes true.

Quoted 12 lines

Yes, but as you point out, the DSA master driver doesn't know what header/trailer format it's dealing with. We could use netdev_uses_dsa() as a very rough approximation, and that might work when we know that the particular Ethernet controller is used only in conjunction with a single type of DSA switch [from the same vendor], but I think we're just delaying the inevitable, which is to treat the case where an Ethernet controller can be a DSA master for more than one switch type, and it understands some protocols but not others.
Also, scattering "if (netdev_uses_dsa(dev)) skb_checksum_help()" in DSA-unaware drivers (the common case) seems like the improper approach. We might end up seeing this pattern quite a lot, so DSA-unaware drivers won't be DSA-unaware any longer.

It's not about DSA. The driver should not check

if (dsa())

	blah;

it should check

if (!(eth [-> vlan] -> ip -> tcp/udp))

	csum_help();
Quoted 1 line

It's still possible I'm misunderstanding something...

maintainer
#68 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 14 lines · On Mon, 24 Jan 2022 09:35:56 -0800 Jakub Kicinski wrote:

Sorry I used "geometry" loosely.

What I meant is simply that if the driver uses NETIF_F_IP*_CSUM it should parse the packet before it hands it off to the HW.

There is infinity of protocols users can come up with, while the device parser is very much finite, so it's only practical to check compliance with the HW parser in the driver. The reverse approach of adding per-protocol caps is a dead end IMO. And we should not bloat the stack when NETIF_F_HW_CSUM exists and the memo that parsing packets on Tx is bad b/c of protocol ossification went out a decade ago.

It's not about DSA. The driver should not check

Quoted 7 lines

if (dsa())

	blah;

it should check

if (!(eth [-> vlan] -> ip -> tcp/udp))

	csum_help();

Admittedly on a quick look thru the drivers which already do this I only see L3, L4 and GRE/UDP encap checks. Nothing validates L2.

maintainer
#69 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 18 lines · Jakub Kicinski
Quoted 17 lines · On Mon, 24 Jan 2022 09:35:56 -0800 Jakub Kicinski wrote:

Sorry I used "geometry" loosely.

What I meant is simply that if the driver uses NETIF_F_IP*_CSUM it should parse the packet before it hands it off to the HW.

There is infinity of protocols users can come up with, while the device parser is very much finite, so it's only practical to check compliance with the HW parser in the driver. The reverse approach of adding per-protocol caps is a dead end IMO. And we should not bloat the stack when NETIF_F_HW_CSUM exists and the memo that parsing packets on Tx is bad b/c of protocol ossification went out a decade ago.

It's not about DSA. The driver should not check

if (dsa())

	blah;
Quoted 7 lines
Quoted 4 lines

it should check

if (!(eth [-> vlan] -> ip -> tcp/udp))

	csum_help();

Admittedly on a quick look thru the drivers which already do this I only see L3, L4 and GRE/UDP encap checks. Nothing validates L2.

So before we declare that any given Ethernet driver is buggy for declaring NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM and not checking that skb->csum_start points where it expects it to (taking into consideration potential VLAN headers, IPv6 extension headers), is there any driver that _does_ perform these checks correctly, that could be used as an example?

maintainer
#70 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 24 lines · On Mon, 24 Jan 2022 21:08:45 +0200 Vladimir Oltean wrote:
Quoted 23 lines · Jakub Kicinski
Quoted 21 lines · On Mon, 24 Jan 2022 09:35:56 -0800 Jakub Kicinski wrote:

Sorry I used "geometry" loosely.

What I meant is simply that if the driver uses NETIF_F_IP*_CSUM it should parse the packet before it hands it off to the HW.

There is infinity of protocols users can come up with, while the device parser is very much finite, so it's only practical to check compliance with the HW parser in the driver. The reverse approach of adding per-protocol caps is a dead end IMO. And we should not bloat the stack when NETIF_F_HW_CSUM exists and the memo that parsing packets on Tx is bad b/c of protocol ossification went out a decade ago.

It's not about DSA. The driver should not check

if (dsa())

	blah;

it should check

if (!(eth [-> vlan] -> ip -> tcp/udp))

	csum_help();  
Quoted 7 lines
Quoted 2 lines

Admittedly on a quick look thru the drivers which already do this I only see L3, L4 and GRE/UDP encap checks. Nothing validates L2.

So before we declare that any given Ethernet driver is buggy for declaring NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM and not checking that skb->csum_start points where it expects it to (taking into consideration potential VLAN headers, IPv6 extension headers),

Extension headers are explicitly not supported by NETIF_F_IPV6_CSUM.

IIRC Tom's hope was to delete NETIF_F_IP*_CSUM completely once all drivers are converted to parsing and therefore can use NETIF_F_HW_CSUM.

Quoted 2 lines

is there any driver that _does_ perform these checks correctly, that could be used as an example?

I don't think so. Let me put it this way - my understanding is that up until now we had been using the vlan_features, mpls_features etc to perform L2/L2.5/below-IP feature stripping. This scales poorly to DSA tags, as discussed in this thread.

I'm suggesting we extend the kind of checking we already do to work around inevitable deficiencies of device parsers for tunnels to DSA tags.

We can come up with various schemes of expressing capabilities between underlying driver and tag driver. I'm not aware of similar out-of-band schemes existing today so it'd be "DSA doing it's own thing", which does not seem great.

maintainer
#71 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 33 lines · Jakub Kicinski
Quoted 24 lines · On Mon, 24 Jan 2022 21:08:45 +0200 Vladimir Oltean wrote:
Quoted 23 lines · Jakub Kicinski
Quoted 21 lines · On Mon, 24 Jan 2022 09:35:56 -0800 Jakub Kicinski wrote:

Sorry I used "geometry" loosely.

What I meant is simply that if the driver uses NETIF_F_IP*_CSUM it should parse the packet before it hands it off to the HW.

There is infinity of protocols users can come up with, while the device parser is very much finite, so it's only practical to check compliance with the HW parser in the driver. The reverse approach of adding per-protocol caps is a dead end IMO. And we should not bloat the stack when NETIF_F_HW_CSUM exists and the memo that parsing packets on Tx is bad b/c of protocol ossification went out a decade ago.

It's not about DSA. The driver should not check

if (dsa())

	blah;

it should check

if (!(eth [-> vlan] -> ip -> tcp/udp))

	csum_help();
Quoted 7 lines
Quoted 2 lines

Admittedly on a quick look thru the drivers which already do this I only see L3, L4 and GRE/UDP encap checks. Nothing validates L2.

So before we declare that any given Ethernet driver is buggy for declaring NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM and not checking that skb->csum_start points where it expects it to (taking into consideration potential VLAN headers, IPv6 extension headers),

Quoted 4 lines

Extension headers are explicitly not supported by NETIF_F_IPV6_CSUM.

IIRC Tom's hope was to delete NETIF_F_IP*_CSUM completely once all drivers are converted to parsing and therefore can use NETIF_F_HW_CSUM.

IIUC, NETIF_F_IP*_CSUM vs NETIF_F_HW_CSUM doesn't make that big of a difference in terms of what the driver should check for, if the hardware checksum offload engine can't directly be given the csum_start and csum_offset, wherever they may be.

Quoted 11 lines
Quoted 2 lines

is there any driver that _does_ perform these checks correctly, that could be used as an example?

I don't think so. Let me put it this way - my understanding is that up until now we had been using the vlan_features, mpls_features etc to perform L2/L2.5/below-IP feature stripping. This scales poorly to DSA tags, as discussed in this thread.

I'm suggesting we extend the kind of checking we already do to work around inevitable deficiencies of device parsers for tunnels to DSA tags.

Sorry, I'm very tired and I probably don't understand what you're saying, so excuse the extra clarification questions.

The typical protocol checking that drivers with NETIF_F_HW_CSUM do seems to be based on vlan_get_protocol()/skb->protocol/skb_network_header()/ skb_transport_header() values, all of which make DSA invisible. So they don't work if the underlying hardware really doesn't like seeing an unexpected DSA header.

When you say "I'm suggesting we extend the kind of checking we already do", do you mean we should modify the likes of e1000e and igb such that, if they're ever used as DSA masters, they do a full header parse of the packet (struct ethhdr :: h_proto, check if VLAN, struct iphdr/ipv6hdr, etc.) instead of the current logic? It will be pretty convoluted unless we have some helper. Because if I follow through, for a DSA-tagged IP packet on xmit, skb->protocol is certainly htons(ETH_P_IP):

ntohs(skb->protocol) = 0x800, csum_offset = 16, csum_start = 280, skb_checksum_start_offset = 54, skb->network_header = 260, skb_network_header_len = 20

skb_dump output:
skb len=94 headroom=226 headlen=94 tailroom=384
mac=(226,34) net=(260,20) trans=280
shinfo(txflags=0 nr_frags=0 gso(size=0 type=1 segs=1)) csum(0x100118 ip_summed=3 complete_sw=0 valid=0 level=0) hash(0x7710ee84 sw=0 l4=1) proto=0x0800 pkttype=0 iif=0
dev name=eno2 feat=0x00020100001149a9
sk family=2 type=1 proto=6
skb headroom: 00000000: 6c 00 03 02 64 65 76 00 fe ed ca fe 28 00 00 00 ...(junk)...
skb headroom: 000000e0: 5f 43
20 byte DSA tag
|
v
skb linear: 00000000: 88 80 00 0a 80 00 00 00 00 00 00 00 08 00 30 00

                                    skb_mac_header()
                                    |
                                    v
skb linear:   00000010: 00 00 00 00 68 05 ca 92 af 20 00 04 9f 05 f6 28
                              skb_network_header()
                              |
                              v
skb linear:   00000020: 08 00 45 00 00 3c 26 47 40 00 40 06 00 49 0a 00
                                          skb_checksum_start_offset
                                          |
                                          |                       csum_offset
                                          v                       v
skb linear:   00000030: 00 2c 0a 00 00 01 b6 08 14 51 11 1f 91 4f 00 00
skb linear:   00000040: 00 00 a0 02 fa f0 14 5b 00 00 02 04 05 b4 04 02
skb linear:   00000050: 08 0a 2e 00 e5 b8 00 00 00 00 01 03 03 07

I don't know, I just don't expect that non-DSA users of those drivers will be very happy about such changes. Do these existing protocol checking schemes qualify as buggy?

If this is the convention that we want to enforce, then I can't really help Luiz with fixing the OpenWRT mtk_eth_soc.c - he'll have to figure out a way to parse the packets for which his hardware will accept the checksumming offload, and call skb_checksum_help() otherwise.

Quoted 4 lines

We can come up with various schemes of expressing capabilities between underlying driver and tag driver. I'm not aware of similar out-of-band schemes existing today so it'd be "DSA doing it's own thing", which does not seem great.

It at least seems less complex to me, and less checking in the fast path if I understand everything that's been said correctly.

maintainer
#72 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 38 lines · On Mon, 24 Jan 2022 22:56:07 +0200 Vladimir Oltean wrote:
Quoted 10 lines · Jakub Kicinski
Quoted 4 lines · On Mon, 24 Jan 2022 21:08:45 +0200 Vladimir Oltean wrote:

So before we declare that any given Ethernet driver is buggy for declaring NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM and not checking that skb->csum_start points where it expects it to (taking into consideration potential VLAN headers, IPv6 extension headers),

Extension headers are explicitly not supported by NETIF_F_IPV6_CSUM.

IIRC Tom's hope was to delete NETIF_F_IP*_CSUM completely once all drivers are converted to parsing and therefore can use NETIF_F_HW_CSUM.

IIUC, NETIF_F_IP*_CSUM vs NETIF_F_HW_CSUM doesn't make that big of a difference in terms of what the driver should check for, if the hardware checksum offload engine can't directly be given the csum_start and csum_offset, wherever they may be.

Quoted 11 lines
Quoted 2 lines

is there any driver that _does_ perform these checks correctly, that could be used as an example?

I don't think so. Let me put it this way - my understanding is that up until now we had been using the vlan_features, mpls_features etc to perform L2/L2.5/below-IP feature stripping. This scales poorly to DSA tags, as discussed in this thread.

I'm suggesting we extend the kind of checking we already do to work around inevitable deficiencies of device parsers for tunnels to DSA tags.

Sorry, I'm very tired and I probably don't understand what you're saying, so excuse the extra clarification questions.

The typical protocol checking that drivers with NETIF_F_HW_CSUM do seems to be based on vlan_get_protocol()/skb->protocol/skb_network_header()/ skb_transport_header() values, all of which make DSA invisible. So they don't work if the underlying hardware really doesn't like seeing an unexpected DSA header.

Quoted 5 lines

When you say "I'm suggesting we extend the kind of checking we already do", do you mean we should modify the likes of e1000e and igb such that, if they're ever used as DSA masters, they do a full header parse of the packet (struct ethhdr :: h_proto, check if VLAN, struct iphdr/ipv6hdr, etc.) instead of the current logic?

That was my thinking, yes. The exact amount of work depends on the driver, I believe that more recent Intel parts (igb, ixgbe and newer) pass a L3 offset to the HW. They treat L2 as opaque, ergo no patches needed. At a glance e1000e passes the full skb_checksum_start_offset() to HW, so likely even better.

It's only drivers for devices which actually want to parse the Ethertype that would need extra checks. (Coincidentally such devices can't support MPLS given the lack of L3 indication in the frame.)

Quoted 21 lines

It will be pretty convoluted unless
we have some helper. Because if I follow through, for a DSA-tagged IP packet on xmit, skb->protocol is certainly htons(ETH_P_IP):

ntohs(skb->protocol) = 0x800, csum_offset = 16, csum_start = 280, skb_checksum_start_offset = 54, skb->network_header = 260, skb_network_header_len = 20

skb_dump output:
skb len=94 headroom=226 headlen=94 tailroom=384
mac=(226,34) net=(260,20) trans=280
shinfo(txflags=0 nr_frags=0 gso(size=0 type=1 segs=1)) csum(0x100118 ip_summed=3 complete_sw=0 valid=0 level=0) hash(0x7710ee84 sw=0 l4=1) proto=0x0800 pkttype=0 iif=0
dev name=eno2 feat=0x00020100001149a9
sk family=2 type=1 proto=6
skb headroom: 00000000: 6c 00 03 02 64 65 76 00 fe ed ca fe 28 00 00 00 ...(junk)...
skb headroom: 000000e0: 5f 43
20 byte DSA tag
|
v
skb linear: 00000000: 88 80 00 0a 80 00 00 00 00 00 00 00 08 00 30 00

Quoted 15 lines
                                    skb_mac_header()
                                    |
                                    v
skb linear:   00000010: 00 00 00 00 68 05 ca 92 af 20 00 04 9f 05 f6 28
                              skb_network_header()
                              |
                              v
skb linear:   00000020: 08 00 45 00 00 3c 26 47 40 00 40 06 00 49 0a 00
                                          skb_checksum_start_offset
                                          |
                                          |                       csum_offset
                                          v                       v
skb linear:   00000030: 00 2c 0a 00 00 01 b6 08 14 51 11 1f 91 4f 00 00
skb linear:   00000040: 00 00 a0 02 fa f0 14 5b 00 00 02 04 05 b4 04 02
skb linear:   00000050: 08 0a 2e 00 e5 b8 00 00 00 00 01 03 03 07

Oof, so in this case the DSA tag is _before_ the skb_mac_header()? Or the prepend is supposed to be parsable as a Ethernet header? Seems like any device that can do csum over this packet must already use L3/L4 offsets or have explicit knowledge of DSA, right?

Quoted 3 lines

I don't know, I just don't expect that non-DSA users of those drivers will be very happy about such changes. Do these existing protocol checking schemes qualify as buggy?

Unfortunate reality of the checksum offloads is that most drivers for devices which parse on Tx are buggy, it's more of a question of whether anyone tried to use an unsupported protocol stack :( Recent example that comes to mind is 1698d600b361 ("bnxt_en: Implement .ndo_features_check().").

Quoted 5 lines

If this is the convention that we want to enforce, then I can't really help Luiz with fixing the OpenWRT mtk_eth_soc.c - he'll have to figure out a way to parse the packets for which his hardware will accept the checksumming offload, and call skb_checksum_help() otherwise.

Quoted 7 lines
Quoted 4 lines

We can come up with various schemes of expressing capabilities between underlying driver and tag driver. I'm not aware of similar out-of-band schemes existing today so it'd be "DSA doing it's own thing", which does not seem great.

It at least seems less complex to me, and less checking in the fast path if I understand everything that's been said correctly.

I understand, I'm primarily trying to share some context and prior work. I don't mean to nack all other approaches.

I believe writing a parser matching the device behavior would be easier for a driver author than interpreting the runes of our csum offload API and getting thru the thicket of all the bits. If that's not the case my argument is likely defeated.

maintainer
#73 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 45 lines · Jakub Kicinski
Quoted 38 lines · On Mon, 24 Jan 2022 22:56:07 +0200 Vladimir Oltean wrote:
Quoted 10 lines · Jakub Kicinski
Quoted 4 lines · On Mon, 24 Jan 2022 21:08:45 +0200 Vladimir Oltean wrote:

So before we declare that any given Ethernet driver is buggy for declaring NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM and not checking that skb->csum_start points where it expects it to (taking into consideration potential VLAN headers, IPv6 extension headers),

Extension headers are explicitly not supported by NETIF_F_IPV6_CSUM.

IIRC Tom's hope was to delete NETIF_F_IP*_CSUM completely once all drivers are converted to parsing and therefore can use NETIF_F_HW_CSUM.

IIUC, NETIF_F_IP*_CSUM vs NETIF_F_HW_CSUM doesn't make that big of a difference in terms of what the driver should check for, if the hardware checksum offload engine can't directly be given the csum_start and csum_offset, wherever they may be.

Quoted 11 lines
Quoted 2 lines

is there any driver that _does_ perform these checks correctly, that could be used as an example?

I don't think so. Let me put it this way - my understanding is that up until now we had been using the vlan_features, mpls_features etc to perform L2/L2.5/below-IP feature stripping. This scales poorly to DSA tags, as discussed in this thread.

I'm suggesting we extend the kind of checking we already do to work around inevitable deficiencies of device parsers for tunnels to DSA tags.

Sorry, I'm very tired and I probably don't understand what you're saying, so excuse the extra clarification questions.

The typical protocol checking that drivers with NETIF_F_HW_CSUM do seems to be based on vlan_get_protocol()/skb->protocol/skb_network_header()/ skb_transport_header() values, all of which make DSA invisible. So they don't work if the underlying hardware really doesn't like seeing an unexpected DSA header.

Quoted 5 lines

When you say "I'm suggesting we extend the kind of checking we already do", do you mean we should modify the likes of e1000e and igb such that, if they're ever used as DSA masters, they do a full header parse of the packet (struct ethhdr :: h_proto, check if VLAN, struct iphdr/ipv6hdr, etc.) instead of the current logic?

Quoted 5 lines

That was my thinking, yes. The exact amount of work depends on the driver, I believe that more recent Intel parts (igb, ixgbe and newer) pass a L3 offset to the HW. They treat L2 as opaque, ergo no patches needed. At a glance e1000e passes the full skb_checksum_start_offset() to HW, so likely even better.

Ah, right, I missed that. I agree that a driver that uses skb->csum_start is very likely to work unmodified with DSA headers (not trailers). I didn't notice this in e1000 because I was just searching for csum_start.

Quoted 41 lines

It's only drivers for devices which actually want to parse the Ethertype that would need extra checks. (Coincidentally such devices can't support MPLS given the lack of L3 indication in the frame.)

Quoted 21 lines

It will be pretty convoluted unless
we have some helper. Because if I follow through, for a DSA-tagged IP packet on xmit, skb->protocol is certainly htons(ETH_P_IP):

ntohs(skb->protocol) = 0x800, csum_offset = 16, csum_start = 280, skb_checksum_start_offset = 54, skb->network_header = 260, skb_network_header_len = 20

skb_dump output:
skb len=94 headroom=226 headlen=94 tailroom=384
mac=(226,34) net=(260,20) trans=280
shinfo(txflags=0 nr_frags=0 gso(size=0 type=1 segs=1)) csum(0x100118 ip_summed=3 complete_sw=0 valid=0 level=0) hash(0x7710ee84 sw=0 l4=1) proto=0x0800 pkttype=0 iif=0
dev name=eno2 feat=0x00020100001149a9
sk family=2 type=1 proto=6
skb headroom: 00000000: 6c 00 03 02 64 65 76 00 fe ed ca fe 28 00 00 00 ...(junk)...
skb headroom: 000000e0: 5f 43
20 byte DSA tag
|
v
skb linear: 00000000: 88 80 00 0a 80 00 00 00 00 00 00 00 08 00 30 00

Quoted 15 lines
                                    skb_mac_header()
                                    |
                                    v
skb linear:   00000010: 00 00 00 00 68 05 ca 92 af 20 00 04 9f 05 f6 28
                              skb_network_header()
                              |
                              v
skb linear:   00000020: 08 00 45 00 00 3c 26 47 40 00 40 06 00 49 0a 00
                                          skb_checksum_start_offset
                                          |
                                          |                       csum_offset
                                          v                       v
skb linear:   00000030: 00 2c 0a 00 00 01 b6 08 14 51 11 1f 91 4f 00 00
skb linear:   00000040: 00 00 a0 02 fa f0 14 5b 00 00 02 04 05 b4 04 02
skb linear:   00000050: 08 0a 2e 00 e5 b8 00 00 00 00 01 03 03 07
Quoted 4 lines

Oof, so in this case the DSA tag is _before_ the skb_mac_header()? Or the prepend is supposed to be parsable as a Ethernet header? Seems like any device that can do csum over this packet must already use L3/L4 offsets or have explicit knowledge of DSA, right?

I'm sorry, there's a mistake, skb_mac_header() points to the DSA tag here (and skb_mac_header_len is 34), I wanted to say "real MAC header" but failed to do so. This case shouldn't pose any special problems.

Quoted 9 lines
Quoted 3 lines

I don't know, I just don't expect that non-DSA users of those drivers will be very happy about such changes. Do these existing protocol checking schemes qualify as buggy?

Unfortunate reality of the checksum offloads is that most drivers for devices which parse on Tx are buggy, it's more of a question of whether anyone tried to use an unsupported protocol stack :( Recent example that comes to mind is 1698d600b361 ("bnxt_en: Implement .ndo_features_check().").

Nice hook this ndo_features_check! In my reading of validate_xmit_skb() I went right past it.

Quoted 13 lines
Quoted 5 lines

If this is the convention that we want to enforce, then I can't really help Luiz with fixing the OpenWRT mtk_eth_soc.c - he'll have to figure out a way to parse the packets for which his hardware will accept the checksumming offload, and call skb_checksum_help() otherwise.

Quoted 7 lines
Quoted 4 lines

We can come up with various schemes of expressing capabilities between underlying driver and tag driver. I'm not aware of similar out-of-band schemes existing today so it'd be "DSA doing it's own thing", which does not seem great.

It at least seems less complex to me, and less checking in the fast path if I understand everything that's been said correctly.

Quoted 7 lines

I understand, I'm primarily trying to share some context and prior work. I don't mean to nack all other approaches.

I believe writing a parser matching the device behavior would be easier for a driver author than interpreting the runes of our csum offload API and getting thru the thicket of all the bits. If that's not the case my argument is likely defeated.

Ok, writing a parser might be needed if the DSA master is going, for some reason, to support TX checksum offloading with some DSA headers but not with others.

If that is not the case, and that Ethernet controller simply doesn't support TX checksumming unless it's the plain old Ethernet {+ VLAN} + IP/IPv6 + TCP/UDP, then this blanket patch below should fix the problem almost elegantly, and parsing is a useless complication (warning, not even compile-tested!):

-----------------------------[ cut here ]----------------------------- From 5ef3d3cd8441d756933558212f518f48754c64d9 Mon Sep 17 00:00:00 2001
From: Vladimir Oltean <[email protected]>
Date: Tue, 25 Jan 2022 00:16:57 +0200
Subject: [PATCH] ramips: ethernet: ralink: disable TX checksumming on DSA
masters

---
 .../files/drivers/net/ethernet/ralink/mtk_eth_soc.c  | 12 ++++++++++++
 1 file changed, 12 insertions(+)
target/linux/ramips/files/drivers/net/ethernet/ralink/mtk_eth_soc.c +12 -0
index e07e5ed5a8f8..6ed9bc5942fd 100644
@@ -31,6 +31,7 @@
 #include <linux/io.h>
 #include <linux/bug.h>
 #include <linux/netfilter.h>
+#include <net/dsa.h>
 #include <net/netfilter/nf_flow_table.h>
 #include <linux/of_gpio.h>
 #include <linux/gpio.h>
@@ -1497,6 +1498,16 @@ static int fe_change_mtu(struct net_device *dev, int new_mtu)
 	return fe_open(dev);
 }
 
+static netdev_features_t fe_features_check(struct sk_buff *skb,
+					   struct net_device *dev,
+					   netdev_features_t features)
+{
+	if (netdev_uses_dsa(dev))
+		features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
+
+	return features;
+}
+
 static const struct net_device_ops fe_netdev_ops = {
 	.ndo_init		= fe_init,
 	.ndo_uninit		= fe_uninit,
@@ -1514,6 +1525,7 @@ static const struct net_device_ops fe_netdev_ops = {
 #ifdef CONFIG_NET_POLL_CONTROLLER
 	.ndo_poll_controller	= fe_poll_controller,
 #endif
+	.ndo_features_check	= fe_features_check,
 };
 
 static void fe_reset_pending(struct fe_priv *priv)

-----------------------------[ cut here ]-----------------------------

This is essentially what Florian said ages ago, it just took me a very long time to process. I guess what hadn't fully clicked in my head is that the TX checksumming offload being functional is more a matter of telling the hardware what are the L3 and L4 offsets, and the csum_offset, rather than it requiring any particular understanding of the DSA header. In turn, it means that for "nice" Ethernet controller implementations where that is the case, it would be actively detrimential to add a new .ndo_get_dsa_features() or something like that - because such a driver would report that it supports all DSA header-type formats (trailers are still broken as long as there isn't a csum_end). And keeping that kind of driver in sync with all DSA protocols that appear will become a repetitive task.

So crisis averted, I guess?
Thanks a lot to both of you for the patient explanations!
I retract my proposal for a new ndo and also suggest that the DSA master driver takes care to not leave as zero a TX checksum it can't offload.

author maintainer
#74 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Wow... that's a lot to digest.

Quoted 35 lines
---
 .../files/drivers/net/ethernet/ralink/mtk_eth_soc.c  | 12 ++++++++++++
 1 file changed, 12 insertions(+)
target/linux/ramips/files/drivers/net/ethernet/ralink/mtk_eth_soc.c +11 -0
index e07e5ed5a8f8..6ed9bc5942fd 100644
@@ -31,6 +31,7 @@
 #include <linux/io.h>
 #include <linux/bug.h>
 #include <linux/netfilter.h>
+#include <net/dsa.h>
 #include <net/netfilter/nf_flow_table.h>
 #include <linux/of_gpio.h>
 #include <linux/gpio.h>
@@ -1497,6 +1498,16 @@ static int fe_change_mtu(struct net_device *dev, int new_mtu)
        return fe_open(dev);
 }
 
+static netdev_features_t fe_features_check(struct sk_buff *skb,
+                                          struct net_device *dev,
+                                          netdev_features_t features)
+{
+       if (netdev_uses_dsa(dev))
+               features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
+
+       return features;
+}
+
 static const struct net_device_ops fe_netdev_ops = {
        .ndo_init               = fe_init,
        .ndo_uninit             = fe_uninit,

target/linux/ramips/files/drivers/net/ethernet/ralink/mtk_eth_soc.c static const struct net_device_ops fe_netdev_ops = { · line 1531

Quoted 7 lines
target/linux/ramips/files/drivers/net/ethernet/ralink/mtk_eth_soc.c +1 -0
@@ -1514,6 +1525,7 @@ static const struct net_device_ops fe_netdev_ops = {
 #ifdef CONFIG_NET_POLL_CONTROLLER
        .ndo_poll_controller    = fe_poll_controller,
 #endif
+       .ndo_features_check     = fe_features_check,
 };
 
 static void fe_reset_pending(struct fe_priv *priv)

Thanks, Vladimir. I'll try that patch soon. However, it will never be accepted even in OpenWrt as is because it does offload its own proprietary tag.
I might need to add another if like:

target/linux/ramips/files/drivers/net/ethernet/ralink/mtk_eth_soc.c static int fe_change_mtu(struct net_device *dev, int new_mtu) · line 1506

Quoted 3 lines
target/linux/ramips/files/drivers/net/ethernet/ralink/mtk_eth_soc.c +3 -0
+       if (netdev_uses_dsa(dev))
+               if (skb->???->proto_in_use != DSA_TAG_PROTO_MTK)
+                      features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);

I think it would need to save the used tag in some form of an oob signal (as Florian suggested). In that case, even the netdev_uses_dsa() test can be removed as a skb with an oob tag signal is surely from a dsa device.

Anyway, even with existing offload code not doing exactly what they should, they normally work given a normal device usage. The strange part is that DSA assumes those features, copied from master to slave, will still be the same even after a tag was injected into the packet.

Sorry for my arrogance being a newbie but I think the place to fix the problem is still in slave feature list. It is better than having the kernel repeat the test for every single packet. And nobody will be willing to add extra overhead to a working code just because DSA needs it. It is easier to add a new function that does not touch existing code paths.

I believe that those drivers with NETIF_F_HW_CSUM are fine for every type of DSA, right? So, just those with NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM set needs to be adjusted. A fully implemented ndo_features_check() will work but improving it for every driver will add extra code/overhead for all packets, used with DSA or not. And that extra code needed for DSA will either always keep or remove the same features for a given slave.

I imagine that for NETIF_F_CSUM_MASK and NETIF_F_GSO_MASK, it would not be too hard to build a set of candidate packets to test if that feature is still valid after the tag was added. With that assumption, a new ndo_features_check_offline(), similar to ndo_features_check() but not be called by netif_skb_features, will test each candidate during slave setup. If the check disagrees after the tag was added, that feature should be disabled for that slave. Something like:

slave->features = master->features;
if (slave->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))

    if (dev->netdev_ops->ndo_features_check_offline)
        foreach (test_candidate)
            tagged_test_candidate = add_tag (test_candidate, slave->tag);

            slave->features &=

~(master->netdev_ops->ndo_features_check_offline(test_candidate, master, slave->features) ^

master->netdev_ops->ndo_features_check_offline(tagged_test_candidate, master, slave->features)

    else
        slave->features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK)

The only drivers that would have performance regression while used as DSA master ports are those:
1) that does not have NETIF_F_HW_CSUM set
2) but could still offload after a particular DSA tag was added (when tag vendor and HW matches)
3) and still didn't implement the new ndo_features_check_offline().

ndo_features_check_offline() would not be too much different from what Vladmir suggested for the out-of-tree mtk_eth_soc driver.

ndo_features_check_offline(sbk, dev, features) {
    switch (sbk->oob->tag) {
    case SUPPORTED_TAG_1:
    case SUPPORTED_TAG_2:
    case SUPPORTED_TAG_3:
    case NO_TAG:
        break;
    default:
        features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
    }

    if (dev->netdev_ops->ndo_features_check)
         features &= dev->netdev_ops->ndo_features_check(skb, dev, features);

    /* some more test if needed*/

    return features;
}

If used exclusively by DSA, ndo_features_check_offline could also be called ndo_dsa_features_check (or any better name than ndo_features_check_offline). That is not far away from what Vladmir suggested (and later retracted) in the first place.

Regards,

Luiz

maintainer
#75 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Hi Luiz,

Quoted 7 lines · Luiz Angelo Daros de Luca

I believe that those drivers with NETIF_F_HW_CSUM are fine for every type of DSA, right? So, just those with NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM set needs to be adjusted. A fully implemented ndo_features_check() will work but improving it for every driver will add extra code/overhead for all packets, used with DSA or not. And that extra code needed for DSA will either always keep or remove the same features for a given slave.

Could you implement a prototype of packet parsing in ndo_features_check, which checks for the known DSA EtherType and clears the offload bit for unsupported packets, and do some performance testing before and after, to lean the argument in your favor with some numbers? I've no problem if you test for the worst case, i.e. line rate with small UDP packets encapsulated with the known (offload-capable) DSA tag format, where there is little benefit for offloading TX checksumming.

author maintainer
#76 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 7 lines

Could you implement a prototype of packet parsing in ndo_features_check, which checks for the known DSA EtherType and clears the offload bit for unsupported packets, and do some performance testing before and after, to lean the argument in your favor with some numbers? I've no problem if you test for the worst case, i.e. line rate with small UDP packets encapsulated with the known (offload-capable) DSA tag format, where there is little benefit for offloading TX checksumming.

There is no way to tell if a packet has a DSA tag only by parsing its content. For Realtek and Marvel EDSA, there is a distinct ethertype (although Marvel EDSA uses a non-registered number) that drivers can check. For others, specially those that add the tag before the ethernet header or after the payload, it might not have a magic number. It is impossible to securely identify if and which DSA is in use for some DSA tags from the packet alone. This is also the case for mediatek. Although it places its tag just before ethertype (like Realtek and Marvel), there is no magic number. It needs some context to know what type of DSA was applied.

skb_buf today knows nothing about the added DSA tag. Although net_device does know if it is a master port in a dsa tree, and it has a default dsa tag, with multiple switches using different tags, it cannot tell which dsa tag was added to that packet.
That is the information I need to test if that tag is supported or not by this drive.

I believe once an offload HW can digest a dsa tag, it might support the same type of protocols with or without the tag.
In the end, what really matters is if a driver supports a specific dsa tag.

Wouldn't it be much easier to have a dedicated optional ndo_dsa_tag_supported()? It would be only needed for those drivers that still use NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM and only those that can digest a tag.

Regards,

#77 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 8 lines · Luiz Angelo Daros de Luca
Quoted 7 lines

Could you implement a prototype of packet parsing in ndo_features_check, which checks for the known DSA EtherType and clears the offload bit for unsupported packets, and do some performance testing before and after, to lean the argument in your favor with some numbers? I've no problem if you test for the worst case, i.e. line rate with small UDP packets encapsulated with the known (offload-capable) DSA tag format, where there is little benefit for offloading TX checksumming.

Quoted 10 lines

There is no way to tell if a packet has a DSA tag only by parsing its content. For Realtek and Marvel EDSA, there is a distinct ethertype (although Marvel EDSA uses a non-registered number) that drivers can check. For others, specially those that add the tag before the ethernet header or after the payload, it might not have a magic number. It is impossible to securely identify if and which DSA is in use for some DSA tags from the packet alone. This is also the case for mediatek. Although it places its tag just before ethertype (like Realtek and Marvel), there is no magic number. It needs some context to know what type of DSA was applied.

Looking at mtk_eth_soc.h TX_DMA_CHKSUM is 0x7 << 29 so we set 3 bits there, which makes me think that either we defined too many bits, or some of those bits have a compounded meaning. The rest of the bits do not seem to be defined, so maybe there is a programmable offset where to calculate the checksum from and deposit it. Is there a public programmable manual?

Quoted 11 lines

skb_buf today knows nothing about the added DSA tag. Although net_device does know if it is a master port in a dsa tree, and it has a default dsa tag, with multiple switches using different tags, it cannot tell which dsa tag was added to that packet.
That is the information I need to test if that tag is supported or not by this drive.

I believe once an offload HW can digest a dsa tag, it might support the same type of protocols with or without the tag.
In the end, what really matters is if a driver supports a specific dsa tag.

To be honest, I am not sure if we need to know about the specific details of the tag like is it Realtek, Broadcom, Mediatek, QCA, more than knowing whether the L3/L4 offsets will be at "expected" locations. By that I mean, located at 14 bytes from the start of the frame for IP without VLAN , and 18 bytes with VLAN, did we "stack" switch tags on top of another thus moving by another X bytes etc.

Quoted 5 lines

Wouldn't it be much easier to have a dedicated optional ndo_dsa_tag_supported()? It would be only needed for those drivers that still use NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM and only those that can digest a tag.

I don't think we need to invent something new, we "just" need to tell the DSA conduit interface what type of switch tagger it is attached to and where it is in the Ethernet frame. Once we do that, the DSA conduit ought to be able to strip out features statically, or dynamically via ndo_features_check().

Signature · 1 line
Florian
author maintainer
#78 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 20 lines · Luiz Angelo Daros de Luca
Quoted 8 lines
Quoted 7 lines

Could you implement a prototype of packet parsing in ndo_features_check, which checks for the known DSA EtherType and clears the offload bit for unsupported packets, and do some performance testing before and after, to lean the argument in your favor with some numbers? I've no problem if you test for the worst case, i.e. line rate with small UDP packets encapsulated with the known (offload-capable) DSA tag format, where there is little benefit for offloading TX checksumming.

Quoted 10 lines

There is no way to tell if a packet has a DSA tag only by parsing its content. For Realtek and Marvel EDSA, there is a distinct ethertype (although Marvel EDSA uses a non-registered number) that drivers can check. For others, specially those that add the tag before the ethernet header or after the payload, it might not have a magic number. It is impossible to securely identify if and which DSA is in use for some DSA tags from the packet alone. This is also the case for mediatek. Although it places its tag just before ethertype (like Realtek and Marvel), there is no magic number. It needs some context to know what type of DSA was applied.

Quoted 6 lines

Looking at mtk_eth_soc.h TX_DMA_CHKSUM is 0x7 << 29 so we set 3 bits there, which makes me think that either we defined too many bits, or some of those bits have a compounded meaning. The rest of the bits do not seem to be defined, so maybe there is a programmable offset where to calculate the checksum from and deposit it. Is there a public programmable manual?

Thanks Florian, I'm using this that I googled ;-)

http://download.villagetelco.org/hardware/MT7620/MT7620_ProgrammingGuide.pdf
page 206?

It says:

DWORD0 31:0 SDP0 Segment Data Pointer0

DWORD1 31 DDONE DMA Done: Indicates DMA has transferred the segment pointed to by this Tx
descriptor.
30 LS0 Last Segment0: Data pointed to by SDP0 is the last segment. 29:16 SDL0 Segment Data Length0: Segment data length for the data pointed to by SDP0.
15 BURST When set, the scheduler cannot hand over to other Tx queues. Should not transmit
the next packet.
14 LS1 Last Segment1: Data pointed to by SDP1 is the last segment. 13:0 SDL1 Segment Data Length1: Segment data length for the data pointed to by SDP1.

DWORD2
31:0 SDP1 Segment Data Pointer1

DWORD3 (TXINFO)
31 ICO IP checksum offload enable
30 UCO UDP checksum offload enable
23 TCO TCP checksum offload enable
28 TSO TCP segmentation offload
27:20 FP_BMAP Forced destination port on GSW
bit[0:5]: Ports 0 to 5
bit[6]: CPU
bit[7]: PPE
FP_BMAP = 0: routing by DA
19:15 UDF User defined field
14 0 Reserved
13 0 Reserved
12 INSP Insert PPPoE header
11:8 SIDX PPPoE session index
7 INSV Insert VLAN tag
6:4 VPRI VLAN priority tag to be inserted
3:0 VIDX VLAN ID index

It looks like st->txd.txd4 is DWORD3. There is nothing too useful for pointing L3 headers. The remaining bits are about vlan and pppoe offload (and forcing the forwarding port).
There are those two segment 0 and 1 data pointers and their size in txd{1,2,3} that I don't understand how it works (yet), but I guess that is not what we are looking for.

There are also some offload settings at CDMA (page 217), but it is simply an enable bit. (And I don't know what CDMA or GDMA are :-/).

Quoted 11 lines
Quoted 10 lines

skb_buf today knows nothing about the added DSA tag. Although net_device does know if it is a master port in a dsa tree, and it has a default dsa tag, with multiple switches using different tags, it cannot tell which dsa tag was added to that packet.
That is the information I need to test if that tag is supported or not by this drive.

I believe once an offload HW can digest a dsa tag, it might support the same type of protocols with or without the tag.
In the end, what really matters is if a driver supports a specific dsa tag.

Quoted 6 lines

To be honest, I am not sure if we need to know about the specific details of the tag like is it Realtek, Broadcom, Mediatek, QCA, more than knowing whether the L3/L4 offsets will be at "expected" locations. By that I mean, located at 14 bytes from the start of the frame for IP without VLAN , and 18 bytes with VLAN, did we "stack" switch tags on top of another thus moving by another X bytes etc.

I would be perfect if the HW supported that (and I'm afraid it does not).

Quoted 8 lines
Quoted 7 lines

Wouldn't it be much easier to have a dedicated optional ndo_dsa_tag_supported()? It would be only needed for those drivers that still use NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM and only those that can digest a tag.

Quoted 5 lines

I don't think we need to invent something new, we "just" need to tell the DSA conduit interface what type of switch tagger it is attached to and where it is in the Ethernet frame. Once we do that, the DSA conduit ought to be able to strip out features statically, or dynamically via ndo_features_check().

Is it a 1:1 relation between tags and the DSA conduit interface (I'm guessing this is the CPU port Ethernet device)?
Anyway, this mediatek does not seem to support multiple tags. This patch might disable offloading when it is using any DSA tags but the mediatek one.
I never used the NONE tag but maybe I should also exempt that tag from disabling offloading.

(quoted diff) +0 -0

b/target/linux/ramips/files/drivers/net/ethernet/ralink/mtk_eth_soc.c index 0ae520183b..8eb5dd8721 100644

target/linux/ramips/files/drivers/net/ethernet/ralink/mtk_eth_soc.c +0 -0
@@ -31,6 +31,7 @@

#include <linux/io.h>
#include <linux/bug.h>
#include <linux/netfilter.h>
+#include <net/dsa.h>
#include <net/netfilter/nf_flow_table.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
@@ -788,6 +789,27 @@ err_out:

       return -1;
}

+static netdev_features_t fe_features_check(struct sk_buff *skb,
+ struct net_device *dev,
+ netdev_features_t features)
+{
+ /* No point in doing any of this if neither checksum nor GSO are

+        * being requested for this frame. We can rule out both by just
+        * checking for CHECKSUM_PARTIAL
+        */

+ if (skb->ip_summed != CHECKSUM_PARTIAL)
+ return features;
+
+ if (netdev_uses_dsa(dev)) {
+ struct dsa_device_ops *tag_ops = dev->dsa_ptr->tag_ops;
+
+ if (tag_ops && (tag_ops->proto != DSA_TAG_PROTO_MTK))
+ features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
+ }
+
+ return features;
+}
+
static inline int fe_skb_padto(struct sk_buff *skb, struct fe_priv *priv)

{
       unsigned int len;

@@ -1523,6 +1545,7 @@ static const struct net_device_ops fe_netdev_ops = { #ifdef CONFIG_NET_POLL_CONTROLLER
.ndo_poll_controller = fe_poll_controller,
#endif
+ .ndo_features_check = fe_features_check,
};

static void fe_reset_pending(struct fe_priv *priv)

However, I still feel odd to call a function for every single packet when the return value is exclusively dependent on a state that will change only when the CPU port joins or leaves the DSA switch (or when tag is changed).

Regards,

Luiz

#79 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

In reply to Vladimir Oltean
Quoted 145 lines · Vladimir Oltean
Quoted 45 lines · Jakub Kicinski
Quoted 38 lines · On Mon, 24 Jan 2022 22:56:07 +0200 Vladimir Oltean wrote:
Quoted 10 lines · Jakub Kicinski
Quoted 4 lines · On Mon, 24 Jan 2022 21:08:45 +0200 Vladimir Oltean wrote:

So before we declare that any given Ethernet driver is buggy for declaring NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM and not checking that skb->csum_start points where it expects it to (taking into consideration potential VLAN headers, IPv6 extension headers),

Extension headers are explicitly not supported by NETIF_F_IPV6_CSUM.

IIRC Tom's hope was to delete NETIF_F_IP*_CSUM completely once all drivers are converted to parsing and therefore can use NETIF_F_HW_CSUM.

IIUC, NETIF_F_IP*_CSUM vs NETIF_F_HW_CSUM doesn't make that big of a difference in terms of what the driver should check for, if the hardware checksum offload engine can't directly be given the csum_start and csum_offset, wherever they may be.

Quoted 11 lines
Quoted 2 lines

is there any driver that _does_ perform these checks correctly, that could be used as an example?

I don't think so. Let me put it this way - my understanding is that up until now we had been using the vlan_features, mpls_features etc to perform L2/L2.5/below-IP feature stripping. This scales poorly to DSA tags, as discussed in this thread.

I'm suggesting we extend the kind of checking we already do to work around inevitable deficiencies of device parsers for tunnels to DSA tags.

Sorry, I'm very tired and I probably don't understand what you're saying, so excuse the extra clarification questions.

The typical protocol checking that drivers with NETIF_F_HW_CSUM do seems to be based on vlan_get_protocol()/skb->protocol/skb_network_header()/ skb_transport_header() values, all of which make DSA invisible. So they don't work if the underlying hardware really doesn't like seeing an unexpected DSA header.

Quoted 5 lines

When you say "I'm suggesting we extend the kind of checking we already do", do you mean we should modify the likes of e1000e and igb such that, if they're ever used as DSA masters, they do a full header parse of the packet (struct ethhdr :: h_proto, check if VLAN, struct iphdr/ipv6hdr, etc.) instead of the current logic?

Quoted 5 lines

That was my thinking, yes. The exact amount of work depends on the driver, I believe that more recent Intel parts (igb, ixgbe and newer) pass a L3 offset to the HW. They treat L2 as opaque, ergo no patches needed. At a glance e1000e passes the full skb_checksum_start_offset() to HW, so likely even better.

Ah, right, I missed that. I agree that a driver that uses skb->csum_start is very likely to work unmodified with DSA headers (not trailers). I didn't notice this in e1000 because I was just searching for csum_start.

Quoted 41 lines

It's only drivers for devices which actually want to parse the Ethertype that would need extra checks. (Coincidentally such devices can't support MPLS given the lack of L3 indication in the frame.)

Quoted 21 lines

It will be pretty convoluted unless
we have some helper. Because if I follow through, for a DSA-tagged IP packet on xmit, skb->protocol is certainly htons(ETH_P_IP):

ntohs(skb->protocol) = 0x800, csum_offset = 16, csum_start = 280, skb_checksum_start_offset = 54, skb->network_header = 260, skb_network_header_len = 20

skb_dump output:
skb len=94 headroom=226 headlen=94 tailroom=384
mac=(226,34) net=(260,20) trans=280
shinfo(txflags=0 nr_frags=0 gso(size=0 type=1 segs=1)) csum(0x100118 ip_summed=3 complete_sw=0 valid=0 level=0) hash(0x7710ee84 sw=0 l4=1) proto=0x0800 pkttype=0 iif=0
dev name=eno2 feat=0x00020100001149a9
sk family=2 type=1 proto=6
skb headroom: 00000000: 6c 00 03 02 64 65 76 00 fe ed ca fe 28 00 00 00 ...(junk)...
skb headroom: 000000e0: 5f 43
20 byte DSA tag
|
v
skb linear: 00000000: 88 80 00 0a 80 00 00 00 00 00 00 00 08 00 30 00

Quoted 15 lines
                                     skb_mac_header()
                                     |
                                     v
skb linear:   00000010: 00 00 00 00 68 05 ca 92 af 20 00 04 9f 05 f6 28
                               skb_network_header()
                               |
                               v
skb linear:   00000020: 08 00 45 00 00 3c 26 47 40 00 40 06 00 49 0a 00
                                           skb_checksum_start_offset
                                           |
                                           |                       csum_offset
                                           v                       v
skb linear:   00000030: 00 2c 0a 00 00 01 b6 08 14 51 11 1f 91 4f 00 00
skb linear:   00000040: 00 00 a0 02 fa f0 14 5b 00 00 02 04 05 b4 04 02
skb linear:   00000050: 08 0a 2e 00 e5 b8 00 00 00 00 01 03 03 07
Quoted 4 lines

Oof, so in this case the DSA tag is _before_ the skb_mac_header()? Or the prepend is supposed to be parsable as a Ethernet header? Seems like any device that can do csum over this packet must already use L3/L4 offsets or have explicit knowledge of DSA, right?

I'm sorry, there's a mistake, skb_mac_header() points to the DSA tag here (and skb_mac_header_len is 34), I wanted to say "real MAC header" but failed to do so. This case shouldn't pose any special problems.

Quoted 9 lines
Quoted 3 lines

I don't know, I just don't expect that non-DSA users of those drivers will be very happy about such changes. Do these existing protocol checking schemes qualify as buggy?

Unfortunate reality of the checksum offloads is that most drivers for devices which parse on Tx are buggy, it's more of a question of whether anyone tried to use an unsupported protocol stack :( Recent example that comes to mind is 1698d600b361 ("bnxt_en: Implement .ndo_features_check().").

Nice hook this ndo_features_check! In my reading of validate_xmit_skb() I went right past it.

Quoted 13 lines
Quoted 5 lines

If this is the convention that we want to enforce, then I can't really help Luiz with fixing the OpenWRT mtk_eth_soc.c - he'll have to figure out a way to parse the packets for which his hardware will accept the checksumming offload, and call skb_checksum_help() otherwise.

Quoted 7 lines
Quoted 4 lines

We can come up with various schemes of expressing capabilities between underlying driver and tag driver. I'm not aware of similar out-of-band schemes existing today so it'd be "DSA doing it's own thing", which does not seem great.

It at least seems less complex to me, and less checking in the fast path if I understand everything that's been said correctly.

Quoted 7 lines

I understand, I'm primarily trying to share some context and prior work. I don't mean to nack all other approaches.

I believe writing a parser matching the device behavior would be easier for a driver author than interpreting the runes of our csum offload API and getting thru the thicket of all the bits. If that's not the case my argument is likely defeated.

Ok, writing a parser might be needed if the DSA master is going, for some reason, to support TX checksum offloading with some DSA headers but not with others.

Quoted 5 lines

If that is not the case, and that Ethernet controller simply doesn't support TX checksumming unless it's the plain old Ethernet {+ VLAN} + IP/IPv6 + TCP/UDP, then this blanket patch below should fix the problem almost elegantly, and parsing is a useless complication (warning, not even compile-tested!):

I tried it on the upstream mtk_eth_soc on an mt7621a board with an rtl8367s switch connected to the 2nd GMAC of the SoC running 5.17-rc1.

Although "ethtool --show-offload eth1" does not show anything different, I can now ssh to the device (only way that came into my mind to quickly check TCP traffic).

drivers/net/ethernet/mediatek/mtk_eth_soc.c +11 -0
@@ -1986,6 +1986,16 @@ static int mtk_hwlro_get_fdir_all(struct
 	return 0;
 }
 
+static netdev_features_t fe_features_check(struct sk_buff *skb,
+					   struct net_device *dev,
+					   netdev_features_t features)
+{
+	if (netdev_uses_dsa(dev))
+		features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
+
+	return features;
+}
+
 static netdev_features_t mtk_fix_features(struct net_device *dev,
 					  netdev_features_t features)
 {
@@ -2906,6 +2916,7 @@ static const struct net_device_ops mtk_n
 #ifdef CONFIG_NET_POLL_CONTROLLER
 	.ndo_poll_controller	= mtk_poll_controller,
 #endif
+	.ndo_features_check	= fe_features_check,
 	.ndo_setup_tc		= mtk_eth_setup_tc,
 };
 
Quoted 69 lines

-----------------------------[ cut here ]-----------------------------
From 5ef3d3cd8441d756933558212f518f48754c64d9 Mon Sep 17 00:00:00 2001
From: Vladimir Oltean <[email protected]>
Date: Tue, 25 Jan 2022 00:16:57 +0200
Subject: [PATCH] ramips: ethernet: ralink: disable TX checksumming on DSA
masters

---
  .../files/drivers/net/ethernet/ralink/mtk_eth_soc.c  | 12 ++++++++++++
  1 file changed, 12 insertions(+)
target/linux/ramips/files/drivers/net/ethernet/ralink/mtk_eth_soc.c +12 -1
index e07e5ed5a8f8..6ed9bc5942fd 100644
@@ -31,6 +31,7 @@
  #include <linux/io.h>
  #include <linux/bug.h>
  #include <linux/netfilter.h>
+#include <net/dsa.h>
  #include <net/netfilter/nf_flow_table.h>
  #include <linux/of_gpio.h>
  #include <linux/gpio.h>
@@ -1497,6 +1498,16 @@ static int fe_change_mtu(struct net_device *dev, int new_mtu)
  	return fe_open(dev);
  }
 
+static netdev_features_t fe_features_check(struct sk_buff *skb,
+					   struct net_device *dev,
+					   netdev_features_t features)
+{
+	if (netdev_uses_dsa(dev))
+		features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
+
+	return features;
+}
+
  static const struct net_device_ops fe_netdev_ops = {
  	.ndo_init		= fe_init,
  	.ndo_uninit		= fe_uninit,
@@ -1514,6 +1525,7 @@ static const struct net_device_ops fe_netdev_ops = {
  #ifdef CONFIG_NET_POLL_CONTROLLER
  	.ndo_poll_controller	= fe_poll_controller,
  #endif
+	.ndo_features_check	= fe_features_check,
  };
 
  static void fe_reset_pending(struct fe_priv *priv)
-----------------------------[ cut here ]-----------------------------

This is essentially what Florian said ages ago, it just took me a very long time to process. I guess what hadn't fully clicked in my head is that the TX checksumming offload being functional is more a matter of telling the hardware what are the L3 and L4 offsets, and the csum_offset, rather than it requiring any particular understanding of the DSA header. In turn, it means that for "nice" Ethernet controller implementations where that is the case, it would be actively detrimential to add a new .ndo_get_dsa_features() or something like that - because such a driver would report that it supports all DSA header-type formats (trailers are still broken as long as there isn't a csum_end). And keeping that kind of driver in sync with all DSA protocols that appear will become a repetitive task.

So crisis averted, I guess?
Thanks a lot to both of you for the patient explanations!
I retract my proposal for a new ndo and also suggest that the DSA master driver takes care to not leave as zero a TX checksum it can't offload.

maintainer
#80 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

In reply to Luiz Angelo Daros de Luca
Quoted 10 lines

trap_port seems to be where the switch will send any packet captured from LAN ports. There are a couple of situations it will be used like:
1) untagged or unmatched vlan packets (if configured to do so)
2) some multicasting packets (Reserved Multicast Address), for some cases like capturing STP or LACP
3) IGMP and 802.1X EAPOL packets
4) Switch ACL rules that could match a packet and send it to the trap port.

In my early tests, I only saw some IGMP packets trapped to CPU. I also do not know how important they are.

STP is important for detecting loops in the ethernet traffic and blocking ports. The linux software bridge will want to see these packets.

IGMP will become important when you implement multicast support in the switch. It will allow you to optimize the distribution of multicast to only ports which have expressed an interest in receiving the group.

Currently we don't have any switch driver making use of 802.1x. It is something which many switches have, but so far nobody has spent the time to implement an interface to wpa_supplicant etc.

Andrew

author maintainer
#81 lore raw

Re: Re: Re: Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

In reply to Luiz Angelo Daros de Luca
Quoted 6 lines

I suggested it might be checksum problem because I'm also affected. In my case, I have an mt7620a SoC connected to the rtl8367s switch. The OS offloads checksum to HW but the mt7620a cannot calculate the checksum with the (EtherType) Realtek CPU Tag in place. I'll try to move the CPU tag to test if the mt7620a will then digest the frame correctly.

I implemented a new DSA tag (rtl8_4t, with "t" as in trailing) that puts the DSA tag before the Ethernet CRC (the switch supports both). With no tag in the mac layer, mediatek correctly calculated the ip checksum. However, mediatek SoC included the extra bytes from the DSA tag in the TCP checksum, even if they are after the ip length.

This is the packet leaving the OS:

0000   04 0e 3c fc 4f aa 50 d4 f7 33 15 8a 08 00 45 10
0010   00 3c 00 00 40 00 40 06 b7 58 c0 a8 01 01 c0 a8
0020   01 02 00 16 a1 50 80 da 39 e9 b2 2a 23 cf a0 12
0030   fe 88 83 82 00 00 02 04 05 b4 04 02 08 0a 01 64
0040   fb 28 66 42 e0 79 01 03 03 03 88 99 04 00 00 20
0050   00 08

TCP checksum is at 0x0032 with 0x8382 is the tcp checksum DSA Tag is at 0x4a with 8899040000200008

This is what arrived at the other end:

0000   04 0e 3c fc 4f aa 50 d4 f7 33 15 8a 08 00 45 10
0010   00 3c 00 00 40 00 40 06 b7 58 c0 a8 01 01 c0 a8
0020   01 02 00 16 a1 50 80 da 39 e9 b2 2a 23 cf a0 12
0030   fe 88 c3 e8 00 00 02 04 05 b4 04 02 08 0a 01 64
0040   fb 28 66 42 e0 79 01 03 03 03

TCP checksum is 0xc3e8, but the correct one should be 0x50aa
If you calculate tcp checksum including 8899040000200008, you'll get exactly 0xc3e8 (I did the math).

So, If we use a trailing DSA tag, we can leave the IP checksum offloading on and just turn off the TCP checksum offload. Is it worth it?

Is it still interesting to have the rtl8_4t merged?

Regards,

author maintainer
#82 lore raw

Re: Re: Re: Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 33 lines
Quoted 6 lines

I suggested it might be checksum problem because I'm also affected. In my case, I have an mt7620a SoC connected to the rtl8367s switch. The OS offloads checksum to HW but the mt7620a cannot calculate the checksum with the (EtherType) Realtek CPU Tag in place. I'll try to move the CPU tag to test if the mt7620a will then digest the frame correctly.

I implemented a new DSA tag (rtl8_4t, with "t" as in trailing) that puts the DSA tag before the Ethernet CRC (the switch supports both). With no tag in the mac layer, mediatek correctly calculated the ip checksum. However, mediatek SoC included the extra bytes from the DSA tag in the TCP checksum, even if they are after the ip length.

This is the packet leaving the OS:

0000   04 0e 3c fc 4f aa 50 d4 f7 33 15 8a 08 00 45 10
0010   00 3c 00 00 40 00 40 06 b7 58 c0 a8 01 01 c0 a8
0020   01 02 00 16 a1 50 80 da 39 e9 b2 2a 23 cf a0 12
0030   fe 88 83 82 00 00 02 04 05 b4 04 02 08 0a 01 64
0040   fb 28 66 42 e0 79 01 03 03 03 88 99 04 00 00 20
0050   00 08

TCP checksum is at 0x0032 with 0x8382 is the tcp checksum DSA Tag is at 0x4a with 8899040000200008

This is what arrived at the other end:

0000   04 0e 3c fc 4f aa 50 d4 f7 33 15 8a 08 00 45 10
0010   00 3c 00 00 40 00 40 06 b7 58 c0 a8 01 01 c0 a8
0020   01 02 00 16 a1 50 80 da 39 e9 b2 2a 23 cf a0 12
0030   fe 88 c3 e8 00 00 02 04 05 b4 04 02 08 0a 01 64
0040   fb 28 66 42 e0 79 01 03 03 03
Quoted 6 lines

TCP checksum is 0xc3e8, but the correct one should be 0x50aa
If you calculate tcp checksum including 8899040000200008, you'll get exactly 0xc3e8 (I did the math).

So, If we use a trailing DSA tag, we can leave the IP checksum offloading on and just turn off the TCP checksum offload. Is it worth it?

No, IP checksum is always done in SW.

Quoted 1 line

Is it still interesting to have the rtl8_4t merged?

Maybe it is. It has uncovered a problem. The case of trailing tags seems to be unsolvable even with csum_start. AFAIK, the driver must cksum from "skb->csum_start up to the end". When the switch is using an incompatible tag, we have:

slave(): my features copied from master tells me I can offload checksum. Do nothing
tagger(): add tag to the end of skb
master(): Offloading HW, chksum from csum_start until the end, including the added tag
switch(): remove the tag, forward to the network remove_client(): I got a packet with a broken checksum.

ndo_features_check() will not help because, either in HW or SW, it is expected to calculate the checksum up to the end. However, there is no csum_end or csum_len. I don't know if HW offloading will support some kind of csum_end but it would not be a problem in SW (considering skb_checksum_help() is adapted to something like skb_checksum_trimmed without the clone).

That amount of bytes to ignore at the end is a complex question: the driver either needs some hint (like it happens with skb->csum_offset) to know where transport payload ends or the taggers (or the dsa) must save the amount of extra bytes (or which tags were added) in the sbk_buff. With that info, the driver can check if HW will work with a different csum_start / csum_end or if only a supported tag is in use.

In my case, using an incompatible tailing tag, I just made it work hacking dsa and forcing slave interfaces to disable offloading. This way, checksum is calculated before any tag is added and offloading is skipped. But it is not a real solution.

Regards,

Luiz,

#83 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 49 lines · Luiz Angelo Daros de Luca
Quoted 33 lines
Quoted 6 lines

I suggested it might be checksum problem because I'm also affected. In my case, I have an mt7620a SoC connected to the rtl8367s switch. The OS offloads checksum to HW but the mt7620a cannot calculate the checksum with the (EtherType) Realtek CPU Tag in place. I'll try to move the CPU tag to test if the mt7620a will then digest the frame correctly.

I implemented a new DSA tag (rtl8_4t, with "t" as in trailing) that puts the DSA tag before the Ethernet CRC (the switch supports both). With no tag in the mac layer, mediatek correctly calculated the ip checksum. However, mediatek SoC included the extra bytes from the DSA tag in the TCP checksum, even if they are after the ip length.

This is the packet leaving the OS:

0000   04 0e 3c fc 4f aa 50 d4 f7 33 15 8a 08 00 45 10
0010   00 3c 00 00 40 00 40 06 b7 58 c0 a8 01 01 c0 a8
0020   01 02 00 16 a1 50 80 da 39 e9 b2 2a 23 cf a0 12
0030   fe 88 83 82 00 00 02 04 05 b4 04 02 08 0a 01 64
0040   fb 28 66 42 e0 79 01 03 03 03 88 99 04 00 00 20
0050   00 08

TCP checksum is at 0x0032 with 0x8382 is the tcp checksum DSA Tag is at 0x4a with 8899040000200008

This is what arrived at the other end:

0000   04 0e 3c fc 4f aa 50 d4 f7 33 15 8a 08 00 45 10
0010   00 3c 00 00 40 00 40 06 b7 58 c0 a8 01 01 c0 a8
0020   01 02 00 16 a1 50 80 da 39 e9 b2 2a 23 cf a0 12
0030   fe 88 c3 e8 00 00 02 04 05 b4 04 02 08 0a 01 64
0040   fb 28 66 42 e0 79 01 03 03 03
Quoted 6 lines

TCP checksum is 0xc3e8, but the correct one should be 0x50aa
If you calculate tcp checksum including 8899040000200008, you'll get exactly 0xc3e8 (I did the math).

So, If we use a trailing DSA tag, we can leave the IP checksum offloading on and just turn off the TCP checksum offload. Is it worth it?

No, IP checksum is always done in SW.

Quoted 1 line

Is it still interesting to have the rtl8_4t merged?

Maybe it is. It has uncovered a problem. The case of trailing tags seems to be unsolvable even with csum_start. AFAIK, the driver must cksum from "skb->csum_start up to the end". When the switch is using an incompatible tag, we have:

Quoted 7 lines

slave(): my features copied from master tells me I can offload checksum. Do nothing
tagger(): add tag to the end of skb
master(): Offloading HW, chksum from csum_start until the end, including the added tag
switch(): remove the tag, forward to the network remove_client(): I got a packet with a broken checksum.

This is unfortunately expected here, because you program the hardware with the full Ethernet frame length which does include the trailer tag, and it then uses that length to calculate the transport header checksum over the enter payload, thinking the trailer tag is the UDP/TCP payload.

The checksum is calculated "on the fly" as part of the DMA operation to send the packet on the wire, so you cannot decouple the checksum calculation from the DMA operation, other than by not asking the HW *not to* checksum the packet, and instead having software provide that.

Now looking at the datasheet you quoted, there is this:

241. FE_GLO_CFG: Frame Engine Global Configuration (offset: 0x0000)

7:4 RW L2_SPACE L2 Space
(unit: 8 bytes)
0xB

Can you play with this and see if you can account for the extra 4 bytes added by the Realtek tag?

Quoted 15 lines

ndo_features_check() will not help because, either in HW or SW, it is expected to calculate the checksum up to the end. However, there is no csum_end or csum_len. I don't know if HW offloading will support some kind of csum_end but it would not be a problem in SW (considering skb_checksum_help() is adapted to something like skb_checksum_trimmed without the clone).

That amount of bytes to ignore at the end is a complex question: the driver either needs some hint (like it happens with skb->csum_offset) to know where transport payload ends or the taggers (or the dsa) must save the amount of extra bytes (or which tags were added) in the sbk_buff. With that info, the driver can check if HW will work with a different csum_start / csum_end or if only a supported tag is in use.

Quoted 4 lines

In my case, using an incompatible tailing tag, I just made it work hacking dsa and forcing slave interfaces to disable offloading. This way, checksum is calculated before any tag is added and offloading is skipped. But it is not a real solution.

Not sure which one is not a "real solution", but for this specific combination of DSA conduit driver and switch tag, you have to disable checksum offload in the conduit driver and provide it in software. The other way would be to configure the realtek switch to work with DSA_TAG_8021Q and see if you can continue to offload the data path since tagging would use regular 802.1Q vlans, but that means you are going to lose a whole lot of management functionality offered by the native Realtek tag.

Signature · 1 line
Florian
author maintainer
#84 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 70 lines · Luiz Angelo Daros de Luca
Quoted 49 lines
Quoted 33 lines
Quoted 6 lines

I suggested it might be checksum problem because I'm also affected. In my case, I have an mt7620a SoC connected to the rtl8367s switch. The OS offloads checksum to HW but the mt7620a cannot calculate the checksum with the (EtherType) Realtek CPU Tag in place. I'll try to move the CPU tag to test if the mt7620a will then digest the frame correctly.

I implemented a new DSA tag (rtl8_4t, with "t" as in trailing) that puts the DSA tag before the Ethernet CRC (the switch supports both). With no tag in the mac layer, mediatek correctly calculated the ip checksum. However, mediatek SoC included the extra bytes from the DSA tag in the TCP checksum, even if they are after the ip length.

This is the packet leaving the OS:

0000   04 0e 3c fc 4f aa 50 d4 f7 33 15 8a 08 00 45 10
0010   00 3c 00 00 40 00 40 06 b7 58 c0 a8 01 01 c0 a8
0020   01 02 00 16 a1 50 80 da 39 e9 b2 2a 23 cf a0 12
0030   fe 88 83 82 00 00 02 04 05 b4 04 02 08 0a 01 64
0040   fb 28 66 42 e0 79 01 03 03 03 88 99 04 00 00 20
0050   00 08

TCP checksum is at 0x0032 with 0x8382 is the tcp checksum DSA Tag is at 0x4a with 8899040000200008

This is what arrived at the other end:

0000   04 0e 3c fc 4f aa 50 d4 f7 33 15 8a 08 00 45 10
0010   00 3c 00 00 40 00 40 06 b7 58 c0 a8 01 01 c0 a8
0020   01 02 00 16 a1 50 80 da 39 e9 b2 2a 23 cf a0 12
0030   fe 88 c3 e8 00 00 02 04 05 b4 04 02 08 0a 01 64
0040   fb 28 66 42 e0 79 01 03 03 03
Quoted 6 lines

TCP checksum is 0xc3e8, but the correct one should be 0x50aa
If you calculate tcp checksum including 8899040000200008, you'll get exactly 0xc3e8 (I did the math).

So, If we use a trailing DSA tag, we can leave the IP checksum offloading on and just turn off the TCP checksum offload. Is it worth it?

No, IP checksum is always done in SW.

Quoted 1 line

Is it still interesting to have the rtl8_4t merged?

Maybe it is. It has uncovered a problem. The case of trailing tags seems to be unsolvable even with csum_start. AFAIK, the driver must cksum from "skb->csum_start up to the end". When the switch is using an incompatible tag, we have:

Quoted 7 lines

slave(): my features copied from master tells me I can offload checksum. Do nothing
tagger(): add tag to the end of skb
master(): Offloading HW, chksum from csum_start until the end, including the added tag
switch(): remove the tag, forward to the network remove_client(): I got a packet with a broken checksum.

This is unfortunately expected here, because you program the hardware with the full Ethernet frame length which does include the trailer tag, and it then uses that length to calculate the transport header checksum over the enter payload, thinking the trailer tag is the UDP/TCP payload.

The checksum is calculated "on the fly" as part of the DMA operation to send the packet on the wire, so you cannot decouple the checksum calculation from the DMA operation, other than by not asking the HW *not to* checksum the packet, and instead having software provide that.

Now looking at the datasheet you quoted, there is this:

Quoted 9 lines

241. FE_GLO_CFG: Frame Engine Global Configuration (offset: 0x0000)

7:4 RW L2_SPACE L2 Space
(unit: 8 bytes)
0xB

Can you play with this and see if you can account for the extra 4 bytes added by the Realtek tag?

I played with it, both with the L2_SPACE and RATE_MINUS:

FE_GLO_CFG_REG=0x10100000 FE_GLO_CFG_SIZE=32 FE_GLO_CFG=$(($(devmem $FE_GLO_CFG_REG $FE_GLO_CFG_SIZE))); for l2space_sig in b0 b1 c0 c1 d0 d1 e0 e1 a0 a1 90 91 80 81 70 71 60 61 50 51 40 41 30 31 20 21 10 11 01 00 e0 e1; do

    FE_GLO_CFG=$(($(devmem $FE_GLO_CFG_REG $FE_GLO_CFG_SIZE)));
    printf 'Before FE_GLO_CFG = 0x%X\n' $FE_GLO_CFG;
    devmem $FE_GLO_CFG_REG $FE_GLO_CFG_SIZE $((FE_GLO_CFG & ~0x00000ff
| (0x$l2space_sig)));
    FE_GLO_CFG=$(($(devmem $FE_GLO_CFG_REG $FE_GLO_CFG_SIZE)));
    printf 'After  FE_GLO_CFG = 0x%X\n' $FE_GLO_CFG;
    echo "Please test L2_SPACE_sig==$l2space_sig"; read;

done; devmem $FE_GLO_CFG_REG $FE_GLO_CFG_SIZE $FE_GLO_CFG_ORIG

It only made a difference for values 0x0 and 0xf but it looks more like an overflow. And only on the traffic I receive, not send. The remote endpoint
always receive 0x8382 as the tcp checksum, which is the "fake ip header" sum.

The default value is 0xb (11) and docs says it is a 8-byte unit. What is 11 * 8 bytes? 88 bytes? Maybe it is wrong in docs.
That same register also has EXT_VLAN, which points to 0x8100 (802.1Q ethertype).

In the same doc, there is also a mention about the L2 space usage, only related to received traffic:

"1. RX_CTRL pass through VLAN tags on L2 space (at most 2 tags)" (page 245-247)

Anyway, even if the Mediatek switch could remove the Realtek tag, it should not do that. The Realtek switch still needs it.

Quoted 7 lines
Quoted 7 lines

ndo_features_check() will not help because, either in HW or SW, it is expected to calculate the checksum up to the end. However, there is no csum_end or csum_len. I don't know if HW offloading will support some kind of csum_end but it would not be a problem in SW (considering skb_checksum_help() is adapted to something like skb_checksum_trimmed without the clone).

Quoted 6 lines
Quoted 6 lines

That amount of bytes to ignore at the end is a complex question: the driver either needs some hint (like it happens with skb->csum_offset) to know where transport payload ends or the taggers (or the dsa) must save the amount of extra bytes (or which tags were added) in the sbk_buff. With that info, the driver can check if HW will work with a different csum_start / csum_end or if only a supported tag is in use.

I must be missing something. Is SW TCP checksum really broken when a tailing tag is in use? If so, it will only work if TCP checksum offload is enabled in a compatible HW. Anything else like different vendors, software checksum or stacked tags will be broken.

Quoted 5 lines
Quoted 4 lines

In my case, using an incompatible tailing tag, I just made it work hacking dsa and forcing slave interfaces to disable offloading. This way, checksum is calculated before any tag is added and offloading is skipped. But it is not a real solution.

Quoted 8 lines

Not sure which one is not a "real solution", but for this specific combination of DSA conduit driver and switch tag, you have to disable checksum offload in the conduit driver and provide it in software. The other way would be to configure the realtek switch to work with DSA_TAG_8021Q and see if you can continue to offload the data path since tagging would use regular 802.1Q vlans, but that means you are going to lose a whole lot of management functionality offered by the native Realtek tag.

Definitely not a real solution. It was just a hack to check if checksumming at slave device will overcome the issue. As I said, simply disabling checksum and doing it in SW "as usual" is not enough because SW checksum also sums to the end. We need to parse each possible transport layer to find its end or taggers must hint how many bytes to ignore, something like a new skb->cksum_stop_before_end. Another solution would be to hint the slave interface if it needs to checksum right there (modifying slave->vlan_features). None of that exists today. Is it the right way?

--

Luiz

maintainer
#85 lore raw

Re: [PATCH net-next v4 11/11] net: dsa: realtek: rtl8365mb: multiple cpu ports, non cpu extint

Quoted 14 lines · Luiz Angelo Daros de Luca
Quoted 5 lines
Quoted 4 lines

In my case, using an incompatible tailing tag, I just made it work hacking dsa and forcing slave interfaces to disable offloading. This way, checksum is calculated before any tag is added and offloading is skipped. But it is not a real solution.

Quoted 8 lines

Not sure which one is not a "real solution", but for this specific combination of DSA conduit driver and switch tag, you have to disable checksum offload in the conduit driver and provide it in software. The other way would be to configure the realtek switch to work with DSA_TAG_8021Q and see if you can continue to offload the data path since tagging would use regular 802.1Q vlans, but that means you are going to lose a whole lot of management functionality offered by the native Realtek tag.

Quoted 9 lines

Definitely not a real solution. It was just a hack to check if checksumming at slave device will overcome the issue. As I said, simply disabling checksum and doing it in SW "as usual" is not enough because SW checksum also sums to the end. We need to parse each possible transport layer to find its end or taggers must hint how many bytes to ignore, something like a new skb->cksum_stop_before_end. Another solution would be to hint the slave interface if it needs to checksum right there (modifying slave->vlan_features). None of that exists today. Is it the right way?

I think we're not getting any closer to a solution if we've started discussing tail taggers.

See commit 37120f23ac89 ("net: dsa: tag_ksz: dont let the hardware process the layer 4 checksum"). It proves that if you calculate the L4 checksum in software before inserting the DSA tag, it won't get recalculated upon dev_queue_xmit() on the DSA master, since skb_checksum_help() transitions skb->ip_summed to CHECKSUM_NONE, and the process of inserting a header/trailer will not update the checksum, so it will end up being correct on the receive end after the tail tag is stripped.

Otherwise, I don't completely understand what is the end goal you're after. Each skb is checked for netdev features when determining whether to calculate the L4 checksum in software or not. Then even if that skb was marked for L4 checksum offload by the stack, you can still call skb_checksum_help() from the xmit procedure of the driver.

Do you want hardware offloading with your DSA header, or why do you say that forcing slave interfaces to disable the offload is not a real solution? If so, I recommend looking into a custom tagging protocol based on tag_8021q.c, but word of warning, some elbow grease will be required.

If you're ok with software checksumming and just want the minimum amount of checks in the fastpath, I believe you should listen for NETDEV_CHANGEUPPER events in your DSA master driver, where dsa_slave_dev_check(info->upper_dev) is true. From there you should be able to retrieve the tagging protocol used (if you can't, then export some helpers that will do that), and enable NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM in master->features if the tag is Mediatek, clear them otherwise. See bcmsysport.c for an example.
The timing of this notifier is such that it's pointless to mangle master->vlan_features at that stage, since DSA has already inherited them. So DSA slaves would still report NETIF_F_IP_CSUM, but the DSA master would force a software calculation from the correct L3 & L4 offsets, and it would practically work.
Alternatively, I think you could move dsa_slave_setup_tagger() beneath netdev_upper_dev_link(), and this would give the DSA master an opportunity to modulate its master->vlan_features in a way that is desirable to you. I don't see something that would break if you do that.

As Florian and Jakub explained, the APIs for TX checksumming are what they are, I'm not very happy with the state of things either, but I can't justify a DSA-specific API. With HW_CSUM, the stack gives you an L3 and L4 offset, and that is compatible with DSA headers (not trailers), so the onus is on the DSA master to fall back to software on offsets it doesn't like. One could argue that DSA should not work with IP_CSUM | IPV6_CSUM, but I believe that there are existing drivers that use these checksum features and that do work at least with certain DSA tagging protocols (bcmsysport) or even look at the L3 and L4 offsets (mvneta), meaning that they would work generically with DSA. So practically speaking, if we issue a blanket statement that DSA shouldn't inherit IP_CSUM | IPV6_CSUM but just HW_CSUM, that would still break working setups. Now, we could still do that (since IP_CSUM | IPV6_CSUM are theoretically deprecated), but then you'd have to be there and help with some more elbow grease to fix the breakage in mvneta etc, to convert them to HW_CSUM.

maintainer
#86 lore raw
In reply to Luiz Angelo Daros de Luca
Quoted 13 lines · Luiz Angelo Daros de Luca

The old realtek-smi driver was linking subdrivers into a single realtek-smi.ko After this series, each subdriver will be an independent module required by either realtek-smi (platform driver) or the new realtek-mdio (mdio driver). Both interface drivers (SMI or MDIO) are independent, and they might even work side-by-side, although it will be difficult to find such device. The subdriver can be individually selected but only at buildtime, saving some storage space for custom embedded systems.

Existing realtek-smi devices continue to work untouched during the tests. The realtek-smi was moved into a realtek subdirectory, but it normally does not break things.

Quoted 5 lines

I couldn't identify a fixed relation between port numbers (0..9) and external interfaces (0..2), and I'm not sure if it is fixed for each chip version or a device configuration. Until there is more info about it, there is a new port property "realtek,ext-int" that can inform the external interface.

Generally it isn't a good idea to put in the device tree things that you don't understand. The reason being that you'd have to support those device tree bindings even when you do understand those things. A device tree blob has a separate lifetime compared to the kernel image, so a new kernel image would have to support the device trees in circulation which have this realtek,ext-int property.

Can you use a fixed relationship between the port number and the external interface in the driver, until it is proven that this info cannot be known statically or by reading some device configuration?

Quoted 49 lines

The rtl8365mb might now handle multiple CPU ports and extint ports not used as CPU ports. RTL8367S has an SGMII external interface, but my test device (TP-Link Archer C5v4) uses only the second RGMII interface. We need a test device with more external ports to test these features. The driver still cannot handle SGMII ports.

The rtl8365mb was tested with a MDIO-connected RTL8367S (TP-Link Acher C5v4) and a SMI-connected RTL8365MB-VC switch (Asus RT-AC88U)

The rtl8366rb subdriver was not tested with this patch series, but it was only slightly touched. It would be nice to test it, especially in an MDIO-connected switch.

Best,

Luiz

Changelog:

v1-v2)
- formatting fixes
- dropped the rtl8365mb->rtl8367c rename
- other suggestions

v2-v3)
* realtek-mdio.c:
- cleanup realtek-mdio.c (BUG_ON, comments and includes)
- check devm_regmap_init return code
- removed realtek,rtl8366s string from realtek-mdio
* realtek-smi.c:
- removed void* type cast
* rtl8365mb.c:
- using macros to identify EXT interfaces
- rename some extra extport->extint cases
- allow extint as non cpu (not tested)
- allow multple cpu ports (not tested)
- dropped cpu info from struct rtl8365mb
* dropped dt-bindings changes (dealing outside this series)
* formatting issues fixed

v3-v4)
* fix cover message numbering 0/13 -> 0/11
* use static for realtek_mdio_read_reg
- Reported-by: kernel test robot <[email protected]>
* use dsa_switch_for_each_cpu_port
* mention realtek_smi_{variant,ops} to realtek_{variant,ops} in commit message

author maintainer
#87 lore raw
Quoted 27 lines · Luiz Angelo Daros de Luca
Quoted 13 lines

The old realtek-smi driver was linking subdrivers into a single realtek-smi.ko After this series, each subdriver will be an independent module required by either realtek-smi (platform driver) or the new realtek-mdio (mdio driver). Both interface drivers (SMI or MDIO) are independent, and they might even work side-by-side, although it will be difficult to find such device. The subdriver can be individually selected but only at buildtime, saving some storage space for custom embedded systems.

Existing realtek-smi devices continue to work untouched during the tests. The realtek-smi was moved into a realtek subdirectory, but it normally does not break things.

Quoted 5 lines

I couldn't identify a fixed relation between port numbers (0..9) and external interfaces (0..2), and I'm not sure if it is fixed for each chip version or a device configuration. Until there is more info about it, there is a new port property "realtek,ext-int" that can inform the external interface.

Generally it isn't a good idea to put in the device tree things that you don't understand. The reason being that you'd have to support those device tree bindings even when you do understand those things. A device tree blob has a separate lifetime compared to the kernel image, so a new kernel image would have to support the device trees in circulation which have this realtek,ext-int property.

Quoted 3 lines

Can you use a fixed relationship between the port number and the external interface in the driver, until it is proven that this info cannot be known statically or by reading some device configuration?

Thanks, Vladimir. OK. I'll make it automatically defined. I dug again into the realtek driver and I might be able to remove that property. There are just a couple of situations that I might select based on chip_id/version.

BTW, Should I split the series in two, submitting the already consensus patch?

Luiz