Home | History | Annotate | Line # | Download | only in flash
flash_io.c revision 1.4.8.2
      1  1.4.8.2  matt /*	$NetBSD: flash_io.c,v 1.4.8.2 2011/12/27 17:35:47 matt Exp $	*/
      2  1.4.8.2  matt 
      3  1.4.8.2  matt /*-
      4  1.4.8.2  matt  * Copyright (c) 2011 Department of Software Engineering,
      5  1.4.8.2  matt  *		      University of Szeged, Hungary
      6  1.4.8.2  matt  * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
      7  1.4.8.2  matt  * All rights reserved.
      8  1.4.8.2  matt  *
      9  1.4.8.2  matt  * This code is derived from software contributed to The NetBSD Foundation
     10  1.4.8.2  matt  * by the Department of Software Engineering, University of Szeged, Hungary
     11  1.4.8.2  matt  *
     12  1.4.8.2  matt  * Redistribution and use in source and binary forms, with or without
     13  1.4.8.2  matt  * modification, are permitted provided that the following conditions
     14  1.4.8.2  matt  * are met:
     15  1.4.8.2  matt  * 1. Redistributions of source code must retain the above copyright
     16  1.4.8.2  matt  *    notice, this list of conditions and the following disclaimer.
     17  1.4.8.2  matt  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.4.8.2  matt  *    notice, this list of conditions and the following disclaimer in the
     19  1.4.8.2  matt  *    documentation and/or other materials provided with the distribution.
     20  1.4.8.2  matt  *
     21  1.4.8.2  matt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.4.8.2  matt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.4.8.2  matt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.4.8.2  matt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.4.8.2  matt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26  1.4.8.2  matt  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  1.4.8.2  matt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     28  1.4.8.2  matt  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29  1.4.8.2  matt  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.4.8.2  matt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.4.8.2  matt  * SUCH DAMAGE.
     32  1.4.8.2  matt  */
     33  1.4.8.2  matt 
     34  1.4.8.2  matt #include <sys/cdefs.h>
     35  1.4.8.2  matt __KERNEL_RCSID(0, "$NetBSD: flash_io.c,v 1.4.8.2 2011/12/27 17:35:47 matt Exp $");
     36  1.4.8.2  matt 
     37  1.4.8.2  matt #include <sys/param.h>
     38  1.4.8.2  matt #include <sys/buf.h>
     39  1.4.8.2  matt #include <sys/bufq.h>
     40  1.4.8.2  matt #include <sys/kernel.h>
     41  1.4.8.2  matt #include <sys/kmem.h>
     42  1.4.8.2  matt #include <sys/kthread.h>
     43  1.4.8.2  matt #include <sys/mutex.h>
     44  1.4.8.2  matt #include <sys/sysctl.h>
     45  1.4.8.2  matt 
     46  1.4.8.2  matt #include <dev/flash/flash.h>
     47  1.4.8.2  matt #include <dev/flash/flash_io.h>
     48  1.4.8.2  matt 
     49  1.4.8.2  matt #ifdef FLASH_DEBUG
     50  1.4.8.2  matt extern int flashdebug;
     51  1.4.8.2  matt #endif
     52  1.4.8.2  matt 
     53  1.4.8.2  matt int flash_cachesync_timeout = 1;
     54  1.4.8.2  matt int flash_cachesync_nodenum;
     55  1.4.8.2  matt 
     56  1.4.8.2  matt void flash_io_read(struct flash_io *, struct buf *);
     57  1.4.8.2  matt void flash_io_write(struct flash_io *, struct buf *);
     58  1.4.8.2  matt void flash_io_done(struct flash_io *, struct buf *, int);
     59  1.4.8.2  matt int flash_io_cache_write(struct flash_io *, flash_addr_t, struct buf *);
     60  1.4.8.2  matt void flash_io_cache_sync(struct flash_io *);
     61  1.4.8.2  matt 
     62  1.4.8.2  matt static int
     63  1.4.8.2  matt flash_timestamp_diff(struct bintime *bt, struct bintime *b2)
     64  1.4.8.2  matt {
     65  1.4.8.2  matt 	struct bintime b1 = *bt;
     66  1.4.8.2  matt 	struct timeval tv;
     67  1.4.8.2  matt 
     68  1.4.8.2  matt 	bintime_sub(&b1, b2);
     69  1.4.8.2  matt 	bintime2timeval(&b1, &tv);
     70  1.4.8.2  matt 
     71  1.4.8.2  matt 	return tvtohz(&tv);
     72  1.4.8.2  matt }
     73  1.4.8.2  matt 
     74  1.4.8.2  matt static flash_addr_t
     75  1.4.8.2  matt flash_io_getblock(struct flash_io *fio, struct buf *bp)
     76  1.4.8.2  matt {
     77  1.4.8.2  matt 	flash_off_t block, last;
     78  1.4.8.2  matt 
     79  1.4.8.2  matt 	/* get block number of first byte */
     80  1.4.8.2  matt 	block = bp->b_rawblkno * DEV_BSIZE / fio->fio_if->erasesize;
     81  1.4.8.2  matt 
     82  1.4.8.2  matt 	/* block of the last bite */
     83  1.4.8.2  matt 	last = (bp->b_rawblkno * DEV_BSIZE + bp->b_resid - 1)
     84  1.4.8.2  matt 	    / fio->fio_if->erasesize;
     85  1.4.8.2  matt 
     86  1.4.8.2  matt 	/* spans trough multiple blocks, needs special handling */
     87  1.4.8.2  matt 	if (last != block) {
     88  1.4.8.2  matt 		printf("0x%jx -> 0x%jx\n",
     89  1.4.8.2  matt 		    bp->b_rawblkno * DEV_BSIZE,
     90  1.4.8.2  matt 		    bp->b_rawblkno * DEV_BSIZE + bp->b_resid - 1);
     91  1.4.8.2  matt 		panic("TODO: multiple block write. last: %jd, current: %jd",
     92  1.4.8.2  matt 		    (intmax_t )last, (intmax_t )block);
     93  1.4.8.2  matt 	}
     94  1.4.8.2  matt 
     95  1.4.8.2  matt 	return block;
     96  1.4.8.2  matt }
     97  1.4.8.2  matt 
     98  1.4.8.2  matt int
     99  1.4.8.2  matt flash_sync_thread_init(struct flash_io *fio, device_t dev,
    100  1.4.8.2  matt     struct flash_interface *flash_if)
    101  1.4.8.2  matt {
    102  1.4.8.2  matt 	int error;
    103  1.4.8.2  matt 
    104  1.4.8.2  matt 	FLDPRINTF(("starting flash io thread\n"));
    105  1.4.8.2  matt 
    106  1.4.8.2  matt 	fio->fio_dev = dev;
    107  1.4.8.2  matt 	fio->fio_if = flash_if;
    108  1.4.8.2  matt 
    109  1.4.8.2  matt 	fio->fio_data = kmem_alloc(fio->fio_if->erasesize, KM_SLEEP);
    110  1.4.8.2  matt 
    111  1.4.8.2  matt 	mutex_init(&fio->fio_lock, MUTEX_DEFAULT, IPL_NONE);
    112  1.4.8.2  matt 	cv_init(&fio->fio_cv, "flashcv");
    113  1.4.8.2  matt 
    114  1.4.8.2  matt 	error = bufq_alloc(&fio->fio_bufq, "fcfs", BUFQ_SORT_RAWBLOCK);
    115  1.4.8.2  matt 	if (error)
    116  1.4.8.2  matt 		goto err_bufq;
    117  1.4.8.2  matt 
    118  1.4.8.2  matt 	fio->fio_exiting = false;
    119  1.4.8.2  matt 	fio->fio_write_pending = false;
    120  1.4.8.2  matt 
    121  1.4.8.2  matt 	/* arrange to allocate the kthread */
    122  1.4.8.2  matt 	error = kthread_create(PRI_NONE, KTHREAD_JOINABLE | KTHREAD_MPSAFE,
    123  1.4.8.2  matt 	    NULL, flash_sync_thread, fio, &fio->fio_thread, "flashio");
    124  1.4.8.2  matt 
    125  1.4.8.2  matt 	if (!error)
    126  1.4.8.2  matt 		return 0;
    127  1.4.8.2  matt 
    128  1.4.8.2  matt 	bufq_free(fio->fio_bufq);
    129  1.4.8.2  matt err_bufq:
    130  1.4.8.2  matt 	cv_destroy(&fio->fio_cv);
    131  1.4.8.2  matt 	mutex_destroy(&fio->fio_lock);
    132  1.4.8.2  matt 	kmem_free(fio->fio_data, fio->fio_if->erasesize);
    133  1.4.8.2  matt 
    134  1.4.8.2  matt 	return error;
    135  1.4.8.2  matt }
    136  1.4.8.2  matt 
    137  1.4.8.2  matt void
    138  1.4.8.2  matt flash_sync_thread_destroy(struct flash_io *fio)
    139  1.4.8.2  matt {
    140  1.4.8.2  matt 	FLDPRINTF(("stopping flash io thread\n"));
    141  1.4.8.2  matt 
    142  1.4.8.2  matt 	mutex_enter(&fio->fio_lock);
    143  1.4.8.2  matt 
    144  1.4.8.2  matt 	fio->fio_exiting = true;
    145  1.4.8.2  matt 	cv_broadcast(&fio->fio_cv);
    146  1.4.8.2  matt 
    147  1.4.8.2  matt 	mutex_exit(&fio->fio_lock);
    148  1.4.8.2  matt 
    149  1.4.8.2  matt 	kthread_join(fio->fio_thread);
    150  1.4.8.2  matt 
    151  1.4.8.2  matt 	kmem_free(fio->fio_data, fio->fio_if->erasesize);
    152  1.4.8.2  matt 	bufq_free(fio->fio_bufq);
    153  1.4.8.2  matt 	mutex_destroy(&fio->fio_lock);
    154  1.4.8.2  matt 	cv_destroy(&fio->fio_cv);
    155  1.4.8.2  matt }
    156  1.4.8.2  matt 
    157  1.4.8.2  matt int
    158  1.4.8.2  matt flash_io_submit(struct flash_io *fio, struct buf *bp)
    159  1.4.8.2  matt {
    160  1.4.8.2  matt 	FLDPRINTF(("submitting job to flash io thread: %p\n", bp));
    161  1.4.8.2  matt 
    162  1.4.8.2  matt 	if (__predict_false(fio->fio_exiting)) {
    163  1.4.8.2  matt 		flash_io_done(fio, bp, ENODEV);
    164  1.4.8.2  matt 		return ENODEV;
    165  1.4.8.2  matt 	}
    166  1.4.8.2  matt 
    167  1.4.8.2  matt 	if (BUF_ISREAD(bp)) {
    168  1.4.8.2  matt 		FLDPRINTF(("we have a read job\n"));
    169  1.4.8.2  matt 
    170  1.4.8.2  matt 		mutex_enter(&fio->fio_lock);
    171  1.4.8.2  matt 		if (fio->fio_write_pending)
    172  1.4.8.2  matt 			flash_io_cache_sync(fio);
    173  1.4.8.2  matt 		mutex_exit(&fio->fio_lock);
    174  1.4.8.2  matt 
    175  1.4.8.2  matt 		flash_io_read(fio, bp);
    176  1.4.8.2  matt 	} else {
    177  1.4.8.2  matt 		FLDPRINTF(("we have a write job\n"));
    178  1.4.8.2  matt 
    179  1.4.8.2  matt 		flash_io_write(fio, bp);
    180  1.4.8.2  matt 	}
    181  1.4.8.2  matt 	return 0;
    182  1.4.8.2  matt }
    183  1.4.8.2  matt 
    184  1.4.8.2  matt int
    185  1.4.8.2  matt flash_io_cache_write(struct flash_io *fio, flash_addr_t block, struct buf *bp)
    186  1.4.8.2  matt {
    187  1.4.8.2  matt 	size_t retlen;
    188  1.4.8.2  matt 	flash_addr_t base, offset;
    189  1.4.8.2  matt 	int error;
    190  1.4.8.2  matt 
    191  1.4.8.2  matt 	KASSERT(mutex_owned(&fio->fio_lock));
    192  1.4.8.2  matt 	KASSERT(fio->fio_if->erasesize != 0);
    193  1.4.8.2  matt 
    194  1.4.8.2  matt 	base = block * fio->fio_if->erasesize;
    195  1.4.8.2  matt 	offset = bp->b_rawblkno * DEV_BSIZE - base;
    196  1.4.8.2  matt 
    197  1.4.8.2  matt 	FLDPRINTF(("io cache write, offset: %jd\n", (intmax_t )offset));
    198  1.4.8.2  matt 
    199  1.4.8.2  matt 	if (!fio->fio_write_pending) {
    200  1.4.8.2  matt 		fio->fio_block = block;
    201  1.4.8.2  matt 		/*
    202  1.4.8.2  matt 		 * fill the cache with data from flash,
    203  1.4.8.2  matt 		 * so we dont have to bother with gaps later
    204  1.4.8.2  matt 		 */
    205  1.4.8.2  matt 		FLDPRINTF(("filling buffer from offset %ju\n", (uintmax_t)base));
    206  1.4.8.2  matt 		error = fio->fio_if->read(fio->fio_dev,
    207  1.4.8.2  matt 		    base, fio->fio_if->erasesize,
    208  1.4.8.2  matt 		    &retlen, fio->fio_data);
    209  1.4.8.2  matt 		FLDPRINTF(("cache filled\n"));
    210  1.4.8.2  matt 
    211  1.4.8.2  matt 		if (error)
    212  1.4.8.2  matt 			return error;
    213  1.4.8.2  matt 
    214  1.4.8.2  matt 		fio->fio_write_pending = true;
    215  1.4.8.2  matt 		/* save creation time for aging */
    216  1.4.8.2  matt 		binuptime(&fio->fio_creation);
    217  1.4.8.2  matt 	}
    218  1.4.8.2  matt 	/* copy data to cache */
    219  1.4.8.2  matt 	memcpy(fio->fio_data + offset, bp->b_data, bp->b_resid);
    220  1.4.8.2  matt 	bufq_put(fio->fio_bufq, bp);
    221  1.4.8.2  matt 
    222  1.4.8.2  matt 	/* update timestamp */
    223  1.4.8.2  matt 	binuptime(&fio->fio_last_write);
    224  1.4.8.2  matt 
    225  1.4.8.2  matt 	return 0;
    226  1.4.8.2  matt }
    227  1.4.8.2  matt 
    228  1.4.8.2  matt void
    229  1.4.8.2  matt flash_io_cache_sync(struct flash_io *fio)
    230  1.4.8.2  matt {
    231  1.4.8.2  matt 	struct flash_erase_instruction ei;
    232  1.4.8.2  matt 	struct buf *bp;
    233  1.4.8.2  matt 	size_t retlen;
    234  1.4.8.2  matt 	flash_addr_t base;
    235  1.4.8.2  matt 	int error;
    236  1.4.8.2  matt 
    237  1.4.8.2  matt 	KASSERT(mutex_owned(&fio->fio_lock));
    238  1.4.8.2  matt 
    239  1.4.8.2  matt 	if (!fio->fio_write_pending) {
    240  1.4.8.2  matt 		FLDPRINTF(("trying to sync with an invalid buffer\n"));
    241  1.4.8.2  matt 		return;
    242  1.4.8.2  matt 	}
    243  1.4.8.2  matt 
    244  1.4.8.2  matt 	base = fio->fio_block * fio->fio_if->erasesize;
    245  1.4.8.2  matt 
    246  1.4.8.2  matt 	FLDPRINTF(("eraseing block at 0x%jx\n", (uintmax_t )base));
    247  1.4.8.2  matt 	ei.ei_addr = base;
    248  1.4.8.2  matt 	ei.ei_len = fio->fio_if->erasesize;
    249  1.4.8.2  matt 	ei.ei_callback = NULL;
    250  1.4.8.2  matt 	error = fio->fio_if->erase(fio->fio_dev, &ei);
    251  1.4.8.2  matt 
    252  1.4.8.2  matt 	if (error) {
    253  1.4.8.2  matt 		aprint_error_dev(fio->fio_dev, "cannot erase flash flash!\n");
    254  1.4.8.2  matt 		goto out;
    255  1.4.8.2  matt 	}
    256  1.4.8.2  matt 
    257  1.4.8.2  matt 	FLDPRINTF(("writing %" PRIu32 " bytes to 0x%jx\n",
    258  1.4.8.2  matt 		fio->fio_if->erasesize, (uintmax_t )base));
    259  1.4.8.2  matt 
    260  1.4.8.2  matt 	error = fio->fio_if->write(fio->fio_dev,
    261  1.4.8.2  matt 	    base, fio->fio_if->erasesize, &retlen, fio->fio_data);
    262  1.4.8.2  matt 
    263  1.4.8.2  matt 	if (error || retlen != fio->fio_if->erasesize) {
    264  1.4.8.2  matt 		aprint_error_dev(fio->fio_dev, "can't sync write cache: %d\n", error);
    265  1.4.8.2  matt 		goto out;
    266  1.4.8.2  matt 	}
    267  1.4.8.2  matt 
    268  1.4.8.2  matt out:
    269  1.4.8.2  matt 	while ((bp = bufq_get(fio->fio_bufq)) != NULL)
    270  1.4.8.2  matt 		flash_io_done(fio, bp, error);
    271  1.4.8.2  matt 
    272  1.4.8.2  matt 	fio->fio_block = -1;
    273  1.4.8.2  matt 	fio->fio_write_pending = false;
    274  1.4.8.2  matt }
    275  1.4.8.2  matt 
    276  1.4.8.2  matt void
    277  1.4.8.2  matt flash_sync_thread(void * arg)
    278  1.4.8.2  matt {
    279  1.4.8.2  matt 	struct flash_io *fio = arg;
    280  1.4.8.2  matt 	struct bintime now;
    281  1.4.8.2  matt 
    282  1.4.8.2  matt 	mutex_enter(&fio->fio_lock);
    283  1.4.8.2  matt 
    284  1.4.8.2  matt 	while (!fio->fio_exiting) {
    285  1.4.8.2  matt 		cv_timedwait_sig(&fio->fio_cv, &fio->fio_lock, hz / 4);
    286  1.4.8.2  matt 		if (!fio->fio_write_pending) {
    287  1.4.8.2  matt 			continue;
    288  1.4.8.2  matt 		}
    289  1.4.8.2  matt 		/* see if the cache is older than 3 seconds (safety limit),
    290  1.4.8.2  matt 		 * or if we havent touched the cache since more than 1 ms
    291  1.4.8.2  matt 		 */
    292  1.4.8.2  matt 		binuptime(&now);
    293  1.4.8.2  matt 		if (flash_timestamp_diff(&now, &fio->fio_last_write) > hz / 5) {
    294  1.4.8.2  matt 			FLDPRINTF(("syncing write cache after timeout\n"));
    295  1.4.8.2  matt 			flash_io_cache_sync(fio);
    296  1.4.8.2  matt 		} else if (flash_timestamp_diff(&now, &fio->fio_creation)
    297  1.4.8.2  matt 		    > 3 * hz) {
    298  1.4.8.2  matt 			aprint_error_dev(fio->fio_dev,
    299  1.4.8.2  matt 			    "syncing write cache after 3 sec timeout!\n");
    300  1.4.8.2  matt 			flash_io_cache_sync(fio);
    301  1.4.8.2  matt 		}
    302  1.4.8.2  matt 	}
    303  1.4.8.2  matt 
    304  1.4.8.2  matt 	mutex_exit(&fio->fio_lock);
    305  1.4.8.2  matt 
    306  1.4.8.2  matt 	kthread_exit(0);
    307  1.4.8.2  matt }
    308  1.4.8.2  matt 
    309  1.4.8.2  matt void
    310  1.4.8.2  matt flash_io_read(struct flash_io *fio, struct buf *bp)
    311  1.4.8.2  matt {
    312  1.4.8.2  matt 	size_t retlen;
    313  1.4.8.2  matt 	flash_addr_t offset;
    314  1.4.8.2  matt 	int error;
    315  1.4.8.2  matt 
    316  1.4.8.2  matt 	FLDPRINTF(("flash io read\n"));
    317  1.4.8.2  matt 
    318  1.4.8.2  matt 	offset = bp->b_rawblkno * DEV_BSIZE;
    319  1.4.8.2  matt 
    320  1.4.8.2  matt 	error = fio->fio_if->read(fio->fio_dev, offset, bp->b_resid,
    321  1.4.8.2  matt 	    &retlen, bp->b_data);
    322  1.4.8.2  matt 
    323  1.4.8.2  matt 	flash_io_done(fio, bp, error);
    324  1.4.8.2  matt }
    325  1.4.8.2  matt 
    326  1.4.8.2  matt void
    327  1.4.8.2  matt flash_io_write(struct flash_io *fio, struct buf *bp)
    328  1.4.8.2  matt {
    329  1.4.8.2  matt 	flash_addr_t block;
    330  1.4.8.2  matt 
    331  1.4.8.2  matt 	FLDPRINTF(("flash io write\n"));
    332  1.4.8.2  matt 
    333  1.4.8.2  matt 	block = flash_io_getblock(fio, bp);
    334  1.4.8.2  matt 	FLDPRINTF(("write to block %jd\n", (intmax_t )block));
    335  1.4.8.2  matt 
    336  1.4.8.2  matt 	mutex_enter(&fio->fio_lock);
    337  1.4.8.2  matt 
    338  1.4.8.2  matt 	if (fio->fio_write_pending && fio->fio_block != block) {
    339  1.4.8.2  matt 		FLDPRINTF(("writing to new block, syncing caches\n"));
    340  1.4.8.2  matt 		flash_io_cache_sync(fio);
    341  1.4.8.2  matt 	}
    342  1.4.8.2  matt 
    343  1.4.8.2  matt 	flash_io_cache_write(fio, block, bp);
    344  1.4.8.2  matt 
    345  1.4.8.2  matt 	mutex_exit(&fio->fio_lock);
    346  1.4.8.2  matt }
    347  1.4.8.2  matt 
    348  1.4.8.2  matt void
    349  1.4.8.2  matt flash_io_done(struct flash_io *fio, struct buf *bp, int error)
    350  1.4.8.2  matt {
    351  1.4.8.2  matt 	FLDPRINTF(("io done: %p\n", bp));
    352  1.4.8.2  matt 
    353  1.4.8.2  matt 	if (error == 0)
    354  1.4.8.2  matt 		bp->b_resid = 0;
    355  1.4.8.2  matt 
    356  1.4.8.2  matt 	bp->b_error = error;
    357  1.4.8.2  matt 	biodone(bp);
    358  1.4.8.2  matt }
    359  1.4.8.2  matt 
    360  1.4.8.2  matt static int
    361  1.4.8.2  matt sysctl_flash_verify(SYSCTLFN_ARGS)
    362  1.4.8.2  matt {
    363  1.4.8.2  matt 	int error, t;
    364  1.4.8.2  matt 	struct sysctlnode node;
    365  1.4.8.2  matt 
    366  1.4.8.2  matt 	node = *rnode;
    367  1.4.8.2  matt 	t = *(int *)rnode->sysctl_data;
    368  1.4.8.2  matt 	node.sysctl_data = &t;
    369  1.4.8.2  matt 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    370  1.4.8.2  matt 	if (error || newp == NULL)
    371  1.4.8.2  matt 		return error;
    372  1.4.8.2  matt 
    373  1.4.8.2  matt 	if (node.sysctl_num == flash_cachesync_nodenum) {
    374  1.4.8.2  matt 		if (t <= 0 || t > 60)
    375  1.4.8.2  matt 			return EINVAL;
    376  1.4.8.2  matt 	} else {
    377  1.4.8.2  matt 		return EINVAL;
    378  1.4.8.2  matt 	}
    379  1.4.8.2  matt 
    380  1.4.8.2  matt 	*(int *)rnode->sysctl_data = t;
    381  1.4.8.2  matt 
    382  1.4.8.2  matt 	return 0;
    383  1.4.8.2  matt }
    384  1.4.8.2  matt 
    385  1.4.8.2  matt SYSCTL_SETUP(sysctl_flash, "sysctl flash subtree setup")
    386  1.4.8.2  matt {
    387  1.4.8.2  matt 	int rc, flash_root_num;
    388  1.4.8.2  matt 	const struct sysctlnode *node;
    389  1.4.8.2  matt 
    390  1.4.8.2  matt 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
    391  1.4.8.2  matt 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
    392  1.4.8.2  matt 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
    393  1.4.8.2  matt 		goto error;
    394  1.4.8.2  matt 	}
    395  1.4.8.2  matt 
    396  1.4.8.2  matt 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    397  1.4.8.2  matt 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "flash",
    398  1.4.8.2  matt 	    SYSCTL_DESCR("FLASH driver controls"),
    399  1.4.8.2  matt 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
    400  1.4.8.2  matt 		goto error;
    401  1.4.8.2  matt 	}
    402  1.4.8.2  matt 
    403  1.4.8.2  matt 	flash_root_num = node->sysctl_num;
    404  1.4.8.2  matt 
    405  1.4.8.2  matt 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    406  1.4.8.2  matt 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    407  1.4.8.2  matt 	    CTLTYPE_INT, "cache_sync_timeout",
    408  1.4.8.2  matt 	    SYSCTL_DESCR("FLASH write cache sync timeout in seconds"),
    409  1.4.8.2  matt 	    sysctl_flash_verify, 0, &flash_cachesync_timeout,
    410  1.4.8.2  matt 	    0, CTL_HW, flash_root_num, CTL_CREATE,
    411  1.4.8.2  matt 	    CTL_EOL)) != 0) {
    412  1.4.8.2  matt 		goto error;
    413  1.4.8.2  matt 	}
    414  1.4.8.2  matt 
    415  1.4.8.2  matt 	flash_cachesync_nodenum = node->sysctl_num;
    416  1.4.8.2  matt 
    417  1.4.8.2  matt 	return;
    418  1.4.8.2  matt 
    419  1.4.8.2  matt error:
    420  1.4.8.2  matt 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    421  1.4.8.2  matt }
    422