ld.c revision 1.49 1 /* $NetBSD: ld.c,v 1.49 2007/07/29 12:50:18 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran and Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Disk driver for use by RAID controllers.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.49 2007/07/29 12:50:18 ad Exp $");
45
46 #include "rnd.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/device.h>
52 #include <sys/queue.h>
53 #include <sys/proc.h>
54 #include <sys/buf.h>
55 #include <sys/bufq.h>
56 #include <sys/endian.h>
57 #include <sys/disklabel.h>
58 #include <sys/disk.h>
59 #include <sys/dkio.h>
60 #include <sys/stat.h>
61 #include <sys/lock.h>
62 #include <sys/conf.h>
63 #include <sys/fcntl.h>
64 #include <sys/vnode.h>
65 #include <sys/syslog.h>
66 #include <sys/mutex.h>
67 #if NRND > 0
68 #include <sys/rnd.h>
69 #endif
70
71 #include <dev/ldvar.h>
72
73 #include <prop/proplib.h>
74
75 static void ldgetdefaultlabel(struct ld_softc *, struct disklabel *);
76 static void ldgetdisklabel(struct ld_softc *);
77 static void ldminphys(struct buf *bp);
78 static void ldshutdown(void *);
79 static void ldstart(struct ld_softc *, struct buf *);
80 static void ld_set_properties(struct ld_softc *);
81 static void ld_config_interrupts (struct device *);
82
83 extern struct cfdriver ld_cd;
84
85 static dev_type_open(ldopen);
86 static dev_type_close(ldclose);
87 static dev_type_read(ldread);
88 static dev_type_write(ldwrite);
89 static dev_type_ioctl(ldioctl);
90 static dev_type_strategy(ldstrategy);
91 static dev_type_dump(lddump);
92 static dev_type_size(ldsize);
93
94 const struct bdevsw ld_bdevsw = {
95 ldopen, ldclose, ldstrategy, ldioctl, lddump, ldsize, D_DISK
96 };
97
98 const struct cdevsw ld_cdevsw = {
99 ldopen, ldclose, ldread, ldwrite, ldioctl,
100 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
101 };
102
103 static struct dkdriver lddkdriver = { ldstrategy, ldminphys };
104 static void *ld_sdh;
105
106 void
107 ldattach(struct ld_softc *sc)
108 {
109 char tbuf[9];
110
111 mutex_init(&sc->sc_mutex, MUTEX_DRIVER, IPL_BIO);
112
113 if ((sc->sc_flags & LDF_ENABLED) == 0) {
114 aprint_normal("%s: disabled\n", sc->sc_dv.dv_xname);
115 return;
116 }
117
118 /* Initialise and attach the disk structure. */
119 sc->sc_dk.dk_driver = &lddkdriver;
120 sc->sc_dk.dk_name = sc->sc_dv.dv_xname;
121 disk_attach(&sc->sc_dk);
122
123 if (sc->sc_maxxfer > MAXPHYS)
124 sc->sc_maxxfer = MAXPHYS;
125
126 /* Build synthetic geometry if necessary. */
127 if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 ||
128 sc->sc_ncylinders == 0) {
129 uint64_t ncyl;
130
131 if (sc->sc_secperunit <= 528 * 2048) /* 528MB */
132 sc->sc_nheads = 16;
133 else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */
134 sc->sc_nheads = 32;
135 else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */
136 sc->sc_nheads = 64;
137 else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */
138 sc->sc_nheads = 128;
139 else
140 sc->sc_nheads = 255;
141
142 sc->sc_nsectors = 63;
143 sc->sc_ncylinders = INT_MAX;
144 ncyl = sc->sc_secperunit /
145 (sc->sc_nheads * sc->sc_nsectors);
146 if (ncyl < INT_MAX)
147 sc->sc_ncylinders = (int)ncyl;
148 }
149
150 format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
151 sc->sc_secsize);
152 aprint_normal("%s: %s, %d cyl, %d head, %d sec, %d bytes/sect x %"PRIu64" sectors\n",
153 sc->sc_dv.dv_xname, tbuf, sc->sc_ncylinders, sc->sc_nheads,
154 sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
155
156 ld_set_properties(sc);
157
158 #if NRND > 0
159 /* Attach the device into the rnd source list. */
160 rnd_attach_source(&sc->sc_rnd_source, sc->sc_dv.dv_xname,
161 RND_TYPE_DISK, 0);
162 #endif
163
164 /* Set the `shutdownhook'. */
165 if (ld_sdh == NULL)
166 ld_sdh = shutdownhook_establish(ldshutdown, NULL);
167 bufq_alloc(&sc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
168
169 /* Discover wedges on this disk. */
170 config_interrupts(&sc->sc_dv, ld_config_interrupts);
171 }
172
173 int
174 ldadjqparam(struct ld_softc *sc, int xmax)
175 {
176 int s;
177
178 s = splbio();
179 sc->sc_maxqueuecnt = xmax;
180 splx(s);
181
182 return (0);
183 }
184
185 int
186 ldbegindetach(struct ld_softc *sc, int flags)
187 {
188 int s, rv = 0;
189
190 if ((sc->sc_flags & LDF_ENABLED) == 0)
191 return (0);
192
193 if ((flags & DETACH_FORCE) == 0 && sc->sc_dk.dk_openmask != 0)
194 return (EBUSY);
195
196 s = splbio();
197 sc->sc_maxqueuecnt = 0;
198 sc->sc_flags |= LDF_DETACH;
199 while (sc->sc_queuecnt > 0) {
200 sc->sc_flags |= LDF_DRAIN;
201 rv = tsleep(&sc->sc_queuecnt, PRIBIO, "lddrn", 0);
202 if (rv)
203 break;
204 }
205 splx(s);
206
207 return (rv);
208 }
209
210 void
211 ldenddetach(struct ld_softc *sc)
212 {
213 int s, bmaj, cmaj, i, mn;
214
215 if ((sc->sc_flags & LDF_ENABLED) == 0)
216 return;
217
218 /* Wait for commands queued with the hardware to complete. */
219 if (sc->sc_queuecnt != 0)
220 if (tsleep(&sc->sc_queuecnt, PRIBIO, "lddtch", 30 * hz))
221 printf("%s: not drained\n", sc->sc_dv.dv_xname);
222
223 /* Locate the major numbers. */
224 bmaj = bdevsw_lookup_major(&ld_bdevsw);
225 cmaj = cdevsw_lookup_major(&ld_cdevsw);
226
227 /* Kill off any queued buffers. */
228 s = splbio();
229 bufq_drain(sc->sc_bufq);
230 splx(s);
231
232 bufq_free(sc->sc_bufq);
233
234 /* Nuke the vnodes for any open instances. */
235 for (i = 0; i < MAXPARTITIONS; i++) {
236 mn = DISKMINOR(device_unit(&sc->sc_dv), i);
237 vdevgone(bmaj, mn, mn, VBLK);
238 vdevgone(cmaj, mn, mn, VCHR);
239 }
240
241 /* Delete all of our wedges. */
242 dkwedge_delall(&sc->sc_dk);
243
244 /* Detach from the disk list. */
245 disk_detach(&sc->sc_dk);
246
247 #if NRND > 0
248 /* Unhook the entropy source. */
249 rnd_detach_source(&sc->sc_rnd_source);
250 #endif
251
252 /*
253 * XXX We can't really flush the cache here, beceause the
254 * XXX device may already be non-existent from the controller's
255 * XXX perspective.
256 */
257 #if 0
258 /* Flush the device's cache. */
259 if (sc->sc_flush != NULL)
260 if ((*sc->sc_flush)(sc) != 0)
261 printf("%s: unable to flush cache\n",
262 sc->sc_dv.dv_xname);
263 #endif
264 }
265
266 /* ARGSUSED */
267 static void
268 ldshutdown(void *cookie)
269 {
270 struct ld_softc *sc;
271 int i;
272
273 for (i = 0; i < ld_cd.cd_ndevs; i++) {
274 if ((sc = device_lookup(&ld_cd, i)) == NULL)
275 continue;
276 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
277 printf("%s: unable to flush cache\n",
278 sc->sc_dv.dv_xname);
279 }
280 }
281
282 /* ARGSUSED */
283 static int
284 ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
285 {
286 struct ld_softc *sc;
287 int error, unit, part;
288
289 unit = DISKUNIT(dev);
290 if ((sc = device_lookup(&ld_cd, unit)) == NULL)
291 return (ENXIO);
292 if ((sc->sc_flags & LDF_ENABLED) == 0)
293 return (ENODEV);
294 part = DISKPART(dev);
295
296 mutex_enter(&sc->sc_dk.dk_openlock);
297
298 if (sc->sc_dk.dk_openmask == 0) {
299 /* Load the partition info if not already loaded. */
300 if ((sc->sc_flags & LDF_VLABEL) == 0)
301 ldgetdisklabel(sc);
302 }
303
304 /* Check that the partition exists. */
305 if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
306 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
307 error = ENXIO;
308 goto bad1;
309 }
310
311 /* Ensure only one open at a time. */
312 switch (fmt) {
313 case S_IFCHR:
314 sc->sc_dk.dk_copenmask |= (1 << part);
315 break;
316 case S_IFBLK:
317 sc->sc_dk.dk_bopenmask |= (1 << part);
318 break;
319 }
320 sc->sc_dk.dk_openmask =
321 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
322
323 error = 0;
324 bad1:
325 mutex_exit(&sc->sc_dk.dk_openlock);
326 return (error);
327 }
328
329 /* ARGSUSED */
330 static int
331 ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
332 {
333 struct ld_softc *sc;
334 int part, unit;
335
336 unit = DISKUNIT(dev);
337 part = DISKPART(dev);
338 sc = device_lookup(&ld_cd, unit);
339
340 mutex_enter(&sc->sc_dk.dk_openlock);
341
342 switch (fmt) {
343 case S_IFCHR:
344 sc->sc_dk.dk_copenmask &= ~(1 << part);
345 break;
346 case S_IFBLK:
347 sc->sc_dk.dk_bopenmask &= ~(1 << part);
348 break;
349 }
350 sc->sc_dk.dk_openmask =
351 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
352
353 if (sc->sc_dk.dk_openmask == 0) {
354 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
355 printf("%s: unable to flush cache\n",
356 sc->sc_dv.dv_xname);
357 if ((sc->sc_flags & LDF_KLABEL) == 0)
358 sc->sc_flags &= ~LDF_VLABEL;
359 }
360
361 mutex_exit(&sc->sc_dk.dk_openlock);
362 return (0);
363 }
364
365 /* ARGSUSED */
366 static int
367 ldread(dev_t dev, struct uio *uio, int ioflag)
368 {
369
370 return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
371 }
372
373 /* ARGSUSED */
374 static int
375 ldwrite(dev_t dev, struct uio *uio, int ioflag)
376 {
377
378 return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
379 }
380
381 /* ARGSUSED */
382 static int
383 ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
384 {
385 struct ld_softc *sc;
386 int part, unit, error;
387 #ifdef __HAVE_OLD_DISKLABEL
388 struct disklabel newlabel;
389 #endif
390 struct disklabel *lp;
391
392 unit = DISKUNIT(dev);
393 part = DISKPART(dev);
394 sc = device_lookup(&ld_cd, unit);
395
396 error = disk_ioctl(&sc->sc_dk, cmd, addr, flag, l);
397 if (error != EPASSTHROUGH)
398 return (error);
399
400 error = 0;
401 switch (cmd) {
402 case DIOCGDINFO:
403 memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
404 return (0);
405
406 #ifdef __HAVE_OLD_DISKLABEL
407 case ODIOCGDINFO:
408 newlabel = *(sc->sc_dk.dk_label);
409 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
410 return ENOTTY;
411 memcpy(addr, &newlabel, sizeof(struct olddisklabel));
412 return (0);
413 #endif
414
415 case DIOCGPART:
416 ((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
417 ((struct partinfo *)addr)->part =
418 &sc->sc_dk.dk_label->d_partitions[part];
419 break;
420
421 case DIOCWDINFO:
422 case DIOCSDINFO:
423 #ifdef __HAVE_OLD_DISKLABEL
424 case ODIOCWDINFO:
425 case ODIOCSDINFO:
426
427 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
428 memset(&newlabel, 0, sizeof newlabel);
429 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
430 lp = &newlabel;
431 } else
432 #endif
433 lp = (struct disklabel *)addr;
434
435 if ((flag & FWRITE) == 0)
436 return (EBADF);
437
438 mutex_enter(&sc->sc_dk.dk_openlock);
439 sc->sc_flags |= LDF_LABELLING;
440
441 error = setdisklabel(sc->sc_dk.dk_label,
442 lp, /*sc->sc_dk.dk_openmask : */0,
443 sc->sc_dk.dk_cpulabel);
444 if (error == 0 && (cmd == DIOCWDINFO
445 #ifdef __HAVE_OLD_DISKLABEL
446 || cmd == ODIOCWDINFO
447 #endif
448 ))
449 error = writedisklabel(
450 MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
451 ldstrategy, sc->sc_dk.dk_label,
452 sc->sc_dk.dk_cpulabel);
453
454 sc->sc_flags &= ~LDF_LABELLING;
455 mutex_exit(&sc->sc_dk.dk_openlock);
456 break;
457
458 case DIOCKLABEL:
459 if ((flag & FWRITE) == 0)
460 return (EBADF);
461 if (*(int *)addr)
462 sc->sc_flags |= LDF_KLABEL;
463 else
464 sc->sc_flags &= ~LDF_KLABEL;
465 break;
466
467 case DIOCWLABEL:
468 if ((flag & FWRITE) == 0)
469 return (EBADF);
470 if (*(int *)addr)
471 sc->sc_flags |= LDF_WLABEL;
472 else
473 sc->sc_flags &= ~LDF_WLABEL;
474 break;
475
476 case DIOCGDEFLABEL:
477 ldgetdefaultlabel(sc, (struct disklabel *)addr);
478 break;
479
480 #ifdef __HAVE_OLD_DISKLABEL
481 case ODIOCGDEFLABEL:
482 ldgetdefaultlabel(sc, &newlabel);
483 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
484 return ENOTTY;
485 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
486 break;
487 #endif
488
489 case DIOCCACHESYNC:
490 /*
491 * XXX Do we really need to care about having a writable
492 * file descriptor here?
493 */
494 if ((flag & FWRITE) == 0)
495 error = EBADF;
496 else if (sc->sc_flush)
497 error = (*sc->sc_flush)(sc);
498 else
499 error = 0; /* XXX Error out instead? */
500 break;
501
502 case DIOCAWEDGE:
503 {
504 struct dkwedge_info *dkw = (void *) addr;
505
506 if ((flag & FWRITE) == 0)
507 return (EBADF);
508
509 /* If the ioctl happens here, the parent is us. */
510 strcpy(dkw->dkw_parent, sc->sc_dv.dv_xname);
511 return (dkwedge_add(dkw));
512 }
513
514 case DIOCDWEDGE:
515 {
516 struct dkwedge_info *dkw = (void *) addr;
517
518 if ((flag & FWRITE) == 0)
519 return (EBADF);
520
521 /* If the ioctl happens here, the parent is us. */
522 strcpy(dkw->dkw_parent, sc->sc_dv.dv_xname);
523 return (dkwedge_del(dkw));
524 }
525
526 case DIOCLWEDGES:
527 {
528 struct dkwedge_list *dkwl = (void *) addr;
529
530 return (dkwedge_list(&sc->sc_dk, dkwl, l));
531 }
532
533 default:
534 error = ENOTTY;
535 break;
536 }
537
538 return (error);
539 }
540
541 static void
542 ldstrategy(struct buf *bp)
543 {
544 struct ld_softc *sc;
545 struct disklabel *lp;
546 daddr_t blkno;
547 int s, part;
548
549 sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
550 part = DISKPART(bp->b_dev);
551
552 if ((sc->sc_flags & LDF_DETACH) != 0) {
553 bp->b_error = EIO;
554 goto done;
555 }
556
557 lp = sc->sc_dk.dk_label;
558
559 /*
560 * The transfer must be a whole number of blocks and the offset must
561 * not be negative.
562 */
563 if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
564 bp->b_error = EINVAL;
565 goto done;
566 }
567
568 /* If it's a null transfer, return immediately. */
569 if (bp->b_bcount == 0)
570 goto done;
571
572 /*
573 * Do bounds checking and adjust the transfer. If error, process.
574 * If past the end of partition, just return.
575 */
576 if (part != RAW_PART &&
577 bounds_check_with_label(&sc->sc_dk, bp,
578 (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) {
579 goto done;
580 }
581
582 /*
583 * Convert the block number to absolute and put it in terms
584 * of the device's logical block size.
585 */
586 if (lp->d_secsize == DEV_BSIZE)
587 blkno = bp->b_blkno;
588 else if (lp->d_secsize > DEV_BSIZE)
589 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
590 else
591 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
592
593 if (part != RAW_PART)
594 blkno += lp->d_partitions[part].p_offset;
595
596 bp->b_rawblkno = blkno;
597
598 s = splbio();
599 ldstart(sc, bp);
600 splx(s);
601 return;
602
603 done:
604 bp->b_resid = bp->b_bcount;
605 biodone(bp);
606 }
607
608 static void
609 ldstart(struct ld_softc *sc, struct buf *bp)
610 {
611 int error;
612
613 mutex_enter(&sc->sc_mutex);
614
615 if (bp != NULL)
616 BUFQ_PUT(sc->sc_bufq, bp);
617
618 while (sc->sc_queuecnt < sc->sc_maxqueuecnt) {
619 /* See if there is work to do. */
620 if ((bp = BUFQ_PEEK(sc->sc_bufq)) == NULL)
621 break;
622
623 disk_busy(&sc->sc_dk);
624 sc->sc_queuecnt++;
625
626 if (__predict_true((error = (*sc->sc_start)(sc, bp)) == 0)) {
627 /*
628 * The back-end is running the job; remove it from
629 * the queue.
630 */
631 (void) BUFQ_GET(sc->sc_bufq);
632 } else {
633 disk_unbusy(&sc->sc_dk, 0, (bp->b_flags & B_READ));
634 sc->sc_queuecnt--;
635 if (error == EAGAIN) {
636 /*
637 * Temporary resource shortage in the
638 * back-end; just defer the job until
639 * later.
640 *
641 * XXX We might consider a watchdog timer
642 * XXX to make sure we are kicked into action.
643 */
644 break;
645 } else {
646 (void) BUFQ_GET(sc->sc_bufq);
647 bp->b_error = error;
648 bp->b_resid = bp->b_bcount;
649 mutex_exit(&sc->sc_mutex);
650 biodone(bp);
651 mutex_enter(&sc->sc_mutex);
652 }
653 }
654 }
655
656 mutex_exit(&sc->sc_mutex);
657 }
658
659 void
660 lddone(struct ld_softc *sc, struct buf *bp)
661 {
662
663 if (bp->b_error != 0) {
664 diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
665 printf("\n");
666 }
667
668 disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
669 (bp->b_flags & B_READ));
670 #if NRND > 0
671 rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
672 #endif
673 biodone(bp);
674
675 mutex_enter(&sc->sc_mutex);
676 if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
677 if ((sc->sc_flags & LDF_DRAIN) != 0) {
678 sc->sc_flags &= ~LDF_DRAIN;
679 wakeup(&sc->sc_queuecnt);
680 }
681 mutex_exit(&sc->sc_mutex);
682 ldstart(sc, NULL);
683 } else
684 mutex_exit(&sc->sc_mutex);
685 }
686
687 static int
688 ldsize(dev_t dev)
689 {
690 struct ld_softc *sc;
691 int part, unit, omask, size;
692
693 unit = DISKUNIT(dev);
694 if ((sc = device_lookup(&ld_cd, unit)) == NULL)
695 return (ENODEV);
696 if ((sc->sc_flags & LDF_ENABLED) == 0)
697 return (ENODEV);
698 part = DISKPART(dev);
699
700 omask = sc->sc_dk.dk_openmask & (1 << part);
701
702 if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
703 return (-1);
704 else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
705 size = -1;
706 else
707 size = sc->sc_dk.dk_label->d_partitions[part].p_size *
708 (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
709 if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
710 return (-1);
711
712 return (size);
713 }
714
715 /*
716 * Load the label information from the specified device.
717 */
718 static void
719 ldgetdisklabel(struct ld_softc *sc)
720 {
721 const char *errstring;
722
723 ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
724
725 /* Call the generic disklabel extraction routine. */
726 errstring = readdisklabel(MAKEDISKDEV(0, device_unit(&sc->sc_dv),
727 RAW_PART), ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
728 if (errstring != NULL)
729 printf("%s: %s\n", sc->sc_dv.dv_xname, errstring);
730
731 /* In-core label now valid. */
732 sc->sc_flags |= LDF_VLABEL;
733 }
734
735 /*
736 * Construct a ficticious label.
737 */
738 static void
739 ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
740 {
741
742 memset(lp, 0, sizeof(struct disklabel));
743
744 lp->d_secsize = sc->sc_secsize;
745 lp->d_ntracks = sc->sc_nheads;
746 lp->d_nsectors = sc->sc_nsectors;
747 lp->d_ncylinders = sc->sc_ncylinders;
748 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
749 lp->d_type = DTYPE_LD;
750 strlcpy(lp->d_typename, "unknown", sizeof(lp->d_typename));
751 strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
752 lp->d_secperunit = sc->sc_secperunit;
753 lp->d_rpm = 7200;
754 lp->d_interleave = 1;
755 lp->d_flags = 0;
756
757 lp->d_partitions[RAW_PART].p_offset = 0;
758 lp->d_partitions[RAW_PART].p_size =
759 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
760 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
761 lp->d_npartitions = RAW_PART + 1;
762
763 lp->d_magic = DISKMAGIC;
764 lp->d_magic2 = DISKMAGIC;
765 lp->d_checksum = dkcksum(lp);
766 }
767
768 /*
769 * Take a dump.
770 */
771 static int
772 lddump(dev_t dev, daddr_t blkno, void *vav, size_t size)
773 {
774 char *va = vav;
775 struct ld_softc *sc;
776 struct disklabel *lp;
777 int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
778 static int dumping;
779
780 unit = DISKUNIT(dev);
781 if ((sc = device_lookup(&ld_cd, unit)) == NULL)
782 return (ENXIO);
783 if ((sc->sc_flags & LDF_ENABLED) == 0)
784 return (ENODEV);
785 if (sc->sc_dump == NULL)
786 return (ENXIO);
787
788 /* Check if recursive dump; if so, punt. */
789 if (dumping)
790 return (EFAULT);
791 dumping = 1;
792
793 /* Convert to disk sectors. Request must be a multiple of size. */
794 part = DISKPART(dev);
795 lp = sc->sc_dk.dk_label;
796 if ((size % lp->d_secsize) != 0)
797 return (EFAULT);
798 towrt = size / lp->d_secsize;
799 blkno = dbtob(blkno) / lp->d_secsize; /* blkno in DEV_BSIZE units */
800
801 nsects = lp->d_partitions[part].p_size;
802 sectoff = lp->d_partitions[part].p_offset;
803
804 /* Check transfer bounds against partition size. */
805 if ((blkno < 0) || ((blkno + towrt) > nsects))
806 return (EINVAL);
807
808 /* Offset block number to start of partition. */
809 blkno += sectoff;
810
811 /* Start dumping and return when done. */
812 maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1;
813 while (towrt > 0) {
814 nblk = min(maxblkcnt, towrt);
815
816 if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
817 return (rv);
818
819 towrt -= nblk;
820 blkno += nblk;
821 va += nblk * sc->sc_secsize;
822 }
823
824 dumping = 0;
825 return (0);
826 }
827
828 /*
829 * Adjust the size of a transfer.
830 */
831 static void
832 ldminphys(struct buf *bp)
833 {
834 struct ld_softc *sc;
835
836 sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
837
838 if (bp->b_bcount > sc->sc_maxxfer)
839 bp->b_bcount = sc->sc_maxxfer;
840 minphys(bp);
841 }
842
843 static void
844 ld_set_properties(struct ld_softc *ld)
845 {
846 prop_dictionary_t disk_info, odisk_info, geom;
847
848 disk_info = prop_dictionary_create();
849
850 geom = prop_dictionary_create();
851
852 prop_dictionary_set_uint64(geom, "sectors-per-unit",
853 ld->sc_secperunit);
854
855 prop_dictionary_set_uint32(geom, "sector-size",
856 ld->sc_secsize);
857
858 prop_dictionary_set_uint16(geom, "sectors-per-track",
859 ld->sc_nsectors);
860
861 prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
862 ld->sc_nheads);
863
864 prop_dictionary_set_uint64(geom, "cylinders-per-unit",
865 ld->sc_ncylinders);
866
867 prop_dictionary_set(disk_info, "geometry", geom);
868 prop_object_release(geom);
869
870 prop_dictionary_set(device_properties(&ld->sc_dv),
871 "disk-info", disk_info);
872
873 /*
874 * Don't release disk_info here; we keep a reference to it.
875 * disk_detach() will release it when we go away.
876 */
877
878 odisk_info = ld->sc_dk.dk_info;
879 ld->sc_dk.dk_info = disk_info;
880 if (odisk_info)
881 prop_object_release(odisk_info);
882 }
883
884 static void
885 ld_config_interrupts (struct device *d)
886 {
887 struct ld_softc *sc = (struct ld_softc *)d;
888 dkwedge_discover(&sc->sc_dk);
889 }
890