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