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