xref: /netbsd/sys/arch/sparc/dev/fd.c (revision c4a72b64)
1 /*	$NetBSD: fd.c,v 1.96 2002/11/01 11:31:53 mrg Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*-
40  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
41  * Copyright (c) 1995 Paul Kranenburg.
42  * Copyright (c) 1990 The Regents of the University of California.
43  * All rights reserved.
44  *
45  * This code is derived from software contributed to Berkeley by
46  * Don Ahn.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. All advertising materials mentioning features or use of this software
57  *    must display the following acknowledgement:
58  *	This product includes software developed by the University of
59  *	California, Berkeley and its contributors.
60  * 4. Neither the name of the University nor the names of its contributors
61  *    may be used to endorse or promote products derived from this software
62  *    without specific prior written permission.
63  *
64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74  * SUCH DAMAGE.
75  *
76  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
77  */
78 
79 #include "opt_ddb.h"
80 #include "opt_md.h"
81 
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/callout.h>
85 #include <sys/kernel.h>
86 #include <sys/file.h>
87 #include <sys/ioctl.h>
88 #include <sys/device.h>
89 #include <sys/disklabel.h>
90 #include <sys/dkstat.h>
91 #include <sys/disk.h>
92 #include <sys/fdio.h>
93 #include <sys/buf.h>
94 #include <sys/malloc.h>
95 #include <sys/proc.h>
96 #include <sys/uio.h>
97 #include <sys/stat.h>
98 #include <sys/syslog.h>
99 #include <sys/queue.h>
100 #include <sys/conf.h>
101 
102 #include <dev/cons.h>
103 
104 #include <uvm/uvm_extern.h>
105 
106 #include <machine/autoconf.h>
107 #include <machine/intr.h>
108 
109 #include <sparc/sparc/auxreg.h>
110 #include <sparc/dev/fdreg.h>
111 #include <sparc/dev/fdvar.h>
112 
113 #define FDUNIT(dev)	(minor(dev) / 8)
114 #define FDTYPE(dev)	(minor(dev) % 8)
115 
116 /* XXX misuse a flag to identify format operation */
117 #define B_FORMAT B_XXX
118 
119 #define FD_DEBUG
120 #ifdef FD_DEBUG
121 int	fdc_debug = 0;
122 #endif
123 
124 enum fdc_state {
125 	DEVIDLE = 0,
126 	MOTORWAIT,	/*  1 */
127 	DOSEEK,		/*  2 */
128 	SEEKWAIT,	/*  3 */
129 	SEEKTIMEDOUT,	/*  4 */
130 	SEEKCOMPLETE,	/*  5 */
131 	DOIO,		/*  6 */
132 	IOCOMPLETE,	/*  7 */
133 	IOTIMEDOUT,	/*  8 */
134 	IOCLEANUPWAIT,	/*  9 */
135 	IOCLEANUPTIMEDOUT,/*10 */
136 	DORESET,	/* 11 */
137 	RESETCOMPLETE,	/* 12 */
138 	RESETTIMEDOUT,	/* 13 */
139 	DORECAL,	/* 14 */
140 	RECALWAIT,	/* 15 */
141 	RECALTIMEDOUT,	/* 16 */
142 	RECALCOMPLETE,	/* 17 */
143 };
144 
145 /* software state, per controller */
146 struct fdc_softc {
147 	struct device	sc_dev;		/* boilerplate */
148 	bus_space_tag_t	sc_bustag;
149 
150 	struct callout sc_timo_ch;	/* timeout callout */
151 	struct callout sc_intr_ch;	/* pseudo-intr callout */
152 
153 	struct fd_softc *sc_fd[4];	/* pointers to children */
154 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
155 	enum fdc_state	sc_state;
156 	int		sc_flags;
157 #define FDC_82077		0x01
158 #define FDC_NEEDHEADSETTLE	0x02
159 #define FDC_EIS			0x04
160 #define FDC_NEEDMOTORWAIT	0x08
161 	int		sc_errors;		/* number of retries so far */
162 	int		sc_overruns;		/* number of DMA overruns */
163 	int		sc_cfg;			/* current configuration */
164 	struct fdcio	sc_io;
165 #define sc_handle	sc_io.fdcio_handle
166 #define sc_reg_msr	sc_io.fdcio_reg_msr
167 #define sc_reg_fifo	sc_io.fdcio_reg_fifo
168 #define sc_reg_dor	sc_io.fdcio_reg_dor
169 #define sc_reg_drs	sc_io.fdcio_reg_msr
170 #define sc_itask	sc_io.fdcio_itask
171 #define sc_istatus	sc_io.fdcio_istatus
172 #define sc_data		sc_io.fdcio_data
173 #define sc_tc		sc_io.fdcio_tc
174 #define sc_nstat	sc_io.fdcio_nstat
175 #define sc_status	sc_io.fdcio_status
176 #define sc_intrcnt	sc_io.fdcio_intrcnt
177 };
178 
179 extern	struct fdcio	*fdciop;	/* I/O descriptor used in fdintr.s */
180 
181 /* controller driver configuration */
182 int	fdcmatch_mainbus __P((struct device *, struct cfdata *, void *));
183 int	fdcmatch_obio __P((struct device *, struct cfdata *, void *));
184 void	fdcattach_mainbus __P((struct device *, struct device *, void *));
185 void	fdcattach_obio __P((struct device *, struct device *, void *));
186 
187 int	fdcattach __P((struct fdc_softc *, int));
188 
189 CFATTACH_DECL(fdc_mainbus, sizeof(struct fdc_softc),
190     fdcmatch_mainbus, fdcattach_mainbus, NULL, NULL);
191 
192 CFATTACH_DECL(fdc_obio, sizeof(struct fdc_softc),
193     fdcmatch_obio, fdcattach_obio, NULL, NULL);
194 
195 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
196 
197 /*
198  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
199  * we tell them apart.
200  */
201 struct fd_type {
202 	int	sectrac;	/* sectors per track */
203 	int	heads;		/* number of heads */
204 	int	seccyl;		/* sectors per cylinder */
205 	int	secsize;	/* size code for sectors */
206 	int	datalen;	/* data len when secsize = 0 */
207 	int	steprate;	/* step rate and head unload time */
208 	int	gap1;		/* gap len between sectors */
209 	int	gap2;		/* formatting gap */
210 	int	cylinders;	/* total num of cylinders */
211 	int	size;		/* size of disk in sectors */
212 	int	step;		/* steps per cylinder */
213 	int	rate;		/* transfer speed code */
214 	int	fillbyte;	/* format fill byte */
215 	int	interleave;	/* interleave factor (formatting) */
216 	char	*name;
217 };
218 
219 /* The order of entries in the following table is important -- BEWARE! */
220 struct fd_type fd_types[] = {
221 	{ 18,2,36,2,0xff,0xcf,0x1b,0x54,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette */
222 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5" 720kB diskette */
223 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
224 	{  8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,0xf6,1, "1.2MB/NEC" } /* 1.2 MB japanese format */
225 };
226 
227 /* software state, per disk (with up to 4 disks per ctlr) */
228 struct fd_softc {
229 	struct device	sc_dv;		/* generic device info */
230 	struct disk	sc_dk;		/* generic disk info */
231 
232 	struct fd_type *sc_deftype;	/* default type descriptor */
233 	struct fd_type *sc_type;	/* current type descriptor */
234 
235 	struct callout sc_motoron_ch;
236 	struct callout sc_motoroff_ch;
237 
238 	daddr_t	sc_blkno;	/* starting block number */
239 	int sc_bcount;		/* byte count left */
240 	int sc_skip;		/* bytes already transferred */
241 	int sc_nblks;		/* number of blocks currently transferring */
242 	int sc_nbytes;		/* number of bytes currently transferring */
243 
244 	int sc_drive;		/* physical unit number */
245 	int sc_flags;
246 #define	FD_OPEN		0x01		/* it's open */
247 #define	FD_MOTOR	0x02		/* motor should be on */
248 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
249 	int sc_cylin;		/* where we think the head is */
250 	int sc_opts;		/* user-set options */
251 
252 	void	*sc_sdhook;	/* shutdownhook cookie */
253 
254 	TAILQ_ENTRY(fd_softc) sc_drivechain;
255 	int sc_ops;		/* I/O ops since last switch */
256 	struct bufq_state sc_q;	/* pending I/O requests */
257 	int sc_active;		/* number of active I/O requests */
258 };
259 
260 /* floppy driver configuration */
261 int	fdmatch __P((struct device *, struct cfdata *, void *));
262 void	fdattach __P((struct device *, struct device *, void *));
263 
264 CFATTACH_DECL(fd, sizeof(struct fd_softc),
265     fdmatch, fdattach, NULL, NULL);
266 
267 extern struct cfdriver fd_cd;
268 
269 dev_type_open(fdopen);
270 dev_type_close(fdclose);
271 dev_type_read(fdread);
272 dev_type_write(fdwrite);
273 dev_type_ioctl(fdioctl);
274 dev_type_strategy(fdstrategy);
275 
276 const struct bdevsw fd_bdevsw = {
277 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
278 };
279 
280 const struct cdevsw fd_cdevsw = {
281 	fdopen, fdclose, fdread, fdwrite, fdioctl,
282 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
283 };
284 
285 void fdgetdisklabel __P((dev_t));
286 int fd_get_parms __P((struct fd_softc *));
287 void fdstart __P((struct fd_softc *));
288 int fdprint __P((void *, const char *));
289 
290 struct dkdriver fddkdriver = { fdstrategy };
291 
292 struct	fd_type *fd_nvtotype __P((char *, int, int));
293 void	fd_set_motor __P((struct fdc_softc *fdc));
294 void	fd_motor_off __P((void *arg));
295 void	fd_motor_on __P((void *arg));
296 int	fdcresult __P((struct fdc_softc *fdc));
297 int	fdc_wrfifo __P((struct fdc_softc *fdc, u_char x));
298 void	fdcstart __P((struct fdc_softc *fdc));
299 void	fdcstatus __P((struct fdc_softc *fdc, char *s));
300 void	fdc_reset __P((struct fdc_softc *fdc));
301 void	fdctimeout __P((void *arg));
302 void	fdcpseudointr __P((void *arg));
303 int	fdc_c_hwintr __P((void *));
304 void	fdchwintr __P((void));
305 int	fdcswintr __P((void *));
306 int	fdcstate __P((struct fdc_softc *));
307 void	fdcretry __P((struct fdc_softc *fdc));
308 void	fdfinish __P((struct fd_softc *fd, struct buf *bp));
309 int	fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
310 void	fd_do_eject __P((struct fd_softc *));
311 void	fd_mountroot_hook __P((struct device *));
312 static int fdconf __P((struct fdc_softc *));
313 static void establish_chip_type __P((
314 		struct fdc_softc *,
315 		bus_space_tag_t,
316 		bus_addr_t,
317 		bus_size_t,
318 		bus_space_handle_t));
319 
320 
321 #if PIL_FDSOFT == 4
322 #define IE_FDSOFT	IE_L4
323 #else
324 #error 4
325 #endif
326 
327 #if defined(SUN4M)
328 #define FD_SET_SWINTR do {		\
329 	if (CPU_ISSUN4M)		\
330 		raise(0, PIL_FDSOFT);	\
331 	else				\
332 		ienab_bis(IE_L4);	\
333 } while(0)
334 #else
335 #define FD_SET_SWINTR ienab_bis(IE_FDSOFT)
336 #endif /* defined(SUN4M) */
337 
338 #define OBP_FDNAME	(CPU_ISSUN4M ? "SUNW,fdtwo" : "fd")
339 
340 int
341 fdcmatch_mainbus(parent, match, aux)
342 	struct device *parent;
343 	struct cfdata *match;
344 	void *aux;
345 {
346 	struct mainbus_attach_args *ma = aux;
347 
348 	/*
349 	 * Floppy controller is on mainbus on sun4c.
350 	 */
351 	if (!CPU_ISSUN4C)
352 		return (0);
353 
354 	/* sun4c PROMs call the controller "fd" */
355 	if (strcmp("fd", ma->ma_name) != 0)
356 		return (0);
357 
358 	return (bus_space_probe(ma->ma_bustag,
359 				ma->ma_paddr,
360 				1,	/* probe size */
361 				0,	/* offset */
362 				0,	/* flags */
363 				NULL, NULL));
364 }
365 
366 int
367 fdcmatch_obio(parent, match, aux)
368 	struct device *parent;
369 	struct cfdata *match;
370 	void *aux;
371 {
372 	union obio_attach_args *uoba = aux;
373 	struct sbus_attach_args *sa;
374 
375 	/*
376 	 * Floppy controller is on obio on sun4m.
377 	 */
378 	if (uoba->uoba_isobio4 != 0)
379 		return (0);
380 
381 	sa = &uoba->uoba_sbus;
382 
383 	/* sun4m PROMs call the controller "SUNW,fdtwo" */
384 	if (strcmp("SUNW,fdtwo", sa->sa_name) != 0)
385 		return (0);
386 
387 	return (bus_space_probe(sa->sa_bustag,
388 			sbus_bus_addr(sa->sa_bustag,
389 					sa->sa_slot, sa->sa_offset),
390 			1,	/* probe size */
391 			0,	/* offset */
392 			0,	/* flags */
393 			NULL, NULL));
394 }
395 
396 static void
397 establish_chip_type(fdc, tag, addr, size, handle)
398 	struct fdc_softc	*fdc;
399 	bus_space_tag_t		tag;
400 	bus_addr_t		addr;
401 	bus_size_t		size;
402 	bus_space_handle_t	handle;
403 {
404 	u_int8_t v;
405 
406 	/*
407 	 * This hack from Chris Torek: apparently DOR really
408 	 * addresses MSR/DRS on a 82072.
409 	 * We used to rely on the VERSION command to tell the
410 	 * difference (which did not work).
411 	 */
412 
413 	/* First, check the size of the register bank */
414 	if (size < 8)
415 		/* It isn't a 82077 */
416 		return;
417 
418 	/* Then probe the DOR register offset */
419 	if (bus_space_probe(tag, addr,
420 			    1,			/* probe size */
421 			    FDREG77_DOR,	/* offset */
422 			    0,			/* flags */
423 			    NULL, NULL) == 0) {
424 
425 		/* It isn't a 82077 */
426 		return;
427 	}
428 
429 	v = bus_space_read_1(tag, handle, FDREG77_DOR);
430 	if (v == NE7_RQM) {
431 		/*
432 		 * Value in DOR looks like it's really MSR
433 		 */
434 		bus_space_write_1(tag, handle, FDREG77_DOR, FDC_250KBPS);
435 		v = bus_space_read_1(tag, handle, FDREG77_DOR);
436 		if (v == NE7_RQM) {
437 			/*
438 			 * The value in the DOR didn't stick;
439 			 * it isn't a 82077
440 			 */
441 			return;
442 		}
443 	}
444 
445 	fdc->sc_flags |= FDC_82077;
446 }
447 
448 /*
449  * Arguments passed between fdcattach and fdprobe.
450  */
451 struct fdc_attach_args {
452 	int fa_drive;
453 	struct fd_type *fa_deftype;
454 };
455 
456 /*
457  * Print the location of a disk drive (called just before attaching the
458  * the drive).  If `fdc' is not NULL, the drive was found but was not
459  * in the system config file; print the drive name as well.
460  * Return QUIET (config_find ignores this if the device was configured) to
461  * avoid printing `fdN not configured' messages.
462  */
463 int
464 fdprint(aux, fdc)
465 	void *aux;
466 	const char *fdc;
467 {
468 	register struct fdc_attach_args *fa = aux;
469 
470 	if (!fdc)
471 		printf(" drive %d", fa->fa_drive);
472 	return (QUIET);
473 }
474 
475 /*
476  * Configure several parameters and features on the FDC.
477  * Return 0 on success.
478  */
479 static int
480 fdconf(fdc)
481 	struct fdc_softc *fdc;
482 {
483 	int	vroom;
484 
485 	if (fdc_wrfifo(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
486 		return (-1);
487 
488 	/*
489 	 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
490 	 * the PROM thinks is appropriate.
491 	 */
492 	if ((vroom = fdc->sc_status[7]) == 0)
493 		vroom = 0x64;
494 
495 	/* Configure controller to use FIFO and Implied Seek */
496 	if (fdc_wrfifo(fdc, NE7CMD_CFG) != 0)
497 		return (-1);
498 	if (fdc_wrfifo(fdc, vroom) != 0)
499 		return (-1);
500 	if (fdc_wrfifo(fdc, fdc->sc_cfg) != 0)
501 		return (-1);
502 	if (fdc_wrfifo(fdc, 0) != 0)	/* PRETRK */
503 		return (-1);
504 	/* No result phase for the NE7CMD_CFG command */
505 
506 	if ((fdc->sc_flags & FDC_82077) != 0) {
507 		/* Lock configuration across soft resets. */
508 		if (fdc_wrfifo(fdc, NE7CMD_LOCK | CFG_LOCK) != 0 ||
509 		    fdcresult(fdc) != 1) {
510 #ifdef DEBUG
511 			printf("fdconf: CFGLOCK failed");
512 #endif
513 			return (-1);
514 		}
515 	}
516 
517 	return (0);
518 #if 0
519 	if (fdc_wrfifo(fdc, NE7CMD_VERSION) == 0 &&
520 	    fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
521 		if (fdc_debug)
522 			printf("[version cmd]");
523 	}
524 #endif
525 }
526 
527 void
528 fdcattach_mainbus(parent, self, aux)
529 	struct device *parent, *self;
530 	void *aux;
531 {
532 	struct fdc_softc *fdc = (void *)self;
533 	struct mainbus_attach_args *ma = aux;
534 
535 	fdc->sc_bustag = ma->ma_bustag;
536 
537 	if (bus_space_map(
538 			ma->ma_bustag,
539 			ma->ma_paddr,
540 			ma->ma_size,
541 			BUS_SPACE_MAP_LINEAR,
542 			&fdc->sc_handle) != 0) {
543 		printf("%s: cannot map registers\n", self->dv_xname);
544 		return;
545 	}
546 
547 	establish_chip_type(fdc,
548 			    ma->ma_bustag,
549 			    ma->ma_paddr,
550 			    ma->ma_size,
551 			    fdc->sc_handle);
552 
553 	if (fdcattach(fdc, ma->ma_pri) != 0)
554 		bus_space_unmap(ma->ma_bustag, fdc->sc_handle, ma->ma_size);
555 }
556 
557 void
558 fdcattach_obio(parent, self, aux)
559 	struct device *parent, *self;
560 	void *aux;
561 {
562 	struct fdc_softc *fdc = (void *)self;
563 	union obio_attach_args *uoba = aux;
564 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
565 
566 	if (sa->sa_nintr == 0) {
567 		printf(": no interrupt line configured\n");
568 		return;
569 	}
570 
571 	fdc->sc_bustag = sa->sa_bustag;
572 
573 	if (sbus_bus_map(sa->sa_bustag,
574 			 sa->sa_slot, sa->sa_offset, sa->sa_size,
575 			 BUS_SPACE_MAP_LINEAR, &fdc->sc_handle) != 0) {
576 		printf("%s: cannot map control registers\n",
577 			self->dv_xname);
578 		return;
579 	}
580 
581 	establish_chip_type(fdc,
582 		sa->sa_bustag,
583 		sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset),
584 		sa->sa_size,
585 		fdc->sc_handle);
586 
587 	if (strcmp(PROM_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
588 		printf(": no drives attached\n");
589 		return;
590 	}
591 
592 	if (fdcattach(fdc, sa->sa_pri) != 0)
593 		bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
594 }
595 
596 int
597 fdcattach(fdc, pri)
598 	struct fdc_softc *fdc;
599 	int pri;
600 {
601 	struct fdc_attach_args fa;
602 	int drive_attached;
603 	char code;
604 
605 	callout_init(&fdc->sc_timo_ch);
606 	callout_init(&fdc->sc_intr_ch);
607 
608 	fdc->sc_state = DEVIDLE;
609 	fdc->sc_itask = FDC_ITASK_NONE;
610 	fdc->sc_istatus = FDC_ISTATUS_NONE;
611 	fdc->sc_flags |= FDC_EIS;
612 	TAILQ_INIT(&fdc->sc_drives);
613 
614 	if ((fdc->sc_flags & FDC_82077) != 0) {
615 		fdc->sc_reg_msr = FDREG77_MSR;
616 		fdc->sc_reg_fifo = FDREG77_FIFO;
617 		fdc->sc_reg_dor = FDREG77_DOR;
618 		code = '7';
619 		fdc->sc_flags |= FDC_NEEDMOTORWAIT;
620 	} else {
621 		fdc->sc_reg_msr = FDREG72_MSR;
622 		fdc->sc_reg_fifo = FDREG72_FIFO;
623 		fdc->sc_reg_dor = 0;
624 		code = '2';
625 	}
626 
627 	printf(" softpri %d: chip 8207%c\n", PIL_FDSOFT, code);
628 
629 	/*
630 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
631 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
632 	 */
633 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
634 	if (fdconf(fdc) != 0) {
635 		printf("%s: no drives attached\n", fdc->sc_dev.dv_xname);
636 		return (-1);
637 	}
638 
639 	fdciop = &fdc->sc_io;
640 	if (bus_intr_establish(fdc->sc_bustag, pri, IPL_BIO,
641 			 BUS_INTR_ESTABLISH_FASTTRAP,
642 			 (int (*) __P((void *)))fdchwintr, NULL) == NULL) {
643 
644 		printf("%s: notice: no fast trap handler slot available\n",
645 			fdc->sc_dev.dv_xname);
646 		if (bus_intr_establish(fdc->sc_bustag, pri, IPL_BIO, 0,
647 				 fdc_c_hwintr, fdc) == NULL) {
648 			printf("%s: cannot register interrupt handler\n",
649 				fdc->sc_dev.dv_xname);
650 			return (-1);
651 		}
652 	}
653 
654 	if (bus_intr_establish(fdc->sc_bustag, PIL_FDSOFT, IPL_BIO,
655 			 BUS_INTR_ESTABLISH_SOFTINTR,
656 			 fdcswintr, fdc) == NULL) {
657 		printf("%s: cannot register interrupt handler\n",
658 			fdc->sc_dev.dv_xname);
659 		return (-1);
660 	}
661 
662 	evcnt_attach_dynamic(&fdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
663 	    fdc->sc_dev.dv_xname, "intr");
664 
665 	/* physical limit: four drives per controller. */
666 	drive_attached = 0;
667 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
668 		fa.fa_deftype = NULL;		/* unknown */
669 	fa.fa_deftype = &fd_types[0];		/* XXX */
670 		if (config_found(&fdc->sc_dev, (void *)&fa, fdprint) != NULL)
671 			drive_attached = 1;
672 	}
673 
674 	if (drive_attached == 0) {
675 		/* XXX - dis-establish interrupts here */
676 		/* return (-1); */
677 	}
678 
679 	return (0);
680 }
681 
682 int
683 fdmatch(parent, match, aux)
684 	struct device *parent;
685 	struct cfdata *match;
686 	void *aux;
687 {
688 	struct fdc_softc *fdc = (void *)parent;
689 	bus_space_tag_t t = fdc->sc_bustag;
690 	bus_space_handle_t h = fdc->sc_handle;
691 	struct fdc_attach_args *fa = aux;
692 	int drive = fa->fa_drive;
693 	int n, ok;
694 
695 	if (drive > 0)
696 		/* XXX - for now, punt on more than one drive */
697 		return (0);
698 
699 	if ((fdc->sc_flags & FDC_82077) != 0) {
700 		/* select drive and turn on motor */
701 		bus_space_write_1(t, h, fdc->sc_reg_dor,
702 				  drive | FDO_FRST | FDO_MOEN(drive));
703 		/* wait for motor to spin up */
704 		delay(250000);
705 	} else {
706 		auxregbisc(AUXIO4C_FDS, 0);
707 	}
708 	fdc->sc_nstat = 0;
709 	fdc_wrfifo(fdc, NE7CMD_RECAL);
710 	fdc_wrfifo(fdc, drive);
711 
712 	/* Wait for recalibration to complete */
713 	for (n = 0; n < 10000; n++) {
714 		u_int8_t v;
715 
716 		delay(1000);
717 		v = bus_space_read_1(t, h, fdc->sc_reg_msr);
718 		if ((v & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
719 			/* wait a bit longer till device *really* is ready */
720 			delay(100000);
721 			if (fdc_wrfifo(fdc, NE7CMD_SENSEI))
722 				break;
723 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
724 				/*
725 				 * Got `invalid command'; we interpret it
726 				 * to mean that the re-calibrate hasn't in
727 				 * fact finished yet
728 				 */
729 				continue;
730 			break;
731 		}
732 	}
733 	n = fdc->sc_nstat;
734 #ifdef FD_DEBUG
735 	if (fdc_debug) {
736 		int i;
737 		printf("fdprobe: %d stati:", n);
738 		for (i = 0; i < n; i++)
739 			printf(" 0x%x", fdc->sc_status[i]);
740 		printf("\n");
741 	}
742 #endif
743 	ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
744 
745 	/* turn off motor */
746 	if ((fdc->sc_flags & FDC_82077) != 0) {
747 		/* deselect drive and turn motor off */
748 		bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
749 	} else {
750 		auxregbisc(0, AUXIO4C_FDS);
751 	}
752 
753 	return (ok);
754 }
755 
756 /*
757  * Controller is working, and drive responded.  Attach it.
758  */
759 void
760 fdattach(parent, self, aux)
761 	struct device *parent, *self;
762 	void *aux;
763 {
764 	struct fdc_softc *fdc = (void *)parent;
765 	struct fd_softc *fd = (void *)self;
766 	struct fdc_attach_args *fa = aux;
767 	struct fd_type *type = fa->fa_deftype;
768 	int drive = fa->fa_drive;
769 
770 	callout_init(&fd->sc_motoron_ch);
771 	callout_init(&fd->sc_motoroff_ch);
772 
773 	/* XXX Allow `flags' to override device type? */
774 
775 	if (type)
776 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
777 		    type->cylinders, type->heads, type->sectrac);
778 	else
779 		printf(": density unknown\n");
780 
781 	bufq_alloc(&fd->sc_q, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER);
782 	fd->sc_cylin = -1;
783 	fd->sc_drive = drive;
784 	fd->sc_deftype = type;
785 	fdc->sc_fd[drive] = fd;
786 
787 	fdc_wrfifo(fdc, NE7CMD_SPECIFY);
788 	fdc_wrfifo(fdc, type->steprate);
789 	/* XXX head load time == 6ms */
790 	fdc_wrfifo(fdc, 6 | NE7_SPECIFY_NODMA);
791 
792 	/*
793 	 * Initialize and attach the disk structure.
794 	 */
795 	fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
796 	fd->sc_dk.dk_driver = &fddkdriver;
797 	disk_attach(&fd->sc_dk);
798 
799 	/*
800 	 * Establish a mountroot_hook anyway in case we booted
801 	 * with RB_ASKNAME and get selected as the boot device.
802 	 */
803 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
804 
805 	/* Make sure the drive motor gets turned off at shutdown time. */
806 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
807 }
808 
809 __inline struct fd_type *
810 fd_dev_to_type(fd, dev)
811 	struct fd_softc *fd;
812 	dev_t dev;
813 {
814 	int type = FDTYPE(dev);
815 
816 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
817 		return (NULL);
818 	return (type ? &fd_types[type - 1] : fd->sc_deftype);
819 }
820 
821 void
822 fdstrategy(bp)
823 	register struct buf *bp;	/* IO operation to perform */
824 {
825 	struct fd_softc *fd;
826 	int unit = FDUNIT(bp->b_dev);
827 	int sz;
828  	int s;
829 
830 	/* Valid unit, controller, and request? */
831 	if (unit >= fd_cd.cd_ndevs ||
832 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
833 	    bp->b_blkno < 0 ||
834 	    (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
835 	      (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
836 	     (bp->b_flags & B_FORMAT) == 0)) {
837 		bp->b_error = EINVAL;
838 		goto bad;
839 	}
840 
841 	/* If it's a null transfer, return immediately. */
842 	if (bp->b_bcount == 0)
843 		goto done;
844 
845 	sz = howmany(bp->b_bcount, DEV_BSIZE);
846 
847 	if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
848 		sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
849 		     - bp->b_blkno;
850 		if (sz == 0) {
851 			/* If exactly at end of disk, return EOF. */
852 			bp->b_resid = bp->b_bcount;
853 			goto done;
854 		}
855 		if (sz < 0) {
856 			/* If past end of disk, return EINVAL. */
857 			bp->b_error = EINVAL;
858 			goto bad;
859 		}
860 		/* Otherwise, truncate request. */
861 		bp->b_bcount = sz << DEV_BSHIFT;
862 	}
863 
864 	bp->b_rawblkno = bp->b_blkno;
865  	bp->b_cylinder = (bp->b_blkno * DEV_BSIZE) /
866 		      (FD_BSIZE(fd) * fd->sc_type->seccyl);
867 
868 #ifdef FD_DEBUG
869 	if (fdc_debug > 1)
870 	    printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n",
871 		    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder);
872 #endif
873 
874 	/* Queue transfer on drive, activate drive and controller if idle. */
875 	s = splbio();
876 	BUFQ_PUT(&fd->sc_q, bp);
877 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
878 	if (fd->sc_active == 0)
879 		fdstart(fd);
880 #ifdef DIAGNOSTIC
881 	else {
882 		struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
883 		if (fdc->sc_state == DEVIDLE) {
884 			printf("fdstrategy: controller inactive\n");
885 			fdcstart(fdc);
886 		}
887 	}
888 #endif
889 	splx(s);
890 	return;
891 
892 bad:
893 	bp->b_flags |= B_ERROR;
894 done:
895 	/* Toss transfer; we're done early. */
896 	biodone(bp);
897 }
898 
899 void
900 fdstart(fd)
901 	struct fd_softc *fd;
902 {
903 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
904 	int active = fdc->sc_drives.tqh_first != 0;
905 
906 	/* Link into controller queue. */
907 	fd->sc_active = 1;
908 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
909 
910 	/* If controller not already active, start it. */
911 	if (!active)
912 		fdcstart(fdc);
913 }
914 
915 void
916 fdfinish(fd, bp)
917 	struct fd_softc *fd;
918 	struct buf *bp;
919 {
920 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
921 
922 	/*
923 	 * Move this drive to the end of the queue to give others a `fair'
924 	 * chance.  We only force a switch if N operations are completed while
925 	 * another drive is waiting to be serviced, since there is a long motor
926 	 * startup delay whenever we switch.
927 	 */
928 	(void)BUFQ_GET(&fd->sc_q);
929 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
930 		fd->sc_ops = 0;
931 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
932 		if (BUFQ_PEEK(&fd->sc_q) != NULL) {
933 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
934 		} else
935 			fd->sc_active = 0;
936 	}
937 	bp->b_resid = fd->sc_bcount;
938 	fd->sc_skip = 0;
939 
940 	biodone(bp);
941 	/* turn off motor 5s from now */
942 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
943 	fdc->sc_state = DEVIDLE;
944 }
945 
946 void
947 fdc_reset(fdc)
948 	struct fdc_softc *fdc;
949 {
950 	bus_space_tag_t t = fdc->sc_bustag;
951 	bus_space_handle_t h = fdc->sc_handle;
952 
953 	if ((fdc->sc_flags & FDC_82077) != 0) {
954 		bus_space_write_1(t, h, fdc->sc_reg_dor,
955 				  FDO_FDMAEN | FDO_MOEN(0));
956 	}
957 
958 	bus_space_write_1(t, h, fdc->sc_reg_drs, DRS_RESET);
959 	delay(10);
960 	bus_space_write_1(t, h, fdc->sc_reg_drs, 0);
961 
962 	if ((fdc->sc_flags & FDC_82077) != 0) {
963 		bus_space_write_1(t, h, fdc->sc_reg_dor,
964 				  FDO_FRST | FDO_FDMAEN | FDO_DS);
965 	}
966 #ifdef FD_DEBUG
967 	if (fdc_debug)
968 		printf("fdc reset\n");
969 #endif
970 }
971 
972 void
973 fd_set_motor(fdc)
974 	struct fdc_softc *fdc;
975 {
976 	struct fd_softc *fd;
977 	u_char status;
978 	int n;
979 
980 	if ((fdc->sc_flags & FDC_82077) != 0) {
981 		status = FDO_FRST | FDO_FDMAEN;
982 		if ((fd = fdc->sc_drives.tqh_first) != NULL)
983 			status |= fd->sc_drive;
984 
985 		for (n = 0; n < 4; n++)
986 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
987 				status |= FDO_MOEN(n);
988 		bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
989 				  fdc->sc_reg_dor, status);
990 	} else {
991 
992 		for (n = 0; n < 4; n++) {
993 			if ((fd = fdc->sc_fd[n]) != NULL  &&
994 			    (fd->sc_flags & FD_MOTOR) != 0) {
995 				auxregbisc(AUXIO4C_FDS, 0);
996 				return;
997 			}
998 		}
999 		auxregbisc(0, AUXIO4C_FDS);
1000 	}
1001 }
1002 
1003 void
1004 fd_motor_off(arg)
1005 	void *arg;
1006 {
1007 	struct fd_softc *fd = arg;
1008 	int s;
1009 
1010 	s = splbio();
1011 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1012 	fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
1013 	splx(s);
1014 }
1015 
1016 void
1017 fd_motor_on(arg)
1018 	void *arg;
1019 {
1020 	struct fd_softc *fd = arg;
1021 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
1022 	int s;
1023 
1024 	s = splbio();
1025 	fd->sc_flags &= ~FD_MOTOR_WAIT;
1026 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
1027 		(void) fdcstate(fdc);
1028 	splx(s);
1029 }
1030 
1031 /*
1032  * Get status bytes off the FDC after a command has finished
1033  * Returns the number of status bytes read; -1 on error.
1034  * The return value is also stored in `sc_nstat'.
1035  */
1036 int
1037 fdcresult(fdc)
1038 	struct fdc_softc *fdc;
1039 {
1040 	bus_space_tag_t t = fdc->sc_bustag;
1041 	bus_space_handle_t h = fdc->sc_handle;
1042 	int j, n = 0;
1043 
1044 	for (j = 10000; j; j--) {
1045 		u_int8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
1046 		v &= (NE7_DIO | NE7_RQM | NE7_CB);
1047 		if (v == NE7_RQM)
1048 			return (fdc->sc_nstat = n);
1049 		if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
1050 			if (n >= sizeof(fdc->sc_status)) {
1051 				log(LOG_ERR, "fdcresult: overrun\n");
1052 				return (-1);
1053 			}
1054 			fdc->sc_status[n++] =
1055 				bus_space_read_1(t, h, fdc->sc_reg_fifo);
1056 		} else
1057 			delay(1);
1058 	}
1059 
1060 	log(LOG_ERR, "fdcresult: timeout\n");
1061 	return (fdc->sc_nstat = -1);
1062 }
1063 
1064 /*
1065  * Write a command byte to the FDC.
1066  * Returns 0 on success; -1 on failure (i.e. timeout)
1067  */
1068 int
1069 fdc_wrfifo(fdc, x)
1070 	struct fdc_softc *fdc;
1071 	u_int8_t x;
1072 {
1073 	bus_space_tag_t t = fdc->sc_bustag;
1074 	bus_space_handle_t h = fdc->sc_handle;
1075 	int i;
1076 
1077 	for (i = 100000; i-- > 0;) {
1078 		u_int8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
1079 		if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM) {
1080 			/* The chip is ready */
1081 			bus_space_write_1(t, h, fdc->sc_reg_fifo, x);
1082 			return (0);
1083 		}
1084 		delay(1);
1085 	}
1086 	return (-1);
1087 }
1088 
1089 int
1090 fdopen(dev, flags, fmt, p)
1091 	dev_t dev;
1092 	int flags, fmt;
1093 	struct proc *p;
1094 {
1095  	int unit, pmask;
1096 	struct fd_softc *fd;
1097 	struct fd_type *type;
1098 
1099 	unit = FDUNIT(dev);
1100 	if (unit >= fd_cd.cd_ndevs)
1101 		return (ENXIO);
1102 	fd = fd_cd.cd_devs[unit];
1103 	if (fd == NULL)
1104 		return (ENXIO);
1105 	type = fd_dev_to_type(fd, dev);
1106 	if (type == NULL)
1107 		return (ENXIO);
1108 
1109 	if ((fd->sc_flags & FD_OPEN) != 0 &&
1110 	    fd->sc_type != type)
1111 		return (EBUSY);
1112 
1113 	fd->sc_type = type;
1114 	fd->sc_cylin = -1;
1115 	fd->sc_flags |= FD_OPEN;
1116 
1117 	/*
1118 	 * Only update the disklabel if we're not open anywhere else.
1119 	 */
1120 	if (fd->sc_dk.dk_openmask == 0)
1121 		fdgetdisklabel(dev);
1122 
1123 	pmask = (1 << DISKPART(dev));
1124 
1125 	switch (fmt) {
1126 	case S_IFCHR:
1127 		fd->sc_dk.dk_copenmask |= pmask;
1128 		break;
1129 
1130 	case S_IFBLK:
1131 		fd->sc_dk.dk_bopenmask |= pmask;
1132 		break;
1133 	}
1134 	fd->sc_dk.dk_openmask =
1135 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1136 
1137 	return (0);
1138 }
1139 
1140 int
1141 fdclose(dev, flags, fmt, p)
1142 	dev_t dev;
1143 	int flags, fmt;
1144 	struct proc *p;
1145 {
1146 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1147 	int pmask = (1 << DISKPART(dev));
1148 
1149 	fd->sc_flags &= ~FD_OPEN;
1150 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
1151 
1152 	switch (fmt) {
1153 	case S_IFCHR:
1154 		fd->sc_dk.dk_copenmask &= ~pmask;
1155 		break;
1156 
1157 	case S_IFBLK:
1158 		fd->sc_dk.dk_bopenmask &= ~pmask;
1159 		break;
1160 	}
1161 	fd->sc_dk.dk_openmask =
1162 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1163 
1164 	return (0);
1165 }
1166 
1167 int
1168 fdread(dev, uio, flag)
1169         dev_t dev;
1170         struct uio *uio;
1171 	int flag;
1172 {
1173 
1174         return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
1175 }
1176 
1177 int
1178 fdwrite(dev, uio, flag)
1179         dev_t dev;
1180         struct uio *uio;
1181 	int flag;
1182 {
1183 
1184         return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
1185 }
1186 
1187 void
1188 fdcstart(fdc)
1189 	struct fdc_softc *fdc;
1190 {
1191 
1192 #ifdef DIAGNOSTIC
1193 	/* only got here if controller's drive queue was inactive; should
1194 	   be in idle state */
1195 	if (fdc->sc_state != DEVIDLE) {
1196 		printf("fdcstart: not idle\n");
1197 		return;
1198 	}
1199 #endif
1200 	(void) fdcstate(fdc);
1201 }
1202 
1203 void
1204 fdcstatus(fdc, s)
1205 	struct fdc_softc *fdc;
1206 	char *s;
1207 {
1208 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
1209 	int n;
1210 	char bits[64];
1211 
1212 	/* Just print last status */
1213 	n = fdc->sc_nstat;
1214 
1215 #if 0
1216 	/*
1217 	 * A 82072 seems to return <invalid command> on
1218 	 * gratuitous Sense Interrupt commands.
1219 	 */
1220 	if (n == 0 && (fdc->sc_flags & FDC_82077) != 0) {
1221 		fdc_wrfifo(fdc, NE7CMD_SENSEI);
1222 		(void) fdcresult(fdc);
1223 		n = 2;
1224 	}
1225 #endif
1226 
1227 	printf("%s: %s: state %d",
1228 		fd ? fd->sc_dv.dv_xname : "fdc", s, fdc->sc_state);
1229 
1230 	switch (n) {
1231 	case 0:
1232 		printf("\n");
1233 		break;
1234 	case 2:
1235 		printf(" (st0 %s cyl %d)\n",
1236 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
1237 		    bits, sizeof(bits)), fdc->sc_status[1]);
1238 		break;
1239 	case 7:
1240 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1241 		    NE7_ST0BITS, bits, sizeof(bits)));
1242 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1243 		    NE7_ST1BITS, bits, sizeof(bits)));
1244 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1245 		    NE7_ST2BITS, bits, sizeof(bits)));
1246 		printf(" cyl %d head %d sec %d)\n",
1247 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1248 		break;
1249 #ifdef DIAGNOSTIC
1250 	default:
1251 		printf(" fdcstatus: weird size: %d\n", n);
1252 		break;
1253 #endif
1254 	}
1255 }
1256 
1257 void
1258 fdctimeout(arg)
1259 	void *arg;
1260 {
1261 	struct fdc_softc *fdc = arg;
1262 	struct fd_softc *fd;
1263 	int s;
1264 
1265 	s = splbio();
1266 	fd = fdc->sc_drives.tqh_first;
1267 	if (fd == NULL) {
1268 		printf("%s: timeout but no I/O pending: state %d, istatus=%d\n",
1269 			fdc->sc_dev.dv_xname,
1270 			fdc->sc_state, fdc->sc_istatus);
1271 		fdc->sc_state = DEVIDLE;
1272 		goto out;
1273 	}
1274 
1275 	if (BUFQ_PEEK(&fd->sc_q) != NULL)
1276 		fdc->sc_state++;
1277 	else
1278 		fdc->sc_state = DEVIDLE;
1279 
1280 	(void) fdcstate(fdc);
1281 out:
1282 	splx(s);
1283 
1284 }
1285 
1286 void
1287 fdcpseudointr(arg)
1288 	void *arg;
1289 {
1290 	struct fdc_softc *fdc = arg;
1291 	int s;
1292 
1293 	/* Just ensure it has the right spl. */
1294 	s = splbio();
1295 	(void) fdcstate(fdc);
1296 	splx(s);
1297 }
1298 
1299 
1300 /*
1301  * hardware interrupt entry point: used only if no `fast trap' * (in-window)
1302  * handler is available. Unfortunately, we have no reliable way to
1303  * determine that the interrupt really came from the floppy controller;
1304  * just hope that the other devices that share this interrupt level
1305  * can do better..
1306  */
1307 int
1308 fdc_c_hwintr(arg)
1309 	void *arg;
1310 {
1311 	struct fdc_softc *fdc = arg;
1312 	bus_space_tag_t t = fdc->sc_bustag;
1313 	bus_space_handle_t h = fdc->sc_handle;
1314 
1315 	switch (fdc->sc_itask) {
1316 	case FDC_ITASK_NONE:
1317 		return (0);
1318 	case FDC_ITASK_SENSEI:
1319 		if (fdc_wrfifo(fdc, NE7CMD_SENSEI) != 0 || fdcresult(fdc) == -1)
1320 			fdc->sc_istatus = FDC_ISTATUS_ERROR;
1321 		else
1322 			fdc->sc_istatus = FDC_ISTATUS_DONE;
1323 		FD_SET_SWINTR;
1324 		return (1);
1325 	case FDC_ITASK_DMA:
1326 		/* Proceed with pseudo-dma below */
1327 		break;
1328 	default:
1329 		printf("fdc: stray hard interrupt: itask=%d\n", fdc->sc_itask);
1330 		fdc->sc_istatus = FDC_ISTATUS_SPURIOUS;
1331 		FD_SET_SWINTR;
1332 		return (1);
1333 	}
1334 
1335 	/*
1336 	 * Pseudo DMA in progress
1337 	 */
1338 	for (;;) {
1339 		u_int8_t msr;
1340 
1341 		msr = bus_space_read_1(t, h, fdc->sc_reg_msr);
1342 
1343 		if ((msr & NE7_RQM) == 0)
1344 			/* That's all this round */
1345 			break;
1346 
1347 		if ((msr & NE7_NDM) == 0) {
1348 			fdcresult(fdc);
1349 			fdc->sc_istatus = FDC_ISTATUS_DONE;
1350 			FD_SET_SWINTR;
1351 #ifdef FD_DEBUG
1352 			if (fdc_debug > 1)
1353 				printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
1354 #endif
1355 			break;
1356 		}
1357 
1358 		/* Another byte can be transferred */
1359 		if ((msr & NE7_DIO) != 0)
1360 			*fdc->sc_data =
1361 				bus_space_read_1(t, h, fdc->sc_reg_fifo);
1362 		else
1363 			bus_space_write_1(t, h, fdc->sc_reg_fifo,
1364 					  *fdc->sc_data);
1365 
1366 		fdc->sc_data++;
1367 		if (--fdc->sc_tc == 0) {
1368 			fdc->sc_istatus = FDC_ISTATUS_DONE;
1369 			FTC_FLIP;
1370 			fdcresult(fdc);
1371 			FD_SET_SWINTR;
1372 			break;
1373 		}
1374 	}
1375 	return (1);
1376 }
1377 
1378 int
1379 fdcswintr(arg)
1380 	void *arg;
1381 {
1382 	struct fdc_softc *fdc = arg;
1383 	int s;
1384 
1385 	if (fdc->sc_istatus == FDC_ISTATUS_NONE)
1386 		/* This (software) interrupt is not for us */
1387 		return (0);
1388 
1389 	switch (fdc->sc_istatus) {
1390 	case FDC_ISTATUS_ERROR:
1391 		printf("fdc: ierror status: state %d\n", fdc->sc_state);
1392 		break;
1393 	case FDC_ISTATUS_SPURIOUS:
1394 		printf("fdc: spurious interrupt: state %d\n", fdc->sc_state);
1395 		break;
1396 	}
1397 
1398 	s = splbio();
1399 	fdcstate(fdc);
1400 	splx(s);
1401 	return (1);
1402 }
1403 
1404 int
1405 fdcstate(fdc)
1406 	struct fdc_softc *fdc;
1407 {
1408 #define	st0	fdc->sc_status[0]
1409 #define	st1	fdc->sc_status[1]
1410 #define	cyl	fdc->sc_status[1]
1411 #define FDC_WRFIFO(fdc, c) do {			\
1412 	if (fdc_wrfifo(fdc, (c))) {		\
1413 		goto xxx;			\
1414 	}					\
1415 } while(0)
1416 
1417 	struct fd_softc *fd;
1418 	struct buf *bp;
1419 	int read, head, sec, nblks;
1420 	struct fd_type *type;
1421 	struct ne7_fd_formb *finfo = NULL;
1422 
1423 	if (fdc->sc_istatus == FDC_ISTATUS_ERROR) {
1424 		/* Prevent loop if the reset sequence produces errors */
1425 		if (fdc->sc_state != RESETCOMPLETE &&
1426 		    fdc->sc_state != RECALWAIT &&
1427 		    fdc->sc_state != RECALCOMPLETE)
1428 			fdc->sc_state = DORESET;
1429 	}
1430 
1431 	/* Clear I task/status field */
1432 	fdc->sc_istatus = FDC_ISTATUS_NONE;
1433 	fdc->sc_itask = FDC_ITASK_NONE;
1434 
1435 loop:
1436 	/* Is there a drive for the controller to do a transfer with? */
1437 	fd = fdc->sc_drives.tqh_first;
1438 	if (fd == NULL) {
1439 		fdc->sc_state = DEVIDLE;
1440  		return (0);
1441 	}
1442 
1443 	/* Is there a transfer to this drive?  If not, deactivate drive. */
1444 	bp = BUFQ_PEEK(&fd->sc_q);
1445 	if (bp == NULL) {
1446 		fd->sc_ops = 0;
1447 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1448 		fd->sc_active = 0;
1449 		goto loop;
1450 	}
1451 
1452 	if (bp->b_flags & B_FORMAT)
1453 		finfo = (struct ne7_fd_formb *)bp->b_data;
1454 
1455 	switch (fdc->sc_state) {
1456 	case DEVIDLE:
1457 		fdc->sc_errors = 0;
1458 		fd->sc_skip = 0;
1459 		fd->sc_bcount = bp->b_bcount;
1460 		fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
1461 		callout_stop(&fd->sc_motoroff_ch);
1462 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1463 			fdc->sc_state = MOTORWAIT;
1464 			return (1);
1465 		}
1466 		if ((fd->sc_flags & FD_MOTOR) == 0) {
1467 			/* Turn on the motor, being careful about pairing. */
1468 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1469 			if (ofd && ofd->sc_flags & FD_MOTOR) {
1470 				callout_stop(&ofd->sc_motoroff_ch);
1471 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1472 			}
1473 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1474 			fd_set_motor(fdc);
1475 			fdc->sc_state = MOTORWAIT;
1476 			if ((fdc->sc_flags & FDC_NEEDMOTORWAIT) != 0) { /*XXX*/
1477 				/* Allow .25s for motor to stabilize. */
1478 				callout_reset(&fd->sc_motoron_ch, hz / 4,
1479 				    fd_motor_on, fd);
1480 			} else {
1481 				fd->sc_flags &= ~FD_MOTOR_WAIT;
1482 				goto loop;
1483 			}
1484 			return (1);
1485 		}
1486 		/* Make sure the right drive is selected. */
1487 		fd_set_motor(fdc);
1488 
1489 		/*FALLTHROUGH*/
1490 	case DOSEEK:
1491 	doseek:
1492 		if ((fdc->sc_flags & FDC_EIS) &&
1493 		    (bp->b_flags & B_FORMAT) == 0) {
1494 			fd->sc_cylin = bp->b_cylinder;
1495 			/* We use implied seek */
1496 			goto doio;
1497 		}
1498 
1499 		if (fd->sc_cylin == bp->b_cylinder)
1500 			goto doio;
1501 
1502 		fd->sc_cylin = -1;
1503 		fdc->sc_state = SEEKWAIT;
1504 		fdc->sc_nstat = 0;
1505 
1506 		fd->sc_dk.dk_seek++;
1507 
1508 		disk_busy(&fd->sc_dk);
1509 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1510 
1511 		/* specify command */
1512 		FDC_WRFIFO(fdc, NE7CMD_SPECIFY);
1513 		FDC_WRFIFO(fdc, fd->sc_type->steprate);
1514 		/* XXX head load time == 6ms */
1515 		FDC_WRFIFO(fdc, 6 | NE7_SPECIFY_NODMA);
1516 
1517 		fdc->sc_itask = FDC_ITASK_SENSEI;
1518 		/* seek function */
1519 		FDC_WRFIFO(fdc, NE7CMD_SEEK);
1520 		FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
1521 		FDC_WRFIFO(fdc, bp->b_cylinder * fd->sc_type->step);
1522 		return (1);
1523 
1524 	case DOIO:
1525 	doio:
1526 		if (finfo != NULL)
1527 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1528 				      (char *)finfo;
1529 		type = fd->sc_type;
1530 		sec = fd->sc_blkno % type->seccyl;
1531 		nblks = type->seccyl - sec;
1532 		nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
1533 		nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
1534 		fd->sc_nblks = nblks;
1535 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
1536 		head = sec / type->sectrac;
1537 		sec -= head * type->sectrac;
1538 #ifdef DIAGNOSTIC
1539 		{int block;
1540 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1541 		 if (block != fd->sc_blkno) {
1542 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1543 #ifdef DDB
1544 			 Debugger();
1545 #endif
1546 		 }}
1547 #endif
1548 		read = bp->b_flags & B_READ;
1549 
1550 		/* Setup for pseudo DMA */
1551 		fdc->sc_data = bp->b_data + fd->sc_skip;
1552 		fdc->sc_tc = fd->sc_nbytes;
1553 
1554 		bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
1555 				  fdc->sc_reg_drs, type->rate);
1556 #ifdef FD_DEBUG
1557 		if (fdc_debug > 1)
1558 			printf("fdcstate: doio: %s drive %d "
1559 				"track %d head %d sec %d nblks %d\n",
1560 				finfo ? "format" :
1561 					(read ? "read" : "write"),
1562 				fd->sc_drive, fd->sc_cylin, head, sec, nblks);
1563 #endif
1564 		fdc->sc_state = IOCOMPLETE;
1565 		fdc->sc_itask = FDC_ITASK_DMA;
1566 		fdc->sc_nstat = 0;
1567 
1568 		disk_busy(&fd->sc_dk);
1569 
1570 		/* allow 3 seconds for operation */
1571 		callout_reset(&fdc->sc_timo_ch, 3 * hz, fdctimeout, fdc);
1572 
1573 		if (finfo != NULL) {
1574 			/* formatting */
1575 			FDC_WRFIFO(fdc, NE7CMD_FORMAT);
1576 			FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1577 			FDC_WRFIFO(fdc, finfo->fd_formb_secshift);
1578 			FDC_WRFIFO(fdc, finfo->fd_formb_nsecs);
1579 			FDC_WRFIFO(fdc, finfo->fd_formb_gaplen);
1580 			FDC_WRFIFO(fdc, finfo->fd_formb_fillbyte);
1581 		} else {
1582 			if (read)
1583 				FDC_WRFIFO(fdc, NE7CMD_READ);
1584 			else
1585 				FDC_WRFIFO(fdc, NE7CMD_WRITE);
1586 			FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1587 			FDC_WRFIFO(fdc, fd->sc_cylin);	/*track*/
1588 			FDC_WRFIFO(fdc, head);
1589 			FDC_WRFIFO(fdc, sec + 1);	/*sector+1*/
1590 			FDC_WRFIFO(fdc, type->secsize);/*sector size*/
1591 			FDC_WRFIFO(fdc, type->sectrac);/*secs/track*/
1592 			FDC_WRFIFO(fdc, type->gap1);	/*gap1 size*/
1593 			FDC_WRFIFO(fdc, type->datalen);/*data length*/
1594 		}
1595 
1596 		return (1);				/* will return later */
1597 
1598 	case SEEKWAIT:
1599 		callout_stop(&fdc->sc_timo_ch);
1600 		fdc->sc_state = SEEKCOMPLETE;
1601 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1602 			/* allow 1/50 second for heads to settle */
1603 			callout_reset(&fdc->sc_intr_ch, hz / 50,
1604 			    fdcpseudointr, fdc);
1605 			return (1);		/* will return later */
1606 		}
1607 		/*FALLTHROUGH*/
1608 	case SEEKCOMPLETE:
1609 		/* no data on seek */
1610 		disk_unbusy(&fd->sc_dk, 0, 0);
1611 
1612 		/* Make sure seek really happened. */
1613 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1614 		    cyl != bp->b_cylinder * fd->sc_type->step) {
1615 #ifdef FD_DEBUG
1616 			if (fdc_debug)
1617 				fdcstatus(fdc, "seek failed");
1618 #endif
1619 			fdcretry(fdc);
1620 			goto loop;
1621 		}
1622 		fd->sc_cylin = bp->b_cylinder;
1623 		goto doio;
1624 
1625 	case IOTIMEDOUT:
1626 		/*
1627 		 * Try to abort the I/O operation without resetting
1628 		 * the chip first.  Poke TC and arrange to pick up
1629 		 * the timed out I/O command's status.
1630 		 */
1631 		fdc->sc_itask = FDC_ITASK_RESULT;
1632 		fdc->sc_state = IOCLEANUPWAIT;
1633 		fdc->sc_nstat = 0;
1634 		/* 1/10 second should be enough */
1635 		callout_reset(&fdc->sc_timo_ch, hz / 10, fdctimeout, fdc);
1636 		FTC_FLIP;
1637 		return (1);
1638 
1639 	case IOCLEANUPTIMEDOUT:
1640 	case SEEKTIMEDOUT:
1641 	case RECALTIMEDOUT:
1642 	case RESETTIMEDOUT:
1643 		fdcstatus(fdc, "timeout");
1644 
1645 		/* All other timeouts always roll through to a chip reset */
1646 		fdcretry(fdc);
1647 
1648 		/* Force reset, no matter what fdcretry() says */
1649 		fdc->sc_state = DORESET;
1650 		goto loop;
1651 
1652 	case IOCLEANUPWAIT: /* IO FAILED, cleanup succeeded */
1653 		callout_stop(&fdc->sc_timo_ch);
1654 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1655 		    (bp->b_flags & B_READ));
1656 		fdcretry(fdc);
1657 		goto loop;
1658 
1659 	case IOCOMPLETE: /* IO DONE, post-analyze */
1660 		callout_stop(&fdc->sc_timo_ch);
1661 
1662 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1663 		    (bp->b_flags & B_READ));
1664 
1665 		if (fdc->sc_nstat != 7 || st1 != 0 ||
1666 		    ((st0 & 0xf8) != 0 &&
1667 		     ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
1668 #ifdef FD_DEBUG
1669 			if (fdc_debug) {
1670 				fdcstatus(fdc,
1671 					bp->b_flags & B_READ
1672 					? "read failed" : "write failed");
1673 				printf("blkno %d nblks %d nstat %d tc %d\n",
1674 				       fd->sc_blkno, fd->sc_nblks,
1675 				       fdc->sc_nstat, fdc->sc_tc);
1676 			}
1677 #endif
1678 			if (fdc->sc_nstat == 7 &&
1679 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1680 
1681 				/*
1682 				 * Silently retry overruns if no other
1683 				 * error bit is set. Adjust threshold.
1684 				 */
1685 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1686 				if (thr < 15) {
1687 					thr++;
1688 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1689 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1690 #ifdef FD_DEBUG
1691 					if (fdc_debug)
1692 						printf("fdc: %d -> threshold\n", thr);
1693 #endif
1694 					fdconf(fdc);
1695 					fdc->sc_overruns = 0;
1696 				}
1697 				if (++fdc->sc_overruns < 3) {
1698 					fdc->sc_state = DOIO;
1699 					goto loop;
1700 				}
1701 			}
1702 			fdcretry(fdc);
1703 			goto loop;
1704 		}
1705 		if (fdc->sc_errors) {
1706 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
1707 			    fd->sc_skip / FD_BSIZE(fd),
1708 			    (struct disklabel *)NULL);
1709 			printf("\n");
1710 			fdc->sc_errors = 0;
1711 		} else {
1712 			if (--fdc->sc_overruns < -20) {
1713 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1714 				if (thr > 0) {
1715 					thr--;
1716 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1717 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1718 #ifdef FD_DEBUG
1719 					if (fdc_debug)
1720 						printf("fdc: %d -> threshold\n", thr);
1721 #endif
1722 					fdconf(fdc);
1723 				}
1724 				fdc->sc_overruns = 0;
1725 			}
1726 		}
1727 		fd->sc_blkno += fd->sc_nblks;
1728 		fd->sc_skip += fd->sc_nbytes;
1729 		fd->sc_bcount -= fd->sc_nbytes;
1730 		if (finfo == NULL && fd->sc_bcount > 0) {
1731 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1732 			goto doseek;
1733 		}
1734 		fdfinish(fd, bp);
1735 		goto loop;
1736 
1737 	case DORESET:
1738 		/* try a reset, keep motor on */
1739 		fd_set_motor(fdc);
1740 		delay(100);
1741 		fdc->sc_nstat = 0;
1742 		fdc->sc_itask = FDC_ITASK_SENSEI;
1743 		fdc->sc_state = RESETCOMPLETE;
1744 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1745 		fdc_reset(fdc);
1746 		return (1);			/* will return later */
1747 
1748 	case RESETCOMPLETE:
1749 		callout_stop(&fdc->sc_timo_ch);
1750 		fdconf(fdc);
1751 
1752 		/* FALLTHROUGH */
1753 	case DORECAL:
1754 		fdc->sc_state = RECALWAIT;
1755 		fdc->sc_itask = FDC_ITASK_SENSEI;
1756 		fdc->sc_nstat = 0;
1757 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1758 		/* recalibrate function */
1759 		FDC_WRFIFO(fdc, NE7CMD_RECAL);
1760 		FDC_WRFIFO(fdc, fd->sc_drive);
1761 		return (1);			/* will return later */
1762 
1763 	case RECALWAIT:
1764 		callout_stop(&fdc->sc_timo_ch);
1765 		fdc->sc_state = RECALCOMPLETE;
1766 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1767 			/* allow 1/30 second for heads to settle */
1768 			callout_reset(&fdc->sc_intr_ch, hz / 30,
1769 			    fdcpseudointr, fdc);
1770 			return (1);		/* will return later */
1771 		}
1772 
1773 	case RECALCOMPLETE:
1774 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1775 #ifdef FD_DEBUG
1776 			if (fdc_debug)
1777 				fdcstatus(fdc, "recalibrate failed");
1778 #endif
1779 			fdcretry(fdc);
1780 			goto loop;
1781 		}
1782 		fd->sc_cylin = 0;
1783 		goto doseek;
1784 
1785 	case MOTORWAIT:
1786 		if (fd->sc_flags & FD_MOTOR_WAIT)
1787 			return (1);		/* time's not up yet */
1788 		goto doseek;
1789 
1790 	default:
1791 		fdcstatus(fdc, "stray interrupt");
1792 		return (1);
1793 	}
1794 #ifdef DIAGNOSTIC
1795 	panic("fdcintr: impossible");
1796 #endif
1797 
1798 xxx:
1799 	/*
1800 	 * We get here if the chip locks up in FDC_WRFIFO()
1801 	 * Cancel any operation and schedule a reset
1802 	 */
1803 	callout_stop(&fdc->sc_timo_ch);
1804 	fdcretry(fdc);
1805 	(fdc)->sc_state = DORESET;
1806 	goto loop;
1807 
1808 #undef	st0
1809 #undef	st1
1810 #undef	cyl
1811 }
1812 
1813 void
1814 fdcretry(fdc)
1815 	struct fdc_softc *fdc;
1816 {
1817 	struct fd_softc *fd;
1818 	struct buf *bp;
1819 	int error = EIO;
1820 
1821 	fd = fdc->sc_drives.tqh_first;
1822 	bp = BUFQ_PEEK(&fd->sc_q);
1823 
1824 	fdc->sc_overruns = 0;
1825 	if (fd->sc_opts & FDOPT_NORETRY)
1826 		goto fail;
1827 
1828 	switch (fdc->sc_errors) {
1829 	case 0:
1830 		if (fdc->sc_nstat == 7 &&
1831 		    (fdc->sc_status[0] & 0xd8) == 0x40 &&
1832 		    (fdc->sc_status[1] & 0x2) == 0x2) {
1833 			printf("%s: read-only medium\n", fd->sc_dv.dv_xname);
1834 			error = EROFS;
1835 			goto failsilent;
1836 		}
1837 		/* try again */
1838 		fdc->sc_state =
1839 			(fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
1840 		break;
1841 
1842 	case 1: case 2: case 3:
1843 		/* didn't work; try recalibrating */
1844 		fdc->sc_state = DORECAL;
1845 		break;
1846 
1847 	case 4:
1848 		if (fdc->sc_nstat == 7 &&
1849 		    fdc->sc_status[0] == 0 &&
1850 		    fdc->sc_status[1] == 0 &&
1851 		    fdc->sc_status[2] == 0) {
1852 			/*
1853 			 * We've retried a few times and we've got
1854 			 * valid status and all three status bytes
1855 			 * are zero.  Assume this condition is the
1856 			 * result of no disk loaded into the drive.
1857 			 */
1858 			printf("%s: no medium?\n", fd->sc_dv.dv_xname);
1859 			error = ENODEV;
1860 			goto failsilent;
1861 		}
1862 
1863 		/* still no go; reset the bastard */
1864 		fdc->sc_state = DORESET;
1865 		break;
1866 
1867 	default:
1868 	fail:
1869 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1870 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
1871 				fd->sc_skip / FD_BSIZE(fd),
1872 				(struct disklabel *)NULL);
1873 			printf("\n");
1874 			fdcstatus(fdc, "controller status");
1875 		}
1876 
1877 	failsilent:
1878 		bp->b_flags |= B_ERROR;
1879 		bp->b_error = error;
1880 		fdfinish(fd, bp);
1881 	}
1882 	fdc->sc_errors++;
1883 }
1884 
1885 int
1886 fdioctl(dev, cmd, addr, flag, p)
1887 	dev_t dev;
1888 	u_long cmd;
1889 	caddr_t addr;
1890 	int flag;
1891 	struct proc *p;
1892 {
1893 	struct fd_softc *fd;
1894 	struct fdc_softc *fdc;
1895 	struct fdformat_parms *form_parms;
1896 	struct fdformat_cmd *form_cmd;
1897 	struct ne7_fd_formb *fd_formb;
1898 	int il[FD_MAX_NSEC + 1];
1899 	int unit;
1900 	int i, j;
1901 	int error;
1902 
1903 	unit = FDUNIT(dev);
1904 	if (unit >= fd_cd.cd_ndevs)
1905 		return (ENXIO);
1906 
1907 	fd = fd_cd.cd_devs[FDUNIT(dev)];
1908 	fdc = (struct fdc_softc *)fd->sc_dv.dv_parent;
1909 
1910 	switch (cmd) {
1911 	case DIOCGDINFO:
1912 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1913 		return 0;
1914 
1915 	case DIOCWLABEL:
1916 		if ((flag & FWRITE) == 0)
1917 			return EBADF;
1918 		/* XXX do something */
1919 		return (0);
1920 
1921 	case DIOCWDINFO:
1922 		if ((flag & FWRITE) == 0)
1923 			return (EBADF);
1924 
1925 		error = setdisklabel(fd->sc_dk.dk_label,
1926 				    (struct disklabel *)addr, 0,
1927 				    fd->sc_dk.dk_cpulabel);
1928 		if (error)
1929 			return (error);
1930 
1931 		error = writedisklabel(dev, fdstrategy,
1932 				       fd->sc_dk.dk_label,
1933 				       fd->sc_dk.dk_cpulabel);
1934 		return (error);
1935 
1936 	case DIOCLOCK:
1937 		/*
1938 		 * Nothing to do here, really.
1939 		 */
1940 		return (0);
1941 
1942 	case DIOCEJECT:
1943 		if (*(int *)addr == 0) {
1944 			int part = DISKPART(dev);
1945 			/*
1946 			 * Don't force eject: check that we are the only
1947 			 * partition open. If so, unlock it.
1948 			 */
1949 			if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
1950 			    fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
1951 			    fd->sc_dk.dk_openmask) {
1952 				return (EBUSY);
1953 			}
1954 		}
1955 		/* FALLTHROUGH */
1956 	case ODIOCEJECT:
1957 		fd_do_eject(fd);
1958 		return (0);
1959 
1960 	case FDIOCGETFORMAT:
1961 		form_parms = (struct fdformat_parms *)addr;
1962 		form_parms->fdformat_version = FDFORMAT_VERSION;
1963 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1964 		form_parms->ncyl = fd->sc_type->cylinders;
1965 		form_parms->nspt = fd->sc_type->sectrac;
1966 		form_parms->ntrk = fd->sc_type->heads;
1967 		form_parms->stepspercyl = fd->sc_type->step;
1968 		form_parms->gaplen = fd->sc_type->gap2;
1969 		form_parms->fillbyte = fd->sc_type->fillbyte;
1970 		form_parms->interleave = fd->sc_type->interleave;
1971 		switch (fd->sc_type->rate) {
1972 		case FDC_500KBPS:
1973 			form_parms->xfer_rate = 500 * 1024;
1974 			break;
1975 		case FDC_300KBPS:
1976 			form_parms->xfer_rate = 300 * 1024;
1977 			break;
1978 		case FDC_250KBPS:
1979 			form_parms->xfer_rate = 250 * 1024;
1980 			break;
1981 		default:
1982 			return (EINVAL);
1983 		}
1984 		return (0);
1985 
1986 	case FDIOCSETFORMAT:
1987 		if ((flag & FWRITE) == 0)
1988 			return (EBADF);	/* must be opened for writing */
1989 
1990 		form_parms = (struct fdformat_parms *)addr;
1991 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
1992 			return (EINVAL);/* wrong version of formatting prog */
1993 
1994 		i = form_parms->nbps >> 7;
1995 		if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
1996 		    i & ~(1 << (ffs(i)-1)))
1997 			/* not a power-of-two multiple of 128 */
1998 			return (EINVAL);
1999 
2000 		switch (form_parms->xfer_rate) {
2001 		case 500 * 1024:
2002 			fd->sc_type->rate = FDC_500KBPS;
2003 			break;
2004 		case 300 * 1024:
2005 			fd->sc_type->rate = FDC_300KBPS;
2006 			break;
2007 		case 250 * 1024:
2008 			fd->sc_type->rate = FDC_250KBPS;
2009 			break;
2010 		default:
2011 			return (EINVAL);
2012 		}
2013 
2014 		if (form_parms->nspt > FD_MAX_NSEC ||
2015 		    form_parms->fillbyte > 0xff ||
2016 		    form_parms->interleave > 0xff)
2017 			return EINVAL;
2018 		fd->sc_type->sectrac = form_parms->nspt;
2019 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
2020 			return EINVAL;
2021 		fd->sc_type->heads = form_parms->ntrk;
2022 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
2023 		fd->sc_type->secsize = ffs(i)-1;
2024 		fd->sc_type->gap2 = form_parms->gaplen;
2025 		fd->sc_type->cylinders = form_parms->ncyl;
2026 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
2027 			form_parms->nbps / DEV_BSIZE;
2028 		fd->sc_type->step = form_parms->stepspercyl;
2029 		fd->sc_type->fillbyte = form_parms->fillbyte;
2030 		fd->sc_type->interleave = form_parms->interleave;
2031 		return (0);
2032 
2033 	case FDIOCFORMAT_TRACK:
2034 		if((flag & FWRITE) == 0)
2035 			/* must be opened for writing */
2036 			return (EBADF);
2037 		form_cmd = (struct fdformat_cmd *)addr;
2038 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
2039 			/* wrong version of formatting prog */
2040 			return (EINVAL);
2041 
2042 		if (form_cmd->head >= fd->sc_type->heads ||
2043 		    form_cmd->cylinder >= fd->sc_type->cylinders) {
2044 			return (EINVAL);
2045 		}
2046 
2047 		fd_formb = malloc(sizeof(struct ne7_fd_formb),
2048 		    M_TEMP, M_NOWAIT);
2049 		if (fd_formb == 0)
2050 			return (ENOMEM);
2051 
2052 		fd_formb->head = form_cmd->head;
2053 		fd_formb->cyl = form_cmd->cylinder;
2054 		fd_formb->transfer_rate = fd->sc_type->rate;
2055 		fd_formb->fd_formb_secshift = fd->sc_type->secsize;
2056 		fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
2057 		fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
2058 		fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
2059 
2060 		bzero(il, sizeof il);
2061 		for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
2062 			while (il[(j%fd_formb->fd_formb_nsecs) + 1])
2063 				j++;
2064 			il[(j%fd_formb->fd_formb_nsecs) + 1] = i;
2065 			j += fd->sc_type->interleave;
2066 		}
2067 		for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
2068 			fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
2069 			fd_formb->fd_formb_headno(i) = form_cmd->head;
2070 			fd_formb->fd_formb_secno(i) = il[i+1];
2071 			fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
2072 		}
2073 
2074 		error = fdformat(dev, fd_formb, p);
2075 		free(fd_formb, M_TEMP);
2076 		return error;
2077 
2078 	case FDIOCGETOPTS:		/* get drive options */
2079 		*(int *)addr = fd->sc_opts;
2080 		return (0);
2081 
2082 	case FDIOCSETOPTS:		/* set drive options */
2083 		fd->sc_opts = *(int *)addr;
2084 		return (0);
2085 
2086 #ifdef FD_DEBUG
2087 	case _IO('f', 100):
2088 		fdc_wrfifo(fdc, NE7CMD_DUMPREG);
2089 		fdcresult(fdc);
2090 		printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
2091 		for (i = 0; i < fdc->sc_nstat; i++)
2092 			printf(" 0x%x", fdc->sc_status[i]);
2093 		printf(">\n");
2094 		return (0);
2095 
2096 	case _IOW('f', 101, int):
2097 		fdc->sc_cfg &= ~CFG_THRHLD_MASK;
2098 		fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
2099 		fdconf(fdc);
2100 		return (0);
2101 
2102 	case _IO('f', 102):
2103 		fdc_wrfifo(fdc, NE7CMD_SENSEI);
2104 		fdcresult(fdc);
2105 		printf("fdc: sensei(%d regs): <", fdc->sc_nstat);
2106 		for (i=0; i< fdc->sc_nstat; i++)
2107 			printf(" 0x%x", fdc->sc_status[i]);
2108 		printf(">\n");
2109 		return (0);
2110 #endif
2111 	default:
2112 		return (ENOTTY);
2113 	}
2114 
2115 #ifdef DIAGNOSTIC
2116 	panic("fdioctl: impossible");
2117 #endif
2118 }
2119 
2120 int
2121 fdformat(dev, finfo, p)
2122 	dev_t dev;
2123 	struct ne7_fd_formb *finfo;
2124 	struct proc *p;
2125 {
2126 	int rv = 0, s;
2127 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
2128 	struct fd_type *type = fd->sc_type;
2129 	struct buf *bp;
2130 
2131 	/* set up a buffer header for fdstrategy() */
2132 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
2133 	if (bp == 0)
2134 		return (ENOBUFS);
2135 
2136 	memset((void *)bp, 0, sizeof(struct buf));
2137 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
2138 	bp->b_proc = p;
2139 	bp->b_dev = dev;
2140 
2141 	/*
2142 	 * Calculate a fake blkno, so fdstrategy() would initiate a
2143 	 * seek to the requested cylinder.
2144 	 */
2145 	bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
2146 		       + finfo->head * type->sectrac) * FD_BSIZE(fd))
2147 		      / DEV_BSIZE;
2148 
2149 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2150 	bp->b_data = (caddr_t)finfo;
2151 
2152 #ifdef FD_DEBUG
2153 	if (fdc_debug) {
2154 		int i;
2155 
2156 		printf("fdformat: blkno 0x%x count %ld\n",
2157 			bp->b_blkno, bp->b_bcount);
2158 
2159 		printf("\tcyl:\t%d\n", finfo->cyl);
2160 		printf("\thead:\t%d\n", finfo->head);
2161 		printf("\tnsecs:\t%d\n", finfo->fd_formb_nsecs);
2162 		printf("\tsshft:\t%d\n", finfo->fd_formb_secshift);
2163 		printf("\tgaplen:\t%d\n", finfo->fd_formb_gaplen);
2164 		printf("\ttrack data:");
2165 		for (i = 0; i < finfo->fd_formb_nsecs; i++) {
2166 			printf(" [c%d h%d s%d]",
2167 					finfo->fd_formb_cylno(i),
2168 					finfo->fd_formb_headno(i),
2169 					finfo->fd_formb_secno(i) );
2170 			if (finfo->fd_formb_secsize(i) != 2)
2171 				printf("<sz:%d>", finfo->fd_formb_secsize(i));
2172 		}
2173 		printf("\n");
2174 	}
2175 #endif
2176 
2177 	/* now do the format */
2178 	fdstrategy(bp);
2179 
2180 	/* ...and wait for it to complete */
2181 	s = splbio();
2182 	while (!(bp->b_flags & B_DONE)) {
2183 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2184 		if (rv == EWOULDBLOCK)
2185 			break;
2186 	}
2187 	splx(s);
2188 
2189 	if (rv == EWOULDBLOCK) {
2190 		/* timed out */
2191 		rv = EIO;
2192 		biodone(bp);
2193 	}
2194 	if (bp->b_flags & B_ERROR) {
2195 		rv = bp->b_error;
2196 	}
2197 	free(bp, M_TEMP);
2198 	return (rv);
2199 }
2200 
2201 void
2202 fdgetdisklabel(dev)
2203 	dev_t dev;
2204 {
2205 	int unit = FDUNIT(dev), i;
2206 	struct fd_softc *fd = fd_cd.cd_devs[unit];
2207 	struct disklabel *lp = fd->sc_dk.dk_label;
2208 	struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
2209 
2210 	bzero(lp, sizeof(struct disklabel));
2211 	bzero(lp, sizeof(struct cpu_disklabel));
2212 
2213 	lp->d_type = DTYPE_FLOPPY;
2214 	lp->d_secsize = FD_BSIZE(fd);
2215 	lp->d_secpercyl = fd->sc_type->seccyl;
2216 	lp->d_nsectors = fd->sc_type->sectrac;
2217 	lp->d_ncylinders = fd->sc_type->cylinders;
2218 	lp->d_ntracks = fd->sc_type->heads;	/* Go figure... */
2219 	lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
2220 	lp->d_rpm = 3600;	/* XXX like it matters... */
2221 
2222 	strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename));
2223 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
2224 	lp->d_interleave = 1;
2225 
2226 	lp->d_partitions[RAW_PART].p_offset = 0;
2227 	lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
2228 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
2229 	lp->d_npartitions = RAW_PART + 1;
2230 
2231 	lp->d_magic = DISKMAGIC;
2232 	lp->d_magic2 = DISKMAGIC;
2233 	lp->d_checksum = dkcksum(lp);
2234 
2235 	/*
2236 	 * Call the generic disklabel extraction routine.  If there's
2237 	 * not a label there, fake it.
2238 	 */
2239 	if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
2240 		strncpy(lp->d_packname, "default label",
2241 		    sizeof(lp->d_packname));
2242 		/*
2243 		 * Reset the partition info; it might have gotten
2244 		 * trashed in readdisklabel().
2245 		 *
2246 		 * XXX Why do we have to do this?  readdisklabel()
2247 		 * should be safe...
2248 		 */
2249 		for (i = 0; i < MAXPARTITIONS; ++i) {
2250 			lp->d_partitions[i].p_offset = 0;
2251 			if (i == RAW_PART) {
2252 				lp->d_partitions[i].p_size =
2253 				    lp->d_secpercyl * lp->d_ncylinders;
2254 				lp->d_partitions[i].p_fstype = FS_BSDFFS;
2255 			} else {
2256 				lp->d_partitions[i].p_size = 0;
2257 				lp->d_partitions[i].p_fstype = FS_UNUSED;
2258 			}
2259 		}
2260 		lp->d_npartitions = RAW_PART + 1;
2261 	}
2262 }
2263 
2264 void
2265 fd_do_eject(fd)
2266 	struct fd_softc *fd;
2267 {
2268 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
2269 
2270 	if (CPU_ISSUN4C) {
2271 		auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
2272 		delay(10);
2273 		auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
2274 		return;
2275 	}
2276 	if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
2277 		bus_space_tag_t t = fdc->sc_bustag;
2278 		bus_space_handle_t h = fdc->sc_handle;
2279 		u_int8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
2280 
2281 		bus_space_write_1(t, h, fdc->sc_reg_dor, dor | FDO_EJ);
2282 		delay(10);
2283 		bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
2284 		return;
2285 	}
2286 }
2287 
2288 #ifdef MEMORY_DISK_HOOKS
2289 int	fd_read_md_image __P((size_t *, caddr_t *));
2290 #endif
2291 
2292 /* ARGSUSED */
2293 void
2294 fd_mountroot_hook(dev)
2295 	struct device *dev;
2296 {
2297 	int c;
2298 
2299 	fd_do_eject((struct fd_softc *)dev);
2300 	printf("Insert filesystem floppy and press return.");
2301 	for (;;) {
2302 		c = cngetc();
2303 		if ((c == '\r') || (c == '\n')) {
2304 			printf("\n");
2305 			break;
2306 		}
2307 	}
2308 }
2309 
2310 #ifdef MEMORY_DISK_HOOKS
2311 
2312 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
2313 
2314 int
2315 fd_read_md_image(sizep, addrp)
2316 	size_t	*sizep;
2317 	caddr_t	*addrp;
2318 {
2319 	struct buf buf, *bp = &buf;
2320 	dev_t dev;
2321 	off_t offset;
2322 	caddr_t addr;
2323 
2324 	dev = makedev(54,0);	/* XXX */
2325 
2326 	MALLOC(addr, caddr_t, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
2327 	*addrp = addr;
2328 
2329 	if (fdopen(dev, 0, S_IFCHR, NULL))
2330 		panic("fd: mountroot: fdopen");
2331 
2332 	offset = 0;
2333 
2334 	for (;;) {
2335 		bp->b_dev = dev;
2336 		bp->b_error = 0;
2337 		bp->b_resid = 0;
2338 		bp->b_proc = NULL;
2339 		bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
2340 		bp->b_blkno = btodb(offset);
2341 		bp->b_bcount = DEV_BSIZE;
2342 		bp->b_data = addr;
2343 		fdstrategy(bp);
2344 		while ((bp->b_flags & B_DONE) == 0) {
2345 			tsleep((caddr_t)bp, PRIBIO + 1, "physio", 0);
2346 		}
2347 		if (bp->b_error)
2348 			panic("fd: mountroot: fdread error %d", bp->b_error);
2349 
2350 		if (bp->b_resid != 0)
2351 			break;
2352 
2353 		addr += DEV_BSIZE;
2354 		offset += DEV_BSIZE;
2355 		if (offset + DEV_BSIZE > FDMICROROOTSIZE)
2356 			break;
2357 	}
2358 	(void)fdclose(dev, 0, S_IFCHR, NULL);
2359 	*sizep = offset;
2360 	fd_do_eject(fd_cd.cd_devs[FDUNIT(dev)]);
2361 	return (0);
2362 }
2363 #endif
2364