xref: /freebsd/sys/dev/fdc/fdc.c (revision 27651b7faa6983dff360aa67c5366d33bba5619b)
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
4627651b7fSJustin T. Gibbs  *	$Id: fd.c,v 1.103 1997/09/21 21:41:00 gibbs 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"
55d2fb4892SJoerg Wunsch #include "opt_fdc.h"
565b81b6b3SRodney W. Grimes 
57b99f0a4aSAndrew Moore #if NFDC > 0
58b99f0a4aSAndrew Moore 
59b99f0a4aSAndrew Moore #include <sys/param.h>
60b99f0a4aSAndrew Moore #include <sys/systm.h>
61b99f0a4aSAndrew Moore #include <sys/kernel.h>
62b99f0a4aSAndrew Moore #include <sys/conf.h>
633ac4d1efSBruce Evans #include <sys/fcntl.h>
64671e2ceeSBruce Evans #include <machine/clock.h>
65b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h>
66b99f0a4aSAndrew Moore #include <sys/disklabel.h>
67b99f0a4aSAndrew Moore #include <sys/buf.h>
68b99f0a4aSAndrew Moore #include <sys/malloc.h>
693a2f7427SDavid Greenman #include <sys/proc.h>
70b99f0a4aSAndrew Moore #include <sys/syslog.h>
71d4319080SBruce Evans #ifdef notyet
7292200632SGarrett Wollman #include <sys/dkstat.h>
73d4319080SBruce Evans #endif
74f540b106SGarrett Wollman #include <i386/isa/isa.h>
75f540b106SGarrett Wollman #include <i386/isa/isa_device.h>
76f540b106SGarrett Wollman #include <i386/isa/fdreg.h>
77f540b106SGarrett Wollman #include <i386/isa/fdc.h>
78f540b106SGarrett Wollman #include <i386/isa/rtc.h>
79dc5df763SJoerg Wunsch #include <machine/stdarg.h>
8087eafbcaSPoul-Henning Kamp #if NFT > 0
8187eafbcaSPoul-Henning Kamp #include <sys/ftape.h>
8287eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h>
8387eafbcaSPoul-Henning Kamp #endif
848af5d536SJulian Elischer #ifdef DEVFS
858af5d536SJulian Elischer #include <sys/devfsext.h>
868af5d536SJulian Elischer #endif
875b81b6b3SRodney W. Grimes 
88b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
89b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
905b81b6b3SRodney W. Grimes 
913a2f7427SDavid Greenman /*
923a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
933a2f7427SDavid Greenman  * on the controller
943a2f7427SDavid Greenman  */
953a2f7427SDavid Greenman #define id_physid id_scsiid
963a2f7427SDavid Greenman 
97dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
98dc5df763SJoerg Wunsch #define FD_FAILED -1
99dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
100dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
101dc5df763SJoerg Wunsch 
102b39c878eSAndrey A. Chernov #define NUMTYPES 14
103b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1047ca0641bSAndrey A. Chernov 
1053a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
106b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
107b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
108b99f0a4aSAndrew Moore #define FD_1720         1
109b99f0a4aSAndrew Moore #define FD_1480         2
110b99f0a4aSAndrew Moore #define FD_1440         3
111b99f0a4aSAndrew Moore #define FD_1200         4
112b99f0a4aSAndrew Moore #define FD_820          5
113b99f0a4aSAndrew Moore #define FD_800          6
114b99f0a4aSAndrew Moore #define FD_720          7
115b99f0a4aSAndrew Moore #define FD_360          8
116ed2fa05eSAndrey A. Chernov 
117b99f0a4aSAndrew Moore #define FD_1480in5_25   9
118b99f0a4aSAndrew Moore #define FD_1440in5_25   10
119b99f0a4aSAndrew Moore #define FD_820in5_25    11
120b99f0a4aSAndrew Moore #define FD_800in5_25    12
121b99f0a4aSAndrew Moore #define FD_720in5_25    13
122b99f0a4aSAndrew Moore #define FD_360in5_25    14
123b99f0a4aSAndrew Moore 
1247ca0641bSAndrey A. Chernov 
1256f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
1265b81b6b3SRodney W. Grimes {
127126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
128126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
129126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
130126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
131126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
132126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
133126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
134b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
135ed2fa05eSAndrey A. Chernov 
136126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
137126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
138126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
139126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
140126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
141126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1425b81b6b3SRodney W. Grimes };
1435b81b6b3SRodney W. Grimes 
144b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
145dc16046fSJoerg Wunsch 
1465b81b6b3SRodney W. Grimes /***********************************************************************\
1475b81b6b3SRodney W. Grimes * Per controller structure.						*
1485b81b6b3SRodney W. Grimes \***********************************************************************/
149b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
1505b81b6b3SRodney W. Grimes 
1515b81b6b3SRodney W. Grimes /***********************************************************************\
1525b81b6b3SRodney W. Grimes * Per drive structure.							*
153b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1545b81b6b3SRodney W. Grimes \***********************************************************************/
1556f4e0bebSPoul-Henning Kamp static struct fd_data {
156b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1575b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1583a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1595b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1605b81b6b3SRodney W. Grimes 	int	flags;
1615b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1625b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1635b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1645b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1655b81b6b3SRodney W. Grimes 	int	skip;
1665b81b6b3SRodney W. Grimes 	int	hddrv;
167dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
1685b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1693a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
170d4319080SBruce Evans #ifdef notyet
17192200632SGarrett Wollman 	int	dkunit;		/* disk stats unit number */
172d4319080SBruce Evans #endif
17302a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
17402a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
17587f6c662SJulian Elischer #ifdef DEVFS
17621519754SBruce Evans 	void	*bdevs[1 + NUMDENS + MAXPARTITIONS];
17721519754SBruce Evans 	void	*cdevs[1 + NUMDENS + MAXPARTITIONS];
17887f6c662SJulian Elischer #endif
1795b81b6b3SRodney W. Grimes } fd_data[NFD];
1805b81b6b3SRodney W. Grimes 
1815b81b6b3SRodney W. Grimes /***********************************************************************\
1825b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
1835b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
1845b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
1855b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
1865b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
1875b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
1885b81b6b3SRodney W. Grimes \***********************************************************************/
189b99f0a4aSAndrew Moore 
1903a2f7427SDavid Greenman #if NFT > 0
1913a2f7427SDavid Greenman int ftopen(dev_t, int);
1923a2f7427SDavid Greenman int ftintr(ftu_t ftu);
1933a2f7427SDavid Greenman int ftclose(dev_t, int);
1943a2f7427SDavid Greenman void ftstrategy(struct buf *);
1953a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
1963a2f7427SDavid Greenman int ftdump(dev_t);
1973a2f7427SDavid Greenman int ftsize(dev_t);
19874fa89f4SRodney W. Grimes int ftattach(struct isa_device *, struct isa_device *, int);
1993a2f7427SDavid Greenman #endif
2005b81b6b3SRodney W. Grimes 
2013a2f7427SDavid Greenman /* autoconfig functions */
2023a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2033a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2043a2f7427SDavid Greenman 
2053a2f7427SDavid Greenman /* needed for ft driver, thus exported */
2063a2f7427SDavid Greenman int in_fdc(fdcu_t);
2073a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
2083a2f7427SDavid Greenman 
2093a2f7427SDavid Greenman /* internal functions */
2103a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
2113a2f7427SDavid Greenman #  define TURNON 1
2123a2f7427SDavid Greenman #  define TURNOFF 0
2133a2f7427SDavid Greenman static timeout_t fd_turnoff;
2143a2f7427SDavid Greenman static timeout_t fd_motor_on;
2153a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2163a2f7427SDavid Greenman static void fdc_reset(fdc_p);
217b5e8ce9fSBruce Evans static int fd_in(fdcu_t, int *);
2183a2f7427SDavid Greenman static void fdstart(fdcu_t);
2193a2f7427SDavid Greenman static timeout_t fd_timeout;
2203a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2213a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
222aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2233a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2243a2f7427SDavid Greenman 
225d66c374fSTor Egge static int enable_fifo(fdc_p fdc);
226d66c374fSTor Egge 
227d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
228d66c374fSTor Egge 
229aaf08d94SGarrett Wollman 
2305b81b6b3SRodney W. Grimes #define DEVIDLE		0
2315b81b6b3SRodney W. Grimes #define FINDWORK	1
2325b81b6b3SRodney W. Grimes #define	DOSEEK		2
2335b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
2345b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
2355b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
2365b81b6b3SRodney W. Grimes #define	STARTRECAL	6
2375b81b6b3SRodney W. Grimes #define	RESETCTLR	7
2385b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
2395b81b6b3SRodney W. Grimes #define	RECALWAIT	9
2405b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
2415b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
2425b81b6b3SRodney W. Grimes 
243d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
244cba2a7c6SBruce Evans static char const * const fdstates[] =
2455b81b6b3SRodney W. Grimes {
2465b81b6b3SRodney W. Grimes "DEVIDLE",
2475b81b6b3SRodney W. Grimes "FINDWORK",
2485b81b6b3SRodney W. Grimes "DOSEEK",
2495b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2505b81b6b3SRodney W. Grimes "IOCOMPLETE",
2515b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2525b81b6b3SRodney W. Grimes "STARTRECAL",
2535b81b6b3SRodney W. Grimes "RESETCTLR",
2545b81b6b3SRodney W. Grimes "SEEKWAIT",
2555b81b6b3SRodney W. Grimes "RECALWAIT",
2565b81b6b3SRodney W. Grimes "MOTORWAIT",
2575b81b6b3SRodney W. Grimes "IOTIMEDOUT"
2585b81b6b3SRodney W. Grimes };
2595b81b6b3SRodney W. Grimes 
2603a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
261cba2a7c6SBruce Evans static int volatile fd_debug = 0;
2625b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2635b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
264d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
2655b81b6b3SRodney W. Grimes #define TRACE0(arg)
2665b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
267d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
2685b81b6b3SRodney W. Grimes 
269dc16046fSJoerg Wunsch /* autoconfig structure */
270dc16046fSJoerg Wunsch 
271dc16046fSJoerg Wunsch struct	isa_driver fdcdriver = {
272dc16046fSJoerg Wunsch 	fdprobe, fdattach, "fdc",
273dc16046fSJoerg Wunsch };
274dc16046fSJoerg Wunsch 
27587f6c662SJulian Elischer static	d_open_t	Fdopen;	/* NOTE, not fdopen */
27687f6c662SJulian Elischer static	d_close_t	fdclose;
27787f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
27887f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
27987f6c662SJulian Elischer 
28087f6c662SJulian Elischer #define CDEV_MAJOR 9
28187f6c662SJulian Elischer #define BDEV_MAJOR 2
282cba8a5ddSPoul-Henning Kamp static struct cdevsw fd_cdevsw;
283d2f265faSPoul-Henning Kamp static struct bdevsw fd_bdevsw =
28487f6c662SJulian Elischer 	{ Fdopen,	fdclose,	fdstrategy,	fdioctl,	/*2*/
285996c772fSJohn Dyson 	  nodump,	nopsize,	D_DISK,	"fd",	&fd_cdevsw,	-1 };
28687f6c662SJulian Elischer 
28787f6c662SJulian Elischer 
2886f4e0bebSPoul-Henning Kamp static struct isa_device *fdcdevs[NFDC];
28992200632SGarrett Wollman 
290dc5df763SJoerg Wunsch static int
291dc5df763SJoerg Wunsch fdc_err(fdcu_t fdcu, const char *s)
292dc5df763SJoerg Wunsch {
293dc5df763SJoerg Wunsch 	fdc_data[fdcu].fdc_errs++;
29416b04b6aSJoerg Wunsch 	if(s) {
295dc5df763SJoerg Wunsch 		if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX)
2966a0e6f42SRodney W. Grimes 			printf("fdc%d: %s", fdcu, s);
297dc5df763SJoerg Wunsch 		else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX)
298dc5df763SJoerg Wunsch 			printf("fdc%d: too many errors, not logging any more\n",
299dc5df763SJoerg Wunsch 			       fdcu);
30016b04b6aSJoerg Wunsch 	}
301dc5df763SJoerg Wunsch 
302dc5df763SJoerg Wunsch 	return FD_FAILED;
303dc5df763SJoerg Wunsch }
304dc5df763SJoerg Wunsch 
305dc5df763SJoerg Wunsch /*
306dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
307dc5df763SJoerg Wunsch  * Unit number,
308dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
309dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
310dc5df763SJoerg Wunsch  */
311dc5df763SJoerg Wunsch 
3126f4e0bebSPoul-Henning Kamp static int
313dc5df763SJoerg Wunsch fd_cmd(fdcu_t fdcu, int n_out, ...)
314dc5df763SJoerg Wunsch {
315dc5df763SJoerg Wunsch 	u_char cmd;
316dc5df763SJoerg Wunsch 	int n_in;
317dc5df763SJoerg Wunsch 	int n;
318dc5df763SJoerg Wunsch 	va_list ap;
319dc5df763SJoerg Wunsch 
320dc5df763SJoerg Wunsch 	va_start(ap, n_out);
321dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
322dc5df763SJoerg Wunsch 	va_end(ap);
323dc5df763SJoerg Wunsch 	va_start(ap, n_out);
324dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
325dc5df763SJoerg Wunsch 	{
326dc5df763SJoerg Wunsch 		if (out_fdc(fdcu, va_arg(ap, int)) < 0)
327dc5df763SJoerg Wunsch 		{
328dc5df763SJoerg Wunsch 			char msg[50];
329dc5df763SJoerg Wunsch 			sprintf(msg,
330dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
331dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
332dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
333dc5df763SJoerg Wunsch 		}
334dc5df763SJoerg Wunsch 	}
335dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
336dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
337dc5df763SJoerg Wunsch 	{
338dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
339dc5df763SJoerg Wunsch 		if (fd_in(fdcu, ptr) < 0)
340dc5df763SJoerg Wunsch 		{
341dc5df763SJoerg Wunsch 			char msg[50];
342dc5df763SJoerg Wunsch 			sprintf(msg,
343dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
344dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
345dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
346dc5df763SJoerg Wunsch 		}
347dc5df763SJoerg Wunsch 	}
348dc5df763SJoerg Wunsch 
349dc5df763SJoerg Wunsch 	return 0;
350dc5df763SJoerg Wunsch }
351dc5df763SJoerg Wunsch 
3526f4e0bebSPoul-Henning Kamp static int
353d66c374fSTor Egge enable_fifo(fdc_p fdc)
354d66c374fSTor Egge {
355d66c374fSTor Egge 	int i, j;
356d66c374fSTor Egge 
357d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
358d66c374fSTor Egge 
359d66c374fSTor Egge 		/*
360d66c374fSTor Egge 		 * XXX:
361d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
362d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
363d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
364d66c374fSTor Egge 		 */
365d66c374fSTor Egge 
366d66c374fSTor Egge 		if (out_fdc(fdc->fdcu, I8207X_CONFIGURE) < 0)
367d66c374fSTor Egge 			return fdc_err(fdc->fdcu, "Enable FIFO failed\n");
368d66c374fSTor Egge 
369d66c374fSTor Egge 		/* If command is invalid, return */
370d66c374fSTor Egge 		j = 100000;
371d66c374fSTor Egge 		while ((i = inb(fdc->baseport + FDSTS) & (NE7_DIO | NE7_RQM))
372d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
373d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
374d66c374fSTor Egge 				fdc_reset(fdc);
375d66c374fSTor Egge 				return FD_FAILED;
376d66c374fSTor Egge 			}
377d66c374fSTor Egge 		if (j<0 ||
378d66c374fSTor Egge 		    fd_cmd(fdc->fdcu, 3,
379d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
380d66c374fSTor Egge 			fdc_reset(fdc);
381d66c374fSTor Egge 			return fdc_err(fdc->fdcu, "Enable FIFO failed\n");
382d66c374fSTor Egge 		}
383d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
384d66c374fSTor Egge 		return 0;
385d66c374fSTor Egge 	}
386d66c374fSTor Egge 	if (fd_cmd(fdc->fdcu, 4,
387d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
388d66c374fSTor Egge 		return fdc_err(fdc->fdcu, "Re-enable FIFO failed\n");
389d66c374fSTor Egge 	return 0;
390d66c374fSTor Egge }
391d66c374fSTor Egge 
392d66c374fSTor Egge static int
393dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
394dc5df763SJoerg Wunsch {
395dc5df763SJoerg Wunsch 	int st3;
396dc5df763SJoerg Wunsch 
397dc5df763SJoerg Wunsch 	if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
398dc5df763SJoerg Wunsch 	{
3996a0e6f42SRodney W. Grimes 		return fdc_err(fdc->fdcu, "Sense Drive Status failed\n");
400dc5df763SJoerg Wunsch 	}
401dc5df763SJoerg Wunsch 	if (st3p)
402dc5df763SJoerg Wunsch 		*st3p = st3;
403dc5df763SJoerg Wunsch 
404dc5df763SJoerg Wunsch 	return 0;
405dc5df763SJoerg Wunsch }
406dc5df763SJoerg Wunsch 
4076f4e0bebSPoul-Henning Kamp static int
408dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
409dc5df763SJoerg Wunsch {
410dc5df763SJoerg Wunsch 	int st0, cyl;
411dc5df763SJoerg Wunsch 
412dc5df763SJoerg Wunsch 	int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0);
413dc5df763SJoerg Wunsch 
414dc5df763SJoerg Wunsch 	if (ret)
415dc5df763SJoerg Wunsch 	{
416dc5df763SJoerg Wunsch 		(void)fdc_err(fdc->fdcu,
417dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
418dc5df763SJoerg Wunsch 		return ret;
419dc5df763SJoerg Wunsch 	}
420dc5df763SJoerg Wunsch 
421dc5df763SJoerg Wunsch 	if (st0p)
422dc5df763SJoerg Wunsch 		*st0p = st0;
423dc5df763SJoerg Wunsch 
424dc5df763SJoerg Wunsch 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV)
425dc5df763SJoerg Wunsch 	{
426dc5df763SJoerg Wunsch 		/*
427dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
428dc5df763SJoerg Wunsch 		 */
429dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
430dc5df763SJoerg Wunsch 	}
431dc5df763SJoerg Wunsch 
432dc5df763SJoerg Wunsch 	if (fd_in(fdc->fdcu, &cyl) < 0)
433dc5df763SJoerg Wunsch 	{
434dc5df763SJoerg Wunsch 		return fdc_err(fdc->fdcu, "can't get cyl num\n");
435dc5df763SJoerg Wunsch 	}
436dc5df763SJoerg Wunsch 
437dc5df763SJoerg Wunsch 	if (cylp)
438dc5df763SJoerg Wunsch 		*cylp = cyl;
439dc5df763SJoerg Wunsch 
440dc5df763SJoerg Wunsch 	return 0;
441dc5df763SJoerg Wunsch }
442dc5df763SJoerg Wunsch 
443dc5df763SJoerg Wunsch 
4446f4e0bebSPoul-Henning Kamp static int
445dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
446dc5df763SJoerg Wunsch {
447dc5df763SJoerg Wunsch 	int i, ret;
448b5e8ce9fSBruce Evans 
449dc5df763SJoerg Wunsch 	for (i = 0; i < 7; i++)
450dc5df763SJoerg Wunsch 	{
451b5e8ce9fSBruce Evans 		/*
452b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
453b5e8ce9fSBruce Evans 		 * from the hardware, but fdc_status wants u_longs and
454b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
455b5e8ce9fSBruce Evans 		 */
456b5e8ce9fSBruce Evans 		int status;
457b5e8ce9fSBruce Evans 
458b5e8ce9fSBruce Evans 		ret = fd_in(fdc->fdcu, &status);
459b5e8ce9fSBruce Evans 		fdc->status[i] = status;
460b5e8ce9fSBruce Evans 		if (ret != 0)
461dc5df763SJoerg Wunsch 			break;
462dc5df763SJoerg Wunsch 	}
463dc5df763SJoerg Wunsch 
464dc5df763SJoerg Wunsch 	if (ret == 0)
465dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
466dc5df763SJoerg Wunsch 	else
467dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
468dc5df763SJoerg Wunsch 
469dc5df763SJoerg Wunsch 	return ret;
470dc5df763SJoerg Wunsch }
471dc5df763SJoerg Wunsch 
4725b81b6b3SRodney W. Grimes /****************************************************************************/
4735b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
4745b81b6b3SRodney W. Grimes /****************************************************************************/
475dc5df763SJoerg Wunsch 
4765b81b6b3SRodney W. Grimes /*
4775b81b6b3SRodney W. Grimes  * probe for existance of controller
4785b81b6b3SRodney W. Grimes  */
4793a2f7427SDavid Greenman static int
480dc5df763SJoerg Wunsch fdprobe(struct isa_device *dev)
4815b81b6b3SRodney W. Grimes {
4825b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
4835b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
4845b81b6b3SRodney W. Grimes 	{
4856a0e6f42SRodney W. Grimes 		printf("fdc%d: unit used multiple times\n", fdcu);
4865b81b6b3SRodney W. Grimes 		return 0;
4875b81b6b3SRodney W. Grimes 	}
4885b81b6b3SRodney W. Grimes 
48992200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
4905b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
4915b81b6b3SRodney W. Grimes 
49216111cedSAndrew Moore 	/* First - lets reset the floppy controller */
4933a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
49416111cedSAndrew Moore 	DELAY(100);
4953a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
49616111cedSAndrew Moore 
4975b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
498dc5df763SJoerg Wunsch 	if (fd_cmd(fdcu,
499dc5df763SJoerg Wunsch 		   3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
500dc5df763SJoerg Wunsch 		   0))
5015b81b6b3SRodney W. Grimes 	{
5025b81b6b3SRodney W. Grimes 		return(0);
5035b81b6b3SRodney W. Grimes 	}
5045b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
5055b81b6b3SRodney W. Grimes }
5065b81b6b3SRodney W. Grimes 
5075b81b6b3SRodney W. Grimes /*
5085b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
5095b81b6b3SRodney W. Grimes  */
5103a2f7427SDavid Greenman static int
511dc5df763SJoerg Wunsch fdattach(struct isa_device *dev)
5125b81b6b3SRodney W. Grimes {
5133a2f7427SDavid Greenman 	unsigned fdt;
5145b81b6b3SRodney W. Grimes 	fdu_t	fdu;
5155b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5165b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
5175b81b6b3SRodney W. Grimes 	fd_p	fd;
518cba2a7c6SBruce Evans 	int	fdsu, st0, st3, i;
519cba2a7c6SBruce Evans #if NFT > 0
520cba2a7c6SBruce Evans 	int	unithasfd;
521cba2a7c6SBruce Evans #endif
522b99f0a4aSAndrew Moore 	struct isa_device *fdup;
523dc5df763SJoerg Wunsch 	int ic_type = 0;
524999422d7SJulian Elischer #ifdef DEVFS
525c8f2fe8dSBruce Evans 	int	mynor;
52621519754SBruce Evans 	int	typemynor;
52721519754SBruce Evans 	int	typesize;
52821519754SBruce Evans #endif
52992200632SGarrett Wollman 
5305b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
5315b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
5325b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
533100f78bbSSujal Patel 	/* Acquire the DMA channel forever, The driver will do the rest */
534100f78bbSSujal Patel 	isa_dma_acquire(fdc->dmachan);
535dd87702aSBruce Evans 	isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
5365b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
5373a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
5383a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
53902a19910SJustin T. Gibbs 	bufq_init(&fdc->head);
5405b81b6b3SRodney W. Grimes 
5415b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
542b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
543b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
544b99f0a4aSAndrew Moore 			continue;
545b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
546b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
547b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
548b99f0a4aSAndrew Moore 			continue;
549b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
550b99f0a4aSAndrew Moore 		/* look up what bios thinks we have */
551b99f0a4aSAndrew Moore 		switch (fdu) {
552b99f0a4aSAndrew Moore 			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
553b99f0a4aSAndrew Moore 				break;
554b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
555b99f0a4aSAndrew Moore 				break;
556b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
557b99f0a4aSAndrew Moore 				break;
558b99f0a4aSAndrew Moore 		}
5595b81b6b3SRodney W. Grimes 		/* is there a unit? */
560b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
561b99f0a4aSAndrew Moore #if NFT > 0
562b99f0a4aSAndrew Moore 		    || (fdsu >= DRVS_PER_CTLR)) {
563b99f0a4aSAndrew Moore #else
564b99f0a4aSAndrew Moore 		) {
56556ef0285SAndrew Moore 			fd->type = NO_TYPE;
566b99f0a4aSAndrew Moore #endif
567b99f0a4aSAndrew Moore #if NFT > 0
568b99f0a4aSAndrew Moore 			/* If BIOS says no floppy, or > 2nd device */
569b99f0a4aSAndrew Moore 			/* Probe for and attach a floppy tape.     */
57074fa89f4SRodney W. Grimes 			/* Tell FT if there was already a disk     */
57174fa89f4SRodney W. Grimes 			/* with this unit number found.            */
57274fa89f4SRodney W. Grimes 
57374fa89f4SRodney W. Grimes 			unithasfd = 0;
57474fa89f4SRodney W. Grimes 			if (fdu < NFD && fd->type != NO_TYPE)
57574fa89f4SRodney W. Grimes 				unithasfd = 1;
57685827d9cSJoerg Wunsch 			if (ftattach(dev, fdup, unithasfd))
577b99f0a4aSAndrew Moore 				continue;
57856ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
579b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
58056ef0285SAndrew Moore #endif
5815b81b6b3SRodney W. Grimes 			continue;
582f5f7ba03SJordan K. Hubbard 		}
5835b81b6b3SRodney W. Grimes 
5845b81b6b3SRodney W. Grimes 		/* select it */
5853a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
5866b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
587dc5df763SJoerg Wunsch 
588dc5df763SJoerg Wunsch 		if (ic_type == 0 &&
589dc5df763SJoerg Wunsch 		    fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
590dc5df763SJoerg Wunsch 		{
591d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
5926a0e6f42SRodney W. Grimes 			printf("fdc%d: ", fdcu);
593d2fb4892SJoerg Wunsch #endif
594dc5df763SJoerg Wunsch 			ic_type = (u_char)ic_type;
595dc5df763SJoerg Wunsch 			switch( ic_type ) {
596dc5df763SJoerg Wunsch 			case 0x80:
597d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
5986a0e6f42SRodney W. Grimes 				printf("NEC 765\n");
599d2fb4892SJoerg Wunsch #endif
600dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE765;
601dc5df763SJoerg Wunsch 				break;
602dc5df763SJoerg Wunsch 			case 0x81:
603d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6046a0e6f42SRodney W. Grimes 				printf("Intel 82077\n");
605d2fb4892SJoerg Wunsch #endif
606dc5df763SJoerg Wunsch 				fdc->fdct = FDC_I82077;
607dc5df763SJoerg Wunsch 				break;
608dc5df763SJoerg Wunsch 			case 0x90:
609d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6106a0e6f42SRodney W. Grimes 				printf("NEC 72065B\n");
611d2fb4892SJoerg Wunsch #endif
612dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE72065;
613dc5df763SJoerg Wunsch 				break;
614dc5df763SJoerg Wunsch 			default:
615d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6166a0e6f42SRodney W. Grimes 				printf("unknown IC type %02x\n", ic_type);
617d2fb4892SJoerg Wunsch #endif
618dc5df763SJoerg Wunsch 				fdc->fdct = FDC_UNKNOWN;
619dc5df763SJoerg Wunsch 				break;
6206b7bd95bSJoerg Wunsch 			}
621d66c374fSTor Egge 			if (fdc->fdct != FDC_NE765 &&
622d66c374fSTor Egge 			    fdc->fdct != FDC_UNKNOWN &&
623d66c374fSTor Egge 			    enable_fifo(fdc) == 0) {
624d66c374fSTor Egge 				printf("fdc%d: FIFO enabled", fdcu);
625d66c374fSTor Egge 				printf(", %d bytes threshold\n",
626d66c374fSTor Egge 				       fifo_threshold);
627d66c374fSTor Egge 			}
628dc5df763SJoerg Wunsch 		}
629dc5df763SJoerg Wunsch 		if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
630dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
631dc5df763SJoerg Wunsch 			/* if at track 0, first seek inwards */
632dc5df763SJoerg Wunsch 			/* seek some steps: */
633dc5df763SJoerg Wunsch 			(void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
634dc5df763SJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
635dc5df763SJoerg Wunsch 			(void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
636dc5df763SJoerg Wunsch 		}
637dc5df763SJoerg Wunsch 
638dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
639dc5df763SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
640dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
641dc5df763SJoerg Wunsch 			/* Seek some steps... */
642dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
643dc5df763SJoerg Wunsch 				/* ...wait a moment... */
644dc5df763SJoerg Wunsch 				DELAY(300000);
645dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
646dc5df763SJoerg Wunsch 				(void)fd_sense_int(fdc, 0, 0);
647dc5df763SJoerg Wunsch 			}
648dc5df763SJoerg Wunsch 		}
649dc5df763SJoerg Wunsch 
6506b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
6516b7bd95bSJoerg Wunsch 			/*
6526b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
6536b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
6546b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
6556b7bd95bSJoerg Wunsch 			 * more than 77 steps
6566b7bd95bSJoerg Wunsch 			 */
657dc5df763SJoerg Wunsch 			/* go back to 0: */
658dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
6596b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
6606b7bd95bSJoerg Wunsch 				DELAY(i == 0? 1000000: 300000);
6615b81b6b3SRodney W. Grimes 
6626b7bd95bSJoerg Wunsch 				/* anything responding? */
663dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
664dc5df763SJoerg Wunsch 				(st0 & NE7_ST0_EC) == 0)
6656b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
6666b7bd95bSJoerg Wunsch 			}
667dc5df763SJoerg Wunsch 		}
6686b7bd95bSJoerg Wunsch 
6693a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
6703a2f7427SDavid Greenman 
6713a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
6725b81b6b3SRodney W. Grimes 			continue;
6735b81b6b3SRodney W. Grimes 
674dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
675b99f0a4aSAndrew Moore 		fd->fdc = fdc;
676b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
6773a2f7427SDavid Greenman 		fd->options = 0;
67802a19910SJustin T. Gibbs 		callout_handle_init(&fd->toffhandle);
67902a19910SJustin T. Gibbs 		callout_handle_init(&fd->tohandle);
6802d9d0204SRodney W. Grimes 		printf("fd%d: ", fdu);
6815b81b6b3SRodney W. Grimes 
682b99f0a4aSAndrew Moore 		switch (fdt) {
6837ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
6846a0e6f42SRodney W. Grimes 			printf("1.2MB 5.25in\n");
685b99f0a4aSAndrew Moore 			fd->type = FD_1200;
6867ca0641bSAndrey A. Chernov 			break;
6877ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
6886a0e6f42SRodney W. Grimes 			printf("1.44MB 3.5in\n");
689b99f0a4aSAndrew Moore 			fd->type = FD_1440;
6907ca0641bSAndrey A. Chernov 			break;
691290dd077SJoerg Wunsch 		case RTCFDT_288M:
69286a727d9SJoerg Wunsch 		case RTCFDT_288M_1:
6936a0e6f42SRodney W. Grimes 			printf("2.88MB 3.5in - 1.44MB mode\n");
694290dd077SJoerg Wunsch 			fd->type = FD_1440;
695290dd077SJoerg Wunsch 			break;
6967ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
6976a0e6f42SRodney W. Grimes 			printf("360KB 5.25in\n");
698b99f0a4aSAndrew Moore 			fd->type = FD_360;
6997ca0641bSAndrey A. Chernov 			break;
700ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
7016a0e6f42SRodney W. Grimes 			printf("720KB 3.5in\n");
702b99f0a4aSAndrew Moore 			fd->type = FD_720;
703ed2fa05eSAndrey A. Chernov 			break;
7047ca0641bSAndrey A. Chernov 		default:
7056a0e6f42SRodney W. Grimes 			printf("unknown\n");
706b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
70721519754SBruce Evans 			continue;
7085b81b6b3SRodney W. Grimes 		}
709999422d7SJulian Elischer #ifdef DEVFS
71021519754SBruce Evans 		mynor = fdu << 6;
71121519754SBruce Evans 		fd->bdevs[0] = devfs_add_devswf(&fd_bdevsw, mynor, DV_BLK,
712f85120acSBruce Evans 						UID_ROOT, GID_OPERATOR, 0640,
71321519754SBruce Evans 						"fd%d", fdu);
71421519754SBruce Evans 		fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR,
715f85120acSBruce Evans 						UID_ROOT, GID_OPERATOR, 0640,
71621519754SBruce Evans 						"rfd%d", fdu);
71721519754SBruce Evans 		for (i = 1; i < 1 + NUMDENS; i++) {
71821519754SBruce Evans 			/*
71921519754SBruce Evans 			 * XXX this and the lookup in Fdopen() should be
72021519754SBruce Evans 			 * data driven.
72121519754SBruce Evans 			 */
72221519754SBruce Evans 			switch (fd->type) {
72321519754SBruce Evans 			case FD_360:
72421519754SBruce Evans 				if (i != FD_360)
72521519754SBruce Evans 					continue;
72621519754SBruce Evans 				break;
72721519754SBruce Evans 			case FD_720:
72821519754SBruce Evans 				if (i != FD_720 && i != FD_800 && i != FD_820)
72921519754SBruce Evans 					continue;
73021519754SBruce Evans 				break;
73121519754SBruce Evans 			case FD_1200:
73221519754SBruce Evans 				if (i != FD_360 && i != FD_720 && i != FD_800
73321519754SBruce Evans 				    && i != FD_820 && i != FD_1200
73421519754SBruce Evans 				    && i != FD_1440 && i != FD_1480)
73521519754SBruce Evans 					continue;
73621519754SBruce Evans 				break;
73721519754SBruce Evans 			case FD_1440:
73821519754SBruce Evans 				if (i != FD_720 && i != FD_800 && i != FD_820
73921519754SBruce Evans 				    && i != FD_1200 && i != FD_1440
74021519754SBruce Evans 				    && i != FD_1480 && i != FD_1720)
74121519754SBruce Evans 					continue;
74221519754SBruce Evans 				break;
74321519754SBruce Evans 			}
74421519754SBruce Evans 			typemynor = mynor | i;
74521519754SBruce Evans 			typesize = fd_types[i - 1].size / 2;
74621519754SBruce Evans 			/*
74721519754SBruce Evans 			 * XXX all these conversions give bloated code and
74821519754SBruce Evans 			 * confusing names.
74921519754SBruce Evans 			 */
75021519754SBruce Evans 			if (typesize == 1476)
75121519754SBruce Evans 				typesize = 1480;
75221519754SBruce Evans 			if (typesize == 1722)
75321519754SBruce Evans 				typesize = 1720;
75421519754SBruce Evans 			fd->bdevs[i] =
75521519754SBruce Evans 				devfs_add_devswf(&fd_bdevsw, typemynor, DV_BLK,
75621519754SBruce Evans 						 UID_ROOT, GID_OPERATOR, 0640,
75721519754SBruce Evans 						 "fd%d.%d", fdu, typesize);
75821519754SBruce Evans 			fd->cdevs[i] =
75921519754SBruce Evans 				devfs_add_devswf(&fd_cdevsw, typemynor, DV_CHR,
76021519754SBruce Evans 						 UID_ROOT, GID_OPERATOR, 0640,
76121519754SBruce Evans 						 "rfd%d.%d", fdu, typesize);
76221519754SBruce Evans 		}
76321519754SBruce Evans 		for (i = 0; i < MAXPARTITIONS; i++) {
76421519754SBruce Evans 			fd->bdevs[1 + NUMDENS + i] =
76521519754SBruce Evans 				devfs_link(fd->bdevs[0],
76621519754SBruce Evans 					   "fd%d%c", fdu, 'a' + i);
76721519754SBruce Evans 			fd->cdevs[1 + NUMDENS + i] =
76821519754SBruce Evans 				devfs_link(fd->cdevs[0],
76921519754SBruce Evans 					   "rfd%d%c", fdu, 'a' + i);
77021519754SBruce Evans 		}
771999422d7SJulian Elischer #endif /* DEVFS */
772d4319080SBruce Evans #ifdef notyet
77392200632SGarrett Wollman 		if (dk_ndrive < DK_NDRIVE) {
77492200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
7756a0e6f42SRodney W. Grimes 			fd->dkunit = dk_ndrive++;
776671e2ceeSBruce Evans 			/*
777671e2ceeSBruce Evans 			 * XXX assume rate is FDC_500KBPS.
778671e2ceeSBruce Evans 			 */
779671e2ceeSBruce Evans 			dk_wpms[dk_ndrive] = 500000 / 8 / 2;
78092200632SGarrett Wollman 		} else {
78192200632SGarrett Wollman 			fd->dkunit = -1;
78292200632SGarrett Wollman 		}
783d4319080SBruce Evans #endif
7845b81b6b3SRodney W. Grimes 	}
7855b81b6b3SRodney W. Grimes 
7863a2f7427SDavid Greenman 	return (1);
7875b81b6b3SRodney W. Grimes }
7885b81b6b3SRodney W. Grimes 
7895b81b6b3SRodney W. Grimes /****************************************************************************/
7905b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
7915e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
7925b81b6b3SRodney W. Grimes /****************************************************************************/
7933a2f7427SDavid Greenman static void
794dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
7955b81b6b3SRodney W. Grimes {
7963a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
7973a2f7427SDavid Greenman 	int needspecify = 0;
7983a2f7427SDavid Greenman 
7993a2f7427SDavid Greenman 	if(turnon) {
8003a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
8013a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
8023a2f7427SDavid Greenman 	} else
8033a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
8043a2f7427SDavid Greenman 
8055e235068SJordan K. Hubbard 	if(!turnon
8065e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
8075e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
8085e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
8095e235068SJordan K. Hubbard 	else {
8103a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
8113a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
8123a2f7427SDavid Greenman 			needspecify = 1;
8133a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
8145b81b6b3SRodney W. Grimes 	}
8155b81b6b3SRodney W. Grimes 
8163a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
8173a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
8183a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
8193a2f7427SDavid Greenman 
8203a2f7427SDavid Greenman 	if(needspecify) {
821dc8603e3SJoerg Wunsch 		/*
822dc5df763SJoerg Wunsch 		 * XXX
823dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
824dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
825dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
826dc8603e3SJoerg Wunsch 		 */
827dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
828dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
829dc5df763SJoerg Wunsch 			     0);
830d66c374fSTor Egge 		if (fdc_data[fdcu].flags & FDC_HAS_FIFO)
831d66c374fSTor Egge 			(void) enable_fifo(&fdc_data[fdcu]);
8323a2f7427SDavid Greenman 	}
8333a2f7427SDavid Greenman }
8343a2f7427SDavid Greenman 
835381fe1aaSGarrett Wollman static void
836d0917939SPaul Richards fd_turnoff(void *arg1)
8375b81b6b3SRodney W. Grimes {
838381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
839f5f7ba03SJordan K. Hubbard 	int	s;
8405b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
841dc16046fSJoerg Wunsch 
842dc16046fSJoerg Wunsch 	TRACE1("[fd%d: turnoff]", fdu);
8438335c1b8SBruce Evans 
8448335c1b8SBruce Evans 	/*
8458335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
8468335c1b8SBruce Evans 	 * XXX shouldn't even schedule turnoff until drive is inactive
8478335c1b8SBruce Evans 	 * and nothing is queued on it.
8488335c1b8SBruce Evans 	 */
8498335c1b8SBruce Evans 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fdu) {
85002a19910SJustin T. Gibbs 		fd->toffhandle = timeout(fd_turnoff, arg1, 4 * hz);
8518335c1b8SBruce Evans 		return;
8528335c1b8SBruce Evans 	}
8538335c1b8SBruce Evans 
854f5f7ba03SJordan K. Hubbard 	s = splbio();
8555b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
8563a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
857f5f7ba03SJordan K. Hubbard 	splx(s);
8585b81b6b3SRodney W. Grimes }
8595b81b6b3SRodney W. Grimes 
8603a2f7427SDavid Greenman static void
861d0917939SPaul Richards fd_motor_on(void *arg1)
8625b81b6b3SRodney W. Grimes {
863381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
864f5f7ba03SJordan K. Hubbard 	int	s;
865f5f7ba03SJordan K. Hubbard 
8665b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
867f5f7ba03SJordan K. Hubbard 	s = splbio();
8685b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
8695b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
8705b81b6b3SRodney W. Grimes 	{
871f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
8725b81b6b3SRodney W. Grimes 	}
873f5f7ba03SJordan K. Hubbard 	splx(s);
8745b81b6b3SRodney W. Grimes }
8755b81b6b3SRodney W. Grimes 
8763a2f7427SDavid Greenman static void
877dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
8785b81b6b3SRodney W. Grimes {
8795b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
8805b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
8815b81b6b3SRodney W. Grimes 	{
8823a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
8833a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
8845e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
8855b81b6b3SRodney W. Grimes 	}
8865b81b6b3SRodney W. Grimes }
8875b81b6b3SRodney W. Grimes 
888381fe1aaSGarrett Wollman static void
889dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
8905b81b6b3SRodney W. Grimes {
8913a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
8923a2f7427SDavid Greenman 
8933a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
8943a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8953a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8963a2f7427SDavid Greenman 	DELAY(100);
8973a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
8983a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
8993a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
9003a2f7427SDavid Greenman 	DELAY(100);
9013a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
9023a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
9033a2f7427SDavid Greenman 
904dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
905dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
906dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
907dc5df763SJoerg Wunsch 		     0);
908d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
909d66c374fSTor Egge 		(void) enable_fifo(fdc);
9105b81b6b3SRodney W. Grimes }
9115b81b6b3SRodney W. Grimes 
9125b81b6b3SRodney W. Grimes /****************************************************************************/
9135b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
9145b81b6b3SRodney W. Grimes /****************************************************************************/
9155b81b6b3SRodney W. Grimes int
916dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
9175b81b6b3SRodney W. Grimes {
9185b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9195b81b6b3SRodney W. Grimes 	int i, j = 100000;
9203a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
9215b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
922dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9236a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
9245b81b6b3SRodney W. Grimes 	if (j <= 0)
92516b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
926d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
9273a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
9283a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
9295b81b6b3SRodney W. Grimes 	return(i);
930d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
9313a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
932d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
9335b81b6b3SRodney W. Grimes }
9345b81b6b3SRodney W. Grimes 
935dc5df763SJoerg Wunsch /*
936dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
937dc5df763SJoerg Wunsch  */
938b5e8ce9fSBruce Evans static int
939dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
940dc5df763SJoerg Wunsch {
941dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
942dc5df763SJoerg Wunsch 	int i, j = 100000;
943dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
944dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
945dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9466a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
947dc5df763SJoerg Wunsch 	if (j <= 0)
94816b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
949d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
950dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
951dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
952dc5df763SJoerg Wunsch 	*ptr = i;
953dc5df763SJoerg Wunsch 	return 0;
954d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
955dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
956dc5df763SJoerg Wunsch 	if (ptr)
957dc5df763SJoerg Wunsch 		*ptr = i;
958dc5df763SJoerg Wunsch 	return 0;
959d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
960dc5df763SJoerg Wunsch }
961dc5df763SJoerg Wunsch 
962dc5df763SJoerg Wunsch int
963dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
9645b81b6b3SRodney W. Grimes {
9655b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9663b3837dbSRodney W. Grimes 	int i;
9675b81b6b3SRodney W. Grimes 
9683b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
9693b3837dbSRodney W. Grimes 	i = 100000;
9703a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
9716a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
9723b3837dbSRodney W. Grimes 
9733b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
9743b3837dbSRodney W. Grimes 	i = 100000;
9753a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
97616b04b6aSJoerg Wunsch 	if (i <= 0)
97716b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0);
9783b3837dbSRodney W. Grimes 
9793b3837dbSRodney W. Grimes 	/* Send the command and return */
9803a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
9813a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
9825b81b6b3SRodney W. Grimes 	return (0);
9835b81b6b3SRodney W. Grimes }
9845b81b6b3SRodney W. Grimes 
9855b81b6b3SRodney W. Grimes /****************************************************************************/
9865b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
9875b81b6b3SRodney W. Grimes /****************************************************************************/
988381fe1aaSGarrett Wollman int
989671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
9905b81b6b3SRodney W. Grimes {
9915b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
99220a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
993b99f0a4aSAndrew Moore 	fdc_p	fdc;
9945b81b6b3SRodney W. Grimes 
995b99f0a4aSAndrew Moore #if NFT > 0
996b99f0a4aSAndrew Moore 	/* check for a tape open */
997b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
998b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
999b99f0a4aSAndrew Moore #endif
10005b81b6b3SRodney W. Grimes 	/* check bounds */
1001b99f0a4aSAndrew Moore 	if (fdu >= NFD)
1002b99f0a4aSAndrew Moore 		return(ENXIO);
1003b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
1004b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
1005b99f0a4aSAndrew Moore 		return(ENXIO);
1006b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1007b99f0a4aSAndrew Moore 		return(ENXIO);
10087ca0641bSAndrey A. Chernov 	if (type == 0)
10097ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
10107ca0641bSAndrey A. Chernov 	else {
10117ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
1012fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
10137ca0641bSAndrey A. Chernov 			case FD_360:
10147ca0641bSAndrey A. Chernov 				return(ENXIO);
1015ed2fa05eSAndrey A. Chernov 			case FD_720:
1016b39c878eSAndrey A. Chernov 				if (   type != FD_820
1017b39c878eSAndrey A. Chernov 				    && type != FD_800
1018ed2fa05eSAndrey A. Chernov 				   )
1019ed2fa05eSAndrey A. Chernov 					return(ENXIO);
1020ed2fa05eSAndrey A. Chernov 				break;
10217ca0641bSAndrey A. Chernov 			case FD_1200:
1022b39c878eSAndrey A. Chernov 				switch (type) {
1023b39c878eSAndrey A. Chernov 				case FD_1480:
1024b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1025fa4700b4SAndrey A. Chernov 					break;
10267ca0641bSAndrey A. Chernov 				case FD_1440:
1027b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1028b39c878eSAndrey A. Chernov 					break;
1029b39c878eSAndrey A. Chernov 				case FD_820:
1030b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1031b39c878eSAndrey A. Chernov 					break;
1032b39c878eSAndrey A. Chernov 				case FD_800:
1033b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1034b39c878eSAndrey A. Chernov 					break;
1035b39c878eSAndrey A. Chernov 				case FD_720:
1036b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1037b39c878eSAndrey A. Chernov 					break;
1038b39c878eSAndrey A. Chernov 				case FD_360:
1039b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1040b39c878eSAndrey A. Chernov 					break;
1041b39c878eSAndrey A. Chernov 				default:
1042b39c878eSAndrey A. Chernov 					return(ENXIO);
1043b39c878eSAndrey A. Chernov 				}
1044b39c878eSAndrey A. Chernov 				break;
1045b39c878eSAndrey A. Chernov 			case FD_1440:
1046b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1047b39c878eSAndrey A. Chernov 				    && type != FD_1480
1048ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1049b39c878eSAndrey A. Chernov 				    && type != FD_820
1050b39c878eSAndrey A. Chernov 				    && type != FD_800
1051b39c878eSAndrey A. Chernov 				    && type != FD_720
10527ca0641bSAndrey A. Chernov 				    )
1053dffff499SAndrey A. Chernov 					return(ENXIO);
1054fa4700b4SAndrey A. Chernov 				break;
10557ca0641bSAndrey A. Chernov 			}
10567ca0641bSAndrey A. Chernov 		}
1057fa4700b4SAndrey A. Chernov 	}
1058b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
10595b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
10605b81b6b3SRodney W. Grimes 
10615b81b6b3SRodney W. Grimes 	return 0;
10625b81b6b3SRodney W. Grimes }
10635b81b6b3SRodney W. Grimes 
1064381fe1aaSGarrett Wollman int
1065671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
10665b81b6b3SRodney W. Grimes {
10675b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
1068b99f0a4aSAndrew Moore 
1069b99f0a4aSAndrew Moore #if NFT > 0
10703a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
10713a2f7427SDavid Greenman 
1072b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
10733a2f7427SDavid Greenman 		return ftclose(dev, flags);
1074b99f0a4aSAndrew Moore #endif
10755b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
10763a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
1077dc16046fSJoerg Wunsch 
10785b81b6b3SRodney W. Grimes 	return(0);
10795b81b6b3SRodney W. Grimes }
10805b81b6b3SRodney W. Grimes 
10815b81b6b3SRodney W. Grimes 
10823a2f7427SDavid Greenman /****************************************************************************/
10833a2f7427SDavid Greenman /*                               fdstrategy                                 */
10843a2f7427SDavid Greenman /****************************************************************************/
10853a2f7427SDavid Greenman void
10863a2f7427SDavid Greenman fdstrategy(struct buf *bp)
10873a2f7427SDavid Greenman {
1088bb6382faSJoerg Wunsch 	unsigned nblocks, blknum, cando;
10893a2f7427SDavid Greenman  	int	s;
10903a2f7427SDavid Greenman  	fdcu_t	fdcu;
10913a2f7427SDavid Greenman  	fdu_t	fdu;
10923a2f7427SDavid Greenman  	fdc_p	fdc;
10933a2f7427SDavid Greenman  	fd_p	fd;
10943a2f7427SDavid Greenman 	size_t	fdblk;
10953a2f7427SDavid Greenman 
10963a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
10973a2f7427SDavid Greenman 	fd = &fd_data[fdu];
10983a2f7427SDavid Greenman 	fdc = fd->fdc;
10993a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
11003a2f7427SDavid Greenman 
11013a2f7427SDavid Greenman #if NFT > 0
11023a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
11033a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
1104d3628763SRodney W. Grimes 		bp->b_error = ENODEV;
11053a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11063a2f7427SDavid Greenman 		goto bad;
11073a2f7427SDavid Greenman 	}
11083a2f7427SDavid Greenman 	/* check for controller already busy with tape */
11093a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
11103a2f7427SDavid Greenman 		bp->b_error = EBUSY;
11113a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11123a2f7427SDavid Greenman 		goto bad;
11133a2f7427SDavid Greenman 	}
11143a2f7427SDavid Greenman #endif
1115d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
11163a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
11173a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1118dc5df763SJoerg Wunsch 			printf(
11196a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1120702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
11213a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11223a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11233a2f7427SDavid Greenman 			goto bad;
11243a2f7427SDavid Greenman 		}
11253a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
11263a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11273a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11283a2f7427SDavid Greenman 			goto bad;
11293a2f7427SDavid Greenman 		}
11303a2f7427SDavid Greenman 	}
11313a2f7427SDavid Greenman 
11323a2f7427SDavid Greenman 	/*
11333a2f7427SDavid Greenman 	 * Set up block calculations.
11343a2f7427SDavid Greenman 	 */
1135bb6382faSJoerg Wunsch 	if (bp->b_blkno > 20000000) {
1136bb6382faSJoerg Wunsch 		/*
1137bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1138bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1139bb6382faSJoerg Wunsch 		 */
1140bb6382faSJoerg Wunsch 		bp->b_error = EINVAL;
11413a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11423a2f7427SDavid Greenman 		goto bad;
11433a2f7427SDavid Greenman 	}
1144bb6382faSJoerg Wunsch 	blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1145bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1146bb6382faSJoerg Wunsch 	bp->b_resid = 0;
1147bb6382faSJoerg Wunsch 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1148bb6382faSJoerg Wunsch 		if (blknum <= nblocks) {
1149bb6382faSJoerg Wunsch 			cando = (nblocks - blknum) * fdblk;
1150bb6382faSJoerg Wunsch 			bp->b_resid = bp->b_bcount - cando;
1151bb6382faSJoerg Wunsch 			if (cando == 0)
1152bb6382faSJoerg Wunsch 				goto bad;	/* not actually bad but EOF */
1153bb6382faSJoerg Wunsch 		} else {
1154bb6382faSJoerg Wunsch 			bp->b_error = EINVAL;
1155bb6382faSJoerg Wunsch 			bp->b_flags |= B_ERROR;
1156bb6382faSJoerg Wunsch 			goto bad;
1157bb6382faSJoerg Wunsch 		}
1158bb6382faSJoerg Wunsch 	}
115927513ca7SBruce Evans  	bp->b_pblkno = bp->b_blkno;
11603a2f7427SDavid Greenman 	s = splbio();
116102a19910SJustin T. Gibbs 	bufqdisksort(&fdc->head, bp);
116202a19910SJustin T. Gibbs 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); /* a good idea */
11633a2f7427SDavid Greenman 	fdstart(fdcu);
11643a2f7427SDavid Greenman 	splx(s);
11653a2f7427SDavid Greenman 	return;
11663a2f7427SDavid Greenman 
11673a2f7427SDavid Greenman bad:
11683a2f7427SDavid Greenman 	biodone(bp);
11693a2f7427SDavid Greenman }
11703a2f7427SDavid Greenman 
11715b81b6b3SRodney W. Grimes /***************************************************************\
11725b81b6b3SRodney W. Grimes *				fdstart				*
11735b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
11745b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
11755b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
11765b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
11775b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
11785b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
11795b81b6b3SRodney W. Grimes \***************************************************************/
1180381fe1aaSGarrett Wollman static void
1181dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
11825b81b6b3SRodney W. Grimes {
11835b81b6b3SRodney W. Grimes 	int s;
11845b81b6b3SRodney W. Grimes 
11855b81b6b3SRodney W. Grimes 	s = splbio();
11865b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
11875b81b6b3SRodney W. Grimes 	{
11885b81b6b3SRodney W. Grimes 		fdintr(fdcu);
11895b81b6b3SRodney W. Grimes 	}
11905b81b6b3SRodney W. Grimes 	splx(s);
11915b81b6b3SRodney W. Grimes }
11925b81b6b3SRodney W. Grimes 
1193381fe1aaSGarrett Wollman static void
1194d0917939SPaul Richards fd_timeout(void *arg1)
11955b81b6b3SRodney W. Grimes {
1196381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
11975b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
11983a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
119917542807SPoul-Henning Kamp 	struct buf *bp;
1200f5f7ba03SJordan K. Hubbard 	int s;
12015b81b6b3SRodney W. Grimes 
120202a19910SJustin T. Gibbs 	bp = bufq_first(&fdc_data[fdcu].head);
12035b81b6b3SRodney W. Grimes 
12043a2f7427SDavid Greenman 	/*
12053a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
12063a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
12073a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
12083a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
12093a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
12103a2f7427SDavid Greenman 	 */
12115b81b6b3SRodney W. Grimes 
12123a2f7427SDavid Greenman 	s = splbio();
12133a2f7427SDavid Greenman 
12143a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
12153a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
12163a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
12173a2f7427SDavid Greenman 	{
12183a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
12193a2f7427SDavid Greenman 		/* yup, it is; kill it now */
12203a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
12213a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
12223a2f7427SDavid Greenman 	}
12235b81b6b3SRodney W. Grimes 
12245b81b6b3SRodney W. Grimes 	if (bp)
12255b81b6b3SRodney W. Grimes 	{
12265b81b6b3SRodney W. Grimes 		retrier(fdcu);
12273a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
12285b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
12295b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
12305b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
12315b81b6b3SRodney W. Grimes 	}
12325b81b6b3SRodney W. Grimes 	else
12335b81b6b3SRodney W. Grimes 	{
12345b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
12355b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
12365b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
12375b81b6b3SRodney W. Grimes 	}
1238f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1239f5f7ba03SJordan K. Hubbard 	splx(s);
12405b81b6b3SRodney W. Grimes }
12415b81b6b3SRodney W. Grimes 
12425b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1243381fe1aaSGarrett Wollman static void
1244d0917939SPaul Richards fd_pseudointr(void *arg1)
12455b81b6b3SRodney W. Grimes {
1246381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
12475b81b6b3SRodney W. Grimes 	int	s;
12483a2f7427SDavid Greenman 
12495b81b6b3SRodney W. Grimes 	s = splbio();
12505b81b6b3SRodney W. Grimes 	fdintr(fdcu);
12515b81b6b3SRodney W. Grimes 	splx(s);
12525b81b6b3SRodney W. Grimes }
12535b81b6b3SRodney W. Grimes 
12545b81b6b3SRodney W. Grimes /***********************************************************************\
12555b81b6b3SRodney W. Grimes *                                 fdintr				*
12565b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
12575b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
12585b81b6b3SRodney W. Grimes \***********************************************************************/
1259381fe1aaSGarrett Wollman void
1260381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
12615b81b6b3SRodney W. Grimes {
12625b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1263b99f0a4aSAndrew Moore #if NFT > 0
1264b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1265b99f0a4aSAndrew Moore 
1266b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1267b99f0a4aSAndrew Moore 		(ftintr(fdu));
1268b99f0a4aSAndrew Moore 	else
1269b99f0a4aSAndrew Moore #endif
1270381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1271381fe1aaSGarrett Wollman 			;
12725b81b6b3SRodney W. Grimes }
12735b81b6b3SRodney W. Grimes 
12745b81b6b3SRodney W. Grimes /***********************************************************************\
12755b81b6b3SRodney W. Grimes * The controller state machine.						*
12765b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
12775b81b6b3SRodney W. Grimes \***********************************************************************/
12783a2f7427SDavid Greenman static int
1279dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
12805b81b6b3SRodney W. Grimes {
12814ccc87c5SPoul-Henning Kamp 	int read, format, head, sec = 0, sectrac, st0, cyl, st3;
1282bb6382faSJoerg Wunsch 	unsigned blknum = 0, b_cylinder = 0;
12835b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
12845b81b6b3SRodney W. Grimes 	fd_p fd;
128517542807SPoul-Henning Kamp 	register struct buf *bp;
1286b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
12873a2f7427SDavid Greenman 	size_t fdblk;
12885b81b6b3SRodney W. Grimes 
128902a19910SJustin T. Gibbs 	bp = bufq_first(&fdc->head);
129017542807SPoul-Henning Kamp 	if(!bp) {
12915b81b6b3SRodney W. Grimes 		/***********************************************\
12925b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
12935b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
12945b81b6b3SRodney W. Grimes 		\***********************************************/
12955b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
12965b81b6b3SRodney W. Grimes 		if(fdc->fd)
12975b81b6b3SRodney W. Grimes 		{
12986a0e6f42SRodney W. Grimes 			printf("fd%d: unexpected valid fd pointer\n",
12993a2f7427SDavid Greenman 			       fdc->fdu);
13005b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
13015b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
13025b81b6b3SRodney W. Grimes 		}
13035b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
13045b81b6b3SRodney W. Grimes  		return(0);
13055b81b6b3SRodney W. Grimes 	}
13065b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
13075b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
13083a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
13095b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
13105b81b6b3SRodney W. Grimes 	{
13116a0e6f42SRodney W. Grimes 		printf("fd%d: confused fd pointers\n", fdu);
13125b81b6b3SRodney W. Grimes 	}
13135b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1314b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1315bb6382faSJoerg Wunsch 	if(format) {
1316b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
1317bb6382faSJoerg Wunsch 		fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1318bb6382faSJoerg Wunsch 			- (char *)finfo;
1319bb6382faSJoerg Wunsch 	}
1320bb6382faSJoerg Wunsch 	if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
1321bb6382faSJoerg Wunsch 		blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk +
1322bb6382faSJoerg Wunsch 			fd->skip/fdblk;
1323bb6382faSJoerg Wunsch 		b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1324bb6382faSJoerg Wunsch 	}
13255b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
13265b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
13275b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
132802a19910SJustin T. Gibbs 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle);
132927651b7fSJustin T. Gibbs 	fd->toffhandle = timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
13305b81b6b3SRodney W. Grimes 	switch (fdc->state)
13315b81b6b3SRodney W. Grimes 	{
13325b81b6b3SRodney W. Grimes 	case DEVIDLE:
13335b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
13345b81b6b3SRodney W. Grimes 		fdc->retry = 0;
13355b81b6b3SRodney W. Grimes 		fd->skip = 0;
13365b81b6b3SRodney W. Grimes 		fdc->fd = fd;
13375b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
13383a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
13393a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
13405b81b6b3SRodney W. Grimes 		/*******************************************************\
13415b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
13425b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
13435b81b6b3SRodney W. Grimes 		\*******************************************************/
13445b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
13455b81b6b3SRodney W. Grimes 		{
13465b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13475b81b6b3SRodney W. Grimes 			return(0); /* come back later */
13485b81b6b3SRodney W. Grimes 		}
13495b81b6b3SRodney W. Grimes 		/*******************************************************\
13505b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
13515b81b6b3SRodney W. Grimes 		\*******************************************************/
13525b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
13535b81b6b3SRodney W. Grimes 		{
13545b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13555b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
13565b81b6b3SRodney W. Grimes 			return(0);
13575b81b6b3SRodney W. Grimes 		}
13585b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
13595b81b6b3SRodney W. Grimes 		{
13603a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
13615b81b6b3SRodney W. Grimes 		}
13625e235068SJordan K. Hubbard 		fdc->state = DOSEEK;
13635b81b6b3SRodney W. Grimes 		break;
13645b81b6b3SRodney W. Grimes 	case DOSEEK:
1365bb6382faSJoerg Wunsch 		if (b_cylinder == (unsigned)fd->track)
13665b81b6b3SRodney W. Grimes 		{
13675b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
13685b81b6b3SRodney W. Grimes 			break;
13695b81b6b3SRodney W. Grimes 		}
1370dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1371bb6382faSJoerg Wunsch 			   fd->fdsu, b_cylinder * fd->ft->steptrac,
1372dc5df763SJoerg Wunsch 			   0))
1373dc8603e3SJoerg Wunsch 		{
1374dc8603e3SJoerg Wunsch 			/*
1375dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1376dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1377dc8603e3SJoerg Wunsch 			 */
1378dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1379dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1380dc8603e3SJoerg Wunsch 		}
1381dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
13825b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
13835b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13845b81b6b3SRodney W. Grimes 	case SEEKWAIT:
13855b81b6b3SRodney W. Grimes 		/* allow heads to settle */
138604b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
13875b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
13885b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13895b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
13905b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1391dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
13925b81b6b3SRodney W. Grimes 		{
1393bb6382faSJoerg Wunsch 			int descyl = b_cylinder * fd->ft->steptrac;
13943a2f7427SDavid Greenman 			do {
13953a2f7427SDavid Greenman 				/*
1396dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1397dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1398dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1399dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1400dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1401dc5df763SJoerg Wunsch 				 *
1402dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1403dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1404dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1405dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1406dc5df763SJoerg Wunsch 				 * real interrupt condition.
1407dc5df763SJoerg Wunsch 				 *
1408dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1409dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1410dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1411dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1412dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1413dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1414dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
14153a2f7427SDavid Greenman 				 */
1416dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1417dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1418dc5df763SJoerg Wunsch 					return 0;
1419dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1420dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1421dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
14223a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1423dc5df763SJoerg Wunsch 
14243a2f7427SDavid Greenman 			if (0 == descyl)
14253a2f7427SDavid Greenman 			{
1426dc5df763SJoerg Wunsch 				int failed = 0;
14273a2f7427SDavid Greenman 				/*
14283a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
14293a2f7427SDavid Greenman 				 * really there
14303a2f7427SDavid Greenman 				 */
1431dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1432dc5df763SJoerg Wunsch 					failed = 1;
14333a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
14343a2f7427SDavid Greenman 					printf(
14353a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
14363a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1437dc5df763SJoerg Wunsch 					failed = 1;
1438dc5df763SJoerg Wunsch 				}
1439dc5df763SJoerg Wunsch 
1440dc5df763SJoerg Wunsch 				if (failed)
1441dc5df763SJoerg Wunsch 				{
14423a2f7427SDavid Greenman 					if(fdc->retry < 3)
14433a2f7427SDavid Greenman 						fdc->retry = 3;
14443a2f7427SDavid Greenman 					return(retrier(fdcu));
14453a2f7427SDavid Greenman 				}
14463a2f7427SDavid Greenman 			}
1447dc5df763SJoerg Wunsch 
14485b81b6b3SRodney W. Grimes 			if (cyl != descyl)
14495b81b6b3SRodney W. Grimes 			{
14503a2f7427SDavid Greenman 				printf(
14513a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
14522d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
14535b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
14545b81b6b3SRodney W. Grimes 			}
14555b81b6b3SRodney W. Grimes 		}
14565b81b6b3SRodney W. Grimes 
1457bb6382faSJoerg Wunsch 		fd->track = b_cylinder;
14585b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
14593a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
14605b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
14615b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
14625b81b6b3SRodney W. Grimes 		head = sec / sectrac;
14635b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
14643a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
14653a2f7427SDavid Greenman 
14663a2f7427SDavid Greenman 		if(format || !read)
14673a2f7427SDavid Greenman 		{
14683a2f7427SDavid Greenman 			/* make sure the drive is writable */
1469dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1470dc8603e3SJoerg Wunsch 			{
1471dc8603e3SJoerg Wunsch 				/* stuck controller? */
1472dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1473dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1474dc8603e3SJoerg Wunsch 			}
14753a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
14763a2f7427SDavid Greenman 			{
14773a2f7427SDavid Greenman 				/*
14783a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
14793a2f7427SDavid Greenman 				 * in order to force the current operation
14803a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
14813a2f7427SDavid Greenman 				 * error - all error handling is done
14823a2f7427SDavid Greenman 				 * by the retrier()
14833a2f7427SDavid Greenman 				 */
14843a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
14853a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
14863a2f7427SDavid Greenman 				fdc->status[2] = 0;
14873a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
14883a2f7427SDavid Greenman 				fdc->status[4] = head;
14893a2f7427SDavid Greenman 				fdc->status[5] = sec;
14903a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
14913a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
14923a2f7427SDavid Greenman 				return (1);
14933a2f7427SDavid Greenman 			}
14943a2f7427SDavid Greenman 		}
14955b81b6b3SRodney W. Grimes 
1496b39c878eSAndrey A. Chernov 		if(format)
1497b39c878eSAndrey A. Chernov 		{
1498b39c878eSAndrey A. Chernov 			/* formatting */
1499dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1500dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1501dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1502dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1503dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1504dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1505dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1506dc5df763SJoerg Wunsch 				  0))
1507dc8603e3SJoerg Wunsch 			{
1508dc8603e3SJoerg Wunsch 				/* controller fell over */
1509dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1510dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1511dc8603e3SJoerg Wunsch 			}
1512b39c878eSAndrey A. Chernov 		}
1513b39c878eSAndrey A. Chernov 		else
1514b39c878eSAndrey A. Chernov 		{
1515dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1516dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1517dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1518dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1519dc5df763SJoerg Wunsch 				   head,
1520dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1521dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1522dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1523dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1524dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1525dc5df763SJoerg Wunsch 				   0))
15265b81b6b3SRodney W. Grimes 			{
1527dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1528dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1529dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
15305b81b6b3SRodney W. Grimes 			}
1531b39c878eSAndrey A. Chernov 		}
15325b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
153302a19910SJustin T. Gibbs 		fd->tohandle = timeout(fd_timeout, (caddr_t)fdcu, hz);
15345b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15355b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
153602a19910SJustin T. Gibbs 		untimeout(fd_timeout, (caddr_t)fdcu, fd->tohandle);
1537dc5df763SJoerg Wunsch 
1538dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
15395b81b6b3SRodney W. Grimes 		{
1540dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1541dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1542dc5df763SJoerg Wunsch 			return retrier(fdcu);
15435b81b6b3SRodney W. Grimes   		}
1544dc5df763SJoerg Wunsch 
15453a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1546dc5df763SJoerg Wunsch 
15473a2f7427SDavid Greenman 		/* FALLTHROUGH */
1548dc5df763SJoerg Wunsch 
15493a2f7427SDavid Greenman 	case IOTIMEDOUT:
15505b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
15513a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
15523a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
15535b81b6b3SRodney W. Grimes 		{
15543a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15553a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1556b39c878eSAndrey A. Chernov                                 /*
15573a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
15583a2f7427SDavid Greenman 				 * and didn't release it in time for the
15593a2f7427SDavid Greenman 				 * next FDC transfer.
15603a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
15613a2f7427SDavid Greenman 				 * count. (vak)
1562b39c878eSAndrey A. Chernov                                  */
1563b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1564b39c878eSAndrey A. Chernov                                 return (1);
1565b39c878eSAndrey A. Chernov                         }
15663a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
15673a2f7427SDavid Greenman 				&& fdc->retry < 6)
15683a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
15693a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15703a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
15713a2f7427SDavid Greenman 				&& fdc->retry < 3)
15723a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
15735b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
15745b81b6b3SRodney W. Grimes 		}
15755b81b6b3SRodney W. Grimes 		/* All OK */
15763a2f7427SDavid Greenman 		fd->skip += fdblk;
1577bb6382faSJoerg Wunsch 		if (!format && fd->skip < bp->b_bcount - bp->b_resid)
15785b81b6b3SRodney W. Grimes 		{
15795b81b6b3SRodney W. Grimes 			/* set up next transfer */
15805b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
15815b81b6b3SRodney W. Grimes 		}
15825b81b6b3SRodney W. Grimes 		else
15835b81b6b3SRodney W. Grimes 		{
15845b81b6b3SRodney W. Grimes 			/* ALL DONE */
15855b81b6b3SRodney W. Grimes 			fd->skip = 0;
158602a19910SJustin T. Gibbs 			bufq_remove(&fdc->head, bp);
15875b81b6b3SRodney W. Grimes 			biodone(bp);
15885b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
15895b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
15905b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
15915b81b6b3SRodney W. Grimes 		}
15925b81b6b3SRodney W. Grimes 		return(1);
15935b81b6b3SRodney W. Grimes 	case RESETCTLR:
15943a2f7427SDavid Greenman 		fdc_reset(fdc);
15955b81b6b3SRodney W. Grimes 		fdc->retry++;
15965b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
15975b81b6b3SRodney W. Grimes 		break;
15985b81b6b3SRodney W. Grimes 	case STARTRECAL:
15990e317d05SJoerg Wunsch 		/* XXX clear the fdc results from the last reset, if any. */
16000e317d05SJoerg Wunsch 		{
16010e317d05SJoerg Wunsch 			int i;
16020e317d05SJoerg Wunsch 			for (i = 0; i < 4; i++)
16030e317d05SJoerg Wunsch 				(void)fd_sense_int(fdc, &st0, &cyl);
16040e317d05SJoerg Wunsch 		}
16050e317d05SJoerg Wunsch 
1606dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1607dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1608dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1609dc8603e3SJoerg Wunsch 		{
1610dc8603e3SJoerg Wunsch 			/* arrgl */
1611dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1612dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1613dc8603e3SJoerg Wunsch 		}
16145b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
16155b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
16165b81b6b3SRodney W. Grimes 	case RECALWAIT:
16175b81b6b3SRodney W. Grimes 		/* allow heads to settle */
161804b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
16195b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
16205b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
16215b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
16223a2f7427SDavid Greenman 		do {
16233a2f7427SDavid Greenman 			/*
1624dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
16253a2f7427SDavid Greenman 			 */
1626dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1627dc5df763SJoerg Wunsch 				return 0;
1628dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1629dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1630dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
16313a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
16323a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
16335b81b6b3SRodney W. Grimes 		{
1634dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1635dc8603e3SJoerg Wunsch 				/*
1636dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1637dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1638dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1639dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1640dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1641dc8603e3SJoerg Wunsch 				 * time in a line
1642dc8603e3SJoerg Wunsch 				 */
1643dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1644dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
16453a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
16465b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
16475b81b6b3SRodney W. Grimes 		}
16485b81b6b3SRodney W. Grimes 		fd->track = 0;
16495b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
16505b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
16515b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16525b81b6b3SRodney W. Grimes 	case MOTORWAIT:
16535b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
16545b81b6b3SRodney W. Grimes 		{
16555b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
16565b81b6b3SRodney W. Grimes 		}
16575e235068SJordan K. Hubbard 		/*
16585e235068SJordan K. Hubbard 		 * since the controller was off, it has lost its
16595e235068SJordan K. Hubbard 		 * idea about the current track it were; thus,
16605e235068SJordan K. Hubbard 		 * recalibrate the bastard
16615e235068SJordan K. Hubbard 		 */
16625e235068SJordan K. Hubbard 		fdc->state = STARTRECAL;
16635b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16645b81b6b3SRodney W. Grimes 	default:
1665dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1666dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1667dac0f2dbSJoerg Wunsch 			printf("FDC status :%lx %lx %lx %lx %lx %lx %lx   ",
16685b81b6b3SRodney W. Grimes 			       fdc->status[0],
16695b81b6b3SRodney W. Grimes 			       fdc->status[1],
16705b81b6b3SRodney W. Grimes 			       fdc->status[2],
16715b81b6b3SRodney W. Grimes 			       fdc->status[3],
16725b81b6b3SRodney W. Grimes 			       fdc->status[4],
16735b81b6b3SRodney W. Grimes 			       fdc->status[5],
16745b81b6b3SRodney W. Grimes 			       fdc->status[6] );
16753a2f7427SDavid Greenman 		else
1676dac0f2dbSJoerg Wunsch 			printf("No status available   ");
1677dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1678dac0f2dbSJoerg Wunsch 		{
1679dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
16805b81b6b3SRodney W. Grimes 			return(0);
16815b81b6b3SRodney W. Grimes 		}
1682dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1683dac0f2dbSJoerg Wunsch 		return(0);
1684dac0f2dbSJoerg Wunsch 	}
1685dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
16865b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
16875b81b6b3SRodney W. Grimes }
16885b81b6b3SRodney W. Grimes 
1689aaf08d94SGarrett Wollman static int
1690f5f7ba03SJordan K. Hubbard retrier(fdcu)
1691f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
16925b81b6b3SRodney W. Grimes {
16935b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
169417542807SPoul-Henning Kamp 	register struct buf *bp;
16955b81b6b3SRodney W. Grimes 
169602a19910SJustin T. Gibbs 	bp = bufq_first(&fdc->head);
16975b81b6b3SRodney W. Grimes 
16983a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
16993a2f7427SDavid Greenman 		goto fail;
17005b81b6b3SRodney W. Grimes 	switch(fdc->retry)
17015b81b6b3SRodney W. Grimes 	{
17025b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
17035b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
17045b81b6b3SRodney W. Grimes 		break;
17055b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
17065b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
17075b81b6b3SRodney W. Grimes 		break;
17085b81b6b3SRodney W. Grimes 	case 6:
17095b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
17105b81b6b3SRodney W. Grimes 		break;
17115b81b6b3SRodney W. Grimes 	case 7:
17125b81b6b3SRodney W. Grimes 		break;
17135b81b6b3SRodney W. Grimes 	default:
17143a2f7427SDavid Greenman 	fail:
17155b81b6b3SRodney W. Grimes 		{
17167ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
17177ca0641bSAndrey A. Chernov 			/* Trick diskerr */
17183a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
17193a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
172092ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
17213a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
17223a2f7427SDavid Greenman 				(struct disklabel *)NULL);
17237ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
1724dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1725dc5df763SJoerg Wunsch 			{
1726dc5df763SJoerg Wunsch 				printf(
1727dc5df763SJoerg Wunsch 			" (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n",
1728dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1729dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1730dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1731dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1732dc5df763SJoerg Wunsch 				       fdc->status[5]);
1733dc5df763SJoerg Wunsch 			}
1734dc5df763SJoerg Wunsch 			else
1735dc5df763SJoerg Wunsch 				printf(" (No status)\n");
17365b81b6b3SRodney W. Grimes 		}
17375b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
17385b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
1739bb6382faSJoerg Wunsch 		bp->b_resid += bp->b_bcount - fdc->fd->skip;
174002a19910SJustin T. Gibbs 		bufq_remove(&fdc->head, bp);
17415b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
17425b81b6b3SRodney W. Grimes 		biodone(bp);
174392ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
17445b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
17455b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1746f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
174792ed385aSRodney W. Grimes 		return(1);
17485b81b6b3SRodney W. Grimes 	}
17495b81b6b3SRodney W. Grimes 	fdc->retry++;
17505b81b6b3SRodney W. Grimes 	return(1);
17515b81b6b3SRodney W. Grimes }
17525b81b6b3SRodney W. Grimes 
1753b39c878eSAndrey A. Chernov static int
1754b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1755b39c878eSAndrey A. Chernov 	dev_t dev;
1756b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1757b39c878eSAndrey A. Chernov 	struct proc *p;
1758b39c878eSAndrey A. Chernov {
1759b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1760b39c878eSAndrey A. Chernov  	fd_p	fd;
1761b39c878eSAndrey A. Chernov 
1762b39c878eSAndrey A. Chernov 	struct buf *bp;
1763b39c878eSAndrey A. Chernov 	int rv = 0, s;
17643a2f7427SDavid Greenman 	size_t fdblk;
1765b39c878eSAndrey A. Chernov 
1766b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1767b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
17683a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1769b39c878eSAndrey A. Chernov 
1770b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1771b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1772b39c878eSAndrey A. Chernov 	if(bp == 0)
1773b39c878eSAndrey A. Chernov 		return ENOBUFS;
177482f5379bSJoerg Wunsch 	/*
177582f5379bSJoerg Wunsch 	 * keep the process from being swapped
177682f5379bSJoerg Wunsch 	 */
177782f5379bSJoerg Wunsch 	p->p_flag |= P_PHYSIO;
1778b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1779b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1780b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1781b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1782b39c878eSAndrey A. Chernov 
1783b39c878eSAndrey A. Chernov 	/*
1784b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1785b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1786b39c878eSAndrey A. Chernov 	 */
1787b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
17883a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1789b39c878eSAndrey A. Chernov 
1790b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
17915e235068SJordan K. Hubbard 	bp->b_un.b_addr = (caddr_t)finfo;
1792b39c878eSAndrey A. Chernov 
1793b39c878eSAndrey A. Chernov 	/* now do the format */
1794b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1795b39c878eSAndrey A. Chernov 
1796b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1797b39c878eSAndrey A. Chernov 	s = splbio();
1798b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1799b39c878eSAndrey A. Chernov 	{
18005e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1801b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1802b39c878eSAndrey A. Chernov 			break;
1803b39c878eSAndrey A. Chernov 	}
1804b39c878eSAndrey A. Chernov 	splx(s);
1805b39c878eSAndrey A. Chernov 
180682f5379bSJoerg Wunsch 	if(rv == EWOULDBLOCK) {
1807b39c878eSAndrey A. Chernov 		/* timed out */
1808b39c878eSAndrey A. Chernov 		rv = EIO;
180982f5379bSJoerg Wunsch 		biodone(bp);
181082f5379bSJoerg Wunsch 	}
18113a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
18123a2f7427SDavid Greenman 		rv = bp->b_error;
181382f5379bSJoerg Wunsch 	/*
181482f5379bSJoerg Wunsch 	 * allow the process to be swapped
181582f5379bSJoerg Wunsch 	 */
181682f5379bSJoerg Wunsch 	p->p_flag &= ~P_PHYSIO;
1817b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1818b39c878eSAndrey A. Chernov 	return rv;
1819b39c878eSAndrey A. Chernov }
1820b39c878eSAndrey A. Chernov 
1821f5f7ba03SJordan K. Hubbard /*
1822671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
1823f5f7ba03SJordan K. Hubbard  */
18245b81b6b3SRodney W. Grimes 
1825f5f7ba03SJordan K. Hubbard int
1826b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1827f5f7ba03SJordan K. Hubbard 	dev_t dev;
1828f5f7ba03SJordan K. Hubbard 	int cmd;
1829f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1830f5f7ba03SJordan K. Hubbard 	int flag;
1831b39c878eSAndrey A. Chernov 	struct proc *p;
1832f5f7ba03SJordan K. Hubbard {
18333a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
18343a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
18353a2f7427SDavid Greenman 	size_t fdblk;
18363a2f7427SDavid Greenman 
1837f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1838f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1839f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
18403a2f7427SDavid Greenman 	int error = 0;
1841f5f7ba03SJordan K. Hubbard 
1842b99f0a4aSAndrew Moore #if NFT > 0
1843a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1844a60eff27SNate Williams 
1845a60eff27SNate Williams 	/* check for a tape ioctl */
1846a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1847b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1848b99f0a4aSAndrew Moore #endif
1849b99f0a4aSAndrew Moore 
18503a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1851f5f7ba03SJordan K. Hubbard 
1852f5f7ba03SJordan K. Hubbard 	switch (cmd)
1853f5f7ba03SJordan K. Hubbard 	{
1854f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1855f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1856f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
18573a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
185892ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1859f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1860f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1861f5f7ba03SJordan K. Hubbard 
1862191e1a59SBruce Evans 		if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl)
1863191e1a59SBruce Evans 		    == NULL)
1864f5f7ba03SJordan K. Hubbard 			error = 0;
1865f5f7ba03SJordan K. Hubbard 		else
1866f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1867f5f7ba03SJordan K. Hubbard 
1868f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1869f5f7ba03SJordan K. Hubbard 		break;
1870f5f7ba03SJordan K. Hubbard 
1871f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1872f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1873f5f7ba03SJordan K. Hubbard 			error = EBADF;
1874f5f7ba03SJordan K. Hubbard 		break;
1875f5f7ba03SJordan K. Hubbard 
1876f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1877f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1878f5f7ba03SJordan K. Hubbard 			error = EBADF;
1879f5f7ba03SJordan K. Hubbard 		break;
1880f5f7ba03SJordan K. Hubbard 
1881f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1882f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1883f5f7ba03SJordan K. Hubbard 		{
1884f5f7ba03SJordan K. Hubbard 			error = EBADF;
1885f5f7ba03SJordan K. Hubbard 			break;
1886f5f7ba03SJordan K. Hubbard 		}
1887f5f7ba03SJordan K. Hubbard 
1888f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1889f5f7ba03SJordan K. Hubbard 
1890191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
1891191e1a59SBruce Evans 					  (u_long)0)) != 0)
1892f5f7ba03SJordan K. Hubbard 			break;
1893f5f7ba03SJordan K. Hubbard 
1894b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
189554c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
1896b39c878eSAndrey A. Chernov 		break;
1897b39c878eSAndrey A. Chernov 
1898b39c878eSAndrey A. Chernov 	case FD_FORM:
1899b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1900b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1901b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1902b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1903b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1904b39c878eSAndrey A. Chernov 		else
1905b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1906b39c878eSAndrey A. Chernov 		break;
1907b39c878eSAndrey A. Chernov 
1908b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1909b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1910f5f7ba03SJordan K. Hubbard 		break;
1911f5f7ba03SJordan K. Hubbard 
19123a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
19133a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
19143a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
19153a2f7427SDavid Greenman 			return EPERM;
19163a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
19173a2f7427SDavid Greenman 		break;
19183a2f7427SDavid Greenman 
19193a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
19203a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
19213a2f7427SDavid Greenman 		break;
19223a2f7427SDavid Greenman 
19233a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
19243a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
19253a2f7427SDavid Greenman 		break;
19263a2f7427SDavid Greenman 
1927f5f7ba03SJordan K. Hubbard 	default:
19283a2f7427SDavid Greenman 		error = ENOTTY;
1929f5f7ba03SJordan K. Hubbard 		break;
1930f5f7ba03SJordan K. Hubbard 	}
1931f5f7ba03SJordan K. Hubbard 	return (error);
1932f5f7ba03SJordan K. Hubbard }
1933f5f7ba03SJordan K. Hubbard 
19347146c13eSJulian Elischer 
19357146c13eSJulian Elischer static fd_devsw_installed = 0;
19367146c13eSJulian Elischer 
193787f6c662SJulian Elischer static void 	fd_drvinit(void *notused )
19387146c13eSJulian Elischer {
193987f6c662SJulian Elischer 
19407146c13eSJulian Elischer 	if( ! fd_devsw_installed ) {
1941cba8a5ddSPoul-Henning Kamp 		bdevsw_add_generic(BDEV_MAJOR,CDEV_MAJOR, &fd_bdevsw);
19427146c13eSJulian Elischer 		fd_devsw_installed = 1;
19437146c13eSJulian Elischer 	}
19447146c13eSJulian Elischer }
194587f6c662SJulian Elischer 
194687f6c662SJulian Elischer SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL)
194787f6c662SJulian Elischer 
1948f5f7ba03SJordan K. Hubbard #endif
19493a2f7427SDavid Greenman /*
19503a2f7427SDavid Greenman  * Hello emacs, these are the
19513a2f7427SDavid Greenman  * Local Variables:
19523a2f7427SDavid Greenman  *  c-indent-level:               8
19533a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
19543a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
19553a2f7427SDavid Greenman  *  c-brace-offset:              -8
19563a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
19573a2f7427SDavid Greenman  *  c-argdecl-indent:             8
19583a2f7427SDavid Greenman  *  c-label-offset:              -8
19593a2f7427SDavid Greenman  *  c++-hanging-braces:           1
19603a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
19613a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
19623a2f7427SDavid Greenman  *  c++-friend-offset:            0
19633a2f7427SDavid Greenman  * End:
19643a2f7427SDavid Greenman  */
1965