net: phy: factor out legacy PHY fixup support and make it always built-in

PHY fixup registration is used from platform code in init phase only.
Let's move the PHY fixup code from the modular part of phylib to the
always built-in part of phylib. This allows to annotate the fixup
registration as __init. No caller uses the return code of PHY fixup
registration, therefore change related functions to return void.

phy_needs_fixup() and phy_scan_fixups() wouldn't have to be moved to
the built-in part of phylib. But doing so allows to fully factor out
legacy fixup support into its own source code file, and make struct
phy_fixup and phy_fixup_list strictly private to phy_fixup.c.

phy_scan_fixups() is used after init phase only, then phy_fixup_list
is read-only. So we don't need the mutex when accessing the list.
Also when registering PHY fixups the mutex isn't needed, because
fixup registration is done sequentially from platform init code.
Actually there is only one platform with more than one fixup.

In addition this change lays the foundation for enabling modular
phylib on platforms where the fixup just sets a flag.

Last but not least reflect the change in the documentation,
and add a hint that fixup's are legacy functionality.

Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
Link: https://patch.msgid.link/2d722b84-a263-400b-ac91-bf37125eb85b@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Heiner Kallweit 2026-09-16 08:05:22 +02:00 committed by Jakub Kicinski
parent c2437bf72a
commit f619d5003f
6 changed files with 114 additions and 102 deletions

View File

@ -511,24 +511,27 @@ Board Fixups
Sometimes the specific interaction between the platform and the PHY requires
special handling. For instance, to change where the PHY's clock input is,
or to add a delay to account for latency issues in the data path. In order
to support such contingencies, the PHY Layer allows platform code to register
fixups to be run when the PHY is brought up (or subsequently reset).
to support such contingencies, the PHY Layer allows platform init code to
register fixups to be run when the PHY is brought up (or subsequently reset).
Note: This is legacy functionality and not supposed to be used in new code.
Fixup's should be handled in the PHY driver.
When the PHY Layer brings up a PHY it checks to see if there are any fixups
registered for it, matching based on UID (contained in the PHY device's phy_id
field) and the bus identifier (contained in phydev->dev.bus_id). Both must
match, however two constants, PHY_ANY_ID and PHY_ANY_UID, are provided as
wildcards for the bus ID and UID, respectively.
match, however NULL for the bus identifier and 0 for phy_uid_mask can be
used to disable the respective type of matching.
When a match is found, the PHY layer will invoke the run function associated
with the fixup. This function is passed a pointer to the phy_device of
interest. It should therefore only operate on that PHY.
interest. It should therefore only operate on that PHY.
The platform code can register the fixup using one of::
int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
void __init phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
int (*run)(struct phy_device *));
int phy_register_fixup_for_id(const char *phy_id,
void __init phy_register_fixup_for_id(const char *phy_id,
int (*run)(struct phy_device *));
Standards

View File

@ -8,7 +8,7 @@ libphy-y := phy.o phy-c45.o phy-core.o phy_device.o \
ifdef CONFIG_PHYLIB
# built-in whenever PHYLIB is built-in or module
obj-y += stubs.o
obj-y += stubs.o phy_fixup.o
endif
libphy-$(CONFIG_SWPHY) += swphy.o

View File

@ -49,14 +49,6 @@ MODULE_DESCRIPTION("PHY library");
MODULE_AUTHOR("Andy Fleming");
MODULE_LICENSE("GPL");
struct phy_fixup {
struct list_head list;
char bus_id[MII_BUS_ID_SIZE + 3];
u32 phy_uid;
u32 phy_uid_mask;
int (*run)(struct phy_device *phydev);
};
static struct phy_driver genphy_c45_driver = {
.phy_id = 0xffffffff,
.phy_id_mask = 0xffffffff,
@ -237,9 +229,6 @@ static void phy_mdio_device_remove(struct mdio_device *mdiodev)
static struct phy_driver genphy_driver;
static LIST_HEAD(phy_fixup_list);
static DEFINE_MUTEX(phy_fixup_lock);
static bool phy_drv_wol_enabled(struct phy_device *phydev)
{
struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
@ -427,85 +416,6 @@ static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,
mdio_bus_phy_resume);
/**
* phy_register_fixup - creates a new phy_fixup and adds it to the list
* @bus_id: A string which matches phydev->mdio.dev.bus_id (or NULL)
* @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
* @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
* comparison (or 0 to disable id-based matching)
* @run: The actual code to be run when a matching PHY is found
*/
static int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
int (*run)(struct phy_device *))
{
struct phy_fixup *fixup = kzalloc_obj(*fixup);
if (!fixup)
return -ENOMEM;
if (bus_id)
strscpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
fixup->phy_uid = phy_uid;
fixup->phy_uid_mask = phy_uid_mask;
fixup->run = run;
mutex_lock(&phy_fixup_lock);
list_add_tail(&fixup->list, &phy_fixup_list);
mutex_unlock(&phy_fixup_lock);
return 0;
}
/* Registers a fixup to be run on any PHY with the UID in phy_uid */
int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
int (*run)(struct phy_device *))
{
return phy_register_fixup(NULL, phy_uid, phy_uid_mask, run);
}
EXPORT_SYMBOL(phy_register_fixup_for_uid);
/* Registers a fixup to be run on the PHY with id string bus_id */
int phy_register_fixup_for_id(const char *bus_id,
int (*run)(struct phy_device *))
{
return phy_register_fixup(bus_id, 0, 0, run);
}
EXPORT_SYMBOL(phy_register_fixup_for_id);
static bool phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
{
if (!strcmp(fixup->bus_id, phydev_name(phydev)))
return true;
if (fixup->phy_uid_mask &&
phy_id_compare(phydev->phy_id, fixup->phy_uid, fixup->phy_uid_mask))
return true;
return false;
}
/* Runs any matching fixups for this phydev */
static int phy_scan_fixups(struct phy_device *phydev)
{
struct phy_fixup *fixup;
mutex_lock(&phy_fixup_lock);
list_for_each_entry(fixup, &phy_fixup_list, list) {
if (phy_needs_fixup(phydev, fixup)) {
int err = fixup->run(phydev);
if (err < 0) {
mutex_unlock(&phy_fixup_lock);
return err;
}
phydev->has_fixups = true;
}
}
mutex_unlock(&phy_fixup_lock);
return 0;
}
/**
* genphy_match_phy_device - match a PHY device with a PHY driver
* @phydev: target phy_device struct

View File

@ -0,0 +1,98 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* PHY fixup support
*/
#include <linux/list.h>
#include <linux/phy.h>
#include <linux/slab.h>
#include <linux/string.h>
#include "phylib-internal.h"
static struct list_head phy_fixup_list __ro_after_init =
LIST_HEAD_INIT(phy_fixup_list);
struct phy_fixup {
struct list_head list;
char bus_id[MII_BUS_ID_SIZE + 3];
u32 phy_uid;
u32 phy_uid_mask;
int (*run)(struct phy_device *phydev);
};
/**
* phy_register_fixup - creates a new phy_fixup and adds it to the list
* @bus_id: A string which matches phydev->mdio.dev.bus_id (or NULL)
* @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
* @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
* comparison (or 0 to disable id-based matching)
* @run: The actual code to be run when a matching PHY is found
*/
static void __init phy_register_fixup(const char *bus_id, u32 phy_uid,
u32 phy_uid_mask,
int (*run)(struct phy_device *))
{
struct phy_fixup *fixup = kzalloc_obj(*fixup);
if (!fixup)
return;
if (bus_id)
strscpy(fixup->bus_id, bus_id);
fixup->phy_uid = phy_uid;
fixup->phy_uid_mask = phy_uid_mask;
fixup->run = run;
list_add_tail(&fixup->list, &phy_fixup_list);
}
/* Registers a fixup to be run on any PHY with the UID in phy_uid */
void __init phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
int (*run)(struct phy_device *))
{
phy_register_fixup(NULL, phy_uid, phy_uid_mask, run);
}
/* Registers a fixup to be run on the PHY with id string bus_id */
void __init phy_register_fixup_for_id(const char *bus_id,
int (*run)(struct phy_device *))
{
phy_register_fixup(bus_id, 0, 0, run);
}
static bool phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
{
if (!strcmp(fixup->bus_id, phydev_name(phydev)))
return true;
if (fixup->phy_uid_mask &&
phy_id_compare(phydev->phy_id, fixup->phy_uid, fixup->phy_uid_mask))
return true;
return false;
}
/**
* phy_scan_fixups - runs any matching fixups for this phydev
* @phydev: the phydev to search and run fixups for
* Returns: 0 or an errno
*/
int phy_scan_fixups(struct phy_device *phydev)
{
struct phy_fixup *fixup;
list_for_each_entry(fixup, &phy_fixup_list, list) {
if (phy_needs_fixup(phydev, fixup)) {
int err = fixup->run(phydev);
if (err < 0)
return err;
phydev->has_fixups = true;
}
}
return 0;
}
EXPORT_SYMBOL_GPL(phy_scan_fixups);

View File

@ -23,6 +23,7 @@ void of_set_phy_eee_broken(struct phy_device *phydev);
void of_set_phy_timing_role(struct phy_device *phydev);
int phy_speed_down_core(struct phy_device *phydev);
void phy_check_downshift(struct phy_device *phydev);
int phy_scan_fixups(struct phy_device *phydev);
int mdiobus_register_device(struct mdio_device *mdiodev);
int mdiobus_unregister_device(struct mdio_device *mdiodev);

View File

@ -2440,10 +2440,10 @@ int phy_get_mac_termination(struct phy_device *phydev, struct device *dev,
void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv,
bool *tx_pause, bool *rx_pause);
int phy_register_fixup_for_id(const char *bus_id,
int (*run)(struct phy_device *));
int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
int (*run)(struct phy_device *));
void __init phy_register_fixup_for_id(const char *bus_id,
int (*run)(struct phy_device *));
void __init phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
int (*run)(struct phy_device *));
int phy_eee_tx_clock_stop_capable(struct phy_device *phydev);
int phy_eee_rx_clock_stop(struct phy_device *phydev, bool clk_stop_enable);