Skip to contentSkip to conversation

net: dsa: modular build fixes and improvements

PATCH In review Florian Fainelli 3 participants 12 messages Jan 12, 2015 – Jan 13, 2015

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

cover net: dsa: modular build fixes and improvements

Hi David,

This patch series fixes some problems seen when using DSA as a module. There was a circular functional dependency which made it impossible to successfully load a DSA switch driver module, and impossible re-insert it afterwards. Some other problems appearing where the lack of resource cleanup, leading to various leaks and -EEXIST errors upon module re-insertion.

Thanks!

Florian Fainelli (8):
net: dsa: add missing netdevice.h include
net: dsa: make module builds work
net: phy: fixed: allow setting no update_link callback
net: dsa: cleanup resources upon module removal
net: dsa: allow switch drivers to cleanup their resources
net: dsa: bcm_sf2: factor interrupt disabling in a function
net: dsa: bcm_sf2: cleanup resources in remove callback
net: dsa: free distributed switch tree pointer in dsa_remove

 drivers/net/dsa/bcm_sf2.c   | 44 ++++++++++++++++++++--------
 drivers/net/phy/fixed_phy.c |  2 +-
 include/net/dsa.h           |  1 +
 net/dsa/Makefile            |  2 ++
 net/dsa/dsa.c               | 71 ++++++++++++++-------------------------------
 net/dsa/dsa_lib.c           | 68 +++++++++++++++++++++++++++++++++++++++++++
 net/dsa/dsa_priv.h          |  6 ++++
 net/dsa/slave.c             | 14 +++++++++
 8 files changed, 146 insertions(+), 62 deletions(-)
 create mode 100644 net/dsa/dsa_lib.c
Signature · 1 line
2.1.0
#1 lore raw

PATCH 1/8 net: dsa: add missing netdevice.h include

dsa_priv.h contains function prototypes which use a netdev_tx_t return type, include netdevice.h to get that definition.

---
 net/dsa/dsa_priv.h | 1 +
 1 file changed, 1 insertion(+)
net/dsa/dsa_priv.h +1 -0
index dc9756d3154c..e560ae53996c 100644
@@ -11,6 +11,7 @@
 #ifndef __DSA_PRIV_H
 #define __DSA_PRIV_H
 
+#include <linux/netdevice.h>
 #include <linux/phy.h>
 #include <linux/netdevice.h>
 
Signature · 1 line
2.1.0
#2 lore raw

PATCH 2/8 net: dsa: make module builds work

In reply to Florian Fainelli

Building any DSA driver as a module will work from a compilation/linking perspective, but the resulting modules produced are not functional.

Any DSA driver references register_switch_driver and unregister_switch_driver which are provided by net/dsa/dsa.c, so loading any of these modules prior to dsa_core.ko being loaded will faill.

Unfortunately, loading dsa_core.ko will make us call dsa_switch_probe() which will find no DSA switch driver and return an error so we are stuck there because there is no switch driver available. So this is getting us nowhere.

This patch introduces a separate module, named dsa_lib which contains register_switch_driver, unregister_switch_driver and dsa_switch_probe (to avoid exposing the list and mutex used for walking switch drivers), such that the following can be done:

- load dsa_lib
- load the dsa switch driver, e.g: mv88e6060, bcm_sf2
- load the dsa_core module

---
 net/dsa/Makefile   |  2 ++
 net/dsa/dsa.c      | 49 ---------------------------------------
 net/dsa/dsa_lib.c  | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 net/dsa/dsa_priv.h |  4 ++++
 4 files changed, 74 insertions(+), 49 deletions(-)
 create mode 100644 net/dsa/dsa_lib.c
net/dsa/Makefile +2 -0
index da06ed1df620..17827194754d 100644
@@ -2,6 +2,8 @@
 obj-$(CONFIG_NET_DSA) += dsa_core.o
 dsa_core-y += dsa.o slave.o
 
+obj-$(CONFIG_NET_DSA) += dsa_lib.o
+
 # tagging formats
 dsa_core-$(CONFIG_NET_DSA_TAG_BRCM) += tag_brcm.o
 dsa_core-$(CONFIG_NET_DSA_TAG_DSA) += tag_dsa.o
net/dsa/dsa.c +0 -49

55 lines not on this page · raw message

net/dsa/dsa_lib.c new file +68 -0

68 lines not on this page · raw message

net/dsa/dsa_priv.h +4 -0
index e560ae53996c..1397b2894c1f 100644
@@ -51,6 +51,10 @@ struct dsa_slave_priv {
 /* dsa.c */
 extern char dsa_driver_version[];
 
+/* dsa_lib.c */
+struct dsa_switch_driver *
+dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name);
+
 /* slave.c */
 extern const struct dsa_device_ops notag_netdev_ops;
 void dsa_slave_mii_bus_init(struct dsa_switch *ds);
Signature · 1 line
2.1.0
maintainer
#3 lore raw

Re: [PATCH net-next 2/8] net: dsa: make module builds work

From: Florian Fainelli <[email protected]>
Date: Mon, 12 Jan 2015 13:57:40 -0800

Quoted 17 lines

Building any DSA driver as a module will work from a compilation/linking perspective, but the resulting modules produced are not functional.

Any DSA driver references register_switch_driver and unregister_switch_driver which are provided by net/dsa/dsa.c, so loading any of these modules prior to dsa_core.ko being loaded will faill.

Unfortunately, loading dsa_core.ko will make us call dsa_switch_probe() which will find no DSA switch driver and return an error so we are stuck there because there is no switch driver available. So this is getting us nowhere.

This patch introduces a separate module, named dsa_lib which contains register_switch_driver, unregister_switch_driver and dsa_switch_probe (to avoid exposing the list and mutex used for walking switch drivers), such that the following can be done:

Quoted 5 lines

- load dsa_lib
- load the dsa switch driver, e.g: mv88e6060, bcm_sf2
- load the dsa_core module

This looks worse to me.

Really, the match table and probing should not be in dsa_core at all.

It should only be done in individual drivers.

#4 lore raw

Re: [PATCH net-next 2/8] net: dsa: make module builds work

Quoted 24 lines · David Miller

From: Florian Fainelli <[email protected]>
Date: Mon, 12 Jan 2015 13:57:40 -0800

Quoted 21 lines

Building any DSA driver as a module will work from a compilation/linking perspective, but the resulting modules produced are not functional.

Any DSA driver references register_switch_driver and unregister_switch_driver which are provided by net/dsa/dsa.c, so loading any of these modules prior to dsa_core.ko being loaded will faill.

Unfortunately, loading dsa_core.ko will make us call dsa_switch_probe() which will find no DSA switch driver and return an error so we are stuck there because there is no switch driver available. So this is getting us nowhere.

This patch introduces a separate module, named dsa_lib which contains register_switch_driver, unregister_switch_driver and dsa_switch_probe (to avoid exposing the list and mutex used for walking switch drivers), such that the following can be done:

- load dsa_lib
- load the dsa switch driver, e.g: mv88e6060, bcm_sf2
- load the dsa_core module

Quoted 7 lines
Quoted 1 line

This looks worse to me.

Really, the match table and probing should not be in dsa_core at all.

It should only be done in individual drivers.

Right, I guess enough procrastination on my side is enough, time to get this plan submitted: http://marc.info/?t=141038714600002&r=1&w=2.

Signature · 1 line
Florian
#5 lore raw

PATCH 3/8 net: phy: fixed: allow setting no update_link callback

In reply to Florian Fainelli

fixed_phy_set_link_update() contains an early check against a NULL callback pointer, which basically prevents us from removing any previous callback we may have set. The users of the fp->link_update callback deal with a NULL callback just fine.

---
 drivers/net/phy/fixed_phy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/phy/fixed_phy.c +1 -1
index 3ad0e6e16c39..a08a3c78ba97 100644
@@ -168,7 +168,7 @@ int fixed_phy_set_link_update(struct phy_device *phydev,
 	struct fixed_mdio_bus *fmb = &platform_fmb;
 	struct fixed_phy *fp;
 
-	if (!link_update || !phydev || !phydev->bus)
+	if (!phydev || !phydev->bus)
 		return -EINVAL;
 
 	list_for_each_entry(fp, &fmb->phys, node) {
Signature · 1 line
2.1.0
#6 lore raw

PATCH 4/8 net: dsa: cleanup resources upon module removal

In reply to Florian Fainelli

We were not doing anything while removing the dsa module, which means that we were leaving dangling network devices without any sort of driver backing them, leading to all sorts of crashes. Make sure that we do cleanup the slave network devices, slave MII bus we created, and unassign the master_netdev dsa_ptr to make the packet processing go through the regulard Ethernet receive path.

---
 net/dsa/dsa.c      | 19 +++++++++++++++++++
 net/dsa/dsa_priv.h |  1 +
 net/dsa/slave.c    | 14 ++++++++++++++
 3 files changed, 34 insertions(+)
net/dsa/dsa.c +19 -0
index de77c83cfd9a..df7ec066ac64 100644
@@ -316,10 +316,22 @@ out:
 
 static void dsa_switch_destroy(struct dsa_switch *ds)
 {
+	int i;
 #ifdef CONFIG_NET_DSA_HWMON
 	if (ds->hwmon_dev)
 		hwmon_device_unregister(ds->hwmon_dev);
 #endif
+
+	for (i = 0; i < DSA_MAX_PORTS; i++) {
+		if (!(ds->phys_port_mask & (1 << i)))
+			continue;
+
+		dsa_slave_destroy(ds->ports[i]);
+	}
+
+	mdiobus_unregister(ds->slave_mii_bus);
+	mdiobus_free(ds->slave_mii_bus);
+	kfree(ds);
 }
 
 #ifdef CONFIG_PM_SLEEP
@@ -754,6 +766,13 @@ static int dsa_remove(struct platform_device *pdev)
 			dsa_switch_destroy(ds);
 	}
 
+	dst->master_netdev->dsa_ptr = NULL;
+	/* If we used a tagging format that doesn't have an ethertype
+	 * field, make sure that all packets from this point get sent
+	 * without the tag and go through the regular receive path.
+	 */
+	wmb();
+
 	dsa_of_remove(pdev);
 
 	return 0;
net/dsa/dsa_priv.h +1 -0
index 1397b2894c1f..1773b2a20d90 100644
@@ -61,6 +61,7 @@ void dsa_slave_mii_bus_init(struct dsa_switch *ds);
 struct net_device *dsa_slave_create(struct dsa_switch *ds,
 				    struct device *parent,
 				    int port, char *name);
+void dsa_slave_destroy(struct net_device *slave_dev);
 int dsa_slave_suspend(struct net_device *slave_dev);
 int dsa_slave_resume(struct net_device *slave_dev);
 
net/dsa/slave.c +14 -0
index 515569ffde8a..043cffcb1a3a 100644
@@ -690,3 +690,17 @@ dsa_slave_create(struct dsa_switch *ds, struct device *parent,
 
 	return slave_dev;
 }
+
+void dsa_slave_destroy(struct net_device *slave_dev)
+{
+	struct dsa_slave_priv *p = netdev_priv(slave_dev);
+
+	if (p->phy) {
+		/* Prevent further updates */
+		fixed_phy_set_link_update(p->phy, NULL);
+		phy_disconnect(p->phy);
+	}
+
+	unregister_netdev(slave_dev);
+	free_netdev(slave_dev);
+}
Signature · 1 line
2.1.0
#7 lore raw

Re: [PATCH net-next 4/8] net: dsa: cleanup resources upon module removal

Hello.

Quoted 6 lines · Florian Fainelli

We were not doing anything while removing the dsa module, which means that we were leaving dangling network devices without any sort of driver backing them, leading to all sorts of crashes. Make sure that we do cleanup the slave network devices, slave MII bus we created, and unassign the master_netdev dsa_ptr to make the packet processing go through the regulard Ethernet receive path.

Regular. :-)

Quoted 1 line

[...]

net/dsa/dsa.c out: · line 319

Quoted 9 lines
net/dsa/dsa.c +1 -0
index de77c83cfd9a..df7ec066ac64 100644
@@ -316,10 +316,22 @@ out:
 
  static void dsa_switch_destroy(struct dsa_switch *ds)
  {
+	int i;

Need empty line here.

net/dsa/dsa.c out: · line 323

Quoted 4 lines
net/dsa/dsa.c +0 -0
  #ifdef CONFIG_NET_DSA_HWMON
  	if (ds->hwmon_dev)
  		hwmon_device_unregister(ds->hwmon_dev);
  #endif

[...]

WBR, Sergei

#8 lore raw

PATCH 5/8 net: dsa: allow switch drivers to cleanup their resources

In reply to Florian Fainelli

Some switch drivers might request interrupts, remap register ranges, allow such drivers to implement a "remove" callback doing just that.

---
 include/net/dsa.h | 1 +
 net/dsa/dsa.c     | 2 ++
 2 files changed, 3 insertions(+)
include/net/dsa.h +1 -0
index ed3c34bbb67a..cace02a4cbdd 100644
@@ -192,6 +192,7 @@ struct dsa_switch_driver {
 	 */
 	char	*(*probe)(struct device *host_dev, int sw_addr);
 	int	(*setup)(struct dsa_switch *ds);
+	void	(*remove)(struct dsa_switch *ds);
 	int	(*set_addr)(struct dsa_switch *ds, u8 *addr);
 	u32	(*get_phy_flags)(struct dsa_switch *ds, int port);
 
net/dsa/dsa.c +2 -0
index df7ec066ac64..008a1a58cf0c 100644
@@ -331,6 +331,8 @@ static void dsa_switch_destroy(struct dsa_switch *ds)
 
 	mdiobus_unregister(ds->slave_mii_bus);
 	mdiobus_free(ds->slave_mii_bus);
+	if (ds->drv->remove)
+		ds->drv->remove(ds);
 	kfree(ds);
 }
 
Signature · 1 line
2.1.0
#9 lore raw

PATCH 6/8 net: dsa: bcm_sf2: factor interrupt disabling in a function

In reply to Florian Fainelli

Factor the interrupt disabling in a function: bcm_sf2_intr_disable() since we are doing the same thing in the setup and suspend paths.

---
 drivers/net/dsa/bcm_sf2.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/net/dsa/bcm_sf2.c +12 -12
index feb29c4526f7..09f6b3cc1f66 100644
@@ -400,6 +400,16 @@ static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
 	return 0;
 }
 
+static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
+{
+	intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
+	intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
+	intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
+	intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
+	intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
+	intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
+}
+
 static int bcm_sf2_sw_setup(struct dsa_switch *ds)
 {
 	const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
@@ -440,12 +450,7 @@ static int bcm_sf2_sw_setup(struct dsa_switch *ds)
 	}
 
 	/* Disable all interrupts and request them */
-	intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
-	intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
-	intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
-	intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
-	intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
-	intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
+	bcm_sf2_intr_disable(priv);
 
 	ret = request_irq(priv->irq0, bcm_sf2_switch_0_isr, 0,
 			  "switch_0", priv);
@@ -747,12 +752,7 @@ static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
 	struct bcm_sf2_priv *priv = ds_to_priv(ds);
 	unsigned int port;
 
-	intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
-	intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
-	intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
-	intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
-	intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
-	intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
+	bcm_sf2_intr_disable(priv);
 
 	/* Disable all ports physically present including the IMP
 	 * port, the other ones have already been disabled during
Signature · 1 line
2.1.0
#10 lore raw

PATCH 7/8 net: dsa: bcm_sf2: cleanup resources in remove callback

In reply to Florian Fainelli

Implement a remove callback allowing the switch driver to cleanup resources it used: interrupts and remapped register ranges.

---
 drivers/net/dsa/bcm_sf2.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/net/dsa/bcm_sf2.c +20 -0
index 09f6b3cc1f66..a41b20c7d602 100644
@@ -526,6 +526,25 @@ out_unmap:
 	return ret;
 }
 
+static void bcm_sf2_sw_remove(struct dsa_switch *ds)
+{
+	struct bcm_sf2_priv *priv = ds_to_priv(ds);
+	void __iomem **base;
+	unsigned int i;
+
+	/* Disable all interrupts and free them */
+	bcm_sf2_intr_disable(priv);
+
+	free_irq(priv->irq0, priv);
+	free_irq(priv->irq1, priv);
+
+	base = &priv->core;
+	for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
+		iounmap(*base);
+		base++;
+	}
+}
+
 static int bcm_sf2_sw_set_addr(struct dsa_switch *ds, u8 *addr)
 {
 	return 0;
@@ -858,6 +877,7 @@ static struct dsa_switch_driver bcm_sf2_switch_driver = {
 	.tag_protocol		= DSA_TAG_PROTO_BRCM,
 	.priv_size		= sizeof(struct bcm_sf2_priv),
 	.probe			= bcm_sf2_sw_probe,
+	.remove			= bcm_sf2_sw_remove,
 	.setup			= bcm_sf2_sw_setup,
 	.set_addr		= bcm_sf2_sw_set_addr,
 	.get_phy_flags		= bcm_sf2_sw_get_phy_flags,
Signature · 1 line
2.1.0
#11 lore raw

PATCH 8/8 net: dsa: free distributed switch tree pointer in dsa_remove

In reply to Florian Fainelli

We allocate a "dst" pointer in dsa_probe(), but we are not freeing it accordingly in dsa_remove(), do that to avoid leaking it.

Fixes 91da11f870f00 ("net: Distributed Switch Architecture protocol support")
---
 net/dsa/dsa.c | 1 +
 1 file changed, 1 insertion(+)
net/dsa/dsa.c +1 -0
index 008a1a58cf0c..b511713b8885 100644
@@ -776,6 +776,7 @@ static int dsa_remove(struct platform_device *pdev)
 	wmb();
 
 	dsa_of_remove(pdev);
+	kfree(dst);
 
 	return 0;
 }
Signature · 1 line
2.1.0