md.c revision 1.52 1 /* $NetBSD: md.c,v 1.52 2008/04/09 05:47:19 cegger 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/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: md.c,v 1.52 2008/04/09 05:47:19 cegger Exp $");
50
51 #include "opt_md.h"
52
53 #include <sys/param.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/systm.h>
57 #include <sys/buf.h>
58 #include <sys/bufq.h>
59 #include <sys/device.h>
60 #include <sys/disk.h>
61 #include <sys/proc.h>
62 #include <sys/conf.h>
63 #include <sys/disklabel.h>
64
65 #include <uvm/uvm_extern.h>
66
67 #include <dev/md.h>
68
69 /*
70 * The user-space functionality is included by default.
71 * Use `options MEMORY_DISK_SERVER=0' to turn it off.
72 */
73 #ifndef MEMORY_DISK_SERVER
74 #error MEMORY_DISK_SERVER should be defined by opt_md.h
75 #endif /* MEMORY_DISK_SERVER */
76
77 /*
78 * We should use the raw partition for ioctl.
79 */
80 #define MD_MAX_UNITS 0x10
81 #define MD_UNIT(unit) DISKUNIT(unit)
82
83 /* autoconfig stuff... */
84
85 struct md_softc {
86 struct device sc_dev; /* REQUIRED first entry */
87 struct disk sc_dkdev; /* hook for generic disk handling */
88 struct md_conf sc_md;
89 struct bufq_state *sc_buflist;
90 };
91 /* shorthand for fields in sc_md: */
92 #define sc_addr sc_md.md_addr
93 #define sc_size sc_md.md_size
94 #define sc_type sc_md.md_type
95
96 void mdattach(int);
97
98 static void md_attach(struct device *, struct device *, void *);
99
100 static dev_type_open(mdopen);
101 static dev_type_close(mdclose);
102 static dev_type_read(mdread);
103 static dev_type_write(mdwrite);
104 static dev_type_ioctl(mdioctl);
105 static dev_type_strategy(mdstrategy);
106 static dev_type_size(mdsize);
107
108 const struct bdevsw md_bdevsw = {
109 mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK
110 };
111
112 const struct cdevsw md_cdevsw = {
113 mdopen, mdclose, mdread, mdwrite, mdioctl,
114 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
115 };
116
117 static struct dkdriver mddkdriver = { mdstrategy, NULL };
118
119 static int ramdisk_ndevs;
120 static void *ramdisk_devs[MD_MAX_UNITS];
121
122 /*
123 * This is called if we are configured as a pseudo-device
124 */
125 void
126 mdattach(int n)
127 {
128 struct md_softc *sc;
129 int i;
130
131 #ifdef DIAGNOSTIC
132 if (ramdisk_ndevs) {
133 aprint_error("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 /* Attach as if by autoconfig. */
146 for (i = 0; i < n; i++) {
147
148 sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT|M_ZERO);
149 if (!sc) {
150 aprint_error("ramdisk: malloc for attach failed!\n");
151 return;
152 }
153 ramdisk_devs[i] = sc;
154 sc->sc_dev.dv_unit = i;
155 snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname),
156 "md%d", i);
157 md_attach(NULL, &sc->sc_dev, NULL);
158 }
159 }
160
161 static void
162 md_attach(struct device *parent, struct device *self,
163 void *aux)
164 {
165 struct md_softc *sc = (struct md_softc *)self;
166
167 bufq_alloc(&sc->sc_buflist, "fcfs", 0);
168
169 /* XXX - Could accept aux info here to set the config. */
170 #ifdef MEMORY_DISK_HOOKS
171 /*
172 * This external function might setup a pre-loaded disk.
173 * All it would need to do is setup the md_conf struct.
174 * See sys/dev/md_root.c for an example.
175 */
176 md_attach_hook(device_unit(&sc->sc_dev), &sc->sc_md);
177 #endif
178
179 /*
180 * Initialize and attach the disk structure.
181 */
182 disk_init(&sc->sc_dkdev, device_xname(&sc->sc_dev), &mddkdriver);
183 disk_attach(&sc->sc_dkdev);
184 }
185
186 /*
187 * operational routines:
188 * open, close, read, write, strategy,
189 * ioctl, dump, size
190 */
191
192 #if MEMORY_DISK_SERVER
193 static int md_server_loop(struct md_softc *sc);
194 static int md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
195 struct lwp *l);
196 #endif /* MEMORY_DISK_SERVER */
197 static int md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
198 struct lwp *l);
199
200 static int
201 mdsize(dev_t dev)
202 {
203 int unit;
204 struct md_softc *sc;
205
206 unit = MD_UNIT(dev);
207 if (unit >= ramdisk_ndevs)
208 return 0;
209 sc = ramdisk_devs[unit];
210 if (sc == NULL)
211 return 0;
212
213 if (sc->sc_type == MD_UNCONFIGURED)
214 return 0;
215
216 return (sc->sc_size >> DEV_BSHIFT);
217 }
218
219 static int
220 mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
221 {
222 int unit;
223 struct md_softc *sc;
224
225 unit = MD_UNIT(dev);
226 if (unit >= ramdisk_ndevs)
227 return ENXIO;
228 sc = ramdisk_devs[unit];
229 if (sc == NULL)
230 return ENXIO;
231
232 /*
233 * The raw partition is used for ioctl to configure.
234 */
235 if (DISKPART(dev) == RAW_PART)
236 return 0;
237
238 #ifdef MEMORY_DISK_HOOKS
239 /* Call the open hook to allow loading the device. */
240 md_open_hook(unit, &sc->sc_md);
241 #endif
242
243 /*
244 * This is a normal, "slave" device, so
245 * enforce initialized.
246 */
247 if (sc->sc_type == MD_UNCONFIGURED)
248 return ENXIO;
249
250 return 0;
251 }
252
253 static int
254 mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
255 {
256 int unit;
257
258 unit = MD_UNIT(dev);
259
260 if (unit >= ramdisk_ndevs)
261 return ENXIO;
262
263 return 0;
264 }
265
266 static int
267 mdread(dev_t dev, struct uio *uio, int flags)
268 {
269 int unit;
270 struct md_softc *sc;
271
272 unit = MD_UNIT(dev);
273
274 if (unit >= ramdisk_ndevs)
275 return ENXIO;
276
277 sc = ramdisk_devs[unit];
278
279 if (sc->sc_type == MD_UNCONFIGURED)
280 return ENXIO;
281
282 return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
283 }
284
285 static int
286 mdwrite(dev_t dev, struct uio *uio, int flags)
287 {
288 int unit;
289 struct md_softc *sc;
290
291 unit = MD_UNIT(dev);
292
293 if (unit >= ramdisk_ndevs)
294 return ENXIO;
295
296 sc = ramdisk_devs[unit];
297
298 if (sc->sc_type == MD_UNCONFIGURED)
299 return ENXIO;
300
301 return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
302 }
303
304 /*
305 * Handle I/O requests, either directly, or
306 * by passing them to the server process.
307 */
308 static void
309 mdstrategy(struct buf *bp)
310 {
311 int unit;
312 struct md_softc *sc;
313 void * addr;
314 size_t off, xfer;
315
316 unit = MD_UNIT(bp->b_dev);
317 sc = ramdisk_devs[unit];
318
319 if (sc->sc_type == MD_UNCONFIGURED) {
320 bp->b_error = ENXIO;
321 goto done;
322 }
323
324 switch (sc->sc_type) {
325 #if MEMORY_DISK_SERVER
326 case MD_UMEM_SERVER:
327 /* Just add this job to the server's queue. */
328 BUFQ_PUT(sc->sc_buflist, bp);
329 wakeup((void *)sc);
330 /* see md_server_loop() */
331 /* no biodone in this case */
332 return;
333 #endif /* MEMORY_DISK_SERVER */
334
335 case MD_KMEM_FIXED:
336 case MD_KMEM_ALLOCATED:
337 /* These are in kernel space. Access directly. */
338 bp->b_resid = bp->b_bcount;
339 off = (bp->b_blkno << DEV_BSHIFT);
340 if (off >= sc->sc_size) {
341 if (bp->b_flags & B_READ)
342 break; /* EOF */
343 goto set_eio;
344 }
345 xfer = bp->b_resid;
346 if (xfer > (sc->sc_size - off))
347 xfer = (sc->sc_size - off);
348 addr = (char *)sc->sc_addr + off;
349 if (bp->b_flags & B_READ)
350 memcpy(bp->b_data, addr, xfer);
351 else
352 memcpy(addr, bp->b_data, xfer);
353 bp->b_resid -= xfer;
354 break;
355
356 default:
357 bp->b_resid = bp->b_bcount;
358 set_eio:
359 bp->b_error = EIO;
360 break;
361 }
362 done:
363 biodone(bp);
364 }
365
366 static int
367 mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
368 {
369 int unit;
370 struct md_softc *sc;
371 struct md_conf *umd;
372
373 unit = MD_UNIT(dev);
374 sc = ramdisk_devs[unit];
375
376 /* If this is not the raw partition, punt! */
377 if (DISKPART(dev) != RAW_PART)
378 return ENOTTY;
379
380 umd = (struct md_conf *)data;
381 switch (cmd) {
382 case MD_GETCONF:
383 *umd = sc->sc_md;
384 return 0;
385
386 case MD_SETCONF:
387 /* Can only set it once. */
388 if (sc->sc_type != MD_UNCONFIGURED)
389 break;
390 switch (umd->md_type) {
391 case MD_KMEM_ALLOCATED:
392 return md_ioctl_kalloc(sc, umd, l);
393 #if MEMORY_DISK_SERVER
394 case MD_UMEM_SERVER:
395 return md_ioctl_server(sc, umd, l);
396 #endif /* MEMORY_DISK_SERVER */
397 default:
398 break;
399 }
400 break;
401 }
402 return EINVAL;
403 }
404
405 /*
406 * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
407 * Just allocate some kernel memory and return.
408 */
409 static int
410 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
411 struct lwp *l)
412 {
413 vaddr_t addr;
414 vsize_t size;
415
416 /* Sanity check the size. */
417 size = umd->md_size;
418 addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
419 if (!addr)
420 return ENOMEM;
421
422 /* This unit is now configured. */
423 sc->sc_addr = (void *)addr; /* kernel space */
424 sc->sc_size = (size_t)size;
425 sc->sc_type = MD_KMEM_ALLOCATED;
426 return 0;
427 }
428
429 #if MEMORY_DISK_SERVER
430
431 /*
432 * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
433 * Set config, then become the I/O server for this unit.
434 */
435 static int
436 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
437 struct lwp *l)
438 {
439 vaddr_t end;
440 int error;
441
442 /* Sanity check addr, size. */
443 end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
444
445 if ((end >= VM_MAXUSER_ADDRESS) ||
446 (end < ((vaddr_t) umd->md_addr)) )
447 return EINVAL;
448
449 /* This unit is now configured. */
450 sc->sc_addr = umd->md_addr; /* user space */
451 sc->sc_size = umd->md_size;
452 sc->sc_type = MD_UMEM_SERVER;
453
454 /* Become the server daemon */
455 error = md_server_loop(sc);
456
457 /* This server is now going away! */
458 sc->sc_type = MD_UNCONFIGURED;
459 sc->sc_addr = 0;
460 sc->sc_size = 0;
461
462 return (error);
463 }
464
465 static int md_sleep_pri = PWAIT | PCATCH;
466
467 static int
468 md_server_loop(struct md_softc *sc)
469 {
470 struct buf *bp;
471 void *addr; /* user space address */
472 size_t off; /* offset into "device" */
473 size_t xfer; /* amount to transfer */
474 int error;
475
476 for (;;) {
477 /* Wait for some work to arrive. */
478 while ((bp = BUFQ_GET(sc->sc_buflist)) == NULL) {
479 error = tsleep((void *)sc, md_sleep_pri, "md_idle", 0);
480 if (error)
481 return error;
482 }
483
484 /* Do the transfer to/from user space. */
485 error = 0;
486 bp->b_resid = bp->b_bcount;
487 off = (bp->b_blkno << DEV_BSHIFT);
488 if (off >= sc->sc_size) {
489 if (bp->b_flags & B_READ)
490 goto done; /* EOF (not an error) */
491 error = EIO;
492 goto done;
493 }
494 xfer = bp->b_resid;
495 if (xfer > (sc->sc_size - off))
496 xfer = (sc->sc_size - off);
497 addr = (char *)sc->sc_addr + off;
498 if (bp->b_flags & B_READ)
499 error = copyin(addr, bp->b_data, xfer);
500 else
501 error = copyout(bp->b_data, addr, xfer);
502 if (!error)
503 bp->b_resid -= xfer;
504
505 done:
506 if (error) {
507 bp->b_error = error;
508 }
509 biodone(bp);
510 }
511 }
512 #endif /* MEMORY_DISK_SERVER */
513