git: 43c4b47b75f3 - main - flex_spi: Don't try to destroy disk if it doesn't exist

From: Wojciech Macek <wma_at_FreeBSD.org>
Date: Tue, 07 Dec 2021 06:14:12 UTC
The branch main has been updated by wma:

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

commit 43c4b47b75f37313c122799fdf7488251bdc7cf7
Author:     Kornel Duleba <mindal@semihalf.com>
AuthorDate: 2021-12-06 14:17:04 +0000
Commit:     Wojciech Macek <wma@FreeBSD.org>
CommitDate: 2021-12-07 06:13:53 +0000

    flex_spi: Don't try to destroy disk if it doesn't exist
    
    Try to stop and destroy the dist only if the driver has been successfully
    attached. Otherwise a kernel panic will be triggered from disk_destroy.
    The issue was observed on a board that missed SPI clock description in DT.
---
 sys/dev/flash/flexspi/flex_spi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/sys/dev/flash/flexspi/flex_spi.c b/sys/dev/flash/flexspi/flex_spi.c
index fc85919fc2f8..fea3134730f1 100644
--- a/sys/dev/flash/flexspi/flex_spi.c
+++ b/sys/dev/flash/flexspi/flex_spi.c
@@ -824,6 +824,9 @@ flex_spi_detach(device_t dev)
 	sc = device_get_softc(dev);
 	err = 0;
 
+	if (!device_is_attached(dev))
+		goto free_resources;
+
 	mtx_lock(&sc->disk_mtx);
 	if (sc->taskstate == TSTATE_RUNNING) {
 		sc->taskstate = TSTATE_STOPPING;
@@ -848,7 +851,7 @@ flex_spi_detach(device_t dev)
 	}
 
 	/* Disable hardware. */
-
+free_resources:
 	/* Release memory resource. */
 	if (sc->mem_res != NULL)
 		bus_release_resource(dev, SYS_RES_MEMORY,