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