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