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