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