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