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