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