5a22fbcc10
When LAN9303 is MDIO-connected two callchains exist into
mdio->bus->write():
1. switch ports 1&2 ("physical" PHYs):
virtual (switch-internal) MDIO bus (lan9303_switch_ops->phy_{read|write})->
lan9303_mdio_phy_{read|write} -> mdiobus_{read|write}_nested
2. LAN9303 virtual PHY:
virtual MDIO bus (lan9303_phy_{read|write}) ->
lan9303_virt_phy_reg_{read|write} -> regmap -> lan9303_mdio_{read|write}
If the latter functions just take
mutex_lock(&sw_dev->device->bus->mdio_lock) it triggers a LOCKDEP
false-positive splat. It's false-positive because the first
mdio_lock in the second callchain above belongs to virtual MDIO bus, the
second mdio_lock belongs to physical MDIO bus.
Consequent annotation in lan9303_mdio_{read|write} as nested lock
(similar to lan9303_mdio_phy_{read|write}, it's the same physical MDIO bus)
prevents the following splat:
WARNING: possible circular locking dependency detected
5.15.71 #1 Not tainted
------------------------------------------------------
kworker/u4:3/609 is trying to acquire lock:
ffff000011531c68 (lan9303_mdio:131:(&lan9303_mdio_regmap_config)->lock){+.+.}-{3:3}, at: regmap_lock_mutex
but task is already holding lock:
ffff0000114c44d8 (&bus->mdio_lock){+.+.}-{3:3}, at: mdiobus_read
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&bus->mdio_lock){+.+.}-{3:3}:
lock_acquire
__mutex_lock
mutex_lock_nested
lan9303_mdio_read
_regmap_read
regmap_read
lan9303_probe
lan9303_mdio_probe
mdio_probe
really_probe
__driver_probe_device
driver_probe_device
__device_attach_driver
bus_for_each_drv
__device_attach
device_initial_probe
bus_probe_device
deferred_probe_work_func
process_one_work
worker_thread
kthread
ret_from_fork
-> #0 (lan9303_mdio:131:(&lan9303_mdio_regmap_config)->lock){+.+.}-{3:3}:
__lock_acquire
lock_acquire.part.0
lock_acquire
__mutex_lock
mutex_lock_nested
regmap_lock_mutex
regmap_read
lan9303_phy_read
dsa_slave_phy_read
__mdiobus_read
mdiobus_read
get_phy_device
mdiobus_scan
__mdiobus_register
dsa_register_switch
lan9303_probe
lan9303_mdio_probe
mdio_probe
really_probe
__driver_probe_device
driver_probe_device
__device_attach_driver
bus_for_each_drv
__device_attach
device_initial_probe
bus_probe_device
deferred_probe_work_func
process_one_work
worker_thread
kthread
ret_from_fork
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&bus->mdio_lock);
lock(lan9303_mdio:131:(&lan9303_mdio_regmap_config)->lock);
lock(&bus->mdio_lock);
lock(lan9303_mdio:131:(&lan9303_mdio_regmap_config)->lock);
*** DEADLOCK ***
5 locks held by kworker/u4:3/609:
#0: ffff000002842938 ((wq_completion)events_unbound){+.+.}-{0:0}, at: process_one_work
#1: ffff80000bacbd60 (deferred_probe_work){+.+.}-{0:0}, at: process_one_work
#2: ffff000007645178 (&dev->mutex){....}-{3:3}, at: __device_attach
#3: ffff8000096e6e78 (dsa2_mutex){+.+.}-{3:3}, at: dsa_register_switch
#4: ffff0000114c44d8 (&bus->mdio_lock){+.+.}-{3:3}, at: mdiobus_read
stack backtrace:
CPU: 1 PID: 609 Comm: kworker/u4:3 Not tainted 5.15.71 #1
Workqueue: events_unbound deferred_probe_work_func
Call trace:
dump_backtrace
show_stack
dump_stack_lvl
dump_stack
print_circular_bug
check_noncircular
__lock_acquire
lock_acquire.part.0
lock_acquire
__mutex_lock
mutex_lock_nested
regmap_lock_mutex
regmap_read
lan9303_phy_read
dsa_slave_phy_read
__mdiobus_read
mdiobus_read
get_phy_device
mdiobus_scan
__mdiobus_register
dsa_register_switch
lan9303_probe
lan9303_mdio_probe
...
Cc: stable@vger.kernel.org
Fixes: dc70058315
("net: dsa: LAN9303: add MDIO managed mode support")
Signed-off-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Link: https://lore.kernel.org/r/20231027065741.534971-1-alexander.sverdlin@siemens.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
178 lines
4.7 KiB
C
178 lines
4.7 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de>
|
|
*
|
|
* Partially based on a patch from
|
|
* Copyright (c) 2014 Stefan Roese <sr@denx.de>
|
|
*/
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/mdio.h>
|
|
#include <linux/phy.h>
|
|
#include <linux/of.h>
|
|
|
|
#include "lan9303.h"
|
|
|
|
/* Generate phy-addr and -reg from the input address */
|
|
#define PHY_ADDR(x) ((((x) >> 6) + 0x10) & 0x1f)
|
|
#define PHY_REG(x) (((x) >> 1) & 0x1f)
|
|
|
|
struct lan9303_mdio {
|
|
struct mdio_device *device;
|
|
struct lan9303 chip;
|
|
};
|
|
|
|
static void lan9303_mdio_real_write(struct mdio_device *mdio, int reg, u16 val)
|
|
{
|
|
mdio->bus->write(mdio->bus, PHY_ADDR(reg), PHY_REG(reg), val);
|
|
}
|
|
|
|
static int lan9303_mdio_write(void *ctx, uint32_t reg, uint32_t val)
|
|
{
|
|
struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
|
|
|
|
reg <<= 2; /* reg num to offset */
|
|
mutex_lock_nested(&sw_dev->device->bus->mdio_lock, MDIO_MUTEX_NESTED);
|
|
lan9303_mdio_real_write(sw_dev->device, reg, val & 0xffff);
|
|
lan9303_mdio_real_write(sw_dev->device, reg + 2, (val >> 16) & 0xffff);
|
|
mutex_unlock(&sw_dev->device->bus->mdio_lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static u16 lan9303_mdio_real_read(struct mdio_device *mdio, int reg)
|
|
{
|
|
return mdio->bus->read(mdio->bus, PHY_ADDR(reg), PHY_REG(reg));
|
|
}
|
|
|
|
static int lan9303_mdio_read(void *ctx, uint32_t reg, uint32_t *val)
|
|
{
|
|
struct lan9303_mdio *sw_dev = (struct lan9303_mdio *)ctx;
|
|
|
|
reg <<= 2; /* reg num to offset */
|
|
mutex_lock_nested(&sw_dev->device->bus->mdio_lock, MDIO_MUTEX_NESTED);
|
|
*val = lan9303_mdio_real_read(sw_dev->device, reg);
|
|
*val |= (lan9303_mdio_real_read(sw_dev->device, reg + 2) << 16);
|
|
mutex_unlock(&sw_dev->device->bus->mdio_lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int lan9303_mdio_phy_write(struct lan9303 *chip, int phy, int reg,
|
|
u16 val)
|
|
{
|
|
struct lan9303_mdio *sw_dev = dev_get_drvdata(chip->dev);
|
|
|
|
return mdiobus_write_nested(sw_dev->device->bus, phy, reg, val);
|
|
}
|
|
|
|
static int lan9303_mdio_phy_read(struct lan9303 *chip, int phy, int reg)
|
|
{
|
|
struct lan9303_mdio *sw_dev = dev_get_drvdata(chip->dev);
|
|
|
|
return mdiobus_read_nested(sw_dev->device->bus, phy, reg);
|
|
}
|
|
|
|
static const struct lan9303_phy_ops lan9303_mdio_phy_ops = {
|
|
.phy_read = lan9303_mdio_phy_read,
|
|
.phy_write = lan9303_mdio_phy_write,
|
|
};
|
|
|
|
static const struct regmap_config lan9303_mdio_regmap_config = {
|
|
.reg_bits = 8,
|
|
.val_bits = 32,
|
|
.reg_stride = 1,
|
|
.can_multi_write = true,
|
|
.max_register = 0x0ff, /* address bits 0..1 are not used */
|
|
.reg_format_endian = REGMAP_ENDIAN_LITTLE,
|
|
|
|
.volatile_table = &lan9303_register_set,
|
|
.wr_table = &lan9303_register_set,
|
|
.rd_table = &lan9303_register_set,
|
|
|
|
.reg_read = lan9303_mdio_read,
|
|
.reg_write = lan9303_mdio_write,
|
|
|
|
.cache_type = REGCACHE_NONE,
|
|
};
|
|
|
|
static int lan9303_mdio_probe(struct mdio_device *mdiodev)
|
|
{
|
|
struct lan9303_mdio *sw_dev;
|
|
int ret;
|
|
|
|
sw_dev = devm_kzalloc(&mdiodev->dev, sizeof(struct lan9303_mdio),
|
|
GFP_KERNEL);
|
|
if (!sw_dev)
|
|
return -ENOMEM;
|
|
|
|
sw_dev->chip.regmap = devm_regmap_init(&mdiodev->dev, NULL, sw_dev,
|
|
&lan9303_mdio_regmap_config);
|
|
if (IS_ERR(sw_dev->chip.regmap)) {
|
|
ret = PTR_ERR(sw_dev->chip.regmap);
|
|
dev_err(&mdiodev->dev, "regmap init failed: %d\n", ret);
|
|
return ret;
|
|
}
|
|
|
|
/* link forward and backward */
|
|
sw_dev->device = mdiodev;
|
|
dev_set_drvdata(&mdiodev->dev, sw_dev);
|
|
sw_dev->chip.dev = &mdiodev->dev;
|
|
|
|
sw_dev->chip.ops = &lan9303_mdio_phy_ops;
|
|
|
|
ret = lan9303_probe(&sw_dev->chip, mdiodev->dev.of_node);
|
|
if (ret != 0)
|
|
return ret;
|
|
|
|
dev_info(&mdiodev->dev, "LAN9303 MDIO driver loaded successfully\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void lan9303_mdio_remove(struct mdio_device *mdiodev)
|
|
{
|
|
struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev);
|
|
|
|
if (!sw_dev)
|
|
return;
|
|
|
|
lan9303_remove(&sw_dev->chip);
|
|
}
|
|
|
|
static void lan9303_mdio_shutdown(struct mdio_device *mdiodev)
|
|
{
|
|
struct lan9303_mdio *sw_dev = dev_get_drvdata(&mdiodev->dev);
|
|
|
|
if (!sw_dev)
|
|
return;
|
|
|
|
lan9303_shutdown(&sw_dev->chip);
|
|
|
|
dev_set_drvdata(&mdiodev->dev, NULL);
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
static const struct of_device_id lan9303_mdio_of_match[] = {
|
|
{ .compatible = "smsc,lan9303-mdio" },
|
|
{ .compatible = "microchip,lan9354-mdio" },
|
|
{ /* sentinel */ },
|
|
};
|
|
MODULE_DEVICE_TABLE(of, lan9303_mdio_of_match);
|
|
|
|
static struct mdio_driver lan9303_mdio_driver = {
|
|
.mdiodrv.driver = {
|
|
.name = "LAN9303_MDIO",
|
|
.of_match_table = lan9303_mdio_of_match,
|
|
},
|
|
.probe = lan9303_mdio_probe,
|
|
.remove = lan9303_mdio_remove,
|
|
.shutdown = lan9303_mdio_shutdown,
|
|
};
|
|
mdio_module_driver(lan9303_mdio_driver);
|
|
|
|
MODULE_AUTHOR("Stefan Roese <sr@denx.de>, Juergen Borleis <kernel@pengutronix.de>");
|
|
MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in MDIO managed mode");
|
|
MODULE_LICENSE("GPL v2");
|