xref: /netbsd-src/sys/dev/isa/fd.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: fd.c,v 1.114 2019/11/10 21:16:35 chs 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.114 2019/11/10 21:16:35 chs 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);
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 			(void)config_found_ia(fdc->sc_dev, "fdcisa", (void *)&fa, fdprint);
465 #else
466 			/*
467 			 * Default to 1.44MB on Alpha and BeBox.  How do we tell
468 			 * on these platforms?
469 			 */
470 			fa.fa_deftype = &fd_types[0];
471 			(void)config_found_ia(fdc->sc_dev, "fdc", (void *)&fa, fdprint);
472 #endif
473 		}
474 	}
475 	fdc->sc_state = DEVIDLE;
476 }
477 
478 int
479 fdprobe(device_t parent, cfdata_t match, void *aux)
480 {
481 	struct fdc_softc *fdc = device_private(parent);
482 	cfdata_t cf = match;
483 	struct fdc_attach_args *fa = aux;
484 	int drive = fa->fa_drive;
485 	bus_space_tag_t iot = fdc->sc_iot;
486 	bus_space_handle_t ioh = fdc->sc_ioh;
487 	int n;
488 
489 	if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
490 	    cf->cf_loc[FDCCF_DRIVE] != drive)
491 		return 0;
492 	/*
493 	 * XXX
494 	 * This is to work around some odd interactions between this driver
495 	 * and SMC Ethernet cards.
496 	 */
497 	if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2)
498 		return 0;
499 
500 	/* Use PNP information if available */
501 	if (fdc->sc_known)
502 		return 1;
503 
504 	mutex_enter(&fdc->sc_mtx);
505 	/* toss any interrupt status */
506 	for (n = 0; n < 4; n++) {
507 		out_fdc(iot, ioh, NE7CMD_SENSEI);
508 		(void) fdcresult(fdc);
509 	}
510 	/* select drive and turn on motor */
511 	bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
512 	/* wait for motor to spin up */
513 	/* XXX check sc_probe */
514 	(void) cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, hz / 4);
515 	out_fdc(iot, ioh, NE7CMD_RECAL);
516 	out_fdc(iot, ioh, drive);
517 	/* wait for recalibrate, up to 2s */
518 	/* XXX check sc_probe */
519 	if (cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, 2 * hz) != EWOULDBLOCK){
520 #ifdef FD_DEBUG
521 		/* XXX */
522 		printf("fdprobe: got intr\n");
523 #endif
524 	}
525 	out_fdc(iot, ioh, NE7CMD_SENSEI);
526 	n = fdcresult(fdc);
527 #ifdef FD_DEBUG
528 	{
529 		int i;
530 		printf("fdprobe: status");
531 		for (i = 0; i < n; i++)
532 			printf(" %x", fdc->sc_status[i]);
533 		printf("\n");
534 	}
535 #endif
536 	/* turn off motor */
537 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
538 	mutex_exit(&fdc->sc_mtx);
539 
540 #if defined(bebox)	/* XXX What is this about? --thorpej@NetBSD.org */
541 	if (n != 2 || (fdc->sc_status[1] != 0))
542 		return 0;
543 #else
544 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
545 		return 0;
546 #endif /* bebox */
547 
548 	return 1;
549 }
550 
551 /*
552  * Controller is working, and drive responded.  Attach it.
553  */
554 void
555 fdattach(device_t parent, device_t self, void *aux)
556 {
557 	struct fdc_softc *fdc = device_private(parent);
558 	struct fd_softc *fd = device_private(self);
559 	struct fdc_attach_args *fa = aux;
560 	const struct fd_type *type = fa->fa_deftype;
561 	int drive = fa->fa_drive;
562 
563 	fd->sc_dev = self;
564 
565 	callout_init(&fd->sc_motoron_ch, 0);
566 	callout_init(&fd->sc_motoroff_ch, 0);
567 
568 	/* XXX Allow `flags' to override device type? */
569 
570 	if (type)
571 		aprint_normal(": %s, %d cyl, %d head, %d sec\n", type->name,
572 		    type->cyls, type->heads, type->sectrac);
573 	else
574 		aprint_normal(": density unknown\n");
575 
576 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
577 	fd->sc_cylin = -1;
578 	fd->sc_drive = drive;
579 	fd->sc_deftype = type;
580 	fdc->sc_fd[drive] = fd;
581 
582 	/*
583 	 * Initialize and attach the disk structure.
584 	 */
585 	disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
586 	disk_attach(&fd->sc_dk);
587 
588 	/*
589 	 * Establish a mountroot hook.
590 	 */
591 	fd->sc_roothook =
592 	    mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
593 
594 	rnd_attach_source(&fd->rnd_source, device_xname(fd->sc_dev),
595 			  RND_TYPE_DISK, RND_FLAG_DEFAULT);
596 
597 	fd_set_geometry(fd);
598 
599 	if (!pmf_device_register(self, NULL, NULL))
600 		aprint_error_dev(self, "cannot set power mgmt handler\n");
601 }
602 
603 static int
604 fddetach(device_t self, int flags)
605 {
606 	struct fd_softc *fd = device_private(self);
607 	int bmaj, cmaj, i, mn;
608 
609 	fd_motor_off(fd);
610 
611 	/* locate the major number */
612 	bmaj = bdevsw_lookup_major(&fd_bdevsw);
613 	cmaj = cdevsw_lookup_major(&fd_cdevsw);
614 
615 	/* Nuke the vnodes for any open instances. */
616 	for (i = 0; i < MAXPARTITIONS; i++) {
617 		mn = DISKMINOR(device_unit(self), i);
618 		vdevgone(bmaj, mn, mn, VBLK);
619 		vdevgone(cmaj, mn, mn, VCHR);
620 	}
621 
622 	pmf_device_deregister(self);
623 
624 #if 0 /* XXX need to undo at detach? */
625 	fd_set_geometry(fd);
626 #endif
627 
628 	rnd_detach_source(&fd->rnd_source);
629 
630 	disk_detach(&fd->sc_dk);
631 	disk_destroy(&fd->sc_dk);
632 
633 	/* Kill off any queued buffers. */
634 	bufq_drain(fd->sc_q);
635 
636 	bufq_free(fd->sc_q);
637 
638 	callout_destroy(&fd->sc_motoroff_ch);
639 	callout_destroy(&fd->sc_motoron_ch);
640 
641 	return 0;
642 }
643 
644 #if defined(i386) || defined(x86_64)
645 /*
646  * Translate nvram type into internal data structure.  Return NULL for
647  * none/unknown/unusable.
648  */
649 const struct fd_type *
650 fd_nvtotype(const char *fdc, int nvraminfo, int drive)
651 {
652 	int type;
653 
654 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
655 	switch (type) {
656 	case NVRAM_DISKETTE_NONE:
657 		return NULL;
658 	case NVRAM_DISKETTE_12M:
659 		return &fd_types[1];
660 	case NVRAM_DISKETTE_TYPE5:
661 	case NVRAM_DISKETTE_TYPE6:
662 		/* XXX We really ought to handle 2.88MB format. */
663 	case NVRAM_DISKETTE_144M:
664 #if NMCA > 0
665 		if (MCA_system)
666 			return &mca_fd_types[0];
667 		else
668 #endif /* NMCA > 0 */
669 			return &fd_types[0];
670 	case NVRAM_DISKETTE_360K:
671 		return &fd_types[3];
672 	case NVRAM_DISKETTE_720K:
673 #if NMCA > 0
674 		if (MCA_system)
675 			return &mca_fd_types[1];
676 		else
677 #endif /* NMCA > 0 */
678 			return &fd_types[4];
679 	default:
680 		printf("%s: drive %d: unknown device type 0x%x\n",
681 		    fdc, drive, type);
682 		return NULL;
683 	}
684 }
685 #endif /* i386 || x86_64 */
686 
687 static const struct fd_type *
688 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
689 {
690 	u_int type = FDTYPE(dev);
691 
692 	if (type > __arraycount(fd_types))
693 		return NULL;
694 	return type ? &fd_types[type - 1] : fd->sc_deftype;
695 }
696 
697 void
698 fdstrategy(struct buf *bp)
699 {
700 	struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev));
701 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
702 	int sz;
703 
704 	/* Valid unit, controller, and request? */
705 	if (bp->b_blkno < 0 ||
706 	    ((bp->b_bcount % FDC_BSIZE) != 0 &&
707 	     (bp->b_flags & B_FORMAT) == 0)) {
708 		bp->b_error = EINVAL;
709 		goto done;
710 	}
711 
712 	/* If it's a null transfer, return immediately. */
713 	if (bp->b_bcount == 0)
714 		goto done;
715 
716 	sz = howmany(bp->b_bcount, FDC_BSIZE);
717 
718 	if (bp->b_blkno + sz > fd->sc_type->size) {
719 		sz = fd->sc_type->size - bp->b_blkno;
720 		if (sz == 0) {
721 			/* If exactly at end of disk, return EOF. */
722 			goto done;
723 		}
724 		if (sz < 0) {
725 			/* If past end of disk, return EINVAL. */
726 			bp->b_error = EINVAL;
727 			goto done;
728 		}
729 		/* Otherwise, truncate request. */
730 		bp->b_bcount = sz << DEV_BSHIFT;
731 	}
732 
733 	bp->b_rawblkno = bp->b_blkno;
734  	bp->b_cylinder =
735 	    bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
736 
737 #ifdef FD_DEBUG
738 	printf("fdstrategy: b_blkno %llu b_bcount %d blkno %llu cylin %d "
739 	    "sz %d\n", (unsigned long long)bp->b_blkno, bp->b_bcount,
740 	    (unsigned long long)fd->sc_blkno, bp->b_cylinder, sz);
741 #endif
742 
743 	/* Queue transfer on drive, activate drive and controller if idle. */
744 	mutex_enter(&fdc->sc_mtx);
745 	bufq_put(fd->sc_q, bp);
746 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
747 	if (fd->sc_active == 0)
748 		fdstart(fd);
749 #ifdef DIAGNOSTIC
750 	else {
751 		if (fdc->sc_state == DEVIDLE) {
752 			printf("fdstrategy: controller inactive\n");
753 			fdcstart(fdc);
754 		}
755 	}
756 #endif
757 	mutex_exit(&fdc->sc_mtx);
758 	return;
759 
760 done:
761 	/* Toss transfer; we're done early. */
762 	bp->b_resid = bp->b_bcount;
763 	biodone(bp);
764 }
765 
766 void
767 fdstart(struct fd_softc *fd)
768 {
769 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
770 	int active = !TAILQ_EMPTY(&fdc->sc_drives);
771 
772 	KASSERT(mutex_owned(&fdc->sc_mtx));
773 	/* Link into controller queue. */
774 	fd->sc_active = 1;
775 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
776 
777 	/* If controller not already active, start it. */
778 	if (!active)
779 		fdcstart(fdc);
780 }
781 
782 void
783 fdfinish(struct fd_softc *fd, struct buf *bp)
784 {
785 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
786 
787 	/*
788 	 * Move this drive to the end of the queue to give others a `fair'
789 	 * chance.  We only force a switch if N operations are completed while
790 	 * another drive is waiting to be serviced, since there is a long motor
791 	 * startup delay whenever we switch.
792 	 */
793 	(void)bufq_get(fd->sc_q);
794 	if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
795 		fd->sc_ops = 0;
796 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
797 		if (bufq_peek(fd->sc_q) != NULL)
798 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
799 		else
800 			fd->sc_active = 0;
801 	}
802 	bp->b_resid = fd->sc_bcount;
803 	fd->sc_skip = 0;
804 
805 	rnd_add_uint32(&fd->rnd_source, bp->b_blkno);
806 
807 	biodone(bp);
808 	/* turn off motor 5s from now */
809 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
810 	fdc->sc_state = DEVIDLE;
811 }
812 
813 int
814 fdread(dev_t dev, struct uio *uio, int flags)
815 {
816 
817 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
818 }
819 
820 int
821 fdwrite(dev_t dev, struct uio *uio, int flags)
822 {
823 
824 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
825 }
826 
827 void
828 fd_set_motor(struct fdc_softc *fdc, int reset)
829 {
830 	struct fd_softc *fd;
831 	u_char status;
832 	int n;
833 
834 	if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
835 		status = fd->sc_drive;
836 	else
837 		status = 0;
838 	if (!reset)
839 		status |= FDO_FRST | FDO_FDMAEN;
840 	for (n = 0; n < 4; n++)
841 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
842 			status |= FDO_MOEN(n);
843 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
844 }
845 
846 void
847 fd_motor_off(void *arg)
848 {
849 	struct fd_softc *fd = arg;
850 	struct fdc_softc *fdc;
851 
852 	fdc = device_private(device_parent(fd->sc_dev));
853 
854 	mutex_enter(&fdc->sc_mtx);
855 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
856 	fd_set_motor(fdc, 0);
857 	mutex_exit(&fdc->sc_mtx);
858 }
859 
860 void
861 fd_motor_on(void *arg)
862 {
863 	struct fd_softc *fd = arg;
864 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
865 
866 	mutex_enter(&fdc->sc_mtx);
867 	fd->sc_flags &= ~FD_MOTOR_WAIT;
868 	if (TAILQ_FIRST(&fdc->sc_drives) == fd && fdc->sc_state == MOTORWAIT)
869 		(void)fdcintr1(fdc);
870 	mutex_exit(&fdc->sc_mtx);
871 }
872 
873 int
874 fdcresult(struct fdc_softc *fdc)
875 {
876 	bus_space_tag_t iot = fdc->sc_iot;
877 	bus_space_handle_t ioh = fdc->sc_ioh;
878 	u_char i;
879 	u_int j = 100000,
880 	      n = 0;
881 
882 	for (; j; j--) {
883 		i = bus_space_read_1(iot, ioh, fdsts) &
884 		    (NE7_DIO | NE7_RQM | NE7_CB);
885 		if (i == NE7_RQM)
886 			return n;
887 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
888 			if (n >= sizeof(fdc->sc_status)) {
889 				log(LOG_ERR, "fdcresult: overrun\n");
890 				return -1;
891 			}
892 			fdc->sc_status[n++] =
893 			    bus_space_read_1(iot, ioh, fddata);
894 		}
895 		delay(10);
896 	}
897 	log(LOG_ERR, "fdcresult: timeout\n");
898 	return -1;
899 }
900 
901 int
902 out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x)
903 {
904 	u_char i;
905 	u_int j = 100000;
906 
907 	for (; j; j--) {
908 		i = bus_space_read_1(iot, ioh, fdsts) &
909 		    (NE7_DIO | NE7_RQM);
910 		if (i == NE7_RQM) {
911 			bus_space_write_1(iot, ioh, fddata, x);
912 			return 0;
913 		}
914 		delay(10);
915 	}
916 	return -1;
917 }
918 
919 int
920 fdopen(dev_t dev, int flags, int mode, struct lwp *l)
921 {
922 	struct fd_softc *fd;
923 	const struct fd_type *type;
924 
925 	fd = device_lookup_private(&fd_cd, FDUNIT(dev));
926 	if (fd == NULL)
927 		return (ENXIO);
928 
929 	type = fd_dev_to_type(fd, dev);
930 	if (type == NULL)
931 		return ENXIO;
932 
933 	if ((fd->sc_flags & FD_OPEN) != 0 &&
934 	    memcmp(fd->sc_type, type, sizeof(*type)))
935 		return EBUSY;
936 
937 	fd->sc_type_copy = *type;
938 	fd->sc_type = &fd->sc_type_copy;
939 	fd->sc_cylin = -1;
940 	fd->sc_flags |= FD_OPEN;
941 
942 	fd_set_geometry(fd);
943 
944 	return 0;
945 }
946 
947 int
948 fdclose(dev_t dev, int flags, int mode, struct lwp *l)
949 {
950 	struct fd_softc *fd =
951 	    device_lookup_private(&fd_cd, FDUNIT(dev));
952 
953 	fd->sc_flags &= ~FD_OPEN;
954 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
955 	return 0;
956 }
957 
958 void
959 fdcstart(struct fdc_softc *fdc)
960 {
961 
962 	KASSERT(mutex_owned(&fdc->sc_mtx));
963 
964 	if (!device_is_active(fdc->sc_dev))
965 		return;
966 
967 #ifdef DIAGNOSTIC
968 	/* only got here if controller's drive queue was inactive; should
969 	   be in idle state */
970 	if (fdc->sc_state != DEVIDLE) {
971 		printf("fdcstart: not idle\n");
972 		return;
973 	}
974 #endif
975 	(void)fdcintr1(fdc);
976 }
977 
978 static void
979 fdcpstatus(int n, struct fdc_softc *fdc)
980 {
981 	char bits[64];
982 
983 	switch (n) {
984 	case 0:
985 		printf("\n");
986 		break;
987 	case 2:
988 		snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
989 		printf(" (st0 %s cyl %d)\n", bits, fdc->sc_status[1]);
990 		break;
991 	case 7:
992 		snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
993 		printf(" (st0 %s", bits);
994 		snprintb(bits, sizeof(bits), NE7_ST1BITS, fdc->sc_status[1]);
995 		printf(" st1 %s", bits);
996 		snprintb(bits, sizeof(bits), NE7_ST2BITS, fdc->sc_status[2]);
997 		printf(" st2 %s", bits);
998 		printf(" cyl %d head %d sec %d)\n",
999 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1000 		break;
1001 #ifdef DIAGNOSTIC
1002 	default:
1003 		printf("\nfdcstatus: weird size");
1004 		break;
1005 #endif
1006 	}
1007 }
1008 
1009 void
1010 fdcstatus(device_t dv, int n, const char *s)
1011 {
1012 	struct fdc_softc *fdc = device_private(device_parent(dv));
1013 
1014 	if (n == 0) {
1015 		out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
1016 		(void) fdcresult(fdc);
1017 		n = 2;
1018 	}
1019 	fdcpstatus(n, fdc);
1020 
1021 	aprint_normal_dev(dv, "%s", s);
1022 
1023 }
1024 
1025 void
1026 fdctimeout(void *arg)
1027 {
1028 	struct fdc_softc *fdc = arg;
1029 	struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
1030 
1031 	mutex_enter(&fdc->sc_mtx);
1032 #ifdef DEBUG
1033 	log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
1034 #endif
1035 	fdcstatus(fd->sc_dev, 0, "timeout");
1036 
1037 	if (bufq_peek(fd->sc_q) != NULL)
1038 		fdc->sc_state++;
1039 	else
1040 		fdc->sc_state = DEVIDLE;
1041 
1042 	(void)fdcintr1(fdc);
1043 	mutex_exit(&fdc->sc_mtx);
1044 }
1045 
1046 static int
1047 fdcintr1(struct fdc_softc *fdc)
1048 {
1049 #define	st0	fdc->sc_status[0]
1050 #define	cyl	fdc->sc_status[1]
1051 	struct fd_softc *fd;
1052 	struct buf *bp;
1053 	bus_space_tag_t iot = fdc->sc_iot;
1054 	bus_space_handle_t ioh = fdc->sc_ioh;
1055 	int read, head, sec, i, nblks;
1056 	struct fd_type *type;
1057 	struct ne7_fd_formb *finfo = NULL;
1058 
1059 	KASSERT(mutex_owned(&fdc->sc_mtx));
1060 	if (fdc->sc_state == PROBING) {
1061 #ifdef DEBUG
1062 		printf("fdcintr: got probe interrupt\n");
1063 #endif
1064 		fdc->sc_probe++;
1065 		goto out;
1066 	}
1067 
1068 loop:
1069 	/* Is there a drive for the controller to do a transfer with? */
1070 	fd = TAILQ_FIRST(&fdc->sc_drives);
1071 	if (fd == NULL) {
1072 		fdc->sc_state = DEVIDLE;
1073  		goto out;
1074 	}
1075 
1076 	/* Is there a transfer to this drive?  If not, deactivate drive. */
1077 	bp = bufq_peek(fd->sc_q);
1078 	if (bp == NULL) {
1079 		fd->sc_ops = 0;
1080 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1081 		fd->sc_active = 0;
1082 		goto loop;
1083 	}
1084 
1085 	if (bp->b_flags & B_FORMAT)
1086 		finfo = (struct ne7_fd_formb *)bp->b_data;
1087 
1088 	switch (fdc->sc_state) {
1089 	case DEVIDLE:
1090 		fdc->sc_errors = 0;
1091 		fd->sc_skip = 0;
1092 		fd->sc_bcount = bp->b_bcount;
1093 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
1094 		callout_stop(&fd->sc_motoroff_ch);
1095 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1096 			fdc->sc_state = MOTORWAIT;
1097 			return 1;
1098 		}
1099 		if ((fd->sc_flags & FD_MOTOR) == 0) {
1100 			/* Turn on the motor, being careful about pairing. */
1101 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1102 			if (ofd && ofd->sc_flags & FD_MOTOR) {
1103 				callout_stop(&ofd->sc_motoroff_ch);
1104 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1105 			}
1106 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1107 			fd_set_motor(fdc, 0);
1108 			fdc->sc_state = MOTORWAIT;
1109 			/* Allow .25s for motor to stabilize. */
1110 			callout_reset(&fd->sc_motoron_ch, hz / 4,
1111 			    fd_motor_on, fd);
1112 			return 1;
1113 		}
1114 		/* Make sure the right drive is selected. */
1115 		fd_set_motor(fdc, 0);
1116 
1117 		/* fall through */
1118 	case DOSEEK:
1119 	doseek:
1120 		if (fd->sc_cylin == bp->b_cylinder)
1121 			goto doio;
1122 
1123 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
1124 		out_fdc(iot, ioh, fd->sc_type->steprate);
1125 		out_fdc(iot, ioh, 6);		/* XXX head load time == 6ms */
1126 
1127 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
1128 		out_fdc(iot, ioh, fd->sc_drive); /* drive number */
1129 		out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
1130 
1131 		fd->sc_cylin = -1;
1132 		fdc->sc_state = SEEKWAIT;
1133 
1134 		iostat_seek(fd->sc_dk.dk_stats);
1135 		disk_busy(&fd->sc_dk);
1136 
1137 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1138 		return 1;
1139 
1140 	case DOIO:
1141 	doio:
1142 		type = fd->sc_type;
1143 		if (finfo)
1144 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1145 				      (char *)finfo;
1146 		sec = fd->sc_blkno % type->seccyl;
1147 		nblks = type->seccyl - sec;
1148 		nblks = uimin(nblks, fd->sc_bcount / FDC_BSIZE);
1149 		nblks = uimin(nblks, fdc->sc_maxiosize / FDC_BSIZE);
1150 		fd->sc_nblks = nblks;
1151 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
1152 		head = sec / type->sectrac;
1153 		sec -= head * type->sectrac;
1154 #ifdef DIAGNOSTIC
1155 		{
1156 			int block;
1157 			block = (fd->sc_cylin * type->heads + head)
1158 			    * type->sectrac + sec;
1159 			if (block != fd->sc_blkno) {
1160 				printf("fdcintr: block %d != blkno "
1161 				    "%" PRId64 "\n", block, fd->sc_blkno);
1162 #ifdef DDB
1163 				 Debugger();
1164 #endif
1165 			}
1166 		}
1167 #endif
1168 		read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
1169 		isa_dmastart(fdc->sc_ic, fdc->sc_drq,
1170 		    (char *)bp->b_data + fd->sc_skip, fd->sc_nbytes,
1171 		    NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT);
1172 		bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate);
1173 #ifdef FD_DEBUG
1174 		printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1175 			read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
1176 			head, sec, nblks);
1177 #endif
1178 		if (finfo) {
1179 			/* formatting */
1180 			if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
1181 				fdc->sc_errors = 4;
1182 				fdcretry(fdc);
1183 				goto loop;
1184 			}
1185 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1186 			out_fdc(iot, ioh, finfo->fd_formb_secshift);
1187 			out_fdc(iot, ioh, finfo->fd_formb_nsecs);
1188 			out_fdc(iot, ioh, finfo->fd_formb_gaplen);
1189 			out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
1190 		} else {
1191 			if (read)
1192 				out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
1193 			else
1194 				out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
1195 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1196 			out_fdc(iot, ioh, fd->sc_cylin); /* track */
1197 			out_fdc(iot, ioh, head);
1198 			out_fdc(iot, ioh, sec + 1);	 /* sector +1 */
1199 			out_fdc(iot, ioh, type->secsize);/* sector size */
1200 			out_fdc(iot, ioh, type->sectrac);/* sectors/track */
1201 			out_fdc(iot, ioh, type->gap1);	 /* gap1 size */
1202 			out_fdc(iot, ioh, type->datalen);/* data length */
1203 		}
1204 		fdc->sc_state = IOCOMPLETE;
1205 
1206 		disk_busy(&fd->sc_dk);
1207 
1208 		/* allow 2 seconds for operation */
1209 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
1210 		return 1;				/* will return later */
1211 
1212 	case SEEKWAIT:
1213 		callout_stop(&fdc->sc_timo_ch);
1214 		fdc->sc_state = SEEKCOMPLETE;
1215 		/* allow 1/50 second for heads to settle */
1216 		callout_reset(&fdc->sc_intr_ch, hz / 50, fdcintrcb, fdc);
1217 		return 1;
1218 
1219 	case SEEKCOMPLETE:
1220 		/* no data on seek */
1221 		disk_unbusy(&fd->sc_dk, 0, 0);
1222 
1223 		/* Make sure seek really happened. */
1224 		out_fdc(iot, ioh, NE7CMD_SENSEI);
1225 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
1226 		    cyl != bp->b_cylinder * fd->sc_type->step) {
1227 #ifdef FD_DEBUG
1228 			fdcstatus(fd->sc_dev, 2, "seek failed");
1229 #endif
1230 			fdcretry(fdc);
1231 			goto loop;
1232 		}
1233 		fd->sc_cylin = bp->b_cylinder;
1234 		goto doio;
1235 
1236 	case IOTIMEDOUT:
1237 		isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1238 		/* FALLTHROUGH */
1239 	case SEEKTIMEDOUT:
1240 	case RECALTIMEDOUT:
1241 	case RESETTIMEDOUT:
1242 		fdcretry(fdc);
1243 		goto loop;
1244 
1245 	case IOCOMPLETE: /* IO DONE, post-analyze */
1246 		callout_stop(&fdc->sc_timo_ch);
1247 
1248 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1249 		    (bp->b_flags & B_READ));
1250 
1251 		if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
1252 			isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1253 #ifdef FD_DEBUG
1254 			fdcstatus(fd->sc_dev, 7, bp->b_flags & B_READ ?
1255 			    "read failed" : "write failed");
1256 			printf("blkno %llu nblks %d\n",
1257 			    (unsigned long long)fd->sc_blkno, fd->sc_nblks);
1258 #endif
1259 			fdcretry(fdc);
1260 			goto loop;
1261 		}
1262 		isa_dmadone(fdc->sc_ic, fdc->sc_drq);
1263 		if (fdc->sc_errors) {
1264 			diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
1265 			    fd->sc_skip / FDC_BSIZE, NULL);
1266 			printf("\n");
1267 			fdc->sc_errors = 0;
1268 		}
1269 		fd->sc_blkno += fd->sc_nblks;
1270 		fd->sc_skip += fd->sc_nbytes;
1271 		fd->sc_bcount -= fd->sc_nbytes;
1272 		if (!finfo && fd->sc_bcount > 0) {
1273 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1274 			goto doseek;
1275 		}
1276 		fdfinish(fd, bp);
1277 		goto loop;
1278 
1279 	case DORESET:
1280 		/* try a reset, keep motor on */
1281 		fd_set_motor(fdc, 1);
1282 		delay(100);
1283 		fd_set_motor(fdc, 0);
1284 		fdc->sc_state = RESETCOMPLETE;
1285 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1286 		return 1;			/* will return later */
1287 
1288 	case RESETCOMPLETE:
1289 		callout_stop(&fdc->sc_timo_ch);
1290 		/* clear the controller output buffer */
1291 		for (i = 0; i < 4; i++) {
1292 			out_fdc(iot, ioh, NE7CMD_SENSEI);
1293 			(void) fdcresult(fdc);
1294 		}
1295 
1296 		/* fall through */
1297 	case DORECAL:
1298 		out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
1299 		out_fdc(iot, ioh, fd->sc_drive);
1300 		fdc->sc_state = RECALWAIT;
1301 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1302 		return 1;			/* will return later */
1303 
1304 	case RECALWAIT:
1305 		callout_stop(&fdc->sc_timo_ch);
1306 		fdc->sc_state = RECALCOMPLETE;
1307 		/* allow 1/30 second for heads to settle */
1308 		callout_reset(&fdc->sc_intr_ch, hz / 30, fdcintrcb, fdc);
1309 		return 1;			/* will return later */
1310 
1311 	case RECALCOMPLETE:
1312 		out_fdc(iot, ioh, NE7CMD_SENSEI);
1313 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1314 #ifdef FD_DEBUG
1315 			fdcstatus(fd->sc_dev, 2, "recalibrate failed");
1316 #endif
1317 			fdcretry(fdc);
1318 			goto loop;
1319 		}
1320 		fd->sc_cylin = 0;
1321 		goto doseek;
1322 
1323 	case MOTORWAIT:
1324 		if (fd->sc_flags & FD_MOTOR_WAIT)
1325 			return 1;		/* time's not up yet */
1326 		goto doseek;
1327 
1328 	default:
1329 		fdcstatus(fd->sc_dev, 0, "stray interrupt");
1330 		return 1;
1331 	}
1332 #undef	st0
1333 #undef	cyl
1334 
1335 out:
1336 	cv_signal(&fdc->sc_cv);
1337 	return 1;
1338 }
1339 
1340 static void
1341 fdcintrcb(void *arg)
1342 {
1343 	(void)fdcintr(arg);
1344 }
1345 
1346 int
1347 fdcintr(void *arg)
1348 {
1349 	int rc;
1350 	struct fdc_softc *fdc = arg;
1351 
1352 	mutex_enter(&fdc->sc_mtx);
1353 	rc = fdcintr1(fdc);
1354 	mutex_exit(&fdc->sc_mtx);
1355 	return rc;
1356 }
1357 
1358 void
1359 fdcretry(struct fdc_softc *fdc)
1360 {
1361 	struct fd_softc *fd;
1362 	struct buf *bp;
1363 
1364 	fd = TAILQ_FIRST(&fdc->sc_drives);
1365 	bp = bufq_peek(fd->sc_q);
1366 
1367 	if (fd->sc_opts & FDOPT_NORETRY)
1368 	    goto fail;
1369 	switch (fdc->sc_errors) {
1370 	case 0:
1371 		/* try again */
1372 		fdc->sc_state = DOSEEK;
1373 		break;
1374 
1375 	case 1: case 2: case 3:
1376 		/* didn't work; try recalibrating */
1377 		fdc->sc_state = DORECAL;
1378 		break;
1379 
1380 	case 4:
1381 		/* still no go; reset the bastard */
1382 		fdc->sc_state = DORESET;
1383 		break;
1384 
1385 	default:
1386 	fail:
1387 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1388 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
1389 				fd->sc_skip / FDC_BSIZE, NULL);
1390 			fdcpstatus(7, fdc);
1391 		}
1392 
1393 		bp->b_error = EIO;
1394 		fdfinish(fd, bp);
1395 	}
1396 	fdc->sc_errors++;
1397 }
1398 
1399 int
1400 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1401 {
1402 	struct fd_softc *fd =
1403 	    device_lookup_private(&fd_cd, FDUNIT(dev));
1404 	struct fdformat_parms *form_parms;
1405 	struct fdformat_cmd *form_cmd;
1406 	struct ne7_fd_formb *fd_formb;
1407 	struct disklabel *lp = fd->sc_dk.dk_label;
1408 	int error;
1409 	unsigned int scratch;
1410 	int il[FD_MAX_NSEC + 1];
1411 	int i, j;
1412 #ifdef __HAVE_OLD_DISKLABEL
1413 	struct disklabel newlabel;
1414 #endif
1415 
1416 	switch (cmd) {
1417 	case DIOCGPARTINFO:
1418 	case DIOCGDINFO:
1419 #ifdef __HAVE_OLD_DISKLABEL
1420 	case ODIOCGDINFO:
1421 #endif
1422 		memset(lp, 0, sizeof(*lp));
1423 
1424 		lp->d_type = DKTYPE_FLOPPY;
1425 		lp->d_secsize = FDC_BSIZE;
1426 		lp->d_nsectors = fd->sc_type->sectrac;
1427 		lp->d_ntracks = fd->sc_type->heads;
1428 		lp->d_ncylinders = fd->sc_type->cyls;
1429 		lp->d_secpercyl = fd->sc_type->seccyl;
1430 		lp->d_secperunit = fd->sc_type->size;
1431 
1432 		if (readdisklabel(dev, fdstrategy, lp, NULL) != NULL)
1433 			return EINVAL;
1434 		break;
1435 	}
1436 
1437 	error = disk_ioctl(&fd->sc_dk, dev, cmd, addr, flag, l);
1438 	if (error != EPASSTHROUGH)
1439 		return error;
1440 
1441 	switch (cmd) {
1442 	case DIOCWLABEL:
1443 		if ((flag & FWRITE) == 0)
1444 			return EBADF;
1445 		/* XXX do something */
1446 		return 0;
1447 
1448 	case DIOCWDINFO:
1449 #ifdef __HAVE_OLD_DISKLABEL
1450 	case ODIOCWDINFO:
1451 #endif
1452 	{
1453 		if ((flag & FWRITE) == 0)
1454 			return EBADF;
1455 #ifdef __HAVE_OLD_DISKLABEL
1456 		if (cmd == ODIOCWDINFO) {
1457 			memset(&newlabel, 0, sizeof newlabel);
1458 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1459 			addr = &newlabel;
1460 		}
1461 #endif
1462 		error = setdisklabel(lp, addr, 0, NULL);
1463 		if (error)
1464 			return error;
1465 
1466 		error = writedisklabel(dev, fdstrategy, lp, NULL);
1467 		return error;
1468 	}
1469 
1470 	case FDIOCGETFORMAT:
1471 		form_parms = (struct fdformat_parms *)addr;
1472 		form_parms->fdformat_version = FDFORMAT_VERSION;
1473 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1474 		form_parms->ncyl = fd->sc_type->cyls;
1475 		form_parms->nspt = fd->sc_type->sectrac;
1476 		form_parms->ntrk = fd->sc_type->heads;
1477 		form_parms->stepspercyl = fd->sc_type->step;
1478 		form_parms->gaplen = fd->sc_type->gap2;
1479 		form_parms->fillbyte = fd->sc_type->fillbyte;
1480 		form_parms->interleave = fd->sc_type->interleave;
1481 		switch (fd->sc_type->rate) {
1482 		case FDC_500KBPS:
1483 			form_parms->xfer_rate = 500 * 1024;
1484 			break;
1485 		case FDC_300KBPS:
1486 			form_parms->xfer_rate = 300 * 1024;
1487 			break;
1488 		case FDC_250KBPS:
1489 			form_parms->xfer_rate = 250 * 1024;
1490 			break;
1491 		default:
1492 			return EINVAL;
1493 		}
1494 		return 0;
1495 
1496 	case FDIOCSETFORMAT:
1497 		if((flag & FWRITE) == 0)
1498 			return EBADF;	/* must be opened for writing */
1499 		form_parms = (struct fdformat_parms *)addr;
1500 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
1501 			return EINVAL;	/* wrong version of formatting prog */
1502 
1503 		scratch = form_parms->nbps >> 7;
1504 		if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
1505 		    scratch & ~(1 << (ffs(scratch)-1)))
1506 			/* not a power-of-two multiple of 128 */
1507 			return EINVAL;
1508 
1509 		switch (form_parms->xfer_rate) {
1510 		case 500 * 1024:
1511 			fd->sc_type->rate = FDC_500KBPS;
1512 			break;
1513 		case 300 * 1024:
1514 			fd->sc_type->rate = FDC_300KBPS;
1515 			break;
1516 		case 250 * 1024:
1517 			fd->sc_type->rate = FDC_250KBPS;
1518 			break;
1519 		default:
1520 			return EINVAL;
1521 		}
1522 
1523 		if (form_parms->nspt > FD_MAX_NSEC ||
1524 		    form_parms->fillbyte > 0xff ||
1525 		    form_parms->interleave > 0xff)
1526 			return EINVAL;
1527 		fd->sc_type->sectrac = form_parms->nspt;
1528 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
1529 			return EINVAL;
1530 		fd->sc_type->heads = form_parms->ntrk;
1531 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
1532 		fd->sc_type->secsize = ffs(scratch)-1;
1533 		fd->sc_type->gap2 = form_parms->gaplen;
1534 		fd->sc_type->cyls = form_parms->ncyl;
1535 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
1536 		    form_parms->nbps / DEV_BSIZE;
1537 		fd->sc_type->step = form_parms->stepspercyl;
1538 		fd->sc_type->fillbyte = form_parms->fillbyte;
1539 		fd->sc_type->interleave = form_parms->interleave;
1540 		return 0;
1541 
1542 	case FDIOCFORMAT_TRACK:
1543 		if((flag & FWRITE) == 0)
1544 			return EBADF;	/* must be opened for writing */
1545 		form_cmd = (struct fdformat_cmd *)addr;
1546 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
1547 			return EINVAL;	/* wrong version of formatting prog */
1548 
1549 		if (form_cmd->head >= fd->sc_type->heads ||
1550 		    form_cmd->cylinder >= fd->sc_type->cyls) {
1551 			return EINVAL;
1552 		}
1553 
1554 		fd_formb = malloc(sizeof(struct ne7_fd_formb),
1555 		    M_TEMP, M_WAITOK);
1556 		fd_formb->head = form_cmd->head;
1557 		fd_formb->cyl = form_cmd->cylinder;
1558 		fd_formb->transfer_rate = fd->sc_type->rate;
1559 		fd_formb->fd_formb_secshift = fd->sc_type->secsize;
1560 		fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
1561 		fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
1562 		fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
1563 
1564 		memset(il, 0, sizeof il);
1565 		for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
1566 			while (il[(j%fd_formb->fd_formb_nsecs)+1])
1567 				j++;
1568 			il[(j%fd_formb->fd_formb_nsecs)+1] = i;
1569 			j += fd->sc_type->interleave;
1570 		}
1571 		for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
1572 			fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
1573 			fd_formb->fd_formb_headno(i) = form_cmd->head;
1574 			fd_formb->fd_formb_secno(i) = il[i+1];
1575 			fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
1576 		}
1577 
1578 		error = fdformat(dev, fd_formb, l);
1579 		free(fd_formb, M_TEMP);
1580 		return error;
1581 
1582 	case FDIOCGETOPTS:		/* get drive options */
1583 		*(int *)addr = fd->sc_opts;
1584 		return 0;
1585 
1586 	case FDIOCSETOPTS:		/* set drive options */
1587 		fd->sc_opts = *(int *)addr;
1588 		return 0;
1589 
1590 	default:
1591 		return ENOTTY;
1592 	}
1593 
1594 #ifdef DIAGNOSTIC
1595 	panic("fdioctl: impossible");
1596 #endif
1597 }
1598 
1599 int
1600 fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct lwp *l)
1601 {
1602 	int rv = 0;
1603 	struct fd_softc *fd =
1604 	    device_lookup_private(&fd_cd, FDUNIT(dev));
1605 	struct fd_type *type = fd->sc_type;
1606 	struct buf *bp;
1607 
1608 	/* set up a buffer header for fdstrategy() */
1609 	bp = getiobuf(NULL, false);
1610 	if (bp == NULL)
1611 		return ENOBUFS;
1612 
1613 	bp->b_cflags = BC_BUSY;
1614 	bp->b_flags = B_PHYS | B_FORMAT;
1615 	bp->b_proc = l->l_proc;
1616 	bp->b_dev = dev;
1617 
1618 	/*
1619 	 * calculate a fake blkno, so fdstrategy() would initiate a
1620 	 * seek to the requested cylinder
1621 	 */
1622 	bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1623 		       + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
1624 
1625 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1626 	bp->b_data = (void *)finfo;
1627 
1628 #ifdef FD_DEBUG
1629 	printf("fdformat: blkno %" PRIx64 " count %x\n",
1630 	    bp->b_blkno, bp->b_bcount);
1631 #endif
1632 
1633 	/* now do the format */
1634 	fdstrategy(bp);
1635 
1636 	/* ...and wait for it to complete */
1637 	rv = biowait(bp);
1638 	putiobuf(bp);
1639 	return rv;
1640 }
1641 
1642 /*
1643  * Mountroot hook: prompt the user to enter the root file system
1644  * floppy.
1645  */
1646 void
1647 fd_mountroot_hook(device_t dev)
1648 {
1649 	int c;
1650 
1651 	printf("Insert filesystem floppy and press return.");
1652 	cnpollc(1);
1653 	for (;;) {
1654 		c = cngetc();
1655 		if ((c == '\r') || (c == '\n')) {
1656 			printf("\n");
1657 			break;
1658 		}
1659 	}
1660 	cnpollc(0);
1661 }
1662 
1663 static void
1664 fd_set_geometry(struct fd_softc *fd)
1665 {
1666 	const struct fd_type *fdt;
1667 
1668 	fdt = fd->sc_type;
1669 	if (fdt == NULL) {
1670 		fdt = fd->sc_deftype;
1671 		if (fdt == NULL)
1672 			return;
1673 	}
1674 
1675 	struct disk_geom *dg = &fd->sc_dk.dk_geom;
1676 
1677 	memset(dg, 0, sizeof(*dg));
1678 	dg->dg_secperunit = fdt->size;
1679 	dg->dg_nsectors = fdt->sectrac;
1680 	switch (fdt->secsize) {
1681 	case 2:
1682 		dg->dg_secsize = 512;
1683 		break;
1684 	case 3:
1685 		dg->dg_secsize = 1024;
1686 		break;
1687 	default:
1688 		break;
1689 	}
1690 	dg->dg_ntracks = fdt->heads;
1691 	dg->dg_ncylinders = fdt->cyls;
1692 	disk_set_info(fd->sc_dev, &fd->sc_dk, NULL);
1693 }
1694