Skip to contentSkip to conversation

net: improve devres helpers

PATCH Superseded Bartosz Golaszewski 3 participants 17 messages Jun 22, 2020 – Jun 25, 2020

Subsystems (10)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover net: improve devres helpers

From: Bartosz Golaszewski <[email protected]>

When I first submitted the series adding devm_register_netdev() I was told during review that it should check if the underlying struct net_device is managed too before proceeding. I initially accepted this as the right approach but in the back of my head something seemed wrong about this. I started looking around and noticed how devm_mdiobus_register() is implemented.

It turned out that struct mii_bus contains information about whether it's managed or not and the release callback of devm_mdiobus_alloc() is responsible for calling mdiobus_unregister(). This seems wrong to me as managed structures shouldn't care about who manages them. It's devres' code task to correctly undo whatever it registers/allocates.

With this series I propose to make the release callbacks of mdiobus devm helpers only release the resources they actually allocate themselves as it the standard in devm routines. I also propose to not check whether the structures passed to devm_mdiobus_register() and devm_register_netdev() are already managed as they could have been allocated over devres as part of bigger memory chunk. I see this as an unnecessary limitation.

First two patches aim at removing the only use of devm_mdiobus_free(). It modifies the ixgbe driver. I only compile tested it as I don't have the relevant hw.

Next two patches relax devm_register_netdev() - we stop checking whether struct net_device was registered using devm_etherdev_alloc().

We then document the mdio devres helper that's missing in devres.rst list and un-inline the current implementation of devm_mdiobus_register().

Patch 8 re-implements the devres helpers for mdio conforming to common devres patterns.

Patches 9 and 10 provide devm_of_mdiobus_register() and the last patch adds its first user.

Bartosz Golaszewski (11):
net: ethernet: ixgbe: check the return value of ixgbe_mii_bus_init()
net: ethernet: ixgbe: don't call devm_mdiobus_free()
net: devres: relax devm_register_netdev()
net: devres: rename the release callback of devm_register_netdev()
Documentation: devres: add missing mdio helper
phy: un-inline devm_mdiobus_register()
phy: mdio: add kerneldoc for __devm_mdiobus_register()
net: phy: don't abuse devres in devm_mdiobus_register()
of: mdio: remove the 'extern' keyword from function declarations
of: mdio: provide devm_of_mdiobus_register()
net: ethernet: mtk-star-emac: use devm_of_mdiobus_register()

 .../driver-api/driver-model/devres.rst        |  3 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_main.c |  6 +-
 drivers/net/ethernet/intel/ixgbe/ixgbe_phy.c  | 14 +---
 drivers/net/ethernet/mediatek/mtk_star_emac.c | 13 +--
 drivers/net/ethernet/realtek/r8169_main.c     |  2 +-
 drivers/net/phy/Makefile                      |  2 +-
 drivers/net/phy/mdio_bus.c                    | 73 ----------------
 drivers/net/phy/mdio_devres.c                 | 83 +++++++++++++++++++
 drivers/of/of_mdio.c                          | 43 ++++++++++
 include/linux/of_mdio.h                       | 40 ++++-----
 include/linux/phy.h                           | 21 +----
 net/devres.c                                  | 23 +----
 12 files changed, 167 insertions(+), 156 deletions(-)
 create mode 100644 drivers/net/phy/mdio_devres.c
Signature · 1 line
2.26.1
#1 lore raw

PATCH 1/11 net: ethernet: ixgbe: check the return value of ixgbe_mii_bus_init()

From: Bartosz Golaszewski <[email protected]>

This function may fail. Check its return value and propagate the error code.

---
 drivers/net/ethernet/intel/ixgbe/ixgbe_main.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/ethernet/intel/ixgbe/ixgbe_main.c +5 -1
index f162b8b8f345..4ec4eeb9736b 100644
@@ -11167,10 +11167,14 @@ static int ixgbe_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 			IXGBE_LINK_SPEED_10GB_FULL | IXGBE_LINK_SPEED_1GB_FULL,
 			true);
 
-	ixgbe_mii_bus_init(hw);
+	err = ixgbe_mii_bus_init(hw);
+	if (err)
+		goto err_netdev;
 
 	return 0;
 
+err_netdev:
+	unregister_netdev(netdev);
 err_register:
 	ixgbe_release_hw_control(adapter);
 	ixgbe_clear_interrupt_scheme(adapter);
Signature · 1 line
2.26.1
#2 lore raw

PATCH 2/11 net: ethernet: ixgbe: don't call devm_mdiobus_free()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

The idea behind devres is that the release callbacks are called if probe fails. As we now check the return value of ixgbe_mii_bus_init(), we can drop the call devm_mdiobus_free() in error path as the release callback will be called automatically.

---
 drivers/net/ethernet/intel/ixgbe/ixgbe_phy.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
drivers/net/ethernet/intel/ixgbe/ixgbe_phy.c +3 -11
index 2fb97967961c..7980d7265e10 100644
@@ -905,7 +905,6 @@ s32 ixgbe_mii_bus_init(struct ixgbe_hw *hw)
 	struct pci_dev *pdev = adapter->pdev;
 	struct device *dev = &adapter->netdev->dev;
 	struct mii_bus *bus;
-	int err = -ENODEV;
 
 	bus = devm_mdiobus_alloc(dev);
 	if (!bus)
@@ -923,7 +922,7 @@ s32 ixgbe_mii_bus_init(struct ixgbe_hw *hw)
 	case IXGBE_DEV_ID_X550EM_A_1G_T:
 	case IXGBE_DEV_ID_X550EM_A_1G_T_L:
 		if (!ixgbe_x550em_a_has_mii(hw))
-			goto ixgbe_no_mii_bus;
+			return -ENODEV;
 		bus->read = &ixgbe_x550em_a_mii_bus_read;
 		bus->write = &ixgbe_x550em_a_mii_bus_write;
 		break;
@@ -948,15 +947,8 @@ s32 ixgbe_mii_bus_init(struct ixgbe_hw *hw)
 	 */
 	hw->phy.mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22;
 
-	err = mdiobus_register(bus);
-	if (!err) {
-		adapter->mii_bus = bus;
-		return 0;
-	}
-
-ixgbe_no_mii_bus:
-	devm_mdiobus_free(dev, bus);
-	return err;
+	adapter->mii_bus = bus;
+	return mdiobus_register(bus);
 }
 
 /**
Signature · 1 line
2.26.1
#3 lore raw

PATCH 3/11 net: devres: relax devm_register_netdev()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This devres helper registers a release callback that only unregisters the net_device. It works perfectly fine with netdev structs that are not managed on their own. There's no reason to check this - drop the warning.

---
 net/devres.c | 19 +------------------
 1 file changed, 1 insertion(+), 18 deletions(-)
net/devres.c +1 -18
index 57a6a88d11f6..1583ccb207c0 100644
@@ -46,14 +46,6 @@ static void devm_netdev_release(struct device *dev, void *this)
 	unregister_netdev(res->ndev);
 }
 
-static int netdev_devres_match(struct device *dev, void *this, void *match_data)
-{
-	struct net_device_devres *res = this;
-	struct net_device *ndev = match_data;
-
-	return ndev == res->ndev;
-}
-
 /**
  *	devm_register_netdev - resource managed variant of register_netdev()
  *	@dev: managing device for this netdev - usually the parent device
@@ -61,22 +53,13 @@ static int netdev_devres_match(struct device *dev, void *this, void *match_data)
  *
  *	This is a devres variant of register_netdev() for which the unregister
  *	function will be call automatically when the managing device is
- *	detached. Note: the net_device used must also be resource managed by
- *	the same struct device.
+ *	detached.
  */
 int devm_register_netdev(struct device *dev, struct net_device *ndev)
 {
 	struct net_device_devres *dr;
 	int ret;
 
-	/* struct net_device must itself be managed. For now a managed netdev
-	 * can only be allocated by devm_alloc_etherdev_mqs() so the check is
-	 * straightforward.
-	 */
-	if (WARN_ON(!devres_find(dev, devm_free_netdev,
-				 netdev_devres_match, ndev)))
-		return -EINVAL;
-
 	dr = devres_alloc(devm_netdev_release, sizeof(*dr), GFP_KERNEL);
 	if (!dr)
 		return -ENOMEM;
Signature · 1 line
2.26.1
maintainer
#4 lore raw

Re: [PATCH 03/11] net: devres: relax devm_register_netdev()

Quoted 8 lines · On Mon, 22 Jun 2020 12:00:48 +0200 Bartosz Golaszewski wrote:

From: Bartosz Golaszewski <[email protected]>

This devres helper registers a release callback that only unregisters the net_device. It works perfectly fine with netdev structs that are not managed on their own. There's no reason to check this - drop the warning.

I think the reasoning for this suggestion was to catch possible UAF errors. The netdev doesn't necessarily has to be from devm_alloc_* but it has to be part of devm-ed memory or memory which is freed after driver's remove callback.

Are there cases in practice where you've seen the netdev not being devm allocated?

#5 lore raw

Re: [PATCH 03/11] net: devres: relax devm_register_netdev()

wt., 23 cze 2020 o 00:49 Jakub Kicinski <[email protected]> napisa?(a):

Quoted 9 lines · On Mon, 22 Jun 2020 12:00:48 +0200 Bartosz Golaszewski wrote:
Quoted 7 lines

From: Bartosz Golaszewski <[email protected]>

This devres helper registers a release callback that only unregisters the net_device. It works perfectly fine with netdev structs that are not managed on their own. There's no reason to check this - drop the warning.

Quoted 7 lines
Quoted 1 line

I think the reasoning for this suggestion was to catch possible UAF errors. The netdev doesn't necessarily has to be from devm_alloc_* but it has to be part of devm-ed memory or memory which is freed after driver's remove callback.

Yes I understand that UAF was the concern here, but this limitation is unnecessary. In its current form devm_register_netdev() only works for struct net_device allocated with devm_alloc_etherdev(). Meanwhile calling alloc_netdev() (which doesn't have its devm counterpart yet - I may look into it shortly), then registering a devm action with devm_add_action_or_reset() which would free this memory is a perfectly fine use case. This patch would make it possible.

Quoted 2 lines

Are there cases in practice where you've seen the netdev not being devm allocated?

As I said above - alloc_netdev() used by wireless, can, usb etc. drivers doesn't have a devres variant.

Bartosz

maintainer
#6 lore raw

Re: [PATCH 03/11] net: devres: relax devm_register_netdev()

Quoted 17 lines · On Tue, 23 Jun 2020 11:12:24 +0200 Bartosz Golaszewski wrote:

wt., 23 cze 2020 o 00:49 Jakub Kicinski <[email protected]> napisa?(a):

Quoted 8 lines · On Mon, 22 Jun 2020 12:00:48 +0200 Bartosz Golaszewski wrote:
Quoted 7 lines

From: Bartosz Golaszewski <[email protected]>

This devres helper registers a release callback that only unregisters the net_device. It works perfectly fine with netdev structs that are not managed on their own. There's no reason to check this - drop the warning.

Quoted 7 lines
Quoted 1 line

I think the reasoning for this suggestion was to catch possible UAF errors. The netdev doesn't necessarily has to be from devm_alloc_* but it has to be part of devm-ed memory or memory which is freed after driver's remove callback.

Quoted 5 lines

Yes I understand that UAF was the concern here, but this limitation is unnecessary. In its current form devm_register_netdev() only works for struct net_device allocated with devm_alloc_etherdev(). Meanwhile calling alloc_netdev() (which doesn't have its devm counterpart yet - I may look into it shortly),

If resource managed alloc_netdev() is needed devm_alloc_netdev() can be created, and even reuse devm_free_netdev() so no changes to the warning are even necessary for such extension.

Quoted 3 lines

then registering a devm action with devm_add_action_or_reset() which would free this memory is a perfectly fine use case. This patch would make it possible.

alloc_netdev() + devm_add_action makes no sense in the upstream kernel, just add the appropriate helper, we care little about out of tree code.

Quoted 5 lines
Quoted 2 lines

Are there cases in practice where you've seen the netdev not being devm allocated?

As I said above - alloc_netdev() used by wireless, can, usb etc. drivers doesn't have a devres variant.

#7 lore raw

PATCH 4/11 net: devres: rename the release callback of devm_register_netdev()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Make it an explicit counterpart to devm_register_netdev() just like we do with devm_free_netdev() for better clarity.

---
 net/devres.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/devres.c +2 -2
index 1583ccb207c0..d7aa92243844 100644
@@ -39,7 +39,7 @@ struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv,
 }
 EXPORT_SYMBOL(devm_alloc_etherdev_mqs);
 
-static void devm_netdev_release(struct device *dev, void *this)
+static void devm_unregister_netdev(struct device *dev, void *this)
 {
 	struct net_device_devres *res = this;
 
@@ -60,7 +60,7 @@ int devm_register_netdev(struct device *dev, struct net_device *ndev)
 	struct net_device_devres *dr;
 	int ret;
 
-	dr = devres_alloc(devm_netdev_release, sizeof(*dr), GFP_KERNEL);
+	dr = devres_alloc(devm_unregister_netdev, sizeof(*dr), GFP_KERNEL);
 	if (!dr)
 		return -ENOMEM;
 
Signature · 1 line
2.26.1
#8 lore raw

PATCH 5/11 Documentation: devres: add missing mdio helper

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We have a devres variant of mdiobus_register() but it's not listed in devres.rst. Add it under other mdio devm functions.

---
 Documentation/driver-api/driver-model/devres.rst | 1 +
 1 file changed, 1 insertion(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
index e0b58c392e4f..5463fc8a60c1 100644
@@ -343,6 +343,7 @@ MDIO
   devm_mdiobus_alloc()
   devm_mdiobus_alloc_size()
   devm_mdiobus_free()
+  devm_mdiobus_register()
 
 MEM
   devm_free_pages()
Signature · 1 line
2.26.1
#9 lore raw

PATCH 6/11 phy: un-inline devm_mdiobus_register()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Functions should only be static inline if they're very short. This devres helper is already over 10 lines and it will grow soon as we'll be improving upon its approach. Pull it into mdio_devres.c.

---
 drivers/net/phy/Makefile      |  2 +-
 drivers/net/phy/mdio_devres.c | 18 ++++++++++++++++++
 include/linux/phy.h           | 15 ++-------------
 3 files changed, 21 insertions(+), 14 deletions(-)
 create mode 100644 drivers/net/phy/mdio_devres.c
drivers/net/phy/Makefile +1 -1
index dc9e53b511d6..896afdcac437 100644
@@ -3,7 +3,7 @@
 
 libphy-y			:= phy.o phy-c45.o phy-core.o phy_device.o \
 				   linkmode.o
-mdio-bus-y			+= mdio_bus.o mdio_device.o
+mdio-bus-y			+= mdio_bus.o mdio_device.o mdio_devres.o
 
 ifdef CONFIG_MDIO_DEVICE
 obj-y				+= mdio-boardinfo.o
drivers/net/phy/mdio_devres.c new file +18 -0
new file mode 100644
index 000000000000..f0b4b6cfe5e3
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <linux/phy.h>
+
+int __devm_mdiobus_register(struct mii_bus *bus, struct module *owner)
+{
+	int ret;
+
+	if (!bus->is_managed)
+		return -EPERM;
+
+	ret = __mdiobus_register(bus, owner);
+	if (!ret)
+		bus->is_managed_registered = 1;
+
+	return ret;
+}
+EXPORT_SYMBOL(__devm_mdiobus_register);
include/linux/phy.h +2 -13
index 8c05d0fb5c00..62149945c5b3 100644
@@ -313,20 +313,9 @@ static inline struct mii_bus *mdiobus_alloc(void)
 }
 
 int __mdiobus_register(struct mii_bus *bus, struct module *owner);
+int __devm_mdiobus_register(struct mii_bus *bus, struct module *owner);
 #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
-static inline int devm_mdiobus_register(struct mii_bus *bus)
-{
-	int ret;
-
-	if (!bus->is_managed)
-		return -EPERM;
-
-	ret = mdiobus_register(bus);
-	if (!ret)
-		bus->is_managed_registered = 1;
-
-	return ret;
-}
+#define devm_mdiobus_register(bus) __devm_mdiobus_register(bus, THIS_MODULE)
 
 void mdiobus_unregister(struct mii_bus *bus);
 void mdiobus_free(struct mii_bus *bus);
Signature · 1 line
2.26.1
#10 lore raw

Re: [PATCH 06/11] phy: un-inline devm_mdiobus_register()

Quoted 6 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Functions should only be static inline if they're very short. This devres helper is already over 10 lines and it will grow soon as we'll be improving upon its approach. Pull it into mdio_devres.c.

Quoted 7 lines
---
 drivers/net/phy/Makefile      |  2 +-
 drivers/net/phy/mdio_devres.c | 18 ++++++++++++++++++
 include/linux/phy.h           | 15 ++-------------
 3 files changed, 21 insertions(+), 14 deletions(-)
 create mode 100644 drivers/net/phy/mdio_devres.c

This would likely require an update to the MAINTAINERS file for this new file to be picked up by the correct entry.

Signature · 1 line
Florian
#11 lore raw

Re: [PATCH 06/11] phy: un-inline devm_mdiobus_register()

Quoted 16 lines · Florian Fainelli
Quoted 6 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Functions should only be static inline if they're very short. This devres helper is already over 10 lines and it will grow soon as we'll be improving upon its approach. Pull it into mdio_devres.c.

Quoted 7 lines
---
 drivers/net/phy/Makefile      |  2 +-
 drivers/net/phy/mdio_devres.c | 18 ++++++++++++++++++
 include/linux/phy.h           | 15 ++-------------
 3 files changed, 21 insertions(+), 14 deletions(-)
 create mode 100644 drivers/net/phy/mdio_devres.c
Quoted 2 lines

This would likely require an update to the MAINTAINERS file for this new file to be picked up by the correct entry.

It's already included in drivers/net/phy/ in the ETHERNET PHY LIBRARY entry.

Bartosz

#12 lore raw

PATCH 7/11 phy: mdio: add kerneldoc for __devm_mdiobus_register()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This function is not documented. Add a short kerneldoc description.

---
 drivers/net/phy/mdio_devres.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/net/phy/mdio_devres.c +7 -0
index f0b4b6cfe5e3..3ee887733d4a 100644
@@ -2,6 +2,13 @@
 
 #include <linux/phy.h>
 
+/**
+ * __devm_mdiobus_register - Resource-managed variant of mdiobus_register()
+ * @bus:	MII bus structure to register
+ * @owner:	Owning module
+ *
+ * Returns 0 on success, negative error number on failure.
+ */
 int __devm_mdiobus_register(struct mii_bus *bus, struct module *owner)
 {
 	int ret;
Signature · 1 line
2.26.1
#13 lore raw

PATCH 8/11 net: phy: don't abuse devres in devm_mdiobus_register()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We currently have two managed helpers for mdiobus - devm_mdiobus_alloc() and devm_mdiobus_register(). The idea behind devres is that the release callback releases whatever resource the devm function allocates. In the mdiobus case however there's no devres associated with the device by devm_mdiobus_register(). Instead the release callback for devm_mdiobus_alloc(): _devm_mdiobus_free() unregisters the device if it is marked as managed.

This all seems wrong. The managed structure shouldn't need to know or care about whether it's managed or not - and this is the case now for struct mii_bus. The devres wrapper should be opaque to the managed resource.

This changeset makes devm_mdiobus_alloc() and devm_mdiobus_register() conform to common devres standards: devm_mdiobus_alloc() allocates a devres structure and registers a callback that will call mdiobus_free(). __devm_mdiobus_register() allocated another devres and registers a callback that will unregister the bus. Similarily to how we modified devm_register_netdev() - we're not checking whether struct mii_bus is managed - it could have been allocated as part of a bigger structure.

---
 .../driver-api/driver-model/devres.rst        |  1 -
 drivers/net/ethernet/realtek/r8169_main.c     |  2 +-
 drivers/net/phy/mdio_bus.c                    | 73 -------------------
 drivers/net/phy/mdio_devres.c                 | 70 ++++++++++++++++--
 include/linux/phy.h                           | 10 +--
 5 files changed, 69 insertions(+), 87 deletions(-)
Documentation/driver-api/driver-model/devres.rst +0 -1
index 5463fc8a60c1..e0333d66a7f4 100644
@@ -342,7 +342,6 @@ LED
 MDIO
   devm_mdiobus_alloc()
   devm_mdiobus_alloc_size()
-  devm_mdiobus_free()
   devm_mdiobus_register()
 
 MEM
drivers/net/ethernet/realtek/r8169_main.c +1 -1
index dad84ecf5a77..34d5797695ba 100644
@@ -5100,7 +5100,7 @@ static int r8169_mdio_register(struct rtl8169_private *tp)
 	new_bus->read = r8169_mdio_read_reg;
 	new_bus->write = r8169_mdio_write_reg;
 
-	ret = devm_mdiobus_register(new_bus);
+	ret = devm_mdiobus_register(&pdev->dev, new_bus);
 	if (ret)
 		return ret;
 
drivers/net/phy/mdio_bus.c +0 -73

79 lines not on this page · raw message

drivers/net/phy/mdio_devres.c +64 -6

89 lines not on this page · raw message

include/linux/phy.h +4 -6
index 62149945c5b3..fa6697edefb3 100644
@@ -260,9 +260,6 @@ struct mii_bus {
 	int (*reset)(struct mii_bus *bus);
 	struct mdio_bus_stats stats[PHY_MAX_ADDR];
 
-	unsigned int is_managed:1;	/* is device-managed */
-	unsigned int is_managed_registered:1;
-
 	/*
 	 * A lock to ensure that only one thing can read/write
 	 * the MDIO bus at a time
@@ -313,9 +310,11 @@ static inline struct mii_bus *mdiobus_alloc(void)
 }
 
 int __mdiobus_register(struct mii_bus *bus, struct module *owner);
-int __devm_mdiobus_register(struct mii_bus *bus, struct module *owner);
+int __devm_mdiobus_register(struct device *dev, struct mii_bus *bus,
+			    struct module *owner);
 #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
-#define devm_mdiobus_register(bus) __devm_mdiobus_register(bus, THIS_MODULE)
+#define devm_mdiobus_register(dev, bus) \
+		__devm_mdiobus_register(dev, bus, THIS_MODULE)
 
 void mdiobus_unregister(struct mii_bus *bus);
 void mdiobus_free(struct mii_bus *bus);
@@ -326,7 +325,6 @@ static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
 }
 
 struct mii_bus *mdio_find_bus(const char *mdio_name);
-void devm_mdiobus_free(struct device *dev, struct mii_bus *bus);
 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
 
 #define PHY_INTERRUPT_DISABLED	false
Signature · 1 line
2.26.1
#14 lore raw

PATCH 9/11 of: mdio: remove the 'extern' keyword from function declarations

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

The 'extern' keyword in headers doesn't have any benefit. Remove them all from the of_mdio.h header.

---
 include/linux/of_mdio.h | 37 ++++++++++++++++++-------------------
 1 file changed, 18 insertions(+), 19 deletions(-)
include/linux/of_mdio.h +18 -19
index 0f61a4ac6bcf..ba8e157f24ad 100644
@@ -12,27 +12,26 @@
 #include <linux/of.h>
 
 #if IS_ENABLED(CONFIG_OF_MDIO)
-extern bool of_mdiobus_child_is_phy(struct device_node *child);
-extern int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np);
-extern struct phy_device *of_phy_find_device(struct device_node *phy_np);
-extern struct phy_device *of_phy_connect(struct net_device *dev,
-					 struct device_node *phy_np,
-					 void (*hndlr)(struct net_device *),
-					 u32 flags, phy_interface_t iface);
-extern struct phy_device *
+bool of_mdiobus_child_is_phy(struct device_node *child);
+int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np);
+struct phy_device *of_phy_find_device(struct device_node *phy_np);
+struct phy_device *
+of_phy_connect(struct net_device *dev, struct device_node *phy_np,
+	       void (*hndlr)(struct net_device *), u32 flags,
+	       phy_interface_t iface);
+struct phy_device *
 of_phy_get_and_connect(struct net_device *dev, struct device_node *np,
 		       void (*hndlr)(struct net_device *));
-struct phy_device *of_phy_attach(struct net_device *dev,
-				 struct device_node *phy_np, u32 flags,
-				 phy_interface_t iface);
-
-extern struct mii_bus *of_mdio_find_bus(struct device_node *mdio_np);
-extern int of_phy_register_fixed_link(struct device_node *np);
-extern void of_phy_deregister_fixed_link(struct device_node *np);
-extern bool of_phy_is_fixed_link(struct device_node *np);
-extern int of_mdiobus_phy_device_register(struct mii_bus *mdio,
-				     struct phy_device *phy,
-				     struct device_node *child, u32 addr);
+struct phy_device *
+of_phy_attach(struct net_device *dev, struct device_node *phy_np,
+	      u32 flags, phy_interface_t iface);
+
+struct mii_bus *of_mdio_find_bus(struct device_node *mdio_np);
+int of_phy_register_fixed_link(struct device_node *np);
+void of_phy_deregister_fixed_link(struct device_node *np);
+bool of_phy_is_fixed_link(struct device_node *np);
+int of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy,
+				   struct device_node *child, u32 addr);
 
 static inline int of_mdio_parse_addr(struct device *dev,
 				     const struct device_node *np)
Signature · 1 line
2.26.1
#15 lore raw

PATCH 10/11 of: mdio: provide devm_of_mdiobus_register()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Implement a managed variant of of_mdiobus_register(). We need to reimplement the devres structure and the release callback because we can't put this function in drivers/net/phy/mdio_devres.c or we'd hit circular dependencies between module symbols. We also don't want to build this bit if OF is not selected in Kconfig.

---
 .../driver-api/driver-model/devres.rst        |  1 +
 drivers/of/of_mdio.c                          | 43 +++++++++++++++++++
 include/linux/of_mdio.h                       |  3 ++
 3 files changed, 47 insertions(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
index e0333d66a7f4..eaaaafc21134 100644
@@ -343,6 +343,7 @@ MDIO
   devm_mdiobus_alloc()
   devm_mdiobus_alloc_size()
   devm_mdiobus_register()
+  devm_of_mdiobus_register()
 
 MEM
   devm_free_pages()
drivers/of/of_mdio.c +43 -0

49 lines not on this page · raw message

include/linux/of_mdio.h +3 -0
index ba8e157f24ad..1efb88d9f892 100644
@@ -8,12 +8,15 @@
 #ifndef __LINUX_OF_MDIO_H
 #define __LINUX_OF_MDIO_H
 
+#include <linux/device.h>
 #include <linux/phy.h>
 #include <linux/of.h>
 
 #if IS_ENABLED(CONFIG_OF_MDIO)
 bool of_mdiobus_child_is_phy(struct device_node *child);
 int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np);
+int devm_of_mdiobus_register(struct device *dev, struct mii_bus *mdio,
+			     struct device_node *np);
 struct phy_device *of_phy_find_device(struct device_node *phy_np);
 struct phy_device *
 of_phy_connect(struct net_device *dev, struct device_node *phy_np,
Signature · 1 line
2.26.1
#16 lore raw

PATCH 11/11 net: ethernet: mtk-star-emac: use devm_of_mdiobus_register()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Shrink the code by using the managed variant of of_mdiobus_register().

---
 drivers/net/ethernet/mediatek/mtk_star_emac.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
drivers/net/ethernet/mediatek/mtk_star_emac.c +1 -12
index 3e765bdcf9e1..13250553263b 100644
@@ -1389,7 +1389,7 @@ static int mtk_star_mdio_init(struct net_device *ndev)
 	priv->mii->write = mtk_star_mdio_write;
 	priv->mii->priv = priv;
 
-	ret = of_mdiobus_register(priv->mii, mdio_node);
+	ret = devm_of_mdiobus_register(dev, priv->mii, mdio_node);
 
 out_put_node:
 	of_node_put(mdio_node);
@@ -1441,13 +1441,6 @@ static void mtk_star_clk_disable_unprepare(void *data)
 	clk_bulk_disable_unprepare(MTK_STAR_NCLKS, priv->clks);
 }
 
-static void mtk_star_mdiobus_unregister(void *data)
-{
-	struct mtk_star_priv *priv = data;
-
-	mdiobus_unregister(priv->mii);
-}
-
 static int mtk_star_probe(struct platform_device *pdev)
 {
 	struct device_node *of_node;
@@ -1549,10 +1542,6 @@ static int mtk_star_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	ret = devm_add_action_or_reset(dev, mtk_star_mdiobus_unregister, priv);
-	if (ret)
-		return ret;
-
 	ret = eth_platform_get_mac_address(dev, ndev->dev_addr);
 	if (ret || !is_valid_ether_addr(ndev->dev_addr))
 		eth_hw_addr_random(ndev);
Signature · 1 line
2.26.1