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