Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.8
      1 /*	$NetBSD: fd.c,v 1.8 1995/05/20 20:03:37 pk Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
      5  * Copyright (c) 1995 Paul Kranenburg.
      6  * Copyright (c) 1990 The Regents of the University of California.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * Don Ahn.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/conf.h>
     47 #include <sys/file.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/device.h>
     50 #include <sys/disklabel.h>
     51 #include <sys/dkstat.h>
     52 #include <sys/disk.h>
     53 #include <sys/buf.h>
     54 #include <sys/uio.h>
     55 #include <sys/syslog.h>
     56 #include <sys/queue.h>
     57 
     58 #include <machine/cpu.h>
     59 #include <machine/autoconf.h>
     60 #include <sparc/sparc/auxreg.h>
     61 #include <sparc/dev/fdreg.h>
     62 #include <sparc/dev/fdvar.h>
     63 
     64 #define FDUNIT(dev)	(minor(dev) / 8)
     65 #define FDTYPE(dev)	(minor(dev) % 8)
     66 
     67 #define b_cylin b_resid
     68 
     69 #define FD_DEBUG
     70 #ifdef FD_DEBUG
     71 int	fdc_debug = 0;
     72 #endif
     73 
     74 enum fdc_state {
     75 	DEVIDLE = 0,
     76 	MOTORWAIT,
     77 	DOSEEK,
     78 	SEEKWAIT,
     79 	SEEKTIMEDOUT,
     80 	SEEKCOMPLETE,
     81 	DOIO,
     82 	IOCOMPLETE,
     83 	IOTIMEDOUT,
     84 	DORESET,
     85 	RESETCOMPLETE,
     86 	RESETTIMEDOUT,
     87 	DORECAL,
     88 	RECALWAIT,
     89 	RECALTIMEDOUT,
     90 	RECALCOMPLETE,
     91 };
     92 
     93 /* software state, per controller */
     94 struct fdc_softc {
     95 	struct dkdevice sc_dk;		/* boilerplate */
     96 	struct intrhand sc_sih;
     97 	struct intrhand sc_hih;
     98 	caddr_t		sc_reg;
     99 	struct fd_softc *sc_fd[4];	/* pointers to children */
    100 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    101 	enum fdc_state	sc_state;
    102 	int		sc_flags;
    103 #define FDC_82077		0x01
    104 #define FDC_NEEDHEADSETTLE	0x02
    105 #define FDC_EIS			0x04
    106 	int		sc_errors;		/* number of retries so far */
    107 	int		sc_overruns;		/* number of DMA overruns */
    108 	int		sc_cfg;			/* current configuration */
    109 	struct fdcio	sc_io;
    110 #define sc_reg_msr	sc_io.fdcio_reg_msr
    111 #define sc_reg_fifo	sc_io.fdcio_reg_fifo
    112 #define sc_reg_dor	sc_io.fdcio_reg_dor
    113 #define sc_reg_drs	sc_io.fdcio_reg_msr
    114 #define sc_istate	sc_io.fdcio_istate
    115 #define sc_data		sc_io.fdcio_data
    116 #define sc_tc		sc_io.fdcio_tc
    117 #define sc_nstat	sc_io.fdcio_nstat
    118 #define sc_status	sc_io.fdcio_status
    119 #define sc_intrcnt	sc_io.fdcio_intrcnt
    120 };
    121 
    122 #ifndef FDC_C_HANDLER
    123 extern	struct fdcio	*fdciop;
    124 #endif
    125 
    126 /* controller driver configuration */
    127 int	fdcmatch __P((struct device *, void *, void *));
    128 void	fdcattach __P((struct device *, struct device *, void *));
    129 
    130 struct cfdriver fdccd = {
    131 	NULL, "fdc", fdcmatch, fdcattach, DV_DULL, sizeof(struct fdc_softc)
    132 };
    133 
    134 /*
    135  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    136  * we tell them apart.
    137  */
    138 struct fd_type {
    139 	int	sectrac;	/* sectors per track */
    140 	int	heads;		/* number of heads */
    141 	int	seccyl;		/* sectors per cylinder */
    142 	int	secsize;	/* size code for sectors */
    143 	int	datalen;	/* data len when secsize = 0 */
    144 	int	steprate;	/* step rate and head unload time */
    145 	int	gap1;		/* gap len between sectors */
    146 	int	gap2;		/* formatting gap */
    147 	int	tracks;		/* total num of tracks */
    148 	int	size;		/* size of disk in sectors */
    149 	int	step;		/* steps per cylinder */
    150 	int	rate;		/* transfer speed code */
    151 	char	*name;
    152 };
    153 
    154 /* The order of entries in the following table is important -- BEWARE! */
    155 struct fd_type fd_types[] = {
    156 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    }, /* 1.44MB diskette */
    157 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,"1.2MB"    }, /* 1.2 MB AT-diskettes */
    158 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,"360KB/AT" }, /* 360kB in 1.2MB drive */
    159 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,"360KB/PC" }, /* 360kB PC diskettes */
    160 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,"720KB"    }, /* 3.5" 720kB diskette */
    161 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,"720KB/x"  }, /* 720kB in 1.2MB drive */
    162 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,"360KB/x"  }, /* 360kB in 720kB drive */
    163 };
    164 
    165 /* software state, per disk (with up to 4 disks per ctlr) */
    166 struct fd_softc {
    167 	struct dkdevice sc_dk;
    168 
    169 	struct fd_type *sc_deftype;	/* default type descriptor */
    170 	struct fd_type *sc_type;	/* current type descriptor */
    171 
    172 	daddr_t	sc_blkno;	/* starting block number */
    173 	int sc_bcount;		/* byte count left */
    174 	int sc_skip;		/* bytes already transferred */
    175 	int sc_nblks;		/* number of blocks currently tranferring */
    176 	int sc_nbytes;		/* number of bytes currently tranferring */
    177 
    178 	int sc_drive;		/* physical unit number */
    179 	int sc_flags;
    180 #define	FD_OPEN		0x01		/* it's open */
    181 #define	FD_MOTOR	0x02		/* motor should be on */
    182 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    183 	int sc_cylin;		/* where we think the head is */
    184 
    185 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    186 	int sc_ops;		/* I/O ops since last switch */
    187 	struct buf sc_q;	/* head of buf chain */
    188 };
    189 
    190 /* floppy driver configuration */
    191 int	fdmatch __P((struct device *, void *, void *));
    192 void	fdattach __P((struct device *, struct device *, void *));
    193 
    194 struct cfdriver fdcd = {
    195 	NULL, "fd", fdmatch, fdattach, DV_DISK, sizeof(struct fd_softc)
    196 };
    197 
    198 void fdgetdisklabel __P((struct fd_softc *));
    199 int fd_get_parms __P((struct fd_softc *));
    200 void fdstrategy __P((struct buf *));
    201 void fdstart __P((struct fd_softc *));
    202 
    203 struct dkdriver fddkdriver = { fdstrategy };
    204 
    205 struct	fd_type *fd_nvtotype __P((char *, int, int));
    206 void	fd_set_motor __P((struct fdc_softc *fdc, int reset));
    207 void	fd_motor_off __P((void *arg));
    208 void	fd_motor_on __P((void *arg));
    209 int	fdcresult __P((struct fdc_softc *fdc));
    210 int	out_fdc __P((struct fdc_softc *fdc, u_char x));
    211 void	fdcstart __P((struct fdc_softc *fdc));
    212 void	fdcstatus __P((struct device *dv, int n, char *s));
    213 void	fdctimeout __P((void *arg));
    214 void	fdcpseudointr __P((void *arg));
    215 #ifdef FDC_C_HANDLER
    216 int	fdchwintr __P((struct fdc_softc *));
    217 #else
    218 void	fdchwintr __P((void));
    219 #endif
    220 int	fdcswintr __P((struct fdc_softc *));
    221 void	fdcretry __P((struct fdc_softc *fdc));
    222 void	fdfinish __P((struct fd_softc *fd, struct buf *bp));
    223 
    224 #if PIL_FDSOFT == 4
    225 #define IE_FDSOFT	IE_L4
    226 #else
    227 #error 4
    228 #endif
    229 
    230 int
    231 fdcmatch(parent, match, aux)
    232 	struct device *parent;
    233 	void *match, *aux;
    234 {
    235 	struct cfdata *cf = match;
    236 	register struct confargs *ca = aux;
    237 	register struct romaux *ra = &ca->ca_ra;
    238 
    239 	/* Sun PROMs call the controller an "fd" */
    240 	if (strcmp("fd", ra->ra_name))
    241 		return (0);
    242 	if (ca->ca_bustype == BUS_MAIN) {
    243 		if (ca->ca_ra.ra_vaddr &&
    244 		    probeget(ca->ca_ra.ra_vaddr, 1) == -1) {
    245 			return (0);
    246 		}
    247 		return (1);
    248 	}
    249 
    250 	return (0);
    251 }
    252 
    253 /*
    254  * Arguments passed between fdcattach and fdprobe.
    255  */
    256 struct fdc_attach_args {
    257 	int fa_drive;
    258 	struct fd_type *fa_deftype;
    259 };
    260 
    261 /*
    262  * Print the location of a disk drive (called just before attaching the
    263  * the drive).  If `fdc' is not NULL, the drive was found but was not
    264  * in the system config file; print the drive name as well.
    265  * Return QUIET (config_find ignores this if the device was configured) to
    266  * avoid printing `fdN not configured' messages.
    267  */
    268 int
    269 fdprint(aux, fdc)
    270 	void *aux;
    271 	char *fdc;
    272 {
    273 	register struct fdc_attach_args *fa = aux;
    274 
    275 	if (!fdc)
    276 		printf(" drive %d", fa->fa_drive);
    277 	return QUIET;
    278 }
    279 
    280 static void
    281 fdconf(fdc)
    282 	struct fdc_softc *fdc;
    283 {
    284 	int	vroom;
    285 
    286 	if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
    287 		return;
    288 
    289 	/*
    290 	 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
    291 	 * the PROM thinks is appropriate.
    292 	 */
    293 	if ((vroom = fdc->sc_status[7]) == 0)
    294 		vroom = 0x64;
    295 
    296 	/* Configure controller to use FIFO and Implied Seek */
    297 	out_fdc(fdc, NE7CMD_CFG);
    298 	out_fdc(fdc, vroom);
    299 	out_fdc(fdc, fdc->sc_cfg);
    300 	out_fdc(fdc, 0); /* PRETRK */
    301 	/* No result phase */
    302 }
    303 
    304 void
    305 fdcattach(parent, self, aux)
    306 	struct device *parent, *self;
    307 	void *aux;
    308 {
    309 	register struct confargs *ca = aux;
    310 	struct fdc_softc *fdc = (void *)self;
    311 	struct fdc_attach_args fa;
    312 	int n, pri;
    313 	char code;
    314 
    315 	if (ca->ca_ra.ra_vaddr)
    316 		fdc->sc_reg = (caddr_t)ca->ca_ra.ra_vaddr;
    317 	else
    318 		fdc->sc_reg = (caddr_t)mapiodev(ca->ca_ra.ra_paddr,
    319 						ca->ca_ra.ra_len,
    320 						ca->ca_bustype);
    321 
    322 	fdc->sc_state = DEVIDLE;
    323 	fdc->sc_istate = ISTATE_IDLE;
    324 	fdc->sc_flags |= FDC_EIS;
    325 	TAILQ_INIT(&fdc->sc_drives);
    326 
    327 	pri = ca->ca_ra.ra_intr[0].int_pri;
    328 #ifdef FDC_C_HANDLER
    329 	fdc->sc_hih.ih_fun = (void *)fdchwintr;
    330 	fdc->sc_hih.ih_arg = fdc;
    331 	intr_establish(pri, &fdc->sc_hih);
    332 #else
    333 	fdciop = &fdc->sc_io;
    334 	intr_fasttrap(pri, fdchwintr);
    335 #endif
    336 	fdc->sc_sih.ih_fun = (void *)fdcswintr;
    337 	fdc->sc_sih.ih_arg = fdc;
    338 	intr_establish(PIL_FDSOFT, &fdc->sc_sih);
    339 
    340 	/* Assume a 82077 */
    341 	fdc->sc_reg_msr = &((struct fdreg_77 *)fdc->sc_reg)->fd_msr;
    342 	fdc->sc_reg_fifo = &((struct fdreg_77 *)fdc->sc_reg)->fd_fifo;
    343 	fdc->sc_reg_dor = &((struct fdreg_77 *)fdc->sc_reg)->fd_dor;
    344 
    345 	code = '7';
    346 	if (*fdc->sc_reg_dor == NE7_RQM) {
    347 		/*
    348 		 * This hack from Chris Torek: apparently DOR really
    349 		 * addresses MSR/DRS on a 82072.
    350 		 * We used to rely on the VERSION command to tell the
    351 		 * difference (which did not work).
    352 		 */
    353 		*fdc->sc_reg_dor = FDC_250KBPS;
    354 		if (*fdc->sc_reg_dor == NE7_RQM)
    355 			code = '2';
    356 	}
    357 	if (code == '7') {
    358 		fdc->sc_flags |= FDC_82077;
    359 	} else {
    360 		fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr;
    361 		fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo;
    362 		fdc->sc_reg_dor = 0;
    363 	}
    364 
    365 #ifdef FD_DEBUG
    366 	if (out_fdc(fdc, NE7CMD_VERSION) == 0 &&
    367 	    fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
    368 		if (fdc_debug)
    369 			printf("[version cmd]");
    370 	}
    371 #endif
    372 
    373 	/*
    374 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
    375 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
    376 	 */
    377 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
    378 	fdconf(fdc);
    379 
    380 	if (fdc->sc_flags & FDC_82077) {
    381 		/* Lock configuration across soft resets. */
    382 		out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK);
    383 		if (fdcresult(fdc) != 1)
    384 			printf(" CFGLOCK: unexpected response");
    385 	}
    386 
    387 	evcnt_attach(&fdc->sc_dk.dk_dev, "intr", &fdc->sc_intrcnt);
    388 
    389 	printf(" pri %d, softpri %d: chip 8207%c\n", pri, PIL_FDSOFT, code);
    390 
    391 	/* physical limit: four drives per controller. */
    392 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    393 		fa.fa_deftype = NULL;		/* unknown */
    394 	fa.fa_deftype = &fd_types[0];		/* XXX */
    395 		(void)config_found(self, (void *)&fa, fdprint);
    396 	}
    397 }
    398 
    399 int
    400 fdmatch(parent, match, aux)
    401 	struct device *parent;
    402 	void *match, *aux;
    403 {
    404 	struct fdc_softc *fdc = (void *)parent;
    405 	struct cfdata *cf = match;
    406 	struct fdc_attach_args *fa = aux;
    407 	int drive = fa->fa_drive;
    408 	int n;
    409 
    410 	if (drive > 0)
    411 		/* XXX - for now, punt > 1 drives */
    412 		return 0;
    413 
    414 	if (fdc->sc_flags & FDC_82077) {
    415 		/* select drive and turn on motor */
    416 		*fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive);
    417 		/* wait for motor to spin up */
    418 		delay(250000);
    419 	} else {
    420 		auxregbisc(AUXIO_FDS, 0);
    421 	}
    422 	fdc->sc_nstat = 0;
    423 	out_fdc(fdc, NE7CMD_RECAL);
    424 	out_fdc(fdc, drive);
    425 	/* wait for recalibrate */
    426 	for (n = 0; n < 100000; n++) {
    427 		delay(10);
    428 		if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
    429 			/* wait a bit longer till device *really* is ready */
    430 			delay(100000);
    431 			if (out_fdc(fdc, NE7CMD_SENSEI))
    432 				break;
    433 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
    434 				/*
    435 				 * Got `invalid command'; we interpret it
    436 				 * to mean that the re-calibrate hasn't in
    437 				 * fact finished yet
    438 				 */
    439 				continue;
    440 			break;
    441 		}
    442 	}
    443 	n = fdc->sc_nstat;
    444 #ifdef FD_DEBUG
    445 	if (fdc_debug) {
    446 		int i;
    447 		printf("fdprobe: %d stati:", n);
    448 		for (i = 0; i < n; i++)
    449 			printf(" %x", fdc->sc_status[i]);
    450 		printf("\n");
    451 	}
    452 #endif
    453 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    454 		return 0;
    455 	/* turn off motor */
    456 	if (fdc->sc_flags & FDC_82077) {
    457 		/* select drive and turn on motor */
    458 		*fdc->sc_reg_dor = FDO_FRST;
    459 	} else {
    460 		auxregbisc(0, AUXIO_FDS);
    461 	}
    462 
    463 	return 1;
    464 }
    465 
    466 /*
    467  * Controller is working, and drive responded.  Attach it.
    468  */
    469 void
    470 fdattach(parent, self, aux)
    471 	struct device *parent, *self;
    472 	void *aux;
    473 {
    474 	struct fdc_softc *fdc = (void *)parent;
    475 	struct fd_softc *fd = (void *)self;
    476 	struct fdc_attach_args *fa = aux;
    477 	struct fd_type *type = fa->fa_deftype;
    478 	int drive = fa->fa_drive;
    479 
    480 	/* XXX Allow `flags' to override device type? */
    481 
    482 	if (type)
    483 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    484 		    type->tracks, type->heads, type->sectrac);
    485 	else
    486 		printf(": density unknown\n");
    487 
    488 	fd->sc_cylin = -1;
    489 	fd->sc_drive = drive;
    490 	fd->sc_deftype = type;
    491 	fdc->sc_fd[drive] = fd;
    492 	fd->sc_dk.dk_driver = &fddkdriver;
    493 #if 0
    494 	/* XXX Need to do some more fiddling with sc_dk. */
    495 	/* XXX sparc's dk_establish is bogus */
    496 	dk_establish(&fd->sc_dk, &fd->sc_dk.dk_dev);
    497 #endif
    498 }
    499 
    500 inline struct fd_type *
    501 fd_dev_to_type(fd, dev)
    502 	struct fd_softc *fd;
    503 	dev_t dev;
    504 {
    505 	int type = FDTYPE(dev);
    506 
    507 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    508 		return NULL;
    509 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    510 }
    511 
    512 void
    513 fdstrategy(bp)
    514 	register struct buf *bp;	/* IO operation to perform */
    515 {
    516 	struct fd_softc *fd;
    517 	int unit = FDUNIT(bp->b_dev);
    518 	int sz;
    519  	int s;
    520 
    521 	/* Valid unit, controller, and request? */
    522 	if (unit >= fdcd.cd_ndevs ||
    523 	    (fd = fdcd.cd_devs[unit]) == 0 ||
    524 	    bp->b_blkno < 0 ||
    525 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    526 		bp->b_error = EINVAL;
    527 		goto bad;
    528 	}
    529 
    530 	/* If it's a null transfer, return immediately. */
    531 	if (bp->b_bcount == 0)
    532 		goto done;
    533 
    534 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    535 
    536 	if (bp->b_blkno + sz > fd->sc_type->size) {
    537 		sz = fd->sc_type->size - bp->b_blkno;
    538 		if (sz == 0) {
    539 			/* If exactly at end of disk, return EOF. */
    540 			bp->b_resid = bp->b_bcount;
    541 			goto done;
    542 		}
    543 		if (sz < 0) {
    544 			/* If past end of disk, return EINVAL. */
    545 			bp->b_error = EINVAL;
    546 			goto bad;
    547 		}
    548 		/* Otherwise, truncate request. */
    549 		bp->b_bcount = sz << DEV_BSHIFT;
    550 	}
    551 
    552  	bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    553 
    554 #ifdef FD_DEBUG
    555 	if (fdc_debug > 1)
    556 		printf("fdstrategy: b_blkno %d b_bcount %d blkno %d cylin %d\n",
    557 		    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
    558 #endif
    559 
    560 	/* Queue transfer on drive, activate drive and controller if idle. */
    561 	s = splbio();
    562 	disksort(&fd->sc_q, bp);
    563 	untimeout(fd_motor_off, fd); /* a good idea */
    564 	if (!fd->sc_q.b_active)
    565 		fdstart(fd);
    566 #ifdef DIAGNOSTIC
    567 	else {
    568 		struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    569 		if (fdc->sc_state == DEVIDLE) {
    570 			printf("fdstrategy: controller inactive\n");
    571 			fdcstart(fdc);
    572 		}
    573 	}
    574 #endif
    575 	splx(s);
    576 	return;
    577 
    578 bad:
    579 	bp->b_flags |= B_ERROR;
    580 done:
    581 	/* Toss transfer; we're done early. */
    582 	biodone(bp);
    583 }
    584 
    585 void
    586 fdstart(fd)
    587 	struct fd_softc *fd;
    588 {
    589 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    590 	int active = fdc->sc_drives.tqh_first != 0;
    591 
    592 	/* Link into controller queue. */
    593 	fd->sc_q.b_active = 1;
    594 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    595 
    596 	/* If controller not already active, start it. */
    597 	if (!active)
    598 		fdcstart(fdc);
    599 }
    600 
    601 void
    602 fdfinish(fd, bp)
    603 	struct fd_softc *fd;
    604 	struct buf *bp;
    605 {
    606 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    607 
    608 	/*
    609 	 * Move this drive to the end of the queue to give others a `fair'
    610 	 * chance.  We only force a switch if N operations are completed while
    611 	 * another drive is waiting to be serviced, since there is a long motor
    612 	 * startup delay whenever we switch.
    613 	 */
    614 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    615 		fd->sc_ops = 0;
    616 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    617 		if (bp->b_actf) {
    618 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    619 		} else
    620 			fd->sc_q.b_active = 0;
    621 	}
    622 	bp->b_resid = fd->sc_bcount;
    623 	fd->sc_skip = 0;
    624 	fd->sc_q.b_actf = bp->b_actf;
    625 	biodone(bp);
    626 	/* turn off motor 5s from now */
    627 	timeout(fd_motor_off, fd, 5 * hz);
    628 	fdc->sc_state = DEVIDLE;
    629 }
    630 
    631 void
    632 fd_set_motor(fdc, reset)
    633 	struct fdc_softc *fdc;
    634 	int reset;
    635 {
    636 	struct fd_softc *fd;
    637 	u_char status;
    638 	int n;
    639 
    640 	if (fdc->sc_flags & FDC_82077) {
    641 		if (fd = fdc->sc_drives.tqh_first)
    642 			status = fd->sc_drive;
    643 		else
    644 			status = 0;
    645 		if (!reset)
    646 			status |= FDO_FRST | FDO_FDMAEN;
    647 		for (n = 0; n < 4; n++)
    648 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    649 				status |= FDO_MOEN(n);
    650 		*fdc->sc_reg_dor = status;
    651 	} else {
    652 		int on = 0;
    653 
    654 		for (n = 0; n < 4; n++)
    655 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    656 				on = 1;
    657 		if (on) {
    658 			auxregbisc(AUXIO_FDS, 0);
    659 		} else {
    660 			auxregbisc(0, AUXIO_FDS);
    661 		}
    662 		delay(10);
    663 		if (reset) {
    664 			*fdc->sc_reg_drs = DRS_RESET;
    665 			delay(10);
    666 			*fdc->sc_reg_drs = 0;
    667 #ifdef FD_DEBUG
    668 			if (fdc_debug)
    669 				printf("fdc reset\n");
    670 #endif
    671 			fdconf(fdc);
    672 		}
    673 
    674 	}
    675 }
    676 
    677 void
    678 fd_motor_off(arg)
    679 	void *arg;
    680 {
    681 	struct fd_softc *fd = arg;
    682 	int s;
    683 
    684 	s = splbio();
    685 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    686 	fd_set_motor((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent, 0);
    687 	splx(s);
    688 }
    689 
    690 void
    691 fd_motor_on(arg)
    692 	void *arg;
    693 {
    694 	struct fd_softc *fd = arg;
    695 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    696 	int s;
    697 
    698 	s = splbio();
    699 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    700 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
    701 		(void) fdcswintr(fdc);
    702 	splx(s);
    703 }
    704 
    705 int
    706 fdcresult(fdc)
    707 	struct fdc_softc *fdc;
    708 {
    709 	u_char i;
    710 	int j = 100000,
    711 	    n = 0;
    712 
    713 	for (; j; j--) {
    714 		i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
    715 		if (i == NE7_RQM)
    716 			return (fdc->sc_nstat = n);
    717 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    718 			if (n >= sizeof(fdc->sc_status)) {
    719 				log(LOG_ERR, "fdcresult: overrun\n");
    720 				return -1;
    721 			}
    722 			fdc->sc_status[n++] = *fdc->sc_reg_fifo;
    723 		}
    724 	}
    725 	log(LOG_ERR, "fdcresult: timeout\n");
    726 	return (fdc->sc_nstat = -1);
    727 }
    728 
    729 int
    730 out_fdc(fdc, x)
    731 	struct fdc_softc *fdc;
    732 	u_char x;
    733 {
    734 	int i = 100000;
    735 
    736 	while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0);
    737 	if (i <= 0)
    738 		return -1;
    739 
    740 	*fdc->sc_reg_fifo = x;
    741 	return 0;
    742 }
    743 
    744 int
    745 Fdopen(dev, flags)
    746 	dev_t dev;
    747 	int flags;
    748 {
    749  	int unit;
    750 	struct fd_softc *fd;
    751 	struct fd_type *type;
    752 
    753 	unit = FDUNIT(dev);
    754 	if (unit >= fdcd.cd_ndevs)
    755 		return ENXIO;
    756 	fd = fdcd.cd_devs[unit];
    757 	if (fd == 0)
    758 		return ENXIO;
    759 	type = fd_dev_to_type(fd, dev);
    760 	if (type == NULL)
    761 		return ENXIO;
    762 
    763 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    764 	    fd->sc_type != type)
    765 		return EBUSY;
    766 
    767 	fd->sc_type = type;
    768 	fd->sc_cylin = -1;
    769 	fd->sc_flags |= FD_OPEN;
    770 
    771 	return 0;
    772 }
    773 
    774 int
    775 fdclose(dev, flags)
    776 	dev_t dev;
    777 	int flags;
    778 {
    779 	struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
    780 
    781 	fd->sc_flags &= ~FD_OPEN;
    782 	return 0;
    783 }
    784 
    785 void
    786 fdcstart(fdc)
    787 	struct fdc_softc *fdc;
    788 {
    789 
    790 #ifdef DIAGNOSTIC
    791 	/* only got here if controller's drive queue was inactive; should
    792 	   be in idle state */
    793 	if (fdc->sc_state != DEVIDLE) {
    794 		printf("fdcstart: not idle\n");
    795 		return;
    796 	}
    797 #endif
    798 	(void) fdcswintr(fdc);
    799 }
    800 
    801 void
    802 fdcstatus(dv, n, s)
    803 	struct device *dv;
    804 	int n;
    805 	char *s;
    806 {
    807 	struct fdc_softc *fdc = (void *)dv->dv_parent;
    808 
    809 #if 0
    810 	/*
    811 	 * A 82072 seems to return <invalid command> on
    812 	 * gratuitous Sense Interrupt commands.
    813 	 */
    814 	if (n == 0 && (fdc->sc_flags & FDC_82077)) {
    815 		out_fdc(fdc, NE7CMD_SENSEI);
    816 		(void) fdcresult(fdc);
    817 		n = 2;
    818 	}
    819 #endif
    820 
    821 	/* Just print last status */
    822 	n = fdc->sc_nstat;
    823 
    824 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
    825 
    826 	switch (n) {
    827 	case 0:
    828 		printf("\n");
    829 		break;
    830 	case 2:
    831 		printf(" (st0 %b cyl %d)\n",
    832 		    fdc->sc_status[0], NE7_ST0BITS,
    833 		    fdc->sc_status[1]);
    834 		break;
    835 	case 7:
    836 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
    837 		    fdc->sc_status[0], NE7_ST0BITS,
    838 		    fdc->sc_status[1], NE7_ST1BITS,
    839 		    fdc->sc_status[2], NE7_ST2BITS,
    840 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    841 		break;
    842 #ifdef DIAGNOSTIC
    843 	default:
    844 		printf(" fdcstatus: weird size: %d\n", n);
    845 		break;
    846 #endif
    847 	}
    848 }
    849 
    850 void
    851 fdctimeout(arg)
    852 	void *arg;
    853 {
    854 	struct fdc_softc *fdc = arg;
    855 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
    856 	int s;
    857 
    858 	s = splbio();
    859 	fdcstatus(&fd->sc_dk.dk_dev, 0, "timeout");
    860 
    861 	if (fd->sc_q.b_actf)
    862 		fdc->sc_state++;
    863 	else
    864 		fdc->sc_state = DEVIDLE;
    865 
    866 	(void) fdcswintr(fdc);
    867 	splx(s);
    868 }
    869 
    870 void
    871 fdcpseudointr(arg)
    872 	void *arg;
    873 {
    874 	struct fdc_softc *fdc = arg;
    875 	int s;
    876 
    877 	/* Just ensure it has the right spl. */
    878 	s = splbio();
    879 	(void) fdcswintr(fdc);
    880 	splx(s);
    881 }
    882 
    883 
    884 #ifdef FDC_C_HANDLER
    885 /*
    886  * hardware interrupt entry point: must be converted to `fast'
    887  * (in-window) handler.
    888  */
    889 int
    890 fdchwintr(fdc)
    891 	struct fdc_softc *fdc;
    892 {
    893 	struct buf *bp;
    894 	int read;
    895 
    896 	switch (fdc->sc_istate) {
    897 	case ISTATE_SENSEI:
    898 		out_fdc(fdc, NE7CMD_SENSEI);
    899 		fdcresult(fdc);
    900 		fdc->sc_istate = ISTATE_IDLE;
    901 		ienab_bis(IE_FDSOFT);
    902 		return 1;
    903 	case ISTATE_IDLE:
    904 	case ISTATE_SPURIOUS:
    905 		auxregbisc(0, AUXIO_FDS);	/* Does this help? */
    906 		fdcresult(fdc);
    907 		fdc->sc_istate = ISTATE_SPURIOUS;
    908 		printf("fdc: stray hard interrupt... ");
    909 		ienab_bis(IE_FDSOFT);
    910 		return 1;
    911 	case ISTATE_DMA:
    912 		break;
    913 	default:
    914 		printf("fdc: goofed ...\n");
    915 		return 1;
    916 	}
    917 
    918 	read = bp->b_flags & B_READ;
    919 	for (;;) {
    920 		register int msr;
    921 
    922 		msr = *fdc->sc_reg_msr;
    923 
    924 		if ((msr & NE7_RQM) == 0)
    925 			break;
    926 
    927 		if ((msr & NE7_NDM) == 0) {
    928 			fdcresult(fdc);
    929 			fdc->sc_istate = ISTATE_IDLE;
    930 			ienab_bis(IE_FDSOFT);
    931 			printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
    932 			break;
    933 		}
    934 
    935 		if (msr & NE7_DIO) {
    936 #ifdef DIAGNOSTIC
    937 			if (!read)
    938 				printf("fdxfer: false read\n");
    939 #endif
    940 			*fdc->sc_data++ = *fdc->sc_reg_fifo;
    941 		} else {
    942 #ifdef DIAGNOSTIC
    943 			if (read)
    944 				printf("fdxfer: false write\n");
    945 #endif
    946 			*fdc->sc_reg_fifo = *fdc->sc_data++;
    947 		}
    948 		if (--fdc->sc_tc == 0) {
    949 			auxregbisc(AUXIO_FTC, 0);
    950 			fdc->sc_istate = ISTATE_IDLE;
    951 			delay(10);
    952 			auxregbisc(0, AUXIO_FTC);
    953 			fdcresult(fdc);
    954 			ienab_bis(IE_FDSOFT);
    955 			break;
    956 		}
    957 	}
    958 	return 1;
    959 }
    960 #endif
    961 
    962 int
    963 fdcswintr(fdc)
    964 	struct fdc_softc *fdc;
    965 {
    966 #define	st0	fdc->sc_status[0]
    967 #define	st1	fdc->sc_status[1]
    968 #define	cyl	fdc->sc_status[1]
    969 #define OUT_FDC(fdc, c, s) \
    970     do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
    971 
    972 	struct fd_softc *fd;
    973 	struct buf *bp;
    974 	int read, head, trac, sec, i, s, nblks;
    975 	struct fd_type *type;
    976 
    977 loop:
    978 	if (fdc->sc_istate != ISTATE_IDLE) {
    979 		/* Trouble... */
    980 		printf("fdc: spurious interrupt: istate=%d\n", fdc->sc_istate);
    981 		fdc->sc_istate = ISTATE_IDLE;
    982 		goto doreset;
    983 	}
    984 
    985 	/* Is there a drive for the controller to do a transfer with? */
    986 	fd = fdc->sc_drives.tqh_first;
    987 	if (fd == NULL) {
    988 		fdc->sc_state = DEVIDLE;
    989  		return 0;
    990 	}
    991 
    992 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    993 	bp = fd->sc_q.b_actf;
    994 	if (bp == NULL) {
    995 		fd->sc_ops = 0;
    996 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    997 		fd->sc_q.b_active = 0;
    998 		goto loop;
    999 	}
   1000 
   1001 	switch (fdc->sc_state) {
   1002 	case DEVIDLE:
   1003 		fdc->sc_errors = 0;
   1004 		fd->sc_skip = 0;
   1005 		fd->sc_bcount = bp->b_bcount;
   1006 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
   1007 		untimeout(fd_motor_off, fd);
   1008 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1009 			fdc->sc_state = MOTORWAIT;
   1010 			return 1;
   1011 		}
   1012 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1013 			/* Turn on the motor, being careful about pairing. */
   1014 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1015 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1016 				untimeout(fd_motor_off, ofd);
   1017 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1018 			}
   1019 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1020 			fd_set_motor(fdc, 0);
   1021 			fdc->sc_state = MOTORWAIT;
   1022 			if (fdc->sc_flags & FDC_82077) { /* XXX */
   1023 				/* Allow .25s for motor to stabilize. */
   1024 				timeout(fd_motor_on, fd, hz / 4);
   1025 			} else {
   1026 				fd->sc_flags &= ~FD_MOTOR_WAIT;
   1027 				goto loop;
   1028 			}
   1029 			return 1;
   1030 		}
   1031 		/* Make sure the right drive is selected. */
   1032 		fd_set_motor(fdc, 0);
   1033 
   1034 		/* fall through */
   1035 	case DOSEEK:
   1036 	doseek:
   1037 		if (fdc->sc_flags & FDC_EIS) {
   1038 			fd->sc_cylin = bp->b_cylin;
   1039 			/* We use implied seek */
   1040 			goto doio;
   1041 		}
   1042 
   1043 		if (fd->sc_cylin == bp->b_cylin)
   1044 			goto doio;
   1045 
   1046 		/* specify command */
   1047 		OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
   1048 		OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
   1049 		OUT_FDC(fdc, 6, SEEKTIMEDOUT);	/* XXX head load time == 6ms */
   1050 
   1051 		fdc->sc_istate = ISTATE_SENSEI;
   1052 		/* seek function */
   1053 		OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
   1054 		OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
   1055 		OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT);
   1056 
   1057 		fd->sc_cylin = -1;
   1058 		fdc->sc_state = SEEKWAIT;
   1059 		fdc->sc_nstat = 0;
   1060 		timeout(fdctimeout, fdc, 4 * hz);
   1061 		return 1;
   1062 
   1063 	case DOIO:
   1064 	doio:
   1065 		type = fd->sc_type;
   1066 		sec = fd->sc_blkno % type->seccyl;
   1067 		nblks = type->seccyl - sec;
   1068 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1069 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
   1070 		fd->sc_nblks = nblks;
   1071 		fd->sc_nbytes = nblks * FDC_BSIZE;
   1072 		head = sec / type->sectrac;
   1073 		sec -= head * type->sectrac;
   1074 #ifdef DIAGNOSTIC
   1075 		{int block;
   1076 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
   1077 		 if (block != fd->sc_blkno) {
   1078 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
   1079 #ifdef DDB
   1080 			 Debugger();
   1081 #endif
   1082 		 }}
   1083 #endif
   1084 		read = bp->b_flags & B_READ;
   1085 
   1086 		/* Setup for pseudo DMA */
   1087 		fdc->sc_data = bp->b_data + fd->sc_skip;
   1088 		fdc->sc_tc = fd->sc_nbytes;
   1089 
   1090 		*fdc->sc_reg_drs = type->rate;
   1091 #ifdef FD_DEBUG
   1092 		if (fdc_debug > 1)
   1093 			printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
   1094 				read ? "read" : "write", fd->sc_drive,
   1095 				fd->sc_cylin, head, sec, nblks);
   1096 #endif
   1097 		fdc->sc_state = IOCOMPLETE;
   1098 		fdc->sc_istate = ISTATE_DMA;
   1099 		fdc->sc_nstat = 0;
   1100 		if (read)
   1101 			OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT);	/* READ */
   1102 		else
   1103 			OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT);	/* WRITE */
   1104 		OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
   1105 		OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT);	/* track */
   1106 		OUT_FDC(fdc, head, IOTIMEDOUT);
   1107 		OUT_FDC(fdc, sec + 1, IOTIMEDOUT);	/* sector +1 */
   1108 		OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/* sector size */
   1109 		OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/* sectors/track */
   1110 		OUT_FDC(fdc, type->gap1, IOTIMEDOUT);	/* gap1 size */
   1111 		OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/* data length */
   1112 		/* allow 2 seconds for operation */
   1113 		timeout(fdctimeout, fdc, 2 * hz);
   1114 		return 1;				/* will return later */
   1115 
   1116 	case SEEKWAIT:
   1117 		untimeout(fdctimeout, fdc);
   1118 		fdc->sc_state = SEEKCOMPLETE;
   1119 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1120 			/* allow 1/50 second for heads to settle */
   1121 			timeout(fdcpseudointr, fdc, hz / 50);
   1122 			return 1;		/* will return later */
   1123 		}
   1124 
   1125 	case SEEKCOMPLETE:
   1126 		/* Make sure seek really happened. */
   1127 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1128 		    cyl != bp->b_cylin * fd->sc_type->step) {
   1129 #ifdef FD_DEBUG
   1130 			if (fdc_debug)
   1131 				fdcstatus(&fd->sc_dk.dk_dev, 2, "seek failed");
   1132 #endif
   1133 			fdcretry(fdc);
   1134 			goto loop;
   1135 		}
   1136 		fd->sc_cylin = bp->b_cylin;
   1137 		goto doio;
   1138 
   1139 	case IOTIMEDOUT:
   1140 		auxregbisc(AUXIO_FTC, 0);
   1141 		delay(10);
   1142 		auxregbisc(0, AUXIO_FTC);
   1143 		(void)fdcresult(fdc);
   1144 	case SEEKTIMEDOUT:
   1145 	case RECALTIMEDOUT:
   1146 	case RESETTIMEDOUT:
   1147 		fdcretry(fdc);
   1148 		goto loop;
   1149 
   1150 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1151 		untimeout(fdctimeout, fdc);
   1152 		if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) {
   1153 #ifdef FD_DEBUG
   1154 			if (fdc_debug) {
   1155 				fdcstatus(&fd->sc_dk.dk_dev, 7,
   1156 					bp->b_flags & B_READ
   1157 					? "read failed" : "write failed");
   1158 				printf("blkno %d nblks %d tc %d\n",
   1159 				       fd->sc_blkno, fd->sc_nblks, fdc->sc_tc);
   1160 			}
   1161 #endif
   1162 			if (fdc->sc_nstat == 7 &&
   1163 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
   1164 
   1165 				/*
   1166 				 * Silently retry overruns if no other
   1167 				 * error bit is set. Adjust threshold.
   1168 				 */
   1169 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1170 				if (thr < 15) {
   1171 					thr++;
   1172 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1173 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1174 #ifdef FD_DEBUG
   1175 					if (fdc_debug)
   1176 						printf("fdc: %d -> threshold\n", thr);
   1177 #endif
   1178 					fdconf(fdc);
   1179 					fdc->sc_state = DOIO;
   1180 					fdc->sc_overruns = 0;
   1181 				}
   1182 				if (++fdc->sc_overruns < 3)
   1183 					goto loop;
   1184 			}
   1185 			fdcretry(fdc);
   1186 			goto loop;
   1187 		}
   1188 		if (fdc->sc_errors) {
   1189 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1190 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1191 			printf("\n");
   1192 			fdc->sc_errors = 0;
   1193 		} else {
   1194 			if (--fdc->sc_overruns < -5) {
   1195 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1196 				if (thr > 0) {
   1197 					thr--;
   1198 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1199 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1200 #ifdef FD_DEBUG
   1201 					if (fdc_debug)
   1202 						printf("fdc: %d -> threshold\n", thr);
   1203 #endif
   1204 					fdconf(fdc);
   1205 				}
   1206 				fdc->sc_overruns = 0;
   1207 			}
   1208 		}
   1209 		fd->sc_blkno += fd->sc_nblks;
   1210 		fd->sc_skip += fd->sc_nbytes;
   1211 		fd->sc_bcount -= fd->sc_nbytes;
   1212 		if (fd->sc_bcount > 0) {
   1213 			bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
   1214 			goto doseek;
   1215 		}
   1216 		fdfinish(fd, bp);
   1217 		goto loop;
   1218 
   1219 	case DORESET:
   1220 	doreset:
   1221 		/* try a reset, keep motor on */
   1222 		fd_set_motor(fdc, 1);
   1223 		delay(100);
   1224 		fd_set_motor(fdc, 0);
   1225 		fdc->sc_state = RESETCOMPLETE;
   1226 		timeout(fdctimeout, fdc, hz / 2);
   1227 		return 1;			/* will return later */
   1228 
   1229 	case RESETCOMPLETE:
   1230 		untimeout(fdctimeout, fdc);
   1231 		/* clear the controller output buffer */
   1232 		for (i = 0; i < 4; i++) {
   1233 			out_fdc(fdc, NE7CMD_SENSEI);
   1234 			(void) fdcresult(fdc);
   1235 		}
   1236 
   1237 		/* fall through */
   1238 	case DORECAL:
   1239 		fdc->sc_state = RECALWAIT;
   1240 		fdc->sc_istate = ISTATE_SENSEI;
   1241 		fdc->sc_nstat = 0;
   1242 		/* recalibrate function */
   1243 		OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
   1244 		OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
   1245 		timeout(fdctimeout, fdc, 5 * hz);
   1246 		return 1;			/* will return later */
   1247 
   1248 	case RECALWAIT:
   1249 		untimeout(fdctimeout, fdc);
   1250 		fdc->sc_state = RECALCOMPLETE;
   1251 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1252 			/* allow 1/30 second for heads to settle */
   1253 			timeout(fdcpseudointr, fdc, hz / 30);
   1254 			return 1;		/* will return later */
   1255 		}
   1256 
   1257 	case RECALCOMPLETE:
   1258 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1259 #ifdef FD_DEBUG
   1260 			if (fdc_debug)
   1261 				fdcstatus(&fd->sc_dk.dk_dev, 2, "recalibrate failed");
   1262 #endif
   1263 			fdcretry(fdc);
   1264 			goto loop;
   1265 		}
   1266 		fd->sc_cylin = 0;
   1267 		goto doseek;
   1268 
   1269 	case MOTORWAIT:
   1270 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1271 			return 1;		/* time's not up yet */
   1272 		goto doseek;
   1273 
   1274 	default:
   1275 		fdcstatus(&fd->sc_dk.dk_dev, 0, "stray interrupt");
   1276 		return 1;
   1277 	}
   1278 #ifdef DIAGNOSTIC
   1279 	panic("fdcintr: impossible");
   1280 #endif
   1281 #undef	st0
   1282 #undef	st1
   1283 #undef	cyl
   1284 }
   1285 
   1286 void
   1287 fdcretry(fdc)
   1288 	struct fdc_softc *fdc;
   1289 {
   1290 	struct fd_softc *fd;
   1291 	struct buf *bp;
   1292 
   1293 	fd = fdc->sc_drives.tqh_first;
   1294 	bp = fd->sc_q.b_actf;
   1295 
   1296 	fdc->sc_overruns = 0;
   1297 
   1298 	switch (fdc->sc_errors) {
   1299 	case 0:
   1300 		/* try again */
   1301 		fdc->sc_state =
   1302 			(fdc->sc_flags & FDC_EIS) ? DOIO : SEEKCOMPLETE;
   1303 		break;
   1304 
   1305 	case 1: case 2: case 3:
   1306 		/* didn't work; try recalibrating */
   1307 		fdc->sc_state = DORECAL;
   1308 		break;
   1309 
   1310 	case 4:
   1311 		/* still no go; reset the bastard */
   1312 		fdc->sc_state = DORESET;
   1313 		break;
   1314 
   1315 	default:
   1316 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1317 		    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1318 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
   1319 		    fdc->sc_status[0], NE7_ST0BITS,
   1320 		    fdc->sc_status[1], NE7_ST1BITS,
   1321 		    fdc->sc_status[2], NE7_ST2BITS,
   1322 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1323 
   1324 		bp->b_flags |= B_ERROR;
   1325 		bp->b_error = EIO;
   1326 		fdfinish(fd, bp);
   1327 	}
   1328 	fdc->sc_errors++;
   1329 }
   1330 
   1331 int
   1332 fdsize(dev)
   1333 	dev_t dev;
   1334 {
   1335 
   1336 	/* Swapping to floppies would not make sense. */
   1337 	return -1;
   1338 }
   1339 
   1340 int
   1341 fddump()
   1342 {
   1343 
   1344 	/* Not implemented. */
   1345 	return EINVAL;
   1346 }
   1347 
   1348 int
   1349 fdioctl(dev, cmd, addr, flag)
   1350 	dev_t dev;
   1351 	u_long cmd;
   1352 	caddr_t addr;
   1353 	int flag;
   1354 {
   1355 	struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
   1356 	struct disklabel buffer;
   1357 	int error;
   1358 
   1359 	switch (cmd) {
   1360 	case DIOCGDINFO:
   1361 		bzero(&buffer, sizeof(buffer));
   1362 
   1363 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1364 		buffer.d_type = DTYPE_FLOPPY;
   1365 		buffer.d_secsize = FDC_BSIZE;
   1366 
   1367 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1368 			return EINVAL;
   1369 
   1370 		*(struct disklabel *)addr = buffer;
   1371 		return 0;
   1372 
   1373 	case DIOCWLABEL:
   1374 		if ((flag & FWRITE) == 0)
   1375 			return EBADF;
   1376 		/* XXX do something */
   1377 		return 0;
   1378 
   1379 	case DIOCWDINFO:
   1380 		if ((flag & FWRITE) == 0)
   1381 			return EBADF;
   1382 
   1383 		error = setdisklabel(&buffer, (struct disklabel *)addr, 0, NULL);
   1384 		if (error)
   1385 			return error;
   1386 
   1387 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1388 		return error;
   1389 
   1390 	case FDIOCEJECT:
   1391 		auxregbisc(AUXIO_FDS, AUXIO_FEJ);
   1392 		delay(10);
   1393 		auxregbisc(AUXIO_FEJ, AUXIO_FDS);
   1394 		return 0;
   1395 #ifdef DEBUG
   1396 	case _IO('f', 100):
   1397 		{
   1398 		int i;
   1399 		struct fdc_softc *fdc = (struct fdc_softc *)
   1400 					fd->sc_dk.dk_dev.dv_parent;
   1401 
   1402 		out_fdc(fdc, NE7CMD_DUMPREG);
   1403 		fdcresult(fdc);
   1404 		printf("dumpreg(%d regs): <", fdc->sc_nstat);
   1405 		for (i = 0; i < fdc->sc_nstat; i++)
   1406 			printf(" %x", fdc->sc_status[i]);
   1407 		printf(">\n");
   1408 		}
   1409 
   1410 		return 0;
   1411 	case _IOW('f', 101, int):
   1412 		((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent)->sc_cfg &=
   1413 			~CFG_THRHLD_MASK;
   1414 		((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent)->sc_cfg |=
   1415 			(*(int *)addr & CFG_THRHLD_MASK);
   1416 		fdconf(fd->sc_dk.dk_dev.dv_parent);
   1417 		return 0;
   1418 	case _IO('f', 102):
   1419 		{
   1420 		int i;
   1421 		struct fdc_softc *fdc = (struct fdc_softc *)
   1422 					fd->sc_dk.dk_dev.dv_parent;
   1423 		out_fdc(fdc, NE7CMD_SENSEI);
   1424 		fdcresult(fdc);
   1425 		printf("sensei(%d regs): <", fdc->sc_nstat);
   1426 		for (i=0; i< fdc->sc_nstat; i++)
   1427 			printf(" 0x%x", fdc->sc_status[i]);
   1428 		}
   1429 		printf(">\n");
   1430 		return 0;
   1431 #endif
   1432 	default:
   1433 		return ENOTTY;
   1434 	}
   1435 
   1436 #ifdef DIAGNOSTIC
   1437 	panic("fdioctl: impossible");
   1438 #endif
   1439 }
   1440