xref: /freebsd/sys/dev/fdc/fdc.c (revision 7ac40f5f59dea6e9ab4869e974bdd4026274e921)
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 = {
4807ac40f5fSPoul-Henning Kamp 	.d_open =	Fdopen,
4817ac40f5fSPoul-Henning Kamp 	.d_close =	fdclose,
4827ac40f5fSPoul-Henning Kamp 	.d_read =	physread,
4837ac40f5fSPoul-Henning Kamp 	.d_write =	physwrite,
4847ac40f5fSPoul-Henning Kamp 	.d_ioctl =	fdioctl,
4857ac40f5fSPoul-Henning Kamp 	.d_strategy =	fdstrategy,
4867ac40f5fSPoul-Henning Kamp 	.d_name =	"fd",
4877ac40f5fSPoul-Henning Kamp 	.d_maj =	CDEV_MAJOR,
4887ac40f5fSPoul-Henning Kamp 	.d_flags =	D_DISK,
4894e2f199eSPoul-Henning Kamp };
4904e2f199eSPoul-Henning Kamp 
491246ed35dSJoerg Wunsch /*
492246ed35dSJoerg Wunsch  * Auxiliary functions.  Well, some only.  Others are scattered
493246ed35dSJoerg Wunsch  * throughout the entire file.
494246ed35dSJoerg Wunsch  */
495dc5df763SJoerg Wunsch static int
4966182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
497dc5df763SJoerg Wunsch {
4986182fdbdSPeter Wemm 	fdc->fdc_errs++;
49916b04b6aSJoerg Wunsch 	if (s) {
500b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
501b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
502b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
503b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
504b6e5f28eSPeter Wemm 						    "logging any more\n");
50516b04b6aSJoerg Wunsch 	}
506dc5df763SJoerg Wunsch 
507dc5df763SJoerg Wunsch 	return FD_FAILED;
508dc5df763SJoerg Wunsch }
509dc5df763SJoerg Wunsch 
510dc5df763SJoerg Wunsch /*
511dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
512dc5df763SJoerg Wunsch  * Unit number,
513dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
514dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
515dc5df763SJoerg Wunsch  */
5166f4e0bebSPoul-Henning Kamp static int
5176182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...)
518dc5df763SJoerg Wunsch {
519dc5df763SJoerg Wunsch 	u_char cmd;
520dc5df763SJoerg Wunsch 	int n_in;
521dc5df763SJoerg Wunsch 	int n;
522dc5df763SJoerg Wunsch 	va_list ap;
523dc5df763SJoerg Wunsch 
524dc5df763SJoerg Wunsch 	va_start(ap, n_out);
525dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
526dc5df763SJoerg Wunsch 	va_end(ap);
527dc5df763SJoerg Wunsch 	va_start(ap, n_out);
528dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
529dc5df763SJoerg Wunsch 	{
5306182fdbdSPeter Wemm 		if (out_fdc(fdc, va_arg(ap, int)) < 0)
531dc5df763SJoerg Wunsch 		{
532dc5df763SJoerg Wunsch 			char msg[50];
5332127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
534dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
535dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
5366182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
537dc5df763SJoerg Wunsch 		}
538dc5df763SJoerg Wunsch 	}
539dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
540dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
541dc5df763SJoerg Wunsch 	{
542dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
5436182fdbdSPeter Wemm 		if (fd_in(fdc, ptr) < 0)
544dc5df763SJoerg Wunsch 		{
545dc5df763SJoerg Wunsch 			char msg[50];
5462127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
547dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
548dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
5496182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
550dc5df763SJoerg Wunsch 		}
551dc5df763SJoerg Wunsch 	}
552dc5df763SJoerg Wunsch 
553dc5df763SJoerg Wunsch 	return 0;
554dc5df763SJoerg Wunsch }
555dc5df763SJoerg Wunsch 
5566f4e0bebSPoul-Henning Kamp static int
557d66c374fSTor Egge enable_fifo(fdc_p fdc)
558d66c374fSTor Egge {
559d66c374fSTor Egge 	int i, j;
560d66c374fSTor Egge 
561d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
562d66c374fSTor Egge 
563d66c374fSTor Egge 		/*
564d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
565d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
566d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
567d66c374fSTor Egge 		 */
568d66c374fSTor Egge 
5696182fdbdSPeter Wemm 		if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
5706182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
571d66c374fSTor Egge 
572d66c374fSTor Egge 		/* If command is invalid, return */
573d0900d6bSJoerg Wunsch 		j = FDSTS_TIMEOUT;
574427ccf00SDoug Rabson 		while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
575d0900d6bSJoerg Wunsch 		       != NE7_RQM && j-- > 0) {
576d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
577d66c374fSTor Egge 				fdc_reset(fdc);
578d66c374fSTor Egge 				return FD_FAILED;
579d66c374fSTor Egge 			}
580d0900d6bSJoerg Wunsch 			DELAY(1);
581d0900d6bSJoerg Wunsch 		}
582d66c374fSTor Egge 		if (j<0 ||
5836182fdbdSPeter Wemm 		    fd_cmd(fdc, 3,
584d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
585d66c374fSTor Egge 			fdc_reset(fdc);
5866182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
587d66c374fSTor Egge 		}
588d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
589d66c374fSTor Egge 		return 0;
590d66c374fSTor Egge 	}
5916182fdbdSPeter Wemm 	if (fd_cmd(fdc, 4,
592d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
5936182fdbdSPeter Wemm 		return fdc_err(fdc, "Re-enable FIFO failed\n");
594d66c374fSTor Egge 	return 0;
595d66c374fSTor Egge }
596d66c374fSTor Egge 
597d66c374fSTor Egge static int
598dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
599dc5df763SJoerg Wunsch {
600dc5df763SJoerg Wunsch 	int st3;
601dc5df763SJoerg Wunsch 
6026182fdbdSPeter Wemm 	if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
603dc5df763SJoerg Wunsch 	{
6046182fdbdSPeter Wemm 		return fdc_err(fdc, "Sense Drive Status failed\n");
605dc5df763SJoerg Wunsch 	}
606dc5df763SJoerg Wunsch 	if (st3p)
607dc5df763SJoerg Wunsch 		*st3p = st3;
608dc5df763SJoerg Wunsch 
609dc5df763SJoerg Wunsch 	return 0;
610dc5df763SJoerg Wunsch }
611dc5df763SJoerg Wunsch 
6126f4e0bebSPoul-Henning Kamp static int
613dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
614dc5df763SJoerg Wunsch {
6156182fdbdSPeter Wemm 	int cyl, st0, ret;
616dc5df763SJoerg Wunsch 
6176182fdbdSPeter Wemm 	ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
6186182fdbdSPeter Wemm 	if (ret) {
6196182fdbdSPeter Wemm 		(void)fdc_err(fdc,
620dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
621dc5df763SJoerg Wunsch 		return ret;
622dc5df763SJoerg Wunsch 	}
623dc5df763SJoerg Wunsch 
624dc5df763SJoerg Wunsch 	if (st0p)
625dc5df763SJoerg Wunsch 		*st0p = st0;
626dc5df763SJoerg Wunsch 
6276182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
628dc5df763SJoerg Wunsch 		/*
629dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
630dc5df763SJoerg Wunsch 		 */
631dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
632dc5df763SJoerg Wunsch 	}
633dc5df763SJoerg Wunsch 
6346182fdbdSPeter Wemm 	if (fd_in(fdc, &cyl) < 0) {
6356182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
636dc5df763SJoerg Wunsch 	}
637dc5df763SJoerg Wunsch 
638dc5df763SJoerg Wunsch 	if (cylp)
639dc5df763SJoerg Wunsch 		*cylp = cyl;
640dc5df763SJoerg Wunsch 
641dc5df763SJoerg Wunsch 	return 0;
642dc5df763SJoerg Wunsch }
643dc5df763SJoerg Wunsch 
644dc5df763SJoerg Wunsch 
6456f4e0bebSPoul-Henning Kamp static int
64664860614SJoerg Wunsch fd_read_status(fdc_p fdc)
647dc5df763SJoerg Wunsch {
648dc5df763SJoerg Wunsch 	int i, ret;
649b5e8ce9fSBruce Evans 
650246ed35dSJoerg Wunsch 	for (i = ret = 0; i < 7; i++) {
651b5e8ce9fSBruce Evans 		/*
65264860614SJoerg Wunsch 		 * XXX types are poorly chosen.  Only bytes can be read
653a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
654b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
655b5e8ce9fSBruce Evans 		 */
656b5e8ce9fSBruce Evans 		int status;
657b5e8ce9fSBruce Evans 
6586182fdbdSPeter Wemm 		ret = fd_in(fdc, &status);
659b5e8ce9fSBruce Evans 		fdc->status[i] = status;
660b5e8ce9fSBruce Evans 		if (ret != 0)
661dc5df763SJoerg Wunsch 			break;
662dc5df763SJoerg Wunsch 	}
663dc5df763SJoerg Wunsch 
664dc5df763SJoerg Wunsch 	if (ret == 0)
665dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
666dc5df763SJoerg Wunsch 	else
667dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
668dc5df763SJoerg Wunsch 
669dc5df763SJoerg Wunsch 	return ret;
670dc5df763SJoerg Wunsch }
671dc5df763SJoerg Wunsch 
6723a2f7427SDavid Greenman static int
67337286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc)
6745b81b6b3SRodney W. Grimes {
67537286586SPeter Wemm 	device_t dev;
676e219897aSJoerg Wunsch 	int ispnp, ispcmcia, nports;
6775b81b6b3SRodney W. Grimes 
67837286586SPeter Wemm 	dev = fdc->fdc_dev;
6795f830ea2SJoerg Wunsch 	ispnp = (fdc->flags & FDC_ISPNP) != 0;
6805f830ea2SJoerg Wunsch 	ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
6816182fdbdSPeter Wemm 	fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
6826182fdbdSPeter Wemm 	fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
6836182fdbdSPeter Wemm 
684b6e5f28eSPeter Wemm 	/*
6855f830ea2SJoerg Wunsch 	 * On standard ISA, we don't just use an 8 port range
6865f830ea2SJoerg Wunsch 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
6875f830ea2SJoerg Wunsch 	 * register at 0x3f6.
6885f830ea2SJoerg Wunsch 	 *
689b6e5f28eSPeter Wemm 	 * Isn't PC hardware wonderful.
6905f830ea2SJoerg Wunsch 	 *
6915f830ea2SJoerg Wunsch 	 * The Y-E Data PCMCIA FDC doesn't have this problem, it
6925f830ea2SJoerg Wunsch 	 * uses the register with offset 6 for pseudo-DMA, and the
6935f830ea2SJoerg Wunsch 	 * one with offset 7 as control register.
694b6e5f28eSPeter Wemm 	 */
695e219897aSJoerg Wunsch 	nports = ispcmcia ? 8 : (ispnp ? 1 : 6);
6966182fdbdSPeter Wemm 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
6976182fdbdSPeter Wemm 					     &fdc->rid_ioport, 0ul, ~0ul,
698e219897aSJoerg Wunsch 					     nports, RF_ACTIVE);
6996182fdbdSPeter Wemm 	if (fdc->res_ioport == 0) {
700e219897aSJoerg Wunsch 		device_printf(dev, "cannot reserve I/O port range (%d ports)\n",
701e219897aSJoerg Wunsch 			      nports);
70237286586SPeter Wemm 		return ENXIO;
7036182fdbdSPeter Wemm 	}
704427ccf00SDoug Rabson 	fdc->portt = rman_get_bustag(fdc->res_ioport);
705427ccf00SDoug Rabson 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
706427ccf00SDoug Rabson 
7075f830ea2SJoerg Wunsch 	if (!ispcmcia) {
708427ccf00SDoug Rabson 		/*
7095f830ea2SJoerg Wunsch 		 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
7105f830ea2SJoerg Wunsch 		 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
7115f830ea2SJoerg Wunsch 		 * by checking the size and adjust the port address
7125f830ea2SJoerg Wunsch 		 * accordingly.
713b6e5f28eSPeter Wemm 		 */
714b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
715b6e5f28eSPeter Wemm 			fdc->port_off = -2;
716b6e5f28eSPeter Wemm 
717b6e5f28eSPeter Wemm 		/*
7185f830ea2SJoerg Wunsch 		 * Register the control port range as rid 1 if it
7195f830ea2SJoerg Wunsch 		 * isn't there already. Most PnP BIOSen will have
7205f830ea2SJoerg Wunsch 		 * already done this but non-PnP configurations don't.
721a2639a18SPeter Wemm 		 *
7225f830ea2SJoerg Wunsch 		 * And some (!!) report 0x3f2-0x3f5 and completely
7235f830ea2SJoerg Wunsch 		 * leave out the control register!  It seems that some
7245f830ea2SJoerg Wunsch 		 * non-antique controller chips have a different
7255f830ea2SJoerg Wunsch 		 * method of programming the transfer speed which
7265f830ea2SJoerg Wunsch 		 * doesn't require the control register, but it's
7275f830ea2SJoerg Wunsch 		 * mighty bogus as the chip still responds to the
7285f830ea2SJoerg Wunsch 		 * address for the control register.
729427ccf00SDoug Rabson 		 */
730b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
731b9da888fSPeter Wemm 			u_long ctlstart;
732b9da888fSPeter Wemm 
733b6e5f28eSPeter Wemm 			/* Find the control port, usually 0x3f7 */
7345f830ea2SJoerg Wunsch 			ctlstart = rman_get_start(fdc->res_ioport) +
7355f830ea2SJoerg Wunsch 				fdc->port_off + 7;
736b6e5f28eSPeter Wemm 
737b6e5f28eSPeter Wemm 			bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
738b9da888fSPeter Wemm 		}
739b6e5f28eSPeter Wemm 
740b6e5f28eSPeter Wemm 		/*
741b6e5f28eSPeter Wemm 		 * Now (finally!) allocate the control port.
742b6e5f28eSPeter Wemm 		 */
743427ccf00SDoug Rabson 		fdc->rid_ctl = 1;
7445f830ea2SJoerg Wunsch 		fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
7455f830ea2SJoerg Wunsch 						  &fdc->rid_ctl,
746b6e5f28eSPeter Wemm 						  0ul, ~0ul, 1, RF_ACTIVE);
747427ccf00SDoug Rabson 		if (fdc->res_ctl == 0) {
7485f830ea2SJoerg Wunsch 			device_printf(dev,
749e219897aSJoerg Wunsch 		"cannot reserve control I/O port range (control port)\n");
75037286586SPeter Wemm 			return ENXIO;
751427ccf00SDoug Rabson 		}
752427ccf00SDoug Rabson 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
753427ccf00SDoug Rabson 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
7545f830ea2SJoerg Wunsch 	}
7556182fdbdSPeter Wemm 
7566182fdbdSPeter Wemm 	fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
7576182fdbdSPeter Wemm 					  &fdc->rid_irq, 0ul, ~0ul, 1,
7586182fdbdSPeter Wemm 					  RF_ACTIVE);
7596182fdbdSPeter Wemm 	if (fdc->res_irq == 0) {
760427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve interrupt line\n");
76137286586SPeter Wemm 		return ENXIO;
7626182fdbdSPeter Wemm 	}
7635f830ea2SJoerg Wunsch 
7645f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
7656182fdbdSPeter Wemm 		fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
7666182fdbdSPeter Wemm 						  &fdc->rid_drq, 0ul, ~0ul, 1,
7676182fdbdSPeter Wemm 						  RF_ACTIVE);
7686182fdbdSPeter Wemm 		if (fdc->res_drq == 0) {
769427ccf00SDoug Rabson 			device_printf(dev, "cannot reserve DMA request line\n");
77037286586SPeter Wemm 			return ENXIO;
7716182fdbdSPeter Wemm 		}
7726182fdbdSPeter Wemm 		fdc->dmachan = fdc->res_drq->r_start;
7735f830ea2SJoerg Wunsch 	}
77437286586SPeter Wemm 
77537286586SPeter Wemm 	return 0;
77637286586SPeter Wemm }
77737286586SPeter Wemm 
77837286586SPeter Wemm static void
77937286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
78037286586SPeter Wemm {
78137286586SPeter Wemm 	device_t dev;
78237286586SPeter Wemm 
78337286586SPeter Wemm 	dev = fdc->fdc_dev;
78437286586SPeter Wemm 	if (fdc->res_irq != 0) {
78537286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
78637286586SPeter Wemm 					fdc->res_irq);
78737286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
78837286586SPeter Wemm 				     fdc->res_irq);
78937286586SPeter Wemm 	}
79037286586SPeter Wemm 	if (fdc->res_ctl != 0) {
79137286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
79237286586SPeter Wemm 					fdc->res_ctl);
79337286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
79437286586SPeter Wemm 				     fdc->res_ctl);
79537286586SPeter Wemm 	}
79637286586SPeter Wemm 	if (fdc->res_ioport != 0) {
79737286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
79837286586SPeter Wemm 					fdc->res_ioport);
79937286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
80037286586SPeter Wemm 				     fdc->res_ioport);
80137286586SPeter Wemm 	}
80237286586SPeter Wemm 	if (fdc->res_drq != 0) {
80337286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
80437286586SPeter Wemm 					fdc->res_drq);
80537286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
80637286586SPeter Wemm 				     fdc->res_drq);
80737286586SPeter Wemm 	}
80837286586SPeter Wemm }
80937286586SPeter Wemm 
810246ed35dSJoerg Wunsch /*
811246ed35dSJoerg Wunsch  * Configuration/initialization stuff, per controller.
812246ed35dSJoerg Wunsch  */
81337286586SPeter Wemm 
81437286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = {
81537286586SPeter Wemm 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
81637286586SPeter Wemm 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
81737286586SPeter Wemm 	{0}
81837286586SPeter Wemm };
81937286586SPeter Wemm 
82037286586SPeter Wemm static int
8213aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
82237286586SPeter Wemm {
82337286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
82437286586SPeter Wemm 
82537286586SPeter Wemm 	switch (which) {
82637286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
82737286586SPeter Wemm 		*result = ivars->fdunit;
82837286586SPeter Wemm 		break;
82937286586SPeter Wemm 	default:
83037286586SPeter Wemm 		return ENOENT;
83137286586SPeter Wemm 	}
83237286586SPeter Wemm 	return 0;
83337286586SPeter Wemm }
83437286586SPeter Wemm 
83537286586SPeter Wemm static int
83637286586SPeter Wemm fdc_probe(device_t dev)
83737286586SPeter Wemm {
83837286586SPeter Wemm 	int	error, ic_type;
83937286586SPeter Wemm 	struct	fdc_data *fdc;
84037286586SPeter Wemm 
84137286586SPeter Wemm 	fdc = device_get_softc(dev);
84237286586SPeter Wemm 	bzero(fdc, sizeof *fdc);
84337286586SPeter Wemm 	fdc->fdc_dev = dev;
8445f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_isa;
84537286586SPeter Wemm 
84637286586SPeter Wemm 	/* Check pnp ids */
84737286586SPeter Wemm 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
84837286586SPeter Wemm 	if (error == ENXIO)
84937286586SPeter Wemm 		return ENXIO;
8505f830ea2SJoerg Wunsch 	if (error == 0)
8515f830ea2SJoerg Wunsch 		fdc->flags |= FDC_ISPNP;
85237286586SPeter Wemm 
85337286586SPeter Wemm 	/* Attempt to allocate our resources for the duration of the probe */
85437286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
85537286586SPeter Wemm 	if (error)
85637286586SPeter Wemm 		goto out;
8575b81b6b3SRodney W. Grimes 
85816111cedSAndrew Moore 	/* First - lets reset the floppy controller */
859427ccf00SDoug Rabson 	fdout_wr(fdc, 0);
86016111cedSAndrew Moore 	DELAY(100);
861427ccf00SDoug Rabson 	fdout_wr(fdc, FDO_FRST);
86216111cedSAndrew Moore 
8635b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
8646182fdbdSPeter Wemm 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
8656182fdbdSPeter Wemm 		   NE7_SPEC_2(2, 0), 0)) {
8666182fdbdSPeter Wemm 		error = ENXIO;
8676182fdbdSPeter Wemm 		goto out;
8685b81b6b3SRodney W. Grimes 	}
8696182fdbdSPeter Wemm 
8706182fdbdSPeter Wemm 	if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
8716182fdbdSPeter Wemm 		ic_type = (u_char)ic_type;
8726182fdbdSPeter Wemm 		switch (ic_type) {
8736182fdbdSPeter Wemm 		case 0x80:
8746182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 765 or clone");
8756182fdbdSPeter Wemm 			fdc->fdct = FDC_NE765;
8766182fdbdSPeter Wemm 			break;
8771a6bed68SJoerg Wunsch 		case 0x81:	/* not mentioned in any hardware doc */
8786182fdbdSPeter Wemm 		case 0x90:
8791a6bed68SJoerg Wunsch 			device_set_desc(dev,
8802ac8c61aSMaxime Henrion 		"Enhanced floppy controller (i82077, NE72065 or clone)");
8811a6bed68SJoerg Wunsch 			fdc->fdct = FDC_ENHANCED;
8826182fdbdSPeter Wemm 			break;
8836182fdbdSPeter Wemm 		default:
8842ac8c61aSMaxime Henrion 			device_set_desc(dev, "Generic floppy controller");
8856182fdbdSPeter Wemm 			fdc->fdct = FDC_UNKNOWN;
8866182fdbdSPeter Wemm 			break;
8876182fdbdSPeter Wemm 		}
8886182fdbdSPeter Wemm 	}
8896182fdbdSPeter Wemm 
8906182fdbdSPeter Wemm out:
89137286586SPeter Wemm 	fdc_release_resources(fdc);
8926182fdbdSPeter Wemm 	return (error);
8935b81b6b3SRodney W. Grimes }
8945b81b6b3SRodney W. Grimes 
8955f830ea2SJoerg Wunsch #if NCARD > 0
8965f830ea2SJoerg Wunsch 
8975f830ea2SJoerg Wunsch static int
8985f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev)
8995f830ea2SJoerg Wunsch {
9005f830ea2SJoerg Wunsch 	int	error;
9015f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
9025f830ea2SJoerg Wunsch 
9035f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
9045f830ea2SJoerg Wunsch 	bzero(fdc, sizeof *fdc);
9055f830ea2SJoerg Wunsch 	fdc->fdc_dev = dev;
9065f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_pcmcia;
9075f830ea2SJoerg Wunsch 
9085f830ea2SJoerg Wunsch 	fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
9095f830ea2SJoerg Wunsch 
9105f830ea2SJoerg Wunsch 	/* Attempt to allocate our resources for the duration of the probe */
9115f830ea2SJoerg Wunsch 	error = fdc_alloc_resources(fdc);
9125f830ea2SJoerg Wunsch 	if (error)
9135f830ea2SJoerg Wunsch 		goto out;
9145f830ea2SJoerg Wunsch 
9155f830ea2SJoerg Wunsch 	/* First - lets reset the floppy controller */
9165f830ea2SJoerg Wunsch 	fdout_wr(fdc, 0);
9175f830ea2SJoerg Wunsch 	DELAY(100);
9185f830ea2SJoerg Wunsch 	fdout_wr(fdc, FDO_FRST);
9195f830ea2SJoerg Wunsch 
9205f830ea2SJoerg Wunsch 	/* see if it can handle a command */
9215f830ea2SJoerg Wunsch 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
9225f830ea2SJoerg Wunsch 		   NE7_SPEC_2(2, 0), 0)) {
9235f830ea2SJoerg Wunsch 		error = ENXIO;
9245f830ea2SJoerg Wunsch 		goto out;
9255f830ea2SJoerg Wunsch 	}
9265f830ea2SJoerg Wunsch 
9275f830ea2SJoerg Wunsch 	device_set_desc(dev, "Y-E Data PCMCIA floppy");
9285f830ea2SJoerg Wunsch 	fdc->fdct = FDC_NE765;
9295f830ea2SJoerg Wunsch 
9305f830ea2SJoerg Wunsch out:
9315f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
9325f830ea2SJoerg Wunsch 	return (error);
9335f830ea2SJoerg Wunsch }
9345f830ea2SJoerg Wunsch 
935e219897aSJoerg Wunsch #endif /* NCARD > 0 */
936e219897aSJoerg Wunsch 
9375f830ea2SJoerg Wunsch static int
938e219897aSJoerg Wunsch fdc_detach(device_t dev)
9395f830ea2SJoerg Wunsch {
9405f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
9415f830ea2SJoerg Wunsch 	int	error;
9425f830ea2SJoerg Wunsch 
9435f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
9445f830ea2SJoerg Wunsch 
9455f830ea2SJoerg Wunsch 	/* have our children detached first */
9465f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
9475f830ea2SJoerg Wunsch 		return (error);
9485f830ea2SJoerg Wunsch 
949e219897aSJoerg Wunsch 	/* reset controller, turn motor off */
950e219897aSJoerg Wunsch 	fdout_wr(fdc, 0);
951e219897aSJoerg Wunsch 
952e219897aSJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0)
953e219897aSJoerg Wunsch 		isa_dma_release(fdc->dmachan);
954e219897aSJoerg Wunsch 
9555f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_ATTACHED) == 0) {
9565f830ea2SJoerg Wunsch 		device_printf(dev, "already unloaded\n");
9575f830ea2SJoerg Wunsch 		return (0);
9585f830ea2SJoerg Wunsch 	}
9595f830ea2SJoerg Wunsch 	fdc->flags &= ~FDC_ATTACHED;
9605f830ea2SJoerg Wunsch 
9615f830ea2SJoerg Wunsch 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
9625f830ea2SJoerg Wunsch 			  fdc->fdc_intr);
9635f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
9645f830ea2SJoerg Wunsch 	device_printf(dev, "unload\n");
9655f830ea2SJoerg Wunsch 	return (0);
9665f830ea2SJoerg Wunsch }
9675f830ea2SJoerg Wunsch 
9685b81b6b3SRodney W. Grimes /*
96937286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
9705b81b6b3SRodney W. Grimes  */
9716182fdbdSPeter Wemm static void
97237286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
9735b81b6b3SRodney W. Grimes {
9741a6bed68SJoerg Wunsch 	int	disabled, flags;
97537286586SPeter Wemm 	struct fdc_ivars *ivar;
9766182fdbdSPeter Wemm 	device_t child;
97792200632SGarrett Wollman 
9787cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
97937286586SPeter Wemm 	if (ivar == NULL)
9806182fdbdSPeter Wemm 		return;
98137286586SPeter Wemm 	if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
98237286586SPeter Wemm 		ivar->fdunit = 0;
983fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
98442117b6cSPoul-Henning Kamp 	if (child == NULL) {
98542117b6cSPoul-Henning Kamp 		free(ivar, M_DEVBUF);
9866182fdbdSPeter Wemm 		return;
98742117b6cSPoul-Henning Kamp 	}
98837286586SPeter Wemm 	device_set_ivars(child, ivar);
9891a6bed68SJoerg Wunsch 	if (resource_int_value(name, unit, "flags", &flags) == 0)
9901a6bed68SJoerg Wunsch 		 device_set_flags(child, flags);
991a97c75b7SDoug Rabson 	if (resource_int_value(name, unit, "disabled", &disabled) == 0
992a97c75b7SDoug Rabson 	    && disabled != 0)
9936182fdbdSPeter Wemm 		device_disable(child);
9946182fdbdSPeter Wemm }
9956182fdbdSPeter Wemm 
9966182fdbdSPeter Wemm static int
9976182fdbdSPeter Wemm fdc_attach(device_t dev)
9986182fdbdSPeter Wemm {
99937286586SPeter Wemm 	struct	fdc_data *fdc;
10002398f0cdSPeter Wemm 	const char *name, *dname;
10015d54fe91SJoerg Wunsch 	int	i, error, dunit;
1002427ccf00SDoug Rabson 
100337286586SPeter Wemm 	fdc = device_get_softc(dev);
100437286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
100537286586SPeter Wemm 	if (error) {
1006f8ce7dd5SJoerg Wunsch 		device_printf(dev, "cannot re-acquire resources\n");
100737286586SPeter Wemm 		return error;
100837286586SPeter Wemm 	}
100937286586SPeter Wemm 	error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
10103c36743eSMark Murray 			       INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
10113c36743eSMark Murray 			       &fdc->fdc_intr);
101237286586SPeter Wemm 	if (error) {
101337286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
101437286586SPeter Wemm 		return error;
101537286586SPeter Wemm 	}
101637286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
1017fb35bd37SJoerg Wunsch 	fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET;
10186182fdbdSPeter Wemm 
10195f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
1020f8ce7dd5SJoerg Wunsch 		/*
1021f8ce7dd5SJoerg Wunsch 		 * Acquire the DMA channel forever, the driver will do
1022f8ce7dd5SJoerg Wunsch 		 * the rest
1023f8ce7dd5SJoerg Wunsch 		 * XXX should integrate with rman
1024f8ce7dd5SJoerg Wunsch 		 */
1025100f78bbSSujal Patel 		isa_dma_acquire(fdc->dmachan);
1026f8ce7dd5SJoerg Wunsch 		isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
10275f830ea2SJoerg Wunsch 	}
10285b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
10296182fdbdSPeter Wemm 
10303a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
103164860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
10328177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
10335b81b6b3SRodney W. Grimes 
10346182fdbdSPeter Wemm 	/*
103537286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
103637286586SPeter Wemm 	 * devices from the BIOS unless overridden.
10376182fdbdSPeter Wemm 	 */
1038ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
10392398f0cdSPeter Wemm 	i = 0;
10402398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
10412398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
104237286586SPeter Wemm 
104360444853SJoerg Wunsch 	if ((error = bus_generic_attach(dev)) != 0)
104460444853SJoerg Wunsch 		return (error);
104560444853SJoerg Wunsch 
104660444853SJoerg Wunsch 	return (0);
10476182fdbdSPeter Wemm }
10486182fdbdSPeter Wemm 
104915317dd8SMatthew N. Dodd static int
10506182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
10516182fdbdSPeter Wemm {
10521a6bed68SJoerg Wunsch 	int retval = 0, flags;
105315317dd8SMatthew N. Dodd 
105415317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
10551a6bed68SJoerg Wunsch 	retval += printf(" on %s drive %d", device_get_nameunit(me),
105637286586SPeter Wemm 	       fdc_get_fdunit(child));
10571a6bed68SJoerg Wunsch 	if ((flags = device_get_flags(me)) != 0)
10581a6bed68SJoerg Wunsch 		retval += printf(" flags %#x", flags);
10591a6bed68SJoerg Wunsch 	retval += printf("\n");
106015317dd8SMatthew N. Dodd 
106115317dd8SMatthew N. Dodd 	return (retval);
10626182fdbdSPeter Wemm }
10636182fdbdSPeter Wemm 
106416e68fc6SPeter Wemm static device_method_t fdc_methods[] = {
106516e68fc6SPeter Wemm 	/* Device interface */
106616e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fdc_probe),
106716e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fdc_attach),
1068e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
106916e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
107016e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend),
107116e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume),
107216e68fc6SPeter Wemm 
107316e68fc6SPeter Wemm 	/* Bus interface */
107416e68fc6SPeter Wemm 	DEVMETHOD(bus_print_child,	fdc_print_child),
107537286586SPeter Wemm 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
107616e68fc6SPeter Wemm 	/* Our children never use any other bus interface methods. */
107716e68fc6SPeter Wemm 
107816e68fc6SPeter Wemm 	{ 0, 0 }
107916e68fc6SPeter Wemm };
108016e68fc6SPeter Wemm 
108116e68fc6SPeter Wemm static driver_t fdc_driver = {
108216e68fc6SPeter Wemm 	"fdc",
108316e68fc6SPeter Wemm 	fdc_methods,
108416e68fc6SPeter Wemm 	sizeof(struct fdc_data)
108516e68fc6SPeter Wemm };
108616e68fc6SPeter Wemm 
108716e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
10885f063c7bSMike Smith DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0);
108916e68fc6SPeter Wemm 
10905f830ea2SJoerg Wunsch #if NCARD > 0
10915f830ea2SJoerg Wunsch 
10925f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = {
10935f830ea2SJoerg Wunsch 	/* Device interface */
10945f830ea2SJoerg Wunsch 	DEVMETHOD(device_probe,		fdc_pccard_probe),
10955f830ea2SJoerg Wunsch 	DEVMETHOD(device_attach,	fdc_attach),
1096e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
10975f830ea2SJoerg Wunsch 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
10985f830ea2SJoerg Wunsch 	DEVMETHOD(device_suspend,	bus_generic_suspend),
10995f830ea2SJoerg Wunsch 	DEVMETHOD(device_resume,	bus_generic_resume),
11005f830ea2SJoerg Wunsch 
11015f830ea2SJoerg Wunsch 	/* Bus interface */
11025f830ea2SJoerg Wunsch 	DEVMETHOD(bus_print_child,	fdc_print_child),
11035f830ea2SJoerg Wunsch 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
11045f830ea2SJoerg Wunsch 	/* Our children never use any other bus interface methods. */
11055f830ea2SJoerg Wunsch 
11065f830ea2SJoerg Wunsch 	{ 0, 0 }
11075f830ea2SJoerg Wunsch };
11085f830ea2SJoerg Wunsch 
11095f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = {
11105f830ea2SJoerg Wunsch 	"fdc",
11115f830ea2SJoerg Wunsch 	fdc_pccard_methods,
11125f830ea2SJoerg Wunsch 	sizeof(struct fdc_data)
11135f830ea2SJoerg Wunsch };
11145f830ea2SJoerg Wunsch 
11155f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
11165f830ea2SJoerg Wunsch 
11175f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
11185f830ea2SJoerg Wunsch 
111960444853SJoerg Wunsch /*
11201a6bed68SJoerg Wunsch  * Create a clone device upon request by devfs.
112160444853SJoerg Wunsch  */
11223f54a085SPoul-Henning Kamp static void
112380909a7dSJoerg Wunsch fd_clone(void *arg, char *name, int namelen, dev_t *dev)
11243f54a085SPoul-Henning Kamp {
112560444853SJoerg Wunsch 	struct	fd_data *fd;
11261a6bed68SJoerg Wunsch 	int i, u;
11273f54a085SPoul-Henning Kamp 	char *n;
11281a6bed68SJoerg Wunsch 	size_t l;
11293f54a085SPoul-Henning Kamp 
113060444853SJoerg Wunsch 	fd = (struct fd_data *)arg;
11313f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
11323f54a085SPoul-Henning Kamp 		return;
1133db901281SPoul-Henning Kamp 	if (dev_stdclone(name, &n, "fd", &u) != 2)
11343f54a085SPoul-Henning Kamp 		return;
1135294d8844SJoerg Wunsch 	if (u != fd->fdu)
1136294d8844SJoerg Wunsch 		/* unit # mismatch */
1137294d8844SJoerg Wunsch 		return;
11381a6bed68SJoerg Wunsch 	l = strlen(n);
11391a6bed68SJoerg Wunsch 	if (l == 1 && *n >= 'a' && *n <= 'h') {
11401a6bed68SJoerg Wunsch 		/*
11411a6bed68SJoerg Wunsch 		 * Trailing letters a through h denote
11421a6bed68SJoerg Wunsch 		 * pseudo-partitions.  We don't support true
11431a6bed68SJoerg Wunsch 		 * (UFS-style) partitions, so we just implement them
11441a6bed68SJoerg Wunsch 		 * as symlinks if someone asks us nicely.
11451a6bed68SJoerg Wunsch 		 */
114660444853SJoerg Wunsch 		*dev = make_dev_alias(fd->masterdev, name);
11471a6bed68SJoerg Wunsch 		return;
11481a6bed68SJoerg Wunsch 	}
11491a6bed68SJoerg Wunsch 	if (l >= 2 && l <= 5 && *n == '.') {
11501a6bed68SJoerg Wunsch 		/*
11511a6bed68SJoerg Wunsch 		 * Trailing numbers, preceded by a dot, denote
11521a6bed68SJoerg Wunsch 		 * subdevices for different densities.  Historically,
11531a6bed68SJoerg Wunsch 		 * they have been named by density (like fd0.1440),
11541a6bed68SJoerg Wunsch 		 * but we allow arbitrary numbers between 1 and 4
11551a6bed68SJoerg Wunsch 		 * digits, so fd0.1 through fd0.15 are possible as
11561a6bed68SJoerg Wunsch 		 * well.
11571a6bed68SJoerg Wunsch 		 */
11581a6bed68SJoerg Wunsch 		for (i = 1; i < l; i++)
11591a6bed68SJoerg Wunsch 			if (n[i] < '0' || n[i] > '9')
11601a6bed68SJoerg Wunsch 				return;
11611a6bed68SJoerg Wunsch 		for (i = 0; i < NUMDENS - 1; i++)
11621a6bed68SJoerg Wunsch 			if (fd->clonedevs[i] == NODEV) {
11631a6bed68SJoerg Wunsch 				*dev = make_dev(&fd_cdevsw,
11641a6bed68SJoerg Wunsch 						FDNUMTOUNIT(u) + i + 1,
11651a6bed68SJoerg Wunsch 						UID_ROOT, GID_OPERATOR, 0640,
11661a6bed68SJoerg Wunsch 						name);
11671a6bed68SJoerg Wunsch 				fd->clonedevs[i] = *dev;
11681a6bed68SJoerg Wunsch 				return;
11691a6bed68SJoerg Wunsch 			}
11703f54a085SPoul-Henning Kamp 	}
11713f54a085SPoul-Henning Kamp }
11723f54a085SPoul-Henning Kamp 
117316e68fc6SPeter Wemm /*
1174246ed35dSJoerg Wunsch  * Configuration/initialization, per drive.
117516e68fc6SPeter Wemm  */
11766182fdbdSPeter Wemm static int
11776182fdbdSPeter Wemm fd_probe(device_t dev)
11786182fdbdSPeter Wemm {
11796182fdbdSPeter Wemm 	int	i;
11801a6bed68SJoerg Wunsch 	u_int	st0, st3;
11816182fdbdSPeter Wemm 	struct	fd_data *fd;
11826182fdbdSPeter Wemm 	struct	fdc_data *fdc;
11836182fdbdSPeter Wemm 	fdsu_t	fdsu;
11841a6bed68SJoerg Wunsch 	int	flags;
11856182fdbdSPeter Wemm 
11866182fdbdSPeter Wemm 	fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
11876182fdbdSPeter Wemm 	fd = device_get_softc(dev);
11886182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
11891a6bed68SJoerg Wunsch 	flags = device_get_flags(dev);
11906182fdbdSPeter Wemm 
11916182fdbdSPeter Wemm 	bzero(fd, sizeof *fd);
11926182fdbdSPeter Wemm 	fd->dev = dev;
11936182fdbdSPeter Wemm 	fd->fdc = fdc;
11946182fdbdSPeter Wemm 	fd->fdsu = fdsu;
11956182fdbdSPeter Wemm 	fd->fdu = device_get_unit(dev);
11961a6bed68SJoerg Wunsch 	fd->flags = FD_UA;	/* make sure fdautoselect() will be called */
11976182fdbdSPeter Wemm 
11981a6bed68SJoerg Wunsch 	fd->type = FD_DTYPE(flags);
1199038d1bbdSJoerg Wunsch /*
1200038d1bbdSJoerg Wunsch  * XXX I think using __i386__ is wrong here since we actually want to probe
1201038d1bbdSJoerg Wunsch  * for the machine type, not the CPU type (so non-PC arch's like the PC98 will
1202038d1bbdSJoerg Wunsch  * fail the probe).  However, for whatever reason, testing for _MACHINE_ARCH
1203038d1bbdSJoerg Wunsch  * == i386 breaks the test on FreeBSD/Alpha.
1204038d1bbdSJoerg Wunsch  */
1205038d1bbdSJoerg Wunsch #ifdef __i386__
12061a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) {
12071a6bed68SJoerg Wunsch 		/* Look up what the BIOS thinks we have. */
12081a6bed68SJoerg Wunsch 		if (fd->fdu == 0) {
12095f830ea2SJoerg Wunsch 			if ((fdc->flags & FDC_ISPCMCIA))
12101a6bed68SJoerg Wunsch 				/*
12111a6bed68SJoerg Wunsch 				 * Somewhat special.  No need to force the
12121a6bed68SJoerg Wunsch 				 * user to set device flags, since the Y-E
12131a6bed68SJoerg Wunsch 				 * Data PCMCIA floppy is always a 1.44 MB
12141a6bed68SJoerg Wunsch 				 * device.
12151a6bed68SJoerg Wunsch 				 */
12161a6bed68SJoerg Wunsch 				fd->type = FDT_144M;
12170722d6abSJoerg Wunsch 			else
12181a6bed68SJoerg Wunsch 				fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
12191a6bed68SJoerg Wunsch 		} else {
12201a6bed68SJoerg Wunsch 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
12216b7bd95bSJoerg Wunsch 		}
12221a6bed68SJoerg Wunsch 		if (fd->type == FDT_288M_1)
12231a6bed68SJoerg Wunsch 			fd->type = FDT_288M;
12241a6bed68SJoerg Wunsch 	}
1225038d1bbdSJoerg Wunsch #endif /* __i386__ */
12266182fdbdSPeter Wemm 	/* is there a unit? */
12271a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE)
12286182fdbdSPeter Wemm 		return (ENXIO);
12296182fdbdSPeter Wemm 
12306182fdbdSPeter Wemm 	/* select it */
12316182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNON);
1232fb35bd37SJoerg Wunsch 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
12336182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
12346182fdbdSPeter Wemm 
12358de0675cSPeter Wemm 	/* XXX This doesn't work before the first set_motor() */
12361a6bed68SJoerg Wunsch 	if ((fdc->flags & FDC_HAS_FIFO) == 0  &&
12371a6bed68SJoerg Wunsch 	    fdc->fdct == FDC_ENHANCED &&
12381a6bed68SJoerg Wunsch 	    (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 &&
12391a6bed68SJoerg Wunsch 	    enable_fifo(fdc) == 0) {
1240b6e5f28eSPeter Wemm 		device_printf(device_get_parent(dev),
1241b6e5f28eSPeter Wemm 		    "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1242d66c374fSTor Egge 	}
12436182fdbdSPeter Wemm 
12441a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0) {
1245dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
12461a6bed68SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
12471a6bed68SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
1248dc5df763SJoerg Wunsch 			/* Seek some steps... */
12496182fdbdSPeter Wemm 			if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1250dc5df763SJoerg Wunsch 				/* ...wait a moment... */
1251dc5df763SJoerg Wunsch 				DELAY(300000);
1252dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
12536182fdbdSPeter Wemm 				fd_sense_int(fdc, 0, 0);
1254dc5df763SJoerg Wunsch 			}
1255dc5df763SJoerg Wunsch 		}
1256dc5df763SJoerg Wunsch 
12576b7bd95bSJoerg Wunsch 		for (i = 0; i < 2; i++) {
12586b7bd95bSJoerg Wunsch 			/*
12596b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
12601a6bed68SJoerg Wunsch 			 * heads have been beyond cylinder 76, since
12611a6bed68SJoerg Wunsch 			 * most FDCs still barf when attempting to
12621a6bed68SJoerg Wunsch 			 * recalibrate more than 77 steps
12636b7bd95bSJoerg Wunsch 			 */
1264dc5df763SJoerg Wunsch 			/* go back to 0: */
12656182fdbdSPeter Wemm 			if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
12666b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
12676b7bd95bSJoerg Wunsch 				DELAY(i == 0 ? 1000000 : 300000);
12685b81b6b3SRodney W. Grimes 
12696b7bd95bSJoerg Wunsch 				/* anything responding? */
1270dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
1271dc5df763SJoerg Wunsch 				    (st0 & NE7_ST0_EC) == 0)
12726b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
12736b7bd95bSJoerg Wunsch 			}
1274dc5df763SJoerg Wunsch 		}
12751a6bed68SJoerg Wunsch 	}
12766b7bd95bSJoerg Wunsch 
12776182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNOFF);
12783a2f7427SDavid Greenman 
12791a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0 &&
12801a6bed68SJoerg Wunsch 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
12816182fdbdSPeter Wemm 		return (ENXIO);
12825b81b6b3SRodney W. Grimes 
12831a6bed68SJoerg Wunsch 	switch (fd->type) {
12841a6bed68SJoerg Wunsch 	case FDT_12M:
12851a6bed68SJoerg Wunsch 		device_set_desc(dev, "1200-KB 5.25\" drive");
12861a6bed68SJoerg Wunsch 		fd->type = FDT_12M;
12871a6bed68SJoerg Wunsch 		break;
12881a6bed68SJoerg Wunsch 	case FDT_144M:
12891a6bed68SJoerg Wunsch 		device_set_desc(dev, "1440-KB 3.5\" drive");
12901a6bed68SJoerg Wunsch 		fd->type = FDT_144M;
12911a6bed68SJoerg Wunsch 		break;
12921a6bed68SJoerg Wunsch 	case FDT_288M:
12931a6bed68SJoerg Wunsch 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
12941a6bed68SJoerg Wunsch 		fd->type = FDT_288M;
12951a6bed68SJoerg Wunsch 		break;
12961a6bed68SJoerg Wunsch 	case FDT_360K:
12971a6bed68SJoerg Wunsch 		device_set_desc(dev, "360-KB 5.25\" drive");
12981a6bed68SJoerg Wunsch 		fd->type = FDT_360K;
12991a6bed68SJoerg Wunsch 		break;
13001a6bed68SJoerg Wunsch 	case FDT_720K:
13011a6bed68SJoerg Wunsch 		device_set_desc(dev, "720-KB 3.5\" drive");
13021a6bed68SJoerg Wunsch 		fd->type = FDT_720K;
13031a6bed68SJoerg Wunsch 		break;
13041a6bed68SJoerg Wunsch 	default:
13051a6bed68SJoerg Wunsch 		return (ENXIO);
13061a6bed68SJoerg Wunsch 	}
1307dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1308b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1309b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
13103a2f7427SDavid Greenman 	fd->options = 0;
131102a19910SJustin T. Gibbs 	callout_handle_init(&fd->toffhandle);
131202a19910SJustin T. Gibbs 	callout_handle_init(&fd->tohandle);
13135b81b6b3SRodney W. Grimes 
13141a6bed68SJoerg Wunsch 	/* initialize densities for subdevices */
13151a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS; i++)
13161a6bed68SJoerg Wunsch 		memcpy(fd->fts + i, fd_native_types + fd->type,
13171a6bed68SJoerg Wunsch 		       sizeof(struct fd_type));
13186182fdbdSPeter Wemm 	return (0);
13196182fdbdSPeter Wemm }
13206182fdbdSPeter Wemm 
13216182fdbdSPeter Wemm static int
13226182fdbdSPeter Wemm fd_attach(device_t dev)
13236182fdbdSPeter Wemm {
13246182fdbdSPeter Wemm 	struct	fd_data *fd;
132560444853SJoerg Wunsch 	int i;
13266182fdbdSPeter Wemm 
132764860614SJoerg Wunsch 	fd = device_get_softc(dev);
132860444853SJoerg Wunsch 	fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, fd, 1000);
13295f431174SJoerg Wunsch 	fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6,
13303f54a085SPoul-Henning Kamp 				 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
13311a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS - 1; i++)
133260444853SJoerg Wunsch 		fd->clonedevs[i] = NODEV;
13336182fdbdSPeter Wemm 	devstat_add_entry(&fd->device_stats, device_get_name(dev),
1334f8ce7dd5SJoerg Wunsch 			  device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
13352a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
13362a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_FD);
13376182fdbdSPeter Wemm 	return (0);
13385b81b6b3SRodney W. Grimes }
13395b81b6b3SRodney W. Grimes 
13405f830ea2SJoerg Wunsch static int
13415f830ea2SJoerg Wunsch fd_detach(device_t dev)
13425f830ea2SJoerg Wunsch {
13435f830ea2SJoerg Wunsch 	struct	fd_data *fd;
134460444853SJoerg Wunsch 	int i;
13455f830ea2SJoerg Wunsch 
13465f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
134760444853SJoerg Wunsch 	untimeout(fd_turnoff, fd, fd->toffhandle);
1348e219897aSJoerg Wunsch 	devstat_remove_entry(&fd->device_stats);
1349e219897aSJoerg Wunsch 	destroy_dev(fd->masterdev);
13501a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS - 1; i++)
135160444853SJoerg Wunsch 		if (fd->clonedevs[i] != NODEV)
135260444853SJoerg Wunsch 			destroy_dev(fd->clonedevs[i]);
1353e219897aSJoerg Wunsch 	EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag);
13545f830ea2SJoerg Wunsch 
13555f830ea2SJoerg Wunsch 	return (0);
13565f830ea2SJoerg Wunsch }
13575f830ea2SJoerg Wunsch 
135816e68fc6SPeter Wemm static device_method_t fd_methods[] = {
135916e68fc6SPeter Wemm 	/* Device interface */
136016e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
136116e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
13625f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
136316e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
136416e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
136516e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
136616e68fc6SPeter Wemm 
136716e68fc6SPeter Wemm 	{ 0, 0 }
136816e68fc6SPeter Wemm };
136916e68fc6SPeter Wemm 
137016e68fc6SPeter Wemm static driver_t fd_driver = {
137116e68fc6SPeter Wemm 	"fd",
137216e68fc6SPeter Wemm 	fd_methods,
137316e68fc6SPeter Wemm 	sizeof(struct fd_data)
137416e68fc6SPeter Wemm };
137516e68fc6SPeter Wemm 
1376475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
137716e68fc6SPeter Wemm 
1378246ed35dSJoerg Wunsch /*
1379246ed35dSJoerg Wunsch  * More auxiliary functions.
1380246ed35dSJoerg Wunsch  */
1381246ed35dSJoerg Wunsch /*
1382246ed35dSJoerg Wunsch  * Motor control stuff.
1383246ed35dSJoerg Wunsch  * Remember to not deselect the drive we're working on.
1384246ed35dSJoerg Wunsch  */
13853a2f7427SDavid Greenman static void
13866182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon)
13875b81b6b3SRodney W. Grimes {
1388fb35bd37SJoerg Wunsch 	int fdout;
13893a2f7427SDavid Greenman 
1390fb35bd37SJoerg Wunsch 	fdout = fdc->fdout;
13913a2f7427SDavid Greenman 	if (turnon) {
13923a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
1393fb35bd37SJoerg Wunsch 		fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
13943a2f7427SDavid Greenman 	} else
13953a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
13966182fdbdSPeter Wemm 	fdc->fdout = fdout;
1397fb35bd37SJoerg Wunsch 	fdout_wr(fdc, fdout);
13983a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
13993a2f7427SDavid Greenman }
14003a2f7427SDavid Greenman 
1401381fe1aaSGarrett Wollman static void
14026182fdbdSPeter Wemm fd_turnoff(void *xfd)
14035b81b6b3SRodney W. Grimes {
1404f5f7ba03SJordan K. Hubbard 	int	s;
14056182fdbdSPeter Wemm 	fd_p fd = xfd;
1406dc16046fSJoerg Wunsch 
14076182fdbdSPeter Wemm 	TRACE1("[fd%d: turnoff]", fd->fdu);
14088335c1b8SBruce Evans 
14095f830ea2SJoerg Wunsch 	s = splbio();
14108335c1b8SBruce Evans 	/*
14118335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
14125f830ea2SJoerg Wunsch 	 *
14135f830ea2SJoerg Wunsch 	 * If we got here, this could only mean we missed an interrupt.
14145f830ea2SJoerg Wunsch 	 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
14155f830ea2SJoerg Wunsch 	 * after a controller reset.  Just schedule a pseudo-interrupt
14165f830ea2SJoerg Wunsch 	 * so the state machine gets re-entered.
14178335c1b8SBruce Evans 	 */
14186182fdbdSPeter Wemm 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
14195f830ea2SJoerg Wunsch 		fdc_intr(fd->fdc);
14205f830ea2SJoerg Wunsch 		splx(s);
14218335c1b8SBruce Evans 		return;
14228335c1b8SBruce Evans 	}
14238335c1b8SBruce Evans 
14245b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
14256182fdbdSPeter Wemm 	set_motor(fd->fdc, fd->fdsu, TURNOFF);
1426f5f7ba03SJordan K. Hubbard 	splx(s);
14275b81b6b3SRodney W. Grimes }
14285b81b6b3SRodney W. Grimes 
14293a2f7427SDavid Greenman static void
14306182fdbdSPeter Wemm fd_motor_on(void *xfd)
14315b81b6b3SRodney W. Grimes {
1432f5f7ba03SJordan K. Hubbard 	int	s;
14336182fdbdSPeter Wemm 	fd_p fd = xfd;
1434f5f7ba03SJordan K. Hubbard 
1435f5f7ba03SJordan K. Hubbard 	s = splbio();
14365b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
14375b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
14385b81b6b3SRodney W. Grimes 	{
14396182fdbdSPeter Wemm 		fdc_intr(fd->fdc);
14405b81b6b3SRodney W. Grimes 	}
1441f5f7ba03SJordan K. Hubbard 	splx(s);
14425b81b6b3SRodney W. Grimes }
14435b81b6b3SRodney W. Grimes 
14443a2f7427SDavid Greenman static void
14456182fdbdSPeter Wemm fd_turnon(fd_p fd)
14465b81b6b3SRodney W. Grimes {
14475b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
14485b81b6b3SRodney W. Grimes 	{
14493a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
14506182fdbdSPeter Wemm 		set_motor(fd->fdc, fd->fdsu, TURNON);
14516182fdbdSPeter Wemm 		timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
14525b81b6b3SRodney W. Grimes 	}
14535b81b6b3SRodney W. Grimes }
14545b81b6b3SRodney W. Grimes 
1455381fe1aaSGarrett Wollman static void
1456dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
14575b81b6b3SRodney W. Grimes {
14583a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
1459427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
14603a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
14613a2f7427SDavid Greenman 	DELAY(100);
14623a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
1463427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
14643a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
14653a2f7427SDavid Greenman 	DELAY(100);
1466427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout);
14673a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
14683a2f7427SDavid Greenman 
146964860614SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
14706182fdbdSPeter Wemm 	(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1471dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1472dc5df763SJoerg Wunsch 		     0);
1473d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1474d66c374fSTor Egge 		(void) enable_fifo(fdc);
14755b81b6b3SRodney W. Grimes }
14765b81b6b3SRodney W. Grimes 
1477246ed35dSJoerg Wunsch /*
1478246ed35dSJoerg Wunsch  * FDC IO functions, take care of the main status register, timeout
1479246ed35dSJoerg Wunsch  * in case the desired status bits are never set.
1480a2642c4dSJoerg Wunsch  *
1481a2642c4dSJoerg Wunsch  * These PIO loops initially start out with short delays between
1482a2642c4dSJoerg Wunsch  * each iteration in the expectation that the required condition
1483a2642c4dSJoerg Wunsch  * is usually met quickly, so it can be handled immediately.  After
1484a2642c4dSJoerg Wunsch  * about 1 ms, stepping is increased to achieve a better timing
1485a2642c4dSJoerg Wunsch  * accuracy in the calls to DELAY().
1486246ed35dSJoerg Wunsch  */
1487b5e8ce9fSBruce Evans static int
14886182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr)
1489dc5df763SJoerg Wunsch {
1490a2642c4dSJoerg Wunsch 	int i, j, step;
1491a2642c4dSJoerg Wunsch 
1492a2642c4dSJoerg Wunsch 	for (j = 0, step = 1;
1493a2642c4dSJoerg Wunsch 	    (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1494a2642c4dSJoerg Wunsch 	    j < FDSTS_TIMEOUT;
1495a2642c4dSJoerg Wunsch 	    j += step) {
1496dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
14978a0ba818SJoerg Wunsch 			return (fdc_err(fdc, "ready for output in input\n"));
1498a2642c4dSJoerg Wunsch 		if (j == 1000)
1499a2642c4dSJoerg Wunsch 			step = 1000;
1500a2642c4dSJoerg Wunsch 		DELAY(step);
1501d0900d6bSJoerg Wunsch 	}
1502a2642c4dSJoerg Wunsch 	if (j >= FDSTS_TIMEOUT)
15038a0ba818SJoerg Wunsch 		return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1504d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1505427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1506dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1507dc5df763SJoerg Wunsch 	*ptr = i;
15088a0ba818SJoerg Wunsch 	return (0);
1509d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1510427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1511dc5df763SJoerg Wunsch 	if (ptr)
1512dc5df763SJoerg Wunsch 		*ptr = i;
15138a0ba818SJoerg Wunsch 	return (0);
1514d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1515dc5df763SJoerg Wunsch }
1516dc5df763SJoerg Wunsch 
151737c84183SPoul-Henning Kamp static int
15186182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x)
15195b81b6b3SRodney W. Grimes {
1520a2642c4dSJoerg Wunsch 	int i, j, step;
15215b81b6b3SRodney W. Grimes 
1522a2642c4dSJoerg Wunsch 	for (j = 0, step = 1;
1523a2642c4dSJoerg Wunsch 	    (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1524a2642c4dSJoerg Wunsch 	    j < FDSTS_TIMEOUT;
1525a2642c4dSJoerg Wunsch 	    j += step) {
1526a2642c4dSJoerg Wunsch 		if (i == (NE7_DIO|NE7_RQM))
1527a2642c4dSJoerg Wunsch 			return (fdc_err(fdc, "ready for input in output\n"));
1528a2642c4dSJoerg Wunsch 		if (j == 1000)
1529a2642c4dSJoerg Wunsch 			step = 1000;
1530a2642c4dSJoerg Wunsch 		DELAY(step);
1531a2642c4dSJoerg Wunsch 	}
1532a2642c4dSJoerg Wunsch 	if (j >= FDSTS_TIMEOUT)
15338a0ba818SJoerg Wunsch 		return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
15343b3837dbSRodney W. Grimes 
15353b3837dbSRodney W. Grimes 	/* Send the command and return */
1536427ccf00SDoug Rabson 	fddata_wr(fdc, x);
15373a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
15385b81b6b3SRodney W. Grimes 	return (0);
15395b81b6b3SRodney W. Grimes }
15405b81b6b3SRodney W. Grimes 
1541246ed35dSJoerg Wunsch /*
1542246ed35dSJoerg Wunsch  * Block device driver interface functions (interspersed with even more
1543246ed35dSJoerg Wunsch  * auxiliary functions).
1544246ed35dSJoerg Wunsch  */
154537c84183SPoul-Henning Kamp static int
1546b40ce416SJulian Elischer Fdopen(dev_t dev, int flags, int mode, struct thread *td)
15475b81b6b3SRodney W. Grimes {
15485b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
154920a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
15506182fdbdSPeter Wemm 	fd_p	fd;
1551b99f0a4aSAndrew Moore 	fdc_p	fdc;
15521a6bed68SJoerg Wunsch  	int rv, unitattn, dflags;
15535b81b6b3SRodney W. Grimes 
15546182fdbdSPeter Wemm 	if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1555b99f0a4aSAndrew Moore 		return (ENXIO);
15566182fdbdSPeter Wemm 	fdc = fd->fdc;
15571a6bed68SJoerg Wunsch 	if ((fdc == NULL) || (fd->type == FDT_NONE))
1558b99f0a4aSAndrew Moore 		return (ENXIO);
1559b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1560b99f0a4aSAndrew Moore 		return (ENXIO);
15611a6bed68SJoerg Wunsch 	dflags = device_get_flags(fd->dev);
15623e425b96SJulian Elischer 	/*
15631a6bed68SJoerg Wunsch 	 * This is a bit bogus.  It's still possible that e. g. a
15641a6bed68SJoerg Wunsch 	 * descriptor gets inherited to a child, but then it's at
15651a6bed68SJoerg Wunsch 	 * least for the same subdevice.  By checking FD_OPEN here, we
15661a6bed68SJoerg Wunsch 	 * can ensure that a device isn't attempted to be opened with
15671a6bed68SJoerg Wunsch 	 * different densities at the same time where the second open
15681a6bed68SJoerg Wunsch 	 * could clobber the settings from the first one.
15693e425b96SJulian Elischer 	 */
15701a6bed68SJoerg Wunsch 	if (fd->flags & FD_OPEN)
15711a6bed68SJoerg Wunsch 		return (EBUSY);
15721a6bed68SJoerg Wunsch 
15731a6bed68SJoerg Wunsch 	if (type == 0) {
15741a6bed68SJoerg Wunsch 		if (flags & FNONBLOCK) {
15751a6bed68SJoerg Wunsch 			/*
15761a6bed68SJoerg Wunsch 			 * Unfortunately, physio(9) discards its ioflag
15771a6bed68SJoerg Wunsch 			 * argument, thus preventing us from seeing the
15781a6bed68SJoerg Wunsch 			 * IO_NDELAY bit.  So we need to keep track
15791a6bed68SJoerg Wunsch 			 * ourselves.
15801a6bed68SJoerg Wunsch 			 */
15811a6bed68SJoerg Wunsch 			fd->flags |= FD_NONBLOCK;
15821a6bed68SJoerg Wunsch 			fd->ft = 0;
15831a6bed68SJoerg Wunsch 		} else {
15841a6bed68SJoerg Wunsch 			/*
15851a6bed68SJoerg Wunsch 			 * Figure out a unit attention condition.
15861a6bed68SJoerg Wunsch 			 *
15871a6bed68SJoerg Wunsch 			 * If UA has been forced, proceed.
15881a6bed68SJoerg Wunsch 			 *
15891a6bed68SJoerg Wunsch 			 * If motor is off, turn it on for a moment
15901a6bed68SJoerg Wunsch 			 * and select our drive, in order to read the
15911a6bed68SJoerg Wunsch 			 * UA hardware signal.
15921a6bed68SJoerg Wunsch 			 *
15931a6bed68SJoerg Wunsch 			 * If motor is on, and our drive is currently
15941a6bed68SJoerg Wunsch 			 * selected, just read the hardware bit.
15951a6bed68SJoerg Wunsch 			 *
15961a6bed68SJoerg Wunsch 			 * If motor is on, but active for another
15971a6bed68SJoerg Wunsch 			 * drive on that controller, we are lost.  We
15981a6bed68SJoerg Wunsch 			 * cannot risk to deselect the other drive, so
15991a6bed68SJoerg Wunsch 			 * we just assume a forced UA condition to be
16001a6bed68SJoerg Wunsch 			 * on the safe side.
16011a6bed68SJoerg Wunsch 			 */
16021a6bed68SJoerg Wunsch 			unitattn = 0;
16031a6bed68SJoerg Wunsch 			if ((dflags & FD_NO_CHLINE) != 0 ||
16041a6bed68SJoerg Wunsch 			    (fd->flags & FD_UA) != 0) {
16051a6bed68SJoerg Wunsch 				unitattn = 1;
16061a6bed68SJoerg Wunsch 				fd->flags &= ~FD_UA;
16071a6bed68SJoerg Wunsch 			} else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 |
16081a6bed68SJoerg Wunsch 						 FDO_MOEN2 | FDO_MOEN3)) {
16091a6bed68SJoerg Wunsch 				if ((fdc->fdout & FDO_FDSEL) == fd->fdsu)
16101a6bed68SJoerg Wunsch 					unitattn = fdin_rd(fdc) & FDI_DCHG;
16111a6bed68SJoerg Wunsch 				else
16121a6bed68SJoerg Wunsch 					unitattn = 1;
16131a6bed68SJoerg Wunsch 			} else {
16141a6bed68SJoerg Wunsch 				set_motor(fdc, fd->fdsu, TURNON);
16151a6bed68SJoerg Wunsch 				unitattn = fdin_rd(fdc) & FDI_DCHG;
16161a6bed68SJoerg Wunsch 				set_motor(fdc, fd->fdsu, TURNOFF);
1617b39c878eSAndrey A. Chernov 			}
16181a6bed68SJoerg Wunsch 			if (unitattn && (rv = fdautoselect(dev)) != 0)
16191a6bed68SJoerg Wunsch 				return (rv);
16207ca0641bSAndrey A. Chernov 		}
16211a6bed68SJoerg Wunsch 	} else {
16221a6bed68SJoerg Wunsch 		fd->ft = fd->fts + type;
16237ca0641bSAndrey A. Chernov 	}
16246182fdbdSPeter Wemm 	fd->flags |= FD_OPEN;
16253fef646eSJoerg Wunsch 	/*
16263fef646eSJoerg Wunsch 	 * Clearing the DMA overrun counter at open time is a bit messy.
16273fef646eSJoerg Wunsch 	 * Since we're only managing one counter per controller, opening
16283fef646eSJoerg Wunsch 	 * the second drive could mess it up.  Anyway, if the DMA overrun
16293fef646eSJoerg Wunsch 	 * condition is really persistent, it will eventually time out
16303fef646eSJoerg Wunsch 	 * still.  OTOH, clearing it here will ensure we'll at least start
16313fef646eSJoerg Wunsch 	 * trying again after a previous (maybe even long ago) failure.
16323fef646eSJoerg Wunsch 	 * Also, this is merely a stop-gap measure only that should not
16333fef646eSJoerg Wunsch 	 * happen during normal operation, so we can tolerate it to be a
16343fef646eSJoerg Wunsch 	 * bit sloppy about this.
16353fef646eSJoerg Wunsch 	 */
16363fef646eSJoerg Wunsch 	fdc->dma_overruns = 0;
16375f830ea2SJoerg Wunsch 
16385b81b6b3SRodney W. Grimes 	return 0;
16395b81b6b3SRodney W. Grimes }
16405b81b6b3SRodney W. Grimes 
164137c84183SPoul-Henning Kamp static int
1642b40ce416SJulian Elischer fdclose(dev_t dev, int flags, int mode, struct thread *td)
16435b81b6b3SRodney W. Grimes {
16445b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
16456182fdbdSPeter Wemm 	struct fd_data *fd;
1646b99f0a4aSAndrew Moore 
16476182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
16481a6bed68SJoerg Wunsch 	fd->flags &= ~(FD_OPEN | FD_NONBLOCK);
16492995d110SJoerg Wunsch 	fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1650dc16046fSJoerg Wunsch 
16515b81b6b3SRodney W. Grimes 	return (0);
16525b81b6b3SRodney W. Grimes }
16535b81b6b3SRodney W. Grimes 
165437c84183SPoul-Henning Kamp static void
16558177437dSPoul-Henning Kamp fdstrategy(struct bio *bp)
16563a2f7427SDavid Greenman {
1657fb35bd37SJoerg Wunsch 	long blknum, nblocks;
16583a2f7427SDavid Greenman  	int	s;
16593a2f7427SDavid Greenman  	fdu_t	fdu;
16603a2f7427SDavid Greenman  	fdc_p	fdc;
16613a2f7427SDavid Greenman  	fd_p	fd;
16623a2f7427SDavid Greenman 	size_t	fdblk;
16633a2f7427SDavid Greenman 
16648177437dSPoul-Henning Kamp  	fdu = FDUNIT(minor(bp->bio_dev));
16656182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
16666182fdbdSPeter Wemm 	if (fd == 0)
16676182fdbdSPeter Wemm 		panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
16688177437dSPoul-Henning Kamp 		      (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev));
16693a2f7427SDavid Greenman 	fdc = fd->fdc;
16701a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE || fd->ft == 0) {
16718177437dSPoul-Henning Kamp 		bp->bio_error = ENXIO;
16728177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
16733b178206SWarner Losh 		goto bad;
167464860614SJoerg Wunsch 	}
1675d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
1676419f39ceSPoul-Henning Kamp 	if (bp->bio_cmd != FDBIO_FORMAT && bp->bio_cmd != FDBIO_RDSECTID) {
16771a6bed68SJoerg Wunsch 		if (fd->flags & FD_NONBLOCK) {
16781a6bed68SJoerg Wunsch 			bp->bio_error = EAGAIN;
16791a6bed68SJoerg Wunsch 			bp->bio_flags |= BIO_ERROR;
16801a6bed68SJoerg Wunsch 			goto bad;
16811a6bed68SJoerg Wunsch 		}
16828177437dSPoul-Henning Kamp 		if (bp->bio_blkno < 0) {
1683dc5df763SJoerg Wunsch 			printf(
16846a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
16858177437dSPoul-Henning Kamp 			       fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
16868177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
16878177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
16883a2f7427SDavid Greenman 			goto bad;
16893a2f7427SDavid Greenman 		}
16908177437dSPoul-Henning Kamp 		if ((bp->bio_bcount % fdblk) != 0) {
16918177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
16928177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
16933a2f7427SDavid Greenman 			goto bad;
16943a2f7427SDavid Greenman 		}
16953a2f7427SDavid Greenman 	}
16963a2f7427SDavid Greenman 
16973a2f7427SDavid Greenman 	/*
16983a2f7427SDavid Greenman 	 * Set up block calculations.
16993a2f7427SDavid Greenman 	 */
17008177437dSPoul-Henning Kamp 	if (bp->bio_blkno > 20000000) {
1701bb6382faSJoerg Wunsch 		/*
1702bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1703bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1704bb6382faSJoerg Wunsch 		 */
17058177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
17068177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
17073a2f7427SDavid Greenman 		goto bad;
17083a2f7427SDavid Greenman 	}
1709fb35bd37SJoerg Wunsch 	blknum = bp->bio_blkno * DEV_BSIZE / fdblk;
1710bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1711fb35bd37SJoerg Wunsch 	if (blknum + bp->bio_bcount / fdblk > nblocks) {
1712fb35bd37SJoerg Wunsch 		if (blknum >= nblocks) {
1713fb35bd37SJoerg Wunsch 			if (bp->bio_cmd == BIO_READ)
1714fb35bd37SJoerg Wunsch 				bp->bio_resid = bp->bio_bcount;
1715fb35bd37SJoerg Wunsch 			else {
1716fb35bd37SJoerg Wunsch 				bp->bio_error = ENOSPC;
17178177437dSPoul-Henning Kamp 				bp->bio_flags |= BIO_ERROR;
1718bb6382faSJoerg Wunsch 			}
1719fb35bd37SJoerg Wunsch 			goto bad;	/* not always bad, but EOF */
1720fb35bd37SJoerg Wunsch 		}
1721fb35bd37SJoerg Wunsch 		bp->bio_bcount = (nblocks - blknum) * fdblk;
1722bb6382faSJoerg Wunsch 	}
17239a5e3ddbSJoerg Wunsch  	bp->bio_pblkno = blknum;
17243a2f7427SDavid Greenman 	s = splbio();
17258177437dSPoul-Henning Kamp 	bioqdisksort(&fdc->head, bp);
17266182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1727b2dfb1f9SJustin T. Gibbs 	devstat_start_transaction(&fd->device_stats);
17285f830ea2SJoerg Wunsch 	device_busy(fd->dev);
17296182fdbdSPeter Wemm 	fdstart(fdc);
17303a2f7427SDavid Greenman 	splx(s);
17313a2f7427SDavid Greenman 	return;
17323a2f7427SDavid Greenman 
17333a2f7427SDavid Greenman bad:
17343a2f7427SDavid Greenman 	biodone(bp);
17353a2f7427SDavid Greenman }
17363a2f7427SDavid Greenman 
1737246ed35dSJoerg Wunsch /*
1738246ed35dSJoerg Wunsch  * fdstart
1739246ed35dSJoerg Wunsch  *
1740246ed35dSJoerg Wunsch  * We have just queued something.  If the controller is not busy
1741246ed35dSJoerg Wunsch  * then simulate the case where it has just finished a command
1742246ed35dSJoerg Wunsch  * So that it (the interrupt routine) looks on the queue for more
1743246ed35dSJoerg Wunsch  * work to do and picks up what we just added.
1744246ed35dSJoerg Wunsch  *
1745246ed35dSJoerg Wunsch  * If the controller is already busy, we need do nothing, as it
1746246ed35dSJoerg Wunsch  * will pick up our work when the present work completes.
1747246ed35dSJoerg Wunsch  */
1748381fe1aaSGarrett Wollman static void
17496182fdbdSPeter Wemm fdstart(struct fdc_data *fdc)
17505b81b6b3SRodney W. Grimes {
17515b81b6b3SRodney W. Grimes 	int s;
17525b81b6b3SRodney W. Grimes 
17535b81b6b3SRodney W. Grimes 	s = splbio();
17546182fdbdSPeter Wemm 	if(fdc->state == DEVIDLE)
17555b81b6b3SRodney W. Grimes 	{
17566182fdbdSPeter Wemm 		fdc_intr(fdc);
17575b81b6b3SRodney W. Grimes 	}
17585b81b6b3SRodney W. Grimes 	splx(s);
17595b81b6b3SRodney W. Grimes }
17605b81b6b3SRodney W. Grimes 
1761381fe1aaSGarrett Wollman static void
17626182fdbdSPeter Wemm fd_iotimeout(void *xfdc)
17635b81b6b3SRodney W. Grimes {
17645c1a1eaeSBruce Evans  	fdc_p fdc;
1765f5f7ba03SJordan K. Hubbard 	int s;
17665b81b6b3SRodney W. Grimes 
17676182fdbdSPeter Wemm 	fdc = xfdc;
17685c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
17695b81b6b3SRodney W. Grimes 
17703a2f7427SDavid Greenman 	/*
17713a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
17723a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
17733a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
17743a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
17753a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
17765c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
17775c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
17785c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
17795c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
17803a2f7427SDavid Greenman 	 */
17813a2f7427SDavid Greenman 	s = splbio();
17825c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
17835c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
17845c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
17856182fdbdSPeter Wemm 	fdc_intr(fdc);
1786f5f7ba03SJordan K. Hubbard 	splx(s);
17875b81b6b3SRodney W. Grimes }
17885b81b6b3SRodney W. Grimes 
1789246ed35dSJoerg Wunsch /* Just ensure it has the right spl. */
1790381fe1aaSGarrett Wollman static void
17916182fdbdSPeter Wemm fd_pseudointr(void *xfdc)
17925b81b6b3SRodney W. Grimes {
17935b81b6b3SRodney W. Grimes 	int	s;
17943a2f7427SDavid Greenman 
17955b81b6b3SRodney W. Grimes 	s = splbio();
17966182fdbdSPeter Wemm 	fdc_intr(xfdc);
17975b81b6b3SRodney W. Grimes 	splx(s);
17985b81b6b3SRodney W. Grimes }
17995b81b6b3SRodney W. Grimes 
1800246ed35dSJoerg Wunsch /*
1801246ed35dSJoerg Wunsch  * fdc_intr
1802246ed35dSJoerg Wunsch  *
1803246ed35dSJoerg Wunsch  * Keep calling the state machine until it returns a 0.
1804246ed35dSJoerg Wunsch  * Always called at splbio.
1805246ed35dSJoerg Wunsch  */
1806fe310de8SBruce Evans static void
18076182fdbdSPeter Wemm fdc_intr(void *xfdc)
18085b81b6b3SRodney W. Grimes {
18096182fdbdSPeter Wemm 	fdc_p fdc = xfdc;
18106182fdbdSPeter Wemm 	while(fdstate(fdc))
1811381fe1aaSGarrett Wollman 		;
18125b81b6b3SRodney W. Grimes }
18135b81b6b3SRodney W. Grimes 
181469acd21dSWarner Losh /*
1815246ed35dSJoerg Wunsch  * Magic pseudo-DMA initialization for YE FDC. Sets count and
1816246ed35dSJoerg Wunsch  * direction.
181769acd21dSWarner Losh  */
18183b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \
18193b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port,	 \
18203b178206SWarner Losh 	    ((cnt)-1) & 0xff);						 \
18213b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
18223b178206SWarner Losh 	    ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
182369acd21dSWarner Losh 
182469acd21dSWarner Losh /*
1825246ed35dSJoerg Wunsch  * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy.
182669acd21dSWarner Losh  */
18274c34deeeSJoerg Wunsch static int
18284c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
182969acd21dSWarner Losh {
183069acd21dSWarner Losh 	u_char *cptr = (u_char *)addr;
183169acd21dSWarner Losh 
183221144e3bSPoul-Henning Kamp 	if (flags == BIO_READ) {
183369acd21dSWarner Losh 		if (fdc->state != PIOREAD) {
183469acd21dSWarner Losh 			fdc->state = PIOREAD;
183569acd21dSWarner Losh 			return(0);
183664860614SJoerg Wunsch 		}
18373b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
18383b178206SWarner Losh 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
18393b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
184069acd21dSWarner Losh 	} else {
18413b178206SWarner Losh 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
18423b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
18433b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
184464860614SJoerg Wunsch 	}
184569acd21dSWarner Losh 	return(1);
184669acd21dSWarner Losh }
184769acd21dSWarner Losh 
1848246ed35dSJoerg Wunsch /*
18491a6bed68SJoerg Wunsch  * Try figuring out the density of the media present in our device.
18501a6bed68SJoerg Wunsch  */
18511a6bed68SJoerg Wunsch static int
18521a6bed68SJoerg Wunsch fdautoselect(dev_t dev)
18531a6bed68SJoerg Wunsch {
18541a6bed68SJoerg Wunsch 	fdu_t fdu;
18551a6bed68SJoerg Wunsch  	fd_p fd;
18561a6bed68SJoerg Wunsch 	struct fd_type *fdtp;
18571a6bed68SJoerg Wunsch 	struct fdc_readid id;
18581a6bed68SJoerg Wunsch 	int i, n, oopts, rv;
18591a6bed68SJoerg Wunsch 
18601a6bed68SJoerg Wunsch  	fdu = FDUNIT(minor(dev));
18611a6bed68SJoerg Wunsch 	fd = devclass_get_softc(fd_devclass, fdu);
18621a6bed68SJoerg Wunsch 
18631a6bed68SJoerg Wunsch 	switch (fd->type) {
18641a6bed68SJoerg Wunsch 	default:
18651a6bed68SJoerg Wunsch 		return (ENXIO);
18661a6bed68SJoerg Wunsch 
18671a6bed68SJoerg Wunsch 	case FDT_360K:
18681a6bed68SJoerg Wunsch 	case FDT_720K:
18691a6bed68SJoerg Wunsch 		/* no autoselection on those drives */
18701a6bed68SJoerg Wunsch 		fd->ft = fd_native_types + fd->type;
18711a6bed68SJoerg Wunsch 		return (0);
18721a6bed68SJoerg Wunsch 
18731a6bed68SJoerg Wunsch 	case FDT_12M:
18741a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_12m;
18751a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_12m / sizeof(struct fd_type);
18761a6bed68SJoerg Wunsch 		break;
18771a6bed68SJoerg Wunsch 
18781a6bed68SJoerg Wunsch 	case FDT_144M:
18791a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_144m;
18801a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_144m / sizeof(struct fd_type);
18811a6bed68SJoerg Wunsch 		break;
18821a6bed68SJoerg Wunsch 
18831a6bed68SJoerg Wunsch 	case FDT_288M:
18841a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_288m;
18851a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_288m / sizeof(struct fd_type);
18861a6bed68SJoerg Wunsch 		break;
18871a6bed68SJoerg Wunsch 	}
18881a6bed68SJoerg Wunsch 
18891a6bed68SJoerg Wunsch 	/*
18901a6bed68SJoerg Wunsch 	 * Try reading sector ID fields, first at cylinder 0, head 0,
18911a6bed68SJoerg Wunsch 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
18921a6bed68SJoerg Wunsch 	 * since for 5.25in DD media in a HD drive, there are no data
18931a6bed68SJoerg Wunsch 	 * to read (2 step pulses per media cylinder required).  For
18941a6bed68SJoerg Wunsch 	 * two-sided media, the second probe always goes to head 1, so
18951a6bed68SJoerg Wunsch 	 * we can tell them apart from single-sided media.  As a
18961a6bed68SJoerg Wunsch 	 * side-effect this means that single-sided media should be
18971a6bed68SJoerg Wunsch 	 * mentioned in the search list after two-sided media of an
18981a6bed68SJoerg Wunsch 	 * otherwise identical density.  Media with a different number
18991a6bed68SJoerg Wunsch 	 * of sectors per track but otherwise identical parameters
19001a6bed68SJoerg Wunsch 	 * cannot be distinguished at all.
19011a6bed68SJoerg Wunsch 	 *
19021a6bed68SJoerg Wunsch 	 * If we successfully read an ID field on both cylinders where
19031a6bed68SJoerg Wunsch 	 * the recorded values match our expectation, we are done.
19041a6bed68SJoerg Wunsch 	 * Otherwise, we try the next density entry from the table.
19051a6bed68SJoerg Wunsch 	 *
19061a6bed68SJoerg Wunsch 	 * Stepping to cylinder 2 has the side-effect of clearing the
19071a6bed68SJoerg Wunsch 	 * unit attention bit.
19081a6bed68SJoerg Wunsch 	 */
19091a6bed68SJoerg Wunsch 	oopts = fd->options;
19101a6bed68SJoerg Wunsch 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
19111a6bed68SJoerg Wunsch 	for (i = 0; i < n; i++, fdtp++) {
19121a6bed68SJoerg Wunsch 		fd->ft = fdtp;
19131a6bed68SJoerg Wunsch 
19141a6bed68SJoerg Wunsch 		id.cyl = id.head = 0;
1915419f39ceSPoul-Henning Kamp 		rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
19161a6bed68SJoerg Wunsch 		if (rv != 0)
19171a6bed68SJoerg Wunsch 			continue;
19181a6bed68SJoerg Wunsch 		if (id.cyl != 0 || id.head != 0 ||
19191a6bed68SJoerg Wunsch 		    id.secshift != fdtp->secsize)
19201a6bed68SJoerg Wunsch 			continue;
19211a6bed68SJoerg Wunsch 		id.cyl = 2;
19221a6bed68SJoerg Wunsch 		id.head = fd->ft->heads - 1;
1923419f39ceSPoul-Henning Kamp 		rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
19241a6bed68SJoerg Wunsch 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
19251a6bed68SJoerg Wunsch 		    id.secshift != fdtp->secsize)
19261a6bed68SJoerg Wunsch 			continue;
19271a6bed68SJoerg Wunsch 		if (rv == 0)
19281a6bed68SJoerg Wunsch 			break;
19291a6bed68SJoerg Wunsch 	}
19301a6bed68SJoerg Wunsch 
19311a6bed68SJoerg Wunsch 	fd->options = oopts;
19321a6bed68SJoerg Wunsch 	if (i == n) {
19335613959dSJoerg Wunsch 		if (bootverbose)
19341a6bed68SJoerg Wunsch 			device_printf(fd->dev, "autoselection failed\n");
19351a6bed68SJoerg Wunsch 		fd->ft = 0;
19361a6bed68SJoerg Wunsch 		return (EIO);
19371a6bed68SJoerg Wunsch 	} else {
19385613959dSJoerg Wunsch 		if (bootverbose)
19391a6bed68SJoerg Wunsch 			device_printf(fd->dev, "autoselected %d KB medium\n",
19401a6bed68SJoerg Wunsch 				      fd->ft->size / 2);
19411a6bed68SJoerg Wunsch 		return (0);
19421a6bed68SJoerg Wunsch 	}
19431a6bed68SJoerg Wunsch }
19441a6bed68SJoerg Wunsch 
19451a6bed68SJoerg Wunsch 
19461a6bed68SJoerg Wunsch /*
1947246ed35dSJoerg Wunsch  * The controller state machine.
1948246ed35dSJoerg Wunsch  *
1949246ed35dSJoerg Wunsch  * If it returns a non zero value, it should be called again immediately.
1950246ed35dSJoerg Wunsch  */
19513a2f7427SDavid Greenman static int
19526182fdbdSPeter Wemm fdstate(fdc_p fdc)
19535b81b6b3SRodney W. Grimes {
195464860614SJoerg Wunsch 	struct fdc_readid *idp;
1955fb35bd37SJoerg Wunsch 	int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
19561a6bed68SJoerg Wunsch 	int st0, cyl, st3, idf, ne7cmd, mfm, steptrac;
1957fb35bd37SJoerg Wunsch 	unsigned long blknum;
19585b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
19595b81b6b3SRodney W. Grimes 	fd_p fd;
19608177437dSPoul-Henning Kamp 	register struct bio *bp;
1961b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
19623a2f7427SDavid Greenman 	size_t fdblk;
19635b81b6b3SRodney W. Grimes 
1964e93e63cbSBruce Evans 	bp = fdc->bp;
1965e93e63cbSBruce Evans 	if (bp == NULL) {
19668177437dSPoul-Henning Kamp 		bp = bioq_first(&fdc->head);
1967e93e63cbSBruce Evans 		if (bp != NULL) {
19688177437dSPoul-Henning Kamp 			bioq_remove(&fdc->head, bp);
1969e93e63cbSBruce Evans 			fdc->bp = bp;
1970e93e63cbSBruce Evans 		}
1971e93e63cbSBruce Evans 	}
1972e93e63cbSBruce Evans 	if (bp == NULL) {
1973246ed35dSJoerg Wunsch 		/*
1974246ed35dSJoerg Wunsch 		 * Nothing left for this controller to do,
1975246ed35dSJoerg Wunsch 		 * force into the IDLE state.
1976246ed35dSJoerg Wunsch 		 */
19775b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
19786182fdbdSPeter Wemm 		if (fdc->fd) {
1979b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev,
1980b6e5f28eSPeter Wemm 			    "unexpected valid fd pointer\n");
19815b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
19825b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
19835b81b6b3SRodney W. Grimes 		}
19846182fdbdSPeter Wemm 		TRACE1("[fdc%d IDLE]", fdc->fdcu);
19855b81b6b3SRodney W. Grimes  		return (0);
19865b81b6b3SRodney W. Grimes 	}
19878177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
19886182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
19893a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1990b6e5f28eSPeter Wemm 	if (fdc->fd && (fd != fdc->fd))
1991b6e5f28eSPeter Wemm 		device_printf(fd->dev, "confused fd pointers\n");
19928177437dSPoul-Henning Kamp 	read = bp->bio_cmd == BIO_READ;
19931a6bed68SJoerg Wunsch 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
19941a6bed68SJoerg Wunsch 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
199556a23089SPoul-Henning Kamp 	if (read)
199656a23089SPoul-Henning Kamp 		idf = ISADMA_READ;
199756a23089SPoul-Henning Kamp 	else
199856a23089SPoul-Henning Kamp 		idf = ISADMA_WRITE;
1999419f39ceSPoul-Henning Kamp 	format = bp->bio_cmd == FDBIO_FORMAT;
2000419f39ceSPoul-Henning Kamp 	rdsectid = bp->bio_cmd == FDBIO_RDSECTID;
2001fb35bd37SJoerg Wunsch 	if (format)
20028177437dSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
20035b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
20045b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
20055b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
20066182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle);
20076182fdbdSPeter Wemm 	fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
20085b81b6b3SRodney W. Grimes 	switch (fdc->state)
20095b81b6b3SRodney W. Grimes 	{
20105b81b6b3SRodney W. Grimes 	case DEVIDLE:
20115b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
20125b81b6b3SRodney W. Grimes 		fdc->retry = 0;
20135b81b6b3SRodney W. Grimes 		fd->skip = 0;
20145b81b6b3SRodney W. Grimes 		fdc->fd = fd;
20155b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
20165f830ea2SJoerg Wunsch 		fdc->fdctl_wr(fdc, fd->ft->trans);
20173a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
2018246ed35dSJoerg Wunsch 		/*
2019246ed35dSJoerg Wunsch 		 * If the next drive has a motor startup pending, then
2020246ed35dSJoerg Wunsch 		 * it will start up in its own good time.
2021246ed35dSJoerg Wunsch 		 */
20226182fdbdSPeter Wemm 		if(fd->flags & FD_MOTOR_WAIT) {
20235b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
2024246ed35dSJoerg Wunsch 			return (0); /* will return later */
20255b81b6b3SRodney W. Grimes 		}
2026246ed35dSJoerg Wunsch 		/*
2027246ed35dSJoerg Wunsch 		 * Maybe if it's not starting, it SHOULD be starting.
2028246ed35dSJoerg Wunsch 		 */
20295b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
20305b81b6b3SRodney W. Grimes 		{
20315b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
20326182fdbdSPeter Wemm 			fd_turnon(fd);
2033246ed35dSJoerg Wunsch 			return (0); /* will return later */
20345b81b6b3SRodney W. Grimes 		}
20355b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
20365b81b6b3SRodney W. Grimes 		{
20376182fdbdSPeter Wemm 			set_motor(fdc, fd->fdsu, TURNON);
20385b81b6b3SRodney W. Grimes 		}
20395c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
20405c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
20415c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
20425c1a1eaeSBruce Evans 		} else
20435e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
2044246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2045fb35bd37SJoerg Wunsch 
20465b81b6b3SRodney W. Grimes 	case DOSEEK:
2047fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
2048fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2049fb35bd37SJoerg Wunsch 		if (cylinder == fd->track)
20505b81b6b3SRodney W. Grimes 		{
20515b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
2052246ed35dSJoerg Wunsch 			return (1); /* will return immediately */
20535b81b6b3SRodney W. Grimes 		}
20546182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK,
20551a6bed68SJoerg Wunsch 			   fd->fdsu, cylinder * steptrac, 0))
2056dc8603e3SJoerg Wunsch 		{
2057dc8603e3SJoerg Wunsch 			/*
2058246ed35dSJoerg Wunsch 			 * Seek command not accepted, looks like
2059dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
2060dc8603e3SJoerg Wunsch 			 */
2061dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
20626182fdbdSPeter Wemm 			return(retrier(fdc));
2063dc8603e3SJoerg Wunsch 		}
2064dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
20655b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
20665b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
2067fb35bd37SJoerg Wunsch 
20685b81b6b3SRodney W. Grimes 	case SEEKWAIT:
20695b81b6b3SRodney W. Grimes 		/* allow heads to settle */
20706182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 16);
20715b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
20725b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
2073fb35bd37SJoerg Wunsch 
2074246ed35dSJoerg Wunsch 	case SEEKCOMPLETE : /* seek done, start DMA */
2075fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
2076fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2077fb35bd37SJoerg Wunsch 
2078246ed35dSJoerg Wunsch 		/* Make sure seek really happened. */
20796182fdbdSPeter Wemm 		if(fd->track == FD_NO_TRACK) {
20801a6bed68SJoerg Wunsch 			int descyl = cylinder * steptrac;
20813a2f7427SDavid Greenman 			do {
20823a2f7427SDavid Greenman 				/*
2083dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
2084dc5df763SJoerg Wunsch 				 * which cannot really happen since the
2085dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
2086dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
2087dc5df763SJoerg Wunsch 				 * line, so do one of the following:
2088dc5df763SJoerg Wunsch 				 *
2089dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
2090dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
2091dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
2092dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
2093dc5df763SJoerg Wunsch 				 * real interrupt condition.
2094dc5df763SJoerg Wunsch 				 *
2095dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
2096dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
2097dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
2098dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
2099dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
2100dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
2101dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
21023a2f7427SDavid Greenman 				 */
2103dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
2104dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
2105246ed35dSJoerg Wunsch 					return (0); /* will return later */
2106dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
2107dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2108246ed35dSJoerg Wunsch 					return (0); /* hope for a real intr */
21093a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2110dc5df763SJoerg Wunsch 
21116182fdbdSPeter Wemm 			if (0 == descyl) {
2112dc5df763SJoerg Wunsch 				int failed = 0;
21133a2f7427SDavid Greenman 				/*
21143a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
21153a2f7427SDavid Greenman 				 * really there
21163a2f7427SDavid Greenman 				 */
2117dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
2118dc5df763SJoerg Wunsch 					failed = 1;
21193a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
21203a2f7427SDavid Greenman 					printf(
21213a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
21223a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
2123dc5df763SJoerg Wunsch 					failed = 1;
2124dc5df763SJoerg Wunsch 				}
2125dc5df763SJoerg Wunsch 
21266182fdbdSPeter Wemm 				if (failed) {
21273a2f7427SDavid Greenman 					if(fdc->retry < 3)
21283a2f7427SDavid Greenman 						fdc->retry = 3;
21296182fdbdSPeter Wemm 					return (retrier(fdc));
21303a2f7427SDavid Greenman 				}
21313a2f7427SDavid Greenman 			}
2132dc5df763SJoerg Wunsch 
21336182fdbdSPeter Wemm 			if (cyl != descyl) {
21343a2f7427SDavid Greenman 				printf(
21353a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
21362d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
2137e5d7d243SBruce Evans 				if (fdc->retry < 3)
2138e5d7d243SBruce Evans 					fdc->retry = 3;
21396182fdbdSPeter Wemm 				return (retrier(fdc));
21405b81b6b3SRodney W. Grimes 			}
21415b81b6b3SRodney W. Grimes 		}
21425b81b6b3SRodney W. Grimes 
2143fb35bd37SJoerg Wunsch 		fd->track = cylinder;
2144fb35bd37SJoerg Wunsch 		if (format)
2145fb35bd37SJoerg Wunsch 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
2146fb35bd37SJoerg Wunsch 			    - (char *)finfo;
2147250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
21488177437dSPoul-Henning Kamp 			isa_dmastart(idf, bp->bio_data+fd->skip,
21498177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
2150fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
21515b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
21525b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
21535b81b6b3SRodney W. Grimes 		head = sec / sectrac;
21545b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
21551a6bed68SJoerg Wunsch 		if (head != 0 && fd->ft->offset_side2 != 0)
21561a6bed68SJoerg Wunsch 			sec += fd->ft->offset_side2;
21573a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
21583a2f7427SDavid Greenman 
2159250300ebSJoerg Wunsch 		if(format || !(read || rdsectid))
21603a2f7427SDavid Greenman 		{
21613a2f7427SDavid Greenman 			/* make sure the drive is writable */
2162dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
2163dc8603e3SJoerg Wunsch 			{
2164dc8603e3SJoerg Wunsch 				/* stuck controller? */
21655f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
216656a23089SPoul-Henning Kamp 					isa_dmadone(idf,
21678177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
21688177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
21695c1a1eaeSBruce Evans 						    fdc->dmachan);
2170dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
21716182fdbdSPeter Wemm 				return (retrier(fdc));
2172dc8603e3SJoerg Wunsch 			}
21733a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
21743a2f7427SDavid Greenman 			{
21753a2f7427SDavid Greenman 				/*
21763a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
21773a2f7427SDavid Greenman 				 * in order to force the current operation
21783a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
21793a2f7427SDavid Greenman 				 * error - all error handling is done
21803a2f7427SDavid Greenman 				 * by the retrier()
21813a2f7427SDavid Greenman 				 */
21823a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
21833a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
21843a2f7427SDavid Greenman 				fdc->status[2] = 0;
21853a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
21863a2f7427SDavid Greenman 				fdc->status[4] = head;
21873a2f7427SDavid Greenman 				fdc->status[5] = sec;
21883a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
21893a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
2190246ed35dSJoerg Wunsch 				return (1); /* will return immediately */
21913a2f7427SDavid Greenman 			}
21923a2f7427SDavid Greenman 		}
21935b81b6b3SRodney W. Grimes 
21946182fdbdSPeter Wemm 		if (format) {
21951a6bed68SJoerg Wunsch 			ne7cmd = NE7CMD_FORMAT | mfm;
21965f830ea2SJoerg Wunsch 			if (fdc->flags & FDC_NODMA) {
21975f830ea2SJoerg Wunsch 				/*
21985f830ea2SJoerg Wunsch 				 * This seems to be necessary for
21995f830ea2SJoerg Wunsch 				 * whatever obscure reason; if we omit
22005f830ea2SJoerg Wunsch 				 * it, we end up filling the sector ID
22015f830ea2SJoerg Wunsch 				 * fields of the newly formatted track
22025f830ea2SJoerg Wunsch 				 * entirely with garbage, causing
22035f830ea2SJoerg Wunsch 				 * `wrong cylinder' errors all over
22045f830ea2SJoerg Wunsch 				 * the place when trying to read them
22055f830ea2SJoerg Wunsch 				 * back.
22065f830ea2SJoerg Wunsch 				 *
22075f830ea2SJoerg Wunsch 				 * Umpf.
22085f830ea2SJoerg Wunsch 				 */
22098177437dSPoul-Henning Kamp 				SET_BCDR(fdc, 1, bp->bio_bcount, 0);
22105f830ea2SJoerg Wunsch 
22118177437dSPoul-Henning Kamp 				(void)fdcpio(fdc,bp->bio_cmd,
22128177437dSPoul-Henning Kamp 					bp->bio_data+fd->skip,
22138177437dSPoul-Henning Kamp 					bp->bio_bcount);
22145f830ea2SJoerg Wunsch 
22155f830ea2SJoerg Wunsch 			}
2216b39c878eSAndrey A. Chernov 			/* formatting */
22171a6bed68SJoerg Wunsch 			if(fd_cmd(fdc, 6,  ne7cmd, head << 2 | fdu,
2218dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
2219dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
2220dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
22216182fdbdSPeter Wemm 				  finfo->fd_formb_fillbyte, 0)) {
2222dc8603e3SJoerg Wunsch 				/* controller fell over */
22235f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
222456a23089SPoul-Henning Kamp 					isa_dmadone(idf,
22258177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
22268177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
22275c1a1eaeSBruce Evans 						    fdc->dmachan);
2228dc8603e3SJoerg Wunsch 				fdc->retry = 6;
22296182fdbdSPeter Wemm 				return (retrier(fdc));
2230dc8603e3SJoerg Wunsch 			}
2231250300ebSJoerg Wunsch 		} else if (rdsectid) {
22321a6bed68SJoerg Wunsch 			ne7cmd = NE7CMD_READID | mfm;
22331a6bed68SJoerg Wunsch 			if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) {
2234250300ebSJoerg Wunsch 				/* controller jamming */
2235250300ebSJoerg Wunsch 				fdc->retry = 6;
2236250300ebSJoerg Wunsch 				return (retrier(fdc));
2237250300ebSJoerg Wunsch 			}
22386182fdbdSPeter Wemm 		} else {
2239250300ebSJoerg Wunsch 			/* read or write operation */
22401a6bed68SJoerg Wunsch 			ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm;
22413b178206SWarner Losh 			if (fdc->flags & FDC_NODMA) {
224269acd21dSWarner Losh 				/*
2243246ed35dSJoerg Wunsch 				 * This seems to be necessary even when
2244246ed35dSJoerg Wunsch 				 * reading data.
224569acd21dSWarner Losh 				 */
22463b178206SWarner Losh 				SET_BCDR(fdc, 1, fdblk, 0);
224769acd21dSWarner Losh 
224869acd21dSWarner Losh 				/*
2249246ed35dSJoerg Wunsch 				 * Perform the write pseudo-DMA before
2250246ed35dSJoerg Wunsch 				 * the WRITE command is sent.
225169acd21dSWarner Losh 				 */
225269acd21dSWarner Losh 				if (!read)
22538177437dSPoul-Henning Kamp 					(void)fdcpio(fdc,bp->bio_cmd,
22548177437dSPoul-Henning Kamp 					    bp->bio_data+fd->skip,
225569acd21dSWarner Losh 					    fdblk);
225669acd21dSWarner Losh 			}
22576182fdbdSPeter Wemm 			if (fd_cmd(fdc, 9,
22581a6bed68SJoerg Wunsch 				   ne7cmd,
2259dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
2260dc5df763SJoerg Wunsch 				   fd->track,        /* track */
2261dc5df763SJoerg Wunsch 				   head,
2262dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
2263dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
2264dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
2265dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
2266dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
22676182fdbdSPeter Wemm 				   0)) {
2268dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
22695f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
227056a23089SPoul-Henning Kamp 					isa_dmadone(idf,
22718177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
22728177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
22735c1a1eaeSBruce Evans 						    fdc->dmachan);
2274dc8603e3SJoerg Wunsch 				fdc->retry = 6;
22756182fdbdSPeter Wemm 				return (retrier(fdc));
22765b81b6b3SRodney W. Grimes 			}
2277b39c878eSAndrey A. Chernov 		}
2278250300ebSJoerg Wunsch 		if (!rdsectid && (fdc->flags & FDC_NODMA))
227969acd21dSWarner Losh 			/*
2280246ed35dSJoerg Wunsch 			 * If this is a read, then simply await interrupt
2281246ed35dSJoerg Wunsch 			 * before performing PIO.
228269acd21dSWarner Losh 			 */
22838177437dSPoul-Henning Kamp 			if (read && !fdcpio(fdc,bp->bio_cmd,
22848177437dSPoul-Henning Kamp 			    bp->bio_data+fd->skip,fdblk)) {
22853b178206SWarner Losh 				fd->tohandle = timeout(fd_iotimeout, fdc, hz);
228669acd21dSWarner Losh 				return(0);      /* will return later */
228764860614SJoerg Wunsch 			}
228869acd21dSWarner Losh 
228969acd21dSWarner Losh 		/*
2290246ed35dSJoerg Wunsch 		 * Write (or format) operation will fall through and
2291246ed35dSJoerg Wunsch 		 * await completion interrupt.
229269acd21dSWarner Losh 		 */
22935b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
22946182fdbdSPeter Wemm 		fd->tohandle = timeout(fd_iotimeout, fdc, hz);
22955b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2296fb35bd37SJoerg Wunsch 
229769acd21dSWarner Losh 	case PIOREAD:
229869acd21dSWarner Losh 		/*
2299246ed35dSJoerg Wunsch 		 * Actually perform the PIO read.  The IOCOMPLETE case
230069acd21dSWarner Losh 		 * removes the timeout for us.
230169acd21dSWarner Losh 		 */
23028177437dSPoul-Henning Kamp 		(void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
230369acd21dSWarner Losh 		fdc->state = IOCOMPLETE;
230469acd21dSWarner Losh 		/* FALLTHROUGH */
2305246ed35dSJoerg Wunsch 	case IOCOMPLETE: /* IO done, post-analyze */
23066182fdbdSPeter Wemm 		untimeout(fd_iotimeout, fdc, fd->tohandle);
2307dc5df763SJoerg Wunsch 
230864860614SJoerg Wunsch 		if (fd_read_status(fdc)) {
2309250300ebSJoerg Wunsch 			if (!rdsectid && !(fdc->flags & FDC_NODMA))
23108177437dSPoul-Henning Kamp 				isa_dmadone(idf, bp->bio_data + fd->skip,
23118177437dSPoul-Henning Kamp 					    format ? bp->bio_bcount : fdblk,
23125c1a1eaeSBruce Evans 					    fdc->dmachan);
2313dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
2314dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
23156182fdbdSPeter Wemm 			return (retrier(fdc));
23165b81b6b3SRodney W. Grimes   		}
2317dc5df763SJoerg Wunsch 
23183a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
2319dc5df763SJoerg Wunsch 
23203a2f7427SDavid Greenman 		/* FALLTHROUGH */
23213a2f7427SDavid Greenman 	case IOTIMEDOUT:
2322250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
23238177437dSPoul-Henning Kamp 			isa_dmadone(idf, bp->bio_data + fd->skip,
23248177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
23256182fdbdSPeter Wemm 		if (fdc->status[0] & NE7_ST0_IC) {
23263a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
23273a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
2328b39c878eSAndrey A. Chernov                                 /*
23293fef646eSJoerg Wunsch 				 * DMA overrun. Someone hogged the bus and
23303fef646eSJoerg Wunsch 				 * didn't release it in time for the next
23313fef646eSJoerg Wunsch 				 * FDC transfer.
23323fef646eSJoerg Wunsch 				 *
23333fef646eSJoerg Wunsch 				 * We normally restart this without bumping
23343fef646eSJoerg Wunsch 				 * the retry counter.  However, in case
23353fef646eSJoerg Wunsch 				 * something is seriously messed up (like
23363fef646eSJoerg Wunsch 				 * broken hardware), we rather limit the
23373fef646eSJoerg Wunsch 				 * number of retries so the IO operation
23383fef646eSJoerg Wunsch 				 * doesn't block indefinately.
2339b39c878eSAndrey A. Chernov 				 */
23403fef646eSJoerg Wunsch 				if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2341b39c878eSAndrey A. Chernov 					fdc->state = SEEKCOMPLETE;
2342246ed35dSJoerg Wunsch 					return (1);/* will return immediately */
23433fef646eSJoerg Wunsch 				} /* else fall through */
2344b39c878eSAndrey A. Chernov                         }
23453fef646eSJoerg Wunsch 			if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
23463a2f7427SDavid Greenman 				&& fdc->retry < 6)
23473a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
23483a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
23493a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
23503a2f7427SDavid Greenman 				&& fdc->retry < 3)
23513a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
23526182fdbdSPeter Wemm 			return (retrier(fdc));
23535b81b6b3SRodney W. Grimes 		}
23545b81b6b3SRodney W. Grimes 		/* All OK */
2355250300ebSJoerg Wunsch 		if (rdsectid) {
2356250300ebSJoerg Wunsch 			/* copy out ID field contents */
235764860614SJoerg Wunsch 			idp = (struct fdc_readid *)bp->bio_data;
2358250300ebSJoerg Wunsch 			idp->cyl = fdc->status[3];
2359250300ebSJoerg Wunsch 			idp->head = fdc->status[4];
2360250300ebSJoerg Wunsch 			idp->sec = fdc->status[5];
2361250300ebSJoerg Wunsch 			idp->secshift = fdc->status[6];
2362250300ebSJoerg Wunsch 		}
23633fef646eSJoerg Wunsch 		/* Operation successful, retry DMA overruns again next time. */
23643fef646eSJoerg Wunsch 		fdc->dma_overruns = 0;
23653a2f7427SDavid Greenman 		fd->skip += fdblk;
2366fb35bd37SJoerg Wunsch 		if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
23675b81b6b3SRodney W. Grimes 			/* set up next transfer */
23685b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
23696182fdbdSPeter Wemm 		} else {
23705b81b6b3SRodney W. Grimes 			/* ALL DONE */
23715b81b6b3SRodney W. Grimes 			fd->skip = 0;
2372fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2373e93e63cbSBruce Evans 			fdc->bp = NULL;
23745f830ea2SJoerg Wunsch 			device_unbusy(fd->dev);
2375a468031cSPoul-Henning Kamp 			biofinish(bp, &fd->device_stats, 0);
23765b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
23775b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
23785b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
23795b81b6b3SRodney W. Grimes 		}
2380246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2381fb35bd37SJoerg Wunsch 
23825b81b6b3SRodney W. Grimes 	case RESETCTLR:
23833a2f7427SDavid Greenman 		fdc_reset(fdc);
23845b81b6b3SRodney W. Grimes 		fdc->retry++;
23855c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
2386246ed35dSJoerg Wunsch 		return (0);	/* will return later */
2387fb35bd37SJoerg Wunsch 
23885c1a1eaeSBruce Evans 	case RESETCOMPLETE:
23895c1a1eaeSBruce Evans 		/*
23905c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
23915c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
23925c1a1eaeSBruce Evans 		 */
23930e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
23940e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
23955c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
2396fb35bd37SJoerg Wunsch 		/* FALLTHROUGH */
23975c1a1eaeSBruce Evans 	case STARTRECAL:
23986182fdbdSPeter Wemm 		if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2399dc8603e3SJoerg Wunsch 			/* arrgl */
2400dc8603e3SJoerg Wunsch 			fdc->retry = 6;
24016182fdbdSPeter Wemm 			return (retrier(fdc));
2402dc8603e3SJoerg Wunsch 		}
24035b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
24045b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2405fb35bd37SJoerg Wunsch 
24065b81b6b3SRodney W. Grimes 	case RECALWAIT:
24075b81b6b3SRodney W. Grimes 		/* allow heads to settle */
24086182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 8);
24095b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
24105b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2411fb35bd37SJoerg Wunsch 
24125b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
24133a2f7427SDavid Greenman 		do {
24143a2f7427SDavid Greenman 			/*
2415dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
24163a2f7427SDavid Greenman 			 */
2417dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2418246ed35dSJoerg Wunsch 				return (0); /* will return later */
2419dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
2420dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2421246ed35dSJoerg Wunsch 				return (0); /* hope for a real intr */
24223a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
24233a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
24245b81b6b3SRodney W. Grimes 		{
2425dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
2426dc8603e3SJoerg Wunsch 				/*
2427246ed35dSJoerg Wunsch 				 * A recalibrate from beyond cylinder 77
2428dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
2429dc8603e3SJoerg Wunsch 				 * since people used to complain much about
2430dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
2431dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
2432246ed35dSJoerg Wunsch 				 * time in a line.
2433dc8603e3SJoerg Wunsch 				 */
2434dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
2435dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
24363a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
24376182fdbdSPeter Wemm 			return (retrier(fdc));
24385b81b6b3SRodney W. Grimes 		}
24395b81b6b3SRodney W. Grimes 		fd->track = 0;
24405b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
24415b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
2442246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2443fb35bd37SJoerg Wunsch 
24445b81b6b3SRodney W. Grimes 	case MOTORWAIT:
24455b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
24465b81b6b3SRodney W. Grimes 		{
24475b81b6b3SRodney W. Grimes 			return (0); /* time's not up yet */
24485b81b6b3SRodney W. Grimes 		}
24495c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
24505c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
24515c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
2452fb35bd37SJoerg Wunsch 		} else
2453fb35bd37SJoerg Wunsch 			fdc->state = DOSEEK;
2454246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2455fb35bd37SJoerg Wunsch 
24565b81b6b3SRodney W. Grimes 	default:
2457b6e5f28eSPeter Wemm 		device_printf(fdc->fdc_dev, "unexpected FD int->");
245864860614SJoerg Wunsch 		if (fd_read_status(fdc) == 0)
2459a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
24605b81b6b3SRodney W. Grimes 			       fdc->status[0],
24615b81b6b3SRodney W. Grimes 			       fdc->status[1],
24625b81b6b3SRodney W. Grimes 			       fdc->status[2],
24635b81b6b3SRodney W. Grimes 			       fdc->status[3],
24645b81b6b3SRodney W. Grimes 			       fdc->status[4],
24655b81b6b3SRodney W. Grimes 			       fdc->status[5],
24665b81b6b3SRodney W. Grimes 			       fdc->status[6] );
24673a2f7427SDavid Greenman 		else
2468dac0f2dbSJoerg Wunsch 			printf("No status available   ");
2469dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
2470dac0f2dbSJoerg Wunsch 		{
2471dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
2472246ed35dSJoerg Wunsch 			return (0); /* will return later */
24735b81b6b3SRodney W. Grimes 		}
2474dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
2475246ed35dSJoerg Wunsch 		return (0);	/* will return later */
2476dac0f2dbSJoerg Wunsch 	}
2477246ed35dSJoerg Wunsch 	/* noone should ever get here */
24785b81b6b3SRodney W. Grimes }
24795b81b6b3SRodney W. Grimes 
2480aaf08d94SGarrett Wollman static int
24816182fdbdSPeter Wemm retrier(struct fdc_data *fdc)
24825b81b6b3SRodney W. Grimes {
24838177437dSPoul-Henning Kamp 	struct bio *bp;
24846182fdbdSPeter Wemm 	struct fd_data *fd;
24856182fdbdSPeter Wemm 	int fdu;
24865b81b6b3SRodney W. Grimes 
2487e93e63cbSBruce Evans 	bp = fdc->bp;
24885b81b6b3SRodney W. Grimes 
24896182fdbdSPeter Wemm 	/* XXX shouldn't this be cached somewhere?  */
24908177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
24916182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
24926182fdbdSPeter Wemm 	if (fd->options & FDOPT_NORETRY)
24933a2f7427SDavid Greenman 		goto fail;
24946182fdbdSPeter Wemm 
24956182fdbdSPeter Wemm 	switch (fdc->retry) {
24965b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
24975b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
24985b81b6b3SRodney W. Grimes 		break;
24995b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
25005b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
25015b81b6b3SRodney W. Grimes 		break;
25025b81b6b3SRodney W. Grimes 	case 6:
25035b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
25045b81b6b3SRodney W. Grimes 		break;
25055b81b6b3SRodney W. Grimes 	case 7:
25065b81b6b3SRodney W. Grimes 		break;
25075b81b6b3SRodney W. Grimes 	default:
25083a2f7427SDavid Greenman 	fail:
2509fb35bd37SJoerg Wunsch 		if ((fd->options & FDOPT_NOERRLOG) == 0) {
2510f90c382cSPoul-Henning Kamp 			disk_err(bp, "hard error",
2511f90c382cSPoul-Henning Kamp 			    fdc->fd->skip / DEV_BSIZE, 0);
2512fb35bd37SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID) {
2513dc5df763SJoerg Wunsch 				printf(
2514a838d83dSBruce Evans 				" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2515dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
2516dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
2517dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
2518dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
2519dc5df763SJoerg Wunsch 				       fdc->status[5]);
2520dc5df763SJoerg Wunsch 			}
2521dc5df763SJoerg Wunsch 			else
2522dc5df763SJoerg Wunsch 				printf(" (No status)\n");
25235b81b6b3SRodney W. Grimes 		}
25242995d110SJoerg Wunsch 		if ((fd->options & FDOPT_NOERROR) == 0) {
25258177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
25268177437dSPoul-Henning Kamp 			bp->bio_error = EIO;
2527fb35bd37SJoerg Wunsch 			bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
2528fb35bd37SJoerg Wunsch 		} else
2529fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2530e93e63cbSBruce Evans 		fdc->bp = NULL;
25315b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
25325f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
2533a468031cSPoul-Henning Kamp 		biofinish(bp, &fdc->fd->device_stats, 0);
253492ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
25355c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
25365b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
25375b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
253892ed385aSRodney W. Grimes 		return (1);
25395b81b6b3SRodney W. Grimes 	}
25405b81b6b3SRodney W. Grimes 	fdc->retry++;
25415b81b6b3SRodney W. Grimes 	return (1);
25425b81b6b3SRodney W. Grimes }
25435b81b6b3SRodney W. Grimes 
25441fdb6e6cSPoul-Henning Kamp static void
25451fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp)
25461fdb6e6cSPoul-Henning Kamp {
25471fdb6e6cSPoul-Henning Kamp 	wakeup(bp);
25481fdb6e6cSPoul-Henning Kamp }
25491fdb6e6cSPoul-Henning Kamp 
2550b39c878eSAndrey A. Chernov static int
2551f664aeeeSJoerg Wunsch fdmisccmd(dev_t dev, u_int cmd, void *data)
2552b39c878eSAndrey A. Chernov {
2553b39c878eSAndrey A. Chernov  	fdu_t fdu;
2554b39c878eSAndrey A. Chernov  	fd_p fd;
25551fdb6e6cSPoul-Henning Kamp 	struct bio *bp;
2556f664aeeeSJoerg Wunsch 	struct fd_formb *finfo;
2557f664aeeeSJoerg Wunsch 	struct fdc_readid *idfield;
25583a2f7427SDavid Greenman 	size_t fdblk;
255902aad38cSPoul-Henning Kamp 	int error;
2560b39c878eSAndrey A. Chernov 
2561b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
25626182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
25633a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2564f664aeeeSJoerg Wunsch 	finfo = (struct fd_formb *)data;
2565f664aeeeSJoerg Wunsch 	idfield = (struct fdc_readid *)data;
2566b39c878eSAndrey A. Chernov 
2567fb35bd37SJoerg Wunsch 	bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO);
2568b39c878eSAndrey A. Chernov 
2569b39c878eSAndrey A. Chernov 	/*
2570f664aeeeSJoerg Wunsch 	 * Set up a bio request for fdstrategy().  bio_blkno is faked
2571f664aeeeSJoerg Wunsch 	 * so that fdstrategy() will seek to the the requested
2572817988beSPoul-Henning Kamp 	 * cylinder, and use the desired head.
2573b39c878eSAndrey A. Chernov 	 */
2574f664aeeeSJoerg Wunsch 	bp->bio_cmd = cmd;
2575419f39ceSPoul-Henning Kamp 	if (cmd == FDBIO_FORMAT) {
2576f664aeeeSJoerg Wunsch 		bp->bio_blkno =
2577f664aeeeSJoerg Wunsch 		    (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
2578f664aeeeSJoerg Wunsch 		     finfo->head * fd->ft->sectrac) *
2579f664aeeeSJoerg Wunsch 		    fdblk / DEV_BSIZE;
2580f664aeeeSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fd_idfield_data) *
2581f664aeeeSJoerg Wunsch 		    finfo->fd_formb_nsecs;
2582419f39ceSPoul-Henning Kamp 	} else if (cmd == FDBIO_RDSECTID) {
2583f664aeeeSJoerg Wunsch 		bp->bio_blkno =
2584f664aeeeSJoerg Wunsch 		    (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
2585f664aeeeSJoerg Wunsch 		     idfield->head * fd->ft->sectrac) *
2586f664aeeeSJoerg Wunsch 		    fdblk / DEV_BSIZE;
2587250300ebSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fdc_readid);
2588f664aeeeSJoerg Wunsch 	} else
2589f664aeeeSJoerg Wunsch 		panic("wrong cmd in fdmisccmd()");
2590f664aeeeSJoerg Wunsch 	bp->bio_data = data;
2591250300ebSJoerg Wunsch 	bp->bio_dev = dev;
2592250300ebSJoerg Wunsch 	bp->bio_done = fdbiodone;
2593817988beSPoul-Henning Kamp 	bp->bio_flags = 0;
2594250300ebSJoerg Wunsch 
2595c3bdb2f7SPoul-Henning Kamp 	/* Now run the command. */
2596f664aeeeSJoerg Wunsch 	fdstrategy(bp);
259702aad38cSPoul-Henning Kamp 	error = biowait(bp, "fdcmd");
2598c3bdb2f7SPoul-Henning Kamp 
2599f664aeeeSJoerg Wunsch 	free(bp, M_TEMP);
260002aad38cSPoul-Henning Kamp 	return (error);
2601f664aeeeSJoerg Wunsch }
26025b81b6b3SRodney W. Grimes 
26033e425b96SJulian Elischer static int
2604b40ce416SJulian Elischer fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
2605f5f7ba03SJordan K. Hubbard {
2606f664aeeeSJoerg Wunsch  	fdu_t fdu;
2607f664aeeeSJoerg Wunsch  	fd_p fd;
26082995d110SJoerg Wunsch 	struct fdc_status *fsp;
2609250300ebSJoerg Wunsch 	struct fdc_readid *rid;
2610f664aeeeSJoerg Wunsch 	size_t fdblk;
26111a6bed68SJoerg Wunsch 	int error, type;
2612f5f7ba03SJordan K. Hubbard 
2613f664aeeeSJoerg Wunsch  	fdu = FDUNIT(minor(dev));
26141a6bed68SJoerg Wunsch 	type = FDTYPE(minor(dev));
2615f664aeeeSJoerg Wunsch  	fd = devclass_get_softc(fd_devclass, fdu);
2616fb35bd37SJoerg Wunsch 
26171a6bed68SJoerg Wunsch 	/*
26181a6bed68SJoerg Wunsch 	 * First, handle everything that could be done with
26191a6bed68SJoerg Wunsch 	 * FD_NONBLOCK still being set.
26201a6bed68SJoerg Wunsch 	 */
26211a6bed68SJoerg Wunsch 	switch (cmd) {
2622c91a63aaSPoul-Henning Kamp 
2623c91a63aaSPoul-Henning Kamp 	case DIOCGMEDIASIZE:
2624c91a63aaSPoul-Henning Kamp 		*(off_t *)addr = (128 << (fd->ft->secsize)) * fd->ft->size;
2625c91a63aaSPoul-Henning Kamp 		return (0);
2626c91a63aaSPoul-Henning Kamp 
2627c91a63aaSPoul-Henning Kamp 	case DIOCGSECTORSIZE:
2628c91a63aaSPoul-Henning Kamp 		*(u_int *)addr = 128 << (fd->ft->secsize);
2629c91a63aaSPoul-Henning Kamp 		return (0);
2630c91a63aaSPoul-Henning Kamp 
26311a6bed68SJoerg Wunsch 	case FIONBIO:
26321a6bed68SJoerg Wunsch 		if (*(int *)addr != 0)
26331a6bed68SJoerg Wunsch 			fd->flags |= FD_NONBLOCK;
26341a6bed68SJoerg Wunsch 		else {
26351a6bed68SJoerg Wunsch 			if (fd->ft == 0) {
26361a6bed68SJoerg Wunsch 				/*
26371a6bed68SJoerg Wunsch 				 * No drive type has been selected yet,
26381a6bed68SJoerg Wunsch 				 * cannot turn FNONBLOCK off.
26391a6bed68SJoerg Wunsch 				 */
26401a6bed68SJoerg Wunsch 				return (EINVAL);
26411a6bed68SJoerg Wunsch 			}
26421a6bed68SJoerg Wunsch 			fd->flags &= ~FD_NONBLOCK;
26431a6bed68SJoerg Wunsch 		}
26441a6bed68SJoerg Wunsch 		return (0);
2645fb35bd37SJoerg Wunsch 
26461a6bed68SJoerg Wunsch 	case FIOASYNC:
26471a6bed68SJoerg Wunsch 		/* keep the generic fcntl() code happy */
26481a6bed68SJoerg Wunsch 		return (0);
26491a6bed68SJoerg Wunsch 
26501a6bed68SJoerg Wunsch 	case FD_GTYPE:                  /* get drive type */
26511a6bed68SJoerg Wunsch 		if (fd->ft == 0)
26521a6bed68SJoerg Wunsch 			/* no type known yet, return the native type */
26531a6bed68SJoerg Wunsch 			*(struct fd_type *)addr = fd_native_types[fd->type];
26541a6bed68SJoerg Wunsch 		else
26551a6bed68SJoerg Wunsch 			*(struct fd_type *)addr = *fd->ft;
26561a6bed68SJoerg Wunsch 		return (0);
26571a6bed68SJoerg Wunsch 
26581a6bed68SJoerg Wunsch 	case FD_STYPE:                  /* set drive type */
26591a6bed68SJoerg Wunsch 		if (type == 0) {
26601a6bed68SJoerg Wunsch 			/*
26611a6bed68SJoerg Wunsch 			 * Allow setting drive type temporarily iff
26621a6bed68SJoerg Wunsch 			 * currently unset.  Used for fdformat so any
26631a6bed68SJoerg Wunsch 			 * user can set it, and then start formatting.
26641a6bed68SJoerg Wunsch 			 */
26651a6bed68SJoerg Wunsch 			if (fd->ft)
26661a6bed68SJoerg Wunsch 				return (EINVAL); /* already set */
26671a6bed68SJoerg Wunsch 			fd->ft = fd->fts;
26681a6bed68SJoerg Wunsch 			*fd->ft = *(struct fd_type *)addr;
26691a6bed68SJoerg Wunsch 			fd->flags |= FD_UA;
26701a6bed68SJoerg Wunsch 		} else {
26711a6bed68SJoerg Wunsch 			/*
26721a6bed68SJoerg Wunsch 			 * Set density definition permanently.  Only
26731a6bed68SJoerg Wunsch 			 * allow for superuser.
26741a6bed68SJoerg Wunsch 			 */
267544731cabSJohn Baldwin 			if (suser(td) != 0)
26761a6bed68SJoerg Wunsch 				return (EPERM);
26771a6bed68SJoerg Wunsch 			fd->fts[type] = *(struct fd_type *)addr;
26781a6bed68SJoerg Wunsch 		}
26791a6bed68SJoerg Wunsch 		return (0);
26801a6bed68SJoerg Wunsch 
26811a6bed68SJoerg Wunsch 	case FD_GOPTS:			/* get drive options */
26821a6bed68SJoerg Wunsch 		*(int *)addr = fd->options + (type == 0? FDOPT_AUTOSEL: 0);
26831a6bed68SJoerg Wunsch 		return (0);
26841a6bed68SJoerg Wunsch 
26851a6bed68SJoerg Wunsch 	case FD_SOPTS:			/* set drive options */
26861a6bed68SJoerg Wunsch 		fd->options = *(int *)addr & ~FDOPT_AUTOSEL;
26871a6bed68SJoerg Wunsch 		return (0);
26881a6bed68SJoerg Wunsch 
26891a6bed68SJoerg Wunsch #ifdef FDC_DEBUG
26901a6bed68SJoerg Wunsch 	case FD_DEBUG:
26911a6bed68SJoerg Wunsch 		if ((fd_debug != 0) != (*(int *)addr != 0)) {
26921a6bed68SJoerg Wunsch 			fd_debug = (*(int *)addr != 0);
26931a6bed68SJoerg Wunsch 			printf("fd%d: debugging turned %s\n",
26941a6bed68SJoerg Wunsch 			    fd->fdu, fd_debug ? "on" : "off");
26951a6bed68SJoerg Wunsch 		}
26961a6bed68SJoerg Wunsch 		return (0);
26971a6bed68SJoerg Wunsch #endif
26981a6bed68SJoerg Wunsch 
26991a6bed68SJoerg Wunsch 	case FD_CLRERR:
270044731cabSJohn Baldwin 		if (suser(td) != 0)
27011a6bed68SJoerg Wunsch 			return (EPERM);
27021a6bed68SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
27031a6bed68SJoerg Wunsch 		return (0);
27041a6bed68SJoerg Wunsch 
27051a6bed68SJoerg Wunsch 	case FD_GSTAT:
27061a6bed68SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
27071a6bed68SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
27081a6bed68SJoerg Wunsch 			return (EINVAL);
27091a6bed68SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
27101a6bed68SJoerg Wunsch 		return (0);
27111a6bed68SJoerg Wunsch 
27121a6bed68SJoerg Wunsch 	case FD_GDTYPE:
27131a6bed68SJoerg Wunsch 		*(enum fd_drivetype *)addr = fd->type;
27141a6bed68SJoerg Wunsch 		return (0);
27151a6bed68SJoerg Wunsch 	}
27161a6bed68SJoerg Wunsch 
27171a6bed68SJoerg Wunsch 	/*
27181a6bed68SJoerg Wunsch 	 * Now handle everything else.  Make sure we have a valid
27191a6bed68SJoerg Wunsch 	 * drive type.
27201a6bed68SJoerg Wunsch 	 */
27211a6bed68SJoerg Wunsch 	if (fd->flags & FD_NONBLOCK)
27221a6bed68SJoerg Wunsch 		return (EAGAIN);
27231a6bed68SJoerg Wunsch 	if (fd->ft == 0)
27241a6bed68SJoerg Wunsch 		return (ENXIO);
27253a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2726fb35bd37SJoerg Wunsch 	error = 0;
2727f5f7ba03SJordan K. Hubbard 
27286182fdbdSPeter Wemm 	switch (cmd) {
2729f664aeeeSJoerg Wunsch 
2730b39c878eSAndrey A. Chernov 	case FD_FORM:
2731b39c878eSAndrey A. Chernov 		if ((flag & FWRITE) == 0)
2732fb35bd37SJoerg Wunsch 			return (EBADF);	/* must be opened for writing */
2733fb35bd37SJoerg Wunsch 		if (((struct fd_formb *)addr)->format_version !=
2734b39c878eSAndrey A. Chernov 		    FD_FORMAT_VERSION)
2735fb35bd37SJoerg Wunsch 			return (EINVAL); /* wrong version of formatting prog */
2736419f39ceSPoul-Henning Kamp 		error = fdmisccmd(dev, FDBIO_FORMAT, addr);
2737b39c878eSAndrey A. Chernov 		break;
2738b39c878eSAndrey A. Chernov 
2739b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
27403e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2741f5f7ba03SJordan K. Hubbard 		break;
2742f5f7ba03SJordan K. Hubbard 
27433a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
27443a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
274544731cabSJohn Baldwin 		if (suser(td) != 0)
2746fb35bd37SJoerg Wunsch 			return (EPERM);
27473e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
27483a2f7427SDavid Greenman 		break;
27493a2f7427SDavid Greenman 
27503a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
27513e425b96SJulian Elischer 		*(int *)addr = fd->options;
27523a2f7427SDavid Greenman 		break;
27533a2f7427SDavid Greenman 
27543a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
27553e425b96SJulian Elischer 		fd->options = *(int *)addr;
27563a2f7427SDavid Greenman 		break;
27573a2f7427SDavid Greenman 
2758f664aeeeSJoerg Wunsch #ifdef FDC_DEBUG
2759f664aeeeSJoerg Wunsch 	case FD_DEBUG:
27600e17a5bcSJoerg Wunsch 		if ((fd_debug != 0) != (*(int *)addr != 0)) {
27610e17a5bcSJoerg Wunsch 			fd_debug = (*(int *)addr != 0);
27620e17a5bcSJoerg Wunsch 			printf("fd%d: debugging turned %s\n",
27630e17a5bcSJoerg Wunsch 			    fd->fdu, fd_debug ? "on" : "off");
27640e17a5bcSJoerg Wunsch 		}
2765f664aeeeSJoerg Wunsch 		break;
2766f664aeeeSJoerg Wunsch #endif
2767f664aeeeSJoerg Wunsch 
27682995d110SJoerg Wunsch 	case FD_CLRERR:
276944731cabSJohn Baldwin 		if (suser(td) != 0)
2770fb35bd37SJoerg Wunsch 			return (EPERM);
27712995d110SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
27722995d110SJoerg Wunsch 		break;
27732995d110SJoerg Wunsch 
27742995d110SJoerg Wunsch 	case FD_GSTAT:
27752995d110SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
27762995d110SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2777fb35bd37SJoerg Wunsch 			return (EINVAL);
27782995d110SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
27792995d110SJoerg Wunsch 		break;
27802995d110SJoerg Wunsch 
2781250300ebSJoerg Wunsch 	case FD_READID:
2782250300ebSJoerg Wunsch 		rid = (struct fdc_readid *)addr;
2783250300ebSJoerg Wunsch 		if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2784fb35bd37SJoerg Wunsch 			return (EINVAL);
2785419f39ceSPoul-Henning Kamp 		error = fdmisccmd(dev, FDBIO_RDSECTID, addr);
2786250300ebSJoerg Wunsch 		break;
2787250300ebSJoerg Wunsch 
2788f5f7ba03SJordan K. Hubbard 	default:
27893a2f7427SDavid Greenman 		error = ENOTTY;
2790f5f7ba03SJordan K. Hubbard 		break;
2791f5f7ba03SJordan K. Hubbard 	}
2792f5f7ba03SJordan K. Hubbard 	return (error);
2793f5f7ba03SJordan K. Hubbard }
2794