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