xref: /freebsd/sys/dev/fdc/fdc.c (revision 3745c395ecae17ef47be82433463d561629220b0)
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 */
1001b67be7bSPoul-Henning Kamp #define FD_NO_PROBE	0x20	/* don't probe drive (seek test), just
1011b67be7bSPoul-Henning Kamp 				 * assume it is there */
1021b67be7bSPoul-Henning Kamp 
1031b67be7bSPoul-Henning Kamp /*
1041b67be7bSPoul-Henning Kamp  * Things that could conceiveably considered parameters or tweakables
1051b67be7bSPoul-Henning Kamp  */
1061b67be7bSPoul-Henning Kamp 
1071b67be7bSPoul-Henning Kamp /*
1081b67be7bSPoul-Henning Kamp  * Maximal number of bytes in a cylinder.
1091b67be7bSPoul-Henning Kamp  * This is used for ISADMA bouncebuffer allocation and sets the max
1101b67be7bSPoul-Henning Kamp  * xfersize we support.
1111b67be7bSPoul-Henning Kamp  *
11261909e4cSPoul-Henning Kamp  * 2.88M format has 2 x 36 x 512, allow for hacked up density.
1131b67be7bSPoul-Henning Kamp  */
1141b67be7bSPoul-Henning Kamp #define MAX_BYTES_PER_CYL	(2 * 40 * 512)
115dc5df763SJoerg Wunsch 
1161a6bed68SJoerg Wunsch /*
117cb38bb6cSJoerg Wunsch  * Timeout value for the PIO loops to wait until the FDC main status
118cb38bb6cSJoerg Wunsch  * register matches our expectations (request for master, direction
119cb38bb6cSJoerg Wunsch  * bit).  This is supposed to be a number of microseconds, although
120cb38bb6cSJoerg Wunsch  * timing might actually not be very accurate.
121cb38bb6cSJoerg Wunsch  *
122cb38bb6cSJoerg Wunsch  * Timeouts of 100 msec are believed to be required for some broken
123cb38bb6cSJoerg Wunsch  * (old) hardware.
124cb38bb6cSJoerg Wunsch  */
125cb38bb6cSJoerg Wunsch #define	FDSTS_TIMEOUT	100000
126cb38bb6cSJoerg Wunsch 
127cb38bb6cSJoerg Wunsch /*
1281b67be7bSPoul-Henning Kamp  * After this many errors, stop whining.  Close will reset this count.
1291a6bed68SJoerg Wunsch  */
13061909e4cSPoul-Henning Kamp #define FDC_ERRMAX	100
1317ca0641bSAndrey A. Chernov 
1321a6bed68SJoerg Wunsch /*
1331b67be7bSPoul-Henning Kamp  * AutoDensity search lists for each drive type.
1341a6bed68SJoerg Wunsch  */
1351b67be7bSPoul-Henning Kamp 
1361b67be7bSPoul-Henning Kamp static struct fd_type fd_searchlist_360k[] = {
1371b67be7bSPoul-Henning Kamp 	{ FDF_5_360 },
1381b67be7bSPoul-Henning Kamp 	{ 0 }
1395b81b6b3SRodney W. Grimes };
1405b81b6b3SRodney W. Grimes 
1411a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_12m[] = {
1421b67be7bSPoul-Henning Kamp 	{ FDF_5_1200 | FL_AUTO },
1431b67be7bSPoul-Henning Kamp 	{ FDF_5_360 | FL_2STEP | FL_AUTO},
1441b67be7bSPoul-Henning Kamp 	{ 0 }
1451b67be7bSPoul-Henning Kamp };
1461b67be7bSPoul-Henning Kamp 
1471b67be7bSPoul-Henning Kamp static struct fd_type fd_searchlist_720k[] = {
1481b67be7bSPoul-Henning Kamp 	{ FDF_3_720 },
1491b67be7bSPoul-Henning Kamp 	{ 0 }
1501a6bed68SJoerg Wunsch };
1511a6bed68SJoerg Wunsch 
1521a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_144m[] = {
1531b67be7bSPoul-Henning Kamp 	{ FDF_3_1440 | FL_AUTO},
1541b67be7bSPoul-Henning Kamp 	{ FDF_3_720 | FL_AUTO},
1551b67be7bSPoul-Henning Kamp 	{ 0 }
1561a6bed68SJoerg Wunsch };
1571a6bed68SJoerg Wunsch 
1581a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_288m[] = {
1591b67be7bSPoul-Henning Kamp 	{ FDF_3_1440 | FL_AUTO },
1601a6bed68SJoerg Wunsch #if 0
1611b67be7bSPoul-Henning Kamp 	{ FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
1621a6bed68SJoerg Wunsch #endif
1631b67be7bSPoul-Henning Kamp 	{ FDF_3_720 | FL_AUTO},
1641b67be7bSPoul-Henning Kamp 	{ 0 }
1651a6bed68SJoerg Wunsch };
166dc16046fSJoerg Wunsch 
1671b67be7bSPoul-Henning Kamp /*
1681b67be7bSPoul-Henning Kamp  * Order must match enum fd_drivetype in <sys/fdcio.h>.
1691b67be7bSPoul-Henning Kamp  */
1701b67be7bSPoul-Henning Kamp static struct fd_type *fd_native_types[] = {
1711b67be7bSPoul-Henning Kamp 	NULL,				/* FDT_NONE */
1721b67be7bSPoul-Henning Kamp 	fd_searchlist_360k, 		/* FDT_360K */
1731b67be7bSPoul-Henning Kamp 	fd_searchlist_12m, 		/* FDT_12M */
1741b67be7bSPoul-Henning Kamp 	fd_searchlist_720k, 		/* FDT_720K */
1751b67be7bSPoul-Henning Kamp 	fd_searchlist_144m, 		/* FDT_144M */
1768fe457d6SPeter Edwards 	fd_searchlist_288m,		/* FDT_288M_1 (mapped to FDT_288M) */
1771b67be7bSPoul-Henning Kamp 	fd_searchlist_288m, 		/* FDT_288M */
1781b67be7bSPoul-Henning Kamp };
179f8ce7dd5SJoerg Wunsch 
1801b67be7bSPoul-Henning Kamp /*
1811b67be7bSPoul-Henning Kamp  * Internals start here
1821b67be7bSPoul-Henning Kamp  */
1831b67be7bSPoul-Henning Kamp 
1841b67be7bSPoul-Henning Kamp /* registers */
1851b67be7bSPoul-Henning Kamp #define	FDOUT	2	/* Digital Output Register (W) */
1861b67be7bSPoul-Henning Kamp #define	FDO_FDSEL	0x03	/*  floppy device select */
1871b67be7bSPoul-Henning Kamp #define	FDO_FRST	0x04	/*  floppy controller reset */
1881b67be7bSPoul-Henning Kamp #define	FDO_FDMAEN	0x08	/*  enable floppy DMA and Interrupt */
1891b67be7bSPoul-Henning Kamp #define	FDO_MOEN0	0x10	/*  motor enable drive 0 */
1901b67be7bSPoul-Henning Kamp #define	FDO_MOEN1	0x20	/*  motor enable drive 1 */
1911b67be7bSPoul-Henning Kamp #define	FDO_MOEN2	0x40	/*  motor enable drive 2 */
1921b67be7bSPoul-Henning Kamp #define	FDO_MOEN3	0x80	/*  motor enable drive 3 */
1931b67be7bSPoul-Henning Kamp 
1941b67be7bSPoul-Henning Kamp #define	FDSTS	4	/* NEC 765 Main Status Register (R) */
19550d99d1aSJung-uk Kim #define FDDSR	4	/* Data Rate Select Register (W) */
1961b67be7bSPoul-Henning Kamp #define	FDDATA	5	/* NEC 765 Data Register (R/W) */
1971b67be7bSPoul-Henning Kamp #define	FDCTL	7	/* Control Register (W) */
1981b67be7bSPoul-Henning Kamp 
1991b67be7bSPoul-Henning Kamp /*
200973bfe6cSWarner Losh  * The YE-DATA PC Card floppies use PIO to read in the data rather
201973bfe6cSWarner Losh  * than DMA due to the wild variability of DMA for the PC Card
202973bfe6cSWarner Losh  * devices.  DMA was deleted from the PC Card specification in version
203973bfe6cSWarner Losh  * 7.2 of the standard, but that post-dates the YE-DATA devices by many
204973bfe6cSWarner Losh  * years.
205973bfe6cSWarner Losh  *
206973bfe6cSWarner Losh  * In addition, if we cannot setup the DMA resources for the ISA
207973bfe6cSWarner Losh  * attachment, we'll use this same offset for data transfer.  However,
208973bfe6cSWarner Losh  * that almost certainly won't work.
209b4046cd7SWarner Losh  *
210b4046cd7SWarner Losh  * For this mode, offset 0 and 1 must be used to setup the transfer
211973bfe6cSWarner Losh  * for this floppy.  This is OK for PC Card YE Data devices, but for
212973bfe6cSWarner Losh  * ISA this is likely wrong.  These registers are only available on
213973bfe6cSWarner Losh  * those systems that map them to the floppy drive.  Newer systems do
214973bfe6cSWarner Losh  * not do this, and we should likely prohibit access to them (or
215973bfe6cSWarner Losh  * disallow NODMA to be set).
2161b67be7bSPoul-Henning Kamp  */
217b4046cd7SWarner Losh #define FDBCDR		0	/* And 1 */
218b4046cd7SWarner Losh #define FD_YE_DATAPORT	6	/* Drive Data port */
2191b67be7bSPoul-Henning Kamp 
2201b67be7bSPoul-Henning Kamp #define	FDI_DCHG	0x80	/* diskette has been changed */
2211b67be7bSPoul-Henning Kamp 				/* requires drive and motor being selected */
2221b67be7bSPoul-Henning Kamp 				/* is cleared by any step pulse to drive */
2231b67be7bSPoul-Henning Kamp 
2241b67be7bSPoul-Henning Kamp /*
22561909e4cSPoul-Henning Kamp  * We have three private BIO commands.
2261b67be7bSPoul-Henning Kamp  */
2271b67be7bSPoul-Henning Kamp #define BIO_PROBE	BIO_CMD0
2281b67be7bSPoul-Henning Kamp #define BIO_RDID	BIO_CMD1
2291b67be7bSPoul-Henning Kamp #define BIO_FMT		BIO_CMD2
2305b81b6b3SRodney W. Grimes 
231246ed35dSJoerg Wunsch /*
232246ed35dSJoerg Wunsch  * Per drive structure (softc).
233246ed35dSJoerg Wunsch  */
2346182fdbdSPeter Wemm struct fd_data {
2351b67be7bSPoul-Henning Kamp 	u_char 	*fd_ioptr;	/* IO pointer */
2361b67be7bSPoul-Henning Kamp 	u_int	fd_iosize;	/* Size of IO chunks */
2371b67be7bSPoul-Henning Kamp 	u_int	fd_iocount;	/* Outstanding requests */
238b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2395b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2401a6bed68SJoerg Wunsch 	enum	fd_drivetype type; /* drive type */
2411a6bed68SJoerg Wunsch 	struct	fd_type *ft;	/* pointer to current type descriptor */
2421b67be7bSPoul-Henning Kamp 	struct	fd_type fts;	/* type descriptors */
2431b67be7bSPoul-Henning Kamp 	int	sectorsize;
2445b81b6b3SRodney W. Grimes 	int	flags;
2451b67be7bSPoul-Henning Kamp #define	FD_WP		(1<<0)	/* Write protected	*/
2461b67be7bSPoul-Henning Kamp #define	FD_MOTOR	(1<<1)	/* motor should be on	*/
2471b67be7bSPoul-Henning Kamp #define	FD_MOTORWAIT	(1<<2)	/* motor should be on	*/
2481b67be7bSPoul-Henning Kamp #define	FD_EMPTY	(1<<3)	/* no media		*/
2491b67be7bSPoul-Henning Kamp #define	FD_NEWDISK	(1<<4)	/* media changed	*/
2501b67be7bSPoul-Henning Kamp #define	FD_ISADMA	(1<<5)	/* isa dma started 	*/
2515b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
2521b67be7bSPoul-Henning Kamp #define FD_NO_TRACK	 -2
2531b67be7bSPoul-Henning Kamp 	int	options;	/* FDOPT_* */
2541b67be7bSPoul-Henning Kamp 	struct	callout toffhandle;
2551b67be7bSPoul-Henning Kamp 	struct g_geom *fd_geom;
2561b67be7bSPoul-Henning Kamp 	struct g_provider *fd_provider;
2576182fdbdSPeter Wemm 	device_t dev;
2581b67be7bSPoul-Henning Kamp 	struct bio_queue_head fd_bq;
2596182fdbdSPeter Wemm };
26037286586SPeter Wemm 
2611b67be7bSPoul-Henning Kamp #define FD_NOT_VALID -2
262752d4735SNate Lawson 
263246ed35dSJoerg Wunsch static driver_intr_t fdc_intr;
264ef544f63SPaolo Pisati static driver_filter_t fdc_intr_fast;
2651b67be7bSPoul-Henning Kamp static void fdc_reset(struct fdc_data *);
266d66c374fSTor Egge 
2671b67be7bSPoul-Henning Kamp SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
268d66c374fSTor Egge 
2691b67be7bSPoul-Henning Kamp static int fifo_threshold = 8;
2701b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
2711b67be7bSPoul-Henning Kamp 	"FIFO threshold setting");
2721b67be7bSPoul-Henning Kamp 
2731b67be7bSPoul-Henning Kamp static int debugflags = 0;
2741b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
2751b67be7bSPoul-Henning Kamp 	"Debug flags");
2761b67be7bSPoul-Henning Kamp 
2771b67be7bSPoul-Henning Kamp static int retries = 10;
2781b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
2791b67be7bSPoul-Henning Kamp 	"Number of retries to attempt");
2801b67be7bSPoul-Henning Kamp 
2811b67be7bSPoul-Henning Kamp static int spec1 = 0xaf;
2821b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
2831b67be7bSPoul-Henning Kamp 	"Specification byte one (step-rate + head unload)");
2841b67be7bSPoul-Henning Kamp 
2851b67be7bSPoul-Henning Kamp static int spec2 = 0x10;
2861b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
2871b67be7bSPoul-Henning Kamp 	"Specification byte two (head load time + no-dma)");
2881b67be7bSPoul-Henning Kamp 
2891b67be7bSPoul-Henning Kamp static int settle;
2901b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
2911b67be7bSPoul-Henning Kamp 	"Head settling time in sec/hz");
2921b67be7bSPoul-Henning Kamp 
2931b67be7bSPoul-Henning Kamp static void
2941b67be7bSPoul-Henning Kamp fdprinttype(struct fd_type *ft)
2951b67be7bSPoul-Henning Kamp {
2961b67be7bSPoul-Henning Kamp 
2971b67be7bSPoul-Henning Kamp 	printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
2981b67be7bSPoul-Henning Kamp 	    ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
2991b67be7bSPoul-Henning Kamp 	    ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
3001b67be7bSPoul-Henning Kamp 	    ft->offset_side2, ft->flags);
3011b67be7bSPoul-Henning Kamp }
3021b67be7bSPoul-Henning Kamp 
3031b67be7bSPoul-Henning Kamp static void
3041b67be7bSPoul-Henning Kamp fdsettype(struct fd_data *fd, struct fd_type *ft)
3051b67be7bSPoul-Henning Kamp {
3061b67be7bSPoul-Henning Kamp 	fd->ft = ft;
3071b67be7bSPoul-Henning Kamp 	ft->size = ft->sectrac * ft->heads * ft->tracks;
3081b67be7bSPoul-Henning Kamp 	fd->sectorsize = 128 << fd->ft->secsize;
3091b67be7bSPoul-Henning Kamp }
3105b81b6b3SRodney W. Grimes 
311246ed35dSJoerg Wunsch /*
312246ed35dSJoerg Wunsch  * Bus space handling (access to low-level IO).
313246ed35dSJoerg Wunsch  */
314973bfe6cSWarner Losh __inline static void
315973bfe6cSWarner Losh fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
316973bfe6cSWarner Losh {
317973bfe6cSWarner Losh 
318973bfe6cSWarner Losh 	bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
319973bfe6cSWarner Losh }
320973bfe6cSWarner Losh 
321973bfe6cSWarner Losh __inline static uint8_t
322973bfe6cSWarner Losh fdregrd(struct fdc_data *fdc, int reg)
323973bfe6cSWarner Losh {
324973bfe6cSWarner Losh 
325973bfe6cSWarner Losh 	return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
326973bfe6cSWarner Losh }
327973bfe6cSWarner Losh 
3281b67be7bSPoul-Henning Kamp static void
3291b67be7bSPoul-Henning Kamp fdctl_wr(struct fdc_data *fdc, u_int8_t v)
330427ccf00SDoug Rabson {
3311b67be7bSPoul-Henning Kamp 
332973bfe6cSWarner Losh 	fdregwr(fdc, FDCTL, v);
3331b67be7bSPoul-Henning Kamp }
3341b67be7bSPoul-Henning Kamp 
3351b67be7bSPoul-Henning Kamp static void
3361b67be7bSPoul-Henning Kamp fdout_wr(struct fdc_data *fdc, u_int8_t v)
3371b67be7bSPoul-Henning Kamp {
3381b67be7bSPoul-Henning Kamp 
339973bfe6cSWarner Losh 	fdregwr(fdc, FDOUT, v);
340427ccf00SDoug Rabson }
341427ccf00SDoug Rabson 
342427ccf00SDoug Rabson static u_int8_t
3431b67be7bSPoul-Henning Kamp fdsts_rd(struct fdc_data *fdc)
344427ccf00SDoug Rabson {
3451b67be7bSPoul-Henning Kamp 
346973bfe6cSWarner Losh 	return fdregrd(fdc, FDSTS);
347427ccf00SDoug Rabson }
348427ccf00SDoug Rabson 
349427ccf00SDoug Rabson static void
35050d99d1aSJung-uk Kim fddsr_wr(struct fdc_data *fdc, u_int8_t v)
35150d99d1aSJung-uk Kim {
35250d99d1aSJung-uk Kim 
35350d99d1aSJung-uk Kim 	fdregwr(fdc, FDDSR, v);
35450d99d1aSJung-uk Kim }
35550d99d1aSJung-uk Kim 
35650d99d1aSJung-uk Kim static void
3571b67be7bSPoul-Henning Kamp fddata_wr(struct fdc_data *fdc, u_int8_t v)
358427ccf00SDoug Rabson {
3591b67be7bSPoul-Henning Kamp 
360973bfe6cSWarner Losh 	fdregwr(fdc, FDDATA, v);
361427ccf00SDoug Rabson }
362427ccf00SDoug Rabson 
363427ccf00SDoug Rabson static u_int8_t
3641b67be7bSPoul-Henning Kamp fddata_rd(struct fdc_data *fdc)
365427ccf00SDoug Rabson {
3661b67be7bSPoul-Henning Kamp 
367973bfe6cSWarner Losh 	return fdregrd(fdc, FDDATA);
368427ccf00SDoug Rabson }
369427ccf00SDoug Rabson 
370427ccf00SDoug Rabson static u_int8_t
3711b67be7bSPoul-Henning Kamp fdin_rd(struct fdc_data *fdc)
372427ccf00SDoug Rabson {
3731b67be7bSPoul-Henning Kamp 
374973bfe6cSWarner Losh 	return fdregrd(fdc, FDCTL);
375427ccf00SDoug Rabson }
376427ccf00SDoug Rabson 
377b4046cd7SWarner Losh /*
378b4046cd7SWarner Losh  * Magic pseudo-DMA initialization for YE FDC. Sets count and
379b4046cd7SWarner Losh  * direction.
380b4046cd7SWarner Losh  */
381b4046cd7SWarner Losh static void
382b4046cd7SWarner Losh fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
383b4046cd7SWarner Losh {
384973bfe6cSWarner Losh 	fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
385973bfe6cSWarner Losh 	fdregwr(fdc, FDBCDR + 1,
386973bfe6cSWarner Losh 	    (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
387b4046cd7SWarner Losh }
388b4046cd7SWarner Losh 
389dc5df763SJoerg Wunsch static int
3906182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
391dc5df763SJoerg Wunsch {
3926182fdbdSPeter Wemm 	fdc->fdc_errs++;
39316b04b6aSJoerg Wunsch 	if (s) {
394b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
395b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
396b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
397b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
398b6e5f28eSPeter Wemm 						    "logging any more\n");
39916b04b6aSJoerg Wunsch 	}
400dc5df763SJoerg Wunsch 
4011b67be7bSPoul-Henning Kamp 	return (1);
402dc5df763SJoerg Wunsch }
403dc5df763SJoerg Wunsch 
404dc5df763SJoerg Wunsch /*
4051b67be7bSPoul-Henning Kamp  * FDC IO functions, take care of the main status register, timeout
4061b67be7bSPoul-Henning Kamp  * in case the desired status bits are never set.
4071b67be7bSPoul-Henning Kamp  *
4081b67be7bSPoul-Henning Kamp  * These PIO loops initially start out with short delays between
4091b67be7bSPoul-Henning Kamp  * each iteration in the expectation that the required condition
4101b67be7bSPoul-Henning Kamp  * is usually met quickly, so it can be handled immediately.
411dc5df763SJoerg Wunsch  */
4121b67be7bSPoul-Henning Kamp static int
4131b67be7bSPoul-Henning Kamp fdc_in(struct fdc_data *fdc, int *ptr)
414dc5df763SJoerg Wunsch {
4151b67be7bSPoul-Henning Kamp 	int i, j, step;
4161b67be7bSPoul-Henning Kamp 
4171b67be7bSPoul-Henning Kamp 	step = 1;
4181b67be7bSPoul-Henning Kamp 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
4191b67be7bSPoul-Henning Kamp 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
4201b67be7bSPoul-Henning Kamp 	        if (i == (NE7_DIO|NE7_RQM)) {
4211b67be7bSPoul-Henning Kamp 			i = fddata_rd(fdc);
4221b67be7bSPoul-Henning Kamp 			if (ptr)
4231b67be7bSPoul-Henning Kamp 				*ptr = i;
4241b67be7bSPoul-Henning Kamp 			return (0);
4251b67be7bSPoul-Henning Kamp 		}
4261b67be7bSPoul-Henning Kamp 		if (i == NE7_RQM)
4271b67be7bSPoul-Henning Kamp 			return (fdc_err(fdc, "ready for output in input\n"));
4281b67be7bSPoul-Henning Kamp 		step += step;
4291b67be7bSPoul-Henning Kamp 		DELAY(step);
4301b67be7bSPoul-Henning Kamp 	}
4311b67be7bSPoul-Henning Kamp 	return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
4321b67be7bSPoul-Henning Kamp }
4331b67be7bSPoul-Henning Kamp 
4341b67be7bSPoul-Henning Kamp static int
4351b67be7bSPoul-Henning Kamp fdc_out(struct fdc_data *fdc, int x)
4361b67be7bSPoul-Henning Kamp {
4371b67be7bSPoul-Henning Kamp 	int i, j, step;
4381b67be7bSPoul-Henning Kamp 
4391b67be7bSPoul-Henning Kamp 	step = 1;
4401b67be7bSPoul-Henning Kamp 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
4411b67be7bSPoul-Henning Kamp 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
4421b67be7bSPoul-Henning Kamp 	        if (i == NE7_RQM) {
4431b67be7bSPoul-Henning Kamp 			fddata_wr(fdc, x);
4441b67be7bSPoul-Henning Kamp 			return (0);
4451b67be7bSPoul-Henning Kamp 		}
4461b67be7bSPoul-Henning Kamp 		if (i == (NE7_DIO|NE7_RQM))
4471b67be7bSPoul-Henning Kamp 			return (fdc_err(fdc, "ready for input in output\n"));
4481b67be7bSPoul-Henning Kamp 		step += step;
4491b67be7bSPoul-Henning Kamp 		DELAY(step);
4501b67be7bSPoul-Henning Kamp 	}
4511b67be7bSPoul-Henning Kamp 	return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
4521b67be7bSPoul-Henning Kamp }
4531b67be7bSPoul-Henning Kamp 
4541b67be7bSPoul-Henning Kamp /*
4551b67be7bSPoul-Henning Kamp  * fdc_cmd: Send a command to the chip.
4561b67be7bSPoul-Henning Kamp  * Takes a varargs with this structure:
4571b67be7bSPoul-Henning Kamp  *	# of output bytes
4581b67be7bSPoul-Henning Kamp  *	output bytes as int [...]
4591b67be7bSPoul-Henning Kamp  *	# of input bytes
4601b67be7bSPoul-Henning Kamp  *	input bytes as int* [...]
4611b67be7bSPoul-Henning Kamp  */
4621b67be7bSPoul-Henning Kamp static int
4631b67be7bSPoul-Henning Kamp fdc_cmd(struct fdc_data *fdc, int n_out, ...)
4641b67be7bSPoul-Henning Kamp {
4651b67be7bSPoul-Henning Kamp 	u_char cmd = 0;
466dc5df763SJoerg Wunsch 	int n_in;
4671b67be7bSPoul-Henning Kamp 	int n, i;
468dc5df763SJoerg Wunsch 	va_list ap;
469dc5df763SJoerg Wunsch 
470dc5df763SJoerg Wunsch 	va_start(ap, n_out);
4711b67be7bSPoul-Henning Kamp 	for (n = 0; n < n_out; n++) {
4721b67be7bSPoul-Henning Kamp 		i = va_arg(ap, int);
4731b67be7bSPoul-Henning Kamp 		if (n == 0)
4741b67be7bSPoul-Henning Kamp 			cmd = i;
4751b67be7bSPoul-Henning Kamp 		if (fdc_out(fdc, i) < 0) {
476dc5df763SJoerg Wunsch 			char msg[50];
4772127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
478dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
479dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
4801b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
481a7e943a4SPyun YongHyeon 			va_end(ap);
4826182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
483dc5df763SJoerg Wunsch 		}
484dc5df763SJoerg Wunsch 	}
485dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
4861b67be7bSPoul-Henning Kamp 	for (n = 0; n < n_in; n++) {
487dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
4881b67be7bSPoul-Henning Kamp 		if (fdc_in(fdc, ptr) < 0) {
489dc5df763SJoerg Wunsch 			char msg[50];
4902127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
491dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
492dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
4931b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
494a7e943a4SPyun YongHyeon 			va_end(ap);
4956182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
496dc5df763SJoerg Wunsch 		}
497dc5df763SJoerg Wunsch 	}
498a7e943a4SPyun YongHyeon 	va_end(ap);
4991b67be7bSPoul-Henning Kamp 	return (0);
500dc5df763SJoerg Wunsch }
501dc5df763SJoerg Wunsch 
5021b67be7bSPoul-Henning Kamp static void
5031b67be7bSPoul-Henning Kamp fdc_reset(struct fdc_data *fdc)
504d66c374fSTor Egge {
5051b67be7bSPoul-Henning Kamp 	int i, r[10];
506d66c374fSTor Egge 
50750d99d1aSJung-uk Kim 	if (fdc->fdct == FDC_ENHANCED) {
50850d99d1aSJung-uk Kim 		/* Try a software reset, default precomp, and 500 kb/s */
50950d99d1aSJung-uk Kim 		fddsr_wr(fdc, I8207X_DSR_SR);
51050d99d1aSJung-uk Kim 	} else {
51150d99d1aSJung-uk Kim 		/* Try a hardware reset, keep motor on */
5121b67be7bSPoul-Henning Kamp 		fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
5131b67be7bSPoul-Henning Kamp 		DELAY(100);
5141b67be7bSPoul-Henning Kamp 		/* enable FDC, but defer interrupts a moment */
5151b67be7bSPoul-Henning Kamp 		fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
51619a388e6SJung-uk Kim 	}
5171b67be7bSPoul-Henning Kamp 	DELAY(100);
5181b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
519d66c374fSTor Egge 
5201b67be7bSPoul-Henning Kamp 	/* XXX after a reset, silently believe the FDC will accept commands */
5211b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
5221b67be7bSPoul-Henning Kamp 		device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
523d66c374fSTor Egge 
5241b67be7bSPoul-Henning Kamp 	if (fdc->fdct == FDC_ENHANCED) {
5251b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 4,
5260da90eb8SJung-uk Kim 		    I8207X_CONFIG,
5271b67be7bSPoul-Henning Kamp 		    0,
5281b67be7bSPoul-Henning Kamp 		    0x40 |			/* Enable Implied Seek */
5291b67be7bSPoul-Henning Kamp 		    0x10 |			/* Polling disabled */
5301b67be7bSPoul-Henning Kamp 		    (fifo_threshold - 1),	/* Fifo threshold */
5311b67be7bSPoul-Henning Kamp 		    0x00,			/* Precomp track */
5321b67be7bSPoul-Henning Kamp 		    0))
5331b67be7bSPoul-Henning Kamp 			device_printf(fdc->fdc_dev,
5341b67be7bSPoul-Henning Kamp 			    " CONFIGURE failed in reset\n");
5351b67be7bSPoul-Henning Kamp 		if (debugflags & 1) {
5361b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 1,
5370da90eb8SJung-uk Kim 			    I8207X_DUMPREG,
5381b67be7bSPoul-Henning Kamp 			    10, &r[0], &r[1], &r[2], &r[3], &r[4],
5391b67be7bSPoul-Henning Kamp 			    &r[5], &r[6], &r[7], &r[8], &r[9]))
5401b67be7bSPoul-Henning Kamp 				device_printf(fdc->fdc_dev,
5411b67be7bSPoul-Henning Kamp 				    " DUMPREG failed in reset\n");
5421b67be7bSPoul-Henning Kamp 			for (i = 0; i < 10; i++)
5431b67be7bSPoul-Henning Kamp 				printf(" %02x", r[i]);
5441b67be7bSPoul-Henning Kamp 			printf("\n");
545d66c374fSTor Egge 		}
546d0900d6bSJoerg Wunsch 	}
547d66c374fSTor Egge }
548d66c374fSTor Egge 
549d66c374fSTor Egge static int
5501b67be7bSPoul-Henning Kamp fdc_sense_drive(struct fdc_data *fdc, int *st3p)
551dc5df763SJoerg Wunsch {
552dc5df763SJoerg Wunsch 	int st3;
553dc5df763SJoerg Wunsch 
5541b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
5551b67be7bSPoul-Henning Kamp 		return (fdc_err(fdc, "Sense Drive Status failed\n"));
556dc5df763SJoerg Wunsch 	if (st3p)
557dc5df763SJoerg Wunsch 		*st3p = st3;
5581b67be7bSPoul-Henning Kamp 	return (0);
559dc5df763SJoerg Wunsch }
560dc5df763SJoerg Wunsch 
5616f4e0bebSPoul-Henning Kamp static int
5621b67be7bSPoul-Henning Kamp fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
563dc5df763SJoerg Wunsch {
5646182fdbdSPeter Wemm 	int cyl, st0, ret;
565dc5df763SJoerg Wunsch 
5661b67be7bSPoul-Henning Kamp 	ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
5676182fdbdSPeter Wemm 	if (ret) {
5681b67be7bSPoul-Henning Kamp 		(void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
5691b67be7bSPoul-Henning Kamp 		return (ret);
570dc5df763SJoerg Wunsch 	}
571dc5df763SJoerg Wunsch 
572dc5df763SJoerg Wunsch 	if (st0p)
573dc5df763SJoerg Wunsch 		*st0p = st0;
574dc5df763SJoerg Wunsch 
5756182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
576dc5df763SJoerg Wunsch 		/*
577dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
578dc5df763SJoerg Wunsch 		 */
5791b67be7bSPoul-Henning Kamp 		return (FD_NOT_VALID);
580dc5df763SJoerg Wunsch 	}
581dc5df763SJoerg Wunsch 
5821b67be7bSPoul-Henning Kamp 	if (fdc_in(fdc, &cyl) < 0)
5836182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
584dc5df763SJoerg Wunsch 
585dc5df763SJoerg Wunsch 	if (cylp)
586dc5df763SJoerg Wunsch 		*cylp = cyl;
587dc5df763SJoerg Wunsch 
5881b67be7bSPoul-Henning Kamp 	return (0);
589dc5df763SJoerg Wunsch }
590dc5df763SJoerg Wunsch 
5916f4e0bebSPoul-Henning Kamp static int
5921b67be7bSPoul-Henning Kamp fdc_read_status(struct fdc_data *fdc)
593dc5df763SJoerg Wunsch {
5941b67be7bSPoul-Henning Kamp 	int i, ret, status;
595b5e8ce9fSBruce Evans 
596246ed35dSJoerg Wunsch 	for (i = ret = 0; i < 7; i++) {
5971b67be7bSPoul-Henning Kamp 		ret = fdc_in(fdc, &status);
598b5e8ce9fSBruce Evans 		fdc->status[i] = status;
599b5e8ce9fSBruce Evans 		if (ret != 0)
600dc5df763SJoerg Wunsch 			break;
601dc5df763SJoerg Wunsch 	}
602dc5df763SJoerg Wunsch 
603dc5df763SJoerg Wunsch 	if (ret == 0)
604dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
605dc5df763SJoerg Wunsch 	else
606dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
607dc5df763SJoerg Wunsch 
608dc5df763SJoerg Wunsch 	return ret;
609dc5df763SJoerg Wunsch }
610dc5df763SJoerg Wunsch 
6111b67be7bSPoul-Henning Kamp /*
6121b67be7bSPoul-Henning Kamp  * Select this drive
6131b67be7bSPoul-Henning Kamp  */
6141b67be7bSPoul-Henning Kamp static void
6151b67be7bSPoul-Henning Kamp fd_select(struct fd_data *fd)
6161b67be7bSPoul-Henning Kamp {
6171b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
6181b67be7bSPoul-Henning Kamp 
6191b67be7bSPoul-Henning Kamp 	/* XXX: lock controller */
6201b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
6211b67be7bSPoul-Henning Kamp 	fdc->fdout &= ~FDO_FDSEL;
6221b67be7bSPoul-Henning Kamp 	fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
6231b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
6241b67be7bSPoul-Henning Kamp }
6251b67be7bSPoul-Henning Kamp 
6261b67be7bSPoul-Henning Kamp static void
6271b67be7bSPoul-Henning Kamp fd_turnon(void *arg)
6281b67be7bSPoul-Henning Kamp {
6291b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
6301b67be7bSPoul-Henning Kamp 	struct bio *bp;
6311b67be7bSPoul-Henning Kamp 	int once;
6321b67be7bSPoul-Henning Kamp 
6331b67be7bSPoul-Henning Kamp 	fd = arg;
6349fc9d84bSJohn Baldwin 	mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
6351b67be7bSPoul-Henning Kamp 	fd->flags &= ~FD_MOTORWAIT;
6361b67be7bSPoul-Henning Kamp 	fd->flags |= FD_MOTOR;
6371b67be7bSPoul-Henning Kamp 	once = 0;
6381b67be7bSPoul-Henning Kamp 	for (;;) {
6391b67be7bSPoul-Henning Kamp 		bp = bioq_takefirst(&fd->fd_bq);
6401b67be7bSPoul-Henning Kamp 		if (bp == NULL)
6411b67be7bSPoul-Henning Kamp 			break;
6421b67be7bSPoul-Henning Kamp 		bioq_disksort(&fd->fdc->head, bp);
6431b67be7bSPoul-Henning Kamp 		once = 1;
6441b67be7bSPoul-Henning Kamp 	}
6451b67be7bSPoul-Henning Kamp 	if (once)
6461b67be7bSPoul-Henning Kamp 		wakeup(&fd->fdc->head);
6471b67be7bSPoul-Henning Kamp }
6481b67be7bSPoul-Henning Kamp 
6491b67be7bSPoul-Henning Kamp static void
6501b67be7bSPoul-Henning Kamp fd_motor(struct fd_data *fd, int turnon)
6511b67be7bSPoul-Henning Kamp {
6521b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
6531b67be7bSPoul-Henning Kamp 
6541b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
6551b67be7bSPoul-Henning Kamp /*
6561b67be7bSPoul-Henning Kamp 	mtx_assert(&fdc->fdc_mtx, MA_OWNED);
6571b67be7bSPoul-Henning Kamp */
6581b67be7bSPoul-Henning Kamp 	if (turnon) {
6591b67be7bSPoul-Henning Kamp 		fd->flags |= FD_MOTORWAIT;
6601b67be7bSPoul-Henning Kamp 		fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
6611b67be7bSPoul-Henning Kamp 		callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
6621b67be7bSPoul-Henning Kamp 	} else {
6639fc9d84bSJohn Baldwin 		callout_stop(&fd->toffhandle);
6641b67be7bSPoul-Henning Kamp 		fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
6651b67be7bSPoul-Henning Kamp 		fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
6661b67be7bSPoul-Henning Kamp 	}
6671b67be7bSPoul-Henning Kamp 	fdout_wr(fdc, fdc->fdout);
6681b67be7bSPoul-Henning Kamp }
6691b67be7bSPoul-Henning Kamp 
6701b67be7bSPoul-Henning Kamp static void
6711b67be7bSPoul-Henning Kamp fd_turnoff(void *xfd)
6721b67be7bSPoul-Henning Kamp {
6731b67be7bSPoul-Henning Kamp 	struct fd_data *fd = xfd;
6741b67be7bSPoul-Henning Kamp 
6759fc9d84bSJohn Baldwin 	mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
6761b67be7bSPoul-Henning Kamp 	fd_motor(fd, 0);
6771b67be7bSPoul-Henning Kamp }
6781b67be7bSPoul-Henning Kamp 
6791b67be7bSPoul-Henning Kamp /*
68061909e4cSPoul-Henning Kamp  * fdc_intr - wake up the worker thread.
6811b67be7bSPoul-Henning Kamp  */
68261909e4cSPoul-Henning Kamp 
6831b67be7bSPoul-Henning Kamp static void
6841b67be7bSPoul-Henning Kamp fdc_intr(void *arg)
6851b67be7bSPoul-Henning Kamp {
6861b67be7bSPoul-Henning Kamp 
6871b67be7bSPoul-Henning Kamp 	wakeup(arg);
6881b67be7bSPoul-Henning Kamp }
6891b67be7bSPoul-Henning Kamp 
690ef544f63SPaolo Pisati static int
691ef544f63SPaolo Pisati fdc_intr_fast(void *arg)
692ef544f63SPaolo Pisati {
693ef544f63SPaolo Pisati 
694ef544f63SPaolo Pisati 	wakeup(arg);
695ef544f63SPaolo Pisati 	return(FILTER_HANDLED);
696ef544f63SPaolo Pisati }
697ef544f63SPaolo Pisati 
6981b67be7bSPoul-Henning Kamp /*
6991b67be7bSPoul-Henning Kamp  * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
7001b67be7bSPoul-Henning Kamp  */
7011b67be7bSPoul-Henning Kamp static void
7021b67be7bSPoul-Henning Kamp fdc_pio(struct fdc_data *fdc)
7031b67be7bSPoul-Henning Kamp {
7041b67be7bSPoul-Henning Kamp 	u_char *cptr;
7051b67be7bSPoul-Henning Kamp 	struct bio *bp;
7061b67be7bSPoul-Henning Kamp 	u_int count;
7071b67be7bSPoul-Henning Kamp 
7081b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
7091b67be7bSPoul-Henning Kamp 	cptr = fdc->fd->fd_ioptr;
7101b67be7bSPoul-Henning Kamp 	count = fdc->fd->fd_iosize;
7111b67be7bSPoul-Henning Kamp 
7121b67be7bSPoul-Henning Kamp 	if (bp->bio_cmd == BIO_READ) {
713b4046cd7SWarner Losh 		fdbcdr_wr(fdc, 0, count);
714973bfe6cSWarner Losh 		bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
715973bfe6cSWarner Losh 		    fdc->ioff[FD_YE_DATAPORT], cptr, count);
7161b67be7bSPoul-Henning Kamp 	} else {
717973bfe6cSWarner Losh 		bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
718973bfe6cSWarner Losh 		    fdc->ioff[FD_YE_DATAPORT], cptr, count);
719b4046cd7SWarner Losh 		fdbcdr_wr(fdc, 0, count);	/* needed? */
7201b67be7bSPoul-Henning Kamp 	}
7211b67be7bSPoul-Henning Kamp }
7221b67be7bSPoul-Henning Kamp 
7231b67be7bSPoul-Henning Kamp static int
7241b67be7bSPoul-Henning Kamp fdc_biodone(struct fdc_data *fdc, int error)
7251b67be7bSPoul-Henning Kamp {
7261b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
7271b67be7bSPoul-Henning Kamp 	struct bio *bp;
7281b67be7bSPoul-Henning Kamp 
7291b67be7bSPoul-Henning Kamp 	fd = fdc->fd;
7301b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
7311b67be7bSPoul-Henning Kamp 
7321b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
7331b67be7bSPoul-Henning Kamp 	if (--fd->fd_iocount == 0)
7341b67be7bSPoul-Henning Kamp 		callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
7351b67be7bSPoul-Henning Kamp 	fdc->bp = NULL;
7361b67be7bSPoul-Henning Kamp 	fdc->fd = NULL;
7371b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
7381b67be7bSPoul-Henning Kamp 	if (bp->bio_to != NULL) {
7391b67be7bSPoul-Henning Kamp 		if ((debugflags & 2) && fd->fdc->retry > 0)
7401b67be7bSPoul-Henning Kamp 			printf("retries: %d\n", fd->fdc->retry);
7411b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, error);
7421b67be7bSPoul-Henning Kamp 		return (0);
7431b67be7bSPoul-Henning Kamp 	}
7441b67be7bSPoul-Henning Kamp 	bp->bio_error = error;
7451b67be7bSPoul-Henning Kamp 	bp->bio_flags |= BIO_DONE;
7461b67be7bSPoul-Henning Kamp 	wakeup(bp);
7471b67be7bSPoul-Henning Kamp 	return (0);
7481b67be7bSPoul-Henning Kamp }
7491b67be7bSPoul-Henning Kamp 
7501b67be7bSPoul-Henning Kamp static int retry_line;
7511b67be7bSPoul-Henning Kamp 
7521b67be7bSPoul-Henning Kamp static int
7531b67be7bSPoul-Henning Kamp fdc_worker(struct fdc_data *fdc)
7541b67be7bSPoul-Henning Kamp {
7551b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
7561b67be7bSPoul-Henning Kamp 	struct bio *bp;
7571b67be7bSPoul-Henning Kamp 	int i, nsect;
7581b67be7bSPoul-Henning Kamp 	int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
7591b67be7bSPoul-Henning Kamp 	int head;
7601b67be7bSPoul-Henning Kamp 	static int need_recal;
7611b67be7bSPoul-Henning Kamp 	struct fdc_readid *idp;
7621b67be7bSPoul-Henning Kamp 	struct fd_formb *finfo;
7631b67be7bSPoul-Henning Kamp 
7641b67be7bSPoul-Henning Kamp 	/* Have we exhausted our retries ? */
7651b67be7bSPoul-Henning Kamp 	bp = fdc->bp;
7661b67be7bSPoul-Henning Kamp 	fd = fdc->fd;
7671b67be7bSPoul-Henning Kamp 	if (bp != NULL &&
7681217bf6aSCraig Rodrigues 		(fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
7691b67be7bSPoul-Henning Kamp 		if ((debugflags & 4))
7701b67be7bSPoul-Henning Kamp 			printf("Too many retries (EIO)\n");
771b31da03fSJung-uk Kim 		mtx_lock(&fdc->fdc_mtx);
772b31da03fSJung-uk Kim 		fd->flags |= FD_EMPTY;
773b31da03fSJung-uk Kim 		mtx_unlock(&fdc->fdc_mtx);
7741b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, EIO));
7751b67be7bSPoul-Henning Kamp 	}
7761b67be7bSPoul-Henning Kamp 
7771b67be7bSPoul-Henning Kamp 	/* Disable ISADMA if we bailed while it was active */
7781b67be7bSPoul-Henning Kamp 	if (fd != NULL && (fd->flags & FD_ISADMA)) {
7791b67be7bSPoul-Henning Kamp 		mtx_lock(&Giant);
7801b67be7bSPoul-Henning Kamp 		isa_dmadone(
7811b67be7bSPoul-Henning Kamp 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
7821b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
7831b67be7bSPoul-Henning Kamp 		mtx_unlock(&Giant);
78461909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
7851b67be7bSPoul-Henning Kamp 		fd->flags &= ~FD_ISADMA;
78661909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
7871b67be7bSPoul-Henning Kamp 	}
7881b67be7bSPoul-Henning Kamp 
7891b67be7bSPoul-Henning Kamp 	/* Unwedge the controller ? */
7901b67be7bSPoul-Henning Kamp 	if (fdc->flags & FDC_NEEDS_RESET) {
7911b67be7bSPoul-Henning Kamp 		fdc->flags &= ~FDC_NEEDS_RESET;
7921b67be7bSPoul-Henning Kamp 		fdc_reset(fdc);
7936e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdcrst", hz);
7941b67be7bSPoul-Henning Kamp 		/* Discard results */
7951b67be7bSPoul-Henning Kamp 		for (i = 0; i < 4; i++)
7961b67be7bSPoul-Henning Kamp 			fdc_sense_int(fdc, &st0, &cyl);
7971b67be7bSPoul-Henning Kamp 		/* All drives must recal */
7981b67be7bSPoul-Henning Kamp 		need_recal = 0xf;
7991b67be7bSPoul-Henning Kamp 	}
8001b67be7bSPoul-Henning Kamp 
8011b67be7bSPoul-Henning Kamp 	/* Pick up a request, if need be wait for it */
8021b67be7bSPoul-Henning Kamp 	if (fdc->bp == NULL) {
8031b67be7bSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
8041b67be7bSPoul-Henning Kamp 		do {
8051b67be7bSPoul-Henning Kamp 			fdc->bp = bioq_takefirst(&fdc->head);
8061b67be7bSPoul-Henning Kamp 			if (fdc->bp == NULL)
8071b67be7bSPoul-Henning Kamp 				msleep(&fdc->head, &fdc->fdc_mtx,
8081b67be7bSPoul-Henning Kamp 				    PRIBIO, "-", hz);
80902910eeeSJoerg Wunsch 		} while (fdc->bp == NULL &&
81002910eeeSJoerg Wunsch 		    (fdc->flags & FDC_KTHREAD_EXIT) == 0);
8111b67be7bSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
81202910eeeSJoerg Wunsch 
81302910eeeSJoerg Wunsch 		if (fdc->bp == NULL)
81402910eeeSJoerg Wunsch 			/*
81502910eeeSJoerg Wunsch 			 * Nothing to do, worker thread has been
81602910eeeSJoerg Wunsch 			 * requested to stop.
81702910eeeSJoerg Wunsch 			 */
81802910eeeSJoerg Wunsch 			return (0);
81902910eeeSJoerg Wunsch 
8201b67be7bSPoul-Henning Kamp 		bp = fdc->bp;
8211b67be7bSPoul-Henning Kamp 		fd = fdc->fd = bp->bio_driver1;
8221b67be7bSPoul-Henning Kamp 		fdc->retry = 0;
8231b67be7bSPoul-Henning Kamp 		fd->fd_ioptr = bp->bio_data;
8241b67be7bSPoul-Henning Kamp 		if (bp->bio_cmd & BIO_FMT) {
8251b67be7bSPoul-Henning Kamp 			i = offsetof(struct fd_formb, fd_formb_cylno(0));
8261b67be7bSPoul-Henning Kamp 			fd->fd_ioptr += i;
8271b67be7bSPoul-Henning Kamp 			fd->fd_iosize = bp->bio_length - i;
8281b67be7bSPoul-Henning Kamp 		}
8291b67be7bSPoul-Henning Kamp 	}
8301b67be7bSPoul-Henning Kamp 
8311b67be7bSPoul-Henning Kamp 	/* Select drive, setup params */
8321b67be7bSPoul-Henning Kamp 	fd_select(fd);
83350d99d1aSJung-uk Kim 	if (fdc->fdct == FDC_ENHANCED)
83450d99d1aSJung-uk Kim 		fddsr_wr(fdc, fd->ft->trans);
83550d99d1aSJung-uk Kim 	else
8361b67be7bSPoul-Henning Kamp 		fdctl_wr(fdc, fd->ft->trans);
8371b67be7bSPoul-Henning Kamp 
8381b67be7bSPoul-Henning Kamp 	if (bp->bio_cmd & BIO_PROBE) {
8391b67be7bSPoul-Henning Kamp 
8401b67be7bSPoul-Henning Kamp 		if (!(fdin_rd(fdc) & FDI_DCHG) && !(fd->flags & FD_EMPTY))
8411b67be7bSPoul-Henning Kamp 			return (fdc_biodone(fdc, 0));
8421b67be7bSPoul-Henning Kamp 
8431b67be7bSPoul-Henning Kamp 		/*
8441b67be7bSPoul-Henning Kamp 		 * Try to find out if we have a disk in the drive
8451b67be7bSPoul-Henning Kamp 		 *
8461b67be7bSPoul-Henning Kamp 		 * First recal, then seek to cyl#1, this clears the
8471b67be7bSPoul-Henning Kamp 		 * old condition on the disk change line so we can
8481b67be7bSPoul-Henning Kamp 		 * examine it for current status
8491b67be7bSPoul-Henning Kamp 		 */
8501b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
8511b67be7bSPoul-Henning Kamp 			printf("New disk in probe\n");
85261909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
8531b67be7bSPoul-Henning Kamp 		fd->flags |= FD_NEWDISK;
85461909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
8551b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8561b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
8571b67be7bSPoul-Henning Kamp 			return (1);
8586e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdrecal", hz);
8591b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8601b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
8611b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
8621b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8631b67be7bSPoul-Henning Kamp 		if ((st0 & 0xc0) || cyl != 0)
8641b67be7bSPoul-Henning Kamp 			return (1);
8651b67be7bSPoul-Henning Kamp 
8661b67be7bSPoul-Henning Kamp 		/* Seek to track 1 */
8671b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8681b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
8691b67be7bSPoul-Henning Kamp 			return (1);
8706e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdseek", hz);
8711b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
8721b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
8731b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
8741b67be7bSPoul-Henning Kamp 		need_recal |= (1 << fd->fdsu);
8751b67be7bSPoul-Henning Kamp 		if (fdin_rd(fdc) & FDI_DCHG) {
8761b67be7bSPoul-Henning Kamp 			if (debugflags & 0x40)
8771b67be7bSPoul-Henning Kamp 				printf("Empty in probe\n");
87861909e4cSPoul-Henning Kamp 			mtx_lock(&fdc->fdc_mtx);
8791b67be7bSPoul-Henning Kamp 			fd->flags |= FD_EMPTY;
88061909e4cSPoul-Henning Kamp 			mtx_unlock(&fdc->fdc_mtx);
8811b67be7bSPoul-Henning Kamp 		} else {
8821b67be7bSPoul-Henning Kamp 			if (debugflags & 0x40)
8831b67be7bSPoul-Henning Kamp 				printf("Got disk in probe\n");
88461909e4cSPoul-Henning Kamp 			mtx_lock(&fdc->fdc_mtx);
8851b67be7bSPoul-Henning Kamp 			fd->flags &= ~FD_EMPTY;
88661909e4cSPoul-Henning Kamp 			mtx_unlock(&fdc->fdc_mtx);
8871b67be7bSPoul-Henning Kamp 			retry_line = __LINE__;
8881b67be7bSPoul-Henning Kamp 			if(fdc_sense_drive(fdc, &st3) != 0)
8891b67be7bSPoul-Henning Kamp 				return (1);
89061909e4cSPoul-Henning Kamp 			mtx_lock(&fdc->fdc_mtx);
8911b67be7bSPoul-Henning Kamp 			if(st3 & NE7_ST3_WP)
8921b67be7bSPoul-Henning Kamp 				fd->flags |= FD_WP;
8931b67be7bSPoul-Henning Kamp 			else
8941b67be7bSPoul-Henning Kamp 				fd->flags &= ~FD_WP;
89561909e4cSPoul-Henning Kamp 			mtx_unlock(&fdc->fdc_mtx);
8961b67be7bSPoul-Henning Kamp 		}
8971b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, 0));
8981b67be7bSPoul-Henning Kamp 	}
8991b67be7bSPoul-Henning Kamp 
9001b67be7bSPoul-Henning Kamp 	/*
9011b67be7bSPoul-Henning Kamp 	 * If we are dead just flush the requests
9021b67be7bSPoul-Henning Kamp 	 */
9031b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_EMPTY)
9041b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, ENXIO));
9051b67be7bSPoul-Henning Kamp 
9061b67be7bSPoul-Henning Kamp 	/* Check if we lost our media */
9071b67be7bSPoul-Henning Kamp 	if (fdin_rd(fdc) & FDI_DCHG) {
9081b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
9091b67be7bSPoul-Henning Kamp 			printf("Lost disk\n");
91061909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
9111b67be7bSPoul-Henning Kamp 		fd->flags |= FD_EMPTY;
9121b67be7bSPoul-Henning Kamp 		fd->flags |= FD_NEWDISK;
91361909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
9141b67be7bSPoul-Henning Kamp 		g_topology_lock();
9151b67be7bSPoul-Henning Kamp 		g_orphan_provider(fd->fd_provider, EXDEV);
9161b67be7bSPoul-Henning Kamp 		fd->fd_provider->flags |= G_PF_WITHER;
9171b67be7bSPoul-Henning Kamp 		fd->fd_provider =
9181b67be7bSPoul-Henning Kamp 		    g_new_providerf(fd->fd_geom, fd->fd_geom->name);
9191b67be7bSPoul-Henning Kamp 		g_error_provider(fd->fd_provider, 0);
9201b67be7bSPoul-Henning Kamp 		g_topology_unlock();
9211b67be7bSPoul-Henning Kamp 		return (fdc_biodone(fdc, ENXIO));
9221b67be7bSPoul-Henning Kamp 	}
9231b67be7bSPoul-Henning Kamp 
9241b67be7bSPoul-Henning Kamp 	/* Check if the floppy is write-protected */
9251b67be7bSPoul-Henning Kamp 	if(bp->bio_cmd & (BIO_FMT | BIO_WRITE)) {
9261b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9271b67be7bSPoul-Henning Kamp 		if(fdc_sense_drive(fdc, &st3) != 0)
9281b67be7bSPoul-Henning Kamp 			return (1);
9291b67be7bSPoul-Henning Kamp 		if(st3 & NE7_ST3_WP)
9301b67be7bSPoul-Henning Kamp 			return (fdc_biodone(fdc, EROFS));
9311b67be7bSPoul-Henning Kamp 	}
9321b67be7bSPoul-Henning Kamp 
9331b67be7bSPoul-Henning Kamp 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
9341b67be7bSPoul-Henning Kamp 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
9351b67be7bSPoul-Henning Kamp 	i = fd->ft->sectrac * fd->ft->heads;
9361b67be7bSPoul-Henning Kamp 	cylinder = bp->bio_pblkno / i;
9371b67be7bSPoul-Henning Kamp 	descyl = cylinder * steptrac;
9381b67be7bSPoul-Henning Kamp 	sec = bp->bio_pblkno % i;
9391b67be7bSPoul-Henning Kamp 	nsect = i - sec;
9401b67be7bSPoul-Henning Kamp 	head = sec / fd->ft->sectrac;
9411b67be7bSPoul-Henning Kamp 	sec = sec % fd->ft->sectrac + 1;
9421b67be7bSPoul-Henning Kamp 
9431b67be7bSPoul-Henning Kamp 	/* If everything is going swimmingly, use multisector xfer */
9441b67be7bSPoul-Henning Kamp 	if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
9451b67be7bSPoul-Henning Kamp 		fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
9461b67be7bSPoul-Henning Kamp 		nsect = fd->fd_iosize / fd->sectorsize;
9471b67be7bSPoul-Henning Kamp 	} else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
9481b67be7bSPoul-Henning Kamp 		fd->fd_iosize = fd->sectorsize;
9491b67be7bSPoul-Henning Kamp 		nsect = 1;
9501b67be7bSPoul-Henning Kamp 	}
9511b67be7bSPoul-Henning Kamp 
9521b67be7bSPoul-Henning Kamp 	/* Do RECAL if we need to or are going to track zero anyway */
9531b67be7bSPoul-Henning Kamp 	if ((need_recal & (1 << fd->fdsu)) ||
9541b67be7bSPoul-Henning Kamp 	    (cylinder == 0 && fd->track != 0) ||
9551b67be7bSPoul-Henning Kamp 	    fdc->retry > 2) {
9561b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9571b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
9581b67be7bSPoul-Henning Kamp 			return (1);
9596e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdrecal", hz);
9601b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9611b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
9621b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
9631b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9641b67be7bSPoul-Henning Kamp 		if ((st0 & 0xc0) || cyl != 0)
9651b67be7bSPoul-Henning Kamp 			return (1);
9661b67be7bSPoul-Henning Kamp 		need_recal &= ~(1 << fd->fdsu);
9671b67be7bSPoul-Henning Kamp 		fd->track = 0;
9681b67be7bSPoul-Henning Kamp 		/* let the heads settle */
9691b67be7bSPoul-Henning Kamp 		if (settle)
9706e50e38fSJohn Baldwin 			tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
9711b67be7bSPoul-Henning Kamp 	}
9721b67be7bSPoul-Henning Kamp 
9731b67be7bSPoul-Henning Kamp 	/*
9741b67be7bSPoul-Henning Kamp 	 * SEEK to where we want to be
9751b67be7bSPoul-Henning Kamp 	 *
9761b67be7bSPoul-Henning Kamp 	 * Enhanced controllers do implied seeks for read&write as long as
9771b67be7bSPoul-Henning Kamp 	 * we do not need multiple steps per track.
9781b67be7bSPoul-Henning Kamp 	 */
9791b67be7bSPoul-Henning Kamp 	if (cylinder != fd->track && (
9801b67be7bSPoul-Henning Kamp 	    fdc->fdct != FDC_ENHANCED ||
9811b67be7bSPoul-Henning Kamp 	    descyl != cylinder ||
9821b67be7bSPoul-Henning Kamp 	    (bp->bio_cmd & (BIO_RDID|BIO_FMT)))) {
9831b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9841b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
9851b67be7bSPoul-Henning Kamp 			return (1);
9866e50e38fSJohn Baldwin 		tsleep(fdc, PRIBIO, "fdseek", hz);
9871b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9881b67be7bSPoul-Henning Kamp 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
9891b67be7bSPoul-Henning Kamp 			return (1); /* XXX */
9901b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
9911b67be7bSPoul-Henning Kamp 		if ((st0 & 0xc0) || cyl != descyl) {
9921b67be7bSPoul-Henning Kamp 			need_recal |= (1 << fd->fdsu);
9931b67be7bSPoul-Henning Kamp 			return (1);
9941b67be7bSPoul-Henning Kamp 		}
9951b67be7bSPoul-Henning Kamp 		/* let the heads settle */
9961b67be7bSPoul-Henning Kamp 		if (settle)
9976e50e38fSJohn Baldwin 			tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
9981b67be7bSPoul-Henning Kamp 	}
9991b67be7bSPoul-Henning Kamp 	fd->track = cylinder;
10001b67be7bSPoul-Henning Kamp 
10011b67be7bSPoul-Henning Kamp 	if (debugflags & 8)
10021b67be7bSPoul-Henning Kamp 		printf("op %x bn %ju siz %u ptr %p retry %d\n",
10031b67be7bSPoul-Henning Kamp 		    bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
10041b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fdc->retry);
10051b67be7bSPoul-Henning Kamp 
10061b67be7bSPoul-Henning Kamp 	/* Setup ISADMA if we need it and have it */
10071b67be7bSPoul-Henning Kamp 	if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
10081b67be7bSPoul-Henning Kamp 	     && !(fdc->flags & FDC_NODMA)) {
10091b67be7bSPoul-Henning Kamp 		mtx_lock(&Giant);
10101b67be7bSPoul-Henning Kamp 		isa_dmastart(
10111b67be7bSPoul-Henning Kamp 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
10121b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
10131b67be7bSPoul-Henning Kamp 		mtx_unlock(&Giant);
101461909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
10151b67be7bSPoul-Henning Kamp 		fd->flags |= FD_ISADMA;
101661909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
10171b67be7bSPoul-Henning Kamp 	}
10181b67be7bSPoul-Henning Kamp 
10191b67be7bSPoul-Henning Kamp 	/* Do PIO if we have to */
10201b67be7bSPoul-Henning Kamp 	if (fdc->flags & FDC_NODMA) {
10211b67be7bSPoul-Henning Kamp 		if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
1022b4046cd7SWarner Losh 			fdbcdr_wr(fdc, 1, fd->fd_iosize);
10231b67be7bSPoul-Henning Kamp 		if (bp->bio_cmd & (BIO_WRITE|BIO_FMT))
10241b67be7bSPoul-Henning Kamp 			fdc_pio(fdc);
10251b67be7bSPoul-Henning Kamp 	}
10261b67be7bSPoul-Henning Kamp 
10271b67be7bSPoul-Henning Kamp 	switch(bp->bio_cmd) {
10281b67be7bSPoul-Henning Kamp 	case BIO_FMT:
10291b67be7bSPoul-Henning Kamp 		/* formatting */
10301b67be7bSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
10311b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10321b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 6,
10331b67be7bSPoul-Henning Kamp 		    NE7CMD_FORMAT | mfm,
10341b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,
10351b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_secshift,
10361b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_nsecs,
10371b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_gaplen,
10381b67be7bSPoul-Henning Kamp 		    finfo->fd_formb_fillbyte, 0))
10391b67be7bSPoul-Henning Kamp 			return (1);
10401b67be7bSPoul-Henning Kamp 		break;
10411b67be7bSPoul-Henning Kamp 	case BIO_RDID:
10421b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10431b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 2,
10441b67be7bSPoul-Henning Kamp 		    NE7CMD_READID | mfm,
10451b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu, 0))
10461b67be7bSPoul-Henning Kamp 			return (1);
10471b67be7bSPoul-Henning Kamp 		break;
10481b67be7bSPoul-Henning Kamp 	case BIO_READ:
10491b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10501b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 9,
10511b67be7bSPoul-Henning Kamp 		    NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
10521b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,	/* head & unit */
10531b67be7bSPoul-Henning Kamp 		    fd->track,			/* track */
10541b67be7bSPoul-Henning Kamp 		    head,			/* head */
10551b67be7bSPoul-Henning Kamp 		    sec,			/* sector + 1 */
10561b67be7bSPoul-Henning Kamp 		    fd->ft->secsize,		/* sector size */
10571b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac,		/* sectors/track */
10581b67be7bSPoul-Henning Kamp 		    fd->ft->gap,		/* gap size */
10591b67be7bSPoul-Henning Kamp 		    fd->ft->datalen,		/* data length */
10601b67be7bSPoul-Henning Kamp 		    0))
10611b67be7bSPoul-Henning Kamp 			return (1);
10621b67be7bSPoul-Henning Kamp 		break;
10631b67be7bSPoul-Henning Kamp 	case BIO_WRITE:
10641b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
10651b67be7bSPoul-Henning Kamp 		if (fdc_cmd(fdc, 9,
10661b67be7bSPoul-Henning Kamp 		    NE7CMD_WRITE | mfm | NE7CMD_MT,
10671b67be7bSPoul-Henning Kamp 		    head << 2 | fd->fdsu,	/* head & unit */
10681b67be7bSPoul-Henning Kamp 		    fd->track,			/* track */
10691b67be7bSPoul-Henning Kamp 		    head,			/* head */
10701b67be7bSPoul-Henning Kamp 		    sec,			/* sector + 1 */
10711b67be7bSPoul-Henning Kamp 		    fd->ft->secsize,		/* sector size */
10721b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac,		/* sectors/track */
10731b67be7bSPoul-Henning Kamp 		    fd->ft->gap,		/* gap size */
10741b67be7bSPoul-Henning Kamp 		    fd->ft->datalen,		/* data length */
10751b67be7bSPoul-Henning Kamp 		    0))
10761b67be7bSPoul-Henning Kamp 			return (1);
10771b67be7bSPoul-Henning Kamp 		break;
10781b67be7bSPoul-Henning Kamp 	default:
10791b67be7bSPoul-Henning Kamp 		KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
10801b67be7bSPoul-Henning Kamp 	}
10811b67be7bSPoul-Henning Kamp 
10821b67be7bSPoul-Henning Kamp 	/* Wait for interrupt */
10836e50e38fSJohn Baldwin 	i = tsleep(fdc, PRIBIO, "fddata", hz);
10841b67be7bSPoul-Henning Kamp 
10851b67be7bSPoul-Henning Kamp 	/* PIO if the read looks good */
10861b67be7bSPoul-Henning Kamp 	if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ))
10871b67be7bSPoul-Henning Kamp 		fdc_pio(fdc);
10881b67be7bSPoul-Henning Kamp 
10891b67be7bSPoul-Henning Kamp 	/* Finish DMA */
10901b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_ISADMA) {
10911b67be7bSPoul-Henning Kamp 		mtx_lock(&Giant);
10921b67be7bSPoul-Henning Kamp 		isa_dmadone(
10931b67be7bSPoul-Henning Kamp 		    bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
10941b67be7bSPoul-Henning Kamp 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
10951b67be7bSPoul-Henning Kamp 		mtx_unlock(&Giant);
109661909e4cSPoul-Henning Kamp 		mtx_lock(&fdc->fdc_mtx);
10971b67be7bSPoul-Henning Kamp 		fd->flags &= ~FD_ISADMA;
109861909e4cSPoul-Henning Kamp 		mtx_unlock(&fdc->fdc_mtx);
10991b67be7bSPoul-Henning Kamp 	}
11001b67be7bSPoul-Henning Kamp 
11011b67be7bSPoul-Henning Kamp 	if (i != 0) {
11021b67be7bSPoul-Henning Kamp 		/*
11031b67be7bSPoul-Henning Kamp 		 * Timeout.
11041b67be7bSPoul-Henning Kamp 		 *
11051b67be7bSPoul-Henning Kamp 		 * Due to IBM's brain-dead design, the FDC has a faked ready
11061b67be7bSPoul-Henning Kamp 		 * signal, hardwired to ready == true. Thus, any command
11071b67be7bSPoul-Henning Kamp 		 * issued if there's no diskette in the drive will _never_
11081b67be7bSPoul-Henning Kamp 		 * complete, and must be aborted by resetting the FDC.
11091b67be7bSPoul-Henning Kamp 		 * Many thanks, Big Blue!
11101b67be7bSPoul-Henning Kamp 		 */
11111b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11121b67be7bSPoul-Henning Kamp 		fdc->flags |= FDC_NEEDS_RESET;
11131b67be7bSPoul-Henning Kamp 		return (1);
11141b67be7bSPoul-Henning Kamp 	}
11151b67be7bSPoul-Henning Kamp 
11161b67be7bSPoul-Henning Kamp 	retry_line = __LINE__;
11171b67be7bSPoul-Henning Kamp 	if (fdc_read_status(fdc))
11181b67be7bSPoul-Henning Kamp 		return (1);
11191b67be7bSPoul-Henning Kamp 
11201b67be7bSPoul-Henning Kamp 	if (debugflags & 0x10)
11211b67be7bSPoul-Henning Kamp 		printf("  -> %x %x %x %x\n",
11221b67be7bSPoul-Henning Kamp 		    fdc->status[0], fdc->status[1],
11231b67be7bSPoul-Henning Kamp 		    fdc->status[2], fdc->status[3]);
11241b67be7bSPoul-Henning Kamp 
11251b67be7bSPoul-Henning Kamp 	st0 = fdc->status[0] & NE7_ST0_IC;
11261b67be7bSPoul-Henning Kamp 	if (st0 != 0) {
11271b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11281b67be7bSPoul-Henning Kamp 		if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
11291b67be7bSPoul-Henning Kamp 			/*
11301b67be7bSPoul-Henning Kamp 			 * DMA overrun. Someone hogged the bus and
11311b67be7bSPoul-Henning Kamp 			 * didn't release it in time for the next
11321b67be7bSPoul-Henning Kamp 			 * FDC transfer.
11331b67be7bSPoul-Henning Kamp 			 */
11341b67be7bSPoul-Henning Kamp 			return (1);
11351b67be7bSPoul-Henning Kamp 		}
11361b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11371b67be7bSPoul-Henning Kamp 		if(st0 == NE7_ST0_IC_IV) {
11381b67be7bSPoul-Henning Kamp 			fdc->flags |= FDC_NEEDS_RESET;
11391b67be7bSPoul-Henning Kamp 			return (1);
11401b67be7bSPoul-Henning Kamp 		}
11411b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11421b67be7bSPoul-Henning Kamp 		if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
11431b67be7bSPoul-Henning Kamp 			need_recal |= (1 << fd->fdsu);
11441b67be7bSPoul-Henning Kamp 			return (1);
11451b67be7bSPoul-Henning Kamp 		}
11461b67be7bSPoul-Henning Kamp 		if (debugflags & 0x20) {
11471b67be7bSPoul-Henning Kamp 			printf("status %02x %02x %02x %02x %02x %02x\n",
11481b67be7bSPoul-Henning Kamp 			    fdc->status[0], fdc->status[1], fdc->status[2],
11491b67be7bSPoul-Henning Kamp 			    fdc->status[3], fdc->status[4], fdc->status[5]);
11501b67be7bSPoul-Henning Kamp 		}
11511b67be7bSPoul-Henning Kamp 		retry_line = __LINE__;
11521b67be7bSPoul-Henning Kamp 		return (1);
11531b67be7bSPoul-Henning Kamp 	}
11541b67be7bSPoul-Henning Kamp 	/* All OK */
11551b67be7bSPoul-Henning Kamp 	switch(bp->bio_cmd) {
11561b67be7bSPoul-Henning Kamp 	case BIO_RDID:
11571b67be7bSPoul-Henning Kamp 		/* copy out ID field contents */
11581b67be7bSPoul-Henning Kamp 		idp = (struct fdc_readid *)bp->bio_data;
11591b67be7bSPoul-Henning Kamp 		idp->cyl = fdc->status[3];
11601b67be7bSPoul-Henning Kamp 		idp->head = fdc->status[4];
11611b67be7bSPoul-Henning Kamp 		idp->sec = fdc->status[5];
11621b67be7bSPoul-Henning Kamp 		idp->secshift = fdc->status[6];
11631b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
11641b67be7bSPoul-Henning Kamp 			printf("c %d h %d s %d z %d\n",
11651b67be7bSPoul-Henning Kamp 			    idp->cyl, idp->head, idp->sec, idp->secshift);
11661b67be7bSPoul-Henning Kamp 		break;
11671b67be7bSPoul-Henning Kamp 	case BIO_READ:
11681b67be7bSPoul-Henning Kamp 	case BIO_WRITE:
11691b67be7bSPoul-Henning Kamp 		bp->bio_pblkno += nsect;
11701b67be7bSPoul-Henning Kamp 		bp->bio_resid -= fd->fd_iosize;
11711b67be7bSPoul-Henning Kamp 		bp->bio_completed += fd->fd_iosize;
11721b67be7bSPoul-Henning Kamp 		fd->fd_ioptr += fd->fd_iosize;
11731b67be7bSPoul-Henning Kamp 		/* Since we managed to get something done, reset the retry */
11741b67be7bSPoul-Henning Kamp 		fdc->retry = 0;
11751b67be7bSPoul-Henning Kamp 		if (bp->bio_resid > 0)
11761b67be7bSPoul-Henning Kamp 			return (0);
11771b67be7bSPoul-Henning Kamp 		break;
11781b67be7bSPoul-Henning Kamp 	case BIO_FMT:
11791b67be7bSPoul-Henning Kamp 		break;
11801b67be7bSPoul-Henning Kamp 	}
11811b67be7bSPoul-Henning Kamp 	return (fdc_biodone(fdc, 0));
11821b67be7bSPoul-Henning Kamp }
11831b67be7bSPoul-Henning Kamp 
11841b67be7bSPoul-Henning Kamp static void
11851b67be7bSPoul-Henning Kamp fdc_thread(void *arg)
11861b67be7bSPoul-Henning Kamp {
11871b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
11881b67be7bSPoul-Henning Kamp 
11891b67be7bSPoul-Henning Kamp 	fdc = arg;
11901b67be7bSPoul-Henning Kamp 	int i;
11911b67be7bSPoul-Henning Kamp 
11923fc95726SJoerg Wunsch 	mtx_lock(&fdc->fdc_mtx);
119302910eeeSJoerg Wunsch 	fdc->flags |= FDC_KTHREAD_ALIVE;
119402910eeeSJoerg Wunsch 	while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
11953fc95726SJoerg Wunsch 		mtx_unlock(&fdc->fdc_mtx);
11961b67be7bSPoul-Henning Kamp 		i = fdc_worker(fdc);
11971b67be7bSPoul-Henning Kamp 		if (i && debugflags & 0x20) {
11981b67be7bSPoul-Henning Kamp 			if (fdc->bp != NULL) {
11991b67be7bSPoul-Henning Kamp 				g_print_bio(fdc->bp);
12001b67be7bSPoul-Henning Kamp 				printf("\n");
12011b67be7bSPoul-Henning Kamp 			}
12021b67be7bSPoul-Henning Kamp 			printf("Retry line %d\n", retry_line);
12031b67be7bSPoul-Henning Kamp 		}
12041b67be7bSPoul-Henning Kamp 		fdc->retry += i;
12053fc95726SJoerg Wunsch 		mtx_lock(&fdc->fdc_mtx);
12061b67be7bSPoul-Henning Kamp 	}
120702910eeeSJoerg Wunsch 	fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
12083fc95726SJoerg Wunsch 	mtx_unlock(&fdc->fdc_mtx);
12093fc95726SJoerg Wunsch 
12103745c395SJulian Elischer 	kproc_exit(0);
12111b67be7bSPoul-Henning Kamp }
12121b67be7bSPoul-Henning Kamp 
12131b67be7bSPoul-Henning Kamp /*
1214360048a7SJoerg Wunsch  * Enqueue a request.
12151b67be7bSPoul-Henning Kamp  */
12161b67be7bSPoul-Henning Kamp static void
12171b67be7bSPoul-Henning Kamp fd_enqueue(struct fd_data *fd, struct bio *bp)
12181b67be7bSPoul-Henning Kamp {
12191b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
12201b67be7bSPoul-Henning Kamp 	int call;
12211b67be7bSPoul-Henning Kamp 
12221b67be7bSPoul-Henning Kamp 	call = 0;
12231b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
12241b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
12251b67be7bSPoul-Henning Kamp 	/* If we go from idle, cancel motor turnoff */
12261b67be7bSPoul-Henning Kamp 	if (fd->fd_iocount++ == 0)
12279fc9d84bSJohn Baldwin 		callout_stop(&fd->toffhandle);
12281b67be7bSPoul-Henning Kamp 	if (fd->flags & FD_MOTOR) {
12291b67be7bSPoul-Henning Kamp 		/* The motor is on, send it directly to the controller */
12301b67be7bSPoul-Henning Kamp 		bioq_disksort(&fdc->head, bp);
12311b67be7bSPoul-Henning Kamp 		wakeup(&fdc->head);
12321b67be7bSPoul-Henning Kamp 	} else {
12331b67be7bSPoul-Henning Kamp 		/* Queue it on the drive until the motor has started */
12341b67be7bSPoul-Henning Kamp 		bioq_insert_tail(&fd->fd_bq, bp);
12351b67be7bSPoul-Henning Kamp 		if (!(fd->flags & FD_MOTORWAIT))
12361b67be7bSPoul-Henning Kamp 			fd_motor(fd, 1);
12371b67be7bSPoul-Henning Kamp 	}
12381b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
12391b67be7bSPoul-Henning Kamp }
12401b67be7bSPoul-Henning Kamp 
12411b67be7bSPoul-Henning Kamp static int
12421b67be7bSPoul-Henning Kamp fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
12431b67be7bSPoul-Henning Kamp {
12441b67be7bSPoul-Henning Kamp 	struct bio *bp;
12451b67be7bSPoul-Henning Kamp 	struct fd_formb *finfo;
12461b67be7bSPoul-Henning Kamp 	struct fdc_readid *idfield;
12471b67be7bSPoul-Henning Kamp 	int error;
12481b67be7bSPoul-Henning Kamp 
12491b67be7bSPoul-Henning Kamp 	bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
12501b67be7bSPoul-Henning Kamp 
12511b67be7bSPoul-Henning Kamp 	/*
12521b67be7bSPoul-Henning Kamp 	 * Set up a bio request for fdstrategy().  bio_offset is faked
12531b67be7bSPoul-Henning Kamp 	 * so that fdstrategy() will seek to the the requested
12541b67be7bSPoul-Henning Kamp 	 * cylinder, and use the desired head.
12551b67be7bSPoul-Henning Kamp 	 */
12561b67be7bSPoul-Henning Kamp 	bp->bio_cmd = cmd;
12571b67be7bSPoul-Henning Kamp 	if (cmd == BIO_FMT) {
12581b67be7bSPoul-Henning Kamp 		finfo = (struct fd_formb *)data;
12591b67be7bSPoul-Henning Kamp 		bp->bio_pblkno =
12601b67be7bSPoul-Henning Kamp 		    (finfo->cyl * fd->ft->heads + finfo->head) *
12611b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac;
12621b67be7bSPoul-Henning Kamp 		bp->bio_length = sizeof *finfo;
12631b67be7bSPoul-Henning Kamp 	} else if (cmd == BIO_RDID) {
12641b67be7bSPoul-Henning Kamp 		idfield = (struct fdc_readid *)data;
12651b67be7bSPoul-Henning Kamp 		bp->bio_pblkno =
12661b67be7bSPoul-Henning Kamp 		    (idfield->cyl * fd->ft->heads + idfield->head) *
12671b67be7bSPoul-Henning Kamp 		    fd->ft->sectrac;
12681b67be7bSPoul-Henning Kamp 		bp->bio_length = sizeof(struct fdc_readid);
12691b67be7bSPoul-Henning Kamp 	} else if (cmd == BIO_PROBE) {
12701b67be7bSPoul-Henning Kamp 		/* nothing */
12711b67be7bSPoul-Henning Kamp 	} else
12721b67be7bSPoul-Henning Kamp 		panic("wrong cmd in fdmisccmd()");
12731b67be7bSPoul-Henning Kamp 	bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
12741b67be7bSPoul-Henning Kamp 	bp->bio_data = data;
12751b67be7bSPoul-Henning Kamp 	bp->bio_driver1 = fd;
12761b67be7bSPoul-Henning Kamp 	bp->bio_flags = 0;
12771b67be7bSPoul-Henning Kamp 
12781b67be7bSPoul-Henning Kamp 	fd_enqueue(fd, bp);
12791b67be7bSPoul-Henning Kamp 
12801b67be7bSPoul-Henning Kamp 	do {
12816e50e38fSJohn Baldwin 		tsleep(bp, PRIBIO, "fdwait", hz);
12821b67be7bSPoul-Henning Kamp 	} while (!(bp->bio_flags & BIO_DONE));
12831b67be7bSPoul-Henning Kamp 	error = bp->bio_error;
12841b67be7bSPoul-Henning Kamp 
12851b67be7bSPoul-Henning Kamp 	free(bp, M_TEMP);
12861b67be7bSPoul-Henning Kamp 	return (error);
12871b67be7bSPoul-Henning Kamp }
12881b67be7bSPoul-Henning Kamp 
12891b67be7bSPoul-Henning Kamp /*
12901b67be7bSPoul-Henning Kamp  * Try figuring out the density of the media present in our device.
12911b67be7bSPoul-Henning Kamp  */
12921b67be7bSPoul-Henning Kamp static int
12931b67be7bSPoul-Henning Kamp fdautoselect(struct fd_data *fd)
12941b67be7bSPoul-Henning Kamp {
12951b67be7bSPoul-Henning Kamp 	struct fd_type *fdtp;
12961b67be7bSPoul-Henning Kamp 	struct fdc_readid id;
12971b67be7bSPoul-Henning Kamp 	int oopts, rv;
12981b67be7bSPoul-Henning Kamp 
12991b67be7bSPoul-Henning Kamp 	if (!(fd->ft->flags & FL_AUTO))
13001b67be7bSPoul-Henning Kamp 		return (0);
13011b67be7bSPoul-Henning Kamp 
13021b67be7bSPoul-Henning Kamp 	fdtp = fd_native_types[fd->type];
13031b67be7bSPoul-Henning Kamp 	fdsettype(fd, fdtp);
13041b67be7bSPoul-Henning Kamp 	if (!(fd->ft->flags & FL_AUTO))
13051b67be7bSPoul-Henning Kamp 		return (0);
13061b67be7bSPoul-Henning Kamp 
13071b67be7bSPoul-Henning Kamp 	/*
13081b67be7bSPoul-Henning Kamp 	 * Try reading sector ID fields, first at cylinder 0, head 0,
13091b67be7bSPoul-Henning Kamp 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
13101b67be7bSPoul-Henning Kamp 	 * since for 5.25in DD media in a HD drive, there are no data
13111b67be7bSPoul-Henning Kamp 	 * to read (2 step pulses per media cylinder required).  For
13121b67be7bSPoul-Henning Kamp 	 * two-sided media, the second probe always goes to head 1, so
13131b67be7bSPoul-Henning Kamp 	 * we can tell them apart from single-sided media.  As a
13141b67be7bSPoul-Henning Kamp 	 * side-effect this means that single-sided media should be
13151b67be7bSPoul-Henning Kamp 	 * mentioned in the search list after two-sided media of an
13161b67be7bSPoul-Henning Kamp 	 * otherwise identical density.  Media with a different number
13171b67be7bSPoul-Henning Kamp 	 * of sectors per track but otherwise identical parameters
13181b67be7bSPoul-Henning Kamp 	 * cannot be distinguished at all.
13191b67be7bSPoul-Henning Kamp 	 *
13201b67be7bSPoul-Henning Kamp 	 * If we successfully read an ID field on both cylinders where
13211b67be7bSPoul-Henning Kamp 	 * the recorded values match our expectation, we are done.
13221b67be7bSPoul-Henning Kamp 	 * Otherwise, we try the next density entry from the table.
13231b67be7bSPoul-Henning Kamp 	 *
13241b67be7bSPoul-Henning Kamp 	 * Stepping to cylinder 2 has the side-effect of clearing the
13251b67be7bSPoul-Henning Kamp 	 * unit attention bit.
13261b67be7bSPoul-Henning Kamp 	 */
13271b67be7bSPoul-Henning Kamp 	oopts = fd->options;
13281b67be7bSPoul-Henning Kamp 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
13291b67be7bSPoul-Henning Kamp 	for (; fdtp->heads; fdtp++) {
13301b67be7bSPoul-Henning Kamp 		fdsettype(fd, fdtp);
13311b67be7bSPoul-Henning Kamp 
13321b67be7bSPoul-Henning Kamp 		id.cyl = id.head = 0;
13331b67be7bSPoul-Henning Kamp 		rv = fdmisccmd(fd, BIO_RDID, &id);
13341b67be7bSPoul-Henning Kamp 		if (rv != 0)
13351b67be7bSPoul-Henning Kamp 			continue;
13361b67be7bSPoul-Henning Kamp 		if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
13371b67be7bSPoul-Henning Kamp 			continue;
13381b67be7bSPoul-Henning Kamp 		id.cyl = 2;
13391b67be7bSPoul-Henning Kamp 		id.head = fd->ft->heads - 1;
13401b67be7bSPoul-Henning Kamp 		rv = fdmisccmd(fd, BIO_RDID, &id);
13411b67be7bSPoul-Henning Kamp 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
13421b67be7bSPoul-Henning Kamp 		    id.secshift != fdtp->secsize)
13431b67be7bSPoul-Henning Kamp 			continue;
13441b67be7bSPoul-Henning Kamp 		if (rv == 0)
13451b67be7bSPoul-Henning Kamp 			break;
13461b67be7bSPoul-Henning Kamp 	}
13471b67be7bSPoul-Henning Kamp 
13481b67be7bSPoul-Henning Kamp 	fd->options = oopts;
13491b67be7bSPoul-Henning Kamp 	if (fdtp->heads == 0) {
13501b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
13511b67be7bSPoul-Henning Kamp 			device_printf(fd->dev, "autoselection failed\n");
13521b67be7bSPoul-Henning Kamp 		fdsettype(fd, fd_native_types[fd->type]);
13531b67be7bSPoul-Henning Kamp 		return (0);
13541b67be7bSPoul-Henning Kamp 	} else {
13551b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40) {
13561b67be7bSPoul-Henning Kamp 			device_printf(fd->dev,
13571b67be7bSPoul-Henning Kamp 			    "autoselected %d KB medium\n", fd->ft->size / 2);
13581b67be7bSPoul-Henning Kamp 			fdprinttype(fd->ft);
13591b67be7bSPoul-Henning Kamp 		}
13601b67be7bSPoul-Henning Kamp 		return (0);
13611b67be7bSPoul-Henning Kamp 	}
13621b67be7bSPoul-Henning Kamp }
13631b67be7bSPoul-Henning Kamp 
13641b67be7bSPoul-Henning Kamp /*
13651b67be7bSPoul-Henning Kamp  * GEOM class implementation
13661b67be7bSPoul-Henning Kamp  */
13671b67be7bSPoul-Henning Kamp 
13681b67be7bSPoul-Henning Kamp static g_access_t	fd_access;
13691b67be7bSPoul-Henning Kamp static g_start_t	fd_start;
13701b67be7bSPoul-Henning Kamp static g_ioctl_t	fd_ioctl;
13711b67be7bSPoul-Henning Kamp 
13721b67be7bSPoul-Henning Kamp struct g_class g_fd_class = {
13731b67be7bSPoul-Henning Kamp 	.name =		"FD",
13741b67be7bSPoul-Henning Kamp 	.version =	G_VERSION,
13751b67be7bSPoul-Henning Kamp 	.start =	fd_start,
13761b67be7bSPoul-Henning Kamp 	.access =	fd_access,
13771b67be7bSPoul-Henning Kamp 	.ioctl =	fd_ioctl,
13781b67be7bSPoul-Henning Kamp };
13791b67be7bSPoul-Henning Kamp 
13801b67be7bSPoul-Henning Kamp static int
13811b67be7bSPoul-Henning Kamp fd_access(struct g_provider *pp, int r, int w, int e)
13821b67be7bSPoul-Henning Kamp {
13831b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
13841b67be7bSPoul-Henning Kamp 	struct fdc_data *fdc;
13851b67be7bSPoul-Henning Kamp 	int ar, aw, ae;
13861b67be7bSPoul-Henning Kamp 
13871b67be7bSPoul-Henning Kamp 	fd = pp->geom->softc;
13881b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
13891b67be7bSPoul-Henning Kamp 
13901b67be7bSPoul-Henning Kamp 	/*
13911b67be7bSPoul-Henning Kamp 	 * If our provider is withering, we can only get negative requests
13921b67be7bSPoul-Henning Kamp 	 * and we don't want to even see them
13931b67be7bSPoul-Henning Kamp 	 */
13941b67be7bSPoul-Henning Kamp 	if (pp->flags & G_PF_WITHER)
13951b67be7bSPoul-Henning Kamp 		return (0);
13961b67be7bSPoul-Henning Kamp 
13971b67be7bSPoul-Henning Kamp 	ar = r + pp->acr;
13981b67be7bSPoul-Henning Kamp 	aw = w + pp->acw;
13991b67be7bSPoul-Henning Kamp 	ae = e + pp->ace;
14001b67be7bSPoul-Henning Kamp 
14011b67be7bSPoul-Henning Kamp 	if (ar == 0 && aw == 0 && ae == 0) {
14021b67be7bSPoul-Henning Kamp 		device_unbusy(fd->dev);
14031b67be7bSPoul-Henning Kamp 		return (0);
14041b67be7bSPoul-Henning Kamp 	}
14051b67be7bSPoul-Henning Kamp 
14061b67be7bSPoul-Henning Kamp 	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
14071b67be7bSPoul-Henning Kamp 		if (fdmisccmd(fd, BIO_PROBE, NULL))
14081b67be7bSPoul-Henning Kamp 			return (ENXIO);
14091b67be7bSPoul-Henning Kamp 		if (fd->flags & FD_EMPTY)
14101b67be7bSPoul-Henning Kamp 			return (ENXIO);
14111b67be7bSPoul-Henning Kamp 		if (fd->flags & FD_NEWDISK) {
14121b67be7bSPoul-Henning Kamp 			fdautoselect(fd);
141361909e4cSPoul-Henning Kamp 			mtx_lock(&fdc->fdc_mtx);
14141b67be7bSPoul-Henning Kamp 			fd->flags &= ~FD_NEWDISK;
141561909e4cSPoul-Henning Kamp 			mtx_unlock(&fdc->fdc_mtx);
14161b67be7bSPoul-Henning Kamp 		}
14171b67be7bSPoul-Henning Kamp 		device_busy(fd->dev);
14181b67be7bSPoul-Henning Kamp 	}
14191b67be7bSPoul-Henning Kamp 
14201b67be7bSPoul-Henning Kamp 	if (w > 0 && (fd->flags & FD_WP))
14211b67be7bSPoul-Henning Kamp 		return (EROFS);
14221b67be7bSPoul-Henning Kamp 
14231b67be7bSPoul-Henning Kamp 	pp->sectorsize = fd->sectorsize;
14241b67be7bSPoul-Henning Kamp 	pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
14251b67be7bSPoul-Henning Kamp 	pp->mediasize = pp->stripesize * fd->ft->tracks;
14261b67be7bSPoul-Henning Kamp 	return (0);
14271b67be7bSPoul-Henning Kamp }
14281b67be7bSPoul-Henning Kamp 
14291b67be7bSPoul-Henning Kamp static void
14301b67be7bSPoul-Henning Kamp fd_start(struct bio *bp)
14311b67be7bSPoul-Henning Kamp {
14321b67be7bSPoul-Henning Kamp  	struct fdc_data *	fdc;
14331b67be7bSPoul-Henning Kamp  	struct fd_data *	fd;
14341b67be7bSPoul-Henning Kamp 
14351b67be7bSPoul-Henning Kamp 	fd = bp->bio_to->geom->softc;
14361b67be7bSPoul-Henning Kamp 	fdc = fd->fdc;
14371b67be7bSPoul-Henning Kamp 	bp->bio_driver1 = fd;
14381b67be7bSPoul-Henning Kamp 	if (bp->bio_cmd & BIO_GETATTR) {
14391b67be7bSPoul-Henning Kamp 		if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
14401b67be7bSPoul-Henning Kamp 			return;
14411b67be7bSPoul-Henning Kamp 		if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
14421b67be7bSPoul-Henning Kamp 			return;
14431b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, ENOIOCTL);
14441b67be7bSPoul-Henning Kamp 		return;
14451b67be7bSPoul-Henning Kamp 	}
14461b67be7bSPoul-Henning Kamp 	if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
14471b67be7bSPoul-Henning Kamp 		g_io_deliver(bp, EOPNOTSUPP);
14481b67be7bSPoul-Henning Kamp 		return;
14491b67be7bSPoul-Henning Kamp 	}
14501b67be7bSPoul-Henning Kamp 	bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
14511b67be7bSPoul-Henning Kamp 	bp->bio_resid = bp->bio_length;
14521b67be7bSPoul-Henning Kamp 	fd_enqueue(fd, bp);
14531b67be7bSPoul-Henning Kamp 	return;
14541b67be7bSPoul-Henning Kamp }
14551b67be7bSPoul-Henning Kamp 
14561b67be7bSPoul-Henning Kamp static int
14572221dbebSPoul-Henning Kamp fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
14581b67be7bSPoul-Henning Kamp {
14591b67be7bSPoul-Henning Kamp 	struct fd_data *fd;
14601b67be7bSPoul-Henning Kamp 	struct fdc_status *fsp;
14611b67be7bSPoul-Henning Kamp 	struct fdc_readid *rid;
14621b67be7bSPoul-Henning Kamp 	int error;
14631b67be7bSPoul-Henning Kamp 
14641b67be7bSPoul-Henning Kamp 	fd = pp->geom->softc;
14651b67be7bSPoul-Henning Kamp 
14661b67be7bSPoul-Henning Kamp 	switch (cmd) {
14671b67be7bSPoul-Henning Kamp 	case FD_GTYPE:                  /* get drive type */
14681b67be7bSPoul-Henning Kamp 		*(struct fd_type *)data = *fd->ft;
14691b67be7bSPoul-Henning Kamp 		return (0);
14701b67be7bSPoul-Henning Kamp 
14711b67be7bSPoul-Henning Kamp 	case FD_STYPE:                  /* set drive type */
147262da7a2dSPoul-Henning Kamp 		if (!(fflag & FWRITE))
14732221dbebSPoul-Henning Kamp 			return (EPERM);
14741b67be7bSPoul-Henning Kamp 		/*
14751b67be7bSPoul-Henning Kamp 		 * Allow setting drive type temporarily iff
14761b67be7bSPoul-Henning Kamp 		 * currently unset.  Used for fdformat so any
14771b67be7bSPoul-Henning Kamp 		 * user can set it, and then start formatting.
14781b67be7bSPoul-Henning Kamp 		 */
14791b67be7bSPoul-Henning Kamp 		fd->fts = *(struct fd_type *)data;
14801b67be7bSPoul-Henning Kamp 		if (fd->fts.sectrac) {
14811b67be7bSPoul-Henning Kamp 			/* XXX: check for rubbish */
14821b67be7bSPoul-Henning Kamp 			fdsettype(fd, &fd->fts);
14831b67be7bSPoul-Henning Kamp 		} else {
14841b67be7bSPoul-Henning Kamp 			fdsettype(fd, fd_native_types[fd->type]);
14851b67be7bSPoul-Henning Kamp 		}
14861b67be7bSPoul-Henning Kamp 		if (debugflags & 0x40)
14871b67be7bSPoul-Henning Kamp 			fdprinttype(fd->ft);
14881b67be7bSPoul-Henning Kamp 		return (0);
14891b67be7bSPoul-Henning Kamp 
14901b67be7bSPoul-Henning Kamp 	case FD_GOPTS:			/* get drive options */
14911b67be7bSPoul-Henning Kamp 		*(int *)data = fd->options;
14921b67be7bSPoul-Henning Kamp 		return (0);
14931b67be7bSPoul-Henning Kamp 
14941b67be7bSPoul-Henning Kamp 	case FD_SOPTS:			/* set drive options */
149562da7a2dSPoul-Henning Kamp 		if (!(fflag & FWRITE))
14962221dbebSPoul-Henning Kamp 			return (EPERM);
14971b67be7bSPoul-Henning Kamp 		fd->options = *(int *)data;
14981b67be7bSPoul-Henning Kamp 		return (0);
14991b67be7bSPoul-Henning Kamp 
15001b67be7bSPoul-Henning Kamp 	case FD_CLRERR:
1501acd3428bSRobert Watson 		error = priv_check(td, PRIV_DRIVER);
1502acd3428bSRobert Watson 		if (error)
1503acd3428bSRobert Watson 			return (error);
15041b67be7bSPoul-Henning Kamp 		fd->fdc->fdc_errs = 0;
15051b67be7bSPoul-Henning Kamp 		return (0);
15061b67be7bSPoul-Henning Kamp 
15071b67be7bSPoul-Henning Kamp 	case FD_GSTAT:
15081b67be7bSPoul-Henning Kamp 		fsp = (struct fdc_status *)data;
15091b67be7bSPoul-Henning Kamp 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
15101b67be7bSPoul-Henning Kamp 			return (EINVAL);
15111b67be7bSPoul-Henning Kamp 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
15121b67be7bSPoul-Henning Kamp 		return (0);
15131b67be7bSPoul-Henning Kamp 
15141b67be7bSPoul-Henning Kamp 	case FD_GDTYPE:
15151b67be7bSPoul-Henning Kamp 		*(enum fd_drivetype *)data = fd->type;
15161b67be7bSPoul-Henning Kamp 		return (0);
15171b67be7bSPoul-Henning Kamp 
15181b67be7bSPoul-Henning Kamp 	case FD_FORM:
151962da7a2dSPoul-Henning Kamp 		if (!(fflag & FWRITE))
15202221dbebSPoul-Henning Kamp 			return (EPERM);
15211b67be7bSPoul-Henning Kamp 		if (((struct fd_formb *)data)->format_version !=
15221b67be7bSPoul-Henning Kamp 		    FD_FORMAT_VERSION)
15231b67be7bSPoul-Henning Kamp 			return (EINVAL); /* wrong version of formatting prog */
15241b67be7bSPoul-Henning Kamp 		error = fdmisccmd(fd, BIO_FMT, data);
152561909e4cSPoul-Henning Kamp 		mtx_lock(&fd->fdc->fdc_mtx);
15261b67be7bSPoul-Henning Kamp 		fd->flags |= FD_NEWDISK;
152761909e4cSPoul-Henning Kamp 		mtx_unlock(&fd->fdc->fdc_mtx);
15281b67be7bSPoul-Henning Kamp 		break;
15291b67be7bSPoul-Henning Kamp 
15301b67be7bSPoul-Henning Kamp 	case FD_READID:
15311b67be7bSPoul-Henning Kamp 		rid = (struct fdc_readid *)data;
15321b67be7bSPoul-Henning Kamp 		if (rid->cyl > 85 || rid->head > 1)
15331b67be7bSPoul-Henning Kamp 			return (EINVAL);
15341b67be7bSPoul-Henning Kamp 		error = fdmisccmd(fd, BIO_RDID, data);
15351b67be7bSPoul-Henning Kamp 		break;
15361b67be7bSPoul-Henning Kamp 
15371b67be7bSPoul-Henning Kamp 	case FIONBIO:
15381b67be7bSPoul-Henning Kamp 	case FIOASYNC:
15391b67be7bSPoul-Henning Kamp 		/* For backwards compat with old fd*(8) tools */
15401b67be7bSPoul-Henning Kamp 		error = 0;
15411b67be7bSPoul-Henning Kamp 		break;
15421b67be7bSPoul-Henning Kamp 
15431b67be7bSPoul-Henning Kamp 	default:
15441b67be7bSPoul-Henning Kamp 		if (debugflags & 0x80)
15451b67be7bSPoul-Henning Kamp 			printf("Unknown ioctl %lx\n", cmd);
15461b67be7bSPoul-Henning Kamp 		error = ENOIOCTL;
15471b67be7bSPoul-Henning Kamp 		break;
15481b67be7bSPoul-Henning Kamp 	}
15491b67be7bSPoul-Henning Kamp 	return (error);
15501b67be7bSPoul-Henning Kamp };
15511b67be7bSPoul-Henning Kamp 
15521b67be7bSPoul-Henning Kamp 
15531b67be7bSPoul-Henning Kamp 
15541b67be7bSPoul-Henning Kamp /*
15551b67be7bSPoul-Henning Kamp  * Configuration/initialization stuff, per controller.
15561b67be7bSPoul-Henning Kamp  */
15571b67be7bSPoul-Henning Kamp 
15581b67be7bSPoul-Henning Kamp devclass_t fdc_devclass;
15591b67be7bSPoul-Henning Kamp static devclass_t fd_devclass;
15601b67be7bSPoul-Henning Kamp 
15611b67be7bSPoul-Henning Kamp struct fdc_ivars {
15621b67be7bSPoul-Henning Kamp 	int	fdunit;
15631b67be7bSPoul-Henning Kamp 	int	fdtype;
15641b67be7bSPoul-Henning Kamp };
15651b67be7bSPoul-Henning Kamp 
15666d6fa4fdSWarner Losh void
156737286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
156837286586SPeter Wemm {
156937286586SPeter Wemm 	device_t dev;
1570973bfe6cSWarner Losh 	struct resource *last;
1571973bfe6cSWarner Losh 	int i;
157237286586SPeter Wemm 
157337286586SPeter Wemm 	dev = fdc->fdc_dev;
1574b4046cd7SWarner Losh 	if (fdc->fdc_intr)
15757964bd0cSWarner Losh 		bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1576a6e4d8c4SNate Lawson 	fdc->fdc_intr = NULL;
1577b4046cd7SWarner Losh 	if (fdc->res_irq != NULL)
157837286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
157937286586SPeter Wemm 		    fdc->res_irq);
1580a6e4d8c4SNate Lawson 	fdc->res_irq = NULL;
1581973bfe6cSWarner Losh 	last = NULL;
1582973bfe6cSWarner Losh 	for (i = 0; i < FDC_MAXREG; i++) {
1583973bfe6cSWarner Losh 		if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1584973bfe6cSWarner Losh 			bus_release_resource(dev, SYS_RES_IOPORT,
1585973bfe6cSWarner Losh 			    fdc->ridio[i], fdc->resio[i]);
1586973bfe6cSWarner Losh 			last = fdc->resio[i];
1587973bfe6cSWarner Losh 			fdc->resio[i] = NULL;
1588973bfe6cSWarner Losh 		}
1589973bfe6cSWarner Losh 	}
1590b4046cd7SWarner Losh 	if (fdc->res_drq != NULL)
159137286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
159237286586SPeter Wemm 		    fdc->res_drq);
15935ceae6b8SYoshihiro Takahashi 	fdc->res_drq = NULL;
159437286586SPeter Wemm }
159537286586SPeter Wemm 
15966d6fa4fdSWarner Losh int
15973aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
159837286586SPeter Wemm {
159937286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
160037286586SPeter Wemm 
160137286586SPeter Wemm 	switch (which) {
160237286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
160337286586SPeter Wemm 		*result = ivars->fdunit;
160437286586SPeter Wemm 		break;
1605752d4735SNate Lawson 	case FDC_IVAR_FDTYPE:
1606752d4735SNate Lawson 		*result = ivars->fdtype;
1607752d4735SNate Lawson 		break;
160837286586SPeter Wemm 	default:
1609752d4735SNate Lawson 		return (ENOENT);
161037286586SPeter Wemm 	}
1611752d4735SNate Lawson 	return (0);
1612752d4735SNate Lawson }
1613752d4735SNate Lawson 
1614752d4735SNate Lawson int
1615752d4735SNate Lawson fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1616752d4735SNate Lawson {
1617752d4735SNate Lawson 	struct fdc_ivars *ivars = device_get_ivars(child);
1618752d4735SNate Lawson 
1619752d4735SNate Lawson 	switch (which) {
1620752d4735SNate Lawson 	case FDC_IVAR_FDUNIT:
1621752d4735SNate Lawson 		ivars->fdunit = value;
1622752d4735SNate Lawson 		break;
1623752d4735SNate Lawson 	case FDC_IVAR_FDTYPE:
1624752d4735SNate Lawson 		ivars->fdtype = value;
1625752d4735SNate Lawson 		break;
1626752d4735SNate Lawson 	default:
1627752d4735SNate Lawson 		return (ENOENT);
1628752d4735SNate Lawson 	}
1629752d4735SNate Lawson 	return (0);
1630752d4735SNate Lawson }
1631752d4735SNate Lawson 
1632752d4735SNate Lawson int
16331b67be7bSPoul-Henning Kamp fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1634752d4735SNate Lawson {
16351b67be7bSPoul-Henning Kamp 	int ic_type, part_id;
16361b67be7bSPoul-Henning Kamp 
1637ec200f23SPoul-Henning Kamp 	/*
1638ec200f23SPoul-Henning Kamp 	 * A status value of 0xff is very unlikely, but not theoretically
1639ec200f23SPoul-Henning Kamp 	 * impossible, but it is far more likely to indicate an empty bus.
1640ec200f23SPoul-Henning Kamp 	 */
1641ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) == 0xff)
1642ec200f23SPoul-Henning Kamp 		return (ENXIO);
1643ec200f23SPoul-Henning Kamp 
1644ec200f23SPoul-Henning Kamp 	/*
1645ec200f23SPoul-Henning Kamp 	 * Assert a reset to the floppy controller and check that the status
1646ec200f23SPoul-Henning Kamp 	 * register goes to zero.
1647ec200f23SPoul-Henning Kamp 	 */
1648752d4735SNate Lawson 	fdout_wr(fdc, 0);
1649ec200f23SPoul-Henning Kamp 	fdout_wr(fdc, 0);
1650ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) != 0)
1651ec200f23SPoul-Henning Kamp 		return (ENXIO);
1652ec200f23SPoul-Henning Kamp 
1653ec200f23SPoul-Henning Kamp 	/*
1654ec200f23SPoul-Henning Kamp 	 * Clear the reset and see it come ready.
1655ec200f23SPoul-Henning Kamp 	 */
1656752d4735SNate Lawson 	fdout_wr(fdc, FDO_FRST);
1657ec200f23SPoul-Henning Kamp 	DELAY(100);
1658ec200f23SPoul-Henning Kamp 	if (fdsts_rd(fdc) != 0x80)
1659ec200f23SPoul-Henning Kamp 		return (ENXIO);
1660752d4735SNate Lawson 
1661752d4735SNate Lawson 	/* Then, see if it can handle a command. */
16621b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0))
1663752d4735SNate Lawson 		return (ENXIO);
16641b67be7bSPoul-Henning Kamp 
16651b67be7bSPoul-Henning Kamp 	/*
16661b67be7bSPoul-Henning Kamp 	 * Try to identify the chip.
16671b67be7bSPoul-Henning Kamp 	 *
16681b67be7bSPoul-Henning Kamp 	 * The i8272 datasheet documents that unknown commands
16691b67be7bSPoul-Henning Kamp 	 * will return ST0 as 0x80.  The i8272 is supposedly identical
16701b67be7bSPoul-Henning Kamp 	 * to the NEC765.
16711b67be7bSPoul-Henning Kamp 	 * The i82077SL datasheet says 0x90 for the VERSION command,
16721b67be7bSPoul-Henning Kamp 	 * and several "superio" chips emulate this.
16731b67be7bSPoul-Henning Kamp 	 */
16741b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
16751b67be7bSPoul-Henning Kamp 		return (ENXIO);
16761b67be7bSPoul-Henning Kamp 	if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
16771b67be7bSPoul-Henning Kamp 		return (ENXIO);
16781d4b1bb7SPoul-Henning Kamp 	if (bootverbose)
16791d4b1bb7SPoul-Henning Kamp 		device_printf(dev,
16801d4b1bb7SPoul-Henning Kamp 		    "ic_type %02x part_id %02x\n", ic_type, part_id);
16811b67be7bSPoul-Henning Kamp 	switch (ic_type & 0xff) {
16821b67be7bSPoul-Henning Kamp 	case 0x80:
16831b67be7bSPoul-Henning Kamp 		device_set_desc(dev, "NEC 765 or clone");
16841b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_NE765;
16851b67be7bSPoul-Henning Kamp 		break;
16861b67be7bSPoul-Henning Kamp 	case 0x81:
16871b67be7bSPoul-Henning Kamp 	case 0x90:
16881b67be7bSPoul-Henning Kamp 		device_set_desc(dev,
16891b67be7bSPoul-Henning Kamp 		    "Enhanced floppy controller");
16901b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_ENHANCED;
16911b67be7bSPoul-Henning Kamp 		break;
16921b67be7bSPoul-Henning Kamp 	default:
16931b67be7bSPoul-Henning Kamp 		device_set_desc(dev, "Generic floppy controller");
16941b67be7bSPoul-Henning Kamp 		fdc->fdct = FDC_UNKNOWN;
16951b67be7bSPoul-Henning Kamp 		break;
16961b67be7bSPoul-Henning Kamp 	}
1697752d4735SNate Lawson 	return (0);
169837286586SPeter Wemm }
169937286586SPeter Wemm 
17006d6fa4fdSWarner Losh int
1701e219897aSJoerg Wunsch fdc_detach(device_t dev)
17025f830ea2SJoerg Wunsch {
17035f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
17045f830ea2SJoerg Wunsch 	int	error;
17055f830ea2SJoerg Wunsch 
17065f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
17075f830ea2SJoerg Wunsch 
17085f830ea2SJoerg Wunsch 	/* have our children detached first */
17095f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
17105f830ea2SJoerg Wunsch 		return (error);
17115f830ea2SJoerg Wunsch 
171202910eeeSJoerg Wunsch 	/* kill worker thread */
171302910eeeSJoerg Wunsch 	mtx_lock(&fdc->fdc_mtx);
17145f6fcd82SJohn Baldwin 	fdc->flags |= FDC_KTHREAD_EXIT;
1715f8353edeSPoul-Henning Kamp 	wakeup(&fdc->head);
171602910eeeSJoerg Wunsch 	while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
171702910eeeSJoerg Wunsch 		msleep(&fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
171802910eeeSJoerg Wunsch 	mtx_unlock(&fdc->fdc_mtx);
171902910eeeSJoerg Wunsch 
1720e219897aSJoerg Wunsch 	/* reset controller, turn motor off */
1721e219897aSJoerg Wunsch 	fdout_wr(fdc, 0);
1722e219897aSJoerg Wunsch 
17237964bd0cSWarner Losh 	if (!(fdc->flags & FDC_NODMA))
17247964bd0cSWarner Losh 		isa_dma_release(fdc->dmachan);
17255f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
17261b67be7bSPoul-Henning Kamp 	mtx_destroy(&fdc->fdc_mtx);
17275f830ea2SJoerg Wunsch 	return (0);
17285f830ea2SJoerg Wunsch }
17295f830ea2SJoerg Wunsch 
17305b81b6b3SRodney W. Grimes /*
173137286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
17325b81b6b3SRodney W. Grimes  */
1733a6e4d8c4SNate Lawson device_t
173437286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
17355b81b6b3SRodney W. Grimes {
173637286586SPeter Wemm 	struct fdc_ivars *ivar;
17376182fdbdSPeter Wemm 	device_t child;
173892200632SGarrett Wollman 
17397cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
174037286586SPeter Wemm 	if (ivar == NULL)
1741a6e4d8c4SNate Lawson 		return (NULL);
1742fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
174342117b6cSPoul-Henning Kamp 	if (child == NULL) {
174442117b6cSPoul-Henning Kamp 		free(ivar, M_DEVBUF);
1745a6e4d8c4SNate Lawson 		return (NULL);
174642117b6cSPoul-Henning Kamp 	}
174737286586SPeter Wemm 	device_set_ivars(child, ivar);
1748a6e4d8c4SNate Lawson 	ivar->fdunit = unit;
1749a6e4d8c4SNate Lawson 	ivar->fdtype = FDT_NONE;
17508a9bc9c0SJohn Baldwin 	if (resource_disabled(name, unit))
17516182fdbdSPeter Wemm 		device_disable(child);
1752a6e4d8c4SNate Lawson 	return (child);
17536182fdbdSPeter Wemm }
17546182fdbdSPeter Wemm 
17556d6fa4fdSWarner Losh int
17566182fdbdSPeter Wemm fdc_attach(device_t dev)
17576182fdbdSPeter Wemm {
175837286586SPeter Wemm 	struct	fdc_data *fdc;
1759a6e4d8c4SNate Lawson 	int	error;
1760427ccf00SDoug Rabson 
176137286586SPeter Wemm 	fdc = device_get_softc(dev);
1762b0c2b925SWarner Losh 	fdc->fdc_dev = dev;
17631b67be7bSPoul-Henning Kamp 	error = fdc_initial_reset(dev, fdc);
17641b67be7bSPoul-Henning Kamp 	if (error) {
17651b67be7bSPoul-Henning Kamp 		device_printf(dev, "does not respond\n");
17661b67be7bSPoul-Henning Kamp 		return (error);
17671b67be7bSPoul-Henning Kamp 	}
1768b593dd82SWarner Losh 	error = bus_setup_intr(dev, fdc->res_irq,
1769ef544f63SPaolo Pisati 	    INTR_TYPE_BIO | INTR_ENTROPY |
1770ef544f63SPaolo Pisati 	    ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
1771ef544f63SPaolo Pisati             ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
1772ef544f63SPaolo Pisati 	    ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
1773ef544f63SPaolo Pisati 			       fdc, &fdc->fdc_intr);
177437286586SPeter Wemm 	if (error) {
177537286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
17761b67be7bSPoul-Henning Kamp 		return (error);
177737286586SPeter Wemm 	}
17787964bd0cSWarner Losh 	if (!(fdc->flags & FDC_NODMA)) {
17797964bd0cSWarner Losh 		error = isa_dma_acquire(fdc->dmachan);
17801ef0fd82SPoul-Henning Kamp 		if (!error) {
17811ef0fd82SPoul-Henning Kamp 			error = isa_dma_init(fdc->dmachan,
17821ef0fd82SPoul-Henning Kamp 			    MAX_BYTES_PER_CYL, M_WAITOK);
17831ef0fd82SPoul-Henning Kamp 			if (error)
17841ef0fd82SPoul-Henning Kamp 				isa_dma_release(fdc->dmachan);
17851ef0fd82SPoul-Henning Kamp 		}
17867964bd0cSWarner Losh 		if (error)
17877964bd0cSWarner Losh 			return (error);
17887964bd0cSWarner Losh 	}
178937286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
1790a6e4d8c4SNate Lawson 	fdc->flags |= FDC_NEEDS_RESET;
17916182fdbdSPeter Wemm 
17921b67be7bSPoul-Henning Kamp 	mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
17936182fdbdSPeter Wemm 
17943a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
179564860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
17968177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
17975b81b6b3SRodney W. Grimes 
17983745c395SJulian Elischer 	kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
17991b67be7bSPoul-Henning Kamp 	    "fdc%d", device_get_unit(dev));
18001b67be7bSPoul-Henning Kamp 
18011b67be7bSPoul-Henning Kamp 	settle = hz / 8;
18021b67be7bSPoul-Henning Kamp 
1803a6e4d8c4SNate Lawson 	return (0);
1804a6e4d8c4SNate Lawson }
1805a6e4d8c4SNate Lawson 
1806a6e4d8c4SNate Lawson int
1807a6e4d8c4SNate Lawson fdc_hints_probe(device_t dev)
1808a6e4d8c4SNate Lawson {
1809a6e4d8c4SNate Lawson 	const char *name, *dname;
1810a6e4d8c4SNate Lawson 	int i, error, dunit;
1811a6e4d8c4SNate Lawson 
18126182fdbdSPeter Wemm 	/*
181337286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
181437286586SPeter Wemm 	 * devices from the BIOS unless overridden.
18156182fdbdSPeter Wemm 	 */
1816ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
18172398f0cdSPeter Wemm 	i = 0;
1818a6e4d8c4SNate Lawson 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1819a6e4d8c4SNate Lawson 		resource_int_value(dname, dunit, "drive", &dunit);
18202398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
1821a6e4d8c4SNate Lawson 	}
182237286586SPeter Wemm 
182360444853SJoerg Wunsch 	if ((error = bus_generic_attach(dev)) != 0)
182460444853SJoerg Wunsch 		return (error);
182560444853SJoerg Wunsch 	return (0);
18266182fdbdSPeter Wemm }
18276182fdbdSPeter Wemm 
18286d6fa4fdSWarner Losh int
18296182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
18306182fdbdSPeter Wemm {
18311a6bed68SJoerg Wunsch 	int retval = 0, flags;
183215317dd8SMatthew N. Dodd 
183315317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
18341a6bed68SJoerg Wunsch 	retval += printf(" on %s drive %d", device_get_nameunit(me),
183537286586SPeter Wemm 	       fdc_get_fdunit(child));
18361a6bed68SJoerg Wunsch 	if ((flags = device_get_flags(me)) != 0)
18371a6bed68SJoerg Wunsch 		retval += printf(" flags %#x", flags);
18381a6bed68SJoerg Wunsch 	retval += printf("\n");
183915317dd8SMatthew N. Dodd 
184015317dd8SMatthew N. Dodd 	return (retval);
18416182fdbdSPeter Wemm }
18426182fdbdSPeter Wemm 
184316e68fc6SPeter Wemm /*
1844246ed35dSJoerg Wunsch  * Configuration/initialization, per drive.
184516e68fc6SPeter Wemm  */
18466182fdbdSPeter Wemm static int
18476182fdbdSPeter Wemm fd_probe(device_t dev)
18486182fdbdSPeter Wemm {
18491b67be7bSPoul-Henning Kamp 	int	i, unit;
18501a6bed68SJoerg Wunsch 	u_int	st0, st3;
18516182fdbdSPeter Wemm 	struct	fd_data *fd;
18526182fdbdSPeter Wemm 	struct	fdc_data *fdc;
18531b67be7bSPoul-Henning Kamp 	int	fdsu;
1854752d4735SNate Lawson 	int	flags, type;
18556182fdbdSPeter Wemm 
1856752d4735SNate Lawson 	fdsu = fdc_get_fdunit(dev);
18576182fdbdSPeter Wemm 	fd = device_get_softc(dev);
18586182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
18591a6bed68SJoerg Wunsch 	flags = device_get_flags(dev);
18606182fdbdSPeter Wemm 
18616182fdbdSPeter Wemm 	fd->dev = dev;
18626182fdbdSPeter Wemm 	fd->fdc = fdc;
18636182fdbdSPeter Wemm 	fd->fdsu = fdsu;
18641b67be7bSPoul-Henning Kamp 	unit = device_get_unit(dev);
18656182fdbdSPeter Wemm 
1866752d4735SNate Lawson 	/* Auto-probe if fdinfo is present, but always allow override. */
18671b67be7bSPoul-Henning Kamp 	type = flags & FD_TYPEMASK;
1868752d4735SNate Lawson 	if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1869752d4735SNate Lawson 		fd->type = type;
1870752d4735SNate Lawson 		goto done;
1871752d4735SNate Lawson 	} else {
1872752d4735SNate Lawson 		/* make sure fdautoselect() will be called */
18731b67be7bSPoul-Henning Kamp 		fd->flags = FD_EMPTY;
1874752d4735SNate Lawson 		fd->type = type;
1875752d4735SNate Lawson 	}
1876752d4735SNate Lawson 
187727df52f9SWarner Losh #if (defined(__i386__) && !defined(PC98)) || defined(__amd64__)
18781b67be7bSPoul-Henning Kamp 	if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
18791a6bed68SJoerg Wunsch 		/* Look up what the BIOS thinks we have. */
188027df52f9SWarner Losh 		if (unit == 0)
18811a6bed68SJoerg Wunsch 			fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
188227df52f9SWarner Losh 		else
18831a6bed68SJoerg Wunsch 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
18841a6bed68SJoerg Wunsch 		if (fd->type == FDT_288M_1)
18851a6bed68SJoerg Wunsch 			fd->type = FDT_288M;
18861a6bed68SJoerg Wunsch 	}
188704209354SPeter Wemm #endif /* __i386__ || __amd64__ */
18886182fdbdSPeter Wemm 	/* is there a unit? */
18891a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE)
18906182fdbdSPeter Wemm 		return (ENXIO);
18916182fdbdSPeter Wemm 
18921b67be7bSPoul-Henning Kamp /*
18931b67be7bSPoul-Henning Kamp 	mtx_lock(&fdc->fdc_mtx);
18941b67be7bSPoul-Henning Kamp */
18956182fdbdSPeter Wemm 	/* select it */
18961b67be7bSPoul-Henning Kamp 	fd_select(fd);
18971b67be7bSPoul-Henning Kamp 	fd_motor(fd, 1);
18981b67be7bSPoul-Henning Kamp 	fdc->fd = fd;
1899fb35bd37SJoerg Wunsch 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
19006182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
19016182fdbdSPeter Wemm 
19021a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0) {
1903dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
19041b67be7bSPoul-Henning Kamp 		if ((fdc_sense_drive(fdc, &st3) == 0) &&
19051a6bed68SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
1906dc5df763SJoerg Wunsch 			/* Seek some steps... */
19071b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1908dc5df763SJoerg Wunsch 				/* ...wait a moment... */
1909dc5df763SJoerg Wunsch 				DELAY(300000);
1910dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
1911a7e943a4SPyun YongHyeon 				fdc_sense_int(fdc, NULL, NULL);
1912dc5df763SJoerg Wunsch 			}
1913dc5df763SJoerg Wunsch 		}
1914dc5df763SJoerg Wunsch 
19156b7bd95bSJoerg Wunsch 		for (i = 0; i < 2; i++) {
19166b7bd95bSJoerg Wunsch 			/*
19176b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
19181a6bed68SJoerg Wunsch 			 * heads have been beyond cylinder 76, since
19191a6bed68SJoerg Wunsch 			 * most FDCs still barf when attempting to
19201a6bed68SJoerg Wunsch 			 * recalibrate more than 77 steps
19216b7bd95bSJoerg Wunsch 			 */
1922dc5df763SJoerg Wunsch 			/* go back to 0: */
19231b67be7bSPoul-Henning Kamp 			if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
19246b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
19256b7bd95bSJoerg Wunsch 				DELAY(i == 0 ? 1000000 : 300000);
19265b81b6b3SRodney W. Grimes 
19276b7bd95bSJoerg Wunsch 				/* anything responding? */
1928a7e943a4SPyun YongHyeon 				if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
1929dc5df763SJoerg Wunsch 				    (st0 & NE7_ST0_EC) == 0)
19306b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
19316b7bd95bSJoerg Wunsch 			}
1932dc5df763SJoerg Wunsch 		}
19331a6bed68SJoerg Wunsch 	}
19346b7bd95bSJoerg Wunsch 
19351b67be7bSPoul-Henning Kamp 	fd_motor(fd, 0);
19361b67be7bSPoul-Henning Kamp 	fdc->fd = NULL;
19371b67be7bSPoul-Henning Kamp /*
19381b67be7bSPoul-Henning Kamp 	mtx_unlock(&fdc->fdc_mtx);
19391b67be7bSPoul-Henning Kamp */
19403a2f7427SDavid Greenman 
19411a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0 &&
19421a6bed68SJoerg Wunsch 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
19436182fdbdSPeter Wemm 		return (ENXIO);
19445b81b6b3SRodney W. Grimes 
1945752d4735SNate Lawson done:
1946752d4735SNate Lawson 
19471a6bed68SJoerg Wunsch 	switch (fd->type) {
19481a6bed68SJoerg Wunsch 	case FDT_12M:
19491a6bed68SJoerg Wunsch 		device_set_desc(dev, "1200-KB 5.25\" drive");
19501a6bed68SJoerg Wunsch 		break;
19511a6bed68SJoerg Wunsch 	case FDT_144M:
19521a6bed68SJoerg Wunsch 		device_set_desc(dev, "1440-KB 3.5\" drive");
19531a6bed68SJoerg Wunsch 		break;
19541a6bed68SJoerg Wunsch 	case FDT_288M:
19551a6bed68SJoerg Wunsch 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
19561a6bed68SJoerg Wunsch 		break;
19571a6bed68SJoerg Wunsch 	case FDT_360K:
19581a6bed68SJoerg Wunsch 		device_set_desc(dev, "360-KB 5.25\" drive");
19591a6bed68SJoerg Wunsch 		break;
19601a6bed68SJoerg Wunsch 	case FDT_720K:
19611a6bed68SJoerg Wunsch 		device_set_desc(dev, "720-KB 3.5\" drive");
19621a6bed68SJoerg Wunsch 		break;
19631a6bed68SJoerg Wunsch 	default:
19641a6bed68SJoerg Wunsch 		return (ENXIO);
19651a6bed68SJoerg Wunsch 	}
1966dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1967b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1968b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
19693a2f7427SDavid Greenman 	fd->options = 0;
19709fc9d84bSJohn Baldwin 	callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
19715b81b6b3SRodney W. Grimes 
19721a6bed68SJoerg Wunsch 	/* initialize densities for subdevices */
19731b67be7bSPoul-Henning Kamp 	fdsettype(fd, fd_native_types[fd->type]);
19746182fdbdSPeter Wemm 	return (0);
19756182fdbdSPeter Wemm }
19766182fdbdSPeter Wemm 
19771b67be7bSPoul-Henning Kamp /*
19781b67be7bSPoul-Henning Kamp  * We have to do this in a geom event because GEOM is not running
19791b67be7bSPoul-Henning Kamp  * when fd_attach() is.
19801b67be7bSPoul-Henning Kamp  * XXX: move fd_attach after geom like ata/scsi disks
19811b67be7bSPoul-Henning Kamp  */
19821b67be7bSPoul-Henning Kamp static void
19831b67be7bSPoul-Henning Kamp fd_attach2(void *arg, int flag)
19841b67be7bSPoul-Henning Kamp {
19851b67be7bSPoul-Henning Kamp 	struct	fd_data *fd;
19861b67be7bSPoul-Henning Kamp 
19871b67be7bSPoul-Henning Kamp 	fd = arg;
19881b67be7bSPoul-Henning Kamp 
19891b67be7bSPoul-Henning Kamp 	fd->fd_geom = g_new_geomf(&g_fd_class,
19903ba0c6b1SPoul-Henning Kamp 	    "fd%d", device_get_unit(fd->dev));
19911b67be7bSPoul-Henning Kamp 	fd->fd_provider = g_new_providerf(fd->fd_geom, fd->fd_geom->name);
19921b67be7bSPoul-Henning Kamp 	fd->fd_geom->softc = fd;
19931b67be7bSPoul-Henning Kamp 	g_error_provider(fd->fd_provider, 0);
19941b67be7bSPoul-Henning Kamp }
19951b67be7bSPoul-Henning Kamp 
19966182fdbdSPeter Wemm static int
19976182fdbdSPeter Wemm fd_attach(device_t dev)
19986182fdbdSPeter Wemm {
19996182fdbdSPeter Wemm 	struct	fd_data *fd;
20006182fdbdSPeter Wemm 
200164860614SJoerg Wunsch 	fd = device_get_softc(dev);
20021b67be7bSPoul-Henning Kamp 	g_post_event(fd_attach2, fd, M_WAITOK, NULL);
20031b67be7bSPoul-Henning Kamp 	fd->flags |= FD_EMPTY;
20041b67be7bSPoul-Henning Kamp 	bioq_init(&fd->fd_bq);
20051b67be7bSPoul-Henning Kamp 
20066182fdbdSPeter Wemm 	return (0);
20075b81b6b3SRodney W. Grimes }
20085b81b6b3SRodney W. Grimes 
20095f830ea2SJoerg Wunsch static int
20105f830ea2SJoerg Wunsch fd_detach(device_t dev)
20115f830ea2SJoerg Wunsch {
20125f830ea2SJoerg Wunsch 	struct	fd_data *fd;
20135f830ea2SJoerg Wunsch 
20145f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
20154b87c653SWarner Losh 	g_topology_lock();
20164b87c653SWarner Losh 	g_wither_geom(fd->fd_geom, ENXIO);
20174b87c653SWarner Losh 	g_topology_unlock();
20184b87c653SWarner Losh 	while (device_get_state(dev) == DS_BUSY)
20194b87c653SWarner Losh 		tsleep(fd, PZERO, "fdd", hz/10);
20201b67be7bSPoul-Henning Kamp 	callout_drain(&fd->toffhandle);
20215f830ea2SJoerg Wunsch 
20225f830ea2SJoerg Wunsch 	return (0);
20235f830ea2SJoerg Wunsch }
20245f830ea2SJoerg Wunsch 
202516e68fc6SPeter Wemm static device_method_t fd_methods[] = {
202616e68fc6SPeter Wemm 	/* Device interface */
202716e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
202816e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
20295f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
203016e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
203116e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
203216e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
203316e68fc6SPeter Wemm 	{ 0, 0 }
203416e68fc6SPeter Wemm };
203516e68fc6SPeter Wemm 
203616e68fc6SPeter Wemm static driver_t fd_driver = {
203716e68fc6SPeter Wemm 	"fd",
203816e68fc6SPeter Wemm 	fd_methods,
203916e68fc6SPeter Wemm 	sizeof(struct fd_data)
204016e68fc6SPeter Wemm };
204116e68fc6SPeter Wemm 
2042f8353edeSPoul-Henning Kamp static int
2043f8353edeSPoul-Henning Kamp fdc_modevent(module_t mod, int type, void *data)
2044f8353edeSPoul-Henning Kamp {
2045f8353edeSPoul-Henning Kamp 
2046f8353edeSPoul-Henning Kamp 	g_modevent(NULL, type, &g_fd_class);
2047f8353edeSPoul-Henning Kamp 	return (0);
2048f8353edeSPoul-Henning Kamp }
2049f8353edeSPoul-Henning Kamp 
2050f8353edeSPoul-Henning Kamp DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);
2051