Module Name:    src
Committed By:   ahoka
Date:           Tue Apr 26 13:38:13 UTC 2011

Modified Files:
        src/sys/dev/nand: nand.c nand.h nand_bbt.c nand_io.c nandemulator.c

Log Message:
fix some bugs in detachment


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/dev/nand/nand.c
cvs rdiff -u -r1.6 -r1.7 src/sys/dev/nand/nand.h
cvs rdiff -u -r1.2 -r1.3 src/sys/dev/nand/nand_bbt.c \
    src/sys/dev/nand/nand_io.c
cvs rdiff -u -r1.3 -r1.4 src/sys/dev/nand/nandemulator.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/nand/nand.c
diff -u src/sys/dev/nand/nand.c:1.8 src/sys/dev/nand/nand.c:1.9
--- src/sys/dev/nand/nand.c:1.8	Sun Apr 10 12:48:09 2011
+++ src/sys/dev/nand/nand.c	Tue Apr 26 13:38:13 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: nand.c,v 1.8 2011/04/10 12:48:09 ahoka Exp $	*/
+/*	$NetBSD: nand.c,v 1.9 2011/04/26 13:38:13 ahoka Exp $	*/
 
 /*-
  * Copyright (c) 2010 Department of Software Engineering,
@@ -34,7 +34,7 @@
 /* Common driver for NAND chips implementing the ONFI 2.2 specification */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nand.c,v 1.8 2011/04/10 12:48:09 ahoka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nand.c,v 1.9 2011/04/26 13:38:13 ahoka Exp $");
 
 #include "locators.h"
 
@@ -43,6 +43,7 @@
 #include <sys/device.h>
 #include <sys/kmem.h>
 #include <sys/sysctl.h>
+#include <sys/atomic.h>
 
 #include <dev/flash/flash.h>
 #include <dev/nand/nand.h>
@@ -57,6 +58,7 @@
 void nand_attach(device_t, device_t, void *);
 int nand_detach(device_t, int);
 bool nand_shutdown(device_t, int);
+void nand_childdet(device_t, device_t);
 
 int nand_print(void *, const char *);
 
@@ -67,8 +69,12 @@
 static int nand_scan_media(device_t, struct nand_chip *);
 static bool nand_check_wp(device_t);
 
-CFATTACH_DECL_NEW(nand, sizeof(struct nand_softc),
-    nand_match, nand_attach, nand_detach, NULL);
+CFATTACH_DECL2_NEW(nand, sizeof(struct nand_softc),
+    nand_match, nand_attach, nand_detach,
+    NULL, NULL, nand_childdet);
+
+//CFATTACH_DECL_NEW(nand, sizeof(struct nand_softc),
+//    nand_match, nand_attach, nand_detach, NULL);
 
 #ifdef NAND_DEBUG
 int	nanddebug = NAND_DEBUG;
@@ -126,6 +132,8 @@
 	sc->controller_dev = parent;
 	sc->nand_if = naa->naa_nand_if;
 
+	sc->sc_children = 0;
+
 	aprint_naive("\n");
 
 	if (nand_check_wp(self)) {
@@ -211,8 +219,12 @@
 	faa.flash_if = flash_if;
 
 	if (config_match(parent, cf, &faa)) {
-		config_attach(parent, cf, &faa, nand_print);
-		return 0;
+		if (config_attach(parent, cf, &faa, nand_print) != NULL) {
+			atomic_inc_uint(&sc->sc_children);
+			return 0;
+		} else {
+			return 1;
+		}
 	} else {
 		kmem_free(flash_if, sizeof(*flash_if));
 	}
@@ -227,11 +239,14 @@
 	struct nand_chip *chip = &sc->sc_chip;
 	int ret = 0;
 
+	if (sc->sc_children != 0) {
+		return EBUSY;
+	}
+
+	nand_sync_thread_stop(self);
 #ifdef NAND_BBT
 	nand_bbt_detach(self);
 #endif
-	nand_sync_thread_stop(self);
-
 	/* free oob cache */
 	kmem_free(chip->nc_oob_cache, chip->nc_spare_size);
 	kmem_free(chip->nc_page_cache, chip->nc_page_size);
@@ -244,6 +259,15 @@
 	return ret;
 }
 
+void
+nand_childdet(device_t self, device_t child)
+{
+	struct nand_softc *sc = device_private(self);
+
+	atomic_dec_uint(&sc->sc_children);
+	KASSERT(sc->sc_children >= 0);
+}
+
 int
 nand_print(void *aux, const char *pnp)
 {

Index: src/sys/dev/nand/nand.h
diff -u src/sys/dev/nand/nand.h:1.6 src/sys/dev/nand/nand.h:1.7
--- src/sys/dev/nand/nand.h:1.6	Sun Apr 10 12:48:09 2011
+++ src/sys/dev/nand/nand.h	Tue Apr 26 13:38:13 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: nand.h,v 1.6 2011/04/10 12:48:09 ahoka Exp $	*/
+/*	$NetBSD: nand.h,v 1.7 2011/04/26 13:38:13 ahoka Exp $	*/
 
 /*-
  * Copyright (c) 2010 Department of Software Engineering,
@@ -45,11 +45,9 @@
 #include <dev/flash/flash.h>
 
 #ifdef NAND_DEBUG
-#define DPRINTF(x)	if (nanddebug) printf x
-#define DPRINTFN(n,x)	if (nanddebug>(n)) printf x
+#define DPRINTF(x)	printf x
 #else
 #define DPRINTF(x)
-#define DPRINTFN(n,x)
 #endif
 
 //#define NAND_VERBOSE
@@ -161,9 +159,15 @@
 	struct lwp *sc_sync_thread;
 	struct nand_write_cache sc_cache;
 	kmutex_t sc_io_lock;
-	kmutex_t sc_waitq_lock;
 	kcondvar_t sc_io_cv;
 	bool sc_io_running;
+
+	/* currently we cant automatically detach children
+	 * so keep count of attached children so we will
+	 * know, that when is safe to detach...
+	 * XXX is it a problem only as a module? (ioconf bug?)
+	 */
+	unsigned int sc_children;
 };
 
 /* structure holding the nand api */

Index: src/sys/dev/nand/nand_bbt.c
diff -u src/sys/dev/nand/nand_bbt.c:1.2 src/sys/dev/nand/nand_bbt.c:1.3
--- src/sys/dev/nand/nand_bbt.c:1.2	Mon Apr  4 14:25:10 2011
+++ src/sys/dev/nand/nand_bbt.c	Tue Apr 26 13:38:13 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: nand_bbt.c,v 1.2 2011/04/04 14:25:10 ahoka Exp $	*/
+/*	$NetBSD: nand_bbt.c,v 1.3 2011/04/26 13:38:13 ahoka Exp $	*/
 
 /*-
  * Copyright (c) 2011 Department of Software Engineering,
@@ -58,9 +58,7 @@
 	struct nand_softc *sc = device_private(self);
 	struct nand_bbt *bbt = &sc->sc_bbt;
 
-	printf("freeing bbt bitmap...");
 	kmem_free(bbt->nbbt_bitmap, bbt->nbbt_size);
-	printf("done!\n");
 }
 
 void
Index: src/sys/dev/nand/nand_io.c
diff -u src/sys/dev/nand/nand_io.c:1.2 src/sys/dev/nand/nand_io.c:1.3
--- src/sys/dev/nand/nand_io.c:1.2	Mon Apr  4 14:25:10 2011
+++ src/sys/dev/nand/nand_io.c	Tue Apr 26 13:38:13 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: nand_io.c,v 1.2 2011/04/04 14:25:10 ahoka Exp $	*/
+/*	$NetBSD: nand_io.c,v 1.3 2011/04/26 13:38:13 ahoka Exp $	*/
 
 /*-
  * Copyright (c) 2011 Department of Software Engineering,
@@ -51,7 +51,7 @@
 
 void nand_io_read(device_t, struct buf *);
 void nand_io_write(device_t, struct buf *);
-void nand_io_done(device_t, int error, struct buf *);
+void nand_io_done(device_t, struct buf *, int);
 int nand_io_cache_write(device_t, daddr_t, struct buf *);
 void nand_io_cache_sync(device_t);
 
@@ -147,15 +147,24 @@
 	kmem_free(wc->nwc_data, chip->nc_block_size);
 	
 	sc->sc_io_running = false;
+	
+	mutex_enter(&sc->sc_io_lock);
+	cv_broadcast(&sc->sc_io_cv);
+	mutex_exit(&sc->sc_io_lock);
+	
 	kthread_join(sc->sc_sync_thread);
 
 	bufq_free(wc->nwc_bufq);
-
-	mutex_destroy(&sc->sc_io_lock);
-	mutex_destroy(&sc->sc_waitq_lock);
 	mutex_destroy(&wc->nwc_lock);
 
+#ifdef DIAGNOSTIC
+	mutex_enter(&sc->sc_io_lock);
+	KASSERT(!cv_has_waiters(&sc->sc_io_cv));
+	mutex_exit(&sc->sc_io_lock);
+#endif
+	
 	cv_destroy(&sc->sc_io_cv);
+	mutex_destroy(&sc->sc_io_lock);
 }
 
 int
@@ -166,6 +175,11 @@
 
 	DPRINTF(("submitting job to nand io thread: %p\n", bp));
 
+	if (__predict_false(!sc->sc_io_running)) {
+		nand_io_done(self, bp, ENODEV);
+		return ENODEV;
+	}
+
 	if (BUF_ISREAD(bp)) {
 		DPRINTF(("we have a read job\n"));
 		
@@ -274,7 +288,7 @@
 
 out:
 	while ((bp = bufq_get(wc->nwc_bufq)) != NULL)
-		nand_io_done(self, error, bp);
+		nand_io_done(self, bp, error);
 	
 	wc->nwc_block = -1;
 	wc->nwc_write_pending = false;
@@ -332,7 +346,7 @@
 	error = nand_flash_read(self, offset, bp->b_resid,
 	    &retlen, bp->b_data);
 	
-	nand_io_done(self, error, bp);
+	nand_io_done(self, bp, error);
 }
 
 void
@@ -360,7 +374,7 @@
 }
 
 void
-nand_io_done(device_t self, int error, struct buf *bp)
+nand_io_done(device_t self, struct buf *bp, int error)
 {
 	DPRINTF(("io done: %p\n", bp));
 	

Index: src/sys/dev/nand/nandemulator.c
diff -u src/sys/dev/nand/nandemulator.c:1.3 src/sys/dev/nand/nandemulator.c:1.4
--- src/sys/dev/nand/nandemulator.c:1.3	Sun Apr 10 10:56:37 2011
+++ src/sys/dev/nand/nandemulator.c	Tue Apr 26 13:38:13 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: nandemulator.c,v 1.3 2011/04/10 10:56:37 ahoka Exp $	*/
+/*	$NetBSD: nandemulator.c,v 1.4 2011/04/26 13:38:13 ahoka Exp $	*/
 
 /*-
  * Copyright (c) 2011 Department of Software Engineering,
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nandemulator.c,v 1.3 2011/04/10 10:56:37 ahoka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nandemulator.c,v 1.4 2011/04/26 13:38:13 ahoka Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
@@ -785,6 +785,7 @@
 		(void)config_attach_pseudo(nandemulator_cfdata);
 
 		return 0;
+
 	case MODULE_CMD_FINI:
 		error = config_cfdata_detach(nandemulator_cfdata);
 		if (error) {
@@ -796,6 +797,11 @@
 		config_cfdriver_detach(&nandemulator_cd);
 
 		return 0;
+
+	case MODULE_CMD_AUTOUNLOAD:
+		/* prevent auto-unload */
+		return EBUSY;
+
 	default:
 		return ENOTTY;
 	}

Reply via email to