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