net: mdio-mux: Recover gracefully from bad parents
Subsystems (2)
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).
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 | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+)
---
base-commit: 2b8902913c19eb581fb0d567201c09b8fa46ac74
branch: mdio_mux_parent
Signature · 1 line
2.53.0
PATCH 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.
---
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 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.
---
drivers/net/Kconfig | 1 + net/mdio-mux-uclass.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 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 +29 -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,31 @@ 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 (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
Re: [PATCH 2/2] net: mdio-mux: Remove ourselves when our parent is removed
Quoted 53 lines · Sean Anderson
Normally, 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.
Fixes 8880edba06d5 ("net: add MDIO_MUX DM class")Signed-off-by Sean Anderson <[email protected]>---
drivers/net/Kconfig | 1 + net/mdio-mux-uclass.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 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 devicesnet/mdio-mux-uclass.c +7
-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,31 @@ 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;
net/mdio-mux-uclass.c UCLASS_DRIVER(mdio_mux) = { · line 250
Quoted 10 lines
net/mdio-mux-uclass.c +10
-0+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 (mmux_get_parent_mdio (mux) != mdio_parent)
This needs a check for device_active() which I will add in v2.
--Sean
Quoted 12 lines
net/mdio-mux-uclass.c +12
-0+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);