Home | History | Annotate | Line # | Download | only in scsipi
sd.c revision 1.132
      1 /*	$NetBSD: sd.c,v 1.132 1998/08/15 01:10:54 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995, 1997 Charles M. Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Charles M. Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Originally written by Julian Elischer (julian (at) dialix.oz.au)
     34  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     35  *
     36  * TRW Financial Systems, in accordance with their agreement with Carnegie
     37  * Mellon University, makes this software available to CMU to distribute
     38  * or use in any manner that they see fit as long as this message is kept with
     39  * the software. For this reason TFS also grants any other persons or
     40  * organisations permission to use or modify this software.
     41  *
     42  * TFS supplies this software to be publicly redistributed
     43  * on the understanding that TFS is not responsible for the correct
     44  * functioning of this software in any circumstances.
     45  *
     46  * Ported to run under 386BSD by Julian Elischer (julian (at) dialix.oz.au) Sept 1992
     47  */
     48 
     49 #include "opt_scsi.h"
     50 #include "rnd.h"
     51 
     52 #include <sys/types.h>
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/kernel.h>
     56 #include <sys/file.h>
     57 #include <sys/stat.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/buf.h>
     60 #include <sys/uio.h>
     61 #include <sys/malloc.h>
     62 #include <sys/errno.h>
     63 #include <sys/device.h>
     64 #include <sys/disklabel.h>
     65 #include <sys/disk.h>
     66 #include <sys/proc.h>
     67 #include <sys/conf.h>
     68 #if NRND > 0
     69 #include <sys/rnd.h>
     70 #endif
     71 
     72 #include <dev/scsipi/scsipi_all.h>
     73 #include <dev/scsipi/scsi_all.h>
     74 #include <dev/scsipi/scsipi_disk.h>
     75 #include <dev/scsipi/scsi_disk.h>
     76 #include <dev/scsipi/scsiconf.h>
     77 #include <dev/scsipi/sdvar.h>
     78 
     79 #include "sd.h"		/* NSD_SCSIBUS and NSD_ATAPIBUS come from here */
     80 
     81 #ifndef	SDOUTSTANDING
     82 #define	SDOUTSTANDING	4
     83 #endif
     84 
     85 #define	SDUNIT(dev)			DISKUNIT(dev)
     86 #define	SDPART(dev)			DISKPART(dev)
     87 #define	MAKESDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
     88 
     89 #define	SDLABELDEV(dev)	(MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
     90 
     91 int	sdlock __P((struct sd_softc *));
     92 void	sdunlock __P((struct sd_softc *));
     93 void	sdminphys __P((struct buf *));
     94 void	sdgetdefaultlabel __P((struct sd_softc *, struct disklabel *));
     95 void	sdgetdisklabel __P((struct sd_softc *));
     96 void	sdstart __P((void *));
     97 void	sddone __P((struct scsipi_xfer *));
     98 void	sd_shutdown __P((void *));
     99 int	sd_reassign_blocks __P((struct sd_softc *, u_long));
    100 int	sd_interpret_sense __P((struct scsipi_xfer *));
    101 
    102 extern struct cfdriver sd_cd;
    103 
    104 struct dkdriver sddkdriver = { sdstrategy };
    105 
    106 struct scsipi_device sd_switch = {
    107 	sd_interpret_sense,	/* check our error handler first */
    108 	sdstart,		/* have a queue, served by this */
    109 	NULL,			/* have no async handler */
    110 	sddone,			/* deal with stats at interrupt time */
    111 };
    112 
    113 /*
    114  * The routine called by the low level scsi routine when it discovers
    115  * a device suitable for this driver.
    116  */
    117 void
    118 sdattach(parent, sd, sc_link, ops)
    119 	struct device *parent;
    120 	struct sd_softc *sd;
    121 	struct scsipi_link *sc_link;
    122 	const struct sd_ops *ops;
    123 {
    124 	int error, result;
    125 	struct disk_parms *dp = &sd->params;
    126 
    127 	SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: "));
    128 
    129 	/*
    130 	 * Store information needed to contact our base driver
    131 	 */
    132 	sd->sc_link = sc_link;
    133 	sd->sc_ops = ops;
    134 	sc_link->device = &sd_switch;
    135 	sc_link->device_softc = sd;
    136 	if (sc_link->openings > SDOUTSTANDING)
    137 		sc_link->openings = SDOUTSTANDING;
    138 
    139 	/*
    140 	 * Initialize and attach the disk structure.
    141 	 */
    142 	sd->sc_dk.dk_driver = &sddkdriver;
    143 	sd->sc_dk.dk_name = sd->sc_dev.dv_xname;
    144 	disk_attach(&sd->sc_dk);
    145 
    146 #if !defined(i386)
    147 	dk_establish(&sd->sc_dk, &sd->sc_dev);		/* XXX */
    148 #endif
    149 
    150 	/*
    151 	 * Use the subdriver to request information regarding
    152 	 * the drive. We cannot use interrupts yet, so the
    153 	 * request must specify this.
    154 	 */
    155 	printf("\n");
    156 
    157 #if NSD_ATAPIBUS > 0
    158 	/* XXX BEGIN HACK ALERT!!! */
    159 	if (sc_link->type == BUS_ATAPI) {
    160 		/*
    161 		 * The ATAPI sense data handling is _TOTALLY_ broken.
    162 		 * In particular, it _never_ does a REQUEST_SENSE to get
    163 		 * sense data!  This causes UNIT ATTENTION to be not
    164 		 * cleared properly for some ATAPI devices (e.g. Zip),
    165 		 * which messes up the probe.
    166 		 *
    167 		 * This is the easiest way to get around the problem.
    168 		 *
    169 		 * YUCK!
    170 		 */
    171 		(void)scsipi_test_unit_ready(sc_link, SCSI_AUTOCONF |
    172 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
    173 		    SCSI_IGNORE_NOT_READY);
    174 	}
    175 	/* XXX END HACK ALERT!!! */
    176 #endif /* NSD_ATAPIBUS > 0 */
    177 
    178 	if ((sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) {
    179 		error = scsipi_start(sd->sc_link, SSS_START,
    180 		    SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST |
    181 		    SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT);
    182 	} else
    183 		error = 0;
    184 
    185 	if (error)
    186 		result = SDGP_RESULT_OFFLINE;
    187 	else
    188 		result = (*sd->sc_ops->sdo_get_parms)(sd, &sd->params,
    189 		    SCSI_AUTOCONF);
    190 	printf("%s: ", sd->sc_dev.dv_xname);
    191 	switch (result) {
    192 	case SDGP_RESULT_OK:
    193 	        printf("%ldMB, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %ld sectors",
    194 		    dp->disksize / (1048576 / dp->blksize), dp->cyls,
    195 		    dp->heads, dp->sectors, dp->blksize, dp->disksize);
    196 		break;
    197 
    198 	case SDGP_RESULT_OFFLINE:
    199 		printf("drive offline");
    200 		break;
    201 
    202 	case SDGP_RESULT_UNFORMATTED:
    203 		printf("unformatted media");
    204 		break;
    205 
    206 #ifdef DIAGNOSTIC
    207 	default:
    208 		panic("sdattach: unknown result from get_parms");
    209 		break;
    210 #endif
    211 	}
    212 	printf("\n");
    213 
    214 	/*
    215 	 * Establish a shutdown hook so that we can ensure that
    216 	 * our data has actually made it onto the platter at
    217 	 * shutdown time.  Note that this relies on the fact
    218 	 * that the shutdown hook code puts us at the head of
    219 	 * the list (thus guaranteeing that our hook runs before
    220 	 * our ancestors').
    221 	 */
    222 	if ((sd->sc_sdhook =
    223 	    shutdownhook_establish(sd_shutdown, sd)) == NULL)
    224 		printf("%s: WARNING: unable to establish shutdown hook\n",
    225 		    sd->sc_dev.dv_xname);
    226 
    227 #if NRND > 0
    228 	/*
    229 	 * attach the device into the random source list
    230 	 */
    231 	rnd_attach_source(&sd->rnd_source, sd->sc_dev.dv_xname, RND_TYPE_DISK);
    232 #endif
    233 }
    234 
    235 /*
    236  * Wait interruptibly for an exclusive lock.
    237  *
    238  * XXX
    239  * Several drivers do this; it should be abstracted and made MP-safe.
    240  */
    241 int
    242 sdlock(sd)
    243 	struct sd_softc *sd;
    244 {
    245 	int error;
    246 
    247 	while ((sd->flags & SDF_LOCKED) != 0) {
    248 		sd->flags |= SDF_WANTED;
    249 		if ((error = tsleep(sd, PRIBIO | PCATCH, "sdlck", 0)) != 0)
    250 			return (error);
    251 	}
    252 	sd->flags |= SDF_LOCKED;
    253 	return (0);
    254 }
    255 
    256 /*
    257  * Unlock and wake up any waiters.
    258  */
    259 void
    260 sdunlock(sd)
    261 	struct sd_softc *sd;
    262 {
    263 
    264 	sd->flags &= ~SDF_LOCKED;
    265 	if ((sd->flags & SDF_WANTED) != 0) {
    266 		sd->flags &= ~SDF_WANTED;
    267 		wakeup(sd);
    268 	}
    269 }
    270 
    271 /*
    272  * open the device. Make sure the partition info is a up-to-date as can be.
    273  */
    274 int
    275 sdopen(dev, flag, fmt, p)
    276 	dev_t dev;
    277 	int flag, fmt;
    278 	struct proc *p;
    279 {
    280 	struct sd_softc *sd;
    281 	struct scsipi_link *sc_link;
    282 	int unit, part;
    283 	int error;
    284 
    285 	unit = SDUNIT(dev);
    286 	if (unit >= sd_cd.cd_ndevs)
    287 		return (ENXIO);
    288 	sd = sd_cd.cd_devs[unit];
    289 	if (sd == NULL)
    290 		return (ENXIO);
    291 
    292 	sc_link = sd->sc_link;
    293 
    294 	SC_DEBUG(sc_link, SDEV_DB1,
    295 	    ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
    296 	    sd_cd.cd_ndevs, SDPART(dev)));
    297 
    298 	if ((error = sdlock(sd)) != 0)
    299 		return (error);
    300 
    301 	if (sd->sc_dk.dk_openmask != 0) {
    302 		/*
    303 		 * If any partition is open, but the disk has been invalidated,
    304 		 * disallow further opens.
    305 		 */
    306 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
    307 			error = EIO;
    308 			goto bad3;
    309 		}
    310 	} else {
    311 		/* Check that it is still responding and ok. */
    312 		error = scsipi_test_unit_ready(sc_link,
    313 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE |
    314 		    SCSI_IGNORE_NOT_READY);
    315 		if (error)
    316 			goto bad3;
    317 
    318 		/* Start the pack spinning if necessary. */
    319 		if ((sc_link->quirks & SDEV_NOSTARTUNIT) == 0) {
    320 			error = scsipi_start(sc_link, SSS_START,
    321 			    SCSI_IGNORE_ILLEGAL_REQUEST |
    322 			    SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT);
    323 			if (error)
    324 				goto bad3;
    325 		}
    326 
    327 		sc_link->flags |= SDEV_OPEN;
    328 
    329 		/* Lock the pack in. */
    330 		error = scsipi_prevent(sc_link, PR_PREVENT,
    331 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
    332 		if (error)
    333 			goto bad;
    334 
    335 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
    336 			sc_link->flags |= SDEV_MEDIA_LOADED;
    337 
    338 			/*
    339 			 * Load the physical device parameters.
    340 			 *
    341 			 * Note that if media is present but unformatted,
    342 			 * we allow the open (so that it can be formatted!).
    343 			 * The drive should refuse real I/O, if the media is
    344 			 * unformatted.
    345 			 */
    346 			if ((*sd->sc_ops->sdo_get_parms)(sd, &sd->params,
    347 			    0) == SDGP_RESULT_OFFLINE) {
    348 				error = ENXIO;
    349 				goto bad2;
    350 			}
    351 			SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
    352 
    353 			/* Load the partition info if not already loaded. */
    354 			sdgetdisklabel(sd);
    355 			SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded "));
    356 		}
    357 	}
    358 
    359 	part = SDPART(dev);
    360 
    361 	/* Check that the partition exists. */
    362 	if (part != RAW_PART &&
    363 	    (part >= sd->sc_dk.dk_label->d_npartitions ||
    364 	     sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    365 		error = ENXIO;
    366 		goto bad;
    367 	}
    368 
    369 	/* Insure only one open at a time. */
    370 	switch (fmt) {
    371 	case S_IFCHR:
    372 		sd->sc_dk.dk_copenmask |= (1 << part);
    373 		break;
    374 	case S_IFBLK:
    375 		sd->sc_dk.dk_bopenmask |= (1 << part);
    376 		break;
    377 	}
    378 	sd->sc_dk.dk_openmask =
    379 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
    380 
    381 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
    382 	sdunlock(sd);
    383 	return (0);
    384 
    385 bad2:
    386 	sc_link->flags &= ~SDEV_MEDIA_LOADED;
    387 
    388 bad:
    389 	if (sd->sc_dk.dk_openmask == 0) {
    390 		scsipi_prevent(sc_link, PR_ALLOW,
    391 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
    392 		sc_link->flags &= ~SDEV_OPEN;
    393 	}
    394 
    395 bad3:
    396 	sdunlock(sd);
    397 	return (error);
    398 }
    399 
    400 /*
    401  * close the device.. only called if we are the LAST occurence of an open
    402  * device.  Convenient now but usually a pain.
    403  */
    404 int
    405 sdclose(dev, flag, fmt, p)
    406 	dev_t dev;
    407 	int flag, fmt;
    408 	struct proc *p;
    409 {
    410 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
    411 	int part = SDPART(dev);
    412 	int error;
    413 
    414 	if ((error = sdlock(sd)) != 0)
    415 		return (error);
    416 
    417 	switch (fmt) {
    418 	case S_IFCHR:
    419 		sd->sc_dk.dk_copenmask &= ~(1 << part);
    420 		break;
    421 	case S_IFBLK:
    422 		sd->sc_dk.dk_bopenmask &= ~(1 << part);
    423 		break;
    424 	}
    425 	sd->sc_dk.dk_openmask =
    426 	    sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
    427 
    428 	if (sd->sc_dk.dk_openmask == 0) {
    429 		/*
    430 		 * If the disk cache needs flushing, and the disk supports
    431 		 * it, do it now.
    432 		 */
    433 		if ((sd->flags & SDF_DIRTY) != 0 &&
    434 		    sd->sc_ops->sdo_flush != NULL)
    435 			(*sd->sc_ops->sdo_flush)(sd, 0);
    436 
    437 		/* XXXX Must wait for I/O to complete! */
    438 
    439 		scsipi_prevent(sd->sc_link, PR_ALLOW,
    440 		    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
    441 		sd->sc_link->flags &= ~(SDEV_OPEN|SDEV_MEDIA_LOADED);
    442 	}
    443 
    444 	sdunlock(sd);
    445 	return (0);
    446 }
    447 
    448 /*
    449  * Actually translate the requested transfer into one the physical driver
    450  * can understand.  The transfer is described by a buf and will include
    451  * only one physical transfer.
    452  */
    453 void
    454 sdstrategy(bp)
    455 	struct buf *bp;
    456 {
    457 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
    458 	int s;
    459 
    460 	SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy "));
    461 	SC_DEBUG(sd->sc_link, SDEV_DB1,
    462 	    ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
    463 	/*
    464 	 * The transfer must be a whole number of blocks.
    465 	 */
    466 	if ((bp->b_bcount % sd->sc_dk.dk_label->d_secsize) != 0) {
    467 		bp->b_error = EINVAL;
    468 		goto bad;
    469 	}
    470 	/*
    471 	 * If the device has been made invalid, error out
    472 	 */
    473 	if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
    474 		bp->b_error = EIO;
    475 		goto bad;
    476 	}
    477 	/*
    478 	 * If it's a null transfer, return immediatly
    479 	 */
    480 	if (bp->b_bcount == 0)
    481 		goto done;
    482 
    483 	/*
    484 	 * Do bounds checking, adjust transfer. if error, process.
    485 	 * If end of partition, just return.
    486 	 */
    487 	if (SDPART(bp->b_dev) != RAW_PART &&
    488 	    bounds_check_with_label(bp, sd->sc_dk.dk_label,
    489 	    (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
    490 		goto done;
    491 
    492 	s = splbio();
    493 
    494 	/*
    495 	 * Place it in the queue of disk activities for this disk
    496 	 */
    497 	disksort(&sd->buf_queue, bp);
    498 
    499 	/*
    500 	 * Tell the device to get going on the transfer if it's
    501 	 * not doing anything, otherwise just wait for completion
    502 	 */
    503 	sdstart(sd);
    504 
    505 	splx(s);
    506 	return;
    507 
    508 bad:
    509 	bp->b_flags |= B_ERROR;
    510 done:
    511 	/*
    512 	 * Correctly set the buf to indicate a completed xfer
    513 	 */
    514 	bp->b_resid = bp->b_bcount;
    515 	biodone(bp);
    516 }
    517 
    518 /*
    519  * sdstart looks to see if there is a buf waiting for the device
    520  * and that the device is not already busy. If both are true,
    521  * It dequeues the buf and creates a scsi command to perform the
    522  * transfer in the buf. The transfer request will call scsipi_done
    523  * on completion, which will in turn call this routine again
    524  * so that the next queued transfer is performed.
    525  * The bufs are queued by the strategy routine (sdstrategy)
    526  *
    527  * This routine is also called after other non-queued requests
    528  * have been made of the scsi driver, to ensure that the queue
    529  * continues to be drained.
    530  *
    531  * must be called at the correct (highish) spl level
    532  * sdstart() is called at splbio from sdstrategy and scsipi_done
    533  */
    534 void
    535 sdstart(v)
    536 	register void *v;
    537 {
    538 	register struct sd_softc *sd = v;
    539 	register struct	scsipi_link *sc_link = sd->sc_link;
    540 	struct disklabel *lp = sd->sc_dk.dk_label;
    541 	struct buf *bp = 0;
    542 	struct buf *dp;
    543 	struct scsipi_rw_big cmd_big;
    544 #if NSD_SCSIBUS > 0
    545 	struct scsi_rw cmd_small;
    546 #endif
    547 	struct scsipi_generic *cmdp;
    548 	int blkno, nblks, cmdlen, error;
    549 	struct partition *p;
    550 
    551 	SC_DEBUG(sc_link, SDEV_DB2, ("sdstart "));
    552 	/*
    553 	 * Check if the device has room for another command
    554 	 */
    555 	while (sc_link->openings > 0) {
    556 		/*
    557 		 * there is excess capacity, but a special waits
    558 		 * It'll need the adapter as soon as we clear out of the
    559 		 * way and let it run (user level wait).
    560 		 */
    561 		if (sc_link->flags & SDEV_WAITING) {
    562 			sc_link->flags &= ~SDEV_WAITING;
    563 			wakeup((caddr_t)sc_link);
    564 			return;
    565 		}
    566 
    567 		/*
    568 		 * See if there is a buf with work for us to do..
    569 		 */
    570 		dp = &sd->buf_queue;
    571 		if ((bp = dp->b_actf) == NULL)	/* yes, an assign */
    572 			return;
    573 		dp->b_actf = bp->b_actf;
    574 
    575 		/*
    576 		 * If the device has become invalid, abort all the
    577 		 * reads and writes until all files have been closed and
    578 		 * re-opened
    579 		 */
    580 		if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
    581 			bp->b_error = EIO;
    582 			bp->b_flags |= B_ERROR;
    583 			bp->b_resid = bp->b_bcount;
    584 			biodone(bp);
    585 			continue;
    586 		}
    587 
    588 		/*
    589 		 * We have a buf, now we should make a command
    590 		 *
    591 		 * First, translate the block to absolute and put it in terms
    592 		 * of the logical blocksize of the device.
    593 		 */
    594 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    595 		if (SDPART(bp->b_dev) != RAW_PART) {
    596 			p = &lp->d_partitions[SDPART(bp->b_dev)];
    597 			blkno += p->p_offset;
    598 		}
    599 		nblks = howmany(bp->b_bcount, lp->d_secsize);
    600 
    601 #if NSD_SCSIBUS > 0
    602 		/*
    603 		 *  Fill out the scsi command.  If the transfer will
    604 		 *  fit in a "small" cdb, use it.
    605 		 */
    606 		if (((blkno & 0x1fffff) == blkno) &&
    607 		    ((nblks & 0xff) == nblks) && sc_link->type == BUS_SCSI) {
    608 			/*
    609 			 * We can fit in a small cdb.
    610 			 */
    611 			bzero(&cmd_small, sizeof(cmd_small));
    612 			cmd_small.opcode = (bp->b_flags & B_READ) ?
    613 			    SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
    614 			_lto3b(blkno, cmd_small.addr);
    615 			cmd_small.length = nblks & 0xff;
    616 			cmdlen = sizeof(cmd_small);
    617 			cmdp = (struct scsipi_generic *)&cmd_small;
    618 		} else
    619 #endif
    620 		{
    621 			/*
    622 			 * Need a large cdb.
    623 			 */
    624 			bzero(&cmd_big, sizeof(cmd_big));
    625 			cmd_big.opcode = (bp->b_flags & B_READ) ?
    626 			    READ_BIG : WRITE_BIG;
    627 			_lto4b(blkno, cmd_big.addr);
    628 			_lto2b(nblks, cmd_big.length);
    629 			cmdlen = sizeof(cmd_big);
    630 			cmdp = (struct scsipi_generic *)&cmd_big;
    631 		}
    632 
    633 		/* Instrumentation. */
    634 		disk_busy(&sd->sc_dk);
    635 
    636 		/*
    637 		 * Mark the disk dirty so that the cache will be
    638 		 * flushed on close.
    639 		 */
    640 		sd->flags |= SDF_DIRTY;
    641 
    642 		/*
    643 		 * Call the routine that chats with the adapter.
    644 		 * Note: we cannot sleep as we may be an interrupt
    645 		 */
    646 		error = scsipi_command(sc_link, cmdp, cmdlen,
    647 		    (u_char *)bp->b_data, bp->b_bcount,
    648 		    SDRETRIES, 60000, bp, SCSI_NOSLEEP |
    649 		    ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT));
    650 		if (error) {
    651 			disk_unbusy(&sd->sc_dk, 0);
    652 			printf("%s: not queued, error %d\n",
    653 			    sd->sc_dev.dv_xname, error);
    654 		}
    655 	}
    656 }
    657 
    658 void
    659 sddone(xs)
    660 	struct scsipi_xfer *xs;
    661 {
    662 	struct sd_softc *sd = xs->sc_link->device_softc;
    663 
    664 	if (sd->flags & SDF_FLUSHING) {
    665 		/* Flush completed, no longer dirty. */
    666 		sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
    667 	}
    668 
    669 	if (xs->bp != NULL) {
    670 		disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid);
    671 #if NRND > 0
    672 		rnd_add_uint32(&sd->rnd_source, xs->bp->b_blkno);
    673 #endif
    674 	}
    675 }
    676 
    677 void
    678 sdminphys(bp)
    679 	struct buf *bp;
    680 {
    681 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
    682 	long max;
    683 
    684 	/*
    685 	 * If the device is ancient, we want to make sure that
    686 	 * the transfer fits into a 6-byte cdb.
    687 	 *
    688 	 * XXX Note that the SCSI-I spec says that 256-block transfers
    689 	 * are allowed in a 6-byte read/write, and are specified
    690 	 * by settng the "length" to 0.  However, we're conservative
    691 	 * here, allowing only 255-block transfers in case an
    692 	 * ancient device gets confused by length == 0.  A length of 0
    693 	 * in a 10-byte read/write actually means 0 blocks.
    694 	 */
    695 	if (sd->flags & SDF_ANCIENT) {
    696 		max = sd->sc_dk.dk_label->d_secsize * 0xff;
    697 
    698 		if (bp->b_bcount > max)
    699 			bp->b_bcount = max;
    700 	}
    701 
    702 	(*sd->sc_link->adapter->scsipi_minphys)(bp);
    703 }
    704 
    705 int
    706 sdread(dev, uio, ioflag)
    707 	dev_t dev;
    708 	struct uio *uio;
    709 	int ioflag;
    710 {
    711 
    712 	return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
    713 }
    714 
    715 int
    716 sdwrite(dev, uio, ioflag)
    717 	dev_t dev;
    718 	struct uio *uio;
    719 	int ioflag;
    720 {
    721 
    722 	return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
    723 }
    724 
    725 /*
    726  * Perform special action on behalf of the user
    727  * Knows about the internals of this device
    728  */
    729 int
    730 sdioctl(dev, cmd, addr, flag, p)
    731 	dev_t dev;
    732 	u_long cmd;
    733 	caddr_t addr;
    734 	int flag;
    735 	struct proc *p;
    736 {
    737 	struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
    738 	int error;
    739 
    740 	SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd));
    741 
    742 	/*
    743 	 * If the device is not valid.. abandon ship
    744 	 */
    745 	if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0)
    746 		return (EIO);
    747 
    748 	switch (cmd) {
    749 	case DIOCGDINFO:
    750 		*(struct disklabel *)addr = *(sd->sc_dk.dk_label);
    751 		return (0);
    752 
    753 	case DIOCGPART:
    754 		((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
    755 		((struct partinfo *)addr)->part =
    756 		    &sd->sc_dk.dk_label->d_partitions[SDPART(dev)];
    757 		return (0);
    758 
    759 	case DIOCWDINFO:
    760 	case DIOCSDINFO:
    761 		if ((flag & FWRITE) == 0)
    762 			return (EBADF);
    763 
    764 		if ((error = sdlock(sd)) != 0)
    765 			return (error);
    766 		sd->flags |= SDF_LABELLING;
    767 
    768 		error = setdisklabel(sd->sc_dk.dk_label,
    769 		    (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0,
    770 		    sd->sc_dk.dk_cpulabel);
    771 		if (error == 0) {
    772 			if (cmd == DIOCWDINFO)
    773 				error = writedisklabel(SDLABELDEV(dev),
    774 				    sdstrategy, sd->sc_dk.dk_label,
    775 				    sd->sc_dk.dk_cpulabel);
    776 		}
    777 
    778 		sd->flags &= ~SDF_LABELLING;
    779 		sdunlock(sd);
    780 		return (error);
    781 
    782 	case DIOCWLABEL:
    783 		if ((flag & FWRITE) == 0)
    784 			return (EBADF);
    785 		if (*(int *)addr)
    786 			sd->flags |= SDF_WLABEL;
    787 		else
    788 			sd->flags &= ~SDF_WLABEL;
    789 		return (0);
    790 
    791 	case DIOCLOCK:
    792 		return (scsipi_prevent(sd->sc_link,
    793 		    (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
    794 
    795 	case DIOCEJECT:
    796 		return ((sd->sc_link->flags & SDEV_REMOVABLE) == 0 ? ENOTTY :
    797 		    scsipi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0));
    798 
    799 	case DIOCGDEFLABEL:
    800 		sdgetdefaultlabel(sd, (struct disklabel *)addr);
    801 		return (0);
    802 
    803 	default:
    804 		if (SDPART(dev) != RAW_PART)
    805 			return (ENOTTY);
    806 		return (scsipi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p));
    807 	}
    808 
    809 #ifdef DIAGNOSTIC
    810 	panic("sdioctl: impossible");
    811 #endif
    812 }
    813 
    814 void
    815 sdgetdefaultlabel(sd, lp)
    816 	struct sd_softc *sd;
    817 	struct disklabel *lp;
    818 {
    819 
    820 	bzero(lp, sizeof(struct disklabel));
    821 
    822 	lp->d_secsize = sd->params.blksize;
    823 	lp->d_ntracks = sd->params.heads;
    824 	lp->d_nsectors = sd->params.sectors;
    825 	lp->d_ncylinders = sd->params.cyls;
    826 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    827 
    828 	switch (sd->sc_link->type) {
    829 #if NSD_SCSIBUS > 0
    830 	    case BUS_SCSI:
    831 		lp->d_type = DTYPE_SCSI;
    832 		break;
    833 #endif
    834 #if NSD_ATAPIBUS > 0
    835 	    case BUS_ATAPI:
    836 		lp->d_type = DTYPE_ATAPI;
    837 		break;
    838 #endif
    839 	}
    840 	strncpy(lp->d_typename, sd->name, 16);
    841 	strncpy(lp->d_packname, "fictitious", 16);
    842 	lp->d_secperunit = sd->params.disksize;
    843 	lp->d_rpm = sd->params.rot_rate;
    844 	lp->d_interleave = 1;
    845 	lp->d_flags = 0;
    846 
    847 	lp->d_partitions[RAW_PART].p_offset = 0;
    848 	lp->d_partitions[RAW_PART].p_size =
    849 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    850 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    851 	lp->d_npartitions = RAW_PART + 1;
    852 
    853 	lp->d_magic = DISKMAGIC;
    854 	lp->d_magic2 = DISKMAGIC;
    855 	lp->d_checksum = dkcksum(lp);
    856 }
    857 
    858 
    859 /*
    860  * Load the label information on the named device
    861  */
    862 void
    863 sdgetdisklabel(sd)
    864 	struct sd_softc *sd;
    865 {
    866 	struct disklabel *lp = sd->sc_dk.dk_label;
    867 	char *errstring;
    868 
    869 	bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
    870 
    871 	sdgetdefaultlabel(sd, lp);
    872 
    873 	if (lp->d_secpercyl == 0) {
    874 		lp->d_secpercyl = 100;
    875 		/* as long as it's not 0 - readdisklabel divides by it (?) */
    876 	}
    877 
    878 	/*
    879 	 * Call the generic disklabel extraction routine
    880 	 */
    881 	errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
    882 	    sdstrategy, lp, sd->sc_dk.dk_cpulabel);
    883 	if (errstring) {
    884 		printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
    885 		return;
    886 	}
    887 }
    888 
    889 void
    890 sd_shutdown(arg)
    891 	void *arg;
    892 {
    893 	struct sd_softc *sd = arg;
    894 
    895 	/*
    896 	 * If the disk cache needs to be flushed, and the disk supports
    897 	 * it, flush it.  We're cold at this point, so we poll for
    898 	 * completion.
    899 	 */
    900 	if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL)
    901 		(*sd->sc_ops->sdo_flush)(sd, SCSI_AUTOCONF);
    902 }
    903 
    904 /*
    905  * Tell the device to map out a defective block
    906  */
    907 int
    908 sd_reassign_blocks(sd, blkno)
    909 	struct sd_softc *sd;
    910 	u_long blkno;
    911 {
    912 	struct scsi_reassign_blocks scsipi_cmd;
    913 	struct scsi_reassign_blocks_data rbdata;
    914 
    915 	bzero(&scsipi_cmd, sizeof(scsipi_cmd));
    916 	bzero(&rbdata, sizeof(rbdata));
    917 	scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS;
    918 
    919 	_lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
    920 	_lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
    921 
    922 	return (scsipi_command(sd->sc_link,
    923 	    (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
    924 	    (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL,
    925 	    SCSI_DATA_OUT));
    926 }
    927 
    928 /*
    929  * Check Errors
    930  */
    931 int
    932 sd_interpret_sense(xs)
    933 	struct scsipi_xfer *xs;
    934 {
    935 	struct scsipi_link *sc_link = xs->sc_link;
    936 	struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
    937 	struct sd_softc *sd = sc_link->device_softc;
    938 	int retval = SCSIRET_CONTINUE;
    939 
    940 	/*
    941 	 * If the device is not open yet, let the generic code handle it.
    942 	 */
    943 	if ((sc_link->flags & SDEV_OPEN) == 0) {
    944 		return (retval);
    945 	}
    946 
    947 	/*
    948 	 * If it isn't a extended or extended/deferred error, let
    949 	 * the generic code handle it.
    950 	 */
    951 	if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
    952 	    (sense->error_code & SSD_ERRCODE) != 0x71) {	/* DEFFERRED */
    953 		return (retval);
    954 	}
    955 
    956 	if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
    957 	    sense->add_sense_code == 0x4) {
    958 		if (sense->add_sense_code_qual == 0x01)	{
    959 			printf("%s: ..is spinning up...waiting\n",
    960 			    sd->sc_dev.dv_xname);
    961 			/*
    962 			 * I really need a sdrestart function I can call here.
    963 			 */
    964 			delay(1000000 * 5);	/* 5 seconds */
    965 			retval = SCSIRET_RETRY;
    966 		} else if ((sense->add_sense_code_qual == 0x2) &&
    967 		    (sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) {
    968 			if (sd->sc_link->flags & SDEV_REMOVABLE) {
    969 				printf("%s: removable disk stopped- not "
    970 				    "restarting\n", sd->sc_dev.dv_xname);
    971 				retval = EIO;
    972 			} else {
    973 				printf("%s: respinning up disk\n",
    974 				    sd->sc_dev.dv_xname);
    975 				retval = scsipi_start(sd->sc_link, SSS_START,
    976 				    SCSI_URGENT | SCSI_NOSLEEP | SCSI_POLL);
    977 				if (retval != 0) {
    978 					printf("%s: respin of disk failed-%d\n",
    979 					    sd->sc_dev.dv_xname, retval);
    980 					retval = EIO;
    981 				} else {
    982 					retval = SCSIRET_RETRY;
    983 				}
    984 			}
    985 		}
    986 	}
    987 	return (retval);
    988 }
    989 
    990 
    991 int
    992 sdsize(dev)
    993 	dev_t dev;
    994 {
    995 	struct sd_softc *sd;
    996 	int part, unit, omask;
    997 	int size;
    998 
    999 	unit = SDUNIT(dev);
   1000 	if (unit >= sd_cd.cd_ndevs)
   1001 		return (-1);
   1002 	sd = sd_cd.cd_devs[unit];
   1003 	if (sd == NULL)
   1004 		return (-1);
   1005 
   1006 	part = SDPART(dev);
   1007 	omask = sd->sc_dk.dk_openmask & (1 << part);
   1008 
   1009 	if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
   1010 		return (-1);
   1011 	if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1012 		size = -1;
   1013 	else
   1014 		size = sd->sc_dk.dk_label->d_partitions[part].p_size *
   1015 		    (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1016 	if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
   1017 		return (-1);
   1018 	return (size);
   1019 }
   1020 
   1021 #ifndef __BDEVSW_DUMP_OLD_TYPE
   1022 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
   1023 static struct scsipi_xfer sx;
   1024 static int sddoingadump;
   1025 
   1026 /*
   1027  * dump all of physical memory into the partition specified, starting
   1028  * at offset 'dumplo' into the partition.
   1029  */
   1030 int
   1031 sddump(dev, blkno, va, size)
   1032 	dev_t dev;
   1033 	daddr_t blkno;
   1034 	caddr_t va;
   1035 	size_t size;
   1036 {
   1037 	struct sd_softc *sd;	/* disk unit to do the I/O */
   1038 	struct disklabel *lp;	/* disk's disklabel */
   1039 	int	unit, part;
   1040 	int	sectorsize;	/* size of a disk sector */
   1041 	int	nsects;		/* number of sectors in partition */
   1042 	int	sectoff;	/* sector offset of partition */
   1043 	int	totwrt;		/* total number of sectors left to write */
   1044 	int	nwrt;		/* current number of sectors to write */
   1045 	struct scsipi_rw_big cmd;	/* write command */
   1046 	struct scsipi_xfer *xs;	/* ... convenience */
   1047 	int	retval;
   1048 
   1049 	/* Check if recursive dump; if so, punt. */
   1050 	if (sddoingadump)
   1051 		return (EFAULT);
   1052 
   1053 	/* Mark as active early. */
   1054 	sddoingadump = 1;
   1055 
   1056 	unit = SDUNIT(dev);	/* Decompose unit & partition. */
   1057 	part = SDPART(dev);
   1058 
   1059 	/* Check for acceptable drive number. */
   1060 	if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
   1061 		return (ENXIO);
   1062 
   1063 	/*
   1064 	 * XXX Can't do this check, since the media might have been
   1065 	 * XXX marked `invalid' by successful unmounting of all
   1066 	 * XXX filesystems.
   1067 	 */
   1068 #if 0
   1069 	/* Make sure it was initialized. */
   1070 	if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED)
   1071 		return (ENXIO);
   1072 #endif
   1073 
   1074 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1075 	lp = sd->sc_dk.dk_label;
   1076 	sectorsize = lp->d_secsize;
   1077 	if ((size % sectorsize) != 0)
   1078 		return (EFAULT);
   1079 	totwrt = size / sectorsize;
   1080 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1081 
   1082 	nsects = lp->d_partitions[part].p_size;
   1083 	sectoff = lp->d_partitions[part].p_offset;
   1084 
   1085 	/* Check transfer bounds against partition size. */
   1086 	if ((blkno < 0) || ((blkno + totwrt) > nsects))
   1087 		return (EINVAL);
   1088 
   1089 	/* Offset block number to start of partition. */
   1090 	blkno += sectoff;
   1091 
   1092 	xs = &sx;
   1093 
   1094 	while (totwrt > 0) {
   1095 		nwrt = totwrt;		/* XXX */
   1096 #ifndef	SD_DUMP_NOT_TRUSTED
   1097 		/*
   1098 		 *  Fill out the scsi command
   1099 		 */
   1100 		bzero(&cmd, sizeof(cmd));
   1101 		cmd.opcode = WRITE_BIG;
   1102 		_lto4b(blkno, cmd.addr);
   1103 		_lto2b(nwrt, cmd.length);
   1104 		/*
   1105 		 * Fill out the scsipi_xfer structure
   1106 		 *    Note: we cannot sleep as we may be an interrupt
   1107 		 * don't use scsipi_command() as it may want to wait
   1108 		 * for an xs.
   1109 		 */
   1110 		bzero(xs, sizeof(sx));
   1111 		xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT;
   1112 		xs->sc_link = sd->sc_link;
   1113 		xs->retries = SDRETRIES;
   1114 		xs->timeout = 10000;	/* 10000 millisecs for a disk ! */
   1115 		xs->cmd = (struct scsipi_generic *)&cmd;
   1116 		xs->cmdlen = sizeof(cmd);
   1117 		xs->resid = nwrt * sectorsize;
   1118 		xs->error = XS_NOERROR;
   1119 		xs->bp = 0;
   1120 		xs->data = va;
   1121 		xs->datalen = nwrt * sectorsize;
   1122 
   1123 		/*
   1124 		 * Pass all this info to the scsi driver.
   1125 		 */
   1126 		retval = scsipi_command_direct(xs);
   1127 		if (retval != COMPLETE)
   1128 			return (ENXIO);
   1129 #else	/* SD_DUMP_NOT_TRUSTED */
   1130 		/* Let's just talk about this first... */
   1131 		printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
   1132 		delay(500 * 1000);	/* half a second */
   1133 #endif	/* SD_DUMP_NOT_TRUSTED */
   1134 
   1135 		/* update block count */
   1136 		totwrt -= nwrt;
   1137 		blkno += nwrt;
   1138 		va += sectorsize * nwrt;
   1139 	}
   1140 	sddoingadump = 0;
   1141 	return (0);
   1142 }
   1143 #else	/* __BDEVSW_DUMP_NEW_TYPE */
   1144 int
   1145 sddump(dev, blkno, va, size)
   1146 	dev_t dev;
   1147 	daddr_t blkno;
   1148 	caddr_t va;
   1149 	size_t size;
   1150 {
   1151 
   1152 	/* Not implemented. */
   1153 	return (ENXIO);
   1154 }
   1155 #endif	/* __BDEVSW_DUMP_NEW_TYPE */
   1156