Home | History | Annotate | Line # | Download | only in scsipi
sd.c revision 1.151.2.5
      1 /*	$NetBSD: sd.c,v 1.151.2.5 2000/12/08 09:12:41 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by 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  * Originally written by Julian Elischer (julian (at) dialix.oz.au)
     41  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     42  *
     43  * TRW Financial Systems, in accordance with their agreement with Carnegie
     44  * Mellon University, makes this software available to CMU to distribute
     45  * or use in any manner that they see fit as long as this message is kept with
     46  * the software. For this reason TFS also grants any other persons or
     47  * organisations permission to use or modify this software.
     48  *
     49  * TFS supplies this software to be publicly redistributed
     50  * on the understanding that TFS is not responsible for the correct
     51  * functioning of this software in any circumstances.
     52  *
     53  * Ported to run under 386BSD by Julian Elischer (julian (at) dialix.oz.au) Sept 1992
     54  */
     55 
     56 #include "opt_scsi.h"
     57 #include "rnd.h"
     58 
     59 #include <sys/types.h>
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/kernel.h>
     63 #include <sys/file.h>
     64 #include <sys/stat.h>
     65 #include <sys/ioctl.h>
     66 #include <sys/scsiio.h>
     67 #include <sys/buf.h>
     68 #include <sys/uio.h>
     69 #include <sys/malloc.h>
     70 #include <sys/errno.h>
     71 #include <sys/device.h>
     72 #include <sys/disklabel.h>
     73 #include <sys/disk.h>
     74 #include <sys/proc.h>
     75 #include <sys/conf.h>
     76 #include <sys/vnode.h>
     77 #if NRND > 0
     78 #include <sys/rnd.h>
     79 #endif
     80 
     81 #include <dev/scsipi/scsipi_all.h>
     82 #include <dev/scsipi/scsi_all.h>
     83 #include <dev/scsipi/scsipi_disk.h>
     84 #include <dev/scsipi/scsi_disk.h>
     85 #include <dev/scsipi/scsiconf.h>
     86 #include <dev/scsipi/sdvar.h>
     87 
     88 #include "sd.h"		/* NSD_SCSIBUS and NSD_ATAPIBUS come from here */
     89 
     90 #define	SDUNIT(dev)			DISKUNIT(dev)
     91 #define	SDPART(dev)			DISKPART(dev)
     92 #define	SDMINOR(unit, part)		DISKMINOR(unit, part)
     93 #define	MAKESDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
     94 
     95 #define	SDLABELDEV(dev)	(MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
     96 
     97 int	sdlock __P((struct sd_softc *));
     98 void	sdunlock __P((struct sd_softc *));
     99 void	sdminphys __P((struct buf *));
    100 void	sdgetdefaultlabel __P((struct sd_softc *, struct disklabel *));
    101 void	sdgetdisklabel __P((struct sd_softc *));
    102 void	sdstart __P((struct scsipi_periph *));
    103 void	sddone __P((struct scsipi_xfer *));
    104 void	sd_shutdown __P((void *));
    105 int	sd_reassign_blocks __P((struct sd_softc *, u_long));
    106 int	sd_interpret_sense __P((struct scsipi_xfer *));
    107 
    108 extern struct cfdriver sd_cd;
    109 
    110 struct dkdriver sddkdriver = { sdstrategy };
    111 
    112 const struct scsipi_periphsw sd_switch = {
    113 	sd_interpret_sense,	/* check our error handler first */
    114 	sdstart,		/* have a queue, served by this */
    115 	NULL,			/* have no async handler */
    116 	sddone,			/* deal with stats at interrupt time */
    117 };
    118 
    119 /*
    120  * Attach routine common to atapi & scsi.
    121  */
    122 void
    123 sdattach(parent, sd, periph, ops)
    124 	struct device *parent;
    125 	struct sd_softc *sd;
    126 	struct scsipi_periph *periph;
    127 	const struct sd_ops *ops;
    128 {
    129 	int error, result;
    130 	struct disk_parms *dp = &sd->params;
    131 	char pbuf[9];
    132 
    133 	SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: "));
    134 
    135 	BUFQ_INIT(&sd->buf_queue);
    136 
    137 	/*
    138 	 * Store information needed to contact our base driver
    139 	 */
    140 	sd->sc_periph = periph;
    141 	sd->sc_ops = ops;
    142 
    143 	periph->periph_dev = &sd->sc_dev;
    144 	periph->periph_switch = &sd_switch;
    145 
    146         /*
    147          * Increase our openings to the maximum-per-periph
    148          * supported by the adapter.  This will either be
    149          * clamped down or grown by the adapter if necessary.
    150          */
    151 	periph->periph_openings =
    152 	    SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel);
    153 	periph->periph_flags |= PERIPH_GROW_OPENINGS;
    154 
    155 	/*
    156 	 * Initialize and attach the disk structure.
    157 	 */
    158 	sd->sc_dk.dk_driver = &sddkdriver;
    159 	sd->sc_dk.dk_name = sd->sc_dev.dv_xname;
    160 	disk_attach(&sd->sc_dk);
    161 
    162 #ifdef __BROKEN_DK_ESTABLISH
    163 	dk_establish(&sd->sc_dk, &sd->sc_dev);		/* XXX */
    164 #endif
    165 
    166 	/*
    167 	 * Use the subdriver to request information regarding the drive.
    168 	 */
    169 	printf("\n");
    170 
    171 	error = scsipi_start(periph, SSS_START,
    172 	    XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
    173 	    XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT);
    174 
    175 	if (error)
    176 		result = SDGP_RESULT_OFFLINE;
    177 	else
    178 		result = (*sd->sc_ops->sdo_get_parms)(sd, &sd->params,
    179 		    XS_CTL_DISCOVERY);
    180 	printf("%s: ", sd->sc_dev.dv_xname);
    181 	switch (result) {
    182 	case SDGP_RESULT_OK:
    183 		format_bytes(pbuf, sizeof(pbuf),
    184 		    (u_int64_t)dp->disksize * dp->blksize);
    185 	        printf(
    186 		"%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %ld sectors",
    187 		    pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize,
    188 		    dp->disksize);
    189 		break;
    190 
    191 	case SDGP_RESULT_OFFLINE:
    192 		printf("drive offline");
    193 		break;
    194 
    195 	case SDGP_RESULT_UNFORMATTED:
    196 		printf("unformatted media");
    197 		break;
    198 
    199 #ifdef DIAGNOSTIC
    200 	default:
    201 		panic("sdattach: unknown result from get_parms");
    202 		break;
    203 #endif
    204 	}
    205 	printf("\n");
    206 
    207 	/*
    208 	 * Establish a shutdown hook so that we can ensure that
    209 	 * our data has actually made it onto the platter at
    210 	 * shutdown time.  Note that this relies on the fact
    211 	 * that the shutdown hook code puts us at the head of
    212 	 * the list (thus guaranteeing that our hook runs before
    213 	 * our ancestors').
    214 	 */
    215 	if ((sd->sc_sdhook =
    216 	    shutdownhook_establish(sd_shutdown, sd)) == NULL)
    217 		printf("%s: WARNING: unable to establish shutdown hook\n",
    218 		    sd->sc_dev.dv_xname);
    219 
    220 #if NRND > 0
    221 	/*
    222 	 * attach the device into the random source list
    223 	 */
    224 	rnd_attach_source(&sd->rnd_source, sd->sc_dev.dv_xname,
    225 			  RND_TYPE_DISK, 0);
    226 #endif
    227 }
    228 
    229 int
    230 sdactivate(self, act)
    231 	struct device *self;
    232 	enum devact act;
    233 {
    234 	int rv = 0;
    235 
    236 	switch (act) {
    237 	case DVACT_ACTIVATE:
    238 		rv = EOPNOTSUPP;
    239 		break;
    240 
    241 	case DVACT_DEACTIVATE:
    242 		/*
    243 		 * Nothing to do; we key off the device's DVF_ACTIVE.
    244 		 */
    245 		break;
    246 	}
    247 	return (rv);
    248 }
    249 
    250 int
    251 sddetach(self, flags)
    252 	struct device *self;
    253 	int flags;
    254 {
    255 	struct sd_softc *sd = (struct sd_softc *) self;
    256 	struct buf *bp;
    257 	int s, bmaj, cmaj, mn;
    258 
    259 	/* locate the major number */
    260 	for (bmaj = 0; bmaj <= nblkdev; bmaj++)
    261 		if (bdevsw[bmaj].d_open == sdopen)
    262 			break;
    263 	for (cmaj = 0; cmaj <= nchrdev; cmaj++)
    264 		if (cdevsw[cmaj].d_open == sdopen)
    265 			break;
    266 
    267 	s = splbio();
    268 
    269 	/* Kill off any queued buffers. */
    270 	while ((bp = BUFQ_FIRST(&sd->buf_queue)) != NULL) {
    271 		BUFQ_REMOVE(&sd->buf_queue, bp);
    272 		bp->b_error = EIO;
    273 		bp->b_flags |= B_ERROR;
    274 		bp->b_resid = bp->b_bcount;
    275 		biodone(bp);
    276 	}
    277 
    278 	/* Kill off any pending commands. */
    279 	scsipi_kill_pending(sd->sc_periph);
    280 
    281 	splx(s);
    282 
    283 	/* Nuke the vnodes for any open instances */
    284 	mn = SDMINOR(self->dv_unit, 0);
    285 	vdevgone(bmaj, mn, mn + (MAXPARTITIONS - 1), VBLK);
    286 	vdevgone(cmaj, mn, mn + (MAXPARTITIONS - 1), VCHR);
    287 
    288 	/* Detach from the disk list. */
    289 	disk_detach(&sd->sc_dk);
    290 
    291 	/* Get rid of the shutdown hook. */
    292 	shutdownhook_disestablish(sd->sc_sdhook);
    293 
    294 #if NRND > 0
    295 	/* Unhook the entropy source. */
    296 	rnd_detach_source(&sd->rnd_source);
    297 #endif
    298 
    299 	return (0);
    300 }
    301 
    302 /*
    303  * Wait interruptibly for an exclusive lock.
    304  *
    305  * XXX
    306  * Several drivers do this; it should be abstracted and made MP-safe.
    307  */
    308 int
    309 sdlock(sd)
    310 	struct sd_softc *sd;
    311 {
    312 	int error;
    313 
    314 	while ((sd->flags & SDF_LOCKED) != 0) {
    315 		sd->flags |= SDF_WANTED;
    316 		if ((error = tsleep(sd, PRIBIO | PCATCH, "sdlck", 0)) != 0)
    317 			return (error);
    318 	}
    319 	sd->flags |= SDF_LOCKED;
    320 	return (0);
    321 }
    322 
    323 /*
    324  * Unlock and wake up any waiters.
    325  */
    326 void
    327 sdunlock(sd)
    328 	struct sd_softc *sd;
    329 {
    330 
    331 	sd->flags &= ~SDF_LOCKED;
    332 	if ((sd->flags & SDF_WANTED) != 0) {
    333 		sd->flags &= ~SDF_WANTED;
    334 		wakeup(sd);
    335 	}
    336 }
    337 
    338 /*
    339  * open the device. Make sure the partition info is a up-to-date as can be.
    340  */
    341 int
    342 sdopen(dev, flag, fmt, p)
    343 	dev_t dev;
    344 	int flag, fmt;
    345 	struct proc *p;
    346 {
    347 	struct sd_softc *sd;
    348 	struct scsipi_periph *periph;
    349 	struct scsipi_adapter *adapt;
    350 	int unit, part;
    351 	int error;
    352 
    353 	unit = SDUNIT(dev);
    354 	if (unit >= sd_cd.cd_ndevs)
    355 		return (ENXIO);
    356 	sd = sd_cd.cd_devs[unit];
    357 	if (sd == NULL)
    358 		return (ENXIO);
    359 
    360 	if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    361 		return (ENODEV);
    362 
    363 	periph = sd->sc_periph;
    364 	adapt = periph->periph_channel->chan_adapter;
    365 	part = SDPART(dev);
    366 
    367 	SC_DEBUG(periph, SCSIPI_DB1,
    368 	    ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
    369 	    sd_cd.cd_ndevs, part));
    370 
    371 	/*
    372 	 * If this is the first open of this device, add a reference
    373 	 * to the adapter.
    374 	 */
    375 	if (sd->sc_dk.dk_openmask == 0 &&
    376 	    (error = scsipi_adapter_addref(adapt)) != 0)
    377 		return (error);
    378 
    379 	if ((error = sdlock(sd)) != 0)
    380 		goto bad4;
    381 
    382 	if ((periph->periph_flags & PERIPH_OPEN) != 0) {
    383 		/*
    384 		 * If any partition is open, but the disk has been invalidated,
    385 		 * disallow further opens of non-raw partition
    386 		 */
    387 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
    388 		    (part != RAW_PART || fmt != S_IFCHR)) {
    389 			error = EIO;
    390 			goto bad3;
    391 		}
    392 	} else {
    393 		/* Check that it is still responding and ok. */
    394 		error = scsipi_test_unit_ready(periph,
    395 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
    396 		    XS_CTL_IGNORE_NOT_READY);
    397 		if (error)
    398 			goto bad3;
    399 
    400 		/*
    401 		 * Start the pack spinning if necessary. Always allow the
    402 		 * raw parition to be opened, for raw IOCTLs. Data transfers
    403 		 * will check for SDEV_MEDIA_LOADED.
    404 		 */
    405 		error = scsipi_start(periph, SSS_START,
    406 		    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    407 		    XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT);
    408 		if (error) {
    409 			if (part != RAW_PART || fmt != S_IFCHR)
    410 				goto bad3;
    411 			else
    412 				goto out;
    413 		}
    414 
    415 		periph->periph_flags |= PERIPH_OPEN;
    416 
    417 		/* Lock the pack in. */
    418 		error = scsipi_prevent(periph, PR_PREVENT,
    419 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE);
    420 		if (error)
    421 			goto bad;
    422 
    423 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    424 			periph->periph_flags |= PERIPH_MEDIA_LOADED;
    425 
    426 			/*
    427 			 * Load the physical device parameters.
    428 			 *
    429 			 * Note that if media is present but unformatted,
    430 			 * we allow the open (so that it can be formatted!).
    431 			 * The drive should refuse real I/O, if the media is
    432 			 * unformatted.
    433 			 */
    434 			if ((*sd->sc_ops->sdo_get_parms)(sd, &sd->params,
    435 			    0) == SDGP_RESULT_OFFLINE) {
    436 				error = ENXIO;
    437 				goto bad2;
    438 			}
    439 			SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
    440 
    441 			/* Load the partition info if not already loaded. */
    442 			sdgetdisklabel(sd);
    443 			SC_DEBUG(periph, SCSIPI_DB3, ("Disklabel loaded "));
    444 		}
    445 	}
    446 
    447 	/* Check that the partition exists. */
    448 	if (part != RAW_PART &&
    449 	    (part >= sd->sc_dk.dk_label->d_npartitions ||
    450 	     sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    451 		error = ENXIO;
    452 		goto bad;
    453 	}
    454 
    455 out:	/* Insure only one open at a time. */
    456 	switch (fmt) {
    457 	case S_IFCHR:
    458 		sd->sc_dk.dk_copenmask |= (1 << part);
    459 		break;
    460 	case S_IFBLK:
    461 		sd->sc_dk.dk_bopenmask |= (1 << part);
    462 		break;
    463 	}
    464 	sd->sc_dk.dk_openmask =
    465 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
    466 
    467 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
    468 	sdunlock(sd);
    469 	return (0);
    470 
    471 bad2:
    472 	periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    473 
    474 bad:
    475 	if (sd->sc_dk.dk_openmask == 0) {
    476 		scsipi_prevent(periph, PR_ALLOW,
    477 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE);
    478 		periph->periph_flags &= ~PERIPH_OPEN;
    479 	}
    480 
    481 bad3:
    482 	sdunlock(sd);
    483 bad4:
    484 	if (sd->sc_dk.dk_openmask == 0)
    485 		scsipi_adapter_delref(adapt);
    486 	return (error);
    487 }
    488 
    489 /*
    490  * close the device.. only called if we are the LAST occurence of an open
    491  * device.  Convenient now but usually a pain.
    492  */
    493 int
    494 sdclose(dev, flag, fmt, p)
    495 	dev_t dev;
    496 	int flag, fmt;
    497 	struct proc *p;
    498 {
    499 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
    500 	struct scsipi_periph *periph = sd->sc_periph;
    501 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    502 	int part = SDPART(dev);
    503 	int error;
    504 
    505 	if ((error = sdlock(sd)) != 0)
    506 		return (error);
    507 
    508 	switch (fmt) {
    509 	case S_IFCHR:
    510 		sd->sc_dk.dk_copenmask &= ~(1 << part);
    511 		break;
    512 	case S_IFBLK:
    513 		sd->sc_dk.dk_bopenmask &= ~(1 << part);
    514 		break;
    515 	}
    516 	sd->sc_dk.dk_openmask =
    517 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
    518 
    519 	if (sd->sc_dk.dk_openmask == 0) {
    520 		/*
    521 		 * If the disk cache needs flushing, and the disk supports
    522 		 * it, do it now.
    523 		 */
    524 		if ((sd->flags & SDF_DIRTY) != 0 &&
    525 		    sd->sc_ops->sdo_flush != NULL) {
    526 			if ((*sd->sc_ops->sdo_flush)(sd, 0)) {
    527 				printf("%s: cache synchronization failed\n",
    528 				    sd->sc_dev.dv_xname);
    529 				sd->flags &= ~SDF_FLUSHING;
    530 			} else
    531 				sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    532 		}
    533 
    534 		if (! (periph->periph_flags & PERIPH_KEEP_LABEL))
    535 			periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    536 
    537 		scsipi_wait_drain(periph);
    538 
    539 		scsipi_prevent(periph, PR_ALLOW,
    540 		    XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
    541 		periph->periph_flags &= ~PERIPH_OPEN;
    542 
    543 		scsipi_wait_drain(periph);
    544 
    545 		scsipi_adapter_delref(adapt);
    546 	}
    547 
    548 	sdunlock(sd);
    549 	return (0);
    550 }
    551 
    552 /*
    553  * Actually translate the requested transfer into one the physical driver
    554  * can understand.  The transfer is described by a buf and will include
    555  * only one physical transfer.
    556  */
    557 void
    558 sdstrategy(bp)
    559 	struct buf *bp;
    560 {
    561 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
    562 	struct scsipi_periph *periph = sd->sc_periph;
    563 	struct disklabel *lp;
    564 	daddr_t blkno;
    565 	int s;
    566 	boolean_t sector_aligned;
    567 
    568 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy "));
    569 	SC_DEBUG(sd->sc_periph, SCSIPI_DB1,
    570 	    ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
    571 	/*
    572 	 * If the device has been made invalid, error out
    573 	 */
    574 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 ||
    575 	    (sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
    576 		if (periph->periph_flags & PERIPH_OPEN)
    577 			bp->b_error = EIO;
    578 		else
    579 			bp->b_error = ENODEV;
    580 		goto bad;
    581 	}
    582 
    583 	lp = sd->sc_dk.dk_label;
    584 
    585 	/*
    586 	 * The transfer must be a whole number of blocks, offset must not be
    587 	 * negative.
    588 	 */
    589 	if (lp->d_secsize == DEV_BSIZE) {
    590 		sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0;
    591 	} else {
    592 		sector_aligned = (bp->b_bcount % lp->d_secsize) == 0;
    593 	}
    594 	if (!sector_aligned || bp->b_blkno < 0) {
    595 		bp->b_error = EINVAL;
    596 		goto bad;
    597 	}
    598 	/*
    599 	 * If it's a null transfer, return immediatly
    600 	 */
    601 	if (bp->b_bcount == 0)
    602 		goto done;
    603 
    604 	/*
    605 	 * Do bounds checking, adjust transfer. if error, process.
    606 	 * If end of partition, just return.
    607 	 */
    608 	if (SDPART(bp->b_dev) != RAW_PART &&
    609 	    bounds_check_with_label(bp, lp,
    610 	    (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
    611 		goto done;
    612 
    613 	/*
    614 	 * Now convert the block number to absolute and put it in
    615 	 * terms of the device's logical block size.
    616 	 */
    617 	if (lp->d_secsize == DEV_BSIZE)
    618 		blkno = bp->b_blkno;
    619 	else if (lp->d_secsize > DEV_BSIZE)
    620 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    621 	else
    622 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    623 
    624 	if (SDPART(bp->b_dev) != RAW_PART)
    625 		blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset;
    626 
    627 	bp->b_rawblkno = blkno;
    628 
    629 	s = splbio();
    630 
    631 	/*
    632 	 * Place it in the queue of disk activities for this disk.
    633 	 *
    634 	 * XXX Only do disksort() if the current operating mode does not
    635 	 * XXX include tagged queueing.
    636 	 */
    637 	disksort_blkno(&sd->buf_queue, bp);
    638 
    639 	/*
    640 	 * Tell the device to get going on the transfer if it's
    641 	 * not doing anything, otherwise just wait for completion
    642 	 */
    643 	sdstart(sd->sc_periph);
    644 
    645 	splx(s);
    646 	return;
    647 
    648 bad:
    649 	bp->b_flags |= B_ERROR;
    650 done:
    651 	/*
    652 	 * Correctly set the buf to indicate a completed xfer
    653 	 */
    654 	bp->b_resid = bp->b_bcount;
    655 	biodone(bp);
    656 }
    657 
    658 /*
    659  * sdstart looks to see if there is a buf waiting for the device
    660  * and that the device is not already busy. If both are true,
    661  * It dequeues the buf and creates a scsi command to perform the
    662  * transfer in the buf. The transfer request will call scsipi_done
    663  * on completion, which will in turn call this routine again
    664  * so that the next queued transfer is performed.
    665  * The bufs are queued by the strategy routine (sdstrategy)
    666  *
    667  * This routine is also called after other non-queued requests
    668  * have been made of the scsi driver, to ensure that the queue
    669  * continues to be drained.
    670  *
    671  * must be called at the correct (highish) spl level
    672  * sdstart() is called at splbio from sdstrategy and scsipi_done
    673  */
    674 void
    675 sdstart(periph)
    676 	struct scsipi_periph *periph;
    677 {
    678 	struct sd_softc *sd = (void *)periph->periph_dev;
    679 	struct disklabel *lp = sd->sc_dk.dk_label;
    680 	struct buf *bp = 0;
    681 	struct scsipi_rw_big cmd_big;
    682 #if NSD_SCSIBUS > 0
    683 	struct scsi_rw cmd_small;
    684 #endif
    685 	struct scsipi_generic *cmdp;
    686 	int nblks, cmdlen, error, flags;
    687 
    688 	SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
    689 	/*
    690 	 * Check if the device has room for another command
    691 	 */
    692 	while (periph->periph_active < periph->periph_openings) {
    693 		/*
    694 		 * there is excess capacity, but a special waits
    695 		 * It'll need the adapter as soon as we clear out of the
    696 		 * way and let it run (user level wait).
    697 		 */
    698 		if (periph->periph_flags & PERIPH_WAITING) {
    699 			periph->periph_flags &= ~PERIPH_WAITING;
    700 			wakeup((caddr_t)periph);
    701 			return;
    702 		}
    703 
    704 		/*
    705 		 * See if there is a buf with work for us to do..
    706 		 */
    707 		if ((bp = BUFQ_FIRST(&sd->buf_queue)) == NULL)
    708 			return;
    709 		BUFQ_REMOVE(&sd->buf_queue, bp);
    710 
    711 		/*
    712 		 * If the device has become invalid, abort all the
    713 		 * reads and writes until all files have been closed and
    714 		 * re-opened
    715 		 */
    716 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    717 			bp->b_error = EIO;
    718 			bp->b_flags |= B_ERROR;
    719 			bp->b_resid = bp->b_bcount;
    720 			biodone(bp);
    721 			continue;
    722 		}
    723 
    724 		/*
    725 		 * We have a buf, now we should make a command.
    726 		 */
    727 
    728 		if (lp->d_secsize == DEV_BSIZE)
    729 			nblks = bp->b_bcount >> DEV_BSHIFT;
    730 		else
    731 			nblks = howmany(bp->b_bcount, lp->d_secsize);
    732 
    733 #if NSD_SCSIBUS > 0
    734 		/*
    735 		 *  Fill out the scsi command.  If the transfer will
    736 		 *  fit in a "small" cdb, use it.
    737 		 */
    738 		if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
    739 		    ((nblks & 0xff) == nblks) &&
    740 		    !(periph->periph_quirks & PQUIRK_ONLYBIG) &&
    741 		    scsipi_periph_bustype(periph) == SCSIPI_BUSTYPE_SCSI) {
    742 			/*
    743 			 * We can fit in a small cdb.
    744 			 */
    745 			bzero(&cmd_small, sizeof(cmd_small));
    746 			cmd_small.opcode = (bp->b_flags & B_READ) ?
    747 			    SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
    748 			_lto3b(bp->b_rawblkno, cmd_small.addr);
    749 			cmd_small.length = nblks & 0xff;
    750 			cmdlen = sizeof(cmd_small);
    751 			cmdp = (struct scsipi_generic *)&cmd_small;
    752 		} else
    753 #endif
    754 		{
    755 			/*
    756 			 * Need a large cdb.
    757 			 */
    758 			bzero(&cmd_big, sizeof(cmd_big));
    759 			cmd_big.opcode = (bp->b_flags & B_READ) ?
    760 			    READ_BIG : WRITE_BIG;
    761 			_lto4b(bp->b_rawblkno, cmd_big.addr);
    762 			_lto2b(nblks, cmd_big.length);
    763 			cmdlen = sizeof(cmd_big);
    764 			cmdp = (struct scsipi_generic *)&cmd_big;
    765 		}
    766 
    767 		/* Instrumentation. */
    768 		disk_busy(&sd->sc_dk);
    769 
    770 		/*
    771 		 * Mark the disk dirty so that the cache will be
    772 		 * flushed on close.
    773 		 */
    774 		if ((bp->b_flags & B_READ) == 0)
    775 			sd->flags |= SDF_DIRTY;
    776 
    777 		/*
    778 		 * Figure out what flags to use.
    779 		 * XXX Need a B_ORDERED.
    780 		 */
    781 		flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC;
    782 		if (bp->b_flags & B_READ)
    783 			flags |= XS_CTL_DATA_IN | XS_CTL_SIMPLE_TAG;
    784 		else
    785 			flags |= XS_CTL_DATA_OUT | XS_CTL_ORDERED_TAG;
    786 
    787 		/*
    788 		 * Call the routine that chats with the adapter.
    789 		 * Note: we cannot sleep as we may be an interrupt
    790 		 */
    791 		error = scsipi_command(periph, cmdp, cmdlen,
    792 		    (u_char *)bp->b_data, bp->b_bcount,
    793 		    SDRETRIES, 60000, bp, flags);
    794 		if (error) {
    795 			disk_unbusy(&sd->sc_dk, 0);
    796 			printf("%s: not queued, error %d\n",
    797 			    sd->sc_dev.dv_xname, error);
    798 		}
    799 	}
    800 }
    801 
    802 void
    803 sddone(xs)
    804 	struct scsipi_xfer *xs;
    805 {
    806 	struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
    807 
    808 	if (sd->flags & SDF_FLUSHING) {
    809 		/* Flush completed, no longer dirty. */
    810 		sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    811 	}
    812 
    813 	if (xs->bp != NULL) {
    814 		disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid);
    815 #if NRND > 0
    816 		rnd_add_uint32(&sd->rnd_source, xs->bp->b_rawblkno);
    817 #endif
    818 	}
    819 }
    820 
    821 void
    822 sdminphys(bp)
    823 	struct buf *bp;
    824 {
    825 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
    826 	long max;
    827 
    828 	/*
    829 	 * If the device is ancient, we want to make sure that
    830 	 * the transfer fits into a 6-byte cdb.
    831 	 *
    832 	 * XXX Note that the SCSI-I spec says that 256-block transfers
    833 	 * are allowed in a 6-byte read/write, and are specified
    834 	 * by settng the "length" to 0.  However, we're conservative
    835 	 * here, allowing only 255-block transfers in case an
    836 	 * ancient device gets confused by length == 0.  A length of 0
    837 	 * in a 10-byte read/write actually means 0 blocks.
    838 	 */
    839 	if (sd->flags & SDF_ANCIENT) {
    840 		max = sd->sc_dk.dk_label->d_secsize * 0xff;
    841 
    842 		if (bp->b_bcount > max)
    843 			bp->b_bcount = max;
    844 	}
    845 
    846 	(*sd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp);
    847 }
    848 
    849 int
    850 sdread(dev, uio, ioflag)
    851 	dev_t dev;
    852 	struct uio *uio;
    853 	int ioflag;
    854 {
    855 
    856 	return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
    857 }
    858 
    859 int
    860 sdwrite(dev, uio, ioflag)
    861 	dev_t dev;
    862 	struct uio *uio;
    863 	int ioflag;
    864 {
    865 
    866 	return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
    867 }
    868 
    869 /*
    870  * Perform special action on behalf of the user
    871  * Knows about the internals of this device
    872  */
    873 int
    874 sdioctl(dev, cmd, addr, flag, p)
    875 	dev_t dev;
    876 	u_long cmd;
    877 	caddr_t addr;
    878 	int flag;
    879 	struct proc *p;
    880 {
    881 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
    882 	struct scsipi_periph *periph = sd->sc_periph;
    883 	int part = SDPART(dev);
    884 	int error;
    885 
    886 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
    887 
    888 	/*
    889 	 * If the device is not valid, some IOCTLs can still be
    890 	 * handled on the raw partition. Check this here.
    891 	 */
    892 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    893 		switch (cmd) {
    894 		case DIOCKLABEL:
    895 		case DIOCWLABEL:
    896 		case DIOCLOCK:
    897 		case DIOCEJECT:
    898 		case ODIOCEJECT:
    899 		case SCIOCIDENTIFY:
    900 		case OSCIOCIDENTIFY:
    901 		case SCIOCCOMMAND:
    902 		case SCIOCDEBUG:
    903 			if (part == RAW_PART)
    904 				break;
    905 		/* FALLTHROUGH */
    906 		default:
    907 			if ((periph->periph_flags & PERIPH_OPEN) == 0)
    908 				return (ENODEV);
    909 			else
    910 				return (EIO);
    911 		}
    912 	}
    913 
    914 	switch (cmd) {
    915 	case DIOCGDINFO:
    916 		*(struct disklabel *)addr = *(sd->sc_dk.dk_label);
    917 		return (0);
    918 
    919 	case DIOCGPART:
    920 		((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
    921 		((struct partinfo *)addr)->part =
    922 		    &sd->sc_dk.dk_label->d_partitions[part];
    923 		return (0);
    924 
    925 	case DIOCWDINFO:
    926 	case DIOCSDINFO:
    927 		if ((flag & FWRITE) == 0)
    928 			return (EBADF);
    929 
    930 		if ((error = sdlock(sd)) != 0)
    931 			return (error);
    932 		sd->flags |= SDF_LABELLING;
    933 
    934 		error = setdisklabel(sd->sc_dk.dk_label,
    935 		    (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0,
    936 		    sd->sc_dk.dk_cpulabel);
    937 		if (error == 0) {
    938 			if (cmd == DIOCWDINFO)
    939 				error = writedisklabel(SDLABELDEV(dev),
    940 				    sdstrategy, sd->sc_dk.dk_label,
    941 				    sd->sc_dk.dk_cpulabel);
    942 		}
    943 
    944 		sd->flags &= ~SDF_LABELLING;
    945 		sdunlock(sd);
    946 		return (error);
    947 
    948 	case DIOCKLABEL:
    949 		if (*(int *)addr)
    950 			periph->periph_flags |= PERIPH_KEEP_LABEL;
    951 		else
    952 			periph->periph_flags &= ~PERIPH_KEEP_LABEL;
    953 		return (0);
    954 
    955 	case DIOCWLABEL:
    956 		if ((flag & FWRITE) == 0)
    957 			return (EBADF);
    958 		if (*(int *)addr)
    959 			sd->flags |= SDF_WLABEL;
    960 		else
    961 			sd->flags &= ~SDF_WLABEL;
    962 		return (0);
    963 
    964 	case DIOCLOCK:
    965 		return (scsipi_prevent(periph,
    966 		    (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
    967 
    968 	case DIOCEJECT:
    969 		if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
    970 			return (ENOTTY);
    971 		if (*(int *)addr == 0) {
    972 			/*
    973 			 * Don't force eject: check that we are the only
    974 			 * partition open. If so, unlock it.
    975 			 */
    976 			if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
    977 			    sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
    978 			    sd->sc_dk.dk_openmask) {
    979 				error = scsipi_prevent(periph, PR_ALLOW,
    980 				    XS_CTL_IGNORE_NOT_READY);
    981 				if (error)
    982 					return (error);
    983 			} else {
    984 				return (EBUSY);
    985 			}
    986 		}
    987 		/* FALLTHROUGH */
    988 	case ODIOCEJECT:
    989 		return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
    990 		    ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
    991 
    992 	case DIOCGDEFLABEL:
    993 		sdgetdefaultlabel(sd, (struct disklabel *)addr);
    994 		return (0);
    995 
    996 	default:
    997 		if (part != RAW_PART)
    998 			return (ENOTTY);
    999 		return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p));
   1000 	}
   1001 
   1002 #ifdef DIAGNOSTIC
   1003 	panic("sdioctl: impossible");
   1004 #endif
   1005 }
   1006 
   1007 void
   1008 sdgetdefaultlabel(sd, lp)
   1009 	struct sd_softc *sd;
   1010 	struct disklabel *lp;
   1011 {
   1012 
   1013 	bzero(lp, sizeof(struct disklabel));
   1014 
   1015 	lp->d_secsize = sd->params.blksize;
   1016 	lp->d_ntracks = sd->params.heads;
   1017 	lp->d_nsectors = sd->params.sectors;
   1018 	lp->d_ncylinders = sd->params.cyls;
   1019 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1020 
   1021 	switch (scsipi_periph_bustype(sd->sc_periph)) {
   1022 #if NSD_SCSIBUS > 0
   1023 	case SCSIPI_BUSTYPE_SCSI:
   1024 		lp->d_type = DTYPE_SCSI;
   1025 		break;
   1026 #endif
   1027 #if NSD_ATAPIBUS > 0
   1028 	case SCSIPI_BUSTYPE_ATAPI:
   1029 		lp->d_type = DTYPE_ATAPI;
   1030 		break;
   1031 #endif
   1032 	}
   1033 	strncpy(lp->d_typename, sd->name, 16);
   1034 	strncpy(lp->d_packname, "fictitious", 16);
   1035 	lp->d_secperunit = sd->params.disksize;
   1036 	lp->d_rpm = sd->params.rot_rate;
   1037 	lp->d_interleave = 1;
   1038 	lp->d_flags = 0;
   1039 
   1040 	lp->d_partitions[RAW_PART].p_offset = 0;
   1041 	lp->d_partitions[RAW_PART].p_size =
   1042 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   1043 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1044 	lp->d_npartitions = RAW_PART + 1;
   1045 
   1046 	lp->d_magic = DISKMAGIC;
   1047 	lp->d_magic2 = DISKMAGIC;
   1048 	lp->d_checksum = dkcksum(lp);
   1049 }
   1050 
   1051 
   1052 /*
   1053  * Load the label information on the named device
   1054  */
   1055 void
   1056 sdgetdisklabel(sd)
   1057 	struct sd_softc *sd;
   1058 {
   1059 	struct disklabel *lp = sd->sc_dk.dk_label;
   1060 	char *errstring;
   1061 
   1062 	bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
   1063 
   1064 	sdgetdefaultlabel(sd, lp);
   1065 
   1066 	if (lp->d_secpercyl == 0) {
   1067 		lp->d_secpercyl = 100;
   1068 		/* as long as it's not 0 - readdisklabel divides by it (?) */
   1069 	}
   1070 
   1071 	/*
   1072 	 * Call the generic disklabel extraction routine
   1073 	 */
   1074 	errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
   1075 	    sdstrategy, lp, sd->sc_dk.dk_cpulabel);
   1076 	if (errstring) {
   1077 		printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
   1078 		return;
   1079 	}
   1080 }
   1081 
   1082 void
   1083 sd_shutdown(arg)
   1084 	void *arg;
   1085 {
   1086 	struct sd_softc *sd = arg;
   1087 
   1088 	/*
   1089 	 * If the disk cache needs to be flushed, and the disk supports
   1090 	 * it, flush it.  We're cold at this point, so we poll for
   1091 	 * completion.
   1092 	 */
   1093 	if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) {
   1094 		if ((*sd->sc_ops->sdo_flush)(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
   1095 			printf("%s: cache synchronization failed\n",
   1096 			    sd->sc_dev.dv_xname);
   1097 			sd->flags &= ~SDF_FLUSHING;
   1098 		} else
   1099 			sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
   1100 	}
   1101 }
   1102 
   1103 /*
   1104  * Tell the device to map out a defective block
   1105  */
   1106 int
   1107 sd_reassign_blocks(sd, blkno)
   1108 	struct sd_softc *sd;
   1109 	u_long blkno;
   1110 {
   1111 	struct scsi_reassign_blocks scsipi_cmd;
   1112 	struct scsi_reassign_blocks_data rbdata;
   1113 
   1114 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
   1115 	bzero(&rbdata, sizeof(rbdata));
   1116 	scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS;
   1117 
   1118 	_lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
   1119 	_lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
   1120 
   1121 	return (scsipi_command(sd->sc_periph,
   1122 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
   1123 	    (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL,
   1124 	    XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK));
   1125 }
   1126 
   1127 /*
   1128  * Check Errors
   1129  */
   1130 int
   1131 sd_interpret_sense(xs)
   1132 	struct scsipi_xfer *xs;
   1133 {
   1134 	struct scsipi_periph *periph = xs->xs_periph;
   1135 	struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
   1136 	struct sd_softc *sd = (void *)periph->periph_dev;
   1137 	int s, error, retval = EJUSTRETURN;
   1138 
   1139 	/*
   1140 	 * If the periph is already recovering, just do the normal
   1141 	 * error processing.
   1142 	 */
   1143 	if (periph->periph_flags & PERIPH_RECOVERING)
   1144 		return (retval);
   1145 
   1146 	/*
   1147 	 * If the device is not open yet, let the generic code handle it.
   1148 	 */
   1149 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1150 		return (retval);
   1151 
   1152 	/*
   1153 	 * If it isn't a extended or extended/deferred error, let
   1154 	 * the generic code handle it.
   1155 	 */
   1156 	if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
   1157 	    (sense->error_code & SSD_ERRCODE) != 0x71)
   1158 		return (retval);
   1159 
   1160 	if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
   1161 	    sense->add_sense_code == 0x4) {
   1162 		if (sense->add_sense_code_qual == 0x01)	{
   1163 			/*
   1164 			 * Unit In The Process Of Becoming Ready.
   1165 			 */
   1166 			printf("%s: waiting for pack to spin up...\n",
   1167 			    sd->sc_dev.dv_xname);
   1168 			scsipi_periph_freeze(periph, 1);
   1169 			callout_reset(&periph->periph_callout,
   1170 			    5 * hz, scsipi_periph_timed_thaw, periph);
   1171 			retval = ERESTART;
   1172 		} else if ((sense->add_sense_code_qual == 0x2) &&
   1173 		    (periph->periph_quirks & PQUIRK_NOSTARTUNIT) == 0) {
   1174 			printf("%s: pack is stopped, restarting...\n",
   1175 			    sd->sc_dev.dv_xname);
   1176 			s = splbio();
   1177 			periph->periph_flags |= PERIPH_RECOVERING;
   1178 			splx(s);
   1179 			error = scsipi_start(periph, SSS_START,
   1180 			    XS_CTL_URGENT|XS_CTL_HEAD_TAG|
   1181 			    XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
   1182 			if (error) {
   1183 				printf("%s: unable to restart pack\n",
   1184 				    sd->sc_dev.dv_xname);
   1185 				retval = error;
   1186 			} else
   1187 				retval = ERESTART;
   1188 			s = splbio();
   1189 			periph->periph_flags &= ~PERIPH_RECOVERING;
   1190 			splx(s);
   1191 		}
   1192 	}
   1193 	return (retval);
   1194 }
   1195 
   1196 
   1197 int
   1198 sdsize(dev)
   1199 	dev_t dev;
   1200 {
   1201 	struct sd_softc *sd;
   1202 	int part, unit, omask;
   1203 	int size;
   1204 
   1205 	unit = SDUNIT(dev);
   1206 	if (unit >= sd_cd.cd_ndevs)
   1207 		return (-1);
   1208 	sd = sd_cd.cd_devs[unit];
   1209 	if (sd == NULL)
   1210 		return (-1);
   1211 
   1212 	if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1213 		return (-1);
   1214 
   1215 	part = SDPART(dev);
   1216 	omask = sd->sc_dk.dk_openmask & (1 << part);
   1217 
   1218 	if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
   1219 		return (-1);
   1220 	if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1221 		size = -1;
   1222 	else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1223 		size = -1;
   1224 	else
   1225 		size = sd->sc_dk.dk_label->d_partitions[part].p_size *
   1226 		    (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1227 	if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
   1228 		return (-1);
   1229 	return (size);
   1230 }
   1231 
   1232 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
   1233 static struct scsipi_xfer sx;
   1234 static int sddoingadump;
   1235 
   1236 /*
   1237  * dump all of physical memory into the partition specified, starting
   1238  * at offset 'dumplo' into the partition.
   1239  */
   1240 int
   1241 sddump(dev, blkno, va, size)
   1242 	dev_t dev;
   1243 	daddr_t blkno;
   1244 	caddr_t va;
   1245 	size_t size;
   1246 {
   1247 	struct sd_softc *sd;	/* disk unit to do the I/O */
   1248 	struct disklabel *lp;	/* disk's disklabel */
   1249 	int	unit, part;
   1250 	int	sectorsize;	/* size of a disk sector */
   1251 	int	nsects;		/* number of sectors in partition */
   1252 	int	sectoff;	/* sector offset of partition */
   1253 	int	totwrt;		/* total number of sectors left to write */
   1254 	int	nwrt;		/* current number of sectors to write */
   1255 	struct scsipi_rw_big cmd;	/* write command */
   1256 	struct scsipi_xfer *xs;	/* ... convenience */
   1257 	struct scsipi_periph *periph = sd->sc_periph;
   1258 	struct scsipi_channel *chan = periph->periph_channel;
   1259 
   1260 	if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1261 		return (ENODEV);
   1262 
   1263 	/* Check if recursive dump; if so, punt. */
   1264 	if (sddoingadump)
   1265 		return (EFAULT);
   1266 
   1267 	/* Mark as active early. */
   1268 	sddoingadump = 1;
   1269 
   1270 	unit = SDUNIT(dev);	/* Decompose unit & partition. */
   1271 	part = SDPART(dev);
   1272 
   1273 	/* Check for acceptable drive number. */
   1274 	if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
   1275 		return (ENXIO);
   1276 
   1277 	if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1278 		return (ENODEV);
   1279 
   1280 	/* Make sure it was initialized. */
   1281 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) != 0)
   1282 		return (ENXIO);
   1283 
   1284 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1285 	lp = sd->sc_dk.dk_label;
   1286 	sectorsize = lp->d_secsize;
   1287 	if ((size % sectorsize) != 0)
   1288 		return (EFAULT);
   1289 	totwrt = size / sectorsize;
   1290 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1291 
   1292 	nsects = lp->d_partitions[part].p_size;
   1293 	sectoff = lp->d_partitions[part].p_offset;
   1294 
   1295 	/* Check transfer bounds against partition size. */
   1296 	if ((blkno < 0) || ((blkno + totwrt) > nsects))
   1297 		return (EINVAL);
   1298 
   1299 	/* Offset block number to start of partition. */
   1300 	blkno += sectoff;
   1301 
   1302 	xs = &sx;
   1303 
   1304 	while (totwrt > 0) {
   1305 		nwrt = totwrt;		/* XXX */
   1306 #ifndef	SD_DUMP_NOT_TRUSTED
   1307 		/*
   1308 		 *  Fill out the scsi command
   1309 		 */
   1310 		bzero(&cmd, sizeof(cmd));
   1311 		cmd.opcode = WRITE_BIG;
   1312 		_lto4b(blkno, cmd.addr);
   1313 		_lto2b(nwrt, cmd.length);
   1314 		/*
   1315 		 * Fill out the scsipi_xfer structure
   1316 		 *    Note: we cannot sleep as we may be an interrupt
   1317 		 * don't use scsipi_command() as it may want to wait
   1318 		 * for an xs.
   1319 		 */
   1320 		bzero(xs, sizeof(sx));
   1321 		xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
   1322 		    XS_CTL_DATA_OUT;
   1323 		xs->xs_status = 0;
   1324 		xs->xs_periph = periph;
   1325 		xs->xs_retries = SDRETRIES;
   1326 		xs->timeout = 10000;	/* 10000 millisecs for a disk ! */
   1327 		xs->cmd = (struct scsipi_generic *)&cmd;
   1328 		xs->cmdlen = sizeof(cmd);
   1329 		xs->resid = nwrt * sectorsize;
   1330 		xs->error = XS_NOERROR;
   1331 		xs->bp = 0;
   1332 		xs->data = va;
   1333 		xs->datalen = nwrt * sectorsize;
   1334 
   1335 		/*
   1336 		 * Pass all this info to the scsi driver.
   1337 		 */
   1338 		scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
   1339 		if ((xs->xs_status & XS_STS_DONE) == 0 ||
   1340 		    xs->error != XS_NOERROR)
   1341 			return (EIO);
   1342 #else	/* SD_DUMP_NOT_TRUSTED */
   1343 		/* Let's just talk about this first... */
   1344 		printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
   1345 		delay(500 * 1000);	/* half a second */
   1346 #endif	/* SD_DUMP_NOT_TRUSTED */
   1347 
   1348 		/* update block count */
   1349 		totwrt -= nwrt;
   1350 		blkno += nwrt;
   1351 		va += sectorsize * nwrt;
   1352 	}
   1353 	sddoingadump = 0;
   1354 	return (0);
   1355 }
   1356