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