xref: /freebsd/sys/dev/fdc/fdc.c (revision f8353ede58132ea200d95139602672d23ddd2565)
1752d4735SNate Lawson /*-
21b67be7bSPoul-Henning Kamp  * Copyright (c) 2004 Poul-Henning Kamp
35b81b6b3SRodney W. Grimes  * Copyright (c) 1990 The Regents of the University of California.
45b81b6b3SRodney W. Grimes  * All rights reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
75b81b6b3SRodney W. Grimes  * Don Ahn.
85b81b6b3SRodney W. Grimes  *
969acd21dSWarner Losh  * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
1069acd21dSWarner Losh  * aided by the Linux floppy driver modifications from David Bateman
1169acd21dSWarner Losh  * (dbateman@eng.uts.edu.au).
1269acd21dSWarner Losh  *
13dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994 by
143a2f7427SDavid Greenman  *  jc@irbs.UUCP (John Capo)
153a2f7427SDavid Greenman  *  vak@zebub.msk.su (Serge Vakulenko)
163a2f7427SDavid Greenman  *  ache@astral.msk.su (Andrew A. Chernov)
17dc16046fSJoerg Wunsch  *
18dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994, 1995 by
193a2f7427SDavid Greenman  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
20dc5df763SJoerg Wunsch  *  dufault@hda.com (Peter Dufault)
213a2f7427SDavid Greenman  *
222995d110SJoerg Wunsch  * Copyright (c) 2001 Joerg Wunsch,
2306740f7bSJoerg Wunsch  *  joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
242995d110SJoerg Wunsch  *
255b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
265b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
275b81b6b3SRodney W. Grimes  * are met:
285b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
295b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
305b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
315b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
325b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
3364860614SJoerg Wunsch  * 4. Neither the name of the University nor the names of its contributors
3464860614SJoerg Wunsch  *    may be used to endorse or promote products derived from this software
3564860614SJoerg Wunsch  *    without specific prior written permission.
365b81b6b3SRodney W. Grimes  *
375b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
385b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
395b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
405b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
415b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
425b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
435b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
445b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
455b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
465b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
475b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
485b81b6b3SRodney W. Grimes  *
49dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
501b67be7bSPoul-Henning Kamp  *
515b81b6b3SRodney W. Grimes  */
525b81b6b3SRodney W. Grimes 
538c9bbf48SDavid E. O'Brien #include <sys/cdefs.h>
548c9bbf48SDavid E. O'Brien __FBSDID("$FreeBSD$");
558c9bbf48SDavid E. O'Brien 
56d2fb4892SJoerg Wunsch #include "opt_fdc.h"
575b81b6b3SRodney W. Grimes 
58b99f0a4aSAndrew Moore #include <sys/param.h>
599626b608SPoul-Henning Kamp #include <sys/bio.h>
606182fdbdSPeter Wemm #include <sys/bus.h>
61b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h>
62f90c382cSPoul-Henning Kamp #include <sys/disk.h>
636182fdbdSPeter Wemm #include <sys/fcntl.h>
64e774b251SJoerg Wunsch #include <sys/fdcio.h>
651a6bed68SJoerg Wunsch #include <sys/filio.h>
66fb919e4dSMark Murray #include <sys/kernel.h>
671b67be7bSPoul-Henning Kamp #include <sys/kthread.h>
68fb919e4dSMark Murray #include <sys/lock.h>
69b99f0a4aSAndrew Moore #include <sys/malloc.h>
706182fdbdSPeter Wemm #include <sys/module.h>
71fb919e4dSMark Murray #include <sys/mutex.h>
723a2f7427SDavid Greenman #include <sys/proc.h>
736182fdbdSPeter Wemm #include <sys/rman.h>
741b67be7bSPoul-Henning Kamp #include <sys/sysctl.h>
756d6fa4fdSWarner Losh #include <sys/systm.h>
766182fdbdSPeter Wemm 
771b67be7bSPoul-Henning Kamp #include <geom/geom.h>
781b67be7bSPoul-Henning Kamp 
79c3ae4c40SWarner Losh #include <machine/bus.h>
806182fdbdSPeter Wemm #include <machine/clock.h>
81dc5df763SJoerg Wunsch #include <machine/stdarg.h>
826182fdbdSPeter Wemm 
836182fdbdSPeter Wemm #include <isa/isavar.h>
84a97c75b7SDoug Rabson #include <isa/isareg.h>
856d6fa4fdSWarner Losh #include <dev/fdc/fdcvar.h>
86a97c75b7SDoug Rabson #include <isa/rtc.h>
876182fdbdSPeter Wemm 
881b67be7bSPoul-Henning Kamp #include <dev/ic/nec765.h>
890722d6abSJoerg Wunsch 
903fef646eSJoerg Wunsch /*
911b67be7bSPoul-Henning Kamp  * Runtime configuration hints/flags
923fef646eSJoerg Wunsch  */
931b67be7bSPoul-Henning Kamp 
941b67be7bSPoul-Henning Kamp /* configuration flags for fd */
951b67be7bSPoul-Henning Kamp #define FD_TYPEMASK	0x0f	/* drive type, matches enum
961b67be7bSPoul-Henning Kamp 				 * fd_drivetype; on i386 machines, if
971b67be7bSPoul-Henning Kamp 				 * given as 0, use RTC type for fd0
981b67be7bSPoul-Henning Kamp 				 * and fd1 */
991b67be7bSPoul-Henning Kamp #define FD_NO_PROBE	0x20	/* don't probe drive (seek test), just
1001b67be7bSPoul-Henning Kamp 				 * assume it is there */
1011b67be7bSPoul-Henning Kamp 
1021b67be7bSPoul-Henning Kamp /*
1031b67be7bSPoul-Henning Kamp  * Things that could conceiveably considered parameters or tweakables
1041b67be7bSPoul-Henning Kamp  */
1051b67be7bSPoul-Henning Kamp 
1061b67be7bSPoul-Henning Kamp /*
1071b67be7bSPoul-Henning Kamp  * Maximal number of bytes in a cylinder.
1081b67be7bSPoul-Henning Kamp  * This is used for ISADMA bouncebuffer allocation and sets the max
1091b67be7bSPoul-Henning Kamp  * xfersize we support.
1101b67be7bSPoul-Henning Kamp  *
11161909e4cSPoul-Henning Kamp  * 2.88M format has 2 x 36 x 512, allow for hacked up density.
1121b67be7bSPoul-Henning Kamp  */
1131b67be7bSPoul-Henning Kamp 
1141b67be7bSPoul-Henning Kamp #define MAX_BYTES_PER_CYL	(2 * 40 * 512)
115dc5df763SJoerg Wunsch 
1161a6bed68SJoerg Wunsch /*
117cb38bb6cSJoerg Wunsch  * Timeout value for the PIO loops to wait until the FDC main status
118cb38bb6cSJoerg Wunsch  * register matches our expectations (request for master, direction
119cb38bb6cSJoerg Wunsch  * bit).  This is supposed to be a number of microseconds, although
120cb38bb6cSJoerg Wunsch  * timing might actually not be very accurate.
121cb38bb6cSJoerg Wunsch  *
122cb38bb6cSJoerg Wunsch  * Timeouts of 100 msec are believed to be required for some broken
123cb38bb6cSJoerg Wunsch  * (old) hardware.
124cb38bb6cSJoerg Wunsch  */
125cb38bb6cSJoerg Wunsch #define	FDSTS_TIMEOUT	100000
126cb38bb6cSJoerg Wunsch 
127cb38bb6cSJoerg Wunsch /*
1281b67be7bSPoul-Henning Kamp  * After this many errors, stop whining.  Close will reset this count.
1291a6bed68SJoerg Wunsch  */
13061909e4cSPoul-Henning Kamp #define FDC_ERRMAX	100
1317ca0641bSAndrey A. Chernov 
1321a6bed68SJoerg Wunsch /*
1331b67be7bSPoul-Henning Kamp  * AutoDensity search lists for each drive type.
1341a6bed68SJoerg Wunsch  */
1351b67be7bSPoul-Henning Kamp 
1361b67be7bSPoul-Henning Kamp static struct fd_type fd_searchlist_360k[] = {
1371b67be7bSPoul-Henning Kamp 	{ FDF_5_360 },
1381b67be7bSPoul-Henning Kamp 	{ 0 }
1395b81b6b3SRodney W. Grimes };
1405b81b6b3SRodney W. Grimes 
1411a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_12m[] = {
1421b67be7bSPoul-Henning Kamp 	{ FDF_5_1200 | FL_AUTO },
1431b67be7bSPoul-Henning Kamp 	{ FDF_5_360 | FL_2STEP | FL_AUTO},
1441b67be7bSPoul-Henning Kamp 	{ 0 }
1451b67be7bSPoul-Henning Kamp };
1461b67be7bSPoul-Henning Kamp 
1471b67be7bSPoul-Henning Kamp static struct fd_type fd_searchlist_720k[] = {
1481b67be7bSPoul-Henning Kamp 	{ FDF_3_720 },
1491b67be7bSPoul-Henning Kamp 	{ 0 }
1501a6bed68SJoerg Wunsch };
1511a6bed68SJoerg Wunsch 
1521a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_144m[] = {
1531b67be7bSPoul-Henning Kamp 	{ FDF_3_1440 | FL_AUTO},
1541b67be7bSPoul-Henning Kamp 	{ FDF_3_720 | FL_AUTO},
1551b67be7bSPoul-Henning Kamp 	{ 0 }
1561a6bed68SJoerg Wunsch };
1571a6bed68SJoerg Wunsch 
1581a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_288m[] = {
1591b67be7bSPoul-Henning Kamp 	{ FDF_3_1440 | FL_AUTO },
1601a6bed68SJoerg Wunsch #if 0
1611b67be7bSPoul-Henning Kamp 	{ FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
1621a6bed68SJoerg Wunsch #endif
1631b67be7bSPoul-Henning Kamp 	{ FDF_3_720 | FL_AUTO},
1641b67be7bSPoul-Henning Kamp 	{ 0 }
1651a6bed68SJoerg Wunsch };
166dc16046fSJoerg Wunsch 
1671b67be7bSPoul-Henning Kamp /*
1681b67be7bSPoul-Henning Kamp  * Order must match enum fd_drivetype in <sys/fdcio.h>.
1691b67be7bSPoul-Henning Kamp  */
1701b67be7bSPoul-Henning Kamp static struct fd_type *fd_native_types[] = {
1711b67be7bSPoul-Henning Kamp 	NULL,				/* FDT_NONE */
1721b67be7bSPoul-Henning Kamp 	fd_searchlist_360k, 		/* FDT_360K */
1731b67be7bSPoul-Henning Kamp 	fd_searchlist_12m, 		/* FDT_12M */
1741b67be7bSPoul-Henning Kamp 	fd_searchlist_720k, 		/* FDT_720K */
1751b67be7bSPoul-Henning Kamp 	fd_searchlist_144m, 		/* FDT_144M */
1768fe457d6SPeter Edwards 	fd_searchlist_288m,		/* FDT_288M_1 (mapped to FDT_288M) */
1771b67be7bSPoul-Henning Kamp 	fd_searchlist_288m, 		/* FDT_288M */
1781b67be7bSPoul-Henning Kamp };
179f8ce7dd5SJoerg Wunsch 
1801b67be7bSPoul-Henning Kamp /*
1811b67be7bSPoul-Henning Kamp  * Internals start here
1821b67be7bSPoul-Henning Kamp  */
1831b67be7bSPoul-Henning Kamp 
1841b67be7bSPoul-Henning Kamp /* registers */
1851b67be7bSPoul-Henning Kamp #define	FDOUT	2	/* Digital Output Register (W) */
1861b67be7bSPoul-Henning Kamp #define	FDO_FDSEL	0x03	/*  floppy device select */
1871b67be7bSPoul-Henning Kamp #define	FDO_FRST	0x04	/*  floppy controller reset */
1881b67be7bSPoul-Henning Kamp #define	FDO_FDMAEN	0x08	/*  enable floppy DMA and Interrupt */
1891b67be7bSPoul-Henning Kamp #define	FDO_MOEN0	0x10	/*  motor enable drive 0 */
1901b67be7bSPoul-Henning Kamp #define	FDO_MOEN1	0x20	/*  motor enable drive 1 */
1911b67be7bSPoul-Henning Kamp #define	FDO_MOEN2	0x40	/*  motor enable drive 2 */
1921b67be7bSPoul-Henning Kamp #define	FDO_MOEN3	0x80	/*  motor enable drive 3 */
1931b67be7bSPoul-Henning Kamp 
1941b67be7bSPoul-Henning Kamp #define	FDSTS	4	/* NEC 765 Main Status Register (R) */
1951b67be7bSPoul-Henning Kamp #define	FDDATA	5	/* NEC 765 Data Register (R/W) */
1961b67be7bSPoul-Henning Kamp #define	FDCTL	7	/* Control Register (W) */
1971b67be7bSPoul-Henning Kamp 
1981b67be7bSPoul-Henning Kamp /*
199b4046cd7SWarner Losh  * The YE-DATA PC Card floppies use PIO to read in the data rather than
200b4046cd7SWarner Losh  * DMA due to the wild variability of DMA for the PC Card devices.  In
201b4046cd7SWarner Losh  * addition, if we cannot setup the DMA resources for the ISA attachment,
202b4046cd7SWarner Losh  * we'll use this same offset for data transfer.
203b4046cd7SWarner Losh  *
204b4046cd7SWarner Losh  * For this mode, offset 0 and 1 must be used to setup the transfer
205b4046cd7SWarner Losh  * for this floppy.  This means they are only available on those systems
206b4046cd7SWarner Losh  * that map them to the floppy drive.  Newer systems do not do this, and
207b4046cd7SWarner Losh  * we should likely prohibit access to them (or disallow NODMA to be set).
2081b67be7bSPoul-Henning Kamp  */
209b4046cd7SWarner Losh #define FDBCDR		0	/* And 1 */
210b4046cd7SWarner Losh #define FD_YE_DATAPORT	6	/* Drive Data port */
2111b67be7bSPoul-Henning Kamp 
2121b67be7bSPoul-Henning Kamp #define	FDI_DCHG	0x80	/* diskette has been changed */
2131b67be7bSPoul-Henning Kamp 				/* requires drive and motor being selected */
2141b67be7bSPoul-Henning Kamp 				/* is cleared by any step pulse to drive */
2151b67be7bSPoul-Henning Kamp 
2161b67be7bSPoul-Henning Kamp /*
21761909e4cSPoul-Henning Kamp  * We have three private BIO commands.
2181b67be7bSPoul-Henning Kamp  */
2191b67be7bSPoul-Henning Kamp #define BIO_PROBE	BIO_CMD0
2201b67be7bSPoul-Henning Kamp #define BIO_RDID	BIO_CMD1
2211b67be7bSPoul-Henning Kamp #define BIO_FMT		BIO_CMD2
2225b81b6b3SRodney W. Grimes 
223246ed35dSJoerg Wunsch /*
224246ed35dSJoerg Wunsch  * Per drive structure (softc).
225246ed35dSJoerg Wunsch  */
2266182fdbdSPeter Wemm struct fd_data {
2271b67be7bSPoul-Henning Kamp 	u_char 	*fd_ioptr;	/* IO pointer */
2281b67be7bSPoul-Henning Kamp 	u_int	fd_iosize;	/* Size of IO chunks */
2291b67be7bSPoul-Henning Kamp 	u_int	fd_iocount;	/* Outstanding requests */
230b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2315b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2321a6bed68SJoerg Wunsch 	enum	fd_drivetype type; /* drive type */
2331a6bed68SJoerg Wunsch 	struct	fd_type *ft;	/* pointer to current type descriptor */
2341b67be7bSPoul-Henning Kamp 	struct	fd_type fts;	/* type descriptors */
2351b67be7bSPoul-Henning Kamp 	int	sectorsize;
2365b81b6b3SRodney W. Grimes 	int	flags;
2371b67be7bSPoul-Henning Kamp #define	FD_WP		(1<<0)	/* Write protected	*/
2381b67be7bSPoul-Henning Kamp #define	FD_MOTOR	(1<<1)	/* motor should be on	*/
2391b67be7bSPoul-Henning Kamp #define	FD_MOTORWAIT	(1<<2)	/* motor should be on	*/
2401b67be7bSPoul-Henning Kamp #define	FD_EMPTY	(1<<3)	/* no media		*/
2411b67be7bSPoul-Henning Kamp #define	FD_NEWDISK	(1<<4)	/* media changed	*/
2421b67be7bSPoul-Henning Kamp #define	FD_ISADMA	(1<<5)	/* isa dma started 	*/
2435b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
2441b67be7bSPoul-Henning Kamp #define FD_NO_TRACK	 -2
2451b67be7bSPoul-Henning Kamp 	int	options;	/* FDOPT_* */
2461b67be7bSPoul-Henning Kamp 	struct	callout toffhandle;
2471b67be7bSPoul-Henning Kamp 	struct	callout tohandle;
2481b67be7bSPoul-Henning Kamp 	struct g_geom *fd_geom;
2491b67be7bSPoul-Henning Kamp 	struct g_provider *fd_provider;
2506182fdbdSPeter Wemm 	device_t dev;
2511b67be7bSPoul-Henning Kamp 	struct bio_queue_head fd_bq;
2526182fdbdSPeter Wemm };
25337286586SPeter Wemm 
2541b67be7bSPoul-Henning Kamp #define FD_NOT_VALID -2
255752d4735SNate Lawson 
256246ed35dSJoerg Wunsch static driver_intr_t fdc_intr;
2571b67be7bSPoul-Henning Kamp static void fdc_reset(struct fdc_data *);
258d66c374fSTor Egge 
2591b67be7bSPoul-Henning Kamp SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
260d66c374fSTor Egge 
2611b67be7bSPoul-Henning Kamp static int fifo_threshold = 8;
2621b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
2631b67be7bSPoul-Henning Kamp 	"FIFO threshold setting");
2641b67be7bSPoul-Henning Kamp 
2651b67be7bSPoul-Henning Kamp static int debugflags = 0;
2661b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
2671b67be7bSPoul-Henning Kamp 	"Debug flags");
2681b67be7bSPoul-Henning Kamp 
2691b67be7bSPoul-Henning Kamp static int retries = 10;
2701b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
2711b67be7bSPoul-Henning Kamp 	"Number of retries to attempt");
2721b67be7bSPoul-Henning Kamp 
2731b67be7bSPoul-Henning Kamp static int spec1 = 0xaf;
2741b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
2751b67be7bSPoul-Henning Kamp 	"Specification byte one (step-rate + head unload)");
2761b67be7bSPoul-Henning Kamp 
2771b67be7bSPoul-Henning Kamp static int spec2 = 0x10;
2781b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
2791b67be7bSPoul-Henning Kamp 	"Specification byte two (head load time + no-dma)");
2801b67be7bSPoul-Henning Kamp 
2811b67be7bSPoul-Henning Kamp static int settle;
2821b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
2831b67be7bSPoul-Henning Kamp 	"Head settling time in sec/hz");
2841b67be7bSPoul-Henning Kamp 
2851b67be7bSPoul-Henning Kamp static void
2861b67be7bSPoul-Henning Kamp fdprinttype(struct fd_type *ft)
2871b67be7bSPoul-Henning Kamp {
2881b67be7bSPoul-Henning Kamp 
2891b67be7bSPoul-Henning Kamp 	printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
2901b67be7bSPoul-Henning Kamp 	    ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
2911b67be7bSPoul-Henning Kamp 	    ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
2921b67be7bSPoul-Henning Kamp 	    ft->offset_side2, ft->flags);
2931b67be7bSPoul-Henning Kamp }
2941b67be7bSPoul-Henning Kamp 
2951b67be7bSPoul-Henning Kamp static void
2961b67be7bSPoul-Henning Kamp fdsettype(struct fd_data *fd, struct fd_type *ft)
2971b67be7bSPoul-Henning Kamp {
2981b67be7bSPoul-Henning Kamp 	fd->ft = ft;
2991b67be7bSPoul-Henning Kamp 	ft->size = ft->sectrac * ft->heads * ft->tracks;
3001b67be7bSPoul-Henning Kamp 	fd->sectorsize = 128 << fd->ft->secsize;
3011b67be7bSPoul-Henning Kamp }
3025b81b6b3SRodney W. Grimes 
303246ed35dSJoerg Wunsch /*
304246ed35dSJoerg Wunsch  * Bus space handling (access to low-level IO).
305246ed35dSJoerg Wunsch  */
3061b67be7bSPoul-Henning Kamp static void
3071b67be7bSPoul-Henning Kamp fdctl_wr(struct fdc_data *fdc, u_int8_t v)
308427ccf00SDoug Rabson {
3091b67be7bSPoul-Henning Kamp 
3101b67be7bSPoul-Henning Kamp 	bus_space_write_1(fdc->ctlt, fdc->ctlh, fdc->ctl_off, v);
3111b67be7bSPoul-Henning Kamp }
3121b67be7bSPoul-Henning Kamp 
3131b67be7bSPoul-Henning Kamp static void
3141b67be7bSPoul-Henning Kamp fdout_wr(struct fdc_data *fdc, u_int8_t v)
3151b67be7bSPoul-Henning Kamp {
3161b67be7bSPoul-Henning Kamp 
317427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
318427ccf00SDoug Rabson }
319427ccf00SDoug Rabson 
320427ccf00SDoug Rabson static u_int8_t
3211b67be7bSPoul-Henning Kamp fdsts_rd(struct fdc_data *fdc)
322427ccf00SDoug Rabson {
3231b67be7bSPoul-Henning Kamp 
324427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
325427ccf00SDoug Rabson }
326427ccf00SDoug Rabson 
327427ccf00SDoug Rabson static void
3281b67be7bSPoul-Henning Kamp fddata_wr(struct fdc_data *fdc, u_int8_t v)
329427ccf00SDoug Rabson {
3301b67be7bSPoul-Henning Kamp 
331427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
332427ccf00SDoug Rabson }
333427ccf00SDoug Rabson 
334427ccf00SDoug Rabson static u_int8_t
3351b67be7bSPoul-Henning Kamp fddata_rd(struct fdc_data *fdc)
336427ccf00SDoug Rabson {
3371b67be7bSPoul-Henning Kamp 
338427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
339427ccf00SDoug Rabson }
340427ccf00SDoug Rabson 
341427ccf00SDoug Rabson static u_int8_t
3421b67be7bSPoul-Henning Kamp fdin_rd(struct fdc_data *fdc)
343427ccf00SDoug Rabson {
3441b67be7bSPoul-Henning Kamp 
3451b67be7bSPoul-Henning Kamp 	return bus_space_read_1(fdc->ctlt, fdc->ctlh, fdc->ctl_off);
346427ccf00SDoug Rabson }
347427ccf00SDoug Rabson 
348b4046cd7SWarner Losh /*
349b4046cd7SWarner Losh  * Magic pseudo-DMA initialization for YE FDC. Sets count and
350b4046cd7SWarner Losh  * direction.
351b4046cd7SWarner Losh  */
352b4046cd7SWarner Losh static void
353b4046cd7SWarner Losh fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
354b4046cd7SWarner Losh {
355b4046cd7SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + FDBCDR,
356b4046cd7SWarner Losh 	    (count - 1) & 0xff);
357b4046cd7SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + FDBCDR + 1,
358b4046cd7SWarner Losh 	  ((iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f)));
359b4046cd7SWarner Losh }
360b4046cd7SWarner Losh 
361dc5df763SJoerg Wunsch static int
3626182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
363dc5df763SJoerg Wunsch {
3646182fdbdSPeter Wemm 	fdc->fdc_errs++;
36516b04b6aSJoerg Wunsch 	if (s) {
366b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
367b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
368b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
369b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
370b6e5f28eSPeter Wemm 						    "logging any more\n");
37116b04b6aSJoerg Wunsch 	}
372dc5df763SJoerg Wunsch 
3731b67be7bSPoul-Henning Kamp 	return (1);
374dc5df763SJoerg Wunsch }
375dc5df763SJoerg Wunsch 
376dc5df763SJoerg Wunsch /*
3771b67be7bSPoul-Henning Kamp  * FDC IO functions, take care of the main status register, timeout
3781b67be7bSPoul-Henning Kamp  * in case the desired status bits are never set.
3791b67be7bSPoul-Henning Kamp  *
3801b67be7bSPoul-Henning Kamp  * These PIO loops initially start out with short delays between
3811b67be7bSPoul-Henning Kamp  * each iteration in the expectation that the required condition
3821b67be7bSPoul-Henning Kamp  * is usually met quickly, so it can be handled immediately.
383dc5df763SJoerg Wunsch  */
3841b67be7bSPoul-Henning Kamp static int
3851b67be7bSPoul-Henning Kamp fdc_in(struct fdc_data *fdc, int *ptr)
386dc5df763SJoerg Wunsch {
3871b67be7bSPoul-Henning Kamp 	int i, j, step;
3881b67be7bSPoul-Henning Kamp 
3891b67be7bSPoul-Henning Kamp 	step = 1;
3901b67be7bSPoul-Henning Kamp 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
3911b67be7bSPoul-Henning Kamp 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
3921b67be7bSPoul-Henning Kamp 	        if (i == (NE7_DIO|NE7_RQM)) {
3931b67be7bSPoul-Henning Kamp 			i = fddata_rd(fdc);
3941b67be7bSPoul-Henning Kamp 			if (ptr)
3951b67be7bSPoul-Henning Kamp 				*ptr = i;
3961b67be7bSPoul-Henning Kamp 			return (0);
3971b67be7bSPoul-Henning Kamp 		}
3981b67be7bSPoul-Henning Kamp 		if (i == NE7_RQM)
3991b67be7bSPoul-Henning Kamp 			return (fdc_err(fdc, "ready for output in input\n"));
4001b67be7bSPoul-Henning Kamp 		step += step;
4011b67be7bSPoul-Henning Kamp 		DELAY(step);
4021b67be7bSPoul-Henning Kamp 	}
4031b67be7bSPoul-Henning Kamp 	return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
4041b67be7bSPoul-Henning Kamp }
4051b67be7bSPoul-Henning Kamp 
4061b67be7bSPoul-Henning Kamp static int
4071b67be7bSPoul-Henning Kamp fdc_out(struct fdc_data *fdc, int x)
4081b67be7bSPoul-Henning Kamp {
4091b67be7bSPoul-Henning Kamp 	int i, j, step;
4101b67be7bSPoul-Henning Kamp 
4111b67be7bSPoul-Henning Kamp 	step = 1;
4121b67be7bSPoul-Henning Kamp 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
4131b67be7bSPoul-Henning Kamp 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
4141b67be7bSPoul-Henning Kamp 	        if (i == NE7_RQM) {
4151b67be7bSPoul-Henning Kamp 			fddata_wr(fdc, x);
4161b67be7bSPoul-Henning Kamp 			return (0);
4171b67be7bSPoul-Henning Kamp 		}
4181b67be7bSPoul-Henning Kamp 		if (i == (NE7_DIO|NE7_RQM))
4191b67be7bSPoul-Henning Kamp 			return (fdc_err(fdc, "ready for input in output\n"));
4201b67be7bSPoul-Henning Kamp 		step += step;
4211b67be7bSPoul-Henning Kamp 		DELAY(step);
4221b67be7bSPoul-Henning Kamp 	}
4231b67be7bSPoul-Henning Kamp 	return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
4241b67be7bSPoul-Henning Kamp }
4251b67be7bSPoul-Henning Kamp 
4261b67be7bSPoul-Henning Kamp /*
4271b67be7bSPoul-Henning Kamp  * fdc_cmd: Send a command to the chip.
4281b67be7bSPoul-Henning Kamp  * Takes a varargs with this structure:
4291b67be7bSPoul-Henning Kamp  *	# of output bytes
4301b67be7bSPoul-Henning Kamp  *	output bytes as int [...]
4311b67be7bSPoul-Henning Kamp  *	# of input bytes
4321b67be7bSPoul-Henning Kamp  *	input bytes as int* [...]
4331b67be7bSPoul-Henning Kamp  */
4341b67be7bSPoul-Henning Kamp static int
4351b67be7bSPoul-Henning Kamp fdc_cmd(struct fdc_data *fdc, int n_out, ...)
4361b67be7bSPoul-Henning Kamp {
4371b67be7bSPoul-Henning Kamp 	u_char cmd = 0;
438dc5df763SJoerg Wunsch 	int n_in;
4391b67be7bSPoul-Henning Kamp 	int n, i;
440dc5df763SJoerg Wunsch 	va_list ap;
441dc5df763SJoerg Wunsch 
442dc5df763SJoerg Wunsch 	va_start(ap, n_out);
4431b67be7bSPoul-Henning Kamp 	for (n = 0; n < n_out; n++) {
4441b67be7bSPoul-Henning Kamp 		i = va_arg(ap, int);
4451b67be7bSPoul-Henning Kamp 		if (n == 0)
4461b67be7bSPoul-Henning Kamp 			cmd = i;
4471b67be7bSPoul-Henning Kamp 		if (fdc_out(fdc, i) < 0) {
448dc5df763SJoerg Wunsch 			char msg[50];
4492127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
450dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
451dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
4521b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
4536182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
454dc5df763SJoerg Wunsch 		}
455dc5df763SJoerg Wunsch 	}
456dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
4571b67be7bSPoul-Henning Kamp 	for (n = 0; n < n_in; n++) {
458dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
4591b67be7bSPoul-Henning Kamp 		if (fdc_in(fdc, ptr) < 0) {
460dc5df763SJoerg Wunsch 			char msg[50];
4612127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
462dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
463dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
4641b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
4656182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
466dc5df763SJoerg Wunsch 		}
467dc5df763SJoerg Wunsch 	}
4681b67be7bSPoul-Henning Kamp 	return (0);
469dc5df763SJoerg Wunsch }
470dc5df763SJoerg Wunsch 
4711b67be7bSPoul-Henning Kamp static void
4721b67be7bSPoul-Henning Kamp fdc_reset(struct fdc_data *fdc)
473d66c374fSTor Egge {
4741b67be7bSPoul-Henning Kamp 	int i, r[10];
475d66c374fSTor Egge 
4761b67be7bSPoul-Henning Kamp 	/* Try a reset, keep motor on */
4771b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
4781b67be7bSPoul-Henning Kamp 	DELAY(100);
4791b67be7bSPoul-Henning Kamp 	/* enable FDC, but defer interrupts a moment */
4801b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
4811b67be7bSPoul-Henning Kamp 	DELAY(100);
4821b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
483d66c374fSTor Egge 
4841b67be7bSPoul-Henning Kamp 	/* XXX after a reset, silently believe the FDC will accept commands */
4851b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
4861b67be7bSPoul-Henning Kamp 		device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
487d66c374fSTor Egge 
4881b67be7bSPoul-Henning Kamp 	if (fdc->fdct == FDC_ENHANCED) {
4891b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 4,
4901b67be7bSPoul-Henning Kamp 		    I8207X_CONFIGURE,
4911b67be7bSPoul-Henning Kamp 		    0,
4921b67be7bSPoul-Henning Kamp 		    0x40 |			/* Enable Implied Seek */
4931b67be7bSPoul-Henning Kamp 		    0x10 |			/* Polling disabled */
4941b67be7bSPoul-Henning Kamp 		    (fifo_threshold - 1),	/* Fifo threshold */
4951b67be7bSPoul-Henning Kamp 		    0x00,			/* Precomp track */
4961b67be7bSPoul-Henning Kamp 		    0))
4971b67be7bSPoul-Henning Kamp 			device_printf(fdc->fdc_dev,
4981b67be7bSPoul-Henning Kamp 			    " CONFIGURE failed in reset\n");
4991b67be7bSPoul-Henning Kamp 		if (debugflags & 1) {
5001b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 1,
5011b67be7bSPoul-Henning Kamp 			    0x0e,			/* DUMPREG */
5021b67be7bSPoul-Henning Kamp 			    10, &r[0], &r[1], &r[2], &r[3], &r[4],
5031b67be7bSPoul-Henning Kamp 			    &r[5], &r[6], &r[7], &r[8], &r[9]))
5041b67be7bSPoul-Henning Kamp 				device_printf(fdc->fdc_dev,
5051b67be7bSPoul-Henning Kamp 				    " DUMPREG failed in reset\n");
5061b67be7bSPoul-Henning Kamp 			for (i = 0; i < 10; i++)
5071b67be7bSPoul-Henning Kamp 				printf(" %02x", r[i]);
5081b67be7bSPoul-Henning Kamp 			printf("\n");
509d66c374fSTor Egge 		}
510d0900d6bSJoerg Wunsch 	}
511d66c374fSTor Egge }
512d66c374fSTor Egge 
513d66c374fSTor Egge static int
5141b67be7bSPoul-Henning Kamp fdc_sense_drive(struct fdc_data *fdc, int *st3p)
515dc5df763SJoerg Wunsch {
516dc5df763SJoerg Wunsch 	int st3;
517dc5df763SJoerg Wunsch 
5181b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
5191b67be7bSPoul-Henning Kamp 		return (fdc_err(fdc, "Sense Drive Status failed\n"));
520dc5df763SJoerg Wunsch 	if (st3p)
521dc5df763SJoerg Wunsch 		*st3p = st3;
5221b67be7bSPoul-Henning Kamp 	return (0);
523dc5df763SJoerg Wunsch }
524dc5df763SJoerg Wunsch 
5256f4e0bebSPoul-Henning Kamp static int
5261b67be7bSPoul-Henning Kamp fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
527dc5df763SJoerg Wunsch {
5286182fdbdSPeter Wemm 	int cyl, st0, ret;
529dc5df763SJoerg Wunsch 
5301b67be7bSPoul-Henning Kamp 	ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
5316182fdbdSPeter Wemm 	if (ret) {
5321b67be7bSPoul-Henning Kamp 		(void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
5331b67be7bSPoul-Henning Kamp 		return (ret);
534dc5df763SJoerg Wunsch 	}
535dc5df763SJoerg Wunsch 
536dc5df763SJoerg Wunsch 	if (st0p)
537dc5df763SJoerg Wunsch 		*st0p = st0;
538dc5df763SJoerg Wunsch 
5396182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
540dc5df763SJoerg Wunsch 		/*
541dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
542dc5df763SJoerg Wunsch 		 */
5431b67be7bSPoul-Henning Kamp 		return (FD_NOT_VALID);
544dc5df763SJoerg Wunsch 	}
545dc5df763SJoerg Wunsch 
5461b67be7bSPoul-Henning Kamp 	if (fdc_in(fdc, &cyl) < 0)
5476182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
548dc5df763SJoerg Wunsch 
549dc5df763SJoerg Wunsch 	if (cylp)
550dc5df763SJoerg Wunsch 		*cylp = cyl;
551dc5df763SJoerg Wunsch 
5521b67be7bSPoul-Henning Kamp 	return (0);
553dc5df763SJoerg Wunsch }
554dc5df763SJoerg Wunsch 
5556f4e0bebSPoul-Henning Kamp static int
5561b67be7bSPoul-Henning Kamp fdc_read_status(struct fdc_data *fdc)
557dc5df763SJoerg Wunsch {
5581b67be7bSPoul-Henning Kamp 	int i, ret, status;
559b5e8ce9fSBruce Evans 
560246ed35dSJoerg Wunsch 	for (i = ret = 0; i < 7; i++) {
5611b67be7bSPoul-Henning Kamp 		ret = fdc_in(fdc, &status);
562b5e8ce9fSBruce Evans 		fdc->status[i] = status;
563b5e8ce9fSBruce Evans 		if (ret != 0)
564dc5df763SJoerg Wunsch 			break;
565dc5df763SJoerg Wunsch 	}
566dc5df763SJoerg Wunsch 
567dc5df763SJoerg Wunsch 	if (ret == 0)
568dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
569dc5df763SJoerg Wunsch 	else
570dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
571dc5df763SJoerg Wunsch 
572dc5df763SJoerg Wunsch 	return ret;
573dc5df763SJoerg Wunsch }
574dc5df763SJoerg Wunsch 
5751b67be7bSPoul-Henning Kamp /*
5761b67be7bSPoul-Henning Kamp  * Select this drive
5771b67be7bSPoul-Henning Kamp  */
5781b67be7bSPoul-Henning Kamp static void
5791b67be7bSPoul-Henning Kamp fd_select(struct fd_data *fd)
5801b67be7bSPoul-Henning Kamp {
5811b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
5821b67be7bSPoul-Henning Kamp 
5831b67be7bSPoul-Henning Kamp 	/* XXX: lock controller */
5841b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
5851b67be7bSPoul-Henning Kamp 	fdc->fdout &= ~FDO_FDSEL;
5861b67be7bSPoul-Henning Kamp 	fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
5871b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
5881b67be7bSPoul-Henning Kamp }
5891b67be7bSPoul-Henning Kamp 
5901b67be7bSPoul-Henning Kamp static void
5911b67be7bSPoul-Henning Kamp fd_turnon(void *arg)
5921b67be7bSPoul-Henning Kamp {
5931b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
5941b67be7bSPoul-Henning Kamp 	struct bio *bp;
5951b67be7bSPoul-Henning Kamp 	int once;
5961b67be7bSPoul-Henning Kamp 
5971b67be7bSPoul-Henning Kamp 	fd = arg;
5981b67be7bSPoul-Henning Kamp 	mtx_lock(&fd->fdc->fdc_mtx);
5991b67be7bSPoul-Henning Kamp 	fd->flags &= ~FD_MOTORWAIT;
6001b67be7bSPoul-Henning Kamp 	fd->flags |= FD_MOTOR;
6011b67be7bSPoul-Henning Kamp 	once = 0;
6021b67be7bSPoul-Henning Kamp 	for (;;) {
6031b67be7bSPoul-Henning Kamp 		bp = bioq_takefirst(&fd->fd_bq);
6041b67be7bSPoul-Henning Kamp 		if (bp == NULL)
6051b67be7bSPoul-Henning Kamp 			break;
6061b67be7bSPoul-Henning Kamp 		bioq_disksort(&fd->fdc->head, bp);
6071b67be7bSPoul-Henning Kamp 		once = 1;
6081b67be7bSPoul-Henning Kamp 	}
6091b67be7bSPoul-Henning Kamp 	mtx_unlock(&fd->fdc->fdc_mtx);
6101b67be7bSPoul-Henning Kamp 	if (once)
6111b67be7bSPoul-Henning Kamp 		wakeup(&fd->fdc->head);
6121b67be7bSPoul-Henning Kamp }
6131b67be7bSPoul-Henning Kamp 
6141b67be7bSPoul-Henning Kamp static void
6151b67be7bSPoul-Henning Kamp fd_motor(struct fd_data *fd, int turnon)
6161b67be7bSPoul-Henning Kamp {
6171b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
6181b67be7bSPoul-Henning Kamp 
6191b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
6201b67be7bSPoul-Henning Kamp /*
6211b67be7bSPoul-Henning Kamp 	mtx_assert(&fdc->fdc_mtx, MA_OWNED);
6221b67be7bSPoul-Henning Kamp */
6231b67be7bSPoul-Henning Kamp 	if (turnon) {
6241b67be7bSPoul-Henning Kamp 		fd->flags |= FD_MOTORWAIT;
6251b67be7bSPoul-Henning Kamp 		fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
6261b67be7bSPoul-Henning Kamp 		callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
6271b67be7bSPoul-Henning Kamp 	} else {
6281b67be7bSPoul-Henning Kamp 		callout_drain(&fd->toffhandle);
6291b67be7bSPoul-Henning Kamp 		fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
6301b67be7bSPoul-Henning Kamp 		fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
6311b67be7bSPoul-Henning Kamp 	}
6321b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
6331b67be7bSPoul-Henning Kamp }
6341b67be7bSPoul-Henning Kamp 
6351b67be7bSPoul-Henning Kamp static void
6361b67be7bSPoul-Henning Kamp fd_turnoff(void *xfd)
6371b67be7bSPoul-Henning Kamp {
6381b67be7bSPoul-Henning Kamp 	struct fd_data *fd = xfd;
6391b67be7bSPoul-Henning Kamp 
6401b67be7bSPoul-Henning Kamp 	mtx_lock(&fd->fdc->fdc_mtx);
6411b67be7bSPoul-Henning Kamp 	fd_motor(fd, 0);
6421b67be7bSPoul-Henning Kamp 	mtx_unlock(&fd->fdc->fdc_mtx);
6431b67be7bSPoul-Henning Kamp }
6441b67be7bSPoul-Henning Kamp 
6451b67be7bSPoul-Henning Kamp /*
64661909e4cSPoul-Henning Kamp  * fdc_intr - wake up the worker thread.
6471b67be7bSPoul-Henning Kamp  */
64861909e4cSPoul-Henning Kamp 
6491b67be7bSPoul-Henning Kamp static void
6501b67be7bSPoul-Henning Kamp fdc_intr(void *arg)
6511b67be7bSPoul-Henning Kamp {
6521b67be7bSPoul-Henning Kamp 
6531b67be7bSPoul-Henning Kamp 	wakeup(arg);
6541b67be7bSPoul-Henning Kamp }
6551b67be7bSPoul-Henning Kamp 
6561b67be7bSPoul-Henning Kamp /*
6571b67be7bSPoul-Henning Kamp  * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
6581b67be7bSPoul-Henning Kamp  */
6591b67be7bSPoul-Henning Kamp static void
6601b67be7bSPoul-Henning Kamp fdc_pio(struct fdc_data *fdc)
6611b67be7bSPoul-Henning Kamp {
6621b67be7bSPoul-Henning Kamp 	u_char *cptr;
6631b67be7bSPoul-Henning Kamp 	struct bio *bp;
6641b67be7bSPoul-Henning Kamp 	u_int count;
6651b67be7bSPoul-Henning Kamp 
6661b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
6671b67be7bSPoul-Henning Kamp 	cptr = fdc->fd->fd_ioptr;
6681b67be7bSPoul-Henning Kamp 	count = fdc->fd->fd_iosize;
6691b67be7bSPoul-Henning Kamp 
6701b67be7bSPoul-Henning Kamp 	if (bp->bio_cmd == BIO_READ) {
671b4046cd7SWarner Losh 		fdbcdr_wr(fdc, 0, count);
6721b67be7bSPoul-Henning Kamp 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
673b4046cd7SWarner Losh 		    FD_YE_DATAPORT, cptr, count);
6741b67be7bSPoul-Henning Kamp 	} else {
6751b67be7bSPoul-Henning Kamp 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
676b4046cd7SWarner Losh 		    FD_YE_DATAPORT, cptr, count);
677b4046cd7SWarner Losh 		fdbcdr_wr(fdc, 0, count);	/* needed? */
6781b67be7bSPoul-Henning Kamp 	}
6791b67be7bSPoul-Henning Kamp }
6801b67be7bSPoul-Henning Kamp 
6811b67be7bSPoul-Henning Kamp static int
6821b67be7bSPoul-Henning Kamp fdc_biodone(struct fdc_data *fdc, int error)
6831b67be7bSPoul-Henning Kamp {
6841b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
6851b67be7bSPoul-Henning Kamp 	struct bio *bp;
6861b67be7bSPoul-Henning Kamp 
6871b67be7bSPoul-Henning Kamp 	fd = fdc->fd;
6881b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
6891b67be7bSPoul-Henning Kamp 
6901b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
6911b67be7bSPoul-Henning Kamp 	if (--fd->fd_iocount == 0)
6921b67be7bSPoul-Henning Kamp 		callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
6931b67be7bSPoul-Henning Kamp 	fdc->bp = NULL;
6941b67be7bSPoul-Henning Kamp 	fdc->fd = NULL;
6951b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
6961b67be7bSPoul-Henning Kamp 	if (bp->bio_to != NULL) {
6971b67be7bSPoul-Henning Kamp 		if ((debugflags & 2) && fd->fdc->retry > 0)
6981b67be7bSPoul-Henning Kamp 			printf("retries: %d\n", fd->fdc->retry);
6991b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, error);
7001b67be7bSPoul-Henning Kamp 		return (0);
7011b67be7bSPoul-Henning Kamp 	}
7021b67be7bSPoul-Henning Kamp 	bp->bio_error = error;
7031b67be7bSPoul-Henning Kamp 	bp->bio_flags |= BIO_DONE;
7041b67be7bSPoul-Henning Kamp 	wakeup(bp);
7051b67be7bSPoul-Henning Kamp 	return (0);
7061b67be7bSPoul-Henning Kamp }
7071b67be7bSPoul-Henning Kamp 
7081b67be7bSPoul-Henning Kamp static int retry_line;
7091b67be7bSPoul-Henning Kamp 
7101b67be7bSPoul-Henning Kamp static int
7111b67be7bSPoul-Henning Kamp fdc_worker(struct fdc_data *fdc)
7121b67be7bSPoul-Henning Kamp {
7131b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
7141b67be7bSPoul-Henning Kamp 	struct bio *bp;
7151b67be7bSPoul-Henning Kamp 	int i, nsect;
7161b67be7bSPoul-Henning Kamp 	int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
7171b67be7bSPoul-Henning Kamp 	int head;
7181b67be7bSPoul-Henning Kamp 	static int need_recal;
7191b67be7bSPoul-Henning Kamp 	struct fdc_readid *idp;
7201b67be7bSPoul-Henning Kamp 	struct fd_formb *finfo;
7211b67be7bSPoul-Henning Kamp 
7221b67be7bSPoul-Henning Kamp 	/* Have we exhausted our retries ? */
7231b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
7241b67be7bSPoul-Henning Kamp 	fd = fdc->fd;
7251b67be7bSPoul-Henning Kamp 	if (bp != NULL &&
7261b67be7bSPoul-Henning Kamp 		(fdc->retry >= retries || (fd->flags & FDOPT_NORETRY))) {
7271b67be7bSPoul-Henning Kamp 		if ((debugflags & 4))
7281b67be7bSPoul-Henning Kamp 			printf("Too many retries (EIO)\n");
7291b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, EIO));
7301b67be7bSPoul-Henning Kamp 	}
7311b67be7bSPoul-Henning Kamp 
7321b67be7bSPoul-Henning Kamp 	/* Disable ISADMA if we bailed while it was active */
7331b67be7bSPoul-Henning Kamp 	if (fd != NULL && (fd->flags & FD_ISADMA)) {
7341b67be7bSPoul-Henning Kamp 		mtx_lock(&Giant);
7351b67be7bSPoul-Henning Kamp 		isa_dmadone(
7361b67be7bSPoul-Henning Kamp 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
7371b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
7381b67be7bSPoul-Henning Kamp 		mtx_unlock(&Giant);
73961909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
7401b67be7bSPoul-Henning Kamp 		fd->flags &= ~FD_ISADMA;
74161909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
7421b67be7bSPoul-Henning Kamp 	}
7431b67be7bSPoul-Henning Kamp 
7441b67be7bSPoul-Henning Kamp 	/* Unwedge the controller ? */
7451b67be7bSPoul-Henning Kamp 	if (fdc->flags & FDC_NEEDS_RESET) {
7461b67be7bSPoul-Henning Kamp 		fdc->flags &= ~FDC_NEEDS_RESET;
7471b67be7bSPoul-Henning Kamp 		fdc_reset(fdc);
7481b67be7bSPoul-Henning Kamp 		msleep(fdc, NULL, PRIBIO, "fdcrst", hz);
7491b67be7bSPoul-Henning Kamp 		/* Discard results */
7501b67be7bSPoul-Henning Kamp 		for (i = 0; i < 4; i++)
7511b67be7bSPoul-Henning Kamp 			fdc_sense_int(fdc, &st0, &cyl);
7521b67be7bSPoul-Henning Kamp 		/* All drives must recal */
7531b67be7bSPoul-Henning Kamp 		need_recal = 0xf;
7541b67be7bSPoul-Henning Kamp 	}
7551b67be7bSPoul-Henning Kamp 
7561b67be7bSPoul-Henning Kamp 	/* Pick up a request, if need be wait for it */
7571b67be7bSPoul-Henning Kamp 	if (fdc->bp == NULL) {
7581b67be7bSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
7591b67be7bSPoul-Henning Kamp 		do {
7601b67be7bSPoul-Henning Kamp 			fdc->bp = bioq_takefirst(&fdc->head);
7611b67be7bSPoul-Henning Kamp 			if (fdc->bp == NULL)
7621b67be7bSPoul-Henning Kamp 				msleep(&fdc->head, &fdc->fdc_mtx,
7631b67be7bSPoul-Henning Kamp 				    PRIBIO, "-", hz);
76402910eeeSJoerg Wunsch 		} while (fdc->bp == NULL &&
76502910eeeSJoerg Wunsch 		    (fdc->flags & FDC_KTHREAD_EXIT) == 0);
7661b67be7bSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
76702910eeeSJoerg Wunsch 
76802910eeeSJoerg Wunsch 		if (fdc->bp == NULL)
76902910eeeSJoerg Wunsch 			/*
77002910eeeSJoerg Wunsch 			 * Nothing to do, worker thread has been
77102910eeeSJoerg Wunsch 			 * requested to stop.
77202910eeeSJoerg Wunsch 			 */
77302910eeeSJoerg Wunsch 			return (0);
77402910eeeSJoerg Wunsch 
7751b67be7bSPoul-Henning Kamp 		bp = fdc->bp;
7761b67be7bSPoul-Henning Kamp 		fd = fdc->fd = bp->bio_driver1;
7771b67be7bSPoul-Henning Kamp 		fdc->retry = 0;
7781b67be7bSPoul-Henning Kamp 		fd->fd_ioptr = bp->bio_data;
7791b67be7bSPoul-Henning Kamp 		if (bp->bio_cmd & BIO_FMT) {
7801b67be7bSPoul-Henning Kamp 			i = offsetof(struct fd_formb, fd_formb_cylno(0));
7811b67be7bSPoul-Henning Kamp 			fd->fd_ioptr += i;
7821b67be7bSPoul-Henning Kamp 			fd->fd_iosize = bp->bio_length - i;
7831b67be7bSPoul-Henning Kamp 		}
7841b67be7bSPoul-Henning Kamp 	}
7851b67be7bSPoul-Henning Kamp 
7861b67be7bSPoul-Henning Kamp 	/* Select drive, setup params */
7871b67be7bSPoul-Henning Kamp 	fd_select(fd);
7881b67be7bSPoul-Henning Kamp 	fdctl_wr(fdc, fd->ft->trans);
7891b67be7bSPoul-Henning Kamp 
7901b67be7bSPoul-Henning Kamp 	if (bp->bio_cmd & BIO_PROBE) {
7911b67be7bSPoul-Henning Kamp 
7921b67be7bSPoul-Henning Kamp 		if (!(fdin_rd(fdc) & FDI_DCHG) && !(fd->flags & FD_EMPTY))
7931b67be7bSPoul-Henning Kamp 			return (fdc_biodone(fdc, 0));
7941b67be7bSPoul-Henning Kamp 
7951b67be7bSPoul-Henning Kamp 		/*
7961b67be7bSPoul-Henning Kamp 		 * Try to find out if we have a disk in the drive
7971b67be7bSPoul-Henning Kamp 		 *
7981b67be7bSPoul-Henning Kamp 		 * First recal, then seek to cyl#1, this clears the
7991b67be7bSPoul-Henning Kamp 		 * old condition on the disk change line so we can
8001b67be7bSPoul-Henning Kamp 		 * examine it for current status
8011b67be7bSPoul-Henning Kamp 		 */
8021b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
8031b67be7bSPoul-Henning Kamp 			printf("New disk in probe\n");
80461909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
8051b67be7bSPoul-Henning Kamp 		fd->flags |= FD_NEWDISK;
80661909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
8071b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8081b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
8091b67be7bSPoul-Henning Kamp 			return (1);
8101b67be7bSPoul-Henning Kamp 		msleep(fdc, NULL, PRIBIO, "fdrecal", hz);
8111b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8121b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
8131b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
8141b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8151b67be7bSPoul-Henning Kamp 		if ((st0 & 0xc0) || cyl != 0)
8161b67be7bSPoul-Henning Kamp 			return (1);
8171b67be7bSPoul-Henning Kamp 
8181b67be7bSPoul-Henning Kamp 		/* Seek to track 1 */
8191b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8201b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
8211b67be7bSPoul-Henning Kamp 			return (1);
8221b67be7bSPoul-Henning Kamp 		msleep(fdc, NULL, PRIBIO, "fdseek", hz);
8231b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8241b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
8251b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
8261b67be7bSPoul-Henning Kamp 		need_recal |= (1 << fd->fdsu);
8271b67be7bSPoul-Henning Kamp 		if (fdin_rd(fdc) & FDI_DCHG) {
8281b67be7bSPoul-Henning Kamp 			if (debugflags & 0x40)
8291b67be7bSPoul-Henning Kamp 				printf("Empty in probe\n");
83061909e4cSPoul-Henning Kamp 			mtx_lock(&fdc->fdc_mtx);
8311b67be7bSPoul-Henning Kamp 			fd->flags |= FD_EMPTY;
83261909e4cSPoul-Henning Kamp 			mtx_unlock(&fdc->fdc_mtx);
8331b67be7bSPoul-Henning Kamp 		} else {
8341b67be7bSPoul-Henning Kamp 			if (debugflags & 0x40)
8351b67be7bSPoul-Henning Kamp 				printf("Got disk in probe\n");
83661909e4cSPoul-Henning Kamp 			mtx_lock(&fdc->fdc_mtx);
8371b67be7bSPoul-Henning Kamp 			fd->flags &= ~FD_EMPTY;
83861909e4cSPoul-Henning Kamp 			mtx_unlock(&fdc->fdc_mtx);
8391b67be7bSPoul-Henning Kamp 			retry_line = __LINE__;
8401b67be7bSPoul-Henning Kamp 			if(fdc_sense_drive(fdc, &st3) != 0)
8411b67be7bSPoul-Henning Kamp 				return (1);
84261909e4cSPoul-Henning Kamp 			mtx_lock(&fdc->fdc_mtx);
8431b67be7bSPoul-Henning Kamp 			if(st3 & NE7_ST3_WP)
8441b67be7bSPoul-Henning Kamp 				fd->flags |= FD_WP;
8451b67be7bSPoul-Henning Kamp 			else
8461b67be7bSPoul-Henning Kamp 				fd->flags &= ~FD_WP;
84761909e4cSPoul-Henning Kamp 			mtx_unlock(&fdc->fdc_mtx);
8481b67be7bSPoul-Henning Kamp 		}
8491b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, 0));
8501b67be7bSPoul-Henning Kamp 	}
8511b67be7bSPoul-Henning Kamp 
8521b67be7bSPoul-Henning Kamp 	/*
8531b67be7bSPoul-Henning Kamp 	 * If we are dead just flush the requests
8541b67be7bSPoul-Henning Kamp 	 */
8551b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_EMPTY)
8561b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, ENXIO));
8571b67be7bSPoul-Henning Kamp 
8581b67be7bSPoul-Henning Kamp 	/* Check if we lost our media */
8591b67be7bSPoul-Henning Kamp 	if (fdin_rd(fdc) & FDI_DCHG) {
8601b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
8611b67be7bSPoul-Henning Kamp 			printf("Lost disk\n");
86261909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
8631b67be7bSPoul-Henning Kamp 		fd->flags |= FD_EMPTY;
8641b67be7bSPoul-Henning Kamp 		fd->flags |= FD_NEWDISK;
86561909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
8661b67be7bSPoul-Henning Kamp 		g_topology_lock();
8671b67be7bSPoul-Henning Kamp 		g_orphan_provider(fd->fd_provider, EXDEV);
8681b67be7bSPoul-Henning Kamp 		fd->fd_provider->flags |= G_PF_WITHER;
8691b67be7bSPoul-Henning Kamp 		fd->fd_provider =
8701b67be7bSPoul-Henning Kamp 		    g_new_providerf(fd->fd_geom, fd->fd_geom->name);
8711b67be7bSPoul-Henning Kamp 		g_error_provider(fd->fd_provider, 0);
8721b67be7bSPoul-Henning Kamp 		g_topology_unlock();
8731b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, ENXIO));
8741b67be7bSPoul-Henning Kamp 	}
8751b67be7bSPoul-Henning Kamp 
8761b67be7bSPoul-Henning Kamp 	/* Check if the floppy is write-protected */
8771b67be7bSPoul-Henning Kamp 	if(bp->bio_cmd & (BIO_FMT | BIO_WRITE)) {
8781b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8791b67be7bSPoul-Henning Kamp 		if(fdc_sense_drive(fdc, &st3) != 0)
8801b67be7bSPoul-Henning Kamp 			return (1);
8811b67be7bSPoul-Henning Kamp 		if(st3 & NE7_ST3_WP)
8821b67be7bSPoul-Henning Kamp 			return (fdc_biodone(fdc, EROFS));
8831b67be7bSPoul-Henning Kamp 	}
8841b67be7bSPoul-Henning Kamp 
8851b67be7bSPoul-Henning Kamp 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
8861b67be7bSPoul-Henning Kamp 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
8871b67be7bSPoul-Henning Kamp 	i = fd->ft->sectrac * fd->ft->heads;
8881b67be7bSPoul-Henning Kamp 	cylinder = bp->bio_pblkno / i;
8891b67be7bSPoul-Henning Kamp 	descyl = cylinder * steptrac;
8901b67be7bSPoul-Henning Kamp 	sec = bp->bio_pblkno % i;
8911b67be7bSPoul-Henning Kamp 	nsect = i - sec;
8921b67be7bSPoul-Henning Kamp 	head = sec / fd->ft->sectrac;
8931b67be7bSPoul-Henning Kamp 	sec = sec % fd->ft->sectrac + 1;
8941b67be7bSPoul-Henning Kamp 
8951b67be7bSPoul-Henning Kamp 	/* If everything is going swimmingly, use multisector xfer */
8961b67be7bSPoul-Henning Kamp 	if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
8971b67be7bSPoul-Henning Kamp 		fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
8981b67be7bSPoul-Henning Kamp 		nsect = fd->fd_iosize / fd->sectorsize;
8991b67be7bSPoul-Henning Kamp 	} else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
9001b67be7bSPoul-Henning Kamp 		fd->fd_iosize = fd->sectorsize;
9011b67be7bSPoul-Henning Kamp 		nsect = 1;
9021b67be7bSPoul-Henning Kamp 	}
9031b67be7bSPoul-Henning Kamp 
9041b67be7bSPoul-Henning Kamp 	/* Do RECAL if we need to or are going to track zero anyway */
9051b67be7bSPoul-Henning Kamp 	if ((need_recal & (1 << fd->fdsu)) ||
9061b67be7bSPoul-Henning Kamp 	    (cylinder == 0 && fd->track != 0) ||
9071b67be7bSPoul-Henning Kamp 	    fdc->retry > 2) {
9081b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9091b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
9101b67be7bSPoul-Henning Kamp 			return (1);
9111b67be7bSPoul-Henning Kamp 		msleep(fdc, NULL, PRIBIO, "fdrecal", hz);
9121b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9131b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
9141b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
9151b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9161b67be7bSPoul-Henning Kamp 		if ((st0 & 0xc0) || cyl != 0)
9171b67be7bSPoul-Henning Kamp 			return (1);
9181b67be7bSPoul-Henning Kamp 		need_recal &= ~(1 << fd->fdsu);
9191b67be7bSPoul-Henning Kamp 		fd->track = 0;
9201b67be7bSPoul-Henning Kamp 		/* let the heads settle */
9211b67be7bSPoul-Henning Kamp 		if (settle)
9221b67be7bSPoul-Henning Kamp 			msleep(fdc->fd, NULL, PRIBIO, "fdhdstl", settle);
9231b67be7bSPoul-Henning Kamp 	}
9241b67be7bSPoul-Henning Kamp 
9251b67be7bSPoul-Henning Kamp 	/*
9261b67be7bSPoul-Henning Kamp 	 * SEEK to where we want to be
9271b67be7bSPoul-Henning Kamp 	 *
9281b67be7bSPoul-Henning Kamp 	 * Enhanced controllers do implied seeks for read&write as long as
9291b67be7bSPoul-Henning Kamp 	 * we do not need multiple steps per track.
9301b67be7bSPoul-Henning Kamp 	 */
9311b67be7bSPoul-Henning Kamp 	if (cylinder != fd->track && (
9321b67be7bSPoul-Henning Kamp 	    fdc->fdct != FDC_ENHANCED ||
9331b67be7bSPoul-Henning Kamp 	    descyl != cylinder ||
9341b67be7bSPoul-Henning Kamp 	    (bp->bio_cmd & (BIO_RDID|BIO_FMT)))) {
9351b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9361b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
9371b67be7bSPoul-Henning Kamp 			return (1);
9381b67be7bSPoul-Henning Kamp 		msleep(fdc, NULL, PRIBIO, "fdseek", hz);
9391b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9401b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
9411b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
9421b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9431b67be7bSPoul-Henning Kamp 		if ((st0 & 0xc0) || cyl != descyl) {
9441b67be7bSPoul-Henning Kamp 			need_recal |= (1 << fd->fdsu);
9451b67be7bSPoul-Henning Kamp 			return (1);
9461b67be7bSPoul-Henning Kamp 		}
9471b67be7bSPoul-Henning Kamp 		/* let the heads settle */
9481b67be7bSPoul-Henning Kamp 		if (settle)
9491b67be7bSPoul-Henning Kamp 			msleep(fdc->fd, NULL, PRIBIO, "fdhdstl", settle);
9501b67be7bSPoul-Henning Kamp 	}
9511b67be7bSPoul-Henning Kamp 	fd->track = cylinder;
9521b67be7bSPoul-Henning Kamp 
9531b67be7bSPoul-Henning Kamp 	if (debugflags & 8)
9541b67be7bSPoul-Henning Kamp 		printf("op %x bn %ju siz %u ptr %p retry %d\n",
9551b67be7bSPoul-Henning Kamp 		    bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
9561b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fdc->retry);
9571b67be7bSPoul-Henning Kamp 
9581b67be7bSPoul-Henning Kamp 	/* Setup ISADMA if we need it and have it */
9591b67be7bSPoul-Henning Kamp 	if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
9601b67be7bSPoul-Henning Kamp 	     && !(fdc->flags & FDC_NODMA)) {
9611b67be7bSPoul-Henning Kamp 		mtx_lock(&Giant);
9621b67be7bSPoul-Henning Kamp 		isa_dmastart(
9631b67be7bSPoul-Henning Kamp 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
9641b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
9651b67be7bSPoul-Henning Kamp 		mtx_unlock(&Giant);
96661909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
9671b67be7bSPoul-Henning Kamp 		fd->flags |= FD_ISADMA;
96861909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
9691b67be7bSPoul-Henning Kamp 	}
9701b67be7bSPoul-Henning Kamp 
9711b67be7bSPoul-Henning Kamp 	/* Do PIO if we have to */
9721b67be7bSPoul-Henning Kamp 	if (fdc->flags & FDC_NODMA) {
9731b67be7bSPoul-Henning Kamp 		if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
974b4046cd7SWarner Losh 			fdbcdr_wr(fdc, 1, fd->fd_iosize);
9751b67be7bSPoul-Henning Kamp 		if (bp->bio_cmd & (BIO_WRITE|BIO_FMT))
9761b67be7bSPoul-Henning Kamp 			fdc_pio(fdc);
9771b67be7bSPoul-Henning Kamp 	}
9781b67be7bSPoul-Henning Kamp 
9791b67be7bSPoul-Henning Kamp 	switch(bp->bio_cmd) {
9801b67be7bSPoul-Henning Kamp 	case BIO_FMT:
9811b67be7bSPoul-Henning Kamp 		/* formatting */
9821b67be7bSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
9831b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9841b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 6,
9851b67be7bSPoul-Henning Kamp 		    NE7CMD_FORMAT | mfm,
9861b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,
9871b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_secshift,
9881b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_nsecs,
9891b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_gaplen,
9901b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_fillbyte, 0))
9911b67be7bSPoul-Henning Kamp 			return (1);
9921b67be7bSPoul-Henning Kamp 		break;
9931b67be7bSPoul-Henning Kamp 	case BIO_RDID:
9941b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9951b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 2,
9961b67be7bSPoul-Henning Kamp 		    NE7CMD_READID | mfm,
9971b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu, 0))
9981b67be7bSPoul-Henning Kamp 			return (1);
9991b67be7bSPoul-Henning Kamp 		break;
10001b67be7bSPoul-Henning Kamp 	case BIO_READ:
10011b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10021b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 9,
10031b67be7bSPoul-Henning Kamp 		    NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
10041b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,	/* head & unit */
10051b67be7bSPoul-Henning Kamp 		    fd->track,			/* track */
10061b67be7bSPoul-Henning Kamp 		    head,			/* head */
10071b67be7bSPoul-Henning Kamp 		    sec,			/* sector + 1 */
10081b67be7bSPoul-Henning Kamp 		    fd->ft->secsize,		/* sector size */
10091b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac,		/* sectors/track */
10101b67be7bSPoul-Henning Kamp 		    fd->ft->gap,		/* gap size */
10111b67be7bSPoul-Henning Kamp 		    fd->ft->datalen,		/* data length */
10121b67be7bSPoul-Henning Kamp 		    0))
10131b67be7bSPoul-Henning Kamp 			return (1);
10141b67be7bSPoul-Henning Kamp 		break;
10151b67be7bSPoul-Henning Kamp 	case BIO_WRITE:
10161b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10171b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 9,
10181b67be7bSPoul-Henning Kamp 		    NE7CMD_WRITE | mfm | NE7CMD_MT,
10191b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,	/* head & unit */
10201b67be7bSPoul-Henning Kamp 		    fd->track,			/* track */
10211b67be7bSPoul-Henning Kamp 		    head,			/* head */
10221b67be7bSPoul-Henning Kamp 		    sec,			/* sector + 1 */
10231b67be7bSPoul-Henning Kamp 		    fd->ft->secsize,		/* sector size */
10241b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac,		/* sectors/track */
10251b67be7bSPoul-Henning Kamp 		    fd->ft->gap,		/* gap size */
10261b67be7bSPoul-Henning Kamp 		    fd->ft->datalen,		/* data length */
10271b67be7bSPoul-Henning Kamp 		    0))
10281b67be7bSPoul-Henning Kamp 			return (1);
10291b67be7bSPoul-Henning Kamp 		break;
10301b67be7bSPoul-Henning Kamp 	default:
10311b67be7bSPoul-Henning Kamp 		KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
10321b67be7bSPoul-Henning Kamp 	}
10331b67be7bSPoul-Henning Kamp 
10341b67be7bSPoul-Henning Kamp 	/* Wait for interrupt */
10351b67be7bSPoul-Henning Kamp 	i = msleep(fdc, NULL, PRIBIO, "fddata", hz);
10361b67be7bSPoul-Henning Kamp 
10371b67be7bSPoul-Henning Kamp 	/* PIO if the read looks good */
10381b67be7bSPoul-Henning Kamp 	if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ))
10391b67be7bSPoul-Henning Kamp 		fdc_pio(fdc);
10401b67be7bSPoul-Henning Kamp 
10411b67be7bSPoul-Henning Kamp 	/* Finish DMA */
10421b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_ISADMA) {
10431b67be7bSPoul-Henning Kamp 		mtx_lock(&Giant);
10441b67be7bSPoul-Henning Kamp 		isa_dmadone(
10451b67be7bSPoul-Henning Kamp 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
10461b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
10471b67be7bSPoul-Henning Kamp 		mtx_unlock(&Giant);
104861909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
10491b67be7bSPoul-Henning Kamp 		fd->flags &= ~FD_ISADMA;
105061909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
10511b67be7bSPoul-Henning Kamp 	}
10521b67be7bSPoul-Henning Kamp 
10531b67be7bSPoul-Henning Kamp 	if (i != 0) {
10541b67be7bSPoul-Henning Kamp 		/*
10551b67be7bSPoul-Henning Kamp 		 * Timeout.
10561b67be7bSPoul-Henning Kamp 		 *
10571b67be7bSPoul-Henning Kamp 		 * Due to IBM's brain-dead design, the FDC has a faked ready
10581b67be7bSPoul-Henning Kamp 		 * signal, hardwired to ready == true. Thus, any command
10591b67be7bSPoul-Henning Kamp 		 * issued if there's no diskette in the drive will _never_
10601b67be7bSPoul-Henning Kamp 		 * complete, and must be aborted by resetting the FDC.
10611b67be7bSPoul-Henning Kamp 		 * Many thanks, Big Blue!
10621b67be7bSPoul-Henning Kamp 		 */
10631b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10641b67be7bSPoul-Henning Kamp 		fdc->flags |= FDC_NEEDS_RESET;
10651b67be7bSPoul-Henning Kamp 		return (1);
10661b67be7bSPoul-Henning Kamp 	}
10671b67be7bSPoul-Henning Kamp 
10681b67be7bSPoul-Henning Kamp 	retry_line = __LINE__;
10691b67be7bSPoul-Henning Kamp 	if (fdc_read_status(fdc))
10701b67be7bSPoul-Henning Kamp 		return (1);
10711b67be7bSPoul-Henning Kamp 
10721b67be7bSPoul-Henning Kamp 	if (debugflags & 0x10)
10731b67be7bSPoul-Henning Kamp 		printf("  -> %x %x %x %x\n",
10741b67be7bSPoul-Henning Kamp 		    fdc->status[0], fdc->status[1],
10751b67be7bSPoul-Henning Kamp 		    fdc->status[2], fdc->status[3]);
10761b67be7bSPoul-Henning Kamp 
10771b67be7bSPoul-Henning Kamp 	st0 = fdc->status[0] & NE7_ST0_IC;
10781b67be7bSPoul-Henning Kamp 	if (st0 != 0) {
10791b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10801b67be7bSPoul-Henning Kamp 		if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
10811b67be7bSPoul-Henning Kamp 			/*
10821b67be7bSPoul-Henning Kamp 			 * DMA overrun. Someone hogged the bus and
10831b67be7bSPoul-Henning Kamp 			 * didn't release it in time for the next
10841b67be7bSPoul-Henning Kamp 			 * FDC transfer.
10851b67be7bSPoul-Henning Kamp 			 */
10861b67be7bSPoul-Henning Kamp 			return (1);
10871b67be7bSPoul-Henning Kamp 		}
10881b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10891b67be7bSPoul-Henning Kamp 		if(st0 == NE7_ST0_IC_IV) {
10901b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
10911b67be7bSPoul-Henning Kamp 			return (1);
10921b67be7bSPoul-Henning Kamp 		}
10931b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10941b67be7bSPoul-Henning Kamp 		if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
10951b67be7bSPoul-Henning Kamp 			need_recal |= (1 << fd->fdsu);
10961b67be7bSPoul-Henning Kamp 			return (1);
10971b67be7bSPoul-Henning Kamp 		}
10981b67be7bSPoul-Henning Kamp 		if (debugflags & 0x20) {
10991b67be7bSPoul-Henning Kamp 			printf("status %02x %02x %02x %02x %02x %02x\n",
11001b67be7bSPoul-Henning Kamp 			    fdc->status[0], fdc->status[1], fdc->status[2],
11011b67be7bSPoul-Henning Kamp 			    fdc->status[3], fdc->status[4], fdc->status[5]);
11021b67be7bSPoul-Henning Kamp 		}
11031b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11041b67be7bSPoul-Henning Kamp 		return (1);
11051b67be7bSPoul-Henning Kamp 	}
11061b67be7bSPoul-Henning Kamp 	/* All OK */
11071b67be7bSPoul-Henning Kamp 	switch(bp->bio_cmd) {
11081b67be7bSPoul-Henning Kamp 	case BIO_RDID:
11091b67be7bSPoul-Henning Kamp 		/* copy out ID field contents */
11101b67be7bSPoul-Henning Kamp 		idp = (struct fdc_readid *)bp->bio_data;
11111b67be7bSPoul-Henning Kamp 		idp->cyl = fdc->status[3];
11121b67be7bSPoul-Henning Kamp 		idp->head = fdc->status[4];
11131b67be7bSPoul-Henning Kamp 		idp->sec = fdc->status[5];
11141b67be7bSPoul-Henning Kamp 		idp->secshift = fdc->status[6];
11151b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
11161b67be7bSPoul-Henning Kamp 			printf("c %d h %d s %d z %d\n",
11171b67be7bSPoul-Henning Kamp 			    idp->cyl, idp->head, idp->sec, idp->secshift);
11181b67be7bSPoul-Henning Kamp 		break;
11191b67be7bSPoul-Henning Kamp 	case BIO_READ:
11201b67be7bSPoul-Henning Kamp 	case BIO_WRITE:
11211b67be7bSPoul-Henning Kamp 		bp->bio_pblkno += nsect;
11221b67be7bSPoul-Henning Kamp 		bp->bio_resid -= fd->fd_iosize;
11231b67be7bSPoul-Henning Kamp 		bp->bio_completed += fd->fd_iosize;
11241b67be7bSPoul-Henning Kamp 		fd->fd_ioptr += fd->fd_iosize;
11251b67be7bSPoul-Henning Kamp 		/* Since we managed to get something done, reset the retry */
11261b67be7bSPoul-Henning Kamp 		fdc->retry = 0;
11271b67be7bSPoul-Henning Kamp 		if (bp->bio_resid > 0)
11281b67be7bSPoul-Henning Kamp 			return (0);
11291b67be7bSPoul-Henning Kamp 		break;
11301b67be7bSPoul-Henning Kamp 	case BIO_FMT:
11311b67be7bSPoul-Henning Kamp 		break;
11321b67be7bSPoul-Henning Kamp 	}
11331b67be7bSPoul-Henning Kamp 	return (fdc_biodone(fdc, 0));
11341b67be7bSPoul-Henning Kamp }
11351b67be7bSPoul-Henning Kamp 
11361b67be7bSPoul-Henning Kamp static void
11371b67be7bSPoul-Henning Kamp fdc_thread(void *arg)
11381b67be7bSPoul-Henning Kamp {
11391b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
11401b67be7bSPoul-Henning Kamp 
11411b67be7bSPoul-Henning Kamp 	fdc = arg;
11421b67be7bSPoul-Henning Kamp 	int i;
11431b67be7bSPoul-Henning Kamp 
11443fc95726SJoerg Wunsch 	mtx_lock(&fdc->fdc_mtx);
114502910eeeSJoerg Wunsch 	fdc->flags |= FDC_KTHREAD_ALIVE;
114602910eeeSJoerg Wunsch 	while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
11473fc95726SJoerg Wunsch 		mtx_unlock(&fdc->fdc_mtx);
11481b67be7bSPoul-Henning Kamp 		i = fdc_worker(fdc);
11491b67be7bSPoul-Henning Kamp 		if (i && debugflags & 0x20) {
11501b67be7bSPoul-Henning Kamp 			if (fdc->bp != NULL) {
11511b67be7bSPoul-Henning Kamp 				g_print_bio(fdc->bp);
11521b67be7bSPoul-Henning Kamp 				printf("\n");
11531b67be7bSPoul-Henning Kamp 			}
11541b67be7bSPoul-Henning Kamp 			printf("Retry line %d\n", retry_line);
11551b67be7bSPoul-Henning Kamp 		}
11561b67be7bSPoul-Henning Kamp 		fdc->retry += i;
11573fc95726SJoerg Wunsch 		mtx_lock(&fdc->fdc_mtx);
11581b67be7bSPoul-Henning Kamp 	}
115902910eeeSJoerg Wunsch 	fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
1160f8353edeSPoul-Henning Kamp 	wakeup(&fdc->fdc_thread);
11613fc95726SJoerg Wunsch 	mtx_unlock(&fdc->fdc_mtx);
11623fc95726SJoerg Wunsch 
11633fc95726SJoerg Wunsch 	kthread_exit(0);
11641b67be7bSPoul-Henning Kamp }
11651b67be7bSPoul-Henning Kamp 
11661b67be7bSPoul-Henning Kamp /*
1167360048a7SJoerg Wunsch  * Enqueue a request.
11681b67be7bSPoul-Henning Kamp  */
11691b67be7bSPoul-Henning Kamp static void
11701b67be7bSPoul-Henning Kamp fd_enqueue(struct fd_data *fd, struct bio *bp)
11711b67be7bSPoul-Henning Kamp {
11721b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
11731b67be7bSPoul-Henning Kamp 	int call;
11741b67be7bSPoul-Henning Kamp 
11751b67be7bSPoul-Henning Kamp 	call = 0;
11761b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
11771b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
11781b67be7bSPoul-Henning Kamp 	/* If we go from idle, cancel motor turnoff */
11791b67be7bSPoul-Henning Kamp 	if (fd->fd_iocount++ == 0)
11801b67be7bSPoul-Henning Kamp 		callout_drain(&fd->toffhandle);
11811b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_MOTOR) {
11821b67be7bSPoul-Henning Kamp 		/* The motor is on, send it directly to the controller */
11831b67be7bSPoul-Henning Kamp 		bioq_disksort(&fdc->head, bp);
11841b67be7bSPoul-Henning Kamp 		wakeup(&fdc->head);
11851b67be7bSPoul-Henning Kamp 	} else {
11861b67be7bSPoul-Henning Kamp 		/* Queue it on the drive until the motor has started */
11871b67be7bSPoul-Henning Kamp 		bioq_insert_tail(&fd->fd_bq, bp);
11881b67be7bSPoul-Henning Kamp 		if (!(fd->flags & FD_MOTORWAIT))
11891b67be7bSPoul-Henning Kamp 			fd_motor(fd, 1);
11901b67be7bSPoul-Henning Kamp 	}
11911b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
11921b67be7bSPoul-Henning Kamp }
11931b67be7bSPoul-Henning Kamp 
11941b67be7bSPoul-Henning Kamp static int
11951b67be7bSPoul-Henning Kamp fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
11961b67be7bSPoul-Henning Kamp {
11971b67be7bSPoul-Henning Kamp 	struct bio *bp;
11981b67be7bSPoul-Henning Kamp 	struct fd_formb *finfo;
11991b67be7bSPoul-Henning Kamp 	struct fdc_readid *idfield;
12001b67be7bSPoul-Henning Kamp 	int error;
12011b67be7bSPoul-Henning Kamp 
12021b67be7bSPoul-Henning Kamp 	bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
12031b67be7bSPoul-Henning Kamp 
12041b67be7bSPoul-Henning Kamp 	/*
12051b67be7bSPoul-Henning Kamp 	 * Set up a bio request for fdstrategy().  bio_offset is faked
12061b67be7bSPoul-Henning Kamp 	 * so that fdstrategy() will seek to the the requested
12071b67be7bSPoul-Henning Kamp 	 * cylinder, and use the desired head.
12081b67be7bSPoul-Henning Kamp 	 */
12091b67be7bSPoul-Henning Kamp 	bp->bio_cmd = cmd;
12101b67be7bSPoul-Henning Kamp 	if (cmd == BIO_FMT) {
12111b67be7bSPoul-Henning Kamp 		finfo = (struct fd_formb *)data;
12121b67be7bSPoul-Henning Kamp 		bp->bio_pblkno =
12131b67be7bSPoul-Henning Kamp 		    (finfo->cyl * fd->ft->heads + finfo->head) *
12141b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac;
12151b67be7bSPoul-Henning Kamp 		bp->bio_length = sizeof *finfo;
12161b67be7bSPoul-Henning Kamp 	} else if (cmd == BIO_RDID) {
12171b67be7bSPoul-Henning Kamp 		idfield = (struct fdc_readid *)data;
12181b67be7bSPoul-Henning Kamp 		bp->bio_pblkno =
12191b67be7bSPoul-Henning Kamp 		    (idfield->cyl * fd->ft->heads + idfield->head) *
12201b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac;
12211b67be7bSPoul-Henning Kamp 		bp->bio_length = sizeof(struct fdc_readid);
12221b67be7bSPoul-Henning Kamp 	} else if (cmd == BIO_PROBE) {
12231b67be7bSPoul-Henning Kamp 		/* nothing */
12241b67be7bSPoul-Henning Kamp 	} else
12251b67be7bSPoul-Henning Kamp 		panic("wrong cmd in fdmisccmd()");
12261b67be7bSPoul-Henning Kamp 	bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
12271b67be7bSPoul-Henning Kamp 	bp->bio_data = data;
12281b67be7bSPoul-Henning Kamp 	bp->bio_driver1 = fd;
12291b67be7bSPoul-Henning Kamp 	bp->bio_flags = 0;
12301b67be7bSPoul-Henning Kamp 
12311b67be7bSPoul-Henning Kamp 	fd_enqueue(fd, bp);
12321b67be7bSPoul-Henning Kamp 
12331b67be7bSPoul-Henning Kamp 	do {
12341b67be7bSPoul-Henning Kamp 		msleep(bp, NULL, PRIBIO, "fdwait", hz);
12351b67be7bSPoul-Henning Kamp 	} while (!(bp->bio_flags & BIO_DONE));
12361b67be7bSPoul-Henning Kamp 	error = bp->bio_error;
12371b67be7bSPoul-Henning Kamp 
12381b67be7bSPoul-Henning Kamp 	free(bp, M_TEMP);
12391b67be7bSPoul-Henning Kamp 	return (error);
12401b67be7bSPoul-Henning Kamp }
12411b67be7bSPoul-Henning Kamp 
12421b67be7bSPoul-Henning Kamp /*
12431b67be7bSPoul-Henning Kamp  * Try figuring out the density of the media present in our device.
12441b67be7bSPoul-Henning Kamp  */
12451b67be7bSPoul-Henning Kamp static int
12461b67be7bSPoul-Henning Kamp fdautoselect(struct fd_data *fd)
12471b67be7bSPoul-Henning Kamp {
12481b67be7bSPoul-Henning Kamp 	struct fd_type *fdtp;
12491b67be7bSPoul-Henning Kamp 	struct fdc_readid id;
12501b67be7bSPoul-Henning Kamp 	int oopts, rv;
12511b67be7bSPoul-Henning Kamp 
12521b67be7bSPoul-Henning Kamp 	if (!(fd->ft->flags & FL_AUTO))
12531b67be7bSPoul-Henning Kamp 		return (0);
12541b67be7bSPoul-Henning Kamp 
12551b67be7bSPoul-Henning Kamp 	fdtp = fd_native_types[fd->type];
12561b67be7bSPoul-Henning Kamp 	fdsettype(fd, fdtp);
12571b67be7bSPoul-Henning Kamp 	if (!(fd->ft->flags & FL_AUTO))
12581b67be7bSPoul-Henning Kamp 		return (0);
12591b67be7bSPoul-Henning Kamp 
12601b67be7bSPoul-Henning Kamp 	/*
12611b67be7bSPoul-Henning Kamp 	 * Try reading sector ID fields, first at cylinder 0, head 0,
12621b67be7bSPoul-Henning Kamp 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
12631b67be7bSPoul-Henning Kamp 	 * since for 5.25in DD media in a HD drive, there are no data
12641b67be7bSPoul-Henning Kamp 	 * to read (2 step pulses per media cylinder required).  For
12651b67be7bSPoul-Henning Kamp 	 * two-sided media, the second probe always goes to head 1, so
12661b67be7bSPoul-Henning Kamp 	 * we can tell them apart from single-sided media.  As a
12671b67be7bSPoul-Henning Kamp 	 * side-effect this means that single-sided media should be
12681b67be7bSPoul-Henning Kamp 	 * mentioned in the search list after two-sided media of an
12691b67be7bSPoul-Henning Kamp 	 * otherwise identical density.  Media with a different number
12701b67be7bSPoul-Henning Kamp 	 * of sectors per track but otherwise identical parameters
12711b67be7bSPoul-Henning Kamp 	 * cannot be distinguished at all.
12721b67be7bSPoul-Henning Kamp 	 *
12731b67be7bSPoul-Henning Kamp 	 * If we successfully read an ID field on both cylinders where
12741b67be7bSPoul-Henning Kamp 	 * the recorded values match our expectation, we are done.
12751b67be7bSPoul-Henning Kamp 	 * Otherwise, we try the next density entry from the table.
12761b67be7bSPoul-Henning Kamp 	 *
12771b67be7bSPoul-Henning Kamp 	 * Stepping to cylinder 2 has the side-effect of clearing the
12781b67be7bSPoul-Henning Kamp 	 * unit attention bit.
12791b67be7bSPoul-Henning Kamp 	 */
12801b67be7bSPoul-Henning Kamp 	oopts = fd->options;
12811b67be7bSPoul-Henning Kamp 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
12821b67be7bSPoul-Henning Kamp 	for (; fdtp->heads; fdtp++) {
12831b67be7bSPoul-Henning Kamp 		fdsettype(fd, fdtp);
12841b67be7bSPoul-Henning Kamp 
12851b67be7bSPoul-Henning Kamp 		id.cyl = id.head = 0;
12861b67be7bSPoul-Henning Kamp 		rv = fdmisccmd(fd, BIO_RDID, &id);
12871b67be7bSPoul-Henning Kamp 		if (rv != 0)
12881b67be7bSPoul-Henning Kamp 			continue;
12891b67be7bSPoul-Henning Kamp 		if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
12901b67be7bSPoul-Henning Kamp 			continue;
12911b67be7bSPoul-Henning Kamp 		id.cyl = 2;
12921b67be7bSPoul-Henning Kamp 		id.head = fd->ft->heads - 1;
12931b67be7bSPoul-Henning Kamp 		rv = fdmisccmd(fd, BIO_RDID, &id);
12941b67be7bSPoul-Henning Kamp 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
12951b67be7bSPoul-Henning Kamp 		    id.secshift != fdtp->secsize)
12961b67be7bSPoul-Henning Kamp 			continue;
12971b67be7bSPoul-Henning Kamp 		if (rv == 0)
12981b67be7bSPoul-Henning Kamp 			break;
12991b67be7bSPoul-Henning Kamp 	}
13001b67be7bSPoul-Henning Kamp 
13011b67be7bSPoul-Henning Kamp 	fd->options = oopts;
13021b67be7bSPoul-Henning Kamp 	if (fdtp->heads == 0) {
13031b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
13041b67be7bSPoul-Henning Kamp 			device_printf(fd->dev, "autoselection failed\n");
13051b67be7bSPoul-Henning Kamp 		fdsettype(fd, fd_native_types[fd->type]);
13061b67be7bSPoul-Henning Kamp 		return (0);
13071b67be7bSPoul-Henning Kamp 	} else {
13081b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40) {
13091b67be7bSPoul-Henning Kamp 			device_printf(fd->dev,
13101b67be7bSPoul-Henning Kamp 			    "autoselected %d KB medium\n", fd->ft->size / 2);
13111b67be7bSPoul-Henning Kamp 			fdprinttype(fd->ft);
13121b67be7bSPoul-Henning Kamp 		}
13131b67be7bSPoul-Henning Kamp 		return (0);
13141b67be7bSPoul-Henning Kamp 	}
13151b67be7bSPoul-Henning Kamp }
13161b67be7bSPoul-Henning Kamp 
13171b67be7bSPoul-Henning Kamp /*
13181b67be7bSPoul-Henning Kamp  * GEOM class implementation
13191b67be7bSPoul-Henning Kamp  */
13201b67be7bSPoul-Henning Kamp 
13211b67be7bSPoul-Henning Kamp static g_access_t	fd_access;
13221b67be7bSPoul-Henning Kamp static g_start_t	fd_start;
13231b67be7bSPoul-Henning Kamp static g_ioctl_t	fd_ioctl;
13241b67be7bSPoul-Henning Kamp 
13251b67be7bSPoul-Henning Kamp struct g_class g_fd_class = {
13261b67be7bSPoul-Henning Kamp 	.name =		"FD",
13271b67be7bSPoul-Henning Kamp 	.version =	G_VERSION,
13281b67be7bSPoul-Henning Kamp 	.start =	fd_start,
13291b67be7bSPoul-Henning Kamp 	.access =	fd_access,
13301b67be7bSPoul-Henning Kamp 	.ioctl =	fd_ioctl,
13311b67be7bSPoul-Henning Kamp };
13321b67be7bSPoul-Henning Kamp 
13331b67be7bSPoul-Henning Kamp static int
13341b67be7bSPoul-Henning Kamp fd_access(struct g_provider *pp, int r, int w, int e)
13351b67be7bSPoul-Henning Kamp {
13361b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
13371b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
13381b67be7bSPoul-Henning Kamp 	int ar, aw, ae;
13391b67be7bSPoul-Henning Kamp 
13401b67be7bSPoul-Henning Kamp 	fd = pp->geom->softc;
13411b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
13421b67be7bSPoul-Henning Kamp 
13431b67be7bSPoul-Henning Kamp 	/*
13441b67be7bSPoul-Henning Kamp 	 * If our provider is withering, we can only get negative requests
13451b67be7bSPoul-Henning Kamp 	 * and we don't want to even see them
13461b67be7bSPoul-Henning Kamp 	 */
13471b67be7bSPoul-Henning Kamp 	if (pp->flags & G_PF_WITHER)
13481b67be7bSPoul-Henning Kamp 		return (0);
13491b67be7bSPoul-Henning Kamp 
13501b67be7bSPoul-Henning Kamp 	ar = r + pp->acr;
13511b67be7bSPoul-Henning Kamp 	aw = w + pp->acw;
13521b67be7bSPoul-Henning Kamp 	ae = e + pp->ace;
13531b67be7bSPoul-Henning Kamp 
13541b67be7bSPoul-Henning Kamp 	if (ar == 0 && aw == 0 && ae == 0) {
13551b67be7bSPoul-Henning Kamp 		device_unbusy(fd->dev);
13561b67be7bSPoul-Henning Kamp 		return (0);
13571b67be7bSPoul-Henning Kamp 	}
13581b67be7bSPoul-Henning Kamp 
13591b67be7bSPoul-Henning Kamp 	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
13601b67be7bSPoul-Henning Kamp 		if (fdmisccmd(fd, BIO_PROBE, NULL))
13611b67be7bSPoul-Henning Kamp 			return (ENXIO);
13621b67be7bSPoul-Henning Kamp 		if (fd->flags & FD_EMPTY)
13631b67be7bSPoul-Henning Kamp 			return (ENXIO);
13641b67be7bSPoul-Henning Kamp 		if (fd->flags & FD_NEWDISK) {
13651b67be7bSPoul-Henning Kamp 			fdautoselect(fd);
136661909e4cSPoul-Henning Kamp 			mtx_lock(&fdc->fdc_mtx);
13671b67be7bSPoul-Henning Kamp 			fd->flags &= ~FD_NEWDISK;
136861909e4cSPoul-Henning Kamp 			mtx_unlock(&fdc->fdc_mtx);
13691b67be7bSPoul-Henning Kamp 		}
13701b67be7bSPoul-Henning Kamp 		device_busy(fd->dev);
13711b67be7bSPoul-Henning Kamp 	}
13721b67be7bSPoul-Henning Kamp 
13731b67be7bSPoul-Henning Kamp 	if (w > 0 && (fd->flags & FD_WP))
13741b67be7bSPoul-Henning Kamp 		return (EROFS);
13751b67be7bSPoul-Henning Kamp 
13761b67be7bSPoul-Henning Kamp 	pp->sectorsize = fd->sectorsize;
13771b67be7bSPoul-Henning Kamp 	pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
13781b67be7bSPoul-Henning Kamp 	pp->mediasize = pp->stripesize * fd->ft->tracks;
13791b67be7bSPoul-Henning Kamp 	return (0);
13801b67be7bSPoul-Henning Kamp }
13811b67be7bSPoul-Henning Kamp 
13821b67be7bSPoul-Henning Kamp static void
13831b67be7bSPoul-Henning Kamp fd_start(struct bio *bp)
13841b67be7bSPoul-Henning Kamp {
13851b67be7bSPoul-Henning Kamp  	struct fdc_data *	fdc;
13861b67be7bSPoul-Henning Kamp  	struct fd_data *	fd;
13871b67be7bSPoul-Henning Kamp 
13881b67be7bSPoul-Henning Kamp 	fd = bp->bio_to->geom->softc;
13891b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
13901b67be7bSPoul-Henning Kamp 	bp->bio_driver1 = fd;
13911b67be7bSPoul-Henning Kamp 	if (bp->bio_cmd & BIO_GETATTR) {
13921b67be7bSPoul-Henning Kamp 		if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
13931b67be7bSPoul-Henning Kamp 			return;
13941b67be7bSPoul-Henning Kamp 		if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
13951b67be7bSPoul-Henning Kamp 			return;
13961b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, ENOIOCTL);
13971b67be7bSPoul-Henning Kamp 		return;
13981b67be7bSPoul-Henning Kamp 	}
13991b67be7bSPoul-Henning Kamp 	if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
14001b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, EOPNOTSUPP);
14011b67be7bSPoul-Henning Kamp 		return;
14021b67be7bSPoul-Henning Kamp 	}
14031b67be7bSPoul-Henning Kamp 	bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
14041b67be7bSPoul-Henning Kamp 	bp->bio_resid = bp->bio_length;
14051b67be7bSPoul-Henning Kamp 	fd_enqueue(fd, bp);
14061b67be7bSPoul-Henning Kamp 	return;
14071b67be7bSPoul-Henning Kamp }
14081b67be7bSPoul-Henning Kamp 
14091b67be7bSPoul-Henning Kamp static int
14101b67be7bSPoul-Henning Kamp fd_ioctl(struct g_provider *pp, u_long cmd, void *data, struct thread *td)
14111b67be7bSPoul-Henning Kamp {
14121b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
14131b67be7bSPoul-Henning Kamp 	struct fdc_status *fsp;
14141b67be7bSPoul-Henning Kamp 	struct fdc_readid *rid;
14151b67be7bSPoul-Henning Kamp 	int error;
14161b67be7bSPoul-Henning Kamp 
14171b67be7bSPoul-Henning Kamp 	fd = pp->geom->softc;
14181b67be7bSPoul-Henning Kamp 
14191b67be7bSPoul-Henning Kamp 	switch (cmd) {
14201b67be7bSPoul-Henning Kamp 	case FD_GTYPE:                  /* get drive type */
14211b67be7bSPoul-Henning Kamp 		*(struct fd_type *)data = *fd->ft;
14221b67be7bSPoul-Henning Kamp 		return (0);
14231b67be7bSPoul-Henning Kamp 
14241b67be7bSPoul-Henning Kamp 	case FD_STYPE:                  /* set drive type */
14251b67be7bSPoul-Henning Kamp 		/*
14261b67be7bSPoul-Henning Kamp 		 * Allow setting drive type temporarily iff
14271b67be7bSPoul-Henning Kamp 		 * currently unset.  Used for fdformat so any
14281b67be7bSPoul-Henning Kamp 		 * user can set it, and then start formatting.
14291b67be7bSPoul-Henning Kamp 		 */
14301b67be7bSPoul-Henning Kamp 		fd->fts = *(struct fd_type *)data;
14311b67be7bSPoul-Henning Kamp 		if (fd->fts.sectrac) {
14321b67be7bSPoul-Henning Kamp 			/* XXX: check for rubbish */
14331b67be7bSPoul-Henning Kamp 			fdsettype(fd, &fd->fts);
14341b67be7bSPoul-Henning Kamp 		} else {
14351b67be7bSPoul-Henning Kamp 			fdsettype(fd, fd_native_types[fd->type]);
14361b67be7bSPoul-Henning Kamp 		}
14371b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
14381b67be7bSPoul-Henning Kamp 			fdprinttype(fd->ft);
14391b67be7bSPoul-Henning Kamp 		return (0);
14401b67be7bSPoul-Henning Kamp 
14411b67be7bSPoul-Henning Kamp 	case FD_GOPTS:			/* get drive options */
14421b67be7bSPoul-Henning Kamp 		*(int *)data = fd->options;
14431b67be7bSPoul-Henning Kamp 		return (0);
14441b67be7bSPoul-Henning Kamp 
14451b67be7bSPoul-Henning Kamp 	case FD_SOPTS:			/* set drive options */
14461b67be7bSPoul-Henning Kamp 		fd->options = *(int *)data;
14471b67be7bSPoul-Henning Kamp 		return (0);
14481b67be7bSPoul-Henning Kamp 
14491b67be7bSPoul-Henning Kamp 	case FD_CLRERR:
14501b67be7bSPoul-Henning Kamp 		if (suser(td) != 0)
14511b67be7bSPoul-Henning Kamp 			return (EPERM);
14521b67be7bSPoul-Henning Kamp 		fd->fdc->fdc_errs = 0;
14531b67be7bSPoul-Henning Kamp 		return (0);
14541b67be7bSPoul-Henning Kamp 
14551b67be7bSPoul-Henning Kamp 	case FD_GSTAT:
14561b67be7bSPoul-Henning Kamp 		fsp = (struct fdc_status *)data;
14571b67be7bSPoul-Henning Kamp 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
14581b67be7bSPoul-Henning Kamp 			return (EINVAL);
14591b67be7bSPoul-Henning Kamp 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
14601b67be7bSPoul-Henning Kamp 		return (0);
14611b67be7bSPoul-Henning Kamp 
14621b67be7bSPoul-Henning Kamp 	case FD_GDTYPE:
14631b67be7bSPoul-Henning Kamp 		*(enum fd_drivetype *)data = fd->type;
14641b67be7bSPoul-Henning Kamp 		return (0);
14651b67be7bSPoul-Henning Kamp 
14661b67be7bSPoul-Henning Kamp 	case FD_FORM:
14671b67be7bSPoul-Henning Kamp 		if (((struct fd_formb *)data)->format_version !=
14681b67be7bSPoul-Henning Kamp 		    FD_FORMAT_VERSION)
14691b67be7bSPoul-Henning Kamp 			return (EINVAL); /* wrong version of formatting prog */
14701b67be7bSPoul-Henning Kamp 		error = fdmisccmd(fd, BIO_FMT, data);
147161909e4cSPoul-Henning Kamp 		mtx_lock(&fd->fdc->fdc_mtx);
14721b67be7bSPoul-Henning Kamp 		fd->flags |= FD_NEWDISK;
147361909e4cSPoul-Henning Kamp 		mtx_unlock(&fd->fdc->fdc_mtx);
14741b67be7bSPoul-Henning Kamp 		break;
14751b67be7bSPoul-Henning Kamp 
14761b67be7bSPoul-Henning Kamp 	case FD_READID:
14771b67be7bSPoul-Henning Kamp 		rid = (struct fdc_readid *)data;
14781b67be7bSPoul-Henning Kamp 		if (rid->cyl > 85 || rid->head > 1)
14791b67be7bSPoul-Henning Kamp 			return (EINVAL);
14801b67be7bSPoul-Henning Kamp 		error = fdmisccmd(fd, BIO_RDID, data);
14811b67be7bSPoul-Henning Kamp 		break;
14821b67be7bSPoul-Henning Kamp 
14831b67be7bSPoul-Henning Kamp 	case FIONBIO:
14841b67be7bSPoul-Henning Kamp 	case FIOASYNC:
14851b67be7bSPoul-Henning Kamp 		/* For backwards compat with old fd*(8) tools */
14861b67be7bSPoul-Henning Kamp 		error = 0;
14871b67be7bSPoul-Henning Kamp 		break;
14881b67be7bSPoul-Henning Kamp 
14891b67be7bSPoul-Henning Kamp 	default:
14901b67be7bSPoul-Henning Kamp 		if (debugflags & 0x80)
14911b67be7bSPoul-Henning Kamp 			printf("Unknown ioctl %lx\n", cmd);
14921b67be7bSPoul-Henning Kamp 		error = ENOIOCTL;
14931b67be7bSPoul-Henning Kamp 		break;
14941b67be7bSPoul-Henning Kamp 	}
14951b67be7bSPoul-Henning Kamp 	return (error);
14961b67be7bSPoul-Henning Kamp };
14971b67be7bSPoul-Henning Kamp 
14981b67be7bSPoul-Henning Kamp 
14991b67be7bSPoul-Henning Kamp 
15001b67be7bSPoul-Henning Kamp /*
15011b67be7bSPoul-Henning Kamp  * Configuration/initialization stuff, per controller.
15021b67be7bSPoul-Henning Kamp  */
15031b67be7bSPoul-Henning Kamp 
15041b67be7bSPoul-Henning Kamp devclass_t fdc_devclass;
15051b67be7bSPoul-Henning Kamp static devclass_t fd_devclass;
15061b67be7bSPoul-Henning Kamp 
15071b67be7bSPoul-Henning Kamp struct fdc_ivars {
15081b67be7bSPoul-Henning Kamp 	int	fdunit;
15091b67be7bSPoul-Henning Kamp 	int	fdtype;
15101b67be7bSPoul-Henning Kamp };
15111b67be7bSPoul-Henning Kamp 
15126d6fa4fdSWarner Losh void
151337286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
151437286586SPeter Wemm {
151537286586SPeter Wemm 	device_t dev;
151637286586SPeter Wemm 
151737286586SPeter Wemm 	dev = fdc->fdc_dev;
1518b4046cd7SWarner Losh 	if (fdc->fdc_intr)
15197964bd0cSWarner Losh 		bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1520a6e4d8c4SNate Lawson 	fdc->fdc_intr = NULL;
1521b4046cd7SWarner Losh 	if (fdc->res_irq != NULL)
152237286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
152337286586SPeter Wemm 		    fdc->res_irq);
1524a6e4d8c4SNate Lawson 	fdc->res_irq = NULL;
1525b4046cd7SWarner Losh 	if (fdc->res_ctl != NULL)
152637286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
152737286586SPeter Wemm 		    fdc->res_ctl);
1528a6e4d8c4SNate Lawson 	fdc->res_ctl = NULL;
1529b4046cd7SWarner Losh 	if (fdc->res_sts != NULL)
1530b4046cd7SWarner Losh 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_sts,
1531b4046cd7SWarner Losh 		    fdc->res_sts);
1532b4046cd7SWarner Losh 	fdc->res_sts = NULL;
1533b4046cd7SWarner Losh 	if (fdc->res_ioport != NULL)
153437286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
153537286586SPeter Wemm 		    fdc->res_ioport);
1536a6e4d8c4SNate Lawson 	fdc->res_ioport = NULL;
1537b4046cd7SWarner Losh 	if (fdc->res_drq != NULL)
153837286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
153937286586SPeter Wemm 		    fdc->res_drq);
15405ceae6b8SYoshihiro Takahashi 	fdc->res_drq = NULL;
154137286586SPeter Wemm }
154237286586SPeter Wemm 
15436d6fa4fdSWarner Losh int
15443aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
154537286586SPeter Wemm {
154637286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
154737286586SPeter Wemm 
154837286586SPeter Wemm 	switch (which) {
154937286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
155037286586SPeter Wemm 		*result = ivars->fdunit;
155137286586SPeter Wemm 		break;
1552752d4735SNate Lawson 	case FDC_IVAR_FDTYPE:
1553752d4735SNate Lawson 		*result = ivars->fdtype;
1554752d4735SNate Lawson 		break;
155537286586SPeter Wemm 	default:
1556752d4735SNate Lawson 		return (ENOENT);
155737286586SPeter Wemm 	}
1558752d4735SNate Lawson 	return (0);
1559752d4735SNate Lawson }
1560752d4735SNate Lawson 
1561752d4735SNate Lawson int
1562752d4735SNate Lawson fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1563752d4735SNate Lawson {
1564752d4735SNate Lawson 	struct fdc_ivars *ivars = device_get_ivars(child);
1565752d4735SNate Lawson 
1566752d4735SNate Lawson 	switch (which) {
1567752d4735SNate Lawson 	case FDC_IVAR_FDUNIT:
1568752d4735SNate Lawson 		ivars->fdunit = value;
1569752d4735SNate Lawson 		break;
1570752d4735SNate Lawson 	case FDC_IVAR_FDTYPE:
1571752d4735SNate Lawson 		ivars->fdtype = value;
1572752d4735SNate Lawson 		break;
1573752d4735SNate Lawson 	default:
1574752d4735SNate Lawson 		return (ENOENT);
1575752d4735SNate Lawson 	}
1576752d4735SNate Lawson 	return (0);
1577752d4735SNate Lawson }
1578752d4735SNate Lawson 
1579752d4735SNate Lawson int
15801b67be7bSPoul-Henning Kamp fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1581752d4735SNate Lawson {
15821b67be7bSPoul-Henning Kamp 	int ic_type, part_id;
15831b67be7bSPoul-Henning Kamp 
1584ec200f23SPoul-Henning Kamp 	/*
1585ec200f23SPoul-Henning Kamp 	 * A status value of 0xff is very unlikely, but not theoretically
1586ec200f23SPoul-Henning Kamp 	 * impossible, but it is far more likely to indicate an empty bus.
1587ec200f23SPoul-Henning Kamp 	 */
1588ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) == 0xff)
1589ec200f23SPoul-Henning Kamp 		return (ENXIO);
1590ec200f23SPoul-Henning Kamp 
1591ec200f23SPoul-Henning Kamp 	/*
1592ec200f23SPoul-Henning Kamp 	 * Assert a reset to the floppy controller and check that the status
1593ec200f23SPoul-Henning Kamp 	 * register goes to zero.
1594ec200f23SPoul-Henning Kamp 	 */
1595752d4735SNate Lawson 	fdout_wr(fdc, 0);
1596ec200f23SPoul-Henning Kamp 	fdout_wr(fdc, 0);
1597ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) != 0)
1598ec200f23SPoul-Henning Kamp 		return (ENXIO);
1599ec200f23SPoul-Henning Kamp 
1600ec200f23SPoul-Henning Kamp 	/*
1601ec200f23SPoul-Henning Kamp 	 * Clear the reset and see it come ready.
1602ec200f23SPoul-Henning Kamp 	 */
1603752d4735SNate Lawson 	fdout_wr(fdc, FDO_FRST);
1604ec200f23SPoul-Henning Kamp 	DELAY(100);
1605ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) != 0x80)
1606ec200f23SPoul-Henning Kamp 		return (ENXIO);
1607752d4735SNate Lawson 
1608752d4735SNate Lawson 	/* Then, see if it can handle a command. */
16091b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0))
1610752d4735SNate Lawson 		return (ENXIO);
16111b67be7bSPoul-Henning Kamp 
16121b67be7bSPoul-Henning Kamp 	/*
16131b67be7bSPoul-Henning Kamp 	 * Try to identify the chip.
16141b67be7bSPoul-Henning Kamp 	 *
16151b67be7bSPoul-Henning Kamp 	 * The i8272 datasheet documents that unknown commands
16161b67be7bSPoul-Henning Kamp 	 * will return ST0 as 0x80.  The i8272 is supposedly identical
16171b67be7bSPoul-Henning Kamp 	 * to the NEC765.
16181b67be7bSPoul-Henning Kamp 	 * The i82077SL datasheet says 0x90 for the VERSION command,
16191b67be7bSPoul-Henning Kamp 	 * and several "superio" chips emulate this.
16201b67be7bSPoul-Henning Kamp 	 */
16211b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
16221b67be7bSPoul-Henning Kamp 		return (ENXIO);
16231b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
16241b67be7bSPoul-Henning Kamp 		return (ENXIO);
16251d4b1bb7SPoul-Henning Kamp 	if (bootverbose)
16261d4b1bb7SPoul-Henning Kamp 		device_printf(dev,
16271d4b1bb7SPoul-Henning Kamp 		    "ic_type %02x part_id %02x\n", ic_type, part_id);
16281b67be7bSPoul-Henning Kamp 	switch (ic_type & 0xff) {
16291b67be7bSPoul-Henning Kamp 	case 0x80:
16301b67be7bSPoul-Henning Kamp 		device_set_desc(dev, "NEC 765 or clone");
16311b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_NE765;
16321b67be7bSPoul-Henning Kamp 		break;
16331b67be7bSPoul-Henning Kamp 	case 0x81:
16341b67be7bSPoul-Henning Kamp 	case 0x90:
16351b67be7bSPoul-Henning Kamp 		device_set_desc(dev,
16361b67be7bSPoul-Henning Kamp 		    "Enhanced floppy controller");
16371b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_ENHANCED;
16381b67be7bSPoul-Henning Kamp 		break;
16391b67be7bSPoul-Henning Kamp 	default:
16401b67be7bSPoul-Henning Kamp 		device_set_desc(dev, "Generic floppy controller");
16411b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_UNKNOWN;
16421b67be7bSPoul-Henning Kamp 		break;
16431b67be7bSPoul-Henning Kamp 	}
1644752d4735SNate Lawson 	return (0);
164537286586SPeter Wemm }
164637286586SPeter Wemm 
16476d6fa4fdSWarner Losh int
1648e219897aSJoerg Wunsch fdc_detach(device_t dev)
16495f830ea2SJoerg Wunsch {
16505f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
16515f830ea2SJoerg Wunsch 	int	error;
16525f830ea2SJoerg Wunsch 
16535f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
16545f830ea2SJoerg Wunsch 
16555f830ea2SJoerg Wunsch 	/* have our children detached first */
16565f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
16575f830ea2SJoerg Wunsch 		return (error);
16585f830ea2SJoerg Wunsch 
165902910eeeSJoerg Wunsch 	/* kill worker thread */
166002910eeeSJoerg Wunsch 	fdc->flags |= FDC_KTHREAD_EXIT;
166102910eeeSJoerg Wunsch 	mtx_lock(&fdc->fdc_mtx);
1662f8353edeSPoul-Henning Kamp 	wakeup(&fdc->head);
166302910eeeSJoerg Wunsch 	while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
166402910eeeSJoerg Wunsch 		msleep(&fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
166502910eeeSJoerg Wunsch 	mtx_unlock(&fdc->fdc_mtx);
166602910eeeSJoerg Wunsch 
1667e219897aSJoerg Wunsch 	/* reset controller, turn motor off */
1668e219897aSJoerg Wunsch 	fdout_wr(fdc, 0);
1669e219897aSJoerg Wunsch 
16707964bd0cSWarner Losh 	if (!(fdc->flags & FDC_NODMA))
16717964bd0cSWarner Losh 		isa_dma_release(fdc->dmachan);
16725f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
16731b67be7bSPoul-Henning Kamp 	mtx_destroy(&fdc->fdc_mtx);
16745f830ea2SJoerg Wunsch 	return (0);
16755f830ea2SJoerg Wunsch }
16765f830ea2SJoerg Wunsch 
16775b81b6b3SRodney W. Grimes /*
167837286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
16795b81b6b3SRodney W. Grimes  */
1680a6e4d8c4SNate Lawson device_t
168137286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
16825b81b6b3SRodney W. Grimes {
168337286586SPeter Wemm 	struct fdc_ivars *ivar;
16846182fdbdSPeter Wemm 	device_t child;
168592200632SGarrett Wollman 
16867cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
168737286586SPeter Wemm 	if (ivar == NULL)
1688a6e4d8c4SNate Lawson 		return (NULL);
1689fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
169042117b6cSPoul-Henning Kamp 	if (child == NULL) {
169142117b6cSPoul-Henning Kamp 		free(ivar, M_DEVBUF);
1692a6e4d8c4SNate Lawson 		return (NULL);
169342117b6cSPoul-Henning Kamp 	}
169437286586SPeter Wemm 	device_set_ivars(child, ivar);
1695a6e4d8c4SNate Lawson 	ivar->fdunit = unit;
1696a6e4d8c4SNate Lawson 	ivar->fdtype = FDT_NONE;
16978a9bc9c0SJohn Baldwin 	if (resource_disabled(name, unit))
16986182fdbdSPeter Wemm 		device_disable(child);
1699a6e4d8c4SNate Lawson 	return (child);
17006182fdbdSPeter Wemm }
17016182fdbdSPeter Wemm 
17026d6fa4fdSWarner Losh int
17036182fdbdSPeter Wemm fdc_attach(device_t dev)
17046182fdbdSPeter Wemm {
170537286586SPeter Wemm 	struct	fdc_data *fdc;
1706a6e4d8c4SNate Lawson 	int	error;
1707427ccf00SDoug Rabson 
170837286586SPeter Wemm 	fdc = device_get_softc(dev);
1709b0c2b925SWarner Losh 	fdc->fdc_dev = dev;
17101b67be7bSPoul-Henning Kamp 	error = fdc_initial_reset(dev, fdc);
17111b67be7bSPoul-Henning Kamp 	if (error) {
17121b67be7bSPoul-Henning Kamp 		device_printf(dev, "does not respond\n");
17131b67be7bSPoul-Henning Kamp 		return (error);
17141b67be7bSPoul-Henning Kamp 	}
1715b593dd82SWarner Losh 	error = bus_setup_intr(dev, fdc->res_irq,
17161b67be7bSPoul-Henning Kamp 	    INTR_TYPE_BIO | INTR_ENTROPY | INTR_FAST | INTR_MPSAFE,
17171b67be7bSPoul-Henning Kamp 	    fdc_intr, fdc, &fdc->fdc_intr);
171837286586SPeter Wemm 	if (error) {
171937286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
17201b67be7bSPoul-Henning Kamp 		return (error);
172137286586SPeter Wemm 	}
17227964bd0cSWarner Losh 	if (!(fdc->flags & FDC_NODMA)) {
17237964bd0cSWarner Losh 		error = isa_dma_acquire(fdc->dmachan);
17241ef0fd82SPoul-Henning Kamp 		if (!error) {
17251ef0fd82SPoul-Henning Kamp 			error = isa_dma_init(fdc->dmachan,
17261ef0fd82SPoul-Henning Kamp 			    MAX_BYTES_PER_CYL, M_WAITOK);
17271ef0fd82SPoul-Henning Kamp 			if (error)
17281ef0fd82SPoul-Henning Kamp 				isa_dma_release(fdc->dmachan);
17291ef0fd82SPoul-Henning Kamp 		}
17307964bd0cSWarner Losh 		if (error)
17317964bd0cSWarner Losh 			return (error);
17327964bd0cSWarner Losh 	}
173337286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
1734a6e4d8c4SNate Lawson 	fdc->flags |= FDC_NEEDS_RESET;
17356182fdbdSPeter Wemm 
17361b67be7bSPoul-Henning Kamp 	mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
17376182fdbdSPeter Wemm 
17383a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
173964860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
17408177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
17415b81b6b3SRodney W. Grimes 
17421b67be7bSPoul-Henning Kamp 	kthread_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
17431b67be7bSPoul-Henning Kamp 	    "fdc%d", device_get_unit(dev));
17441b67be7bSPoul-Henning Kamp 
17451b67be7bSPoul-Henning Kamp 	settle = hz / 8;
17461b67be7bSPoul-Henning Kamp 
1747a6e4d8c4SNate Lawson 	return (0);
1748a6e4d8c4SNate Lawson }
1749a6e4d8c4SNate Lawson 
1750a6e4d8c4SNate Lawson int
1751a6e4d8c4SNate Lawson fdc_hints_probe(device_t dev)
1752a6e4d8c4SNate Lawson {
1753a6e4d8c4SNate Lawson 	const char *name, *dname;
1754a6e4d8c4SNate Lawson 	int i, error, dunit;
1755a6e4d8c4SNate Lawson 
17566182fdbdSPeter Wemm 	/*
175737286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
175837286586SPeter Wemm 	 * devices from the BIOS unless overridden.
17596182fdbdSPeter Wemm 	 */
1760ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
17612398f0cdSPeter Wemm 	i = 0;
1762a6e4d8c4SNate Lawson 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1763a6e4d8c4SNate Lawson 		resource_int_value(dname, dunit, "drive", &dunit);
17642398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
1765a6e4d8c4SNate Lawson 	}
176637286586SPeter Wemm 
176760444853SJoerg Wunsch 	if ((error = bus_generic_attach(dev)) != 0)
176860444853SJoerg Wunsch 		return (error);
176960444853SJoerg Wunsch 	return (0);
17706182fdbdSPeter Wemm }
17716182fdbdSPeter Wemm 
17726d6fa4fdSWarner Losh int
17736182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
17746182fdbdSPeter Wemm {
17751a6bed68SJoerg Wunsch 	int retval = 0, flags;
177615317dd8SMatthew N. Dodd 
177715317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
17781a6bed68SJoerg Wunsch 	retval += printf(" on %s drive %d", device_get_nameunit(me),
177937286586SPeter Wemm 	       fdc_get_fdunit(child));
17801a6bed68SJoerg Wunsch 	if ((flags = device_get_flags(me)) != 0)
17811a6bed68SJoerg Wunsch 		retval += printf(" flags %#x", flags);
17821a6bed68SJoerg Wunsch 	retval += printf("\n");
178315317dd8SMatthew N. Dodd 
178415317dd8SMatthew N. Dodd 	return (retval);
17856182fdbdSPeter Wemm }
17866182fdbdSPeter Wemm 
178716e68fc6SPeter Wemm /*
1788246ed35dSJoerg Wunsch  * Configuration/initialization, per drive.
178916e68fc6SPeter Wemm  */
17906182fdbdSPeter Wemm static int
17916182fdbdSPeter Wemm fd_probe(device_t dev)
17926182fdbdSPeter Wemm {
17931b67be7bSPoul-Henning Kamp 	int	i, unit;
17941a6bed68SJoerg Wunsch 	u_int	st0, st3;
17956182fdbdSPeter Wemm 	struct	fd_data *fd;
17966182fdbdSPeter Wemm 	struct	fdc_data *fdc;
17971b67be7bSPoul-Henning Kamp 	int	fdsu;
1798752d4735SNate Lawson 	int	flags, type;
17996182fdbdSPeter Wemm 
1800752d4735SNate Lawson 	fdsu = fdc_get_fdunit(dev);
18016182fdbdSPeter Wemm 	fd = device_get_softc(dev);
18026182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
18031a6bed68SJoerg Wunsch 	flags = device_get_flags(dev);
18046182fdbdSPeter Wemm 
18056182fdbdSPeter Wemm 	fd->dev = dev;
18066182fdbdSPeter Wemm 	fd->fdc = fdc;
18076182fdbdSPeter Wemm 	fd->fdsu = fdsu;
18081b67be7bSPoul-Henning Kamp 	unit = device_get_unit(dev);
18096182fdbdSPeter Wemm 
1810752d4735SNate Lawson 	/* Auto-probe if fdinfo is present, but always allow override. */
18111b67be7bSPoul-Henning Kamp 	type = flags & FD_TYPEMASK;
1812752d4735SNate Lawson 	if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1813752d4735SNate Lawson 		fd->type = type;
1814752d4735SNate Lawson 		goto done;
1815752d4735SNate Lawson 	} else {
1816752d4735SNate Lawson 		/* make sure fdautoselect() will be called */
18171b67be7bSPoul-Henning Kamp 		fd->flags = FD_EMPTY;
1818752d4735SNate Lawson 		fd->type = type;
1819752d4735SNate Lawson 	}
1820752d4735SNate Lawson 
182127df52f9SWarner Losh #if (defined(__i386__) && !defined(PC98)) || defined(__amd64__)
18221b67be7bSPoul-Henning Kamp 	if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
18231a6bed68SJoerg Wunsch 		/* Look up what the BIOS thinks we have. */
182427df52f9SWarner Losh 		if (unit == 0)
18251a6bed68SJoerg Wunsch 			fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
182627df52f9SWarner Losh 		else
18271a6bed68SJoerg Wunsch 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
18281a6bed68SJoerg Wunsch 		if (fd->type == FDT_288M_1)
18291a6bed68SJoerg Wunsch 			fd->type = FDT_288M;
18301a6bed68SJoerg Wunsch 	}
183104209354SPeter Wemm #endif /* __i386__ || __amd64__ */
18326182fdbdSPeter Wemm 	/* is there a unit? */
18331a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE)
18346182fdbdSPeter Wemm 		return (ENXIO);
18356182fdbdSPeter Wemm 
18361b67be7bSPoul-Henning Kamp /*
18371b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
18381b67be7bSPoul-Henning Kamp */
18396182fdbdSPeter Wemm 	/* select it */
18401b67be7bSPoul-Henning Kamp 	fd_select(fd);
18411b67be7bSPoul-Henning Kamp 	fd_motor(fd, 1);
18421b67be7bSPoul-Henning Kamp 	fdc->fd = fd;
1843fb35bd37SJoerg Wunsch 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
18446182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
18456182fdbdSPeter Wemm 
18461a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0) {
1847dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
18481b67be7bSPoul-Henning Kamp 		if ((fdc_sense_drive(fdc, &st3) == 0) &&
18491a6bed68SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
1850dc5df763SJoerg Wunsch 			/* Seek some steps... */
18511b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1852dc5df763SJoerg Wunsch 				/* ...wait a moment... */
1853dc5df763SJoerg Wunsch 				DELAY(300000);
1854dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
18551b67be7bSPoul-Henning Kamp 				fdc_sense_int(fdc, 0, 0);
1856dc5df763SJoerg Wunsch 			}
1857dc5df763SJoerg Wunsch 		}
1858dc5df763SJoerg Wunsch 
18596b7bd95bSJoerg Wunsch 		for (i = 0; i < 2; i++) {
18606b7bd95bSJoerg Wunsch 			/*
18616b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
18621a6bed68SJoerg Wunsch 			 * heads have been beyond cylinder 76, since
18631a6bed68SJoerg Wunsch 			 * most FDCs still barf when attempting to
18641a6bed68SJoerg Wunsch 			 * recalibrate more than 77 steps
18656b7bd95bSJoerg Wunsch 			 */
1866dc5df763SJoerg Wunsch 			/* go back to 0: */
18671b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
18686b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
18696b7bd95bSJoerg Wunsch 				DELAY(i == 0 ? 1000000 : 300000);
18705b81b6b3SRodney W. Grimes 
18716b7bd95bSJoerg Wunsch 				/* anything responding? */
18721b67be7bSPoul-Henning Kamp 				if (fdc_sense_int(fdc, &st0, 0) == 0 &&
1873dc5df763SJoerg Wunsch 				    (st0 & NE7_ST0_EC) == 0)
18746b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
18756b7bd95bSJoerg Wunsch 			}
1876dc5df763SJoerg Wunsch 		}
18771a6bed68SJoerg Wunsch 	}
18786b7bd95bSJoerg Wunsch 
18791b67be7bSPoul-Henning Kamp 	fd_motor(fd, 0);
18801b67be7bSPoul-Henning Kamp 	fdc->fd = NULL;
18811b67be7bSPoul-Henning Kamp /*
18821b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
18831b67be7bSPoul-Henning Kamp */
18843a2f7427SDavid Greenman 
18851a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0 &&
18861a6bed68SJoerg Wunsch 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
18876182fdbdSPeter Wemm 		return (ENXIO);
18885b81b6b3SRodney W. Grimes 
1889752d4735SNate Lawson done:
1890752d4735SNate Lawson 
18911a6bed68SJoerg Wunsch 	switch (fd->type) {
18921a6bed68SJoerg Wunsch 	case FDT_12M:
18931a6bed68SJoerg Wunsch 		device_set_desc(dev, "1200-KB 5.25\" drive");
18941a6bed68SJoerg Wunsch 		break;
18951a6bed68SJoerg Wunsch 	case FDT_144M:
18961a6bed68SJoerg Wunsch 		device_set_desc(dev, "1440-KB 3.5\" drive");
18971a6bed68SJoerg Wunsch 		break;
18981a6bed68SJoerg Wunsch 	case FDT_288M:
18991a6bed68SJoerg Wunsch 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
19001a6bed68SJoerg Wunsch 		break;
19011a6bed68SJoerg Wunsch 	case FDT_360K:
19021a6bed68SJoerg Wunsch 		device_set_desc(dev, "360-KB 5.25\" drive");
19031a6bed68SJoerg Wunsch 		break;
19041a6bed68SJoerg Wunsch 	case FDT_720K:
19051a6bed68SJoerg Wunsch 		device_set_desc(dev, "720-KB 3.5\" drive");
19061a6bed68SJoerg Wunsch 		break;
19071a6bed68SJoerg Wunsch 	default:
19081a6bed68SJoerg Wunsch 		return (ENXIO);
19091a6bed68SJoerg Wunsch 	}
1910dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1911b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1912b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
19133a2f7427SDavid Greenman 	fd->options = 0;
19141b67be7bSPoul-Henning Kamp 	callout_init(&fd->toffhandle, 1);
19151b67be7bSPoul-Henning Kamp 	callout_init(&fd->tohandle, 1);
19165b81b6b3SRodney W. Grimes 
19171a6bed68SJoerg Wunsch 	/* initialize densities for subdevices */
19181b67be7bSPoul-Henning Kamp 	fdsettype(fd, fd_native_types[fd->type]);
19196182fdbdSPeter Wemm 	return (0);
19206182fdbdSPeter Wemm }
19216182fdbdSPeter Wemm 
19221b67be7bSPoul-Henning Kamp /*
19231b67be7bSPoul-Henning Kamp  * We have to do this in a geom event because GEOM is not running
19241b67be7bSPoul-Henning Kamp  * when fd_attach() is.
19251b67be7bSPoul-Henning Kamp  * XXX: move fd_attach after geom like ata/scsi disks
19261b67be7bSPoul-Henning Kamp  */
19271b67be7bSPoul-Henning Kamp static void
19281b67be7bSPoul-Henning Kamp fd_attach2(void *arg, int flag)
19291b67be7bSPoul-Henning Kamp {
19301b67be7bSPoul-Henning Kamp 	struct	fd_data *fd;
19311b67be7bSPoul-Henning Kamp 
19321b67be7bSPoul-Henning Kamp 	fd = arg;
19331b67be7bSPoul-Henning Kamp 
19341b67be7bSPoul-Henning Kamp 	fd->fd_geom = g_new_geomf(&g_fd_class,
19353ba0c6b1SPoul-Henning Kamp 	    "fd%d", device_get_unit(fd->dev));
19361b67be7bSPoul-Henning Kamp 	fd->fd_provider = g_new_providerf(fd->fd_geom, fd->fd_geom->name);
19371b67be7bSPoul-Henning Kamp 	fd->fd_geom->softc = fd;
19381b67be7bSPoul-Henning Kamp 	g_error_provider(fd->fd_provider, 0);
19391b67be7bSPoul-Henning Kamp }
19401b67be7bSPoul-Henning Kamp 
19416182fdbdSPeter Wemm static int
19426182fdbdSPeter Wemm fd_attach(device_t dev)
19436182fdbdSPeter Wemm {
19446182fdbdSPeter Wemm 	struct	fd_data *fd;
19456182fdbdSPeter Wemm 
194664860614SJoerg Wunsch 	fd = device_get_softc(dev);
19471b67be7bSPoul-Henning Kamp 	g_post_event(fd_attach2, fd, M_WAITOK, NULL);
19481b67be7bSPoul-Henning Kamp 	fd->flags |= FD_EMPTY;
19491b67be7bSPoul-Henning Kamp 	bioq_init(&fd->fd_bq);
19501b67be7bSPoul-Henning Kamp 
19516182fdbdSPeter Wemm 	return (0);
19525b81b6b3SRodney W. Grimes }
19535b81b6b3SRodney W. Grimes 
19545f830ea2SJoerg Wunsch static int
19555f830ea2SJoerg Wunsch fd_detach(device_t dev)
19565f830ea2SJoerg Wunsch {
19575f830ea2SJoerg Wunsch 	struct	fd_data *fd;
19585f830ea2SJoerg Wunsch 
19595f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
19601b67be7bSPoul-Henning Kamp 	callout_drain(&fd->toffhandle);
19615f830ea2SJoerg Wunsch 
19625f830ea2SJoerg Wunsch 	return (0);
19635f830ea2SJoerg Wunsch }
19645f830ea2SJoerg Wunsch 
196516e68fc6SPeter Wemm static device_method_t fd_methods[] = {
196616e68fc6SPeter Wemm 	/* Device interface */
196716e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
196816e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
19695f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
197016e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
197116e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
197216e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
197316e68fc6SPeter Wemm 	{ 0, 0 }
197416e68fc6SPeter Wemm };
197516e68fc6SPeter Wemm 
197616e68fc6SPeter Wemm static driver_t fd_driver = {
197716e68fc6SPeter Wemm 	"fd",
197816e68fc6SPeter Wemm 	fd_methods,
197916e68fc6SPeter Wemm 	sizeof(struct fd_data)
198016e68fc6SPeter Wemm };
198116e68fc6SPeter Wemm 
1982f8353edeSPoul-Henning Kamp static int
1983f8353edeSPoul-Henning Kamp fdc_modevent(module_t mod, int type, void *data)
1984f8353edeSPoul-Henning Kamp {
1985f8353edeSPoul-Henning Kamp 
1986f8353edeSPoul-Henning Kamp 	g_modevent(NULL, type, &g_fd_class);
1987f8353edeSPoul-Henning Kamp 	return (0);
1988f8353edeSPoul-Henning Kamp }
1989f8353edeSPoul-Henning Kamp 
1990f8353edeSPoul-Henning Kamp DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);
1991