md.c revision 1.6 1 /* $NetBSD: md.c,v 1.6 1996/03/17 00:43:52 thorpej 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 cfattach rd_ca = {
107 sizeof(struct rd_softc), rd_match, rd_attach
108 };
109
110 struct cfdriver rd_cd = {
111 NULL, "rd", DV_DULL, NULL, 0
112 };
113
114 void rdstrategy __P((struct buf *bp));
115
116 struct dkdriver rddkdriver = { rdstrategy };
117
118 static int
119 rd_match(parent, self, aux)
120 struct device *parent;
121 void *self;
122 void *aux;
123 {
124 #ifdef RAMDISK_HOOKS
125 /*
126 * This external function allows for a machine dependent
127 * match function.
128 */
129 return (rd_match_hook(parent, self, aux));
130 #else
131 return(1);
132 #endif
133 }
134
135 static void
136 rd_attach(parent, self, aux)
137 struct device *parent, *self;
138 void *aux;
139 {
140 struct rd_softc *sc = (struct rd_softc *)self;
141
142 /* XXX - Could accept aux info here to set the config. */
143 #ifdef RAMDISK_HOOKS
144 /*
145 * This external function might setup a pre-loaded disk.
146 * All it would need to do is setup the rd_conf struct.
147 * See sys/arch/sun3/dev/rd_root.c for an example.
148 */
149 rd_attach_hook(sc->sc_dev.dv_unit, &sc->sc_rd);
150 #endif
151 printf("\n");
152
153 /*
154 * Initialize and attach the disk structure.
155 */
156 bzero(&sc->sc_dkdev, sizeof(sc->sc_dkdev));
157 sc->sc_dkdev.dk_driver = &rddkdriver;
158 sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
159 disk_attach(&sc->sc_dkdev);
160 }
161
162 /*
163 * operational routines:
164 * open, close, read, write, strategy,
165 * ioctl, dump, size
166 */
167
168 #if RAMDISK_SERVER
169 static int rd_server_loop __P((struct rd_softc *sc));
170 static int rd_ioctl_server __P((struct rd_softc *sc,
171 struct rd_conf *urd, struct proc *proc));
172 #endif
173
174 int rddump(dev, blkno, va, size)
175 dev_t dev;
176 daddr_t blkno;
177 caddr_t va;
178 size_t size;
179 {
180 return ENODEV;
181 }
182
183 int rdsize(dev_t dev)
184 {
185 int unit;
186 struct rd_softc *sc;
187
188 /* Disallow control units. */
189 unit = minor(dev);
190 if (unit >= rd_cd.cd_ndevs)
191 return 0;
192 sc = rd_cd.cd_devs[unit];
193 if (sc == NULL)
194 return 0;
195
196 if (sc->sc_type == RD_UNCONFIGURED)
197 return 0;
198
199 return (sc->sc_size >> DEV_BSHIFT);
200 }
201
202 int rdopen(dev, flag, fmt, proc)
203 dev_t dev;
204 int flag, fmt;
205 struct proc *proc;
206 {
207 int md, unit;
208 struct rd_softc *sc;
209
210 md = minor(dev);
211 unit = RD_UNIT(md);
212 if (unit >= rd_cd.cd_ndevs)
213 return ENXIO;
214 sc = rd_cd.cd_devs[unit];
215 if (sc == NULL)
216 return ENXIO;
217
218 /*
219 * The control device is not exclusive, and can
220 * open uninitialized units (so you can setconf).
221 */
222 if (RD_IS_CTRL(md))
223 return 0;
224
225 #ifdef RAMDISK_HOOKS
226 /* Call the open hook to allow loading the device. */
227 rd_open_hook(unit, &sc->sc_rd);
228 #endif
229
230 /*
231 * This is a normal, "slave" device, so
232 * enforce initialized, exclusive open.
233 */
234 if (sc->sc_type == RD_UNCONFIGURED)
235 return ENXIO;
236 if (sc->sc_flags & RD_ISOPEN)
237 return EBUSY;
238
239 return 0;
240 }
241
242 int rdclose(dev, flag, fmt, proc)
243 dev_t dev;
244 int flag, fmt;
245 struct proc *proc;
246 {
247 int md, unit;
248 struct rd_softc *sc;
249
250 md = minor(dev);
251 unit = RD_UNIT(md);
252 sc = rd_cd.cd_devs[unit];
253
254 if (RD_IS_CTRL(md))
255 return 0;
256
257 /* Normal device. */
258 sc->sc_flags = 0;
259
260 return 0;
261 }
262
263 int
264 rdread(dev, uio)
265 dev_t dev;
266 struct uio *uio;
267 {
268 return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
269 }
270
271 int
272 rdwrite(dev, uio)
273 dev_t dev;
274 struct uio *uio;
275 {
276 return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
277 }
278
279 /*
280 * Handle I/O requests, either directly, or
281 * by passing them to the server process.
282 */
283 void
284 rdstrategy(bp)
285 struct buf *bp;
286 {
287 int md, unit;
288 struct rd_softc *sc;
289 caddr_t addr;
290 size_t off, xfer;
291
292 md = minor(bp->b_dev);
293 unit = RD_UNIT(md);
294 sc = rd_cd.cd_devs[unit];
295
296 switch (sc->sc_type) {
297 #if RAMDISK_SERVER
298 case RD_UMEM_SERVER:
299 /* Just add this job to the server's queue. */
300 bp->b_actf = sc->sc_buflist;
301 sc->sc_buflist = bp;
302 if (bp->b_actf == NULL) {
303 /* server queue was empty. */
304 wakeup((caddr_t)sc);
305 /* see rd_server_loop() */
306 }
307 /* no biodone in this case */
308 return;
309 #endif /* RAMDISK_SERVER */
310
311 case RD_KMEM_FIXED:
312 case RD_KMEM_ALLOCATED:
313 /* These are in kernel space. Access directly. */
314 bp->b_resid = bp->b_bcount;
315 off = (bp->b_blkno << DEV_BSHIFT);
316 if (off >= sc->sc_size) {
317 if (bp->b_flags & B_READ)
318 break; /* EOF */
319 goto set_eio;
320 }
321 xfer = bp->b_resid;
322 if (xfer > (sc->sc_size - off))
323 xfer = (sc->sc_size - off);
324 addr = sc->sc_addr + off;
325 if (bp->b_flags & B_READ)
326 bcopy(addr, bp->b_data, xfer);
327 else
328 bcopy(bp->b_data, addr, xfer);
329 bp->b_resid -= xfer;
330 break;
331
332 default:
333 bp->b_resid = bp->b_bcount;
334 set_eio:
335 bp->b_error = EIO;
336 bp->b_flags |= B_ERROR;
337 break;
338 }
339 biodone(bp);
340 }
341
342 int
343 rdioctl(dev, cmd, data, flag, proc)
344 dev_t dev;
345 u_long cmd;
346 int flag;
347 caddr_t data;
348 struct proc *proc;
349 {
350 int md, unit;
351 struct rd_softc *sc;
352 struct rd_conf *urd;
353
354 md = minor(dev);
355 unit = RD_UNIT(md);
356 sc = rd_cd.cd_devs[unit];
357
358 /* If this is not the control device, punt! */
359 if (RD_IS_CTRL(md) == 0)
360 return ENOTTY;
361
362 urd = (struct rd_conf *)data;
363 switch (cmd) {
364 case RD_GETCONF:
365 *urd = sc->sc_rd;
366 return 0;
367
368 case RD_SETCONF:
369 /* Can only set it once. */
370 if (sc->sc_type != RD_UNCONFIGURED)
371 break;
372 switch (urd->rd_type) {
373 case RD_KMEM_ALLOCATED:
374 return rd_ioctl_kalloc(sc, urd, proc);
375 #if RAMDISK_SERVER
376 case RD_UMEM_SERVER:
377 return rd_ioctl_server(sc, urd, proc);
378 #endif
379 default:
380 break;
381 }
382 break;
383 }
384 return EINVAL;
385 }
386
387 /*
388 * Handle ioctl RD_SETCONF for (sc_type == RD_KMEM_ALLOCATED)
389 * Just allocate some kernel memory and return.
390 */
391 int
392 rd_ioctl_kalloc(sc, urd, proc)
393 struct rd_softc *sc;
394 struct rd_conf *urd;
395 struct proc *proc;
396 {
397 vm_offset_t addr;
398 vm_size_t size;
399
400 /* Sanity check the size. */
401 size = urd->rd_size;
402 if (size > RD_KMEM_MAX_SIZE)
403 return EINVAL;
404 addr = kmem_alloc(kernel_map, size);
405 if (!addr)
406 return ENOMEM;
407
408 /* This unit is now configured. */
409 sc->sc_addr = (caddr_t)addr; /* kernel space */
410 sc->sc_size = (size_t)size;
411 sc->sc_type = RD_KMEM_ALLOCATED;
412 return 0;
413 }
414
415 #if RAMDISK_SERVER
416
417 /*
418 * Handle ioctl RD_SETCONF for (sc_type == RD_UMEM_SERVER)
419 * Set config, then become the I/O server for this unit.
420 */
421 int
422 rd_ioctl_server(sc, urd, proc)
423 struct rd_softc *sc;
424 struct rd_conf *urd;
425 struct proc *proc;
426 {
427 vm_offset_t end;
428 int error;
429
430 /* Sanity check addr, size. */
431 end = (vm_offset_t) (urd->rd_addr + urd->rd_size);
432
433 if ((end >= VM_MAXUSER_ADDRESS) ||
434 (end < ((vm_offset_t) urd->rd_addr)) )
435 return EINVAL;
436
437 /* This unit is now configured. */
438 sc->sc_addr = urd->rd_addr; /* user space */
439 sc->sc_size = urd->rd_size;
440 sc->sc_type = RD_UMEM_SERVER;
441
442 /* Become the server daemon */
443 error = rd_server_loop(sc);
444
445 /* This server is now going away! */
446 sc->sc_type = RD_UNCONFIGURED;
447 sc->sc_addr = 0;
448 sc->sc_size = 0;
449
450 return (error);
451 }
452
453 int rd_sleep_pri = PWAIT | PCATCH;
454
455 static int
456 rd_server_loop(sc)
457 struct rd_softc *sc;
458 {
459 struct buf *bp;
460 caddr_t addr; /* user space address */
461 size_t off; /* offset into "device" */
462 size_t xfer; /* amount to transfer */
463 int error;
464
465 for (;;) {
466 /* Wait for some work to arrive. */
467 while (sc->sc_buflist == NULL) {
468 error = tsleep((caddr_t)sc, rd_sleep_pri, "rd_idle", 0);
469 if (error)
470 return error;
471 }
472
473 /* Unlink buf from head of list. */
474 bp = sc->sc_buflist;
475 sc->sc_buflist = bp->b_actf;
476 bp->b_actf = NULL;
477
478 /* Do the transfer to/from user space. */
479 error = 0;
480 bp->b_resid = bp->b_bcount;
481 off = (bp->b_blkno << DEV_BSHIFT);
482 if (off >= sc->sc_size) {
483 if (bp->b_flags & B_READ)
484 goto done; /* EOF (not an error) */
485 error = EIO;
486 goto done;
487 }
488 xfer = bp->b_resid;
489 if (xfer > (sc->sc_size - off))
490 xfer = (sc->sc_size - off);
491 addr = sc->sc_addr + off;
492 if (bp->b_flags & B_READ)
493 error = copyin(addr, bp->b_data, xfer);
494 else
495 error = copyout(bp->b_data, addr, xfer);
496 if (!error)
497 bp->b_resid -= xfer;
498
499 done:
500 if (error) {
501 bp->b_error = error;
502 bp->b_flags |= B_ERROR;
503 }
504 biodone(bp);
505 }
506 }
507
508 #endif /* RAMDISK_SERVER */
509