xref: /freebsd/sys/dev/fdc/fdc.c (revision 734e3cc5fd306eec8013522bd10edd50a64ffc9f)
187f6c662SJulian Elischer /*
25b81b6b3SRodney W. Grimes  * Copyright (c) 1990 The Regents of the University of California.
35b81b6b3SRodney W. Grimes  * All rights reserved.
45b81b6b3SRodney W. Grimes  *
55b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
65b81b6b3SRodney W. Grimes  * Don Ahn.
75b81b6b3SRodney W. Grimes  *
869acd21dSWarner Losh  * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
969acd21dSWarner Losh  * aided by the Linux floppy driver modifications from David Bateman
1069acd21dSWarner Losh  * (dbateman@eng.uts.edu.au).
1169acd21dSWarner Losh  *
12dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994 by
133a2f7427SDavid Greenman  *  jc@irbs.UUCP (John Capo)
143a2f7427SDavid Greenman  *  vak@zebub.msk.su (Serge Vakulenko)
153a2f7427SDavid Greenman  *  ache@astral.msk.su (Andrew A. Chernov)
16dc16046fSJoerg Wunsch  *
17dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994, 1995 by
183a2f7427SDavid Greenman  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
19dc5df763SJoerg Wunsch  *  dufault@hda.com (Peter Dufault)
203a2f7427SDavid Greenman  *
212995d110SJoerg Wunsch  * Copyright (c) 2001 Joerg Wunsch,
2206740f7bSJoerg Wunsch  *  joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
232995d110SJoerg Wunsch  *
245b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
255b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
265b81b6b3SRodney W. Grimes  * are met:
275b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
285b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
295b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
305b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
315b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
3264860614SJoerg Wunsch  * 4. Neither the name of the University nor the names of its contributors
3364860614SJoerg Wunsch  *    may be used to endorse or promote products derived from this software
3464860614SJoerg Wunsch  *    without specific prior written permission.
355b81b6b3SRodney W. Grimes  *
365b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
375b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
385b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
395b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
405b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
415b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
425b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
435b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
445b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
455b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
465b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
475b81b6b3SRodney W. Grimes  *
48dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
495b81b6b3SRodney W. Grimes  */
505b81b6b3SRodney W. Grimes 
518c9bbf48SDavid E. O'Brien #include <sys/cdefs.h>
528c9bbf48SDavid E. O'Brien __FBSDID("$FreeBSD$");
538c9bbf48SDavid E. O'Brien 
54d2fb4892SJoerg Wunsch #include "opt_fdc.h"
555f830ea2SJoerg Wunsch #include "card.h"
565b81b6b3SRodney W. Grimes 
57b99f0a4aSAndrew Moore #include <sys/param.h>
58b99f0a4aSAndrew Moore #include <sys/systm.h>
599626b608SPoul-Henning Kamp #include <sys/bio.h>
606182fdbdSPeter Wemm #include <sys/bus.h>
616182fdbdSPeter Wemm #include <sys/conf.h>
62b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h>
63f90c382cSPoul-Henning Kamp #include <sys/disk.h>
646182fdbdSPeter Wemm #include <sys/fcntl.h>
65e774b251SJoerg Wunsch #include <sys/fdcio.h>
661a6bed68SJoerg Wunsch #include <sys/filio.h>
67fb919e4dSMark Murray #include <sys/kernel.h>
68fb919e4dSMark Murray #include <sys/lock.h>
69b99f0a4aSAndrew Moore #include <sys/malloc.h>
706182fdbdSPeter Wemm #include <sys/module.h>
71fb919e4dSMark Murray #include <sys/mutex.h>
723a2f7427SDavid Greenman #include <sys/proc.h>
73b99f0a4aSAndrew Moore #include <sys/syslog.h>
746182fdbdSPeter Wemm 
756182fdbdSPeter Wemm #include <machine/bus.h>
766182fdbdSPeter Wemm #include <sys/rman.h>
776182fdbdSPeter Wemm 
786182fdbdSPeter Wemm #include <machine/clock.h>
796182fdbdSPeter Wemm #include <machine/resource.h>
80dc5df763SJoerg Wunsch #include <machine/stdarg.h>
816182fdbdSPeter Wemm 
826182fdbdSPeter Wemm #include <isa/isavar.h>
83a97c75b7SDoug Rabson #include <isa/isareg.h>
84734e3cc5SWarner Losh #include <dev/fdc/fdcreg.h>
85a97c75b7SDoug Rabson #include <isa/rtc.h>
866182fdbdSPeter Wemm 
87246ed35dSJoerg Wunsch enum fdc_type
88246ed35dSJoerg Wunsch {
891a6bed68SJoerg Wunsch 	FDC_NE765, FDC_ENHANCED, FDC_UNKNOWN = -1
90246ed35dSJoerg Wunsch };
91246ed35dSJoerg Wunsch 
92246ed35dSJoerg Wunsch enum fdc_states {
93246ed35dSJoerg Wunsch 	DEVIDLE,
94246ed35dSJoerg Wunsch 	FINDWORK,
95246ed35dSJoerg Wunsch 	DOSEEK,
96246ed35dSJoerg Wunsch 	SEEKCOMPLETE ,
97246ed35dSJoerg Wunsch 	IOCOMPLETE,
98246ed35dSJoerg Wunsch 	RECALCOMPLETE,
99246ed35dSJoerg Wunsch 	STARTRECAL,
100246ed35dSJoerg Wunsch 	RESETCTLR,
101246ed35dSJoerg Wunsch 	SEEKWAIT,
102246ed35dSJoerg Wunsch 	RECALWAIT,
103246ed35dSJoerg Wunsch 	MOTORWAIT,
104246ed35dSJoerg Wunsch 	IOTIMEDOUT,
105246ed35dSJoerg Wunsch 	RESETCOMPLETE,
106246ed35dSJoerg Wunsch 	PIOREAD
107246ed35dSJoerg Wunsch };
108246ed35dSJoerg Wunsch 
109246ed35dSJoerg Wunsch #ifdef	FDC_DEBUG
110246ed35dSJoerg Wunsch static char const * const fdstates[] = {
111246ed35dSJoerg Wunsch 	"DEVIDLE",
112246ed35dSJoerg Wunsch 	"FINDWORK",
113246ed35dSJoerg Wunsch 	"DOSEEK",
114246ed35dSJoerg Wunsch 	"SEEKCOMPLETE",
115246ed35dSJoerg Wunsch 	"IOCOMPLETE",
116246ed35dSJoerg Wunsch 	"RECALCOMPLETE",
117246ed35dSJoerg Wunsch 	"STARTRECAL",
118246ed35dSJoerg Wunsch 	"RESETCTLR",
119246ed35dSJoerg Wunsch 	"SEEKWAIT",
120246ed35dSJoerg Wunsch 	"RECALWAIT",
121246ed35dSJoerg Wunsch 	"MOTORWAIT",
122246ed35dSJoerg Wunsch 	"IOTIMEDOUT",
123246ed35dSJoerg Wunsch 	"RESETCOMPLETE",
124246ed35dSJoerg Wunsch 	"PIOREAD"
125246ed35dSJoerg Wunsch };
126246ed35dSJoerg Wunsch #endif
127246ed35dSJoerg Wunsch 
128246ed35dSJoerg Wunsch /*
129246ed35dSJoerg Wunsch  * Per controller structure (softc).
130246ed35dSJoerg Wunsch  */
131246ed35dSJoerg Wunsch struct fdc_data
132246ed35dSJoerg Wunsch {
133246ed35dSJoerg Wunsch 	int	fdcu;		/* our unit number */
134246ed35dSJoerg Wunsch 	int	dmachan;
135246ed35dSJoerg Wunsch 	int	flags;
136246ed35dSJoerg Wunsch #define FDC_ATTACHED	0x01
137246ed35dSJoerg Wunsch #define FDC_STAT_VALID	0x08
138246ed35dSJoerg Wunsch #define FDC_HAS_FIFO	0x10
139246ed35dSJoerg Wunsch #define FDC_NEEDS_RESET	0x20
140246ed35dSJoerg Wunsch #define FDC_NODMA	0x40
141246ed35dSJoerg Wunsch #define FDC_ISPNP	0x80
142246ed35dSJoerg Wunsch #define FDC_ISPCMCIA	0x100
143246ed35dSJoerg Wunsch 	struct	fd_data *fd;
144246ed35dSJoerg Wunsch 	int	fdu;		/* the active drive	*/
145246ed35dSJoerg Wunsch 	enum	fdc_states state;
146246ed35dSJoerg Wunsch 	int	retry;
147246ed35dSJoerg Wunsch 	int	fdout;		/* mirror of the w/o digital output reg */
148246ed35dSJoerg Wunsch 	u_int	status[7];	/* copy of the registers */
149246ed35dSJoerg Wunsch 	enum	fdc_type fdct;	/* chip version of FDC */
150246ed35dSJoerg Wunsch 	int	fdc_errs;	/* number of logged errors */
151246ed35dSJoerg Wunsch 	int	dma_overruns;	/* number of DMA overruns */
152246ed35dSJoerg Wunsch 	struct	bio_queue_head head;
153246ed35dSJoerg Wunsch 	struct	bio *bp;	/* active buffer */
154246ed35dSJoerg Wunsch 	struct	resource *res_ioport, *res_ctl, *res_irq, *res_drq;
155246ed35dSJoerg Wunsch 	int	rid_ioport, rid_ctl, rid_irq, rid_drq;
156246ed35dSJoerg Wunsch 	int	port_off;
157246ed35dSJoerg Wunsch 	bus_space_tag_t portt;
158246ed35dSJoerg Wunsch 	bus_space_handle_t porth;
159246ed35dSJoerg Wunsch 	bus_space_tag_t ctlt;
160246ed35dSJoerg Wunsch 	bus_space_handle_t ctlh;
161246ed35dSJoerg Wunsch 	void	*fdc_intr;
162246ed35dSJoerg Wunsch 	struct	device *fdc_dev;
163246ed35dSJoerg Wunsch 	void	(*fdctl_wr)(struct fdc_data *fdc, u_int8_t v);
164246ed35dSJoerg Wunsch };
165246ed35dSJoerg Wunsch 
166419f39ceSPoul-Henning Kamp #define FDBIO_FORMAT	BIO_CMD2
167d306122dSPoul-Henning Kamp 
168246ed35dSJoerg Wunsch typedef int	fdu_t;
169246ed35dSJoerg Wunsch typedef int	fdcu_t;
170246ed35dSJoerg Wunsch typedef int	fdsu_t;
171246ed35dSJoerg Wunsch typedef	struct fd_data *fd_p;
172246ed35dSJoerg Wunsch typedef struct fdc_data *fdc_p;
173246ed35dSJoerg Wunsch typedef enum fdc_type fdc_t;
174246ed35dSJoerg Wunsch 
175246ed35dSJoerg Wunsch /*
176246ed35dSJoerg Wunsch  * fdc maintains a set (1!) of ivars per child of each controller.
177246ed35dSJoerg Wunsch  */
178246ed35dSJoerg Wunsch enum fdc_device_ivars {
179246ed35dSJoerg Wunsch 	FDC_IVAR_FDUNIT,
180246ed35dSJoerg Wunsch };
181246ed35dSJoerg Wunsch 
182246ed35dSJoerg Wunsch /*
183246ed35dSJoerg Wunsch  * Simple access macros for the ivars.
184246ed35dSJoerg Wunsch  */
185246ed35dSJoerg Wunsch #define FDC_ACCESSOR(A, B, T)						\
186246ed35dSJoerg Wunsch static __inline T fdc_get_ ## A(device_t dev)				\
187246ed35dSJoerg Wunsch {									\
188246ed35dSJoerg Wunsch 	uintptr_t v;							\
189246ed35dSJoerg Wunsch 	BUS_READ_IVAR(device_get_parent(dev), dev, FDC_IVAR_ ## B, &v);	\
190246ed35dSJoerg Wunsch 	return (T) v;							\
191246ed35dSJoerg Wunsch }
192246ed35dSJoerg Wunsch FDC_ACCESSOR(fdunit,	FDUNIT,	int)
193246ed35dSJoerg Wunsch 
1941a6bed68SJoerg Wunsch /* configuration flags for fdc */
195e34c71eaSJoerg Wunsch #define FDC_NO_FIFO	(1 << 2)	/* do not enable FIFO  */
1960722d6abSJoerg Wunsch 
197dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
198dc5df763SJoerg Wunsch #define FD_FAILED -1
199dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
200dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
2013fef646eSJoerg Wunsch /*
2023fef646eSJoerg Wunsch  * Stop retrying after this many DMA overruns.  Since each retry takes
20383edbfa5SJoerg Wunsch  * one revolution, with 300 rpm., 25 retries take approximately 5
2043fef646eSJoerg Wunsch  * seconds which the read attempt will block in case the DMA overrun
2053fef646eSJoerg Wunsch  * is persistent.
2063fef646eSJoerg Wunsch  */
2073fef646eSJoerg Wunsch #define FDC_DMAOV_MAX	25
208dc5df763SJoerg Wunsch 
2091a6bed68SJoerg Wunsch /*
210cb38bb6cSJoerg Wunsch  * Timeout value for the PIO loops to wait until the FDC main status
211cb38bb6cSJoerg Wunsch  * register matches our expectations (request for master, direction
212cb38bb6cSJoerg Wunsch  * bit).  This is supposed to be a number of microseconds, although
213cb38bb6cSJoerg Wunsch  * timing might actually not be very accurate.
214cb38bb6cSJoerg Wunsch  *
215cb38bb6cSJoerg Wunsch  * Timeouts of 100 msec are believed to be required for some broken
216cb38bb6cSJoerg Wunsch  * (old) hardware.
217cb38bb6cSJoerg Wunsch  */
218cb38bb6cSJoerg Wunsch #define	FDSTS_TIMEOUT	100000
219cb38bb6cSJoerg Wunsch 
220cb38bb6cSJoerg Wunsch /*
2211a6bed68SJoerg Wunsch  * Number of subdevices that can be used for different density types.
2221a6bed68SJoerg Wunsch  */
2231a6bed68SJoerg Wunsch #define NUMDENS		16
224b99f0a4aSAndrew Moore 
225419f39ceSPoul-Henning Kamp #define FDBIO_RDSECTID	BIO_CMD1
2267ca0641bSAndrey A. Chernov 
2271a6bed68SJoerg Wunsch /*
2281a6bed68SJoerg Wunsch  * List of native drive densities.  Order must match enum fd_drivetype
2291a6bed68SJoerg Wunsch  * in <sys/fdcio.h>.  Upon attaching the drive, each of the
2301a6bed68SJoerg Wunsch  * programmable subdevices is initialized with the native density
2311a6bed68SJoerg Wunsch  * definition.
2321a6bed68SJoerg Wunsch  */
2331a6bed68SJoerg Wunsch static struct fd_type fd_native_types[] =
2345b81b6b3SRodney W. Grimes {
2351a6bed68SJoerg Wunsch { 0 },				/* FDT_NONE */
2361a6bed68SJoerg Wunsch {  9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */
2371a6bed68SJoerg Wunsch { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M  */
2381a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */
2391a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
2401a6bed68SJoerg Wunsch #if 0				/* we currently don't handle 2.88 MB */
2411a6bed68SJoerg Wunsch { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS,  2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/
2421a6bed68SJoerg Wunsch #else
2431a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
2441a6bed68SJoerg Wunsch #endif
2455b81b6b3SRodney W. Grimes };
2465b81b6b3SRodney W. Grimes 
2471a6bed68SJoerg Wunsch /*
2481a6bed68SJoerg Wunsch  * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density
2491a6bed68SJoerg Wunsch  * selection, they just start out with their native density (or lose).
2501a6bed68SJoerg Wunsch  * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their
2511a6bed68SJoerg Wunsch  * respective lists of densities to search for.
2521a6bed68SJoerg Wunsch  */
2531a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_12m[] = {
2541a6bed68SJoerg Wunsch { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */
2551a6bed68SJoerg Wunsch {  9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */
2561a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2571a6bed68SJoerg Wunsch };
2581a6bed68SJoerg Wunsch 
2591a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_144m[] = {
2601a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
2611a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2621a6bed68SJoerg Wunsch };
2631a6bed68SJoerg Wunsch 
2641a6bed68SJoerg Wunsch /* We search for 1.44M first since this is the most common case. */
2651a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_288m[] = {
2661a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
2671a6bed68SJoerg Wunsch #if 0
2681a6bed68SJoerg Wunsch { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS,  2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */
2691a6bed68SJoerg Wunsch #endif
2701a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2711a6bed68SJoerg Wunsch };
272dc16046fSJoerg Wunsch 
273f8ce7dd5SJoerg Wunsch #define MAX_SEC_SIZE	(128 << 3)
27464860614SJoerg Wunsch #define MAX_CYLINDER	85	/* some people really stress their drives
27564860614SJoerg Wunsch 				 * up to cyl 82 */
276250300ebSJoerg Wunsch #define MAX_HEAD	1
277f8ce7dd5SJoerg Wunsch 
2786182fdbdSPeter Wemm static devclass_t fdc_devclass;
2795b81b6b3SRodney W. Grimes 
280246ed35dSJoerg Wunsch /*
281246ed35dSJoerg Wunsch  * Per drive structure (softc).
282246ed35dSJoerg Wunsch  */
2836182fdbdSPeter Wemm struct fd_data {
284b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2855b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2861a6bed68SJoerg Wunsch 	enum	fd_drivetype type; /* drive type */
2871a6bed68SJoerg Wunsch 	struct	fd_type *ft;	/* pointer to current type descriptor */
2881a6bed68SJoerg Wunsch 	struct	fd_type fts[NUMDENS]; /* type descriptors */
2895b81b6b3SRodney W. Grimes 	int	flags;
2905b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
2911a6bed68SJoerg Wunsch #define	FD_NONBLOCK	0x02	/* O_NONBLOCK set	*/
2921a6bed68SJoerg Wunsch #define	FD_ACTIVE	0x04	/* it's active		*/
2931a6bed68SJoerg Wunsch #define	FD_MOTOR	0x08	/* motor should be on	*/
2941a6bed68SJoerg Wunsch #define	FD_MOTOR_WAIT	0x10	/* motor coming up	*/
2951a6bed68SJoerg Wunsch #define	FD_UA		0x20	/* force unit attention */
2965b81b6b3SRodney W. Grimes 	int	skip;
2975b81b6b3SRodney W. Grimes 	int	hddrv;
298dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
2995b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
3001a6bed68SJoerg Wunsch 	int	options;	/* user configurable options, see fdcio.h */
30102a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
30202a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
30380980460SPoul-Henning Kamp 	struct	devstat *device_stats;
304e219897aSJoerg Wunsch 	dev_t	masterdev;
3056182fdbdSPeter Wemm 	device_t dev;
3066182fdbdSPeter Wemm 	fdu_t	fdu;
3076182fdbdSPeter Wemm };
30837286586SPeter Wemm 
30937286586SPeter Wemm struct fdc_ivars {
31037286586SPeter Wemm 	int	fdunit;
31137286586SPeter Wemm };
3126182fdbdSPeter Wemm static devclass_t fd_devclass;
3135b81b6b3SRodney W. Grimes 
3141a6bed68SJoerg Wunsch /* configuration flags for fd */
3151a6bed68SJoerg Wunsch #define FD_TYPEMASK	0x0f	/* drive type, matches enum
3161a6bed68SJoerg Wunsch 				 * fd_drivetype; on i386 machines, if
3171a6bed68SJoerg Wunsch 				 * given as 0, use RTC type for fd0
3181a6bed68SJoerg Wunsch 				 * and fd1 */
3191a6bed68SJoerg Wunsch #define FD_DTYPE(flags)	((flags) & FD_TYPEMASK)
3201a6bed68SJoerg Wunsch #define FD_NO_CHLINE	0x10	/* drive does not support changeline
3211a6bed68SJoerg Wunsch 				 * aka. unit attention */
3221a6bed68SJoerg Wunsch #define FD_NO_PROBE	0x20	/* don't probe drive (seek test), just
3231a6bed68SJoerg Wunsch 				 * assume it is there */
3241a6bed68SJoerg Wunsch 
325246ed35dSJoerg Wunsch /*
326246ed35dSJoerg Wunsch  * Throughout this file the following conventions will be used:
327246ed35dSJoerg Wunsch  *
328246ed35dSJoerg Wunsch  * fd is a pointer to the fd_data struct for the drive in question
329246ed35dSJoerg Wunsch  * fdc is a pointer to the fdc_data struct for the controller
330246ed35dSJoerg Wunsch  * fdu is the floppy drive unit number
331246ed35dSJoerg Wunsch  * fdcu is the floppy controller unit number
332246ed35dSJoerg Wunsch  * fdsu is the floppy drive unit number on that controller. (sub-unit)
333246ed35dSJoerg Wunsch  */
334b99f0a4aSAndrew Moore 
335246ed35dSJoerg Wunsch /*
336246ed35dSJoerg Wunsch  * Function declarations, same (chaotic) order as they appear in the
337246ed35dSJoerg Wunsch  * file.  Re-ordering is too late now, it would only obfuscate the
338246ed35dSJoerg Wunsch  * diffs against old and offspring versions (like the PC98 one).
339246ed35dSJoerg Wunsch  *
340246ed35dSJoerg Wunsch  * Anyone adding functions here, please keep this sequence the same
341246ed35dSJoerg Wunsch  * as below -- makes locating a particular function in the body much
342246ed35dSJoerg Wunsch  * easier.
343246ed35dSJoerg Wunsch  */
344246ed35dSJoerg Wunsch static void fdout_wr(fdc_p, u_int8_t);
345246ed35dSJoerg Wunsch static u_int8_t fdsts_rd(fdc_p);
346246ed35dSJoerg Wunsch static void fddata_wr(fdc_p, u_int8_t);
347246ed35dSJoerg Wunsch static u_int8_t fddata_rd(fdc_p);
348246ed35dSJoerg Wunsch static void fdctl_wr_isa(fdc_p, u_int8_t);
349246ed35dSJoerg Wunsch #if NCARD > 0
350246ed35dSJoerg Wunsch static void fdctl_wr_pcmcia(fdc_p, u_int8_t);
351246ed35dSJoerg Wunsch #endif
352246ed35dSJoerg Wunsch #if 0
353246ed35dSJoerg Wunsch static u_int8_t fdin_rd(fdc_p);
354246ed35dSJoerg Wunsch #endif
355246ed35dSJoerg Wunsch static int fdc_err(struct fdc_data *, const char *);
356246ed35dSJoerg Wunsch static int fd_cmd(struct fdc_data *, int, ...);
357246ed35dSJoerg Wunsch static int enable_fifo(fdc_p fdc);
358246ed35dSJoerg Wunsch static int fd_sense_drive_status(fdc_p, int *);
359246ed35dSJoerg Wunsch static int fd_sense_int(fdc_p, int *, int *);
360246ed35dSJoerg Wunsch static int fd_read_status(fdc_p);
361246ed35dSJoerg Wunsch static int fdc_alloc_resources(struct fdc_data *);
362246ed35dSJoerg Wunsch static void fdc_release_resources(struct fdc_data *);
363246ed35dSJoerg Wunsch static int fdc_read_ivar(device_t, device_t, int, uintptr_t *);
364246ed35dSJoerg Wunsch static int fdc_probe(device_t);
365246ed35dSJoerg Wunsch #if NCARD > 0
366246ed35dSJoerg Wunsch static int fdc_pccard_probe(device_t);
367246ed35dSJoerg Wunsch #endif
368246ed35dSJoerg Wunsch static int fdc_detach(device_t dev);
369246ed35dSJoerg Wunsch static void fdc_add_child(device_t, const char *, int);
370246ed35dSJoerg Wunsch static int fdc_attach(device_t);
371246ed35dSJoerg Wunsch static int fdc_print_child(device_t, device_t);
372246ed35dSJoerg Wunsch static int fd_probe(device_t);
373246ed35dSJoerg Wunsch static int fd_attach(device_t);
374246ed35dSJoerg Wunsch static int fd_detach(device_t);
3756182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int);
3763a2f7427SDavid Greenman #  define TURNON 1
3773a2f7427SDavid Greenman #  define TURNOFF 0
3783a2f7427SDavid Greenman static timeout_t fd_turnoff;
3793a2f7427SDavid Greenman static timeout_t fd_motor_on;
3806182fdbdSPeter Wemm static void fd_turnon(struct fd_data *);
3813a2f7427SDavid Greenman static void fdc_reset(fdc_p);
3826182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *);
38380909a7dSJoerg Wunsch static int out_fdc(struct fdc_data *, int);
384246ed35dSJoerg Wunsch /*
385f967c4f9SPoul-Henning Kamp  * The open function is named fdopen() to avoid confusion with fdopen()
386246ed35dSJoerg Wunsch  * in fd(4).  The difference is now only meaningful for debuggers.
387246ed35dSJoerg Wunsch  */
388f967c4f9SPoul-Henning Kamp static	d_open_t	fdopen;
389246ed35dSJoerg Wunsch static	d_close_t	fdclose;
390246ed35dSJoerg Wunsch static	d_strategy_t	fdstrategy;
3916182fdbdSPeter Wemm static void fdstart(struct fdc_data *);
3925c1a1eaeSBruce Evans static timeout_t fd_iotimeout;
3933a2f7427SDavid Greenman static timeout_t fd_pseudointr;
394246ed35dSJoerg Wunsch static driver_intr_t fdc_intr;
395246ed35dSJoerg Wunsch static int fdcpio(fdc_p, long, caddr_t, u_int);
3961a6bed68SJoerg Wunsch static int fdautoselect(dev_t);
3976182fdbdSPeter Wemm static int fdstate(struct fdc_data *);
3986182fdbdSPeter Wemm static int retrier(struct fdc_data *);
399246ed35dSJoerg Wunsch static void fdbiodone(struct bio *);
400f664aeeeSJoerg Wunsch static int fdmisccmd(dev_t, u_int, void *);
401246ed35dSJoerg Wunsch static	d_ioctl_t	fdioctl;
402d66c374fSTor Egge 
403d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
404d66c374fSTor Egge 
405d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
4063a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
407cba2a7c6SBruce Evans static int volatile fd_debug = 0;
4080e17a5bcSJoerg Wunsch #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0)
4090e17a5bcSJoerg Wunsch #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0)
410d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
4110e17a5bcSJoerg Wunsch #define TRACE0(arg) do { } while (0)
4120e17a5bcSJoerg Wunsch #define TRACE1(arg1, arg2) do { } while (0)
413d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
4145b81b6b3SRodney W. Grimes 
415246ed35dSJoerg Wunsch /*
416246ed35dSJoerg Wunsch  * Bus space handling (access to low-level IO).
417246ed35dSJoerg Wunsch  */
418427ccf00SDoug Rabson static void
419427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v)
420427ccf00SDoug Rabson {
421427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
422427ccf00SDoug Rabson }
423427ccf00SDoug Rabson 
424427ccf00SDoug Rabson static u_int8_t
425427ccf00SDoug Rabson fdsts_rd(fdc_p fdc)
426427ccf00SDoug Rabson {
427427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
428427ccf00SDoug Rabson }
429427ccf00SDoug Rabson 
430427ccf00SDoug Rabson static void
431427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v)
432427ccf00SDoug Rabson {
433427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
434427ccf00SDoug Rabson }
435427ccf00SDoug Rabson 
436427ccf00SDoug Rabson static u_int8_t
437427ccf00SDoug Rabson fddata_rd(fdc_p fdc)
438427ccf00SDoug Rabson {
439427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
440427ccf00SDoug Rabson }
441427ccf00SDoug Rabson 
442427ccf00SDoug Rabson static void
4435f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v)
444427ccf00SDoug Rabson {
445427ccf00SDoug Rabson 	bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
446427ccf00SDoug Rabson }
447427ccf00SDoug Rabson 
44858c9d623SPeter Wemm #if NCARD > 0
4495f830ea2SJoerg Wunsch static void
4505f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
4515f830ea2SJoerg Wunsch {
4525f830ea2SJoerg Wunsch 	bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
4535f830ea2SJoerg Wunsch }
45458c9d623SPeter Wemm #endif
4555f830ea2SJoerg Wunsch 
456427ccf00SDoug Rabson static u_int8_t
457427ccf00SDoug Rabson fdin_rd(fdc_p fdc)
458427ccf00SDoug Rabson {
459427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
460427ccf00SDoug Rabson }
461427ccf00SDoug Rabson 
4624e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = {
463dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
464f967c4f9SPoul-Henning Kamp 	.d_open =	fdopen,
4657ac40f5fSPoul-Henning Kamp 	.d_close =	fdclose,
4667ac40f5fSPoul-Henning Kamp 	.d_read =	physread,
4677ac40f5fSPoul-Henning Kamp 	.d_write =	physwrite,
4687ac40f5fSPoul-Henning Kamp 	.d_ioctl =	fdioctl,
4697ac40f5fSPoul-Henning Kamp 	.d_strategy =	fdstrategy,
4707ac40f5fSPoul-Henning Kamp 	.d_name =	"fd",
471dc08ffecSPoul-Henning Kamp 	.d_flags =	D_DISK | D_NEEDGIANT,
4724e2f199eSPoul-Henning Kamp };
4734e2f199eSPoul-Henning Kamp 
474246ed35dSJoerg Wunsch /*
475246ed35dSJoerg Wunsch  * Auxiliary functions.  Well, some only.  Others are scattered
476246ed35dSJoerg Wunsch  * throughout the entire file.
477246ed35dSJoerg Wunsch  */
478dc5df763SJoerg Wunsch static int
4796182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
480dc5df763SJoerg Wunsch {
4816182fdbdSPeter Wemm 	fdc->fdc_errs++;
48216b04b6aSJoerg Wunsch 	if (s) {
483b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
484b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
485b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
486b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
487b6e5f28eSPeter Wemm 						    "logging any more\n");
48816b04b6aSJoerg Wunsch 	}
489dc5df763SJoerg Wunsch 
490dc5df763SJoerg Wunsch 	return FD_FAILED;
491dc5df763SJoerg Wunsch }
492dc5df763SJoerg Wunsch 
493dc5df763SJoerg Wunsch /*
494dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
495dc5df763SJoerg Wunsch  * Unit number,
496dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
497dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
498dc5df763SJoerg Wunsch  */
4996f4e0bebSPoul-Henning Kamp static int
5006182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...)
501dc5df763SJoerg Wunsch {
502dc5df763SJoerg Wunsch 	u_char cmd;
503dc5df763SJoerg Wunsch 	int n_in;
504dc5df763SJoerg Wunsch 	int n;
505dc5df763SJoerg Wunsch 	va_list ap;
506dc5df763SJoerg Wunsch 
507dc5df763SJoerg Wunsch 	va_start(ap, n_out);
508dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
509dc5df763SJoerg Wunsch 	va_end(ap);
510dc5df763SJoerg Wunsch 	va_start(ap, n_out);
511dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
512dc5df763SJoerg Wunsch 	{
5136182fdbdSPeter Wemm 		if (out_fdc(fdc, va_arg(ap, int)) < 0)
514dc5df763SJoerg Wunsch 		{
515dc5df763SJoerg Wunsch 			char msg[50];
5162127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
517dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
518dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
5196182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
520dc5df763SJoerg Wunsch 		}
521dc5df763SJoerg Wunsch 	}
522dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
523dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
524dc5df763SJoerg Wunsch 	{
525dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
5266182fdbdSPeter Wemm 		if (fd_in(fdc, ptr) < 0)
527dc5df763SJoerg Wunsch 		{
528dc5df763SJoerg Wunsch 			char msg[50];
5292127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
530dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
531dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
5326182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
533dc5df763SJoerg Wunsch 		}
534dc5df763SJoerg Wunsch 	}
535dc5df763SJoerg Wunsch 
536dc5df763SJoerg Wunsch 	return 0;
537dc5df763SJoerg Wunsch }
538dc5df763SJoerg Wunsch 
5396f4e0bebSPoul-Henning Kamp static int
540d66c374fSTor Egge enable_fifo(fdc_p fdc)
541d66c374fSTor Egge {
542d66c374fSTor Egge 	int i, j;
543d66c374fSTor Egge 
544d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
545d66c374fSTor Egge 
546d66c374fSTor Egge 		/*
547d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
548d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
549d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
550d66c374fSTor Egge 		 */
551d66c374fSTor Egge 
5526182fdbdSPeter Wemm 		if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
5536182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
554d66c374fSTor Egge 
555d66c374fSTor Egge 		/* If command is invalid, return */
556d0900d6bSJoerg Wunsch 		j = FDSTS_TIMEOUT;
557427ccf00SDoug Rabson 		while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
558d0900d6bSJoerg Wunsch 		       != NE7_RQM && j-- > 0) {
559d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
560d66c374fSTor Egge 				fdc_reset(fdc);
561d66c374fSTor Egge 				return FD_FAILED;
562d66c374fSTor Egge 			}
563d0900d6bSJoerg Wunsch 			DELAY(1);
564d0900d6bSJoerg Wunsch 		}
565d66c374fSTor Egge 		if (j<0 ||
5666182fdbdSPeter Wemm 		    fd_cmd(fdc, 3,
567d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
568d66c374fSTor Egge 			fdc_reset(fdc);
5696182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
570d66c374fSTor Egge 		}
571d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
572d66c374fSTor Egge 		return 0;
573d66c374fSTor Egge 	}
5746182fdbdSPeter Wemm 	if (fd_cmd(fdc, 4,
575d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
5766182fdbdSPeter Wemm 		return fdc_err(fdc, "Re-enable FIFO failed\n");
577d66c374fSTor Egge 	return 0;
578d66c374fSTor Egge }
579d66c374fSTor Egge 
580d66c374fSTor Egge static int
581dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
582dc5df763SJoerg Wunsch {
583dc5df763SJoerg Wunsch 	int st3;
584dc5df763SJoerg Wunsch 
5856182fdbdSPeter Wemm 	if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
586dc5df763SJoerg Wunsch 	{
5876182fdbdSPeter Wemm 		return fdc_err(fdc, "Sense Drive Status failed\n");
588dc5df763SJoerg Wunsch 	}
589dc5df763SJoerg Wunsch 	if (st3p)
590dc5df763SJoerg Wunsch 		*st3p = st3;
591dc5df763SJoerg Wunsch 
592dc5df763SJoerg Wunsch 	return 0;
593dc5df763SJoerg Wunsch }
594dc5df763SJoerg Wunsch 
5956f4e0bebSPoul-Henning Kamp static int
596dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
597dc5df763SJoerg Wunsch {
5986182fdbdSPeter Wemm 	int cyl, st0, ret;
599dc5df763SJoerg Wunsch 
6006182fdbdSPeter Wemm 	ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
6016182fdbdSPeter Wemm 	if (ret) {
6026182fdbdSPeter Wemm 		(void)fdc_err(fdc,
603dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
604dc5df763SJoerg Wunsch 		return ret;
605dc5df763SJoerg Wunsch 	}
606dc5df763SJoerg Wunsch 
607dc5df763SJoerg Wunsch 	if (st0p)
608dc5df763SJoerg Wunsch 		*st0p = st0;
609dc5df763SJoerg Wunsch 
6106182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
611dc5df763SJoerg Wunsch 		/*
612dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
613dc5df763SJoerg Wunsch 		 */
614dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
615dc5df763SJoerg Wunsch 	}
616dc5df763SJoerg Wunsch 
6176182fdbdSPeter Wemm 	if (fd_in(fdc, &cyl) < 0) {
6186182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
619dc5df763SJoerg Wunsch 	}
620dc5df763SJoerg Wunsch 
621dc5df763SJoerg Wunsch 	if (cylp)
622dc5df763SJoerg Wunsch 		*cylp = cyl;
623dc5df763SJoerg Wunsch 
624dc5df763SJoerg Wunsch 	return 0;
625dc5df763SJoerg Wunsch }
626dc5df763SJoerg Wunsch 
627dc5df763SJoerg Wunsch 
6286f4e0bebSPoul-Henning Kamp static int
62964860614SJoerg Wunsch fd_read_status(fdc_p fdc)
630dc5df763SJoerg Wunsch {
631dc5df763SJoerg Wunsch 	int i, ret;
632b5e8ce9fSBruce Evans 
633246ed35dSJoerg Wunsch 	for (i = ret = 0; i < 7; i++) {
634b5e8ce9fSBruce Evans 		/*
63564860614SJoerg Wunsch 		 * XXX types are poorly chosen.  Only bytes can be read
636a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
637b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
638b5e8ce9fSBruce Evans 		 */
639b5e8ce9fSBruce Evans 		int status;
640b5e8ce9fSBruce Evans 
6416182fdbdSPeter Wemm 		ret = fd_in(fdc, &status);
642b5e8ce9fSBruce Evans 		fdc->status[i] = status;
643b5e8ce9fSBruce Evans 		if (ret != 0)
644dc5df763SJoerg Wunsch 			break;
645dc5df763SJoerg Wunsch 	}
646dc5df763SJoerg Wunsch 
647dc5df763SJoerg Wunsch 	if (ret == 0)
648dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
649dc5df763SJoerg Wunsch 	else
650dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
651dc5df763SJoerg Wunsch 
652dc5df763SJoerg Wunsch 	return ret;
653dc5df763SJoerg Wunsch }
654dc5df763SJoerg Wunsch 
6553a2f7427SDavid Greenman static int
65637286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc)
6575b81b6b3SRodney W. Grimes {
65837286586SPeter Wemm 	device_t dev;
659e219897aSJoerg Wunsch 	int ispnp, ispcmcia, nports;
6605b81b6b3SRodney W. Grimes 
66137286586SPeter Wemm 	dev = fdc->fdc_dev;
6625f830ea2SJoerg Wunsch 	ispnp = (fdc->flags & FDC_ISPNP) != 0;
6635f830ea2SJoerg Wunsch 	ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
6646182fdbdSPeter Wemm 	fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
6656182fdbdSPeter Wemm 	fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
66664746d06SNate Lawson 	fdc->rid_ctl = 1;
6676182fdbdSPeter Wemm 
668b6e5f28eSPeter Wemm 	/*
6695f830ea2SJoerg Wunsch 	 * On standard ISA, we don't just use an 8 port range
6705f830ea2SJoerg Wunsch 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
6715f830ea2SJoerg Wunsch 	 * register at 0x3f6.
6725f830ea2SJoerg Wunsch 	 *
673b6e5f28eSPeter Wemm 	 * Isn't PC hardware wonderful.
6745f830ea2SJoerg Wunsch 	 *
6755f830ea2SJoerg Wunsch 	 * The Y-E Data PCMCIA FDC doesn't have this problem, it
6765f830ea2SJoerg Wunsch 	 * uses the register with offset 6 for pseudo-DMA, and the
6775f830ea2SJoerg Wunsch 	 * one with offset 7 as control register.
678b6e5f28eSPeter Wemm 	 */
679e219897aSJoerg Wunsch 	nports = ispcmcia ? 8 : (ispnp ? 1 : 6);
68064746d06SNate Lawson 
68164746d06SNate Lawson 	/*
68264746d06SNate Lawson 	 * Some ACPI BIOSen have _CRS objects for the floppy device that
68364746d06SNate Lawson 	 * split the I/O port resource into several resources.  We detect
68464746d06SNate Lawson 	 * this case by checking if there are more than 2 IOPORT resources.
68564746d06SNate Lawson 	 * If so, we use the resource with the smallest start address as
68664746d06SNate Lawson 	 * the port RID and the largest start address as the control RID.
68764746d06SNate Lawson 	 */
68864746d06SNate Lawson 	if (bus_get_resource_count(dev, SYS_RES_IOPORT, 2) != 0) {
68964746d06SNate Lawson 		u_long min_start, max_start, tmp;
69064746d06SNate Lawson 		int i;
69164746d06SNate Lawson 
69264746d06SNate Lawson 		/* Find the min/max start addresses and their RIDs. */
69364746d06SNate Lawson 		max_start = 0ul;
69464746d06SNate Lawson 		min_start = ~0ul;
69564746d06SNate Lawson 		for (i = 0; bus_get_resource_count(dev, SYS_RES_IOPORT, i) > 0;
69664746d06SNate Lawson 		    i++) {
69764746d06SNate Lawson 			tmp = bus_get_resource_start(dev, SYS_RES_IOPORT, i);
69864746d06SNate Lawson 			KASSERT(tmp != 0, ("bogus resource"));
69964746d06SNate Lawson 			if (tmp < min_start) {
70064746d06SNate Lawson 				min_start = tmp;
70164746d06SNate Lawson 				fdc->rid_ioport = i;
70264746d06SNate Lawson 			}
70364746d06SNate Lawson 			if (tmp > max_start) {
70464746d06SNate Lawson 				max_start = tmp;
70564746d06SNate Lawson 				fdc->rid_ctl = i;
70664746d06SNate Lawson 			}
70764746d06SNate Lawson 		}
70864746d06SNate Lawson 		if (min_start + 7 != max_start) {
70964746d06SNate Lawson 			device_printf(dev, "I/O to control range incorrect\n");
71064746d06SNate Lawson 			return (ENXIO);
71164746d06SNate Lawson 		}
71264746d06SNate Lawson 	}
71364746d06SNate Lawson 
7146182fdbdSPeter Wemm 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
7156182fdbdSPeter Wemm 					     &fdc->rid_ioport, 0ul, ~0ul,
716e219897aSJoerg Wunsch 					     nports, RF_ACTIVE);
7176182fdbdSPeter Wemm 	if (fdc->res_ioport == 0) {
718e219897aSJoerg Wunsch 		device_printf(dev, "cannot reserve I/O port range (%d ports)\n",
719e219897aSJoerg Wunsch 			      nports);
72037286586SPeter Wemm 		return ENXIO;
7216182fdbdSPeter Wemm 	}
722427ccf00SDoug Rabson 	fdc->portt = rman_get_bustag(fdc->res_ioport);
723427ccf00SDoug Rabson 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
724427ccf00SDoug Rabson 
7255f830ea2SJoerg Wunsch 	if (!ispcmcia) {
726427ccf00SDoug Rabson 		/*
7275f830ea2SJoerg Wunsch 		 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
7285f830ea2SJoerg Wunsch 		 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
7295f830ea2SJoerg Wunsch 		 * by checking the size and adjust the port address
7305f830ea2SJoerg Wunsch 		 * accordingly.
731b6e5f28eSPeter Wemm 		 */
732b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
733b6e5f28eSPeter Wemm 			fdc->port_off = -2;
734b6e5f28eSPeter Wemm 
735b6e5f28eSPeter Wemm 		/*
7365f830ea2SJoerg Wunsch 		 * Register the control port range as rid 1 if it
7375f830ea2SJoerg Wunsch 		 * isn't there already. Most PnP BIOSen will have
7385f830ea2SJoerg Wunsch 		 * already done this but non-PnP configurations don't.
739a2639a18SPeter Wemm 		 *
7405f830ea2SJoerg Wunsch 		 * And some (!!) report 0x3f2-0x3f5 and completely
7415f830ea2SJoerg Wunsch 		 * leave out the control register!  It seems that some
7425f830ea2SJoerg Wunsch 		 * non-antique controller chips have a different
7435f830ea2SJoerg Wunsch 		 * method of programming the transfer speed which
7445f830ea2SJoerg Wunsch 		 * doesn't require the control register, but it's
7455f830ea2SJoerg Wunsch 		 * mighty bogus as the chip still responds to the
7465f830ea2SJoerg Wunsch 		 * address for the control register.
747427ccf00SDoug Rabson 		 */
748b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
749b9da888fSPeter Wemm 			u_long ctlstart;
750b9da888fSPeter Wemm 
751b6e5f28eSPeter Wemm 			/* Find the control port, usually 0x3f7 */
7525f830ea2SJoerg Wunsch 			ctlstart = rman_get_start(fdc->res_ioport) +
7535f830ea2SJoerg Wunsch 				fdc->port_off + 7;
754b6e5f28eSPeter Wemm 
755b6e5f28eSPeter Wemm 			bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
756b9da888fSPeter Wemm 		}
757b6e5f28eSPeter Wemm 
758b6e5f28eSPeter Wemm 		/*
759b6e5f28eSPeter Wemm 		 * Now (finally!) allocate the control port.
760b6e5f28eSPeter Wemm 		 */
7615f96beb9SNate Lawson 		fdc->res_ctl = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
7625f96beb9SNate Lawson 						      &fdc->rid_ctl, RF_ACTIVE);
763427ccf00SDoug Rabson 		if (fdc->res_ctl == 0) {
7645f830ea2SJoerg Wunsch 			device_printf(dev,
765e219897aSJoerg Wunsch 		"cannot reserve control I/O port range (control port)\n");
76637286586SPeter Wemm 			return ENXIO;
767427ccf00SDoug Rabson 		}
768427ccf00SDoug Rabson 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
769427ccf00SDoug Rabson 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
7705f830ea2SJoerg Wunsch 	}
7716182fdbdSPeter Wemm 
7725f96beb9SNate Lawson 	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
7735f96beb9SNate Lawson 					      &fdc->rid_irq, RF_ACTIVE);
7746182fdbdSPeter Wemm 	if (fdc->res_irq == 0) {
775427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve interrupt line\n");
77637286586SPeter Wemm 		return ENXIO;
7776182fdbdSPeter Wemm 	}
7785f830ea2SJoerg Wunsch 
7795f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
7805f96beb9SNate Lawson 		fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
7815f96beb9SNate Lawson 						      &fdc->rid_drq,
7826182fdbdSPeter Wemm 						      RF_ACTIVE);
7836182fdbdSPeter Wemm 		if (fdc->res_drq == 0) {
784427ccf00SDoug Rabson 			device_printf(dev, "cannot reserve DMA request line\n");
78537286586SPeter Wemm 			return ENXIO;
7866182fdbdSPeter Wemm 		}
7876182fdbdSPeter Wemm 		fdc->dmachan = fdc->res_drq->r_start;
7885f830ea2SJoerg Wunsch 	}
78937286586SPeter Wemm 
79037286586SPeter Wemm 	return 0;
79137286586SPeter Wemm }
79237286586SPeter Wemm 
79337286586SPeter Wemm static void
79437286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
79537286586SPeter Wemm {
79637286586SPeter Wemm 	device_t dev;
79737286586SPeter Wemm 
79837286586SPeter Wemm 	dev = fdc->fdc_dev;
79937286586SPeter Wemm 	if (fdc->res_irq != 0) {
80037286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
80137286586SPeter Wemm 					fdc->res_irq);
80237286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
80337286586SPeter Wemm 				     fdc->res_irq);
80437286586SPeter Wemm 	}
80537286586SPeter Wemm 	if (fdc->res_ctl != 0) {
80637286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
80737286586SPeter Wemm 					fdc->res_ctl);
80837286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
80937286586SPeter Wemm 				     fdc->res_ctl);
81037286586SPeter Wemm 	}
81137286586SPeter Wemm 	if (fdc->res_ioport != 0) {
81237286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
81337286586SPeter Wemm 					fdc->res_ioport);
81437286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
81537286586SPeter Wemm 				     fdc->res_ioport);
81637286586SPeter Wemm 	}
81737286586SPeter Wemm 	if (fdc->res_drq != 0) {
81837286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
81937286586SPeter Wemm 					fdc->res_drq);
82037286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
82137286586SPeter Wemm 				     fdc->res_drq);
82237286586SPeter Wemm 	}
82337286586SPeter Wemm }
82437286586SPeter Wemm 
825246ed35dSJoerg Wunsch /*
826246ed35dSJoerg Wunsch  * Configuration/initialization stuff, per controller.
827246ed35dSJoerg Wunsch  */
82837286586SPeter Wemm 
82937286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = {
83037286586SPeter Wemm 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
83137286586SPeter Wemm 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
83237286586SPeter Wemm 	{0}
83337286586SPeter Wemm };
83437286586SPeter Wemm 
83537286586SPeter Wemm static int
8363aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
83737286586SPeter Wemm {
83837286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
83937286586SPeter Wemm 
84037286586SPeter Wemm 	switch (which) {
84137286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
84237286586SPeter Wemm 		*result = ivars->fdunit;
84337286586SPeter Wemm 		break;
84437286586SPeter Wemm 	default:
84537286586SPeter Wemm 		return ENOENT;
84637286586SPeter Wemm 	}
84737286586SPeter Wemm 	return 0;
84837286586SPeter Wemm }
84937286586SPeter Wemm 
85037286586SPeter Wemm static int
85137286586SPeter Wemm fdc_probe(device_t dev)
85237286586SPeter Wemm {
85337286586SPeter Wemm 	int	error, ic_type;
85437286586SPeter Wemm 	struct	fdc_data *fdc;
85537286586SPeter Wemm 
85637286586SPeter Wemm 	fdc = device_get_softc(dev);
85737286586SPeter Wemm 	bzero(fdc, sizeof *fdc);
85837286586SPeter Wemm 	fdc->fdc_dev = dev;
8595f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_isa;
86037286586SPeter Wemm 
86137286586SPeter Wemm 	/* Check pnp ids */
86237286586SPeter Wemm 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
86337286586SPeter Wemm 	if (error == ENXIO)
86437286586SPeter Wemm 		return ENXIO;
8655f830ea2SJoerg Wunsch 	if (error == 0)
8665f830ea2SJoerg Wunsch 		fdc->flags |= FDC_ISPNP;
86737286586SPeter Wemm 
86837286586SPeter Wemm 	/* Attempt to allocate our resources for the duration of the probe */
86937286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
87037286586SPeter Wemm 	if (error)
87137286586SPeter Wemm 		goto out;
8725b81b6b3SRodney W. Grimes 
87316111cedSAndrew Moore 	/* First - lets reset the floppy controller */
874427ccf00SDoug Rabson 	fdout_wr(fdc, 0);
87516111cedSAndrew Moore 	DELAY(100);
876427ccf00SDoug Rabson 	fdout_wr(fdc, FDO_FRST);
87716111cedSAndrew Moore 
8785b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
8796182fdbdSPeter Wemm 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
8806182fdbdSPeter Wemm 		   NE7_SPEC_2(2, 0), 0)) {
8816182fdbdSPeter Wemm 		error = ENXIO;
8826182fdbdSPeter Wemm 		goto out;
8835b81b6b3SRodney W. Grimes 	}
8846182fdbdSPeter Wemm 
8856182fdbdSPeter Wemm 	if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
8866182fdbdSPeter Wemm 		ic_type = (u_char)ic_type;
8876182fdbdSPeter Wemm 		switch (ic_type) {
8886182fdbdSPeter Wemm 		case 0x80:
8896182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 765 or clone");
8906182fdbdSPeter Wemm 			fdc->fdct = FDC_NE765;
8916182fdbdSPeter Wemm 			break;
8921a6bed68SJoerg Wunsch 		case 0x81:	/* not mentioned in any hardware doc */
8936182fdbdSPeter Wemm 		case 0x90:
8941a6bed68SJoerg Wunsch 			device_set_desc(dev,
8952ac8c61aSMaxime Henrion 		"Enhanced floppy controller (i82077, NE72065 or clone)");
8961a6bed68SJoerg Wunsch 			fdc->fdct = FDC_ENHANCED;
8976182fdbdSPeter Wemm 			break;
8986182fdbdSPeter Wemm 		default:
8992ac8c61aSMaxime Henrion 			device_set_desc(dev, "Generic floppy controller");
9006182fdbdSPeter Wemm 			fdc->fdct = FDC_UNKNOWN;
9016182fdbdSPeter Wemm 			break;
9026182fdbdSPeter Wemm 		}
9036182fdbdSPeter Wemm 	}
9046182fdbdSPeter Wemm 
9056182fdbdSPeter Wemm out:
90637286586SPeter Wemm 	fdc_release_resources(fdc);
9076182fdbdSPeter Wemm 	return (error);
9085b81b6b3SRodney W. Grimes }
9095b81b6b3SRodney W. Grimes 
9105f830ea2SJoerg Wunsch #if NCARD > 0
9115f830ea2SJoerg Wunsch 
9125f830ea2SJoerg Wunsch static int
9135f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev)
9145f830ea2SJoerg Wunsch {
9155f830ea2SJoerg Wunsch 	int	error;
9165f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
9175f830ea2SJoerg Wunsch 
9185f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
9195f830ea2SJoerg Wunsch 	bzero(fdc, sizeof *fdc);
9205f830ea2SJoerg Wunsch 	fdc->fdc_dev = dev;
9215f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_pcmcia;
9225f830ea2SJoerg Wunsch 
9235f830ea2SJoerg Wunsch 	fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
9245f830ea2SJoerg Wunsch 
9255f830ea2SJoerg Wunsch 	/* Attempt to allocate our resources for the duration of the probe */
9265f830ea2SJoerg Wunsch 	error = fdc_alloc_resources(fdc);
9275f830ea2SJoerg Wunsch 	if (error)
9285f830ea2SJoerg Wunsch 		goto out;
9295f830ea2SJoerg Wunsch 
9305f830ea2SJoerg Wunsch 	/* First - lets reset the floppy controller */
9315f830ea2SJoerg Wunsch 	fdout_wr(fdc, 0);
9325f830ea2SJoerg Wunsch 	DELAY(100);
9335f830ea2SJoerg Wunsch 	fdout_wr(fdc, FDO_FRST);
9345f830ea2SJoerg Wunsch 
9355f830ea2SJoerg Wunsch 	/* see if it can handle a command */
9365f830ea2SJoerg Wunsch 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
9375f830ea2SJoerg Wunsch 		   NE7_SPEC_2(2, 0), 0)) {
9385f830ea2SJoerg Wunsch 		error = ENXIO;
9395f830ea2SJoerg Wunsch 		goto out;
9405f830ea2SJoerg Wunsch 	}
9415f830ea2SJoerg Wunsch 
9425f830ea2SJoerg Wunsch 	device_set_desc(dev, "Y-E Data PCMCIA floppy");
9435f830ea2SJoerg Wunsch 	fdc->fdct = FDC_NE765;
9445f830ea2SJoerg Wunsch 
9455f830ea2SJoerg Wunsch out:
9465f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
9475f830ea2SJoerg Wunsch 	return (error);
9485f830ea2SJoerg Wunsch }
9495f830ea2SJoerg Wunsch 
950e219897aSJoerg Wunsch #endif /* NCARD > 0 */
951e219897aSJoerg Wunsch 
9525f830ea2SJoerg Wunsch static int
953e219897aSJoerg Wunsch fdc_detach(device_t dev)
9545f830ea2SJoerg Wunsch {
9555f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
9565f830ea2SJoerg Wunsch 	int	error;
9575f830ea2SJoerg Wunsch 
9585f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
9595f830ea2SJoerg Wunsch 
9605f830ea2SJoerg Wunsch 	/* have our children detached first */
9615f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
9625f830ea2SJoerg Wunsch 		return (error);
9635f830ea2SJoerg Wunsch 
964e219897aSJoerg Wunsch 	/* reset controller, turn motor off */
965e219897aSJoerg Wunsch 	fdout_wr(fdc, 0);
966e219897aSJoerg Wunsch 
967e219897aSJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0)
968e219897aSJoerg Wunsch 		isa_dma_release(fdc->dmachan);
969e219897aSJoerg Wunsch 
9705f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_ATTACHED) == 0) {
9715f830ea2SJoerg Wunsch 		device_printf(dev, "already unloaded\n");
9725f830ea2SJoerg Wunsch 		return (0);
9735f830ea2SJoerg Wunsch 	}
9745f830ea2SJoerg Wunsch 	fdc->flags &= ~FDC_ATTACHED;
9755f830ea2SJoerg Wunsch 
9765f830ea2SJoerg Wunsch 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
9775f830ea2SJoerg Wunsch 			  fdc->fdc_intr);
9785f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
9795f830ea2SJoerg Wunsch 	device_printf(dev, "unload\n");
9805f830ea2SJoerg Wunsch 	return (0);
9815f830ea2SJoerg Wunsch }
9825f830ea2SJoerg Wunsch 
9835b81b6b3SRodney W. Grimes /*
98437286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
9855b81b6b3SRodney W. Grimes  */
9866182fdbdSPeter Wemm static void
98737286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
9885b81b6b3SRodney W. Grimes {
9898a9bc9c0SJohn Baldwin 	int	flags;
99037286586SPeter Wemm 	struct fdc_ivars *ivar;
9916182fdbdSPeter Wemm 	device_t child;
99292200632SGarrett Wollman 
9937cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
99437286586SPeter Wemm 	if (ivar == NULL)
9956182fdbdSPeter Wemm 		return;
99637286586SPeter Wemm 	if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
99737286586SPeter Wemm 		ivar->fdunit = 0;
998fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
99942117b6cSPoul-Henning Kamp 	if (child == NULL) {
100042117b6cSPoul-Henning Kamp 		free(ivar, M_DEVBUF);
10016182fdbdSPeter Wemm 		return;
100242117b6cSPoul-Henning Kamp 	}
100337286586SPeter Wemm 	device_set_ivars(child, ivar);
10041a6bed68SJoerg Wunsch 	if (resource_int_value(name, unit, "flags", &flags) == 0)
10051a6bed68SJoerg Wunsch 		 device_set_flags(child, flags);
10068a9bc9c0SJohn Baldwin 	if (resource_disabled(name, unit))
10076182fdbdSPeter Wemm 		device_disable(child);
10086182fdbdSPeter Wemm }
10096182fdbdSPeter Wemm 
10106182fdbdSPeter Wemm static int
10116182fdbdSPeter Wemm fdc_attach(device_t dev)
10126182fdbdSPeter Wemm {
101337286586SPeter Wemm 	struct	fdc_data *fdc;
10142398f0cdSPeter Wemm 	const char *name, *dname;
10155d54fe91SJoerg Wunsch 	int	i, error, dunit;
1016427ccf00SDoug Rabson 
101737286586SPeter Wemm 	fdc = device_get_softc(dev);
101837286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
101937286586SPeter Wemm 	if (error) {
1020f8ce7dd5SJoerg Wunsch 		device_printf(dev, "cannot re-acquire resources\n");
102137286586SPeter Wemm 		return error;
102237286586SPeter Wemm 	}
102337286586SPeter Wemm 	error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
10243c36743eSMark Murray 			       INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
10253c36743eSMark Murray 			       &fdc->fdc_intr);
102637286586SPeter Wemm 	if (error) {
102737286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
102837286586SPeter Wemm 		return error;
102937286586SPeter Wemm 	}
103037286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
1031fb35bd37SJoerg Wunsch 	fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET;
10326182fdbdSPeter Wemm 
10335f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
1034f8ce7dd5SJoerg Wunsch 		/*
1035f8ce7dd5SJoerg Wunsch 		 * Acquire the DMA channel forever, the driver will do
1036f8ce7dd5SJoerg Wunsch 		 * the rest
1037f8ce7dd5SJoerg Wunsch 		 * XXX should integrate with rman
1038f8ce7dd5SJoerg Wunsch 		 */
1039100f78bbSSujal Patel 		isa_dma_acquire(fdc->dmachan);
1040f8ce7dd5SJoerg Wunsch 		isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
10415f830ea2SJoerg Wunsch 	}
10425b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
10436182fdbdSPeter Wemm 
10443a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
104564860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
10468177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
10475b81b6b3SRodney W. Grimes 
10486182fdbdSPeter Wemm 	/*
104937286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
105037286586SPeter Wemm 	 * devices from the BIOS unless overridden.
10516182fdbdSPeter Wemm 	 */
1052ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
10532398f0cdSPeter Wemm 	i = 0;
10542398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
10552398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
105637286586SPeter Wemm 
105760444853SJoerg Wunsch 	if ((error = bus_generic_attach(dev)) != 0)
105860444853SJoerg Wunsch 		return (error);
105960444853SJoerg Wunsch 
106060444853SJoerg Wunsch 	return (0);
10616182fdbdSPeter Wemm }
10626182fdbdSPeter Wemm 
106315317dd8SMatthew N. Dodd static int
10646182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
10656182fdbdSPeter Wemm {
10661a6bed68SJoerg Wunsch 	int retval = 0, flags;
106715317dd8SMatthew N. Dodd 
106815317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
10691a6bed68SJoerg Wunsch 	retval += printf(" on %s drive %d", device_get_nameunit(me),
107037286586SPeter Wemm 	       fdc_get_fdunit(child));
10711a6bed68SJoerg Wunsch 	if ((flags = device_get_flags(me)) != 0)
10721a6bed68SJoerg Wunsch 		retval += printf(" flags %#x", flags);
10731a6bed68SJoerg Wunsch 	retval += printf("\n");
107415317dd8SMatthew N. Dodd 
107515317dd8SMatthew N. Dodd 	return (retval);
10766182fdbdSPeter Wemm }
10776182fdbdSPeter Wemm 
107816e68fc6SPeter Wemm static device_method_t fdc_methods[] = {
107916e68fc6SPeter Wemm 	/* Device interface */
108016e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fdc_probe),
108116e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fdc_attach),
1082e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
108316e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
108416e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend),
108516e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume),
108616e68fc6SPeter Wemm 
108716e68fc6SPeter Wemm 	/* Bus interface */
108816e68fc6SPeter Wemm 	DEVMETHOD(bus_print_child,	fdc_print_child),
108937286586SPeter Wemm 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
109016e68fc6SPeter Wemm 	/* Our children never use any other bus interface methods. */
109116e68fc6SPeter Wemm 
109216e68fc6SPeter Wemm 	{ 0, 0 }
109316e68fc6SPeter Wemm };
109416e68fc6SPeter Wemm 
109516e68fc6SPeter Wemm static driver_t fdc_driver = {
109616e68fc6SPeter Wemm 	"fdc",
109716e68fc6SPeter Wemm 	fdc_methods,
109816e68fc6SPeter Wemm 	sizeof(struct fdc_data)
109916e68fc6SPeter Wemm };
110016e68fc6SPeter Wemm 
110116e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
11025f063c7bSMike Smith DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0);
110316e68fc6SPeter Wemm 
11045f830ea2SJoerg Wunsch #if NCARD > 0
11055f830ea2SJoerg Wunsch 
11065f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = {
11075f830ea2SJoerg Wunsch 	/* Device interface */
11085f830ea2SJoerg Wunsch 	DEVMETHOD(device_probe,		fdc_pccard_probe),
11095f830ea2SJoerg Wunsch 	DEVMETHOD(device_attach,	fdc_attach),
1110e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
11115f830ea2SJoerg Wunsch 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
11125f830ea2SJoerg Wunsch 	DEVMETHOD(device_suspend,	bus_generic_suspend),
11135f830ea2SJoerg Wunsch 	DEVMETHOD(device_resume,	bus_generic_resume),
11145f830ea2SJoerg Wunsch 
11155f830ea2SJoerg Wunsch 	/* Bus interface */
11165f830ea2SJoerg Wunsch 	DEVMETHOD(bus_print_child,	fdc_print_child),
11175f830ea2SJoerg Wunsch 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
11185f830ea2SJoerg Wunsch 	/* Our children never use any other bus interface methods. */
11195f830ea2SJoerg Wunsch 
11205f830ea2SJoerg Wunsch 	{ 0, 0 }
11215f830ea2SJoerg Wunsch };
11225f830ea2SJoerg Wunsch 
11235f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = {
11245f830ea2SJoerg Wunsch 	"fdc",
11255f830ea2SJoerg Wunsch 	fdc_pccard_methods,
11265f830ea2SJoerg Wunsch 	sizeof(struct fdc_data)
11275f830ea2SJoerg Wunsch };
11285f830ea2SJoerg Wunsch 
11295f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
11305f830ea2SJoerg Wunsch 
11315f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
11325f830ea2SJoerg Wunsch 
11333f54a085SPoul-Henning Kamp 
113416e68fc6SPeter Wemm /*
1135246ed35dSJoerg Wunsch  * Configuration/initialization, per drive.
113616e68fc6SPeter Wemm  */
11376182fdbdSPeter Wemm static int
11386182fdbdSPeter Wemm fd_probe(device_t dev)
11396182fdbdSPeter Wemm {
11406182fdbdSPeter Wemm 	int	i;
11411a6bed68SJoerg Wunsch 	u_int	st0, st3;
11426182fdbdSPeter Wemm 	struct	fd_data *fd;
11436182fdbdSPeter Wemm 	struct	fdc_data *fdc;
11446182fdbdSPeter Wemm 	fdsu_t	fdsu;
11451a6bed68SJoerg Wunsch 	int	flags;
11466182fdbdSPeter Wemm 
11476182fdbdSPeter Wemm 	fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
11486182fdbdSPeter Wemm 	fd = device_get_softc(dev);
11496182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
11501a6bed68SJoerg Wunsch 	flags = device_get_flags(dev);
11516182fdbdSPeter Wemm 
11526182fdbdSPeter Wemm 	bzero(fd, sizeof *fd);
11536182fdbdSPeter Wemm 	fd->dev = dev;
11546182fdbdSPeter Wemm 	fd->fdc = fdc;
11556182fdbdSPeter Wemm 	fd->fdsu = fdsu;
11566182fdbdSPeter Wemm 	fd->fdu = device_get_unit(dev);
11571a6bed68SJoerg Wunsch 	fd->flags = FD_UA;	/* make sure fdautoselect() will be called */
11586182fdbdSPeter Wemm 
11591a6bed68SJoerg Wunsch 	fd->type = FD_DTYPE(flags);
1160038d1bbdSJoerg Wunsch /*
1161038d1bbdSJoerg Wunsch  * XXX I think using __i386__ is wrong here since we actually want to probe
1162038d1bbdSJoerg Wunsch  * for the machine type, not the CPU type (so non-PC arch's like the PC98 will
1163038d1bbdSJoerg Wunsch  * fail the probe).  However, for whatever reason, testing for _MACHINE_ARCH
1164038d1bbdSJoerg Wunsch  * == i386 breaks the test on FreeBSD/Alpha.
1165038d1bbdSJoerg Wunsch  */
116604209354SPeter Wemm #if defined(__i386__) || defined(__amd64__)
11671a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) {
11681a6bed68SJoerg Wunsch 		/* Look up what the BIOS thinks we have. */
11691a6bed68SJoerg Wunsch 		if (fd->fdu == 0) {
11705f830ea2SJoerg Wunsch 			if ((fdc->flags & FDC_ISPCMCIA))
11711a6bed68SJoerg Wunsch 				/*
11721a6bed68SJoerg Wunsch 				 * Somewhat special.  No need to force the
11731a6bed68SJoerg Wunsch 				 * user to set device flags, since the Y-E
11741a6bed68SJoerg Wunsch 				 * Data PCMCIA floppy is always a 1.44 MB
11751a6bed68SJoerg Wunsch 				 * device.
11761a6bed68SJoerg Wunsch 				 */
11771a6bed68SJoerg Wunsch 				fd->type = FDT_144M;
11780722d6abSJoerg Wunsch 			else
11791a6bed68SJoerg Wunsch 				fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
11801a6bed68SJoerg Wunsch 		} else {
11811a6bed68SJoerg Wunsch 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
11826b7bd95bSJoerg Wunsch 		}
11831a6bed68SJoerg Wunsch 		if (fd->type == FDT_288M_1)
11841a6bed68SJoerg Wunsch 			fd->type = FDT_288M;
11851a6bed68SJoerg Wunsch 	}
118604209354SPeter Wemm #endif /* __i386__ || __amd64__ */
11876182fdbdSPeter Wemm 	/* is there a unit? */
11881a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE)
11896182fdbdSPeter Wemm 		return (ENXIO);
11906182fdbdSPeter Wemm 
11916182fdbdSPeter Wemm 	/* select it */
11926182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNON);
1193fb35bd37SJoerg Wunsch 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
11946182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
11956182fdbdSPeter Wemm 
11968de0675cSPeter Wemm 	/* XXX This doesn't work before the first set_motor() */
11971a6bed68SJoerg Wunsch 	if ((fdc->flags & FDC_HAS_FIFO) == 0  &&
11981a6bed68SJoerg Wunsch 	    fdc->fdct == FDC_ENHANCED &&
11991a6bed68SJoerg Wunsch 	    (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 &&
12001a6bed68SJoerg Wunsch 	    enable_fifo(fdc) == 0) {
1201b6e5f28eSPeter Wemm 		device_printf(device_get_parent(dev),
1202b6e5f28eSPeter Wemm 		    "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1203d66c374fSTor Egge 	}
12046182fdbdSPeter Wemm 
12051a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0) {
1206dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
12071a6bed68SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
12081a6bed68SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
1209dc5df763SJoerg Wunsch 			/* Seek some steps... */
12106182fdbdSPeter Wemm 			if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1211dc5df763SJoerg Wunsch 				/* ...wait a moment... */
1212dc5df763SJoerg Wunsch 				DELAY(300000);
1213dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
12146182fdbdSPeter Wemm 				fd_sense_int(fdc, 0, 0);
1215dc5df763SJoerg Wunsch 			}
1216dc5df763SJoerg Wunsch 		}
1217dc5df763SJoerg Wunsch 
12186b7bd95bSJoerg Wunsch 		for (i = 0; i < 2; i++) {
12196b7bd95bSJoerg Wunsch 			/*
12206b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
12211a6bed68SJoerg Wunsch 			 * heads have been beyond cylinder 76, since
12221a6bed68SJoerg Wunsch 			 * most FDCs still barf when attempting to
12231a6bed68SJoerg Wunsch 			 * recalibrate more than 77 steps
12246b7bd95bSJoerg Wunsch 			 */
1225dc5df763SJoerg Wunsch 			/* go back to 0: */
12266182fdbdSPeter Wemm 			if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
12276b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
12286b7bd95bSJoerg Wunsch 				DELAY(i == 0 ? 1000000 : 300000);
12295b81b6b3SRodney W. Grimes 
12306b7bd95bSJoerg Wunsch 				/* anything responding? */
1231dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
1232dc5df763SJoerg Wunsch 				    (st0 & NE7_ST0_EC) == 0)
12336b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
12346b7bd95bSJoerg Wunsch 			}
1235dc5df763SJoerg Wunsch 		}
12361a6bed68SJoerg Wunsch 	}
12376b7bd95bSJoerg Wunsch 
12386182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNOFF);
12393a2f7427SDavid Greenman 
12401a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0 &&
12411a6bed68SJoerg Wunsch 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
12426182fdbdSPeter Wemm 		return (ENXIO);
12435b81b6b3SRodney W. Grimes 
12441a6bed68SJoerg Wunsch 	switch (fd->type) {
12451a6bed68SJoerg Wunsch 	case FDT_12M:
12461a6bed68SJoerg Wunsch 		device_set_desc(dev, "1200-KB 5.25\" drive");
12471a6bed68SJoerg Wunsch 		fd->type = FDT_12M;
12481a6bed68SJoerg Wunsch 		break;
12491a6bed68SJoerg Wunsch 	case FDT_144M:
12501a6bed68SJoerg Wunsch 		device_set_desc(dev, "1440-KB 3.5\" drive");
12511a6bed68SJoerg Wunsch 		fd->type = FDT_144M;
12521a6bed68SJoerg Wunsch 		break;
12531a6bed68SJoerg Wunsch 	case FDT_288M:
12541a6bed68SJoerg Wunsch 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
12551a6bed68SJoerg Wunsch 		fd->type = FDT_288M;
12561a6bed68SJoerg Wunsch 		break;
12571a6bed68SJoerg Wunsch 	case FDT_360K:
12581a6bed68SJoerg Wunsch 		device_set_desc(dev, "360-KB 5.25\" drive");
12591a6bed68SJoerg Wunsch 		fd->type = FDT_360K;
12601a6bed68SJoerg Wunsch 		break;
12611a6bed68SJoerg Wunsch 	case FDT_720K:
12621a6bed68SJoerg Wunsch 		device_set_desc(dev, "720-KB 3.5\" drive");
12631a6bed68SJoerg Wunsch 		fd->type = FDT_720K;
12641a6bed68SJoerg Wunsch 		break;
12651a6bed68SJoerg Wunsch 	default:
12661a6bed68SJoerg Wunsch 		return (ENXIO);
12671a6bed68SJoerg Wunsch 	}
1268dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1269b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1270b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
12713a2f7427SDavid Greenman 	fd->options = 0;
127202a19910SJustin T. Gibbs 	callout_handle_init(&fd->toffhandle);
127302a19910SJustin T. Gibbs 	callout_handle_init(&fd->tohandle);
12745b81b6b3SRodney W. Grimes 
12751a6bed68SJoerg Wunsch 	/* initialize densities for subdevices */
12761a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS; i++)
12771a6bed68SJoerg Wunsch 		memcpy(fd->fts + i, fd_native_types + fd->type,
12781a6bed68SJoerg Wunsch 		       sizeof(struct fd_type));
12796182fdbdSPeter Wemm 	return (0);
12806182fdbdSPeter Wemm }
12816182fdbdSPeter Wemm 
12826182fdbdSPeter Wemm static int
12836182fdbdSPeter Wemm fd_attach(device_t dev)
12846182fdbdSPeter Wemm {
12856182fdbdSPeter Wemm 	struct	fd_data *fd;
12866182fdbdSPeter Wemm 
128764860614SJoerg Wunsch 	fd = device_get_softc(dev);
1288503799eaSPoul-Henning Kamp 	fd->masterdev = make_dev(&fd_cdevsw, fd->fdu,
12893f54a085SPoul-Henning Kamp 				 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
1290a2f19df9SPoul-Henning Kamp 	fd->masterdev->si_drv1 = fd;
129180980460SPoul-Henning Kamp 	fd->device_stats = devstat_new_entry(device_get_name(dev),
1292f8ce7dd5SJoerg Wunsch 			  device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
12932a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
12942a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_FD);
12956182fdbdSPeter Wemm 	return (0);
12965b81b6b3SRodney W. Grimes }
12975b81b6b3SRodney W. Grimes 
12985f830ea2SJoerg Wunsch static int
12995f830ea2SJoerg Wunsch fd_detach(device_t dev)
13005f830ea2SJoerg Wunsch {
13015f830ea2SJoerg Wunsch 	struct	fd_data *fd;
13025f830ea2SJoerg Wunsch 
13035f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
130460444853SJoerg Wunsch 	untimeout(fd_turnoff, fd, fd->toffhandle);
130580980460SPoul-Henning Kamp 	devstat_remove_entry(fd->device_stats);
1306e219897aSJoerg Wunsch 	destroy_dev(fd->masterdev);
13075f830ea2SJoerg Wunsch 
13085f830ea2SJoerg Wunsch 	return (0);
13095f830ea2SJoerg Wunsch }
13105f830ea2SJoerg Wunsch 
131116e68fc6SPeter Wemm static device_method_t fd_methods[] = {
131216e68fc6SPeter Wemm 	/* Device interface */
131316e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
131416e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
13155f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
131616e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
131716e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
131816e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
131916e68fc6SPeter Wemm 
132016e68fc6SPeter Wemm 	{ 0, 0 }
132116e68fc6SPeter Wemm };
132216e68fc6SPeter Wemm 
132316e68fc6SPeter Wemm static driver_t fd_driver = {
132416e68fc6SPeter Wemm 	"fd",
132516e68fc6SPeter Wemm 	fd_methods,
132616e68fc6SPeter Wemm 	sizeof(struct fd_data)
132716e68fc6SPeter Wemm };
132816e68fc6SPeter Wemm 
1329475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
133016e68fc6SPeter Wemm 
1331246ed35dSJoerg Wunsch /*
1332246ed35dSJoerg Wunsch  * More auxiliary functions.
1333246ed35dSJoerg Wunsch  */
1334246ed35dSJoerg Wunsch /*
1335246ed35dSJoerg Wunsch  * Motor control stuff.
1336246ed35dSJoerg Wunsch  * Remember to not deselect the drive we're working on.
1337246ed35dSJoerg Wunsch  */
13383a2f7427SDavid Greenman static void
13396182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon)
13405b81b6b3SRodney W. Grimes {
1341fb35bd37SJoerg Wunsch 	int fdout;
13423a2f7427SDavid Greenman 
1343fb35bd37SJoerg Wunsch 	fdout = fdc->fdout;
13443a2f7427SDavid Greenman 	if (turnon) {
13453a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
1346fb35bd37SJoerg Wunsch 		fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
13473a2f7427SDavid Greenman 	} else
13483a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
13496182fdbdSPeter Wemm 	fdc->fdout = fdout;
1350fb35bd37SJoerg Wunsch 	fdout_wr(fdc, fdout);
13513a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
13523a2f7427SDavid Greenman }
13533a2f7427SDavid Greenman 
1354381fe1aaSGarrett Wollman static void
13556182fdbdSPeter Wemm fd_turnoff(void *xfd)
13565b81b6b3SRodney W. Grimes {
1357f5f7ba03SJordan K. Hubbard 	int	s;
13586182fdbdSPeter Wemm 	fd_p fd = xfd;
1359dc16046fSJoerg Wunsch 
13606182fdbdSPeter Wemm 	TRACE1("[fd%d: turnoff]", fd->fdu);
13618335c1b8SBruce Evans 
13625f830ea2SJoerg Wunsch 	s = splbio();
13638335c1b8SBruce Evans 	/*
13648335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
13655f830ea2SJoerg Wunsch 	 *
13665f830ea2SJoerg Wunsch 	 * If we got here, this could only mean we missed an interrupt.
13675f830ea2SJoerg Wunsch 	 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
13685f830ea2SJoerg Wunsch 	 * after a controller reset.  Just schedule a pseudo-interrupt
13695f830ea2SJoerg Wunsch 	 * so the state machine gets re-entered.
13708335c1b8SBruce Evans 	 */
13716182fdbdSPeter Wemm 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
13725f830ea2SJoerg Wunsch 		fdc_intr(fd->fdc);
13735f830ea2SJoerg Wunsch 		splx(s);
13748335c1b8SBruce Evans 		return;
13758335c1b8SBruce Evans 	}
13768335c1b8SBruce Evans 
13775b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
13786182fdbdSPeter Wemm 	set_motor(fd->fdc, fd->fdsu, TURNOFF);
1379f5f7ba03SJordan K. Hubbard 	splx(s);
13805b81b6b3SRodney W. Grimes }
13815b81b6b3SRodney W. Grimes 
13823a2f7427SDavid Greenman static void
13836182fdbdSPeter Wemm fd_motor_on(void *xfd)
13845b81b6b3SRodney W. Grimes {
1385f5f7ba03SJordan K. Hubbard 	int	s;
13866182fdbdSPeter Wemm 	fd_p fd = xfd;
1387f5f7ba03SJordan K. Hubbard 
1388f5f7ba03SJordan K. Hubbard 	s = splbio();
13895b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
13905b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
13915b81b6b3SRodney W. Grimes 	{
13926182fdbdSPeter Wemm 		fdc_intr(fd->fdc);
13935b81b6b3SRodney W. Grimes 	}
1394f5f7ba03SJordan K. Hubbard 	splx(s);
13955b81b6b3SRodney W. Grimes }
13965b81b6b3SRodney W. Grimes 
13973a2f7427SDavid Greenman static void
13986182fdbdSPeter Wemm fd_turnon(fd_p fd)
13995b81b6b3SRodney W. Grimes {
14005b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
14015b81b6b3SRodney W. Grimes 	{
14023a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
14036182fdbdSPeter Wemm 		set_motor(fd->fdc, fd->fdsu, TURNON);
14046182fdbdSPeter Wemm 		timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
14055b81b6b3SRodney W. Grimes 	}
14065b81b6b3SRodney W. Grimes }
14075b81b6b3SRodney W. Grimes 
1408381fe1aaSGarrett Wollman static void
1409dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
14105b81b6b3SRodney W. Grimes {
14113a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
1412427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
14133a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
14143a2f7427SDavid Greenman 	DELAY(100);
14153a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
1416427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
14173a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
14183a2f7427SDavid Greenman 	DELAY(100);
1419427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout);
14203a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
14213a2f7427SDavid Greenman 
142264860614SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
14236182fdbdSPeter Wemm 	(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1424dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1425dc5df763SJoerg Wunsch 		     0);
1426d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1427d66c374fSTor Egge 		(void) enable_fifo(fdc);
14285b81b6b3SRodney W. Grimes }
14295b81b6b3SRodney W. Grimes 
1430246ed35dSJoerg Wunsch /*
1431246ed35dSJoerg Wunsch  * FDC IO functions, take care of the main status register, timeout
1432246ed35dSJoerg Wunsch  * in case the desired status bits are never set.
1433a2642c4dSJoerg Wunsch  *
1434a2642c4dSJoerg Wunsch  * These PIO loops initially start out with short delays between
1435a2642c4dSJoerg Wunsch  * each iteration in the expectation that the required condition
1436a2642c4dSJoerg Wunsch  * is usually met quickly, so it can be handled immediately.  After
1437a2642c4dSJoerg Wunsch  * about 1 ms, stepping is increased to achieve a better timing
1438a2642c4dSJoerg Wunsch  * accuracy in the calls to DELAY().
1439246ed35dSJoerg Wunsch  */
1440b5e8ce9fSBruce Evans static int
14416182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr)
1442dc5df763SJoerg Wunsch {
1443a2642c4dSJoerg Wunsch 	int i, j, step;
1444a2642c4dSJoerg Wunsch 
1445a2642c4dSJoerg Wunsch 	for (j = 0, step = 1;
1446a2642c4dSJoerg Wunsch 	    (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1447a2642c4dSJoerg Wunsch 	    j < FDSTS_TIMEOUT;
1448a2642c4dSJoerg Wunsch 	    j += step) {
1449dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
14508a0ba818SJoerg Wunsch 			return (fdc_err(fdc, "ready for output in input\n"));
1451a2642c4dSJoerg Wunsch 		if (j == 1000)
1452a2642c4dSJoerg Wunsch 			step = 1000;
1453a2642c4dSJoerg Wunsch 		DELAY(step);
1454d0900d6bSJoerg Wunsch 	}
1455a2642c4dSJoerg Wunsch 	if (j >= FDSTS_TIMEOUT)
14568a0ba818SJoerg Wunsch 		return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1457d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1458427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1459dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1460dc5df763SJoerg Wunsch 	*ptr = i;
14618a0ba818SJoerg Wunsch 	return (0);
1462d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1463427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1464dc5df763SJoerg Wunsch 	if (ptr)
1465dc5df763SJoerg Wunsch 		*ptr = i;
14668a0ba818SJoerg Wunsch 	return (0);
1467d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1468dc5df763SJoerg Wunsch }
1469dc5df763SJoerg Wunsch 
147037c84183SPoul-Henning Kamp static int
14716182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x)
14725b81b6b3SRodney W. Grimes {
1473a2642c4dSJoerg Wunsch 	int i, j, step;
14745b81b6b3SRodney W. Grimes 
1475a2642c4dSJoerg Wunsch 	for (j = 0, step = 1;
1476a2642c4dSJoerg Wunsch 	    (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1477a2642c4dSJoerg Wunsch 	    j < FDSTS_TIMEOUT;
1478a2642c4dSJoerg Wunsch 	    j += step) {
1479a2642c4dSJoerg Wunsch 		if (i == (NE7_DIO|NE7_RQM))
1480a2642c4dSJoerg Wunsch 			return (fdc_err(fdc, "ready for input in output\n"));
1481a2642c4dSJoerg Wunsch 		if (j == 1000)
1482a2642c4dSJoerg Wunsch 			step = 1000;
1483a2642c4dSJoerg Wunsch 		DELAY(step);
1484a2642c4dSJoerg Wunsch 	}
1485a2642c4dSJoerg Wunsch 	if (j >= FDSTS_TIMEOUT)
14868a0ba818SJoerg Wunsch 		return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
14873b3837dbSRodney W. Grimes 
14883b3837dbSRodney W. Grimes 	/* Send the command and return */
1489427ccf00SDoug Rabson 	fddata_wr(fdc, x);
14903a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
14915b81b6b3SRodney W. Grimes 	return (0);
14925b81b6b3SRodney W. Grimes }
14935b81b6b3SRodney W. Grimes 
1494246ed35dSJoerg Wunsch /*
1495246ed35dSJoerg Wunsch  * Block device driver interface functions (interspersed with even more
1496246ed35dSJoerg Wunsch  * auxiliary functions).
1497246ed35dSJoerg Wunsch  */
149837c84183SPoul-Henning Kamp static int
1499f967c4f9SPoul-Henning Kamp fdopen(dev_t dev, int flags, int mode, struct thread *td)
15005b81b6b3SRodney W. Grimes {
15016182fdbdSPeter Wemm 	fd_p	fd;
1502b99f0a4aSAndrew Moore 	fdc_p	fdc;
15031a6bed68SJoerg Wunsch  	int rv, unitattn, dflags;
15045b81b6b3SRodney W. Grimes 
1505a2f19df9SPoul-Henning Kamp 	fd = dev->si_drv1;
1506a2f19df9SPoul-Henning Kamp 	if (fd == NULL)
1507b99f0a4aSAndrew Moore 		return (ENXIO);
15086182fdbdSPeter Wemm 	fdc = fd->fdc;
15091a6bed68SJoerg Wunsch 	if ((fdc == NULL) || (fd->type == FDT_NONE))
1510b99f0a4aSAndrew Moore 		return (ENXIO);
15111a6bed68SJoerg Wunsch 	dflags = device_get_flags(fd->dev);
15123e425b96SJulian Elischer 	/*
15131a6bed68SJoerg Wunsch 	 * This is a bit bogus.  It's still possible that e. g. a
15141a6bed68SJoerg Wunsch 	 * descriptor gets inherited to a child, but then it's at
15151a6bed68SJoerg Wunsch 	 * least for the same subdevice.  By checking FD_OPEN here, we
15161a6bed68SJoerg Wunsch 	 * can ensure that a device isn't attempted to be opened with
15171a6bed68SJoerg Wunsch 	 * different densities at the same time where the second open
15181a6bed68SJoerg Wunsch 	 * could clobber the settings from the first one.
15193e425b96SJulian Elischer 	 */
15201a6bed68SJoerg Wunsch 	if (fd->flags & FD_OPEN)
15211a6bed68SJoerg Wunsch 		return (EBUSY);
15221a6bed68SJoerg Wunsch 
15231a6bed68SJoerg Wunsch 	if (flags & FNONBLOCK) {
15241a6bed68SJoerg Wunsch 		/*
15251a6bed68SJoerg Wunsch 		 * Unfortunately, physio(9) discards its ioflag
15261a6bed68SJoerg Wunsch 		 * argument, thus preventing us from seeing the
15271a6bed68SJoerg Wunsch 		 * IO_NDELAY bit.  So we need to keep track
15281a6bed68SJoerg Wunsch 		 * ourselves.
15291a6bed68SJoerg Wunsch 		 */
15301a6bed68SJoerg Wunsch 		fd->flags |= FD_NONBLOCK;
15311a6bed68SJoerg Wunsch 		fd->ft = 0;
15321a6bed68SJoerg Wunsch 	} else {
15331a6bed68SJoerg Wunsch 		/*
15341a6bed68SJoerg Wunsch 		 * Figure out a unit attention condition.
15351a6bed68SJoerg Wunsch 		 *
15361a6bed68SJoerg Wunsch 		 * If UA has been forced, proceed.
15371a6bed68SJoerg Wunsch 		 *
153862cc1e05SYaroslav Tykhiy 		 * If the drive has no changeline support,
153962cc1e05SYaroslav Tykhiy 		 * or if the drive parameters have been lost
154062cc1e05SYaroslav Tykhiy 		 * due to previous non-blocking access,
154162cc1e05SYaroslav Tykhiy 		 * assume a forced UA condition.
154262cc1e05SYaroslav Tykhiy 		 *
15431a6bed68SJoerg Wunsch 		 * If motor is off, turn it on for a moment
15441a6bed68SJoerg Wunsch 		 * and select our drive, in order to read the
15451a6bed68SJoerg Wunsch 		 * UA hardware signal.
15461a6bed68SJoerg Wunsch 		 *
15471a6bed68SJoerg Wunsch 		 * If motor is on, and our drive is currently
15481a6bed68SJoerg Wunsch 		 * selected, just read the hardware bit.
15491a6bed68SJoerg Wunsch 		 *
15501a6bed68SJoerg Wunsch 		 * If motor is on, but active for another
15511a6bed68SJoerg Wunsch 		 * drive on that controller, we are lost.  We
15521a6bed68SJoerg Wunsch 		 * cannot risk to deselect the other drive, so
15531a6bed68SJoerg Wunsch 		 * we just assume a forced UA condition to be
15541a6bed68SJoerg Wunsch 		 * on the safe side.
15551a6bed68SJoerg Wunsch 		 */
15561a6bed68SJoerg Wunsch 		unitattn = 0;
15571a6bed68SJoerg Wunsch 		if ((dflags & FD_NO_CHLINE) != 0 ||
155862cc1e05SYaroslav Tykhiy 		    (fd->flags & FD_UA) != 0 ||
155962cc1e05SYaroslav Tykhiy 		    fd->ft == 0) {
15601a6bed68SJoerg Wunsch 			unitattn = 1;
15611a6bed68SJoerg Wunsch 			fd->flags &= ~FD_UA;
15621a6bed68SJoerg Wunsch 		} else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 |
15631a6bed68SJoerg Wunsch 					 FDO_MOEN2 | FDO_MOEN3)) {
15641a6bed68SJoerg Wunsch 			if ((fdc->fdout & FDO_FDSEL) == fd->fdsu)
15651a6bed68SJoerg Wunsch 				unitattn = fdin_rd(fdc) & FDI_DCHG;
15661a6bed68SJoerg Wunsch 			else
15671a6bed68SJoerg Wunsch 				unitattn = 1;
15681a6bed68SJoerg Wunsch 		} else {
15691a6bed68SJoerg Wunsch 			set_motor(fdc, fd->fdsu, TURNON);
15701a6bed68SJoerg Wunsch 			unitattn = fdin_rd(fdc) & FDI_DCHG;
15711a6bed68SJoerg Wunsch 			set_motor(fdc, fd->fdsu, TURNOFF);
1572b39c878eSAndrey A. Chernov 		}
15731a6bed68SJoerg Wunsch 		if (unitattn && (rv = fdautoselect(dev)) != 0)
15741a6bed68SJoerg Wunsch 			return (rv);
15757ca0641bSAndrey A. Chernov 	}
15766182fdbdSPeter Wemm 	fd->flags |= FD_OPEN;
15773fef646eSJoerg Wunsch 	/*
15783fef646eSJoerg Wunsch 	 * Clearing the DMA overrun counter at open time is a bit messy.
15793fef646eSJoerg Wunsch 	 * Since we're only managing one counter per controller, opening
15803fef646eSJoerg Wunsch 	 * the second drive could mess it up.  Anyway, if the DMA overrun
15813fef646eSJoerg Wunsch 	 * condition is really persistent, it will eventually time out
15823fef646eSJoerg Wunsch 	 * still.  OTOH, clearing it here will ensure we'll at least start
15833fef646eSJoerg Wunsch 	 * trying again after a previous (maybe even long ago) failure.
15843fef646eSJoerg Wunsch 	 * Also, this is merely a stop-gap measure only that should not
15853fef646eSJoerg Wunsch 	 * happen during normal operation, so we can tolerate it to be a
15863fef646eSJoerg Wunsch 	 * bit sloppy about this.
15873fef646eSJoerg Wunsch 	 */
15883fef646eSJoerg Wunsch 	fdc->dma_overruns = 0;
15895f830ea2SJoerg Wunsch 
15905b81b6b3SRodney W. Grimes 	return 0;
15915b81b6b3SRodney W. Grimes }
15925b81b6b3SRodney W. Grimes 
159337c84183SPoul-Henning Kamp static int
1594b40ce416SJulian Elischer fdclose(dev_t dev, int flags, int mode, struct thread *td)
15955b81b6b3SRodney W. Grimes {
15966182fdbdSPeter Wemm 	struct fd_data *fd;
1597b99f0a4aSAndrew Moore 
1598a2f19df9SPoul-Henning Kamp 	fd = dev->si_drv1;
15991a6bed68SJoerg Wunsch 	fd->flags &= ~(FD_OPEN | FD_NONBLOCK);
16002995d110SJoerg Wunsch 	fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1601dc16046fSJoerg Wunsch 
16025b81b6b3SRodney W. Grimes 	return (0);
16035b81b6b3SRodney W. Grimes }
16045b81b6b3SRodney W. Grimes 
160537c84183SPoul-Henning Kamp static void
16068177437dSPoul-Henning Kamp fdstrategy(struct bio *bp)
16073a2f7427SDavid Greenman {
1608fb35bd37SJoerg Wunsch 	long blknum, nblocks;
16093a2f7427SDavid Greenman  	int	s;
16103a2f7427SDavid Greenman  	fdu_t	fdu;
16113a2f7427SDavid Greenman  	fdc_p	fdc;
16123a2f7427SDavid Greenman  	fd_p	fd;
16133a2f7427SDavid Greenman 	size_t	fdblk;
16143a2f7427SDavid Greenman 
1615a2f19df9SPoul-Henning Kamp 	fd = bp->bio_dev->si_drv1;
1616503799eaSPoul-Henning Kamp  	fdu = fd->fdu;
16173a2f7427SDavid Greenman 	fdc = fd->fdc;
161883efe35aSYaroslav Tykhiy 	bp->bio_resid = bp->bio_bcount;
16191a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE || fd->ft == 0) {
1620f79981acSYaroslav Tykhiy 		if (fd->type != FDT_NONE && (fd->flags & FD_NONBLOCK))
1621f79981acSYaroslav Tykhiy 			bp->bio_error = EAGAIN;
1622f79981acSYaroslav Tykhiy 		else
16238177437dSPoul-Henning Kamp 			bp->bio_error = ENXIO;
16248177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
16253b178206SWarner Losh 		goto bad;
162664860614SJoerg Wunsch 	}
1627d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
1628419f39ceSPoul-Henning Kamp 	if (bp->bio_cmd != FDBIO_FORMAT && bp->bio_cmd != FDBIO_RDSECTID) {
16291a6bed68SJoerg Wunsch 		if (fd->flags & FD_NONBLOCK) {
16301a6bed68SJoerg Wunsch 			bp->bio_error = EAGAIN;
16311a6bed68SJoerg Wunsch 			bp->bio_flags |= BIO_ERROR;
16321a6bed68SJoerg Wunsch 			goto bad;
16331a6bed68SJoerg Wunsch 		}
1634b52b7f46SPoul-Henning Kamp 		if (bp->bio_offset < 0) {
1635dc5df763SJoerg Wunsch 			printf(
1636b52b7f46SPoul-Henning Kamp 		"fd%d: fdstrat: bad request offset = %ju, bcount = %ld\n",
1637b52b7f46SPoul-Henning Kamp 			       fdu, (intmax_t)bp->bio_offset, bp->bio_bcount);
16388177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
16398177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
16403a2f7427SDavid Greenman 			goto bad;
16413a2f7427SDavid Greenman 		}
16428177437dSPoul-Henning Kamp 		if ((bp->bio_bcount % fdblk) != 0) {
16438177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
16448177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
16453a2f7427SDavid Greenman 			goto bad;
16463a2f7427SDavid Greenman 		}
16473a2f7427SDavid Greenman 	}
16483a2f7427SDavid Greenman 
16493a2f7427SDavid Greenman 	/*
16503a2f7427SDavid Greenman 	 * Set up block calculations.
16513a2f7427SDavid Greenman 	 */
1652b52b7f46SPoul-Henning Kamp 	if (bp->bio_offset >= ((off_t)128 << fd->ft->secsize) * fd->ft->size) {
16538177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
16548177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
16553a2f7427SDavid Greenman 		goto bad;
16563a2f7427SDavid Greenman 	}
1657b52b7f46SPoul-Henning Kamp 	blknum = bp->bio_offset / fdblk;
1658bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1659fb35bd37SJoerg Wunsch 	if (blknum + bp->bio_bcount / fdblk > nblocks) {
1660fb35bd37SJoerg Wunsch 		if (blknum >= nblocks) {
166183efe35aSYaroslav Tykhiy 			if (bp->bio_cmd != BIO_READ) {
1662fb35bd37SJoerg Wunsch 				bp->bio_error = ENOSPC;
16638177437dSPoul-Henning Kamp 				bp->bio_flags |= BIO_ERROR;
1664bb6382faSJoerg Wunsch 			}
1665fb35bd37SJoerg Wunsch 			goto bad;	/* not always bad, but EOF */
1666fb35bd37SJoerg Wunsch 		}
1667fb35bd37SJoerg Wunsch 		bp->bio_bcount = (nblocks - blknum) * fdblk;
1668bb6382faSJoerg Wunsch 	}
16699a5e3ddbSJoerg Wunsch  	bp->bio_pblkno = blknum;
16703a2f7427SDavid Greenman 	s = splbio();
1671891619a6SPoul-Henning Kamp 	bioq_disksort(&fdc->head, bp);
16726182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
16731ecc485cSPoul-Henning Kamp 	devstat_start_transaction_bio(fd->device_stats, bp);
16745f830ea2SJoerg Wunsch 	device_busy(fd->dev);
16756182fdbdSPeter Wemm 	fdstart(fdc);
16763a2f7427SDavid Greenman 	splx(s);
16773a2f7427SDavid Greenman 	return;
16783a2f7427SDavid Greenman 
16793a2f7427SDavid Greenman bad:
16803a2f7427SDavid Greenman 	biodone(bp);
16813a2f7427SDavid Greenman }
16823a2f7427SDavid Greenman 
1683246ed35dSJoerg Wunsch /*
1684246ed35dSJoerg Wunsch  * fdstart
1685246ed35dSJoerg Wunsch  *
1686246ed35dSJoerg Wunsch  * We have just queued something.  If the controller is not busy
1687246ed35dSJoerg Wunsch  * then simulate the case where it has just finished a command
1688246ed35dSJoerg Wunsch  * So that it (the interrupt routine) looks on the queue for more
1689246ed35dSJoerg Wunsch  * work to do and picks up what we just added.
1690246ed35dSJoerg Wunsch  *
1691246ed35dSJoerg Wunsch  * If the controller is already busy, we need do nothing, as it
1692246ed35dSJoerg Wunsch  * will pick up our work when the present work completes.
1693246ed35dSJoerg Wunsch  */
1694381fe1aaSGarrett Wollman static void
16956182fdbdSPeter Wemm fdstart(struct fdc_data *fdc)
16965b81b6b3SRodney W. Grimes {
16975b81b6b3SRodney W. Grimes 	int s;
16985b81b6b3SRodney W. Grimes 
16995b81b6b3SRodney W. Grimes 	s = splbio();
17006182fdbdSPeter Wemm 	if(fdc->state == DEVIDLE)
17015b81b6b3SRodney W. Grimes 	{
17026182fdbdSPeter Wemm 		fdc_intr(fdc);
17035b81b6b3SRodney W. Grimes 	}
17045b81b6b3SRodney W. Grimes 	splx(s);
17055b81b6b3SRodney W. Grimes }
17065b81b6b3SRodney W. Grimes 
1707381fe1aaSGarrett Wollman static void
17086182fdbdSPeter Wemm fd_iotimeout(void *xfdc)
17095b81b6b3SRodney W. Grimes {
17105c1a1eaeSBruce Evans  	fdc_p fdc;
1711f5f7ba03SJordan K. Hubbard 	int s;
17125b81b6b3SRodney W. Grimes 
17136182fdbdSPeter Wemm 	fdc = xfdc;
17145c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
17155b81b6b3SRodney W. Grimes 
17163a2f7427SDavid Greenman 	/*
17173a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
17183a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
17193a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
17203a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
17213a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
17225c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
17235c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
17245c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
17255c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
17263a2f7427SDavid Greenman 	 */
17273a2f7427SDavid Greenman 	s = splbio();
17285c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
17295c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
17305c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
17316182fdbdSPeter Wemm 	fdc_intr(fdc);
1732f5f7ba03SJordan K. Hubbard 	splx(s);
17335b81b6b3SRodney W. Grimes }
17345b81b6b3SRodney W. Grimes 
1735246ed35dSJoerg Wunsch /* Just ensure it has the right spl. */
1736381fe1aaSGarrett Wollman static void
17376182fdbdSPeter Wemm fd_pseudointr(void *xfdc)
17385b81b6b3SRodney W. Grimes {
17395b81b6b3SRodney W. Grimes 	int	s;
17403a2f7427SDavid Greenman 
17415b81b6b3SRodney W. Grimes 	s = splbio();
17426182fdbdSPeter Wemm 	fdc_intr(xfdc);
17435b81b6b3SRodney W. Grimes 	splx(s);
17445b81b6b3SRodney W. Grimes }
17455b81b6b3SRodney W. Grimes 
1746246ed35dSJoerg Wunsch /*
1747246ed35dSJoerg Wunsch  * fdc_intr
1748246ed35dSJoerg Wunsch  *
1749246ed35dSJoerg Wunsch  * Keep calling the state machine until it returns a 0.
1750246ed35dSJoerg Wunsch  * Always called at splbio.
1751246ed35dSJoerg Wunsch  */
1752fe310de8SBruce Evans static void
17536182fdbdSPeter Wemm fdc_intr(void *xfdc)
17545b81b6b3SRodney W. Grimes {
17556182fdbdSPeter Wemm 	fdc_p fdc = xfdc;
17566182fdbdSPeter Wemm 	while(fdstate(fdc))
1757381fe1aaSGarrett Wollman 		;
17585b81b6b3SRodney W. Grimes }
17595b81b6b3SRodney W. Grimes 
176069acd21dSWarner Losh /*
1761246ed35dSJoerg Wunsch  * Magic pseudo-DMA initialization for YE FDC. Sets count and
1762246ed35dSJoerg Wunsch  * direction.
176369acd21dSWarner Losh  */
17643b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \
17653b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port,	 \
17663b178206SWarner Losh 	    ((cnt)-1) & 0xff);						 \
17673b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
17683b178206SWarner Losh 	    ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
176969acd21dSWarner Losh 
177069acd21dSWarner Losh /*
1771246ed35dSJoerg Wunsch  * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy.
177269acd21dSWarner Losh  */
17734c34deeeSJoerg Wunsch static int
17744c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
177569acd21dSWarner Losh {
177669acd21dSWarner Losh 	u_char *cptr = (u_char *)addr;
177769acd21dSWarner Losh 
177821144e3bSPoul-Henning Kamp 	if (flags == BIO_READ) {
177969acd21dSWarner Losh 		if (fdc->state != PIOREAD) {
178069acd21dSWarner Losh 			fdc->state = PIOREAD;
178169acd21dSWarner Losh 			return(0);
178264860614SJoerg Wunsch 		}
17833b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
17843b178206SWarner Losh 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
17853b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
178669acd21dSWarner Losh 	} else {
17873b178206SWarner Losh 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
17883b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
17893b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
179064860614SJoerg Wunsch 	}
179169acd21dSWarner Losh 	return(1);
179269acd21dSWarner Losh }
179369acd21dSWarner Losh 
1794246ed35dSJoerg Wunsch /*
17951a6bed68SJoerg Wunsch  * Try figuring out the density of the media present in our device.
17961a6bed68SJoerg Wunsch  */
17971a6bed68SJoerg Wunsch static int
17981a6bed68SJoerg Wunsch fdautoselect(dev_t dev)
17991a6bed68SJoerg Wunsch {
18001a6bed68SJoerg Wunsch  	fd_p fd;
18011a6bed68SJoerg Wunsch 	struct fd_type *fdtp;
18021a6bed68SJoerg Wunsch 	struct fdc_readid id;
18031a6bed68SJoerg Wunsch 	int i, n, oopts, rv;
18041a6bed68SJoerg Wunsch 
1805a2f19df9SPoul-Henning Kamp 	fd = dev->si_drv1;
18061a6bed68SJoerg Wunsch 
18071a6bed68SJoerg Wunsch 	switch (fd->type) {
18081a6bed68SJoerg Wunsch 	default:
18091a6bed68SJoerg Wunsch 		return (ENXIO);
18101a6bed68SJoerg Wunsch 
18111a6bed68SJoerg Wunsch 	case FDT_360K:
18121a6bed68SJoerg Wunsch 	case FDT_720K:
18131a6bed68SJoerg Wunsch 		/* no autoselection on those drives */
18141a6bed68SJoerg Wunsch 		fd->ft = fd_native_types + fd->type;
18151a6bed68SJoerg Wunsch 		return (0);
18161a6bed68SJoerg Wunsch 
18171a6bed68SJoerg Wunsch 	case FDT_12M:
18181a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_12m;
18191a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_12m / sizeof(struct fd_type);
18201a6bed68SJoerg Wunsch 		break;
18211a6bed68SJoerg Wunsch 
18221a6bed68SJoerg Wunsch 	case FDT_144M:
18231a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_144m;
18241a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_144m / sizeof(struct fd_type);
18251a6bed68SJoerg Wunsch 		break;
18261a6bed68SJoerg Wunsch 
18271a6bed68SJoerg Wunsch 	case FDT_288M:
18281a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_288m;
18291a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_288m / sizeof(struct fd_type);
18301a6bed68SJoerg Wunsch 		break;
18311a6bed68SJoerg Wunsch 	}
18321a6bed68SJoerg Wunsch 
18331a6bed68SJoerg Wunsch 	/*
18341a6bed68SJoerg Wunsch 	 * Try reading sector ID fields, first at cylinder 0, head 0,
18351a6bed68SJoerg Wunsch 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
18361a6bed68SJoerg Wunsch 	 * since for 5.25in DD media in a HD drive, there are no data
18371a6bed68SJoerg Wunsch 	 * to read (2 step pulses per media cylinder required).  For
18381a6bed68SJoerg Wunsch 	 * two-sided media, the second probe always goes to head 1, so
18391a6bed68SJoerg Wunsch 	 * we can tell them apart from single-sided media.  As a
18401a6bed68SJoerg Wunsch 	 * side-effect this means that single-sided media should be
18411a6bed68SJoerg Wunsch 	 * mentioned in the search list after two-sided media of an
18421a6bed68SJoerg Wunsch 	 * otherwise identical density.  Media with a different number
18431a6bed68SJoerg Wunsch 	 * of sectors per track but otherwise identical parameters
18441a6bed68SJoerg Wunsch 	 * cannot be distinguished at all.
18451a6bed68SJoerg Wunsch 	 *
18461a6bed68SJoerg Wunsch 	 * If we successfully read an ID field on both cylinders where
18471a6bed68SJoerg Wunsch 	 * the recorded values match our expectation, we are done.
18481a6bed68SJoerg Wunsch 	 * Otherwise, we try the next density entry from the table.
18491a6bed68SJoerg Wunsch 	 *
18501a6bed68SJoerg Wunsch 	 * Stepping to cylinder 2 has the side-effect of clearing the
18511a6bed68SJoerg Wunsch 	 * unit attention bit.
18521a6bed68SJoerg Wunsch 	 */
18531a6bed68SJoerg Wunsch 	oopts = fd->options;
18541a6bed68SJoerg Wunsch 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
18551a6bed68SJoerg Wunsch 	for (i = 0; i < n; i++, fdtp++) {
18561a6bed68SJoerg Wunsch 		fd->ft = fdtp;
18571a6bed68SJoerg Wunsch 
18581a6bed68SJoerg Wunsch 		id.cyl = id.head = 0;
1859419f39ceSPoul-Henning Kamp 		rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
18601a6bed68SJoerg Wunsch 		if (rv != 0)
18611a6bed68SJoerg Wunsch 			continue;
18621a6bed68SJoerg Wunsch 		if (id.cyl != 0 || id.head != 0 ||
18631a6bed68SJoerg Wunsch 		    id.secshift != fdtp->secsize)
18641a6bed68SJoerg Wunsch 			continue;
18651a6bed68SJoerg Wunsch 		id.cyl = 2;
18661a6bed68SJoerg Wunsch 		id.head = fd->ft->heads - 1;
1867419f39ceSPoul-Henning Kamp 		rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
18681a6bed68SJoerg Wunsch 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
18691a6bed68SJoerg Wunsch 		    id.secshift != fdtp->secsize)
18701a6bed68SJoerg Wunsch 			continue;
18711a6bed68SJoerg Wunsch 		if (rv == 0)
18721a6bed68SJoerg Wunsch 			break;
18731a6bed68SJoerg Wunsch 	}
18741a6bed68SJoerg Wunsch 
18751a6bed68SJoerg Wunsch 	fd->options = oopts;
18761a6bed68SJoerg Wunsch 	if (i == n) {
18775613959dSJoerg Wunsch 		if (bootverbose)
18781a6bed68SJoerg Wunsch 			device_printf(fd->dev, "autoselection failed\n");
18791a6bed68SJoerg Wunsch 		fd->ft = 0;
18801a6bed68SJoerg Wunsch 		return (EIO);
18811a6bed68SJoerg Wunsch 	} else {
18825613959dSJoerg Wunsch 		if (bootverbose)
18831a6bed68SJoerg Wunsch 			device_printf(fd->dev, "autoselected %d KB medium\n",
18841a6bed68SJoerg Wunsch 				      fd->ft->size / 2);
18851a6bed68SJoerg Wunsch 		return (0);
18861a6bed68SJoerg Wunsch 	}
18871a6bed68SJoerg Wunsch }
18881a6bed68SJoerg Wunsch 
18891a6bed68SJoerg Wunsch 
18901a6bed68SJoerg Wunsch /*
1891246ed35dSJoerg Wunsch  * The controller state machine.
1892246ed35dSJoerg Wunsch  *
1893246ed35dSJoerg Wunsch  * If it returns a non zero value, it should be called again immediately.
1894246ed35dSJoerg Wunsch  */
18953a2f7427SDavid Greenman static int
18966182fdbdSPeter Wemm fdstate(fdc_p fdc)
18975b81b6b3SRodney W. Grimes {
189864860614SJoerg Wunsch 	struct fdc_readid *idp;
1899fb35bd37SJoerg Wunsch 	int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
19001a6bed68SJoerg Wunsch 	int st0, cyl, st3, idf, ne7cmd, mfm, steptrac;
1901fb35bd37SJoerg Wunsch 	unsigned long blknum;
19025b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
19035b81b6b3SRodney W. Grimes 	fd_p fd;
19048177437dSPoul-Henning Kamp 	register struct bio *bp;
1905b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
19063a2f7427SDavid Greenman 	size_t fdblk;
19075b81b6b3SRodney W. Grimes 
1908e93e63cbSBruce Evans 	bp = fdc->bp;
1909e93e63cbSBruce Evans 	if (bp == NULL) {
19108177437dSPoul-Henning Kamp 		bp = bioq_first(&fdc->head);
1911e93e63cbSBruce Evans 		if (bp != NULL) {
19128177437dSPoul-Henning Kamp 			bioq_remove(&fdc->head, bp);
1913e93e63cbSBruce Evans 			fdc->bp = bp;
1914e93e63cbSBruce Evans 		}
1915e93e63cbSBruce Evans 	}
1916e93e63cbSBruce Evans 	if (bp == NULL) {
1917246ed35dSJoerg Wunsch 		/*
1918246ed35dSJoerg Wunsch 		 * Nothing left for this controller to do,
1919246ed35dSJoerg Wunsch 		 * force into the IDLE state.
1920246ed35dSJoerg Wunsch 		 */
19215b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
19226182fdbdSPeter Wemm 		if (fdc->fd) {
1923b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev,
1924b6e5f28eSPeter Wemm 			    "unexpected valid fd pointer\n");
19255b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
19265b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
19275b81b6b3SRodney W. Grimes 		}
19286182fdbdSPeter Wemm 		TRACE1("[fdc%d IDLE]", fdc->fdcu);
19295b81b6b3SRodney W. Grimes  		return (0);
19305b81b6b3SRodney W. Grimes 	}
1931a2f19df9SPoul-Henning Kamp 	fd = bp->bio_dev->si_drv1;
1932503799eaSPoul-Henning Kamp 	fdu = fd->fdu;
19333a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1934b6e5f28eSPeter Wemm 	if (fdc->fd && (fd != fdc->fd))
1935b6e5f28eSPeter Wemm 		device_printf(fd->dev, "confused fd pointers\n");
19368177437dSPoul-Henning Kamp 	read = bp->bio_cmd == BIO_READ;
19371a6bed68SJoerg Wunsch 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
19381a6bed68SJoerg Wunsch 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
193956a23089SPoul-Henning Kamp 	if (read)
194056a23089SPoul-Henning Kamp 		idf = ISADMA_READ;
194156a23089SPoul-Henning Kamp 	else
194256a23089SPoul-Henning Kamp 		idf = ISADMA_WRITE;
1943419f39ceSPoul-Henning Kamp 	format = bp->bio_cmd == FDBIO_FORMAT;
1944419f39ceSPoul-Henning Kamp 	rdsectid = bp->bio_cmd == FDBIO_RDSECTID;
1945fb35bd37SJoerg Wunsch 	if (format)
19468177437dSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
19475b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
19485b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
19495b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
19506182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle);
19516182fdbdSPeter Wemm 	fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
19525b81b6b3SRodney W. Grimes 	switch (fdc->state)
19535b81b6b3SRodney W. Grimes 	{
19545b81b6b3SRodney W. Grimes 	case DEVIDLE:
19555b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
19565b81b6b3SRodney W. Grimes 		fdc->retry = 0;
19575b81b6b3SRodney W. Grimes 		fd->skip = 0;
19585b81b6b3SRodney W. Grimes 		fdc->fd = fd;
19595b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
19605f830ea2SJoerg Wunsch 		fdc->fdctl_wr(fdc, fd->ft->trans);
19613a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1962246ed35dSJoerg Wunsch 		/*
1963246ed35dSJoerg Wunsch 		 * If the next drive has a motor startup pending, then
1964246ed35dSJoerg Wunsch 		 * it will start up in its own good time.
1965246ed35dSJoerg Wunsch 		 */
19666182fdbdSPeter Wemm 		if(fd->flags & FD_MOTOR_WAIT) {
19675b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
1968246ed35dSJoerg Wunsch 			return (0); /* will return later */
19695b81b6b3SRodney W. Grimes 		}
1970246ed35dSJoerg Wunsch 		/*
1971246ed35dSJoerg Wunsch 		 * Maybe if it's not starting, it SHOULD be starting.
1972246ed35dSJoerg Wunsch 		 */
19735b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
19745b81b6b3SRodney W. Grimes 		{
19755b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
19766182fdbdSPeter Wemm 			fd_turnon(fd);
1977246ed35dSJoerg Wunsch 			return (0); /* will return later */
19785b81b6b3SRodney W. Grimes 		}
19795b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
19805b81b6b3SRodney W. Grimes 		{
19816182fdbdSPeter Wemm 			set_motor(fdc, fd->fdsu, TURNON);
19825b81b6b3SRodney W. Grimes 		}
19835c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
19845c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
19855c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
19865c1a1eaeSBruce Evans 		} else
19875e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
1988246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
1989fb35bd37SJoerg Wunsch 
19905b81b6b3SRodney W. Grimes 	case DOSEEK:
1991fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
1992fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1993fb35bd37SJoerg Wunsch 		if (cylinder == fd->track)
19945b81b6b3SRodney W. Grimes 		{
19955b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
1996246ed35dSJoerg Wunsch 			return (1); /* will return immediately */
19975b81b6b3SRodney W. Grimes 		}
19986182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK,
19991a6bed68SJoerg Wunsch 			   fd->fdsu, cylinder * steptrac, 0))
2000dc8603e3SJoerg Wunsch 		{
2001dc8603e3SJoerg Wunsch 			/*
2002246ed35dSJoerg Wunsch 			 * Seek command not accepted, looks like
2003dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
2004dc8603e3SJoerg Wunsch 			 */
2005dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
20066182fdbdSPeter Wemm 			return(retrier(fdc));
2007dc8603e3SJoerg Wunsch 		}
2008dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
20095b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
20105b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
2011fb35bd37SJoerg Wunsch 
20125b81b6b3SRodney W. Grimes 	case SEEKWAIT:
20135b81b6b3SRodney W. Grimes 		/* allow heads to settle */
20146182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 16);
20155b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
20165b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
2017fb35bd37SJoerg Wunsch 
2018246ed35dSJoerg Wunsch 	case SEEKCOMPLETE : /* seek done, start DMA */
2019fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
2020fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2021fb35bd37SJoerg Wunsch 
2022246ed35dSJoerg Wunsch 		/* Make sure seek really happened. */
20236182fdbdSPeter Wemm 		if(fd->track == FD_NO_TRACK) {
20241a6bed68SJoerg Wunsch 			int descyl = cylinder * steptrac;
20253a2f7427SDavid Greenman 			do {
20263a2f7427SDavid Greenman 				/*
2027dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
2028dc5df763SJoerg Wunsch 				 * which cannot really happen since the
2029dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
2030dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
2031dc5df763SJoerg Wunsch 				 * line, so do one of the following:
2032dc5df763SJoerg Wunsch 				 *
2033dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
2034dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
2035dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
2036dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
2037dc5df763SJoerg Wunsch 				 * real interrupt condition.
2038dc5df763SJoerg Wunsch 				 *
2039dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
2040dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
2041dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
2042dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
2043dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
2044dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
2045dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
20463a2f7427SDavid Greenman 				 */
2047dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
2048dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
2049246ed35dSJoerg Wunsch 					return (0); /* will return later */
2050dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
2051dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2052246ed35dSJoerg Wunsch 					return (0); /* hope for a real intr */
20533a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2054dc5df763SJoerg Wunsch 
20556182fdbdSPeter Wemm 			if (0 == descyl) {
2056dc5df763SJoerg Wunsch 				int failed = 0;
20573a2f7427SDavid Greenman 				/*
20583a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
20593a2f7427SDavid Greenman 				 * really there
20603a2f7427SDavid Greenman 				 */
2061dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
2062dc5df763SJoerg Wunsch 					failed = 1;
20633a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
20643a2f7427SDavid Greenman 					printf(
20653a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
20663a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
2067dc5df763SJoerg Wunsch 					failed = 1;
2068dc5df763SJoerg Wunsch 				}
2069dc5df763SJoerg Wunsch 
20706182fdbdSPeter Wemm 				if (failed) {
20713a2f7427SDavid Greenman 					if(fdc->retry < 3)
20723a2f7427SDavid Greenman 						fdc->retry = 3;
20736182fdbdSPeter Wemm 					return (retrier(fdc));
20743a2f7427SDavid Greenman 				}
20753a2f7427SDavid Greenman 			}
2076dc5df763SJoerg Wunsch 
20776182fdbdSPeter Wemm 			if (cyl != descyl) {
20783a2f7427SDavid Greenman 				printf(
20793a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
20802d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
2081e5d7d243SBruce Evans 				if (fdc->retry < 3)
2082e5d7d243SBruce Evans 					fdc->retry = 3;
20836182fdbdSPeter Wemm 				return (retrier(fdc));
20845b81b6b3SRodney W. Grimes 			}
20855b81b6b3SRodney W. Grimes 		}
20865b81b6b3SRodney W. Grimes 
2087fb35bd37SJoerg Wunsch 		fd->track = cylinder;
2088fb35bd37SJoerg Wunsch 		if (format)
2089fb35bd37SJoerg Wunsch 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
2090fb35bd37SJoerg Wunsch 			    - (char *)finfo;
2091250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
20928177437dSPoul-Henning Kamp 			isa_dmastart(idf, bp->bio_data+fd->skip,
20938177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
2094fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
20955b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
20965b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
20975b81b6b3SRodney W. Grimes 		head = sec / sectrac;
20985b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
20991a6bed68SJoerg Wunsch 		if (head != 0 && fd->ft->offset_side2 != 0)
21001a6bed68SJoerg Wunsch 			sec += fd->ft->offset_side2;
21013a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
21023a2f7427SDavid Greenman 
2103250300ebSJoerg Wunsch 		if(format || !(read || rdsectid))
21043a2f7427SDavid Greenman 		{
21053a2f7427SDavid Greenman 			/* make sure the drive is writable */
2106dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
2107dc8603e3SJoerg Wunsch 			{
2108dc8603e3SJoerg Wunsch 				/* stuck controller? */
21095f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
211056a23089SPoul-Henning Kamp 					isa_dmadone(idf,
21118177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
21128177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
21135c1a1eaeSBruce Evans 						    fdc->dmachan);
2114dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
21156182fdbdSPeter Wemm 				return (retrier(fdc));
2116dc8603e3SJoerg Wunsch 			}
21173a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
21183a2f7427SDavid Greenman 			{
21193a2f7427SDavid Greenman 				/*
21203a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
21213a2f7427SDavid Greenman 				 * in order to force the current operation
21223a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
21233a2f7427SDavid Greenman 				 * error - all error handling is done
21243a2f7427SDavid Greenman 				 * by the retrier()
21253a2f7427SDavid Greenman 				 */
21263a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
21273a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
21283a2f7427SDavid Greenman 				fdc->status[2] = 0;
21293a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
21303a2f7427SDavid Greenman 				fdc->status[4] = head;
21313a2f7427SDavid Greenman 				fdc->status[5] = sec;
21323a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
21333a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
2134246ed35dSJoerg Wunsch 				return (1); /* will return immediately */
21353a2f7427SDavid Greenman 			}
21363a2f7427SDavid Greenman 		}
21375b81b6b3SRodney W. Grimes 
21386182fdbdSPeter Wemm 		if (format) {
21391a6bed68SJoerg Wunsch 			ne7cmd = NE7CMD_FORMAT | mfm;
21405f830ea2SJoerg Wunsch 			if (fdc->flags & FDC_NODMA) {
21415f830ea2SJoerg Wunsch 				/*
21425f830ea2SJoerg Wunsch 				 * This seems to be necessary for
21435f830ea2SJoerg Wunsch 				 * whatever obscure reason; if we omit
21445f830ea2SJoerg Wunsch 				 * it, we end up filling the sector ID
21455f830ea2SJoerg Wunsch 				 * fields of the newly formatted track
21465f830ea2SJoerg Wunsch 				 * entirely with garbage, causing
21475f830ea2SJoerg Wunsch 				 * `wrong cylinder' errors all over
21485f830ea2SJoerg Wunsch 				 * the place when trying to read them
21495f830ea2SJoerg Wunsch 				 * back.
21505f830ea2SJoerg Wunsch 				 *
21515f830ea2SJoerg Wunsch 				 * Umpf.
21525f830ea2SJoerg Wunsch 				 */
21538177437dSPoul-Henning Kamp 				SET_BCDR(fdc, 1, bp->bio_bcount, 0);
21545f830ea2SJoerg Wunsch 
21558177437dSPoul-Henning Kamp 				(void)fdcpio(fdc,bp->bio_cmd,
21568177437dSPoul-Henning Kamp 					bp->bio_data+fd->skip,
21578177437dSPoul-Henning Kamp 					bp->bio_bcount);
21585f830ea2SJoerg Wunsch 
21595f830ea2SJoerg Wunsch 			}
2160b39c878eSAndrey A. Chernov 			/* formatting */
21611a6bed68SJoerg Wunsch 			if(fd_cmd(fdc, 6,  ne7cmd, head << 2 | fdu,
2162dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
2163dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
2164dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
21656182fdbdSPeter Wemm 				  finfo->fd_formb_fillbyte, 0)) {
2166dc8603e3SJoerg Wunsch 				/* controller fell over */
21675f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
216856a23089SPoul-Henning Kamp 					isa_dmadone(idf,
21698177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
21708177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
21715c1a1eaeSBruce Evans 						    fdc->dmachan);
2172dc8603e3SJoerg Wunsch 				fdc->retry = 6;
21736182fdbdSPeter Wemm 				return (retrier(fdc));
2174dc8603e3SJoerg Wunsch 			}
2175250300ebSJoerg Wunsch 		} else if (rdsectid) {
21761a6bed68SJoerg Wunsch 			ne7cmd = NE7CMD_READID | mfm;
21771a6bed68SJoerg Wunsch 			if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) {
2178250300ebSJoerg Wunsch 				/* controller jamming */
2179250300ebSJoerg Wunsch 				fdc->retry = 6;
2180250300ebSJoerg Wunsch 				return (retrier(fdc));
2181250300ebSJoerg Wunsch 			}
21826182fdbdSPeter Wemm 		} else {
2183250300ebSJoerg Wunsch 			/* read or write operation */
21841a6bed68SJoerg Wunsch 			ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm;
21853b178206SWarner Losh 			if (fdc->flags & FDC_NODMA) {
218669acd21dSWarner Losh 				/*
2187246ed35dSJoerg Wunsch 				 * This seems to be necessary even when
2188246ed35dSJoerg Wunsch 				 * reading data.
218969acd21dSWarner Losh 				 */
21903b178206SWarner Losh 				SET_BCDR(fdc, 1, fdblk, 0);
219169acd21dSWarner Losh 
219269acd21dSWarner Losh 				/*
2193246ed35dSJoerg Wunsch 				 * Perform the write pseudo-DMA before
2194246ed35dSJoerg Wunsch 				 * the WRITE command is sent.
219569acd21dSWarner Losh 				 */
219669acd21dSWarner Losh 				if (!read)
21978177437dSPoul-Henning Kamp 					(void)fdcpio(fdc,bp->bio_cmd,
21988177437dSPoul-Henning Kamp 					    bp->bio_data+fd->skip,
219969acd21dSWarner Losh 					    fdblk);
220069acd21dSWarner Losh 			}
22016182fdbdSPeter Wemm 			if (fd_cmd(fdc, 9,
22021a6bed68SJoerg Wunsch 				   ne7cmd,
2203dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
2204dc5df763SJoerg Wunsch 				   fd->track,        /* track */
2205dc5df763SJoerg Wunsch 				   head,
2206dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
2207dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
2208dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
2209dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
2210dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
22116182fdbdSPeter Wemm 				   0)) {
2212dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
22135f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
221456a23089SPoul-Henning Kamp 					isa_dmadone(idf,
22158177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
22168177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
22175c1a1eaeSBruce Evans 						    fdc->dmachan);
2218dc8603e3SJoerg Wunsch 				fdc->retry = 6;
22196182fdbdSPeter Wemm 				return (retrier(fdc));
22205b81b6b3SRodney W. Grimes 			}
2221b39c878eSAndrey A. Chernov 		}
2222250300ebSJoerg Wunsch 		if (!rdsectid && (fdc->flags & FDC_NODMA))
222369acd21dSWarner Losh 			/*
2224246ed35dSJoerg Wunsch 			 * If this is a read, then simply await interrupt
2225246ed35dSJoerg Wunsch 			 * before performing PIO.
222669acd21dSWarner Losh 			 */
22278177437dSPoul-Henning Kamp 			if (read && !fdcpio(fdc,bp->bio_cmd,
22288177437dSPoul-Henning Kamp 			    bp->bio_data+fd->skip,fdblk)) {
22293b178206SWarner Losh 				fd->tohandle = timeout(fd_iotimeout, fdc, hz);
223069acd21dSWarner Losh 				return(0);      /* will return later */
223164860614SJoerg Wunsch 			}
223269acd21dSWarner Losh 
223369acd21dSWarner Losh 		/*
2234246ed35dSJoerg Wunsch 		 * Write (or format) operation will fall through and
2235246ed35dSJoerg Wunsch 		 * await completion interrupt.
223669acd21dSWarner Losh 		 */
22375b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
22386182fdbdSPeter Wemm 		fd->tohandle = timeout(fd_iotimeout, fdc, hz);
22395b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2240fb35bd37SJoerg Wunsch 
224169acd21dSWarner Losh 	case PIOREAD:
224269acd21dSWarner Losh 		/*
2243246ed35dSJoerg Wunsch 		 * Actually perform the PIO read.  The IOCOMPLETE case
224469acd21dSWarner Losh 		 * removes the timeout for us.
224569acd21dSWarner Losh 		 */
22468177437dSPoul-Henning Kamp 		(void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
224769acd21dSWarner Losh 		fdc->state = IOCOMPLETE;
224869acd21dSWarner Losh 		/* FALLTHROUGH */
2249246ed35dSJoerg Wunsch 	case IOCOMPLETE: /* IO done, post-analyze */
22506182fdbdSPeter Wemm 		untimeout(fd_iotimeout, fdc, fd->tohandle);
2251dc5df763SJoerg Wunsch 
225264860614SJoerg Wunsch 		if (fd_read_status(fdc)) {
2253250300ebSJoerg Wunsch 			if (!rdsectid && !(fdc->flags & FDC_NODMA))
22548177437dSPoul-Henning Kamp 				isa_dmadone(idf, bp->bio_data + fd->skip,
22558177437dSPoul-Henning Kamp 					    format ? bp->bio_bcount : fdblk,
22565c1a1eaeSBruce Evans 					    fdc->dmachan);
2257dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
2258dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
22596182fdbdSPeter Wemm 			return (retrier(fdc));
22605b81b6b3SRodney W. Grimes   		}
2261dc5df763SJoerg Wunsch 
22623a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
2263dc5df763SJoerg Wunsch 
22643a2f7427SDavid Greenman 		/* FALLTHROUGH */
22653a2f7427SDavid Greenman 	case IOTIMEDOUT:
2266250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
22678177437dSPoul-Henning Kamp 			isa_dmadone(idf, bp->bio_data + fd->skip,
22688177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
22696182fdbdSPeter Wemm 		if (fdc->status[0] & NE7_ST0_IC) {
22703a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
22713a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
2272b39c878eSAndrey A. Chernov                                 /*
22733fef646eSJoerg Wunsch 				 * DMA overrun. Someone hogged the bus and
22743fef646eSJoerg Wunsch 				 * didn't release it in time for the next
22753fef646eSJoerg Wunsch 				 * FDC transfer.
22763fef646eSJoerg Wunsch 				 *
22773fef646eSJoerg Wunsch 				 * We normally restart this without bumping
22783fef646eSJoerg Wunsch 				 * the retry counter.  However, in case
22793fef646eSJoerg Wunsch 				 * something is seriously messed up (like
22803fef646eSJoerg Wunsch 				 * broken hardware), we rather limit the
22813fef646eSJoerg Wunsch 				 * number of retries so the IO operation
22823fef646eSJoerg Wunsch 				 * doesn't block indefinately.
2283b39c878eSAndrey A. Chernov 				 */
22843fef646eSJoerg Wunsch 				if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2285b39c878eSAndrey A. Chernov 					fdc->state = SEEKCOMPLETE;
2286246ed35dSJoerg Wunsch 					return (1);/* will return immediately */
22873fef646eSJoerg Wunsch 				} /* else fall through */
2288b39c878eSAndrey A. Chernov                         }
22893fef646eSJoerg Wunsch 			if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
22903a2f7427SDavid Greenman 				&& fdc->retry < 6)
22913a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
22923a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
22933a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
22943a2f7427SDavid Greenman 				&& fdc->retry < 3)
22953a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
22966182fdbdSPeter Wemm 			return (retrier(fdc));
22975b81b6b3SRodney W. Grimes 		}
22985b81b6b3SRodney W. Grimes 		/* All OK */
2299250300ebSJoerg Wunsch 		if (rdsectid) {
2300250300ebSJoerg Wunsch 			/* copy out ID field contents */
230164860614SJoerg Wunsch 			idp = (struct fdc_readid *)bp->bio_data;
2302250300ebSJoerg Wunsch 			idp->cyl = fdc->status[3];
2303250300ebSJoerg Wunsch 			idp->head = fdc->status[4];
2304250300ebSJoerg Wunsch 			idp->sec = fdc->status[5];
2305250300ebSJoerg Wunsch 			idp->secshift = fdc->status[6];
2306250300ebSJoerg Wunsch 		}
23073fef646eSJoerg Wunsch 		/* Operation successful, retry DMA overruns again next time. */
23083fef646eSJoerg Wunsch 		fdc->dma_overruns = 0;
23093a2f7427SDavid Greenman 		fd->skip += fdblk;
2310fb35bd37SJoerg Wunsch 		if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
23115b81b6b3SRodney W. Grimes 			/* set up next transfer */
23125b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
23136182fdbdSPeter Wemm 		} else {
23145b81b6b3SRodney W. Grimes 			/* ALL DONE */
23155b81b6b3SRodney W. Grimes 			fd->skip = 0;
2316fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2317e93e63cbSBruce Evans 			fdc->bp = NULL;
23185f830ea2SJoerg Wunsch 			device_unbusy(fd->dev);
231980980460SPoul-Henning Kamp 			biofinish(bp, fd->device_stats, 0);
23205b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
23215b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
23225b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
23235b81b6b3SRodney W. Grimes 		}
2324246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2325fb35bd37SJoerg Wunsch 
23265b81b6b3SRodney W. Grimes 	case RESETCTLR:
23273a2f7427SDavid Greenman 		fdc_reset(fdc);
23285b81b6b3SRodney W. Grimes 		fdc->retry++;
23295c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
2330246ed35dSJoerg Wunsch 		return (0);	/* will return later */
2331fb35bd37SJoerg Wunsch 
23325c1a1eaeSBruce Evans 	case RESETCOMPLETE:
23335c1a1eaeSBruce Evans 		/*
23345c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
23355c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
23365c1a1eaeSBruce Evans 		 */
23370e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
23380e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
23395c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
2340fb35bd37SJoerg Wunsch 		/* FALLTHROUGH */
23415c1a1eaeSBruce Evans 	case STARTRECAL:
23426182fdbdSPeter Wemm 		if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2343dc8603e3SJoerg Wunsch 			/* arrgl */
2344dc8603e3SJoerg Wunsch 			fdc->retry = 6;
23456182fdbdSPeter Wemm 			return (retrier(fdc));
2346dc8603e3SJoerg Wunsch 		}
23475b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
23485b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2349fb35bd37SJoerg Wunsch 
23505b81b6b3SRodney W. Grimes 	case RECALWAIT:
23515b81b6b3SRodney W. Grimes 		/* allow heads to settle */
23526182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 8);
23535b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
23545b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2355fb35bd37SJoerg Wunsch 
23565b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
23573a2f7427SDavid Greenman 		do {
23583a2f7427SDavid Greenman 			/*
2359dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
23603a2f7427SDavid Greenman 			 */
2361dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2362246ed35dSJoerg Wunsch 				return (0); /* will return later */
2363dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
2364dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2365246ed35dSJoerg Wunsch 				return (0); /* hope for a real intr */
23663a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
23673a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
23685b81b6b3SRodney W. Grimes 		{
2369dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
2370dc8603e3SJoerg Wunsch 				/*
2371246ed35dSJoerg Wunsch 				 * A recalibrate from beyond cylinder 77
2372dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
2373dc8603e3SJoerg Wunsch 				 * since people used to complain much about
2374dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
2375dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
2376246ed35dSJoerg Wunsch 				 * time in a line.
2377dc8603e3SJoerg Wunsch 				 */
2378dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
2379dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
23803a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
23816182fdbdSPeter Wemm 			return (retrier(fdc));
23825b81b6b3SRodney W. Grimes 		}
23835b81b6b3SRodney W. Grimes 		fd->track = 0;
23845b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
23855b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
2386246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2387fb35bd37SJoerg Wunsch 
23885b81b6b3SRodney W. Grimes 	case MOTORWAIT:
23895b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
23905b81b6b3SRodney W. Grimes 		{
23915b81b6b3SRodney W. Grimes 			return (0); /* time's not up yet */
23925b81b6b3SRodney W. Grimes 		}
23935c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
23945c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
23955c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
2396fb35bd37SJoerg Wunsch 		} else
2397fb35bd37SJoerg Wunsch 			fdc->state = DOSEEK;
2398246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2399fb35bd37SJoerg Wunsch 
24005b81b6b3SRodney W. Grimes 	default:
2401b6e5f28eSPeter Wemm 		device_printf(fdc->fdc_dev, "unexpected FD int->");
240264860614SJoerg Wunsch 		if (fd_read_status(fdc) == 0)
2403a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
24045b81b6b3SRodney W. Grimes 			       fdc->status[0],
24055b81b6b3SRodney W. Grimes 			       fdc->status[1],
24065b81b6b3SRodney W. Grimes 			       fdc->status[2],
24075b81b6b3SRodney W. Grimes 			       fdc->status[3],
24085b81b6b3SRodney W. Grimes 			       fdc->status[4],
24095b81b6b3SRodney W. Grimes 			       fdc->status[5],
24105b81b6b3SRodney W. Grimes 			       fdc->status[6] );
24113a2f7427SDavid Greenman 		else
2412dac0f2dbSJoerg Wunsch 			printf("No status available   ");
2413dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
2414dac0f2dbSJoerg Wunsch 		{
2415dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
2416246ed35dSJoerg Wunsch 			return (0); /* will return later */
24175b81b6b3SRodney W. Grimes 		}
2418dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
2419246ed35dSJoerg Wunsch 		return (0);	/* will return later */
2420dac0f2dbSJoerg Wunsch 	}
2421246ed35dSJoerg Wunsch 	/* noone should ever get here */
24225b81b6b3SRodney W. Grimes }
24235b81b6b3SRodney W. Grimes 
2424aaf08d94SGarrett Wollman static int
24256182fdbdSPeter Wemm retrier(struct fdc_data *fdc)
24265b81b6b3SRodney W. Grimes {
24278177437dSPoul-Henning Kamp 	struct bio *bp;
24286182fdbdSPeter Wemm 	struct fd_data *fd;
24296182fdbdSPeter Wemm 	int fdu;
24305b81b6b3SRodney W. Grimes 
2431e93e63cbSBruce Evans 	bp = fdc->bp;
24325b81b6b3SRodney W. Grimes 
24336182fdbdSPeter Wemm 	/* XXX shouldn't this be cached somewhere?  */
2434a2f19df9SPoul-Henning Kamp 	fd = bp->bio_dev->si_drv1;
2435503799eaSPoul-Henning Kamp 	fdu = fd->fdu;
24366182fdbdSPeter Wemm 	if (fd->options & FDOPT_NORETRY)
24373a2f7427SDavid Greenman 		goto fail;
24386182fdbdSPeter Wemm 
24396182fdbdSPeter Wemm 	switch (fdc->retry) {
24405b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
24415b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
24425b81b6b3SRodney W. Grimes 		break;
24435b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
24445b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
24455b81b6b3SRodney W. Grimes 		break;
24465b81b6b3SRodney W. Grimes 	case 6:
24475b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
24485b81b6b3SRodney W. Grimes 		break;
24495b81b6b3SRodney W. Grimes 	case 7:
24505b81b6b3SRodney W. Grimes 		break;
24515b81b6b3SRodney W. Grimes 	default:
24523a2f7427SDavid Greenman 	fail:
2453fb35bd37SJoerg Wunsch 		if ((fd->options & FDOPT_NOERRLOG) == 0) {
2454f90c382cSPoul-Henning Kamp 			disk_err(bp, "hard error",
2455f90c382cSPoul-Henning Kamp 			    fdc->fd->skip / DEV_BSIZE, 0);
2456fb35bd37SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID) {
2457dc5df763SJoerg Wunsch 				printf(
2458a838d83dSBruce Evans 				" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2459dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
2460dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
2461dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
2462dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
2463dc5df763SJoerg Wunsch 				       fdc->status[5]);
2464dc5df763SJoerg Wunsch 			}
2465dc5df763SJoerg Wunsch 			else
2466dc5df763SJoerg Wunsch 				printf(" (No status)\n");
24675b81b6b3SRodney W. Grimes 		}
24682995d110SJoerg Wunsch 		if ((fd->options & FDOPT_NOERROR) == 0) {
24698177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
24708177437dSPoul-Henning Kamp 			bp->bio_error = EIO;
2471fb35bd37SJoerg Wunsch 			bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
2472fb35bd37SJoerg Wunsch 		} else
2473fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2474e93e63cbSBruce Evans 		fdc->bp = NULL;
24755b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
24765f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
247780980460SPoul-Henning Kamp 		biofinish(bp, fdc->fd->device_stats, 0);
247892ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
24795c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
24805b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
24815b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
248292ed385aSRodney W. Grimes 		return (1);
24835b81b6b3SRodney W. Grimes 	}
24845b81b6b3SRodney W. Grimes 	fdc->retry++;
24855b81b6b3SRodney W. Grimes 	return (1);
24865b81b6b3SRodney W. Grimes }
24875b81b6b3SRodney W. Grimes 
24881fdb6e6cSPoul-Henning Kamp static void
24891fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp)
24901fdb6e6cSPoul-Henning Kamp {
24911fdb6e6cSPoul-Henning Kamp 	wakeup(bp);
24921fdb6e6cSPoul-Henning Kamp }
24931fdb6e6cSPoul-Henning Kamp 
2494b39c878eSAndrey A. Chernov static int
2495f664aeeeSJoerg Wunsch fdmisccmd(dev_t dev, u_int cmd, void *data)
2496b39c878eSAndrey A. Chernov {
2497b39c878eSAndrey A. Chernov  	fdu_t fdu;
2498b39c878eSAndrey A. Chernov  	fd_p fd;
24991fdb6e6cSPoul-Henning Kamp 	struct bio *bp;
2500f664aeeeSJoerg Wunsch 	struct fd_formb *finfo;
2501f664aeeeSJoerg Wunsch 	struct fdc_readid *idfield;
25023a2f7427SDavid Greenman 	size_t fdblk;
250302aad38cSPoul-Henning Kamp 	int error;
2504b39c878eSAndrey A. Chernov 
2505a2f19df9SPoul-Henning Kamp 	fd = dev->si_drv1;
2506503799eaSPoul-Henning Kamp  	fdu = fd->fdu;
25073a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2508f664aeeeSJoerg Wunsch 	finfo = (struct fd_formb *)data;
2509f664aeeeSJoerg Wunsch 	idfield = (struct fdc_readid *)data;
2510b39c878eSAndrey A. Chernov 
25118595de4aSPoul-Henning Kamp 	bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
2512b39c878eSAndrey A. Chernov 
2513b39c878eSAndrey A. Chernov 	/*
2514b52b7f46SPoul-Henning Kamp 	 * Set up a bio request for fdstrategy().  bio_offset is faked
2515f664aeeeSJoerg Wunsch 	 * so that fdstrategy() will seek to the the requested
2516817988beSPoul-Henning Kamp 	 * cylinder, and use the desired head.
2517b39c878eSAndrey A. Chernov 	 */
2518f664aeeeSJoerg Wunsch 	bp->bio_cmd = cmd;
2519419f39ceSPoul-Henning Kamp 	if (cmd == FDBIO_FORMAT) {
2520b52b7f46SPoul-Henning Kamp 		bp->bio_offset =
2521f664aeeeSJoerg Wunsch 		    (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
2522b52b7f46SPoul-Henning Kamp 		     finfo->head * fd->ft->sectrac) * fdblk;
2523f664aeeeSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fd_idfield_data) *
2524f664aeeeSJoerg Wunsch 		    finfo->fd_formb_nsecs;
2525419f39ceSPoul-Henning Kamp 	} else if (cmd == FDBIO_RDSECTID) {
2526b52b7f46SPoul-Henning Kamp 		bp->bio_offset =
2527f664aeeeSJoerg Wunsch 		    (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
2528b52b7f46SPoul-Henning Kamp 		     idfield->head * fd->ft->sectrac) * fdblk;
2529250300ebSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fdc_readid);
2530f664aeeeSJoerg Wunsch 	} else
2531f664aeeeSJoerg Wunsch 		panic("wrong cmd in fdmisccmd()");
2532f664aeeeSJoerg Wunsch 	bp->bio_data = data;
2533250300ebSJoerg Wunsch 	bp->bio_dev = dev;
2534250300ebSJoerg Wunsch 	bp->bio_done = fdbiodone;
2535817988beSPoul-Henning Kamp 	bp->bio_flags = 0;
2536250300ebSJoerg Wunsch 
2537c3bdb2f7SPoul-Henning Kamp 	/* Now run the command. */
2538f664aeeeSJoerg Wunsch 	fdstrategy(bp);
253902aad38cSPoul-Henning Kamp 	error = biowait(bp, "fdcmd");
2540c3bdb2f7SPoul-Henning Kamp 
2541f664aeeeSJoerg Wunsch 	free(bp, M_TEMP);
254202aad38cSPoul-Henning Kamp 	return (error);
2543f664aeeeSJoerg Wunsch }
25445b81b6b3SRodney W. Grimes 
25453e425b96SJulian Elischer static int
2546b40ce416SJulian Elischer fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
2547f5f7ba03SJordan K. Hubbard {
2548f664aeeeSJoerg Wunsch  	fdu_t fdu;
2549f664aeeeSJoerg Wunsch  	fd_p fd;
25502995d110SJoerg Wunsch 	struct fdc_status *fsp;
2551250300ebSJoerg Wunsch 	struct fdc_readid *rid;
2552503799eaSPoul-Henning Kamp 	int error;
2553f5f7ba03SJordan K. Hubbard 
2554a2f19df9SPoul-Henning Kamp  	fd = dev->si_drv1;
2555503799eaSPoul-Henning Kamp  	fdu = fd->fdu;
2556fb35bd37SJoerg Wunsch 
25571a6bed68SJoerg Wunsch 	/*
25581a6bed68SJoerg Wunsch 	 * First, handle everything that could be done with
25591a6bed68SJoerg Wunsch 	 * FD_NONBLOCK still being set.
25601a6bed68SJoerg Wunsch 	 */
25611a6bed68SJoerg Wunsch 	switch (cmd) {
2562c91a63aaSPoul-Henning Kamp 
2563c91a63aaSPoul-Henning Kamp 	case DIOCGMEDIASIZE:
256429ec21d1SYaroslav Tykhiy 		if (fd->ft == 0)
256529ec21d1SYaroslav Tykhiy 			return ((fd->flags & FD_NONBLOCK) ? EAGAIN : ENXIO);
2566c91a63aaSPoul-Henning Kamp 		*(off_t *)addr = (128 << (fd->ft->secsize)) * fd->ft->size;
2567c91a63aaSPoul-Henning Kamp 		return (0);
2568c91a63aaSPoul-Henning Kamp 
2569c91a63aaSPoul-Henning Kamp 	case DIOCGSECTORSIZE:
257029ec21d1SYaroslav Tykhiy 		if (fd->ft == 0)
257129ec21d1SYaroslav Tykhiy 			return ((fd->flags & FD_NONBLOCK) ? EAGAIN : ENXIO);
2572c91a63aaSPoul-Henning Kamp 		*(u_int *)addr = 128 << (fd->ft->secsize);
2573c91a63aaSPoul-Henning Kamp 		return (0);
2574c91a63aaSPoul-Henning Kamp 
25751a6bed68SJoerg Wunsch 	case FIONBIO:
25761a6bed68SJoerg Wunsch 		if (*(int *)addr != 0)
25771a6bed68SJoerg Wunsch 			fd->flags |= FD_NONBLOCK;
25781a6bed68SJoerg Wunsch 		else {
25791a6bed68SJoerg Wunsch 			if (fd->ft == 0) {
25801a6bed68SJoerg Wunsch 				/*
25811a6bed68SJoerg Wunsch 				 * No drive type has been selected yet,
25821a6bed68SJoerg Wunsch 				 * cannot turn FNONBLOCK off.
25831a6bed68SJoerg Wunsch 				 */
25841a6bed68SJoerg Wunsch 				return (EINVAL);
25851a6bed68SJoerg Wunsch 			}
25861a6bed68SJoerg Wunsch 			fd->flags &= ~FD_NONBLOCK;
25871a6bed68SJoerg Wunsch 		}
25881a6bed68SJoerg Wunsch 		return (0);
2589fb35bd37SJoerg Wunsch 
25901a6bed68SJoerg Wunsch 	case FIOASYNC:
25911a6bed68SJoerg Wunsch 		/* keep the generic fcntl() code happy */
25921a6bed68SJoerg Wunsch 		return (0);
25931a6bed68SJoerg Wunsch 
25941a6bed68SJoerg Wunsch 	case FD_GTYPE:                  /* get drive type */
25951a6bed68SJoerg Wunsch 		if (fd->ft == 0)
25961a6bed68SJoerg Wunsch 			/* no type known yet, return the native type */
25971a6bed68SJoerg Wunsch 			*(struct fd_type *)addr = fd_native_types[fd->type];
25981a6bed68SJoerg Wunsch 		else
25991a6bed68SJoerg Wunsch 			*(struct fd_type *)addr = *fd->ft;
26001a6bed68SJoerg Wunsch 		return (0);
26011a6bed68SJoerg Wunsch 
26021a6bed68SJoerg Wunsch 	case FD_STYPE:                  /* set drive type */
2603a3a10d1cSBruce Evans 		/*
2604a3a10d1cSBruce Evans 		 * Allow setting drive type temporarily iff
2605a3a10d1cSBruce Evans 		 * currently unset.  Used for fdformat so any
2606a3a10d1cSBruce Evans 		 * user can set it, and then start formatting.
2607a3a10d1cSBruce Evans 		 */
2608a3a10d1cSBruce Evans 		if (fd->ft)
2609a3a10d1cSBruce Evans 			return (EINVAL); /* already set */
2610503799eaSPoul-Henning Kamp 		fd->fts[0] = *(struct fd_type *)addr;
2611a3a10d1cSBruce Evans 		fd->ft = &fd->fts[0];
2612a3a10d1cSBruce Evans 		fd->flags |= FD_UA;
26131a6bed68SJoerg Wunsch 		return (0);
26141a6bed68SJoerg Wunsch 
26151a6bed68SJoerg Wunsch 	case FD_GOPTS:			/* get drive options */
2616503799eaSPoul-Henning Kamp 		*(int *)addr = fd->options + FDOPT_AUTOSEL;
26171a6bed68SJoerg Wunsch 		return (0);
26181a6bed68SJoerg Wunsch 
26191a6bed68SJoerg Wunsch 	case FD_SOPTS:			/* set drive options */
26201a6bed68SJoerg Wunsch 		fd->options = *(int *)addr & ~FDOPT_AUTOSEL;
26211a6bed68SJoerg Wunsch 		return (0);
26221a6bed68SJoerg Wunsch 
26231a6bed68SJoerg Wunsch #ifdef FDC_DEBUG
26241a6bed68SJoerg Wunsch 	case FD_DEBUG:
26251a6bed68SJoerg Wunsch 		if ((fd_debug != 0) != (*(int *)addr != 0)) {
26261a6bed68SJoerg Wunsch 			fd_debug = (*(int *)addr != 0);
26271a6bed68SJoerg Wunsch 			printf("fd%d: debugging turned %s\n",
26281a6bed68SJoerg Wunsch 			    fd->fdu, fd_debug ? "on" : "off");
26291a6bed68SJoerg Wunsch 		}
26301a6bed68SJoerg Wunsch 		return (0);
26311a6bed68SJoerg Wunsch #endif
26321a6bed68SJoerg Wunsch 
26331a6bed68SJoerg Wunsch 	case FD_CLRERR:
263444731cabSJohn Baldwin 		if (suser(td) != 0)
26351a6bed68SJoerg Wunsch 			return (EPERM);
26361a6bed68SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
26371a6bed68SJoerg Wunsch 		return (0);
26381a6bed68SJoerg Wunsch 
26391a6bed68SJoerg Wunsch 	case FD_GSTAT:
26401a6bed68SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
26411a6bed68SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
26421a6bed68SJoerg Wunsch 			return (EINVAL);
26431a6bed68SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
26441a6bed68SJoerg Wunsch 		return (0);
26451a6bed68SJoerg Wunsch 
26461a6bed68SJoerg Wunsch 	case FD_GDTYPE:
26471a6bed68SJoerg Wunsch 		*(enum fd_drivetype *)addr = fd->type;
26481a6bed68SJoerg Wunsch 		return (0);
26491a6bed68SJoerg Wunsch 	}
26501a6bed68SJoerg Wunsch 
26511a6bed68SJoerg Wunsch 	/*
26521a6bed68SJoerg Wunsch 	 * Now handle everything else.  Make sure we have a valid
26531a6bed68SJoerg Wunsch 	 * drive type.
26541a6bed68SJoerg Wunsch 	 */
26551a6bed68SJoerg Wunsch 	if (fd->flags & FD_NONBLOCK)
26561a6bed68SJoerg Wunsch 		return (EAGAIN);
26571a6bed68SJoerg Wunsch 	if (fd->ft == 0)
26581a6bed68SJoerg Wunsch 		return (ENXIO);
2659fb35bd37SJoerg Wunsch 	error = 0;
2660f5f7ba03SJordan K. Hubbard 
26616182fdbdSPeter Wemm 	switch (cmd) {
2662f664aeeeSJoerg Wunsch 
2663b39c878eSAndrey A. Chernov 	case FD_FORM:
2664b39c878eSAndrey A. Chernov 		if ((flag & FWRITE) == 0)
2665fb35bd37SJoerg Wunsch 			return (EBADF);	/* must be opened for writing */
2666fb35bd37SJoerg Wunsch 		if (((struct fd_formb *)addr)->format_version !=
2667b39c878eSAndrey A. Chernov 		    FD_FORMAT_VERSION)
2668fb35bd37SJoerg Wunsch 			return (EINVAL); /* wrong version of formatting prog */
2669419f39ceSPoul-Henning Kamp 		error = fdmisccmd(dev, FDBIO_FORMAT, addr);
2670b39c878eSAndrey A. Chernov 		break;
2671b39c878eSAndrey A. Chernov 
2672b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
26733e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2674f5f7ba03SJordan K. Hubbard 		break;
2675f5f7ba03SJordan K. Hubbard 
26763a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
26773a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
267844731cabSJohn Baldwin 		if (suser(td) != 0)
2679fb35bd37SJoerg Wunsch 			return (EPERM);
26803e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
26813a2f7427SDavid Greenman 		break;
26823a2f7427SDavid Greenman 
26833a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
26843e425b96SJulian Elischer 		*(int *)addr = fd->options;
26853a2f7427SDavid Greenman 		break;
26863a2f7427SDavid Greenman 
26873a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
26883e425b96SJulian Elischer 		fd->options = *(int *)addr;
26893a2f7427SDavid Greenman 		break;
26903a2f7427SDavid Greenman 
2691f664aeeeSJoerg Wunsch #ifdef FDC_DEBUG
2692f664aeeeSJoerg Wunsch 	case FD_DEBUG:
26930e17a5bcSJoerg Wunsch 		if ((fd_debug != 0) != (*(int *)addr != 0)) {
26940e17a5bcSJoerg Wunsch 			fd_debug = (*(int *)addr != 0);
26950e17a5bcSJoerg Wunsch 			printf("fd%d: debugging turned %s\n",
26960e17a5bcSJoerg Wunsch 			    fd->fdu, fd_debug ? "on" : "off");
26970e17a5bcSJoerg Wunsch 		}
2698f664aeeeSJoerg Wunsch 		break;
2699f664aeeeSJoerg Wunsch #endif
2700f664aeeeSJoerg Wunsch 
27012995d110SJoerg Wunsch 	case FD_CLRERR:
270244731cabSJohn Baldwin 		if (suser(td) != 0)
2703fb35bd37SJoerg Wunsch 			return (EPERM);
27042995d110SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
27052995d110SJoerg Wunsch 		break;
27062995d110SJoerg Wunsch 
27072995d110SJoerg Wunsch 	case FD_GSTAT:
27082995d110SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
27092995d110SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2710fb35bd37SJoerg Wunsch 			return (EINVAL);
27112995d110SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
27122995d110SJoerg Wunsch 		break;
27132995d110SJoerg Wunsch 
2714250300ebSJoerg Wunsch 	case FD_READID:
2715250300ebSJoerg Wunsch 		rid = (struct fdc_readid *)addr;
2716250300ebSJoerg Wunsch 		if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2717fb35bd37SJoerg Wunsch 			return (EINVAL);
2718419f39ceSPoul-Henning Kamp 		error = fdmisccmd(dev, FDBIO_RDSECTID, addr);
2719250300ebSJoerg Wunsch 		break;
2720250300ebSJoerg Wunsch 
2721f5f7ba03SJordan K. Hubbard 	default:
27223a2f7427SDavid Greenman 		error = ENOTTY;
2723f5f7ba03SJordan K. Hubbard 		break;
2724f5f7ba03SJordan K. Hubbard 	}
2725f5f7ba03SJordan K. Hubbard 	return (error);
2726f5f7ba03SJordan K. Hubbard }
2727