From 511cb45260228e19c4659d67251267d5dadceaac Mon Sep 17 00:00:00 2001 From: Heiner Kallweit Date: Sun, 11 Jan 2026 13:41:39 +0100 Subject: [PATCH 1/2] net: phy: fixed_phy: replace list of fixed PHYs with static array Due to max 32 PHY addresses being available per mii bus, using a list can't support more fixed PHY's. And there's no known use case for as much as 32 fixed PHY's on a system. 8 should be plenty of fixed PHY's, so use an array of that size instead of a list. This allows to significantly reduce the code size and complexity. Signed-off-by: Heiner Kallweit Tested-by: Maxime Chevallier Reviewed-by: Maxime Chevallier Link: https://patch.msgid.link/8610d30c-eac7-4100-9008-d3b6cee6a5cd@gmail.com Signed-off-by: Jakub Kicinski --- drivers/net/phy/fixed_phy.c | 69 +++++++++---------------------------- 1 file changed, 17 insertions(+), 52 deletions(-) diff --git a/drivers/net/phy/fixed_phy.c b/drivers/net/phy/fixed_phy.c index 50684271f81a..7d6078d15709 100644 --- a/drivers/net/phy/fixed_phy.c +++ b/drivers/net/phy/fixed_phy.c @@ -10,7 +10,6 @@ #include #include -#include #include #include #include @@ -22,27 +21,24 @@ #include "swphy.h" +/* The DSA loop driver may allocate 4 fixed PHY's, and 4 additional + * fixed PHY's for a system should be sufficient. + */ +#define NUM_FP 8 + struct fixed_phy { - int addr; struct phy_device *phydev; struct fixed_phy_status status; int (*link_update)(struct net_device *, struct fixed_phy_status *); - struct list_head node; }; +static struct fixed_phy fmb_fixed_phys[NUM_FP]; static struct mii_bus *fmb_mii_bus; -static LIST_HEAD(fmb_phys); +static DEFINE_IDA(phy_fixed_ida); static struct fixed_phy *fixed_phy_find(int addr) { - struct fixed_phy *fp; - - list_for_each_entry(fp, &fmb_phys, node) { - if (fp->addr == addr) - return fp; - } - - return NULL; + return ida_exists(&phy_fixed_ida, addr) ? fmb_fixed_phys + addr : NULL; } int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier) @@ -108,31 +104,6 @@ int fixed_phy_set_link_update(struct phy_device *phydev, } EXPORT_SYMBOL_GPL(fixed_phy_set_link_update); -static int __fixed_phy_add(int phy_addr, - const struct fixed_phy_status *status) -{ - struct fixed_phy *fp; - int ret; - - ret = swphy_validate_state(status); - if (ret < 0) - return ret; - - fp = kzalloc(sizeof(*fp), GFP_KERNEL); - if (!fp) - return -ENOMEM; - - fp->addr = phy_addr; - fp->status = *status; - fp->status.link = true; - - list_add_tail(&fp->node, &fmb_phys); - - return 0; -} - -static DEFINE_IDA(phy_fixed_ida); - static void fixed_phy_del(int phy_addr) { struct fixed_phy *fp; @@ -141,8 +112,7 @@ static void fixed_phy_del(int phy_addr) if (!fp) return; - list_del(&fp->node); - kfree(fp); + memset(fp, 0, sizeof(*fp)); ida_free(&phy_fixed_ida, phy_addr); } @@ -153,19 +123,20 @@ struct phy_device *fixed_phy_register(const struct fixed_phy_status *status, int phy_addr; int ret; + ret = swphy_validate_state(status); + if (ret < 0) + return ERR_PTR(ret); + if (!fmb_mii_bus || fmb_mii_bus->state != MDIOBUS_REGISTERED) return ERR_PTR(-EPROBE_DEFER); - /* Get the next available PHY address, up to PHY_MAX_ADDR */ - phy_addr = ida_alloc_max(&phy_fixed_ida, PHY_MAX_ADDR - 1, GFP_KERNEL); + /* Get the next available PHY address, up to NUM_FP */ + phy_addr = ida_alloc_max(&phy_fixed_ida, NUM_FP - 1, GFP_KERNEL); if (phy_addr < 0) return ERR_PTR(phy_addr); - ret = __fixed_phy_add(phy_addr, status); - if (ret < 0) { - ida_free(&phy_fixed_ida, phy_addr); - return ERR_PTR(ret); - } + fmb_fixed_phys[phy_addr].status = *status; + fmb_fixed_phys[phy_addr].status.link = true; phy = get_phy_device(fmb_mii_bus, phy_addr, false); if (IS_ERR(phy)) { @@ -237,15 +208,9 @@ module_init(fixed_mdio_bus_init); static void __exit fixed_mdio_bus_exit(void) { - struct fixed_phy *fp, *tmp; - mdiobus_unregister(fmb_mii_bus); mdiobus_free(fmb_mii_bus); - list_for_each_entry_safe(fp, tmp, &fmb_phys, node) { - list_del(&fp->node); - kfree(fp); - } ida_destroy(&phy_fixed_ida); } module_exit(fixed_mdio_bus_exit); From ca8934f80c4f386a08a4e6132a8943a8b0380ec7 Mon Sep 17 00:00:00 2001 From: Heiner Kallweit Date: Sun, 11 Jan 2026 13:43:22 +0100 Subject: [PATCH 2/2] net: phy: fixed_phy: replace IDA with a bitmap Size of array fmb_fixed_phys is small, so we can use a simple bitmap instead of an IDA to manage dynamic allocation of fixed PHY's. find_first_zero_bit() isn't atomic, so we need the loop to rule out double allocation of a PHY address. Suggested-by: Jakub Kicinski Signed-off-by: Heiner Kallweit Tested-by: Maxime Chevallier Reviewed-by: Maxime Chevallier Link: https://patch.msgid.link/d4614463-d532-41fc-92e9-ef97107aceb5@gmail.com Signed-off-by: Jakub Kicinski --- drivers/net/phy/fixed_phy.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/drivers/net/phy/fixed_phy.c b/drivers/net/phy/fixed_phy.c index 7d6078d15709..0b83fb30a548 100644 --- a/drivers/net/phy/fixed_phy.c +++ b/drivers/net/phy/fixed_phy.c @@ -16,7 +16,6 @@ #include #include #include -#include #include #include "swphy.h" @@ -32,13 +31,13 @@ struct fixed_phy { int (*link_update)(struct net_device *, struct fixed_phy_status *); }; +static DECLARE_BITMAP(fixed_phy_ids, NUM_FP); static struct fixed_phy fmb_fixed_phys[NUM_FP]; static struct mii_bus *fmb_mii_bus; -static DEFINE_IDA(phy_fixed_ida); static struct fixed_phy *fixed_phy_find(int addr) { - return ida_exists(&phy_fixed_ida, addr) ? fmb_fixed_phys + addr : NULL; + return test_bit(addr, fixed_phy_ids) ? fmb_fixed_phys + addr : NULL; } int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier) @@ -113,7 +112,20 @@ static void fixed_phy_del(int phy_addr) return; memset(fp, 0, sizeof(*fp)); - ida_free(&phy_fixed_ida, phy_addr); + clear_bit(phy_addr, fixed_phy_ids); +} + +static int fixed_phy_get_free_addr(void) +{ + int addr; + + do { + addr = find_first_zero_bit(fixed_phy_ids, NUM_FP); + if (addr == NUM_FP) + return -ENOSPC; + } while (test_and_set_bit(addr, fixed_phy_ids)); + + return addr; } struct phy_device *fixed_phy_register(const struct fixed_phy_status *status, @@ -131,7 +143,7 @@ struct phy_device *fixed_phy_register(const struct fixed_phy_status *status, return ERR_PTR(-EPROBE_DEFER); /* Get the next available PHY address, up to NUM_FP */ - phy_addr = ida_alloc_max(&phy_fixed_ida, NUM_FP - 1, GFP_KERNEL); + phy_addr = fixed_phy_get_free_addr(); if (phy_addr < 0) return ERR_PTR(phy_addr); @@ -210,8 +222,6 @@ static void __exit fixed_mdio_bus_exit(void) { mdiobus_unregister(fmb_mii_bus); mdiobus_free(fmb_mii_bus); - - ida_destroy(&phy_fixed_ida); } module_exit(fixed_mdio_bus_exit);