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