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