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