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