Home | History | Annotate | Line # | Download | only in dev
ld.c revision 1.45
      1 /*	$NetBSD: ld.c,v 1.45 2007/02/22 22:28:32 riz 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.45 2007/02/22 22:28:32 riz 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 	if ((error = lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    297 		return (error);
    298 
    299 	if (sc->sc_dk.dk_openmask == 0) {
    300 		/* Load the partition info if not already loaded. */
    301 		if ((sc->sc_flags & LDF_VLABEL) == 0)
    302 			ldgetdisklabel(sc);
    303 	}
    304 
    305 	/* Check that the partition exists. */
    306 	if (part != RAW_PART && (part >= sc->sc_dk.dk_label->d_npartitions ||
    307 	    sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    308 		error = ENXIO;
    309 		goto bad1;
    310 	}
    311 
    312 	/* Ensure only one open at a time. */
    313 	switch (fmt) {
    314 	case S_IFCHR:
    315 		sc->sc_dk.dk_copenmask |= (1 << part);
    316 		break;
    317 	case S_IFBLK:
    318 		sc->sc_dk.dk_bopenmask |= (1 << part);
    319 		break;
    320 	}
    321 	sc->sc_dk.dk_openmask =
    322 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    323 
    324 	(void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
    325 	return (0);
    326 
    327  bad1:
    328 	(void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
    329 	return (error);
    330 }
    331 
    332 /* ARGSUSED */
    333 static int
    334 ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
    335 {
    336 	struct ld_softc *sc;
    337 	int error, part, unit;
    338 
    339 	unit = DISKUNIT(dev);
    340 	part = DISKPART(dev);
    341 	sc = device_lookup(&ld_cd, unit);
    342 
    343 	if ((error = lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    344 		return (error);
    345 
    346 	switch (fmt) {
    347 	case S_IFCHR:
    348 		sc->sc_dk.dk_copenmask &= ~(1 << part);
    349 		break;
    350 	case S_IFBLK:
    351 		sc->sc_dk.dk_bopenmask &= ~(1 << part);
    352 		break;
    353 	}
    354 	sc->sc_dk.dk_openmask =
    355 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    356 
    357 	if (sc->sc_dk.dk_openmask == 0) {
    358 		if (sc->sc_flush != NULL && (*sc->sc_flush)(sc) != 0)
    359 			printf("%s: unable to flush cache\n",
    360 			    sc->sc_dv.dv_xname);
    361 		if ((sc->sc_flags & LDF_KLABEL) == 0)
    362 			sc->sc_flags &= ~LDF_VLABEL;
    363 	}
    364 
    365 	(void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
    366 	return (0);
    367 }
    368 
    369 /* ARGSUSED */
    370 static int
    371 ldread(dev_t dev, struct uio *uio, int ioflag)
    372 {
    373 
    374 	return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
    375 }
    376 
    377 /* ARGSUSED */
    378 static int
    379 ldwrite(dev_t dev, struct uio *uio, int ioflag)
    380 {
    381 
    382 	return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
    383 }
    384 
    385 /* ARGSUSED */
    386 static int
    387 ldioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, struct lwp *l)
    388 {
    389 	struct ld_softc *sc;
    390 	int part, unit, error;
    391 #ifdef __HAVE_OLD_DISKLABEL
    392 	struct disklabel newlabel;
    393 #endif
    394 	struct disklabel *lp;
    395 
    396 	unit = DISKUNIT(dev);
    397 	part = DISKPART(dev);
    398 	sc = device_lookup(&ld_cd, unit);
    399 	error = 0;
    400 
    401 	error = disk_ioctl(&sc->sc_dk, cmd, addr, flag, l);
    402 	if (error != EPASSTHROUGH)
    403 		return (error);
    404 
    405 	switch (cmd) {
    406 	case DIOCGDINFO:
    407 		memcpy(addr, sc->sc_dk.dk_label, sizeof(struct disklabel));
    408 		return (0);
    409 
    410 #ifdef __HAVE_OLD_DISKLABEL
    411 	case ODIOCGDINFO:
    412 		newlabel = *(sc->sc_dk.dk_label);
    413 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    414 			return ENOTTY;
    415 		memcpy(addr, &newlabel, sizeof(struct olddisklabel));
    416 		return (0);
    417 #endif
    418 
    419 	case DIOCGPART:
    420 		((struct partinfo *)addr)->disklab = sc->sc_dk.dk_label;
    421 		((struct partinfo *)addr)->part =
    422 		    &sc->sc_dk.dk_label->d_partitions[part];
    423 		break;
    424 
    425 	case DIOCWDINFO:
    426 	case DIOCSDINFO:
    427 #ifdef __HAVE_OLD_DISKLABEL
    428 	case ODIOCWDINFO:
    429 	case ODIOCSDINFO:
    430 
    431 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    432 			memset(&newlabel, 0, sizeof newlabel);
    433 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
    434 			lp = &newlabel;
    435 		} else
    436 #endif
    437 		lp = (struct disklabel *)addr;
    438 
    439 		if ((flag & FWRITE) == 0)
    440 			return (EBADF);
    441 
    442 		if ((error = lockmgr(&sc->sc_dk.dk_openlock, LK_EXCLUSIVE,
    443 				     NULL)) != 0)
    444 			return (error);
    445 		sc->sc_flags |= LDF_LABELLING;
    446 
    447 		error = setdisklabel(sc->sc_dk.dk_label,
    448 		    lp, /*sc->sc_dk.dk_openmask : */0,
    449 		    sc->sc_dk.dk_cpulabel);
    450 		if (error == 0 && (cmd == DIOCWDINFO
    451 #ifdef __HAVE_OLD_DISKLABEL
    452 		    || cmd == ODIOCWDINFO
    453 #endif
    454 		    ))
    455 			error = writedisklabel(
    456 			    MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
    457 			    ldstrategy, sc->sc_dk.dk_label,
    458 			    sc->sc_dk.dk_cpulabel);
    459 
    460 		sc->sc_flags &= ~LDF_LABELLING;
    461 		(void) lockmgr(&sc->sc_dk.dk_openlock, LK_RELEASE, NULL);
    462 		break;
    463 
    464 	case DIOCKLABEL:
    465 		if ((flag & FWRITE) == 0)
    466 			return (EBADF);
    467 		if (*(int *)addr)
    468 			sc->sc_flags |= LDF_KLABEL;
    469 		else
    470 			sc->sc_flags &= ~LDF_KLABEL;
    471 		break;
    472 
    473 	case DIOCWLABEL:
    474 		if ((flag & FWRITE) == 0)
    475 			return (EBADF);
    476 		if (*(int *)addr)
    477 			sc->sc_flags |= LDF_WLABEL;
    478 		else
    479 			sc->sc_flags &= ~LDF_WLABEL;
    480 		break;
    481 
    482 	case DIOCGDEFLABEL:
    483 		ldgetdefaultlabel(sc, (struct disklabel *)addr);
    484 		break;
    485 
    486 #ifdef __HAVE_OLD_DISKLABEL
    487 	case ODIOCGDEFLABEL:
    488 		ldgetdefaultlabel(sc, &newlabel);
    489 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    490 			return ENOTTY;
    491 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
    492 		break;
    493 #endif
    494 
    495 	case DIOCCACHESYNC:
    496 		/*
    497 		 * XXX Do we really need to care about having a writable
    498 		 * file descriptor here?
    499 		 */
    500 		if ((flag & FWRITE) == 0)
    501 			error = EBADF;
    502 		else if (sc->sc_flush)
    503 			error = (*sc->sc_flush)(sc);
    504 		else
    505 			error = 0;	/* XXX Error out instead? */
    506 		break;
    507 
    508 	case DIOCAWEDGE:
    509 	    {
    510 	    	struct dkwedge_info *dkw = (void *) addr;
    511 
    512 		if ((flag & FWRITE) == 0)
    513 			return (EBADF);
    514 
    515 		/* If the ioctl happens here, the parent is us. */
    516 		strcpy(dkw->dkw_parent, sc->sc_dv.dv_xname);
    517 		return (dkwedge_add(dkw));
    518 	    }
    519 
    520 	case DIOCDWEDGE:
    521 	    {
    522 	    	struct dkwedge_info *dkw = (void *) addr;
    523 
    524 		if ((flag & FWRITE) == 0)
    525 			return (EBADF);
    526 
    527 		/* If the ioctl happens here, the parent is us. */
    528 		strcpy(dkw->dkw_parent, sc->sc_dv.dv_xname);
    529 		return (dkwedge_del(dkw));
    530 	    }
    531 
    532 	case DIOCLWEDGES:
    533 	    {
    534 	    	struct dkwedge_list *dkwl = (void *) addr;
    535 
    536 		return (dkwedge_list(&sc->sc_dk, dkwl, l));
    537 	    }
    538 
    539 	default:
    540 		error = ENOTTY;
    541 		break;
    542 	}
    543 
    544 	return (error);
    545 }
    546 
    547 static void
    548 ldstrategy(struct buf *bp)
    549 {
    550 	struct ld_softc *sc;
    551 	struct disklabel *lp;
    552 	daddr_t blkno;
    553 	int s, part;
    554 
    555 	sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
    556 	part = DISKPART(bp->b_dev);
    557 
    558 	if ((sc->sc_flags & LDF_DETACH) != 0) {
    559 		bp->b_error = EIO;
    560 		goto bad;
    561 	}
    562 
    563 	lp = sc->sc_dk.dk_label;
    564 
    565 	/*
    566 	 * The transfer must be a whole number of blocks and the offset must
    567 	 * not be negative.
    568 	 */
    569 	if ((bp->b_bcount % lp->d_secsize) != 0 || bp->b_blkno < 0) {
    570 		bp->b_error = EINVAL;
    571 		goto bad;
    572 	}
    573 
    574 	/* If it's a null transfer, return immediately. */
    575 	if (bp->b_bcount == 0)
    576 		goto done;
    577 
    578 	/*
    579 	 * Do bounds checking and adjust the transfer.  If error, process.
    580 	 * If past the end of partition, just return.
    581 	 */
    582 	if (part != RAW_PART &&
    583 	    bounds_check_with_label(&sc->sc_dk, bp,
    584 	    (sc->sc_flags & (LDF_WLABEL | LDF_LABELLING)) != 0) <= 0) {
    585 		goto done;
    586 	}
    587 
    588 	/*
    589 	 * Convert the block number to absolute and put it in terms
    590 	 * of the device's logical block size.
    591 	 */
    592 	if (lp->d_secsize == DEV_BSIZE)
    593 		blkno = bp->b_blkno;
    594 	else if (lp->d_secsize > DEV_BSIZE)
    595 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    596 	else
    597 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    598 
    599 	if (part != RAW_PART)
    600 		blkno += lp->d_partitions[part].p_offset;
    601 
    602 	bp->b_rawblkno = blkno;
    603 
    604 	s = splbio();
    605 	ldstart(sc, bp);
    606 	splx(s);
    607 	return;
    608 
    609  bad:
    610 	bp->b_flags |= B_ERROR;
    611  done:
    612 	bp->b_resid = bp->b_bcount;
    613 	biodone(bp);
    614 }
    615 
    616 static void
    617 ldstart(struct ld_softc *sc, struct buf *bp)
    618 {
    619 	int error;
    620 
    621 	mutex_enter(&sc->sc_mutex);
    622 
    623 	if (bp != NULL)
    624 		BUFQ_PUT(sc->sc_bufq, bp);
    625 
    626 	while (sc->sc_queuecnt < sc->sc_maxqueuecnt) {
    627 		/* See if there is work to do. */
    628 		if ((bp = BUFQ_PEEK(sc->sc_bufq)) == NULL)
    629 			break;
    630 
    631 		disk_busy(&sc->sc_dk);
    632 		sc->sc_queuecnt++;
    633 
    634 		if (__predict_true((error = (*sc->sc_start)(sc, bp)) == 0)) {
    635 			/*
    636 			 * The back-end is running the job; remove it from
    637 			 * the queue.
    638 			 */
    639 			(void) BUFQ_GET(sc->sc_bufq);
    640 		} else  {
    641 			disk_unbusy(&sc->sc_dk, 0, (bp->b_flags & B_READ));
    642 			sc->sc_queuecnt--;
    643 			if (error == EAGAIN) {
    644 				/*
    645 				 * Temporary resource shortage in the
    646 				 * back-end; just defer the job until
    647 				 * later.
    648 				 *
    649 				 * XXX We might consider a watchdog timer
    650 				 * XXX to make sure we are kicked into action.
    651 				 */
    652 				break;
    653 			} else {
    654 				(void) BUFQ_GET(sc->sc_bufq);
    655 				bp->b_error = error;
    656 				bp->b_flags |= B_ERROR;
    657 				bp->b_resid = bp->b_bcount;
    658 				mutex_exit(&sc->sc_mutex);
    659 				biodone(bp);
    660 				mutex_enter(&sc->sc_mutex);
    661 			}
    662 		}
    663 	}
    664 
    665 	mutex_exit(&sc->sc_mutex);
    666 }
    667 
    668 void
    669 lddone(struct ld_softc *sc, struct buf *bp)
    670 {
    671 
    672 	if ((bp->b_flags & B_ERROR) != 0) {
    673 		diskerr(bp, "ld", "error", LOG_PRINTF, 0, sc->sc_dk.dk_label);
    674 		printf("\n");
    675 	}
    676 
    677 	disk_unbusy(&sc->sc_dk, bp->b_bcount - bp->b_resid,
    678 	    (bp->b_flags & B_READ));
    679 #if NRND > 0
    680 	rnd_add_uint32(&sc->sc_rnd_source, bp->b_rawblkno);
    681 #endif
    682 	biodone(bp);
    683 
    684 	mutex_enter(&sc->sc_mutex);
    685 	if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
    686 		if ((sc->sc_flags & LDF_DRAIN) != 0) {
    687 			sc->sc_flags &= ~LDF_DRAIN;
    688 			wakeup(&sc->sc_queuecnt);
    689 		}
    690 		mutex_exit(&sc->sc_mutex);
    691 		ldstart(sc, NULL);
    692 	} else
    693 		mutex_exit(&sc->sc_mutex);
    694 }
    695 
    696 static int
    697 ldsize(dev_t dev)
    698 {
    699 	struct ld_softc *sc;
    700 	int part, unit, omask, size;
    701 
    702 	unit = DISKUNIT(dev);
    703 	if ((sc = device_lookup(&ld_cd, unit)) == NULL)
    704 		return (ENODEV);
    705 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    706 		return (ENODEV);
    707 	part = DISKPART(dev);
    708 
    709 	omask = sc->sc_dk.dk_openmask & (1 << part);
    710 
    711 	if (omask == 0 && ldopen(dev, 0, S_IFBLK, NULL) != 0)
    712 		return (-1);
    713 	else if (sc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    714 		size = -1;
    715 	else
    716 		size = sc->sc_dk.dk_label->d_partitions[part].p_size *
    717 		    (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    718 	if (omask == 0 && ldclose(dev, 0, S_IFBLK, NULL) != 0)
    719 		return (-1);
    720 
    721 	return (size);
    722 }
    723 
    724 /*
    725  * Load the label information from the specified device.
    726  */
    727 static void
    728 ldgetdisklabel(struct ld_softc *sc)
    729 {
    730 	const char *errstring;
    731 
    732 	ldgetdefaultlabel(sc, sc->sc_dk.dk_label);
    733 
    734 	/* Call the generic disklabel extraction routine. */
    735 	errstring = readdisklabel(MAKEDISKDEV(0, device_unit(&sc->sc_dv),
    736 	    RAW_PART), ldstrategy, sc->sc_dk.dk_label, sc->sc_dk.dk_cpulabel);
    737 	if (errstring != NULL)
    738 		printf("%s: %s\n", sc->sc_dv.dv_xname, errstring);
    739 
    740 	/* In-core label now valid. */
    741 	sc->sc_flags |= LDF_VLABEL;
    742 }
    743 
    744 /*
    745  * Construct a ficticious label.
    746  */
    747 static void
    748 ldgetdefaultlabel(struct ld_softc *sc, struct disklabel *lp)
    749 {
    750 
    751 	memset(lp, 0, sizeof(struct disklabel));
    752 
    753 	lp->d_secsize = sc->sc_secsize;
    754 	lp->d_ntracks = sc->sc_nheads;
    755 	lp->d_nsectors = sc->sc_nsectors;
    756 	lp->d_ncylinders = sc->sc_ncylinders;
    757 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    758 	lp->d_type = DTYPE_LD;
    759 	strlcpy(lp->d_typename, "unknown", sizeof(lp->d_typename));
    760 	strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
    761 	lp->d_secperunit = sc->sc_secperunit;
    762 	lp->d_rpm = 7200;
    763 	lp->d_interleave = 1;
    764 	lp->d_flags = 0;
    765 
    766 	lp->d_partitions[RAW_PART].p_offset = 0;
    767 	lp->d_partitions[RAW_PART].p_size =
    768 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    769 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    770 	lp->d_npartitions = RAW_PART + 1;
    771 
    772 	lp->d_magic = DISKMAGIC;
    773 	lp->d_magic2 = DISKMAGIC;
    774 	lp->d_checksum = dkcksum(lp);
    775 }
    776 
    777 /*
    778  * Take a dump.
    779  */
    780 static int
    781 lddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
    782 {
    783 	struct ld_softc *sc;
    784 	struct disklabel *lp;
    785 	int unit, part, nsects, sectoff, towrt, nblk, maxblkcnt, rv;
    786 	static int dumping;
    787 
    788 	unit = DISKUNIT(dev);
    789 	if ((sc = device_lookup(&ld_cd, unit)) == NULL)
    790 		return (ENXIO);
    791 	if ((sc->sc_flags & LDF_ENABLED) == 0)
    792 		return (ENODEV);
    793 	if (sc->sc_dump == NULL)
    794 		return (ENXIO);
    795 
    796 	/* Check if recursive dump; if so, punt. */
    797 	if (dumping)
    798 		return (EFAULT);
    799 	dumping = 1;
    800 
    801 	/* Convert to disk sectors.  Request must be a multiple of size. */
    802 	part = DISKPART(dev);
    803 	lp = sc->sc_dk.dk_label;
    804 	if ((size % lp->d_secsize) != 0)
    805 		return (EFAULT);
    806 	towrt = size / lp->d_secsize;
    807 	blkno = dbtob(blkno) / lp->d_secsize;	/* blkno in DEV_BSIZE units */
    808 
    809 	nsects = lp->d_partitions[part].p_size;
    810 	sectoff = lp->d_partitions[part].p_offset;
    811 
    812 	/* Check transfer bounds against partition size. */
    813 	if ((blkno < 0) || ((blkno + towrt) > nsects))
    814 		return (EINVAL);
    815 
    816 	/* Offset block number to start of partition. */
    817 	blkno += sectoff;
    818 
    819 	/* Start dumping and return when done. */
    820 	maxblkcnt = sc->sc_maxxfer / sc->sc_secsize - 1;
    821 	while (towrt > 0) {
    822 		nblk = min(maxblkcnt, towrt);
    823 
    824 		if ((rv = (*sc->sc_dump)(sc, va, blkno, nblk)) != 0)
    825 			return (rv);
    826 
    827 		towrt -= nblk;
    828 		blkno += nblk;
    829 		va += nblk * sc->sc_secsize;
    830 	}
    831 
    832 	dumping = 0;
    833 	return (0);
    834 }
    835 
    836 /*
    837  * Adjust the size of a transfer.
    838  */
    839 static void
    840 ldminphys(struct buf *bp)
    841 {
    842 	struct ld_softc *sc;
    843 
    844 	sc = device_lookup(&ld_cd, DISKUNIT(bp->b_dev));
    845 
    846 	if (bp->b_bcount > sc->sc_maxxfer)
    847 		bp->b_bcount = sc->sc_maxxfer;
    848 	minphys(bp);
    849 }
    850 
    851 static void
    852 ld_set_properties(struct ld_softc *ld)
    853 {
    854 	prop_dictionary_t disk_info, odisk_info, geom;
    855 
    856 	disk_info = prop_dictionary_create();
    857 
    858 	geom = prop_dictionary_create();
    859 
    860 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
    861 	    ld->sc_secperunit);
    862 
    863 	prop_dictionary_set_uint32(geom, "sector-size",
    864 	    ld->sc_secsize);
    865 
    866 	prop_dictionary_set_uint16(geom, "sectors-per-track",
    867 	    ld->sc_nsectors);
    868 
    869 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
    870 	    ld->sc_nheads);
    871 
    872 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
    873 	    ld->sc_ncylinders);
    874 
    875 	prop_dictionary_set(disk_info, "geometry", geom);
    876 	prop_object_release(geom);
    877 
    878 	prop_dictionary_set(device_properties(&ld->sc_dv),
    879 	    "disk-info", disk_info);
    880 
    881 	/*
    882 	 * Don't release disk_info here; we keep a reference to it.
    883 	 * disk_detach() will release it when we go away.
    884 	 */
    885 
    886 	odisk_info = ld->sc_dk.dk_info;
    887 	ld->sc_dk.dk_info = disk_info;
    888 	if (odisk_info)
    889 		prop_object_release(odisk_info);
    890 }
    891 
    892 static void
    893 ld_config_interrupts (struct device *d)
    894 {
    895 	struct ld_softc *sc = (struct ld_softc *)d;
    896 	dkwedge_discover(&sc->sc_dk);
    897 }
    898