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