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