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