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