xref: /freebsd/sys/dev/fdc/fdc.c (revision c884498f0bde4c8a8b7924a4a9fe63d561a8c7d5)
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>
85a97c75b7SDoug Rabson #include <isa/isareg.h>
866d6fa4fdSWarner Losh #include <dev/fdc/fdcvar.h>
87a97c75b7SDoug Rabson #include <isa/rtc.h>
886182fdbdSPeter Wemm 
891b67be7bSPoul-Henning Kamp #include <dev/ic/nec765.h>
900722d6abSJoerg Wunsch 
913fef646eSJoerg Wunsch /*
921b67be7bSPoul-Henning Kamp  * Runtime configuration hints/flags
933fef646eSJoerg Wunsch  */
941b67be7bSPoul-Henning Kamp 
951b67be7bSPoul-Henning Kamp /* configuration flags for fd */
961b67be7bSPoul-Henning Kamp #define FD_TYPEMASK	0x0f	/* drive type, matches enum
971b67be7bSPoul-Henning Kamp 				 * fd_drivetype; on i386 machines, if
981b67be7bSPoul-Henning Kamp 				 * given as 0, use RTC type for fd0
991b67be7bSPoul-Henning Kamp 				 * and fd1 */
100c884498fSJung-uk Kim #define	FD_NO_CHLINE	0x10	/* drive does not support changeline
101c884498fSJung-uk Kim 				 * aka. unit attention */
1021b67be7bSPoul-Henning Kamp #define FD_NO_PROBE	0x20	/* don't probe drive (seek test), just
1031b67be7bSPoul-Henning Kamp 				 * assume it is there */
1041b67be7bSPoul-Henning Kamp 
1051b67be7bSPoul-Henning Kamp /*
1061b67be7bSPoul-Henning Kamp  * Things that could conceiveably considered parameters or tweakables
1071b67be7bSPoul-Henning Kamp  */
1081b67be7bSPoul-Henning Kamp 
1091b67be7bSPoul-Henning Kamp /*
1101b67be7bSPoul-Henning Kamp  * Maximal number of bytes in a cylinder.
1111b67be7bSPoul-Henning Kamp  * This is used for ISADMA bouncebuffer allocation and sets the max
1121b67be7bSPoul-Henning Kamp  * xfersize we support.
1131b67be7bSPoul-Henning Kamp  *
11461909e4cSPoul-Henning Kamp  * 2.88M format has 2 x 36 x 512, allow for hacked up density.
1151b67be7bSPoul-Henning Kamp  */
1161b67be7bSPoul-Henning Kamp #define MAX_BYTES_PER_CYL	(2 * 40 * 512)
117dc5df763SJoerg Wunsch 
1181a6bed68SJoerg Wunsch /*
119cb38bb6cSJoerg Wunsch  * Timeout value for the PIO loops to wait until the FDC main status
120cb38bb6cSJoerg Wunsch  * register matches our expectations (request for master, direction
121cb38bb6cSJoerg Wunsch  * bit).  This is supposed to be a number of microseconds, although
122cb38bb6cSJoerg Wunsch  * timing might actually not be very accurate.
123cb38bb6cSJoerg Wunsch  *
124cb38bb6cSJoerg Wunsch  * Timeouts of 100 msec are believed to be required for some broken
125cb38bb6cSJoerg Wunsch  * (old) hardware.
126cb38bb6cSJoerg Wunsch  */
127cb38bb6cSJoerg Wunsch #define	FDSTS_TIMEOUT	100000
128cb38bb6cSJoerg Wunsch 
129cb38bb6cSJoerg Wunsch /*
1301b67be7bSPoul-Henning Kamp  * After this many errors, stop whining.  Close will reset this count.
1311a6bed68SJoerg Wunsch  */
13261909e4cSPoul-Henning Kamp #define FDC_ERRMAX	100
1337ca0641bSAndrey A. Chernov 
1341a6bed68SJoerg Wunsch /*
1351b67be7bSPoul-Henning Kamp  * AutoDensity search lists for each drive type.
1361a6bed68SJoerg Wunsch  */
1371b67be7bSPoul-Henning Kamp 
1381b67be7bSPoul-Henning Kamp static struct fd_type fd_searchlist_360k[] = {
1391b67be7bSPoul-Henning Kamp 	{ FDF_5_360 },
1401b67be7bSPoul-Henning Kamp 	{ 0 }
1415b81b6b3SRodney W. Grimes };
1425b81b6b3SRodney W. Grimes 
1431a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_12m[] = {
1441b67be7bSPoul-Henning Kamp 	{ FDF_5_1200 | FL_AUTO },
1451b67be7bSPoul-Henning Kamp 	{ FDF_5_360 | FL_2STEP | FL_AUTO},
1461b67be7bSPoul-Henning Kamp 	{ 0 }
1471b67be7bSPoul-Henning Kamp };
1481b67be7bSPoul-Henning Kamp 
1491b67be7bSPoul-Henning Kamp static struct fd_type fd_searchlist_720k[] = {
1501b67be7bSPoul-Henning Kamp 	{ FDF_3_720 },
1511b67be7bSPoul-Henning Kamp 	{ 0 }
1521a6bed68SJoerg Wunsch };
1531a6bed68SJoerg Wunsch 
1541a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_144m[] = {
1551b67be7bSPoul-Henning Kamp 	{ FDF_3_1440 | FL_AUTO},
1561b67be7bSPoul-Henning Kamp 	{ FDF_3_720 | FL_AUTO},
1571b67be7bSPoul-Henning Kamp 	{ 0 }
1581a6bed68SJoerg Wunsch };
1591a6bed68SJoerg Wunsch 
1601a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_288m[] = {
1611b67be7bSPoul-Henning Kamp 	{ FDF_3_1440 | FL_AUTO },
1621a6bed68SJoerg Wunsch #if 0
1631b67be7bSPoul-Henning Kamp 	{ FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
1641a6bed68SJoerg Wunsch #endif
1651b67be7bSPoul-Henning Kamp 	{ FDF_3_720 | FL_AUTO},
1661b67be7bSPoul-Henning Kamp 	{ 0 }
1671a6bed68SJoerg Wunsch };
168dc16046fSJoerg Wunsch 
1691b67be7bSPoul-Henning Kamp /*
1701b67be7bSPoul-Henning Kamp  * Order must match enum fd_drivetype in <sys/fdcio.h>.
1711b67be7bSPoul-Henning Kamp  */
1721b67be7bSPoul-Henning Kamp static struct fd_type *fd_native_types[] = {
1731b67be7bSPoul-Henning Kamp 	NULL,				/* FDT_NONE */
1741b67be7bSPoul-Henning Kamp 	fd_searchlist_360k, 		/* FDT_360K */
1751b67be7bSPoul-Henning Kamp 	fd_searchlist_12m, 		/* FDT_12M */
1761b67be7bSPoul-Henning Kamp 	fd_searchlist_720k, 		/* FDT_720K */
1771b67be7bSPoul-Henning Kamp 	fd_searchlist_144m, 		/* FDT_144M */
1788fe457d6SPeter Edwards 	fd_searchlist_288m,		/* FDT_288M_1 (mapped to FDT_288M) */
1791b67be7bSPoul-Henning Kamp 	fd_searchlist_288m, 		/* FDT_288M */
1801b67be7bSPoul-Henning Kamp };
181f8ce7dd5SJoerg Wunsch 
1821b67be7bSPoul-Henning Kamp /*
1831b67be7bSPoul-Henning Kamp  * Internals start here
1841b67be7bSPoul-Henning Kamp  */
1851b67be7bSPoul-Henning Kamp 
1861b67be7bSPoul-Henning Kamp /* registers */
1871b67be7bSPoul-Henning Kamp #define	FDOUT	2	/* Digital Output Register (W) */
1881b67be7bSPoul-Henning Kamp #define	FDO_FDSEL	0x03	/*  floppy device select */
1891b67be7bSPoul-Henning Kamp #define	FDO_FRST	0x04	/*  floppy controller reset */
1901b67be7bSPoul-Henning Kamp #define	FDO_FDMAEN	0x08	/*  enable floppy DMA and Interrupt */
1911b67be7bSPoul-Henning Kamp #define	FDO_MOEN0	0x10	/*  motor enable drive 0 */
1921b67be7bSPoul-Henning Kamp #define	FDO_MOEN1	0x20	/*  motor enable drive 1 */
1931b67be7bSPoul-Henning Kamp #define	FDO_MOEN2	0x40	/*  motor enable drive 2 */
1941b67be7bSPoul-Henning Kamp #define	FDO_MOEN3	0x80	/*  motor enable drive 3 */
1951b67be7bSPoul-Henning Kamp 
1961b67be7bSPoul-Henning Kamp #define	FDSTS	4	/* NEC 765 Main Status Register (R) */
19750d99d1aSJung-uk Kim #define FDDSR	4	/* Data Rate Select Register (W) */
1981b67be7bSPoul-Henning Kamp #define	FDDATA	5	/* NEC 765 Data Register (R/W) */
1991b67be7bSPoul-Henning Kamp #define	FDCTL	7	/* Control Register (W) */
2001b67be7bSPoul-Henning Kamp 
2011b67be7bSPoul-Henning Kamp /*
202973bfe6cSWarner Losh  * The YE-DATA PC Card floppies use PIO to read in the data rather
203973bfe6cSWarner Losh  * than DMA due to the wild variability of DMA for the PC Card
204973bfe6cSWarner Losh  * devices.  DMA was deleted from the PC Card specification in version
205973bfe6cSWarner Losh  * 7.2 of the standard, but that post-dates the YE-DATA devices by many
206973bfe6cSWarner Losh  * years.
207973bfe6cSWarner Losh  *
208973bfe6cSWarner Losh  * In addition, if we cannot setup the DMA resources for the ISA
209973bfe6cSWarner Losh  * attachment, we'll use this same offset for data transfer.  However,
210973bfe6cSWarner Losh  * that almost certainly won't work.
211b4046cd7SWarner Losh  *
212b4046cd7SWarner Losh  * For this mode, offset 0 and 1 must be used to setup the transfer
213973bfe6cSWarner Losh  * for this floppy.  This is OK for PC Card YE Data devices, but for
214973bfe6cSWarner Losh  * ISA this is likely wrong.  These registers are only available on
215973bfe6cSWarner Losh  * those systems that map them to the floppy drive.  Newer systems do
216973bfe6cSWarner Losh  * not do this, and we should likely prohibit access to them (or
217973bfe6cSWarner Losh  * disallow NODMA to be set).
2181b67be7bSPoul-Henning Kamp  */
219b4046cd7SWarner Losh #define FDBCDR		0	/* And 1 */
220b4046cd7SWarner Losh #define FD_YE_DATAPORT	6	/* Drive Data port */
2211b67be7bSPoul-Henning Kamp 
2221b67be7bSPoul-Henning Kamp #define	FDI_DCHG	0x80	/* diskette has been changed */
2231b67be7bSPoul-Henning Kamp 				/* requires drive and motor being selected */
2241b67be7bSPoul-Henning Kamp 				/* is cleared by any step pulse to drive */
2251b67be7bSPoul-Henning Kamp 
2261b67be7bSPoul-Henning Kamp /*
22761909e4cSPoul-Henning Kamp  * We have three private BIO commands.
2281b67be7bSPoul-Henning Kamp  */
2291b67be7bSPoul-Henning Kamp #define BIO_PROBE	BIO_CMD0
2301b67be7bSPoul-Henning Kamp #define BIO_RDID	BIO_CMD1
2311b67be7bSPoul-Henning Kamp #define BIO_FMT		BIO_CMD2
2325b81b6b3SRodney W. Grimes 
233246ed35dSJoerg Wunsch /*
234246ed35dSJoerg Wunsch  * Per drive structure (softc).
235246ed35dSJoerg Wunsch  */
2366182fdbdSPeter Wemm struct fd_data {
2371b67be7bSPoul-Henning Kamp 	u_char 	*fd_ioptr;	/* IO pointer */
2381b67be7bSPoul-Henning Kamp 	u_int	fd_iosize;	/* Size of IO chunks */
2391b67be7bSPoul-Henning Kamp 	u_int	fd_iocount;	/* Outstanding requests */
240b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2415b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2421a6bed68SJoerg Wunsch 	enum	fd_drivetype type; /* drive type */
2431a6bed68SJoerg Wunsch 	struct	fd_type *ft;	/* pointer to current type descriptor */
2441b67be7bSPoul-Henning Kamp 	struct	fd_type fts;	/* type descriptors */
2451b67be7bSPoul-Henning Kamp 	int	sectorsize;
2465b81b6b3SRodney W. Grimes 	int	flags;
2471b67be7bSPoul-Henning Kamp #define	FD_WP		(1<<0)	/* Write protected	*/
2481b67be7bSPoul-Henning Kamp #define	FD_MOTOR	(1<<1)	/* motor should be on	*/
2491b67be7bSPoul-Henning Kamp #define	FD_MOTORWAIT	(1<<2)	/* motor should be on	*/
2501b67be7bSPoul-Henning Kamp #define	FD_EMPTY	(1<<3)	/* no media		*/
2511b67be7bSPoul-Henning Kamp #define	FD_NEWDISK	(1<<4)	/* media changed	*/
2521b67be7bSPoul-Henning Kamp #define	FD_ISADMA	(1<<5)	/* isa dma started 	*/
2535b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
2541b67be7bSPoul-Henning Kamp #define FD_NO_TRACK	 -2
2551b67be7bSPoul-Henning Kamp 	int	options;	/* FDOPT_* */
2561b67be7bSPoul-Henning Kamp 	struct	callout toffhandle;
2571b67be7bSPoul-Henning Kamp 	struct g_geom *fd_geom;
2581b67be7bSPoul-Henning Kamp 	struct g_provider *fd_provider;
2596182fdbdSPeter Wemm 	device_t dev;
2601b67be7bSPoul-Henning Kamp 	struct bio_queue_head fd_bq;
2616182fdbdSPeter Wemm };
26237286586SPeter Wemm 
2631b67be7bSPoul-Henning Kamp #define FD_NOT_VALID -2
264752d4735SNate Lawson 
265246ed35dSJoerg Wunsch static driver_intr_t fdc_intr;
266ef544f63SPaolo Pisati static driver_filter_t fdc_intr_fast;
2671b67be7bSPoul-Henning Kamp static void fdc_reset(struct fdc_data *);
268c884498fSJung-uk Kim static int fd_probe_disk(struct fd_data *, int *);
269d66c374fSTor Egge 
2701b67be7bSPoul-Henning Kamp SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
271d66c374fSTor Egge 
2721b67be7bSPoul-Henning Kamp static int fifo_threshold = 8;
2731b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
2741b67be7bSPoul-Henning Kamp 	"FIFO threshold setting");
2751b67be7bSPoul-Henning Kamp 
2761b67be7bSPoul-Henning Kamp static int debugflags = 0;
2771b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
2781b67be7bSPoul-Henning Kamp 	"Debug flags");
2791b67be7bSPoul-Henning Kamp 
2801b67be7bSPoul-Henning Kamp static int retries = 10;
2811b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
2821b67be7bSPoul-Henning Kamp 	"Number of retries to attempt");
2831b67be7bSPoul-Henning Kamp 
2841b67be7bSPoul-Henning Kamp static int spec1 = 0xaf;
2851b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
2861b67be7bSPoul-Henning Kamp 	"Specification byte one (step-rate + head unload)");
2871b67be7bSPoul-Henning Kamp 
2881b67be7bSPoul-Henning Kamp static int spec2 = 0x10;
2891b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
2901b67be7bSPoul-Henning Kamp 	"Specification byte two (head load time + no-dma)");
2911b67be7bSPoul-Henning Kamp 
2921b67be7bSPoul-Henning Kamp static int settle;
2931b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
2941b67be7bSPoul-Henning Kamp 	"Head settling time in sec/hz");
2951b67be7bSPoul-Henning Kamp 
2961b67be7bSPoul-Henning Kamp static void
2971b67be7bSPoul-Henning Kamp fdprinttype(struct fd_type *ft)
2981b67be7bSPoul-Henning Kamp {
2991b67be7bSPoul-Henning Kamp 
3001b67be7bSPoul-Henning Kamp 	printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
3011b67be7bSPoul-Henning Kamp 	    ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
3021b67be7bSPoul-Henning Kamp 	    ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
3031b67be7bSPoul-Henning Kamp 	    ft->offset_side2, ft->flags);
3041b67be7bSPoul-Henning Kamp }
3051b67be7bSPoul-Henning Kamp 
3061b67be7bSPoul-Henning Kamp static void
3071b67be7bSPoul-Henning Kamp fdsettype(struct fd_data *fd, struct fd_type *ft)
3081b67be7bSPoul-Henning Kamp {
3091b67be7bSPoul-Henning Kamp 	fd->ft = ft;
3101b67be7bSPoul-Henning Kamp 	ft->size = ft->sectrac * ft->heads * ft->tracks;
3111b67be7bSPoul-Henning Kamp 	fd->sectorsize = 128 << fd->ft->secsize;
3121b67be7bSPoul-Henning Kamp }
3135b81b6b3SRodney W. Grimes 
314246ed35dSJoerg Wunsch /*
315246ed35dSJoerg Wunsch  * Bus space handling (access to low-level IO).
316246ed35dSJoerg Wunsch  */
317973bfe6cSWarner Losh __inline static void
318973bfe6cSWarner Losh fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
319973bfe6cSWarner Losh {
320973bfe6cSWarner Losh 
321973bfe6cSWarner Losh 	bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
322973bfe6cSWarner Losh }
323973bfe6cSWarner Losh 
324973bfe6cSWarner Losh __inline static uint8_t
325973bfe6cSWarner Losh fdregrd(struct fdc_data *fdc, int reg)
326973bfe6cSWarner Losh {
327973bfe6cSWarner Losh 
328973bfe6cSWarner Losh 	return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
329973bfe6cSWarner Losh }
330973bfe6cSWarner Losh 
3311b67be7bSPoul-Henning Kamp static void
3321b67be7bSPoul-Henning Kamp fdctl_wr(struct fdc_data *fdc, u_int8_t v)
333427ccf00SDoug Rabson {
3341b67be7bSPoul-Henning Kamp 
335973bfe6cSWarner Losh 	fdregwr(fdc, FDCTL, v);
3361b67be7bSPoul-Henning Kamp }
3371b67be7bSPoul-Henning Kamp 
3381b67be7bSPoul-Henning Kamp static void
3391b67be7bSPoul-Henning Kamp fdout_wr(struct fdc_data *fdc, u_int8_t v)
3401b67be7bSPoul-Henning Kamp {
3411b67be7bSPoul-Henning Kamp 
342973bfe6cSWarner Losh 	fdregwr(fdc, FDOUT, v);
343427ccf00SDoug Rabson }
344427ccf00SDoug Rabson 
345427ccf00SDoug Rabson static u_int8_t
3461b67be7bSPoul-Henning Kamp fdsts_rd(struct fdc_data *fdc)
347427ccf00SDoug Rabson {
3481b67be7bSPoul-Henning Kamp 
349973bfe6cSWarner Losh 	return fdregrd(fdc, FDSTS);
350427ccf00SDoug Rabson }
351427ccf00SDoug Rabson 
352427ccf00SDoug Rabson static void
35350d99d1aSJung-uk Kim fddsr_wr(struct fdc_data *fdc, u_int8_t v)
35450d99d1aSJung-uk Kim {
35550d99d1aSJung-uk Kim 
35650d99d1aSJung-uk Kim 	fdregwr(fdc, FDDSR, v);
35750d99d1aSJung-uk Kim }
35850d99d1aSJung-uk Kim 
35950d99d1aSJung-uk Kim static void
3601b67be7bSPoul-Henning Kamp fddata_wr(struct fdc_data *fdc, u_int8_t v)
361427ccf00SDoug Rabson {
3621b67be7bSPoul-Henning Kamp 
363973bfe6cSWarner Losh 	fdregwr(fdc, FDDATA, v);
364427ccf00SDoug Rabson }
365427ccf00SDoug Rabson 
366427ccf00SDoug Rabson static u_int8_t
3671b67be7bSPoul-Henning Kamp fddata_rd(struct fdc_data *fdc)
368427ccf00SDoug Rabson {
3691b67be7bSPoul-Henning Kamp 
370973bfe6cSWarner Losh 	return fdregrd(fdc, FDDATA);
371427ccf00SDoug Rabson }
372427ccf00SDoug Rabson 
373427ccf00SDoug Rabson static u_int8_t
3741b67be7bSPoul-Henning Kamp fdin_rd(struct fdc_data *fdc)
375427ccf00SDoug Rabson {
3761b67be7bSPoul-Henning Kamp 
377973bfe6cSWarner Losh 	return fdregrd(fdc, FDCTL);
378427ccf00SDoug Rabson }
379427ccf00SDoug Rabson 
380b4046cd7SWarner Losh /*
381b4046cd7SWarner Losh  * Magic pseudo-DMA initialization for YE FDC. Sets count and
382b4046cd7SWarner Losh  * direction.
383b4046cd7SWarner Losh  */
384b4046cd7SWarner Losh static void
385b4046cd7SWarner Losh fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
386b4046cd7SWarner Losh {
387973bfe6cSWarner Losh 	fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
388973bfe6cSWarner Losh 	fdregwr(fdc, FDBCDR + 1,
389973bfe6cSWarner Losh 	    (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
390b4046cd7SWarner Losh }
391b4046cd7SWarner Losh 
392dc5df763SJoerg Wunsch static int
3936182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
394dc5df763SJoerg Wunsch {
3956182fdbdSPeter Wemm 	fdc->fdc_errs++;
39616b04b6aSJoerg Wunsch 	if (s) {
397b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
398b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
399b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
400b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
401b6e5f28eSPeter Wemm 						    "logging any more\n");
40216b04b6aSJoerg Wunsch 	}
403dc5df763SJoerg Wunsch 
4041b67be7bSPoul-Henning Kamp 	return (1);
405dc5df763SJoerg Wunsch }
406dc5df763SJoerg Wunsch 
407dc5df763SJoerg Wunsch /*
4081b67be7bSPoul-Henning Kamp  * FDC IO functions, take care of the main status register, timeout
4091b67be7bSPoul-Henning Kamp  * in case the desired status bits are never set.
4101b67be7bSPoul-Henning Kamp  *
4111b67be7bSPoul-Henning Kamp  * These PIO loops initially start out with short delays between
4121b67be7bSPoul-Henning Kamp  * each iteration in the expectation that the required condition
4131b67be7bSPoul-Henning Kamp  * is usually met quickly, so it can be handled immediately.
414dc5df763SJoerg Wunsch  */
4151b67be7bSPoul-Henning Kamp static int
4161b67be7bSPoul-Henning Kamp fdc_in(struct fdc_data *fdc, int *ptr)
417dc5df763SJoerg Wunsch {
4181b67be7bSPoul-Henning Kamp 	int i, j, step;
4191b67be7bSPoul-Henning Kamp 
4201b67be7bSPoul-Henning Kamp 	step = 1;
4211b67be7bSPoul-Henning Kamp 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
4221b67be7bSPoul-Henning Kamp 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
4231b67be7bSPoul-Henning Kamp 	        if (i == (NE7_DIO|NE7_RQM)) {
4241b67be7bSPoul-Henning Kamp 			i = fddata_rd(fdc);
4251b67be7bSPoul-Henning Kamp 			if (ptr)
4261b67be7bSPoul-Henning Kamp 				*ptr = i;
4271b67be7bSPoul-Henning Kamp 			return (0);
4281b67be7bSPoul-Henning Kamp 		}
4291b67be7bSPoul-Henning Kamp 		if (i == NE7_RQM)
4301b67be7bSPoul-Henning Kamp 			return (fdc_err(fdc, "ready for output in input\n"));
4311b67be7bSPoul-Henning Kamp 		step += step;
4321b67be7bSPoul-Henning Kamp 		DELAY(step);
4331b67be7bSPoul-Henning Kamp 	}
4341b67be7bSPoul-Henning Kamp 	return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
4351b67be7bSPoul-Henning Kamp }
4361b67be7bSPoul-Henning Kamp 
4371b67be7bSPoul-Henning Kamp static int
4381b67be7bSPoul-Henning Kamp fdc_out(struct fdc_data *fdc, int x)
4391b67be7bSPoul-Henning Kamp {
4401b67be7bSPoul-Henning Kamp 	int i, j, step;
4411b67be7bSPoul-Henning Kamp 
4421b67be7bSPoul-Henning Kamp 	step = 1;
4431b67be7bSPoul-Henning Kamp 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
4441b67be7bSPoul-Henning Kamp 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
4451b67be7bSPoul-Henning Kamp 	        if (i == NE7_RQM) {
4461b67be7bSPoul-Henning Kamp 			fddata_wr(fdc, x);
4471b67be7bSPoul-Henning Kamp 			return (0);
4481b67be7bSPoul-Henning Kamp 		}
4491b67be7bSPoul-Henning Kamp 		if (i == (NE7_DIO|NE7_RQM))
4501b67be7bSPoul-Henning Kamp 			return (fdc_err(fdc, "ready for input in output\n"));
4511b67be7bSPoul-Henning Kamp 		step += step;
4521b67be7bSPoul-Henning Kamp 		DELAY(step);
4531b67be7bSPoul-Henning Kamp 	}
4541b67be7bSPoul-Henning Kamp 	return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
4551b67be7bSPoul-Henning Kamp }
4561b67be7bSPoul-Henning Kamp 
4571b67be7bSPoul-Henning Kamp /*
4581b67be7bSPoul-Henning Kamp  * fdc_cmd: Send a command to the chip.
4591b67be7bSPoul-Henning Kamp  * Takes a varargs with this structure:
4601b67be7bSPoul-Henning Kamp  *	# of output bytes
4611b67be7bSPoul-Henning Kamp  *	output bytes as int [...]
4621b67be7bSPoul-Henning Kamp  *	# of input bytes
4631b67be7bSPoul-Henning Kamp  *	input bytes as int* [...]
4641b67be7bSPoul-Henning Kamp  */
4651b67be7bSPoul-Henning Kamp static int
4661b67be7bSPoul-Henning Kamp fdc_cmd(struct fdc_data *fdc, int n_out, ...)
4671b67be7bSPoul-Henning Kamp {
4681b67be7bSPoul-Henning Kamp 	u_char cmd = 0;
469dc5df763SJoerg Wunsch 	int n_in;
4701b67be7bSPoul-Henning Kamp 	int n, i;
471dc5df763SJoerg Wunsch 	va_list ap;
472dc5df763SJoerg Wunsch 
473dc5df763SJoerg Wunsch 	va_start(ap, n_out);
4741b67be7bSPoul-Henning Kamp 	for (n = 0; n < n_out; n++) {
4751b67be7bSPoul-Henning Kamp 		i = va_arg(ap, int);
4761b67be7bSPoul-Henning Kamp 		if (n == 0)
4771b67be7bSPoul-Henning Kamp 			cmd = i;
4781b67be7bSPoul-Henning Kamp 		if (fdc_out(fdc, i) < 0) {
479dc5df763SJoerg Wunsch 			char msg[50];
4802127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
481dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
482dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
4831b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
484a7e943a4SPyun YongHyeon 			va_end(ap);
4856182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
486dc5df763SJoerg Wunsch 		}
487dc5df763SJoerg Wunsch 	}
488dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
4891b67be7bSPoul-Henning Kamp 	for (n = 0; n < n_in; n++) {
490dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
4911b67be7bSPoul-Henning Kamp 		if (fdc_in(fdc, ptr) < 0) {
492dc5df763SJoerg Wunsch 			char msg[50];
4932127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
494dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
495dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
4961b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
497a7e943a4SPyun YongHyeon 			va_end(ap);
4986182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
499dc5df763SJoerg Wunsch 		}
500dc5df763SJoerg Wunsch 	}
501a7e943a4SPyun YongHyeon 	va_end(ap);
5021b67be7bSPoul-Henning Kamp 	return (0);
503dc5df763SJoerg Wunsch }
504dc5df763SJoerg Wunsch 
5051b67be7bSPoul-Henning Kamp static void
5061b67be7bSPoul-Henning Kamp fdc_reset(struct fdc_data *fdc)
507d66c374fSTor Egge {
5081b67be7bSPoul-Henning Kamp 	int i, r[10];
509d66c374fSTor Egge 
51050d99d1aSJung-uk Kim 	if (fdc->fdct == FDC_ENHANCED) {
51150d99d1aSJung-uk Kim 		/* Try a software reset, default precomp, and 500 kb/s */
51250d99d1aSJung-uk Kim 		fddsr_wr(fdc, I8207X_DSR_SR);
51350d99d1aSJung-uk Kim 	} else {
51450d99d1aSJung-uk Kim 		/* Try a hardware reset, keep motor on */
5151b67be7bSPoul-Henning Kamp 		fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
5161b67be7bSPoul-Henning Kamp 		DELAY(100);
5171b67be7bSPoul-Henning Kamp 		/* enable FDC, but defer interrupts a moment */
5181b67be7bSPoul-Henning Kamp 		fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
51919a388e6SJung-uk Kim 	}
5201b67be7bSPoul-Henning Kamp 	DELAY(100);
5211b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
522d66c374fSTor Egge 
5231b67be7bSPoul-Henning Kamp 	/* XXX after a reset, silently believe the FDC will accept commands */
5241b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
5251b67be7bSPoul-Henning Kamp 		device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
526d66c374fSTor Egge 
5271b67be7bSPoul-Henning Kamp 	if (fdc->fdct == FDC_ENHANCED) {
5281b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 4,
5290da90eb8SJung-uk Kim 		    I8207X_CONFIG,
5301b67be7bSPoul-Henning Kamp 		    0,
5311b67be7bSPoul-Henning Kamp 		    0x40 |			/* Enable Implied Seek */
5321b67be7bSPoul-Henning Kamp 		    0x10 |			/* Polling disabled */
5331b67be7bSPoul-Henning Kamp 		    (fifo_threshold - 1),	/* Fifo threshold */
5341b67be7bSPoul-Henning Kamp 		    0x00,			/* Precomp track */
5351b67be7bSPoul-Henning Kamp 		    0))
5361b67be7bSPoul-Henning Kamp 			device_printf(fdc->fdc_dev,
5371b67be7bSPoul-Henning Kamp 			    " CONFIGURE failed in reset\n");
5381b67be7bSPoul-Henning Kamp 		if (debugflags & 1) {
5391b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 1,
5400da90eb8SJung-uk Kim 			    I8207X_DUMPREG,
5411b67be7bSPoul-Henning Kamp 			    10, &r[0], &r[1], &r[2], &r[3], &r[4],
5421b67be7bSPoul-Henning Kamp 			    &r[5], &r[6], &r[7], &r[8], &r[9]))
5431b67be7bSPoul-Henning Kamp 				device_printf(fdc->fdc_dev,
5441b67be7bSPoul-Henning Kamp 				    " DUMPREG failed in reset\n");
5451b67be7bSPoul-Henning Kamp 			for (i = 0; i < 10; i++)
5461b67be7bSPoul-Henning Kamp 				printf(" %02x", r[i]);
5471b67be7bSPoul-Henning Kamp 			printf("\n");
548d66c374fSTor Egge 		}
549d0900d6bSJoerg Wunsch 	}
550d66c374fSTor Egge }
551d66c374fSTor Egge 
552d66c374fSTor Egge static int
5531b67be7bSPoul-Henning Kamp fdc_sense_drive(struct fdc_data *fdc, int *st3p)
554dc5df763SJoerg Wunsch {
555dc5df763SJoerg Wunsch 	int st3;
556dc5df763SJoerg Wunsch 
5571b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
5581b67be7bSPoul-Henning Kamp 		return (fdc_err(fdc, "Sense Drive Status failed\n"));
559dc5df763SJoerg Wunsch 	if (st3p)
560dc5df763SJoerg Wunsch 		*st3p = st3;
5611b67be7bSPoul-Henning Kamp 	return (0);
562dc5df763SJoerg Wunsch }
563dc5df763SJoerg Wunsch 
5646f4e0bebSPoul-Henning Kamp static int
5651b67be7bSPoul-Henning Kamp fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
566dc5df763SJoerg Wunsch {
5676182fdbdSPeter Wemm 	int cyl, st0, ret;
568dc5df763SJoerg Wunsch 
5691b67be7bSPoul-Henning Kamp 	ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
5706182fdbdSPeter Wemm 	if (ret) {
5711b67be7bSPoul-Henning Kamp 		(void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
5721b67be7bSPoul-Henning Kamp 		return (ret);
573dc5df763SJoerg Wunsch 	}
574dc5df763SJoerg Wunsch 
575dc5df763SJoerg Wunsch 	if (st0p)
576dc5df763SJoerg Wunsch 		*st0p = st0;
577dc5df763SJoerg Wunsch 
5786182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
579dc5df763SJoerg Wunsch 		/*
580dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
581dc5df763SJoerg Wunsch 		 */
5821b67be7bSPoul-Henning Kamp 		return (FD_NOT_VALID);
583dc5df763SJoerg Wunsch 	}
584dc5df763SJoerg Wunsch 
5851b67be7bSPoul-Henning Kamp 	if (fdc_in(fdc, &cyl) < 0)
5866182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
587dc5df763SJoerg Wunsch 
588dc5df763SJoerg Wunsch 	if (cylp)
589dc5df763SJoerg Wunsch 		*cylp = cyl;
590dc5df763SJoerg Wunsch 
5911b67be7bSPoul-Henning Kamp 	return (0);
592dc5df763SJoerg Wunsch }
593dc5df763SJoerg Wunsch 
5946f4e0bebSPoul-Henning Kamp static int
5951b67be7bSPoul-Henning Kamp fdc_read_status(struct fdc_data *fdc)
596dc5df763SJoerg Wunsch {
5971b67be7bSPoul-Henning Kamp 	int i, ret, status;
598b5e8ce9fSBruce Evans 
599246ed35dSJoerg Wunsch 	for (i = ret = 0; i < 7; i++) {
6001b67be7bSPoul-Henning Kamp 		ret = fdc_in(fdc, &status);
601b5e8ce9fSBruce Evans 		fdc->status[i] = status;
602b5e8ce9fSBruce Evans 		if (ret != 0)
603dc5df763SJoerg Wunsch 			break;
604dc5df763SJoerg Wunsch 	}
605dc5df763SJoerg Wunsch 
606dc5df763SJoerg Wunsch 	if (ret == 0)
607dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
608dc5df763SJoerg Wunsch 	else
609dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
610dc5df763SJoerg Wunsch 
611dc5df763SJoerg Wunsch 	return ret;
612dc5df763SJoerg Wunsch }
613dc5df763SJoerg Wunsch 
6141b67be7bSPoul-Henning Kamp /*
6151b67be7bSPoul-Henning Kamp  * Select this drive
6161b67be7bSPoul-Henning Kamp  */
6171b67be7bSPoul-Henning Kamp static void
6181b67be7bSPoul-Henning Kamp fd_select(struct fd_data *fd)
6191b67be7bSPoul-Henning Kamp {
6201b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
6211b67be7bSPoul-Henning Kamp 
6221b67be7bSPoul-Henning Kamp 	/* XXX: lock controller */
6231b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
6241b67be7bSPoul-Henning Kamp 	fdc->fdout &= ~FDO_FDSEL;
6251b67be7bSPoul-Henning Kamp 	fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
6261b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
6271b67be7bSPoul-Henning Kamp }
6281b67be7bSPoul-Henning Kamp 
6291b67be7bSPoul-Henning Kamp static void
6301b67be7bSPoul-Henning Kamp fd_turnon(void *arg)
6311b67be7bSPoul-Henning Kamp {
6321b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
6331b67be7bSPoul-Henning Kamp 	struct bio *bp;
6341b67be7bSPoul-Henning Kamp 	int once;
6351b67be7bSPoul-Henning Kamp 
6361b67be7bSPoul-Henning Kamp 	fd = arg;
6379fc9d84bSJohn Baldwin 	mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
6381b67be7bSPoul-Henning Kamp 	fd->flags &= ~FD_MOTORWAIT;
6391b67be7bSPoul-Henning Kamp 	fd->flags |= FD_MOTOR;
6401b67be7bSPoul-Henning Kamp 	once = 0;
6411b67be7bSPoul-Henning Kamp 	for (;;) {
6421b67be7bSPoul-Henning Kamp 		bp = bioq_takefirst(&fd->fd_bq);
6431b67be7bSPoul-Henning Kamp 		if (bp == NULL)
6441b67be7bSPoul-Henning Kamp 			break;
6451b67be7bSPoul-Henning Kamp 		bioq_disksort(&fd->fdc->head, bp);
6461b67be7bSPoul-Henning Kamp 		once = 1;
6471b67be7bSPoul-Henning Kamp 	}
6481b67be7bSPoul-Henning Kamp 	if (once)
6491b67be7bSPoul-Henning Kamp 		wakeup(&fd->fdc->head);
6501b67be7bSPoul-Henning Kamp }
6511b67be7bSPoul-Henning Kamp 
6521b67be7bSPoul-Henning Kamp static void
6531b67be7bSPoul-Henning Kamp fd_motor(struct fd_data *fd, int turnon)
6541b67be7bSPoul-Henning Kamp {
6551b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
6561b67be7bSPoul-Henning Kamp 
6571b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
6581b67be7bSPoul-Henning Kamp /*
6591b67be7bSPoul-Henning Kamp 	mtx_assert(&fdc->fdc_mtx, MA_OWNED);
6601b67be7bSPoul-Henning Kamp */
6611b67be7bSPoul-Henning Kamp 	if (turnon) {
6621b67be7bSPoul-Henning Kamp 		fd->flags |= FD_MOTORWAIT;
6631b67be7bSPoul-Henning Kamp 		fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
6641b67be7bSPoul-Henning Kamp 		callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
6651b67be7bSPoul-Henning Kamp 	} else {
6669fc9d84bSJohn Baldwin 		callout_stop(&fd->toffhandle);
6671b67be7bSPoul-Henning Kamp 		fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
6681b67be7bSPoul-Henning Kamp 		fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
6691b67be7bSPoul-Henning Kamp 	}
6701b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
6711b67be7bSPoul-Henning Kamp }
6721b67be7bSPoul-Henning Kamp 
6731b67be7bSPoul-Henning Kamp static void
6741b67be7bSPoul-Henning Kamp fd_turnoff(void *xfd)
6751b67be7bSPoul-Henning Kamp {
6761b67be7bSPoul-Henning Kamp 	struct fd_data *fd = xfd;
6771b67be7bSPoul-Henning Kamp 
6789fc9d84bSJohn Baldwin 	mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
6791b67be7bSPoul-Henning Kamp 	fd_motor(fd, 0);
6801b67be7bSPoul-Henning Kamp }
6811b67be7bSPoul-Henning Kamp 
6821b67be7bSPoul-Henning Kamp /*
68361909e4cSPoul-Henning Kamp  * fdc_intr - wake up the worker thread.
6841b67be7bSPoul-Henning Kamp  */
68561909e4cSPoul-Henning Kamp 
6861b67be7bSPoul-Henning Kamp static void
6871b67be7bSPoul-Henning Kamp fdc_intr(void *arg)
6881b67be7bSPoul-Henning Kamp {
6891b67be7bSPoul-Henning Kamp 
6901b67be7bSPoul-Henning Kamp 	wakeup(arg);
6911b67be7bSPoul-Henning Kamp }
6921b67be7bSPoul-Henning Kamp 
693ef544f63SPaolo Pisati static int
694ef544f63SPaolo Pisati fdc_intr_fast(void *arg)
695ef544f63SPaolo Pisati {
696ef544f63SPaolo Pisati 
697ef544f63SPaolo Pisati 	wakeup(arg);
698ef544f63SPaolo Pisati 	return(FILTER_HANDLED);
699ef544f63SPaolo Pisati }
700ef544f63SPaolo Pisati 
7011b67be7bSPoul-Henning Kamp /*
7021b67be7bSPoul-Henning Kamp  * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
7031b67be7bSPoul-Henning Kamp  */
7041b67be7bSPoul-Henning Kamp static void
7051b67be7bSPoul-Henning Kamp fdc_pio(struct fdc_data *fdc)
7061b67be7bSPoul-Henning Kamp {
7071b67be7bSPoul-Henning Kamp 	u_char *cptr;
7081b67be7bSPoul-Henning Kamp 	struct bio *bp;
7091b67be7bSPoul-Henning Kamp 	u_int count;
7101b67be7bSPoul-Henning Kamp 
7111b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
7121b67be7bSPoul-Henning Kamp 	cptr = fdc->fd->fd_ioptr;
7131b67be7bSPoul-Henning Kamp 	count = fdc->fd->fd_iosize;
7141b67be7bSPoul-Henning Kamp 
7151b67be7bSPoul-Henning Kamp 	if (bp->bio_cmd == BIO_READ) {
716b4046cd7SWarner Losh 		fdbcdr_wr(fdc, 0, count);
717973bfe6cSWarner Losh 		bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
718973bfe6cSWarner Losh 		    fdc->ioff[FD_YE_DATAPORT], cptr, count);
7191b67be7bSPoul-Henning Kamp 	} else {
720973bfe6cSWarner Losh 		bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
721973bfe6cSWarner Losh 		    fdc->ioff[FD_YE_DATAPORT], cptr, count);
722b4046cd7SWarner Losh 		fdbcdr_wr(fdc, 0, count);	/* needed? */
7231b67be7bSPoul-Henning Kamp 	}
7241b67be7bSPoul-Henning Kamp }
7251b67be7bSPoul-Henning Kamp 
7261b67be7bSPoul-Henning Kamp static int
7271b67be7bSPoul-Henning Kamp fdc_biodone(struct fdc_data *fdc, int error)
7281b67be7bSPoul-Henning Kamp {
7291b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
7301b67be7bSPoul-Henning Kamp 	struct bio *bp;
7311b67be7bSPoul-Henning Kamp 
7321b67be7bSPoul-Henning Kamp 	fd = fdc->fd;
7331b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
7341b67be7bSPoul-Henning Kamp 
7351b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
7361b67be7bSPoul-Henning Kamp 	if (--fd->fd_iocount == 0)
7371b67be7bSPoul-Henning Kamp 		callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
7381b67be7bSPoul-Henning Kamp 	fdc->bp = NULL;
7391b67be7bSPoul-Henning Kamp 	fdc->fd = NULL;
7401b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
7411b67be7bSPoul-Henning Kamp 	if (bp->bio_to != NULL) {
7421b67be7bSPoul-Henning Kamp 		if ((debugflags & 2) && fd->fdc->retry > 0)
7431b67be7bSPoul-Henning Kamp 			printf("retries: %d\n", fd->fdc->retry);
7441b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, error);
7451b67be7bSPoul-Henning Kamp 		return (0);
7461b67be7bSPoul-Henning Kamp 	}
7471b67be7bSPoul-Henning Kamp 	bp->bio_error = error;
7481b67be7bSPoul-Henning Kamp 	bp->bio_flags |= BIO_DONE;
7491b67be7bSPoul-Henning Kamp 	wakeup(bp);
7501b67be7bSPoul-Henning Kamp 	return (0);
7511b67be7bSPoul-Henning Kamp }
7521b67be7bSPoul-Henning Kamp 
7531b67be7bSPoul-Henning Kamp static int retry_line;
7541b67be7bSPoul-Henning Kamp 
7551b67be7bSPoul-Henning Kamp static int
7561b67be7bSPoul-Henning Kamp fdc_worker(struct fdc_data *fdc)
7571b67be7bSPoul-Henning Kamp {
7581b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
7591b67be7bSPoul-Henning Kamp 	struct bio *bp;
7601b67be7bSPoul-Henning Kamp 	int i, nsect;
7611b67be7bSPoul-Henning Kamp 	int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
7621b67be7bSPoul-Henning Kamp 	int head;
7631b67be7bSPoul-Henning Kamp 	static int need_recal;
7641b67be7bSPoul-Henning Kamp 	struct fdc_readid *idp;
7651b67be7bSPoul-Henning Kamp 	struct fd_formb *finfo;
7661b67be7bSPoul-Henning Kamp 
7671b67be7bSPoul-Henning Kamp 	/* Have we exhausted our retries ? */
7681b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
7691b67be7bSPoul-Henning Kamp 	fd = fdc->fd;
7701b67be7bSPoul-Henning Kamp 	if (bp != NULL &&
7711217bf6aSCraig Rodrigues 		(fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
7721b67be7bSPoul-Henning Kamp 		if ((debugflags & 4))
7731b67be7bSPoul-Henning Kamp 			printf("Too many retries (EIO)\n");
774c884498fSJung-uk Kim 		if (fdc->flags & FDC_NEEDS_RESET) {
775b31da03fSJung-uk Kim 			mtx_lock(&fdc->fdc_mtx);
776b31da03fSJung-uk Kim 			fd->flags |= FD_EMPTY;
777b31da03fSJung-uk Kim 			mtx_unlock(&fdc->fdc_mtx);
778c884498fSJung-uk Kim 		}
7791b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, EIO));
7801b67be7bSPoul-Henning Kamp 	}
7811b67be7bSPoul-Henning Kamp 
7821b67be7bSPoul-Henning Kamp 	/* Disable ISADMA if we bailed while it was active */
7831b67be7bSPoul-Henning Kamp 	if (fd != NULL && (fd->flags & FD_ISADMA)) {
7841b67be7bSPoul-Henning Kamp 		mtx_lock(&Giant);
7851b67be7bSPoul-Henning Kamp 		isa_dmadone(
7861b67be7bSPoul-Henning Kamp 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
7871b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
7881b67be7bSPoul-Henning Kamp 		mtx_unlock(&Giant);
78961909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
7901b67be7bSPoul-Henning Kamp 		fd->flags &= ~FD_ISADMA;
79161909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
7921b67be7bSPoul-Henning Kamp 	}
7931b67be7bSPoul-Henning Kamp 
7941b67be7bSPoul-Henning Kamp 	/* Unwedge the controller ? */
7951b67be7bSPoul-Henning Kamp 	if (fdc->flags & FDC_NEEDS_RESET) {
7961b67be7bSPoul-Henning Kamp 		fdc->flags &= ~FDC_NEEDS_RESET;
7971b67be7bSPoul-Henning Kamp 		fdc_reset(fdc);
7986e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdcrst", hz);
7991b67be7bSPoul-Henning Kamp 		/* Discard results */
8001b67be7bSPoul-Henning Kamp 		for (i = 0; i < 4; i++)
8011b67be7bSPoul-Henning Kamp 			fdc_sense_int(fdc, &st0, &cyl);
8021b67be7bSPoul-Henning Kamp 		/* All drives must recal */
8031b67be7bSPoul-Henning Kamp 		need_recal = 0xf;
8041b67be7bSPoul-Henning Kamp 	}
8051b67be7bSPoul-Henning Kamp 
8061b67be7bSPoul-Henning Kamp 	/* Pick up a request, if need be wait for it */
8071b67be7bSPoul-Henning Kamp 	if (fdc->bp == NULL) {
8081b67be7bSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
8091b67be7bSPoul-Henning Kamp 		do {
8101b67be7bSPoul-Henning Kamp 			fdc->bp = bioq_takefirst(&fdc->head);
8111b67be7bSPoul-Henning Kamp 			if (fdc->bp == NULL)
8121b67be7bSPoul-Henning Kamp 				msleep(&fdc->head, &fdc->fdc_mtx,
8131b67be7bSPoul-Henning Kamp 				    PRIBIO, "-", hz);
81402910eeeSJoerg Wunsch 		} while (fdc->bp == NULL &&
81502910eeeSJoerg Wunsch 		    (fdc->flags & FDC_KTHREAD_EXIT) == 0);
8161b67be7bSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
81702910eeeSJoerg Wunsch 
81802910eeeSJoerg Wunsch 		if (fdc->bp == NULL)
81902910eeeSJoerg Wunsch 			/*
82002910eeeSJoerg Wunsch 			 * Nothing to do, worker thread has been
82102910eeeSJoerg Wunsch 			 * requested to stop.
82202910eeeSJoerg Wunsch 			 */
82302910eeeSJoerg Wunsch 			return (0);
82402910eeeSJoerg Wunsch 
8251b67be7bSPoul-Henning Kamp 		bp = fdc->bp;
8261b67be7bSPoul-Henning Kamp 		fd = fdc->fd = bp->bio_driver1;
8271b67be7bSPoul-Henning Kamp 		fdc->retry = 0;
8281b67be7bSPoul-Henning Kamp 		fd->fd_ioptr = bp->bio_data;
8291b67be7bSPoul-Henning Kamp 		if (bp->bio_cmd & BIO_FMT) {
8301b67be7bSPoul-Henning Kamp 			i = offsetof(struct fd_formb, fd_formb_cylno(0));
8311b67be7bSPoul-Henning Kamp 			fd->fd_ioptr += i;
8321b67be7bSPoul-Henning Kamp 			fd->fd_iosize = bp->bio_length - i;
8331b67be7bSPoul-Henning Kamp 		}
8341b67be7bSPoul-Henning Kamp 	}
8351b67be7bSPoul-Henning Kamp 
8361b67be7bSPoul-Henning Kamp 	/* Select drive, setup params */
8371b67be7bSPoul-Henning Kamp 	fd_select(fd);
83850d99d1aSJung-uk Kim 	if (fdc->fdct == FDC_ENHANCED)
83950d99d1aSJung-uk Kim 		fddsr_wr(fdc, fd->ft->trans);
84050d99d1aSJung-uk Kim 	else
8411b67be7bSPoul-Henning Kamp 		fdctl_wr(fdc, fd->ft->trans);
8421b67be7bSPoul-Henning Kamp 
8431b67be7bSPoul-Henning Kamp 	if (bp->bio_cmd & BIO_PROBE) {
844c884498fSJung-uk Kim 		if ((!(device_get_flags(fd->dev) & FD_NO_CHLINE) &&
845c884498fSJung-uk Kim 		    !(fdin_rd(fdc) & FDI_DCHG) &&
846c884498fSJung-uk Kim 		    !(fd->flags & FD_EMPTY)) ||
847c884498fSJung-uk Kim 		    fd_probe_disk(fd, &need_recal) == 0)
8481b67be7bSPoul-Henning Kamp 			return (fdc_biodone(fdc, 0));
8491b67be7bSPoul-Henning Kamp 		return (1);
8501b67be7bSPoul-Henning Kamp 	}
8511b67be7bSPoul-Henning Kamp 
8521b67be7bSPoul-Henning Kamp 	/*
8531b67be7bSPoul-Henning Kamp 	 * If we are dead just flush the requests
8541b67be7bSPoul-Henning Kamp 	 */
8551b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_EMPTY)
8561b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, ENXIO));
8571b67be7bSPoul-Henning Kamp 
8581b67be7bSPoul-Henning Kamp 	/* Check if we lost our media */
8591b67be7bSPoul-Henning Kamp 	if (fdin_rd(fdc) & FDI_DCHG) {
8601b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
8611b67be7bSPoul-Henning Kamp 			printf("Lost disk\n");
86261909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
8631b67be7bSPoul-Henning Kamp 		fd->flags |= FD_EMPTY;
8641b67be7bSPoul-Henning Kamp 		fd->flags |= FD_NEWDISK;
86561909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
8661b67be7bSPoul-Henning Kamp 		g_topology_lock();
8671b67be7bSPoul-Henning Kamp 		g_orphan_provider(fd->fd_provider, EXDEV);
8681b67be7bSPoul-Henning Kamp 		fd->fd_provider->flags |= G_PF_WITHER;
8691b67be7bSPoul-Henning Kamp 		fd->fd_provider =
8701b67be7bSPoul-Henning Kamp 		    g_new_providerf(fd->fd_geom, fd->fd_geom->name);
8711b67be7bSPoul-Henning Kamp 		g_error_provider(fd->fd_provider, 0);
8721b67be7bSPoul-Henning Kamp 		g_topology_unlock();
8731b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, ENXIO));
8741b67be7bSPoul-Henning Kamp 	}
8751b67be7bSPoul-Henning Kamp 
8761b67be7bSPoul-Henning Kamp 	/* Check if the floppy is write-protected */
8771b67be7bSPoul-Henning Kamp 	if(bp->bio_cmd & (BIO_FMT | BIO_WRITE)) {
8781b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8791b67be7bSPoul-Henning Kamp 		if(fdc_sense_drive(fdc, &st3) != 0)
8801b67be7bSPoul-Henning Kamp 			return (1);
8811b67be7bSPoul-Henning Kamp 		if(st3 & NE7_ST3_WP)
8821b67be7bSPoul-Henning Kamp 			return (fdc_biodone(fdc, EROFS));
8831b67be7bSPoul-Henning Kamp 	}
8841b67be7bSPoul-Henning Kamp 
8851b67be7bSPoul-Henning Kamp 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
8861b67be7bSPoul-Henning Kamp 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
8871b67be7bSPoul-Henning Kamp 	i = fd->ft->sectrac * fd->ft->heads;
8881b67be7bSPoul-Henning Kamp 	cylinder = bp->bio_pblkno / i;
8891b67be7bSPoul-Henning Kamp 	descyl = cylinder * steptrac;
8901b67be7bSPoul-Henning Kamp 	sec = bp->bio_pblkno % i;
8911b67be7bSPoul-Henning Kamp 	nsect = i - sec;
8921b67be7bSPoul-Henning Kamp 	head = sec / fd->ft->sectrac;
8931b67be7bSPoul-Henning Kamp 	sec = sec % fd->ft->sectrac + 1;
8941b67be7bSPoul-Henning Kamp 
8951b67be7bSPoul-Henning Kamp 	/* If everything is going swimmingly, use multisector xfer */
8961b67be7bSPoul-Henning Kamp 	if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
8971b67be7bSPoul-Henning Kamp 		fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
8981b67be7bSPoul-Henning Kamp 		nsect = fd->fd_iosize / fd->sectorsize;
8991b67be7bSPoul-Henning Kamp 	} else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
9001b67be7bSPoul-Henning Kamp 		fd->fd_iosize = fd->sectorsize;
9011b67be7bSPoul-Henning Kamp 		nsect = 1;
9021b67be7bSPoul-Henning Kamp 	}
9031b67be7bSPoul-Henning Kamp 
9041b67be7bSPoul-Henning Kamp 	/* Do RECAL if we need to or are going to track zero anyway */
9051b67be7bSPoul-Henning Kamp 	if ((need_recal & (1 << fd->fdsu)) ||
9061b67be7bSPoul-Henning Kamp 	    (cylinder == 0 && fd->track != 0) ||
9071b67be7bSPoul-Henning Kamp 	    fdc->retry > 2) {
9081b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9091b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
9101b67be7bSPoul-Henning Kamp 			return (1);
9116e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdrecal", hz);
9121b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9131b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
9141b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
9151b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9161b67be7bSPoul-Henning Kamp 		if ((st0 & 0xc0) || cyl != 0)
9171b67be7bSPoul-Henning Kamp 			return (1);
9181b67be7bSPoul-Henning Kamp 		need_recal &= ~(1 << fd->fdsu);
9191b67be7bSPoul-Henning Kamp 		fd->track = 0;
9201b67be7bSPoul-Henning Kamp 		/* let the heads settle */
9211b67be7bSPoul-Henning Kamp 		if (settle)
9226e50e38fSJohn Baldwin 			tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
9231b67be7bSPoul-Henning Kamp 	}
9241b67be7bSPoul-Henning Kamp 
9251b67be7bSPoul-Henning Kamp 	/*
9261b67be7bSPoul-Henning Kamp 	 * SEEK to where we want to be
9271b67be7bSPoul-Henning Kamp 	 *
9281b67be7bSPoul-Henning Kamp 	 * Enhanced controllers do implied seeks for read&write as long as
9291b67be7bSPoul-Henning Kamp 	 * we do not need multiple steps per track.
9301b67be7bSPoul-Henning Kamp 	 */
9311b67be7bSPoul-Henning Kamp 	if (cylinder != fd->track && (
9321b67be7bSPoul-Henning Kamp 	    fdc->fdct != FDC_ENHANCED ||
9331b67be7bSPoul-Henning Kamp 	    descyl != cylinder ||
9341b67be7bSPoul-Henning Kamp 	    (bp->bio_cmd & (BIO_RDID|BIO_FMT)))) {
9351b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9361b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
9371b67be7bSPoul-Henning Kamp 			return (1);
9386e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdseek", hz);
9391b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9401b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
9411b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
9421b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9431b67be7bSPoul-Henning Kamp 		if ((st0 & 0xc0) || cyl != descyl) {
9441b67be7bSPoul-Henning Kamp 			need_recal |= (1 << fd->fdsu);
9451b67be7bSPoul-Henning Kamp 			return (1);
9461b67be7bSPoul-Henning Kamp 		}
9471b67be7bSPoul-Henning Kamp 		/* let the heads settle */
9481b67be7bSPoul-Henning Kamp 		if (settle)
9496e50e38fSJohn Baldwin 			tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
9501b67be7bSPoul-Henning Kamp 	}
9511b67be7bSPoul-Henning Kamp 	fd->track = cylinder;
9521b67be7bSPoul-Henning Kamp 
9531b67be7bSPoul-Henning Kamp 	if (debugflags & 8)
9541b67be7bSPoul-Henning Kamp 		printf("op %x bn %ju siz %u ptr %p retry %d\n",
9551b67be7bSPoul-Henning Kamp 		    bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
9561b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fdc->retry);
9571b67be7bSPoul-Henning Kamp 
9581b67be7bSPoul-Henning Kamp 	/* Setup ISADMA if we need it and have it */
9591b67be7bSPoul-Henning Kamp 	if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
9601b67be7bSPoul-Henning Kamp 	     && !(fdc->flags & FDC_NODMA)) {
9611b67be7bSPoul-Henning Kamp 		mtx_lock(&Giant);
9621b67be7bSPoul-Henning Kamp 		isa_dmastart(
9631b67be7bSPoul-Henning Kamp 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
9641b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
9651b67be7bSPoul-Henning Kamp 		mtx_unlock(&Giant);
96661909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
9671b67be7bSPoul-Henning Kamp 		fd->flags |= FD_ISADMA;
96861909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
9691b67be7bSPoul-Henning Kamp 	}
9701b67be7bSPoul-Henning Kamp 
9711b67be7bSPoul-Henning Kamp 	/* Do PIO if we have to */
9721b67be7bSPoul-Henning Kamp 	if (fdc->flags & FDC_NODMA) {
9731b67be7bSPoul-Henning Kamp 		if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
974b4046cd7SWarner Losh 			fdbcdr_wr(fdc, 1, fd->fd_iosize);
9751b67be7bSPoul-Henning Kamp 		if (bp->bio_cmd & (BIO_WRITE|BIO_FMT))
9761b67be7bSPoul-Henning Kamp 			fdc_pio(fdc);
9771b67be7bSPoul-Henning Kamp 	}
9781b67be7bSPoul-Henning Kamp 
9791b67be7bSPoul-Henning Kamp 	switch(bp->bio_cmd) {
9801b67be7bSPoul-Henning Kamp 	case BIO_FMT:
9811b67be7bSPoul-Henning Kamp 		/* formatting */
9821b67be7bSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
9831b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9841b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 6,
9851b67be7bSPoul-Henning Kamp 		    NE7CMD_FORMAT | mfm,
9861b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,
9871b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_secshift,
9881b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_nsecs,
9891b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_gaplen,
9901b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_fillbyte, 0))
9911b67be7bSPoul-Henning Kamp 			return (1);
9921b67be7bSPoul-Henning Kamp 		break;
9931b67be7bSPoul-Henning Kamp 	case BIO_RDID:
9941b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9951b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 2,
9961b67be7bSPoul-Henning Kamp 		    NE7CMD_READID | mfm,
9971b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu, 0))
9981b67be7bSPoul-Henning Kamp 			return (1);
9991b67be7bSPoul-Henning Kamp 		break;
10001b67be7bSPoul-Henning Kamp 	case BIO_READ:
10011b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10021b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 9,
10031b67be7bSPoul-Henning Kamp 		    NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
10041b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,	/* head & unit */
10051b67be7bSPoul-Henning Kamp 		    fd->track,			/* track */
10061b67be7bSPoul-Henning Kamp 		    head,			/* head */
10071b67be7bSPoul-Henning Kamp 		    sec,			/* sector + 1 */
10081b67be7bSPoul-Henning Kamp 		    fd->ft->secsize,		/* sector size */
10091b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac,		/* sectors/track */
10101b67be7bSPoul-Henning Kamp 		    fd->ft->gap,		/* gap size */
10111b67be7bSPoul-Henning Kamp 		    fd->ft->datalen,		/* data length */
10121b67be7bSPoul-Henning Kamp 		    0))
10131b67be7bSPoul-Henning Kamp 			return (1);
10141b67be7bSPoul-Henning Kamp 		break;
10151b67be7bSPoul-Henning Kamp 	case BIO_WRITE:
10161b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10171b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 9,
10181b67be7bSPoul-Henning Kamp 		    NE7CMD_WRITE | mfm | NE7CMD_MT,
10191b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,	/* head & unit */
10201b67be7bSPoul-Henning Kamp 		    fd->track,			/* track */
10211b67be7bSPoul-Henning Kamp 		    head,			/* head */
10221b67be7bSPoul-Henning Kamp 		    sec,			/* sector + 1 */
10231b67be7bSPoul-Henning Kamp 		    fd->ft->secsize,		/* sector size */
10241b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac,		/* sectors/track */
10251b67be7bSPoul-Henning Kamp 		    fd->ft->gap,		/* gap size */
10261b67be7bSPoul-Henning Kamp 		    fd->ft->datalen,		/* data length */
10271b67be7bSPoul-Henning Kamp 		    0))
10281b67be7bSPoul-Henning Kamp 			return (1);
10291b67be7bSPoul-Henning Kamp 		break;
10301b67be7bSPoul-Henning Kamp 	default:
10311b67be7bSPoul-Henning Kamp 		KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
10321b67be7bSPoul-Henning Kamp 	}
10331b67be7bSPoul-Henning Kamp 
10341b67be7bSPoul-Henning Kamp 	/* Wait for interrupt */
10356e50e38fSJohn Baldwin 	i = tsleep(fdc, PRIBIO, "fddata", hz);
10361b67be7bSPoul-Henning Kamp 
10371b67be7bSPoul-Henning Kamp 	/* PIO if the read looks good */
10381b67be7bSPoul-Henning Kamp 	if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ))
10391b67be7bSPoul-Henning Kamp 		fdc_pio(fdc);
10401b67be7bSPoul-Henning Kamp 
10411b67be7bSPoul-Henning Kamp 	/* Finish DMA */
10421b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_ISADMA) {
10431b67be7bSPoul-Henning Kamp 		mtx_lock(&Giant);
10441b67be7bSPoul-Henning Kamp 		isa_dmadone(
10451b67be7bSPoul-Henning Kamp 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
10461b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
10471b67be7bSPoul-Henning Kamp 		mtx_unlock(&Giant);
104861909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
10491b67be7bSPoul-Henning Kamp 		fd->flags &= ~FD_ISADMA;
105061909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
10511b67be7bSPoul-Henning Kamp 	}
10521b67be7bSPoul-Henning Kamp 
10531b67be7bSPoul-Henning Kamp 	if (i != 0) {
10541b67be7bSPoul-Henning Kamp 		/*
10551b67be7bSPoul-Henning Kamp 		 * Timeout.
10561b67be7bSPoul-Henning Kamp 		 *
10571b67be7bSPoul-Henning Kamp 		 * Due to IBM's brain-dead design, the FDC has a faked ready
10581b67be7bSPoul-Henning Kamp 		 * signal, hardwired to ready == true. Thus, any command
10591b67be7bSPoul-Henning Kamp 		 * issued if there's no diskette in the drive will _never_
10601b67be7bSPoul-Henning Kamp 		 * complete, and must be aborted by resetting the FDC.
10611b67be7bSPoul-Henning Kamp 		 * Many thanks, Big Blue!
10621b67be7bSPoul-Henning Kamp 		 */
10631b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10641b67be7bSPoul-Henning Kamp 		fdc->flags |= FDC_NEEDS_RESET;
10651b67be7bSPoul-Henning Kamp 		return (1);
10661b67be7bSPoul-Henning Kamp 	}
10671b67be7bSPoul-Henning Kamp 
10681b67be7bSPoul-Henning Kamp 	retry_line = __LINE__;
10691b67be7bSPoul-Henning Kamp 	if (fdc_read_status(fdc))
10701b67be7bSPoul-Henning Kamp 		return (1);
10711b67be7bSPoul-Henning Kamp 
10721b67be7bSPoul-Henning Kamp 	if (debugflags & 0x10)
10731b67be7bSPoul-Henning Kamp 		printf("  -> %x %x %x %x\n",
10741b67be7bSPoul-Henning Kamp 		    fdc->status[0], fdc->status[1],
10751b67be7bSPoul-Henning Kamp 		    fdc->status[2], fdc->status[3]);
10761b67be7bSPoul-Henning Kamp 
10771b67be7bSPoul-Henning Kamp 	st0 = fdc->status[0] & NE7_ST0_IC;
10781b67be7bSPoul-Henning Kamp 	if (st0 != 0) {
10791b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10801b67be7bSPoul-Henning Kamp 		if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
10811b67be7bSPoul-Henning Kamp 			/*
10821b67be7bSPoul-Henning Kamp 			 * DMA overrun. Someone hogged the bus and
10831b67be7bSPoul-Henning Kamp 			 * didn't release it in time for the next
10841b67be7bSPoul-Henning Kamp 			 * FDC transfer.
10851b67be7bSPoul-Henning Kamp 			 */
10861b67be7bSPoul-Henning Kamp 			return (1);
10871b67be7bSPoul-Henning Kamp 		}
10881b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10891b67be7bSPoul-Henning Kamp 		if(st0 == NE7_ST0_IC_IV) {
10901b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
10911b67be7bSPoul-Henning Kamp 			return (1);
10921b67be7bSPoul-Henning Kamp 		}
10931b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10941b67be7bSPoul-Henning Kamp 		if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
10951b67be7bSPoul-Henning Kamp 			need_recal |= (1 << fd->fdsu);
10961b67be7bSPoul-Henning Kamp 			return (1);
10971b67be7bSPoul-Henning Kamp 		}
10981b67be7bSPoul-Henning Kamp 		if (debugflags & 0x20) {
10991b67be7bSPoul-Henning Kamp 			printf("status %02x %02x %02x %02x %02x %02x\n",
11001b67be7bSPoul-Henning Kamp 			    fdc->status[0], fdc->status[1], fdc->status[2],
11011b67be7bSPoul-Henning Kamp 			    fdc->status[3], fdc->status[4], fdc->status[5]);
11021b67be7bSPoul-Henning Kamp 		}
11031b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11041b67be7bSPoul-Henning Kamp 		return (1);
11051b67be7bSPoul-Henning Kamp 	}
11061b67be7bSPoul-Henning Kamp 	/* All OK */
11071b67be7bSPoul-Henning Kamp 	switch(bp->bio_cmd) {
11081b67be7bSPoul-Henning Kamp 	case BIO_RDID:
11091b67be7bSPoul-Henning Kamp 		/* copy out ID field contents */
11101b67be7bSPoul-Henning Kamp 		idp = (struct fdc_readid *)bp->bio_data;
11111b67be7bSPoul-Henning Kamp 		idp->cyl = fdc->status[3];
11121b67be7bSPoul-Henning Kamp 		idp->head = fdc->status[4];
11131b67be7bSPoul-Henning Kamp 		idp->sec = fdc->status[5];
11141b67be7bSPoul-Henning Kamp 		idp->secshift = fdc->status[6];
11151b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
11161b67be7bSPoul-Henning Kamp 			printf("c %d h %d s %d z %d\n",
11171b67be7bSPoul-Henning Kamp 			    idp->cyl, idp->head, idp->sec, idp->secshift);
11181b67be7bSPoul-Henning Kamp 		break;
11191b67be7bSPoul-Henning Kamp 	case BIO_READ:
11201b67be7bSPoul-Henning Kamp 	case BIO_WRITE:
11211b67be7bSPoul-Henning Kamp 		bp->bio_pblkno += nsect;
11221b67be7bSPoul-Henning Kamp 		bp->bio_resid -= fd->fd_iosize;
11231b67be7bSPoul-Henning Kamp 		bp->bio_completed += fd->fd_iosize;
11241b67be7bSPoul-Henning Kamp 		fd->fd_ioptr += fd->fd_iosize;
11251b67be7bSPoul-Henning Kamp 		/* Since we managed to get something done, reset the retry */
11261b67be7bSPoul-Henning Kamp 		fdc->retry = 0;
11271b67be7bSPoul-Henning Kamp 		if (bp->bio_resid > 0)
11281b67be7bSPoul-Henning Kamp 			return (0);
11291b67be7bSPoul-Henning Kamp 		break;
11301b67be7bSPoul-Henning Kamp 	case BIO_FMT:
11311b67be7bSPoul-Henning Kamp 		break;
11321b67be7bSPoul-Henning Kamp 	}
11331b67be7bSPoul-Henning Kamp 	return (fdc_biodone(fdc, 0));
11341b67be7bSPoul-Henning Kamp }
11351b67be7bSPoul-Henning Kamp 
11361b67be7bSPoul-Henning Kamp static void
11371b67be7bSPoul-Henning Kamp fdc_thread(void *arg)
11381b67be7bSPoul-Henning Kamp {
11391b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
11401b67be7bSPoul-Henning Kamp 
11411b67be7bSPoul-Henning Kamp 	fdc = arg;
11421b67be7bSPoul-Henning Kamp 	int i;
11431b67be7bSPoul-Henning Kamp 
11443fc95726SJoerg Wunsch 	mtx_lock(&fdc->fdc_mtx);
114502910eeeSJoerg Wunsch 	fdc->flags |= FDC_KTHREAD_ALIVE;
114602910eeeSJoerg Wunsch 	while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
11473fc95726SJoerg Wunsch 		mtx_unlock(&fdc->fdc_mtx);
11481b67be7bSPoul-Henning Kamp 		i = fdc_worker(fdc);
11491b67be7bSPoul-Henning Kamp 		if (i && debugflags & 0x20) {
11501b67be7bSPoul-Henning Kamp 			if (fdc->bp != NULL) {
11511b67be7bSPoul-Henning Kamp 				g_print_bio(fdc->bp);
11521b67be7bSPoul-Henning Kamp 				printf("\n");
11531b67be7bSPoul-Henning Kamp 			}
11541b67be7bSPoul-Henning Kamp 			printf("Retry line %d\n", retry_line);
11551b67be7bSPoul-Henning Kamp 		}
11561b67be7bSPoul-Henning Kamp 		fdc->retry += i;
11573fc95726SJoerg Wunsch 		mtx_lock(&fdc->fdc_mtx);
11581b67be7bSPoul-Henning Kamp 	}
115902910eeeSJoerg Wunsch 	fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
11603fc95726SJoerg Wunsch 	mtx_unlock(&fdc->fdc_mtx);
11613fc95726SJoerg Wunsch 
11623745c395SJulian Elischer 	kproc_exit(0);
11631b67be7bSPoul-Henning Kamp }
11641b67be7bSPoul-Henning Kamp 
11651b67be7bSPoul-Henning Kamp /*
1166360048a7SJoerg Wunsch  * Enqueue a request.
11671b67be7bSPoul-Henning Kamp  */
11681b67be7bSPoul-Henning Kamp static void
11691b67be7bSPoul-Henning Kamp fd_enqueue(struct fd_data *fd, struct bio *bp)
11701b67be7bSPoul-Henning Kamp {
11711b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
11721b67be7bSPoul-Henning Kamp 	int call;
11731b67be7bSPoul-Henning Kamp 
11741b67be7bSPoul-Henning Kamp 	call = 0;
11751b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
11761b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
11771b67be7bSPoul-Henning Kamp 	/* If we go from idle, cancel motor turnoff */
11781b67be7bSPoul-Henning Kamp 	if (fd->fd_iocount++ == 0)
11799fc9d84bSJohn Baldwin 		callout_stop(&fd->toffhandle);
11801b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_MOTOR) {
11811b67be7bSPoul-Henning Kamp 		/* The motor is on, send it directly to the controller */
11821b67be7bSPoul-Henning Kamp 		bioq_disksort(&fdc->head, bp);
11831b67be7bSPoul-Henning Kamp 		wakeup(&fdc->head);
11841b67be7bSPoul-Henning Kamp 	} else {
11851b67be7bSPoul-Henning Kamp 		/* Queue it on the drive until the motor has started */
11861b67be7bSPoul-Henning Kamp 		bioq_insert_tail(&fd->fd_bq, bp);
11871b67be7bSPoul-Henning Kamp 		if (!(fd->flags & FD_MOTORWAIT))
11881b67be7bSPoul-Henning Kamp 			fd_motor(fd, 1);
11891b67be7bSPoul-Henning Kamp 	}
11901b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
11911b67be7bSPoul-Henning Kamp }
11921b67be7bSPoul-Henning Kamp 
1193c884498fSJung-uk Kim /*
1194c884498fSJung-uk Kim  * Try to find out if we have a disk in the drive.
1195c884498fSJung-uk Kim  */
1196c884498fSJung-uk Kim static int
1197c884498fSJung-uk Kim fd_probe_disk(struct fd_data *fd, int *recal)
1198c884498fSJung-uk Kim {
1199c884498fSJung-uk Kim 	struct fdc_data *fdc;
1200c884498fSJung-uk Kim 	int st0, st3, cyl;
1201c884498fSJung-uk Kim 	int oopts, ret;
1202c884498fSJung-uk Kim 
1203c884498fSJung-uk Kim 	fdc = fd->fdc;
1204c884498fSJung-uk Kim 	oopts = fd->options;
1205c884498fSJung-uk Kim 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1206c884498fSJung-uk Kim 	ret = 1;
1207c884498fSJung-uk Kim 
1208c884498fSJung-uk Kim 	/*
1209c884498fSJung-uk Kim 	 * First recal, then seek to cyl#1, this clears the old condition on
1210c884498fSJung-uk Kim 	 * the disk change line so we can examine it for current status.
1211c884498fSJung-uk Kim 	 */
1212c884498fSJung-uk Kim 	if (debugflags & 0x40)
1213c884498fSJung-uk Kim 		printf("New disk in probe\n");
1214c884498fSJung-uk Kim 	mtx_lock(&fdc->fdc_mtx);
1215c884498fSJung-uk Kim 	fd->flags |= FD_NEWDISK;
1216c884498fSJung-uk Kim 	mtx_unlock(&fdc->fdc_mtx);
1217c884498fSJung-uk Kim 	if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
1218c884498fSJung-uk Kim 		goto done;
1219c884498fSJung-uk Kim 	tsleep(fdc, PRIBIO, "fdrecal", hz);
1220c884498fSJung-uk Kim 	if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1221c884498fSJung-uk Kim 		goto done;	/* XXX */
1222c884498fSJung-uk Kim 	if ((st0 & 0xc0) || cyl != 0)
1223c884498fSJung-uk Kim 		goto done;
1224c884498fSJung-uk Kim 
1225c884498fSJung-uk Kim 	/* Seek to track 1 */
1226c884498fSJung-uk Kim 	if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
1227c884498fSJung-uk Kim 		goto done;
1228c884498fSJung-uk Kim 	tsleep(fdc, PRIBIO, "fdseek", hz);
1229c884498fSJung-uk Kim 	if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1230c884498fSJung-uk Kim 		goto done;	/* XXX */
1231c884498fSJung-uk Kim 	*recal |= (1 << fd->fdsu);
1232c884498fSJung-uk Kim 	if (fdin_rd(fdc) & FDI_DCHG) {
1233c884498fSJung-uk Kim 		if (debugflags & 0x40)
1234c884498fSJung-uk Kim 			printf("Empty in probe\n");
1235c884498fSJung-uk Kim 		mtx_lock(&fdc->fdc_mtx);
1236c884498fSJung-uk Kim 		fd->flags |= FD_EMPTY;
1237c884498fSJung-uk Kim 		mtx_unlock(&fdc->fdc_mtx);
1238c884498fSJung-uk Kim 	} else {
1239c884498fSJung-uk Kim 		if (fdc_sense_drive(fdc, &st3) != 0)
1240c884498fSJung-uk Kim 			goto done;
1241c884498fSJung-uk Kim 		if (debugflags & 0x40)
1242c884498fSJung-uk Kim 			printf("Got disk in probe\n");
1243c884498fSJung-uk Kim 		mtx_lock(&fdc->fdc_mtx);
1244c884498fSJung-uk Kim 		fd->flags &= ~FD_EMPTY;
1245c884498fSJung-uk Kim 		if (st3 & NE7_ST3_WP)
1246c884498fSJung-uk Kim 			fd->flags |= FD_WP;
1247c884498fSJung-uk Kim 		else
1248c884498fSJung-uk Kim 			fd->flags &= ~FD_WP;
1249c884498fSJung-uk Kim 		mtx_unlock(&fdc->fdc_mtx);
1250c884498fSJung-uk Kim 	}
1251c884498fSJung-uk Kim 	ret = 0;
1252c884498fSJung-uk Kim 
1253c884498fSJung-uk Kim done:
1254c884498fSJung-uk Kim 	fd->options = oopts;
1255c884498fSJung-uk Kim 	return (ret);
1256c884498fSJung-uk Kim }
1257c884498fSJung-uk Kim 
12581b67be7bSPoul-Henning Kamp static int
12591b67be7bSPoul-Henning Kamp fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
12601b67be7bSPoul-Henning Kamp {
12611b67be7bSPoul-Henning Kamp 	struct bio *bp;
12621b67be7bSPoul-Henning Kamp 	struct fd_formb *finfo;
12631b67be7bSPoul-Henning Kamp 	struct fdc_readid *idfield;
12641b67be7bSPoul-Henning Kamp 	int error;
12651b67be7bSPoul-Henning Kamp 
12661b67be7bSPoul-Henning Kamp 	bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
12671b67be7bSPoul-Henning Kamp 
12681b67be7bSPoul-Henning Kamp 	/*
12691b67be7bSPoul-Henning Kamp 	 * Set up a bio request for fdstrategy().  bio_offset is faked
12701b67be7bSPoul-Henning Kamp 	 * so that fdstrategy() will seek to the the requested
12711b67be7bSPoul-Henning Kamp 	 * cylinder, and use the desired head.
12721b67be7bSPoul-Henning Kamp 	 */
12731b67be7bSPoul-Henning Kamp 	bp->bio_cmd = cmd;
12741b67be7bSPoul-Henning Kamp 	if (cmd == BIO_FMT) {
12751b67be7bSPoul-Henning Kamp 		finfo = (struct fd_formb *)data;
12761b67be7bSPoul-Henning Kamp 		bp->bio_pblkno =
12771b67be7bSPoul-Henning Kamp 		    (finfo->cyl * fd->ft->heads + finfo->head) *
12781b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac;
12791b67be7bSPoul-Henning Kamp 		bp->bio_length = sizeof *finfo;
12801b67be7bSPoul-Henning Kamp 	} else if (cmd == BIO_RDID) {
12811b67be7bSPoul-Henning Kamp 		idfield = (struct fdc_readid *)data;
12821b67be7bSPoul-Henning Kamp 		bp->bio_pblkno =
12831b67be7bSPoul-Henning Kamp 		    (idfield->cyl * fd->ft->heads + idfield->head) *
12841b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac;
12851b67be7bSPoul-Henning Kamp 		bp->bio_length = sizeof(struct fdc_readid);
12861b67be7bSPoul-Henning Kamp 	} else if (cmd == BIO_PROBE) {
12871b67be7bSPoul-Henning Kamp 		/* nothing */
12881b67be7bSPoul-Henning Kamp 	} else
12891b67be7bSPoul-Henning Kamp 		panic("wrong cmd in fdmisccmd()");
12901b67be7bSPoul-Henning Kamp 	bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
12911b67be7bSPoul-Henning Kamp 	bp->bio_data = data;
12921b67be7bSPoul-Henning Kamp 	bp->bio_driver1 = fd;
12931b67be7bSPoul-Henning Kamp 	bp->bio_flags = 0;
12941b67be7bSPoul-Henning Kamp 
12951b67be7bSPoul-Henning Kamp 	fd_enqueue(fd, bp);
12961b67be7bSPoul-Henning Kamp 
12971b67be7bSPoul-Henning Kamp 	do {
12986e50e38fSJohn Baldwin 		tsleep(bp, PRIBIO, "fdwait", hz);
12991b67be7bSPoul-Henning Kamp 	} while (!(bp->bio_flags & BIO_DONE));
13001b67be7bSPoul-Henning Kamp 	error = bp->bio_error;
13011b67be7bSPoul-Henning Kamp 
13021b67be7bSPoul-Henning Kamp 	free(bp, M_TEMP);
13031b67be7bSPoul-Henning Kamp 	return (error);
13041b67be7bSPoul-Henning Kamp }
13051b67be7bSPoul-Henning Kamp 
13061b67be7bSPoul-Henning Kamp /*
13071b67be7bSPoul-Henning Kamp  * Try figuring out the density of the media present in our device.
13081b67be7bSPoul-Henning Kamp  */
13091b67be7bSPoul-Henning Kamp static int
13101b67be7bSPoul-Henning Kamp fdautoselect(struct fd_data *fd)
13111b67be7bSPoul-Henning Kamp {
13121b67be7bSPoul-Henning Kamp 	struct fd_type *fdtp;
13131b67be7bSPoul-Henning Kamp 	struct fdc_readid id;
13141b67be7bSPoul-Henning Kamp 	int oopts, rv;
13151b67be7bSPoul-Henning Kamp 
13161b67be7bSPoul-Henning Kamp 	if (!(fd->ft->flags & FL_AUTO))
13171b67be7bSPoul-Henning Kamp 		return (0);
13181b67be7bSPoul-Henning Kamp 
13191b67be7bSPoul-Henning Kamp 	fdtp = fd_native_types[fd->type];
13201b67be7bSPoul-Henning Kamp 	fdsettype(fd, fdtp);
13211b67be7bSPoul-Henning Kamp 	if (!(fd->ft->flags & FL_AUTO))
13221b67be7bSPoul-Henning Kamp 		return (0);
13231b67be7bSPoul-Henning Kamp 
13241b67be7bSPoul-Henning Kamp 	/*
13251b67be7bSPoul-Henning Kamp 	 * Try reading sector ID fields, first at cylinder 0, head 0,
13261b67be7bSPoul-Henning Kamp 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
13271b67be7bSPoul-Henning Kamp 	 * since for 5.25in DD media in a HD drive, there are no data
13281b67be7bSPoul-Henning Kamp 	 * to read (2 step pulses per media cylinder required).  For
13291b67be7bSPoul-Henning Kamp 	 * two-sided media, the second probe always goes to head 1, so
13301b67be7bSPoul-Henning Kamp 	 * we can tell them apart from single-sided media.  As a
13311b67be7bSPoul-Henning Kamp 	 * side-effect this means that single-sided media should be
13321b67be7bSPoul-Henning Kamp 	 * mentioned in the search list after two-sided media of an
13331b67be7bSPoul-Henning Kamp 	 * otherwise identical density.  Media with a different number
13341b67be7bSPoul-Henning Kamp 	 * of sectors per track but otherwise identical parameters
13351b67be7bSPoul-Henning Kamp 	 * cannot be distinguished at all.
13361b67be7bSPoul-Henning Kamp 	 *
13371b67be7bSPoul-Henning Kamp 	 * If we successfully read an ID field on both cylinders where
13381b67be7bSPoul-Henning Kamp 	 * the recorded values match our expectation, we are done.
13391b67be7bSPoul-Henning Kamp 	 * Otherwise, we try the next density entry from the table.
13401b67be7bSPoul-Henning Kamp 	 *
13411b67be7bSPoul-Henning Kamp 	 * Stepping to cylinder 2 has the side-effect of clearing the
13421b67be7bSPoul-Henning Kamp 	 * unit attention bit.
13431b67be7bSPoul-Henning Kamp 	 */
13441b67be7bSPoul-Henning Kamp 	oopts = fd->options;
13451b67be7bSPoul-Henning Kamp 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
13461b67be7bSPoul-Henning Kamp 	for (; fdtp->heads; fdtp++) {
13471b67be7bSPoul-Henning Kamp 		fdsettype(fd, fdtp);
13481b67be7bSPoul-Henning Kamp 
13491b67be7bSPoul-Henning Kamp 		id.cyl = id.head = 0;
13501b67be7bSPoul-Henning Kamp 		rv = fdmisccmd(fd, BIO_RDID, &id);
13511b67be7bSPoul-Henning Kamp 		if (rv != 0)
13521b67be7bSPoul-Henning Kamp 			continue;
13531b67be7bSPoul-Henning Kamp 		if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
13541b67be7bSPoul-Henning Kamp 			continue;
13551b67be7bSPoul-Henning Kamp 		id.cyl = 2;
13561b67be7bSPoul-Henning Kamp 		id.head = fd->ft->heads - 1;
13571b67be7bSPoul-Henning Kamp 		rv = fdmisccmd(fd, BIO_RDID, &id);
13581b67be7bSPoul-Henning Kamp 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
13591b67be7bSPoul-Henning Kamp 		    id.secshift != fdtp->secsize)
13601b67be7bSPoul-Henning Kamp 			continue;
13611b67be7bSPoul-Henning Kamp 		if (rv == 0)
13621b67be7bSPoul-Henning Kamp 			break;
13631b67be7bSPoul-Henning Kamp 	}
13641b67be7bSPoul-Henning Kamp 
13651b67be7bSPoul-Henning Kamp 	fd->options = oopts;
13661b67be7bSPoul-Henning Kamp 	if (fdtp->heads == 0) {
13671b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
13681b67be7bSPoul-Henning Kamp 			device_printf(fd->dev, "autoselection failed\n");
13691b67be7bSPoul-Henning Kamp 		fdsettype(fd, fd_native_types[fd->type]);
1370c884498fSJung-uk Kim 		return (-1);
13711b67be7bSPoul-Henning Kamp 	} else {
13721b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40) {
13731b67be7bSPoul-Henning Kamp 			device_printf(fd->dev,
13741b67be7bSPoul-Henning Kamp 			    "autoselected %d KB medium\n", fd->ft->size / 2);
13751b67be7bSPoul-Henning Kamp 			fdprinttype(fd->ft);
13761b67be7bSPoul-Henning Kamp 		}
13771b67be7bSPoul-Henning Kamp 		return (0);
13781b67be7bSPoul-Henning Kamp 	}
13791b67be7bSPoul-Henning Kamp }
13801b67be7bSPoul-Henning Kamp 
13811b67be7bSPoul-Henning Kamp /*
13821b67be7bSPoul-Henning Kamp  * GEOM class implementation
13831b67be7bSPoul-Henning Kamp  */
13841b67be7bSPoul-Henning Kamp 
13851b67be7bSPoul-Henning Kamp static g_access_t	fd_access;
13861b67be7bSPoul-Henning Kamp static g_start_t	fd_start;
13871b67be7bSPoul-Henning Kamp static g_ioctl_t	fd_ioctl;
13881b67be7bSPoul-Henning Kamp 
13891b67be7bSPoul-Henning Kamp struct g_class g_fd_class = {
13901b67be7bSPoul-Henning Kamp 	.name =		"FD",
13911b67be7bSPoul-Henning Kamp 	.version =	G_VERSION,
13921b67be7bSPoul-Henning Kamp 	.start =	fd_start,
13931b67be7bSPoul-Henning Kamp 	.access =	fd_access,
13941b67be7bSPoul-Henning Kamp 	.ioctl =	fd_ioctl,
13951b67be7bSPoul-Henning Kamp };
13961b67be7bSPoul-Henning Kamp 
13971b67be7bSPoul-Henning Kamp static int
13981b67be7bSPoul-Henning Kamp fd_access(struct g_provider *pp, int r, int w, int e)
13991b67be7bSPoul-Henning Kamp {
14001b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
14011b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
14021b67be7bSPoul-Henning Kamp 	int ar, aw, ae;
1403fc9f8bcfSKonstantin Belousov 	int busy;
14041b67be7bSPoul-Henning Kamp 
14051b67be7bSPoul-Henning Kamp 	fd = pp->geom->softc;
14061b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
14071b67be7bSPoul-Henning Kamp 
14081b67be7bSPoul-Henning Kamp 	/*
14091b67be7bSPoul-Henning Kamp 	 * If our provider is withering, we can only get negative requests
14101b67be7bSPoul-Henning Kamp 	 * and we don't want to even see them
14111b67be7bSPoul-Henning Kamp 	 */
14121b67be7bSPoul-Henning Kamp 	if (pp->flags & G_PF_WITHER)
14131b67be7bSPoul-Henning Kamp 		return (0);
14141b67be7bSPoul-Henning Kamp 
14151b67be7bSPoul-Henning Kamp 	ar = r + pp->acr;
14161b67be7bSPoul-Henning Kamp 	aw = w + pp->acw;
14171b67be7bSPoul-Henning Kamp 	ae = e + pp->ace;
14181b67be7bSPoul-Henning Kamp 
14191b67be7bSPoul-Henning Kamp 	if (ar == 0 && aw == 0 && ae == 0) {
14201b67be7bSPoul-Henning Kamp 		device_unbusy(fd->dev);
14211b67be7bSPoul-Henning Kamp 		return (0);
14221b67be7bSPoul-Henning Kamp 	}
14231b67be7bSPoul-Henning Kamp 
1424fc9f8bcfSKonstantin Belousov 	busy = 0;
14251b67be7bSPoul-Henning Kamp 	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
14261b67be7bSPoul-Henning Kamp 		if (fdmisccmd(fd, BIO_PROBE, NULL))
14271b67be7bSPoul-Henning Kamp 			return (ENXIO);
14281b67be7bSPoul-Henning Kamp 		if (fd->flags & FD_EMPTY)
14291b67be7bSPoul-Henning Kamp 			return (ENXIO);
14301b67be7bSPoul-Henning Kamp 		if (fd->flags & FD_NEWDISK) {
1431c884498fSJung-uk Kim 			if (fdautoselect(fd) != 0 &&
1432c884498fSJung-uk Kim 			    (device_get_flags(fd->dev) & FD_NO_CHLINE)) {
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 				return (ENXIO);
1437c884498fSJung-uk Kim 			}
143861909e4cSPoul-Henning Kamp 			mtx_lock(&fdc->fdc_mtx);
14391b67be7bSPoul-Henning Kamp 			fd->flags &= ~FD_NEWDISK;
144061909e4cSPoul-Henning Kamp 			mtx_unlock(&fdc->fdc_mtx);
14411b67be7bSPoul-Henning Kamp 		}
14421b67be7bSPoul-Henning Kamp 		device_busy(fd->dev);
1443fc9f8bcfSKonstantin Belousov 		busy = 1;
14441b67be7bSPoul-Henning Kamp 	}
14451b67be7bSPoul-Henning Kamp 
1446fc9f8bcfSKonstantin Belousov 	if (w > 0 && (fd->flags & FD_WP)) {
1447fc9f8bcfSKonstantin Belousov 		if (busy)
1448fc9f8bcfSKonstantin Belousov 			device_unbusy(fd->dev);
14491b67be7bSPoul-Henning Kamp 		return (EROFS);
1450fc9f8bcfSKonstantin Belousov 	}
14511b67be7bSPoul-Henning Kamp 
14521b67be7bSPoul-Henning Kamp 	pp->sectorsize = fd->sectorsize;
14531b67be7bSPoul-Henning Kamp 	pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
14541b67be7bSPoul-Henning Kamp 	pp->mediasize = pp->stripesize * fd->ft->tracks;
14551b67be7bSPoul-Henning Kamp 	return (0);
14561b67be7bSPoul-Henning Kamp }
14571b67be7bSPoul-Henning Kamp 
14581b67be7bSPoul-Henning Kamp static void
14591b67be7bSPoul-Henning Kamp fd_start(struct bio *bp)
14601b67be7bSPoul-Henning Kamp {
14611b67be7bSPoul-Henning Kamp  	struct fdc_data *	fdc;
14621b67be7bSPoul-Henning Kamp  	struct fd_data *	fd;
14631b67be7bSPoul-Henning Kamp 
14641b67be7bSPoul-Henning Kamp 	fd = bp->bio_to->geom->softc;
14651b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
14661b67be7bSPoul-Henning Kamp 	bp->bio_driver1 = fd;
14671b67be7bSPoul-Henning Kamp 	if (bp->bio_cmd & BIO_GETATTR) {
14681b67be7bSPoul-Henning Kamp 		if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
14691b67be7bSPoul-Henning Kamp 			return;
14701b67be7bSPoul-Henning Kamp 		if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
14711b67be7bSPoul-Henning Kamp 			return;
14721b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, ENOIOCTL);
14731b67be7bSPoul-Henning Kamp 		return;
14741b67be7bSPoul-Henning Kamp 	}
14751b67be7bSPoul-Henning Kamp 	if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
14761b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, EOPNOTSUPP);
14771b67be7bSPoul-Henning Kamp 		return;
14781b67be7bSPoul-Henning Kamp 	}
14791b67be7bSPoul-Henning Kamp 	bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
14801b67be7bSPoul-Henning Kamp 	bp->bio_resid = bp->bio_length;
14811b67be7bSPoul-Henning Kamp 	fd_enqueue(fd, bp);
14821b67be7bSPoul-Henning Kamp 	return;
14831b67be7bSPoul-Henning Kamp }
14841b67be7bSPoul-Henning Kamp 
14851b67be7bSPoul-Henning Kamp static int
14862221dbebSPoul-Henning Kamp fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
14871b67be7bSPoul-Henning Kamp {
14881b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
14891b67be7bSPoul-Henning Kamp 	struct fdc_status *fsp;
14901b67be7bSPoul-Henning Kamp 	struct fdc_readid *rid;
14911b67be7bSPoul-Henning Kamp 	int error;
14921b67be7bSPoul-Henning Kamp 
14931b67be7bSPoul-Henning Kamp 	fd = pp->geom->softc;
14941b67be7bSPoul-Henning Kamp 
14951b67be7bSPoul-Henning Kamp 	switch (cmd) {
14961b67be7bSPoul-Henning Kamp 	case FD_GTYPE:                  /* get drive type */
14971b67be7bSPoul-Henning Kamp 		*(struct fd_type *)data = *fd->ft;
14981b67be7bSPoul-Henning Kamp 		return (0);
14991b67be7bSPoul-Henning Kamp 
15001b67be7bSPoul-Henning Kamp 	case FD_STYPE:                  /* set drive type */
150162da7a2dSPoul-Henning Kamp 		if (!(fflag & FWRITE))
15022221dbebSPoul-Henning Kamp 			return (EPERM);
15031b67be7bSPoul-Henning Kamp 		/*
15041b67be7bSPoul-Henning Kamp 		 * Allow setting drive type temporarily iff
15051b67be7bSPoul-Henning Kamp 		 * currently unset.  Used for fdformat so any
15061b67be7bSPoul-Henning Kamp 		 * user can set it, and then start formatting.
15071b67be7bSPoul-Henning Kamp 		 */
15081b67be7bSPoul-Henning Kamp 		fd->fts = *(struct fd_type *)data;
15091b67be7bSPoul-Henning Kamp 		if (fd->fts.sectrac) {
15101b67be7bSPoul-Henning Kamp 			/* XXX: check for rubbish */
15111b67be7bSPoul-Henning Kamp 			fdsettype(fd, &fd->fts);
15121b67be7bSPoul-Henning Kamp 		} else {
15131b67be7bSPoul-Henning Kamp 			fdsettype(fd, fd_native_types[fd->type]);
15141b67be7bSPoul-Henning Kamp 		}
15151b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
15161b67be7bSPoul-Henning Kamp 			fdprinttype(fd->ft);
15171b67be7bSPoul-Henning Kamp 		return (0);
15181b67be7bSPoul-Henning Kamp 
15191b67be7bSPoul-Henning Kamp 	case FD_GOPTS:			/* get drive options */
15201b67be7bSPoul-Henning Kamp 		*(int *)data = fd->options;
15211b67be7bSPoul-Henning Kamp 		return (0);
15221b67be7bSPoul-Henning Kamp 
15231b67be7bSPoul-Henning Kamp 	case FD_SOPTS:			/* set drive options */
152462da7a2dSPoul-Henning Kamp 		if (!(fflag & FWRITE))
15252221dbebSPoul-Henning Kamp 			return (EPERM);
15261b67be7bSPoul-Henning Kamp 		fd->options = *(int *)data;
15271b67be7bSPoul-Henning Kamp 		return (0);
15281b67be7bSPoul-Henning Kamp 
15291b67be7bSPoul-Henning Kamp 	case FD_CLRERR:
1530acd3428bSRobert Watson 		error = priv_check(td, PRIV_DRIVER);
1531acd3428bSRobert Watson 		if (error)
1532acd3428bSRobert Watson 			return (error);
15331b67be7bSPoul-Henning Kamp 		fd->fdc->fdc_errs = 0;
15341b67be7bSPoul-Henning Kamp 		return (0);
15351b67be7bSPoul-Henning Kamp 
15361b67be7bSPoul-Henning Kamp 	case FD_GSTAT:
15371b67be7bSPoul-Henning Kamp 		fsp = (struct fdc_status *)data;
15381b67be7bSPoul-Henning Kamp 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
15391b67be7bSPoul-Henning Kamp 			return (EINVAL);
15401b67be7bSPoul-Henning Kamp 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
15411b67be7bSPoul-Henning Kamp 		return (0);
15421b67be7bSPoul-Henning Kamp 
15431b67be7bSPoul-Henning Kamp 	case FD_GDTYPE:
15441b67be7bSPoul-Henning Kamp 		*(enum fd_drivetype *)data = fd->type;
15451b67be7bSPoul-Henning Kamp 		return (0);
15461b67be7bSPoul-Henning Kamp 
15471b67be7bSPoul-Henning Kamp 	case FD_FORM:
154862da7a2dSPoul-Henning Kamp 		if (!(fflag & FWRITE))
15492221dbebSPoul-Henning Kamp 			return (EPERM);
15501b67be7bSPoul-Henning Kamp 		if (((struct fd_formb *)data)->format_version !=
15511b67be7bSPoul-Henning Kamp 		    FD_FORMAT_VERSION)
15521b67be7bSPoul-Henning Kamp 			return (EINVAL); /* wrong version of formatting prog */
15531b67be7bSPoul-Henning Kamp 		error = fdmisccmd(fd, BIO_FMT, data);
155461909e4cSPoul-Henning Kamp 		mtx_lock(&fd->fdc->fdc_mtx);
15551b67be7bSPoul-Henning Kamp 		fd->flags |= FD_NEWDISK;
155661909e4cSPoul-Henning Kamp 		mtx_unlock(&fd->fdc->fdc_mtx);
15571b67be7bSPoul-Henning Kamp 		break;
15581b67be7bSPoul-Henning Kamp 
15591b67be7bSPoul-Henning Kamp 	case FD_READID:
15601b67be7bSPoul-Henning Kamp 		rid = (struct fdc_readid *)data;
15611b67be7bSPoul-Henning Kamp 		if (rid->cyl > 85 || rid->head > 1)
15621b67be7bSPoul-Henning Kamp 			return (EINVAL);
15631b67be7bSPoul-Henning Kamp 		error = fdmisccmd(fd, BIO_RDID, data);
15641b67be7bSPoul-Henning Kamp 		break;
15651b67be7bSPoul-Henning Kamp 
15661b67be7bSPoul-Henning Kamp 	case FIONBIO:
15671b67be7bSPoul-Henning Kamp 	case FIOASYNC:
15681b67be7bSPoul-Henning Kamp 		/* For backwards compat with old fd*(8) tools */
15691b67be7bSPoul-Henning Kamp 		error = 0;
15701b67be7bSPoul-Henning Kamp 		break;
15711b67be7bSPoul-Henning Kamp 
15721b67be7bSPoul-Henning Kamp 	default:
15731b67be7bSPoul-Henning Kamp 		if (debugflags & 0x80)
15741b67be7bSPoul-Henning Kamp 			printf("Unknown ioctl %lx\n", cmd);
15751b67be7bSPoul-Henning Kamp 		error = ENOIOCTL;
15761b67be7bSPoul-Henning Kamp 		break;
15771b67be7bSPoul-Henning Kamp 	}
15781b67be7bSPoul-Henning Kamp 	return (error);
15791b67be7bSPoul-Henning Kamp };
15801b67be7bSPoul-Henning Kamp 
15811b67be7bSPoul-Henning Kamp 
15821b67be7bSPoul-Henning Kamp 
15831b67be7bSPoul-Henning Kamp /*
15841b67be7bSPoul-Henning Kamp  * Configuration/initialization stuff, per controller.
15851b67be7bSPoul-Henning Kamp  */
15861b67be7bSPoul-Henning Kamp 
15871b67be7bSPoul-Henning Kamp devclass_t fdc_devclass;
15881b67be7bSPoul-Henning Kamp static devclass_t fd_devclass;
15891b67be7bSPoul-Henning Kamp 
15901b67be7bSPoul-Henning Kamp struct fdc_ivars {
15911b67be7bSPoul-Henning Kamp 	int	fdunit;
15921b67be7bSPoul-Henning Kamp 	int	fdtype;
15931b67be7bSPoul-Henning Kamp };
15941b67be7bSPoul-Henning Kamp 
15956d6fa4fdSWarner Losh void
159637286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
159737286586SPeter Wemm {
159837286586SPeter Wemm 	device_t dev;
1599973bfe6cSWarner Losh 	struct resource *last;
1600973bfe6cSWarner Losh 	int i;
160137286586SPeter Wemm 
160237286586SPeter Wemm 	dev = fdc->fdc_dev;
1603b4046cd7SWarner Losh 	if (fdc->fdc_intr)
16047964bd0cSWarner Losh 		bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1605a6e4d8c4SNate Lawson 	fdc->fdc_intr = NULL;
1606b4046cd7SWarner Losh 	if (fdc->res_irq != NULL)
160737286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
160837286586SPeter Wemm 		    fdc->res_irq);
1609a6e4d8c4SNate Lawson 	fdc->res_irq = NULL;
1610973bfe6cSWarner Losh 	last = NULL;
1611973bfe6cSWarner Losh 	for (i = 0; i < FDC_MAXREG; i++) {
1612973bfe6cSWarner Losh 		if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1613973bfe6cSWarner Losh 			bus_release_resource(dev, SYS_RES_IOPORT,
1614973bfe6cSWarner Losh 			    fdc->ridio[i], fdc->resio[i]);
1615973bfe6cSWarner Losh 			last = fdc->resio[i];
1616973bfe6cSWarner Losh 			fdc->resio[i] = NULL;
1617973bfe6cSWarner Losh 		}
1618973bfe6cSWarner Losh 	}
1619b4046cd7SWarner Losh 	if (fdc->res_drq != NULL)
162037286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
162137286586SPeter Wemm 		    fdc->res_drq);
16225ceae6b8SYoshihiro Takahashi 	fdc->res_drq = NULL;
162337286586SPeter Wemm }
162437286586SPeter Wemm 
16256d6fa4fdSWarner Losh int
16263aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
162737286586SPeter Wemm {
162837286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
162937286586SPeter Wemm 
163037286586SPeter Wemm 	switch (which) {
163137286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
163237286586SPeter Wemm 		*result = ivars->fdunit;
163337286586SPeter Wemm 		break;
1634752d4735SNate Lawson 	case FDC_IVAR_FDTYPE:
1635752d4735SNate Lawson 		*result = ivars->fdtype;
1636752d4735SNate Lawson 		break;
163737286586SPeter Wemm 	default:
1638752d4735SNate Lawson 		return (ENOENT);
163937286586SPeter Wemm 	}
1640752d4735SNate Lawson 	return (0);
1641752d4735SNate Lawson }
1642752d4735SNate Lawson 
1643752d4735SNate Lawson int
1644752d4735SNate Lawson fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1645752d4735SNate Lawson {
1646752d4735SNate Lawson 	struct fdc_ivars *ivars = device_get_ivars(child);
1647752d4735SNate Lawson 
1648752d4735SNate Lawson 	switch (which) {
1649752d4735SNate Lawson 	case FDC_IVAR_FDUNIT:
1650752d4735SNate Lawson 		ivars->fdunit = value;
1651752d4735SNate Lawson 		break;
1652752d4735SNate Lawson 	case FDC_IVAR_FDTYPE:
1653752d4735SNate Lawson 		ivars->fdtype = value;
1654752d4735SNate Lawson 		break;
1655752d4735SNate Lawson 	default:
1656752d4735SNate Lawson 		return (ENOENT);
1657752d4735SNate Lawson 	}
1658752d4735SNate Lawson 	return (0);
1659752d4735SNate Lawson }
1660752d4735SNate Lawson 
1661752d4735SNate Lawson int
16621b67be7bSPoul-Henning Kamp fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1663752d4735SNate Lawson {
16641b67be7bSPoul-Henning Kamp 	int ic_type, part_id;
16651b67be7bSPoul-Henning Kamp 
1666ec200f23SPoul-Henning Kamp 	/*
1667ec200f23SPoul-Henning Kamp 	 * A status value of 0xff is very unlikely, but not theoretically
1668ec200f23SPoul-Henning Kamp 	 * impossible, but it is far more likely to indicate an empty bus.
1669ec200f23SPoul-Henning Kamp 	 */
1670ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) == 0xff)
1671ec200f23SPoul-Henning Kamp 		return (ENXIO);
1672ec200f23SPoul-Henning Kamp 
1673ec200f23SPoul-Henning Kamp 	/*
1674ec200f23SPoul-Henning Kamp 	 * Assert a reset to the floppy controller and check that the status
1675ec200f23SPoul-Henning Kamp 	 * register goes to zero.
1676ec200f23SPoul-Henning Kamp 	 */
1677752d4735SNate Lawson 	fdout_wr(fdc, 0);
1678ec200f23SPoul-Henning Kamp 	fdout_wr(fdc, 0);
1679ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) != 0)
1680ec200f23SPoul-Henning Kamp 		return (ENXIO);
1681ec200f23SPoul-Henning Kamp 
1682ec200f23SPoul-Henning Kamp 	/*
1683ec200f23SPoul-Henning Kamp 	 * Clear the reset and see it come ready.
1684ec200f23SPoul-Henning Kamp 	 */
1685752d4735SNate Lawson 	fdout_wr(fdc, FDO_FRST);
1686ec200f23SPoul-Henning Kamp 	DELAY(100);
1687ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) != 0x80)
1688ec200f23SPoul-Henning Kamp 		return (ENXIO);
1689752d4735SNate Lawson 
1690752d4735SNate Lawson 	/* Then, see if it can handle a command. */
16911b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0))
1692752d4735SNate Lawson 		return (ENXIO);
16931b67be7bSPoul-Henning Kamp 
16941b67be7bSPoul-Henning Kamp 	/*
16951b67be7bSPoul-Henning Kamp 	 * Try to identify the chip.
16961b67be7bSPoul-Henning Kamp 	 *
16971b67be7bSPoul-Henning Kamp 	 * The i8272 datasheet documents that unknown commands
16981b67be7bSPoul-Henning Kamp 	 * will return ST0 as 0x80.  The i8272 is supposedly identical
16991b67be7bSPoul-Henning Kamp 	 * to the NEC765.
17001b67be7bSPoul-Henning Kamp 	 * The i82077SL datasheet says 0x90 for the VERSION command,
17011b67be7bSPoul-Henning Kamp 	 * and several "superio" chips emulate this.
17021b67be7bSPoul-Henning Kamp 	 */
17031b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
17041b67be7bSPoul-Henning Kamp 		return (ENXIO);
17051b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
17061b67be7bSPoul-Henning Kamp 		return (ENXIO);
17071d4b1bb7SPoul-Henning Kamp 	if (bootverbose)
17081d4b1bb7SPoul-Henning Kamp 		device_printf(dev,
17091d4b1bb7SPoul-Henning Kamp 		    "ic_type %02x part_id %02x\n", ic_type, part_id);
17101b67be7bSPoul-Henning Kamp 	switch (ic_type & 0xff) {
17111b67be7bSPoul-Henning Kamp 	case 0x80:
17121b67be7bSPoul-Henning Kamp 		device_set_desc(dev, "NEC 765 or clone");
17131b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_NE765;
17141b67be7bSPoul-Henning Kamp 		break;
17151b67be7bSPoul-Henning Kamp 	case 0x81:
17161b67be7bSPoul-Henning Kamp 	case 0x90:
17171b67be7bSPoul-Henning Kamp 		device_set_desc(dev,
17181b67be7bSPoul-Henning Kamp 		    "Enhanced floppy controller");
17191b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_ENHANCED;
17201b67be7bSPoul-Henning Kamp 		break;
17211b67be7bSPoul-Henning Kamp 	default:
17221b67be7bSPoul-Henning Kamp 		device_set_desc(dev, "Generic floppy controller");
17231b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_UNKNOWN;
17241b67be7bSPoul-Henning Kamp 		break;
17251b67be7bSPoul-Henning Kamp 	}
1726752d4735SNate Lawson 	return (0);
172737286586SPeter Wemm }
172837286586SPeter Wemm 
17296d6fa4fdSWarner Losh int
1730e219897aSJoerg Wunsch fdc_detach(device_t dev)
17315f830ea2SJoerg Wunsch {
17325f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
17335f830ea2SJoerg Wunsch 	int	error;
17345f830ea2SJoerg Wunsch 
17355f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
17365f830ea2SJoerg Wunsch 
17375f830ea2SJoerg Wunsch 	/* have our children detached first */
17385f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
17395f830ea2SJoerg Wunsch 		return (error);
17405f830ea2SJoerg Wunsch 
174102910eeeSJoerg Wunsch 	/* kill worker thread */
174202910eeeSJoerg Wunsch 	mtx_lock(&fdc->fdc_mtx);
17435f6fcd82SJohn Baldwin 	fdc->flags |= FDC_KTHREAD_EXIT;
1744f8353edeSPoul-Henning Kamp 	wakeup(&fdc->head);
174502910eeeSJoerg Wunsch 	while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
174689a828fdSKonstantin Belousov 		msleep(fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
174702910eeeSJoerg Wunsch 	mtx_unlock(&fdc->fdc_mtx);
174802910eeeSJoerg Wunsch 
1749e219897aSJoerg Wunsch 	/* reset controller, turn motor off */
1750e219897aSJoerg Wunsch 	fdout_wr(fdc, 0);
1751e219897aSJoerg Wunsch 
17527964bd0cSWarner Losh 	if (!(fdc->flags & FDC_NODMA))
17537964bd0cSWarner Losh 		isa_dma_release(fdc->dmachan);
17545f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
17551b67be7bSPoul-Henning Kamp 	mtx_destroy(&fdc->fdc_mtx);
17565f830ea2SJoerg Wunsch 	return (0);
17575f830ea2SJoerg Wunsch }
17585f830ea2SJoerg Wunsch 
17595b81b6b3SRodney W. Grimes /*
176037286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
17615b81b6b3SRodney W. Grimes  */
1762a6e4d8c4SNate Lawson device_t
176337286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
17645b81b6b3SRodney W. Grimes {
176537286586SPeter Wemm 	struct fdc_ivars *ivar;
17666182fdbdSPeter Wemm 	device_t child;
176792200632SGarrett Wollman 
17687cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
176937286586SPeter Wemm 	if (ivar == NULL)
1770a6e4d8c4SNate Lawson 		return (NULL);
1771fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
177242117b6cSPoul-Henning Kamp 	if (child == NULL) {
177342117b6cSPoul-Henning Kamp 		free(ivar, M_DEVBUF);
1774a6e4d8c4SNate Lawson 		return (NULL);
177542117b6cSPoul-Henning Kamp 	}
177637286586SPeter Wemm 	device_set_ivars(child, ivar);
1777a6e4d8c4SNate Lawson 	ivar->fdunit = unit;
1778a6e4d8c4SNate Lawson 	ivar->fdtype = FDT_NONE;
17798a9bc9c0SJohn Baldwin 	if (resource_disabled(name, unit))
17806182fdbdSPeter Wemm 		device_disable(child);
1781a6e4d8c4SNate Lawson 	return (child);
17826182fdbdSPeter Wemm }
17836182fdbdSPeter Wemm 
17846d6fa4fdSWarner Losh int
17856182fdbdSPeter Wemm fdc_attach(device_t dev)
17866182fdbdSPeter Wemm {
178737286586SPeter Wemm 	struct	fdc_data *fdc;
1788a6e4d8c4SNate Lawson 	int	error;
1789427ccf00SDoug Rabson 
179037286586SPeter Wemm 	fdc = device_get_softc(dev);
1791b0c2b925SWarner Losh 	fdc->fdc_dev = dev;
17921b67be7bSPoul-Henning Kamp 	error = fdc_initial_reset(dev, fdc);
17931b67be7bSPoul-Henning Kamp 	if (error) {
17941b67be7bSPoul-Henning Kamp 		device_printf(dev, "does not respond\n");
17951b67be7bSPoul-Henning Kamp 		return (error);
17961b67be7bSPoul-Henning Kamp 	}
1797b593dd82SWarner Losh 	error = bus_setup_intr(dev, fdc->res_irq,
1798ef544f63SPaolo Pisati 	    INTR_TYPE_BIO | INTR_ENTROPY |
1799ef544f63SPaolo Pisati 	    ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
1800ef544f63SPaolo Pisati             ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
1801ef544f63SPaolo Pisati 	    ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
1802ef544f63SPaolo Pisati 			       fdc, &fdc->fdc_intr);
180337286586SPeter Wemm 	if (error) {
180437286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
18051b67be7bSPoul-Henning Kamp 		return (error);
180637286586SPeter Wemm 	}
18077964bd0cSWarner Losh 	if (!(fdc->flags & FDC_NODMA)) {
18087964bd0cSWarner Losh 		error = isa_dma_acquire(fdc->dmachan);
18091ef0fd82SPoul-Henning Kamp 		if (!error) {
18101ef0fd82SPoul-Henning Kamp 			error = isa_dma_init(fdc->dmachan,
18111ef0fd82SPoul-Henning Kamp 			    MAX_BYTES_PER_CYL, M_WAITOK);
18121ef0fd82SPoul-Henning Kamp 			if (error)
18131ef0fd82SPoul-Henning Kamp 				isa_dma_release(fdc->dmachan);
18141ef0fd82SPoul-Henning Kamp 		}
18157964bd0cSWarner Losh 		if (error)
18167964bd0cSWarner Losh 			return (error);
18177964bd0cSWarner Losh 	}
181837286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
1819a6e4d8c4SNate Lawson 	fdc->flags |= FDC_NEEDS_RESET;
18206182fdbdSPeter Wemm 
18211b67be7bSPoul-Henning Kamp 	mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
18226182fdbdSPeter Wemm 
18233a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
182464860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
18258177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
18265b81b6b3SRodney W. Grimes 
18273745c395SJulian Elischer 	kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
18281b67be7bSPoul-Henning Kamp 	    "fdc%d", device_get_unit(dev));
18291b67be7bSPoul-Henning Kamp 
18301b67be7bSPoul-Henning Kamp 	settle = hz / 8;
18311b67be7bSPoul-Henning Kamp 
1832a6e4d8c4SNate Lawson 	return (0);
1833a6e4d8c4SNate Lawson }
1834a6e4d8c4SNate Lawson 
1835a6e4d8c4SNate Lawson int
1836a6e4d8c4SNate Lawson fdc_hints_probe(device_t dev)
1837a6e4d8c4SNate Lawson {
1838a6e4d8c4SNate Lawson 	const char *name, *dname;
1839a6e4d8c4SNate Lawson 	int i, error, dunit;
1840a6e4d8c4SNate Lawson 
18416182fdbdSPeter Wemm 	/*
184237286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
184337286586SPeter Wemm 	 * devices from the BIOS unless overridden.
18446182fdbdSPeter Wemm 	 */
1845ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
18462398f0cdSPeter Wemm 	i = 0;
1847a6e4d8c4SNate Lawson 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1848a6e4d8c4SNate Lawson 		resource_int_value(dname, dunit, "drive", &dunit);
18492398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
1850a6e4d8c4SNate Lawson 	}
185137286586SPeter Wemm 
185260444853SJoerg Wunsch 	if ((error = bus_generic_attach(dev)) != 0)
185360444853SJoerg Wunsch 		return (error);
185460444853SJoerg Wunsch 	return (0);
18556182fdbdSPeter Wemm }
18566182fdbdSPeter Wemm 
18576d6fa4fdSWarner Losh int
18586182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
18596182fdbdSPeter Wemm {
18601a6bed68SJoerg Wunsch 	int retval = 0, flags;
186115317dd8SMatthew N. Dodd 
186215317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
18631a6bed68SJoerg Wunsch 	retval += printf(" on %s drive %d", device_get_nameunit(me),
186437286586SPeter Wemm 	       fdc_get_fdunit(child));
18651a6bed68SJoerg Wunsch 	if ((flags = device_get_flags(me)) != 0)
18661a6bed68SJoerg Wunsch 		retval += printf(" flags %#x", flags);
18671a6bed68SJoerg Wunsch 	retval += printf("\n");
186815317dd8SMatthew N. Dodd 
186915317dd8SMatthew N. Dodd 	return (retval);
18706182fdbdSPeter Wemm }
18716182fdbdSPeter Wemm 
187216e68fc6SPeter Wemm /*
1873246ed35dSJoerg Wunsch  * Configuration/initialization, per drive.
187416e68fc6SPeter Wemm  */
18756182fdbdSPeter Wemm static int
18766182fdbdSPeter Wemm fd_probe(device_t dev)
18776182fdbdSPeter Wemm {
18781b67be7bSPoul-Henning Kamp 	int	i, unit;
18791a6bed68SJoerg Wunsch 	u_int	st0, st3;
18806182fdbdSPeter Wemm 	struct	fd_data *fd;
18816182fdbdSPeter Wemm 	struct	fdc_data *fdc;
18821b67be7bSPoul-Henning Kamp 	int	fdsu;
1883752d4735SNate Lawson 	int	flags, type;
18846182fdbdSPeter Wemm 
1885752d4735SNate Lawson 	fdsu = fdc_get_fdunit(dev);
18866182fdbdSPeter Wemm 	fd = device_get_softc(dev);
18876182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
18881a6bed68SJoerg Wunsch 	flags = device_get_flags(dev);
18896182fdbdSPeter Wemm 
18906182fdbdSPeter Wemm 	fd->dev = dev;
18916182fdbdSPeter Wemm 	fd->fdc = fdc;
18926182fdbdSPeter Wemm 	fd->fdsu = fdsu;
18931b67be7bSPoul-Henning Kamp 	unit = device_get_unit(dev);
18946182fdbdSPeter Wemm 
1895752d4735SNate Lawson 	/* Auto-probe if fdinfo is present, but always allow override. */
18961b67be7bSPoul-Henning Kamp 	type = flags & FD_TYPEMASK;
1897752d4735SNate Lawson 	if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1898752d4735SNate Lawson 		fd->type = type;
1899752d4735SNate Lawson 		goto done;
1900752d4735SNate Lawson 	} else {
1901752d4735SNate Lawson 		/* make sure fdautoselect() will be called */
19021b67be7bSPoul-Henning Kamp 		fd->flags = FD_EMPTY;
1903752d4735SNate Lawson 		fd->type = type;
1904752d4735SNate Lawson 	}
1905752d4735SNate Lawson 
190627df52f9SWarner Losh #if (defined(__i386__) && !defined(PC98)) || defined(__amd64__)
19071b67be7bSPoul-Henning Kamp 	if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
19081a6bed68SJoerg Wunsch 		/* Look up what the BIOS thinks we have. */
190927df52f9SWarner Losh 		if (unit == 0)
19101a6bed68SJoerg Wunsch 			fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
191127df52f9SWarner Losh 		else
19121a6bed68SJoerg Wunsch 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
19131a6bed68SJoerg Wunsch 		if (fd->type == FDT_288M_1)
19141a6bed68SJoerg Wunsch 			fd->type = FDT_288M;
19151a6bed68SJoerg Wunsch 	}
191604209354SPeter Wemm #endif /* __i386__ || __amd64__ */
19176182fdbdSPeter Wemm 	/* is there a unit? */
19181a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE)
19196182fdbdSPeter Wemm 		return (ENXIO);
19206182fdbdSPeter Wemm 
19211b67be7bSPoul-Henning Kamp /*
19221b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
19231b67be7bSPoul-Henning Kamp */
19246182fdbdSPeter Wemm 	/* select it */
19251b67be7bSPoul-Henning Kamp 	fd_select(fd);
19261b67be7bSPoul-Henning Kamp 	fd_motor(fd, 1);
19271b67be7bSPoul-Henning Kamp 	fdc->fd = fd;
1928fb35bd37SJoerg Wunsch 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
19296182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
19306182fdbdSPeter Wemm 
19311a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0) {
1932dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
19331b67be7bSPoul-Henning Kamp 		if ((fdc_sense_drive(fdc, &st3) == 0) &&
19341a6bed68SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
1935dc5df763SJoerg Wunsch 			/* Seek some steps... */
19361b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1937dc5df763SJoerg Wunsch 				/* ...wait a moment... */
1938dc5df763SJoerg Wunsch 				DELAY(300000);
1939dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
1940a7e943a4SPyun YongHyeon 				fdc_sense_int(fdc, NULL, NULL);
1941dc5df763SJoerg Wunsch 			}
1942dc5df763SJoerg Wunsch 		}
1943dc5df763SJoerg Wunsch 
19446b7bd95bSJoerg Wunsch 		for (i = 0; i < 2; i++) {
19456b7bd95bSJoerg Wunsch 			/*
19466b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
19471a6bed68SJoerg Wunsch 			 * heads have been beyond cylinder 76, since
19481a6bed68SJoerg Wunsch 			 * most FDCs still barf when attempting to
19491a6bed68SJoerg Wunsch 			 * recalibrate more than 77 steps
19506b7bd95bSJoerg Wunsch 			 */
1951dc5df763SJoerg Wunsch 			/* go back to 0: */
19521b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
19536b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
19546b7bd95bSJoerg Wunsch 				DELAY(i == 0 ? 1000000 : 300000);
19555b81b6b3SRodney W. Grimes 
19566b7bd95bSJoerg Wunsch 				/* anything responding? */
1957a7e943a4SPyun YongHyeon 				if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
1958dc5df763SJoerg Wunsch 				    (st0 & NE7_ST0_EC) == 0)
19596b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
19606b7bd95bSJoerg Wunsch 			}
1961dc5df763SJoerg Wunsch 		}
19621a6bed68SJoerg Wunsch 	}
19636b7bd95bSJoerg Wunsch 
19641b67be7bSPoul-Henning Kamp 	fd_motor(fd, 0);
19651b67be7bSPoul-Henning Kamp 	fdc->fd = NULL;
19661b67be7bSPoul-Henning Kamp /*
19671b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
19681b67be7bSPoul-Henning Kamp */
19693a2f7427SDavid Greenman 
19701a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0 &&
19711a6bed68SJoerg Wunsch 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
19726182fdbdSPeter Wemm 		return (ENXIO);
19735b81b6b3SRodney W. Grimes 
1974752d4735SNate Lawson done:
1975752d4735SNate Lawson 
19761a6bed68SJoerg Wunsch 	switch (fd->type) {
19771a6bed68SJoerg Wunsch 	case FDT_12M:
19781a6bed68SJoerg Wunsch 		device_set_desc(dev, "1200-KB 5.25\" drive");
19791a6bed68SJoerg Wunsch 		break;
19801a6bed68SJoerg Wunsch 	case FDT_144M:
19811a6bed68SJoerg Wunsch 		device_set_desc(dev, "1440-KB 3.5\" drive");
19821a6bed68SJoerg Wunsch 		break;
19831a6bed68SJoerg Wunsch 	case FDT_288M:
19841a6bed68SJoerg Wunsch 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
19851a6bed68SJoerg Wunsch 		break;
19861a6bed68SJoerg Wunsch 	case FDT_360K:
19871a6bed68SJoerg Wunsch 		device_set_desc(dev, "360-KB 5.25\" drive");
19881a6bed68SJoerg Wunsch 		break;
19891a6bed68SJoerg Wunsch 	case FDT_720K:
19901a6bed68SJoerg Wunsch 		device_set_desc(dev, "720-KB 3.5\" drive");
19911a6bed68SJoerg Wunsch 		break;
19921a6bed68SJoerg Wunsch 	default:
19931a6bed68SJoerg Wunsch 		return (ENXIO);
19941a6bed68SJoerg Wunsch 	}
1995dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1996b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1997b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
19983a2f7427SDavid Greenman 	fd->options = 0;
19999fc9d84bSJohn Baldwin 	callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
20005b81b6b3SRodney W. Grimes 
20011a6bed68SJoerg Wunsch 	/* initialize densities for subdevices */
20021b67be7bSPoul-Henning Kamp 	fdsettype(fd, fd_native_types[fd->type]);
20036182fdbdSPeter Wemm 	return (0);
20046182fdbdSPeter Wemm }
20056182fdbdSPeter Wemm 
20061b67be7bSPoul-Henning Kamp /*
20071b67be7bSPoul-Henning Kamp  * We have to do this in a geom event because GEOM is not running
20081b67be7bSPoul-Henning Kamp  * when fd_attach() is.
20091b67be7bSPoul-Henning Kamp  * XXX: move fd_attach after geom like ata/scsi disks
20101b67be7bSPoul-Henning Kamp  */
20111b67be7bSPoul-Henning Kamp static void
20121b67be7bSPoul-Henning Kamp fd_attach2(void *arg, int flag)
20131b67be7bSPoul-Henning Kamp {
20141b67be7bSPoul-Henning Kamp 	struct	fd_data *fd;
20151b67be7bSPoul-Henning Kamp 
20161b67be7bSPoul-Henning Kamp 	fd = arg;
20171b67be7bSPoul-Henning Kamp 
20181b67be7bSPoul-Henning Kamp 	fd->fd_geom = g_new_geomf(&g_fd_class,
20193ba0c6b1SPoul-Henning Kamp 	    "fd%d", device_get_unit(fd->dev));
20201b67be7bSPoul-Henning Kamp 	fd->fd_provider = g_new_providerf(fd->fd_geom, fd->fd_geom->name);
20211b67be7bSPoul-Henning Kamp 	fd->fd_geom->softc = fd;
20221b67be7bSPoul-Henning Kamp 	g_error_provider(fd->fd_provider, 0);
20231b67be7bSPoul-Henning Kamp }
20241b67be7bSPoul-Henning Kamp 
20256182fdbdSPeter Wemm static int
20266182fdbdSPeter Wemm fd_attach(device_t dev)
20276182fdbdSPeter Wemm {
20286182fdbdSPeter Wemm 	struct	fd_data *fd;
20296182fdbdSPeter Wemm 
203064860614SJoerg Wunsch 	fd = device_get_softc(dev);
20311b67be7bSPoul-Henning Kamp 	g_post_event(fd_attach2, fd, M_WAITOK, NULL);
20321b67be7bSPoul-Henning Kamp 	fd->flags |= FD_EMPTY;
20331b67be7bSPoul-Henning Kamp 	bioq_init(&fd->fd_bq);
20341b67be7bSPoul-Henning Kamp 
20356182fdbdSPeter Wemm 	return (0);
20365b81b6b3SRodney W. Grimes }
20375b81b6b3SRodney W. Grimes 
20385f830ea2SJoerg Wunsch static int
20395f830ea2SJoerg Wunsch fd_detach(device_t dev)
20405f830ea2SJoerg Wunsch {
20415f830ea2SJoerg Wunsch 	struct	fd_data *fd;
20425f830ea2SJoerg Wunsch 
20435f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
20444b87c653SWarner Losh 	g_topology_lock();
20454b87c653SWarner Losh 	g_wither_geom(fd->fd_geom, ENXIO);
20464b87c653SWarner Losh 	g_topology_unlock();
20474b87c653SWarner Losh 	while (device_get_state(dev) == DS_BUSY)
20484b87c653SWarner Losh 		tsleep(fd, PZERO, "fdd", hz/10);
20491b67be7bSPoul-Henning Kamp 	callout_drain(&fd->toffhandle);
20505f830ea2SJoerg Wunsch 
20515f830ea2SJoerg Wunsch 	return (0);
20525f830ea2SJoerg Wunsch }
20535f830ea2SJoerg Wunsch 
205416e68fc6SPeter Wemm static device_method_t fd_methods[] = {
205516e68fc6SPeter Wemm 	/* Device interface */
205616e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
205716e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
20585f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
205916e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
206016e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
206116e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
206216e68fc6SPeter Wemm 	{ 0, 0 }
206316e68fc6SPeter Wemm };
206416e68fc6SPeter Wemm 
206516e68fc6SPeter Wemm static driver_t fd_driver = {
206616e68fc6SPeter Wemm 	"fd",
206716e68fc6SPeter Wemm 	fd_methods,
206816e68fc6SPeter Wemm 	sizeof(struct fd_data)
206916e68fc6SPeter Wemm };
207016e68fc6SPeter Wemm 
2071f8353edeSPoul-Henning Kamp static int
2072f8353edeSPoul-Henning Kamp fdc_modevent(module_t mod, int type, void *data)
2073f8353edeSPoul-Henning Kamp {
2074f8353edeSPoul-Henning Kamp 
2075f8353edeSPoul-Henning Kamp 	g_modevent(NULL, type, &g_fd_class);
2076f8353edeSPoul-Henning Kamp 	return (0);
2077f8353edeSPoul-Henning Kamp }
2078f8353edeSPoul-Henning Kamp 
2079f8353edeSPoul-Henning Kamp DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);
2080