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