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