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