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