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