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