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