diff mbox series

[net-next,v3,08/11] net: phy: add support for C45-over-C22 transfers

Message ID 20230620-feature-c45-over-c22-v3-8-9eb37edf7be0@kernel.org (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net: phy: C45-over-C22 access | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 1744 this patch: 1745
netdev/cc_maintainers success CCed 8 of 8 maintainers
netdev/build_clang fail Errors and warnings before: 1643 this patch: 1644
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 1753 this patch: 1754
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 190 lines checked
netdev/kdoc fail Errors and warnings before: 0 this patch: 1
netdev/source_inline success Was 0 now: 0

Commit Message

Michael Walle July 12, 2023, 3:07 p.m. UTC
If an MDIO bus is only capable of doing C22 transfers we can use
indirect accesses to C45 registers over C22 registers. This was already
the intention of the GPY215 driver. The author described their use case
as follows:

  Our product supports both C22 and C45.

  In the real system, we found C22 was used by customers (with indirect
  access to C45 registers when necessary).

In its probe function phy_get_c45_ids() is called but this will always
do C45 accesses and thus will fail on a C22-only bus.

Add a new transfer mode C45-over-C22. Care has to be taken for
get_phy_device() which is called by either the bus scanning code or the
network device drivers. In the former case, we must not do any
C45-over-C22 accesses because it invokes register writes and we cannot
be sure if the accessed device is a PHY. C45-over-C22 is just defined
for network PHYs, not generic MDIO devices. Therefore, the it is the
callers responsibility to choose the access mode for get_phy_device().

Due to the reasons above, the current scanning code the PHY core cannot
make use of the new transfer mode because we cannot be sure what device
is a PHY. The code will be used for the device tree scanning where we
know which device is a PHY and therefore, C45-over-C22 is safe to use.

A word on the error checking in the MMD access methods: due to legacy
reasons, we cannot check for errors if we do "normal" MMD access to a
C22 PHY without introducing possible regressions. Although, C45-over-C22
is doing essentially the same access, we can do better now and check for
any errors while doing the indirect access.

Signed-off-by: Michael Walle <mwalle@kernel.org>
---
v3:
 - instead of the boolean c45_over_c22 flag pass an enum access_mode
 - use new low-level helper phy_mdiobus_read_mmd()
---
 drivers/net/phy/phy-core.c   | 25 ++++++++++++++++++++-----
 drivers/net/phy/phy_device.c | 41 +++++++++++++++++++++++++++--------------
 include/linux/phy.h          |  3 +++
 3 files changed, 50 insertions(+), 19 deletions(-)

Comments

Simon Horman July 13, 2023, 8:56 a.m. UTC | #1
On Wed, Jul 12, 2023 at 05:07:08PM +0200, Michael Walle wrote:

...

> @@ -780,6 +782,7 @@ static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
>   * the "devices in package" is invalid.
>   */
>  static int get_phy_c45_ids(struct mii_bus *bus, int addr,
> +			   enum phy_access_mode mode,

Hi Michael,

Please add the new mode parameter to the kernel doc for this function,
which is in the lines preceding this hunk.

...
Michael Walle July 13, 2023, 9 a.m. UTC | #2
Hi Simon,

Am 2023-07-13 10:56, schrieb Simon Horman:
> On Wed, Jul 12, 2023 at 05:07:08PM +0200, Michael Walle wrote:
> 
> ...
> 
>> @@ -780,6 +782,7 @@ static int get_phy_c45_devs_in_pkg(struct mii_bus 
>> *bus, int addr, int dev_addr,
>>   * the "devices in package" is invalid.
>>   */
>>  static int get_phy_c45_ids(struct mii_bus *bus, int addr,
>> +			   enum phy_access_mode mode,
> 
> Please add the new mode parameter to the kernel doc for this function,
> which is in the lines preceding this hunk.

Ahh. Sorry. You've already mentioned that last time :/

Also true for get_phy_c45_devs_in_pkg().

-michael
Simon Horman July 13, 2023, 9:19 a.m. UTC | #3
On Thu, Jul 13, 2023 at 11:00:07AM +0200, Michael Walle wrote:
> Hi Simon,
> 
> Am 2023-07-13 10:56, schrieb Simon Horman:
> > On Wed, Jul 12, 2023 at 05:07:08PM +0200, Michael Walle wrote:
> > 
> > ...
> > 
> > > @@ -780,6 +782,7 @@ static int get_phy_c45_devs_in_pkg(struct
> > > mii_bus *bus, int addr, int dev_addr,
> > >   * the "devices in package" is invalid.
> > >   */
> > >  static int get_phy_c45_ids(struct mii_bus *bus, int addr,
> > > +			   enum phy_access_mode mode,
> > 
> > Please add the new mode parameter to the kernel doc for this function,
> > which is in the lines preceding this hunk.
> 
> Ahh. Sorry. You've already mentioned that last time :/
> 
> Also true for get_phy_c45_devs_in_pkg().

Thanks, I missed that one today.
diff mbox series

Patch

diff --git a/drivers/net/phy/phy-core.c b/drivers/net/phy/phy-core.c
index 4863a85f8385..17ad118edbf1 100644
--- a/drivers/net/phy/phy-core.c
+++ b/drivers/net/phy/phy-core.c
@@ -550,12 +550,20 @@  static int __phy_mdiobus_read_mmd(struct mii_bus *bus, int phy_addr,
 				  enum phy_access_mode access_mode,
 				  int devad, u32 regnum)
 {
+	bool check_rc = true;
+	int ret;
+
 	switch (access_mode) {
 	case PHY_ACCESS_C45:
 		return __mdiobus_c45_read(bus, phy_addr, devad, regnum);
 	case PHY_ACCESS_C22:
 		/* ignore return value for legacy reasons */
-		mmd_phy_indirect(bus, phy_addr, devad, regnum, false);
+		check_rc = false;
+		fallthrough;
+	case PHY_ACCESS_C45_OVER_C22:
+		ret = mmd_phy_indirect(bus, phy_addr, devad, regnum, check_rc);
+		if (check_rc && ret)
+			return ret;
 
 		/* Read the content of the MMD's selected register */
 		return __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
@@ -597,17 +605,24 @@  static int __phy_mdiobus_write_mmd(struct mii_bus *bus, int phy_addr,
 				   enum phy_access_mode mode,
 				   int devad, u32 regnum, u16 val)
 {
+	bool check_rc = true;
+	int ret;
+
 	switch (mode) {
 	case PHY_ACCESS_C45:
 		return __mdiobus_c45_write(bus, phy_addr, devad, regnum, val);
 	case PHY_ACCESS_C22:
-		/* ignore return value for legacy reasons */
-		mmd_phy_indirect(bus, phy_addr, devad, regnum, false);
+		check_rc = false;
+		fallthrough;
+	case PHY_ACCESS_C45_OVER_C22:
+		ret = mmd_phy_indirect(bus, phy_addr, devad, regnum, check_rc);
+		if (check_rc && ret)
+			return ret;
 
 		/* Write the data into MMD's selected register */
-		__mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
+		ret = __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
 
-		return 0;
+		return check_rc ? ret : 0;
 	default:
 		return -EOPNOTSUPP;
 	}
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 6254212d65a5..01c41ecb63a7 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -726,11 +726,12 @@  EXPORT_SYMBOL(phy_device_create);
  * Returns: negative error number on bus access error, zero if no device
  * is responding, or positive if a device is present.
  */
-static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
+static int phy_c45_probe_present(struct mii_bus *bus, int prtad,
+				 enum phy_access_mode mode, int devad)
 {
 	int stat2;
 
-	stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2);
+	stat2 = phy_mdiobus_read_mmd(bus, prtad, mode, devad, MDIO_STAT2);
 	if (stat2 < 0)
 		return stat2;
 
@@ -748,17 +749,18 @@  static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
  *
  * Returns: 0 on success, -EIO on failure.
  */
-static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
+static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr,
+				   enum phy_access_mode mode, int dev_addr,
 				   u32 *devices_in_package)
 {
 	int phy_reg;
 
-	phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2);
+	phy_reg = phy_mdiobus_read_mmd(bus, addr, mode, dev_addr, MDIO_DEVS2);
 	if (phy_reg < 0)
 		return -EIO;
 	*devices_in_package = phy_reg << 16;
 
-	phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1);
+	phy_reg = phy_mdiobus_read_mmd(bus, addr, mode, dev_addr, MDIO_DEVS1);
 	if (phy_reg < 0)
 		return -EIO;
 	*devices_in_package |= phy_reg;
@@ -780,6 +782,7 @@  static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
  * the "devices in package" is invalid.
  */
 static int get_phy_c45_ids(struct mii_bus *bus, int addr,
+			   enum phy_access_mode mode,
 			   struct phy_c45_device_ids *c45_ids)
 {
 	const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
@@ -798,14 +801,15 @@  static int get_phy_c45_ids(struct mii_bus *bus, int addr,
 			 * Some PHYs (88x3310) vendor space is not IEEE802.3
 			 * compliant.
 			 */
-			ret = phy_c45_probe_present(bus, addr, i);
+			ret = phy_c45_probe_present(bus, addr, mode, i);
 			if (ret < 0)
 				return -EIO;
 
 			if (!ret)
 				continue;
 		}
-		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
+		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, mode, i,
+						  &devs_in_pkg);
 		if (phy_reg < 0)
 			return -EIO;
 	}
@@ -815,7 +819,8 @@  static int get_phy_c45_ids(struct mii_bus *bus, int addr,
 		 * MMD 0, as some 10G PHYs have zero Devices In package,
 		 * e.g. Cortina CS4315/CS4340 PHY.
 		 */
-		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
+		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, mode, 0,
+						  &devs_in_pkg);
 		if (phy_reg < 0)
 			return -EIO;
 
@@ -834,7 +839,7 @@  static int get_phy_c45_ids(struct mii_bus *bus, int addr,
 			 * to ignore these if they do not contain IEEE 802.3
 			 * registers.
 			 */
-			ret = phy_c45_probe_present(bus, addr, i);
+			ret = phy_c45_probe_present(bus, addr, mode, i);
 			if (ret < 0)
 				return ret;
 
@@ -842,12 +847,14 @@  static int get_phy_c45_ids(struct mii_bus *bus, int addr,
 				continue;
 		}
 
-		phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1);
+		phy_reg = phy_mdiobus_read_mmd(bus, addr, mode, i,
+					       MII_PHYSID1);
 		if (phy_reg < 0)
 			return -EIO;
 		c45_ids->device_ids[i] = phy_reg << 16;
 
-		phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2);
+		phy_reg = phy_mdiobus_read_mmd(bus, addr, mode, i,
+					       MII_PHYSID2);
 		if (phy_reg < 0)
 			return -EIO;
 		c45_ids->device_ids[i] |= phy_reg;
@@ -937,6 +944,11 @@  EXPORT_SYMBOL(fwnode_get_phy_id);
  * If the "devices in package" appears valid, read the ID registers for each
  * MMD, allocate and return a &struct phy_device.
  *
+ * When using %PHY_ACCESS_C45_OVER_C22 as @mode care have to be taken to not
+ * access a non-PHY device as C45-over-C22 is a property of a PHY and not a
+ * generic MDIO device. As the access involves register writes, it may be
+ * destructive on non-PHY devices. IOW, it cannot be used for bus scanning.
+ *
  * Returns an allocated &struct phy_device on success, %-ENODEV if there is
  * no PHY present, or %-EIO on bus access error.
  */
@@ -956,7 +968,8 @@  struct phy_device *get_phy_device(struct mii_bus *bus, int addr,
 		r = get_phy_c22_id(bus, addr, &phy_id);
 		break;
 	case PHY_ACCESS_C45:
-		r = get_phy_c45_ids(bus, addr, &c45_ids);
+	case PHY_ACCESS_C45_OVER_C22:
+		r = get_phy_c45_ids(bus, addr, mode, &c45_ids);
 		break;
 	default:
 		return ERR_PTR(-EIO);
@@ -971,7 +984,7 @@  struct phy_device *get_phy_device(struct mii_bus *bus, int addr,
 	 * space, if successful, create the C45 PHY device.
 	 */
 	if (mode == PHY_ACCESS_C22 && phy_id == 0 && bus->read_c45) {
-		r = get_phy_c45_ids(bus, addr, &c45_ids);
+		r = get_phy_c45_ids(bus, addr, PHY_ACCESS_C45, &c45_ids);
 		if (!r)
 			return phy_device_create(bus, addr, phy_id,
 						 PHY_ACCESS_C45, &c45_ids);
@@ -1053,7 +1066,7 @@  EXPORT_SYMBOL(phy_device_remove);
 int phy_get_c45_ids(struct phy_device *phydev)
 {
 	return get_phy_c45_ids(phydev->mdio.bus, phydev->mdio.addr,
-			       &phydev->c45_ids);
+			       PHY_ACCESS_C45, &phydev->c45_ids);
 }
 EXPORT_SYMBOL(phy_get_c45_ids);
 
diff --git a/include/linux/phy.h b/include/linux/phy.h
index a482696a17a6..e4014972b799 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -541,10 +541,13 @@  struct macsec_ops;
  *
  * @PHY_ACCESS_C22: use 802.3 c22 MDIO transactions
  * @PHY_ACCESS_C45: use 802.3 c45 MDIO transactions
+ * @PHY_ACCESS_C45_OVER_C22: indirectly access C45 registers by using by 802.3
+ *			     c22 MDIO transactions and registers 13 and 14.
  */
 enum phy_access_mode {
 	PHY_ACCESS_C22 = 22,
 	PHY_ACCESS_C45 = 45,
+	PHY_ACCESS_C45_OVER_C22 = 4522,
 };
 
 /**