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