Skip to contentSkip to conversation

net: dsa: Add Airoha AN8855 support

PATCH Superseded Christian Marangi 6 participants 15 messages Nov 6, 2024 – Nov 8, 2024

Subsystems (6)

v3 of 25 Superseded Latest is v25 →

lore
Share & tools
lore
#0 lore raw

v3 cover net: dsa: Add Airoha AN8855 support

This small series add the initial support for the Airoha AN8855 Switch.

It's a 5 port Gigabit Switch with SGMII/HSGMII upstream port.

This is starting to get in the wild and there are already some router having this switch chip.

It's conceptually similar to mediatek switch but register and bits are different. And there is that massive Hell that is the PCS configuration.
Saddly for that part we have absolutely NO documentation currently.

There is this special thing where PHY needs to be calibrated with values from the switch efuse. (the thing have a whole cpu timer and MCU)

Some cleanup API are used and one extra patch for mdio_mutex_nested is introduced. As suggested some time ago, the use of such API is limited to scoped variants and not the guard ones.

Posting as RFC as I expect in later version to add additional feature but this is already working and upstream-ready. So this is really to have a review of the very basic features and if I missed anything in recent implementation of DSA.

Changes v3:
- Out of RFC
- Switch PHY code to select_page API
- Better describe masks and bits in PHY driver for ADC register
- Drop raw values and use define for mii read/write
- Switch to absolute PHY address
- Replace raw values with mask and bits for pcs_config
- Fix typo for ext-surge property name
- Drop support for relocating Switch base PHY address on the bus Changes v2:
- Drop mutex guard patch
- Drop guard usage in DSA driver
- Use __mdiobus_write/read
- Check return condition and return errors for mii read/write
- Fix wrong logic for EEE
- Fix link_down (don't force link down with autoneg)
- Fix forcing speed on sgmii autoneg
- Better document link speed for sgmii reg
- Use standard define for sgmii reg
- Imlement nvmem support to expose switch EFUSE
- Rework PHY calibration with the use of NVMEM producer/consumer
- Update DT with new NVMEM property
- Move aneg validation for 2500-basex in pcs_config
- Move r50Ohm table and function to PHY driver

Christian Marangi (3):
dt-bindings: net: dsa: Add Airoha AN8855 Gigabit Switch documentation
net: dsa: Add Airoha AN8855 5-Port Gigabit DSA Switch driver
net: phy: Add Airoha AN8855 Internal Switch Gigabit PHY

 .../bindings/net/dsa/airoha,an8855.yaml       |  242 ++
 MAINTAINERS                                   |   11 +
 drivers/net/dsa/Kconfig                       |    9 +
 drivers/net/dsa/Makefile                      |    1 +
 drivers/net/dsa/an8855.c                      | 2054 +++++++++++++++++
 drivers/net/dsa/an8855.h                      |  628 +++++
 drivers/net/phy/Kconfig                       |    5 +
 drivers/net/phy/Makefile                      |    1 +
 drivers/net/phy/air_an8855.c                  |  278 +++
 9 files changed, 3229 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/dsa/airoha,an8855.yaml
 create mode 100644 drivers/net/dsa/an8855.c
 create mode 100644 drivers/net/dsa/an8855.h
 create mode 100644 drivers/net/phy/air_an8855.c
Signature · 1 line
2.45.2
#1 lore raw

PATCH v3 1/3 dt-bindings: net: dsa: Add Airoha AN8855 Gigabit Switch documentation

Add Airoha AN8855 5 port Gigabit Switch documentation.

The switch node requires an additional mdio node to describe each internal PHY absolute address on the bus.

Calibration values might be stored in switch EFUSE and internal PHY might need to be calibrated, in such case, airoha,ext-surge needs to be enabled and relative NVMEM cells needs to be defined in nvmem-layout node.

---
 .../bindings/net/dsa/airoha,an8855.yaml       | 242 ++++++++++++++++++
 1 file changed, 242 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/dsa/airoha,an8855.yaml
Documentation/devicetree/bindings/net/dsa/airoha,an8855.yaml new file +242 -0
new file mode 100644
index 000000000000..8ea2fadbab85
@@ -0,0 +1,242 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/dsa/airoha,an8855.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Airoha AN8855 Gigabit switch
+
+maintainers:
+  - Christian Marangi <[email protected]>
+
+description: >
+  Airoha AN8855 is a 5-port Gigabit Switch.
+
+  The switch node requires an additional mdio node to describe each internal
+  PHY relative offset as the PHY address for the switch match the one for
+  the PHY ports. On top of internal PHY address, the switch base PHY address
+  is added.
+
+  Also the switch base PHY address can be configured and changed after the
+  first initialization. On reset, the switch PHY address is ALWAYS 1.
+
+properties:
+  compatible:
+    const: airoha,an8855
+
+  reg:
+    maxItems: 1
+
+  reset-gpios:
+    description:
+      GPIO to be used to reset the whole device
+    maxItems: 1
+
+  airoha,ext-surge:
+    $ref: /schemas/types.yaml#/definitions/flag
+    description:
+      Calibrate the internal PHY with the calibration values stored in EFUSE
+      for the r50Ohm values.
+
+  '#nvmem-cell-cells':
+    const: 0
+
+  nvmem-layout:
+    $ref: /schemas/nvmem/layouts/nvmem-layout.yaml
+    description:
+      NVMEM Layout for exposed EFUSE. (for example to propagate calibration
+      value for r50Ohm for internal PHYs)
+
+  mdio:
+    $ref: /schemas/net/mdio.yaml#
+    unevaluatedProperties: false
+    description:
+      Define the absolute address of the internal PHY for each port.
+
+$ref: dsa.yaml#
+
+required:
+  - compatible
+  - reg
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    mdio {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        switch@1 {
+            compatible = "airoha,an8855";
+            reg = <1>;
+            reset-gpios = <&pio 39 0>;
+
+            airoha,ext-surge;
+
+            #nvmem-cell-cells = <0>;
+
+            nvmem-layout {
+                compatible = "fixed-layout";
+                #address-cells = <1>;
+                #size-cells = <1>;
+
+                shift_sel_port0_tx_a: shift-sel-port0-tx-a@c {
+                    reg = <0xc 0x4>;
+                };
+
+                shift_sel_port0_tx_b: shift-sel-port0-tx-b@10 {
+                    reg = <0x10 0x4>;
+                };
+
+                shift_sel_port0_tx_c: shift-sel-port0-tx-c@14 {
+                    reg = <0x14 0x4>;
+                };
+
+                shift_sel_port0_tx_d: shift-sel-port0-tx-d@18 {
+                    reg = <0x18 0x4>;
+                };
+
+                shift_sel_port1_tx_a: shift-sel-port1-tx-a@1c {
+                    reg = <0x1c 0x4>;
+                };
+
+                shift_sel_port1_tx_b: shift-sel-port1-tx-b@20 {
+                    reg = <0x20 0x4>;
+                };
+
+                shift_sel_port1_tx_c: shift-sel-port1-tx-c@24 {
+                    reg = <0x24 0x4>;
+                };
+
+                shift_sel_port1_tx_d: shift-sel-port1-tx-d@28 {
+                    reg = <0x28 0x4>;
+                };
+
+                shift_sel_port2_tx_a: shift-sel-port2-tx-a@2c {
+                    reg = <0x2c 0x4>;
+                };
+
+                shift_sel_port2_tx_b: shift-sel-port2-tx-b@30 {
+                    reg = <0x30 0x4>;
+                };
+
+                shift_sel_port2_tx_c: shift-sel-port2-tx-c@34 {
+                    reg = <0x34 0x4>;
+                };
+
+                shift_sel_port2_tx_d: shift-sel-port2-tx-d@38 {
+                    reg = <0x38 0x4>;
+                };
+
+                shift_sel_port3_tx_a: shift-sel-port3-tx-a@4c {
+                    reg = <0x4c 0x4>;
+                };
+
+                shift_sel_port3_tx_b: shift-sel-port3-tx-b@50 {
+                    reg = <0x50 0x4>;
+                };
+
+                shift_sel_port3_tx_c: shift-sel-port3-tx-c@54 {
+                    reg = <0x54 0x4>;
+                };
+
+                shift_sel_port3_tx_d: shift-sel-port3-tx-d@58 {
+                    reg = <0x58 0x4>;
+                };
+            };
+
+            ports {
+                #address-cells = <1>;
+                #size-cells = <0>;
+
+                port@0 {
+                    reg = <0>;
+                    label = "lan1";
+                    phy-mode = "internal";
+                    phy-handle = <&internal_phy1>;
+                };
+
+                port@1 {
+                    reg = <1>;
+                    label = "lan2";
+                    phy-mode = "internal";
+                    phy-handle = <&internal_phy2>;
+                };
+
+                port@2 {
+                    reg = <2>;
+                    label = "lan3";
+                    phy-mode = "internal";
+                    phy-handle = <&internal_phy3>;
+                };
+
+                port@3 {
+                    reg = <3>;
+                    label = "lan4";
+                    phy-mode = "internal";
+                    phy-handle = <&internal_phy4>;
+                };
+
+                port@5 {
+                    reg = <5>;
+                    label = "cpu";
+                    ethernet = <&gmac0>;
+                    phy-mode = "2500base-x";
+
+                    fixed-link {
+                        speed = <2500>;
+                        full-duplex;
+                        pause;
+                    };
+                };
+            };
+
+            mdio {
+                #address-cells = <1>;
+                #size-cells = <0>;
+
+                internal_phy1: phy@1 {
+                    reg = <1>;
+
+                    nvmem-cells = <&shift_sel_port0_tx_a>,
+                                  <&shift_sel_port0_tx_b>,
+                                  <&shift_sel_port0_tx_c>,
+                                  <&shift_sel_port0_tx_d>;
+                    nvmem-cell-names = "tx_a", "tx_b", "tx_c", "tx_d";
+                };
+
+                internal_phy2: phy@2 {
+                    reg = <2>;
+
+                    nvmem-cells = <&shift_sel_port1_tx_a>,
+                                  <&shift_sel_port1_tx_b>,
+                                  <&shift_sel_port1_tx_c>,
+                                  <&shift_sel_port1_tx_d>;
+                    nvmem-cell-names = "tx_a", "tx_b", "tx_c", "tx_d";
+                };
+
+                internal_phy3: phy@3 {
+                    reg = <3>;
+
+                    nvmem-cells = <&shift_sel_port2_tx_a>,
+                                  <&shift_sel_port2_tx_b>,
+                                  <&shift_sel_port2_tx_c>,
+                                  <&shift_sel_port2_tx_d>;
+                    nvmem-cell-names = "tx_a", "tx_b", "tx_c", "tx_d";
+                };
+
+                internal_phy4: phy@4 {
+                    reg = <4>;
+
+                    nvmem-cells = <&shift_sel_port3_tx_a>,
+                                  <&shift_sel_port3_tx_b>,
+                                  <&shift_sel_port3_tx_c>,
+                                  <&shift_sel_port3_tx_d>;
+                    nvmem-cell-names = "tx_a", "tx_b", "tx_c", "tx_d";
+                };
+            };
+        };
+    };
Signature · 1 line
2.45.2
#2 lore raw

PATCH v3 2/3 net: dsa: Add Airoha AN8855 5-Port Gigabit DSA Switch driver

In reply to Christian Marangi

Add Airoha AN8855 5-Port Gigabit DSA switch.

The switch is also a nvmem-provider as it does have EFUSE to calibrate the internal PHYs.

---
 MAINTAINERS              |   10 +
 drivers/net/dsa/Kconfig  |    9 +
 drivers/net/dsa/Makefile |    1 +
 drivers/net/dsa/an8855.c | 2054 ++++++++++++++++++++++++++++++++++++++
 drivers/net/dsa/an8855.h |  628 ++++++++++++
 5 files changed, 2702 insertions(+)
 create mode 100644 drivers/net/dsa/an8855.c
 create mode 100644 drivers/net/dsa/an8855.h
MAINTAINERS +10 -0
index 16933f9faa0f..e3077d9feee2 100644
@@ -717,6 +717,16 @@ S:	Supported
 F:	fs/aio.c
 F:	include/linux/*aio*.h
 
+AIROHA DSA DRIVER
+M:	Christian Marangi <[email protected]>
+L:	[email protected] (moderated for non-subscribers)
+L:	[email protected] (moderated for non-subscribers)
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/net/dsa/airoha,an8855.yaml
+F:	drivers/net/dsa/an8855.c
+F:	drivers/net/dsa/an8855.h
+
 AIROHA ETHERNET DRIVER
 M:	Lorenzo Bianconi <[email protected]>
 L:	[email protected] (moderated for non-subscribers)
drivers/net/dsa/Kconfig +9 -0
index 2d10b4d6cfbb..6b6d0b7bae72 100644
@@ -24,6 +24,15 @@ config NET_DSA_LOOP
 	  This enables support for a fake mock-up switch chip which
 	  exercises the DSA APIs.
 
+
+config NET_DSA_AN8855
+	tristate "Airoha AN8855 Ethernet switch support"
+	depends on NET_DSA
+	select NET_DSA_TAG_MTK
+	help
+	  This enables support for the Airoha AN8855 Ethernet switch
+	  chip.
+
 source "drivers/net/dsa/hirschmann/Kconfig"
 
 config NET_DSA_LANTIQ_GSWIP
drivers/net/dsa/Makefile +1 -0
index cb9a97340e58..a74afb41a491 100644
@@ -5,6 +5,7 @@ obj-$(CONFIG_NET_DSA_LOOP)	+= dsa_loop.o
 ifdef CONFIG_NET_DSA_LOOP
 obj-$(CONFIG_FIXED_PHY)		+= dsa_loop_bdinfo.o
 endif
+obj-$(CONFIG_NET_DSA_AN8855)	+= an8855.o
 obj-$(CONFIG_NET_DSA_LANTIQ_GSWIP) += lantiq_gswip.o
 obj-$(CONFIG_NET_DSA_MT7530)	+= mt7530.o
 obj-$(CONFIG_NET_DSA_MT7530_MDIO) += mt7530-mdio.o
drivers/net/dsa/an8855.c new file +2054 -0

2,054 lines not on this page · raw message

drivers/net/dsa/an8855.h new file +628 -0

628 lines not on this page · raw message

Signature · 1 line
2.45.2
#3 lore raw

Re: [net-next PATCH v3 2/3] net: dsa: Add Airoha AN8855 5-Port Gigabit DSA Switch driver

Le 06/11/2024 à 13:22, Christian Marangi a écrit :

Quoted 7 lines

Add Airoha AN8855 5-Port Gigabit DSA switch.

The switch is also a nvmem-provider as it does have EFUSE to calibrate the internal PHYs.

---

Hi,

...

drivers/net/dsa/an8855.c line 18

Quoted 12 lines
drivers/net/dsa/an8855.c new file +12 -0
+#include <linux/bitfield.h>
+#include <linux/ethtool.h>
+#include <linux/etherdevice.h>
+#include <linux/gpio/consumer.h>
+#include <linux/if_bridge.h>
+#include <linux/iopoll.h>
+#include <linux/mdio.h>
+#include <linux/netdevice.h>
+#include <linux/of_mdio.h>
+#include <linux/of_net.h>
+#include <linux/of_platform.h>
+#include <linux/nvmem-provider.h>

Could be moved a few lines above to keep order.

Quoted 3 lines
drivers/net/dsa/an8855.c new file +3 -0
+#include <linux/phylink.h>
+#include <linux/regmap.h>
+#include <net/dsa.h>

...

drivers/net/dsa/an8855.c line 495

Quoted 5 lines
drivers/net/dsa/an8855.c new file +5 -0
+static int an8855_port_fdb_dump(struct dsa_switch *ds, int port,
+				dsa_fdb_dump_cb_t *cb, void *data)
+{
+	struct an8855_priv *priv = ds->priv;
+	struct an8855_fdb _fdb = {  };

Should it but reseted in the do loop below, instead of only once here?

Quoted 62 lines
drivers/net/dsa/an8855.c new file +62 -0
+	int banks, count = 0;
+	u32 rsp;
+	int ret;
+	int i;
+
+	mutex_lock(&priv->reg_mutex);
+
+	/* Load search port */
+	ret = regmap_write(priv->regmap, AN8855_ATWD2,
+			   FIELD_PREP(AN8855_ATWD2_PORT, port));
+	if (ret)
+		goto exit;
+	ret = an8855_fdb_cmd(priv, AN8855_ATC_MAT(AND8855_FDB_MAT_MAC_PORT) |
+			     AN8855_FDB_START, &rsp);
+	if (ret < 0)
+		goto exit;
+
+	do {
+		/* From response get the number of banks to read, exit if 0 */
+		banks = FIELD_GET(AN8855_ATC_HIT, rsp);
+		if (!banks)
+			break;
+
+		/* Each banks have 4 entry */
+		for (i = 0; i < 4; i++) {
+			count++;
+
+			/* Check if bank is present */
+			if (!(banks & BIT(i)))
+				continue;
+
+			/* Select bank entry index */
+			ret = regmap_write(priv->regmap, AN8855_ATRDS,
+					   FIELD_PREP(AN8855_ATRD_SEL, i));
+			if (ret)
+				break;
+			/* wait 1ms for the bank entry to be filled */
+			usleep_range(1000, 1500);
+			an8855_fdb_read(priv, &_fdb);
+
+			if (!_fdb.live)
+				continue;
+			ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp, data);
+			if (ret < 0)
+				break;
+		}
+
+		/* Stop if reached max FDB number */
+		if (count >= AN8855_NUM_FDB_RECORDS)
+			break;
+
+		/* Read next bank */
+		ret = an8855_fdb_cmd(priv, AN8855_ATC_MAT(AND8855_FDB_MAT_MAC_PORT) |
+				     AN8855_FDB_NEXT, &rsp);
+		if (ret < 0)
+			break;
+	} while (true);
+
+exit:
+	mutex_unlock(&priv->reg_mutex);
+	return ret;
+}

...

drivers/net/dsa/an8855.c line 1813

Quoted 12 lines
drivers/net/dsa/an8855.c new file +12 -0
+	ret = regmap_set_bits(priv->regmap, AN8855_RG_RATE_ADAPT_CTRL_0,
+			      AN8855_RG_RATE_ADAPT_RX_BYPASS |
+			      AN8855_RG_RATE_ADAPT_TX_BYPASS |
+			      AN8855_RG_RATE_ADAPT_RX_EN |
+			      AN8855_RG_RATE_ADAPT_TX_EN);
+	if (ret)
+		return ret;
+
+	/* Disable AN if not in autoneg */
+	ret = regmap_update_bits(priv->regmap, AN8855_SGMII_REG_AN0, BMCR_ANENABLE,
+				 neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED ? BMCR_ANENABLE :
+									      0);

Should 'ret' be tested here?

Quoted 8 lines
drivers/net/dsa/an8855.c new file +8 -0
+
+	if (interface == PHY_INTERFACE_MODE_SGMII &&
+	    neg_mode == PHYLINK_PCS_NEG_INBAND_DISABLED) {
+		ret = regmap_set_bits(priv->regmap, AN8855_PHY_RX_FORCE_CTRL_0,
+				      AN8855_RG_FORCE_TXC_SEL);
+		if (ret)
+			return ret;
+	}

...

drivers/net/dsa/an8855.c line 2010

Quoted 9 lines
drivers/net/dsa/an8855.c new file +9 -0
+	priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
+	if (!priv->ds)
+		return -ENOMEM;
+
+	priv->ds->dev = &mdiodev->dev;
+	priv->ds->num_ports = AN8855_NUM_PORTS;
+	priv->ds->priv = priv;
+	priv->ds->ops = &an8855_switch_ops;
+	mutex_init(&priv->reg_mutex);

devm_mutex_init() to slightly simplify the remove function?

Quoted 17 lines
drivers/net/dsa/an8855.c new file +17 -0
+	priv->ds->phylink_mac_ops = &an8855_phylink_mac_ops;
+
+	priv->pcs.ops = &an8855_pcs_ops;
+	priv->pcs.neg_mode = true;
+	priv->pcs.poll = true;
+
+	ret = an8855_sw_register_nvmem(priv);
+	if (ret)
+		return ret;
+
+	dev_set_drvdata(&mdiodev->dev, priv);
+
+	return dsa_register_switch(priv->ds);
+}
+
+static void
+an8855_sw_remove(struct mdio_device *mdiodev)

drivers/net/dsa/an8855.c line 2037

Quoted 10 lines
drivers/net/dsa/an8855.c new file +10 -0
+{
+	struct an8855_priv *priv = dev_get_drvdata(&mdiodev->dev);
+
+	dsa_unregister_switch(priv->ds);
+	mutex_destroy(&priv->reg_mutex);
+}
+
+static const struct of_device_id an8855_of_match[] = {
+	{ .compatible = "airoha,an8855" },
+	{ /* sentinel */ },

Ending comma is usually not needed after a terminator.

Quoted 10 lines
drivers/net/dsa/an8855.c new file +10 -0
+};
+
+static struct mdio_driver an8855_mdio_driver = {
+	.probe = an8855_sw_probe,
+	.remove = an8855_sw_remove,
+	.mdiodrv.driver = {
+		.name = "an8855",
+		.of_match_table = an8855_of_match,
+	},
+};

...

drivers/net/dsa/an8855.h line 232

Quoted 7 lines
drivers/net/dsa/an8855.h new file +7 -0
+#define	  AN8855_VA0_VTAG_EN		BIT(10) /* Per VLAN Egress Tag Control */
+#define	  AN8855_VA0_IVL_MAC		BIT(5) /* Independent VLAN Learning */
+#define	  AN8855_VA0_VLAN_VALID		BIT(0) /* VLAN Entry Valid */
+#define AN8855_VAWD1			0x10200608
+#define	  AN8855_VA1_PORT_STAG		BIT(1)
+
+/* Same register map of VAWD0 */

Not sure to follow. AN8855_VAWD0 above is 0x10200604, not 0x10200618

Quoted 6 lines
drivers/net/dsa/an8855.h new file +6 -0
+#define AN8855_VARD0			0x10200618
+
+enum an8855_vlan_egress_attr {
+	AN8855_VLAN_EGRESS_UNTAG = 0,
+	AN8855_VLAN_EGRESS_TAG = 2,
+	AN8855_VLAN_EGRESS_STACK = 3,

drivers/net/dsa/an8855.h line 248

Quoted 10 lines
drivers/net/dsa/an8855.h new file +10 -0
+};
+
+/* Register for port STP state control */
+#define AN8855_SSP_P(x)			(0x10208000 + ((x) * 0x200))
+#define	 AN8855_FID_PST			GENMASK(1, 0)
+
+enum an8855_stp_state {
+	AN8855_STP_DISABLED = 0,
+	AN8855_STP_BLOCKING = 1,
+	AN8855_STP_LISTENING = 1,

Just wondering if this 0, 1, *1*, 2, 3 was intentional?

Quoted 17 lines
drivers/net/dsa/an8855.h new file +17 -0
+	AN8855_STP_LEARNING = 2,
+	AN8855_STP_FORWARDING = 3
+};
+
+/* Register for port control */
+#define AN8855_PCR_P(x)			(0x10208004 + ((x) * 0x200))
+#define	  AN8855_EG_TAG			GENMASK(29, 28)
+#define	  AN8855_PORT_PRI		GENMASK(26, 24)
+#define	  AN8855_PORT_TX_MIR		BIT(20)
+#define	  AN8855_PORT_RX_MIR		BIT(16)
+#define	  AN8855_PORT_VLAN		GENMASK(1, 0)
+
+enum an8855_port_mode {
+	/* Port Matrix Mode: Frames are forwarded by the PCR_MATRIX members. */
+	AN8855_PORT_MATRIX_MODE = 0,
+
+	/* Fallback Mode: Forward received frames with ingress ports that do

drivers/net/dsa/an8855.h line 275

Quoted 10 lines
drivers/net/dsa/an8855.h new file +10 -0
+	 * not belong to the VLAN member. Frames whose VID is not listed on
+	 * the VLAN table are forwarded by the PCR_MATRIX members.
+	 */
+	AN8855_PORT_FALLBACK_MODE = 1,
+
+	/* Check Mode: Forward received frames whose ingress do not
+	 * belong to the VLAN member. Discard frames if VID ismiddes on the
+	 * VLAN table.
+	 */
+	AN8855_PORT_CHECK_MODE = 1,

Just wondering if this 0, 1, *1*, 3 was intentional?

drivers/net/dsa/an8855.h line 281

Quoted 6 lines
drivers/net/dsa/an8855.h new file +6 -0
+
+	/* Security Mode: Discard any frame due to ingress membership
+	 * violation or VID missed on the VLAN table.
+	 */
+	AN8855_PORT_SECURITY_MODE = 3,
+};

...

CJ

maintainer
#4 lore raw

Re: [net-next PATCH v3 2/3] net: dsa: Add Airoha AN8855 5-Port Gigabit DSA Switch driver

Quoted 6 lines

drivers/net/dsa/an8855.h line 248

Quoted 4 lines
drivers/net/dsa/an8855.h new file +4 -0
+enum an8855_stp_state {
+	AN8855_STP_DISABLED = 0,
+	AN8855_STP_BLOCKING = 1,
+	AN8855_STP_LISTENING = 1,

Just wondering if this 0, 1, *1*, 2, 3 was intentional?

There are some devices which don't differentiate between blocking and listening. If this is true, then maybe:

+enum an8855_stp_state {
+ AN8855_STP_DISABLED = 0,
+ AN8855_STP_BLOCKING = 1,
+ AN8855_STP_LISTENING = AN8855_STP_BLOCKING,

Which saves a comment.

Andrew

#5 lore raw

Re: [net-next PATCH v3 2/3] net: dsa: Add Airoha AN8855 5-Port Gigabit DSA Switch driver

In reply to Christophe JAILLET
Quoted 33 lines · Christophe JAILLET

Le 06/11/2024 à 13:22, Christian Marangi a écrit :

Quoted 7 lines

Add Airoha AN8855 5-Port Gigabit DSA switch.

The switch is also a nvmem-provider as it does have EFUSE to calibrate the internal PHYs.

---

Hi,

...

drivers/net/dsa/an8855.c line 18

Quoted 12 lines
drivers/net/dsa/an8855.c new file +12 -0
+#include <linux/bitfield.h>
+#include <linux/ethtool.h>
+#include <linux/etherdevice.h>
+#include <linux/gpio/consumer.h>
+#include <linux/if_bridge.h>
+#include <linux/iopoll.h>
+#include <linux/mdio.h>
+#include <linux/netdevice.h>
+#include <linux/of_mdio.h>
+#include <linux/of_net.h>
+#include <linux/of_platform.h>
+#include <linux/nvmem-provider.h>

Could be moved a few lines above to keep order.

Quoted 3 lines
drivers/net/dsa/an8855.c new file +3 -0
+#include <linux/phylink.h>
+#include <linux/regmap.h>
+#include <net/dsa.h>

...

Quoted 8 lines

drivers/net/dsa/an8855.c line 495

Quoted 5 lines
drivers/net/dsa/an8855.c new file +5 -0
+static int an8855_port_fdb_dump(struct dsa_switch *ds, int port,
+				dsa_fdb_dump_cb_t *cb, void *data)
+{
+	struct an8855_priv *priv = ds->priv;
+	struct an8855_fdb _fdb = {  };

Should it but reseted in the do loop below, instead of only once here?

Common practice is to fill EVERY value in _fdb to not have to reset, but yes just as extra caution, I will move the define in the for loop.

Quoted 65 lines
Quoted 62 lines
drivers/net/dsa/an8855.c new file +62 -0
+	int banks, count = 0;
+	u32 rsp;
+	int ret;
+	int i;
+
+	mutex_lock(&priv->reg_mutex);
+
+	/* Load search port */
+	ret = regmap_write(priv->regmap, AN8855_ATWD2,
+			   FIELD_PREP(AN8855_ATWD2_PORT, port));
+	if (ret)
+		goto exit;
+	ret = an8855_fdb_cmd(priv, AN8855_ATC_MAT(AND8855_FDB_MAT_MAC_PORT) |
+			     AN8855_FDB_START, &rsp);
+	if (ret < 0)
+		goto exit;
+
+	do {
+		/* From response get the number of banks to read, exit if 0 */
+		banks = FIELD_GET(AN8855_ATC_HIT, rsp);
+		if (!banks)
+			break;
+
+		/* Each banks have 4 entry */
+		for (i = 0; i < 4; i++) {
+			count++;
+
+			/* Check if bank is present */
+			if (!(banks & BIT(i)))
+				continue;
+
+			/* Select bank entry index */
+			ret = regmap_write(priv->regmap, AN8855_ATRDS,
+					   FIELD_PREP(AN8855_ATRD_SEL, i));
+			if (ret)
+				break;
+			/* wait 1ms for the bank entry to be filled */
+			usleep_range(1000, 1500);
+			an8855_fdb_read(priv, &_fdb);
+
+			if (!_fdb.live)
+				continue;
+			ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp, data);
+			if (ret < 0)
+				break;
+		}
+
+		/* Stop if reached max FDB number */
+		if (count >= AN8855_NUM_FDB_RECORDS)
+			break;
+
+		/* Read next bank */
+		ret = an8855_fdb_cmd(priv, AN8855_ATC_MAT(AND8855_FDB_MAT_MAC_PORT) |
+				     AN8855_FDB_NEXT, &rsp);
+		if (ret < 0)
+			break;
+	} while (true);
+
+exit:
+	mutex_unlock(&priv->reg_mutex);
+	return ret;
+}

...

Quoted 15 lines

drivers/net/dsa/an8855.c line 1813

Quoted 12 lines
drivers/net/dsa/an8855.c new file +12 -0
+	ret = regmap_set_bits(priv->regmap, AN8855_RG_RATE_ADAPT_CTRL_0,
+			      AN8855_RG_RATE_ADAPT_RX_BYPASS |
+			      AN8855_RG_RATE_ADAPT_TX_BYPASS |
+			      AN8855_RG_RATE_ADAPT_RX_EN |
+			      AN8855_RG_RATE_ADAPT_TX_EN);
+	if (ret)
+		return ret;
+
+	/* Disable AN if not in autoneg */
+	ret = regmap_update_bits(priv->regmap, AN8855_SGMII_REG_AN0, BMCR_ANENABLE,
+				 neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED ? BMCR_ANENABLE :
+									      0);

Should 'ret' be tested here?

Sorry forgot to add.

Quoted 11 lines
Quoted 8 lines
drivers/net/dsa/an8855.c new file +8 -0
+
+	if (interface == PHY_INTERFACE_MODE_SGMII &&
+	    neg_mode == PHYLINK_PCS_NEG_INBAND_DISABLED) {
+		ret = regmap_set_bits(priv->regmap, AN8855_PHY_RX_FORCE_CTRL_0,
+				      AN8855_RG_FORCE_TXC_SEL);
+		if (ret)
+			return ret;
+	}

...

Quoted 12 lines

drivers/net/dsa/an8855.c line 2010

Quoted 9 lines
drivers/net/dsa/an8855.c new file +9 -0
+	priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
+	if (!priv->ds)
+		return -ENOMEM;
+
+	priv->ds->dev = &mdiodev->dev;
+	priv->ds->num_ports = AN8855_NUM_PORTS;
+	priv->ds->priv = priv;
+	priv->ds->ops = &an8855_switch_ops;
+	mutex_init(&priv->reg_mutex);

devm_mutex_init() to slightly simplify the remove function?

Wonder if there is a variant also for dsa_unregister_switch. That would effectively remove the need for a remove function.

Quoted 43 lines
Quoted 17 lines
drivers/net/dsa/an8855.c new file +17 -0
+	priv->ds->phylink_mac_ops = &an8855_phylink_mac_ops;
+
+	priv->pcs.ops = &an8855_pcs_ops;
+	priv->pcs.neg_mode = true;
+	priv->pcs.poll = true;
+
+	ret = an8855_sw_register_nvmem(priv);
+	if (ret)
+		return ret;
+
+	dev_set_drvdata(&mdiodev->dev, priv);
+
+	return dsa_register_switch(priv->ds);
+}
+
+static void
+an8855_sw_remove(struct mdio_device *mdiodev)

drivers/net/dsa/an8855.c line 2037

Quoted 10 lines
drivers/net/dsa/an8855.c new file +10 -0
+{
+	struct an8855_priv *priv = dev_get_drvdata(&mdiodev->dev);
+
+	dsa_unregister_switch(priv->ds);
+	mutex_destroy(&priv->reg_mutex);
+}
+
+static const struct of_device_id an8855_of_match[] = {
+	{ .compatible = "airoha,an8855" },
+	{ /* sentinel */ },

Ending comma is usually not needed after a terminator.

Quoted 10 lines
drivers/net/dsa/an8855.c new file +10 -0
+};
+
+static struct mdio_driver an8855_mdio_driver = {
+	.probe = an8855_sw_probe,
+	.remove = an8855_sw_remove,
+	.mdiodrv.driver = {
+		.name = "an8855",
+		.of_match_table = an8855_of_match,
+	},
+};

...

Quoted 10 lines

drivers/net/dsa/an8855.h line 232

Quoted 7 lines
drivers/net/dsa/an8855.h new file +7 -0
+#define	  AN8855_VA0_VTAG_EN		BIT(10) /* Per VLAN Egress Tag Control */
+#define	  AN8855_VA0_IVL_MAC		BIT(5) /* Independent VLAN Learning */
+#define	  AN8855_VA0_VLAN_VALID		BIT(0) /* VLAN Entry Valid */
+#define AN8855_VAWD1			0x10200608
+#define	  AN8855_VA1_PORT_STAG		BIT(1)
+
+/* Same register map of VAWD0 */

Not sure to follow. AN8855_VAWD0 above is 0x10200604, not 0x10200618

Confusing comment. The meaning here is not "same register" but same register fields aka bits and mask for the register are the same of ..604.

Quoted 10 lines
Quoted 10 lines
drivers/net/dsa/an8855.h new file +10 -0
+#define AN8855_VARD0			0x10200618
+
+enum an8855_vlan_egress_attr {
+	AN8855_VLAN_EGRESS_UNTAG = 0,
+	AN8855_VLAN_EGRESS_TAG = 2,
+	AN8855_VLAN_EGRESS_STACK = 3,
+};
+
+/* Register for port STP state control */
+#define AN8855_SSP_P(x)			(0x10208000 + ((x) * 0x200))
Quoted 9 lines

drivers/net/dsa/an8855.h line 248

Quoted 6 lines
drivers/net/dsa/an8855.h new file +6 -0
+#define	 AN8855_FID_PST			GENMASK(1, 0)
+
+enum an8855_stp_state {
+	AN8855_STP_DISABLED = 0,
+	AN8855_STP_BLOCKING = 1,
+	AN8855_STP_LISTENING = 1,

Just wondering if this 0, 1, *1*, 2, 3 was intentional?

Yes blocking and listening is the same, I will follow suggestion by Andrew and make blocking = listening.

Quoted 21 lines
Quoted 21 lines
drivers/net/dsa/an8855.h new file +21 -0
+	AN8855_STP_LEARNING = 2,
+	AN8855_STP_FORWARDING = 3
+};
+
+/* Register for port control */
+#define AN8855_PCR_P(x)			(0x10208004 + ((x) * 0x200))
+#define	  AN8855_EG_TAG			GENMASK(29, 28)
+#define	  AN8855_PORT_PRI		GENMASK(26, 24)
+#define	  AN8855_PORT_TX_MIR		BIT(20)
+#define	  AN8855_PORT_RX_MIR		BIT(16)
+#define	  AN8855_PORT_VLAN		GENMASK(1, 0)
+
+enum an8855_port_mode {
+	/* Port Matrix Mode: Frames are forwarded by the PCR_MATRIX members. */
+	AN8855_PORT_MATRIX_MODE = 0,
+
+	/* Fallback Mode: Forward received frames with ingress ports that do
+	 * not belong to the VLAN member. Frames whose VID is not listed on
+	 * the VLAN table are forwarded by the PCR_MATRIX members.
+	 */
+	AN8855_PORT_FALLBACK_MODE = 1,
Quoted 9 lines

drivers/net/dsa/an8855.h line 275

Quoted 6 lines
drivers/net/dsa/an8855.h new file +6 -0
+
+	/* Check Mode: Forward received frames whose ingress do not
+	 * belong to the VLAN member. Discard frames if VID ismiddes on the
+	 * VLAN table.
+	 */
+	AN8855_PORT_CHECK_MODE = 1,

Just wondering if this 0, 1, *1*, 3 was intentional?

Nope a typo. Thanks for noticing this.

Quoted 9 lines

drivers/net/dsa/an8855.h line 281

Quoted 6 lines
drivers/net/dsa/an8855.h new file +6 -0
+
+	/* Security Mode: Discard any frame due to ingress membership
+	 * violation or VID missed on the VLAN table.
+	 */
+	AN8855_PORT_SECURITY_MODE = 3,
+};

...

CJ

Signature · 1 line
	Ansuel
#6 lore raw

Re: [net-next PATCH v3 2/3] net: dsa: Add Airoha AN8855 5-Port Gigabit DSA Switch driver

In reply to Christian Marangi

drivers/net/dsa/an8855.c line 936

Quoted 11 lines · Christian Marangi
drivers/net/dsa/an8855.c new file +11 -0
+static int an8855_port_enable(struct dsa_switch *ds, int port,
+			      struct phy_device *phy)
+{
+	int ret;
+
+	ret = an8855_port_set_status(ds->priv, port, true);
+	if (ret)
+		return ret;
+
+	if (dsa_is_user_port(ds, port))
+		phy_support_asym_pause(phy);

This is unnecessary if you've set the phylink capabilities correctly because phylink_bringup_phy() will setup the PHY accordingly.

drivers/net/dsa/an8855.c line 1029

Quoted 10 lines
drivers/net/dsa/an8855.c new file +10 -0
+static u32 en8855_get_phy_flags(struct dsa_switch *ds, int port)
+{
+	struct an8855_priv *priv = ds->priv;
+
+	/* PHY doesn't need calibration */
+	if (!priv->phy_require_calib)
+		return 0;
+
+	/* Use BIT(0) to signal calibration needed */
+	return BIT(0);

This should be a #define somewhere that preferably includes the PHY name so we have an idea in future which PHY driver is going to be making use of this random bit.

Quoted 30 lines
drivers/net/dsa/an8855.c new file +30 -0
+static struct phylink_pcs *
+an8855_phylink_mac_select_pcs(struct phylink_config *config,
+			      phy_interface_t interface)
+{
+	struct dsa_port *dp = dsa_phylink_to_port(config);
+	struct an8855_priv *priv = dp->ds->priv;
+
+	switch (interface) {
+	case PHY_INTERFACE_MODE_SGMII:
+	case PHY_INTERFACE_MODE_2500BASEX:
+		return &priv->pcs;
+	default:
+		return NULL;
+	}
+}
+
+static void
+an8855_phylink_mac_config(struct phylink_config *config, unsigned int mode,
+			  const struct phylink_link_state *state)
+{
+	struct dsa_port *dp = dsa_phylink_to_port(config);
+	struct dsa_switch *ds = dp->ds;
+	struct an8855_priv *priv;
+	int port = dp->index;
+
+	priv = ds->priv;
+
+	switch (port) {
+	case 0:
+	case 1:

drivers/net/dsa/an8855.c line 1219

Quoted 10 lines
drivers/net/dsa/an8855.c new file +10 -0
+	case 2:
+	case 3:
+	case 4:
+		return;
+	case 5:
+		break;
+	default:
+		dev_err(ds->dev, "unsupported port: %d", port);
+		return;
+	}
	if (port != 5) {
		if (port > 5)
			dev_err(ds->dev, "unsupported port: %d", port);
		return;
	}

is simpler, no?

drivers/net/dsa/an8855.c line 1389

Quoted 12 lines
drivers/net/dsa/an8855.c new file +12 -0
+static int an8855_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
+			     phy_interface_t interface,
+			     const unsigned long *advertising,
+			     bool permit_pause_to_mac)
+{
+	struct an8855_priv *priv = container_of(pcs, struct an8855_priv, pcs);
+	u32 val;
+	int ret;
+
+	switch (interface) {
+	case PHY_INTERFACE_MODE_RGMII:
+		return 0;

This shouldn't happen. First, in an8855_phylink_mac_select_pcs(), you don't return a PCS in this mode. Second, phylink is highly unlikely to switch from SGMII/2500baseX to RGMII mode. Third, in commit 486dc391ef43 ("net: phylink: allow mac_select_pcs() to remove a PCS") will ensure that if phylink does do such a switch, because an8855_phylink_mac_select_pcs() returns NULL for RGMII, these PCS functions will never be called for RGMII modes.

Thanks.

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

PATCH v3 3/3 net: phy: Add Airoha AN8855 Internal Switch Gigabit PHY

In reply to Christian Marangi

Add support for Airoha AN8855 Internal Switch Gigabit PHY.

This is a simple PHY driver to configure and calibrate the PHY for the AN8855 Switch with the use of NVMEM cells.

---
 MAINTAINERS                  |   1 +
 drivers/net/phy/Kconfig      |   5 +
 drivers/net/phy/Makefile     |   1 +
 drivers/net/phy/air_an8855.c | 278 +++++++++++++++++++++++++++++++++++
 4 files changed, 285 insertions(+)
 create mode 100644 drivers/net/phy/air_an8855.c
MAINTAINERS +1 -0
index e3077d9feee2..cf34add2a0bb 100644
@@ -726,6 +726,7 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/net/dsa/airoha,an8855.yaml
 F:	drivers/net/dsa/an8855.c
 F:	drivers/net/dsa/an8855.h
+F:	drivers/net/phy/air_an8855.c
 
 AIROHA ETHERNET DRIVER
 M:	Lorenzo Bianconi <[email protected]>
drivers/net/phy/Kconfig +5 -0
index ee3ea0b56d48..1d474038ea7f 100644
@@ -79,6 +79,11 @@ config SFP
 
 comment "MII PHY device drivers"
 
+config AIR_AN8855_PHY
+	tristate "Airoha AN8855 Internal Gigabit PHY"
+	help
+	  Currently supports the internal Airoha AN8855 Switch PHY.
+
 config AIR_EN8811H_PHY
 	tristate "Airoha EN8811H 2.5 Gigabit PHY"
 	help
drivers/net/phy/Makefile +1 -0
index 90f886844381..baba7894785b 100644
@@ -35,6 +35,7 @@ obj-y				+= $(sfp-obj-y) $(sfp-obj-m)
 
 obj-$(CONFIG_ADIN_PHY)		+= adin.o
 obj-$(CONFIG_ADIN1100_PHY)	+= adin1100.o
+obj-$(CONFIG_AIR_AN8855_PHY)   += air_an8855.o
 obj-$(CONFIG_AIR_EN8811H_PHY)   += air_en8811h.o
 obj-$(CONFIG_AMD_PHY)		+= amd.o
 obj-$(CONFIG_AMCC_QT2025_PHY)	+= qt2025.o
drivers/net/phy/air_an8855.c new file +278 -0

278 lines not on this page · raw message

Signature · 1 line
2.45.2
#8 lore raw

Re: [net-next PATCH v3 3/3] net: phy: Add Airoha AN8855 Internal Switch Gigabit PHY

Hello Christian,

Quoted 6 lines · On Wed, 6 Nov 2024 13:22:38 +0100 Christian Marangi <[email protected]>…

Add support for Airoha AN8855 Internal Switch Gigabit PHY.

This is a simple PHY driver to configure and calibrate the PHY for the AN8855 Switch with the use of NVMEM cells.

[...]

drivers/net/phy/air_an8855.c line 139

Quoted 10 lines
drivers/net/phy/air_an8855.c new file +10 -0
+static int an8855_get_downshift(struct phy_device *phydev, u8 *data)
+{
+	int saved_page;
+	int val;
+	int ret;
+
+	saved_page = phy_select_page(phydev, AN8855_PHY_PAGE_EXTENDED_1);
+	if (saved_page >= 0)
+		val = __phy_read(phydev, AN8855_PHY_EXT_REG_14);
+	ret = phy_restore_page(phydev, saved_page, val);

I think this can be replaced with phy_read_paged()

[...]

Quoted 6 lines
drivers/net/phy/air_an8855.c new file +6 -0
+static int an8855_set_downshift(struct phy_device *phydev, u8 cnt)
+{
+	int saved_page;
+	int ret;
+
+	saved_page = phy_select_page(phydev, AN8855_PHY_PAGE_EXTENDED_1);

drivers/net/phy/air_an8855.c line 164

Quoted 10 lines
drivers/net/phy/air_an8855.c new file +10 -0
+	if (saved_page >= 0) {
+		if (cnt != DOWNSHIFT_DEV_DISABLE)
+			ret = __phy_set_bits(phydev, AN8855_PHY_EXT_REG_14,
+					     AN8855_PHY_EN_DOWN_SHFIT);
+		else
+			ret = __phy_clear_bits(phydev, AN8855_PHY_EXT_REG_14,
+					       AN8855_PHY_EN_DOWN_SHFIT);
+	}
+
+	return phy_restore_page(phydev, saved_page, ret);

And this by phy_modify_paged() :)

Thanks,

Maxime

#9 lore raw

Re: [net-next PATCH v3 3/3] net: phy: Add Airoha AN8855 Internal Switch Gigabit PHY

Quoted 39 lines · Maxime Chevallier

Hello Christian,

Quoted 6 lines · On Wed, 6 Nov 2024 13:22:38 +0100 Christian Marangi <[email protected]>…

Add support for Airoha AN8855 Internal Switch Gigabit PHY.

This is a simple PHY driver to configure and calibrate the PHY for the AN8855 Switch with the use of NVMEM cells.

[...]

drivers/net/phy/air_an8855.c line 139

Quoted 10 lines
drivers/net/phy/air_an8855.c new file +10 -0
+static int an8855_get_downshift(struct phy_device *phydev, u8 *data)
+{
+	int saved_page;
+	int val;
+	int ret;
+
+	saved_page = phy_select_page(phydev, AN8855_PHY_PAGE_EXTENDED_1);
+	if (saved_page >= 0)
+		val = __phy_read(phydev, AN8855_PHY_EXT_REG_14);
+	ret = phy_restore_page(phydev, saved_page, val);

I think this can be replaced with phy_read_paged()

[...]

Quoted 10 lines
drivers/net/phy/air_an8855.c new file +10 -0
+static int an8855_set_downshift(struct phy_device *phydev, u8 cnt)
+{
+	int saved_page;
+	int ret;
+
+	saved_page = phy_select_page(phydev, AN8855_PHY_PAGE_EXTENDED_1);
+	if (saved_page >= 0) {
+		if (cnt != DOWNSHIFT_DEV_DISABLE)
+			ret = __phy_set_bits(phydev, AN8855_PHY_EXT_REG_14,
+					     AN8855_PHY_EN_DOWN_SHFIT);
Quoted 9 lines

drivers/net/phy/air_an8855.c line 164

Quoted 6 lines
drivers/net/phy/air_an8855.c new file +6 -0
+		else
+			ret = __phy_clear_bits(phydev, AN8855_PHY_EXT_REG_14,
+					       AN8855_PHY_EN_DOWN_SHFIT);
+	}
+
+	return phy_restore_page(phydev, saved_page, ret);

And this by phy_modify_paged() :)

Didn't notice those, even better! Thanks!

Signature · 1 line
	Ansuel
maintainer
#10 lore raw

Re: [net-next PATCH v3 3/3] net: phy: Add Airoha AN8855 Internal Switch Gigabit PHY

In reply to Christian Marangi
Quoted 15 lines
drivers/net/phy/air_an8855.c new file +15 -0
+static const u8 dsa_r50ohm_table[] = {
+	127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
+	127, 127, 127, 127, 127, 127, 127, 126, 122, 117,
+	112, 109, 104, 101,  97,  94,  90,  88,  84,  80,
+	78,  74,  72,  68,  66,  64,  61,  58,  56,  53,
+	51,  48,  47,  44,  42,  40,  38,  36,  34,  32,
+	31,  28,  27,  24,  24,  22,  20,  18,  16,  16,
+	14,  12,  11,   9
+};
+
+static int en8855_get_r50ohm_val(struct device *dev, const char *calib_name,
+				 u8 *dest)
+{
+	u32 shift_sel, val;
+	int ret;

drivers/net/phy/air_an8855.c line 84

Quoted 10 lines
drivers/net/phy/air_an8855.c new file +10 -0
+	int i;
+
+	ret = nvmem_cell_read_u32(dev, calib_name, &val);
+	if (ret)
+		return ret;
+
+	shift_sel = FIELD_GET(AN8855_SWITCH_EFUSE_R50O, val);
+	for (i = 0; i < ARRAY_SIZE(dsa_r50ohm_table); i++)
+		if (dsa_r50ohm_table[i] == shift_sel)
+			break;

Is an exact match expected? Should this be >= so the nearest match is found?

Quoted 9 lines
drivers/net/phy/air_an8855.c new file +9 -0
+
+	if (i < 8 || i >= ARRAY_SIZE(dsa_r50ohm_table))
+		*dest = dsa_r50ohm_table[25];
+	else
+		*dest = dsa_r50ohm_table[i - 8];
+
+	return 0;
+}
+

drivers/net/phy/air_an8855.c line 103

Quoted 10 lines
drivers/net/phy/air_an8855.c new file +10 -0
+static int an8855_probe(struct phy_device *phydev)
+{
+	struct device *dev = &phydev->mdio.dev;
+	struct device_node *node = dev->of_node;
+	struct air_an8855_priv *priv;
+	int ret;
+
+	/* If we don't have a node, skip get calib */
+	if (!node)
+		return 0;

phydev->priv will be a NULL pointer, causing problems in an8855_config_init()

Andrew

#11 lore raw

Re: [net-next PATCH v3 3/3] net: phy: Add Airoha AN8855 Internal Switch Gigabit PHY

Quoted 20 lines · Andrew Lunn
Quoted 20 lines
drivers/net/phy/air_an8855.c new file +20 -0
+static const u8 dsa_r50ohm_table[] = {
+	127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
+	127, 127, 127, 127, 127, 127, 127, 126, 122, 117,
+	112, 109, 104, 101,  97,  94,  90,  88,  84,  80,
+	78,  74,  72,  68,  66,  64,  61,  58,  56,  53,
+	51,  48,  47,  44,  42,  40,  38,  36,  34,  32,
+	31,  28,  27,  24,  24,  22,  20,  18,  16,  16,
+	14,  12,  11,   9
+};
+
+static int en8855_get_r50ohm_val(struct device *dev, const char *calib_name,
+				 u8 *dest)
+{
+	u32 shift_sel, val;
+	int ret;
+	int i;
+
+	ret = nvmem_cell_read_u32(dev, calib_name, &val);
+	if (ret)
+		return ret;
Quoted 9 lines

drivers/net/phy/air_an8855.c line 84

Quoted 5 lines
drivers/net/phy/air_an8855.c new file +5 -0
+
+	shift_sel = FIELD_GET(AN8855_SWITCH_EFUSE_R50O, val);
+	for (i = 0; i < ARRAY_SIZE(dsa_r50ohm_table); i++)
+		if (dsa_r50ohm_table[i] == shift_sel)
+			break;

Is an exact match expected? Should this be >= so the nearest match is found?

As strange as this is, yes this is what the original code does.

Quoted 14 lines
Quoted 14 lines
drivers/net/phy/air_an8855.c new file +14 -0
+
+	if (i < 8 || i >= ARRAY_SIZE(dsa_r50ohm_table))
+		*dest = dsa_r50ohm_table[25];
+	else
+		*dest = dsa_r50ohm_table[i - 8];
+
+	return 0;
+}
+
+static int an8855_probe(struct phy_device *phydev)
+{
+	struct device *dev = &phydev->mdio.dev;
+	struct device_node *node = dev->of_node;
+	struct air_an8855_priv *priv;
Quoted 9 lines

drivers/net/phy/air_an8855.c line 103

Quoted 5 lines
drivers/net/phy/air_an8855.c new file +5 -0
+	int ret;
+
+	/* If we don't have a node, skip get calib */
+	if (!node)
+		return 0;

phydev->priv will be a NULL pointer, causing problems in an8855_config_init()

Quite unlikely scenario since for the switch, defining the internal PHY in an MDIO node is mandatory but yes it's a fragility.

2 solution:
- I check priv in config_init and skip that section
- I always set phydev->priv

Solution 1 is safer (handle case where for some reason en8855_get_r50ohm_val fails (it's really almost impossible)) but error prone if the PHY gets extended with other parts and priv starts to gets used for other thing.

Solution 2 require an extra bool to signal full calibrarion read and is waste more resource (in case calib is not needed...)

Anyway thanks for the review!

Signature · 1 line
	Ansuel
#12 lore raw

Re: [net-next PATCH v3 3/3] net: phy: Add Airoha AN8855 Internal Switch Gigabit PHY

In reply to Christian Marangi
kernel test robot noticed the following build warnings:

Hi Christian,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Christian-Marangi/dt-bindings-net-dsa-Add-Airoha-AN8855-Gigabit-Switch-documentation/20241106-203624
base:   net-next/main
patch link:    https://lore.kernel.org/r/20241106122254.13228-4-ansuelsmth%40gmail.com
patch subject: [net-next PATCH v3 3/3] net: phy: Add Airoha AN8855 Internal Switch Gigabit PHY
config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20241107/[email protected]/config)
compiler: clang version 19.1.3 (https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b254b6afab99)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241107/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from drivers/net/phy/air_an8855.c:6:
In file included from include/linux/phy.h:16:
In file included from include/linux/ethtool.h:18:
In file included from include/linux/if_ether.h:19:
In file included from include/linux/skbuff.h:17:
In file included from include/linux/bvec.h:10:
In file included from include/linux/highmem.h:8:
In file included from include/linux/cacheflush.h:5:
In file included from arch/x86/include/asm/cacheflush.h:5:
In file included from include/linux/mm.h:2213:
include/linux/vmstat.h:504:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

     504 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     505 |                            item];
         |                            ~~~~
   include/linux/vmstat.h:511:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     511 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     512 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   include/linux/vmstat.h:524:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     524 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     525 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
>> drivers/net/phy/air_an8855.c:137:6: warning: variable 'val' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
     137 |         if (saved_page >= 0)
         |             ^~~~~~~~~~~~~~~
   drivers/net/phy/air_an8855.c:139:45: note: uninitialized use occurs here
     139 |         ret = phy_restore_page(phydev, saved_page, val);
         |                                                    ^~~
   drivers/net/phy/air_an8855.c:137:2: note: remove the 'if' if its condition is always true
     137 |         if (saved_page >= 0)
         |         ^~~~~~~~~~~~~~~~~~~~
     138 |                 val = __phy_read(phydev, AN8855_PHY_EXT_REG_14);
   drivers/net/phy/air_an8855.c:133:9: note: initialize the variable 'val' to silence this warning
     133 |         int val;
         |                ^
         |                 = 0
>> drivers/net/phy/air_an8855.c:155:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
     155 |         if (saved_page >= 0) {
         |             ^~~~~~~~~~~~~~~
   drivers/net/phy/air_an8855.c:164:46: note: uninitialized use occurs here
     164 |         return phy_restore_page(phydev, saved_page, ret);
         |                                                     ^~~
   drivers/net/phy/air_an8855.c:155:2: note: remove the 'if' if its condition is always true
     155 |         if (saved_page >= 0) {
         |         ^~~~~~~~~~~~~~~~~~~~
   drivers/net/phy/air_an8855.c:152:9: note: initialize the variable 'ret' to silence this warning
     152 |         int ret;
         |                ^
         |                 = 0

6 warnings generated.

vim +137 drivers/net/phy/air_an8855.c

129
130 static int an8855_get_downshift(struct phy_device *phydev, u8 *data)
131 {
132 int saved_page;
133 int val;
134 int ret;
135
136 saved_page = phy_select_page(phydev, AN8855_PHY_PAGE_EXTENDED_1);

 > 137		if (saved_page >= 0)
   138			val = __phy_read(phydev, AN8855_PHY_EXT_REG_14);
   139		ret = phy_restore_page(phydev, saved_page, val);
   140		if (ret)
   141			return ret;
   142	
   143		*data = val & AN8855_PHY_EXT_REG_14 ? DOWNSHIFT_DEV_DEFAULT_COUNT :
   144						      DOWNSHIFT_DEV_DISABLE;
   145	
   146		return 0;
   147	}
   148	
   149	static int an8855_set_downshift(struct phy_device *phydev, u8 cnt)
   150	{
   151		int saved_page;
   152		int ret;
   153	
   154		saved_page = phy_select_page(phydev, AN8855_PHY_PAGE_EXTENDED_1);
 > 155		if (saved_page >= 0) {

156 if (cnt != DOWNSHIFT_DEV_DISABLE)
157 ret = __phy_set_bits(phydev, AN8855_PHY_EXT_REG_14,
158 AN8855_PHY_EN_DOWN_SHFIT);
159 else
160 ret = __phy_clear_bits(phydev, AN8855_PHY_EXT_REG_14,
161 AN8855_PHY_EN_DOWN_SHFIT);
162 }
163
164 return phy_restore_page(phydev, saved_page, ret);
165 }
166

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#13 lore raw

Re: [net-next PATCH v3 3/3] net: phy: Add Airoha AN8855 Internal Switch Gigabit PHY

In reply to Christian Marangi

drivers/net/phy/air_an8855.c line 19

Quoted 3 lines · Christian Marangi
drivers/net/phy/air_an8855.c new file +3 -0
+/* MII Registers Page 1 */
+#define AN8855_PHY_EXT_REG_14			0x14
+#define   AN8855_PHY_EN_DOWN_SHFIT		BIT(4)

Shouldn't "AN8855_PHY_EN_DOWN_SHFIT" be "AN8855_PHY_EN_DOWN_SHIFT" (notice the I and F are swapped) ?

drivers/net/phy/air_an8855.c line 141

Quoted 12 lines
drivers/net/phy/air_an8855.c new file +12 -0
+static int an8855_get_downshift(struct phy_device *phydev, u8 *data)
+{
+	int saved_page;
+	int val;
+	int ret;
+
+	saved_page = phy_select_page(phydev, AN8855_PHY_PAGE_EXTENDED_1);
+	if (saved_page >= 0)
+		val = __phy_read(phydev, AN8855_PHY_EXT_REG_14);
+	ret = phy_restore_page(phydev, saved_page, val);
+	if (ret)
+		return ret;

This function is entirely broken.

phy_restore_page() will return "val" if everything went successfully, so here you end up returning "val" via this very return statement without executing any further code in the function. The only time further code will be executed is if "val" was successfully read as zero.

Please use the helpers provided:

	ret = phy_read_paged(phydev, AN8855_PHY_PAGE_EXTENDED_1,
			     AN8855_PHY_EXT_REG_14);
	if (ret < 0)
		return ret;

ret now contains what you're using as "val" below. No need to open code phy_read_paged().

drivers/net/phy/air_an8855.c line 144

Quoted 3 lines
drivers/net/phy/air_an8855.c new file +3 -0
+
+	*data = val & AN8855_PHY_EXT_REG_14 ? DOWNSHIFT_DEV_DEFAULT_COUNT :
+					      DOWNSHIFT_DEV_DISABLE;

Here, the test is against the register number rather than the bit that controls downshift. Shouldn't AN8855_PHY_EXT_REG_14 be AN8855_PHY_EN_DOWN_SH(F)I(F)T ?

Quoted 6 lines
drivers/net/phy/air_an8855.c new file +6 -0
+static int an8855_set_downshift(struct phy_device *phydev, u8 cnt)
+{
+	int saved_page;
+	int ret;
+
+	saved_page = phy_select_page(phydev, AN8855_PHY_PAGE_EXTENDED_1);

drivers/net/phy/air_an8855.c line 164

Quoted 10 lines
drivers/net/phy/air_an8855.c new file +10 -0
+	if (saved_page >= 0) {
+		if (cnt != DOWNSHIFT_DEV_DISABLE)
+			ret = __phy_set_bits(phydev, AN8855_PHY_EXT_REG_14,
+					     AN8855_PHY_EN_DOWN_SHFIT);
+		else
+			ret = __phy_clear_bits(phydev, AN8855_PHY_EXT_REG_14,
+					       AN8855_PHY_EN_DOWN_SHFIT);
+	}
+
+	return phy_restore_page(phydev, saved_page, ret);

This entire thing can be simplified to:

	u16 ds = cnt != DOWNSHIFT_DEV_DISABLE ? AN8855_PHY_EN_DOWN_SHFIT: 0;

	return phy_modify_paged(phydev, AN8855_PHY_PAGE_EXTENDED_1,
				AN8855_PHY_EXT_REG_14, AN8855_PHY_EN_DOWN_SHFIT,
				ds);

Thanks.

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

Re: [net-next PATCH v3 3/3] net: phy: Add Airoha AN8855 Internal Switch Gigabit PHY

Quoted 8 lines · Russell King (Oracle)

drivers/net/phy/air_an8855.c line 19

Quoted 3 lines · Christian Marangi
drivers/net/phy/air_an8855.c new file +3 -0
+/* MII Registers Page 1 */
+#define AN8855_PHY_EXT_REG_14			0x14
+#define   AN8855_PHY_EN_DOWN_SHFIT		BIT(4)

Shouldn't "AN8855_PHY_EN_DOWN_SHFIT" be "AN8855_PHY_EN_DOWN_SHIFT" (notice the I and F are swapped) ?

Typo from SDK that I didn't notice fun.

Quoted 23 lines

drivers/net/phy/air_an8855.c line 141

Quoted 12 lines
drivers/net/phy/air_an8855.c new file +12 -0
+static int an8855_get_downshift(struct phy_device *phydev, u8 *data)
+{
+	int saved_page;
+	int val;
+	int ret;
+
+	saved_page = phy_select_page(phydev, AN8855_PHY_PAGE_EXTENDED_1);
+	if (saved_page >= 0)
+		val = __phy_read(phydev, AN8855_PHY_EXT_REG_14);
+	ret = phy_restore_page(phydev, saved_page, val);
+	if (ret)
+		return ret;

This function is entirely broken.

phy_restore_page() will return "val" if everything went successfully, so here you end up returning "val" via this very return statement without executing any further code in the function. The only time further code will be executed is if "val" was successfully read as zero.

Please use the helpers provided:

Quoted 7 lines
	ret = phy_read_paged(phydev, AN8855_PHY_PAGE_EXTENDED_1,
			     AN8855_PHY_EXT_REG_14);
	if (ret < 0)
		return ret;

ret now contains what you're using as "val" below. No need to open code phy_read_paged().

Thanks for the explaination, totally got confused by reading the restore_page code. Anyway yes I will use the helper.

Quoted 8 lines

drivers/net/phy/air_an8855.c line 144

Quoted 4 lines
drivers/net/phy/air_an8855.c new file +3 -0
+
+	*data = val & AN8855_PHY_EXT_REG_14 ? DOWNSHIFT_DEV_DEFAULT_COUNT :
+					      DOWNSHIFT_DEV_DISABLE;

Here, the test is against the register number rather than the bit that controls downshift. Shouldn't AN8855_PHY_EXT_REG_14 be AN8855_PHY_EN_DOWN_SH(F)I(F)T ?

Copy paste error, was already staged to fix, thanks for extra eye on this.

Quoted 20 lines
Quoted 7 lines
drivers/net/phy/air_an8855.c new file +6 -0
+static int an8855_set_downshift(struct phy_device *phydev, u8 cnt)
+{
+	int saved_page;
+	int ret;
+
+	saved_page = phy_select_page(phydev, AN8855_PHY_PAGE_EXTENDED_1);

drivers/net/phy/air_an8855.c line 164

Quoted 10 lines
drivers/net/phy/air_an8855.c new file +10 -0
+	if (saved_page >= 0) {
+		if (cnt != DOWNSHIFT_DEV_DISABLE)
+			ret = __phy_set_bits(phydev, AN8855_PHY_EXT_REG_14,
+					     AN8855_PHY_EN_DOWN_SHFIT);
+		else
+			ret = __phy_clear_bits(phydev, AN8855_PHY_EXT_REG_14,
+					       AN8855_PHY_EN_DOWN_SHFIT);
+	}
+
+	return phy_restore_page(phydev, saved_page, ret);

This entire thing can be simplified to:

Quoted 5 lines
	u16 ds = cnt != DOWNSHIFT_DEV_DISABLE ? AN8855_PHY_EN_DOWN_SHFIT: 0;

	return phy_modify_paged(phydev, AN8855_PHY_PAGE_EXTENDED_1,
				AN8855_PHY_EXT_REG_14, AN8855_PHY_EN_DOWN_SHFIT,
				ds);

Funnly in rechecking I produced the same exact change.

Quoted 2 lines

Thanks.

Thanks to you for the review.

Quoted 4 lines
Signature · 3 lines
-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Signature · 1 line
	Ansuel