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