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