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