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