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