Home | History | Annotate | Line # | Download | only in dev
md.c revision 1.1
      1 /*	$NetBSD: md.c,v 1.1 1995/10/08 23:30:57 gwr 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 
     53 #include <vm/vm.h>
     54 #include <vm/vm_kern.h>
     55 /* Don't want all those other VM headers... */
     56 extern vm_offset_t	 kmem_alloc __P((vm_map_t, vm_size_t));
     57 
     58 #include <dev/ramdisk.h>
     59 
     60 /*
     61  * By default, include the user-space functionality.
     62  * Use:  option RAMDISK_SERVER=0 to turn it off.
     63  */
     64 #ifndef RAMDISK_SERVER
     65 #define	RAMDISK_SERVER 1
     66 #endif
     67 
     68 /*
     69  * XXX: the "control" unit is (base unit + 16).
     70  * We should just use the cdev as the "control", but
     71  * that interferes with the security stuff preventing
     72  * simulatneous use of raw and block devices.
     73  *
     74  * XXX Assumption: 16 RAM-disks are enough!
     75  */
     76 #define RD_IS_CTRL(unit) (unit & 0x10)
     77 #define RD_UNIT(unit)    (unit &  0xF)
     78 
     79 /*
     80  * XXX -  This is just for a sanity check.  Only
     81  * applies to kernel-space RAM disk allocations.
     82  */
     83 #define RD_KMEM_MAX_SIZE	0x100000	/* 1MB */
     84 
     85 /* autoconfig stuff... */
     86 
     87 struct rd_softc {
     88 	struct device sc_dev;	/* REQUIRED first entry */
     89 	struct rd_conf sc_rd;
     90 	struct buf *sc_buflist;
     91 	int sc_flags;
     92 };
     93 /* shorthand for fields in sc_rd: */
     94 #define sc_addr sc_rd.rd_addr
     95 #define sc_size sc_rd.rd_size
     96 #define sc_type sc_rd.rd_type
     97 /* flags */
     98 #define RD_ISOPEN	0x01
     99 #define RD_SERVED	0x02
    100 
    101 static int  rd_match (struct device *, void *self, void *);
    102 static void rd_attach(struct device *, struct device *self, void *);
    103 
    104 struct cfdriver rdcd = {
    105 	NULL, "rd", rd_match, rd_attach,
    106 	DV_DISK, sizeof(struct device), NULL, 0 };
    107 
    108 static int
    109 rd_match(parent, self, aux)
    110 	struct device	*parent;
    111 	void	*self;
    112 	void	*aux;
    113 {
    114 	return(1);
    115 }
    116 
    117 static void
    118 rd_attach(parent, self, aux)
    119 	struct device	*parent, *self;
    120 	void		*aux;
    121 {
    122 	struct rd_softc *sc = (struct rd_softc *)self;
    123 
    124 	/* XXX - Could accept aux info here to set the config. */
    125 #ifdef	RAMDISK_HOOKS
    126 	/*
    127 	 * This external function might setup a pre-loaded disk.
    128 	 * All it would need to do is setup the rd_conf struct.
    129 	 * See sys/arch/sun3/dev/rd_root.c for an example.
    130 	 */
    131 	rd_attach_hook(sc->sc_dev.dv_unit, &sc->sc_rd);
    132 #endif
    133 	printf("\n");
    134 }
    135 
    136 /*
    137  * operational routines:
    138  * open, close, read, write, strategy,
    139  * ioctl, dump, size
    140  */
    141 
    142 void rdstrategy __P((struct buf *bp));
    143 
    144 #if RAMDISK_SERVER
    145 static int rd_server_loop __P((struct rd_softc *sc));
    146 static int rd_ioctl_server __P((struct rd_softc *sc,
    147 		struct rd_conf *urd, struct proc *proc));
    148 #endif
    149 
    150 int rddump(dev, blkno, va, size)
    151 	dev_t dev;
    152 	daddr_t blkno;
    153 	caddr_t va;
    154 	size_t size;
    155 {
    156 	return ENODEV;
    157 }
    158 
    159 int rdsize(dev_t dev)
    160 {
    161 	int unit;
    162 	struct rd_softc *sc;
    163 
    164 	/* Disallow control units. */
    165 	unit = minor(dev);
    166 	if (unit >= rdcd.cd_ndevs)
    167 		return 0;
    168 	sc = rdcd.cd_devs[unit];
    169 	if (sc == NULL)
    170 		return 0;
    171 
    172 	if (sc->sc_type == RD_UNCONFIGURED)
    173 		return 0;
    174 
    175 	return (sc->sc_size >> DEV_BSHIFT);
    176 }
    177 
    178 int rdopen(dev, flag, fmt, proc)
    179 	dev_t   dev;
    180 	int     flag, fmt;
    181 	struct proc *proc;
    182 {
    183 	int md, unit;
    184 	struct rd_softc *sc;
    185 
    186 	md = minor(dev);
    187 	unit = RD_UNIT(md);
    188 	if (unit >= rdcd.cd_ndevs)
    189 		return ENXIO;
    190 	sc = rdcd.cd_devs[unit];
    191 	if (sc == NULL)
    192 		return ENXIO;
    193 
    194 	/*
    195 	 * The control device is not exclusive, and can
    196 	 * open uninitialized units (so you can setconf).
    197 	 */
    198 	if (RD_IS_CTRL(md))
    199 		return 0;
    200 
    201 #ifdef	RAMDISK_HOOKS
    202 	/* Call the open hook to allow loading the device. */
    203 	rd_open_hook(unit, &sc->sc_rd);
    204 #endif
    205 
    206 	/*
    207 	 * This is a normal, "slave" device, so
    208 	 * enforce initialized, exclusive open.
    209 	 */
    210 	if (sc->sc_type == RD_UNCONFIGURED)
    211 		return ENXIO;
    212 	if (sc->sc_flags & RD_ISOPEN)
    213 		return EBUSY;
    214 
    215 	return 0;
    216 }
    217 
    218 int rdclose(dev, flag, fmt, proc)
    219 	dev_t   dev;
    220 	int     flag, fmt;
    221 	struct proc *proc;
    222 {
    223 	int md, unit;
    224 	struct rd_softc *sc;
    225 
    226 	md = minor(dev);
    227 	unit = RD_UNIT(md);
    228 	sc = rdcd.cd_devs[unit];
    229 
    230 	if (RD_IS_CTRL(md))
    231 		return 0;
    232 
    233 	/* Normal device. */
    234 	sc->sc_flags = 0;
    235 
    236 	return 0;
    237 }
    238 
    239 int
    240 rdread(dev, uio)
    241 	dev_t		dev;
    242 	struct uio	*uio;
    243 {
    244 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
    245 }
    246 
    247 int
    248 rdwrite(dev, uio)
    249 	dev_t		dev;
    250 	struct uio	*uio;
    251 {
    252 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
    253 }
    254 
    255 /*
    256  * Handle I/O requests, either directly, or
    257  * by passing them to the server process.
    258  */
    259 void
    260 rdstrategy(bp)
    261 	struct buf *bp;
    262 {
    263 	int md, unit;
    264 	struct rd_softc *sc;
    265 	caddr_t addr;
    266 	size_t  off, xfer;
    267 
    268 	md = minor(bp->b_dev);
    269 	unit = RD_UNIT(md);
    270 	sc = rdcd.cd_devs[unit];
    271 
    272 	switch (sc->sc_type) {
    273 #if RAMDISK_SERVER
    274 	case RD_UMEM_SERVER:
    275 		/* Just add this job to the server's queue. */
    276 		bp->b_actf = sc->sc_buflist;
    277 		sc->sc_buflist = bp;
    278 		if (bp->b_actf == NULL) {
    279 			/* server queue was empty. */
    280 			wakeup((caddr_t)sc);
    281 			/* see rd_server_loop() */
    282 		}
    283 		/* no biodone in this case */
    284 		return;
    285 #endif	/* RAMDISK_SERVER */
    286 
    287 	case RD_KMEM_FIXED:
    288 	case RD_KMEM_ALLOCATED:
    289 		/* These are in kernel space.  Access directly. */
    290 		bp->b_resid = bp->b_bcount;
    291 		off = (bp->b_blkno << DEV_BSHIFT);
    292 		if (off >= sc->sc_size) {
    293 			if (bp->b_flags & B_READ)
    294 				break;	/* EOF */
    295 			goto set_eio;
    296 		}
    297 		xfer = bp->b_resid;
    298 		if (xfer > (sc->sc_size - off))
    299 			xfer = (sc->sc_size - off);
    300 		addr = sc->sc_addr + off;
    301 		if (bp->b_flags & B_READ)
    302 			bcopy(addr, bp->b_data, xfer);
    303 		else
    304 			bcopy(bp->b_data, addr, xfer);
    305 		bp->b_resid -= xfer;
    306 		break;
    307 
    308 	default:
    309 		bp->b_resid = bp->b_bcount;
    310 	set_eio:
    311 		bp->b_error = EIO;
    312 		bp->b_flags |= B_ERROR;
    313 		break;
    314 	}
    315 	biodone(bp);
    316 }
    317 
    318 int
    319 rdioctl(dev, cmd, data, flag, proc)
    320 	dev_t	dev;
    321 	u_long	cmd;
    322 	int		flag;
    323 	caddr_t	data;
    324 	struct proc	*proc;
    325 {
    326 	int md, unit;
    327 	struct rd_softc *sc;
    328 	struct rd_conf *urd;
    329 
    330 	md = minor(dev);
    331 	unit = RD_UNIT(md);
    332 	sc = rdcd.cd_devs[unit];
    333 
    334 	/* If this is not the control device, punt! */
    335 	if (RD_IS_CTRL(md) == 0)
    336 		return ENOTTY;
    337 
    338 	urd = (struct rd_conf *)data;
    339 	switch (cmd) {
    340 	case RD_GETCONF:
    341 		*urd = sc->sc_rd;
    342 		return 0;
    343 
    344 	case RD_SETCONF:
    345 		/* Can only set it once. */
    346 		if (sc->sc_type != RD_UNCONFIGURED)
    347 			break;
    348 		switch (urd->rd_type) {
    349 		case RD_KMEM_ALLOCATED:
    350 			return rd_ioctl_kalloc(sc, urd, proc);
    351 #if RAMDISK_SERVER
    352 		case RD_UMEM_SERVER:
    353 			return rd_ioctl_server(sc, urd, proc);
    354 #endif
    355 		default:
    356 			break;
    357 		}
    358 		break;
    359 	}
    360 	return EINVAL;
    361 }
    362 
    363 /*
    364  * Handle ioctl RD_SETCONF for (sc_type == RD_KMEM_ALLOCATED)
    365  * Just allocate some kernel memory and return.
    366  */
    367 int
    368 rd_ioctl_kalloc(sc, urd, proc)
    369 	struct rd_softc *sc;
    370 	struct rd_conf *urd;
    371 	struct proc	*proc;
    372 {
    373 	vm_offset_t addr;
    374 	vm_size_t  size;
    375 
    376 	/* Sanity check the size. */
    377 	size = urd->rd_size;
    378 	if (size > RD_KMEM_MAX_SIZE)
    379 		return EINVAL;
    380 	addr = kmem_alloc(kernel_map, size);
    381 	if (!addr)
    382 		return ENOMEM;
    383 
    384 	/* This unit is now configured. */
    385 	sc->sc_addr = (caddr_t)addr; 	/* kernel space */
    386 	sc->sc_size = (size_t)size;
    387 	sc->sc_type = RD_KMEM_ALLOCATED;
    388 	return 0;
    389 }
    390 
    391 #if RAMDISK_SERVER
    392 
    393 /*
    394  * Handle ioctl RD_SETCONF for (sc_type == RD_KMEM_ALLOCATED)
    395  * Set config, then become the I/O server for this unit.
    396  */
    397 int
    398 rd_ioctl_server(sc, urd, proc)
    399 	struct rd_softc *sc;
    400 	struct rd_conf *urd;
    401 	struct proc	*proc;
    402 {
    403 	vm_offset_t end;
    404 	int error;
    405 
    406 	/* Sanity check addr, size. */
    407 	end = (vm_offset_t) (urd->rd_addr + urd->rd_size);
    408 
    409 	if ((end >= VM_MAXUSER_ADDRESS) ||
    410 		(end < ((vm_offset_t) urd->rd_addr)) )
    411 		return EINVAL;
    412 
    413 	/* This unit is now configured. */
    414 	sc->sc_addr = urd->rd_addr; 	/* user space */
    415 	sc->sc_size = urd->rd_size;
    416 	sc->sc_type = RD_UMEM_SERVER;
    417 
    418 	/* Become the server daemon */
    419 	error = rd_server_loop(sc);
    420 
    421 	/* This server is now going away! */
    422 	sc->sc_type = RD_UNCONFIGURED;
    423 	sc->sc_addr = 0;
    424 	sc->sc_size = 0;
    425 
    426 	return (error);
    427 }
    428 
    429 int	rd_sleep_pri = PWAIT | PCATCH;
    430 
    431 static int
    432 rd_server_loop(sc)
    433 	struct rd_softc *sc;
    434 {
    435 	struct buf *bp;
    436 	caddr_t addr;	/* user space address */
    437 	size_t  off;	/* offset into "device" */
    438 	size_t  xfer;	/* amount to transfer */
    439 	int error;
    440 
    441 	for (;;) {
    442 		/* Wait for some work to arrive. */
    443 		while (sc->sc_buflist == NULL) {
    444 			error = tsleep((caddr_t)sc, rd_sleep_pri, "rd_idle", 0);
    445 			if (error)
    446 				return error;
    447 		}
    448 
    449 		/* Unlink buf from head of list. */
    450 		bp = sc->sc_buflist;
    451 		sc->sc_buflist = bp->b_actf;
    452 		bp->b_actf = NULL;
    453 
    454 		/* Do the transfer to/from user space. */
    455 		error = 0;
    456 		bp->b_resid = bp->b_bcount;
    457 		off = (bp->b_blkno << DEV_BSHIFT);
    458 		if (off >= sc->sc_size) {
    459 			if (bp->b_flags & B_READ)
    460 				goto done;	/* EOF (not an error) */
    461 			error = EIO;
    462 			goto done;
    463 		}
    464 		xfer = bp->b_resid;
    465 		if (xfer > (sc->sc_size - off))
    466 			xfer = (sc->sc_size - off);
    467 		addr = sc->sc_addr + off;
    468 		if (bp->b_flags & B_READ)
    469 			error = copyin(addr, bp->b_data, xfer);
    470 		else
    471 			error = copyout(bp->b_data, addr, xfer);
    472 		if (!error)
    473 			bp->b_resid -= xfer;
    474 
    475 	done:
    476 		if (error) {
    477 			bp->b_error = error;
    478 			bp->b_flags |= B_ERROR;
    479 		}
    480 		biodone(bp);
    481 	}
    482 }
    483 
    484 #endif	/* RAMDISK_SERVER */
    485