netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Walle <mwalle@kernel.org>
To: "Andrew Lunn" <andrew@lunn.ch>,
	"Heiner Kallweit" <hkallweit1@gmail.com>,
	"Russell King" <linux@armlinux.org.uk>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@google.com>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Yisen Zhuang" <yisen.zhuang@huawei.com>,
	"Salil Mehta" <salil.mehta@huawei.com>,
	"Florian Fainelli" <florian.fainelli@broadcom.com>,
	"Broadcom internal kernel review list"
	<bcm-kernel-feedback-list@broadcom.com>,
	"Marek Behún" <kabel@kernel.org>, "Xu Liang" <lxu@maxlinear.com>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Simon Horman <simon.horman@corigine.com>,
	Michael Walle <mwalle@kernel.org>
Subject: [PATCH net-next v3 08/11] net: phy: add support for C45-over-C22 transfers
Date: Wed, 12 Jul 2023 17:07:08 +0200	[thread overview]
Message-ID: <20230620-feature-c45-over-c22-v3-8-9eb37edf7be0@kernel.org> (raw)
In-Reply-To: <20230620-feature-c45-over-c22-v3-0-9eb37edf7be0@kernel.org>

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(-)

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,
 };
 
 /**

-- 
2.39.2


  parent reply	other threads:[~2023-07-12 15:07 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-12 15:07 [PATCH net-next v3 00/11] net: phy: C45-over-C22 access Michael Walle
2023-07-12 15:07 ` [PATCH net-next v3 01/11] net: phy: get rid of redundant is_c45 information Michael Walle
2023-07-18 17:25   ` Andrew Lunn
2023-07-12 15:07 ` [PATCH net-next v3 02/11] net: phy: introduce phy_has_c45_registers() Michael Walle
2023-07-18 17:26   ` Andrew Lunn
2023-07-18 20:07   ` Andrew Lunn
2023-07-19  7:11     ` Michael Walle
2023-08-01 14:47       ` Michael Walle
2023-08-01 14:57         ` Russell King (Oracle)
2023-08-01 15:20           ` Michael Walle
2023-08-01 15:57             ` Russell King (Oracle)
2023-08-02 15:33               ` Michael Walle
2023-08-02 16:06                 ` Russell King (Oracle)
2023-08-02 17:11                   ` Michael Walle
2023-08-02 23:00                     ` Russell King (Oracle)
2023-08-02 16:15                 ` Andrew Lunn
2023-08-02 17:10                   ` Russell King (Oracle)
2023-08-02 22:21                     ` Andrew Lunn
2023-08-02 22:28                       ` Russell King (Oracle)
2023-09-05  8:22                         ` Michael Walle
2023-07-12 15:07 ` [PATCH net-next v3 03/11] net: phy: replace is_c45 with phy_accces_mode Michael Walle
2023-07-18 17:40   ` Andrew Lunn
2023-07-18 17:52     ` Russell King (Oracle)
2023-07-18 19:18       ` Andrew Lunn
2023-07-18 21:46         ` Russell King (Oracle)
2023-07-18 23:30           ` Andrew Lunn
2023-07-18 19:53     ` Michael Walle
2023-07-18 20:16       ` Andrew Lunn
2023-07-12 15:07 ` [PATCH net-next v3 04/11] net: phy: make the "prevent_c45_scan" a property of the MII bus Michael Walle
2023-07-18 23:31   ` Andrew Lunn
2023-07-12 15:07 ` [PATCH net-next v3 05/11] net: phy: print an info if a broken C45 bus is found Michael Walle
2023-07-18 23:32   ` Andrew Lunn
2023-07-12 15:07 ` [PATCH net-next v3 06/11] net: phy: add error checks in mmd_phy_indirect() Michael Walle
2023-07-18 23:34   ` Andrew Lunn
2023-07-12 15:07 ` [PATCH net-next v3 07/11] net: phy: introduce phy_mdiobus_read_mmd() Michael Walle
2023-07-18 23:54   ` Andrew Lunn
2023-07-19  7:21     ` Michael Walle
2023-07-12 15:07 ` Michael Walle [this message]
2023-07-13  8:56   ` [PATCH net-next v3 08/11] net: phy: add support for C45-over-C22 transfers Simon Horman
2023-07-13  9:00     ` Michael Walle
2023-07-13  9:19       ` Simon Horman
2023-07-12 15:07 ` [PATCH net-next v3 09/11] net: phy: introduce phy_promote_to_c45() Michael Walle
2023-07-13  8:56   ` Simon Horman
2023-07-12 15:07 ` [PATCH net-next v3 10/11] net: mdio: add C45-over-C22 fallback to fwnode_mdiobus_register_phy() Michael Walle
2023-07-19  0:03   ` Andrew Lunn
2023-07-19  7:32     ` Michael Walle
2023-07-12 15:07 ` [PATCH net-next v3 11/11] net: mdio: support C45-over-C22 when probed via OF Michael Walle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230620-feature-c45-over-c22-v3-8-9eb37edf7be0@kernel.org \
    --to=mwalle@kernel.org \
    --cc=andrew@lunn.ch \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=florian.fainelli@broadcom.com \
    --cc=hkallweit1@gmail.com \
    --cc=kabel@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=lxu@maxlinear.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=salil.mehta@huawei.com \
    --cc=simon.horman@corigine.com \
    --cc=yisen.zhuang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).