xref: /freebsd/sys/dev/fdc/fdc.c (revision e5d7d243615d5a196a46d23a162623d438454de1)
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
47e5d7d243SBruce Evans  *	$Id: fd.c,v 1.118 1998/07/15 09:59:30 bde 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 *);
22029d6b96dSBruce Evans int ftioctl(dev_t, unsigned long, 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
512a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints 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 			namebuf);
7983e425b96SJulian Elischer 		/* Allow full probing */
7993e425b96SJulian Elischer 		fd->subdevs[0].slice->probeinfo.typespecific = NULL;
8003e425b96SJulian Elischer 		fd->subdevs[0].slice->probeinfo.type = NULL;
8013e425b96SJulian Elischer 
8023e425b96SJulian Elischer 		fd->ich.ich_func = fdsinit;
8033e425b96SJulian Elischer 		fd->ich.ich_arg = &fd->subdevs[0];
8043e425b96SJulian Elischer 		config_intrhook_establish(&fd->ich);
8053e425b96SJulian Elischer #else	/* SLICE */
80621519754SBruce Evans 		mynor = fdu << 6;
807f7ea2f55SJulian Elischer 		fd->bdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_BLK,
808f85120acSBruce Evans 						UID_ROOT, GID_OPERATOR, 0640,
80921519754SBruce Evans 						"fd%d", fdu);
81021519754SBruce Evans 		fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR,
811f85120acSBruce Evans 						UID_ROOT, GID_OPERATOR, 0640,
81221519754SBruce Evans 						"rfd%d", fdu);
8133e425b96SJulian Elischer #endif	/* SLICE */
81421519754SBruce Evans 		for (i = 1; i < 1 + NUMDENS; i++) {
81521519754SBruce Evans 			/*
81621519754SBruce Evans 			 * XXX this and the lookup in Fdopen() should be
81721519754SBruce Evans 			 * data driven.
81821519754SBruce Evans 			 */
81921519754SBruce Evans 			switch (fd->type) {
82021519754SBruce Evans 			case FD_360:
82121519754SBruce Evans 				if (i != FD_360)
82221519754SBruce Evans 					continue;
82321519754SBruce Evans 				break;
82421519754SBruce Evans 			case FD_720:
82521519754SBruce Evans 				if (i != FD_720 && i != FD_800 && i != FD_820)
82621519754SBruce Evans 					continue;
82721519754SBruce Evans 				break;
82821519754SBruce Evans 			case FD_1200:
82921519754SBruce Evans 				if (i != FD_360 && i != FD_720 && i != FD_800
83021519754SBruce Evans 				    && i != FD_820 && i != FD_1200
83121519754SBruce Evans 				    && i != FD_1440 && i != FD_1480)
83221519754SBruce Evans 					continue;
83321519754SBruce Evans 				break;
83421519754SBruce Evans 			case FD_1440:
83521519754SBruce Evans 				if (i != FD_720 && i != FD_800 && i != FD_820
83621519754SBruce Evans 				    && i != FD_1200 && i != FD_1440
83721519754SBruce Evans 				    && i != FD_1480 && i != FD_1720)
83821519754SBruce Evans 					continue;
83921519754SBruce Evans 				break;
84021519754SBruce Evans 			}
84121519754SBruce Evans 			typesize = fd_types[i - 1].size / 2;
84221519754SBruce Evans 			/*
84321519754SBruce Evans 			 * XXX all these conversions give bloated code and
84421519754SBruce Evans 			 * confusing names.
84521519754SBruce Evans 			 */
84621519754SBruce Evans 			if (typesize == 1476)
84721519754SBruce Evans 				typesize = 1480;
84821519754SBruce Evans 			if (typesize == 1722)
84921519754SBruce Evans 				typesize = 1720;
8503e425b96SJulian Elischer #ifdef SLICE
8513e425b96SJulian Elischer 			sprintf(namebuf,"fd%d.%d",fdu,typesize);
8523e425b96SJulian Elischer 			fd->subdevs[i].minor = i;
8533e425b96SJulian Elischer 			fd->subdevs[i].drive = fd;
8543e425b96SJulian Elischer 			fd->subdevs[i].limit.blksize =
8553e425b96SJulian Elischer 				128 << (fd_types[i - 1].secsize);
8563e425b96SJulian Elischer 			fd->subdevs[i].limit.slicesize =
8573e425b96SJulian Elischer 				fd_types[i - 1].size
8583e425b96SJulian Elischer 					* fd->subdevs[i].limit.blksize;
8593e425b96SJulian Elischer 			sl_make_slice(&slicetype,
8603e425b96SJulian Elischer 				&fd->subdevs[i],
8613e425b96SJulian Elischer 				&fd->subdevs[i].limit,
8623e425b96SJulian Elischer 			 	&fd->subdevs[i].slice,
8633e425b96SJulian Elischer 				namebuf);
8643e425b96SJulian Elischer 			/* Allow full probing */
8653e425b96SJulian Elischer 			fd->subdevs[i].slice->probeinfo.typespecific = NULL;
8663e425b96SJulian Elischer 			fd->subdevs[i].slice->probeinfo.type = NO_SUBPART;
8673e425b96SJulian Elischer 		}
8683e425b96SJulian Elischer #else	/* SLICE */
8693e425b96SJulian Elischer 			typemynor = mynor | i;
87021519754SBruce Evans 			fd->bdevs[i] =
871f7ea2f55SJulian Elischer 				devfs_add_devswf(&fd_cdevsw, typemynor, DV_BLK,
87221519754SBruce Evans 						 UID_ROOT, GID_OPERATOR, 0640,
87321519754SBruce Evans 						 "fd%d.%d", fdu, typesize);
87421519754SBruce Evans 			fd->cdevs[i] =
87521519754SBruce Evans 				devfs_add_devswf(&fd_cdevsw, typemynor, DV_CHR,
87621519754SBruce Evans 						 UID_ROOT, GID_OPERATOR, 0640,
87721519754SBruce Evans 						 "rfd%d.%d", fdu, typesize);
87821519754SBruce Evans 		}
8793e425b96SJulian Elischer 
88021519754SBruce Evans 		for (i = 0; i < MAXPARTITIONS; i++) {
8813e425b96SJulian Elischer 			fd->bdevs[1 + NUMDENS + i] = devfs_link(fd->bdevs[0],
88221519754SBruce Evans 					   "fd%d%c", fdu, 'a' + i);
88321519754SBruce Evans 			fd->cdevs[1 + NUMDENS + i] =
88421519754SBruce Evans 				devfs_link(fd->cdevs[0],
88521519754SBruce Evans 					   "rfd%d%c", fdu, 'a' + i);
88621519754SBruce Evans 		}
8873e425b96SJulian Elischer #endif /* SLICE */
888999422d7SJulian Elischer #endif /* DEVFS */
889d4319080SBruce Evans #ifdef notyet
89092200632SGarrett Wollman 		if (dk_ndrive < DK_NDRIVE) {
89192200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
8926a0e6f42SRodney W. Grimes 			fd->dkunit = dk_ndrive++;
893671e2ceeSBruce Evans 			/*
894671e2ceeSBruce Evans 			 * XXX assume rate is FDC_500KBPS.
895671e2ceeSBruce Evans 			 */
896671e2ceeSBruce Evans 			dk_wpms[dk_ndrive] = 500000 / 8 / 2;
89792200632SGarrett Wollman 		} else {
89892200632SGarrett Wollman 			fd->dkunit = -1;
89992200632SGarrett Wollman 		}
900d4319080SBruce Evans #endif
9015b81b6b3SRodney W. Grimes 	}
9025b81b6b3SRodney W. Grimes 
9033a2f7427SDavid Greenman 	return (1);
9045b81b6b3SRodney W. Grimes }
9055b81b6b3SRodney W. Grimes 
9063e425b96SJulian Elischer 
9073e425b96SJulian Elischer #ifdef	SLICE
9083e425b96SJulian Elischer 
9093e425b96SJulian Elischer static void
9103e425b96SJulian Elischer fdsinit(void *arg)
9113e425b96SJulian Elischer {
9123e425b96SJulian Elischer 	struct subdev *sd = arg;
9133e425b96SJulian Elischer 	sh_p	tp;
9143e425b96SJulian Elischer 
915ad27f8d9SJulian Elischer 	slice_start_probe(sd->slice);
9163e425b96SJulian Elischer 	config_intrhook_disestablish(&sd->drive->ich);
917ad27f8d9SJulian Elischer 	DELAY(2000000); /* XXX */
9183e425b96SJulian Elischer }
9193e425b96SJulian Elischer #endif	/* SLICE */
9203e425b96SJulian Elischer 
9215b81b6b3SRodney W. Grimes /****************************************************************************/
9225b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
9235e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
9245b81b6b3SRodney W. Grimes /****************************************************************************/
9253a2f7427SDavid Greenman static void
926dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
9275b81b6b3SRodney W. Grimes {
9283a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
9293a2f7427SDavid Greenman 	int needspecify = 0;
9303a2f7427SDavid Greenman 
9313a2f7427SDavid Greenman 	if(turnon) {
9323a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
9333a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
9343a2f7427SDavid Greenman 	} else
9353a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
9363a2f7427SDavid Greenman 
9375e235068SJordan K. Hubbard 	if(!turnon
9385e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
9395e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
9405e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
9415e235068SJordan K. Hubbard 	else {
9423a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
9433a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
9443a2f7427SDavid Greenman 			needspecify = 1;
9453a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
9465b81b6b3SRodney W. Grimes 	}
9475b81b6b3SRodney W. Grimes 
9483a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
9493a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
9503a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
9513a2f7427SDavid Greenman 
9523a2f7427SDavid Greenman 	if(needspecify) {
953dc8603e3SJoerg Wunsch 		/*
954dc5df763SJoerg Wunsch 		 * XXX
955dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
956dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
957dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
958dc8603e3SJoerg Wunsch 		 */
959dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
960dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
961dc5df763SJoerg Wunsch 			     0);
962d66c374fSTor Egge 		if (fdc_data[fdcu].flags & FDC_HAS_FIFO)
963d66c374fSTor Egge 			(void) enable_fifo(&fdc_data[fdcu]);
9643a2f7427SDavid Greenman 	}
9653a2f7427SDavid Greenman }
9663a2f7427SDavid Greenman 
967381fe1aaSGarrett Wollman static void
968d0917939SPaul Richards fd_turnoff(void *arg1)
9695b81b6b3SRodney W. Grimes {
970381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
971f5f7ba03SJordan K. Hubbard 	int	s;
9725b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
973dc16046fSJoerg Wunsch 
974dc16046fSJoerg Wunsch 	TRACE1("[fd%d: turnoff]", fdu);
9758335c1b8SBruce Evans 
9768335c1b8SBruce Evans 	/*
9778335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
9788335c1b8SBruce Evans 	 * XXX shouldn't even schedule turnoff until drive is inactive
9798335c1b8SBruce Evans 	 * and nothing is queued on it.
9808335c1b8SBruce Evans 	 */
9818335c1b8SBruce Evans 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fdu) {
98202a19910SJustin T. Gibbs 		fd->toffhandle = timeout(fd_turnoff, arg1, 4 * hz);
9838335c1b8SBruce Evans 		return;
9848335c1b8SBruce Evans 	}
9858335c1b8SBruce Evans 
986f5f7ba03SJordan K. Hubbard 	s = splbio();
9875b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
9883a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
989f5f7ba03SJordan K. Hubbard 	splx(s);
9905b81b6b3SRodney W. Grimes }
9915b81b6b3SRodney W. Grimes 
9923a2f7427SDavid Greenman static void
993d0917939SPaul Richards fd_motor_on(void *arg1)
9945b81b6b3SRodney W. Grimes {
995381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
996f5f7ba03SJordan K. Hubbard 	int	s;
997f5f7ba03SJordan K. Hubbard 
9985b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
999f5f7ba03SJordan K. Hubbard 	s = splbio();
10005b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
10015b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
10025b81b6b3SRodney W. Grimes 	{
1003f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
10045b81b6b3SRodney W. Grimes 	}
1005f5f7ba03SJordan K. Hubbard 	splx(s);
10065b81b6b3SRodney W. Grimes }
10075b81b6b3SRodney W. Grimes 
10083a2f7427SDavid Greenman static void
1009dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
10105b81b6b3SRodney W. Grimes {
10115b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
10125b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
10135b81b6b3SRodney W. Grimes 	{
10143a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
10153a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
10165e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
10175b81b6b3SRodney W. Grimes 	}
10185b81b6b3SRodney W. Grimes }
10195b81b6b3SRodney W. Grimes 
1020381fe1aaSGarrett Wollman static void
1021dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
10225b81b6b3SRodney W. Grimes {
10233a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
10243a2f7427SDavid Greenman 
10253a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
10263a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
10273a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
10283a2f7427SDavid Greenman 	DELAY(100);
10293a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
10303a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
10313a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
10323a2f7427SDavid Greenman 	DELAY(100);
10333a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
10343a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
10353a2f7427SDavid Greenman 
1036dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
1037dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
1038dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1039dc5df763SJoerg Wunsch 		     0);
1040d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1041d66c374fSTor Egge 		(void) enable_fifo(fdc);
10425b81b6b3SRodney W. Grimes }
10435b81b6b3SRodney W. Grimes 
10445b81b6b3SRodney W. Grimes /****************************************************************************/
10455b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
10465b81b6b3SRodney W. Grimes /****************************************************************************/
10475b81b6b3SRodney W. Grimes int
1048dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
10495b81b6b3SRodney W. Grimes {
10505b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
10515b81b6b3SRodney W. Grimes 	int i, j = 100000;
10523a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
10535b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1054dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
10556a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
10565b81b6b3SRodney W. Grimes 	if (j <= 0)
105716b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
1058d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
10593a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
10603a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
10615b81b6b3SRodney W. Grimes 	return(i);
1062d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
10633a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
1064d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
10655b81b6b3SRodney W. Grimes }
10665b81b6b3SRodney W. Grimes 
1067dc5df763SJoerg Wunsch /*
1068dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
1069dc5df763SJoerg Wunsch  */
1070b5e8ce9fSBruce Evans static int
1071dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
1072dc5df763SJoerg Wunsch {
1073dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
1074dc5df763SJoerg Wunsch 	int i, j = 100000;
1075dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
1076dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1077dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
10786a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
1079dc5df763SJoerg Wunsch 	if (j <= 0)
108016b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
1081d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1082dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
1083dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1084dc5df763SJoerg Wunsch 	*ptr = i;
1085dc5df763SJoerg Wunsch 	return 0;
1086d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1087dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
1088dc5df763SJoerg Wunsch 	if (ptr)
1089dc5df763SJoerg Wunsch 		*ptr = i;
1090dc5df763SJoerg Wunsch 	return 0;
1091d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1092dc5df763SJoerg Wunsch }
1093dc5df763SJoerg Wunsch 
1094dc5df763SJoerg Wunsch int
1095dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
10965b81b6b3SRodney W. Grimes {
10975b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
10983b3837dbSRodney W. Grimes 	int i;
10995b81b6b3SRodney W. Grimes 
11003b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
11013b3837dbSRodney W. Grimes 	i = 100000;
11023a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
11036a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
11043b3837dbSRodney W. Grimes 
11053b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
11063b3837dbSRodney W. Grimes 	i = 100000;
11073a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
110816b04b6aSJoerg Wunsch 	if (i <= 0)
110916b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0);
11103b3837dbSRodney W. Grimes 
11113b3837dbSRodney W. Grimes 	/* Send the command and return */
11123a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
11133a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
11145b81b6b3SRodney W. Grimes 	return (0);
11155b81b6b3SRodney W. Grimes }
11165b81b6b3SRodney W. Grimes 
11175b81b6b3SRodney W. Grimes /****************************************************************************/
11185b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
11195b81b6b3SRodney W. Grimes /****************************************************************************/
1120381fe1aaSGarrett Wollman int
1121671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
11225b81b6b3SRodney W. Grimes {
11235b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
112420a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
1125b99f0a4aSAndrew Moore 	fdc_p	fdc;
11265b81b6b3SRodney W. Grimes 
1127b99f0a4aSAndrew Moore #if NFT > 0
1128b99f0a4aSAndrew Moore 	/* check for a tape open */
1129b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
1130b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
1131b99f0a4aSAndrew Moore #endif
11325b81b6b3SRodney W. Grimes 	/* check bounds */
1133b99f0a4aSAndrew Moore 	if (fdu >= NFD)
1134b99f0a4aSAndrew Moore 		return(ENXIO);
1135b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
1136b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
1137b99f0a4aSAndrew Moore 		return(ENXIO);
1138b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1139b99f0a4aSAndrew Moore 		return(ENXIO);
11407ca0641bSAndrey A. Chernov 	if (type == 0)
11417ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
11427ca0641bSAndrey A. Chernov 	else {
11433e425b96SJulian Elischer 		/*
11443e425b96SJulian Elischer 		 * For each type of basic drive, make sure we are trying
11453e425b96SJulian Elischer 		 * to open a type it can do,
11463e425b96SJulian Elischer 		 */
11477ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
1148fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
11497ca0641bSAndrey A. Chernov 			case FD_360:
11507ca0641bSAndrey A. Chernov 				return(ENXIO);
1151ed2fa05eSAndrey A. Chernov 			case FD_720:
1152b39c878eSAndrey A. Chernov 				if (   type != FD_820
1153b39c878eSAndrey A. Chernov 				    && type != FD_800
1154ed2fa05eSAndrey A. Chernov 				   )
1155ed2fa05eSAndrey A. Chernov 					return(ENXIO);
1156ed2fa05eSAndrey A. Chernov 				break;
11577ca0641bSAndrey A. Chernov 			case FD_1200:
1158b39c878eSAndrey A. Chernov 				switch (type) {
1159b39c878eSAndrey A. Chernov 				case FD_1480:
1160b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1161fa4700b4SAndrey A. Chernov 					break;
11627ca0641bSAndrey A. Chernov 				case FD_1440:
1163b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1164b39c878eSAndrey A. Chernov 					break;
1165b39c878eSAndrey A. Chernov 				case FD_820:
1166b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1167b39c878eSAndrey A. Chernov 					break;
1168b39c878eSAndrey A. Chernov 				case FD_800:
1169b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1170b39c878eSAndrey A. Chernov 					break;
1171b39c878eSAndrey A. Chernov 				case FD_720:
1172b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1173b39c878eSAndrey A. Chernov 					break;
1174b39c878eSAndrey A. Chernov 				case FD_360:
1175b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1176b39c878eSAndrey A. Chernov 					break;
1177b39c878eSAndrey A. Chernov 				default:
1178b39c878eSAndrey A. Chernov 					return(ENXIO);
1179b39c878eSAndrey A. Chernov 				}
1180b39c878eSAndrey A. Chernov 				break;
1181b39c878eSAndrey A. Chernov 			case FD_1440:
1182b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1183b39c878eSAndrey A. Chernov 				    && type != FD_1480
1184ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1185b39c878eSAndrey A. Chernov 				    && type != FD_820
1186b39c878eSAndrey A. Chernov 				    && type != FD_800
1187b39c878eSAndrey A. Chernov 				    && type != FD_720
11887ca0641bSAndrey A. Chernov 				    )
1189dffff499SAndrey A. Chernov 					return(ENXIO);
1190fa4700b4SAndrey A. Chernov 				break;
11917ca0641bSAndrey A. Chernov 			}
11927ca0641bSAndrey A. Chernov 		}
1193fa4700b4SAndrey A. Chernov 	}
1194b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
11955b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
11965b81b6b3SRodney W. Grimes 
11975b81b6b3SRodney W. Grimes 	return 0;
11985b81b6b3SRodney W. Grimes }
11995b81b6b3SRodney W. Grimes 
1200381fe1aaSGarrett Wollman int
1201671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
12025b81b6b3SRodney W. Grimes {
12035b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
1204b99f0a4aSAndrew Moore 
1205b99f0a4aSAndrew Moore #if NFT > 0
12063a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
12073a2f7427SDavid Greenman 
1208b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
12093a2f7427SDavid Greenman 		return ftclose(dev, flags);
1210b99f0a4aSAndrew Moore #endif
12115b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
12123a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
1213dc16046fSJoerg Wunsch 
12145b81b6b3SRodney W. Grimes 	return(0);
12155b81b6b3SRodney W. Grimes }
12165b81b6b3SRodney W. Grimes 
1217f7ea2f55SJulian Elischer static int
1218f7ea2f55SJulian Elischer fdread(dev_t dev, struct uio *uio, int ioflag)
1219f7ea2f55SJulian Elischer {
1220f7ea2f55SJulian Elischer 	return (physio(fdstrategy, NULL, dev, 1, minphys, uio));
1221f7ea2f55SJulian Elischer }
1222f7ea2f55SJulian Elischer 
1223f7ea2f55SJulian Elischer static int
1224f7ea2f55SJulian Elischer fdwrite(dev_t dev, struct uio *uio, int ioflag)
1225f7ea2f55SJulian Elischer {
1226f7ea2f55SJulian Elischer 	return (physio(fdstrategy, NULL, dev, 0, minphys, uio));
1227f7ea2f55SJulian Elischer }
1228f7ea2f55SJulian Elischer 
12295b81b6b3SRodney W. Grimes 
12303a2f7427SDavid Greenman /****************************************************************************/
12313a2f7427SDavid Greenman /*                               fdstrategy                                 */
12323a2f7427SDavid Greenman /****************************************************************************/
12333a2f7427SDavid Greenman void
12343a2f7427SDavid Greenman fdstrategy(struct buf *bp)
12353a2f7427SDavid Greenman {
1236bb6382faSJoerg Wunsch 	unsigned nblocks, blknum, cando;
12373a2f7427SDavid Greenman  	int	s;
12383a2f7427SDavid Greenman  	fdcu_t	fdcu;
12393a2f7427SDavid Greenman  	fdu_t	fdu;
12403a2f7427SDavid Greenman  	fdc_p	fdc;
12413a2f7427SDavid Greenman  	fd_p	fd;
12423a2f7427SDavid Greenman 	size_t	fdblk;
12433a2f7427SDavid Greenman 
12443a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
12453a2f7427SDavid Greenman 	fd = &fd_data[fdu];
12463a2f7427SDavid Greenman 	fdc = fd->fdc;
12473a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
12483a2f7427SDavid Greenman 
12493a2f7427SDavid Greenman #if NFT > 0
12503a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
12513a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
1252d3628763SRodney W. Grimes 		bp->b_error = ENODEV;
12533a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
12543a2f7427SDavid Greenman 		goto bad;
12553a2f7427SDavid Greenman 	}
12563a2f7427SDavid Greenman 	/* check for controller already busy with tape */
12573a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
12583a2f7427SDavid Greenman 		bp->b_error = EBUSY;
12593a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
12603a2f7427SDavid Greenman 		goto bad;
12613a2f7427SDavid Greenman 	}
12623a2f7427SDavid Greenman #endif
1263d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
12643a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
12653a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1266dc5df763SJoerg Wunsch 			printf(
12676a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1268702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
12693a2f7427SDavid Greenman 			bp->b_error = EINVAL;
12703a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
12713a2f7427SDavid Greenman 			goto bad;
12723a2f7427SDavid Greenman 		}
12733a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
12743a2f7427SDavid Greenman 			bp->b_error = EINVAL;
12753a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
12763a2f7427SDavid Greenman 			goto bad;
12773a2f7427SDavid Greenman 		}
12783a2f7427SDavid Greenman 	}
12793a2f7427SDavid Greenman 
12803a2f7427SDavid Greenman 	/*
12813a2f7427SDavid Greenman 	 * Set up block calculations.
12823a2f7427SDavid Greenman 	 */
1283bb6382faSJoerg Wunsch 	if (bp->b_blkno > 20000000) {
1284bb6382faSJoerg Wunsch 		/*
1285bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1286bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1287bb6382faSJoerg Wunsch 		 */
1288bb6382faSJoerg Wunsch 		bp->b_error = EINVAL;
12893a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
12903a2f7427SDavid Greenman 		goto bad;
12913a2f7427SDavid Greenman 	}
1292bb6382faSJoerg Wunsch 	blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk;
1293bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1294bb6382faSJoerg Wunsch 	bp->b_resid = 0;
1295bb6382faSJoerg Wunsch 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
1296bb6382faSJoerg Wunsch 		if (blknum <= nblocks) {
1297bb6382faSJoerg Wunsch 			cando = (nblocks - blknum) * fdblk;
1298bb6382faSJoerg Wunsch 			bp->b_resid = bp->b_bcount - cando;
1299bb6382faSJoerg Wunsch 			if (cando == 0)
1300bb6382faSJoerg Wunsch 				goto bad;	/* not actually bad but EOF */
1301bb6382faSJoerg Wunsch 		} else {
1302bb6382faSJoerg Wunsch 			bp->b_error = EINVAL;
1303bb6382faSJoerg Wunsch 			bp->b_flags |= B_ERROR;
1304bb6382faSJoerg Wunsch 			goto bad;
1305bb6382faSJoerg Wunsch 		}
1306bb6382faSJoerg Wunsch 	}
130727513ca7SBruce Evans  	bp->b_pblkno = bp->b_blkno;
13083a2f7427SDavid Greenman 	s = splbio();
130902a19910SJustin T. Gibbs 	bufqdisksort(&fdc->head, bp);
131002a19910SJustin T. Gibbs 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); /* a good idea */
13113a2f7427SDavid Greenman 	fdstart(fdcu);
13123a2f7427SDavid Greenman 	splx(s);
13133a2f7427SDavid Greenman 	return;
13143a2f7427SDavid Greenman 
13153a2f7427SDavid Greenman bad:
13163a2f7427SDavid Greenman 	biodone(bp);
13173a2f7427SDavid Greenman }
13183a2f7427SDavid Greenman 
13193e425b96SJulian Elischer #ifdef	SLICE
13203e425b96SJulian Elischer /****************************************************************************/
13213e425b96SJulian Elischer /*                               fdsIOreq                                   */
13223e425b96SJulian Elischer /****************************************************************************/
13233e425b96SJulian Elischer static void
13243e425b96SJulian Elischer fdsIOreq(void *private ,struct buf *bp)
13253e425b96SJulian Elischer {
13263e425b96SJulian Elischer 	unsigned nblocks, blknum, cando;
13273e425b96SJulian Elischer  	int	s;
13283e425b96SJulian Elischer  	fdcu_t	fdcu;
13293e425b96SJulian Elischer  	fdu_t	fdu;
13303e425b96SJulian Elischer  	fdc_p	fdc;
13313e425b96SJulian Elischer  	fd_p	fd;
13323e425b96SJulian Elischer 	size_t	fdblk;
13333e425b96SJulian Elischer 	struct subdev *sd;
13343e425b96SJulian Elischer 
13353e425b96SJulian Elischer 	sd = private;
13363e425b96SJulian Elischer 	fd = sd->drive;
13373e425b96SJulian Elischer  	fdu = fd->unit;
13383e425b96SJulian Elischer 	fdc = fd->fdc;
13393e425b96SJulian Elischer 	fdcu = fdc->fdcu;
13403e425b96SJulian Elischer 
13413e425b96SJulian Elischer 	/* check for controller already busy with tape */
13423e425b96SJulian Elischer 	if (fdc->flags & FDC_TAPE_BUSY) {
13433e425b96SJulian Elischer 		bp->b_error = EBUSY;
13443e425b96SJulian Elischer 		bp->b_flags |= B_ERROR;
13453e425b96SJulian Elischer 		goto bad;
13463e425b96SJulian Elischer 	}
13473e425b96SJulian Elischer 	bp->b_driver1 = sd; /* squirrel away which device.. */
13483e425b96SJulian Elischer 	bp->b_resid = 0;
13493e425b96SJulian Elischer 	s = splbio();
13503e425b96SJulian Elischer 	bufqdisksort(&fdc->head, bp);
13513e425b96SJulian Elischer 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); /* a good idea */
13523e425b96SJulian Elischer 	fdstart(fdcu);
13533e425b96SJulian Elischer 	splx(s);
13543e425b96SJulian Elischer 	return;
13553e425b96SJulian Elischer 
13563e425b96SJulian Elischer bad:
13573e425b96SJulian Elischer 	biodone(bp);
13583e425b96SJulian Elischer 	return;
13593e425b96SJulian Elischer }
13603e425b96SJulian Elischer #endif	/* SLICE */
13613e425b96SJulian Elischer 
13625b81b6b3SRodney W. Grimes /***************************************************************\
13635b81b6b3SRodney W. Grimes *				fdstart				*
13645b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
13655b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
13665b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
13675b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
13685b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
13695b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
13705b81b6b3SRodney W. Grimes \***************************************************************/
1371381fe1aaSGarrett Wollman static void
1372dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
13735b81b6b3SRodney W. Grimes {
13745b81b6b3SRodney W. Grimes 	int s;
13755b81b6b3SRodney W. Grimes 
13765b81b6b3SRodney W. Grimes 	s = splbio();
13775b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
13785b81b6b3SRodney W. Grimes 	{
13795b81b6b3SRodney W. Grimes 		fdintr(fdcu);
13805b81b6b3SRodney W. Grimes 	}
13815b81b6b3SRodney W. Grimes 	splx(s);
13825b81b6b3SRodney W. Grimes }
13835b81b6b3SRodney W. Grimes 
1384381fe1aaSGarrett Wollman static void
1385d0917939SPaul Richards fd_timeout(void *arg1)
13865b81b6b3SRodney W. Grimes {
1387381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
13885b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
13893a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
139017542807SPoul-Henning Kamp 	struct buf *bp;
1391f5f7ba03SJordan K. Hubbard 	int s;
13925b81b6b3SRodney W. Grimes 
139302a19910SJustin T. Gibbs 	bp = bufq_first(&fdc_data[fdcu].head);
13945b81b6b3SRodney W. Grimes 
13953a2f7427SDavid Greenman 	/*
13963a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
13973a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
13983a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
13993a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
14003a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
14013a2f7427SDavid Greenman 	 */
14025b81b6b3SRodney W. Grimes 
14033a2f7427SDavid Greenman 	s = splbio();
14043a2f7427SDavid Greenman 
14053a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
14063a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
14073a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
14083a2f7427SDavid Greenman 	{
14093a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
14103a2f7427SDavid Greenman 		/* yup, it is; kill it now */
14113a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
14123a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
14133a2f7427SDavid Greenman 	}
14145b81b6b3SRodney W. Grimes 
14155b81b6b3SRodney W. Grimes 	if (bp)
14165b81b6b3SRodney W. Grimes 	{
14175b81b6b3SRodney W. Grimes 		retrier(fdcu);
14183a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
14195b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
14205b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
14215b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
14225b81b6b3SRodney W. Grimes 	}
14235b81b6b3SRodney W. Grimes 	else
14245b81b6b3SRodney W. Grimes 	{
14255b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
14265b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
14275b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
14285b81b6b3SRodney W. Grimes 	}
1429f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1430f5f7ba03SJordan K. Hubbard 	splx(s);
14315b81b6b3SRodney W. Grimes }
14325b81b6b3SRodney W. Grimes 
14335b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1434381fe1aaSGarrett Wollman static void
1435d0917939SPaul Richards fd_pseudointr(void *arg1)
14365b81b6b3SRodney W. Grimes {
1437381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
14385b81b6b3SRodney W. Grimes 	int	s;
14393a2f7427SDavid Greenman 
14405b81b6b3SRodney W. Grimes 	s = splbio();
14415b81b6b3SRodney W. Grimes 	fdintr(fdcu);
14425b81b6b3SRodney W. Grimes 	splx(s);
14435b81b6b3SRodney W. Grimes }
14445b81b6b3SRodney W. Grimes 
14455b81b6b3SRodney W. Grimes /***********************************************************************\
14465b81b6b3SRodney W. Grimes *                                 fdintr				*
14475b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
14485b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
14495b81b6b3SRodney W. Grimes \***********************************************************************/
1450381fe1aaSGarrett Wollman void
1451381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
14525b81b6b3SRodney W. Grimes {
14535b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1454b99f0a4aSAndrew Moore #if NFT > 0
1455b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1456b99f0a4aSAndrew Moore 
1457b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1458b99f0a4aSAndrew Moore 		(ftintr(fdu));
1459b99f0a4aSAndrew Moore 	else
1460b99f0a4aSAndrew Moore #endif
1461381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1462381fe1aaSGarrett Wollman 			;
14635b81b6b3SRodney W. Grimes }
14645b81b6b3SRodney W. Grimes 
14655b81b6b3SRodney W. Grimes /***********************************************************************\
14665b81b6b3SRodney W. Grimes * The controller state machine.						*
14675b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
14685b81b6b3SRodney W. Grimes \***********************************************************************/
14693a2f7427SDavid Greenman static int
1470dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
14715b81b6b3SRodney W. Grimes {
14723e425b96SJulian Elischer 	struct subdev *sd;
14734ccc87c5SPoul-Henning Kamp 	int read, format, head, sec = 0, sectrac, st0, cyl, st3;
1474bb6382faSJoerg Wunsch 	unsigned blknum = 0, b_cylinder = 0;
14755b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
14765b81b6b3SRodney W. Grimes 	fd_p fd;
147717542807SPoul-Henning Kamp 	register struct buf *bp;
1478b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
14793a2f7427SDavid Greenman 	size_t fdblk;
14805b81b6b3SRodney W. Grimes 
148102a19910SJustin T. Gibbs 	bp = bufq_first(&fdc->head);
148217542807SPoul-Henning Kamp 	if(!bp) {
14835b81b6b3SRodney W. Grimes 		/***********************************************\
14845b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
14855b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
14865b81b6b3SRodney W. Grimes 		\***********************************************/
14875b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
14885b81b6b3SRodney W. Grimes 		if(fdc->fd)
14895b81b6b3SRodney W. Grimes 		{
14906a0e6f42SRodney W. Grimes 			printf("fd%d: unexpected valid fd pointer\n",
14913a2f7427SDavid Greenman 			       fdc->fdu);
14925b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
14935b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
14945b81b6b3SRodney W. Grimes 		}
14955b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
14965b81b6b3SRodney W. Grimes  		return(0);
14975b81b6b3SRodney W. Grimes 	}
14983e425b96SJulian Elischer #ifdef	SLICE
14993e425b96SJulian Elischer 	sd = bp->b_driver1;
15003e425b96SJulian Elischer 	fd = sd->drive;
15013e425b96SJulian Elischer  	fdu = fd->unit;
15023e425b96SJulian Elischer #else
15035b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
15045b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
15053e425b96SJulian Elischer #endif
15063a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
15075b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
15085b81b6b3SRodney W. Grimes 	{
15096a0e6f42SRodney W. Grimes 		printf("fd%d: confused fd pointers\n", fdu);
15105b81b6b3SRodney W. Grimes 	}
15115b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1512b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1513bb6382faSJoerg Wunsch 	if(format) {
1514ab3f7469SPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->b_data;
1515bb6382faSJoerg Wunsch 		fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1516bb6382faSJoerg Wunsch 			- (char *)finfo;
1517bb6382faSJoerg Wunsch 	}
1518bb6382faSJoerg Wunsch 	if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
15193e425b96SJulian Elischer 		blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk +
1520bb6382faSJoerg Wunsch 			fd->skip/fdblk;
1521bb6382faSJoerg Wunsch 		b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1522bb6382faSJoerg Wunsch 	}
15235b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
15245b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
15255b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
152602a19910SJustin T. Gibbs 	untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle);
152727651b7fSJustin T. Gibbs 	fd->toffhandle = timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
15285b81b6b3SRodney W. Grimes 	switch (fdc->state)
15295b81b6b3SRodney W. Grimes 	{
15305b81b6b3SRodney W. Grimes 	case DEVIDLE:
15315b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
15325b81b6b3SRodney W. Grimes 		fdc->retry = 0;
15335b81b6b3SRodney W. Grimes 		fd->skip = 0;
15345b81b6b3SRodney W. Grimes 		fdc->fd = fd;
15355b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
15363a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
15373a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
15385b81b6b3SRodney W. Grimes 		/*******************************************************\
15395b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
1540dc733423SDag-Erling Smørgrav 		* it will start up in its own good time		*
15415b81b6b3SRodney W. Grimes 		\*******************************************************/
15425b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
15435b81b6b3SRodney W. Grimes 		{
15445b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
15455b81b6b3SRodney W. Grimes 			return(0); /* come back later */
15465b81b6b3SRodney W. Grimes 		}
15475b81b6b3SRodney W. Grimes 		/*******************************************************\
15485b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
15495b81b6b3SRodney W. Grimes 		\*******************************************************/
15505b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
15515b81b6b3SRodney W. Grimes 		{
15525b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
15535b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
15545b81b6b3SRodney W. Grimes 			return(0);
15555b81b6b3SRodney W. Grimes 		}
15565b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
15575b81b6b3SRodney W. Grimes 		{
15583a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
15595b81b6b3SRodney W. Grimes 		}
15605e235068SJordan K. Hubbard 		fdc->state = DOSEEK;
15615b81b6b3SRodney W. Grimes 		break;
15625b81b6b3SRodney W. Grimes 	case DOSEEK:
1563bb6382faSJoerg Wunsch 		if (b_cylinder == (unsigned)fd->track)
15645b81b6b3SRodney W. Grimes 		{
15655b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
15665b81b6b3SRodney W. Grimes 			break;
15675b81b6b3SRodney W. Grimes 		}
1568dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1569bb6382faSJoerg Wunsch 			   fd->fdsu, b_cylinder * fd->ft->steptrac,
1570dc5df763SJoerg Wunsch 			   0))
1571dc8603e3SJoerg Wunsch 		{
1572dc8603e3SJoerg Wunsch 			/*
1573dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1574dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1575dc8603e3SJoerg Wunsch 			 */
1576dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1577dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1578dc8603e3SJoerg Wunsch 		}
1579dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
15805b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
15815b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15825b81b6b3SRodney W. Grimes 	case SEEKWAIT:
15835b81b6b3SRodney W. Grimes 		/* allow heads to settle */
158404b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
15855b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
15865b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15875b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
15885b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1589dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
15905b81b6b3SRodney W. Grimes 		{
1591bb6382faSJoerg Wunsch 			int descyl = b_cylinder * fd->ft->steptrac;
15923a2f7427SDavid Greenman 			do {
15933a2f7427SDavid Greenman 				/*
1594dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1595dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1596dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1597dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1598dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1599dc5df763SJoerg Wunsch 				 *
1600dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1601dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1602dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1603dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1604dc5df763SJoerg Wunsch 				 * real interrupt condition.
1605dc5df763SJoerg Wunsch 				 *
1606dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1607dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1608dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1609dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1610dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1611dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1612dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
16133a2f7427SDavid Greenman 				 */
1614dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1615dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1616dc5df763SJoerg Wunsch 					return 0;
1617dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1618dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1619dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
16203a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1621dc5df763SJoerg Wunsch 
16223a2f7427SDavid Greenman 			if (0 == descyl)
16233a2f7427SDavid Greenman 			{
1624dc5df763SJoerg Wunsch 				int failed = 0;
16253a2f7427SDavid Greenman 				/*
16263a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
16273a2f7427SDavid Greenman 				 * really there
16283a2f7427SDavid Greenman 				 */
1629dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1630dc5df763SJoerg Wunsch 					failed = 1;
16313a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
16323a2f7427SDavid Greenman 					printf(
16333a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
16343a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1635dc5df763SJoerg Wunsch 					failed = 1;
1636dc5df763SJoerg Wunsch 				}
1637dc5df763SJoerg Wunsch 
1638dc5df763SJoerg Wunsch 				if (failed)
1639dc5df763SJoerg Wunsch 				{
16403a2f7427SDavid Greenman 					if(fdc->retry < 3)
16413a2f7427SDavid Greenman 						fdc->retry = 3;
16423a2f7427SDavid Greenman 					return(retrier(fdcu));
16433a2f7427SDavid Greenman 				}
16443a2f7427SDavid Greenman 			}
1645dc5df763SJoerg Wunsch 
16465b81b6b3SRodney W. Grimes 			if (cyl != descyl)
16475b81b6b3SRodney W. Grimes 			{
16483a2f7427SDavid Greenman 				printf(
16493a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
16502d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
1651e5d7d243SBruce Evans 				if (fdc->retry < 3)
1652e5d7d243SBruce Evans 					fdc->retry = 3;
16535b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
16545b81b6b3SRodney W. Grimes 			}
16555b81b6b3SRodney W. Grimes 		}
16565b81b6b3SRodney W. Grimes 
1657bb6382faSJoerg Wunsch 		fd->track = b_cylinder;
1658ab3f7469SPoul-Henning Kamp 		isa_dmastart(bp->b_flags, bp->b_data+fd->skip,
16593a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
16605b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
16615b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
16625b81b6b3SRodney W. Grimes 		head = sec / sectrac;
16635b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
16643a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
16653a2f7427SDavid Greenman 
16663a2f7427SDavid Greenman 		if(format || !read)
16673a2f7427SDavid Greenman 		{
16683a2f7427SDavid Greenman 			/* make sure the drive is writable */
1669dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1670dc8603e3SJoerg Wunsch 			{
1671dc8603e3SJoerg Wunsch 				/* stuck controller? */
1672dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1673dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1674dc8603e3SJoerg Wunsch 			}
16753a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
16763a2f7427SDavid Greenman 			{
16773a2f7427SDavid Greenman 				/*
16783a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
16793a2f7427SDavid Greenman 				 * in order to force the current operation
16803a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
16813a2f7427SDavid Greenman 				 * error - all error handling is done
16823a2f7427SDavid Greenman 				 * by the retrier()
16833a2f7427SDavid Greenman 				 */
16843a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
16853a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
16863a2f7427SDavid Greenman 				fdc->status[2] = 0;
16873a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
16883a2f7427SDavid Greenman 				fdc->status[4] = head;
16893a2f7427SDavid Greenman 				fdc->status[5] = sec;
16903a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
16913a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
16923a2f7427SDavid Greenman 				return (1);
16933a2f7427SDavid Greenman 			}
16943a2f7427SDavid Greenman 		}
16955b81b6b3SRodney W. Grimes 
1696b39c878eSAndrey A. Chernov 		if(format)
1697b39c878eSAndrey A. Chernov 		{
1698b39c878eSAndrey A. Chernov 			/* formatting */
1699dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1700dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1701dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1702dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1703dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1704dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1705dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1706dc5df763SJoerg Wunsch 				  0))
1707dc8603e3SJoerg Wunsch 			{
1708dc8603e3SJoerg Wunsch 				/* controller fell over */
1709dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1710dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1711dc8603e3SJoerg Wunsch 			}
1712b39c878eSAndrey A. Chernov 		}
1713b39c878eSAndrey A. Chernov 		else
1714b39c878eSAndrey A. Chernov 		{
1715dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1716dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1717dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1718dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1719dc5df763SJoerg Wunsch 				   head,
1720dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1721dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1722dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1723dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1724dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1725dc5df763SJoerg Wunsch 				   0))
17265b81b6b3SRodney W. Grimes 			{
1727dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1728dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1729dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
17305b81b6b3SRodney W. Grimes 			}
1731b39c878eSAndrey A. Chernov 		}
17325b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
173302a19910SJustin T. Gibbs 		fd->tohandle = timeout(fd_timeout, (caddr_t)fdcu, hz);
17345b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
17355b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
173602a19910SJustin T. Gibbs 		untimeout(fd_timeout, (caddr_t)fdcu, fd->tohandle);
1737dc5df763SJoerg Wunsch 
1738dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
17395b81b6b3SRodney W. Grimes 		{
1740dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1741dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1742dc5df763SJoerg Wunsch 			return retrier(fdcu);
17435b81b6b3SRodney W. Grimes   		}
1744dc5df763SJoerg Wunsch 
17453a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1746dc5df763SJoerg Wunsch 
17473a2f7427SDavid Greenman 		/* FALLTHROUGH */
1748dc5df763SJoerg Wunsch 
17493a2f7427SDavid Greenman 	case IOTIMEDOUT:
1750ab3f7469SPoul-Henning Kamp 		isa_dmadone(bp->b_flags, bp->b_data+fd->skip,
17513a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
17523a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
17535b81b6b3SRodney W. Grimes 		{
17543a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
17553a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1756b39c878eSAndrey A. Chernov                                 /*
17573a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
17583a2f7427SDavid Greenman 				 * and didn't release it in time for the
17593a2f7427SDavid Greenman 				 * next FDC transfer.
17603a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
17613a2f7427SDavid Greenman 				 * count. (vak)
1762b39c878eSAndrey A. Chernov                                  */
1763b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1764b39c878eSAndrey A. Chernov                                 return (1);
1765b39c878eSAndrey A. Chernov                         }
17663a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
17673a2f7427SDavid Greenman 				&& fdc->retry < 6)
17683a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
17693a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
17703a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
17713a2f7427SDavid Greenman 				&& fdc->retry < 3)
17723a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
17735b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
17745b81b6b3SRodney W. Grimes 		}
17755b81b6b3SRodney W. Grimes 		/* All OK */
17763a2f7427SDavid Greenman 		fd->skip += fdblk;
1777bb6382faSJoerg Wunsch 		if (!format && fd->skip < bp->b_bcount - bp->b_resid)
17785b81b6b3SRodney W. Grimes 		{
17795b81b6b3SRodney W. Grimes 			/* set up next transfer */
17805b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
17815b81b6b3SRodney W. Grimes 		}
17825b81b6b3SRodney W. Grimes 		else
17835b81b6b3SRodney W. Grimes 		{
17845b81b6b3SRodney W. Grimes 			/* ALL DONE */
17855b81b6b3SRodney W. Grimes 			fd->skip = 0;
178602a19910SJustin T. Gibbs 			bufq_remove(&fdc->head, bp);
17875b81b6b3SRodney W. Grimes 			biodone(bp);
17885b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
17895b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
17905b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
17915b81b6b3SRodney W. Grimes 		}
17925b81b6b3SRodney W. Grimes 		return(1);
17935b81b6b3SRodney W. Grimes 	case RESETCTLR:
17943a2f7427SDavid Greenman 		fdc_reset(fdc);
17955b81b6b3SRodney W. Grimes 		fdc->retry++;
17965b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
17975b81b6b3SRodney W. Grimes 		break;
17985b81b6b3SRodney W. Grimes 	case STARTRECAL:
17990e317d05SJoerg Wunsch 		/* XXX clear the fdc results from the last reset, if any. */
18000e317d05SJoerg Wunsch 		{
18010e317d05SJoerg Wunsch 			int i;
18020e317d05SJoerg Wunsch 			for (i = 0; i < 4; i++)
18030e317d05SJoerg Wunsch 				(void)fd_sense_int(fdc, &st0, &cyl);
18040e317d05SJoerg Wunsch 		}
18050e317d05SJoerg Wunsch 
1806dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1807dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1808dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1809dc8603e3SJoerg Wunsch 		{
1810dc8603e3SJoerg Wunsch 			/* arrgl */
1811dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1812dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1813dc8603e3SJoerg Wunsch 		}
18145b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
18155b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
18165b81b6b3SRodney W. Grimes 	case RECALWAIT:
18175b81b6b3SRodney W. Grimes 		/* allow heads to settle */
181804b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
18195b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
18205b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
18215b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
18223a2f7427SDavid Greenman 		do {
18233a2f7427SDavid Greenman 			/*
1824dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
18253a2f7427SDavid Greenman 			 */
1826dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1827dc5df763SJoerg Wunsch 				return 0;
1828dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1829dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1830dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
18313a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
18323a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
18335b81b6b3SRodney W. Grimes 		{
1834dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1835dc8603e3SJoerg Wunsch 				/*
1836dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1837dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1838dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1839dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1840dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1841dc8603e3SJoerg Wunsch 				 * time in a line
1842dc8603e3SJoerg Wunsch 				 */
1843dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1844dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
18453a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
18465b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
18475b81b6b3SRodney W. Grimes 		}
18485b81b6b3SRodney W. Grimes 		fd->track = 0;
18495b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
18505b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
18515b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
18525b81b6b3SRodney W. Grimes 	case MOTORWAIT:
18535b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
18545b81b6b3SRodney W. Grimes 		{
18555b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
18565b81b6b3SRodney W. Grimes 		}
18575e235068SJordan K. Hubbard 		/*
18585e235068SJordan K. Hubbard 		 * since the controller was off, it has lost its
18595e235068SJordan K. Hubbard 		 * idea about the current track it were; thus,
18605e235068SJordan K. Hubbard 		 * recalibrate the bastard
18615e235068SJordan K. Hubbard 		 */
18625e235068SJordan K. Hubbard 		fdc->state = STARTRECAL;
18635b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
18645b81b6b3SRodney W. Grimes 	default:
1865dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1866dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1867a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
18685b81b6b3SRodney W. Grimes 			       fdc->status[0],
18695b81b6b3SRodney W. Grimes 			       fdc->status[1],
18705b81b6b3SRodney W. Grimes 			       fdc->status[2],
18715b81b6b3SRodney W. Grimes 			       fdc->status[3],
18725b81b6b3SRodney W. Grimes 			       fdc->status[4],
18735b81b6b3SRodney W. Grimes 			       fdc->status[5],
18745b81b6b3SRodney W. Grimes 			       fdc->status[6] );
18753a2f7427SDavid Greenman 		else
1876dac0f2dbSJoerg Wunsch 			printf("No status available   ");
1877dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1878dac0f2dbSJoerg Wunsch 		{
1879dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
18805b81b6b3SRodney W. Grimes 			return(0);
18815b81b6b3SRodney W. Grimes 		}
1882dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1883dac0f2dbSJoerg Wunsch 		return(0);
1884dac0f2dbSJoerg Wunsch 	}
1885dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
18865b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
18875b81b6b3SRodney W. Grimes }
18885b81b6b3SRodney W. Grimes 
1889aaf08d94SGarrett Wollman static int
1890f5f7ba03SJordan K. Hubbard retrier(fdcu)
1891f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
18925b81b6b3SRodney W. Grimes {
18933e425b96SJulian Elischer 	struct subdev *sd;
18945b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
189517542807SPoul-Henning Kamp 	register struct buf *bp;
18963e425b96SJulian Elischer #ifdef	SLICE
18973e425b96SJulian Elischer 	struct fd_data *fd;
18983e425b96SJulian Elischer 	int fdu;
18993e425b96SJulian Elischer #endif
19005b81b6b3SRodney W. Grimes 
190102a19910SJustin T. Gibbs 	bp = bufq_first(&fdc->head);
19025b81b6b3SRodney W. Grimes 
19033e425b96SJulian Elischer #ifdef	SLICE
19043e425b96SJulian Elischer 	sd = bp->b_driver1;
19053e425b96SJulian Elischer 	fd = sd->drive;
19063e425b96SJulian Elischer  	fdu = fd->unit;
19073e425b96SJulian Elischer 	if(fd->options & FDOPT_NORETRY)
19083e425b96SJulian Elischer 		goto fail;
19093e425b96SJulian Elischer #else
19103a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
19113a2f7427SDavid Greenman 		goto fail;
19123e425b96SJulian Elischer #endif
19135b81b6b3SRodney W. Grimes 	switch(fdc->retry)
19145b81b6b3SRodney W. Grimes 	{
19155b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
19165b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
19175b81b6b3SRodney W. Grimes 		break;
19185b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
19195b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
19205b81b6b3SRodney W. Grimes 		break;
19215b81b6b3SRodney W. Grimes 	case 6:
19225b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
19235b81b6b3SRodney W. Grimes 		break;
19245b81b6b3SRodney W. Grimes 	case 7:
19255b81b6b3SRodney W. Grimes 		break;
19265b81b6b3SRodney W. Grimes 	default:
19273a2f7427SDavid Greenman 	fail:
19285b81b6b3SRodney W. Grimes 		{
19293e425b96SJulian Elischer #ifdef SLICE
19303e425b96SJulian Elischer 			printf("fd%d: hard error, block %d ", fdu,
19313e425b96SJulian Elischer 				fd->skip / DEV_BSIZE);
19323e425b96SJulian Elischer #else
19337ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
19347ca0641bSAndrey A. Chernov 			/* Trick diskerr */
19353a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
19363a2f7427SDavid Greenman 				    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
193792ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
19383a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
19393a2f7427SDavid Greenman 				(struct disklabel *)NULL);
19407ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
19413e425b96SJulian Elischer #endif /* !SLICE */
1942dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1943dc5df763SJoerg Wunsch 			{
1944dc5df763SJoerg Wunsch 				printf(
1945a838d83dSBruce Evans 			" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
1946dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1947dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1948dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1949dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1950dc5df763SJoerg Wunsch 				       fdc->status[5]);
1951dc5df763SJoerg Wunsch 			}
1952dc5df763SJoerg Wunsch 			else
1953dc5df763SJoerg Wunsch 				printf(" (No status)\n");
19545b81b6b3SRodney W. Grimes 		}
19555b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
19565b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
1957bb6382faSJoerg Wunsch 		bp->b_resid += bp->b_bcount - fdc->fd->skip;
195802a19910SJustin T. Gibbs 		bufq_remove(&fdc->head, bp);
19595b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
19605b81b6b3SRodney W. Grimes 		biodone(bp);
196192ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
19625b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
19635b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1964f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
196592ed385aSRodney W. Grimes 		return(1);
19665b81b6b3SRodney W. Grimes 	}
19675b81b6b3SRodney W. Grimes 	fdc->retry++;
19685b81b6b3SRodney W. Grimes 	return(1);
19695b81b6b3SRodney W. Grimes }
19705b81b6b3SRodney W. Grimes 
19713e425b96SJulian Elischer #ifdef	SLICE
19723e425b96SJulian Elischer static int
19733e425b96SJulian Elischer fdformat( struct subdev *sd, struct fd_formb *finfo, struct proc *p)
19743e425b96SJulian Elischer #else	/* !SLICE */
1975b39c878eSAndrey A. Chernov static int
1976b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1977b39c878eSAndrey A. Chernov 	dev_t dev;
1978b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1979b39c878eSAndrey A. Chernov 	struct proc *p;
19803e425b96SJulian Elischer #endif	/* !SLICE */
1981b39c878eSAndrey A. Chernov {
1982b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1983b39c878eSAndrey A. Chernov  	fd_p	fd;
1984b39c878eSAndrey A. Chernov 
1985b39c878eSAndrey A. Chernov 	struct buf *bp;
1986b39c878eSAndrey A. Chernov 	int rv = 0, s;
19873a2f7427SDavid Greenman 	size_t fdblk;
1988b39c878eSAndrey A. Chernov 
19893e425b96SJulian Elischer #ifdef	SLICE
19903e425b96SJulian Elischer  	fd	= sd->drive;
19913e425b96SJulian Elischer  	fdu	= fd->unit;
19923e425b96SJulian Elischer #else
1993b39c878eSAndrey A. Chernov  	fdu	= FDUNIT(minor(dev));
1994b39c878eSAndrey A. Chernov 	fd	= &fd_data[fdu];
19953e425b96SJulian Elischer #endif
19963a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1997b39c878eSAndrey A. Chernov 
1998b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1999b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
2000b39c878eSAndrey A. Chernov 	if(bp == 0)
2001b39c878eSAndrey A. Chernov 		return ENOBUFS;
200282f5379bSJoerg Wunsch 	/*
200382f5379bSJoerg Wunsch 	 * keep the process from being swapped
200482f5379bSJoerg Wunsch 	 */
200582f5379bSJoerg Wunsch 	p->p_flag |= P_PHYSIO;
2006b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
2007b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
2008b39c878eSAndrey A. Chernov 	bp->b_proc = p;
2009b39c878eSAndrey A. Chernov 
2010b39c878eSAndrey A. Chernov 	/*
2011b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
2012b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
2013b39c878eSAndrey A. Chernov 	 */
2014b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
20153a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2016b39c878eSAndrey A. Chernov 
2017b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2018ab3f7469SPoul-Henning Kamp 	bp->b_data = (caddr_t)finfo;
2019b39c878eSAndrey A. Chernov 
2020b39c878eSAndrey A. Chernov 	/* now do the format */
20213e425b96SJulian Elischer #ifdef SLICE
20223e425b96SJulian Elischer 	bp->b_driver1 = sd;
20233e425b96SJulian Elischer 	fdsIOreq(sd, bp);
20243e425b96SJulian Elischer #else	/* !SLICE */
20253e425b96SJulian Elischer 	bp->b_dev = dev;
2026b39c878eSAndrey A. Chernov 	fdstrategy(bp);
20273e425b96SJulian Elischer #endif	/* !SLICE */
2028b39c878eSAndrey A. Chernov 
2029b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
2030b39c878eSAndrey A. Chernov 	s = splbio();
2031b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
2032b39c878eSAndrey A. Chernov 	{
20335e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2034b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
2035b39c878eSAndrey A. Chernov 			break;
2036b39c878eSAndrey A. Chernov 	}
2037b39c878eSAndrey A. Chernov 	splx(s);
2038b39c878eSAndrey A. Chernov 
203982f5379bSJoerg Wunsch 	if(rv == EWOULDBLOCK) {
2040b39c878eSAndrey A. Chernov 		/* timed out */
2041b39c878eSAndrey A. Chernov 		rv = EIO;
204282f5379bSJoerg Wunsch 		biodone(bp);
204382f5379bSJoerg Wunsch 	}
20443a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
20453a2f7427SDavid Greenman 		rv = bp->b_error;
204682f5379bSJoerg Wunsch 	/*
204782f5379bSJoerg Wunsch 	 * allow the process to be swapped
204882f5379bSJoerg Wunsch 	 */
204982f5379bSJoerg Wunsch 	p->p_flag &= ~P_PHYSIO;
2050b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
2051b39c878eSAndrey A. Chernov 	return rv;
2052b39c878eSAndrey A. Chernov }
2053b39c878eSAndrey A. Chernov 
2054f5f7ba03SJordan K. Hubbard /*
2055671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
2056f5f7ba03SJordan K. Hubbard  */
20575b81b6b3SRodney W. Grimes 
20583e425b96SJulian Elischer static int
2059b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
2060f5f7ba03SJordan K. Hubbard 	dev_t dev;
2061ecbb00a2SDoug Rabson 	u_long cmd;
2062f5f7ba03SJordan K. Hubbard 	caddr_t addr;
2063f5f7ba03SJordan K. Hubbard 	int flag;
2064b39c878eSAndrey A. Chernov 	struct proc *p;
2065f5f7ba03SJordan K. Hubbard {
20663a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
20673a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
20683a2f7427SDavid Greenman 	size_t fdblk;
20693a2f7427SDavid Greenman 
2070f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
2071f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
2072f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
20733a2f7427SDavid Greenman 	int error = 0;
2074f5f7ba03SJordan K. Hubbard 
2075b99f0a4aSAndrew Moore #if NFT > 0
2076a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
2077a60eff27SNate Williams 
2078a60eff27SNate Williams 	/* check for a tape ioctl */
2079a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
2080b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
2081b99f0a4aSAndrew Moore #endif
2082b99f0a4aSAndrew Moore 
20833e425b96SJulian Elischer #ifdef	SLICE
20843e425b96SJulian Elischer 	/*
20853e425b96SJulian Elischer 	 * if SLICE is defined then only ft accesses come here
20863e425b96SJulian Elischer 	 * so break the rest off to another function for SLICE access.
20873e425b96SJulian Elischer 	 */
20883e425b96SJulian Elischer 	return (ENOTTY);
20893e425b96SJulian Elischer }
20903e425b96SJulian Elischer 
20913e425b96SJulian Elischer /*
20923e425b96SJulian Elischer  * Slice ioctls come here
20933e425b96SJulian Elischer  */
20943e425b96SJulian Elischer static int
2095498b291eSDoug Rabson fdsioctl( void *private, u_long cmd, caddr_t addr, int flag, struct proc *p)
20963e425b96SJulian Elischer {
20973e425b96SJulian Elischer 	struct	subdev *sd = private;
20983e425b96SJulian Elischer  	fd_p	fd	= sd->drive;
20993e425b96SJulian Elischer  	fdu_t	fdu	= fd->unit;
21003e425b96SJulian Elischer 	fdc_p	fdc	= fd->fdc;
21013e425b96SJulian Elischer 	fdcu_t	fdcu	= fdc->fdcu;
21023e425b96SJulian Elischer 	size_t	fdblk;
21033e425b96SJulian Elischer 	int	error	= 0;
21043e425b96SJulian Elischer #endif	/* SLICE */
21053a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2106f5f7ba03SJordan K. Hubbard 
2107f5f7ba03SJordan K. Hubbard 	switch (cmd)
2108f5f7ba03SJordan K. Hubbard 	{
21093e425b96SJulian Elischer #ifndef	SLICE
2110f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
2111f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
2112f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
21133a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
211492ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
2115f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
2116f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
2117f5f7ba03SJordan K. Hubbard 
2118191e1a59SBruce Evans 		if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl)
2119191e1a59SBruce Evans 		    == NULL)
2120f5f7ba03SJordan K. Hubbard 			error = 0;
2121f5f7ba03SJordan K. Hubbard 		else
2122f5f7ba03SJordan K. Hubbard 			error = EINVAL;
2123f5f7ba03SJordan K. Hubbard 
2124f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
2125f5f7ba03SJordan K. Hubbard 		break;
2126f5f7ba03SJordan K. Hubbard 
2127f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
2128f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2129f5f7ba03SJordan K. Hubbard 			error = EBADF;
2130f5f7ba03SJordan K. Hubbard 		break;
2131f5f7ba03SJordan K. Hubbard 
2132f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
2133f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2134f5f7ba03SJordan K. Hubbard 			error = EBADF;
2135f5f7ba03SJordan K. Hubbard 		break;
2136f5f7ba03SJordan K. Hubbard 
2137f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
2138f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2139f5f7ba03SJordan K. Hubbard 		{
2140f5f7ba03SJordan K. Hubbard 			error = EBADF;
2141f5f7ba03SJordan K. Hubbard 			break;
2142f5f7ba03SJordan K. Hubbard 		}
2143f5f7ba03SJordan K. Hubbard 
2144f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
2145f5f7ba03SJordan K. Hubbard 
2146191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
2147191e1a59SBruce Evans 					  (u_long)0)) != 0)
2148f5f7ba03SJordan K. Hubbard 			break;
2149f5f7ba03SJordan K. Hubbard 
2150b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
215154c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
2152b39c878eSAndrey A. Chernov 		break;
21533e425b96SJulian Elischer #endif	/* !SLICE */
2154b39c878eSAndrey A. Chernov 	case FD_FORM:
2155b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
2156b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
2157b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
2158b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
2159b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
2160b39c878eSAndrey A. Chernov 		else
21613e425b96SJulian Elischer #ifdef	SLICE
21623e425b96SJulian Elischer 			error = fdformat(sd, (struct fd_formb *)addr, p);
21633e425b96SJulian Elischer #else
2164b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
21653e425b96SJulian Elischer #endif
2166b39c878eSAndrey A. Chernov 		break;
2167b39c878eSAndrey A. Chernov 
2168b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
21693e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2170f5f7ba03SJordan K. Hubbard 		break;
2171f5f7ba03SJordan K. Hubbard 
21723a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
21733a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
21743a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
21753a2f7427SDavid Greenman 			return EPERM;
21763e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
21773a2f7427SDavid Greenman 		break;
21783a2f7427SDavid Greenman 
21793a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
21803e425b96SJulian Elischer 		*(int *)addr = fd->options;
21813a2f7427SDavid Greenman 		break;
21823a2f7427SDavid Greenman 
21833a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
21843e425b96SJulian Elischer 		fd->options = *(int *)addr;
21853a2f7427SDavid Greenman 		break;
21863a2f7427SDavid Greenman 
2187f5f7ba03SJordan K. Hubbard 	default:
21883a2f7427SDavid Greenman 		error = ENOTTY;
2189f5f7ba03SJordan K. Hubbard 		break;
2190f5f7ba03SJordan K. Hubbard 	}
2191f5f7ba03SJordan K. Hubbard 	return (error);
2192f5f7ba03SJordan K. Hubbard }
2193f5f7ba03SJordan K. Hubbard 
21947146c13eSJulian Elischer 
21957146c13eSJulian Elischer static fd_devsw_installed = 0;
21967146c13eSJulian Elischer 
219787f6c662SJulian Elischer static void 	fd_drvinit(void *notused )
21987146c13eSJulian Elischer {
219987f6c662SJulian Elischer 
22007146c13eSJulian Elischer 	if( ! fd_devsw_installed ) {
2201f7ea2f55SJulian Elischer 		cdevsw_add_generic(BDEV_MAJOR,CDEV_MAJOR, &fd_cdevsw);
22027146c13eSJulian Elischer 		fd_devsw_installed = 1;
22037146c13eSJulian Elischer 	}
22047146c13eSJulian Elischer }
220587f6c662SJulian Elischer 
220687f6c662SJulian Elischer SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL)
220787f6c662SJulian Elischer 
22083e425b96SJulian Elischer 
22093e425b96SJulian Elischer #ifdef	SLICE
22103e425b96SJulian Elischer static int
22113e425b96SJulian Elischer fdsopen(void *private, int flags, int mode, struct proc *p)
22123e425b96SJulian Elischer {
22133e425b96SJulian Elischer 	struct subdev *sd;
22143e425b96SJulian Elischer 
22153e425b96SJulian Elischer 	sd = private;
22163e425b96SJulian Elischer 
221726d3bf5fSJulian Elischer 	if((flags & (FREAD|FWRITE)) != 0) {
22187f2f1b78SJulian Elischer 		return(Fdopen(makedev(0,sd->minor), flags , mode, p));
221926d3bf5fSJulian Elischer 	} else {
22207f2f1b78SJulian Elischer 		return(fdclose(makedev(0,sd->minor), 0 , mode, p));
222126d3bf5fSJulian Elischer 	}
22223e425b96SJulian Elischer }
22233e425b96SJulian Elischer 
222426d3bf5fSJulian Elischer #if 0
22253e425b96SJulian Elischer static void
22263e425b96SJulian Elischer fdsclose(void *private, int flags, int mode, struct proc *p)
22273e425b96SJulian Elischer {
22283e425b96SJulian Elischer 	struct subdev *sd;
22293e425b96SJulian Elischer 
22303e425b96SJulian Elischer 	sd = private;
22313e425b96SJulian Elischer 
22323e425b96SJulian Elischer 	fdclose(makedev(0,sd->minor), 0 , 0, p);
22333e425b96SJulian Elischer 	return ;
22343e425b96SJulian Elischer }
223526d3bf5fSJulian Elischer #endif	/* 0 */
22363e425b96SJulian Elischer 
22373e425b96SJulian Elischer #endif	/* SLICE */
2238f5f7ba03SJordan K. Hubbard #endif
22393e425b96SJulian Elischer 
22403a2f7427SDavid Greenman /*
22413a2f7427SDavid Greenman  * Hello emacs, these are the
22423a2f7427SDavid Greenman  * Local Variables:
22433a2f7427SDavid Greenman  *  c-indent-level:               8
22443a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
22453a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
22463a2f7427SDavid Greenman  *  c-brace-offset:              -8
22473a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
22483a2f7427SDavid Greenman  *  c-argdecl-indent:             8
22493a2f7427SDavid Greenman  *  c-label-offset:              -8
22503a2f7427SDavid Greenman  *  c++-hanging-braces:           1
22513a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
22523a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
22533a2f7427SDavid Greenman  *  c++-friend-offset:            0
22543a2f7427SDavid Greenman  * End:
22553a2f7427SDavid Greenman  */
2256