Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/nand fix some bugs in detachment



details:   https://anonhg.NetBSD.org/src/rev/2e6681fff0dd
branches:  trunk
changeset: 764529:2e6681fff0dd
user:      ahoka <ahoka%NetBSD.org@localhost>
date:      Tue Apr 26 13:38:13 2011 +0000

description:
fix some bugs in detachment

diffstat:

 sys/dev/nand/nand.c         |  40 ++++++++++++++++++++++++++++++++--------
 sys/dev/nand/nand.h         |  14 +++++++++-----
 sys/dev/nand/nand_bbt.c     |   4 +---
 sys/dev/nand/nand_io.c      |  30 ++++++++++++++++++++++--------
 sys/dev/nand/nandemulator.c |  10 ++++++++--
 5 files changed, 72 insertions(+), 26 deletions(-)

diffs (287 lines):

diff -r 17e1e6a221b2 -r 2e6681fff0dd sys/dev/nand/nand.c
--- a/sys/dev/nand/nand.c       Tue Apr 26 11:32:38 2011 +0000
+++ b/sys/dev/nand/nand.c       Tue Apr 26 13:38:13 2011 +0000
@@ -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)
 {
diff -r 17e1e6a221b2 -r 2e6681fff0dd sys/dev/nand/nand.h
--- a/sys/dev/nand/nand.h       Tue Apr 26 11:32:38 2011 +0000
+++ b/sys/dev/nand/nand.h       Tue Apr 26 13:38:13 2011 +0000
@@ -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 */
diff -r 17e1e6a221b2 -r 2e6681fff0dd sys/dev/nand/nand_bbt.c
--- a/sys/dev/nand/nand_bbt.c   Tue Apr 26 11:32:38 2011 +0000
+++ b/sys/dev/nand/nand_bbt.c   Tue Apr 26 13:38:13 2011 +0000
@@ -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
diff -r 17e1e6a221b2 -r 2e6681fff0dd sys/dev/nand/nand_io.c
--- a/sys/dev/nand/nand_io.c    Tue Apr 26 11:32:38 2011 +0000
+++ b/sys/dev/nand/nand_io.c    Tue Apr 26 13:38:13 2011 +0000
@@ -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));
        
diff -r 17e1e6a221b2 -r 2e6681fff0dd sys/dev/nand/nandemulator.c
--- a/sys/dev/nand/nandemulator.c       Tue Apr 26 11:32:38 2011 +0000
+++ b/sys/dev/nand/nandemulator.c       Tue Apr 26 13:38:13 2011 +0000
@@ -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;
        }



Home | Main Index | Thread Index | Old Index