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