xref: /freebsd/sys/dev/fdc/fdc.c (revision 3fc95726c4065a4b38d3fffa570f94db07faba91)
1 /*-
2  * Copyright (c) 2004 Poul-Henning Kamp
3  * Copyright (c) 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Don Ahn.
8  *
9  * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
10  * aided by the Linux floppy driver modifications from David Bateman
11  * (dbateman@eng.uts.edu.au).
12  *
13  * Copyright (c) 1993, 1994 by
14  *  jc@irbs.UUCP (John Capo)
15  *  vak@zebub.msk.su (Serge Vakulenko)
16  *  ache@astral.msk.su (Andrew A. Chernov)
17  *
18  * Copyright (c) 1993, 1994, 1995 by
19  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
20  *  dufault@hda.com (Peter Dufault)
21  *
22  * Copyright (c) 2001 Joerg Wunsch,
23  *  joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the above copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  *    notice, this list of conditions and the following disclaimer in the
32  *    documentation and/or other materials provided with the distribution.
33  * 4. Neither the name of the University nor the names of its contributors
34  *    may be used to endorse or promote products derived from this software
35  *    without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
50  *
51  */
52 
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55 
56 #include "opt_fdc.h"
57 
58 #include <sys/param.h>
59 #include <sys/bio.h>
60 #include <sys/bus.h>
61 #include <sys/devicestat.h>
62 #include <sys/disk.h>
63 #include <sys/fcntl.h>
64 #include <sys/fdcio.h>
65 #include <sys/filio.h>
66 #include <sys/kernel.h>
67 #include <sys/kthread.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/module.h>
71 #include <sys/mutex.h>
72 #include <sys/proc.h>
73 #include <sys/rman.h>
74 #include <sys/sysctl.h>
75 #include <sys/systm.h>
76 
77 #include <geom/geom.h>
78 
79 #include <machine/bus.h>
80 #include <machine/clock.h>
81 #include <machine/stdarg.h>
82 
83 #include <isa/isavar.h>
84 #include <isa/isareg.h>
85 #include <dev/fdc/fdcvar.h>
86 #include <isa/rtc.h>
87 
88 #include <dev/ic/nec765.h>
89 
90 /*
91  * Runtime configuration hints/flags
92  */
93 
94 /* configuration flags for fd */
95 #define FD_TYPEMASK	0x0f	/* drive type, matches enum
96 				 * fd_drivetype; on i386 machines, if
97 				 * given as 0, use RTC type for fd0
98 				 * and fd1 */
99 #define FD_NO_PROBE	0x20	/* don't probe drive (seek test), just
100 				 * assume it is there */
101 
102 /*
103  * Things that could conceiveably considered parameters or tweakables
104  */
105 
106 /*
107  * Maximal number of bytes in a cylinder.
108  * This is used for ISADMA bouncebuffer allocation and sets the max
109  * xfersize we support.
110  *
111  * 2.88M format has 2 x 36 x 512, allow for hacked up density.
112  */
113 
114 #define MAX_BYTES_PER_CYL	(2 * 40 * 512)
115 
116 /*
117  * Timeout value for the PIO loops to wait until the FDC main status
118  * register matches our expectations (request for master, direction
119  * bit).  This is supposed to be a number of microseconds, although
120  * timing might actually not be very accurate.
121  *
122  * Timeouts of 100 msec are believed to be required for some broken
123  * (old) hardware.
124  */
125 #define	FDSTS_TIMEOUT	100000
126 
127 /*
128  * After this many errors, stop whining.  Close will reset this count.
129  */
130 #define FDC_ERRMAX	100
131 
132 /*
133  * AutoDensity search lists for each drive type.
134  */
135 
136 static struct fd_type fd_searchlist_360k[] = {
137 	{ FDF_5_360 },
138 	{ 0 }
139 };
140 
141 static struct fd_type fd_searchlist_12m[] = {
142 	{ FDF_5_1200 | FL_AUTO },
143 	{ FDF_5_360 | FL_2STEP | FL_AUTO},
144 	{ 0 }
145 };
146 
147 static struct fd_type fd_searchlist_720k[] = {
148 	{ FDF_3_720 },
149 	{ 0 }
150 };
151 
152 static struct fd_type fd_searchlist_144m[] = {
153 	{ FDF_3_1440 | FL_AUTO},
154 	{ FDF_3_720 | FL_AUTO},
155 	{ 0 }
156 };
157 
158 static struct fd_type fd_searchlist_288m[] = {
159 	{ FDF_3_1440 | FL_AUTO },
160 #if 0
161 	{ FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
162 #endif
163 	{ FDF_3_720 | FL_AUTO},
164 	{ 0 }
165 };
166 
167 /*
168  * Order must match enum fd_drivetype in <sys/fdcio.h>.
169  */
170 static struct fd_type *fd_native_types[] = {
171 	NULL,				/* FDT_NONE */
172 	fd_searchlist_360k, 		/* FDT_360K */
173 	fd_searchlist_12m, 		/* FDT_12M */
174 	fd_searchlist_720k, 		/* FDT_720K */
175 	fd_searchlist_144m, 		/* FDT_144M */
176 	fd_searchlist_288m, 		/* FDT_288M */
177 };
178 
179 /*
180  * Internals start here
181  */
182 
183 /* registers */
184 #define	FDOUT	2	/* Digital Output Register (W) */
185 #define	FDO_FDSEL	0x03	/*  floppy device select */
186 #define	FDO_FRST	0x04	/*  floppy controller reset */
187 #define	FDO_FDMAEN	0x08	/*  enable floppy DMA and Interrupt */
188 #define	FDO_MOEN0	0x10	/*  motor enable drive 0 */
189 #define	FDO_MOEN1	0x20	/*  motor enable drive 1 */
190 #define	FDO_MOEN2	0x40	/*  motor enable drive 2 */
191 #define	FDO_MOEN3	0x80	/*  motor enable drive 3 */
192 
193 #define	FDSTS	4	/* NEC 765 Main Status Register (R) */
194 #define	FDDATA	5	/* NEC 765 Data Register (R/W) */
195 #define	FDCTL	7	/* Control Register (W) */
196 
197 /*
198  * The YE-DATA PC Card floppies use PIO to read in the data rather than
199  * DMA due to the wild variability of DMA for the PC Card devices.  In
200  * addition, if we cannot setup the DMA resources for the ISA attachment,
201  * we'll use this same offset for data transfer.
202  *
203  * For this mode, offset 0 and 1 must be used to setup the transfer
204  * for this floppy.  This means they are only available on those systems
205  * that map them to the floppy drive.  Newer systems do not do this, and
206  * we should likely prohibit access to them (or disallow NODMA to be set).
207  */
208 #define FDBCDR		0	/* And 1 */
209 #define FD_YE_DATAPORT	6	/* Drive Data port */
210 
211 #define	FDI_DCHG	0x80	/* diskette has been changed */
212 				/* requires drive and motor being selected */
213 				/* is cleared by any step pulse to drive */
214 
215 /*
216  * We have three private BIO commands.
217  */
218 #define BIO_PROBE	BIO_CMD0
219 #define BIO_RDID	BIO_CMD1
220 #define BIO_FMT		BIO_CMD2
221 
222 /*
223  * Per drive structure (softc).
224  */
225 struct fd_data {
226 	u_char 	*fd_ioptr;	/* IO pointer */
227 	u_int	fd_iosize;	/* Size of IO chunks */
228 	u_int	fd_iocount;	/* Outstanding requests */
229 	struct	fdc_data *fdc;	/* pointer to controller structure */
230 	int	fdsu;		/* this units number on this controller */
231 	enum	fd_drivetype type; /* drive type */
232 	struct	fd_type *ft;	/* pointer to current type descriptor */
233 	struct	fd_type fts;	/* type descriptors */
234 	int	sectorsize;
235 	int	flags;
236 #define	FD_WP		(1<<0)	/* Write protected	*/
237 #define	FD_MOTOR	(1<<1)	/* motor should be on	*/
238 #define	FD_MOTORWAIT	(1<<2)	/* motor should be on	*/
239 #define	FD_EMPTY	(1<<3)	/* no media		*/
240 #define	FD_NEWDISK	(1<<4)	/* media changed	*/
241 #define	FD_ISADMA	(1<<5)	/* isa dma started 	*/
242 	int	track;		/* where we think the head is */
243 #define FD_NO_TRACK	 -2
244 	int	options;	/* FDOPT_* */
245 	struct	callout toffhandle;
246 	struct	callout tohandle;
247 	struct g_geom *fd_geom;
248 	struct g_provider *fd_provider;
249 	device_t dev;
250 	struct bio_queue_head fd_bq;
251 };
252 
253 #define FD_NOT_VALID -2
254 
255 static driver_intr_t fdc_intr;
256 static void fdc_reset(struct fdc_data *);
257 
258 SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
259 
260 static int fifo_threshold = 8;
261 SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
262 	"FIFO threshold setting");
263 
264 static int debugflags = 0;
265 SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
266 	"Debug flags");
267 
268 static int retries = 10;
269 SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
270 	"Number of retries to attempt");
271 
272 static int spec1 = 0xaf;
273 SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
274 	"Specification byte one (step-rate + head unload)");
275 
276 static int spec2 = 0x10;
277 SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
278 	"Specification byte two (head load time + no-dma)");
279 
280 static int settle;
281 SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
282 	"Head settling time in sec/hz");
283 
284 static void
285 fdprinttype(struct fd_type *ft)
286 {
287 
288 	printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
289 	    ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
290 	    ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
291 	    ft->offset_side2, ft->flags);
292 }
293 
294 static void
295 fdsettype(struct fd_data *fd, struct fd_type *ft)
296 {
297 	fd->ft = ft;
298 	ft->size = ft->sectrac * ft->heads * ft->tracks;
299 	fd->sectorsize = 128 << fd->ft->secsize;
300 }
301 
302 /*
303  * Bus space handling (access to low-level IO).
304  */
305 static void
306 fdctl_wr(struct fdc_data *fdc, u_int8_t v)
307 {
308 
309 	bus_space_write_1(fdc->ctlt, fdc->ctlh, fdc->ctl_off, v);
310 }
311 
312 static void
313 fdout_wr(struct fdc_data *fdc, u_int8_t v)
314 {
315 
316 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
317 }
318 
319 static u_int8_t
320 fdsts_rd(struct fdc_data *fdc)
321 {
322 
323 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
324 }
325 
326 static void
327 fddata_wr(struct fdc_data *fdc, u_int8_t v)
328 {
329 
330 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
331 }
332 
333 static u_int8_t
334 fddata_rd(struct fdc_data *fdc)
335 {
336 
337 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
338 }
339 
340 static u_int8_t
341 fdin_rd(struct fdc_data *fdc)
342 {
343 
344 	return bus_space_read_1(fdc->ctlt, fdc->ctlh, fdc->ctl_off);
345 }
346 
347 /*
348  * Magic pseudo-DMA initialization for YE FDC. Sets count and
349  * direction.
350  */
351 static void
352 fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
353 {
354 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + FDBCDR,
355 	    (count - 1) & 0xff);
356 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + FDBCDR + 1,
357 	  ((iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f)));
358 }
359 
360 static int
361 fdc_err(struct fdc_data *fdc, const char *s)
362 {
363 	fdc->fdc_errs++;
364 	if (s) {
365 		if (fdc->fdc_errs < FDC_ERRMAX)
366 			device_printf(fdc->fdc_dev, "%s", s);
367 		else if (fdc->fdc_errs == FDC_ERRMAX)
368 			device_printf(fdc->fdc_dev, "too many errors, not "
369 						    "logging any more\n");
370 	}
371 
372 	return (1);
373 }
374 
375 /*
376  * FDC IO functions, take care of the main status register, timeout
377  * in case the desired status bits are never set.
378  *
379  * These PIO loops initially start out with short delays between
380  * each iteration in the expectation that the required condition
381  * is usually met quickly, so it can be handled immediately.
382  */
383 static int
384 fdc_in(struct fdc_data *fdc, int *ptr)
385 {
386 	int i, j, step;
387 
388 	step = 1;
389 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
390 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
391 	        if (i == (NE7_DIO|NE7_RQM)) {
392 			i = fddata_rd(fdc);
393 			if (ptr)
394 				*ptr = i;
395 			return (0);
396 		}
397 		if (i == NE7_RQM)
398 			return (fdc_err(fdc, "ready for output in input\n"));
399 		step += step;
400 		DELAY(step);
401 	}
402 	return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
403 }
404 
405 static int
406 fdc_out(struct fdc_data *fdc, int x)
407 {
408 	int i, j, step;
409 
410 	step = 1;
411 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
412 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
413 	        if (i == NE7_RQM) {
414 			fddata_wr(fdc, x);
415 			return (0);
416 		}
417 		if (i == (NE7_DIO|NE7_RQM))
418 			return (fdc_err(fdc, "ready for input in output\n"));
419 		step += step;
420 		DELAY(step);
421 	}
422 	return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
423 }
424 
425 /*
426  * fdc_cmd: Send a command to the chip.
427  * Takes a varargs with this structure:
428  *	# of output bytes
429  *	output bytes as int [...]
430  *	# of input bytes
431  *	input bytes as int* [...]
432  */
433 static int
434 fdc_cmd(struct fdc_data *fdc, int n_out, ...)
435 {
436 	u_char cmd = 0;
437 	int n_in;
438 	int n, i;
439 	va_list ap;
440 
441 	va_start(ap, n_out);
442 	for (n = 0; n < n_out; n++) {
443 		i = va_arg(ap, int);
444 		if (n == 0)
445 			cmd = i;
446 		if (fdc_out(fdc, i) < 0) {
447 			char msg[50];
448 			snprintf(msg, sizeof(msg),
449 				"cmd %x failed at out byte %d of %d\n",
450 				cmd, n + 1, n_out);
451 			fdc->flags |= FDC_NEEDS_RESET;
452 			return fdc_err(fdc, msg);
453 		}
454 	}
455 	n_in = va_arg(ap, int);
456 	for (n = 0; n < n_in; n++) {
457 		int *ptr = va_arg(ap, int *);
458 		if (fdc_in(fdc, ptr) < 0) {
459 			char msg[50];
460 			snprintf(msg, sizeof(msg),
461 				"cmd %02x failed at in byte %d of %d\n",
462 				cmd, n + 1, n_in);
463 			fdc->flags |= FDC_NEEDS_RESET;
464 			return fdc_err(fdc, msg);
465 		}
466 	}
467 	return (0);
468 }
469 
470 static void
471 fdc_reset(struct fdc_data *fdc)
472 {
473 	int i, r[10];
474 
475 	/* Try a reset, keep motor on */
476 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
477 	DELAY(100);
478 	/* enable FDC, but defer interrupts a moment */
479 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
480 	DELAY(100);
481 	fdout_wr(fdc, fdc->fdout);
482 
483 	/* XXX after a reset, silently believe the FDC will accept commands */
484 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
485 		device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
486 
487 	if (fdc->fdct == FDC_ENHANCED) {
488 		if (fdc_cmd(fdc, 4,
489 		    I8207X_CONFIGURE,
490 		    0,
491 		    0x40 |			/* Enable Implied Seek */
492 		    0x10 |			/* Polling disabled */
493 		    (fifo_threshold - 1),	/* Fifo threshold */
494 		    0x00,			/* Precomp track */
495 		    0))
496 			device_printf(fdc->fdc_dev,
497 			    " CONFIGURE failed in reset\n");
498 		if (debugflags & 1) {
499 			if (fdc_cmd(fdc, 1,
500 			    0x0e,			/* DUMPREG */
501 			    10, &r[0], &r[1], &r[2], &r[3], &r[4],
502 			    &r[5], &r[6], &r[7], &r[8], &r[9]))
503 				device_printf(fdc->fdc_dev,
504 				    " DUMPREG failed in reset\n");
505 			for (i = 0; i < 10; i++)
506 				printf(" %02x", r[i]);
507 			printf("\n");
508 		}
509 	}
510 }
511 
512 static int
513 fdc_sense_drive(struct fdc_data *fdc, int *st3p)
514 {
515 	int st3;
516 
517 	if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
518 		return (fdc_err(fdc, "Sense Drive Status failed\n"));
519 	if (st3p)
520 		*st3p = st3;
521 	return (0);
522 }
523 
524 static int
525 fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
526 {
527 	int cyl, st0, ret;
528 
529 	ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
530 	if (ret) {
531 		(void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
532 		return (ret);
533 	}
534 
535 	if (st0p)
536 		*st0p = st0;
537 
538 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
539 		/*
540 		 * There doesn't seem to have been an interrupt.
541 		 */
542 		return (FD_NOT_VALID);
543 	}
544 
545 	if (fdc_in(fdc, &cyl) < 0)
546 		return fdc_err(fdc, "can't get cyl num\n");
547 
548 	if (cylp)
549 		*cylp = cyl;
550 
551 	return (0);
552 }
553 
554 static int
555 fdc_read_status(struct fdc_data *fdc)
556 {
557 	int i, ret, status;
558 
559 	for (i = ret = 0; i < 7; i++) {
560 		ret = fdc_in(fdc, &status);
561 		fdc->status[i] = status;
562 		if (ret != 0)
563 			break;
564 	}
565 
566 	if (ret == 0)
567 		fdc->flags |= FDC_STAT_VALID;
568 	else
569 		fdc->flags &= ~FDC_STAT_VALID;
570 
571 	return ret;
572 }
573 
574 /*
575  * Select this drive
576  */
577 static void
578 fd_select(struct fd_data *fd)
579 {
580 	struct fdc_data *fdc;
581 
582 	/* XXX: lock controller */
583 	fdc = fd->fdc;
584 	fdc->fdout &= ~FDO_FDSEL;
585 	fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
586 	fdout_wr(fdc, fdc->fdout);
587 }
588 
589 static void
590 fd_turnon(void *arg)
591 {
592 	struct fd_data *fd;
593 	struct bio *bp;
594 	int once;
595 
596 	fd = arg;
597 	mtx_lock(&fd->fdc->fdc_mtx);
598 	fd->flags &= ~FD_MOTORWAIT;
599 	fd->flags |= FD_MOTOR;
600 	once = 0;
601 	for (;;) {
602 		bp = bioq_takefirst(&fd->fd_bq);
603 		if (bp == NULL)
604 			break;
605 		bioq_disksort(&fd->fdc->head, bp);
606 		once = 1;
607 	}
608 	mtx_unlock(&fd->fdc->fdc_mtx);
609 	if (once)
610 		wakeup(&fd->fdc->head);
611 }
612 
613 static void
614 fd_motor(struct fd_data *fd, int turnon)
615 {
616 	struct fdc_data *fdc;
617 
618 	fdc = fd->fdc;
619 /*
620 	mtx_assert(&fdc->fdc_mtx, MA_OWNED);
621 */
622 	if (turnon) {
623 		fd->flags |= FD_MOTORWAIT;
624 		fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
625 		callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
626 	} else {
627 		callout_drain(&fd->toffhandle);
628 		fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
629 		fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
630 	}
631 	fdout_wr(fdc, fdc->fdout);
632 }
633 
634 static void
635 fd_turnoff(void *xfd)
636 {
637 	struct fd_data *fd = xfd;
638 
639 	mtx_lock(&fd->fdc->fdc_mtx);
640 	fd_motor(fd, 0);
641 	mtx_unlock(&fd->fdc->fdc_mtx);
642 }
643 
644 /*
645  * fdc_intr - wake up the worker thread.
646  */
647 
648 static void
649 fdc_intr(void *arg)
650 {
651 
652 	wakeup(arg);
653 }
654 
655 /*
656  * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
657  */
658 static void
659 fdc_pio(struct fdc_data *fdc)
660 {
661 	u_char *cptr;
662 	struct bio *bp;
663 	u_int count;
664 
665 	bp = fdc->bp;
666 	cptr = fdc->fd->fd_ioptr;
667 	count = fdc->fd->fd_iosize;
668 
669 	if (bp->bio_cmd == BIO_READ) {
670 		fdbcdr_wr(fdc, 0, count);
671 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
672 		    FD_YE_DATAPORT, cptr, count);
673 	} else {
674 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
675 		    FD_YE_DATAPORT, cptr, count);
676 		fdbcdr_wr(fdc, 0, count);	/* needed? */
677 	}
678 }
679 
680 static int
681 fdc_biodone(struct fdc_data *fdc, int error)
682 {
683 	struct fd_data *fd;
684 	struct bio *bp;
685 
686 	fd = fdc->fd;
687 	bp = fdc->bp;
688 
689 	mtx_lock(&fdc->fdc_mtx);
690 	if (--fd->fd_iocount == 0)
691 		callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
692 	fdc->bp = NULL;
693 	fdc->fd = NULL;
694 	mtx_unlock(&fdc->fdc_mtx);
695 	if (bp->bio_to != NULL) {
696 		if ((debugflags & 2) && fd->fdc->retry > 0)
697 			printf("retries: %d\n", fd->fdc->retry);
698 		g_io_deliver(bp, error);
699 		return (0);
700 	}
701 	bp->bio_error = error;
702 	bp->bio_flags |= BIO_DONE;
703 	wakeup(bp);
704 	return (0);
705 }
706 
707 static int retry_line;
708 
709 static int
710 fdc_worker(struct fdc_data *fdc)
711 {
712 	struct fd_data *fd;
713 	struct bio *bp;
714 	int i, nsect;
715 	int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
716 	int head;
717 	static int need_recal;
718 	struct fdc_readid *idp;
719 	struct fd_formb *finfo;
720 
721 	/* Have we exhausted our retries ? */
722 	bp = fdc->bp;
723 	fd = fdc->fd;
724 	if (bp != NULL &&
725 		(fdc->retry >= retries || (fd->flags & FDOPT_NORETRY))) {
726 		if ((debugflags & 4))
727 			printf("Too many retries (EIO)\n");
728 		return (fdc_biodone(fdc, EIO));
729 	}
730 
731 	/* Disable ISADMA if we bailed while it was active */
732 	if (fd != NULL && (fd->flags & FD_ISADMA)) {
733 		mtx_lock(&Giant);
734 		isa_dmadone(
735 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
736 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
737 		mtx_unlock(&Giant);
738 		mtx_lock(&fdc->fdc_mtx);
739 		fd->flags &= ~FD_ISADMA;
740 		mtx_unlock(&fdc->fdc_mtx);
741 	}
742 
743 	/* Unwedge the controller ? */
744 	if (fdc->flags & FDC_NEEDS_RESET) {
745 		fdc->flags &= ~FDC_NEEDS_RESET;
746 		fdc_reset(fdc);
747 		msleep(fdc, NULL, PRIBIO, "fdcrst", hz);
748 		/* Discard results */
749 		for (i = 0; i < 4; i++)
750 			fdc_sense_int(fdc, &st0, &cyl);
751 		/* All drives must recal */
752 		need_recal = 0xf;
753 	}
754 
755 	/* Pick up a request, if need be wait for it */
756 	if (fdc->bp == NULL) {
757 		mtx_lock(&fdc->fdc_mtx);
758 		do {
759 			fdc->bp = bioq_takefirst(&fdc->head);
760 			if (fdc->bp == NULL)
761 				msleep(&fdc->head, &fdc->fdc_mtx,
762 				    PRIBIO, "-", hz);
763 		} while (fdc->bp == NULL &&
764 		    (fdc->flags & FDC_KTHREAD_EXIT) == 0);
765 		mtx_unlock(&fdc->fdc_mtx);
766 
767 		if (fdc->bp == NULL)
768 			/*
769 			 * Nothing to do, worker thread has been
770 			 * requested to stop.
771 			 */
772 			return (0);
773 
774 		bp = fdc->bp;
775 		fd = fdc->fd = bp->bio_driver1;
776 		fdc->retry = 0;
777 		fd->fd_ioptr = bp->bio_data;
778 		if (bp->bio_cmd & BIO_FMT) {
779 			i = offsetof(struct fd_formb, fd_formb_cylno(0));
780 			fd->fd_ioptr += i;
781 			fd->fd_iosize = bp->bio_length - i;
782 		}
783 	}
784 
785 	/* Select drive, setup params */
786 	fd_select(fd);
787 	fdctl_wr(fdc, fd->ft->trans);
788 
789 	if (bp->bio_cmd & BIO_PROBE) {
790 
791 		if (!(fdin_rd(fdc) & FDI_DCHG) && !(fd->flags & FD_EMPTY))
792 			return (fdc_biodone(fdc, 0));
793 
794 		/*
795 		 * Try to find out if we have a disk in the drive
796 		 *
797 		 * First recal, then seek to cyl#1, this clears the
798 		 * old condition on the disk change line so we can
799 		 * examine it for current status
800 		 */
801 		if (debugflags & 0x40)
802 			printf("New disk in probe\n");
803 		mtx_lock(&fdc->fdc_mtx);
804 		fd->flags |= FD_NEWDISK;
805 		mtx_unlock(&fdc->fdc_mtx);
806 		retry_line = __LINE__;
807 		if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
808 			return (1);
809 		msleep(fdc, NULL, PRIBIO, "fdrecal", hz);
810 		retry_line = __LINE__;
811 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
812 			return (1); /* XXX */
813 		retry_line = __LINE__;
814 		if ((st0 & 0xc0) || cyl != 0)
815 			return (1);
816 
817 		/* Seek to track 1 */
818 		retry_line = __LINE__;
819 		if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
820 			return (1);
821 		msleep(fdc, NULL, PRIBIO, "fdseek", hz);
822 		retry_line = __LINE__;
823 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
824 			return (1); /* XXX */
825 		need_recal |= (1 << fd->fdsu);
826 		if (fdin_rd(fdc) & FDI_DCHG) {
827 			if (debugflags & 0x40)
828 				printf("Empty in probe\n");
829 			mtx_lock(&fdc->fdc_mtx);
830 			fd->flags |= FD_EMPTY;
831 			mtx_unlock(&fdc->fdc_mtx);
832 		} else {
833 			if (debugflags & 0x40)
834 				printf("Got disk in probe\n");
835 			mtx_lock(&fdc->fdc_mtx);
836 			fd->flags &= ~FD_EMPTY;
837 			mtx_unlock(&fdc->fdc_mtx);
838 			retry_line = __LINE__;
839 			if(fdc_sense_drive(fdc, &st3) != 0)
840 				return (1);
841 			mtx_lock(&fdc->fdc_mtx);
842 			if(st3 & NE7_ST3_WP)
843 				fd->flags |= FD_WP;
844 			else
845 				fd->flags &= ~FD_WP;
846 			mtx_unlock(&fdc->fdc_mtx);
847 		}
848 		return (fdc_biodone(fdc, 0));
849 	}
850 
851 	/*
852 	 * If we are dead just flush the requests
853 	 */
854 	if (fd->flags & FD_EMPTY)
855 		return (fdc_biodone(fdc, ENXIO));
856 
857 	/* Check if we lost our media */
858 	if (fdin_rd(fdc) & FDI_DCHG) {
859 		if (debugflags & 0x40)
860 			printf("Lost disk\n");
861 		mtx_lock(&fdc->fdc_mtx);
862 		fd->flags |= FD_EMPTY;
863 		fd->flags |= FD_NEWDISK;
864 		mtx_unlock(&fdc->fdc_mtx);
865 		g_topology_lock();
866 		g_orphan_provider(fd->fd_provider, EXDEV);
867 		fd->fd_provider->flags |= G_PF_WITHER;
868 		fd->fd_provider =
869 		    g_new_providerf(fd->fd_geom, fd->fd_geom->name);
870 		g_error_provider(fd->fd_provider, 0);
871 		g_topology_unlock();
872 		return (fdc_biodone(fdc, ENXIO));
873 	}
874 
875 	/* Check if the floppy is write-protected */
876 	if(bp->bio_cmd & (BIO_FMT | BIO_WRITE)) {
877 		retry_line = __LINE__;
878 		if(fdc_sense_drive(fdc, &st3) != 0)
879 			return (1);
880 		if(st3 & NE7_ST3_WP)
881 			return (fdc_biodone(fdc, EROFS));
882 	}
883 
884 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
885 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
886 	i = fd->ft->sectrac * fd->ft->heads;
887 	cylinder = bp->bio_pblkno / i;
888 	descyl = cylinder * steptrac;
889 	sec = bp->bio_pblkno % i;
890 	nsect = i - sec;
891 	head = sec / fd->ft->sectrac;
892 	sec = sec % fd->ft->sectrac + 1;
893 
894 	/* If everything is going swimmingly, use multisector xfer */
895 	if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
896 		fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
897 		nsect = fd->fd_iosize / fd->sectorsize;
898 	} else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
899 		fd->fd_iosize = fd->sectorsize;
900 		nsect = 1;
901 	}
902 
903 	/* Do RECAL if we need to or are going to track zero anyway */
904 	if ((need_recal & (1 << fd->fdsu)) ||
905 	    (cylinder == 0 && fd->track != 0) ||
906 	    fdc->retry > 2) {
907 		retry_line = __LINE__;
908 		if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
909 			return (1);
910 		msleep(fdc, NULL, PRIBIO, "fdrecal", hz);
911 		retry_line = __LINE__;
912 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
913 			return (1); /* XXX */
914 		retry_line = __LINE__;
915 		if ((st0 & 0xc0) || cyl != 0)
916 			return (1);
917 		need_recal &= ~(1 << fd->fdsu);
918 		fd->track = 0;
919 		/* let the heads settle */
920 		if (settle)
921 			msleep(fdc->fd, NULL, PRIBIO, "fdhdstl", settle);
922 	}
923 
924 	/*
925 	 * SEEK to where we want to be
926 	 *
927 	 * Enhanced controllers do implied seeks for read&write as long as
928 	 * we do not need multiple steps per track.
929 	 */
930 	if (cylinder != fd->track && (
931 	    fdc->fdct != FDC_ENHANCED ||
932 	    descyl != cylinder ||
933 	    (bp->bio_cmd & (BIO_RDID|BIO_FMT)))) {
934 		retry_line = __LINE__;
935 		if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
936 			return (1);
937 		msleep(fdc, NULL, PRIBIO, "fdseek", hz);
938 		retry_line = __LINE__;
939 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
940 			return (1); /* XXX */
941 		retry_line = __LINE__;
942 		if ((st0 & 0xc0) || cyl != descyl) {
943 			need_recal |= (1 << fd->fdsu);
944 			return (1);
945 		}
946 		/* let the heads settle */
947 		if (settle)
948 			msleep(fdc->fd, NULL, PRIBIO, "fdhdstl", settle);
949 	}
950 	fd->track = cylinder;
951 
952 	if (debugflags & 8)
953 		printf("op %x bn %ju siz %u ptr %p retry %d\n",
954 		    bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
955 		    fd->fd_ioptr, fdc->retry);
956 
957 	/* Setup ISADMA if we need it and have it */
958 	if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
959 	     && !(fdc->flags & FDC_NODMA)) {
960 		mtx_lock(&Giant);
961 		isa_dmastart(
962 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
963 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
964 		mtx_unlock(&Giant);
965 		mtx_lock(&fdc->fdc_mtx);
966 		fd->flags |= FD_ISADMA;
967 		mtx_unlock(&fdc->fdc_mtx);
968 	}
969 
970 	/* Do PIO if we have to */
971 	if (fdc->flags & FDC_NODMA) {
972 		if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
973 			fdbcdr_wr(fdc, 1, fd->fd_iosize);
974 		if (bp->bio_cmd & (BIO_WRITE|BIO_FMT))
975 			fdc_pio(fdc);
976 	}
977 
978 	switch(bp->bio_cmd) {
979 	case BIO_FMT:
980 		/* formatting */
981 		finfo = (struct fd_formb *)bp->bio_data;
982 		retry_line = __LINE__;
983 		if (fdc_cmd(fdc, 6,
984 		    NE7CMD_FORMAT | mfm,
985 		    head << 2 | fd->fdsu,
986 		    finfo->fd_formb_secshift,
987 		    finfo->fd_formb_nsecs,
988 		    finfo->fd_formb_gaplen,
989 		    finfo->fd_formb_fillbyte, 0))
990 			return (1);
991 		break;
992 	case BIO_RDID:
993 		retry_line = __LINE__;
994 		if (fdc_cmd(fdc, 2,
995 		    NE7CMD_READID | mfm,
996 		    head << 2 | fd->fdsu, 0))
997 			return (1);
998 		break;
999 	case BIO_READ:
1000 		retry_line = __LINE__;
1001 		if (fdc_cmd(fdc, 9,
1002 		    NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
1003 		    head << 2 | fd->fdsu,	/* head & unit */
1004 		    fd->track,			/* track */
1005 		    head,			/* head */
1006 		    sec,			/* sector + 1 */
1007 		    fd->ft->secsize,		/* sector size */
1008 		    fd->ft->sectrac,		/* sectors/track */
1009 		    fd->ft->gap,		/* gap size */
1010 		    fd->ft->datalen,		/* data length */
1011 		    0))
1012 			return (1);
1013 		break;
1014 	case BIO_WRITE:
1015 		retry_line = __LINE__;
1016 		if (fdc_cmd(fdc, 9,
1017 		    NE7CMD_WRITE | mfm | NE7CMD_MT,
1018 		    head << 2 | fd->fdsu,	/* head & unit */
1019 		    fd->track,			/* track */
1020 		    head,			/* head */
1021 		    sec,			/* sector + 1 */
1022 		    fd->ft->secsize,		/* sector size */
1023 		    fd->ft->sectrac,		/* sectors/track */
1024 		    fd->ft->gap,		/* gap size */
1025 		    fd->ft->datalen,		/* data length */
1026 		    0))
1027 			return (1);
1028 		break;
1029 	default:
1030 		KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
1031 	}
1032 
1033 	/* Wait for interrupt */
1034 	i = msleep(fdc, NULL, PRIBIO, "fddata", hz);
1035 
1036 	/* PIO if the read looks good */
1037 	if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ))
1038 		fdc_pio(fdc);
1039 
1040 	/* Finish DMA */
1041 	if (fd->flags & FD_ISADMA) {
1042 		mtx_lock(&Giant);
1043 		isa_dmadone(
1044 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1045 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1046 		mtx_unlock(&Giant);
1047 		mtx_lock(&fdc->fdc_mtx);
1048 		fd->flags &= ~FD_ISADMA;
1049 		mtx_unlock(&fdc->fdc_mtx);
1050 	}
1051 
1052 	if (i != 0) {
1053 		/*
1054 		 * Timeout.
1055 		 *
1056 		 * Due to IBM's brain-dead design, the FDC has a faked ready
1057 		 * signal, hardwired to ready == true. Thus, any command
1058 		 * issued if there's no diskette in the drive will _never_
1059 		 * complete, and must be aborted by resetting the FDC.
1060 		 * Many thanks, Big Blue!
1061 		 */
1062 		retry_line = __LINE__;
1063 		fdc->flags |= FDC_NEEDS_RESET;
1064 		return (1);
1065 	}
1066 
1067 	retry_line = __LINE__;
1068 	if (fdc_read_status(fdc))
1069 		return (1);
1070 
1071 	if (debugflags & 0x10)
1072 		printf("  -> %x %x %x %x\n",
1073 		    fdc->status[0], fdc->status[1],
1074 		    fdc->status[2], fdc->status[3]);
1075 
1076 	st0 = fdc->status[0] & NE7_ST0_IC;
1077 	if (st0 != 0) {
1078 		retry_line = __LINE__;
1079 		if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
1080 			/*
1081 			 * DMA overrun. Someone hogged the bus and
1082 			 * didn't release it in time for the next
1083 			 * FDC transfer.
1084 			 */
1085 			return (1);
1086 		}
1087 		retry_line = __LINE__;
1088 		if(st0 == NE7_ST0_IC_IV) {
1089 			fdc->flags |= FDC_NEEDS_RESET;
1090 			return (1);
1091 		}
1092 		retry_line = __LINE__;
1093 		if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
1094 			need_recal |= (1 << fd->fdsu);
1095 			return (1);
1096 		}
1097 		if (debugflags & 0x20) {
1098 			printf("status %02x %02x %02x %02x %02x %02x\n",
1099 			    fdc->status[0], fdc->status[1], fdc->status[2],
1100 			    fdc->status[3], fdc->status[4], fdc->status[5]);
1101 		}
1102 		retry_line = __LINE__;
1103 		return (1);
1104 	}
1105 	/* All OK */
1106 	switch(bp->bio_cmd) {
1107 	case BIO_RDID:
1108 		/* copy out ID field contents */
1109 		idp = (struct fdc_readid *)bp->bio_data;
1110 		idp->cyl = fdc->status[3];
1111 		idp->head = fdc->status[4];
1112 		idp->sec = fdc->status[5];
1113 		idp->secshift = fdc->status[6];
1114 		if (debugflags & 0x40)
1115 			printf("c %d h %d s %d z %d\n",
1116 			    idp->cyl, idp->head, idp->sec, idp->secshift);
1117 		break;
1118 	case BIO_READ:
1119 	case BIO_WRITE:
1120 		bp->bio_pblkno += nsect;
1121 		bp->bio_resid -= fd->fd_iosize;
1122 		bp->bio_completed += fd->fd_iosize;
1123 		fd->fd_ioptr += fd->fd_iosize;
1124 		/* Since we managed to get something done, reset the retry */
1125 		fdc->retry = 0;
1126 		if (bp->bio_resid > 0)
1127 			return (0);
1128 		break;
1129 	case BIO_FMT:
1130 		break;
1131 	}
1132 	return (fdc_biodone(fdc, 0));
1133 }
1134 
1135 static void
1136 fdc_thread(void *arg)
1137 {
1138 	struct fdc_data *fdc;
1139 
1140 	fdc = arg;
1141 	int i;
1142 
1143 	mtx_lock(&fdc->fdc_mtx);
1144 	fdc->flags |= FDC_KTHREAD_ALIVE;
1145 	while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
1146 		mtx_unlock(&fdc->fdc_mtx);
1147 		i = fdc_worker(fdc);
1148 		if (i && debugflags & 0x20) {
1149 			if (fdc->bp != NULL) {
1150 				g_print_bio(fdc->bp);
1151 				printf("\n");
1152 			}
1153 			printf("Retry line %d\n", retry_line);
1154 		}
1155 		fdc->retry += i;
1156 		mtx_lock(&fdc->fdc_mtx);
1157 	}
1158 	fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
1159 	mtx_unlock(&fdc->fdc_mtx);
1160 
1161 	kthread_exit(0);
1162 }
1163 
1164 /*
1165  * Enqueue a requst.
1166  */
1167 static void
1168 fd_enqueue(struct fd_data *fd, struct bio *bp)
1169 {
1170 	struct fdc_data *fdc;
1171 	int call;
1172 
1173 	call = 0;
1174 	fdc = fd->fdc;
1175 	mtx_lock(&fdc->fdc_mtx);
1176 	/* If we go from idle, cancel motor turnoff */
1177 	if (fd->fd_iocount++ == 0)
1178 		callout_drain(&fd->toffhandle);
1179 	if (fd->flags & FD_MOTOR) {
1180 		/* The motor is on, send it directly to the controller */
1181 		bioq_disksort(&fdc->head, bp);
1182 		wakeup(&fdc->head);
1183 	} else {
1184 		/* Queue it on the drive until the motor has started */
1185 		bioq_insert_tail(&fd->fd_bq, bp);
1186 		if (!(fd->flags & FD_MOTORWAIT))
1187 			fd_motor(fd, 1);
1188 	}
1189 	mtx_unlock(&fdc->fdc_mtx);
1190 }
1191 
1192 static int
1193 fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
1194 {
1195 	struct bio *bp;
1196 	struct fd_formb *finfo;
1197 	struct fdc_readid *idfield;
1198 	int error;
1199 
1200 	bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
1201 
1202 	/*
1203 	 * Set up a bio request for fdstrategy().  bio_offset is faked
1204 	 * so that fdstrategy() will seek to the the requested
1205 	 * cylinder, and use the desired head.
1206 	 */
1207 	bp->bio_cmd = cmd;
1208 	if (cmd == BIO_FMT) {
1209 		finfo = (struct fd_formb *)data;
1210 		bp->bio_pblkno =
1211 		    (finfo->cyl * fd->ft->heads + finfo->head) *
1212 		    fd->ft->sectrac;
1213 		bp->bio_length = sizeof *finfo;
1214 	} else if (cmd == BIO_RDID) {
1215 		idfield = (struct fdc_readid *)data;
1216 		bp->bio_pblkno =
1217 		    (idfield->cyl * fd->ft->heads + idfield->head) *
1218 		    fd->ft->sectrac;
1219 		bp->bio_length = sizeof(struct fdc_readid);
1220 	} else if (cmd == BIO_PROBE) {
1221 		/* nothing */
1222 	} else
1223 		panic("wrong cmd in fdmisccmd()");
1224 	bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
1225 	bp->bio_data = data;
1226 	bp->bio_driver1 = fd;
1227 	bp->bio_flags = 0;
1228 
1229 	fd_enqueue(fd, bp);
1230 
1231 	do {
1232 		msleep(bp, NULL, PRIBIO, "fdwait", hz);
1233 	} while (!(bp->bio_flags & BIO_DONE));
1234 	error = bp->bio_error;
1235 
1236 	free(bp, M_TEMP);
1237 	return (error);
1238 }
1239 
1240 /*
1241  * Try figuring out the density of the media present in our device.
1242  */
1243 static int
1244 fdautoselect(struct fd_data *fd)
1245 {
1246 	struct fd_type *fdtp;
1247 	struct fdc_readid id;
1248 	int oopts, rv;
1249 
1250 	if (!(fd->ft->flags & FL_AUTO))
1251 		return (0);
1252 
1253 	fdtp = fd_native_types[fd->type];
1254 	fdsettype(fd, fdtp);
1255 	if (!(fd->ft->flags & FL_AUTO))
1256 		return (0);
1257 
1258 	/*
1259 	 * Try reading sector ID fields, first at cylinder 0, head 0,
1260 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
1261 	 * since for 5.25in DD media in a HD drive, there are no data
1262 	 * to read (2 step pulses per media cylinder required).  For
1263 	 * two-sided media, the second probe always goes to head 1, so
1264 	 * we can tell them apart from single-sided media.  As a
1265 	 * side-effect this means that single-sided media should be
1266 	 * mentioned in the search list after two-sided media of an
1267 	 * otherwise identical density.  Media with a different number
1268 	 * of sectors per track but otherwise identical parameters
1269 	 * cannot be distinguished at all.
1270 	 *
1271 	 * If we successfully read an ID field on both cylinders where
1272 	 * the recorded values match our expectation, we are done.
1273 	 * Otherwise, we try the next density entry from the table.
1274 	 *
1275 	 * Stepping to cylinder 2 has the side-effect of clearing the
1276 	 * unit attention bit.
1277 	 */
1278 	oopts = fd->options;
1279 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1280 	for (; fdtp->heads; fdtp++) {
1281 		fdsettype(fd, fdtp);
1282 
1283 		id.cyl = id.head = 0;
1284 		rv = fdmisccmd(fd, BIO_RDID, &id);
1285 		if (rv != 0)
1286 			continue;
1287 		if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
1288 			continue;
1289 		id.cyl = 2;
1290 		id.head = fd->ft->heads - 1;
1291 		rv = fdmisccmd(fd, BIO_RDID, &id);
1292 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1293 		    id.secshift != fdtp->secsize)
1294 			continue;
1295 		if (rv == 0)
1296 			break;
1297 	}
1298 
1299 	fd->options = oopts;
1300 	if (fdtp->heads == 0) {
1301 		if (debugflags & 0x40)
1302 			device_printf(fd->dev, "autoselection failed\n");
1303 		fdsettype(fd, fd_native_types[fd->type]);
1304 		return (0);
1305 	} else {
1306 		if (debugflags & 0x40) {
1307 			device_printf(fd->dev,
1308 			    "autoselected %d KB medium\n", fd->ft->size / 2);
1309 			fdprinttype(fd->ft);
1310 		}
1311 		return (0);
1312 	}
1313 }
1314 
1315 /*
1316  * GEOM class implementation
1317  */
1318 
1319 static g_access_t	fd_access;
1320 static g_start_t	fd_start;
1321 static g_ioctl_t	fd_ioctl;
1322 
1323 struct g_class g_fd_class = {
1324 	.name =		"FD",
1325 	.version =	G_VERSION,
1326 	.start =	fd_start,
1327 	.access =	fd_access,
1328 	.ioctl =	fd_ioctl,
1329 };
1330 
1331 DECLARE_GEOM_CLASS(g_fd_class, g_fd);
1332 
1333 static int
1334 fd_access(struct g_provider *pp, int r, int w, int e)
1335 {
1336 	struct fd_data *fd;
1337 	struct fdc_data *fdc;
1338 	int ar, aw, ae;
1339 
1340 	fd = pp->geom->softc;
1341 	fdc = fd->fdc;
1342 
1343 	/*
1344 	 * If our provider is withering, we can only get negative requests
1345 	 * and we don't want to even see them
1346 	 */
1347 	if (pp->flags & G_PF_WITHER)
1348 		return (0);
1349 
1350 	ar = r + pp->acr;
1351 	aw = w + pp->acw;
1352 	ae = e + pp->ace;
1353 
1354 	if (ar == 0 && aw == 0 && ae == 0) {
1355 		device_unbusy(fd->dev);
1356 		return (0);
1357 	}
1358 
1359 	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
1360 		if (fdmisccmd(fd, BIO_PROBE, NULL))
1361 			return (ENXIO);
1362 		if (fd->flags & FD_EMPTY)
1363 			return (ENXIO);
1364 		if (fd->flags & FD_NEWDISK) {
1365 			fdautoselect(fd);
1366 			mtx_lock(&fdc->fdc_mtx);
1367 			fd->flags &= ~FD_NEWDISK;
1368 			mtx_unlock(&fdc->fdc_mtx);
1369 		}
1370 		device_busy(fd->dev);
1371 	}
1372 
1373 #ifdef notyet
1374 	if (w > 0 && (fd->flags & FD_WP))
1375 		return (EROFS);
1376 #endif
1377 
1378 	pp->sectorsize = fd->sectorsize;
1379 	pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
1380 	pp->mediasize = pp->stripesize * fd->ft->tracks;
1381 	return (0);
1382 }
1383 
1384 static void
1385 fd_start(struct bio *bp)
1386 {
1387  	struct fdc_data *	fdc;
1388  	struct fd_data *	fd;
1389 
1390 	fd = bp->bio_to->geom->softc;
1391 	fdc = fd->fdc;
1392 	bp->bio_driver1 = fd;
1393 	if (bp->bio_cmd & BIO_GETATTR) {
1394 		if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
1395 			return;
1396 		if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
1397 			return;
1398 		g_io_deliver(bp, ENOIOCTL);
1399 		return;
1400 	}
1401 	if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
1402 		g_io_deliver(bp, EOPNOTSUPP);
1403 		return;
1404 	}
1405 	bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
1406 	bp->bio_resid = bp->bio_length;
1407 	fd_enqueue(fd, bp);
1408 	return;
1409 }
1410 
1411 static int
1412 fd_ioctl(struct g_provider *pp, u_long cmd, void *data, struct thread *td)
1413 {
1414 	struct fd_data *fd;
1415 	struct fdc_status *fsp;
1416 	struct fdc_readid *rid;
1417 	int error;
1418 
1419 	fd = pp->geom->softc;
1420 
1421 	switch (cmd) {
1422 	case FD_GTYPE:                  /* get drive type */
1423 		*(struct fd_type *)data = *fd->ft;
1424 		return (0);
1425 
1426 	case FD_STYPE:                  /* set drive type */
1427 		/*
1428 		 * Allow setting drive type temporarily iff
1429 		 * currently unset.  Used for fdformat so any
1430 		 * user can set it, and then start formatting.
1431 		 */
1432 		fd->fts = *(struct fd_type *)data;
1433 		if (fd->fts.sectrac) {
1434 			/* XXX: check for rubbish */
1435 			fdsettype(fd, &fd->fts);
1436 		} else {
1437 			fdsettype(fd, fd_native_types[fd->type]);
1438 		}
1439 		if (debugflags & 0x40)
1440 			fdprinttype(fd->ft);
1441 		return (0);
1442 
1443 	case FD_GOPTS:			/* get drive options */
1444 		*(int *)data = fd->options;
1445 		return (0);
1446 
1447 	case FD_SOPTS:			/* set drive options */
1448 		fd->options = *(int *)data;
1449 		return (0);
1450 
1451 	case FD_CLRERR:
1452 		if (suser(td) != 0)
1453 			return (EPERM);
1454 		fd->fdc->fdc_errs = 0;
1455 		return (0);
1456 
1457 	case FD_GSTAT:
1458 		fsp = (struct fdc_status *)data;
1459 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
1460 			return (EINVAL);
1461 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
1462 		return (0);
1463 
1464 	case FD_GDTYPE:
1465 		*(enum fd_drivetype *)data = fd->type;
1466 		return (0);
1467 
1468 	case FD_FORM:
1469 		if (((struct fd_formb *)data)->format_version !=
1470 		    FD_FORMAT_VERSION)
1471 			return (EINVAL); /* wrong version of formatting prog */
1472 		error = fdmisccmd(fd, BIO_FMT, data);
1473 		mtx_lock(&fd->fdc->fdc_mtx);
1474 		fd->flags |= FD_NEWDISK;
1475 		mtx_unlock(&fd->fdc->fdc_mtx);
1476 		break;
1477 
1478 	case FD_READID:
1479 		rid = (struct fdc_readid *)data;
1480 		if (rid->cyl > 85 || rid->head > 1)
1481 			return (EINVAL);
1482 		error = fdmisccmd(fd, BIO_RDID, data);
1483 		break;
1484 
1485 	case FIONBIO:
1486 	case FIOASYNC:
1487 		/* For backwards compat with old fd*(8) tools */
1488 		error = 0;
1489 		break;
1490 
1491 	default:
1492 		if (debugflags & 0x80)
1493 			printf("Unknown ioctl %lx\n", cmd);
1494 		error = ENOIOCTL;
1495 		break;
1496 	}
1497 	return (error);
1498 };
1499 
1500 
1501 
1502 /*
1503  * Configuration/initialization stuff, per controller.
1504  */
1505 
1506 devclass_t fdc_devclass;
1507 static devclass_t fd_devclass;
1508 
1509 struct fdc_ivars {
1510 	int	fdunit;
1511 	int	fdtype;
1512 };
1513 
1514 void
1515 fdc_release_resources(struct fdc_data *fdc)
1516 {
1517 	device_t dev;
1518 
1519 	dev = fdc->fdc_dev;
1520 	if (fdc->fdc_intr)
1521 		bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1522 	fdc->fdc_intr = NULL;
1523 	if (fdc->res_irq != NULL)
1524 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1525 		    fdc->res_irq);
1526 	fdc->res_irq = NULL;
1527 	if (fdc->res_ctl != NULL)
1528 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
1529 		    fdc->res_ctl);
1530 	fdc->res_ctl = NULL;
1531 	if (fdc->res_sts != NULL)
1532 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_sts,
1533 		    fdc->res_sts);
1534 	fdc->res_sts = NULL;
1535 	if (fdc->res_ioport != NULL)
1536 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
1537 		    fdc->res_ioport);
1538 	fdc->res_ioport = NULL;
1539 	if (fdc->res_drq != NULL)
1540 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1541 		    fdc->res_drq);
1542 	fdc->res_drq = NULL;
1543 }
1544 
1545 int
1546 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1547 {
1548 	struct fdc_ivars *ivars = device_get_ivars(child);
1549 
1550 	switch (which) {
1551 	case FDC_IVAR_FDUNIT:
1552 		*result = ivars->fdunit;
1553 		break;
1554 	case FDC_IVAR_FDTYPE:
1555 		*result = ivars->fdtype;
1556 		break;
1557 	default:
1558 		return (ENOENT);
1559 	}
1560 	return (0);
1561 }
1562 
1563 int
1564 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1565 {
1566 	struct fdc_ivars *ivars = device_get_ivars(child);
1567 
1568 	switch (which) {
1569 	case FDC_IVAR_FDUNIT:
1570 		ivars->fdunit = value;
1571 		break;
1572 	case FDC_IVAR_FDTYPE:
1573 		ivars->fdtype = value;
1574 		break;
1575 	default:
1576 		return (ENOENT);
1577 	}
1578 	return (0);
1579 }
1580 
1581 int
1582 fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1583 {
1584 	int ic_type, part_id;
1585 
1586 	/*
1587 	 * A status value of 0xff is very unlikely, but not theoretically
1588 	 * impossible, but it is far more likely to indicate an empty bus.
1589 	 */
1590 	if (fdsts_rd(fdc) == 0xff)
1591 		return (ENXIO);
1592 
1593 	/*
1594 	 * Assert a reset to the floppy controller and check that the status
1595 	 * register goes to zero.
1596 	 */
1597 	fdout_wr(fdc, 0);
1598 	fdout_wr(fdc, 0);
1599 	if (fdsts_rd(fdc) != 0)
1600 		return (ENXIO);
1601 
1602 	/*
1603 	 * Clear the reset and see it come ready.
1604 	 */
1605 	fdout_wr(fdc, FDO_FRST);
1606 	DELAY(100);
1607 	if (fdsts_rd(fdc) != 0x80)
1608 		return (ENXIO);
1609 
1610 	/* Then, see if it can handle a command. */
1611 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0))
1612 		return (ENXIO);
1613 
1614 	/*
1615 	 * Try to identify the chip.
1616 	 *
1617 	 * The i8272 datasheet documents that unknown commands
1618 	 * will return ST0 as 0x80.  The i8272 is supposedly identical
1619 	 * to the NEC765.
1620 	 * The i82077SL datasheet says 0x90 for the VERSION command,
1621 	 * and several "superio" chips emulate this.
1622 	 */
1623 	if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
1624 		return (ENXIO);
1625 	if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
1626 		return (ENXIO);
1627 	if (bootverbose)
1628 		device_printf(dev,
1629 		    "ic_type %02x part_id %02x\n", ic_type, part_id);
1630 	switch (ic_type & 0xff) {
1631 	case 0x80:
1632 		device_set_desc(dev, "NEC 765 or clone");
1633 		fdc->fdct = FDC_NE765;
1634 		break;
1635 	case 0x81:
1636 	case 0x90:
1637 		device_set_desc(dev,
1638 		    "Enhanced floppy controller");
1639 		fdc->fdct = FDC_ENHANCED;
1640 		break;
1641 	default:
1642 		device_set_desc(dev, "Generic floppy controller");
1643 		fdc->fdct = FDC_UNKNOWN;
1644 		break;
1645 	}
1646 	return (0);
1647 }
1648 
1649 int
1650 fdc_detach(device_t dev)
1651 {
1652 	struct	fdc_data *fdc;
1653 	int	error;
1654 
1655 	fdc = device_get_softc(dev);
1656 
1657 	/* have our children detached first */
1658 	if ((error = bus_generic_detach(dev)))
1659 		return (error);
1660 
1661 	/* kill worker thread */
1662 	fdc->flags |= FDC_KTHREAD_EXIT;
1663 	mtx_lock(&fdc->fdc_mtx);
1664 	while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
1665 		msleep(&fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
1666 	mtx_unlock(&fdc->fdc_mtx);
1667 
1668 	/* reset controller, turn motor off */
1669 	fdout_wr(fdc, 0);
1670 
1671 	if (!(fdc->flags & FDC_NODMA))
1672 		isa_dma_release(fdc->dmachan);
1673 	fdc_release_resources(fdc);
1674 	mtx_destroy(&fdc->fdc_mtx);
1675 	return (0);
1676 }
1677 
1678 /*
1679  * Add a child device to the fdc controller.  It will then be probed etc.
1680  */
1681 device_t
1682 fdc_add_child(device_t dev, const char *name, int unit)
1683 {
1684 	int flags;
1685 	struct fdc_ivars *ivar;
1686 	device_t child;
1687 
1688 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1689 	if (ivar == NULL)
1690 		return (NULL);
1691 	child = device_add_child(dev, name, unit);
1692 	if (child == NULL) {
1693 		free(ivar, M_DEVBUF);
1694 		return (NULL);
1695 	}
1696 	device_set_ivars(child, ivar);
1697 	ivar->fdunit = unit;
1698 	ivar->fdtype = FDT_NONE;
1699 	if (resource_int_value(name, unit, "flags", &flags) == 0)
1700 		device_set_flags(child, flags);
1701 	if (resource_disabled(name, unit))
1702 		device_disable(child);
1703 	return (child);
1704 }
1705 
1706 int
1707 fdc_attach(device_t dev)
1708 {
1709 	struct	fdc_data *fdc;
1710 	int	error;
1711 
1712 	fdc = device_get_softc(dev);
1713 	fdc->fdc_dev = dev;
1714 	error = fdc_initial_reset(dev, fdc);
1715 	if (error) {
1716 		device_printf(dev, "does not respond\n");
1717 		return (error);
1718 	}
1719 	error = bus_setup_intr(dev, fdc->res_irq,
1720 	    INTR_TYPE_BIO | INTR_ENTROPY | INTR_FAST | INTR_MPSAFE,
1721 	    fdc_intr, fdc, &fdc->fdc_intr);
1722 	if (error) {
1723 		device_printf(dev, "cannot setup interrupt\n");
1724 		return (error);
1725 	}
1726 	if (!(fdc->flags & FDC_NODMA)) {
1727 		error = isa_dma_acquire(fdc->dmachan);
1728 		if (error)
1729 			return (error);
1730 		/* XXX no error return */
1731 		isa_dmainit(fdc->dmachan, MAX_BYTES_PER_CYL);
1732 	}
1733 	fdc->fdcu = device_get_unit(dev);
1734 	fdc->flags |= FDC_NEEDS_RESET;
1735 
1736 	mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
1737 
1738 	/* reset controller, turn motor off, clear fdout mirror reg */
1739 	fdout_wr(fdc, fdc->fdout = 0);
1740 	bioq_init(&fdc->head);
1741 
1742 	kthread_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
1743 	    "fdc%d", device_get_unit(dev));
1744 
1745 	settle = hz / 8;
1746 
1747 	return (0);
1748 }
1749 
1750 int
1751 fdc_hints_probe(device_t dev)
1752 {
1753 	const char *name, *dname;
1754 	int i, error, dunit;
1755 
1756 	/*
1757 	 * Probe and attach any children.  We should probably detect
1758 	 * devices from the BIOS unless overridden.
1759 	 */
1760 	name = device_get_nameunit(dev);
1761 	i = 0;
1762 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1763 		resource_int_value(dname, dunit, "drive", &dunit);
1764 		fdc_add_child(dev, dname, dunit);
1765 	}
1766 
1767 	if ((error = bus_generic_attach(dev)) != 0)
1768 		return (error);
1769 	return (0);
1770 }
1771 
1772 int
1773 fdc_print_child(device_t me, device_t child)
1774 {
1775 	int retval = 0, flags;
1776 
1777 	retval += bus_print_child_header(me, child);
1778 	retval += printf(" on %s drive %d", device_get_nameunit(me),
1779 	       fdc_get_fdunit(child));
1780 	if ((flags = device_get_flags(me)) != 0)
1781 		retval += printf(" flags %#x", flags);
1782 	retval += printf("\n");
1783 
1784 	return (retval);
1785 }
1786 
1787 /*
1788  * Configuration/initialization, per drive.
1789  */
1790 static int
1791 fd_probe(device_t dev)
1792 {
1793 	int	i, unit;
1794 	u_int	st0, st3;
1795 	struct	fd_data *fd;
1796 	struct	fdc_data *fdc;
1797 	int	fdsu;
1798 	int	flags, type;
1799 
1800 	fdsu = fdc_get_fdunit(dev);
1801 	fd = device_get_softc(dev);
1802 	fdc = device_get_softc(device_get_parent(dev));
1803 	flags = device_get_flags(dev);
1804 
1805 	fd->dev = dev;
1806 	fd->fdc = fdc;
1807 	fd->fdsu = fdsu;
1808 	unit = device_get_unit(dev);
1809 
1810 	/* Auto-probe if fdinfo is present, but always allow override. */
1811 	type = flags & FD_TYPEMASK;
1812 	if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1813 		fd->type = type;
1814 		goto done;
1815 	} else {
1816 		/* make sure fdautoselect() will be called */
1817 		fd->flags = FD_EMPTY;
1818 		fd->type = type;
1819 	}
1820 
1821 #if (defined(__i386__) && !defined(PC98)) || defined(__amd64__)
1822 	if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
1823 		/* Look up what the BIOS thinks we have. */
1824 		if (unit == 0)
1825 			fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1826 		else
1827 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1828 		if (fd->type == FDT_288M_1)
1829 			fd->type = FDT_288M;
1830 	}
1831 #endif /* __i386__ || __amd64__ */
1832 	/* is there a unit? */
1833 	if (fd->type == FDT_NONE)
1834 		return (ENXIO);
1835 
1836 /*
1837 	mtx_lock(&fdc->fdc_mtx);
1838 */
1839 	/* select it */
1840 	fd_select(fd);
1841 	fd_motor(fd, 1);
1842 	fdc->fd = fd;
1843 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
1844 	DELAY(1000000);	/* 1 sec */
1845 
1846 	if ((flags & FD_NO_PROBE) == 0) {
1847 		/* If we're at track 0 first seek inwards. */
1848 		if ((fdc_sense_drive(fdc, &st3) == 0) &&
1849 		    (st3 & NE7_ST3_T0)) {
1850 			/* Seek some steps... */
1851 			if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1852 				/* ...wait a moment... */
1853 				DELAY(300000);
1854 				/* make ctrlr happy: */
1855 				fdc_sense_int(fdc, 0, 0);
1856 			}
1857 		}
1858 
1859 		for (i = 0; i < 2; i++) {
1860 			/*
1861 			 * we must recalibrate twice, just in case the
1862 			 * heads have been beyond cylinder 76, since
1863 			 * most FDCs still barf when attempting to
1864 			 * recalibrate more than 77 steps
1865 			 */
1866 			/* go back to 0: */
1867 			if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1868 				/* a second being enough for full stroke seek*/
1869 				DELAY(i == 0 ? 1000000 : 300000);
1870 
1871 				/* anything responding? */
1872 				if (fdc_sense_int(fdc, &st0, 0) == 0 &&
1873 				    (st0 & NE7_ST0_EC) == 0)
1874 					break; /* already probed succesfully */
1875 			}
1876 		}
1877 	}
1878 
1879 	fd_motor(fd, 0);
1880 	fdc->fd = NULL;
1881 /*
1882 	mtx_unlock(&fdc->fdc_mtx);
1883 */
1884 
1885 	if ((flags & FD_NO_PROBE) == 0 &&
1886 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1887 		return (ENXIO);
1888 
1889 done:
1890 
1891 	switch (fd->type) {
1892 	case FDT_12M:
1893 		device_set_desc(dev, "1200-KB 5.25\" drive");
1894 		break;
1895 	case FDT_144M:
1896 		device_set_desc(dev, "1440-KB 3.5\" drive");
1897 		break;
1898 	case FDT_288M:
1899 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1900 		break;
1901 	case FDT_360K:
1902 		device_set_desc(dev, "360-KB 5.25\" drive");
1903 		break;
1904 	case FDT_720K:
1905 		device_set_desc(dev, "720-KB 3.5\" drive");
1906 		break;
1907 	default:
1908 		return (ENXIO);
1909 	}
1910 	fd->track = FD_NO_TRACK;
1911 	fd->fdc = fdc;
1912 	fd->fdsu = fdsu;
1913 	fd->options = 0;
1914 	callout_init(&fd->toffhandle, 1);
1915 	callout_init(&fd->tohandle, 1);
1916 
1917 	/* initialize densities for subdevices */
1918 	fdsettype(fd, fd_native_types[fd->type]);
1919 	return (0);
1920 }
1921 
1922 /*
1923  * We have to do this in a geom event because GEOM is not running
1924  * when fd_attach() is.
1925  * XXX: move fd_attach after geom like ata/scsi disks
1926  */
1927 static void
1928 fd_attach2(void *arg, int flag)
1929 {
1930 	struct	fd_data *fd;
1931 
1932 	fd = arg;
1933 
1934 	fd->fd_geom = g_new_geomf(&g_fd_class,
1935 	    "fd%d", device_get_unit(fd->fdc->fdc_dev));
1936 	fd->fd_provider = g_new_providerf(fd->fd_geom, fd->fd_geom->name);
1937 	fd->fd_geom->softc = fd;
1938 	g_error_provider(fd->fd_provider, 0);
1939 }
1940 
1941 static int
1942 fd_attach(device_t dev)
1943 {
1944 	struct	fd_data *fd;
1945 
1946 	fd = device_get_softc(dev);
1947 	g_post_event(fd_attach2, fd, M_WAITOK, NULL);
1948 	fd->flags |= FD_EMPTY;
1949 	bioq_init(&fd->fd_bq);
1950 	return (0);
1951 
1952 	return (0);
1953 }
1954 
1955 static int
1956 fd_detach(device_t dev)
1957 {
1958 	struct	fd_data *fd;
1959 
1960 	fd = device_get_softc(dev);
1961 	callout_drain(&fd->toffhandle);
1962 
1963 	return (0);
1964 }
1965 
1966 static device_method_t fd_methods[] = {
1967 	/* Device interface */
1968 	DEVMETHOD(device_probe,		fd_probe),
1969 	DEVMETHOD(device_attach,	fd_attach),
1970 	DEVMETHOD(device_detach,	fd_detach),
1971 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1972 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
1973 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
1974 	{ 0, 0 }
1975 };
1976 
1977 static driver_t fd_driver = {
1978 	"fd",
1979 	fd_methods,
1980 	sizeof(struct fd_data)
1981 };
1982 
1983 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1984