Skip to contentSkip to conversation

net: dsa: microchip: DSA driver support for LAN937x switch

PATCH Superseded Prasanna Vengateshan 5 participants 44 messages Jul 23, 2021 – Aug 11, 2021

Subsystems (7)

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v3 cover net: dsa: microchip: DSA driver support for LAN937x switch

LAN937x is a Multi-Port 100BASE-T1 Ethernet Physical Layer switch compliant with the IEEE 802.3bw-2015 specification. The device provides 100 Mbit/s transmit and receive capability over a single Unshielded Twisted Pair (UTP) cable. LAN937x is successive revision of KSZ series switch. This series of patches provide the DSA driver support for Microchip LAN937X switch and it configures through SPI interface.

This driver shares some of the functions from KSZ common layer.

The LAN937x switch series family consists of following SKUs:

LAN9370:
- 4 T1 Phys
- 1 RGMII port

LAN9371:
- 3 T1 Phys & 1 TX Phy
- 2 RGMII ports

LAN9372:
- 5 T1 Phys & 1 TX Phy
- 2 RGMII ports

LAN9373:
- 5 T1 Phys
- 2 RGMII & 1 SGMII port

LAN9374:
- 6 T1 Phys
- 2 RGMII ports

More support will be added at a later stage.

Changes in v3:
- Removed settings of cnt_ptr to zero and the memset() added a cleanup patch which moves this into ksz_init_mib_timer().
- Used ret everywhere instead of rc
- microchip,lan937x.yaml: Remove mdio compatible
- microchip_t1.c: Renaming standard phy registers
- tag_ksz.c: LAN937X_TAIL_TAG_OVERRIDE renaming LAN937X_TAIL_TAG_BLOCKING_OVERRIDE
- tag_ksz.c: Changed Ingress and Egress naming convention based on Host
- tag_ksz.c: converted to skb_mac_header(skb) from (is_link_local_ether_addr(hdr->h_dest))
- lan937x_dev.c: Removed BCAST Storm protection settings since we have Tc commands for them
- lan937x_dev.c: Flow control setting in lan937x_port_setup function
- lan937x_dev.c: RGMII internal delay added only for cpu port,
- lan937x_dev.c: of_get_compatible_child(node, "microchip,lan937x-mdio") to of_get_child_by_name(node, "mdio");
- lan937x_dev.c:lan937x_get_interface API: returned PHY_INTERFACE_MODE_INTERNAL instead of PHY_INTERFACE_MODE_NA
- lan937x_main.c: Removed compat interface implementation in lan937x_config_cpu_port() API & dev_info corrected as well
- lan937x_main.c: deleted ds->configure_vlan_while_not_filtering = true
- lan937x_main.c: Added explanation for lan937x_setup lines
- lan937x_main.c: FR_MAX_SIZE correction in lan937x_get_max_mtu API
- lan937x_main.c: removed lan937x_port_bridge_flags dummy functions
- lan937x_spi.c - mdiobus_unregister to be added to spi_remove function
- lan937x_main.c: phy link layer changes
- lan937x_main.c: port mirroring: sniff port selection limiting to one port
- lan937x_main.c: Changed to global vlan filtering
- lan937x_main.c: vlan_table array to structure
- lan937x_main.c -Use extack instead of reporting errors to Console
- lan937x_main.c - Remove cpu_port addition in vlan_add api
- lan937x_main.c - removed pvid resetting

Changes in v2:
- return check for register read/writes
- dt compatible compatible check is added against chip id value
- lan937x_internal_t1_tx_phy_write() is renamed to lan937x_internal_phy_write()
- lan937x_is_internal_tx_phy_port is renamed to lan937x_is_internal_100BTX_phy_port as it is 100Base-Tx phy
- Return value for lan937x_internal_phy_write() is -EOPNOTSUPP in case of failures
- Return value for lan937x_internal_phy_read() is 0xffff for non existent phy
- cpu_port checking is removed from lan937x_port_stp_state_set()
- lan937x_phy_link_validate: 100baseT_Full to 100baseT1_Full
- T1 Phy driver is moved to drivers/net/phy/microchip_t1.c
- Tx phy driver support will be added later
- Legacy switch checkings in dts file are removed.
- tag_ksz.c: Re-used ksz9477_rcv for lan937x_rcv
- tag_ksz.c: Xmit() & rcv() Comments are corrected w.r.to host
- net/dsa/Kconfig: Family skew numbers altered in ascending order
- microchip,lan937x.yaml: eth is replaced with ethernet
- microchip,lan937x.yaml: spi1 is replaced with spi
- microchip,lan937x.yaml: cpu labelling is removed
- microchip,lan937x.yaml: port@x value will match the reg value now

Prasanna Vengateshan (10):
dt-bindings: net: dsa: dt bindings for microchip lan937x
net: dsa: move mib->cnt_ptr reset code to ksz_common.c
net: phy: Add support for LAN937x T1 phy driver
net: dsa: tag_ksz: add tag handling for Microchip LAN937x
net: dsa: microchip: add DSA support for microchip lan937x
net: dsa: microchip: add support for phylink management
net: dsa: microchip: add support for ethtool port counters
net: dsa: microchip: add support for port mirror operations
net: dsa: microchip: add support for fdb and mdb management
net: dsa: microchip: add support for vlan operations

 .../bindings/net/dsa/microchip,lan937x.yaml   |  148 ++
 MAINTAINERS                                   |    1 +
 drivers/net/dsa/microchip/Kconfig             |   12 +
 drivers/net/dsa/microchip/Makefile            |    5 +
 drivers/net/dsa/microchip/ksz8795.c           |    2 -
 drivers/net/dsa/microchip/ksz9477.c           |    3 -
 drivers/net/dsa/microchip/ksz_common.c        |    8 +-
 drivers/net/dsa/microchip/ksz_common.h        |    5 +
 drivers/net/dsa/microchip/lan937x_dev.c       |  696 ++++++++++
 drivers/net/dsa/microchip/lan937x_dev.h       |   83 ++
 drivers/net/dsa/microchip/lan937x_main.c      | 1231 +++++++++++++++++
 drivers/net/dsa/microchip/lan937x_reg.h       |  677 +++++++++
 drivers/net/dsa/microchip/lan937x_spi.c       |  223 +++
 drivers/net/phy/microchip_t1.c                |  319 ++++-
 include/net/dsa.h                             |    2 +
 net/dsa/Kconfig                               |    4 +-
 net/dsa/tag_ksz.c                             |   56 +
 17 files changed, 3408 insertions(+), 67 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/net/dsa/microchip,lan937x.yaml
 create mode 100644 drivers/net/dsa/microchip/lan937x_dev.c
 create mode 100644 drivers/net/dsa/microchip/lan937x_dev.h
 create mode 100644 drivers/net/dsa/microchip/lan937x_main.c
 create mode 100644 drivers/net/dsa/microchip/lan937x_reg.h
 create mode 100644 drivers/net/dsa/microchip/lan937x_spi.c
Signature · 1 line
2.27.0
#1 lore raw

PATCH v3 1/10 dt-bindings: net: dsa: dt bindings for microchip lan937x

Documentation in .yaml format and updates to the MAINTAINERS Also 'make dt_binding_check' is passed

---
 .../bindings/net/dsa/microchip,lan937x.yaml   | 148 ++++++++++++++++++
 MAINTAINERS                                   |   1 +
 2 files changed, 149 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/dsa/microchip,lan937x.yaml
Documentation/devicetree/bindings/net/dsa/microchip,lan937x.yaml new file +148 -0
new file mode 100644
index 000000000000..69021117d595
@@ -0,0 +1,148 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/dsa/microchip,lan937x.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: LAN937x Ethernet Switch Series Tree Bindings
+
+maintainers:
+  - [email protected]
+
+allOf:
+  - $ref: dsa.yaml#
+
+properties:
+  compatible:
+    enum:
+      - microchip,lan9370
+      - microchip,lan9371
+      - microchip,lan9372
+      - microchip,lan9373
+      - microchip,lan9374
+
+  reg:
+    maxItems: 1
+
+  spi-max-frequency:
+    maximum: 50000000
+
+  reset-gpios:
+    description: Optional gpio specifier for a reset line
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    //Ethernet switch connected via spi to the host
+    ethernet {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      fixed-link {
+        speed = <1000>;
+        full-duplex;
+      };
+    };
+
+    spi {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      lan9374: switch@0 {
+        compatible = "microchip,lan9374";
+        reg = <0>;
+
+        spi-max-frequency = <44000000>;
+
+        ethernet-ports {
+          #address-cells = <1>;
+          #size-cells = <0>;
+          port@0 {
+            reg = <0>;
+            label = "lan1";
+            phy-mode = "internal";
+            phy-handle = <&t1phy0>;
+          };
+          port@1 {
+            reg = <1>;
+            label = "lan2";
+            phy-mode = "internal";
+            phy-handle = <&t1phy1>;
+          };
+          port@2 {
+            reg = <2>;
+            label = "lan4";
+            phy-mode = "internal";
+            phy-handle = <&t1phy2>;
+          };
+          port@3 {
+            reg = <3>;
+            label = "lan6";
+            phy-mode = "internal";
+            phy-handle = <&t1phy3>;
+          };
+          port@4 {
+            reg = <4>;
+            phy-mode = "rgmii";
+            ethernet = <&ethernet>;
+            fixed-link {
+              speed = <1000>;
+              full-duplex;
+            };
+          };
+          port@5 {
+            reg = <5>;
+            label = "lan7";
+            phy-mode = "rgmii";
+            fixed-link {
+              speed = <1000>;
+              full-duplex;
+            };
+          };
+          port@6 {
+            reg = <6>;
+            label = "lan5";
+            phy-mode = "internal";
+            phy-handle = <&t1phy4>;
+          };
+          port@7 {
+            reg = <7>;
+            label = "lan3";
+            phy-mode = "internal";
+            phy-handle = <&t1phy5>;
+          };
+        };
+
+        mdio {
+          #address-cells = <1>;
+          #size-cells = <0>;
+
+          t1phy0: ethernet-phy@0{
+            reg = <0x0>;
+          };
+          t1phy1: ethernet-phy@1{
+            reg = <0x1>;
+          };
+          t1phy2: ethernet-phy@2{
+            reg = <0x2>;
+          };
+          t1phy3: ethernet-phy@3{
+            reg = <0x3>;
+          };
+          t1phy4: ethernet-phy@6{
+            reg = <0x6>;
+          };
+          t1phy5: ethernet-phy@7{
+            reg = <0x7>;
+          };
+        };
+      };
+    };
MAINTAINERS +1 -0
index da478d5c8b0c..f0ce2378b681 100644
@@ -12181,6 +12181,7 @@ M:	[email protected]
 L:	[email protected]
 S:	Maintained
 F:	Documentation/devicetree/bindings/net/dsa/microchip,ksz.yaml
+F:	Documentation/devicetree/bindings/net/dsa/microchip,lan937x.yaml
 F:	drivers/net/dsa/microchip/*
 F:	include/linux/platform_data/microchip-ksz.h
 F:	net/dsa/tag_ksz.c
Signature · 1 line
2.27.0
maintainer Reviewed-by on v3 1/10
#2 lore raw

Re: [PATCH v3 net-next 01/10] dt-bindings: net: dsa: dt bindings for microchip lan937x

Quoted 10 lines · Prasanna Vengateshan

Documentation in .yaml format and updates to the MAINTAINERS Also 'make dt_binding_check' is passed

---
 .../bindings/net/dsa/microchip,lan937x.yaml   | 148 ++++++++++++++++++
 MAINTAINERS                                   |   1 +
 2 files changed, 149 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/dsa/microchip,lan937x.yaml
#3 lore raw

PATCH v3 2/10 net: dsa: move mib->cnt_ptr reset code to ksz_common.c

In reply to Prasanna Vengateshan

mib->cnt_ptr resetting is handled in multiple places as part of port_init_cnt(). Hence moved mib->cnt_ptr code to ksz common layer and removed from individual product files.

---
 drivers/net/dsa/microchip/ksz8795.c    | 2 --
 drivers/net/dsa/microchip/ksz9477.c    | 3 ---
 drivers/net/dsa/microchip/ksz_common.c | 8 +++++++-
 3 files changed, 7 insertions(+), 6 deletions(-)
drivers/net/dsa/microchip/ksz8795.c +0 -2
index 560f6843bb65..52cf2843dbeb 100644
@@ -447,8 +447,6 @@ static void ksz8_port_init_cnt(struct ksz_device *dev, int port)
 					dropped, &mib->counters[mib->cnt_ptr]);
 		++mib->cnt_ptr;
 	}
-	mib->cnt_ptr = 0;
-	memset(mib->counters, 0, dev->mib_cnt * sizeof(u64));
 }
 
 static void ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data)
drivers/net/dsa/microchip/ksz9477.c +0 -3
index 854e25f43fa7..35b430d531de 100644
@@ -289,9 +289,6 @@ static void ksz9477_port_init_cnt(struct ksz_device *dev, int port)
 	ksz_write8(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FLUSH);
 	ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, 0);
 	mutex_unlock(&mib->cnt_mutex);
-
-	mib->cnt_ptr = 0;
-	memset(mib->counters, 0, dev->mib_cnt * sizeof(u64));
 }
 
 static enum dsa_tag_protocol ksz9477_get_tag_protocol(struct dsa_switch *ds,
drivers/net/dsa/microchip/ksz_common.c +7 -1
index 1542bfb8b5e5..2b27d7917903 100644
@@ -103,8 +103,14 @@ void ksz_init_mib_timer(struct ksz_device *dev)
 
 	INIT_DELAYED_WORK(&dev->mib_read, ksz_mib_read_work);
 
-	for (i = 0; i < dev->port_cnt; i++)
+	for (i = 0; i < dev->port_cnt; i++) {
+		struct ksz_port_mib *mib = &dev->ports[i].mib;
+
 		dev->dev_ops->port_init_cnt(dev, i);
+
+		mib->cnt_ptr = 0;
+		memset(mib->counters, 0, dev->mib_cnt * sizeof(u64));
+	}
 }
 EXPORT_SYMBOL_GPL(ksz_init_mib_timer);
 
Signature · 1 line
2.27.0
#5 lore raw

PATCH v3 3/10 net: phy: Add support for LAN937x T1 phy driver

In reply to Prasanna Vengateshan

Added support for Microchip LAN937x T1 phy driver. The sequence of initialization is used commonly for both LAN87xx and LAN937x drivers. The new initialization sequence is an improvement to existing LAN87xx and it is shared with LAN937x.

Also relevant comments are added in the existing code and existing soft-reset customized code has been replaced with genphy_soft_reset().

access_ereg_clr_poll_timeout() API is introduced for polling phy bank write and this is linked with PHYACC_ATTR_MODE_POLL.

Finally introduced function table for LAN937X_T1_PHY_ID along with microchip_t1_phy_driver struct.

---
 drivers/net/phy/microchip_t1.c | 319 +++++++++++++++++++++++++++------
 1 file changed, 260 insertions(+), 59 deletions(-)
drivers/net/phy/microchip_t1.c +260 -59
index 4dc00bd5a8d2..a3f1b5d123ce 100644
@@ -30,15 +30,53 @@
 #define	PHYACC_ATTR_MODE_READ		0
 #define	PHYACC_ATTR_MODE_WRITE		1
 #define	PHYACC_ATTR_MODE_MODIFY		2
+#define	PHYACC_ATTR_MODE_POLL		3
 
 #define	PHYACC_ATTR_BANK_SMI		0
 #define	PHYACC_ATTR_BANK_MISC		1
 #define	PHYACC_ATTR_BANK_PCS		2
 #define	PHYACC_ATTR_BANK_AFE		3
+#define	PHYACC_ATTR_BANK_DSP		4
 #define	PHYACC_ATTR_BANK_MAX		7
 
+#define T1_M_CTRL_REG			0x09
+#define T1_M_CFG			BIT(11)
+
+#define T1_MODE_STAT_REG		0x11
+#define T1_DSCR_LOCK_STATUS_MSK		BIT(3)
+#define T1_LINK_UP_MSK			BIT(0)
+
+#define T1_REG_BANK_SEL_MASK		0x7
+#define T1_REG_BANK_SEL			8
+#define T1_REG_ADDR_MASK		0xFF
+
+#define T1_M_STATUS_REG			0x0A
+#define T1_LOCAL_RX_OK			BIT(13)
+#define T1_REMOTE_RX_OK			BIT(12)
+
+#define LAN87XX_PHY_ID			0x0007c150
+#define LAN937X_T1_PHY_ID		0x0007c181
+#define LAN87XX_PHY_ID_MASK		0xfffffff0
+#define LAN937X_PHY_ID_MASK		0xfffffff0
+
+/* T1 Registers */
+#define T1_AFE_PORT_CFG1_REG		0x0B
+#define T1_POWER_DOWN_CONTROL_REG	0x1A
+#define T1_SLV_FD_MULT_CFG_REG		0x18
+#define T1_CDR_CFG_PRE_LOCK_REG		0x05
+#define T1_CDR_CFG_POST_LOCK_REG	0x06
+#define T1_LCK_STG2_MUFACT_CFG_REG	0x1A
+#define T1_LCK_STG3_MUFACT_CFG_REG	0x1B
+#define T1_POST_LCK_MUFACT_CFG_REG	0x1C
+#define T1_TX_RX_FIFO_CFG_REG		0x02
+#define T1_TX_LPF_FIR_CFG_REG		0x55
+#define T1_SQI_CONFIG_REG		0x2E
+#define T1_MDIO_CONTROL2_REG		0x10
+#define T1_INTERRUPT_SOURCE_REG		0x18
+#define T1_INTERRUPT2_SOURCE_REG	0x08
+
 #define DRIVER_AUTHOR	"Nisar Sayed <[email protected]>"
-#define DRIVER_DESC	"Microchip LAN87XX T1 PHY driver"
+#define DRIVER_DESC	"Microchip LAN87XX/LAN937X T1 PHY driver"
 
 struct access_ereg_val {
 	u8  mode;
@@ -51,12 +89,16 @@ struct access_ereg_val {
 static int access_ereg(struct phy_device *phydev, u8 mode, u8 bank,
 		       u8 offset, u16 val)
 {
+	u8 prev_bank;
 	u16 ereg = 0;
 	int rc = 0;
+	u16 t;
 
+	/* return if mode and bank are invalid */
 	if (mode > PHYACC_ATTR_MODE_WRITE || bank > PHYACC_ATTR_BANK_MAX)
 		return -EINVAL;
 
+	/* if the bank is SMI, then call phy_read() & phy_write() directly */
 	if (bank == PHYACC_ATTR_BANK_SMI) {
 		if (mode == PHYACC_ATTR_MODE_WRITE)
 			rc = phy_write(phydev, offset, val);
@@ -66,16 +108,43 @@ static int access_ereg(struct phy_device *phydev, u8 mode, u8 bank,
 	}
 
 	if (mode == PHYACC_ATTR_MODE_WRITE) {
+		/* Initialize to Write Mode */
 		ereg = LAN87XX_EXT_REG_CTL_WR_CTL;
+
+		/* Write the data to be written in to the Bank */
 		rc = phy_write(phydev, LAN87XX_EXT_REG_WR_DATA, val);
 		if (rc < 0)
 			return rc;
 	} else {
+		/* Initialize to Read Mode */
 		ereg = LAN87XX_EXT_REG_CTL_RD_CTL;
 	}
 
 	ereg |= (bank << 8) | offset;
 
+	/* DSP bank access workaround for lan937x*/
+	if (phydev->phy_id == LAN937X_T1_PHY_ID) {
+		/* Read previous selected bank */
+		rc = phy_read(phydev, LAN87XX_EXT_REG_CTL);
+		if (rc < 0)
+			return rc;
+
+		/* Store the prev_bank */
+		prev_bank = (rc >> T1_REG_BANK_SEL) & T1_REG_BANK_SEL_MASK;
+
+		if (bank != prev_bank && bank == PHYACC_ATTR_BANK_DSP) {
+			t = ereg & ~T1_REG_ADDR_MASK;
+
+			t &= ~LAN87XX_EXT_REG_CTL_WR_CTL;
+			t |= LAN87XX_EXT_REG_CTL_RD_CTL;
+
+			/*access twice for DSP bank change,dummy access*/
+			rc = phy_write(phydev, LAN87XX_EXT_REG_CTL, t);
+			if (rc < 0)
+				return rc;
+		}
+	}
+
 	rc = phy_write(phydev, LAN87XX_EXT_REG_CTL, ereg);
 	if (rc < 0)
 		return rc;
@@ -104,63 +173,152 @@ static int access_ereg_modify_changed(struct phy_device *phydev,
 	return rc;
 }
 
+static int access_ereg_clr_poll_timeout(struct phy_device *phydev, u8 bank,
+					u8 offset, u16 mask, u16 clr)
+{
+	int val;
+
+	if (bank != PHYACC_ATTR_BANK_SMI)
+		return -EINVAL;
+
+	return phy_read_poll_timeout(phydev, offset, val, (val & mask) == clr,
+				     150, 30000, true);
+}
+
 static int lan87xx_phy_init(struct phy_device *phydev)
 {
 	static const struct access_ereg_val init[] = {
-		/* TX Amplitude = 5 */
-		{PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_AFE, 0x0B,
-		 0x000A, 0x001E},
-		/* Clear SMI interrupts */
-		{PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_SMI, 0x18,
-		 0, 0},
-		/* Clear MISC interrupts */
-		{PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_MISC, 0x08,
-		 0, 0},
-		/* Turn on TC10 Ring Oscillator (ROSC) */
-		{PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_MISC, 0x20,
-		 0x0020, 0x0020},
-		/* WUR Detect Length to 1.2uS, LPC Detect Length to 1.09uS */
-		{PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_PCS, 0x20,
-		 0x283C, 0},
-		/* Wake_In Debounce Length to 39uS, Wake_Out Length to 79uS */
-		{PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x21,
-		 0x274F, 0},
-		/* Enable Auto Wake Forward to Wake_Out, ROSC on, Sleep,
-		 * and Wake_In to wake PHY
-		 */
-		{PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x20,
-		 0x80A7, 0},
-		/* Enable WUP Auto Fwd, Enable Wake on MDI, Wakeup Debouncer
-		 * to 128 uS
-		 */
-		{PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x24,
-		 0xF110, 0},
-		/* Enable HW Init */
-		{PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_SMI, 0x1A,
-		 0x0100, 0x0100},
+		/* TXPD/TXAMP6 Configs*/
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_AFE,
+		  T1_AFE_PORT_CFG1_REG,       0x002D,  0 },
+		/* HW_Init Hi and Force_ED */
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_SMI,
+		  T1_POWER_DOWN_CONTROL_REG,  0x0308,  0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_SLV_FD_MULT_CFG_REG,     0x0D53,  0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_CDR_CFG_PRE_LOCK_REG,    0x0AB2,  0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_CDR_CFG_POST_LOCK_REG,   0x0AB3,  0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_LCK_STG2_MUFACT_CFG_REG, 0x0AEA,  0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_LCK_STG3_MUFACT_CFG_REG, 0x0AEB,  0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_POST_LCK_MUFACT_CFG_REG, 0x0AEB,  0 },
+		/* Pointer delay */
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_RX_FIFO_CFG_REG, 0x1C00, 0 },
+		/* Tx iir edits */
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1000, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1861, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1061, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1922, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1122, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1983, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1183, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1944, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1144, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x18c5, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x10c5, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1846, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1046, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1807, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1007, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1808, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1008, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1809, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1009, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x180A, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x100A, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x180B, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x100B, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x180C, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x100C, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x180D, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x100D, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x180E, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x100E, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x180F, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x100F, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1810, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1010, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1811, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1011, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_TX_LPF_FIR_CFG_REG, 0x1000, 0 },
+		/* SQI enable */
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_DSP,
+		  T1_SQI_CONFIG_REG,		0x9572, 0 },
+		/* Flag LPS and WUR as idle errors */
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_SMI,
+		  T1_MDIO_CONTROL2_REG,		0x0014, 0 },
+		/* Restore state machines without clearing registers */
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_SMI,
+		  T1_POWER_DOWN_CONTROL_REG,	0x0200, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_SMI,
+		  T1_MDIO_CONTROL2_REG,		0x0094, 0 },
+		{ PHYACC_ATTR_MODE_POLL, PHYACC_ATTR_BANK_SMI,
+		  T1_MDIO_CONTROL2_REG,		0x0080, 0 },
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_AFE,
+		  T1_AFE_PORT_CFG1_REG,		0x000C, 0 },
+		/* Read INTERRUPT_SOURCE Register */
+		{ PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_SMI,
+		  T1_INTERRUPT_SOURCE_REG,	0,	0 },
+		/* Read INTERRUPT_SOURCE Register */
+		{ PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_MISC,
+		  T1_INTERRUPT2_SOURCE_REG,	0,	0 },
+		/* HW_Init Hi */
+		{ PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_SMI,
+		  T1_POWER_DOWN_CONTROL_REG,	0x0300, 0 },
 	};
 	int rc, i;
 
-	/* Start manual initialization procedures in Managed Mode */
-	rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_SMI,
-					0x1a, 0x0000, 0x0100);
-	if (rc < 0)
-		return rc;
-
-	/* Soft Reset the SMI block */
+	/* Set Master Mode */
 	rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_SMI,
-					0x00, 0x8000, 0x8000);
+					T1_M_CTRL_REG, T1_M_CFG, T1_M_CFG);
 	if (rc < 0)
 		return rc;
 
-	/* Check to see if the self-clearing bit is cleared */
-	usleep_range(1000, 2000);
-	rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
-			 PHYACC_ATTR_BANK_SMI, 0x00, 0);
+	/* phy Soft reset */
+	rc = genphy_soft_reset(phydev);
 	if (rc < 0)
 		return rc;
-	if ((rc & 0x8000) != 0)
-		return -ETIMEDOUT;
 
 	/* PHY Initialization */
 	for (i = 0; i < ARRAY_SIZE(init); i++) {
@@ -169,6 +327,11 @@ static int lan87xx_phy_init(struct phy_device *phydev)
 							init[i].offset,
 							init[i].val,
 							init[i].mask);
+		} else if (init[i].mode == PHYACC_ATTR_MODE_POLL) {
+			rc = access_ereg_clr_poll_timeout(phydev, init[i].bank,
+							  init[i].offset,
+							  init[i].val,
+							  init[i].mask);
 		} else {
 			rc = access_ereg(phydev, init[i].mode, init[i].bank,
 					 init[i].offset, init[i].val);
@@ -223,32 +386,70 @@ static int lan87xx_config_init(struct phy_device *phydev)
 {
 	int rc = lan87xx_phy_init(phydev);
 
+	if (rc < 0)
+		phydev_err(phydev, "failed to initialize phy\n");
+
 	return rc < 0 ? rc : 0;
 }
 
-static struct phy_driver microchip_t1_phy_driver[] = {
-	{
-		.phy_id         = 0x0007c150,
-		.phy_id_mask    = 0xfffffff0,
-		.name           = "Microchip LAN87xx T1",
+static int lan937x_read_status(struct phy_device *phydev)
+{
+	int val;
 
-		.features       = PHY_BASIC_T1_FEATURES,
+	val = phy_read(phydev, T1_MODE_STAT_REG);
 
-		.config_init	= lan87xx_config_init,
+	if (val < 0)
+		return val;
 
-		.config_intr    = lan87xx_phy_config_intr,
-		.handle_interrupt = lan87xx_handle_interrupt,
+	if (val & T1_LINK_UP_MSK)
+		phydev->link = 1;
+	else
+		phydev->link = 0;
 
-		.suspend        = genphy_suspend,
-		.resume         = genphy_resume,
+	phydev->duplex = DUPLEX_FULL;
+	phydev->speed = SPEED_100;
+	phydev->pause = 0;
+	phydev->asym_pause = 0;
+
+	return 0;
+}
+
+static int lan937x_config_init(struct phy_device *phydev)
+{
+	/* lan87xx & lan937x follows same init sequence */
+	return lan87xx_config_init(phydev);
+}
+
+static struct phy_driver microchip_t1_phy_driver[] = {
+	{
+		.phy_id = LAN87XX_PHY_ID,
+		.phy_id_mask = LAN87XX_PHY_ID_MASK,
+		.name = "LAN87xx T1",
+		.features = PHY_BASIC_T1_FEATURES,
+		.config_init = lan87xx_config_init,
+		.config_intr = lan87xx_phy_config_intr,
+		.handle_interrupt = lan87xx_handle_interrupt,
+		.suspend = genphy_suspend,
+		.resume = genphy_resume,
+	},
+	{
+		.phy_id = LAN937X_T1_PHY_ID,
+		.phy_id_mask = LAN937X_PHY_ID_MASK,
+		.name = "LAN937x T1",
+		.read_status = lan937x_read_status,
+		.features = PHY_BASIC_T1_FEATURES,
+		.config_init = lan937x_config_init,
+		.suspend = genphy_suspend,
+		.resume = genphy_resume,
 	}
 };
 
 module_phy_driver(microchip_t1_phy_driver);
 
 static struct mdio_device_id __maybe_unused microchip_t1_tbl[] = {
-	{ 0x0007c150, 0xfffffff0 },
-	{ }
+	{ LAN87XX_PHY_ID, LAN87XX_PHY_ID_MASK },
+	{ LAN937X_T1_PHY_ID, LAN937X_PHY_ID_MASK },
+	{}
 };
 
 MODULE_DEVICE_TABLE(mdio, microchip_t1_tbl);
Signature · 1 line
2.27.0
#6 lore raw

Re: [PATCH v3 net-next 03/10] net: phy: Add support for LAN937x T1 phy driver

Andrew,

Quoted 29 lines · Prasanna Vengateshan

Added support for Microchip LAN937x T1 phy driver. The sequence of initialization is used commonly for both LAN87xx and LAN937x drivers. The new initialization sequence is an improvement to existing LAN87xx and it is shared with LAN937x.

Also relevant comments are added in the existing code and existing soft-reset customized code has been replaced with genphy_soft_reset().

access_ereg_clr_poll_timeout() API is introduced for polling phy bank write and this is linked with PHYACC_ATTR_MODE_POLL.

Finally introduced function table for LAN937X_T1_PHY_ID along with microchip_t1_phy_driver struct.

---
 drivers/net/phy/microchip_t1.c | 319 +++++++++++++++++++++++++++------

 1 file changed, 260 insertions(+), 59 deletions(-)
drivers/net/phy/microchip_t1.c +1 -0
index 4dc00bd5a8d2..a3f1b5d123ce 100644
@@ -30,15 +30,53 @@
  #define        PHYACC_ATTR_MODE_READ           0
  #define        PHYACC_ATTR_MODE_WRITE          1
  #define        PHYACC_ATTR_MODE_MODIFY         2
+#define        PHYACC_ATTR_MODE_POLL           3
Quoted 7 lines


 #define        PHYACC_ATTR_BANK_SMI            0
 #define        PHYACC_ATTR_BANK_MISC           1
 #define        PHYACC_ATTR_BANK_PCS            2
 #define        PHYACC_ATTR_BANK_AFE            3
+#define        PHYACC_ATTR_BANK_DSP            4
 #define        PHYACC_ATTR_BANK_MAX            7

Are there any items that need a change in this patch? It will be helpful for me to include them in the next version. Thanks.

Prasanna V

#7 lore raw

PATCH v3 4/10 net: dsa: tag_ksz: add tag handling for Microchip LAN937x

In reply to Prasanna Vengateshan

The Microchip LAN937X switches have a tagging protocol which is very similar to KSZ tagging. So that the implementation is added to tag_ksz.c and reused common APIs

---
 include/net/dsa.h |  2 ++
 net/dsa/Kconfig   |  4 ++--
 net/dsa/tag_ksz.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 2 deletions(-)
include/net/dsa.h +2 -0
index 9e5593885357..3c4e1a2e49be 100644
@@ -51,6 +51,7 @@ struct phylink_link_state;
 #define DSA_TAG_PROTO_SEVILLE_VALUE		21
 #define DSA_TAG_PROTO_BRCM_LEGACY_VALUE		22
 #define DSA_TAG_PROTO_SJA1110_VALUE		23
+#define DSA_TAG_PROTO_LAN937X_VALUE		24
 
 enum dsa_tag_protocol {
 	DSA_TAG_PROTO_NONE		= DSA_TAG_PROTO_NONE_VALUE,
@@ -77,6 +78,7 @@ enum dsa_tag_protocol {
 	DSA_TAG_PROTO_OCELOT_8021Q	= DSA_TAG_PROTO_OCELOT_8021Q_VALUE,
 	DSA_TAG_PROTO_SEVILLE		= DSA_TAG_PROTO_SEVILLE_VALUE,
 	DSA_TAG_PROTO_SJA1110		= DSA_TAG_PROTO_SJA1110_VALUE,
+	DSA_TAG_PROTO_LAN937X		= DSA_TAG_PROTO_LAN937X_VALUE,
 };
 
 struct packet_type;
net/dsa/Kconfig +2 -2
index bca1b5d66df2..f728e60e0bd3 100644
@@ -87,10 +87,10 @@ config NET_DSA_TAG_MTK
 	  Mediatek switches.
 
 config NET_DSA_TAG_KSZ
-	tristate "Tag driver for Microchip 8795/9477/9893 families of switches"
+	tristate "Tag driver for Microchip 8795/937x/9477/9893 families of switches"
 	help
 	  Say Y if you want to enable support for tagging frames for the
-	  Microchip 8795/9477/9893 families of switches.
+	  Microchip 8795/937x/9477/9893 families of switches.
 
 config NET_DSA_TAG_RTL4_A
 	tristate "Tag driver for Realtek 4 byte protocol A tags"
net/dsa/tag_ksz.c +56 -0

66 lines not on this page · raw message

Signature · 1 line
2.27.0
maintainer
#8 lore raw

Re: [PATCH v3 net-next 04/10] net: dsa: tag_ksz: add tag handling for Microchip LAN937x

Quoted 24 lines · Prasanna Vengateshan
net/dsa/tag_ksz.c +18 -0
@@ -187,10 +187,66 @@ static const struct dsa_device_ops ksz9893_netdev_ops = {
 DSA_TAG_DRIVER(ksz9893_netdev_ops);
 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_KSZ9893);
 
+/* For xmit, 2 bytes are added before FCS.
+ * ---------------------------------------------------------------------------
+ * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
+ * ---------------------------------------------------------------------------
+ * tag0 : represents tag override, lookup and valid
+ * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x80=port8)
+ *
+ * For rcv, 1 byte is added before FCS.
+ * ---------------------------------------------------------------------------
+ * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
+ * ---------------------------------------------------------------------------
+ * tag0 : zero-based value represents port
+ *	  (eg, 0x00=port1, 0x02=port3, 0x07=port8)
+ */
+#define LAN937X_EGRESS_TAG_LEN		2
+
+#define LAN937X_TAIL_TAG_BLOCKING_OVERRIDE	BIT(11)
+#define LAN937X_TAIL_TAG_LOOKUP			BIT(12)

net/dsa/tag_ksz.c static const struct dsa_device_ops ksz9893_netdev_ops = { · line 217

Quoted 10 lines
net/dsa/tag_ksz.c +10 -0
+#define LAN937X_TAIL_TAG_VALID			BIT(13)
+#define LAN937X_TAIL_TAG_PORT_MASK		7
+
+static struct sk_buff *lan937x_xmit(struct sk_buff *skb,
+				    struct net_device *dev)
+{
+	struct dsa_port *dp = dsa_slave_to_port(dev);
+	const struct ethhdr *hdr = eth_hdr(skb);
+	__be16 *tag;
+	u16 val;

There was a recent patch on net.git to fix an issue with DSA master drivers which set NETIF_F_HW_CSUM in dev->vlan_features, which DSA inherits.
https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/commit/?id=37120f23ac8998c250573ea3247ff77426551f69
Until we fix the issue at the DSA layer, could you also apply that fix here please?

net/dsa/tag_ksz.c static const struct dsa_device_ops ksz9893_netdev_ops = { · line 227

Quoted 10 lines
net/dsa/tag_ksz.c +10 -0
+
+	tag = skb_put(skb, LAN937X_EGRESS_TAG_LEN);
+
+	val = BIT(dp->index);
+
+	if (is_link_local_ether_addr(hdr->h_dest))
+		val |= LAN937X_TAIL_TAG_BLOCKING_OVERRIDE;
+
+	/* Tail tag valid bit - This bit should always be set by the CPU*/
+	val |= LAN937X_TAIL_TAG_VALID;

Please add an extra space here between "CPU" and the comment ending delimiter.

net/dsa/tag_ksz.c static const struct dsa_device_ops ksz9893_netdev_ops = { · line 229

Quoted 2 lines
net/dsa/tag_ksz.c +2 -0
+
+	*tag = cpu_to_be16(val);

This probably works for the arch you are testing on, but it is better to avoid unaligned accesses when the skb->len is odd:
https://www.kernel.org/doc/Documentation/unaligned-memory-access.txt

You can use put_unaligned_be16().

Quoted 3 lines
net/dsa/tag_ksz.c +3 -0
+
+	return skb;
+}
#9 lore raw

PATCH v3 5/10 net: dsa: microchip: add DSA support for microchip lan937x

In reply to Prasanna Vengateshan

Basic DSA driver support for lan937x and the device will be configured through SPI interface.

drivers/net/dsa/microchip/ path is already part of MAINTAINERS & the new files come under this path. Hence no update needed to the MAINTAINERS

Reused KSZ APIs for port_bridge_join() & port_bridge_leave() and added support for port_stp_state_set() & port_fast_age().

lan937x_flush_dyn_mac_table() which gets called from port_fast_age() of KSZ common layer, hence added support for it.

---
 drivers/net/dsa/microchip/Kconfig        |  12 +
 drivers/net/dsa/microchip/Makefile       |   5 +
 drivers/net/dsa/microchip/ksz_common.h   |   5 +
 drivers/net/dsa/microchip/lan937x_dev.c  | 650 ++++++++++++++++++++++
 drivers/net/dsa/microchip/lan937x_dev.h  |  80 +++
 drivers/net/dsa/microchip/lan937x_main.c | 317 +++++++++++
 drivers/net/dsa/microchip/lan937x_reg.h  | 677 +++++++++++++++++++++++
 drivers/net/dsa/microchip/lan937x_spi.c  | 223 ++++++++
 8 files changed, 1969 insertions(+)
 create mode 100644 drivers/net/dsa/microchip/lan937x_dev.c
 create mode 100644 drivers/net/dsa/microchip/lan937x_dev.h
 create mode 100644 drivers/net/dsa/microchip/lan937x_main.c
 create mode 100644 drivers/net/dsa/microchip/lan937x_reg.h
 create mode 100644 drivers/net/dsa/microchip/lan937x_spi.c
drivers/net/dsa/microchip/Kconfig +12 -0
index c9e2a8989556..f329cca934ee 100644
@@ -3,6 +3,18 @@ config NET_DSA_MICROCHIP_KSZ_COMMON
 	select NET_DSA_TAG_KSZ
 	tristate
 
+config NET_DSA_MICROCHIP_LAN937X
+	tristate "Microchip LAN937X series SPI connected switch support"
+	depends on NET_DSA && SPI
+	select NET_DSA_MICROCHIP_KSZ_COMMON
+	select REGMAP_SPI
+	help
+	  This driver adds support for Microchip LAN937X series
+	  switch chips.
+
+	  Select to enable support for registering switches configured
+	  through SPI.
+
 menuconfig NET_DSA_MICROCHIP_KSZ9477
 	tristate "Microchip KSZ9477 series switch support"
 	depends on NET_DSA
drivers/net/dsa/microchip/Makefile +5 -0
index 2a03b21a3386..28d8eb62a795 100644
@@ -6,3 +6,8 @@ obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_SPI)	+= ksz9477_spi.o
 obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ8795)		+= ksz8795.o
 obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ8795_SPI)	+= ksz8795_spi.o
 obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ8863_SMI)	+= ksz8863_smi.o
+
+obj-$(CONFIG_NET_DSA_MICROCHIP_LAN937X)		+= lan937x.o
+lan937x-objs := lan937x_dev.o
+lan937x-objs += lan937x_main.o
+lan937x-objs += lan937x_spi.o
drivers/net/dsa/microchip/ksz_common.h +5 -0
index 2e6bfd333f50..690d339edd7b 100644
@@ -60,6 +60,8 @@ struct ksz_device {
 
 	struct gpio_desc *reset_gpio;	/* Optional reset GPIO */
 
+	struct device_node *mdio_np;
+
 	/* chip specific data */
 	u32 chip_id;
 	int num_vlans;
@@ -145,6 +147,9 @@ void ksz_switch_remove(struct ksz_device *dev);
 
 int ksz8_switch_register(struct ksz_device *dev);
 int ksz9477_switch_register(struct ksz_device *dev);
+int lan937x_switch_register(struct ksz_device *dev);
+
+int lan937x_check_device_id(struct ksz_device *dev);
 
 void ksz_update_port_member(struct ksz_device *dev, int port);
 void ksz_init_mib_timer(struct ksz_device *dev);
drivers/net/dsa/microchip/lan937x_dev.c new file +650 -0

650 lines not on this page · raw message

drivers/net/dsa/microchip/lan937x_dev.h new file +80 -0

80 lines not on this page · raw message

drivers/net/dsa/microchip/lan937x_main.c new file +317 -0

317 lines not on this page · raw message

drivers/net/dsa/microchip/lan937x_reg.h new file +677 -0

677 lines not on this page · raw message

drivers/net/dsa/microchip/lan937x_spi.c new file +223 -0

223 lines not on this page · raw message

Signature · 1 line
2.27.0
maintainer
#10 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

drivers/net/dsa/microchip/ksz_common.h struct ksz_device { · line 64

Quoted 10 lines · Prasanna Vengateshan
drivers/net/dsa/microchip/ksz_common.h +2 -0
index 2e6bfd333f50..690d339edd7b 100644
@@ -60,6 +60,8 @@ struct ksz_device {
 
 	struct gpio_desc *reset_gpio;	/* Optional reset GPIO */
 
+	struct device_node *mdio_np;
+

I don't think you need to hold a reference to the MDIO bus DT node across the lifetime of the driver, at least other drivers don't, they drop the reference to it as soon as they finish with of_mdiobus_register and I don't think they have seen any adverse effects.

drivers/net/dsa/microchip/ksz_common.h struct ksz_device {

Quoted 4 lines
drivers/net/dsa/microchip/ksz_common.h +4 -0
+int lan937x_cfg32(struct ksz_device *dev, u32 addr, u32 bits, bool set)
+{
+	return regmap_update_bits(dev->regmap[2], addr, bits, set ? bits : 0);
+}

This function is unused.

drivers/net/dsa/microchip/ksz_common.h

Quoted 6 lines
drivers/net/dsa/microchip/ksz_common.h +6 -0
+int lan937x_port_cfg32(struct ksz_device *dev, int port, int offset, u32 bits,
+		       bool set)
+{
+	return regmap_update_bits(dev->regmap[2], PORT_CTRL_ADDR(port, offset),
+				  bits, set ? bits : 0);
+}

Likewise.

drivers/net/dsa/microchip/ksz_common.h

Quoted 8 lines
drivers/net/dsa/microchip/ksz_common.h +8 -0
+static void lan937x_r_mib_cnt(struct ksz_device *dev, int port, u16 addr,
+			      u64 *cnt)
+{
+	unsigned int val;
+	u32 data;
+	int ret;
+
+	/* Enable MIB Counter read*/

You can try to be more careful with the style of the comments, ensure that there is a space between the last character and the */ marker, here and everywhere.

Quoted 10 lines
drivers/net/dsa/microchip/ksz_common.h +10 -0
+	data = MIB_COUNTER_READ;
+	data |= (addr << MIB_COUNTER_INDEX_S);
+	lan937x_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT, data);
+
+	ret = regmap_read_poll_timeout(dev->regmap[2],
+				       PORT_CTRL_ADDR(port, REG_PORT_MIB_CTRL_STAT),
+				       val, !(val & MIB_COUNTER_READ),
+				       10, 1000);
+	if (ret) {
+		dev_err(dev->dev, "Failed to get MIB\n");

drivers/net/dsa/microchip/ksz_common.h

Quoted 10 lines
drivers/net/dsa/microchip/ksz_common.h +9 -0
+		return;
+	}
+
+	/* count resets upon read */
+	lan937x_pread32(dev, port, REG_PORT_MIB_DATA, &data);
+	*cnt += data;
+}
 
+
+bool lan937x_is_internal_100BTX_phy_port(struct ksz_device *dev, int port)

In another similar driver (sja1110), instead of adding camel case names, I went for "base_tx" and "base_t1". Maybe that looks slightly better and more uniform between your "100BTX" vs "t1".

Quoted 19 lines
drivers/net/dsa/microchip/ksz_common.h +19 -0
+void lan937x_mac_config(struct ksz_device *dev, int port,
+			phy_interface_t interface)
+{
+	u8 data8;
+
+	lan937x_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
+
+	/* clear MII selection & set it based on interface later */
+	data8 &= ~PORT_MII_SEL_M;
+
+	/* configure MAC based on interface */
+	switch (interface) {
+	case PHY_INTERFACE_MODE_MII:
+		lan937x_config_gbit(dev, false, &data8);
+		data8 |= PORT_MII_SEL;
+		break;
+	case PHY_INTERFACE_MODE_RMII:
+		lan937x_config_gbit(dev, false, &data8);
+		data8 |= PORT_RMII_SEL;

drivers/net/dsa/microchip/ksz_common.h

Quoted 10 lines
drivers/net/dsa/microchip/ksz_common.h +10 -0
+		break;
+	case PHY_INTERFACE_MODE_RGMII:
+	case PHY_INTERFACE_MODE_RGMII_ID:
+	case PHY_INTERFACE_MODE_RGMII_TXID:
+	case PHY_INTERFACE_MODE_RGMII_RXID:
+		lan937x_config_gbit(dev, true, &data8);
+		data8 |= PORT_RGMII_SEL;
+
+		/* Add RGMII internal delay for cpu port*/
+		if (dsa_is_cpu_port(dev->ds, port)) {

Why only for the CPU port? I would like Andrew/Florian to have a look here, I guess the assumption is that if the port has a phy-handle, the RGMII delays should be dealt with by the PHY, but the logic seems to be "is a CPU port <=> has a phy-handle / isn't a CPU port <=> doesn't have a phy-handle"? What if it's a fixed-link port connected to a downstream switch, for which this one is a DSA master?

Quoted 15 lines
drivers/net/dsa/microchip/ksz_common.h +15 -0
+			if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
+			    interface == PHY_INTERFACE_MODE_RGMII_RXID)
+				data8 |= PORT_RGMII_ID_IG_ENABLE;
+
+			if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
+			    interface == PHY_INTERFACE_MODE_RGMII_TXID)
+				data8 |= PORT_RGMII_ID_EG_ENABLE;
+		}
+		break;
+	default:
+		dev_err(dev->dev, "Unsupported interface '%s' for port %d\n",
+			phy_modes(interface), port);
+		return;
+	}
+

drivers/net/dsa/microchip/ksz_common.h

Quoted 10 lines
drivers/net/dsa/microchip/ksz_common.h +9 -0
+	/* Write the updated value */
+	lan937x_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, data8);
+}
 
+static int lan937x_mdio_register(struct dsa_switch *ds)
+{
+	struct ksz_device *dev = ds->priv;
+	int ret;
+
+	dev->mdio_np = of_get_child_by_name(ds->dev->of_node, "mdio");

So you support both the cases where an internal PHY is described using OF bindings, and where the internal PHY is implicitly accessed using the slave_mii_bus of the switch, at a PHY address equal to the port number, and with no phy-handle or fixed-link device tree property for that port?

Do you need both alternatives? The first is already more flexible than the second.

Quoted 19 lines
drivers/net/dsa/microchip/ksz_common.h +19 -0
+static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port,
+				       u8 state)
+{
+	struct ksz_device *dev = ds->priv;
+	struct ksz_port *p = &dev->ports[port];
+	int forward = dev->member;
+	int member = -1;
+	u8 data;
+
+	lan937x_pread8(dev, port, P_STP_CTRL, &data);
+	data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
+
+	switch (state) {
+	case BR_STATE_DISABLED:
+		data |= PORT_LEARN_DISABLE;
+		break;
+	case BR_STATE_LISTENING:
+		data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE);
+		if (p->stp_state == BR_STATE_DISABLED)

drivers/net/dsa/microchip/ksz_common.h

Quoted 10 lines
drivers/net/dsa/microchip/ksz_common.h +10 -0
+			member = dev->host_mask | p->vid_member;
+		break;
+	case BR_STATE_LEARNING:
+		data |= PORT_RX_ENABLE;
+		break;
+	case BR_STATE_FORWARDING:
+		data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
+
+		member = dev->host_mask | p->vid_member;
+		mutex_lock(&dev->dev_mutex);

I am not convinced that the dev_mutex brings any value in any of the ksz drivers that use it.

Quoted 41 lines
drivers/net/dsa/microchip/ksz_common.h +41 -0
+
+		/* Port is a member of a bridge. */
+		if (dev->br_member & (1 << port)) {
+			dev->member |= (1 << port);
+			member = dev->member;
+		}
+		mutex_unlock(&dev->dev_mutex);
+		break;
+	case BR_STATE_BLOCKING:
+		data |= PORT_LEARN_DISABLE;
+		if (p->stp_state == BR_STATE_DISABLED)
+			member = dev->host_mask | p->vid_member;
+		break;
+	default:
+		dev_err(ds->dev, "invalid STP state: %d\n", state);
+		return;
+	}
+
+	lan937x_pwrite8(dev, port, P_STP_CTRL, data);
+
+	p->stp_state = state;
+	mutex_lock(&dev->dev_mutex);
+
+	/* Port membership may share register with STP state. */
+	if (member >= 0 && member != p->member)
+		lan937x_cfg_port_member(dev, port, (u8)member);
+
+	/* Check if forwarding needs to be updated. */
+	if (state != BR_STATE_FORWARDING) {
+		if (dev->br_member & (1 << port))
+			dev->member &= ~(1 << port);
+	}
+
+	/* When topology has changed the function ksz_update_port_member
+	 * should be called to modify port forwarding behavior.
+	 */
+	if (forward != dev->member)
+		ksz_update_port_member(dev, port);
+
+	mutex_unlock(&dev->dev_mutex);
+}
maintainer
#11 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 28 lines
Quoted 28 lines
drivers/net/dsa/microchip/lan937x_dev.c new file +28 -0
+void lan937x_mac_config(struct ksz_device *dev, int port,
+			phy_interface_t interface)
+{
+	u8 data8;
+
+	lan937x_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
+
+	/* clear MII selection & set it based on interface later */
+	data8 &= ~PORT_MII_SEL_M;
+
+	/* configure MAC based on interface */
+	switch (interface) {
+	case PHY_INTERFACE_MODE_MII:
+		lan937x_config_gbit(dev, false, &data8);
+		data8 |= PORT_MII_SEL;
+		break;
+	case PHY_INTERFACE_MODE_RMII:
+		lan937x_config_gbit(dev, false, &data8);
+		data8 |= PORT_RMII_SEL;
+		break;
+	case PHY_INTERFACE_MODE_RGMII:
+	case PHY_INTERFACE_MODE_RGMII_ID:
+	case PHY_INTERFACE_MODE_RGMII_TXID:
+	case PHY_INTERFACE_MODE_RGMII_RXID:
+		lan937x_config_gbit(dev, true, &data8);
+		data8 |= PORT_RGMII_SEL;
+
+		/* Add RGMII internal delay for cpu port*/
Quoted 8 lines

drivers/net/dsa/microchip/lan937x_dev.c line 460

Quoted 1 line
drivers/net/dsa/microchip/lan937x_dev.c new file +1 -0
+		if (dsa_is_cpu_port(dev->ds, port)) {

Why only for the CPU port? I would like Andrew/Florian to have a look here, I guess the assumption is that if the port has a phy-handle, the RGMII delays should be dealt with by the PHY, but the logic seems to be "is a CPU port <=> has a phy-handle / isn't a CPU port <=> doesn't have a phy-handle"? What if it's a fixed-link port connected to a downstream switch, for which this one is a DSA master?

The marvell driver applies delays unconditionally. And as far as i remember, it is only used in the use case you suggest, a DSA link, which is using RGMII. For marvell switches, that is pretty unusual, most boards use 1000BaseX or higher SERDES speeds for links between switches.

I'm not sure if we have the case of an external PHY using RGMII. I suspect it might actually be broken, because i think both the MAC and the PHY might add the same delay. For phylib in general, if the MAC applies the delays, it needs to manipulate the value passed to the PHY so it also does not add delays. And i'm not sure DSA does that.

So limiting RGMII delays to only the CPU port is not unreasonable. However, i suspect you are correct about chained switches not working.

We might need to look at this at a higher level, when the PHY is connected to the MAC and what mode gets passed to it.

Quoted 31 lines
Quoted 15 lines
drivers/net/dsa/microchip/lan937x_dev.c new file +15 -0
+			if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
+			    interface == PHY_INTERFACE_MODE_RGMII_RXID)
+				data8 |= PORT_RGMII_ID_IG_ENABLE;
+
+			if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
+			    interface == PHY_INTERFACE_MODE_RGMII_TXID)
+				data8 |= PORT_RGMII_ID_EG_ENABLE;
+		}
+		break;
+	default:
+		dev_err(dev->dev, "Unsupported interface '%s' for port %d\n",
+			phy_modes(interface), port);
+		return;
+	}
+

drivers/net/dsa/microchip/lan937x_dev.c line 544

Quoted 10 lines
drivers/net/dsa/microchip/lan937x_dev.c new file +9 -0
+	/* Write the updated value */
+	lan937x_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, data8);
+}
 
+static int lan937x_mdio_register(struct dsa_switch *ds)
+{
+	struct ksz_device *dev = ds->priv;
+	int ret;
+
+	dev->mdio_np = of_get_child_by_name(ds->dev->of_node, "mdio");

So you support both the cases where an internal PHY is described using OF bindings, and where the internal PHY is implicitly accessed using the slave_mii_bus of the switch, at a PHY address equal to the port number, and with no phy-handle or fixed-link device tree property for that port?

Quoted 2 lines

Do you need both alternatives? The first is already more flexible than the second.

The first is also much more verbose in DT, and the second generally just works without any DT. What can be tricky with the second is getting PHY interrupts to work, but it is possible, the mv88e6xxx does it.

Andrew

maintainer
#12 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 5 lines · Andrew Lunn
Quoted 5 lines

So you support both the cases where an internal PHY is described using OF bindings, and where the internal PHY is implicitly accessed using the slave_mii_bus of the switch, at a PHY address equal to the port number, and with no phy-handle or fixed-link device tree property for that port?

Quoted 7 lines
Quoted 2 lines

Do you need both alternatives? The first is already more flexible than the second.

The first is also much more verbose in DT, and the second generally just works without any DT. What can be tricky with the second is getting PHY interrupts to work, but it is possible, the mv88e6xxx does it.

- The explicit phy-handle is more verbose as far as the DT description goes for one particular use case of indirect internal PHY access, but it also leads to less complex code (more uniform with other usage patterns in the kernel). What is tricky with an implicit phy-handle is trivial without it. This makes a difference with DM_DSA in U-Boot, where I would really like to avoid bloating the code and just support 2 options for a DSA switch port: either a phy-handle or a fixed-link. These two are already "Turing-complete" (they can describe any system) so I only see the implicit phy-handle as a helping hand for a few lazy DT writers. Since I have been pushing back that we shouldn't bloat U-Boot with implicit phy-handle logic when it doesn't give a concrete benefit, and have gotten a push back in return that Linux does allow it and it would be desirable for one DT binding to cover all, I now need to promote the more generic approach for Linux going forward too.

- If the switch has the ability for its internal PHYs to be accessed directly over MDIO pins instead of using indirect SPI transfers, using a phy-handle is a generic way to handle both cases, while the implicit phy-handle does not give you that option.

- If there is complex pinmuxing in the SoC and one port can either be connected to an internal 100base-T1 or to a 100base-TX PHY, and this is not detectable by software, this cannot be dealt with using an implicit phy-handle if the 100base-T1 and 100base-TX PHYs are not at the same address.

- In general, if the internal PHYs are not at an MDIO address equal to the port number, this cannot be dealt with using the implicit phy-handle method.

maintainer
#13 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 40 lines · Vladimir Oltean
Quoted 5 lines · Andrew Lunn
Quoted 5 lines

So you support both the cases where an internal PHY is described using OF bindings, and where the internal PHY is implicitly accessed using the slave_mii_bus of the switch, at a PHY address equal to the port number, and with no phy-handle or fixed-link device tree property for that port?

Quoted 7 lines
Quoted 2 lines

Do you need both alternatives? The first is already more flexible than the second.

The first is also much more verbose in DT, and the second generally just works without any DT. What can be tricky with the second is getting PHY interrupts to work, but it is possible, the mv88e6xxx does it.

- The explicit phy-handle is more verbose as far as the DT description goes for one particular use case of indirect internal PHY access, but it also leads to less complex code (more uniform with other usage patterns in the kernel). What is tricky with an implicit phy-handle is trivial without it. This makes a difference with DM_DSA in U-Boot, where I would really like to avoid bloating the code and just support 2 options for a DSA switch port: either a phy-handle or a fixed-link. These two are already "Turing-complete" (they can describe any system) so I only see the implicit phy-handle as a helping hand for a few lazy DT writers. Since I have been pushing back that we shouldn't bloat U-Boot with implicit phy-handle logic when it doesn't give a concrete benefit, and have gotten a push back in return that Linux does allow it and it would be desirable for one DT binding to cover all, I now need to promote the more generic approach for Linux going forward too.

- If the switch has the ability for its internal PHYs to be accessed directly over MDIO pins instead of using indirect SPI transfers, using a phy-handle is a generic way to handle both cases, while the implicit phy-handle does not give you that option.

- If there is complex pinmuxing in the SoC and one port can either be connected to an internal 100base-T1 or to a 100base-TX PHY, and this is not detectable by software, this cannot be dealt with using an implicit phy-handle if the 100base-T1 and 100base-TX PHYs are not at the same address.

Quoted 3 lines

- In general, if the internal PHYs are not at an MDIO address equal to the port number, this cannot be dealt with using the implicit phy-handle method.

There are good reasons to use an explicit phy-handle, and i would never block such code. However, implicit is historically how it was done. There are many DT blobs which assume it works. So implicit is not going away.

If you want to only support explicit in U-Boot, that is fine. I would suggest making this clear in the U-Boot documentation.

Andrew

maintainer
#14 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 7 lines · Andrew Lunn

There are good reasons to use an explicit phy-handle, and i would never block such code. However, implicit is historically how it was done. There are many DT blobs which assume it works. So implicit is not going away.

If you want to only support explicit in U-Boot, that is fine. I would suggest making this clear in the U-Boot documentation.

I am happy that Prasanna made it possible for OF-based descriptions of the internal PHYs to be written for the lan937x generation. I did take a look at the bindings that Prasanna proposed and I think they would work with what DM_DSA can parse too. The work that Tim Harvey did was for ksz9897, and it is slightly different: the MDIO controller node has a compatible string of "microchip,ksz-mdio", and a parent container node of "mdios".
https://source.denx.de/u-boot/u-boot/-/blob/master/arch/arm/dts/imx8mm-venice-gw7901.dts#L634
However, since the lan937x would probably have a different driver even in U-Boot, 100% binding consistency between lan937x and ksz9897 is probably not necessary, since some of that can boil down to driver author choice too. As long as an OF based choice is available I'm absolutely fine.

#15 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

In reply to Vladimir Oltean
Quoted 31 lines · Vladimir Oltean

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 29 lines
drivers/net/dsa/microchip/lan937x_dev.c new file +28 -0
+void lan937x_mac_config(struct ksz_device *dev, int port,
+                     phy_interface_t interface)
+{
+     u8 data8;
+
+     lan937x_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
+
+     /* clear MII selection & set it based on interface later */
+     data8 &= ~PORT_MII_SEL_M;
+
+     /* configure MAC based on interface */
+     switch (interface) {
+     case PHY_INTERFACE_MODE_MII:
+             lan937x_config_gbit(dev, false, &data8);
+             data8 |= PORT_MII_SEL;
+             break;
+     case PHY_INTERFACE_MODE_RMII:
+             lan937x_config_gbit(dev, false, &data8);
+             data8 |= PORT_RMII_SEL;
+             break;
+     case PHY_INTERFACE_MODE_RGMII:
+     case PHY_INTERFACE_MODE_RGMII_ID:
+     case PHY_INTERFACE_MODE_RGMII_TXID:
+     case PHY_INTERFACE_MODE_RGMII_RXID:
+             lan937x_config_gbit(dev, true, &data8);
+             data8 |= PORT_RGMII_SEL;
+
+             /* Add RGMII internal delay for cpu port*/
Quoted 9 lines
Quoted 1 line

+             if (dsa_is_cpu_port(dev->ds, port)) {

Why only for the CPU port? I would like Andrew/Florian to have a look here, I guess the assumption is that if the port has a phy-handle, the RGMII delays should be dealt with by the PHY, but the logic seems to be "is a CPU port <=> has a phy-handle / isn't a CPU port <=> doesn't have a phy-handle"? What if it's a fixed-link port connected to a downstream switch, for which this one is a DSA master?

Quoted 1 line

Thanks for reviewing the patches. My earlier proposal here was to check if there is no phydev (dp->slave->phydev) or if PHY is genphy, then apply RGMII delays assuming delays should be dealt with the phy driver if available. What do you think of that?

maintainer
#16 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 38 lines · Prasanna Vengateshan
Quoted 28 lines · Vladimir Oltean
Quoted 28 lines
drivers/net/dsa/microchip/lan937x_dev.c new file +28 -0
+void lan937x_mac_config(struct ksz_device *dev, int port,
+                     phy_interface_t interface)
+{
+     u8 data8;
+
+     lan937x_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
+
+     /* clear MII selection & set it based on interface later */
+     data8 &= ~PORT_MII_SEL_M;
+
+     /* configure MAC based on interface */
+     switch (interface) {
+     case PHY_INTERFACE_MODE_MII:
+             lan937x_config_gbit(dev, false, &data8);
+             data8 |= PORT_MII_SEL;
+             break;
+     case PHY_INTERFACE_MODE_RMII:
+             lan937x_config_gbit(dev, false, &data8);
+             data8 |= PORT_RMII_SEL;
+             break;
+     case PHY_INTERFACE_MODE_RGMII:
+     case PHY_INTERFACE_MODE_RGMII_ID:
+     case PHY_INTERFACE_MODE_RGMII_TXID:
+     case PHY_INTERFACE_MODE_RGMII_RXID:
+             lan937x_config_gbit(dev, true, &data8);
+             data8 |= PORT_RGMII_SEL;
+
+             /* Add RGMII internal delay for cpu port*/
Quoted 8 lines
Quoted 1 line

+             if (dsa_is_cpu_port(dev->ds, port)) {

Why only for the CPU port? I would like Andrew/Florian to have a look here, I guess the assumption is that if the port has a phy-handle, the RGMII delays should be dealt with by the PHY, but the logic seems to be "is a CPU port <=> has a phy-handle / isn't a CPU port <=> doesn't have a phy-handle"? What if it's a fixed-link port connected to a downstream switch, for which this one is a DSA master?

Quoted 4 lines

Thanks for reviewing the patches. My earlier proposal here was to check if there is no phydev (dp->slave->phydev) or if PHY is genphy, then apply RGMII delays assuming delays should be dealt with the phy driver if available. What do you think of that?

I don't know what to suggest, this is a bit of a minefield.
A while ago and in a different thread, Russell King said that PHY_INTERFACE_MODE_RGMII_TXID means that the MAC should behave as if it is connected to a PHY which has applied RGMII delays in the TX direction, regardless of whether it is in fixed-link or not. So if the MAC was to add any internal delays in PHY_INTERFACE_MODE_RGMII_TXID mode, those would have to be RX delays, because the phy-mode specifies what was already added and nothing more.
However, that is yet another problem. "what is already added" does not mean "what more needs to be added". The fact that internal delays were added in the TX direction doesn't necessarily mean that they still need to be added in the RX direction. If you have a PHY which can only delay the clock that it drives (the RX clock), and this is connected to a MAC that cannot add any delays of its own, then you would end up adding PCB traces on the board in the TX direction. But if you specify the phy-mode as "rgmii-rxid", the MAC driver would complain that it can't add delays in the TX direction (under the assumption that these are needed to work), and if you specify the phy-mode as "rgmii-id", the PHY driver would complain that it can't add delays in the TX direction.
So you'd have a system that works from a hardware PoV, but you wouldn't have any way to describe it to Linux, which sucks.

I think the only way to do things correctly today and have a way to describe any possible hardware setup is to parse the explicit rx-internal-delay-ps and tx-internal-delay-ps properties in the MAC driver, as defined in Documentation/bindings/net/ethernet-controller.yaml. Then only let the MAC add the internal delays if the device tree has added those properties, leave nothing down to assumptions.

maintainer
#17 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 39 lines · Vladimir Oltean
Quoted 38 lines · Prasanna Vengateshan
Quoted 36 lines · Vladimir Oltean
Quoted 19 lines
drivers/net/dsa/microchip/lan937x_dev.c new file +19 -0
+void lan937x_mac_config(struct ksz_device *dev, int port,
+                     phy_interface_t interface)
+{
+     u8 data8;
+
+     lan937x_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
+
+     /* clear MII selection & set it based on interface later */
+     data8 &= ~PORT_MII_SEL_M;
+
+     /* configure MAC based on interface */
+     switch (interface) {
+     case PHY_INTERFACE_MODE_MII:
+             lan937x_config_gbit(dev, false, &data8);
+             data8 |= PORT_MII_SEL;
+             break;
+     case PHY_INTERFACE_MODE_RMII:
+             lan937x_config_gbit(dev, false, &data8);
+             data8 |= PORT_RMII_SEL;

drivers/net/dsa/microchip/lan937x_dev.c line 460

Quoted 10 lines
drivers/net/dsa/microchip/lan937x_dev.c new file +10 -0
+             break;
+     case PHY_INTERFACE_MODE_RGMII:
+     case PHY_INTERFACE_MODE_RGMII_ID:
+     case PHY_INTERFACE_MODE_RGMII_TXID:
+     case PHY_INTERFACE_MODE_RGMII_RXID:
+             lan937x_config_gbit(dev, true, &data8);
+             data8 |= PORT_RGMII_SEL;
+
+             /* Add RGMII internal delay for cpu port*/
+             if (dsa_is_cpu_port(dev->ds, port)) {

Why only for the CPU port? I would like Andrew/Florian to have a look here, I guess the assumption is that if the port has a phy-handle, the RGMII delays should be dealt with by the PHY, but the logic seems to be "is a CPU port <=> has a phy-handle / isn't a CPU port <=> doesn't have a phy-handle"? What if it's a fixed-link port connected to a downstream switch, for which this one is a DSA master?

Quoted 6 lines
Quoted 4 lines

Thanks for reviewing the patches. My earlier proposal here was to check if there is no phydev (dp->slave->phydev) or if PHY is genphy, then apply RGMII delays assuming delays should be dealt with the phy driver if available. What do you think of that?

I don't know what to suggest, this is a bit of a minefield.

In general, the MAC does nothing, and passes the value to the PHY. The PHY inserts delays as requested. To address Vladimir point, PHY_INTERFACE_MODE_RGMII_TXID would mean the PHY adds delay in the TX direction, and assumes the RX delay comes from somewhere else, probably the PCB.

I only recommend the MAC adds delays when the PHY cannot, or there is no PHY, e.g. SoC to switch, or switch to switch link. There are a few MAC drivers that do add delays, mostly because that is how the vendor crap tree does it.

So as i said, what you propose is O.K, it follows this general rule of thumb.

Andrew

maintainer
#18 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 5 lines · Andrew Lunn

In general, the MAC does nothing, and passes the value to the PHY. The PHY inserts delays as requested. To address Vladimir point, PHY_INTERFACE_MODE_RGMII_TXID would mean the PHY adds delay in the TX direction, and assumes the RX delay comes from somewhere else, probably the PCB.

For the PHY, that is the only portion where things are clear.

Quoted 7 lines

I only recommend the MAC adds delays when the PHY cannot, or there is no PHY, e.g. SoC to switch, or switch to switch link. There are a few MAC drivers that do add delays, mostly because that is how the vendor crap tree does it.

So as i said, what you propose is O.K, it follows this general rule of thumb.

The "rule of thumb" for a MAC driver is actually applied in reverse by most MAC drivers compared to what Russell described should be happening. For example, mv88e6xxx_port_set_rgmii_delay():

	switch (mode) {
	case PHY_INTERFACE_MODE_RGMII_RXID:
		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK;

The mv88e6xxx is a MAC, so when it has a phy-mode = "rgmii-rxid", it should assume it is connected to a link partner (PHY or otherwise) that has applied the RXCLK delay already. So it should only be concerned with the TXCLK delay. That is my point. I am just trying to lay out the points to Prasanna that would make a sane system going forward. I am not sure that we actually have an in-tree driver that is sane in that regard.

That discussion, and Russell's point, was here, btw:
https://patchwork.ozlabs.org/project/netdev/patch/20200616074955.GA9092@laureti-dev/#2461123

maintainer
#19 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 25 lines · Vladimir Oltean
Quoted 5 lines · Andrew Lunn

In general, the MAC does nothing, and passes the value to the PHY. The PHY inserts delays as requested. To address Vladimir point, PHY_INTERFACE_MODE_RGMII_TXID would mean the PHY adds delay in the TX direction, and assumes the RX delay comes from somewhere else, probably the PCB.

For the PHY, that is the only portion where things are clear.

Quoted 7 lines

I only recommend the MAC adds delays when the PHY cannot, or there is no PHY, e.g. SoC to switch, or switch to switch link. There are a few MAC drivers that do add delays, mostly because that is how the vendor crap tree does it.

So as i said, what you propose is O.K, it follows this general rule of thumb.

The "rule of thumb" for a MAC driver is actually applied in reverse by most MAC drivers compared to what Russell described should be happening. For example, mv88e6xxx_port_set_rgmii_delay():

	switch (mode) {
	case PHY_INTERFACE_MODE_RGMII_RXID:
		reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK;
Quoted 7 lines

The mv88e6xxx is a MAC, so when it has a phy-mode = "rgmii-rxid", it should assume it is connected to a link partner (PHY or otherwise) that has applied the RXCLK delay already. So it should only be concerned with the TXCLK delay. That is my point. I am just trying to lay out the points to Prasanna that would make a sane system going forward. I am not sure that we actually have an in-tree driver that is sane in that regard.

It is a can or worms. For the used use case for the mv88e6xxx, it is a DSA link, so there isn't really one side MAC and the other side PHY. And if i remember correctly, both sides use rgmii-rxid.

Andrew

#20 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

In reply to Vladimir Oltean
Quoted 36 lines · Vladimir Oltean

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 5 lines · Andrew Lunn

In general, the MAC does nothing, and passes the value to the PHY. The PHY inserts delays as requested. To address Vladimir point, PHY_INTERFACE_MODE_RGMII_TXID would mean the PHY adds delay in the TX direction, and assumes the RX delay comes from somewhere else, probably the PCB.

For the PHY, that is the only portion where things are clear.

Quoted 7 lines

I only recommend the MAC adds delays when the PHY cannot, or there is no PHY, e.g. SoC to switch, or switch to switch link. There are a few MAC drivers that do add delays, mostly because that is how the vendor crap tree does it.

So as i said, what you propose is O.K, it follows this general rule of thumb.

The "rule of thumb" for a MAC driver is actually applied in reverse by most MAC drivers compared to what Russell described should be happening. For example, mv88e6xxx_port_set_rgmii_delay():

        switch (mode) {
        case PHY_INTERFACE_MODE_RGMII_RXID:
                reg |= MV88E6XXX_PORT_MAC_CTL_RGMII_DELAY_RXCLK;

The mv88e6xxx is a MAC, so when it has a phy-mode = "rgmii-rxid", it should assume it is connected to a link partner (PHY or otherwise) that has applied the RXCLK delay already. So it should only be concerned with the TXCLK delay. That is my point. I am just trying to lay out the points to Prasanna that would make a sane system going forward. I am not sure that we actually have an in-tree driver that is sane in that regard.

Quoted 2 lines

That discussion, and Russell's point, was here, btw:
https://patchwork.ozlabs.org/project/netdev/patch/20200616074955.GA9092@laureti-dev/#2461123

Thanks Vladimir & Andrew for the right pointers and info. The thread talks about "rgmii-*" are going to be applied by the PHY only as per the doc. For fixed- link, MAC needs to add the delay. This fixed-link can be No-PHY or MAC-MAC or MAC to in-accessible PHY. In such case, i am not convinced in using rgmii-tx- delay-ps & rgmii-rx-delay-ps on the MAC side and apply delay. I still think proposed code in earlier mail thread should still be okay.

maintainer
#21 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 6 lines · Prasanna Vengateshan

Thanks Vladimir & Andrew for the right pointers and info. The thread talks about "rgmii-*" are going to be applied by the PHY only as per the doc. For fixed- link, MAC needs to add the delay. This fixed-link can be No-PHY or MAC-MAC or MAC to in-accessible PHY. In such case, i am not convinced in using rgmii-tx- delay-ps & rgmii-rx-delay-ps on the MAC side and apply delay. I still think proposed code in earlier mail thread should still be okay.

Why? I genuinely do not understand your reasoning

- I read a thread that brings some arguments for which MACs should not add delays based on the delay type in the "rgmii-*" phy-mode string [ but based on explicit rgmii-tx-delay-ps and rgmii-rx-delay-ps properties under the MAC OF node; this is written in the same message as the quote that you chose ]

- I acknowledge that in certain configurations I need the MAC to apply internal delays.

=> I disagree that I should parse the rgmii-tx-delay-ps and
rgmii-rx-delay-ps OF properties of the MAC, just apply RGMII delays based on the "rgmii-*" phy-mode string value, when I am a DSA CPU port and in no other circumstance

?!

I mean, feel free to feel convinced or not, but you have not actually brought any argument to the table here, or I'm not seeing it.

Anyway, I don't believe that whatever you decide to do with the RGMII delays is likely to be a decisive factor in whether the patches are accepted or not, considering the fact that traditionally, everyone did what suited their board best and that's about it; I will stop pushing back.

I have a theory that all the RGMII setups driven by the Linux PHY library cannot all work at the same time, with the same code base. Someone will sooner or later come and change a driver to make it do what they need, which will break what the original author intended, which will then be again patched, which will again break ..., which ....

If a perpetual motion device will ever be built by mankind, I am sure it will be powered by RGMII delays.

#22 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 34 lines · Vladimir Oltean
Quoted 6 lines · Prasanna Vengateshan

Thanks Vladimir & Andrew for the right pointers and info. The thread talks about "rgmii-*" are going to be applied by the PHY only as per the doc. For fixed- link, MAC needs to add the delay. This fixed-link can be No-PHY or MAC-MAC or MAC to in-accessible PHY. In such case, i am not convinced in using rgmii-tx- delay-ps & rgmii-rx-delay-ps on the MAC side and apply delay. I still think proposed code in earlier mail thread should still be okay.

Why? I genuinely do not understand your reasoning

- I read a thread that brings some arguments for which MACs should not add delays based on the delay type in the "rgmii-*" phy-mode string [ but based on explicit rgmii-tx-delay-ps and rgmii-rx-delay-ps properties under the MAC OF node; this is written in the same message as the quote that you chose ]

- I acknowledge that in certain configurations I need the MAC to apply internal delays.

=> I disagree that I should parse the rgmii-tx-delay-ps and
rgmii-rx-delay-ps OF properties of the MAC, just apply RGMII delays based on the "rgmii-*" phy-mode string value, when I am a DSA CPU port and in no other circumstance

?!

I mean, feel free to feel convinced or not, but you have not actually brought any argument to the table here, or I'm not seeing it.

Anyway, I don't believe that whatever you decide to do with the RGMII delays is likely to be a decisive factor in whether the patches are accepted or not, considering the fact that traditionally, everyone did what suited their board best and that's about it; I will stop pushing back.

Quoted 5 lines

I have a theory that all the RGMII setups driven by the Linux PHY library cannot all work at the same time, with the same code base. Someone will sooner or later come and change a driver to make it do what they need, which will break what the original author intended, which will then be again patched, which will again break ..., which ....

This is why we need to have a clear definition of what the various RGMII interface types are, how and where they are applied, and make sure everyone sticks to that. We have this documented in Documentation/networking/phy.rst.

The RGMII interface modes _only_ determine how the PHY should be configured - they do not determine how the MAC should be configured (with /maybe/ the exception of PHY_INTERFACE_MODE_RGMII allowing the MAC to insert "default delays".)

In the case of a fixed link, there is no "PHY" as such, but the PHY interface mode describes the properties of the link from the MAC perspective, since it is specified in the MAC node. So, if we have e.g. PHY_INTERFACE_MODE_RGMII_TXID, then from the MAC perspective, we expect the device on the other end of the fixed link to be adding the transmit data line delay. Since we have a fixed link though, we have no way to communicate that to the other side - but the delays do need to be configured to conform with RGMII.

An interesting point here, however, is the mv88e6xxx DSA driver - it appears to set the RGMII delays for _all_ ports based on what is in the DT node for the port. So, even if you have a port operating in RGMII with an external PHY, specifying a phy-mode of rgmii-txid results in that being configured at the DSA end of the RGMII link. Andrew - we may need to look at that since it doesn't conform to what we have in the documentation...

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
maintainer
#23 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 4 lines · Russell King (Oracle)

This is why we need to have a clear definition of what the various RGMII interface types are, how and where they are applied, and make sure everyone sticks to that. We have this documented in Documentation/networking/phy.rst.

The problem is that I have no clear migration path for the drivers I maintain, like sja1105, and I suspect that others might be in the exact same situation.

Currently, if the sja1105 needs to add internal delays in a MAC-to-MAC (fixed-link) setup, it does that based on the phy-mode string. So "rgmii-id" + "fixed-link" means for sja1105 "add RX and TX RGMII internal delays", even though the documentation now says "the MAC should not add the RX or TX delays in this case".

There are 2 cases to think about, old driver with new DT blob and new driver with old DT blob. If breakage is involved, I am not actually very interested in doing the migration, because even though the interpretation of the phy-mode string is inconsistent between the phy-handle and fixed-link case (which was deliberate), at least it currently does all that I need it to.

I am not even clear what is the expected canonical behavior for a MAC driver. It parses rx-internal-delay-ps and tx-internal-delay-ps, and then what? It treats all "rgmii*" phy-mode strings identically? Or is it an error to have "rgmii-rxid" for phy-mode and non-zero rx-internal-delay-ps? If it is an error, should all MAC drivers check for it? And if it is an error, does it not make migration even more difficult (adding an rx-internal-delay-ps property to a MAC OF node which already uses "rgmii-id" would be preferable to also having to change the "rgmii-id" to "rgmii", because an old kernel might also need to work with that DT blob, and that will ignore the new rx-internal-delay-ps property).

Does qca8k_setup_of_rgmii_delay(), a very recent function, even do the right thing with rx-internal-delay-ps, or is it doing the exact opposite of the right thing (it applies rx-internal-delay-ps when in rgmii-id or rgmii-rxid mode).

#24 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 19 lines · Vladimir Oltean

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

The problem is that I have no clear migration path for the drivers I maintain, like sja1105, and I suspect that others might be in the exact same situation.

Currently, if the sja1105 needs to add internal delays in a MAC-to-MAC (fixed-link) setup, it does that based on the phy-mode string. So "rgmii-id" + "fixed-link" means for sja1105 "add RX and TX RGMII internal delays", even though the documentation now says "the MAC should not add the RX or TX delays in this case".

There are 2 cases to think about, old driver with new DT blob and new driver with old DT blob. If breakage is involved, I am not actually very interested in doing the migration, because even though the interpretation of the phy-mode string is inconsistent between the phy-handle and fixed-link case (which was deliberate), at least it currently does all that I need it to.

Quoted 10 lines

I am not even clear what is the expected canonical behavior for a MAC driver. It parses rx-internal-delay-ps and tx-internal-delay-ps, and then what? It treats all "rgmii*" phy-mode strings identically? Or is it an error to have "rgmii-rxid" for phy-mode and non-zero rx-internal-delay-ps? If it is an error, should all MAC drivers check for it? And if it is an error, does it not make migration even more difficult (adding an rx-internal-delay-ps property to a MAC OF node which already uses "rgmii-id" would be preferable to also having to change the "rgmii-id" to "rgmii", because an old kernel might also need to work with that DT blob, and that will ignore the new rx-internal-delay-ps property).

Considering the PHY is responsible to add internal delays w.r.to phy-mode, "*- tx-internal-delay-ps" approach that i was applying to different connections as shown below by bringing up different examples.

1) Fixed-link MAC-MAC:

       port@4 {
            .....
            phy-mode = "rgmii";
            rx-internal-delay-ps = <xxx>;
            tx-internal-delay-ps = <xxx>;
            ethernet = <&ethernet>;
            fixed-link {
           	......
            };
          };

2) Fixed-link MAC-Unknown:

        port@5 {
            ......
            phy-mode = "rgmii-id";
            rx-internal-delay-ps = <xxx>;
            tx-internal-delay-ps = <xxx>;
            fixed-link {
           .	....
            };
          };

3) Fixed-link :

        port@5 {
            ......
            phy-mode = "rgmii-id";
            fixed-link {
              .....
            };
          };

From above examples,
a) MAC node is responsible to add RGMII delay by parsing "*-internal-
delay-ps" for (1) & (2). Its a known item in this discussion.
b) Is rgmii-* to be ignored by the MAC in (2) and just apply the delays
from MAC side? Because if its forced to have "rgmii", would it become just -

Quoted 1 line

interface=*_MODE_RGMII and affects legacy?

c) if MAC follows standard delay, then it needs to be validated against
"*-internal-delay-ps", may be validating against single value and throw an error. Might be okay.
d) For 3), Neither MAC nor other side will apply delays. Expected.

3) MAC-PHY

	i) &test3 {
		phy-handle = <&phy0>;
		phy-mode = "rgmii-id";
		phy0: ethernet-phy@xx {
			.....
			rx-internal-delay = <xxx>;
			tx-internal-delay = <xxx>;
		};
	  };

	ii) &test4 {
		phy-handle = <&phy0>;
		phy-mode = "rgmii";
        	rx-internal-delay-ps = <xxx>;
        	tx-internal-delay-ps = <xxx>;
		phy0: ethernet-phy@xx {
			reg = <x>;
	        };
	     };

For 3(i), I assume phy would apply internal delay values by checking its phydev-

Quoted 1 line

interface.

For 3(ii), MAC would apply the delays.

Overall, only (b) need a right decision? or any other items are missed?

Prasanna V

maintainer
#25 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 65 lines · Prasanna Vengateshan
Quoted 16 lines · Vladimir Oltean

The problem is that I have no clear migration path for the drivers I maintain, like sja1105, and I suspect that others might be in the exact same situation.

Currently, if the sja1105 needs to add internal delays in a MAC-to-MAC (fixed-link) setup, it does that based on the phy-mode string. So "rgmii-id" + "fixed-link" means for sja1105 "add RX and TX RGMII internal delays", even though the documentation now says "the MAC should not add the RX or TX delays in this case".

There are 2 cases to think about, old driver with new DT blob and new driver with old DT blob. If breakage is involved, I am not actually very interested in doing the migration, because even though the interpretation of the phy-mode string is inconsistent between the phy-handle and fixed-link case (which was deliberate), at least it currently does all that I need it to.

Quoted 10 lines

I am not even clear what is the expected canonical behavior for a MAC driver. It parses rx-internal-delay-ps and tx-internal-delay-ps, and then what? It treats all "rgmii*" phy-mode strings identically? Or is it an error to have "rgmii-rxid" for phy-mode and non-zero rx-internal-delay-ps? If it is an error, should all MAC drivers check for it? And if it is an error, does it not make migration even more difficult (adding an rx-internal-delay-ps property to a MAC OF node which already uses "rgmii-id" would be preferable to also having to change the "rgmii-id" to "rgmii", because an old kernel might also need to work with that DT blob, and that will ignore the new rx-internal-delay-ps property).

Considering the PHY is responsible to add internal delays w.r.to phy-mode, "*- tx-internal-delay-ps" approach that i was applying to different connections as shown below by bringing up different examples.

1) Fixed-link MAC-MAC:

       port@4 {
            .....
            phy-mode = "rgmii";
            rx-internal-delay-ps = <xxx>;
            tx-internal-delay-ps = <xxx>;
            ethernet = <&ethernet>;
            fixed-link {
           	......
            };
          };

2) Fixed-link MAC-Unknown:

        port@5 {
            ......
            phy-mode = "rgmii-id";
            rx-internal-delay-ps = <xxx>;
            tx-internal-delay-ps = <xxx>;
            fixed-link {
           .	....
            };
          };

3) Fixed-link :

        port@5 {
            ......
            phy-mode = "rgmii-id";
            fixed-link {
              .....
            };
          };
Quoted 6 lines

From above examples,
a) MAC node is responsible to add RGMII delay by parsing "*-internal-
delay-ps" for (1) & (2). Its a known item in this discussion.
b) Is rgmii-* to be ignored by the MAC in (2) and just apply the delays
from MAC side? Because if its forced to have "rgmii", would it become just -

Quoted 1 line

interface=*_MODE_RGMII and affects legacy?

Yes, I think the MAC would have to accept any "rgmii*" phy-mode in fixed-link. The legacy behavior would be do to whatever it did before, and the new behavior would be to NOT apply any MAC-level delays based on the phy-mode value, but only based on the {rx,tx}-internal-delay-ps properties if these are present, or fall back to the legacy behavior if they aren't.

This way:
- New kernel with old DT blob falls back to legacy behavior
- New kernel with new DT blob finds the explicit {rx,tx}-internal-delay-ps properties and applies MAC-level delays only according to those, while accepting any phy-mode string
- Old kernel with new DT blob behaves the same as before, because it does not parse {rx,tx}-internal-delay-ps and we will not change its phy-mode.

Quoted 3 lines

c) if MAC follows standard delay, then it needs to be validated against
"*-internal-delay-ps", may be validating against single value and throw an error. Might be okay.

Drivers with no legacy might throw an error if:
- phy-mode == "rgmii-id" or "rgmii-rxid" and there is a non-zero rx-internal-delay-ps
- phy-mode == "rgmii-id" or "rgmii-txid" and there is a non-zero tx-internal-delay-ps

but considering that most drivers already have a legacy to support, I'm not sure how useful that error will be.

Quoted 1 line

d) For 3), Neither MAC nor other side will apply delays. Expected.

In the "new" behavior, correct. In "legacy" behavior, they might have to.

Quoted 23 lines

3) MAC-PHY

	i) &test3 {
		phy-handle = <&phy0>;
		phy-mode = "rgmii-id";
		phy0: ethernet-phy@xx {
			.....
			rx-internal-delay = <xxx>;
			tx-internal-delay = <xxx>;
		};
	  };

	ii) &test4 {
		phy-handle = <&phy0>;
		phy-mode = "rgmii";
        	rx-internal-delay-ps = <xxx>;
        	tx-internal-delay-ps = <xxx>;
		phy0: ethernet-phy@xx {
			reg = <x>;
	        };
	     };
Quoted 2 lines

For 3(i), I assume phy would apply internal delay values by checking its phydev-

Quoted 1 line

interface.

PHY drivers have a phy_get_internal_delay() helper that takes into consideration both the phy-mode value and the {rx,tx}-internal-delay properties. In example 3(i), the {rx,tx}-internal-delay properties would prevail as long as the PHY driver uses that helper.

Quoted 7 lines

For 3(ii), MAC would apply the delays.

Overall, only (b) need a right decision? or any other items are missed?

Prasanna V

maintainer
#26 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

In reply to Vladimir Oltean
Quoted 3 lines

I am not even clear what is the expected canonical behavior for a MAC driver. It parses rx-internal-delay-ps and tx-internal-delay-ps, and then what?

So best practices are based around a MAC-PHY link. phy-mode is passed to the PHY, and the MAC does not act upon it. MAC rx-internal-delay-ps and tx-internal-delay-ps can be used to fine tune the link. You can use them to add and sometimes subtract small amounts of delay.

Quoted 3 lines

It treats all "rgmii*" phy-mode strings identically? Or is it an error to have "rgmii-rxid" for phy-mode and non-zero rx-internal-delay-ps?

I would say the first is correct, the second statement is false. You should always be able to fine tune the link, independent of the PHY mode.

We also have to consider the case when the PHY is not actually able to implement the delay. It hopefully returns -EOPNOTSUPP for anything other than "rgmii". You can then put the full 2ns delay into tx-internal-delay-ps nd rx-internal-delay-ps.

And lastly there is one MAC driver which mostly ignores these best practices because the vendor crap tree always did the delay in the MAC. It correctly masks the phy-mode, so the PHY does not add delays.

For MAC-MAC and fixed link best practices are very fuzzily defined. It is not something we have much of in the kernel. We might also want to narrow the discussion down to MACs within a switch. MACs within in NIC should probably follow the best practices for a MAC-PHY link, even if it is actually a switch on the other end.

I also agree with Russell that mv88e6xxx is probably broken for a MAC-PHY link. It is known to work for a Marvell DSA in MAC-MAC link, we have boards doing that.

It seems like a switch MAC should parse rx-internal-delay-ps and tx-internal-delay-ps and apply them independent of the phy-mode. That keeps it consistent with MAC-PHY. And if there is a PHY connected, pass the phy-mode on unmasked.

So that we don't break mv88e6xxx, for a CPU or DSA port, we probably should continue to locally implement the delay, with the assumption there is no PHY, it is a MAC-MAC link. We probably want to patch mv88e6xxx to do nothing for user ports.

I suspect it is a 50/50 roll of a dice what rx and tx actually mean. Is it from the perspective of the MAC or the PHY? Luckily, rgmii-rxid and rgmii-txid don't appear in DT very often.

Andrew

maintainer
#27 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 3 lines · Andrew Lunn

I suspect it is a 50/50 roll of a dice what rx and tx actually mean. Is it from the perspective of the MAC or the PHY? Luckily, rgmii-rxid and rgmii-txid don't appear in DT very often.

I checked an NXP board schematic which has both, and:

When you connect a MAC to a PHY using RGMII, the RXD[3:0] pins of the MAC connect to the RXD[3:0] pins of the PHY, and the TXD[3:0] goes to TXD[3:0]. So it is neither the perspective of the MAC nor of the PHY.

When you connect a MAC to a MAC using RGMII, the RXD[3:0] of one MAC goes to the TXD[3:0] of the other, and vice versa.

Nonetheless, a phy-mode of "rgmii-rxid" always means to the local MAC that "the RX delays have been dealt with" - either by PCB traces, or a remote MAC applying TX delay, or the PHY applying RX delay.

#28 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

In reply to Andrew Lunn
Quoted 16 lines · Andrew Lunn

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 3 lines

I am not even clear what is the expected canonical behavior for a MAC driver. It parses rx-internal-delay-ps and tx-internal-delay-ps, and then what?

So best practices are based around a MAC-PHY link. phy-mode is passed to the PHY, and the MAC does not act upon it. MAC rx-internal-delay-ps and tx-internal-delay-ps can be used to fine tune the link. You can use them to add and sometimes subtract small amounts of delay.

Quoted 3 lines

It treats all "rgmii*" phy-mode strings identically? Or is it an error to have "rgmii-rxid" for phy-mode and non-zero rx-internal-delay-ps?

Quoted 8 lines

I would say the first is correct, the second statement is false. You should always be able to fine tune the link, independent of the PHY mode.

We also have to consider the case when the PHY is not actually able to implement the delay. It hopefully returns -EOPNOTSUPP for anything other than "rgmii". You can then put the full 2ns delay into tx-internal-delay-ps nd rx-internal-delay-ps.

I hope that using "*-internal-delay-ps" for Mac would be the right option. Shall i include these changes as we discussed in next revision of the patch?

maintainer
#29 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 2 lines

I hope that using "*-internal-delay-ps" for Mac would be the right option. Shall i include these changes as we discussed in next revision of the patch?

Yes, that seems sensible. But please limit them to the CPU port. Maybe return -EINVAL for other ports.

Andrew

#30 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 5 lines · Andrew Lunn
Quoted 2 lines

I hope that using "*-internal-delay-ps" for Mac would be the right option. Shall i include these changes as we discussed in next revision of the patch?

Yes, that seems sensible. But please limit them to the CPU port. Maybe return -EINVAL for other ports.

Hmm. Don't we want ports that are "MAC like" to behave "MAC like" ? In other words, shouldn't a DSA port that can be connected to an external PHY should accept the same properties as a conventional Ethernet MAC e.g. in a SoC device?

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
maintainer
#31 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 11 lines · Russell King (Oracle)
Quoted 5 lines · Andrew Lunn
Quoted 2 lines

I hope that using "*-internal-delay-ps" for Mac would be the right option. Shall i include these changes as we discussed in next revision of the patch?

Yes, that seems sensible. But please limit them to the CPU port. Maybe return -EINVAL for other ports.

Hmm. Don't we want ports that are "MAC like" to behave "MAC like" ? In other words, shouldn't a DSA port that can be connected to an external PHY should accept the same properties as a conventional Ethernet MAC e.g. in a SoC device?

+1, I thought the whole purpose of the discussion was to stop singling out the CPU port as being special in any way w.r.t. RGMII delays.

maintainer
#32 lore raw

Re: [PATCH v3 net-next 05/10] net: dsa: microchip: add DSA support for microchip lan937x

Quoted 8 lines · Vladimir Oltean
Quoted 7 lines · Russell King (Oracle)
Quoted 5 lines · Andrew Lunn
Quoted 2 lines

I hope that using "*-internal-delay-ps" for Mac would be the right option. Shall i include these changes as we discussed in next revision of the patch?

Yes, that seems sensible. But please limit them to the CPU port. Maybe return -EINVAL for other ports.

Quoted 7 lines
Quoted 4 lines

Hmm. Don't we want ports that are "MAC like" to behave "MAC like" ? In other words, shouldn't a DSA port that can be connected to an external PHY should accept the same properties as a conventional Ethernet MAC e.g. in a SoC device?

+1, I thought the whole purpose of the discussion was to stop singling out the CPU port as being special in any way w.r.t. RGMII delays.

Yes, sorry.

What we don't want is it acting upon phy-mode.

Andrew

#33 lore raw

PATCH v3 6/10 net: dsa: microchip: add support for phylink management

In reply to Prasanna Vengateshan

Support for phylink_validate() and reused KSZ commmon API for phylink_mac_link_down() operation

lan937x_phylink_mac_config configures the interface using lan937x_mac_config and lan937x_phylink_mac_link_up configures the speed/duplex/flow control.

Currently SGMII & in-band neg are not supported & it will be added later.

---
 drivers/net/dsa/microchip/lan937x_dev.c  | 46 ++++++++++++
 drivers/net/dsa/microchip/lan937x_dev.h  |  3 +
 drivers/net/dsa/microchip/lan937x_main.c | 90 ++++++++++++++++++++++++
 3 files changed, 139 insertions(+)
drivers/net/dsa/microchip/lan937x_dev.c +46 -0
index 86e84a79f464..c36fdc4692ff 100644
@@ -477,6 +477,52 @@ void lan937x_mac_config(struct ksz_device *dev, int port,
 	lan937x_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, data8);
 }
 
+void lan937x_config_interface(struct ksz_device *dev, int port,
+			      int speed, int duplex,
+			      bool tx_pause, bool rx_pause)
+{
+	u8 xmii_ctrl0, xmii_ctrl1;
+
+	lan937x_pread8(dev, port, REG_PORT_XMII_CTRL_0, &xmii_ctrl0);
+	lan937x_pread8(dev, port, REG_PORT_XMII_CTRL_1, &xmii_ctrl1);
+
+	switch (speed) {
+	case SPEED_1000:
+		lan937x_config_gbit(dev, true, &xmii_ctrl1);
+		break;
+	case SPEED_100:
+		lan937x_config_gbit(dev, false, &xmii_ctrl1);
+		xmii_ctrl0 |= PORT_MAC_SPEED_100;
+		break;
+	case SPEED_10:
+		lan937x_config_gbit(dev, false, &xmii_ctrl1);
+		xmii_ctrl0 &= ~PORT_MAC_SPEED_100;
+		break;
+	default:
+		dev_err(dev->dev, "Unsupported speed on port %d: %d\n",
+			port, speed);
+		return;
+	}
+
+	if (duplex)
+		xmii_ctrl0 |= PORT_FULL_DUPLEX;
+	else
+		xmii_ctrl0 &= ~PORT_FULL_DUPLEX;
+
+	if (tx_pause)
+		xmii_ctrl0 |= PORT_TX_FLOW_CTRL;
+	else
+		xmii_ctrl1 &= ~PORT_TX_FLOW_CTRL;
+
+	if (rx_pause)
+		xmii_ctrl0 |= PORT_RX_FLOW_CTRL;
+	else
+		xmii_ctrl0 &= ~PORT_RX_FLOW_CTRL;
+
+	lan937x_pwrite8(dev, port, REG_PORT_XMII_CTRL_0, xmii_ctrl0);
+	lan937x_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, xmii_ctrl1);
+}
+
 void lan937x_port_setup(struct ksz_device *dev, int port, bool cpu_port)
 {
 	struct ksz_port *p = &dev->ports[port];
drivers/net/dsa/microchip/lan937x_dev.h +3 -0
index 76c00db05616..0ddd7aa87468 100644
@@ -36,6 +36,9 @@ void lan937x_cfg_port_member(struct ksz_device *dev, int port,
 			     u8 member);
 void lan937x_port_setup(struct ksz_device *dev, int port, bool cpu_port);
 int lan937x_enable_spi_indirect_access(struct ksz_device *dev);
+void lan937x_config_interface(struct ksz_device *dev, int port,
+			      int speed, int duplex,
+			      bool tx_pause, bool rx_pause);
 void lan937x_mac_config(struct ksz_device *dev, int port,
 			phy_interface_t interface);
 
drivers/net/dsa/microchip/lan937x_main.c +90 -0

102 lines not on this page · raw message

Signature · 1 line
2.27.0
maintainer
#34 lore raw

Re: [PATCH v3 net-next 06/10] net: dsa: microchip: add support for phylink management

drivers/net/dsa/microchip/lan937x_main.c static int lan937x_get_max_mtu(struct dsa_switch *ds, int port) · line 344

Quoted 12 lines · Prasanna Vengateshan
drivers/net/dsa/microchip/lan937x_main.c +12 -0
+static void lan937x_phylink_validate(struct dsa_switch *ds, int port,
+				     unsigned long *supported,
+				     struct phylink_link_state *state)
+{
+	struct ksz_device *dev = ds->priv;
+	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
+
+	/* Check for unsupported interfaces */
+	if (!phy_interface_mode_is_rgmii(state->interface) &&
+	    state->interface != PHY_INTERFACE_MODE_RMII &&
+	    state->interface != PHY_INTERFACE_MODE_MII &&
+	    state->interface != PHY_INTERFACE_MODE_INTERNAL) {

According to include/linux/phylink.h, when phylink passes state->interface == PHY_INTERFACE_MODE_NA, you are expected to return all supported link modes.

#35 lore raw

Re: [PATCH v3 net-next 06/10] net: dsa: microchip: add support for phylink management

Quoted 4 lines · Vladimir Oltean

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

On Fri, Jul 23, 2021 at 11:01:04PM +0530, Prasanna Vengateshan wrote:

Quoted 16 lines

drivers/net/dsa/microchip/lan937x_main.c static int lan937x_get_max_mtu(struct dsa_switch *ds, int port) · line 344

Quoted 12 lines
drivers/net/dsa/microchip/lan937x_main.c +12 -0
+static void lan937x_phylink_validate(struct dsa_switch *ds, int port,
+                                  unsigned long *supported,
+                                  struct phylink_link_state *state)
+{
+     struct ksz_device *dev = ds->priv;
+     __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
+
+     /* Check for unsupported interfaces */
+     if (!phy_interface_mode_is_rgmii(state->interface) &&
+         state->interface != PHY_INTERFACE_MODE_RMII &&
+         state->interface != PHY_INTERFACE_MODE_MII &&
+         state->interface != PHY_INTERFACE_MODE_INTERNAL) {

According to include/linux/phylink.h, when phylink passes state->interface == PHY_INTERFACE_MODE_NA, you are expected to return all supported link modes.

Okay, Noted. i think PHY_INTERFACE_MODE_NA check should be added in all of the 'if' (==) checks down to the above one and including the above one (!=).

#36 lore raw

PATCH v3 7/10 net: dsa: microchip: add support for ethtool port counters

In reply to Prasanna Vengateshan

Reused the KSZ common APIs for get_ethtool_stats() & get_sset_count() along with relevant lan937x hooks for KSZ common layer and added support for get_strings()

---
 drivers/net/dsa/microchip/lan937x_main.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/net/dsa/microchip/lan937x_main.c +18 -0
index 74b6c9563dc1..3380a4617725 100644
@@ -43,6 +43,21 @@ static int lan937x_phy_write16(struct dsa_switch *ds, int addr, int reg,
 	return lan937x_internal_phy_write(dev, addr, reg, val);
 }
 
+static void lan937x_get_strings(struct dsa_switch *ds, int port, u32 stringset,
+				uint8_t *buf)
+{
+	struct ksz_device *dev = ds->priv;
+	int i;
+
+	if (stringset != ETH_SS_STATS)
+		return;
+
+	for (i = 0; i < dev->mib_cnt; i++) {
+		memcpy(buf + i * ETH_GSTRING_LEN, lan937x_mib_names[i].string,
+		       ETH_GSTRING_LEN);
+	}
+}
+
 static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port,
 				       u8 state)
 {
@@ -384,6 +399,9 @@ const struct dsa_switch_ops lan937x_switch_ops = {
 	.phy_read = lan937x_phy_read16,
 	.phy_write = lan937x_phy_write16,
 	.port_enable = ksz_enable_port,
+	.get_strings = lan937x_get_strings,
+	.get_ethtool_stats = ksz_get_ethtool_stats,
+	.get_sset_count = ksz_sset_count,
 	.port_bridge_join = ksz_port_bridge_join,
 	.port_bridge_leave = ksz_port_bridge_leave,
 	.port_stp_state_set = lan937x_port_stp_state_set,
Signature · 1 line
2.27.0
#37 lore raw

PATCH v3 8/10 net: dsa: microchip: add support for port mirror operations

In reply to Prasanna Vengateshan

Added support for port_mirror_add() and port_mirror_del operations

Sniffing is limited to one port & alert the user if any new sniffing port is selected

---
 drivers/net/dsa/microchip/lan937x_main.c | 83 ++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
drivers/net/dsa/microchip/lan937x_main.c +83 -0
index 3380a4617725..171c46f37fa4 100644
@@ -129,6 +129,87 @@ static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port,
 	mutex_unlock(&dev->dev_mutex);
 }
 
+static int lan937x_port_mirror_add(struct dsa_switch *ds, int port,
+				   struct dsa_mall_mirror_tc_entry *mirror,
+				   bool ingress)
+{
+	struct ksz_device *dev = ds->priv;
+	int ret, p;
+	u8 data;
+
+	/* Configure ingress/egress mirroring*/
+	if (ingress)
+		ret = lan937x_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX,
+				       true);
+	else
+		ret = lan937x_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX,
+				       true);
+	if (ret < 0)
+		return ret;
+
+	/* Configure sniffer port meanwhile limit to one sniffer port
+	 * Check if any of the port is already set for sniffing
+	 * If yes, instruct the user to remove the previous entry & exit
+	 */
+	for (p = 0; p < dev->port_cnt; p++) {
+		/*Skip the current sniffing port*/
+		if (p == mirror->to_local_port)
+			continue;
+
+		ret = lan937x_pread8(dev, p, P_MIRROR_CTRL, &data);
+		if (ret < 0)
+			return ret;
+
+		if (data & PORT_MIRROR_SNIFFER) {
+			dev_err(dev->dev,
+				"Delete existing rules towards %s & try\n",
+				dsa_to_port(ds, p)->name);
+			return -EBUSY;
+		}
+	}
+
+	ret = lan937x_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
+			       PORT_MIRROR_SNIFFER, true);
+	if (ret < 0)
+		return ret;
+
+	ret = lan937x_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
+
+	return ret;
+}
+
+static void lan937x_port_mirror_del(struct dsa_switch *ds, int port,
+				    struct dsa_mall_mirror_tc_entry *mirror)
+{
+	struct ksz_device *dev = ds->priv;
+	bool in_use = false;
+	u8 data;
+	int p;
+
+	/* clear ingress/egress mirroring port*/
+	if (mirror->ingress)
+		lan937x_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX,
+				 false);
+	else
+		lan937x_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX,
+				 false);
+
+	/* Check if any of the port is still referring to sniffer port*/
+	for (p = 0; p < dev->port_cnt; p++) {
+		lan937x_pread8(dev, p, P_MIRROR_CTRL, &data);
+
+		if ((data & (PORT_MIRROR_RX | PORT_MIRROR_TX))) {
+			in_use = true;
+			break;
+		}
+	}
+
+	/* delete sniffing if there are no other mirroring rule exist*/
+	if (!in_use)
+		lan937x_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
+				 PORT_MIRROR_SNIFFER, false);
+}
+
 static phy_interface_t lan937x_get_interface(struct ksz_device *dev, int port)
 {
 	phy_interface_t interface;
@@ -406,6 +487,8 @@ const struct dsa_switch_ops lan937x_switch_ops = {
 	.port_bridge_leave = ksz_port_bridge_leave,
 	.port_stp_state_set = lan937x_port_stp_state_set,
 	.port_fast_age = ksz_port_fast_age,
+	.port_mirror_add = lan937x_port_mirror_add,
+	.port_mirror_del = lan937x_port_mirror_del,
 	.port_max_mtu = lan937x_get_max_mtu,
 	.port_change_mtu = lan937x_change_mtu,
 	.phylink_validate = lan937x_phylink_validate,
Signature · 1 line
2.27.0
maintainer
#38 lore raw

Re: [PATCH v3 net-next 08/10] net: dsa: microchip: add support for port mirror operations

Quoted 46 lines · Prasanna Vengateshan

Added support for port_mirror_add() and port_mirror_del operations

Sniffing is limited to one port & alert the user if any new sniffing port is selected

---
 drivers/net/dsa/microchip/lan937x_main.c | 83 ++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
drivers/net/dsa/microchip/lan937x_main.c +28 -0
index 3380a4617725..171c46f37fa4 100644
@@ -129,6 +129,87 @@ static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port,
 	mutex_unlock(&dev->dev_mutex);
 }
 
+static int lan937x_port_mirror_add(struct dsa_switch *ds, int port,
+				   struct dsa_mall_mirror_tc_entry *mirror,
+				   bool ingress)
+{
+	struct ksz_device *dev = ds->priv;
+	int ret, p;
+	u8 data;
+
+	/* Configure ingress/egress mirroring*/
+	if (ingress)
+		ret = lan937x_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX,
+				       true);
+	else
+		ret = lan937x_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX,
+				       true);
+	if (ret < 0)
+		return ret;
+
+	/* Configure sniffer port meanwhile limit to one sniffer port
+	 * Check if any of the port is already set for sniffing
+	 * If yes, instruct the user to remove the previous entry & exit
+	 */
+	for (p = 0; p < dev->port_cnt; p++) {
+		/*Skip the current sniffing port*/
+		if (p == mirror->to_local_port)
+			continue;
+
+		ret = lan937x_pread8(dev, p, P_MIRROR_CTRL, &data);

drivers/net/dsa/microchip/lan937x_main.c static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port, · line 169

Quoted 10 lines
drivers/net/dsa/microchip/lan937x_main.c +10 -0
+		if (ret < 0)
+			return ret;
+
+		if (data & PORT_MIRROR_SNIFFER) {
+			dev_err(dev->dev,
+				"Delete existing rules towards %s & try\n",
+				dsa_to_port(ds, p)->name);
+			return -EBUSY;
+		}
+	}

I think this check should be placed before you enable PORT_MIRROR_RX/PORT_MIRROR_TX.

drivers/net/dsa/microchip/lan937x_main.c static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port, · line 178

Quoted 9 lines
drivers/net/dsa/microchip/lan937x_main.c +9 -0
+
+	ret = lan937x_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
+			       PORT_MIRROR_SNIFFER, true);
+	if (ret < 0)
+		return ret;
+
+	ret = lan937x_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
+
+	return ret;

You can forgo an assignment to "ret" here and do "return lan937x_cfg(...)"

#39 lore raw

PATCH v3 9/10 net: dsa: microchip: add support for fdb and mdb management

In reply to Prasanna Vengateshan

Support for fdb_add, mdb_add, fdb_del, mdb_del and fdb_dump operations. ALU1 and ALU2 are used for fdb operations.

fdb_add: find any existing entries and update the port map. if ALU1 write is failed and attempt to write ALU2.
If ALU2 is also failed then exit. Clear WRITE_FAIL for both ALU1 & ALU2.

fdb_del: find the matching entry and clear the respective port in the port map by writing the ALU tables

fdb_dump: read and dump 2 ALUs upto last entry. ALU_START bit is used to find the last entry. If the read is timed out, then pass the error message.

mdb_add: Find the empty slot in ALU and update the port map & mac address by writing the ALU

mdb_del: find the matching entry and delete the respective port in port map by writing the ALU

---
 drivers/net/dsa/microchip/lan937x_main.c | 525 +++++++++++++++++++++++
 1 file changed, 525 insertions(+)
drivers/net/dsa/microchip/lan937x_main.c +525 -0
index 171c46f37fa4..fb780678ef8d 100644
@@ -15,6 +15,74 @@
 #include "ksz_common.h"
 #include "lan937x_dev.h"
 
+static u8 lan937x_get_fid(u16 vid)
+{
+	if (vid > ALU_FID_SIZE)
+		return LAN937X_GET_FID(vid);
+	else
+		return vid;
+}
+
+static int lan937x_read_table(struct ksz_device *dev, u32 *table)
+{
+	int ret;
+
+	/* read alu table */
+	ret = ksz_read32(dev, REG_SW_ALU_VAL_A, &table[0]);
+	if (ret < 0)
+		return ret;
+
+	ret = ksz_read32(dev, REG_SW_ALU_VAL_B, &table[1]);
+	if (ret < 0)
+		return ret;
+
+	ret = ksz_read32(dev, REG_SW_ALU_VAL_C, &table[2]);
+	if (ret < 0)
+		return ret;
+
+	ret = ksz_read32(dev, REG_SW_ALU_VAL_D, &table[3]);
+
+	return ret;
+}
+
+static int lan937x_write_table(struct ksz_device *dev, u32 *table)
+{
+	int ret;
+
+	/* write alu table */
+	ret = ksz_write32(dev, REG_SW_ALU_VAL_A, table[0]);
+	if (ret < 0)
+		return ret;
+
+	ret = ksz_write32(dev, REG_SW_ALU_VAL_B, table[1]);
+	if (ret < 0)
+		return ret;
+
+	ret = ksz_write32(dev, REG_SW_ALU_VAL_C, table[2]);
+	if (ret < 0)
+		return ret;
+
+	ret = ksz_write32(dev, REG_SW_ALU_VAL_D, table[3]);
+
+	return ret;
+}
+
+static int lan937x_wait_alu_ready(int alu, struct ksz_device *dev)
+{
+	unsigned int val;
+
+	return regmap_read_poll_timeout(dev->regmap[2], REG_SW_ALU_CTRL(alu),
+					val, !(val & ALU_START), 10, 1000);
+}
+
+static int lan937x_wait_alu_sta_ready(struct ksz_device *dev)
+{
+	unsigned int val;
+
+	return regmap_read_poll_timeout(dev->regmap[2], REG_SW_ALU_STAT_CTRL__4,
+					val, !(val & ALU_STAT_START), 10, 1000);
+}
+
 static enum dsa_tag_protocol lan937x_get_tag_protocol(struct dsa_switch *ds,
 						      int port,
 						      enum dsa_tag_protocol mp)
@@ -129,6 +197,458 @@ static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port,
 	mutex_unlock(&dev->dev_mutex);
 }
 
+static int lan937x_port_fdb_add(struct dsa_switch *ds, int port,
+				const unsigned char *addr, u16 vid)
+{
+	struct ksz_device *dev = ds->priv;
+	u8 fid = lan937x_get_fid(vid);
+	u32 alu_table[4];
+	int ret, i;
+	u32 data;
+	u8 val;
+
+	mutex_lock(&dev->alu_mutex);
+
+	/* Accessing two ALU tables through loop */
+	for (i = 0; i < ALU_STA_DYN_CNT; i++) {
+		/* find any entry with mac & fid */
+		data = fid << ALU_FID_INDEX_S;
+		data |= ((addr[0] << 8) | addr[1]);
+
+		ret = ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
+		if (ret < 0)
+			break;
+
+		data = ((addr[2] << 24) | (addr[3] << 16));
+		data |= ((addr[4] << 8) | addr[5]);
+
+		ret = ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
+		if (ret < 0)
+			break;
+
+		/* start read operation */
+		ret = ksz_write32(dev, REG_SW_ALU_CTRL(i),
+				  ALU_READ | ALU_START);
+		if (ret < 0)
+			break;
+
+		/* wait to be finished */
+		ret = lan937x_wait_alu_ready(i, dev);
+		if (ret < 0) {
+			dev_err(dev->dev, "Failed to read ALU\n");
+			break;
+		}
+
+		/* read ALU entry */
+		ret = lan937x_read_table(dev, alu_table);
+		if (ret < 0) {
+			dev_err(dev->dev, "Failed to read ALU\n");
+			break;
+		}
+
+		/* update ALU entry */
+		alu_table[0] = ALU_V_STATIC_VALID;
+
+		/* update port number */
+		alu_table[1] |= BIT(port);
+
+		if (fid)
+			alu_table[1] |= ALU_V_USE_FID;
+
+		alu_table[2] = (fid << ALU_V_FID_S);
+		alu_table[2] |= ((addr[0] << 8) | addr[1]);
+		alu_table[3] = ((addr[2] << 24) | (addr[3] << 16));
+		alu_table[3] |= ((addr[4] << 8) | addr[5]);
+
+		ret = lan937x_write_table(dev, alu_table);
+		if (ret < 0)
+			break;
+
+		ret = ksz_write32(dev, REG_SW_ALU_CTRL(i),
+				  (ALU_WRITE | ALU_START));
+		if (ret < 0)
+			break;
+
+		/* wait to be finished */
+		ret = lan937x_wait_alu_ready(i, dev);
+		if (ret < 0) {
+			dev_err(dev->dev, "Failed to write ALU\n");
+			break;
+		}
+
+		ret = ksz_read8(dev, REG_SW_LUE_INT_STATUS__1, &val);
+		if (ret < 0)
+			break;
+
+		/* ALU2 write failed */
+		if (val & WRITE_FAIL_INT && i == 1)
+			dev_err(dev->dev, "Failed to write ALU\n");
+
+		/* if ALU1 write is failed and attempt to write ALU2,
+		 * otherwise exit. Clear Write fail for both ALU1 & ALU2
+		 */
+		if (val & WRITE_FAIL_INT) {
+			/* Write to clear the Write Fail */
+			ret = ksz_write8(dev, REG_SW_LUE_INT_STATUS__1,
+					 WRITE_FAIL_INT);
+			if (ret < 0)
+				break;
+		} else {
+			break;
+		}
+	}
+
+	mutex_unlock(&dev->alu_mutex);
+
+	return ret;
+}
+
+static int lan937x_port_fdb_del(struct dsa_switch *ds, int port,
+				const unsigned char *addr, u16 vid)
+{
+	struct ksz_device *dev = ds->priv;
+	u8 fid = lan937x_get_fid(vid);
+	u32 alu_table[4];
+	int ret, i;
+	u32 data;
+
+	mutex_lock(&dev->alu_mutex);
+
+	/* Accessing two ALU tables through loop */
+	for (i = 0; i < ALU_STA_DYN_CNT; i++) {
+		/* read any entry with mac & fid */
+		data = fid << ALU_FID_INDEX_S;
+		data |= ((addr[0] << 8) | addr[1]);
+		ret = ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
+		if (ret < 0)
+			break;
+
+		data = ((addr[2] << 24) | (addr[3] << 16));
+		data |= ((addr[4] << 8) | addr[5]);
+		ret = ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
+		if (ret < 0)
+			break;
+
+		/* start read operation */
+		ret = ksz_write32(dev, REG_SW_ALU_CTRL(i),
+				  (ALU_READ | ALU_START));
+		if (ret < 0)
+			break;
+
+		/* wait to be finished */
+		ret = lan937x_wait_alu_ready(i, dev);
+		if (ret < 0) {
+			dev_err(dev->dev, "Failed to read ALU\n");
+			break;
+		}
+
+		ret = ksz_read32(dev, REG_SW_ALU_VAL_A, &alu_table[0]);
+		if (ret < 0)
+			break;
+
+		if (alu_table[0] & ALU_V_STATIC_VALID) {
+			/* read ALU entry */
+			ret = lan937x_read_table(dev, alu_table);
+			if (ret < 0) {
+				dev_err(dev->dev, "Failed to read ALU table\n");
+				break;
+			}
+
+			/* clear forwarding port */
+			alu_table[1] &= ~BIT(port);
+
+			/* if there is no port to forward, clear table */
+			if ((alu_table[1] & ALU_V_PORT_MAP) == 0) {
+				alu_table[0] = 0;
+				alu_table[1] = 0;
+				alu_table[2] = 0;
+				alu_table[3] = 0;
+			}
+		} else {
+			alu_table[0] = 0;
+			alu_table[1] = 0;
+			alu_table[2] = 0;
+			alu_table[3] = 0;
+		}
+
+		ret = lan937x_write_table(dev, alu_table);
+		if (ret < 0)
+			break;
+
+		ret = ksz_write32(dev, REG_SW_ALU_CTRL(i),
+				  (ALU_WRITE | ALU_START));
+		if (ret < 0)
+			break;
+
+		/* wait to be finished */
+		ret = lan937x_wait_alu_ready(i, dev);
+		if (ret < 0) {
+			dev_err(dev->dev, "Failed to delete ALU Entries\n");
+			break;
+		}
+	}
+
+	mutex_unlock(&dev->alu_mutex);
+
+	return ret;
+}
+
+static void lan937x_convert_alu(struct lan_alu_struct *alu, u32 *alu_table)
+{
+	alu->is_static = !!(alu_table[0] & ALU_V_STATIC_VALID);
+	alu->is_src_filter = !!(alu_table[0] & ALU_V_SRC_FILTER);
+	alu->is_dst_filter = !!(alu_table[0] & ALU_V_DST_FILTER);
+	alu->prio_age = (alu_table[0] >> ALU_V_PRIO_AGE_CNT_S) &
+			 ALU_V_PRIO_AGE_CNT_M;
+	alu->mstp = alu_table[0] & ALU_V_MSTP_M;
+
+	alu->is_override = !!(alu_table[1] & ALU_V_OVERRIDE);
+	alu->is_use_fid = !!(alu_table[1] & ALU_V_USE_FID);
+	alu->port_forward = alu_table[1] & ALU_V_PORT_MAP;
+
+	alu->fid = (alu_table[2] >> ALU_V_FID_S) & ALU_V_FID_M;
+
+	alu->mac[0] = (alu_table[2] >> 8) & 0xFF;
+	alu->mac[1] = alu_table[2] & 0xFF;
+	alu->mac[2] = (alu_table[3] >> 24) & 0xFF;
+	alu->mac[3] = (alu_table[3] >> 16) & 0xFF;
+	alu->mac[4] = (alu_table[3] >> 8) & 0xFF;
+	alu->mac[5] = alu_table[3] & 0xFF;
+}
+
+static int lan937x_port_fdb_dump(struct dsa_switch *ds, int port,
+				 dsa_fdb_dump_cb_t *cb, void *data)
+{
+	struct ksz_device *dev = ds->priv;
+	struct lan_alu_struct alu;
+	u32 lan937x_data;
+	u32 alu_table[4];
+	int timeout;
+	int ret, i;
+
+	mutex_lock(&dev->alu_mutex);
+
+	/* Accessing two ALU tables through loop */
+	for (i = 0; i < ALU_STA_DYN_CNT; i++) {
+		/* start ALU search */
+		ret = ksz_write32(dev, REG_SW_ALU_CTRL(i),
+				  (ALU_START | ALU_SEARCH));
+		if (ret < 0)
+			goto exit;
+
+		do {
+			timeout = 1000;
+			do {
+				ret = ksz_read32(dev, REG_SW_ALU_CTRL(i),
+						 &lan937x_data);
+				if (ret < 0)
+					goto exit;
+
+				if ((lan937x_data & ALU_VALID) ||
+				    !(lan937x_data & ALU_START))
+					break;
+				usleep_range(1, 10);
+			} while (timeout-- > 0);
+
+			if (!timeout) {
+				dev_err(dev->dev, "Failed to search ALU\n");
+				ret = -ETIMEDOUT;
+				goto exit;
+			}
+
+			/* read ALU table */
+			ret = lan937x_read_table(dev, alu_table);
+			if (ret < 0)
+				goto exit;
+
+			lan937x_convert_alu(&alu, alu_table);
+
+			if (alu.port_forward & BIT(port)) {
+				ret = cb(alu.mac, alu.fid, alu.is_static, data);
+				if (ret)
+					goto exit;
+			}
+		} while (lan937x_data & ALU_START);
+
+exit:
+		/* stop ALU search & continue to next ALU if available */
+		ret = ksz_write32(dev, REG_SW_ALU_CTRL(i), 0);
+	}
+
+	mutex_unlock(&dev->alu_mutex);
+
+	return ret;
+}
+
+static int lan937x_port_mdb_add(struct dsa_switch *ds, int port,
+				const struct switchdev_obj_port_mdb *mdb)
+{
+	struct ksz_device *dev = ds->priv;
+	u8 fid = lan937x_get_fid(mdb->vid);
+	u32 static_table[4];
+	u32 mac_hi, mac_lo;
+	int index, ret;
+	u32 data;
+
+	mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
+	mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
+	mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
+
+	mutex_lock(&dev->alu_mutex);
+
+	/* Access the entries in the table */
+	for (index = 0; index < dev->num_statics; index++) {
+		/* find empty slot first */
+		data = (index << ALU_STAT_INDEX_S) |
+			ALU_STAT_READ | ALU_STAT_START;
+
+		ret = ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
+		if (ret < 0)
+			goto exit;
+
+		/* wait to be finished */
+		ret = lan937x_wait_alu_sta_ready(dev);
+		if (ret < 0) {
+			dev_err(dev->dev, "Failed to read ALU STATIC\n");
+			goto exit;
+		}
+
+		/* read ALU static table */
+		ret = lan937x_read_table(dev, static_table);
+		if (ret < 0)
+			goto exit;
+
+		if (static_table[0] & ALU_V_STATIC_VALID) {
+			/* check this has same fid & mac address */
+			if (((static_table[2] >> ALU_V_FID_S) == fid) &&
+			    ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
+			    static_table[3] == mac_lo) {
+				/* found matching one */
+				break;
+			}
+		} else {
+			/* found empty one */
+			break;
+		}
+	}
+
+	/* no available entry */
+	if (index == dev->num_statics) {
+		ret = -ENOSPC;
+		goto exit;
+	}
+
+	/* add entry */
+	static_table[0] = ALU_V_STATIC_VALID;
+
+	static_table[1] |= BIT(port);
+	if (fid)
+		static_table[1] |= ALU_V_USE_FID;
+	static_table[2] = (fid << ALU_V_FID_S);
+	static_table[2] |= mac_hi;
+	static_table[3] = mac_lo;
+
+	ret = lan937x_write_table(dev, static_table);
+	if (ret < 0)
+		goto exit;
+
+	data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START;
+	ret = ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
+	if (ret < 0)
+		goto exit;
+
+	/* wait to be finished */
+	ret = lan937x_wait_alu_sta_ready(dev);
+	if (ret < 0)
+		dev_err(dev->dev, "Failed to read ALU STATIC\n");
+
+exit:
+	mutex_unlock(&dev->alu_mutex);
+	return ret;
+}
+
+static int lan937x_port_mdb_del(struct dsa_switch *ds, int port,
+				const struct switchdev_obj_port_mdb *mdb)
+{
+	struct ksz_device *dev = ds->priv;
+	u8 fid = lan937x_get_fid(mdb->vid);
+	u32 static_table[4];
+	u32 mac_hi, mac_lo;
+	int index, ret;
+	u32 data;
+
+	mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
+	mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
+	mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
+
+	mutex_lock(&dev->alu_mutex);
+
+	/* Access the entries in the table */
+	for (index = 0; index < dev->num_statics; index++) {
+		data = (index << ALU_STAT_INDEX_S) |
+			ALU_STAT_READ | ALU_STAT_START;
+		ret = ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
+		if (ret < 0)
+			goto exit;
+
+		/* wait to be finished */
+		ret = lan937x_wait_alu_sta_ready(dev);
+		if (ret < 0) {
+			dev_err(dev->dev, "Failed to read ALU STATIC\n");
+			goto exit;
+		}
+
+		/* read ALU static table */
+		ret = lan937x_read_table(dev, static_table);
+		if (ret < 0)
+			goto exit;
+
+		if (static_table[0] & ALU_V_STATIC_VALID) {
+			/* check this has same fid & mac address */
+			if (((static_table[2] >> ALU_V_FID_S) == fid) &&
+			    ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
+			    static_table[3] == mac_lo) {
+				/* found matching one */
+				break;
+			}
+		}
+	}
+
+	/* no available entry */
+	if (index == dev->num_statics)
+		goto exit;
+
+	/* clear port based on port arg */
+	static_table[1] &= ~BIT(port);
+
+	if ((static_table[1] & ALU_V_PORT_MAP) == 0) {
+		/* delete entry */
+		static_table[0] = 0;
+		static_table[1] = 0;
+		static_table[2] = 0;
+		static_table[3] = 0;
+	}
+
+	ret = lan937x_write_table(dev, static_table);
+	if (ret < 0)
+		goto exit;
+
+	data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START;
+	ret = ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
+	if (ret < 0)
+		goto exit;
+
+	/* wait to be finished */
+	ret = lan937x_wait_alu_sta_ready(dev);
+	if (ret < 0)
+		dev_err(dev->dev, "Failed to read ALU STATIC\n");
+
+exit:
+	mutex_unlock(&dev->alu_mutex);
+
+	return ret;
+}
+
 static int lan937x_port_mirror_add(struct dsa_switch *ds, int port,
 				   struct dsa_mall_mirror_tc_entry *mirror,
 				   bool ingress)
@@ -487,6 +1007,11 @@ const struct dsa_switch_ops lan937x_switch_ops = {
 	.port_bridge_leave = ksz_port_bridge_leave,
 	.port_stp_state_set = lan937x_port_stp_state_set,
 	.port_fast_age = ksz_port_fast_age,
+	.port_fdb_dump = lan937x_port_fdb_dump,
+	.port_fdb_add = lan937x_port_fdb_add,
+	.port_fdb_del = lan937x_port_fdb_del,
+	.port_mdb_add = lan937x_port_mdb_add,
+	.port_mdb_del = lan937x_port_mdb_del,
 	.port_mirror_add = lan937x_port_mirror_add,
 	.port_mirror_del = lan937x_port_mirror_del,
 	.port_max_mtu = lan937x_get_max_mtu,
Signature · 1 line
2.27.0
maintainer
#40 lore raw

Re: [PATCH v3 net-next 09/10] net: dsa: microchip: add support for fdb and mdb management

Quoted 7 lines · Prasanna Vengateshan
drivers/net/dsa/microchip/lan937x_main.c +7 -0
+static int lan937x_port_fdb_add(struct dsa_switch *ds, int port,
+				const unsigned char *addr, u16 vid)
+{
+	struct ksz_device *dev = ds->priv;
+	u8 fid = lan937x_get_fid(vid);
+	u32 alu_table[4];
+	int ret, i;

drivers/net/dsa/microchip/lan937x_main.c static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port, · line 216

Quoted 10 lines
drivers/net/dsa/microchip/lan937x_main.c +10 -0
+	u32 data;
+	u8 val;
+
+	mutex_lock(&dev->alu_mutex);
+
+	/* Accessing two ALU tables through loop */
+	for (i = 0; i < ALU_STA_DYN_CNT; i++) {
+		/* find any entry with mac & fid */
+		data = fid << ALU_FID_INDEX_S;
+		data |= ((addr[0] << 8) | addr[1]);

Maybe upper_32_bits(ether_addr_to_u64(addr)) and lower_32_bits(ether_addr_to_u64(addr)) would be slightly easier on the eye?

Quoted 7 lines
drivers/net/dsa/microchip/lan937x_main.c +7 -0
+		if (alu_table[0] & ALU_V_STATIC_VALID) {
+			/* read ALU entry */
+			ret = lan937x_read_table(dev, alu_table);
+			if (ret < 0) {
+				dev_err(dev->dev, "Failed to read ALU table\n");
+				break;
+			}

drivers/net/dsa/microchip/lan937x_main.c static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port, · line 365

Quoted 10 lines
drivers/net/dsa/microchip/lan937x_main.c +10 -0
+
+			/* clear forwarding port */
+			alu_table[1] &= ~BIT(port);
+
+			/* if there is no port to forward, clear table */
+			if ((alu_table[1] & ALU_V_PORT_MAP) == 0) {
+				alu_table[0] = 0;
+				alu_table[1] = 0;
+				alu_table[2] = 0;
+				alu_table[3] = 0;

memset?

Quoted 11 lines
drivers/net/dsa/microchip/lan937x_main.c +11 -0
+			}
+		} else {
+			alu_table[0] = 0;
+			alu_table[1] = 0;
+			alu_table[2] = 0;
+			alu_table[3] = 0;
+		}
+
+		ret = lan937x_write_table(dev, alu_table);
+		if (ret < 0)
+			break;
#41 lore raw

PATCH v3 10/10 net: dsa: microchip: add support for vlan operations

In reply to Prasanna Vengateshan

Support for VLAN add, del, prepare and filtering operations.

The VLAN aware is a global setting. Mixed vlan filterings are not supported. vlan_filtering_is_global is made as true in lan937x_setup function.

---
 drivers/net/dsa/microchip/lan937x_main.c | 198 +++++++++++++++++++++++
 1 file changed, 198 insertions(+)
drivers/net/dsa/microchip/lan937x_main.c +198 -0
index fb780678ef8d..963b066a8ad1 100644
@@ -15,6 +15,14 @@
 #include "ksz_common.h"
 #include "lan937x_dev.h"
 
+static int lan937x_wait_vlan_ctrl_ready(struct ksz_device *dev)
+{
+	unsigned int val;
+
+	return regmap_read_poll_timeout(dev->regmap[0], REG_SW_VLAN_CTRL, val,
+					!(val & VLAN_START), 10, 1000);
+}
+
 static u8 lan937x_get_fid(u16 vid)
 {
 	if (vid > ALU_FID_SIZE)
@@ -23,6 +31,97 @@ static u8 lan937x_get_fid(u16 vid)
 		return vid;
 }
 
+static int lan937x_get_vlan_table(struct ksz_device *dev, u16 vid,
+				  struct lan937x_vlan *vlan_entry)
+{
+	u32 data;
+	int ret;
+
+	mutex_lock(&dev->vlan_mutex);
+
+	ret = ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
+	if (ret < 0)
+		goto exit;
+
+	ret = ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_READ | VLAN_START);
+	if (ret < 0)
+		goto exit;
+
+	/* wait to be cleared */
+	ret = lan937x_wait_vlan_ctrl_ready(dev);
+	if (ret < 0)
+		goto exit;
+
+	ret = ksz_read32(dev, REG_SW_VLAN_ENTRY__4, &data);
+	if (ret < 0)
+		goto exit;
+
+	vlan_entry->valid = !!(data & VLAN_VALID);
+	vlan_entry->fid	= data & VLAN_FID_M;
+
+	ret = ksz_read32(dev, REG_SW_VLAN_ENTRY_UNTAG__4,
+			 &vlan_entry->untag_prtmap);
+	if (ret < 0)
+		goto exit;
+
+	ret = ksz_read32(dev, REG_SW_VLAN_ENTRY_PORTS__4,
+			 &vlan_entry->fwd_map);
+	if (ret < 0)
+		goto exit;
+
+	ret = ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
+	if (ret < 0)
+		goto exit;
+
+exit:
+	mutex_unlock(&dev->vlan_mutex);
+
+	return ret;
+}
+
+static int lan937x_set_vlan_table(struct ksz_device *dev, u16 vid,
+				  struct lan937x_vlan *vlan_entry)
+{
+	u32 data;
+	int ret;
+
+	mutex_lock(&dev->vlan_mutex);
+
+	data = vlan_entry->valid ? VLAN_VALID : 0;
+	data |= vlan_entry->fid;
+
+	ret = ksz_write32(dev, REG_SW_VLAN_ENTRY__4, data);
+	if (ret < 0)
+		goto exit;
+
+	ret = ksz_write32(dev, REG_SW_VLAN_ENTRY_UNTAG__4,
+			  vlan_entry->untag_prtmap);
+	if (ret < 0)
+		goto exit;
+
+	ret = ksz_write32(dev, REG_SW_VLAN_ENTRY_PORTS__4, vlan_entry->fwd_map);
+	if (ret < 0)
+		goto exit;
+
+	ret = ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
+	if (ret < 0)
+		goto exit;
+
+	ret = ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_START | VLAN_WRITE);
+	if (ret < 0)
+		goto exit;
+
+	/* wait to be cleared */
+	ret = lan937x_wait_vlan_ctrl_ready(dev);
+	if (ret < 0)
+		goto exit;
+
+exit:
+	mutex_unlock(&dev->vlan_mutex);
+
+	return ret;
+}
+
 static int lan937x_read_table(struct ksz_device *dev, u32 *table)
 {
 	int ret;
@@ -197,6 +296,102 @@ static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port,
 	mutex_unlock(&dev->dev_mutex);
 }
 
+static int lan937x_port_vlan_filtering(struct dsa_switch *ds, int port,
+				       bool flag,
+				       struct netlink_ext_ack *extack)
+{
+	struct ksz_device *dev = ds->priv;
+	int ret;
+
+	ret = lan937x_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE,
+			  flag);
+
+	return ret;
+}
+
+static int lan937x_port_vlan_add(struct dsa_switch *ds, int port,
+				 const struct switchdev_obj_port_vlan *vlan,
+				 struct netlink_ext_ack *extack)
+{
+	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
+	struct ksz_device *dev = ds->priv;
+	struct lan937x_vlan vlan_entry;
+	int ret;
+
+	ret = lan937x_get_vlan_table(dev, vlan->vid, &vlan_entry);
+	if (ret < 0) {
+		NL_SET_ERR_MSG_MOD(extack, "Failed to get vlan table\n");
+		return ret;
+	}
+
+	vlan_entry.fid = lan937x_get_fid(vlan->vid);
+	vlan_entry.valid = true;
+
+	/* set/clear switch port when updating vlan table registers */
+	if (untagged)
+		vlan_entry.untag_prtmap |= BIT(port);
+	else
+		vlan_entry.untag_prtmap &= ~BIT(port);
+
+	vlan_entry.fwd_map |= BIT(port);
+
+	ret = lan937x_set_vlan_table(dev, vlan->vid, &vlan_entry);
+	if (ret < 0) {
+		NL_SET_ERR_MSG_MOD(extack, "Failed to set vlan table\n");
+		return ret;
+	}
+
+	/* change PVID */
+	if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
+		ret = lan937x_pwrite16(dev, port, REG_PORT_DEFAULT_VID,
+				       vlan->vid);
+		if (ret < 0) {
+			NL_SET_ERR_MSG_MOD(extack, "Failed to set pvid\n");
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+static int lan937x_port_vlan_del(struct dsa_switch *ds, int port,
+				 const struct switchdev_obj_port_vlan *vlan)
+{
+	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
+	struct ksz_device *dev = ds->priv;
+	struct lan937x_vlan vlan_entry;
+	u16 pvid;
+	int ret;
+
+	lan937x_pread16(dev, port, REG_PORT_DEFAULT_VID, &pvid);
+	pvid &= 0xFFF;
+
+	ret = lan937x_get_vlan_table(dev, vlan->vid, &vlan_entry);
+	if (ret < 0) {
+		dev_err(dev->dev, "Failed to get vlan table\n");
+		return ret;
+	}
+	/* clear port fwd map */
+	vlan_entry.fwd_map &= ~BIT(port);
+
+	if (untagged)
+		vlan_entry.untag_prtmap &= ~BIT(port);
+
+	ret = lan937x_set_vlan_table(dev, vlan->vid, &vlan_entry);
+	if (ret < 0) {
+		dev_err(dev->dev, "Failed to set vlan table\n");
+		return ret;
+	}
+
+	ret = lan937x_pwrite16(dev, port, REG_PORT_DEFAULT_VID, pvid);
+	if (ret < 0) {
+		dev_err(dev->dev, "Failed to set pvid\n");
+		return ret;
+	}
+
+	return 0;
+}
+
 static int lan937x_port_fdb_add(struct dsa_switch *ds, int port,
 				const unsigned char *addr, u16 vid)
 {
@@ -1007,6 +1202,9 @@ const struct dsa_switch_ops lan937x_switch_ops = {
 	.port_bridge_leave = ksz_port_bridge_leave,
 	.port_stp_state_set = lan937x_port_stp_state_set,
 	.port_fast_age = ksz_port_fast_age,
+	.port_vlan_filtering = lan937x_port_vlan_filtering,
+	.port_vlan_add = lan937x_port_vlan_add,
+	.port_vlan_del = lan937x_port_vlan_del,
 	.port_fdb_dump = lan937x_port_fdb_dump,
 	.port_fdb_add = lan937x_port_fdb_add,
 	.port_fdb_del = lan937x_port_fdb_del,
Signature · 1 line
2.27.0
maintainer
#42 lore raw

Re: [PATCH v3 net-next 10/10] net: dsa: microchip: add support for vlan operations

drivers/net/dsa/microchip/lan937x_main.c static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port, · line 323

Quoted 12 lines · Prasanna Vengateshan
drivers/net/dsa/microchip/lan937x_main.c +12 -0
+static int lan937x_port_vlan_add(struct dsa_switch *ds, int port,
+				 const struct switchdev_obj_port_vlan *vlan,
+				 struct netlink_ext_ack *extack)
+{
+	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
+	struct ksz_device *dev = ds->priv;
+	struct lan937x_vlan vlan_entry;
+	int ret;
+
+	ret = lan937x_get_vlan_table(dev, vlan->vid, &vlan_entry);
+	if (ret < 0) {
+		NL_SET_ERR_MSG_MOD(extack, "Failed to get vlan table\n");

The NL_SET_ERR_MSG_MOD function already adds the \n at the end.

Quoted 22 lines
drivers/net/dsa/microchip/lan937x_main.c +22 -0
+		return ret;
+	}
+
+	vlan_entry.fid = lan937x_get_fid(vlan->vid);
+	vlan_entry.valid = true;
+
+	/* set/clear switch port when updating vlan table registers */
+	if (untagged)
+		vlan_entry.untag_prtmap |= BIT(port);
+	else
+		vlan_entry.untag_prtmap &= ~BIT(port);
+
+	vlan_entry.fwd_map |= BIT(port);
+
+	ret = lan937x_set_vlan_table(dev, vlan->vid, &vlan_entry);
+	if (ret < 0) {
+		NL_SET_ERR_MSG_MOD(extack, "Failed to set vlan table\n");
+		return ret;
+	}
+
+	/* change PVID */
+	if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {

drivers/net/dsa/microchip/lan937x_main.c static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port, · line 355

Quoted 10 lines
drivers/net/dsa/microchip/lan937x_main.c +10 -0
+		ret = lan937x_pwrite16(dev, port, REG_PORT_DEFAULT_VID,
+				       vlan->vid);
+		if (ret < 0) {
+			NL_SET_ERR_MSG_MOD(extack, "Failed to set pvid\n");
+			return ret;
+		}
+	}
+
+	return 0;
+}

Side question: do you think the ds->configure_vlan_while_not_filtering = false from ksz9477.c and ksz8795.c serve any purpose, considering that you did not need this setting for lan937x? If not, could you please send a patch to remove that setting from those 2 other KSZ drivers? Thanks.

#43 lore raw

Re: [PATCH v3 net-next 10/10] net: dsa: microchip: add support for vlan operations

Quoted 4 lines · Vladimir Oltean

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

On Fri, Jul 23, 2021 at 11:01:08PM +0530, Prasanna Vengateshan wrote:

Quoted 14 lines

drivers/net/dsa/microchip/lan937x_main.c static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port, · line 323

Quoted 12 lines
drivers/net/dsa/microchip/lan937x_main.c +12 -0
+static int lan937x_port_vlan_add(struct dsa_switch *ds, int port,
+                              const struct switchdev_obj_port_vlan *vlan,
+                              struct netlink_ext_ack *extack)
+{
+     bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
+     struct ksz_device *dev = ds->priv;
+     struct lan937x_vlan vlan_entry;
+     int ret;
+
+     ret = lan937x_get_vlan_table(dev, vlan->vid, &vlan_entry);
+     if (ret < 0) {
+             NL_SET_ERR_MSG_MOD(extack, "Failed to get vlan table\n");

The NL_SET_ERR_MSG_MOD function already adds the \n at the end.

i will remove \n from other places as well for NL_SET_ERR_MSG_MOD.

Quoted 13 lines
Quoted 13 lines

+             return ret;

+
+     /* change PVID */
+     if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
+             ret = lan937x_pwrite16(dev, port, REG_PORT_DEFAULT_VID, +                                    vlan->vid);
+             if (ret < 0) {
+                     NL_SET_ERR_MSG_MOD(extack, "Failed to set pvid\n"); +                     return ret;
+             }
+     }

Quoted 8 lines

drivers/net/dsa/microchip/lan937x_main.c static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port, · line 355

Quoted 3 lines
drivers/net/dsa/microchip/lan937x_main.c +3 -0
+
+     return 0;
+}

Side question: do you think the ds->configure_vlan_while_not_filtering = false from ksz9477.c and ksz8795.c serve any purpose, considering that you did not need this setting for lan937x? If not, could you please send a patch to remove that setting from those 2 other KSZ drivers? Thanks.

Sure, I will add this patch in my next submission.