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