xref: /netbsd-src/sys/dev/isa/fd.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: fd.c,v 1.26 2002/09/11 07:22:19 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*-
40  * Copyright (c) 1990 The Regents of the University of California.
41  * All rights reserved.
42  *
43  * This code is derived from software contributed to Berkeley by
44  * Don Ahn.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *	This product includes software developed by the University of
57  *	California, Berkeley and its contributors.
58  * 4. Neither the name of the University nor the names of its contributors
59  *    may be used to endorse or promote products derived from this software
60  *    without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  *
74  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
75  */
76 
77 /*
78  * Floppy formatting facilities merged from FreeBSD fd.c driver:
79  *	Id: fd.c,v 1.53 1995/03/12 22:40:56 joerg Exp
80  * which carries the same copyright/redistribution notice as shown above with
81  * the addition of the following statement before the "Redistribution and
82  * use ..." clause:
83  *
84  * Copyright (c) 1993, 1994 by
85  *  jc@irbs.UUCP (John Capo)
86  *  vak@zebub.msk.su (Serge Vakulenko)
87  *  ache@astral.msk.su (Andrew A. Chernov)
88  *
89  * Copyright (c) 1993, 1994, 1995 by
90  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
91  *  dufault@hda.com (Peter Dufault)
92  */
93 
94 #include <sys/cdefs.h>
95 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.26 2002/09/11 07:22:19 thorpej Exp $");
96 
97 #include "rnd.h"
98 #include "opt_ddb.h"
99 
100 /*
101  * XXX This driver should be properly MI'd some day, but this allows us
102  * XXX to eliminate a lot of code duplication for now.
103  */
104 #if !defined(alpha) && !defined(algor) && !defined(atari) && \
105     !defined(bebox) && !defined(evbmips) && !defined(i386) && \
106     !defined(prep) && !defined(sandpoint) && !defined(x86_64)
107 #error platform not supported by this driver, yet
108 #endif
109 
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/callout.h>
113 #include <sys/kernel.h>
114 #include <sys/file.h>
115 #include <sys/ioctl.h>
116 #include <sys/device.h>
117 #include <sys/disklabel.h>
118 #include <sys/dkstat.h>
119 #include <sys/disk.h>
120 #include <sys/buf.h>
121 #include <sys/malloc.h>
122 #include <sys/uio.h>
123 #include <sys/syslog.h>
124 #include <sys/queue.h>
125 #include <sys/proc.h>
126 #include <sys/fdio.h>
127 #include <sys/conf.h>
128 #if NRND > 0
129 #include <sys/rnd.h>
130 #endif
131 
132 #include <uvm/uvm_extern.h>
133 
134 #include <dev/cons.h>
135 
136 #include <machine/cpu.h>
137 #include <machine/bus.h>
138 
139 #if defined(atari)
140 /*
141  * On the atari, it is configured as fdcisa
142  */
143 #define	FDCCF_DRIVE		FDCISACF_DRIVE
144 #define	FDCCF_DRIVE_DEFAULT	FDCISACF_DRIVE_DEFAULT
145 
146 #define	fd_cd	fdisa_cd
147 #define	fd_ca	fdisa_ca
148 #endif /* atari */
149 
150 #include <machine/intr.h>
151 
152 #include <dev/isa/isavar.h>
153 #include <dev/isa/isadmavar.h>
154 
155 #include <dev/isa/fdreg.h>
156 #include <dev/isa/fdcvar.h>
157 
158 #if defined(i386)
159 
160 #include <dev/ic/mc146818reg.h>			/* for NVRAM access */
161 #include <i386/isa/nvram.h>
162 
163 #include "mca.h"
164 #if NMCA > 0
165 #include <machine/mca_machdep.h>		/* for MCA_system */
166 #endif
167 
168 #endif /* i386 */
169 
170 #define FDUNIT(dev)	(minor(dev) / 8)
171 #define FDTYPE(dev)	(minor(dev) % 8)
172 
173 /* XXX misuse a flag to identify format operation */
174 #define B_FORMAT B_XXX
175 
176 /* controller driver configuration */
177 int fdprint __P((void *, const char *));
178 
179 /*
180  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
181  * we tell them apart.
182  */
183 struct fd_type {
184 	int	sectrac;	/* sectors per track */
185 	int	heads;		/* number of heads */
186 	int	seccyl;		/* sectors per cylinder */
187 	int	secsize;	/* size code for sectors */
188 	int	datalen;	/* data len when secsize = 0 */
189 	int	steprate;	/* step rate and head unload time */
190 	int	gap1;		/* gap len between sectors */
191 	int	gap2;		/* formatting gap */
192 	int	cyls;		/* total num of cylinders */
193 	int	size;		/* size of disk in sectors */
194 	int	step;		/* steps per cylinder */
195 	int	rate;		/* transfer speed code */
196 	u_char	fillbyte;	/* format fill byte */
197 	u_char	interleave;	/* interleave factor (formatting) */
198 	const char	*name;
199 };
200 
201 #if NMCA > 0
202 /* MCA - specific entries */
203 const struct fd_type mca_fd_types[] = {
204 	{ 18,2,36,2,0xff,0x0f,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette - XXX try 16ms step rate */
205 	{  9,2,18,2,0xff,0x4f,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette - XXX try 24ms step rate */
206 };
207 #endif /* NMCA > 0 */
208 
209 /* The order of entries in the following table is important -- BEWARE! */
210 
211 #if defined(atari)
212 const struct fd_type fd_types[] = {
213 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
214 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette */
215 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"   }, /* 1.44MB diskette */
216 };
217 #else
218 const struct fd_type fd_types[] = {
219 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"   }, /* 1.44MB diskette */
220 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB"    }, /* 1.2 MB AT-diskettes */
221 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */
222 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
223 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette */
224 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x"  }, /* 720kB in 1.2MB drive */
225 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
226 };
227 #endif /* defined(atari) */
228 
229 /* software state, per disk (with up to 4 disks per ctlr) */
230 struct fd_softc {
231 	struct device sc_dev;
232 	struct disk sc_dk;
233 
234 	const struct fd_type *sc_deftype; /* default type descriptor */
235 	struct fd_type *sc_type;	/* current type descriptor */
236 	struct fd_type sc_type_copy;	/* copy for fiddling when formatting */
237 
238 	struct callout sc_motoron_ch;
239 	struct callout sc_motoroff_ch;
240 
241 	daddr_t	sc_blkno;	/* starting block number */
242 	int sc_bcount;		/* byte count left */
243  	int sc_opts;		/* user-set options */
244 	int sc_skip;		/* bytes already transferred */
245 	int sc_nblks;		/* number of blocks currently transferring */
246 	int sc_nbytes;		/* number of bytes currently transferring */
247 
248 	int sc_drive;		/* physical unit number */
249 	int sc_flags;
250 #define	FD_OPEN		0x01		/* it's open */
251 #define	FD_MOTOR	0x02		/* motor should be on */
252 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
253 	int sc_cylin;		/* where we think the head is */
254 
255 	void *sc_sdhook;	/* saved shutdown hook for drive. */
256 
257 	TAILQ_ENTRY(fd_softc) sc_drivechain;
258 	int sc_ops;		/* I/O ops since last switch */
259 	struct bufq_state sc_q;	/* pending I/O requests */
260 	int sc_active;		/* number of active I/O operations */
261 
262 #if NRND > 0
263 	rndsource_element_t	rnd_source;
264 #endif
265 };
266 
267 int fdprobe __P((struct device *, struct cfdata *, void *));
268 void fdattach __P((struct device *, struct device *, void *));
269 
270 extern struct cfdriver fd_cd;
271 
272 struct cfattach fd_ca = {
273 	sizeof(struct fd_softc), fdprobe, fdattach,
274 };
275 
276 dev_type_open(fdopen);
277 dev_type_close(fdclose);
278 dev_type_read(fdread);
279 dev_type_write(fdwrite);
280 dev_type_ioctl(fdioctl);
281 dev_type_strategy(fdstrategy);
282 
283 const struct bdevsw fd_bdevsw = {
284 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
285 };
286 
287 const struct cdevsw fd_cdevsw = {
288 	fdopen, fdclose, fdread, fdwrite, fdioctl,
289 	nostop, notty, nopoll, nommap, D_DISK
290 };
291 
292 void fdgetdisklabel __P((struct fd_softc *));
293 int fd_get_parms __P((struct fd_softc *));
294 void fdstart __P((struct fd_softc *));
295 
296 struct dkdriver fddkdriver = { fdstrategy };
297 
298 #if defined(i386)
299 const struct fd_type *fd_nvtotype __P((char *, int, int));
300 #endif /* i386 */
301 void fd_set_motor __P((struct fdc_softc *fdc, int reset));
302 void fd_motor_off __P((void *arg));
303 void fd_motor_on __P((void *arg));
304 int fdcresult __P((struct fdc_softc *fdc));
305 void fdcstart __P((struct fdc_softc *fdc));
306 void fdcstatus __P((struct device *dv, int n, char *s));
307 void fdctimeout __P((void *arg));
308 void fdcpseudointr __P((void *arg));
309 void fdcretry __P((struct fdc_softc *fdc));
310 void fdfinish __P((struct fd_softc *fd, struct buf *bp));
311 __inline const struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
312 int fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
313 
314 void	fd_mountroot_hook __P((struct device *));
315 
316 /*
317  * Arguments passed between fdcattach and fdprobe.
318  */
319 struct fdc_attach_args {
320 	int fa_drive;
321 	const struct fd_type *fa_deftype;
322 };
323 
324 /*
325  * Print the location of a disk drive (called just before attaching the
326  * the drive).  If `fdc' is not NULL, the drive was found but was not
327  * in the system config file; print the drive name as well.
328  * Return QUIET (config_find ignores this if the device was configured) to
329  * avoid printing `fdN not configured' messages.
330  */
331 int
332 fdprint(aux, fdc)
333 	void *aux;
334 	const char *fdc;
335 {
336 	register struct fdc_attach_args *fa = aux;
337 
338 	if (!fdc)
339 		printf(" drive %d", fa->fa_drive);
340 	return QUIET;
341 }
342 
343 void
344 fdcattach(fdc)
345 	struct fdc_softc *fdc;
346 {
347 	struct fdc_attach_args fa;
348 	bus_space_tag_t iot;
349 	bus_space_handle_t ioh;
350 #if defined(i386)
351 	int type;
352 #endif
353 
354 	iot = fdc->sc_iot;
355 	ioh = fdc->sc_ioh;
356 	callout_init(&fdc->sc_timo_ch);
357 	callout_init(&fdc->sc_intr_ch);
358 
359 	fdc->sc_state = DEVIDLE;
360 	TAILQ_INIT(&fdc->sc_drives);
361 
362 	fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq);
363 
364 	if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize,
365 	    BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
366 		printf("%s: can't set up ISA DMA map\n",
367 		    fdc->sc_dev.dv_xname);
368 		return;
369 	}
370 
371 	/*
372 	 * Reset the controller to get it into a known state. Not all
373 	 * probes necessarily need do this to discover the controller up
374 	 * front, so don't assume anything.
375 	 */
376 
377 	bus_space_write_1(iot, ioh, fdout, 0);
378 	delay(100);
379 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
380 
381 	/* see if it can handle a command */
382 	if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) {
383 		printf ("%s: can't reset controller\n", fdc->sc_dev.dv_xname);
384 		return;
385 	}
386 	out_fdc(iot, ioh, 0xdf);
387 	out_fdc(iot, ioh, 2);
388 
389 #if defined(i386)
390 	/*
391 	 * The NVRAM info only tells us about the first two disks on the
392 	 * `primary' floppy controller.
393 	 */
394 	if (fdc->sc_dev.dv_unit == 0)
395 		type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */
396 	else
397 		type = -1;
398 #endif /* i386 */
399 
400 	/* physical limit: four drives per controller. */
401 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
402 #if defined(i386)
403 		if (type >= 0 && fa.fa_drive < 2)
404 			fa.fa_deftype = fd_nvtotype(fdc->sc_dev.dv_xname,
405 			    type, fa.fa_drive);
406 		else
407 			fa.fa_deftype = NULL;		/* unknown */
408 #elif defined(atari)
409 		/*
410 		 * Atari has a different ordening, defaults to 1.44
411 		 */
412 		fa.fa_deftype = &fd_types[2];
413 #else
414 		/*
415 		 * Default to 1.44MB on Alpha and BeBox.  How do we tell
416 		 * on these platforms?
417 		 */
418 		fa.fa_deftype = &fd_types[0];
419 #endif /* i386 */
420 		(void)config_found(&fdc->sc_dev, (void *)&fa, fdprint);
421 	}
422 }
423 
424 int
425 fdprobe(parent, match, aux)
426 	struct device *parent;
427 	struct cfdata *match;
428 	void *aux;
429 {
430 	struct fdc_softc *fdc = (void *)parent;
431 	struct cfdata *cf = match;
432 	struct fdc_attach_args *fa = aux;
433 	int drive = fa->fa_drive;
434 	bus_space_tag_t iot = fdc->sc_iot;
435 	bus_space_handle_t ioh = fdc->sc_ioh;
436 	int n;
437 
438 	if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
439 	    cf->cf_loc[FDCCF_DRIVE] != drive)
440 		return 0;
441 	/*
442 	 * XXX
443 	 * This is to work around some odd interactions between this driver
444 	 * and SMC Ethernet cards.
445 	 */
446 	if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2)
447 		return 0;
448 
449 	/* select drive and turn on motor */
450 	bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
451 	/* wait for motor to spin up */
452 	delay(250000);
453 	out_fdc(iot, ioh, NE7CMD_RECAL);
454 	out_fdc(iot, ioh, drive);
455 	/* wait for recalibrate */
456 	delay(2000000);
457 	out_fdc(iot, ioh, NE7CMD_SENSEI);
458 	n = fdcresult(fdc);
459 #ifdef FD_DEBUG
460 	{
461 		int i;
462 		printf("fdprobe: status");
463 		for (i = 0; i < n; i++)
464 			printf(" %x", fdc->sc_status[i]);
465 		printf("\n");
466 	}
467 #endif
468 	/* turn off motor */
469 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
470 
471 #if defined(bebox)	/* XXX What is this about? --thorpej@netbsd.org */
472 	if (n != 2 || (fdc->sc_status[1] != 0))
473 		return 0;
474 #else
475 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
476 		return 0;
477 #endif /* bebox */
478 
479 	return 1;
480 }
481 
482 /*
483  * Controller is working, and drive responded.  Attach it.
484  */
485 void
486 fdattach(parent, self, aux)
487 	struct device *parent, *self;
488 	void *aux;
489 {
490 	struct fdc_softc *fdc = (void *)parent;
491 	struct fd_softc *fd = (void *)self;
492 	struct fdc_attach_args *fa = aux;
493 	const struct fd_type *type = fa->fa_deftype;
494 	int drive = fa->fa_drive;
495 
496 	callout_init(&fd->sc_motoron_ch);
497 	callout_init(&fd->sc_motoroff_ch);
498 
499 	/* XXX Allow `flags' to override device type? */
500 
501 	if (type)
502 		printf(": %s, %d cyl, %d head, %d sec\n", type->name,
503 		    type->cyls, type->heads, type->sectrac);
504 	else
505 		printf(": density unknown\n");
506 
507 	bufq_alloc(&fd->sc_q, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER);
508 	fd->sc_cylin = -1;
509 	fd->sc_drive = drive;
510 	fd->sc_deftype = type;
511 	fdc->sc_fd[drive] = fd;
512 
513 	/*
514 	 * Initialize and attach the disk structure.
515 	 */
516 	fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
517 	fd->sc_dk.dk_driver = &fddkdriver;
518 	disk_attach(&fd->sc_dk);
519 
520 	/*
521 	 * Establish a mountroot hook.
522 	 */
523 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
524 
525 	/* Needed to power off if the motor is on when we halt. */
526 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
527 
528 #if NRND > 0
529 	rnd_attach_source(&fd->rnd_source, fd->sc_dev.dv_xname,
530 			  RND_TYPE_DISK, 0);
531 #endif
532 }
533 
534 #if defined(i386)
535 /*
536  * Translate nvram type into internal data structure.  Return NULL for
537  * none/unknown/unusable.
538  */
539 const struct fd_type *
540 fd_nvtotype(fdc, nvraminfo, drive)
541 	char *fdc;
542 	int nvraminfo, drive;
543 {
544 	int type;
545 
546 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
547 	switch (type) {
548 	case NVRAM_DISKETTE_NONE:
549 		return NULL;
550 	case NVRAM_DISKETTE_12M:
551 		return &fd_types[1];
552 	case NVRAM_DISKETTE_TYPE5:
553 	case NVRAM_DISKETTE_TYPE6:
554 		/* XXX We really ought to handle 2.88MB format. */
555 	case NVRAM_DISKETTE_144M:
556 #if NMCA > 0
557 		if (MCA_system)
558 			return &mca_fd_types[0];
559 		else
560 #endif /* NMCA > 0 */
561 			return &fd_types[0];
562 	case NVRAM_DISKETTE_360K:
563 		return &fd_types[3];
564 	case NVRAM_DISKETTE_720K:
565 #if NMCA > 0
566 		if (MCA_system)
567 			return &mca_fd_types[1];
568 		else
569 #endif /* NMCA > 0 */
570 			return &fd_types[4];
571 	default:
572 		printf("%s: drive %d: unknown device type 0x%x\n",
573 		    fdc, drive, type);
574 		return NULL;
575 	}
576 }
577 #endif /* i386 */
578 
579 __inline const struct fd_type *
580 fd_dev_to_type(fd, dev)
581 	struct fd_softc *fd;
582 	dev_t dev;
583 {
584 	u_int type = FDTYPE(dev);
585 
586 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
587 		return NULL;
588 	return type ? &fd_types[type - 1] : fd->sc_deftype;
589 }
590 
591 void
592 fdstrategy(bp)
593 	register struct buf *bp;	/* IO operation to perform */
594 {
595 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(bp->b_dev));
596 	int sz;
597  	int s;
598 
599 	/* Valid unit, controller, and request? */
600 	if (bp->b_blkno < 0 ||
601 	    ((bp->b_bcount % FDC_BSIZE) != 0 &&
602 	     (bp->b_flags & B_FORMAT) == 0)) {
603 		bp->b_error = EINVAL;
604 		goto bad;
605 	}
606 
607 	/* If it's a null transfer, return immediately. */
608 	if (bp->b_bcount == 0)
609 		goto done;
610 
611 	sz = howmany(bp->b_bcount, FDC_BSIZE);
612 
613 	if (bp->b_blkno + sz > fd->sc_type->size) {
614 		sz = fd->sc_type->size - bp->b_blkno;
615 		if (sz == 0) {
616 			/* If exactly at end of disk, return EOF. */
617 			goto done;
618 		}
619 		if (sz < 0) {
620 			/* If past end of disk, return EINVAL. */
621 			bp->b_error = EINVAL;
622 			goto bad;
623 		}
624 		/* Otherwise, truncate request. */
625 		bp->b_bcount = sz << DEV_BSHIFT;
626 	}
627 
628 	bp->b_rawblkno = bp->b_blkno;
629  	bp->b_cylinder =
630 	    bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
631 
632 #ifdef FD_DEBUG
633 	printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld sz %d\n",
634 	    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
635 #endif
636 
637 	/* Queue transfer on drive, activate drive and controller if idle. */
638 	s = splbio();
639 	BUFQ_PUT(&fd->sc_q, bp);
640 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
641 	if (fd->sc_active == 0)
642 		fdstart(fd);
643 #ifdef DIAGNOSTIC
644 	else {
645 		struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
646 		if (fdc->sc_state == DEVIDLE) {
647 			printf("fdstrategy: controller inactive\n");
648 			fdcstart(fdc);
649 		}
650 	}
651 #endif
652 	splx(s);
653 	return;
654 
655 bad:
656 	bp->b_flags |= B_ERROR;
657 done:
658 	/* Toss transfer; we're done early. */
659 	bp->b_resid = bp->b_bcount;
660 	biodone(bp);
661 }
662 
663 void
664 fdstart(fd)
665 	struct fd_softc *fd;
666 {
667 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
668 	int active = !TAILQ_EMPTY(&fdc->sc_drives);
669 
670 	/* Link into controller queue. */
671 	fd->sc_active = 1;
672 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
673 
674 	/* If controller not already active, start it. */
675 	if (!active)
676 		fdcstart(fdc);
677 }
678 
679 void
680 fdfinish(fd, bp)
681 	struct fd_softc *fd;
682 	struct buf *bp;
683 {
684 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
685 
686 	/*
687 	 * Move this drive to the end of the queue to give others a `fair'
688 	 * chance.  We only force a switch if N operations are completed while
689 	 * another drive is waiting to be serviced, since there is a long motor
690 	 * startup delay whenever we switch.
691 	 */
692 	(void)BUFQ_GET(&fd->sc_q);
693 	if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
694 		fd->sc_ops = 0;
695 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
696 		if (BUFQ_PEEK(&fd->sc_q) != NULL)
697 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
698 		else
699 			fd->sc_active = 0;
700 	}
701 	bp->b_resid = fd->sc_bcount;
702 	fd->sc_skip = 0;
703 
704 #if NRND > 0
705 	rnd_add_uint32(&fd->rnd_source, bp->b_blkno);
706 #endif
707 
708 	biodone(bp);
709 	/* turn off motor 5s from now */
710 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
711 	fdc->sc_state = DEVIDLE;
712 }
713 
714 int
715 fdread(dev, uio, flags)
716 	dev_t dev;
717 	struct uio *uio;
718 	int flags;
719 {
720 
721 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
722 }
723 
724 int
725 fdwrite(dev, uio, flags)
726 	dev_t dev;
727 	struct uio *uio;
728 	int flags;
729 {
730 
731 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
732 }
733 
734 void
735 fd_set_motor(fdc, reset)
736 	struct fdc_softc *fdc;
737 	int reset;
738 {
739 	struct fd_softc *fd;
740 	u_char status;
741 	int n;
742 
743 	if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
744 		status = fd->sc_drive;
745 	else
746 		status = 0;
747 	if (!reset)
748 		status |= FDO_FRST | FDO_FDMAEN;
749 	for (n = 0; n < 4; n++)
750 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
751 			status |= FDO_MOEN(n);
752 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
753 }
754 
755 void
756 fd_motor_off(arg)
757 	void *arg;
758 {
759 	struct fd_softc *fd = arg;
760 	int s;
761 
762 	s = splbio();
763 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
764 	fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0);
765 	splx(s);
766 }
767 
768 void
769 fd_motor_on(arg)
770 	void *arg;
771 {
772 	struct fd_softc *fd = arg;
773 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
774 	int s;
775 
776 	s = splbio();
777 	fd->sc_flags &= ~FD_MOTOR_WAIT;
778 	if ((TAILQ_FIRST(&fdc->sc_drives) == fd) &&(fdc->sc_state == MOTORWAIT))
779 		(void) fdcintr(fdc);
780 	splx(s);
781 }
782 
783 int
784 fdcresult(fdc)
785 	struct fdc_softc *fdc;
786 {
787 	bus_space_tag_t iot = fdc->sc_iot;
788 	bus_space_handle_t ioh = fdc->sc_ioh;
789 	u_char i;
790 	u_int j = 100000,
791 	      n = 0;
792 
793 	for (; j; j--) {
794 		i = bus_space_read_1(iot, ioh, fdsts) &
795 		    (NE7_DIO | NE7_RQM | NE7_CB);
796 		if (i == NE7_RQM)
797 			return n;
798 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
799 			if (n >= sizeof(fdc->sc_status)) {
800 				log(LOG_ERR, "fdcresult: overrun\n");
801 				return -1;
802 			}
803 			fdc->sc_status[n++] =
804 			    bus_space_read_1(iot, ioh, fddata);
805 		}
806 		delay(10);
807 	}
808 	log(LOG_ERR, "fdcresult: timeout\n");
809 	return -1;
810 }
811 
812 int
813 out_fdc(iot, ioh, x)
814 	bus_space_tag_t iot;
815 	bus_space_handle_t ioh;
816 	u_char x;
817 {
818 	int i = 100000;
819 
820 	while ((bus_space_read_1(iot, ioh, fdsts) & NE7_DIO) && i-- > 0);
821 	if (i <= 0)
822 		return -1;
823 	while ((bus_space_read_1(iot, ioh, fdsts) & NE7_RQM) == 0 && i-- > 0);
824 	if (i <= 0)
825 		return -1;
826 	bus_space_write_1(iot, ioh, fddata, x);
827 	return 0;
828 }
829 
830 int
831 fdopen(dev, flags, mode, p)
832 	dev_t dev;
833 	int flags;
834 	int mode;
835 	struct proc *p;
836 {
837 	struct fd_softc *fd;
838 	const struct fd_type *type;
839 
840 	fd = device_lookup(&fd_cd, FDUNIT(dev));
841 	if (fd == NULL)
842 		return (ENXIO);
843 
844 	type = fd_dev_to_type(fd, dev);
845 	if (type == NULL)
846 		return ENXIO;
847 
848 	if ((fd->sc_flags & FD_OPEN) != 0 &&
849 	    memcmp(fd->sc_type, type, sizeof(*type)))
850 		return EBUSY;
851 
852 	fd->sc_type_copy = *type;
853 	fd->sc_type = &fd->sc_type_copy;
854 	fd->sc_cylin = -1;
855 	fd->sc_flags |= FD_OPEN;
856 
857 	return 0;
858 }
859 
860 int
861 fdclose(dev, flags, mode, p)
862 	dev_t dev;
863 	int flags;
864 	int mode;
865 	struct proc *p;
866 {
867 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
868 
869 	fd->sc_flags &= ~FD_OPEN;
870 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
871 	return 0;
872 }
873 
874 void
875 fdcstart(fdc)
876 	struct fdc_softc *fdc;
877 {
878 
879 #ifdef DIAGNOSTIC
880 	/* only got here if controller's drive queue was inactive; should
881 	   be in idle state */
882 	if (fdc->sc_state != DEVIDLE) {
883 		printf("fdcstart: not idle\n");
884 		return;
885 	}
886 #endif
887 	(void) fdcintr(fdc);
888 }
889 
890 void
891 fdcstatus(dv, n, s)
892 	struct device *dv;
893 	int n;
894 	char *s;
895 {
896 	struct fdc_softc *fdc = (void *)dv->dv_parent;
897 	char bits[64];
898 
899 	if (n == 0) {
900 		out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
901 		(void) fdcresult(fdc);
902 		n = 2;
903 	}
904 
905 	printf("%s: %s", dv->dv_xname, s);
906 
907 	switch (n) {
908 	case 0:
909 		printf("\n");
910 		break;
911 	case 2:
912 		printf(" (st0 %s cyl %d)\n",
913 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
914 		    bits, sizeof(bits)), fdc->sc_status[1]);
915 		break;
916 	case 7:
917 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
918 		    NE7_ST0BITS, bits, sizeof(bits)));
919 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
920 		    NE7_ST1BITS, bits, sizeof(bits)));
921 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
922 		    NE7_ST2BITS, bits, sizeof(bits)));
923 		printf(" cyl %d head %d sec %d)\n",
924 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
925 		break;
926 #ifdef DIAGNOSTIC
927 	default:
928 		printf("\nfdcstatus: weird size");
929 		break;
930 #endif
931 	}
932 }
933 
934 void
935 fdctimeout(arg)
936 	void *arg;
937 {
938 	struct fdc_softc *fdc = arg;
939 	struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
940 	int s;
941 
942 	s = splbio();
943 #ifdef DEBUG
944 	log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
945 #endif
946 	fdcstatus(&fd->sc_dev, 0, "timeout");
947 
948 	if (BUFQ_PEEK(&fd->sc_q) != NULL)
949 		fdc->sc_state++;
950 	else
951 		fdc->sc_state = DEVIDLE;
952 
953 	(void) fdcintr(fdc);
954 	splx(s);
955 }
956 
957 void
958 fdcpseudointr(arg)
959 	void *arg;
960 {
961 	int s;
962 
963 	/* Just ensure it has the right spl. */
964 	s = splbio();
965 	(void) fdcintr(arg);
966 	splx(s);
967 }
968 
969 int
970 fdcintr(arg)
971 	void *arg;
972 {
973 	struct fdc_softc *fdc = arg;
974 #define	st0	fdc->sc_status[0]
975 #define	cyl	fdc->sc_status[1]
976 	struct fd_softc *fd;
977 	struct buf *bp;
978 	bus_space_tag_t iot = fdc->sc_iot;
979 	bus_space_handle_t ioh = fdc->sc_ioh;
980 	int read, head, sec, i, nblks;
981 	struct fd_type *type;
982 	struct ne7_fd_formb *finfo = NULL;
983 
984 loop:
985 	/* Is there a drive for the controller to do a transfer with? */
986 	fd = TAILQ_FIRST(&fdc->sc_drives);
987 	if (fd == NULL) {
988 		fdc->sc_state = DEVIDLE;
989  		return 1;
990 	}
991 
992 	/* Is there a transfer to this drive?  If not, deactivate drive. */
993 	bp = BUFQ_PEEK(&fd->sc_q);
994 	if (bp == NULL) {
995 		fd->sc_ops = 0;
996 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
997 		fd->sc_active = 0;
998 		goto loop;
999 	}
1000 
1001 	if (bp->b_flags & B_FORMAT)
1002 		finfo = (struct ne7_fd_formb *)bp->b_data;
1003 
1004 	switch (fdc->sc_state) {
1005 	case DEVIDLE:
1006 		fdc->sc_errors = 0;
1007 		fd->sc_skip = 0;
1008 		fd->sc_bcount = bp->b_bcount;
1009 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
1010 		callout_stop(&fd->sc_motoroff_ch);
1011 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1012 			fdc->sc_state = MOTORWAIT;
1013 			return 1;
1014 		}
1015 		if ((fd->sc_flags & FD_MOTOR) == 0) {
1016 			/* Turn on the motor, being careful about pairing. */
1017 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1018 			if (ofd && ofd->sc_flags & FD_MOTOR) {
1019 				callout_stop(&ofd->sc_motoroff_ch);
1020 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1021 			}
1022 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1023 			fd_set_motor(fdc, 0);
1024 			fdc->sc_state = MOTORWAIT;
1025 			/* Allow .25s for motor to stabilize. */
1026 			callout_reset(&fd->sc_motoron_ch, hz / 4,
1027 			    fd_motor_on, fd);
1028 			return 1;
1029 		}
1030 		/* Make sure the right drive is selected. */
1031 		fd_set_motor(fdc, 0);
1032 
1033 		/* fall through */
1034 	case DOSEEK:
1035 	doseek:
1036 		if (fd->sc_cylin == bp->b_cylinder)
1037 			goto doio;
1038 
1039 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
1040 		out_fdc(iot, ioh, fd->sc_type->steprate);
1041 		out_fdc(iot, ioh, 6);		/* XXX head load time == 6ms */
1042 
1043 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
1044 		out_fdc(iot, ioh, fd->sc_drive); /* drive number */
1045 		out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
1046 
1047 		fd->sc_cylin = -1;
1048 		fdc->sc_state = SEEKWAIT;
1049 
1050 		fd->sc_dk.dk_seek++;
1051 		disk_busy(&fd->sc_dk);
1052 
1053 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1054 		return 1;
1055 
1056 	case DOIO:
1057 	doio:
1058 		type = fd->sc_type;
1059 		if (finfo)
1060 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1061 				      (char *)finfo;
1062 		sec = fd->sc_blkno % type->seccyl;
1063 		nblks = type->seccyl - sec;
1064 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1065 		nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
1066 		fd->sc_nblks = nblks;
1067 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
1068 		head = sec / type->sectrac;
1069 		sec -= head * type->sectrac;
1070 #ifdef DIAGNOSTIC
1071 		{
1072 			int block;
1073 			block = (fd->sc_cylin * type->heads + head)
1074 			    * type->sectrac + sec;
1075 			if (block != fd->sc_blkno) {
1076 				printf("fdcintr: block %d != blkno %d\n",
1077 				    block, fd->sc_blkno);
1078 #ifdef DDB
1079 				 Debugger();
1080 #endif
1081 			}
1082 		}
1083 #endif
1084 		read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
1085 		isa_dmastart(fdc->sc_ic, fdc->sc_drq,
1086 		    bp->b_data + fd->sc_skip, fd->sc_nbytes,
1087 		    NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT);
1088 		bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate);
1089 #ifdef FD_DEBUG
1090 		printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1091 			read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
1092 			head, sec, nblks);
1093 #endif
1094 		if (finfo) {
1095 			/* formatting */
1096 			if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
1097 				fdc->sc_errors = 4;
1098 				fdcretry(fdc);
1099 				goto loop;
1100 			}
1101 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1102 			out_fdc(iot, ioh, finfo->fd_formb_secshift);
1103 			out_fdc(iot, ioh, finfo->fd_formb_nsecs);
1104 			out_fdc(iot, ioh, finfo->fd_formb_gaplen);
1105 			out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
1106 		} else {
1107 			if (read)
1108 				out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
1109 			else
1110 				out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
1111 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1112 			out_fdc(iot, ioh, fd->sc_cylin); /* track */
1113 			out_fdc(iot, ioh, head);
1114 			out_fdc(iot, ioh, sec + 1);	 /* sector +1 */
1115 			out_fdc(iot, ioh, type->secsize);/* sector size */
1116 			out_fdc(iot, ioh, type->sectrac);/* sectors/track */
1117 			out_fdc(iot, ioh, type->gap1);	 /* gap1 size */
1118 			out_fdc(iot, ioh, type->datalen);/* data length */
1119 		}
1120 		fdc->sc_state = IOCOMPLETE;
1121 
1122 		disk_busy(&fd->sc_dk);
1123 
1124 		/* allow 2 seconds for operation */
1125 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
1126 		return 1;				/* will return later */
1127 
1128 	case SEEKWAIT:
1129 		callout_stop(&fdc->sc_timo_ch);
1130 		fdc->sc_state = SEEKCOMPLETE;
1131 		/* allow 1/50 second for heads to settle */
1132 		callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
1133 		return 1;
1134 
1135 	case SEEKCOMPLETE:
1136 		disk_unbusy(&fd->sc_dk, 0);	/* no data on seek */
1137 
1138 		/* Make sure seek really happened. */
1139 		out_fdc(iot, ioh, NE7CMD_SENSEI);
1140 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
1141 		    cyl != bp->b_cylinder * fd->sc_type->step) {
1142 #ifdef FD_DEBUG
1143 			fdcstatus(&fd->sc_dev, 2, "seek failed");
1144 #endif
1145 			fdcretry(fdc);
1146 			goto loop;
1147 		}
1148 		fd->sc_cylin = bp->b_cylinder;
1149 		goto doio;
1150 
1151 	case IOTIMEDOUT:
1152 		isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1153 	case SEEKTIMEDOUT:
1154 	case RECALTIMEDOUT:
1155 	case RESETTIMEDOUT:
1156 		fdcretry(fdc);
1157 		goto loop;
1158 
1159 	case IOCOMPLETE: /* IO DONE, post-analyze */
1160 		callout_stop(&fdc->sc_timo_ch);
1161 
1162 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
1163 
1164 		if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
1165 			isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1166 #ifdef FD_DEBUG
1167 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
1168 			    "read failed" : "write failed");
1169 			printf("blkno %d nblks %d\n",
1170 			    fd->sc_blkno, fd->sc_nblks);
1171 #endif
1172 			fdcretry(fdc);
1173 			goto loop;
1174 		}
1175 		isa_dmadone(fdc->sc_ic, fdc->sc_drq);
1176 		if (fdc->sc_errors) {
1177 			diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
1178 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1179 			printf("\n");
1180 			fdc->sc_errors = 0;
1181 		}
1182 		fd->sc_blkno += fd->sc_nblks;
1183 		fd->sc_skip += fd->sc_nbytes;
1184 		fd->sc_bcount -= fd->sc_nbytes;
1185 		if (!finfo && fd->sc_bcount > 0) {
1186 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1187 			goto doseek;
1188 		}
1189 		fdfinish(fd, bp);
1190 		goto loop;
1191 
1192 	case DORESET:
1193 		/* try a reset, keep motor on */
1194 		fd_set_motor(fdc, 1);
1195 		delay(100);
1196 		fd_set_motor(fdc, 0);
1197 		fdc->sc_state = RESETCOMPLETE;
1198 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1199 		return 1;			/* will return later */
1200 
1201 	case RESETCOMPLETE:
1202 		callout_stop(&fdc->sc_timo_ch);
1203 		/* clear the controller output buffer */
1204 		for (i = 0; i < 4; i++) {
1205 			out_fdc(iot, ioh, NE7CMD_SENSEI);
1206 			(void) fdcresult(fdc);
1207 		}
1208 
1209 		/* fall through */
1210 	case DORECAL:
1211 		out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
1212 		out_fdc(iot, ioh, fd->sc_drive);
1213 		fdc->sc_state = RECALWAIT;
1214 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1215 		return 1;			/* will return later */
1216 
1217 	case RECALWAIT:
1218 		callout_stop(&fdc->sc_timo_ch);
1219 		fdc->sc_state = RECALCOMPLETE;
1220 		/* allow 1/30 second for heads to settle */
1221 		callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
1222 		return 1;			/* will return later */
1223 
1224 	case RECALCOMPLETE:
1225 		out_fdc(iot, ioh, NE7CMD_SENSEI);
1226 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1227 #ifdef FD_DEBUG
1228 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
1229 #endif
1230 			fdcretry(fdc);
1231 			goto loop;
1232 		}
1233 		fd->sc_cylin = 0;
1234 		goto doseek;
1235 
1236 	case MOTORWAIT:
1237 		if (fd->sc_flags & FD_MOTOR_WAIT)
1238 			return 1;		/* time's not up yet */
1239 		goto doseek;
1240 
1241 	default:
1242 		fdcstatus(&fd->sc_dev, 0, "stray interrupt");
1243 		return 1;
1244 	}
1245 #ifdef DIAGNOSTIC
1246 	panic("fdcintr: impossible");
1247 #endif
1248 #undef	st0
1249 #undef	cyl
1250 }
1251 
1252 void
1253 fdcretry(fdc)
1254 	struct fdc_softc *fdc;
1255 {
1256 	char bits[64];
1257 	struct fd_softc *fd;
1258 	struct buf *bp;
1259 
1260 	fd = TAILQ_FIRST(&fdc->sc_drives);
1261 	bp = BUFQ_PEEK(&fd->sc_q);
1262 
1263 	if (fd->sc_opts & FDOPT_NORETRY)
1264 	    goto fail;
1265 	switch (fdc->sc_errors) {
1266 	case 0:
1267 		/* try again */
1268 		fdc->sc_state = DOSEEK;
1269 		break;
1270 
1271 	case 1: case 2: case 3:
1272 		/* didn't work; try recalibrating */
1273 		fdc->sc_state = DORECAL;
1274 		break;
1275 
1276 	case 4:
1277 		/* still no go; reset the bastard */
1278 		fdc->sc_state = DORESET;
1279 		break;
1280 
1281 	default:
1282 	fail:
1283 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1284 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
1285 				fd->sc_skip / FDC_BSIZE,
1286 				(struct disklabel *)NULL);
1287 
1288 			printf(" (st0 %s",
1289 			       bitmask_snprintf(fdc->sc_status[0],
1290 						NE7_ST0BITS, bits,
1291 						sizeof(bits)));
1292 			printf(" st1 %s",
1293 			       bitmask_snprintf(fdc->sc_status[1],
1294 						NE7_ST1BITS, bits,
1295 						sizeof(bits)));
1296 			printf(" st2 %s",
1297 			       bitmask_snprintf(fdc->sc_status[2],
1298 						NE7_ST2BITS, bits,
1299 						sizeof(bits)));
1300 			printf(" cyl %d head %d sec %d)\n",
1301 			       fdc->sc_status[3],
1302 			       fdc->sc_status[4],
1303 			       fdc->sc_status[5]);
1304 		}
1305 
1306 		bp->b_flags |= B_ERROR;
1307 		bp->b_error = EIO;
1308 		fdfinish(fd, bp);
1309 	}
1310 	fdc->sc_errors++;
1311 }
1312 
1313 int
1314 fdioctl(dev, cmd, addr, flag, p)
1315 	dev_t dev;
1316 	u_long cmd;
1317 	caddr_t addr;
1318 	int flag;
1319 	struct proc *p;
1320 {
1321 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1322 	struct fdformat_parms *form_parms;
1323 	struct fdformat_cmd *form_cmd;
1324 	struct ne7_fd_formb *fd_formb;
1325 	struct disklabel buffer;
1326 	int error;
1327 	unsigned int scratch;
1328 	int il[FD_MAX_NSEC + 1];
1329 	register int i, j;
1330 #ifdef __HAVE_OLD_DISKLABEL
1331 	struct disklabel newlabel;
1332 #endif
1333 
1334 	switch (cmd) {
1335 	case DIOCGDINFO:
1336 #ifdef __HAVE_OLD_DISKLABEL
1337 	case ODIOCGDINFO:
1338 #endif
1339 		memset(&buffer, 0, sizeof(buffer));
1340 
1341 		buffer.d_secpercyl = fd->sc_type->seccyl;
1342 		buffer.d_type = DTYPE_FLOPPY;
1343 		buffer.d_secsize = FDC_BSIZE;
1344 
1345 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1346 			return EINVAL;
1347 
1348 #ifdef __HAVE_OLD_DISKLABEL
1349 		if (cmd == ODIOCGDINFO) {
1350 			if (buffer.d_npartitions > OLDMAXPARTITIONS)
1351 				return ENOTTY;
1352 			memcpy(addr, &buffer, sizeof (struct olddisklabel));
1353 		} else
1354 #endif
1355 		*(struct disklabel *)addr = buffer;
1356 		return 0;
1357 
1358 	case DIOCWLABEL:
1359 		if ((flag & FWRITE) == 0)
1360 			return EBADF;
1361 		/* XXX do something */
1362 		return 0;
1363 
1364 	case DIOCWDINFO:
1365 #ifdef __HAVE_OLD_DISKLABEL
1366 	case ODIOCWDINFO:
1367 #endif
1368 	{
1369 		struct disklabel *lp;
1370 
1371 		if ((flag & FWRITE) == 0)
1372 			return EBADF;
1373 #ifdef __HAVE_OLD_DISKLABEL
1374 		if (cmd == ODIOCWDINFO) {
1375 			memset(&newlabel, 0, sizeof newlabel);
1376 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1377 			lp = &newlabel;
1378 		} else
1379 #endif
1380 		lp = (struct disklabel *)addr;
1381 
1382 		error = setdisklabel(&buffer, lp, 0, NULL);
1383 		if (error)
1384 			return error;
1385 
1386 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1387 		return error;
1388 	}
1389 
1390 	case FDIOCGETFORMAT:
1391 		form_parms = (struct fdformat_parms *)addr;
1392 		form_parms->fdformat_version = FDFORMAT_VERSION;
1393 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1394 		form_parms->ncyl = fd->sc_type->cyls;
1395 		form_parms->nspt = fd->sc_type->sectrac;
1396 		form_parms->ntrk = fd->sc_type->heads;
1397 		form_parms->stepspercyl = fd->sc_type->step;
1398 		form_parms->gaplen = fd->sc_type->gap2;
1399 		form_parms->fillbyte = fd->sc_type->fillbyte;
1400 		form_parms->interleave = fd->sc_type->interleave;
1401 		switch (fd->sc_type->rate) {
1402 		case FDC_500KBPS:
1403 			form_parms->xfer_rate = 500 * 1024;
1404 			break;
1405 		case FDC_300KBPS:
1406 			form_parms->xfer_rate = 300 * 1024;
1407 			break;
1408 		case FDC_250KBPS:
1409 			form_parms->xfer_rate = 250 * 1024;
1410 			break;
1411 		default:
1412 			return EINVAL;
1413 		}
1414 		return 0;
1415 
1416 	case FDIOCSETFORMAT:
1417 		if((flag & FWRITE) == 0)
1418 			return EBADF;	/* must be opened for writing */
1419 		form_parms = (struct fdformat_parms *)addr;
1420 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
1421 			return EINVAL;	/* wrong version of formatting prog */
1422 
1423 		scratch = form_parms->nbps >> 7;
1424 		if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
1425 		    scratch & ~(1 << (ffs(scratch)-1)))
1426 			/* not a power-of-two multiple of 128 */
1427 			return EINVAL;
1428 
1429 		switch (form_parms->xfer_rate) {
1430 		case 500 * 1024:
1431 			fd->sc_type->rate = FDC_500KBPS;
1432 			break;
1433 		case 300 * 1024:
1434 			fd->sc_type->rate = FDC_300KBPS;
1435 			break;
1436 		case 250 * 1024:
1437 			fd->sc_type->rate = FDC_250KBPS;
1438 			break;
1439 		default:
1440 			return EINVAL;
1441 		}
1442 
1443 		if (form_parms->nspt > FD_MAX_NSEC ||
1444 		    form_parms->fillbyte > 0xff ||
1445 		    form_parms->interleave > 0xff)
1446 			return EINVAL;
1447 		fd->sc_type->sectrac = form_parms->nspt;
1448 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
1449 			return EINVAL;
1450 		fd->sc_type->heads = form_parms->ntrk;
1451 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
1452 		fd->sc_type->secsize = ffs(scratch)-1;
1453 		fd->sc_type->gap2 = form_parms->gaplen;
1454 		fd->sc_type->cyls = form_parms->ncyl;
1455 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
1456 		    form_parms->nbps / DEV_BSIZE;
1457 		fd->sc_type->step = form_parms->stepspercyl;
1458 		fd->sc_type->fillbyte = form_parms->fillbyte;
1459 		fd->sc_type->interleave = form_parms->interleave;
1460 		return 0;
1461 
1462 	case FDIOCFORMAT_TRACK:
1463 		if((flag & FWRITE) == 0)
1464 			return EBADF;	/* must be opened for writing */
1465 		form_cmd = (struct fdformat_cmd *)addr;
1466 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
1467 			return EINVAL;	/* wrong version of formatting prog */
1468 
1469 		if (form_cmd->head >= fd->sc_type->heads ||
1470 		    form_cmd->cylinder >= fd->sc_type->cyls) {
1471 			return EINVAL;
1472 		}
1473 
1474 		fd_formb = malloc(sizeof(struct ne7_fd_formb),
1475 		    M_TEMP, M_NOWAIT);
1476 		if (fd_formb == 0)
1477 			return ENOMEM;
1478 
1479 		fd_formb->head = form_cmd->head;
1480 		fd_formb->cyl = form_cmd->cylinder;
1481 		fd_formb->transfer_rate = fd->sc_type->rate;
1482 		fd_formb->fd_formb_secshift = fd->sc_type->secsize;
1483 		fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
1484 		fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
1485 		fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
1486 
1487 		memset(il, 0, sizeof il);
1488 		for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
1489 			while (il[(j%fd_formb->fd_formb_nsecs)+1])
1490 				j++;
1491 			il[(j%fd_formb->fd_formb_nsecs)+1] = i;
1492 			j += fd->sc_type->interleave;
1493 		}
1494 		for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
1495 			fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
1496 			fd_formb->fd_formb_headno(i) = form_cmd->head;
1497 			fd_formb->fd_formb_secno(i) = il[i+1];
1498 			fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
1499 		}
1500 
1501 		error = fdformat(dev, fd_formb, p);
1502 		free(fd_formb, M_TEMP);
1503 		return error;
1504 
1505 	case FDIOCGETOPTS:		/* get drive options */
1506 		*(int *)addr = fd->sc_opts;
1507 		return 0;
1508 
1509 	case FDIOCSETOPTS:		/* set drive options */
1510 		fd->sc_opts = *(int *)addr;
1511 		return 0;
1512 
1513 	default:
1514 		return ENOTTY;
1515 	}
1516 
1517 #ifdef DIAGNOSTIC
1518 	panic("fdioctl: impossible");
1519 #endif
1520 }
1521 
1522 int
1523 fdformat(dev, finfo, p)
1524 	dev_t dev;
1525 	struct ne7_fd_formb *finfo;
1526 	struct proc *p;
1527 {
1528 	int rv = 0, s;
1529 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1530 	struct fd_type *type = fd->sc_type;
1531 	struct buf *bp;
1532 
1533 	/* set up a buffer header for fdstrategy() */
1534 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT|M_ZERO);
1535 	if(bp == 0)
1536 		return ENOBUFS;
1537 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1538 	bp->b_proc = p;
1539 	bp->b_dev = dev;
1540 
1541 	/*
1542 	 * calculate a fake blkno, so fdstrategy() would initiate a
1543 	 * seek to the requested cylinder
1544 	 */
1545 	bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1546 		       + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
1547 
1548 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1549 	bp->b_data = (caddr_t)finfo;
1550 
1551 #ifdef DEBUG
1552 	printf("fdformat: blkno %x count %lx\n", bp->b_blkno, bp->b_bcount);
1553 #endif
1554 
1555 	/* now do the format */
1556 	fdstrategy(bp);
1557 
1558 	/* ...and wait for it to complete */
1559 	s = splbio();
1560 	while (!(bp->b_flags & B_DONE)) {
1561 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1562 		if (rv == EWOULDBLOCK)
1563 			break;
1564 	}
1565 	splx(s);
1566 
1567 	if (rv == EWOULDBLOCK) {
1568 		/* timed out */
1569 		rv = EIO;
1570 		biodone(bp);
1571 	}
1572 	if(bp->b_flags & B_ERROR) {
1573 		rv = bp->b_error;
1574 	}
1575 	free(bp, M_TEMP);
1576 	return rv;
1577 }
1578 
1579 /*
1580  * Mountroot hook: prompt the user to enter the root file system
1581  * floppy.
1582  */
1583 void
1584 fd_mountroot_hook(dev)
1585 	struct device *dev;
1586 {
1587 	int c;
1588 
1589 	printf("Insert filesystem floppy and press return.");
1590 	cnpollc(1);
1591 	for (;;) {
1592 		c = cngetc();
1593 		if ((c == '\r') || (c == '\n')) {
1594 			printf("\n");
1595 			break;
1596 		}
1597 	}
1598 	cnpollc(0);
1599 }
1600