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