xref: /freebsd/sys/dev/fdc/fdc.c (revision 25f98057076ce0474c2d37a238861476f974079b)
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>
72acd3428bSRobert Watson #include <sys/priv.h>
733a2f7427SDavid Greenman #include <sys/proc.h>
746182fdbdSPeter Wemm #include <sys/rman.h>
751b67be7bSPoul-Henning Kamp #include <sys/sysctl.h>
766d6fa4fdSWarner Losh #include <sys/systm.h>
776182fdbdSPeter Wemm 
781b67be7bSPoul-Henning Kamp #include <geom/geom.h>
791b67be7bSPoul-Henning Kamp 
80c3ae4c40SWarner Losh #include <machine/bus.h>
816182fdbdSPeter Wemm #include <machine/clock.h>
82dc5df763SJoerg Wunsch #include <machine/stdarg.h>
836182fdbdSPeter Wemm 
846182fdbdSPeter Wemm #include <isa/isavar.h>
8583e78a7fSJohn Baldwin #ifdef PC98
8683e78a7fSJohn Baldwin #include <pc98/pc98/pc98_machdep.h>
8783e78a7fSJohn Baldwin #else
88a97c75b7SDoug Rabson #include <isa/isareg.h>
89a97c75b7SDoug Rabson #include <isa/rtc.h>
9083e78a7fSJohn Baldwin #endif
9183e78a7fSJohn Baldwin #include <dev/fdc/fdcvar.h>
926182fdbdSPeter Wemm 
931b67be7bSPoul-Henning Kamp #include <dev/ic/nec765.h>
940722d6abSJoerg Wunsch 
953fef646eSJoerg Wunsch /*
961b67be7bSPoul-Henning Kamp  * Runtime configuration hints/flags
973fef646eSJoerg Wunsch  */
981b67be7bSPoul-Henning Kamp 
991b67be7bSPoul-Henning Kamp /* configuration flags for fd */
1001b67be7bSPoul-Henning Kamp #define FD_TYPEMASK	0x0f	/* drive type, matches enum
1011b67be7bSPoul-Henning Kamp 				 * fd_drivetype; on i386 machines, if
1021b67be7bSPoul-Henning Kamp 				 * given as 0, use RTC type for fd0
1031b67be7bSPoul-Henning Kamp 				 * and fd1 */
104c884498fSJung-uk Kim #define	FD_NO_CHLINE	0x10	/* drive does not support changeline
105c884498fSJung-uk Kim 				 * aka. unit attention */
1061b67be7bSPoul-Henning Kamp #define FD_NO_PROBE	0x20	/* don't probe drive (seek test), just
1071b67be7bSPoul-Henning Kamp 				 * assume it is there */
1081b67be7bSPoul-Henning Kamp 
1091b67be7bSPoul-Henning Kamp /*
1101b67be7bSPoul-Henning Kamp  * Things that could conceiveably considered parameters or tweakables
1111b67be7bSPoul-Henning Kamp  */
1121b67be7bSPoul-Henning Kamp 
1131b67be7bSPoul-Henning Kamp /*
1141b67be7bSPoul-Henning Kamp  * Maximal number of bytes in a cylinder.
1151b67be7bSPoul-Henning Kamp  * This is used for ISADMA bouncebuffer allocation and sets the max
1161b67be7bSPoul-Henning Kamp  * xfersize we support.
1171b67be7bSPoul-Henning Kamp  *
11861909e4cSPoul-Henning Kamp  * 2.88M format has 2 x 36 x 512, allow for hacked up density.
1191b67be7bSPoul-Henning Kamp  */
1201b67be7bSPoul-Henning Kamp #define MAX_BYTES_PER_CYL	(2 * 40 * 512)
121dc5df763SJoerg Wunsch 
1221a6bed68SJoerg Wunsch /*
123cb38bb6cSJoerg Wunsch  * Timeout value for the PIO loops to wait until the FDC main status
124cb38bb6cSJoerg Wunsch  * register matches our expectations (request for master, direction
125cb38bb6cSJoerg Wunsch  * bit).  This is supposed to be a number of microseconds, although
126cb38bb6cSJoerg Wunsch  * timing might actually not be very accurate.
127cb38bb6cSJoerg Wunsch  *
128cb38bb6cSJoerg Wunsch  * Timeouts of 100 msec are believed to be required for some broken
129cb38bb6cSJoerg Wunsch  * (old) hardware.
130cb38bb6cSJoerg Wunsch  */
131cb38bb6cSJoerg Wunsch #define	FDSTS_TIMEOUT	100000
132cb38bb6cSJoerg Wunsch 
133cb38bb6cSJoerg Wunsch /*
1341b67be7bSPoul-Henning Kamp  * After this many errors, stop whining.  Close will reset this count.
1351a6bed68SJoerg Wunsch  */
13661909e4cSPoul-Henning Kamp #define FDC_ERRMAX	100
1377ca0641bSAndrey A. Chernov 
1381a6bed68SJoerg Wunsch /*
1391b67be7bSPoul-Henning Kamp  * AutoDensity search lists for each drive type.
1401a6bed68SJoerg Wunsch  */
1411b67be7bSPoul-Henning Kamp 
1421b67be7bSPoul-Henning Kamp static struct fd_type fd_searchlist_360k[] = {
14383e78a7fSJohn Baldwin #ifndef PC98
1441b67be7bSPoul-Henning Kamp 	{ FDF_5_360 },
14583e78a7fSJohn Baldwin #endif
1461b67be7bSPoul-Henning Kamp 	{ 0 }
1475b81b6b3SRodney W. Grimes };
1485b81b6b3SRodney W. Grimes 
1491a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_12m[] = {
15083e78a7fSJohn Baldwin #ifdef PC98
15183e78a7fSJohn Baldwin 	{ FDF_5_1200 | FL_AUTO },
15283e78a7fSJohn Baldwin 	{ FDF_5_720 | FL_AUTO },
15383e78a7fSJohn Baldwin 	{ FDF_5_360 | FL_AUTO },
15483e78a7fSJohn Baldwin 	{ FDF_5_640 | FL_AUTO },
15583e78a7fSJohn Baldwin 	{ FDF_5_1230 | FL_AUTO },
15683e78a7fSJohn Baldwin #else
1571b67be7bSPoul-Henning Kamp 	{ FDF_5_1200 | FL_AUTO },
158c1a6d28aSWarner Losh 	{ FDF_5_400 | FL_AUTO },
1591b67be7bSPoul-Henning Kamp 	{ FDF_5_360 | FL_2STEP | FL_AUTO},
16083e78a7fSJohn Baldwin #endif
1611b67be7bSPoul-Henning Kamp 	{ 0 }
1621b67be7bSPoul-Henning Kamp };
1631b67be7bSPoul-Henning Kamp 
1641b67be7bSPoul-Henning Kamp static struct fd_type fd_searchlist_720k[] = {
16583e78a7fSJohn Baldwin #ifndef PC98
1661b67be7bSPoul-Henning Kamp 	{ FDF_3_720 },
16783e78a7fSJohn Baldwin #endif
1681b67be7bSPoul-Henning Kamp 	{ 0 }
1691a6bed68SJoerg Wunsch };
1701a6bed68SJoerg Wunsch 
1711a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_144m[] = {
17283e78a7fSJohn Baldwin #ifdef PC98
17383e78a7fSJohn Baldwin 	{ FDF_3_1440 | FL_AUTO},
17483e78a7fSJohn Baldwin 	{ FDF_3_1200 | FL_AUTO},
17583e78a7fSJohn Baldwin 	{ FDF_3_720 | FL_AUTO},
17683e78a7fSJohn Baldwin 	{ FDF_3_360 | FL_AUTO},
17783e78a7fSJohn Baldwin 	{ FDF_3_640 | FL_AUTO},
17883e78a7fSJohn Baldwin 	{ FDF_3_1230 | FL_AUTO},
17983e78a7fSJohn Baldwin #else
1801b67be7bSPoul-Henning Kamp 	{ FDF_3_1440 | FL_AUTO},
1811b67be7bSPoul-Henning Kamp 	{ FDF_3_720 | FL_AUTO},
18283e78a7fSJohn Baldwin #endif
1831b67be7bSPoul-Henning Kamp 	{ 0 }
1841a6bed68SJoerg Wunsch };
1851a6bed68SJoerg Wunsch 
1861a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_288m[] = {
18783e78a7fSJohn Baldwin #ifndef PC98
1881b67be7bSPoul-Henning Kamp 	{ FDF_3_1440 | FL_AUTO },
1891a6bed68SJoerg Wunsch #if 0
1901b67be7bSPoul-Henning Kamp 	{ FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
1911a6bed68SJoerg Wunsch #endif
1921b67be7bSPoul-Henning Kamp 	{ FDF_3_720 | FL_AUTO},
19383e78a7fSJohn Baldwin #endif
1941b67be7bSPoul-Henning Kamp 	{ 0 }
1951a6bed68SJoerg Wunsch };
196dc16046fSJoerg Wunsch 
1971b67be7bSPoul-Henning Kamp /*
1981b67be7bSPoul-Henning Kamp  * Order must match enum fd_drivetype in <sys/fdcio.h>.
1991b67be7bSPoul-Henning Kamp  */
2001b67be7bSPoul-Henning Kamp static struct fd_type *fd_native_types[] = {
2011b67be7bSPoul-Henning Kamp 	NULL,				/* FDT_NONE */
2021b67be7bSPoul-Henning Kamp 	fd_searchlist_360k, 		/* FDT_360K */
2031b67be7bSPoul-Henning Kamp 	fd_searchlist_12m, 		/* FDT_12M */
2041b67be7bSPoul-Henning Kamp 	fd_searchlist_720k, 		/* FDT_720K */
2051b67be7bSPoul-Henning Kamp 	fd_searchlist_144m, 		/* FDT_144M */
2068fe457d6SPeter Edwards 	fd_searchlist_288m,		/* FDT_288M_1 (mapped to FDT_288M) */
2071b67be7bSPoul-Henning Kamp 	fd_searchlist_288m, 		/* FDT_288M */
2081b67be7bSPoul-Henning Kamp };
209f8ce7dd5SJoerg Wunsch 
2101b67be7bSPoul-Henning Kamp /*
2111b67be7bSPoul-Henning Kamp  * Internals start here
2121b67be7bSPoul-Henning Kamp  */
2131b67be7bSPoul-Henning Kamp 
21483e78a7fSJohn Baldwin #ifdef PC98
21583e78a7fSJohn Baldwin /* registers */
21683e78a7fSJohn Baldwin #define	FDSTS	0	/* NEC 765 Main Status Register (R) */
21783e78a7fSJohn Baldwin #define	FDDATA	1	/* NEC 765 Data Register (R/W) */
21883e78a7fSJohn Baldwin #define	FDCTL	2	/* FD Control Register */
21983e78a7fSJohn Baldwin #define	FDC_RST		0x80	/*  FDC RESET */
22083e78a7fSJohn Baldwin #define	FDC_RDY		0x40	/*  force READY */
22183e78a7fSJohn Baldwin #define	FDC_DD		0x20	/*  FDD Mode Exchange 0:1M 1:640K */
22283e78a7fSJohn Baldwin #define	FDC_DMAE	0x10	/*  enable floppy DMA */
22383e78a7fSJohn Baldwin #define	FDC_MTON	0x08	/*  MOTOR ON (when EMTON=1)*/
22483e78a7fSJohn Baldwin #define	FDC_TMSK	0x04	/*  TIMER MASK */
22583e78a7fSJohn Baldwin #define	FDC_TTRG	0x01	/*  TIMER TRIGER */
22683e78a7fSJohn Baldwin 
22783e78a7fSJohn Baldwin #define	FDP	3
22883e78a7fSJohn Baldwin #define	FDP_EMTON	0x04	/*  enable MTON */
22983e78a7fSJohn Baldwin #define	FDP_FDDEXC	0x02	/*  FDD Mode Exchange 1:1M 0:640K */
23083e78a7fSJohn Baldwin #define	FDP_PORTEXC	0x01	/*  PORT Exchane 1:1M 0:640K */
23183e78a7fSJohn Baldwin 
23283e78a7fSJohn Baldwin #define	FDEM	4
23383e78a7fSJohn Baldwin #else
2341b67be7bSPoul-Henning Kamp /* registers */
2351b67be7bSPoul-Henning Kamp #define	FDOUT	2	/* Digital Output Register (W) */
2361b67be7bSPoul-Henning Kamp #define	FDO_FDSEL	0x03	/*  floppy device select */
2371b67be7bSPoul-Henning Kamp #define	FDO_FRST	0x04	/*  floppy controller reset */
2381b67be7bSPoul-Henning Kamp #define	FDO_FDMAEN	0x08	/*  enable floppy DMA and Interrupt */
2391b67be7bSPoul-Henning Kamp #define	FDO_MOEN0	0x10	/*  motor enable drive 0 */
2401b67be7bSPoul-Henning Kamp #define	FDO_MOEN1	0x20	/*  motor enable drive 1 */
2411b67be7bSPoul-Henning Kamp #define	FDO_MOEN2	0x40	/*  motor enable drive 2 */
2421b67be7bSPoul-Henning Kamp #define	FDO_MOEN3	0x80	/*  motor enable drive 3 */
2431b67be7bSPoul-Henning Kamp 
2441b67be7bSPoul-Henning Kamp #define	FDSTS	4	/* NEC 765 Main Status Register (R) */
24550d99d1aSJung-uk Kim #define FDDSR	4	/* Data Rate Select Register (W) */
2461b67be7bSPoul-Henning Kamp #define	FDDATA	5	/* NEC 765 Data Register (R/W) */
2471b67be7bSPoul-Henning Kamp #define	FDCTL	7	/* Control Register (W) */
24883e78a7fSJohn Baldwin #endif /* PC98 */
2491b67be7bSPoul-Henning Kamp 
2501b67be7bSPoul-Henning Kamp /*
251973bfe6cSWarner Losh  * The YE-DATA PC Card floppies use PIO to read in the data rather
252973bfe6cSWarner Losh  * than DMA due to the wild variability of DMA for the PC Card
253973bfe6cSWarner Losh  * devices.  DMA was deleted from the PC Card specification in version
254973bfe6cSWarner Losh  * 7.2 of the standard, but that post-dates the YE-DATA devices by many
255973bfe6cSWarner Losh  * years.
256973bfe6cSWarner Losh  *
257973bfe6cSWarner Losh  * In addition, if we cannot setup the DMA resources for the ISA
258973bfe6cSWarner Losh  * attachment, we'll use this same offset for data transfer.  However,
259973bfe6cSWarner Losh  * that almost certainly won't work.
260b4046cd7SWarner Losh  *
261b4046cd7SWarner Losh  * For this mode, offset 0 and 1 must be used to setup the transfer
262973bfe6cSWarner Losh  * for this floppy.  This is OK for PC Card YE Data devices, but for
263973bfe6cSWarner Losh  * ISA this is likely wrong.  These registers are only available on
264973bfe6cSWarner Losh  * those systems that map them to the floppy drive.  Newer systems do
265973bfe6cSWarner Losh  * not do this, and we should likely prohibit access to them (or
266973bfe6cSWarner Losh  * disallow NODMA to be set).
2671b67be7bSPoul-Henning Kamp  */
268b4046cd7SWarner Losh #define FDBCDR		0	/* And 1 */
269b4046cd7SWarner Losh #define FD_YE_DATAPORT	6	/* Drive Data port */
2701b67be7bSPoul-Henning Kamp 
27183e78a7fSJohn Baldwin #ifndef PC98
2721b67be7bSPoul-Henning Kamp #define	FDI_DCHG	0x80	/* diskette has been changed */
2731b67be7bSPoul-Henning Kamp 				/* requires drive and motor being selected */
2741b67be7bSPoul-Henning Kamp 				/* is cleared by any step pulse to drive */
27583e78a7fSJohn Baldwin #endif
2761b67be7bSPoul-Henning Kamp 
2771b67be7bSPoul-Henning Kamp /*
27861909e4cSPoul-Henning Kamp  * We have three private BIO commands.
2791b67be7bSPoul-Henning Kamp  */
2801b67be7bSPoul-Henning Kamp #define BIO_PROBE	BIO_CMD0
2811b67be7bSPoul-Henning Kamp #define BIO_RDID	BIO_CMD1
2821b67be7bSPoul-Henning Kamp #define BIO_FMT		BIO_CMD2
2835b81b6b3SRodney W. Grimes 
284246ed35dSJoerg Wunsch /*
285246ed35dSJoerg Wunsch  * Per drive structure (softc).
286246ed35dSJoerg Wunsch  */
2876182fdbdSPeter Wemm struct fd_data {
2881b67be7bSPoul-Henning Kamp 	u_char 	*fd_ioptr;	/* IO pointer */
2891b67be7bSPoul-Henning Kamp 	u_int	fd_iosize;	/* Size of IO chunks */
2901b67be7bSPoul-Henning Kamp 	u_int	fd_iocount;	/* Outstanding requests */
291b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2925b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2931a6bed68SJoerg Wunsch 	enum	fd_drivetype type; /* drive type */
2941a6bed68SJoerg Wunsch 	struct	fd_type *ft;	/* pointer to current type descriptor */
2951b67be7bSPoul-Henning Kamp 	struct	fd_type fts;	/* type descriptors */
2961b67be7bSPoul-Henning Kamp 	int	sectorsize;
2975b81b6b3SRodney W. Grimes 	int	flags;
2981b67be7bSPoul-Henning Kamp #define	FD_WP		(1<<0)	/* Write protected	*/
2991b67be7bSPoul-Henning Kamp #define	FD_MOTOR	(1<<1)	/* motor should be on	*/
3001b67be7bSPoul-Henning Kamp #define	FD_MOTORWAIT	(1<<2)	/* motor should be on	*/
3011b67be7bSPoul-Henning Kamp #define	FD_EMPTY	(1<<3)	/* no media		*/
3021b67be7bSPoul-Henning Kamp #define	FD_NEWDISK	(1<<4)	/* media changed	*/
3031b67be7bSPoul-Henning Kamp #define	FD_ISADMA	(1<<5)	/* isa dma started 	*/
3045b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
3051b67be7bSPoul-Henning Kamp #define FD_NO_TRACK	 -2
3061b67be7bSPoul-Henning Kamp 	int	options;	/* FDOPT_* */
3071b67be7bSPoul-Henning Kamp 	struct	callout toffhandle;
3081b67be7bSPoul-Henning Kamp 	struct g_geom *fd_geom;
3091b67be7bSPoul-Henning Kamp 	struct g_provider *fd_provider;
3106182fdbdSPeter Wemm 	device_t dev;
3111b67be7bSPoul-Henning Kamp 	struct bio_queue_head fd_bq;
31283e78a7fSJohn Baldwin #ifdef PC98
31383e78a7fSJohn Baldwin 	int	pc98_trans;
31483e78a7fSJohn Baldwin #endif
3156182fdbdSPeter Wemm };
31637286586SPeter Wemm 
3171b67be7bSPoul-Henning Kamp #define FD_NOT_VALID -2
318752d4735SNate Lawson 
319246ed35dSJoerg Wunsch static driver_intr_t fdc_intr;
320ef544f63SPaolo Pisati static driver_filter_t fdc_intr_fast;
3211b67be7bSPoul-Henning Kamp static void fdc_reset(struct fdc_data *);
322c884498fSJung-uk Kim static int fd_probe_disk(struct fd_data *, int *);
323d66c374fSTor Egge 
3246472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
325d66c374fSTor Egge 
3261b67be7bSPoul-Henning Kamp static int fifo_threshold = 8;
3271b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
3281b67be7bSPoul-Henning Kamp 	"FIFO threshold setting");
3291b67be7bSPoul-Henning Kamp 
3301b67be7bSPoul-Henning Kamp static int debugflags = 0;
3311b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
3321b67be7bSPoul-Henning Kamp 	"Debug flags");
3331b67be7bSPoul-Henning Kamp 
3341b67be7bSPoul-Henning Kamp static int retries = 10;
3351b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
3361b67be7bSPoul-Henning Kamp 	"Number of retries to attempt");
3371b67be7bSPoul-Henning Kamp 
33883e78a7fSJohn Baldwin #ifdef PC98
33983e78a7fSJohn Baldwin static int spec1 = NE7_SPEC_1(4, 240);
34083e78a7fSJohn Baldwin #else
34183e78a7fSJohn Baldwin static int spec1 = NE7_SPEC_1(6, 240);
34283e78a7fSJohn Baldwin #endif
3431b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
3441b67be7bSPoul-Henning Kamp 	"Specification byte one (step-rate + head unload)");
3451b67be7bSPoul-Henning Kamp 
34683e78a7fSJohn Baldwin #ifdef PC98
34783e78a7fSJohn Baldwin static int spec2 = NE7_SPEC_2(2, 0);
34883e78a7fSJohn Baldwin #else
34983e78a7fSJohn Baldwin static int spec2 = NE7_SPEC_2(16, 0);
35083e78a7fSJohn Baldwin #endif
3511b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
3521b67be7bSPoul-Henning Kamp 	"Specification byte two (head load time + no-dma)");
3531b67be7bSPoul-Henning Kamp 
3541b67be7bSPoul-Henning Kamp static int settle;
3551b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
3561b67be7bSPoul-Henning Kamp 	"Head settling time in sec/hz");
3571b67be7bSPoul-Henning Kamp 
3581b67be7bSPoul-Henning Kamp static void
3591b67be7bSPoul-Henning Kamp fdprinttype(struct fd_type *ft)
3601b67be7bSPoul-Henning Kamp {
3611b67be7bSPoul-Henning Kamp 
3621b67be7bSPoul-Henning Kamp 	printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
3631b67be7bSPoul-Henning Kamp 	    ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
3641b67be7bSPoul-Henning Kamp 	    ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
3651b67be7bSPoul-Henning Kamp 	    ft->offset_side2, ft->flags);
3661b67be7bSPoul-Henning Kamp }
3671b67be7bSPoul-Henning Kamp 
3681b67be7bSPoul-Henning Kamp static void
3691b67be7bSPoul-Henning Kamp fdsettype(struct fd_data *fd, struct fd_type *ft)
3701b67be7bSPoul-Henning Kamp {
3711b67be7bSPoul-Henning Kamp 	fd->ft = ft;
3721b67be7bSPoul-Henning Kamp 	ft->size = ft->sectrac * ft->heads * ft->tracks;
3731b67be7bSPoul-Henning Kamp 	fd->sectorsize = 128 << fd->ft->secsize;
3741b67be7bSPoul-Henning Kamp }
3755b81b6b3SRodney W. Grimes 
376246ed35dSJoerg Wunsch /*
377246ed35dSJoerg Wunsch  * Bus space handling (access to low-level IO).
378246ed35dSJoerg Wunsch  */
37975da3c1aSEd Schouten static inline void
380973bfe6cSWarner Losh fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
381973bfe6cSWarner Losh {
382973bfe6cSWarner Losh 
383973bfe6cSWarner Losh 	bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
384973bfe6cSWarner Losh }
385973bfe6cSWarner Losh 
38675da3c1aSEd Schouten static inline uint8_t
387973bfe6cSWarner Losh fdregrd(struct fdc_data *fdc, int reg)
388973bfe6cSWarner Losh {
389973bfe6cSWarner Losh 
390973bfe6cSWarner Losh 	return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
391973bfe6cSWarner Losh }
392973bfe6cSWarner Losh 
3931b67be7bSPoul-Henning Kamp static void
3941b67be7bSPoul-Henning Kamp fdctl_wr(struct fdc_data *fdc, u_int8_t v)
395427ccf00SDoug Rabson {
3961b67be7bSPoul-Henning Kamp 
397973bfe6cSWarner Losh 	fdregwr(fdc, FDCTL, v);
3981b67be7bSPoul-Henning Kamp }
3991b67be7bSPoul-Henning Kamp 
40083e78a7fSJohn Baldwin #ifndef PC98
4011b67be7bSPoul-Henning Kamp static void
4021b67be7bSPoul-Henning Kamp fdout_wr(struct fdc_data *fdc, u_int8_t v)
4031b67be7bSPoul-Henning Kamp {
4041b67be7bSPoul-Henning Kamp 
405973bfe6cSWarner Losh 	fdregwr(fdc, FDOUT, v);
406427ccf00SDoug Rabson }
40783e78a7fSJohn Baldwin #endif
408427ccf00SDoug Rabson 
409427ccf00SDoug Rabson static u_int8_t
4101b67be7bSPoul-Henning Kamp fdsts_rd(struct fdc_data *fdc)
411427ccf00SDoug Rabson {
4121b67be7bSPoul-Henning Kamp 
413973bfe6cSWarner Losh 	return fdregrd(fdc, FDSTS);
414427ccf00SDoug Rabson }
415427ccf00SDoug Rabson 
41683e78a7fSJohn Baldwin #ifndef PC98
417427ccf00SDoug Rabson static void
41850d99d1aSJung-uk Kim fddsr_wr(struct fdc_data *fdc, u_int8_t v)
41950d99d1aSJung-uk Kim {
42050d99d1aSJung-uk Kim 
42150d99d1aSJung-uk Kim 	fdregwr(fdc, FDDSR, v);
42250d99d1aSJung-uk Kim }
42383e78a7fSJohn Baldwin #endif
42450d99d1aSJung-uk Kim 
42550d99d1aSJung-uk Kim static void
4261b67be7bSPoul-Henning Kamp fddata_wr(struct fdc_data *fdc, u_int8_t v)
427427ccf00SDoug Rabson {
4281b67be7bSPoul-Henning Kamp 
429973bfe6cSWarner Losh 	fdregwr(fdc, FDDATA, v);
430427ccf00SDoug Rabson }
431427ccf00SDoug Rabson 
432427ccf00SDoug Rabson static u_int8_t
4331b67be7bSPoul-Henning Kamp fddata_rd(struct fdc_data *fdc)
434427ccf00SDoug Rabson {
4351b67be7bSPoul-Henning Kamp 
436973bfe6cSWarner Losh 	return fdregrd(fdc, FDDATA);
437427ccf00SDoug Rabson }
438427ccf00SDoug Rabson 
43983e78a7fSJohn Baldwin #ifndef PC98
440427ccf00SDoug Rabson static u_int8_t
4411b67be7bSPoul-Henning Kamp fdin_rd(struct fdc_data *fdc)
442427ccf00SDoug Rabson {
4431b67be7bSPoul-Henning Kamp 
444973bfe6cSWarner Losh 	return fdregrd(fdc, FDCTL);
445427ccf00SDoug Rabson }
44683e78a7fSJohn Baldwin #endif
447427ccf00SDoug Rabson 
448b4046cd7SWarner Losh /*
449b4046cd7SWarner Losh  * Magic pseudo-DMA initialization for YE FDC. Sets count and
450b4046cd7SWarner Losh  * direction.
451b4046cd7SWarner Losh  */
452b4046cd7SWarner Losh static void
453b4046cd7SWarner Losh fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
454b4046cd7SWarner Losh {
455973bfe6cSWarner Losh 	fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
456973bfe6cSWarner Losh 	fdregwr(fdc, FDBCDR + 1,
457973bfe6cSWarner Losh 	    (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
458b4046cd7SWarner Losh }
459b4046cd7SWarner Losh 
460dc5df763SJoerg Wunsch static int
4616182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
462dc5df763SJoerg Wunsch {
4636182fdbdSPeter Wemm 	fdc->fdc_errs++;
46416b04b6aSJoerg Wunsch 	if (s) {
465b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
466b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
467b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
468b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
469b6e5f28eSPeter Wemm 						    "logging any more\n");
47016b04b6aSJoerg Wunsch 	}
471dc5df763SJoerg Wunsch 
4721b67be7bSPoul-Henning Kamp 	return (1);
473dc5df763SJoerg Wunsch }
474dc5df763SJoerg Wunsch 
475dc5df763SJoerg Wunsch /*
4761b67be7bSPoul-Henning Kamp  * FDC IO functions, take care of the main status register, timeout
4771b67be7bSPoul-Henning Kamp  * in case the desired status bits are never set.
4781b67be7bSPoul-Henning Kamp  *
4791b67be7bSPoul-Henning Kamp  * These PIO loops initially start out with short delays between
4801b67be7bSPoul-Henning Kamp  * each iteration in the expectation that the required condition
4811b67be7bSPoul-Henning Kamp  * is usually met quickly, so it can be handled immediately.
482dc5df763SJoerg Wunsch  */
4831b67be7bSPoul-Henning Kamp static int
4841b67be7bSPoul-Henning Kamp fdc_in(struct fdc_data *fdc, int *ptr)
485dc5df763SJoerg Wunsch {
4861b67be7bSPoul-Henning Kamp 	int i, j, step;
4871b67be7bSPoul-Henning Kamp 
4881b67be7bSPoul-Henning Kamp 	step = 1;
4891b67be7bSPoul-Henning Kamp 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
4901b67be7bSPoul-Henning Kamp 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
4911b67be7bSPoul-Henning Kamp 	        if (i == (NE7_DIO|NE7_RQM)) {
4921b67be7bSPoul-Henning Kamp 			i = fddata_rd(fdc);
4931b67be7bSPoul-Henning Kamp 			if (ptr)
4941b67be7bSPoul-Henning Kamp 				*ptr = i;
4951b67be7bSPoul-Henning Kamp 			return (0);
4961b67be7bSPoul-Henning Kamp 		}
4971b67be7bSPoul-Henning Kamp 		if (i == NE7_RQM)
4981b67be7bSPoul-Henning Kamp 			return (fdc_err(fdc, "ready for output in input\n"));
4991b67be7bSPoul-Henning Kamp 		step += step;
5001b67be7bSPoul-Henning Kamp 		DELAY(step);
5011b67be7bSPoul-Henning Kamp 	}
5021b67be7bSPoul-Henning Kamp 	return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
5031b67be7bSPoul-Henning Kamp }
5041b67be7bSPoul-Henning Kamp 
5051b67be7bSPoul-Henning Kamp static int
5061b67be7bSPoul-Henning Kamp fdc_out(struct fdc_data *fdc, int x)
5071b67be7bSPoul-Henning Kamp {
5081b67be7bSPoul-Henning Kamp 	int i, j, step;
5091b67be7bSPoul-Henning Kamp 
5101b67be7bSPoul-Henning Kamp 	step = 1;
5111b67be7bSPoul-Henning Kamp 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
5121b67be7bSPoul-Henning Kamp 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
5131b67be7bSPoul-Henning Kamp 	        if (i == NE7_RQM) {
5141b67be7bSPoul-Henning Kamp 			fddata_wr(fdc, x);
5151b67be7bSPoul-Henning Kamp 			return (0);
5161b67be7bSPoul-Henning Kamp 		}
5171b67be7bSPoul-Henning Kamp 		if (i == (NE7_DIO|NE7_RQM))
5181b67be7bSPoul-Henning Kamp 			return (fdc_err(fdc, "ready for input in output\n"));
5191b67be7bSPoul-Henning Kamp 		step += step;
5201b67be7bSPoul-Henning Kamp 		DELAY(step);
5211b67be7bSPoul-Henning Kamp 	}
5221b67be7bSPoul-Henning Kamp 	return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
5231b67be7bSPoul-Henning Kamp }
5241b67be7bSPoul-Henning Kamp 
5251b67be7bSPoul-Henning Kamp /*
5261b67be7bSPoul-Henning Kamp  * fdc_cmd: Send a command to the chip.
5271b67be7bSPoul-Henning Kamp  * Takes a varargs with this structure:
5281b67be7bSPoul-Henning Kamp  *	# of output bytes
5291b67be7bSPoul-Henning Kamp  *	output bytes as int [...]
5301b67be7bSPoul-Henning Kamp  *	# of input bytes
5311b67be7bSPoul-Henning Kamp  *	input bytes as int* [...]
5321b67be7bSPoul-Henning Kamp  */
5331b67be7bSPoul-Henning Kamp static int
5341b67be7bSPoul-Henning Kamp fdc_cmd(struct fdc_data *fdc, int n_out, ...)
5351b67be7bSPoul-Henning Kamp {
5361b67be7bSPoul-Henning Kamp 	u_char cmd = 0;
537dc5df763SJoerg Wunsch 	int n_in;
5381b67be7bSPoul-Henning Kamp 	int n, i;
539dc5df763SJoerg Wunsch 	va_list ap;
540dc5df763SJoerg Wunsch 
541dc5df763SJoerg Wunsch 	va_start(ap, n_out);
5421b67be7bSPoul-Henning Kamp 	for (n = 0; n < n_out; n++) {
5431b67be7bSPoul-Henning Kamp 		i = va_arg(ap, int);
5441b67be7bSPoul-Henning Kamp 		if (n == 0)
5451b67be7bSPoul-Henning Kamp 			cmd = i;
5461b67be7bSPoul-Henning Kamp 		if (fdc_out(fdc, i) < 0) {
547dc5df763SJoerg Wunsch 			char msg[50];
5482127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
549dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
550dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
5511b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
552a7e943a4SPyun YongHyeon 			va_end(ap);
5536182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
554dc5df763SJoerg Wunsch 		}
555dc5df763SJoerg Wunsch 	}
556dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
5571b67be7bSPoul-Henning Kamp 	for (n = 0; n < n_in; n++) {
558dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
5591b67be7bSPoul-Henning Kamp 		if (fdc_in(fdc, ptr) < 0) {
560dc5df763SJoerg Wunsch 			char msg[50];
5612127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
562dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
563dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
5641b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
565a7e943a4SPyun YongHyeon 			va_end(ap);
5666182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
567dc5df763SJoerg Wunsch 		}
568dc5df763SJoerg Wunsch 	}
569a7e943a4SPyun YongHyeon 	va_end(ap);
5701b67be7bSPoul-Henning Kamp 	return (0);
571dc5df763SJoerg Wunsch }
572dc5df763SJoerg Wunsch 
57383e78a7fSJohn Baldwin #ifdef PC98
57483e78a7fSJohn Baldwin static void	fd_motor(struct fd_data *fd, int turnon);
57583e78a7fSJohn Baldwin 
57683e78a7fSJohn Baldwin static int pc98_trans = 0; /* 0 : HD , 1 : DD , 2 : 1.44 */
57783e78a7fSJohn Baldwin static int pc98_trans_prev = -1;
57883e78a7fSJohn Baldwin 
57983e78a7fSJohn Baldwin static void
58083e78a7fSJohn Baldwin set_density(struct fdc_data *fdc)
58183e78a7fSJohn Baldwin {
58283e78a7fSJohn Baldwin 	/* always motor on */
58383e78a7fSJohn Baldwin 	fdregwr(fdc, FDP, (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
58483e78a7fSJohn Baldwin 	DELAY(100);
58583e78a7fSJohn Baldwin 	fdctl_wr(fdc, FDC_RST | FDC_DMAE);
58683e78a7fSJohn Baldwin 	/* in the case of note W, always inhibit 100ms timer */
58783e78a7fSJohn Baldwin }
58883e78a7fSJohn Baldwin 
58983e78a7fSJohn Baldwin static int
59083e78a7fSJohn Baldwin pc98_fd_check_ready(struct fd_data *fd)
59183e78a7fSJohn Baldwin {
59283e78a7fSJohn Baldwin 	struct fdc_data *fdc = fd->fdc;
59383e78a7fSJohn Baldwin 	int retry = 0, status;
59483e78a7fSJohn Baldwin 	int fdu = device_get_unit(fd->dev);
59583e78a7fSJohn Baldwin 
59683e78a7fSJohn Baldwin 	while (retry++ < 30000) {
59783e78a7fSJohn Baldwin 		fd_motor(fd, 1);
59883e78a7fSJohn Baldwin 		fdc_out(fdc, NE7CMD_SENSED); /* Sense Drive Status */
59983e78a7fSJohn Baldwin 		DELAY(100);
60083e78a7fSJohn Baldwin 		fdc_out(fdc, fdu); /* Drive number */
60183e78a7fSJohn Baldwin 		DELAY(100);
60283e78a7fSJohn Baldwin 		if ((fdc_in(fdc, &status) == 0) && (status & NE7_ST3_RD)) {
60383e78a7fSJohn Baldwin 			fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
60483e78a7fSJohn Baldwin 			DELAY(10);
60583e78a7fSJohn Baldwin 			return (0);
60683e78a7fSJohn Baldwin 		}
60783e78a7fSJohn Baldwin 	}
60883e78a7fSJohn Baldwin 	return (-1);
60983e78a7fSJohn Baldwin }
61083e78a7fSJohn Baldwin 
61183e78a7fSJohn Baldwin static void
61283e78a7fSJohn Baldwin pc98_fd_check_type(struct fd_data *fd, int unit)
61383e78a7fSJohn Baldwin {
61483e78a7fSJohn Baldwin 	struct fdc_data *fdc;
61583e78a7fSJohn Baldwin 
61683e78a7fSJohn Baldwin 	if (fd->type != FDT_NONE || unit < 0 || unit > 3)
61783e78a7fSJohn Baldwin 		return;
61883e78a7fSJohn Baldwin 
61983e78a7fSJohn Baldwin 	fdc = fd->fdc;
62083e78a7fSJohn Baldwin 
62183e78a7fSJohn Baldwin 	/* Look up what the BIOS thinks we have. */
62283e78a7fSJohn Baldwin 	if (!((PC98_SYSTEM_PARAMETER(0x55c) >> unit) & 0x01)) {
62383e78a7fSJohn Baldwin 		fd->type = FDT_NONE;
62483e78a7fSJohn Baldwin 		return;
62583e78a7fSJohn Baldwin 	}
62683e78a7fSJohn Baldwin 	if ((PC98_SYSTEM_PARAMETER(0x5ae) >> unit) & 0x01) {
62783e78a7fSJohn Baldwin 		/* Check 3mode I/F */
62883e78a7fSJohn Baldwin 		fd->pc98_trans = 0;
62983e78a7fSJohn Baldwin 		fdregwr(fdc, FDEM, (unit << 5) | 0x10);
63083e78a7fSJohn Baldwin 		if (!(fdregrd(fdc, FDEM) & 0x01)) {
63183e78a7fSJohn Baldwin 			fd->type = FDT_144M;
63283e78a7fSJohn Baldwin 			return;
63383e78a7fSJohn Baldwin 		}
63483e78a7fSJohn Baldwin 		device_printf(fd->dev,
63583e78a7fSJohn Baldwin 		    "Warning: can't control 3mode I/F, fallback to 2mode.\n");
63683e78a7fSJohn Baldwin 	}
63783e78a7fSJohn Baldwin 
63883e78a7fSJohn Baldwin 	fd->type = FDT_12M;
63983e78a7fSJohn Baldwin }
64083e78a7fSJohn Baldwin #endif /* PC98 */
64183e78a7fSJohn Baldwin 
6421b67be7bSPoul-Henning Kamp static void
6431b67be7bSPoul-Henning Kamp fdc_reset(struct fdc_data *fdc)
644d66c374fSTor Egge {
6451b67be7bSPoul-Henning Kamp 	int i, r[10];
646d66c374fSTor Egge 
64783e78a7fSJohn Baldwin #ifdef PC98
64883e78a7fSJohn Baldwin 	set_density(fdc);
64983e78a7fSJohn Baldwin 	if (pc98_machine_type & M_EPSON_PC98)
65083e78a7fSJohn Baldwin 		fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DD | FDC_MTON);
65183e78a7fSJohn Baldwin 	else
65283e78a7fSJohn Baldwin 		fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DMAE | FDC_MTON);
65383e78a7fSJohn Baldwin 	DELAY(200);
65483e78a7fSJohn Baldwin 	fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
65583e78a7fSJohn Baldwin 	DELAY(10);
65683e78a7fSJohn Baldwin #else
65750d99d1aSJung-uk Kim 	if (fdc->fdct == FDC_ENHANCED) {
65850d99d1aSJung-uk Kim 		/* Try a software reset, default precomp, and 500 kb/s */
65950d99d1aSJung-uk Kim 		fddsr_wr(fdc, I8207X_DSR_SR);
66050d99d1aSJung-uk Kim 	} else {
66150d99d1aSJung-uk Kim 		/* Try a hardware reset, keep motor on */
6621b67be7bSPoul-Henning Kamp 		fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
6631b67be7bSPoul-Henning Kamp 		DELAY(100);
6641b67be7bSPoul-Henning Kamp 		/* enable FDC, but defer interrupts a moment */
6651b67be7bSPoul-Henning Kamp 		fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
66619a388e6SJung-uk Kim 	}
6671b67be7bSPoul-Henning Kamp 	DELAY(100);
6681b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
66983e78a7fSJohn Baldwin #endif
670d66c374fSTor Egge 
6711b67be7bSPoul-Henning Kamp 	/* XXX after a reset, silently believe the FDC will accept commands */
6721b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
6731b67be7bSPoul-Henning Kamp 		device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
674d66c374fSTor Egge 
6751b67be7bSPoul-Henning Kamp 	if (fdc->fdct == FDC_ENHANCED) {
6761b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 4,
6770da90eb8SJung-uk Kim 		    I8207X_CONFIG,
6781b67be7bSPoul-Henning Kamp 		    0,
6791f97b64cSJoerg Wunsch 		    /* 0x40 | */		/* Enable Implied Seek -
6801f97b64cSJoerg Wunsch 						 * breaks 2step! */
6811b67be7bSPoul-Henning Kamp 		    0x10 |			/* Polling disabled */
6821b67be7bSPoul-Henning Kamp 		    (fifo_threshold - 1),	/* Fifo threshold */
6831b67be7bSPoul-Henning Kamp 		    0x00,			/* Precomp track */
6841b67be7bSPoul-Henning Kamp 		    0))
6851b67be7bSPoul-Henning Kamp 			device_printf(fdc->fdc_dev,
6861b67be7bSPoul-Henning Kamp 			    " CONFIGURE failed in reset\n");
6871b67be7bSPoul-Henning Kamp 		if (debugflags & 1) {
6881b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 1,
6890da90eb8SJung-uk Kim 			    I8207X_DUMPREG,
6901b67be7bSPoul-Henning Kamp 			    10, &r[0], &r[1], &r[2], &r[3], &r[4],
6911b67be7bSPoul-Henning Kamp 			    &r[5], &r[6], &r[7], &r[8], &r[9]))
6921b67be7bSPoul-Henning Kamp 				device_printf(fdc->fdc_dev,
6931b67be7bSPoul-Henning Kamp 				    " DUMPREG failed in reset\n");
6941b67be7bSPoul-Henning Kamp 			for (i = 0; i < 10; i++)
6951b67be7bSPoul-Henning Kamp 				printf(" %02x", r[i]);
6961b67be7bSPoul-Henning Kamp 			printf("\n");
697d66c374fSTor Egge 		}
698d0900d6bSJoerg Wunsch 	}
699d66c374fSTor Egge }
700d66c374fSTor Egge 
701d66c374fSTor Egge static int
7021b67be7bSPoul-Henning Kamp fdc_sense_drive(struct fdc_data *fdc, int *st3p)
703dc5df763SJoerg Wunsch {
704dc5df763SJoerg Wunsch 	int st3;
705dc5df763SJoerg Wunsch 
7061b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
7071b67be7bSPoul-Henning Kamp 		return (fdc_err(fdc, "Sense Drive Status failed\n"));
708dc5df763SJoerg Wunsch 	if (st3p)
709dc5df763SJoerg Wunsch 		*st3p = st3;
7101b67be7bSPoul-Henning Kamp 	return (0);
711dc5df763SJoerg Wunsch }
712dc5df763SJoerg Wunsch 
7136f4e0bebSPoul-Henning Kamp static int
7141b67be7bSPoul-Henning Kamp fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
715dc5df763SJoerg Wunsch {
7166182fdbdSPeter Wemm 	int cyl, st0, ret;
717dc5df763SJoerg Wunsch 
7181b67be7bSPoul-Henning Kamp 	ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
7196182fdbdSPeter Wemm 	if (ret) {
7201b67be7bSPoul-Henning Kamp 		(void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
7211b67be7bSPoul-Henning Kamp 		return (ret);
722dc5df763SJoerg Wunsch 	}
723dc5df763SJoerg Wunsch 
724dc5df763SJoerg Wunsch 	if (st0p)
725dc5df763SJoerg Wunsch 		*st0p = st0;
726dc5df763SJoerg Wunsch 
7276182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
728dc5df763SJoerg Wunsch 		/*
729dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
730dc5df763SJoerg Wunsch 		 */
7311b67be7bSPoul-Henning Kamp 		return (FD_NOT_VALID);
732dc5df763SJoerg Wunsch 	}
733dc5df763SJoerg Wunsch 
7341b67be7bSPoul-Henning Kamp 	if (fdc_in(fdc, &cyl) < 0)
7356182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
736dc5df763SJoerg Wunsch 
737dc5df763SJoerg Wunsch 	if (cylp)
738dc5df763SJoerg Wunsch 		*cylp = cyl;
739dc5df763SJoerg Wunsch 
7401b67be7bSPoul-Henning Kamp 	return (0);
741dc5df763SJoerg Wunsch }
742dc5df763SJoerg Wunsch 
7436f4e0bebSPoul-Henning Kamp static int
7441b67be7bSPoul-Henning Kamp fdc_read_status(struct fdc_data *fdc)
745dc5df763SJoerg Wunsch {
7461b67be7bSPoul-Henning Kamp 	int i, ret, status;
747b5e8ce9fSBruce Evans 
748246ed35dSJoerg Wunsch 	for (i = ret = 0; i < 7; i++) {
7491b67be7bSPoul-Henning Kamp 		ret = fdc_in(fdc, &status);
750b5e8ce9fSBruce Evans 		fdc->status[i] = status;
751b5e8ce9fSBruce Evans 		if (ret != 0)
752dc5df763SJoerg Wunsch 			break;
753dc5df763SJoerg Wunsch 	}
754dc5df763SJoerg Wunsch 
755dc5df763SJoerg Wunsch 	if (ret == 0)
756dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
757dc5df763SJoerg Wunsch 	else
758dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
759dc5df763SJoerg Wunsch 
760dc5df763SJoerg Wunsch 	return ret;
761dc5df763SJoerg Wunsch }
762dc5df763SJoerg Wunsch 
76383e78a7fSJohn Baldwin #ifndef PC98
7641b67be7bSPoul-Henning Kamp /*
7651b67be7bSPoul-Henning Kamp  * Select this drive
7661b67be7bSPoul-Henning Kamp  */
7671b67be7bSPoul-Henning Kamp static void
7681b67be7bSPoul-Henning Kamp fd_select(struct fd_data *fd)
7691b67be7bSPoul-Henning Kamp {
7701b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
7711b67be7bSPoul-Henning Kamp 
7721b67be7bSPoul-Henning Kamp 	/* XXX: lock controller */
7731b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
7741b67be7bSPoul-Henning Kamp 	fdc->fdout &= ~FDO_FDSEL;
7751b67be7bSPoul-Henning Kamp 	fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
7761b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
7771b67be7bSPoul-Henning Kamp }
7781b67be7bSPoul-Henning Kamp 
7791b67be7bSPoul-Henning Kamp static void
7801b67be7bSPoul-Henning Kamp fd_turnon(void *arg)
7811b67be7bSPoul-Henning Kamp {
7821b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
7831b67be7bSPoul-Henning Kamp 	struct bio *bp;
7841b67be7bSPoul-Henning Kamp 	int once;
7851b67be7bSPoul-Henning Kamp 
7861b67be7bSPoul-Henning Kamp 	fd = arg;
7879fc9d84bSJohn Baldwin 	mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
7881b67be7bSPoul-Henning Kamp 	fd->flags &= ~FD_MOTORWAIT;
7891b67be7bSPoul-Henning Kamp 	fd->flags |= FD_MOTOR;
7901b67be7bSPoul-Henning Kamp 	once = 0;
7911b67be7bSPoul-Henning Kamp 	for (;;) {
7921b67be7bSPoul-Henning Kamp 		bp = bioq_takefirst(&fd->fd_bq);
7931b67be7bSPoul-Henning Kamp 		if (bp == NULL)
7941b67be7bSPoul-Henning Kamp 			break;
7951b67be7bSPoul-Henning Kamp 		bioq_disksort(&fd->fdc->head, bp);
7961b67be7bSPoul-Henning Kamp 		once = 1;
7971b67be7bSPoul-Henning Kamp 	}
7981b67be7bSPoul-Henning Kamp 	if (once)
7991b67be7bSPoul-Henning Kamp 		wakeup(&fd->fdc->head);
8001b67be7bSPoul-Henning Kamp }
80183e78a7fSJohn Baldwin #endif
8021b67be7bSPoul-Henning Kamp 
8031b67be7bSPoul-Henning Kamp static void
8041b67be7bSPoul-Henning Kamp fd_motor(struct fd_data *fd, int turnon)
8051b67be7bSPoul-Henning Kamp {
8061b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
8071b67be7bSPoul-Henning Kamp 
8081b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
8091b67be7bSPoul-Henning Kamp /*
8101b67be7bSPoul-Henning Kamp 	mtx_assert(&fdc->fdc_mtx, MA_OWNED);
8111b67be7bSPoul-Henning Kamp */
81283e78a7fSJohn Baldwin #ifdef PC98
81383e78a7fSJohn Baldwin 	fdregwr(fdc, FDP, (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
81483e78a7fSJohn Baldwin 	DELAY(10);
81583e78a7fSJohn Baldwin 	fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
81683e78a7fSJohn Baldwin #else
8171b67be7bSPoul-Henning Kamp 	if (turnon) {
8181b67be7bSPoul-Henning Kamp 		fd->flags |= FD_MOTORWAIT;
8191b67be7bSPoul-Henning Kamp 		fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
8201b67be7bSPoul-Henning Kamp 		callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
8211b67be7bSPoul-Henning Kamp 	} else {
8229fc9d84bSJohn Baldwin 		callout_stop(&fd->toffhandle);
8231b67be7bSPoul-Henning Kamp 		fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
8241b67be7bSPoul-Henning Kamp 		fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
8251b67be7bSPoul-Henning Kamp 	}
8261b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
82783e78a7fSJohn Baldwin #endif
8281b67be7bSPoul-Henning Kamp }
8291b67be7bSPoul-Henning Kamp 
8301b67be7bSPoul-Henning Kamp static void
8311b67be7bSPoul-Henning Kamp fd_turnoff(void *xfd)
8321b67be7bSPoul-Henning Kamp {
8331b67be7bSPoul-Henning Kamp 	struct fd_data *fd = xfd;
8341b67be7bSPoul-Henning Kamp 
8359fc9d84bSJohn Baldwin 	mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
8361b67be7bSPoul-Henning Kamp 	fd_motor(fd, 0);
8371b67be7bSPoul-Henning Kamp }
8381b67be7bSPoul-Henning Kamp 
8391b67be7bSPoul-Henning Kamp /*
84061909e4cSPoul-Henning Kamp  * fdc_intr - wake up the worker thread.
8411b67be7bSPoul-Henning Kamp  */
84261909e4cSPoul-Henning Kamp 
8431b67be7bSPoul-Henning Kamp static void
8441b67be7bSPoul-Henning Kamp fdc_intr(void *arg)
8451b67be7bSPoul-Henning Kamp {
8461b67be7bSPoul-Henning Kamp 
8471b67be7bSPoul-Henning Kamp 	wakeup(arg);
8481b67be7bSPoul-Henning Kamp }
8491b67be7bSPoul-Henning Kamp 
850ef544f63SPaolo Pisati static int
851ef544f63SPaolo Pisati fdc_intr_fast(void *arg)
852ef544f63SPaolo Pisati {
853ef544f63SPaolo Pisati 
854ef544f63SPaolo Pisati 	wakeup(arg);
855ef544f63SPaolo Pisati 	return(FILTER_HANDLED);
856ef544f63SPaolo Pisati }
857ef544f63SPaolo Pisati 
8581b67be7bSPoul-Henning Kamp /*
8591b67be7bSPoul-Henning Kamp  * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
8601b67be7bSPoul-Henning Kamp  */
8611b67be7bSPoul-Henning Kamp static void
8621b67be7bSPoul-Henning Kamp fdc_pio(struct fdc_data *fdc)
8631b67be7bSPoul-Henning Kamp {
8641b67be7bSPoul-Henning Kamp 	u_char *cptr;
8651b67be7bSPoul-Henning Kamp 	struct bio *bp;
8661b67be7bSPoul-Henning Kamp 	u_int count;
8671b67be7bSPoul-Henning Kamp 
8681b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
8691b67be7bSPoul-Henning Kamp 	cptr = fdc->fd->fd_ioptr;
8701b67be7bSPoul-Henning Kamp 	count = fdc->fd->fd_iosize;
8711b67be7bSPoul-Henning Kamp 
8721b67be7bSPoul-Henning Kamp 	if (bp->bio_cmd == BIO_READ) {
873b4046cd7SWarner Losh 		fdbcdr_wr(fdc, 0, count);
874973bfe6cSWarner Losh 		bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
875973bfe6cSWarner Losh 		    fdc->ioff[FD_YE_DATAPORT], cptr, count);
8761b67be7bSPoul-Henning Kamp 	} else {
877973bfe6cSWarner Losh 		bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
878973bfe6cSWarner Losh 		    fdc->ioff[FD_YE_DATAPORT], cptr, count);
879b4046cd7SWarner Losh 		fdbcdr_wr(fdc, 0, count);	/* needed? */
8801b67be7bSPoul-Henning Kamp 	}
8811b67be7bSPoul-Henning Kamp }
8821b67be7bSPoul-Henning Kamp 
8831b67be7bSPoul-Henning Kamp static int
8841b67be7bSPoul-Henning Kamp fdc_biodone(struct fdc_data *fdc, int error)
8851b67be7bSPoul-Henning Kamp {
8861b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
8871b67be7bSPoul-Henning Kamp 	struct bio *bp;
8881b67be7bSPoul-Henning Kamp 
8891b67be7bSPoul-Henning Kamp 	fd = fdc->fd;
8901b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
8911b67be7bSPoul-Henning Kamp 
8921b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
8931b67be7bSPoul-Henning Kamp 	if (--fd->fd_iocount == 0)
8941b67be7bSPoul-Henning Kamp 		callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
8951b67be7bSPoul-Henning Kamp 	fdc->bp = NULL;
8961b67be7bSPoul-Henning Kamp 	fdc->fd = NULL;
8971b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
8981b67be7bSPoul-Henning Kamp 	if (bp->bio_to != NULL) {
8991b67be7bSPoul-Henning Kamp 		if ((debugflags & 2) && fd->fdc->retry > 0)
9001b67be7bSPoul-Henning Kamp 			printf("retries: %d\n", fd->fdc->retry);
9011b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, error);
9021b67be7bSPoul-Henning Kamp 		return (0);
9031b67be7bSPoul-Henning Kamp 	}
9041b67be7bSPoul-Henning Kamp 	bp->bio_error = error;
9051b67be7bSPoul-Henning Kamp 	bp->bio_flags |= BIO_DONE;
9061b67be7bSPoul-Henning Kamp 	wakeup(bp);
9071b67be7bSPoul-Henning Kamp 	return (0);
9081b67be7bSPoul-Henning Kamp }
9091b67be7bSPoul-Henning Kamp 
9101b67be7bSPoul-Henning Kamp static int retry_line;
9111b67be7bSPoul-Henning Kamp 
9121b67be7bSPoul-Henning Kamp static int
9131b67be7bSPoul-Henning Kamp fdc_worker(struct fdc_data *fdc)
9141b67be7bSPoul-Henning Kamp {
9151b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
9161b67be7bSPoul-Henning Kamp 	struct bio *bp;
9171b67be7bSPoul-Henning Kamp 	int i, nsect;
9181b67be7bSPoul-Henning Kamp 	int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
9191b67be7bSPoul-Henning Kamp 	int head;
920acb3b5d2SJoerg Wunsch 	int override_error;
9211b67be7bSPoul-Henning Kamp 	static int need_recal;
9221b67be7bSPoul-Henning Kamp 	struct fdc_readid *idp;
9231b67be7bSPoul-Henning Kamp 	struct fd_formb *finfo;
9241b67be7bSPoul-Henning Kamp 
925acb3b5d2SJoerg Wunsch 	override_error = 0;
926acb3b5d2SJoerg Wunsch 
9271b67be7bSPoul-Henning Kamp 	/* Have we exhausted our retries ? */
9281b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
9291b67be7bSPoul-Henning Kamp 	fd = fdc->fd;
9301b67be7bSPoul-Henning Kamp 	if (bp != NULL &&
9311217bf6aSCraig Rodrigues 		(fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
9321b67be7bSPoul-Henning Kamp 		if ((debugflags & 4))
9331b67be7bSPoul-Henning Kamp 			printf("Too many retries (EIO)\n");
934c884498fSJung-uk Kim 		if (fdc->flags & FDC_NEEDS_RESET) {
935b31da03fSJung-uk Kim 			mtx_lock(&fdc->fdc_mtx);
936b31da03fSJung-uk Kim 			fd->flags |= FD_EMPTY;
937b31da03fSJung-uk Kim 			mtx_unlock(&fdc->fdc_mtx);
938c884498fSJung-uk Kim 		}
9391b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, EIO));
9401b67be7bSPoul-Henning Kamp 	}
9411b67be7bSPoul-Henning Kamp 
9421b67be7bSPoul-Henning Kamp 	/* Disable ISADMA if we bailed while it was active */
9431b67be7bSPoul-Henning Kamp 	if (fd != NULL && (fd->flags & FD_ISADMA)) {
9441b67be7bSPoul-Henning Kamp 		isa_dmadone(
94580139618SWarner Losh 		    bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
9461b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
94761909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
9481b67be7bSPoul-Henning Kamp 		fd->flags &= ~FD_ISADMA;
94961909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
9501b67be7bSPoul-Henning Kamp 	}
9511b67be7bSPoul-Henning Kamp 
9521b67be7bSPoul-Henning Kamp 	/* Unwedge the controller ? */
9531b67be7bSPoul-Henning Kamp 	if (fdc->flags & FDC_NEEDS_RESET) {
9541b67be7bSPoul-Henning Kamp 		fdc->flags &= ~FDC_NEEDS_RESET;
9551b67be7bSPoul-Henning Kamp 		fdc_reset(fdc);
9566e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdcrst", hz);
9571b67be7bSPoul-Henning Kamp 		/* Discard results */
9581b67be7bSPoul-Henning Kamp 		for (i = 0; i < 4; i++)
9591b67be7bSPoul-Henning Kamp 			fdc_sense_int(fdc, &st0, &cyl);
9601b67be7bSPoul-Henning Kamp 		/* All drives must recal */
9611b67be7bSPoul-Henning Kamp 		need_recal = 0xf;
9621b67be7bSPoul-Henning Kamp 	}
9631b67be7bSPoul-Henning Kamp 
9641b67be7bSPoul-Henning Kamp 	/* Pick up a request, if need be wait for it */
9651b67be7bSPoul-Henning Kamp 	if (fdc->bp == NULL) {
9661b67be7bSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
9671b67be7bSPoul-Henning Kamp 		do {
9681b67be7bSPoul-Henning Kamp 			fdc->bp = bioq_takefirst(&fdc->head);
9691b67be7bSPoul-Henning Kamp 			if (fdc->bp == NULL)
9701b67be7bSPoul-Henning Kamp 				msleep(&fdc->head, &fdc->fdc_mtx,
971*25f98057SJohn Baldwin 				    PRIBIO, "-", 0);
97202910eeeSJoerg Wunsch 		} while (fdc->bp == NULL &&
97302910eeeSJoerg Wunsch 		    (fdc->flags & FDC_KTHREAD_EXIT) == 0);
9741b67be7bSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
97502910eeeSJoerg Wunsch 
97602910eeeSJoerg Wunsch 		if (fdc->bp == NULL)
97702910eeeSJoerg Wunsch 			/*
97802910eeeSJoerg Wunsch 			 * Nothing to do, worker thread has been
97902910eeeSJoerg Wunsch 			 * requested to stop.
98002910eeeSJoerg Wunsch 			 */
98102910eeeSJoerg Wunsch 			return (0);
98202910eeeSJoerg Wunsch 
9831b67be7bSPoul-Henning Kamp 		bp = fdc->bp;
9841b67be7bSPoul-Henning Kamp 		fd = fdc->fd = bp->bio_driver1;
9851b67be7bSPoul-Henning Kamp 		fdc->retry = 0;
9861b67be7bSPoul-Henning Kamp 		fd->fd_ioptr = bp->bio_data;
98780139618SWarner Losh 		if (bp->bio_cmd == BIO_FMT) {
9881b67be7bSPoul-Henning Kamp 			i = offsetof(struct fd_formb, fd_formb_cylno(0));
9891b67be7bSPoul-Henning Kamp 			fd->fd_ioptr += i;
9901b67be7bSPoul-Henning Kamp 			fd->fd_iosize = bp->bio_length - i;
9911b67be7bSPoul-Henning Kamp 		}
9921b67be7bSPoul-Henning Kamp 	}
9931b67be7bSPoul-Henning Kamp 
9941b67be7bSPoul-Henning Kamp 	/* Select drive, setup params */
99583e78a7fSJohn Baldwin #ifdef PC98
99683e78a7fSJohn Baldwin 	pc98_trans = fd->ft->trans;
99783e78a7fSJohn Baldwin 	if (pc98_trans_prev != pc98_trans) {
99883e78a7fSJohn Baldwin 		int i;
99983e78a7fSJohn Baldwin 
100083e78a7fSJohn Baldwin 		set_density(fdc);
100183e78a7fSJohn Baldwin 		for (i = 0; i < 10; i++) {
100283e78a7fSJohn Baldwin 			outb(0x5f, 0);
100383e78a7fSJohn Baldwin 			outb(0x5f, 0);
100483e78a7fSJohn Baldwin 		}
100583e78a7fSJohn Baldwin 		pc98_trans_prev = pc98_trans;
100683e78a7fSJohn Baldwin 	}
100783e78a7fSJohn Baldwin 	if (pc98_trans != fd->pc98_trans) {
100883e78a7fSJohn Baldwin 		if (fd->type == FDT_144M) {
100983e78a7fSJohn Baldwin 			fdregwr(fdc, FDEM,
101083e78a7fSJohn Baldwin 			    (device_get_unit(fd->dev) << 5) | 0x10 |
101183e78a7fSJohn Baldwin 			    (pc98_trans >> 1));
101283e78a7fSJohn Baldwin 			outb(0x5f, 0);
101383e78a7fSJohn Baldwin 			outb(0x5f, 0);
101483e78a7fSJohn Baldwin 		}
101583e78a7fSJohn Baldwin 		fd->pc98_trans = pc98_trans;
101683e78a7fSJohn Baldwin 	}
101783e78a7fSJohn Baldwin #else
10181b67be7bSPoul-Henning Kamp 	fd_select(fd);
101950d99d1aSJung-uk Kim 	if (fdc->fdct == FDC_ENHANCED)
102050d99d1aSJung-uk Kim 		fddsr_wr(fdc, fd->ft->trans);
102150d99d1aSJung-uk Kim 	else
10221b67be7bSPoul-Henning Kamp 		fdctl_wr(fdc, fd->ft->trans);
102383e78a7fSJohn Baldwin #endif
10241b67be7bSPoul-Henning Kamp 
102580139618SWarner Losh 	if (bp->bio_cmd == BIO_PROBE) {
1026c884498fSJung-uk Kim 		if ((!(device_get_flags(fd->dev) & FD_NO_CHLINE) &&
102783e78a7fSJohn Baldwin #ifndef PC98
1028c884498fSJung-uk Kim 		    !(fdin_rd(fdc) & FDI_DCHG) &&
102983e78a7fSJohn Baldwin #endif
1030c884498fSJung-uk Kim 		    !(fd->flags & FD_EMPTY)) ||
1031c884498fSJung-uk Kim 		    fd_probe_disk(fd, &need_recal) == 0)
10321b67be7bSPoul-Henning Kamp 			return (fdc_biodone(fdc, 0));
10331b67be7bSPoul-Henning Kamp 		return (1);
10341b67be7bSPoul-Henning Kamp 	}
10351b67be7bSPoul-Henning Kamp 
10361b67be7bSPoul-Henning Kamp 	/*
10371b67be7bSPoul-Henning Kamp 	 * If we are dead just flush the requests
10381b67be7bSPoul-Henning Kamp 	 */
10391b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_EMPTY)
10401b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, ENXIO));
10411b67be7bSPoul-Henning Kamp 
104283e78a7fSJohn Baldwin #ifndef PC98
10431b67be7bSPoul-Henning Kamp 	/* Check if we lost our media */
10441b67be7bSPoul-Henning Kamp 	if (fdin_rd(fdc) & FDI_DCHG) {
10451b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
10461b67be7bSPoul-Henning Kamp 			printf("Lost disk\n");
104761909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
10481b67be7bSPoul-Henning Kamp 		fd->flags |= FD_EMPTY;
10491b67be7bSPoul-Henning Kamp 		fd->flags |= FD_NEWDISK;
105061909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
10511b67be7bSPoul-Henning Kamp 		g_topology_lock();
10527c3a3fbeSEdward Tomasz Napierala 		g_orphan_provider(fd->fd_provider, ENXIO);
10531b67be7bSPoul-Henning Kamp 		fd->fd_provider->flags |= G_PF_WITHER;
10541b67be7bSPoul-Henning Kamp 		fd->fd_provider =
105502c62349SJaakko Heinonen 		    g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
10561b67be7bSPoul-Henning Kamp 		g_error_provider(fd->fd_provider, 0);
10571b67be7bSPoul-Henning Kamp 		g_topology_unlock();
10581b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, ENXIO));
10591b67be7bSPoul-Henning Kamp 	}
106083e78a7fSJohn Baldwin #endif
10611b67be7bSPoul-Henning Kamp 
10621b67be7bSPoul-Henning Kamp 	/* Check if the floppy is write-protected */
106380139618SWarner Losh 	if (bp->bio_cmd == BIO_FMT || bp->bio_cmd == BIO_WRITE) {
10641b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10651b67be7bSPoul-Henning Kamp 		if(fdc_sense_drive(fdc, &st3) != 0)
10661b67be7bSPoul-Henning Kamp 			return (1);
10671b67be7bSPoul-Henning Kamp 		if(st3 & NE7_ST3_WP)
10681b67be7bSPoul-Henning Kamp 			return (fdc_biodone(fdc, EROFS));
10691b67be7bSPoul-Henning Kamp 	}
10701b67be7bSPoul-Henning Kamp 
10711b67be7bSPoul-Henning Kamp 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
10721b67be7bSPoul-Henning Kamp 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
10731b67be7bSPoul-Henning Kamp 	i = fd->ft->sectrac * fd->ft->heads;
10741b67be7bSPoul-Henning Kamp 	cylinder = bp->bio_pblkno / i;
10751b67be7bSPoul-Henning Kamp 	descyl = cylinder * steptrac;
10761b67be7bSPoul-Henning Kamp 	sec = bp->bio_pblkno % i;
10771b67be7bSPoul-Henning Kamp 	nsect = i - sec;
10781b67be7bSPoul-Henning Kamp 	head = sec / fd->ft->sectrac;
10791b67be7bSPoul-Henning Kamp 	sec = sec % fd->ft->sectrac + 1;
10801b67be7bSPoul-Henning Kamp 
10811b67be7bSPoul-Henning Kamp 	/* If everything is going swimmingly, use multisector xfer */
108280139618SWarner Losh 	if (fdc->retry == 0 &&
108380139618SWarner Losh 	    (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
10841b67be7bSPoul-Henning Kamp 		fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
10851b67be7bSPoul-Henning Kamp 		nsect = fd->fd_iosize / fd->sectorsize;
108680139618SWarner Losh 	} else if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
10871b67be7bSPoul-Henning Kamp 		fd->fd_iosize = fd->sectorsize;
10881b67be7bSPoul-Henning Kamp 		nsect = 1;
10891b67be7bSPoul-Henning Kamp 	}
10901b67be7bSPoul-Henning Kamp 
10911b67be7bSPoul-Henning Kamp 	/* Do RECAL if we need to or are going to track zero anyway */
10921b67be7bSPoul-Henning Kamp 	if ((need_recal & (1 << fd->fdsu)) ||
10931b67be7bSPoul-Henning Kamp 	    (cylinder == 0 && fd->track != 0) ||
10941b67be7bSPoul-Henning Kamp 	    fdc->retry > 2) {
109583e78a7fSJohn Baldwin #ifdef PC98
109683e78a7fSJohn Baldwin 		pc98_fd_check_ready(fd);
109783e78a7fSJohn Baldwin #endif
10981b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10991b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
11001b67be7bSPoul-Henning Kamp 			return (1);
11016e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdrecal", hz);
11021b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11031b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
11041b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
11051b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11061b67be7bSPoul-Henning Kamp 		if ((st0 & 0xc0) || cyl != 0)
11071b67be7bSPoul-Henning Kamp 			return (1);
11081b67be7bSPoul-Henning Kamp 		need_recal &= ~(1 << fd->fdsu);
11091b67be7bSPoul-Henning Kamp 		fd->track = 0;
11101b67be7bSPoul-Henning Kamp 		/* let the heads settle */
11111b67be7bSPoul-Henning Kamp 		if (settle)
11126e50e38fSJohn Baldwin 			tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
11131b67be7bSPoul-Henning Kamp 	}
11141b67be7bSPoul-Henning Kamp 
11151b67be7bSPoul-Henning Kamp 	/*
11161b67be7bSPoul-Henning Kamp 	 * SEEK to where we want to be
11171b67be7bSPoul-Henning Kamp 	 */
11181f97b64cSJoerg Wunsch 	if (cylinder != fd->track) {
111983e78a7fSJohn Baldwin #ifdef PC98
112083e78a7fSJohn Baldwin 		pc98_fd_check_ready(fd);
112183e78a7fSJohn Baldwin #endif
11221b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11231b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
11241b67be7bSPoul-Henning Kamp 			return (1);
11256e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdseek", hz);
11261b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11271b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
11281b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
11291b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11301b67be7bSPoul-Henning Kamp 		if ((st0 & 0xc0) || cyl != descyl) {
11311b67be7bSPoul-Henning Kamp 			need_recal |= (1 << fd->fdsu);
11321b67be7bSPoul-Henning Kamp 			return (1);
11331b67be7bSPoul-Henning Kamp 		}
11341b67be7bSPoul-Henning Kamp 		/* let the heads settle */
11351b67be7bSPoul-Henning Kamp 		if (settle)
11366e50e38fSJohn Baldwin 			tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
11371b67be7bSPoul-Henning Kamp 	}
11381b67be7bSPoul-Henning Kamp 	fd->track = cylinder;
11391b67be7bSPoul-Henning Kamp 
11401b67be7bSPoul-Henning Kamp 	if (debugflags & 8)
11411b67be7bSPoul-Henning Kamp 		printf("op %x bn %ju siz %u ptr %p retry %d\n",
11421b67be7bSPoul-Henning Kamp 		    bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
11431b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fdc->retry);
11441b67be7bSPoul-Henning Kamp 
11451b67be7bSPoul-Henning Kamp 	/* Setup ISADMA if we need it and have it */
114680139618SWarner Losh 	if ((bp->bio_cmd == BIO_READ ||
114780139618SWarner Losh 		bp->bio_cmd == BIO_WRITE ||
114880139618SWarner Losh 		bp->bio_cmd == BIO_FMT)
11491b67be7bSPoul-Henning Kamp 	     && !(fdc->flags & FDC_NODMA)) {
11501b67be7bSPoul-Henning Kamp 		isa_dmastart(
115180139618SWarner Losh 		    bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
11521b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
115361909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
11541b67be7bSPoul-Henning Kamp 		fd->flags |= FD_ISADMA;
115561909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
11561b67be7bSPoul-Henning Kamp 	}
11571b67be7bSPoul-Henning Kamp 
11581b67be7bSPoul-Henning Kamp 	/* Do PIO if we have to */
11591b67be7bSPoul-Henning Kamp 	if (fdc->flags & FDC_NODMA) {
116080139618SWarner Losh 		if (bp->bio_cmd == BIO_READ ||
116180139618SWarner Losh 		    bp->bio_cmd == BIO_WRITE ||
116280139618SWarner Losh 		    bp->bio_cmd == BIO_FMT)
1163b4046cd7SWarner Losh 			fdbcdr_wr(fdc, 1, fd->fd_iosize);
116480139618SWarner Losh 		if (bp->bio_cmd == BIO_WRITE ||
116580139618SWarner Losh 		    bp->bio_cmd == BIO_FMT)
11661b67be7bSPoul-Henning Kamp 			fdc_pio(fdc);
11671b67be7bSPoul-Henning Kamp 	}
11681b67be7bSPoul-Henning Kamp 
11691b67be7bSPoul-Henning Kamp 	switch(bp->bio_cmd) {
11701b67be7bSPoul-Henning Kamp 	case BIO_FMT:
11711b67be7bSPoul-Henning Kamp 		/* formatting */
11721b67be7bSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
11731b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11741b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 6,
11751b67be7bSPoul-Henning Kamp 		    NE7CMD_FORMAT | mfm,
11761b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,
11771b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_secshift,
11781b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_nsecs,
11791b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_gaplen,
11801b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_fillbyte, 0))
11811b67be7bSPoul-Henning Kamp 			return (1);
11821b67be7bSPoul-Henning Kamp 		break;
11831b67be7bSPoul-Henning Kamp 	case BIO_RDID:
11841b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11851b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 2,
11861b67be7bSPoul-Henning Kamp 		    NE7CMD_READID | mfm,
11871b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu, 0))
11881b67be7bSPoul-Henning Kamp 			return (1);
11891b67be7bSPoul-Henning Kamp 		break;
11901b67be7bSPoul-Henning Kamp 	case BIO_READ:
11911b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11921b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 9,
11931b67be7bSPoul-Henning Kamp 		    NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
11941b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,	/* head & unit */
11951b67be7bSPoul-Henning Kamp 		    fd->track,			/* track */
11961b67be7bSPoul-Henning Kamp 		    head,			/* head */
11971b67be7bSPoul-Henning Kamp 		    sec,			/* sector + 1 */
11981b67be7bSPoul-Henning Kamp 		    fd->ft->secsize,		/* sector size */
11991b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac,		/* sectors/track */
12001b67be7bSPoul-Henning Kamp 		    fd->ft->gap,		/* gap size */
12011b67be7bSPoul-Henning Kamp 		    fd->ft->datalen,		/* data length */
12021b67be7bSPoul-Henning Kamp 		    0))
12031b67be7bSPoul-Henning Kamp 			return (1);
12041b67be7bSPoul-Henning Kamp 		break;
12051b67be7bSPoul-Henning Kamp 	case BIO_WRITE:
12061b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
12071b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 9,
12081b67be7bSPoul-Henning Kamp 		    NE7CMD_WRITE | mfm | NE7CMD_MT,
12091b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,	/* head & unit */
12101b67be7bSPoul-Henning Kamp 		    fd->track,			/* track */
12111b67be7bSPoul-Henning Kamp 		    head,			/* head */
12121b67be7bSPoul-Henning Kamp 		    sec,			/* sector + 1 */
12131b67be7bSPoul-Henning Kamp 		    fd->ft->secsize,		/* sector size */
12141b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac,		/* sectors/track */
12151b67be7bSPoul-Henning Kamp 		    fd->ft->gap,		/* gap size */
12161b67be7bSPoul-Henning Kamp 		    fd->ft->datalen,		/* data length */
12171b67be7bSPoul-Henning Kamp 		    0))
12181b67be7bSPoul-Henning Kamp 			return (1);
12191b67be7bSPoul-Henning Kamp 		break;
12201b67be7bSPoul-Henning Kamp 	default:
12211b67be7bSPoul-Henning Kamp 		KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
12221b67be7bSPoul-Henning Kamp 	}
12231b67be7bSPoul-Henning Kamp 
12241b67be7bSPoul-Henning Kamp 	/* Wait for interrupt */
12256e50e38fSJohn Baldwin 	i = tsleep(fdc, PRIBIO, "fddata", hz);
12261b67be7bSPoul-Henning Kamp 
12271b67be7bSPoul-Henning Kamp 	/* PIO if the read looks good */
122880139618SWarner Losh 	if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd == BIO_READ))
12291b67be7bSPoul-Henning Kamp 		fdc_pio(fdc);
12301b67be7bSPoul-Henning Kamp 
12311b67be7bSPoul-Henning Kamp 	/* Finish DMA */
12321b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_ISADMA) {
12331b67be7bSPoul-Henning Kamp 		isa_dmadone(
123480139618SWarner Losh 		    bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
12351b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
123661909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
12371b67be7bSPoul-Henning Kamp 		fd->flags &= ~FD_ISADMA;
123861909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
12391b67be7bSPoul-Henning Kamp 	}
12401b67be7bSPoul-Henning Kamp 
12411b67be7bSPoul-Henning Kamp 	if (i != 0) {
12421b67be7bSPoul-Henning Kamp 		/*
12431b67be7bSPoul-Henning Kamp 		 * Timeout.
12441b67be7bSPoul-Henning Kamp 		 *
12451b67be7bSPoul-Henning Kamp 		 * Due to IBM's brain-dead design, the FDC has a faked ready
12461b67be7bSPoul-Henning Kamp 		 * signal, hardwired to ready == true. Thus, any command
12471b67be7bSPoul-Henning Kamp 		 * issued if there's no diskette in the drive will _never_
12481b67be7bSPoul-Henning Kamp 		 * complete, and must be aborted by resetting the FDC.
12491b67be7bSPoul-Henning Kamp 		 * Many thanks, Big Blue!
12501b67be7bSPoul-Henning Kamp 		 */
12511b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
12521b67be7bSPoul-Henning Kamp 		fdc->flags |= FDC_NEEDS_RESET;
12531b67be7bSPoul-Henning Kamp 		return (1);
12541b67be7bSPoul-Henning Kamp 	}
12551b67be7bSPoul-Henning Kamp 
12561b67be7bSPoul-Henning Kamp 	retry_line = __LINE__;
12571b67be7bSPoul-Henning Kamp 	if (fdc_read_status(fdc))
12581b67be7bSPoul-Henning Kamp 		return (1);
12591b67be7bSPoul-Henning Kamp 
12601b67be7bSPoul-Henning Kamp 	if (debugflags & 0x10)
12611b67be7bSPoul-Henning Kamp 		printf("  -> %x %x %x %x\n",
12621b67be7bSPoul-Henning Kamp 		    fdc->status[0], fdc->status[1],
12631b67be7bSPoul-Henning Kamp 		    fdc->status[2], fdc->status[3]);
12641b67be7bSPoul-Henning Kamp 
12651b67be7bSPoul-Henning Kamp 	st0 = fdc->status[0] & NE7_ST0_IC;
12661b67be7bSPoul-Henning Kamp 	if (st0 != 0) {
12671b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
12681b67be7bSPoul-Henning Kamp 		if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
12691b67be7bSPoul-Henning Kamp 			/*
12701b67be7bSPoul-Henning Kamp 			 * DMA overrun. Someone hogged the bus and
12711b67be7bSPoul-Henning Kamp 			 * didn't release it in time for the next
12721b67be7bSPoul-Henning Kamp 			 * FDC transfer.
12731b67be7bSPoul-Henning Kamp 			 */
12741b67be7bSPoul-Henning Kamp 			return (1);
12751b67be7bSPoul-Henning Kamp 		}
12761b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
12771b67be7bSPoul-Henning Kamp 		if(st0 == NE7_ST0_IC_IV) {
12781b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
12791b67be7bSPoul-Henning Kamp 			return (1);
12801b67be7bSPoul-Henning Kamp 		}
12811b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
12821b67be7bSPoul-Henning Kamp 		if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
12831b67be7bSPoul-Henning Kamp 			need_recal |= (1 << fd->fdsu);
12841b67be7bSPoul-Henning Kamp 			return (1);
12851b67be7bSPoul-Henning Kamp 		}
12861b67be7bSPoul-Henning Kamp 		if (debugflags & 0x20) {
12871b67be7bSPoul-Henning Kamp 			printf("status %02x %02x %02x %02x %02x %02x\n",
12881b67be7bSPoul-Henning Kamp 			    fdc->status[0], fdc->status[1], fdc->status[2],
12891b67be7bSPoul-Henning Kamp 			    fdc->status[3], fdc->status[4], fdc->status[5]);
12901b67be7bSPoul-Henning Kamp 		}
12911b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
1292acb3b5d2SJoerg Wunsch 		if (fd->options & FDOPT_NOERROR)
1293acb3b5d2SJoerg Wunsch 			override_error = 1;
1294acb3b5d2SJoerg Wunsch 		else
12951b67be7bSPoul-Henning Kamp 			return (1);
12961b67be7bSPoul-Henning Kamp 	}
12971b67be7bSPoul-Henning Kamp 	/* All OK */
12981b67be7bSPoul-Henning Kamp 	switch(bp->bio_cmd) {
12991b67be7bSPoul-Henning Kamp 	case BIO_RDID:
13001b67be7bSPoul-Henning Kamp 		/* copy out ID field contents */
13011b67be7bSPoul-Henning Kamp 		idp = (struct fdc_readid *)bp->bio_data;
13021b67be7bSPoul-Henning Kamp 		idp->cyl = fdc->status[3];
13031b67be7bSPoul-Henning Kamp 		idp->head = fdc->status[4];
13041b67be7bSPoul-Henning Kamp 		idp->sec = fdc->status[5];
13051b67be7bSPoul-Henning Kamp 		idp->secshift = fdc->status[6];
13061b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
13071b67be7bSPoul-Henning Kamp 			printf("c %d h %d s %d z %d\n",
13081b67be7bSPoul-Henning Kamp 			    idp->cyl, idp->head, idp->sec, idp->secshift);
13091b67be7bSPoul-Henning Kamp 		break;
13101b67be7bSPoul-Henning Kamp 	case BIO_READ:
13111b67be7bSPoul-Henning Kamp 	case BIO_WRITE:
13121b67be7bSPoul-Henning Kamp 		bp->bio_pblkno += nsect;
13131b67be7bSPoul-Henning Kamp 		bp->bio_resid -= fd->fd_iosize;
13141b67be7bSPoul-Henning Kamp 		bp->bio_completed += fd->fd_iosize;
13151b67be7bSPoul-Henning Kamp 		fd->fd_ioptr += fd->fd_iosize;
1316acb3b5d2SJoerg Wunsch 		if (override_error) {
1317acb3b5d2SJoerg Wunsch 			if ((debugflags & 4))
1318acb3b5d2SJoerg Wunsch 				printf("FDOPT_NOERROR: returning bad data\n");
1319acb3b5d2SJoerg Wunsch 		} else {
1320acb3b5d2SJoerg Wunsch 			/* Since we managed to get something done,
1321acb3b5d2SJoerg Wunsch 			 * reset the retry */
13221b67be7bSPoul-Henning Kamp 			fdc->retry = 0;
13231b67be7bSPoul-Henning Kamp 			if (bp->bio_resid > 0)
13241b67be7bSPoul-Henning Kamp 				return (0);
1325acb3b5d2SJoerg Wunsch 		}
13261b67be7bSPoul-Henning Kamp 		break;
13271b67be7bSPoul-Henning Kamp 	case BIO_FMT:
13281b67be7bSPoul-Henning Kamp 		break;
13291b67be7bSPoul-Henning Kamp 	}
13301b67be7bSPoul-Henning Kamp 	return (fdc_biodone(fdc, 0));
13311b67be7bSPoul-Henning Kamp }
13321b67be7bSPoul-Henning Kamp 
13331b67be7bSPoul-Henning Kamp static void
13341b67be7bSPoul-Henning Kamp fdc_thread(void *arg)
13351b67be7bSPoul-Henning Kamp {
13361b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
13371b67be7bSPoul-Henning Kamp 
13381b67be7bSPoul-Henning Kamp 	fdc = arg;
13391b67be7bSPoul-Henning Kamp 	int i;
13401b67be7bSPoul-Henning Kamp 
13413fc95726SJoerg Wunsch 	mtx_lock(&fdc->fdc_mtx);
134202910eeeSJoerg Wunsch 	fdc->flags |= FDC_KTHREAD_ALIVE;
134302910eeeSJoerg Wunsch 	while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
13443fc95726SJoerg Wunsch 		mtx_unlock(&fdc->fdc_mtx);
13451b67be7bSPoul-Henning Kamp 		i = fdc_worker(fdc);
13461b67be7bSPoul-Henning Kamp 		if (i && debugflags & 0x20) {
13471b67be7bSPoul-Henning Kamp 			if (fdc->bp != NULL) {
13481b67be7bSPoul-Henning Kamp 				g_print_bio(fdc->bp);
13491b67be7bSPoul-Henning Kamp 				printf("\n");
13501b67be7bSPoul-Henning Kamp 			}
13511b67be7bSPoul-Henning Kamp 			printf("Retry line %d\n", retry_line);
13521b67be7bSPoul-Henning Kamp 		}
13531b67be7bSPoul-Henning Kamp 		fdc->retry += i;
13543fc95726SJoerg Wunsch 		mtx_lock(&fdc->fdc_mtx);
13551b67be7bSPoul-Henning Kamp 	}
135602910eeeSJoerg Wunsch 	fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
13573fc95726SJoerg Wunsch 	mtx_unlock(&fdc->fdc_mtx);
13583fc95726SJoerg Wunsch 
13593745c395SJulian Elischer 	kproc_exit(0);
13601b67be7bSPoul-Henning Kamp }
13611b67be7bSPoul-Henning Kamp 
13621b67be7bSPoul-Henning Kamp /*
1363360048a7SJoerg Wunsch  * Enqueue a request.
13641b67be7bSPoul-Henning Kamp  */
13651b67be7bSPoul-Henning Kamp static void
13661b67be7bSPoul-Henning Kamp fd_enqueue(struct fd_data *fd, struct bio *bp)
13671b67be7bSPoul-Henning Kamp {
13681b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
13691b67be7bSPoul-Henning Kamp 	int call;
13701b67be7bSPoul-Henning Kamp 
13711b67be7bSPoul-Henning Kamp 	call = 0;
13721b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
13731b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
13741b67be7bSPoul-Henning Kamp 	/* If we go from idle, cancel motor turnoff */
13751b67be7bSPoul-Henning Kamp 	if (fd->fd_iocount++ == 0)
13769fc9d84bSJohn Baldwin 		callout_stop(&fd->toffhandle);
13771b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_MOTOR) {
13781b67be7bSPoul-Henning Kamp 		/* The motor is on, send it directly to the controller */
13791b67be7bSPoul-Henning Kamp 		bioq_disksort(&fdc->head, bp);
13801b67be7bSPoul-Henning Kamp 		wakeup(&fdc->head);
13811b67be7bSPoul-Henning Kamp 	} else {
13821b67be7bSPoul-Henning Kamp 		/* Queue it on the drive until the motor has started */
13831b67be7bSPoul-Henning Kamp 		bioq_insert_tail(&fd->fd_bq, bp);
13841b67be7bSPoul-Henning Kamp 		if (!(fd->flags & FD_MOTORWAIT))
13851b67be7bSPoul-Henning Kamp 			fd_motor(fd, 1);
13861b67be7bSPoul-Henning Kamp 	}
13871b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
13881b67be7bSPoul-Henning Kamp }
13891b67be7bSPoul-Henning Kamp 
1390c884498fSJung-uk Kim /*
1391c884498fSJung-uk Kim  * Try to find out if we have a disk in the drive.
1392c884498fSJung-uk Kim  */
1393c884498fSJung-uk Kim static int
1394c884498fSJung-uk Kim fd_probe_disk(struct fd_data *fd, int *recal)
1395c884498fSJung-uk Kim {
1396c884498fSJung-uk Kim 	struct fdc_data *fdc;
1397c884498fSJung-uk Kim 	int st0, st3, cyl;
1398c884498fSJung-uk Kim 	int oopts, ret;
1399c884498fSJung-uk Kim 
1400c884498fSJung-uk Kim 	fdc = fd->fdc;
1401c884498fSJung-uk Kim 	oopts = fd->options;
1402c884498fSJung-uk Kim 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1403c884498fSJung-uk Kim 	ret = 1;
1404c884498fSJung-uk Kim 
1405c884498fSJung-uk Kim 	/*
1406c884498fSJung-uk Kim 	 * First recal, then seek to cyl#1, this clears the old condition on
1407c884498fSJung-uk Kim 	 * the disk change line so we can examine it for current status.
1408c884498fSJung-uk Kim 	 */
1409c884498fSJung-uk Kim 	if (debugflags & 0x40)
1410c884498fSJung-uk Kim 		printf("New disk in probe\n");
1411c884498fSJung-uk Kim 	mtx_lock(&fdc->fdc_mtx);
1412c884498fSJung-uk Kim 	fd->flags |= FD_NEWDISK;
1413c884498fSJung-uk Kim 	mtx_unlock(&fdc->fdc_mtx);
1414c884498fSJung-uk Kim 	if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
1415c884498fSJung-uk Kim 		goto done;
1416c884498fSJung-uk Kim 	tsleep(fdc, PRIBIO, "fdrecal", hz);
1417c884498fSJung-uk Kim 	if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1418c884498fSJung-uk Kim 		goto done;	/* XXX */
1419c884498fSJung-uk Kim 	if ((st0 & 0xc0) || cyl != 0)
1420c884498fSJung-uk Kim 		goto done;
1421c884498fSJung-uk Kim 
1422c884498fSJung-uk Kim 	/* Seek to track 1 */
1423c884498fSJung-uk Kim 	if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
1424c884498fSJung-uk Kim 		goto done;
1425c884498fSJung-uk Kim 	tsleep(fdc, PRIBIO, "fdseek", hz);
1426c884498fSJung-uk Kim 	if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1427c884498fSJung-uk Kim 		goto done;	/* XXX */
1428c884498fSJung-uk Kim 	*recal |= (1 << fd->fdsu);
142983e78a7fSJohn Baldwin #ifndef PC98
1430c884498fSJung-uk Kim 	if (fdin_rd(fdc) & FDI_DCHG) {
1431c884498fSJung-uk Kim 		if (debugflags & 0x40)
1432c884498fSJung-uk Kim 			printf("Empty in probe\n");
1433c884498fSJung-uk Kim 		mtx_lock(&fdc->fdc_mtx);
1434c884498fSJung-uk Kim 		fd->flags |= FD_EMPTY;
1435c884498fSJung-uk Kim 		mtx_unlock(&fdc->fdc_mtx);
1436c884498fSJung-uk Kim 	} else {
143783e78a7fSJohn Baldwin #else
143883e78a7fSJohn Baldwin 	{
143983e78a7fSJohn Baldwin #endif
1440c884498fSJung-uk Kim 		if (fdc_sense_drive(fdc, &st3) != 0)
1441c884498fSJung-uk Kim 			goto done;
1442c884498fSJung-uk Kim 		if (debugflags & 0x40)
1443c884498fSJung-uk Kim 			printf("Got disk in probe\n");
1444c884498fSJung-uk Kim 		mtx_lock(&fdc->fdc_mtx);
1445c884498fSJung-uk Kim 		fd->flags &= ~FD_EMPTY;
1446c884498fSJung-uk Kim 		if (st3 & NE7_ST3_WP)
1447c884498fSJung-uk Kim 			fd->flags |= FD_WP;
1448c884498fSJung-uk Kim 		else
1449c884498fSJung-uk Kim 			fd->flags &= ~FD_WP;
1450c884498fSJung-uk Kim 		mtx_unlock(&fdc->fdc_mtx);
1451c884498fSJung-uk Kim 	}
1452c884498fSJung-uk Kim 	ret = 0;
1453c884498fSJung-uk Kim 
1454c884498fSJung-uk Kim done:
1455c884498fSJung-uk Kim 	fd->options = oopts;
1456c884498fSJung-uk Kim 	return (ret);
1457c884498fSJung-uk Kim }
1458c884498fSJung-uk Kim 
14591b67be7bSPoul-Henning Kamp static int
14601b67be7bSPoul-Henning Kamp fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
14611b67be7bSPoul-Henning Kamp {
14621b67be7bSPoul-Henning Kamp 	struct bio *bp;
14631b67be7bSPoul-Henning Kamp 	struct fd_formb *finfo;
14641b67be7bSPoul-Henning Kamp 	struct fdc_readid *idfield;
14651b67be7bSPoul-Henning Kamp 	int error;
14661b67be7bSPoul-Henning Kamp 
14671b67be7bSPoul-Henning Kamp 	bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
14681b67be7bSPoul-Henning Kamp 
14691b67be7bSPoul-Henning Kamp 	/*
14701b67be7bSPoul-Henning Kamp 	 * Set up a bio request for fdstrategy().  bio_offset is faked
14716bccea7cSRebecca Cran 	 * so that fdstrategy() will seek to the requested
14721b67be7bSPoul-Henning Kamp 	 * cylinder, and use the desired head.
14731b67be7bSPoul-Henning Kamp 	 */
14741b67be7bSPoul-Henning Kamp 	bp->bio_cmd = cmd;
14751b67be7bSPoul-Henning Kamp 	if (cmd == BIO_FMT) {
14761b67be7bSPoul-Henning Kamp 		finfo = (struct fd_formb *)data;
14771b67be7bSPoul-Henning Kamp 		bp->bio_pblkno =
14781b67be7bSPoul-Henning Kamp 		    (finfo->cyl * fd->ft->heads + finfo->head) *
14791b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac;
14801b67be7bSPoul-Henning Kamp 		bp->bio_length = sizeof *finfo;
14811b67be7bSPoul-Henning Kamp 	} else if (cmd == BIO_RDID) {
14821b67be7bSPoul-Henning Kamp 		idfield = (struct fdc_readid *)data;
14831b67be7bSPoul-Henning Kamp 		bp->bio_pblkno =
14841b67be7bSPoul-Henning Kamp 		    (idfield->cyl * fd->ft->heads + idfield->head) *
14851b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac;
14861b67be7bSPoul-Henning Kamp 		bp->bio_length = sizeof(struct fdc_readid);
14871b67be7bSPoul-Henning Kamp 	} else if (cmd == BIO_PROBE) {
14881b67be7bSPoul-Henning Kamp 		/* nothing */
14891b67be7bSPoul-Henning Kamp 	} else
14901b67be7bSPoul-Henning Kamp 		panic("wrong cmd in fdmisccmd()");
14911b67be7bSPoul-Henning Kamp 	bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
14921b67be7bSPoul-Henning Kamp 	bp->bio_data = data;
14931b67be7bSPoul-Henning Kamp 	bp->bio_driver1 = fd;
14941b67be7bSPoul-Henning Kamp 	bp->bio_flags = 0;
14951b67be7bSPoul-Henning Kamp 
14961b67be7bSPoul-Henning Kamp 	fd_enqueue(fd, bp);
14971b67be7bSPoul-Henning Kamp 
14981b67be7bSPoul-Henning Kamp 	do {
14996e50e38fSJohn Baldwin 		tsleep(bp, PRIBIO, "fdwait", hz);
15001b67be7bSPoul-Henning Kamp 	} while (!(bp->bio_flags & BIO_DONE));
15011b67be7bSPoul-Henning Kamp 	error = bp->bio_error;
15021b67be7bSPoul-Henning Kamp 
15031b67be7bSPoul-Henning Kamp 	free(bp, M_TEMP);
15041b67be7bSPoul-Henning Kamp 	return (error);
15051b67be7bSPoul-Henning Kamp }
15061b67be7bSPoul-Henning Kamp 
15071b67be7bSPoul-Henning Kamp /*
15081b67be7bSPoul-Henning Kamp  * Try figuring out the density of the media present in our device.
15091b67be7bSPoul-Henning Kamp  */
15101b67be7bSPoul-Henning Kamp static int
15111b67be7bSPoul-Henning Kamp fdautoselect(struct fd_data *fd)
15121b67be7bSPoul-Henning Kamp {
15131b67be7bSPoul-Henning Kamp 	struct fd_type *fdtp;
15141b67be7bSPoul-Henning Kamp 	struct fdc_readid id;
15151b67be7bSPoul-Henning Kamp 	int oopts, rv;
15161b67be7bSPoul-Henning Kamp 
15171b67be7bSPoul-Henning Kamp 	if (!(fd->ft->flags & FL_AUTO))
15181b67be7bSPoul-Henning Kamp 		return (0);
15191b67be7bSPoul-Henning Kamp 
15201b67be7bSPoul-Henning Kamp 	fdtp = fd_native_types[fd->type];
15211b67be7bSPoul-Henning Kamp 	fdsettype(fd, fdtp);
15221b67be7bSPoul-Henning Kamp 	if (!(fd->ft->flags & FL_AUTO))
15231b67be7bSPoul-Henning Kamp 		return (0);
15241b67be7bSPoul-Henning Kamp 
15251b67be7bSPoul-Henning Kamp 	/*
15261b67be7bSPoul-Henning Kamp 	 * Try reading sector ID fields, first at cylinder 0, head 0,
15271b67be7bSPoul-Henning Kamp 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
15281b67be7bSPoul-Henning Kamp 	 * since for 5.25in DD media in a HD drive, there are no data
15291b67be7bSPoul-Henning Kamp 	 * to read (2 step pulses per media cylinder required).  For
15301b67be7bSPoul-Henning Kamp 	 * two-sided media, the second probe always goes to head 1, so
15311b67be7bSPoul-Henning Kamp 	 * we can tell them apart from single-sided media.  As a
15321b67be7bSPoul-Henning Kamp 	 * side-effect this means that single-sided media should be
15331b67be7bSPoul-Henning Kamp 	 * mentioned in the search list after two-sided media of an
15341b67be7bSPoul-Henning Kamp 	 * otherwise identical density.  Media with a different number
15351b67be7bSPoul-Henning Kamp 	 * of sectors per track but otherwise identical parameters
15361b67be7bSPoul-Henning Kamp 	 * cannot be distinguished at all.
15371b67be7bSPoul-Henning Kamp 	 *
15381b67be7bSPoul-Henning Kamp 	 * If we successfully read an ID field on both cylinders where
15391b67be7bSPoul-Henning Kamp 	 * the recorded values match our expectation, we are done.
15401b67be7bSPoul-Henning Kamp 	 * Otherwise, we try the next density entry from the table.
15411b67be7bSPoul-Henning Kamp 	 *
15421b67be7bSPoul-Henning Kamp 	 * Stepping to cylinder 2 has the side-effect of clearing the
15431b67be7bSPoul-Henning Kamp 	 * unit attention bit.
15441b67be7bSPoul-Henning Kamp 	 */
15451b67be7bSPoul-Henning Kamp 	oopts = fd->options;
15461b67be7bSPoul-Henning Kamp 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
15471b67be7bSPoul-Henning Kamp 	for (; fdtp->heads; fdtp++) {
15481b67be7bSPoul-Henning Kamp 		fdsettype(fd, fdtp);
15491b67be7bSPoul-Henning Kamp 
15501b67be7bSPoul-Henning Kamp 		id.cyl = id.head = 0;
15511b67be7bSPoul-Henning Kamp 		rv = fdmisccmd(fd, BIO_RDID, &id);
15521b67be7bSPoul-Henning Kamp 		if (rv != 0)
15531b67be7bSPoul-Henning Kamp 			continue;
15541b67be7bSPoul-Henning Kamp 		if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
15551b67be7bSPoul-Henning Kamp 			continue;
15561b67be7bSPoul-Henning Kamp 		id.cyl = 2;
15571b67be7bSPoul-Henning Kamp 		id.head = fd->ft->heads - 1;
15581b67be7bSPoul-Henning Kamp 		rv = fdmisccmd(fd, BIO_RDID, &id);
15591b67be7bSPoul-Henning Kamp 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
15601b67be7bSPoul-Henning Kamp 		    id.secshift != fdtp->secsize)
15611b67be7bSPoul-Henning Kamp 			continue;
15621b67be7bSPoul-Henning Kamp 		if (rv == 0)
15631b67be7bSPoul-Henning Kamp 			break;
15641b67be7bSPoul-Henning Kamp 	}
15651b67be7bSPoul-Henning Kamp 
15661b67be7bSPoul-Henning Kamp 	fd->options = oopts;
15671b67be7bSPoul-Henning Kamp 	if (fdtp->heads == 0) {
15681b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
15691b67be7bSPoul-Henning Kamp 			device_printf(fd->dev, "autoselection failed\n");
15701b67be7bSPoul-Henning Kamp 		fdsettype(fd, fd_native_types[fd->type]);
1571c884498fSJung-uk Kim 		return (-1);
15721b67be7bSPoul-Henning Kamp 	} else {
15731b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40) {
15741b67be7bSPoul-Henning Kamp 			device_printf(fd->dev,
157583e78a7fSJohn Baldwin 			    "autoselected %d KB medium\n",
157683e78a7fSJohn Baldwin #ifdef PC98
157783e78a7fSJohn Baldwin 			    (128 << (fd->ft->secsize)) * fd->ft->size / 1024);
157883e78a7fSJohn Baldwin #else
157983e78a7fSJohn Baldwin 			    fd->ft->size / 2);
158083e78a7fSJohn Baldwin #endif
15811b67be7bSPoul-Henning Kamp 			fdprinttype(fd->ft);
15821b67be7bSPoul-Henning Kamp 		}
15831b67be7bSPoul-Henning Kamp 		return (0);
15841b67be7bSPoul-Henning Kamp 	}
15851b67be7bSPoul-Henning Kamp }
15861b67be7bSPoul-Henning Kamp 
15871b67be7bSPoul-Henning Kamp /*
15881b67be7bSPoul-Henning Kamp  * GEOM class implementation
15891b67be7bSPoul-Henning Kamp  */
15901b67be7bSPoul-Henning Kamp 
15911b67be7bSPoul-Henning Kamp static g_access_t	fd_access;
15921b67be7bSPoul-Henning Kamp static g_start_t	fd_start;
15931b67be7bSPoul-Henning Kamp static g_ioctl_t	fd_ioctl;
15941b67be7bSPoul-Henning Kamp 
15951b67be7bSPoul-Henning Kamp struct g_class g_fd_class = {
15961b67be7bSPoul-Henning Kamp 	.name =		"FD",
15971b67be7bSPoul-Henning Kamp 	.version =	G_VERSION,
15981b67be7bSPoul-Henning Kamp 	.start =	fd_start,
15991b67be7bSPoul-Henning Kamp 	.access =	fd_access,
16001b67be7bSPoul-Henning Kamp 	.ioctl =	fd_ioctl,
16011b67be7bSPoul-Henning Kamp };
16021b67be7bSPoul-Henning Kamp 
16031b67be7bSPoul-Henning Kamp static int
16041b67be7bSPoul-Henning Kamp fd_access(struct g_provider *pp, int r, int w, int e)
16051b67be7bSPoul-Henning Kamp {
16061b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
16071b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
16081b67be7bSPoul-Henning Kamp 	int ar, aw, ae;
1609fc9f8bcfSKonstantin Belousov 	int busy;
16101b67be7bSPoul-Henning Kamp 
16111b67be7bSPoul-Henning Kamp 	fd = pp->geom->softc;
16121b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
16131b67be7bSPoul-Henning Kamp 
16141b67be7bSPoul-Henning Kamp 	/*
16151b67be7bSPoul-Henning Kamp 	 * If our provider is withering, we can only get negative requests
16161b67be7bSPoul-Henning Kamp 	 * and we don't want to even see them
16171b67be7bSPoul-Henning Kamp 	 */
16181b67be7bSPoul-Henning Kamp 	if (pp->flags & G_PF_WITHER)
16191b67be7bSPoul-Henning Kamp 		return (0);
16201b67be7bSPoul-Henning Kamp 
16211b67be7bSPoul-Henning Kamp 	ar = r + pp->acr;
16221b67be7bSPoul-Henning Kamp 	aw = w + pp->acw;
16231b67be7bSPoul-Henning Kamp 	ae = e + pp->ace;
16241b67be7bSPoul-Henning Kamp 
16251b67be7bSPoul-Henning Kamp 	if (ar == 0 && aw == 0 && ae == 0) {
1626acb3b5d2SJoerg Wunsch 		fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
16271b67be7bSPoul-Henning Kamp 		device_unbusy(fd->dev);
16281b67be7bSPoul-Henning Kamp 		return (0);
16291b67be7bSPoul-Henning Kamp 	}
16301b67be7bSPoul-Henning Kamp 
1631fc9f8bcfSKonstantin Belousov 	busy = 0;
16321b67be7bSPoul-Henning Kamp 	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
163383e78a7fSJohn Baldwin #ifdef PC98
163483e78a7fSJohn Baldwin 		if (pc98_fd_check_ready(fd) == -1)
163583e78a7fSJohn Baldwin 			return (ENXIO);
163683e78a7fSJohn Baldwin #endif
16371b67be7bSPoul-Henning Kamp 		if (fdmisccmd(fd, BIO_PROBE, NULL))
16381b67be7bSPoul-Henning Kamp 			return (ENXIO);
16391b67be7bSPoul-Henning Kamp 		if (fd->flags & FD_EMPTY)
16401b67be7bSPoul-Henning Kamp 			return (ENXIO);
16411b67be7bSPoul-Henning Kamp 		if (fd->flags & FD_NEWDISK) {
1642c884498fSJung-uk Kim 			if (fdautoselect(fd) != 0 &&
1643c884498fSJung-uk Kim 			    (device_get_flags(fd->dev) & FD_NO_CHLINE)) {
1644c884498fSJung-uk Kim 				mtx_lock(&fdc->fdc_mtx);
1645c884498fSJung-uk Kim 				fd->flags |= FD_EMPTY;
1646c884498fSJung-uk Kim 				mtx_unlock(&fdc->fdc_mtx);
1647c884498fSJung-uk Kim 				return (ENXIO);
1648c884498fSJung-uk Kim 			}
164961909e4cSPoul-Henning Kamp 			mtx_lock(&fdc->fdc_mtx);
16501b67be7bSPoul-Henning Kamp 			fd->flags &= ~FD_NEWDISK;
165161909e4cSPoul-Henning Kamp 			mtx_unlock(&fdc->fdc_mtx);
16521b67be7bSPoul-Henning Kamp 		}
16531b67be7bSPoul-Henning Kamp 		device_busy(fd->dev);
1654fc9f8bcfSKonstantin Belousov 		busy = 1;
16551b67be7bSPoul-Henning Kamp 	}
16561b67be7bSPoul-Henning Kamp 
1657fc9f8bcfSKonstantin Belousov 	if (w > 0 && (fd->flags & FD_WP)) {
1658fc9f8bcfSKonstantin Belousov 		if (busy)
1659fc9f8bcfSKonstantin Belousov 			device_unbusy(fd->dev);
16601b67be7bSPoul-Henning Kamp 		return (EROFS);
1661fc9f8bcfSKonstantin Belousov 	}
16621b67be7bSPoul-Henning Kamp 
16631b67be7bSPoul-Henning Kamp 	pp->sectorsize = fd->sectorsize;
16641b67be7bSPoul-Henning Kamp 	pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
16651b67be7bSPoul-Henning Kamp 	pp->mediasize = pp->stripesize * fd->ft->tracks;
16661b67be7bSPoul-Henning Kamp 	return (0);
16671b67be7bSPoul-Henning Kamp }
16681b67be7bSPoul-Henning Kamp 
16691b67be7bSPoul-Henning Kamp static void
16701b67be7bSPoul-Henning Kamp fd_start(struct bio *bp)
16711b67be7bSPoul-Henning Kamp {
16721b67be7bSPoul-Henning Kamp  	struct fdc_data *	fdc;
16731b67be7bSPoul-Henning Kamp  	struct fd_data *	fd;
16741b67be7bSPoul-Henning Kamp 
16751b67be7bSPoul-Henning Kamp 	fd = bp->bio_to->geom->softc;
16761b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
16771b67be7bSPoul-Henning Kamp 	bp->bio_driver1 = fd;
167880139618SWarner Losh 	if (bp->bio_cmd == BIO_GETATTR) {
16791b67be7bSPoul-Henning Kamp 		if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
16801b67be7bSPoul-Henning Kamp 			return;
16811b67be7bSPoul-Henning Kamp 		if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
16821b67be7bSPoul-Henning Kamp 			return;
16831b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, ENOIOCTL);
16841b67be7bSPoul-Henning Kamp 		return;
16851b67be7bSPoul-Henning Kamp 	}
168680139618SWarner Losh 	if (!(bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
16871b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, EOPNOTSUPP);
16881b67be7bSPoul-Henning Kamp 		return;
16891b67be7bSPoul-Henning Kamp 	}
16901b67be7bSPoul-Henning Kamp 	bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
16911b67be7bSPoul-Henning Kamp 	bp->bio_resid = bp->bio_length;
16921b67be7bSPoul-Henning Kamp 	fd_enqueue(fd, bp);
16931b67be7bSPoul-Henning Kamp 	return;
16941b67be7bSPoul-Henning Kamp }
16951b67be7bSPoul-Henning Kamp 
16961b67be7bSPoul-Henning Kamp static int
16972221dbebSPoul-Henning Kamp fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
16981b67be7bSPoul-Henning Kamp {
16991b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
17001b67be7bSPoul-Henning Kamp 	struct fdc_status *fsp;
17011b67be7bSPoul-Henning Kamp 	struct fdc_readid *rid;
17021b67be7bSPoul-Henning Kamp 	int error;
17031b67be7bSPoul-Henning Kamp 
17041b67be7bSPoul-Henning Kamp 	fd = pp->geom->softc;
17051b67be7bSPoul-Henning Kamp 
170683e78a7fSJohn Baldwin #ifdef PC98
170783e78a7fSJohn Baldwin 	pc98_fd_check_ready(fd);
170883e78a7fSJohn Baldwin #endif
170983e78a7fSJohn Baldwin 
17101b67be7bSPoul-Henning Kamp 	switch (cmd) {
17111b67be7bSPoul-Henning Kamp 	case FD_GTYPE:                  /* get drive type */
17121b67be7bSPoul-Henning Kamp 		*(struct fd_type *)data = *fd->ft;
17131b67be7bSPoul-Henning Kamp 		return (0);
17141b67be7bSPoul-Henning Kamp 
17151b67be7bSPoul-Henning Kamp 	case FD_STYPE:                  /* set drive type */
17161b67be7bSPoul-Henning Kamp 		/*
17171b67be7bSPoul-Henning Kamp 		 * Allow setting drive type temporarily iff
17181b67be7bSPoul-Henning Kamp 		 * currently unset.  Used for fdformat so any
17191b67be7bSPoul-Henning Kamp 		 * user can set it, and then start formatting.
17201b67be7bSPoul-Henning Kamp 		 */
17211b67be7bSPoul-Henning Kamp 		fd->fts = *(struct fd_type *)data;
17221b67be7bSPoul-Henning Kamp 		if (fd->fts.sectrac) {
17231b67be7bSPoul-Henning Kamp 			/* XXX: check for rubbish */
17241b67be7bSPoul-Henning Kamp 			fdsettype(fd, &fd->fts);
17251b67be7bSPoul-Henning Kamp 		} else {
17261b67be7bSPoul-Henning Kamp 			fdsettype(fd, fd_native_types[fd->type]);
17271b67be7bSPoul-Henning Kamp 		}
17281b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
17291b67be7bSPoul-Henning Kamp 			fdprinttype(fd->ft);
17301b67be7bSPoul-Henning Kamp 		return (0);
17311b67be7bSPoul-Henning Kamp 
17321b67be7bSPoul-Henning Kamp 	case FD_GOPTS:			/* get drive options */
17331b67be7bSPoul-Henning Kamp 		*(int *)data = fd->options;
17341b67be7bSPoul-Henning Kamp 		return (0);
17351b67be7bSPoul-Henning Kamp 
17361b67be7bSPoul-Henning Kamp 	case FD_SOPTS:			/* set drive options */
17371b67be7bSPoul-Henning Kamp 		fd->options = *(int *)data;
17381b67be7bSPoul-Henning Kamp 		return (0);
17391b67be7bSPoul-Henning Kamp 
17401b67be7bSPoul-Henning Kamp 	case FD_CLRERR:
1741acd3428bSRobert Watson 		error = priv_check(td, PRIV_DRIVER);
1742acd3428bSRobert Watson 		if (error)
1743acd3428bSRobert Watson 			return (error);
17441b67be7bSPoul-Henning Kamp 		fd->fdc->fdc_errs = 0;
17451b67be7bSPoul-Henning Kamp 		return (0);
17461b67be7bSPoul-Henning Kamp 
17471b67be7bSPoul-Henning Kamp 	case FD_GSTAT:
17481b67be7bSPoul-Henning Kamp 		fsp = (struct fdc_status *)data;
17491b67be7bSPoul-Henning Kamp 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
17501b67be7bSPoul-Henning Kamp 			return (EINVAL);
17511b67be7bSPoul-Henning Kamp 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
17521b67be7bSPoul-Henning Kamp 		return (0);
17531b67be7bSPoul-Henning Kamp 
17541b67be7bSPoul-Henning Kamp 	case FD_GDTYPE:
17551b67be7bSPoul-Henning Kamp 		*(enum fd_drivetype *)data = fd->type;
17561b67be7bSPoul-Henning Kamp 		return (0);
17571b67be7bSPoul-Henning Kamp 
17581b67be7bSPoul-Henning Kamp 	case FD_FORM:
175962da7a2dSPoul-Henning Kamp 		if (!(fflag & FWRITE))
17602221dbebSPoul-Henning Kamp 			return (EPERM);
17611b67be7bSPoul-Henning Kamp 		if (((struct fd_formb *)data)->format_version !=
17621b67be7bSPoul-Henning Kamp 		    FD_FORMAT_VERSION)
17631b67be7bSPoul-Henning Kamp 			return (EINVAL); /* wrong version of formatting prog */
17641b67be7bSPoul-Henning Kamp 		error = fdmisccmd(fd, BIO_FMT, data);
176561909e4cSPoul-Henning Kamp 		mtx_lock(&fd->fdc->fdc_mtx);
17661b67be7bSPoul-Henning Kamp 		fd->flags |= FD_NEWDISK;
176761909e4cSPoul-Henning Kamp 		mtx_unlock(&fd->fdc->fdc_mtx);
17681b67be7bSPoul-Henning Kamp 		break;
17691b67be7bSPoul-Henning Kamp 
17701b67be7bSPoul-Henning Kamp 	case FD_READID:
17711b67be7bSPoul-Henning Kamp 		rid = (struct fdc_readid *)data;
17721b67be7bSPoul-Henning Kamp 		if (rid->cyl > 85 || rid->head > 1)
17731b67be7bSPoul-Henning Kamp 			return (EINVAL);
17741b67be7bSPoul-Henning Kamp 		error = fdmisccmd(fd, BIO_RDID, data);
17751b67be7bSPoul-Henning Kamp 		break;
17761b67be7bSPoul-Henning Kamp 
17771b67be7bSPoul-Henning Kamp 	case FIONBIO:
17781b67be7bSPoul-Henning Kamp 	case FIOASYNC:
17791b67be7bSPoul-Henning Kamp 		/* For backwards compat with old fd*(8) tools */
17801b67be7bSPoul-Henning Kamp 		error = 0;
17811b67be7bSPoul-Henning Kamp 		break;
17821b67be7bSPoul-Henning Kamp 
17831b67be7bSPoul-Henning Kamp 	default:
17841b67be7bSPoul-Henning Kamp 		if (debugflags & 0x80)
17851b67be7bSPoul-Henning Kamp 			printf("Unknown ioctl %lx\n", cmd);
17861b67be7bSPoul-Henning Kamp 		error = ENOIOCTL;
17871b67be7bSPoul-Henning Kamp 		break;
17881b67be7bSPoul-Henning Kamp 	}
17891b67be7bSPoul-Henning Kamp 	return (error);
17901b67be7bSPoul-Henning Kamp };
17911b67be7bSPoul-Henning Kamp 
17921b67be7bSPoul-Henning Kamp 
17931b67be7bSPoul-Henning Kamp 
17941b67be7bSPoul-Henning Kamp /*
17951b67be7bSPoul-Henning Kamp  * Configuration/initialization stuff, per controller.
17961b67be7bSPoul-Henning Kamp  */
17971b67be7bSPoul-Henning Kamp 
17981b67be7bSPoul-Henning Kamp devclass_t fdc_devclass;
17991b67be7bSPoul-Henning Kamp static devclass_t fd_devclass;
18001b67be7bSPoul-Henning Kamp 
18011b67be7bSPoul-Henning Kamp struct fdc_ivars {
18021b67be7bSPoul-Henning Kamp 	int	fdunit;
18031b67be7bSPoul-Henning Kamp 	int	fdtype;
18041b67be7bSPoul-Henning Kamp };
18051b67be7bSPoul-Henning Kamp 
18066d6fa4fdSWarner Losh void
180737286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
180837286586SPeter Wemm {
180937286586SPeter Wemm 	device_t dev;
1810973bfe6cSWarner Losh 	struct resource *last;
1811973bfe6cSWarner Losh 	int i;
181237286586SPeter Wemm 
181337286586SPeter Wemm 	dev = fdc->fdc_dev;
1814b4046cd7SWarner Losh 	if (fdc->fdc_intr)
18157964bd0cSWarner Losh 		bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1816a6e4d8c4SNate Lawson 	fdc->fdc_intr = NULL;
1817b4046cd7SWarner Losh 	if (fdc->res_irq != NULL)
181837286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
181937286586SPeter Wemm 		    fdc->res_irq);
1820a6e4d8c4SNate Lawson 	fdc->res_irq = NULL;
1821973bfe6cSWarner Losh 	last = NULL;
1822973bfe6cSWarner Losh 	for (i = 0; i < FDC_MAXREG; i++) {
1823973bfe6cSWarner Losh 		if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1824973bfe6cSWarner Losh 			bus_release_resource(dev, SYS_RES_IOPORT,
1825973bfe6cSWarner Losh 			    fdc->ridio[i], fdc->resio[i]);
1826973bfe6cSWarner Losh 			last = fdc->resio[i];
1827973bfe6cSWarner Losh 			fdc->resio[i] = NULL;
1828973bfe6cSWarner Losh 		}
1829973bfe6cSWarner Losh 	}
1830b4046cd7SWarner Losh 	if (fdc->res_drq != NULL)
183137286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
183237286586SPeter Wemm 		    fdc->res_drq);
18335ceae6b8SYoshihiro Takahashi 	fdc->res_drq = NULL;
183437286586SPeter Wemm }
183537286586SPeter Wemm 
18366d6fa4fdSWarner Losh int
18373aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
183837286586SPeter Wemm {
183937286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
184037286586SPeter Wemm 
184137286586SPeter Wemm 	switch (which) {
184237286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
184337286586SPeter Wemm 		*result = ivars->fdunit;
184437286586SPeter Wemm 		break;
1845752d4735SNate Lawson 	case FDC_IVAR_FDTYPE:
1846752d4735SNate Lawson 		*result = ivars->fdtype;
1847752d4735SNate Lawson 		break;
184837286586SPeter Wemm 	default:
1849752d4735SNate Lawson 		return (ENOENT);
185037286586SPeter Wemm 	}
1851752d4735SNate Lawson 	return (0);
1852752d4735SNate Lawson }
1853752d4735SNate Lawson 
1854752d4735SNate Lawson int
1855752d4735SNate Lawson fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1856752d4735SNate Lawson {
1857752d4735SNate Lawson 	struct fdc_ivars *ivars = device_get_ivars(child);
1858752d4735SNate Lawson 
1859752d4735SNate Lawson 	switch (which) {
1860752d4735SNate Lawson 	case FDC_IVAR_FDUNIT:
1861752d4735SNate Lawson 		ivars->fdunit = value;
1862752d4735SNate Lawson 		break;
1863752d4735SNate Lawson 	case FDC_IVAR_FDTYPE:
1864752d4735SNate Lawson 		ivars->fdtype = value;
1865752d4735SNate Lawson 		break;
1866752d4735SNate Lawson 	default:
1867752d4735SNate Lawson 		return (ENOENT);
1868752d4735SNate Lawson 	}
1869752d4735SNate Lawson 	return (0);
1870752d4735SNate Lawson }
1871752d4735SNate Lawson 
1872752d4735SNate Lawson int
18731b67be7bSPoul-Henning Kamp fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1874752d4735SNate Lawson {
18751b67be7bSPoul-Henning Kamp 	int ic_type, part_id;
18761b67be7bSPoul-Henning Kamp 
187783e78a7fSJohn Baldwin #ifdef PC98
187883e78a7fSJohn Baldwin 	/* See if it can handle a command. */
187983e78a7fSJohn Baldwin 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(4, 240),
188083e78a7fSJohn Baldwin 	    NE7_SPEC_2(2, 0), 0))
188183e78a7fSJohn Baldwin 		return (ENXIO);
188283e78a7fSJohn Baldwin #else
1883ec200f23SPoul-Henning Kamp 	/*
1884ec200f23SPoul-Henning Kamp 	 * A status value of 0xff is very unlikely, but not theoretically
1885ec200f23SPoul-Henning Kamp 	 * impossible, but it is far more likely to indicate an empty bus.
1886ec200f23SPoul-Henning Kamp 	 */
1887ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) == 0xff)
1888ec200f23SPoul-Henning Kamp 		return (ENXIO);
1889ec200f23SPoul-Henning Kamp 
1890ec200f23SPoul-Henning Kamp 	/*
1891ec200f23SPoul-Henning Kamp 	 * Assert a reset to the floppy controller and check that the status
1892ec200f23SPoul-Henning Kamp 	 * register goes to zero.
1893ec200f23SPoul-Henning Kamp 	 */
1894752d4735SNate Lawson 	fdout_wr(fdc, 0);
1895ec200f23SPoul-Henning Kamp 	fdout_wr(fdc, 0);
1896ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) != 0)
1897ec200f23SPoul-Henning Kamp 		return (ENXIO);
1898ec200f23SPoul-Henning Kamp 
1899ec200f23SPoul-Henning Kamp 	/*
1900ec200f23SPoul-Henning Kamp 	 * Clear the reset and see it come ready.
1901ec200f23SPoul-Henning Kamp 	 */
1902752d4735SNate Lawson 	fdout_wr(fdc, FDO_FRST);
1903ec200f23SPoul-Henning Kamp 	DELAY(100);
1904ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) != 0x80)
1905ec200f23SPoul-Henning Kamp 		return (ENXIO);
1906752d4735SNate Lawson 
1907752d4735SNate Lawson 	/* Then, see if it can handle a command. */
190883e78a7fSJohn Baldwin 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(6, 240),
190983e78a7fSJohn Baldwin 	    NE7_SPEC_2(31, 0), 0))
1910752d4735SNate Lawson 		return (ENXIO);
191183e78a7fSJohn Baldwin #endif
19121b67be7bSPoul-Henning Kamp 
19131b67be7bSPoul-Henning Kamp 	/*
19141b67be7bSPoul-Henning Kamp 	 * Try to identify the chip.
19151b67be7bSPoul-Henning Kamp 	 *
19161b67be7bSPoul-Henning Kamp 	 * The i8272 datasheet documents that unknown commands
19171b67be7bSPoul-Henning Kamp 	 * will return ST0 as 0x80.  The i8272 is supposedly identical
19181b67be7bSPoul-Henning Kamp 	 * to the NEC765.
19191b67be7bSPoul-Henning Kamp 	 * The i82077SL datasheet says 0x90 for the VERSION command,
19201b67be7bSPoul-Henning Kamp 	 * and several "superio" chips emulate this.
19211b67be7bSPoul-Henning Kamp 	 */
19221b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
19231b67be7bSPoul-Henning Kamp 		return (ENXIO);
19241b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
19251b67be7bSPoul-Henning Kamp 		return (ENXIO);
19261d4b1bb7SPoul-Henning Kamp 	if (bootverbose)
19271d4b1bb7SPoul-Henning Kamp 		device_printf(dev,
19281d4b1bb7SPoul-Henning Kamp 		    "ic_type %02x part_id %02x\n", ic_type, part_id);
19291b67be7bSPoul-Henning Kamp 	switch (ic_type & 0xff) {
19301b67be7bSPoul-Henning Kamp 	case 0x80:
19311b67be7bSPoul-Henning Kamp 		device_set_desc(dev, "NEC 765 or clone");
19321b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_NE765;
19331b67be7bSPoul-Henning Kamp 		break;
19341b67be7bSPoul-Henning Kamp 	case 0x81:
19351b67be7bSPoul-Henning Kamp 	case 0x90:
19361b67be7bSPoul-Henning Kamp 		device_set_desc(dev,
19371b67be7bSPoul-Henning Kamp 		    "Enhanced floppy controller");
19381b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_ENHANCED;
19391b67be7bSPoul-Henning Kamp 		break;
19401b67be7bSPoul-Henning Kamp 	default:
19411b67be7bSPoul-Henning Kamp 		device_set_desc(dev, "Generic floppy controller");
19421b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_UNKNOWN;
19431b67be7bSPoul-Henning Kamp 		break;
19441b67be7bSPoul-Henning Kamp 	}
1945752d4735SNate Lawson 	return (0);
194637286586SPeter Wemm }
194737286586SPeter Wemm 
19486d6fa4fdSWarner Losh int
1949e219897aSJoerg Wunsch fdc_detach(device_t dev)
19505f830ea2SJoerg Wunsch {
19515f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
19525f830ea2SJoerg Wunsch 	int	error;
19535f830ea2SJoerg Wunsch 
19545f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
19555f830ea2SJoerg Wunsch 
19565f830ea2SJoerg Wunsch 	/* have our children detached first */
19575f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
19585f830ea2SJoerg Wunsch 		return (error);
19595f830ea2SJoerg Wunsch 
19604586315aSJaakko Heinonen 	if (fdc->fdc_intr)
19614586315aSJaakko Heinonen 		bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
19624586315aSJaakko Heinonen 	fdc->fdc_intr = NULL;
19634586315aSJaakko Heinonen 
196402910eeeSJoerg Wunsch 	/* kill worker thread */
196502910eeeSJoerg Wunsch 	mtx_lock(&fdc->fdc_mtx);
19665f6fcd82SJohn Baldwin 	fdc->flags |= FDC_KTHREAD_EXIT;
1967f8353edeSPoul-Henning Kamp 	wakeup(&fdc->head);
196802910eeeSJoerg Wunsch 	while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
196989a828fdSKonstantin Belousov 		msleep(fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
197002910eeeSJoerg Wunsch 	mtx_unlock(&fdc->fdc_mtx);
197102910eeeSJoerg Wunsch 
1972e219897aSJoerg Wunsch 	/* reset controller, turn motor off */
197383e78a7fSJohn Baldwin #ifdef PC98
197483e78a7fSJohn Baldwin 	fdc_reset(fdc);
197583e78a7fSJohn Baldwin #else
1976e219897aSJoerg Wunsch 	fdout_wr(fdc, 0);
197783e78a7fSJohn Baldwin #endif
1978e219897aSJoerg Wunsch 
19797964bd0cSWarner Losh 	if (!(fdc->flags & FDC_NODMA))
19807964bd0cSWarner Losh 		isa_dma_release(fdc->dmachan);
19815f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
19821b67be7bSPoul-Henning Kamp 	mtx_destroy(&fdc->fdc_mtx);
19835f830ea2SJoerg Wunsch 	return (0);
19845f830ea2SJoerg Wunsch }
19855f830ea2SJoerg Wunsch 
19865b81b6b3SRodney W. Grimes /*
198737286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
19885b81b6b3SRodney W. Grimes  */
1989a6e4d8c4SNate Lawson device_t
199037286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
19915b81b6b3SRodney W. Grimes {
199237286586SPeter Wemm 	struct fdc_ivars *ivar;
19936182fdbdSPeter Wemm 	device_t child;
199492200632SGarrett Wollman 
19957cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
199637286586SPeter Wemm 	if (ivar == NULL)
1997a6e4d8c4SNate Lawson 		return (NULL);
1998fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
199942117b6cSPoul-Henning Kamp 	if (child == NULL) {
200042117b6cSPoul-Henning Kamp 		free(ivar, M_DEVBUF);
2001a6e4d8c4SNate Lawson 		return (NULL);
200242117b6cSPoul-Henning Kamp 	}
200337286586SPeter Wemm 	device_set_ivars(child, ivar);
2004a6e4d8c4SNate Lawson 	ivar->fdunit = unit;
2005a6e4d8c4SNate Lawson 	ivar->fdtype = FDT_NONE;
20068a9bc9c0SJohn Baldwin 	if (resource_disabled(name, unit))
20076182fdbdSPeter Wemm 		device_disable(child);
2008a6e4d8c4SNate Lawson 	return (child);
20096182fdbdSPeter Wemm }
20106182fdbdSPeter Wemm 
20116d6fa4fdSWarner Losh int
20126182fdbdSPeter Wemm fdc_attach(device_t dev)
20136182fdbdSPeter Wemm {
201437286586SPeter Wemm 	struct	fdc_data *fdc;
2015a6e4d8c4SNate Lawson 	int	error;
2016427ccf00SDoug Rabson 
201737286586SPeter Wemm 	fdc = device_get_softc(dev);
2018b0c2b925SWarner Losh 	fdc->fdc_dev = dev;
20191b67be7bSPoul-Henning Kamp 	error = fdc_initial_reset(dev, fdc);
20201b67be7bSPoul-Henning Kamp 	if (error) {
20211b67be7bSPoul-Henning Kamp 		device_printf(dev, "does not respond\n");
20221b67be7bSPoul-Henning Kamp 		return (error);
20231b67be7bSPoul-Henning Kamp 	}
2024b593dd82SWarner Losh 	error = bus_setup_intr(dev, fdc->res_irq,
2025ef544f63SPaolo Pisati 	    INTR_TYPE_BIO | INTR_ENTROPY |
2026ef544f63SPaolo Pisati 	    ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
2027ef544f63SPaolo Pisati             ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
2028ef544f63SPaolo Pisati 	    ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
2029ef544f63SPaolo Pisati 			       fdc, &fdc->fdc_intr);
203037286586SPeter Wemm 	if (error) {
203137286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
20321b67be7bSPoul-Henning Kamp 		return (error);
203337286586SPeter Wemm 	}
20347964bd0cSWarner Losh 	if (!(fdc->flags & FDC_NODMA)) {
20357964bd0cSWarner Losh 		error = isa_dma_acquire(fdc->dmachan);
20361ef0fd82SPoul-Henning Kamp 		if (!error) {
20371ef0fd82SPoul-Henning Kamp 			error = isa_dma_init(fdc->dmachan,
20381ef0fd82SPoul-Henning Kamp 			    MAX_BYTES_PER_CYL, M_WAITOK);
20391ef0fd82SPoul-Henning Kamp 			if (error)
20401ef0fd82SPoul-Henning Kamp 				isa_dma_release(fdc->dmachan);
20411ef0fd82SPoul-Henning Kamp 		}
20427964bd0cSWarner Losh 		if (error)
20437964bd0cSWarner Losh 			return (error);
20447964bd0cSWarner Losh 	}
204537286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
2046a6e4d8c4SNate Lawson 	fdc->flags |= FDC_NEEDS_RESET;
20476182fdbdSPeter Wemm 
20481b67be7bSPoul-Henning Kamp 	mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
20496182fdbdSPeter Wemm 
20503a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
205183e78a7fSJohn Baldwin #ifdef PC98
205283e78a7fSJohn Baldwin 	fdc_reset(fdc);
205383e78a7fSJohn Baldwin #else
205464860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
205583e78a7fSJohn Baldwin #endif
20568177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
20575b81b6b3SRodney W. Grimes 
20583745c395SJulian Elischer 	kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
20591b67be7bSPoul-Henning Kamp 	    "fdc%d", device_get_unit(dev));
20601b67be7bSPoul-Henning Kamp 
20611b67be7bSPoul-Henning Kamp 	settle = hz / 8;
20621b67be7bSPoul-Henning Kamp 
2063a6e4d8c4SNate Lawson 	return (0);
2064a6e4d8c4SNate Lawson }
2065a6e4d8c4SNate Lawson 
2066a6e4d8c4SNate Lawson int
2067a6e4d8c4SNate Lawson fdc_hints_probe(device_t dev)
2068a6e4d8c4SNate Lawson {
2069a6e4d8c4SNate Lawson 	const char *name, *dname;
2070a6e4d8c4SNate Lawson 	int i, error, dunit;
2071a6e4d8c4SNate Lawson 
20726182fdbdSPeter Wemm 	/*
207337286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
207437286586SPeter Wemm 	 * devices from the BIOS unless overridden.
20756182fdbdSPeter Wemm 	 */
2076ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
20772398f0cdSPeter Wemm 	i = 0;
2078a6e4d8c4SNate Lawson 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
2079a6e4d8c4SNate Lawson 		resource_int_value(dname, dunit, "drive", &dunit);
20802398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
2081a6e4d8c4SNate Lawson 	}
208237286586SPeter Wemm 
208360444853SJoerg Wunsch 	if ((error = bus_generic_attach(dev)) != 0)
208460444853SJoerg Wunsch 		return (error);
208560444853SJoerg Wunsch 	return (0);
20866182fdbdSPeter Wemm }
20876182fdbdSPeter Wemm 
20886d6fa4fdSWarner Losh int
20896182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
20906182fdbdSPeter Wemm {
20911a6bed68SJoerg Wunsch 	int retval = 0, flags;
209215317dd8SMatthew N. Dodd 
209315317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
20941a6bed68SJoerg Wunsch 	retval += printf(" on %s drive %d", device_get_nameunit(me),
209537286586SPeter Wemm 	       fdc_get_fdunit(child));
20961a6bed68SJoerg Wunsch 	if ((flags = device_get_flags(me)) != 0)
20971a6bed68SJoerg Wunsch 		retval += printf(" flags %#x", flags);
20981a6bed68SJoerg Wunsch 	retval += printf("\n");
209915317dd8SMatthew N. Dodd 
210015317dd8SMatthew N. Dodd 	return (retval);
21016182fdbdSPeter Wemm }
21026182fdbdSPeter Wemm 
210316e68fc6SPeter Wemm /*
2104246ed35dSJoerg Wunsch  * Configuration/initialization, per drive.
210516e68fc6SPeter Wemm  */
21066182fdbdSPeter Wemm static int
21076182fdbdSPeter Wemm fd_probe(device_t dev)
21086182fdbdSPeter Wemm {
210983e78a7fSJohn Baldwin 	int	unit;
211083e78a7fSJohn Baldwin #ifndef PC98
211183e78a7fSJohn Baldwin 	int	i;
21121a6bed68SJoerg Wunsch 	u_int	st0, st3;
211383e78a7fSJohn Baldwin #endif
21146182fdbdSPeter Wemm 	struct	fd_data *fd;
21156182fdbdSPeter Wemm 	struct	fdc_data *fdc;
21161b67be7bSPoul-Henning Kamp 	int	fdsu;
2117752d4735SNate Lawson 	int	flags, type;
21186182fdbdSPeter Wemm 
2119752d4735SNate Lawson 	fdsu = fdc_get_fdunit(dev);
21206182fdbdSPeter Wemm 	fd = device_get_softc(dev);
21216182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
21221a6bed68SJoerg Wunsch 	flags = device_get_flags(dev);
21236182fdbdSPeter Wemm 
21246182fdbdSPeter Wemm 	fd->dev = dev;
21256182fdbdSPeter Wemm 	fd->fdc = fdc;
21266182fdbdSPeter Wemm 	fd->fdsu = fdsu;
21271b67be7bSPoul-Henning Kamp 	unit = device_get_unit(dev);
21286182fdbdSPeter Wemm 
2129752d4735SNate Lawson 	/* Auto-probe if fdinfo is present, but always allow override. */
21301b67be7bSPoul-Henning Kamp 	type = flags & FD_TYPEMASK;
2131752d4735SNate Lawson 	if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
2132752d4735SNate Lawson 		fd->type = type;
2133752d4735SNate Lawson 		goto done;
2134752d4735SNate Lawson 	} else {
2135752d4735SNate Lawson 		/* make sure fdautoselect() will be called */
21361b67be7bSPoul-Henning Kamp 		fd->flags = FD_EMPTY;
2137752d4735SNate Lawson 		fd->type = type;
2138752d4735SNate Lawson 	}
2139752d4735SNate Lawson 
214083e78a7fSJohn Baldwin #ifdef PC98
214183e78a7fSJohn Baldwin 	pc98_fd_check_type(fd, unit);
214283e78a7fSJohn Baldwin #elif defined(__i386__) || defined(__amd64__)
21431b67be7bSPoul-Henning Kamp 	if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
21441a6bed68SJoerg Wunsch 		/* Look up what the BIOS thinks we have. */
214527df52f9SWarner Losh 		if (unit == 0)
21461a6bed68SJoerg Wunsch 			fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
214727df52f9SWarner Losh 		else
21481a6bed68SJoerg Wunsch 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
21491a6bed68SJoerg Wunsch 		if (fd->type == FDT_288M_1)
21501a6bed68SJoerg Wunsch 			fd->type = FDT_288M;
21511a6bed68SJoerg Wunsch 	}
215204209354SPeter Wemm #endif /* __i386__ || __amd64__ */
21536182fdbdSPeter Wemm 	/* is there a unit? */
21541a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE)
21556182fdbdSPeter Wemm 		return (ENXIO);
21566182fdbdSPeter Wemm 
215783e78a7fSJohn Baldwin #ifndef PC98
21581b67be7bSPoul-Henning Kamp /*
21591b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
21601b67be7bSPoul-Henning Kamp */
21616182fdbdSPeter Wemm 	/* select it */
21621b67be7bSPoul-Henning Kamp 	fd_select(fd);
21631b67be7bSPoul-Henning Kamp 	fd_motor(fd, 1);
21641b67be7bSPoul-Henning Kamp 	fdc->fd = fd;
2165fb35bd37SJoerg Wunsch 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
21666182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
21676182fdbdSPeter Wemm 
21681a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0) {
2169dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
21701b67be7bSPoul-Henning Kamp 		if ((fdc_sense_drive(fdc, &st3) == 0) &&
21711a6bed68SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
2172dc5df763SJoerg Wunsch 			/* Seek some steps... */
21731b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
2174dc5df763SJoerg Wunsch 				/* ...wait a moment... */
2175dc5df763SJoerg Wunsch 				DELAY(300000);
2176dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
2177a7e943a4SPyun YongHyeon 				fdc_sense_int(fdc, NULL, NULL);
2178dc5df763SJoerg Wunsch 			}
2179dc5df763SJoerg Wunsch 		}
2180dc5df763SJoerg Wunsch 
21816b7bd95bSJoerg Wunsch 		for (i = 0; i < 2; i++) {
21826b7bd95bSJoerg Wunsch 			/*
21836b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
21841a6bed68SJoerg Wunsch 			 * heads have been beyond cylinder 76, since
21851a6bed68SJoerg Wunsch 			 * most FDCs still barf when attempting to
21861a6bed68SJoerg Wunsch 			 * recalibrate more than 77 steps
21876b7bd95bSJoerg Wunsch 			 */
2188dc5df763SJoerg Wunsch 			/* go back to 0: */
21891b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
21906b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
21916b7bd95bSJoerg Wunsch 				DELAY(i == 0 ? 1000000 : 300000);
21925b81b6b3SRodney W. Grimes 
21936b7bd95bSJoerg Wunsch 				/* anything responding? */
2194a7e943a4SPyun YongHyeon 				if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
2195dc5df763SJoerg Wunsch 				    (st0 & NE7_ST0_EC) == 0)
21966b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
21976b7bd95bSJoerg Wunsch 			}
2198dc5df763SJoerg Wunsch 		}
21991a6bed68SJoerg Wunsch 	}
22006b7bd95bSJoerg Wunsch 
22011b67be7bSPoul-Henning Kamp 	fd_motor(fd, 0);
22021b67be7bSPoul-Henning Kamp 	fdc->fd = NULL;
22031b67be7bSPoul-Henning Kamp /*
22041b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
22051b67be7bSPoul-Henning Kamp */
22063a2f7427SDavid Greenman 
22071a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0 &&
22081a6bed68SJoerg Wunsch 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
22096182fdbdSPeter Wemm 		return (ENXIO);
221083e78a7fSJohn Baldwin #endif /* PC98 */
22115b81b6b3SRodney W. Grimes 
2212752d4735SNate Lawson done:
2213752d4735SNate Lawson 
22141a6bed68SJoerg Wunsch 	switch (fd->type) {
221583e78a7fSJohn Baldwin #ifdef PC98
221683e78a7fSJohn Baldwin 	case FDT_144M:
221783e78a7fSJohn Baldwin 		device_set_desc(dev, "1.44M FDD");
221883e78a7fSJohn Baldwin 		break;
221983e78a7fSJohn Baldwin 	case FDT_12M:
222083e78a7fSJohn Baldwin 		device_set_desc(dev, "1M/640K FDD");
222183e78a7fSJohn Baldwin 		break;
222283e78a7fSJohn Baldwin #else
22231a6bed68SJoerg Wunsch 	case FDT_12M:
22241a6bed68SJoerg Wunsch 		device_set_desc(dev, "1200-KB 5.25\" drive");
22251a6bed68SJoerg Wunsch 		break;
22261a6bed68SJoerg Wunsch 	case FDT_144M:
22271a6bed68SJoerg Wunsch 		device_set_desc(dev, "1440-KB 3.5\" drive");
22281a6bed68SJoerg Wunsch 		break;
22291a6bed68SJoerg Wunsch 	case FDT_288M:
22301a6bed68SJoerg Wunsch 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
22311a6bed68SJoerg Wunsch 		break;
22321a6bed68SJoerg Wunsch 	case FDT_360K:
22331a6bed68SJoerg Wunsch 		device_set_desc(dev, "360-KB 5.25\" drive");
22341a6bed68SJoerg Wunsch 		break;
22351a6bed68SJoerg Wunsch 	case FDT_720K:
22361a6bed68SJoerg Wunsch 		device_set_desc(dev, "720-KB 3.5\" drive");
22371a6bed68SJoerg Wunsch 		break;
223883e78a7fSJohn Baldwin #endif
22391a6bed68SJoerg Wunsch 	default:
22401a6bed68SJoerg Wunsch 		return (ENXIO);
22411a6bed68SJoerg Wunsch 	}
2242dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
2243b99f0a4aSAndrew Moore 	fd->fdc = fdc;
2244b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
22453a2f7427SDavid Greenman 	fd->options = 0;
224683e78a7fSJohn Baldwin #ifdef PC98
224783e78a7fSJohn Baldwin 	fd->pc98_trans = 0;
224883e78a7fSJohn Baldwin #endif
22499fc9d84bSJohn Baldwin 	callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
22505b81b6b3SRodney W. Grimes 
22511a6bed68SJoerg Wunsch 	/* initialize densities for subdevices */
22521b67be7bSPoul-Henning Kamp 	fdsettype(fd, fd_native_types[fd->type]);
22536182fdbdSPeter Wemm 	return (0);
22546182fdbdSPeter Wemm }
22556182fdbdSPeter Wemm 
22561b67be7bSPoul-Henning Kamp /*
22571b67be7bSPoul-Henning Kamp  * We have to do this in a geom event because GEOM is not running
22581b67be7bSPoul-Henning Kamp  * when fd_attach() is.
22591b67be7bSPoul-Henning Kamp  * XXX: move fd_attach after geom like ata/scsi disks
22601b67be7bSPoul-Henning Kamp  */
22611b67be7bSPoul-Henning Kamp static void
22621b67be7bSPoul-Henning Kamp fd_attach2(void *arg, int flag)
22631b67be7bSPoul-Henning Kamp {
22641b67be7bSPoul-Henning Kamp 	struct	fd_data *fd;
22651b67be7bSPoul-Henning Kamp 
22661b67be7bSPoul-Henning Kamp 	fd = arg;
22671b67be7bSPoul-Henning Kamp 
22681b67be7bSPoul-Henning Kamp 	fd->fd_geom = g_new_geomf(&g_fd_class,
22693ba0c6b1SPoul-Henning Kamp 	    "fd%d", device_get_unit(fd->dev));
227002c62349SJaakko Heinonen 	fd->fd_provider = g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
22711b67be7bSPoul-Henning Kamp 	fd->fd_geom->softc = fd;
22721b67be7bSPoul-Henning Kamp 	g_error_provider(fd->fd_provider, 0);
22731b67be7bSPoul-Henning Kamp }
22741b67be7bSPoul-Henning Kamp 
22756182fdbdSPeter Wemm static int
22766182fdbdSPeter Wemm fd_attach(device_t dev)
22776182fdbdSPeter Wemm {
22786182fdbdSPeter Wemm 	struct	fd_data *fd;
22796182fdbdSPeter Wemm 
228064860614SJoerg Wunsch 	fd = device_get_softc(dev);
22811b67be7bSPoul-Henning Kamp 	g_post_event(fd_attach2, fd, M_WAITOK, NULL);
22821b67be7bSPoul-Henning Kamp 	fd->flags |= FD_EMPTY;
22831b67be7bSPoul-Henning Kamp 	bioq_init(&fd->fd_bq);
22841b67be7bSPoul-Henning Kamp 
22856182fdbdSPeter Wemm 	return (0);
22865b81b6b3SRodney W. Grimes }
22875b81b6b3SRodney W. Grimes 
22884586315aSJaakko Heinonen static void
22894586315aSJaakko Heinonen fd_detach_geom(void *arg, int flag)
22904586315aSJaakko Heinonen {
22914586315aSJaakko Heinonen 	struct	fd_data *fd = arg;
22924586315aSJaakko Heinonen 
22934586315aSJaakko Heinonen 	g_topology_assert();
22944586315aSJaakko Heinonen 	g_wither_geom(fd->fd_geom, ENXIO);
22954586315aSJaakko Heinonen }
22964586315aSJaakko Heinonen 
22975f830ea2SJoerg Wunsch static int
22985f830ea2SJoerg Wunsch fd_detach(device_t dev)
22995f830ea2SJoerg Wunsch {
23005f830ea2SJoerg Wunsch 	struct	fd_data *fd;
23015f830ea2SJoerg Wunsch 
23025f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
23034586315aSJaakko Heinonen 	g_waitfor_event(fd_detach_geom, fd, M_WAITOK, NULL);
23044b87c653SWarner Losh 	while (device_get_state(dev) == DS_BUSY)
23054b87c653SWarner Losh 		tsleep(fd, PZERO, "fdd", hz/10);
23061b67be7bSPoul-Henning Kamp 	callout_drain(&fd->toffhandle);
23075f830ea2SJoerg Wunsch 
23085f830ea2SJoerg Wunsch 	return (0);
23095f830ea2SJoerg Wunsch }
23105f830ea2SJoerg Wunsch 
231116e68fc6SPeter Wemm static device_method_t fd_methods[] = {
231216e68fc6SPeter Wemm 	/* Device interface */
231316e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
231416e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
23155f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
231616e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
231716e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
231816e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
231916e68fc6SPeter Wemm 	{ 0, 0 }
232016e68fc6SPeter Wemm };
232116e68fc6SPeter Wemm 
232216e68fc6SPeter Wemm static driver_t fd_driver = {
232316e68fc6SPeter Wemm 	"fd",
232416e68fc6SPeter Wemm 	fd_methods,
232516e68fc6SPeter Wemm 	sizeof(struct fd_data)
232616e68fc6SPeter Wemm };
232716e68fc6SPeter Wemm 
2328f8353edeSPoul-Henning Kamp static int
2329f8353edeSPoul-Henning Kamp fdc_modevent(module_t mod, int type, void *data)
2330f8353edeSPoul-Henning Kamp {
2331f8353edeSPoul-Henning Kamp 
23320c73f68bSJaakko Heinonen 	return (g_modevent(NULL, type, &g_fd_class));
2333f8353edeSPoul-Henning Kamp }
2334f8353edeSPoul-Henning Kamp 
2335f8353edeSPoul-Henning Kamp DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);
2336