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