xref: /freebsd/sys/dev/fdc/fdc.c (revision d306122d60b484718888cfbda76b211d3d7767d1)
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;
149246ed35dSJoerg Wunsch 	int	fdout;		/* mirror of the w/o digital output reg */
150246ed35dSJoerg Wunsch 	u_int	status[7];	/* copy of the registers */
151246ed35dSJoerg Wunsch 	enum	fdc_type fdct;	/* chip version of FDC */
152246ed35dSJoerg Wunsch 	int	fdc_errs;	/* number of logged errors */
153246ed35dSJoerg Wunsch 	int	dma_overruns;	/* number of DMA overruns */
154246ed35dSJoerg Wunsch 	struct	bio_queue_head head;
155246ed35dSJoerg Wunsch 	struct	bio *bp;	/* active buffer */
156246ed35dSJoerg Wunsch 	struct	resource *res_ioport, *res_ctl, *res_irq, *res_drq;
157246ed35dSJoerg Wunsch 	int	rid_ioport, rid_ctl, rid_irq, rid_drq;
158246ed35dSJoerg Wunsch 	int	port_off;
159246ed35dSJoerg Wunsch 	bus_space_tag_t portt;
160246ed35dSJoerg Wunsch 	bus_space_handle_t porth;
161246ed35dSJoerg Wunsch 	bus_space_tag_t ctlt;
162246ed35dSJoerg Wunsch 	bus_space_handle_t ctlh;
163246ed35dSJoerg Wunsch 	void	*fdc_intr;
164246ed35dSJoerg Wunsch 	struct	device *fdc_dev;
165246ed35dSJoerg Wunsch 	void	(*fdctl_wr)(struct fdc_data *fdc, u_int8_t v);
166246ed35dSJoerg Wunsch };
167246ed35dSJoerg Wunsch 
168d306122dSPoul-Henning Kamp #define BIO_FORMAT	BIO_CMD2
169d306122dSPoul-Henning Kamp 
170246ed35dSJoerg Wunsch typedef int	fdu_t;
171246ed35dSJoerg Wunsch typedef int	fdcu_t;
172246ed35dSJoerg Wunsch typedef int	fdsu_t;
173246ed35dSJoerg Wunsch typedef	struct fd_data *fd_p;
174246ed35dSJoerg Wunsch typedef struct fdc_data *fdc_p;
175246ed35dSJoerg Wunsch typedef enum fdc_type fdc_t;
176246ed35dSJoerg Wunsch 
177246ed35dSJoerg Wunsch #define FDUNIT(s)	(((s) >> 6) & 3)
1781a6bed68SJoerg Wunsch #define FDNUMTOUNIT(n)	(((n) & 3) << 6)
179246ed35dSJoerg Wunsch #define FDTYPE(s)	((s) & 0x3f)
180246ed35dSJoerg Wunsch 
181246ed35dSJoerg Wunsch /*
182246ed35dSJoerg Wunsch  * fdc maintains a set (1!) of ivars per child of each controller.
183246ed35dSJoerg Wunsch  */
184246ed35dSJoerg Wunsch enum fdc_device_ivars {
185246ed35dSJoerg Wunsch 	FDC_IVAR_FDUNIT,
186246ed35dSJoerg Wunsch };
187246ed35dSJoerg Wunsch 
188246ed35dSJoerg Wunsch /*
189246ed35dSJoerg Wunsch  * Simple access macros for the ivars.
190246ed35dSJoerg Wunsch  */
191246ed35dSJoerg Wunsch #define FDC_ACCESSOR(A, B, T)						\
192246ed35dSJoerg Wunsch static __inline T fdc_get_ ## A(device_t dev)				\
193246ed35dSJoerg Wunsch {									\
194246ed35dSJoerg Wunsch 	uintptr_t v;							\
195246ed35dSJoerg Wunsch 	BUS_READ_IVAR(device_get_parent(dev), dev, FDC_IVAR_ ## B, &v);	\
196246ed35dSJoerg Wunsch 	return (T) v;							\
197246ed35dSJoerg Wunsch }
198246ed35dSJoerg Wunsch FDC_ACCESSOR(fdunit,	FDUNIT,	int)
199246ed35dSJoerg Wunsch 
2001a6bed68SJoerg Wunsch /* configuration flags for fdc */
201e34c71eaSJoerg Wunsch #define FDC_NO_FIFO	(1 << 2)	/* do not enable FIFO  */
2020722d6abSJoerg Wunsch 
203dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
204dc5df763SJoerg Wunsch #define FD_FAILED -1
205dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
206dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
2073fef646eSJoerg Wunsch /*
2083fef646eSJoerg Wunsch  * Stop retrying after this many DMA overruns.  Since each retry takes
20983edbfa5SJoerg Wunsch  * one revolution, with 300 rpm., 25 retries take approximately 5
2103fef646eSJoerg Wunsch  * seconds which the read attempt will block in case the DMA overrun
2113fef646eSJoerg Wunsch  * is persistent.
2123fef646eSJoerg Wunsch  */
2133fef646eSJoerg Wunsch #define FDC_DMAOV_MAX	25
214dc5df763SJoerg Wunsch 
2151a6bed68SJoerg Wunsch /*
2161a6bed68SJoerg Wunsch  * Number of subdevices that can be used for different density types.
2171a6bed68SJoerg Wunsch  * By now, the lower 6 bit of the minor number are reserved for this,
2181a6bed68SJoerg Wunsch  * allowing for up to 64 subdevices, but we only use 16 out of this.
2191a6bed68SJoerg Wunsch  * Density #0 is used for automatic format detection, the other
2201a6bed68SJoerg Wunsch  * densities are available as programmable densities (for assignment
2211a6bed68SJoerg Wunsch  * by fdcontrol(8)).
2221a6bed68SJoerg Wunsch  * The upper 2 bits of the minor number are reserved for the subunit
2231a6bed68SJoerg Wunsch  * (drive #) per controller.
2241a6bed68SJoerg Wunsch  */
2251a6bed68SJoerg Wunsch #define NUMDENS		16
226b99f0a4aSAndrew Moore 
227f664aeeeSJoerg Wunsch #define BIO_RDSECTID	BIO_CMD1
2287ca0641bSAndrey A. Chernov 
2291a6bed68SJoerg Wunsch /*
2301a6bed68SJoerg Wunsch  * List of native drive densities.  Order must match enum fd_drivetype
2311a6bed68SJoerg Wunsch  * in <sys/fdcio.h>.  Upon attaching the drive, each of the
2321a6bed68SJoerg Wunsch  * programmable subdevices is initialized with the native density
2331a6bed68SJoerg Wunsch  * definition.
2341a6bed68SJoerg Wunsch  */
2351a6bed68SJoerg Wunsch static struct fd_type fd_native_types[] =
2365b81b6b3SRodney W. Grimes {
2371a6bed68SJoerg Wunsch { 0 },				/* FDT_NONE */
2381a6bed68SJoerg Wunsch {  9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */
2391a6bed68SJoerg Wunsch { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M  */
2401a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */
2411a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
2421a6bed68SJoerg Wunsch #if 0				/* we currently don't handle 2.88 MB */
2431a6bed68SJoerg Wunsch { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS,  2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/
2441a6bed68SJoerg Wunsch #else
2451a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
2461a6bed68SJoerg Wunsch #endif
2475b81b6b3SRodney W. Grimes };
2485b81b6b3SRodney W. Grimes 
2491a6bed68SJoerg Wunsch /*
2501a6bed68SJoerg Wunsch  * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density
2511a6bed68SJoerg Wunsch  * selection, they just start out with their native density (or lose).
2521a6bed68SJoerg Wunsch  * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their
2531a6bed68SJoerg Wunsch  * respective lists of densities to search for.
2541a6bed68SJoerg Wunsch  */
2551a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_12m[] = {
2561a6bed68SJoerg Wunsch { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */
2571a6bed68SJoerg Wunsch {  9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */
2581a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2591a6bed68SJoerg Wunsch };
2601a6bed68SJoerg Wunsch 
2611a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_144m[] = {
2621a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
2631a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2641a6bed68SJoerg Wunsch };
2651a6bed68SJoerg Wunsch 
2661a6bed68SJoerg Wunsch /* We search for 1.44M first since this is the most common case. */
2671a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_288m[] = {
2681a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
2691a6bed68SJoerg Wunsch #if 0
2701a6bed68SJoerg Wunsch { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS,  2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */
2711a6bed68SJoerg Wunsch #endif
2721a6bed68SJoerg Wunsch {  9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
2731a6bed68SJoerg Wunsch };
274dc16046fSJoerg Wunsch 
275f8ce7dd5SJoerg Wunsch #define MAX_SEC_SIZE	(128 << 3)
27664860614SJoerg Wunsch #define MAX_CYLINDER	85	/* some people really stress their drives
27764860614SJoerg Wunsch 				 * up to cyl 82 */
278250300ebSJoerg Wunsch #define MAX_HEAD	1
279f8ce7dd5SJoerg Wunsch 
2806182fdbdSPeter Wemm static devclass_t fdc_devclass;
2815b81b6b3SRodney W. Grimes 
282246ed35dSJoerg Wunsch /*
283246ed35dSJoerg Wunsch  * Per drive structure (softc).
284246ed35dSJoerg Wunsch  */
2856182fdbdSPeter Wemm struct fd_data {
286b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2875b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2881a6bed68SJoerg Wunsch 	enum	fd_drivetype type; /* drive type */
2891a6bed68SJoerg Wunsch 	struct	fd_type *ft;	/* pointer to current type descriptor */
2901a6bed68SJoerg Wunsch 	struct	fd_type fts[NUMDENS]; /* type descriptors */
2915b81b6b3SRodney W. Grimes 	int	flags;
2925b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
2931a6bed68SJoerg Wunsch #define	FD_NONBLOCK	0x02	/* O_NONBLOCK set	*/
2941a6bed68SJoerg Wunsch #define	FD_ACTIVE	0x04	/* it's active		*/
2951a6bed68SJoerg Wunsch #define	FD_MOTOR	0x08	/* motor should be on	*/
2961a6bed68SJoerg Wunsch #define	FD_MOTOR_WAIT	0x10	/* motor coming up	*/
2971a6bed68SJoerg Wunsch #define	FD_UA		0x20	/* force unit attention */
2985b81b6b3SRodney W. Grimes 	int	skip;
2995b81b6b3SRodney W. Grimes 	int	hddrv;
300dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
3015b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
3021a6bed68SJoerg Wunsch 	int	options;	/* user configurable options, see fdcio.h */
30302a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
30402a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
305b2dfb1f9SJustin T. Gibbs 	struct	devstat device_stats;
306e219897aSJoerg Wunsch 	eventhandler_tag clonetag;
307e219897aSJoerg Wunsch 	dev_t	masterdev;
3081a6bed68SJoerg Wunsch 	dev_t	clonedevs[NUMDENS - 1];
3096182fdbdSPeter Wemm 	device_t dev;
3106182fdbdSPeter Wemm 	fdu_t	fdu;
3116182fdbdSPeter Wemm };
31237286586SPeter Wemm 
31337286586SPeter Wemm struct fdc_ivars {
31437286586SPeter Wemm 	int	fdunit;
31537286586SPeter Wemm };
3166182fdbdSPeter Wemm static devclass_t fd_devclass;
3175b81b6b3SRodney W. Grimes 
3181a6bed68SJoerg Wunsch /* configuration flags for fd */
3191a6bed68SJoerg Wunsch #define FD_TYPEMASK	0x0f	/* drive type, matches enum
3201a6bed68SJoerg Wunsch 				 * fd_drivetype; on i386 machines, if
3211a6bed68SJoerg Wunsch 				 * given as 0, use RTC type for fd0
3221a6bed68SJoerg Wunsch 				 * and fd1 */
3231a6bed68SJoerg Wunsch #define FD_DTYPE(flags)	((flags) & FD_TYPEMASK)
3241a6bed68SJoerg Wunsch #define FD_NO_CHLINE	0x10	/* drive does not support changeline
3251a6bed68SJoerg Wunsch 				 * aka. unit attention */
3261a6bed68SJoerg Wunsch #define FD_NO_PROBE	0x20	/* don't probe drive (seek test), just
3271a6bed68SJoerg Wunsch 				 * assume it is there */
3281a6bed68SJoerg Wunsch 
329246ed35dSJoerg Wunsch /*
330246ed35dSJoerg Wunsch  * Throughout this file the following conventions will be used:
331246ed35dSJoerg Wunsch  *
332246ed35dSJoerg Wunsch  * fd is a pointer to the fd_data struct for the drive in question
333246ed35dSJoerg Wunsch  * fdc is a pointer to the fdc_data struct for the controller
334246ed35dSJoerg Wunsch  * fdu is the floppy drive unit number
335246ed35dSJoerg Wunsch  * fdcu is the floppy controller unit number
336246ed35dSJoerg Wunsch  * fdsu is the floppy drive unit number on that controller. (sub-unit)
337246ed35dSJoerg Wunsch  */
338b99f0a4aSAndrew Moore 
339246ed35dSJoerg Wunsch /*
340246ed35dSJoerg Wunsch  * Function declarations, same (chaotic) order as they appear in the
341246ed35dSJoerg Wunsch  * file.  Re-ordering is too late now, it would only obfuscate the
342246ed35dSJoerg Wunsch  * diffs against old and offspring versions (like the PC98 one).
343246ed35dSJoerg Wunsch  *
344246ed35dSJoerg Wunsch  * Anyone adding functions here, please keep this sequence the same
345246ed35dSJoerg Wunsch  * as below -- makes locating a particular function in the body much
346246ed35dSJoerg Wunsch  * easier.
347246ed35dSJoerg Wunsch  */
348246ed35dSJoerg Wunsch static void fdout_wr(fdc_p, u_int8_t);
349246ed35dSJoerg Wunsch static u_int8_t fdsts_rd(fdc_p);
350246ed35dSJoerg Wunsch static void fddata_wr(fdc_p, u_int8_t);
351246ed35dSJoerg Wunsch static u_int8_t fddata_rd(fdc_p);
352246ed35dSJoerg Wunsch static void fdctl_wr_isa(fdc_p, u_int8_t);
353246ed35dSJoerg Wunsch #if NCARD > 0
354246ed35dSJoerg Wunsch static void fdctl_wr_pcmcia(fdc_p, u_int8_t);
355246ed35dSJoerg Wunsch #endif
356246ed35dSJoerg Wunsch #if 0
357246ed35dSJoerg Wunsch static u_int8_t fdin_rd(fdc_p);
358246ed35dSJoerg Wunsch #endif
359246ed35dSJoerg Wunsch static int fdc_err(struct fdc_data *, const char *);
360246ed35dSJoerg Wunsch static int fd_cmd(struct fdc_data *, int, ...);
361246ed35dSJoerg Wunsch static int enable_fifo(fdc_p fdc);
362246ed35dSJoerg Wunsch static int fd_sense_drive_status(fdc_p, int *);
363246ed35dSJoerg Wunsch static int fd_sense_int(fdc_p, int *, int *);
364246ed35dSJoerg Wunsch static int fd_read_status(fdc_p);
365246ed35dSJoerg Wunsch static int fdc_alloc_resources(struct fdc_data *);
366246ed35dSJoerg Wunsch static void fdc_release_resources(struct fdc_data *);
367246ed35dSJoerg Wunsch static int fdc_read_ivar(device_t, device_t, int, uintptr_t *);
368246ed35dSJoerg Wunsch static int fdc_probe(device_t);
369246ed35dSJoerg Wunsch #if NCARD > 0
370246ed35dSJoerg Wunsch static int fdc_pccard_probe(device_t);
371246ed35dSJoerg Wunsch #endif
372246ed35dSJoerg Wunsch static int fdc_detach(device_t dev);
373246ed35dSJoerg Wunsch static void fdc_add_child(device_t, const char *, int);
374246ed35dSJoerg Wunsch static int fdc_attach(device_t);
375246ed35dSJoerg Wunsch static int fdc_print_child(device_t, device_t);
376246ed35dSJoerg Wunsch static void fd_clone (void *, char *, int, dev_t *);
377246ed35dSJoerg Wunsch static int fd_probe(device_t);
378246ed35dSJoerg Wunsch static int fd_attach(device_t);
379246ed35dSJoerg Wunsch static int fd_detach(device_t);
3806182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int);
3813a2f7427SDavid Greenman #  define TURNON 1
3823a2f7427SDavid Greenman #  define TURNOFF 0
3833a2f7427SDavid Greenman static timeout_t fd_turnoff;
3843a2f7427SDavid Greenman static timeout_t fd_motor_on;
3856182fdbdSPeter Wemm static void fd_turnon(struct fd_data *);
3863a2f7427SDavid Greenman static void fdc_reset(fdc_p);
3876182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *);
38880909a7dSJoerg Wunsch static int out_fdc(struct fdc_data *, int);
389246ed35dSJoerg Wunsch /*
390246ed35dSJoerg Wunsch  * The open function is named Fdopen() to avoid confusion with fdopen()
391246ed35dSJoerg Wunsch  * in fd(4).  The difference is now only meaningful for debuggers.
392246ed35dSJoerg Wunsch  */
393246ed35dSJoerg Wunsch static	d_open_t	Fdopen;
394246ed35dSJoerg Wunsch static	d_close_t	fdclose;
395246ed35dSJoerg Wunsch static	d_strategy_t	fdstrategy;
3966182fdbdSPeter Wemm static void fdstart(struct fdc_data *);
3975c1a1eaeSBruce Evans static timeout_t fd_iotimeout;
3983a2f7427SDavid Greenman static timeout_t fd_pseudointr;
399246ed35dSJoerg Wunsch static driver_intr_t fdc_intr;
400246ed35dSJoerg Wunsch static int fdcpio(fdc_p, long, caddr_t, u_int);
4011a6bed68SJoerg Wunsch static int fdautoselect(dev_t);
4026182fdbdSPeter Wemm static int fdstate(struct fdc_data *);
4036182fdbdSPeter Wemm static int retrier(struct fdc_data *);
404246ed35dSJoerg Wunsch static void fdbiodone(struct bio *);
405f664aeeeSJoerg Wunsch static int fdmisccmd(dev_t, u_int, void *);
406246ed35dSJoerg Wunsch static	d_ioctl_t	fdioctl;
407d66c374fSTor Egge 
408d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
409d66c374fSTor Egge 
410d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
4113a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
412cba2a7c6SBruce Evans static int volatile fd_debug = 0;
4130e17a5bcSJoerg Wunsch #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0)
4140e17a5bcSJoerg Wunsch #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0)
415d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
4160e17a5bcSJoerg Wunsch #define TRACE0(arg) do { } while (0)
4170e17a5bcSJoerg Wunsch #define TRACE1(arg1, arg2) do { } while (0)
418d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
4195b81b6b3SRodney W. Grimes 
420246ed35dSJoerg Wunsch /*
421246ed35dSJoerg Wunsch  * Bus space handling (access to low-level IO).
422246ed35dSJoerg Wunsch  */
423427ccf00SDoug Rabson static void
424427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v)
425427ccf00SDoug Rabson {
426427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
427427ccf00SDoug Rabson }
428427ccf00SDoug Rabson 
429427ccf00SDoug Rabson static u_int8_t
430427ccf00SDoug Rabson fdsts_rd(fdc_p fdc)
431427ccf00SDoug Rabson {
432427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
433427ccf00SDoug Rabson }
434427ccf00SDoug Rabson 
435427ccf00SDoug Rabson static void
436427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v)
437427ccf00SDoug Rabson {
438427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
439427ccf00SDoug Rabson }
440427ccf00SDoug Rabson 
441427ccf00SDoug Rabson static u_int8_t
442427ccf00SDoug Rabson fddata_rd(fdc_p fdc)
443427ccf00SDoug Rabson {
444427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
445427ccf00SDoug Rabson }
446427ccf00SDoug Rabson 
447427ccf00SDoug Rabson static void
4485f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v)
449427ccf00SDoug Rabson {
450427ccf00SDoug Rabson 	bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
451427ccf00SDoug Rabson }
452427ccf00SDoug Rabson 
45358c9d623SPeter Wemm #if NCARD > 0
4545f830ea2SJoerg Wunsch static void
4555f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
4565f830ea2SJoerg Wunsch {
4575f830ea2SJoerg Wunsch 	bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
4585f830ea2SJoerg Wunsch }
45958c9d623SPeter Wemm #endif
4605f830ea2SJoerg Wunsch 
461427ccf00SDoug Rabson static u_int8_t
462427ccf00SDoug Rabson fdin_rd(fdc_p fdc)
463427ccf00SDoug Rabson {
464427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
465427ccf00SDoug Rabson }
466427ccf00SDoug Rabson 
46787f6c662SJulian Elischer #define CDEV_MAJOR 9
4684e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = {
4694e2f199eSPoul-Henning Kamp 	/* open */	Fdopen,
4704e2f199eSPoul-Henning Kamp 	/* close */	fdclose,
4714e2f199eSPoul-Henning Kamp 	/* read */	physread,
4724e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
4734e2f199eSPoul-Henning Kamp 	/* ioctl */	fdioctl,
4744e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
4754e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
4764e2f199eSPoul-Henning Kamp 	/* strategy */	fdstrategy,
4774e2f199eSPoul-Henning Kamp 	/* name */	"fd",
4784e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
4794e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
4804e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
4814e2f199eSPoul-Henning Kamp 	/* flags */	D_DISK,
4824e2f199eSPoul-Henning Kamp };
4834e2f199eSPoul-Henning Kamp 
484246ed35dSJoerg Wunsch /*
485246ed35dSJoerg Wunsch  * Auxiliary functions.  Well, some only.  Others are scattered
486246ed35dSJoerg Wunsch  * throughout the entire file.
487246ed35dSJoerg Wunsch  */
488dc5df763SJoerg Wunsch static int
4896182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
490dc5df763SJoerg Wunsch {
4916182fdbdSPeter Wemm 	fdc->fdc_errs++;
49216b04b6aSJoerg Wunsch 	if (s) {
493b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
494b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
495b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
496b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
497b6e5f28eSPeter Wemm 						    "logging any more\n");
49816b04b6aSJoerg Wunsch 	}
499dc5df763SJoerg Wunsch 
500dc5df763SJoerg Wunsch 	return FD_FAILED;
501dc5df763SJoerg Wunsch }
502dc5df763SJoerg Wunsch 
503dc5df763SJoerg Wunsch /*
504dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
505dc5df763SJoerg Wunsch  * Unit number,
506dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
507dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
508dc5df763SJoerg Wunsch  */
5096f4e0bebSPoul-Henning Kamp static int
5106182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...)
511dc5df763SJoerg Wunsch {
512dc5df763SJoerg Wunsch 	u_char cmd;
513dc5df763SJoerg Wunsch 	int n_in;
514dc5df763SJoerg Wunsch 	int n;
515dc5df763SJoerg Wunsch 	va_list ap;
516dc5df763SJoerg Wunsch 
517dc5df763SJoerg Wunsch 	va_start(ap, n_out);
518dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
519dc5df763SJoerg Wunsch 	va_end(ap);
520dc5df763SJoerg Wunsch 	va_start(ap, n_out);
521dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
522dc5df763SJoerg Wunsch 	{
5236182fdbdSPeter Wemm 		if (out_fdc(fdc, va_arg(ap, int)) < 0)
524dc5df763SJoerg Wunsch 		{
525dc5df763SJoerg Wunsch 			char msg[50];
5262127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
527dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
528dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
5296182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
530dc5df763SJoerg Wunsch 		}
531dc5df763SJoerg Wunsch 	}
532dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
533dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
534dc5df763SJoerg Wunsch 	{
535dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
5366182fdbdSPeter Wemm 		if (fd_in(fdc, ptr) < 0)
537dc5df763SJoerg Wunsch 		{
538dc5df763SJoerg Wunsch 			char msg[50];
5392127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
540dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
541dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
5426182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
543dc5df763SJoerg Wunsch 		}
544dc5df763SJoerg Wunsch 	}
545dc5df763SJoerg Wunsch 
546dc5df763SJoerg Wunsch 	return 0;
547dc5df763SJoerg Wunsch }
548dc5df763SJoerg Wunsch 
5496f4e0bebSPoul-Henning Kamp static int
550d66c374fSTor Egge enable_fifo(fdc_p fdc)
551d66c374fSTor Egge {
552d66c374fSTor Egge 	int i, j;
553d66c374fSTor Egge 
554d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
555d66c374fSTor Egge 
556d66c374fSTor Egge 		/*
557d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
558d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
559d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
560d66c374fSTor Egge 		 */
561d66c374fSTor Egge 
5626182fdbdSPeter Wemm 		if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
5636182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
564d66c374fSTor Egge 
565d66c374fSTor Egge 		/* If command is invalid, return */
566d66c374fSTor Egge 		j = 100000;
567427ccf00SDoug Rabson 		while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
568d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
569d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
570d66c374fSTor Egge 				fdc_reset(fdc);
571d66c374fSTor Egge 				return FD_FAILED;
572d66c374fSTor Egge 			}
573d66c374fSTor Egge 		if (j<0 ||
5746182fdbdSPeter Wemm 		    fd_cmd(fdc, 3,
575d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
576d66c374fSTor Egge 			fdc_reset(fdc);
5776182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
578d66c374fSTor Egge 		}
579d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
580d66c374fSTor Egge 		return 0;
581d66c374fSTor Egge 	}
5826182fdbdSPeter Wemm 	if (fd_cmd(fdc, 4,
583d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
5846182fdbdSPeter Wemm 		return fdc_err(fdc, "Re-enable FIFO failed\n");
585d66c374fSTor Egge 	return 0;
586d66c374fSTor Egge }
587d66c374fSTor Egge 
588d66c374fSTor Egge static int
589dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
590dc5df763SJoerg Wunsch {
591dc5df763SJoerg Wunsch 	int st3;
592dc5df763SJoerg Wunsch 
5936182fdbdSPeter Wemm 	if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
594dc5df763SJoerg Wunsch 	{
5956182fdbdSPeter Wemm 		return fdc_err(fdc, "Sense Drive Status failed\n");
596dc5df763SJoerg Wunsch 	}
597dc5df763SJoerg Wunsch 	if (st3p)
598dc5df763SJoerg Wunsch 		*st3p = st3;
599dc5df763SJoerg Wunsch 
600dc5df763SJoerg Wunsch 	return 0;
601dc5df763SJoerg Wunsch }
602dc5df763SJoerg Wunsch 
6036f4e0bebSPoul-Henning Kamp static int
604dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
605dc5df763SJoerg Wunsch {
6066182fdbdSPeter Wemm 	int cyl, st0, ret;
607dc5df763SJoerg Wunsch 
6086182fdbdSPeter Wemm 	ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
6096182fdbdSPeter Wemm 	if (ret) {
6106182fdbdSPeter Wemm 		(void)fdc_err(fdc,
611dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
612dc5df763SJoerg Wunsch 		return ret;
613dc5df763SJoerg Wunsch 	}
614dc5df763SJoerg Wunsch 
615dc5df763SJoerg Wunsch 	if (st0p)
616dc5df763SJoerg Wunsch 		*st0p = st0;
617dc5df763SJoerg Wunsch 
6186182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
619dc5df763SJoerg Wunsch 		/*
620dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
621dc5df763SJoerg Wunsch 		 */
622dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
623dc5df763SJoerg Wunsch 	}
624dc5df763SJoerg Wunsch 
6256182fdbdSPeter Wemm 	if (fd_in(fdc, &cyl) < 0) {
6266182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
627dc5df763SJoerg Wunsch 	}
628dc5df763SJoerg Wunsch 
629dc5df763SJoerg Wunsch 	if (cylp)
630dc5df763SJoerg Wunsch 		*cylp = cyl;
631dc5df763SJoerg Wunsch 
632dc5df763SJoerg Wunsch 	return 0;
633dc5df763SJoerg Wunsch }
634dc5df763SJoerg Wunsch 
635dc5df763SJoerg Wunsch 
6366f4e0bebSPoul-Henning Kamp static int
63764860614SJoerg Wunsch fd_read_status(fdc_p fdc)
638dc5df763SJoerg Wunsch {
639dc5df763SJoerg Wunsch 	int i, ret;
640b5e8ce9fSBruce Evans 
641246ed35dSJoerg Wunsch 	for (i = ret = 0; i < 7; i++) {
642b5e8ce9fSBruce Evans 		/*
64364860614SJoerg Wunsch 		 * XXX types are poorly chosen.  Only bytes can be read
644a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
645b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
646b5e8ce9fSBruce Evans 		 */
647b5e8ce9fSBruce Evans 		int status;
648b5e8ce9fSBruce Evans 
6496182fdbdSPeter Wemm 		ret = fd_in(fdc, &status);
650b5e8ce9fSBruce Evans 		fdc->status[i] = status;
651b5e8ce9fSBruce Evans 		if (ret != 0)
652dc5df763SJoerg Wunsch 			break;
653dc5df763SJoerg Wunsch 	}
654dc5df763SJoerg Wunsch 
655dc5df763SJoerg Wunsch 	if (ret == 0)
656dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
657dc5df763SJoerg Wunsch 	else
658dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
659dc5df763SJoerg Wunsch 
660dc5df763SJoerg Wunsch 	return ret;
661dc5df763SJoerg Wunsch }
662dc5df763SJoerg Wunsch 
6633a2f7427SDavid Greenman static int
66437286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc)
6655b81b6b3SRodney W. Grimes {
66637286586SPeter Wemm 	device_t dev;
667e219897aSJoerg Wunsch 	int ispnp, ispcmcia, nports;
6685b81b6b3SRodney W. Grimes 
66937286586SPeter Wemm 	dev = fdc->fdc_dev;
6705f830ea2SJoerg Wunsch 	ispnp = (fdc->flags & FDC_ISPNP) != 0;
6715f830ea2SJoerg Wunsch 	ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
6726182fdbdSPeter Wemm 	fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
6736182fdbdSPeter Wemm 	fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
6746182fdbdSPeter Wemm 
675b6e5f28eSPeter Wemm 	/*
6765f830ea2SJoerg Wunsch 	 * On standard ISA, we don't just use an 8 port range
6775f830ea2SJoerg Wunsch 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
6785f830ea2SJoerg Wunsch 	 * register at 0x3f6.
6795f830ea2SJoerg Wunsch 	 *
680b6e5f28eSPeter Wemm 	 * Isn't PC hardware wonderful.
6815f830ea2SJoerg Wunsch 	 *
6825f830ea2SJoerg Wunsch 	 * The Y-E Data PCMCIA FDC doesn't have this problem, it
6835f830ea2SJoerg Wunsch 	 * uses the register with offset 6 for pseudo-DMA, and the
6845f830ea2SJoerg Wunsch 	 * one with offset 7 as control register.
685b6e5f28eSPeter Wemm 	 */
686e219897aSJoerg Wunsch 	nports = ispcmcia ? 8 : (ispnp ? 1 : 6);
6876182fdbdSPeter Wemm 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
6886182fdbdSPeter Wemm 					     &fdc->rid_ioport, 0ul, ~0ul,
689e219897aSJoerg Wunsch 					     nports, RF_ACTIVE);
6906182fdbdSPeter Wemm 	if (fdc->res_ioport == 0) {
691e219897aSJoerg Wunsch 		device_printf(dev, "cannot reserve I/O port range (%d ports)\n",
692e219897aSJoerg Wunsch 			      nports);
69337286586SPeter Wemm 		return ENXIO;
6946182fdbdSPeter Wemm 	}
695427ccf00SDoug Rabson 	fdc->portt = rman_get_bustag(fdc->res_ioport);
696427ccf00SDoug Rabson 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
697427ccf00SDoug Rabson 
6985f830ea2SJoerg Wunsch 	if (!ispcmcia) {
699427ccf00SDoug Rabson 		/*
7005f830ea2SJoerg Wunsch 		 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
7015f830ea2SJoerg Wunsch 		 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
7025f830ea2SJoerg Wunsch 		 * by checking the size and adjust the port address
7035f830ea2SJoerg Wunsch 		 * accordingly.
704b6e5f28eSPeter Wemm 		 */
705b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
706b6e5f28eSPeter Wemm 			fdc->port_off = -2;
707b6e5f28eSPeter Wemm 
708b6e5f28eSPeter Wemm 		/*
7095f830ea2SJoerg Wunsch 		 * Register the control port range as rid 1 if it
7105f830ea2SJoerg Wunsch 		 * isn't there already. Most PnP BIOSen will have
7115f830ea2SJoerg Wunsch 		 * already done this but non-PnP configurations don't.
712a2639a18SPeter Wemm 		 *
7135f830ea2SJoerg Wunsch 		 * And some (!!) report 0x3f2-0x3f5 and completely
7145f830ea2SJoerg Wunsch 		 * leave out the control register!  It seems that some
7155f830ea2SJoerg Wunsch 		 * non-antique controller chips have a different
7165f830ea2SJoerg Wunsch 		 * method of programming the transfer speed which
7175f830ea2SJoerg Wunsch 		 * doesn't require the control register, but it's
7185f830ea2SJoerg Wunsch 		 * mighty bogus as the chip still responds to the
7195f830ea2SJoerg Wunsch 		 * address for the control register.
720427ccf00SDoug Rabson 		 */
721b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
722b9da888fSPeter Wemm 			u_long ctlstart;
723b9da888fSPeter Wemm 
724b6e5f28eSPeter Wemm 			/* Find the control port, usually 0x3f7 */
7255f830ea2SJoerg Wunsch 			ctlstart = rman_get_start(fdc->res_ioport) +
7265f830ea2SJoerg Wunsch 				fdc->port_off + 7;
727b6e5f28eSPeter Wemm 
728b6e5f28eSPeter Wemm 			bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
729b9da888fSPeter Wemm 		}
730b6e5f28eSPeter Wemm 
731b6e5f28eSPeter Wemm 		/*
732b6e5f28eSPeter Wemm 		 * Now (finally!) allocate the control port.
733b6e5f28eSPeter Wemm 		 */
734427ccf00SDoug Rabson 		fdc->rid_ctl = 1;
7355f830ea2SJoerg Wunsch 		fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
7365f830ea2SJoerg Wunsch 						  &fdc->rid_ctl,
737b6e5f28eSPeter Wemm 						  0ul, ~0ul, 1, RF_ACTIVE);
738427ccf00SDoug Rabson 		if (fdc->res_ctl == 0) {
7395f830ea2SJoerg Wunsch 			device_printf(dev,
740e219897aSJoerg Wunsch 		"cannot reserve control I/O port range (control port)\n");
74137286586SPeter Wemm 			return ENXIO;
742427ccf00SDoug Rabson 		}
743427ccf00SDoug Rabson 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
744427ccf00SDoug Rabson 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
7455f830ea2SJoerg Wunsch 	}
7466182fdbdSPeter Wemm 
7476182fdbdSPeter Wemm 	fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
7486182fdbdSPeter Wemm 					  &fdc->rid_irq, 0ul, ~0ul, 1,
7496182fdbdSPeter Wemm 					  RF_ACTIVE);
7506182fdbdSPeter Wemm 	if (fdc->res_irq == 0) {
751427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve interrupt line\n");
75237286586SPeter Wemm 		return ENXIO;
7536182fdbdSPeter Wemm 	}
7545f830ea2SJoerg Wunsch 
7555f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
7566182fdbdSPeter Wemm 		fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
7576182fdbdSPeter Wemm 						  &fdc->rid_drq, 0ul, ~0ul, 1,
7586182fdbdSPeter Wemm 						  RF_ACTIVE);
7596182fdbdSPeter Wemm 		if (fdc->res_drq == 0) {
760427ccf00SDoug Rabson 			device_printf(dev, "cannot reserve DMA request line\n");
76137286586SPeter Wemm 			return ENXIO;
7626182fdbdSPeter Wemm 		}
7636182fdbdSPeter Wemm 		fdc->dmachan = fdc->res_drq->r_start;
7645f830ea2SJoerg Wunsch 	}
76537286586SPeter Wemm 
76637286586SPeter Wemm 	return 0;
76737286586SPeter Wemm }
76837286586SPeter Wemm 
76937286586SPeter Wemm static void
77037286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
77137286586SPeter Wemm {
77237286586SPeter Wemm 	device_t dev;
77337286586SPeter Wemm 
77437286586SPeter Wemm 	dev = fdc->fdc_dev;
77537286586SPeter Wemm 	if (fdc->res_irq != 0) {
77637286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
77737286586SPeter Wemm 					fdc->res_irq);
77837286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
77937286586SPeter Wemm 				     fdc->res_irq);
78037286586SPeter Wemm 	}
78137286586SPeter Wemm 	if (fdc->res_ctl != 0) {
78237286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
78337286586SPeter Wemm 					fdc->res_ctl);
78437286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
78537286586SPeter Wemm 				     fdc->res_ctl);
78637286586SPeter Wemm 	}
78737286586SPeter Wemm 	if (fdc->res_ioport != 0) {
78837286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
78937286586SPeter Wemm 					fdc->res_ioport);
79037286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
79137286586SPeter Wemm 				     fdc->res_ioport);
79237286586SPeter Wemm 	}
79337286586SPeter Wemm 	if (fdc->res_drq != 0) {
79437286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
79537286586SPeter Wemm 					fdc->res_drq);
79637286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
79737286586SPeter Wemm 				     fdc->res_drq);
79837286586SPeter Wemm 	}
79937286586SPeter Wemm }
80037286586SPeter Wemm 
801246ed35dSJoerg Wunsch /*
802246ed35dSJoerg Wunsch  * Configuration/initialization stuff, per controller.
803246ed35dSJoerg Wunsch  */
80437286586SPeter Wemm 
80537286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = {
80637286586SPeter Wemm 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
80737286586SPeter Wemm 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
80837286586SPeter Wemm 	{0}
80937286586SPeter Wemm };
81037286586SPeter Wemm 
81137286586SPeter Wemm static int
8123aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
81337286586SPeter Wemm {
81437286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
81537286586SPeter Wemm 
81637286586SPeter Wemm 	switch (which) {
81737286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
81837286586SPeter Wemm 		*result = ivars->fdunit;
81937286586SPeter Wemm 		break;
82037286586SPeter Wemm 	default:
82137286586SPeter Wemm 		return ENOENT;
82237286586SPeter Wemm 	}
82337286586SPeter Wemm 	return 0;
82437286586SPeter Wemm }
82537286586SPeter Wemm 
82637286586SPeter Wemm static int
82737286586SPeter Wemm fdc_probe(device_t dev)
82837286586SPeter Wemm {
82937286586SPeter Wemm 	int	error, ic_type;
83037286586SPeter Wemm 	struct	fdc_data *fdc;
83137286586SPeter Wemm 
83237286586SPeter Wemm 	fdc = device_get_softc(dev);
83337286586SPeter Wemm 	bzero(fdc, sizeof *fdc);
83437286586SPeter Wemm 	fdc->fdc_dev = dev;
8355f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_isa;
83637286586SPeter Wemm 
83737286586SPeter Wemm 	/* Check pnp ids */
83837286586SPeter Wemm 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
83937286586SPeter Wemm 	if (error == ENXIO)
84037286586SPeter Wemm 		return ENXIO;
8415f830ea2SJoerg Wunsch 	if (error == 0)
8425f830ea2SJoerg Wunsch 		fdc->flags |= FDC_ISPNP;
84337286586SPeter Wemm 
84437286586SPeter Wemm 	/* Attempt to allocate our resources for the duration of the probe */
84537286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
84637286586SPeter Wemm 	if (error)
84737286586SPeter Wemm 		goto out;
8485b81b6b3SRodney W. Grimes 
84916111cedSAndrew Moore 	/* First - lets reset the floppy controller */
850427ccf00SDoug Rabson 	fdout_wr(fdc, 0);
85116111cedSAndrew Moore 	DELAY(100);
852427ccf00SDoug Rabson 	fdout_wr(fdc, FDO_FRST);
85316111cedSAndrew Moore 
8545b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
8556182fdbdSPeter Wemm 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
8566182fdbdSPeter Wemm 		   NE7_SPEC_2(2, 0), 0)) {
8576182fdbdSPeter Wemm 		error = ENXIO;
8586182fdbdSPeter Wemm 		goto out;
8595b81b6b3SRodney W. Grimes 	}
8606182fdbdSPeter Wemm 
8616182fdbdSPeter Wemm 	if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
8626182fdbdSPeter Wemm 		ic_type = (u_char)ic_type;
8636182fdbdSPeter Wemm 		switch (ic_type) {
8646182fdbdSPeter Wemm 		case 0x80:
8656182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 765 or clone");
8666182fdbdSPeter Wemm 			fdc->fdct = FDC_NE765;
8676182fdbdSPeter Wemm 			break;
8681a6bed68SJoerg Wunsch 		case 0x81:	/* not mentioned in any hardware doc */
8696182fdbdSPeter Wemm 		case 0x90:
8701a6bed68SJoerg Wunsch 			device_set_desc(dev,
8711a6bed68SJoerg Wunsch 		"enhanced floppy controller (i82077, NE72065 or clone)");
8721a6bed68SJoerg Wunsch 			fdc->fdct = FDC_ENHANCED;
8736182fdbdSPeter Wemm 			break;
8746182fdbdSPeter Wemm 		default:
8756182fdbdSPeter Wemm 			device_set_desc(dev, "generic floppy controller");
8766182fdbdSPeter Wemm 			fdc->fdct = FDC_UNKNOWN;
8776182fdbdSPeter Wemm 			break;
8786182fdbdSPeter Wemm 		}
8796182fdbdSPeter Wemm 	}
8806182fdbdSPeter Wemm 
8816182fdbdSPeter Wemm out:
88237286586SPeter Wemm 	fdc_release_resources(fdc);
8836182fdbdSPeter Wemm 	return (error);
8845b81b6b3SRodney W. Grimes }
8855b81b6b3SRodney W. Grimes 
8865f830ea2SJoerg Wunsch #if NCARD > 0
8875f830ea2SJoerg Wunsch 
8885f830ea2SJoerg Wunsch static int
8895f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev)
8905f830ea2SJoerg Wunsch {
8915f830ea2SJoerg Wunsch 	int	error;
8925f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
8935f830ea2SJoerg Wunsch 
8945f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
8955f830ea2SJoerg Wunsch 	bzero(fdc, sizeof *fdc);
8965f830ea2SJoerg Wunsch 	fdc->fdc_dev = dev;
8975f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_pcmcia;
8985f830ea2SJoerg Wunsch 
8995f830ea2SJoerg Wunsch 	fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
9005f830ea2SJoerg Wunsch 
9015f830ea2SJoerg Wunsch 	/* Attempt to allocate our resources for the duration of the probe */
9025f830ea2SJoerg Wunsch 	error = fdc_alloc_resources(fdc);
9035f830ea2SJoerg Wunsch 	if (error)
9045f830ea2SJoerg Wunsch 		goto out;
9055f830ea2SJoerg Wunsch 
9065f830ea2SJoerg Wunsch 	/* First - lets reset the floppy controller */
9075f830ea2SJoerg Wunsch 	fdout_wr(fdc, 0);
9085f830ea2SJoerg Wunsch 	DELAY(100);
9095f830ea2SJoerg Wunsch 	fdout_wr(fdc, FDO_FRST);
9105f830ea2SJoerg Wunsch 
9115f830ea2SJoerg Wunsch 	/* see if it can handle a command */
9125f830ea2SJoerg Wunsch 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
9135f830ea2SJoerg Wunsch 		   NE7_SPEC_2(2, 0), 0)) {
9145f830ea2SJoerg Wunsch 		error = ENXIO;
9155f830ea2SJoerg Wunsch 		goto out;
9165f830ea2SJoerg Wunsch 	}
9175f830ea2SJoerg Wunsch 
9185f830ea2SJoerg Wunsch 	device_set_desc(dev, "Y-E Data PCMCIA floppy");
9195f830ea2SJoerg Wunsch 	fdc->fdct = FDC_NE765;
9205f830ea2SJoerg Wunsch 
9215f830ea2SJoerg Wunsch out:
9225f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
9235f830ea2SJoerg Wunsch 	return (error);
9245f830ea2SJoerg Wunsch }
9255f830ea2SJoerg Wunsch 
926e219897aSJoerg Wunsch #endif /* NCARD > 0 */
927e219897aSJoerg Wunsch 
9285f830ea2SJoerg Wunsch static int
929e219897aSJoerg Wunsch fdc_detach(device_t dev)
9305f830ea2SJoerg Wunsch {
9315f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
9325f830ea2SJoerg Wunsch 	int	error;
9335f830ea2SJoerg Wunsch 
9345f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
9355f830ea2SJoerg Wunsch 
9365f830ea2SJoerg Wunsch 	/* have our children detached first */
9375f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
9385f830ea2SJoerg Wunsch 		return (error);
9395f830ea2SJoerg Wunsch 
940e219897aSJoerg Wunsch 	/* reset controller, turn motor off */
941e219897aSJoerg Wunsch 	fdout_wr(fdc, 0);
942e219897aSJoerg Wunsch 
943e219897aSJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0)
944e219897aSJoerg Wunsch 		isa_dma_release(fdc->dmachan);
945e219897aSJoerg Wunsch 
9465f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_ATTACHED) == 0) {
9475f830ea2SJoerg Wunsch 		device_printf(dev, "already unloaded\n");
9485f830ea2SJoerg Wunsch 		return (0);
9495f830ea2SJoerg Wunsch 	}
9505f830ea2SJoerg Wunsch 	fdc->flags &= ~FDC_ATTACHED;
9515f830ea2SJoerg Wunsch 
9525f830ea2SJoerg Wunsch 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
9535f830ea2SJoerg Wunsch 			  fdc->fdc_intr);
9545f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
9555f830ea2SJoerg Wunsch 	device_printf(dev, "unload\n");
9565f830ea2SJoerg Wunsch 	return (0);
9575f830ea2SJoerg Wunsch }
9585f830ea2SJoerg Wunsch 
9595b81b6b3SRodney W. Grimes /*
96037286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
9615b81b6b3SRodney W. Grimes  */
9626182fdbdSPeter Wemm static void
96337286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
9645b81b6b3SRodney W. Grimes {
9651a6bed68SJoerg Wunsch 	int	disabled, flags;
96637286586SPeter Wemm 	struct fdc_ivars *ivar;
9676182fdbdSPeter Wemm 	device_t child;
96892200632SGarrett Wollman 
9697cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
97037286586SPeter Wemm 	if (ivar == NULL)
9716182fdbdSPeter Wemm 		return;
97237286586SPeter Wemm 	if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
97337286586SPeter Wemm 		ivar->fdunit = 0;
974fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
97537286586SPeter Wemm 	if (child == NULL)
9766182fdbdSPeter Wemm 		return;
97737286586SPeter Wemm 	device_set_ivars(child, ivar);
9781a6bed68SJoerg Wunsch 	if (resource_int_value(name, unit, "flags", &flags) == 0)
9791a6bed68SJoerg Wunsch 		 device_set_flags(child, flags);
980a97c75b7SDoug Rabson 	if (resource_int_value(name, unit, "disabled", &disabled) == 0
981a97c75b7SDoug Rabson 	    && disabled != 0)
9826182fdbdSPeter Wemm 		device_disable(child);
9836182fdbdSPeter Wemm }
9846182fdbdSPeter Wemm 
9856182fdbdSPeter Wemm static int
9866182fdbdSPeter Wemm fdc_attach(device_t dev)
9876182fdbdSPeter Wemm {
98837286586SPeter Wemm 	struct	fdc_data *fdc;
9892398f0cdSPeter Wemm 	const char *name, *dname;
9905d54fe91SJoerg Wunsch 	int	i, error, dunit;
991427ccf00SDoug Rabson 
99237286586SPeter Wemm 	fdc = device_get_softc(dev);
99337286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
99437286586SPeter Wemm 	if (error) {
995f8ce7dd5SJoerg Wunsch 		device_printf(dev, "cannot re-acquire resources\n");
99637286586SPeter Wemm 		return error;
99737286586SPeter Wemm 	}
99837286586SPeter Wemm 	error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
9993c36743eSMark Murray 			       INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
10003c36743eSMark Murray 			       &fdc->fdc_intr);
100137286586SPeter Wemm 	if (error) {
100237286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
100337286586SPeter Wemm 		return error;
100437286586SPeter Wemm 	}
100537286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
1006fb35bd37SJoerg Wunsch 	fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET;
10076182fdbdSPeter Wemm 
10085f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
1009f8ce7dd5SJoerg Wunsch 		/*
1010f8ce7dd5SJoerg Wunsch 		 * Acquire the DMA channel forever, the driver will do
1011f8ce7dd5SJoerg Wunsch 		 * the rest
1012f8ce7dd5SJoerg Wunsch 		 * XXX should integrate with rman
1013f8ce7dd5SJoerg Wunsch 		 */
1014100f78bbSSujal Patel 		isa_dma_acquire(fdc->dmachan);
1015f8ce7dd5SJoerg Wunsch 		isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
10165f830ea2SJoerg Wunsch 	}
10175b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
10186182fdbdSPeter Wemm 
10193a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
102064860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
10218177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
10225b81b6b3SRodney W. Grimes 
10236182fdbdSPeter Wemm 	/*
102437286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
102537286586SPeter Wemm 	 * devices from the BIOS unless overridden.
10266182fdbdSPeter Wemm 	 */
1027ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
10282398f0cdSPeter Wemm 	i = 0;
10292398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
10302398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
103137286586SPeter Wemm 
103260444853SJoerg Wunsch 	if ((error = bus_generic_attach(dev)) != 0)
103360444853SJoerg Wunsch 		return (error);
103460444853SJoerg Wunsch 
103560444853SJoerg Wunsch 	return (0);
10366182fdbdSPeter Wemm }
10376182fdbdSPeter Wemm 
103815317dd8SMatthew N. Dodd static int
10396182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
10406182fdbdSPeter Wemm {
10411a6bed68SJoerg Wunsch 	int retval = 0, flags;
104215317dd8SMatthew N. Dodd 
104315317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
10441a6bed68SJoerg Wunsch 	retval += printf(" on %s drive %d", device_get_nameunit(me),
104537286586SPeter Wemm 	       fdc_get_fdunit(child));
10461a6bed68SJoerg Wunsch 	if ((flags = device_get_flags(me)) != 0)
10471a6bed68SJoerg Wunsch 		retval += printf(" flags %#x", flags);
10481a6bed68SJoerg Wunsch 	retval += printf("\n");
104915317dd8SMatthew N. Dodd 
105015317dd8SMatthew N. Dodd 	return (retval);
10516182fdbdSPeter Wemm }
10526182fdbdSPeter Wemm 
105316e68fc6SPeter Wemm static device_method_t fdc_methods[] = {
105416e68fc6SPeter Wemm 	/* Device interface */
105516e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fdc_probe),
105616e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fdc_attach),
1057e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
105816e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
105916e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend),
106016e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume),
106116e68fc6SPeter Wemm 
106216e68fc6SPeter Wemm 	/* Bus interface */
106316e68fc6SPeter Wemm 	DEVMETHOD(bus_print_child,	fdc_print_child),
106437286586SPeter Wemm 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
106516e68fc6SPeter Wemm 	/* Our children never use any other bus interface methods. */
106616e68fc6SPeter Wemm 
106716e68fc6SPeter Wemm 	{ 0, 0 }
106816e68fc6SPeter Wemm };
106916e68fc6SPeter Wemm 
107016e68fc6SPeter Wemm static driver_t fdc_driver = {
107116e68fc6SPeter Wemm 	"fdc",
107216e68fc6SPeter Wemm 	fdc_methods,
107316e68fc6SPeter Wemm 	sizeof(struct fdc_data)
107416e68fc6SPeter Wemm };
107516e68fc6SPeter Wemm 
107616e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
10775f063c7bSMike Smith DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0);
107816e68fc6SPeter Wemm 
10795f830ea2SJoerg Wunsch #if NCARD > 0
10805f830ea2SJoerg Wunsch 
10815f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = {
10825f830ea2SJoerg Wunsch 	/* Device interface */
10835f830ea2SJoerg Wunsch 	DEVMETHOD(device_probe,		fdc_pccard_probe),
10845f830ea2SJoerg Wunsch 	DEVMETHOD(device_attach,	fdc_attach),
1085e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
10865f830ea2SJoerg Wunsch 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
10875f830ea2SJoerg Wunsch 	DEVMETHOD(device_suspend,	bus_generic_suspend),
10885f830ea2SJoerg Wunsch 	DEVMETHOD(device_resume,	bus_generic_resume),
10895f830ea2SJoerg Wunsch 
10905f830ea2SJoerg Wunsch 	/* Bus interface */
10915f830ea2SJoerg Wunsch 	DEVMETHOD(bus_print_child,	fdc_print_child),
10925f830ea2SJoerg Wunsch 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
10935f830ea2SJoerg Wunsch 	/* Our children never use any other bus interface methods. */
10945f830ea2SJoerg Wunsch 
10955f830ea2SJoerg Wunsch 	{ 0, 0 }
10965f830ea2SJoerg Wunsch };
10975f830ea2SJoerg Wunsch 
10985f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = {
10995f830ea2SJoerg Wunsch 	"fdc",
11005f830ea2SJoerg Wunsch 	fdc_pccard_methods,
11015f830ea2SJoerg Wunsch 	sizeof(struct fdc_data)
11025f830ea2SJoerg Wunsch };
11035f830ea2SJoerg Wunsch 
11045f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
11055f830ea2SJoerg Wunsch 
11065f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
11075f830ea2SJoerg Wunsch 
110860444853SJoerg Wunsch /*
11091a6bed68SJoerg Wunsch  * Create a clone device upon request by devfs.
111060444853SJoerg Wunsch  */
11113f54a085SPoul-Henning Kamp static void
111280909a7dSJoerg Wunsch fd_clone(void *arg, char *name, int namelen, dev_t *dev)
11133f54a085SPoul-Henning Kamp {
111460444853SJoerg Wunsch 	struct	fd_data *fd;
11151a6bed68SJoerg Wunsch 	int i, u;
11163f54a085SPoul-Henning Kamp 	char *n;
11171a6bed68SJoerg Wunsch 	size_t l;
11183f54a085SPoul-Henning Kamp 
111960444853SJoerg Wunsch 	fd = (struct fd_data *)arg;
11203f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
11213f54a085SPoul-Henning Kamp 		return;
1122db901281SPoul-Henning Kamp 	if (dev_stdclone(name, &n, "fd", &u) != 2)
11233f54a085SPoul-Henning Kamp 		return;
11241a6bed68SJoerg Wunsch 	l = strlen(n);
11251a6bed68SJoerg Wunsch 	if (l == 1 && *n >= 'a' && *n <= 'h') {
11261a6bed68SJoerg Wunsch 		/*
11271a6bed68SJoerg Wunsch 		 * Trailing letters a through h denote
11281a6bed68SJoerg Wunsch 		 * pseudo-partitions.  We don't support true
11291a6bed68SJoerg Wunsch 		 * (UFS-style) partitions, so we just implement them
11301a6bed68SJoerg Wunsch 		 * as symlinks if someone asks us nicely.
11311a6bed68SJoerg Wunsch 		 */
113260444853SJoerg Wunsch 		*dev = make_dev_alias(fd->masterdev, name);
11331a6bed68SJoerg Wunsch 		return;
11341a6bed68SJoerg Wunsch 	}
11351a6bed68SJoerg Wunsch 	if (l >= 2 && l <= 5 && *n == '.') {
11361a6bed68SJoerg Wunsch 		/*
11371a6bed68SJoerg Wunsch 		 * Trailing numbers, preceded by a dot, denote
11381a6bed68SJoerg Wunsch 		 * subdevices for different densities.  Historically,
11391a6bed68SJoerg Wunsch 		 * they have been named by density (like fd0.1440),
11401a6bed68SJoerg Wunsch 		 * but we allow arbitrary numbers between 1 and 4
11411a6bed68SJoerg Wunsch 		 * digits, so fd0.1 through fd0.15 are possible as
11421a6bed68SJoerg Wunsch 		 * well.
11431a6bed68SJoerg Wunsch 		 */
11441a6bed68SJoerg Wunsch 		for (i = 1; i < l; i++)
11451a6bed68SJoerg Wunsch 			if (n[i] < '0' || n[i] > '9')
11461a6bed68SJoerg Wunsch 				return;
11471a6bed68SJoerg Wunsch 		for (i = 0; i < NUMDENS - 1; i++)
11481a6bed68SJoerg Wunsch 			if (fd->clonedevs[i] == NODEV) {
11491a6bed68SJoerg Wunsch 				*dev = make_dev(&fd_cdevsw,
11501a6bed68SJoerg Wunsch 						FDNUMTOUNIT(u) + i + 1,
11511a6bed68SJoerg Wunsch 						UID_ROOT, GID_OPERATOR, 0640,
11521a6bed68SJoerg Wunsch 						name);
11531a6bed68SJoerg Wunsch 				fd->clonedevs[i] = *dev;
11541a6bed68SJoerg Wunsch 				return;
11551a6bed68SJoerg Wunsch 			}
11563f54a085SPoul-Henning Kamp 	}
11573f54a085SPoul-Henning Kamp }
11583f54a085SPoul-Henning Kamp 
115916e68fc6SPeter Wemm /*
1160246ed35dSJoerg Wunsch  * Configuration/initialization, per drive.
116116e68fc6SPeter Wemm  */
11626182fdbdSPeter Wemm static int
11636182fdbdSPeter Wemm fd_probe(device_t dev)
11646182fdbdSPeter Wemm {
11656182fdbdSPeter Wemm 	int	i;
11661a6bed68SJoerg Wunsch 	u_int	st0, st3;
11676182fdbdSPeter Wemm 	struct	fd_data *fd;
11686182fdbdSPeter Wemm 	struct	fdc_data *fdc;
11696182fdbdSPeter Wemm 	fdsu_t	fdsu;
11701a6bed68SJoerg Wunsch 	int	flags;
11716182fdbdSPeter Wemm 
11726182fdbdSPeter Wemm 	fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
11736182fdbdSPeter Wemm 	fd = device_get_softc(dev);
11746182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
11751a6bed68SJoerg Wunsch 	flags = device_get_flags(dev);
11766182fdbdSPeter Wemm 
11776182fdbdSPeter Wemm 	bzero(fd, sizeof *fd);
11786182fdbdSPeter Wemm 	fd->dev = dev;
11796182fdbdSPeter Wemm 	fd->fdc = fdc;
11806182fdbdSPeter Wemm 	fd->fdsu = fdsu;
11816182fdbdSPeter Wemm 	fd->fdu = device_get_unit(dev);
11821a6bed68SJoerg Wunsch 	fd->flags = FD_UA;	/* make sure fdautoselect() will be called */
11836182fdbdSPeter Wemm 
11841a6bed68SJoerg Wunsch 	fd->type = FD_DTYPE(flags);
1185038d1bbdSJoerg Wunsch /*
1186038d1bbdSJoerg Wunsch  * XXX I think using __i386__ is wrong here since we actually want to probe
1187038d1bbdSJoerg Wunsch  * for the machine type, not the CPU type (so non-PC arch's like the PC98 will
1188038d1bbdSJoerg Wunsch  * fail the probe).  However, for whatever reason, testing for _MACHINE_ARCH
1189038d1bbdSJoerg Wunsch  * == i386 breaks the test on FreeBSD/Alpha.
1190038d1bbdSJoerg Wunsch  */
1191038d1bbdSJoerg Wunsch #ifdef __i386__
11921a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) {
11931a6bed68SJoerg Wunsch 		/* Look up what the BIOS thinks we have. */
11941a6bed68SJoerg Wunsch 		if (fd->fdu == 0) {
11955f830ea2SJoerg Wunsch 			if ((fdc->flags & FDC_ISPCMCIA))
11961a6bed68SJoerg Wunsch 				/*
11971a6bed68SJoerg Wunsch 				 * Somewhat special.  No need to force the
11981a6bed68SJoerg Wunsch 				 * user to set device flags, since the Y-E
11991a6bed68SJoerg Wunsch 				 * Data PCMCIA floppy is always a 1.44 MB
12001a6bed68SJoerg Wunsch 				 * device.
12011a6bed68SJoerg Wunsch 				 */
12021a6bed68SJoerg Wunsch 				fd->type = FDT_144M;
12030722d6abSJoerg Wunsch 			else
12041a6bed68SJoerg Wunsch 				fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
12051a6bed68SJoerg Wunsch 		} else {
12061a6bed68SJoerg Wunsch 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
12076b7bd95bSJoerg Wunsch 		}
12081a6bed68SJoerg Wunsch 		if (fd->type == FDT_288M_1)
12091a6bed68SJoerg Wunsch 			fd->type = FDT_288M;
12101a6bed68SJoerg Wunsch 	}
1211038d1bbdSJoerg Wunsch #endif /* __i386__ */
12126182fdbdSPeter Wemm 	/* is there a unit? */
12131a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE)
12146182fdbdSPeter Wemm 		return (ENXIO);
12156182fdbdSPeter Wemm 
12166182fdbdSPeter Wemm 	/* select it */
12176182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNON);
1218fb35bd37SJoerg Wunsch 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
12196182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
12206182fdbdSPeter Wemm 
12218de0675cSPeter Wemm 	/* XXX This doesn't work before the first set_motor() */
12221a6bed68SJoerg Wunsch 	if ((fdc->flags & FDC_HAS_FIFO) == 0  &&
12231a6bed68SJoerg Wunsch 	    fdc->fdct == FDC_ENHANCED &&
12241a6bed68SJoerg Wunsch 	    (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 &&
12251a6bed68SJoerg Wunsch 	    enable_fifo(fdc) == 0) {
1226b6e5f28eSPeter Wemm 		device_printf(device_get_parent(dev),
1227b6e5f28eSPeter Wemm 		    "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1228d66c374fSTor Egge 	}
12296182fdbdSPeter Wemm 
12301a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0) {
1231dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
12321a6bed68SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
12331a6bed68SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
1234dc5df763SJoerg Wunsch 			/* Seek some steps... */
12356182fdbdSPeter Wemm 			if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1236dc5df763SJoerg Wunsch 				/* ...wait a moment... */
1237dc5df763SJoerg Wunsch 				DELAY(300000);
1238dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
12396182fdbdSPeter Wemm 				fd_sense_int(fdc, 0, 0);
1240dc5df763SJoerg Wunsch 			}
1241dc5df763SJoerg Wunsch 		}
1242dc5df763SJoerg Wunsch 
12436b7bd95bSJoerg Wunsch 		for (i = 0; i < 2; i++) {
12446b7bd95bSJoerg Wunsch 			/*
12456b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
12461a6bed68SJoerg Wunsch 			 * heads have been beyond cylinder 76, since
12471a6bed68SJoerg Wunsch 			 * most FDCs still barf when attempting to
12481a6bed68SJoerg Wunsch 			 * recalibrate more than 77 steps
12496b7bd95bSJoerg Wunsch 			 */
1250dc5df763SJoerg Wunsch 			/* go back to 0: */
12516182fdbdSPeter Wemm 			if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
12526b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
12536b7bd95bSJoerg Wunsch 				DELAY(i == 0 ? 1000000 : 300000);
12545b81b6b3SRodney W. Grimes 
12556b7bd95bSJoerg Wunsch 				/* anything responding? */
1256dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
1257dc5df763SJoerg Wunsch 				    (st0 & NE7_ST0_EC) == 0)
12586b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
12596b7bd95bSJoerg Wunsch 			}
1260dc5df763SJoerg Wunsch 		}
12611a6bed68SJoerg Wunsch 	}
12626b7bd95bSJoerg Wunsch 
12636182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNOFF);
12643a2f7427SDavid Greenman 
12651a6bed68SJoerg Wunsch 	if ((flags & FD_NO_PROBE) == 0 &&
12661a6bed68SJoerg Wunsch 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
12676182fdbdSPeter Wemm 		return (ENXIO);
12685b81b6b3SRodney W. Grimes 
12691a6bed68SJoerg Wunsch 	switch (fd->type) {
12701a6bed68SJoerg Wunsch 	case FDT_12M:
12711a6bed68SJoerg Wunsch 		device_set_desc(dev, "1200-KB 5.25\" drive");
12721a6bed68SJoerg Wunsch 		fd->type = FDT_12M;
12731a6bed68SJoerg Wunsch 		break;
12741a6bed68SJoerg Wunsch 	case FDT_144M:
12751a6bed68SJoerg Wunsch 		device_set_desc(dev, "1440-KB 3.5\" drive");
12761a6bed68SJoerg Wunsch 		fd->type = FDT_144M;
12771a6bed68SJoerg Wunsch 		break;
12781a6bed68SJoerg Wunsch 	case FDT_288M:
12791a6bed68SJoerg Wunsch 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
12801a6bed68SJoerg Wunsch 		fd->type = FDT_288M;
12811a6bed68SJoerg Wunsch 		break;
12821a6bed68SJoerg Wunsch 	case FDT_360K:
12831a6bed68SJoerg Wunsch 		device_set_desc(dev, "360-KB 5.25\" drive");
12841a6bed68SJoerg Wunsch 		fd->type = FDT_360K;
12851a6bed68SJoerg Wunsch 		break;
12861a6bed68SJoerg Wunsch 	case FDT_720K:
12871a6bed68SJoerg Wunsch 		device_set_desc(dev, "720-KB 3.5\" drive");
12881a6bed68SJoerg Wunsch 		fd->type = FDT_720K;
12891a6bed68SJoerg Wunsch 		break;
12901a6bed68SJoerg Wunsch 	default:
12911a6bed68SJoerg Wunsch 		return (ENXIO);
12921a6bed68SJoerg Wunsch 	}
1293dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1294b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1295b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
12963a2f7427SDavid Greenman 	fd->options = 0;
129702a19910SJustin T. Gibbs 	callout_handle_init(&fd->toffhandle);
129802a19910SJustin T. Gibbs 	callout_handle_init(&fd->tohandle);
12995b81b6b3SRodney W. Grimes 
13001a6bed68SJoerg Wunsch 	/* initialize densities for subdevices */
13011a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS; i++)
13021a6bed68SJoerg Wunsch 		memcpy(fd->fts + i, fd_native_types + fd->type,
13031a6bed68SJoerg Wunsch 		       sizeof(struct fd_type));
13046182fdbdSPeter Wemm 	return (0);
13056182fdbdSPeter Wemm }
13066182fdbdSPeter Wemm 
13076182fdbdSPeter Wemm static int
13086182fdbdSPeter Wemm fd_attach(device_t dev)
13096182fdbdSPeter Wemm {
13106182fdbdSPeter Wemm 	struct	fd_data *fd;
131164860614SJoerg Wunsch 	static	int cdevsw_add_done;
131260444853SJoerg Wunsch 	int i;
13136182fdbdSPeter Wemm 
131402211baeSNick Hibma 	if (!cdevsw_add_done) {
1315475ad603SPeter Wemm 		cdevsw_add(&fd_cdevsw);	/* XXX */
131664860614SJoerg Wunsch 		cdevsw_add_done = 1;
131702211baeSNick Hibma 	}
131864860614SJoerg Wunsch 	fd = device_get_softc(dev);
131960444853SJoerg Wunsch 	fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, fd, 1000);
13205f431174SJoerg Wunsch 	fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6,
13213f54a085SPoul-Henning Kamp 				 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
13221a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS - 1; i++)
132360444853SJoerg Wunsch 		fd->clonedevs[i] = NODEV;
13246182fdbdSPeter Wemm 	devstat_add_entry(&fd->device_stats, device_get_name(dev),
1325f8ce7dd5SJoerg Wunsch 			  device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
13262a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
13272a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_FD);
13286182fdbdSPeter Wemm 	return (0);
13295b81b6b3SRodney W. Grimes }
13305b81b6b3SRodney W. Grimes 
13315f830ea2SJoerg Wunsch static int
13325f830ea2SJoerg Wunsch fd_detach(device_t dev)
13335f830ea2SJoerg Wunsch {
13345f830ea2SJoerg Wunsch 	struct	fd_data *fd;
133560444853SJoerg Wunsch 	int i;
13365f830ea2SJoerg Wunsch 
13375f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
133860444853SJoerg Wunsch 	untimeout(fd_turnoff, fd, fd->toffhandle);
1339e219897aSJoerg Wunsch 	devstat_remove_entry(&fd->device_stats);
1340e219897aSJoerg Wunsch 	destroy_dev(fd->masterdev);
13411a6bed68SJoerg Wunsch 	for (i = 0; i < NUMDENS - 1; i++)
134260444853SJoerg Wunsch 		if (fd->clonedevs[i] != NODEV)
134360444853SJoerg Wunsch 			destroy_dev(fd->clonedevs[i]);
1344e219897aSJoerg Wunsch 	cdevsw_remove(&fd_cdevsw);
1345e219897aSJoerg Wunsch 	EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag);
13465f830ea2SJoerg Wunsch 
13475f830ea2SJoerg Wunsch 	return (0);
13485f830ea2SJoerg Wunsch }
13495f830ea2SJoerg Wunsch 
135016e68fc6SPeter Wemm static device_method_t fd_methods[] = {
135116e68fc6SPeter Wemm 	/* Device interface */
135216e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
135316e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
13545f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
135516e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
135616e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
135716e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
135816e68fc6SPeter Wemm 
135916e68fc6SPeter Wemm 	{ 0, 0 }
136016e68fc6SPeter Wemm };
136116e68fc6SPeter Wemm 
136216e68fc6SPeter Wemm static driver_t fd_driver = {
136316e68fc6SPeter Wemm 	"fd",
136416e68fc6SPeter Wemm 	fd_methods,
136516e68fc6SPeter Wemm 	sizeof(struct fd_data)
136616e68fc6SPeter Wemm };
136716e68fc6SPeter Wemm 
1368475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
136916e68fc6SPeter Wemm 
1370246ed35dSJoerg Wunsch /*
1371246ed35dSJoerg Wunsch  * More auxiliary functions.
1372246ed35dSJoerg Wunsch  */
1373246ed35dSJoerg Wunsch /*
1374246ed35dSJoerg Wunsch  * Motor control stuff.
1375246ed35dSJoerg Wunsch  * Remember to not deselect the drive we're working on.
1376246ed35dSJoerg Wunsch  */
13773a2f7427SDavid Greenman static void
13786182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon)
13795b81b6b3SRodney W. Grimes {
1380fb35bd37SJoerg Wunsch 	int fdout;
13813a2f7427SDavid Greenman 
1382fb35bd37SJoerg Wunsch 	fdout = fdc->fdout;
13833a2f7427SDavid Greenman 	if (turnon) {
13843a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
1385fb35bd37SJoerg Wunsch 		fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
13863a2f7427SDavid Greenman 	} else
13873a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
13886182fdbdSPeter Wemm 	fdc->fdout = fdout;
1389fb35bd37SJoerg Wunsch 	fdout_wr(fdc, fdout);
13903a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
13913a2f7427SDavid Greenman }
13923a2f7427SDavid Greenman 
1393381fe1aaSGarrett Wollman static void
13946182fdbdSPeter Wemm fd_turnoff(void *xfd)
13955b81b6b3SRodney W. Grimes {
1396f5f7ba03SJordan K. Hubbard 	int	s;
13976182fdbdSPeter Wemm 	fd_p fd = xfd;
1398dc16046fSJoerg Wunsch 
13996182fdbdSPeter Wemm 	TRACE1("[fd%d: turnoff]", fd->fdu);
14008335c1b8SBruce Evans 
14015f830ea2SJoerg Wunsch 	s = splbio();
14028335c1b8SBruce Evans 	/*
14038335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
14045f830ea2SJoerg Wunsch 	 *
14055f830ea2SJoerg Wunsch 	 * If we got here, this could only mean we missed an interrupt.
14065f830ea2SJoerg Wunsch 	 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
14075f830ea2SJoerg Wunsch 	 * after a controller reset.  Just schedule a pseudo-interrupt
14085f830ea2SJoerg Wunsch 	 * so the state machine gets re-entered.
14098335c1b8SBruce Evans 	 */
14106182fdbdSPeter Wemm 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
14115f830ea2SJoerg Wunsch 		fdc_intr(fd->fdc);
14125f830ea2SJoerg Wunsch 		splx(s);
14138335c1b8SBruce Evans 		return;
14148335c1b8SBruce Evans 	}
14158335c1b8SBruce Evans 
14165b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
14176182fdbdSPeter Wemm 	set_motor(fd->fdc, fd->fdsu, TURNOFF);
1418f5f7ba03SJordan K. Hubbard 	splx(s);
14195b81b6b3SRodney W. Grimes }
14205b81b6b3SRodney W. Grimes 
14213a2f7427SDavid Greenman static void
14226182fdbdSPeter Wemm fd_motor_on(void *xfd)
14235b81b6b3SRodney W. Grimes {
1424f5f7ba03SJordan K. Hubbard 	int	s;
14256182fdbdSPeter Wemm 	fd_p fd = xfd;
1426f5f7ba03SJordan K. Hubbard 
1427f5f7ba03SJordan K. Hubbard 	s = splbio();
14285b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
14295b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
14305b81b6b3SRodney W. Grimes 	{
14316182fdbdSPeter Wemm 		fdc_intr(fd->fdc);
14325b81b6b3SRodney W. Grimes 	}
1433f5f7ba03SJordan K. Hubbard 	splx(s);
14345b81b6b3SRodney W. Grimes }
14355b81b6b3SRodney W. Grimes 
14363a2f7427SDavid Greenman static void
14376182fdbdSPeter Wemm fd_turnon(fd_p fd)
14385b81b6b3SRodney W. Grimes {
14395b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
14405b81b6b3SRodney W. Grimes 	{
14413a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
14426182fdbdSPeter Wemm 		set_motor(fd->fdc, fd->fdsu, TURNON);
14436182fdbdSPeter Wemm 		timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
14445b81b6b3SRodney W. Grimes 	}
14455b81b6b3SRodney W. Grimes }
14465b81b6b3SRodney W. Grimes 
1447381fe1aaSGarrett Wollman static void
1448dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
14495b81b6b3SRodney W. Grimes {
14503a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
1451427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
14523a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
14533a2f7427SDavid Greenman 	DELAY(100);
14543a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
1455427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
14563a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
14573a2f7427SDavid Greenman 	DELAY(100);
1458427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout);
14593a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
14603a2f7427SDavid Greenman 
146164860614SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
14626182fdbdSPeter Wemm 	(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1463dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1464dc5df763SJoerg Wunsch 		     0);
1465d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1466d66c374fSTor Egge 		(void) enable_fifo(fdc);
14675b81b6b3SRodney W. Grimes }
14685b81b6b3SRodney W. Grimes 
1469246ed35dSJoerg Wunsch /*
1470246ed35dSJoerg Wunsch  * FDC IO functions, take care of the main status register, timeout
1471246ed35dSJoerg Wunsch  * in case the desired status bits are never set.
1472246ed35dSJoerg Wunsch  */
1473b5e8ce9fSBruce Evans static int
14746182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr)
1475dc5df763SJoerg Wunsch {
1476dc5df763SJoerg Wunsch 	int i, j = 100000;
1477427ccf00SDoug Rabson 	while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1478dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1479dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
14806182fdbdSPeter Wemm 			return fdc_err(fdc, "ready for output in input\n");
1481dc5df763SJoerg Wunsch 	if (j <= 0)
14826182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1483d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1484427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1485dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1486dc5df763SJoerg Wunsch 	*ptr = i;
1487dc5df763SJoerg Wunsch 	return 0;
1488d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1489427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1490dc5df763SJoerg Wunsch 	if (ptr)
1491dc5df763SJoerg Wunsch 		*ptr = i;
1492dc5df763SJoerg Wunsch 	return 0;
1493d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1494dc5df763SJoerg Wunsch }
1495dc5df763SJoerg Wunsch 
1496dc5df763SJoerg Wunsch int
14976182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x)
14985b81b6b3SRodney W. Grimes {
14993b3837dbSRodney W. Grimes 	int i;
15005b81b6b3SRodney W. Grimes 
15013b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
15023b3837dbSRodney W. Grimes 	i = 100000;
1503427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0);
15046182fdbdSPeter Wemm 	if (i <= 0) return fdc_err(fdc, "direction bit not set\n");
15053b3837dbSRodney W. Grimes 
15063b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
15073b3837dbSRodney W. Grimes 	i = 100000;
1508427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0);
150916b04b6aSJoerg Wunsch 	if (i <= 0)
15106182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "output ready timeout\n": 0);
15113b3837dbSRodney W. Grimes 
15123b3837dbSRodney W. Grimes 	/* Send the command and return */
1513427ccf00SDoug Rabson 	fddata_wr(fdc, x);
15143a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
15155b81b6b3SRodney W. Grimes 	return (0);
15165b81b6b3SRodney W. Grimes }
15175b81b6b3SRodney W. Grimes 
1518246ed35dSJoerg Wunsch /*
1519246ed35dSJoerg Wunsch  * Block device driver interface functions (interspersed with even more
1520246ed35dSJoerg Wunsch  * auxiliary functions).
1521246ed35dSJoerg Wunsch  */
1522381fe1aaSGarrett Wollman int
1523b40ce416SJulian Elischer Fdopen(dev_t dev, int flags, int mode, struct thread *td)
15245b81b6b3SRodney W. Grimes {
15255b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
152620a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
15276182fdbdSPeter Wemm 	fd_p	fd;
1528b99f0a4aSAndrew Moore 	fdc_p	fdc;
15291a6bed68SJoerg Wunsch  	int rv, unitattn, dflags;
15305b81b6b3SRodney W. Grimes 
15316182fdbdSPeter Wemm 	if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1532b99f0a4aSAndrew Moore 		return (ENXIO);
15336182fdbdSPeter Wemm 	fdc = fd->fdc;
15341a6bed68SJoerg Wunsch 	if ((fdc == NULL) || (fd->type == FDT_NONE))
1535b99f0a4aSAndrew Moore 		return (ENXIO);
1536b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1537b99f0a4aSAndrew Moore 		return (ENXIO);
15381a6bed68SJoerg Wunsch 	dflags = device_get_flags(fd->dev);
15393e425b96SJulian Elischer 	/*
15401a6bed68SJoerg Wunsch 	 * This is a bit bogus.  It's still possible that e. g. a
15411a6bed68SJoerg Wunsch 	 * descriptor gets inherited to a child, but then it's at
15421a6bed68SJoerg Wunsch 	 * least for the same subdevice.  By checking FD_OPEN here, we
15431a6bed68SJoerg Wunsch 	 * can ensure that a device isn't attempted to be opened with
15441a6bed68SJoerg Wunsch 	 * different densities at the same time where the second open
15451a6bed68SJoerg Wunsch 	 * could clobber the settings from the first one.
15463e425b96SJulian Elischer 	 */
15471a6bed68SJoerg Wunsch 	if (fd->flags & FD_OPEN)
15481a6bed68SJoerg Wunsch 		return (EBUSY);
15491a6bed68SJoerg Wunsch 
15501a6bed68SJoerg Wunsch 	if (type == 0) {
15511a6bed68SJoerg Wunsch 		if (flags & FNONBLOCK) {
15521a6bed68SJoerg Wunsch 			/*
15531a6bed68SJoerg Wunsch 			 * Unfortunately, physio(9) discards its ioflag
15541a6bed68SJoerg Wunsch 			 * argument, thus preventing us from seeing the
15551a6bed68SJoerg Wunsch 			 * IO_NDELAY bit.  So we need to keep track
15561a6bed68SJoerg Wunsch 			 * ourselves.
15571a6bed68SJoerg Wunsch 			 */
15581a6bed68SJoerg Wunsch 			fd->flags |= FD_NONBLOCK;
15591a6bed68SJoerg Wunsch 			fd->ft = 0;
15601a6bed68SJoerg Wunsch 		} else {
15611a6bed68SJoerg Wunsch 			/*
15621a6bed68SJoerg Wunsch 			 * Figure out a unit attention condition.
15631a6bed68SJoerg Wunsch 			 *
15641a6bed68SJoerg Wunsch 			 * If UA has been forced, proceed.
15651a6bed68SJoerg Wunsch 			 *
15661a6bed68SJoerg Wunsch 			 * If motor is off, turn it on for a moment
15671a6bed68SJoerg Wunsch 			 * and select our drive, in order to read the
15681a6bed68SJoerg Wunsch 			 * UA hardware signal.
15691a6bed68SJoerg Wunsch 			 *
15701a6bed68SJoerg Wunsch 			 * If motor is on, and our drive is currently
15711a6bed68SJoerg Wunsch 			 * selected, just read the hardware bit.
15721a6bed68SJoerg Wunsch 			 *
15731a6bed68SJoerg Wunsch 			 * If motor is on, but active for another
15741a6bed68SJoerg Wunsch 			 * drive on that controller, we are lost.  We
15751a6bed68SJoerg Wunsch 			 * cannot risk to deselect the other drive, so
15761a6bed68SJoerg Wunsch 			 * we just assume a forced UA condition to be
15771a6bed68SJoerg Wunsch 			 * on the safe side.
15781a6bed68SJoerg Wunsch 			 */
15791a6bed68SJoerg Wunsch 			unitattn = 0;
15801a6bed68SJoerg Wunsch 			if ((dflags & FD_NO_CHLINE) != 0 ||
15811a6bed68SJoerg Wunsch 			    (fd->flags & FD_UA) != 0) {
15821a6bed68SJoerg Wunsch 				unitattn = 1;
15831a6bed68SJoerg Wunsch 				fd->flags &= ~FD_UA;
15841a6bed68SJoerg Wunsch 			} else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 |
15851a6bed68SJoerg Wunsch 						 FDO_MOEN2 | FDO_MOEN3)) {
15861a6bed68SJoerg Wunsch 				if ((fdc->fdout & FDO_FDSEL) == fd->fdsu)
15871a6bed68SJoerg Wunsch 					unitattn = fdin_rd(fdc) & FDI_DCHG;
15881a6bed68SJoerg Wunsch 				else
15891a6bed68SJoerg Wunsch 					unitattn = 1;
15901a6bed68SJoerg Wunsch 			} else {
15911a6bed68SJoerg Wunsch 				set_motor(fdc, fd->fdsu, TURNON);
15921a6bed68SJoerg Wunsch 				unitattn = fdin_rd(fdc) & FDI_DCHG;
15931a6bed68SJoerg Wunsch 				set_motor(fdc, fd->fdsu, TURNOFF);
1594b39c878eSAndrey A. Chernov 			}
15951a6bed68SJoerg Wunsch 			if (unitattn && (rv = fdautoselect(dev)) != 0)
15961a6bed68SJoerg Wunsch 				return (rv);
15977ca0641bSAndrey A. Chernov 		}
15981a6bed68SJoerg Wunsch 	} else {
15991a6bed68SJoerg Wunsch 		fd->ft = fd->fts + type;
16007ca0641bSAndrey A. Chernov 	}
16016182fdbdSPeter Wemm 	fd->flags |= FD_OPEN;
16023fef646eSJoerg Wunsch 	/*
16033fef646eSJoerg Wunsch 	 * Clearing the DMA overrun counter at open time is a bit messy.
16043fef646eSJoerg Wunsch 	 * Since we're only managing one counter per controller, opening
16053fef646eSJoerg Wunsch 	 * the second drive could mess it up.  Anyway, if the DMA overrun
16063fef646eSJoerg Wunsch 	 * condition is really persistent, it will eventually time out
16073fef646eSJoerg Wunsch 	 * still.  OTOH, clearing it here will ensure we'll at least start
16083fef646eSJoerg Wunsch 	 * trying again after a previous (maybe even long ago) failure.
16093fef646eSJoerg Wunsch 	 * Also, this is merely a stop-gap measure only that should not
16103fef646eSJoerg Wunsch 	 * happen during normal operation, so we can tolerate it to be a
16113fef646eSJoerg Wunsch 	 * bit sloppy about this.
16123fef646eSJoerg Wunsch 	 */
16133fef646eSJoerg Wunsch 	fdc->dma_overruns = 0;
16145f830ea2SJoerg Wunsch 
16155b81b6b3SRodney W. Grimes 	return 0;
16165b81b6b3SRodney W. Grimes }
16175b81b6b3SRodney W. Grimes 
1618381fe1aaSGarrett Wollman int
1619b40ce416SJulian Elischer fdclose(dev_t dev, int flags, int mode, struct thread *td)
16205b81b6b3SRodney W. Grimes {
16215b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
16226182fdbdSPeter Wemm 	struct fd_data *fd;
1623b99f0a4aSAndrew Moore 
16246182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
16251a6bed68SJoerg Wunsch 	fd->flags &= ~(FD_OPEN | FD_NONBLOCK);
16262995d110SJoerg Wunsch 	fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1627dc16046fSJoerg Wunsch 
16285b81b6b3SRodney W. Grimes 	return (0);
16295b81b6b3SRodney W. Grimes }
16305b81b6b3SRodney W. Grimes 
16313a2f7427SDavid Greenman void
16328177437dSPoul-Henning Kamp fdstrategy(struct bio *bp)
16333a2f7427SDavid Greenman {
1634fb35bd37SJoerg Wunsch 	long blknum, nblocks;
16353a2f7427SDavid Greenman  	int	s;
16363a2f7427SDavid Greenman  	fdu_t	fdu;
16373a2f7427SDavid Greenman  	fdc_p	fdc;
16383a2f7427SDavid Greenman  	fd_p	fd;
16393a2f7427SDavid Greenman 	size_t	fdblk;
16403a2f7427SDavid Greenman 
16418177437dSPoul-Henning Kamp  	fdu = FDUNIT(minor(bp->bio_dev));
16426182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
16436182fdbdSPeter Wemm 	if (fd == 0)
16446182fdbdSPeter Wemm 		panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
16458177437dSPoul-Henning Kamp 		      (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev));
16463a2f7427SDavid Greenman 	fdc = fd->fdc;
16471a6bed68SJoerg Wunsch 	if (fd->type == FDT_NONE || fd->ft == 0) {
16488177437dSPoul-Henning Kamp 		bp->bio_error = ENXIO;
16498177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
16503b178206SWarner Losh 		goto bad;
165164860614SJoerg Wunsch 	}
1652d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
1653f664aeeeSJoerg Wunsch 	if (bp->bio_cmd != BIO_FORMAT && bp->bio_cmd != BIO_RDSECTID) {
16541a6bed68SJoerg Wunsch 		if (fd->flags & FD_NONBLOCK) {
16551a6bed68SJoerg Wunsch 			bp->bio_error = EAGAIN;
16561a6bed68SJoerg Wunsch 			bp->bio_flags |= BIO_ERROR;
16571a6bed68SJoerg Wunsch 			goto bad;
16581a6bed68SJoerg Wunsch 		}
16598177437dSPoul-Henning Kamp 		if (bp->bio_blkno < 0) {
1660dc5df763SJoerg Wunsch 			printf(
16616a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
16628177437dSPoul-Henning Kamp 			       fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
16638177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
16648177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
16653a2f7427SDavid Greenman 			goto bad;
16663a2f7427SDavid Greenman 		}
16678177437dSPoul-Henning Kamp 		if ((bp->bio_bcount % fdblk) != 0) {
16688177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
16698177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
16703a2f7427SDavid Greenman 			goto bad;
16713a2f7427SDavid Greenman 		}
16723a2f7427SDavid Greenman 	}
16733a2f7427SDavid Greenman 
16743a2f7427SDavid Greenman 	/*
16753a2f7427SDavid Greenman 	 * Set up block calculations.
16763a2f7427SDavid Greenman 	 */
16778177437dSPoul-Henning Kamp 	if (bp->bio_blkno > 20000000) {
1678bb6382faSJoerg Wunsch 		/*
1679bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1680bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1681bb6382faSJoerg Wunsch 		 */
16828177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
16838177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
16843a2f7427SDavid Greenman 		goto bad;
16853a2f7427SDavid Greenman 	}
1686fb35bd37SJoerg Wunsch 	blknum = bp->bio_blkno * DEV_BSIZE / fdblk;
1687bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1688fb35bd37SJoerg Wunsch 	if (blknum + bp->bio_bcount / fdblk > nblocks) {
1689fb35bd37SJoerg Wunsch 		if (blknum >= nblocks) {
1690fb35bd37SJoerg Wunsch 			if (bp->bio_cmd == BIO_READ)
1691fb35bd37SJoerg Wunsch 				bp->bio_resid = bp->bio_bcount;
1692fb35bd37SJoerg Wunsch 			else {
1693fb35bd37SJoerg Wunsch 				bp->bio_error = ENOSPC;
16948177437dSPoul-Henning Kamp 				bp->bio_flags |= BIO_ERROR;
1695bb6382faSJoerg Wunsch 			}
1696fb35bd37SJoerg Wunsch 			goto bad;	/* not always bad, but EOF */
1697fb35bd37SJoerg Wunsch 		}
1698fb35bd37SJoerg Wunsch 		bp->bio_bcount = (nblocks - blknum) * fdblk;
1699bb6382faSJoerg Wunsch 	}
17008177437dSPoul-Henning Kamp  	bp->bio_pblkno = bp->bio_blkno;
17013a2f7427SDavid Greenman 	s = splbio();
17028177437dSPoul-Henning Kamp 	bioqdisksort(&fdc->head, bp);
17036182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1704b2dfb1f9SJustin T. Gibbs 	devstat_start_transaction(&fd->device_stats);
17055f830ea2SJoerg Wunsch 	device_busy(fd->dev);
17066182fdbdSPeter Wemm 	fdstart(fdc);
17073a2f7427SDavid Greenman 	splx(s);
17083a2f7427SDavid Greenman 	return;
17093a2f7427SDavid Greenman 
17103a2f7427SDavid Greenman bad:
17113a2f7427SDavid Greenman 	biodone(bp);
17123a2f7427SDavid Greenman }
17133a2f7427SDavid Greenman 
1714246ed35dSJoerg Wunsch /*
1715246ed35dSJoerg Wunsch  * fdstart
1716246ed35dSJoerg Wunsch  *
1717246ed35dSJoerg Wunsch  * We have just queued something.  If the controller is not busy
1718246ed35dSJoerg Wunsch  * then simulate the case where it has just finished a command
1719246ed35dSJoerg Wunsch  * So that it (the interrupt routine) looks on the queue for more
1720246ed35dSJoerg Wunsch  * work to do and picks up what we just added.
1721246ed35dSJoerg Wunsch  *
1722246ed35dSJoerg Wunsch  * If the controller is already busy, we need do nothing, as it
1723246ed35dSJoerg Wunsch  * will pick up our work when the present work completes.
1724246ed35dSJoerg Wunsch  */
1725381fe1aaSGarrett Wollman static void
17266182fdbdSPeter Wemm fdstart(struct fdc_data *fdc)
17275b81b6b3SRodney W. Grimes {
17285b81b6b3SRodney W. Grimes 	int s;
17295b81b6b3SRodney W. Grimes 
17305b81b6b3SRodney W. Grimes 	s = splbio();
17316182fdbdSPeter Wemm 	if(fdc->state == DEVIDLE)
17325b81b6b3SRodney W. Grimes 	{
17336182fdbdSPeter Wemm 		fdc_intr(fdc);
17345b81b6b3SRodney W. Grimes 	}
17355b81b6b3SRodney W. Grimes 	splx(s);
17365b81b6b3SRodney W. Grimes }
17375b81b6b3SRodney W. Grimes 
1738381fe1aaSGarrett Wollman static void
17396182fdbdSPeter Wemm fd_iotimeout(void *xfdc)
17405b81b6b3SRodney W. Grimes {
17415c1a1eaeSBruce Evans  	fdc_p fdc;
1742f5f7ba03SJordan K. Hubbard 	int s;
17435b81b6b3SRodney W. Grimes 
17446182fdbdSPeter Wemm 	fdc = xfdc;
17455c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
17465b81b6b3SRodney W. Grimes 
17473a2f7427SDavid Greenman 	/*
17483a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
17493a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
17503a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
17513a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
17523a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
17535c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
17545c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
17555c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
17565c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
17573a2f7427SDavid Greenman 	 */
17583a2f7427SDavid Greenman 	s = splbio();
17595c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
17605c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
17615c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
17626182fdbdSPeter Wemm 	fdc_intr(fdc);
1763f5f7ba03SJordan K. Hubbard 	splx(s);
17645b81b6b3SRodney W. Grimes }
17655b81b6b3SRodney W. Grimes 
1766246ed35dSJoerg Wunsch /* Just ensure it has the right spl. */
1767381fe1aaSGarrett Wollman static void
17686182fdbdSPeter Wemm fd_pseudointr(void *xfdc)
17695b81b6b3SRodney W. Grimes {
17705b81b6b3SRodney W. Grimes 	int	s;
17713a2f7427SDavid Greenman 
17725b81b6b3SRodney W. Grimes 	s = splbio();
17736182fdbdSPeter Wemm 	fdc_intr(xfdc);
17745b81b6b3SRodney W. Grimes 	splx(s);
17755b81b6b3SRodney W. Grimes }
17765b81b6b3SRodney W. Grimes 
1777246ed35dSJoerg Wunsch /*
1778246ed35dSJoerg Wunsch  * fdc_intr
1779246ed35dSJoerg Wunsch  *
1780246ed35dSJoerg Wunsch  * Keep calling the state machine until it returns a 0.
1781246ed35dSJoerg Wunsch  * Always called at splbio.
1782246ed35dSJoerg Wunsch  */
1783fe310de8SBruce Evans static void
17846182fdbdSPeter Wemm fdc_intr(void *xfdc)
17855b81b6b3SRodney W. Grimes {
17866182fdbdSPeter Wemm 	fdc_p fdc = xfdc;
17876182fdbdSPeter Wemm 	while(fdstate(fdc))
1788381fe1aaSGarrett Wollman 		;
17895b81b6b3SRodney W. Grimes }
17905b81b6b3SRodney W. Grimes 
179169acd21dSWarner Losh /*
1792246ed35dSJoerg Wunsch  * Magic pseudo-DMA initialization for YE FDC. Sets count and
1793246ed35dSJoerg Wunsch  * direction.
179469acd21dSWarner Losh  */
17953b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \
17963b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port,	 \
17973b178206SWarner Losh 	    ((cnt)-1) & 0xff);						 \
17983b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
17993b178206SWarner Losh 	    ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
180069acd21dSWarner Losh 
180169acd21dSWarner Losh /*
1802246ed35dSJoerg Wunsch  * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy.
180369acd21dSWarner Losh  */
18044c34deeeSJoerg Wunsch static int
18054c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
180669acd21dSWarner Losh {
180769acd21dSWarner Losh 	u_char *cptr = (u_char *)addr;
180869acd21dSWarner Losh 
180921144e3bSPoul-Henning Kamp 	if (flags == BIO_READ) {
181069acd21dSWarner Losh 		if (fdc->state != PIOREAD) {
181169acd21dSWarner Losh 			fdc->state = PIOREAD;
181269acd21dSWarner Losh 			return(0);
181364860614SJoerg Wunsch 		}
18143b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
18153b178206SWarner Losh 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
18163b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
181769acd21dSWarner Losh 	} else {
18183b178206SWarner Losh 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
18193b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
18203b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
182164860614SJoerg Wunsch 	}
182269acd21dSWarner Losh 	return(1);
182369acd21dSWarner Losh }
182469acd21dSWarner Losh 
1825246ed35dSJoerg Wunsch /*
18261a6bed68SJoerg Wunsch  * Try figuring out the density of the media present in our device.
18271a6bed68SJoerg Wunsch  */
18281a6bed68SJoerg Wunsch static int
18291a6bed68SJoerg Wunsch fdautoselect(dev_t dev)
18301a6bed68SJoerg Wunsch {
18311a6bed68SJoerg Wunsch 	fdu_t fdu;
18321a6bed68SJoerg Wunsch  	fd_p fd;
18331a6bed68SJoerg Wunsch 	struct fd_type *fdtp;
18341a6bed68SJoerg Wunsch 	struct fdc_readid id;
18351a6bed68SJoerg Wunsch 	int i, n, oopts, rv;
18361a6bed68SJoerg Wunsch 
18371a6bed68SJoerg Wunsch  	fdu = FDUNIT(minor(dev));
18381a6bed68SJoerg Wunsch 	fd = devclass_get_softc(fd_devclass, fdu);
18391a6bed68SJoerg Wunsch 
18401a6bed68SJoerg Wunsch 	switch (fd->type) {
18411a6bed68SJoerg Wunsch 	default:
18421a6bed68SJoerg Wunsch 		return (ENXIO);
18431a6bed68SJoerg Wunsch 
18441a6bed68SJoerg Wunsch 	case FDT_360K:
18451a6bed68SJoerg Wunsch 	case FDT_720K:
18461a6bed68SJoerg Wunsch 		/* no autoselection on those drives */
18471a6bed68SJoerg Wunsch 		fd->ft = fd_native_types + fd->type;
18481a6bed68SJoerg Wunsch 		return (0);
18491a6bed68SJoerg Wunsch 
18501a6bed68SJoerg Wunsch 	case FDT_12M:
18511a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_12m;
18521a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_12m / sizeof(struct fd_type);
18531a6bed68SJoerg Wunsch 		break;
18541a6bed68SJoerg Wunsch 
18551a6bed68SJoerg Wunsch 	case FDT_144M:
18561a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_144m;
18571a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_144m / sizeof(struct fd_type);
18581a6bed68SJoerg Wunsch 		break;
18591a6bed68SJoerg Wunsch 
18601a6bed68SJoerg Wunsch 	case FDT_288M:
18611a6bed68SJoerg Wunsch 		fdtp = fd_searchlist_288m;
18621a6bed68SJoerg Wunsch 		n = sizeof fd_searchlist_288m / sizeof(struct fd_type);
18631a6bed68SJoerg Wunsch 		break;
18641a6bed68SJoerg Wunsch 	}
18651a6bed68SJoerg Wunsch 
18661a6bed68SJoerg Wunsch 	/*
18671a6bed68SJoerg Wunsch 	 * Try reading sector ID fields, first at cylinder 0, head 0,
18681a6bed68SJoerg Wunsch 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
18691a6bed68SJoerg Wunsch 	 * since for 5.25in DD media in a HD drive, there are no data
18701a6bed68SJoerg Wunsch 	 * to read (2 step pulses per media cylinder required).  For
18711a6bed68SJoerg Wunsch 	 * two-sided media, the second probe always goes to head 1, so
18721a6bed68SJoerg Wunsch 	 * we can tell them apart from single-sided media.  As a
18731a6bed68SJoerg Wunsch 	 * side-effect this means that single-sided media should be
18741a6bed68SJoerg Wunsch 	 * mentioned in the search list after two-sided media of an
18751a6bed68SJoerg Wunsch 	 * otherwise identical density.  Media with a different number
18761a6bed68SJoerg Wunsch 	 * of sectors per track but otherwise identical parameters
18771a6bed68SJoerg Wunsch 	 * cannot be distinguished at all.
18781a6bed68SJoerg Wunsch 	 *
18791a6bed68SJoerg Wunsch 	 * If we successfully read an ID field on both cylinders where
18801a6bed68SJoerg Wunsch 	 * the recorded values match our expectation, we are done.
18811a6bed68SJoerg Wunsch 	 * Otherwise, we try the next density entry from the table.
18821a6bed68SJoerg Wunsch 	 *
18831a6bed68SJoerg Wunsch 	 * Stepping to cylinder 2 has the side-effect of clearing the
18841a6bed68SJoerg Wunsch 	 * unit attention bit.
18851a6bed68SJoerg Wunsch 	 */
18861a6bed68SJoerg Wunsch 	oopts = fd->options;
18871a6bed68SJoerg Wunsch 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
18881a6bed68SJoerg Wunsch 	for (i = 0; i < n; i++, fdtp++) {
18891a6bed68SJoerg Wunsch 		fd->ft = fdtp;
18901a6bed68SJoerg Wunsch 
18911a6bed68SJoerg Wunsch 		id.cyl = id.head = 0;
18921a6bed68SJoerg Wunsch 		rv = fdmisccmd(dev, BIO_RDSECTID, &id);
18931a6bed68SJoerg Wunsch 		if (rv != 0)
18941a6bed68SJoerg Wunsch 			continue;
18951a6bed68SJoerg Wunsch 		if (id.cyl != 0 || id.head != 0 ||
18961a6bed68SJoerg Wunsch 		    id.secshift != fdtp->secsize)
18971a6bed68SJoerg Wunsch 			continue;
18981a6bed68SJoerg Wunsch 		id.cyl = 2;
18991a6bed68SJoerg Wunsch 		id.head = fd->ft->heads - 1;
19001a6bed68SJoerg Wunsch 		rv = fdmisccmd(dev, BIO_RDSECTID, &id);
19011a6bed68SJoerg Wunsch 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
19021a6bed68SJoerg Wunsch 		    id.secshift != fdtp->secsize)
19031a6bed68SJoerg Wunsch 			continue;
19041a6bed68SJoerg Wunsch 		if (rv == 0)
19051a6bed68SJoerg Wunsch 			break;
19061a6bed68SJoerg Wunsch 	}
19071a6bed68SJoerg Wunsch 
19081a6bed68SJoerg Wunsch 	fd->options = oopts;
19091a6bed68SJoerg Wunsch 	if (i == n) {
19101a6bed68SJoerg Wunsch 		device_printf(fd->dev, "autoselection failed\n");
19111a6bed68SJoerg Wunsch 		fd->ft = 0;
19121a6bed68SJoerg Wunsch 		return (EIO);
19131a6bed68SJoerg Wunsch 	} else {
19141a6bed68SJoerg Wunsch 		device_printf(fd->dev, "autoselected %d KB medium\n",
19151a6bed68SJoerg Wunsch 			      fd->ft->size / 2);
19161a6bed68SJoerg Wunsch 		return (0);
19171a6bed68SJoerg Wunsch 	}
19181a6bed68SJoerg Wunsch }
19191a6bed68SJoerg Wunsch 
19201a6bed68SJoerg Wunsch 
19211a6bed68SJoerg Wunsch /*
1922246ed35dSJoerg Wunsch  * The controller state machine.
1923246ed35dSJoerg Wunsch  *
1924246ed35dSJoerg Wunsch  * If it returns a non zero value, it should be called again immediately.
1925246ed35dSJoerg Wunsch  */
19263a2f7427SDavid Greenman static int
19276182fdbdSPeter Wemm fdstate(fdc_p fdc)
19285b81b6b3SRodney W. Grimes {
192964860614SJoerg Wunsch 	struct fdc_readid *idp;
1930fb35bd37SJoerg Wunsch 	int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
19311a6bed68SJoerg Wunsch 	int st0, cyl, st3, idf, ne7cmd, mfm, steptrac;
1932fb35bd37SJoerg Wunsch 	unsigned long blknum;
19335b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
19345b81b6b3SRodney W. Grimes 	fd_p fd;
19358177437dSPoul-Henning Kamp 	register struct bio *bp;
1936b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
19373a2f7427SDavid Greenman 	size_t fdblk;
19385b81b6b3SRodney W. Grimes 
1939e93e63cbSBruce Evans 	bp = fdc->bp;
1940e93e63cbSBruce Evans 	if (bp == NULL) {
19418177437dSPoul-Henning Kamp 		bp = bioq_first(&fdc->head);
1942e93e63cbSBruce Evans 		if (bp != NULL) {
19438177437dSPoul-Henning Kamp 			bioq_remove(&fdc->head, bp);
1944e93e63cbSBruce Evans 			fdc->bp = bp;
1945e93e63cbSBruce Evans 		}
1946e93e63cbSBruce Evans 	}
1947e93e63cbSBruce Evans 	if (bp == NULL) {
1948246ed35dSJoerg Wunsch 		/*
1949246ed35dSJoerg Wunsch 		 * Nothing left for this controller to do,
1950246ed35dSJoerg Wunsch 		 * force into the IDLE state.
1951246ed35dSJoerg Wunsch 		 */
19525b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
19536182fdbdSPeter Wemm 		if (fdc->fd) {
1954b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev,
1955b6e5f28eSPeter Wemm 			    "unexpected valid fd pointer\n");
19565b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
19575b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
19585b81b6b3SRodney W. Grimes 		}
19596182fdbdSPeter Wemm 		TRACE1("[fdc%d IDLE]", fdc->fdcu);
19605b81b6b3SRodney W. Grimes  		return (0);
19615b81b6b3SRodney W. Grimes 	}
19628177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
19636182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
19643a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1965b6e5f28eSPeter Wemm 	if (fdc->fd && (fd != fdc->fd))
1966b6e5f28eSPeter Wemm 		device_printf(fd->dev, "confused fd pointers\n");
19678177437dSPoul-Henning Kamp 	read = bp->bio_cmd == BIO_READ;
19681a6bed68SJoerg Wunsch 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
19691a6bed68SJoerg Wunsch 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
197056a23089SPoul-Henning Kamp 	if (read)
197156a23089SPoul-Henning Kamp 		idf = ISADMA_READ;
197256a23089SPoul-Henning Kamp 	else
197356a23089SPoul-Henning Kamp 		idf = ISADMA_WRITE;
197464860614SJoerg Wunsch 	format = bp->bio_cmd == BIO_FORMAT;
1975f664aeeeSJoerg Wunsch 	rdsectid = bp->bio_cmd == BIO_RDSECTID;
1976fb35bd37SJoerg Wunsch 	if (format)
19778177437dSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
19785b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
19795b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
19805b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
19816182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle);
19826182fdbdSPeter Wemm 	fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
19835b81b6b3SRodney W. Grimes 	switch (fdc->state)
19845b81b6b3SRodney W. Grimes 	{
19855b81b6b3SRodney W. Grimes 	case DEVIDLE:
19865b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
19875b81b6b3SRodney W. Grimes 		fdc->retry = 0;
19885b81b6b3SRodney W. Grimes 		fd->skip = 0;
19895b81b6b3SRodney W. Grimes 		fdc->fd = fd;
19905b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
19915f830ea2SJoerg Wunsch 		fdc->fdctl_wr(fdc, fd->ft->trans);
19923a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1993246ed35dSJoerg Wunsch 		/*
1994246ed35dSJoerg Wunsch 		 * If the next drive has a motor startup pending, then
1995246ed35dSJoerg Wunsch 		 * it will start up in its own good time.
1996246ed35dSJoerg Wunsch 		 */
19976182fdbdSPeter Wemm 		if(fd->flags & FD_MOTOR_WAIT) {
19985b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
1999246ed35dSJoerg Wunsch 			return (0); /* will return later */
20005b81b6b3SRodney W. Grimes 		}
2001246ed35dSJoerg Wunsch 		/*
2002246ed35dSJoerg Wunsch 		 * Maybe if it's not starting, it SHOULD be starting.
2003246ed35dSJoerg Wunsch 		 */
20045b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
20055b81b6b3SRodney W. Grimes 		{
20065b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
20076182fdbdSPeter Wemm 			fd_turnon(fd);
2008246ed35dSJoerg Wunsch 			return (0); /* will return later */
20095b81b6b3SRodney W. Grimes 		}
20105b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
20115b81b6b3SRodney W. Grimes 		{
20126182fdbdSPeter Wemm 			set_motor(fdc, fd->fdsu, TURNON);
20135b81b6b3SRodney W. Grimes 		}
20145c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
20155c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
20165c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
20175c1a1eaeSBruce Evans 		} else
20185e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
2019246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2020fb35bd37SJoerg Wunsch 
20215b81b6b3SRodney W. Grimes 	case DOSEEK:
2022fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
2023fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2024fb35bd37SJoerg Wunsch 		if (cylinder == fd->track)
20255b81b6b3SRodney W. Grimes 		{
20265b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
2027246ed35dSJoerg Wunsch 			return (1); /* will return immediately */
20285b81b6b3SRodney W. Grimes 		}
20296182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK,
20301a6bed68SJoerg Wunsch 			   fd->fdsu, cylinder * steptrac, 0))
2031dc8603e3SJoerg Wunsch 		{
2032dc8603e3SJoerg Wunsch 			/*
2033246ed35dSJoerg Wunsch 			 * Seek command not accepted, looks like
2034dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
2035dc8603e3SJoerg Wunsch 			 */
2036dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
20376182fdbdSPeter Wemm 			return(retrier(fdc));
2038dc8603e3SJoerg Wunsch 		}
2039dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
20405b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
20415b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
2042fb35bd37SJoerg Wunsch 
20435b81b6b3SRodney W. Grimes 	case SEEKWAIT:
20445b81b6b3SRodney W. Grimes 		/* allow heads to settle */
20456182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 16);
20465b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
20475b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
2048fb35bd37SJoerg Wunsch 
2049246ed35dSJoerg Wunsch 	case SEEKCOMPLETE : /* seek done, start DMA */
2050fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
2051fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
2052fb35bd37SJoerg Wunsch 
2053246ed35dSJoerg Wunsch 		/* Make sure seek really happened. */
20546182fdbdSPeter Wemm 		if(fd->track == FD_NO_TRACK) {
20551a6bed68SJoerg Wunsch 			int descyl = cylinder * steptrac;
20563a2f7427SDavid Greenman 			do {
20573a2f7427SDavid Greenman 				/*
2058dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
2059dc5df763SJoerg Wunsch 				 * which cannot really happen since the
2060dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
2061dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
2062dc5df763SJoerg Wunsch 				 * line, so do one of the following:
2063dc5df763SJoerg Wunsch 				 *
2064dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
2065dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
2066dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
2067dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
2068dc5df763SJoerg Wunsch 				 * real interrupt condition.
2069dc5df763SJoerg Wunsch 				 *
2070dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
2071dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
2072dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
2073dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
2074dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
2075dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
2076dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
20773a2f7427SDavid Greenman 				 */
2078dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
2079dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
2080246ed35dSJoerg Wunsch 					return (0); /* will return later */
2081dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
2082dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2083246ed35dSJoerg Wunsch 					return (0); /* hope for a real intr */
20843a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2085dc5df763SJoerg Wunsch 
20866182fdbdSPeter Wemm 			if (0 == descyl) {
2087dc5df763SJoerg Wunsch 				int failed = 0;
20883a2f7427SDavid Greenman 				/*
20893a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
20903a2f7427SDavid Greenman 				 * really there
20913a2f7427SDavid Greenman 				 */
2092dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
2093dc5df763SJoerg Wunsch 					failed = 1;
20943a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
20953a2f7427SDavid Greenman 					printf(
20963a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
20973a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
2098dc5df763SJoerg Wunsch 					failed = 1;
2099dc5df763SJoerg Wunsch 				}
2100dc5df763SJoerg Wunsch 
21016182fdbdSPeter Wemm 				if (failed) {
21023a2f7427SDavid Greenman 					if(fdc->retry < 3)
21033a2f7427SDavid Greenman 						fdc->retry = 3;
21046182fdbdSPeter Wemm 					return (retrier(fdc));
21053a2f7427SDavid Greenman 				}
21063a2f7427SDavid Greenman 			}
2107dc5df763SJoerg Wunsch 
21086182fdbdSPeter Wemm 			if (cyl != descyl) {
21093a2f7427SDavid Greenman 				printf(
21103a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
21112d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
2112e5d7d243SBruce Evans 				if (fdc->retry < 3)
2113e5d7d243SBruce Evans 					fdc->retry = 3;
21146182fdbdSPeter Wemm 				return (retrier(fdc));
21155b81b6b3SRodney W. Grimes 			}
21165b81b6b3SRodney W. Grimes 		}
21175b81b6b3SRodney W. Grimes 
2118fb35bd37SJoerg Wunsch 		fd->track = cylinder;
2119fb35bd37SJoerg Wunsch 		if (format)
2120fb35bd37SJoerg Wunsch 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
2121fb35bd37SJoerg Wunsch 			    - (char *)finfo;
2122250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
21238177437dSPoul-Henning Kamp 			isa_dmastart(idf, bp->bio_data+fd->skip,
21248177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
2125fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
21265b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
21275b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
21285b81b6b3SRodney W. Grimes 		head = sec / sectrac;
21295b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
21301a6bed68SJoerg Wunsch 		if (head != 0 && fd->ft->offset_side2 != 0)
21311a6bed68SJoerg Wunsch 			sec += fd->ft->offset_side2;
21323a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
21333a2f7427SDavid Greenman 
2134250300ebSJoerg Wunsch 		if(format || !(read || rdsectid))
21353a2f7427SDavid Greenman 		{
21363a2f7427SDavid Greenman 			/* make sure the drive is writable */
2137dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
2138dc8603e3SJoerg Wunsch 			{
2139dc8603e3SJoerg Wunsch 				/* stuck controller? */
21405f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
214156a23089SPoul-Henning Kamp 					isa_dmadone(idf,
21428177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
21438177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
21445c1a1eaeSBruce Evans 						    fdc->dmachan);
2145dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
21466182fdbdSPeter Wemm 				return (retrier(fdc));
2147dc8603e3SJoerg Wunsch 			}
21483a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
21493a2f7427SDavid Greenman 			{
21503a2f7427SDavid Greenman 				/*
21513a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
21523a2f7427SDavid Greenman 				 * in order to force the current operation
21533a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
21543a2f7427SDavid Greenman 				 * error - all error handling is done
21553a2f7427SDavid Greenman 				 * by the retrier()
21563a2f7427SDavid Greenman 				 */
21573a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
21583a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
21593a2f7427SDavid Greenman 				fdc->status[2] = 0;
21603a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
21613a2f7427SDavid Greenman 				fdc->status[4] = head;
21623a2f7427SDavid Greenman 				fdc->status[5] = sec;
21633a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
21643a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
2165246ed35dSJoerg Wunsch 				return (1); /* will return immediately */
21663a2f7427SDavid Greenman 			}
21673a2f7427SDavid Greenman 		}
21685b81b6b3SRodney W. Grimes 
21696182fdbdSPeter Wemm 		if (format) {
21701a6bed68SJoerg Wunsch 			ne7cmd = NE7CMD_FORMAT | mfm;
21715f830ea2SJoerg Wunsch 			if (fdc->flags & FDC_NODMA) {
21725f830ea2SJoerg Wunsch 				/*
21735f830ea2SJoerg Wunsch 				 * This seems to be necessary for
21745f830ea2SJoerg Wunsch 				 * whatever obscure reason; if we omit
21755f830ea2SJoerg Wunsch 				 * it, we end up filling the sector ID
21765f830ea2SJoerg Wunsch 				 * fields of the newly formatted track
21775f830ea2SJoerg Wunsch 				 * entirely with garbage, causing
21785f830ea2SJoerg Wunsch 				 * `wrong cylinder' errors all over
21795f830ea2SJoerg Wunsch 				 * the place when trying to read them
21805f830ea2SJoerg Wunsch 				 * back.
21815f830ea2SJoerg Wunsch 				 *
21825f830ea2SJoerg Wunsch 				 * Umpf.
21835f830ea2SJoerg Wunsch 				 */
21848177437dSPoul-Henning Kamp 				SET_BCDR(fdc, 1, bp->bio_bcount, 0);
21855f830ea2SJoerg Wunsch 
21868177437dSPoul-Henning Kamp 				(void)fdcpio(fdc,bp->bio_cmd,
21878177437dSPoul-Henning Kamp 					bp->bio_data+fd->skip,
21888177437dSPoul-Henning Kamp 					bp->bio_bcount);
21895f830ea2SJoerg Wunsch 
21905f830ea2SJoerg Wunsch 			}
2191b39c878eSAndrey A. Chernov 			/* formatting */
21921a6bed68SJoerg Wunsch 			if(fd_cmd(fdc, 6,  ne7cmd, head << 2 | fdu,
2193dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
2194dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
2195dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
21966182fdbdSPeter Wemm 				  finfo->fd_formb_fillbyte, 0)) {
2197dc8603e3SJoerg Wunsch 				/* controller fell over */
21985f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
219956a23089SPoul-Henning Kamp 					isa_dmadone(idf,
22008177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
22018177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
22025c1a1eaeSBruce Evans 						    fdc->dmachan);
2203dc8603e3SJoerg Wunsch 				fdc->retry = 6;
22046182fdbdSPeter Wemm 				return (retrier(fdc));
2205dc8603e3SJoerg Wunsch 			}
2206250300ebSJoerg Wunsch 		} else if (rdsectid) {
22071a6bed68SJoerg Wunsch 			ne7cmd = NE7CMD_READID | mfm;
22081a6bed68SJoerg Wunsch 			if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) {
2209250300ebSJoerg Wunsch 				/* controller jamming */
2210250300ebSJoerg Wunsch 				fdc->retry = 6;
2211250300ebSJoerg Wunsch 				return (retrier(fdc));
2212250300ebSJoerg Wunsch 			}
22136182fdbdSPeter Wemm 		} else {
2214250300ebSJoerg Wunsch 			/* read or write operation */
22151a6bed68SJoerg Wunsch 			ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm;
22163b178206SWarner Losh 			if (fdc->flags & FDC_NODMA) {
221769acd21dSWarner Losh 				/*
2218246ed35dSJoerg Wunsch 				 * This seems to be necessary even when
2219246ed35dSJoerg Wunsch 				 * reading data.
222069acd21dSWarner Losh 				 */
22213b178206SWarner Losh 				SET_BCDR(fdc, 1, fdblk, 0);
222269acd21dSWarner Losh 
222369acd21dSWarner Losh 				/*
2224246ed35dSJoerg Wunsch 				 * Perform the write pseudo-DMA before
2225246ed35dSJoerg Wunsch 				 * the WRITE command is sent.
222669acd21dSWarner Losh 				 */
222769acd21dSWarner Losh 				if (!read)
22288177437dSPoul-Henning Kamp 					(void)fdcpio(fdc,bp->bio_cmd,
22298177437dSPoul-Henning Kamp 					    bp->bio_data+fd->skip,
223069acd21dSWarner Losh 					    fdblk);
223169acd21dSWarner Losh 			}
22326182fdbdSPeter Wemm 			if (fd_cmd(fdc, 9,
22331a6bed68SJoerg Wunsch 				   ne7cmd,
2234dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
2235dc5df763SJoerg Wunsch 				   fd->track,        /* track */
2236dc5df763SJoerg Wunsch 				   head,
2237dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
2238dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
2239dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
2240dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
2241dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
22426182fdbdSPeter Wemm 				   0)) {
2243dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
22445f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
224556a23089SPoul-Henning Kamp 					isa_dmadone(idf,
22468177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
22478177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
22485c1a1eaeSBruce Evans 						    fdc->dmachan);
2249dc8603e3SJoerg Wunsch 				fdc->retry = 6;
22506182fdbdSPeter Wemm 				return (retrier(fdc));
22515b81b6b3SRodney W. Grimes 			}
2252b39c878eSAndrey A. Chernov 		}
2253250300ebSJoerg Wunsch 		if (!rdsectid && (fdc->flags & FDC_NODMA))
225469acd21dSWarner Losh 			/*
2255246ed35dSJoerg Wunsch 			 * If this is a read, then simply await interrupt
2256246ed35dSJoerg Wunsch 			 * before performing PIO.
225769acd21dSWarner Losh 			 */
22588177437dSPoul-Henning Kamp 			if (read && !fdcpio(fdc,bp->bio_cmd,
22598177437dSPoul-Henning Kamp 			    bp->bio_data+fd->skip,fdblk)) {
22603b178206SWarner Losh 				fd->tohandle = timeout(fd_iotimeout, fdc, hz);
226169acd21dSWarner Losh 				return(0);      /* will return later */
226264860614SJoerg Wunsch 			}
226369acd21dSWarner Losh 
226469acd21dSWarner Losh 		/*
2265246ed35dSJoerg Wunsch 		 * Write (or format) operation will fall through and
2266246ed35dSJoerg Wunsch 		 * await completion interrupt.
226769acd21dSWarner Losh 		 */
22685b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
22696182fdbdSPeter Wemm 		fd->tohandle = timeout(fd_iotimeout, fdc, hz);
22705b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2271fb35bd37SJoerg Wunsch 
227269acd21dSWarner Losh 	case PIOREAD:
227369acd21dSWarner Losh 		/*
2274246ed35dSJoerg Wunsch 		 * Actually perform the PIO read.  The IOCOMPLETE case
227569acd21dSWarner Losh 		 * removes the timeout for us.
227669acd21dSWarner Losh 		 */
22778177437dSPoul-Henning Kamp 		(void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
227869acd21dSWarner Losh 		fdc->state = IOCOMPLETE;
227969acd21dSWarner Losh 		/* FALLTHROUGH */
2280246ed35dSJoerg Wunsch 	case IOCOMPLETE: /* IO done, post-analyze */
22816182fdbdSPeter Wemm 		untimeout(fd_iotimeout, fdc, fd->tohandle);
2282dc5df763SJoerg Wunsch 
228364860614SJoerg Wunsch 		if (fd_read_status(fdc)) {
2284250300ebSJoerg Wunsch 			if (!rdsectid && !(fdc->flags & FDC_NODMA))
22858177437dSPoul-Henning Kamp 				isa_dmadone(idf, bp->bio_data + fd->skip,
22868177437dSPoul-Henning Kamp 					    format ? bp->bio_bcount : fdblk,
22875c1a1eaeSBruce Evans 					    fdc->dmachan);
2288dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
2289dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
22906182fdbdSPeter Wemm 			return (retrier(fdc));
22915b81b6b3SRodney W. Grimes   		}
2292dc5df763SJoerg Wunsch 
22933a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
2294dc5df763SJoerg Wunsch 
22953a2f7427SDavid Greenman 		/* FALLTHROUGH */
22963a2f7427SDavid Greenman 	case IOTIMEDOUT:
2297250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
22988177437dSPoul-Henning Kamp 			isa_dmadone(idf, bp->bio_data + fd->skip,
22998177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
23006182fdbdSPeter Wemm 		if (fdc->status[0] & NE7_ST0_IC) {
23013a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
23023a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
2303b39c878eSAndrey A. Chernov                                 /*
23043fef646eSJoerg Wunsch 				 * DMA overrun. Someone hogged the bus and
23053fef646eSJoerg Wunsch 				 * didn't release it in time for the next
23063fef646eSJoerg Wunsch 				 * FDC transfer.
23073fef646eSJoerg Wunsch 				 *
23083fef646eSJoerg Wunsch 				 * We normally restart this without bumping
23093fef646eSJoerg Wunsch 				 * the retry counter.  However, in case
23103fef646eSJoerg Wunsch 				 * something is seriously messed up (like
23113fef646eSJoerg Wunsch 				 * broken hardware), we rather limit the
23123fef646eSJoerg Wunsch 				 * number of retries so the IO operation
23133fef646eSJoerg Wunsch 				 * doesn't block indefinately.
2314b39c878eSAndrey A. Chernov 				 */
23153fef646eSJoerg Wunsch 				if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2316b39c878eSAndrey A. Chernov 					fdc->state = SEEKCOMPLETE;
2317246ed35dSJoerg Wunsch 					return (1);/* will return immediately */
23183fef646eSJoerg Wunsch 				} /* else fall through */
2319b39c878eSAndrey A. Chernov                         }
23203fef646eSJoerg Wunsch 			if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
23213a2f7427SDavid Greenman 				&& fdc->retry < 6)
23223a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
23233a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
23243a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
23253a2f7427SDavid Greenman 				&& fdc->retry < 3)
23263a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
23276182fdbdSPeter Wemm 			return (retrier(fdc));
23285b81b6b3SRodney W. Grimes 		}
23295b81b6b3SRodney W. Grimes 		/* All OK */
2330250300ebSJoerg Wunsch 		if (rdsectid) {
2331250300ebSJoerg Wunsch 			/* copy out ID field contents */
233264860614SJoerg Wunsch 			idp = (struct fdc_readid *)bp->bio_data;
2333250300ebSJoerg Wunsch 			idp->cyl = fdc->status[3];
2334250300ebSJoerg Wunsch 			idp->head = fdc->status[4];
2335250300ebSJoerg Wunsch 			idp->sec = fdc->status[5];
2336250300ebSJoerg Wunsch 			idp->secshift = fdc->status[6];
2337250300ebSJoerg Wunsch 		}
23383fef646eSJoerg Wunsch 		/* Operation successful, retry DMA overruns again next time. */
23393fef646eSJoerg Wunsch 		fdc->dma_overruns = 0;
23403a2f7427SDavid Greenman 		fd->skip += fdblk;
2341fb35bd37SJoerg Wunsch 		if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
23425b81b6b3SRodney W. Grimes 			/* set up next transfer */
23435b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
23446182fdbdSPeter Wemm 		} else {
23455b81b6b3SRodney W. Grimes 			/* ALL DONE */
23465b81b6b3SRodney W. Grimes 			fd->skip = 0;
2347fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2348e93e63cbSBruce Evans 			fdc->bp = NULL;
23495f830ea2SJoerg Wunsch 			device_unbusy(fd->dev);
2350a468031cSPoul-Henning Kamp 			biofinish(bp, &fd->device_stats, 0);
23515b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
23525b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
23535b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
23545b81b6b3SRodney W. Grimes 		}
2355246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2356fb35bd37SJoerg Wunsch 
23575b81b6b3SRodney W. Grimes 	case RESETCTLR:
23583a2f7427SDavid Greenman 		fdc_reset(fdc);
23595b81b6b3SRodney W. Grimes 		fdc->retry++;
23605c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
2361246ed35dSJoerg Wunsch 		return (0);	/* will return later */
2362fb35bd37SJoerg Wunsch 
23635c1a1eaeSBruce Evans 	case RESETCOMPLETE:
23645c1a1eaeSBruce Evans 		/*
23655c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
23665c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
23675c1a1eaeSBruce Evans 		 */
23680e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
23690e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
23705c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
2371fb35bd37SJoerg Wunsch 		/* FALLTHROUGH */
23725c1a1eaeSBruce Evans 	case STARTRECAL:
23736182fdbdSPeter Wemm 		if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2374dc8603e3SJoerg Wunsch 			/* arrgl */
2375dc8603e3SJoerg Wunsch 			fdc->retry = 6;
23766182fdbdSPeter Wemm 			return (retrier(fdc));
2377dc8603e3SJoerg Wunsch 		}
23785b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
23795b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2380fb35bd37SJoerg Wunsch 
23815b81b6b3SRodney W. Grimes 	case RECALWAIT:
23825b81b6b3SRodney W. Grimes 		/* allow heads to settle */
23836182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 8);
23845b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
23855b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2386fb35bd37SJoerg Wunsch 
23875b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
23883a2f7427SDavid Greenman 		do {
23893a2f7427SDavid Greenman 			/*
2390dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
23913a2f7427SDavid Greenman 			 */
2392dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2393246ed35dSJoerg Wunsch 				return (0); /* will return later */
2394dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
2395dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2396246ed35dSJoerg Wunsch 				return (0); /* hope for a real intr */
23973a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
23983a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
23995b81b6b3SRodney W. Grimes 		{
2400dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
2401dc8603e3SJoerg Wunsch 				/*
2402246ed35dSJoerg Wunsch 				 * A recalibrate from beyond cylinder 77
2403dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
2404dc8603e3SJoerg Wunsch 				 * since people used to complain much about
2405dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
2406dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
2407246ed35dSJoerg Wunsch 				 * time in a line.
2408dc8603e3SJoerg Wunsch 				 */
2409dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
2410dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
24113a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
24126182fdbdSPeter Wemm 			return (retrier(fdc));
24135b81b6b3SRodney W. Grimes 		}
24145b81b6b3SRodney W. Grimes 		fd->track = 0;
24155b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
24165b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
2417246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2418fb35bd37SJoerg Wunsch 
24195b81b6b3SRodney W. Grimes 	case MOTORWAIT:
24205b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
24215b81b6b3SRodney W. Grimes 		{
24225b81b6b3SRodney W. Grimes 			return (0); /* time's not up yet */
24235b81b6b3SRodney W. Grimes 		}
24245c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
24255c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
24265c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
2427fb35bd37SJoerg Wunsch 		} else
2428fb35bd37SJoerg Wunsch 			fdc->state = DOSEEK;
2429246ed35dSJoerg Wunsch 		return (1);	/* will return immediately */
2430fb35bd37SJoerg Wunsch 
24315b81b6b3SRodney W. Grimes 	default:
2432b6e5f28eSPeter Wemm 		device_printf(fdc->fdc_dev, "unexpected FD int->");
243364860614SJoerg Wunsch 		if (fd_read_status(fdc) == 0)
2434a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
24355b81b6b3SRodney W. Grimes 			       fdc->status[0],
24365b81b6b3SRodney W. Grimes 			       fdc->status[1],
24375b81b6b3SRodney W. Grimes 			       fdc->status[2],
24385b81b6b3SRodney W. Grimes 			       fdc->status[3],
24395b81b6b3SRodney W. Grimes 			       fdc->status[4],
24405b81b6b3SRodney W. Grimes 			       fdc->status[5],
24415b81b6b3SRodney W. Grimes 			       fdc->status[6] );
24423a2f7427SDavid Greenman 		else
2443dac0f2dbSJoerg Wunsch 			printf("No status available   ");
2444dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
2445dac0f2dbSJoerg Wunsch 		{
2446dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
2447246ed35dSJoerg Wunsch 			return (0); /* will return later */
24485b81b6b3SRodney W. Grimes 		}
2449dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
2450246ed35dSJoerg Wunsch 		return (0);	/* will return later */
2451dac0f2dbSJoerg Wunsch 	}
2452246ed35dSJoerg Wunsch 	/* noone should ever get here */
24535b81b6b3SRodney W. Grimes }
24545b81b6b3SRodney W. Grimes 
2455aaf08d94SGarrett Wollman static int
24566182fdbdSPeter Wemm retrier(struct fdc_data *fdc)
24575b81b6b3SRodney W. Grimes {
24588177437dSPoul-Henning Kamp 	struct bio *bp;
24596182fdbdSPeter Wemm 	struct fd_data *fd;
24606182fdbdSPeter Wemm 	int fdu;
24615b81b6b3SRodney W. Grimes 
2462e93e63cbSBruce Evans 	bp = fdc->bp;
24635b81b6b3SRodney W. Grimes 
24646182fdbdSPeter Wemm 	/* XXX shouldn't this be cached somewhere?  */
24658177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
24666182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
24676182fdbdSPeter Wemm 	if (fd->options & FDOPT_NORETRY)
24683a2f7427SDavid Greenman 		goto fail;
24696182fdbdSPeter Wemm 
24706182fdbdSPeter Wemm 	switch (fdc->retry) {
24715b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
24725b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
24735b81b6b3SRodney W. Grimes 		break;
24745b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
24755b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
24765b81b6b3SRodney W. Grimes 		break;
24775b81b6b3SRodney W. Grimes 	case 6:
24785b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
24795b81b6b3SRodney W. Grimes 		break;
24805b81b6b3SRodney W. Grimes 	case 7:
24815b81b6b3SRodney W. Grimes 		break;
24825b81b6b3SRodney W. Grimes 	default:
24833a2f7427SDavid Greenman 	fail:
2484fb35bd37SJoerg Wunsch 		if ((fd->options & FDOPT_NOERRLOG) == 0) {
2485fb35bd37SJoerg Wunsch 			diskerr(bp, "hard error", fdc->fd->skip / DEV_BSIZE,
24863a2f7427SDavid Greenman 				(struct disklabel *)NULL);
2487fb35bd37SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID) {
2488dc5df763SJoerg Wunsch 				printf(
2489a838d83dSBruce Evans 				" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2490dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
2491dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
2492dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
2493dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
2494dc5df763SJoerg Wunsch 				       fdc->status[5]);
2495dc5df763SJoerg Wunsch 			}
2496dc5df763SJoerg Wunsch 			else
2497dc5df763SJoerg Wunsch 				printf(" (No status)\n");
24985b81b6b3SRodney W. Grimes 		}
24992995d110SJoerg Wunsch 		if ((fd->options & FDOPT_NOERROR) == 0) {
25008177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
25018177437dSPoul-Henning Kamp 			bp->bio_error = EIO;
2502fb35bd37SJoerg Wunsch 			bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
2503fb35bd37SJoerg Wunsch 		} else
2504fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2505e93e63cbSBruce Evans 		fdc->bp = NULL;
25065b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
25075f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
2508a468031cSPoul-Henning Kamp 		biofinish(bp, &fdc->fd->device_stats, 0);
250992ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
25105c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
25115b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
25125b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
251392ed385aSRodney W. Grimes 		return (1);
25145b81b6b3SRodney W. Grimes 	}
25155b81b6b3SRodney W. Grimes 	fdc->retry++;
25165b81b6b3SRodney W. Grimes 	return (1);
25175b81b6b3SRodney W. Grimes }
25185b81b6b3SRodney W. Grimes 
25191fdb6e6cSPoul-Henning Kamp static void
25201fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp)
25211fdb6e6cSPoul-Henning Kamp {
25221fdb6e6cSPoul-Henning Kamp 	wakeup(bp);
25231fdb6e6cSPoul-Henning Kamp }
25241fdb6e6cSPoul-Henning Kamp 
2525b39c878eSAndrey A. Chernov static int
2526f664aeeeSJoerg Wunsch fdmisccmd(dev_t dev, u_int cmd, void *data)
2527b39c878eSAndrey A. Chernov {
2528b39c878eSAndrey A. Chernov  	fdu_t fdu;
2529b39c878eSAndrey A. Chernov  	fd_p fd;
25301fdb6e6cSPoul-Henning Kamp 	struct bio *bp;
2531f664aeeeSJoerg Wunsch 	struct fd_formb *finfo;
2532f664aeeeSJoerg Wunsch 	struct fdc_readid *idfield;
25333a2f7427SDavid Greenman 	size_t fdblk;
2534b39c878eSAndrey A. Chernov 
2535b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
25366182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
25373a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2538f664aeeeSJoerg Wunsch 	finfo = (struct fd_formb *)data;
2539f664aeeeSJoerg Wunsch 	idfield = (struct fdc_readid *)data;
2540b39c878eSAndrey A. Chernov 
2541fb35bd37SJoerg Wunsch 	bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO);
2542b39c878eSAndrey A. Chernov 
2543b39c878eSAndrey A. Chernov 	/*
2544f664aeeeSJoerg Wunsch 	 * Set up a bio request for fdstrategy().  bio_blkno is faked
2545f664aeeeSJoerg Wunsch 	 * so that fdstrategy() will seek to the the requested
2546817988beSPoul-Henning Kamp 	 * cylinder, and use the desired head.
2547b39c878eSAndrey A. Chernov 	 */
2548f664aeeeSJoerg Wunsch 	bp->bio_cmd = cmd;
2549f664aeeeSJoerg Wunsch 	if (cmd == BIO_FORMAT) {
2550f664aeeeSJoerg Wunsch 		bp->bio_blkno =
2551f664aeeeSJoerg Wunsch 		    (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
2552f664aeeeSJoerg Wunsch 		     finfo->head * fd->ft->sectrac) *
2553f664aeeeSJoerg Wunsch 		    fdblk / DEV_BSIZE;
2554f664aeeeSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fd_idfield_data) *
2555f664aeeeSJoerg Wunsch 		    finfo->fd_formb_nsecs;
2556f664aeeeSJoerg Wunsch 	} else if (cmd == BIO_RDSECTID) {
2557f664aeeeSJoerg Wunsch 		bp->bio_blkno =
2558f664aeeeSJoerg Wunsch 		    (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
2559f664aeeeSJoerg Wunsch 		     idfield->head * fd->ft->sectrac) *
2560f664aeeeSJoerg Wunsch 		    fdblk / DEV_BSIZE;
2561250300ebSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fdc_readid);
2562f664aeeeSJoerg Wunsch 	} else
2563f664aeeeSJoerg Wunsch 		panic("wrong cmd in fdmisccmd()");
2564f664aeeeSJoerg Wunsch 	bp->bio_data = data;
2565250300ebSJoerg Wunsch 	bp->bio_dev = dev;
2566250300ebSJoerg Wunsch 	bp->bio_done = fdbiodone;
2567817988beSPoul-Henning Kamp 	bp->bio_flags = 0;
2568250300ebSJoerg Wunsch 
2569f5f7ba03SJordan K. Hubbard 	/*
2570fb35bd37SJoerg Wunsch 	 * Now run the command.  The wait loop is a version of bufwait()
2571fb35bd37SJoerg Wunsch 	 * adapted for struct bio instead of struct buf and specialized
2572fb35bd37SJoerg Wunsch 	 * for the current context.
2573f5f7ba03SJordan K. Hubbard 	 */
2574f664aeeeSJoerg Wunsch 	fdstrategy(bp);
2575f664aeeeSJoerg Wunsch 	while ((bp->bio_flags & BIO_DONE) == 0)
2576f664aeeeSJoerg Wunsch 		tsleep(bp, PRIBIO, "fdcmd", 0);
2577f664aeeeSJoerg Wunsch 
2578f664aeeeSJoerg Wunsch 	free(bp, M_TEMP);
2579f664aeeeSJoerg Wunsch 	return (bp->bio_flags & BIO_ERROR ? bp->bio_error : 0);
2580f664aeeeSJoerg Wunsch }
25815b81b6b3SRodney W. Grimes 
25823e425b96SJulian Elischer static int
2583b40ce416SJulian Elischer fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
2584f5f7ba03SJordan K. Hubbard {
2585f664aeeeSJoerg Wunsch  	fdu_t fdu;
2586f664aeeeSJoerg Wunsch  	fd_p fd;
2587f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
2588fb35bd37SJoerg Wunsch 	struct disklabel *lp;
25892995d110SJoerg Wunsch 	struct fdc_status *fsp;
2590250300ebSJoerg Wunsch 	struct fdc_readid *rid;
2591f664aeeeSJoerg Wunsch 	size_t fdblk;
25921a6bed68SJoerg Wunsch 	int error, type;
2593f5f7ba03SJordan K. Hubbard 
2594f664aeeeSJoerg Wunsch  	fdu = FDUNIT(minor(dev));
25951a6bed68SJoerg Wunsch 	type = FDTYPE(minor(dev));
2596f664aeeeSJoerg Wunsch  	fd = devclass_get_softc(fd_devclass, fdu);
2597fb35bd37SJoerg Wunsch 
25981a6bed68SJoerg Wunsch 	/*
25991a6bed68SJoerg Wunsch 	 * First, handle everything that could be done with
26001a6bed68SJoerg Wunsch 	 * FD_NONBLOCK still being set.
26011a6bed68SJoerg Wunsch 	 */
26021a6bed68SJoerg Wunsch 	switch (cmd) {
26031a6bed68SJoerg Wunsch 	case FIONBIO:
26041a6bed68SJoerg Wunsch 		if (*(int *)addr != 0)
26051a6bed68SJoerg Wunsch 			fd->flags |= FD_NONBLOCK;
26061a6bed68SJoerg Wunsch 		else {
26071a6bed68SJoerg Wunsch 			if (fd->ft == 0) {
26081a6bed68SJoerg Wunsch 				/*
26091a6bed68SJoerg Wunsch 				 * No drive type has been selected yet,
26101a6bed68SJoerg Wunsch 				 * cannot turn FNONBLOCK off.
26111a6bed68SJoerg Wunsch 				 */
26121a6bed68SJoerg Wunsch 				return (EINVAL);
26131a6bed68SJoerg Wunsch 			}
26141a6bed68SJoerg Wunsch 			fd->flags &= ~FD_NONBLOCK;
26151a6bed68SJoerg Wunsch 		}
26161a6bed68SJoerg Wunsch 		return (0);
2617fb35bd37SJoerg Wunsch 
26181a6bed68SJoerg Wunsch 	case FIOASYNC:
26191a6bed68SJoerg Wunsch 		/* keep the generic fcntl() code happy */
26201a6bed68SJoerg Wunsch 		return (0);
26211a6bed68SJoerg Wunsch 
26221a6bed68SJoerg Wunsch 	case FD_GTYPE:                  /* get drive type */
26231a6bed68SJoerg Wunsch 		if (fd->ft == 0)
26241a6bed68SJoerg Wunsch 			/* no type known yet, return the native type */
26251a6bed68SJoerg Wunsch 			*(struct fd_type *)addr = fd_native_types[fd->type];
26261a6bed68SJoerg Wunsch 		else
26271a6bed68SJoerg Wunsch 			*(struct fd_type *)addr = *fd->ft;
26281a6bed68SJoerg Wunsch 		return (0);
26291a6bed68SJoerg Wunsch 
26301a6bed68SJoerg Wunsch 	case FD_STYPE:                  /* set drive type */
26311a6bed68SJoerg Wunsch 		if (type == 0) {
26321a6bed68SJoerg Wunsch 			/*
26331a6bed68SJoerg Wunsch 			 * Allow setting drive type temporarily iff
26341a6bed68SJoerg Wunsch 			 * currently unset.  Used for fdformat so any
26351a6bed68SJoerg Wunsch 			 * user can set it, and then start formatting.
26361a6bed68SJoerg Wunsch 			 */
26371a6bed68SJoerg Wunsch 			if (fd->ft)
26381a6bed68SJoerg Wunsch 				return (EINVAL); /* already set */
26391a6bed68SJoerg Wunsch 			fd->ft = fd->fts;
26401a6bed68SJoerg Wunsch 			*fd->ft = *(struct fd_type *)addr;
26411a6bed68SJoerg Wunsch 			fd->flags |= FD_UA;
26421a6bed68SJoerg Wunsch 		} else {
26431a6bed68SJoerg Wunsch 			/*
26441a6bed68SJoerg Wunsch 			 * Set density definition permanently.  Only
26451a6bed68SJoerg Wunsch 			 * allow for superuser.
26461a6bed68SJoerg Wunsch 			 */
26471a6bed68SJoerg Wunsch 			if (suser_td(td) != 0)
26481a6bed68SJoerg Wunsch 				return (EPERM);
26491a6bed68SJoerg Wunsch 			fd->fts[type] = *(struct fd_type *)addr;
26501a6bed68SJoerg Wunsch 		}
26511a6bed68SJoerg Wunsch 		return (0);
26521a6bed68SJoerg Wunsch 
26531a6bed68SJoerg Wunsch 	case FD_GOPTS:			/* get drive options */
26541a6bed68SJoerg Wunsch 		*(int *)addr = fd->options + (type == 0? FDOPT_AUTOSEL: 0);
26551a6bed68SJoerg Wunsch 		return (0);
26561a6bed68SJoerg Wunsch 
26571a6bed68SJoerg Wunsch 	case FD_SOPTS:			/* set drive options */
26581a6bed68SJoerg Wunsch 		fd->options = *(int *)addr & ~FDOPT_AUTOSEL;
26591a6bed68SJoerg Wunsch 		return (0);
26601a6bed68SJoerg Wunsch 
26611a6bed68SJoerg Wunsch #ifdef FDC_DEBUG
26621a6bed68SJoerg Wunsch 	case FD_DEBUG:
26631a6bed68SJoerg Wunsch 		if ((fd_debug != 0) != (*(int *)addr != 0)) {
26641a6bed68SJoerg Wunsch 			fd_debug = (*(int *)addr != 0);
26651a6bed68SJoerg Wunsch 			printf("fd%d: debugging turned %s\n",
26661a6bed68SJoerg Wunsch 			    fd->fdu, fd_debug ? "on" : "off");
26671a6bed68SJoerg Wunsch 		}
26681a6bed68SJoerg Wunsch 		return (0);
26691a6bed68SJoerg Wunsch #endif
26701a6bed68SJoerg Wunsch 
26711a6bed68SJoerg Wunsch 	case FD_CLRERR:
26721a6bed68SJoerg Wunsch 		if (suser_td(td) != 0)
26731a6bed68SJoerg Wunsch 			return (EPERM);
26741a6bed68SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
26751a6bed68SJoerg Wunsch 		return (0);
26761a6bed68SJoerg Wunsch 
26771a6bed68SJoerg Wunsch 	case FD_GSTAT:
26781a6bed68SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
26791a6bed68SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
26801a6bed68SJoerg Wunsch 			return (EINVAL);
26811a6bed68SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
26821a6bed68SJoerg Wunsch 		return (0);
26831a6bed68SJoerg Wunsch 
26841a6bed68SJoerg Wunsch 	case FD_GDTYPE:
26851a6bed68SJoerg Wunsch 		*(enum fd_drivetype *)addr = fd->type;
26861a6bed68SJoerg Wunsch 		return (0);
26871a6bed68SJoerg Wunsch 	}
26881a6bed68SJoerg Wunsch 
26891a6bed68SJoerg Wunsch 	/*
26901a6bed68SJoerg Wunsch 	 * Now handle everything else.  Make sure we have a valid
26911a6bed68SJoerg Wunsch 	 * drive type.
26921a6bed68SJoerg Wunsch 	 */
26931a6bed68SJoerg Wunsch 	if (fd->flags & FD_NONBLOCK)
26941a6bed68SJoerg Wunsch 		return (EAGAIN);
26951a6bed68SJoerg Wunsch 	if (fd->ft == 0)
26961a6bed68SJoerg Wunsch 		return (ENXIO);
26973a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2698fb35bd37SJoerg Wunsch 	error = 0;
2699f5f7ba03SJordan K. Hubbard 
27006182fdbdSPeter Wemm 	switch (cmd) {
2701f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
2702fb35bd37SJoerg Wunsch 		lp = malloc(sizeof(*lp), M_TEMP, M_ZERO);
2703fb35bd37SJoerg Wunsch 		lp->d_secsize = fdblk;
27046182fdbdSPeter Wemm 		fdt = fd->ft;
2705fb35bd37SJoerg Wunsch 		lp->d_secpercyl = fdt->size / fdt->tracks;
2706fb35bd37SJoerg Wunsch 		lp->d_type = DTYPE_FLOPPY;
2707fb35bd37SJoerg Wunsch 		if (readdisklabel(dkmodpart(dev, RAW_PART), lp) != NULL)
2708f5f7ba03SJordan K. Hubbard 			error = EINVAL;
2709fb35bd37SJoerg Wunsch 		else
2710fb35bd37SJoerg Wunsch 			*(struct disklabel *)addr = *lp;
2711fb35bd37SJoerg Wunsch 		free(lp, M_TEMP);
2712f5f7ba03SJordan K. Hubbard 		break;
2713f5f7ba03SJordan K. Hubbard 
2714f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
2715f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2716fb35bd37SJoerg Wunsch 			return (EBADF);
2717fb35bd37SJoerg Wunsch 		/*
2718fb35bd37SJoerg Wunsch 		 * XXX perhaps should call setdisklabel() to do error checking
2719fb35bd37SJoerg Wunsch 		 * although there is nowhere to "set" the result.  Perhaps
2720fb35bd37SJoerg Wunsch 		 * should always just fail.
2721fb35bd37SJoerg Wunsch 		 */
2722f5f7ba03SJordan K. Hubbard 		break;
2723f5f7ba03SJordan K. Hubbard 
2724f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
2725f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2726fb35bd37SJoerg Wunsch 			return (EBADF);
2727f5f7ba03SJordan K. Hubbard 		break;
2728f5f7ba03SJordan K. Hubbard 
2729f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
2730fb35bd37SJoerg Wunsch 		if ((flag & FWRITE) == 0)
2731fb35bd37SJoerg Wunsch 			return (EBADF);
2732fb35bd37SJoerg Wunsch 		lp = malloc(DEV_BSIZE, M_TEMP, M_ZERO);
2733fb35bd37SJoerg Wunsch 		error = setdisklabel(lp, (struct disklabel *)addr, (u_long)0);
2734fb35bd37SJoerg Wunsch 		if (error != 0)
2735fb35bd37SJoerg Wunsch 			error = writedisklabel(dev, lp);
2736fb35bd37SJoerg Wunsch 		free(lp, M_TEMP);
2737b39c878eSAndrey A. Chernov 		break;
2738f664aeeeSJoerg Wunsch 
2739b39c878eSAndrey A. Chernov 	case FD_FORM:
2740b39c878eSAndrey A. Chernov 		if ((flag & FWRITE) == 0)
2741fb35bd37SJoerg Wunsch 			return (EBADF);	/* must be opened for writing */
2742fb35bd37SJoerg Wunsch 		if (((struct fd_formb *)addr)->format_version !=
2743b39c878eSAndrey A. Chernov 		    FD_FORMAT_VERSION)
2744fb35bd37SJoerg Wunsch 			return (EINVAL); /* wrong version of formatting prog */
2745f664aeeeSJoerg Wunsch 		error = fdmisccmd(dev, BIO_FORMAT, addr);
2746b39c878eSAndrey A. Chernov 		break;
2747b39c878eSAndrey A. Chernov 
2748b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
27493e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2750f5f7ba03SJordan K. Hubbard 		break;
2751f5f7ba03SJordan K. Hubbard 
27523a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
27533a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
2754b40ce416SJulian Elischer 		if (suser_td(td) != 0)
2755fb35bd37SJoerg Wunsch 			return (EPERM);
27563e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
27573a2f7427SDavid Greenman 		break;
27583a2f7427SDavid Greenman 
27593a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
27603e425b96SJulian Elischer 		*(int *)addr = fd->options;
27613a2f7427SDavid Greenman 		break;
27623a2f7427SDavid Greenman 
27633a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
27643e425b96SJulian Elischer 		fd->options = *(int *)addr;
27653a2f7427SDavid Greenman 		break;
27663a2f7427SDavid Greenman 
2767f664aeeeSJoerg Wunsch #ifdef FDC_DEBUG
2768f664aeeeSJoerg Wunsch 	case FD_DEBUG:
27690e17a5bcSJoerg Wunsch 		if ((fd_debug != 0) != (*(int *)addr != 0)) {
27700e17a5bcSJoerg Wunsch 			fd_debug = (*(int *)addr != 0);
27710e17a5bcSJoerg Wunsch 			printf("fd%d: debugging turned %s\n",
27720e17a5bcSJoerg Wunsch 			    fd->fdu, fd_debug ? "on" : "off");
27730e17a5bcSJoerg Wunsch 		}
2774f664aeeeSJoerg Wunsch 		break;
2775f664aeeeSJoerg Wunsch #endif
2776f664aeeeSJoerg Wunsch 
27772995d110SJoerg Wunsch 	case FD_CLRERR:
2778b40ce416SJulian Elischer 		if (suser_td(td) != 0)
2779fb35bd37SJoerg Wunsch 			return (EPERM);
27802995d110SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
27812995d110SJoerg Wunsch 		break;
27822995d110SJoerg Wunsch 
27832995d110SJoerg Wunsch 	case FD_GSTAT:
27842995d110SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
27852995d110SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2786fb35bd37SJoerg Wunsch 			return (EINVAL);
27872995d110SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
27882995d110SJoerg Wunsch 		break;
27892995d110SJoerg Wunsch 
2790250300ebSJoerg Wunsch 	case FD_READID:
2791250300ebSJoerg Wunsch 		rid = (struct fdc_readid *)addr;
2792250300ebSJoerg Wunsch 		if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2793fb35bd37SJoerg Wunsch 			return (EINVAL);
2794f664aeeeSJoerg Wunsch 		error = fdmisccmd(dev, BIO_RDSECTID, addr);
2795250300ebSJoerg Wunsch 		break;
2796250300ebSJoerg Wunsch 
2797f5f7ba03SJordan K. Hubbard 	default:
27983a2f7427SDavid Greenman 		error = ENOTTY;
2799f5f7ba03SJordan K. Hubbard 		break;
2800f5f7ba03SJordan K. Hubbard 	}
2801f5f7ba03SJordan K. Hubbard 	return (error);
2802f5f7ba03SJordan K. Hubbard }
2803