net: ibm: emac: some cleanups and devm
Subsystems (2)
cover net: ibm: emac: some cleanups and devm
It's a very old driver with a lot of potential for cleaning up code to modern standards. This was a simple one dealing with mostly the probe function and adding some devm to it.
All patches were tested on a Cisco Meraki MX60W. Boot and Shutdown/Reboot showed no warnings.
Rosen Penev (6):
net: ibm: emac: use devm for alloc_etherdev
net: ibm: emac: manage emac_irq with devm
net: ibm: emac: use devm for of_iomap
net: ibm: emac: remove mii_bus with devm
net: ibm: emac: use devm for register_netdev
net: ibm: emac: use netdev's phydev directly
drivers/net/ethernet/ibm/emac/core.c | 130 +++++++++++---------------- drivers/net/ethernet/ibm/emac/core.h | 4 - 2 files changed, 50 insertions(+), 84 deletions(-)
Signature · 1 line
2.46.0
PATCH 1/6 net: ibm: emac: use devm for alloc_etherdev
Allows to simplify the code slightly. This is safe to do as free_netdev gets called last.
--- drivers/net/ethernet/ibm/emac/core.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +3 -6
@@ -3053,7 +3053,7 @@ static int emac_probe(struct platform_device *ofdev)/* Allocate our net_device structure */ err = -ENOMEM ; - ndev =alloc_etherdev (sizeof (struct emac_instance )); + ndev =devm_alloc_etherdev (&ofdev->dev,sizeof (struct emac_instance ));if (!ndev) goto err_gone;
@@ -3072,7 +3072,7 @@ static int emac_probe(struct platform_device *ofdev)/* Init various config data based on device-tree */ err =emac_init_config (dev);if (err) - goto err_free; + goto err_gone;/* Get interrupts. EMAC irq is mandatory, WOL irq is optional */ dev->emac_irq =irq_of_parse_and_map (np,0 );
@@ -3080,7 +3080,7 @@ static int emac_probe(struct platform_device *ofdev)if (!dev->emac_irq) {printk (KERN_ERR "%pOF: Can't map main interrupt\n" , np); err = -ENODEV ; - goto err_free; + goto err_gone; } ndev->irq = dev->emac_irq;
@@ -3239,8 +3239,6 @@ static int emac_probe(struct platform_device *ofdev)irq_dispose_mapping (dev->wol_irq);if (dev->emac_irq)irq_dispose_mapping (dev->emac_irq); - err_free: -free_netdev (ndev); err_gone:/* if we were on the bootlist, remove us as we won't show up and * wake up all waiters to notify them in case they were waiting
@@ -3289,7 +3287,6 @@ static void emac_remove(struct platform_device *ofdev)if (dev->emac_irq)irq_dispose_mapping (dev->emac_irq); -free_netdev (dev->ndev); }/* XXX Features in here should be replaced by properties... */
Signature · 1 line
2.46.0
PATCH 2/6 net: ibm: emac: manage emac_irq with devm
In reply to Rosen PenevIt's the last to go in remove. Safe to let devm handle it.
Also move request_irq to probe for clarity. It's removed in _remove not close.
Use dev_err instead of printk. Handles names automatically.
--- drivers/net/ethernet/ibm/emac/core.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +9 -18
@@ -1228,18 +1228,10 @@ static void emac_print_link_status(struct emac_instance *dev)static int emac_open (struct net_device *ndev) {struct emac_instance *dev =netdev_priv (ndev); -int err, i; +int i;DBG (dev,"open" NL ); -/* Setup error IRQ handler */ - err =request_irq (dev->emac_irq, emac_irq,0 ,"EMAC" , dev); -if (err) { -printk (KERN_ERR "%s: failed to request IRQ %d\n" , - ndev->name, dev->emac_irq); -return err; - } -/* Allocate RX ring */ for (i =0 ; i <NUM_RX_BUFF ; ++i)if (emac_alloc_rx_skb (dev, i)) {
@@ -1293,8 +1285,6 @@ static int emac_open(struct net_device *ndev)return 0 ; oom:emac_clean_rx_ring (dev); -free_irq (dev->emac_irq, dev); -return -ENOMEM ; }
@@ -1408,8 +1398,6 @@ static int emac_close(struct net_device *ndev)emac_clean_tx_ring (dev);emac_clean_rx_ring (dev); -free_irq (dev->emac_irq, dev); -netif_carrier_off (ndev);return 0 ;
@@ -3082,6 +3070,14 @@ static int emac_probe(struct platform_device *ofdev) err = -ENODEV ; goto err_gone; } + +/* Setup error IRQ handler */ + err =devm_request_irq (&ofdev->dev, dev->emac_irq, emac_irq,0 ,"EMAC" , dev); +if (err) { +dev_err (&ofdev->dev,"failed to request IRQ %d" , dev->emac_irq); + goto err_gone; + } + ndev->irq = dev->emac_irq;/* Map EMAC regs */
@@ -3237,8 +3233,6 @@ static int emac_probe(struct platform_device *ofdev) err_irq_unmap:if (dev->wol_irq)irq_dispose_mapping (dev->wol_irq); -if (dev->emac_irq) -irq_dispose_mapping (dev->emac_irq); err_gone:/* if we were on the bootlist, remove us as we won't show up and * wake up all waiters to notify them in case they were waiting
@@ -3284,9 +3278,6 @@ static void emac_remove(struct platform_device *ofdev)if (dev->wol_irq)irq_dispose_mapping (dev->wol_irq); -if (dev->emac_irq) -irq_dispose_mapping (dev->emac_irq); - }/* XXX Features in here should be replaced by properties... */
Signature · 1 line
2.46.0
Re: [PATCH 2/6] net: ibm: emac: manage emac_irq with devm
drivers/net/ethernet/ibm/emac/core.c static int emac_probe(struct platform_device *ofdev) · line 3079
Quoted 13 lines · Rosen Penev
It's the last to go in remove. Safe to let devm handle it.
Also move
request_irqto probe for clarity. It's removed in_removenot close.Use
dev_errinstead of printk. Handles names automatically.drivers/net/ethernet/ibm/emac/core.c +6
-0+/* Setup error IRQ handler */ + err =devm_request_irq (&ofdev->dev, dev->emac_irq, emac_irq,0 ,"EMAC" , dev); +if (err) { +dev_err (&ofdev->dev,"failed to request IRQ %d" , dev->emac_irq); + goto err_gone; + }
Is this an internal interrupt, or a GPIO? It could be it is done in open because there is a danger the GPIO controller has not probed yet. So here you might get an EPROBE_DEFFER, where as the much older kernel this was written for might not of done, if just gave an error had gave up. So dev_err_probe() might be better.
Andrew
Re: [PATCH 2/6] net: ibm: emac: manage emac_irq with devm
Quoted 16 lines · Andrew Lunn
drivers/net/ethernet/ibm/emac/core.c static int emac_probe(struct platform_device *ofdev) · line 3079
Quoted 13 lines · Rosen Penev
It's the last to go in remove. Safe to let devm handle it.
Also move
request_irqto probe for clarity. It's removed in_removenot close.Use
dev_errinstead of printk. Handles names automatically.drivers/net/ethernet/ibm/emac/core.c +6
-0+/* Setup error IRQ handler */ + err =devm_request_irq (&ofdev->dev, dev->emac_irq, emac_irq,0 ,"EMAC" , dev); +if (err) { +dev_err (&ofdev->dev,"failed to request IRQ %d" , dev->emac_irq); + goto err_gone; + }
Quoted 5 lines
Is this an internal interrupt, or a GPIO? It could be it is done in open because there is a danger the GPIO controller has not probed yet. So here you might get an
EPROBE_DEFFER, where as the much older kernel this was written for might not of done, if just gave an error had gave up. Sodev_err_probe()might be better.
Good call on that. In my experience, I get these EPROBE_DEFER errors on OpenWrt's ath79 target (QCA MIPS) but not on PowerPC when trying to use GPIOs. Nevertheless it seems to be good practice to use dev_err_probe anyway. Will fix in v2.
Quoted 2 lines
Andrew
Re: [PATCH 2/6] net: ibm: emac: manage emac_irq with devm
Quoted 10 lines
Quoted 5 lines
Is this an internal interrupt, or a GPIO? It could be it is done in open because there is a danger the GPIO controller has not probed yet. So here you might get an
EPROBE_DEFFER, where as the much older kernel this was written for might not of done, if just gave an error had gave up. Sodev_err_probe()might be better.Good call on that. In my experience, I get these
EPROBE_DEFERerrors on OpenWrt's ath79 target (QCA MIPS) but not on PowerPC when trying to use GPIOs. Nevertheless it seems to be good practice to usedev_err_probeanyway. Will fix in v2.
You might want to look at
https://elixir.bootlin.com/linux/v6.10.7/source/drivers/net/ethernet/ibm/emac/core.c#L2418
and then replace it by correctly handling EPROBE_DEFER.
As you said, an old driver, needing some cleanup.
Andrew
PATCH 3/6 net: ibm: emac: use devm for of_iomap
In reply to Rosen PenevAllows removing manual iounmap.
--- drivers/net/ethernet/ibm/emac/core.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +4 -9
@@ -3082,10 +3082,9 @@ static int emac_probe(struct platform_device *ofdev)/* Map EMAC regs */ // TODO : platform_get_resource() and devm_ioremap_resource() - dev->emacp =of_iomap (np,0 ); -if (dev->emacp ==NULL ) { -printk (KERN_ERR "%pOF: Can't map device registers!\n" , np); - err = -ENOMEM ; + dev->emacp =devm_of_iomap (&ofdev->dev, np,0 ,NULL ); +if (!dev->emacp) { + err =dev_err_probe (&ofdev->dev, -ENOMEM ,"can't map device registers" ); goto err_irq_unmap; }
@@ -3095,7 +3094,7 @@ static int emac_probe(struct platform_device *ofdev)printk (KERN_ERR "%pOF: Timeout waiting for dependent devices\n" , np);/* display more info about what's missing ? */ - goto err_reg_unmap; + goto err_irq_unmap; } dev->mal =platform_get_drvdata (dev->mal_dev);if (dev->mdio_dev !=NULL )
@@ -3228,8 +3227,6 @@ static int emac_probe(struct platform_device *ofdev)mal_unregister_commac (dev->mal, &dev->commac); err_rel_deps:emac_put_deps (dev); - err_reg_unmap: -iounmap (dev->emacp); err_irq_unmap:if (dev->wol_irq)irq_dispose_mapping (dev->wol_irq);
@@ -3274,8 +3271,6 @@ static void emac_remove(struct platform_device *ofdev)mal_unregister_commac (dev->mal, &dev->commac);emac_put_deps (dev); -iounmap (dev->emacp); -if (dev->wol_irq)irq_dispose_mapping (dev->wol_irq); }
Signature · 1 line
2.46.0
PATCH 4/6 net: ibm: emac: remove mii_bus with devm
In reply to Rosen PenevSwitching to devm management of mii_bus allows to remove mdiobus_unregister calls and thus avoids needing a mii_bus global struct member.
--- drivers/net/ethernet/ibm/emac/core.c | 32 +++++++++++----------------- drivers/net/ethernet/ibm/emac/core.h | 1 - 2 files changed, 13 insertions(+), 20 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +13 -19
@@ -2580,6 +2580,7 @@ static const struct mii_phy_ops emac_dt_mdio_phy_ops = {static int emac_dt_mdio_probe (struct emac_instance *dev) { +struct mii_bus *bus;struct device_node *mii_np;int res;
@@ -2594,23 +2595,23 @@ static int emac_dt_mdio_probe(struct emac_instance *dev) goto put_node; } - dev->mii_bus =devm_mdiobus_alloc (&dev->ofdev->dev); -if (!dev->mii_bus) { + bus =devm_mdiobus_alloc (&dev->ofdev->dev); +if (!bus) { res = -ENOMEM ; goto put_node; } - dev->mii_bus->priv = dev->ndev; - dev->mii_bus->parent = dev->ndev->dev.parent; - dev->mii_bus->name ="emac_mdio" ; - dev->mii_bus->read = &emac_mii_bus_read; - dev->mii_bus->write = &emac_mii_bus_write; - dev->mii_bus->reset = &emac_mii_bus_reset; -snprintf (dev->mii_bus->id,MII_BUS_ID_SIZE ,"%s" , dev->ofdev->name); - res =of_mdiobus_register (dev->mii_bus, mii_np); + bus->priv = dev->ndev; + bus->parent = dev->ndev->dev.parent; + bus->name ="emac_mdio" ; + bus->read = &emac_mii_bus_read; + bus->write = &emac_mii_bus_write; + bus->reset = &emac_mii_bus_reset; +snprintf (bus->id,MII_BUS_ID_SIZE ,"%s" , dev->ofdev->name); + res =devm_of_mdiobus_register (&dev->ofdev->dev, bus, mii_np);if (res) {dev_err (&dev->ofdev->dev,"cannot register MDIO bus %s (%d)" , - dev->mii_bus->name, res); + bus->name, res); } put_node:
@@ -2656,8 +2657,6 @@ static int emac_dt_phy_probe(struct emac_instance *dev) res =emac_dt_mdio_probe (dev);if (!res) { res =emac_dt_phy_connect (dev, phy_handle); -if (res) -mdiobus_unregister (dev->mii_bus); } }
@@ -2697,10 +2696,8 @@ static int emac_init_phy(struct emac_instance *dev) res =of_phy_register_fixed_link (np); dev->phy_dev =of_phy_find_device (np); -if (res || !dev->phy_dev) { -mdiobus_unregister (dev->mii_bus); +if (res || !dev->phy_dev)return res ? res : -EINVAL ; - }emac_adjust_link (dev->ndev);put_device (&dev->phy_dev->mdio.dev); }
@@ -3262,9 +3259,6 @@ static void emac_remove(struct platform_device *ofdev)if (dev->phy_dev)phy_disconnect (dev->phy_dev); -if (dev->mii_bus) -mdiobus_unregister (dev->mii_bus); - busy_phy_map &= ~(1 << dev->phy.address);DBG (dev,"busy_phy_map now %#x" NL , busy_phy_map);
drivers/net/ethernet/ibm/emac/core.h +0 -1
@@ -189,7 +189,6 @@ struct emac_instance {struct mutex mdio_lock;/* Device-tree based phy configuration */ -struct mii_bus *mii_bus;struct phy_device *phy_dev;/* ZMII infos if any */
Signature · 1 line
2.46.0
PATCH 5/6 net: ibm: emac: use devm for register_netdev
In reply to Rosen PenevCleans it up automatically. No need to handle manually.
--- drivers/net/ethernet/ibm/emac/core.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +1 -3
@@ -3179,7 +3179,7 @@ static int emac_probe(struct platform_device *ofdev)netif_carrier_off (ndev); - err =register_netdev (ndev); + err =devm_register_netdev (&ofdev->dev, ndev);if (err) {printk (KERN_ERR "%pOF: failed to register net device (%d)!\n" , np, err);
@@ -3245,8 +3245,6 @@ static void emac_remove(struct platform_device *ofdev)DBG (dev,"remove" NL ); -unregister_netdev (dev->ndev); -cancel_work_sync (&dev->reset_work);if (emac_has_feature (dev,EMAC_FTR_HAS_TAH ))
Signature · 1 line
2.46.0
PATCH 6/6 net: ibm: emac: use netdev's phydev directly
In reply to Rosen PenevAvoids having to use own struct member.
--- drivers/net/ethernet/ibm/emac/core.c | 47 +++++++++++++--------------- drivers/net/ethernet/ibm/emac/core.h | 3 -- 2 files changed, 21 insertions(+), 29 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +21 -26
@@ -2459,7 +2459,7 @@ static int emac_read_uint_prop(struct device_node *np, const char *name,static void emac_adjust_link (struct net_device *ndev) {struct emac_instance *dev =netdev_priv (ndev); -struct phy_device *phy = dev->phy_dev; +struct phy_device *phy = ndev->phydev; dev->phy.autoneg = phy->autoneg; dev->phy.speed = phy->speed;
@@ -2510,22 +2510,20 @@ static int emac_mdio_phy_start_aneg(struct mii_phy *phy,static int emac_mdio_setup_aneg (struct mii_phy *phy,u32 advertise) {struct net_device *ndev = phy->dev; -struct emac_instance *dev =netdev_priv (ndev); phy->autoneg =AUTONEG_ENABLE ; phy->advertising = advertise; -return emac_mdio_phy_start_aneg (phy, dev->phy_dev); +return emac_mdio_phy_start_aneg (phy, ndev->phydev); }static int emac_mdio_setup_forced (struct mii_phy *phy,int speed,int fd) {struct net_device *ndev = phy->dev; -struct emac_instance *dev =netdev_priv (ndev); phy->autoneg =AUTONEG_DISABLE ; phy->speed = speed; phy->duplex = fd; -return emac_mdio_phy_start_aneg (phy, dev->phy_dev); +return emac_mdio_phy_start_aneg (phy, ndev->phydev); }static int emac_mdio_poll_link (struct mii_phy *phy)
@@ -2534,20 +2532,19 @@ static int emac_mdio_poll_link(struct mii_phy *phy)struct emac_instance *dev =netdev_priv (ndev);int res; - res =phy_read_status (dev->phy_dev); + res =phy_read_status (ndev->phydev);if (res) {dev_err (&dev->ofdev->dev,"link update failed (%d)." , res);return ethtool_op_get_link (ndev); } -return dev->phy_dev->link; +return ndev->phydev->link; }static int emac_mdio_read_link (struct mii_phy *phy) {struct net_device *ndev = phy->dev; -struct emac_instance *dev =netdev_priv (ndev); -struct phy_device *phy_dev = dev->phy_dev; +struct phy_device *phy_dev = ndev->phydev;int res; res =phy_read_status (phy_dev);
@@ -2564,10 +2561,9 @@ static int emac_mdio_read_link(struct mii_phy *phy)static int emac_mdio_init_phy (struct mii_phy *phy) {struct net_device *ndev = phy->dev; -struct emac_instance *dev =netdev_priv (ndev); -phy_start (dev->phy_dev); -return phy_init_hw (dev->phy_dev); +phy_start (ndev->phydev); +return phy_init_hw (ndev->phydev); }static const struct mii_phy_ops emac_dt_mdio_phy_ops = {
@@ -2622,26 +2618,28 @@ static int emac_dt_mdio_probe(struct emac_instance *dev)static int emac_dt_phy_connect (struct emac_instance *dev,struct device_node *phy_handle) { +struct phy_device *phy_dev = dev->ndev->phydev; + dev->phy.def =devm_kzalloc (&dev->ofdev->dev,sizeof (*dev->phy.def),GFP_KERNEL );if (!dev->phy.def)return -ENOMEM ; - dev->phy_dev =of_phy_connect (dev->ndev, phy_handle, &emac_adjust_link, + phy_dev =of_phy_connect (dev->ndev, phy_handle, &emac_adjust_link,0 , dev->phy_mode); -if (!dev->phy_dev) { +if (!phy_dev) {dev_err (&dev->ofdev->dev,"failed to connect to PHY.\n" );return -ENODEV ; } - dev->phy.def->phy_id = dev->phy_dev->drv->phy_id; - dev->phy.def->phy_id_mask = dev->phy_dev->drv->phy_id_mask; - dev->phy.def->name = dev->phy_dev->drv->name; + dev->phy.def->phy_id = phy_dev->drv->phy_id; + dev->phy.def->phy_id_mask = phy_dev->drv->phy_id_mask; + dev->phy.def->name = phy_dev->drv->name; dev->phy.def->ops = &emac_dt_mdio_phy_ops;ethtool_convert_link_mode_to_legacy_u32 (&dev->phy.features, - dev->phy_dev->supported); - dev->phy.address = dev->phy_dev->mdio.addr; - dev->phy.mode = dev->phy_dev->interface; + phy_dev->supported); + dev->phy.address = phy_dev->mdio.addr; + dev->phy.mode = phy_dev->interface;return 0 ; }
@@ -2695,11 +2693,11 @@ static int emac_init_phy(struct emac_instance *dev)return res; res =of_phy_register_fixed_link (np); - dev->phy_dev =of_phy_find_device (np); -if (res || !dev->phy_dev) + ndev->phydev =of_phy_find_device (np); +if (res || !ndev->phydev)return res ? res : -EINVAL ;emac_adjust_link (dev->ndev); -put_device (&dev->phy_dev->mdio.dev); +put_device (&ndev->phydev->mdio.dev); }return 0 ; }
@@ -3254,9 +3252,6 @@ static void emac_remove(struct platform_device *ofdev)if (emac_has_feature (dev,EMAC_FTR_HAS_ZMII ))zmii_detach (dev->zmii_dev, dev->zmii_port); -if (dev->phy_dev) -phy_disconnect (dev->phy_dev); - busy_phy_map &= ~(1 << dev->phy.address);DBG (dev,"busy_phy_map now %#x" NL , busy_phy_map);
drivers/net/ethernet/ibm/emac/core.h +0 -3
@@ -188,9 +188,6 @@ struct emac_instance {struct emac_instance *mdio_instance;struct mutex mdio_lock; -/* Device-tree based phy configuration */ -struct phy_device *phy_dev; -/* ZMII infos if any */ u32 zmii_ph;u32 zmii_port;
Signature · 1 line
2.46.0
Quoted 6 lines · Rosen Penev
It's a very old driver with a lot of potential for cleaning up code to modern standards. This was a simple one dealing with mostly the probe function and adding some devm to it.
All patches were tested on a Cisco Meraki MX60W. Boot and Shutdown/Reboot showed no warnings.
This is a 12 year old PowerPC system. Cool you found one. Looks like OpenWRT still supports it.
Andrwq
Quoted 11 lines · Andrew Lunn
Quoted 6 lines · Rosen Penev
It's a very old driver with a lot of potential for cleaning up code to modern standards. This was a simple one dealing with mostly the probe function and adding some devm to it.
All patches were tested on a Cisco Meraki MX60W. Boot and Shutdown/Reboot showed no warnings.
This is a 12 year old PowerPC system. Cool you found one. Looks like OpenWRT still supports it.
I recently bought one in an attempt to get qca8k working on it. Unfortunately the bootloader disables all ports except WAN and the driver has a hack that disables MDIO 0-3, which qca8k seems to need. I tried removing it but then mdiobus_alloc fails with error code -5.
qca8k only manages to bring up port 5 (WAN).
Quoted 2 lines
Andrwq