Commit 9a95f4c4 authored by Florian Fainelli's avatar Florian Fainelli Committed by Ben Hutchings

net: phy: Manage fixed PHY address space using IDA

commit 69fc58a5 upstream.

If we have a system which uses fixed PHY devices and calls
fixed_phy_register() then fixed_phy_unregister() we can exhaust the
number of fixed PHYs available after a while, since we keep incrementing
the variable phy_fixed_addr, but we never decrement it.

This patch fixes that by converting the fixed PHY allocation to using
IDA, which takes care of the allocation/dealloaction of the PHY
addresses for us.

Fixes: a7595121 ("net: phy: extend fixed driver with fixed_phy_register()")
Signed-off-by: default avatarFlorian Fainelli <f.fainelli@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
[bwh: Backported to 3.16:
 - Adjust filename, context
 - fixed_phy_register() returns an integer, not a pointer/error]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
parent ceb3c227
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <linux/err.h> #include <linux/err.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/of.h> #include <linux/of.h>
#include <linux/idr.h>
#define MII_REGS_NUM 29 #define MII_REGS_NUM 29
...@@ -204,6 +205,8 @@ int fixed_phy_add(unsigned int irq, int phy_addr, ...@@ -204,6 +205,8 @@ int fixed_phy_add(unsigned int irq, int phy_addr,
} }
EXPORT_SYMBOL_GPL(fixed_phy_add); EXPORT_SYMBOL_GPL(fixed_phy_add);
static DEFINE_IDA(phy_fixed_ida);
void fixed_phy_del(int phy_addr) void fixed_phy_del(int phy_addr)
{ {
struct fixed_mdio_bus *fmb = &platform_fmb; struct fixed_mdio_bus *fmb = &platform_fmb;
...@@ -213,15 +216,13 @@ void fixed_phy_del(int phy_addr) ...@@ -213,15 +216,13 @@ void fixed_phy_del(int phy_addr)
if (fp->addr == phy_addr) { if (fp->addr == phy_addr) {
list_del(&fp->node); list_del(&fp->node);
kfree(fp); kfree(fp);
ida_simple_remove(&phy_fixed_ida, phy_addr);
return; return;
} }
} }
} }
EXPORT_SYMBOL_GPL(fixed_phy_del); EXPORT_SYMBOL_GPL(fixed_phy_del);
static int phy_fixed_addr;
static DEFINE_SPINLOCK(phy_fixed_addr_lock);
int fixed_phy_register(unsigned int irq, int fixed_phy_register(unsigned int irq,
struct fixed_phy_status *status, struct fixed_phy_status *status,
struct device_node *np) struct device_node *np)
...@@ -232,17 +233,15 @@ int fixed_phy_register(unsigned int irq, ...@@ -232,17 +233,15 @@ int fixed_phy_register(unsigned int irq,
int ret; int ret;
/* Get the next available PHY address, up to PHY_MAX_ADDR */ /* Get the next available PHY address, up to PHY_MAX_ADDR */
spin_lock(&phy_fixed_addr_lock); phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
if (phy_fixed_addr == PHY_MAX_ADDR) { if (phy_addr < 0)
spin_unlock(&phy_fixed_addr_lock); return phy_addr;
return -ENOSPC;
}
phy_addr = phy_fixed_addr++;
spin_unlock(&phy_fixed_addr_lock);
ret = fixed_phy_add(irq, phy_addr, status); ret = fixed_phy_add(irq, phy_addr, status);
if (ret < 0) if (ret < 0) {
ida_simple_remove(&phy_fixed_ida, phy_addr);
return ret; return ret;
}
phy = get_phy_device(fmb->mii_bus, phy_addr, false); phy = get_phy_device(fmb->mii_bus, phy_addr, false);
if (!phy || IS_ERR(phy)) { if (!phy || IS_ERR(phy)) {
...@@ -317,6 +316,7 @@ static void __exit fixed_mdio_bus_exit(void) ...@@ -317,6 +316,7 @@ static void __exit fixed_mdio_bus_exit(void)
list_del(&fp->node); list_del(&fp->node);
kfree(fp); kfree(fp);
} }
ida_destroy(&phy_fixed_ida);
} }
module_exit(fixed_mdio_bus_exit); module_exit(fixed_mdio_bus_exit);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment