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