Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.42
      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.42 1994/02/25 18:17:30 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 #endif
    842 		if (msg = readdisklabel(makewddev(major(dev), wdunit(dev),
    843 		    WDRAW), wdstrategy, &du->dk_dd, &du->dk_cpd)) {
    844 			if ((du->dk_flags & DKFL_QUIET) == 0) {
    845 				log(LOG_WARNING,
    846 				    "wd%d: cannot find label (%s)\n",
    847 				    lunit, msg);
    848 				error = EINVAL;	/* XXX needs translation */
    849 			}
    850 			goto done;
    851 		} else {
    852 			wdsetctlr(dev, du);
    853 			du->dk_flags |= DKFL_BSDLABEL;
    854 			du->dk_flags &= ~DKFL_WRITEPROT;
    855 			if (du->dk_dd.d_flags & D_BADSECT)
    856 				du->dk_flags |= DKFL_BADSECT;
    857 		}
    858 
    859 done:
    860 		if (error)
    861 			return error;
    862 	}
    863 
    864 	if (du->dk_flags & DKFL_BADSECT)
    865 		bad144intern(du);
    866 
    867 	/*
    868 	 * Warn if a partion is opened
    869 	 * that overlaps another partition which is open
    870 	 * unless one is the "raw" partition (whole disk).
    871 	 */
    872 	if ((du->dk_openpart & mask) == 0 /*&& part != RAWPART*/ && part != WDRAW) {
    873 		int	start, end;
    874 
    875 		pp = &du->dk_dd.d_partitions[part];
    876 		start = pp->p_offset;
    877 		end = pp->p_offset + pp->p_size;
    878 		for (pp = du->dk_dd.d_partitions;
    879 		    pp < &du->dk_dd.d_partitions[du->dk_dd.d_npartitions]; pp++) {
    880 			if (pp->p_offset + pp->p_size <= start || pp->p_offset >= end)
    881 				continue;
    882 			/*if (pp - du->dk_dd.d_partitions == RAWPART)
    883 				continue; */
    884 			if (pp - du->dk_dd.d_partitions == WDRAW)
    885 				continue;
    886 			if (du->dk_openpart & (1 << (pp - du->dk_dd.d_partitions)))
    887 				log(LOG_WARNING,
    888 				    "wd%d%c: overlaps open partition (%c)\n",
    889 				    lunit, part + 'a',
    890 				    pp - du->dk_dd.d_partitions + 'a');
    891 		}
    892 	}
    893 
    894 	if (part >= du->dk_dd.d_npartitions && part != WDRAW)
    895 		return ENXIO;
    896 
    897 	/* insure only one open at a time */
    898 	du->dk_openpart |= mask;
    899 	switch (fmt) {
    900 	case S_IFCHR:
    901 		du->dk_copenpart |= mask;
    902 		break;
    903 	case S_IFBLK:
    904 		du->dk_bopenpart |= mask;
    905 		break;
    906 	}
    907 	return 0;
    908 }
    909 
    910 /*
    911  * Implement operations other than read/write.
    912  * Called from wdstart or wdintr during opens and formats.
    913  * Uses finite-state-machine to track progress of operation in progress.
    914  * Returns 0 if operation still in progress, 1 if completed.
    915  */
    916 static int
    917 wdcontrol(register struct buf *bp)
    918 {
    919 	register struct disk *du;
    920 	register unit, lunit;
    921 	unsigned char  stat;
    922 	int s, ctrlr;
    923 	int wdc;
    924 
    925 	du = wddrives[wdunit(bp->b_dev)];
    926 	ctrlr = du->dk_ctrlr;
    927 	unit = du->dk_unit;
    928 	lunit = du->dk_lunit;
    929 	wdc = du->dk_port;
    930 
    931 	switch (du->dk_state) {
    932 tryagainrecal:
    933 	case WANTOPEN:			/* set SDH, step rate, do restore */
    934 #ifdef	WDDEBUG
    935 		printf("wd%d: recal ", lunit);
    936 #endif
    937 		s = splbio();		/* not called from intr level ... */
    938 		wdgetctlr(unit, du);
    939 
    940 		if (wait_for_ready(wdc, ctrlr) == -1) {
    941 			wdreset(ctrlr, wdc, 1);
    942 			goto tryagainrecal;
    943 		}
    944 		outb(wdc+wd_sdh, WDSD_IBM | (unit << 4));
    945 		wdtab[ctrlr].b_active = 1;
    946 		outb(wdc+wd_command, WDCC_RESTORE | WD_STEP);
    947 		if (wait_for_ready(wdc, ctrlr) == -1) {
    948 			wdreset(ctrlr, wdc, 1);
    949 			goto tryagainrecal;
    950 		}
    951 		du->dk_state = RECAL;
    952 		splx(s);
    953 		return 0;
    954 	case RECAL:
    955 		if ((stat = inb(wdc+wd_status)) & WDCS_ERR) {
    956 			if ((du->dk_flags & DKFL_QUIET) == 0) {
    957 				printf("wd%d: recal", du->dk_lunit);
    958 				printf(": status %b error %b\n", stat,
    959 				    WDCS_BITS, inb(wdc+wd_error), WDERR_BITS);
    960 			}
    961 			if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
    962 				goto tryagainrecal;
    963 			bp->b_error = ENXIO;	/* XXX needs translation */
    964 			goto badopen;
    965 		}
    966 
    967 		/* some controllers require this ... */
    968 		wdsetctlr(bp->b_dev, du);
    969 
    970 		wdtab[ctrlr].b_errcnt = 0;
    971 		du->dk_state = OPEN;
    972 		/*
    973 		 * The rest of the initialization can be done
    974 		 * by normal means.
    975 		 */
    976 		return 1;
    977 	default:
    978 		panic("wdcontrol");
    979 	}
    980 	/* NOTREACHED */
    981 
    982 badopen:
    983 	if ((du->dk_flags & DKFL_QUIET) == 0)
    984 		printf(": status %b error %b\n",
    985 			stat, WDCS_BITS, inb(wdc + wd_error), WDERR_BITS);
    986 	bp->b_flags |= B_ERROR;
    987 	return 1;
    988 }
    989 
    990 /*
    991  * send a command and wait uninterruptibly until controller is finished.
    992  * return -1 if controller busy for too long, otherwise
    993  * return status. intended for brief controller commands at critical points.
    994  * assumes interrupts are blocked.
    995  */
    996 static int
    997 wdcommand(struct disk *du, int cmd)
    998 {
    999 	int timeout, stat, wdc;
   1000 
   1001 	/*DELAY(2000);*/
   1002 	wdc = du->dk_port;
   1003 
   1004 	/* controller ready for command? */
   1005 	if (wait_for_unbusy(wdc, du->dk_ctrlr) == -1)
   1006 		return -1;
   1007 
   1008 	/* send command, await results */
   1009 	outb(wdc+wd_command, cmd);
   1010 	stat = wait_for_unbusy(wdc, du->dk_ctrlr);
   1011 	if (stat == -1)
   1012 		return -1;
   1013 
   1014 	if (cmd != WDCC_READP)
   1015 		return stat;
   1016 
   1017 	/* is controller ready to return data? */
   1018 	return wdc_wait(wdc+wd_status, du->dk_ctrlr, 0, WDCS_ERR | WDCS_DRQ);
   1019 }
   1020 
   1021 /*
   1022  * issue IDC to drive to tell it just what geometry it is to be.
   1023  */
   1024 static int
   1025 wdsetctlr(dev_t dev, struct disk *du)
   1026 {
   1027 	int stat, x, wdc;
   1028 
   1029 /*
   1030 	printf("wd(%d,%d) C%dH%dS%d\n", du->dk_ctrlr, du->dk_unit,
   1031 	    du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks, du->dk_dd.d_nsectors);
   1032 */
   1033 
   1034 	wdc = du->dk_port;
   1035 
   1036 	/*DELAY(2000);*/
   1037 
   1038 	x = splbio();
   1039 	outb(wdc+wd_cyl_lo, du->dk_dd.d_ncylinders);	/* TIH: was ...ders+1 */
   1040 	outb(wdc+wd_cyl_hi, du->dk_dd.d_ncylinders>>8);	/* TIH: was ...ders+1 */
   1041 	outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4) + du->dk_dd.d_ntracks-1);
   1042 	outb(wdc+wd_seccnt, du->dk_dd.d_nsectors);
   1043 	stat = wdcommand(du, WDCC_IDC);
   1044 
   1045 	if (stat & WDCS_ERR)
   1046 		printf("wdsetctlr: status %b error %b\n", stat, WDCS_BITS,
   1047 		    inb(wdc+wd_error), WDERR_BITS);
   1048 	splx(x);
   1049 	return stat;
   1050 }
   1051 
   1052 /*
   1053  * issue READP to drive to ask it what it is.
   1054  */
   1055 static int
   1056 wdgetctlr(int u, struct disk *du)
   1057 {
   1058 	int stat, x, i, wdc;
   1059 	char tb[DEV_BSIZE];
   1060 	struct wdparams *wp;
   1061 
   1062 	x = splbio();		/* not called from intr level ... */
   1063 	wdc = du->dk_port;
   1064 	if (wait_for_unbusy(wdc, du->dk_ctrlr) == -1) {
   1065 		splx(x);
   1066 		return -1;
   1067 	}
   1068 
   1069 	outb(wdc+wd_sdh, WDSD_IBM | (u << 4));
   1070 	if (wait_for_ready(wdc, du->dk_ctrlr) == -1) {
   1071 		splx(x);
   1072 		return -1;
   1073 	}
   1074 
   1075 	stat = wdcommand(du, WDCC_READP);
   1076 	if (wait_for_ready(wdc, du->dk_ctrlr) == -1) {
   1077 		splx(x);
   1078 		return -1;
   1079 	}
   1080 
   1081 	if (stat < 0) {
   1082 		splx(x);
   1083 		return stat;
   1084 	}
   1085 
   1086 	if ((stat & WDCS_ERR) == 0) {
   1087 		/* obtain parameters */
   1088 		wp = &du->dk_params;
   1089 		insw(wdc+wd_data, tb, sizeof(tb)/sizeof(short));
   1090 		bcopy(tb, wp, sizeof(struct wdparams));
   1091 
   1092 		/* shuffle string byte order */
   1093 		for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
   1094 			u_short *p;
   1095 			p = (u_short *)(wp->wdp_model + i);
   1096 			*p = ntohs(*p);
   1097 		}
   1098 
   1099 		strncpy(du->dk_dd.d_typename, "ESDI/IDE",
   1100 		    sizeof du->dk_dd.d_typename);
   1101 		du->dk_dd.d_type = DTYPE_ESDI;
   1102 		bcopy(wp->wdp_model+20, du->dk_dd.d_packname, 14-1);
   1103 
   1104 		/* update disklabel given drive information */
   1105 		du->dk_dd.d_ncylinders = wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
   1106 		du->dk_dd.d_ntracks = wp->wdp_heads;
   1107 		du->dk_dd.d_nsectors = wp->wdp_sectors;
   1108 		du->dk_dd.d_secpercyl = du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
   1109 		du->dk_dd.d_partitions[1].p_size = du->dk_dd.d_secpercyl *
   1110 			wp->wdp_sectors;
   1111 		du->dk_dd.d_partitions[1].p_offset = 0;
   1112 	} else {
   1113 		/*
   1114 		 * If WDCC_READP fails then we might have an old drive
   1115 		 * so we try a seek to 0; if that passes then the
   1116 		 * drive is there but it's OLD AND KRUSTY.
   1117 		 */
   1118 		stat = wdcommand(du, WDCC_RESTORE | WD_STEP);
   1119 		if (stat & WDCS_ERR) {
   1120 			splx(x);
   1121 			return inb(wdc+wd_error);
   1122 		}
   1123 
   1124 		strncpy(du->dk_dd.d_typename, "ST506",
   1125 		    sizeof du->dk_dd.d_typename);
   1126 		strncpy(du->dk_params.wdp_model, "Unknown Type",
   1127 		    sizeof du->dk_params.wdp_model);
   1128 		du->dk_dd.d_type = DTYPE_ST506;
   1129 	}
   1130 
   1131 #if 0
   1132 	printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
   1133 	    wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
   1134 	    wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
   1135 #endif
   1136 
   1137 	/* better ... */
   1138 	du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
   1139 
   1140 	/* XXX sometimes possibly needed */
   1141 	(void) inb(wdc+wd_status);
   1142 	splx(x);
   1143 	return 0;
   1144 }
   1145 
   1146 
   1147 /* ARGSUSED */
   1148 int
   1149 wdclose(dev_t dev, int flags, int fmt)
   1150 {
   1151 	register struct disk *du;
   1152 	int part = wdpart(dev), mask = 1 << part;
   1153 
   1154 	du = wddrives[wdunit(dev)];
   1155 
   1156 	/* insure only one open at a time */
   1157 	du->dk_openpart &= ~mask;
   1158 	switch (fmt) {
   1159 	case S_IFCHR:
   1160 		du->dk_copenpart &= ~mask;
   1161 		break;
   1162 	case S_IFBLK:
   1163 		du->dk_bopenpart &= ~mask;
   1164 		break;
   1165 	}
   1166 	return 0;
   1167 }
   1168 
   1169 int
   1170 wdioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p)
   1171 {
   1172 	int lunit = wdunit(dev);
   1173 	register struct disk *du;
   1174 	int error = 0;
   1175 	struct uio auio;
   1176 	struct iovec aiov;
   1177 
   1178 	du = wddrives[lunit];
   1179 
   1180 	switch (cmd) {
   1181 	case DIOCSBAD:
   1182 		if ((flag & FWRITE) == 0)
   1183 			error = EBADF;
   1184 		else {
   1185 			du->dk_cpd.bad = *(struct dkbad *)addr;
   1186 			bad144intern(du);
   1187 		}
   1188 		break;
   1189 
   1190 	case DIOCGDINFO:
   1191 		*(struct disklabel *)addr = du->dk_dd;
   1192 		break;
   1193 
   1194 	case DIOCGPART:
   1195 		((struct partinfo *)addr)->disklab = &du->dk_dd;
   1196 		((struct partinfo *)addr)->part =
   1197 		    &du->dk_dd.d_partitions[wdpart(dev)];
   1198 		break;
   1199 
   1200 	case DIOCSDINFO:
   1201 		if ((flag & FWRITE) == 0)
   1202 			error = EBADF;
   1203 		else {
   1204 			error = setdisklabel(&du->dk_dd,						    (struct disklabel *)addr,
   1205 			    /*(du->dk_flags&DKFL_BSDLABEL) ? du->dk_openpart : */0,
   1206 			    &du->dk_cpd);
   1207 		}
   1208 		if (error == 0) {
   1209 			du->dk_flags |= DKFL_BSDLABEL;
   1210 			wdsetctlr(dev, du);
   1211 		}
   1212 		break;
   1213 
   1214     case DIOCWLABEL:
   1215 		du->dk_flags &= ~DKFL_WRITEPROT;
   1216 		if ((flag & FWRITE) == 0)
   1217 			error = EBADF;
   1218 		else
   1219 			du->dk_wlabel = *(int *)addr;
   1220 		break;
   1221 
   1222     case DIOCWDINFO:
   1223 		du->dk_flags &= ~DKFL_WRITEPROT;
   1224 		if ((flag & FWRITE) == 0)
   1225 			error = EBADF;
   1226 		else if ((error = setdisklabel(&du->dk_dd,
   1227 		    (struct disklabel *)addr,
   1228 		    /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart :*/ 0,
   1229 		    &du->dk_cpd)) == 0) {
   1230 			int wlab;
   1231 
   1232 			du->dk_flags |= DKFL_BSDLABEL;
   1233 			wdsetctlr(dev, du);
   1234 
   1235 			/* simulate opening partition 0 so write succeeds */
   1236 			du->dk_openpart |= (1 << 0);	    /* XXX */
   1237 			wlab = du->dk_wlabel;
   1238 			du->dk_wlabel = 1;
   1239 			error = writedisklabel(dev, wdstrategy, &du->dk_dd,				    &du->dk_cpd);
   1240 			du->dk_openpart = du->dk_copenpart | du->dk_bopenpart;
   1241 			du->dk_wlabel = wlab;
   1242 		}
   1243 		break;
   1244 
   1245 #ifdef notyet
   1246 	case DIOCGDINFOP:
   1247 		*(struct disklabel **)addr = &du->dk_dd;
   1248 		break;
   1249 
   1250 	case DIOCWFORMAT:
   1251 		if ((flag & FWRITE) == 0)
   1252 			error = EBADF;
   1253 		else {
   1254 			register struct format_op *fop;
   1255 
   1256 			fop = (struct format_op *)addr;
   1257 			aiov.iov_base = fop->df_buf;
   1258 			aiov.iov_len = fop->df_count;
   1259 			auio.uio_iov = &aiov;
   1260 			auio.uio_iovcnt = 1;
   1261 			auio.uio_resid = fop->df_count;
   1262 			auio.uio_segflg = 0;
   1263 			auio.uio_offset = fop->df_startblk * du->dk_dd.d_secsize;
   1264 			error = physio(wdformat, &rwdbuf[lunit], dev, B_WRITE,
   1265 			    minphys, &auio);
   1266 			fop->df_count -= auio.uio_resid;
   1267 			fop->df_reg[0] = du->dk_status;
   1268 			fop->df_reg[1] = du->dk_error;
   1269 		}
   1270 		break;
   1271 #endif
   1272 
   1273 	default:
   1274 		error = ENOTTY;
   1275 		break;
   1276 	}
   1277 	return error;
   1278 }
   1279 
   1280 #ifdef	B_FORMAT
   1281 int
   1282 wdformat(struct buf *bp)
   1283 {
   1284 	bp->b_flags |= B_FORMAT;
   1285 	return wdstrategy(bp);
   1286 }
   1287 #endif
   1288 
   1289 int
   1290 wdsize(dev_t dev)
   1291 {
   1292 	int lunit = wdunit(dev), part = wdpart(dev);
   1293 	struct disk *du;
   1294 
   1295 	if (lunit >= NWD)
   1296 		return -1;
   1297 
   1298 	if ((du = wddrives[lunit]) == 0)
   1299 		return -1;
   1300 
   1301 	if (du->dk_state < OPEN || (du->dk_flags & DKFL_BSDLABEL) == 0) {
   1302 		int val;
   1303 		val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD, S_IFBLK, 0);
   1304 		if (val != 0)
   1305 			return -1;
   1306 	}
   1307 
   1308 	if ((du->dk_flags & (DKFL_WRITEPROT | DKFL_BSDLABEL)) != DKFL_BSDLABEL)
   1309 		return -1;
   1310 	else
   1311 		return (int)du->dk_dd.d_partitions[part].p_size;
   1312 }
   1313 
   1314 extern	char *vmmap;	    /* poor name! */
   1315 
   1316 /* dump core after a system crash */
   1317 int
   1318 wddump(dev_t dev)
   1319 {
   1320 	register struct disk *du;	/* disk unit to do the IO */
   1321 	long	num;			/* number of sectors to write */
   1322 	int	ctrlr, lunit, part, wdc;
   1323 	long	blkoff, blknum;
   1324 	long	cylin, head, sector, stat;
   1325 	long	secpertrk, secpercyl, nblocks, i;
   1326 	char *addr;
   1327 	static  wddoingadump = 0;
   1328 	extern caddr_t CADDR1;
   1329 	extern struct pte *CMAP1;
   1330 
   1331 	addr = (char *)0;		/* starting address */
   1332 
   1333 #if DO_NOT_KNOW_HOW
   1334 	/* toss any characters present prior to dump, ie. non-blocking getc */
   1335 	while (cngetc())
   1336 		;
   1337 #endif
   1338 
   1339 	/* size of memory to dump */
   1340 	lunit = wdunit(dev);	/* eventually support floppies? */
   1341 	part = wdpart(dev);		/* file system */
   1342 	/* check for acceptable drive number */
   1343 	if (lunit >= NWD)
   1344 		return ENXIO;
   1345 
   1346 	du = wddrives[lunit];
   1347 	if (du == 0)
   1348 		return ENXIO;
   1349 	/* was it ever initialized ? */
   1350 	if (du->dk_state < OPEN)
   1351 		return ENXIO;
   1352 	if (du->dk_flags & DKFL_WRITEPROT)
   1353 		return ENXIO;
   1354 	wdc = du->dk_port;
   1355 	ctrlr = du->dk_ctrlr;
   1356 
   1357 	/* Convert to disk sectors */
   1358 	num = ctob(physmem) / du->dk_dd.d_secsize;
   1359 
   1360 	/* check if controller active */
   1361 	/*if (wdtab[ctrlr].b_active)
   1362 		return EFAULT; */
   1363 	if (wddoingadump)
   1364 		return EFAULT;
   1365 
   1366 	secpertrk = du->dk_dd.d_nsectors;
   1367 	secpercyl = du->dk_dd.d_secpercyl;
   1368 	nblocks = du->dk_dd.d_partitions[part].p_size;
   1369 	blkoff = du->dk_dd.d_partitions[part].p_offset;
   1370 
   1371 	/*pg("xunit %x, nblocks %d, dumplo %d num %d\n", part, nblocks, dumplo, num);*/
   1372 	/* check transfer bounds against partition size */
   1373 	if ((dumplo < 0) || ((dumplo + num) > nblocks))
   1374 		return EINVAL;
   1375 
   1376 	/* mark controller active for if we panic during the dump */
   1377 	/* wdtab[ctrlr].b_active = 1; */
   1378 	wddoingadump = 1;
   1379 	i = 200000000;
   1380 	while ((inb(wdc+wd_status) & WDCS_BUSY) && (i-- > 0))
   1381 		;
   1382 	outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4));
   1383 	outb(wdc+wd_command, WDCC_RESTORE | WD_STEP);
   1384 	while (inb(wdc+wd_status) & WDCS_BUSY)
   1385 		;
   1386 
   1387 	/* some compaq controllers require this ... */
   1388 	wdsetctlr(dev, du);
   1389 
   1390 	blknum = dumplo + blkoff;
   1391 	while (num > 0) {
   1392 #ifdef	__notdef__	/* cannot use this since this address was mapped differently */
   1393 		pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
   1394 #else
   1395 		*(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
   1396 		tlbflush();
   1397 #endif
   1398 
   1399 		/* compute disk address */
   1400 		cylin = blknum / secpercyl;
   1401 		head = (blknum % secpercyl) / secpertrk;
   1402 		sector = blknum % secpertrk;
   1403 
   1404 		if (du->dk_flags & DKFL_BADSECT) {
   1405 			long newblk;
   1406 			int i;
   1407 
   1408 			for (i = 0; du->dk_badsect[i] != -1; i++) {
   1409 				if (blknum < du->dk_badsect[i]) {
   1410 					break;	/* sorted list, passed our block by */
   1411 				} else if (blknum == du->dk_badsect[i]) {
   1412 					newblk = du->dk_dd.d_secperunit -
   1413 						du->dk_dd.d_nsectors - i - 1;
   1414 					cylin = newblk / secpercyl;
   1415 					head = (newblk % secpercyl) / secpertrk;
   1416 					sector = newblk % secpertrk;
   1417 					/* found and repl; done scanning bad144 table */
   1418 					break;
   1419 				}
   1420 			}
   1421 		}
   1422 		sector++;		/* origin 1 */
   1423 
   1424 		/* select drive.     */
   1425 		outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4) | (head & 0xf));
   1426 		while ((inb(wdc+wd_status) & WDCS_READY) == 0)
   1427 			;
   1428 
   1429 		/* transfer some blocks */
   1430 		outb(wdc+wd_sector, sector);
   1431 		outb(wdc+wd_seccnt, 1);
   1432 		outb(wdc+wd_cyl_lo, cylin);
   1433 		outb(wdc+wd_cyl_hi, cylin >> 8);
   1434 #ifdef notdef
   1435 		/* lets just talk about this first...*/
   1436 		pg("sdh 0%o sector %d cyl %d addr 0x%x",
   1437 			inb(wdc+wd_sdh), inb(wdc+wd_sector),
   1438 			(inb(wdc+wd_cyl_hi) << 8) + inb(wdc+wd_cyl_lo), addr);
   1439 #endif
   1440 		outb(wdc+wd_command, WDCC_WRITE);
   1441 
   1442 		/* Ready to send data?	*/
   1443 		while ((inb(wdc+wd_status) & WDCS_DRQ) == 0)
   1444 			;
   1445 		if (inb(wdc+wd_status) & WDCS_ERR)
   1446 			return EIO;
   1447 
   1448 		outsw(wdc+wd_data, CADDR1 + ((int)addr&PGOFSET), 256);
   1449 
   1450 		if (inb(wdc+wd_status) & WDCS_ERR)
   1451 			return EIO;
   1452 		/* Check data request (should be done). */
   1453 		if (inb(wdc+wd_status) & WDCS_DRQ)
   1454 			return EIO;
   1455 
   1456 		/* wait for completion */
   1457 		for (i = 200000000; inb(wdc+wd_status) & WDCS_BUSY; i--) {
   1458 			if (i < 0)
   1459 				return EIO;
   1460 		}
   1461 
   1462 		/* error check the xfer */
   1463 		if (inb(wdc+wd_status) & WDCS_ERR)
   1464 			return EIO;
   1465 
   1466 		if ((unsigned)addr % (1024*1024) == 0)
   1467 			printf("%d ", num/2048);
   1468 
   1469 		/* update block count */
   1470 		num--;
   1471 		blknum++;
   1472 		(int)addr += 512;
   1473 
   1474 #if DO_NOT_KNOW_HOW
   1475 		/* operator aborting dump? non-blocking getc() */
   1476 		if (cngetc())
   1477 			return EINTR;
   1478 #endif
   1479 	}
   1480 	return 0;
   1481 }
   1482 #endif
   1483 
   1484 /*
   1485  * Internalize the bad sector table.
   1486  */
   1487 void
   1488 bad144intern(struct disk *du)
   1489 {
   1490 	int i;
   1491 	if (du->dk_flags & DKFL_BADSECT) {
   1492 		for (i = 0; i < 127; i++)
   1493 			du->dk_badsect[i] = -1;
   1494 		for (i = 0; i < 126; i++) {
   1495 			if (du->dk_cpd.bad.bt_bad[i].bt_cyl == 0xffff)
   1496 				break;
   1497 			du->dk_badsect[i] =
   1498 			    du->dk_cpd.bad.bt_bad[i].bt_cyl *
   1499 				du->dk_dd.d_secpercyl +
   1500 			    (du->dk_cpd.bad.bt_bad[i].bt_trksec >> 8) *
   1501 				du->dk_dd.d_nsectors +
   1502 			    (du->dk_cpd.bad.bt_bad[i].bt_trksec & 0x00ff);
   1503 		}
   1504 	}
   1505 }
   1506 
   1507 static int
   1508 wdreset(int ctrlr, int wdc, int err)
   1509 {
   1510 	int stat;
   1511 
   1512 	if (err)
   1513 		printf("wdc%d: busy too long, resetting\n", ctrlr);
   1514 
   1515 	/* reset the device  */
   1516 	outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
   1517 	DELAY(1000);
   1518 	outb(wdc+wd_ctlr, WDCTL_4BIT);
   1519 
   1520 	if (wait_for_unbusy(wdc, ctrlr) == -1)
   1521 		printf("wdc%d: failed to reset controller\n", ctrlr);
   1522 }
   1523 
   1524 int
   1525 wdc_wait(port, ctrlr, on, off)
   1526 	int port, ctrlr;
   1527 	int on, off;
   1528 {
   1529 	int timeout = 0;
   1530 	int stat;
   1531 
   1532 	for (;;) {
   1533 		stat = inb(port);
   1534 		if ((stat & on) != on || (stat & off) != 0)
   1535 			break;
   1536 		DELAY(WDCDELAY);
   1537 		if (++timeout > WDCNDELAY)
   1538 			return -1;
   1539 	}
   1540 #ifdef WDCNDELAY_DEBUG
   1541 	if (timeout > WDCNDELAY_DEBUG)
   1542 		printf("wdc%d: timeout took %dus\n", ctrlr, WDCDELAY * timeout);
   1543 #endif
   1544 	return stat;
   1545 }
   1546 
   1547 static int
   1548 wdtimeout(caddr_t arg)
   1549 {
   1550 	int x = splbio();
   1551 	register int unit = (int)arg;
   1552 
   1553 	if (wdtimeoutstatus[unit] && --wdtimeoutstatus[unit] == 0) {
   1554 		struct disk *du = wddrives[unit];
   1555 		int wdc = du->dk_port;
   1556 
   1557 		printf("wd%d: lost interrupt - status %x, error %x\n",
   1558 		    unit, inb(wdc+wd_status), inb(wdc+wd_error));
   1559 		outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
   1560 		DELAY(1000);
   1561 		outb(wdc+wd_ctlr, WDCTL_IDS);
   1562 		DELAY(1000);
   1563 		(void) inb(wdc+wd_error);
   1564 		outb(wdc+wd_ctlr, WDCTL_4BIT);
   1565 		du->dk_skip = 0;
   1566 		du->dk_flags |= DKFL_SINGLE;
   1567 		wdstart(du->dk_ctrlr);		/* start controller */
   1568 	}
   1569 	timeout((timeout_t)wdtimeout, (caddr_t)unit, hz/2);
   1570 	splx(x);
   1571 	return 0;
   1572 }
   1573