md.c revision 1.15 1 /* $NetBSD: md.c,v 1.15 1998/02/05 07:59:37 mrg 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 md.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 #include <sys/disklabel.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_extern.h>
62
63 #include <dev/md.h>
64
65 /*
66 * By default, include the user-space functionality.
67 * Use `options MEMORY_DISK_SERVER=0' to turn it off.
68 */
69 #ifndef MEMORY_DISK_SERVER
70 #define MEMORY_DISK_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 memory-disks are enough!
80 */
81 #define MD_MAX_UNITS 0x10
82 #define MD_IS_CTRL(unit) (unit & 0x10)
83 #define MD_UNIT(unit) (unit & 0xF)
84
85 /* autoconfig stuff... */
86
87 struct md_softc {
88 struct device sc_dev; /* REQUIRED first entry */
89 struct disk sc_dkdev; /* hook for generic disk handling */
90 struct md_conf sc_md;
91 struct buf *sc_buflist;
92 int sc_flags;
93 };
94 /* shorthand for fields in sc_md: */
95 #define sc_addr sc_md.md_addr
96 #define sc_size sc_md.md_size
97 #define sc_type sc_md.md_type
98 /* flags */
99 #define MD_ISOPEN 0x01
100 #define MD_SERVED 0x02
101
102 void mdattach __P((int));
103 static void md_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 md_cd structure. It is preserved
108 * (for now) to remain compatible with such practice.
109 * XXX - that practice is questionable...
110 */
111 struct cfdriver md_cd = {
112 NULL, "md", DV_DULL, NULL, 0
113 };
114
115 void mdstrategy __P((struct buf *bp));
116 struct dkdriver mddkdriver = { mdstrategy };
117
118 static int ramdisk_ndevs;
119 static void *ramdisk_devs[MD_MAX_UNITS];
120
121 /*
122 * This is called if we are configured as a pseudo-device
123 */
124 void
125 mdattach(n)
126 int n;
127 {
128 struct md_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 > MD_MAX_UNITS)
142 n = MD_MAX_UNITS;
143 ramdisk_ndevs = n;
144
145 /* XXX: Fake-up md_cd (see above) */
146 md_cd.cd_ndevs = ramdisk_ndevs;
147 md_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_NOWAIT);
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, "md%d", i);
161 md_attach(NULL, &sc->sc_dev, NULL);
162 }
163 }
164
165 static void
166 md_attach(parent, self, aux)
167 struct device *parent, *self;
168 void *aux;
169 {
170 struct md_softc *sc = (struct md_softc *)self;
171
172 /* XXX - Could accept aux info here to set the config. */
173 #ifdef MEMORY_DISK_HOOKS
174 /*
175 * This external function might setup a pre-loaded disk.
176 * All it would need to do is setup the md_conf struct.
177 * See sys/arch/sun3/dev/md_root.c for an example.
178 */
179 md_attach_hook(sc->sc_dev.dv_unit, &sc->sc_md);
180 #endif
181
182 /*
183 * Initialize and attach the disk structure.
184 */
185 sc->sc_dkdev.dk_driver = &mddkdriver;
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 MEMORY_DISK_SERVER
197 static int md_server_loop __P((struct md_softc *sc));
198 static int md_ioctl_server __P((struct md_softc *sc,
199 struct md_conf *umd, struct proc *proc));
200 #endif
201 static int md_ioctl_kalloc __P((struct md_softc *sc,
202 struct md_conf *umd, struct proc *proc));
203
204 dev_type_open(mdopen);
205 dev_type_close(mdclose);
206 dev_type_read(mdread);
207 dev_type_write(mdwrite);
208 dev_type_ioctl(mdioctl);
209 dev_type_size(mdsize);
210 dev_type_dump(mddump);
211
212 int mddump(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 mdsize(dev_t dev)
222 {
223 int unit;
224 struct md_softc *sc;
225
226 /* Disallow control units. */
227 unit = DISKUNIT(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 == MD_UNCONFIGURED)
235 return 0;
236
237 return (sc->sc_size >> DEV_BSHIFT);
238 }
239
240 int
241 mdopen(dev, flag, fmt, proc)
242 dev_t dev;
243 int flag, fmt;
244 struct proc *proc;
245 {
246 int md, unit;
247 struct md_softc *sc;
248
249 md = DISKUNIT(dev);
250 unit = MD_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 (MD_IS_CTRL(md))
262 return 0;
263
264 #ifdef MEMORY_DISK_HOOKS
265 /* Call the open hook to allow loading the device. */
266 md_open_hook(unit, &sc->sc_md);
267 #endif
268
269 /*
270 * This is a normal, "slave" device, so
271 * enforce initialized, exclusive open.
272 */
273 if (sc->sc_type == MD_UNCONFIGURED)
274 return ENXIO;
275 if (sc->sc_flags & MD_ISOPEN)
276 return EBUSY;
277
278 return 0;
279 }
280
281 int
282 mdclose(dev, flag, fmt, proc)
283 dev_t dev;
284 int flag, fmt;
285 struct proc *proc;
286 {
287 int md, unit;
288 struct md_softc *sc;
289
290 md = DISKUNIT(dev);
291 unit = MD_UNIT(md);
292 sc = ramdisk_devs[unit];
293
294 if (MD_IS_CTRL(md))
295 return 0;
296
297 /* Normal device. */
298 sc->sc_flags = 0;
299
300 return 0;
301 }
302
303 int
304 mdread(dev, uio, flags)
305 dev_t dev;
306 struct uio *uio;
307 int flags;
308 {
309 return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
310 }
311
312 int
313 mdwrite(dev, uio, flags)
314 dev_t dev;
315 struct uio *uio;
316 int flags;
317 {
318 return (physio(mdstrategy, 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 mdstrategy(bp)
327 struct buf *bp;
328 {
329 int md, unit;
330 struct md_softc *sc;
331 caddr_t addr;
332 size_t off, xfer;
333
334 md = DISKUNIT(bp->b_dev);
335 unit = MD_UNIT(md);
336 sc = ramdisk_devs[unit];
337
338 switch (sc->sc_type) {
339 #if MEMORY_DISK_SERVER
340 case MD_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 md_server_loop() */
348 }
349 /* no biodone in this case */
350 return;
351 #endif /* MEMORY_DISK_SERVER */
352
353 case MD_KMEM_FIXED:
354 case MD_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 mdioctl(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 md_softc *sc;
394 struct md_conf *umd;
395
396 md = DISKUNIT(dev);
397 unit = MD_UNIT(md);
398 sc = ramdisk_devs[unit];
399
400 /* If this is not the control device, punt! */
401 if (MD_IS_CTRL(md) == 0)
402 return ENOTTY;
403
404 umd = (struct md_conf *)data;
405 switch (cmd) {
406 case MD_GETCONF:
407 *umd = sc->sc_md;
408 return 0;
409
410 case MD_SETCONF:
411 /* Can only set it once. */
412 if (sc->sc_type != MD_UNCONFIGURED)
413 break;
414 switch (umd->md_type) {
415 case MD_KMEM_ALLOCATED:
416 return md_ioctl_kalloc(sc, umd, proc);
417 #if MEMORY_DISK_SERVER
418 case MD_UMEM_SERVER:
419 return md_ioctl_server(sc, umd, proc);
420 #endif
421 default:
422 break;
423 }
424 break;
425 }
426 return EINVAL;
427 }
428
429 /*
430 * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
431 * Just allocate some kernel memory and return.
432 */
433 static int
434 md_ioctl_kalloc(sc, umd, proc)
435 struct md_softc *sc;
436 struct md_conf *umd;
437 struct proc *proc;
438 {
439 vm_offset_t addr;
440 vm_size_t size;
441
442 /* Sanity check the size. */
443 size = umd->md_size;
444 #if defined(UVM)
445 addr = uvm_km_zalloc(kernel_map, size);
446 #else
447 addr = kmem_alloc(kernel_map, size);
448 #endif
449 if (!addr)
450 return ENOMEM;
451
452 /* This unit is now configured. */
453 sc->sc_addr = (caddr_t)addr; /* kernel space */
454 sc->sc_size = (size_t)size;
455 sc->sc_type = MD_KMEM_ALLOCATED;
456 return 0;
457 }
458
459 #if MEMORY_DISK_SERVER
460
461 /*
462 * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
463 * Set config, then become the I/O server for this unit.
464 */
465 static int
466 md_ioctl_server(sc, umd, proc)
467 struct md_softc *sc;
468 struct md_conf *umd;
469 struct proc *proc;
470 {
471 vm_offset_t end;
472 int error;
473
474 /* Sanity check addr, size. */
475 end = (vm_offset_t) (umd->md_addr + umd->md_size);
476
477 if ((end >= VM_MAXUSER_ADDRESS) ||
478 (end < ((vm_offset_t) umd->md_addr)) )
479 return EINVAL;
480
481 /* This unit is now configured. */
482 sc->sc_addr = umd->md_addr; /* user space */
483 sc->sc_size = umd->md_size;
484 sc->sc_type = MD_UMEM_SERVER;
485
486 /* Become the server daemon */
487 error = md_server_loop(sc);
488
489 /* This server is now going away! */
490 sc->sc_type = MD_UNCONFIGURED;
491 sc->sc_addr = 0;
492 sc->sc_size = 0;
493
494 return (error);
495 }
496
497 int md_sleep_pri = PWAIT | PCATCH;
498
499 static int
500 md_server_loop(sc)
501 struct md_softc *sc;
502 {
503 struct buf *bp;
504 caddr_t addr; /* user space address */
505 size_t off; /* offset into "device" */
506 size_t xfer; /* amount to transfer */
507 int error;
508
509 for (;;) {
510 /* Wait for some work to arrive. */
511 while (sc->sc_buflist == NULL) {
512 error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0);
513 if (error)
514 return error;
515 }
516
517 /* Unlink buf from head of list. */
518 bp = sc->sc_buflist;
519 sc->sc_buflist = bp->b_actf;
520 bp->b_actf = NULL;
521
522 /* Do the transfer to/from user space. */
523 error = 0;
524 bp->b_resid = bp->b_bcount;
525 off = (bp->b_blkno << DEV_BSHIFT);
526 if (off >= sc->sc_size) {
527 if (bp->b_flags & B_READ)
528 goto done; /* EOF (not an error) */
529 error = EIO;
530 goto done;
531 }
532 xfer = bp->b_resid;
533 if (xfer > (sc->sc_size - off))
534 xfer = (sc->sc_size - off);
535 addr = sc->sc_addr + off;
536 if (bp->b_flags & B_READ)
537 error = copyin(addr, bp->b_data, xfer);
538 else
539 error = copyout(bp->b_data, addr, xfer);
540 if (!error)
541 bp->b_resid -= xfer;
542
543 done:
544 if (error) {
545 bp->b_error = error;
546 bp->b_flags |= B_ERROR;
547 }
548 biodone(bp);
549 }
550 }
551 #endif /* MEMORY_DISK_SERVER */
552