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