Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.53
      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.53 1994/03/02 23:27:13 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)
    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 	unsigned char status;
    411 
    412 loop:
    413 	/* is there a drive for the controller to do a transfer with? */
    414 	dp = wdtab[ctrlr].b_forw;
    415 	if (dp == NULL) {
    416 		wdtab[ctrlr].b_active = 0;
    417 		return;
    418 	}
    419 
    420 	/* is there a transfer to this drive ? if so, link it on
    421 	   the controller's queue */
    422 	bp = dp->b_actf;
    423 	if (bp == NULL) {
    424 		dp->b_active = 0;
    425 		wdtab[ctrlr].b_forw = dp->b_forw;
    426 		goto loop;
    427 	}
    428 
    429 	/* obtain controller and drive information */
    430 	lunit = WDUNIT(bp->b_dev);
    431 	du = wddrives[lunit];
    432 
    433 	/* clear any pending timeout, just in case */
    434 	wdtimeoutstatus[lunit] = 0;
    435 
    436 	/* if not really a transfer, do control operations specially */
    437 	if (du->dk_state < OPEN) {
    438 		(void) wdcontrol(bp);
    439 		return;
    440 	}
    441 
    442 	/* calculate transfer details */
    443 	blknum = bp->b_blkno + du->dk_skip;
    444 #ifdef WDDEBUG
    445 	if (du->dk_skip == 0)
    446 		printf("\nwdstart %d: %s %d@%d; map ", lunit,
    447 		    (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
    448 		    blknum);
    449 	else
    450 		printf(" %d)%x", du->dk_skip, inb(du->dk_port+wd_altsts));
    451 #endif
    452 	addr = (int)bp->b_un.b_addr;
    453 	if (du->dk_skip == 0)
    454 		du->dk_bc = bp->b_bcount;
    455 	if (du->dk_skipm == 0) {
    456 		struct buf *oldbp, *nextbp;
    457 		oldbp = bp;
    458 		nextbp = bp->b_actf;
    459 		du->dk_bct = du->dk_bc;
    460 		oldbp->b_flags |= B_XXX;
    461 		while (nextbp && (oldbp->b_flags & DKFL_SINGLE) == 0 &&
    462 		    oldbp->b_dev == nextbp->b_dev && nextbp->b_blkno ==
    463 		    (oldbp->b_blkno + (oldbp->b_bcount / DEV_BSIZE)) &&
    464 		    (oldbp->b_flags & B_READ) == (nextbp->b_flags & B_READ)) {
    465 			if ((du->dk_bct + nextbp->b_bcount) / DEV_BSIZE >= 240)
    466 				break;
    467 			du->dk_bct += nextbp->b_bcount;
    468 			nextbp->b_flags |= B_XXX;
    469 			oldbp = nextbp;
    470 			nextbp = nextbp->b_actf;
    471 		}
    472 	}
    473 
    474 	lp = &du->dk_dd;
    475 	secpertrk = lp->d_nsectors;
    476 	secpercyl = lp->d_secpercyl;
    477 	if ((du->dk_flags & DKFL_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW)
    478 		blknum += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    479 	cylin = blknum / secpercyl;
    480 	head = (blknum % secpercyl) / secpertrk;
    481 	sector = blknum % secpertrk;
    482 
    483 	/* Check for bad sectors if we have them, and not formatting */
    484 	/* Only do this in single-sector mode, or when starting a */
    485 	/* multiple-sector transfer. */
    486 	if ((du->dk_flags & DKFL_BADSECT) &&
    487 #ifdef B_FORMAT
    488 	    (bp->b_flags & B_FORMAT) == 0 &&
    489 #endif
    490 	    (du->dk_skipm == 0 || (du->dk_flags & DKFL_SINGLE))) {
    491 
    492 		long blkchk, blkend, blknew;
    493 		int i;
    494 
    495 		blkend = blknum + howmany(du->dk_bct, DEV_BSIZE) - 1;
    496 		for (i = 0; (blkchk = du->dk_badsect[i]) != -1; i++) {
    497 			if (blkchk > blkend)
    498 				break;	/* transfer is completely OK; done */
    499 			if (blkchk == blknum) {
    500 				blknew =
    501 				    lp->d_secperunit - lp->d_nsectors - i - 1;
    502 				cylin = blknew / secpercyl;
    503 				head = (blknew % secpercyl) / secpertrk;
    504 				sector = blknew % secpertrk;
    505 				du->dk_flags |= DKFL_SINGLE;
    506 				/* found and replaced first blk of transfer; done */
    507 				break;
    508 			} else if (blkchk > blknum) {
    509 				du->dk_flags |= DKFL_SINGLE;
    510 				break;	/* bad block inside transfer; done */
    511 			}
    512 		}
    513 	}
    514 	if (du->dk_flags & DKFL_SINGLE) {
    515 		du->dk_bct = du->dk_bc;
    516 		du->dk_skipm = du->dk_skip;
    517 	}
    518 
    519 #ifdef WDDEBUG
    520 	printf("c%d h%d s%d ", cylin, head, sector);
    521 #endif
    522 
    523 	sector++;	/* sectors begin with 1, not 0 */
    524 
    525 	wdtab[ctrlr].b_active = 1;		/* mark controller active */
    526 	wdc = du->dk_port;
    527 
    528 #ifdef INSTRUMENT
    529 	/* instrumentation */
    530 	if (du->dk_unit >= 0 && du->dk_skip == 0) {
    531 		dk_busy |= 1 << du->dk_lunit;
    532 		dk_wds[du->dk_lunit] += bp->b_bcount >> 6;
    533 	}
    534 	if (du->dk_unit >= 0 && du->dk_skipm == 0) {
    535 		++dk_seek[du->dk_lunit];
    536 		++dk_xfer[du->dk_lunit];
    537 	}
    538 #endif
    539 
    540 retry:
    541 	/* if starting a multisector transfer, or doing single transfers */
    542 	if (du->dk_skipm == 0 || (du->dk_flags & DKFL_SINGLE)) {
    543 		int command;
    544 
    545 		if (wdtab[ctrlr].b_errcnt && (bp->b_flags & B_READ) == 0) {
    546 			du->dk_bc += DEV_BSIZE;
    547 			du->dk_bct += DEV_BSIZE;
    548 		}
    549 
    550 		/* controller idle? */
    551 		if (wait_for_unbusy(du) < 0) {
    552 			wdreset(du, 1);
    553 			goto retry;
    554 		}
    555 
    556 		/* stuff the task file */
    557 		outb(wdc+wd_precomp, lp->d_precompcyl / 4);
    558 #ifdef B_FORMAT
    559 		if (bp->b_flags & B_FORMAT) {
    560 			outb(wdc+wd_sector, lp->d_gap3);
    561 			outb(wdc+wd_seccnt, lp->d_nsectors);
    562 		} else {
    563 			if (du->dk_flags & DKFL_SINGLE)
    564 				outb(wdc+wd_seccnt, 1);
    565 			else
    566 				outb(wdc+wd_seccnt,
    567 				    howmany(du->dk_bct, DEV_BSIZE));
    568 			outb(wdc+wd_sector, sector);
    569 		}
    570 #else
    571 		if (du->dk_flags & DKFL_SINGLE)
    572 			outb(wdc+wd_seccnt, 1);
    573 		else
    574 			outb(wdc+wd_seccnt, howmany(du->dk_bct, DEV_BSIZE));
    575 		outb(wdc+wd_sector, sector);
    576 #endif
    577 		outb(wdc+wd_cyl_lo, cylin);
    578 		outb(wdc+wd_cyl_hi, cylin >> 8);
    579 
    580 		/* set up the SDH register (select drive) */
    581 		outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit<<4) | (head & 0xf));
    582 		/* wait for drive to become ready */
    583 		if (wait_for_ready(du) < 0) {
    584 			wdreset(du, 1);
    585 			goto retry;
    586 		}
    587 
    588 		/* initiate command! */
    589 #ifdef B_FORMAT
    590 		if (bp->b_flags & B_FORMAT)
    591 			command = WDCC_FORMAT;
    592 		else
    593 			command =
    594 			    (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
    595 #else
    596 		command = (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
    597 #endif
    598 		if (wdcommand(du, command) < 0) {
    599 			wdreset(du, 1);
    600 			goto retry;
    601 		}
    602 #ifdef WDDEBUG
    603 		printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
    604 		    cylin, head, addr, inb(wdc+wd_altsts));
    605 #endif
    606 	}
    607 
    608 	/* if this is a read operation, just go away until it's done.	*/
    609 	if (bp->b_flags & B_READ) {
    610 		wdtimeoutstatus[lunit] = 2;
    611 		return;
    612 	}
    613 
    614 	if (wait_for_drq(du) < 0) {
    615 		wdreset(du, 1);
    616 		goto retry;
    617 	}
    618 	/* clear interrupt from command upload */
    619 	(void) inb(wdc+wd_status);
    620 	/* then send it! */
    621 	outsw(wdc+wd_data, addr + du->dk_skip * DEV_BSIZE,
    622 	    DEV_BSIZE / sizeof(short));
    623 
    624 	du->dk_bc -= DEV_BSIZE;
    625 	du->dk_bct -= DEV_BSIZE;
    626 
    627 	wdtimeoutstatus[lunit] = 2;
    628 }
    629 
    630 /* Interrupt routine for the controller.  Acknowledge the interrupt, check for
    631  * errors on the current operation, mark it done if necessary, and start
    632  * the next request.  Also check for a partially done transfer, and
    633  * continue with the next chunk if so.
    634  */
    635 void
    636 wdintr(int ctrlr)
    637 {
    638 	register struct	disk *du;
    639 	register struct buf *bp, *dp;
    640 	int stat, wdc;
    641 
    642 	/* clear the pending interrupt */
    643 	stat = inb(wdcontroller[ctrlr].dkc_port+wd_status);
    644 #ifdef WDDEBUG
    645 	printf("i%02x ", stat);
    646 #endif
    647 
    648 	if (!wdtab[ctrlr].b_active) {
    649 		printf("wdc%d: extra interrupt\n", ctrlr);
    650 		return;
    651 	}
    652 
    653 	dp = wdtab[ctrlr].b_forw;
    654 	bp = dp->b_actf;
    655 	du = wddrives[WDUNIT(bp->b_dev)];
    656 	wdc = du->dk_port;
    657 	wdtimeoutstatus[WDUNIT(bp->b_dev)] = 0;
    658 
    659 #ifdef WDDEBUG
    660 	printf("I%d ", ctrlr);
    661 #endif
    662 
    663 	stat = wait_for_unbusy(du);
    664 	if (stat < 0) {
    665 		printf("wdc%d: timeout waiting for unbusy\n", ctrlr);
    666 		goto lose;
    667 	}
    668 
    669 	/* is it not a transfer, but a control operation? */
    670 	if (du->dk_state < OPEN) {
    671 		wdtab[ctrlr].b_active = 0;
    672 		if (wdcontrol(bp))
    673 			wdstart(ctrlr);
    674 		return;
    675 	}
    676 
    677 	/* have we an error? */
    678 	if (stat & (WDCS_ERR | WDCS_ECCCOR)) {
    679 	lose:
    680 		du->dk_status = stat;
    681 		du->dk_error = inb(wdc+wd_error);
    682 #ifdef WDDEBUG
    683 		printf("stat %x error %x\n", stat, du->dk_error);
    684 #endif
    685 		if ((du->dk_flags & DKFL_SINGLE) == 0) {
    686 			du->dk_flags |= DKFL_ERROR;
    687 			goto outt;
    688 		}
    689 #ifdef B_FORMAT
    690 		if (bp->b_flags & B_FORMAT) {
    691 			bp->b_error = EIO;
    692 			bp->b_flags |= B_ERROR;
    693 			goto done;
    694 		}
    695 #endif
    696 
    697 		/* error or error correction? */
    698 		if (stat & WDCS_ERR) {
    699 			if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
    700 				wdtab[ctrlr].b_active = 0;
    701 			else {
    702 				if ((du->dk_flags & DKFL_QUIET) == 0) {
    703 					diskerr(bp, "wd", "hard error",
    704 					    LOG_PRINTF, du->dk_skip,
    705 					    &du->dk_dd);
    706 #ifdef WDDEBUG
    707 					printf("stat %b error %b\n", stat,
    708 					    WDCS_BITS, inb(wdc+wd_error),
    709 					    WDERR_BITS);
    710 #endif
    711 				}
    712 				bp->b_error = EIO;
    713 				bp->b_flags |= B_ERROR;	/* flag the error */
    714 			}
    715 		} else if ((du->dk_flags & DKFL_QUIET) == 0)
    716 			diskerr(bp, "wd", "soft ecc", 0, du->dk_skip,
    717 			    &du->dk_dd);
    718 	}
    719 
    720 	/*
    721 	 * If this was a successful read operation, fetch the data.
    722 	 */
    723 	if (((bp->b_flags & (B_READ | B_ERROR)) == B_READ) &&
    724 	    wdtab[ctrlr].b_active) {
    725 		int chk;
    726 
    727 		chk = min(DEV_BSIZE / sizeof(short), du->dk_bc / sizeof(short));
    728 
    729 		/* ready to receive data? */
    730 		if (wait_for_drq(du) < 0) {
    731 			printf("wdc%d: timeout waiting for drq\n", ctrlr);
    732 			goto lose;
    733 		}
    734 
    735 		/* suck in data */
    736 		insw(wdc+wd_data,
    737 		    (int)bp->b_un.b_addr + du->dk_skip * DEV_BSIZE, chk);
    738 		du->dk_bc -= chk * sizeof(short);
    739 		du->dk_bct -= chk * sizeof(short);
    740 
    741 		/* for obselete fractional sector reads */
    742 		while (chk++ < (DEV_BSIZE / sizeof(short)))
    743 			(void) inw(wdc+wd_data);
    744 	}
    745 
    746 	wdxfer[du->dk_lunit]++;
    747 outt:
    748 	if (wdtab[ctrlr].b_active) {
    749 #ifdef INSTRUMENT
    750 		if (du->dk_unit >= 0)
    751 			dk_busy &= ~(1 << du->dk_unit);
    752 #endif
    753 		if ((bp->b_flags & B_ERROR) == 0) {
    754 			du->dk_skip++;	/* Add to succ. sect */
    755 			du->dk_skipm++;	/* Add to succ. sect for multitransfer */
    756 			if (wdtab[ctrlr].b_errcnt &&
    757 			    (du->dk_flags & DKFL_QUIET) == 0)
    758 				diskerr(bp, "wd", "soft error", 0, du->dk_skip,
    759 				    &du->dk_dd);
    760 			wdtab[ctrlr].b_errcnt = 0;
    761 
    762 			/* see if more to transfer */
    763 			if (du->dk_bc > 0 && (du->dk_flags & DKFL_ERROR) == 0) {
    764 				wdtab[ctrlr].b_active = 0;
    765 				wdstart(ctrlr);
    766 				return;	/* next chunk is started */
    767 			} else if ((du->dk_flags & (DKFL_SINGLE | DKFL_ERROR)) == DKFL_ERROR) {
    768 				du->dk_skip = 0;
    769 				du->dk_skipm = 0;
    770 				du->dk_flags &= ~DKFL_ERROR;
    771 				du->dk_flags |=  DKFL_SINGLE;
    772 				wdtab[ctrlr].b_active = 0;
    773 				wdstart(ctrlr);
    774 				return;	/* redo xfer sector by sector */
    775 			}
    776 		}
    777 
    778 done:
    779 		/* done with this transfer, with or without error */
    780 		du->dk_flags &= ~DKFL_SINGLE;
    781 		wdtab[ctrlr].b_errcnt = 0;
    782 		if (du->dk_bct == 0) {
    783 			wdtab[ctrlr].b_forw = dp->b_forw;
    784 			du->dk_skipm = 0;
    785 			dp->b_active = 0;
    786 		}
    787 		bp->b_resid = bp->b_bcount - du->dk_skip * DEV_BSIZE;
    788 		bp->b_flags &= ~B_XXX;
    789 		du->dk_skip = 0;
    790 		dp->b_actf = bp->b_actf;
    791 		dp->b_errcnt = 0;
    792 		biodone(bp);
    793 	}
    794 
    795 	/* controller now idle */
    796 	wdtab[ctrlr].b_active = 0;
    797 
    798 	/* anything more on drive queue? */
    799 	if (dp->b_actf && du->dk_bct == 0)
    800 		wdustart(du);
    801 
    802 	/* anything more for controller to do? */
    803 	if (wdtab[ctrlr].b_forw)
    804 		wdstart(ctrlr);
    805 }
    806 
    807 /*
    808  * Initialize a drive.
    809  */
    810 int
    811 wdopen(dev_t dev, int flags, int fmt, struct proc *p)
    812 {
    813 	register unsigned int lunit;
    814 	register struct disk *du;
    815 	int part = WDPART(dev), mask = 1 << part;
    816 	struct partition *pp;
    817 	char *msg;
    818 
    819 	lunit = WDUNIT(dev);
    820 	if (lunit >= NWD)
    821 		return ENXIO;
    822 	du = wddrives[lunit];
    823 	if (du == 0)
    824 		return ENXIO;
    825 
    826 #ifdef QUIETWORKS
    827 	if (part == WDRAW)
    828 		du->dk_flags |= DKFL_QUIET;
    829 	else
    830 		du->dk_flags &= ~DKFL_QUIET;
    831 #else
    832 	du->dk_flags &= ~DKFL_QUIET;
    833 #endif
    834 
    835 	if ((du->dk_flags & DKFL_BSDLABEL) == 0) {
    836 		du->dk_flags |= DKFL_WRITEPROT;
    837 		wdutab[lunit].b_actf = NULL;
    838 
    839 		/*
    840 		 * Use the default sizes until we've read the label,
    841 		 * or longer if there isn't one there.
    842 		 */
    843 		bzero(&du->dk_dd, sizeof(du->dk_dd));
    844 		du->dk_dd.d_type = DTYPE_ST506;
    845 		du->dk_dd.d_ncylinders = 1024;
    846 		du->dk_dd.d_secsize = DEV_BSIZE;
    847 		du->dk_dd.d_ntracks = 8;
    848 		du->dk_dd.d_nsectors = 17;
    849 		du->dk_dd.d_secpercyl = 17*8;
    850 		du->dk_dd.d_secperunit = 17*8*1024;
    851 		du->dk_state = WANTOPEN;
    852 
    853 		/* read label using "raw" partition */
    854 #if defined(TIHMODS) && defined(garbage)
    855 		/* wdsetctlr(dev, du); */	/* Maybe do this TIH */
    856 		msg = readdisklabel(makewddev(major(dev), WDUNIT(dev), WDRAW),
    857 		    wdstrategy, &du->dk_dd, &du->dk_cpd);
    858 		wdsetctlr(dev, du);
    859 #endif
    860 		if (msg = readdisklabel(makewddev(major(dev), WDUNIT(dev),
    861 		    WDRAW), wdstrategy, &du->dk_dd, &du->dk_cpd)) {
    862 			if ((du->dk_flags & DKFL_QUIET) == 0) {
    863 				log(LOG_WARNING,
    864 				    "wd%d: cannot find label (%s)\n",
    865 				    lunit, msg);
    866 				return EINVAL;	/* XXX needs translation */
    867 			}
    868 		} else {
    869 			wdsetctlr(dev, du);
    870 			du->dk_flags |= DKFL_BSDLABEL;
    871 			du->dk_flags &= ~DKFL_WRITEPROT;
    872 			if (du->dk_dd.d_flags & D_BADSECT)
    873 				du->dk_flags |= DKFL_BADSECT;
    874 		}
    875 	}
    876 
    877 	if (du->dk_flags & DKFL_BADSECT)
    878 		bad144intern(du);
    879 
    880 	/*
    881 	 * Warn if a partion is opened
    882 	 * that overlaps another partition which is open
    883 	 * unless one is the "raw" partition (whole disk).
    884 	 */
    885 	if ((du->dk_openpart & mask) == 0 && part != WDRAW) {
    886 		int start, end;
    887 
    888 		pp = &du->dk_dd.d_partitions[part];
    889 		start = pp->p_offset;
    890 		end = pp->p_offset + pp->p_size;
    891 		for (pp = du->dk_dd.d_partitions;
    892 		    pp < &du->dk_dd.d_partitions[du->dk_dd.d_npartitions];
    893 		    pp++) {
    894 			if (pp->p_offset + pp->p_size <= start ||
    895 			    pp->p_offset >= end)
    896 				continue;
    897 			if (pp - du->dk_dd.d_partitions == WDRAW)
    898 				continue;
    899 			if (du->dk_openpart & (1 << (pp - du->dk_dd.d_partitions)))
    900 				log(LOG_WARNING,
    901 				    "wd%d%c: overlaps open partition (%c)\n",
    902 				    lunit, part + 'a',
    903 				    pp - du->dk_dd.d_partitions + 'a');
    904 		}
    905 	}
    906 	if (part >= du->dk_dd.d_npartitions && part != WDRAW)
    907 		return ENXIO;
    908 
    909 	/* insure only one open at a time */
    910 	du->dk_openpart |= mask;
    911 	switch (fmt) {
    912 	case S_IFCHR:
    913 		du->dk_copenpart |= mask;
    914 		break;
    915 	case S_IFBLK:
    916 		du->dk_bopenpart |= mask;
    917 		break;
    918 	}
    919 	return 0;
    920 }
    921 
    922 /*
    923  * Implement operations other than read/write.
    924  * Called from wdstart or wdintr during opens and formats.
    925  * Uses finite-state-machine to track progress of operation in progress.
    926  * Returns 0 if operation still in progress, 1 if completed.
    927  */
    928 static int
    929 wdcontrol(register struct buf *bp)
    930 {
    931 	register struct disk *du;
    932 	register unit, lunit;
    933 	unsigned char  stat;
    934 	int s, ctrlr;
    935 	int wdc;
    936 
    937 	du = wddrives[WDUNIT(bp->b_dev)];
    938 	ctrlr = du->dk_ctrlr;
    939 	unit = du->dk_unit;
    940 	lunit = du->dk_lunit;
    941 	wdc = du->dk_port;
    942 
    943 	switch (du->dk_state) {
    944 	case WANTOPEN:			/* set SDH, step rate, do restore */
    945 	tryagainrecal:
    946 #ifdef WDDEBUG
    947 		printf("wd%d: recal ", lunit);
    948 #endif
    949 		s = splbio();		/* not called from intr level ... */
    950 		wdgetctlr(unit, du);
    951 
    952 		if (wait_for_unbusy(du) < 0) {
    953 			wdreset(du, 1);
    954 			goto tryagainrecal;
    955 		}
    956 
    957 		outb(wdc+wd_sdh, WDSD_IBM | (unit << 4));
    958 		if (wait_for_ready(du) < 0) {
    959 			wdreset(du, 1);
    960 			goto tryagainrecal;
    961 		}
    962 
    963 		wdtab[ctrlr].b_active = 1;
    964 		if (wdcommand(du, WDCC_RESTORE | WD_STEP) < 0) {
    965 			wdreset(du, 1);
    966 			goto tryagainrecal;
    967 		}
    968 		du->dk_state = RECAL;
    969 		splx(s);
    970 		return 0;
    971 	case RECAL:
    972 		if ((stat = inb(wdc+wd_altsts)) & WDCS_ERR) {
    973 			if ((du->dk_flags & DKFL_QUIET) == 0) {
    974 				printf("wd%d: recal", du->dk_lunit);
    975 				printf(": status %b error %b\n", stat,
    976 				    WDCS_BITS, inb(wdc+wd_error), WDERR_BITS);
    977 			}
    978 			if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
    979 				goto tryagainrecal;
    980 			bp->b_error = ENXIO;	/* XXX needs translation */
    981 			goto badopen;
    982 		}
    983 
    984 		/* some controllers require this ... */
    985 		wdsetctlr(bp->b_dev, du);
    986 
    987 		wdtab[ctrlr].b_errcnt = 0;
    988 		du->dk_state = OPEN;
    989 		/*
    990 		 * The rest of the initialization can be done
    991 		 * by normal means.
    992 		 */
    993 		return 1;
    994 	default:
    995 		panic("wdcontrol");
    996 	}
    997 	/* NOTREACHED */
    998 
    999 badopen:
   1000 	if ((du->dk_flags & DKFL_QUIET) == 0)
   1001 		printf(": status %b error %b\n", stat, WDCS_BITS,
   1002 		    inb(wdc+wd_error), WDERR_BITS);
   1003 	bp->b_flags |= B_ERROR;
   1004 	return 1;
   1005 }
   1006 
   1007 /*
   1008  * send a command and wait uninterruptibly until controller is finished.
   1009  * return -1 if controller busy for too long, otherwise
   1010  * return status. intended for brief controller commands at critical points.
   1011  * assumes interrupts are blocked.
   1012  */
   1013 static int
   1014 wdcommand(struct disk *du, int cmd)
   1015 {
   1016 	int timeout, stat, wdc;
   1017 
   1018 	/*DELAY(2000);*/
   1019 	wdc = du->dk_port;
   1020 
   1021 	/* controller ready for command? */
   1022 	if (wait_for_unbusy(du) < 0)
   1023 		return -1;
   1024 
   1025 	/* send command, await results */
   1026 	outb(wdc+wd_command, cmd);
   1027 
   1028 	switch (cmd) {
   1029 	default:
   1030 #if 0
   1031 	case WDCC_FORMAT:
   1032 	case WDCC_RESTORE | WD_STEP:
   1033 	case WDCC_IDC:
   1034 	case WDCC_DIAGNOSE:
   1035 #endif
   1036 		return wait_for_unbusy(du);
   1037 	case WDCC_READ:
   1038 	case WDCC_WRITE:
   1039 		return 0;
   1040 	case WDCC_READP:
   1041 		return wait_for_drq(du);
   1042 	}
   1043 }
   1044 
   1045 /*
   1046  * issue IDC to drive to tell it just what geometry it is to be.
   1047  */
   1048 static int
   1049 wdsetctlr(dev_t dev, struct disk *du)
   1050 {
   1051 	int stat, x, wdc;
   1052 
   1053 #ifdef WDDEBUG
   1054 	printf("wd(%d,%d) C%dH%dS%d\n", du->dk_ctrlr, du->dk_unit,
   1055 	    du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks, du->dk_dd.d_nsectors);
   1056 #endif
   1057 
   1058 	wdc = du->dk_port;
   1059 
   1060 	x = splbio();
   1061 	if (wait_for_unbusy(du) < 0)
   1062 		return -1;
   1063 	outb(wdc+wd_cyl_lo, du->dk_dd.d_ncylinders);	/* TIH: was ...ders+1 */
   1064 	outb(wdc+wd_cyl_hi, du->dk_dd.d_ncylinders>>8);	/* TIH: was ...ders+1 */
   1065 	outb(wdc+wd_sdh,
   1066 	    WDSD_IBM | (du->dk_unit << 4) + du->dk_dd.d_ntracks - 1);
   1067 	outb(wdc+wd_seccnt, du->dk_dd.d_nsectors);
   1068 	if (wait_for_ready(du) < 0)
   1069 		return -1;
   1070 	stat = wdcommand(du, WDCC_IDC);
   1071 	if (stat < 0 || stat & WDCS_ERR)
   1072 		printf("wdsetctlr: stat %b error %b\n", stat, WDCS_BITS,
   1073 		    inb(wdc+wd_error), WDERR_BITS);
   1074 	splx(x);
   1075 	return stat;
   1076 }
   1077 
   1078 /*
   1079  * issue READP to drive to ask it what it is.
   1080  */
   1081 static int
   1082 wdgetctlr(int u, struct disk *du)
   1083 {
   1084 	int stat, x, i, wdc;
   1085 	char tb[DEV_BSIZE];
   1086 	struct wdparams *wp;
   1087 
   1088 	x = splbio();		/* not called from intr level ... */
   1089 	wdc = du->dk_port;
   1090 	if (wait_for_unbusy(du) < 0) {
   1091 		splx(x);
   1092 		return -1;
   1093 	}
   1094 
   1095 	outb(wdc+wd_sdh, WDSD_IBM | (u << 4));
   1096 	if (wait_for_ready(du) < 0) {
   1097 		splx(x);
   1098 		return -1;
   1099 	}
   1100 
   1101 	stat = wdcommand(du, WDCC_READP);
   1102 	if (stat < 0) {
   1103 		splx(x);
   1104 		return -1;
   1105 	}
   1106 
   1107 	if ((stat & WDCS_ERR) == 0) {
   1108 		/* obtain parameters */
   1109 		wp = &du->dk_params;
   1110 		insw(wdc+wd_data, tb, sizeof(tb)/sizeof(short));
   1111 		bcopy(tb, wp, sizeof(struct wdparams));
   1112 
   1113 		/* shuffle string byte order */
   1114 		for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
   1115 			u_short *p;
   1116 			p = (u_short *)(wp->wdp_model + i);
   1117 			*p = ntohs(*p);
   1118 		}
   1119 
   1120 		strncpy(du->dk_dd.d_typename, "ESDI/IDE",
   1121 		    sizeof du->dk_dd.d_typename);
   1122 		du->dk_dd.d_type = DTYPE_ESDI;
   1123 		bcopy(wp->wdp_model+20, du->dk_dd.d_packname, 14-1);
   1124 
   1125 		/* update disklabel given drive information */
   1126 		du->dk_dd.d_ncylinders =
   1127 		    wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
   1128 		du->dk_dd.d_ntracks = wp->wdp_heads;
   1129 		du->dk_dd.d_nsectors = wp->wdp_sectors;
   1130 		du->dk_dd.d_secpercyl =
   1131 		    du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
   1132 		du->dk_dd.d_partitions[1].p_size =
   1133 		    du->dk_dd.d_secpercyl * wp->wdp_sectors;
   1134 		du->dk_dd.d_partitions[1].p_offset = 0;
   1135 	} else {
   1136 		/*
   1137 		 * If WDCC_READP fails then we might have an old drive
   1138 		 * so we try a seek to 0; if that passes then the
   1139 		 * drive is there but it's OLD AND KRUSTY.
   1140 		 */
   1141 		stat = wdcommand(du, WDCC_RESTORE | WD_STEP);
   1142 		if (stat < 0 || stat & WDCS_ERR) {
   1143 			splx(x);
   1144 			return inb(wdc+wd_error);
   1145 		}
   1146 
   1147 		strncpy(du->dk_dd.d_typename, "ST506",
   1148 		    sizeof du->dk_dd.d_typename);
   1149 		strncpy(du->dk_params.wdp_model, "Unknown Type",
   1150 		    sizeof du->dk_params.wdp_model);
   1151 		du->dk_dd.d_type = DTYPE_ST506;
   1152 	}
   1153 
   1154 #if 0
   1155 	printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
   1156 	    wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
   1157 	    wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
   1158 #endif
   1159 
   1160 	/* better ... */
   1161 	du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
   1162 
   1163 	/* XXX sometimes possibly needed */
   1164 	(void) inb(wdc+wd_status);
   1165 	splx(x);
   1166 	return 0;
   1167 }
   1168 
   1169 
   1170 /* ARGSUSED */
   1171 int
   1172 wdclose(dev_t dev, int flags, int fmt)
   1173 {
   1174 	register struct disk *du;
   1175 	int part = WDPART(dev), mask = 1 << part;
   1176 
   1177 	du = wddrives[WDUNIT(dev)];
   1178 
   1179 	/* insure only one open at a time */
   1180 	du->dk_openpart &= ~mask;
   1181 	switch (fmt) {
   1182 	case S_IFCHR:
   1183 		du->dk_copenpart &= ~mask;
   1184 		break;
   1185 	case S_IFBLK:
   1186 		du->dk_bopenpart &= ~mask;
   1187 		break;
   1188 	}
   1189 	return 0;
   1190 }
   1191 
   1192 int
   1193 wdioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p)
   1194 {
   1195 	int lunit = WDUNIT(dev);
   1196 	register struct disk *du;
   1197 	int error = 0;
   1198 	struct uio auio;
   1199 	struct iovec aiov;
   1200 
   1201 	du = wddrives[lunit];
   1202 
   1203 	switch (cmd) {
   1204 	case DIOCSBAD:
   1205 		if ((flag & FWRITE) == 0)
   1206 			error = EBADF;
   1207 		else {
   1208 			du->dk_cpd.bad = *(struct dkbad *)addr;
   1209 			bad144intern(du);
   1210 		}
   1211 		break;
   1212 
   1213 	case DIOCGDINFO:
   1214 		*(struct disklabel *)addr = du->dk_dd;
   1215 		break;
   1216 
   1217 	case DIOCGPART:
   1218 		((struct partinfo *)addr)->disklab = &du->dk_dd;
   1219 		((struct partinfo *)addr)->part =
   1220 		    &du->dk_dd.d_partitions[WDPART(dev)];
   1221 		break;
   1222 
   1223 	case DIOCSDINFO:
   1224 		if ((flag & FWRITE) == 0)
   1225 			error = EBADF;
   1226 		else {
   1227 			error = setdisklabel(&du->dk_dd,						    (struct disklabel *)addr,
   1228 			    /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart : */0,
   1229 			    &du->dk_cpd);
   1230 		}
   1231 		if (error == 0) {
   1232 			du->dk_flags |= DKFL_BSDLABEL;
   1233 			wdsetctlr(dev, du);
   1234 		}
   1235 		break;
   1236 
   1237 	case DIOCWLABEL:
   1238 		du->dk_flags &= ~DKFL_WRITEPROT;
   1239 		if ((flag & FWRITE) == 0)
   1240 			error = EBADF;
   1241 		else
   1242 			du->dk_wlabel = *(int *)addr;
   1243 		break;
   1244 
   1245 	case DIOCWDINFO:
   1246 		du->dk_flags &= ~DKFL_WRITEPROT;
   1247 		if ((flag & FWRITE) == 0)
   1248 			error = EBADF;
   1249 		else if ((error = setdisklabel(&du->dk_dd,
   1250 		    (struct disklabel *)addr,
   1251 		    /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart :*/0,
   1252 		    &du->dk_cpd)) == 0) {
   1253 			int wlab;
   1254 
   1255 			du->dk_flags |= DKFL_BSDLABEL;
   1256 			wdsetctlr(dev, du);
   1257 
   1258 			/* simulate opening partition 0 so write succeeds */
   1259 			du->dk_openpart |= (1 << 0);	    /* XXX */
   1260 			wlab = du->dk_wlabel;
   1261 			du->dk_wlabel = 1;
   1262 			error = writedisklabel(dev, wdstrategy, &du->dk_dd,				    &du->dk_cpd);
   1263 			du->dk_openpart = du->dk_copenpart | du->dk_bopenpart;
   1264 			du->dk_wlabel = wlab;
   1265 		}
   1266 		break;
   1267 
   1268 #ifdef notyet
   1269 	case DIOCGDINFOP:
   1270 		*(struct disklabel **)addr = &du->dk_dd;
   1271 		break;
   1272 
   1273 	case DIOCWFORMAT:
   1274 		if ((flag & FWRITE) == 0)
   1275 			error = EBADF;
   1276 		else {
   1277 			register struct format_op *fop;
   1278 
   1279 			fop = (struct format_op *)addr;
   1280 			aiov.iov_base = fop->df_buf;
   1281 			aiov.iov_len = fop->df_count;
   1282 			auio.uio_iov = &aiov;
   1283 			auio.uio_iovcnt = 1;
   1284 			auio.uio_resid = fop->df_count;
   1285 			auio.uio_segflg = 0;
   1286 			auio.uio_offset =
   1287 			    fop->df_startblk * du->dk_dd.d_secsize;
   1288 			error = physio(wdformat, &rwdbuf[lunit], dev, B_WRITE,
   1289 			    minphys, &auio);
   1290 			fop->df_count -= auio.uio_resid;
   1291 			fop->df_reg[0] = du->dk_status;
   1292 			fop->df_reg[1] = du->dk_error;
   1293 		}
   1294 		break;
   1295 #endif
   1296 
   1297 	default:
   1298 		error = ENOTTY;
   1299 		break;
   1300 	}
   1301 	return error;
   1302 }
   1303 
   1304 #ifdef B_FORMAT
   1305 int
   1306 wdformat(struct buf *bp)
   1307 {
   1308 
   1309 	bp->b_flags |= B_FORMAT;
   1310 	return wdstrategy(bp);
   1311 }
   1312 #endif
   1313 
   1314 int
   1315 wdsize(dev_t dev)
   1316 {
   1317 	int lunit = WDUNIT(dev), part = WDPART(dev);
   1318 	struct disk *du;
   1319 
   1320 	if (lunit >= NWD)
   1321 		return -1;
   1322 	du = wddrives[lunit];
   1323 	if (du == 0)
   1324 		return -1;
   1325 
   1326 	if (du->dk_state < OPEN || (du->dk_flags & DKFL_BSDLABEL) == 0) {
   1327 		int val;
   1328 		val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD,
   1329 		    S_IFBLK, 0);
   1330 		if (val != 0)
   1331 			return -1;
   1332 	}
   1333 
   1334 	if ((du->dk_flags & (DKFL_WRITEPROT | DKFL_BSDLABEL)) != DKFL_BSDLABEL)
   1335 		return -1;
   1336 
   1337 	return (int)du->dk_dd.d_partitions[part].p_size;
   1338 }
   1339 
   1340 extern	char *vmmap;	    /* poor name! */
   1341 
   1342 /* dump core after a system crash */
   1343 int
   1344 wddump(dev_t dev)
   1345 {
   1346 	register struct disk *du;	/* disk unit to do the IO */
   1347 	long num;			/* number of sectors to write */
   1348 	int ctrlr, lunit, part, wdc;
   1349 	long blkoff, blknum;
   1350 	long cylin, head, sector, stat;
   1351 	long secpertrk, secpercyl, nblocks, i;
   1352 	char *addr;
   1353 	static wddoingadump = 0;
   1354 	extern caddr_t CADDR1;
   1355 	extern struct pte *CMAP1;
   1356 
   1357 	addr = (char *)0;		/* starting address */
   1358 
   1359 #if DO_NOT_KNOW_HOW
   1360 	/* toss any characters present prior to dump, ie. non-blocking getc */
   1361 	while (cngetc())
   1362 		;
   1363 #endif
   1364 
   1365 	/* size of memory to dump */
   1366 	lunit = WDUNIT(dev);
   1367 	part = WDPART(dev);	/* file system */
   1368 	/* check for acceptable drive number */
   1369 	if (lunit >= NWD)
   1370 		return ENXIO;
   1371 	du = wddrives[lunit];
   1372 	/* was it ever initialized ? */
   1373 	if (du == 0 || du->dk_state < OPEN || du->dk_flags & DKFL_WRITEPROT)
   1374 		return ENXIO;
   1375 
   1376 	wdc = du->dk_port;
   1377 	ctrlr = du->dk_ctrlr;
   1378 
   1379 	/* Convert to disk sectors */
   1380 	num = ctob(physmem) / du->dk_dd.d_secsize;
   1381 
   1382 	/* check if controller active */
   1383 	/*if (wdtab[ctrlr].b_active)
   1384 		return EFAULT;*/
   1385 	if (wddoingadump)
   1386 		return EFAULT;
   1387 
   1388 	secpertrk = du->dk_dd.d_nsectors;
   1389 	secpercyl = du->dk_dd.d_secpercyl;
   1390 	nblocks = du->dk_dd.d_partitions[part].p_size;
   1391 	blkoff = du->dk_dd.d_partitions[part].p_offset;
   1392 
   1393 	/*printf("xunit %x, nblocks %d, dumplo %d num %d\n", part, nblocks,
   1394 	    dumplo, num);*/
   1395 	/* check transfer bounds against partition size */
   1396 	if (dumplo < 0 || dumplo + num > nblocks)
   1397 		return EINVAL;
   1398 
   1399 	/* mark controller active for if we panic during the dump */
   1400 	/*wdtab[ctrlr].b_active = 1;*/
   1401 	wddoingadump = 1;
   1402 	i = 200000000;
   1403 	if (wait_for_unbusy(du) < 0)
   1404 		return EIO;
   1405 	outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4));
   1406 	if (wait_for_ready(du) < 0)
   1407 		return EIO;
   1408 	if (wdcommand(du, WDCC_RESTORE | WD_STEP) < 0)
   1409 		return EIO;
   1410 
   1411 	/* some compaq controllers require this ... */
   1412 	wdsetctlr(dev, du);
   1413 
   1414 	blknum = dumplo + blkoff;
   1415 	while (num > 0) {
   1416 		/* compute disk address */
   1417 		cylin = blknum / secpercyl;
   1418 		head = (blknum % secpercyl) / secpertrk;
   1419 		sector = blknum % secpertrk;
   1420 
   1421 		if (du->dk_flags & DKFL_BADSECT) {
   1422 			long newblk;
   1423 			int i;
   1424 
   1425 			for (i = 0; du->dk_badsect[i] != -1; i++) {
   1426 				if (blknum < du->dk_badsect[i]) {
   1427 					/* sorted list, passed our block by */
   1428 					break;
   1429 				} else if (blknum == du->dk_badsect[i]) {
   1430 					newblk = du->dk_dd.d_secperunit -
   1431 						du->dk_dd.d_nsectors - i - 1;
   1432 					cylin = newblk / secpercyl;
   1433 					head = (newblk % secpercyl) / secpertrk;
   1434 					sector = newblk % secpertrk;
   1435 					/* found and replaced; done */
   1436 					break;
   1437 				}
   1438 			}
   1439 		}
   1440 		sector++;		/* origin 1 */
   1441 
   1442 		if (wait_for_unbusy(du) < 0)
   1443 			return EIO;
   1444 		/* select drive.     */
   1445 		outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4) | (head & 0xf));
   1446 		if (wait_for_ready(du) < 0)
   1447 			return EIO;
   1448 
   1449 		/* transfer some blocks */
   1450 		outb(wdc+wd_sector, sector);
   1451 		outb(wdc+wd_seccnt, 1);
   1452 		outb(wdc+wd_cyl_lo, cylin);
   1453 		outb(wdc+wd_cyl_hi, cylin >> 8);
   1454 #ifdef notdef
   1455 		/* lets just talk about this first...*/
   1456 		printf("sdh 0%o sector %d cyl %d addr 0x%x", inb(wdc+wd_sdh),
   1457 		    inb(wdc+wd_sector),
   1458 		    (inb(wdc+wd_cyl_hi) << 8) + inb(wdc+wd_cyl_lo), addr);
   1459 #endif
   1460 		stat = wdcommand(du, WDCC_WRITE);
   1461 		if (stat < 0 || stat & WDCS_ERR)
   1462 			return EIO;
   1463 
   1464 #ifdef notdef	/* cannot use this since this address was mapped differently */
   1465 		pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
   1466 #else
   1467 		*(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
   1468 		tlbflush();
   1469 #endif
   1470 
   1471 		outsw(wdc+wd_data, CADDR1 + ((int)addr & PGOFSET),
   1472 		    DEV_BSIZE / sizeof(short));
   1473 
   1474 		/* Check data request (should be done). */
   1475 		if (inb(wdc+wd_status) & (WDCS_ERR | WDCS_DRQ))
   1476 			return EIO;
   1477 
   1478 		if (wait_for_unbusy(du) < 0)
   1479 			return EIO;
   1480 
   1481 		/* error check the xfer */
   1482 		if (inb(wdc+wd_status) & WDCS_ERR)
   1483 			return EIO;
   1484 
   1485 		if ((unsigned)addr % 1048576 == 0)
   1486 			printf("%d ", num / (1048576 / DEV_BSIZE));
   1487 
   1488 		/* update block count */
   1489 		num--;
   1490 		blknum++;
   1491 		(int)addr += DEV_BSIZE;
   1492 
   1493 #if DO_NOT_KNOW_HOW
   1494 		/* operator aborting dump? non-blocking getc() */
   1495 		if (cngetc())
   1496 			return EINTR;
   1497 #endif
   1498 	}
   1499 	return 0;
   1500 }
   1501 
   1502 /*
   1503  * Internalize the bad sector table.
   1504  */
   1505 void
   1506 bad144intern(struct disk *du)
   1507 {
   1508 	int i;
   1509 	if (du->dk_flags & DKFL_BADSECT) {
   1510 		for (i = 0; i < 127; i++)
   1511 			du->dk_badsect[i] = -1;
   1512 		for (i = 0; i < 126; i++) {
   1513 			if (du->dk_cpd.bad.bt_bad[i].bt_cyl == 0xffff)
   1514 				break;
   1515 			du->dk_badsect[i] =
   1516 			    du->dk_cpd.bad.bt_bad[i].bt_cyl *
   1517 				du->dk_dd.d_secpercyl +
   1518 			    (du->dk_cpd.bad.bt_bad[i].bt_trksec >> 8) *
   1519 				du->dk_dd.d_nsectors +
   1520 			    (du->dk_cpd.bad.bt_bad[i].bt_trksec & 0x00ff);
   1521 		}
   1522 	}
   1523 }
   1524 
   1525 static int
   1526 wdreset(struct disk *du, int err)
   1527 {
   1528 	int wdc = du->dk_port;
   1529 	int stat;
   1530 
   1531 	if (err)
   1532 		printf("wdc%d: busy too long, resetting\n", du->dk_ctrlr);
   1533 
   1534 	/* reset the device  */
   1535 	outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
   1536 	DELAY(1000);
   1537 	outb(wdc+wd_ctlr, WDCTL_4BIT);
   1538 
   1539 	if (wait_for_unbusy(du) < 0)
   1540 		printf("wdc%d: failed to reset controller\n", du->dk_ctrlr);
   1541 }
   1542 
   1543 int
   1544 wdc_wait(du, mask)
   1545 	struct disk *du;
   1546 	int mask;
   1547 {
   1548 	int wdc = du->dk_port;
   1549 	int timeout = 0;
   1550 	int stat;
   1551 
   1552 	for (;;) {
   1553 		stat = inb(wdc+wd_altsts);
   1554 		if ((stat & WDCS_BUSY) == 0)
   1555 			/* XXXX Yuck.  Need to redo this junk. */
   1556 			if (mask == 0 || (stat & mask) != 0)
   1557 				break;
   1558 		DELAY(WDCDELAY);
   1559 		if (++timeout > WDCNDELAY)
   1560 			return -1;
   1561 	}
   1562 #ifdef WDCNDELAY_DEBUG
   1563 	if (timeout > WDCNDELAY_DEBUG)
   1564 		printf("wdc%d: timeout took %dus\n", du->dk_ctrlr,
   1565 		    WDCDELAY * timeout);
   1566 #endif
   1567 	return stat;
   1568 }
   1569 
   1570 static int
   1571 wdtimeout(caddr_t arg)
   1572 {
   1573 	int x = splbio();
   1574 	register int unit = (int)arg;
   1575 
   1576 	if (wdtimeoutstatus[unit] && --wdtimeoutstatus[unit] == 0) {
   1577 		struct disk *du = wddrives[unit];
   1578 		int wdc = du->dk_port;
   1579 
   1580 		printf("wd%d: lost interrupt - status %x, error %x\n",
   1581 		    unit, inb(wdc+wd_status), inb(wdc+wd_error));
   1582 		outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
   1583 		DELAY(1000);
   1584 		outb(wdc+wd_ctlr, WDCTL_IDS);
   1585 		DELAY(1000);
   1586 		(void) inb(wdc+wd_error);
   1587 		outb(wdc+wd_ctlr, WDCTL_4BIT);
   1588 		du->dk_skip = 0;
   1589 		du->dk_flags |= DKFL_SINGLE;
   1590 		wdstart(du->dk_ctrlr);		/* start controller */
   1591 	}
   1592 	timeout((timeout_t)wdtimeout, (caddr_t)unit, hz*2);
   1593 	splx(x);
   1594 	return 0;
   1595 }
   1596 
   1597 #endif /* NWDC > 0 */
   1598