Skip to contentSkip to conversation

net: phy: extend PHY package API to support multiple global address

PATCH Superseded Christian Marangi 3 participants 13 messages Nov 26, 2023

Subsystems (4)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 net: phy: extend PHY package API to support multiple global address

Current API for PHY package are limited to single address to configure global settings for the PHY package.

It was found that some PHY package (for example the qca807x, a PHY package that is shipped with a bundle of 5 PHY) requires multiple PHY address to configure global settings. An example scenario is a PHY that have a dedicated PHY for PSGMII/serdes calibrarion and have a specific PHY in the package where the global PHY mode is set and affects every other PHY in the package.

Change the API in the following way:
- Change phy_package_join() to take the base addr of the PHY package instead of the global PHY addr.
- Make __/phy_package_write/read() require an additional arg that select what global PHY address to use by passing the offset from the base addr passed on phy_package_join().

Each user of this API is updated to follow this new implementation following a pattern where an enum is defined to declare the offset of the addr.

---
 drivers/net/phy/bcm54140.c       | 16 +++++++++---
 drivers/net/phy/mscc/mscc.h      |  5 ++++
 drivers/net/phy/mscc/mscc_main.c |  4 +--
 drivers/net/phy/phy_device.c     | 40 ++++++++++++++++------------
 include/linux/phy.h              | 45 ++++++++++++++++++++------------
 5 files changed, 72 insertions(+), 38 deletions(-)
drivers/net/phy/bcm54140.c +12 -4
index d43076592f81..2eea3d09b1e6 100644
@@ -128,6 +128,10 @@
 #define BCM54140_DEFAULT_DOWNSHIFT 5
 #define BCM54140_MAX_DOWNSHIFT 9
 
+enum bcm54140_global_phy {
+	BCM54140_BASE_ADDR = 0,
+};
+
 struct bcm54140_priv {
 	int port;
 	int base_addr;
@@ -429,11 +433,13 @@ static int bcm54140_base_read_rdb(struct phy_device *phydev, u16 rdb)
 	int ret;
 
 	phy_lock_mdio_bus(phydev);
-	ret = __phy_package_write(phydev, MII_BCM54XX_RDB_ADDR, rdb);
+	ret = __phy_package_write(phydev, BCM54140_BASE_ADDR,
+				  MII_BCM54XX_RDB_ADDR, rdb);
 	if (ret < 0)
 		goto out;
 
-	ret = __phy_package_read(phydev, MII_BCM54XX_RDB_DATA);
+	ret = __phy_package_read(phydev, BCM54140_BASE_ADDR,
+				 MII_BCM54XX_RDB_DATA);
 
 out:
 	phy_unlock_mdio_bus(phydev);
@@ -446,11 +452,13 @@ static int bcm54140_base_write_rdb(struct phy_device *phydev,
 	int ret;
 
 	phy_lock_mdio_bus(phydev);
-	ret = __phy_package_write(phydev, MII_BCM54XX_RDB_ADDR, rdb);
+	ret = __phy_package_write(phydev, BCM54140_BASE_ADDR,
+				  MII_BCM54XX_RDB_ADDR, rdb);
 	if (ret < 0)
 		goto out;
 
-	ret = __phy_package_write(phydev, MII_BCM54XX_RDB_DATA, val);
+	ret = __phy_package_write(phydev, BCM54140_BASE_ADDR,
+				  MII_BCM54XX_RDB_DATA, val);
 
 out:
 	phy_unlock_mdio_bus(phydev);
drivers/net/phy/mscc/mscc.h +5 -0
index 7a962050a4d4..6a3d8a754eb8 100644
@@ -416,6 +416,11 @@ struct vsc8531_private {
  * gpio_lock: used for PHC operations. Common for all PHYs as the load/save GPIO
  * is shared.
  */
+
+enum vsc85xx_global_phy {
+	VSC88XX_BASE_ADDR = 0,
+};
+
 struct vsc85xx_shared_private {
 	struct mutex gpio_lock;
 };
drivers/net/phy/mscc/mscc_main.c +2 -2
index 4171f01d34e5..6f74ce0ab1aa 100644
@@ -711,7 +711,7 @@ int phy_base_write(struct phy_device *phydev, u32 regnum, u16 val)
 		dump_stack();
 	}
 
-	return __phy_package_write(phydev, regnum, val);
+	return __phy_package_write(phydev, VSC88XX_BASE_ADDR, regnum, val);
 }
 
 /* phydev->bus->mdio_lock should be locked when using this function */
@@ -722,7 +722,7 @@ int phy_base_read(struct phy_device *phydev, u32 regnum)
 		dump_stack();
 	}
 
-	return __phy_package_read(phydev, regnum);
+	return __phy_package_read(phydev, VSC88XX_BASE_ADDR, regnum);
 }
 
 u32 vsc85xx_csr_read(struct phy_device *phydev,
drivers/net/phy/phy_device.c +24 -16
index 478126f6b5bc..823b25bb3e3e 100644
@@ -1648,20 +1648,27 @@ EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
 /**
  * phy_package_join - join a common PHY group
  * @phydev: target phy_device struct
- * @addr: cookie and PHY address for global register access
+ * @base_addr: cookie and base PHY address of PHY package for offset
+ *   calculation of global register access
  * @priv_size: if non-zero allocate this amount of bytes for private data
  *
  * This joins a PHY group and provides a shared storage for all phydevs in
  * this group. This is intended to be used for packages which contain
  * more than one PHY, for example a quad PHY transceiver.
  *
- * The addr parameter serves as a cookie which has to have the same value
- * for all members of one group and as a PHY address to access generic
- * registers of a PHY package. Usually, one of the PHY addresses of the
- * different PHYs in the package provides access to these global registers.
+ * The addr parameter serves as cookie which has to have the same values
+ * for all members of one group and as the base PHY address of the PHY package
+ * for offset calculation to access generic registers of a PHY package.
+ * Usually, one of the PHY addresses of the different PHYs in the package
+ * provides access to these global registers.
  * The address which is given here, will be used in the phy_package_read()
- * and phy_package_write() convenience functions. If your PHY doesn't have
- * global registers you can just pick any of the PHY addresses.
+ * and phy_package_write() convenience functions as base and added to the
+ * passed offset in those functions. If your PHY doesn't have global registers
+ * you can just pick any of the PHY addresses.
+ * In some special PHY package, multiple PHY are used for global init of
+ * the entire PHY package. In this the scenario, phy_package_read() and
+ * phy_package_write() offset is used to communicate which PHY to use for
+ * global init on read/write.
  *
  * This will set the shared pointer of the phydev to the shared storage.
  * If this is the first call for a this cookie the shared storage will be
@@ -1671,17 +1678,17 @@ EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
  * Returns < 1 on error, 0 on success. Esp. calling phy_package_join()
  * with the same cookie but a different priv_size is an error.
  */
-int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size)
+int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size)
 {
 	struct mii_bus *bus = phydev->mdio.bus;
 	struct phy_package_shared *shared;
 	int ret;
 
-	if (addr < 0 || addr >= PHY_MAX_ADDR)
+	if (base_addr < 0 || base_addr >= PHY_MAX_ADDR)
 		return -EINVAL;
 
 	mutex_lock(&bus->shared_lock);
-	shared = bus->shared[addr];
+	shared = bus->shared[base_addr];
 	if (!shared) {
 		ret = -ENOMEM;
 		shared = kzalloc(sizeof(*shared), GFP_KERNEL);
@@ -1693,9 +1700,9 @@ int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size)
 				goto err_free;
 			shared->priv_size = priv_size;
 		}
-		shared->addr = addr;
+		shared->base_addr = base_addr;
 		refcount_set(&shared->refcnt, 1);
-		bus->shared[addr] = shared;
+		bus->shared[base_addr] = shared;
 	} else {
 		ret = -EINVAL;
 		if (priv_size && priv_size != shared->priv_size)
@@ -1733,7 +1740,7 @@ void phy_package_leave(struct phy_device *phydev)
 		return;
 
 	if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
-		bus->shared[shared->addr] = NULL;
+		bus->shared[shared->base_addr] = NULL;
 		mutex_unlock(&bus->shared_lock);
 		kfree(shared->priv);
 		kfree(shared);
@@ -1752,7 +1759,8 @@ static void devm_phy_package_leave(struct device *dev, void *res)
  * devm_phy_package_join - resource managed phy_package_join()
  * @dev: device that is registering this PHY package
  * @phydev: target phy_device struct
- * @addr: cookie and PHY address for global register access
+ * @base_addr: cookie and base PHY address of PHY package for offset
+ *   calculation of global register access
  * @priv_size: if non-zero allocate this amount of bytes for private data
  *
  * Managed phy_package_join(). Shared storage fetched by this function,
@@ -1760,7 +1768,7 @@ static void devm_phy_package_leave(struct device *dev, void *res)
  * phy_package_join() for more information.
  */
 int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
-			  int addr, size_t priv_size)
+			  int base_addr, size_t priv_size)
 {
 	struct phy_device **ptr;
 	int ret;
@@ -1770,7 +1778,7 @@ int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
 	if (!ptr)
 		return -ENOMEM;
 
-	ret = phy_package_join(phydev, addr, priv_size);
+	ret = phy_package_join(phydev, base_addr, priv_size);
 
 	if (!ret) {
 		*ptr = phydev;
include/linux/phy.h +29 -16

99 lines not on this page · raw message

Signature · 1 line
2.40.1
#1 lore raw

PATCH 2/3 net: phy: move mmd_phy_indirect to generic header

Move mmd_phy_indirect function from phy-core to generic phy.h to permit future usage for PHY package read/write_mmd.

---
 drivers/net/phy/phy-core.c | 14 --------------
 include/linux/phy.h        | 14 ++++++++++++++
 2 files changed, 14 insertions(+), 14 deletions(-)
drivers/net/phy/phy-core.c +0 -14
index 966c93cbe616..b4f80847eefd 100644
@@ -526,20 +526,6 @@ int phy_speed_down_core(struct phy_device *phydev)
 	return 0;
 }
 
-static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
-			     u16 regnum)
-{
-	/* Write the desired MMD Devad */
-	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
-
-	/* Write the desired MMD register address */
-	__mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
-
-	/* Select the Function : DATA with no post increment */
-	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
-			devad | MII_MMD_CTRL_NOINCR);
-}
-
 /**
  * __phy_read_mmd - Convenience function for reading a register
  * from an MMD on a given PHY.
include/linux/phy.h +14 -0
index 8253c7871d68..984bca9a82f4 100644
@@ -1316,6 +1316,20 @@ static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
 					regnum, mask, set);
 }
 
+static inline void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
+				    u16 regnum)
+{
+	/* Write the desired MMD Devad */
+	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
+
+	/* Write the desired MMD register address */
+	__mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
+
+	/* Select the Function : DATA with no post increment */
+	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
+			devad | MII_MMD_CTRL_NOINCR);
+}
+
 /*
  * phy_read_mmd - Convenience function for reading a register
  * from an MMD on a given PHY.
Signature · 1 line
2.40.1
#2 lore raw

PATCH 3/3 net: phy: add support for PHY package MMD read/write

In reply to Christian Marangi

Some PHY in PHY package may require to read/write MMD regs to correctly configure the PHY package.

Add support for these additional required function in both lock and no lock variant.

---
 include/linux/phy.h | 74 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
include/linux/phy.h +74 -0
index 984bca9a82f4..1799133c8387 100644
@@ -2067,6 +2067,80 @@ static inline int __phy_package_write(struct phy_device *phydev,
 	return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
 }
 
+static inline int phy_package_read_mmd(struct phy_device *phydev,
+				       unsigned int addr_offset, int devad,
+				       u32 regnum)
+{
+	struct phy_package_shared *shared = phydev->shared;
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr, val;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;
+
+	addr = shared->base_addr + addr_offset;
+
+	phy_lock_mdio_bus(phydev);
+	mmd_phy_indirect(bus, addr, devad, regnum);
+	val = __mdiobus_read(bus, addr, MII_MMD_DATA);
+	phy_unlock_mdio_bus(phydev);
+
+	return val;
+}
+
+static inline int __phy_package_read_mmd(struct phy_device *phydev,
+					 unsigned int addr_offset, int devad,
+					 u32 regnum)
+{
+	struct phy_package_shared *shared = phydev->shared;
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;
+
+	addr = shared->base_addr + addr_offset;
+	mmd_phy_indirect(bus, addr, devad, regnum);
+	return __mdiobus_read(bus, addr, MII_MMD_DATA);
+}
+
+static inline int phy_package_write_mmd(struct phy_device *phydev,
+					unsigned int addr_offset, int devad,
+					u32 regnum, u16 val)
+{
+	struct phy_package_shared *shared = phydev->shared;
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr, ret;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;
+
+	addr = shared->base_addr + addr_offset;
+
+	phy_lock_mdio_bus(phydev);
+	mmd_phy_indirect(bus, addr, devad, regnum);
+	ret = __mdiobus_write(bus, addr, MII_MMD_DATA, val);
+	phy_unlock_mdio_bus(phydev);
+
+	return ret;
+}
+
+static inline int __phy_package_write_mmd(struct phy_device *phydev,
+					  unsigned int addr_offset, int devad,
+					  u32 regnum, u16 val)
+{
+	struct phy_package_shared *shared = phydev->shared;
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;
+
+	addr = shared->base_addr + addr_offset;
+	mmd_phy_indirect(bus, addr, devad, regnum);
+	return __mdiobus_write(bus, addr, MII_MMD_DATA, val);
+}
+
 static inline bool __phy_package_set_once(struct phy_device *phydev,
 					  unsigned int b)
 {
Signature · 1 line
2.40.1
#3 lore raw

Re: [net-next PATCH 3/3] net: phy: add support for PHY package MMD read/write

Quoted 19 lines · Christian Marangi

Some PHY in PHY package may require to read/write MMD regs to correctly configure the PHY package.

Add support for these additional required function in both lock and no lock variant.

---
  include/linux/phy.h | 74 +++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 74 insertions(+)
include/linux/phy.h +0 -0
index 984bca9a82f4..1799133c8387 100644
@@ -2067,6 +2067,80 @@ static inline int __phy_package_write(struct phy_device *phydev,
  	return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
  }
 

include/linux/phy.h static inline int __phy_package_write(struct phy_device *phydev, · line 2079

Quoted 10 lines
include/linux/phy.h +10 -0
+static inline int phy_package_read_mmd(struct phy_device *phydev,
+				       unsigned int addr_offset, int devad,
+				       u32 regnum)
+{
+	struct phy_package_shared *shared = phydev->shared;
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr, val;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;

You might be off by one here, should not that >= PHY_MAX_ADDR here and below?

Signature · 1 line
Florian
#4 lore raw

Re: [net-next PATCH 3/3] net: phy: add support for PHY package MMD read/write

Quoted 26 lines · Florian Fainelli
Quoted 23 lines · Christian Marangi

Some PHY in PHY package may require to read/write MMD regs to correctly configure the PHY package.

Add support for these additional required function in both lock and no lock variant.

---
  include/linux/phy.h | 74 +++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 74 insertions(+)
include/linux/phy.h +5 -0
index 984bca9a82f4..1799133c8387 100644
@@ -2067,6 +2067,80 @@ static inline int __phy_package_write(struct phy_device *phydev,
  	return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
  }
+static inline int phy_package_read_mmd(struct phy_device *phydev,
+				       unsigned int addr_offset, int devad,
+				       u32 regnum)
+{
+	struct phy_package_shared *shared = phydev->shared;
Quoted 8 lines

include/linux/phy.h static inline int __phy_package_write(struct phy_device *phydev, · line 2079

Quoted 5 lines
include/linux/phy.h +5 -0
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr, val;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;

You might be off by one here, should not that >= PHY_MAX_ADDR here and below?

Thanks for the review. Yes PHY_MAX_ADDR is 32 so I should use >=.

(interesting choice to use 32 instead of 31 as MAX, guess an old mistake)

Signature · 1 line
	Ansuel
#5 lore raw

Re: [net-next PATCH 3/3] net: phy: add support for PHY package MMD read/write

Quoted 36 lines · Christian Marangi
Quoted 26 lines · Florian Fainelli
Quoted 23 lines · Christian Marangi

Some PHY in PHY package may require to read/write MMD regs to correctly configure the PHY package.

Add support for these additional required function in both lock and no lock variant.

---
   include/linux/phy.h | 74 +++++++++++++++++++++++++++++++++++++++++++++
   1 file changed, 74 insertions(+)
include/linux/phy.h +5 -0
index 984bca9a82f4..1799133c8387 100644
@@ -2067,6 +2067,80 @@ static inline int __phy_package_write(struct phy_device *phydev,
   	return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
   }
+static inline int phy_package_read_mmd(struct phy_device *phydev,
+				       unsigned int addr_offset, int devad,
+				       u32 regnum)
+{
+	struct phy_package_shared *shared = phydev->shared;
Quoted 8 lines

include/linux/phy.h static inline int __phy_package_write(struct phy_device *phydev, · line 2079

Quoted 5 lines
include/linux/phy.h +5 -0
+	struct mii_bus *bus = phydev->mdio.bus;
+	int addr, val;
+
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
+		return -EIO;

You might be off by one here, should not that >= PHY_MAX_ADDR here and below?

Quoted 3 lines

Thanks for the review. Yes PHY_MAX_ADDR is 32 so I should use >=.

(interesting choice to use 32 instead of 31 as MAX, guess an old mistake)

It has historically been used as an iterator upper bound, hence the 32.

Signature · 1 line
Florian
maintainer
#8 lore raw
In reply to Christian Marangi
Quoted 8 lines
drivers/net/phy/phy_device.c +2 -1
@@ -1648,20 +1648,27 @@ EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
 /**
  * phy_package_join - join a common PHY group
  * @phydev: target phy_device struct
- * @addr: cookie and PHY address for global register access
+ * @base_addr: cookie and base PHY address of PHY package for offset
+ *   calculation of global register access
  * @priv_size: if non-zero allocate this amount of bytes for private data

drivers/net/phy/phy_device.c EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); · line 1659

Quoted 10 lines
drivers/net/phy/phy_device.c +1 -4
  *
  * This joins a PHY group and provides a shared storage for all phydevs in
  * this group. This is intended to be used for packages which contain
  * more than one PHY, for example a quad PHY transceiver.
  *
- * The addr parameter serves as a cookie which has to have the same value
- * for all members of one group and as a PHY address to access generic
- * registers of a PHY package. Usually, one of the PHY addresses of the
- * different PHYs in the package provides access to these global registers.
+ * The addr parameter serves as cookie which has to have the same values

addr has been renamed base_addr.

drivers/net/phy/phy_device.c EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); · line 1667

Quoted 10 lines
drivers/net/phy/phy_device.c +7 -2
+ * for all members of one group and as the base PHY address of the PHY package
+ * for offset calculation to access generic registers of a PHY package.
+ * Usually, one of the PHY addresses of the different PHYs in the package
+ * provides access to these global registers.
  * The address which is given here, will be used in the phy_package_read()
- * and phy_package_write() convenience functions. If your PHY doesn't have
- * global registers you can just pick any of the PHY addresses.
+ * and phy_package_write() convenience functions as base and added to the
+ * passed offset in those functions. If your PHY doesn't have global registers
+ * you can just pick any of the PHY addresses.

I would not add this last sentence. We want a clearly defined meaning of base_addr. Its the lowest address in the package. It does not matter if its not used, it should still be the lowest address in the package.

drivers/net/phy/phy_device.c EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); · line 1668

Quoted 1 line
drivers/net/phy/phy_device.c +1 -0
+ * In some special PHY package, multiple PHY are used for global init of

I don't see why they are special.

Quoted 4 lines
include/linux/phy.h +2 -1
-static inline int phy_package_read(struct phy_device *phydev, u32 regnum)
+static inline int phy_package_read(struct phy_device *phydev,
+				   unsigned int addr_offset, u32 regnum)
 {

include/linux/phy.h int __phy_hwtstamp_set(struct phy_device *phydev, · line 2012

Quoted 10 lines
include/linux/phy.h +4 -2
 	struct phy_package_shared *shared = phydev->shared;
+	int addr;
 
-	if (!shared)
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
 		return -EIO;
 
-	return mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
+	addr = shared->base_addr + addr_offset;
+	return mdiobus_read(phydev->mdio.bus, addr, regnum);

This might be a little bit more readable:

static inline int phy_package_read(struct phy_device *phydev,
unsigned int addr_offset, u32 regnum)

{
	struct phy_package_shared *shared = phydev->shared;
	int addr = shared->base_addr + addr_offset;

	if (!shared)
	if (!shared || addr > PHY_MAX_ADDR)
		return -EIO;

	return mdiobus_read(phydev->mdio.bus, addr, regnum);
}

Andrew

---
pw-bot: cr

#9 lore raw
Quoted 59 lines · Andrew Lunn
Quoted 8 lines
drivers/net/phy/phy_device.c +2 -1
@@ -1648,20 +1648,27 @@ EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
 /**
  * phy_package_join - join a common PHY group
  * @phydev: target phy_device struct
- * @addr: cookie and PHY address for global register access
+ * @base_addr: cookie and base PHY address of PHY package for offset
+ *   calculation of global register access
  * @priv_size: if non-zero allocate this amount of bytes for private data

drivers/net/phy/phy_device.c EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); · line 1659

Quoted 10 lines
drivers/net/phy/phy_device.c +1 -4
  *
  * This joins a PHY group and provides a shared storage for all phydevs in
  * this group. This is intended to be used for packages which contain
  * more than one PHY, for example a quad PHY transceiver.
  *
- * The addr parameter serves as a cookie which has to have the same value
- * for all members of one group and as a PHY address to access generic
- * registers of a PHY package. Usually, one of the PHY addresses of the
- * different PHYs in the package provides access to these global registers.
+ * The addr parameter serves as cookie which has to have the same values

addr has been renamed base_addr.

drivers/net/phy/phy_device.c EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); · line 1667

Quoted 10 lines
drivers/net/phy/phy_device.c +7 -2
+ * for all members of one group and as the base PHY address of the PHY package
+ * for offset calculation to access generic registers of a PHY package.
+ * Usually, one of the PHY addresses of the different PHYs in the package
+ * provides access to these global registers.
  * The address which is given here, will be used in the phy_package_read()
- * and phy_package_write() convenience functions. If your PHY doesn't have
- * global registers you can just pick any of the PHY addresses.
+ * and phy_package_write() convenience functions as base and added to the
+ * passed offset in those functions. If your PHY doesn't have global registers
+ * you can just pick any of the PHY addresses.

I would not add this last sentence. We want a clearly defined meaning of base_addr. Its the lowest address in the package. It does not matter if its not used, it should still be the lowest address in the package.

drivers/net/phy/phy_device.c EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); · line 1668

Quoted 1 line
drivers/net/phy/phy_device.c +1 -0
+ * In some special PHY package, multiple PHY are used for global init of

I don't see why they are special.

Quoted 4 lines
include/linux/phy.h +2 -1
-static inline int phy_package_read(struct phy_device *phydev, u32 regnum)
+static inline int phy_package_read(struct phy_device *phydev,
+				   unsigned int addr_offset, u32 regnum)
 {

include/linux/phy.h int __phy_hwtstamp_set(struct phy_device *phydev, · line 2012

Quoted 10 lines
include/linux/phy.h +4 -2
 	struct phy_package_shared *shared = phydev->shared;
+	int addr;
 
-	if (!shared)
+	if (!shared || shared->base_addr + addr_offset > PHY_MAX_ADDR)
 		return -EIO;
 
-	return mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
+	addr = shared->base_addr + addr_offset;
+	return mdiobus_read(phydev->mdio.bus, addr, regnum);

This might be a little bit more readable:

Quoted 5 lines

static inline int phy_package_read(struct phy_device *phydev,
unsigned int addr_offset, u32 regnum)

{
	struct phy_package_shared *shared = phydev->shared;
	int addr = shared->base_addr + addr_offset;

Isn't this problematic if shared is NULL?

I can add 2 if condition and set addr in between only after shared not NULL check is done?

Quoted 9 lines
	if (!shared)
	if (!shared || addr > PHY_MAX_ADDR)
		return -EIO;

	return mdiobus_read(phydev->mdio.bus, addr, regnum);
}
Signature · 1 line
	Ansuel
maintainer
#6 lore raw

Re: [net-next PATCH 3/3] net: phy: add support for PHY package MMD read/write

In reply to Christian Marangi
Quoted 5 lines · Christian Marangi

Some PHY in PHY package may require to read/write MMD regs to correctly configure the PHY package.

Add support for these additional required function in both lock and no lock variant.

You are assuming the PHY only supports C45 over C22. But what about those PHYs which have native C45? And maybe don't have C22 at all?

You should refactor the code of __phy_read_mmd() into a helper and use it here.

Andrew

maintainer
#10 lore raw
In reply to Christian Marangi
Quoted 7 lines
Quoted 5 lines

static inline int phy_package_read(struct phy_device *phydev,
unsigned int addr_offset, u32 regnum)

{
	struct phy_package_shared *shared = phydev->shared;
	int addr = shared->base_addr + addr_offset;

Isn't this problematic if shared is NULL?

Duh! Yes, it is. But why should shared be NULL? The driver is doing a read on the package before the package is created. That is a bug. So an Opps is O.K, it helps find the bug. So i would drop the test for !shared.

Andrew

#11 lore raw
Quoted 6 lines · Andrew Lunn
Quoted 6 lines
Quoted 5 lines

static inline int phy_package_read(struct phy_device *phydev,
unsigned int addr_offset, u32 regnum)

{
	struct phy_package_shared *shared = phydev->shared;
	int addr = shared->base_addr + addr_offset;
Quoted 6 lines
Quoted 1 line

Isn't this problematic if shared is NULL?

Duh! Yes, it is. But why should shared be NULL? The driver is doing a read on the package before the package is created. That is a bug. So an Opps is O.K, it helps find the bug. So i would drop the test for !shared.

Well yes I think we should assume those API to be called only in config_once context or in package context. But is it Panic ok? I would at least use something like BUG() to give descriptive warning instead of NULL pointer exception. What do you think?

Signature · 1 line
	Ansuel
#7 lore raw

Re: [net-next PATCH 3/3] net: phy: add support for PHY package MMD read/write

In reply to Andrew Lunn
Quoted 4 lines · Andrew Lunn
Quoted 3 lines · Christian Marangi

Some PHY in PHY package may require to read/write MMD regs to correctly configure the PHY package.

Quoted 9 lines
Quoted 2 lines

Add support for these additional required function in both lock and no lock variant.

You are assuming the PHY only supports C45 over C22. But what about those PHYs which have native C45? And maybe don't have C22 at all?

You should refactor the code of __phy_read_mmd() into a helper and use it here.

Have to be honest here and chose the ""easy"" way. Was a little scared by all the complexity with mmd with C45 and C22... The idea was to add C22 support for now and if used by others extend the function. But I guess with the generic approach we are taking it's better to handle it now. Will update this in v2.

Signature · 1 line
	Ansuel
maintainer
#12 lore raw
In reply to Christian Marangi
Quoted 4 lines

Well yes I think we should assume those API to be called only in config_once context or in package context. But is it Panic ok? I would at least use something like BUG() to give descriptive warning instead of NULL pointer exception. What do you think?

BUG() is way too strong. It causes an immediate stop of everything, and probably file system data loss and a reboot. WARN_ON() is generally no better.

An Opps gives a stack trace, which is what you need to find the bug, and kills the process. Generally, you can do a controlled shutdown, without any losses.

Andrew