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