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