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