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