diff mbox series

[net-next,v6,4/6] net: phy: introduce phy_find_by_mdio_handle()

Message ID 20200711065600.9448-5-calvin.johnson@oss.nxp.com
State Changes Requested
Delegated to: David Miller
Headers show
Series [net-next,v6,1/6] Documentation: ACPI: DSD: Document MDIO PHY | expand

Commit Message

Calvin Johnson July 11, 2020, 6:55 a.m. UTC
The PHYs on an mdiobus are probed and registered using mdiobus_register().
Later, for connecting these PHYs to MAC, the PHYs registered on the
mdiobus have to be referenced.

For each MAC node, a property "mdio-handle" is used to reference the
MDIO bus on which the PHYs are registered. On getting hold of the MDIO
bus, use phy_find_by_fwnode() to get the PHY connected to the MAC.

Introduce fwnode_mdio_find_bus() to find the mii_bus that corresponds
to given mii_bus fwnode.

Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com>

---

Changes in v6: None
Changes in v5:
- rename phy_find_by_fwnode() to phy_find_by_mdio_handle()
- add docment for phy_find_by_mdio_handle()
- error out DT in phy_find_by_mdio_handle()
- clean up err return

Changes in v4:
- release fwnode_mdio after use
- return ERR_PTR instead of NULL

Changes in v3:
- introduce fwnode_mdio_find_bus()
- renamed and improved phy_find_by_fwnode()

Changes in v2: None

 drivers/net/phy/mdio_bus.c   | 25 ++++++++++++++++++++++
 drivers/net/phy/phy_device.c | 40 ++++++++++++++++++++++++++++++++++++
 include/linux/phy.h          |  2 ++
 3 files changed, 67 insertions(+)

Comments

Florian Fainelli July 11, 2020, 9:41 p.m. UTC | #1
On 7/10/2020 11:55 PM, Calvin Johnson wrote:
> The PHYs on an mdiobus are probed and registered using mdiobus_register().
> Later, for connecting these PHYs to MAC, the PHYs registered on the
> mdiobus have to be referenced.
> 
> For each MAC node, a property "mdio-handle" is used to reference the
> MDIO bus on which the PHYs are registered. On getting hold of the MDIO
> bus, use phy_find_by_fwnode() to get the PHY connected to the MAC.
> 
> Introduce fwnode_mdio_find_bus() to find the mii_bus that corresponds
> to given mii_bus fwnode.
> 
> Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com>
> 
> ---
> 
> Changes in v6: None
> Changes in v5:
> - rename phy_find_by_fwnode() to phy_find_by_mdio_handle()
> - add docment for phy_find_by_mdio_handle()
> - error out DT in phy_find_by_mdio_handle()
> - clean up err return
> 
> Changes in v4:
> - release fwnode_mdio after use
> - return ERR_PTR instead of NULL
> 
> Changes in v3:
> - introduce fwnode_mdio_find_bus()
> - renamed and improved phy_find_by_fwnode()
> 
> Changes in v2: None
> 
>  drivers/net/phy/mdio_bus.c   | 25 ++++++++++++++++++++++
>  drivers/net/phy/phy_device.c | 40 ++++++++++++++++++++++++++++++++++++
>  include/linux/phy.h          |  2 ++
>  3 files changed, 67 insertions(+)
> 
> diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
> index 8610f938f81f..d9597c5b55ae 100644
> --- a/drivers/net/phy/mdio_bus.c
> +++ b/drivers/net/phy/mdio_bus.c
> @@ -435,6 +435,31 @@ struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
>  }
>  EXPORT_SYMBOL(of_mdio_find_bus);
>  
> +/**
> + * fwnode_mdio_find_bus - Given an mii_bus fwnode, find the mii_bus.
> + * @mdio_bus_fwnode: fwnode of the mii_bus.
> + *
> + * Returns a reference to the mii_bus, or NULL if none found.  The
> + * embedded struct device will have its reference count incremented,
> + * and this must be put once the bus is finished with.
> + *
> + * Because the association of a fwnode and mii_bus is made via
> + * mdiobus_register(), the mii_bus cannot be found before it is
> + * registered with mdiobus_register().
> + *
> + */
> +struct mii_bus *fwnode_mdio_find_bus(struct fwnode_handle *mdio_bus_fwnode)
> +{
> +	struct device *d;
> +
> +	if (!mdio_bus_fwnode)
> +		return NULL;
> +
> +	d = class_find_device_by_fwnode(&mdio_bus_class, mdio_bus_fwnode);
> +	return d ? to_mii_bus(d) : NULL;
> +}
> +EXPORT_SYMBOL(fwnode_mdio_find_bus);
> +
>  /* Walk the list of subnodes of a mdio bus and look for a node that
>   * matches the mdio device's address with its 'reg' property. If
>   * found, set the of_node pointer for the mdio device. This allows
> diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> index 7cda95330aea..00b2ade9714f 100644
> --- a/drivers/net/phy/phy_device.c
> +++ b/drivers/net/phy/phy_device.c
> @@ -23,8 +23,10 @@
>  #include <linux/mm.h>
>  #include <linux/module.h>
>  #include <linux/netdevice.h>
> +#include <linux/of.h>
>  #include <linux/phy.h>
>  #include <linux/phy_led_triggers.h>
> +#include <linux/platform_device.h>
>  #include <linux/property.h>
>  #include <linux/sfp.h>
>  #include <linux/skbuff.h>
> @@ -964,6 +966,44 @@ struct phy_device *phy_find_first(struct mii_bus *bus)
>  }
>  EXPORT_SYMBOL(phy_find_first);
>  
> +/**
> + * phy_find_by_mdio_handle - get phy device from mdio-handle and phy-channel
> + * @fwnode: a pointer to a &struct fwnode_handle  to get mdio-handle and
> + * phy-channel
> + *
> + * Find fwnode_mdio using mdio-handle reference. Using fwnode_mdio get the
> + * mdio bus. Property phy-channel provides the phy address on the mdio bus.
> + * Pass mdio bus and phy address to mdiobus_get_phy() and get corresponding
> + * phy_device. This method is used for ACPI and not for DT.
> + *
> + * Returns pointer to the phy device on success, else ERR_PTR.
> + */
> +struct phy_device *phy_find_by_mdio_handle(struct fwnode_handle *fwnode)
> +{
> +	struct fwnode_handle *fwnode_mdio;
> +	struct mii_bus *mdio;
> +	int addr;
> +	int err;
> +
> +	if (is_of_node(fwnode))
> +		return ERR_PTR(-EINVAL);
> +
> +	fwnode_mdio = fwnode_find_reference(fwnode, "mdio-handle", 0);

I would export the positional argument here to phy_find_by_mdio_handle()
but have no strong opinion if this is not done right now and punted to
when another users comes in.

> +	mdio = fwnode_mdio_find_bus(fwnode_mdio);
> +	fwnode_handle_put(fwnode_mdio);
> +	if (!mdio)
> +		return ERR_PTR(-ENODEV);
> +
> +	err = fwnode_property_read_u32(fwnode, "phy-channel", &addr);
> +	if (err)
> +		return ERR_PTR(err);
> +	if (addr < 0 || addr >= PHY_MAX_ADDR)

Can an u32 ever be < 0?
Calvin Johnson July 13, 2020, 6:04 a.m. UTC | #2
On Sat, Jul 11, 2020 at 02:41:12PM -0700, Florian Fainelli wrote:
> 
> 
> On 7/10/2020 11:55 PM, Calvin Johnson wrote:
> > The PHYs on an mdiobus are probed and registered using mdiobus_register().
> > Later, for connecting these PHYs to MAC, the PHYs registered on the
> > mdiobus have to be referenced.
> > 
> > For each MAC node, a property "mdio-handle" is used to reference the
> > MDIO bus on which the PHYs are registered. On getting hold of the MDIO
> > bus, use phy_find_by_fwnode() to get the PHY connected to the MAC.
> > 
> > Introduce fwnode_mdio_find_bus() to find the mii_bus that corresponds
> > to given mii_bus fwnode.
> > 
> > Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com>
> > 
> > ---
> > 
> > Changes in v6: None
> > Changes in v5:
> > - rename phy_find_by_fwnode() to phy_find_by_mdio_handle()
> > - add docment for phy_find_by_mdio_handle()
> > - error out DT in phy_find_by_mdio_handle()
> > - clean up err return
> > 
> > Changes in v4:
> > - release fwnode_mdio after use
> > - return ERR_PTR instead of NULL
> > 
> > Changes in v3:
> > - introduce fwnode_mdio_find_bus()
> > - renamed and improved phy_find_by_fwnode()
> > 
> > Changes in v2: None
> > 
> >  drivers/net/phy/mdio_bus.c   | 25 ++++++++++++++++++++++
> >  drivers/net/phy/phy_device.c | 40 ++++++++++++++++++++++++++++++++++++
> >  include/linux/phy.h          |  2 ++
> >  3 files changed, 67 insertions(+)
> > 
> > diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
> > index 8610f938f81f..d9597c5b55ae 100644
> > --- a/drivers/net/phy/mdio_bus.c
> > +++ b/drivers/net/phy/mdio_bus.c
> > @@ -435,6 +435,31 @@ struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
> >  }
> >  EXPORT_SYMBOL(of_mdio_find_bus);
> >  
> > +/**
> > + * fwnode_mdio_find_bus - Given an mii_bus fwnode, find the mii_bus.
> > + * @mdio_bus_fwnode: fwnode of the mii_bus.
> > + *
> > + * Returns a reference to the mii_bus, or NULL if none found.  The
> > + * embedded struct device will have its reference count incremented,
> > + * and this must be put once the bus is finished with.
> > + *
> > + * Because the association of a fwnode and mii_bus is made via
> > + * mdiobus_register(), the mii_bus cannot be found before it is
> > + * registered with mdiobus_register().
> > + *
> > + */
> > +struct mii_bus *fwnode_mdio_find_bus(struct fwnode_handle *mdio_bus_fwnode)
> > +{
> > +	struct device *d;
> > +
> > +	if (!mdio_bus_fwnode)
> > +		return NULL;
> > +
> > +	d = class_find_device_by_fwnode(&mdio_bus_class, mdio_bus_fwnode);
> > +	return d ? to_mii_bus(d) : NULL;
> > +}
> > +EXPORT_SYMBOL(fwnode_mdio_find_bus);
> > +
> >  /* Walk the list of subnodes of a mdio bus and look for a node that
> >   * matches the mdio device's address with its 'reg' property. If
> >   * found, set the of_node pointer for the mdio device. This allows
> > diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
> > index 7cda95330aea..00b2ade9714f 100644
> > --- a/drivers/net/phy/phy_device.c
> > +++ b/drivers/net/phy/phy_device.c
> > @@ -23,8 +23,10 @@
> >  #include <linux/mm.h>
> >  #include <linux/module.h>
> >  #include <linux/netdevice.h>
> > +#include <linux/of.h>
> >  #include <linux/phy.h>
> >  #include <linux/phy_led_triggers.h>
> > +#include <linux/platform_device.h>
> >  #include <linux/property.h>
> >  #include <linux/sfp.h>
> >  #include <linux/skbuff.h>
> > @@ -964,6 +966,44 @@ struct phy_device *phy_find_first(struct mii_bus *bus)
> >  }
> >  EXPORT_SYMBOL(phy_find_first);
> >  
> > +/**
> > + * phy_find_by_mdio_handle - get phy device from mdio-handle and phy-channel
> > + * @fwnode: a pointer to a &struct fwnode_handle  to get mdio-handle and
> > + * phy-channel
> > + *
> > + * Find fwnode_mdio using mdio-handle reference. Using fwnode_mdio get the
> > + * mdio bus. Property phy-channel provides the phy address on the mdio bus.
> > + * Pass mdio bus and phy address to mdiobus_get_phy() and get corresponding
> > + * phy_device. This method is used for ACPI and not for DT.
> > + *
> > + * Returns pointer to the phy device on success, else ERR_PTR.
> > + */
> > +struct phy_device *phy_find_by_mdio_handle(struct fwnode_handle *fwnode)
> > +{
> > +	struct fwnode_handle *fwnode_mdio;
> > +	struct mii_bus *mdio;
> > +	int addr;
> > +	int err;
> > +
> > +	if (is_of_node(fwnode))
> > +		return ERR_PTR(-EINVAL);
> > +
> > +	fwnode_mdio = fwnode_find_reference(fwnode, "mdio-handle", 0);
> 
> I would export the positional argument here to phy_find_by_mdio_handle()
> but have no strong opinion if this is not done right now and punted to
> when another users comes in.

Sorry, I didn't get you. Can you please elaborate?

> 
> > +	mdio = fwnode_mdio_find_bus(fwnode_mdio);
> > +	fwnode_handle_put(fwnode_mdio);
> > +	if (!mdio)
> > +		return ERR_PTR(-ENODEV);
> > +
> > +	err = fwnode_property_read_u32(fwnode, "phy-channel", &addr);
> > +	if (err)
> > +		return ERR_PTR(err);
> > +	if (addr < 0 || addr >= PHY_MAX_ADDR)
> 
> Can an u32 ever be < 0?

Will remove it.

Thanks
Calvin
Andy Shevchenko July 13, 2020, 11:04 a.m. UTC | #3
On Mon, Jul 13, 2020 at 9:04 AM Calvin Johnson
<calvin.johnson@oss.nxp.com> wrote:
> On Sat, Jul 11, 2020 at 02:41:12PM -0700, Florian Fainelli wrote:
> > On 7/10/2020 11:55 PM, Calvin Johnson wrote:

...

> > > +   err = fwnode_property_read_u32(fwnode, "phy-channel", &addr);
> > > +   if (err)
> > > +           return ERR_PTR(err);
> > > +   if (addr < 0 || addr >= PHY_MAX_ADDR)
> >
> > Can an u32 ever be < 0?
>
> Will remove it.

Since it's harmless and we have a fix to shut the compiler up on this
kind of checks and Linus actually in favour of _seeing_ range checks
explicitly I think no need to drop, but it's up to you.
diff mbox series

Patch

diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
index 8610f938f81f..d9597c5b55ae 100644
--- a/drivers/net/phy/mdio_bus.c
+++ b/drivers/net/phy/mdio_bus.c
@@ -435,6 +435,31 @@  struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
 }
 EXPORT_SYMBOL(of_mdio_find_bus);
 
+/**
+ * fwnode_mdio_find_bus - Given an mii_bus fwnode, find the mii_bus.
+ * @mdio_bus_fwnode: fwnode of the mii_bus.
+ *
+ * Returns a reference to the mii_bus, or NULL if none found.  The
+ * embedded struct device will have its reference count incremented,
+ * and this must be put once the bus is finished with.
+ *
+ * Because the association of a fwnode and mii_bus is made via
+ * mdiobus_register(), the mii_bus cannot be found before it is
+ * registered with mdiobus_register().
+ *
+ */
+struct mii_bus *fwnode_mdio_find_bus(struct fwnode_handle *mdio_bus_fwnode)
+{
+	struct device *d;
+
+	if (!mdio_bus_fwnode)
+		return NULL;
+
+	d = class_find_device_by_fwnode(&mdio_bus_class, mdio_bus_fwnode);
+	return d ? to_mii_bus(d) : NULL;
+}
+EXPORT_SYMBOL(fwnode_mdio_find_bus);
+
 /* Walk the list of subnodes of a mdio bus and look for a node that
  * matches the mdio device's address with its 'reg' property. If
  * found, set the of_node pointer for the mdio device. This allows
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 7cda95330aea..00b2ade9714f 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -23,8 +23,10 @@ 
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/netdevice.h>
+#include <linux/of.h>
 #include <linux/phy.h>
 #include <linux/phy_led_triggers.h>
+#include <linux/platform_device.h>
 #include <linux/property.h>
 #include <linux/sfp.h>
 #include <linux/skbuff.h>
@@ -964,6 +966,44 @@  struct phy_device *phy_find_first(struct mii_bus *bus)
 }
 EXPORT_SYMBOL(phy_find_first);
 
+/**
+ * phy_find_by_mdio_handle - get phy device from mdio-handle and phy-channel
+ * @fwnode: a pointer to a &struct fwnode_handle  to get mdio-handle and
+ * phy-channel
+ *
+ * Find fwnode_mdio using mdio-handle reference. Using fwnode_mdio get the
+ * mdio bus. Property phy-channel provides the phy address on the mdio bus.
+ * Pass mdio bus and phy address to mdiobus_get_phy() and get corresponding
+ * phy_device. This method is used for ACPI and not for DT.
+ *
+ * Returns pointer to the phy device on success, else ERR_PTR.
+ */
+struct phy_device *phy_find_by_mdio_handle(struct fwnode_handle *fwnode)
+{
+	struct fwnode_handle *fwnode_mdio;
+	struct mii_bus *mdio;
+	int addr;
+	int err;
+
+	if (is_of_node(fwnode))
+		return ERR_PTR(-EINVAL);
+
+	fwnode_mdio = fwnode_find_reference(fwnode, "mdio-handle", 0);
+	mdio = fwnode_mdio_find_bus(fwnode_mdio);
+	fwnode_handle_put(fwnode_mdio);
+	if (!mdio)
+		return ERR_PTR(-ENODEV);
+
+	err = fwnode_property_read_u32(fwnode, "phy-channel", &addr);
+	if (err)
+		return ERR_PTR(err);
+	if (addr < 0 || addr >= PHY_MAX_ADDR)
+		return ERR_PTR(-EINVAL);
+
+	return mdiobus_get_phy(mdio, addr);
+}
+EXPORT_SYMBOL(phy_find_by_mdio_handle);
+
 static void phy_link_change(struct phy_device *phydev, bool up)
 {
 	struct net_device *netdev = phydev->attached_dev;
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 0403eb799913..fd383a22eb61 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -334,6 +334,7 @@  static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
 }
 
 struct mii_bus *mdio_find_bus(const char *mdio_name);
+struct mii_bus *fwnode_mdio_find_bus(struct fwnode_handle *mdio_bus_fwnode);
 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
 
 #define PHY_INTERRUPT_DISABLED	false
@@ -1245,6 +1246,7 @@  int phy_sfp_probe(struct phy_device *phydev,
 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
 			      phy_interface_t interface);
 struct phy_device *phy_find_first(struct mii_bus *bus);
+struct phy_device *phy_find_by_mdio_handle(struct fwnode_handle *fwnode);
 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 		      u32 flags, phy_interface_t interface);
 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,