xref: /freebsd/sys/dev/fdc/fdc.c (revision 294d88444b7de0eaf12ff654ec9e6a7c3ce4a615)
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  * 3. All advertising materials mentioning features or use of this software
3364860614SJoerg Wunsch  *    must display the following acknowledgement:
3464860614SJoerg Wunsch  *	This product includes software developed by the University of
3564860614SJoerg Wunsch  *	California, Berkeley and its contributors.
3664860614SJoerg Wunsch  * 4. Neither the name of the University nor the names of its contributors
3764860614SJoerg Wunsch  *    may be used to endorse or promote products derived from this software
3864860614SJoerg Wunsch  *    without specific prior written permission.
395b81b6b3SRodney W. Grimes  *
405b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
415b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
425b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
435b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
445b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
455b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
465b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
475b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
485b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
495b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
505b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
515b81b6b3SRodney W. Grimes  *
52dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
53c3aac50fSPeter Wemm  * $FreeBSD$
545b81b6b3SRodney W. Grimes  */
555b81b6b3SRodney W. Grimes 
56d2fb4892SJoerg Wunsch #include "opt_fdc.h"
575f830ea2SJoerg Wunsch #include "card.h"
585b81b6b3SRodney W. Grimes 
59b99f0a4aSAndrew Moore #include <sys/param.h>
60b99f0a4aSAndrew Moore #include <sys/systm.h>
619626b608SPoul-Henning Kamp #include <sys/bio.h>
626182fdbdSPeter Wemm #include <sys/bus.h>
636182fdbdSPeter Wemm #include <sys/conf.h>
64b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h>
65f90c382cSPoul-Henning Kamp #include <sys/disk.h>
666182fdbdSPeter Wemm #include <sys/fcntl.h>
67e774b251SJoerg Wunsch #include <sys/fdcio.h>
681a6bed68SJoerg Wunsch #include <sys/filio.h>
69fb919e4dSMark Murray #include <sys/kernel.h>
70fb919e4dSMark Murray #include <sys/lock.h>
71b99f0a4aSAndrew Moore #include <sys/malloc.h>
726182fdbdSPeter Wemm #include <sys/module.h>
73fb919e4dSMark Murray #include <sys/mutex.h>
743a2f7427SDavid Greenman #include <sys/proc.h>
75b99f0a4aSAndrew Moore #include <sys/syslog.h>
766182fdbdSPeter Wemm 
776182fdbdSPeter Wemm #include <machine/bus.h>
786182fdbdSPeter Wemm #include <sys/rman.h>
796182fdbdSPeter Wemm 
806182fdbdSPeter Wemm #include <machine/clock.h>
816182fdbdSPeter Wemm #include <machine/resource.h>
82dc5df763SJoerg Wunsch #include <machine/stdarg.h>
836182fdbdSPeter Wemm 
846182fdbdSPeter Wemm #include <isa/isavar.h>
85a97c75b7SDoug Rabson #include <isa/isareg.h>
86a97c75b7SDoug Rabson #include <isa/fdreg.h>
87a97c75b7SDoug Rabson #include <isa/rtc.h>
886182fdbdSPeter Wemm 
89246ed35dSJoerg Wunsch enum fdc_type
90246ed35dSJoerg Wunsch {
911a6bed68SJoerg Wunsch 	FDC_NE765, FDC_ENHANCED, FDC_UNKNOWN = -1
92246ed35dSJoerg Wunsch };
93246ed35dSJoerg Wunsch 
94246ed35dSJoerg Wunsch enum fdc_states {
95246ed35dSJoerg Wunsch 	DEVIDLE,
96246ed35dSJoerg Wunsch 	FINDWORK,
97246ed35dSJoerg Wunsch 	DOSEEK,
98246ed35dSJoerg Wunsch 	SEEKCOMPLETE ,
99246ed35dSJoerg Wunsch 	IOCOMPLETE,
100246ed35dSJoerg Wunsch 	RECALCOMPLETE,
101246ed35dSJoerg Wunsch 	STARTRECAL,
102246ed35dSJoerg Wunsch 	RESETCTLR,
103246ed35dSJoerg Wunsch 	SEEKWAIT,
104246ed35dSJoerg Wunsch 	RECALWAIT,
105246ed35dSJoerg Wunsch 	MOTORWAIT,
106246ed35dSJoerg Wunsch 	IOTIMEDOUT,
107246ed35dSJoerg Wunsch 	RESETCOMPLETE,
108246ed35dSJoerg Wunsch 	PIOREAD
109246ed35dSJoerg Wunsch };
110246ed35dSJoerg Wunsch 
111246ed35dSJoerg Wunsch #ifdef	FDC_DEBUG
112246ed35dSJoerg Wunsch static char const * const fdstates[] = {
113246ed35dSJoerg Wunsch 	"DEVIDLE",
114246ed35dSJoerg Wunsch 	"FINDWORK",
115246ed35dSJoerg Wunsch 	"DOSEEK",
116246ed35dSJoerg Wunsch 	"SEEKCOMPLETE",
117246ed35dSJoerg Wunsch 	"IOCOMPLETE",
118246ed35dSJoerg Wunsch 	"RECALCOMPLETE",
119246ed35dSJoerg Wunsch 	"STARTRECAL",
120246ed35dSJoerg Wunsch 	"RESETCTLR",
121246ed35dSJoerg Wunsch 	"SEEKWAIT",
122246ed35dSJoerg Wunsch 	"RECALWAIT",
123246ed35dSJoerg Wunsch 	"MOTORWAIT",
124246ed35dSJoerg Wunsch 	"IOTIMEDOUT",
125246ed35dSJoerg Wunsch 	"RESETCOMPLETE",
126246ed35dSJoerg Wunsch 	"PIOREAD"
127246ed35dSJoerg Wunsch };
128246ed35dSJoerg Wunsch #endif
129246ed35dSJoerg Wunsch 
130246ed35dSJoerg Wunsch /*
131246ed35dSJoerg Wunsch  * Per controller structure (softc).
132246ed35dSJoerg Wunsch  */
133246ed35dSJoerg Wunsch struct fdc_data
134246ed35dSJoerg Wunsch {
135246ed35dSJoerg Wunsch 	int	fdcu;		/* our unit number */
136246ed35dSJoerg Wunsch 	int	dmachan;
137246ed35dSJoerg Wunsch 	int	flags;
138246ed35dSJoerg Wunsch #define FDC_ATTACHED	0x01
139246ed35dSJoerg Wunsch #define FDC_STAT_VALID	0x08
140246ed35dSJoerg Wunsch #define FDC_HAS_FIFO	0x10
141246ed35dSJoerg Wunsch #define FDC_NEEDS_RESET	0x20
142246ed35dSJoerg Wunsch #define FDC_NODMA	0x40
143246ed35dSJoerg Wunsch #define FDC_ISPNP	0x80
144246ed35dSJoerg Wunsch #define FDC_ISPCMCIA	0x100
145246ed35dSJoerg Wunsch 	struct	fd_data *fd;
146246ed35dSJoerg Wunsch 	int	fdu;		/* the active drive	*/
147246ed35dSJoerg Wunsch 	enum	fdc_states state;
148246ed35dSJoerg Wunsch 	int	retry;
149246ed35dSJoerg Wunsch 	int	fdout;		/* mirror of the w/o digital output reg */
150246ed35dSJoerg Wunsch 	u_int	status[7];	/* copy of the registers */
151246ed35dSJoerg Wunsch 	enum	fdc_type fdct;	/* chip version of FDC */
152246ed35dSJoerg Wunsch 	int	fdc_errs;	/* number of logged errors */
153246ed35dSJoerg Wunsch 	int	dma_overruns;	/* number of DMA overruns */
154246ed35dSJoerg Wunsch 	struct	bio_queue_head head;
155246ed35dSJoerg Wunsch 	struct	bio *bp;	/* active buffer */
156246ed35dSJoerg Wunsch 	struct	resource *res_ioport, *res_ctl, *res_irq, *res_drq;
157246ed35dSJoerg Wunsch 	int	rid_ioport, rid_ctl, rid_irq, rid_drq;
158246ed35dSJoerg Wunsch 	int	port_off;
159246ed35dSJoerg Wunsch 	bus_space_tag_t portt;
160246ed35dSJoerg Wunsch 	bus_space_handle_t porth;
161246ed35dSJoerg Wunsch 	bus_space_tag_t ctlt;
162246ed35dSJoerg Wunsch 	bus_space_handle_t ctlh;
163246ed35dSJoerg Wunsch 	void	*fdc_intr;
164246ed35dSJoerg Wunsch 	struct	device *fdc_dev;
165246ed35dSJoerg Wunsch 	void	(*fdctl_wr)(struct fdc_data *fdc, u_int8_t v);
166246ed35dSJoerg Wunsch };
167246ed35dSJoerg Wunsch 
168419f39ceSPoul-Henning Kamp #define FDBIO_FORMAT	BIO_CMD2
169d306122dSPoul-Henning Kamp 
170246ed35dSJoerg Wunsch typedef int	fdu_t;
171246ed35dSJoerg Wunsch typedef int	fdcu_t;
172246ed35dSJoerg Wunsch typedef int	fdsu_t;
173246ed35dSJoerg Wunsch typedef	struct fd_data *fd_p;
174246ed35dSJoerg Wunsch typedef struct fdc_data *fdc_p;
175246ed35dSJoerg Wunsch typedef enum fdc_type fdc_t;
176246ed35dSJoerg Wunsch 
177246ed35dSJoerg Wunsch #define FDUNIT(s)	(((s) >> 6) & 3)
1781a6bed68SJoerg Wunsch #define FDNUMTOUNIT(n)	(((n) & 3) << 6)
179246ed35dSJoerg Wunsch #define FDTYPE(s)	((s) & 0x3f)
180246ed35dSJoerg Wunsch 
181246ed35dSJoerg Wunsch /*
182246ed35dSJoerg Wunsch  * fdc maintains a set (1!) of ivars per child of each controller.
183246ed35dSJoerg Wunsch  */
184246ed35dSJoerg Wunsch enum fdc_device_ivars {
185246ed35dSJoerg Wunsch 	FDC_IVAR_FDUNIT,
186246ed35dSJoerg Wunsch };
187246ed35dSJoerg Wunsch 
188246ed35dSJoerg Wunsch /*
189246ed35dSJoerg Wunsch  * Simple access macros for the ivars.
190246ed35dSJoerg Wunsch  */
191246ed35dSJoerg Wunsch #define FDC_ACCESSOR(A, B, T)						\
192246ed35dSJoerg Wunsch static __inline T fdc_get_ ## A(device_t dev)				\
193246ed35dSJoerg Wunsch {									\
194246ed35dSJoerg Wunsch 	uintptr_t v;							\
195246ed35dSJoerg Wunsch 	BUS_READ_IVAR(device_get_parent(dev), dev, FDC_IVAR_ ## B, &v);	\
196246ed35dSJoerg Wunsch 	return (T) v;							\
197246ed35dSJoerg Wunsch }
198246ed35dSJoerg Wunsch FDC_ACCESSOR(fdunit,	FDUNIT,	int)
199246ed35dSJoerg Wunsch 
2001a6bed68SJoerg Wunsch /* configuration flags for fdc */
201e34c71eaSJoerg Wunsch #define FDC_NO_FIFO	(1 << 2)	/* do not enable FIFO  */
2020722d6abSJoerg Wunsch 
203dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
204dc5df763SJoerg Wunsch #define FD_FAILED -1
205dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
206dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
2073fef646eSJoerg Wunsch /*
2083fef646eSJoerg Wunsch  * Stop retrying after this many DMA overruns.  Since each retry takes
20983edbfa5SJoerg Wunsch  * one revolution, with 300 rpm., 25 retries take approximately 5
2103fef646eSJoerg Wunsch  * seconds which the read attempt will block in case the DMA overrun
2113fef646eSJoerg Wunsch  * is persistent.
2123fef646eSJoerg Wunsch  */
2133fef646eSJoerg Wunsch #define FDC_DMAOV_MAX	25
214dc5df763SJoerg Wunsch 
2151a6bed68SJoerg Wunsch /*
216cb38bb6cSJoerg Wunsch  * Timeout value for the PIO loops to wait until the FDC main status
217cb38bb6cSJoerg Wunsch  * register matches our expectations (request for master, direction
218cb38bb6cSJoerg Wunsch  * bit).  This is supposed to be a number of microseconds, although
219cb38bb6cSJoerg Wunsch  * timing might actually not be very accurate.
220cb38bb6cSJoerg Wunsch  *
221cb38bb6cSJoerg Wunsch  * Timeouts of 100 msec are believed to be required for some broken
222cb38bb6cSJoerg Wunsch  * (old) hardware.
223cb38bb6cSJoerg Wunsch  */
224cb38bb6cSJoerg Wunsch #define	FDSTS_TIMEOUT	100000
225cb38bb6cSJoerg Wunsch 
226cb38bb6cSJoerg Wunsch /*
2271a6bed68SJoerg Wunsch  * Number of subdevices that can be used for different density types.
2281a6bed68SJoerg Wunsch  * By now, the lower 6 bit of the minor number are reserved for this,
2291a6bed68SJoerg Wunsch  * allowing for up to 64 subdevices, but we only use 16 out of this.
2301a6bed68SJoerg Wunsch  * Density #0 is used for automatic format detection, the other
2311a6bed68SJoerg Wunsch  * densities are available as programmable densities (for assignment
2321a6bed68SJoerg Wunsch  * by fdcontrol(8)).
2331a6bed68SJoerg Wunsch  * The upper 2 bits of the minor number are reserved for the subunit
2341a6bed68SJoerg Wunsch  * (drive #) per controller.
2351a6bed68SJoerg Wunsch  */
2361a6bed68SJoerg Wunsch #define NUMDENS		16
237b99f0a4aSAndrew Moore 
238419f39ceSPoul-Henning Kamp #define FDBIO_RDSECTID	BIO_CMD1
2397ca0641bSAndrey A. Chernov 
2401a6bed68SJoerg Wunsch /*
2411a6bed68SJoerg Wunsch  * List of native drive densities.  Order must match enum fd_drivetype
2421a6bed68SJoerg Wunsch  * in <sys/fdcio.h>.  Upon attaching the drive, each of the
2431a6bed68SJoerg Wunsch  * programmable subdevices is initialized with the native density
2441a6bed68SJoerg Wunsch  * definition.
2451a6bed68SJoerg Wunsch  */
2461a6bed68SJoerg Wunsch static struct fd_type fd_native_types[] =
2475b81b6b3SRodney W. Grimes {
2481a6bed68SJoerg Wunsch { 0 },				/* FDT_NONE */
2491a6bed68SJoerg Wunsch {  9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */
2501a6bed68SJoerg Wunsch { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M  */
2511a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */
2521a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
2531a6bed68SJoerg Wunsch #if 0				/* we currently don't handle 2.88 MB */
2541a6bed68SJoerg Wunsch { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS,  2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/
2551a6bed68SJoerg Wunsch #else
2561a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
2571a6bed68SJoerg Wunsch #endif
2585b81b6b3SRodney W. Grimes };
2595b81b6b3SRodney W. Grimes 
2601a6bed68SJoerg Wunsch /*
2611a6bed68SJoerg Wunsch  * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density
2621a6bed68SJoerg Wunsch  * selection, they just start out with their native density (or lose).
2631a6bed68SJoerg Wunsch  * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their
2641a6bed68SJoerg Wunsch  * respective lists of densities to search for.
2651a6bed68SJoerg Wunsch  */
2661a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_12m[] = {
2671a6bed68SJoerg Wunsch { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */
2681a6bed68SJoerg Wunsch {  9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */
2691a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2701a6bed68SJoerg Wunsch };
2711a6bed68SJoerg Wunsch 
2721a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_144m[] = {
2731a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
2741a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2751a6bed68SJoerg Wunsch };
2761a6bed68SJoerg Wunsch 
2771a6bed68SJoerg Wunsch /* We search for 1.44M first since this is the most common case. */
2781a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_288m[] = {
2791a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
2801a6bed68SJoerg Wunsch #if 0
2811a6bed68SJoerg Wunsch { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS,  2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */
2821a6bed68SJoerg Wunsch #endif
2831a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2841a6bed68SJoerg Wunsch };
285dc16046fSJoerg Wunsch 
286f8ce7dd5SJoerg Wunsch #define MAX_SEC_SIZE	(128 << 3)
28764860614SJoerg Wunsch #define MAX_CYLINDER	85	/* some people really stress their drives
28864860614SJoerg Wunsch 				 * up to cyl 82 */
289250300ebSJoerg Wunsch #define MAX_HEAD	1
290f8ce7dd5SJoerg Wunsch 
2916182fdbdSPeter Wemm static devclass_t fdc_devclass;
2925b81b6b3SRodney W. Grimes 
293246ed35dSJoerg Wunsch /*
294246ed35dSJoerg Wunsch  * Per drive structure (softc).
295246ed35dSJoerg Wunsch  */
2966182fdbdSPeter Wemm struct fd_data {
297b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2985b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2991a6bed68SJoerg Wunsch 	enum	fd_drivetype type; /* drive type */
3001a6bed68SJoerg Wunsch 	struct	fd_type *ft;	/* pointer to current type descriptor */
3011a6bed68SJoerg Wunsch 	struct	fd_type fts[NUMDENS]; /* type descriptors */
3025b81b6b3SRodney W. Grimes 	int	flags;
3035b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
3041a6bed68SJoerg Wunsch #define	FD_NONBLOCK	0x02	/* O_NONBLOCK set	*/
3051a6bed68SJoerg Wunsch #define	FD_ACTIVE	0x04	/* it's active		*/
3061a6bed68SJoerg Wunsch #define	FD_MOTOR	0x08	/* motor should be on	*/
3071a6bed68SJoerg Wunsch #define	FD_MOTOR_WAIT	0x10	/* motor coming up	*/
3081a6bed68SJoerg Wunsch #define	FD_UA		0x20	/* force unit attention */
3095b81b6b3SRodney W. Grimes 	int	skip;
3105b81b6b3SRodney W. Grimes 	int	hddrv;
311dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
3125b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
3131a6bed68SJoerg Wunsch 	int	options;	/* user configurable options, see fdcio.h */
31402a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
31502a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
316b2dfb1f9SJustin T. Gibbs 	struct	devstat device_stats;
317e219897aSJoerg Wunsch 	eventhandler_tag clonetag;
318e219897aSJoerg Wunsch 	dev_t	masterdev;
3191a6bed68SJoerg Wunsch 	dev_t	clonedevs[NUMDENS - 1];
3206182fdbdSPeter Wemm 	device_t dev;
3216182fdbdSPeter Wemm 	fdu_t	fdu;
3226182fdbdSPeter Wemm };
32337286586SPeter Wemm 
32437286586SPeter Wemm struct fdc_ivars {
32537286586SPeter Wemm 	int	fdunit;
32637286586SPeter Wemm };
3276182fdbdSPeter Wemm static devclass_t fd_devclass;
3285b81b6b3SRodney W. Grimes 
3291a6bed68SJoerg Wunsch /* configuration flags for fd */
3301a6bed68SJoerg Wunsch #define FD_TYPEMASK	0x0f	/* drive type, matches enum
3311a6bed68SJoerg Wunsch 				 * fd_drivetype; on i386 machines, if
3321a6bed68SJoerg Wunsch 				 * given as 0, use RTC type for fd0
3331a6bed68SJoerg Wunsch 				 * and fd1 */
3341a6bed68SJoerg Wunsch #define FD_DTYPE(flags)	((flags) & FD_TYPEMASK)
3351a6bed68SJoerg Wunsch #define FD_NO_CHLINE	0x10	/* drive does not support changeline
3361a6bed68SJoerg Wunsch 				 * aka. unit attention */
3371a6bed68SJoerg Wunsch #define FD_NO_PROBE	0x20	/* don't probe drive (seek test), just
3381a6bed68SJoerg Wunsch 				 * assume it is there */
3391a6bed68SJoerg Wunsch 
340246ed35dSJoerg Wunsch /*
341246ed35dSJoerg Wunsch  * Throughout this file the following conventions will be used:
342246ed35dSJoerg Wunsch  *
343246ed35dSJoerg Wunsch  * fd is a pointer to the fd_data struct for the drive in question
344246ed35dSJoerg Wunsch  * fdc is a pointer to the fdc_data struct for the controller
345246ed35dSJoerg Wunsch  * fdu is the floppy drive unit number
346246ed35dSJoerg Wunsch  * fdcu is the floppy controller unit number
347246ed35dSJoerg Wunsch  * fdsu is the floppy drive unit number on that controller. (sub-unit)
348246ed35dSJoerg Wunsch  */
349b99f0a4aSAndrew Moore 
350246ed35dSJoerg Wunsch /*
351246ed35dSJoerg Wunsch  * Function declarations, same (chaotic) order as they appear in the
352246ed35dSJoerg Wunsch  * file.  Re-ordering is too late now, it would only obfuscate the
353246ed35dSJoerg Wunsch  * diffs against old and offspring versions (like the PC98 one).
354246ed35dSJoerg Wunsch  *
355246ed35dSJoerg Wunsch  * Anyone adding functions here, please keep this sequence the same
356246ed35dSJoerg Wunsch  * as below -- makes locating a particular function in the body much
357246ed35dSJoerg Wunsch  * easier.
358246ed35dSJoerg Wunsch  */
359246ed35dSJoerg Wunsch static void fdout_wr(fdc_p, u_int8_t);
360246ed35dSJoerg Wunsch static u_int8_t fdsts_rd(fdc_p);
361246ed35dSJoerg Wunsch static void fddata_wr(fdc_p, u_int8_t);
362246ed35dSJoerg Wunsch static u_int8_t fddata_rd(fdc_p);
363246ed35dSJoerg Wunsch static void fdctl_wr_isa(fdc_p, u_int8_t);
364246ed35dSJoerg Wunsch #if NCARD > 0
365246ed35dSJoerg Wunsch static void fdctl_wr_pcmcia(fdc_p, u_int8_t);
366246ed35dSJoerg Wunsch #endif
367246ed35dSJoerg Wunsch #if 0
368246ed35dSJoerg Wunsch static u_int8_t fdin_rd(fdc_p);
369246ed35dSJoerg Wunsch #endif
370246ed35dSJoerg Wunsch static int fdc_err(struct fdc_data *, const char *);
371246ed35dSJoerg Wunsch static int fd_cmd(struct fdc_data *, int, ...);
372246ed35dSJoerg Wunsch static int enable_fifo(fdc_p fdc);
373246ed35dSJoerg Wunsch static int fd_sense_drive_status(fdc_p, int *);
374246ed35dSJoerg Wunsch static int fd_sense_int(fdc_p, int *, int *);
375246ed35dSJoerg Wunsch static int fd_read_status(fdc_p);
376246ed35dSJoerg Wunsch static int fdc_alloc_resources(struct fdc_data *);
377246ed35dSJoerg Wunsch static void fdc_release_resources(struct fdc_data *);
378246ed35dSJoerg Wunsch static int fdc_read_ivar(device_t, device_t, int, uintptr_t *);
379246ed35dSJoerg Wunsch static int fdc_probe(device_t);
380246ed35dSJoerg Wunsch #if NCARD > 0
381246ed35dSJoerg Wunsch static int fdc_pccard_probe(device_t);
382246ed35dSJoerg Wunsch #endif
383246ed35dSJoerg Wunsch static int fdc_detach(device_t dev);
384246ed35dSJoerg Wunsch static void fdc_add_child(device_t, const char *, int);
385246ed35dSJoerg Wunsch static int fdc_attach(device_t);
386246ed35dSJoerg Wunsch static int fdc_print_child(device_t, device_t);
387246ed35dSJoerg Wunsch static void fd_clone (void *, char *, int, dev_t *);
388246ed35dSJoerg Wunsch static int fd_probe(device_t);
389246ed35dSJoerg Wunsch static int fd_attach(device_t);
390246ed35dSJoerg Wunsch static int fd_detach(device_t);
3916182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int);
3923a2f7427SDavid Greenman #  define TURNON 1
3933a2f7427SDavid Greenman #  define TURNOFF 0
3943a2f7427SDavid Greenman static timeout_t fd_turnoff;
3953a2f7427SDavid Greenman static timeout_t fd_motor_on;
3966182fdbdSPeter Wemm static void fd_turnon(struct fd_data *);
3973a2f7427SDavid Greenman static void fdc_reset(fdc_p);
3986182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *);
39980909a7dSJoerg Wunsch static int out_fdc(struct fdc_data *, int);
400246ed35dSJoerg Wunsch /*
401246ed35dSJoerg Wunsch  * The open function is named Fdopen() to avoid confusion with fdopen()
402246ed35dSJoerg Wunsch  * in fd(4).  The difference is now only meaningful for debuggers.
403246ed35dSJoerg Wunsch  */
404246ed35dSJoerg Wunsch static	d_open_t	Fdopen;
405246ed35dSJoerg Wunsch static	d_close_t	fdclose;
406246ed35dSJoerg Wunsch static	d_strategy_t	fdstrategy;
4076182fdbdSPeter Wemm static void fdstart(struct fdc_data *);
4085c1a1eaeSBruce Evans static timeout_t fd_iotimeout;
4093a2f7427SDavid Greenman static timeout_t fd_pseudointr;
410246ed35dSJoerg Wunsch static driver_intr_t fdc_intr;
411246ed35dSJoerg Wunsch static int fdcpio(fdc_p, long, caddr_t, u_int);
4121a6bed68SJoerg Wunsch static int fdautoselect(dev_t);
4136182fdbdSPeter Wemm static int fdstate(struct fdc_data *);
4146182fdbdSPeter Wemm static int retrier(struct fdc_data *);
415246ed35dSJoerg Wunsch static void fdbiodone(struct bio *);
416f664aeeeSJoerg Wunsch static int fdmisccmd(dev_t, u_int, void *);
417246ed35dSJoerg Wunsch static	d_ioctl_t	fdioctl;
418d66c374fSTor Egge 
419d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
420d66c374fSTor Egge 
421d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
4223a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
423cba2a7c6SBruce Evans static int volatile fd_debug = 0;
4240e17a5bcSJoerg Wunsch #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0)
4250e17a5bcSJoerg Wunsch #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0)
426d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
4270e17a5bcSJoerg Wunsch #define TRACE0(arg) do { } while (0)
4280e17a5bcSJoerg Wunsch #define TRACE1(arg1, arg2) do { } while (0)
429d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
4305b81b6b3SRodney W. Grimes 
431246ed35dSJoerg Wunsch /*
432246ed35dSJoerg Wunsch  * Bus space handling (access to low-level IO).
433246ed35dSJoerg Wunsch  */
434427ccf00SDoug Rabson static void
435427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v)
436427ccf00SDoug Rabson {
437427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
438427ccf00SDoug Rabson }
439427ccf00SDoug Rabson 
440427ccf00SDoug Rabson static u_int8_t
441427ccf00SDoug Rabson fdsts_rd(fdc_p fdc)
442427ccf00SDoug Rabson {
443427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
444427ccf00SDoug Rabson }
445427ccf00SDoug Rabson 
446427ccf00SDoug Rabson static void
447427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v)
448427ccf00SDoug Rabson {
449427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
450427ccf00SDoug Rabson }
451427ccf00SDoug Rabson 
452427ccf00SDoug Rabson static u_int8_t
453427ccf00SDoug Rabson fddata_rd(fdc_p fdc)
454427ccf00SDoug Rabson {
455427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
456427ccf00SDoug Rabson }
457427ccf00SDoug Rabson 
458427ccf00SDoug Rabson static void
4595f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v)
460427ccf00SDoug Rabson {
461427ccf00SDoug Rabson 	bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
462427ccf00SDoug Rabson }
463427ccf00SDoug Rabson 
46458c9d623SPeter Wemm #if NCARD > 0
4655f830ea2SJoerg Wunsch static void
4665f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
4675f830ea2SJoerg Wunsch {
4685f830ea2SJoerg Wunsch 	bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
4695f830ea2SJoerg Wunsch }
47058c9d623SPeter Wemm #endif
4715f830ea2SJoerg Wunsch 
472427ccf00SDoug Rabson static u_int8_t
473427ccf00SDoug Rabson fdin_rd(fdc_p fdc)
474427ccf00SDoug Rabson {
475427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
476427ccf00SDoug Rabson }
477427ccf00SDoug Rabson 
47887f6c662SJulian Elischer #define CDEV_MAJOR 9
4794e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = {
4804e2f199eSPoul-Henning Kamp 	/* open */	Fdopen,
4814e2f199eSPoul-Henning Kamp 	/* close */	fdclose,
4824e2f199eSPoul-Henning Kamp 	/* read */	physread,
4834e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
4844e2f199eSPoul-Henning Kamp 	/* ioctl */	fdioctl,
4854e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
4864e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
4874e2f199eSPoul-Henning Kamp 	/* strategy */	fdstrategy,
4884e2f199eSPoul-Henning Kamp 	/* name */	"fd",
4894e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
4904e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
4914e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
4924e2f199eSPoul-Henning Kamp 	/* flags */	D_DISK,
4934e2f199eSPoul-Henning Kamp };
4944e2f199eSPoul-Henning Kamp 
495246ed35dSJoerg Wunsch /*
496246ed35dSJoerg Wunsch  * Auxiliary functions.  Well, some only.  Others are scattered
497246ed35dSJoerg Wunsch  * throughout the entire file.
498246ed35dSJoerg Wunsch  */
499dc5df763SJoerg Wunsch static int
5006182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
501dc5df763SJoerg Wunsch {
5026182fdbdSPeter Wemm 	fdc->fdc_errs++;
50316b04b6aSJoerg Wunsch 	if (s) {
504b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
505b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
506b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
507b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
508b6e5f28eSPeter Wemm 						    "logging any more\n");
50916b04b6aSJoerg Wunsch 	}
510dc5df763SJoerg Wunsch 
511dc5df763SJoerg Wunsch 	return FD_FAILED;
512dc5df763SJoerg Wunsch }
513dc5df763SJoerg Wunsch 
514dc5df763SJoerg Wunsch /*
515dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
516dc5df763SJoerg Wunsch  * Unit number,
517dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
518dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
519dc5df763SJoerg Wunsch  */
5206f4e0bebSPoul-Henning Kamp static int
5216182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...)
522dc5df763SJoerg Wunsch {
523dc5df763SJoerg Wunsch 	u_char cmd;
524dc5df763SJoerg Wunsch 	int n_in;
525dc5df763SJoerg Wunsch 	int n;
526dc5df763SJoerg Wunsch 	va_list ap;
527dc5df763SJoerg Wunsch 
528dc5df763SJoerg Wunsch 	va_start(ap, n_out);
529dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
530dc5df763SJoerg Wunsch 	va_end(ap);
531dc5df763SJoerg Wunsch 	va_start(ap, n_out);
532dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
533dc5df763SJoerg Wunsch 	{
5346182fdbdSPeter Wemm 		if (out_fdc(fdc, va_arg(ap, int)) < 0)
535dc5df763SJoerg Wunsch 		{
536dc5df763SJoerg Wunsch 			char msg[50];
5372127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
538dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
539dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
5406182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
541dc5df763SJoerg Wunsch 		}
542dc5df763SJoerg Wunsch 	}
543dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
544dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
545dc5df763SJoerg Wunsch 	{
546dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
5476182fdbdSPeter Wemm 		if (fd_in(fdc, ptr) < 0)
548dc5df763SJoerg Wunsch 		{
549dc5df763SJoerg Wunsch 			char msg[50];
5502127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
551dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
552dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
5536182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
554dc5df763SJoerg Wunsch 		}
555dc5df763SJoerg Wunsch 	}
556dc5df763SJoerg Wunsch 
557dc5df763SJoerg Wunsch 	return 0;
558dc5df763SJoerg Wunsch }
559dc5df763SJoerg Wunsch 
5606f4e0bebSPoul-Henning Kamp static int
561d66c374fSTor Egge enable_fifo(fdc_p fdc)
562d66c374fSTor Egge {
563d66c374fSTor Egge 	int i, j;
564d66c374fSTor Egge 
565d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
566d66c374fSTor Egge 
567d66c374fSTor Egge 		/*
568d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
569d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
570d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
571d66c374fSTor Egge 		 */
572d66c374fSTor Egge 
5736182fdbdSPeter Wemm 		if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
5746182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
575d66c374fSTor Egge 
576d66c374fSTor Egge 		/* If command is invalid, return */
577d0900d6bSJoerg Wunsch 		j = FDSTS_TIMEOUT;
578427ccf00SDoug Rabson 		while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
579d0900d6bSJoerg Wunsch 		       != NE7_RQM && j-- > 0) {
580d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
581d66c374fSTor Egge 				fdc_reset(fdc);
582d66c374fSTor Egge 				return FD_FAILED;
583d66c374fSTor Egge 			}
584d0900d6bSJoerg Wunsch 			DELAY(1);
585d0900d6bSJoerg Wunsch 		}
586d66c374fSTor Egge 		if (j<0 ||
5876182fdbdSPeter Wemm 		    fd_cmd(fdc, 3,
588d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
589d66c374fSTor Egge 			fdc_reset(fdc);
5906182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
591d66c374fSTor Egge 		}
592d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
593d66c374fSTor Egge 		return 0;
594d66c374fSTor Egge 	}
5956182fdbdSPeter Wemm 	if (fd_cmd(fdc, 4,
596d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
5976182fdbdSPeter Wemm 		return fdc_err(fdc, "Re-enable FIFO failed\n");
598d66c374fSTor Egge 	return 0;
599d66c374fSTor Egge }
600d66c374fSTor Egge 
601d66c374fSTor Egge static int
602dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
603dc5df763SJoerg Wunsch {
604dc5df763SJoerg Wunsch 	int st3;
605dc5df763SJoerg Wunsch 
6066182fdbdSPeter Wemm 	if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
607dc5df763SJoerg Wunsch 	{
6086182fdbdSPeter Wemm 		return fdc_err(fdc, "Sense Drive Status failed\n");
609dc5df763SJoerg Wunsch 	}
610dc5df763SJoerg Wunsch 	if (st3p)
611dc5df763SJoerg Wunsch 		*st3p = st3;
612dc5df763SJoerg Wunsch 
613dc5df763SJoerg Wunsch 	return 0;
614dc5df763SJoerg Wunsch }
615dc5df763SJoerg Wunsch 
6166f4e0bebSPoul-Henning Kamp static int
617dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
618dc5df763SJoerg Wunsch {
6196182fdbdSPeter Wemm 	int cyl, st0, ret;
620dc5df763SJoerg Wunsch 
6216182fdbdSPeter Wemm 	ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
6226182fdbdSPeter Wemm 	if (ret) {
6236182fdbdSPeter Wemm 		(void)fdc_err(fdc,
624dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
625dc5df763SJoerg Wunsch 		return ret;
626dc5df763SJoerg Wunsch 	}
627dc5df763SJoerg Wunsch 
628dc5df763SJoerg Wunsch 	if (st0p)
629dc5df763SJoerg Wunsch 		*st0p = st0;
630dc5df763SJoerg Wunsch 
6316182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
632dc5df763SJoerg Wunsch 		/*
633dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
634dc5df763SJoerg Wunsch 		 */
635dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
636dc5df763SJoerg Wunsch 	}
637dc5df763SJoerg Wunsch 
6386182fdbdSPeter Wemm 	if (fd_in(fdc, &cyl) < 0) {
6396182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
640dc5df763SJoerg Wunsch 	}
641dc5df763SJoerg Wunsch 
642dc5df763SJoerg Wunsch 	if (cylp)
643dc5df763SJoerg Wunsch 		*cylp = cyl;
644dc5df763SJoerg Wunsch 
645dc5df763SJoerg Wunsch 	return 0;
646dc5df763SJoerg Wunsch }
647dc5df763SJoerg Wunsch 
648dc5df763SJoerg Wunsch 
6496f4e0bebSPoul-Henning Kamp static int
65064860614SJoerg Wunsch fd_read_status(fdc_p fdc)
651dc5df763SJoerg Wunsch {
652dc5df763SJoerg Wunsch 	int i, ret;
653b5e8ce9fSBruce Evans 
654246ed35dSJoerg Wunsch 	for (i = ret = 0; i < 7; i++) {
655b5e8ce9fSBruce Evans 		/*
65664860614SJoerg Wunsch 		 * XXX types are poorly chosen.  Only bytes can be read
657a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
658b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
659b5e8ce9fSBruce Evans 		 */
660b5e8ce9fSBruce Evans 		int status;
661b5e8ce9fSBruce Evans 
6626182fdbdSPeter Wemm 		ret = fd_in(fdc, &status);
663b5e8ce9fSBruce Evans 		fdc->status[i] = status;
664b5e8ce9fSBruce Evans 		if (ret != 0)
665dc5df763SJoerg Wunsch 			break;
666dc5df763SJoerg Wunsch 	}
667dc5df763SJoerg Wunsch 
668dc5df763SJoerg Wunsch 	if (ret == 0)
669dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
670dc5df763SJoerg Wunsch 	else
671dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
672dc5df763SJoerg Wunsch 
673dc5df763SJoerg Wunsch 	return ret;
674dc5df763SJoerg Wunsch }
675dc5df763SJoerg Wunsch 
6763a2f7427SDavid Greenman static int
67737286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc)
6785b81b6b3SRodney W. Grimes {
67937286586SPeter Wemm 	device_t dev;
680e219897aSJoerg Wunsch 	int ispnp, ispcmcia, nports;
6815b81b6b3SRodney W. Grimes 
68237286586SPeter Wemm 	dev = fdc->fdc_dev;
6835f830ea2SJoerg Wunsch 	ispnp = (fdc->flags & FDC_ISPNP) != 0;
6845f830ea2SJoerg Wunsch 	ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
6856182fdbdSPeter Wemm 	fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
6866182fdbdSPeter Wemm 	fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
6876182fdbdSPeter Wemm 
688b6e5f28eSPeter Wemm 	/*
6895f830ea2SJoerg Wunsch 	 * On standard ISA, we don't just use an 8 port range
6905f830ea2SJoerg Wunsch 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
6915f830ea2SJoerg Wunsch 	 * register at 0x3f6.
6925f830ea2SJoerg Wunsch 	 *
693b6e5f28eSPeter Wemm 	 * Isn't PC hardware wonderful.
6945f830ea2SJoerg Wunsch 	 *
6955f830ea2SJoerg Wunsch 	 * The Y-E Data PCMCIA FDC doesn't have this problem, it
6965f830ea2SJoerg Wunsch 	 * uses the register with offset 6 for pseudo-DMA, and the
6975f830ea2SJoerg Wunsch 	 * one with offset 7 as control register.
698b6e5f28eSPeter Wemm 	 */
699e219897aSJoerg Wunsch 	nports = ispcmcia ? 8 : (ispnp ? 1 : 6);
7006182fdbdSPeter Wemm 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
7016182fdbdSPeter Wemm 					     &fdc->rid_ioport, 0ul, ~0ul,
702e219897aSJoerg Wunsch 					     nports, RF_ACTIVE);
7036182fdbdSPeter Wemm 	if (fdc->res_ioport == 0) {
704e219897aSJoerg Wunsch 		device_printf(dev, "cannot reserve I/O port range (%d ports)\n",
705e219897aSJoerg Wunsch 			      nports);
70637286586SPeter Wemm 		return ENXIO;
7076182fdbdSPeter Wemm 	}
708427ccf00SDoug Rabson 	fdc->portt = rman_get_bustag(fdc->res_ioport);
709427ccf00SDoug Rabson 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
710427ccf00SDoug Rabson 
7115f830ea2SJoerg Wunsch 	if (!ispcmcia) {
712427ccf00SDoug Rabson 		/*
7135f830ea2SJoerg Wunsch 		 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
7145f830ea2SJoerg Wunsch 		 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
7155f830ea2SJoerg Wunsch 		 * by checking the size and adjust the port address
7165f830ea2SJoerg Wunsch 		 * accordingly.
717b6e5f28eSPeter Wemm 		 */
718b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
719b6e5f28eSPeter Wemm 			fdc->port_off = -2;
720b6e5f28eSPeter Wemm 
721b6e5f28eSPeter Wemm 		/*
7225f830ea2SJoerg Wunsch 		 * Register the control port range as rid 1 if it
7235f830ea2SJoerg Wunsch 		 * isn't there already. Most PnP BIOSen will have
7245f830ea2SJoerg Wunsch 		 * already done this but non-PnP configurations don't.
725a2639a18SPeter Wemm 		 *
7265f830ea2SJoerg Wunsch 		 * And some (!!) report 0x3f2-0x3f5 and completely
7275f830ea2SJoerg Wunsch 		 * leave out the control register!  It seems that some
7285f830ea2SJoerg Wunsch 		 * non-antique controller chips have a different
7295f830ea2SJoerg Wunsch 		 * method of programming the transfer speed which
7305f830ea2SJoerg Wunsch 		 * doesn't require the control register, but it's
7315f830ea2SJoerg Wunsch 		 * mighty bogus as the chip still responds to the
7325f830ea2SJoerg Wunsch 		 * address for the control register.
733427ccf00SDoug Rabson 		 */
734b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
735b9da888fSPeter Wemm 			u_long ctlstart;
736b9da888fSPeter Wemm 
737b6e5f28eSPeter Wemm 			/* Find the control port, usually 0x3f7 */
7385f830ea2SJoerg Wunsch 			ctlstart = rman_get_start(fdc->res_ioport) +
7395f830ea2SJoerg Wunsch 				fdc->port_off + 7;
740b6e5f28eSPeter Wemm 
741b6e5f28eSPeter Wemm 			bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
742b9da888fSPeter Wemm 		}
743b6e5f28eSPeter Wemm 
744b6e5f28eSPeter Wemm 		/*
745b6e5f28eSPeter Wemm 		 * Now (finally!) allocate the control port.
746b6e5f28eSPeter Wemm 		 */
747427ccf00SDoug Rabson 		fdc->rid_ctl = 1;
7485f830ea2SJoerg Wunsch 		fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
7495f830ea2SJoerg Wunsch 						  &fdc->rid_ctl,
750b6e5f28eSPeter Wemm 						  0ul, ~0ul, 1, RF_ACTIVE);
751427ccf00SDoug Rabson 		if (fdc->res_ctl == 0) {
7525f830ea2SJoerg Wunsch 			device_printf(dev,
753e219897aSJoerg Wunsch 		"cannot reserve control I/O port range (control port)\n");
75437286586SPeter Wemm 			return ENXIO;
755427ccf00SDoug Rabson 		}
756427ccf00SDoug Rabson 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
757427ccf00SDoug Rabson 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
7585f830ea2SJoerg Wunsch 	}
7596182fdbdSPeter Wemm 
7606182fdbdSPeter Wemm 	fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
7616182fdbdSPeter Wemm 					  &fdc->rid_irq, 0ul, ~0ul, 1,
7626182fdbdSPeter Wemm 					  RF_ACTIVE);
7636182fdbdSPeter Wemm 	if (fdc->res_irq == 0) {
764427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve interrupt line\n");
76537286586SPeter Wemm 		return ENXIO;
7666182fdbdSPeter Wemm 	}
7675f830ea2SJoerg Wunsch 
7685f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
7696182fdbdSPeter Wemm 		fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
7706182fdbdSPeter Wemm 						  &fdc->rid_drq, 0ul, ~0ul, 1,
7716182fdbdSPeter Wemm 						  RF_ACTIVE);
7726182fdbdSPeter Wemm 		if (fdc->res_drq == 0) {
773427ccf00SDoug Rabson 			device_printf(dev, "cannot reserve DMA request line\n");
77437286586SPeter Wemm 			return ENXIO;
7756182fdbdSPeter Wemm 		}
7766182fdbdSPeter Wemm 		fdc->dmachan = fdc->res_drq->r_start;
7775f830ea2SJoerg Wunsch 	}
77837286586SPeter Wemm 
77937286586SPeter Wemm 	return 0;
78037286586SPeter Wemm }
78137286586SPeter Wemm 
78237286586SPeter Wemm static void
78337286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
78437286586SPeter Wemm {
78537286586SPeter Wemm 	device_t dev;
78637286586SPeter Wemm 
78737286586SPeter Wemm 	dev = fdc->fdc_dev;
78837286586SPeter Wemm 	if (fdc->res_irq != 0) {
78937286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
79037286586SPeter Wemm 					fdc->res_irq);
79137286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
79237286586SPeter Wemm 				     fdc->res_irq);
79337286586SPeter Wemm 	}
79437286586SPeter Wemm 	if (fdc->res_ctl != 0) {
79537286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
79637286586SPeter Wemm 					fdc->res_ctl);
79737286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
79837286586SPeter Wemm 				     fdc->res_ctl);
79937286586SPeter Wemm 	}
80037286586SPeter Wemm 	if (fdc->res_ioport != 0) {
80137286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
80237286586SPeter Wemm 					fdc->res_ioport);
80337286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
80437286586SPeter Wemm 				     fdc->res_ioport);
80537286586SPeter Wemm 	}
80637286586SPeter Wemm 	if (fdc->res_drq != 0) {
80737286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
80837286586SPeter Wemm 					fdc->res_drq);
80937286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
81037286586SPeter Wemm 				     fdc->res_drq);
81137286586SPeter Wemm 	}
81237286586SPeter Wemm }
81337286586SPeter Wemm 
814246ed35dSJoerg Wunsch /*
815246ed35dSJoerg Wunsch  * Configuration/initialization stuff, per controller.
816246ed35dSJoerg Wunsch  */
81737286586SPeter Wemm 
81837286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = {
81937286586SPeter Wemm 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
82037286586SPeter Wemm 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
82137286586SPeter Wemm 	{0}
82237286586SPeter Wemm };
82337286586SPeter Wemm 
82437286586SPeter Wemm static int
8253aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
82637286586SPeter Wemm {
82737286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
82837286586SPeter Wemm 
82937286586SPeter Wemm 	switch (which) {
83037286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
83137286586SPeter Wemm 		*result = ivars->fdunit;
83237286586SPeter Wemm 		break;
83337286586SPeter Wemm 	default:
83437286586SPeter Wemm 		return ENOENT;
83537286586SPeter Wemm 	}
83637286586SPeter Wemm 	return 0;
83737286586SPeter Wemm }
83837286586SPeter Wemm 
83937286586SPeter Wemm static int
84037286586SPeter Wemm fdc_probe(device_t dev)
84137286586SPeter Wemm {
84237286586SPeter Wemm 	int	error, ic_type;
84337286586SPeter Wemm 	struct	fdc_data *fdc;
84437286586SPeter Wemm 
84537286586SPeter Wemm 	fdc = device_get_softc(dev);
84637286586SPeter Wemm 	bzero(fdc, sizeof *fdc);
84737286586SPeter Wemm 	fdc->fdc_dev = dev;
8485f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_isa;
84937286586SPeter Wemm 
85037286586SPeter Wemm 	/* Check pnp ids */
85137286586SPeter Wemm 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
85237286586SPeter Wemm 	if (error == ENXIO)
85337286586SPeter Wemm 		return ENXIO;
8545f830ea2SJoerg Wunsch 	if (error == 0)
8555f830ea2SJoerg Wunsch 		fdc->flags |= FDC_ISPNP;
85637286586SPeter Wemm 
85737286586SPeter Wemm 	/* Attempt to allocate our resources for the duration of the probe */
85837286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
85937286586SPeter Wemm 	if (error)
86037286586SPeter Wemm 		goto out;
8615b81b6b3SRodney W. Grimes 
86216111cedSAndrew Moore 	/* First - lets reset the floppy controller */
863427ccf00SDoug Rabson 	fdout_wr(fdc, 0);
86416111cedSAndrew Moore 	DELAY(100);
865427ccf00SDoug Rabson 	fdout_wr(fdc, FDO_FRST);
86616111cedSAndrew Moore 
8675b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
8686182fdbdSPeter Wemm 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
8696182fdbdSPeter Wemm 		   NE7_SPEC_2(2, 0), 0)) {
8706182fdbdSPeter Wemm 		error = ENXIO;
8716182fdbdSPeter Wemm 		goto out;
8725b81b6b3SRodney W. Grimes 	}
8736182fdbdSPeter Wemm 
8746182fdbdSPeter Wemm 	if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
8756182fdbdSPeter Wemm 		ic_type = (u_char)ic_type;
8766182fdbdSPeter Wemm 		switch (ic_type) {
8776182fdbdSPeter Wemm 		case 0x80:
8786182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 765 or clone");
8796182fdbdSPeter Wemm 			fdc->fdct = FDC_NE765;
8806182fdbdSPeter Wemm 			break;
8811a6bed68SJoerg Wunsch 		case 0x81:	/* not mentioned in any hardware doc */
8826182fdbdSPeter Wemm 		case 0x90:
8831a6bed68SJoerg Wunsch 			device_set_desc(dev,
8842ac8c61aSMaxime Henrion 		"Enhanced floppy controller (i82077, NE72065 or clone)");
8851a6bed68SJoerg Wunsch 			fdc->fdct = FDC_ENHANCED;
8866182fdbdSPeter Wemm 			break;
8876182fdbdSPeter Wemm 		default:
8882ac8c61aSMaxime Henrion 			device_set_desc(dev, "Generic floppy controller");
8896182fdbdSPeter Wemm 			fdc->fdct = FDC_UNKNOWN;
8906182fdbdSPeter Wemm 			break;
8916182fdbdSPeter Wemm 		}
8926182fdbdSPeter Wemm 	}
8936182fdbdSPeter Wemm 
8946182fdbdSPeter Wemm out:
89537286586SPeter Wemm 	fdc_release_resources(fdc);
8966182fdbdSPeter Wemm 	return (error);
8975b81b6b3SRodney W. Grimes }
8985b81b6b3SRodney W. Grimes 
8995f830ea2SJoerg Wunsch #if NCARD > 0
9005f830ea2SJoerg Wunsch 
9015f830ea2SJoerg Wunsch static int
9025f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev)
9035f830ea2SJoerg Wunsch {
9045f830ea2SJoerg Wunsch 	int	error;
9055f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
9065f830ea2SJoerg Wunsch 
9075f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
9085f830ea2SJoerg Wunsch 	bzero(fdc, sizeof *fdc);
9095f830ea2SJoerg Wunsch 	fdc->fdc_dev = dev;
9105f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_pcmcia;
9115f830ea2SJoerg Wunsch 
9125f830ea2SJoerg Wunsch 	fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
9135f830ea2SJoerg Wunsch 
9145f830ea2SJoerg Wunsch 	/* Attempt to allocate our resources for the duration of the probe */
9155f830ea2SJoerg Wunsch 	error = fdc_alloc_resources(fdc);
9165f830ea2SJoerg Wunsch 	if (error)
9175f830ea2SJoerg Wunsch 		goto out;
9185f830ea2SJoerg Wunsch 
9195f830ea2SJoerg Wunsch 	/* First - lets reset the floppy controller */
9205f830ea2SJoerg Wunsch 	fdout_wr(fdc, 0);
9215f830ea2SJoerg Wunsch 	DELAY(100);
9225f830ea2SJoerg Wunsch 	fdout_wr(fdc, FDO_FRST);
9235f830ea2SJoerg Wunsch 
9245f830ea2SJoerg Wunsch 	/* see if it can handle a command */
9255f830ea2SJoerg Wunsch 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
9265f830ea2SJoerg Wunsch 		   NE7_SPEC_2(2, 0), 0)) {
9275f830ea2SJoerg Wunsch 		error = ENXIO;
9285f830ea2SJoerg Wunsch 		goto out;
9295f830ea2SJoerg Wunsch 	}
9305f830ea2SJoerg Wunsch 
9315f830ea2SJoerg Wunsch 	device_set_desc(dev, "Y-E Data PCMCIA floppy");
9325f830ea2SJoerg Wunsch 	fdc->fdct = FDC_NE765;
9335f830ea2SJoerg Wunsch 
9345f830ea2SJoerg Wunsch out:
9355f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
9365f830ea2SJoerg Wunsch 	return (error);
9375f830ea2SJoerg Wunsch }
9385f830ea2SJoerg Wunsch 
939e219897aSJoerg Wunsch #endif /* NCARD > 0 */
940e219897aSJoerg Wunsch 
9415f830ea2SJoerg Wunsch static int
942e219897aSJoerg Wunsch fdc_detach(device_t dev)
9435f830ea2SJoerg Wunsch {
9445f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
9455f830ea2SJoerg Wunsch 	int	error;
9465f830ea2SJoerg Wunsch 
9475f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
9485f830ea2SJoerg Wunsch 
9495f830ea2SJoerg Wunsch 	/* have our children detached first */
9505f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
9515f830ea2SJoerg Wunsch 		return (error);
9525f830ea2SJoerg Wunsch 
953e219897aSJoerg Wunsch 	/* reset controller, turn motor off */
954e219897aSJoerg Wunsch 	fdout_wr(fdc, 0);
955e219897aSJoerg Wunsch 
956e219897aSJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0)
957e219897aSJoerg Wunsch 		isa_dma_release(fdc->dmachan);
958e219897aSJoerg Wunsch 
9595f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_ATTACHED) == 0) {
9605f830ea2SJoerg Wunsch 		device_printf(dev, "already unloaded\n");
9615f830ea2SJoerg Wunsch 		return (0);
9625f830ea2SJoerg Wunsch 	}
9635f830ea2SJoerg Wunsch 	fdc->flags &= ~FDC_ATTACHED;
9645f830ea2SJoerg Wunsch 
9655f830ea2SJoerg Wunsch 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
9665f830ea2SJoerg Wunsch 			  fdc->fdc_intr);
9675f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
9685f830ea2SJoerg Wunsch 	device_printf(dev, "unload\n");
9695f830ea2SJoerg Wunsch 	return (0);
9705f830ea2SJoerg Wunsch }
9715f830ea2SJoerg Wunsch 
9725b81b6b3SRodney W. Grimes /*
97337286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
9745b81b6b3SRodney W. Grimes  */
9756182fdbdSPeter Wemm static void
97637286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
9775b81b6b3SRodney W. Grimes {
9781a6bed68SJoerg Wunsch 	int	disabled, flags;
97937286586SPeter Wemm 	struct fdc_ivars *ivar;
9806182fdbdSPeter Wemm 	device_t child;
98192200632SGarrett Wollman 
9827cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
98337286586SPeter Wemm 	if (ivar == NULL)
9846182fdbdSPeter Wemm 		return;
98537286586SPeter Wemm 	if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
98637286586SPeter Wemm 		ivar->fdunit = 0;
987fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
98842117b6cSPoul-Henning Kamp 	if (child == NULL) {
98942117b6cSPoul-Henning Kamp 		free(ivar, M_DEVBUF);
9906182fdbdSPeter Wemm 		return;
99142117b6cSPoul-Henning Kamp 	}
99237286586SPeter Wemm 	device_set_ivars(child, ivar);
9931a6bed68SJoerg Wunsch 	if (resource_int_value(name, unit, "flags", &flags) == 0)
9941a6bed68SJoerg Wunsch 		 device_set_flags(child, flags);
995a97c75b7SDoug Rabson 	if (resource_int_value(name, unit, "disabled", &disabled) == 0
996a97c75b7SDoug Rabson 	    && disabled != 0)
9976182fdbdSPeter Wemm 		device_disable(child);
9986182fdbdSPeter Wemm }
9996182fdbdSPeter Wemm 
10006182fdbdSPeter Wemm static int
10016182fdbdSPeter Wemm fdc_attach(device_t dev)
10026182fdbdSPeter Wemm {
100337286586SPeter Wemm 	struct	fdc_data *fdc;
10042398f0cdSPeter Wemm 	const char *name, *dname;
10055d54fe91SJoerg Wunsch 	int	i, error, dunit;
1006427ccf00SDoug Rabson 
100737286586SPeter Wemm 	fdc = device_get_softc(dev);
100837286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
100937286586SPeter Wemm 	if (error) {
1010f8ce7dd5SJoerg Wunsch 		device_printf(dev, "cannot re-acquire resources\n");
101137286586SPeter Wemm 		return error;
101237286586SPeter Wemm 	}
101337286586SPeter Wemm 	error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
10143c36743eSMark Murray 			       INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
10153c36743eSMark Murray 			       &fdc->fdc_intr);
101637286586SPeter Wemm 	if (error) {
101737286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
101837286586SPeter Wemm 		return error;
101937286586SPeter Wemm 	}
102037286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
1021fb35bd37SJoerg Wunsch 	fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET;
10226182fdbdSPeter Wemm 
10235f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
1024f8ce7dd5SJoerg Wunsch 		/*
1025f8ce7dd5SJoerg Wunsch 		 * Acquire the DMA channel forever, the driver will do
1026f8ce7dd5SJoerg Wunsch 		 * the rest
1027f8ce7dd5SJoerg Wunsch 		 * XXX should integrate with rman
1028f8ce7dd5SJoerg Wunsch 		 */
1029100f78bbSSujal Patel 		isa_dma_acquire(fdc->dmachan);
1030f8ce7dd5SJoerg Wunsch 		isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
10315f830ea2SJoerg Wunsch 	}
10325b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
10336182fdbdSPeter Wemm 
10343a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
103564860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
10368177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
10375b81b6b3SRodney W. Grimes 
10386182fdbdSPeter Wemm 	/*
103937286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
104037286586SPeter Wemm 	 * devices from the BIOS unless overridden.
10416182fdbdSPeter Wemm 	 */
1042ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
10432398f0cdSPeter Wemm 	i = 0;
10442398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
10452398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
104637286586SPeter Wemm 
104760444853SJoerg Wunsch 	if ((error = bus_generic_attach(dev)) != 0)
104860444853SJoerg Wunsch 		return (error);
104960444853SJoerg Wunsch 
105060444853SJoerg Wunsch 	return (0);
10516182fdbdSPeter Wemm }
10526182fdbdSPeter Wemm 
105315317dd8SMatthew N. Dodd static int
10546182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
10556182fdbdSPeter Wemm {
10561a6bed68SJoerg Wunsch 	int retval = 0, flags;
105715317dd8SMatthew N. Dodd 
105815317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
10591a6bed68SJoerg Wunsch 	retval += printf(" on %s drive %d", device_get_nameunit(me),
106037286586SPeter Wemm 	       fdc_get_fdunit(child));
10611a6bed68SJoerg Wunsch 	if ((flags = device_get_flags(me)) != 0)
10621a6bed68SJoerg Wunsch 		retval += printf(" flags %#x", flags);
10631a6bed68SJoerg Wunsch 	retval += printf("\n");
106415317dd8SMatthew N. Dodd 
106515317dd8SMatthew N. Dodd 	return (retval);
10666182fdbdSPeter Wemm }
10676182fdbdSPeter Wemm 
106816e68fc6SPeter Wemm static device_method_t fdc_methods[] = {
106916e68fc6SPeter Wemm 	/* Device interface */
107016e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fdc_probe),
107116e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fdc_attach),
1072e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
107316e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
107416e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend),
107516e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume),
107616e68fc6SPeter Wemm 
107716e68fc6SPeter Wemm 	/* Bus interface */
107816e68fc6SPeter Wemm 	DEVMETHOD(bus_print_child,	fdc_print_child),
107937286586SPeter Wemm 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
108016e68fc6SPeter Wemm 	/* Our children never use any other bus interface methods. */
108116e68fc6SPeter Wemm 
108216e68fc6SPeter Wemm 	{ 0, 0 }
108316e68fc6SPeter Wemm };
108416e68fc6SPeter Wemm 
108516e68fc6SPeter Wemm static driver_t fdc_driver = {
108616e68fc6SPeter Wemm 	"fdc",
108716e68fc6SPeter Wemm 	fdc_methods,
108816e68fc6SPeter Wemm 	sizeof(struct fdc_data)
108916e68fc6SPeter Wemm };
109016e68fc6SPeter Wemm 
109116e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
10925f063c7bSMike Smith DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0);
109316e68fc6SPeter Wemm 
10945f830ea2SJoerg Wunsch #if NCARD > 0
10955f830ea2SJoerg Wunsch 
10965f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = {
10975f830ea2SJoerg Wunsch 	/* Device interface */
10985f830ea2SJoerg Wunsch 	DEVMETHOD(device_probe,		fdc_pccard_probe),
10995f830ea2SJoerg Wunsch 	DEVMETHOD(device_attach,	fdc_attach),
1100e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
11015f830ea2SJoerg Wunsch 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
11025f830ea2SJoerg Wunsch 	DEVMETHOD(device_suspend,	bus_generic_suspend),
11035f830ea2SJoerg Wunsch 	DEVMETHOD(device_resume,	bus_generic_resume),
11045f830ea2SJoerg Wunsch 
11055f830ea2SJoerg Wunsch 	/* Bus interface */
11065f830ea2SJoerg Wunsch 	DEVMETHOD(bus_print_child,	fdc_print_child),
11075f830ea2SJoerg Wunsch 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
11085f830ea2SJoerg Wunsch 	/* Our children never use any other bus interface methods. */
11095f830ea2SJoerg Wunsch 
11105f830ea2SJoerg Wunsch 	{ 0, 0 }
11115f830ea2SJoerg Wunsch };
11125f830ea2SJoerg Wunsch 
11135f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = {
11145f830ea2SJoerg Wunsch 	"fdc",
11155f830ea2SJoerg Wunsch 	fdc_pccard_methods,
11165f830ea2SJoerg Wunsch 	sizeof(struct fdc_data)
11175f830ea2SJoerg Wunsch };
11185f830ea2SJoerg Wunsch 
11195f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
11205f830ea2SJoerg Wunsch 
11215f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
11225f830ea2SJoerg Wunsch 
112360444853SJoerg Wunsch /*
11241a6bed68SJoerg Wunsch  * Create a clone device upon request by devfs.
112560444853SJoerg Wunsch  */
11263f54a085SPoul-Henning Kamp static void
112780909a7dSJoerg Wunsch fd_clone(void *arg, char *name, int namelen, dev_t *dev)
11283f54a085SPoul-Henning Kamp {
112960444853SJoerg Wunsch 	struct	fd_data *fd;
11301a6bed68SJoerg Wunsch 	int i, u;
11313f54a085SPoul-Henning Kamp 	char *n;
11321a6bed68SJoerg Wunsch 	size_t l;
11333f54a085SPoul-Henning Kamp 
113460444853SJoerg Wunsch 	fd = (struct fd_data *)arg;
11353f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
11363f54a085SPoul-Henning Kamp 		return;
1137db901281SPoul-Henning Kamp 	if (dev_stdclone(name, &n, "fd", &u) != 2)
11383f54a085SPoul-Henning Kamp 		return;
1139294d8844SJoerg Wunsch 	if (u != fd->fdu)
1140294d8844SJoerg Wunsch 		/* unit # mismatch */
1141294d8844SJoerg Wunsch 		return;
11421a6bed68SJoerg Wunsch 	l = strlen(n);
11431a6bed68SJoerg Wunsch 	if (l == 1 && *n >= 'a' && *n <= 'h') {
11441a6bed68SJoerg Wunsch 		/*
11451a6bed68SJoerg Wunsch 		 * Trailing letters a through h denote
11461a6bed68SJoerg Wunsch 		 * pseudo-partitions.  We don't support true
11471a6bed68SJoerg Wunsch 		 * (UFS-style) partitions, so we just implement them
11481a6bed68SJoerg Wunsch 		 * as symlinks if someone asks us nicely.
11491a6bed68SJoerg Wunsch 		 */
115060444853SJoerg Wunsch 		*dev = make_dev_alias(fd->masterdev, name);
11511a6bed68SJoerg Wunsch 		return;
11521a6bed68SJoerg Wunsch 	}
11531a6bed68SJoerg Wunsch 	if (l >= 2 && l <= 5 && *n == '.') {
11541a6bed68SJoerg Wunsch 		/*
11551a6bed68SJoerg Wunsch 		 * Trailing numbers, preceded by a dot, denote
11561a6bed68SJoerg Wunsch 		 * subdevices for different densities.  Historically,
11571a6bed68SJoerg Wunsch 		 * they have been named by density (like fd0.1440),
11581a6bed68SJoerg Wunsch 		 * but we allow arbitrary numbers between 1 and 4
11591a6bed68SJoerg Wunsch 		 * digits, so fd0.1 through fd0.15 are possible as
11601a6bed68SJoerg Wunsch 		 * well.
11611a6bed68SJoerg Wunsch 		 */
11621a6bed68SJoerg Wunsch 		for (i = 1; i < l; i++)
11631a6bed68SJoerg Wunsch 			if (n[i] < '0' || n[i] > '9')
11641a6bed68SJoerg Wunsch 				return;
11651a6bed68SJoerg Wunsch 		for (i = 0; i < NUMDENS - 1; i++)
11661a6bed68SJoerg Wunsch 			if (fd->clonedevs[i] == NODEV) {
11671a6bed68SJoerg Wunsch 				*dev = make_dev(&fd_cdevsw,
11681a6bed68SJoerg Wunsch 						FDNUMTOUNIT(u) + i + 1,
11691a6bed68SJoerg Wunsch 						UID_ROOT, GID_OPERATOR, 0640,
11701a6bed68SJoerg Wunsch 						name);
11711a6bed68SJoerg Wunsch 				fd->clonedevs[i] = *dev;
11721a6bed68SJoerg Wunsch 				return;
11731a6bed68SJoerg Wunsch 			}
11743f54a085SPoul-Henning Kamp 	}
11753f54a085SPoul-Henning Kamp }
11763f54a085SPoul-Henning Kamp 
117716e68fc6SPeter Wemm /*
1178246ed35dSJoerg Wunsch  * Configuration/initialization, per drive.
117916e68fc6SPeter Wemm  */
11806182fdbdSPeter Wemm static int
11816182fdbdSPeter Wemm fd_probe(device_t dev)
11826182fdbdSPeter Wemm {
11836182fdbdSPeter Wemm 	int	i;
11841a6bed68SJoerg Wunsch 	u_int	st0, st3;
11856182fdbdSPeter Wemm 	struct	fd_data *fd;
11866182fdbdSPeter Wemm 	struct	fdc_data *fdc;
11876182fdbdSPeter Wemm 	fdsu_t	fdsu;
11881a6bed68SJoerg Wunsch 	int	flags;
11896182fdbdSPeter Wemm 
11906182fdbdSPeter Wemm 	fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
11916182fdbdSPeter Wemm 	fd = device_get_softc(dev);
11926182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
11931a6bed68SJoerg Wunsch 	flags = device_get_flags(dev);
11946182fdbdSPeter Wemm 
11956182fdbdSPeter Wemm 	bzero(fd, sizeof *fd);
11966182fdbdSPeter Wemm 	fd->dev = dev;
11976182fdbdSPeter Wemm 	fd->fdc = fdc;
11986182fdbdSPeter Wemm 	fd->fdsu = fdsu;
11996182fdbdSPeter Wemm 	fd->fdu = device_get_unit(dev);
12001a6bed68SJoerg Wunsch 	fd->flags = FD_UA;	/* make sure fdautoselect() will be called */
12016182fdbdSPeter Wemm 
12021a6bed68SJoerg Wunsch 	fd->type = FD_DTYPE(flags);
1203038d1bbdSJoerg Wunsch /*
1204038d1bbdSJoerg Wunsch  * XXX I think using __i386__ is wrong here since we actually want to probe
1205038d1bbdSJoerg Wunsch  * for the machine type, not the CPU type (so non-PC arch's like the PC98 will
1206038d1bbdSJoerg Wunsch  * fail the probe).  However, for whatever reason, testing for _MACHINE_ARCH
1207038d1bbdSJoerg Wunsch  * == i386 breaks the test on FreeBSD/Alpha.
1208038d1bbdSJoerg Wunsch  */
1209038d1bbdSJoerg Wunsch #ifdef __i386__
12101a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) {
12111a6bed68SJoerg Wunsch 		/* Look up what the BIOS thinks we have. */
12121a6bed68SJoerg Wunsch 		if (fd->fdu == 0) {
12135f830ea2SJoerg Wunsch 			if ((fdc->flags & FDC_ISPCMCIA))
12141a6bed68SJoerg Wunsch 				/*
12151a6bed68SJoerg Wunsch 				 * Somewhat special.  No need to force the
12161a6bed68SJoerg Wunsch 				 * user to set device flags, since the Y-E
12171a6bed68SJoerg Wunsch 				 * Data PCMCIA floppy is always a 1.44 MB
12181a6bed68SJoerg Wunsch 				 * device.
12191a6bed68SJoerg Wunsch 				 */
12201a6bed68SJoerg Wunsch 				fd->type = FDT_144M;
12210722d6abSJoerg Wunsch 			else
12221a6bed68SJoerg Wunsch 				fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
12231a6bed68SJoerg Wunsch 		} else {
12241a6bed68SJoerg Wunsch 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
12256b7bd95bSJoerg Wunsch 		}
12261a6bed68SJoerg Wunsch 		if (fd->type == FDT_288M_1)
12271a6bed68SJoerg Wunsch 			fd->type = FDT_288M;
12281a6bed68SJoerg Wunsch 	}
1229038d1bbdSJoerg Wunsch #endif /* __i386__ */
12306182fdbdSPeter Wemm 	/* is there a unit? */
12311a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE)
12326182fdbdSPeter Wemm 		return (ENXIO);
12336182fdbdSPeter Wemm 
12346182fdbdSPeter Wemm 	/* select it */
12356182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNON);
1236fb35bd37SJoerg Wunsch 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
12376182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
12386182fdbdSPeter Wemm 
12398de0675cSPeter Wemm 	/* XXX This doesn't work before the first set_motor() */
12401a6bed68SJoerg Wunsch 	if ((fdc->flags & FDC_HAS_FIFO) == 0  &&
12411a6bed68SJoerg Wunsch 	    fdc->fdct == FDC_ENHANCED &&
12421a6bed68SJoerg Wunsch 	    (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 &&
12431a6bed68SJoerg Wunsch 	    enable_fifo(fdc) == 0) {
1244b6e5f28eSPeter Wemm 		device_printf(device_get_parent(dev),
1245b6e5f28eSPeter Wemm 		    "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1246d66c374fSTor Egge 	}
12476182fdbdSPeter Wemm 
12481a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0) {
1249dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
12501a6bed68SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
12511a6bed68SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
1252dc5df763SJoerg Wunsch 			/* Seek some steps... */
12536182fdbdSPeter Wemm 			if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1254dc5df763SJoerg Wunsch 				/* ...wait a moment... */
1255dc5df763SJoerg Wunsch 				DELAY(300000);
1256dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
12576182fdbdSPeter Wemm 				fd_sense_int(fdc, 0, 0);
1258dc5df763SJoerg Wunsch 			}
1259dc5df763SJoerg Wunsch 		}
1260dc5df763SJoerg Wunsch 
12616b7bd95bSJoerg Wunsch 		for (i = 0; i < 2; i++) {
12626b7bd95bSJoerg Wunsch 			/*
12636b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
12641a6bed68SJoerg Wunsch 			 * heads have been beyond cylinder 76, since
12651a6bed68SJoerg Wunsch 			 * most FDCs still barf when attempting to
12661a6bed68SJoerg Wunsch 			 * recalibrate more than 77 steps
12676b7bd95bSJoerg Wunsch 			 */
1268dc5df763SJoerg Wunsch 			/* go back to 0: */
12696182fdbdSPeter Wemm 			if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
12706b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
12716b7bd95bSJoerg Wunsch 				DELAY(i == 0 ? 1000000 : 300000);
12725b81b6b3SRodney W. Grimes 
12736b7bd95bSJoerg Wunsch 				/* anything responding? */
1274dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
1275dc5df763SJoerg Wunsch 				    (st0 & NE7_ST0_EC) == 0)
12766b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
12776b7bd95bSJoerg Wunsch 			}
1278dc5df763SJoerg Wunsch 		}
12791a6bed68SJoerg Wunsch 	}
12806b7bd95bSJoerg Wunsch 
12816182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNOFF);
12823a2f7427SDavid Greenman 
12831a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0 &&
12841a6bed68SJoerg Wunsch 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
12856182fdbdSPeter Wemm 		return (ENXIO);
12865b81b6b3SRodney W. Grimes 
12871a6bed68SJoerg Wunsch 	switch (fd->type) {
12881a6bed68SJoerg Wunsch 	case FDT_12M:
12891a6bed68SJoerg Wunsch 		device_set_desc(dev, "1200-KB 5.25\" drive");
12901a6bed68SJoerg Wunsch 		fd->type = FDT_12M;
12911a6bed68SJoerg Wunsch 		break;
12921a6bed68SJoerg Wunsch 	case FDT_144M:
12931a6bed68SJoerg Wunsch 		device_set_desc(dev, "1440-KB 3.5\" drive");
12941a6bed68SJoerg Wunsch 		fd->type = FDT_144M;
12951a6bed68SJoerg Wunsch 		break;
12961a6bed68SJoerg Wunsch 	case FDT_288M:
12971a6bed68SJoerg Wunsch 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
12981a6bed68SJoerg Wunsch 		fd->type = FDT_288M;
12991a6bed68SJoerg Wunsch 		break;
13001a6bed68SJoerg Wunsch 	case FDT_360K:
13011a6bed68SJoerg Wunsch 		device_set_desc(dev, "360-KB 5.25\" drive");
13021a6bed68SJoerg Wunsch 		fd->type = FDT_360K;
13031a6bed68SJoerg Wunsch 		break;
13041a6bed68SJoerg Wunsch 	case FDT_720K:
13051a6bed68SJoerg Wunsch 		device_set_desc(dev, "720-KB 3.5\" drive");
13061a6bed68SJoerg Wunsch 		fd->type = FDT_720K;
13071a6bed68SJoerg Wunsch 		break;
13081a6bed68SJoerg Wunsch 	default:
13091a6bed68SJoerg Wunsch 		return (ENXIO);
13101a6bed68SJoerg Wunsch 	}
1311dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1312b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1313b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
13143a2f7427SDavid Greenman 	fd->options = 0;
131502a19910SJustin T. Gibbs 	callout_handle_init(&fd->toffhandle);
131602a19910SJustin T. Gibbs 	callout_handle_init(&fd->tohandle);
13175b81b6b3SRodney W. Grimes 
13181a6bed68SJoerg Wunsch 	/* initialize densities for subdevices */
13191a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS; i++)
13201a6bed68SJoerg Wunsch 		memcpy(fd->fts + i, fd_native_types + fd->type,
13211a6bed68SJoerg Wunsch 		       sizeof(struct fd_type));
13226182fdbdSPeter Wemm 	return (0);
13236182fdbdSPeter Wemm }
13246182fdbdSPeter Wemm 
13256182fdbdSPeter Wemm static int
13266182fdbdSPeter Wemm fd_attach(device_t dev)
13276182fdbdSPeter Wemm {
13286182fdbdSPeter Wemm 	struct	fd_data *fd;
132960444853SJoerg Wunsch 	int i;
13306182fdbdSPeter Wemm 
133164860614SJoerg Wunsch 	fd = device_get_softc(dev);
133260444853SJoerg Wunsch 	fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, fd, 1000);
13335f431174SJoerg Wunsch 	fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6,
13343f54a085SPoul-Henning Kamp 				 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
13351a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS - 1; i++)
133660444853SJoerg Wunsch 		fd->clonedevs[i] = NODEV;
13376182fdbdSPeter Wemm 	devstat_add_entry(&fd->device_stats, device_get_name(dev),
1338f8ce7dd5SJoerg Wunsch 			  device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
13392a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
13402a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_FD);
13416182fdbdSPeter Wemm 	return (0);
13425b81b6b3SRodney W. Grimes }
13435b81b6b3SRodney W. Grimes 
13445f830ea2SJoerg Wunsch static int
13455f830ea2SJoerg Wunsch fd_detach(device_t dev)
13465f830ea2SJoerg Wunsch {
13475f830ea2SJoerg Wunsch 	struct	fd_data *fd;
134860444853SJoerg Wunsch 	int i;
13495f830ea2SJoerg Wunsch 
13505f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
135160444853SJoerg Wunsch 	untimeout(fd_turnoff, fd, fd->toffhandle);
1352e219897aSJoerg Wunsch 	devstat_remove_entry(&fd->device_stats);
1353e219897aSJoerg Wunsch 	destroy_dev(fd->masterdev);
13541a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS - 1; i++)
135560444853SJoerg Wunsch 		if (fd->clonedevs[i] != NODEV)
135660444853SJoerg Wunsch 			destroy_dev(fd->clonedevs[i]);
1357e219897aSJoerg Wunsch 	EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag);
13585f830ea2SJoerg Wunsch 
13595f830ea2SJoerg Wunsch 	return (0);
13605f830ea2SJoerg Wunsch }
13615f830ea2SJoerg Wunsch 
136216e68fc6SPeter Wemm static device_method_t fd_methods[] = {
136316e68fc6SPeter Wemm 	/* Device interface */
136416e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
136516e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
13665f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
136716e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
136816e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
136916e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
137016e68fc6SPeter Wemm 
137116e68fc6SPeter Wemm 	{ 0, 0 }
137216e68fc6SPeter Wemm };
137316e68fc6SPeter Wemm 
137416e68fc6SPeter Wemm static driver_t fd_driver = {
137516e68fc6SPeter Wemm 	"fd",
137616e68fc6SPeter Wemm 	fd_methods,
137716e68fc6SPeter Wemm 	sizeof(struct fd_data)
137816e68fc6SPeter Wemm };
137916e68fc6SPeter Wemm 
1380475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
138116e68fc6SPeter Wemm 
1382246ed35dSJoerg Wunsch /*
1383246ed35dSJoerg Wunsch  * More auxiliary functions.
1384246ed35dSJoerg Wunsch  */
1385246ed35dSJoerg Wunsch /*
1386246ed35dSJoerg Wunsch  * Motor control stuff.
1387246ed35dSJoerg Wunsch  * Remember to not deselect the drive we're working on.
1388246ed35dSJoerg Wunsch  */
13893a2f7427SDavid Greenman static void
13906182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon)
13915b81b6b3SRodney W. Grimes {
1392fb35bd37SJoerg Wunsch 	int fdout;
13933a2f7427SDavid Greenman 
1394fb35bd37SJoerg Wunsch 	fdout = fdc->fdout;
13953a2f7427SDavid Greenman 	if (turnon) {
13963a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
1397fb35bd37SJoerg Wunsch 		fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
13983a2f7427SDavid Greenman 	} else
13993a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
14006182fdbdSPeter Wemm 	fdc->fdout = fdout;
1401fb35bd37SJoerg Wunsch 	fdout_wr(fdc, fdout);
14023a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
14033a2f7427SDavid Greenman }
14043a2f7427SDavid Greenman 
1405381fe1aaSGarrett Wollman static void
14066182fdbdSPeter Wemm fd_turnoff(void *xfd)
14075b81b6b3SRodney W. Grimes {
1408f5f7ba03SJordan K. Hubbard 	int	s;
14096182fdbdSPeter Wemm 	fd_p fd = xfd;
1410dc16046fSJoerg Wunsch 
14116182fdbdSPeter Wemm 	TRACE1("[fd%d: turnoff]", fd->fdu);
14128335c1b8SBruce Evans 
14135f830ea2SJoerg Wunsch 	s = splbio();
14148335c1b8SBruce Evans 	/*
14158335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
14165f830ea2SJoerg Wunsch 	 *
14175f830ea2SJoerg Wunsch 	 * If we got here, this could only mean we missed an interrupt.
14185f830ea2SJoerg Wunsch 	 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
14195f830ea2SJoerg Wunsch 	 * after a controller reset.  Just schedule a pseudo-interrupt
14205f830ea2SJoerg Wunsch 	 * so the state machine gets re-entered.
14218335c1b8SBruce Evans 	 */
14226182fdbdSPeter Wemm 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
14235f830ea2SJoerg Wunsch 		fdc_intr(fd->fdc);
14245f830ea2SJoerg Wunsch 		splx(s);
14258335c1b8SBruce Evans 		return;
14268335c1b8SBruce Evans 	}
14278335c1b8SBruce Evans 
14285b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
14296182fdbdSPeter Wemm 	set_motor(fd->fdc, fd->fdsu, TURNOFF);
1430f5f7ba03SJordan K. Hubbard 	splx(s);
14315b81b6b3SRodney W. Grimes }
14325b81b6b3SRodney W. Grimes 
14333a2f7427SDavid Greenman static void
14346182fdbdSPeter Wemm fd_motor_on(void *xfd)
14355b81b6b3SRodney W. Grimes {
1436f5f7ba03SJordan K. Hubbard 	int	s;
14376182fdbdSPeter Wemm 	fd_p fd = xfd;
1438f5f7ba03SJordan K. Hubbard 
1439f5f7ba03SJordan K. Hubbard 	s = splbio();
14405b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
14415b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
14425b81b6b3SRodney W. Grimes 	{
14436182fdbdSPeter Wemm 		fdc_intr(fd->fdc);
14445b81b6b3SRodney W. Grimes 	}
1445f5f7ba03SJordan K. Hubbard 	splx(s);
14465b81b6b3SRodney W. Grimes }
14475b81b6b3SRodney W. Grimes 
14483a2f7427SDavid Greenman static void
14496182fdbdSPeter Wemm fd_turnon(fd_p fd)
14505b81b6b3SRodney W. Grimes {
14515b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
14525b81b6b3SRodney W. Grimes 	{
14533a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
14546182fdbdSPeter Wemm 		set_motor(fd->fdc, fd->fdsu, TURNON);
14556182fdbdSPeter Wemm 		timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
14565b81b6b3SRodney W. Grimes 	}
14575b81b6b3SRodney W. Grimes }
14585b81b6b3SRodney W. Grimes 
1459381fe1aaSGarrett Wollman static void
1460dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
14615b81b6b3SRodney W. Grimes {
14623a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
1463427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
14643a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
14653a2f7427SDavid Greenman 	DELAY(100);
14663a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
1467427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
14683a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
14693a2f7427SDavid Greenman 	DELAY(100);
1470427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout);
14713a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
14723a2f7427SDavid Greenman 
147364860614SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
14746182fdbdSPeter Wemm 	(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1475dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1476dc5df763SJoerg Wunsch 		     0);
1477d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1478d66c374fSTor Egge 		(void) enable_fifo(fdc);
14795b81b6b3SRodney W. Grimes }
14805b81b6b3SRodney W. Grimes 
1481246ed35dSJoerg Wunsch /*
1482246ed35dSJoerg Wunsch  * FDC IO functions, take care of the main status register, timeout
1483246ed35dSJoerg Wunsch  * in case the desired status bits are never set.
1484a2642c4dSJoerg Wunsch  *
1485a2642c4dSJoerg Wunsch  * These PIO loops initially start out with short delays between
1486a2642c4dSJoerg Wunsch  * each iteration in the expectation that the required condition
1487a2642c4dSJoerg Wunsch  * is usually met quickly, so it can be handled immediately.  After
1488a2642c4dSJoerg Wunsch  * about 1 ms, stepping is increased to achieve a better timing
1489a2642c4dSJoerg Wunsch  * accuracy in the calls to DELAY().
1490246ed35dSJoerg Wunsch  */
1491b5e8ce9fSBruce Evans static int
14926182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr)
1493dc5df763SJoerg Wunsch {
1494a2642c4dSJoerg Wunsch 	int i, j, step;
1495a2642c4dSJoerg Wunsch 
1496a2642c4dSJoerg Wunsch 	for (j = 0, step = 1;
1497a2642c4dSJoerg Wunsch 	    (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1498a2642c4dSJoerg Wunsch 	    j < FDSTS_TIMEOUT;
1499a2642c4dSJoerg Wunsch 	    j += step) {
1500dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
15018a0ba818SJoerg Wunsch 			return (fdc_err(fdc, "ready for output in input\n"));
1502a2642c4dSJoerg Wunsch 		if (j == 1000)
1503a2642c4dSJoerg Wunsch 			step = 1000;
1504a2642c4dSJoerg Wunsch 		DELAY(step);
1505d0900d6bSJoerg Wunsch 	}
1506a2642c4dSJoerg Wunsch 	if (j >= FDSTS_TIMEOUT)
15078a0ba818SJoerg Wunsch 		return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1508d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1509427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1510dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1511dc5df763SJoerg Wunsch 	*ptr = i;
15128a0ba818SJoerg Wunsch 	return (0);
1513d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1514427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1515dc5df763SJoerg Wunsch 	if (ptr)
1516dc5df763SJoerg Wunsch 		*ptr = i;
15178a0ba818SJoerg Wunsch 	return (0);
1518d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1519dc5df763SJoerg Wunsch }
1520dc5df763SJoerg Wunsch 
152137c84183SPoul-Henning Kamp static int
15226182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x)
15235b81b6b3SRodney W. Grimes {
1524a2642c4dSJoerg Wunsch 	int i, j, step;
15255b81b6b3SRodney W. Grimes 
1526a2642c4dSJoerg Wunsch 	for (j = 0, step = 1;
1527a2642c4dSJoerg Wunsch 	    (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1528a2642c4dSJoerg Wunsch 	    j < FDSTS_TIMEOUT;
1529a2642c4dSJoerg Wunsch 	    j += step) {
1530a2642c4dSJoerg Wunsch 		if (i == (NE7_DIO|NE7_RQM))
1531a2642c4dSJoerg Wunsch 			return (fdc_err(fdc, "ready for input in output\n"));
1532a2642c4dSJoerg Wunsch 		if (j == 1000)
1533a2642c4dSJoerg Wunsch 			step = 1000;
1534a2642c4dSJoerg Wunsch 		DELAY(step);
1535a2642c4dSJoerg Wunsch 	}
1536a2642c4dSJoerg Wunsch 	if (j >= FDSTS_TIMEOUT)
15378a0ba818SJoerg Wunsch 		return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
15383b3837dbSRodney W. Grimes 
15393b3837dbSRodney W. Grimes 	/* Send the command and return */
1540427ccf00SDoug Rabson 	fddata_wr(fdc, x);
15413a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
15425b81b6b3SRodney W. Grimes 	return (0);
15435b81b6b3SRodney W. Grimes }
15445b81b6b3SRodney W. Grimes 
1545246ed35dSJoerg Wunsch /*
1546246ed35dSJoerg Wunsch  * Block device driver interface functions (interspersed with even more
1547246ed35dSJoerg Wunsch  * auxiliary functions).
1548246ed35dSJoerg Wunsch  */
154937c84183SPoul-Henning Kamp static int
1550b40ce416SJulian Elischer Fdopen(dev_t dev, int flags, int mode, struct thread *td)
15515b81b6b3SRodney W. Grimes {
15525b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
155320a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
15546182fdbdSPeter Wemm 	fd_p	fd;
1555b99f0a4aSAndrew Moore 	fdc_p	fdc;
15561a6bed68SJoerg Wunsch  	int rv, unitattn, dflags;
15575b81b6b3SRodney W. Grimes 
15586182fdbdSPeter Wemm 	if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1559b99f0a4aSAndrew Moore 		return (ENXIO);
15606182fdbdSPeter Wemm 	fdc = fd->fdc;
15611a6bed68SJoerg Wunsch 	if ((fdc == NULL) || (fd->type == FDT_NONE))
1562b99f0a4aSAndrew Moore 		return (ENXIO);
1563b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1564b99f0a4aSAndrew Moore 		return (ENXIO);
15651a6bed68SJoerg Wunsch 	dflags = device_get_flags(fd->dev);
15663e425b96SJulian Elischer 	/*
15671a6bed68SJoerg Wunsch 	 * This is a bit bogus.  It's still possible that e. g. a
15681a6bed68SJoerg Wunsch 	 * descriptor gets inherited to a child, but then it's at
15691a6bed68SJoerg Wunsch 	 * least for the same subdevice.  By checking FD_OPEN here, we
15701a6bed68SJoerg Wunsch 	 * can ensure that a device isn't attempted to be opened with
15711a6bed68SJoerg Wunsch 	 * different densities at the same time where the second open
15721a6bed68SJoerg Wunsch 	 * could clobber the settings from the first one.
15733e425b96SJulian Elischer 	 */
15741a6bed68SJoerg Wunsch 	if (fd->flags & FD_OPEN)
15751a6bed68SJoerg Wunsch 		return (EBUSY);
15761a6bed68SJoerg Wunsch 
15771a6bed68SJoerg Wunsch 	if (type == 0) {
15781a6bed68SJoerg Wunsch 		if (flags & FNONBLOCK) {
15791a6bed68SJoerg Wunsch 			/*
15801a6bed68SJoerg Wunsch 			 * Unfortunately, physio(9) discards its ioflag
15811a6bed68SJoerg Wunsch 			 * argument, thus preventing us from seeing the
15821a6bed68SJoerg Wunsch 			 * IO_NDELAY bit.  So we need to keep track
15831a6bed68SJoerg Wunsch 			 * ourselves.
15841a6bed68SJoerg Wunsch 			 */
15851a6bed68SJoerg Wunsch 			fd->flags |= FD_NONBLOCK;
15861a6bed68SJoerg Wunsch 			fd->ft = 0;
15871a6bed68SJoerg Wunsch 		} else {
15881a6bed68SJoerg Wunsch 			/*
15891a6bed68SJoerg Wunsch 			 * Figure out a unit attention condition.
15901a6bed68SJoerg Wunsch 			 *
15911a6bed68SJoerg Wunsch 			 * If UA has been forced, proceed.
15921a6bed68SJoerg Wunsch 			 *
15931a6bed68SJoerg Wunsch 			 * If motor is off, turn it on for a moment
15941a6bed68SJoerg Wunsch 			 * and select our drive, in order to read the
15951a6bed68SJoerg Wunsch 			 * UA hardware signal.
15961a6bed68SJoerg Wunsch 			 *
15971a6bed68SJoerg Wunsch 			 * If motor is on, and our drive is currently
15981a6bed68SJoerg Wunsch 			 * selected, just read the hardware bit.
15991a6bed68SJoerg Wunsch 			 *
16001a6bed68SJoerg Wunsch 			 * If motor is on, but active for another
16011a6bed68SJoerg Wunsch 			 * drive on that controller, we are lost.  We
16021a6bed68SJoerg Wunsch 			 * cannot risk to deselect the other drive, so
16031a6bed68SJoerg Wunsch 			 * we just assume a forced UA condition to be
16041a6bed68SJoerg Wunsch 			 * on the safe side.
16051a6bed68SJoerg Wunsch 			 */
16061a6bed68SJoerg Wunsch 			unitattn = 0;
16071a6bed68SJoerg Wunsch 			if ((dflags & FD_NO_CHLINE) != 0 ||
16081a6bed68SJoerg Wunsch 			    (fd->flags & FD_UA) != 0) {
16091a6bed68SJoerg Wunsch 				unitattn = 1;
16101a6bed68SJoerg Wunsch 				fd->flags &= ~FD_UA;
16111a6bed68SJoerg Wunsch 			} else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 |
16121a6bed68SJoerg Wunsch 						 FDO_MOEN2 | FDO_MOEN3)) {
16131a6bed68SJoerg Wunsch 				if ((fdc->fdout & FDO_FDSEL) == fd->fdsu)
16141a6bed68SJoerg Wunsch 					unitattn = fdin_rd(fdc) & FDI_DCHG;
16151a6bed68SJoerg Wunsch 				else
16161a6bed68SJoerg Wunsch 					unitattn = 1;
16171a6bed68SJoerg Wunsch 			} else {
16181a6bed68SJoerg Wunsch 				set_motor(fdc, fd->fdsu, TURNON);
16191a6bed68SJoerg Wunsch 				unitattn = fdin_rd(fdc) & FDI_DCHG;
16201a6bed68SJoerg Wunsch 				set_motor(fdc, fd->fdsu, TURNOFF);
1621b39c878eSAndrey A. Chernov 			}
16221a6bed68SJoerg Wunsch 			if (unitattn && (rv = fdautoselect(dev)) != 0)
16231a6bed68SJoerg Wunsch 				return (rv);
16247ca0641bSAndrey A. Chernov 		}
16251a6bed68SJoerg Wunsch 	} else {
16261a6bed68SJoerg Wunsch 		fd->ft = fd->fts + type;
16277ca0641bSAndrey A. Chernov 	}
16286182fdbdSPeter Wemm 	fd->flags |= FD_OPEN;
16293fef646eSJoerg Wunsch 	/*
16303fef646eSJoerg Wunsch 	 * Clearing the DMA overrun counter at open time is a bit messy.
16313fef646eSJoerg Wunsch 	 * Since we're only managing one counter per controller, opening
16323fef646eSJoerg Wunsch 	 * the second drive could mess it up.  Anyway, if the DMA overrun
16333fef646eSJoerg Wunsch 	 * condition is really persistent, it will eventually time out
16343fef646eSJoerg Wunsch 	 * still.  OTOH, clearing it here will ensure we'll at least start
16353fef646eSJoerg Wunsch 	 * trying again after a previous (maybe even long ago) failure.
16363fef646eSJoerg Wunsch 	 * Also, this is merely a stop-gap measure only that should not
16373fef646eSJoerg Wunsch 	 * happen during normal operation, so we can tolerate it to be a
16383fef646eSJoerg Wunsch 	 * bit sloppy about this.
16393fef646eSJoerg Wunsch 	 */
16403fef646eSJoerg Wunsch 	fdc->dma_overruns = 0;
16415f830ea2SJoerg Wunsch 
16425b81b6b3SRodney W. Grimes 	return 0;
16435b81b6b3SRodney W. Grimes }
16445b81b6b3SRodney W. Grimes 
164537c84183SPoul-Henning Kamp static int
1646b40ce416SJulian Elischer fdclose(dev_t dev, int flags, int mode, struct thread *td)
16475b81b6b3SRodney W. Grimes {
16485b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
16496182fdbdSPeter Wemm 	struct fd_data *fd;
1650b99f0a4aSAndrew Moore 
16516182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
16521a6bed68SJoerg Wunsch 	fd->flags &= ~(FD_OPEN | FD_NONBLOCK);
16532995d110SJoerg Wunsch 	fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1654dc16046fSJoerg Wunsch 
16555b81b6b3SRodney W. Grimes 	return (0);
16565b81b6b3SRodney W. Grimes }
16575b81b6b3SRodney W. Grimes 
165837c84183SPoul-Henning Kamp static void
16598177437dSPoul-Henning Kamp fdstrategy(struct bio *bp)
16603a2f7427SDavid Greenman {
1661fb35bd37SJoerg Wunsch 	long blknum, nblocks;
16623a2f7427SDavid Greenman  	int	s;
16633a2f7427SDavid Greenman  	fdu_t	fdu;
16643a2f7427SDavid Greenman  	fdc_p	fdc;
16653a2f7427SDavid Greenman  	fd_p	fd;
16663a2f7427SDavid Greenman 	size_t	fdblk;
16673a2f7427SDavid Greenman 
16688177437dSPoul-Henning Kamp  	fdu = FDUNIT(minor(bp->bio_dev));
16696182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
16706182fdbdSPeter Wemm 	if (fd == 0)
16716182fdbdSPeter Wemm 		panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
16728177437dSPoul-Henning Kamp 		      (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev));
16733a2f7427SDavid Greenman 	fdc = fd->fdc;
16741a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE || fd->ft == 0) {
16758177437dSPoul-Henning Kamp 		bp->bio_error = ENXIO;
16768177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
16773b178206SWarner Losh 		goto bad;
167864860614SJoerg Wunsch 	}
1679d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
1680419f39ceSPoul-Henning Kamp 	if (bp->bio_cmd != FDBIO_FORMAT && bp->bio_cmd != FDBIO_RDSECTID) {
16811a6bed68SJoerg Wunsch 		if (fd->flags & FD_NONBLOCK) {
16821a6bed68SJoerg Wunsch 			bp->bio_error = EAGAIN;
16831a6bed68SJoerg Wunsch 			bp->bio_flags |= BIO_ERROR;
16841a6bed68SJoerg Wunsch 			goto bad;
16851a6bed68SJoerg Wunsch 		}
16868177437dSPoul-Henning Kamp 		if (bp->bio_blkno < 0) {
1687dc5df763SJoerg Wunsch 			printf(
16886a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
16898177437dSPoul-Henning Kamp 			       fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
16908177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
16918177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
16923a2f7427SDavid Greenman 			goto bad;
16933a2f7427SDavid Greenman 		}
16948177437dSPoul-Henning Kamp 		if ((bp->bio_bcount % fdblk) != 0) {
16958177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
16968177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
16973a2f7427SDavid Greenman 			goto bad;
16983a2f7427SDavid Greenman 		}
16993a2f7427SDavid Greenman 	}
17003a2f7427SDavid Greenman 
17013a2f7427SDavid Greenman 	/*
17023a2f7427SDavid Greenman 	 * Set up block calculations.
17033a2f7427SDavid Greenman 	 */
17048177437dSPoul-Henning Kamp 	if (bp->bio_blkno > 20000000) {
1705bb6382faSJoerg Wunsch 		/*
1706bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1707bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1708bb6382faSJoerg Wunsch 		 */
17098177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
17108177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
17113a2f7427SDavid Greenman 		goto bad;
17123a2f7427SDavid Greenman 	}
1713fb35bd37SJoerg Wunsch 	blknum = bp->bio_blkno * DEV_BSIZE / fdblk;
1714bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1715fb35bd37SJoerg Wunsch 	if (blknum + bp->bio_bcount / fdblk > nblocks) {
1716fb35bd37SJoerg Wunsch 		if (blknum >= nblocks) {
1717fb35bd37SJoerg Wunsch 			if (bp->bio_cmd == BIO_READ)
1718fb35bd37SJoerg Wunsch 				bp->bio_resid = bp->bio_bcount;
1719fb35bd37SJoerg Wunsch 			else {
1720fb35bd37SJoerg Wunsch 				bp->bio_error = ENOSPC;
17218177437dSPoul-Henning Kamp 				bp->bio_flags |= BIO_ERROR;
1722bb6382faSJoerg Wunsch 			}
1723fb35bd37SJoerg Wunsch 			goto bad;	/* not always bad, but EOF */
1724fb35bd37SJoerg Wunsch 		}
1725fb35bd37SJoerg Wunsch 		bp->bio_bcount = (nblocks - blknum) * fdblk;
1726bb6382faSJoerg Wunsch 	}
17279a5e3ddbSJoerg Wunsch  	bp->bio_pblkno = blknum;
17283a2f7427SDavid Greenman 	s = splbio();
17298177437dSPoul-Henning Kamp 	bioqdisksort(&fdc->head, bp);
17306182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1731b2dfb1f9SJustin T. Gibbs 	devstat_start_transaction(&fd->device_stats);
17325f830ea2SJoerg Wunsch 	device_busy(fd->dev);
17336182fdbdSPeter Wemm 	fdstart(fdc);
17343a2f7427SDavid Greenman 	splx(s);
17353a2f7427SDavid Greenman 	return;
17363a2f7427SDavid Greenman 
17373a2f7427SDavid Greenman bad:
17383a2f7427SDavid Greenman 	biodone(bp);
17393a2f7427SDavid Greenman }
17403a2f7427SDavid Greenman 
1741246ed35dSJoerg Wunsch /*
1742246ed35dSJoerg Wunsch  * fdstart
1743246ed35dSJoerg Wunsch  *
1744246ed35dSJoerg Wunsch  * We have just queued something.  If the controller is not busy
1745246ed35dSJoerg Wunsch  * then simulate the case where it has just finished a command
1746246ed35dSJoerg Wunsch  * So that it (the interrupt routine) looks on the queue for more
1747246ed35dSJoerg Wunsch  * work to do and picks up what we just added.
1748246ed35dSJoerg Wunsch  *
1749246ed35dSJoerg Wunsch  * If the controller is already busy, we need do nothing, as it
1750246ed35dSJoerg Wunsch  * will pick up our work when the present work completes.
1751246ed35dSJoerg Wunsch  */
1752381fe1aaSGarrett Wollman static void
17536182fdbdSPeter Wemm fdstart(struct fdc_data *fdc)
17545b81b6b3SRodney W. Grimes {
17555b81b6b3SRodney W. Grimes 	int s;
17565b81b6b3SRodney W. Grimes 
17575b81b6b3SRodney W. Grimes 	s = splbio();
17586182fdbdSPeter Wemm 	if(fdc->state == DEVIDLE)
17595b81b6b3SRodney W. Grimes 	{
17606182fdbdSPeter Wemm 		fdc_intr(fdc);
17615b81b6b3SRodney W. Grimes 	}
17625b81b6b3SRodney W. Grimes 	splx(s);
17635b81b6b3SRodney W. Grimes }
17645b81b6b3SRodney W. Grimes 
1765381fe1aaSGarrett Wollman static void
17666182fdbdSPeter Wemm fd_iotimeout(void *xfdc)
17675b81b6b3SRodney W. Grimes {
17685c1a1eaeSBruce Evans  	fdc_p fdc;
1769f5f7ba03SJordan K. Hubbard 	int s;
17705b81b6b3SRodney W. Grimes 
17716182fdbdSPeter Wemm 	fdc = xfdc;
17725c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
17735b81b6b3SRodney W. Grimes 
17743a2f7427SDavid Greenman 	/*
17753a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
17763a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
17773a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
17783a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
17793a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
17805c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
17815c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
17825c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
17835c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
17843a2f7427SDavid Greenman 	 */
17853a2f7427SDavid Greenman 	s = splbio();
17865c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
17875c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
17885c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
17896182fdbdSPeter Wemm 	fdc_intr(fdc);
1790f5f7ba03SJordan K. Hubbard 	splx(s);
17915b81b6b3SRodney W. Grimes }
17925b81b6b3SRodney W. Grimes 
1793246ed35dSJoerg Wunsch /* Just ensure it has the right spl. */
1794381fe1aaSGarrett Wollman static void
17956182fdbdSPeter Wemm fd_pseudointr(void *xfdc)
17965b81b6b3SRodney W. Grimes {
17975b81b6b3SRodney W. Grimes 	int	s;
17983a2f7427SDavid Greenman 
17995b81b6b3SRodney W. Grimes 	s = splbio();
18006182fdbdSPeter Wemm 	fdc_intr(xfdc);
18015b81b6b3SRodney W. Grimes 	splx(s);
18025b81b6b3SRodney W. Grimes }
18035b81b6b3SRodney W. Grimes 
1804246ed35dSJoerg Wunsch /*
1805246ed35dSJoerg Wunsch  * fdc_intr
1806246ed35dSJoerg Wunsch  *
1807246ed35dSJoerg Wunsch  * Keep calling the state machine until it returns a 0.
1808246ed35dSJoerg Wunsch  * Always called at splbio.
1809246ed35dSJoerg Wunsch  */
1810fe310de8SBruce Evans static void
18116182fdbdSPeter Wemm fdc_intr(void *xfdc)
18125b81b6b3SRodney W. Grimes {
18136182fdbdSPeter Wemm 	fdc_p fdc = xfdc;
18146182fdbdSPeter Wemm 	while(fdstate(fdc))
1815381fe1aaSGarrett Wollman 		;
18165b81b6b3SRodney W. Grimes }
18175b81b6b3SRodney W. Grimes 
181869acd21dSWarner Losh /*
1819246ed35dSJoerg Wunsch  * Magic pseudo-DMA initialization for YE FDC. Sets count and
1820246ed35dSJoerg Wunsch  * direction.
182169acd21dSWarner Losh  */
18223b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \
18233b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port,	 \
18243b178206SWarner Losh 	    ((cnt)-1) & 0xff);						 \
18253b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
18263b178206SWarner Losh 	    ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
182769acd21dSWarner Losh 
182869acd21dSWarner Losh /*
1829246ed35dSJoerg Wunsch  * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy.
183069acd21dSWarner Losh  */
18314c34deeeSJoerg Wunsch static int
18324c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
183369acd21dSWarner Losh {
183469acd21dSWarner Losh 	u_char *cptr = (u_char *)addr;
183569acd21dSWarner Losh 
183621144e3bSPoul-Henning Kamp 	if (flags == BIO_READ) {
183769acd21dSWarner Losh 		if (fdc->state != PIOREAD) {
183869acd21dSWarner Losh 			fdc->state = PIOREAD;
183969acd21dSWarner Losh 			return(0);
184064860614SJoerg Wunsch 		}
18413b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
18423b178206SWarner Losh 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
18433b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
184469acd21dSWarner Losh 	} else {
18453b178206SWarner Losh 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
18463b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
18473b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
184864860614SJoerg Wunsch 	}
184969acd21dSWarner Losh 	return(1);
185069acd21dSWarner Losh }
185169acd21dSWarner Losh 
1852246ed35dSJoerg Wunsch /*
18531a6bed68SJoerg Wunsch  * Try figuring out the density of the media present in our device.
18541a6bed68SJoerg Wunsch  */
18551a6bed68SJoerg Wunsch static int
18561a6bed68SJoerg Wunsch fdautoselect(dev_t dev)
18571a6bed68SJoerg Wunsch {
18581a6bed68SJoerg Wunsch 	fdu_t fdu;
18591a6bed68SJoerg Wunsch  	fd_p fd;
18601a6bed68SJoerg Wunsch 	struct fd_type *fdtp;
18611a6bed68SJoerg Wunsch 	struct fdc_readid id;
18621a6bed68SJoerg Wunsch 	int i, n, oopts, rv;
18631a6bed68SJoerg Wunsch 
18641a6bed68SJoerg Wunsch  	fdu = FDUNIT(minor(dev));
18651a6bed68SJoerg Wunsch 	fd = devclass_get_softc(fd_devclass, fdu);
18661a6bed68SJoerg Wunsch 
18671a6bed68SJoerg Wunsch 	switch (fd->type) {
18681a6bed68SJoerg Wunsch 	default:
18691a6bed68SJoerg Wunsch 		return (ENXIO);
18701a6bed68SJoerg Wunsch 
18711a6bed68SJoerg Wunsch 	case FDT_360K:
18721a6bed68SJoerg Wunsch 	case FDT_720K:
18731a6bed68SJoerg Wunsch 		/* no autoselection on those drives */
18741a6bed68SJoerg Wunsch 		fd->ft = fd_native_types + fd->type;
18751a6bed68SJoerg Wunsch 		return (0);
18761a6bed68SJoerg Wunsch 
18771a6bed68SJoerg Wunsch 	case FDT_12M:
18781a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_12m;
18791a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_12m / sizeof(struct fd_type);
18801a6bed68SJoerg Wunsch 		break;
18811a6bed68SJoerg Wunsch 
18821a6bed68SJoerg Wunsch 	case FDT_144M:
18831a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_144m;
18841a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_144m / sizeof(struct fd_type);
18851a6bed68SJoerg Wunsch 		break;
18861a6bed68SJoerg Wunsch 
18871a6bed68SJoerg Wunsch 	case FDT_288M:
18881a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_288m;
18891a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_288m / sizeof(struct fd_type);
18901a6bed68SJoerg Wunsch 		break;
18911a6bed68SJoerg Wunsch 	}
18921a6bed68SJoerg Wunsch 
18931a6bed68SJoerg Wunsch 	/*
18941a6bed68SJoerg Wunsch 	 * Try reading sector ID fields, first at cylinder 0, head 0,
18951a6bed68SJoerg Wunsch 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
18961a6bed68SJoerg Wunsch 	 * since for 5.25in DD media in a HD drive, there are no data
18971a6bed68SJoerg Wunsch 	 * to read (2 step pulses per media cylinder required).  For
18981a6bed68SJoerg Wunsch 	 * two-sided media, the second probe always goes to head 1, so
18991a6bed68SJoerg Wunsch 	 * we can tell them apart from single-sided media.  As a
19001a6bed68SJoerg Wunsch 	 * side-effect this means that single-sided media should be
19011a6bed68SJoerg Wunsch 	 * mentioned in the search list after two-sided media of an
19021a6bed68SJoerg Wunsch 	 * otherwise identical density.  Media with a different number
19031a6bed68SJoerg Wunsch 	 * of sectors per track but otherwise identical parameters
19041a6bed68SJoerg Wunsch 	 * cannot be distinguished at all.
19051a6bed68SJoerg Wunsch 	 *
19061a6bed68SJoerg Wunsch 	 * If we successfully read an ID field on both cylinders where
19071a6bed68SJoerg Wunsch 	 * the recorded values match our expectation, we are done.
19081a6bed68SJoerg Wunsch 	 * Otherwise, we try the next density entry from the table.
19091a6bed68SJoerg Wunsch 	 *
19101a6bed68SJoerg Wunsch 	 * Stepping to cylinder 2 has the side-effect of clearing the
19111a6bed68SJoerg Wunsch 	 * unit attention bit.
19121a6bed68SJoerg Wunsch 	 */
19131a6bed68SJoerg Wunsch 	oopts = fd->options;
19141a6bed68SJoerg Wunsch 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
19151a6bed68SJoerg Wunsch 	for (i = 0; i < n; i++, fdtp++) {
19161a6bed68SJoerg Wunsch 		fd->ft = fdtp;
19171a6bed68SJoerg Wunsch 
19181a6bed68SJoerg Wunsch 		id.cyl = id.head = 0;
1919419f39ceSPoul-Henning Kamp 		rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
19201a6bed68SJoerg Wunsch 		if (rv != 0)
19211a6bed68SJoerg Wunsch 			continue;
19221a6bed68SJoerg Wunsch 		if (id.cyl != 0 || id.head != 0 ||
19231a6bed68SJoerg Wunsch 		    id.secshift != fdtp->secsize)
19241a6bed68SJoerg Wunsch 			continue;
19251a6bed68SJoerg Wunsch 		id.cyl = 2;
19261a6bed68SJoerg Wunsch 		id.head = fd->ft->heads - 1;
1927419f39ceSPoul-Henning Kamp 		rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
19281a6bed68SJoerg Wunsch 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
19291a6bed68SJoerg Wunsch 		    id.secshift != fdtp->secsize)
19301a6bed68SJoerg Wunsch 			continue;
19311a6bed68SJoerg Wunsch 		if (rv == 0)
19321a6bed68SJoerg Wunsch 			break;
19331a6bed68SJoerg Wunsch 	}
19341a6bed68SJoerg Wunsch 
19351a6bed68SJoerg Wunsch 	fd->options = oopts;
19361a6bed68SJoerg Wunsch 	if (i == n) {
19375613959dSJoerg Wunsch 		if (bootverbose)
19381a6bed68SJoerg Wunsch 			device_printf(fd->dev, "autoselection failed\n");
19391a6bed68SJoerg Wunsch 		fd->ft = 0;
19401a6bed68SJoerg Wunsch 		return (EIO);
19411a6bed68SJoerg Wunsch 	} else {
19425613959dSJoerg Wunsch 		if (bootverbose)
19431a6bed68SJoerg Wunsch 			device_printf(fd->dev, "autoselected %d KB medium\n",
19441a6bed68SJoerg Wunsch 				      fd->ft->size / 2);
19451a6bed68SJoerg Wunsch 		return (0);
19461a6bed68SJoerg Wunsch 	}
19471a6bed68SJoerg Wunsch }
19481a6bed68SJoerg Wunsch 
19491a6bed68SJoerg Wunsch 
19501a6bed68SJoerg Wunsch /*
1951246ed35dSJoerg Wunsch  * The controller state machine.
1952246ed35dSJoerg Wunsch  *
1953246ed35dSJoerg Wunsch  * If it returns a non zero value, it should be called again immediately.
1954246ed35dSJoerg Wunsch  */
19553a2f7427SDavid Greenman static int
19566182fdbdSPeter Wemm fdstate(fdc_p fdc)
19575b81b6b3SRodney W. Grimes {
195864860614SJoerg Wunsch 	struct fdc_readid *idp;
1959fb35bd37SJoerg Wunsch 	int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
19601a6bed68SJoerg Wunsch 	int st0, cyl, st3, idf, ne7cmd, mfm, steptrac;
1961fb35bd37SJoerg Wunsch 	unsigned long blknum;
19625b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
19635b81b6b3SRodney W. Grimes 	fd_p fd;
19648177437dSPoul-Henning Kamp 	register struct bio *bp;
1965b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
19663a2f7427SDavid Greenman 	size_t fdblk;
19675b81b6b3SRodney W. Grimes 
1968e93e63cbSBruce Evans 	bp = fdc->bp;
1969e93e63cbSBruce Evans 	if (bp == NULL) {
19708177437dSPoul-Henning Kamp 		bp = bioq_first(&fdc->head);
1971e93e63cbSBruce Evans 		if (bp != NULL) {
19728177437dSPoul-Henning Kamp 			bioq_remove(&fdc->head, bp);
1973e93e63cbSBruce Evans 			fdc->bp = bp;
1974e93e63cbSBruce Evans 		}
1975e93e63cbSBruce Evans 	}
1976e93e63cbSBruce Evans 	if (bp == NULL) {
1977246ed35dSJoerg Wunsch 		/*
1978246ed35dSJoerg Wunsch 		 * Nothing left for this controller to do,
1979246ed35dSJoerg Wunsch 		 * force into the IDLE state.
1980246ed35dSJoerg Wunsch 		 */
19815b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
19826182fdbdSPeter Wemm 		if (fdc->fd) {
1983b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev,
1984b6e5f28eSPeter Wemm 			    "unexpected valid fd pointer\n");
19855b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
19865b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
19875b81b6b3SRodney W. Grimes 		}
19886182fdbdSPeter Wemm 		TRACE1("[fdc%d IDLE]", fdc->fdcu);
19895b81b6b3SRodney W. Grimes  		return (0);
19905b81b6b3SRodney W. Grimes 	}
19918177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
19926182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
19933a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1994b6e5f28eSPeter Wemm 	if (fdc->fd && (fd != fdc->fd))
1995b6e5f28eSPeter Wemm 		device_printf(fd->dev, "confused fd pointers\n");
19968177437dSPoul-Henning Kamp 	read = bp->bio_cmd == BIO_READ;
19971a6bed68SJoerg Wunsch 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
19981a6bed68SJoerg Wunsch 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
199956a23089SPoul-Henning Kamp 	if (read)
200056a23089SPoul-Henning Kamp 		idf = ISADMA_READ;
200156a23089SPoul-Henning Kamp 	else
200256a23089SPoul-Henning Kamp 		idf = ISADMA_WRITE;
2003419f39ceSPoul-Henning Kamp 	format = bp->bio_cmd == FDBIO_FORMAT;
2004419f39ceSPoul-Henning Kamp 	rdsectid = bp->bio_cmd == FDBIO_RDSECTID;
2005fb35bd37SJoerg Wunsch 	if (format)
20068177437dSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
20075b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
20085b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
20095b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
20106182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle);
20116182fdbdSPeter Wemm 	fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
20125b81b6b3SRodney W. Grimes 	switch (fdc->state)
20135b81b6b3SRodney W. Grimes 	{
20145b81b6b3SRodney W. Grimes 	case DEVIDLE:
20155b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
20165b81b6b3SRodney W. Grimes 		fdc->retry = 0;
20175b81b6b3SRodney W. Grimes 		fd->skip = 0;
20185b81b6b3SRodney W. Grimes 		fdc->fd = fd;
20195b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
20205f830ea2SJoerg Wunsch 		fdc->fdctl_wr(fdc, fd->ft->trans);
20213a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
2022246ed35dSJoerg Wunsch 		/*
2023246ed35dSJoerg Wunsch 		 * If the next drive has a motor startup pending, then
2024246ed35dSJoerg Wunsch 		 * it will start up in its own good time.
2025246ed35dSJoerg Wunsch 		 */
20266182fdbdSPeter Wemm 		if(fd->flags & FD_MOTOR_WAIT) {
20275b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
2028246ed35dSJoerg Wunsch 			return (0); /* will return later */
20295b81b6b3SRodney W. Grimes 		}
2030246ed35dSJoerg Wunsch 		/*
2031246ed35dSJoerg Wunsch 		 * Maybe if it's not starting, it SHOULD be starting.
2032246ed35dSJoerg Wunsch 		 */
20335b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
20345b81b6b3SRodney W. Grimes 		{
20355b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
20366182fdbdSPeter Wemm 			fd_turnon(fd);
2037246ed35dSJoerg Wunsch 			return (0); /* will return later */
20385b81b6b3SRodney W. Grimes 		}
20395b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
20405b81b6b3SRodney W. Grimes 		{
20416182fdbdSPeter Wemm 			set_motor(fdc, fd->fdsu, TURNON);
20425b81b6b3SRodney W. Grimes 		}
20435c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
20445c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
20455c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
20465c1a1eaeSBruce Evans 		} else
20475e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
2048246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2049fb35bd37SJoerg Wunsch 
20505b81b6b3SRodney W. Grimes 	case DOSEEK:
2051fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
2052fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2053fb35bd37SJoerg Wunsch 		if (cylinder == fd->track)
20545b81b6b3SRodney W. Grimes 		{
20555b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
2056246ed35dSJoerg Wunsch 			return (1); /* will return immediately */
20575b81b6b3SRodney W. Grimes 		}
20586182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK,
20591a6bed68SJoerg Wunsch 			   fd->fdsu, cylinder * steptrac, 0))
2060dc8603e3SJoerg Wunsch 		{
2061dc8603e3SJoerg Wunsch 			/*
2062246ed35dSJoerg Wunsch 			 * Seek command not accepted, looks like
2063dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
2064dc8603e3SJoerg Wunsch 			 */
2065dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
20666182fdbdSPeter Wemm 			return(retrier(fdc));
2067dc8603e3SJoerg Wunsch 		}
2068dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
20695b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
20705b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
2071fb35bd37SJoerg Wunsch 
20725b81b6b3SRodney W. Grimes 	case SEEKWAIT:
20735b81b6b3SRodney W. Grimes 		/* allow heads to settle */
20746182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 16);
20755b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
20765b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
2077fb35bd37SJoerg Wunsch 
2078246ed35dSJoerg Wunsch 	case SEEKCOMPLETE : /* seek done, start DMA */
2079fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
2080fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2081fb35bd37SJoerg Wunsch 
2082246ed35dSJoerg Wunsch 		/* Make sure seek really happened. */
20836182fdbdSPeter Wemm 		if(fd->track == FD_NO_TRACK) {
20841a6bed68SJoerg Wunsch 			int descyl = cylinder * steptrac;
20853a2f7427SDavid Greenman 			do {
20863a2f7427SDavid Greenman 				/*
2087dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
2088dc5df763SJoerg Wunsch 				 * which cannot really happen since the
2089dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
2090dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
2091dc5df763SJoerg Wunsch 				 * line, so do one of the following:
2092dc5df763SJoerg Wunsch 				 *
2093dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
2094dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
2095dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
2096dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
2097dc5df763SJoerg Wunsch 				 * real interrupt condition.
2098dc5df763SJoerg Wunsch 				 *
2099dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
2100dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
2101dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
2102dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
2103dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
2104dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
2105dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
21063a2f7427SDavid Greenman 				 */
2107dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
2108dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
2109246ed35dSJoerg Wunsch 					return (0); /* will return later */
2110dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
2111dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2112246ed35dSJoerg Wunsch 					return (0); /* hope for a real intr */
21133a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2114dc5df763SJoerg Wunsch 
21156182fdbdSPeter Wemm 			if (0 == descyl) {
2116dc5df763SJoerg Wunsch 				int failed = 0;
21173a2f7427SDavid Greenman 				/*
21183a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
21193a2f7427SDavid Greenman 				 * really there
21203a2f7427SDavid Greenman 				 */
2121dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
2122dc5df763SJoerg Wunsch 					failed = 1;
21233a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
21243a2f7427SDavid Greenman 					printf(
21253a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
21263a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
2127dc5df763SJoerg Wunsch 					failed = 1;
2128dc5df763SJoerg Wunsch 				}
2129dc5df763SJoerg Wunsch 
21306182fdbdSPeter Wemm 				if (failed) {
21313a2f7427SDavid Greenman 					if(fdc->retry < 3)
21323a2f7427SDavid Greenman 						fdc->retry = 3;
21336182fdbdSPeter Wemm 					return (retrier(fdc));
21343a2f7427SDavid Greenman 				}
21353a2f7427SDavid Greenman 			}
2136dc5df763SJoerg Wunsch 
21376182fdbdSPeter Wemm 			if (cyl != descyl) {
21383a2f7427SDavid Greenman 				printf(
21393a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
21402d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
2141e5d7d243SBruce Evans 				if (fdc->retry < 3)
2142e5d7d243SBruce Evans 					fdc->retry = 3;
21436182fdbdSPeter Wemm 				return (retrier(fdc));
21445b81b6b3SRodney W. Grimes 			}
21455b81b6b3SRodney W. Grimes 		}
21465b81b6b3SRodney W. Grimes 
2147fb35bd37SJoerg Wunsch 		fd->track = cylinder;
2148fb35bd37SJoerg Wunsch 		if (format)
2149fb35bd37SJoerg Wunsch 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
2150fb35bd37SJoerg Wunsch 			    - (char *)finfo;
2151250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
21528177437dSPoul-Henning Kamp 			isa_dmastart(idf, bp->bio_data+fd->skip,
21538177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
2154fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
21555b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
21565b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
21575b81b6b3SRodney W. Grimes 		head = sec / sectrac;
21585b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
21591a6bed68SJoerg Wunsch 		if (head != 0 && fd->ft->offset_side2 != 0)
21601a6bed68SJoerg Wunsch 			sec += fd->ft->offset_side2;
21613a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
21623a2f7427SDavid Greenman 
2163250300ebSJoerg Wunsch 		if(format || !(read || rdsectid))
21643a2f7427SDavid Greenman 		{
21653a2f7427SDavid Greenman 			/* make sure the drive is writable */
2166dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
2167dc8603e3SJoerg Wunsch 			{
2168dc8603e3SJoerg Wunsch 				/* stuck controller? */
21695f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
217056a23089SPoul-Henning Kamp 					isa_dmadone(idf,
21718177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
21728177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
21735c1a1eaeSBruce Evans 						    fdc->dmachan);
2174dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
21756182fdbdSPeter Wemm 				return (retrier(fdc));
2176dc8603e3SJoerg Wunsch 			}
21773a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
21783a2f7427SDavid Greenman 			{
21793a2f7427SDavid Greenman 				/*
21803a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
21813a2f7427SDavid Greenman 				 * in order to force the current operation
21823a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
21833a2f7427SDavid Greenman 				 * error - all error handling is done
21843a2f7427SDavid Greenman 				 * by the retrier()
21853a2f7427SDavid Greenman 				 */
21863a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
21873a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
21883a2f7427SDavid Greenman 				fdc->status[2] = 0;
21893a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
21903a2f7427SDavid Greenman 				fdc->status[4] = head;
21913a2f7427SDavid Greenman 				fdc->status[5] = sec;
21923a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
21933a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
2194246ed35dSJoerg Wunsch 				return (1); /* will return immediately */
21953a2f7427SDavid Greenman 			}
21963a2f7427SDavid Greenman 		}
21975b81b6b3SRodney W. Grimes 
21986182fdbdSPeter Wemm 		if (format) {
21991a6bed68SJoerg Wunsch 			ne7cmd = NE7CMD_FORMAT | mfm;
22005f830ea2SJoerg Wunsch 			if (fdc->flags & FDC_NODMA) {
22015f830ea2SJoerg Wunsch 				/*
22025f830ea2SJoerg Wunsch 				 * This seems to be necessary for
22035f830ea2SJoerg Wunsch 				 * whatever obscure reason; if we omit
22045f830ea2SJoerg Wunsch 				 * it, we end up filling the sector ID
22055f830ea2SJoerg Wunsch 				 * fields of the newly formatted track
22065f830ea2SJoerg Wunsch 				 * entirely with garbage, causing
22075f830ea2SJoerg Wunsch 				 * `wrong cylinder' errors all over
22085f830ea2SJoerg Wunsch 				 * the place when trying to read them
22095f830ea2SJoerg Wunsch 				 * back.
22105f830ea2SJoerg Wunsch 				 *
22115f830ea2SJoerg Wunsch 				 * Umpf.
22125f830ea2SJoerg Wunsch 				 */
22138177437dSPoul-Henning Kamp 				SET_BCDR(fdc, 1, bp->bio_bcount, 0);
22145f830ea2SJoerg Wunsch 
22158177437dSPoul-Henning Kamp 				(void)fdcpio(fdc,bp->bio_cmd,
22168177437dSPoul-Henning Kamp 					bp->bio_data+fd->skip,
22178177437dSPoul-Henning Kamp 					bp->bio_bcount);
22185f830ea2SJoerg Wunsch 
22195f830ea2SJoerg Wunsch 			}
2220b39c878eSAndrey A. Chernov 			/* formatting */
22211a6bed68SJoerg Wunsch 			if(fd_cmd(fdc, 6,  ne7cmd, head << 2 | fdu,
2222dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
2223dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
2224dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
22256182fdbdSPeter Wemm 				  finfo->fd_formb_fillbyte, 0)) {
2226dc8603e3SJoerg Wunsch 				/* controller fell over */
22275f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
222856a23089SPoul-Henning Kamp 					isa_dmadone(idf,
22298177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
22308177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
22315c1a1eaeSBruce Evans 						    fdc->dmachan);
2232dc8603e3SJoerg Wunsch 				fdc->retry = 6;
22336182fdbdSPeter Wemm 				return (retrier(fdc));
2234dc8603e3SJoerg Wunsch 			}
2235250300ebSJoerg Wunsch 		} else if (rdsectid) {
22361a6bed68SJoerg Wunsch 			ne7cmd = NE7CMD_READID | mfm;
22371a6bed68SJoerg Wunsch 			if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) {
2238250300ebSJoerg Wunsch 				/* controller jamming */
2239250300ebSJoerg Wunsch 				fdc->retry = 6;
2240250300ebSJoerg Wunsch 				return (retrier(fdc));
2241250300ebSJoerg Wunsch 			}
22426182fdbdSPeter Wemm 		} else {
2243250300ebSJoerg Wunsch 			/* read or write operation */
22441a6bed68SJoerg Wunsch 			ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm;
22453b178206SWarner Losh 			if (fdc->flags & FDC_NODMA) {
224669acd21dSWarner Losh 				/*
2247246ed35dSJoerg Wunsch 				 * This seems to be necessary even when
2248246ed35dSJoerg Wunsch 				 * reading data.
224969acd21dSWarner Losh 				 */
22503b178206SWarner Losh 				SET_BCDR(fdc, 1, fdblk, 0);
225169acd21dSWarner Losh 
225269acd21dSWarner Losh 				/*
2253246ed35dSJoerg Wunsch 				 * Perform the write pseudo-DMA before
2254246ed35dSJoerg Wunsch 				 * the WRITE command is sent.
225569acd21dSWarner Losh 				 */
225669acd21dSWarner Losh 				if (!read)
22578177437dSPoul-Henning Kamp 					(void)fdcpio(fdc,bp->bio_cmd,
22588177437dSPoul-Henning Kamp 					    bp->bio_data+fd->skip,
225969acd21dSWarner Losh 					    fdblk);
226069acd21dSWarner Losh 			}
22616182fdbdSPeter Wemm 			if (fd_cmd(fdc, 9,
22621a6bed68SJoerg Wunsch 				   ne7cmd,
2263dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
2264dc5df763SJoerg Wunsch 				   fd->track,        /* track */
2265dc5df763SJoerg Wunsch 				   head,
2266dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
2267dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
2268dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
2269dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
2270dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
22716182fdbdSPeter Wemm 				   0)) {
2272dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
22735f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
227456a23089SPoul-Henning Kamp 					isa_dmadone(idf,
22758177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
22768177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
22775c1a1eaeSBruce Evans 						    fdc->dmachan);
2278dc8603e3SJoerg Wunsch 				fdc->retry = 6;
22796182fdbdSPeter Wemm 				return (retrier(fdc));
22805b81b6b3SRodney W. Grimes 			}
2281b39c878eSAndrey A. Chernov 		}
2282250300ebSJoerg Wunsch 		if (!rdsectid && (fdc->flags & FDC_NODMA))
228369acd21dSWarner Losh 			/*
2284246ed35dSJoerg Wunsch 			 * If this is a read, then simply await interrupt
2285246ed35dSJoerg Wunsch 			 * before performing PIO.
228669acd21dSWarner Losh 			 */
22878177437dSPoul-Henning Kamp 			if (read && !fdcpio(fdc,bp->bio_cmd,
22888177437dSPoul-Henning Kamp 			    bp->bio_data+fd->skip,fdblk)) {
22893b178206SWarner Losh 				fd->tohandle = timeout(fd_iotimeout, fdc, hz);
229069acd21dSWarner Losh 				return(0);      /* will return later */
229164860614SJoerg Wunsch 			}
229269acd21dSWarner Losh 
229369acd21dSWarner Losh 		/*
2294246ed35dSJoerg Wunsch 		 * Write (or format) operation will fall through and
2295246ed35dSJoerg Wunsch 		 * await completion interrupt.
229669acd21dSWarner Losh 		 */
22975b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
22986182fdbdSPeter Wemm 		fd->tohandle = timeout(fd_iotimeout, fdc, hz);
22995b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2300fb35bd37SJoerg Wunsch 
230169acd21dSWarner Losh 	case PIOREAD:
230269acd21dSWarner Losh 		/*
2303246ed35dSJoerg Wunsch 		 * Actually perform the PIO read.  The IOCOMPLETE case
230469acd21dSWarner Losh 		 * removes the timeout for us.
230569acd21dSWarner Losh 		 */
23068177437dSPoul-Henning Kamp 		(void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
230769acd21dSWarner Losh 		fdc->state = IOCOMPLETE;
230869acd21dSWarner Losh 		/* FALLTHROUGH */
2309246ed35dSJoerg Wunsch 	case IOCOMPLETE: /* IO done, post-analyze */
23106182fdbdSPeter Wemm 		untimeout(fd_iotimeout, fdc, fd->tohandle);
2311dc5df763SJoerg Wunsch 
231264860614SJoerg Wunsch 		if (fd_read_status(fdc)) {
2313250300ebSJoerg Wunsch 			if (!rdsectid && !(fdc->flags & FDC_NODMA))
23148177437dSPoul-Henning Kamp 				isa_dmadone(idf, bp->bio_data + fd->skip,
23158177437dSPoul-Henning Kamp 					    format ? bp->bio_bcount : fdblk,
23165c1a1eaeSBruce Evans 					    fdc->dmachan);
2317dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
2318dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
23196182fdbdSPeter Wemm 			return (retrier(fdc));
23205b81b6b3SRodney W. Grimes   		}
2321dc5df763SJoerg Wunsch 
23223a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
2323dc5df763SJoerg Wunsch 
23243a2f7427SDavid Greenman 		/* FALLTHROUGH */
23253a2f7427SDavid Greenman 	case IOTIMEDOUT:
2326250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
23278177437dSPoul-Henning Kamp 			isa_dmadone(idf, bp->bio_data + fd->skip,
23288177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
23296182fdbdSPeter Wemm 		if (fdc->status[0] & NE7_ST0_IC) {
23303a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
23313a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
2332b39c878eSAndrey A. Chernov                                 /*
23333fef646eSJoerg Wunsch 				 * DMA overrun. Someone hogged the bus and
23343fef646eSJoerg Wunsch 				 * didn't release it in time for the next
23353fef646eSJoerg Wunsch 				 * FDC transfer.
23363fef646eSJoerg Wunsch 				 *
23373fef646eSJoerg Wunsch 				 * We normally restart this without bumping
23383fef646eSJoerg Wunsch 				 * the retry counter.  However, in case
23393fef646eSJoerg Wunsch 				 * something is seriously messed up (like
23403fef646eSJoerg Wunsch 				 * broken hardware), we rather limit the
23413fef646eSJoerg Wunsch 				 * number of retries so the IO operation
23423fef646eSJoerg Wunsch 				 * doesn't block indefinately.
2343b39c878eSAndrey A. Chernov 				 */
23443fef646eSJoerg Wunsch 				if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2345b39c878eSAndrey A. Chernov 					fdc->state = SEEKCOMPLETE;
2346246ed35dSJoerg Wunsch 					return (1);/* will return immediately */
23473fef646eSJoerg Wunsch 				} /* else fall through */
2348b39c878eSAndrey A. Chernov                         }
23493fef646eSJoerg Wunsch 			if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
23503a2f7427SDavid Greenman 				&& fdc->retry < 6)
23513a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
23523a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
23533a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
23543a2f7427SDavid Greenman 				&& fdc->retry < 3)
23553a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
23566182fdbdSPeter Wemm 			return (retrier(fdc));
23575b81b6b3SRodney W. Grimes 		}
23585b81b6b3SRodney W. Grimes 		/* All OK */
2359250300ebSJoerg Wunsch 		if (rdsectid) {
2360250300ebSJoerg Wunsch 			/* copy out ID field contents */
236164860614SJoerg Wunsch 			idp = (struct fdc_readid *)bp->bio_data;
2362250300ebSJoerg Wunsch 			idp->cyl = fdc->status[3];
2363250300ebSJoerg Wunsch 			idp->head = fdc->status[4];
2364250300ebSJoerg Wunsch 			idp->sec = fdc->status[5];
2365250300ebSJoerg Wunsch 			idp->secshift = fdc->status[6];
2366250300ebSJoerg Wunsch 		}
23673fef646eSJoerg Wunsch 		/* Operation successful, retry DMA overruns again next time. */
23683fef646eSJoerg Wunsch 		fdc->dma_overruns = 0;
23693a2f7427SDavid Greenman 		fd->skip += fdblk;
2370fb35bd37SJoerg Wunsch 		if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
23715b81b6b3SRodney W. Grimes 			/* set up next transfer */
23725b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
23736182fdbdSPeter Wemm 		} else {
23745b81b6b3SRodney W. Grimes 			/* ALL DONE */
23755b81b6b3SRodney W. Grimes 			fd->skip = 0;
2376fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2377e93e63cbSBruce Evans 			fdc->bp = NULL;
23785f830ea2SJoerg Wunsch 			device_unbusy(fd->dev);
2379a468031cSPoul-Henning Kamp 			biofinish(bp, &fd->device_stats, 0);
23805b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
23815b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
23825b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
23835b81b6b3SRodney W. Grimes 		}
2384246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2385fb35bd37SJoerg Wunsch 
23865b81b6b3SRodney W. Grimes 	case RESETCTLR:
23873a2f7427SDavid Greenman 		fdc_reset(fdc);
23885b81b6b3SRodney W. Grimes 		fdc->retry++;
23895c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
2390246ed35dSJoerg Wunsch 		return (0);	/* will return later */
2391fb35bd37SJoerg Wunsch 
23925c1a1eaeSBruce Evans 	case RESETCOMPLETE:
23935c1a1eaeSBruce Evans 		/*
23945c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
23955c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
23965c1a1eaeSBruce Evans 		 */
23970e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
23980e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
23995c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
2400fb35bd37SJoerg Wunsch 		/* FALLTHROUGH */
24015c1a1eaeSBruce Evans 	case STARTRECAL:
24026182fdbdSPeter Wemm 		if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2403dc8603e3SJoerg Wunsch 			/* arrgl */
2404dc8603e3SJoerg Wunsch 			fdc->retry = 6;
24056182fdbdSPeter Wemm 			return (retrier(fdc));
2406dc8603e3SJoerg Wunsch 		}
24075b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
24085b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2409fb35bd37SJoerg Wunsch 
24105b81b6b3SRodney W. Grimes 	case RECALWAIT:
24115b81b6b3SRodney W. Grimes 		/* allow heads to settle */
24126182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 8);
24135b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
24145b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2415fb35bd37SJoerg Wunsch 
24165b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
24173a2f7427SDavid Greenman 		do {
24183a2f7427SDavid Greenman 			/*
2419dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
24203a2f7427SDavid Greenman 			 */
2421dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2422246ed35dSJoerg Wunsch 				return (0); /* will return later */
2423dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
2424dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2425246ed35dSJoerg Wunsch 				return (0); /* hope for a real intr */
24263a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
24273a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
24285b81b6b3SRodney W. Grimes 		{
2429dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
2430dc8603e3SJoerg Wunsch 				/*
2431246ed35dSJoerg Wunsch 				 * A recalibrate from beyond cylinder 77
2432dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
2433dc8603e3SJoerg Wunsch 				 * since people used to complain much about
2434dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
2435dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
2436246ed35dSJoerg Wunsch 				 * time in a line.
2437dc8603e3SJoerg Wunsch 				 */
2438dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
2439dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
24403a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
24416182fdbdSPeter Wemm 			return (retrier(fdc));
24425b81b6b3SRodney W. Grimes 		}
24435b81b6b3SRodney W. Grimes 		fd->track = 0;
24445b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
24455b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
2446246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2447fb35bd37SJoerg Wunsch 
24485b81b6b3SRodney W. Grimes 	case MOTORWAIT:
24495b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
24505b81b6b3SRodney W. Grimes 		{
24515b81b6b3SRodney W. Grimes 			return (0); /* time's not up yet */
24525b81b6b3SRodney W. Grimes 		}
24535c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
24545c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
24555c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
2456fb35bd37SJoerg Wunsch 		} else
2457fb35bd37SJoerg Wunsch 			fdc->state = DOSEEK;
2458246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2459fb35bd37SJoerg Wunsch 
24605b81b6b3SRodney W. Grimes 	default:
2461b6e5f28eSPeter Wemm 		device_printf(fdc->fdc_dev, "unexpected FD int->");
246264860614SJoerg Wunsch 		if (fd_read_status(fdc) == 0)
2463a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
24645b81b6b3SRodney W. Grimes 			       fdc->status[0],
24655b81b6b3SRodney W. Grimes 			       fdc->status[1],
24665b81b6b3SRodney W. Grimes 			       fdc->status[2],
24675b81b6b3SRodney W. Grimes 			       fdc->status[3],
24685b81b6b3SRodney W. Grimes 			       fdc->status[4],
24695b81b6b3SRodney W. Grimes 			       fdc->status[5],
24705b81b6b3SRodney W. Grimes 			       fdc->status[6] );
24713a2f7427SDavid Greenman 		else
2472dac0f2dbSJoerg Wunsch 			printf("No status available   ");
2473dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
2474dac0f2dbSJoerg Wunsch 		{
2475dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
2476246ed35dSJoerg Wunsch 			return (0); /* will return later */
24775b81b6b3SRodney W. Grimes 		}
2478dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
2479246ed35dSJoerg Wunsch 		return (0);	/* will return later */
2480dac0f2dbSJoerg Wunsch 	}
2481246ed35dSJoerg Wunsch 	/* noone should ever get here */
24825b81b6b3SRodney W. Grimes }
24835b81b6b3SRodney W. Grimes 
2484aaf08d94SGarrett Wollman static int
24856182fdbdSPeter Wemm retrier(struct fdc_data *fdc)
24865b81b6b3SRodney W. Grimes {
24878177437dSPoul-Henning Kamp 	struct bio *bp;
24886182fdbdSPeter Wemm 	struct fd_data *fd;
24896182fdbdSPeter Wemm 	int fdu;
24905b81b6b3SRodney W. Grimes 
2491e93e63cbSBruce Evans 	bp = fdc->bp;
24925b81b6b3SRodney W. Grimes 
24936182fdbdSPeter Wemm 	/* XXX shouldn't this be cached somewhere?  */
24948177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
24956182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
24966182fdbdSPeter Wemm 	if (fd->options & FDOPT_NORETRY)
24973a2f7427SDavid Greenman 		goto fail;
24986182fdbdSPeter Wemm 
24996182fdbdSPeter Wemm 	switch (fdc->retry) {
25005b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
25015b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
25025b81b6b3SRodney W. Grimes 		break;
25035b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
25045b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
25055b81b6b3SRodney W. Grimes 		break;
25065b81b6b3SRodney W. Grimes 	case 6:
25075b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
25085b81b6b3SRodney W. Grimes 		break;
25095b81b6b3SRodney W. Grimes 	case 7:
25105b81b6b3SRodney W. Grimes 		break;
25115b81b6b3SRodney W. Grimes 	default:
25123a2f7427SDavid Greenman 	fail:
2513fb35bd37SJoerg Wunsch 		if ((fd->options & FDOPT_NOERRLOG) == 0) {
2514f90c382cSPoul-Henning Kamp 			disk_err(bp, "hard error",
2515f90c382cSPoul-Henning Kamp 			    fdc->fd->skip / DEV_BSIZE, 0);
2516fb35bd37SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID) {
2517dc5df763SJoerg Wunsch 				printf(
2518a838d83dSBruce Evans 				" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2519dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
2520dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
2521dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
2522dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
2523dc5df763SJoerg Wunsch 				       fdc->status[5]);
2524dc5df763SJoerg Wunsch 			}
2525dc5df763SJoerg Wunsch 			else
2526dc5df763SJoerg Wunsch 				printf(" (No status)\n");
25275b81b6b3SRodney W. Grimes 		}
25282995d110SJoerg Wunsch 		if ((fd->options & FDOPT_NOERROR) == 0) {
25298177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
25308177437dSPoul-Henning Kamp 			bp->bio_error = EIO;
2531fb35bd37SJoerg Wunsch 			bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
2532fb35bd37SJoerg Wunsch 		} else
2533fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2534e93e63cbSBruce Evans 		fdc->bp = NULL;
25355b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
25365f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
2537a468031cSPoul-Henning Kamp 		biofinish(bp, &fdc->fd->device_stats, 0);
253892ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
25395c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
25405b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
25415b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
254292ed385aSRodney W. Grimes 		return (1);
25435b81b6b3SRodney W. Grimes 	}
25445b81b6b3SRodney W. Grimes 	fdc->retry++;
25455b81b6b3SRodney W. Grimes 	return (1);
25465b81b6b3SRodney W. Grimes }
25475b81b6b3SRodney W. Grimes 
25481fdb6e6cSPoul-Henning Kamp static void
25491fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp)
25501fdb6e6cSPoul-Henning Kamp {
25511fdb6e6cSPoul-Henning Kamp 	wakeup(bp);
25521fdb6e6cSPoul-Henning Kamp }
25531fdb6e6cSPoul-Henning Kamp 
2554b39c878eSAndrey A. Chernov static int
2555f664aeeeSJoerg Wunsch fdmisccmd(dev_t dev, u_int cmd, void *data)
2556b39c878eSAndrey A. Chernov {
2557b39c878eSAndrey A. Chernov  	fdu_t fdu;
2558b39c878eSAndrey A. Chernov  	fd_p fd;
25591fdb6e6cSPoul-Henning Kamp 	struct bio *bp;
2560f664aeeeSJoerg Wunsch 	struct fd_formb *finfo;
2561f664aeeeSJoerg Wunsch 	struct fdc_readid *idfield;
25623a2f7427SDavid Greenman 	size_t fdblk;
256302aad38cSPoul-Henning Kamp 	int error;
2564b39c878eSAndrey A. Chernov 
2565b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
25666182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
25673a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2568f664aeeeSJoerg Wunsch 	finfo = (struct fd_formb *)data;
2569f664aeeeSJoerg Wunsch 	idfield = (struct fdc_readid *)data;
2570b39c878eSAndrey A. Chernov 
2571fb35bd37SJoerg Wunsch 	bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO);
2572b39c878eSAndrey A. Chernov 
2573b39c878eSAndrey A. Chernov 	/*
2574f664aeeeSJoerg Wunsch 	 * Set up a bio request for fdstrategy().  bio_blkno is faked
2575f664aeeeSJoerg Wunsch 	 * so that fdstrategy() will seek to the the requested
2576817988beSPoul-Henning Kamp 	 * cylinder, and use the desired head.
2577b39c878eSAndrey A. Chernov 	 */
2578f664aeeeSJoerg Wunsch 	bp->bio_cmd = cmd;
2579419f39ceSPoul-Henning Kamp 	if (cmd == FDBIO_FORMAT) {
2580f664aeeeSJoerg Wunsch 		bp->bio_blkno =
2581f664aeeeSJoerg Wunsch 		    (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
2582f664aeeeSJoerg Wunsch 		     finfo->head * fd->ft->sectrac) *
2583f664aeeeSJoerg Wunsch 		    fdblk / DEV_BSIZE;
2584f664aeeeSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fd_idfield_data) *
2585f664aeeeSJoerg Wunsch 		    finfo->fd_formb_nsecs;
2586419f39ceSPoul-Henning Kamp 	} else if (cmd == FDBIO_RDSECTID) {
2587f664aeeeSJoerg Wunsch 		bp->bio_blkno =
2588f664aeeeSJoerg Wunsch 		    (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
2589f664aeeeSJoerg Wunsch 		     idfield->head * fd->ft->sectrac) *
2590f664aeeeSJoerg Wunsch 		    fdblk / DEV_BSIZE;
2591250300ebSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fdc_readid);
2592f664aeeeSJoerg Wunsch 	} else
2593f664aeeeSJoerg Wunsch 		panic("wrong cmd in fdmisccmd()");
2594f664aeeeSJoerg Wunsch 	bp->bio_data = data;
2595250300ebSJoerg Wunsch 	bp->bio_dev = dev;
2596250300ebSJoerg Wunsch 	bp->bio_done = fdbiodone;
2597817988beSPoul-Henning Kamp 	bp->bio_flags = 0;
2598250300ebSJoerg Wunsch 
2599c3bdb2f7SPoul-Henning Kamp 	/* Now run the command. */
2600f664aeeeSJoerg Wunsch 	fdstrategy(bp);
260102aad38cSPoul-Henning Kamp 	error = biowait(bp, "fdcmd");
2602c3bdb2f7SPoul-Henning Kamp 
2603f664aeeeSJoerg Wunsch 	free(bp, M_TEMP);
260402aad38cSPoul-Henning Kamp 	return (error);
2605f664aeeeSJoerg Wunsch }
26065b81b6b3SRodney W. Grimes 
26073e425b96SJulian Elischer static int
2608b40ce416SJulian Elischer fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
2609f5f7ba03SJordan K. Hubbard {
2610f664aeeeSJoerg Wunsch  	fdu_t fdu;
2611f664aeeeSJoerg Wunsch  	fd_p fd;
26122995d110SJoerg Wunsch 	struct fdc_status *fsp;
2613250300ebSJoerg Wunsch 	struct fdc_readid *rid;
2614f664aeeeSJoerg Wunsch 	size_t fdblk;
26151a6bed68SJoerg Wunsch 	int error, type;
2616f5f7ba03SJordan K. Hubbard 
2617f664aeeeSJoerg Wunsch  	fdu = FDUNIT(minor(dev));
26181a6bed68SJoerg Wunsch 	type = FDTYPE(minor(dev));
2619f664aeeeSJoerg Wunsch  	fd = devclass_get_softc(fd_devclass, fdu);
2620fb35bd37SJoerg Wunsch 
26211a6bed68SJoerg Wunsch 	/*
26221a6bed68SJoerg Wunsch 	 * First, handle everything that could be done with
26231a6bed68SJoerg Wunsch 	 * FD_NONBLOCK still being set.
26241a6bed68SJoerg Wunsch 	 */
26251a6bed68SJoerg Wunsch 	switch (cmd) {
2626c91a63aaSPoul-Henning Kamp 
2627c91a63aaSPoul-Henning Kamp 	case DIOCGMEDIASIZE:
2628c91a63aaSPoul-Henning Kamp 		*(off_t *)addr = (128 << (fd->ft->secsize)) * fd->ft->size;
2629c91a63aaSPoul-Henning Kamp 		return (0);
2630c91a63aaSPoul-Henning Kamp 
2631c91a63aaSPoul-Henning Kamp 	case DIOCGSECTORSIZE:
2632c91a63aaSPoul-Henning Kamp 		*(u_int *)addr = 128 << (fd->ft->secsize);
2633c91a63aaSPoul-Henning Kamp 		return (0);
2634c91a63aaSPoul-Henning Kamp 
26351a6bed68SJoerg Wunsch 	case FIONBIO:
26361a6bed68SJoerg Wunsch 		if (*(int *)addr != 0)
26371a6bed68SJoerg Wunsch 			fd->flags |= FD_NONBLOCK;
26381a6bed68SJoerg Wunsch 		else {
26391a6bed68SJoerg Wunsch 			if (fd->ft == 0) {
26401a6bed68SJoerg Wunsch 				/*
26411a6bed68SJoerg Wunsch 				 * No drive type has been selected yet,
26421a6bed68SJoerg Wunsch 				 * cannot turn FNONBLOCK off.
26431a6bed68SJoerg Wunsch 				 */
26441a6bed68SJoerg Wunsch 				return (EINVAL);
26451a6bed68SJoerg Wunsch 			}
26461a6bed68SJoerg Wunsch 			fd->flags &= ~FD_NONBLOCK;
26471a6bed68SJoerg Wunsch 		}
26481a6bed68SJoerg Wunsch 		return (0);
2649fb35bd37SJoerg Wunsch 
26501a6bed68SJoerg Wunsch 	case FIOASYNC:
26511a6bed68SJoerg Wunsch 		/* keep the generic fcntl() code happy */
26521a6bed68SJoerg Wunsch 		return (0);
26531a6bed68SJoerg Wunsch 
26541a6bed68SJoerg Wunsch 	case FD_GTYPE:                  /* get drive type */
26551a6bed68SJoerg Wunsch 		if (fd->ft == 0)
26561a6bed68SJoerg Wunsch 			/* no type known yet, return the native type */
26571a6bed68SJoerg Wunsch 			*(struct fd_type *)addr = fd_native_types[fd->type];
26581a6bed68SJoerg Wunsch 		else
26591a6bed68SJoerg Wunsch 			*(struct fd_type *)addr = *fd->ft;
26601a6bed68SJoerg Wunsch 		return (0);
26611a6bed68SJoerg Wunsch 
26621a6bed68SJoerg Wunsch 	case FD_STYPE:                  /* set drive type */
26631a6bed68SJoerg Wunsch 		if (type == 0) {
26641a6bed68SJoerg Wunsch 			/*
26651a6bed68SJoerg Wunsch 			 * Allow setting drive type temporarily iff
26661a6bed68SJoerg Wunsch 			 * currently unset.  Used for fdformat so any
26671a6bed68SJoerg Wunsch 			 * user can set it, and then start formatting.
26681a6bed68SJoerg Wunsch 			 */
26691a6bed68SJoerg Wunsch 			if (fd->ft)
26701a6bed68SJoerg Wunsch 				return (EINVAL); /* already set */
26711a6bed68SJoerg Wunsch 			fd->ft = fd->fts;
26721a6bed68SJoerg Wunsch 			*fd->ft = *(struct fd_type *)addr;
26731a6bed68SJoerg Wunsch 			fd->flags |= FD_UA;
26741a6bed68SJoerg Wunsch 		} else {
26751a6bed68SJoerg Wunsch 			/*
26761a6bed68SJoerg Wunsch 			 * Set density definition permanently.  Only
26771a6bed68SJoerg Wunsch 			 * allow for superuser.
26781a6bed68SJoerg Wunsch 			 */
267944731cabSJohn Baldwin 			if (suser(td) != 0)
26801a6bed68SJoerg Wunsch 				return (EPERM);
26811a6bed68SJoerg Wunsch 			fd->fts[type] = *(struct fd_type *)addr;
26821a6bed68SJoerg Wunsch 		}
26831a6bed68SJoerg Wunsch 		return (0);
26841a6bed68SJoerg Wunsch 
26851a6bed68SJoerg Wunsch 	case FD_GOPTS:			/* get drive options */
26861a6bed68SJoerg Wunsch 		*(int *)addr = fd->options + (type == 0? FDOPT_AUTOSEL: 0);
26871a6bed68SJoerg Wunsch 		return (0);
26881a6bed68SJoerg Wunsch 
26891a6bed68SJoerg Wunsch 	case FD_SOPTS:			/* set drive options */
26901a6bed68SJoerg Wunsch 		fd->options = *(int *)addr & ~FDOPT_AUTOSEL;
26911a6bed68SJoerg Wunsch 		return (0);
26921a6bed68SJoerg Wunsch 
26931a6bed68SJoerg Wunsch #ifdef FDC_DEBUG
26941a6bed68SJoerg Wunsch 	case FD_DEBUG:
26951a6bed68SJoerg Wunsch 		if ((fd_debug != 0) != (*(int *)addr != 0)) {
26961a6bed68SJoerg Wunsch 			fd_debug = (*(int *)addr != 0);
26971a6bed68SJoerg Wunsch 			printf("fd%d: debugging turned %s\n",
26981a6bed68SJoerg Wunsch 			    fd->fdu, fd_debug ? "on" : "off");
26991a6bed68SJoerg Wunsch 		}
27001a6bed68SJoerg Wunsch 		return (0);
27011a6bed68SJoerg Wunsch #endif
27021a6bed68SJoerg Wunsch 
27031a6bed68SJoerg Wunsch 	case FD_CLRERR:
270444731cabSJohn Baldwin 		if (suser(td) != 0)
27051a6bed68SJoerg Wunsch 			return (EPERM);
27061a6bed68SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
27071a6bed68SJoerg Wunsch 		return (0);
27081a6bed68SJoerg Wunsch 
27091a6bed68SJoerg Wunsch 	case FD_GSTAT:
27101a6bed68SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
27111a6bed68SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
27121a6bed68SJoerg Wunsch 			return (EINVAL);
27131a6bed68SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
27141a6bed68SJoerg Wunsch 		return (0);
27151a6bed68SJoerg Wunsch 
27161a6bed68SJoerg Wunsch 	case FD_GDTYPE:
27171a6bed68SJoerg Wunsch 		*(enum fd_drivetype *)addr = fd->type;
27181a6bed68SJoerg Wunsch 		return (0);
27191a6bed68SJoerg Wunsch 	}
27201a6bed68SJoerg Wunsch 
27211a6bed68SJoerg Wunsch 	/*
27221a6bed68SJoerg Wunsch 	 * Now handle everything else.  Make sure we have a valid
27231a6bed68SJoerg Wunsch 	 * drive type.
27241a6bed68SJoerg Wunsch 	 */
27251a6bed68SJoerg Wunsch 	if (fd->flags & FD_NONBLOCK)
27261a6bed68SJoerg Wunsch 		return (EAGAIN);
27271a6bed68SJoerg Wunsch 	if (fd->ft == 0)
27281a6bed68SJoerg Wunsch 		return (ENXIO);
27293a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2730fb35bd37SJoerg Wunsch 	error = 0;
2731f5f7ba03SJordan K. Hubbard 
27326182fdbdSPeter Wemm 	switch (cmd) {
2733f664aeeeSJoerg Wunsch 
2734b39c878eSAndrey A. Chernov 	case FD_FORM:
2735b39c878eSAndrey A. Chernov 		if ((flag & FWRITE) == 0)
2736fb35bd37SJoerg Wunsch 			return (EBADF);	/* must be opened for writing */
2737fb35bd37SJoerg Wunsch 		if (((struct fd_formb *)addr)->format_version !=
2738b39c878eSAndrey A. Chernov 		    FD_FORMAT_VERSION)
2739fb35bd37SJoerg Wunsch 			return (EINVAL); /* wrong version of formatting prog */
2740419f39ceSPoul-Henning Kamp 		error = fdmisccmd(dev, FDBIO_FORMAT, addr);
2741b39c878eSAndrey A. Chernov 		break;
2742b39c878eSAndrey A. Chernov 
2743b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
27443e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2745f5f7ba03SJordan K. Hubbard 		break;
2746f5f7ba03SJordan K. Hubbard 
27473a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
27483a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
274944731cabSJohn Baldwin 		if (suser(td) != 0)
2750fb35bd37SJoerg Wunsch 			return (EPERM);
27513e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
27523a2f7427SDavid Greenman 		break;
27533a2f7427SDavid Greenman 
27543a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
27553e425b96SJulian Elischer 		*(int *)addr = fd->options;
27563a2f7427SDavid Greenman 		break;
27573a2f7427SDavid Greenman 
27583a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
27593e425b96SJulian Elischer 		fd->options = *(int *)addr;
27603a2f7427SDavid Greenman 		break;
27613a2f7427SDavid Greenman 
2762f664aeeeSJoerg Wunsch #ifdef FDC_DEBUG
2763f664aeeeSJoerg Wunsch 	case FD_DEBUG:
27640e17a5bcSJoerg Wunsch 		if ((fd_debug != 0) != (*(int *)addr != 0)) {
27650e17a5bcSJoerg Wunsch 			fd_debug = (*(int *)addr != 0);
27660e17a5bcSJoerg Wunsch 			printf("fd%d: debugging turned %s\n",
27670e17a5bcSJoerg Wunsch 			    fd->fdu, fd_debug ? "on" : "off");
27680e17a5bcSJoerg Wunsch 		}
2769f664aeeeSJoerg Wunsch 		break;
2770f664aeeeSJoerg Wunsch #endif
2771f664aeeeSJoerg Wunsch 
27722995d110SJoerg Wunsch 	case FD_CLRERR:
277344731cabSJohn Baldwin 		if (suser(td) != 0)
2774fb35bd37SJoerg Wunsch 			return (EPERM);
27752995d110SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
27762995d110SJoerg Wunsch 		break;
27772995d110SJoerg Wunsch 
27782995d110SJoerg Wunsch 	case FD_GSTAT:
27792995d110SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
27802995d110SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2781fb35bd37SJoerg Wunsch 			return (EINVAL);
27822995d110SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
27832995d110SJoerg Wunsch 		break;
27842995d110SJoerg Wunsch 
2785250300ebSJoerg Wunsch 	case FD_READID:
2786250300ebSJoerg Wunsch 		rid = (struct fdc_readid *)addr;
2787250300ebSJoerg Wunsch 		if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2788fb35bd37SJoerg Wunsch 			return (EINVAL);
2789419f39ceSPoul-Henning Kamp 		error = fdmisccmd(dev, FDBIO_RDSECTID, addr);
2790250300ebSJoerg Wunsch 		break;
2791250300ebSJoerg Wunsch 
2792f5f7ba03SJordan K. Hubbard 	default:
27933a2f7427SDavid Greenman 		error = ENOTTY;
2794f5f7ba03SJordan K. Hubbard 		break;
2795f5f7ba03SJordan K. Hubbard 	}
2796f5f7ba03SJordan K. Hubbard 	return (error);
2797f5f7ba03SJordan K. Hubbard }
2798