kernel: backport mdio_find_bus from 5.10.x
[openwrt/openwrt.git] / target / linux / generic / backport-5.4 / 771-mdio-bus-add-generic-find-bus.patch
1 From ce69e2162f158d9d4a0e513971d02dabc7d14cb7 Mon Sep 17 00:00:00 2001
2 From: Jeremy Linton <jeremy.linton@arm.com>
3 Date: Mon, 24 Feb 2020 16:53:58 -0600
4 Subject: [PATCH] mdio_bus: Add generic mdio_find_bus()
5
6 It appears most ethernet drivers follow one of two main strategies
7 for mdio bus/phy management. A monolithic model where the net driver
8 itself creates, probes and uses the phy, and one where an external
9 mdio/phy driver instantiates the mdio bus/phy and the net driver
10 only attaches to a known phy. Usually in this latter model the phys
11 are discovered via DT relationships or simply phy name/address
12 hardcoding.
13
14 This is a shame because modern well behaved mdio buses are self
15 describing and can be probed. The mdio layer itself is fully capable
16 of this, yet there isn't a clean way for a standalone net driver
17 to attach and enumerate the discovered devices. This is because
18 outside of of_mdio_find_bus() there isn't a straightforward way
19 to acquire the mii_bus pointer.
20
21 So, lets add a mdio_find_bus which can return the mii_bus based
22 only on its name.
23
24 Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
25 Acked-by: Florian Fainelli <f.fainelli@gmail.com>
26 Signed-off-by: David S. Miller <davem@davemloft.net>
27 ---
28 drivers/net/phy/mdio_bus.c | 17 +++++++++++++++++
29 include/linux/phy.h | 1 +
30 2 files changed, 18 insertions(+)
31
32 --- a/drivers/net/phy/mdio_bus.c
33 +++ b/drivers/net/phy/mdio_bus.c
34 @@ -260,6 +260,23 @@ static struct class mdio_bus_class = {
35 .dev_release = mdiobus_release,
36 };
37
38 +/**
39 + * mdio_find_bus - Given the name of a mdiobus, find the mii_bus.
40 + * @mdio_bus_np: Pointer to the mii_bus.
41 + *
42 + * Returns a reference to the mii_bus, or NULL if none found. The
43 + * embedded struct device will have its reference count incremented,
44 + * and this must be put_deviced'ed once the bus is finished with.
45 + */
46 +struct mii_bus *mdio_find_bus(const char *mdio_name)
47 +{
48 + struct device *d;
49 +
50 + d = class_find_device_by_name(&mdio_bus_class, mdio_name);
51 + return d ? to_mii_bus(d) : NULL;
52 +}
53 +EXPORT_SYMBOL(mdio_find_bus);
54 +
55 #if IS_ENABLED(CONFIG_OF_MDIO)
56 /**
57 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
58 --- a/include/linux/phy.h
59 +++ b/include/linux/phy.h
60 @@ -273,6 +273,7 @@ static inline struct mii_bus *devm_mdiob
61 return devm_mdiobus_alloc_size(dev, 0);
62 }
63
64 +struct mii_bus *mdio_find_bus(const char *mdio_name);
65 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus);
66 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
67