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