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