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