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