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