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