Home | History | Annotate | Line # | Download | only in scsipi
sd.c revision 1.206
      1 /*	$NetBSD: sd.c,v 1.206 2003/09/09 02:43:35 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2003 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.206 2003/09/09 02:43:35 mycroft 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_test_unit_ready(periph,
    194 	    XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
    195 	    XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT_NODEV);
    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_SILENT_NODEV);
    418 
    419 		/*
    420 		 * Start the pack spinning if necessary. Always allow the
    421 		 * raw parition to be opened, for raw IOCTLs. Data transfers
    422 		 * will check for SDEV_MEDIA_LOADED.
    423 		 */
    424 		if (error == EIO) {
    425 			int silent, error2;
    426 
    427 			if (part == RAW_PART && fmt == S_IFCHR)
    428 				silent = XS_CTL_SILENT;
    429 			else
    430 				silent = 0;
    431 			error2 = scsipi_start(periph, SSS_START, silent);
    432 			switch (error2) {
    433 			case 0:
    434 				error = 0;
    435 				break;
    436 			case EIO:
    437 			case EINVAL:
    438 				if (silent)
    439 					goto out;
    440 				break;
    441 			default:
    442 				error = error2;
    443 				break;
    444 			}
    445 		}
    446 		if (error)
    447 			goto bad3;
    448 
    449 		periph->periph_flags |= PERIPH_OPEN;
    450 
    451 		if (periph->periph_flags & PERIPH_REMOVABLE) {
    452 			/* Lock the pack in. */
    453 			error = scsipi_prevent(periph, PR_PREVENT,
    454 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    455 			    XS_CTL_IGNORE_MEDIA_CHANGE);
    456 			if (error)
    457 				goto bad;
    458 		}
    459 
    460 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    461 			periph->periph_flags |= PERIPH_MEDIA_LOADED;
    462 
    463 			/*
    464 			 * Load the physical device parameters.
    465 			 *
    466 			 * Note that if media is present but unformatted,
    467 			 * we allow the open (so that it can be formatted!).
    468 			 * The drive should refuse real I/O, if the media is
    469 			 * unformatted.
    470 			 */
    471 			if ((*sd->sc_ops->sdo_get_parms)(sd, &sd->params,
    472 			    0) == SDGP_RESULT_OFFLINE) {
    473 				error = ENXIO;
    474 				goto bad2;
    475 			}
    476 			SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
    477 
    478 			/* Load the partition info if not already loaded. */
    479 			sdgetdisklabel(sd);
    480 			SC_DEBUG(periph, SCSIPI_DB3, ("Disklabel loaded "));
    481 		}
    482 	}
    483 
    484 	/* Check that the partition exists. */
    485 	if (part != RAW_PART &&
    486 	    (part >= sd->sc_dk.dk_label->d_npartitions ||
    487 	     sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    488 		error = ENXIO;
    489 		goto bad;
    490 	}
    491 
    492 out:	/* Insure only one open at a time. */
    493 	switch (fmt) {
    494 	case S_IFCHR:
    495 		sd->sc_dk.dk_copenmask |= (1 << part);
    496 		break;
    497 	case S_IFBLK:
    498 		sd->sc_dk.dk_bopenmask |= (1 << part);
    499 		break;
    500 	}
    501 	sd->sc_dk.dk_openmask =
    502 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
    503 
    504 	SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
    505 	sdunlock(sd);
    506 	return (0);
    507 
    508 bad2:
    509 	periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    510 
    511 bad:
    512 	if (sd->sc_dk.dk_openmask == 0) {
    513 		if (periph->periph_flags & PERIPH_REMOVABLE)
    514 			scsipi_prevent(periph, PR_ALLOW,
    515 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    516 			    XS_CTL_IGNORE_MEDIA_CHANGE);
    517 		periph->periph_flags &= ~PERIPH_OPEN;
    518 	}
    519 
    520 bad3:
    521 	sdunlock(sd);
    522 bad4:
    523 	if (sd->sc_dk.dk_openmask == 0)
    524 		scsipi_adapter_delref(adapt);
    525 	return (error);
    526 }
    527 
    528 /*
    529  * close the device.. only called if we are the LAST occurence of an open
    530  * device.  Convenient now but usually a pain.
    531  */
    532 int
    533 sdclose(dev, flag, fmt, p)
    534 	dev_t dev;
    535 	int flag, fmt;
    536 	struct proc *p;
    537 {
    538 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
    539 	struct scsipi_periph *periph = sd->sc_periph;
    540 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
    541 	int part = SDPART(dev);
    542 	int error;
    543 
    544 	if ((error = sdlock(sd)) != 0)
    545 		return (error);
    546 
    547 	switch (fmt) {
    548 	case S_IFCHR:
    549 		sd->sc_dk.dk_copenmask &= ~(1 << part);
    550 		break;
    551 	case S_IFBLK:
    552 		sd->sc_dk.dk_bopenmask &= ~(1 << part);
    553 		break;
    554 	}
    555 	sd->sc_dk.dk_openmask =
    556 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
    557 
    558 	if (sd->sc_dk.dk_openmask == 0) {
    559 		/*
    560 		 * If the disk cache needs flushing, and the disk supports
    561 		 * it, do it now.
    562 		 */
    563 		if ((sd->flags & SDF_DIRTY) != 0 &&
    564 		    sd->sc_ops->sdo_flush != NULL) {
    565 			if ((*sd->sc_ops->sdo_flush)(sd, 0)) {
    566 				printf("%s: cache synchronization failed\n",
    567 				    sd->sc_dev.dv_xname);
    568 				sd->flags &= ~SDF_FLUSHING;
    569 			} else
    570 				sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    571 		}
    572 
    573 		if (! (periph->periph_flags & PERIPH_KEEP_LABEL))
    574 			periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
    575 
    576 		scsipi_wait_drain(periph);
    577 
    578 		if (periph->periph_flags & PERIPH_REMOVABLE)
    579 			scsipi_prevent(periph, PR_ALLOW,
    580 			    XS_CTL_IGNORE_ILLEGAL_REQUEST |
    581 			    XS_CTL_IGNORE_NOT_READY);
    582 		periph->periph_flags &= ~PERIPH_OPEN;
    583 
    584 		scsipi_wait_drain(periph);
    585 
    586 		scsipi_adapter_delref(adapt);
    587 	}
    588 
    589 	sdunlock(sd);
    590 	return (0);
    591 }
    592 
    593 /*
    594  * Actually translate the requested transfer into one the physical driver
    595  * can understand.  The transfer is described by a buf and will include
    596  * only one physical transfer.
    597  */
    598 void
    599 sdstrategy(bp)
    600 	struct buf *bp;
    601 {
    602 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
    603 	struct scsipi_periph *periph = sd->sc_periph;
    604 	struct disklabel *lp;
    605 	daddr_t blkno;
    606 	int s;
    607 	boolean_t sector_aligned;
    608 
    609 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy "));
    610 	SC_DEBUG(sd->sc_periph, SCSIPI_DB1,
    611 	    ("%ld bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
    612 	/*
    613 	 * If the device has been made invalid, error out
    614 	 */
    615 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 ||
    616 	    (sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
    617 		if (periph->periph_flags & PERIPH_OPEN)
    618 			bp->b_error = EIO;
    619 		else
    620 			bp->b_error = ENODEV;
    621 		goto bad;
    622 	}
    623 
    624 	lp = sd->sc_dk.dk_label;
    625 
    626 	/*
    627 	 * The transfer must be a whole number of blocks, offset must not be
    628 	 * negative.
    629 	 */
    630 	if (lp->d_secsize == DEV_BSIZE) {
    631 		sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0;
    632 	} else {
    633 		sector_aligned = (bp->b_bcount % lp->d_secsize) == 0;
    634 	}
    635 	if (!sector_aligned || bp->b_blkno < 0) {
    636 		bp->b_error = EINVAL;
    637 		goto bad;
    638 	}
    639 	/*
    640 	 * If it's a null transfer, return immediatly
    641 	 */
    642 	if (bp->b_bcount == 0)
    643 		goto done;
    644 
    645 	/*
    646 	 * Do bounds checking, adjust transfer. if error, process.
    647 	 * If end of partition, just return.
    648 	 */
    649 	if (SDPART(bp->b_dev) == RAW_PART) {
    650 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    651 		    sd->params.disksize512) <= 0)
    652 			goto done;
    653 	} else {
    654 		if (bounds_check_with_label(&sd->sc_dk, bp,
    655 		    (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
    656 			goto done;
    657 	}
    658 
    659 	/*
    660 	 * Now convert the block number to absolute and put it in
    661 	 * terms of the device's logical block size.
    662 	 */
    663 	if (lp->d_secsize == DEV_BSIZE)
    664 		blkno = bp->b_blkno;
    665 	else if (lp->d_secsize > DEV_BSIZE)
    666 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    667 	else
    668 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    669 
    670 	if (SDPART(bp->b_dev) != RAW_PART)
    671 		blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset;
    672 
    673 	bp->b_rawblkno = blkno;
    674 
    675 	s = splbio();
    676 
    677 	/*
    678 	 * Place it in the queue of disk activities for this disk.
    679 	 *
    680 	 * XXX Only do disksort() if the current operating mode does not
    681 	 * XXX include tagged queueing.
    682 	 */
    683 	BUFQ_PUT(&sd->buf_queue, bp);
    684 
    685 	/*
    686 	 * Tell the device to get going on the transfer if it's
    687 	 * not doing anything, otherwise just wait for completion
    688 	 */
    689 	sdstart(sd->sc_periph);
    690 
    691 	splx(s);
    692 	return;
    693 
    694 bad:
    695 	bp->b_flags |= B_ERROR;
    696 done:
    697 	/*
    698 	 * Correctly set the buf to indicate a completed xfer
    699 	 */
    700 	bp->b_resid = bp->b_bcount;
    701 	biodone(bp);
    702 }
    703 
    704 /*
    705  * sdstart looks to see if there is a buf waiting for the device
    706  * and that the device is not already busy. If both are true,
    707  * It dequeues the buf and creates a scsi command to perform the
    708  * transfer in the buf. The transfer request will call scsipi_done
    709  * on completion, which will in turn call this routine again
    710  * so that the next queued transfer is performed.
    711  * The bufs are queued by the strategy routine (sdstrategy)
    712  *
    713  * This routine is also called after other non-queued requests
    714  * have been made of the scsi driver, to ensure that the queue
    715  * continues to be drained.
    716  *
    717  * must be called at the correct (highish) spl level
    718  * sdstart() is called at splbio from sdstrategy and scsipi_done
    719  */
    720 void
    721 sdstart(periph)
    722 	struct scsipi_periph *periph;
    723 {
    724 	struct sd_softc *sd = (void *)periph->periph_dev;
    725 	struct disklabel *lp = sd->sc_dk.dk_label;
    726 	struct buf *bp = 0;
    727 	struct scsipi_rw_big cmd_big;
    728 #if NSD_SCSIBUS > 0
    729 	struct scsi_rw cmd_small;
    730 #endif
    731 	struct scsipi_generic *cmdp;
    732 	int nblks, cmdlen, error, flags;
    733 
    734 	SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
    735 	/*
    736 	 * Check if the device has room for another command
    737 	 */
    738 	while (periph->periph_active < periph->periph_openings) {
    739 		/*
    740 		 * there is excess capacity, but a special waits
    741 		 * It'll need the adapter as soon as we clear out of the
    742 		 * way and let it run (user level wait).
    743 		 */
    744 		if (periph->periph_flags & PERIPH_WAITING) {
    745 			periph->periph_flags &= ~PERIPH_WAITING;
    746 			wakeup((caddr_t)periph);
    747 			return;
    748 		}
    749 
    750 		/*
    751 		 * See if there is a buf with work for us to do..
    752 		 */
    753 		if ((bp = BUFQ_GET(&sd->buf_queue)) == NULL)
    754 			return;
    755 
    756 		/*
    757 		 * If the device has become invalid, abort all the
    758 		 * reads and writes until all files have been closed and
    759 		 * re-opened
    760 		 */
    761 		if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    762 			bp->b_error = EIO;
    763 			bp->b_flags |= B_ERROR;
    764 			bp->b_resid = bp->b_bcount;
    765 			biodone(bp);
    766 			continue;
    767 		}
    768 
    769 		/*
    770 		 * We have a buf, now we should make a command.
    771 		 */
    772 
    773 		if (lp->d_secsize == DEV_BSIZE)
    774 			nblks = bp->b_bcount >> DEV_BSHIFT;
    775 		else
    776 			nblks = howmany(bp->b_bcount, lp->d_secsize);
    777 
    778 #if NSD_SCSIBUS > 0
    779 		/*
    780 		 *  Fill out the scsi command.  If the transfer will
    781 		 *  fit in a "small" cdb, use it.
    782 		 */
    783 		if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
    784 		    ((nblks & 0xff) == nblks) &&
    785 		    !(periph->periph_quirks & PQUIRK_ONLYBIG)) {
    786 			/*
    787 			 * We can fit in a small cdb.
    788 			 */
    789 			memset(&cmd_small, 0, sizeof(cmd_small));
    790 			cmd_small.opcode = (bp->b_flags & B_READ) ?
    791 			    SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
    792 			_lto3b(bp->b_rawblkno, cmd_small.addr);
    793 			cmd_small.length = nblks & 0xff;
    794 			cmdlen = sizeof(cmd_small);
    795 			cmdp = (struct scsipi_generic *)&cmd_small;
    796 		} else
    797 #endif /* NSD_SCSIBUS > 0 */
    798 		{
    799 			/*
    800 			 * Need a large cdb.
    801 			 */
    802 			memset(&cmd_big, 0, sizeof(cmd_big));
    803 			cmd_big.opcode = (bp->b_flags & B_READ) ?
    804 			    READ_BIG : WRITE_BIG;
    805 			_lto4b(bp->b_rawblkno, cmd_big.addr);
    806 			_lto2b(nblks, cmd_big.length);
    807 			cmdlen = sizeof(cmd_big);
    808 			cmdp = (struct scsipi_generic *)&cmd_big;
    809 		}
    810 
    811 		/* Instrumentation. */
    812 		disk_busy(&sd->sc_dk);
    813 
    814 		/*
    815 		 * Mark the disk dirty so that the cache will be
    816 		 * flushed on close.
    817 		 */
    818 		if ((bp->b_flags & B_READ) == 0)
    819 			sd->flags |= SDF_DIRTY;
    820 
    821 		/*
    822 		 * Figure out what flags to use.
    823 		 */
    824 		flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
    825 		if (bp->b_flags & B_READ)
    826 			flags |= XS_CTL_DATA_IN;
    827 		else
    828 			flags |= XS_CTL_DATA_OUT;
    829 
    830 		/*
    831 		 * Call the routine that chats with the adapter.
    832 		 * Note: we cannot sleep as we may be an interrupt
    833 		 */
    834 		error = scsipi_command(periph, cmdp, cmdlen,
    835 		    (u_char *)bp->b_data, bp->b_bcount,
    836 		    SDRETRIES, SD_IO_TIMEOUT, bp, flags);
    837 		if (error) {
    838 			disk_unbusy(&sd->sc_dk, 0, 0);
    839 			printf("%s: not queued, error %d\n",
    840 			    sd->sc_dev.dv_xname, error);
    841 		}
    842 	}
    843 }
    844 
    845 void
    846 sddone(xs)
    847 	struct scsipi_xfer *xs;
    848 {
    849 	struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
    850 
    851 	if (sd->flags & SDF_FLUSHING) {
    852 		/* Flush completed, no longer dirty. */
    853 		sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    854 	}
    855 
    856 	if (xs->bp != NULL) {
    857 		disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid,
    858 		    (xs->bp->b_flags & B_READ));
    859 #if NRND > 0
    860 		rnd_add_uint32(&sd->rnd_source, xs->bp->b_rawblkno);
    861 #endif
    862 	}
    863 }
    864 
    865 void
    866 sdminphys(bp)
    867 	struct buf *bp;
    868 {
    869 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
    870 	long max;
    871 
    872 	/*
    873 	 * If the device is ancient, we want to make sure that
    874 	 * the transfer fits into a 6-byte cdb.
    875 	 *
    876 	 * XXX Note that the SCSI-I spec says that 256-block transfers
    877 	 * are allowed in a 6-byte read/write, and are specified
    878 	 * by settng the "length" to 0.  However, we're conservative
    879 	 * here, allowing only 255-block transfers in case an
    880 	 * ancient device gets confused by length == 0.  A length of 0
    881 	 * in a 10-byte read/write actually means 0 blocks.
    882 	 */
    883 	if ((sd->flags & SDF_ANCIENT) &&
    884 	    ((sd->sc_periph->periph_flags &
    885 	    (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
    886 		max = sd->sc_dk.dk_label->d_secsize * 0xff;
    887 
    888 		if (bp->b_bcount > max)
    889 			bp->b_bcount = max;
    890 	}
    891 
    892 	(*sd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp);
    893 }
    894 
    895 int
    896 sdread(dev, uio, ioflag)
    897 	dev_t dev;
    898 	struct uio *uio;
    899 	int ioflag;
    900 {
    901 
    902 	return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
    903 }
    904 
    905 int
    906 sdwrite(dev, uio, ioflag)
    907 	dev_t dev;
    908 	struct uio *uio;
    909 	int ioflag;
    910 {
    911 
    912 	return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
    913 }
    914 
    915 /*
    916  * Perform special action on behalf of the user
    917  * Knows about the internals of this device
    918  */
    919 int
    920 sdioctl(dev, cmd, addr, flag, p)
    921 	dev_t dev;
    922 	u_long cmd;
    923 	caddr_t addr;
    924 	int flag;
    925 	struct proc *p;
    926 {
    927 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
    928 	struct scsipi_periph *periph = sd->sc_periph;
    929 	int part = SDPART(dev);
    930 	int error = 0;
    931 #ifdef __HAVE_OLD_DISKLABEL
    932 	struct disklabel *newlabel = NULL;
    933 #endif
    934 
    935 	SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
    936 
    937 	/*
    938 	 * If the device is not valid, some IOCTLs can still be
    939 	 * handled on the raw partition. Check this here.
    940 	 */
    941 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
    942 		switch (cmd) {
    943 		case DIOCKLABEL:
    944 		case DIOCWLABEL:
    945 		case DIOCLOCK:
    946 		case DIOCEJECT:
    947 		case ODIOCEJECT:
    948 		case DIOCGCACHE:
    949 		case DIOCSCACHE:
    950 		case SCIOCIDENTIFY:
    951 		case OSCIOCIDENTIFY:
    952 		case SCIOCCOMMAND:
    953 		case SCIOCDEBUG:
    954 			if (part == RAW_PART)
    955 				break;
    956 		/* FALLTHROUGH */
    957 		default:
    958 			if ((periph->periph_flags & PERIPH_OPEN) == 0)
    959 				return (ENODEV);
    960 			else
    961 				return (EIO);
    962 		}
    963 	}
    964 
    965 	switch (cmd) {
    966 	case DIOCGDINFO:
    967 		*(struct disklabel *)addr = *(sd->sc_dk.dk_label);
    968 		return (0);
    969 
    970 #ifdef __HAVE_OLD_DISKLABEL
    971 	case ODIOCGDINFO:
    972 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
    973 		if (newlabel == NULL)
    974 			return EIO;
    975 		memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
    976 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
    977 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
    978 		else
    979 			error = ENOTTY;
    980 		free(newlabel, M_TEMP);
    981 		return error;
    982 #endif
    983 
    984 	case DIOCGPART:
    985 		((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
    986 		((struct partinfo *)addr)->part =
    987 		    &sd->sc_dk.dk_label->d_partitions[part];
    988 		return (0);
    989 
    990 	case DIOCWDINFO:
    991 	case DIOCSDINFO:
    992 #ifdef __HAVE_OLD_DISKLABEL
    993 	case ODIOCWDINFO:
    994 	case ODIOCSDINFO:
    995 #endif
    996 	{
    997 		struct disklabel *lp;
    998 
    999 		if ((flag & FWRITE) == 0)
   1000 			return (EBADF);
   1001 
   1002 #ifdef __HAVE_OLD_DISKLABEL
   1003  		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
   1004 			newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1005 			if (newlabel == NULL)
   1006 				return EIO;
   1007 			memset(newlabel, 0, sizeof newlabel);
   1008 			memcpy(newlabel, addr, sizeof (struct olddisklabel));
   1009 			lp = newlabel;
   1010 		} else
   1011 #endif
   1012 		lp = (struct disklabel *)addr;
   1013 
   1014 		if ((error = sdlock(sd)) != 0)
   1015 			goto bad;
   1016 		sd->flags |= SDF_LABELLING;
   1017 
   1018 		error = setdisklabel(sd->sc_dk.dk_label,
   1019 		    lp, /*sd->sc_dk.dk_openmask : */0,
   1020 		    sd->sc_dk.dk_cpulabel);
   1021 		if (error == 0) {
   1022 			if (cmd == DIOCWDINFO
   1023 #ifdef __HAVE_OLD_DISKLABEL
   1024 			    || cmd == ODIOCWDINFO
   1025 #endif
   1026 			   )
   1027 				error = writedisklabel(SDLABELDEV(dev),
   1028 				    sdstrategy, sd->sc_dk.dk_label,
   1029 				    sd->sc_dk.dk_cpulabel);
   1030 		}
   1031 
   1032 		sd->flags &= ~SDF_LABELLING;
   1033 		sdunlock(sd);
   1034 bad:
   1035 #ifdef __HAVE_OLD_DISKLABEL
   1036 		if (newlabel != NULL)
   1037 			free(newlabel, M_TEMP);
   1038 #endif
   1039 		return (error);
   1040 	}
   1041 
   1042 	case DIOCKLABEL:
   1043 		if (*(int *)addr)
   1044 			periph->periph_flags |= PERIPH_KEEP_LABEL;
   1045 		else
   1046 			periph->periph_flags &= ~PERIPH_KEEP_LABEL;
   1047 		return (0);
   1048 
   1049 	case DIOCWLABEL:
   1050 		if ((flag & FWRITE) == 0)
   1051 			return (EBADF);
   1052 		if (*(int *)addr)
   1053 			sd->flags |= SDF_WLABEL;
   1054 		else
   1055 			sd->flags &= ~SDF_WLABEL;
   1056 		return (0);
   1057 
   1058 	case DIOCLOCK:
   1059 		return (scsipi_prevent(periph,
   1060 		    (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
   1061 
   1062 	case DIOCEJECT:
   1063 		if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
   1064 			return (ENOTTY);
   1065 		if (*(int *)addr == 0) {
   1066 			/*
   1067 			 * Don't force eject: check that we are the only
   1068 			 * partition open. If so, unlock it.
   1069 			 */
   1070 			if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
   1071 			    sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
   1072 			    sd->sc_dk.dk_openmask) {
   1073 				error = scsipi_prevent(periph, PR_ALLOW,
   1074 				    XS_CTL_IGNORE_NOT_READY);
   1075 				if (error)
   1076 					return (error);
   1077 			} else {
   1078 				return (EBUSY);
   1079 			}
   1080 		}
   1081 		/* FALLTHROUGH */
   1082 	case ODIOCEJECT:
   1083 		return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
   1084 		    ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
   1085 
   1086 	case DIOCGDEFLABEL:
   1087 		sdgetdefaultlabel(sd, (struct disklabel *)addr);
   1088 		return (0);
   1089 
   1090 #ifdef __HAVE_OLD_DISKLABEL
   1091 	case ODIOCGDEFLABEL:
   1092 		newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
   1093 		if (newlabel == NULL)
   1094 			return EIO;
   1095 		sdgetdefaultlabel(sd, newlabel);
   1096 		if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
   1097 			memcpy(addr, newlabel, sizeof (struct olddisklabel));
   1098 		else
   1099 			error = ENOTTY;
   1100 		free(newlabel, M_TEMP);
   1101 		return error;
   1102 #endif
   1103 
   1104 	case DIOCGCACHE:
   1105 		if (sd->sc_ops->sdo_getcache != NULL)
   1106 			return ((*sd->sc_ops->sdo_getcache)(sd, (int *) addr));
   1107 
   1108 		/* Not supported on this device. */
   1109 		*(int *) addr = 0;
   1110 		return (0);
   1111 
   1112 	case DIOCSCACHE:
   1113 		if ((flag & FWRITE) == 0)
   1114 			return (EBADF);
   1115 		if (sd->sc_ops->sdo_setcache != NULL)
   1116 			return ((*sd->sc_ops->sdo_setcache)(sd, *(int *) addr));
   1117 
   1118 		/* Not supported on this device. */
   1119 		return (EOPNOTSUPP);
   1120 
   1121 	case DIOCCACHESYNC:
   1122 		/*
   1123 		 * XXX Do we really need to care about having a writable
   1124 		 * file descriptor here?
   1125 		 */
   1126 		if ((flag & FWRITE) == 0)
   1127 			return (EBADF);
   1128 		if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0) &&
   1129 		    sd->sc_ops->sdo_flush != NULL) {
   1130 			error = (*sd->sc_ops->sdo_flush)(sd, 0);
   1131 			if (error)
   1132 				sd->flags &= ~SDF_FLUSHING;
   1133 			else
   1134 				sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
   1135 		} else
   1136 			error = 0;
   1137 		return (error);
   1138 
   1139 	default:
   1140 		if (part != RAW_PART)
   1141 			return (ENOTTY);
   1142 		return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p));
   1143 	}
   1144 
   1145 #ifdef DIAGNOSTIC
   1146 	panic("sdioctl: impossible");
   1147 #endif
   1148 }
   1149 
   1150 void
   1151 sdgetdefaultlabel(sd, lp)
   1152 	struct sd_softc *sd;
   1153 	struct disklabel *lp;
   1154 {
   1155 
   1156 	memset(lp, 0, sizeof(struct disklabel));
   1157 
   1158 	lp->d_secsize = sd->params.blksize;
   1159 	lp->d_ntracks = sd->params.heads;
   1160 	lp->d_nsectors = sd->params.sectors;
   1161 	lp->d_ncylinders = sd->params.cyls;
   1162 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1163 
   1164 	switch (scsipi_periph_bustype(sd->sc_periph)) {
   1165 #if NSD_SCSIBUS > 0
   1166 	case SCSIPI_BUSTYPE_SCSI:
   1167 		lp->d_type = DTYPE_SCSI;
   1168 		break;
   1169 #endif
   1170 #if NSD_ATAPIBUS > 0
   1171 	case SCSIPI_BUSTYPE_ATAPI:
   1172 		lp->d_type = DTYPE_ATAPI;
   1173 		break;
   1174 #endif
   1175 	}
   1176 	strncpy(lp->d_typename, sd->name, 16);
   1177 	strncpy(lp->d_packname, "fictitious", 16);
   1178 	lp->d_secperunit = sd->params.disksize;
   1179 	lp->d_rpm = sd->params.rot_rate;
   1180 	lp->d_interleave = 1;
   1181 	lp->d_flags = 0;
   1182 
   1183 	lp->d_partitions[RAW_PART].p_offset = 0;
   1184 	lp->d_partitions[RAW_PART].p_size =
   1185 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   1186 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1187 	lp->d_npartitions = RAW_PART + 1;
   1188 
   1189 	lp->d_magic = DISKMAGIC;
   1190 	lp->d_magic2 = DISKMAGIC;
   1191 	lp->d_checksum = dkcksum(lp);
   1192 }
   1193 
   1194 
   1195 /*
   1196  * Load the label information on the named device
   1197  */
   1198 void
   1199 sdgetdisklabel(sd)
   1200 	struct sd_softc *sd;
   1201 {
   1202 	struct disklabel *lp = sd->sc_dk.dk_label;
   1203 	const char *errstring;
   1204 
   1205 	memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
   1206 
   1207 	sdgetdefaultlabel(sd, lp);
   1208 
   1209 	if (lp->d_secpercyl == 0) {
   1210 		lp->d_secpercyl = 100;
   1211 		/* as long as it's not 0 - readdisklabel divides by it (?) */
   1212 	}
   1213 
   1214 	/*
   1215 	 * Call the generic disklabel extraction routine
   1216 	 */
   1217 	errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
   1218 	    sdstrategy, lp, sd->sc_dk.dk_cpulabel);
   1219 	if (errstring) {
   1220 		printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
   1221 		return;
   1222 	}
   1223 }
   1224 
   1225 void
   1226 sd_shutdown(arg)
   1227 	void *arg;
   1228 {
   1229 	struct sd_softc *sd = arg;
   1230 
   1231 	/*
   1232 	 * If the disk cache needs to be flushed, and the disk supports
   1233 	 * it, flush it.  We're cold at this point, so we poll for
   1234 	 * completion.
   1235 	 */
   1236 	if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) {
   1237 		if ((*sd->sc_ops->sdo_flush)(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
   1238 			printf("%s: cache synchronization failed\n",
   1239 			    sd->sc_dev.dv_xname);
   1240 			sd->flags &= ~SDF_FLUSHING;
   1241 		} else
   1242 			sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
   1243 	}
   1244 }
   1245 
   1246 /*
   1247  * Tell the device to map out a defective block
   1248  */
   1249 int
   1250 sd_reassign_blocks(sd, blkno)
   1251 	struct sd_softc *sd;
   1252 	u_long blkno;
   1253 {
   1254 	struct scsi_reassign_blocks scsipi_cmd;
   1255 	struct scsi_reassign_blocks_data rbdata;
   1256 
   1257 	memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
   1258 	memset(&rbdata, 0, sizeof(rbdata));
   1259 	scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS;
   1260 
   1261 	_lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
   1262 	_lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
   1263 
   1264 	return (scsipi_command(sd->sc_periph,
   1265 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
   1266 	    (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL,
   1267 	    XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK));
   1268 }
   1269 
   1270 /*
   1271  * Check Errors
   1272  */
   1273 int
   1274 sd_interpret_sense(xs)
   1275 	struct scsipi_xfer *xs;
   1276 {
   1277 	struct scsipi_periph *periph = xs->xs_periph;
   1278 	struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
   1279 	struct sd_softc *sd = (void *)periph->periph_dev;
   1280 	int s, error, retval = EJUSTRETURN;
   1281 
   1282 	/*
   1283 	 * If the periph is already recovering, just do the normal
   1284 	 * error processing.
   1285 	 */
   1286 	if (periph->periph_flags & PERIPH_RECOVERING)
   1287 		return (retval);
   1288 
   1289 	/*
   1290 	 * If the device is not open yet, let the generic code handle it.
   1291 	 */
   1292 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1293 		return (retval);
   1294 
   1295 	/*
   1296 	 * If it isn't a extended or extended/deferred error, let
   1297 	 * the generic code handle it.
   1298 	 */
   1299 	if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
   1300 	    (sense->error_code & SSD_ERRCODE) != 0x71)
   1301 		return (retval);
   1302 
   1303 	if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
   1304 	    sense->add_sense_code == 0x4) {
   1305 		if (sense->add_sense_code_qual == 0x01)	{
   1306 			/*
   1307 			 * Unit In The Process Of Becoming Ready.
   1308 			 */
   1309 			printf("%s: waiting for pack to spin up...\n",
   1310 			    sd->sc_dev.dv_xname);
   1311 			if (!callout_pending(&periph->periph_callout))
   1312 				scsipi_periph_freeze(periph, 1);
   1313 			callout_reset(&periph->periph_callout,
   1314 			    5 * hz, scsipi_periph_timed_thaw, periph);
   1315 			retval = ERESTART;
   1316 		} else if (sense->add_sense_code_qual == 0x02) {
   1317 			printf("%s: pack is stopped, restarting...\n",
   1318 			    sd->sc_dev.dv_xname);
   1319 			s = splbio();
   1320 			periph->periph_flags |= PERIPH_RECOVERING;
   1321 			splx(s);
   1322 			error = scsipi_start(periph, SSS_START,
   1323 			    XS_CTL_URGENT|XS_CTL_HEAD_TAG|
   1324 			    XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
   1325 			if (error) {
   1326 				printf("%s: unable to restart pack\n",
   1327 				    sd->sc_dev.dv_xname);
   1328 				retval = error;
   1329 			} else
   1330 				retval = ERESTART;
   1331 			s = splbio();
   1332 			periph->periph_flags &= ~PERIPH_RECOVERING;
   1333 			splx(s);
   1334 		}
   1335 	}
   1336 	return (retval);
   1337 }
   1338 
   1339 
   1340 int
   1341 sdsize(dev)
   1342 	dev_t dev;
   1343 {
   1344 	struct sd_softc *sd;
   1345 	int part, unit, omask;
   1346 	int size;
   1347 
   1348 	unit = SDUNIT(dev);
   1349 	if (unit >= sd_cd.cd_ndevs)
   1350 		return (-1);
   1351 	sd = sd_cd.cd_devs[unit];
   1352 	if (sd == NULL)
   1353 		return (-1);
   1354 
   1355 	if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1356 		return (-1);
   1357 
   1358 	part = SDPART(dev);
   1359 	omask = sd->sc_dk.dk_openmask & (1 << part);
   1360 
   1361 	if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
   1362 		return (-1);
   1363 	if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1364 		size = -1;
   1365 	else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1366 		size = -1;
   1367 	else
   1368 		size = sd->sc_dk.dk_label->d_partitions[part].p_size *
   1369 		    (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1370 	if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
   1371 		return (-1);
   1372 	return (size);
   1373 }
   1374 
   1375 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
   1376 static struct scsipi_xfer sx;
   1377 static int sddoingadump;
   1378 
   1379 /*
   1380  * dump all of physical memory into the partition specified, starting
   1381  * at offset 'dumplo' into the partition.
   1382  */
   1383 int
   1384 sddump(dev, blkno, va, size)
   1385 	dev_t dev;
   1386 	daddr_t blkno;
   1387 	caddr_t va;
   1388 	size_t size;
   1389 {
   1390 	struct sd_softc *sd;	/* disk unit to do the I/O */
   1391 	struct disklabel *lp;	/* disk's disklabel */
   1392 	int	unit, part;
   1393 	int	sectorsize;	/* size of a disk sector */
   1394 	int	nsects;		/* number of sectors in partition */
   1395 	int	sectoff;	/* sector offset of partition */
   1396 	int	totwrt;		/* total number of sectors left to write */
   1397 	int	nwrt;		/* current number of sectors to write */
   1398 	struct scsipi_rw_big cmd;	/* write command */
   1399 	struct scsipi_xfer *xs;	/* ... convenience */
   1400 	struct scsipi_periph *periph;
   1401 	struct scsipi_channel *chan;
   1402 
   1403 	/* Check if recursive dump; if so, punt. */
   1404 	if (sddoingadump)
   1405 		return (EFAULT);
   1406 
   1407 	/* Mark as active early. */
   1408 	sddoingadump = 1;
   1409 
   1410 	unit = SDUNIT(dev);	/* Decompose unit & partition. */
   1411 	part = SDPART(dev);
   1412 
   1413 	/* Check for acceptable drive number. */
   1414 	if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
   1415 		return (ENXIO);
   1416 
   1417 	if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1418 		return (ENODEV);
   1419 
   1420 	periph = sd->sc_periph;
   1421 	chan = periph->periph_channel;
   1422 
   1423 	/* Make sure it was initialized. */
   1424 	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
   1425 		return (ENXIO);
   1426 
   1427 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1428 	lp = sd->sc_dk.dk_label;
   1429 	sectorsize = lp->d_secsize;
   1430 	if ((size % sectorsize) != 0)
   1431 		return (EFAULT);
   1432 	totwrt = size / sectorsize;
   1433 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1434 
   1435 	nsects = lp->d_partitions[part].p_size;
   1436 	sectoff = lp->d_partitions[part].p_offset;
   1437 
   1438 	/* Check transfer bounds against partition size. */
   1439 	if ((blkno < 0) || ((blkno + totwrt) > nsects))
   1440 		return (EINVAL);
   1441 
   1442 	/* Offset block number to start of partition. */
   1443 	blkno += sectoff;
   1444 
   1445 	xs = &sx;
   1446 
   1447 	while (totwrt > 0) {
   1448 		nwrt = totwrt;		/* XXX */
   1449 #ifndef	SD_DUMP_NOT_TRUSTED
   1450 		/*
   1451 		 *  Fill out the scsi command
   1452 		 */
   1453 		memset(&cmd, 0, sizeof(cmd));
   1454 		cmd.opcode = WRITE_BIG;
   1455 		_lto4b(blkno, cmd.addr);
   1456 		_lto2b(nwrt, cmd.length);
   1457 		/*
   1458 		 * Fill out the scsipi_xfer structure
   1459 		 *    Note: we cannot sleep as we may be an interrupt
   1460 		 * don't use scsipi_command() as it may want to wait
   1461 		 * for an xs.
   1462 		 */
   1463 		memset(xs, 0, sizeof(sx));
   1464 		xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
   1465 		    XS_CTL_DATA_OUT;
   1466 		xs->xs_status = 0;
   1467 		xs->xs_periph = periph;
   1468 		xs->xs_retries = SDRETRIES;
   1469 		xs->timeout = 10000;	/* 10000 millisecs for a disk ! */
   1470 		xs->cmd = (struct scsipi_generic *)&cmd;
   1471 		xs->cmdlen = sizeof(cmd);
   1472 		xs->resid = nwrt * sectorsize;
   1473 		xs->error = XS_NOERROR;
   1474 		xs->bp = 0;
   1475 		xs->data = va;
   1476 		xs->datalen = nwrt * sectorsize;
   1477 
   1478 		/*
   1479 		 * Pass all this info to the scsi driver.
   1480 		 */
   1481 		scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
   1482 		if ((xs->xs_status & XS_STS_DONE) == 0 ||
   1483 		    xs->error != XS_NOERROR)
   1484 			return (EIO);
   1485 #else	/* SD_DUMP_NOT_TRUSTED */
   1486 		/* Let's just talk about this first... */
   1487 		printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
   1488 		delay(500 * 1000);	/* half a second */
   1489 #endif	/* SD_DUMP_NOT_TRUSTED */
   1490 
   1491 		/* update block count */
   1492 		totwrt -= nwrt;
   1493 		blkno += nwrt;
   1494 		va += sectorsize * nwrt;
   1495 	}
   1496 	sddoingadump = 0;
   1497 	return (0);
   1498 }
   1499