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