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