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