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