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