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