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