xref: /freebsd/sys/dev/fdc/fdc.c (revision 1a6bed6863058737edd4f0e3cadcfb61c4032b80)
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>
65fb919e4dSMark Murray #include <sys/disklabel.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;
1493a5c2c86SJoerg Wunsch #ifndef PC98
150246ed35dSJoerg Wunsch 	int	fdout;		/* mirror of the w/o digital output reg */
1513a5c2c86SJoerg Wunsch #endif
152246ed35dSJoerg Wunsch 	u_int	status[7];	/* copy of the registers */
153246ed35dSJoerg Wunsch 	enum	fdc_type fdct;	/* chip version of FDC */
154246ed35dSJoerg Wunsch 	int	fdc_errs;	/* number of logged errors */
155246ed35dSJoerg Wunsch 	int	dma_overruns;	/* number of DMA overruns */
156246ed35dSJoerg Wunsch 	struct	bio_queue_head head;
157246ed35dSJoerg Wunsch 	struct	bio *bp;	/* active buffer */
1583a5c2c86SJoerg Wunsch #ifdef PC98
1593a5c2c86SJoerg Wunsch 	struct	resource *res_ioport, *res_fdsio, *res_fdemsio;
1603a5c2c86SJoerg Wunsch 	struct	resource *res_irq, *res_drq;
1613a5c2c86SJoerg Wunsch 	int	rid_ioport, rid_irq, rid_drq;
1623a5c2c86SJoerg Wunsch #else
163246ed35dSJoerg Wunsch 	struct	resource *res_ioport, *res_ctl, *res_irq, *res_drq;
164246ed35dSJoerg Wunsch 	int	rid_ioport, rid_ctl, rid_irq, rid_drq;
1653a5c2c86SJoerg Wunsch #endif
166246ed35dSJoerg Wunsch 	int	port_off;
167246ed35dSJoerg Wunsch 	bus_space_tag_t portt;
168246ed35dSJoerg Wunsch 	bus_space_handle_t porth;
1693a5c2c86SJoerg Wunsch #ifdef PC98
1703a5c2c86SJoerg Wunsch         bus_space_tag_t		sc_fdsiot;
1713a5c2c86SJoerg Wunsch         bus_space_handle_t	sc_fdsioh;
1723a5c2c86SJoerg Wunsch         bus_space_tag_t		sc_fdemsiot;
1733a5c2c86SJoerg Wunsch         bus_space_handle_t	sc_fdemsioh;
1743a5c2c86SJoerg Wunsch #else
175246ed35dSJoerg Wunsch 	bus_space_tag_t ctlt;
176246ed35dSJoerg Wunsch 	bus_space_handle_t ctlh;
1773a5c2c86SJoerg Wunsch #endif
178246ed35dSJoerg Wunsch 	void	*fdc_intr;
179246ed35dSJoerg Wunsch 	struct	device *fdc_dev;
1803a5c2c86SJoerg Wunsch #ifndef PC98
181246ed35dSJoerg Wunsch 	void	(*fdctl_wr)(struct fdc_data *fdc, u_int8_t v);
1823a5c2c86SJoerg Wunsch #endif
183246ed35dSJoerg Wunsch };
184246ed35dSJoerg Wunsch 
185246ed35dSJoerg Wunsch typedef int	fdu_t;
186246ed35dSJoerg Wunsch typedef int	fdcu_t;
187246ed35dSJoerg Wunsch typedef int	fdsu_t;
188246ed35dSJoerg Wunsch typedef	struct fd_data *fd_p;
189246ed35dSJoerg Wunsch typedef struct fdc_data *fdc_p;
190246ed35dSJoerg Wunsch typedef enum fdc_type fdc_t;
191246ed35dSJoerg Wunsch 
192246ed35dSJoerg Wunsch #define FDUNIT(s)	(((s) >> 6) & 3)
1931a6bed68SJoerg Wunsch #define FDNUMTOUNIT(n)	(((n) & 3) << 6)
194246ed35dSJoerg Wunsch #define FDTYPE(s)	((s) & 0x3f)
195246ed35dSJoerg Wunsch 
196246ed35dSJoerg Wunsch /*
197246ed35dSJoerg Wunsch  * fdc maintains a set (1!) of ivars per child of each controller.
198246ed35dSJoerg Wunsch  */
199246ed35dSJoerg Wunsch enum fdc_device_ivars {
200246ed35dSJoerg Wunsch 	FDC_IVAR_FDUNIT,
201246ed35dSJoerg Wunsch };
202246ed35dSJoerg Wunsch 
203246ed35dSJoerg Wunsch /*
204246ed35dSJoerg Wunsch  * Simple access macros for the ivars.
205246ed35dSJoerg Wunsch  */
206246ed35dSJoerg Wunsch #define FDC_ACCESSOR(A, B, T)						\
207246ed35dSJoerg Wunsch static __inline T fdc_get_ ## A(device_t dev)				\
208246ed35dSJoerg Wunsch {									\
209246ed35dSJoerg Wunsch 	uintptr_t v;							\
210246ed35dSJoerg Wunsch 	BUS_READ_IVAR(device_get_parent(dev), dev, FDC_IVAR_ ## B, &v);	\
211246ed35dSJoerg Wunsch 	return (T) v;							\
212246ed35dSJoerg Wunsch }
213246ed35dSJoerg Wunsch FDC_ACCESSOR(fdunit,	FDUNIT,	int)
214246ed35dSJoerg Wunsch 
2151a6bed68SJoerg Wunsch /* configuration flags for fdc */
216e34c71eaSJoerg Wunsch #define FDC_NO_FIFO	(1 << 2)	/* do not enable FIFO  */
2170722d6abSJoerg Wunsch 
218dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
219dc5df763SJoerg Wunsch #define FD_FAILED -1
220dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
221dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
2223fef646eSJoerg Wunsch /*
2233fef646eSJoerg Wunsch  * Stop retrying after this many DMA overruns.  Since each retry takes
22483edbfa5SJoerg Wunsch  * one revolution, with 300 rpm., 25 retries take approximately 5
2253fef646eSJoerg Wunsch  * seconds which the read attempt will block in case the DMA overrun
2263fef646eSJoerg Wunsch  * is persistent.
2273fef646eSJoerg Wunsch  */
2283fef646eSJoerg Wunsch #define FDC_DMAOV_MAX	25
229dc5df763SJoerg Wunsch 
2301a6bed68SJoerg Wunsch /*
2311a6bed68SJoerg Wunsch  * Number of subdevices that can be used for different density types.
2321a6bed68SJoerg Wunsch  * By now, the lower 6 bit of the minor number are reserved for this,
2331a6bed68SJoerg Wunsch  * allowing for up to 64 subdevices, but we only use 16 out of this.
2341a6bed68SJoerg Wunsch  * Density #0 is used for automatic format detection, the other
2351a6bed68SJoerg Wunsch  * densities are available as programmable densities (for assignment
2361a6bed68SJoerg Wunsch  * by fdcontrol(8)).
2371a6bed68SJoerg Wunsch  * The upper 2 bits of the minor number are reserved for the subunit
2381a6bed68SJoerg Wunsch  * (drive #) per controller.
2391a6bed68SJoerg Wunsch  */
2401a6bed68SJoerg Wunsch #define NUMDENS		16
241b99f0a4aSAndrew Moore 
242f664aeeeSJoerg Wunsch #define BIO_RDSECTID	BIO_CMD1
2437ca0641bSAndrey A. Chernov 
2441a6bed68SJoerg Wunsch /*
2451a6bed68SJoerg Wunsch  * List of native drive densities.  Order must match enum fd_drivetype
2461a6bed68SJoerg Wunsch  * in <sys/fdcio.h>.  Upon attaching the drive, each of the
2471a6bed68SJoerg Wunsch  * programmable subdevices is initialized with the native density
2481a6bed68SJoerg Wunsch  * definition.
2491a6bed68SJoerg Wunsch  */
2501a6bed68SJoerg Wunsch static struct fd_type fd_native_types[] =
2515b81b6b3SRodney W. Grimes {
2521a6bed68SJoerg Wunsch { 0 },				/* FDT_NONE */
2531a6bed68SJoerg Wunsch {  9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */
2541a6bed68SJoerg Wunsch { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M  */
2551a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */
2561a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
2571a6bed68SJoerg Wunsch #if 0				/* we currently don't handle 2.88 MB */
2581a6bed68SJoerg Wunsch { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS,  2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/
2591a6bed68SJoerg Wunsch #else
2601a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
2611a6bed68SJoerg Wunsch #endif
2625b81b6b3SRodney W. Grimes };
2635b81b6b3SRodney W. Grimes 
2641a6bed68SJoerg Wunsch /*
2651a6bed68SJoerg Wunsch  * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density
2661a6bed68SJoerg Wunsch  * selection, they just start out with their native density (or lose).
2671a6bed68SJoerg Wunsch  * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their
2681a6bed68SJoerg Wunsch  * respective lists of densities to search for.
2691a6bed68SJoerg Wunsch  */
2701a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_12m[] = {
2711a6bed68SJoerg Wunsch { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */
2721a6bed68SJoerg Wunsch {  9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */
2731a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2741a6bed68SJoerg Wunsch };
2751a6bed68SJoerg Wunsch 
2761a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_144m[] = {
2771a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
2781a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2791a6bed68SJoerg Wunsch };
2801a6bed68SJoerg Wunsch 
2811a6bed68SJoerg Wunsch /* We search for 1.44M first since this is the most common case. */
2821a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_288m[] = {
2831a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
2841a6bed68SJoerg Wunsch #if 0
2851a6bed68SJoerg Wunsch { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS,  2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */
2861a6bed68SJoerg Wunsch #endif
2871a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2881a6bed68SJoerg Wunsch };
289dc16046fSJoerg Wunsch 
290f8ce7dd5SJoerg Wunsch #define MAX_SEC_SIZE	(128 << 3)
29164860614SJoerg Wunsch #define MAX_CYLINDER	85	/* some people really stress their drives
29264860614SJoerg Wunsch 				 * up to cyl 82 */
293250300ebSJoerg Wunsch #define MAX_HEAD	1
294f8ce7dd5SJoerg Wunsch 
2956182fdbdSPeter Wemm static devclass_t fdc_devclass;
2965b81b6b3SRodney W. Grimes 
297246ed35dSJoerg Wunsch /*
298246ed35dSJoerg Wunsch  * Per drive structure (softc).
299246ed35dSJoerg Wunsch  */
3006182fdbdSPeter Wemm struct fd_data {
301b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
3025b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
3031a6bed68SJoerg Wunsch 	enum	fd_drivetype type; /* drive type */
3041a6bed68SJoerg Wunsch 	struct	fd_type *ft;	/* pointer to current type descriptor */
3051a6bed68SJoerg Wunsch 	struct	fd_type fts[NUMDENS]; /* type descriptors */
3065b81b6b3SRodney W. Grimes 	int	flags;
3075b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
3081a6bed68SJoerg Wunsch #define	FD_NONBLOCK	0x02	/* O_NONBLOCK set	*/
3091a6bed68SJoerg Wunsch #define	FD_ACTIVE	0x04	/* it's active		*/
3101a6bed68SJoerg Wunsch #define	FD_MOTOR	0x08	/* motor should be on	*/
3111a6bed68SJoerg Wunsch #define	FD_MOTOR_WAIT	0x10	/* motor coming up	*/
3121a6bed68SJoerg Wunsch #define	FD_UA		0x20	/* force unit attention */
3135b81b6b3SRodney W. Grimes 	int	skip;
3145b81b6b3SRodney W. Grimes 	int	hddrv;
315dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
3165b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
3171a6bed68SJoerg Wunsch 	int	options;	/* user configurable options, see fdcio.h */
31802a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
31902a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
320b2dfb1f9SJustin T. Gibbs 	struct	devstat device_stats;
321e219897aSJoerg Wunsch 	eventhandler_tag clonetag;
322e219897aSJoerg Wunsch 	dev_t	masterdev;
3231a6bed68SJoerg Wunsch 	dev_t	clonedevs[NUMDENS - 1];
3246182fdbdSPeter Wemm 	device_t dev;
3256182fdbdSPeter Wemm 	fdu_t	fdu;
3266182fdbdSPeter Wemm };
32737286586SPeter Wemm 
32837286586SPeter Wemm struct fdc_ivars {
32937286586SPeter Wemm 	int	fdunit;
33037286586SPeter Wemm };
3316182fdbdSPeter Wemm static devclass_t fd_devclass;
3325b81b6b3SRodney W. Grimes 
3331a6bed68SJoerg Wunsch /* configuration flags for fd */
3341a6bed68SJoerg Wunsch #define FD_TYPEMASK	0x0f	/* drive type, matches enum
3351a6bed68SJoerg Wunsch 				 * fd_drivetype; on i386 machines, if
3361a6bed68SJoerg Wunsch 				 * given as 0, use RTC type for fd0
3371a6bed68SJoerg Wunsch 				 * and fd1 */
3381a6bed68SJoerg Wunsch #define FD_DTYPE(flags)	((flags) & FD_TYPEMASK)
3391a6bed68SJoerg Wunsch #define FD_NO_CHLINE	0x10	/* drive does not support changeline
3401a6bed68SJoerg Wunsch 				 * aka. unit attention */
3411a6bed68SJoerg Wunsch #define FD_NO_PROBE	0x20	/* don't probe drive (seek test), just
3421a6bed68SJoerg Wunsch 				 * assume it is there */
3431a6bed68SJoerg Wunsch 
344246ed35dSJoerg Wunsch /*
345246ed35dSJoerg Wunsch  * Throughout this file the following conventions will be used:
346246ed35dSJoerg Wunsch  *
347246ed35dSJoerg Wunsch  * fd is a pointer to the fd_data struct for the drive in question
348246ed35dSJoerg Wunsch  * fdc is a pointer to the fdc_data struct for the controller
349246ed35dSJoerg Wunsch  * fdu is the floppy drive unit number
350246ed35dSJoerg Wunsch  * fdcu is the floppy controller unit number
351246ed35dSJoerg Wunsch  * fdsu is the floppy drive unit number on that controller. (sub-unit)
352246ed35dSJoerg Wunsch  */
353b99f0a4aSAndrew Moore 
354246ed35dSJoerg Wunsch /*
355246ed35dSJoerg Wunsch  * Function declarations, same (chaotic) order as they appear in the
356246ed35dSJoerg Wunsch  * file.  Re-ordering is too late now, it would only obfuscate the
357246ed35dSJoerg Wunsch  * diffs against old and offspring versions (like the PC98 one).
358246ed35dSJoerg Wunsch  *
359246ed35dSJoerg Wunsch  * Anyone adding functions here, please keep this sequence the same
360246ed35dSJoerg Wunsch  * as below -- makes locating a particular function in the body much
361246ed35dSJoerg Wunsch  * easier.
362246ed35dSJoerg Wunsch  */
363246ed35dSJoerg Wunsch static void fdout_wr(fdc_p, u_int8_t);
364246ed35dSJoerg Wunsch static u_int8_t fdsts_rd(fdc_p);
365246ed35dSJoerg Wunsch static void fddata_wr(fdc_p, u_int8_t);
366246ed35dSJoerg Wunsch static u_int8_t fddata_rd(fdc_p);
367246ed35dSJoerg Wunsch static void fdctl_wr_isa(fdc_p, u_int8_t);
368246ed35dSJoerg Wunsch #if NCARD > 0
369246ed35dSJoerg Wunsch static void fdctl_wr_pcmcia(fdc_p, u_int8_t);
370246ed35dSJoerg Wunsch #endif
371246ed35dSJoerg Wunsch #if 0
372246ed35dSJoerg Wunsch static u_int8_t fdin_rd(fdc_p);
373246ed35dSJoerg Wunsch #endif
374246ed35dSJoerg Wunsch static int fdc_err(struct fdc_data *, const char *);
375246ed35dSJoerg Wunsch static int fd_cmd(struct fdc_data *, int, ...);
376246ed35dSJoerg Wunsch static int enable_fifo(fdc_p fdc);
377246ed35dSJoerg Wunsch static int fd_sense_drive_status(fdc_p, int *);
378246ed35dSJoerg Wunsch static int fd_sense_int(fdc_p, int *, int *);
379246ed35dSJoerg Wunsch static int fd_read_status(fdc_p);
380246ed35dSJoerg Wunsch static int fdc_alloc_resources(struct fdc_data *);
381246ed35dSJoerg Wunsch static void fdc_release_resources(struct fdc_data *);
382246ed35dSJoerg Wunsch static int fdc_read_ivar(device_t, device_t, int, uintptr_t *);
383246ed35dSJoerg Wunsch static int fdc_probe(device_t);
384246ed35dSJoerg Wunsch #if NCARD > 0
385246ed35dSJoerg Wunsch static int fdc_pccard_probe(device_t);
386246ed35dSJoerg Wunsch #endif
387246ed35dSJoerg Wunsch static int fdc_detach(device_t dev);
388246ed35dSJoerg Wunsch static void fdc_add_child(device_t, const char *, int);
389246ed35dSJoerg Wunsch static int fdc_attach(device_t);
390246ed35dSJoerg Wunsch static int fdc_print_child(device_t, device_t);
391246ed35dSJoerg Wunsch static void fd_clone (void *, char *, int, dev_t *);
392246ed35dSJoerg Wunsch static int fd_probe(device_t);
393246ed35dSJoerg Wunsch static int fd_attach(device_t);
394246ed35dSJoerg Wunsch static int fd_detach(device_t);
3956182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int);
3963a2f7427SDavid Greenman #  define TURNON 1
3973a2f7427SDavid Greenman #  define TURNOFF 0
3983a2f7427SDavid Greenman static timeout_t fd_turnoff;
3993a2f7427SDavid Greenman static timeout_t fd_motor_on;
4006182fdbdSPeter Wemm static void fd_turnon(struct fd_data *);
4013a2f7427SDavid Greenman static void fdc_reset(fdc_p);
4026182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *);
40380909a7dSJoerg Wunsch static int out_fdc(struct fdc_data *, int);
404246ed35dSJoerg Wunsch /*
405246ed35dSJoerg Wunsch  * The open function is named Fdopen() to avoid confusion with fdopen()
406246ed35dSJoerg Wunsch  * in fd(4).  The difference is now only meaningful for debuggers.
407246ed35dSJoerg Wunsch  */
408246ed35dSJoerg Wunsch static	d_open_t	Fdopen;
409246ed35dSJoerg Wunsch static	d_close_t	fdclose;
410246ed35dSJoerg Wunsch static	d_strategy_t	fdstrategy;
4116182fdbdSPeter Wemm static void fdstart(struct fdc_data *);
4125c1a1eaeSBruce Evans static timeout_t fd_iotimeout;
4133a2f7427SDavid Greenman static timeout_t fd_pseudointr;
414246ed35dSJoerg Wunsch static driver_intr_t fdc_intr;
415246ed35dSJoerg Wunsch static int fdcpio(fdc_p, long, caddr_t, u_int);
4161a6bed68SJoerg Wunsch static int fdautoselect(dev_t);
4176182fdbdSPeter Wemm static int fdstate(struct fdc_data *);
4186182fdbdSPeter Wemm static int retrier(struct fdc_data *);
419246ed35dSJoerg Wunsch static void fdbiodone(struct bio *);
420f664aeeeSJoerg Wunsch static int fdmisccmd(dev_t, u_int, void *);
421246ed35dSJoerg Wunsch static	d_ioctl_t	fdioctl;
422d66c374fSTor Egge 
423d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
424d66c374fSTor Egge 
425d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
4263a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
427cba2a7c6SBruce Evans static int volatile fd_debug = 0;
4280e17a5bcSJoerg Wunsch #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0)
4290e17a5bcSJoerg Wunsch #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0)
430d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
4310e17a5bcSJoerg Wunsch #define TRACE0(arg) do { } while (0)
4320e17a5bcSJoerg Wunsch #define TRACE1(arg1, arg2) do { } while (0)
433d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
4345b81b6b3SRodney W. Grimes 
435246ed35dSJoerg Wunsch /*
436246ed35dSJoerg Wunsch  * Bus space handling (access to low-level IO).
437246ed35dSJoerg Wunsch  */
438427ccf00SDoug Rabson static void
439427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v)
440427ccf00SDoug Rabson {
441427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
442427ccf00SDoug Rabson }
443427ccf00SDoug Rabson 
444427ccf00SDoug Rabson static u_int8_t
445427ccf00SDoug Rabson fdsts_rd(fdc_p fdc)
446427ccf00SDoug Rabson {
447427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
448427ccf00SDoug Rabson }
449427ccf00SDoug Rabson 
450427ccf00SDoug Rabson static void
451427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v)
452427ccf00SDoug Rabson {
453427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
454427ccf00SDoug Rabson }
455427ccf00SDoug Rabson 
456427ccf00SDoug Rabson static u_int8_t
457427ccf00SDoug Rabson fddata_rd(fdc_p fdc)
458427ccf00SDoug Rabson {
459427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
460427ccf00SDoug Rabson }
461427ccf00SDoug Rabson 
462427ccf00SDoug Rabson static void
4635f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v)
464427ccf00SDoug Rabson {
465427ccf00SDoug Rabson 	bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
466427ccf00SDoug Rabson }
467427ccf00SDoug Rabson 
46858c9d623SPeter Wemm #if NCARD > 0
4695f830ea2SJoerg Wunsch static void
4705f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
4715f830ea2SJoerg Wunsch {
4725f830ea2SJoerg Wunsch 	bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
4735f830ea2SJoerg Wunsch }
47458c9d623SPeter Wemm #endif
4755f830ea2SJoerg Wunsch 
476427ccf00SDoug Rabson static u_int8_t
477427ccf00SDoug Rabson fdin_rd(fdc_p fdc)
478427ccf00SDoug Rabson {
479427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
480427ccf00SDoug Rabson }
481427ccf00SDoug Rabson 
48287f6c662SJulian Elischer #define CDEV_MAJOR 9
4834e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = {
4844e2f199eSPoul-Henning Kamp 	/* open */	Fdopen,
4854e2f199eSPoul-Henning Kamp 	/* close */	fdclose,
4864e2f199eSPoul-Henning Kamp 	/* read */	physread,
4874e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
4884e2f199eSPoul-Henning Kamp 	/* ioctl */	fdioctl,
4894e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
4904e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
4914e2f199eSPoul-Henning Kamp 	/* strategy */	fdstrategy,
4924e2f199eSPoul-Henning Kamp 	/* name */	"fd",
4934e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
4944e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
4954e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
4964e2f199eSPoul-Henning Kamp 	/* flags */	D_DISK,
4974e2f199eSPoul-Henning Kamp };
4984e2f199eSPoul-Henning Kamp 
499246ed35dSJoerg Wunsch /*
500246ed35dSJoerg Wunsch  * Auxiliary functions.  Well, some only.  Others are scattered
501246ed35dSJoerg Wunsch  * throughout the entire file.
502246ed35dSJoerg Wunsch  */
503dc5df763SJoerg Wunsch static int
5046182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
505dc5df763SJoerg Wunsch {
5066182fdbdSPeter Wemm 	fdc->fdc_errs++;
50716b04b6aSJoerg Wunsch 	if (s) {
508b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
509b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
510b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
511b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
512b6e5f28eSPeter Wemm 						    "logging any more\n");
51316b04b6aSJoerg Wunsch 	}
514dc5df763SJoerg Wunsch 
515dc5df763SJoerg Wunsch 	return FD_FAILED;
516dc5df763SJoerg Wunsch }
517dc5df763SJoerg Wunsch 
518dc5df763SJoerg Wunsch /*
519dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
520dc5df763SJoerg Wunsch  * Unit number,
521dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
522dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
523dc5df763SJoerg Wunsch  */
5246f4e0bebSPoul-Henning Kamp static int
5256182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...)
526dc5df763SJoerg Wunsch {
527dc5df763SJoerg Wunsch 	u_char cmd;
528dc5df763SJoerg Wunsch 	int n_in;
529dc5df763SJoerg Wunsch 	int n;
530dc5df763SJoerg Wunsch 	va_list ap;
531dc5df763SJoerg Wunsch 
532dc5df763SJoerg Wunsch 	va_start(ap, n_out);
533dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
534dc5df763SJoerg Wunsch 	va_end(ap);
535dc5df763SJoerg Wunsch 	va_start(ap, n_out);
536dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
537dc5df763SJoerg Wunsch 	{
5386182fdbdSPeter Wemm 		if (out_fdc(fdc, va_arg(ap, int)) < 0)
539dc5df763SJoerg Wunsch 		{
540dc5df763SJoerg Wunsch 			char msg[50];
5412127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
542dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
543dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
5446182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
545dc5df763SJoerg Wunsch 		}
546dc5df763SJoerg Wunsch 	}
547dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
548dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
549dc5df763SJoerg Wunsch 	{
550dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
5516182fdbdSPeter Wemm 		if (fd_in(fdc, ptr) < 0)
552dc5df763SJoerg Wunsch 		{
553dc5df763SJoerg Wunsch 			char msg[50];
5542127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
555dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
556dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
5576182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
558dc5df763SJoerg Wunsch 		}
559dc5df763SJoerg Wunsch 	}
560dc5df763SJoerg Wunsch 
561dc5df763SJoerg Wunsch 	return 0;
562dc5df763SJoerg Wunsch }
563dc5df763SJoerg Wunsch 
5646f4e0bebSPoul-Henning Kamp static int
565d66c374fSTor Egge enable_fifo(fdc_p fdc)
566d66c374fSTor Egge {
567d66c374fSTor Egge 	int i, j;
568d66c374fSTor Egge 
569d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
570d66c374fSTor Egge 
571d66c374fSTor Egge 		/*
572d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
573d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
574d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
575d66c374fSTor Egge 		 */
576d66c374fSTor Egge 
5776182fdbdSPeter Wemm 		if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
5786182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
579d66c374fSTor Egge 
580d66c374fSTor Egge 		/* If command is invalid, return */
581d66c374fSTor Egge 		j = 100000;
582427ccf00SDoug Rabson 		while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
583d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
584d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
585d66c374fSTor Egge 				fdc_reset(fdc);
586d66c374fSTor Egge 				return FD_FAILED;
587d66c374fSTor Egge 			}
588d66c374fSTor Egge 		if (j<0 ||
5896182fdbdSPeter Wemm 		    fd_cmd(fdc, 3,
590d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
591d66c374fSTor Egge 			fdc_reset(fdc);
5926182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
593d66c374fSTor Egge 		}
594d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
595d66c374fSTor Egge 		return 0;
596d66c374fSTor Egge 	}
5976182fdbdSPeter Wemm 	if (fd_cmd(fdc, 4,
598d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
5996182fdbdSPeter Wemm 		return fdc_err(fdc, "Re-enable FIFO failed\n");
600d66c374fSTor Egge 	return 0;
601d66c374fSTor Egge }
602d66c374fSTor Egge 
603d66c374fSTor Egge static int
604dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
605dc5df763SJoerg Wunsch {
606dc5df763SJoerg Wunsch 	int st3;
607dc5df763SJoerg Wunsch 
6086182fdbdSPeter Wemm 	if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
609dc5df763SJoerg Wunsch 	{
6106182fdbdSPeter Wemm 		return fdc_err(fdc, "Sense Drive Status failed\n");
611dc5df763SJoerg Wunsch 	}
612dc5df763SJoerg Wunsch 	if (st3p)
613dc5df763SJoerg Wunsch 		*st3p = st3;
614dc5df763SJoerg Wunsch 
615dc5df763SJoerg Wunsch 	return 0;
616dc5df763SJoerg Wunsch }
617dc5df763SJoerg Wunsch 
6186f4e0bebSPoul-Henning Kamp static int
619dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
620dc5df763SJoerg Wunsch {
6216182fdbdSPeter Wemm 	int cyl, st0, ret;
622dc5df763SJoerg Wunsch 
6236182fdbdSPeter Wemm 	ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
6246182fdbdSPeter Wemm 	if (ret) {
6256182fdbdSPeter Wemm 		(void)fdc_err(fdc,
626dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
627dc5df763SJoerg Wunsch 		return ret;
628dc5df763SJoerg Wunsch 	}
629dc5df763SJoerg Wunsch 
630dc5df763SJoerg Wunsch 	if (st0p)
631dc5df763SJoerg Wunsch 		*st0p = st0;
632dc5df763SJoerg Wunsch 
6336182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
634dc5df763SJoerg Wunsch 		/*
635dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
636dc5df763SJoerg Wunsch 		 */
637dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
638dc5df763SJoerg Wunsch 	}
639dc5df763SJoerg Wunsch 
6406182fdbdSPeter Wemm 	if (fd_in(fdc, &cyl) < 0) {
6416182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
642dc5df763SJoerg Wunsch 	}
643dc5df763SJoerg Wunsch 
644dc5df763SJoerg Wunsch 	if (cylp)
645dc5df763SJoerg Wunsch 		*cylp = cyl;
646dc5df763SJoerg Wunsch 
647dc5df763SJoerg Wunsch 	return 0;
648dc5df763SJoerg Wunsch }
649dc5df763SJoerg Wunsch 
650dc5df763SJoerg Wunsch 
6516f4e0bebSPoul-Henning Kamp static int
65264860614SJoerg Wunsch fd_read_status(fdc_p fdc)
653dc5df763SJoerg Wunsch {
654dc5df763SJoerg Wunsch 	int i, ret;
655b5e8ce9fSBruce Evans 
656246ed35dSJoerg Wunsch 	for (i = ret = 0; i < 7; i++) {
657b5e8ce9fSBruce Evans 		/*
65864860614SJoerg Wunsch 		 * XXX types are poorly chosen.  Only bytes can be read
659a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
660b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
661b5e8ce9fSBruce Evans 		 */
662b5e8ce9fSBruce Evans 		int status;
663b5e8ce9fSBruce Evans 
6646182fdbdSPeter Wemm 		ret = fd_in(fdc, &status);
665b5e8ce9fSBruce Evans 		fdc->status[i] = status;
666b5e8ce9fSBruce Evans 		if (ret != 0)
667dc5df763SJoerg Wunsch 			break;
668dc5df763SJoerg Wunsch 	}
669dc5df763SJoerg Wunsch 
670dc5df763SJoerg Wunsch 	if (ret == 0)
671dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
672dc5df763SJoerg Wunsch 	else
673dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
674dc5df763SJoerg Wunsch 
675dc5df763SJoerg Wunsch 	return ret;
676dc5df763SJoerg Wunsch }
677dc5df763SJoerg Wunsch 
6783a2f7427SDavid Greenman static int
67937286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc)
6805b81b6b3SRodney W. Grimes {
68137286586SPeter Wemm 	device_t dev;
682e219897aSJoerg Wunsch 	int ispnp, ispcmcia, nports;
6835b81b6b3SRodney W. Grimes 
68437286586SPeter Wemm 	dev = fdc->fdc_dev;
6855f830ea2SJoerg Wunsch 	ispnp = (fdc->flags & FDC_ISPNP) != 0;
6865f830ea2SJoerg Wunsch 	ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
6876182fdbdSPeter Wemm 	fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
6886182fdbdSPeter Wemm 	fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
6896182fdbdSPeter Wemm 
690b6e5f28eSPeter Wemm 	/*
6915f830ea2SJoerg Wunsch 	 * On standard ISA, we don't just use an 8 port range
6925f830ea2SJoerg Wunsch 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
6935f830ea2SJoerg Wunsch 	 * register at 0x3f6.
6945f830ea2SJoerg Wunsch 	 *
695b6e5f28eSPeter Wemm 	 * Isn't PC hardware wonderful.
6965f830ea2SJoerg Wunsch 	 *
6975f830ea2SJoerg Wunsch 	 * The Y-E Data PCMCIA FDC doesn't have this problem, it
6985f830ea2SJoerg Wunsch 	 * uses the register with offset 6 for pseudo-DMA, and the
6995f830ea2SJoerg Wunsch 	 * one with offset 7 as control register.
700b6e5f28eSPeter Wemm 	 */
701e219897aSJoerg Wunsch 	nports = ispcmcia ? 8 : (ispnp ? 1 : 6);
7026182fdbdSPeter Wemm 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
7036182fdbdSPeter Wemm 					     &fdc->rid_ioport, 0ul, ~0ul,
704e219897aSJoerg Wunsch 					     nports, RF_ACTIVE);
7056182fdbdSPeter Wemm 	if (fdc->res_ioport == 0) {
706e219897aSJoerg Wunsch 		device_printf(dev, "cannot reserve I/O port range (%d ports)\n",
707e219897aSJoerg Wunsch 			      nports);
70837286586SPeter Wemm 		return ENXIO;
7096182fdbdSPeter Wemm 	}
710427ccf00SDoug Rabson 	fdc->portt = rman_get_bustag(fdc->res_ioport);
711427ccf00SDoug Rabson 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
712427ccf00SDoug Rabson 
7135f830ea2SJoerg Wunsch 	if (!ispcmcia) {
714427ccf00SDoug Rabson 		/*
7155f830ea2SJoerg Wunsch 		 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
7165f830ea2SJoerg Wunsch 		 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
7175f830ea2SJoerg Wunsch 		 * by checking the size and adjust the port address
7185f830ea2SJoerg Wunsch 		 * accordingly.
719b6e5f28eSPeter Wemm 		 */
720b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
721b6e5f28eSPeter Wemm 			fdc->port_off = -2;
722b6e5f28eSPeter Wemm 
723b6e5f28eSPeter Wemm 		/*
7245f830ea2SJoerg Wunsch 		 * Register the control port range as rid 1 if it
7255f830ea2SJoerg Wunsch 		 * isn't there already. Most PnP BIOSen will have
7265f830ea2SJoerg Wunsch 		 * already done this but non-PnP configurations don't.
727a2639a18SPeter Wemm 		 *
7285f830ea2SJoerg Wunsch 		 * And some (!!) report 0x3f2-0x3f5 and completely
7295f830ea2SJoerg Wunsch 		 * leave out the control register!  It seems that some
7305f830ea2SJoerg Wunsch 		 * non-antique controller chips have a different
7315f830ea2SJoerg Wunsch 		 * method of programming the transfer speed which
7325f830ea2SJoerg Wunsch 		 * doesn't require the control register, but it's
7335f830ea2SJoerg Wunsch 		 * mighty bogus as the chip still responds to the
7345f830ea2SJoerg Wunsch 		 * address for the control register.
735427ccf00SDoug Rabson 		 */
736b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
737b9da888fSPeter Wemm 			u_long ctlstart;
738b9da888fSPeter Wemm 
739b6e5f28eSPeter Wemm 			/* Find the control port, usually 0x3f7 */
7405f830ea2SJoerg Wunsch 			ctlstart = rman_get_start(fdc->res_ioport) +
7415f830ea2SJoerg Wunsch 				fdc->port_off + 7;
742b6e5f28eSPeter Wemm 
743b6e5f28eSPeter Wemm 			bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
744b9da888fSPeter Wemm 		}
745b6e5f28eSPeter Wemm 
746b6e5f28eSPeter Wemm 		/*
747b6e5f28eSPeter Wemm 		 * Now (finally!) allocate the control port.
748b6e5f28eSPeter Wemm 		 */
749427ccf00SDoug Rabson 		fdc->rid_ctl = 1;
7505f830ea2SJoerg Wunsch 		fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
7515f830ea2SJoerg Wunsch 						  &fdc->rid_ctl,
752b6e5f28eSPeter Wemm 						  0ul, ~0ul, 1, RF_ACTIVE);
753427ccf00SDoug Rabson 		if (fdc->res_ctl == 0) {
7545f830ea2SJoerg Wunsch 			device_printf(dev,
755e219897aSJoerg Wunsch 		"cannot reserve control I/O port range (control port)\n");
75637286586SPeter Wemm 			return ENXIO;
757427ccf00SDoug Rabson 		}
758427ccf00SDoug Rabson 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
759427ccf00SDoug Rabson 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
7605f830ea2SJoerg Wunsch 	}
7616182fdbdSPeter Wemm 
7626182fdbdSPeter Wemm 	fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
7636182fdbdSPeter Wemm 					  &fdc->rid_irq, 0ul, ~0ul, 1,
7646182fdbdSPeter Wemm 					  RF_ACTIVE);
7656182fdbdSPeter Wemm 	if (fdc->res_irq == 0) {
766427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve interrupt line\n");
76737286586SPeter Wemm 		return ENXIO;
7686182fdbdSPeter Wemm 	}
7695f830ea2SJoerg Wunsch 
7705f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
7716182fdbdSPeter Wemm 		fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
7726182fdbdSPeter Wemm 						  &fdc->rid_drq, 0ul, ~0ul, 1,
7736182fdbdSPeter Wemm 						  RF_ACTIVE);
7746182fdbdSPeter Wemm 		if (fdc->res_drq == 0) {
775427ccf00SDoug Rabson 			device_printf(dev, "cannot reserve DMA request line\n");
77637286586SPeter Wemm 			return ENXIO;
7776182fdbdSPeter Wemm 		}
7786182fdbdSPeter Wemm 		fdc->dmachan = fdc->res_drq->r_start;
7795f830ea2SJoerg Wunsch 	}
78037286586SPeter Wemm 
78137286586SPeter Wemm 	return 0;
78237286586SPeter Wemm }
78337286586SPeter Wemm 
78437286586SPeter Wemm static void
78537286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
78637286586SPeter Wemm {
78737286586SPeter Wemm 	device_t dev;
78837286586SPeter Wemm 
78937286586SPeter Wemm 	dev = fdc->fdc_dev;
79037286586SPeter Wemm 	if (fdc->res_irq != 0) {
79137286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
79237286586SPeter Wemm 					fdc->res_irq);
79337286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
79437286586SPeter Wemm 				     fdc->res_irq);
79537286586SPeter Wemm 	}
79637286586SPeter Wemm 	if (fdc->res_ctl != 0) {
79737286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
79837286586SPeter Wemm 					fdc->res_ctl);
79937286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
80037286586SPeter Wemm 				     fdc->res_ctl);
80137286586SPeter Wemm 	}
80237286586SPeter Wemm 	if (fdc->res_ioport != 0) {
80337286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
80437286586SPeter Wemm 					fdc->res_ioport);
80537286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
80637286586SPeter Wemm 				     fdc->res_ioport);
80737286586SPeter Wemm 	}
80837286586SPeter Wemm 	if (fdc->res_drq != 0) {
80937286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
81037286586SPeter Wemm 					fdc->res_drq);
81137286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
81237286586SPeter Wemm 				     fdc->res_drq);
81337286586SPeter Wemm 	}
81437286586SPeter Wemm }
81537286586SPeter Wemm 
816246ed35dSJoerg Wunsch /*
817246ed35dSJoerg Wunsch  * Configuration/initialization stuff, per controller.
818246ed35dSJoerg Wunsch  */
81937286586SPeter Wemm 
82037286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = {
82137286586SPeter Wemm 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
82237286586SPeter Wemm 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
82337286586SPeter Wemm 	{0}
82437286586SPeter Wemm };
82537286586SPeter Wemm 
82637286586SPeter Wemm static int
8273aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
82837286586SPeter Wemm {
82937286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
83037286586SPeter Wemm 
83137286586SPeter Wemm 	switch (which) {
83237286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
83337286586SPeter Wemm 		*result = ivars->fdunit;
83437286586SPeter Wemm 		break;
83537286586SPeter Wemm 	default:
83637286586SPeter Wemm 		return ENOENT;
83737286586SPeter Wemm 	}
83837286586SPeter Wemm 	return 0;
83937286586SPeter Wemm }
84037286586SPeter Wemm 
84137286586SPeter Wemm static int
84237286586SPeter Wemm fdc_probe(device_t dev)
84337286586SPeter Wemm {
84437286586SPeter Wemm 	int	error, ic_type;
84537286586SPeter Wemm 	struct	fdc_data *fdc;
84637286586SPeter Wemm 
84737286586SPeter Wemm 	fdc = device_get_softc(dev);
84837286586SPeter Wemm 	bzero(fdc, sizeof *fdc);
84937286586SPeter Wemm 	fdc->fdc_dev = dev;
8505f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_isa;
85137286586SPeter Wemm 
85237286586SPeter Wemm 	/* Check pnp ids */
85337286586SPeter Wemm 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
85437286586SPeter Wemm 	if (error == ENXIO)
85537286586SPeter Wemm 		return ENXIO;
8565f830ea2SJoerg Wunsch 	if (error == 0)
8575f830ea2SJoerg Wunsch 		fdc->flags |= FDC_ISPNP;
85837286586SPeter Wemm 
85937286586SPeter Wemm 	/* Attempt to allocate our resources for the duration of the probe */
86037286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
86137286586SPeter Wemm 	if (error)
86237286586SPeter Wemm 		goto out;
8635b81b6b3SRodney W. Grimes 
86416111cedSAndrew Moore 	/* First - lets reset the floppy controller */
865427ccf00SDoug Rabson 	fdout_wr(fdc, 0);
86616111cedSAndrew Moore 	DELAY(100);
867427ccf00SDoug Rabson 	fdout_wr(fdc, FDO_FRST);
86816111cedSAndrew Moore 
8695b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
8706182fdbdSPeter Wemm 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
8716182fdbdSPeter Wemm 		   NE7_SPEC_2(2, 0), 0)) {
8726182fdbdSPeter Wemm 		error = ENXIO;
8736182fdbdSPeter Wemm 		goto out;
8745b81b6b3SRodney W. Grimes 	}
8756182fdbdSPeter Wemm 
8766182fdbdSPeter Wemm 	if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
8776182fdbdSPeter Wemm 		ic_type = (u_char)ic_type;
8786182fdbdSPeter Wemm 		switch (ic_type) {
8796182fdbdSPeter Wemm 		case 0x80:
8806182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 765 or clone");
8816182fdbdSPeter Wemm 			fdc->fdct = FDC_NE765;
8826182fdbdSPeter Wemm 			break;
8831a6bed68SJoerg Wunsch 		case 0x81:	/* not mentioned in any hardware doc */
8846182fdbdSPeter Wemm 		case 0x90:
8851a6bed68SJoerg Wunsch 			device_set_desc(dev,
8861a6bed68SJoerg Wunsch 		"enhanced floppy controller (i82077, NE72065 or clone)");
8871a6bed68SJoerg Wunsch 			fdc->fdct = FDC_ENHANCED;
8886182fdbdSPeter Wemm 			break;
8896182fdbdSPeter Wemm 		default:
8906182fdbdSPeter Wemm 			device_set_desc(dev, "generic floppy controller");
8916182fdbdSPeter Wemm 			fdc->fdct = FDC_UNKNOWN;
8926182fdbdSPeter Wemm 			break;
8936182fdbdSPeter Wemm 		}
8946182fdbdSPeter Wemm 	}
8956182fdbdSPeter Wemm 
8966182fdbdSPeter Wemm out:
89737286586SPeter Wemm 	fdc_release_resources(fdc);
8986182fdbdSPeter Wemm 	return (error);
8995b81b6b3SRodney W. Grimes }
9005b81b6b3SRodney W. Grimes 
9015f830ea2SJoerg Wunsch #if NCARD > 0
9025f830ea2SJoerg Wunsch 
9035f830ea2SJoerg Wunsch static int
9045f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev)
9055f830ea2SJoerg Wunsch {
9065f830ea2SJoerg Wunsch 	int	error;
9075f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
9085f830ea2SJoerg Wunsch 
9095f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
9105f830ea2SJoerg Wunsch 	bzero(fdc, sizeof *fdc);
9115f830ea2SJoerg Wunsch 	fdc->fdc_dev = dev;
9125f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_pcmcia;
9135f830ea2SJoerg Wunsch 
9145f830ea2SJoerg Wunsch 	fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
9155f830ea2SJoerg Wunsch 
9165f830ea2SJoerg Wunsch 	/* Attempt to allocate our resources for the duration of the probe */
9175f830ea2SJoerg Wunsch 	error = fdc_alloc_resources(fdc);
9185f830ea2SJoerg Wunsch 	if (error)
9195f830ea2SJoerg Wunsch 		goto out;
9205f830ea2SJoerg Wunsch 
9215f830ea2SJoerg Wunsch 	/* First - lets reset the floppy controller */
9225f830ea2SJoerg Wunsch 	fdout_wr(fdc, 0);
9235f830ea2SJoerg Wunsch 	DELAY(100);
9245f830ea2SJoerg Wunsch 	fdout_wr(fdc, FDO_FRST);
9255f830ea2SJoerg Wunsch 
9265f830ea2SJoerg Wunsch 	/* see if it can handle a command */
9275f830ea2SJoerg Wunsch 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
9285f830ea2SJoerg Wunsch 		   NE7_SPEC_2(2, 0), 0)) {
9295f830ea2SJoerg Wunsch 		error = ENXIO;
9305f830ea2SJoerg Wunsch 		goto out;
9315f830ea2SJoerg Wunsch 	}
9325f830ea2SJoerg Wunsch 
9335f830ea2SJoerg Wunsch 	device_set_desc(dev, "Y-E Data PCMCIA floppy");
9345f830ea2SJoerg Wunsch 	fdc->fdct = FDC_NE765;
9355f830ea2SJoerg Wunsch 
9365f830ea2SJoerg Wunsch out:
9375f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
9385f830ea2SJoerg Wunsch 	return (error);
9395f830ea2SJoerg Wunsch }
9405f830ea2SJoerg Wunsch 
941e219897aSJoerg Wunsch #endif /* NCARD > 0 */
942e219897aSJoerg Wunsch 
9435f830ea2SJoerg Wunsch static int
944e219897aSJoerg Wunsch fdc_detach(device_t dev)
9455f830ea2SJoerg Wunsch {
9465f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
9475f830ea2SJoerg Wunsch 	int	error;
9485f830ea2SJoerg Wunsch 
9495f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
9505f830ea2SJoerg Wunsch 
9515f830ea2SJoerg Wunsch 	/* have our children detached first */
9525f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
9535f830ea2SJoerg Wunsch 		return (error);
9545f830ea2SJoerg Wunsch 
955e219897aSJoerg Wunsch 	/* reset controller, turn motor off */
956e219897aSJoerg Wunsch 	fdout_wr(fdc, 0);
957e219897aSJoerg Wunsch 
958e219897aSJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0)
959e219897aSJoerg Wunsch 		isa_dma_release(fdc->dmachan);
960e219897aSJoerg Wunsch 
9615f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_ATTACHED) == 0) {
9625f830ea2SJoerg Wunsch 		device_printf(dev, "already unloaded\n");
9635f830ea2SJoerg Wunsch 		return (0);
9645f830ea2SJoerg Wunsch 	}
9655f830ea2SJoerg Wunsch 	fdc->flags &= ~FDC_ATTACHED;
9665f830ea2SJoerg Wunsch 
9675f830ea2SJoerg Wunsch 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
9685f830ea2SJoerg Wunsch 			  fdc->fdc_intr);
9695f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
9705f830ea2SJoerg Wunsch 	device_printf(dev, "unload\n");
9715f830ea2SJoerg Wunsch 	return (0);
9725f830ea2SJoerg Wunsch }
9735f830ea2SJoerg Wunsch 
9745b81b6b3SRodney W. Grimes /*
97537286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
9765b81b6b3SRodney W. Grimes  */
9776182fdbdSPeter Wemm static void
97837286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
9795b81b6b3SRodney W. Grimes {
9801a6bed68SJoerg Wunsch 	int	disabled, flags;
98137286586SPeter Wemm 	struct fdc_ivars *ivar;
9826182fdbdSPeter Wemm 	device_t child;
98392200632SGarrett Wollman 
9847cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
98537286586SPeter Wemm 	if (ivar == NULL)
9866182fdbdSPeter Wemm 		return;
98737286586SPeter Wemm 	if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
98837286586SPeter Wemm 		ivar->fdunit = 0;
989fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
99037286586SPeter Wemm 	if (child == NULL)
9916182fdbdSPeter Wemm 		return;
99237286586SPeter Wemm 	device_set_ivars(child, ivar);
9931a6bed68SJoerg Wunsch 	if (resource_int_value(name, unit, "flags", &flags) == 0)
9941a6bed68SJoerg Wunsch 		 device_set_flags(child, flags);
995a97c75b7SDoug Rabson 	if (resource_int_value(name, unit, "disabled", &disabled) == 0
996a97c75b7SDoug Rabson 	    && disabled != 0)
9976182fdbdSPeter Wemm 		device_disable(child);
9986182fdbdSPeter Wemm }
9996182fdbdSPeter Wemm 
10006182fdbdSPeter Wemm static int
10016182fdbdSPeter Wemm fdc_attach(device_t dev)
10026182fdbdSPeter Wemm {
100337286586SPeter Wemm 	struct	fdc_data *fdc;
10042398f0cdSPeter Wemm 	const char *name, *dname;
10055d54fe91SJoerg Wunsch 	int	i, error, dunit;
1006427ccf00SDoug Rabson 
100737286586SPeter Wemm 	fdc = device_get_softc(dev);
100837286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
100937286586SPeter Wemm 	if (error) {
1010f8ce7dd5SJoerg Wunsch 		device_printf(dev, "cannot re-acquire resources\n");
101137286586SPeter Wemm 		return error;
101237286586SPeter Wemm 	}
101337286586SPeter Wemm 	error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
10143c36743eSMark Murray 			       INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
10153c36743eSMark Murray 			       &fdc->fdc_intr);
101637286586SPeter Wemm 	if (error) {
101737286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
101837286586SPeter Wemm 		return error;
101937286586SPeter Wemm 	}
102037286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
1021fb35bd37SJoerg Wunsch 	fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET;
10226182fdbdSPeter Wemm 
10235f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
1024f8ce7dd5SJoerg Wunsch 		/*
1025f8ce7dd5SJoerg Wunsch 		 * Acquire the DMA channel forever, the driver will do
1026f8ce7dd5SJoerg Wunsch 		 * the rest
1027f8ce7dd5SJoerg Wunsch 		 * XXX should integrate with rman
1028f8ce7dd5SJoerg Wunsch 		 */
1029100f78bbSSujal Patel 		isa_dma_acquire(fdc->dmachan);
1030f8ce7dd5SJoerg Wunsch 		isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
10315f830ea2SJoerg Wunsch 	}
10325b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
10336182fdbdSPeter Wemm 
10343a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
103564860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
10368177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
10375b81b6b3SRodney W. Grimes 
10386182fdbdSPeter Wemm 	/*
103937286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
104037286586SPeter Wemm 	 * devices from the BIOS unless overridden.
10416182fdbdSPeter Wemm 	 */
1042ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
10432398f0cdSPeter Wemm 	i = 0;
10442398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
10452398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
104637286586SPeter Wemm 
104760444853SJoerg Wunsch 	if ((error = bus_generic_attach(dev)) != 0)
104860444853SJoerg Wunsch 		return (error);
104960444853SJoerg Wunsch 
105060444853SJoerg Wunsch 	return (0);
10516182fdbdSPeter Wemm }
10526182fdbdSPeter Wemm 
105315317dd8SMatthew N. Dodd static int
10546182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
10556182fdbdSPeter Wemm {
10561a6bed68SJoerg Wunsch 	int retval = 0, flags;
105715317dd8SMatthew N. Dodd 
105815317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
10591a6bed68SJoerg Wunsch 	retval += printf(" on %s drive %d", device_get_nameunit(me),
106037286586SPeter Wemm 	       fdc_get_fdunit(child));
10611a6bed68SJoerg Wunsch 	if ((flags = device_get_flags(me)) != 0)
10621a6bed68SJoerg Wunsch 		retval += printf(" flags %#x", flags);
10631a6bed68SJoerg Wunsch 	retval += printf("\n");
106415317dd8SMatthew N. Dodd 
106515317dd8SMatthew N. Dodd 	return (retval);
10666182fdbdSPeter Wemm }
10676182fdbdSPeter Wemm 
106816e68fc6SPeter Wemm static device_method_t fdc_methods[] = {
106916e68fc6SPeter Wemm 	/* Device interface */
107016e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fdc_probe),
107116e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fdc_attach),
1072e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
107316e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
107416e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend),
107516e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume),
107616e68fc6SPeter Wemm 
107716e68fc6SPeter Wemm 	/* Bus interface */
107816e68fc6SPeter Wemm 	DEVMETHOD(bus_print_child,	fdc_print_child),
107937286586SPeter Wemm 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
108016e68fc6SPeter Wemm 	/* Our children never use any other bus interface methods. */
108116e68fc6SPeter Wemm 
108216e68fc6SPeter Wemm 	{ 0, 0 }
108316e68fc6SPeter Wemm };
108416e68fc6SPeter Wemm 
108516e68fc6SPeter Wemm static driver_t fdc_driver = {
108616e68fc6SPeter Wemm 	"fdc",
108716e68fc6SPeter Wemm 	fdc_methods,
108816e68fc6SPeter Wemm 	sizeof(struct fdc_data)
108916e68fc6SPeter Wemm };
109016e68fc6SPeter Wemm 
109116e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
10925f063c7bSMike Smith DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0);
109316e68fc6SPeter Wemm 
10945f830ea2SJoerg Wunsch #if NCARD > 0
10955f830ea2SJoerg Wunsch 
10965f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = {
10975f830ea2SJoerg Wunsch 	/* Device interface */
10985f830ea2SJoerg Wunsch 	DEVMETHOD(device_probe,		fdc_pccard_probe),
10995f830ea2SJoerg Wunsch 	DEVMETHOD(device_attach,	fdc_attach),
1100e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
11015f830ea2SJoerg Wunsch 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
11025f830ea2SJoerg Wunsch 	DEVMETHOD(device_suspend,	bus_generic_suspend),
11035f830ea2SJoerg Wunsch 	DEVMETHOD(device_resume,	bus_generic_resume),
11045f830ea2SJoerg Wunsch 
11055f830ea2SJoerg Wunsch 	/* Bus interface */
11065f830ea2SJoerg Wunsch 	DEVMETHOD(bus_print_child,	fdc_print_child),
11075f830ea2SJoerg Wunsch 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
11085f830ea2SJoerg Wunsch 	/* Our children never use any other bus interface methods. */
11095f830ea2SJoerg Wunsch 
11105f830ea2SJoerg Wunsch 	{ 0, 0 }
11115f830ea2SJoerg Wunsch };
11125f830ea2SJoerg Wunsch 
11135f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = {
11145f830ea2SJoerg Wunsch 	"fdc",
11155f830ea2SJoerg Wunsch 	fdc_pccard_methods,
11165f830ea2SJoerg Wunsch 	sizeof(struct fdc_data)
11175f830ea2SJoerg Wunsch };
11185f830ea2SJoerg Wunsch 
11195f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
11205f830ea2SJoerg Wunsch 
11215f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
11225f830ea2SJoerg Wunsch 
112360444853SJoerg Wunsch /*
11241a6bed68SJoerg Wunsch  * Create a clone device upon request by devfs.
112560444853SJoerg Wunsch  */
11263f54a085SPoul-Henning Kamp static void
112780909a7dSJoerg Wunsch fd_clone(void *arg, char *name, int namelen, dev_t *dev)
11283f54a085SPoul-Henning Kamp {
112960444853SJoerg Wunsch 	struct	fd_data *fd;
11301a6bed68SJoerg Wunsch 	int i, u;
11313f54a085SPoul-Henning Kamp 	char *n;
11321a6bed68SJoerg Wunsch 	size_t l;
11333f54a085SPoul-Henning Kamp 
113460444853SJoerg Wunsch 	fd = (struct fd_data *)arg;
11353f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
11363f54a085SPoul-Henning Kamp 		return;
1137db901281SPoul-Henning Kamp 	if (dev_stdclone(name, &n, "fd", &u) != 2)
11383f54a085SPoul-Henning Kamp 		return;
11391a6bed68SJoerg Wunsch 	l = strlen(n);
11401a6bed68SJoerg Wunsch 	if (l == 1 && *n >= 'a' && *n <= 'h') {
11411a6bed68SJoerg Wunsch 		/*
11421a6bed68SJoerg Wunsch 		 * Trailing letters a through h denote
11431a6bed68SJoerg Wunsch 		 * pseudo-partitions.  We don't support true
11441a6bed68SJoerg Wunsch 		 * (UFS-style) partitions, so we just implement them
11451a6bed68SJoerg Wunsch 		 * as symlinks if someone asks us nicely.
11461a6bed68SJoerg Wunsch 		 */
114760444853SJoerg Wunsch 		*dev = make_dev_alias(fd->masterdev, name);
11481a6bed68SJoerg Wunsch 		return;
11491a6bed68SJoerg Wunsch 	}
11501a6bed68SJoerg Wunsch 	if (l >= 2 && l <= 5 && *n == '.') {
11511a6bed68SJoerg Wunsch 		/*
11521a6bed68SJoerg Wunsch 		 * Trailing numbers, preceded by a dot, denote
11531a6bed68SJoerg Wunsch 		 * subdevices for different densities.  Historically,
11541a6bed68SJoerg Wunsch 		 * they have been named by density (like fd0.1440),
11551a6bed68SJoerg Wunsch 		 * but we allow arbitrary numbers between 1 and 4
11561a6bed68SJoerg Wunsch 		 * digits, so fd0.1 through fd0.15 are possible as
11571a6bed68SJoerg Wunsch 		 * well.
11581a6bed68SJoerg Wunsch 		 */
11591a6bed68SJoerg Wunsch 		for (i = 1; i < l; i++)
11601a6bed68SJoerg Wunsch 			if (n[i] < '0' || n[i] > '9')
11611a6bed68SJoerg Wunsch 				return;
11621a6bed68SJoerg Wunsch 		for (i = 0; i < NUMDENS - 1; i++)
11631a6bed68SJoerg Wunsch 			if (fd->clonedevs[i] == NODEV) {
11641a6bed68SJoerg Wunsch 				*dev = make_dev(&fd_cdevsw,
11651a6bed68SJoerg Wunsch 						FDNUMTOUNIT(u) + i + 1,
11661a6bed68SJoerg Wunsch 						UID_ROOT, GID_OPERATOR, 0640,
11671a6bed68SJoerg Wunsch 						name);
11681a6bed68SJoerg Wunsch 				fd->clonedevs[i] = *dev;
11691a6bed68SJoerg Wunsch 				return;
11701a6bed68SJoerg Wunsch 			}
11713f54a085SPoul-Henning Kamp 	}
11723f54a085SPoul-Henning Kamp }
11733f54a085SPoul-Henning Kamp 
117416e68fc6SPeter Wemm /*
1175246ed35dSJoerg Wunsch  * Configuration/initialization, per drive.
117616e68fc6SPeter Wemm  */
11776182fdbdSPeter Wemm static int
11786182fdbdSPeter Wemm fd_probe(device_t dev)
11796182fdbdSPeter Wemm {
11806182fdbdSPeter Wemm 	int	i;
11811a6bed68SJoerg Wunsch 	u_int	st0, st3;
11826182fdbdSPeter Wemm 	struct	fd_data *fd;
11836182fdbdSPeter Wemm 	struct	fdc_data *fdc;
11846182fdbdSPeter Wemm 	fdsu_t	fdsu;
11851a6bed68SJoerg Wunsch 	int	flags;
11866182fdbdSPeter Wemm 
11876182fdbdSPeter Wemm 	fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
11886182fdbdSPeter Wemm 	fd = device_get_softc(dev);
11896182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
11901a6bed68SJoerg Wunsch 	flags = device_get_flags(dev);
11916182fdbdSPeter Wemm 
11926182fdbdSPeter Wemm 	bzero(fd, sizeof *fd);
11936182fdbdSPeter Wemm 	fd->dev = dev;
11946182fdbdSPeter Wemm 	fd->fdc = fdc;
11956182fdbdSPeter Wemm 	fd->fdsu = fdsu;
11966182fdbdSPeter Wemm 	fd->fdu = device_get_unit(dev);
11971a6bed68SJoerg Wunsch 	fd->flags = FD_UA;	/* make sure fdautoselect() will be called */
11986182fdbdSPeter Wemm 
11991a6bed68SJoerg Wunsch 	fd->type = FD_DTYPE(flags);
12001a6bed68SJoerg Wunsch #if _MACHINE_ARCH == i386
12011a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) {
12021a6bed68SJoerg Wunsch 		/* Look up what the BIOS thinks we have. */
12031a6bed68SJoerg Wunsch 		if (fd->fdu == 0) {
12045f830ea2SJoerg Wunsch 			if ((fdc->flags & FDC_ISPCMCIA))
12051a6bed68SJoerg Wunsch 				/*
12061a6bed68SJoerg Wunsch 				 * Somewhat special.  No need to force the
12071a6bed68SJoerg Wunsch 				 * user to set device flags, since the Y-E
12081a6bed68SJoerg Wunsch 				 * Data PCMCIA floppy is always a 1.44 MB
12091a6bed68SJoerg Wunsch 				 * device.
12101a6bed68SJoerg Wunsch 				 */
12111a6bed68SJoerg Wunsch 				fd->type = FDT_144M;
12120722d6abSJoerg Wunsch 			else
12131a6bed68SJoerg Wunsch 				fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
12141a6bed68SJoerg Wunsch 		} else {
12151a6bed68SJoerg Wunsch 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
12166b7bd95bSJoerg Wunsch 		}
12171a6bed68SJoerg Wunsch 		if (fd->type == FDT_288M_1)
12181a6bed68SJoerg Wunsch 			fd->type = FDT_288M;
12191a6bed68SJoerg Wunsch 	}
12201a6bed68SJoerg Wunsch #endif /* _MACHINE_ARCH == i386 */
12216182fdbdSPeter Wemm 	/* is there a unit? */
12221a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE)
12236182fdbdSPeter Wemm 		return (ENXIO);
12246182fdbdSPeter Wemm 
12256182fdbdSPeter Wemm 	/* select it */
12266182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNON);
1227fb35bd37SJoerg Wunsch 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
12286182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
12296182fdbdSPeter Wemm 
12308de0675cSPeter Wemm 	/* XXX This doesn't work before the first set_motor() */
12311a6bed68SJoerg Wunsch 	if ((fdc->flags & FDC_HAS_FIFO) == 0  &&
12321a6bed68SJoerg Wunsch 	    fdc->fdct == FDC_ENHANCED &&
12331a6bed68SJoerg Wunsch 	    (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 &&
12341a6bed68SJoerg Wunsch 	    enable_fifo(fdc) == 0) {
1235b6e5f28eSPeter Wemm 		device_printf(device_get_parent(dev),
1236b6e5f28eSPeter Wemm 		    "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1237d66c374fSTor Egge 	}
12386182fdbdSPeter Wemm 
12391a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0) {
1240dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
12411a6bed68SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
12421a6bed68SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
1243dc5df763SJoerg Wunsch 			/* Seek some steps... */
12446182fdbdSPeter Wemm 			if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1245dc5df763SJoerg Wunsch 				/* ...wait a moment... */
1246dc5df763SJoerg Wunsch 				DELAY(300000);
1247dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
12486182fdbdSPeter Wemm 				fd_sense_int(fdc, 0, 0);
1249dc5df763SJoerg Wunsch 			}
1250dc5df763SJoerg Wunsch 		}
1251dc5df763SJoerg Wunsch 
12526b7bd95bSJoerg Wunsch 		for (i = 0; i < 2; i++) {
12536b7bd95bSJoerg Wunsch 			/*
12546b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
12551a6bed68SJoerg Wunsch 			 * heads have been beyond cylinder 76, since
12561a6bed68SJoerg Wunsch 			 * most FDCs still barf when attempting to
12571a6bed68SJoerg Wunsch 			 * recalibrate more than 77 steps
12586b7bd95bSJoerg Wunsch 			 */
1259dc5df763SJoerg Wunsch 			/* go back to 0: */
12606182fdbdSPeter Wemm 			if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
12616b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
12626b7bd95bSJoerg Wunsch 				DELAY(i == 0 ? 1000000 : 300000);
12635b81b6b3SRodney W. Grimes 
12646b7bd95bSJoerg Wunsch 				/* anything responding? */
1265dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
1266dc5df763SJoerg Wunsch 				    (st0 & NE7_ST0_EC) == 0)
12676b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
12686b7bd95bSJoerg Wunsch 			}
1269dc5df763SJoerg Wunsch 		}
12701a6bed68SJoerg Wunsch 	}
12716b7bd95bSJoerg Wunsch 
12726182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNOFF);
12733a2f7427SDavid Greenman 
12741a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0 &&
12751a6bed68SJoerg Wunsch 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
12766182fdbdSPeter Wemm 		return (ENXIO);
12775b81b6b3SRodney W. Grimes 
12781a6bed68SJoerg Wunsch 	switch (fd->type) {
12791a6bed68SJoerg Wunsch 	case FDT_12M:
12801a6bed68SJoerg Wunsch 		device_set_desc(dev, "1200-KB 5.25\" drive");
12811a6bed68SJoerg Wunsch 		fd->type = FDT_12M;
12821a6bed68SJoerg Wunsch 		break;
12831a6bed68SJoerg Wunsch 	case FDT_144M:
12841a6bed68SJoerg Wunsch 		device_set_desc(dev, "1440-KB 3.5\" drive");
12851a6bed68SJoerg Wunsch 		fd->type = FDT_144M;
12861a6bed68SJoerg Wunsch 		break;
12871a6bed68SJoerg Wunsch 	case FDT_288M:
12881a6bed68SJoerg Wunsch 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
12891a6bed68SJoerg Wunsch 		fd->type = FDT_288M;
12901a6bed68SJoerg Wunsch 		break;
12911a6bed68SJoerg Wunsch 	case FDT_360K:
12921a6bed68SJoerg Wunsch 		device_set_desc(dev, "360-KB 5.25\" drive");
12931a6bed68SJoerg Wunsch 		fd->type = FDT_360K;
12941a6bed68SJoerg Wunsch 		break;
12951a6bed68SJoerg Wunsch 	case FDT_720K:
12961a6bed68SJoerg Wunsch 		device_set_desc(dev, "720-KB 3.5\" drive");
12971a6bed68SJoerg Wunsch 		fd->type = FDT_720K;
12981a6bed68SJoerg Wunsch 		break;
12991a6bed68SJoerg Wunsch 	default:
13001a6bed68SJoerg Wunsch 		return (ENXIO);
13011a6bed68SJoerg Wunsch 	}
1302dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1303b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1304b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
13053a2f7427SDavid Greenman 	fd->options = 0;
130602a19910SJustin T. Gibbs 	callout_handle_init(&fd->toffhandle);
130702a19910SJustin T. Gibbs 	callout_handle_init(&fd->tohandle);
13085b81b6b3SRodney W. Grimes 
13091a6bed68SJoerg Wunsch 	/* initialize densities for subdevices */
13101a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS; i++)
13111a6bed68SJoerg Wunsch 		memcpy(fd->fts + i, fd_native_types + fd->type,
13121a6bed68SJoerg Wunsch 		       sizeof(struct fd_type));
13136182fdbdSPeter Wemm 	return (0);
13146182fdbdSPeter Wemm }
13156182fdbdSPeter Wemm 
13166182fdbdSPeter Wemm static int
13176182fdbdSPeter Wemm fd_attach(device_t dev)
13186182fdbdSPeter Wemm {
13196182fdbdSPeter Wemm 	struct	fd_data *fd;
132064860614SJoerg Wunsch 	static	int cdevsw_add_done;
132160444853SJoerg Wunsch 	int i;
13226182fdbdSPeter Wemm 
132302211baeSNick Hibma 	if (!cdevsw_add_done) {
1324475ad603SPeter Wemm 		cdevsw_add(&fd_cdevsw);	/* XXX */
132564860614SJoerg Wunsch 		cdevsw_add_done = 1;
132602211baeSNick Hibma 	}
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 	cdevsw_remove(&fd_cdevsw);
1354e219897aSJoerg Wunsch 	EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag);
13555f830ea2SJoerg Wunsch 
13565f830ea2SJoerg Wunsch 	return (0);
13575f830ea2SJoerg Wunsch }
13585f830ea2SJoerg Wunsch 
135916e68fc6SPeter Wemm static device_method_t fd_methods[] = {
136016e68fc6SPeter Wemm 	/* Device interface */
136116e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
136216e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
13635f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
136416e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
136516e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
136616e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
136716e68fc6SPeter Wemm 
136816e68fc6SPeter Wemm 	{ 0, 0 }
136916e68fc6SPeter Wemm };
137016e68fc6SPeter Wemm 
137116e68fc6SPeter Wemm static driver_t fd_driver = {
137216e68fc6SPeter Wemm 	"fd",
137316e68fc6SPeter Wemm 	fd_methods,
137416e68fc6SPeter Wemm 	sizeof(struct fd_data)
137516e68fc6SPeter Wemm };
137616e68fc6SPeter Wemm 
1377475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
137816e68fc6SPeter Wemm 
1379246ed35dSJoerg Wunsch /*
1380246ed35dSJoerg Wunsch  * More auxiliary functions.
1381246ed35dSJoerg Wunsch  */
1382246ed35dSJoerg Wunsch /*
1383246ed35dSJoerg Wunsch  * Motor control stuff.
1384246ed35dSJoerg Wunsch  * Remember to not deselect the drive we're working on.
1385246ed35dSJoerg Wunsch  */
13863a2f7427SDavid Greenman static void
13876182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon)
13885b81b6b3SRodney W. Grimes {
1389fb35bd37SJoerg Wunsch 	int fdout;
13903a2f7427SDavid Greenman 
1391fb35bd37SJoerg Wunsch 	fdout = fdc->fdout;
13923a2f7427SDavid Greenman 	if (turnon) {
13933a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
1394fb35bd37SJoerg Wunsch 		fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
13953a2f7427SDavid Greenman 	} else
13963a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
13976182fdbdSPeter Wemm 	fdc->fdout = fdout;
1398fb35bd37SJoerg Wunsch 	fdout_wr(fdc, fdout);
13993a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
14003a2f7427SDavid Greenman }
14013a2f7427SDavid Greenman 
1402381fe1aaSGarrett Wollman static void
14036182fdbdSPeter Wemm fd_turnoff(void *xfd)
14045b81b6b3SRodney W. Grimes {
1405f5f7ba03SJordan K. Hubbard 	int	s;
14066182fdbdSPeter Wemm 	fd_p fd = xfd;
1407dc16046fSJoerg Wunsch 
14086182fdbdSPeter Wemm 	TRACE1("[fd%d: turnoff]", fd->fdu);
14098335c1b8SBruce Evans 
14105f830ea2SJoerg Wunsch 	s = splbio();
14118335c1b8SBruce Evans 	/*
14128335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
14135f830ea2SJoerg Wunsch 	 *
14145f830ea2SJoerg Wunsch 	 * If we got here, this could only mean we missed an interrupt.
14155f830ea2SJoerg Wunsch 	 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
14165f830ea2SJoerg Wunsch 	 * after a controller reset.  Just schedule a pseudo-interrupt
14175f830ea2SJoerg Wunsch 	 * so the state machine gets re-entered.
14188335c1b8SBruce Evans 	 */
14196182fdbdSPeter Wemm 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
14205f830ea2SJoerg Wunsch 		fdc_intr(fd->fdc);
14215f830ea2SJoerg Wunsch 		splx(s);
14228335c1b8SBruce Evans 		return;
14238335c1b8SBruce Evans 	}
14248335c1b8SBruce Evans 
14255b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
14266182fdbdSPeter Wemm 	set_motor(fd->fdc, fd->fdsu, TURNOFF);
1427f5f7ba03SJordan K. Hubbard 	splx(s);
14285b81b6b3SRodney W. Grimes }
14295b81b6b3SRodney W. Grimes 
14303a2f7427SDavid Greenman static void
14316182fdbdSPeter Wemm fd_motor_on(void *xfd)
14325b81b6b3SRodney W. Grimes {
1433f5f7ba03SJordan K. Hubbard 	int	s;
14346182fdbdSPeter Wemm 	fd_p fd = xfd;
1435f5f7ba03SJordan K. Hubbard 
1436f5f7ba03SJordan K. Hubbard 	s = splbio();
14375b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
14385b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
14395b81b6b3SRodney W. Grimes 	{
14406182fdbdSPeter Wemm 		fdc_intr(fd->fdc);
14415b81b6b3SRodney W. Grimes 	}
1442f5f7ba03SJordan K. Hubbard 	splx(s);
14435b81b6b3SRodney W. Grimes }
14445b81b6b3SRodney W. Grimes 
14453a2f7427SDavid Greenman static void
14466182fdbdSPeter Wemm fd_turnon(fd_p fd)
14475b81b6b3SRodney W. Grimes {
14485b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
14495b81b6b3SRodney W. Grimes 	{
14503a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
14516182fdbdSPeter Wemm 		set_motor(fd->fdc, fd->fdsu, TURNON);
14526182fdbdSPeter Wemm 		timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
14535b81b6b3SRodney W. Grimes 	}
14545b81b6b3SRodney W. Grimes }
14555b81b6b3SRodney W. Grimes 
1456381fe1aaSGarrett Wollman static void
1457dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
14585b81b6b3SRodney W. Grimes {
14593a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
1460427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
14613a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
14623a2f7427SDavid Greenman 	DELAY(100);
14633a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
1464427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
14653a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
14663a2f7427SDavid Greenman 	DELAY(100);
1467427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout);
14683a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
14693a2f7427SDavid Greenman 
147064860614SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
14716182fdbdSPeter Wemm 	(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1472dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1473dc5df763SJoerg Wunsch 		     0);
1474d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1475d66c374fSTor Egge 		(void) enable_fifo(fdc);
14765b81b6b3SRodney W. Grimes }
14775b81b6b3SRodney W. Grimes 
1478246ed35dSJoerg Wunsch /*
1479246ed35dSJoerg Wunsch  * FDC IO functions, take care of the main status register, timeout
1480246ed35dSJoerg Wunsch  * in case the desired status bits are never set.
1481246ed35dSJoerg Wunsch  */
1482b5e8ce9fSBruce Evans static int
14836182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr)
1484dc5df763SJoerg Wunsch {
1485dc5df763SJoerg Wunsch 	int i, j = 100000;
1486427ccf00SDoug Rabson 	while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1487dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1488dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
14896182fdbdSPeter Wemm 			return fdc_err(fdc, "ready for output in input\n");
1490dc5df763SJoerg Wunsch 	if (j <= 0)
14916182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1492d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1493427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1494dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1495dc5df763SJoerg Wunsch 	*ptr = i;
1496dc5df763SJoerg Wunsch 	return 0;
1497d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1498427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1499dc5df763SJoerg Wunsch 	if (ptr)
1500dc5df763SJoerg Wunsch 		*ptr = i;
1501dc5df763SJoerg Wunsch 	return 0;
1502d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1503dc5df763SJoerg Wunsch }
1504dc5df763SJoerg Wunsch 
1505dc5df763SJoerg Wunsch int
15066182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x)
15075b81b6b3SRodney W. Grimes {
15083b3837dbSRodney W. Grimes 	int i;
15095b81b6b3SRodney W. Grimes 
15103b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
15113b3837dbSRodney W. Grimes 	i = 100000;
1512427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0);
15136182fdbdSPeter Wemm 	if (i <= 0) return fdc_err(fdc, "direction bit not set\n");
15143b3837dbSRodney W. Grimes 
15153b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
15163b3837dbSRodney W. Grimes 	i = 100000;
1517427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0);
151816b04b6aSJoerg Wunsch 	if (i <= 0)
15196182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "output ready timeout\n": 0);
15203b3837dbSRodney W. Grimes 
15213b3837dbSRodney W. Grimes 	/* Send the command and return */
1522427ccf00SDoug Rabson 	fddata_wr(fdc, x);
15233a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
15245b81b6b3SRodney W. Grimes 	return (0);
15255b81b6b3SRodney W. Grimes }
15265b81b6b3SRodney W. Grimes 
1527246ed35dSJoerg Wunsch /*
1528246ed35dSJoerg Wunsch  * Block device driver interface functions (interspersed with even more
1529246ed35dSJoerg Wunsch  * auxiliary functions).
1530246ed35dSJoerg Wunsch  */
1531381fe1aaSGarrett Wollman int
1532b40ce416SJulian Elischer Fdopen(dev_t dev, int flags, int mode, struct thread *td)
15335b81b6b3SRodney W. Grimes {
15345b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
153520a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
15366182fdbdSPeter Wemm 	fd_p	fd;
1537b99f0a4aSAndrew Moore 	fdc_p	fdc;
15381a6bed68SJoerg Wunsch  	int rv, unitattn, dflags;
15395b81b6b3SRodney W. Grimes 
15406182fdbdSPeter Wemm 	if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1541b99f0a4aSAndrew Moore 		return (ENXIO);
15426182fdbdSPeter Wemm 	fdc = fd->fdc;
15431a6bed68SJoerg Wunsch 	if ((fdc == NULL) || (fd->type == FDT_NONE))
1544b99f0a4aSAndrew Moore 		return (ENXIO);
1545b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1546b99f0a4aSAndrew Moore 		return (ENXIO);
15471a6bed68SJoerg Wunsch 	dflags = device_get_flags(fd->dev);
15483e425b96SJulian Elischer 	/*
15491a6bed68SJoerg Wunsch 	 * This is a bit bogus.  It's still possible that e. g. a
15501a6bed68SJoerg Wunsch 	 * descriptor gets inherited to a child, but then it's at
15511a6bed68SJoerg Wunsch 	 * least for the same subdevice.  By checking FD_OPEN here, we
15521a6bed68SJoerg Wunsch 	 * can ensure that a device isn't attempted to be opened with
15531a6bed68SJoerg Wunsch 	 * different densities at the same time where the second open
15541a6bed68SJoerg Wunsch 	 * could clobber the settings from the first one.
15553e425b96SJulian Elischer 	 */
15561a6bed68SJoerg Wunsch 	if (fd->flags & FD_OPEN)
15571a6bed68SJoerg Wunsch 		return (EBUSY);
15581a6bed68SJoerg Wunsch 
15591a6bed68SJoerg Wunsch 	if (type == 0) {
15601a6bed68SJoerg Wunsch 		if (flags & FNONBLOCK) {
15611a6bed68SJoerg Wunsch 			/*
15621a6bed68SJoerg Wunsch 			 * Unfortunately, physio(9) discards its ioflag
15631a6bed68SJoerg Wunsch 			 * argument, thus preventing us from seeing the
15641a6bed68SJoerg Wunsch 			 * IO_NDELAY bit.  So we need to keep track
15651a6bed68SJoerg Wunsch 			 * ourselves.
15661a6bed68SJoerg Wunsch 			 */
15671a6bed68SJoerg Wunsch 			fd->flags |= FD_NONBLOCK;
15681a6bed68SJoerg Wunsch 			fd->ft = 0;
15691a6bed68SJoerg Wunsch 		} else {
15701a6bed68SJoerg Wunsch 			/*
15711a6bed68SJoerg Wunsch 			 * Figure out a unit attention condition.
15721a6bed68SJoerg Wunsch 			 *
15731a6bed68SJoerg Wunsch 			 * If UA has been forced, proceed.
15741a6bed68SJoerg Wunsch 			 *
15751a6bed68SJoerg Wunsch 			 * If motor is off, turn it on for a moment
15761a6bed68SJoerg Wunsch 			 * and select our drive, in order to read the
15771a6bed68SJoerg Wunsch 			 * UA hardware signal.
15781a6bed68SJoerg Wunsch 			 *
15791a6bed68SJoerg Wunsch 			 * If motor is on, and our drive is currently
15801a6bed68SJoerg Wunsch 			 * selected, just read the hardware bit.
15811a6bed68SJoerg Wunsch 			 *
15821a6bed68SJoerg Wunsch 			 * If motor is on, but active for another
15831a6bed68SJoerg Wunsch 			 * drive on that controller, we are lost.  We
15841a6bed68SJoerg Wunsch 			 * cannot risk to deselect the other drive, so
15851a6bed68SJoerg Wunsch 			 * we just assume a forced UA condition to be
15861a6bed68SJoerg Wunsch 			 * on the safe side.
15871a6bed68SJoerg Wunsch 			 */
15881a6bed68SJoerg Wunsch 			unitattn = 0;
15891a6bed68SJoerg Wunsch 			if ((dflags & FD_NO_CHLINE) != 0 ||
15901a6bed68SJoerg Wunsch 			    (fd->flags & FD_UA) != 0) {
15911a6bed68SJoerg Wunsch 				unitattn = 1;
15921a6bed68SJoerg Wunsch 				fd->flags &= ~FD_UA;
15931a6bed68SJoerg Wunsch 			} else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 |
15941a6bed68SJoerg Wunsch 						 FDO_MOEN2 | FDO_MOEN3)) {
15951a6bed68SJoerg Wunsch 				if ((fdc->fdout & FDO_FDSEL) == fd->fdsu)
15961a6bed68SJoerg Wunsch 					unitattn = fdin_rd(fdc) & FDI_DCHG;
15971a6bed68SJoerg Wunsch 				else
15981a6bed68SJoerg Wunsch 					unitattn = 1;
15991a6bed68SJoerg Wunsch 			} else {
16001a6bed68SJoerg Wunsch 				set_motor(fdc, fd->fdsu, TURNON);
16011a6bed68SJoerg Wunsch 				unitattn = fdin_rd(fdc) & FDI_DCHG;
16021a6bed68SJoerg Wunsch 				set_motor(fdc, fd->fdsu, TURNOFF);
1603b39c878eSAndrey A. Chernov 			}
16041a6bed68SJoerg Wunsch 			if (unitattn && (rv = fdautoselect(dev)) != 0)
16051a6bed68SJoerg Wunsch 				return (rv);
16067ca0641bSAndrey A. Chernov 		}
16071a6bed68SJoerg Wunsch 	} else {
16081a6bed68SJoerg Wunsch 		fd->ft = fd->fts + type;
16097ca0641bSAndrey A. Chernov 	}
16106182fdbdSPeter Wemm 	fd->flags |= FD_OPEN;
16113fef646eSJoerg Wunsch 	/*
16123fef646eSJoerg Wunsch 	 * Clearing the DMA overrun counter at open time is a bit messy.
16133fef646eSJoerg Wunsch 	 * Since we're only managing one counter per controller, opening
16143fef646eSJoerg Wunsch 	 * the second drive could mess it up.  Anyway, if the DMA overrun
16153fef646eSJoerg Wunsch 	 * condition is really persistent, it will eventually time out
16163fef646eSJoerg Wunsch 	 * still.  OTOH, clearing it here will ensure we'll at least start
16173fef646eSJoerg Wunsch 	 * trying again after a previous (maybe even long ago) failure.
16183fef646eSJoerg Wunsch 	 * Also, this is merely a stop-gap measure only that should not
16193fef646eSJoerg Wunsch 	 * happen during normal operation, so we can tolerate it to be a
16203fef646eSJoerg Wunsch 	 * bit sloppy about this.
16213fef646eSJoerg Wunsch 	 */
16223fef646eSJoerg Wunsch 	fdc->dma_overruns = 0;
16235f830ea2SJoerg Wunsch 
16245b81b6b3SRodney W. Grimes 	return 0;
16255b81b6b3SRodney W. Grimes }
16265b81b6b3SRodney W. Grimes 
1627381fe1aaSGarrett Wollman int
1628b40ce416SJulian Elischer fdclose(dev_t dev, int flags, int mode, struct thread *td)
16295b81b6b3SRodney W. Grimes {
16305b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
16316182fdbdSPeter Wemm 	struct fd_data *fd;
1632b99f0a4aSAndrew Moore 
16336182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
16341a6bed68SJoerg Wunsch 	fd->flags &= ~(FD_OPEN | FD_NONBLOCK);
16352995d110SJoerg Wunsch 	fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1636dc16046fSJoerg Wunsch 
16375b81b6b3SRodney W. Grimes 	return (0);
16385b81b6b3SRodney W. Grimes }
16395b81b6b3SRodney W. Grimes 
16403a2f7427SDavid Greenman void
16418177437dSPoul-Henning Kamp fdstrategy(struct bio *bp)
16423a2f7427SDavid Greenman {
1643fb35bd37SJoerg Wunsch 	long blknum, nblocks;
16443a2f7427SDavid Greenman  	int	s;
16453a2f7427SDavid Greenman  	fdu_t	fdu;
16463a2f7427SDavid Greenman  	fdc_p	fdc;
16473a2f7427SDavid Greenman  	fd_p	fd;
16483a2f7427SDavid Greenman 	size_t	fdblk;
16493a2f7427SDavid Greenman 
16508177437dSPoul-Henning Kamp  	fdu = FDUNIT(minor(bp->bio_dev));
16516182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
16526182fdbdSPeter Wemm 	if (fd == 0)
16536182fdbdSPeter Wemm 		panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
16548177437dSPoul-Henning Kamp 		      (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev));
16553a2f7427SDavid Greenman 	fdc = fd->fdc;
16561a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE || fd->ft == 0) {
16578177437dSPoul-Henning Kamp 		bp->bio_error = ENXIO;
16588177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
16593b178206SWarner Losh 		goto bad;
166064860614SJoerg Wunsch 	}
1661d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
1662f664aeeeSJoerg Wunsch 	if (bp->bio_cmd != BIO_FORMAT && bp->bio_cmd != BIO_RDSECTID) {
16631a6bed68SJoerg Wunsch 		if (fd->flags & FD_NONBLOCK) {
16641a6bed68SJoerg Wunsch 			bp->bio_error = EAGAIN;
16651a6bed68SJoerg Wunsch 			bp->bio_flags |= BIO_ERROR;
16661a6bed68SJoerg Wunsch 			goto bad;
16671a6bed68SJoerg Wunsch 		}
16688177437dSPoul-Henning Kamp 		if (bp->bio_blkno < 0) {
1669dc5df763SJoerg Wunsch 			printf(
16706a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
16718177437dSPoul-Henning Kamp 			       fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
16728177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
16738177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
16743a2f7427SDavid Greenman 			goto bad;
16753a2f7427SDavid Greenman 		}
16768177437dSPoul-Henning Kamp 		if ((bp->bio_bcount % fdblk) != 0) {
16778177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
16788177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
16793a2f7427SDavid Greenman 			goto bad;
16803a2f7427SDavid Greenman 		}
16813a2f7427SDavid Greenman 	}
16823a2f7427SDavid Greenman 
16833a2f7427SDavid Greenman 	/*
16843a2f7427SDavid Greenman 	 * Set up block calculations.
16853a2f7427SDavid Greenman 	 */
16868177437dSPoul-Henning Kamp 	if (bp->bio_blkno > 20000000) {
1687bb6382faSJoerg Wunsch 		/*
1688bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1689bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1690bb6382faSJoerg Wunsch 		 */
16918177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
16928177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
16933a2f7427SDavid Greenman 		goto bad;
16943a2f7427SDavid Greenman 	}
1695fb35bd37SJoerg Wunsch 	blknum = bp->bio_blkno * DEV_BSIZE / fdblk;
1696bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1697fb35bd37SJoerg Wunsch 	if (blknum + bp->bio_bcount / fdblk > nblocks) {
1698fb35bd37SJoerg Wunsch 		if (blknum >= nblocks) {
1699fb35bd37SJoerg Wunsch 			if (bp->bio_cmd == BIO_READ)
1700fb35bd37SJoerg Wunsch 				bp->bio_resid = bp->bio_bcount;
1701fb35bd37SJoerg Wunsch 			else {
1702fb35bd37SJoerg Wunsch 				bp->bio_error = ENOSPC;
17038177437dSPoul-Henning Kamp 				bp->bio_flags |= BIO_ERROR;
1704bb6382faSJoerg Wunsch 			}
1705fb35bd37SJoerg Wunsch 			goto bad;	/* not always bad, but EOF */
1706fb35bd37SJoerg Wunsch 		}
1707fb35bd37SJoerg Wunsch 		bp->bio_bcount = (nblocks - blknum) * fdblk;
1708bb6382faSJoerg Wunsch 	}
17098177437dSPoul-Henning Kamp  	bp->bio_pblkno = bp->bio_blkno;
17103a2f7427SDavid Greenman 	s = splbio();
17118177437dSPoul-Henning Kamp 	bioqdisksort(&fdc->head, bp);
17126182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1713b2dfb1f9SJustin T. Gibbs 	devstat_start_transaction(&fd->device_stats);
17145f830ea2SJoerg Wunsch 	device_busy(fd->dev);
17156182fdbdSPeter Wemm 	fdstart(fdc);
17163a2f7427SDavid Greenman 	splx(s);
17173a2f7427SDavid Greenman 	return;
17183a2f7427SDavid Greenman 
17193a2f7427SDavid Greenman bad:
17203a2f7427SDavid Greenman 	biodone(bp);
17213a2f7427SDavid Greenman }
17223a2f7427SDavid Greenman 
1723246ed35dSJoerg Wunsch /*
1724246ed35dSJoerg Wunsch  * fdstart
1725246ed35dSJoerg Wunsch  *
1726246ed35dSJoerg Wunsch  * We have just queued something.  If the controller is not busy
1727246ed35dSJoerg Wunsch  * then simulate the case where it has just finished a command
1728246ed35dSJoerg Wunsch  * So that it (the interrupt routine) looks on the queue for more
1729246ed35dSJoerg Wunsch  * work to do and picks up what we just added.
1730246ed35dSJoerg Wunsch  *
1731246ed35dSJoerg Wunsch  * If the controller is already busy, we need do nothing, as it
1732246ed35dSJoerg Wunsch  * will pick up our work when the present work completes.
1733246ed35dSJoerg Wunsch  */
1734381fe1aaSGarrett Wollman static void
17356182fdbdSPeter Wemm fdstart(struct fdc_data *fdc)
17365b81b6b3SRodney W. Grimes {
17375b81b6b3SRodney W. Grimes 	int s;
17385b81b6b3SRodney W. Grimes 
17395b81b6b3SRodney W. Grimes 	s = splbio();
17406182fdbdSPeter Wemm 	if(fdc->state == DEVIDLE)
17415b81b6b3SRodney W. Grimes 	{
17426182fdbdSPeter Wemm 		fdc_intr(fdc);
17435b81b6b3SRodney W. Grimes 	}
17445b81b6b3SRodney W. Grimes 	splx(s);
17455b81b6b3SRodney W. Grimes }
17465b81b6b3SRodney W. Grimes 
1747381fe1aaSGarrett Wollman static void
17486182fdbdSPeter Wemm fd_iotimeout(void *xfdc)
17495b81b6b3SRodney W. Grimes {
17505c1a1eaeSBruce Evans  	fdc_p fdc;
1751f5f7ba03SJordan K. Hubbard 	int s;
17525b81b6b3SRodney W. Grimes 
17536182fdbdSPeter Wemm 	fdc = xfdc;
17545c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
17555b81b6b3SRodney W. Grimes 
17563a2f7427SDavid Greenman 	/*
17573a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
17583a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
17593a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
17603a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
17613a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
17625c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
17635c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
17645c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
17655c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
17663a2f7427SDavid Greenman 	 */
17673a2f7427SDavid Greenman 	s = splbio();
17685c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
17695c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
17705c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
17716182fdbdSPeter Wemm 	fdc_intr(fdc);
1772f5f7ba03SJordan K. Hubbard 	splx(s);
17735b81b6b3SRodney W. Grimes }
17745b81b6b3SRodney W. Grimes 
1775246ed35dSJoerg Wunsch /* Just ensure it has the right spl. */
1776381fe1aaSGarrett Wollman static void
17776182fdbdSPeter Wemm fd_pseudointr(void *xfdc)
17785b81b6b3SRodney W. Grimes {
17795b81b6b3SRodney W. Grimes 	int	s;
17803a2f7427SDavid Greenman 
17815b81b6b3SRodney W. Grimes 	s = splbio();
17826182fdbdSPeter Wemm 	fdc_intr(xfdc);
17835b81b6b3SRodney W. Grimes 	splx(s);
17845b81b6b3SRodney W. Grimes }
17855b81b6b3SRodney W. Grimes 
1786246ed35dSJoerg Wunsch /*
1787246ed35dSJoerg Wunsch  * fdc_intr
1788246ed35dSJoerg Wunsch  *
1789246ed35dSJoerg Wunsch  * Keep calling the state machine until it returns a 0.
1790246ed35dSJoerg Wunsch  * Always called at splbio.
1791246ed35dSJoerg Wunsch  */
1792fe310de8SBruce Evans static void
17936182fdbdSPeter Wemm fdc_intr(void *xfdc)
17945b81b6b3SRodney W. Grimes {
17956182fdbdSPeter Wemm 	fdc_p fdc = xfdc;
17966182fdbdSPeter Wemm 	while(fdstate(fdc))
1797381fe1aaSGarrett Wollman 		;
17985b81b6b3SRodney W. Grimes }
17995b81b6b3SRodney W. Grimes 
180069acd21dSWarner Losh /*
1801246ed35dSJoerg Wunsch  * Magic pseudo-DMA initialization for YE FDC. Sets count and
1802246ed35dSJoerg Wunsch  * direction.
180369acd21dSWarner Losh  */
18043b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \
18053b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port,	 \
18063b178206SWarner Losh 	    ((cnt)-1) & 0xff);						 \
18073b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
18083b178206SWarner Losh 	    ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
180969acd21dSWarner Losh 
181069acd21dSWarner Losh /*
1811246ed35dSJoerg Wunsch  * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy.
181269acd21dSWarner Losh  */
18134c34deeeSJoerg Wunsch static int
18144c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
181569acd21dSWarner Losh {
181669acd21dSWarner Losh 	u_char *cptr = (u_char *)addr;
181769acd21dSWarner Losh 
181821144e3bSPoul-Henning Kamp 	if (flags == BIO_READ) {
181969acd21dSWarner Losh 		if (fdc->state != PIOREAD) {
182069acd21dSWarner Losh 			fdc->state = PIOREAD;
182169acd21dSWarner Losh 			return(0);
182264860614SJoerg Wunsch 		}
18233b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
18243b178206SWarner Losh 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
18253b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
182669acd21dSWarner Losh 	} else {
18273b178206SWarner Losh 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
18283b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
18293b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
183064860614SJoerg Wunsch 	}
183169acd21dSWarner Losh 	return(1);
183269acd21dSWarner Losh }
183369acd21dSWarner Losh 
1834246ed35dSJoerg Wunsch /*
18351a6bed68SJoerg Wunsch  * Try figuring out the density of the media present in our device.
18361a6bed68SJoerg Wunsch  */
18371a6bed68SJoerg Wunsch static int
18381a6bed68SJoerg Wunsch fdautoselect(dev_t dev)
18391a6bed68SJoerg Wunsch {
18401a6bed68SJoerg Wunsch 	fdu_t fdu;
18411a6bed68SJoerg Wunsch  	fd_p fd;
18421a6bed68SJoerg Wunsch 	struct fd_type *fdtp;
18431a6bed68SJoerg Wunsch 	struct fdc_readid id;
18441a6bed68SJoerg Wunsch 	int i, n, oopts, rv;
18451a6bed68SJoerg Wunsch 
18461a6bed68SJoerg Wunsch  	fdu = FDUNIT(minor(dev));
18471a6bed68SJoerg Wunsch 	fd = devclass_get_softc(fd_devclass, fdu);
18481a6bed68SJoerg Wunsch 
18491a6bed68SJoerg Wunsch 	switch (fd->type) {
18501a6bed68SJoerg Wunsch 	default:
18511a6bed68SJoerg Wunsch 		return (ENXIO);
18521a6bed68SJoerg Wunsch 
18531a6bed68SJoerg Wunsch 	case FDT_360K:
18541a6bed68SJoerg Wunsch 	case FDT_720K:
18551a6bed68SJoerg Wunsch 		/* no autoselection on those drives */
18561a6bed68SJoerg Wunsch 		fd->ft = fd_native_types + fd->type;
18571a6bed68SJoerg Wunsch 		return (0);
18581a6bed68SJoerg Wunsch 
18591a6bed68SJoerg Wunsch 	case FDT_12M:
18601a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_12m;
18611a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_12m / sizeof(struct fd_type);
18621a6bed68SJoerg Wunsch 		break;
18631a6bed68SJoerg Wunsch 
18641a6bed68SJoerg Wunsch 	case FDT_144M:
18651a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_144m;
18661a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_144m / sizeof(struct fd_type);
18671a6bed68SJoerg Wunsch 		break;
18681a6bed68SJoerg Wunsch 
18691a6bed68SJoerg Wunsch 	case FDT_288M:
18701a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_288m;
18711a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_288m / sizeof(struct fd_type);
18721a6bed68SJoerg Wunsch 		break;
18731a6bed68SJoerg Wunsch 	}
18741a6bed68SJoerg Wunsch 
18751a6bed68SJoerg Wunsch 	/*
18761a6bed68SJoerg Wunsch 	 * Try reading sector ID fields, first at cylinder 0, head 0,
18771a6bed68SJoerg Wunsch 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
18781a6bed68SJoerg Wunsch 	 * since for 5.25in DD media in a HD drive, there are no data
18791a6bed68SJoerg Wunsch 	 * to read (2 step pulses per media cylinder required).  For
18801a6bed68SJoerg Wunsch 	 * two-sided media, the second probe always goes to head 1, so
18811a6bed68SJoerg Wunsch 	 * we can tell them apart from single-sided media.  As a
18821a6bed68SJoerg Wunsch 	 * side-effect this means that single-sided media should be
18831a6bed68SJoerg Wunsch 	 * mentioned in the search list after two-sided media of an
18841a6bed68SJoerg Wunsch 	 * otherwise identical density.  Media with a different number
18851a6bed68SJoerg Wunsch 	 * of sectors per track but otherwise identical parameters
18861a6bed68SJoerg Wunsch 	 * cannot be distinguished at all.
18871a6bed68SJoerg Wunsch 	 *
18881a6bed68SJoerg Wunsch 	 * If we successfully read an ID field on both cylinders where
18891a6bed68SJoerg Wunsch 	 * the recorded values match our expectation, we are done.
18901a6bed68SJoerg Wunsch 	 * Otherwise, we try the next density entry from the table.
18911a6bed68SJoerg Wunsch 	 *
18921a6bed68SJoerg Wunsch 	 * Stepping to cylinder 2 has the side-effect of clearing the
18931a6bed68SJoerg Wunsch 	 * unit attention bit.
18941a6bed68SJoerg Wunsch 	 */
18951a6bed68SJoerg Wunsch 	oopts = fd->options;
18961a6bed68SJoerg Wunsch 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
18971a6bed68SJoerg Wunsch 	for (i = 0; i < n; i++, fdtp++) {
18981a6bed68SJoerg Wunsch 		fd->ft = fdtp;
18991a6bed68SJoerg Wunsch 
19001a6bed68SJoerg Wunsch 		id.cyl = id.head = 0;
19011a6bed68SJoerg Wunsch 		rv = fdmisccmd(dev, BIO_RDSECTID, &id);
19021a6bed68SJoerg Wunsch 		if (rv != 0)
19031a6bed68SJoerg Wunsch 			continue;
19041a6bed68SJoerg Wunsch 		if (id.cyl != 0 || id.head != 0 ||
19051a6bed68SJoerg Wunsch 		    id.secshift != fdtp->secsize)
19061a6bed68SJoerg Wunsch 			continue;
19071a6bed68SJoerg Wunsch 		id.cyl = 2;
19081a6bed68SJoerg Wunsch 		id.head = fd->ft->heads - 1;
19091a6bed68SJoerg Wunsch 		rv = fdmisccmd(dev, BIO_RDSECTID, &id);
19101a6bed68SJoerg Wunsch 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
19111a6bed68SJoerg Wunsch 		    id.secshift != fdtp->secsize)
19121a6bed68SJoerg Wunsch 			continue;
19131a6bed68SJoerg Wunsch 		if (rv == 0)
19141a6bed68SJoerg Wunsch 			break;
19151a6bed68SJoerg Wunsch 	}
19161a6bed68SJoerg Wunsch 
19171a6bed68SJoerg Wunsch 	fd->options = oopts;
19181a6bed68SJoerg Wunsch 	if (i == n) {
19191a6bed68SJoerg Wunsch 		device_printf(fd->dev, "autoselection failed\n");
19201a6bed68SJoerg Wunsch 		fd->ft = 0;
19211a6bed68SJoerg Wunsch 		return (EIO);
19221a6bed68SJoerg Wunsch 	} else {
19231a6bed68SJoerg Wunsch 		device_printf(fd->dev, "autoselected %d KB medium\n",
19241a6bed68SJoerg Wunsch 			      fd->ft->size / 2);
19251a6bed68SJoerg Wunsch 		return (0);
19261a6bed68SJoerg Wunsch 	}
19271a6bed68SJoerg Wunsch }
19281a6bed68SJoerg Wunsch 
19291a6bed68SJoerg Wunsch 
19301a6bed68SJoerg Wunsch /*
1931246ed35dSJoerg Wunsch  * The controller state machine.
1932246ed35dSJoerg Wunsch  *
1933246ed35dSJoerg Wunsch  * If it returns a non zero value, it should be called again immediately.
1934246ed35dSJoerg Wunsch  */
19353a2f7427SDavid Greenman static int
19366182fdbdSPeter Wemm fdstate(fdc_p fdc)
19375b81b6b3SRodney W. Grimes {
193864860614SJoerg Wunsch 	struct fdc_readid *idp;
1939fb35bd37SJoerg Wunsch 	int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
19401a6bed68SJoerg Wunsch 	int st0, cyl, st3, idf, ne7cmd, mfm, steptrac;
1941fb35bd37SJoerg Wunsch 	unsigned long blknum;
19425b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
19435b81b6b3SRodney W. Grimes 	fd_p fd;
19448177437dSPoul-Henning Kamp 	register struct bio *bp;
1945b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
19463a2f7427SDavid Greenman 	size_t fdblk;
19475b81b6b3SRodney W. Grimes 
1948e93e63cbSBruce Evans 	bp = fdc->bp;
1949e93e63cbSBruce Evans 	if (bp == NULL) {
19508177437dSPoul-Henning Kamp 		bp = bioq_first(&fdc->head);
1951e93e63cbSBruce Evans 		if (bp != NULL) {
19528177437dSPoul-Henning Kamp 			bioq_remove(&fdc->head, bp);
1953e93e63cbSBruce Evans 			fdc->bp = bp;
1954e93e63cbSBruce Evans 		}
1955e93e63cbSBruce Evans 	}
1956e93e63cbSBruce Evans 	if (bp == NULL) {
1957246ed35dSJoerg Wunsch 		/*
1958246ed35dSJoerg Wunsch 		 * Nothing left for this controller to do,
1959246ed35dSJoerg Wunsch 		 * force into the IDLE state.
1960246ed35dSJoerg Wunsch 		 */
19615b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
19626182fdbdSPeter Wemm 		if (fdc->fd) {
1963b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev,
1964b6e5f28eSPeter Wemm 			    "unexpected valid fd pointer\n");
19655b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
19665b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
19675b81b6b3SRodney W. Grimes 		}
19686182fdbdSPeter Wemm 		TRACE1("[fdc%d IDLE]", fdc->fdcu);
19695b81b6b3SRodney W. Grimes  		return (0);
19705b81b6b3SRodney W. Grimes 	}
19718177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
19726182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
19733a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1974b6e5f28eSPeter Wemm 	if (fdc->fd && (fd != fdc->fd))
1975b6e5f28eSPeter Wemm 		device_printf(fd->dev, "confused fd pointers\n");
19768177437dSPoul-Henning Kamp 	read = bp->bio_cmd == BIO_READ;
19771a6bed68SJoerg Wunsch 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
19781a6bed68SJoerg Wunsch 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
197956a23089SPoul-Henning Kamp 	if (read)
198056a23089SPoul-Henning Kamp 		idf = ISADMA_READ;
198156a23089SPoul-Henning Kamp 	else
198256a23089SPoul-Henning Kamp 		idf = ISADMA_WRITE;
198364860614SJoerg Wunsch 	format = bp->bio_cmd == BIO_FORMAT;
1984f664aeeeSJoerg Wunsch 	rdsectid = bp->bio_cmd == BIO_RDSECTID;
1985fb35bd37SJoerg Wunsch 	if (format)
19868177437dSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
19875b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
19885b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
19895b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
19906182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle);
19916182fdbdSPeter Wemm 	fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
19925b81b6b3SRodney W. Grimes 	switch (fdc->state)
19935b81b6b3SRodney W. Grimes 	{
19945b81b6b3SRodney W. Grimes 	case DEVIDLE:
19955b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
19965b81b6b3SRodney W. Grimes 		fdc->retry = 0;
19975b81b6b3SRodney W. Grimes 		fd->skip = 0;
19985b81b6b3SRodney W. Grimes 		fdc->fd = fd;
19995b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
20005f830ea2SJoerg Wunsch 		fdc->fdctl_wr(fdc, fd->ft->trans);
20013a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
2002246ed35dSJoerg Wunsch 		/*
2003246ed35dSJoerg Wunsch 		 * If the next drive has a motor startup pending, then
2004246ed35dSJoerg Wunsch 		 * it will start up in its own good time.
2005246ed35dSJoerg Wunsch 		 */
20066182fdbdSPeter Wemm 		if(fd->flags & FD_MOTOR_WAIT) {
20075b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
2008246ed35dSJoerg Wunsch 			return (0); /* will return later */
20095b81b6b3SRodney W. Grimes 		}
2010246ed35dSJoerg Wunsch 		/*
2011246ed35dSJoerg Wunsch 		 * Maybe if it's not starting, it SHOULD be starting.
2012246ed35dSJoerg Wunsch 		 */
20135b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
20145b81b6b3SRodney W. Grimes 		{
20155b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
20166182fdbdSPeter Wemm 			fd_turnon(fd);
2017246ed35dSJoerg Wunsch 			return (0); /* will return later */
20185b81b6b3SRodney W. Grimes 		}
20195b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
20205b81b6b3SRodney W. Grimes 		{
20216182fdbdSPeter Wemm 			set_motor(fdc, fd->fdsu, TURNON);
20225b81b6b3SRodney W. Grimes 		}
20235c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
20245c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
20255c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
20265c1a1eaeSBruce Evans 		} else
20275e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
2028246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2029fb35bd37SJoerg Wunsch 
20305b81b6b3SRodney W. Grimes 	case DOSEEK:
2031fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
2032fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2033fb35bd37SJoerg Wunsch 		if (cylinder == fd->track)
20345b81b6b3SRodney W. Grimes 		{
20355b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
2036246ed35dSJoerg Wunsch 			return (1); /* will return immediately */
20375b81b6b3SRodney W. Grimes 		}
20386182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK,
20391a6bed68SJoerg Wunsch 			   fd->fdsu, cylinder * steptrac, 0))
2040dc8603e3SJoerg Wunsch 		{
2041dc8603e3SJoerg Wunsch 			/*
2042246ed35dSJoerg Wunsch 			 * Seek command not accepted, looks like
2043dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
2044dc8603e3SJoerg Wunsch 			 */
2045dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
20466182fdbdSPeter Wemm 			return(retrier(fdc));
2047dc8603e3SJoerg Wunsch 		}
2048dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
20495b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
20505b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
2051fb35bd37SJoerg Wunsch 
20525b81b6b3SRodney W. Grimes 	case SEEKWAIT:
20535b81b6b3SRodney W. Grimes 		/* allow heads to settle */
20546182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 16);
20555b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
20565b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
2057fb35bd37SJoerg Wunsch 
2058246ed35dSJoerg Wunsch 	case SEEKCOMPLETE : /* seek done, start DMA */
2059fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
2060fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2061fb35bd37SJoerg Wunsch 
2062246ed35dSJoerg Wunsch 		/* Make sure seek really happened. */
20636182fdbdSPeter Wemm 		if(fd->track == FD_NO_TRACK) {
20641a6bed68SJoerg Wunsch 			int descyl = cylinder * steptrac;
20653a2f7427SDavid Greenman 			do {
20663a2f7427SDavid Greenman 				/*
2067dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
2068dc5df763SJoerg Wunsch 				 * which cannot really happen since the
2069dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
2070dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
2071dc5df763SJoerg Wunsch 				 * line, so do one of the following:
2072dc5df763SJoerg Wunsch 				 *
2073dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
2074dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
2075dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
2076dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
2077dc5df763SJoerg Wunsch 				 * real interrupt condition.
2078dc5df763SJoerg Wunsch 				 *
2079dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
2080dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
2081dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
2082dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
2083dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
2084dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
2085dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
20863a2f7427SDavid Greenman 				 */
2087dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
2088dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
2089246ed35dSJoerg Wunsch 					return (0); /* will return later */
2090dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
2091dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2092246ed35dSJoerg Wunsch 					return (0); /* hope for a real intr */
20933a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2094dc5df763SJoerg Wunsch 
20956182fdbdSPeter Wemm 			if (0 == descyl) {
2096dc5df763SJoerg Wunsch 				int failed = 0;
20973a2f7427SDavid Greenman 				/*
20983a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
20993a2f7427SDavid Greenman 				 * really there
21003a2f7427SDavid Greenman 				 */
2101dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
2102dc5df763SJoerg Wunsch 					failed = 1;
21033a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
21043a2f7427SDavid Greenman 					printf(
21053a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
21063a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
2107dc5df763SJoerg Wunsch 					failed = 1;
2108dc5df763SJoerg Wunsch 				}
2109dc5df763SJoerg Wunsch 
21106182fdbdSPeter Wemm 				if (failed) {
21113a2f7427SDavid Greenman 					if(fdc->retry < 3)
21123a2f7427SDavid Greenman 						fdc->retry = 3;
21136182fdbdSPeter Wemm 					return (retrier(fdc));
21143a2f7427SDavid Greenman 				}
21153a2f7427SDavid Greenman 			}
2116dc5df763SJoerg Wunsch 
21176182fdbdSPeter Wemm 			if (cyl != descyl) {
21183a2f7427SDavid Greenman 				printf(
21193a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
21202d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
2121e5d7d243SBruce Evans 				if (fdc->retry < 3)
2122e5d7d243SBruce Evans 					fdc->retry = 3;
21236182fdbdSPeter Wemm 				return (retrier(fdc));
21245b81b6b3SRodney W. Grimes 			}
21255b81b6b3SRodney W. Grimes 		}
21265b81b6b3SRodney W. Grimes 
2127fb35bd37SJoerg Wunsch 		fd->track = cylinder;
2128fb35bd37SJoerg Wunsch 		if (format)
2129fb35bd37SJoerg Wunsch 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
2130fb35bd37SJoerg Wunsch 			    - (char *)finfo;
2131250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
21328177437dSPoul-Henning Kamp 			isa_dmastart(idf, bp->bio_data+fd->skip,
21338177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
2134fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
21355b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
21365b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
21375b81b6b3SRodney W. Grimes 		head = sec / sectrac;
21385b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
21391a6bed68SJoerg Wunsch 		if (head != 0 && fd->ft->offset_side2 != 0)
21401a6bed68SJoerg Wunsch 			sec += fd->ft->offset_side2;
21413a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
21423a2f7427SDavid Greenman 
2143250300ebSJoerg Wunsch 		if(format || !(read || rdsectid))
21443a2f7427SDavid Greenman 		{
21453a2f7427SDavid Greenman 			/* make sure the drive is writable */
2146dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
2147dc8603e3SJoerg Wunsch 			{
2148dc8603e3SJoerg Wunsch 				/* stuck controller? */
21495f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
215056a23089SPoul-Henning Kamp 					isa_dmadone(idf,
21518177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
21528177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
21535c1a1eaeSBruce Evans 						    fdc->dmachan);
2154dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
21556182fdbdSPeter Wemm 				return (retrier(fdc));
2156dc8603e3SJoerg Wunsch 			}
21573a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
21583a2f7427SDavid Greenman 			{
21593a2f7427SDavid Greenman 				/*
21603a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
21613a2f7427SDavid Greenman 				 * in order to force the current operation
21623a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
21633a2f7427SDavid Greenman 				 * error - all error handling is done
21643a2f7427SDavid Greenman 				 * by the retrier()
21653a2f7427SDavid Greenman 				 */
21663a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
21673a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
21683a2f7427SDavid Greenman 				fdc->status[2] = 0;
21693a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
21703a2f7427SDavid Greenman 				fdc->status[4] = head;
21713a2f7427SDavid Greenman 				fdc->status[5] = sec;
21723a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
21733a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
2174246ed35dSJoerg Wunsch 				return (1); /* will return immediately */
21753a2f7427SDavid Greenman 			}
21763a2f7427SDavid Greenman 		}
21775b81b6b3SRodney W. Grimes 
21786182fdbdSPeter Wemm 		if (format) {
21791a6bed68SJoerg Wunsch 			ne7cmd = NE7CMD_FORMAT | mfm;
21805f830ea2SJoerg Wunsch 			if (fdc->flags & FDC_NODMA) {
21815f830ea2SJoerg Wunsch 				/*
21825f830ea2SJoerg Wunsch 				 * This seems to be necessary for
21835f830ea2SJoerg Wunsch 				 * whatever obscure reason; if we omit
21845f830ea2SJoerg Wunsch 				 * it, we end up filling the sector ID
21855f830ea2SJoerg Wunsch 				 * fields of the newly formatted track
21865f830ea2SJoerg Wunsch 				 * entirely with garbage, causing
21875f830ea2SJoerg Wunsch 				 * `wrong cylinder' errors all over
21885f830ea2SJoerg Wunsch 				 * the place when trying to read them
21895f830ea2SJoerg Wunsch 				 * back.
21905f830ea2SJoerg Wunsch 				 *
21915f830ea2SJoerg Wunsch 				 * Umpf.
21925f830ea2SJoerg Wunsch 				 */
21938177437dSPoul-Henning Kamp 				SET_BCDR(fdc, 1, bp->bio_bcount, 0);
21945f830ea2SJoerg Wunsch 
21958177437dSPoul-Henning Kamp 				(void)fdcpio(fdc,bp->bio_cmd,
21968177437dSPoul-Henning Kamp 					bp->bio_data+fd->skip,
21978177437dSPoul-Henning Kamp 					bp->bio_bcount);
21985f830ea2SJoerg Wunsch 
21995f830ea2SJoerg Wunsch 			}
2200b39c878eSAndrey A. Chernov 			/* formatting */
22011a6bed68SJoerg Wunsch 			if(fd_cmd(fdc, 6,  ne7cmd, head << 2 | fdu,
2202dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
2203dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
2204dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
22056182fdbdSPeter Wemm 				  finfo->fd_formb_fillbyte, 0)) {
2206dc8603e3SJoerg Wunsch 				/* controller fell over */
22075f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
220856a23089SPoul-Henning Kamp 					isa_dmadone(idf,
22098177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
22108177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
22115c1a1eaeSBruce Evans 						    fdc->dmachan);
2212dc8603e3SJoerg Wunsch 				fdc->retry = 6;
22136182fdbdSPeter Wemm 				return (retrier(fdc));
2214dc8603e3SJoerg Wunsch 			}
2215250300ebSJoerg Wunsch 		} else if (rdsectid) {
22161a6bed68SJoerg Wunsch 			ne7cmd = NE7CMD_READID | mfm;
22171a6bed68SJoerg Wunsch 			if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) {
2218250300ebSJoerg Wunsch 				/* controller jamming */
2219250300ebSJoerg Wunsch 				fdc->retry = 6;
2220250300ebSJoerg Wunsch 				return (retrier(fdc));
2221250300ebSJoerg Wunsch 			}
22226182fdbdSPeter Wemm 		} else {
2223250300ebSJoerg Wunsch 			/* read or write operation */
22241a6bed68SJoerg Wunsch 			ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm;
22253b178206SWarner Losh 			if (fdc->flags & FDC_NODMA) {
222669acd21dSWarner Losh 				/*
2227246ed35dSJoerg Wunsch 				 * This seems to be necessary even when
2228246ed35dSJoerg Wunsch 				 * reading data.
222969acd21dSWarner Losh 				 */
22303b178206SWarner Losh 				SET_BCDR(fdc, 1, fdblk, 0);
223169acd21dSWarner Losh 
223269acd21dSWarner Losh 				/*
2233246ed35dSJoerg Wunsch 				 * Perform the write pseudo-DMA before
2234246ed35dSJoerg Wunsch 				 * the WRITE command is sent.
223569acd21dSWarner Losh 				 */
223669acd21dSWarner Losh 				if (!read)
22378177437dSPoul-Henning Kamp 					(void)fdcpio(fdc,bp->bio_cmd,
22388177437dSPoul-Henning Kamp 					    bp->bio_data+fd->skip,
223969acd21dSWarner Losh 					    fdblk);
224069acd21dSWarner Losh 			}
22416182fdbdSPeter Wemm 			if (fd_cmd(fdc, 9,
22421a6bed68SJoerg Wunsch 				   ne7cmd,
2243dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
2244dc5df763SJoerg Wunsch 				   fd->track,        /* track */
2245dc5df763SJoerg Wunsch 				   head,
2246dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
2247dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
2248dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
2249dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
2250dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
22516182fdbdSPeter Wemm 				   0)) {
2252dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
22535f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
225456a23089SPoul-Henning Kamp 					isa_dmadone(idf,
22558177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
22568177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
22575c1a1eaeSBruce Evans 						    fdc->dmachan);
2258dc8603e3SJoerg Wunsch 				fdc->retry = 6;
22596182fdbdSPeter Wemm 				return (retrier(fdc));
22605b81b6b3SRodney W. Grimes 			}
2261b39c878eSAndrey A. Chernov 		}
2262250300ebSJoerg Wunsch 		if (!rdsectid && (fdc->flags & FDC_NODMA))
226369acd21dSWarner Losh 			/*
2264246ed35dSJoerg Wunsch 			 * If this is a read, then simply await interrupt
2265246ed35dSJoerg Wunsch 			 * before performing PIO.
226669acd21dSWarner Losh 			 */
22678177437dSPoul-Henning Kamp 			if (read && !fdcpio(fdc,bp->bio_cmd,
22688177437dSPoul-Henning Kamp 			    bp->bio_data+fd->skip,fdblk)) {
22693b178206SWarner Losh 				fd->tohandle = timeout(fd_iotimeout, fdc, hz);
227069acd21dSWarner Losh 				return(0);      /* will return later */
227164860614SJoerg Wunsch 			}
227269acd21dSWarner Losh 
227369acd21dSWarner Losh 		/*
2274246ed35dSJoerg Wunsch 		 * Write (or format) operation will fall through and
2275246ed35dSJoerg Wunsch 		 * await completion interrupt.
227669acd21dSWarner Losh 		 */
22775b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
22786182fdbdSPeter Wemm 		fd->tohandle = timeout(fd_iotimeout, fdc, hz);
22795b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2280fb35bd37SJoerg Wunsch 
228169acd21dSWarner Losh 	case PIOREAD:
228269acd21dSWarner Losh 		/*
2283246ed35dSJoerg Wunsch 		 * Actually perform the PIO read.  The IOCOMPLETE case
228469acd21dSWarner Losh 		 * removes the timeout for us.
228569acd21dSWarner Losh 		 */
22868177437dSPoul-Henning Kamp 		(void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
228769acd21dSWarner Losh 		fdc->state = IOCOMPLETE;
228869acd21dSWarner Losh 		/* FALLTHROUGH */
2289246ed35dSJoerg Wunsch 	case IOCOMPLETE: /* IO done, post-analyze */
22906182fdbdSPeter Wemm 		untimeout(fd_iotimeout, fdc, fd->tohandle);
2291dc5df763SJoerg Wunsch 
229264860614SJoerg Wunsch 		if (fd_read_status(fdc)) {
2293250300ebSJoerg Wunsch 			if (!rdsectid && !(fdc->flags & FDC_NODMA))
22948177437dSPoul-Henning Kamp 				isa_dmadone(idf, bp->bio_data + fd->skip,
22958177437dSPoul-Henning Kamp 					    format ? bp->bio_bcount : fdblk,
22965c1a1eaeSBruce Evans 					    fdc->dmachan);
2297dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
2298dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
22996182fdbdSPeter Wemm 			return (retrier(fdc));
23005b81b6b3SRodney W. Grimes   		}
2301dc5df763SJoerg Wunsch 
23023a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
2303dc5df763SJoerg Wunsch 
23043a2f7427SDavid Greenman 		/* FALLTHROUGH */
23053a2f7427SDavid Greenman 	case IOTIMEDOUT:
2306250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
23078177437dSPoul-Henning Kamp 			isa_dmadone(idf, bp->bio_data + fd->skip,
23088177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
23096182fdbdSPeter Wemm 		if (fdc->status[0] & NE7_ST0_IC) {
23103a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
23113a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
2312b39c878eSAndrey A. Chernov                                 /*
23133fef646eSJoerg Wunsch 				 * DMA overrun. Someone hogged the bus and
23143fef646eSJoerg Wunsch 				 * didn't release it in time for the next
23153fef646eSJoerg Wunsch 				 * FDC transfer.
23163fef646eSJoerg Wunsch 				 *
23173fef646eSJoerg Wunsch 				 * We normally restart this without bumping
23183fef646eSJoerg Wunsch 				 * the retry counter.  However, in case
23193fef646eSJoerg Wunsch 				 * something is seriously messed up (like
23203fef646eSJoerg Wunsch 				 * broken hardware), we rather limit the
23213fef646eSJoerg Wunsch 				 * number of retries so the IO operation
23223fef646eSJoerg Wunsch 				 * doesn't block indefinately.
2323b39c878eSAndrey A. Chernov 				 */
23243fef646eSJoerg Wunsch 				if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2325b39c878eSAndrey A. Chernov 					fdc->state = SEEKCOMPLETE;
2326246ed35dSJoerg Wunsch 					return (1);/* will return immediately */
23273fef646eSJoerg Wunsch 				} /* else fall through */
2328b39c878eSAndrey A. Chernov                         }
23293fef646eSJoerg Wunsch 			if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
23303a2f7427SDavid Greenman 				&& fdc->retry < 6)
23313a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
23323a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
23333a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
23343a2f7427SDavid Greenman 				&& fdc->retry < 3)
23353a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
23366182fdbdSPeter Wemm 			return (retrier(fdc));
23375b81b6b3SRodney W. Grimes 		}
23385b81b6b3SRodney W. Grimes 		/* All OK */
2339250300ebSJoerg Wunsch 		if (rdsectid) {
2340250300ebSJoerg Wunsch 			/* copy out ID field contents */
234164860614SJoerg Wunsch 			idp = (struct fdc_readid *)bp->bio_data;
2342250300ebSJoerg Wunsch 			idp->cyl = fdc->status[3];
2343250300ebSJoerg Wunsch 			idp->head = fdc->status[4];
2344250300ebSJoerg Wunsch 			idp->sec = fdc->status[5];
2345250300ebSJoerg Wunsch 			idp->secshift = fdc->status[6];
2346250300ebSJoerg Wunsch 		}
23473fef646eSJoerg Wunsch 		/* Operation successful, retry DMA overruns again next time. */
23483fef646eSJoerg Wunsch 		fdc->dma_overruns = 0;
23493a2f7427SDavid Greenman 		fd->skip += fdblk;
2350fb35bd37SJoerg Wunsch 		if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
23515b81b6b3SRodney W. Grimes 			/* set up next transfer */
23525b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
23536182fdbdSPeter Wemm 		} else {
23545b81b6b3SRodney W. Grimes 			/* ALL DONE */
23555b81b6b3SRodney W. Grimes 			fd->skip = 0;
2356fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2357e93e63cbSBruce Evans 			fdc->bp = NULL;
23585f830ea2SJoerg Wunsch 			device_unbusy(fd->dev);
2359a468031cSPoul-Henning Kamp 			biofinish(bp, &fd->device_stats, 0);
23605b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
23615b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
23625b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
23635b81b6b3SRodney W. Grimes 		}
2364246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2365fb35bd37SJoerg Wunsch 
23665b81b6b3SRodney W. Grimes 	case RESETCTLR:
23673a2f7427SDavid Greenman 		fdc_reset(fdc);
23685b81b6b3SRodney W. Grimes 		fdc->retry++;
23695c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
2370246ed35dSJoerg Wunsch 		return (0);	/* will return later */
2371fb35bd37SJoerg Wunsch 
23725c1a1eaeSBruce Evans 	case RESETCOMPLETE:
23735c1a1eaeSBruce Evans 		/*
23745c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
23755c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
23765c1a1eaeSBruce Evans 		 */
23770e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
23780e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
23795c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
2380fb35bd37SJoerg Wunsch 		/* FALLTHROUGH */
23815c1a1eaeSBruce Evans 	case STARTRECAL:
23826182fdbdSPeter Wemm 		if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2383dc8603e3SJoerg Wunsch 			/* arrgl */
2384dc8603e3SJoerg Wunsch 			fdc->retry = 6;
23856182fdbdSPeter Wemm 			return (retrier(fdc));
2386dc8603e3SJoerg Wunsch 		}
23875b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
23885b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2389fb35bd37SJoerg Wunsch 
23905b81b6b3SRodney W. Grimes 	case RECALWAIT:
23915b81b6b3SRodney W. Grimes 		/* allow heads to settle */
23926182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 8);
23935b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
23945b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2395fb35bd37SJoerg Wunsch 
23965b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
23973a2f7427SDavid Greenman 		do {
23983a2f7427SDavid Greenman 			/*
2399dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
24003a2f7427SDavid Greenman 			 */
2401dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2402246ed35dSJoerg Wunsch 				return (0); /* will return later */
2403dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
2404dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2405246ed35dSJoerg Wunsch 				return (0); /* hope for a real intr */
24063a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
24073a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
24085b81b6b3SRodney W. Grimes 		{
2409dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
2410dc8603e3SJoerg Wunsch 				/*
2411246ed35dSJoerg Wunsch 				 * A recalibrate from beyond cylinder 77
2412dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
2413dc8603e3SJoerg Wunsch 				 * since people used to complain much about
2414dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
2415dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
2416246ed35dSJoerg Wunsch 				 * time in a line.
2417dc8603e3SJoerg Wunsch 				 */
2418dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
2419dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
24203a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
24216182fdbdSPeter Wemm 			return (retrier(fdc));
24225b81b6b3SRodney W. Grimes 		}
24235b81b6b3SRodney W. Grimes 		fd->track = 0;
24245b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
24255b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
2426246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2427fb35bd37SJoerg Wunsch 
24285b81b6b3SRodney W. Grimes 	case MOTORWAIT:
24295b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
24305b81b6b3SRodney W. Grimes 		{
24315b81b6b3SRodney W. Grimes 			return (0); /* time's not up yet */
24325b81b6b3SRodney W. Grimes 		}
24335c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
24345c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
24355c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
2436fb35bd37SJoerg Wunsch 		} else
2437fb35bd37SJoerg Wunsch 			fdc->state = DOSEEK;
2438246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2439fb35bd37SJoerg Wunsch 
24405b81b6b3SRodney W. Grimes 	default:
2441b6e5f28eSPeter Wemm 		device_printf(fdc->fdc_dev, "unexpected FD int->");
244264860614SJoerg Wunsch 		if (fd_read_status(fdc) == 0)
2443a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
24445b81b6b3SRodney W. Grimes 			       fdc->status[0],
24455b81b6b3SRodney W. Grimes 			       fdc->status[1],
24465b81b6b3SRodney W. Grimes 			       fdc->status[2],
24475b81b6b3SRodney W. Grimes 			       fdc->status[3],
24485b81b6b3SRodney W. Grimes 			       fdc->status[4],
24495b81b6b3SRodney W. Grimes 			       fdc->status[5],
24505b81b6b3SRodney W. Grimes 			       fdc->status[6] );
24513a2f7427SDavid Greenman 		else
2452dac0f2dbSJoerg Wunsch 			printf("No status available   ");
2453dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
2454dac0f2dbSJoerg Wunsch 		{
2455dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
2456246ed35dSJoerg Wunsch 			return (0); /* will return later */
24575b81b6b3SRodney W. Grimes 		}
2458dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
2459246ed35dSJoerg Wunsch 		return (0);	/* will return later */
2460dac0f2dbSJoerg Wunsch 	}
2461246ed35dSJoerg Wunsch 	/* noone should ever get here */
24625b81b6b3SRodney W. Grimes }
24635b81b6b3SRodney W. Grimes 
2464aaf08d94SGarrett Wollman static int
24656182fdbdSPeter Wemm retrier(struct fdc_data *fdc)
24665b81b6b3SRodney W. Grimes {
24678177437dSPoul-Henning Kamp 	struct bio *bp;
24686182fdbdSPeter Wemm 	struct fd_data *fd;
24696182fdbdSPeter Wemm 	int fdu;
24705b81b6b3SRodney W. Grimes 
2471e93e63cbSBruce Evans 	bp = fdc->bp;
24725b81b6b3SRodney W. Grimes 
24736182fdbdSPeter Wemm 	/* XXX shouldn't this be cached somewhere?  */
24748177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
24756182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
24766182fdbdSPeter Wemm 	if (fd->options & FDOPT_NORETRY)
24773a2f7427SDavid Greenman 		goto fail;
24786182fdbdSPeter Wemm 
24796182fdbdSPeter Wemm 	switch (fdc->retry) {
24805b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
24815b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
24825b81b6b3SRodney W. Grimes 		break;
24835b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
24845b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
24855b81b6b3SRodney W. Grimes 		break;
24865b81b6b3SRodney W. Grimes 	case 6:
24875b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
24885b81b6b3SRodney W. Grimes 		break;
24895b81b6b3SRodney W. Grimes 	case 7:
24905b81b6b3SRodney W. Grimes 		break;
24915b81b6b3SRodney W. Grimes 	default:
24923a2f7427SDavid Greenman 	fail:
2493fb35bd37SJoerg Wunsch 		if ((fd->options & FDOPT_NOERRLOG) == 0) {
2494fb35bd37SJoerg Wunsch 			diskerr(bp, "hard error", fdc->fd->skip / DEV_BSIZE,
24953a2f7427SDavid Greenman 				(struct disklabel *)NULL);
2496fb35bd37SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID) {
2497dc5df763SJoerg Wunsch 				printf(
2498a838d83dSBruce Evans 				" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2499dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
2500dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
2501dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
2502dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
2503dc5df763SJoerg Wunsch 				       fdc->status[5]);
2504dc5df763SJoerg Wunsch 			}
2505dc5df763SJoerg Wunsch 			else
2506dc5df763SJoerg Wunsch 				printf(" (No status)\n");
25075b81b6b3SRodney W. Grimes 		}
25082995d110SJoerg Wunsch 		if ((fd->options & FDOPT_NOERROR) == 0) {
25098177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
25108177437dSPoul-Henning Kamp 			bp->bio_error = EIO;
2511fb35bd37SJoerg Wunsch 			bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
2512fb35bd37SJoerg Wunsch 		} else
2513fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2514e93e63cbSBruce Evans 		fdc->bp = NULL;
25155b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
25165f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
2517a468031cSPoul-Henning Kamp 		biofinish(bp, &fdc->fd->device_stats, 0);
251892ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
25195c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
25205b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
25215b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
252292ed385aSRodney W. Grimes 		return (1);
25235b81b6b3SRodney W. Grimes 	}
25245b81b6b3SRodney W. Grimes 	fdc->retry++;
25255b81b6b3SRodney W. Grimes 	return (1);
25265b81b6b3SRodney W. Grimes }
25275b81b6b3SRodney W. Grimes 
25281fdb6e6cSPoul-Henning Kamp static void
25291fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp)
25301fdb6e6cSPoul-Henning Kamp {
25311fdb6e6cSPoul-Henning Kamp 	wakeup(bp);
25321fdb6e6cSPoul-Henning Kamp }
25331fdb6e6cSPoul-Henning Kamp 
2534b39c878eSAndrey A. Chernov static int
2535f664aeeeSJoerg Wunsch fdmisccmd(dev_t dev, u_int cmd, void *data)
2536b39c878eSAndrey A. Chernov {
2537b39c878eSAndrey A. Chernov  	fdu_t fdu;
2538b39c878eSAndrey A. Chernov  	fd_p fd;
25391fdb6e6cSPoul-Henning Kamp 	struct bio *bp;
2540f664aeeeSJoerg Wunsch 	struct fd_formb *finfo;
2541f664aeeeSJoerg Wunsch 	struct fdc_readid *idfield;
25423a2f7427SDavid Greenman 	size_t fdblk;
2543b39c878eSAndrey A. Chernov 
2544b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
25456182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
25463a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2547f664aeeeSJoerg Wunsch 	finfo = (struct fd_formb *)data;
2548f664aeeeSJoerg Wunsch 	idfield = (struct fdc_readid *)data;
2549b39c878eSAndrey A. Chernov 
2550fb35bd37SJoerg Wunsch 	bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO);
2551b39c878eSAndrey A. Chernov 
2552b39c878eSAndrey A. Chernov 	/*
2553f664aeeeSJoerg Wunsch 	 * Set up a bio request for fdstrategy().  bio_blkno is faked
2554f664aeeeSJoerg Wunsch 	 * so that fdstrategy() will seek to the the requested
2555f664aeeeSJoerg Wunsch 	 * cylinder, and use the desired head.  Since we are not
2556f664aeeeSJoerg Wunsch 	 * interested in bioqdisksort() munging with our faked bio
2557f664aeeeSJoerg Wunsch 	 * request, we mark it as being an ordered request.
2558b39c878eSAndrey A. Chernov 	 */
2559f664aeeeSJoerg Wunsch 	bp->bio_cmd = cmd;
2560f664aeeeSJoerg Wunsch 	if (cmd == BIO_FORMAT) {
2561f664aeeeSJoerg Wunsch 		bp->bio_blkno =
2562f664aeeeSJoerg Wunsch 		    (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
2563f664aeeeSJoerg Wunsch 		     finfo->head * fd->ft->sectrac) *
2564f664aeeeSJoerg Wunsch 		    fdblk / DEV_BSIZE;
2565f664aeeeSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fd_idfield_data) *
2566f664aeeeSJoerg Wunsch 		    finfo->fd_formb_nsecs;
2567f664aeeeSJoerg Wunsch 	} else if (cmd == BIO_RDSECTID) {
2568f664aeeeSJoerg Wunsch 		bp->bio_blkno =
2569f664aeeeSJoerg Wunsch 		    (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
2570f664aeeeSJoerg Wunsch 		     idfield->head * fd->ft->sectrac) *
2571f664aeeeSJoerg Wunsch 		    fdblk / DEV_BSIZE;
2572250300ebSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fdc_readid);
2573f664aeeeSJoerg Wunsch 	} else
2574f664aeeeSJoerg Wunsch 		panic("wrong cmd in fdmisccmd()");
2575f664aeeeSJoerg Wunsch 	bp->bio_data = data;
2576250300ebSJoerg Wunsch 	bp->bio_dev = dev;
2577250300ebSJoerg Wunsch 	bp->bio_done = fdbiodone;
2578f664aeeeSJoerg Wunsch 	bp->bio_flags = BIO_ORDERED;
2579250300ebSJoerg Wunsch 
2580f5f7ba03SJordan K. Hubbard 	/*
2581fb35bd37SJoerg Wunsch 	 * Now run the command.  The wait loop is a version of bufwait()
2582fb35bd37SJoerg Wunsch 	 * adapted for struct bio instead of struct buf and specialized
2583fb35bd37SJoerg Wunsch 	 * for the current context.
2584f5f7ba03SJordan K. Hubbard 	 */
2585f664aeeeSJoerg Wunsch 	fdstrategy(bp);
2586f664aeeeSJoerg Wunsch 	while ((bp->bio_flags & BIO_DONE) == 0)
2587f664aeeeSJoerg Wunsch 		tsleep(bp, PRIBIO, "fdcmd", 0);
2588f664aeeeSJoerg Wunsch 
2589f664aeeeSJoerg Wunsch 	free(bp, M_TEMP);
2590f664aeeeSJoerg Wunsch 	return (bp->bio_flags & BIO_ERROR ? bp->bio_error : 0);
2591f664aeeeSJoerg Wunsch }
25925b81b6b3SRodney W. Grimes 
25933e425b96SJulian Elischer static int
2594b40ce416SJulian Elischer fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
2595f5f7ba03SJordan K. Hubbard {
2596f664aeeeSJoerg Wunsch  	fdu_t fdu;
2597f664aeeeSJoerg Wunsch  	fd_p fd;
2598f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
2599fb35bd37SJoerg Wunsch 	struct disklabel *lp;
26002995d110SJoerg Wunsch 	struct fdc_status *fsp;
2601250300ebSJoerg Wunsch 	struct fdc_readid *rid;
2602f664aeeeSJoerg Wunsch 	size_t fdblk;
26031a6bed68SJoerg Wunsch 	int error, type;
2604f5f7ba03SJordan K. Hubbard 
2605f664aeeeSJoerg Wunsch  	fdu = FDUNIT(minor(dev));
26061a6bed68SJoerg Wunsch 	type = FDTYPE(minor(dev));
2607f664aeeeSJoerg Wunsch  	fd = devclass_get_softc(fd_devclass, fdu);
2608fb35bd37SJoerg Wunsch 
26091a6bed68SJoerg Wunsch 	/*
26101a6bed68SJoerg Wunsch 	 * First, handle everything that could be done with
26111a6bed68SJoerg Wunsch 	 * FD_NONBLOCK still being set.
26121a6bed68SJoerg Wunsch 	 */
26131a6bed68SJoerg Wunsch 	switch (cmd) {
26141a6bed68SJoerg Wunsch 	case FIONBIO:
26151a6bed68SJoerg Wunsch 		if (*(int *)addr != 0)
26161a6bed68SJoerg Wunsch 			fd->flags |= FD_NONBLOCK;
26171a6bed68SJoerg Wunsch 		else {
26181a6bed68SJoerg Wunsch 			if (fd->ft == 0) {
26191a6bed68SJoerg Wunsch 				/*
26201a6bed68SJoerg Wunsch 				 * No drive type has been selected yet,
26211a6bed68SJoerg Wunsch 				 * cannot turn FNONBLOCK off.
26221a6bed68SJoerg Wunsch 				 */
26231a6bed68SJoerg Wunsch 				return (EINVAL);
26241a6bed68SJoerg Wunsch 			}
26251a6bed68SJoerg Wunsch 			fd->flags &= ~FD_NONBLOCK;
26261a6bed68SJoerg Wunsch 		}
26271a6bed68SJoerg Wunsch 		return (0);
2628fb35bd37SJoerg Wunsch 
26291a6bed68SJoerg Wunsch 	case FIOASYNC:
26301a6bed68SJoerg Wunsch 		/* keep the generic fcntl() code happy */
26311a6bed68SJoerg Wunsch 		return (0);
26321a6bed68SJoerg Wunsch 
26331a6bed68SJoerg Wunsch 	case FD_GTYPE:                  /* get drive type */
26341a6bed68SJoerg Wunsch 		if (fd->ft == 0)
26351a6bed68SJoerg Wunsch 			/* no type known yet, return the native type */
26361a6bed68SJoerg Wunsch 			*(struct fd_type *)addr = fd_native_types[fd->type];
26371a6bed68SJoerg Wunsch 		else
26381a6bed68SJoerg Wunsch 			*(struct fd_type *)addr = *fd->ft;
26391a6bed68SJoerg Wunsch 		return (0);
26401a6bed68SJoerg Wunsch 
26411a6bed68SJoerg Wunsch 	case FD_STYPE:                  /* set drive type */
26421a6bed68SJoerg Wunsch 		if (type == 0) {
26431a6bed68SJoerg Wunsch 			/*
26441a6bed68SJoerg Wunsch 			 * Allow setting drive type temporarily iff
26451a6bed68SJoerg Wunsch 			 * currently unset.  Used for fdformat so any
26461a6bed68SJoerg Wunsch 			 * user can set it, and then start formatting.
26471a6bed68SJoerg Wunsch 			 */
26481a6bed68SJoerg Wunsch 			if (fd->ft)
26491a6bed68SJoerg Wunsch 				return (EINVAL); /* already set */
26501a6bed68SJoerg Wunsch 			fd->ft = fd->fts;
26511a6bed68SJoerg Wunsch 			*fd->ft = *(struct fd_type *)addr;
26521a6bed68SJoerg Wunsch 			fd->flags |= FD_UA;
26531a6bed68SJoerg Wunsch 		} else {
26541a6bed68SJoerg Wunsch 			/*
26551a6bed68SJoerg Wunsch 			 * Set density definition permanently.  Only
26561a6bed68SJoerg Wunsch 			 * allow for superuser.
26571a6bed68SJoerg Wunsch 			 */
26581a6bed68SJoerg Wunsch 			if (suser_td(td) != 0)
26591a6bed68SJoerg Wunsch 				return (EPERM);
26601a6bed68SJoerg Wunsch 			fd->fts[type] = *(struct fd_type *)addr;
26611a6bed68SJoerg Wunsch 		}
26621a6bed68SJoerg Wunsch 		return (0);
26631a6bed68SJoerg Wunsch 
26641a6bed68SJoerg Wunsch 	case FD_GOPTS:			/* get drive options */
26651a6bed68SJoerg Wunsch 		*(int *)addr = fd->options + (type == 0? FDOPT_AUTOSEL: 0);
26661a6bed68SJoerg Wunsch 		return (0);
26671a6bed68SJoerg Wunsch 
26681a6bed68SJoerg Wunsch 	case FD_SOPTS:			/* set drive options */
26691a6bed68SJoerg Wunsch 		fd->options = *(int *)addr & ~FDOPT_AUTOSEL;
26701a6bed68SJoerg Wunsch 		return (0);
26711a6bed68SJoerg Wunsch 
26721a6bed68SJoerg Wunsch #ifdef FDC_DEBUG
26731a6bed68SJoerg Wunsch 	case FD_DEBUG:
26741a6bed68SJoerg Wunsch 		if ((fd_debug != 0) != (*(int *)addr != 0)) {
26751a6bed68SJoerg Wunsch 			fd_debug = (*(int *)addr != 0);
26761a6bed68SJoerg Wunsch 			printf("fd%d: debugging turned %s\n",
26771a6bed68SJoerg Wunsch 			    fd->fdu, fd_debug ? "on" : "off");
26781a6bed68SJoerg Wunsch 		}
26791a6bed68SJoerg Wunsch 		return (0);
26801a6bed68SJoerg Wunsch #endif
26811a6bed68SJoerg Wunsch 
26821a6bed68SJoerg Wunsch 	case FD_CLRERR:
26831a6bed68SJoerg Wunsch 		if (suser_td(td) != 0)
26841a6bed68SJoerg Wunsch 			return (EPERM);
26851a6bed68SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
26861a6bed68SJoerg Wunsch 		return (0);
26871a6bed68SJoerg Wunsch 
26881a6bed68SJoerg Wunsch 	case FD_GSTAT:
26891a6bed68SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
26901a6bed68SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
26911a6bed68SJoerg Wunsch 			return (EINVAL);
26921a6bed68SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
26931a6bed68SJoerg Wunsch 		return (0);
26941a6bed68SJoerg Wunsch 
26951a6bed68SJoerg Wunsch 	case FD_GDTYPE:
26961a6bed68SJoerg Wunsch 		*(enum fd_drivetype *)addr = fd->type;
26971a6bed68SJoerg Wunsch 		return (0);
26981a6bed68SJoerg Wunsch 	}
26991a6bed68SJoerg Wunsch 
27001a6bed68SJoerg Wunsch 	/*
27011a6bed68SJoerg Wunsch 	 * Now handle everything else.  Make sure we have a valid
27021a6bed68SJoerg Wunsch 	 * drive type.
27031a6bed68SJoerg Wunsch 	 */
27041a6bed68SJoerg Wunsch 	if (fd->flags & FD_NONBLOCK)
27051a6bed68SJoerg Wunsch 		return (EAGAIN);
27061a6bed68SJoerg Wunsch 	if (fd->ft == 0)
27071a6bed68SJoerg Wunsch 		return (ENXIO);
27083a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2709fb35bd37SJoerg Wunsch 	error = 0;
2710f5f7ba03SJordan K. Hubbard 
27116182fdbdSPeter Wemm 	switch (cmd) {
2712f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
2713fb35bd37SJoerg Wunsch 		lp = malloc(sizeof(*lp), M_TEMP, M_ZERO);
2714fb35bd37SJoerg Wunsch 		lp->d_secsize = fdblk;
27156182fdbdSPeter Wemm 		fdt = fd->ft;
2716fb35bd37SJoerg Wunsch 		lp->d_secpercyl = fdt->size / fdt->tracks;
2717fb35bd37SJoerg Wunsch 		lp->d_type = DTYPE_FLOPPY;
2718fb35bd37SJoerg Wunsch 		if (readdisklabel(dkmodpart(dev, RAW_PART), lp) != NULL)
2719f5f7ba03SJordan K. Hubbard 			error = EINVAL;
2720fb35bd37SJoerg Wunsch 		else
2721fb35bd37SJoerg Wunsch 			*(struct disklabel *)addr = *lp;
2722fb35bd37SJoerg Wunsch 		free(lp, M_TEMP);
2723f5f7ba03SJordan K. Hubbard 		break;
2724f5f7ba03SJordan K. Hubbard 
2725f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
2726f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2727fb35bd37SJoerg Wunsch 			return (EBADF);
2728fb35bd37SJoerg Wunsch 		/*
2729fb35bd37SJoerg Wunsch 		 * XXX perhaps should call setdisklabel() to do error checking
2730fb35bd37SJoerg Wunsch 		 * although there is nowhere to "set" the result.  Perhaps
2731fb35bd37SJoerg Wunsch 		 * should always just fail.
2732fb35bd37SJoerg Wunsch 		 */
2733f5f7ba03SJordan K. Hubbard 		break;
2734f5f7ba03SJordan K. Hubbard 
2735f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
2736f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2737fb35bd37SJoerg Wunsch 			return (EBADF);
2738f5f7ba03SJordan K. Hubbard 		break;
2739f5f7ba03SJordan K. Hubbard 
2740f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
2741fb35bd37SJoerg Wunsch 		if ((flag & FWRITE) == 0)
2742fb35bd37SJoerg Wunsch 			return (EBADF);
2743fb35bd37SJoerg Wunsch 		lp = malloc(DEV_BSIZE, M_TEMP, M_ZERO);
2744fb35bd37SJoerg Wunsch 		error = setdisklabel(lp, (struct disklabel *)addr, (u_long)0);
2745fb35bd37SJoerg Wunsch 		if (error != 0)
2746fb35bd37SJoerg Wunsch 			error = writedisklabel(dev, lp);
2747fb35bd37SJoerg Wunsch 		free(lp, M_TEMP);
2748b39c878eSAndrey A. Chernov 		break;
2749f664aeeeSJoerg Wunsch 
2750b39c878eSAndrey A. Chernov 	case FD_FORM:
2751b39c878eSAndrey A. Chernov 		if ((flag & FWRITE) == 0)
2752fb35bd37SJoerg Wunsch 			return (EBADF);	/* must be opened for writing */
2753fb35bd37SJoerg Wunsch 		if (((struct fd_formb *)addr)->format_version !=
2754b39c878eSAndrey A. Chernov 		    FD_FORMAT_VERSION)
2755fb35bd37SJoerg Wunsch 			return (EINVAL); /* wrong version of formatting prog */
2756f664aeeeSJoerg Wunsch 		error = fdmisccmd(dev, BIO_FORMAT, addr);
2757b39c878eSAndrey A. Chernov 		break;
2758b39c878eSAndrey A. Chernov 
2759b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
27603e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2761f5f7ba03SJordan K. Hubbard 		break;
2762f5f7ba03SJordan K. Hubbard 
27633a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
27643a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
2765b40ce416SJulian Elischer 		if (suser_td(td) != 0)
2766fb35bd37SJoerg Wunsch 			return (EPERM);
27673e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
27683a2f7427SDavid Greenman 		break;
27693a2f7427SDavid Greenman 
27703a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
27713e425b96SJulian Elischer 		*(int *)addr = fd->options;
27723a2f7427SDavid Greenman 		break;
27733a2f7427SDavid Greenman 
27743a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
27753e425b96SJulian Elischer 		fd->options = *(int *)addr;
27763a2f7427SDavid Greenman 		break;
27773a2f7427SDavid Greenman 
2778f664aeeeSJoerg Wunsch #ifdef FDC_DEBUG
2779f664aeeeSJoerg Wunsch 	case FD_DEBUG:
27800e17a5bcSJoerg Wunsch 		if ((fd_debug != 0) != (*(int *)addr != 0)) {
27810e17a5bcSJoerg Wunsch 			fd_debug = (*(int *)addr != 0);
27820e17a5bcSJoerg Wunsch 			printf("fd%d: debugging turned %s\n",
27830e17a5bcSJoerg Wunsch 			    fd->fdu, fd_debug ? "on" : "off");
27840e17a5bcSJoerg Wunsch 		}
2785f664aeeeSJoerg Wunsch 		break;
2786f664aeeeSJoerg Wunsch #endif
2787f664aeeeSJoerg Wunsch 
27882995d110SJoerg Wunsch 	case FD_CLRERR:
2789b40ce416SJulian Elischer 		if (suser_td(td) != 0)
2790fb35bd37SJoerg Wunsch 			return (EPERM);
27912995d110SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
27922995d110SJoerg Wunsch 		break;
27932995d110SJoerg Wunsch 
27942995d110SJoerg Wunsch 	case FD_GSTAT:
27952995d110SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
27962995d110SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2797fb35bd37SJoerg Wunsch 			return (EINVAL);
27982995d110SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
27992995d110SJoerg Wunsch 		break;
28002995d110SJoerg Wunsch 
2801250300ebSJoerg Wunsch 	case FD_READID:
2802250300ebSJoerg Wunsch 		rid = (struct fdc_readid *)addr;
2803250300ebSJoerg Wunsch 		if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2804fb35bd37SJoerg Wunsch 			return (EINVAL);
2805f664aeeeSJoerg Wunsch 		error = fdmisccmd(dev, BIO_RDSECTID, addr);
2806250300ebSJoerg Wunsch 		break;
2807250300ebSJoerg Wunsch 
2808f5f7ba03SJordan K. Hubbard 	default:
28093a2f7427SDavid Greenman 		error = ENOTTY;
2810f5f7ba03SJordan K. Hubbard 		break;
2811f5f7ba03SJordan K. Hubbard 	}
2812f5f7ba03SJordan K. Hubbard 	return (error);
2813f5f7ba03SJordan K. Hubbard }
2814