Skip to contentSkip to conversation

net: mdio: Clause 45 support for mdio-bcm-unimac

PATCH Superseded Florian Fainelli 3 participants 10 messages Nov 17 – Nov 20

Subsystems (4)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Florian Fainelli

author maintainer
#0 lore raw

v3 cover net: mdio: Clause 45 support for mdio-bcm-unimac

This patch series adds support for Clause 45 MDIO operations within the mdio-bcm-unimac driver. The series breaks things up in this way:

- make the C45 bus scan done by the MDIO core carry on when nothing answers at an address, as the C22 scan already does
- scope the existing defines to make it clear what is C22 vs. C45 specific
- rework the polling completion loop to be a tighter loop
- add support for clause 45 operations

Changes in v3:

- patch 1: collect Reviewed-by tag from Nicolai Buchwitz, no code changes
- patches 2 and 3: no changes
- patch 4:
- after the ADDR cycle, make sure the controller is idle before starting the READ/WRITE cycle, since a wait_func() such as bcmgenet_mii_wait() returns 0 even when it timed out with START_BUSY still set
- take bus->mdio_lock in unimac_mdio_resume() around unimac_mdio_clk_set(), such that its read-modify-write of MDIO_CFG cannot race with the C22/C45 clause switch done by the accessors

Changes in v2:

- add patch 1, since providing read_c45/write_c45 makes the MDIO core scan for C45 PHYs on all addresses that are not masked. On a bus registered without a device tree node (e.g.: GENET with ACPI) a read from an empty address fails with -EIO, which get_phy_c45_ids() passed up instead of mapping it to -ENODEV, and made the whole bus registration fail. Reported by Nicolai Buchwitz.
- patches 2 and 3: collect Reviewed-by tags from Andrew Lunn and Nicolai Buchwitz, no code changes
- patch 4: no code changes

Florian Fainelli (4):
net: phy: Map C45 probe read errors to -ENODEV during bus scan
net: mdio: mdio-bcm-unimac: Define MDIO_OP_SHIFT
net: mdio: mdio-bcm-unimac: Poll for completion at 1us granularity
net: mdio: mdio-bcm-unimac: Add Clause 45 support

 drivers/net/mdio/mdio-bcm-unimac.c | 150 ++++++++++++++++++++++++++---
 drivers/net/phy/phy_device.c       |  24 ++++-
 2 files changed, 153 insertions(+), 21 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v3 1/4 net: phy: Map C45 probe read errors to -ENODEV during bus scan

get_phy_c22_id() maps a bus read error of -EIO or -ENODEV to -ENODEV, so that mdiobus_scan_bus_c22() carries on with the next address when nothing answers. get_phy_c45_ids() only does so for phy_c45_probe_present(); a failed read of the devices in package registers is turned into -EIO, which mdiobus_scan_bus_c45() treats as fatal and which makes the whole __mdiobus_register() fail.

MDIO controllers that report a read from an empty address as a bus error (e.g. MDIO_READ_FAIL on the UniMAC MDIO block) and that provide read_c45() with unmasked addresses therefore cannot be registered: the C45 scan fails on the first empty address.

Let get_phy_c45_devs_in_pkg() return the bus error and apply the same mapping as get_phy_c22_id() to the two places that look for a device at an
address: the loop over the MMDs and the MMD 0 fallback. Errors from the reads of the per-MMD identifiers, which only happen once a device has answered, are still reported as -EIO.

---
 drivers/net/phy/phy_device.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)
drivers/net/phy/phy_device.c +19 -5
index b39e945cebd7..4071041e1051 100644
@@ -780,7 +780,7 @@ static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
  * Description: reads devices in package registers of a MMD at @dev_addr
  * from PHY at @addr on @bus.
  *
- * Returns: 0 on success, -EIO on failure.
+ * Returns: 0 on success, or the negative error code from the bus read.
  */
 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
 				   u32 *devices_in_package)
@@ -789,17 +789,30 @@ static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
 
 	phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2);
 	if (phy_reg < 0)
-		return -EIO;
+		return phy_reg;
 	*devices_in_package = phy_reg << 16;
 
 	phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1);
 	if (phy_reg < 0)
-		return -EIO;
+		return phy_reg;
 	*devices_in_package |= phy_reg;
 
 	return 0;
 }
 
+/* get_phy_c45_probe_err - map a bus error seen while looking for a device.
+ * @err: negative error code returned by a bus read
+ *
+ * Like get_phy_c22_id(), report -ENODEV when the bus read failed with -EIO or
+ * -ENODEV, which is how a controller signals that nothing answered at this
+ * address, so that bus scanning carries on with the next address. Anything
+ * else is reported as -EIO.
+ */
+static int get_phy_c45_probe_err(int err)
+{
+	return (err == -EIO || err == -ENODEV) ? -ENODEV : -EIO;
+}
+
 /**
  * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
  * @bus: the target MII bus
@@ -845,7 +858,8 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr,
 		}
 		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
 		if (phy_reg < 0)
-			return -EIO;
+			/* returning -ENODEV doesn't stop bus scanning */
+			return get_phy_c45_probe_err(phy_reg);
 	}
 
 	if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) {
@@ -855,7 +869,7 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr,
 		 */
 		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
 		if (phy_reg < 0)
-			return -EIO;
+			return get_phy_c45_probe_err(phy_reg);
 
 		/* no device there, let's get out of here */
 		if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH net-next v3 1/4] net: phy: Map C45 probe read errors to -ENODEV during bus scan

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 3 (1)

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] On MDIO controllers that flag a missing turnaround as a bus error, get_phy_c45_ids() now treats one failed Devices-In-Package read…
- [Low] The subject says "during bus scan", but get_phy_c45_ids() is reached through get_phy_device(bus, addr, true) for every C45 caller,…
- [Low] The patch updates the kernel-doc of get_phy_c45_devs_in_pkg() but leaves the return-value docs of get_phy_c45_ids()…
- [Low] (Baseline: judged a false positive, kept only because requires_validation=true.) The concern says the commit message's…

Pre-existing issues:
- [Low] (Baseline: dismissed as a pre-existing low-severity issue, kept only because requires_validation=true.) In the second loop of…

--

net: phy: Map C45 probe read errors to -ENODEV during bus scan

This commit makes get_phy_c45_devs_in_pkg() return the raw bus error. It maps -EIO/-ENODEV from the devices-in-package reads in get_phy_c45_ids() to -ENODEV, the same way get_phy_c22_id() does. The intent is to let mdiobus_scan_bus_c45() move past empty addresses on controllers that report an unanswered read as a bus error, instead of failing __mdiobus_register().

Quoted 1 line

net: phy: Map C45 probe read errors to -ENODEV during bus scan

[Severity: Low]
The subject says "during bus scan". However, every C45 caller reaches get_phy_c45_ids() through get_phy_device(bus, addr, true). Does the new mapping also change behaviour outside the scan?

For DT/ACPI nodes compatible with "ethernet-phy-ieee802.3-c45", fwnode_mdiobus_register_phy() calls get_phy_device() directly:

drivers/net/mdio/fwnode_mdio.c:fwnode_mdiobus_register_phy() {
    ...
	if (is_c45 || fwnode_get_phy_id(child, &phy_id))
		phy = get_phy_device(bus, addr, is_c45);
    ...
}

and __of_mdiobus_parse_phys() handles -ENODEV and -EIO differently:

drivers/net/mdio/of_mdio.c:__of_mdiobus_parse_phys() {
    ...
		if (rc == -ENODEV)
			dev_err(&mdio->dev,
				"MDIO device at address %d is missing.\n",
				addr);
		else if (rc)
			goto exit;
    ...
}

Before this patch, a failed MDIO_DEVS1/MDIO_DEVS2 read made bus registration fail. Now it logs the "missing" message and the bus registers without that PHY.

SFP behaves the same way. sfp_sm_probe_phy() passes -ENODEV through. sfp_sm_main() then retries, eventually reports "no PHY detected" and starts the module. Before, an -EIO from mdio-i2c sent the module to SFP_S_FAIL.

Real controller faults reported as -EIO, such as a timed out C45 address phase, will also look like "no device" now.

This matches what get_phy_c22_id() already does. Could the commit message mention these callers as well?

Quoted 4 lines

MDIO controllers that report a read from an empty address as a bus error (e.g. MDIO_READ_FAIL on the UniMAC MDIO block) and that provide read_c45() with unmasked addresses therefore cannot be registered: the C45 scan fails on the first empty address.

[Severity: Low]
This isn't a bug, but at this point in the series mdio-bcm-unimac.c does not set bus->read_c45, so the UniMAC example cannot reach mdiobus_scan_bus_c45() yet.

The later patch "net: mdio: mdio-bcm-unimac: Add Clause 45 support" adds:

	bus->read_c45 = unimac_mdio_read_c45;
	bus->write_c45 = unimac_mdio_write_c45;

and its C45 read path returns -EIO on MDIO_READ_FAIL. So the premise holds by the end of the series, and the sentence above is already conditional on read_c45() being provided.

Could the commit message say that the UniMAC case is enabled by that follow-up patch?

drivers/net/phy/phy_device.c static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad) · line 784

Quoted 11 lines
drivers/net/phy/phy_device.c +1 -1
index b39e945cebd73..4071041e1051d 100644
@@ -780,7 +780,7 @@ static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
  * Description: reads devices in package registers of a MMD at @dev_addr
  * from PHY at @addr on @bus.
  *
- * Returns: 0 on success, -EIO on failure.
+ * Returns: 0 on success, or the negative error code from the bus read.
  */

[Severity: Low]
This kernel-doc is updated. Should the return value docs of get_phy_c45_ids() and of the exported phy_get_c45_ids() be updated too? The phy_get_c45_ids() doc still says:

* Returns zero on success, %-EIO on bus access error, or %-ENODEV if
* the "devices in package" is invalid.

and get_phy_c45_ids() still documents "%-EIO on bus access error". With this patch, a bus -EIO on the devices-in-package reads is returned as -ENODEV.

The only user outside this file, gpy_probe() in mxl-gpy.c, returns that value directly from probe():

	if (!phydev->is_c45) {
		ret = phy_get_c45_ids(phydev);
		if (ret < 0)
			return ret;
	}

A real bus -EIO there now becomes -ENODEV. The driver core treats that as a quiet match rejection at dev_dbg level, not a logged probe failure.

[ ... ]

drivers/net/phy/phy_device.c static int get_phy_c45_ids(struct mii_bus *bus, int addr, · line 863

Quoted 8 lines
drivers/net/phy/phy_device.c +2 -1
@@ -845,7 +858,8 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr,
 		}
 		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
 		if (phy_reg < 0)
-			return -EIO;
+			/* returning -ENODEV doesn't stop bus scanning */
+			return get_phy_c45_probe_err(phy_reg);
 	}

[Severity: Medium]
On controllers that report a missing turnaround as a bus error, could this skip a real PHY whose first probed MMD doesn't answer?

The loop depends on its condition to step past unimplemented MMDs:

	for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 ||
	     (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) {

The MMD 0 fallback below exists for PHYs such as the Cortina CS4315/CS4340. Both of these assume that an absent MMD reads back as 0xffff.

With the later C45 patch, UniMAC checks TA, and unimac_mdio_do_read() returns -EIO for any (addr, devad) pair that doesn't answer:

	if (!(bus->phy_ignore_ta_mask & 1 << phy_id) && (cmd & MDIO_READ_FAIL)) {
		ret = -EIO;
		goto out;
	}

So if the MMD 1 read fails, get_phy_c45_ids() now returns -ENODEV here. No later MMD and no MMD 0 fallback is tried. mdiobus_scan_bus_c45() then treats the address as empty:

			if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
				return PTR_ERR(phydev);

The DT path through fwnode_mdiobus_register_phy() reaches the same code.

These cases look affected:
- a PHY with no MMD 1 (PMA/PMD)
- a PHY whose MMD 1 reads 0 and whose next MMD doesn't answer
- a PHY that only answers on MMD 0

I haven't confirmed an in-tree controller and PHY pair that does this.

Before this patch the same setup made bus registration fail with -EIO, so no configuration that worked before breaks. With the patch, the PHY is left off the bus without any message.

Would it be more robust to treat an -EIO/-ENODEV devices-in-package read as "this MMD is absent" (devs_in_pkg set to all ones) and keep walking? -ENODEV would then be returned only after the MMD 0 read has also failed.

drivers/net/phy/phy_device.c static int get_phy_c45_ids(struct mii_bus *bus, int addr, · line 875

Quoted 9 lines
drivers/net/phy/phy_device.c +1 -1
@@ -855,7 +869,7 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr,
 		 */
 		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
 		if (phy_reg < 0)
-			return -EIO;
+			return get_phy_c45_probe_err(phy_reg);
 
 		/* no device there, let's get out of here */
 		if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)

[Severity: Low]
This is a pre-existing issue, not one introduced by this patch. Further down in get_phy_c45_ids(), the second loop returns the phy_c45_probe_present() error unchanged:

		if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
			...
			ret = phy_c45_probe_present(bus, addr, i);
			if (ret < 0)
				return ret;

The MII_PHYSID1/MII_PHYSID2 reads right after it return -EIO instead. This path can also return an errno outside the documented 0/-EIO/-ENODEV set.

The commit message says reads made after a device has answered are still reported as -EIO. Should this path be normalized the same way?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117233149.1693881-1-florian.fainelli%40broadcom.com
author maintainer
#3 lore raw

Re: [PATCH net-next v3 1/4] net: phy: Map C45 probe read errors to -ENODEV during bus scan

Quoted 215 lines · On 11/19/2026 6:34 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 3 (1)

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] On MDIO controllers that flag a missing turnaround as a bus error, get_phy_c45_ids() now treats one failed Devices-In-Package read…
- [Low] The subject says "during bus scan", but get_phy_c45_ids() is reached through get_phy_device(bus, addr, true) for every C45 caller,…
- [Low] The patch updates the kernel-doc of get_phy_c45_devs_in_pkg() but leaves the return-value docs of get_phy_c45_ids()…
- [Low] (Baseline: judged a false positive, kept only because requires_validation=true.) The concern says the commit message's…

Pre-existing issues:
- [Low] (Baseline: dismissed as a pre-existing low-severity issue, kept only because requires_validation=true.) In the second loop of…

--

net: phy: Map C45 probe read errors to -ENODEV during bus scan

This commit makes get_phy_c45_devs_in_pkg() return the raw bus error. It maps -EIO/-ENODEV from the devices-in-package reads in get_phy_c45_ids() to -ENODEV, the same way get_phy_c22_id() does. The intent is to let mdiobus_scan_bus_c45() move past empty addresses on controllers that report an unanswered read as a bus error, instead of failing __mdiobus_register().

Quoted 1 line

net: phy: Map C45 probe read errors to -ENODEV during bus scan

[Severity: Low]
The subject says "during bus scan". However, every C45 caller reaches get_phy_c45_ids() through get_phy_device(bus, addr, true). Does the new mapping also change behaviour outside the scan?

For DT/ACPI nodes compatible with "ethernet-phy-ieee802.3-c45", fwnode_mdiobus_register_phy() calls get_phy_device() directly:

drivers/net/mdio/fwnode_mdio.c:fwnode_mdiobus_register_phy() {
     ...
	if (is_c45 || fwnode_get_phy_id(child, &phy_id))
		phy = get_phy_device(bus, addr, is_c45);
     ...
}

and __of_mdiobus_parse_phys() handles -ENODEV and -EIO differently:

drivers/net/mdio/of_mdio.c:__of_mdiobus_parse_phys() {
     ...
		if (rc == -ENODEV)
			dev_err(&mdio->dev,
				"MDIO device at address %d is missing.\n",
				addr);
		else if (rc)
			goto exit;
     ...
}

Before this patch, a failed MDIO_DEVS1/MDIO_DEVS2 read made bus registration fail. Now it logs the "missing" message and the bus registers without that PHY.

SFP behaves the same way. sfp_sm_probe_phy() passes -ENODEV through. sfp_sm_main() then retries, eventually reports "no PHY detected" and starts the module. Before, an -EIO from mdio-i2c sent the module to SFP_S_FAIL.

Real controller faults reported as -EIO, such as a timed out C45 address phase, will also look like "no device" now.

This matches what get_phy_c22_id() already does. Could the commit message mention these callers as well?

Quoted 4 lines

MDIO controllers that report a read from an empty address as a bus error (e.g. MDIO_READ_FAIL on the UniMAC MDIO block) and that provide read_c45() with unmasked addresses therefore cannot be registered: the C45 scan fails on the first empty address.

[Severity: Low]
This isn't a bug, but at this point in the series mdio-bcm-unimac.c does not set bus->read_c45, so the UniMAC example cannot reach mdiobus_scan_bus_c45() yet.

The later patch "net: mdio: mdio-bcm-unimac: Add Clause 45 support" adds:

	bus->read_c45 = unimac_mdio_read_c45;
	bus->write_c45 = unimac_mdio_write_c45;

and its C45 read path returns -EIO on MDIO_READ_FAIL. So the premise holds by the end of the series, and the sentence above is already conditional on read_c45() being provided.

Could the commit message say that the UniMAC case is enabled by that follow-up patch?

drivers/net/phy/phy_device.c static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad) · line 784

Quoted 11 lines
drivers/net/phy/phy_device.c +1 -1
index b39e945cebd73..4071041e1051d 100644
@@ -780,7 +780,7 @@ static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
   * Description: reads devices in package registers of a MMD at @dev_addr
   * from PHY at @addr on @bus.
   *
- * Returns: 0 on success, -EIO on failure.
+ * Returns: 0 on success, or the negative error code from the bus read.
   */

[Severity: Low]
This kernel-doc is updated. Should the return value docs of get_phy_c45_ids() and of the exported phy_get_c45_ids() be updated too? The phy_get_c45_ids() doc still says:

* Returns zero on success, %-EIO on bus access error, or %-ENODEV if
* the "devices in package" is invalid.

and get_phy_c45_ids() still documents "%-EIO on bus access error". With this patch, a bus -EIO on the devices-in-package reads is returned as -ENODEV.

The only user outside this file, gpy_probe() in mxl-gpy.c, returns that value directly from probe():

	if (!phydev->is_c45) {
		ret = phy_get_c45_ids(phydev);
		if (ret < 0)
			return ret;
	}

A real bus -EIO there now becomes -ENODEV. The driver core treats that as a quiet match rejection at dev_dbg level, not a logged probe failure.

[ ... ]

drivers/net/phy/phy_device.c static int get_phy_c45_ids(struct mii_bus *bus, int addr, · line 863

Quoted 8 lines
drivers/net/phy/phy_device.c +2 -1
@@ -845,7 +858,8 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr,
  		}
  		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
  		if (phy_reg < 0)
-			return -EIO;
+			/* returning -ENODEV doesn't stop bus scanning */
+			return get_phy_c45_probe_err(phy_reg);
  	}

[Severity: Medium]
On controllers that report a missing turnaround as a bus error, could this skip a real PHY whose first probed MMD doesn't answer?

The loop depends on its condition to step past unimplemented MMDs:

	for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 ||
	     (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) {

The MMD 0 fallback below exists for PHYs such as the Cortina CS4315/CS4340. Both of these assume that an absent MMD reads back as 0xffff.

With the later C45 patch, UniMAC checks TA, and unimac_mdio_do_read() returns -EIO for any (addr, devad) pair that doesn't answer:

	if (!(bus->phy_ignore_ta_mask & 1 << phy_id) && (cmd & MDIO_READ_FAIL)) {
		ret = -EIO;
		goto out;
	}

So if the MMD 1 read fails, get_phy_c45_ids() now returns -ENODEV here. No later MMD and no MMD 0 fallback is tried. mdiobus_scan_bus_c45() then treats the address as empty:

			if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
				return PTR_ERR(phydev);

The DT path through fwnode_mdiobus_register_phy() reaches the same code.

These cases look affected:
- a PHY with no MMD 1 (PMA/PMD)
- a PHY whose MMD 1 reads 0 and whose next MMD doesn't answer
- a PHY that only answers on MMD 0

I haven't confirmed an in-tree controller and PHY pair that does this.

Before this patch the same setup made bus registration fail with -EIO, so no configuration that worked before breaks. With the patch, the PHY is left off the bus without any message.

Would it be more robust to treat an -EIO/-ENODEV devices-in-package read as "this MMD is absent" (devs_in_pkg set to all ones) and keep walking? -ENODEV would then be returned only after the MMD 0 read has also failed.

drivers/net/phy/phy_device.c static int get_phy_c45_ids(struct mii_bus *bus, int addr, · line 875

Quoted 9 lines
drivers/net/phy/phy_device.c +1 -1
@@ -855,7 +869,7 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr,
  		 */
  		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
  		if (phy_reg < 0)
-			return -EIO;
+			return get_phy_c45_probe_err(phy_reg);
 
  		/* no device there, let's get out of here */
  		if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)

[Severity: Low]
This is a pre-existing issue, not one introduced by this patch. Further down in get_phy_c45_ids(), the second loop returns the phy_c45_probe_present() error unchanged:

		if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
			...
			ret = phy_c45_probe_present(bus, addr, i);
			if (ret < 0)
				return ret;
Quoted 6 lines

The MII_PHYSID1/MII_PHYSID2 reads right after it return -EIO instead. This path can also return an errno outside the documented 0/-EIO/-ENODEV set.

The commit message says reads made after a device has answered are still reported as -EIO. Should this path be normalized the same way?

Because this one is a pre-existing issue, it won't be addressed. All of the other comments incorporated into v4.

pw-bot: cr

Signature · 1 line
Florian
author maintainer
#4 lore raw

PATCH v3 2/4 net: mdio: mdio-bcm-unimac: Define MDIO_OP_SHIFT

In reply to Florian Fainelli

The MDIO_CMD operation code field starts at bit 26. Define MDIO_OP_SHIFT for it and use it to build the opcodes instead of open coding the shift.

While at it, rename MDIO_RD and MDIO_WR to MDIO_RD_C22 and MDIO_WR_C22 to make it clear that these are the Clause 22 opcodes, in preparation for adding the Clause 45 ones.

No functional change.

---
 drivers/net/mdio/mdio-bcm-unimac.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/net/mdio/mdio-bcm-unimac.c +6 -4
index 80fb3b5b52ab..5f79f31220f2 100644
@@ -21,8 +21,9 @@
 #define MDIO_CMD		0x00
 #define  MDIO_START_BUSY	(1 << 29)
 #define  MDIO_READ_FAIL		(1 << 28)
-#define  MDIO_RD		(2 << 26)
-#define  MDIO_WR		(1 << 26)
+#define  MDIO_OP_SHIFT		26
+#define  MDIO_RD_C22		(2 << MDIO_OP_SHIFT)
+#define  MDIO_WR_C22		(1 << MDIO_OP_SHIFT)
 #define  MDIO_PMD_SHIFT		21
 #define  MDIO_PMD_MASK		0x1F
 #define  MDIO_REG_SHIFT		16
@@ -99,7 +100,8 @@ static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
 		return ret;
 
 	/* Prepare the read operation */
-	cmd = MDIO_RD | (phy_id << MDIO_PMD_SHIFT) | (reg << MDIO_REG_SHIFT);
+	cmd = MDIO_RD_C22 | (phy_id << MDIO_PMD_SHIFT) |
+		(reg << MDIO_REG_SHIFT);
 	unimac_mdio_writel(priv, cmd, MDIO_CMD);
 
 	/* Start MDIO transaction */
@@ -139,7 +141,7 @@ static int unimac_mdio_write(struct mii_bus *bus, int phy_id,
 		return ret;
 
 	/* Prepare the write operation */
-	cmd = MDIO_WR | (phy_id << MDIO_PMD_SHIFT) |
+	cmd = MDIO_WR_C22 | (phy_id << MDIO_PMD_SHIFT) |
 		(reg << MDIO_REG_SHIFT) | (0xffff & val);
 	unimac_mdio_writel(priv, cmd, MDIO_CMD);
 
Signature · 1 line
2.34.1
author maintainer
#5 lore raw

PATCH v3 3/4 net: mdio: mdio-bcm-unimac: Poll for completion at 1us granularity

In reply to Florian Fainelli

The current polling code unconditionally waits 30us, which was sized for a ~25us Clause 22 transaction, and then polls MDIO_CMD with a sleep of up to 2ms between samples.

Adding Clause 45 support means that every access becomes two back to back transactions (an ADDR cycle followed by a READ/WRITE cycle), so a fixed delay sized for a single C22 transaction is no longer meaningful, and a sleep of up to 2ms between samples can add significant latency to each access when the transaction has not completed at the first sample.

Instead, poll the MDIO_START_BUSY bit every microsecond so that the completion is noticed as soon as it happens, regardless of the type of transaction. This turns the wait into a busy wait, which is bounded by a timeout reduced from 100ms to 1ms: a 64 bit MDIO frame takes about 33us at the lowest MDIO clock the driver can program from a 250MHz reference clock, so even a two frame C45 access completes well within that.

---
 drivers/net/mdio/mdio-bcm-unimac.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
drivers/net/mdio/mdio-bcm-unimac.c +15 -8
index 5f79f31220f2..df267bb82696 100644
@@ -6,8 +6,8 @@
  */
 
 #include <linux/clk.h>
-#include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -36,6 +36,13 @@
 #define  MDIO_CLK_DIV_MASK	0x3F
 #define  MDIO_SUPP_PREAMBLE	(1 << 12)
 
+/* A 64 bit MDIO frame takes ~25us at 2.5MHz and ~33us at the lowest MDIO
+ * clock the driver can program from a 250MHz reference clock. A C45 access is
+ * two frames, so a 1ms timeout leaves a wide margin.
+ */
+#define MDIO_POLL_DELAY_US	1
+#define MDIO_POLL_TIMEOUT_US	1000
+
 struct unimac_mdio_priv {
 	struct mii_bus		*mii_bus;
 	void __iomem		*base;
@@ -79,14 +86,14 @@ static int unimac_mdio_poll(void *wait_func_data)
 	struct unimac_mdio_priv *priv = wait_func_data;
 	u32 val;
 
-	/*
-	 * C22 transactions should take ~25 usec, will need to adjust
-	 * if C45 support is added.
+	/* C22 transactions should take ~25 usec, C45 transactions take about
+	 * twice as long since they need an address and a data cycle.
 	 */
-	udelay(30);
-
-	return read_poll_timeout(unimac_mdio_readl, val, !(val & MDIO_START_BUSY),
-				 2000, 100000, false, priv, MDIO_CMD);
+	return read_poll_timeout_atomic(unimac_mdio_readl, val,
+					!(val & MDIO_START_BUSY),
+					MDIO_POLL_DELAY_US,
+					MDIO_POLL_TIMEOUT_US, false, priv,
+					MDIO_CMD);
 }
 
 static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
Signature · 1 line
2.34.1
author maintainer
#6 lore raw

PATCH v3 4/4 net: mdio: mdio-bcm-unimac: Add Clause 45 support

In reply to Florian Fainelli

Add Clause 45 read/write support to the UniMAC MDIO bus controller driver by implementing the read_c45/write_c45 bus callbacks. Each access performs the two-step C45 sequence (ADDR cycle followed by a READ/WRITE cycle) under the bus lock and switches MDIO_CFG between C22 and C45 mode as needed; MDIO_CFG is only written when the mode actually changes.

Also skip the BMSR workaround read in unimac_mdio_reset() for children compatible with "ethernet-phy-ieee802.3-c45", since such PHYs are not expected to respond to C22 accesses.

Note that providing read_c45/write_c45 also makes the MDIO core scan for C45 PHYs at registration time, on addresses that are not masked and do not already have a C22 device.

---
 drivers/net/mdio/mdio-bcm-unimac.c | 125 +++++++++++++++++++++++++++--
 1 file changed, 117 insertions(+), 8 deletions(-)
drivers/net/mdio/mdio-bcm-unimac.c +117 -8
index df267bb82696..ec0f2e8070ee 100644
@@ -24,10 +24,16 @@
 #define  MDIO_OP_SHIFT		26
 #define  MDIO_RD_C22		(2 << MDIO_OP_SHIFT)
 #define  MDIO_WR_C22		(1 << MDIO_OP_SHIFT)
+#define  MDIO_ADDR_C45		(0 << MDIO_OP_SHIFT)
+#define  MDIO_WR_C45		(1 << MDIO_OP_SHIFT)
+#define  MDIO_RD_C45		(3 << MDIO_OP_SHIFT)
 #define  MDIO_PMD_SHIFT		21
 #define  MDIO_PMD_MASK		0x1F
 #define  MDIO_REG_SHIFT		16
 #define  MDIO_REG_MASK		0x1F
+#define  MDIO_DEVAD_SHIFT	16
+#define  MDIO_DEVAD_MASK	GENMASK(20, 16)
+#define  MDIO_C45_REG_MASK	GENMASK(15, 0)
 
 #define MDIO_CFG		0x04
 #define  MDIO_C22		(1 << 0)
@@ -96,7 +102,53 @@ static int unimac_mdio_poll(void *wait_func_data)
 					MDIO_CMD);
 }
 
-static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
+static u32 unimac_mdio_c45_devad(int devad)
+{
+	return ((u32)devad << MDIO_DEVAD_SHIFT) & MDIO_DEVAD_MASK;
+}
+
+static int unimac_mdio_addr_c45(struct unimac_mdio_priv *priv,
+				int phy_id, int devad, int reg)
+{
+	u32 cmd;
+	int ret;
+
+	/* Prepare the addr c45 operation */
+	cmd = MDIO_ADDR_C45 | (phy_id << MDIO_PMD_SHIFT) |
+	      unimac_mdio_c45_devad(devad) | (reg & MDIO_C45_REG_MASK);
+	unimac_mdio_writel(priv, cmd, MDIO_CMD);
+
+	/* Start MDIO transaction */
+	unimac_mdio_start(priv);
+
+	ret = priv->wait_func(priv->wait_func_data);
+	if (ret)
+		return ret;
+
+	/* Some wait_func implementations (e.g.: bcmgenet_mii_wait()) return 0
+	 * even when they timed out with the controller still busy. The data
+	 * cycle must not be started before the ADDR cycle has completed, so
+	 * make sure the controller is idle.
+	 */
+	return unimac_mdio_poll(priv);
+}
+
+static void unimac_mdio_set_clause(struct unimac_mdio_priv *priv, bool is_c45)
+{
+	u32 cfg = unimac_mdio_readl(priv, MDIO_CFG);
+	u32 new_cfg = cfg;
+
+	if (is_c45)
+		new_cfg &= ~MDIO_C22;
+	else
+		new_cfg |= MDIO_C22;
+
+	if (new_cfg != cfg)
+		unimac_mdio_writel(priv, new_cfg, MDIO_CFG);
+}
+
+static int unimac_mdio_do_read(struct mii_bus *bus, int phy_id, int devad,
+			       int reg, bool is_c45)
 {
 	struct unimac_mdio_priv *priv = bus->priv;
 	int ret;
@@ -107,8 +159,19 @@ static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
 		return ret;
 
 	/* Prepare the read operation */
-	cmd = MDIO_RD_C22 | (phy_id << MDIO_PMD_SHIFT) |
-		(reg << MDIO_REG_SHIFT);
+	unimac_mdio_set_clause(priv, is_c45);
+	if (is_c45) {
+		ret = unimac_mdio_addr_c45(priv, phy_id, devad, reg);
+		if (ret)
+			goto out;
+
+		cmd = MDIO_RD_C45 | (phy_id << MDIO_PMD_SHIFT) |
+			unimac_mdio_c45_devad(devad) |
+			(reg & MDIO_C45_REG_MASK);
+	} else {
+		cmd = MDIO_RD_C22 | (phy_id << MDIO_PMD_SHIFT) |
+			(reg << MDIO_REG_SHIFT);
+	}
 	unimac_mdio_writel(priv, cmd, MDIO_CMD);
 
 	/* Start MDIO transaction */
@@ -136,8 +199,8 @@ static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
 	return ret;
 }
 
-static int unimac_mdio_write(struct mii_bus *bus, int phy_id,
-			     int reg, u16 val)
+static int unimac_mdio_do_write(struct mii_bus *bus, int phy_id, int devad,
+				int reg, u16 val, bool is_c45)
 {
 	struct unimac_mdio_priv *priv = bus->priv;
 	u32 cmd;
@@ -148,18 +211,50 @@ static int unimac_mdio_write(struct mii_bus *bus, int phy_id,
 		return ret;
 
 	/* Prepare the write operation */
-	cmd = MDIO_WR_C22 | (phy_id << MDIO_PMD_SHIFT) |
-		(reg << MDIO_REG_SHIFT) | (0xffff & val);
+	unimac_mdio_set_clause(priv, is_c45);
+	if (is_c45) {
+		ret = unimac_mdio_addr_c45(priv, phy_id, devad, reg);
+		if (ret)
+			goto out;
+
+		cmd = MDIO_WR_C45 | (phy_id << MDIO_PMD_SHIFT) |
+			unimac_mdio_c45_devad(devad) | (0xffff & val);
+	} else {
+		cmd = MDIO_WR_C22 | (phy_id << MDIO_PMD_SHIFT) |
+			(reg << MDIO_REG_SHIFT) | (0xffff & val);
+	}
 	unimac_mdio_writel(priv, cmd, MDIO_CMD);
 
 	unimac_mdio_start(priv);
 
 	ret = priv->wait_func(priv->wait_func_data);
+out:
 	clk_disable_unprepare(priv->clk);
 
 	return ret;
 }
 
+static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
+{
+	return unimac_mdio_do_read(bus, phy_id, 0, reg, false);
+}
+
+static int unimac_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val)
+{
+	return unimac_mdio_do_write(bus, phy_id, 0, reg, val, false);
+}
+
+static int unimac_mdio_read_c45(struct mii_bus *bus, int addr, int dev, int reg)
+{
+	return unimac_mdio_do_read(bus, addr, dev, reg, true);
+}
+
+static int unimac_mdio_write_c45(struct mii_bus *bus, int addr, int dev,
+				 int reg, u16 val)
+{
+	return unimac_mdio_do_write(bus, addr, dev, reg, val, true);
+}
+
 /* Workaround for integrated BCM7xxx Gigabit PHYs which have a problem with
  * their internal MDIO management controller making them fail to successfully
  * be read from or written to for the first transaction.  We insert a dummy
@@ -190,6 +285,10 @@ static int unimac_mdio_reset(struct mii_bus *bus)
 			if (addr < 0)
 				continue;
 
+			if (of_device_is_compatible(child,
+						    "ethernet-phy-ieee802.3-c45"))
+				continue;
+
 			read_mask |= 1 << addr;
 		}
 	}
@@ -301,6 +400,8 @@ static int unimac_mdio_probe(struct platform_device *pdev)
 	bus->parent = &pdev->dev;
 	bus->read = unimac_mdio_read;
 	bus->write = unimac_mdio_write;
+	bus->read_c45 = unimac_mdio_read_c45;
+	bus->write_c45 = unimac_mdio_write_c45;
 	bus->reset = unimac_mdio_reset;
 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", pdev->name, pdev->id);
 
@@ -336,8 +437,16 @@ static void unimac_mdio_remove(struct platform_device *pdev)
 static int __maybe_unused unimac_mdio_resume(struct device *d)
 {
 	struct unimac_mdio_priv *priv = dev_get_drvdata(d);
+	int ret;
+
+	/* MDIO_CFG is also modified by the MDIO accessors to switch between
+	 * Clause 22 and Clause 45, which run under the bus lock.
+	 */
+	mutex_lock(&priv->mii_bus->mdio_lock);
+	ret = unimac_mdio_clk_set(priv);
+	mutex_unlock(&priv->mii_bus->mdio_lock);
 
-	return unimac_mdio_clk_set(priv);
+	return ret;
 }
 
 static SIMPLE_DEV_PM_OPS(unimac_mdio_pm_ops,
Signature · 1 line
2.34.1
maintainer Reviewed-by on v3 4/4
#7 lore raw

Re: [PATCH net-next v3 4/4] net: mdio: mdio-bcm-unimac: Add Clause 45 support

Quoted 14 lines · Florian Fainelli

Add Clause 45 read/write support to the UniMAC MDIO bus controller driver by implementing the read_c45/write_c45 bus callbacks. Each access performs the two-step C45 sequence (ADDR cycle followed by a READ/WRITE cycle) under the bus lock and switches MDIO_CFG between C22 and C45 mode as needed; MDIO_CFG is only written when the mode actually changes.

Also skip the BMSR workaround read in unimac_mdio_reset() for children compatible with "ethernet-phy-ieee802.3-c45", since such PHYs are not expected to respond to C22 accesses.

Note that providing read_c45/write_c45 also makes the MDIO core scan for C45 PHYs at registration time, on addresses that are not masked and do not already have a C22 device.

Quoted 5 lines

Thanks,
Nicolai

#8 lore raw

Re: [PATCH net-next v3 4/4] net: mdio: mdio-bcm-unimac: Add Clause 45 support

In reply to Florian Fainelli
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] In drivers/net/mdio/mdio-bcm-unimac.c, the patch's own comment in unimac_mdio_addr_c45() says some wait_func implementations…
- [Low] The commit message does not mention the change to unimac_mdio_resume() in drivers/net/mdio/mdio-bcm-unimac.c.

--

net: mdio: mdio-bcm-unimac: Add Clause 45 support

This adds Clause 45 read and write callbacks to the UniMAC MDIO driver. Each access does an ADDR cycle followed by a READ or WRITE cycle, and MDIO_CFG is switched between C22 and C45 mode as needed. It also skips the BMSR workaround read for children compatible with "ethernet-phy-ieee802.3-c45".

Quoted 4 lines
drivers/net/mdio/mdio-bcm-unimac.c +0 -0
index df267bb826966..ec0f2e8070eeb 100644

[ ... ]

Quoted 1 line
drivers/net/mdio/mdio-bcm-unimac.c +0 -0
@@ -96,7 +102,53 @@ static int unimac_mdio_poll(void *wait_func_data)

[ ... ]

Quoted 7 lines
drivers/net/mdio/mdio-bcm-unimac.c +7 -0
+	/* Some wait_func implementations (e.g.: bcmgenet_mii_wait()) return 0
+	 * even when they timed out with the controller still busy. The data
+	 * cycle must not be started before the ADDR cycle has completed, so
+	 * make sure the controller is idle.
+	 */
+	return unimac_mdio_poll(priv);
+}

[ ... ]

Quoted 5 lines
drivers/net/mdio/mdio-bcm-unimac.c +0 -1
@@ -107,8 +159,19 @@ static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
 		return ret;
 
 	/* Prepare the read operation */
-	cmd = MDIO_RD_C22 | (phy_id << MDIO_PMD_SHIFT) |

drivers/net/mdio/mdio-bcm-unimac.c static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg) · line 170

Quoted 10 lines
drivers/net/mdio/mdio-bcm-unimac.c +9 -1
-		(reg << MDIO_REG_SHIFT);
+	unimac_mdio_set_clause(priv, is_c45);
+	if (is_c45) {
+		ret = unimac_mdio_addr_c45(priv, phy_id, devad, reg);
+		if (ret)
+			goto out;
+
+		cmd = MDIO_RD_C45 | (phy_id << MDIO_PMD_SHIFT) |
+			unimac_mdio_c45_devad(devad) |
+			(reg & MDIO_C45_REG_MASK);

[Severity: Medium]
The comment in unimac_mdio_addr_c45() says bcmgenet_mii_wait() can return 0 while MDIO_START_BUSY is still set. The extra unimac_mdio_poll() runs only after the ADDR cycle. Can the same early return happen on the C45 READ data cycle?

bcmgenet_mii_wait() returns 0 every time:

drivers/net/ethernet/broadcom/genet/bcmmii.c:bcmgenet_mii_wait() {
	wait_event_timeout(priv->wq,
			   !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD)
			   & MDIO_START_BUSY),
			   HZ / 100);
	return 0;
}

At probe time, the GENET ISR that calls wake_up(&priv->wq) is not installed yet, because request_irq() happens later in bcmgenet_open(). On v1/v2, UMAC_IRQ_MDIO_EVENT is not enabled at all. In both cases these waits depend only on the timeout. With HZ=100 the timeout is one jiffy, which can expire at the next tick.

The RD_C45 command places reg in MDIO_CMD[15:0]. After wait_func(), unimac_mdio_do_read() does this without checking MDIO_START_BUSY:

	cmd = unimac_mdio_readl(priv, MDIO_CMD);
	...
	ret = cmd & 0xffff;

If MDIO_CMD[15:0] still holds reg until the hardware writes the read data, could an early return give the caller reg as the PHY data? This would also happen before MDIO_READ_FAIL is latched. For example, a read of MDIO_DEVS2 would return 6 and a read of MDIO_DEVS1 would return 5. get_phy_c45_devs_in_pkg() could accept that as a plausible devs_in_pkg.

read_c45 is now set, and the non-DT GENET path leaves bus->phy_mask at 0. So __mdiobus_register()->mdiobus_scan_bus_c45() issues these reads on every address that has no C22 device. Could get_phy_c45_ids() then register a bogus C45 PHY?

The write side looks similar. unimac_mdio_do_write() returns 0 after wait_func() even if the C45 WRITE is still in flight. Could the next access's unimac_mdio_set_clause() then flip the MDIO_C22 bit in MDIO_CFG, or write its ADDR command to MDIO_CMD, while the previous frame is still going out?

The C22 data cycle already returned early in the same way before this patch. The reg-as-data result and the MDIO_CFG switch mid-frame are new, though.

Would it make sense to have bcmgenet_mii_wait() return -ETIMEDOUT on timeout, or to run the idle poll after every cycle and not only between ADDR and DATA?

[ ... ]

drivers/net/mdio/mdio-bcm-unimac.c static void unimac_mdio_remove(struct platform_device *pdev) · line 447

Quoted 12 lines
drivers/net/mdio/mdio-bcm-unimac.c +8 -0
@@ -336,8 +437,16 @@ static void unimac_mdio_remove(struct platform_device *pdev)
 static int __maybe_unused unimac_mdio_resume(struct device *d)
 {
 	struct unimac_mdio_priv *priv = dev_get_drvdata(d);
+	int ret;
+
+	/* MDIO_CFG is also modified by the MDIO accessors to switch between
+	 * Clause 22 and Clause 45, which run under the bus lock.
+	 */
+	mutex_lock(&priv->mii_bus->mdio_lock);
+	ret = unimac_mdio_clk_set(priv);
+	mutex_unlock(&priv->mii_bus->mdio_lock);

[Severity: Low]
This isn't a bug, but the commit message doesn't mention this change to unimac_mdio_resume(). The resume callback now holds priv->mii_bus->mdio_lock around unimac_mdio_clk_set(), because unimac_mdio_set_clause() does a read-modify-write of MDIO_CFG, the same register that holds the clock divider.

The message covers the C45 accessors, the BMSR workaround skip and the C45 scan. Could it also describe this locking change in the resume path?

Quoted 4 lines
drivers/net/mdio/mdio-bcm-unimac.c +1 -1
-	return unimac_mdio_clk_set(priv);
+	return ret;
 }
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117233149.1693881-1-florian.fainelli%40broadcom.com
author maintainer
#9 lore raw

Re: [PATCH net-next v3 4/4] net: mdio: mdio-bcm-unimac: Add Clause 45 support

Quoted 139 lines · On 11/19/2026 6:34 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] In drivers/net/mdio/mdio-bcm-unimac.c, the patch's own comment in unimac_mdio_addr_c45() says some wait_func implementations…
- [Low] The commit message does not mention the change to unimac_mdio_resume() in drivers/net/mdio/mdio-bcm-unimac.c.

--

net: mdio: mdio-bcm-unimac: Add Clause 45 support

This adds Clause 45 read and write callbacks to the UniMAC MDIO driver. Each access does an ADDR cycle followed by a READ or WRITE cycle, and MDIO_CFG is switched between C22 and C45 mode as needed. It also skips the BMSR workaround read for children compatible with "ethernet-phy-ieee802.3-c45".

Quoted 4 lines
drivers/net/mdio/mdio-bcm-unimac.c +0 -0
index df267bb826966..ec0f2e8070eeb 100644

[ ... ]

Quoted 1 line
drivers/net/mdio/mdio-bcm-unimac.c +0 -0
@@ -96,7 +102,53 @@ static int unimac_mdio_poll(void *wait_func_data)

[ ... ]

Quoted 7 lines
drivers/net/mdio/mdio-bcm-unimac.c +7 -0
+	/* Some wait_func implementations (e.g.: bcmgenet_mii_wait()) return 0
+	 * even when they timed out with the controller still busy. The data
+	 * cycle must not be started before the ADDR cycle has completed, so
+	 * make sure the controller is idle.
+	 */
+	return unimac_mdio_poll(priv);
+}

[ ... ]

Quoted 5 lines
drivers/net/mdio/mdio-bcm-unimac.c +0 -1
@@ -107,8 +159,19 @@ static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  		return ret;
 
  	/* Prepare the read operation */
-	cmd = MDIO_RD_C22 | (phy_id << MDIO_PMD_SHIFT) |

drivers/net/mdio/mdio-bcm-unimac.c static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg) · line 170

Quoted 10 lines
drivers/net/mdio/mdio-bcm-unimac.c +9 -1
-		(reg << MDIO_REG_SHIFT);
+	unimac_mdio_set_clause(priv, is_c45);
+	if (is_c45) {
+		ret = unimac_mdio_addr_c45(priv, phy_id, devad, reg);
+		if (ret)
+			goto out;
+
+		cmd = MDIO_RD_C45 | (phy_id << MDIO_PMD_SHIFT) |
+			unimac_mdio_c45_devad(devad) |
+			(reg & MDIO_C45_REG_MASK);

[Severity: Medium]
The comment in unimac_mdio_addr_c45() says bcmgenet_mii_wait() can return 0 while MDIO_START_BUSY is still set. The extra unimac_mdio_poll() runs only after the ADDR cycle. Can the same early return happen on the C45 READ data cycle?

bcmgenet_mii_wait() returns 0 every time:

drivers/net/ethernet/broadcom/genet/bcmmii.c:bcmgenet_mii_wait() {
	wait_event_timeout(priv->wq,
			   !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD)
			   & MDIO_START_BUSY),
			   HZ / 100);
	return 0;
}

At probe time, the GENET ISR that calls wake_up(&priv->wq) is not installed yet, because request_irq() happens later in bcmgenet_open(). On v1/v2, UMAC_IRQ_MDIO_EVENT is not enabled at all. In both cases these waits depend only on the timeout. With HZ=100 the timeout is one jiffy, which can expire at the next tick.

The RD_C45 command places reg in MDIO_CMD[15:0]. After wait_func(), unimac_mdio_do_read() does this without checking MDIO_START_BUSY:

	cmd = unimac_mdio_readl(priv, MDIO_CMD);
	...
	ret = cmd & 0xffff;

If MDIO_CMD[15:0] still holds reg until the hardware writes the read data, could an early return give the caller reg as the PHY data? This would also happen before MDIO_READ_FAIL is latched. For example, a read of MDIO_DEVS2 would return 6 and a read of MDIO_DEVS1 would return 5. get_phy_c45_devs_in_pkg() could accept that as a plausible devs_in_pkg.

read_c45 is now set, and the non-DT GENET path leaves bus->phy_mask at 0. So __mdiobus_register()->mdiobus_scan_bus_c45() issues these reads on every address that has no C22 device. Could get_phy_c45_ids() then register a bogus C45 PHY?

The write side looks similar. unimac_mdio_do_write() returns 0 after wait_func() even if the C45 WRITE is still in flight. Could the next access's unimac_mdio_set_clause() then flip the MDIO_C22 bit in MDIO_CFG, or write its ADDR command to MDIO_CMD, while the previous frame is still going out?

The C22 data cycle already returned early in the same way before this patch. The reg-as-data result and the MDIO_CFG switch mid-frame are new, though.

Would it make sense to have bcmgenet_mii_wait() return -ETIMEDOUT on timeout, or to run the idle poll after every cycle and not only between ADDR and DATA?

[ ... ]

drivers/net/mdio/mdio-bcm-unimac.c static void unimac_mdio_remove(struct platform_device *pdev) · line 447

Quoted 12 lines
drivers/net/mdio/mdio-bcm-unimac.c +8 -0
@@ -336,8 +437,16 @@ static void unimac_mdio_remove(struct platform_device *pdev)
  static int __maybe_unused unimac_mdio_resume(struct device *d)
  {
  	struct unimac_mdio_priv *priv = dev_get_drvdata(d);
+	int ret;
+
+	/* MDIO_CFG is also modified by the MDIO accessors to switch between
+	 * Clause 22 and Clause 45, which run under the bus lock.
+	 */
+	mutex_lock(&priv->mii_bus->mdio_lock);
+	ret = unimac_mdio_clk_set(priv);
+	mutex_unlock(&priv->mii_bus->mdio_lock);

[Severity: Low]
This isn't a bug, but the commit message doesn't mention this change to unimac_mdio_resume(). The resume callback now holds priv->mii_bus->mdio_lock around unimac_mdio_clk_set(), because unimac_mdio_set_clause() does a read-modify-write of MDIO_CFG, the same register that holds the clock divider.

Quoted 8 lines

The message covers the C45 accessors, the BMSR workaround skip and the C45 scan. Could it also describe this locking change in the resume path?

drivers/net/mdio/mdio-bcm-unimac.c static void unimac_mdio_remove(struct platform_device *pdev) · line 450

Quoted 4 lines
drivers/net/mdio/mdio-bcm-unimac.c +1 -1
-	return unimac_mdio_clk_set(priv);
+	return ret;
  }

On the Medium: this one is valid, thanks. wait_func() implementations such as bcmgenet_mii_wait() can return 0 with MDIO_START_BUSY still set, and v3 only guarded the ADDR -> DATA transition. A C45 READ that returned early would hand back MDIO_CMD[15:0] (the register address) as read data, and the next access could switch MDIO_CFG or rewrite MDIO_CMD while the previous frame was still in flight.

v4 adds unimac_mdio_wait(), which calls wait_func() and then polls for the controller to go idle. It is used after the ADDR, READ and WRITE cycles, so the idle check no longer depends on the wait_func() implementation. I kept the fix in the unimac driver rather than changing bcmgenet_mii_wait() to return -ETIMEDOUT.

Side effect, now spelled out in the commit message: C22 accesses use the
same helper, so a C22 cycle that times out on a busy controller now returns -ETIMEDOUT instead of silently returning stale data.

On the Low: the commit message now describes the locking added to unimac_mdio_resume().

pw-bot: cr

Signature · 1 line
Florian