md.c revision 1.50.10.2 1 1.50.10.2 ad /* $NetBSD: md.c,v 1.50.10.2 2007/07/29 12:50:19 ad Exp $ */
2 1.50.10.2 ad
3 1.50.10.2 ad /*
4 1.50.10.2 ad * Copyright (c) 1995 Gordon W. Ross, Leo Weppelman.
5 1.50.10.2 ad * All rights reserved.
6 1.50.10.2 ad *
7 1.50.10.2 ad * Redistribution and use in source and binary forms, with or without
8 1.50.10.2 ad * modification, are permitted provided that the following conditions
9 1.50.10.2 ad * are met:
10 1.50.10.2 ad * 1. Redistributions of source code must retain the above copyright
11 1.50.10.2 ad * notice, this list of conditions and the following disclaimer.
12 1.50.10.2 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.50.10.2 ad * notice, this list of conditions and the following disclaimer in the
14 1.50.10.2 ad * documentation and/or other materials provided with the distribution.
15 1.50.10.2 ad * 3. The name of the author may not be used to endorse or promote products
16 1.50.10.2 ad * derived from this software without specific prior written permission.
17 1.50.10.2 ad * 4. All advertising materials mentioning features or use of this software
18 1.50.10.2 ad * must display the following acknowledgement:
19 1.50.10.2 ad * This product includes software developed by
20 1.50.10.2 ad * Gordon W. Ross and Leo Weppelman.
21 1.50.10.2 ad *
22 1.50.10.2 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.50.10.2 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.50.10.2 ad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.50.10.2 ad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.50.10.2 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.50.10.2 ad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.50.10.2 ad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.50.10.2 ad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.50.10.2 ad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.50.10.2 ad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.50.10.2 ad */
33 1.50.10.2 ad
34 1.50.10.2 ad /*
35 1.50.10.2 ad * This implements a general-purpose memory-disk.
36 1.50.10.2 ad * See md.h for notes on the config types.
37 1.50.10.2 ad *
38 1.50.10.2 ad * Note that this driver provides the same functionality
39 1.50.10.2 ad * as the MFS filesystem hack, but this is better because
40 1.50.10.2 ad * you can use this for any filesystem type you'd like!
41 1.50.10.2 ad *
42 1.50.10.2 ad * Credit for most of the kmem ramdisk code goes to:
43 1.50.10.2 ad * Leo Weppelman (atari) and Phil Nelson (pc532)
44 1.50.10.2 ad * Credit for the ideas behind the "user space memory" code goes
45 1.50.10.2 ad * to the authors of the MFS implementation.
46 1.50.10.2 ad */
47 1.50.10.2 ad
48 1.50.10.2 ad #include <sys/cdefs.h>
49 1.50.10.2 ad __KERNEL_RCSID(0, "$NetBSD: md.c,v 1.50.10.2 2007/07/29 12:50:19 ad Exp $");
50 1.50.10.2 ad
51 1.50.10.2 ad #include "opt_md.h"
52 1.50.10.2 ad
53 1.50.10.2 ad #include <sys/param.h>
54 1.50.10.2 ad #include <sys/kernel.h>
55 1.50.10.2 ad #include <sys/malloc.h>
56 1.50.10.2 ad #include <sys/systm.h>
57 1.50.10.2 ad #include <sys/buf.h>
58 1.50.10.2 ad #include <sys/bufq.h>
59 1.50.10.2 ad #include <sys/device.h>
60 1.50.10.2 ad #include <sys/disk.h>
61 1.50.10.2 ad #include <sys/proc.h>
62 1.50.10.2 ad #include <sys/conf.h>
63 1.50.10.2 ad #include <sys/disklabel.h>
64 1.50.10.2 ad
65 1.50.10.2 ad #include <uvm/uvm_extern.h>
66 1.50.10.2 ad
67 1.50.10.2 ad #include <dev/md.h>
68 1.50.10.2 ad
69 1.50.10.2 ad /*
70 1.50.10.2 ad * The user-space functionality is included by default.
71 1.50.10.2 ad * Use `options MEMORY_DISK_SERVER=0' to turn it off.
72 1.50.10.2 ad */
73 1.50.10.2 ad #ifndef MEMORY_DISK_SERVER
74 1.50.10.2 ad #error MEMORY_DISK_SERVER should be defined by opt_md.h
75 1.50.10.2 ad #endif /* MEMORY_DISK_SERVER */
76 1.50.10.2 ad
77 1.50.10.2 ad /*
78 1.50.10.2 ad * We should use the raw partition for ioctl.
79 1.50.10.2 ad */
80 1.50.10.2 ad #define MD_MAX_UNITS 0x10
81 1.50.10.2 ad #define MD_UNIT(unit) DISKUNIT(unit)
82 1.50.10.2 ad
83 1.50.10.2 ad /* autoconfig stuff... */
84 1.50.10.2 ad
85 1.50.10.2 ad struct md_softc {
86 1.50.10.2 ad struct device sc_dev; /* REQUIRED first entry */
87 1.50.10.2 ad struct disk sc_dkdev; /* hook for generic disk handling */
88 1.50.10.2 ad struct md_conf sc_md;
89 1.50.10.2 ad struct bufq_state *sc_buflist;
90 1.50.10.2 ad };
91 1.50.10.2 ad /* shorthand for fields in sc_md: */
92 1.50.10.2 ad #define sc_addr sc_md.md_addr
93 1.50.10.2 ad #define sc_size sc_md.md_size
94 1.50.10.2 ad #define sc_type sc_md.md_type
95 1.50.10.2 ad
96 1.50.10.2 ad void mdattach(int);
97 1.50.10.2 ad
98 1.50.10.2 ad static void md_attach(struct device *, struct device *, void *);
99 1.50.10.2 ad
100 1.50.10.2 ad static dev_type_open(mdopen);
101 1.50.10.2 ad static dev_type_close(mdclose);
102 1.50.10.2 ad static dev_type_read(mdread);
103 1.50.10.2 ad static dev_type_write(mdwrite);
104 1.50.10.2 ad static dev_type_ioctl(mdioctl);
105 1.50.10.2 ad static dev_type_strategy(mdstrategy);
106 1.50.10.2 ad static dev_type_size(mdsize);
107 1.50.10.2 ad
108 1.50.10.2 ad const struct bdevsw md_bdevsw = {
109 1.50.10.2 ad mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK
110 1.50.10.2 ad };
111 1.50.10.2 ad
112 1.50.10.2 ad const struct cdevsw md_cdevsw = {
113 1.50.10.2 ad mdopen, mdclose, mdread, mdwrite, mdioctl,
114 1.50.10.2 ad nostop, notty, nopoll, nommap, nokqfilter, D_DISK
115 1.50.10.2 ad };
116 1.50.10.2 ad
117 1.50.10.2 ad static struct dkdriver mddkdriver = { mdstrategy, NULL };
118 1.50.10.2 ad
119 1.50.10.2 ad static int ramdisk_ndevs;
120 1.50.10.2 ad static void *ramdisk_devs[MD_MAX_UNITS];
121 1.50.10.2 ad
122 1.50.10.2 ad /*
123 1.50.10.2 ad * This is called if we are configured as a pseudo-device
124 1.50.10.2 ad */
125 1.50.10.2 ad void
126 1.50.10.2 ad mdattach(int n)
127 1.50.10.2 ad {
128 1.50.10.2 ad struct md_softc *sc;
129 1.50.10.2 ad int i;
130 1.50.10.2 ad
131 1.50.10.2 ad #ifdef DIAGNOSTIC
132 1.50.10.2 ad if (ramdisk_ndevs) {
133 1.50.10.2 ad aprint_error("ramdisk: multiple attach calls?\n");
134 1.50.10.2 ad return;
135 1.50.10.2 ad }
136 1.50.10.2 ad #endif
137 1.50.10.2 ad
138 1.50.10.2 ad /* XXX: Are we supposed to provide a default? */
139 1.50.10.2 ad if (n <= 1)
140 1.50.10.2 ad n = 1;
141 1.50.10.2 ad if (n > MD_MAX_UNITS)
142 1.50.10.2 ad n = MD_MAX_UNITS;
143 1.50.10.2 ad ramdisk_ndevs = n;
144 1.50.10.2 ad
145 1.50.10.2 ad /* Attach as if by autoconfig. */
146 1.50.10.2 ad for (i = 0; i < n; i++) {
147 1.50.10.2 ad
148 1.50.10.2 ad sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT|M_ZERO);
149 1.50.10.2 ad if (!sc) {
150 1.50.10.2 ad aprint_error("ramdisk: malloc for attach failed!\n");
151 1.50.10.2 ad return;
152 1.50.10.2 ad }
153 1.50.10.2 ad ramdisk_devs[i] = sc;
154 1.50.10.2 ad sc->sc_dev.dv_unit = i;
155 1.50.10.2 ad snprintf(sc->sc_dev.dv_xname, sizeof(sc->sc_dev.dv_xname),
156 1.50.10.2 ad "md%d", i);
157 1.50.10.2 ad md_attach(NULL, &sc->sc_dev, NULL);
158 1.50.10.2 ad }
159 1.50.10.2 ad }
160 1.50.10.2 ad
161 1.50.10.2 ad static void
162 1.50.10.2 ad md_attach(struct device *parent, struct device *self,
163 1.50.10.2 ad void *aux)
164 1.50.10.2 ad {
165 1.50.10.2 ad struct md_softc *sc = (struct md_softc *)self;
166 1.50.10.2 ad
167 1.50.10.2 ad bufq_alloc(&sc->sc_buflist, "fcfs", 0);
168 1.50.10.2 ad
169 1.50.10.2 ad /* XXX - Could accept aux info here to set the config. */
170 1.50.10.2 ad #ifdef MEMORY_DISK_HOOKS
171 1.50.10.2 ad /*
172 1.50.10.2 ad * This external function might setup a pre-loaded disk.
173 1.50.10.2 ad * All it would need to do is setup the md_conf struct.
174 1.50.10.2 ad * See sys/dev/md_root.c for an example.
175 1.50.10.2 ad */
176 1.50.10.2 ad md_attach_hook(device_unit(&sc->sc_dev), &sc->sc_md);
177 1.50.10.2 ad #endif
178 1.50.10.2 ad
179 1.50.10.2 ad /*
180 1.50.10.2 ad * Initialize and attach the disk structure.
181 1.50.10.2 ad */
182 1.50.10.2 ad sc->sc_dkdev.dk_driver = &mddkdriver;
183 1.50.10.2 ad sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
184 1.50.10.2 ad disk_attach(&sc->sc_dkdev);
185 1.50.10.2 ad }
186 1.50.10.2 ad
187 1.50.10.2 ad /*
188 1.50.10.2 ad * operational routines:
189 1.50.10.2 ad * open, close, read, write, strategy,
190 1.50.10.2 ad * ioctl, dump, size
191 1.50.10.2 ad */
192 1.50.10.2 ad
193 1.50.10.2 ad #if MEMORY_DISK_SERVER
194 1.50.10.2 ad static int md_server_loop(struct md_softc *sc);
195 1.50.10.2 ad static int md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
196 1.50.10.2 ad struct lwp *l);
197 1.50.10.2 ad #endif /* MEMORY_DISK_SERVER */
198 1.50.10.2 ad static int md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
199 1.50.10.2 ad struct lwp *l);
200 1.50.10.2 ad
201 1.50.10.2 ad static int
202 1.50.10.2 ad mdsize(dev_t dev)
203 1.50.10.2 ad {
204 1.50.10.2 ad int unit;
205 1.50.10.2 ad struct md_softc *sc;
206 1.50.10.2 ad
207 1.50.10.2 ad unit = MD_UNIT(dev);
208 1.50.10.2 ad if (unit >= ramdisk_ndevs)
209 1.50.10.2 ad return 0;
210 1.50.10.2 ad sc = ramdisk_devs[unit];
211 1.50.10.2 ad if (sc == NULL)
212 1.50.10.2 ad return 0;
213 1.50.10.2 ad
214 1.50.10.2 ad if (sc->sc_type == MD_UNCONFIGURED)
215 1.50.10.2 ad return 0;
216 1.50.10.2 ad
217 1.50.10.2 ad return (sc->sc_size >> DEV_BSHIFT);
218 1.50.10.2 ad }
219 1.50.10.2 ad
220 1.50.10.2 ad static int
221 1.50.10.2 ad mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
222 1.50.10.2 ad {
223 1.50.10.2 ad int unit;
224 1.50.10.2 ad struct md_softc *sc;
225 1.50.10.2 ad
226 1.50.10.2 ad unit = MD_UNIT(dev);
227 1.50.10.2 ad if (unit >= ramdisk_ndevs)
228 1.50.10.2 ad return ENXIO;
229 1.50.10.2 ad sc = ramdisk_devs[unit];
230 1.50.10.2 ad if (sc == NULL)
231 1.50.10.2 ad return ENXIO;
232 1.50.10.2 ad
233 1.50.10.2 ad /*
234 1.50.10.2 ad * The raw partition is used for ioctl to configure.
235 1.50.10.2 ad */
236 1.50.10.2 ad if (DISKPART(dev) == RAW_PART)
237 1.50.10.2 ad return 0;
238 1.50.10.2 ad
239 1.50.10.2 ad #ifdef MEMORY_DISK_HOOKS
240 1.50.10.2 ad /* Call the open hook to allow loading the device. */
241 1.50.10.2 ad md_open_hook(unit, &sc->sc_md);
242 1.50.10.2 ad #endif
243 1.50.10.2 ad
244 1.50.10.2 ad /*
245 1.50.10.2 ad * This is a normal, "slave" device, so
246 1.50.10.2 ad * enforce initialized.
247 1.50.10.2 ad */
248 1.50.10.2 ad if (sc->sc_type == MD_UNCONFIGURED)
249 1.50.10.2 ad return ENXIO;
250 1.50.10.2 ad
251 1.50.10.2 ad return 0;
252 1.50.10.2 ad }
253 1.50.10.2 ad
254 1.50.10.2 ad static int
255 1.50.10.2 ad mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
256 1.50.10.2 ad {
257 1.50.10.2 ad int unit;
258 1.50.10.2 ad
259 1.50.10.2 ad unit = MD_UNIT(dev);
260 1.50.10.2 ad
261 1.50.10.2 ad if (unit >= ramdisk_ndevs)
262 1.50.10.2 ad return ENXIO;
263 1.50.10.2 ad
264 1.50.10.2 ad return 0;
265 1.50.10.2 ad }
266 1.50.10.2 ad
267 1.50.10.2 ad static int
268 1.50.10.2 ad mdread(dev_t dev, struct uio *uio, int flags)
269 1.50.10.2 ad {
270 1.50.10.2 ad int unit;
271 1.50.10.2 ad struct md_softc *sc;
272 1.50.10.2 ad
273 1.50.10.2 ad unit = MD_UNIT(dev);
274 1.50.10.2 ad
275 1.50.10.2 ad if (unit >= ramdisk_ndevs)
276 1.50.10.2 ad return ENXIO;
277 1.50.10.2 ad
278 1.50.10.2 ad sc = ramdisk_devs[unit];
279 1.50.10.2 ad
280 1.50.10.2 ad if (sc->sc_type == MD_UNCONFIGURED)
281 1.50.10.2 ad return ENXIO;
282 1.50.10.2 ad
283 1.50.10.2 ad return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
284 1.50.10.2 ad }
285 1.50.10.2 ad
286 1.50.10.2 ad static int
287 1.50.10.2 ad mdwrite(dev_t dev, struct uio *uio, int flags)
288 1.50.10.2 ad {
289 1.50.10.2 ad int unit;
290 1.50.10.2 ad struct md_softc *sc;
291 1.50.10.2 ad
292 1.50.10.2 ad unit = MD_UNIT(dev);
293 1.50.10.2 ad
294 1.50.10.2 ad if (unit >= ramdisk_ndevs)
295 1.50.10.2 ad return ENXIO;
296 1.50.10.2 ad
297 1.50.10.2 ad sc = ramdisk_devs[unit];
298 1.50.10.2 ad
299 1.50.10.2 ad if (sc->sc_type == MD_UNCONFIGURED)
300 1.50.10.2 ad return ENXIO;
301 1.50.10.2 ad
302 1.50.10.2 ad return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
303 1.50.10.2 ad }
304 1.50.10.2 ad
305 1.50.10.2 ad /*
306 1.50.10.2 ad * Handle I/O requests, either directly, or
307 1.50.10.2 ad * by passing them to the server process.
308 1.50.10.2 ad */
309 1.50.10.2 ad static void
310 1.50.10.2 ad mdstrategy(struct buf *bp)
311 1.50.10.2 ad {
312 1.50.10.2 ad int unit;
313 1.50.10.2 ad struct md_softc *sc;
314 1.50.10.2 ad void * addr;
315 1.50.10.2 ad size_t off, xfer;
316 1.50.10.2 ad
317 1.50.10.2 ad unit = MD_UNIT(bp->b_dev);
318 1.50.10.2 ad sc = ramdisk_devs[unit];
319 1.50.10.2 ad
320 1.50.10.2 ad if (sc->sc_type == MD_UNCONFIGURED) {
321 1.50.10.2 ad bp->b_error = ENXIO;
322 1.50.10.2 ad goto done;
323 1.50.10.2 ad }
324 1.50.10.2 ad
325 1.50.10.2 ad switch (sc->sc_type) {
326 1.50.10.2 ad #if MEMORY_DISK_SERVER
327 1.50.10.2 ad case MD_UMEM_SERVER:
328 1.50.10.2 ad /* Just add this job to the server's queue. */
329 1.50.10.2 ad BUFQ_PUT(sc->sc_buflist, bp);
330 1.50.10.2 ad wakeup((void *)sc);
331 1.50.10.2 ad /* see md_server_loop() */
332 1.50.10.2 ad /* no biodone in this case */
333 1.50.10.2 ad return;
334 1.50.10.2 ad #endif /* MEMORY_DISK_SERVER */
335 1.50.10.2 ad
336 1.50.10.2 ad case MD_KMEM_FIXED:
337 1.50.10.2 ad case MD_KMEM_ALLOCATED:
338 1.50.10.2 ad /* These are in kernel space. Access directly. */
339 1.50.10.2 ad bp->b_resid = bp->b_bcount;
340 1.50.10.2 ad off = (bp->b_blkno << DEV_BSHIFT);
341 1.50.10.2 ad if (off >= sc->sc_size) {
342 1.50.10.2 ad if (bp->b_flags & B_READ)
343 1.50.10.2 ad break; /* EOF */
344 1.50.10.2 ad goto set_eio;
345 1.50.10.2 ad }
346 1.50.10.2 ad xfer = bp->b_resid;
347 1.50.10.2 ad if (xfer > (sc->sc_size - off))
348 1.50.10.2 ad xfer = (sc->sc_size - off);
349 1.50.10.2 ad addr = (char *)sc->sc_addr + off;
350 1.50.10.2 ad if (bp->b_flags & B_READ)
351 1.50.10.2 ad memcpy(bp->b_data, addr, xfer);
352 1.50.10.2 ad else
353 1.50.10.2 ad memcpy(addr, bp->b_data, xfer);
354 1.50.10.2 ad bp->b_resid -= xfer;
355 1.50.10.2 ad break;
356 1.50.10.2 ad
357 1.50.10.2 ad default:
358 1.50.10.2 ad bp->b_resid = bp->b_bcount;
359 1.50.10.2 ad set_eio:
360 1.50.10.2 ad bp->b_error = EIO;
361 1.50.10.2 ad break;
362 1.50.10.2 ad }
363 1.50.10.2 ad done:
364 1.50.10.2 ad biodone(bp);
365 1.50.10.2 ad }
366 1.50.10.2 ad
367 1.50.10.2 ad static int
368 1.50.10.2 ad mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
369 1.50.10.2 ad {
370 1.50.10.2 ad int unit;
371 1.50.10.2 ad struct md_softc *sc;
372 1.50.10.2 ad struct md_conf *umd;
373 1.50.10.2 ad
374 1.50.10.2 ad unit = MD_UNIT(dev);
375 1.50.10.2 ad sc = ramdisk_devs[unit];
376 1.50.10.2 ad
377 1.50.10.2 ad /* If this is not the raw partition, punt! */
378 1.50.10.2 ad if (DISKPART(dev) != RAW_PART)
379 1.50.10.2 ad return ENOTTY;
380 1.50.10.2 ad
381 1.50.10.2 ad umd = (struct md_conf *)data;
382 1.50.10.2 ad switch (cmd) {
383 1.50.10.2 ad case MD_GETCONF:
384 1.50.10.2 ad *umd = sc->sc_md;
385 1.50.10.2 ad return 0;
386 1.50.10.2 ad
387 1.50.10.2 ad case MD_SETCONF:
388 1.50.10.2 ad /* Can only set it once. */
389 1.50.10.2 ad if (sc->sc_type != MD_UNCONFIGURED)
390 1.50.10.2 ad break;
391 1.50.10.2 ad switch (umd->md_type) {
392 1.50.10.2 ad case MD_KMEM_ALLOCATED:
393 1.50.10.2 ad return md_ioctl_kalloc(sc, umd, l);
394 1.50.10.2 ad #if MEMORY_DISK_SERVER
395 1.50.10.2 ad case MD_UMEM_SERVER:
396 1.50.10.2 ad return md_ioctl_server(sc, umd, l);
397 1.50.10.2 ad #endif /* MEMORY_DISK_SERVER */
398 1.50.10.2 ad default:
399 1.50.10.2 ad break;
400 1.50.10.2 ad }
401 1.50.10.2 ad break;
402 1.50.10.2 ad }
403 1.50.10.2 ad return EINVAL;
404 1.50.10.2 ad }
405 1.50.10.2 ad
406 1.50.10.2 ad /*
407 1.50.10.2 ad * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
408 1.50.10.2 ad * Just allocate some kernel memory and return.
409 1.50.10.2 ad */
410 1.50.10.2 ad static int
411 1.50.10.2 ad md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
412 1.50.10.2 ad struct lwp *l)
413 1.50.10.2 ad {
414 1.50.10.2 ad vaddr_t addr;
415 1.50.10.2 ad vsize_t size;
416 1.50.10.2 ad
417 1.50.10.2 ad /* Sanity check the size. */
418 1.50.10.2 ad size = umd->md_size;
419 1.50.10.2 ad addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
420 1.50.10.2 ad if (!addr)
421 1.50.10.2 ad return ENOMEM;
422 1.50.10.2 ad
423 1.50.10.2 ad /* This unit is now configured. */
424 1.50.10.2 ad sc->sc_addr = (void *)addr; /* kernel space */
425 1.50.10.2 ad sc->sc_size = (size_t)size;
426 1.50.10.2 ad sc->sc_type = MD_KMEM_ALLOCATED;
427 1.50.10.2 ad return 0;
428 1.50.10.2 ad }
429 1.50.10.2 ad
430 1.50.10.2 ad #if MEMORY_DISK_SERVER
431 1.50.10.2 ad
432 1.50.10.2 ad /*
433 1.50.10.2 ad * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
434 1.50.10.2 ad * Set config, then become the I/O server for this unit.
435 1.50.10.2 ad */
436 1.50.10.2 ad static int
437 1.50.10.2 ad md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
438 1.50.10.2 ad struct lwp *l)
439 1.50.10.2 ad {
440 1.50.10.2 ad vaddr_t end;
441 1.50.10.2 ad int error;
442 1.50.10.2 ad
443 1.50.10.2 ad /* Sanity check addr, size. */
444 1.50.10.2 ad end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
445 1.50.10.2 ad
446 1.50.10.2 ad if ((end >= VM_MAXUSER_ADDRESS) ||
447 1.50.10.2 ad (end < ((vaddr_t) umd->md_addr)) )
448 1.50.10.2 ad return EINVAL;
449 1.50.10.2 ad
450 1.50.10.2 ad /* This unit is now configured. */
451 1.50.10.2 ad sc->sc_addr = umd->md_addr; /* user space */
452 1.50.10.2 ad sc->sc_size = umd->md_size;
453 1.50.10.2 ad sc->sc_type = MD_UMEM_SERVER;
454 1.50.10.2 ad
455 1.50.10.2 ad /* Become the server daemon */
456 1.50.10.2 ad error = md_server_loop(sc);
457 1.50.10.2 ad
458 1.50.10.2 ad /* This server is now going away! */
459 1.50.10.2 ad sc->sc_type = MD_UNCONFIGURED;
460 1.50.10.2 ad sc->sc_addr = 0;
461 1.50.10.2 ad sc->sc_size = 0;
462 1.50.10.2 ad
463 1.50.10.2 ad return (error);
464 1.50.10.2 ad }
465 1.50.10.2 ad
466 1.50.10.2 ad static int md_sleep_pri = PWAIT | PCATCH;
467 1.50.10.2 ad
468 1.50.10.2 ad static int
469 1.50.10.2 ad md_server_loop(struct md_softc *sc)
470 1.50.10.2 ad {
471 1.50.10.2 ad struct buf *bp;
472 1.50.10.2 ad void *addr; /* user space address */
473 1.50.10.2 ad size_t off; /* offset into "device" */
474 1.50.10.2 ad size_t xfer; /* amount to transfer */
475 1.50.10.2 ad int error;
476 1.50.10.2 ad
477 1.50.10.2 ad for (;;) {
478 1.50.10.2 ad /* Wait for some work to arrive. */
479 1.50.10.2 ad while ((bp = BUFQ_GET(sc->sc_buflist)) == NULL) {
480 1.50.10.2 ad error = tsleep((void *)sc, md_sleep_pri, "md_idle", 0);
481 1.50.10.2 ad if (error)
482 1.50.10.2 ad return error;
483 1.50.10.2 ad }
484 1.50.10.2 ad
485 1.50.10.2 ad /* Do the transfer to/from user space. */
486 1.50.10.2 ad error = 0;
487 1.50.10.2 ad bp->b_resid = bp->b_bcount;
488 1.50.10.2 ad off = (bp->b_blkno << DEV_BSHIFT);
489 1.50.10.2 ad if (off >= sc->sc_size) {
490 1.50.10.2 ad if (bp->b_flags & B_READ)
491 1.50.10.2 ad goto done; /* EOF (not an error) */
492 1.50.10.2 ad error = EIO;
493 1.50.10.2 ad goto done;
494 1.50.10.2 ad }
495 1.50.10.2 ad xfer = bp->b_resid;
496 1.50.10.2 ad if (xfer > (sc->sc_size - off))
497 1.50.10.2 ad xfer = (sc->sc_size - off);
498 1.50.10.2 ad addr = (char *)sc->sc_addr + off;
499 1.50.10.2 ad if (bp->b_flags & B_READ)
500 1.50.10.2 ad error = copyin(addr, bp->b_data, xfer);
501 1.50.10.2 ad else
502 1.50.10.2 ad error = copyout(bp->b_data, addr, xfer);
503 1.50.10.2 ad if (!error)
504 1.50.10.2 ad bp->b_resid -= xfer;
505 1.50.10.2 ad
506 1.50.10.2 ad done:
507 1.50.10.2 ad if (error) {
508 1.50.10.2 ad bp->b_error = error;
509 1.50.10.2 ad }
510 1.50.10.2 ad biodone(bp);
511 1.50.10.2 ad }
512 1.50.10.2 ad }
513 1.50.10.2 ad #endif /* MEMORY_DISK_SERVER */
514