xref: /freebsd/sys/dev/fdc/fdc.c (revision 9b7408fe46f8c67976b72b06eb619b5ad0c11ca1)
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  *
8dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994 by
93a2f7427SDavid Greenman  *  jc@irbs.UUCP (John Capo)
103a2f7427SDavid Greenman  *  vak@zebub.msk.su (Serge Vakulenko)
113a2f7427SDavid Greenman  *  ache@astral.msk.su (Andrew A. Chernov)
12dc16046fSJoerg Wunsch  *
13dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994, 1995 by
143a2f7427SDavid Greenman  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
15dc5df763SJoerg Wunsch  *  dufault@hda.com (Peter Dufault)
163a2f7427SDavid Greenman  *
175b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
185b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
195b81b6b3SRodney W. Grimes  * are met:
205b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
215b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
225b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
235b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
245b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
255b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
265b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
275b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
285b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
295b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
305b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
315b81b6b3SRodney W. Grimes  *    without specific prior written permission.
325b81b6b3SRodney W. Grimes  *
335b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
345b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
355b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
365b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
375b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
385b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
395b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
405b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
415b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
425b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
435b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
445b81b6b3SRodney W. Grimes  *
45dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
469b7408feSJulian Elischer  *	$Id: fd.c,v 1.111 1998/05/06 22:14:41 julian Exp $
475b81b6b3SRodney W. Grimes  *
485b81b6b3SRodney W. Grimes  */
495b81b6b3SRodney W. Grimes 
50b99f0a4aSAndrew Moore #include "ft.h"
51b99f0a4aSAndrew Moore #if NFT < 1
52b99f0a4aSAndrew Moore #undef NFDC
53b99f0a4aSAndrew Moore #endif
545b81b6b3SRodney W. Grimes #include "fd.h"
557b778b5eSEivind Eklund #include "opt_devfs.h"
56d2fb4892SJoerg Wunsch #include "opt_fdc.h"
575b81b6b3SRodney W. Grimes 
58b99f0a4aSAndrew Moore #if NFDC > 0
59b99f0a4aSAndrew Moore 
60b99f0a4aSAndrew Moore #include <sys/param.h>
61b99f0a4aSAndrew Moore #include <sys/systm.h>
62b99f0a4aSAndrew Moore #include <sys/kernel.h>
63b99f0a4aSAndrew Moore #include <sys/conf.h>
643ac4d1efSBruce Evans #include <sys/fcntl.h>
65671e2ceeSBruce Evans #include <machine/clock.h>
66b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h>
67b99f0a4aSAndrew Moore #include <sys/disklabel.h>
68b99f0a4aSAndrew Moore #include <sys/buf.h>
69b99f0a4aSAndrew Moore #include <sys/malloc.h>
703a2f7427SDavid Greenman #include <sys/proc.h>
71b99f0a4aSAndrew Moore #include <sys/syslog.h>
72d4319080SBruce Evans #ifdef notyet
7392200632SGarrett Wollman #include <sys/dkstat.h>
74d4319080SBruce Evans #endif
75f540b106SGarrett Wollman #include <i386/isa/isa.h>
76f540b106SGarrett Wollman #include <i386/isa/isa_device.h>
77f540b106SGarrett Wollman #include <i386/isa/fdreg.h>
78f540b106SGarrett Wollman #include <i386/isa/fdc.h>
79f540b106SGarrett Wollman #include <i386/isa/rtc.h>
80dc5df763SJoerg Wunsch #include <machine/stdarg.h>
8187eafbcaSPoul-Henning Kamp #if NFT > 0
8287eafbcaSPoul-Henning Kamp #include <sys/ftape.h>
8387eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h>
8487eafbcaSPoul-Henning Kamp #endif
858af5d536SJulian Elischer #ifdef	DEVFS
868af5d536SJulian Elischer #include <sys/devfsext.h>
873e425b96SJulian Elischer #ifdef	SLICE
883e425b96SJulian Elischer #include <sys/device.h>
893e425b96SJulian Elischer #include <dev/slice/slice.h>
903e425b96SJulian Elischer #endif	/* SLICE */
913e425b96SJulian Elischer #endif	/* DEVFS */
925b81b6b3SRodney W. Grimes 
93b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
94b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
955b81b6b3SRodney W. Grimes 
960722d6abSJoerg Wunsch /* configuration flags */
970722d6abSJoerg Wunsch #define FDC_PRETEND_D0	(1 << 0)	/* pretend drive 0 to be there */
980722d6abSJoerg Wunsch 
990722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */
1000722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED	0x1000
1010722d6abSJoerg Wunsch 
1023a2f7427SDavid Greenman /*
1033a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
1043a2f7427SDavid Greenman  * on the controller
1053a2f7427SDavid Greenman  */
1063a2f7427SDavid Greenman #define id_physid id_scsiid
1073a2f7427SDavid Greenman 
108dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
109dc5df763SJoerg Wunsch #define FD_FAILED -1
110dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
111dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
112dc5df763SJoerg Wunsch 
113b39c878eSAndrey A. Chernov #define NUMTYPES 14
114b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1157ca0641bSAndrey A. Chernov 
1163a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
117b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
118b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
119b99f0a4aSAndrew Moore #define FD_1720         1
120b99f0a4aSAndrew Moore #define FD_1480         2
121b99f0a4aSAndrew Moore #define FD_1440         3
122b99f0a4aSAndrew Moore #define FD_1200         4
123b99f0a4aSAndrew Moore #define FD_820          5
124b99f0a4aSAndrew Moore #define FD_800          6
125b99f0a4aSAndrew Moore #define FD_720          7
126b99f0a4aSAndrew Moore #define FD_360          8
127ed2fa05eSAndrey A. Chernov 
128b99f0a4aSAndrew Moore #define FD_1480in5_25   9
129b99f0a4aSAndrew Moore #define FD_1440in5_25   10
130b99f0a4aSAndrew Moore #define FD_820in5_25    11
131b99f0a4aSAndrew Moore #define FD_800in5_25    12
132b99f0a4aSAndrew Moore #define FD_720in5_25    13
133b99f0a4aSAndrew Moore #define FD_360in5_25    14
134b99f0a4aSAndrew Moore 
1357ca0641bSAndrey A. Chernov 
1366f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
1375b81b6b3SRodney W. Grimes {
138126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
139126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
140126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
141126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
142126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
143126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
144126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
145b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
146ed2fa05eSAndrey A. Chernov 
147126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
148126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
149126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
150126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
151126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
152126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1535b81b6b3SRodney W. Grimes };
1545b81b6b3SRodney W. Grimes 
155b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
156dc16046fSJoerg Wunsch 
1575b81b6b3SRodney W. Grimes /***********************************************************************\
1585b81b6b3SRodney W. Grimes * Per controller structure.						*
1595b81b6b3SRodney W. Grimes \***********************************************************************/
160b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
1615b81b6b3SRodney W. Grimes 
1625b81b6b3SRodney W. Grimes /***********************************************************************\
1635b81b6b3SRodney W. Grimes * Per drive structure.							*
164b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1655b81b6b3SRodney W. Grimes \***********************************************************************/
1666f4e0bebSPoul-Henning Kamp static struct fd_data {
167b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1685b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1693a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1705b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1715b81b6b3SRodney W. Grimes 	int	flags;
1725b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1735b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1745b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1755b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1765b81b6b3SRodney W. Grimes 	int	skip;
1775b81b6b3SRodney W. Grimes 	int	hddrv;
178dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
1795b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1803a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
181d4319080SBruce Evans #ifdef notyet
18292200632SGarrett Wollman 	int	dkunit;		/* disk stats unit number */
183d4319080SBruce Evans #endif
18402a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
18502a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
18687f6c662SJulian Elischer #ifdef DEVFS
1873e425b96SJulian Elischer #ifdef SLICE
1883e425b96SJulian Elischer 	int	unit;		/* as in fd0 */
1893e425b96SJulian Elischer 	void	*bdevs[MAXPARTITIONS];
1903e425b96SJulian Elischer 	void	*cdevs[MAXPARTITIONS];
1913e425b96SJulian Elischer 	struct subdev{
1923e425b96SJulian Elischer 		struct slice		*slice;
1933e425b96SJulian Elischer 		int			 minor;
1943e425b96SJulian Elischer 		struct fd_data		*drive;
1953e425b96SJulian Elischer 		struct slicelimits	 limit;
1963e425b96SJulian Elischer 	}subdevs[16];
1973e425b96SJulian Elischer 	struct intr_config_hook ich;
1983e425b96SJulian Elischer #else	/* SLICE */
19921519754SBruce Evans 	void	*bdevs[1 + NUMDENS + MAXPARTITIONS];
20021519754SBruce Evans 	void	*cdevs[1 + NUMDENS + MAXPARTITIONS];
2013e425b96SJulian Elischer #endif	/* SLICE */
20287f6c662SJulian Elischer #endif
2035b81b6b3SRodney W. Grimes } fd_data[NFD];
2045b81b6b3SRodney W. Grimes 
2055b81b6b3SRodney W. Grimes /***********************************************************************\
2065b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
2075b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
2085b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
2095b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
2105b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
2115b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
2125b81b6b3SRodney W. Grimes \***********************************************************************/
213b99f0a4aSAndrew Moore 
2143a2f7427SDavid Greenman #if NFT > 0
2153a2f7427SDavid Greenman int ftopen(dev_t, int);
2163a2f7427SDavid Greenman int ftintr(ftu_t ftu);
2173a2f7427SDavid Greenman int ftclose(dev_t, int);
2183a2f7427SDavid Greenman void ftstrategy(struct buf *);
2193a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
2203a2f7427SDavid Greenman int ftdump(dev_t);
2213a2f7427SDavid Greenman int ftsize(dev_t);
22274fa89f4SRodney W. Grimes int ftattach(struct isa_device *, struct isa_device *, int);
2233a2f7427SDavid Greenman #endif
2245b81b6b3SRodney W. Grimes 
2253a2f7427SDavid Greenman /* autoconfig functions */
2263a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2273a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2283a2f7427SDavid Greenman 
2293a2f7427SDavid Greenman /* needed for ft driver, thus exported */
2303a2f7427SDavid Greenman int in_fdc(fdcu_t);
2313a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
2323a2f7427SDavid Greenman 
2333a2f7427SDavid Greenman /* internal functions */
2343a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
2353a2f7427SDavid Greenman #  define TURNON 1
2363a2f7427SDavid Greenman #  define TURNOFF 0
2373a2f7427SDavid Greenman static timeout_t fd_turnoff;
2383a2f7427SDavid Greenman static timeout_t fd_motor_on;
2393a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2403a2f7427SDavid Greenman static void fdc_reset(fdc_p);
241b5e8ce9fSBruce Evans static int fd_in(fdcu_t, int *);
2423a2f7427SDavid Greenman static void fdstart(fdcu_t);
2433a2f7427SDavid Greenman static timeout_t fd_timeout;
2443a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2453a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
246aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2473e425b96SJulian Elischer #ifndef SLICE
2483a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2493e425b96SJulian Elischer #endif
2503a2f7427SDavid Greenman 
251d66c374fSTor Egge static int enable_fifo(fdc_p fdc);
252d66c374fSTor Egge 
253d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
254d66c374fSTor Egge 
255aaf08d94SGarrett Wollman 
2565b81b6b3SRodney W. Grimes #define DEVIDLE		0
2575b81b6b3SRodney W. Grimes #define FINDWORK	1
2585b81b6b3SRodney W. Grimes #define	DOSEEK		2
2595b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
2605b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
2615b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
2625b81b6b3SRodney W. Grimes #define	STARTRECAL	6
2635b81b6b3SRodney W. Grimes #define	RESETCTLR	7
2645b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
2655b81b6b3SRodney W. Grimes #define	RECALWAIT	9
2665b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
2675b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
2685b81b6b3SRodney W. Grimes 
269d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
270cba2a7c6SBruce Evans static char const * const fdstates[] =
2715b81b6b3SRodney W. Grimes {
2725b81b6b3SRodney W. Grimes "DEVIDLE",
2735b81b6b3SRodney W. Grimes "FINDWORK",
2745b81b6b3SRodney W. Grimes "DOSEEK",
2755b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2765b81b6b3SRodney W. Grimes "IOCOMPLETE",
2775b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2785b81b6b3SRodney W. Grimes "STARTRECAL",
2795b81b6b3SRodney W. Grimes "RESETCTLR",
2805b81b6b3SRodney W. Grimes "SEEKWAIT",
2815b81b6b3SRodney W. Grimes "RECALWAIT",
2825b81b6b3SRodney W. Grimes "MOTORWAIT",
2835b81b6b3SRodney W. Grimes "IOTIMEDOUT"
2845b81b6b3SRodney W. Grimes };
2855b81b6b3SRodney W. Grimes 
2863a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
287cba2a7c6SBruce Evans static int volatile fd_debug = 0;
2885b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2895b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
290d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
2915b81b6b3SRodney W. Grimes #define TRACE0(arg)
2925b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
293d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
2945b81b6b3SRodney W. Grimes 
295dc16046fSJoerg Wunsch /* autoconfig structure */
296dc16046fSJoerg Wunsch 
297dc16046fSJoerg Wunsch struct	isa_driver fdcdriver = {
298dc16046fSJoerg Wunsch 	fdprobe, fdattach, "fdc",
299dc16046fSJoerg Wunsch };
300dc16046fSJoerg Wunsch 
30187f6c662SJulian Elischer static	d_open_t	Fdopen;	/* NOTE, not fdopen */
30287f6c662SJulian Elischer static	d_close_t	fdclose;
30387f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
30487f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
30587f6c662SJulian Elischer 
3063e425b96SJulian Elischer /* even if SLICE defined, these are needed for the ft support. */
30787f6c662SJulian Elischer #define CDEV_MAJOR 9
30887f6c662SJulian Elischer #define BDEV_MAJOR 2
309cba8a5ddSPoul-Henning Kamp static struct cdevsw fd_cdevsw;
310d2f265faSPoul-Henning Kamp static struct bdevsw fd_bdevsw =
31187f6c662SJulian Elischer 	{ Fdopen,	fdclose,	fdstrategy,	fdioctl,	/*2*/
312996c772fSJohn Dyson 	  nodump,	nopsize,	D_DISK,	"fd",	&fd_cdevsw,	-1 };
31387f6c662SJulian Elischer 
31487f6c662SJulian Elischer 
3156f4e0bebSPoul-Henning Kamp static struct isa_device *fdcdevs[NFDC];
31692200632SGarrett Wollman 
3173e425b96SJulian Elischer #ifdef	SLICE
3183e425b96SJulian Elischer static sl_h_IO_req_t	fdsIOreq;	/* IO req downward (to device) */
3193e425b96SJulian Elischer static sl_h_ioctl_t	fdsioctl;	/* ioctl req downward (to device) */
3203e425b96SJulian Elischer static sl_h_open_t	fdsopen;	/* downwards travelling open */
32126d3bf5fSJulian Elischer /*static sl_h_close_t	fdsclose; */	/* downwards travelling close */
3223e425b96SJulian Elischer static void	fdsinit(void *);
3233e425b96SJulian Elischer 
3243e425b96SJulian Elischer static struct slice_handler slicetype = {
3253e425b96SJulian Elischer 	"floppy",
3263e425b96SJulian Elischer 	0,
3273e425b96SJulian Elischer 	NULL,
3283e425b96SJulian Elischer 	0,
3293e425b96SJulian Elischer 	NULL,	/* constructor */
3303e425b96SJulian Elischer 	&fdsIOreq,
3313e425b96SJulian Elischer 	&fdsioctl,
3323e425b96SJulian Elischer 	&fdsopen,
33326d3bf5fSJulian Elischer 	/*&fdsclose*/NULL,
3343e425b96SJulian Elischer 	NULL,	/* revoke */
3353e425b96SJulian Elischer 	NULL,	/* claim */
3363e425b96SJulian Elischer 	NULL,	/* verify */
3379b7408feSJulian Elischer 	NULL,	/* upconfig */
3389b7408feSJulian Elischer 	NULL	/* dump */
3393e425b96SJulian Elischer };
3403e425b96SJulian Elischer #endif	/* SLICE */
3413e425b96SJulian Elischer 
342dc5df763SJoerg Wunsch static int
343dc5df763SJoerg Wunsch fdc_err(fdcu_t fdcu, const char *s)
344dc5df763SJoerg Wunsch {
345dc5df763SJoerg Wunsch 	fdc_data[fdcu].fdc_errs++;
34616b04b6aSJoerg Wunsch 	if(s) {
347dc5df763SJoerg Wunsch 		if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX)
3486a0e6f42SRodney W. Grimes 			printf("fdc%d: %s", fdcu, s);
349dc5df763SJoerg Wunsch 		else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX)
350dc5df763SJoerg Wunsch 			printf("fdc%d: too many errors, not logging any more\n",
351dc5df763SJoerg Wunsch 			       fdcu);
35216b04b6aSJoerg Wunsch 	}
353dc5df763SJoerg Wunsch 
354dc5df763SJoerg Wunsch 	return FD_FAILED;
355dc5df763SJoerg Wunsch }
356dc5df763SJoerg Wunsch 
357dc5df763SJoerg Wunsch /*
358dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
359dc5df763SJoerg Wunsch  * Unit number,
360dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
361dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
362dc5df763SJoerg Wunsch  */
363dc5df763SJoerg Wunsch 
3646f4e0bebSPoul-Henning Kamp static int
365dc5df763SJoerg Wunsch fd_cmd(fdcu_t fdcu, int n_out, ...)
366dc5df763SJoerg Wunsch {
367dc5df763SJoerg Wunsch 	u_char cmd;
368dc5df763SJoerg Wunsch 	int n_in;
369dc5df763SJoerg Wunsch 	int n;
370dc5df763SJoerg Wunsch 	va_list ap;
371dc5df763SJoerg Wunsch 
372dc5df763SJoerg Wunsch 	va_start(ap, n_out);
373dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
374dc5df763SJoerg Wunsch 	va_end(ap);
375dc5df763SJoerg Wunsch 	va_start(ap, n_out);
376dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
377dc5df763SJoerg Wunsch 	{
378dc5df763SJoerg Wunsch 		if (out_fdc(fdcu, va_arg(ap, int)) < 0)
379dc5df763SJoerg Wunsch 		{
380dc5df763SJoerg Wunsch 			char msg[50];
381dc5df763SJoerg Wunsch 			sprintf(msg,
382dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
383dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
384dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
385dc5df763SJoerg Wunsch 		}
386dc5df763SJoerg Wunsch 	}
387dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
388dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
389dc5df763SJoerg Wunsch 	{
390dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
391dc5df763SJoerg Wunsch 		if (fd_in(fdcu, ptr) < 0)
392dc5df763SJoerg Wunsch 		{
393dc5df763SJoerg Wunsch 			char msg[50];
394dc5df763SJoerg Wunsch 			sprintf(msg,
395dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
396dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
397dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
398dc5df763SJoerg Wunsch 		}
399dc5df763SJoerg Wunsch 	}
400dc5df763SJoerg Wunsch 
401dc5df763SJoerg Wunsch 	return 0;
402dc5df763SJoerg Wunsch }
403dc5df763SJoerg Wunsch 
4046f4e0bebSPoul-Henning Kamp static int
405d66c374fSTor Egge enable_fifo(fdc_p fdc)
406d66c374fSTor Egge {
407d66c374fSTor Egge 	int i, j;
408d66c374fSTor Egge 
409d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
410d66c374fSTor Egge 
411d66c374fSTor Egge 		/*
412d66c374fSTor Egge 		 * XXX:
413d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
414d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
415d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
416d66c374fSTor Egge 		 */
417d66c374fSTor Egge 
418d66c374fSTor Egge 		if (out_fdc(fdc->fdcu, I8207X_CONFIGURE) < 0)
419d66c374fSTor Egge 			return fdc_err(fdc->fdcu, "Enable FIFO failed\n");
420d66c374fSTor Egge 
421d66c374fSTor Egge 		/* If command is invalid, return */
422d66c374fSTor Egge 		j = 100000;
423d66c374fSTor Egge 		while ((i = inb(fdc->baseport + FDSTS) & (NE7_DIO | NE7_RQM))
424d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
425d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
426d66c374fSTor Egge 				fdc_reset(fdc);
427d66c374fSTor Egge 				return FD_FAILED;
428d66c374fSTor Egge 			}
429d66c374fSTor Egge 		if (j<0 ||
430d66c374fSTor Egge 		    fd_cmd(fdc->fdcu, 3,
431d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
432d66c374fSTor Egge 			fdc_reset(fdc);
433d66c374fSTor Egge 			return fdc_err(fdc->fdcu, "Enable FIFO failed\n");
434d66c374fSTor Egge 		}
435d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
436d66c374fSTor Egge 		return 0;
437d66c374fSTor Egge 	}
438d66c374fSTor Egge 	if (fd_cmd(fdc->fdcu, 4,
439d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
440d66c374fSTor Egge 		return fdc_err(fdc->fdcu, "Re-enable FIFO failed\n");
441d66c374fSTor Egge 	return 0;
442d66c374fSTor Egge }
443d66c374fSTor Egge 
444d66c374fSTor Egge static int
445dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
446dc5df763SJoerg Wunsch {
447dc5df763SJoerg Wunsch 	int st3;
448dc5df763SJoerg Wunsch 
449dc5df763SJoerg Wunsch 	if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
450dc5df763SJoerg Wunsch 	{
4516a0e6f42SRodney W. Grimes 		return fdc_err(fdc->fdcu, "Sense Drive Status failed\n");
452dc5df763SJoerg Wunsch 	}
453dc5df763SJoerg Wunsch 	if (st3p)
454dc5df763SJoerg Wunsch 		*st3p = st3;
455dc5df763SJoerg Wunsch 
456dc5df763SJoerg Wunsch 	return 0;
457dc5df763SJoerg Wunsch }
458dc5df763SJoerg Wunsch 
4596f4e0bebSPoul-Henning Kamp static int
460dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
461dc5df763SJoerg Wunsch {
462dc5df763SJoerg Wunsch 	int st0, cyl;
463dc5df763SJoerg Wunsch 
464dc5df763SJoerg Wunsch 	int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0);
465dc5df763SJoerg Wunsch 
466dc5df763SJoerg Wunsch 	if (ret)
467dc5df763SJoerg Wunsch 	{
468dc5df763SJoerg Wunsch 		(void)fdc_err(fdc->fdcu,
469dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
470dc5df763SJoerg Wunsch 		return ret;
471dc5df763SJoerg Wunsch 	}
472dc5df763SJoerg Wunsch 
473dc5df763SJoerg Wunsch 	if (st0p)
474dc5df763SJoerg Wunsch 		*st0p = st0;
475dc5df763SJoerg Wunsch 
476dc5df763SJoerg Wunsch 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV)
477dc5df763SJoerg Wunsch 	{
478dc5df763SJoerg Wunsch 		/*
479dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
480dc5df763SJoerg Wunsch 		 */
481dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
482dc5df763SJoerg Wunsch 	}
483dc5df763SJoerg Wunsch 
484dc5df763SJoerg Wunsch 	if (fd_in(fdc->fdcu, &cyl) < 0)
485dc5df763SJoerg Wunsch 	{
486dc5df763SJoerg Wunsch 		return fdc_err(fdc->fdcu, "can't get cyl num\n");
487dc5df763SJoerg Wunsch 	}
488dc5df763SJoerg Wunsch 
489dc5df763SJoerg Wunsch 	if (cylp)
490dc5df763SJoerg Wunsch 		*cylp = cyl;
491dc5df763SJoerg Wunsch 
492dc5df763SJoerg Wunsch 	return 0;
493dc5df763SJoerg Wunsch }
494dc5df763SJoerg Wunsch 
495dc5df763SJoerg Wunsch 
4966f4e0bebSPoul-Henning Kamp static int
497dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
498dc5df763SJoerg Wunsch {
499dc5df763SJoerg Wunsch 	int i, ret;
500b5e8ce9fSBruce Evans 
501dc5df763SJoerg Wunsch 	for (i = 0; i < 7; i++)
502dc5df763SJoerg Wunsch 	{
503b5e8ce9fSBruce Evans 		/*
504b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
505b5e8ce9fSBruce Evans 		 * from the hardware, but fdc_status wants u_longs and
506b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
507b5e8ce9fSBruce Evans 		 */
508b5e8ce9fSBruce Evans 		int status;
509b5e8ce9fSBruce Evans 
510b5e8ce9fSBruce Evans 		ret = fd_in(fdc->fdcu, &status);
511b5e8ce9fSBruce Evans 		fdc->status[i] = status;
512b5e8ce9fSBruce Evans 		if (ret != 0)
513dc5df763SJoerg Wunsch 			break;
514dc5df763SJoerg Wunsch 	}
515dc5df763SJoerg Wunsch 
516dc5df763SJoerg Wunsch 	if (ret == 0)
517dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
518dc5df763SJoerg Wunsch 	else
519dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
520dc5df763SJoerg Wunsch 
521dc5df763SJoerg Wunsch 	return ret;
522dc5df763SJoerg Wunsch }
523dc5df763SJoerg Wunsch 
5245b81b6b3SRodney W. Grimes /****************************************************************************/
5255b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
5265b81b6b3SRodney W. Grimes /****************************************************************************/
527dc5df763SJoerg Wunsch 
5285b81b6b3SRodney W. Grimes /*
5295b81b6b3SRodney W. Grimes  * probe for existance of controller
5305b81b6b3SRodney W. Grimes  */
5313a2f7427SDavid Greenman static int
532dc5df763SJoerg Wunsch fdprobe(struct isa_device *dev)
5335b81b6b3SRodney W. Grimes {
5345b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5355b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
5365b81b6b3SRodney W. Grimes 	{
5376a0e6f42SRodney W. Grimes 		printf("fdc%d: unit used multiple times\n", fdcu);
5385b81b6b3SRodney W. Grimes 		return 0;
5395b81b6b3SRodney W. Grimes 	}
5405b81b6b3SRodney W. Grimes 
54192200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
5425b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
5435b81b6b3SRodney W. Grimes 
54416111cedSAndrew Moore 	/* First - lets reset the floppy controller */
5453a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
54616111cedSAndrew Moore 	DELAY(100);
5473a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
54816111cedSAndrew Moore 
5495b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
550dc5df763SJoerg Wunsch 	if (fd_cmd(fdcu,
551dc5df763SJoerg Wunsch 		   3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
552dc5df763SJoerg Wunsch 		   0))
5535b81b6b3SRodney W. Grimes 	{
5545b81b6b3SRodney W. Grimes 		return(0);
5555b81b6b3SRodney W. Grimes 	}
5565b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
5575b81b6b3SRodney W. Grimes }
5585b81b6b3SRodney W. Grimes 
5595b81b6b3SRodney W. Grimes /*
5605b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
5615b81b6b3SRodney W. Grimes  */
5623a2f7427SDavid Greenman static int
563dc5df763SJoerg Wunsch fdattach(struct isa_device *dev)
5645b81b6b3SRodney W. Grimes {
5653a2f7427SDavid Greenman 	unsigned fdt;
5665b81b6b3SRodney W. Grimes 	fdu_t	fdu;
5675b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5685b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
5695b81b6b3SRodney W. Grimes 	fd_p	fd;
570cba2a7c6SBruce Evans 	int	fdsu, st0, st3, i;
571cba2a7c6SBruce Evans #if NFT > 0
572cba2a7c6SBruce Evans 	int	unithasfd;
573cba2a7c6SBruce Evans #endif
574b99f0a4aSAndrew Moore 	struct isa_device *fdup;
575dc5df763SJoerg Wunsch 	int ic_type = 0;
576999422d7SJulian Elischer #ifdef DEVFS
5773e425b96SJulian Elischer #ifdef	SLICE
5783e425b96SJulian Elischer 	char namebuf[64];
5793e425b96SJulian Elischer #else
580c8f2fe8dSBruce Evans 	int	mynor;
58121519754SBruce Evans 	int	typemynor;
5823e425b96SJulian Elischer #endif	/* SLICE */
58321519754SBruce Evans 	int	typesize;
58421519754SBruce Evans #endif
58592200632SGarrett Wollman 
5865b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
5875b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
5885b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
589100f78bbSSujal Patel 	/* Acquire the DMA channel forever, The driver will do the rest */
590100f78bbSSujal Patel 	isa_dma_acquire(fdc->dmachan);
591dd87702aSBruce Evans 	isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
5925b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
5933a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
5943a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
59502a19910SJustin T. Gibbs 	bufq_init(&fdc->head);
5965b81b6b3SRodney W. Grimes 
5975b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
598b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
599b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
600b99f0a4aSAndrew Moore 			continue;
601b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
602b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
603b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
604b99f0a4aSAndrew Moore 			continue;
605b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
606b99f0a4aSAndrew Moore 		/* look up what bios thinks we have */
607b99f0a4aSAndrew Moore 		switch (fdu) {
6080722d6abSJoerg Wunsch 			case 0: if (dev->id_flags & FDC_PRETEND_D0)
6090722d6abSJoerg Wunsch 					fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
6100722d6abSJoerg Wunsch 				else
6110722d6abSJoerg Wunsch 					fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
612b99f0a4aSAndrew Moore 				break;
613b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
614b99f0a4aSAndrew Moore 				break;
615b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
616b99f0a4aSAndrew Moore 				break;
617b99f0a4aSAndrew Moore 		}
6185b81b6b3SRodney W. Grimes 		/* is there a unit? */
619b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
620b99f0a4aSAndrew Moore #if NFT > 0
621b99f0a4aSAndrew Moore 		    || (fdsu >= DRVS_PER_CTLR)) {
622b99f0a4aSAndrew Moore #else
623b99f0a4aSAndrew Moore 		) {
62456ef0285SAndrew Moore 			fd->type = NO_TYPE;
625b99f0a4aSAndrew Moore #endif
626b99f0a4aSAndrew Moore #if NFT > 0
627b99f0a4aSAndrew Moore 			/* If BIOS says no floppy, or > 2nd device */
628b99f0a4aSAndrew Moore 			/* Probe for and attach a floppy tape.     */
62974fa89f4SRodney W. Grimes 			/* Tell FT if there was already a disk     */
63074fa89f4SRodney W. Grimes 			/* with this unit number found.            */
63174fa89f4SRodney W. Grimes 
63274fa89f4SRodney W. Grimes 			unithasfd = 0;
63374fa89f4SRodney W. Grimes 			if (fdu < NFD && fd->type != NO_TYPE)
63474fa89f4SRodney W. Grimes 				unithasfd = 1;
63585827d9cSJoerg Wunsch 			if (ftattach(dev, fdup, unithasfd))
636b99f0a4aSAndrew Moore 				continue;
63756ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
638b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
63956ef0285SAndrew Moore #endif
6405b81b6b3SRodney W. Grimes 			continue;
641f5f7ba03SJordan K. Hubbard 		}
6425b81b6b3SRodney W. Grimes 
6435b81b6b3SRodney W. Grimes 		/* select it */
6443a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
6456b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
646dc5df763SJoerg Wunsch 
647dc5df763SJoerg Wunsch 		if (ic_type == 0 &&
648dc5df763SJoerg Wunsch 		    fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
649dc5df763SJoerg Wunsch 		{
650d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6516a0e6f42SRodney W. Grimes 			printf("fdc%d: ", fdcu);
652d2fb4892SJoerg Wunsch #endif
653dc5df763SJoerg Wunsch 			ic_type = (u_char)ic_type;
654dc5df763SJoerg Wunsch 			switch( ic_type ) {
655dc5df763SJoerg Wunsch 			case 0x80:
656d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6576a0e6f42SRodney W. Grimes 				printf("NEC 765\n");
658d2fb4892SJoerg Wunsch #endif
659dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE765;
660dc5df763SJoerg Wunsch 				break;
661dc5df763SJoerg Wunsch 			case 0x81:
662d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6636a0e6f42SRodney W. Grimes 				printf("Intel 82077\n");
664d2fb4892SJoerg Wunsch #endif
665dc5df763SJoerg Wunsch 				fdc->fdct = FDC_I82077;
666dc5df763SJoerg Wunsch 				break;
667dc5df763SJoerg Wunsch 			case 0x90:
668d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6696a0e6f42SRodney W. Grimes 				printf("NEC 72065B\n");
670d2fb4892SJoerg Wunsch #endif
671dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE72065;
672dc5df763SJoerg Wunsch 				break;
673dc5df763SJoerg Wunsch 			default:
674d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6756a0e6f42SRodney W. Grimes 				printf("unknown IC type %02x\n", ic_type);
676d2fb4892SJoerg Wunsch #endif
677dc5df763SJoerg Wunsch 				fdc->fdct = FDC_UNKNOWN;
678dc5df763SJoerg Wunsch 				break;
6796b7bd95bSJoerg Wunsch 			}
680d66c374fSTor Egge 			if (fdc->fdct != FDC_NE765 &&
681d66c374fSTor Egge 			    fdc->fdct != FDC_UNKNOWN &&
682d66c374fSTor Egge 			    enable_fifo(fdc) == 0) {
683d66c374fSTor Egge 				printf("fdc%d: FIFO enabled", fdcu);
684d66c374fSTor Egge 				printf(", %d bytes threshold\n",
685d66c374fSTor Egge 				       fifo_threshold);
686d66c374fSTor Egge 			}
687dc5df763SJoerg Wunsch 		}
688dc5df763SJoerg Wunsch 		if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
689dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
690dc5df763SJoerg Wunsch 			/* if at track 0, first seek inwards */
691dc5df763SJoerg Wunsch 			/* seek some steps: */
692dc5df763SJoerg Wunsch 			(void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
693dc5df763SJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
694dc5df763SJoerg Wunsch 			(void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
695dc5df763SJoerg Wunsch 		}
696dc5df763SJoerg Wunsch 
697dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
698dc5df763SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
699dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
700dc5df763SJoerg Wunsch 			/* Seek some steps... */
701dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
702dc5df763SJoerg Wunsch 				/* ...wait a moment... */
703dc5df763SJoerg Wunsch 				DELAY(300000);
704dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
705dc5df763SJoerg Wunsch 				(void)fd_sense_int(fdc, 0, 0);
706dc5df763SJoerg Wunsch 			}
707dc5df763SJoerg Wunsch 		}
708dc5df763SJoerg Wunsch 
7096b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
7106b7bd95bSJoerg Wunsch 			/*
7116b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
7126b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
7136b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
7146b7bd95bSJoerg Wunsch 			 * more than 77 steps
7156b7bd95bSJoerg Wunsch 			 */
716dc5df763SJoerg Wunsch 			/* go back to 0: */
717dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
7186b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
7196b7bd95bSJoerg Wunsch 				DELAY(i == 0? 1000000: 300000);
7205b81b6b3SRodney W. Grimes 
7216b7bd95bSJoerg Wunsch 				/* anything responding? */
722dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
723dc5df763SJoerg Wunsch 				(st0 & NE7_ST0_EC) == 0)
7246b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
7256b7bd95bSJoerg Wunsch 			}
726dc5df763SJoerg Wunsch 		}
7276b7bd95bSJoerg Wunsch 
7283a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
7293a2f7427SDavid Greenman 
7303a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
7315b81b6b3SRodney W. Grimes 			continue;
7325b81b6b3SRodney W. Grimes 
733dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
7343e425b96SJulian Elischer #ifdef	SLICE
7353e425b96SJulian Elischer 		fd->unit = fdu;
7363e425b96SJulian Elischer #endif
737b99f0a4aSAndrew Moore 		fd->fdc = fdc;
738b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
7393a2f7427SDavid Greenman 		fd->options = 0;
74002a19910SJustin T. Gibbs 		callout_handle_init(&fd->toffhandle);
74102a19910SJustin T. Gibbs 		callout_handle_init(&fd->tohandle);
7422d9d0204SRodney W. Grimes 		printf("fd%d: ", fdu);
7435b81b6b3SRodney W. Grimes 
744b99f0a4aSAndrew Moore 		switch (fdt) {
7457ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
7466a0e6f42SRodney W. Grimes 			printf("1.2MB 5.25in\n");
747b99f0a4aSAndrew Moore 			fd->type = FD_1200;
7487ca0641bSAndrey A. Chernov 			break;
7490722d6abSJoerg Wunsch 		case RTCFDT_144M | RTCFDT_144M_PRETENDED:
7500722d6abSJoerg Wunsch 			printf("config-pretended ");
7510722d6abSJoerg Wunsch 			fdt = RTCFDT_144M;
7520722d6abSJoerg Wunsch 			/* fallthrough */
7537ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
7546a0e6f42SRodney W. Grimes 			printf("1.44MB 3.5in\n");
755b99f0a4aSAndrew Moore 			fd->type = FD_1440;
7567ca0641bSAndrey A. Chernov 			break;
757290dd077SJoerg Wunsch 		case RTCFDT_288M:
75886a727d9SJoerg Wunsch 		case RTCFDT_288M_1:
7596a0e6f42SRodney W. Grimes 			printf("2.88MB 3.5in - 1.44MB mode\n");
760290dd077SJoerg Wunsch 			fd->type = FD_1440;
761290dd077SJoerg Wunsch 			break;
7627ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
7636a0e6f42SRodney W. Grimes 			printf("360KB 5.25in\n");
764b99f0a4aSAndrew Moore 			fd->type = FD_360;
7657ca0641bSAndrey A. Chernov 			break;
766ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
7676a0e6f42SRodney W. Grimes 			printf("720KB 3.5in\n");
768b99f0a4aSAndrew Moore 			fd->type = FD_720;
769ed2fa05eSAndrey A. Chernov 			break;
7707ca0641bSAndrey A. Chernov 		default:
7716a0e6f42SRodney W. Grimes 			printf("unknown\n");
772b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
77321519754SBruce Evans 			continue;
7745b81b6b3SRodney W. Grimes 		}
775999422d7SJulian Elischer #ifdef DEVFS
7763e425b96SJulian Elischer #ifdef SLICE
7773e425b96SJulian Elischer 		sprintf(namebuf,"fd%d",fdu);
7783e425b96SJulian Elischer 		fd->subdevs[0].minor = 0;
7793e425b96SJulian Elischer 		fd->subdevs[0].drive = fd;
7803e425b96SJulian Elischer 		fd->subdevs[0].limit.blksize =
7813e425b96SJulian Elischer 			128 << (fd_types[fd->type - 1].secsize);
7823e425b96SJulian Elischer 		fd->subdevs[0].limit.slicesize =
7833e425b96SJulian Elischer 			fd_types[fd->type - 1].size
7843e425b96SJulian Elischer 				* fd->subdevs[0].limit.blksize;
7853e425b96SJulian Elischer 		fd->ft = fd_types + (fd->type - 1); /* default value */
7863e425b96SJulian Elischer 		sl_make_slice(&slicetype,
7873e425b96SJulian Elischer 			&fd->subdevs[0],
7883e425b96SJulian Elischer 			&fd->subdevs[0].limit,
7893e425b96SJulian Elischer 		 	&fd->subdevs[0].slice,
7903e425b96SJulian Elischer 			NULL,
7913e425b96SJulian Elischer 			namebuf);
7923e425b96SJulian Elischer 		/* Allow full probing */
7933e425b96SJulian Elischer 		fd->subdevs[0].slice->probeinfo.typespecific = NULL;
7943e425b96SJulian Elischer 		fd->subdevs[0].slice->probeinfo.type = NULL;
7953e425b96SJulian Elischer 
7963e425b96SJulian Elischer 		fd->ich.ich_func = fdsinit;
7973e425b96SJulian Elischer 		fd->ich.ich_arg = &fd->subdevs[0];
7983e425b96SJulian Elischer 		config_intrhook_establish(&fd->ich);
7993e425b96SJulian Elischer #else	/* SLICE */
80021519754SBruce Evans 		mynor = fdu << 6;
80121519754SBruce Evans 		fd->bdevs[0] = devfs_add_devswf(&fd_bdevsw, mynor, DV_BLK,
802f85120acSBruce Evans 						UID_ROOT, GID_OPERATOR, 0640,
80321519754SBruce Evans 						"fd%d", fdu);
80421519754SBruce Evans 		fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR,
805f85120acSBruce Evans 						UID_ROOT, GID_OPERATOR, 0640,
80621519754SBruce Evans 						"rfd%d", fdu);
8073e425b96SJulian Elischer #endif	/* SLICE */
80821519754SBruce Evans 		for (i = 1; i < 1 + NUMDENS; i++) {
80921519754SBruce Evans 			/*
81021519754SBruce Evans 			 * XXX this and the lookup in Fdopen() should be
81121519754SBruce Evans 			 * data driven.
81221519754SBruce Evans 			 */
81321519754SBruce Evans 			switch (fd->type) {
81421519754SBruce Evans 			case FD_360:
81521519754SBruce Evans 				if (i != FD_360)
81621519754SBruce Evans 					continue;
81721519754SBruce Evans 				break;
81821519754SBruce Evans 			case FD_720:
81921519754SBruce Evans 				if (i != FD_720 && i != FD_800 && i != FD_820)
82021519754SBruce Evans 					continue;
82121519754SBruce Evans 				break;
82221519754SBruce Evans 			case FD_1200:
82321519754SBruce Evans 				if (i != FD_360 && i != FD_720 && i != FD_800
82421519754SBruce Evans 				    && i != FD_820 && i != FD_1200
82521519754SBruce Evans 				    && i != FD_1440 && i != FD_1480)
82621519754SBruce Evans 					continue;
82721519754SBruce Evans 				break;
82821519754SBruce Evans 			case FD_1440:
82921519754SBruce Evans 				if (i != FD_720 && i != FD_800 && i != FD_820
83021519754SBruce Evans 				    && i != FD_1200 && i != FD_1440
83121519754SBruce Evans 				    && i != FD_1480 && i != FD_1720)
83221519754SBruce Evans 					continue;
83321519754SBruce Evans 				break;
83421519754SBruce Evans 			}
83521519754SBruce Evans 			typesize = fd_types[i - 1].size / 2;
83621519754SBruce Evans 			/*
83721519754SBruce Evans 			 * XXX all these conversions give bloated code and
83821519754SBruce Evans 			 * confusing names.
83921519754SBruce Evans 			 */
84021519754SBruce Evans 			if (typesize == 1476)
84121519754SBruce Evans 				typesize = 1480;
84221519754SBruce Evans 			if (typesize == 1722)
84321519754SBruce Evans 				typesize = 1720;
8443e425b96SJulian Elischer #ifdef SLICE
8453e425b96SJulian Elischer 			sprintf(namebuf,"fd%d.%d",fdu,typesize);
8463e425b96SJulian Elischer 			fd->subdevs[i].minor = i;
8473e425b96SJulian Elischer 			fd->subdevs[i].drive = fd;
8483e425b96SJulian Elischer 			fd->subdevs[i].limit.blksize =
8493e425b96SJulian Elischer 				128 << (fd_types[i - 1].secsize);
8503e425b96SJulian Elischer 			fd->subdevs[i].limit.slicesize =
8513e425b96SJulian Elischer 				fd_types[i - 1].size
8523e425b96SJulian Elischer 					* fd->subdevs[i].limit.blksize;
8533e425b96SJulian Elischer 			sl_make_slice(&slicetype,
8543e425b96SJulian Elischer 				&fd->subdevs[i],
8553e425b96SJulian Elischer 				&fd->subdevs[i].limit,
8563e425b96SJulian Elischer 			 	&fd->subdevs[i].slice,
8573e425b96SJulian Elischer 				NULL,
8583e425b96SJulian Elischer 				namebuf);
8593e425b96SJulian Elischer 			/* Allow full probing */
8603e425b96SJulian Elischer 			fd->subdevs[i].slice->probeinfo.typespecific = NULL;
8613e425b96SJulian Elischer 			fd->subdevs[i].slice->probeinfo.type = NO_SUBPART;
8623e425b96SJulian Elischer 		}
8633e425b96SJulian Elischer #else	/* SLICE */
8643e425b96SJulian Elischer 			typemynor = mynor | i;
86521519754SBruce Evans 			fd->bdevs[i] =
86621519754SBruce Evans 				devfs_add_devswf(&fd_bdevsw, typemynor, DV_BLK,
86721519754SBruce Evans 						 UID_ROOT, GID_OPERATOR, 0640,
86821519754SBruce Evans 						 "fd%d.%d", fdu, typesize);
86921519754SBruce Evans 			fd->cdevs[i] =
87021519754SBruce Evans 				devfs_add_devswf(&fd_cdevsw, typemynor, DV_CHR,
87121519754SBruce Evans 						 UID_ROOT, GID_OPERATOR, 0640,
87221519754SBruce Evans 						 "rfd%d.%d", fdu, typesize);
87321519754SBruce Evans 		}
8743e425b96SJulian Elischer 
87521519754SBruce Evans 		for (i = 0; i < MAXPARTITIONS; i++) {
8763e425b96SJulian Elischer 			fd->bdevs[1 + NUMDENS + i] = devfs_link(fd->bdevs[0],
87721519754SBruce Evans 					   "fd%d%c", fdu, 'a' + i);
87821519754SBruce Evans 			fd->cdevs[1 + NUMDENS + i] =
87921519754SBruce Evans 				devfs_link(fd->cdevs[0],
88021519754SBruce Evans 					   "rfd%d%c", fdu, 'a' + i);
88121519754SBruce Evans 		}
8823e425b96SJulian Elischer #endif /* SLICE */
883999422d7SJulian Elischer #endif /* DEVFS */
884d4319080SBruce Evans #ifdef notyet
88592200632SGarrett Wollman 		if (dk_ndrive < DK_NDRIVE) {
88692200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
8876a0e6f42SRodney W. Grimes 			fd->dkunit = dk_ndrive++;
888671e2ceeSBruce Evans 			/*
889671e2ceeSBruce Evans 			 * XXX assume rate is FDC_500KBPS.
890671e2ceeSBruce Evans 			 */
891671e2ceeSBruce Evans 			dk_wpms[dk_ndrive] = 500000 / 8 / 2;
89292200632SGarrett Wollman 		} else {
89392200632SGarrett Wollman 			fd->dkunit = -1;
89492200632SGarrett Wollman 		}
895d4319080SBruce Evans #endif
8965b81b6b3SRodney W. Grimes 	}
8975b81b6b3SRodney W. Grimes 
8983a2f7427SDavid Greenman 	return (1);
8995b81b6b3SRodney W. Grimes }
9005b81b6b3SRodney W. Grimes 
9013e425b96SJulian Elischer 
9023e425b96SJulian Elischer #ifdef	SLICE
9033e425b96SJulian Elischer 
9043e425b96SJulian Elischer static void
9053e425b96SJulian Elischer fdsinit(void *arg)
9063e425b96SJulian Elischer {
9073e425b96SJulian Elischer 	struct subdev *sd = arg;
9083e425b96SJulian Elischer 	sh_p	tp;
9093e425b96SJulian Elischer 
9103e425b96SJulian Elischer 	if ((tp = slice_probeall(sd->slice)) != NULL) {
9113e425b96SJulian Elischer 		(*tp->constructor)(sd->slice);
9123e425b96SJulian Elischer 	}
9133e425b96SJulian Elischer 	config_intrhook_disestablish(&sd->drive->ich);
9143e425b96SJulian Elischer }
9153e425b96SJulian Elischer #endif	/* SLICE */
9163e425b96SJulian Elischer 
9175b81b6b3SRodney W. Grimes /****************************************************************************/
9185b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
9195e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
9205b81b6b3SRodney W. Grimes /****************************************************************************/
9213a2f7427SDavid Greenman static void
922dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
9235b81b6b3SRodney W. Grimes {
9243a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
9253a2f7427SDavid Greenman 	int needspecify = 0;
9263a2f7427SDavid Greenman 
9273a2f7427SDavid Greenman 	if(turnon) {
9283a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
9293a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
9303a2f7427SDavid Greenman 	} else
9313a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
9323a2f7427SDavid Greenman 
9335e235068SJordan K. Hubbard 	if(!turnon
9345e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
9355e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
9365e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
9375e235068SJordan K. Hubbard 	else {
9383a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
9393a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
9403a2f7427SDavid Greenman 			needspecify = 1;
9413a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
9425b81b6b3SRodney W. Grimes 	}
9435b81b6b3SRodney W. Grimes 
9443a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
9453a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
9463a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
9473a2f7427SDavid Greenman 
9483a2f7427SDavid Greenman 	if(needspecify) {
949dc8603e3SJoerg Wunsch 		/*
950dc5df763SJoerg Wunsch 		 * XXX
951dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
952dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
953dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
954dc8603e3SJoerg Wunsch 		 */
955dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
956dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
957dc5df763SJoerg Wunsch 			     0);
958d66c374fSTor Egge 		if (fdc_data[fdcu].flags & FDC_HAS_FIFO)
959d66c374fSTor Egge 			(void) enable_fifo(&fdc_data[fdcu]);
9603a2f7427SDavid Greenman 	}
9613a2f7427SDavid Greenman }
9623a2f7427SDavid Greenman 
963381fe1aaSGarrett Wollman static void
964d0917939SPaul Richards fd_turnoff(void *arg1)
9655b81b6b3SRodney W. Grimes {
966381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
967f5f7ba03SJordan K. Hubbard 	int	s;
9685b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
969dc16046fSJoerg Wunsch 
970dc16046fSJoerg Wunsch 	TRACE1("[fd%d: turnoff]", fdu);
9718335c1b8SBruce Evans 
9728335c1b8SBruce Evans 	/*
9738335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
9748335c1b8SBruce Evans 	 * XXX shouldn't even schedule turnoff until drive is inactive
9758335c1b8SBruce Evans 	 * and nothing is queued on it.
9768335c1b8SBruce Evans 	 */
9778335c1b8SBruce Evans 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fdu) {
97802a19910SJustin T. Gibbs 		fd->toffhandle = timeout(fd_turnoff, arg1, 4 * hz);
9798335c1b8SBruce Evans 		return;
9808335c1b8SBruce Evans 	}
9818335c1b8SBruce Evans 
982f5f7ba03SJordan K. Hubbard 	s = splbio();
9835b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
9843a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
985f5f7ba03SJordan K. Hubbard 	splx(s);
9865b81b6b3SRodney W. Grimes }
9875b81b6b3SRodney W. Grimes 
9883a2f7427SDavid Greenman static void
989d0917939SPaul Richards fd_motor_on(void *arg1)
9905b81b6b3SRodney W. Grimes {
991381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
992f5f7ba03SJordan K. Hubbard 	int	s;
993f5f7ba03SJordan K. Hubbard 
9945b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
995f5f7ba03SJordan K. Hubbard 	s = splbio();
9965b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
9975b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
9985b81b6b3SRodney W. Grimes 	{
999f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
10005b81b6b3SRodney W. Grimes 	}
1001f5f7ba03SJordan K. Hubbard 	splx(s);
10025b81b6b3SRodney W. Grimes }
10035b81b6b3SRodney W. Grimes 
10043a2f7427SDavid Greenman static void
1005dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
10065b81b6b3SRodney W. Grimes {
10075b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
10085b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
10095b81b6b3SRodney W. Grimes 	{
10103a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
10113a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
10125e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
10135b81b6b3SRodney W. Grimes 	}
10145b81b6b3SRodney W. Grimes }
10155b81b6b3SRodney W. Grimes 
1016381fe1aaSGarrett Wollman static void
1017dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
10185b81b6b3SRodney W. Grimes {
10193a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
10203a2f7427SDavid Greenman 
10213a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
10223a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
10233a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
10243a2f7427SDavid Greenman 	DELAY(100);
10253a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
10263a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
10273a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
10283a2f7427SDavid Greenman 	DELAY(100);
10293a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
10303a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
10313a2f7427SDavid Greenman 
1032dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
1033dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
1034dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1035dc5df763SJoerg Wunsch 		     0);
1036d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1037d66c374fSTor Egge 		(void) enable_fifo(fdc);
10385b81b6b3SRodney W. Grimes }
10395b81b6b3SRodney W. Grimes 
10405b81b6b3SRodney W. Grimes /****************************************************************************/
10415b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
10425b81b6b3SRodney W. Grimes /****************************************************************************/
10435b81b6b3SRodney W. Grimes int
1044dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
10455b81b6b3SRodney W. Grimes {
10465b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
10475b81b6b3SRodney W. Grimes 	int i, j = 100000;
10483a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
10495b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1050dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
10516a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
10525b81b6b3SRodney W. Grimes 	if (j <= 0)
105316b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
1054d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
10553a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
10563a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
10575b81b6b3SRodney W. Grimes 	return(i);
1058d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
10593a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
1060d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
10615b81b6b3SRodney W. Grimes }
10625b81b6b3SRodney W. Grimes 
1063dc5df763SJoerg Wunsch /*
1064dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
1065dc5df763SJoerg Wunsch  */
1066b5e8ce9fSBruce Evans static int
1067dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
1068dc5df763SJoerg Wunsch {
1069dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
1070dc5df763SJoerg Wunsch 	int i, j = 100000;
1071dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
1072dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1073dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
10746a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
1075dc5df763SJoerg Wunsch 	if (j <= 0)
107616b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
1077d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1078dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
1079dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1080dc5df763SJoerg Wunsch 	*ptr = i;
1081dc5df763SJoerg Wunsch 	return 0;
1082d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1083dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
1084dc5df763SJoerg Wunsch 	if (ptr)
1085dc5df763SJoerg Wunsch 		*ptr = i;
1086dc5df763SJoerg Wunsch 	return 0;
1087d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1088dc5df763SJoerg Wunsch }
1089dc5df763SJoerg Wunsch 
1090dc5df763SJoerg Wunsch int
1091dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
10925b81b6b3SRodney W. Grimes {
10935b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
10943b3837dbSRodney W. Grimes 	int i;
10955b81b6b3SRodney W. Grimes 
10963b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
10973b3837dbSRodney W. Grimes 	i = 100000;
10983a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
10996a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
11003b3837dbSRodney W. Grimes 
11013b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
11023b3837dbSRodney W. Grimes 	i = 100000;
11033a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
110416b04b6aSJoerg Wunsch 	if (i <= 0)
110516b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0);
11063b3837dbSRodney W. Grimes 
11073b3837dbSRodney W. Grimes 	/* Send the command and return */
11083a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
11093a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
11105b81b6b3SRodney W. Grimes 	return (0);
11115b81b6b3SRodney W. Grimes }
11125b81b6b3SRodney W. Grimes 
11135b81b6b3SRodney W. Grimes /****************************************************************************/
11145b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
11155b81b6b3SRodney W. Grimes /****************************************************************************/
1116381fe1aaSGarrett Wollman int
1117671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
11185b81b6b3SRodney W. Grimes {
11195b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
112020a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
1121b99f0a4aSAndrew Moore 	fdc_p	fdc;
11225b81b6b3SRodney W. Grimes 
1123b99f0a4aSAndrew Moore #if NFT > 0
1124b99f0a4aSAndrew Moore 	/* check for a tape open */
1125b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
1126b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
1127b99f0a4aSAndrew Moore #endif
11285b81b6b3SRodney W. Grimes 	/* check bounds */
1129b99f0a4aSAndrew Moore 	if (fdu >= NFD)
1130b99f0a4aSAndrew Moore 		return(ENXIO);
1131b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
1132b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
1133b99f0a4aSAndrew Moore 		return(ENXIO);
1134b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1135b99f0a4aSAndrew Moore 		return(ENXIO);
11367ca0641bSAndrey A. Chernov 	if (type == 0)
11377ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
11387ca0641bSAndrey A. Chernov 	else {
11393e425b96SJulian Elischer 		/*
11403e425b96SJulian Elischer 		 * For each type of basic drive, make sure we are trying
11413e425b96SJulian Elischer 		 * to open a type it can do,
11423e425b96SJulian Elischer 		 */
11437ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
1144fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
11457ca0641bSAndrey A. Chernov 			case FD_360:
11467ca0641bSAndrey A. Chernov 				return(ENXIO);
1147ed2fa05eSAndrey A. Chernov 			case FD_720:
1148b39c878eSAndrey A. Chernov 				if (   type != FD_820
1149b39c878eSAndrey A. Chernov 				    && type != FD_800
1150ed2fa05eSAndrey A. Chernov 				   )
1151ed2fa05eSAndrey A. Chernov 					return(ENXIO);
1152ed2fa05eSAndrey A. Chernov 				break;
11537ca0641bSAndrey A. Chernov 			case FD_1200:
1154b39c878eSAndrey A. Chernov 				switch (type) {
1155b39c878eSAndrey A. Chernov 				case FD_1480:
1156b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1157fa4700b4SAndrey A. Chernov 					break;
11587ca0641bSAndrey A. Chernov 				case FD_1440:
1159b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1160b39c878eSAndrey A. Chernov 					break;
1161b39c878eSAndrey A. Chernov 				case FD_820:
1162b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1163b39c878eSAndrey A. Chernov 					break;
1164b39c878eSAndrey A. Chernov 				case FD_800:
1165b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1166b39c878eSAndrey A. Chernov 					break;
1167b39c878eSAndrey A. Chernov 				case FD_720:
1168b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1169b39c878eSAndrey A. Chernov 					break;
1170b39c878eSAndrey A. Chernov 				case FD_360:
1171b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1172b39c878eSAndrey A. Chernov 					break;
1173b39c878eSAndrey A. Chernov 				default:
1174b39c878eSAndrey A. Chernov 					return(ENXIO);
1175b39c878eSAndrey A. Chernov 				}
1176b39c878eSAndrey A. Chernov 				break;
1177b39c878eSAndrey A. Chernov 			case FD_1440:
1178b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1179b39c878eSAndrey A. Chernov 				    && type != FD_1480
1180ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1181b39c878eSAndrey A. Chernov 				    && type != FD_820
1182b39c878eSAndrey A. Chernov 				    && type != FD_800
1183b39c878eSAndrey A. Chernov 				    && type != FD_720
11847ca0641bSAndrey A. Chernov 				    )
1185dffff499SAndrey A. Chernov 					return(ENXIO);
1186fa4700b4SAndrey A. Chernov 				break;
11877ca0641bSAndrey A. Chernov 			}
11887ca0641bSAndrey A. Chernov 		}
1189fa4700b4SAndrey A. Chernov 	}
1190b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
11915b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
11925b81b6b3SRodney W. Grimes 
11935b81b6b3SRodney W. Grimes 	return 0;
11945b81b6b3SRodney W. Grimes }
11955b81b6b3SRodney W. Grimes 
1196381fe1aaSGarrett Wollman int
1197671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
11985b81b6b3SRodney W. Grimes {
11995b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
1200b99f0a4aSAndrew Moore 
1201b99f0a4aSAndrew Moore #if NFT > 0
12023a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
12033a2f7427SDavid Greenman 
1204b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
12053a2f7427SDavid Greenman 		return ftclose(dev, flags);
1206b99f0a4aSAndrew Moore #endif
12075b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
12083a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
1209dc16046fSJoerg Wunsch 
12105b81b6b3SRodney W. Grimes 	return(0);
12115b81b6b3SRodney W. Grimes }
12125b81b6b3SRodney W. Grimes 
12135b81b6b3SRodney W. Grimes 
12143a2f7427SDavid Greenman /****************************************************************************/
12153a2f7427SDavid Greenman /*                               fdstrategy                                 */
12163a2f7427SDavid Greenman /****************************************************************************/
12173a2f7427SDavid Greenman void
12183a2f7427SDavid Greenman fdstrategy(struct buf *bp)
12193a2f7427SDavid Greenman {
1220bb6382faSJoerg Wunsch 	unsigned nblocks, blknum, cando;
12213a2f7427SDavid Greenman  	int	s;
12223a2f7427SDavid Greenman  	fdcu_t	fdcu;
12233a2f7427SDavid Greenman  	fdu_t	fdu;
12243a2f7427SDavid Greenman  	fdc_p	fdc;
12253a2f7427SDavid Greenman  	fd_p	fd;
12263a2f7427SDavid Greenman 	size_t	fdblk;
12273a2f7427SDavid Greenman 
12283a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
12293a2f7427SDavid Greenman 	fd = &fd_data[fdu];
12303a2f7427SDavid Greenman 	fdc = fd->fdc;
12313a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
12323a2f7427SDavid Greenman 
12333a2f7427SDavid Greenman #if NFT > 0
12343a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
12353a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
1236d3628763SRodney W. Grimes 		bp->b_error = ENODEV;
12373a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
12383a2f7427SDavid Greenman 		goto bad;
12393a2f7427SDavid Greenman 	}
12403a2f7427SDavid Greenman 	/* check for controller already busy with tape */
12413a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
12423a2f7427SDavid Greenman 		bp->b_error = EBUSY;
12433a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
12443a2f7427SDavid Greenman 		goto bad;
12453a2f7427SDavid Greenman 	}
12463a2f7427SDavid Greenman #endif
1247d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
12483a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
12493a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1250dc5df763SJoerg Wunsch 			printf(
12516a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1252702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
12533a2f7427SDavid Greenman 			bp->b_error = EINVAL;
12543a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
12553a2f7427SDavid Greenman 			goto bad;
12563a2f7427SDavid Greenman 		}
12573a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
12583a2f7427SDavid Greenman 			bp->b_error = EINVAL;
12593a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
12603a2f7427SDavid Greenman 			goto bad;
12613a2f7427SDavid Greenman 		}
12623a2f7427SDavid Greenman 	}
12633a2f7427SDavid Greenman 
12643a2f7427SDavid Greenman 	/*
12653a2f7427SDavid Greenman 	 * Set up block calculations.
12663a2f7427SDavid Greenman 	 */
1267bb6382faSJoerg Wunsch 	if (bp->b_blkno > 20000000) {
1268bb6382faSJoerg Wunsch 		/*
1269bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1270bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1271bb6382faSJoerg Wunsch 		 */
1272bb6382faSJoerg Wunsch 		bp->b_error = EINVAL;
12733a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
12743a2f7427SDavid Greenman 		goto bad;
12753a2f7427SDavid Greenman 	}
1276bb6382faSJoerg Wunsch 	blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1277bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1278bb6382faSJoerg Wunsch 	bp->b_resid = 0;
1279bb6382faSJoerg Wunsch 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1280bb6382faSJoerg Wunsch 		if (blknum <= nblocks) {
1281bb6382faSJoerg Wunsch 			cando = (nblocks - blknum) * fdblk;
1282bb6382faSJoerg Wunsch 			bp->b_resid = bp->b_bcount - cando;
1283bb6382faSJoerg Wunsch 			if (cando == 0)
1284bb6382faSJoerg Wunsch 				goto bad;	/* not actually bad but EOF */
1285bb6382faSJoerg Wunsch 		} else {
1286bb6382faSJoerg Wunsch 			bp->b_error = EINVAL;
1287bb6382faSJoerg Wunsch 			bp->b_flags |= B_ERROR;
1288bb6382faSJoerg Wunsch 			goto bad;
1289bb6382faSJoerg Wunsch 		}
1290bb6382faSJoerg Wunsch 	}
129127513ca7SBruce Evans  	bp->b_pblkno = bp->b_blkno;
12923a2f7427SDavid Greenman 	s = splbio();
129302a19910SJustin T. Gibbs 	bufqdisksort(&fdc->head, bp);
129402a19910SJustin T. Gibbs 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); /* a good idea */
12953a2f7427SDavid Greenman 	fdstart(fdcu);
12963a2f7427SDavid Greenman 	splx(s);
12973a2f7427SDavid Greenman 	return;
12983a2f7427SDavid Greenman 
12993a2f7427SDavid Greenman bad:
13003a2f7427SDavid Greenman 	biodone(bp);
13013a2f7427SDavid Greenman }
13023a2f7427SDavid Greenman 
13033e425b96SJulian Elischer #ifdef	SLICE
13043e425b96SJulian Elischer /****************************************************************************/
13053e425b96SJulian Elischer /*                               fdsIOreq                                   */
13063e425b96SJulian Elischer /****************************************************************************/
13073e425b96SJulian Elischer static void
13083e425b96SJulian Elischer fdsIOreq(void *private ,struct buf *bp)
13093e425b96SJulian Elischer {
13103e425b96SJulian Elischer 	unsigned nblocks, blknum, cando;
13113e425b96SJulian Elischer  	int	s;
13123e425b96SJulian Elischer  	fdcu_t	fdcu;
13133e425b96SJulian Elischer  	fdu_t	fdu;
13143e425b96SJulian Elischer  	fdc_p	fdc;
13153e425b96SJulian Elischer  	fd_p	fd;
13163e425b96SJulian Elischer 	size_t	fdblk;
13173e425b96SJulian Elischer 	struct subdev *sd;
13183e425b96SJulian Elischer 
13193e425b96SJulian Elischer 	sd = private;
13203e425b96SJulian Elischer 	fd = sd->drive;
13213e425b96SJulian Elischer  	fdu = fd->unit;
13223e425b96SJulian Elischer 	fdc = fd->fdc;
13233e425b96SJulian Elischer 	fdcu = fdc->fdcu;
13243e425b96SJulian Elischer 
13253e425b96SJulian Elischer 	/* check for controller already busy with tape */
13263e425b96SJulian Elischer 	if (fdc->flags & FDC_TAPE_BUSY) {
13273e425b96SJulian Elischer 		bp->b_error = EBUSY;
13283e425b96SJulian Elischer 		bp->b_flags |= B_ERROR;
13293e425b96SJulian Elischer 		goto bad;
13303e425b96SJulian Elischer 	}
13313e425b96SJulian Elischer 	bp->b_driver1 = sd; /* squirrel away which device.. */
13323e425b96SJulian Elischer 	bp->b_resid = 0;
13333e425b96SJulian Elischer 	s = splbio();
13343e425b96SJulian Elischer 	bufqdisksort(&fdc->head, bp);
13353e425b96SJulian Elischer 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); /* a good idea */
13363e425b96SJulian Elischer 	fdstart(fdcu);
13373e425b96SJulian Elischer 	splx(s);
13383e425b96SJulian Elischer 	return;
13393e425b96SJulian Elischer 
13403e425b96SJulian Elischer bad:
13413e425b96SJulian Elischer 	biodone(bp);
13423e425b96SJulian Elischer 	return;
13433e425b96SJulian Elischer }
13443e425b96SJulian Elischer #endif	/* SLICE */
13453e425b96SJulian Elischer 
13465b81b6b3SRodney W. Grimes /***************************************************************\
13475b81b6b3SRodney W. Grimes *				fdstart				*
13485b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
13495b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
13505b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
13515b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
13525b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
13535b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
13545b81b6b3SRodney W. Grimes \***************************************************************/
1355381fe1aaSGarrett Wollman static void
1356dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
13575b81b6b3SRodney W. Grimes {
13585b81b6b3SRodney W. Grimes 	int s;
13595b81b6b3SRodney W. Grimes 
13605b81b6b3SRodney W. Grimes 	s = splbio();
13615b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
13625b81b6b3SRodney W. Grimes 	{
13635b81b6b3SRodney W. Grimes 		fdintr(fdcu);
13645b81b6b3SRodney W. Grimes 	}
13655b81b6b3SRodney W. Grimes 	splx(s);
13665b81b6b3SRodney W. Grimes }
13675b81b6b3SRodney W. Grimes 
1368381fe1aaSGarrett Wollman static void
1369d0917939SPaul Richards fd_timeout(void *arg1)
13705b81b6b3SRodney W. Grimes {
1371381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
13725b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
13733a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
137417542807SPoul-Henning Kamp 	struct buf *bp;
1375f5f7ba03SJordan K. Hubbard 	int s;
13765b81b6b3SRodney W. Grimes 
137702a19910SJustin T. Gibbs 	bp = bufq_first(&fdc_data[fdcu].head);
13785b81b6b3SRodney W. Grimes 
13793a2f7427SDavid Greenman 	/*
13803a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
13813a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
13823a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
13833a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
13843a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
13853a2f7427SDavid Greenman 	 */
13865b81b6b3SRodney W. Grimes 
13873a2f7427SDavid Greenman 	s = splbio();
13883a2f7427SDavid Greenman 
13893a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
13903a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
13913a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
13923a2f7427SDavid Greenman 	{
13933a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
13943a2f7427SDavid Greenman 		/* yup, it is; kill it now */
13953a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
13963a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
13973a2f7427SDavid Greenman 	}
13985b81b6b3SRodney W. Grimes 
13995b81b6b3SRodney W. Grimes 	if (bp)
14005b81b6b3SRodney W. Grimes 	{
14015b81b6b3SRodney W. Grimes 		retrier(fdcu);
14023a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
14035b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
14045b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
14055b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
14065b81b6b3SRodney W. Grimes 	}
14075b81b6b3SRodney W. Grimes 	else
14085b81b6b3SRodney W. Grimes 	{
14095b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
14105b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
14115b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
14125b81b6b3SRodney W. Grimes 	}
1413f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1414f5f7ba03SJordan K. Hubbard 	splx(s);
14155b81b6b3SRodney W. Grimes }
14165b81b6b3SRodney W. Grimes 
14175b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1418381fe1aaSGarrett Wollman static void
1419d0917939SPaul Richards fd_pseudointr(void *arg1)
14205b81b6b3SRodney W. Grimes {
1421381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
14225b81b6b3SRodney W. Grimes 	int	s;
14233a2f7427SDavid Greenman 
14245b81b6b3SRodney W. Grimes 	s = splbio();
14255b81b6b3SRodney W. Grimes 	fdintr(fdcu);
14265b81b6b3SRodney W. Grimes 	splx(s);
14275b81b6b3SRodney W. Grimes }
14285b81b6b3SRodney W. Grimes 
14295b81b6b3SRodney W. Grimes /***********************************************************************\
14305b81b6b3SRodney W. Grimes *                                 fdintr				*
14315b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
14325b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
14335b81b6b3SRodney W. Grimes \***********************************************************************/
1434381fe1aaSGarrett Wollman void
1435381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
14365b81b6b3SRodney W. Grimes {
14375b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1438b99f0a4aSAndrew Moore #if NFT > 0
1439b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1440b99f0a4aSAndrew Moore 
1441b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1442b99f0a4aSAndrew Moore 		(ftintr(fdu));
1443b99f0a4aSAndrew Moore 	else
1444b99f0a4aSAndrew Moore #endif
1445381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1446381fe1aaSGarrett Wollman 			;
14475b81b6b3SRodney W. Grimes }
14485b81b6b3SRodney W. Grimes 
14495b81b6b3SRodney W. Grimes /***********************************************************************\
14505b81b6b3SRodney W. Grimes * The controller state machine.						*
14515b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
14525b81b6b3SRodney W. Grimes \***********************************************************************/
14533a2f7427SDavid Greenman static int
1454dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
14555b81b6b3SRodney W. Grimes {
14563e425b96SJulian Elischer 	struct subdev *sd;
14574ccc87c5SPoul-Henning Kamp 	int read, format, head, sec = 0, sectrac, st0, cyl, st3;
1458bb6382faSJoerg Wunsch 	unsigned blknum = 0, b_cylinder = 0;
14595b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
14605b81b6b3SRodney W. Grimes 	fd_p fd;
146117542807SPoul-Henning Kamp 	register struct buf *bp;
1462b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
14633a2f7427SDavid Greenman 	size_t fdblk;
14645b81b6b3SRodney W. Grimes 
146502a19910SJustin T. Gibbs 	bp = bufq_first(&fdc->head);
146617542807SPoul-Henning Kamp 	if(!bp) {
14675b81b6b3SRodney W. Grimes 		/***********************************************\
14685b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
14695b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
14705b81b6b3SRodney W. Grimes 		\***********************************************/
14715b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
14725b81b6b3SRodney W. Grimes 		if(fdc->fd)
14735b81b6b3SRodney W. Grimes 		{
14746a0e6f42SRodney W. Grimes 			printf("fd%d: unexpected valid fd pointer\n",
14753a2f7427SDavid Greenman 			       fdc->fdu);
14765b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
14775b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
14785b81b6b3SRodney W. Grimes 		}
14795b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
14805b81b6b3SRodney W. Grimes  		return(0);
14815b81b6b3SRodney W. Grimes 	}
14823e425b96SJulian Elischer #ifdef	SLICE
14833e425b96SJulian Elischer 	sd = bp->b_driver1;
14843e425b96SJulian Elischer 	fd = sd->drive;
14853e425b96SJulian Elischer  	fdu = fd->unit;
14863e425b96SJulian Elischer #else
14875b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
14885b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
14893e425b96SJulian Elischer #endif
14903a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
14915b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
14925b81b6b3SRodney W. Grimes 	{
14936a0e6f42SRodney W. Grimes 		printf("fd%d: confused fd pointers\n", fdu);
14945b81b6b3SRodney W. Grimes 	}
14955b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1496b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1497bb6382faSJoerg Wunsch 	if(format) {
1498ab3f7469SPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->b_data;
1499bb6382faSJoerg Wunsch 		fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1500bb6382faSJoerg Wunsch 			- (char *)finfo;
1501bb6382faSJoerg Wunsch 	}
1502bb6382faSJoerg Wunsch 	if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
15033e425b96SJulian Elischer 		blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
1504bb6382faSJoerg Wunsch 			fd->skip/fdblk;
1505bb6382faSJoerg Wunsch 		b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1506bb6382faSJoerg Wunsch 	}
15075b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
15085b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
15095b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
151002a19910SJustin T. Gibbs 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle);
151127651b7fSJustin T. Gibbs 	fd->toffhandle = timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
15125b81b6b3SRodney W. Grimes 	switch (fdc->state)
15135b81b6b3SRodney W. Grimes 	{
15145b81b6b3SRodney W. Grimes 	case DEVIDLE:
15155b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
15165b81b6b3SRodney W. Grimes 		fdc->retry = 0;
15175b81b6b3SRodney W. Grimes 		fd->skip = 0;
15185b81b6b3SRodney W. Grimes 		fdc->fd = fd;
15195b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
15203a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
15213a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
15225b81b6b3SRodney W. Grimes 		/*******************************************************\
15235b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
1524dc733423SDag-Erling Smørgrav 		* it will start up in its own good time		*
15255b81b6b3SRodney W. Grimes 		\*******************************************************/
15265b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
15275b81b6b3SRodney W. Grimes 		{
15285b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
15295b81b6b3SRodney W. Grimes 			return(0); /* come back later */
15305b81b6b3SRodney W. Grimes 		}
15315b81b6b3SRodney W. Grimes 		/*******************************************************\
15325b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
15335b81b6b3SRodney W. Grimes 		\*******************************************************/
15345b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
15355b81b6b3SRodney W. Grimes 		{
15365b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
15375b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
15385b81b6b3SRodney W. Grimes 			return(0);
15395b81b6b3SRodney W. Grimes 		}
15405b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
15415b81b6b3SRodney W. Grimes 		{
15423a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
15435b81b6b3SRodney W. Grimes 		}
15445e235068SJordan K. Hubbard 		fdc->state = DOSEEK;
15455b81b6b3SRodney W. Grimes 		break;
15465b81b6b3SRodney W. Grimes 	case DOSEEK:
1547bb6382faSJoerg Wunsch 		if (b_cylinder == (unsigned)fd->track)
15485b81b6b3SRodney W. Grimes 		{
15495b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
15505b81b6b3SRodney W. Grimes 			break;
15515b81b6b3SRodney W. Grimes 		}
1552dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1553bb6382faSJoerg Wunsch 			   fd->fdsu, b_cylinder * fd->ft->steptrac,
1554dc5df763SJoerg Wunsch 			   0))
1555dc8603e3SJoerg Wunsch 		{
1556dc8603e3SJoerg Wunsch 			/*
1557dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1558dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1559dc8603e3SJoerg Wunsch 			 */
1560dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1561dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1562dc8603e3SJoerg Wunsch 		}
1563dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
15645b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
15655b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15665b81b6b3SRodney W. Grimes 	case SEEKWAIT:
15675b81b6b3SRodney W. Grimes 		/* allow heads to settle */
156804b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
15695b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
15705b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15715b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
15725b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1573dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
15745b81b6b3SRodney W. Grimes 		{
1575bb6382faSJoerg Wunsch 			int descyl = b_cylinder * fd->ft->steptrac;
15763a2f7427SDavid Greenman 			do {
15773a2f7427SDavid Greenman 				/*
1578dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1579dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1580dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1581dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1582dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1583dc5df763SJoerg Wunsch 				 *
1584dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1585dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1586dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1587dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1588dc5df763SJoerg Wunsch 				 * real interrupt condition.
1589dc5df763SJoerg Wunsch 				 *
1590dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1591dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1592dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1593dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1594dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1595dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1596dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
15973a2f7427SDavid Greenman 				 */
1598dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1599dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1600dc5df763SJoerg Wunsch 					return 0;
1601dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1602dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1603dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
16043a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1605dc5df763SJoerg Wunsch 
16063a2f7427SDavid Greenman 			if (0 == descyl)
16073a2f7427SDavid Greenman 			{
1608dc5df763SJoerg Wunsch 				int failed = 0;
16093a2f7427SDavid Greenman 				/*
16103a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
16113a2f7427SDavid Greenman 				 * really there
16123a2f7427SDavid Greenman 				 */
1613dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1614dc5df763SJoerg Wunsch 					failed = 1;
16153a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
16163a2f7427SDavid Greenman 					printf(
16173a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
16183a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1619dc5df763SJoerg Wunsch 					failed = 1;
1620dc5df763SJoerg Wunsch 				}
1621dc5df763SJoerg Wunsch 
1622dc5df763SJoerg Wunsch 				if (failed)
1623dc5df763SJoerg Wunsch 				{
16243a2f7427SDavid Greenman 					if(fdc->retry < 3)
16253a2f7427SDavid Greenman 						fdc->retry = 3;
16263a2f7427SDavid Greenman 					return(retrier(fdcu));
16273a2f7427SDavid Greenman 				}
16283a2f7427SDavid Greenman 			}
1629dc5df763SJoerg Wunsch 
16305b81b6b3SRodney W. Grimes 			if (cyl != descyl)
16315b81b6b3SRodney W. Grimes 			{
16323a2f7427SDavid Greenman 				printf(
16333a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
16342d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
16355b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
16365b81b6b3SRodney W. Grimes 			}
16375b81b6b3SRodney W. Grimes 		}
16385b81b6b3SRodney W. Grimes 
1639bb6382faSJoerg Wunsch 		fd->track = b_cylinder;
1640ab3f7469SPoul-Henning Kamp 		isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
16413a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
16425b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
16435b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
16445b81b6b3SRodney W. Grimes 		head = sec / sectrac;
16455b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
16463a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
16473a2f7427SDavid Greenman 
16483a2f7427SDavid Greenman 		if(format || !read)
16493a2f7427SDavid Greenman 		{
16503a2f7427SDavid Greenman 			/* make sure the drive is writable */
1651dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1652dc8603e3SJoerg Wunsch 			{
1653dc8603e3SJoerg Wunsch 				/* stuck controller? */
1654dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1655dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1656dc8603e3SJoerg Wunsch 			}
16573a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
16583a2f7427SDavid Greenman 			{
16593a2f7427SDavid Greenman 				/*
16603a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
16613a2f7427SDavid Greenman 				 * in order to force the current operation
16623a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
16633a2f7427SDavid Greenman 				 * error - all error handling is done
16643a2f7427SDavid Greenman 				 * by the retrier()
16653a2f7427SDavid Greenman 				 */
16663a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
16673a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
16683a2f7427SDavid Greenman 				fdc->status[2] = 0;
16693a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
16703a2f7427SDavid Greenman 				fdc->status[4] = head;
16713a2f7427SDavid Greenman 				fdc->status[5] = sec;
16723a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
16733a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
16743a2f7427SDavid Greenman 				return (1);
16753a2f7427SDavid Greenman 			}
16763a2f7427SDavid Greenman 		}
16775b81b6b3SRodney W. Grimes 
1678b39c878eSAndrey A. Chernov 		if(format)
1679b39c878eSAndrey A. Chernov 		{
1680b39c878eSAndrey A. Chernov 			/* formatting */
1681dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1682dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1683dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1684dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1685dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1686dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1687dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1688dc5df763SJoerg Wunsch 				  0))
1689dc8603e3SJoerg Wunsch 			{
1690dc8603e3SJoerg Wunsch 				/* controller fell over */
1691dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1692dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1693dc8603e3SJoerg Wunsch 			}
1694b39c878eSAndrey A. Chernov 		}
1695b39c878eSAndrey A. Chernov 		else
1696b39c878eSAndrey A. Chernov 		{
1697dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1698dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1699dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1700dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1701dc5df763SJoerg Wunsch 				   head,
1702dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1703dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1704dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1705dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1706dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1707dc5df763SJoerg Wunsch 				   0))
17085b81b6b3SRodney W. Grimes 			{
1709dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1710dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1711dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
17125b81b6b3SRodney W. Grimes 			}
1713b39c878eSAndrey A. Chernov 		}
17145b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
171502a19910SJustin T. Gibbs 		fd->tohandle = timeout(fd_timeout, (caddr_t)fdcu, hz);
17165b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
17175b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
171802a19910SJustin T. Gibbs 		untimeout(fd_timeout, (caddr_t)fdcu, fd->tohandle);
1719dc5df763SJoerg Wunsch 
1720dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
17215b81b6b3SRodney W. Grimes 		{
1722dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1723dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1724dc5df763SJoerg Wunsch 			return retrier(fdcu);
17255b81b6b3SRodney W. Grimes   		}
1726dc5df763SJoerg Wunsch 
17273a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1728dc5df763SJoerg Wunsch 
17293a2f7427SDavid Greenman 		/* FALLTHROUGH */
1730dc5df763SJoerg Wunsch 
17313a2f7427SDavid Greenman 	case IOTIMEDOUT:
1732ab3f7469SPoul-Henning Kamp 		isa_dmadone(bp->b_flags, bp->b_data+fd->skip,
17333a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
17343a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
17355b81b6b3SRodney W. Grimes 		{
17363a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
17373a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1738b39c878eSAndrey A. Chernov                                 /*
17393a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
17403a2f7427SDavid Greenman 				 * and didn't release it in time for the
17413a2f7427SDavid Greenman 				 * next FDC transfer.
17423a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
17433a2f7427SDavid Greenman 				 * count. (vak)
1744b39c878eSAndrey A. Chernov                                  */
1745b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1746b39c878eSAndrey A. Chernov                                 return (1);
1747b39c878eSAndrey A. Chernov                         }
17483a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
17493a2f7427SDavid Greenman 				&& fdc->retry < 6)
17503a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
17513a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
17523a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
17533a2f7427SDavid Greenman 				&& fdc->retry < 3)
17543a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
17555b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
17565b81b6b3SRodney W. Grimes 		}
17575b81b6b3SRodney W. Grimes 		/* All OK */
17583a2f7427SDavid Greenman 		fd->skip += fdblk;
1759bb6382faSJoerg Wunsch 		if (!format && fd->skip < bp->b_bcount - bp->b_resid)
17605b81b6b3SRodney W. Grimes 		{
17615b81b6b3SRodney W. Grimes 			/* set up next transfer */
17625b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
17635b81b6b3SRodney W. Grimes 		}
17645b81b6b3SRodney W. Grimes 		else
17655b81b6b3SRodney W. Grimes 		{
17665b81b6b3SRodney W. Grimes 			/* ALL DONE */
17675b81b6b3SRodney W. Grimes 			fd->skip = 0;
176802a19910SJustin T. Gibbs 			bufq_remove(&fdc->head, bp);
17695b81b6b3SRodney W. Grimes 			biodone(bp);
17705b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
17715b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
17725b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
17735b81b6b3SRodney W. Grimes 		}
17745b81b6b3SRodney W. Grimes 		return(1);
17755b81b6b3SRodney W. Grimes 	case RESETCTLR:
17763a2f7427SDavid Greenman 		fdc_reset(fdc);
17775b81b6b3SRodney W. Grimes 		fdc->retry++;
17785b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
17795b81b6b3SRodney W. Grimes 		break;
17805b81b6b3SRodney W. Grimes 	case STARTRECAL:
17810e317d05SJoerg Wunsch 		/* XXX clear the fdc results from the last reset, if any. */
17820e317d05SJoerg Wunsch 		{
17830e317d05SJoerg Wunsch 			int i;
17840e317d05SJoerg Wunsch 			for (i = 0; i < 4; i++)
17850e317d05SJoerg Wunsch 				(void)fd_sense_int(fdc, &st0, &cyl);
17860e317d05SJoerg Wunsch 		}
17870e317d05SJoerg Wunsch 
1788dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1789dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1790dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1791dc8603e3SJoerg Wunsch 		{
1792dc8603e3SJoerg Wunsch 			/* arrgl */
1793dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1794dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1795dc8603e3SJoerg Wunsch 		}
17965b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
17975b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
17985b81b6b3SRodney W. Grimes 	case RECALWAIT:
17995b81b6b3SRodney W. Grimes 		/* allow heads to settle */
180004b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
18015b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
18025b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
18035b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
18043a2f7427SDavid Greenman 		do {
18053a2f7427SDavid Greenman 			/*
1806dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
18073a2f7427SDavid Greenman 			 */
1808dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1809dc5df763SJoerg Wunsch 				return 0;
1810dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1811dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1812dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
18133a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
18143a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
18155b81b6b3SRodney W. Grimes 		{
1816dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1817dc8603e3SJoerg Wunsch 				/*
1818dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1819dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1820dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1821dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1822dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1823dc8603e3SJoerg Wunsch 				 * time in a line
1824dc8603e3SJoerg Wunsch 				 */
1825dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1826dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
18273a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
18285b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
18295b81b6b3SRodney W. Grimes 		}
18305b81b6b3SRodney W. Grimes 		fd->track = 0;
18315b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
18325b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
18335b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
18345b81b6b3SRodney W. Grimes 	case MOTORWAIT:
18355b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
18365b81b6b3SRodney W. Grimes 		{
18375b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
18385b81b6b3SRodney W. Grimes 		}
18395e235068SJordan K. Hubbard 		/*
18405e235068SJordan K. Hubbard 		 * since the controller was off, it has lost its
18415e235068SJordan K. Hubbard 		 * idea about the current track it were; thus,
18425e235068SJordan K. Hubbard 		 * recalibrate the bastard
18435e235068SJordan K. Hubbard 		 */
18445e235068SJordan K. Hubbard 		fdc->state = STARTRECAL;
18455b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
18465b81b6b3SRodney W. Grimes 	default:
1847dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1848dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1849dac0f2dbSJoerg Wunsch 			printf("FDC status :%lx %lx %lx %lx %lx %lx %lx   ",
18505b81b6b3SRodney W. Grimes 			       fdc->status[0],
18515b81b6b3SRodney W. Grimes 			       fdc->status[1],
18525b81b6b3SRodney W. Grimes 			       fdc->status[2],
18535b81b6b3SRodney W. Grimes 			       fdc->status[3],
18545b81b6b3SRodney W. Grimes 			       fdc->status[4],
18555b81b6b3SRodney W. Grimes 			       fdc->status[5],
18565b81b6b3SRodney W. Grimes 			       fdc->status[6] );
18573a2f7427SDavid Greenman 		else
1858dac0f2dbSJoerg Wunsch 			printf("No status available   ");
1859dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1860dac0f2dbSJoerg Wunsch 		{
1861dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
18625b81b6b3SRodney W. Grimes 			return(0);
18635b81b6b3SRodney W. Grimes 		}
1864dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1865dac0f2dbSJoerg Wunsch 		return(0);
1866dac0f2dbSJoerg Wunsch 	}
1867dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
18685b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
18695b81b6b3SRodney W. Grimes }
18705b81b6b3SRodney W. Grimes 
1871aaf08d94SGarrett Wollman static int
1872f5f7ba03SJordan K. Hubbard retrier(fdcu)
1873f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
18745b81b6b3SRodney W. Grimes {
18753e425b96SJulian Elischer 	struct subdev *sd;
18765b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
187717542807SPoul-Henning Kamp 	register struct buf *bp;
18783e425b96SJulian Elischer #ifdef	SLICE
18793e425b96SJulian Elischer 	struct fd_data *fd;
18803e425b96SJulian Elischer 	int fdu;
18813e425b96SJulian Elischer #endif
18825b81b6b3SRodney W. Grimes 
188302a19910SJustin T. Gibbs 	bp = bufq_first(&fdc->head);
18845b81b6b3SRodney W. Grimes 
18853e425b96SJulian Elischer #ifdef	SLICE
18863e425b96SJulian Elischer 	sd = bp->b_driver1;
18873e425b96SJulian Elischer 	fd = sd->drive;
18883e425b96SJulian Elischer  	fdu = fd->unit;
18893e425b96SJulian Elischer 	if(fd->options & FDOPT_NORETRY)
18903e425b96SJulian Elischer 		goto fail;
18913e425b96SJulian Elischer #else
18923a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
18933a2f7427SDavid Greenman 		goto fail;
18943e425b96SJulian Elischer #endif
18955b81b6b3SRodney W. Grimes 	switch(fdc->retry)
18965b81b6b3SRodney W. Grimes 	{
18975b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
18985b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
18995b81b6b3SRodney W. Grimes 		break;
19005b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
19015b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
19025b81b6b3SRodney W. Grimes 		break;
19035b81b6b3SRodney W. Grimes 	case 6:
19045b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
19055b81b6b3SRodney W. Grimes 		break;
19065b81b6b3SRodney W. Grimes 	case 7:
19075b81b6b3SRodney W. Grimes 		break;
19085b81b6b3SRodney W. Grimes 	default:
19093a2f7427SDavid Greenman 	fail:
19105b81b6b3SRodney W. Grimes 		{
19113e425b96SJulian Elischer #ifdef SLICE
19123e425b96SJulian Elischer 			printf("fd%d: hard error, block %d ", fdu,
19133e425b96SJulian Elischer 				fd->skip / DEV_BSIZE);
19143e425b96SJulian Elischer #else
19157ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
19167ca0641bSAndrey A. Chernov 			/* Trick diskerr */
19173a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
19183a2f7427SDavid Greenman 				    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
191992ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
19203a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
19213a2f7427SDavid Greenman 				(struct disklabel *)NULL);
19227ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
19233e425b96SJulian Elischer #endif /* !SLICE */
1924dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1925dc5df763SJoerg Wunsch 			{
1926dc5df763SJoerg Wunsch 				printf(
1927dc5df763SJoerg Wunsch 			" (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n",
1928dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1929dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1930dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1931dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1932dc5df763SJoerg Wunsch 				       fdc->status[5]);
1933dc5df763SJoerg Wunsch 			}
1934dc5df763SJoerg Wunsch 			else
1935dc5df763SJoerg Wunsch 				printf(" (No status)\n");
19365b81b6b3SRodney W. Grimes 		}
19375b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
19385b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
1939bb6382faSJoerg Wunsch 		bp->b_resid += bp->b_bcount - fdc->fd->skip;
194002a19910SJustin T. Gibbs 		bufq_remove(&fdc->head, bp);
19415b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
19425b81b6b3SRodney W. Grimes 		biodone(bp);
194392ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
19445b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
19455b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1946f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
194792ed385aSRodney W. Grimes 		return(1);
19485b81b6b3SRodney W. Grimes 	}
19495b81b6b3SRodney W. Grimes 	fdc->retry++;
19505b81b6b3SRodney W. Grimes 	return(1);
19515b81b6b3SRodney W. Grimes }
19525b81b6b3SRodney W. Grimes 
19533e425b96SJulian Elischer #ifdef	SLICE
19543e425b96SJulian Elischer static int
19553e425b96SJulian Elischer fdformat( struct subdev *sd, struct fd_formb *finfo, struct proc *p)
19563e425b96SJulian Elischer #else	/* !SLICE */
1957b39c878eSAndrey A. Chernov static int
1958b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1959b39c878eSAndrey A. Chernov 	dev_t dev;
1960b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1961b39c878eSAndrey A. Chernov 	struct proc *p;
19623e425b96SJulian Elischer #endif	/* !SLICE */
1963b39c878eSAndrey A. Chernov {
1964b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1965b39c878eSAndrey A. Chernov  	fd_p	fd;
1966b39c878eSAndrey A. Chernov 
1967b39c878eSAndrey A. Chernov 	struct buf *bp;
1968b39c878eSAndrey A. Chernov 	int rv = 0, s;
19693a2f7427SDavid Greenman 	size_t fdblk;
1970b39c878eSAndrey A. Chernov 
19713e425b96SJulian Elischer #ifdef	SLICE
19723e425b96SJulian Elischer  	fd	= sd->drive;
19733e425b96SJulian Elischer  	fdu	= fd->unit;
19743e425b96SJulian Elischer #else
1975b39c878eSAndrey A. Chernov  	fdu	= FDUNIT(minor(dev));
1976b39c878eSAndrey A. Chernov 	fd	= &fd_data[fdu];
19773e425b96SJulian Elischer #endif
19783a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1979b39c878eSAndrey A. Chernov 
1980b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1981b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1982b39c878eSAndrey A. Chernov 	if(bp == 0)
1983b39c878eSAndrey A. Chernov 		return ENOBUFS;
198482f5379bSJoerg Wunsch 	/*
198582f5379bSJoerg Wunsch 	 * keep the process from being swapped
198682f5379bSJoerg Wunsch 	 */
198782f5379bSJoerg Wunsch 	p->p_flag |= P_PHYSIO;
1988b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1989b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1990b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1991b39c878eSAndrey A. Chernov 
1992b39c878eSAndrey A. Chernov 	/*
1993b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1994b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1995b39c878eSAndrey A. Chernov 	 */
1996b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
19973a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1998b39c878eSAndrey A. Chernov 
1999b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2000ab3f7469SPoul-Henning Kamp 	bp->b_data = (caddr_t)finfo;
2001b39c878eSAndrey A. Chernov 
2002b39c878eSAndrey A. Chernov 	/* now do the format */
20033e425b96SJulian Elischer #ifdef SLICE
20043e425b96SJulian Elischer 	bp->b_driver1 = sd;
20053e425b96SJulian Elischer 	fdsIOreq(sd, bp);
20063e425b96SJulian Elischer #else	/* !SLICE */
20073e425b96SJulian Elischer 	bp->b_dev = dev;
2008b39c878eSAndrey A. Chernov 	fdstrategy(bp);
20093e425b96SJulian Elischer #endif	/* !SLICE */
2010b39c878eSAndrey A. Chernov 
2011b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
2012b39c878eSAndrey A. Chernov 	s = splbio();
2013b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
2014b39c878eSAndrey A. Chernov 	{
20155e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2016b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
2017b39c878eSAndrey A. Chernov 			break;
2018b39c878eSAndrey A. Chernov 	}
2019b39c878eSAndrey A. Chernov 	splx(s);
2020b39c878eSAndrey A. Chernov 
202182f5379bSJoerg Wunsch 	if(rv == EWOULDBLOCK) {
2022b39c878eSAndrey A. Chernov 		/* timed out */
2023b39c878eSAndrey A. Chernov 		rv = EIO;
202482f5379bSJoerg Wunsch 		biodone(bp);
202582f5379bSJoerg Wunsch 	}
20263a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
20273a2f7427SDavid Greenman 		rv = bp->b_error;
202882f5379bSJoerg Wunsch 	/*
202982f5379bSJoerg Wunsch 	 * allow the process to be swapped
203082f5379bSJoerg Wunsch 	 */
203182f5379bSJoerg Wunsch 	p->p_flag &= ~P_PHYSIO;
2032b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
2033b39c878eSAndrey A. Chernov 	return rv;
2034b39c878eSAndrey A. Chernov }
2035b39c878eSAndrey A. Chernov 
2036f5f7ba03SJordan K. Hubbard /*
2037671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
2038f5f7ba03SJordan K. Hubbard  */
20395b81b6b3SRodney W. Grimes 
20403e425b96SJulian Elischer static int
2041b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
2042f5f7ba03SJordan K. Hubbard 	dev_t dev;
2043f5f7ba03SJordan K. Hubbard 	int cmd;
2044f5f7ba03SJordan K. Hubbard 	caddr_t addr;
2045f5f7ba03SJordan K. Hubbard 	int flag;
2046b39c878eSAndrey A. Chernov 	struct proc *p;
2047f5f7ba03SJordan K. Hubbard {
20483a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
20493a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
20503a2f7427SDavid Greenman 	size_t fdblk;
20513a2f7427SDavid Greenman 
2052f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
2053f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
2054f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
20553a2f7427SDavid Greenman 	int error = 0;
2056f5f7ba03SJordan K. Hubbard 
2057b99f0a4aSAndrew Moore #if NFT > 0
2058a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
2059a60eff27SNate Williams 
2060a60eff27SNate Williams 	/* check for a tape ioctl */
2061a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
2062b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
2063b99f0a4aSAndrew Moore #endif
2064b99f0a4aSAndrew Moore 
20653e425b96SJulian Elischer #ifdef	SLICE
20663e425b96SJulian Elischer 	/*
20673e425b96SJulian Elischer 	 * if SLICE is defined then only ft accesses come here
20683e425b96SJulian Elischer 	 * so break the rest off to another function for SLICE access.
20693e425b96SJulian Elischer 	 */
20703e425b96SJulian Elischer 	return (ENOTTY);
20713e425b96SJulian Elischer }
20723e425b96SJulian Elischer 
20733e425b96SJulian Elischer /*
20743e425b96SJulian Elischer  * Slice ioctls come here
20753e425b96SJulian Elischer  */
20763e425b96SJulian Elischer static int
20773e425b96SJulian Elischer fdsioctl( void *private, int cmd, caddr_t addr, int flag, struct proc *p)
20783e425b96SJulian Elischer {
20793e425b96SJulian Elischer 	struct	subdev *sd = private;
20803e425b96SJulian Elischer  	fd_p	fd	= sd->drive;
20813e425b96SJulian Elischer  	fdu_t	fdu	= fd->unit;
20823e425b96SJulian Elischer 	fdc_p	fdc	= fd->fdc;
20833e425b96SJulian Elischer 	fdcu_t	fdcu	= fdc->fdcu;
20843e425b96SJulian Elischer 	size_t	fdblk;
20853e425b96SJulian Elischer 	int	error	= 0;
20863e425b96SJulian Elischer #endif	/* SLICE */
20873a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2088f5f7ba03SJordan K. Hubbard 
2089f5f7ba03SJordan K. Hubbard 	switch (cmd)
2090f5f7ba03SJordan K. Hubbard 	{
20913e425b96SJulian Elischer #ifndef	SLICE
2092f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
2093f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
2094f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
20953a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
209692ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
2097f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
2098f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
2099f5f7ba03SJordan K. Hubbard 
2100191e1a59SBruce Evans 		if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl)
2101191e1a59SBruce Evans 		    == NULL)
2102f5f7ba03SJordan K. Hubbard 			error = 0;
2103f5f7ba03SJordan K. Hubbard 		else
2104f5f7ba03SJordan K. Hubbard 			error = EINVAL;
2105f5f7ba03SJordan K. Hubbard 
2106f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
2107f5f7ba03SJordan K. Hubbard 		break;
2108f5f7ba03SJordan K. Hubbard 
2109f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
2110f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2111f5f7ba03SJordan K. Hubbard 			error = EBADF;
2112f5f7ba03SJordan K. Hubbard 		break;
2113f5f7ba03SJordan K. Hubbard 
2114f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
2115f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2116f5f7ba03SJordan K. Hubbard 			error = EBADF;
2117f5f7ba03SJordan K. Hubbard 		break;
2118f5f7ba03SJordan K. Hubbard 
2119f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
2120f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2121f5f7ba03SJordan K. Hubbard 		{
2122f5f7ba03SJordan K. Hubbard 			error = EBADF;
2123f5f7ba03SJordan K. Hubbard 			break;
2124f5f7ba03SJordan K. Hubbard 		}
2125f5f7ba03SJordan K. Hubbard 
2126f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
2127f5f7ba03SJordan K. Hubbard 
2128191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
2129191e1a59SBruce Evans 					  (u_long)0)) != 0)
2130f5f7ba03SJordan K. Hubbard 			break;
2131f5f7ba03SJordan K. Hubbard 
2132b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
213354c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
2134b39c878eSAndrey A. Chernov 		break;
21353e425b96SJulian Elischer #endif	/* !SLICE */
2136b39c878eSAndrey A. Chernov 	case FD_FORM:
2137b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
2138b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
2139b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
2140b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
2141b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
2142b39c878eSAndrey A. Chernov 		else
21433e425b96SJulian Elischer #ifdef	SLICE
21443e425b96SJulian Elischer 			error = fdformat(sd, (struct fd_formb *)addr, p);
21453e425b96SJulian Elischer #else
2146b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
21473e425b96SJulian Elischer #endif
2148b39c878eSAndrey A. Chernov 		break;
2149b39c878eSAndrey A. Chernov 
2150b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
21513e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2152f5f7ba03SJordan K. Hubbard 		break;
2153f5f7ba03SJordan K. Hubbard 
21543a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
21553a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
21563a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
21573a2f7427SDavid Greenman 			return EPERM;
21583e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
21593a2f7427SDavid Greenman 		break;
21603a2f7427SDavid Greenman 
21613a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
21623e425b96SJulian Elischer 		*(int *)addr = fd->options;
21633a2f7427SDavid Greenman 		break;
21643a2f7427SDavid Greenman 
21653a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
21663e425b96SJulian Elischer 		fd->options = *(int *)addr;
21673a2f7427SDavid Greenman 		break;
21683a2f7427SDavid Greenman 
2169f5f7ba03SJordan K. Hubbard 	default:
21703a2f7427SDavid Greenman 		error = ENOTTY;
2171f5f7ba03SJordan K. Hubbard 		break;
2172f5f7ba03SJordan K. Hubbard 	}
2173f5f7ba03SJordan K. Hubbard 	return (error);
2174f5f7ba03SJordan K. Hubbard }
2175f5f7ba03SJordan K. Hubbard 
21767146c13eSJulian Elischer 
21777146c13eSJulian Elischer static fd_devsw_installed = 0;
21787146c13eSJulian Elischer 
217987f6c662SJulian Elischer static void 	fd_drvinit(void *notused )
21807146c13eSJulian Elischer {
218187f6c662SJulian Elischer 
21827146c13eSJulian Elischer 	if( ! fd_devsw_installed ) {
2183cba8a5ddSPoul-Henning Kamp 		bdevsw_add_generic(BDEV_MAJOR,CDEV_MAJOR, &fd_bdevsw);
21847146c13eSJulian Elischer 		fd_devsw_installed = 1;
21857146c13eSJulian Elischer 	}
21867146c13eSJulian Elischer }
218787f6c662SJulian Elischer 
218887f6c662SJulian Elischer SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL)
218987f6c662SJulian Elischer 
21903e425b96SJulian Elischer 
21913e425b96SJulian Elischer #ifdef	SLICE
21923e425b96SJulian Elischer static int
21933e425b96SJulian Elischer fdsopen(void *private, int flags, int mode, struct proc *p)
21943e425b96SJulian Elischer {
21953e425b96SJulian Elischer 	struct subdev *sd;
21963e425b96SJulian Elischer 
21973e425b96SJulian Elischer 	sd = private;
21983e425b96SJulian Elischer 
219926d3bf5fSJulian Elischer 	if((flags & (FREAD|FWRITE)) != 0) {
22007f2f1b78SJulian Elischer 		return(Fdopen(makedev(0,sd->minor), flags , mode, p));
220126d3bf5fSJulian Elischer 	} else {
22027f2f1b78SJulian Elischer 		return(fdclose(makedev(0,sd->minor), 0 , mode, p));
220326d3bf5fSJulian Elischer 	}
22043e425b96SJulian Elischer }
22053e425b96SJulian Elischer 
220626d3bf5fSJulian Elischer #if 0
22073e425b96SJulian Elischer static void
22083e425b96SJulian Elischer fdsclose(void *private, int flags, int mode, struct proc *p)
22093e425b96SJulian Elischer {
22103e425b96SJulian Elischer 	struct subdev *sd;
22113e425b96SJulian Elischer 
22123e425b96SJulian Elischer 	sd = private;
22133e425b96SJulian Elischer 
22143e425b96SJulian Elischer 	fdclose(makedev(0,sd->minor), 0 , 0, p);
22153e425b96SJulian Elischer 	return ;
22163e425b96SJulian Elischer }
221726d3bf5fSJulian Elischer #endif	/* 0 */
22183e425b96SJulian Elischer 
22193e425b96SJulian Elischer #endif	/* SLICE */
2220f5f7ba03SJordan K. Hubbard #endif
22213e425b96SJulian Elischer 
22223a2f7427SDavid Greenman /*
22233a2f7427SDavid Greenman  * Hello emacs, these are the
22243a2f7427SDavid Greenman  * Local Variables:
22253a2f7427SDavid Greenman  *  c-indent-level:               8
22263a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
22273a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
22283a2f7427SDavid Greenman  *  c-brace-offset:              -8
22293a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
22303a2f7427SDavid Greenman  *  c-argdecl-indent:             8
22313a2f7427SDavid Greenman  *  c-label-offset:              -8
22323a2f7427SDavid Greenman  *  c++-hanging-braces:           1
22333a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
22343a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
22353a2f7427SDavid Greenman  *  c++-friend-offset:            0
22363a2f7427SDavid Greenman  * End:
22373a2f7427SDavid Greenman  */
2238