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