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