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