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