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