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