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