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