Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.7
      1 /*	$NetBSD: fd.c,v 1.7 1995/05/16 17:02:00 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 
    314 	if (ca->ca_ra.ra_vaddr)
    315 		fdc->sc_reg = (caddr_t)ca->ca_ra.ra_vaddr;
    316 	else
    317 		fdc->sc_reg = (caddr_t)mapiodev(ca->ca_ra.ra_paddr,
    318 						ca->ca_ra.ra_len,
    319 						ca->ca_bustype);
    320 
    321 	if (cputyp == CPU_SUN4M) {
    322 		fdc->sc_reg_msr = &((struct fdreg_sun4m *)fdc->sc_reg)->fd_msr;
    323 		fdc->sc_reg_fifo = &((struct fdreg_sun4m *)fdc->sc_reg)->fd_fifo;
    324 		fdc->sc_reg_dor = &((struct fdreg_sun4m *)fdc->sc_reg)->fd_dor;
    325 	} else {
    326 		fdc->sc_reg_msr = &((struct fdreg_sun4c *)fdc->sc_reg)->fd_msr;
    327 		fdc->sc_reg_fifo = &((struct fdreg_sun4c *)fdc->sc_reg)->fd_fifo;
    328 	}
    329 
    330 	fdc->sc_state = DEVIDLE;
    331 	fdc->sc_istate = ISTATE_IDLE;
    332 	fdc->sc_flags |= FDC_EIS;
    333 	TAILQ_INIT(&fdc->sc_drives);
    334 
    335 	pri = ca->ca_ra.ra_intr[0].int_pri;
    336 #ifdef FDC_C_HANDLER
    337 	fdc->sc_hih.ih_fun = (void *)fdchwintr;
    338 	fdc->sc_hih.ih_arg = fdc;
    339 	intr_establish(pri, &fdc->sc_hih);
    340 #else
    341 	fdciop = &fdc->sc_io;
    342 	intr_fasttrap(pri, fdchwintr);
    343 #endif
    344 	fdc->sc_sih.ih_fun = (void *)fdcswintr;
    345 	fdc->sc_sih.ih_arg = fdc;
    346 	intr_establish(PIL_FDSOFT, &fdc->sc_sih);
    347 
    348 	if (out_fdc(fdc, NE7CMD_VERSION)) {
    349 		printf(" misconfigured\n");
    350 		return;
    351 	}
    352 
    353 	n = fdcresult(fdc);
    354 	if (n == 1 && fdc->sc_status[0] == 0x90) {
    355 		fdc->sc_flags |= FDC_82077;
    356 		if (cputyp != CPU_SUN4M)
    357 			printf(" Hmmm.. ");
    358 	} else {
    359 		/* Not a 82077 */
    360 		if (cputyp != CPU_SUN4C)
    361 			printf(" Hmmm.. ");
    362 	}
    363 
    364 	/*
    365 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
    366 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
    367 	 */
    368 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
    369 	fdconf(fdc);
    370 
    371 	if (fdc->sc_flags & FDC_82077) {
    372 		/* Lock configuration across soft resets. */
    373 		out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK);
    374 		if (fdcresult(fdc) != 1)
    375 			printf(" CFGLOCK: unexpected response");
    376 	}
    377 
    378 	evcnt_attach(&fdc->sc_dk.dk_dev, "intr", &fdc->sc_intrcnt);
    379 
    380 	printf(" pri %d, softpri %d: chip %s\n", pri, PIL_FDSOFT,
    381 		(fdc->sc_flags & FDC_82077)?"82077":"82072");
    382 
    383 	/* physical limit: four drives per controller. */
    384 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    385 		fa.fa_deftype = NULL;		/* unknown */
    386 	fa.fa_deftype = &fd_types[0];		/* XXX */
    387 		(void)config_found(self, (void *)&fa, fdprint);
    388 	}
    389 }
    390 
    391 int
    392 fdmatch(parent, match, aux)
    393 	struct device *parent;
    394 	void *match, *aux;
    395 {
    396 	struct fdc_softc *fdc = (void *)parent;
    397 	struct cfdata *cf = match;
    398 	struct fdc_attach_args *fa = aux;
    399 	int drive = fa->fa_drive;
    400 	int n;
    401 
    402 	if (fdc->sc_flags & FDC_82077) {
    403 		/* select drive and turn on motor */
    404 		*fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive);
    405 		/* wait for motor to spin up */
    406 		delay(250000);
    407 	} else {
    408 		if (drive > 0)
    409 			/* XXX - drive 0 always answers */
    410 			return 0;
    411 		auxregbisc(AUXIO_FDS, 0);
    412 	}
    413 	fdc->sc_nstat = 0;
    414 	out_fdc(fdc, NE7CMD_RECAL);
    415 	out_fdc(fdc, drive);
    416 	/* wait for recalibrate */
    417 	for (n = 0; n < 100000; n++) {
    418 		delay(10);
    419 		if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
    420 			/* wait a bit longer till device *really* is ready */
    421 			delay(100000);
    422 			if (out_fdc(fdc, NE7CMD_SENSEI))
    423 				break;
    424 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
    425 				/*
    426 				 * Got `invalid command'; we interpret it
    427 				 * to mean that the re-calibrate hasn't in
    428 				 * fact finished yet
    429 				 */
    430 				continue;
    431 			break;
    432 		}
    433 	}
    434 	n = fdc->sc_nstat;
    435 #ifdef FD_DEBUG
    436 	if (fdc_debug) {
    437 		int i;
    438 		printf("fdprobe: %d stati:", n);
    439 		for (i = 0; i < n; i++)
    440 			printf(" %x", fdc->sc_status[i]);
    441 		printf("\n");
    442 	}
    443 #endif
    444 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    445 		return 0;
    446 	/* turn off motor */
    447 	if (fdc->sc_flags & FDC_82077) {
    448 		/* select drive and turn on motor */
    449 		*fdc->sc_reg_dor = FDO_FRST;
    450 	} else {
    451 		auxregbisc(0, AUXIO_FDS);
    452 	}
    453 
    454 	return 1;
    455 }
    456 
    457 /*
    458  * Controller is working, and drive responded.  Attach it.
    459  */
    460 void
    461 fdattach(parent, self, aux)
    462 	struct device *parent, *self;
    463 	void *aux;
    464 {
    465 	struct fdc_softc *fdc = (void *)parent;
    466 	struct fd_softc *fd = (void *)self;
    467 	struct fdc_attach_args *fa = aux;
    468 	struct fd_type *type = fa->fa_deftype;
    469 	int drive = fa->fa_drive;
    470 
    471 	/* XXX Allow `flags' to override device type? */
    472 
    473 	if (type)
    474 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    475 		    type->tracks, type->heads, type->sectrac);
    476 	else
    477 		printf(": density unknown\n");
    478 
    479 	fd->sc_cylin = -1;
    480 	fd->sc_drive = drive;
    481 	fd->sc_deftype = type;
    482 	fdc->sc_fd[drive] = fd;
    483 	fd->sc_dk.dk_driver = &fddkdriver;
    484 #if 0
    485 	/* XXX Need to do some more fiddling with sc_dk. */
    486 	/* XXX sparc's dk_establish is bogus */
    487 	dk_establish(&fd->sc_dk, &fd->sc_dk.dk_dev);
    488 #endif
    489 }
    490 
    491 inline struct fd_type *
    492 fd_dev_to_type(fd, dev)
    493 	struct fd_softc *fd;
    494 	dev_t dev;
    495 {
    496 	int type = FDTYPE(dev);
    497 
    498 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    499 		return NULL;
    500 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    501 }
    502 
    503 void
    504 fdstrategy(bp)
    505 	register struct buf *bp;	/* IO operation to perform */
    506 {
    507 	struct fd_softc *fd;
    508 	int unit = FDUNIT(bp->b_dev);
    509 	int sz;
    510  	int s;
    511 
    512 	/* Valid unit, controller, and request? */
    513 	if (unit >= fdcd.cd_ndevs ||
    514 	    (fd = fdcd.cd_devs[unit]) == 0 ||
    515 	    bp->b_blkno < 0 ||
    516 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    517 		bp->b_error = EINVAL;
    518 		goto bad;
    519 	}
    520 
    521 	/* If it's a null transfer, return immediately. */
    522 	if (bp->b_bcount == 0)
    523 		goto done;
    524 
    525 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    526 
    527 	if (bp->b_blkno + sz > fd->sc_type->size) {
    528 		sz = fd->sc_type->size - bp->b_blkno;
    529 		if (sz == 0) {
    530 			/* If exactly at end of disk, return EOF. */
    531 			bp->b_resid = bp->b_bcount;
    532 			goto done;
    533 		}
    534 		if (sz < 0) {
    535 			/* If past end of disk, return EINVAL. */
    536 			bp->b_error = EINVAL;
    537 			goto bad;
    538 		}
    539 		/* Otherwise, truncate request. */
    540 		bp->b_bcount = sz << DEV_BSHIFT;
    541 	}
    542 
    543  	bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    544 
    545 #ifdef FD_DEBUG
    546 	if (fdc_debug > 1)
    547 		printf("fdstrategy: b_blkno %d b_bcount %d blkno %d cylin %d\n",
    548 		    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
    549 #endif
    550 
    551 	/* Queue transfer on drive, activate drive and controller if idle. */
    552 	s = splbio();
    553 	disksort(&fd->sc_q, bp);
    554 	untimeout(fd_motor_off, fd); /* a good idea */
    555 	if (!fd->sc_q.b_active)
    556 		fdstart(fd);
    557 #ifdef DIAGNOSTIC
    558 	else {
    559 		struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    560 		if (fdc->sc_state == DEVIDLE) {
    561 			printf("fdstrategy: controller inactive\n");
    562 			fdcstart(fdc);
    563 		}
    564 	}
    565 #endif
    566 	splx(s);
    567 	return;
    568 
    569 bad:
    570 	bp->b_flags |= B_ERROR;
    571 done:
    572 	/* Toss transfer; we're done early. */
    573 	biodone(bp);
    574 }
    575 
    576 void
    577 fdstart(fd)
    578 	struct fd_softc *fd;
    579 {
    580 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    581 	int active = fdc->sc_drives.tqh_first != 0;
    582 
    583 	/* Link into controller queue. */
    584 	fd->sc_q.b_active = 1;
    585 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    586 
    587 	/* If controller not already active, start it. */
    588 	if (!active)
    589 		fdcstart(fdc);
    590 }
    591 
    592 void
    593 fdfinish(fd, bp)
    594 	struct fd_softc *fd;
    595 	struct buf *bp;
    596 {
    597 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    598 
    599 	/*
    600 	 * Move this drive to the end of the queue to give others a `fair'
    601 	 * chance.  We only force a switch if N operations are completed while
    602 	 * another drive is waiting to be serviced, since there is a long motor
    603 	 * startup delay whenever we switch.
    604 	 */
    605 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    606 		fd->sc_ops = 0;
    607 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    608 		if (bp->b_actf) {
    609 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    610 		} else
    611 			fd->sc_q.b_active = 0;
    612 	}
    613 	bp->b_resid = fd->sc_bcount;
    614 	fd->sc_skip = 0;
    615 	fd->sc_q.b_actf = bp->b_actf;
    616 	biodone(bp);
    617 	/* turn off motor 5s from now */
    618 	timeout(fd_motor_off, fd, 5 * hz);
    619 	fdc->sc_state = DEVIDLE;
    620 }
    621 
    622 void
    623 fd_set_motor(fdc, reset)
    624 	struct fdc_softc *fdc;
    625 	int reset;
    626 {
    627 	struct fd_softc *fd;
    628 	u_char status;
    629 	int n;
    630 
    631 	if (fdc->sc_flags & FDC_82077) {
    632 		if (fd = fdc->sc_drives.tqh_first)
    633 			status = fd->sc_drive;
    634 		else
    635 			status = 0;
    636 		if (!reset)
    637 			status |= FDO_FRST | FDO_FDMAEN;
    638 		for (n = 0; n < 4; n++)
    639 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    640 				status |= FDO_MOEN(n);
    641 		*fdc->sc_reg_dor = status;
    642 	} else {
    643 		int on = 0;
    644 
    645 		for (n = 0; n < 4; n++)
    646 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    647 				on = 1;
    648 		if (on) {
    649 			auxregbisc(AUXIO_FDS, 0);
    650 		} else {
    651 			auxregbisc(0, AUXIO_FDS);
    652 		}
    653 		delay(10);
    654 		if (reset) {
    655 			*fdc->sc_reg_drs = DRS_RESET;
    656 			delay(10);
    657 			*fdc->sc_reg_drs = 0;
    658 #ifdef FD_DEBUG
    659 			if (fdc_debug)
    660 				printf("fdc reset\n");
    661 #endif
    662 			fdconf(fdc);
    663 		}
    664 
    665 	}
    666 }
    667 
    668 void
    669 fd_motor_off(arg)
    670 	void *arg;
    671 {
    672 	struct fd_softc *fd = arg;
    673 	int s;
    674 
    675 	s = splbio();
    676 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    677 	fd_set_motor((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent, 0);
    678 	splx(s);
    679 }
    680 
    681 void
    682 fd_motor_on(arg)
    683 	void *arg;
    684 {
    685 	struct fd_softc *fd = arg;
    686 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    687 	int s;
    688 
    689 	s = splbio();
    690 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    691 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
    692 		(void) fdcswintr(fdc);
    693 	splx(s);
    694 }
    695 
    696 int
    697 fdcresult(fdc)
    698 	struct fdc_softc *fdc;
    699 {
    700 	u_char i;
    701 	int j = 100000,
    702 	    n = 0;
    703 
    704 	for (; j; j--) {
    705 		i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
    706 		if (i == NE7_RQM)
    707 			return (fdc->sc_nstat = n);
    708 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    709 			if (n >= sizeof(fdc->sc_status)) {
    710 				log(LOG_ERR, "fdcresult: overrun\n");
    711 				return -1;
    712 			}
    713 			fdc->sc_status[n++] = *fdc->sc_reg_fifo;
    714 		}
    715 	}
    716 	log(LOG_ERR, "fdcresult: timeout\n");
    717 	return (fdc->sc_nstat = -1);
    718 }
    719 
    720 int
    721 out_fdc(fdc, x)
    722 	struct fdc_softc *fdc;
    723 	u_char x;
    724 {
    725 	int i = 100000;
    726 
    727 	while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0);
    728 	if (i <= 0)
    729 		return -1;
    730 
    731 	*fdc->sc_reg_fifo = x;
    732 	return 0;
    733 }
    734 
    735 int
    736 Fdopen(dev, flags)
    737 	dev_t dev;
    738 	int flags;
    739 {
    740  	int unit;
    741 	struct fd_softc *fd;
    742 	struct fd_type *type;
    743 
    744 	unit = FDUNIT(dev);
    745 	if (unit >= fdcd.cd_ndevs)
    746 		return ENXIO;
    747 	fd = fdcd.cd_devs[unit];
    748 	if (fd == 0)
    749 		return ENXIO;
    750 	type = fd_dev_to_type(fd, dev);
    751 	if (type == NULL)
    752 		return ENXIO;
    753 
    754 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    755 	    fd->sc_type != type)
    756 		return EBUSY;
    757 
    758 	fd->sc_type = type;
    759 	fd->sc_cylin = -1;
    760 	fd->sc_flags |= FD_OPEN;
    761 
    762 	return 0;
    763 }
    764 
    765 int
    766 fdclose(dev, flags)
    767 	dev_t dev;
    768 	int flags;
    769 {
    770 	struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
    771 
    772 	fd->sc_flags &= ~FD_OPEN;
    773 	return 0;
    774 }
    775 
    776 void
    777 fdcstart(fdc)
    778 	struct fdc_softc *fdc;
    779 {
    780 
    781 #ifdef DIAGNOSTIC
    782 	/* only got here if controller's drive queue was inactive; should
    783 	   be in idle state */
    784 	if (fdc->sc_state != DEVIDLE) {
    785 		printf("fdcstart: not idle\n");
    786 		return;
    787 	}
    788 #endif
    789 	(void) fdcswintr(fdc);
    790 }
    791 
    792 void
    793 fdcstatus(dv, n, s)
    794 	struct device *dv;
    795 	int n;
    796 	char *s;
    797 {
    798 	struct fdc_softc *fdc = (void *)dv->dv_parent;
    799 
    800 #if 0
    801 	/*
    802 	 * A 82072 seems to return <invalid command> on
    803 	 * gratuitous Sense Interrupt commands.
    804 	 */
    805 	if (n == 0 && (fdc->sc_flags & FDC_82077)) {
    806 		out_fdc(fdc, NE7CMD_SENSEI);
    807 		(void) fdcresult(fdc);
    808 		n = 2;
    809 	}
    810 #endif
    811 
    812 	/* Just print last status */
    813 	n = fdc->sc_nstat;
    814 
    815 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
    816 
    817 	switch (n) {
    818 	case 0:
    819 		printf("\n");
    820 		break;
    821 	case 2:
    822 		printf(" (st0 %b cyl %d)\n",
    823 		    fdc->sc_status[0], NE7_ST0BITS,
    824 		    fdc->sc_status[1]);
    825 		break;
    826 	case 7:
    827 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
    828 		    fdc->sc_status[0], NE7_ST0BITS,
    829 		    fdc->sc_status[1], NE7_ST1BITS,
    830 		    fdc->sc_status[2], NE7_ST2BITS,
    831 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    832 		break;
    833 #ifdef DIAGNOSTIC
    834 	default:
    835 		printf(" fdcstatus: weird size: %d\n", n);
    836 		break;
    837 #endif
    838 	}
    839 }
    840 
    841 void
    842 fdctimeout(arg)
    843 	void *arg;
    844 {
    845 	struct fdc_softc *fdc = arg;
    846 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
    847 	int s;
    848 
    849 	s = splbio();
    850 	fdcstatus(&fd->sc_dk.dk_dev, 0, "timeout");
    851 
    852 	if (fd->sc_q.b_actf)
    853 		fdc->sc_state++;
    854 	else
    855 		fdc->sc_state = DEVIDLE;
    856 
    857 	(void) fdcswintr(fdc);
    858 	splx(s);
    859 }
    860 
    861 void
    862 fdcpseudointr(arg)
    863 	void *arg;
    864 {
    865 	struct fdc_softc *fdc = arg;
    866 	int s;
    867 
    868 	/* Just ensure it has the right spl. */
    869 	s = splbio();
    870 	(void) fdcswintr(fdc);
    871 	splx(s);
    872 }
    873 
    874 
    875 #ifdef FDC_C_HANDLER
    876 /*
    877  * hardware interrupt entry point: must be converted to `fast'
    878  * (in-window) handler.
    879  */
    880 int
    881 fdchwintr(fdc)
    882 	struct fdc_softc *fdc;
    883 {
    884 	struct buf *bp;
    885 	int read;
    886 
    887 	switch (fdc->sc_istate) {
    888 	case ISTATE_SENSEI:
    889 		out_fdc(fdc, NE7CMD_SENSEI);
    890 		fdcresult(fdc);
    891 		fdc->sc_istate = ISTATE_IDLE;
    892 		ienab_bis(IE_FDSOFT);
    893 		return 1;
    894 	case ISTATE_IDLE:
    895 	case ISTATE_SPURIOUS:
    896 		auxregbisc(0, AUXIO_FDS);	/* Does this help? */
    897 		fdcresult(fdc);
    898 		fdc->sc_istate = ISTATE_SPURIOUS;
    899 		printf("fdc: stray hard interrupt... ");
    900 		ienab_bis(IE_FDSOFT);
    901 		return 1;
    902 	case ISTATE_DMA:
    903 		break;
    904 	default:
    905 		printf("fdc: goofed ...\n");
    906 		return 1;
    907 	}
    908 
    909 	read = bp->b_flags & B_READ;
    910 	for (;;) {
    911 		register int msr;
    912 
    913 		msr = *fdc->sc_reg_msr;
    914 
    915 		if ((msr & NE7_RQM) == 0)
    916 			break;
    917 
    918 		if ((msr & NE7_NDM) == 0) {
    919 			fdcresult(fdc);
    920 			fdc->sc_istate = ISTATE_IDLE;
    921 			ienab_bis(IE_FDSOFT);
    922 			printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
    923 			break;
    924 		}
    925 
    926 		if (msr & NE7_DIO) {
    927 #ifdef DIAGNOSTIC
    928 			if (!read)
    929 				printf("fdxfer: false read\n");
    930 #endif
    931 			*fdc->sc_data++ = *fdc->sc_reg_fifo;
    932 		} else {
    933 #ifdef DIAGNOSTIC
    934 			if (read)
    935 				printf("fdxfer: false write\n");
    936 #endif
    937 			*fdc->sc_reg_fifo = *fdc->sc_data++;
    938 		}
    939 		if (--fdc->sc_tc == 0) {
    940 			auxregbisc(AUXIO_FTC, 0);
    941 			fdc->sc_istate = ISTATE_IDLE;
    942 			delay(10);
    943 			auxregbisc(0, AUXIO_FTC);
    944 			fdcresult(fdc);
    945 			ienab_bis(IE_FDSOFT);
    946 			break;
    947 		}
    948 	}
    949 	return 1;
    950 }
    951 #endif
    952 
    953 int
    954 fdcswintr(fdc)
    955 	struct fdc_softc *fdc;
    956 {
    957 #define	st0	fdc->sc_status[0]
    958 #define	st1	fdc->sc_status[1]
    959 #define	cyl	fdc->sc_status[1]
    960 #define OUT_FDC(fdc, c, s) \
    961     do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
    962 
    963 	struct fd_softc *fd;
    964 	struct buf *bp;
    965 	int read, head, trac, sec, i, s, nblks;
    966 	struct fd_type *type;
    967 
    968 loop:
    969 	if (fdc->sc_istate != ISTATE_IDLE) {
    970 		/* Trouble... */
    971 		printf("fdc: spurious interrupt: istate=%d\n", fdc->sc_istate);
    972 		fdc->sc_istate = ISTATE_IDLE;
    973 		goto doreset;
    974 	}
    975 
    976 	/* Is there a drive for the controller to do a transfer with? */
    977 	fd = fdc->sc_drives.tqh_first;
    978 	if (fd == NULL) {
    979 		fdc->sc_state = DEVIDLE;
    980  		return 0;
    981 	}
    982 
    983 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    984 	bp = fd->sc_q.b_actf;
    985 	if (bp == NULL) {
    986 		fd->sc_ops = 0;
    987 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    988 		fd->sc_q.b_active = 0;
    989 		goto loop;
    990 	}
    991 
    992 	switch (fdc->sc_state) {
    993 	case DEVIDLE:
    994 		fdc->sc_errors = 0;
    995 		fd->sc_skip = 0;
    996 		fd->sc_bcount = bp->b_bcount;
    997 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
    998 		untimeout(fd_motor_off, fd);
    999 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1000 			fdc->sc_state = MOTORWAIT;
   1001 			return 1;
   1002 		}
   1003 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1004 			/* Turn on the motor, being careful about pairing. */
   1005 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1006 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1007 				untimeout(fd_motor_off, ofd);
   1008 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1009 			}
   1010 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1011 			fd_set_motor(fdc, 0);
   1012 			fdc->sc_state = MOTORWAIT;
   1013 			if (fdc->sc_flags & FDC_82077) { /* XXX */
   1014 				/* Allow .25s for motor to stabilize. */
   1015 				timeout(fd_motor_on, fd, hz / 4);
   1016 			} else {
   1017 				fd->sc_flags &= ~FD_MOTOR_WAIT;
   1018 				goto loop;
   1019 			}
   1020 			return 1;
   1021 		}
   1022 		/* Make sure the right drive is selected. */
   1023 		fd_set_motor(fdc, 0);
   1024 
   1025 		/* fall through */
   1026 	case DOSEEK:
   1027 	doseek:
   1028 		if (fdc->sc_flags & FDC_EIS) {
   1029 			fd->sc_cylin = bp->b_cylin;
   1030 			/* We use implied seek */
   1031 			goto doio;
   1032 		}
   1033 
   1034 		if (fd->sc_cylin == bp->b_cylin)
   1035 			goto doio;
   1036 
   1037 		/* specify command */
   1038 		OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
   1039 		OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
   1040 		OUT_FDC(fdc, 6, SEEKTIMEDOUT);	/* XXX head load time == 6ms */
   1041 
   1042 		fdc->sc_istate = ISTATE_SENSEI;
   1043 		/* seek function */
   1044 		OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
   1045 		OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
   1046 		OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT);
   1047 
   1048 		fd->sc_cylin = -1;
   1049 		fdc->sc_state = SEEKWAIT;
   1050 		fdc->sc_nstat = 0;
   1051 		timeout(fdctimeout, fdc, 4 * hz);
   1052 		return 1;
   1053 
   1054 	case DOIO:
   1055 	doio:
   1056 		type = fd->sc_type;
   1057 		sec = fd->sc_blkno % type->seccyl;
   1058 		nblks = type->seccyl - sec;
   1059 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1060 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
   1061 		fd->sc_nblks = nblks;
   1062 		fd->sc_nbytes = nblks * FDC_BSIZE;
   1063 		head = sec / type->sectrac;
   1064 		sec -= head * type->sectrac;
   1065 #ifdef DIAGNOSTIC
   1066 		{int block;
   1067 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
   1068 		 if (block != fd->sc_blkno) {
   1069 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
   1070 #ifdef DDB
   1071 			 Debugger();
   1072 #endif
   1073 		 }}
   1074 #endif
   1075 		read = bp->b_flags & B_READ;
   1076 
   1077 		/* Setup for pseudo DMA */
   1078 		fdc->sc_data = bp->b_data + fd->sc_skip;
   1079 		fdc->sc_tc = fd->sc_nbytes;
   1080 
   1081 		*fdc->sc_reg_drs = type->rate;
   1082 #ifdef FD_DEBUG
   1083 		if (fdc_debug > 1)
   1084 			printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
   1085 				read ? "read" : "write", fd->sc_drive,
   1086 				fd->sc_cylin, head, sec, nblks);
   1087 #endif
   1088 		fdc->sc_state = IOCOMPLETE;
   1089 		fdc->sc_istate = ISTATE_DMA;
   1090 		fdc->sc_nstat = 0;
   1091 		if (read)
   1092 			OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT);	/* READ */
   1093 		else
   1094 			OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT);	/* WRITE */
   1095 		OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
   1096 		OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT);	/* track */
   1097 		OUT_FDC(fdc, head, IOTIMEDOUT);
   1098 		OUT_FDC(fdc, sec + 1, IOTIMEDOUT);	/* sector +1 */
   1099 		OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/* sector size */
   1100 		OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/* sectors/track */
   1101 		OUT_FDC(fdc, type->gap1, IOTIMEDOUT);	/* gap1 size */
   1102 		OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/* data length */
   1103 		/* allow 2 seconds for operation */
   1104 		timeout(fdctimeout, fdc, 2 * hz);
   1105 		return 1;				/* will return later */
   1106 
   1107 	case SEEKWAIT:
   1108 		untimeout(fdctimeout, fdc);
   1109 		fdc->sc_state = SEEKCOMPLETE;
   1110 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1111 			/* allow 1/50 second for heads to settle */
   1112 			timeout(fdcpseudointr, fdc, hz / 50);
   1113 			return 1;		/* will return later */
   1114 		}
   1115 
   1116 	case SEEKCOMPLETE:
   1117 		/* Make sure seek really happened. */
   1118 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1119 		    cyl != bp->b_cylin * fd->sc_type->step) {
   1120 #ifdef FD_DEBUG
   1121 			if (fdc_debug)
   1122 				fdcstatus(&fd->sc_dk.dk_dev, 2, "seek failed");
   1123 #endif
   1124 			fdcretry(fdc);
   1125 			goto loop;
   1126 		}
   1127 		fd->sc_cylin = bp->b_cylin;
   1128 		goto doio;
   1129 
   1130 	case IOTIMEDOUT:
   1131 		auxregbisc(AUXIO_FTC, 0);
   1132 		delay(10);
   1133 		auxregbisc(0, AUXIO_FTC);
   1134 		(void)fdcresult(fdc);
   1135 	case SEEKTIMEDOUT:
   1136 	case RECALTIMEDOUT:
   1137 	case RESETTIMEDOUT:
   1138 		fdcretry(fdc);
   1139 		goto loop;
   1140 
   1141 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1142 		untimeout(fdctimeout, fdc);
   1143 		if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) {
   1144 #ifdef FD_DEBUG
   1145 			if (fdc_debug) {
   1146 				fdcstatus(&fd->sc_dk.dk_dev, 7,
   1147 					bp->b_flags & B_READ
   1148 					? "read failed" : "write failed");
   1149 				printf("blkno %d nblks %d tc %d\n",
   1150 				       fd->sc_blkno, fd->sc_nblks, fdc->sc_tc);
   1151 			}
   1152 #endif
   1153 			if (fdc->sc_nstat == 7 &&
   1154 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
   1155 
   1156 				/*
   1157 				 * Silently retry overruns if no other
   1158 				 * error bit is set. Adjust threshold.
   1159 				 */
   1160 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1161 				if (thr < 15) {
   1162 					thr++;
   1163 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1164 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1165 #ifdef FD_DEBUG
   1166 					if (fdc_debug)
   1167 						printf("fdc: %d -> threshold\n", thr);
   1168 #endif
   1169 					fdconf(fdc);
   1170 					fdc->sc_state = DOIO;
   1171 					fdc->sc_overruns = 0;
   1172 				}
   1173 				if (++fdc->sc_overruns < 3)
   1174 					goto loop;
   1175 			}
   1176 			fdcretry(fdc);
   1177 			goto loop;
   1178 		}
   1179 		if (fdc->sc_errors) {
   1180 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1181 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1182 			printf("\n");
   1183 			fdc->sc_errors = 0;
   1184 		} else {
   1185 			if (--fdc->sc_overruns < -5) {
   1186 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1187 				if (thr > 0) {
   1188 					thr--;
   1189 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1190 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1191 #ifdef FD_DEBUG
   1192 					if (fdc_debug)
   1193 						printf("fdc: %d -> threshold\n", thr);
   1194 #endif
   1195 					fdconf(fdc);
   1196 				}
   1197 				fdc->sc_overruns = 0;
   1198 			}
   1199 		}
   1200 		fd->sc_blkno += fd->sc_nblks;
   1201 		fd->sc_skip += fd->sc_nbytes;
   1202 		fd->sc_bcount -= fd->sc_nbytes;
   1203 		if (fd->sc_bcount > 0) {
   1204 			bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
   1205 			goto doseek;
   1206 		}
   1207 		fdfinish(fd, bp);
   1208 		goto loop;
   1209 
   1210 	case DORESET:
   1211 	doreset:
   1212 		/* try a reset, keep motor on */
   1213 		fd_set_motor(fdc, 1);
   1214 		delay(100);
   1215 		fd_set_motor(fdc, 0);
   1216 		fdc->sc_state = RESETCOMPLETE;
   1217 		timeout(fdctimeout, fdc, hz / 2);
   1218 		return 1;			/* will return later */
   1219 
   1220 	case RESETCOMPLETE:
   1221 		untimeout(fdctimeout, fdc);
   1222 		/* clear the controller output buffer */
   1223 		for (i = 0; i < 4; i++) {
   1224 			out_fdc(fdc, NE7CMD_SENSEI);
   1225 			(void) fdcresult(fdc);
   1226 		}
   1227 
   1228 		/* fall through */
   1229 	case DORECAL:
   1230 		fdc->sc_state = RECALWAIT;
   1231 		fdc->sc_istate = ISTATE_SENSEI;
   1232 		fdc->sc_nstat = 0;
   1233 		/* recalibrate function */
   1234 		OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
   1235 		OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
   1236 		timeout(fdctimeout, fdc, 5 * hz);
   1237 		return 1;			/* will return later */
   1238 
   1239 	case RECALWAIT:
   1240 		untimeout(fdctimeout, fdc);
   1241 		fdc->sc_state = RECALCOMPLETE;
   1242 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1243 			/* allow 1/30 second for heads to settle */
   1244 			timeout(fdcpseudointr, fdc, hz / 30);
   1245 			return 1;		/* will return later */
   1246 		}
   1247 
   1248 	case RECALCOMPLETE:
   1249 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1250 #ifdef FD_DEBUG
   1251 			if (fdc_debug)
   1252 				fdcstatus(&fd->sc_dk.dk_dev, 2, "recalibrate failed");
   1253 #endif
   1254 			fdcretry(fdc);
   1255 			goto loop;
   1256 		}
   1257 		fd->sc_cylin = 0;
   1258 		goto doseek;
   1259 
   1260 	case MOTORWAIT:
   1261 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1262 			return 1;		/* time's not up yet */
   1263 		goto doseek;
   1264 
   1265 	default:
   1266 		fdcstatus(&fd->sc_dk.dk_dev, 0, "stray interrupt");
   1267 		return 1;
   1268 	}
   1269 #ifdef DIAGNOSTIC
   1270 	panic("fdcintr: impossible");
   1271 #endif
   1272 #undef	st0
   1273 #undef	st1
   1274 #undef	cyl
   1275 }
   1276 
   1277 void
   1278 fdcretry(fdc)
   1279 	struct fdc_softc *fdc;
   1280 {
   1281 	struct fd_softc *fd;
   1282 	struct buf *bp;
   1283 
   1284 	fd = fdc->sc_drives.tqh_first;
   1285 	bp = fd->sc_q.b_actf;
   1286 
   1287 	fdc->sc_overruns = 0;
   1288 
   1289 	switch (fdc->sc_errors) {
   1290 	case 0:
   1291 		/* try again */
   1292 		fdc->sc_state =
   1293 			(fdc->sc_flags & FDC_EIS) ? DOIO : SEEKCOMPLETE;
   1294 		break;
   1295 
   1296 	case 1: case 2: case 3:
   1297 		/* didn't work; try recalibrating */
   1298 		fdc->sc_state = DORECAL;
   1299 		break;
   1300 
   1301 	case 4:
   1302 		/* still no go; reset the bastard */
   1303 		fdc->sc_state = DORESET;
   1304 		break;
   1305 
   1306 	default:
   1307 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1308 		    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1309 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
   1310 		    fdc->sc_status[0], NE7_ST0BITS,
   1311 		    fdc->sc_status[1], NE7_ST1BITS,
   1312 		    fdc->sc_status[2], NE7_ST2BITS,
   1313 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1314 
   1315 		bp->b_flags |= B_ERROR;
   1316 		bp->b_error = EIO;
   1317 		fdfinish(fd, bp);
   1318 	}
   1319 	fdc->sc_errors++;
   1320 }
   1321 
   1322 int
   1323 fdsize(dev)
   1324 	dev_t dev;
   1325 {
   1326 
   1327 	/* Swapping to floppies would not make sense. */
   1328 	return -1;
   1329 }
   1330 
   1331 int
   1332 fddump()
   1333 {
   1334 
   1335 	/* Not implemented. */
   1336 	return EINVAL;
   1337 }
   1338 
   1339 int
   1340 fdioctl(dev, cmd, addr, flag)
   1341 	dev_t dev;
   1342 	u_long cmd;
   1343 	caddr_t addr;
   1344 	int flag;
   1345 {
   1346 	struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
   1347 	struct disklabel buffer;
   1348 	int error;
   1349 
   1350 	switch (cmd) {
   1351 	case DIOCGDINFO:
   1352 		bzero(&buffer, sizeof(buffer));
   1353 
   1354 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1355 		buffer.d_type = DTYPE_FLOPPY;
   1356 		buffer.d_secsize = FDC_BSIZE;
   1357 
   1358 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1359 			return EINVAL;
   1360 
   1361 		*(struct disklabel *)addr = buffer;
   1362 		return 0;
   1363 
   1364 	case DIOCWLABEL:
   1365 		if ((flag & FWRITE) == 0)
   1366 			return EBADF;
   1367 		/* XXX do something */
   1368 		return 0;
   1369 
   1370 	case DIOCWDINFO:
   1371 		if ((flag & FWRITE) == 0)
   1372 			return EBADF;
   1373 
   1374 		error = setdisklabel(&buffer, (struct disklabel *)addr, 0, NULL);
   1375 		if (error)
   1376 			return error;
   1377 
   1378 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1379 		return error;
   1380 
   1381 	case FDIOCEJECT:
   1382 		auxregbisc(AUXIO_FDS, AUXIO_FEJ);
   1383 		delay(10);
   1384 		auxregbisc(AUXIO_FEJ, AUXIO_FDS);
   1385 		return 0;
   1386 #ifdef DEBUG
   1387 	case _IO('f', 100):
   1388 		{
   1389 		int i;
   1390 		struct fdc_softc *fdc = (struct fdc_softc *)
   1391 					fd->sc_dk.dk_dev.dv_parent;
   1392 
   1393 		out_fdc(fdc, NE7CMD_DUMPREG);
   1394 		fdcresult(fdc);
   1395 		printf("dumpreg(%d regs): <", fdc->sc_nstat);
   1396 		for (i = 0; i < fdc->sc_nstat; i++)
   1397 			printf(" %x", fdc->sc_status[i]);
   1398 		printf(">\n");
   1399 		}
   1400 
   1401 		return 0;
   1402 	case _IOW('f', 101, int):
   1403 		((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent)->sc_cfg &=
   1404 			~CFG_THRHLD_MASK;
   1405 		((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent)->sc_cfg |=
   1406 			(*(int *)addr & CFG_THRHLD_MASK);
   1407 		fdconf(fd->sc_dk.dk_dev.dv_parent);
   1408 		return 0;
   1409 	case _IO('f', 102):
   1410 		{
   1411 		int i;
   1412 		struct fdc_softc *fdc = (struct fdc_softc *)
   1413 					fd->sc_dk.dk_dev.dv_parent;
   1414 		out_fdc(fdc, NE7CMD_SENSEI);
   1415 		fdcresult(fdc);
   1416 		printf("sensei(%d regs): <", fdc->sc_nstat);
   1417 		for (i=0; i< fdc->sc_nstat; i++)
   1418 			printf(" 0x%x", fdc->sc_status[i]);
   1419 		}
   1420 		printf(">\n");
   1421 		return 0;
   1422 #endif
   1423 	default:
   1424 		return ENOTTY;
   1425 	}
   1426 
   1427 #ifdef DIAGNOSTIC
   1428 	panic("fdioctl: impossible");
   1429 #endif
   1430 }
   1431