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