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