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