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