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