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