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