Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.54
      1 /*
      2  * Copyright (c) 1994 Charles Hannum.
      3  * Copyright (c) 1990 The Regents of the University of California.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * William Jolitz.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  *	from: @(#)wd.c	7.2 (Berkeley) 5/9/91
     38  *	$Id: wd.c,v 1.54 1994/03/03 01:58:21 mycroft Exp $
     39  */
     40 
     41 #define	QUIETWORKS	/* define this to make wdopen() set DKFL_QUIET */
     42 #define	INSTRUMENT	/* instrumentation stuff by Brad Parker */
     43 
     44 /* TODO: peel out buffer at low ipl, speed improvement */
     45 /* TODO: find and fix the timing bugs apparent on some controllers */
     46 
     47 #include "wd.h"
     48 #if NWDC > 0
     49 
     50 #include <sys/param.h>
     51 #include <sys/dkbad.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/conf.h>
     55 #include <sys/file.h>
     56 #include <sys/stat.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/disklabel.h>
     59 #include <sys/buf.h>
     60 #include <sys/uio.h>
     61 #include <sys/malloc.h>
     62 #include <sys/syslog.h>
     63 #ifdef INSTRUMENT
     64 #include <sys/dkstat.h>
     65 #endif
     66 
     67 #include <vm/vm.h>
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/cpufunc.h>
     71 #include <machine/pio.h>
     72 
     73 #include <i386/isa/isa.h>
     74 #include <i386/isa/isa_device.h>
     75 #include <i386/isa/icu.h>
     76 #include <i386/isa/wdreg.h>
     77 
     78 #define WDCNDELAY	100000	/* delay = 100us; so 10s for a controller state change */
     79 #define WDCDELAY	100
     80 
     81 #if 0
     82 /* if you enable this, it will report any delays more than 100us * N long */
     83 #define WDCNDELAY_DEBUG	100
     84 #endif
     85 
     86 #define	WDIORETRIES	5	/* number of retries before giving up */
     87 
     88 #define WDUNIT(dev)	((minor(dev) & 0xf8) >> 3)
     89 #define WDPART(dev)	((minor(dev) & 0x07)     )
     90 #define makewddev(maj, unit, part)	(makedev(maj, ((unit << 3) + part)))
     91 #define WDRAW	3		/* 'd' partition isn't a partition! */
     92 
     93 #define b_cylin	b_resid		/* cylinder number for doing IO to */
     94 				/* shares an entry in the buf struct */
     95 
     96 /*
     97  * Drive states.  Used to initialize drive.
     98  */
     99 #define	CLOSED		0		/* disk is closed. */
    100 #define	WANTOPEN	1		/* open requested, not started */
    101 #define	RECAL		2		/* doing restore */
    102 #define	OPEN		3		/* done with open */
    103 
    104 /*
    105  * Drive status.
    106  */
    107 struct	disk {
    108 	long	dk_bc;		/* byte count left */
    109 	long	dk_bct;		/* total byte count left */
    110 	short	dk_skip;	/* blocks already transferred */
    111 	short	dk_skipm;	/* blocks already transferred for multi */
    112 	char	dk_ctrlr;	/* physical controller number */
    113 	char	dk_unit;	/* physical unit number */
    114 	char	dk_lunit;	/* logical unit number */
    115 	char	dk_state;	/* control state */
    116 	u_char	dk_status;	/* copy of status reg. */
    117 	u_char	dk_error;	/* copy of error reg. */
    118 	short	dk_port;	/* i/o port base */
    119 
    120 	u_long  dk_copenpart;   /* character units open on this drive */
    121 	u_long  dk_bopenpart;   /* block units open on this drive */
    122 	u_long  dk_openpart;    /* all units open on this drive */
    123 	short	dk_wlabel;	/* label writable? */
    124 	short	dk_flags;	/* drive characteistics found */
    125 #define	DKFL_QUIET	0x00002	 /* report errors back, but don't complain */
    126 #define	DKFL_SINGLE	0x00004	 /* sector at a time mode */
    127 #define	DKFL_ERROR	0x00008	 /* processing a disk error */
    128 #define	DKFL_BSDLABEL	0x00010	 /* has a BSD disk label */
    129 #define	DKFL_BADSECT	0x00020	 /* has a bad144 badsector table */
    130 #define	DKFL_WRITEPROT	0x00040	 /* manual unit write protect */
    131 	struct wdparams dk_params; /* ESDI/IDE drive/controller parameters */
    132 	struct disklabel dk_dd;	/* device configuration data */
    133 	struct cpu_disklabel dk_cpd;
    134 	long	dk_badsect[127];	/* 126 plus trailing -1 marker */
    135 };
    136 
    137 struct board {
    138 	short dkc_port;
    139 };
    140 
    141 struct	board	wdcontroller[NWDC];
    142 struct	disk	*wddrives[NWD];		/* table of units */
    143 struct	buf	wdtab[NWDC];		/* various per-controller info */
    144 struct	buf	wdutab[NWD];		/* head of queue per drive */
    145 struct	buf	rwdbuf[NWD];		/* buffers for raw IO */
    146 long	wdxfer[NWD];			/* count of transfers */
    147 int	wdtimeoutstatus[NWD];		/* timeout counters */
    148 
    149 int wdprobe(), wdattach();
    150 
    151 struct	isa_driver wdcdriver = {
    152 	wdprobe, wdattach, "wdc",
    153 };
    154 
    155 static void wdustart(struct disk *);
    156 static void wdstart(int);
    157 static int wdcommand(struct disk *, int);
    158 static int wdcontrol(struct buf *);
    159 static int wdsetctlr(dev_t, struct disk *);
    160 static int wdgetctlr(int, struct disk *);
    161 static void bad144intern(struct disk *);
    162 static int wdreset(struct disk *, int);
    163 static int wdtimeout(caddr_t);
    164 void wddisksort(struct buf *dp, struct buf *bp);
    165 int wdc_wait(struct disk *, int);
    166 #define	wait_for_drq(d)		wdc_wait(d, WDCS_DRQ)
    167 #define	wait_for_ready(d)	wdc_wait(d, WDCS_READY | WDCS_SEEKCMPLT)
    168 #define	wait_for_unbusy(d)	wdc_wait(d, 0)
    169 
    170 /*
    171  * Probe for controller.
    172  */
    173 int
    174 wdprobe(struct isa_device *dvp)
    175 {
    176 	struct disk *du;
    177 	int wdc;
    178 
    179 	if (dvp->id_unit >= NWDC)
    180 		return 0;
    181 
    182 	du = (struct disk *)malloc(sizeof(struct disk), M_TEMP, M_NOWAIT);
    183 	bzero(du, sizeof(struct disk));
    184 
    185 	du->dk_ctrlr = dvp->id_unit;
    186 	du->dk_unit = 0;
    187 	du->dk_lunit = 0;
    188 	wdcontroller[dvp->id_unit].dkc_port = dvp->id_iobase;
    189 
    190 	wdc = du->dk_port = dvp->id_iobase;
    191 
    192 	/* check if we have registers that work */
    193 	outb(wdc+wd_error, 0x5a);	/* error register not writable */
    194 	outb(wdc+wd_cyl_lo, 0xa5);	/* but all of cyllo are implemented */
    195 	if (inb(wdc+wd_error) == 0x5a || inb(wdc+wd_cyl_lo) != 0xa5)
    196 		goto nodevice;
    197 
    198 	wdreset(du, 0);
    199 
    200 	/* execute a controller only command */
    201 	if (wdcommand(du, WDCC_DIAGNOSE) < 0)
    202 		goto nodevice;
    203 
    204 	free(du, M_TEMP);
    205 	return 8;
    206 
    207 nodevice:
    208 	free(du, M_TEMP);
    209 	return 0;
    210 }
    211 
    212 /*
    213  * Called for the controller too
    214  * Attach each drive if possible.
    215  */
    216 int
    217 wdattach(struct isa_device *dvp)
    218 {
    219 	int unit, lunit;
    220 	struct disk *du;
    221 
    222 	if (dvp->id_masunit == -1)
    223 		return 0;
    224 	if (dvp->id_masunit >= NWDC)
    225 		return 0;
    226 
    227 	lunit = dvp->id_unit;
    228 	if (lunit == -1) {
    229 		printf("wdc%d: cannot support unit ?\n", dvp->id_masunit);
    230 		return 0;
    231 	}
    232 	if (lunit >= NWD)
    233 		return 0;
    234 	unit = dvp->id_physid;
    235 
    236 	du = wddrives[lunit] =
    237 	    (struct disk *)malloc(sizeof(struct disk), M_TEMP, M_NOWAIT);
    238 	bzero(du, sizeof(struct disk));
    239 	bzero(&wdutab[lunit], sizeof(struct buf));
    240 	bzero(&rwdbuf[lunit], sizeof(struct buf));
    241 	wdxfer[lunit] = 0;
    242 	wdtimeoutstatus[lunit] = 0;
    243 	wdtimeout(lunit);
    244 	du->dk_ctrlr = dvp->id_masunit;
    245 	du->dk_unit = unit;
    246 	du->dk_lunit = lunit;
    247 	du->dk_port = wdcontroller[dvp->id_masunit].dkc_port;
    248 
    249 	if (wdgetctlr(unit, du) == 0)  {
    250 		int i, blank;
    251 
    252 		printf("wd%d at wdc%d targ %d: ", dvp->id_unit,
    253 		    dvp->id_masunit, dvp->id_physid);
    254 		if (du->dk_params.wdp_heads == 0)
    255 			printf("(unknown size) <");
    256 		else
    257 			printf("%dMB %d cyl, %d head, %d sec <",
    258 			    du->dk_dd.d_ncylinders * du->dk_dd.d_secpercyl /
    259 			    (1048576 / DEV_BSIZE),
    260 			    du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks,
    261 			    du->dk_dd.d_nsectors);
    262 		for (i = blank = 0; i < sizeof(du->dk_params.wdp_model); i++) {
    263 			char c = du->dk_params.wdp_model[i];
    264 			if (!c)
    265 				break;
    266 			if (blank && c == ' ')
    267 				continue;
    268 			if (blank && c != ' ') {
    269 				printf(" %c", c);
    270 				blank = 0;
    271 				continue;
    272 			}
    273 			if (c == ' ')
    274 				blank = 1;
    275 			else
    276 				printf("%c", c);
    277 		}
    278 		printf(">\n");
    279 	} else {
    280 		/*printf("wd%d at wdc%d slave %d -- error\n", lunit,
    281 		    dvp->id_masunit, unit);*/
    282 		wddrives[lunit] = 0;
    283 		free(du, M_TEMP);
    284 		return 0;
    285 	}
    286 	return 1;
    287 }
    288 
    289 /* Read/write routine for a buffer.  Finds the proper unit, range checks
    290  * arguments, and schedules the transfer.  Does not wait for the transfer
    291  * to complete.  Multi-page transfers are supported.  All I/O requests must
    292  * be a multiple of a sector in length.
    293  */
    294 int
    295 wdstrategy(register struct buf *bp)
    296 {
    297 	register struct buf *dp;
    298 	struct disk *du;	/* Disk unit to do the IO.	*/
    299 	int lunit = WDUNIT(bp->b_dev);
    300 	int s;
    301 
    302 	/* valid unit, controller, and request?  */
    303 	if (lunit >= NWD || bp->b_blkno < 0 ||
    304 	    howmany(bp->b_bcount, DEV_BSIZE) >= (1 << NBBY) ||
    305 	    (du = wddrives[lunit]) == 0) {
    306 		bp->b_error = EINVAL;
    307 		bp->b_flags |= B_ERROR;
    308 		goto done;
    309 	}
    310 
    311 	/* "soft" write protect check */
    312 	if ((du->dk_flags & DKFL_WRITEPROT) && (bp->b_flags & B_READ) == 0) {
    313 		bp->b_error = EROFS;
    314 		bp->b_flags |= B_ERROR;
    315 		goto done;
    316 	}
    317 
    318 	/* have partitions and want to use them? */
    319 	if ((du->dk_flags & DKFL_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW) {
    320 		/*
    321 		 * do bounds checking, adjust transfer. if error, process.
    322 		 * if end of partition, just return
    323 		 */
    324 		if (bounds_check_with_label(bp, &du->dk_dd, du->dk_wlabel) <= 0)
    325 			goto done;
    326 		/* otherwise, process transfer request */
    327 	}
    328 
    329 	/* don't bother */
    330 	bp->b_cylin = 0;
    331 
    332 	/* queue transfer on drive, activate drive and controller if idle */
    333 	dp = &wdutab[lunit];
    334 	s = splbio();
    335 	wddisksort(dp, bp);
    336 	if (dp->b_active == 0)
    337 		wdustart(du);		/* start drive */
    338 	if (wdtab[du->dk_ctrlr].b_active == 0)
    339 		wdstart(du->dk_ctrlr);		/* start controller */
    340 	splx(s);
    341 	return 0;
    342 
    343 done:
    344 	/* toss transfer, we're done early */
    345 	biodone(bp);
    346 }
    347 
    348 /*
    349  * Need to skip over multitransfer bufs.
    350  */
    351 void
    352 wddisksort(struct buf *dp, struct buf *bp)
    353 {
    354 	register struct buf *ap;
    355 
    356 	while ((ap = dp->b_actf) && ap->b_flags & B_XXX)
    357 		dp = ap;
    358 	disksort(dp, bp);
    359 }
    360 
    361 /*
    362  * Routine to queue a command to the controller.  The unit's
    363  * request is linked into the active list for the controller.
    364  * If the controller is idle, the transfer is started.
    365  */
    366 static void
    367 wdustart(register struct disk *du)
    368 {
    369 	register struct buf *dp = &wdutab[du->dk_lunit];
    370 	int ctrlr = du->dk_ctrlr;
    371 
    372 	/* unit already active? */
    373 	if (dp->b_active)
    374 		return;
    375 
    376 	/* anything to start? */
    377 	if (dp->b_actf == NULL)
    378 		return;
    379 
    380 	/* link onto controller queue */
    381 	dp->b_forw = NULL;
    382 	if (wdtab[ctrlr].b_forw == NULL)
    383 		wdtab[ctrlr].b_forw = dp;
    384 	else
    385 		wdtab[ctrlr].b_actl->b_forw = dp;
    386 	wdtab[ctrlr].b_actl = dp;
    387 
    388 	/* mark the drive unit as busy */
    389 	dp->b_active = 1;
    390 }
    391 
    392 /*
    393  * Controller startup routine.  This does the calculation, and starts
    394  * a single-sector read or write operation.  Called to start a transfer,
    395  * or from the interrupt routine to continue a multi-sector transfer.
    396  * RESTRICTIONS:
    397  * 1.	The transfer length must be an exact multiple of the sector size.
    398  */
    399 static void
    400 wdstart(int ctrlr)
    401 {
    402 	register struct disk *du;	/* disk unit for IO */
    403 	register struct buf *bp;
    404 	struct disklabel *lp;
    405 	struct buf *dp;
    406 	long blknum, cylin, head, sector;
    407 	long secpertrk, secpercyl, addr;
    408 	int lunit, wdc;
    409 	int xfrblknum;
    410 
    411 loop:
    412 	/* is there a drive for the controller to do a transfer with? */
    413 	dp = wdtab[ctrlr].b_forw;
    414 	if (dp == NULL) {
    415 		wdtab[ctrlr].b_active = 0;
    416 		return;
    417 	}
    418 
    419 	/* is there a transfer to this drive ? if so, link it on
    420 	   the controller's queue */
    421 	bp = dp->b_actf;
    422 	if (bp == NULL) {
    423 		dp->b_active = 0;
    424 		wdtab[ctrlr].b_forw = dp->b_forw;
    425 		goto loop;
    426 	}
    427 
    428 	/* obtain controller and drive information */
    429 	lunit = WDUNIT(bp->b_dev);
    430 	du = wddrives[lunit];
    431 
    432 	/* clear any pending timeout, just in case */
    433 	wdtimeoutstatus[lunit] = 0;
    434 
    435 	/* if not really a transfer, do control operations specially */
    436 	if (du->dk_state < OPEN) {
    437 		(void) wdcontrol(bp);
    438 		return;
    439 	}
    440 
    441 	/* calculate transfer details */
    442 	blknum = bp->b_blkno + du->dk_skip;
    443 #ifdef WDDEBUG
    444 	if (du->dk_skip == 0)
    445 		printf("\nwdstart %d: %s %d@%d; map ", lunit,
    446 		    (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
    447 		    blknum);
    448 	else
    449 		printf(" %d)%x", du->dk_skip, inb(du->dk_port+wd_altsts));
    450 #endif
    451 	addr = (int)bp->b_un.b_addr;
    452 	if (du->dk_skip == 0)
    453 		du->dk_bc = bp->b_bcount;
    454 	if (du->dk_skipm == 0) {
    455 		struct buf *oldbp, *nextbp;
    456 		oldbp = bp;
    457 		nextbp = bp->b_actf;
    458 		du->dk_bct = du->dk_bc;
    459 		oldbp->b_flags |= B_XXX;
    460 		while (nextbp && (oldbp->b_flags & DKFL_SINGLE) == 0 &&
    461 		    oldbp->b_dev == nextbp->b_dev && nextbp->b_blkno ==
    462 		    (oldbp->b_blkno + (oldbp->b_bcount / DEV_BSIZE)) &&
    463 		    (oldbp->b_flags & B_READ) == (nextbp->b_flags & B_READ)) {
    464 			if ((du->dk_bct + nextbp->b_bcount) / DEV_BSIZE >= 240)
    465 				break;
    466 			du->dk_bct += nextbp->b_bcount;
    467 			nextbp->b_flags |= B_XXX;
    468 			oldbp = nextbp;
    469 			nextbp = nextbp->b_actf;
    470 		}
    471 	}
    472 
    473 	lp = &du->dk_dd;
    474 	secpertrk = lp->d_nsectors;
    475 	secpercyl = lp->d_secpercyl;
    476 	if ((du->dk_flags & DKFL_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW)
    477 		blknum += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    478 	cylin = blknum / secpercyl;
    479 	head = (blknum % secpercyl) / secpertrk;
    480 	sector = blknum % secpertrk;
    481 
    482 	/* Check for bad sectors if we have them, and not formatting */
    483 	/* Only do this in single-sector mode, or when starting a */
    484 	/* multiple-sector transfer. */
    485 	if ((du->dk_flags & DKFL_BADSECT) &&
    486 #ifdef B_FORMAT
    487 	    (bp->b_flags & B_FORMAT) == 0 &&
    488 #endif
    489 	    (du->dk_skipm == 0 || (du->dk_flags & DKFL_SINGLE))) {
    490 
    491 		long blkchk, blkend, blknew;
    492 		int i;
    493 
    494 		blkend = blknum + howmany(du->dk_bct, DEV_BSIZE) - 1;
    495 		for (i = 0; (blkchk = du->dk_badsect[i]) != -1; i++) {
    496 			if (blkchk > blkend)
    497 				break;	/* transfer is completely OK; done */
    498 			if (blkchk == blknum) {
    499 				blknew =
    500 				    lp->d_secperunit - lp->d_nsectors - i - 1;
    501 				cylin = blknew / secpercyl;
    502 				head = (blknew % secpercyl) / secpertrk;
    503 				sector = blknew % secpertrk;
    504 				du->dk_flags |= DKFL_SINGLE;
    505 				/* found and replaced first blk of transfer; done */
    506 				break;
    507 			} else if (blkchk > blknum) {
    508 				du->dk_flags |= DKFL_SINGLE;
    509 				break;	/* bad block inside transfer; done */
    510 			}
    511 		}
    512 	}
    513 	if (du->dk_flags & DKFL_SINGLE) {
    514 		du->dk_bct = du->dk_bc;
    515 		du->dk_skipm = du->dk_skip;
    516 	}
    517 
    518 #ifdef WDDEBUG
    519 	printf("c%d h%d s%d ", cylin, head, sector);
    520 #endif
    521 
    522 	sector++;	/* sectors begin with 1, not 0 */
    523 
    524 	wdtab[ctrlr].b_active = 1;		/* mark controller active */
    525 	wdc = du->dk_port;
    526 
    527 #ifdef INSTRUMENT
    528 	/* instrumentation */
    529 	if (du->dk_unit >= 0 && du->dk_skip == 0) {
    530 		dk_busy |= 1 << du->dk_lunit;
    531 		dk_wds[du->dk_lunit] += bp->b_bcount >> 6;
    532 	}
    533 	if (du->dk_unit >= 0 && du->dk_skipm == 0) {
    534 		++dk_seek[du->dk_lunit];
    535 		++dk_xfer[du->dk_lunit];
    536 	}
    537 #endif
    538 
    539 retry:
    540 	/* if starting a multisector transfer, or doing single transfers */
    541 	if (du->dk_skipm == 0 || (du->dk_flags & DKFL_SINGLE)) {
    542 		int command;
    543 
    544 		if (wdtab[ctrlr].b_errcnt && (bp->b_flags & B_READ) == 0) {
    545 			du->dk_bc += DEV_BSIZE;
    546 			du->dk_bct += DEV_BSIZE;
    547 		}
    548 
    549 		/* controller idle? */
    550 		if (wait_for_unbusy(du) < 0) {
    551 			wdreset(du, 1);
    552 			goto retry;
    553 		}
    554 
    555 		/* stuff the task file */
    556 		outb(wdc+wd_precomp, lp->d_precompcyl / 4);
    557 #ifdef B_FORMAT
    558 		if (bp->b_flags & B_FORMAT) {
    559 			outb(wdc+wd_sector, lp->d_gap3);
    560 			outb(wdc+wd_seccnt, lp->d_nsectors);
    561 		} else {
    562 			if (du->dk_flags & DKFL_SINGLE)
    563 				outb(wdc+wd_seccnt, 1);
    564 			else
    565 				outb(wdc+wd_seccnt,
    566 				    howmany(du->dk_bct, DEV_BSIZE));
    567 			outb(wdc+wd_sector, sector);
    568 		}
    569 #else
    570 		if (du->dk_flags & DKFL_SINGLE)
    571 			outb(wdc+wd_seccnt, 1);
    572 		else
    573 			outb(wdc+wd_seccnt, howmany(du->dk_bct, DEV_BSIZE));
    574 		outb(wdc+wd_sector, sector);
    575 #endif
    576 		outb(wdc+wd_cyl_lo, cylin);
    577 		outb(wdc+wd_cyl_hi, cylin >> 8);
    578 
    579 		/* set up the SDH register (select drive) */
    580 		outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit<<4) | (head & 0xf));
    581 		/* wait for drive to become ready */
    582 		if (wait_for_ready(du) < 0) {
    583 			wdreset(du, 1);
    584 			goto retry;
    585 		}
    586 
    587 		/* initiate command! */
    588 #ifdef B_FORMAT
    589 		if (bp->b_flags & B_FORMAT)
    590 			command = WDCC_FORMAT;
    591 		else
    592 			command =
    593 			    (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
    594 #else
    595 		command = (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
    596 #endif
    597 		if (wdcommand(du, command) < 0) {
    598 			wdreset(du, 1);
    599 			goto retry;
    600 		}
    601 #ifdef WDDEBUG
    602 		printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
    603 		    cylin, head, addr, inb(wdc+wd_altsts));
    604 #endif
    605 	}
    606 
    607 	/* if this is a read operation, just go away until it's done.	*/
    608 	if (bp->b_flags & B_READ) {
    609 		wdtimeoutstatus[lunit] = 2;
    610 		return;
    611 	}
    612 
    613 	if (wait_for_drq(du) < 0) {
    614 		wdreset(du, 1);
    615 		goto retry;
    616 	}
    617 	/* then send it! */
    618 	outsw(wdc+wd_data, addr + du->dk_skip * DEV_BSIZE,
    619 	    DEV_BSIZE / sizeof(short));
    620 
    621 	du->dk_bc -= DEV_BSIZE;
    622 	du->dk_bct -= DEV_BSIZE;
    623 
    624 	wdtimeoutstatus[lunit] = 2;
    625 }
    626 
    627 /* Interrupt routine for the controller.  Acknowledge the interrupt, check for
    628  * errors on the current operation, mark it done if necessary, and start
    629  * the next request.  Also check for a partially done transfer, and
    630  * continue with the next chunk if so.
    631  */
    632 void
    633 wdintr(int ctrlr)
    634 {
    635 	register struct	disk *du;
    636 	register struct buf *bp, *dp;
    637 	int stat, wdc;
    638 
    639 	if (!wdtab[ctrlr].b_active) {
    640 		printf("wdc%d: extra interrupt\n", ctrlr);
    641 		return;
    642 	}
    643 
    644 	dp = wdtab[ctrlr].b_forw;
    645 	bp = dp->b_actf;
    646 	du = wddrives[WDUNIT(bp->b_dev)];
    647 	wdc = du->dk_port;
    648 	wdtimeoutstatus[WDUNIT(bp->b_dev)] = 0;
    649 
    650 #ifdef WDDEBUG
    651 	printf("I%d ", ctrlr);
    652 #endif
    653 
    654 	stat = wait_for_unbusy(du);
    655 	if (stat < 0) {
    656 		printf("wdc%d: timeout waiting for unbusy\n", ctrlr);
    657 		goto lose;
    658 	}
    659 
    660 	/* is it not a transfer, but a control operation? */
    661 	if (du->dk_state < OPEN) {
    662 		wdtab[ctrlr].b_active = 0;
    663 		if (wdcontrol(bp))
    664 			wdstart(ctrlr);
    665 		return;
    666 	}
    667 
    668 	/* have we an error? */
    669 	if (stat & (WDCS_ERR | WDCS_ECCCOR)) {
    670 	lose:
    671 		du->dk_status = stat;
    672 		du->dk_error = inb(wdc+wd_error);
    673 #ifdef WDDEBUG
    674 		printf("stat %x error %x\n", stat, du->dk_error);
    675 #endif
    676 		if ((du->dk_flags & DKFL_SINGLE) == 0) {
    677 			du->dk_flags |= DKFL_ERROR;
    678 			goto outt;
    679 		}
    680 #ifdef B_FORMAT
    681 		if (bp->b_flags & B_FORMAT) {
    682 			bp->b_error = EIO;
    683 			bp->b_flags |= B_ERROR;
    684 			goto done;
    685 		}
    686 #endif
    687 
    688 		/* error or error correction? */
    689 		if (stat & WDCS_ERR) {
    690 			if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
    691 				wdtab[ctrlr].b_active = 0;
    692 			else {
    693 				if ((du->dk_flags & DKFL_QUIET) == 0) {
    694 					diskerr(bp, "wd", "hard error",
    695 					    LOG_PRINTF, du->dk_skip,
    696 					    &du->dk_dd);
    697 #ifdef WDDEBUG
    698 					printf("stat %b error %b\n", stat,
    699 					    WDCS_BITS, inb(wdc+wd_error),
    700 					    WDERR_BITS);
    701 #endif
    702 				}
    703 				bp->b_error = EIO;
    704 				bp->b_flags |= B_ERROR;	/* flag the error */
    705 			}
    706 		} else if ((du->dk_flags & DKFL_QUIET) == 0)
    707 			diskerr(bp, "wd", "soft ecc", 0, du->dk_skip,
    708 			    &du->dk_dd);
    709 	}
    710 
    711 	/*
    712 	 * If this was a successful read operation, fetch the data.
    713 	 */
    714 	if (((bp->b_flags & (B_READ | B_ERROR)) == B_READ) &&
    715 	    wdtab[ctrlr].b_active) {
    716 		int chk;
    717 
    718 		chk = min(DEV_BSIZE / sizeof(short), du->dk_bc / sizeof(short));
    719 
    720 		/* ready to receive data? */
    721 		if (wait_for_drq(du) < 0) {
    722 			printf("wdc%d: timeout waiting for drq\n", ctrlr);
    723 			goto lose;
    724 		}
    725 
    726 		/* suck in data */
    727 		insw(wdc+wd_data,
    728 		    (int)bp->b_un.b_addr + du->dk_skip * DEV_BSIZE, chk);
    729 		du->dk_bc -= chk * sizeof(short);
    730 		du->dk_bct -= chk * sizeof(short);
    731 
    732 		/* for obselete fractional sector reads */
    733 		while (chk++ < (DEV_BSIZE / sizeof(short)))
    734 			(void) inw(wdc+wd_data);
    735 	}
    736 
    737 	wdxfer[du->dk_lunit]++;
    738 outt:
    739 	if (wdtab[ctrlr].b_active) {
    740 #ifdef INSTRUMENT
    741 		if (du->dk_unit >= 0)
    742 			dk_busy &= ~(1 << du->dk_unit);
    743 #endif
    744 		if ((bp->b_flags & B_ERROR) == 0) {
    745 			du->dk_skip++;	/* Add to succ. sect */
    746 			du->dk_skipm++;	/* Add to succ. sect for multitransfer */
    747 			if (wdtab[ctrlr].b_errcnt &&
    748 			    (du->dk_flags & DKFL_QUIET) == 0)
    749 				diskerr(bp, "wd", "soft error", 0, du->dk_skip,
    750 				    &du->dk_dd);
    751 			wdtab[ctrlr].b_errcnt = 0;
    752 
    753 			/* see if more to transfer */
    754 			if (du->dk_bc > 0 && (du->dk_flags & DKFL_ERROR) == 0) {
    755 				wdtab[ctrlr].b_active = 0;
    756 				wdstart(ctrlr);
    757 				return;	/* next chunk is started */
    758 			} else if ((du->dk_flags & (DKFL_SINGLE | DKFL_ERROR)) == DKFL_ERROR) {
    759 				du->dk_skip = 0;
    760 				du->dk_skipm = 0;
    761 				du->dk_flags &= ~DKFL_ERROR;
    762 				du->dk_flags |=  DKFL_SINGLE;
    763 				wdtab[ctrlr].b_active = 0;
    764 				wdstart(ctrlr);
    765 				return;	/* redo xfer sector by sector */
    766 			}
    767 		}
    768 
    769 done:
    770 		/* done with this transfer, with or without error */
    771 		du->dk_flags &= ~DKFL_SINGLE;
    772 		wdtab[ctrlr].b_errcnt = 0;
    773 		if (du->dk_bct == 0) {
    774 			wdtab[ctrlr].b_forw = dp->b_forw;
    775 			du->dk_skipm = 0;
    776 			dp->b_active = 0;
    777 		}
    778 		bp->b_resid = bp->b_bcount - du->dk_skip * DEV_BSIZE;
    779 		bp->b_flags &= ~B_XXX;
    780 		du->dk_skip = 0;
    781 		dp->b_actf = bp->b_actf;
    782 		dp->b_errcnt = 0;
    783 		biodone(bp);
    784 	}
    785 
    786 	/* controller now idle */
    787 	wdtab[ctrlr].b_active = 0;
    788 
    789 	/* anything more on drive queue? */
    790 	if (dp->b_actf && du->dk_bct == 0)
    791 		wdustart(du);
    792 
    793 	/* anything more for controller to do? */
    794 	if (wdtab[ctrlr].b_forw)
    795 		wdstart(ctrlr);
    796 }
    797 
    798 /*
    799  * Initialize a drive.
    800  */
    801 int
    802 wdopen(dev_t dev, int flags, int fmt, struct proc *p)
    803 {
    804 	int lunit;
    805 	struct disk *du;
    806 	int part = WDPART(dev), mask = 1 << part;
    807 	struct partition *pp;
    808 	char *msg;
    809 
    810 	lunit = WDUNIT(dev);
    811 	if (lunit >= NWD)
    812 		return ENXIO;
    813 	du = wddrives[lunit];
    814 	if (du == 0)
    815 		return ENXIO;
    816 
    817 #ifdef QUIETWORKS
    818 	if (part == WDRAW)
    819 		du->dk_flags |= DKFL_QUIET;
    820 	else
    821 		du->dk_flags &= ~DKFL_QUIET;
    822 #else
    823 	du->dk_flags &= ~DKFL_QUIET;
    824 #endif
    825 
    826 	if ((du->dk_flags & DKFL_BSDLABEL) == 0) {
    827 		du->dk_flags |= DKFL_WRITEPROT;
    828 		wdutab[lunit].b_actf = NULL;
    829 
    830 		/*
    831 		 * Use the default sizes until we've read the label,
    832 		 * or longer if there isn't one there.
    833 		 */
    834 		bzero(&du->dk_dd, sizeof(du->dk_dd));
    835 		du->dk_dd.d_type = DTYPE_ST506;
    836 		du->dk_dd.d_ncylinders = 1024;
    837 		du->dk_dd.d_secsize = DEV_BSIZE;
    838 		du->dk_dd.d_ntracks = 8;
    839 		du->dk_dd.d_nsectors = 17;
    840 		du->dk_dd.d_secpercyl = 17*8;
    841 		du->dk_dd.d_secperunit = 17*8*1024;
    842 		du->dk_state = WANTOPEN;
    843 
    844 		/* read label using "raw" partition */
    845 #if defined(TIHMODS) && defined(garbage)
    846 		/* wdsetctlr(dev, du); */	/* Maybe do this TIH */
    847 		msg = readdisklabel(makewddev(major(dev), WDUNIT(dev), WDRAW),
    848 		    wdstrategy, &du->dk_dd, &du->dk_cpd);
    849 		wdsetctlr(dev, du);
    850 #endif
    851 		if (msg = readdisklabel(makewddev(major(dev), WDUNIT(dev),
    852 		    WDRAW), wdstrategy, &du->dk_dd, &du->dk_cpd)) {
    853 			if ((du->dk_flags & DKFL_QUIET) == 0) {
    854 				log(LOG_WARNING,
    855 				    "wd%d: cannot find label (%s)\n",
    856 				    lunit, msg);
    857 				return EINVAL;	/* XXX needs translation */
    858 			}
    859 		} else {
    860 			wdsetctlr(dev, du);
    861 			du->dk_flags |= DKFL_BSDLABEL;
    862 			du->dk_flags &= ~DKFL_WRITEPROT;
    863 			if (du->dk_dd.d_flags & D_BADSECT)
    864 				du->dk_flags |= DKFL_BADSECT;
    865 		}
    866 	}
    867 
    868 	if (du->dk_flags & DKFL_BADSECT)
    869 		bad144intern(du);
    870 
    871 	/*
    872 	 * Warn if a partion is opened
    873 	 * that overlaps another partition which is open
    874 	 * unless one is the "raw" partition (whole disk).
    875 	 */
    876 	if ((du->dk_openpart & mask) == 0 && part != WDRAW) {
    877 		int start, end;
    878 
    879 		pp = &du->dk_dd.d_partitions[part];
    880 		start = pp->p_offset;
    881 		end = pp->p_offset + pp->p_size;
    882 		for (pp = du->dk_dd.d_partitions;
    883 		    pp < &du->dk_dd.d_partitions[du->dk_dd.d_npartitions];
    884 		    pp++) {
    885 			if (pp->p_offset + pp->p_size <= start ||
    886 			    pp->p_offset >= end)
    887 				continue;
    888 			if (pp - du->dk_dd.d_partitions == WDRAW)
    889 				continue;
    890 			if (du->dk_openpart & (1 << (pp - du->dk_dd.d_partitions)))
    891 				log(LOG_WARNING,
    892 				    "wd%d%c: overlaps open partition (%c)\n",
    893 				    lunit, part + 'a',
    894 				    pp - du->dk_dd.d_partitions + 'a');
    895 		}
    896 	}
    897 	if (part >= du->dk_dd.d_npartitions && part != WDRAW)
    898 		return ENXIO;
    899 
    900 	/* insure only one open at a time */
    901 	du->dk_openpart |= mask;
    902 	switch (fmt) {
    903 	case S_IFCHR:
    904 		du->dk_copenpart |= mask;
    905 		break;
    906 	case S_IFBLK:
    907 		du->dk_bopenpart |= mask;
    908 		break;
    909 	}
    910 	return 0;
    911 }
    912 
    913 /*
    914  * Implement operations other than read/write.
    915  * Called from wdstart or wdintr during opens and formats.
    916  * Uses finite-state-machine to track progress of operation in progress.
    917  * Returns 0 if operation still in progress, 1 if completed.
    918  */
    919 static int
    920 wdcontrol(register struct buf *bp)
    921 {
    922 	struct disk *du;
    923 	int unit, lunit;
    924 	int stat;
    925 	int s, ctrlr;
    926 	int wdc;
    927 
    928 	du = wddrives[WDUNIT(bp->b_dev)];
    929 	ctrlr = du->dk_ctrlr;
    930 	unit = du->dk_unit;
    931 	lunit = du->dk_lunit;
    932 	wdc = du->dk_port;
    933 
    934 	switch (du->dk_state) {
    935 	case WANTOPEN:			/* set SDH, step rate, do restore */
    936 	tryagainrecal:
    937 #ifdef WDDEBUG
    938 		printf("wd%d: recal ", lunit);
    939 #endif
    940 		s = splbio();		/* not called from intr level ... */
    941 		wdgetctlr(unit, du);
    942 
    943 		if (wait_for_unbusy(du) < 0) {
    944 			wdreset(du, 1);
    945 			goto tryagainrecal;
    946 		}
    947 
    948 		outb(wdc+wd_sdh, WDSD_IBM | (unit << 4));
    949 		if (wait_for_ready(du) < 0) {
    950 			wdreset(du, 1);
    951 			goto tryagainrecal;
    952 		}
    953 
    954 		wdtab[ctrlr].b_active = 1;
    955 		if (wdcommand(du, WDCC_RESTORE | WD_STEP) < 0) {
    956 			wdreset(du, 1);
    957 			goto tryagainrecal;
    958 		}
    959 		du->dk_state = RECAL;
    960 		splx(s);
    961 		return 0;
    962 	case RECAL:
    963 		if ((stat = inb(wdc+wd_altsts)) & WDCS_ERR) {
    964 			if ((du->dk_flags & DKFL_QUIET) == 0) {
    965 				printf("wd%d: recal", du->dk_lunit);
    966 				printf(": status %b error %b\n", stat,
    967 				    WDCS_BITS, inb(wdc+wd_error), WDERR_BITS);
    968 			}
    969 			if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
    970 				goto tryagainrecal;
    971 			bp->b_error = ENXIO;	/* XXX needs translation */
    972 			goto badopen;
    973 		}
    974 
    975 		/* some controllers require this ... */
    976 		wdsetctlr(bp->b_dev, du);
    977 
    978 		wdtab[ctrlr].b_errcnt = 0;
    979 		du->dk_state = OPEN;
    980 		/*
    981 		 * The rest of the initialization can be done
    982 		 * by normal means.
    983 		 */
    984 		return 1;
    985 	default:
    986 		panic("wdcontrol");
    987 	}
    988 	/* NOTREACHED */
    989 
    990 badopen:
    991 	if ((du->dk_flags & DKFL_QUIET) == 0)
    992 		printf(": status %b error %b\n", stat, WDCS_BITS,
    993 		    inb(wdc+wd_error), WDERR_BITS);
    994 	bp->b_flags |= B_ERROR;
    995 	return 1;
    996 }
    997 
    998 /*
    999  * send a command and wait uninterruptibly until controller is finished.
   1000  * return -1 if controller busy for too long, otherwise
   1001  * return status. intended for brief controller commands at critical points.
   1002  * assumes interrupts are blocked.
   1003  */
   1004 static int
   1005 wdcommand(struct disk *du, int cmd)
   1006 {
   1007 	int stat, wdc;
   1008 
   1009 	wdc = du->dk_port;
   1010 
   1011 	/* controller ready for command? */
   1012 	if (wait_for_unbusy(du) < 0)
   1013 		return -1;
   1014 
   1015 	/* send command, await results */
   1016 	outb(wdc+wd_command, cmd);
   1017 
   1018 	switch (cmd) {
   1019 	default:
   1020 #if 0
   1021 	case WDCC_FORMAT:
   1022 	case WDCC_RESTORE | WD_STEP:
   1023 #endif
   1024 		return wait_for_unbusy(du);
   1025 	case WDCC_READ:
   1026 	case WDCC_WRITE:
   1027 		return 0;
   1028 	case WDCC_IDC:
   1029 	case WDCC_DIAGNOSE:
   1030 		return wdc_wait(du, WDCS_READY);
   1031 	case WDCC_READP:
   1032 		return wait_for_drq(du);
   1033 	}
   1034 }
   1035 
   1036 /*
   1037  * issue IDC to drive to tell it just what geometry it is to be.
   1038  */
   1039 static int
   1040 wdsetctlr(dev_t dev, struct disk *du)
   1041 {
   1042 	int stat, s, wdc;
   1043 
   1044 #ifdef WDDEBUG
   1045 	printf("wd(%d,%d) C%dH%dS%d\n", du->dk_ctrlr, du->dk_unit,
   1046 	    du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks, du->dk_dd.d_nsectors);
   1047 #endif
   1048 
   1049 	wdc = du->dk_port;
   1050 
   1051 	s = splbio();
   1052 	if (wait_for_unbusy(du) < 0)
   1053 		return -1;
   1054 	outb(wdc+wd_cyl_lo, du->dk_dd.d_ncylinders);	/* TIH: was ...ders+1 */
   1055 	outb(wdc+wd_cyl_hi, du->dk_dd.d_ncylinders>>8);	/* TIH: was ...ders+1 */
   1056 	outb(wdc+wd_sdh,
   1057 	    WDSD_IBM | (du->dk_unit << 4) + du->dk_dd.d_ntracks - 1);
   1058 	outb(wdc+wd_seccnt, du->dk_dd.d_nsectors);
   1059 	if (wait_for_ready(du) < 0)
   1060 		return -1;
   1061 	stat = wdcommand(du, WDCC_IDC);
   1062 	if (stat < 0 || stat & WDCS_ERR)
   1063 		printf("wdsetctlr: stat %b error %b\n", stat, WDCS_BITS,
   1064 		    inb(wdc+wd_error), WDERR_BITS);
   1065 	splx(s);
   1066 	return stat;
   1067 }
   1068 
   1069 /*
   1070  * issue READP to drive to ask it what it is.
   1071  */
   1072 static int
   1073 wdgetctlr(int u, struct disk *du)
   1074 {
   1075 	int stat, s, i, wdc;
   1076 	char tb[DEV_BSIZE];
   1077 	struct wdparams *wp;
   1078 
   1079 	s = splbio();		/* not called from intr level ... */
   1080 	wdc = du->dk_port;
   1081 	if (wait_for_unbusy(du) < 0) {
   1082 		splx(s);
   1083 		return -1;
   1084 	}
   1085 
   1086 	outb(wdc+wd_sdh, WDSD_IBM | (u << 4));
   1087 	if (wait_for_ready(du) < 0) {
   1088 		splx(s);
   1089 		return -1;
   1090 	}
   1091 
   1092 	stat = wdcommand(du, WDCC_READP);
   1093 	if (stat < 0) {
   1094 		splx(s);
   1095 		return -1;
   1096 	}
   1097 
   1098 	if ((stat & WDCS_ERR) == 0) {
   1099 		/* obtain parameters */
   1100 		wp = &du->dk_params;
   1101 		insw(wdc+wd_data, tb, sizeof(tb)/sizeof(short));
   1102 		bcopy(tb, wp, sizeof(struct wdparams));
   1103 
   1104 		/* shuffle string byte order */
   1105 		for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
   1106 			u_short *p;
   1107 			p = (u_short *)(wp->wdp_model + i);
   1108 			*p = ntohs(*p);
   1109 		}
   1110 
   1111 		strncpy(du->dk_dd.d_typename, "ESDI/IDE",
   1112 		    sizeof du->dk_dd.d_typename);
   1113 		du->dk_dd.d_type = DTYPE_ESDI;
   1114 		bcopy(wp->wdp_model+20, du->dk_dd.d_packname, 14-1);
   1115 
   1116 		/* update disklabel given drive information */
   1117 		du->dk_dd.d_ncylinders =
   1118 		    wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
   1119 		du->dk_dd.d_ntracks = wp->wdp_heads;
   1120 		du->dk_dd.d_nsectors = wp->wdp_sectors;
   1121 		du->dk_dd.d_secpercyl =
   1122 		    du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
   1123 		du->dk_dd.d_partitions[1].p_size =
   1124 		    du->dk_dd.d_secpercyl * wp->wdp_sectors;
   1125 		du->dk_dd.d_partitions[1].p_offset = 0;
   1126 	} else {
   1127 		/*
   1128 		 * If WDCC_READP fails then we might have an old drive
   1129 		 * so we try a seek to 0; if that passes then the
   1130 		 * drive is there but it's OLD AND KRUSTY.
   1131 		 */
   1132 		stat = wdcommand(du, WDCC_RESTORE | WD_STEP);
   1133 		if (stat < 0 || stat & WDCS_ERR) {
   1134 			splx(s);
   1135 			return inb(wdc+wd_error);
   1136 		}
   1137 
   1138 		strncpy(du->dk_dd.d_typename, "ST506",
   1139 		    sizeof du->dk_dd.d_typename);
   1140 		strncpy(du->dk_params.wdp_model, "Unknown Type",
   1141 		    sizeof du->dk_params.wdp_model);
   1142 		du->dk_dd.d_type = DTYPE_ST506;
   1143 	}
   1144 
   1145 #if 0
   1146 	printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
   1147 	    wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
   1148 	    wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
   1149 #endif
   1150 
   1151 	/* better ... */
   1152 	du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
   1153 
   1154 	/* XXX sometimes possibly needed */
   1155 	(void) inb(wdc+wd_status);
   1156 	splx(s);
   1157 	return 0;
   1158 }
   1159 
   1160 
   1161 /* ARGSUSED */
   1162 int
   1163 wdclose(dev_t dev, int flags, int fmt)
   1164 {
   1165 	struct disk *du;
   1166 	int part = WDPART(dev), mask = 1 << part;
   1167 
   1168 	du = wddrives[WDUNIT(dev)];
   1169 
   1170 	/* insure only one open at a time */
   1171 	du->dk_openpart &= ~mask;
   1172 	switch (fmt) {
   1173 	case S_IFCHR:
   1174 		du->dk_copenpart &= ~mask;
   1175 		break;
   1176 	case S_IFBLK:
   1177 		du->dk_bopenpart &= ~mask;
   1178 		break;
   1179 	}
   1180 	return 0;
   1181 }
   1182 
   1183 int
   1184 wdioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p)
   1185 {
   1186 	int lunit = WDUNIT(dev);
   1187 	struct disk *du = wddrives[lunit];
   1188 	int error;
   1189 
   1190 	switch (cmd) {
   1191 	case DIOCSBAD:
   1192 		if ((flag & FWRITE) == 0)
   1193 			return EBADF;
   1194 		du->dk_cpd.bad = *(struct dkbad *)addr;
   1195 		bad144intern(du);
   1196 		return 0;
   1197 
   1198 	case DIOCGDINFO:
   1199 		*(struct disklabel *)addr = du->dk_dd;
   1200 		return 0;
   1201 
   1202 	case DIOCGPART:
   1203 		((struct partinfo *)addr)->disklab = &du->dk_dd;
   1204 		((struct partinfo *)addr)->part =
   1205 		    &du->dk_dd.d_partitions[WDPART(dev)];
   1206 		return 0;
   1207 
   1208 	case DIOCSDINFO:
   1209 		if ((flag & FWRITE) == 0)
   1210 			return EBADF;
   1211 		error = setdisklabel(&du->dk_dd,
   1212 		    (struct disklabel *)addr,
   1213 		    /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart : */0,
   1214 		    &du->dk_cpd);
   1215 		if (error == 0) {
   1216 			du->dk_flags |= DKFL_BSDLABEL;
   1217 			wdsetctlr(dev, du);
   1218 		}
   1219 		return error;
   1220 
   1221 	case DIOCWLABEL:
   1222 		du->dk_flags &= ~DKFL_WRITEPROT;
   1223 		if ((flag & FWRITE) == 0)
   1224 			return EBADF;
   1225 		du->dk_wlabel = *(int *)addr;
   1226 		return 0;
   1227 
   1228 	case DIOCWDINFO:
   1229 		du->dk_flags &= ~DKFL_WRITEPROT;
   1230 		if ((flag & FWRITE) == 0)
   1231 			return EBADF;
   1232 		error = setdisklabel(&du->dk_dd,
   1233 		    (struct disklabel *)addr,
   1234 		    /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart :*/0,
   1235 		    &du->dk_cpd);
   1236 		if (error == 0) {
   1237 			int wlab;
   1238 
   1239 			du->dk_flags |= DKFL_BSDLABEL;
   1240 			wdsetctlr(dev, du);
   1241 
   1242 			/* simulate opening partition 0 so write succeeds */
   1243 			du->dk_openpart |= (1 << 0);	    /* XXX */
   1244 			wlab = du->dk_wlabel;
   1245 			du->dk_wlabel = 1;
   1246 			error = writedisklabel(dev, wdstrategy, &du->dk_dd,				    &du->dk_cpd);
   1247 			du->dk_openpart = du->dk_copenpart | du->dk_bopenpart;
   1248 			du->dk_wlabel = wlab;
   1249 		}
   1250 		return error;
   1251 
   1252 #ifdef notyet
   1253 	case DIOCGDINFOP:
   1254 		*(struct disklabel **)addr = &du->dk_dd;
   1255 		return 0;
   1256 
   1257 	case DIOCWFORMAT:
   1258 		if ((flag & FWRITE) == 0)
   1259 			return EBADF;
   1260 	{
   1261 		register struct format_op *fop;
   1262 		struct iovec aiov;
   1263 		struct uio auio;
   1264 
   1265 		fop = (struct format_op *)addr;
   1266 		aiov.iov_base = fop->df_buf;
   1267 		aiov.iov_len = fop->df_count;
   1268 		auio.uio_iov = &aiov;
   1269 		auio.uio_iovcnt = 1;
   1270 		auio.uio_resid = fop->df_count;
   1271 		auio.uio_segflg = 0;
   1272 		auio.uio_offset =
   1273 		    fop->df_startblk * du->dk_dd.d_secsize;
   1274 		error = physio(wdformat, &rwdbuf[lunit], dev, B_WRITE,
   1275 		    minphys, &auio);
   1276 		fop->df_count -= auio.uio_resid;
   1277 		fop->df_reg[0] = du->dk_status;
   1278 		fop->df_reg[1] = du->dk_error;
   1279 		return error;
   1280 	}
   1281 #endif
   1282 
   1283 	default:
   1284 		return ENOTTY;
   1285 	}
   1286 
   1287 #ifdef DIAGNOSTIC
   1288 	panic("wdioctl: impossible");
   1289 #endif
   1290 }
   1291 
   1292 #ifdef B_FORMAT
   1293 int
   1294 wdformat(struct buf *bp)
   1295 {
   1296 
   1297 	bp->b_flags |= B_FORMAT;
   1298 	return wdstrategy(bp);
   1299 }
   1300 #endif
   1301 
   1302 int
   1303 wdsize(dev_t dev)
   1304 {
   1305 	int lunit = WDUNIT(dev), part = WDPART(dev);
   1306 	struct disk *du;
   1307 
   1308 	if (lunit >= NWD)
   1309 		return -1;
   1310 	du = wddrives[lunit];
   1311 	if (du == 0)
   1312 		return -1;
   1313 
   1314 	if (du->dk_state < OPEN || (du->dk_flags & DKFL_BSDLABEL) == 0) {
   1315 		int val;
   1316 		val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD,
   1317 		    S_IFBLK, 0);
   1318 		if (val != 0)
   1319 			return -1;
   1320 	}
   1321 
   1322 	if ((du->dk_flags & (DKFL_WRITEPROT | DKFL_BSDLABEL)) != DKFL_BSDLABEL)
   1323 		return -1;
   1324 
   1325 	return (int)du->dk_dd.d_partitions[part].p_size;
   1326 }
   1327 
   1328 extern	char *vmmap;	    /* poor name! */
   1329 
   1330 /* dump core after a system crash */
   1331 int
   1332 wddump(dev_t dev)
   1333 {
   1334 	register struct disk *du;	/* disk unit to do the IO */
   1335 	long num;			/* number of sectors to write */
   1336 	int ctrlr, lunit, part, wdc;
   1337 	long blkoff, blknum;
   1338 	long cylin, head, sector, stat;
   1339 	long secpertrk, secpercyl, nblocks, i;
   1340 	char *addr;
   1341 	static wddoingadump = 0;
   1342 	extern caddr_t CADDR1;
   1343 	extern struct pte *CMAP1;
   1344 
   1345 	addr = (char *)0;		/* starting address */
   1346 
   1347 #if DO_NOT_KNOW_HOW
   1348 	/* toss any characters present prior to dump, ie. non-blocking getc */
   1349 	while (cngetc())
   1350 		;
   1351 #endif
   1352 
   1353 	/* size of memory to dump */
   1354 	lunit = WDUNIT(dev);
   1355 	part = WDPART(dev);	/* file system */
   1356 	/* check for acceptable drive number */
   1357 	if (lunit >= NWD)
   1358 		return ENXIO;
   1359 	du = wddrives[lunit];
   1360 	/* was it ever initialized ? */
   1361 	if (du == 0 || du->dk_state < OPEN || du->dk_flags & DKFL_WRITEPROT)
   1362 		return ENXIO;
   1363 
   1364 	wdc = du->dk_port;
   1365 	ctrlr = du->dk_ctrlr;
   1366 
   1367 	/* Convert to disk sectors */
   1368 	num = ctob(physmem) / du->dk_dd.d_secsize;
   1369 
   1370 	/* check if controller active */
   1371 	/*if (wdtab[ctrlr].b_active)
   1372 		return EFAULT;*/
   1373 	if (wddoingadump)
   1374 		return EFAULT;
   1375 
   1376 	secpertrk = du->dk_dd.d_nsectors;
   1377 	secpercyl = du->dk_dd.d_secpercyl;
   1378 	nblocks = du->dk_dd.d_partitions[part].p_size;
   1379 	blkoff = du->dk_dd.d_partitions[part].p_offset;
   1380 
   1381 	/*printf("xunit %x, nblocks %d, dumplo %d num %d\n", part, nblocks,
   1382 	    dumplo, num);*/
   1383 	/* check transfer bounds against partition size */
   1384 	if (dumplo < 0 || dumplo + num > nblocks)
   1385 		return EINVAL;
   1386 
   1387 	/* mark controller active for if we panic during the dump */
   1388 	/*wdtab[ctrlr].b_active = 1;*/
   1389 	wddoingadump = 1;
   1390 	i = 200000000;
   1391 	if (wait_for_unbusy(du) < 0)
   1392 		return EIO;
   1393 	outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4));
   1394 	if (wait_for_ready(du) < 0)
   1395 		return EIO;
   1396 	if (wdcommand(du, WDCC_RESTORE | WD_STEP) < 0)
   1397 		return EIO;
   1398 
   1399 	/* some compaq controllers require this ... */
   1400 	wdsetctlr(dev, du);
   1401 
   1402 	blknum = dumplo + blkoff;
   1403 	while (num > 0) {
   1404 		/* compute disk address */
   1405 		cylin = blknum / secpercyl;
   1406 		head = (blknum % secpercyl) / secpertrk;
   1407 		sector = blknum % secpertrk;
   1408 
   1409 		if (du->dk_flags & DKFL_BADSECT) {
   1410 			long newblk;
   1411 			int i;
   1412 
   1413 			for (i = 0; du->dk_badsect[i] != -1; i++) {
   1414 				if (blknum < du->dk_badsect[i]) {
   1415 					/* sorted list, passed our block by */
   1416 					break;
   1417 				} else if (blknum == du->dk_badsect[i]) {
   1418 					newblk = du->dk_dd.d_secperunit -
   1419 						du->dk_dd.d_nsectors - i - 1;
   1420 					cylin = newblk / secpercyl;
   1421 					head = (newblk % secpercyl) / secpertrk;
   1422 					sector = newblk % secpertrk;
   1423 					/* found and replaced; done */
   1424 					break;
   1425 				}
   1426 			}
   1427 		}
   1428 		sector++;		/* origin 1 */
   1429 
   1430 		if (wait_for_unbusy(du) < 0)
   1431 			return EIO;
   1432 		/* select drive.     */
   1433 		outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4) | (head & 0xf));
   1434 		if (wait_for_ready(du) < 0)
   1435 			return EIO;
   1436 
   1437 		/* transfer some blocks */
   1438 		outb(wdc+wd_sector, sector);
   1439 		outb(wdc+wd_seccnt, 1);
   1440 		outb(wdc+wd_cyl_lo, cylin);
   1441 		outb(wdc+wd_cyl_hi, cylin >> 8);
   1442 #ifdef notdef
   1443 		/* lets just talk about this first...*/
   1444 		printf("sdh 0%o sector %d cyl %d addr 0x%x", inb(wdc+wd_sdh),
   1445 		    inb(wdc+wd_sector),
   1446 		    (inb(wdc+wd_cyl_hi) << 8) + inb(wdc+wd_cyl_lo), addr);
   1447 #endif
   1448 		stat = wdcommand(du, WDCC_WRITE);
   1449 		if (stat < 0 || stat & WDCS_ERR)
   1450 			return EIO;
   1451 
   1452 #ifdef notdef	/* cannot use this since this address was mapped differently */
   1453 		pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
   1454 #else
   1455 		*(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
   1456 		tlbflush();
   1457 #endif
   1458 
   1459 		outsw(wdc+wd_data, CADDR1 + ((int)addr & PGOFSET),
   1460 		    DEV_BSIZE / sizeof(short));
   1461 
   1462 		/* Check data request (should be done). */
   1463 		if (inb(wdc+wd_status) & (WDCS_ERR | WDCS_DRQ))
   1464 			return EIO;
   1465 
   1466 		if (wait_for_unbusy(du) < 0)
   1467 			return EIO;
   1468 
   1469 		/* error check the xfer */
   1470 		if (inb(wdc+wd_status) & WDCS_ERR)
   1471 			return EIO;
   1472 
   1473 		if ((unsigned)addr % 1048576 == 0)
   1474 			printf("%d ", num / (1048576 / DEV_BSIZE));
   1475 
   1476 		/* update block count */
   1477 		num--;
   1478 		blknum++;
   1479 		(int)addr += DEV_BSIZE;
   1480 
   1481 #if DO_NOT_KNOW_HOW
   1482 		/* operator aborting dump? non-blocking getc() */
   1483 		if (cngetc())
   1484 			return EINTR;
   1485 #endif
   1486 	}
   1487 	return 0;
   1488 }
   1489 
   1490 /*
   1491  * Internalize the bad sector table.
   1492  */
   1493 void
   1494 bad144intern(struct disk *du)
   1495 {
   1496 	int i;
   1497 	if (du->dk_flags & DKFL_BADSECT) {
   1498 		for (i = 0; i < 127; i++)
   1499 			du->dk_badsect[i] = -1;
   1500 		for (i = 0; i < 126; i++) {
   1501 			if (du->dk_cpd.bad.bt_bad[i].bt_cyl == 0xffff)
   1502 				break;
   1503 			du->dk_badsect[i] =
   1504 			    du->dk_cpd.bad.bt_bad[i].bt_cyl *
   1505 				du->dk_dd.d_secpercyl +
   1506 			    (du->dk_cpd.bad.bt_bad[i].bt_trksec >> 8) *
   1507 				du->dk_dd.d_nsectors +
   1508 			    (du->dk_cpd.bad.bt_bad[i].bt_trksec & 0x00ff);
   1509 		}
   1510 	}
   1511 }
   1512 
   1513 static int
   1514 wdreset(struct disk *du, int err)
   1515 {
   1516 	int wdc = du->dk_port;
   1517 	int stat;
   1518 
   1519 	if (err)
   1520 		printf("wdc%d: busy too long, resetting\n", du->dk_ctrlr);
   1521 
   1522 	/* reset the device  */
   1523 	outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
   1524 	DELAY(1000);
   1525 	outb(wdc+wd_ctlr, WDCTL_4BIT);
   1526 
   1527 	if (wait_for_unbusy(du) < 0)
   1528 		printf("wdc%d: failed to reset controller\n", du->dk_ctrlr);
   1529 }
   1530 
   1531 int
   1532 wdc_wait(du, mask)
   1533 	struct disk *du;
   1534 	int mask;
   1535 {
   1536 	int wdc = du->dk_port;
   1537 	int timeout = 0;
   1538 	int stat;
   1539 
   1540 	for (;;) {
   1541 		stat = inb(wdc+wd_altsts);
   1542 		if ((stat & WDCS_BUSY) == 0 && (stat & mask) == mask)
   1543 			break;
   1544 		DELAY(WDCDELAY);
   1545 		if (++timeout > WDCNDELAY)
   1546 			return -1;
   1547 	}
   1548 #ifdef WDCNDELAY_DEBUG
   1549 	if (timeout > WDCNDELAY_DEBUG)
   1550 		printf("wdc%d: timeout took %dus\n", du->dk_ctrlr,
   1551 		    WDCDELAY * timeout);
   1552 #endif
   1553 	return stat;
   1554 }
   1555 
   1556 static int
   1557 wdtimeout(caddr_t arg)
   1558 {
   1559 	int x = splbio();
   1560 	register int unit = (int)arg;
   1561 
   1562 	if (wdtimeoutstatus[unit] && --wdtimeoutstatus[unit] == 0) {
   1563 		struct disk *du = wddrives[unit];
   1564 		int wdc = du->dk_port;
   1565 
   1566 		printf("wd%d: lost interrupt - status %x, error %x\n",
   1567 		    unit, inb(wdc+wd_status), inb(wdc+wd_error));
   1568 		outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
   1569 		DELAY(1000);
   1570 		outb(wdc+wd_ctlr, WDCTL_IDS);
   1571 		DELAY(1000);
   1572 		(void) inb(wdc+wd_error);
   1573 		outb(wdc+wd_ctlr, WDCTL_4BIT);
   1574 		du->dk_skip = 0;
   1575 		du->dk_flags |= DKFL_SINGLE;
   1576 		wdstart(du->dk_ctrlr);		/* start controller */
   1577 	}
   1578 	timeout((timeout_t)wdtimeout, (caddr_t)unit, hz*2);
   1579 	splx(x);
   1580 	return 0;
   1581 }
   1582 
   1583 #endif /* NWDC > 0 */
   1584