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