xref: /freebsd/sys/dev/fdc/fdc.c (revision f7ea2f55d11712e31e448896b4d1bda81be24e35)
1f7ea2f55SJulian Elischer 
287f6c662SJulian Elischer /*
35b81b6b3SRodney W. Grimes  * Copyright (c) 1990 The Regents of the University of California.
45b81b6b3SRodney W. Grimes  * All rights reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
75b81b6b3SRodney W. Grimes  * Don Ahn.
85b81b6b3SRodney W. Grimes  *
9dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994 by
103a2f7427SDavid Greenman  *  jc@irbs.UUCP (John Capo)
113a2f7427SDavid Greenman  *  vak@zebub.msk.su (Serge Vakulenko)
123a2f7427SDavid Greenman  *  ache@astral.msk.su (Andrew A. Chernov)
13dc16046fSJoerg Wunsch  *
14dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994, 1995 by
153a2f7427SDavid Greenman  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
16dc5df763SJoerg Wunsch  *  dufault@hda.com (Peter Dufault)
173a2f7427SDavid Greenman  *
185b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
195b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
205b81b6b3SRodney W. Grimes  * are met:
215b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
225b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
235b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
245b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
255b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
265b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
275b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
285b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
295b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
305b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
315b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
325b81b6b3SRodney W. Grimes  *    without specific prior written permission.
335b81b6b3SRodney W. Grimes  *
345b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
355b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
365b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
375b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
385b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
395b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
405b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
415b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
425b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
435b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
445b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
455b81b6b3SRodney W. Grimes  *
46dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
47f7ea2f55SJulian Elischer  *	$Id: fd.c,v 1.114 1998/06/07 19:40:39 dfr Exp $
485b81b6b3SRodney W. Grimes  *
495b81b6b3SRodney W. Grimes  */
505b81b6b3SRodney W. Grimes 
51b99f0a4aSAndrew Moore #include "ft.h"
52b99f0a4aSAndrew Moore #if NFT < 1
53b99f0a4aSAndrew Moore #undef NFDC
54b99f0a4aSAndrew Moore #endif
555b81b6b3SRodney W. Grimes #include "fd.h"
567b778b5eSEivind Eklund #include "opt_devfs.h"
57d2fb4892SJoerg Wunsch #include "opt_fdc.h"
585b81b6b3SRodney W. Grimes 
59b99f0a4aSAndrew Moore #if NFDC > 0
60b99f0a4aSAndrew Moore 
61b99f0a4aSAndrew Moore #include <sys/param.h>
62b99f0a4aSAndrew Moore #include <sys/systm.h>
63b99f0a4aSAndrew Moore #include <sys/kernel.h>
64b99f0a4aSAndrew Moore #include <sys/conf.h>
653ac4d1efSBruce Evans #include <sys/fcntl.h>
66671e2ceeSBruce Evans #include <machine/clock.h>
67b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h>
68b99f0a4aSAndrew Moore #include <sys/disklabel.h>
69b99f0a4aSAndrew Moore #include <sys/buf.h>
70b99f0a4aSAndrew Moore #include <sys/malloc.h>
713a2f7427SDavid Greenman #include <sys/proc.h>
72b99f0a4aSAndrew Moore #include <sys/syslog.h>
73d4319080SBruce Evans #ifdef notyet
7492200632SGarrett Wollman #include <sys/dkstat.h>
75d4319080SBruce Evans #endif
76f540b106SGarrett Wollman #include <i386/isa/isa.h>
77f540b106SGarrett Wollman #include <i386/isa/isa_device.h>
78f540b106SGarrett Wollman #include <i386/isa/fdreg.h>
79f540b106SGarrett Wollman #include <i386/isa/fdc.h>
80f540b106SGarrett Wollman #include <i386/isa/rtc.h>
81dc5df763SJoerg Wunsch #include <machine/stdarg.h>
8287eafbcaSPoul-Henning Kamp #if NFT > 0
8387eafbcaSPoul-Henning Kamp #include <sys/ftape.h>
8487eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h>
8587eafbcaSPoul-Henning Kamp #endif
868af5d536SJulian Elischer #ifdef	DEVFS
878af5d536SJulian Elischer #include <sys/devfsext.h>
883e425b96SJulian Elischer #ifdef	SLICE
893e425b96SJulian Elischer #include <sys/device.h>
903e425b96SJulian Elischer #include <dev/slice/slice.h>
913e425b96SJulian Elischer #endif	/* SLICE */
923e425b96SJulian Elischer #endif	/* DEVFS */
935b81b6b3SRodney W. Grimes 
94b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
95b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
965b81b6b3SRodney W. Grimes 
970722d6abSJoerg Wunsch /* configuration flags */
980722d6abSJoerg Wunsch #define FDC_PRETEND_D0	(1 << 0)	/* pretend drive 0 to be there */
990722d6abSJoerg Wunsch 
1000722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */
1010722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED	0x1000
1020722d6abSJoerg Wunsch 
1033a2f7427SDavid Greenman /*
1043a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
1053a2f7427SDavid Greenman  * on the controller
1063a2f7427SDavid Greenman  */
1073a2f7427SDavid Greenman #define id_physid id_scsiid
1083a2f7427SDavid Greenman 
109dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
110dc5df763SJoerg Wunsch #define FD_FAILED -1
111dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
112dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
113dc5df763SJoerg Wunsch 
114b39c878eSAndrey A. Chernov #define NUMTYPES 14
115b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1167ca0641bSAndrey A. Chernov 
1173a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
118b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
119b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
120b99f0a4aSAndrew Moore #define FD_1720         1
121b99f0a4aSAndrew Moore #define FD_1480         2
122b99f0a4aSAndrew Moore #define FD_1440         3
123b99f0a4aSAndrew Moore #define FD_1200         4
124b99f0a4aSAndrew Moore #define FD_820          5
125b99f0a4aSAndrew Moore #define FD_800          6
126b99f0a4aSAndrew Moore #define FD_720          7
127b99f0a4aSAndrew Moore #define FD_360          8
128ed2fa05eSAndrey A. Chernov 
129b99f0a4aSAndrew Moore #define FD_1480in5_25   9
130b99f0a4aSAndrew Moore #define FD_1440in5_25   10
131b99f0a4aSAndrew Moore #define FD_820in5_25    11
132b99f0a4aSAndrew Moore #define FD_800in5_25    12
133b99f0a4aSAndrew Moore #define FD_720in5_25    13
134b99f0a4aSAndrew Moore #define FD_360in5_25    14
135b99f0a4aSAndrew Moore 
1367ca0641bSAndrey A. Chernov 
1376f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
1385b81b6b3SRodney W. Grimes {
139126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
140126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
141126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
142126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
143126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
144126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
145126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
146b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
147ed2fa05eSAndrey A. Chernov 
148126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
149126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
150126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
151126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
152126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
153126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1545b81b6b3SRodney W. Grimes };
1555b81b6b3SRodney W. Grimes 
156b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
157dc16046fSJoerg Wunsch 
1585b81b6b3SRodney W. Grimes /***********************************************************************\
1595b81b6b3SRodney W. Grimes * Per controller structure.						*
1605b81b6b3SRodney W. Grimes \***********************************************************************/
161b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
1625b81b6b3SRodney W. Grimes 
1635b81b6b3SRodney W. Grimes /***********************************************************************\
1645b81b6b3SRodney W. Grimes * Per drive structure.							*
165b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1665b81b6b3SRodney W. Grimes \***********************************************************************/
1676f4e0bebSPoul-Henning Kamp static struct fd_data {
168b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1695b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1703a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1715b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1725b81b6b3SRodney W. Grimes 	int	flags;
1735b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1745b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1755b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1765b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1775b81b6b3SRodney W. Grimes 	int	skip;
1785b81b6b3SRodney W. Grimes 	int	hddrv;
179dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
1805b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1813a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
182d4319080SBruce Evans #ifdef notyet
18392200632SGarrett Wollman 	int	dkunit;		/* disk stats unit number */
184d4319080SBruce Evans #endif
18502a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
18602a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
18787f6c662SJulian Elischer #ifdef DEVFS
1883e425b96SJulian Elischer #ifdef SLICE
1893e425b96SJulian Elischer 	int	unit;		/* as in fd0 */
1903e425b96SJulian Elischer 	void	*bdevs[MAXPARTITIONS];
1913e425b96SJulian Elischer 	void	*cdevs[MAXPARTITIONS];
1923e425b96SJulian Elischer 	struct subdev{
1933e425b96SJulian Elischer 		struct slice		*slice;
1943e425b96SJulian Elischer 		int			 minor;
1953e425b96SJulian Elischer 		struct fd_data		*drive;
1963e425b96SJulian Elischer 		struct slicelimits	 limit;
1973e425b96SJulian Elischer 	}subdevs[16];
1983e425b96SJulian Elischer 	struct intr_config_hook ich;
1993e425b96SJulian Elischer #else	/* SLICE */
20021519754SBruce Evans 	void	*bdevs[1 + NUMDENS + MAXPARTITIONS];
20121519754SBruce Evans 	void	*cdevs[1 + NUMDENS + MAXPARTITIONS];
2023e425b96SJulian Elischer #endif	/* SLICE */
20387f6c662SJulian Elischer #endif
2045b81b6b3SRodney W. Grimes } fd_data[NFD];
2055b81b6b3SRodney W. Grimes 
2065b81b6b3SRodney W. Grimes /***********************************************************************\
2075b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
2085b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
2095b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
2105b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
2115b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
2125b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
2135b81b6b3SRodney W. Grimes \***********************************************************************/
214b99f0a4aSAndrew Moore 
2153a2f7427SDavid Greenman #if NFT > 0
2163a2f7427SDavid Greenman int ftopen(dev_t, int);
2173a2f7427SDavid Greenman int ftintr(ftu_t ftu);
2183a2f7427SDavid Greenman int ftclose(dev_t, int);
2193a2f7427SDavid Greenman void ftstrategy(struct buf *);
2203a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
2213a2f7427SDavid Greenman int ftdump(dev_t);
2223a2f7427SDavid Greenman int ftsize(dev_t);
22374fa89f4SRodney W. Grimes int ftattach(struct isa_device *, struct isa_device *, int);
2243a2f7427SDavid Greenman #endif
2255b81b6b3SRodney W. Grimes 
2263a2f7427SDavid Greenman /* autoconfig functions */
2273a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2283a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2293a2f7427SDavid Greenman 
2303a2f7427SDavid Greenman /* needed for ft driver, thus exported */
2313a2f7427SDavid Greenman int in_fdc(fdcu_t);
2323a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
2333a2f7427SDavid Greenman 
2343a2f7427SDavid Greenman /* internal functions */
2353a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
2363a2f7427SDavid Greenman #  define TURNON 1
2373a2f7427SDavid Greenman #  define TURNOFF 0
2383a2f7427SDavid Greenman static timeout_t fd_turnoff;
2393a2f7427SDavid Greenman static timeout_t fd_motor_on;
2403a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2413a2f7427SDavid Greenman static void fdc_reset(fdc_p);
242b5e8ce9fSBruce Evans static int fd_in(fdcu_t, int *);
2433a2f7427SDavid Greenman static void fdstart(fdcu_t);
2443a2f7427SDavid Greenman static timeout_t fd_timeout;
2453a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2463a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
247aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2483e425b96SJulian Elischer #ifndef SLICE
2493a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2503e425b96SJulian Elischer #endif
2513a2f7427SDavid Greenman 
252d66c374fSTor Egge static int enable_fifo(fdc_p fdc);
253d66c374fSTor Egge 
254d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
255d66c374fSTor Egge 
256aaf08d94SGarrett Wollman 
2575b81b6b3SRodney W. Grimes #define DEVIDLE		0
2585b81b6b3SRodney W. Grimes #define FINDWORK	1
2595b81b6b3SRodney W. Grimes #define	DOSEEK		2
2605b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
2615b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
2625b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
2635b81b6b3SRodney W. Grimes #define	STARTRECAL	6
2645b81b6b3SRodney W. Grimes #define	RESETCTLR	7
2655b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
2665b81b6b3SRodney W. Grimes #define	RECALWAIT	9
2675b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
2685b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
2695b81b6b3SRodney W. Grimes 
270d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
271cba2a7c6SBruce Evans static char const * const fdstates[] =
2725b81b6b3SRodney W. Grimes {
2735b81b6b3SRodney W. Grimes "DEVIDLE",
2745b81b6b3SRodney W. Grimes "FINDWORK",
2755b81b6b3SRodney W. Grimes "DOSEEK",
2765b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2775b81b6b3SRodney W. Grimes "IOCOMPLETE",
2785b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2795b81b6b3SRodney W. Grimes "STARTRECAL",
2805b81b6b3SRodney W. Grimes "RESETCTLR",
2815b81b6b3SRodney W. Grimes "SEEKWAIT",
2825b81b6b3SRodney W. Grimes "RECALWAIT",
2835b81b6b3SRodney W. Grimes "MOTORWAIT",
2845b81b6b3SRodney W. Grimes "IOTIMEDOUT"
2855b81b6b3SRodney W. Grimes };
2865b81b6b3SRodney W. Grimes 
2873a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
288cba2a7c6SBruce Evans static int volatile fd_debug = 0;
2895b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2905b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
291d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
2925b81b6b3SRodney W. Grimes #define TRACE0(arg)
2935b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
294d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
2955b81b6b3SRodney W. Grimes 
296dc16046fSJoerg Wunsch /* autoconfig structure */
297dc16046fSJoerg Wunsch 
298dc16046fSJoerg Wunsch struct	isa_driver fdcdriver = {
299dc16046fSJoerg Wunsch 	fdprobe, fdattach, "fdc",
300dc16046fSJoerg Wunsch };
301dc16046fSJoerg Wunsch 
30287f6c662SJulian Elischer static	d_open_t	Fdopen;	/* NOTE, not fdopen */
303f7ea2f55SJulian Elischer static	d_read_t	fdread;
304f7ea2f55SJulian Elischer static	d_write_t	fdwrite;
30587f6c662SJulian Elischer static	d_close_t	fdclose;
30687f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
30787f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
30887f6c662SJulian Elischer 
3093e425b96SJulian Elischer /* even if SLICE defined, these are needed for the ft support. */
31087f6c662SJulian Elischer #define CDEV_MAJOR 9
31187f6c662SJulian Elischer #define BDEV_MAJOR 2
312f7ea2f55SJulian Elischer 
313f7ea2f55SJulian Elischer 
314f7ea2f55SJulian Elischer static struct cdevsw fd_cdevsw = {
315f7ea2f55SJulian Elischer 	  Fdopen,	fdclose,	fdread,	fdwrite,
316f7ea2f55SJulian Elischer 	  fdioctl,	nostop,		nullreset,	nodevtotty,
317f7ea2f55SJulian Elischer 	  seltrue,	nommap,		fdstrategy,	"fd",
318f7ea2f55SJulian Elischer 	  NULL,		-1,		nodump,		nopsize,
319f7ea2f55SJulian Elischer 	  D_DISK,	0,		-1 };
32087f6c662SJulian Elischer 
32187f6c662SJulian Elischer 
3226f4e0bebSPoul-Henning Kamp static struct isa_device *fdcdevs[NFDC];
32392200632SGarrett Wollman 
3243e425b96SJulian Elischer #ifdef	SLICE
3253e425b96SJulian Elischer static sl_h_IO_req_t	fdsIOreq;	/* IO req downward (to device) */
3263e425b96SJulian Elischer static sl_h_ioctl_t	fdsioctl;	/* ioctl req downward (to device) */
3273e425b96SJulian Elischer static sl_h_open_t	fdsopen;	/* downwards travelling open */
32826d3bf5fSJulian Elischer /*static sl_h_close_t	fdsclose; */	/* downwards travelling close */
3293e425b96SJulian Elischer static void	fdsinit(void *);
3303e425b96SJulian Elischer 
3313e425b96SJulian Elischer static struct slice_handler slicetype = {
3323e425b96SJulian Elischer 	"floppy",
3333e425b96SJulian Elischer 	0,
3343e425b96SJulian Elischer 	NULL,
3353e425b96SJulian Elischer 	0,
3363e425b96SJulian Elischer 	NULL,	/* constructor */
3373e425b96SJulian Elischer 	&fdsIOreq,
3383e425b96SJulian Elischer 	&fdsioctl,
3393e425b96SJulian Elischer 	&fdsopen,
34026d3bf5fSJulian Elischer 	/*&fdsclose*/NULL,
3413e425b96SJulian Elischer 	NULL,	/* revoke */
3423e425b96SJulian Elischer 	NULL,	/* claim */
3433e425b96SJulian Elischer 	NULL,	/* verify */
3449b7408feSJulian Elischer 	NULL,	/* upconfig */
3459b7408feSJulian Elischer 	NULL	/* dump */
3463e425b96SJulian Elischer };
3473e425b96SJulian Elischer #endif	/* SLICE */
3483e425b96SJulian Elischer 
349dc5df763SJoerg Wunsch static int
350dc5df763SJoerg Wunsch fdc_err(fdcu_t fdcu, const char *s)
351dc5df763SJoerg Wunsch {
352dc5df763SJoerg Wunsch 	fdc_data[fdcu].fdc_errs++;
35316b04b6aSJoerg Wunsch 	if(s) {
354dc5df763SJoerg Wunsch 		if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX)
3556a0e6f42SRodney W. Grimes 			printf("fdc%d: %s", fdcu, s);
356dc5df763SJoerg Wunsch 		else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX)
357dc5df763SJoerg Wunsch 			printf("fdc%d: too many errors, not logging any more\n",
358dc5df763SJoerg Wunsch 			       fdcu);
35916b04b6aSJoerg Wunsch 	}
360dc5df763SJoerg Wunsch 
361dc5df763SJoerg Wunsch 	return FD_FAILED;
362dc5df763SJoerg Wunsch }
363dc5df763SJoerg Wunsch 
364dc5df763SJoerg Wunsch /*
365dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
366dc5df763SJoerg Wunsch  * Unit number,
367dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
368dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
369dc5df763SJoerg Wunsch  */
370dc5df763SJoerg Wunsch 
3716f4e0bebSPoul-Henning Kamp static int
372dc5df763SJoerg Wunsch fd_cmd(fdcu_t fdcu, int n_out, ...)
373dc5df763SJoerg Wunsch {
374dc5df763SJoerg Wunsch 	u_char cmd;
375dc5df763SJoerg Wunsch 	int n_in;
376dc5df763SJoerg Wunsch 	int n;
377dc5df763SJoerg Wunsch 	va_list ap;
378dc5df763SJoerg Wunsch 
379dc5df763SJoerg Wunsch 	va_start(ap, n_out);
380dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
381dc5df763SJoerg Wunsch 	va_end(ap);
382dc5df763SJoerg Wunsch 	va_start(ap, n_out);
383dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
384dc5df763SJoerg Wunsch 	{
385dc5df763SJoerg Wunsch 		if (out_fdc(fdcu, va_arg(ap, int)) < 0)
386dc5df763SJoerg Wunsch 		{
387dc5df763SJoerg Wunsch 			char msg[50];
388dc5df763SJoerg Wunsch 			sprintf(msg,
389dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
390dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
391dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
392dc5df763SJoerg Wunsch 		}
393dc5df763SJoerg Wunsch 	}
394dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
395dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
396dc5df763SJoerg Wunsch 	{
397dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
398dc5df763SJoerg Wunsch 		if (fd_in(fdcu, ptr) < 0)
399dc5df763SJoerg Wunsch 		{
400dc5df763SJoerg Wunsch 			char msg[50];
401dc5df763SJoerg Wunsch 			sprintf(msg,
402dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
403dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
404dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
405dc5df763SJoerg Wunsch 		}
406dc5df763SJoerg Wunsch 	}
407dc5df763SJoerg Wunsch 
408dc5df763SJoerg Wunsch 	return 0;
409dc5df763SJoerg Wunsch }
410dc5df763SJoerg Wunsch 
4116f4e0bebSPoul-Henning Kamp static int
412d66c374fSTor Egge enable_fifo(fdc_p fdc)
413d66c374fSTor Egge {
414d66c374fSTor Egge 	int i, j;
415d66c374fSTor Egge 
416d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
417d66c374fSTor Egge 
418d66c374fSTor Egge 		/*
419d66c374fSTor Egge 		 * XXX:
420d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
421d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
422d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
423d66c374fSTor Egge 		 */
424d66c374fSTor Egge 
425d66c374fSTor Egge 		if (out_fdc(fdc->fdcu, I8207X_CONFIGURE) < 0)
426d66c374fSTor Egge 			return fdc_err(fdc->fdcu, "Enable FIFO failed\n");
427d66c374fSTor Egge 
428d66c374fSTor Egge 		/* If command is invalid, return */
429d66c374fSTor Egge 		j = 100000;
430d66c374fSTor Egge 		while ((i = inb(fdc->baseport + FDSTS) & (NE7_DIO | NE7_RQM))
431d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
432d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
433d66c374fSTor Egge 				fdc_reset(fdc);
434d66c374fSTor Egge 				return FD_FAILED;
435d66c374fSTor Egge 			}
436d66c374fSTor Egge 		if (j<0 ||
437d66c374fSTor Egge 		    fd_cmd(fdc->fdcu, 3,
438d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
439d66c374fSTor Egge 			fdc_reset(fdc);
440d66c374fSTor Egge 			return fdc_err(fdc->fdcu, "Enable FIFO failed\n");
441d66c374fSTor Egge 		}
442d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
443d66c374fSTor Egge 		return 0;
444d66c374fSTor Egge 	}
445d66c374fSTor Egge 	if (fd_cmd(fdc->fdcu, 4,
446d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
447d66c374fSTor Egge 		return fdc_err(fdc->fdcu, "Re-enable FIFO failed\n");
448d66c374fSTor Egge 	return 0;
449d66c374fSTor Egge }
450d66c374fSTor Egge 
451d66c374fSTor Egge static int
452dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
453dc5df763SJoerg Wunsch {
454dc5df763SJoerg Wunsch 	int st3;
455dc5df763SJoerg Wunsch 
456dc5df763SJoerg Wunsch 	if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
457dc5df763SJoerg Wunsch 	{
4586a0e6f42SRodney W. Grimes 		return fdc_err(fdc->fdcu, "Sense Drive Status failed\n");
459dc5df763SJoerg Wunsch 	}
460dc5df763SJoerg Wunsch 	if (st3p)
461dc5df763SJoerg Wunsch 		*st3p = st3;
462dc5df763SJoerg Wunsch 
463dc5df763SJoerg Wunsch 	return 0;
464dc5df763SJoerg Wunsch }
465dc5df763SJoerg Wunsch 
4666f4e0bebSPoul-Henning Kamp static int
467dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
468dc5df763SJoerg Wunsch {
469dc5df763SJoerg Wunsch 	int st0, cyl;
470dc5df763SJoerg Wunsch 
471dc5df763SJoerg Wunsch 	int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0);
472dc5df763SJoerg Wunsch 
473dc5df763SJoerg Wunsch 	if (ret)
474dc5df763SJoerg Wunsch 	{
475dc5df763SJoerg Wunsch 		(void)fdc_err(fdc->fdcu,
476dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
477dc5df763SJoerg Wunsch 		return ret;
478dc5df763SJoerg Wunsch 	}
479dc5df763SJoerg Wunsch 
480dc5df763SJoerg Wunsch 	if (st0p)
481dc5df763SJoerg Wunsch 		*st0p = st0;
482dc5df763SJoerg Wunsch 
483dc5df763SJoerg Wunsch 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV)
484dc5df763SJoerg Wunsch 	{
485dc5df763SJoerg Wunsch 		/*
486dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
487dc5df763SJoerg Wunsch 		 */
488dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
489dc5df763SJoerg Wunsch 	}
490dc5df763SJoerg Wunsch 
491dc5df763SJoerg Wunsch 	if (fd_in(fdc->fdcu, &cyl) < 0)
492dc5df763SJoerg Wunsch 	{
493dc5df763SJoerg Wunsch 		return fdc_err(fdc->fdcu, "can't get cyl num\n");
494dc5df763SJoerg Wunsch 	}
495dc5df763SJoerg Wunsch 
496dc5df763SJoerg Wunsch 	if (cylp)
497dc5df763SJoerg Wunsch 		*cylp = cyl;
498dc5df763SJoerg Wunsch 
499dc5df763SJoerg Wunsch 	return 0;
500dc5df763SJoerg Wunsch }
501dc5df763SJoerg Wunsch 
502dc5df763SJoerg Wunsch 
5036f4e0bebSPoul-Henning Kamp static int
504dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
505dc5df763SJoerg Wunsch {
506dc5df763SJoerg Wunsch 	int i, ret;
507b5e8ce9fSBruce Evans 
508dc5df763SJoerg Wunsch 	for (i = 0; i < 7; i++)
509dc5df763SJoerg Wunsch 	{
510b5e8ce9fSBruce Evans 		/*
511b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
512b5e8ce9fSBruce Evans 		 * from the hardware, but fdc_status wants u_longs and
513b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
514b5e8ce9fSBruce Evans 		 */
515b5e8ce9fSBruce Evans 		int status;
516b5e8ce9fSBruce Evans 
517b5e8ce9fSBruce Evans 		ret = fd_in(fdc->fdcu, &status);
518b5e8ce9fSBruce Evans 		fdc->status[i] = status;
519b5e8ce9fSBruce Evans 		if (ret != 0)
520dc5df763SJoerg Wunsch 			break;
521dc5df763SJoerg Wunsch 	}
522dc5df763SJoerg Wunsch 
523dc5df763SJoerg Wunsch 	if (ret == 0)
524dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
525dc5df763SJoerg Wunsch 	else
526dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
527dc5df763SJoerg Wunsch 
528dc5df763SJoerg Wunsch 	return ret;
529dc5df763SJoerg Wunsch }
530dc5df763SJoerg Wunsch 
5315b81b6b3SRodney W. Grimes /****************************************************************************/
5325b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
5335b81b6b3SRodney W. Grimes /****************************************************************************/
534dc5df763SJoerg Wunsch 
5355b81b6b3SRodney W. Grimes /*
5365b81b6b3SRodney W. Grimes  * probe for existance of controller
5375b81b6b3SRodney W. Grimes  */
5383a2f7427SDavid Greenman static int
539dc5df763SJoerg Wunsch fdprobe(struct isa_device *dev)
5405b81b6b3SRodney W. Grimes {
5415b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5425b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
5435b81b6b3SRodney W. Grimes 	{
5446a0e6f42SRodney W. Grimes 		printf("fdc%d: unit used multiple times\n", fdcu);
5455b81b6b3SRodney W. Grimes 		return 0;
5465b81b6b3SRodney W. Grimes 	}
5475b81b6b3SRodney W. Grimes 
54892200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
5495b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
5505b81b6b3SRodney W. Grimes 
55116111cedSAndrew Moore 	/* First - lets reset the floppy controller */
5523a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
55316111cedSAndrew Moore 	DELAY(100);
5543a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
55516111cedSAndrew Moore 
5565b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
557dc5df763SJoerg Wunsch 	if (fd_cmd(fdcu,
558dc5df763SJoerg Wunsch 		   3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
559dc5df763SJoerg Wunsch 		   0))
5605b81b6b3SRodney W. Grimes 	{
5615b81b6b3SRodney W. Grimes 		return(0);
5625b81b6b3SRodney W. Grimes 	}
5635b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
5645b81b6b3SRodney W. Grimes }
5655b81b6b3SRodney W. Grimes 
5665b81b6b3SRodney W. Grimes /*
5675b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
5685b81b6b3SRodney W. Grimes  */
5693a2f7427SDavid Greenman static int
570dc5df763SJoerg Wunsch fdattach(struct isa_device *dev)
5715b81b6b3SRodney W. Grimes {
5723a2f7427SDavid Greenman 	unsigned fdt;
5735b81b6b3SRodney W. Grimes 	fdu_t	fdu;
5745b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5755b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
5765b81b6b3SRodney W. Grimes 	fd_p	fd;
577cba2a7c6SBruce Evans 	int	fdsu, st0, st3, i;
578cba2a7c6SBruce Evans #if NFT > 0
579cba2a7c6SBruce Evans 	int	unithasfd;
580cba2a7c6SBruce Evans #endif
581b99f0a4aSAndrew Moore 	struct isa_device *fdup;
582dc5df763SJoerg Wunsch 	int ic_type = 0;
583999422d7SJulian Elischer #ifdef DEVFS
5843e425b96SJulian Elischer #ifdef	SLICE
5853e425b96SJulian Elischer 	char namebuf[64];
5863e425b96SJulian Elischer #else
587c8f2fe8dSBruce Evans 	int	mynor;
58821519754SBruce Evans 	int	typemynor;
5893e425b96SJulian Elischer #endif	/* SLICE */
59021519754SBruce Evans 	int	typesize;
59121519754SBruce Evans #endif
59292200632SGarrett Wollman 
5935b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
5945b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
5955b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
596100f78bbSSujal Patel 	/* Acquire the DMA channel forever, The driver will do the rest */
597100f78bbSSujal Patel 	isa_dma_acquire(fdc->dmachan);
598dd87702aSBruce Evans 	isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
5995b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
6003a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
6013a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
60202a19910SJustin T. Gibbs 	bufq_init(&fdc->head);
6035b81b6b3SRodney W. Grimes 
6045b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
605b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
606b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
607b99f0a4aSAndrew Moore 			continue;
608b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
609b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
610b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
611b99f0a4aSAndrew Moore 			continue;
612b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
613b99f0a4aSAndrew Moore 		/* look up what bios thinks we have */
614b99f0a4aSAndrew Moore 		switch (fdu) {
6150722d6abSJoerg Wunsch 			case 0: if (dev->id_flags & FDC_PRETEND_D0)
6160722d6abSJoerg Wunsch 					fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
6170722d6abSJoerg Wunsch 				else
6180722d6abSJoerg Wunsch 					fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
619b99f0a4aSAndrew Moore 				break;
620b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
621b99f0a4aSAndrew Moore 				break;
622b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
623b99f0a4aSAndrew Moore 				break;
624b99f0a4aSAndrew Moore 		}
6255b81b6b3SRodney W. Grimes 		/* is there a unit? */
626b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
627b99f0a4aSAndrew Moore #if NFT > 0
628b99f0a4aSAndrew Moore 		    || (fdsu >= DRVS_PER_CTLR)) {
629b99f0a4aSAndrew Moore #else
630b99f0a4aSAndrew Moore 		) {
63156ef0285SAndrew Moore 			fd->type = NO_TYPE;
632b99f0a4aSAndrew Moore #endif
633b99f0a4aSAndrew Moore #if NFT > 0
634b99f0a4aSAndrew Moore 			/* If BIOS says no floppy, or > 2nd device */
635b99f0a4aSAndrew Moore 			/* Probe for and attach a floppy tape.     */
63674fa89f4SRodney W. Grimes 			/* Tell FT if there was already a disk     */
63774fa89f4SRodney W. Grimes 			/* with this unit number found.            */
63874fa89f4SRodney W. Grimes 
63974fa89f4SRodney W. Grimes 			unithasfd = 0;
64074fa89f4SRodney W. Grimes 			if (fdu < NFD && fd->type != NO_TYPE)
64174fa89f4SRodney W. Grimes 				unithasfd = 1;
64285827d9cSJoerg Wunsch 			if (ftattach(dev, fdup, unithasfd))
643b99f0a4aSAndrew Moore 				continue;
64456ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
645b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
64656ef0285SAndrew Moore #endif
6475b81b6b3SRodney W. Grimes 			continue;
648f5f7ba03SJordan K. Hubbard 		}
6495b81b6b3SRodney W. Grimes 
6505b81b6b3SRodney W. Grimes 		/* select it */
6513a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
6526b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
653dc5df763SJoerg Wunsch 
654dc5df763SJoerg Wunsch 		if (ic_type == 0 &&
655dc5df763SJoerg Wunsch 		    fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
656dc5df763SJoerg Wunsch 		{
657d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6586a0e6f42SRodney W. Grimes 			printf("fdc%d: ", fdcu);
659d2fb4892SJoerg Wunsch #endif
660dc5df763SJoerg Wunsch 			ic_type = (u_char)ic_type;
661dc5df763SJoerg Wunsch 			switch( ic_type ) {
662dc5df763SJoerg Wunsch 			case 0x80:
663d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6646a0e6f42SRodney W. Grimes 				printf("NEC 765\n");
665d2fb4892SJoerg Wunsch #endif
666dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE765;
667dc5df763SJoerg Wunsch 				break;
668dc5df763SJoerg Wunsch 			case 0x81:
669d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6706a0e6f42SRodney W. Grimes 				printf("Intel 82077\n");
671d2fb4892SJoerg Wunsch #endif
672dc5df763SJoerg Wunsch 				fdc->fdct = FDC_I82077;
673dc5df763SJoerg Wunsch 				break;
674dc5df763SJoerg Wunsch 			case 0x90:
675d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6766a0e6f42SRodney W. Grimes 				printf("NEC 72065B\n");
677d2fb4892SJoerg Wunsch #endif
678dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE72065;
679dc5df763SJoerg Wunsch 				break;
680dc5df763SJoerg Wunsch 			default:
681d2fb4892SJoerg Wunsch #ifdef FDC_PRINT_BOGUS_CHIPTYPE
6826a0e6f42SRodney W. Grimes 				printf("unknown IC type %02x\n", ic_type);
683d2fb4892SJoerg Wunsch #endif
684dc5df763SJoerg Wunsch 				fdc->fdct = FDC_UNKNOWN;
685dc5df763SJoerg Wunsch 				break;
6866b7bd95bSJoerg Wunsch 			}
687d66c374fSTor Egge 			if (fdc->fdct != FDC_NE765 &&
688d66c374fSTor Egge 			    fdc->fdct != FDC_UNKNOWN &&
689d66c374fSTor Egge 			    enable_fifo(fdc) == 0) {
690d66c374fSTor Egge 				printf("fdc%d: FIFO enabled", fdcu);
691d66c374fSTor Egge 				printf(", %d bytes threshold\n",
692d66c374fSTor Egge 				       fifo_threshold);
693d66c374fSTor Egge 			}
694dc5df763SJoerg Wunsch 		}
695dc5df763SJoerg Wunsch 		if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
696dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
697dc5df763SJoerg Wunsch 			/* if at track 0, first seek inwards */
698dc5df763SJoerg Wunsch 			/* seek some steps: */
699dc5df763SJoerg Wunsch 			(void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
700dc5df763SJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
701dc5df763SJoerg Wunsch 			(void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
702dc5df763SJoerg Wunsch 		}
703dc5df763SJoerg Wunsch 
704dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
705dc5df763SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
706dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
707dc5df763SJoerg Wunsch 			/* Seek some steps... */
708dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
709dc5df763SJoerg Wunsch 				/* ...wait a moment... */
710dc5df763SJoerg Wunsch 				DELAY(300000);
711dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
712dc5df763SJoerg Wunsch 				(void)fd_sense_int(fdc, 0, 0);
713dc5df763SJoerg Wunsch 			}
714dc5df763SJoerg Wunsch 		}
715dc5df763SJoerg Wunsch 
7166b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
7176b7bd95bSJoerg Wunsch 			/*
7186b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
7196b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
7206b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
7216b7bd95bSJoerg Wunsch 			 * more than 77 steps
7226b7bd95bSJoerg Wunsch 			 */
723dc5df763SJoerg Wunsch 			/* go back to 0: */
724dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
7256b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
7266b7bd95bSJoerg Wunsch 				DELAY(i == 0? 1000000: 300000);
7275b81b6b3SRodney W. Grimes 
7286b7bd95bSJoerg Wunsch 				/* anything responding? */
729dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
730dc5df763SJoerg Wunsch 				(st0 & NE7_ST0_EC) == 0)
7316b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
7326b7bd95bSJoerg Wunsch 			}
733dc5df763SJoerg Wunsch 		}
7346b7bd95bSJoerg Wunsch 
7353a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
7363a2f7427SDavid Greenman 
7373a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
7385b81b6b3SRodney W. Grimes 			continue;
7395b81b6b3SRodney W. Grimes 
740dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
7413e425b96SJulian Elischer #ifdef	SLICE
7423e425b96SJulian Elischer 		fd->unit = fdu;
7433e425b96SJulian Elischer #endif
744b99f0a4aSAndrew Moore 		fd->fdc = fdc;
745b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
7463a2f7427SDavid Greenman 		fd->options = 0;
74702a19910SJustin T. Gibbs 		callout_handle_init(&fd->toffhandle);
74802a19910SJustin T. Gibbs 		callout_handle_init(&fd->tohandle);
7492d9d0204SRodney W. Grimes 		printf("fd%d: ", fdu);
7505b81b6b3SRodney W. Grimes 
751b99f0a4aSAndrew Moore 		switch (fdt) {
7527ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
7536a0e6f42SRodney W. Grimes 			printf("1.2MB 5.25in\n");
754b99f0a4aSAndrew Moore 			fd->type = FD_1200;
7557ca0641bSAndrey A. Chernov 			break;
7560722d6abSJoerg Wunsch 		case RTCFDT_144M | RTCFDT_144M_PRETENDED:
7570722d6abSJoerg Wunsch 			printf("config-pretended ");
7580722d6abSJoerg Wunsch 			fdt = RTCFDT_144M;
7590722d6abSJoerg Wunsch 			/* fallthrough */
7607ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
7616a0e6f42SRodney W. Grimes 			printf("1.44MB 3.5in\n");
762b99f0a4aSAndrew Moore 			fd->type = FD_1440;
7637ca0641bSAndrey A. Chernov 			break;
764290dd077SJoerg Wunsch 		case RTCFDT_288M:
76586a727d9SJoerg Wunsch 		case RTCFDT_288M_1:
7666a0e6f42SRodney W. Grimes 			printf("2.88MB 3.5in - 1.44MB mode\n");
767290dd077SJoerg Wunsch 			fd->type = FD_1440;
768290dd077SJoerg Wunsch 			break;
7697ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
7706a0e6f42SRodney W. Grimes 			printf("360KB 5.25in\n");
771b99f0a4aSAndrew Moore 			fd->type = FD_360;
7727ca0641bSAndrey A. Chernov 			break;
773ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
7746a0e6f42SRodney W. Grimes 			printf("720KB 3.5in\n");
775b99f0a4aSAndrew Moore 			fd->type = FD_720;
776ed2fa05eSAndrey A. Chernov 			break;
7777ca0641bSAndrey A. Chernov 		default:
7786a0e6f42SRodney W. Grimes 			printf("unknown\n");
779b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
78021519754SBruce Evans 			continue;
7815b81b6b3SRodney W. Grimes 		}
782999422d7SJulian Elischer #ifdef DEVFS
7833e425b96SJulian Elischer #ifdef SLICE
7843e425b96SJulian Elischer 		sprintf(namebuf,"fd%d",fdu);
7853e425b96SJulian Elischer 		fd->subdevs[0].minor = 0;
7863e425b96SJulian Elischer 		fd->subdevs[0].drive = fd;
7873e425b96SJulian Elischer 		fd->subdevs[0].limit.blksize =
7883e425b96SJulian Elischer 			128 << (fd_types[fd->type - 1].secsize);
7893e425b96SJulian Elischer 		fd->subdevs[0].limit.slicesize =
7903e425b96SJulian Elischer 			fd_types[fd->type - 1].size
7913e425b96SJulian Elischer 				* fd->subdevs[0].limit.blksize;
7923e425b96SJulian Elischer 		fd->ft = fd_types + (fd->type - 1); /* default value */
7933e425b96SJulian Elischer 		sl_make_slice(&slicetype,
7943e425b96SJulian Elischer 			&fd->subdevs[0],
7953e425b96SJulian Elischer 			&fd->subdevs[0].limit,
7963e425b96SJulian Elischer 		 	&fd->subdevs[0].slice,
7973e425b96SJulian Elischer 			NULL,
7983e425b96SJulian Elischer 			namebuf);
7993e425b96SJulian Elischer 		/* Allow full probing */
8003e425b96SJulian Elischer 		fd->subdevs[0].slice->probeinfo.typespecific = NULL;
8013e425b96SJulian Elischer 		fd->subdevs[0].slice->probeinfo.type = NULL;
8023e425b96SJulian Elischer 
8033e425b96SJulian Elischer 		fd->ich.ich_func = fdsinit;
8043e425b96SJulian Elischer 		fd->ich.ich_arg = &fd->subdevs[0];
8053e425b96SJulian Elischer 		config_intrhook_establish(&fd->ich);
8063e425b96SJulian Elischer #else	/* SLICE */
80721519754SBruce Evans 		mynor = fdu << 6;
808f7ea2f55SJulian Elischer 		fd->bdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_BLK,
809f85120acSBruce Evans 						UID_ROOT, GID_OPERATOR, 0640,
81021519754SBruce Evans 						"fd%d", fdu);
81121519754SBruce Evans 		fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR,
812f85120acSBruce Evans 						UID_ROOT, GID_OPERATOR, 0640,
81321519754SBruce Evans 						"rfd%d", fdu);
8143e425b96SJulian Elischer #endif	/* SLICE */
81521519754SBruce Evans 		for (i = 1; i < 1 + NUMDENS; i++) {
81621519754SBruce Evans 			/*
81721519754SBruce Evans 			 * XXX this and the lookup in Fdopen() should be
81821519754SBruce Evans 			 * data driven.
81921519754SBruce Evans 			 */
82021519754SBruce Evans 			switch (fd->type) {
82121519754SBruce Evans 			case FD_360:
82221519754SBruce Evans 				if (i != FD_360)
82321519754SBruce Evans 					continue;
82421519754SBruce Evans 				break;
82521519754SBruce Evans 			case FD_720:
82621519754SBruce Evans 				if (i != FD_720 && i != FD_800 && i != FD_820)
82721519754SBruce Evans 					continue;
82821519754SBruce Evans 				break;
82921519754SBruce Evans 			case FD_1200:
83021519754SBruce Evans 				if (i != FD_360 && i != FD_720 && i != FD_800
83121519754SBruce Evans 				    && i != FD_820 && i != FD_1200
83221519754SBruce Evans 				    && i != FD_1440 && i != FD_1480)
83321519754SBruce Evans 					continue;
83421519754SBruce Evans 				break;
83521519754SBruce Evans 			case FD_1440:
83621519754SBruce Evans 				if (i != FD_720 && i != FD_800 && i != FD_820
83721519754SBruce Evans 				    && i != FD_1200 && i != FD_1440
83821519754SBruce Evans 				    && i != FD_1480 && i != FD_1720)
83921519754SBruce Evans 					continue;
84021519754SBruce Evans 				break;
84121519754SBruce Evans 			}
84221519754SBruce Evans 			typesize = fd_types[i - 1].size / 2;
84321519754SBruce Evans 			/*
84421519754SBruce Evans 			 * XXX all these conversions give bloated code and
84521519754SBruce Evans 			 * confusing names.
84621519754SBruce Evans 			 */
84721519754SBruce Evans 			if (typesize == 1476)
84821519754SBruce Evans 				typesize = 1480;
84921519754SBruce Evans 			if (typesize == 1722)
85021519754SBruce Evans 				typesize = 1720;
8513e425b96SJulian Elischer #ifdef SLICE
8523e425b96SJulian Elischer 			sprintf(namebuf,"fd%d.%d",fdu,typesize);
8533e425b96SJulian Elischer 			fd->subdevs[i].minor = i;
8543e425b96SJulian Elischer 			fd->subdevs[i].drive = fd;
8553e425b96SJulian Elischer 			fd->subdevs[i].limit.blksize =
8563e425b96SJulian Elischer 				128 << (fd_types[i - 1].secsize);
8573e425b96SJulian Elischer 			fd->subdevs[i].limit.slicesize =
8583e425b96SJulian Elischer 				fd_types[i - 1].size
8593e425b96SJulian Elischer 					* fd->subdevs[i].limit.blksize;
8603e425b96SJulian Elischer 			sl_make_slice(&slicetype,
8613e425b96SJulian Elischer 				&fd->subdevs[i],
8623e425b96SJulian Elischer 				&fd->subdevs[i].limit,
8633e425b96SJulian Elischer 			 	&fd->subdevs[i].slice,
8643e425b96SJulian Elischer 				NULL,
8653e425b96SJulian Elischer 				namebuf);
8663e425b96SJulian Elischer 			/* Allow full probing */
8673e425b96SJulian Elischer 			fd->subdevs[i].slice->probeinfo.typespecific = NULL;
8683e425b96SJulian Elischer 			fd->subdevs[i].slice->probeinfo.type = NO_SUBPART;
8693e425b96SJulian Elischer 		}
8703e425b96SJulian Elischer #else	/* SLICE */
8713e425b96SJulian Elischer 			typemynor = mynor | i;
87221519754SBruce Evans 			fd->bdevs[i] =
873f7ea2f55SJulian Elischer 				devfs_add_devswf(&fd_cdevsw, typemynor, DV_BLK,
87421519754SBruce Evans 						 UID_ROOT, GID_OPERATOR, 0640,
87521519754SBruce Evans 						 "fd%d.%d", fdu, typesize);
87621519754SBruce Evans 			fd->cdevs[i] =
87721519754SBruce Evans 				devfs_add_devswf(&fd_cdevsw, typemynor, DV_CHR,
87821519754SBruce Evans 						 UID_ROOT, GID_OPERATOR, 0640,
87921519754SBruce Evans 						 "rfd%d.%d", fdu, typesize);
88021519754SBruce Evans 		}
8813e425b96SJulian Elischer 
88221519754SBruce Evans 		for (i = 0; i < MAXPARTITIONS; i++) {
8833e425b96SJulian Elischer 			fd->bdevs[1 + NUMDENS + i] = devfs_link(fd->bdevs[0],
88421519754SBruce Evans 					   "fd%d%c", fdu, 'a' + i);
88521519754SBruce Evans 			fd->cdevs[1 + NUMDENS + i] =
88621519754SBruce Evans 				devfs_link(fd->cdevs[0],
88721519754SBruce Evans 					   "rfd%d%c", fdu, 'a' + i);
88821519754SBruce Evans 		}
8893e425b96SJulian Elischer #endif /* SLICE */
890999422d7SJulian Elischer #endif /* DEVFS */
891d4319080SBruce Evans #ifdef notyet
89292200632SGarrett Wollman 		if (dk_ndrive < DK_NDRIVE) {
89392200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
8946a0e6f42SRodney W. Grimes 			fd->dkunit = dk_ndrive++;
895671e2ceeSBruce Evans 			/*
896671e2ceeSBruce Evans 			 * XXX assume rate is FDC_500KBPS.
897671e2ceeSBruce Evans 			 */
898671e2ceeSBruce Evans 			dk_wpms[dk_ndrive] = 500000 / 8 / 2;
89992200632SGarrett Wollman 		} else {
90092200632SGarrett Wollman 			fd->dkunit = -1;
90192200632SGarrett Wollman 		}
902d4319080SBruce Evans #endif
9035b81b6b3SRodney W. Grimes 	}
9045b81b6b3SRodney W. Grimes 
9053a2f7427SDavid Greenman 	return (1);
9065b81b6b3SRodney W. Grimes }
9075b81b6b3SRodney W. Grimes 
9083e425b96SJulian Elischer 
9093e425b96SJulian Elischer #ifdef	SLICE
9103e425b96SJulian Elischer 
9113e425b96SJulian Elischer static void
9123e425b96SJulian Elischer fdsinit(void *arg)
9133e425b96SJulian Elischer {
9143e425b96SJulian Elischer 	struct subdev *sd = arg;
9153e425b96SJulian Elischer 	sh_p	tp;
9163e425b96SJulian Elischer 
9173e425b96SJulian Elischer 	if ((tp = slice_probeall(sd->slice)) != NULL) {
9183e425b96SJulian Elischer 		(*tp->constructor)(sd->slice);
9193e425b96SJulian Elischer 	}
9203e425b96SJulian Elischer 	config_intrhook_disestablish(&sd->drive->ich);
9213e425b96SJulian Elischer }
9223e425b96SJulian Elischer #endif	/* SLICE */
9233e425b96SJulian Elischer 
9245b81b6b3SRodney W. Grimes /****************************************************************************/
9255b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
9265e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
9275b81b6b3SRodney W. Grimes /****************************************************************************/
9283a2f7427SDavid Greenman static void
929dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
9305b81b6b3SRodney W. Grimes {
9313a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
9323a2f7427SDavid Greenman 	int needspecify = 0;
9333a2f7427SDavid Greenman 
9343a2f7427SDavid Greenman 	if(turnon) {
9353a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
9363a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
9373a2f7427SDavid Greenman 	} else
9383a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
9393a2f7427SDavid Greenman 
9405e235068SJordan K. Hubbard 	if(!turnon
9415e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
9425e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
9435e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
9445e235068SJordan K. Hubbard 	else {
9453a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
9463a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
9473a2f7427SDavid Greenman 			needspecify = 1;
9483a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
9495b81b6b3SRodney W. Grimes 	}
9505b81b6b3SRodney W. Grimes 
9513a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
9523a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
9533a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
9543a2f7427SDavid Greenman 
9553a2f7427SDavid Greenman 	if(needspecify) {
956dc8603e3SJoerg Wunsch 		/*
957dc5df763SJoerg Wunsch 		 * XXX
958dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
959dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
960dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
961dc8603e3SJoerg Wunsch 		 */
962dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
963dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
964dc5df763SJoerg Wunsch 			     0);
965d66c374fSTor Egge 		if (fdc_data[fdcu].flags & FDC_HAS_FIFO)
966d66c374fSTor Egge 			(void) enable_fifo(&fdc_data[fdcu]);
9673a2f7427SDavid Greenman 	}
9683a2f7427SDavid Greenman }
9693a2f7427SDavid Greenman 
970381fe1aaSGarrett Wollman static void
971d0917939SPaul Richards fd_turnoff(void *arg1)
9725b81b6b3SRodney W. Grimes {
973381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
974f5f7ba03SJordan K. Hubbard 	int	s;
9755b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
976dc16046fSJoerg Wunsch 
977dc16046fSJoerg Wunsch 	TRACE1("[fd%d: turnoff]", fdu);
9788335c1b8SBruce Evans 
9798335c1b8SBruce Evans 	/*
9808335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
9818335c1b8SBruce Evans 	 * XXX shouldn't even schedule turnoff until drive is inactive
9828335c1b8SBruce Evans 	 * and nothing is queued on it.
9838335c1b8SBruce Evans 	 */
9848335c1b8SBruce Evans 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fdu) {
98502a19910SJustin T. Gibbs 		fd->toffhandle = timeout(fd_turnoff, arg1, 4 * hz);
9868335c1b8SBruce Evans 		return;
9878335c1b8SBruce Evans 	}
9888335c1b8SBruce Evans 
989f5f7ba03SJordan K. Hubbard 	s = splbio();
9905b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
9913a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
992f5f7ba03SJordan K. Hubbard 	splx(s);
9935b81b6b3SRodney W. Grimes }
9945b81b6b3SRodney W. Grimes 
9953a2f7427SDavid Greenman static void
996d0917939SPaul Richards fd_motor_on(void *arg1)
9975b81b6b3SRodney W. Grimes {
998381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
999f5f7ba03SJordan K. Hubbard 	int	s;
1000f5f7ba03SJordan K. Hubbard 
10015b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
1002f5f7ba03SJordan K. Hubbard 	s = splbio();
10035b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
10045b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
10055b81b6b3SRodney W. Grimes 	{
1006f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
10075b81b6b3SRodney W. Grimes 	}
1008f5f7ba03SJordan K. Hubbard 	splx(s);
10095b81b6b3SRodney W. Grimes }
10105b81b6b3SRodney W. Grimes 
10113a2f7427SDavid Greenman static void
1012dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
10135b81b6b3SRodney W. Grimes {
10145b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
10155b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
10165b81b6b3SRodney W. Grimes 	{
10173a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
10183a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
10195e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
10205b81b6b3SRodney W. Grimes 	}
10215b81b6b3SRodney W. Grimes }
10225b81b6b3SRodney W. Grimes 
1023381fe1aaSGarrett Wollman static void
1024dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
10255b81b6b3SRodney W. Grimes {
10263a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
10273a2f7427SDavid Greenman 
10283a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
10293a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
10303a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
10313a2f7427SDavid Greenman 	DELAY(100);
10323a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
10333a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
10343a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
10353a2f7427SDavid Greenman 	DELAY(100);
10363a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
10373a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
10383a2f7427SDavid Greenman 
1039dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
1040dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
1041dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1042dc5df763SJoerg Wunsch 		     0);
1043d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1044d66c374fSTor Egge 		(void) enable_fifo(fdc);
10455b81b6b3SRodney W. Grimes }
10465b81b6b3SRodney W. Grimes 
10475b81b6b3SRodney W. Grimes /****************************************************************************/
10485b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
10495b81b6b3SRodney W. Grimes /****************************************************************************/
10505b81b6b3SRodney W. Grimes int
1051dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
10525b81b6b3SRodney W. Grimes {
10535b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
10545b81b6b3SRodney W. Grimes 	int i, j = 100000;
10553a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
10565b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1057dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
10586a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
10595b81b6b3SRodney W. Grimes 	if (j <= 0)
106016b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
1061d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
10623a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
10633a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
10645b81b6b3SRodney W. Grimes 	return(i);
1065d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
10663a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
1067d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
10685b81b6b3SRodney W. Grimes }
10695b81b6b3SRodney W. Grimes 
1070dc5df763SJoerg Wunsch /*
1071dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
1072dc5df763SJoerg Wunsch  */
1073b5e8ce9fSBruce Evans static int
1074dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
1075dc5df763SJoerg Wunsch {
1076dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
1077dc5df763SJoerg Wunsch 	int i, j = 100000;
1078dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
1079dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1080dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
10816a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
1082dc5df763SJoerg Wunsch 	if (j <= 0)
108316b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
1084d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1085dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
1086dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1087dc5df763SJoerg Wunsch 	*ptr = i;
1088dc5df763SJoerg Wunsch 	return 0;
1089d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1090dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
1091dc5df763SJoerg Wunsch 	if (ptr)
1092dc5df763SJoerg Wunsch 		*ptr = i;
1093dc5df763SJoerg Wunsch 	return 0;
1094d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1095dc5df763SJoerg Wunsch }
1096dc5df763SJoerg Wunsch 
1097dc5df763SJoerg Wunsch int
1098dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
10995b81b6b3SRodney W. Grimes {
11005b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
11013b3837dbSRodney W. Grimes 	int i;
11025b81b6b3SRodney W. Grimes 
11033b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
11043b3837dbSRodney W. Grimes 	i = 100000;
11053a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
11066a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
11073b3837dbSRodney W. Grimes 
11083b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
11093b3837dbSRodney W. Grimes 	i = 100000;
11103a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
111116b04b6aSJoerg Wunsch 	if (i <= 0)
111216b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0);
11133b3837dbSRodney W. Grimes 
11143b3837dbSRodney W. Grimes 	/* Send the command and return */
11153a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
11163a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
11175b81b6b3SRodney W. Grimes 	return (0);
11185b81b6b3SRodney W. Grimes }
11195b81b6b3SRodney W. Grimes 
11205b81b6b3SRodney W. Grimes /****************************************************************************/
11215b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
11225b81b6b3SRodney W. Grimes /****************************************************************************/
1123381fe1aaSGarrett Wollman int
1124671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
11255b81b6b3SRodney W. Grimes {
11265b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
112720a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
1128b99f0a4aSAndrew Moore 	fdc_p	fdc;
11295b81b6b3SRodney W. Grimes 
1130b99f0a4aSAndrew Moore #if NFT > 0
1131b99f0a4aSAndrew Moore 	/* check for a tape open */
1132b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
1133b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
1134b99f0a4aSAndrew Moore #endif
11355b81b6b3SRodney W. Grimes 	/* check bounds */
1136b99f0a4aSAndrew Moore 	if (fdu >= NFD)
1137b99f0a4aSAndrew Moore 		return(ENXIO);
1138b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
1139b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
1140b99f0a4aSAndrew Moore 		return(ENXIO);
1141b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1142b99f0a4aSAndrew Moore 		return(ENXIO);
11437ca0641bSAndrey A. Chernov 	if (type == 0)
11447ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
11457ca0641bSAndrey A. Chernov 	else {
11463e425b96SJulian Elischer 		/*
11473e425b96SJulian Elischer 		 * For each type of basic drive, make sure we are trying
11483e425b96SJulian Elischer 		 * to open a type it can do,
11493e425b96SJulian Elischer 		 */
11507ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
1151fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
11527ca0641bSAndrey A. Chernov 			case FD_360:
11537ca0641bSAndrey A. Chernov 				return(ENXIO);
1154ed2fa05eSAndrey A. Chernov 			case FD_720:
1155b39c878eSAndrey A. Chernov 				if (   type != FD_820
1156b39c878eSAndrey A. Chernov 				    && type != FD_800
1157ed2fa05eSAndrey A. Chernov 				   )
1158ed2fa05eSAndrey A. Chernov 					return(ENXIO);
1159ed2fa05eSAndrey A. Chernov 				break;
11607ca0641bSAndrey A. Chernov 			case FD_1200:
1161b39c878eSAndrey A. Chernov 				switch (type) {
1162b39c878eSAndrey A. Chernov 				case FD_1480:
1163b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1164fa4700b4SAndrey A. Chernov 					break;
11657ca0641bSAndrey A. Chernov 				case FD_1440:
1166b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1167b39c878eSAndrey A. Chernov 					break;
1168b39c878eSAndrey A. Chernov 				case FD_820:
1169b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1170b39c878eSAndrey A. Chernov 					break;
1171b39c878eSAndrey A. Chernov 				case FD_800:
1172b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1173b39c878eSAndrey A. Chernov 					break;
1174b39c878eSAndrey A. Chernov 				case FD_720:
1175b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1176b39c878eSAndrey A. Chernov 					break;
1177b39c878eSAndrey A. Chernov 				case FD_360:
1178b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1179b39c878eSAndrey A. Chernov 					break;
1180b39c878eSAndrey A. Chernov 				default:
1181b39c878eSAndrey A. Chernov 					return(ENXIO);
1182b39c878eSAndrey A. Chernov 				}
1183b39c878eSAndrey A. Chernov 				break;
1184b39c878eSAndrey A. Chernov 			case FD_1440:
1185b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1186b39c878eSAndrey A. Chernov 				    && type != FD_1480
1187ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1188b39c878eSAndrey A. Chernov 				    && type != FD_820
1189b39c878eSAndrey A. Chernov 				    && type != FD_800
1190b39c878eSAndrey A. Chernov 				    && type != FD_720
11917ca0641bSAndrey A. Chernov 				    )
1192dffff499SAndrey A. Chernov 					return(ENXIO);
1193fa4700b4SAndrey A. Chernov 				break;
11947ca0641bSAndrey A. Chernov 			}
11957ca0641bSAndrey A. Chernov 		}
1196fa4700b4SAndrey A. Chernov 	}
1197b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
11985b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
11995b81b6b3SRodney W. Grimes 
12005b81b6b3SRodney W. Grimes 	return 0;
12015b81b6b3SRodney W. Grimes }
12025b81b6b3SRodney W. Grimes 
1203381fe1aaSGarrett Wollman int
1204671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
12055b81b6b3SRodney W. Grimes {
12065b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
1207b99f0a4aSAndrew Moore 
1208b99f0a4aSAndrew Moore #if NFT > 0
12093a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
12103a2f7427SDavid Greenman 
1211b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
12123a2f7427SDavid Greenman 		return ftclose(dev, flags);
1213b99f0a4aSAndrew Moore #endif
12145b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
12153a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
1216dc16046fSJoerg Wunsch 
12175b81b6b3SRodney W. Grimes 	return(0);
12185b81b6b3SRodney W. Grimes }
12195b81b6b3SRodney W. Grimes 
1220f7ea2f55SJulian Elischer static int
1221f7ea2f55SJulian Elischer fdread(dev_t dev, struct uio *uio, int ioflag)
1222f7ea2f55SJulian Elischer {
1223f7ea2f55SJulian Elischer 	return (physio(fdstrategy, NULL, dev, 1, minphys, uio));
1224f7ea2f55SJulian Elischer }
1225f7ea2f55SJulian Elischer 
1226f7ea2f55SJulian Elischer static int
1227f7ea2f55SJulian Elischer fdwrite(dev_t dev, struct uio *uio, int ioflag)
1228f7ea2f55SJulian Elischer {
1229f7ea2f55SJulian Elischer 	return (physio(fdstrategy, NULL, dev, 0, minphys, uio));
1230f7ea2f55SJulian Elischer }
1231f7ea2f55SJulian Elischer 
12325b81b6b3SRodney W. Grimes 
12333a2f7427SDavid Greenman /****************************************************************************/
12343a2f7427SDavid Greenman /*                               fdstrategy                                 */
12353a2f7427SDavid Greenman /****************************************************************************/
12363a2f7427SDavid Greenman void
12373a2f7427SDavid Greenman fdstrategy(struct buf *bp)
12383a2f7427SDavid Greenman {
1239bb6382faSJoerg Wunsch 	unsigned nblocks, blknum, cando;
12403a2f7427SDavid Greenman  	int	s;
12413a2f7427SDavid Greenman  	fdcu_t	fdcu;
12423a2f7427SDavid Greenman  	fdu_t	fdu;
12433a2f7427SDavid Greenman  	fdc_p	fdc;
12443a2f7427SDavid Greenman  	fd_p	fd;
12453a2f7427SDavid Greenman 	size_t	fdblk;
12463a2f7427SDavid Greenman 
12473a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
12483a2f7427SDavid Greenman 	fd = &fd_data[fdu];
12493a2f7427SDavid Greenman 	fdc = fd->fdc;
12503a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
12513a2f7427SDavid Greenman 
12523a2f7427SDavid Greenman #if NFT > 0
12533a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
12543a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
1255d3628763SRodney W. Grimes 		bp->b_error = ENODEV;
12563a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
12573a2f7427SDavid Greenman 		goto bad;
12583a2f7427SDavid Greenman 	}
12593a2f7427SDavid Greenman 	/* check for controller already busy with tape */
12603a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
12613a2f7427SDavid Greenman 		bp->b_error = EBUSY;
12623a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
12633a2f7427SDavid Greenman 		goto bad;
12643a2f7427SDavid Greenman 	}
12653a2f7427SDavid Greenman #endif
1266d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
12673a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
12683a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1269dc5df763SJoerg Wunsch 			printf(
12706a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1271702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
12723a2f7427SDavid Greenman 			bp->b_error = EINVAL;
12733a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
12743a2f7427SDavid Greenman 			goto bad;
12753a2f7427SDavid Greenman 		}
12763a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
12773a2f7427SDavid Greenman 			bp->b_error = EINVAL;
12783a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
12793a2f7427SDavid Greenman 			goto bad;
12803a2f7427SDavid Greenman 		}
12813a2f7427SDavid Greenman 	}
12823a2f7427SDavid Greenman 
12833a2f7427SDavid Greenman 	/*
12843a2f7427SDavid Greenman 	 * Set up block calculations.
12853a2f7427SDavid Greenman 	 */
1286bb6382faSJoerg Wunsch 	if (bp->b_blkno > 20000000) {
1287bb6382faSJoerg Wunsch 		/*
1288bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1289bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1290bb6382faSJoerg Wunsch 		 */
1291bb6382faSJoerg Wunsch 		bp->b_error = EINVAL;
12923a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
12933a2f7427SDavid Greenman 		goto bad;
12943a2f7427SDavid Greenman 	}
1295bb6382faSJoerg Wunsch 	blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1296bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1297bb6382faSJoerg Wunsch 	bp->b_resid = 0;
1298bb6382faSJoerg Wunsch 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1299bb6382faSJoerg Wunsch 		if (blknum <= nblocks) {
1300bb6382faSJoerg Wunsch 			cando = (nblocks - blknum) * fdblk;
1301bb6382faSJoerg Wunsch 			bp->b_resid = bp->b_bcount - cando;
1302bb6382faSJoerg Wunsch 			if (cando == 0)
1303bb6382faSJoerg Wunsch 				goto bad;	/* not actually bad but EOF */
1304bb6382faSJoerg Wunsch 		} else {
1305bb6382faSJoerg Wunsch 			bp->b_error = EINVAL;
1306bb6382faSJoerg Wunsch 			bp->b_flags |= B_ERROR;
1307bb6382faSJoerg Wunsch 			goto bad;
1308bb6382faSJoerg Wunsch 		}
1309bb6382faSJoerg Wunsch 	}
131027513ca7SBruce Evans  	bp->b_pblkno = bp->b_blkno;
13113a2f7427SDavid Greenman 	s = splbio();
131202a19910SJustin T. Gibbs 	bufqdisksort(&fdc->head, bp);
131302a19910SJustin T. Gibbs 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); /* a good idea */
13143a2f7427SDavid Greenman 	fdstart(fdcu);
13153a2f7427SDavid Greenman 	splx(s);
13163a2f7427SDavid Greenman 	return;
13173a2f7427SDavid Greenman 
13183a2f7427SDavid Greenman bad:
13193a2f7427SDavid Greenman 	biodone(bp);
13203a2f7427SDavid Greenman }
13213a2f7427SDavid Greenman 
13223e425b96SJulian Elischer #ifdef	SLICE
13233e425b96SJulian Elischer /****************************************************************************/
13243e425b96SJulian Elischer /*                               fdsIOreq                                   */
13253e425b96SJulian Elischer /****************************************************************************/
13263e425b96SJulian Elischer static void
13273e425b96SJulian Elischer fdsIOreq(void *private ,struct buf *bp)
13283e425b96SJulian Elischer {
13293e425b96SJulian Elischer 	unsigned nblocks, blknum, cando;
13303e425b96SJulian Elischer  	int	s;
13313e425b96SJulian Elischer  	fdcu_t	fdcu;
13323e425b96SJulian Elischer  	fdu_t	fdu;
13333e425b96SJulian Elischer  	fdc_p	fdc;
13343e425b96SJulian Elischer  	fd_p	fd;
13353e425b96SJulian Elischer 	size_t	fdblk;
13363e425b96SJulian Elischer 	struct subdev *sd;
13373e425b96SJulian Elischer 
13383e425b96SJulian Elischer 	sd = private;
13393e425b96SJulian Elischer 	fd = sd->drive;
13403e425b96SJulian Elischer  	fdu = fd->unit;
13413e425b96SJulian Elischer 	fdc = fd->fdc;
13423e425b96SJulian Elischer 	fdcu = fdc->fdcu;
13433e425b96SJulian Elischer 
13443e425b96SJulian Elischer 	/* check for controller already busy with tape */
13453e425b96SJulian Elischer 	if (fdc->flags & FDC_TAPE_BUSY) {
13463e425b96SJulian Elischer 		bp->b_error = EBUSY;
13473e425b96SJulian Elischer 		bp->b_flags |= B_ERROR;
13483e425b96SJulian Elischer 		goto bad;
13493e425b96SJulian Elischer 	}
13503e425b96SJulian Elischer 	bp->b_driver1 = sd; /* squirrel away which device.. */
13513e425b96SJulian Elischer 	bp->b_resid = 0;
13523e425b96SJulian Elischer 	s = splbio();
13533e425b96SJulian Elischer 	bufqdisksort(&fdc->head, bp);
13543e425b96SJulian Elischer 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); /* a good idea */
13553e425b96SJulian Elischer 	fdstart(fdcu);
13563e425b96SJulian Elischer 	splx(s);
13573e425b96SJulian Elischer 	return;
13583e425b96SJulian Elischer 
13593e425b96SJulian Elischer bad:
13603e425b96SJulian Elischer 	biodone(bp);
13613e425b96SJulian Elischer 	return;
13623e425b96SJulian Elischer }
13633e425b96SJulian Elischer #endif	/* SLICE */
13643e425b96SJulian Elischer 
13655b81b6b3SRodney W. Grimes /***************************************************************\
13665b81b6b3SRodney W. Grimes *				fdstart				*
13675b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
13685b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
13695b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
13705b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
13715b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
13725b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
13735b81b6b3SRodney W. Grimes \***************************************************************/
1374381fe1aaSGarrett Wollman static void
1375dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
13765b81b6b3SRodney W. Grimes {
13775b81b6b3SRodney W. Grimes 	int s;
13785b81b6b3SRodney W. Grimes 
13795b81b6b3SRodney W. Grimes 	s = splbio();
13805b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
13815b81b6b3SRodney W. Grimes 	{
13825b81b6b3SRodney W. Grimes 		fdintr(fdcu);
13835b81b6b3SRodney W. Grimes 	}
13845b81b6b3SRodney W. Grimes 	splx(s);
13855b81b6b3SRodney W. Grimes }
13865b81b6b3SRodney W. Grimes 
1387381fe1aaSGarrett Wollman static void
1388d0917939SPaul Richards fd_timeout(void *arg1)
13895b81b6b3SRodney W. Grimes {
1390381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
13915b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
13923a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
139317542807SPoul-Henning Kamp 	struct buf *bp;
1394f5f7ba03SJordan K. Hubbard 	int s;
13955b81b6b3SRodney W. Grimes 
139602a19910SJustin T. Gibbs 	bp = bufq_first(&fdc_data[fdcu].head);
13975b81b6b3SRodney W. Grimes 
13983a2f7427SDavid Greenman 	/*
13993a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
14003a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
14013a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
14023a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
14033a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
14043a2f7427SDavid Greenman 	 */
14055b81b6b3SRodney W. Grimes 
14063a2f7427SDavid Greenman 	s = splbio();
14073a2f7427SDavid Greenman 
14083a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
14093a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
14103a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
14113a2f7427SDavid Greenman 	{
14123a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
14133a2f7427SDavid Greenman 		/* yup, it is; kill it now */
14143a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
14153a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
14163a2f7427SDavid Greenman 	}
14175b81b6b3SRodney W. Grimes 
14185b81b6b3SRodney W. Grimes 	if (bp)
14195b81b6b3SRodney W. Grimes 	{
14205b81b6b3SRodney W. Grimes 		retrier(fdcu);
14213a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
14225b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
14235b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
14245b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
14255b81b6b3SRodney W. Grimes 	}
14265b81b6b3SRodney W. Grimes 	else
14275b81b6b3SRodney W. Grimes 	{
14285b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
14295b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
14305b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
14315b81b6b3SRodney W. Grimes 	}
1432f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1433f5f7ba03SJordan K. Hubbard 	splx(s);
14345b81b6b3SRodney W. Grimes }
14355b81b6b3SRodney W. Grimes 
14365b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1437381fe1aaSGarrett Wollman static void
1438d0917939SPaul Richards fd_pseudointr(void *arg1)
14395b81b6b3SRodney W. Grimes {
1440381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
14415b81b6b3SRodney W. Grimes 	int	s;
14423a2f7427SDavid Greenman 
14435b81b6b3SRodney W. Grimes 	s = splbio();
14445b81b6b3SRodney W. Grimes 	fdintr(fdcu);
14455b81b6b3SRodney W. Grimes 	splx(s);
14465b81b6b3SRodney W. Grimes }
14475b81b6b3SRodney W. Grimes 
14485b81b6b3SRodney W. Grimes /***********************************************************************\
14495b81b6b3SRodney W. Grimes *                                 fdintr				*
14505b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
14515b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
14525b81b6b3SRodney W. Grimes \***********************************************************************/
1453381fe1aaSGarrett Wollman void
1454381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
14555b81b6b3SRodney W. Grimes {
14565b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1457b99f0a4aSAndrew Moore #if NFT > 0
1458b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1459b99f0a4aSAndrew Moore 
1460b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1461b99f0a4aSAndrew Moore 		(ftintr(fdu));
1462b99f0a4aSAndrew Moore 	else
1463b99f0a4aSAndrew Moore #endif
1464381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1465381fe1aaSGarrett Wollman 			;
14665b81b6b3SRodney W. Grimes }
14675b81b6b3SRodney W. Grimes 
14685b81b6b3SRodney W. Grimes /***********************************************************************\
14695b81b6b3SRodney W. Grimes * The controller state machine.						*
14705b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
14715b81b6b3SRodney W. Grimes \***********************************************************************/
14723a2f7427SDavid Greenman static int
1473dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
14745b81b6b3SRodney W. Grimes {
14753e425b96SJulian Elischer 	struct subdev *sd;
14764ccc87c5SPoul-Henning Kamp 	int read, format, head, sec = 0, sectrac, st0, cyl, st3;
1477bb6382faSJoerg Wunsch 	unsigned blknum = 0, b_cylinder = 0;
14785b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
14795b81b6b3SRodney W. Grimes 	fd_p fd;
148017542807SPoul-Henning Kamp 	register struct buf *bp;
1481b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
14823a2f7427SDavid Greenman 	size_t fdblk;
14835b81b6b3SRodney W. Grimes 
148402a19910SJustin T. Gibbs 	bp = bufq_first(&fdc->head);
148517542807SPoul-Henning Kamp 	if(!bp) {
14865b81b6b3SRodney W. Grimes 		/***********************************************\
14875b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
14885b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
14895b81b6b3SRodney W. Grimes 		\***********************************************/
14905b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
14915b81b6b3SRodney W. Grimes 		if(fdc->fd)
14925b81b6b3SRodney W. Grimes 		{
14936a0e6f42SRodney W. Grimes 			printf("fd%d: unexpected valid fd pointer\n",
14943a2f7427SDavid Greenman 			       fdc->fdu);
14955b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
14965b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
14975b81b6b3SRodney W. Grimes 		}
14985b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
14995b81b6b3SRodney W. Grimes  		return(0);
15005b81b6b3SRodney W. Grimes 	}
15013e425b96SJulian Elischer #ifdef	SLICE
15023e425b96SJulian Elischer 	sd = bp->b_driver1;
15033e425b96SJulian Elischer 	fd = sd->drive;
15043e425b96SJulian Elischer  	fdu = fd->unit;
15053e425b96SJulian Elischer #else
15065b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
15075b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
15083e425b96SJulian Elischer #endif
15093a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
15105b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
15115b81b6b3SRodney W. Grimes 	{
15126a0e6f42SRodney W. Grimes 		printf("fd%d: confused fd pointers\n", fdu);
15135b81b6b3SRodney W. Grimes 	}
15145b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1515b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1516bb6382faSJoerg Wunsch 	if(format) {
1517ab3f7469SPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->b_data;
1518bb6382faSJoerg Wunsch 		fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1519bb6382faSJoerg Wunsch 			- (char *)finfo;
1520bb6382faSJoerg Wunsch 	}
1521bb6382faSJoerg Wunsch 	if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
15223e425b96SJulian Elischer 		blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
1523bb6382faSJoerg Wunsch 			fd->skip/fdblk;
1524bb6382faSJoerg Wunsch 		b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1525bb6382faSJoerg Wunsch 	}
15265b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
15275b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
15285b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
152902a19910SJustin T. Gibbs 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle);
153027651b7fSJustin T. Gibbs 	fd->toffhandle = timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
15315b81b6b3SRodney W. Grimes 	switch (fdc->state)
15325b81b6b3SRodney W. Grimes 	{
15335b81b6b3SRodney W. Grimes 	case DEVIDLE:
15345b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
15355b81b6b3SRodney W. Grimes 		fdc->retry = 0;
15365b81b6b3SRodney W. Grimes 		fd->skip = 0;
15375b81b6b3SRodney W. Grimes 		fdc->fd = fd;
15385b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
15393a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
15403a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
15415b81b6b3SRodney W. Grimes 		/*******************************************************\
15425b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
1543dc733423SDag-Erling Smørgrav 		* it will start up in its own good time		*
15445b81b6b3SRodney W. Grimes 		\*******************************************************/
15455b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
15465b81b6b3SRodney W. Grimes 		{
15475b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
15485b81b6b3SRodney W. Grimes 			return(0); /* come back later */
15495b81b6b3SRodney W. Grimes 		}
15505b81b6b3SRodney W. Grimes 		/*******************************************************\
15515b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
15525b81b6b3SRodney W. Grimes 		\*******************************************************/
15535b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
15545b81b6b3SRodney W. Grimes 		{
15555b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
15565b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
15575b81b6b3SRodney W. Grimes 			return(0);
15585b81b6b3SRodney W. Grimes 		}
15595b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
15605b81b6b3SRodney W. Grimes 		{
15613a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
15625b81b6b3SRodney W. Grimes 		}
15635e235068SJordan K. Hubbard 		fdc->state = DOSEEK;
15645b81b6b3SRodney W. Grimes 		break;
15655b81b6b3SRodney W. Grimes 	case DOSEEK:
1566bb6382faSJoerg Wunsch 		if (b_cylinder == (unsigned)fd->track)
15675b81b6b3SRodney W. Grimes 		{
15685b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
15695b81b6b3SRodney W. Grimes 			break;
15705b81b6b3SRodney W. Grimes 		}
1571dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1572bb6382faSJoerg Wunsch 			   fd->fdsu, b_cylinder * fd->ft->steptrac,
1573dc5df763SJoerg Wunsch 			   0))
1574dc8603e3SJoerg Wunsch 		{
1575dc8603e3SJoerg Wunsch 			/*
1576dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1577dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1578dc8603e3SJoerg Wunsch 			 */
1579dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1580dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1581dc8603e3SJoerg Wunsch 		}
1582dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
15835b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
15845b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15855b81b6b3SRodney W. Grimes 	case SEEKWAIT:
15865b81b6b3SRodney W. Grimes 		/* allow heads to settle */
158704b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
15885b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
15895b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15905b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
15915b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1592dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
15935b81b6b3SRodney W. Grimes 		{
1594bb6382faSJoerg Wunsch 			int descyl = b_cylinder * fd->ft->steptrac;
15953a2f7427SDavid Greenman 			do {
15963a2f7427SDavid Greenman 				/*
1597dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1598dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1599dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1600dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1601dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1602dc5df763SJoerg Wunsch 				 *
1603dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1604dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1605dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1606dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1607dc5df763SJoerg Wunsch 				 * real interrupt condition.
1608dc5df763SJoerg Wunsch 				 *
1609dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1610dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1611dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1612dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1613dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1614dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1615dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
16163a2f7427SDavid Greenman 				 */
1617dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1618dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1619dc5df763SJoerg Wunsch 					return 0;
1620dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1621dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1622dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
16233a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1624dc5df763SJoerg Wunsch 
16253a2f7427SDavid Greenman 			if (0 == descyl)
16263a2f7427SDavid Greenman 			{
1627dc5df763SJoerg Wunsch 				int failed = 0;
16283a2f7427SDavid Greenman 				/*
16293a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
16303a2f7427SDavid Greenman 				 * really there
16313a2f7427SDavid Greenman 				 */
1632dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1633dc5df763SJoerg Wunsch 					failed = 1;
16343a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
16353a2f7427SDavid Greenman 					printf(
16363a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
16373a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1638dc5df763SJoerg Wunsch 					failed = 1;
1639dc5df763SJoerg Wunsch 				}
1640dc5df763SJoerg Wunsch 
1641dc5df763SJoerg Wunsch 				if (failed)
1642dc5df763SJoerg Wunsch 				{
16433a2f7427SDavid Greenman 					if(fdc->retry < 3)
16443a2f7427SDavid Greenman 						fdc->retry = 3;
16453a2f7427SDavid Greenman 					return(retrier(fdcu));
16463a2f7427SDavid Greenman 				}
16473a2f7427SDavid Greenman 			}
1648dc5df763SJoerg Wunsch 
16495b81b6b3SRodney W. Grimes 			if (cyl != descyl)
16505b81b6b3SRodney W. Grimes 			{
16513a2f7427SDavid Greenman 				printf(
16523a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
16532d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
16545b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
16555b81b6b3SRodney W. Grimes 			}
16565b81b6b3SRodney W. Grimes 		}
16575b81b6b3SRodney W. Grimes 
1658bb6382faSJoerg Wunsch 		fd->track = b_cylinder;
1659ab3f7469SPoul-Henning Kamp 		isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
16603a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
16615b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
16625b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
16635b81b6b3SRodney W. Grimes 		head = sec / sectrac;
16645b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
16653a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
16663a2f7427SDavid Greenman 
16673a2f7427SDavid Greenman 		if(format || !read)
16683a2f7427SDavid Greenman 		{
16693a2f7427SDavid Greenman 			/* make sure the drive is writable */
1670dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1671dc8603e3SJoerg Wunsch 			{
1672dc8603e3SJoerg Wunsch 				/* stuck controller? */
1673dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1674dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1675dc8603e3SJoerg Wunsch 			}
16763a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
16773a2f7427SDavid Greenman 			{
16783a2f7427SDavid Greenman 				/*
16793a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
16803a2f7427SDavid Greenman 				 * in order to force the current operation
16813a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
16823a2f7427SDavid Greenman 				 * error - all error handling is done
16833a2f7427SDavid Greenman 				 * by the retrier()
16843a2f7427SDavid Greenman 				 */
16853a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
16863a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
16873a2f7427SDavid Greenman 				fdc->status[2] = 0;
16883a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
16893a2f7427SDavid Greenman 				fdc->status[4] = head;
16903a2f7427SDavid Greenman 				fdc->status[5] = sec;
16913a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
16923a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
16933a2f7427SDavid Greenman 				return (1);
16943a2f7427SDavid Greenman 			}
16953a2f7427SDavid Greenman 		}
16965b81b6b3SRodney W. Grimes 
1697b39c878eSAndrey A. Chernov 		if(format)
1698b39c878eSAndrey A. Chernov 		{
1699b39c878eSAndrey A. Chernov 			/* formatting */
1700dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1701dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1702dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1703dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1704dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1705dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1706dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1707dc5df763SJoerg Wunsch 				  0))
1708dc8603e3SJoerg Wunsch 			{
1709dc8603e3SJoerg Wunsch 				/* controller fell over */
1710dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1711dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1712dc8603e3SJoerg Wunsch 			}
1713b39c878eSAndrey A. Chernov 		}
1714b39c878eSAndrey A. Chernov 		else
1715b39c878eSAndrey A. Chernov 		{
1716dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1717dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1718dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1719dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1720dc5df763SJoerg Wunsch 				   head,
1721dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1722dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1723dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1724dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1725dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1726dc5df763SJoerg Wunsch 				   0))
17275b81b6b3SRodney W. Grimes 			{
1728dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1729dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1730dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
17315b81b6b3SRodney W. Grimes 			}
1732b39c878eSAndrey A. Chernov 		}
17335b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
173402a19910SJustin T. Gibbs 		fd->tohandle = timeout(fd_timeout, (caddr_t)fdcu, hz);
17355b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
17365b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
173702a19910SJustin T. Gibbs 		untimeout(fd_timeout, (caddr_t)fdcu, fd->tohandle);
1738dc5df763SJoerg Wunsch 
1739dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
17405b81b6b3SRodney W. Grimes 		{
1741dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1742dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1743dc5df763SJoerg Wunsch 			return retrier(fdcu);
17445b81b6b3SRodney W. Grimes   		}
1745dc5df763SJoerg Wunsch 
17463a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1747dc5df763SJoerg Wunsch 
17483a2f7427SDavid Greenman 		/* FALLTHROUGH */
1749dc5df763SJoerg Wunsch 
17503a2f7427SDavid Greenman 	case IOTIMEDOUT:
1751ab3f7469SPoul-Henning Kamp 		isa_dmadone(bp->b_flags, bp->b_data+fd->skip,
17523a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
17533a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
17545b81b6b3SRodney W. Grimes 		{
17553a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
17563a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1757b39c878eSAndrey A. Chernov                                 /*
17583a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
17593a2f7427SDavid Greenman 				 * and didn't release it in time for the
17603a2f7427SDavid Greenman 				 * next FDC transfer.
17613a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
17623a2f7427SDavid Greenman 				 * count. (vak)
1763b39c878eSAndrey A. Chernov                                  */
1764b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1765b39c878eSAndrey A. Chernov                                 return (1);
1766b39c878eSAndrey A. Chernov                         }
17673a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
17683a2f7427SDavid Greenman 				&& fdc->retry < 6)
17693a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
17703a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
17713a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
17723a2f7427SDavid Greenman 				&& fdc->retry < 3)
17733a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
17745b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
17755b81b6b3SRodney W. Grimes 		}
17765b81b6b3SRodney W. Grimes 		/* All OK */
17773a2f7427SDavid Greenman 		fd->skip += fdblk;
1778bb6382faSJoerg Wunsch 		if (!format && fd->skip < bp->b_bcount - bp->b_resid)
17795b81b6b3SRodney W. Grimes 		{
17805b81b6b3SRodney W. Grimes 			/* set up next transfer */
17815b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
17825b81b6b3SRodney W. Grimes 		}
17835b81b6b3SRodney W. Grimes 		else
17845b81b6b3SRodney W. Grimes 		{
17855b81b6b3SRodney W. Grimes 			/* ALL DONE */
17865b81b6b3SRodney W. Grimes 			fd->skip = 0;
178702a19910SJustin T. Gibbs 			bufq_remove(&fdc->head, bp);
17885b81b6b3SRodney W. Grimes 			biodone(bp);
17895b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
17905b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
17915b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
17925b81b6b3SRodney W. Grimes 		}
17935b81b6b3SRodney W. Grimes 		return(1);
17945b81b6b3SRodney W. Grimes 	case RESETCTLR:
17953a2f7427SDavid Greenman 		fdc_reset(fdc);
17965b81b6b3SRodney W. Grimes 		fdc->retry++;
17975b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
17985b81b6b3SRodney W. Grimes 		break;
17995b81b6b3SRodney W. Grimes 	case STARTRECAL:
18000e317d05SJoerg Wunsch 		/* XXX clear the fdc results from the last reset, if any. */
18010e317d05SJoerg Wunsch 		{
18020e317d05SJoerg Wunsch 			int i;
18030e317d05SJoerg Wunsch 			for (i = 0; i < 4; i++)
18040e317d05SJoerg Wunsch 				(void)fd_sense_int(fdc, &st0, &cyl);
18050e317d05SJoerg Wunsch 		}
18060e317d05SJoerg Wunsch 
1807dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1808dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1809dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1810dc8603e3SJoerg Wunsch 		{
1811dc8603e3SJoerg Wunsch 			/* arrgl */
1812dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1813dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1814dc8603e3SJoerg Wunsch 		}
18155b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
18165b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
18175b81b6b3SRodney W. Grimes 	case RECALWAIT:
18185b81b6b3SRodney W. Grimes 		/* allow heads to settle */
181904b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
18205b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
18215b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
18225b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
18233a2f7427SDavid Greenman 		do {
18243a2f7427SDavid Greenman 			/*
1825dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
18263a2f7427SDavid Greenman 			 */
1827dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1828dc5df763SJoerg Wunsch 				return 0;
1829dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1830dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1831dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
18323a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
18333a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
18345b81b6b3SRodney W. Grimes 		{
1835dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1836dc8603e3SJoerg Wunsch 				/*
1837dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1838dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1839dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1840dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1841dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1842dc8603e3SJoerg Wunsch 				 * time in a line
1843dc8603e3SJoerg Wunsch 				 */
1844dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1845dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
18463a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
18475b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
18485b81b6b3SRodney W. Grimes 		}
18495b81b6b3SRodney W. Grimes 		fd->track = 0;
18505b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
18515b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
18525b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
18535b81b6b3SRodney W. Grimes 	case MOTORWAIT:
18545b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
18555b81b6b3SRodney W. Grimes 		{
18565b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
18575b81b6b3SRodney W. Grimes 		}
18585e235068SJordan K. Hubbard 		/*
18595e235068SJordan K. Hubbard 		 * since the controller was off, it has lost its
18605e235068SJordan K. Hubbard 		 * idea about the current track it were; thus,
18615e235068SJordan K. Hubbard 		 * recalibrate the bastard
18625e235068SJordan K. Hubbard 		 */
18635e235068SJordan K. Hubbard 		fdc->state = STARTRECAL;
18645b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
18655b81b6b3SRodney W. Grimes 	default:
1866dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1867dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1868dac0f2dbSJoerg Wunsch 			printf("FDC status :%lx %lx %lx %lx %lx %lx %lx   ",
18695b81b6b3SRodney W. Grimes 			       fdc->status[0],
18705b81b6b3SRodney W. Grimes 			       fdc->status[1],
18715b81b6b3SRodney W. Grimes 			       fdc->status[2],
18725b81b6b3SRodney W. Grimes 			       fdc->status[3],
18735b81b6b3SRodney W. Grimes 			       fdc->status[4],
18745b81b6b3SRodney W. Grimes 			       fdc->status[5],
18755b81b6b3SRodney W. Grimes 			       fdc->status[6] );
18763a2f7427SDavid Greenman 		else
1877dac0f2dbSJoerg Wunsch 			printf("No status available   ");
1878dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1879dac0f2dbSJoerg Wunsch 		{
1880dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
18815b81b6b3SRodney W. Grimes 			return(0);
18825b81b6b3SRodney W. Grimes 		}
1883dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1884dac0f2dbSJoerg Wunsch 		return(0);
1885dac0f2dbSJoerg Wunsch 	}
1886dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
18875b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
18885b81b6b3SRodney W. Grimes }
18895b81b6b3SRodney W. Grimes 
1890aaf08d94SGarrett Wollman static int
1891f5f7ba03SJordan K. Hubbard retrier(fdcu)
1892f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
18935b81b6b3SRodney W. Grimes {
18943e425b96SJulian Elischer 	struct subdev *sd;
18955b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
189617542807SPoul-Henning Kamp 	register struct buf *bp;
18973e425b96SJulian Elischer #ifdef	SLICE
18983e425b96SJulian Elischer 	struct fd_data *fd;
18993e425b96SJulian Elischer 	int fdu;
19003e425b96SJulian Elischer #endif
19015b81b6b3SRodney W. Grimes 
190202a19910SJustin T. Gibbs 	bp = bufq_first(&fdc->head);
19035b81b6b3SRodney W. Grimes 
19043e425b96SJulian Elischer #ifdef	SLICE
19053e425b96SJulian Elischer 	sd = bp->b_driver1;
19063e425b96SJulian Elischer 	fd = sd->drive;
19073e425b96SJulian Elischer  	fdu = fd->unit;
19083e425b96SJulian Elischer 	if(fd->options & FDOPT_NORETRY)
19093e425b96SJulian Elischer 		goto fail;
19103e425b96SJulian Elischer #else
19113a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
19123a2f7427SDavid Greenman 		goto fail;
19133e425b96SJulian Elischer #endif
19145b81b6b3SRodney W. Grimes 	switch(fdc->retry)
19155b81b6b3SRodney W. Grimes 	{
19165b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
19175b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
19185b81b6b3SRodney W. Grimes 		break;
19195b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
19205b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
19215b81b6b3SRodney W. Grimes 		break;
19225b81b6b3SRodney W. Grimes 	case 6:
19235b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
19245b81b6b3SRodney W. Grimes 		break;
19255b81b6b3SRodney W. Grimes 	case 7:
19265b81b6b3SRodney W. Grimes 		break;
19275b81b6b3SRodney W. Grimes 	default:
19283a2f7427SDavid Greenman 	fail:
19295b81b6b3SRodney W. Grimes 		{
19303e425b96SJulian Elischer #ifdef SLICE
19313e425b96SJulian Elischer 			printf("fd%d: hard error, block %d ", fdu,
19323e425b96SJulian Elischer 				fd->skip / DEV_BSIZE);
19333e425b96SJulian Elischer #else
19347ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
19357ca0641bSAndrey A. Chernov 			/* Trick diskerr */
19363a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
19373a2f7427SDavid Greenman 				    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
193892ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
19393a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
19403a2f7427SDavid Greenman 				(struct disklabel *)NULL);
19417ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
19423e425b96SJulian Elischer #endif /* !SLICE */
1943dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1944dc5df763SJoerg Wunsch 			{
1945dc5df763SJoerg Wunsch 				printf(
1946dc5df763SJoerg Wunsch 			" (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n",
1947dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1948dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1949dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1950dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1951dc5df763SJoerg Wunsch 				       fdc->status[5]);
1952dc5df763SJoerg Wunsch 			}
1953dc5df763SJoerg Wunsch 			else
1954dc5df763SJoerg Wunsch 				printf(" (No status)\n");
19555b81b6b3SRodney W. Grimes 		}
19565b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
19575b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
1958bb6382faSJoerg Wunsch 		bp->b_resid += bp->b_bcount - fdc->fd->skip;
195902a19910SJustin T. Gibbs 		bufq_remove(&fdc->head, bp);
19605b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
19615b81b6b3SRodney W. Grimes 		biodone(bp);
196292ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
19635b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
19645b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1965f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
196692ed385aSRodney W. Grimes 		return(1);
19675b81b6b3SRodney W. Grimes 	}
19685b81b6b3SRodney W. Grimes 	fdc->retry++;
19695b81b6b3SRodney W. Grimes 	return(1);
19705b81b6b3SRodney W. Grimes }
19715b81b6b3SRodney W. Grimes 
19723e425b96SJulian Elischer #ifdef	SLICE
19733e425b96SJulian Elischer static int
19743e425b96SJulian Elischer fdformat( struct subdev *sd, struct fd_formb *finfo, struct proc *p)
19753e425b96SJulian Elischer #else	/* !SLICE */
1976b39c878eSAndrey A. Chernov static int
1977b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1978b39c878eSAndrey A. Chernov 	dev_t dev;
1979b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1980b39c878eSAndrey A. Chernov 	struct proc *p;
19813e425b96SJulian Elischer #endif	/* !SLICE */
1982b39c878eSAndrey A. Chernov {
1983b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1984b39c878eSAndrey A. Chernov  	fd_p	fd;
1985b39c878eSAndrey A. Chernov 
1986b39c878eSAndrey A. Chernov 	struct buf *bp;
1987b39c878eSAndrey A. Chernov 	int rv = 0, s;
19883a2f7427SDavid Greenman 	size_t fdblk;
1989b39c878eSAndrey A. Chernov 
19903e425b96SJulian Elischer #ifdef	SLICE
19913e425b96SJulian Elischer  	fd	= sd->drive;
19923e425b96SJulian Elischer  	fdu	= fd->unit;
19933e425b96SJulian Elischer #else
1994b39c878eSAndrey A. Chernov  	fdu	= FDUNIT(minor(dev));
1995b39c878eSAndrey A. Chernov 	fd	= &fd_data[fdu];
19963e425b96SJulian Elischer #endif
19973a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1998b39c878eSAndrey A. Chernov 
1999b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
2000b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
2001b39c878eSAndrey A. Chernov 	if(bp == 0)
2002b39c878eSAndrey A. Chernov 		return ENOBUFS;
200382f5379bSJoerg Wunsch 	/*
200482f5379bSJoerg Wunsch 	 * keep the process from being swapped
200582f5379bSJoerg Wunsch 	 */
200682f5379bSJoerg Wunsch 	p->p_flag |= P_PHYSIO;
2007b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
2008b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
2009b39c878eSAndrey A. Chernov 	bp->b_proc = p;
2010b39c878eSAndrey A. Chernov 
2011b39c878eSAndrey A. Chernov 	/*
2012b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
2013b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
2014b39c878eSAndrey A. Chernov 	 */
2015b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
20163a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2017b39c878eSAndrey A. Chernov 
2018b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2019ab3f7469SPoul-Henning Kamp 	bp->b_data = (caddr_t)finfo;
2020b39c878eSAndrey A. Chernov 
2021b39c878eSAndrey A. Chernov 	/* now do the format */
20223e425b96SJulian Elischer #ifdef SLICE
20233e425b96SJulian Elischer 	bp->b_driver1 = sd;
20243e425b96SJulian Elischer 	fdsIOreq(sd, bp);
20253e425b96SJulian Elischer #else	/* !SLICE */
20263e425b96SJulian Elischer 	bp->b_dev = dev;
2027b39c878eSAndrey A. Chernov 	fdstrategy(bp);
20283e425b96SJulian Elischer #endif	/* !SLICE */
2029b39c878eSAndrey A. Chernov 
2030b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
2031b39c878eSAndrey A. Chernov 	s = splbio();
2032b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
2033b39c878eSAndrey A. Chernov 	{
20345e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2035b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
2036b39c878eSAndrey A. Chernov 			break;
2037b39c878eSAndrey A. Chernov 	}
2038b39c878eSAndrey A. Chernov 	splx(s);
2039b39c878eSAndrey A. Chernov 
204082f5379bSJoerg Wunsch 	if(rv == EWOULDBLOCK) {
2041b39c878eSAndrey A. Chernov 		/* timed out */
2042b39c878eSAndrey A. Chernov 		rv = EIO;
204382f5379bSJoerg Wunsch 		biodone(bp);
204482f5379bSJoerg Wunsch 	}
20453a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
20463a2f7427SDavid Greenman 		rv = bp->b_error;
204782f5379bSJoerg Wunsch 	/*
204882f5379bSJoerg Wunsch 	 * allow the process to be swapped
204982f5379bSJoerg Wunsch 	 */
205082f5379bSJoerg Wunsch 	p->p_flag &= ~P_PHYSIO;
2051b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
2052b39c878eSAndrey A. Chernov 	return rv;
2053b39c878eSAndrey A. Chernov }
2054b39c878eSAndrey A. Chernov 
2055f5f7ba03SJordan K. Hubbard /*
2056671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
2057f5f7ba03SJordan K. Hubbard  */
20585b81b6b3SRodney W. Grimes 
20593e425b96SJulian Elischer static int
2060b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
2061f5f7ba03SJordan K. Hubbard 	dev_t dev;
2062ecbb00a2SDoug Rabson 	u_long cmd;
2063f5f7ba03SJordan K. Hubbard 	caddr_t addr;
2064f5f7ba03SJordan K. Hubbard 	int flag;
2065b39c878eSAndrey A. Chernov 	struct proc *p;
2066f5f7ba03SJordan K. Hubbard {
20673a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
20683a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
20693a2f7427SDavid Greenman 	size_t fdblk;
20703a2f7427SDavid Greenman 
2071f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
2072f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
2073f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
20743a2f7427SDavid Greenman 	int error = 0;
2075f5f7ba03SJordan K. Hubbard 
2076b99f0a4aSAndrew Moore #if NFT > 0
2077a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
2078a60eff27SNate Williams 
2079a60eff27SNate Williams 	/* check for a tape ioctl */
2080a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
2081b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
2082b99f0a4aSAndrew Moore #endif
2083b99f0a4aSAndrew Moore 
20843e425b96SJulian Elischer #ifdef	SLICE
20853e425b96SJulian Elischer 	/*
20863e425b96SJulian Elischer 	 * if SLICE is defined then only ft accesses come here
20873e425b96SJulian Elischer 	 * so break the rest off to another function for SLICE access.
20883e425b96SJulian Elischer 	 */
20893e425b96SJulian Elischer 	return (ENOTTY);
20903e425b96SJulian Elischer }
20913e425b96SJulian Elischer 
20923e425b96SJulian Elischer /*
20933e425b96SJulian Elischer  * Slice ioctls come here
20943e425b96SJulian Elischer  */
20953e425b96SJulian Elischer static int
2096498b291eSDoug Rabson fdsioctl( void *private, u_long cmd, caddr_t addr, int flag, struct proc *p)
20973e425b96SJulian Elischer {
20983e425b96SJulian Elischer 	struct	subdev *sd = private;
20993e425b96SJulian Elischer  	fd_p	fd	= sd->drive;
21003e425b96SJulian Elischer  	fdu_t	fdu	= fd->unit;
21013e425b96SJulian Elischer 	fdc_p	fdc	= fd->fdc;
21023e425b96SJulian Elischer 	fdcu_t	fdcu	= fdc->fdcu;
21033e425b96SJulian Elischer 	size_t	fdblk;
21043e425b96SJulian Elischer 	int	error	= 0;
21053e425b96SJulian Elischer #endif	/* SLICE */
21063a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2107f5f7ba03SJordan K. Hubbard 
2108f5f7ba03SJordan K. Hubbard 	switch (cmd)
2109f5f7ba03SJordan K. Hubbard 	{
21103e425b96SJulian Elischer #ifndef	SLICE
2111f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
2112f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
2113f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
21143a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
211592ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
2116f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
2117f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
2118f5f7ba03SJordan K. Hubbard 
2119191e1a59SBruce Evans 		if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl)
2120191e1a59SBruce Evans 		    == NULL)
2121f5f7ba03SJordan K. Hubbard 			error = 0;
2122f5f7ba03SJordan K. Hubbard 		else
2123f5f7ba03SJordan K. Hubbard 			error = EINVAL;
2124f5f7ba03SJordan K. Hubbard 
2125f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
2126f5f7ba03SJordan K. Hubbard 		break;
2127f5f7ba03SJordan K. Hubbard 
2128f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
2129f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2130f5f7ba03SJordan K. Hubbard 			error = EBADF;
2131f5f7ba03SJordan K. Hubbard 		break;
2132f5f7ba03SJordan K. Hubbard 
2133f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
2134f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2135f5f7ba03SJordan K. Hubbard 			error = EBADF;
2136f5f7ba03SJordan K. Hubbard 		break;
2137f5f7ba03SJordan K. Hubbard 
2138f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
2139f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2140f5f7ba03SJordan K. Hubbard 		{
2141f5f7ba03SJordan K. Hubbard 			error = EBADF;
2142f5f7ba03SJordan K. Hubbard 			break;
2143f5f7ba03SJordan K. Hubbard 		}
2144f5f7ba03SJordan K. Hubbard 
2145f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
2146f5f7ba03SJordan K. Hubbard 
2147191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
2148191e1a59SBruce Evans 					  (u_long)0)) != 0)
2149f5f7ba03SJordan K. Hubbard 			break;
2150f5f7ba03SJordan K. Hubbard 
2151b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
215254c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
2153b39c878eSAndrey A. Chernov 		break;
21543e425b96SJulian Elischer #endif	/* !SLICE */
2155b39c878eSAndrey A. Chernov 	case FD_FORM:
2156b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
2157b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
2158b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
2159b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
2160b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
2161b39c878eSAndrey A. Chernov 		else
21623e425b96SJulian Elischer #ifdef	SLICE
21633e425b96SJulian Elischer 			error = fdformat(sd, (struct fd_formb *)addr, p);
21643e425b96SJulian Elischer #else
2165b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
21663e425b96SJulian Elischer #endif
2167b39c878eSAndrey A. Chernov 		break;
2168b39c878eSAndrey A. Chernov 
2169b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
21703e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2171f5f7ba03SJordan K. Hubbard 		break;
2172f5f7ba03SJordan K. Hubbard 
21733a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
21743a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
21753a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
21763a2f7427SDavid Greenman 			return EPERM;
21773e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
21783a2f7427SDavid Greenman 		break;
21793a2f7427SDavid Greenman 
21803a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
21813e425b96SJulian Elischer 		*(int *)addr = fd->options;
21823a2f7427SDavid Greenman 		break;
21833a2f7427SDavid Greenman 
21843a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
21853e425b96SJulian Elischer 		fd->options = *(int *)addr;
21863a2f7427SDavid Greenman 		break;
21873a2f7427SDavid Greenman 
2188f5f7ba03SJordan K. Hubbard 	default:
21893a2f7427SDavid Greenman 		error = ENOTTY;
2190f5f7ba03SJordan K. Hubbard 		break;
2191f5f7ba03SJordan K. Hubbard 	}
2192f5f7ba03SJordan K. Hubbard 	return (error);
2193f5f7ba03SJordan K. Hubbard }
2194f5f7ba03SJordan K. Hubbard 
21957146c13eSJulian Elischer 
21967146c13eSJulian Elischer static fd_devsw_installed = 0;
21977146c13eSJulian Elischer 
219887f6c662SJulian Elischer static void 	fd_drvinit(void *notused )
21997146c13eSJulian Elischer {
220087f6c662SJulian Elischer 
22017146c13eSJulian Elischer 	if( ! fd_devsw_installed ) {
2202f7ea2f55SJulian Elischer 		cdevsw_add_generic(BDEV_MAJOR,CDEV_MAJOR, &fd_cdevsw);
22037146c13eSJulian Elischer 		fd_devsw_installed = 1;
22047146c13eSJulian Elischer 	}
22057146c13eSJulian Elischer }
220687f6c662SJulian Elischer 
220787f6c662SJulian Elischer SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL)
220887f6c662SJulian Elischer 
22093e425b96SJulian Elischer 
22103e425b96SJulian Elischer #ifdef	SLICE
22113e425b96SJulian Elischer static int
22123e425b96SJulian Elischer fdsopen(void *private, int flags, int mode, struct proc *p)
22133e425b96SJulian Elischer {
22143e425b96SJulian Elischer 	struct subdev *sd;
22153e425b96SJulian Elischer 
22163e425b96SJulian Elischer 	sd = private;
22173e425b96SJulian Elischer 
221826d3bf5fSJulian Elischer 	if((flags & (FREAD|FWRITE)) != 0) {
22197f2f1b78SJulian Elischer 		return(Fdopen(makedev(0,sd->minor), flags , mode, p));
222026d3bf5fSJulian Elischer 	} else {
22217f2f1b78SJulian Elischer 		return(fdclose(makedev(0,sd->minor), 0 , mode, p));
222226d3bf5fSJulian Elischer 	}
22233e425b96SJulian Elischer }
22243e425b96SJulian Elischer 
222526d3bf5fSJulian Elischer #if 0
22263e425b96SJulian Elischer static void
22273e425b96SJulian Elischer fdsclose(void *private, int flags, int mode, struct proc *p)
22283e425b96SJulian Elischer {
22293e425b96SJulian Elischer 	struct subdev *sd;
22303e425b96SJulian Elischer 
22313e425b96SJulian Elischer 	sd = private;
22323e425b96SJulian Elischer 
22333e425b96SJulian Elischer 	fdclose(makedev(0,sd->minor), 0 , 0, p);
22343e425b96SJulian Elischer 	return ;
22353e425b96SJulian Elischer }
223626d3bf5fSJulian Elischer #endif	/* 0 */
22373e425b96SJulian Elischer 
22383e425b96SJulian Elischer #endif	/* SLICE */
2239f5f7ba03SJordan K. Hubbard #endif
22403e425b96SJulian Elischer 
22413a2f7427SDavid Greenman /*
22423a2f7427SDavid Greenman  * Hello emacs, these are the
22433a2f7427SDavid Greenman  * Local Variables:
22443a2f7427SDavid Greenman  *  c-indent-level:               8
22453a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
22463a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
22473a2f7427SDavid Greenman  *  c-brace-offset:              -8
22483a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
22493a2f7427SDavid Greenman  *  c-argdecl-indent:             8
22503a2f7427SDavid Greenman  *  c-label-offset:              -8
22513a2f7427SDavid Greenman  *  c++-hanging-braces:           1
22523a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
22533a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
22543a2f7427SDavid Greenman  *  c++-friend-offset:            0
22553a2f7427SDavid Greenman  * End:
22563a2f7427SDavid Greenman  */
2257