Home | History | Annotate | Line # | Download | only in dev
md.c revision 1.5
      1 /*	$NetBSD: md.c,v 1.5 1996/03/07 10:26:29 leo Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Gordon W. Ross, Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  * 4. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by
     20  *			Gordon W. Ross and Leo Weppelman.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * This implements a general-puspose RAM-disk.
     36  * See ramdisk.h for notes on the config types.
     37  *
     38  * Note that this driver provides the same functionality
     39  * as the MFS filesystem hack, but this is better because
     40  * you can use this for any filesystem type you'd like!
     41  *
     42  * Credit for most of the kmem ramdisk code goes to:
     43  *   Leo Weppelman (atari) and Phil Nelson (pc532)
     44  * Credit for the ideas behind the "user space RAM" code goes
     45  * to the authors of the MFS implementation.
     46  */
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/buf.h>
     51 #include <sys/device.h>
     52 #include <sys/disk.h>
     53 
     54 #include <vm/vm.h>
     55 #include <vm/vm_kern.h>
     56 /* Don't want all those other VM headers... */
     57 extern vm_offset_t	 kmem_alloc __P((vm_map_t, vm_size_t));
     58 
     59 #include <dev/ramdisk.h>
     60 
     61 /*
     62  * By default, include the user-space functionality.
     63  * Use:  option RAMDISK_SERVER=0 to turn it off.
     64  */
     65 #ifndef RAMDISK_SERVER
     66 #define	RAMDISK_SERVER 1
     67 #endif
     68 
     69 /*
     70  * XXX: the "control" unit is (base unit + 16).
     71  * We should just use the cdev as the "control", but
     72  * that interferes with the security stuff preventing
     73  * simulatneous use of raw and block devices.
     74  *
     75  * XXX Assumption: 16 RAM-disks are enough!
     76  */
     77 #define RD_IS_CTRL(unit) (unit & 0x10)
     78 #define RD_UNIT(unit)    (unit &  0xF)
     79 
     80 /*
     81  * XXX -  This is just for a sanity check.  Only
     82  * applies to kernel-space RAM disk allocations.
     83  */
     84 #define RD_KMEM_MAX_SIZE	0x100000	/* 1MB */
     85 
     86 /* autoconfig stuff... */
     87 
     88 struct rd_softc {
     89 	struct device sc_dev;	/* REQUIRED first entry */
     90 	struct disk sc_dkdev;	/* hook for generic disk handling */
     91 	struct rd_conf sc_rd;
     92 	struct buf *sc_buflist;
     93 	int sc_flags;
     94 };
     95 /* shorthand for fields in sc_rd: */
     96 #define sc_addr sc_rd.rd_addr
     97 #define sc_size sc_rd.rd_size
     98 #define sc_type sc_rd.rd_type
     99 /* flags */
    100 #define RD_ISOPEN	0x01
    101 #define RD_SERVED	0x02
    102 
    103 static int  rd_match (struct device *, void *self, void *);
    104 static void rd_attach(struct device *, struct device *self, void *);
    105 
    106 struct cfdriver rdcd = {
    107 	NULL, "rd", rd_match, rd_attach,
    108 	DV_DULL, sizeof(struct rd_softc), NULL, 0 };
    109 
    110 void rdstrategy __P((struct buf *bp));
    111 
    112 struct dkdriver rddkdriver = { rdstrategy };
    113 
    114 static int
    115 rd_match(parent, self, aux)
    116 	struct device	*parent;
    117 	void	*self;
    118 	void	*aux;
    119 {
    120 #ifdef	RAMDISK_HOOKS
    121 	/*
    122 	 * This external function allows for a machine dependent
    123 	 * match function.
    124 	 */
    125 	return (rd_match_hook(parent, self, aux));
    126 #else
    127 	return(1);
    128 #endif
    129 }
    130 
    131 static void
    132 rd_attach(parent, self, aux)
    133 	struct device	*parent, *self;
    134 	void		*aux;
    135 {
    136 	struct rd_softc *sc = (struct rd_softc *)self;
    137 
    138 	/* XXX - Could accept aux info here to set the config. */
    139 #ifdef	RAMDISK_HOOKS
    140 	/*
    141 	 * This external function might setup a pre-loaded disk.
    142 	 * All it would need to do is setup the rd_conf struct.
    143 	 * See sys/arch/sun3/dev/rd_root.c for an example.
    144 	 */
    145 	rd_attach_hook(sc->sc_dev.dv_unit, &sc->sc_rd);
    146 #endif
    147 	printf("\n");
    148 
    149 	/*
    150 	 * Initialize and attach the disk structure.
    151 	 */
    152 	bzero(&sc->sc_dkdev, sizeof(sc->sc_dkdev));
    153 	sc->sc_dkdev.dk_driver = &rddkdriver;
    154 	sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
    155 	disk_attach(&sc->sc_dkdev);
    156 }
    157 
    158 /*
    159  * operational routines:
    160  * open, close, read, write, strategy,
    161  * ioctl, dump, size
    162  */
    163 
    164 #if RAMDISK_SERVER
    165 static int rd_server_loop __P((struct rd_softc *sc));
    166 static int rd_ioctl_server __P((struct rd_softc *sc,
    167 		struct rd_conf *urd, struct proc *proc));
    168 #endif
    169 
    170 int rddump(dev, blkno, va, size)
    171 	dev_t dev;
    172 	daddr_t blkno;
    173 	caddr_t va;
    174 	size_t size;
    175 {
    176 	return ENODEV;
    177 }
    178 
    179 int rdsize(dev_t dev)
    180 {
    181 	int unit;
    182 	struct rd_softc *sc;
    183 
    184 	/* Disallow control units. */
    185 	unit = minor(dev);
    186 	if (unit >= rdcd.cd_ndevs)
    187 		return 0;
    188 	sc = rdcd.cd_devs[unit];
    189 	if (sc == NULL)
    190 		return 0;
    191 
    192 	if (sc->sc_type == RD_UNCONFIGURED)
    193 		return 0;
    194 
    195 	return (sc->sc_size >> DEV_BSHIFT);
    196 }
    197 
    198 int rdopen(dev, flag, fmt, proc)
    199 	dev_t   dev;
    200 	int     flag, fmt;
    201 	struct proc *proc;
    202 {
    203 	int md, unit;
    204 	struct rd_softc *sc;
    205 
    206 	md = minor(dev);
    207 	unit = RD_UNIT(md);
    208 	if (unit >= rdcd.cd_ndevs)
    209 		return ENXIO;
    210 	sc = rdcd.cd_devs[unit];
    211 	if (sc == NULL)
    212 		return ENXIO;
    213 
    214 	/*
    215 	 * The control device is not exclusive, and can
    216 	 * open uninitialized units (so you can setconf).
    217 	 */
    218 	if (RD_IS_CTRL(md))
    219 		return 0;
    220 
    221 #ifdef	RAMDISK_HOOKS
    222 	/* Call the open hook to allow loading the device. */
    223 	rd_open_hook(unit, &sc->sc_rd);
    224 #endif
    225 
    226 	/*
    227 	 * This is a normal, "slave" device, so
    228 	 * enforce initialized, exclusive open.
    229 	 */
    230 	if (sc->sc_type == RD_UNCONFIGURED)
    231 		return ENXIO;
    232 	if (sc->sc_flags & RD_ISOPEN)
    233 		return EBUSY;
    234 
    235 	return 0;
    236 }
    237 
    238 int rdclose(dev, flag, fmt, proc)
    239 	dev_t   dev;
    240 	int     flag, fmt;
    241 	struct proc *proc;
    242 {
    243 	int md, unit;
    244 	struct rd_softc *sc;
    245 
    246 	md = minor(dev);
    247 	unit = RD_UNIT(md);
    248 	sc = rdcd.cd_devs[unit];
    249 
    250 	if (RD_IS_CTRL(md))
    251 		return 0;
    252 
    253 	/* Normal device. */
    254 	sc->sc_flags = 0;
    255 
    256 	return 0;
    257 }
    258 
    259 int
    260 rdread(dev, uio)
    261 	dev_t		dev;
    262 	struct uio	*uio;
    263 {
    264 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
    265 }
    266 
    267 int
    268 rdwrite(dev, uio)
    269 	dev_t		dev;
    270 	struct uio	*uio;
    271 {
    272 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
    273 }
    274 
    275 /*
    276  * Handle I/O requests, either directly, or
    277  * by passing them to the server process.
    278  */
    279 void
    280 rdstrategy(bp)
    281 	struct buf *bp;
    282 {
    283 	int md, unit;
    284 	struct rd_softc *sc;
    285 	caddr_t addr;
    286 	size_t  off, xfer;
    287 
    288 	md = minor(bp->b_dev);
    289 	unit = RD_UNIT(md);
    290 	sc = rdcd.cd_devs[unit];
    291 
    292 	switch (sc->sc_type) {
    293 #if RAMDISK_SERVER
    294 	case RD_UMEM_SERVER:
    295 		/* Just add this job to the server's queue. */
    296 		bp->b_actf = sc->sc_buflist;
    297 		sc->sc_buflist = bp;
    298 		if (bp->b_actf == NULL) {
    299 			/* server queue was empty. */
    300 			wakeup((caddr_t)sc);
    301 			/* see rd_server_loop() */
    302 		}
    303 		/* no biodone in this case */
    304 		return;
    305 #endif	/* RAMDISK_SERVER */
    306 
    307 	case RD_KMEM_FIXED:
    308 	case RD_KMEM_ALLOCATED:
    309 		/* These are in kernel space.  Access directly. */
    310 		bp->b_resid = bp->b_bcount;
    311 		off = (bp->b_blkno << DEV_BSHIFT);
    312 		if (off >= sc->sc_size) {
    313 			if (bp->b_flags & B_READ)
    314 				break;	/* EOF */
    315 			goto set_eio;
    316 		}
    317 		xfer = bp->b_resid;
    318 		if (xfer > (sc->sc_size - off))
    319 			xfer = (sc->sc_size - off);
    320 		addr = sc->sc_addr + off;
    321 		if (bp->b_flags & B_READ)
    322 			bcopy(addr, bp->b_data, xfer);
    323 		else
    324 			bcopy(bp->b_data, addr, xfer);
    325 		bp->b_resid -= xfer;
    326 		break;
    327 
    328 	default:
    329 		bp->b_resid = bp->b_bcount;
    330 	set_eio:
    331 		bp->b_error = EIO;
    332 		bp->b_flags |= B_ERROR;
    333 		break;
    334 	}
    335 	biodone(bp);
    336 }
    337 
    338 int
    339 rdioctl(dev, cmd, data, flag, proc)
    340 	dev_t	dev;
    341 	u_long	cmd;
    342 	int		flag;
    343 	caddr_t	data;
    344 	struct proc	*proc;
    345 {
    346 	int md, unit;
    347 	struct rd_softc *sc;
    348 	struct rd_conf *urd;
    349 
    350 	md = minor(dev);
    351 	unit = RD_UNIT(md);
    352 	sc = rdcd.cd_devs[unit];
    353 
    354 	/* If this is not the control device, punt! */
    355 	if (RD_IS_CTRL(md) == 0)
    356 		return ENOTTY;
    357 
    358 	urd = (struct rd_conf *)data;
    359 	switch (cmd) {
    360 	case RD_GETCONF:
    361 		*urd = sc->sc_rd;
    362 		return 0;
    363 
    364 	case RD_SETCONF:
    365 		/* Can only set it once. */
    366 		if (sc->sc_type != RD_UNCONFIGURED)
    367 			break;
    368 		switch (urd->rd_type) {
    369 		case RD_KMEM_ALLOCATED:
    370 			return rd_ioctl_kalloc(sc, urd, proc);
    371 #if RAMDISK_SERVER
    372 		case RD_UMEM_SERVER:
    373 			return rd_ioctl_server(sc, urd, proc);
    374 #endif
    375 		default:
    376 			break;
    377 		}
    378 		break;
    379 	}
    380 	return EINVAL;
    381 }
    382 
    383 /*
    384  * Handle ioctl RD_SETCONF for (sc_type == RD_KMEM_ALLOCATED)
    385  * Just allocate some kernel memory and return.
    386  */
    387 int
    388 rd_ioctl_kalloc(sc, urd, proc)
    389 	struct rd_softc *sc;
    390 	struct rd_conf *urd;
    391 	struct proc	*proc;
    392 {
    393 	vm_offset_t addr;
    394 	vm_size_t  size;
    395 
    396 	/* Sanity check the size. */
    397 	size = urd->rd_size;
    398 	if (size > RD_KMEM_MAX_SIZE)
    399 		return EINVAL;
    400 	addr = kmem_alloc(kernel_map, size);
    401 	if (!addr)
    402 		return ENOMEM;
    403 
    404 	/* This unit is now configured. */
    405 	sc->sc_addr = (caddr_t)addr; 	/* kernel space */
    406 	sc->sc_size = (size_t)size;
    407 	sc->sc_type = RD_KMEM_ALLOCATED;
    408 	return 0;
    409 }
    410 
    411 #if RAMDISK_SERVER
    412 
    413 /*
    414  * Handle ioctl RD_SETCONF for (sc_type == RD_UMEM_SERVER)
    415  * Set config, then become the I/O server for this unit.
    416  */
    417 int
    418 rd_ioctl_server(sc, urd, proc)
    419 	struct rd_softc *sc;
    420 	struct rd_conf *urd;
    421 	struct proc	*proc;
    422 {
    423 	vm_offset_t end;
    424 	int error;
    425 
    426 	/* Sanity check addr, size. */
    427 	end = (vm_offset_t) (urd->rd_addr + urd->rd_size);
    428 
    429 	if ((end >= VM_MAXUSER_ADDRESS) ||
    430 		(end < ((vm_offset_t) urd->rd_addr)) )
    431 		return EINVAL;
    432 
    433 	/* This unit is now configured. */
    434 	sc->sc_addr = urd->rd_addr; 	/* user space */
    435 	sc->sc_size = urd->rd_size;
    436 	sc->sc_type = RD_UMEM_SERVER;
    437 
    438 	/* Become the server daemon */
    439 	error = rd_server_loop(sc);
    440 
    441 	/* This server is now going away! */
    442 	sc->sc_type = RD_UNCONFIGURED;
    443 	sc->sc_addr = 0;
    444 	sc->sc_size = 0;
    445 
    446 	return (error);
    447 }
    448 
    449 int	rd_sleep_pri = PWAIT | PCATCH;
    450 
    451 static int
    452 rd_server_loop(sc)
    453 	struct rd_softc *sc;
    454 {
    455 	struct buf *bp;
    456 	caddr_t addr;	/* user space address */
    457 	size_t  off;	/* offset into "device" */
    458 	size_t  xfer;	/* amount to transfer */
    459 	int error;
    460 
    461 	for (;;) {
    462 		/* Wait for some work to arrive. */
    463 		while (sc->sc_buflist == NULL) {
    464 			error = tsleep((caddr_t)sc, rd_sleep_pri, "rd_idle", 0);
    465 			if (error)
    466 				return error;
    467 		}
    468 
    469 		/* Unlink buf from head of list. */
    470 		bp = sc->sc_buflist;
    471 		sc->sc_buflist = bp->b_actf;
    472 		bp->b_actf = NULL;
    473 
    474 		/* Do the transfer to/from user space. */
    475 		error = 0;
    476 		bp->b_resid = bp->b_bcount;
    477 		off = (bp->b_blkno << DEV_BSHIFT);
    478 		if (off >= sc->sc_size) {
    479 			if (bp->b_flags & B_READ)
    480 				goto done;	/* EOF (not an error) */
    481 			error = EIO;
    482 			goto done;
    483 		}
    484 		xfer = bp->b_resid;
    485 		if (xfer > (sc->sc_size - off))
    486 			xfer = (sc->sc_size - off);
    487 		addr = sc->sc_addr + off;
    488 		if (bp->b_flags & B_READ)
    489 			error = copyin(addr, bp->b_data, xfer);
    490 		else
    491 			error = copyout(bp->b_data, addr, xfer);
    492 		if (!error)
    493 			bp->b_resid -= xfer;
    494 
    495 	done:
    496 		if (error) {
    497 			bp->b_error = error;
    498 			bp->b_flags |= B_ERROR;
    499 		}
    500 		biodone(bp);
    501 	}
    502 }
    503 
    504 #endif	/* RAMDISK_SERVER */
    505