Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'net-phy-fixed_phy-replace-list-of-fixed-phys-with-static-array'

Heiner Kallweit says:

====================
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.
In addition replace heavy-weight IDA with a simple bitmap.
====================

Link: https://patch.msgid.link/110f676d-727c-4575-abe4-e383f98fc38f@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+31 -56
+31 -56
drivers/net/phy/fixed_phy.c
··· 10 10 11 11 #include <linux/kernel.h> 12 12 #include <linux/module.h> 13 - #include <linux/list.h> 14 13 #include <linux/mii.h> 15 14 #include <linux/phy.h> 16 15 #include <linux/phy_fixed.h> 17 16 #include <linux/err.h> 18 17 #include <linux/slab.h> 19 18 #include <linux/of.h> 20 - #include <linux/idr.h> 21 19 #include <linux/netdevice.h> 22 20 23 21 #include "swphy.h" 24 22 23 + /* The DSA loop driver may allocate 4 fixed PHY's, and 4 additional 24 + * fixed PHY's for a system should be sufficient. 25 + */ 26 + #define NUM_FP 8 27 + 25 28 struct fixed_phy { 26 - int addr; 27 29 struct phy_device *phydev; 28 30 struct fixed_phy_status status; 29 31 int (*link_update)(struct net_device *, struct fixed_phy_status *); 30 - struct list_head node; 31 32 }; 32 33 34 + static DECLARE_BITMAP(fixed_phy_ids, NUM_FP); 35 + static struct fixed_phy fmb_fixed_phys[NUM_FP]; 33 36 static struct mii_bus *fmb_mii_bus; 34 - static LIST_HEAD(fmb_phys); 35 37 36 38 static struct fixed_phy *fixed_phy_find(int addr) 37 39 { 38 - struct fixed_phy *fp; 39 - 40 - list_for_each_entry(fp, &fmb_phys, node) { 41 - if (fp->addr == addr) 42 - return fp; 43 - } 44 - 45 - return NULL; 40 + return test_bit(addr, fixed_phy_ids) ? fmb_fixed_phys + addr : NULL; 46 41 } 47 42 48 43 int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier) ··· 103 108 } 104 109 EXPORT_SYMBOL_GPL(fixed_phy_set_link_update); 105 110 106 - static int __fixed_phy_add(int phy_addr, 107 - const struct fixed_phy_status *status) 108 - { 109 - struct fixed_phy *fp; 110 - int ret; 111 - 112 - ret = swphy_validate_state(status); 113 - if (ret < 0) 114 - return ret; 115 - 116 - fp = kzalloc(sizeof(*fp), GFP_KERNEL); 117 - if (!fp) 118 - return -ENOMEM; 119 - 120 - fp->addr = phy_addr; 121 - fp->status = *status; 122 - fp->status.link = true; 123 - 124 - list_add_tail(&fp->node, &fmb_phys); 125 - 126 - return 0; 127 - } 128 - 129 - static DEFINE_IDA(phy_fixed_ida); 130 - 131 111 static void fixed_phy_del(int phy_addr) 132 112 { 133 113 struct fixed_phy *fp; ··· 111 141 if (!fp) 112 142 return; 113 143 114 - list_del(&fp->node); 115 - kfree(fp); 116 - ida_free(&phy_fixed_ida, phy_addr); 144 + memset(fp, 0, sizeof(*fp)); 145 + clear_bit(phy_addr, fixed_phy_ids); 146 + } 147 + 148 + static int fixed_phy_get_free_addr(void) 149 + { 150 + int addr; 151 + 152 + do { 153 + addr = find_first_zero_bit(fixed_phy_ids, NUM_FP); 154 + if (addr == NUM_FP) 155 + return -ENOSPC; 156 + } while (test_and_set_bit(addr, fixed_phy_ids)); 157 + 158 + return addr; 117 159 } 118 160 119 161 struct phy_device *fixed_phy_register(const struct fixed_phy_status *status, ··· 135 153 int phy_addr; 136 154 int ret; 137 155 156 + ret = swphy_validate_state(status); 157 + if (ret < 0) 158 + return ERR_PTR(ret); 159 + 138 160 if (!fmb_mii_bus || fmb_mii_bus->state != MDIOBUS_REGISTERED) 139 161 return ERR_PTR(-EPROBE_DEFER); 140 162 141 - /* Get the next available PHY address, up to PHY_MAX_ADDR */ 142 - phy_addr = ida_alloc_max(&phy_fixed_ida, PHY_MAX_ADDR - 1, GFP_KERNEL); 163 + /* Get the next available PHY address, up to NUM_FP */ 164 + phy_addr = fixed_phy_get_free_addr(); 143 165 if (phy_addr < 0) 144 166 return ERR_PTR(phy_addr); 145 167 146 - ret = __fixed_phy_add(phy_addr, status); 147 - if (ret < 0) { 148 - ida_free(&phy_fixed_ida, phy_addr); 149 - return ERR_PTR(ret); 150 - } 168 + fmb_fixed_phys[phy_addr].status = *status; 169 + fmb_fixed_phys[phy_addr].status.link = true; 151 170 152 171 phy = get_phy_device(fmb_mii_bus, phy_addr, false); 153 172 if (IS_ERR(phy)) { ··· 220 237 221 238 static void __exit fixed_mdio_bus_exit(void) 222 239 { 223 - struct fixed_phy *fp, *tmp; 224 - 225 240 mdiobus_unregister(fmb_mii_bus); 226 241 mdiobus_free(fmb_mii_bus); 227 - 228 - list_for_each_entry_safe(fp, tmp, &fmb_phys, node) { 229 - list_del(&fp->node); 230 - kfree(fp); 231 - } 232 - ida_destroy(&phy_fixed_ida); 233 242 } 234 243 module_exit(fixed_mdio_bus_exit); 235 244