Skip to contentSkip to conversation

net: dsa: add a dsa_port_is_enabled helper function

PATCH In review Vladimir Oltean 3 participants 5 messages Mar 19, 2020

Subsystems (4)
lore
Share & tools
lore

Vladimir Oltean

author maintainer
#0 lore raw

PATCH 1/2 net: dsa: add a dsa_port_is_enabled helper function

From: Vladimir Oltean <[email protected]>

Sometimes drivers need to do per-port operation outside the port DSA methods, and in that case they typically iterate through their port list themselves.

Give them an aid to skip ports that are disabled in the device tree (which the DSA core already skips).

---
 include/net/dsa.h |  2 ++
 net/dsa/dsa2.c    | 29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
include/net/dsa.h +2 -0
index beeb81a532e3..813792e6f0be 100644
@@ -376,6 +376,8 @@ static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
 		return dp->vlan_filtering;
 }
 
+bool dsa_port_is_enabled(struct dsa_switch *ds, int port);
+
 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
 			      bool is_static, void *data);
 struct dsa_switch_ops {
net/dsa/dsa2.c +29 -0
index e7c30b472034..752f21273bd6 100644
@@ -727,6 +727,35 @@ static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
 	return err;
 }
 
+bool dsa_port_is_enabled(struct dsa_switch *ds, int port)
+{
+	struct device_node *dn = ds->dev->of_node;
+	struct device_node *ports, *port_node;
+	bool found = false;
+	int reg, err;
+
+	ports = of_get_child_by_name(dn, "ports");
+	if (!ports) {
+		dev_err(ds->dev, "no ports child node found\n");
+		return false;
+	}
+
+	for_each_available_child_of_node(ports, port_node) {
+		err = of_property_read_u32(port_node, "reg", &reg);
+		if (err)
+			goto out_put_node;
+
+		if (reg == port) {
+			found = true;
+			break;
+		}
+	}
+
+out_put_node:
+	of_node_put(ports);
+	return found;
+}
+
 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
 				      struct device_node *dn)
 {
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH 2/2 net: dsa: sja1105: Avoid error message for unknown PHY mode on disabled ports

From: Vladimir Oltean <[email protected]>

When sja1105_init_mii_settings iterates over the port list, it prints this message for disabled ports, because they don't have a valid phy-mode:

[    4.778702] sja1105 spi2.0: Unsupported PHY mode unknown!

Use the newly introduced dsa_port_is_enabled helper to avoid this.

---
 drivers/net/dsa/sja1105/sja1105_main.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/dsa/sja1105/sja1105_main.c +3 -0
index d8123288c572..8152b68389a7 100644
@@ -162,6 +162,9 @@ static int sja1105_init_mii_settings(struct sja1105_private *priv,
 	mii = table->entries;
 
 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
+		if (!dsa_port_is_enabled(priv->ds, i))
+			continue;
+
 		switch (ports[i].phy_mode) {
 		case PHY_INTERFACE_MODE_MII:
 			mii->xmii_mode[i] = XMII_MODE_MII;
Signature · 1 line
2.17.1
maintainer
#2 lore raw
In reply to Vladimir Oltean
Quoted 8 lines · Vladimir Oltean

From: Vladimir Oltean <[email protected]>

Sometimes drivers need to do per-port operation outside the port DSA methods, and in that case they typically iterate through their port list themselves.

Give them an aid to skip ports that are disabled in the device tree (which the DSA core already skips).

Hi Vladimir

Why not dsa_is_unused_port()?

Andrew

#3 lore raw
In reply to Vladimir Oltean

Hi Vladimir,

Quoted 54 lines · Vladimir Oltean

From: Vladimir Oltean <[email protected]>

Sometimes drivers need to do per-port operation outside the port DSA methods, and in that case they typically iterate through their port list themselves.

Give them an aid to skip ports that are disabled in the device tree (which the DSA core already skips).

---
 include/net/dsa.h |  2 ++
 net/dsa/dsa2.c    | 29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
include/net/dsa.h +2 -0
index beeb81a532e3..813792e6f0be 100644
@@ -376,6 +376,8 @@ static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
 		return dp->vlan_filtering;
 }
 
+bool dsa_port_is_enabled(struct dsa_switch *ds, int port);
+
 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
 			      bool is_static, void *data);
 struct dsa_switch_ops {
net/dsa/dsa2.c +18 -0
index e7c30b472034..752f21273bd6 100644
@@ -727,6 +727,35 @@ static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
 	return err;
 }
 
+bool dsa_port_is_enabled(struct dsa_switch *ds, int port)
+{
+	struct device_node *dn = ds->dev->of_node;
+	struct device_node *ports, *port_node;
+	bool found = false;
+	int reg, err;
+
+	ports = of_get_child_by_name(dn, "ports");
+	if (!ports) {
+		dev_err(ds->dev, "no ports child node found\n");
+		return false;
+	}
+
+	for_each_available_child_of_node(ports, port_node) {
+		err = of_property_read_u32(port_node, "reg", &reg);
+		if (err)
+			goto out_put_node;
+

net/dsa/dsa2.c static int dsa_switch_parse_ports_of(struct dsa_switch *ds, · line 757

Quoted 10 lines
net/dsa/dsa2.c +10 -0
+		if (reg == port) {
+			found = true;
+			break;
+		}
+	}
+
+out_put_node:
+	of_node_put(ports);
+	return found;
+}

Why do you need to iterate on the device tree? Ideally we could make abstraction of that (basic platform data is supported too) and use pure DSA helpers, given that DSA core has already peeled all that for us.

Your helper above doesn't even look at the port's state, only if it is declared or not. Looking at patch 2, Using !dsa_is_unused_port(ds, port) seems enough.

Thanks,

Vivien

author maintainer
#4 lore raw

Hi Andrew, Vivien,

Quoted 73 lines · Vivien Didelot

Hi Vladimir,

Quoted 54 lines · Vladimir Oltean

From: Vladimir Oltean <[email protected]>

Sometimes drivers need to do per-port operation outside the port DSA methods, and in that case they typically iterate through their port list themselves.

Give them an aid to skip ports that are disabled in the device tree (which the DSA core already skips).

---
 include/net/dsa.h |  2 ++
 net/dsa/dsa2.c    | 29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
include/net/dsa.h +2 -0
index beeb81a532e3..813792e6f0be 100644
@@ -376,6 +376,8 @@ static inline bool dsa_port_is_vlan_filtering(const struct dsa_port *dp)
              return dp->vlan_filtering;
 }
 
+bool dsa_port_is_enabled(struct dsa_switch *ds, int port);
+
 typedef int dsa_fdb_dump_cb_t(const unsigned char *addr, u16 vid,
                            bool is_static, void *data);
 struct dsa_switch_ops {
net/dsa/dsa2.c +18 -0
index e7c30b472034..752f21273bd6 100644
@@ -727,6 +727,35 @@ static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
      return err;
 }
 
+bool dsa_port_is_enabled(struct dsa_switch *ds, int port)
+{
+     struct device_node *dn = ds->dev->of_node;
+     struct device_node *ports, *port_node;
+     bool found = false;
+     int reg, err;
+
+     ports = of_get_child_by_name(dn, "ports");
+     if (!ports) {
+             dev_err(ds->dev, "no ports child node found\n");
+             return false;
+     }
+
+     for_each_available_child_of_node(ports, port_node) {
+             err = of_property_read_u32(port_node, "reg", &reg);
+             if (err)
+                     goto out_put_node;
+

net/dsa/dsa2.c static int dsa_switch_parse_ports_of(struct dsa_switch *ds, · line 757

Quoted 10 lines
net/dsa/dsa2.c +10 -0
+             if (reg == port) {
+                     found = true;
+                     break;
+             }
+     }
+
+out_put_node:
+     of_node_put(ports);
+     return found;
+}

Why do you need to iterate on the device tree? Ideally we could make abstraction of that (basic platform data is supported too) and use pure DSA helpers, given that DSA core has already peeled all that for us.

Quoted 3 lines

Your helper above doesn't even look at the port's state, only if it is declared or not. Looking at patch 2, Using !dsa_is_unused_port(ds, port) seems enough.

Huh, this one was too smart for me.
I wrote the patch a while ago and did not really look deep enough to spot this one. Actually I remember I did notice the DSA_PORT_TYPE_UNUSED keyword, but I hadn't seen the ports being set anywhere to that value. I realized just now that it's zero so they're unused by default.
That's exactly why code review is amazing! Thank you, I've sent a v2 with your suggestion, it works a treat.

Quoted 4 lines

Thanks,

Vivien

Thanks,
-Vladimir