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