[PATCH net-next 06/16] net: pcs: xpcs: Avoid creating dummy XPCS MDIO device

Serge Semin
2023-12-05T10:36:32Z

If the DW XPCS MDIO devices are either left unmasked for being auto-probed
or explicitly registered in the MDIO subsystem by means of the
mdiobus_register_board_info() method there is no point in creating the
dummy MDIO device instance in order to get the DW XPCS handler since the
MDIO core subsystem will create the device during the MDIO bus
registration procedure. All what needs to be done is to just reuse the
MDIO-device instance available in the mii_bus.mdio_map array (using some
getter for it would look better though). It shall prevent the XPCS devices
been accessed over several MDIO-device instances.

Note since the MDIO-device instance might be retrieved from the MDIO-bus
map array its reference counter shall be increased. If the MDIO-device
instance is created in the xpcs_create_mdiodev() method its reference
counter will be already increased. So there is no point in toggling the
reference counter in the xpcs_create() function. Just drop it from there.

Signed-off-by: Serge Semin <fancer.lancer@gmail.com>
---
drivers/net/pcs/pcs-xpcs.c | 26 +++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/drivers/net/pcs/pcs-xpcs.c b/drivers/net/pcs/pcs-xpcs.c
index 2850122f354a..a53376472394 100644
--- a/drivers/net/pcs/pcs-xpcs.c
+++ b/drivers/net/pcs/pcs-xpcs.c
@@ -1376,7 +1376,6 @@ static struct dw_xpcs *xpcs_create(struct mdio_device *mdiodev,
if (!xpcs)
return ERR_PTR(-ENOMEM);

- mdio_device_get(mdiodev);
xpcs->mdiodev = mdiodev;

xpcs_id = xpcs_get_id(xpcs);
@@ -1417,7 +1416,6 @@ static struct dw_xpcs *xpcs_create(struct mdio_device *mdiodev,
ret = -ENODEV;

out:
- mdio_device_put(mdiodev);
kfree(xpcs);

return ERR_PTR(ret);
@@ -1437,19 +1435,21 @@ struct dw_xpcs *xpcs_create_mdiodev(struct mii_bus *bus, int addr,
struct mdio_device *mdiodev;
struct dw_xpcs *xpcs;

- mdiodev = mdio_device_create(bus, addr);
- if (IS_ERR(mdiodev))
- return ERR_CAST(mdiodev);
+ if (addr >= PHY_MAX_ADDR)
+ return ERR_PTR(-EINVAL);

- xpcs = xpcs_create(mdiodev, interface);
+ if (mdiobus_is_registered_device(bus, addr)) {
+ mdiodev = bus->mdio_map[addr];
+ mdio_device_get(mdiodev);
+ } else {
+ mdiodev = mdio_device_create(bus, addr);
+ if (IS_ERR(mdiodev))
+ return ERR_CAST(mdiodev);
+ }

- /* xpcs_create() has taken a refcount on the mdiodev if it was
- * successful. If xpcs_create() fails, this will free the mdio
- * device here. In any case, we don't need to hold our reference
- * anymore, and putting it here will allow mdio_device_put() in
- * xpcs_destroy() to automatically free the mdio device.
- */
- mdio_device_put(mdiodev);
+ xpcs = xpcs_create(mdiodev, interface);
+ if (IS_ERR(xpcs))
+ mdio_device_put(mdiodev);

return xpcs;
}
--
2.42.1