xref: /netbsd-src/sys/arch/sun3/dev/fd.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: fd.c,v 1.28 2002/09/06 13:18:43 gehenna 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/dkstat.h>
53 #include <sys/disk.h>
54 #include <sys/fdio.h>
55 #include <sys/buf.h>
56 #include <sys/malloc.h>
57 #include <sys/proc.h>
58 #include <sys/uio.h>
59 #include <sys/stat.h>
60 #include <sys/syslog.h>
61 #include <sys/queue.h>
62 #include <sys/conf.h>
63 
64 #include <dev/cons.h>
65 
66 #include <uvm/uvm_extern.h>
67 
68 #include <machine/cpu.h>
69 #include <machine/autoconf.h>
70 
71 #include <sun3/dev/fdreg.h>
72 #include <sun3/dev/fdvar.h>
73 
74 /*
75  * Print a complaint when no fd children were specified
76  * in the config file.  Better than a link error...
77  *
78  * XXX: Some folks say this driver should be split in two,
79  * but that seems pointless with ONLY one type of child.
80  * (Thankfully, no 3/80 boxes have floppy tapes!:)
81  */
82 #include "fdc.h"
83 #if NFD == 0
84 #error "fdc but no fd?"
85 #endif
86 
87 #define FDUNIT(dev)	(minor(dev) / 8)
88 #define FDTYPE(dev)	(minor(dev) % 8)
89 
90 /* XXX misuse a flag to identify format operation */
91 #define B_FORMAT B_XXX
92 
93 #ifdef FD_DEBUG
94 int	fdc_debug = 0;
95 #endif
96 
97 enum fdc_state {
98 	DEVIDLE = 0,
99 	MOTORWAIT,
100 	DOSEEK,
101 	SEEKWAIT,
102 	SEEKTIMEDOUT,
103 	SEEKCOMPLETE,
104 	DOIO,
105 	IOCOMPLETE,
106 	IOTIMEDOUT,
107 	DORESET,
108 	RESETCOMPLETE,
109 	RESETTIMEDOUT,
110 	DORECAL,
111 	RECALWAIT,
112 	RECALTIMEDOUT,
113 	RECALCOMPLETE,
114 };
115 
116 /* software state, per controller */
117 struct fdc_softc {
118 	struct device	sc_dev;		/* boilerplate */
119 	caddr_t		sc_reg;
120 
121 	struct callout sc_timo_ch;	/* timeout callout */
122 	struct callout sc_intr_ch;	/* pseudo-intr callout */
123 
124 	struct fd_softc *sc_fd[4];	/* pointers to children */
125 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
126 	enum fdc_state	sc_state;
127 	int		sc_flags;
128 #define FDC_82077		0x01
129 #define FDC_NEEDHEADSETTLE	0x02
130 #define FDC_EIS			0x04
131 	int		sc_errors;		/* number of retries so far */
132 	int		sc_overruns;		/* number of DMA overruns */
133 	int		sc_cfg;			/* current configuration */
134 	int		sc_fcr;			/* current image of floppy ctrlr reg. */
135 	struct fdcio	sc_io;
136 #define sc_reg_msr	sc_io.fdcio_reg_msr
137 #define sc_reg_fifo	sc_io.fdcio_reg_fifo
138 #define sc_reg_fcr	sc_io.fdcio_reg_fcr
139 #define	sc_reg_fvr	sc_io.fdcio_reg_fvr
140 #define sc_reg_drs	sc_io.fdcio_reg_msr
141 #define sc_istate	sc_io.fdcio_istate
142 #define sc_data		sc_io.fdcio_data
143 #define sc_tc		sc_io.fdcio_tc
144 #define sc_nstat	sc_io.fdcio_nstat
145 #define sc_status	sc_io.fdcio_status
146 #define sc_intrcnt	sc_io.fdcio_intrcnt
147 };
148 
149 /* controller driver configuration */
150 int	fdcmatch __P((struct device *, struct cfdata *, void *));
151 void	fdcattach __P((struct device *, struct device *, void *));
152 
153 struct cfattach fdc_ca = {
154 	sizeof(struct fdc_softc), fdcmatch, fdcattach
155 };
156 
157 extern struct cfdriver fdc_cd;
158 
159 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
160 
161 /*
162  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
163  * we tell them apart.
164  */
165 struct fd_type {
166 	int	sectrac;	/* sectors per track */
167 	int	heads;		/* number of heads */
168 	int	seccyl;		/* sectors per cylinder */
169 	int	secsize;	/* size code for sectors */
170 	int	datalen;	/* data len when secsize = 0 */
171 	int	steprate;	/* step rate and head unload time */
172 	int	gap1;		/* gap len between sectors */
173 	int	gap2;		/* formatting gap */
174 	int	tracks;		/* total num of tracks */
175 	int	size;		/* size of disk in sectors */
176 	int	step;		/* steps per cylinder */
177 	int	rate;		/* transfer speed code */
178 	int	fillbyte;	/* format fill byte */
179 	int	interleave;	/* interleave factor (formatting) */
180 	char	*name;
181 };
182 
183 /* The order of entries in the following table is important -- BEWARE! */
184 struct fd_type fd_types[] = {
185 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette */
186 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB"    }, /* 1.2 MB AT-diskettes */
187 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */
188 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
189 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5" 720kB diskette */
190 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x"  }, /* 720kB in 1.2MB drive */
191 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
192 };
193 
194 /* software state, per disk (with up to 4 disks per ctlr) */
195 struct fd_softc {
196 	struct device	sc_dv;		/* generic device info */
197 	struct disk	sc_dk;		/* generic disk info */
198 
199 	struct fd_type *sc_deftype;	/* default type descriptor */
200 	struct fd_type *sc_type;	/* current type descriptor */
201 
202 	struct callout sc_motoron_ch;
203 	struct callout sc_motoroff_ch;
204 
205 	daddr_t	sc_blkno;	/* starting block number */
206 	int sc_bcount;		/* byte count left */
207 	int sc_skip;		/* bytes already transferred */
208 	int sc_nblks;		/* number of blocks currently transferring */
209 	int sc_nbytes;		/* number of bytes currently transferring */
210 
211 	int sc_drive;		/* physical unit number */
212 	int sc_flags;
213 #define	FD_OPEN		0x01		/* it's open */
214 #define	FD_MOTOR	0x02		/* motor should be on */
215 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
216 	int sc_cylin;		/* where we think the head is */
217 	int sc_opts;		/* user-set options */
218 
219 	void	*sc_sdhook;	/* shutdownhook cookie */
220 
221 	TAILQ_ENTRY(fd_softc) sc_drivechain;
222 	int sc_ops;		/* I/O ops since last switch */
223 	struct bufq_state sc_q;	/* pending I/O requests */
224 	int sc_active;		/* number of active I/O operations */
225 };
226 
227 /* floppy driver configuration */
228 int	fdmatch __P((struct device *, struct cfdata *, void *));
229 void	fdattach __P((struct device *, struct device *, void *));
230 
231 struct cfattach fd_ca = {
232 	sizeof(struct fd_softc), fdmatch, fdattach
233 };
234 
235 extern struct cfdriver fd_cd;
236 
237 dev_type_open(fdopen);
238 dev_type_close(fdclose);
239 dev_type_read(fdread);
240 dev_type_write(fdwrite);
241 dev_type_ioctl(fdioctl);
242 dev_type_strategy(fdstrategy);
243 
244 const struct bdevsw fd_bdevsw = {
245 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
246 };
247 
248 const struct cdevsw fd_cdevsw = {
249 	fdopen, fdclose, fdread, fdwrite, fdioctl,
250 	nostop, notty, nopoll, nommap, D_DISK
251 };
252 
253 void fdgetdisklabel __P((dev_t));
254 int fd_get_parms __P((struct fd_softc *));
255 void fdstart __P((struct fd_softc *));
256 int fdprint __P((void *, const char *));
257 
258 struct dkdriver fddkdriver = { fdstrategy };
259 
260 struct	fd_type *fd_nvtotype __P((char *, int, int));
261 void	fd_set_motor __P((struct fdc_softc *fdc));
262 void	fd_motor_off __P((void *arg));
263 void	fd_motor_on __P((void *arg));
264 int	fdcresult __P((struct fdc_softc *fdc));
265 int	out_fdc __P((struct fdc_softc *fdc, u_char x));
266 void	fdcstart __P((struct fdc_softc *fdc));
267 void	fdcstatus __P((struct device *dv, int n, char *s));
268 void	fdc_reset __P((struct fdc_softc *fdc));
269 void	fdctimeout __P((void *arg));
270 void	fdcpseudointr __P((void *arg));
271 int	fdchwintr __P((void *));
272 int	fdcswintr __P((void *));
273 int	fdcstate __P((struct fdc_softc *));
274 void	fdcretry __P((struct fdc_softc *fdc));
275 void	fdfinish __P((struct fd_softc *fd, struct buf *bp));
276 int	fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
277 void	fd_do_eject __P((struct fdc_softc *, int));
278 void	fd_mountroot_hook __P((struct device *));
279 static void fdconf __P((struct fdc_softc *));
280 
281 static int fdc_softpend = 0;
282 #ifndef	FDC_SOFTPRI
283 #define	FDC_SOFTPRI	2
284 #endif
285 #define FD_SET_SWINTR()	{ fdc_softpend = 1; isr_soft_request(FDC_SOFTPRI); }
286 
287 /*
288  * The Floppy Control Register on the sun3x, not to be confused with the
289  * Floppy ControllER Registers that this driver mostly insterfaces with,
290  * controls some of the auxillary functions of the floppy drive.  These
291  * include asserting the floppy eject and terminal data count (or TC) pins
292  * of the floppy drive and controller chip respectively.
293  *
294  * Often it is necessary to toggle individual bits within this register
295  * while keeping the others untouched.  However, the register does not
296  * present its latched data to the processor when read.  This prevents the
297  * use of a read-modify-write cycle that would normally be used to modify
298  * individual bits.  To get around this we must keep a copy of register's
299  * current value and always insure that when we wish to modify the register,
300  * we actually modify the copy and synchronize the register to it.
301  */
302 #define	FCR_REG_SYNC()	(*fdc->sc_reg_fcr = fdc->sc_fcr)
303 
304 int
305 fdcmatch(parent, match, aux)
306 	struct device *parent;
307 	struct cfdata *match;
308 	void *aux;
309 {
310 	struct confargs *ca = aux;
311 
312 	if (bus_peek(ca->ca_bustype, ca->ca_paddr, sizeof(u_char)) == -1)
313 		return (0);
314 
315 	return (1);
316 }
317 
318 /*
319  * Arguments passed between fdcattach and fdprobe.
320  */
321 struct fdc_attach_args {
322 	int fa_drive;
323 	struct bootpath *fa_bootpath;
324 	struct fd_type *fa_deftype;
325 };
326 
327 /*
328  * Print the location of a disk drive (called just before attaching the
329  * the drive).  If `fdc' is not NULL, the drive was found but was not
330  * in the system config file; print the drive name as well.
331  * Return QUIET (config_find ignores this if the device was configured) to
332  * avoid printing `fdN not configured' messages.
333  */
334 int
335 fdprint(aux, fdc)
336 	void *aux;
337 	const char *fdc;
338 {
339 	struct fdc_attach_args *fa = aux;
340 
341 	if (!fdc)
342 		printf(" drive %d", fa->fa_drive);
343 	return (QUIET);
344 }
345 
346 static void
347 fdconf(fdc)
348 	struct fdc_softc *fdc;
349 {
350 	int	vroom;
351 
352 	if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
353 		return;
354 
355 	/*
356 	 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
357 	 * the PROM thinks is appropriate.
358 	 */
359 	if ((vroom = fdc->sc_status[7]) == 0)
360 		vroom = 0x64;
361 
362 	/* Configure controller to use FIFO and Implied Seek */
363 	out_fdc(fdc, NE7CMD_CFG);
364 	out_fdc(fdc, vroom);
365 	out_fdc(fdc, fdc->sc_cfg);
366 	out_fdc(fdc, 0); /* PRETRK */
367 	/* No result phase */
368 }
369 
370 void
371 fdcattach(parent, self, aux)
372 	struct device *parent, *self;
373 	void *aux;
374 {
375 	struct confargs *ca = aux;
376 	struct fdc_softc *fdc = (void *)self;
377 	struct fdc_attach_args fa;
378 	int pri, vec;
379 	char code;
380 
381 	fdc->sc_reg = (caddr_t)bus_mapin(ca->ca_bustype, ca->ca_paddr,
382 		sizeof(union fdreg));
383 
384 	callout_init(&fdc->sc_timo_ch);
385 	callout_init(&fdc->sc_intr_ch);
386 
387 	fdc->sc_state = DEVIDLE;
388 	fdc->sc_istate = ISTATE_IDLE;
389 	fdc->sc_flags |= FDC_EIS;
390 	TAILQ_INIT(&fdc->sc_drives);
391 
392 	/* Assume a 82072 */
393 	code = '2';
394 
395 	if (code == '7') {
396 		panic("no 82077 fdc in this kernel");
397 		/* NOTREACHED */
398 	} else {
399 		fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr;
400 		fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo;
401 
402 		fdc->sc_reg_fcr = ((volatile u_int8_t *) fdc->sc_reg)
403 			+ FDC_FCR_OFFSET;
404 		fdc->sc_reg_fvr = ((volatile u_int8_t *) fdc->sc_reg)
405 			+ FDC_FVR_OFFSET;
406 	}
407 
408 	isr_add_autovect(fdcswintr, fdc, FDC_SOFTPRI);
409 	pri = ca->ca_intpri;
410 	vec = ca->ca_intvec;
411 	if (vec == -1) {
412 		/* Tell the FDC to fake an autovector. */
413 		vec = 0x18 + pri; /* XXX */
414 		isr_add_autovect(fdchwintr, fdc, pri);
415 	} else {
416 		/* An OBIO bus with vectors?  Weird exception. */
417 		isr_add_vectored(fdchwintr, fdc, pri, vec);
418 	}
419 	*fdc->sc_reg_fvr = vec;	/* Program controller w/ interrupt vector */
420 
421 	printf(": (softpri %d) chip 8207%c\n", FDC_SOFTPRI, code);
422 
423 #ifdef FD_DEBUG
424 	if (out_fdc(fdc, NE7CMD_VERSION) == 0 &&
425 	    fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
426 		if (fdc_debug)
427 			printf("[version cmd]");
428 	}
429 #endif
430 
431 	fdc_reset(fdc);
432 	/*
433 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
434 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
435 	 */
436 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
437 	fdconf(fdc);
438 
439 	evcnt_attach_dynamic(&fdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
440 	    fdc->sc_dev.dv_xname, "intr");
441 
442 	/* physical limit: four drives per controller. */
443 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
444 		fa.fa_deftype = NULL;		/* unknown */
445 	fa.fa_deftype = &fd_types[0];		/* XXX */
446 		(void)config_found(self, (void *)&fa, fdprint);
447 	}
448 }
449 
450 int
451 fdmatch(parent, match, aux)
452 	struct device *parent;
453 	struct cfdata *match;
454 	void *aux;
455 {
456 	struct fdc_softc *fdc = (void *)parent;
457 	struct fdc_attach_args *fa = aux;
458 	int drive = fa->fa_drive;
459 	int n, ok;
460 
461 	if (drive > 0)
462 		/* XXX - for now, punt > 1 drives */
463 		return (0);
464 
465 	/* select drive and turn on motor */
466 	fdc->sc_fcr |= FCR_DSEL(drive) | FCR_MTRON;
467 	FCR_REG_SYNC();
468 	/* wait for motor to spin up */
469 	delay(250000);
470 
471 	fdc->sc_nstat = 0;
472 	out_fdc(fdc, NE7CMD_RECAL);
473 	out_fdc(fdc, drive);
474 	/* wait for recalibrate */
475 	for (n = 0; n < 10000; n++) {
476 		delay(1000);
477 		if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
478 			/* wait a bit longer till device *really* is ready */
479 			delay(100000);
480 			if (out_fdc(fdc, NE7CMD_SENSEI))
481 				break;
482 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
483 				/*
484 				 * Got `invalid command'; we interpret it
485 				 * to mean that the re-calibrate hasn't in
486 				 * fact finished yet
487 				 */
488 				continue;
489 			break;
490 		}
491 	}
492 	n = fdc->sc_nstat;
493 #ifdef FD_DEBUG
494 	if (fdc_debug) {
495 		int i;
496 		printf("fdprobe: %d stati:", n);
497 		for (i = 0; i < n; i++)
498 			printf(" %x", fdc->sc_status[i]);
499 		printf("\n");
500 	}
501 #endif
502 	ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
503 
504 	/* turn off motor */
505 	fdc->sc_fcr &= ~(FCR_DSEL(drive)|FCR_MTRON);
506 	FCR_REG_SYNC();
507 
508 	return (ok);
509 }
510 
511 /*
512  * Controller is working, and drive responded.  Attach it.
513  */
514 void
515 fdattach(parent, self, aux)
516 	struct device *parent, *self;
517 	void *aux;
518 {
519 	struct fdc_softc *fdc = (void *)parent;
520 	struct fd_softc *fd = (void *)self;
521 	struct fdc_attach_args *fa = aux;
522 	struct fd_type *type = fa->fa_deftype;
523 	int drive = fa->fa_drive;
524 
525 	callout_init(&fd->sc_motoron_ch);
526 	callout_init(&fd->sc_motoroff_ch);
527 
528 	/* XXX Allow `flags' to override device type? */
529 
530 	if (type)
531 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
532 		    type->tracks, type->heads, type->sectrac);
533 	else
534 		printf(": density unknown\n");
535 
536 	bufq_alloc(&fd->sc_q, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER);
537 	fd->sc_cylin = -1;
538 	fd->sc_drive = drive;
539 	fd->sc_deftype = type;
540 	fdc->sc_fd[drive] = fd;
541 
542 	/*
543 	 * Initialize and attach the disk structure.
544 	 */
545 	fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
546 	fd->sc_dk.dk_driver = &fddkdriver;
547 	disk_attach(&fd->sc_dk);
548 
549 #ifdef	sparc
550 	/*
551 	 * We're told if we're the boot device in fdcattach().
552 	 */
553 	if (fa->fa_bootpath)
554 		fa->fa_bootpath->dev = &fd->sc_dv;
555 #endif
556 #define	OUT_FDC(sc, c)	{                                \
557 	if (out_fdc((sc), (c)))                          \
558 		printf("fdc: specify command failed.\n");\
559 	}
560 	/* specify command */
561 	OUT_FDC(fdc, NE7CMD_SPECIFY);
562 	OUT_FDC(fdc, type->steprate);
563 	/*
564 	 * The '|1' in the following statement turns on the 'Non-DMA' bit
565 	 * specifier in the last byte of the SPECIFY command as described in the
566 	 * datasheet I have.  This is necessary for the driver to work on the
567 	 * sun3x, because the system will not respond to the chip's requests
568 	 * for DMA; there is no hardware on the motherboard to support it.
569 	 * By enabling this bit, we will force the chip to interrupt when its
570 	 * FIFO is full, at which point the interrupt handler will empty it and
571 	 * continue.  This is ``pseudo-DMA''.
572 	 * -J
573 	 */
574 	OUT_FDC(fdc, 6|1);	/* XXX head load time == 6ms */
575 #undef	OUT_FDC
576 
577 	/*
578 	 * Establish a mountroot_hook anyway in case we booted
579 	 * with RB_ASKNAME and get selected as the boot device.
580 	 */
581 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
582 
583 	/* Make sure the drive motor gets turned off at shutdown time. */
584 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
585 }
586 
587 __inline struct fd_type *
588 fd_dev_to_type(fd, dev)
589 	struct fd_softc *fd;
590 	dev_t dev;
591 {
592 	int type = FDTYPE(dev);
593 
594 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
595 		return (NULL);
596 	return (type ? &fd_types[type - 1] : fd->sc_deftype);
597 }
598 
599 void
600 fdstrategy(bp)
601 	struct buf *bp;			/* IO operation to perform */
602 {
603 	struct fd_softc *fd;
604 	int unit = FDUNIT(bp->b_dev);
605 	int sz;
606  	int s;
607 
608 	/* Valid unit, controller, and request? */
609 	if (unit >= fd_cd.cd_ndevs ||
610 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
611 	    bp->b_blkno < 0 ||
612 	    ((bp->b_bcount % FDC_BSIZE) != 0 &&
613 	     (bp->b_flags & B_FORMAT) == 0)) {
614 		bp->b_error = EINVAL;
615 		goto bad;
616 	}
617 
618 	/* If it's a null transfer, return immediately. */
619 	if (bp->b_bcount == 0)
620 		goto done;
621 
622 	sz = howmany(bp->b_bcount, FDC_BSIZE);
623 
624 	if (bp->b_blkno + sz > fd->sc_type->size) {
625 		sz = fd->sc_type->size - bp->b_blkno;
626 		if (sz == 0) {
627 			/* If exactly at end of disk, return EOF. */
628 			bp->b_resid = bp->b_bcount;
629 			goto done;
630 		}
631 		if (sz < 0) {
632 			/* If past end of disk, return EINVAL. */
633 			bp->b_error = EINVAL;
634 			goto bad;
635 		}
636 		/* Otherwise, truncate request. */
637 		bp->b_bcount = sz << DEV_BSHIFT;
638 	}
639 
640 	bp->b_rawblkno = bp->b_blkno;
641  	bp->b_cylinder = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
642 
643 #ifdef FD_DEBUG
644 	if (fdc_debug > 1)
645 	    printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n",
646 		    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder);
647 #endif
648 
649 	/* Queue transfer on drive, activate drive and controller if idle. */
650 	s = splbio();
651 	BUFQ_PUT(&fd->sc_q, bp);
652 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
653 	if (fd->sc_active == 0)
654 		fdstart(fd);
655 #ifdef DIAGNOSTIC
656 	else {
657 		struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
658 		if (fdc->sc_state == DEVIDLE) {
659 			printf("fdstrategy: controller inactive\n");
660 			fdcstart(fdc);
661 		}
662 	}
663 #endif
664 	splx(s);
665 	return;
666 
667 bad:
668 	bp->b_flags |= B_ERROR;
669 done:
670 	/* Toss transfer; we're done early. */
671 	biodone(bp);
672 }
673 
674 void
675 fdstart(fd)
676 	struct fd_softc *fd;
677 {
678 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
679 	int active = fdc->sc_drives.tqh_first != 0;
680 
681 	/* Link into controller queue. */
682 	fd->sc_active = 1;
683 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
684 
685 	/* If controller not already active, start it. */
686 	if (!active)
687 		fdcstart(fdc);
688 }
689 
690 void
691 fdfinish(fd, bp)
692 	struct fd_softc *fd;
693 	struct buf *bp;
694 {
695 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
696 
697 	/*
698 	 * Move this drive to the end of the queue to give others a `fair'
699 	 * chance.  We only force a switch if N operations are completed while
700 	 * another drive is waiting to be serviced, since there is a long motor
701 	 * startup delay whenever we switch.
702 	 */
703 	(void)BUFQ_GET(&fd->sc_q);
704 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
705 		fd->sc_ops = 0;
706 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
707 		if (BUFQ_PEEK(&fd->sc_q) != NULL) {
708 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
709 		} else
710 			fd->sc_active = 0;
711 	}
712 	bp->b_resid = fd->sc_bcount;
713 	fd->sc_skip = 0;
714 
715 	biodone(bp);
716 	/* turn off motor 5s from now */
717 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
718 	fdc->sc_state = DEVIDLE;
719 }
720 
721 void
722 fdc_reset(fdc)
723 	struct fdc_softc *fdc;
724 {
725 	fdc->sc_fcr = 0;
726 	FCR_REG_SYNC();
727 
728 	*fdc->sc_reg_drs = DRS_RESET;
729 	delay(10);
730 	*fdc->sc_reg_drs = 0;
731 
732 #ifdef FD_DEBUG
733 	if (fdc_debug)
734 		printf("fdc reset\n");
735 #endif
736 }
737 
738 void
739 fd_set_motor(fdc)
740 	struct fdc_softc *fdc;
741 {
742 	struct fd_softc *fd;
743 	int n;
744 
745 	int on = 0;
746 
747 	for (n = 0; n < 4; n++)
748 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
749 			on = 1;
750 	if (on) {
751 		fdc->sc_fcr |= FCR_DSEL(0)|FCR_MTRON; /* XXX */
752 	} else {
753 		fdc->sc_fcr &= ~(FCR_DSEL(0)|FCR_MTRON); /* XXX */
754 	}
755 	FCR_REG_SYNC();
756 }
757 
758 void
759 fd_motor_off(arg)
760 	void *arg;
761 {
762 	struct fd_softc *fd = arg;
763 	int s;
764 
765 	s = splbio();
766 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
767 	fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
768 	splx(s);
769 }
770 
771 void
772 fd_motor_on(arg)
773 	void *arg;
774 {
775 	struct fd_softc *fd = arg;
776 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
777 	int s;
778 
779 	s = splbio();
780 	fd->sc_flags &= ~FD_MOTOR_WAIT;
781 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
782 		(void) fdcstate(fdc);
783 	splx(s);
784 }
785 
786 int
787 fdcresult(fdc)
788 	struct fdc_softc *fdc;
789 {
790 	u_char i;
791 	int j = 100000,
792 	    n = 0;
793 
794 	for (; j; j--) {
795 		i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
796 		if (i == NE7_RQM)
797 			return (fdc->sc_nstat = n);
798 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
799 			if (n >= sizeof(fdc->sc_status)) {
800 				log(LOG_ERR, "fdcresult: overrun\n");
801 				return (-1);
802 			}
803 			fdc->sc_status[n++] = *fdc->sc_reg_fifo;
804 		} else
805 			delay(10);
806 	}
807 	log(LOG_ERR, "fdcresult: timeout\n");
808 	return (fdc->sc_nstat = -1);
809 }
810 
811 int
812 out_fdc(fdc, x)
813 	struct fdc_softc *fdc;
814 	u_char x;
815 {
816 	int i = 100000;
817 
818 	while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0)
819 		delay(1);
820 	if (i <= 0)
821 		return (-1);
822 
823 	*fdc->sc_reg_fifo = x;
824 	return (0);
825 }
826 
827 int
828 fdopen(dev, flags, fmt, p)
829 	dev_t dev;
830 	int flags, fmt;
831 	struct proc *p;
832 {
833  	int unit, pmask;
834 	struct fd_softc *fd;
835 	struct fd_type *type;
836 
837 	unit = FDUNIT(dev);
838 	if (unit >= fd_cd.cd_ndevs)
839 		return (ENXIO);
840 	fd = fd_cd.cd_devs[unit];
841 	if (fd == 0)
842 		return (ENXIO);
843 	type = fd_dev_to_type(fd, dev);
844 	if (type == NULL)
845 		return (ENXIO);
846 
847 	if ((fd->sc_flags & FD_OPEN) != 0 &&
848 	    fd->sc_type != type)
849 		return (EBUSY);
850 
851 	fd->sc_type = type;
852 	fd->sc_cylin = -1;
853 	fd->sc_flags |= FD_OPEN;
854 
855 	/*
856 	 * Only update the disklabel if we're not open anywhere else.
857 	 */
858 	if (fd->sc_dk.dk_openmask == 0)
859 		fdgetdisklabel(dev);
860 
861 	pmask = (1 << DISKPART(dev));
862 
863 	switch (fmt) {
864 	case S_IFCHR:
865 		fd->sc_dk.dk_copenmask |= pmask;
866 		break;
867 
868 	case S_IFBLK:
869 		fd->sc_dk.dk_bopenmask |= pmask;
870 		break;
871 	}
872 	fd->sc_dk.dk_openmask =
873 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
874 
875 	return (0);
876 }
877 
878 int
879 fdclose(dev, flags, fmt, p)
880 	dev_t dev;
881 	int flags, fmt;
882 	struct proc *p;
883 {
884 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
885 	int pmask = (1 << DISKPART(dev));
886 
887 	fd->sc_flags &= ~FD_OPEN;
888 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
889 
890 	switch (fmt) {
891 	case S_IFCHR:
892 		fd->sc_dk.dk_copenmask &= ~pmask;
893 		break;
894 
895 	case S_IFBLK:
896 		fd->sc_dk.dk_bopenmask &= ~pmask;
897 		break;
898 	}
899 	fd->sc_dk.dk_openmask =
900 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
901 
902 	return (0);
903 }
904 
905 int
906 fdread(dev, uio, flag)
907         dev_t dev;
908         struct uio *uio;
909 	int flag;
910 {
911 
912         return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
913 }
914 
915 int
916 fdwrite(dev, uio, flag)
917         dev_t dev;
918         struct uio *uio;
919 	int flag;
920 {
921 
922         return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
923 }
924 
925 void
926 fdcstart(fdc)
927 	struct fdc_softc *fdc;
928 {
929 
930 #ifdef DIAGNOSTIC
931 	/* only got here if controller's drive queue was inactive; should
932 	   be in idle state */
933 	if (fdc->sc_state != DEVIDLE) {
934 		printf("fdcstart: not idle\n");
935 		return;
936 	}
937 #endif
938 	(void) fdcstate(fdc);
939 }
940 
941 void
942 fdcstatus(dv, n, s)
943 	struct device *dv;
944 	int n;
945 	char *s;
946 {
947 	struct fdc_softc *fdc = (void *)dv->dv_parent;
948 	char bits[64];
949 #if 0
950 	/*
951 	 * A 82072 seems to return <invalid command> on
952 	 * gratuitous Sense Interrupt commands.
953 	 */
954 	if (n == 0 && (fdc->sc_flags & FDC_82077)) {
955 		out_fdc(fdc, NE7CMD_SENSEI);
956 		(void) fdcresult(fdc);
957 		n = 2;
958 	}
959 #endif
960 
961 	/* Just print last status */
962 	n = fdc->sc_nstat;
963 
964 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
965 
966 	switch (n) {
967 	case 0:
968 		printf("\n");
969 		break;
970 	case 2:
971 		printf(" (st0 %s cyl %d)\n",
972 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
973 		    bits, sizeof(bits)), fdc->sc_status[1]);
974 		break;
975 	case 7:
976 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
977 		    NE7_ST0BITS, bits, sizeof(bits)));
978 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
979 		    NE7_ST1BITS, bits, sizeof(bits)));
980 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
981 		    NE7_ST2BITS, bits, sizeof(bits)));
982 		printf(" cyl %d head %d sec %d)\n",
983 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
984 		break;
985 #ifdef DIAGNOSTIC
986 	default:
987 		printf(" fdcstatus: weird size: %d\n", n);
988 		break;
989 #endif
990 	}
991 }
992 
993 void
994 fdctimeout(arg)
995 	void *arg;
996 {
997 	struct fdc_softc *fdc = arg;
998 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
999 	int s;
1000 
1001 	s = splbio();
1002 	fdcstatus(&fd->sc_dv, 0, "timeout");
1003 
1004 	if (BUFQ_PEEK(&fd->sc_q) != NULL)
1005 		fdc->sc_state++;
1006 	else
1007 		fdc->sc_state = DEVIDLE;
1008 
1009 	(void) fdcstate(fdc);
1010 	splx(s);
1011 }
1012 
1013 void
1014 fdcpseudointr(arg)
1015 	void *arg;
1016 {
1017 	struct fdc_softc *fdc = arg;
1018 	int s;
1019 
1020 	/* Just ensure it has the right spl. */
1021 	s = splbio();
1022 	(void) fdcstate(fdc);
1023 	splx(s);
1024 }
1025 
1026 
1027 /*
1028  * hardware interrupt entry point: must be converted to `fast'
1029  * (in-window) handler.
1030  */
1031 int
1032 fdchwintr(arg)
1033 	void *arg;
1034 {
1035 	struct fdc_softc *fdc = arg;
1036 
1037 	/*
1038 	 * This code was reverse engineered from the SPARC bsd_fdintr.s.
1039 	 */
1040 	switch (fdc->sc_istate) {
1041 	case ISTATE_IDLE:
1042 		return (0);
1043 	case ISTATE_SENSEI:
1044 		out_fdc(fdc, NE7CMD_SENSEI);
1045 		fdcresult(fdc);
1046 		fdc->sc_istate = ISTATE_DONE;
1047 		FD_SET_SWINTR();
1048 		return (1);
1049 	case ISTATE_DMA:
1050 		break;
1051 	default:
1052 		log(LOG_ERR, "fdc: stray hard interrupt.\n");
1053 		fdc->sc_fcr &= ~(FCR_DSEL(0));	/* Does this help? */
1054 		fdc->sc_istate = ISTATE_SPURIOUS;
1055 		FD_SET_SWINTR();
1056 		return (1);
1057 	}
1058 
1059 	for (;;) {
1060 		int msr;
1061 
1062 		msr = *fdc->sc_reg_msr;
1063 
1064 		if ((msr & NE7_RQM) == 0)
1065 			break;
1066 
1067 		if ((msr & NE7_NDM) == 0) {
1068 			fdcresult(fdc);
1069 			fdc->sc_istate = ISTATE_DONE;
1070 			FD_SET_SWINTR();
1071 			log(LOG_ERR, "fdc: overrun: tc = %d\n", fdc->sc_tc);
1072 			break;
1073 		}
1074 
1075 		if (msr & NE7_DIO) {
1076 			*fdc->sc_data++ = *fdc->sc_reg_fifo;
1077 		} else {
1078 			*fdc->sc_reg_fifo = *fdc->sc_data++;
1079 		}
1080 		if (--fdc->sc_tc == 0) {
1081 			fdc->sc_fcr |= FCR_TC;
1082 			FCR_REG_SYNC();
1083 			fdc->sc_istate = ISTATE_DONE;
1084 			delay(10);
1085 			fdc->sc_fcr &= ~FCR_TC;
1086 			FCR_REG_SYNC();
1087 			fdcresult(fdc);
1088 			FD_SET_SWINTR();
1089 			break;
1090 		}
1091 	}
1092 	return (1);
1093 }
1094 
1095 int
1096 fdcswintr(arg)
1097 	void *arg;
1098 {
1099 	struct fdc_softc *fdc = arg;
1100 	int s;
1101 
1102 	if (fdc_softpend == 0)
1103 		return (0);
1104 
1105 	isr_soft_clear(FDC_SOFTPRI);
1106 	fdc_softpend = 0;
1107 
1108 	if (fdc->sc_istate != ISTATE_DONE)
1109 		return (0);
1110 
1111 	fdc->sc_istate = ISTATE_IDLE;
1112 	s = splbio();
1113 	fdcstate(fdc);
1114 	splx(s);
1115 	return (1);
1116 }
1117 
1118 int
1119 fdcstate(fdc)
1120 	struct fdc_softc *fdc;
1121 {
1122 #define	st0	fdc->sc_status[0]
1123 #define	st1	fdc->sc_status[1]
1124 #define	cyl	fdc->sc_status[1]
1125 #define OUT_FDC(fdc, c, s) \
1126     do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
1127 
1128 	struct fd_softc *fd;
1129 	struct buf *bp;
1130 	int read, head, sec, nblks;
1131 	struct fd_type *type;
1132 	struct ne7_fd_formb *finfo = NULL;
1133 
1134 
1135 	if (fdc->sc_istate != ISTATE_IDLE) {
1136 		/* Trouble... */
1137 		printf("fdc: spurious interrupt: state %d, istate=%d\n",
1138 			fdc->sc_state, fdc->sc_istate);
1139 		fdc->sc_istate = ISTATE_IDLE;
1140 		if (fdc->sc_state == RESETCOMPLETE ||
1141 		    fdc->sc_state == RESETTIMEDOUT) {
1142 			panic("fdcintr: spurious interrupt can't be cleared");
1143 		}
1144 		goto doreset;
1145 	}
1146 
1147 loop:
1148 	/* Is there a drive for the controller to do a transfer with? */
1149 	fd = fdc->sc_drives.tqh_first;
1150 	if (fd == NULL) {
1151 		fdc->sc_state = DEVIDLE;
1152  		return (0);
1153 	}
1154 
1155 	/* Is there a transfer to this drive?  If not, deactivate drive. */
1156 	bp = BUFQ_PEEK(&fd->sc_q);
1157 	if (bp == NULL) {
1158 		fd->sc_ops = 0;
1159 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1160 		fd->sc_active = 0;
1161 		goto loop;
1162 	}
1163 
1164 	if (bp->b_flags & B_FORMAT)
1165 		finfo = (struct ne7_fd_formb *)bp->b_data;
1166 
1167 	switch (fdc->sc_state) {
1168 	case DEVIDLE:
1169 		fdc->sc_errors = 0;
1170 		fd->sc_skip = 0;
1171 		fd->sc_bcount = bp->b_bcount;
1172 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
1173 		callout_stop(&fd->sc_motoroff_ch);
1174 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1175 			fdc->sc_state = MOTORWAIT;
1176 			return (1);
1177 		}
1178 		if ((fd->sc_flags & FD_MOTOR) == 0) {
1179 			/* Turn on the motor, being careful about pairing. */
1180 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1181 			if (ofd && ofd->sc_flags & FD_MOTOR) {
1182 				callout_stop(&ofd->sc_motoroff_ch);
1183 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1184 			}
1185 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1186 			fd_set_motor(fdc);
1187 			fdc->sc_state = MOTORWAIT;
1188 			if (fdc->sc_flags & FDC_82077) { /* XXX */
1189 				/* Allow .25s for motor to stabilize. */
1190 				callout_reset(&fd->sc_motoron_ch, hz / 4,
1191 				    fd_motor_on, fd);
1192 			} else {
1193 				fd->sc_flags &= ~FD_MOTOR_WAIT;
1194 				goto loop;
1195 			}
1196 			return (1);
1197 		}
1198 		/* Make sure the right drive is selected. */
1199 		fd_set_motor(fdc);
1200 
1201 		/*FALLTHROUGH*/
1202 	case DOSEEK:
1203 	doseek:
1204 		if ((fdc->sc_flags & FDC_EIS) &&
1205 		    (bp->b_flags & B_FORMAT) == 0) {
1206 			fd->sc_cylin = bp->b_cylinder;
1207 			/* We use implied seek */
1208 			goto doio;
1209 		}
1210 
1211 		if (fd->sc_cylin == bp->b_cylinder)
1212 			goto doio;
1213 
1214 		/* specify command */
1215 		OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
1216 		OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
1217 		OUT_FDC(fdc, 6|1, SEEKTIMEDOUT);	/* XXX head load time == 6ms */
1218 
1219 		fdc->sc_istate = ISTATE_SENSEI;
1220 		/* seek function */
1221 		OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
1222 		OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
1223 		OUT_FDC(fdc, bp->b_cylinder * fd->sc_type->step, SEEKTIMEDOUT);
1224 
1225 		fd->sc_cylin = -1;
1226 		fdc->sc_state = SEEKWAIT;
1227 		fdc->sc_nstat = 0;
1228 
1229 		fd->sc_dk.dk_seek++;
1230 		disk_busy(&fd->sc_dk);
1231 
1232 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1233 		return (1);
1234 
1235 	case DOIO:
1236 	doio:
1237 #ifdef	NOTYET
1238 		/* Check to see if the disk has changed */
1239 		if (fdc->sc_reg_dir & FDI_DCHG) {
1240 			/*
1241 			 * The disk in the drive has changed since
1242 			 * the last transfer.  We need to see if its geometry
1243 			 * has changed.
1244 			 */
1245 		}
1246 #endif	/* NOTYET */
1247 
1248 		if (finfo)
1249 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1250 				      (char *)finfo;
1251 		type = fd->sc_type;
1252 		sec = fd->sc_blkno % type->seccyl;
1253 		nblks = type->seccyl - sec;
1254 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1255 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
1256 		fd->sc_nblks = nblks;
1257 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
1258 		head = sec / type->sectrac;
1259 		sec -= head * type->sectrac;
1260 #ifdef DIAGNOSTIC
1261 		{int block;
1262 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1263 		 if (block != fd->sc_blkno) {
1264 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1265 #ifdef DDB
1266 			 Debugger();
1267 #endif
1268 		 }}
1269 #endif
1270 		read = bp->b_flags & B_READ;
1271 
1272 		/* Setup for pseudo DMA */
1273 		fdc->sc_data = bp->b_data + fd->sc_skip;
1274 		fdc->sc_tc = fd->sc_nbytes;
1275 
1276 		*fdc->sc_reg_drs = type->rate;
1277 #ifdef FD_DEBUG
1278 		if (fdc_debug > 1)
1279 			printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1280 				read ? "read" : "write", fd->sc_drive,
1281 				fd->sc_cylin, head, sec, nblks);
1282 #endif
1283 		fdc->sc_state = IOCOMPLETE;
1284 		fdc->sc_istate = ISTATE_DMA;
1285 		fdc->sc_nstat = 0;
1286 		if (finfo) {
1287 			/* formatting */
1288 			OUT_FDC(fdc, NE7CMD_FORMAT, IOTIMEDOUT);
1289 			OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
1290 			OUT_FDC(fdc, finfo->fd_formb_secshift, IOTIMEDOUT);
1291 			OUT_FDC(fdc, finfo->fd_formb_nsecs, IOTIMEDOUT);
1292 			OUT_FDC(fdc, finfo->fd_formb_gaplen, IOTIMEDOUT);
1293 			OUT_FDC(fdc, finfo->fd_formb_fillbyte, IOTIMEDOUT);
1294 		} else {
1295 			if (read)
1296 				OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT);
1297 			else
1298 				OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT);
1299 			OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
1300 			OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT);	/*track*/
1301 			OUT_FDC(fdc, head, IOTIMEDOUT);
1302 			OUT_FDC(fdc, sec + 1, IOTIMEDOUT);	/*sector+1*/
1303 			OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/*sector size*/
1304 			OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/*secs/track*/
1305 			OUT_FDC(fdc, type->gap1, IOTIMEDOUT);	/*gap1 size*/
1306 			OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/*data length*/
1307 		}
1308 
1309 		disk_busy(&fd->sc_dk);
1310 
1311 		/* allow 2 seconds for operation */
1312 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
1313 		return (1);				/* will return later */
1314 
1315 	case SEEKWAIT:
1316 		callout_stop(&fdc->sc_timo_ch);
1317 		fdc->sc_state = SEEKCOMPLETE;
1318 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1319 			/* allow 1/50 second for heads to settle */
1320 			callout_reset(&fdc->sc_intr_ch, hz / 50,
1321 			    fdcpseudointr, fdc);
1322 			return (1);		/* will return later */
1323 		}
1324 		/*FALLTHROUGH*/
1325 	case SEEKCOMPLETE:
1326 		disk_unbusy(&fd->sc_dk, 0);	/* no data on seek */
1327 
1328 		/* Make sure seek really happened. */
1329 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1330 		    cyl != bp->b_cylinder * fd->sc_type->step) {
1331 #ifdef FD_DEBUG
1332 			if (fdc_debug)
1333 				fdcstatus(&fd->sc_dv, 2, "seek failed");
1334 #endif
1335 			fdcretry(fdc);
1336 			goto loop;
1337 		}
1338 		fd->sc_cylin = bp->b_cylinder;
1339 		goto doio;
1340 
1341 	case IOTIMEDOUT:
1342 		fdc->sc_fcr |= FCR_TC;
1343 		FCR_REG_SYNC();
1344 		delay(10);
1345 		fdc->sc_fcr &= ~FCR_TC;
1346 		FCR_REG_SYNC();
1347 		(void)fdcresult(fdc);
1348 		/*FALLTHROUGH*/
1349 	case SEEKTIMEDOUT:
1350 	case RECALTIMEDOUT:
1351 	case RESETTIMEDOUT:
1352 		fdcretry(fdc);
1353 		goto loop;
1354 
1355 	case IOCOMPLETE: /* IO DONE, post-analyze */
1356 		callout_stop(&fdc->sc_timo_ch);
1357 
1358 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
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