net: mdio-mux: Recover gracefully from bad parents
Subsystems (2)
v2 cover net: mdio-mux: Recover gracefully from bad parents
This series fixes a few bugs I ran into when the parent MDIO bus doesn't behave. You can reproduce these problems by running mdio list on a board where the parent MDIO bus fails to probe (such as if there's no MAC and NET_RANDOM_ETHADDR is disabled).
Changes in v2:
- Don't try to get the parent unless the mux is active
Sean Anderson (2):
net: mdio-mux: Don't crash if our parent never shows up
net: mdio-mux: Remove ourselves when our parent is removed
drivers/net/Kconfig | 1 + net/mdio-mux-uclass.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+)
---
base-commit: 2b8902913c19eb581fb0d567201c09b8fa46ac74
branch: mdio_mux_parent
Signature · 1 line
2.53.0
PATCH v2 1/2 net: mdio-mux: Don't crash if our parent never shows up
The parent MDIO bus may never show up (such as if there is an error while probing it). Make sure to return an error instead of crashing.
---
(no changes since v1)
net/mdio-mux-uclass.c | 9 +++++++++ 1 file changed, 9 insertions(+)
net/mdio-mux-uclass.c +9 -0
@@ -86,6 +86,9 @@ static int mmux_read(struct udevice *ch, int addr, int devad,struct udevice *parent_mdio =mmux_get_parent_mdio (mux);int err; +if (!parent_mdio) +return -ENODEV ; + err =mmux_change_sel (ch, true);if (err)return err;
@@ -104,6 +107,9 @@ static int mmux_write(struct udevice *ch, int addr, int devad,struct udevice *parent_mdio =mmux_get_parent_mdio (mux);int err; +if (!parent_mdio) +return -ENODEV ; + err =mmux_change_sel (ch, true);if (err)return err;
@@ -121,6 +127,9 @@ static int mmux_reset(struct udevice *ch)struct udevice *parent_mdio =mmux_get_parent_mdio (mux);int err; +if (!parent_mdio) +return -ENODEV ; +/* reset is optional, if it's not implemented just exit */ if (!mdio_get_ops (parent_mdio)->reset)return 0 ;
Signature · 1 line
2.53.0
PATCH v2 2/2 net: mdio-mux: Remove ourselves when our parent is removed
In reply to Sean AndersonNormally, all child devices are removed before their parent is removed. However, mdio muxes are not direct children of their parents, so they can stick around even when their parent has been removed. This can cause crashes when the mux tries to access the parent bus.
Fix this by listening for device removals. When an MDIO bus is removed, we remove any child that has that bus as a parent. It's possible we may fail to remove a child if there are multiple children of a bus and the first child fails to remove. However, there are no such configurations in-tree so I went with a simpler approach.
---
Changes in v2:
- Don't try to get the parent unless the mux is active
drivers/net/Kconfig | 1 + net/mdio-mux-uclass.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
drivers/net/Kconfig +1 -0
@@ -30,6 +30,7 @@ config DM_MDIO config DM_MDIO_MUX bool "Enable Driver Model for MDIO MUX devices" + select DM_EVENT depends on DM_MDIO help Enable driver model for MDIO MUX devices
net/mdio-mux-uclass.c +30 -0
@@ -7,6 +7,7 @@#include <dm.h> #include <log.h> #include <miiphy.h> +#include <dm/device_compat.h> #include <dm/device-internal.h> #include <dm/uclass-internal.h> #include <dm/lists.h>
@@ -231,3 +232,32 @@ UCLASS_DRIVER(mdio_mux) = { .per_device_auto = sizeof(struct mdio_mux_perdev_priv), .per_child_plat_auto = sizeof(struct mdio_mux_ch_data), }; + +static int dm_mdio_mux_parent_pre_remove (void *ctx,struct event *event) +{ +struct udevice *mdio_parent = event->data.dm.dev; +struct udevice *mux, *mux_next; +struct uclass *uc; +int ret; + +if (device_get_uclass_id (mdio_parent) !=UCLASS_MDIO ) +return 0 ; + +if (uclass_get (UCLASS_MDIO_MUX , &uc)) +return 0 ; + + uclass_foreach_dev_safe(mux, mux_next, uc) { +if (!device_active (mux) || +mmux_get_parent_mdio (mux) != mdio_parent) +continue ; + +dev_dbg (mdio_parent,"removing %s %s\n" , mux->driver->name, + mux->name); + ret =device_remove (mux,DM_REMOVE_NORMAL ); +if (ret) +return ret; + } + +return 0 ; +} +EVENT_SPY_FULL (EVT_DM_PRE_REMOVE , dm_mdio_mux_parent_pre_remove);
Signature · 1 line
2.53.0
Quoted 9 lines · Sean Anderson
This series fixes a few bugs I ran into when the parent MDIO bus doesn't behave. You can reproduce these problems by running
mdio liston a board where the parent MDIO bus fails to probe (such as if there's no MAC andNET_RANDOM_ETHADDRis disabled).Changes in v2:
- Don't try to get the parent unless the mux is active[...]
Applied to u-boot-net branch for-next, thanks!
[1/2] net: mdio-mux: Don't crash if our parent never shows up
commit: c89733c1f43383d447895cf94fedcf9ed57b8101
[2/2] net: mdio-mux: Remove ourselves when our parent is removed
commit: 2e27bbb8803f40ea828b3a681735d47be5909ffbBest regards,
Signature · 1 line
Jerome Forissier <[email protected]>