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