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