Skip to contentSkip to conversation

net: dsa: microchip: set the correct number of ports in dsa_switch

PATCH In review Codrin Ciubotariu 2 participants 4 messages Jul 1, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 net: dsa: microchip: set the correct number of ports in dsa_switch

The number of ports is incorrectly set to the maximum available for a DSA switch. Even if the extra ports are not used, this causes some functions to be called later, like port_disable() and port_stp_state_set(). If the driver doesn't check the port index, it will end up modifying unknown registers.

Fixes b987e98e50ab ("dsa: add DSA switch driver for Microchip KSZ9477")
---
 drivers/net/dsa/microchip/ksz8795.c    |  7 +++++--
 drivers/net/dsa/microchip/ksz9477.c    |  7 +++++--
 drivers/net/dsa/microchip/ksz_common.c | 26 ++++++++++++++++----------
 drivers/net/dsa/microchip/ksz_common.h |  1 +
 4 files changed, 27 insertions(+), 14 deletions(-)
drivers/net/dsa/microchip/ksz8795.c +5 -2
index 47d65b77caf7..b0227b0e31e6 100644
@@ -1225,8 +1225,6 @@ static int ksz8795_switch_init(struct ksz_device *dev)
 {
 	int i;
 
-	dev->ds->ops = &ksz8795_switch_ops;
-
 	for (i = 0; i < ARRAY_SIZE(ksz8795_switch_chips); i++) {
 		const struct ksz_chip_data *chip = &ksz8795_switch_chips[i];
 
@@ -1268,6 +1266,11 @@ static int ksz8795_switch_init(struct ksz_device *dev)
 			return -ENOMEM;
 	}
 
+	dev->ds = ksz_dsa_switch_alloc(dev);
+	if (!dev->ds)
+		return -ENOMEM;
+	dev->ds->ops = &ksz8795_switch_ops;
+
 	return 0;
 }
 
drivers/net/dsa/microchip/ksz9477.c +5 -2
index 9a51b8a4de5d..833cf3763000 100644
@@ -1545,8 +1545,6 @@ static int ksz9477_switch_init(struct ksz_device *dev)
 {
 	int i;
 
-	dev->ds->ops = &ksz9477_switch_ops;
-
 	for (i = 0; i < ARRAY_SIZE(ksz9477_switch_chips); i++) {
 		const struct ksz_chip_data *chip = &ksz9477_switch_chips[i];
 
@@ -1588,6 +1586,11 @@ static int ksz9477_switch_init(struct ksz_device *dev)
 			return -ENOMEM;
 	}
 
+	dev->ds = ksz_dsa_switch_alloc(dev);
+	if (!dev->ds)
+		return -ENOMEM;
+	dev->ds->ops = &ksz9477_switch_ops;
+
 	return 0;
 }
 
drivers/net/dsa/microchip/ksz_common.c +16 -10
index fd1d6676ae4f..4a41f0c7dbbc 100644
@@ -387,30 +387,36 @@ EXPORT_SYMBOL_GPL(ksz_disable_port);
 
 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv)
 {
-	struct dsa_switch *ds;
 	struct ksz_device *swdev;
 
-	ds = devm_kzalloc(base, sizeof(*ds), GFP_KERNEL);
-	if (!ds)
-		return NULL;
-
-	ds->dev = base;
-	ds->num_ports = DSA_MAX_PORTS;
-
 	swdev = devm_kzalloc(base, sizeof(*swdev), GFP_KERNEL);
 	if (!swdev)
 		return NULL;
 
-	ds->priv = swdev;
 	swdev->dev = base;
 
-	swdev->ds = ds;
 	swdev->priv = priv;
 
 	return swdev;
 }
 EXPORT_SYMBOL(ksz_switch_alloc);
 
+struct dsa_switch *ksz_dsa_switch_alloc(struct ksz_device *swdev)
+{
+	struct dsa_switch *ds;
+
+	ds = devm_kzalloc(swdev->dev, sizeof(*ds), GFP_KERNEL);
+	if (!ds)
+		return NULL;
+
+	ds->dev = swdev->dev;
+	ds->num_ports = swdev->port_cnt;
+	ds->priv = swdev;
+
+	return ds;
+}
+EXPORT_SYMBOL(ksz_dsa_switch_alloc);
+
 int ksz_switch_register(struct ksz_device *dev,
 			const struct ksz_dev_ops *ops)
 {
drivers/net/dsa/microchip/ksz_common.h +1 -0
index f2c9bb68fd33..785702514a46 100644
@@ -145,6 +145,7 @@ struct ksz_dev_ops {
 };
 
 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
+struct dsa_switch *ksz_dsa_switch_alloc(struct ksz_device *swdev);
 int ksz_switch_register(struct ksz_device *dev,
 			const struct ksz_dev_ops *ops);
 void ksz_switch_remove(struct ksz_device *dev);
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 net: dsa: microchip: split adjust_link() in phylink_mac_link_{up|down}()

The DSA subsystem moved to phylink and adjust_link() became deprecated in the process. This patch removes adjust_link from the KSZ DSA switches and adds phylink_mac_link_up() and phylink_mac_link_down().

---
 drivers/net/dsa/microchip/ksz8795.c    |  3 ++-
 drivers/net/dsa/microchip/ksz9477.c    |  3 ++-
 drivers/net/dsa/microchip/ksz_common.c | 32 ++++++++++++++++----------
 drivers/net/dsa/microchip/ksz_common.h |  7 ++++--
 4 files changed, 29 insertions(+), 16 deletions(-)
drivers/net/dsa/microchip/ksz8795.c +2 -1
index b0227b0e31e6..d07231d1def5 100644
@@ -1111,7 +1111,8 @@ static const struct dsa_switch_ops ksz8795_switch_ops = {
 	.setup			= ksz8795_setup,
 	.phy_read		= ksz_phy_read16,
 	.phy_write		= ksz_phy_write16,
-	.adjust_link		= ksz_adjust_link,
+	.phylink_mac_link_down	= ksz_mac_link_down,
+	.phylink_mac_link_up	= ksz_mac_link_up,
 	.port_enable		= ksz_enable_port,
 	.port_disable		= ksz_disable_port,
 	.get_strings		= ksz8795_get_strings,
drivers/net/dsa/microchip/ksz9477.c +2 -1
index 833cf3763000..162f2bd84774 100644
@@ -1399,7 +1399,8 @@ static const struct dsa_switch_ops ksz9477_switch_ops = {
 	.setup			= ksz9477_setup,
 	.phy_read		= ksz9477_phy_read16,
 	.phy_write		= ksz9477_phy_write16,
-	.adjust_link		= ksz_adjust_link,
+	.phylink_mac_link_down	= ksz_mac_link_down,
+	.phylink_mac_link_up	= ksz_mac_link_up,
 	.port_enable		= ksz_enable_port,
 	.port_disable		= ksz_disable_port,
 	.get_strings		= ksz9477_get_strings,
drivers/net/dsa/microchip/ksz_common.c +20 -12
index 4a41f0c7dbbc..a35d6ba8dd8a 100644
@@ -135,26 +135,34 @@ int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
 }
 EXPORT_SYMBOL_GPL(ksz_phy_write16);
 
-void ksz_adjust_link(struct dsa_switch *ds, int port,
-		     struct phy_device *phydev)
+void ksz_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode,
+		       phy_interface_t interface)
 {
 	struct ksz_device *dev = ds->priv;
 	struct ksz_port *p = &dev->ports[port];
 
 	/* Read all MIB counters when the link is going down. */
-	if (!phydev->link) {
-		p->read = true;
-		schedule_delayed_work(&dev->mib_read, 0);
-	}
+	p->read = true;
+	schedule_delayed_work(&dev->mib_read, 0);
+
+	mutex_lock(&dev->dev_mutex);
+	dev->live_ports &= ~(1 << port);
+	mutex_unlock(&dev->dev_mutex);
+}
+EXPORT_SYMBOL_GPL(ksz_mac_link_down);
+
+void ksz_mac_link_up(struct dsa_switch *ds, int port, unsigned int mode,
+		     phy_interface_t interface, struct phy_device *phydev,
+		     int speed, int duplex, bool tx_pause, bool rx_pause)
+{
+	struct ksz_device *dev = ds->priv;
+
+	/* Remember which port is connected and active. */
 	mutex_lock(&dev->dev_mutex);
-	if (!phydev->link)
-		dev->live_ports &= ~(1 << port);
-	else
-		/* Remember which port is connected and active. */
-		dev->live_ports |= (1 << port) & dev->on_ports;
+	dev->live_ports |= (1 << port) & dev->on_ports;
 	mutex_unlock(&dev->dev_mutex);
 }
-EXPORT_SYMBOL_GPL(ksz_adjust_link);
+EXPORT_SYMBOL_GPL(ksz_mac_link_up);
 
 int ksz_sset_count(struct dsa_switch *ds, int port, int sset)
 {
drivers/net/dsa/microchip/ksz_common.h +5 -2
index 785702514a46..b1c468713609 100644
@@ -160,8 +160,11 @@ void ksz_init_mib_timer(struct ksz_device *dev);
 
 int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg);
 int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val);
-void ksz_adjust_link(struct dsa_switch *ds, int port,
-		     struct phy_device *phydev);
+void ksz_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode,
+		       phy_interface_t interface);
+void ksz_mac_link_up(struct dsa_switch *ds, int port, unsigned int mode,
+		     phy_interface_t interface, struct phy_device *phydev,
+		     int speed, int duplex, bool tx_pause, bool rx_pause);
 int ksz_sset_count(struct dsa_switch *ds, int port, int sset);
 void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf);
 int ksz_port_bridge_join(struct dsa_switch *ds, int port,
Signature · 1 line
2.25.1
maintainer
#2 lore raw
In reply to Codrin Ciubotariu
Quoted 7 lines · Codrin Ciubotariu

The number of ports is incorrectly set to the maximum available for a DSA switch. Even if the extra ports are not used, this causes some functions to be called later, like port_disable() and port_stp_state_set(). If the driver doesn't check the port index, it will end up modifying unknown registers.

Fixes b987e98e50ab ("dsa: add DSA switch driver for Microchip KSZ9477")

Hi Codrin

You don't indicate which tree this is for. net-next, or net? It looks like it fixes a real issue, so it probably should be for net. But patches to net should be minimal. Is it possible to do the

	ds->num_ports = swdev->port_cnt;

without all the other changes? You can then have a refactoring patch in net-next.

Thanks
Andrew

#3 lore raw
Quoted 13 lines · Andrew Lunn

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

Quoted 7 lines · Codrin Ciubotariu

The number of ports is incorrectly set to the maximum available for a DSA switch. Even if the extra ports are not used, this causes some functions to be called later, like port_disable() and port_stp_state_set(). If the driver doesn't check the port index, it will end up modifying unknown registers.

Fixes b987e98e50ab ("dsa: add DSA switch driver for Microchip KSZ9477")

Hi Codrin

Quoted 8 lines

You don't indicate which tree this is for. net-next, or net? It looks like it fixes a real issue, so it probably should be for net. But patches to net should be minimal. Is it possible to do the

         ds->num_ports = swdev->port_cnt;

without all the other changes? You can then have a refactoring patch in net-next.

This one should be for net. Ok then, I will send a simpler version of this patch for net, just to fix the issue and another one like this one for net-next.

Thanks and best regards,
Codrin

Quoted 4 lines

Thanks
Andrew