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