net: mdio: Clause 45 support for mdio-bcm-unimac
Subsystems (4)
Florian Fainelli
maintainerv5 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 v5:
- patch 1:
- the commit message now says that mdio-bcm-unimac only provides read_c45() with patch 4, which enables the UniMAC case, so that a standalone backport knows about that dependency
- the commit message now lists the callers other than the bus scan that see -ENODEV instead of -EIO (of_mdio, sfp, mxl-gpy), which is what C22 callers already get
- the vendor MMD presence check done while reading the identifiers and the reads of the per-MMD identifiers are intentionally not mapped, since they only happen once a device has answered; say so in the commit message and reword the kernel-doc of get_phy_c45_ids() and phy_get_c45_ids() to document "a negative error code" rather than -EIO as the only alternative to -ENODEV
- patches 2 and 3: no changes
- patch 4:
- check that the controller is idle after every cycle (the C45 ADDR and data cycles as well as the C22 ones) instead of only after the ADDR cycle, since a wait_func() such as bcmgenet_mii_wait() returns 0 even when it timed out with START_BUSY still set, which could return the command bits instead of the read data or switch MDIO_CFG in the middle of a frame
- the commit message says that unimac_mdio_resume() waiting for the bus lock means that resume may wait for an in-flight MDIO access
- the commit message says that the C45 scan is slower than the C22 one on the platform device GENET registers without a Device Tree node (e.g.:
ACPI) because bcmgenet_mii_wait() sleeps per frame until GENET's interrupt is registered. This is not worked around in this series; making bcmgenet_mii_wait() poll in that window would be a separate GENET change
Changes in v4:
- patch 1:
- when the read of the devices in package registers of an MMD fails with -EIO or -ENODEV, treat it like an all Fs reply and probe MMD 0 instead of returning -ENODEV right away, so that PHYs that only respond on MMD 0 (e.g.: Cortina CS4315/CS4340) are still found on controllers that report a bus error for an MMD that does not answer. The remaining MMDs are not probed, to keep the cost of scanning an empty address bounded. -ENODEV is only returned if MMD 0 does not answer either
- add a Fixes: tag
- update the kernel-doc of get_phy_c45_ids() and phy_get_c45_ids()
- the code changed since Nicolai's Reviewed-by tag was given, which was kept, please let me know if it should be dropped
- patch 2: no changes
- patch 3: reword the comments about the poll timeout, which applies to each frame, a C45 access waits once for each of its two frames
- patch 4: no code changes, the commit message now mentions that the C45 scan also runs for the platform device GENET registers without a Device Tree node (e.g.: ACPI), since it does not mask any address
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 | 162 +++++++++++++++++++++++++---- drivers/net/phy/phy_device.c | 50 +++++++-- 2 files changed, 184 insertions(+), 28 deletions(-)
Signature · 1 line
2.34.1
Florian Fainelli
maintainerPATCH v5 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. mdio-bcm-unimac does not provide read_c45() yet, this is enabled by "net: mdio: mdio-bcm-unimac: Add Clause 45 support" later in this series.
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. In the loop, an MMD whose read fails that way is handled like one that reads as all Fs, except that the remaining MMDs are not probed: the scan goes straight to MMD 0, and -ENODEV is only returned if that does not answer either, so PHYs that only respond on MMD 0 are still found while an empty address costs a bounded number of bus reads.
Errors from the vendor MMD presence check done while reading the identifiers and from the reads of the per-MMD identifiers themselves, which only happen once a device has answered, are not mapped, so a PHY that fails midway is not silently skipped. The kernel-doc therefore
documents "a negative error code" rather than -EIO as the only alternative.
This is not limited to mdiobus_scan_bus_c45(): get_phy_c45_ids() is reached through get_phy_device(bus, addr, true) and phy_get_c45_ids() by every C45 caller. For those, a failed devices in package read is now reported as -ENODEV instead of -EIO, which is what C22 callers already get:
- fwnode_mdiobus_register_phy() for "ethernet-phy-ieee802.3-c45" nodes:
__of_mdiobus_parse_phys() logs that the device is missing and registers the bus without it, instead of failing the registration.
- sfp_sm_probe_phy(): a module whose PHY does not answer ends up with "no PHY detected" after the usual retries, instead of going to SFP_S_FAIL.
- gpy_probe() through phy_get_c45_ids(): the driver core treats -ENODEV as a silent match rejection rather than a probe failure.
--- drivers/net/phy/phy_device.c | 50 ++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 10 deletions(-)
drivers/net/phy/phy_device.c +40 -10
@@ -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
@@ -810,8 +823,10 @@ static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,* the PHY identifiers for each device. Return the "devices in package" * and identifiers in @c45_ids. * -* Returns zero on success, %-EIO on bus access error, or %-ENODEV if -* the "devices in package" is invalid or no device responds. +* Returns zero on success, %-ENODEV if the "devices in package" is invalid or +* if no MMD responds (including when the bus reports -EIO or -ENODEV for the +* reads that look for a device), or a negative error code from a failed bus +* access otherwise (usually %-EIO). */ static int get_phy_c45_ids (struct mii_bus *bus,int addr,struct phy_c45_device_ids *c45_ids)
@@ -844,8 +859,20 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr,continue ; } phy_reg =get_phy_c45_devs_in_pkg (bus, addr, i, &devs_in_pkg); -if (phy_reg <0 ) -return -EIO ; +if (phy_reg <0 ) { + ret =get_phy_c45_probe_err (phy_reg); +if (ret != -ENODEV ) +return ret; + +/* The MMD did not answer: treat it like a read of all +* Fs, which is what controllers that do not check the +* turnaround return, and go straight to the MMD 0 +* probe below. Do not probe the remaining MMDs, each +* one costs two bus reads on a slow bus. +*/ + devs_in_pkg =0xffffffff ; +break ; + } }if ((devs_in_pkg &0x1fffffff ) ==0x1fffffff ) {
@@ -855,7 +882,8 @@ 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 ; +/* returning -ENODEV doesn't stop bus scanning */ +return get_phy_c45_probe_err (phy_reg);/* no device there, let's get out of here */ if ((devs_in_pkg &0x1fffffff ) ==0x1fffffff )
@@ -1077,8 +1105,10 @@ EXPORT_SYMBOL(phy_device_remove);* phy_get_c45_ids - Read 802.3-c45 IDs for phy device. * @phydev: phy_device structure to read 802.3-c45 IDs * -* Returns zero on success, %-EIO on bus access error, or %-ENODEV if -* the "devices in package" is invalid. +* Returns zero on success, %-ENODEV if the "devices in package" is invalid or +* no MMD responds (this includes a bus read failing with -EIO or -ENODEV while +* looking for a device), or a negative error code from a failed bus access +* otherwise (usually %-EIO). */ int phy_get_c45_ids (struct phy_device *phydev) {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH net-next v5 1/4] net: phy: Map C45 probe read errors to -ENODEV during bus scan
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 get_phy_c45_ids() (drivers/net/phy/phy_device.c), a devices-in-package read that fails with -EIO or -ENODEV during the MMD…
- [Low] phy_get_c45_ids() now returns -ENODEV when the devices-in-package read (and then the MMD 0 read) fails with -EIO or -ENODEV.
--
net: phy: Map C45 probe read errors to -ENODEV during bus scan
This commit maps -EIO and -ENODEV from the C45 devices in package reads in get_phy_c45_ids() to -ENODEV, the same way get_phy_c22_id() does. The goal is that mdiobus_scan_bus_c45() moves on to the next address when nothing answers, instead of failing the whole bus registration. When an MMD read fails this way inside the MMD search loop, the loop stops and only the MMD 0 fallback is tried.
Quoted 4 lines
drivers/net/phy/phy_device.c +0
-0
[ ... ]
Quoted 10 lines
drivers/net/phy/phy_device.c +4
-2@@ -844,8 +859,20 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr,continue ; } phy_reg =get_phy_c45_devs_in_pkg (bus, addr, i, &devs_in_pkg); -if (phy_reg <0 ) -return -EIO ; +if (phy_reg <0 ) { + ret =get_phy_c45_probe_err (phy_reg); +if (ret != -ENODEV ) +return ret;
drivers/net/phy/phy_device.c static int get_phy_c45_ids(struct mii_bus *bus, int addr, · line 875
Quoted 10 lines
drivers/net/phy/phy_device.c +10
-0+ +/* The MMD did not answer: treat it like a read of all +* Fs, which is what controllers that do not check the +* turnaround return, and go straight to the MMD 0 +* probe below. Do not probe the remaining MMDs, each +* one costs two bus reads on a slow bus. +*/ + devs_in_pkg =0xffffffff ; +break ; + }
[Severity: Medium]
The comment says a failed MMD is treated like a read of all Fs. With the break, doesn't it behave differently from a real all-Fs read?
On a real all-Fs read, the loop condition in get_phy_c45_ids() keeps searching the later MMDs:
for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 ||
(devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) {Commit bba238ed037c ("continue searching for C45 MMDs even if first returned ffff:ffff") added that on purpose, so that a device without a PMA/PMD MMD is still found. One example is the Lynx PCS, which only answers on MMD 3 (DEVS1=0x008a).
After the break, MMD 0 is the only probe left. If that read fails too, get_phy_c45_probe_err() returns -ENODEV. mdiobus_scan_bus_c45() or fwnode_mdiobus_register_phy() then skips the device without printing anything.
Controllers report the same missing turnaround on an unimplemented MMD in different ways. enetc_mdio_read_c45() returns all Fs:
/* return all Fs if nothing was there */
if (enetc_mdio_rd(mdio_priv, ENETC_MDIO_CFG) & MDIO_CFG_RD_ER) {
...
return 0xffff;UniMAC with read_c45 (MDIO_READ_FAIL -> -EIO, added later in this series) and mvmdio XSMI (!READ_VALID -> -ENODEV) return an error instead.
Could the same PHY or PCS be found behind ENETC but missed behind UniMAC or mvmdio?
There is a second case. MMD 1 answers (for example, it reads 0), so the loop goes on, and then MMD 2 fails the turnaround. If MMD 0 also fails, isn't a device that has already answered skipped silently? That seems to conflict with this part of the commit message:
"so a PHY that fails midway is not silently skipped"
The commit message says the remaining MMDs are not probed. Would using continue with devs_in_pkg = 0xffffffff instead of break keep the detection rules from bba238ed037c, at the cost of the extra bus reads?
drivers/net/phy/phy_device.c static int get_phy_c45_ids(struct mii_bus *bus, int addr, · line 886
Quoted 10 lines
drivers/net/phy/phy_device.c +2
-1}if ((devs_in_pkg &0x1fffffff ) ==0x1fffffff ) {@@ -855,7 +882,8 @@ 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 ; +/* returning -ENODEV doesn't stop bus scanning */ +return get_phy_c45_probe_err (phy_reg);
[Severity: Low]phy_get_c45_ids() also takes this path. Its only caller is gpy_probe() in drivers/net/phy/mxl-gpy.c, and it calls it for a PHY that has already been found over C22 and matched to the driver:
if (!phydev->is_c45) {
ret = phy_get_c45_ids(phydev);
if (ret < 0)
return ret;
}If the C45 devices in package reads on MMD 1 and MMD 0 fail with -EIO, gpy_probe() now returns -ENODEV. call_driver_probe() in drivers/base/dd.c logs that only at debug level:
case -ENODEV: case -ENXIO: dev_dbg(dev, "probe with driver %s rejects match %d\n", drv->name, ret);
Before this patch, -EIO was logged with dev_err() as "probe with driver %s failed with error %d".
The commit message mentions this change. Is -ENODEV the right code for a device that has already been identified, though? "No MMD responds" makes sense during a bus scan. Here it turns a C45 access failure on a known PHY into a silent match rejection. The binding ends up the same either way, so what changes is how easy the fault is to diagnose.
Quoted 3 lines
drivers/net/phy/phy_device.c +0
-0/* no device there, let's get out of here */ if ((devs_in_pkg &0x1fffffff ) ==0x1fffffff )
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120194318.3952487-1-florian.fainelli%40broadcom.com
Florian Fainelli
maintainerPATCH v5 2/4 net: mdio: mdio-bcm-unimac: Define MDIO_OP_SHIFT
In reply to Florian FainelliThe 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
@@ -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
Florian Fainelli
maintainerPATCH v5 3/4 net: mdio: mdio-bcm-unimac: Poll for completion at 1us granularity
In reply to Florian FainelliThe 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 | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
drivers/net/mdio/mdio-bcm-unimac.c +16 -8
@@ -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. The timeout +* applies to the wait for one frame, 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,15 @@ 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. +/* A frame should take ~25 usec. A C22 transaction is a single frame, +* a C45 transaction is an address frame followed by a data frame and +* each of them is waited for separately. */ -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
[email protected]
AI reviewRe: [PATCH net-next v5 3/4] net: mdio: mdio-bcm-unimac: Poll for completion at 1us granularity
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] Concern: the commit message and the new comment in unimac_mdio_poll() (drivers/net/mdio/mdio-bcm-unimac.c) describe…
--
net: mdio: mdio-bcm-unimac: Poll for completion at 1us granularity
This commit drops the fixed 30us udelay() and the 2ms sleeping poll in unimac_mdio_poll(). It now busy-polls MDIO_START_BUSY every microsecond with read_poll_timeout_atomic(), and the timeout goes from 100ms down to 1ms. The goal is to cut per-access latency before Clause 45 support is added.
Quoted 4 lines
drivers/net/mdio/mdio-bcm-unimac.c +0
-0
[ ... ]
drivers/net/mdio/mdio-bcm-unimac.c static int unimac_mdio_poll(void *wait_func_data) · line 92
Quoted 11 lines
drivers/net/mdio/mdio-bcm-unimac.c +3
-3@@ -79,14 +86,15 @@ 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. +/* A frame should take ~25 usec. A C22 transaction is a single frame, +* a C45 transaction is an address frame followed by a data frame and +* each of them is waited for separately. */
[Severity: Low]
This isn't a bug, but the new comment in unimac_mdio_poll() describes C45 address and data frames that are waited for separately. At this commit there is no C45 path yet. unimac_mdio_probe() only sets bus->read and bus->write, and only MDIO_RD_C22/MDIO_WR_C22 are defined.
The next patch in the series, "net: mdio: mdio-bcm-unimac: Add Clause 45 support", adds unimac_mdio_addr_c45() and unimac_mdio_do_read()/do_write(). Each of these calls unimac_mdio_wait(), so the comment is accurate once the series is applied. Would it be clearer to add this comment in that patch, or to word it so it makes sense for this commit on its own?
The 100ms to 1ms timeout reduction for existing C22 users is stated in the commit message. The numbers hold up: the largest divider allowed by MDIO_CLK_DIV_MASK is 128, which gives about 1.95MHz from a 250MHz reference clock and about 32.8us per 64 bit frame. That is well under MDIO_POLL_TIMEOUT_US.
Quoted 10 lines
drivers/net/mdio/mdio-bcm-unimac.c +5
-4-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 ); }
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120194318.3952487-1-florian.fainelli%40broadcom.com
Florian Fainelli
maintainerPATCH v5 4/4 net: mdio: mdio-bcm-unimac: Add Clause 45 support
In reply to Florian FainelliAdd 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.
Since MDIO_CFG also holds the clock divider, unimac_mdio_resume() now takes the bus lock around unimac_mdio_clk_set() so that it cannot race with the read-modify-write done by the accessors. Resume may therefore wait for an MDIO access that is in flight.
Some wait_func implementations (e.g.: bcmgenet_mii_wait()) return 0 even when the controller is still busy. For C45 this would let the data cycle or the next access's MDIO_CFG switch start while a frame is in flight, and return the command bits instead of the read data. Check that the controller is idle after every cycle, C22 ones included, and report a timeout instead.
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. This is not the case when the bus is registered from a Device Tree node, which masks all addresses.
The platform device that GENET instantiates when it is probed without a Device Tree node (e.g.: ACPI) does not mask any address, so it is scanned too. Its wait_func is bcmgenet_mii_wait(), which sleeps for up to a timeout per frame since GENET's interrupt is not registered yet when the bus is registered. That makes the scan slower at probe time on such systems, the same way the C22 scan already is.
--- drivers/net/mdio/mdio-bcm-unimac.c | 136 ++++++++++++++++++++++++++--- 1 file changed, 126 insertions(+), 10 deletions(-)
drivers/net/mdio/mdio-bcm-unimac.c +126 -10
@@ -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)
@@ -97,7 +103,60 @@ 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) +/* Wait for a cycle to complete. Some wait_func implementations (e.g.: +* bcmgenet_mii_wait()) return 0 even when they timed out with the controller +* still busy, in which case MDIO_CMD[15:0] holds the command rather than the +* data, and MDIO_CFG or MDIO_CMD must not be touched before the frame is +* over. Make sure the controller is idle after every cycle. +*/ +static int unimac_mdio_wait (struct unimac_mdio_priv *priv) +{ +int ret; + + ret = priv->wait_func (priv->wait_func_data); +if (ret) +return ret; + +return unimac_mdio_poll (priv); +} + +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; + +/* 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); + +return unimac_mdio_wait (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;
@@ -108,14 +167,25 @@ 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 */ unimac_mdio_start (priv); - ret = priv->wait_func (priv->wait_func_data); + ret =unimac_mdio_wait (priv);if (ret) goto out;
@@ -137,8 +207,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;
@@ -149,18 +219,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); + ret =unimac_mdio_wait (priv); +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
@@ -191,6 +293,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; } }
@@ -302,6 +408,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);
@@ -337,8 +445,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; -return unimac_mdio_clk_set (priv); +/* 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 ret; } static SIMPLE_DEV_PM_OPS(unimac_mdio_pm_ops,
Signature · 1 line
2.34.1
Re: [PATCH net-next v5 4/4] net: mdio: mdio-bcm-unimac: Add Clause 45 support
Quoted 273 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_CFGbetween C22 and C45 mode as needed;MDIO_CFGis only written when the mode actually changes.Since
MDIO_CFGalso holds the clock divider,unimac_mdio_resume()now takes the bus lock aroundunimac_mdio_clk_set()so that it cannot race with the read-modify-write done by the accessors. Resume may therefore wait for an MDIO access that is in flight.Some
wait_funcimplementations (e.g.:bcmgenet_mii_wait()) return 0 even when the controller is still busy. For C45 this would let the data cycle or the next access'sMDIO_CFGswitch start while a frame is in flight, and return the command bits instead of the read data. Check that the controller is idle after every cycle, C22 ones included, and report a timeout instead.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. This is not the case when the bus is registered from a Device Tree node, which masks all addresses.
The platform device that GENET instantiates when it is probed without a Device Tree node (e.g.: ACPI) does not mask any address, so it is scanned too. Its
wait_funcisbcmgenet_mii_wait(), which sleeps for up to a timeout per frame since GENET's interrupt is not registered yet when the bus is registered. That makes the scan slower at probe time on such systems, the same way the C22 scan already is.Signed-off-by Florian Fainelli <[email protected]>Co-developed-by Carlos Soto <[email protected]>Signed-off-by Carlos Soto <[email protected]>Co-developed-by Justin Chen <[email protected]>Signed-off-by Justin Chen <[email protected]>--- drivers/net/mdio/mdio-bcm-unimac.c | 136 ++++++++++++++++++++++++++--- 1 file changed, 126 insertions(+), 10 deletions(-)drivers/net/mdio/mdio-bcm-unimac.c +8
-1@@ -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)@@ -97,7 +103,60 @@ 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) +/* Wait for a cycle to complete. Some wait_func implementations (e.g.: +* bcmgenet_mii_wait()) return 0 even when they timed out with the controller
+ * still busy, in which case MDIO_CMD[15:0] holds the command rather than the
+ * data, andMDIO_CFGorMDIO_CMDmust not be touched before the frame is
+ * over. Make sure the controller is idle after every cycle.
+ */drivers/net/mdio/mdio-bcm-unimac.c +33
-0+static int unimac_mdio_wait (struct unimac_mdio_priv *priv) +{ +int ret; + + ret = priv->wait_func (priv->wait_func_data); +if (ret) +return ret; + +return unimac_mdio_poll (priv); +} + +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; + +/* 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); + +return unimac_mdio_wait (priv); +} + +static void unimac_mdio_set_clause(struct unimac_mdio_priv *priv, bool
is_c45)drivers/net/mdio/mdio-bcm-unimac.c +14
-0+{ +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, intdevad,
drivers/net/mdio/mdio-bcm-unimac.c +17
-5+ int reg, bool is_c45) {struct unimac_mdio_priv *priv = bus->priv;int ret;@@ -108,14 +167,25 @@ 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 */ unimac_mdio_start (priv); - ret = priv->wait_func (priv->wait_func_data); + ret =unimac_mdio_wait (priv);if (ret) goto out;@@ -137,8 +207,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) {drivers/net/mdio/mdio-bcm-unimac.c +30
-3struct unimac_mdio_priv *priv = bus->priv;u32 cmd;@@ -149,18 +219,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); + ret =unimac_mdio_wait (priv); +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, intdev,
drivers/net/mdio/mdio-bcm-unimac.c +5
-0+ 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
dummydrivers/net/mdio/mdio-bcm-unimac.c +15
-1@@ -191,6 +293,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; } }@@ -302,6 +408,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);@@ -337,8 +445,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; -return unimac_mdio_clk_set (priv); +/* 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 ret; }
Quoted 1 line
static SIMPLE_DEV_PM_OPS(unimac_mdio_pm_ops,
Nicolai Buchwitz
maintainerRe: [PATCH net-next v5 4/4] net: mdio: mdio-bcm-unimac: Add Clause 45 support
In reply to Florian FainelliQuoted 33 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_CFGbetween C22 and C45 mode as needed;MDIO_CFGis only written when the mode actually changes.Since
MDIO_CFGalso holds the clock divider,unimac_mdio_resume()now takes the bus lock aroundunimac_mdio_clk_set()so that it cannot race with the read-modify-write done by the accessors. Resume may therefore wait for an MDIO access that is in flight.Some
wait_funcimplementations (e.g.:bcmgenet_mii_wait()) return 0 even when the controller is still busy. For C45 this would let the data cycle or the next access'sMDIO_CFGswitch start while a frame is in flight, and return the command bits instead of the read data. Check that the controller is idle after every cycle, C22 ones included, and report a timeout instead.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. This is not the case when the bus is registered from a Device Tree node, which masks all addresses.
The platform device that GENET instantiates when it is probed without a Device Tree node (e.g.: ACPI) does not mask any address, so it is scanned too. Its
wait_funcisbcmgenet_mii_wait(), which sleeps for up to a timeout per frame since GENET's interrupt is not registered yet when the bus is registered. That makes the scan slower at probe time on such systems, the same way the C22 scan already is.
Quoted 5 lines
Signed-off-by Florian Fainelli <[email protected]>Co-developed-by Carlos Soto <[email protected]>Signed-off-by Carlos Soto <[email protected]>Co-developed-by Justin Chen <[email protected]>Signed-off-by Justin Chen <[email protected]>
Forgot to mention: Your tag needs to go last (v3 had it right). You can keep my R-b as the changes are OK.
Thanks,
Nicolai
Florian Fainelli
maintainerRe: [PATCH net-next v5 4/4] net: mdio: mdio-bcm-unimac: Add Clause 45 support
Quoted 34 lines · Nicolai Buchwitz
Quoted 33 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_CFGbetween C22 and C45 mode as needed;MDIO_CFGis only written when the mode actually changes.Since
MDIO_CFGalso holds the clock divider,unimac_mdio_resume()now takes the bus lock aroundunimac_mdio_clk_set()so that it cannot race with the read-modify-write done by the accessors. Resume may therefore wait for an MDIO access that is in flight.Some
wait_funcimplementations (e.g.:bcmgenet_mii_wait()) return 0 even when the controller is still busy. For C45 this would let the data cycle or the next access'sMDIO_CFGswitch start while a frame is in flight, and return the command bits instead of the read data. Check that the controller is idle after every cycle, C22 ones included, and report a timeout instead.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. This is not the case when the bus is registered from a Device Tree node, which masks all addresses.
The platform device that GENET instantiates when it is probed without a Device Tree node (e.g.: ACPI) does not mask any address, so it is scanned too. Its
wait_funcisbcmgenet_mii_wait(), which sleeps for up to a timeout per frame since GENET's interrupt is not registered yet when the bus is registered. That makes the scan slower at probe time on such systems, the same way the C22 scan already is.
Quoted 8 lines
Quoted 5 lines
Signed-off-by Florian Fainelli <[email protected]>Co-developed-by Carlos Soto <[email protected]>Signed-off-by Carlos Soto <[email protected]>Co-developed-by Justin Chen <[email protected]>Signed-off-by Justin Chen <[email protected]>Forgot to mention: Your tag needs to go last (v3 had it right). You can keep my R-b as the changes are OK.
Indeed I did, thanks!
pw-bot: cr
Signature · 1 line
Florian
[email protected]
AI reviewRe: [PATCH net-next v5 4/4] net: mdio: mdio-bcm-unimac: Add Clause 45 support
In reply to Florian FainelliThank 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
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] The patch sets bus->read_c45/write_c45 for every UniMAC bus, so the non-DT (ACPI) GENET bus, which has no addresses masked, now…
- [Low] The patch fixes a real Clause 22 bug but folds it into a C45 feature commit, with no Fixes: tag and no separate patch.
- [Low] The commit message says the new C45 scan of the unmasked, non-DT (ACPI) GENET bus makes probe slower 'the same way the C22 scan already…
- [Low] unimac_mdio_wait() only guarantees the controller is idle when it succeeds.
--
net: mdio: mdio-bcm-unimac: Add Clause 45 support
This commit adds Clause 45 read/write callbacks to the UniMAC MDIO driver. They switch MDIO_CFG between C22 and C45 mode as needed, and resume now takes the bus lock around the clock divider update. It also polls for the controller to go idle after every MDIO cycle, and skips the BMSR workaround read for children compatible with ethernet-phy-ieee802.3-c45.
Quoted 6 lines
The platform device that GENET instantiates when it is probed without a Device Tree node (e.g.: ACPI) does not mask any address, so it is scanned too. Its
wait_funcisbcmgenet_mii_wait(), which sleeps for up to a timeout per frame since GENET's interrupt is not registered yet when the bus is registered. That makes the scan slower at probe time on such systems, the same way the C22 scan already is.
[Severity: Low]
Does "the same way the C22 scan already is" understate the added cost?
For each empty address, the C22 scan costs about 2 frames: the BMSR workaround read in unimac_mdio_reset() (read_mask = ~bus->phy_mask) and the PHYSID1 read.
The C45 scan goes through get_phy_c45_ids(), as changed by "net: phy: Map C45 probe read errors to -ENODEV during bus scan". It costs 4 frames per empty address: the MMD1 DEVS2 read (ADDR + READ, which fails with -EIO mapped to -ENODEV), then the MMD0 DEVS2 fallback (ADDR + READ).
Each frame waits for the full HZ / 100 timeout in bcmgenet_mii_wait(), because the IRQ is not requested until bcmgenet_open(). With one C22 PHY, the 31 empty addresses add about 31 x 4 x 10ms, roughly 1.2s. That comes on top of the ~0.6s the C22 scan already costs.
On the known ACPI GENET platform (Raspberry Pi 4 with a C22 BCM54213PE), the C45 scan cannot find anything.
Could the commit message give a more accurate size for this delay?
Quoted 4 lines
drivers/net/mdio/mdio-bcm-unimac.c +0
-0
[ ... ]
Quoted 11 lines
drivers/net/mdio/mdio-bcm-unimac.c +6
-1@@ -97,7 +103,60 @@ 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) +/* Wait for a cycle to complete. Some wait_func implementations (e.g.: +* bcmgenet_mii_wait()) return 0 even when they timed out with the controller +* still busy, in which case MDIO_CMD[15:0] holds the command rather than the +* data, and MDIO_CFG or MDIO_CMD must not be touched before the frame is +* over. Make sure the controller is idle after every cycle. +*/
drivers/net/mdio/mdio-bcm-unimac.c static int unimac_mdio_poll(void *wait_func_data) · line 121
Quoted 10 lines
drivers/net/mdio/mdio-bcm-unimac.c +10
-0+static int unimac_mdio_wait (struct unimac_mdio_priv *priv) +{ +int ret; + + ret = priv->wait_func (priv->wait_func_data); +if (ret) +return ret; + +return unimac_mdio_poll (priv); +}
[Severity: Low]
This also fixes an existing C22 problem. Would it be better as a separate patch with a Fixes: tag, so stable trees pick it up?
bcmgenet_mii_wait() always returns 0:
wait_event_timeout(priv->wq, !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD) & MDIO_START_BUSY), HZ / 100); return 0;
GENET registers the UniMAC bus at probe via bcmgenet_mii_init(), but only requests its IRQs in bcmgenet_open(). Probe-time C22 frames therefore rely on the timeout expiring. These are the PHYSID1/PHYSID2 scan reads and the BMSR workaround read in unimac_mdio_reset().
With CONFIG_HZ=100, HZ / 100 is a single jiffy. That can expire at the next tick, before the ~25us frame has finished.
Before this patch, unimac_mdio_read() would then read MDIO_CMD with MDIO_START_BUSY still set. It returned cmd & 0xffff (the command bits) as PHY data, and the next command could start while the frame was still in flight.
The commit message does say "C22 ones included", but presents the change only as groundwork for C45.
[ ... ]
drivers/net/mdio/mdio-bcm-unimac.c static int unimac_mdio_read(struct mii_bus *bus, int phy_id, int reg) · line 174
Quoted 11 lines
drivers/net/mdio/mdio-bcm-unimac.c +5
-2@@ -108,14 +167,25 @@ 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;
[Severity: Low]
The comment above unimac_mdio_wait() says "MDIO_CFG or MDIO_CMD must not be touched before the frame is over". Is that only enforced on the success path?
If wait_func or the following unimac_mdio_poll() times out, unimac_mdio_do_read() and unimac_mdio_do_write() jump to out with MDIO_START_BUSY possibly still set. For C45 this can happen on the address frame inside unimac_mdio_addr_c45(). clk_disable_unprepare() then gates the clock while the frame may still be in flight.
The next access starts with unimac_mdio_set_clause(), a read-modify-write of MDIO_CFG that can flip MDIO_C22. It then rewrites MDIO_CMD and sets MDIO_START_BUSY. Nothing checks that the previous frame has finished.
unimac_mdio_resume()->unimac_mdio_clk_set() also rewrites the MDIO_CFG divider without checking for idle.
After a real hardware timeout, could this corrupt the in-flight frame or the next MDIO frame? It needs a frame to stay busy past the wait_func timeout plus the 1ms MDIO_POLL_TIMEOUT_US, so it takes a stalled controller.
[ ... ]
drivers/net/mdio/mdio-bcm-unimac.c static int unimac_mdio_probe(struct platform_device *pdev) · line 412
Quoted 6 lines
drivers/net/mdio/mdio-bcm-unimac.c +2
-0@@ -302,6 +408,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;
[Severity: Medium]
Can this change which PHY GENET attaches to on the non-DT (ACPI) path?
These callbacks are now set unconditionally. bcmgenet_mii_register() leaves bus->phy_mask at 0 when there is no DT node:
else ppd.phy_mask = ~0;
So __mdiobus_register() also runs the C45 scan over the whole bus. That scan can add phy_devices at addresses that have no C22 device.
GENET's ACPI path then picks its PHY in bcmgenet_mii_probe():
phydev = phy_find_first(unimacbus); ... ret = phy_connect_direct(dev, phydev, bcmgenet_mii_setup, phy_iface);
Something may answer C45 frames at an address below the intended C22 PHY. It could be a C45-only PHY, or a C22 PHY that mishandles C45 frames (the comment in mdio_bus_provider.c mentions this case). Would phy_find_first() then return that device, so GENET attaches to the wrong PHY?
mdiobus_prevent_c45_scan() only filters out the Micrel OUI. The commit message only mentions probe time for this bus, not the change in which PHY gets selected.
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120194318.3952487-1-florian.fainelli%40broadcom.com