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