git: aa48c1ae0831 - main - etherswitch: Cleanup detach and delete of child devices during detach

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Thu, 02 Jan 2025 18:28:31 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=aa48c1ae0831789c6aa34bf3a85b9a2289d425e2

commit aa48c1ae0831789c6aa34bf3a85b9a2289d425e2
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2025-01-02 18:24:46 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2025-01-02 18:24:46 +0000

    etherswitch: Cleanup detach and delete of child devices during detach
    
    Call bus_generic_detach first and return any error.  Remove no longer
    needed individual device_delete_child calls.
    
    Differential Revision:  https://reviews.freebsd.org/D47970
---
 sys/dev/etherswitch/ar40xx/ar40xx_main.c  | 9 +++++----
 sys/dev/etherswitch/arswitch/arswitch.c   | 9 +++++----
 sys/dev/etherswitch/e6000sw/e6060sw.c     | 9 +++++----
 sys/dev/etherswitch/infineon/adm6996fc.c  | 9 +++++----
 sys/dev/etherswitch/ip17x/ip17x.c         | 9 +++++----
 sys/dev/etherswitch/micrel/ksz8995ma.c    | 9 +++++----
 sys/dev/etherswitch/mtkswitch/mtkswitch.c | 9 +++++----
 sys/dev/etherswitch/rtl8366/rtl8366rb.c   | 9 +++++----
 sys/dev/etherswitch/ukswitch/ukswitch.c   | 9 +++++----
 9 files changed, 45 insertions(+), 36 deletions(-)

diff --git a/sys/dev/etherswitch/ar40xx/ar40xx_main.c b/sys/dev/etherswitch/ar40xx/ar40xx_main.c
index 41e6813bc840..d5636d26120b 100644
--- a/sys/dev/etherswitch/ar40xx/ar40xx_main.c
+++ b/sys/dev/etherswitch/ar40xx/ar40xx_main.c
@@ -254,22 +254,23 @@ static int
 ar40xx_detach(device_t dev)
 {
 	struct ar40xx_softc *sc = device_get_softc(dev);
-	int i;
+	int error, i;
 
 	device_printf(sc->sc_dev, "%s: called\n", __func__);
 
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
+
 	callout_drain(&sc->sc_phy_callout);
 
 	/* Free PHYs */
 	for (i = 0; i < AR40XX_NUM_PHYS; i++) {
-		if (sc->sc_phys.miibus[i] != NULL)
-			device_delete_child(dev, sc->sc_phys.miibus[i]);
 		if (sc->sc_phys.ifp[i] != NULL)
 			if_free(sc->sc_phys.ifp[i]);
 		free(sc->sc_phys.ifname[i], M_DEVBUF);
 	}
 
-	bus_generic_detach(dev);
 	mtx_destroy(&sc->sc_mtx);
 
 	return (0);
diff --git a/sys/dev/etherswitch/arswitch/arswitch.c b/sys/dev/etherswitch/arswitch/arswitch.c
index 92c3460e5f78..924793a0488f 100644
--- a/sys/dev/etherswitch/arswitch/arswitch.c
+++ b/sys/dev/etherswitch/arswitch/arswitch.c
@@ -666,13 +666,15 @@ static int
 arswitch_detach(device_t dev)
 {
 	struct arswitch_softc *sc = device_get_softc(dev);
-	int i;
+	int error, i;
 
 	callout_drain(&sc->callout_tick);
 
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
+
 	for (i=0; i < sc->numphys; i++) {
-		if (sc->miibus[i] != NULL)
-			device_delete_child(dev, sc->miibus[i]);
 		if (sc->ifp[i] != NULL)
 			if_free(sc->ifp[i]);
 		free(sc->ifname[i], M_DEVBUF);
@@ -680,7 +682,6 @@ arswitch_detach(device_t dev)
 
 	free(sc->atu.entries, M_DEVBUF);
 
-	bus_generic_detach(dev);
 	mtx_destroy(&sc->sc_mtx);
 
 	return (0);
diff --git a/sys/dev/etherswitch/e6000sw/e6060sw.c b/sys/dev/etherswitch/e6000sw/e6060sw.c
index 374eb8c5316b..901f887ffdc6 100644
--- a/sys/dev/etherswitch/e6000sw/e6060sw.c
+++ b/sys/dev/etherswitch/e6000sw/e6060sw.c
@@ -321,18 +321,20 @@ static int
 e6060sw_detach(device_t dev)
 {
 	struct e6060sw_softc *sc;
-	int i, port;
+	int error, i, port;
 
 	sc = device_get_softc(dev);
 
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
+
 	callout_drain(&sc->callout_tick);
 
 	for (i = 0; i < MII_NPHY; i++) {
 		if (((1 << i) & sc->phymask) == 0)
 			continue;
 		port = e6060sw_portforphy(sc, i);
-		if (sc->miibus[port] != NULL)
-			device_delete_child(dev, (*sc->miibus[port]));
 		if (sc->ifp[port] != NULL)
 			if_free(sc->ifp[port]);
 		free(sc->ifname[port], M_E6060SW);
@@ -344,7 +346,6 @@ e6060sw_detach(device_t dev)
 	free(sc->ifname, M_E6060SW);
 	free(sc->ifp, M_E6060SW);
 
-	bus_generic_detach(dev);
 	mtx_destroy(&sc->sc_mtx);
 
 	return (0);
diff --git a/sys/dev/etherswitch/infineon/adm6996fc.c b/sys/dev/etherswitch/infineon/adm6996fc.c
index 2a8ee58107ee..58a3f9625d4a 100644
--- a/sys/dev/etherswitch/infineon/adm6996fc.c
+++ b/sys/dev/etherswitch/infineon/adm6996fc.c
@@ -281,18 +281,20 @@ static int
 adm6996fc_detach(device_t dev)
 {
 	struct adm6996fc_softc	*sc;
-	int			 i, port;
+	int			 error, i, port;
 
 	sc = device_get_softc(dev);
 
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
+
 	callout_drain(&sc->callout_tick);
 
 	for (i = 0; i < MII_NPHY; i++) {
 		if (((1 << i) & sc->phymask) == 0)
 			continue;
 		port = adm6996fc_portforphy(sc, i);
-		if (sc->miibus[port] != NULL)
-			device_delete_child(dev, (*sc->miibus[port]));
 		if (sc->ifp[port] != NULL)
 			if_free(sc->ifp[port]);
 		free(sc->ifname[port], M_ADM6996FC);
@@ -304,7 +306,6 @@ adm6996fc_detach(device_t dev)
 	free(sc->ifname, M_ADM6996FC);
 	free(sc->ifp, M_ADM6996FC);
 
-	bus_generic_detach(dev);
 	mtx_destroy(&sc->sc_mtx);
 
 	return (0);
diff --git a/sys/dev/etherswitch/ip17x/ip17x.c b/sys/dev/etherswitch/ip17x/ip17x.c
index 1eee45148040..c90d46c49857 100644
--- a/sys/dev/etherswitch/ip17x/ip17x.c
+++ b/sys/dev/etherswitch/ip17x/ip17x.c
@@ -274,7 +274,11 @@ static int
 ip17x_detach(device_t dev)
 {
 	struct ip17x_softc *sc;
-	int i, port;
+	int error, i, port;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 	if (sc->miipoll)
@@ -284,8 +288,6 @@ ip17x_detach(device_t dev)
 		if (((1 << i) & sc->phymask) == 0)
 			continue;
 		port = sc->phyport[i];
-		if (sc->miibus[port] != NULL)
-			device_delete_child(dev, (*sc->miibus[port]));
 		if (sc->ifp[port] != NULL)
 			if_free(sc->ifp[port]);
 		free(sc->miibus[port], M_IP17X);
@@ -299,7 +301,6 @@ ip17x_detach(device_t dev)
 	/* Reset the switch. */
 	sc->hal.ip17x_reset(sc);
 
-	bus_generic_detach(dev);
 	mtx_destroy(&sc->sc_mtx);
 
 	return (0);
diff --git a/sys/dev/etherswitch/micrel/ksz8995ma.c b/sys/dev/etherswitch/micrel/ksz8995ma.c
index a51bdb50fdfc..c2ac994fe882 100644
--- a/sys/dev/etherswitch/micrel/ksz8995ma.c
+++ b/sys/dev/etherswitch/micrel/ksz8995ma.c
@@ -339,7 +339,11 @@ static int
 ksz8995ma_detach(device_t dev)
 {
 	struct ksz8995ma_softc	*sc;
-	int			 i, port;
+	int			 error, i, port;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 
@@ -349,8 +353,6 @@ ksz8995ma_detach(device_t dev)
 		if (((1 << i) & sc->phymask) == 0)
 			continue;
 		port = ksz8995ma_portforphy(sc, i);
-		if (sc->miibus[port] != NULL)
-			device_delete_child(dev, (*sc->miibus[port]));
 		if (sc->ifp[port] != NULL)
 			if_free(sc->ifp[port]);
 		free(sc->ifname[port], M_KSZ8995MA);
@@ -362,7 +364,6 @@ ksz8995ma_detach(device_t dev)
 	free(sc->ifname, M_KSZ8995MA);
 	free(sc->ifp, M_KSZ8995MA);
 
-	bus_generic_detach(dev);
 	mtx_destroy(&sc->sc_mtx);
 
 	return (0);
diff --git a/sys/dev/etherswitch/mtkswitch/mtkswitch.c b/sys/dev/etherswitch/mtkswitch/mtkswitch.c
index 7c4e8ae6f934..89e092d02ac4 100644
--- a/sys/dev/etherswitch/mtkswitch/mtkswitch.c
+++ b/sys/dev/etherswitch/mtkswitch/mtkswitch.c
@@ -248,19 +248,20 @@ static int
 mtkswitch_detach(device_t dev)
 {
 	struct mtkswitch_softc *sc = device_get_softc(dev);
-	int phy;
+	int error, phy;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	callout_drain(&sc->callout_tick);
 
 	for (phy = 0; phy < MTKSWITCH_MAX_PHYS; phy++) {
-		if (sc->miibus[phy] != NULL)
-			device_delete_child(dev, sc->miibus[phy]);
 		if (sc->ifp[phy] != NULL)
 			if_free(sc->ifp[phy]);
 		free(sc->ifname[phy], M_DEVBUF);
 	}
 
-	bus_generic_detach(dev);
 	mtx_destroy(&sc->sc_mtx);
 
 	return (0);
diff --git a/sys/dev/etherswitch/rtl8366/rtl8366rb.c b/sys/dev/etherswitch/rtl8366/rtl8366rb.c
index 304e7f7b8325..079244b2f745 100644
--- a/sys/dev/etherswitch/rtl8366/rtl8366rb.c
+++ b/sys/dev/etherswitch/rtl8366/rtl8366rb.c
@@ -268,18 +268,19 @@ static int
 rtl8366rb_detach(device_t dev)
 {
 	struct rtl8366rb_softc *sc;
-	int i;
+	int error, i;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	sc = device_get_softc(dev);
 
 	for (i=0; i < sc->numphys; i++) {
-		if (sc->miibus[i])
-			device_delete_child(dev, sc->miibus[i]);
 		if (sc->ifp[i] != NULL)
 			if_free(sc->ifp[i]);
 		free(sc->ifname[i], M_DEVBUF);
 	}
-	bus_generic_detach(dev);
 	callout_drain(&sc->callout_tick);
 	mtx_destroy(&sc->callout_mtx);
 	mtx_destroy(&sc->sc_mtx);
diff --git a/sys/dev/etherswitch/ukswitch/ukswitch.c b/sys/dev/etherswitch/ukswitch/ukswitch.c
index ed1a27a19494..a2e30c3af8a1 100644
--- a/sys/dev/etherswitch/ukswitch/ukswitch.c
+++ b/sys/dev/etherswitch/ukswitch/ukswitch.c
@@ -224,7 +224,11 @@ static int
 ukswitch_detach(device_t dev)
 {
 	struct ukswitch_softc *sc = device_get_softc(dev);
-	int i, port;
+	int error, i, port;
+
+	error = bus_generic_detach(dev);
+	if (error != 0)
+		return (error);
 
 	callout_drain(&sc->callout_tick);
 
@@ -232,8 +236,6 @@ ukswitch_detach(device_t dev)
 		if (((1 << i) & sc->phymask) == 0)
 			continue;
 		port = ukswitch_portforphy(sc, i);
-		if (sc->miibus[port] != NULL)
-			device_delete_child(dev, (*sc->miibus[port]));
 		if (sc->ifp[port] != NULL)
 			if_free(sc->ifp[port]);
 		free(sc->ifname[port], M_UKSWITCH);
@@ -245,7 +247,6 @@ ukswitch_detach(device_t dev)
 	free(sc->ifname, M_UKSWITCH);
 	free(sc->ifp, M_UKSWITCH);
 
-	bus_generic_detach(dev);
 	mtx_destroy(&sc->sc_mtx);
 
 	return (0);