Home | History | Annotate | Line # | Download | only in scsipi
sd.c revision 1.77
      1 /*	$NetBSD: sd.c,v 1.77 1995/08/12 22:58:01 mycroft 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 s;
    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 	s = 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(s);
    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 int
    547 sdread(dev, uio)
    548 	dev_t dev;
    549 	struct uio *uio;
    550 {
    551 	struct sd_softc *sd = sdcd.cd_devs[SDUNIT(dev)];
    552 
    553 	return (physio(sdstrategy, NULL, dev, B_READ,
    554 		       sd->sc_link->adapter->scsi_minphys, uio));
    555 }
    556 
    557 int
    558 sdwrite(dev, uio)
    559 	dev_t dev;
    560 	struct uio *uio;
    561 {
    562 	struct sd_softc *sd = sdcd.cd_devs[SDUNIT(dev)];
    563 
    564 	return (physio(sdstrategy, NULL, dev, B_WRITE,
    565 		       sd->sc_link->adapter->scsi_minphys, uio));
    566 }
    567 
    568 /*
    569  * Perform special action on behalf of the user
    570  * Knows about the internals of this device
    571  */
    572 int
    573 sdioctl(dev, cmd, addr, flag, p)
    574 	dev_t dev;
    575 	u_long cmd;
    576 	caddr_t addr;
    577 	int flag;
    578 	struct proc *p;
    579 {
    580 	struct sd_softc *sd = sdcd.cd_devs[SDUNIT(dev)];
    581 	int error;
    582 
    583 	SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd));
    584 
    585 	/*
    586 	 * If the device is not valid.. abandon ship
    587 	 */
    588 	if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0)
    589 		return EIO;
    590 
    591 	switch (cmd) {
    592 	case DIOCGDINFO:
    593 		*(struct disklabel *)addr = sd->sc_dk.dk_label;
    594 		return 0;
    595 
    596 	case DIOCGPART:
    597 		((struct partinfo *)addr)->disklab = &sd->sc_dk.dk_label;
    598 		((struct partinfo *)addr)->part =
    599 		    &sd->sc_dk.dk_label.d_partitions[SDPART(dev)];
    600 		return 0;
    601 
    602 	case DIOCWDINFO:
    603 	case DIOCSDINFO:
    604 		if ((flag & FWRITE) == 0)
    605 			return EBADF;
    606 
    607 		if (error = sdlock(sd))
    608 			return error;
    609 		sd->flags |= SDF_LABELLING;
    610 
    611 		error = setdisklabel(&sd->sc_dk.dk_label,
    612 		    (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0,
    613 		    &sd->sc_dk.dk_cpulabel);
    614 		if (error == 0) {
    615 			if (cmd == DIOCWDINFO)
    616 				error = writedisklabel(SDLABELDEV(dev),
    617 				    sdstrategy, &sd->sc_dk.dk_label,
    618 				    &sd->sc_dk.dk_cpulabel);
    619 		}
    620 
    621 		sd->flags &= ~SDF_LABELLING;
    622 		sdunlock(sd);
    623 		return error;
    624 
    625 	case DIOCWLABEL:
    626 		if ((flag & FWRITE) == 0)
    627 			return EBADF;
    628 		if (*(int *)addr)
    629 			sd->flags |= SDF_WLABEL;
    630 		else
    631 			sd->flags &= ~SDF_WLABEL;
    632 		return 0;
    633 
    634 	default:
    635 		if (SDPART(dev) != RAW_PART)
    636 			return ENOTTY;
    637 		return scsi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p);
    638 	}
    639 
    640 #ifdef DIAGNOSTIC
    641 	panic("sdioctl: impossible");
    642 #endif
    643 }
    644 
    645 /*
    646  * Load the label information on the named device
    647  */
    648 void
    649 sdgetdisklabel(sd)
    650 	struct sd_softc *sd;
    651 {
    652 	struct disklabel *lp = &sd->sc_dk.dk_label;
    653 	char *errstring;
    654 
    655 	bzero(lp, sizeof(struct disklabel));
    656 	bzero(&sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
    657 
    658 	lp->d_secsize = sd->params.blksize;
    659 	lp->d_ntracks = sd->params.heads;
    660 	lp->d_nsectors = sd->params.sectors;
    661 	lp->d_ncylinders = sd->params.cyls;
    662 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    663 	if (lp->d_secpercyl == 0) {
    664 		lp->d_secpercyl = 100;
    665 		/* as long as it's not 0 - readdisklabel divides by it (?) */
    666 	}
    667 
    668 	strncpy(lp->d_typename, "SCSI disk", 16);
    669 	lp->d_type = DTYPE_SCSI;
    670 	strncpy(lp->d_packname, "fictitious", 16);
    671 	lp->d_secperunit = sd->params.disksize;
    672 	lp->d_rpm = 3600;
    673 	lp->d_interleave = 1;
    674 	lp->d_flags = 0;
    675 
    676 	lp->d_partitions[RAW_PART].p_offset = 0;
    677 	lp->d_partitions[RAW_PART].p_size =
    678 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    679 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    680 	lp->d_npartitions = RAW_PART + 1;
    681 
    682 	lp->d_magic = DISKMAGIC;
    683 	lp->d_magic2 = DISKMAGIC;
    684 	lp->d_checksum = dkcksum(lp);
    685 
    686 	/*
    687 	 * Call the generic disklabel extraction routine
    688 	 */
    689 	if (errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit,
    690 	    RAW_PART), sdstrategy, lp, &sd->sc_dk.dk_cpulabel)) {
    691 		printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
    692 		return;
    693 	}
    694 }
    695 
    696 /*
    697  * Find out from the device what it's capacity is
    698  */
    699 u_long
    700 sd_size(sd, flags)
    701 	struct sd_softc *sd;
    702 	int flags;
    703 {
    704 	struct scsi_read_cap_data rdcap;
    705 	struct scsi_read_capacity scsi_cmd;
    706 	u_long size;
    707 
    708 	/*
    709 	 * make up a scsi command and ask the scsi driver to do
    710 	 * it for you.
    711 	 */
    712 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    713 	scsi_cmd.opcode = READ_CAPACITY;
    714 
    715 	/*
    716 	 * If the command works, interpret the result as a 4 byte
    717 	 * number of blocks
    718 	 */
    719 	if (scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd,
    720 	    sizeof(scsi_cmd), (u_char *)&rdcap, sizeof(rdcap), SDRETRIES,
    721 	    2000, NULL, flags | SCSI_DATA_IN) != 0)
    722 		return 0;
    723 
    724 	size = (rdcap.addr_3 << 24) + (rdcap.addr_2 << 16) +
    725 	    (rdcap.addr_1 << 8) + rdcap.addr_0 + 1;
    726 
    727 	return size;
    728 }
    729 
    730 /*
    731  * Tell the device to map out a defective block
    732  */
    733 int
    734 sd_reassign_blocks(sd, block)
    735 	struct sd_softc *sd;
    736 	u_long block;
    737 {
    738 	struct scsi_reassign_blocks scsi_cmd;
    739 	struct scsi_reassign_blocks_data rbdata;
    740 
    741 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    742 	bzero(&rbdata, sizeof(rbdata));
    743 	scsi_cmd.opcode = REASSIGN_BLOCKS;
    744 
    745 	rbdata.length_msb = 0;
    746 	rbdata.length_lsb = sizeof(rbdata.defect_descriptor[0]);
    747 	rbdata.defect_descriptor[0].dlbaddr_3 = (block >> 24) & 0xff;
    748 	rbdata.defect_descriptor[0].dlbaddr_2 = (block >> 16) & 0xff;
    749 	rbdata.defect_descriptor[0].dlbaddr_1 = (block >> 8) & 0xff;
    750 	rbdata.defect_descriptor[0].dlbaddr_0 = block & 0xff;
    751 
    752 	return scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd,
    753 	    sizeof(scsi_cmd), (u_char *)&rbdata, sizeof(rbdata), SDRETRIES,
    754 	    5000, NULL, SCSI_DATA_OUT);
    755 }
    756 
    757 #define b2tol(a)	(((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
    758 
    759 /*
    760  * Get the scsi driver to send a full inquiry to the * device and use the
    761  * results to fill out the disk parameter structure.
    762  */
    763 int
    764 sd_get_parms(sd, flags)
    765 	struct sd_softc *sd;
    766 	int flags;
    767 {
    768 	struct disk_parms *dp = &sd->params;
    769 	struct scsi_mode_sense scsi_cmd;
    770 	struct scsi_mode_sense_data {
    771 		struct scsi_mode_header header;
    772 		struct scsi_blk_desc blk_desc;
    773 		union disk_pages pages;
    774 	} scsi_sense;
    775 	u_long sectors;
    776 
    777 	/*
    778 	 * do a "mode sense page 4"
    779 	 */
    780 	bzero(&scsi_cmd, sizeof(scsi_cmd));
    781 	scsi_cmd.opcode = MODE_SENSE;
    782 	scsi_cmd.page = 4;
    783 	scsi_cmd.length = 0x20;
    784 	/*
    785 	 * If the command worked, use the results to fill out
    786 	 * the parameter structure
    787 	 */
    788 	if (scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *)&scsi_cmd,
    789 	    sizeof(scsi_cmd), (u_char *)&scsi_sense, sizeof(scsi_sense),
    790 	    SDRETRIES, 6000, NULL, flags | SCSI_DATA_IN) != 0) {
    791 		printf("%s: could not mode sense (4)", sd->sc_dev.dv_xname);
    792 	fake_it:
    793 		printf("; using fictitious geometry\n");
    794 		/*
    795 		 * use adaptec standard fictitious geometry
    796 		 * this depends on which controller (e.g. 1542C is
    797 		 * different. but we have to put SOMETHING here..)
    798 		 */
    799 		sectors = sd_size(sd, flags);
    800 		dp->heads = 64;
    801 		dp->sectors = 32;
    802 		dp->cyls = sectors / (64 * 32);
    803 		dp->blksize = 512;
    804 		dp->disksize = sectors;
    805 	} else {
    806 		SC_DEBUG(sd->sc_link, SDEV_DB3,
    807 		    ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
    808 		    _3btol(&scsi_sense.pages.rigid_geometry.ncyl_2),
    809 		    scsi_sense.pages.rigid_geometry.nheads,
    810 		    b2tol(scsi_sense.pages.rigid_geometry.st_cyl_wp),
    811 		    b2tol(scsi_sense.pages.rigid_geometry.st_cyl_rwc),
    812 		    b2tol(scsi_sense.pages.rigid_geometry.land_zone)));
    813 
    814 		/*
    815 		 * KLUDGE!! (for zone recorded disks)
    816 		 * give a number of sectors so that sec * trks * cyls
    817 		 * is <= disk_size
    818 		 * can lead to wasted space! THINK ABOUT THIS !
    819 		 */
    820 		dp->heads = scsi_sense.pages.rigid_geometry.nheads;
    821 		dp->cyls =
    822 		    _3btol(&scsi_sense.pages.rigid_geometry.ncyl_2);
    823 		dp->blksize = _3btol(scsi_sense.blk_desc.blklen);
    824 
    825 		if (dp->heads == 0 || dp->cyls == 0) {
    826 			printf("%s: mode sense (4) returned nonsense",
    827 			    sd->sc_dev.dv_xname);
    828 			goto fake_it;
    829 		}
    830 
    831 		if (dp->blksize == 0)
    832 			dp->blksize = 512;
    833 
    834 		sectors = sd_size(sd, flags);
    835 		dp->disksize = sectors;
    836 		sectors /= (dp->heads * dp->cyls);
    837 		dp->sectors = sectors;	/* XXX dubious on SCSI */
    838 	}
    839 
    840 	return 0;
    841 }
    842 
    843 int
    844 sdsize(dev)
    845 	dev_t dev;
    846 {
    847 	struct sd_softc *sd;
    848 	int part;
    849 	int size;
    850 
    851 	if (sdopen(dev, 0, S_IFBLK) != 0)
    852 		return -1;
    853 	sd = sdcd.cd_devs[SDUNIT(dev)];
    854 	part = SDPART(dev);
    855 	if (sd->sc_dk.dk_label.d_partitions[part].p_fstype != FS_SWAP)
    856 		size = -1;
    857 	else
    858 		size = sd->sc_dk.dk_label.d_partitions[part].p_size;
    859 	if (sdclose(dev, 0, S_IFBLK) != 0)
    860 		return -1;
    861 	return size;
    862 }
    863 
    864 #ifndef __BDEVSW_DUMP_OLD_TYPE
    865 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
    866 static struct scsi_xfer sx;
    867 static int sddoingadump;
    868 
    869 /*
    870  * dump all of physical memory into the partition specified, starting
    871  * at offset 'dumplo' into the partition.
    872  */
    873 int
    874 sddump(dev, blkno, va, size)
    875 	dev_t dev;
    876 	daddr_t blkno;
    877 	caddr_t va;
    878 	size_t size;
    879 {
    880 	struct sd_softc *sd;	/* disk unit to do the I/O */
    881 	struct disklabel *lp;	/* disk's disklabel */
    882 	int	unit, part;
    883 	int	sectorsize;	/* size of a disk sector */
    884 	int	nsects;		/* number of sectors in partition */
    885 	int	sectoff;	/* sector offset of partition */
    886 	int	totwrt;		/* total number of sectors left to write */
    887 	int	nwrt;		/* current number of sectors to write */
    888 	struct scsi_rw_big cmd;	/* write command */
    889 	struct scsi_xfer *xs;	/* ... convenience */
    890 	int	retval;
    891 
    892 	/* Check if recursive dump; if so, punt. */
    893 	if (sddoingadump)
    894 		return EFAULT;
    895 
    896 	/* Mark as active early. */
    897 	sddoingadump = 1;
    898 
    899 	unit = SDUNIT(dev);	/* Decompose unit & partition. */
    900 	part = SDPART(dev);
    901 
    902 	/* Check for acceptable drive number. */
    903 	if (unit >= sdcd.cd_ndevs || (sd = sdcd.cd_devs[unit]) == NULL)
    904 		return ENXIO;
    905 
    906 	/* Make sure it was initialized. */
    907 	if (sd->sc_link->flags & SDEV_MEDIA_LOADED != SDEV_MEDIA_LOADED)
    908 		return ENXIO;
    909 
    910 	/* Convert to disk sectors.  Request must be a multiple of size. */
    911 	lp = &sd->sc_dk.dk_label;
    912 	sectorsize = lp->d_secsize;
    913 	if ((size % sectorsize) != 0)
    914 		return EFAULT;
    915 	totwrt = size / sectorsize;
    916 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
    917 
    918 	nsects = lp->d_partitions[part].p_size;
    919 	sectoff = lp->d_partitions[part].p_offset;
    920 
    921 	/* Check transfer bounds against partition size. */
    922 	if ((blkno < 0) || ((blkno + totwrt) > nsects))
    923 		return EINVAL;
    924 
    925 	/* Offset block number to start of partition. */
    926 	blkno += sectoff;
    927 
    928 	xs = &sx;
    929 
    930 	while (totwrt > 0) {
    931 		nwrt = totwrt;		/* XXX */
    932 #ifndef	SD_DUMP_NOT_TRUSTED
    933 		/*
    934 		 *  Fill out the scsi command
    935 		 */
    936 		bzero(&cmd, sizeof(cmd));
    937 		cmd.opcode = WRITE_BIG;
    938 		cmd.addr_3 = (blkno >> 24) & 0xff;
    939 		cmd.addr_2 = (blkno >> 16) & 0xff;
    940 		cmd.addr_1 = (blkno >> 8) & 0xff;
    941 		cmd.addr_0 = blkno & 0xff;
    942 		cmd.length2 = (nwrt >> 8) & 0xff;
    943 		cmd.length1 = nwrt & 0xff;
    944 		/*
    945 		 * Fill out the scsi_xfer structure
    946 		 *    Note: we cannot sleep as we may be an interrupt
    947 		 * don't use scsi_scsi_cmd() as it may want
    948 		 * to wait for an xs.
    949 		 */
    950 		bzero(xs, sizeof(sx));
    951 		xs->flags |= SCSI_AUTOCONF | INUSE;
    952 		xs->sc_link = sd->sc_link;
    953 		xs->retries = SDRETRIES;
    954 		xs->timeout = 10000;	/* 10000 millisecs for a disk ! */
    955 		xs->cmd = (struct scsi_generic *)&cmd;
    956 		xs->cmdlen = sizeof(cmd);
    957 		xs->resid = nwrt * sectorsize;
    958 		xs->error = XS_NOERROR;
    959 		xs->bp = 0;
    960 		xs->data = va;
    961 		xs->datalen = nwrt * sectorsize;
    962 
    963 		/*
    964 		 * Pass all this info to the scsi driver.
    965 		 */
    966 		retval = (*(sd->sc_link->adapter->scsi_cmd)) (xs);
    967 		if (retval != COMPLETE)
    968 			return ENXIO;
    969 #else	/* SD_DUMP_NOT_TRUSTED */
    970 		/* Let's just talk about this first... */
    971 		printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
    972 		delay(500 * 1000);	/* half a second */
    973 #endif	/* SD_DUMP_NOT_TRUSTED */
    974 
    975 		/* update block count */
    976 		totwrt -= nwrt;
    977 		blkno += nwrt;
    978 		va += sectorsize * nwrt;
    979 	}
    980 	sddoingadump = 0;
    981 	return 0;
    982 }
    983 #else	/* __BDEVSW_DUMP_NEW_TYPE */
    984 int
    985 sddump(dev, blkno, va, size)
    986 	dev_t dev;
    987 	daddr_t blkno;
    988 	caddr_t va;
    989 	size_t size;
    990 {
    991 
    992 	/* Not implemented. */
    993 	return ENXIO;
    994 }
    995 #endif	/* __BDEVSW_DUMP_NEW_TYPE */
    996