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