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