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