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