Home | History | Annotate | Line # | Download | only in scsipi
sd.c revision 1.29
      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.29 1994/04/11 03:54:10 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 	int flags;
     88 #define SDHAVELABEL	0x01	/* have read the label */
     89 #define SDDOSPART	0x02	/* Have read the DOS partition table */
     90 #define SDWRITEPROT	0x04	/* Device in readonly mode (S/W) */
     91 	struct scsi_link *sc_link;	/* contains our targ, lun etc. */
     92 	u_int32 ad_info;	/* info about the adapter */
     93 	u_int32 cmdscount;	/* cmds allowed outstanding by board */
     94 	boolean wlabel;		/* label is writable */
     95 	struct disk_parms {
     96 		u_char heads;		/* Number of heads */
     97 		u_int16 cyls;		/* Number of cylinders */
     98 		u_char sectors;		/* Number of sectors/track */
     99 		u_int32 blksize;	/* Number of bytes/sector */
    100 		u_long disksize;	/* total number sectors */
    101 	} params;
    102 	int partflags[MAXPARTITIONS];	/* per partition flags */
    103 #define SDOPEN	0x01
    104 	u_int32 openparts;		/* one bit for each open partition */
    105 	u_int32 xfer_block_wait;
    106 	struct buf buf_queue;
    107 };
    108 
    109 void sdattach __P((struct device *, struct device *, void *));
    110 
    111 struct cfdriver sdcd = {
    112 	NULL, "sd", scsi_targmatch, sdattach, DV_DISK, sizeof(struct sd_data)
    113 };
    114 
    115 int sdgetdisklabel __P((struct sd_data *));
    116 int sd_get_parms __P((struct sd_data *, int));
    117 void sdstrategy __P((struct buf *));
    118 void sdstart __P((int));
    119 
    120 struct dkdriver sddkdriver = { sdstrategy };
    121 
    122 struct scsi_device sd_switch = {
    123 	NULL,			/* Use default error handler */
    124 	sdstart,		/* have a queue, served by this */
    125 	NULL,			/* have no async handler */
    126 	NULL,			/* Use default 'done' routine */
    127 	"sd",
    128 	0
    129 };
    130 
    131 /*
    132  * The routine called by the low level scsi routine when it discovers
    133  * a device suitable for this driver.
    134  */
    135 void
    136 sdattach(parent, self, aux)
    137 	struct device *parent, *self;
    138 	void *aux;
    139 {
    140 	struct sd_data *sd = (void *)self;
    141 	struct disk_parms *dp = &sd->params;
    142 	struct scsi_link *sc_link = aux;
    143 
    144 	SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: "));
    145 
    146 	/*
    147 	 * Store information needed to contact our base driver
    148 	 */
    149 	sd->sc_link = sc_link;
    150 	sc_link->device = &sd_switch;
    151 	sc_link->dev_unit = self->dv_unit;
    152 
    153 	sd->sc_dk.dk_driver = &sddkdriver;
    154 #if !defined(i386) || defined(NEWCONFIG)
    155 	dk_establish(&sd->sc_dk, &sd->sc_dev);
    156 #endif
    157 
    158 	if (sd->sc_link->adapter->adapter_info) {
    159 		sd->ad_info = ((*(sd->sc_link->adapter->adapter_info)) (sc_link->adapter_softc));
    160 		sd->cmdscount = sd->ad_info & AD_INF_MAX_CMDS;
    161 		if (sd->cmdscount > SDOUTSTANDING)
    162 			sd->cmdscount = SDOUTSTANDING;
    163 	} else {
    164 		sd->ad_info = 1;
    165 		sd->cmdscount = 1;
    166 	}
    167 	sc_link->opennings = sd->cmdscount;
    168 
    169 	/*
    170 	 * Use the subdriver to request information regarding
    171 	 * the drive. We cannot use interrupts yet, so the
    172 	 * request must specify this.
    173 	 */
    174 	sd_get_parms(sd, SCSI_NOSLEEP | SCSI_NOMASK);
    175 	printf(": %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
    176 	    dp->disksize / ((1024L * 1024L) / dp->blksize), dp->cyls,
    177 	    dp->heads, dp->sectors, dp->blksize);
    178 }
    179 
    180 /*
    181  * open the device. Make sure the partition info is a up-to-date as can be.
    182  */
    183 int
    184 sdopen(dev)
    185 	dev_t dev;
    186 {
    187 	int error = 0;
    188 	int unit, part;
    189 	struct sd_data *sd;
    190 	struct scsi_link *sc_link;
    191 
    192 	unit = SDUNIT(dev);
    193 	part = SDPART(dev);
    194 
    195 	if (unit >= sdcd.cd_ndevs)
    196 		return ENXIO;
    197 	sd = sdcd.cd_devs[unit];
    198 	if (!sd)
    199 		return ENXIO;
    200 
    201 	sc_link = sd->sc_link;
    202 
    203 	SC_DEBUG(sc_link, SDEV_DB1,
    204 	    ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
    205 	    sdcd.cd_ndevs, part));
    206 
    207 	/*
    208 	 * If it's been invalidated, then forget the label
    209 	 */
    210 	if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
    211 		sd->flags &= ~SDHAVELABEL;
    212 
    213 		/*
    214 		 * If somebody still has it open, then forbid re-entry.
    215 		 */
    216 		if (sd->openparts)
    217 			return ENXIO;
    218 	}
    219 
    220 	/*
    221 	 * "unit attention" errors should occur here if the
    222 	 * drive has been restarted or the pack changed.
    223 	 * just ingnore the result, it's a decoy instruction
    224 	 * The error code will act on the error though
    225 	 * and invalidate any media information we had.
    226 	 */
    227 	scsi_test_unit_ready(sc_link, SCSI_SILENT);
    228 
    229 	/*
    230 	 * In case it is a funny one, tell it to start
    231 	 * not needed for most hard drives (ignore failure)
    232 	 */
    233 	scsi_start(sc_link, SSS_START, SCSI_ERR_OK | SCSI_SILENT);
    234 
    235 	/*
    236 	 * Check that it is still responding and ok.
    237 	 */
    238 	sc_link->flags |= SDEV_OPEN;	/* unit attn becomes an err now */
    239 	if (scsi_test_unit_ready(sc_link, 0)) {
    240 		SC_DEBUG(sc_link, SDEV_DB3, ("device not reponding\n"));
    241 		error = ENXIO;
    242 		goto bad;
    243 	}
    244 	SC_DEBUG(sc_link, SDEV_DB3, ("device ok\n"));
    245 
    246 	/* Lock the pack in. */
    247 	scsi_prevent(sc_link, PR_PREVENT, SCSI_ERR_OK | SCSI_SILENT);
    248 
    249 	/*
    250 	 * Load the physical device parameters
    251 	 */
    252 	if (sd_get_parms(sd, 0)) {
    253 		error = ENXIO;
    254 		goto bad;
    255 	}
    256 	SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
    257 
    258 	/*
    259 	 * Load the partition info if not already loaded.
    260 	 */
    261 	if ((error = sdgetdisklabel(sd)) && (part != RAW_PART))
    262 		goto bad;
    263 	SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded "));
    264 
    265 	/*
    266 	 * Check the partition is legal
    267 	 */
    268 	if (part >= sd->sc_dk.dk_label.d_npartitions && part != RAW_PART) {
    269 		error = ENXIO;
    270 		goto bad;
    271 	}
    272 	SC_DEBUG(sc_link, SDEV_DB3, ("partition ok"));
    273 
    274 	/*
    275 	 *  Check that the partition exists
    276 	 */
    277 	if (sd->sc_dk.dk_label.d_partitions[part].p_fstype == FS_UNUSED &&
    278 	    part != RAW_PART) {
    279 		error = ENXIO;
    280 		goto bad;
    281 	}
    282 	sd->partflags[part] |= SDOPEN;
    283 	sd->openparts |= (1 << part);
    284 	SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
    285 	sc_link->flags |= SDEV_MEDIA_LOADED;
    286 	return 0;
    287 
    288 bad:
    289 	if (!sd->openparts) {
    290 		scsi_prevent(sc_link, PR_ALLOW, SCSI_ERR_OK | SCSI_SILENT);
    291 		sc_link->flags &= ~SDEV_OPEN;
    292 	}
    293 	return error;
    294 }
    295 
    296 /*
    297  * close the device.. only called if we are the LAST occurence of an open
    298  * device.  Convenient now but usually a pain.
    299  */
    300 int
    301 sdclose(dev)
    302 	dev_t dev;
    303 {
    304 	int unit, part;
    305 	struct sd_data *sd;
    306 
    307 	unit = SDUNIT(dev);
    308 	part = SDPART(dev);
    309 	sd = sdcd.cd_devs[unit];
    310 	sd->partflags[part] &= ~SDOPEN;
    311 	sd->openparts &= ~(1 << part);
    312 	if (!sd->openparts) {
    313 		scsi_prevent(sd->sc_link, PR_ALLOW, SCSI_ERR_OK | SCSI_SILENT);
    314 		sd->sc_link->flags &= ~SDEV_OPEN;
    315 	}
    316 	return 0;
    317 }
    318 
    319 /*
    320  * trim the size of the transfer if needed, called by physio
    321  * basically the smaller of our max and the scsi driver's
    322  * minphys (note we have no max)
    323  *
    324  * Trim buffer length if buffer-size is bigger than page size
    325  */
    326 void
    327 sdminphys(bp)
    328 	struct buf *bp;
    329 {
    330 	register struct sd_data *sd = sdcd.cd_devs[SDUNIT(bp->b_dev)];
    331 
    332 	(sd->sc_link->adapter->scsi_minphys) (bp);
    333 }
    334 
    335 /*
    336  * Actually translate the requested transfer into one the physical driver
    337  * can understand.  The transfer is described by a buf and will include
    338  * only one physical transfer.
    339  */
    340 void
    341 sdstrategy(bp)
    342 	struct buf *bp;
    343 {
    344 	struct buf *dp;
    345 	int opri;
    346 	struct sd_data *sd;
    347 	int unit;
    348 
    349 	unit = SDUNIT(bp->b_dev);
    350 	sd = sdcd.cd_devs[unit];
    351 	SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy "));
    352 	SC_DEBUG(sd->sc_link, SDEV_DB1,
    353 	    ("%d bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
    354 	sdminphys(bp);
    355 	/*
    356 	 * If the device has been made invalid, error out
    357 	 */
    358 	if (!(sd->sc_link->flags & SDEV_MEDIA_LOADED)) {
    359 		sd->flags &= ~SDHAVELABEL;
    360 		bp->b_error = EIO;
    361 		goto bad;
    362 	}
    363 	/*
    364 	 * "soft" write protect check
    365 	 */
    366 	if ((sd->flags & SDWRITEPROT) && (bp->b_flags & B_READ) == 0) {
    367 		bp->b_error = EROFS;
    368 		goto bad;
    369 	}
    370 	/*
    371 	 * If it's a null transfer, return immediatly
    372 	 */
    373 	if (bp->b_bcount == 0)
    374 		goto done;
    375 	/*
    376 	 * Decide which unit and partition we are talking about
    377 	 * only raw is ok if no label
    378 	 */
    379 	if (SDPART(bp->b_dev) != RAW_PART) {
    380 		if (!(sd->flags & SDHAVELABEL)) {
    381 			bp->b_error = EIO;
    382 			goto bad;
    383 		}
    384 		/*
    385 		 * do bounds checking, adjust transfer. if error, process.
    386 		 * if end of partition, just return
    387 		 */
    388 		if (bounds_check_with_label(bp, &sd->sc_dk.dk_label,
    389 		    sd->wlabel) <= 0)
    390 			goto done;
    391 		/* otherwise, process transfer request */
    392 	}
    393 	opri = splbio();
    394 	dp = &sd->buf_queue;
    395 
    396 	/*
    397 	 * Place it in the queue of disk activities for this disk
    398 	 */
    399 	disksort(dp, bp);
    400 
    401 	/*
    402 	 * Tell the device to get going on the transfer if it's
    403 	 * not doing anything, otherwise just wait for completion
    404 	 */
    405 	sdstart(unit);
    406 
    407 	splx(opri);
    408 	return;
    409 
    410 bad:
    411 	bp->b_flags |= B_ERROR;
    412 done:
    413 
    414 	/*
    415 	 * Correctly set the buf to indicate a completed xfer
    416 	 */
    417 	bp->b_resid = bp->b_bcount;
    418 	biodone(bp);
    419 }
    420 
    421 /*
    422  * sdstart looks to see if there is a buf waiting for the device
    423  * and that the device is not already busy. If both are true,
    424  * It dequeues the buf and creates a scsi command to perform the
    425  * transfer in the buf. The transfer request will call scsi_done
    426  * on completion, which will in turn call this routine again
    427  * so that the next queued transfer is performed.
    428  * The bufs are queued by the strategy routine (sdstrategy)
    429  *
    430  * This routine is also called after other non-queued requests
    431  * have been made of the scsi driver, to ensure that the queue
    432  * continues to be drained.
    433  *
    434  * must be called at the correct (highish) spl level
    435  * sdstart() is called at splbio from sdstrategy and scsi_done
    436  */
    437 void
    438 sdstart(unit)
    439 	int unit;
    440 {
    441 	register struct sd_data *sd = sdcd.cd_devs[unit];
    442 	register struct	scsi_link *sc_link = sd->sc_link;
    443 	struct buf *bp = 0;
    444 	struct buf *dp;
    445 	struct scsi_rw_big cmd;
    446 	int blkno, nblks;
    447 	struct partition *p;
    448 
    449 	SC_DEBUG(sc_link, SDEV_DB2, ("sdstart "));
    450 	/*
    451 	 * Check if the device has room for another command
    452 	 */
    453 	while (sc_link->opennings) {
    454 		/*
    455 		 * there is excess capacity, but a special waits
    456 		 * It'll need the adapter as soon as we clear out of the
    457 		 * way and let it run (user level wait).
    458 		 */
    459 		if (sc_link->flags & SDEV_WAITING) {
    460 			sc_link->flags &= ~SDEV_WAITING;
    461 			wakeup((caddr_t)sc_link);
    462 			return;
    463 		}
    464 
    465 		/*
    466 		 * See if there is a buf with work for us to do..
    467 		 */
    468 		dp = &sd->buf_queue;
    469 		if ((bp = dp->b_actf) == NULL)	/* yes, an assign */
    470 			return;
    471 		dp->b_actf = bp->b_actf;
    472 
    473 		/*
    474 		 * If the device has become invalid, abort all the
    475 		 * reads and writes until all files have been closed and
    476 		 * re-openned
    477 		 */
    478 		if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
    479 			sd->flags &= ~SDHAVELABEL;
    480 			goto bad;
    481 		}
    482 
    483 		/*
    484 		 * We have a buf, now we know we are going to go through
    485 		 * With this thing..
    486 		 *
    487 		 *  First, translate the block to absolute
    488 		 */
    489 		blkno = bp->b_blkno / (sd->params.blksize / DEV_BSIZE);
    490 		if (SDPART(bp->b_dev) != RAW_PART) {
    491 			p = &sd->sc_dk.dk_label.d_partitions[SDPART(bp->b_dev)];
    492 			blkno += p->p_offset;
    493 		}
    494 		nblks = (bp->b_bcount + (sd->params.blksize - 1)) /
    495 		    sd->params.blksize;
    496 
    497 		/*
    498 		 *  Fill out the scsi command
    499 		 */
    500 		bzero(&cmd, sizeof(cmd));
    501 		cmd.op_code = (bp->b_flags & B_READ) ? READ_BIG : WRITE_BIG;
    502 		cmd.addr_3 = (blkno & 0xff000000) >> 24;
    503 		cmd.addr_2 = (blkno & 0xff0000) >> 16;
    504 		cmd.addr_1 = (blkno & 0xff00) >> 8;
    505 		cmd.addr_0 = blkno & 0xff;
    506 		cmd.length2 = (nblks & 0xff00) >> 8;
    507 		cmd.length1 = (nblks & 0xff);
    508 
    509 		/*
    510 		 * Call the routine that chats with the adapter.
    511 		 * Note: we cannot sleep as we may be an interrupt
    512 		 */
    513 		if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
    514 		    sizeof(cmd), (u_char *) bp->b_un.b_addr, bp->b_bcount,
    515 		    SDRETRIES, 10000, bp, SCSI_NOSLEEP |
    516 		    ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT))
    517 		    != SUCCESSFULLY_QUEUED) {
    518 bad:
    519 			printf("%s: not queued", sd->sc_dev.dv_xname);
    520 			bp->b_error = EIO;
    521 			bp->b_flags |= B_ERROR;
    522 			biodone(bp);
    523 		}
    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