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