xref: /freebsd/sys/dev/fdc/fdc.c (revision 0e317d05cc50e56df8422a90780933bea6dec2b9)
187f6c662SJulian Elischer /*
25b81b6b3SRodney W. Grimes  * Copyright (c) 1990 The Regents of the University of California.
35b81b6b3SRodney W. Grimes  * All rights reserved.
45b81b6b3SRodney W. Grimes  *
55b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
65b81b6b3SRodney W. Grimes  * Don Ahn.
75b81b6b3SRodney W. Grimes  *
8dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994 by
93a2f7427SDavid Greenman  *  jc@irbs.UUCP (John Capo)
103a2f7427SDavid Greenman  *  vak@zebub.msk.su (Serge Vakulenko)
113a2f7427SDavid Greenman  *  ache@astral.msk.su (Andrew A. Chernov)
12dc16046fSJoerg Wunsch  *
13dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994, 1995 by
143a2f7427SDavid Greenman  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
15dc5df763SJoerg Wunsch  *  dufault@hda.com (Peter Dufault)
163a2f7427SDavid Greenman  *
175b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
185b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
195b81b6b3SRodney W. Grimes  * are met:
205b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
215b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
225b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
235b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
245b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
255b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
265b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
275b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
285b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
295b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
305b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
315b81b6b3SRodney W. Grimes  *    without specific prior written permission.
325b81b6b3SRodney W. Grimes  *
335b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
345b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
355b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
365b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
375b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
385b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
395b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
405b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
415b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
425b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
435b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
445b81b6b3SRodney W. Grimes  *
45dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
460e317d05SJoerg Wunsch  *	$Id: fd.c,v 1.82 1996/03/29 11:45:12 bde Exp $
475b81b6b3SRodney W. Grimes  *
485b81b6b3SRodney W. Grimes  */
495b81b6b3SRodney W. Grimes 
50b99f0a4aSAndrew Moore #include "ft.h"
51b99f0a4aSAndrew Moore #if NFT < 1
52b99f0a4aSAndrew Moore #undef NFDC
53b99f0a4aSAndrew Moore #endif
545b81b6b3SRodney W. Grimes #include "fd.h"
555b81b6b3SRodney W. Grimes 
56b99f0a4aSAndrew Moore #if NFDC > 0
57b99f0a4aSAndrew Moore 
58b99f0a4aSAndrew Moore #include <sys/param.h>
59b99f0a4aSAndrew Moore #include <sys/systm.h>
60b99f0a4aSAndrew Moore #include <sys/kernel.h>
61b99f0a4aSAndrew Moore #include <sys/conf.h>
62b99f0a4aSAndrew Moore #include <sys/file.h>
63b99f0a4aSAndrew Moore #include <sys/ioctl.h>
64671e2ceeSBruce Evans #include <machine/clock.h>
65b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h>
66b99f0a4aSAndrew Moore #include <sys/disklabel.h>
67671e2ceeSBruce Evans #include <sys/diskslice.h>
6816b04b6aSJoerg Wunsch #include <machine/cpu.h>
69b99f0a4aSAndrew Moore #include <sys/buf.h>
70b99f0a4aSAndrew Moore #include <sys/uio.h>
71b99f0a4aSAndrew Moore #include <sys/malloc.h>
723a2f7427SDavid Greenman #include <sys/proc.h>
73b99f0a4aSAndrew Moore #include <sys/syslog.h>
7492200632SGarrett Wollman #include <sys/devconf.h>
7592200632SGarrett Wollman #include <sys/dkstat.h>
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>
888af5d536SJulian Elischer #endif
895b81b6b3SRodney W. Grimes 
9087f6c662SJulian Elischer 
9192200632SGarrett Wollman static int fd_goaway(struct kern_devconf *, int);
9292200632SGarrett Wollman static int fdc_goaway(struct kern_devconf *, int);
934b2af45fSPoul-Henning Kamp static int fd_externalize(struct kern_devconf *, struct sysctl_req *);
945e235068SJordan K. Hubbard 
955e235068SJordan K. Hubbard /*
965e235068SJordan K. Hubbard  * Templates for the kern_devconf structures used when we attach.
975e235068SJordan K. Hubbard  */
985e235068SJordan K. Hubbard static struct kern_devconf kdc_fd[NFD] = { {
995e235068SJordan K. Hubbard 	0, 0, 0,		/* filled in by kern_devconf.c */
1005e235068SJordan K. Hubbard 	"fd", 0, { MDDT_DISK, 0 },
1015e235068SJordan K. Hubbard 	fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN,
1025e235068SJordan K. Hubbard 	0,			/* parent */
1035e235068SJordan K. Hubbard 	0,			/* parentdata */
104dc16046fSJoerg Wunsch 	DC_UNCONFIGURED,	/* state */
1056c0081e9SGarrett Wollman 	"floppy disk",
1066c0081e9SGarrett Wollman 	DC_CLS_DISK		/* class */
1075e235068SJordan K. Hubbard } };
1085e235068SJordan K. Hubbard 
1095e235068SJordan K. Hubbard struct kern_devconf kdc_fdc[NFDC] = { {
1105e235068SJordan K. Hubbard 	0, 0, 0,		/* filled in by kern_devconf.c */
1115e235068SJordan K. Hubbard 	"fdc", 0, { MDDT_ISA, 0, "bio" },
1125e235068SJordan K. Hubbard 	isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN,
1135e235068SJordan K. Hubbard 	0,			/* parent */
1145e235068SJordan K. Hubbard 	0,			/* parentdata */
115dc16046fSJoerg Wunsch 	DC_UNCONFIGURED,	/* state */
1166c0081e9SGarrett Wollman 	"floppy disk/tape controller",
1176c0081e9SGarrett Wollman 	DC_CLS_MISC		/* class */
1185e235068SJordan K. Hubbard } };
1195e235068SJordan K. Hubbard 
1205e235068SJordan K. Hubbard static inline void
1215e235068SJordan K. Hubbard fd_registerdev(int ctlr, int unit)
1225e235068SJordan K. Hubbard {
1235e235068SJordan K. Hubbard 	if(unit != 0)
1245e235068SJordan K. Hubbard 		kdc_fd[unit] = kdc_fd[0];
1255e235068SJordan K. Hubbard 
1265e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_unit = unit;
1275e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_parent = &kdc_fdc[ctlr];
1285e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_parentdata = 0;
1295e235068SJordan K. Hubbard 	dev_attach(&kdc_fd[unit]);
1305e235068SJordan K. Hubbard }
1315e235068SJordan K. Hubbard 
1325e235068SJordan K. Hubbard static inline void
1335e235068SJordan K. Hubbard fdc_registerdev(struct isa_device *dvp)
1345e235068SJordan K. Hubbard {
1355e235068SJordan K. Hubbard 	int unit = dvp->id_unit;
1365e235068SJordan K. Hubbard 
1375e235068SJordan K. Hubbard 	if(unit != 0)
1385e235068SJordan K. Hubbard 		kdc_fdc[unit] = kdc_fdc[0];
1395e235068SJordan K. Hubbard 
1405e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_unit = unit;
1415e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_parent = &kdc_isa0;
1425e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_parentdata = dvp;
1435e235068SJordan K. Hubbard 	dev_attach(&kdc_fdc[unit]);
1445e235068SJordan K. Hubbard }
1455e235068SJordan K. Hubbard 
1465e235068SJordan K. Hubbard static int
1475e235068SJordan K. Hubbard fdc_goaway(struct kern_devconf *kdc, int force)
1485e235068SJordan K. Hubbard {
1495e235068SJordan K. Hubbard 	if(force) {
1505e235068SJordan K. Hubbard 		dev_detach(kdc);
1515e235068SJordan K. Hubbard 		return 0;
1525e235068SJordan K. Hubbard 	} else {
1535e235068SJordan K. Hubbard 		return EBUSY;	/* XXX fix */
1545e235068SJordan K. Hubbard 	}
1555e235068SJordan K. Hubbard }
1565e235068SJordan K. Hubbard 
1575e235068SJordan K. Hubbard static int
1585e235068SJordan K. Hubbard fd_goaway(struct kern_devconf *kdc, int force)
1595e235068SJordan K. Hubbard {
1605e235068SJordan K. Hubbard 	dev_detach(kdc);
1615e235068SJordan K. Hubbard 	return 0;
1625e235068SJordan K. Hubbard }
16392200632SGarrett Wollman 
164671e2ceeSBruce Evans #define	b_cylin	b_resid		/* XXX now spelled b_cylinder elsewhere */
1655b81b6b3SRodney W. Grimes 
166b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
167b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
1685b81b6b3SRodney W. Grimes 
1693a2f7427SDavid Greenman /*
1703a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
1713a2f7427SDavid Greenman  * on the controller
1723a2f7427SDavid Greenman  */
1733a2f7427SDavid Greenman #define id_physid id_scsiid
1743a2f7427SDavid Greenman 
175dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
176dc5df763SJoerg Wunsch #define FD_FAILED -1
177dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
178dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
179dc5df763SJoerg Wunsch 
180b39c878eSAndrey A. Chernov #define NUMTYPES 14
181b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1827ca0641bSAndrey A. Chernov 
1833a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
184b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
185b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
186b99f0a4aSAndrew Moore #define FD_1720         1
187b99f0a4aSAndrew Moore #define FD_1480         2
188b99f0a4aSAndrew Moore #define FD_1440         3
189b99f0a4aSAndrew Moore #define FD_1200         4
190b99f0a4aSAndrew Moore #define FD_820          5
191b99f0a4aSAndrew Moore #define FD_800          6
192b99f0a4aSAndrew Moore #define FD_720          7
193b99f0a4aSAndrew Moore #define FD_360          8
194ed2fa05eSAndrey A. Chernov 
195b99f0a4aSAndrew Moore #define FD_1480in5_25   9
196b99f0a4aSAndrew Moore #define FD_1440in5_25   10
197b99f0a4aSAndrew Moore #define FD_820in5_25    11
198b99f0a4aSAndrew Moore #define FD_800in5_25    12
199b99f0a4aSAndrew Moore #define FD_720in5_25    13
200b99f0a4aSAndrew Moore #define FD_360in5_25    14
201b99f0a4aSAndrew Moore 
2027ca0641bSAndrey A. Chernov 
2036f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
2045b81b6b3SRodney W. Grimes {
205126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
206126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
207126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
208126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
209126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
210126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
211126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
212b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
213ed2fa05eSAndrey A. Chernov 
214126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
215126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
216126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
217126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
218126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
219126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
2205b81b6b3SRodney W. Grimes };
2215b81b6b3SRodney W. Grimes 
222b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
223dc16046fSJoerg Wunsch 
2245b81b6b3SRodney W. Grimes /***********************************************************************\
2255b81b6b3SRodney W. Grimes * Per controller structure.						*
2265b81b6b3SRodney W. Grimes \***********************************************************************/
227b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
2285b81b6b3SRodney W. Grimes 
2295b81b6b3SRodney W. Grimes /***********************************************************************\
2305b81b6b3SRodney W. Grimes * Per drive structure.							*
231b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
2325b81b6b3SRodney W. Grimes \***********************************************************************/
2336f4e0bebSPoul-Henning Kamp static struct fd_data {
234b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2355b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2363a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
2375b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
2385b81b6b3SRodney W. Grimes 	int	flags;
2395b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
2405b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
2415b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
2425b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
2435b81b6b3SRodney W. Grimes 	int	skip;
2445b81b6b3SRodney W. Grimes 	int	hddrv;
245dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
2465b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
2473a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
24892200632SGarrett Wollman 	int	dkunit;		/* disk stats unit number */
24987f6c662SJulian Elischer #ifdef	DEVFS
250c8f2fe8dSBruce Evans 	void	*bdev;
251c8f2fe8dSBruce Evans 	void	*cdev;
25287f6c662SJulian Elischer #endif
2535b81b6b3SRodney W. Grimes } fd_data[NFD];
2545b81b6b3SRodney W. Grimes 
2555b81b6b3SRodney W. Grimes /***********************************************************************\
2565b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
2575b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
2585b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
2595b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
2605b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
2615b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
2625b81b6b3SRodney W. Grimes \***********************************************************************/
263b99f0a4aSAndrew Moore 
2643a2f7427SDavid Greenman #if NFT > 0
2653a2f7427SDavid Greenman int ftopen(dev_t, int);
2663a2f7427SDavid Greenman int ftintr(ftu_t ftu);
2673a2f7427SDavid Greenman int ftclose(dev_t, int);
2683a2f7427SDavid Greenman void ftstrategy(struct buf *);
2693a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
2703a2f7427SDavid Greenman int ftdump(dev_t);
2713a2f7427SDavid Greenman int ftsize(dev_t);
27274fa89f4SRodney W. Grimes int ftattach(struct isa_device *, struct isa_device *, int);
2733a2f7427SDavid Greenman #endif
2745b81b6b3SRodney W. Grimes 
2753a2f7427SDavid Greenman /* autoconfig functions */
2763a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2773a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2783a2f7427SDavid Greenman 
2793a2f7427SDavid Greenman /* needed for ft driver, thus exported */
2803a2f7427SDavid Greenman int in_fdc(fdcu_t);
2813a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
2823a2f7427SDavid Greenman 
2833a2f7427SDavid Greenman /* internal functions */
2843a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
2853a2f7427SDavid Greenman #  define TURNON 1
2863a2f7427SDavid Greenman #  define TURNOFF 0
2873a2f7427SDavid Greenman static timeout_t fd_turnoff;
2883a2f7427SDavid Greenman static timeout_t fd_motor_on;
2893a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2903a2f7427SDavid Greenman static void fdc_reset(fdc_p);
291b5e8ce9fSBruce Evans static int fd_in(fdcu_t, int *);
2923a2f7427SDavid Greenman static void fdstart(fdcu_t);
2933a2f7427SDavid Greenman static timeout_t fd_timeout;
2943a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2953a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
296aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2973a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2983a2f7427SDavid Greenman 
299aaf08d94SGarrett Wollman 
3005b81b6b3SRodney W. Grimes #define DEVIDLE		0
3015b81b6b3SRodney W. Grimes #define FINDWORK	1
3025b81b6b3SRodney W. Grimes #define	DOSEEK		2
3035b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
3045b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
3055b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
3065b81b6b3SRodney W. Grimes #define	STARTRECAL	6
3075b81b6b3SRodney W. Grimes #define	RESETCTLR	7
3085b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
3095b81b6b3SRodney W. Grimes #define	RECALWAIT	9
3105b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
3115b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
3125b81b6b3SRodney W. Grimes 
3135b81b6b3SRodney W. Grimes #ifdef	DEBUG
3145b81b6b3SRodney W. Grimes char *fdstates[] =
3155b81b6b3SRodney W. Grimes {
3165b81b6b3SRodney W. Grimes "DEVIDLE",
3175b81b6b3SRodney W. Grimes "FINDWORK",
3185b81b6b3SRodney W. Grimes "DOSEEK",
3195b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
3205b81b6b3SRodney W. Grimes "IOCOMPLETE",
3215b81b6b3SRodney W. Grimes "RECALCOMPLETE",
3225b81b6b3SRodney W. Grimes "STARTRECAL",
3235b81b6b3SRodney W. Grimes "RESETCTLR",
3245b81b6b3SRodney W. Grimes "SEEKWAIT",
3255b81b6b3SRodney W. Grimes "RECALWAIT",
3265b81b6b3SRodney W. Grimes "MOTORWAIT",
3275b81b6b3SRodney W. Grimes "IOTIMEDOUT"
3285b81b6b3SRodney W. Grimes };
3295b81b6b3SRodney W. Grimes 
3303a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
3313a2f7427SDavid Greenman int	fd_debug = 0;
3325b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
3335b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
334381fe1aaSGarrett Wollman #else /* DEBUG */
3355b81b6b3SRodney W. Grimes #define TRACE0(arg)
3365b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
337381fe1aaSGarrett Wollman #endif /* DEBUG */
3385b81b6b3SRodney W. Grimes 
339dc16046fSJoerg Wunsch /* autoconfig structure */
340dc16046fSJoerg Wunsch 
341dc16046fSJoerg Wunsch struct	isa_driver fdcdriver = {
342dc16046fSJoerg Wunsch 	fdprobe, fdattach, "fdc",
343dc16046fSJoerg Wunsch };
344dc16046fSJoerg Wunsch 
34587f6c662SJulian Elischer static	d_open_t	Fdopen;	/* NOTE, not fdopen */
34687f6c662SJulian Elischer static	d_close_t	fdclose;
34787f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
34887f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
34987f6c662SJulian Elischer 
35087f6c662SJulian Elischer #define CDEV_MAJOR 9
35187f6c662SJulian Elischer #define BDEV_MAJOR 2
35287f6c662SJulian Elischer extern	struct cdevsw fd_cdevsw;
353d2f265faSPoul-Henning Kamp static struct bdevsw fd_bdevsw =
35487f6c662SJulian Elischer 	{ Fdopen,	fdclose,	fdstrategy,	fdioctl,	/*2*/
355f332b8a1SBruce Evans 	  nodump,	nopsize,	0,	"fd",	&fd_cdevsw,	-1 };
35687f6c662SJulian Elischer 
357d2f265faSPoul-Henning Kamp static struct cdevsw fd_cdevsw =
35887f6c662SJulian Elischer 	{ Fdopen,	fdclose,	rawread,	rawwrite,	/*9*/
35987f6c662SJulian Elischer 	  fdioctl,	nostop,		nullreset,	nodevtotty,
36087f6c662SJulian Elischer 	  seltrue,	nommap,		fdstrategy,	"fd",
36187f6c662SJulian Elischer 	  &fd_bdevsw,	-1 };
36287f6c662SJulian Elischer 
3636f4e0bebSPoul-Henning Kamp static struct isa_device *fdcdevs[NFDC];
36492200632SGarrett Wollman 
36592200632SGarrett Wollman /*
36692200632SGarrett Wollman  * Provide hw.devconf information.
36792200632SGarrett Wollman  */
36892200632SGarrett Wollman static int
3694b2af45fSPoul-Henning Kamp fd_externalize(struct kern_devconf *kdc, struct sysctl_req *req)
37092200632SGarrett Wollman {
3714b2af45fSPoul-Henning Kamp 	return disk_externalize(fd_data[kdc->kdc_unit].fdsu, req);
37292200632SGarrett Wollman }
37392200632SGarrett Wollman 
374dc5df763SJoerg Wunsch static int
375dc5df763SJoerg Wunsch fdc_err(fdcu_t fdcu, const char *s)
376dc5df763SJoerg Wunsch {
377dc5df763SJoerg Wunsch 	fdc_data[fdcu].fdc_errs++;
37816b04b6aSJoerg Wunsch 	if(s) {
379dc5df763SJoerg Wunsch 		if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX)
3806a0e6f42SRodney W. Grimes 			printf("fdc%d: %s", fdcu, s);
381dc5df763SJoerg Wunsch 		else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX)
382dc5df763SJoerg Wunsch 			printf("fdc%d: too many errors, not logging any more\n",
383dc5df763SJoerg Wunsch 			       fdcu);
38416b04b6aSJoerg Wunsch 	}
385dc5df763SJoerg Wunsch 
386dc5df763SJoerg Wunsch 	return FD_FAILED;
387dc5df763SJoerg Wunsch }
388dc5df763SJoerg Wunsch 
389dc5df763SJoerg Wunsch /*
390dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
391dc5df763SJoerg Wunsch  * Unit number,
392dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
393dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
394dc5df763SJoerg Wunsch  */
395dc5df763SJoerg Wunsch 
3966f4e0bebSPoul-Henning Kamp static int
397dc5df763SJoerg Wunsch fd_cmd(fdcu_t fdcu, int n_out, ...)
398dc5df763SJoerg Wunsch {
399dc5df763SJoerg Wunsch 	u_char cmd;
400dc5df763SJoerg Wunsch 	int n_in;
401dc5df763SJoerg Wunsch 	int n;
402dc5df763SJoerg Wunsch 	va_list ap;
403dc5df763SJoerg Wunsch 
404dc5df763SJoerg Wunsch 	va_start(ap, n_out);
405dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
406dc5df763SJoerg Wunsch 	va_end(ap);
407dc5df763SJoerg Wunsch 	va_start(ap, n_out);
408dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
409dc5df763SJoerg Wunsch 	{
410dc5df763SJoerg Wunsch 		if (out_fdc(fdcu, va_arg(ap, int)) < 0)
411dc5df763SJoerg Wunsch 		{
412dc5df763SJoerg Wunsch 			char msg[50];
413dc5df763SJoerg Wunsch 			sprintf(msg,
414dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
415dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
416dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
417dc5df763SJoerg Wunsch 		}
418dc5df763SJoerg Wunsch 	}
419dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
420dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
421dc5df763SJoerg Wunsch 	{
422dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
423dc5df763SJoerg Wunsch 		if (fd_in(fdcu, ptr) < 0)
424dc5df763SJoerg Wunsch 		{
425dc5df763SJoerg Wunsch 			char msg[50];
426dc5df763SJoerg Wunsch 			sprintf(msg,
427dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
428dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
429dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
430dc5df763SJoerg Wunsch 		}
431dc5df763SJoerg Wunsch 	}
432dc5df763SJoerg Wunsch 
433dc5df763SJoerg Wunsch 	return 0;
434dc5df763SJoerg Wunsch }
435dc5df763SJoerg Wunsch 
4366f4e0bebSPoul-Henning Kamp static int
437dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
438dc5df763SJoerg Wunsch {
439dc5df763SJoerg Wunsch 	int st3;
440dc5df763SJoerg Wunsch 
441dc5df763SJoerg Wunsch 	if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
442dc5df763SJoerg Wunsch 	{
4436a0e6f42SRodney W. Grimes 		return fdc_err(fdc->fdcu, "Sense Drive Status failed\n");
444dc5df763SJoerg Wunsch 	}
445dc5df763SJoerg Wunsch 	if (st3p)
446dc5df763SJoerg Wunsch 		*st3p = st3;
447dc5df763SJoerg Wunsch 
448dc5df763SJoerg Wunsch 	return 0;
449dc5df763SJoerg Wunsch }
450dc5df763SJoerg Wunsch 
4516f4e0bebSPoul-Henning Kamp static int
452dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
453dc5df763SJoerg Wunsch {
454dc5df763SJoerg Wunsch 	int st0, cyl;
455dc5df763SJoerg Wunsch 
456dc5df763SJoerg Wunsch 	int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0);
457dc5df763SJoerg Wunsch 
458dc5df763SJoerg Wunsch 	if (ret)
459dc5df763SJoerg Wunsch 	{
460dc5df763SJoerg Wunsch 		(void)fdc_err(fdc->fdcu,
461dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
462dc5df763SJoerg Wunsch 		return ret;
463dc5df763SJoerg Wunsch 	}
464dc5df763SJoerg Wunsch 
465dc5df763SJoerg Wunsch 	if (st0p)
466dc5df763SJoerg Wunsch 		*st0p = st0;
467dc5df763SJoerg Wunsch 
468dc5df763SJoerg Wunsch 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV)
469dc5df763SJoerg Wunsch 	{
470dc5df763SJoerg Wunsch 		/*
471dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
472dc5df763SJoerg Wunsch 		 */
473dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
474dc5df763SJoerg Wunsch 	}
475dc5df763SJoerg Wunsch 
476dc5df763SJoerg Wunsch 	if (fd_in(fdc->fdcu, &cyl) < 0)
477dc5df763SJoerg Wunsch 	{
478dc5df763SJoerg Wunsch 		return fdc_err(fdc->fdcu, "can't get cyl num\n");
479dc5df763SJoerg Wunsch 	}
480dc5df763SJoerg Wunsch 
481dc5df763SJoerg Wunsch 	if (cylp)
482dc5df763SJoerg Wunsch 		*cylp = cyl;
483dc5df763SJoerg Wunsch 
484dc5df763SJoerg Wunsch 	return 0;
485dc5df763SJoerg Wunsch }
486dc5df763SJoerg Wunsch 
487dc5df763SJoerg Wunsch 
4886f4e0bebSPoul-Henning Kamp static int
489dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
490dc5df763SJoerg Wunsch {
491dc5df763SJoerg Wunsch 	int i, ret;
492b5e8ce9fSBruce Evans 
493dc5df763SJoerg Wunsch 	for (i = 0; i < 7; i++)
494dc5df763SJoerg Wunsch 	{
495b5e8ce9fSBruce Evans 		/*
496b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
497b5e8ce9fSBruce Evans 		 * from the hardware, but fdc_status wants u_longs and
498b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
499b5e8ce9fSBruce Evans 		 */
500b5e8ce9fSBruce Evans 		int status;
501b5e8ce9fSBruce Evans 
502b5e8ce9fSBruce Evans 		ret = fd_in(fdc->fdcu, &status);
503b5e8ce9fSBruce Evans 		fdc->status[i] = status;
504b5e8ce9fSBruce Evans 		if (ret != 0)
505dc5df763SJoerg Wunsch 			break;
506dc5df763SJoerg Wunsch 	}
507dc5df763SJoerg Wunsch 
508dc5df763SJoerg Wunsch 	if (ret == 0)
509dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
510dc5df763SJoerg Wunsch 	else
511dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
512dc5df763SJoerg Wunsch 
513dc5df763SJoerg Wunsch 	return ret;
514dc5df763SJoerg Wunsch }
515dc5df763SJoerg Wunsch 
5165b81b6b3SRodney W. Grimes /****************************************************************************/
5175b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
5185b81b6b3SRodney W. Grimes /****************************************************************************/
519dc5df763SJoerg Wunsch 
5205b81b6b3SRodney W. Grimes /*
5215b81b6b3SRodney W. Grimes  * probe for existance of controller
5225b81b6b3SRodney W. Grimes  */
5233a2f7427SDavid Greenman static int
524dc5df763SJoerg Wunsch fdprobe(struct isa_device *dev)
5255b81b6b3SRodney W. Grimes {
5265b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5275b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
5285b81b6b3SRodney W. Grimes 	{
5296a0e6f42SRodney W. Grimes 		printf("fdc%d: unit used multiple times\n", fdcu);
5305b81b6b3SRodney W. Grimes 		return 0;
5315b81b6b3SRodney W. Grimes 	}
5325b81b6b3SRodney W. Grimes 
53392200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
5345b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
5355b81b6b3SRodney W. Grimes 
5366c0081e9SGarrett Wollman #ifndef DEV_LKM
5376c0081e9SGarrett Wollman 	fdc_registerdev(dev);
5386c0081e9SGarrett Wollman #endif
5396c0081e9SGarrett Wollman 
54016111cedSAndrew Moore 	/* First - lets reset the floppy controller */
5413a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
54216111cedSAndrew Moore 	DELAY(100);
5433a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
54416111cedSAndrew Moore 
5455b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
546dc5df763SJoerg Wunsch 	if (fd_cmd(fdcu,
547dc5df763SJoerg Wunsch 		   3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
548dc5df763SJoerg Wunsch 		   0))
5495b81b6b3SRodney W. Grimes 	{
5505b81b6b3SRodney W. Grimes 		return(0);
5515b81b6b3SRodney W. Grimes 	}
552dc16046fSJoerg Wunsch 	kdc_fdc[fdcu].kdc_state = DC_IDLE;
5535b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
5545b81b6b3SRodney W. Grimes }
5555b81b6b3SRodney W. Grimes 
5565b81b6b3SRodney W. Grimes /*
5575b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
5585b81b6b3SRodney W. Grimes  */
5593a2f7427SDavid Greenman static int
560dc5df763SJoerg Wunsch fdattach(struct isa_device *dev)
5615b81b6b3SRodney W. Grimes {
5623a2f7427SDavid Greenman 	unsigned fdt;
5635b81b6b3SRodney W. Grimes 	fdu_t	fdu;
5645b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5655b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
5665b81b6b3SRodney W. Grimes 	fd_p	fd;
56774fa89f4SRodney W. Grimes 	int	fdsu, st0, st3, i, unithasfd;
568b99f0a4aSAndrew Moore 	struct isa_device *fdup;
569dc5df763SJoerg Wunsch 	int ic_type = 0;
570999422d7SJulian Elischer #ifdef	DEVFS
571c8f2fe8dSBruce Evans 	int	mynor;
572999422d7SJulian Elischer 	char	name[64];
573999422d7SJulian Elischer #endif	/* DEVFS */
57492200632SGarrett Wollman 
5755b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
5765b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
5775b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
578dd87702aSBruce Evans 	isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
5795b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
5803a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
5813a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
5825b81b6b3SRodney W. Grimes 
5835b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
584b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
585b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
586b99f0a4aSAndrew Moore 			continue;
587b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
588b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
589b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
590b99f0a4aSAndrew Moore 			continue;
591b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
592b99f0a4aSAndrew Moore 		/* look up what bios thinks we have */
593b99f0a4aSAndrew Moore 		switch (fdu) {
594b99f0a4aSAndrew Moore 			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
595b99f0a4aSAndrew Moore 				break;
596b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
597b99f0a4aSAndrew Moore 				break;
598b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
599b99f0a4aSAndrew Moore 				break;
600b99f0a4aSAndrew Moore 		}
6015b81b6b3SRodney W. Grimes 		/* is there a unit? */
602b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
603b99f0a4aSAndrew Moore #if NFT > 0
604b99f0a4aSAndrew Moore 		    || (fdsu >= DRVS_PER_CTLR)) {
605b99f0a4aSAndrew Moore #else
606b99f0a4aSAndrew Moore 		) {
60756ef0285SAndrew Moore 			fd->type = NO_TYPE;
608b99f0a4aSAndrew Moore #endif
609b99f0a4aSAndrew Moore #if NFT > 0
610b99f0a4aSAndrew Moore 			/* If BIOS says no floppy, or > 2nd device */
611b99f0a4aSAndrew Moore 			/* Probe for and attach a floppy tape.     */
61274fa89f4SRodney W. Grimes 			/* Tell FT if there was already a disk     */
61374fa89f4SRodney W. Grimes 			/* with this unit number found.            */
61474fa89f4SRodney W. Grimes 
61574fa89f4SRodney W. Grimes 			unithasfd = 0;
61674fa89f4SRodney W. Grimes 			if (fdu < NFD && fd->type != NO_TYPE)
61774fa89f4SRodney W. Grimes 				unithasfd = 1;
61885827d9cSJoerg Wunsch 			if (ftattach(dev, fdup, unithasfd))
619b99f0a4aSAndrew Moore 				continue;
62056ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
621b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
62256ef0285SAndrew Moore #endif
6235b81b6b3SRodney W. Grimes 			continue;
624f5f7ba03SJordan K. Hubbard 		}
6255b81b6b3SRodney W. Grimes 
6265b81b6b3SRodney W. Grimes 		/* select it */
6273a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
6286b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
629dc5df763SJoerg Wunsch 
630dc5df763SJoerg Wunsch 		if (ic_type == 0 &&
631dc5df763SJoerg Wunsch 		    fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
632dc5df763SJoerg Wunsch 		{
6336a0e6f42SRodney W. Grimes 			printf("fdc%d: ", fdcu);
634dc5df763SJoerg Wunsch 			ic_type = (u_char)ic_type;
635dc5df763SJoerg Wunsch 			switch( ic_type ) {
636dc5df763SJoerg Wunsch 			case 0x80:
6376a0e6f42SRodney W. Grimes 				printf("NEC 765\n");
638dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE765;
6396c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6406c0081e9SGarrett Wollman 					"NEC 765 floppy disk/tape controller";
641dc5df763SJoerg Wunsch 				break;
642dc5df763SJoerg Wunsch 			case 0x81:
6436a0e6f42SRodney W. Grimes 				printf("Intel 82077\n");
644dc5df763SJoerg Wunsch 				fdc->fdct = FDC_I82077;
6456c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6466c0081e9SGarrett Wollman 					"Intel 82077 floppy disk/tape controller";
647dc5df763SJoerg Wunsch 				break;
648dc5df763SJoerg Wunsch 			case 0x90:
6496a0e6f42SRodney W. Grimes 				printf("NEC 72065B\n");
650dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE72065;
6516c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6526c0081e9SGarrett Wollman 					"NEC 72065B floppy disk/tape controller";
653dc5df763SJoerg Wunsch 				break;
654dc5df763SJoerg Wunsch 			default:
6556a0e6f42SRodney W. Grimes 				printf("unknown IC type %02x\n", ic_type);
656dc5df763SJoerg Wunsch 				fdc->fdct = FDC_UNKNOWN;
657dc5df763SJoerg Wunsch 				break;
6586b7bd95bSJoerg Wunsch 			}
659dc5df763SJoerg Wunsch 		}
660dc5df763SJoerg Wunsch 		if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
661dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
662dc5df763SJoerg Wunsch 			/* if at track 0, first seek inwards */
663dc5df763SJoerg Wunsch 			/* seek some steps: */
664dc5df763SJoerg Wunsch 			(void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
665dc5df763SJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
666dc5df763SJoerg Wunsch 			(void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
667dc5df763SJoerg Wunsch 		}
668dc5df763SJoerg Wunsch 
669dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
670dc5df763SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
671dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
672dc5df763SJoerg Wunsch 			/* Seek some steps... */
673dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
674dc5df763SJoerg Wunsch 				/* ...wait a moment... */
675dc5df763SJoerg Wunsch 				DELAY(300000);
676dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
677dc5df763SJoerg Wunsch 				(void)fd_sense_int(fdc, 0, 0);
678dc5df763SJoerg Wunsch 			}
679dc5df763SJoerg Wunsch 		}
680dc5df763SJoerg Wunsch 
6816b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
6826b7bd95bSJoerg Wunsch 			/*
6836b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
6846b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
6856b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
6866b7bd95bSJoerg Wunsch 			 * more than 77 steps
6876b7bd95bSJoerg Wunsch 			 */
688dc5df763SJoerg Wunsch 			/* go back to 0: */
689dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
6906b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
6916b7bd95bSJoerg Wunsch 				DELAY(i == 0? 1000000: 300000);
6925b81b6b3SRodney W. Grimes 
6936b7bd95bSJoerg Wunsch 				/* anything responding? */
694dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
695dc5df763SJoerg Wunsch 				(st0 & NE7_ST0_EC) == 0)
6966b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
6976b7bd95bSJoerg Wunsch 			}
698dc5df763SJoerg Wunsch 		}
6996b7bd95bSJoerg Wunsch 
7003a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
7013a2f7427SDavid Greenman 
7023a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
7035b81b6b3SRodney W. Grimes 			continue;
7045b81b6b3SRodney W. Grimes 
705dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
706b99f0a4aSAndrew Moore 		fd->fdc = fdc;
707b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
7083a2f7427SDavid Greenman 		fd->options = 0;
7092d9d0204SRodney W. Grimes 		printf("fd%d: ", fdu);
7105b81b6b3SRodney W. Grimes 
7116c0081e9SGarrett Wollman 		fd_registerdev(fdcu, fdu);
712b99f0a4aSAndrew Moore 		switch (fdt) {
7137ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
7146a0e6f42SRodney W. Grimes 			printf("1.2MB 5.25in\n");
715b99f0a4aSAndrew Moore 			fd->type = FD_1200;
7166c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7176c0081e9SGarrett Wollman 				"1.2MB (1200K) 5.25in floppy disk drive";
718999422d7SJulian Elischer #ifdef	DEVFS
7194d92e198SJulian Elischer 			sprintf(name,"rfd%d.1200",fdu);
720999422d7SJulian Elischer #endif	/* DEVFS */
7217ca0641bSAndrey A. Chernov 			break;
7227ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
7236a0e6f42SRodney W. Grimes 			printf("1.44MB 3.5in\n");
724b99f0a4aSAndrew Moore 			fd->type = FD_1440;
7256c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7266c0081e9SGarrett Wollman 				"1.44MB (1440K) 3.5in floppy disk drive";
727999422d7SJulian Elischer #ifdef	DEVFS
7284d92e198SJulian Elischer 			sprintf(name,"rfd%d.1440",fdu);
729999422d7SJulian Elischer #endif	/* DEVFS */
7307ca0641bSAndrey A. Chernov 			break;
731290dd077SJoerg Wunsch 		case RTCFDT_288M:
73286a727d9SJoerg Wunsch 		case RTCFDT_288M_1:
7336a0e6f42SRodney W. Grimes 			printf("2.88MB 3.5in - 1.44MB mode\n");
734290dd077SJoerg Wunsch 			fd->type = FD_1440;
7356c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7366c0081e9SGarrett Wollman 				"2.88MB (2880K) 3.5in floppy disk drive in 1.44 mode";
737999422d7SJulian Elischer #ifdef	DEVFS
7384d92e198SJulian Elischer 			sprintf(name,"rfd%d.1440",fdu);
739999422d7SJulian Elischer #endif	/* DEVFS */
740290dd077SJoerg Wunsch 			break;
7417ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
7426a0e6f42SRodney W. Grimes 			printf("360KB 5.25in\n");
743b99f0a4aSAndrew Moore 			fd->type = FD_360;
7446c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7456c0081e9SGarrett Wollman 				"360KB 5.25in floppy disk drive";
746999422d7SJulian Elischer #ifdef	DEVFS
7474d92e198SJulian Elischer 			sprintf(name,"rfd%d.360",fdu);
748999422d7SJulian Elischer #endif	/* DEVFS */
7497ca0641bSAndrey A. Chernov 			break;
750ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
7516a0e6f42SRodney W. Grimes 			printf("720KB 3.5in\n");
752b99f0a4aSAndrew Moore 			fd->type = FD_720;
7536c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7546c0081e9SGarrett Wollman 				"720KB 3.5in floppy disk drive";
755999422d7SJulian Elischer #ifdef	DEVFS
7564d92e198SJulian Elischer 			sprintf(name,"rfd%d.720",fdu);
757999422d7SJulian Elischer #endif	/* DEVFS */
758ed2fa05eSAndrey A. Chernov 			break;
7597ca0641bSAndrey A. Chernov 		default:
7606a0e6f42SRodney W. Grimes 			printf("unknown\n");
761b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
762999422d7SJulian Elischer #ifdef	DEVFS
7634d92e198SJulian Elischer 			sprintf(name,"rfd%d.xxxx",fdu);
764999422d7SJulian Elischer #endif	/* DEVFS */
7657ca0641bSAndrey A. Chernov 			break;
7665b81b6b3SRodney W. Grimes 		}
767dc16046fSJoerg Wunsch 		kdc_fd[fdu].kdc_state = DC_IDLE;
768999422d7SJulian Elischer #ifdef DEVFS
769c8f2fe8dSBruce Evans 		mynor = 8 * fdu;
770f85120acSBruce Evans 		fd->bdev = devfs_add_devswf(&fd_bdevsw, mynor, DV_BLK,
771f85120acSBruce Evans 					    UID_ROOT, GID_OPERATOR, 0640,
772f85120acSBruce Evans 					    "%s", name + 1);
773f85120acSBruce Evans 		fd->cdev = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR,
774f85120acSBruce Evans 					    UID_ROOT, GID_OPERATOR, 0640,
775f85120acSBruce Evans 					    "%s", name);
776f85120acSBruce Evans 		dev_linkf(fd->bdev, "fd%d", fdu);
777f85120acSBruce Evans 		dev_linkf(fd->cdev, "rfd%d", fdu);
778999422d7SJulian Elischer #endif /* DEVFS */
77992200632SGarrett Wollman 		if (dk_ndrive < DK_NDRIVE) {
78092200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
7816a0e6f42SRodney W. Grimes 			fd->dkunit = dk_ndrive++;
782671e2ceeSBruce Evans 			/*
783671e2ceeSBruce Evans 			 * XXX assume rate is FDC_500KBPS.
784671e2ceeSBruce Evans 			 */
785671e2ceeSBruce Evans 			dk_wpms[dk_ndrive] = 500000 / 8 / 2;
78692200632SGarrett Wollman 		} else {
78792200632SGarrett Wollman 			fd->dkunit = -1;
78892200632SGarrett Wollman 		}
7895b81b6b3SRodney W. Grimes 	}
7905b81b6b3SRodney W. Grimes 
7913a2f7427SDavid Greenman 	return (1);
7925b81b6b3SRodney W. Grimes }
7935b81b6b3SRodney W. Grimes 
7945b81b6b3SRodney W. Grimes /****************************************************************************/
7955b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
7965e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
7975b81b6b3SRodney W. Grimes /****************************************************************************/
7983a2f7427SDavid Greenman static void
799dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
8005b81b6b3SRodney W. Grimes {
8013a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
8023a2f7427SDavid Greenman 	int needspecify = 0;
8033a2f7427SDavid Greenman 
8043a2f7427SDavid Greenman 	if(turnon) {
8053a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
8063a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
8073a2f7427SDavid Greenman 	} else
8083a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
8093a2f7427SDavid Greenman 
8105e235068SJordan K. Hubbard 	if(!turnon
8115e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
8125e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
8135e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
8145e235068SJordan K. Hubbard 	else {
8153a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
8163a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
8173a2f7427SDavid Greenman 			needspecify = 1;
8183a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
8195b81b6b3SRodney W. Grimes 	}
8205b81b6b3SRodney W. Grimes 
8213a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
8223a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
823dc16046fSJoerg Wunsch 	kdc_fdc[fdcu].kdc_state = (fdout & FDO_FRST)? DC_BUSY: DC_IDLE;
8243a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
8253a2f7427SDavid Greenman 
8263a2f7427SDavid Greenman 	if(needspecify) {
827dc8603e3SJoerg Wunsch 		/*
828dc5df763SJoerg Wunsch 		 * XXX
829dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
830dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
831dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
832dc8603e3SJoerg Wunsch 		 */
833dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
834dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
835dc5df763SJoerg Wunsch 			     0);
8363a2f7427SDavid Greenman 	}
8373a2f7427SDavid Greenman }
8383a2f7427SDavid Greenman 
839381fe1aaSGarrett Wollman static void
840d0917939SPaul Richards fd_turnoff(void *arg1)
8415b81b6b3SRodney W. Grimes {
842381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
843f5f7ba03SJordan K. Hubbard 	int	s;
8445b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
845dc16046fSJoerg Wunsch 
846dc16046fSJoerg Wunsch 	TRACE1("[fd%d: turnoff]", fdu);
8478335c1b8SBruce Evans 
8488335c1b8SBruce Evans 	/*
8498335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
8508335c1b8SBruce Evans 	 * XXX shouldn't even schedule turnoff until drive is inactive
8518335c1b8SBruce Evans 	 * and nothing is queued on it.
8528335c1b8SBruce Evans 	 */
8538335c1b8SBruce Evans 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fdu) {
8548335c1b8SBruce Evans 		timeout(fd_turnoff, arg1, 4 * hz);
8558335c1b8SBruce Evans 		return;
8568335c1b8SBruce Evans 	}
8578335c1b8SBruce Evans 
858f5f7ba03SJordan K. Hubbard 	s = splbio();
8595b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
8603a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
861f5f7ba03SJordan K. Hubbard 	splx(s);
8625b81b6b3SRodney W. Grimes }
8635b81b6b3SRodney W. Grimes 
8643a2f7427SDavid Greenman static void
865d0917939SPaul Richards fd_motor_on(void *arg1)
8665b81b6b3SRodney W. Grimes {
867381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
868f5f7ba03SJordan K. Hubbard 	int	s;
869f5f7ba03SJordan K. Hubbard 
8705b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
871f5f7ba03SJordan K. Hubbard 	s = splbio();
8725b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
8735b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
8745b81b6b3SRodney W. Grimes 	{
875f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
8765b81b6b3SRodney W. Grimes 	}
877f5f7ba03SJordan K. Hubbard 	splx(s);
8785b81b6b3SRodney W. Grimes }
8795b81b6b3SRodney W. Grimes 
8803a2f7427SDavid Greenman static void
881dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
8825b81b6b3SRodney W. Grimes {
8835b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
8845b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
8855b81b6b3SRodney W. Grimes 	{
8863a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
8873a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
8885e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
8895b81b6b3SRodney W. Grimes 	}
8905b81b6b3SRodney W. Grimes }
8915b81b6b3SRodney W. Grimes 
892381fe1aaSGarrett Wollman static void
893dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
8945b81b6b3SRodney W. Grimes {
8953a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
8963a2f7427SDavid Greenman 
8973a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
8983a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8993a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
9003a2f7427SDavid Greenman 	DELAY(100);
9013a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
9023a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
9033a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
9043a2f7427SDavid Greenman 	DELAY(100);
9053a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
9063a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
9073a2f7427SDavid Greenman 
908dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
909dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
910dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
911dc5df763SJoerg Wunsch 		     0);
9125b81b6b3SRodney W. Grimes }
9135b81b6b3SRodney W. Grimes 
9145b81b6b3SRodney W. Grimes /****************************************************************************/
9155b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
9165b81b6b3SRodney W. Grimes /****************************************************************************/
9175b81b6b3SRodney W. Grimes int
918dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
9195b81b6b3SRodney W. Grimes {
9205b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9215b81b6b3SRodney W. Grimes 	int i, j = 100000;
9223a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
9235b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
924dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9256a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
9265b81b6b3SRodney W. Grimes 	if (j <= 0)
92716b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
9285b81b6b3SRodney W. Grimes #ifdef	DEBUG
9293a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
9303a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
9315b81b6b3SRodney W. Grimes 	return(i);
9325b81b6b3SRodney W. Grimes #else
9333a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
9345b81b6b3SRodney W. Grimes #endif
9355b81b6b3SRodney W. Grimes }
9365b81b6b3SRodney W. Grimes 
937dc5df763SJoerg Wunsch /*
938dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
939dc5df763SJoerg Wunsch  */
940b5e8ce9fSBruce Evans static int
941dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
942dc5df763SJoerg Wunsch {
943dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
944dc5df763SJoerg Wunsch 	int i, j = 100000;
945dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
946dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
947dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9486a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
949dc5df763SJoerg Wunsch 	if (j <= 0)
95016b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
951dc5df763SJoerg Wunsch #ifdef	DEBUG
952dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
953dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
954dc5df763SJoerg Wunsch 	*ptr = i;
955dc5df763SJoerg Wunsch 	return 0;
956dc5df763SJoerg Wunsch #else
957dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
958dc5df763SJoerg Wunsch 	if (ptr)
959dc5df763SJoerg Wunsch 		*ptr = i;
960dc5df763SJoerg Wunsch 	return 0;
961dc5df763SJoerg Wunsch #endif
962dc5df763SJoerg Wunsch }
963dc5df763SJoerg Wunsch 
964dc5df763SJoerg Wunsch int
965dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
9665b81b6b3SRodney W. Grimes {
9675b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9683b3837dbSRodney W. Grimes 	int i;
9695b81b6b3SRodney W. Grimes 
9703b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
9713b3837dbSRodney W. Grimes 	i = 100000;
9723a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
9736a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
9743b3837dbSRodney W. Grimes 
9753b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
9763b3837dbSRodney W. Grimes 	i = 100000;
9773a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
97816b04b6aSJoerg Wunsch 	if (i <= 0)
97916b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0);
9803b3837dbSRodney W. Grimes 
9813b3837dbSRodney W. Grimes 	/* Send the command and return */
9823a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
9833a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
9845b81b6b3SRodney W. Grimes 	return (0);
9855b81b6b3SRodney W. Grimes }
9865b81b6b3SRodney W. Grimes 
9875b81b6b3SRodney W. Grimes /****************************************************************************/
9885b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
9895b81b6b3SRodney W. Grimes /****************************************************************************/
990381fe1aaSGarrett Wollman int
991671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
9925b81b6b3SRodney W. Grimes {
9935b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
99420a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
995b99f0a4aSAndrew Moore 	fdc_p	fdc;
9965b81b6b3SRodney W. Grimes 
997b99f0a4aSAndrew Moore #if NFT > 0
998b99f0a4aSAndrew Moore 	/* check for a tape open */
999b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
1000b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
1001b99f0a4aSAndrew Moore #endif
10025b81b6b3SRodney W. Grimes 	/* check bounds */
1003b99f0a4aSAndrew Moore 	if (fdu >= NFD)
1004b99f0a4aSAndrew Moore 		return(ENXIO);
1005b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
1006b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
1007b99f0a4aSAndrew Moore 		return(ENXIO);
1008b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1009b99f0a4aSAndrew Moore 		return(ENXIO);
10107ca0641bSAndrey A. Chernov 	if (type == 0)
10117ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
10127ca0641bSAndrey A. Chernov 	else {
10137ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
1014fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
10157ca0641bSAndrey A. Chernov 			case FD_360:
10167ca0641bSAndrey A. Chernov 				return(ENXIO);
1017ed2fa05eSAndrey A. Chernov 			case FD_720:
1018b39c878eSAndrey A. Chernov 				if (   type != FD_820
1019b39c878eSAndrey A. Chernov 				    && type != FD_800
1020ed2fa05eSAndrey A. Chernov 				   )
1021ed2fa05eSAndrey A. Chernov 					return(ENXIO);
1022ed2fa05eSAndrey A. Chernov 				break;
10237ca0641bSAndrey A. Chernov 			case FD_1200:
1024b39c878eSAndrey A. Chernov 				switch (type) {
1025b39c878eSAndrey A. Chernov 				case FD_1480:
1026b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1027fa4700b4SAndrey A. Chernov 					break;
10287ca0641bSAndrey A. Chernov 				case FD_1440:
1029b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1030b39c878eSAndrey A. Chernov 					break;
1031b39c878eSAndrey A. Chernov 				case FD_820:
1032b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1033b39c878eSAndrey A. Chernov 					break;
1034b39c878eSAndrey A. Chernov 				case FD_800:
1035b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1036b39c878eSAndrey A. Chernov 					break;
1037b39c878eSAndrey A. Chernov 				case FD_720:
1038b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1039b39c878eSAndrey A. Chernov 					break;
1040b39c878eSAndrey A. Chernov 				case FD_360:
1041b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1042b39c878eSAndrey A. Chernov 					break;
1043b39c878eSAndrey A. Chernov 				default:
1044b39c878eSAndrey A. Chernov 					return(ENXIO);
1045b39c878eSAndrey A. Chernov 				}
1046b39c878eSAndrey A. Chernov 				break;
1047b39c878eSAndrey A. Chernov 			case FD_1440:
1048b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1049b39c878eSAndrey A. Chernov 				    && type != FD_1480
1050ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1051b39c878eSAndrey A. Chernov 				    && type != FD_820
1052b39c878eSAndrey A. Chernov 				    && type != FD_800
1053b39c878eSAndrey A. Chernov 				    && type != FD_720
10547ca0641bSAndrey A. Chernov 				    )
1055dffff499SAndrey A. Chernov 					return(ENXIO);
1056fa4700b4SAndrey A. Chernov 				break;
10577ca0641bSAndrey A. Chernov 			}
10587ca0641bSAndrey A. Chernov 		}
1059fa4700b4SAndrey A. Chernov 	}
1060b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
10615b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
1062dc16046fSJoerg Wunsch 	kdc_fd[fdu].kdc_state = DC_BUSY;
10635b81b6b3SRodney W. Grimes 
10645b81b6b3SRodney W. Grimes 	return 0;
10655b81b6b3SRodney W. Grimes }
10665b81b6b3SRodney W. Grimes 
1067381fe1aaSGarrett Wollman int
1068671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
10695b81b6b3SRodney W. Grimes {
10705b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
1071b99f0a4aSAndrew Moore 
1072b99f0a4aSAndrew Moore #if NFT > 0
10733a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
10743a2f7427SDavid Greenman 
1075b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
10763a2f7427SDavid Greenman 		return ftclose(dev, flags);
1077b99f0a4aSAndrew Moore #endif
10785b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
10793a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
1080dc16046fSJoerg Wunsch 	kdc_fd[fdu].kdc_state = DC_IDLE;
1081dc16046fSJoerg Wunsch 
10825b81b6b3SRodney W. Grimes 	return(0);
10835b81b6b3SRodney W. Grimes }
10845b81b6b3SRodney W. Grimes 
10855b81b6b3SRodney W. Grimes 
10863a2f7427SDavid Greenman /****************************************************************************/
10873a2f7427SDavid Greenman /*                               fdstrategy                                 */
10883a2f7427SDavid Greenman /****************************************************************************/
10893a2f7427SDavid Greenman void
10903a2f7427SDavid Greenman fdstrategy(struct buf *bp)
10913a2f7427SDavid Greenman {
10923a2f7427SDavid Greenman 	register struct buf *dp;
10933a2f7427SDavid Greenman 	long nblocks, blknum;
10943a2f7427SDavid Greenman  	int	s;
10953a2f7427SDavid Greenman  	fdcu_t	fdcu;
10963a2f7427SDavid Greenman  	fdu_t	fdu;
10973a2f7427SDavid Greenman  	fdc_p	fdc;
10983a2f7427SDavid Greenman  	fd_p	fd;
10993a2f7427SDavid Greenman 	size_t	fdblk;
11003a2f7427SDavid Greenman 
11013a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
11023a2f7427SDavid Greenman 	fd = &fd_data[fdu];
11033a2f7427SDavid Greenman 	fdc = fd->fdc;
11043a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
11053a2f7427SDavid Greenman 
11063a2f7427SDavid Greenman #if NFT > 0
11073a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
11083a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
1109d3628763SRodney W. Grimes 		bp->b_error = ENODEV;
11103a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11113a2f7427SDavid Greenman 		goto bad;
11123a2f7427SDavid Greenman 	}
11133a2f7427SDavid Greenman 	/* check for controller already busy with tape */
11143a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
11153a2f7427SDavid Greenman 		bp->b_error = EBUSY;
11163a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11173a2f7427SDavid Greenman 		goto bad;
11183a2f7427SDavid Greenman 	}
11193a2f7427SDavid Greenman #endif
1120d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
11213a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
11223a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1123dc5df763SJoerg Wunsch 			printf(
11246a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1125702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
11263a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11273a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11283a2f7427SDavid Greenman 			goto bad;
11293a2f7427SDavid Greenman 		}
11303a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
11313a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11323a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11333a2f7427SDavid Greenman 			goto bad;
11343a2f7427SDavid Greenman 		}
11353a2f7427SDavid Greenman 	}
11363a2f7427SDavid Greenman 
11373a2f7427SDavid Greenman 	/*
11383a2f7427SDavid Greenman 	 * Set up block calculations.
11393a2f7427SDavid Greenman 	 */
11403a2f7427SDavid Greenman 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk;
11413a2f7427SDavid Greenman  	nblocks = fd->ft->size;
11423a2f7427SDavid Greenman 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
11433a2f7427SDavid Greenman 		if (blknum == nblocks) {
11443a2f7427SDavid Greenman 			bp->b_resid = bp->b_bcount;
11453a2f7427SDavid Greenman 		} else {
11463a2f7427SDavid Greenman 			bp->b_error = ENOSPC;
11473a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11483a2f7427SDavid Greenman 		}
11493a2f7427SDavid Greenman 		goto bad;
11503a2f7427SDavid Greenman 	}
11513a2f7427SDavid Greenman  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
115227513ca7SBruce Evans  	bp->b_pblkno = bp->b_blkno;
11533a2f7427SDavid Greenman 	dp = &(fdc->head);
11543a2f7427SDavid Greenman 	s = splbio();
11553a2f7427SDavid Greenman 	disksort(dp, bp);
11565e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */
11573a2f7427SDavid Greenman 	fdstart(fdcu);
11583a2f7427SDavid Greenman 	splx(s);
11593a2f7427SDavid Greenman 	return;
11603a2f7427SDavid Greenman 
11613a2f7427SDavid Greenman bad:
11623a2f7427SDavid Greenman 	biodone(bp);
11633a2f7427SDavid Greenman }
11643a2f7427SDavid Greenman 
11655b81b6b3SRodney W. Grimes /***************************************************************\
11665b81b6b3SRodney W. Grimes *				fdstart				*
11675b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
11685b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
11695b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
11705b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
11715b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
11725b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
11735b81b6b3SRodney W. Grimes \***************************************************************/
1174381fe1aaSGarrett Wollman static void
1175dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
11765b81b6b3SRodney W. Grimes {
11775b81b6b3SRodney W. Grimes 	int s;
11785b81b6b3SRodney W. Grimes 
11795b81b6b3SRodney W. Grimes 	s = splbio();
11805b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
11815b81b6b3SRodney W. Grimes 	{
11825b81b6b3SRodney W. Grimes 		fdintr(fdcu);
11835b81b6b3SRodney W. Grimes 	}
11845b81b6b3SRodney W. Grimes 	splx(s);
11855b81b6b3SRodney W. Grimes }
11865b81b6b3SRodney W. Grimes 
1187381fe1aaSGarrett Wollman static void
1188d0917939SPaul Richards fd_timeout(void *arg1)
11895b81b6b3SRodney W. Grimes {
1190381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
11915b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
11923a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
11935b81b6b3SRodney W. Grimes 	struct buf *dp, *bp;
1194f5f7ba03SJordan K. Hubbard 	int s;
11955b81b6b3SRodney W. Grimes 
11965b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
11975b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
11985b81b6b3SRodney W. Grimes 
11993a2f7427SDavid Greenman 	/*
12003a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
12013a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
12023a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
12033a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
12043a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
12053a2f7427SDavid Greenman 	 */
12065b81b6b3SRodney W. Grimes 
12073a2f7427SDavid Greenman 	s = splbio();
12083a2f7427SDavid Greenman 
12093a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
12103a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
12113a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
12123a2f7427SDavid Greenman 	{
12133a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
12143a2f7427SDavid Greenman 		/* yup, it is; kill it now */
12153a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
12163a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
12173a2f7427SDavid Greenman 	}
12185b81b6b3SRodney W. Grimes 
12195b81b6b3SRodney W. Grimes 	if (bp)
12205b81b6b3SRodney W. Grimes 	{
12215b81b6b3SRodney W. Grimes 		retrier(fdcu);
12223a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
12235b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
12245b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
12255b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
12265b81b6b3SRodney W. Grimes 	}
12275b81b6b3SRodney W. Grimes 	else
12285b81b6b3SRodney W. Grimes 	{
12295b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
12305b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
12315b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
12325b81b6b3SRodney W. Grimes 	}
1233f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1234f5f7ba03SJordan K. Hubbard 	splx(s);
12355b81b6b3SRodney W. Grimes }
12365b81b6b3SRodney W. Grimes 
12375b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1238381fe1aaSGarrett Wollman static void
1239d0917939SPaul Richards fd_pseudointr(void *arg1)
12405b81b6b3SRodney W. Grimes {
1241381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
12425b81b6b3SRodney W. Grimes 	int	s;
12433a2f7427SDavid Greenman 
12445b81b6b3SRodney W. Grimes 	s = splbio();
12455b81b6b3SRodney W. Grimes 	fdintr(fdcu);
12465b81b6b3SRodney W. Grimes 	splx(s);
12475b81b6b3SRodney W. Grimes }
12485b81b6b3SRodney W. Grimes 
12495b81b6b3SRodney W. Grimes /***********************************************************************\
12505b81b6b3SRodney W. Grimes *                                 fdintr				*
12515b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
12525b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
12535b81b6b3SRodney W. Grimes \***********************************************************************/
1254381fe1aaSGarrett Wollman void
1255381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
12565b81b6b3SRodney W. Grimes {
12575b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1258b99f0a4aSAndrew Moore #if NFT > 0
1259b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1260b99f0a4aSAndrew Moore 
1261b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1262b99f0a4aSAndrew Moore 		(ftintr(fdu));
1263b99f0a4aSAndrew Moore 	else
1264b99f0a4aSAndrew Moore #endif
1265381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1266381fe1aaSGarrett Wollman 			;
12675b81b6b3SRodney W. Grimes }
12685b81b6b3SRodney W. Grimes 
12695b81b6b3SRodney W. Grimes /***********************************************************************\
12705b81b6b3SRodney W. Grimes * The controller state machine.						*
12715b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
12725b81b6b3SRodney W. Grimes \***********************************************************************/
12733a2f7427SDavid Greenman static int
1274dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
12755b81b6b3SRodney W. Grimes {
12764ccc87c5SPoul-Henning Kamp 	int read, format, head, sec = 0, sectrac, st0, cyl, st3;
12775b81b6b3SRodney W. Grimes 	unsigned long blknum;
12785b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
12795b81b6b3SRodney W. Grimes 	fd_p fd;
12805b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
1281b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
12823a2f7427SDavid Greenman 	size_t fdblk;
12835b81b6b3SRodney W. Grimes 
12845b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
12855b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
12865b81b6b3SRodney W. Grimes 	if(!bp)
12875b81b6b3SRodney W. Grimes 	{
12885b81b6b3SRodney W. Grimes 		/***********************************************\
12895b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
12905b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
12915b81b6b3SRodney W. Grimes 		\***********************************************/
12925b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
12935b81b6b3SRodney W. Grimes 		if(fdc->fd)
12945b81b6b3SRodney W. Grimes 		{
12956a0e6f42SRodney W. Grimes 			printf("fd%d: unexpected valid fd pointer\n",
12963a2f7427SDavid Greenman 			       fdc->fdu);
12975b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
12985b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
12995b81b6b3SRodney W. Grimes 		}
13005b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
13015b81b6b3SRodney W. Grimes  		return(0);
13025b81b6b3SRodney W. Grimes 	}
13035b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
13045b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
13053a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
13065b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
13075b81b6b3SRodney W. Grimes 	{
13086a0e6f42SRodney W. Grimes 		printf("fd%d: confused fd pointers\n", fdu);
13095b81b6b3SRodney W. Grimes 	}
13105b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1311b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1312b39c878eSAndrey A. Chernov 	if(format)
1313b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
13145b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
13155b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
13165b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
13175e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu);
13185e235068SJordan K. Hubbard 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
13195b81b6b3SRodney W. Grimes 	switch (fdc->state)
13205b81b6b3SRodney W. Grimes 	{
13215b81b6b3SRodney W. Grimes 	case DEVIDLE:
13225b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
13235b81b6b3SRodney W. Grimes 		fdc->retry = 0;
13245b81b6b3SRodney W. Grimes 		fd->skip = 0;
13255b81b6b3SRodney W. Grimes 		fdc->fd = fd;
13265b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
13273a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
13283a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
13295b81b6b3SRodney W. Grimes 		/*******************************************************\
13305b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
13315b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
13325b81b6b3SRodney W. Grimes 		\*******************************************************/
13335b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
13345b81b6b3SRodney W. Grimes 		{
13355b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13365b81b6b3SRodney W. Grimes 			return(0); /* come back later */
13375b81b6b3SRodney W. Grimes 		}
13385b81b6b3SRodney W. Grimes 		/*******************************************************\
13395b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
13405b81b6b3SRodney W. Grimes 		\*******************************************************/
13415b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
13425b81b6b3SRodney W. Grimes 		{
13435b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13445b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
13455b81b6b3SRodney W. Grimes 			return(0);
13465b81b6b3SRodney W. Grimes 		}
13475b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
13485b81b6b3SRodney W. Grimes 		{
13493a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
13505b81b6b3SRodney W. Grimes 		}
13515e235068SJordan K. Hubbard 		fdc->state = DOSEEK;
13525b81b6b3SRodney W. Grimes 		break;
13535b81b6b3SRodney W. Grimes 	case DOSEEK:
13545b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
13555b81b6b3SRodney W. Grimes 		{
13565b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
13575b81b6b3SRodney W. Grimes 			break;
13585b81b6b3SRodney W. Grimes 		}
1359dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1360dc5df763SJoerg Wunsch 			   fd->fdsu, bp->b_cylin * fd->ft->steptrac,
1361dc5df763SJoerg Wunsch 			   0))
1362dc8603e3SJoerg Wunsch 		{
1363dc8603e3SJoerg Wunsch 			/*
1364dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1365dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1366dc8603e3SJoerg Wunsch 			 */
1367dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1368dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1369dc8603e3SJoerg Wunsch 		}
1370dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
13715b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
13725b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13735b81b6b3SRodney W. Grimes 	case SEEKWAIT:
13745b81b6b3SRodney W. Grimes 		/* allow heads to settle */
137504b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
13765b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
13775b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13785b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
13795b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1380dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
13815b81b6b3SRodney W. Grimes 		{
13825b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
13833a2f7427SDavid Greenman 			do {
13843a2f7427SDavid Greenman 				/*
1385dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1386dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1387dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1388dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1389dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1390dc5df763SJoerg Wunsch 				 *
1391dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1392dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1393dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1394dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1395dc5df763SJoerg Wunsch 				 * real interrupt condition.
1396dc5df763SJoerg Wunsch 				 *
1397dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1398dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1399dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1400dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1401dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1402dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1403dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
14043a2f7427SDavid Greenman 				 */
1405dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1406dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1407dc5df763SJoerg Wunsch 					return 0;
1408dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1409dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1410dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
14113a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1412dc5df763SJoerg Wunsch 
14133a2f7427SDavid Greenman 			if (0 == descyl)
14143a2f7427SDavid Greenman 			{
1415dc5df763SJoerg Wunsch 				int failed = 0;
14163a2f7427SDavid Greenman 				/*
14173a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
14183a2f7427SDavid Greenman 				 * really there
14193a2f7427SDavid Greenman 				 */
1420dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1421dc5df763SJoerg Wunsch 					failed = 1;
14223a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
14233a2f7427SDavid Greenman 					printf(
14243a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
14253a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1426dc5df763SJoerg Wunsch 					failed = 1;
1427dc5df763SJoerg Wunsch 				}
1428dc5df763SJoerg Wunsch 
1429dc5df763SJoerg Wunsch 				if (failed)
1430dc5df763SJoerg Wunsch 				{
14313a2f7427SDavid Greenman 					if(fdc->retry < 3)
14323a2f7427SDavid Greenman 						fdc->retry = 3;
14333a2f7427SDavid Greenman 					return(retrier(fdcu));
14343a2f7427SDavid Greenman 				}
14353a2f7427SDavid Greenman 			}
1436dc5df763SJoerg Wunsch 
14375b81b6b3SRodney W. Grimes 			if (cyl != descyl)
14385b81b6b3SRodney W. Grimes 			{
14393a2f7427SDavid Greenman 				printf(
14403a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
14412d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
14425b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
14435b81b6b3SRodney W. Grimes 			}
14445b81b6b3SRodney W. Grimes 		}
14455b81b6b3SRodney W. Grimes 
14465b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
1447b39c878eSAndrey A. Chernov 		if(format)
1448b39c878eSAndrey A. Chernov 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1449b39c878eSAndrey A. Chernov 				- (char *)finfo;
14505b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
14513a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
14523a2f7427SDavid Greenman 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
14533a2f7427SDavid Greenman 			+ fd->skip/fdblk;
14545b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
14555b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
14565b81b6b3SRodney W. Grimes 		head = sec / sectrac;
14575b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
14583a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
14593a2f7427SDavid Greenman 
14603a2f7427SDavid Greenman 		if(format || !read)
14613a2f7427SDavid Greenman 		{
14623a2f7427SDavid Greenman 			/* make sure the drive is writable */
1463dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1464dc8603e3SJoerg Wunsch 			{
1465dc8603e3SJoerg Wunsch 				/* stuck controller? */
1466dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1467dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1468dc8603e3SJoerg Wunsch 			}
14693a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
14703a2f7427SDavid Greenman 			{
14713a2f7427SDavid Greenman 				/*
14723a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
14733a2f7427SDavid Greenman 				 * in order to force the current operation
14743a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
14753a2f7427SDavid Greenman 				 * error - all error handling is done
14763a2f7427SDavid Greenman 				 * by the retrier()
14773a2f7427SDavid Greenman 				 */
14783a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
14793a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
14803a2f7427SDavid Greenman 				fdc->status[2] = 0;
14813a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
14823a2f7427SDavid Greenman 				fdc->status[4] = head;
14833a2f7427SDavid Greenman 				fdc->status[5] = sec;
14843a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
14853a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
14863a2f7427SDavid Greenman 				return (1);
14873a2f7427SDavid Greenman 			}
14883a2f7427SDavid Greenman 		}
14895b81b6b3SRodney W. Grimes 
1490b39c878eSAndrey A. Chernov 		if(format)
1491b39c878eSAndrey A. Chernov 		{
1492b39c878eSAndrey A. Chernov 			/* formatting */
1493dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1494dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1495dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1496dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1497dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1498dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1499dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1500dc5df763SJoerg Wunsch 				  0))
1501dc8603e3SJoerg Wunsch 			{
1502dc8603e3SJoerg Wunsch 				/* controller fell over */
1503dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1504dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1505dc8603e3SJoerg Wunsch 			}
1506b39c878eSAndrey A. Chernov 		}
1507b39c878eSAndrey A. Chernov 		else
1508b39c878eSAndrey A. Chernov 		{
1509dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1510dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1511dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1512dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1513dc5df763SJoerg Wunsch 				   head,
1514dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1515dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1516dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1517dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1518dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1519dc5df763SJoerg Wunsch 				   0))
15205b81b6b3SRodney W. Grimes 			{
1521dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1522dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1523dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
15245b81b6b3SRodney W. Grimes 			}
1525b39c878eSAndrey A. Chernov 		}
15265b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
15275e235068SJordan K. Hubbard 		timeout(fd_timeout, (caddr_t)fdcu, hz);
15285b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15295b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
15305e235068SJordan K. Hubbard 		untimeout(fd_timeout, (caddr_t)fdcu);
1531dc5df763SJoerg Wunsch 
1532dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
15335b81b6b3SRodney W. Grimes 		{
1534dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1535dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1536dc5df763SJoerg Wunsch 			return retrier(fdcu);
15375b81b6b3SRodney W. Grimes   		}
1538dc5df763SJoerg Wunsch 
15393a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1540dc5df763SJoerg Wunsch 
15413a2f7427SDavid Greenman 		/* FALLTHROUGH */
1542dc5df763SJoerg Wunsch 
15433a2f7427SDavid Greenman 	case IOTIMEDOUT:
15445b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
15453a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
15463a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
15475b81b6b3SRodney W. Grimes 		{
15483a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15493a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1550b39c878eSAndrey A. Chernov                                 /*
15513a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
15523a2f7427SDavid Greenman 				 * and didn't release it in time for the
15533a2f7427SDavid Greenman 				 * next FDC transfer.
15543a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
15553a2f7427SDavid Greenman 				 * count. (vak)
1556b39c878eSAndrey A. Chernov                                  */
1557b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1558b39c878eSAndrey A. Chernov                                 return (1);
1559b39c878eSAndrey A. Chernov                         }
15603a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
15613a2f7427SDavid Greenman 				&& fdc->retry < 6)
15623a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
15633a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15643a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
15653a2f7427SDavid Greenman 				&& fdc->retry < 3)
15663a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
15675b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
15685b81b6b3SRodney W. Grimes 		}
15695b81b6b3SRodney W. Grimes 		/* All OK */
15703a2f7427SDavid Greenman 		fd->skip += fdblk;
1571b39c878eSAndrey A. Chernov 		if (!format && fd->skip < bp->b_bcount)
15725b81b6b3SRodney W. Grimes 		{
15735b81b6b3SRodney W. Grimes 			/* set up next transfer */
15743a2f7427SDavid Greenman 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
15753a2f7427SDavid Greenman 				+ fd->skip/fdblk;
15763a2f7427SDavid Greenman 			bp->b_cylin =
15773a2f7427SDavid Greenman 				(blknum / (fd->ft->sectrac * fd->ft->heads));
15785b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
15795b81b6b3SRodney W. Grimes 		}
15805b81b6b3SRodney W. Grimes 		else
15815b81b6b3SRodney W. Grimes 		{
15825b81b6b3SRodney W. Grimes 			/* ALL DONE */
15835b81b6b3SRodney W. Grimes 			fd->skip = 0;
15845b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
158526f9a767SRodney W. Grimes 			dp->b_actf = bp->b_actf;
15865b81b6b3SRodney W. Grimes 			biodone(bp);
15875b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
15885b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
15895b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
15905b81b6b3SRodney W. Grimes 		}
15915b81b6b3SRodney W. Grimes 		return(1);
15925b81b6b3SRodney W. Grimes 	case RESETCTLR:
15933a2f7427SDavid Greenman 		fdc_reset(fdc);
15945b81b6b3SRodney W. Grimes 		fdc->retry++;
15955b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
15965b81b6b3SRodney W. Grimes 		break;
15975b81b6b3SRodney W. Grimes 	case STARTRECAL:
15980e317d05SJoerg Wunsch 		/* XXX clear the fdc results from the last reset, if any. */
15990e317d05SJoerg Wunsch 		{
16000e317d05SJoerg Wunsch 			int i;
16010e317d05SJoerg Wunsch 			for (i = 0; i < 4; i++)
16020e317d05SJoerg Wunsch 				(void)fd_sense_int(fdc, &st0, &cyl);
16030e317d05SJoerg Wunsch 		}
16040e317d05SJoerg Wunsch 
1605dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1606dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1607dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1608dc8603e3SJoerg Wunsch 		{
1609dc8603e3SJoerg Wunsch 			/* arrgl */
1610dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1611dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1612dc8603e3SJoerg Wunsch 		}
16135b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
16145b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
16155b81b6b3SRodney W. Grimes 	case RECALWAIT:
16165b81b6b3SRodney W. Grimes 		/* allow heads to settle */
161704b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
16185b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
16195b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
16205b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
16213a2f7427SDavid Greenman 		do {
16223a2f7427SDavid Greenman 			/*
1623dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
16243a2f7427SDavid Greenman 			 */
1625dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1626dc5df763SJoerg Wunsch 				return 0;
1627dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1628dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1629dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
16303a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
16313a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
16325b81b6b3SRodney W. Grimes 		{
1633dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1634dc8603e3SJoerg Wunsch 				/*
1635dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1636dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1637dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1638dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1639dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1640dc8603e3SJoerg Wunsch 				 * time in a line
1641dc8603e3SJoerg Wunsch 				 */
1642dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1643dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
16443a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
16455b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
16465b81b6b3SRodney W. Grimes 		}
16475b81b6b3SRodney W. Grimes 		fd->track = 0;
16485b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
16495b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
16505b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16515b81b6b3SRodney W. Grimes 	case MOTORWAIT:
16525b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
16535b81b6b3SRodney W. Grimes 		{
16545b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
16555b81b6b3SRodney W. Grimes 		}
16565e235068SJordan K. Hubbard 		/*
16575e235068SJordan K. Hubbard 		 * since the controller was off, it has lost its
16585e235068SJordan K. Hubbard 		 * idea about the current track it were; thus,
16595e235068SJordan K. Hubbard 		 * recalibrate the bastard
16605e235068SJordan K. Hubbard 		 */
16615e235068SJordan K. Hubbard 		fdc->state = STARTRECAL;
16625b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16635b81b6b3SRodney W. Grimes 	default:
1664dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1665dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1666dac0f2dbSJoerg Wunsch 			printf("FDC status :%lx %lx %lx %lx %lx %lx %lx   ",
16675b81b6b3SRodney W. Grimes 			       fdc->status[0],
16685b81b6b3SRodney W. Grimes 			       fdc->status[1],
16695b81b6b3SRodney W. Grimes 			       fdc->status[2],
16705b81b6b3SRodney W. Grimes 			       fdc->status[3],
16715b81b6b3SRodney W. Grimes 			       fdc->status[4],
16725b81b6b3SRodney W. Grimes 			       fdc->status[5],
16735b81b6b3SRodney W. Grimes 			       fdc->status[6] );
16743a2f7427SDavid Greenman 		else
1675dac0f2dbSJoerg Wunsch 			printf("No status available   ");
1676dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1677dac0f2dbSJoerg Wunsch 		{
1678dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
16795b81b6b3SRodney W. Grimes 			return(0);
16805b81b6b3SRodney W. Grimes 		}
1681dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1682dac0f2dbSJoerg Wunsch 		return(0);
1683dac0f2dbSJoerg Wunsch 	}
1684dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
16855b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
16865b81b6b3SRodney W. Grimes }
16875b81b6b3SRodney W. Grimes 
1688aaf08d94SGarrett Wollman static int
1689f5f7ba03SJordan K. Hubbard retrier(fdcu)
1690f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
16915b81b6b3SRodney W. Grimes {
16925b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
16935b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
16945b81b6b3SRodney W. Grimes 
16955b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
16965b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
16975b81b6b3SRodney W. Grimes 
16983a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
16993a2f7427SDavid Greenman 		goto fail;
17005b81b6b3SRodney W. Grimes 	switch(fdc->retry)
17015b81b6b3SRodney W. Grimes 	{
17025b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
17035b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
17045b81b6b3SRodney W. Grimes 		break;
17055b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
17065b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
17075b81b6b3SRodney W. Grimes 		break;
17085b81b6b3SRodney W. Grimes 	case 6:
17095b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
17105b81b6b3SRodney W. Grimes 		break;
17115b81b6b3SRodney W. Grimes 	case 7:
17125b81b6b3SRodney W. Grimes 		break;
17135b81b6b3SRodney W. Grimes 	default:
17143a2f7427SDavid Greenman 	fail:
17155b81b6b3SRodney W. Grimes 		{
17167ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
17177ca0641bSAndrey A. Chernov 			/* Trick diskerr */
17183a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
17193a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
172092ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
17213a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
17223a2f7427SDavid Greenman 				(struct disklabel *)NULL);
17237ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
1724dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1725dc5df763SJoerg Wunsch 			{
1726dc5df763SJoerg Wunsch 				printf(
1727dc5df763SJoerg Wunsch 			" (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n",
1728dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1729dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1730dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1731dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1732dc5df763SJoerg Wunsch 				       fdc->status[5]);
1733dc5df763SJoerg Wunsch 			}
1734dc5df763SJoerg Wunsch 			else
1735dc5df763SJoerg Wunsch 				printf(" (No status)\n");
17365b81b6b3SRodney W. Grimes 		}
17375b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
17385b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
17395b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
174026f9a767SRodney W. Grimes 		dp->b_actf = bp->b_actf;
17415b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
17425b81b6b3SRodney W. Grimes 		biodone(bp);
174392ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
17445b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
17455b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1746f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
174792ed385aSRodney W. Grimes 		return(1);
17485b81b6b3SRodney W. Grimes 	}
17495b81b6b3SRodney W. Grimes 	fdc->retry++;
17505b81b6b3SRodney W. Grimes 	return(1);
17515b81b6b3SRodney W. Grimes }
17525b81b6b3SRodney W. Grimes 
1753b39c878eSAndrey A. Chernov static int
1754b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1755b39c878eSAndrey A. Chernov 	dev_t dev;
1756b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1757b39c878eSAndrey A. Chernov 	struct proc *p;
1758b39c878eSAndrey A. Chernov {
1759b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1760b39c878eSAndrey A. Chernov  	fd_p	fd;
1761b39c878eSAndrey A. Chernov 
1762b39c878eSAndrey A. Chernov 	struct buf *bp;
1763b39c878eSAndrey A. Chernov 	int rv = 0, s;
17643a2f7427SDavid Greenman 	size_t fdblk;
1765b39c878eSAndrey A. Chernov 
1766b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1767b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
17683a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1769b39c878eSAndrey A. Chernov 
1770b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1771b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1772b39c878eSAndrey A. Chernov 	if(bp == 0)
1773b39c878eSAndrey A. Chernov 		return ENOBUFS;
177482f5379bSJoerg Wunsch 	/*
177582f5379bSJoerg Wunsch 	 * keep the process from being swapped
177682f5379bSJoerg Wunsch 	 */
177782f5379bSJoerg Wunsch 	p->p_flag |= P_PHYSIO;
1778b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1779b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1780b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1781b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1782b39c878eSAndrey A. Chernov 
1783b39c878eSAndrey A. Chernov 	/*
1784b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1785b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1786b39c878eSAndrey A. Chernov 	 */
1787b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
17883a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1789b39c878eSAndrey A. Chernov 
1790b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
17915e235068SJordan K. Hubbard 	bp->b_un.b_addr = (caddr_t)finfo;
1792b39c878eSAndrey A. Chernov 
1793b39c878eSAndrey A. Chernov 	/* now do the format */
1794b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1795b39c878eSAndrey A. Chernov 
1796b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1797b39c878eSAndrey A. Chernov 	s = splbio();
1798b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1799b39c878eSAndrey A. Chernov 	{
18005e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1801b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1802b39c878eSAndrey A. Chernov 			break;
1803b39c878eSAndrey A. Chernov 	}
1804b39c878eSAndrey A. Chernov 	splx(s);
1805b39c878eSAndrey A. Chernov 
180682f5379bSJoerg Wunsch 	if(rv == EWOULDBLOCK) {
1807b39c878eSAndrey A. Chernov 		/* timed out */
1808b39c878eSAndrey A. Chernov 		rv = EIO;
180982f5379bSJoerg Wunsch 		biodone(bp);
181082f5379bSJoerg Wunsch 	}
18113a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
18123a2f7427SDavid Greenman 		rv = bp->b_error;
181382f5379bSJoerg Wunsch 	/*
181482f5379bSJoerg Wunsch 	 * allow the process to be swapped
181582f5379bSJoerg Wunsch 	 */
181682f5379bSJoerg Wunsch 	p->p_flag &= ~P_PHYSIO;
1817b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1818b39c878eSAndrey A. Chernov 	return rv;
1819b39c878eSAndrey A. Chernov }
1820b39c878eSAndrey A. Chernov 
1821f5f7ba03SJordan K. Hubbard /*
1822671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
1823f5f7ba03SJordan K. Hubbard  */
18245b81b6b3SRodney W. Grimes 
1825f5f7ba03SJordan K. Hubbard int
1826b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1827f5f7ba03SJordan K. Hubbard 	dev_t dev;
1828f5f7ba03SJordan K. Hubbard 	int cmd;
1829f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1830f5f7ba03SJordan K. Hubbard 	int flag;
1831b39c878eSAndrey A. Chernov 	struct proc *p;
1832f5f7ba03SJordan K. Hubbard {
18333a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
18343a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
18353a2f7427SDavid Greenman 	size_t fdblk;
18363a2f7427SDavid Greenman 
1837f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1838f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1839f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
18403a2f7427SDavid Greenman 	int error = 0;
1841f5f7ba03SJordan K. Hubbard 
1842b99f0a4aSAndrew Moore #if NFT > 0
1843a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1844a60eff27SNate Williams 
1845a60eff27SNate Williams 	/* check for a tape ioctl */
1846a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1847b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1848b99f0a4aSAndrew Moore #endif
1849b99f0a4aSAndrew Moore 
18503a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1851f5f7ba03SJordan K. Hubbard 
1852f5f7ba03SJordan K. Hubbard 	switch (cmd)
1853f5f7ba03SJordan K. Hubbard 	{
1854f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1855f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1856f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
18573a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
185892ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1859f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1860f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1861f5f7ba03SJordan K. Hubbard 
1862191e1a59SBruce Evans 		if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl)
1863191e1a59SBruce Evans 		    == NULL)
1864f5f7ba03SJordan K. Hubbard 			error = 0;
1865f5f7ba03SJordan K. Hubbard 		else
1866f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1867f5f7ba03SJordan K. Hubbard 
1868f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1869f5f7ba03SJordan K. Hubbard 		break;
1870f5f7ba03SJordan K. Hubbard 
1871f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1872f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1873f5f7ba03SJordan K. Hubbard 			error = EBADF;
1874f5f7ba03SJordan K. Hubbard 		break;
1875f5f7ba03SJordan K. Hubbard 
1876f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1877f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1878f5f7ba03SJordan K. Hubbard 			error = EBADF;
1879f5f7ba03SJordan K. Hubbard 		break;
1880f5f7ba03SJordan K. Hubbard 
1881f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1882f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1883f5f7ba03SJordan K. Hubbard 		{
1884f5f7ba03SJordan K. Hubbard 			error = EBADF;
1885f5f7ba03SJordan K. Hubbard 			break;
1886f5f7ba03SJordan K. Hubbard 		}
1887f5f7ba03SJordan K. Hubbard 
1888f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1889f5f7ba03SJordan K. Hubbard 
1890191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
1891191e1a59SBruce Evans 					  (u_long)0)) != 0)
1892f5f7ba03SJordan K. Hubbard 			break;
1893f5f7ba03SJordan K. Hubbard 
1894b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
189554c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
1896b39c878eSAndrey A. Chernov 		break;
1897b39c878eSAndrey A. Chernov 
1898b39c878eSAndrey A. Chernov 	case FD_FORM:
1899b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1900b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1901b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1902b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1903b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1904b39c878eSAndrey A. Chernov 		else
1905b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1906b39c878eSAndrey A. Chernov 		break;
1907b39c878eSAndrey A. Chernov 
1908b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1909b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1910f5f7ba03SJordan K. Hubbard 		break;
1911f5f7ba03SJordan K. Hubbard 
19123a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
19133a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
19143a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
19153a2f7427SDavid Greenman 			return EPERM;
19163a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
19173a2f7427SDavid Greenman 		break;
19183a2f7427SDavid Greenman 
19193a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
19203a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
19213a2f7427SDavid Greenman 		break;
19223a2f7427SDavid Greenman 
19233a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
19243a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
19253a2f7427SDavid Greenman 		break;
19263a2f7427SDavid Greenman 
1927f5f7ba03SJordan K. Hubbard 	default:
19283a2f7427SDavid Greenman 		error = ENOTTY;
1929f5f7ba03SJordan K. Hubbard 		break;
1930f5f7ba03SJordan K. Hubbard 	}
1931f5f7ba03SJordan K. Hubbard 	return (error);
1932f5f7ba03SJordan K. Hubbard }
1933f5f7ba03SJordan K. Hubbard 
19347146c13eSJulian Elischer 
19357146c13eSJulian Elischer static fd_devsw_installed = 0;
19367146c13eSJulian Elischer 
193787f6c662SJulian Elischer static void 	fd_drvinit(void *notused )
19387146c13eSJulian Elischer {
193987f6c662SJulian Elischer 	dev_t dev;
194087f6c662SJulian Elischer 
19417146c13eSJulian Elischer 	if( ! fd_devsw_installed ) {
194287f6c662SJulian Elischer 		dev = makedev(CDEV_MAJOR, 0);
194387f6c662SJulian Elischer 		cdevsw_add(&dev,&fd_cdevsw, NULL);
194487f6c662SJulian Elischer 		dev = makedev(BDEV_MAJOR, 0);
194587f6c662SJulian Elischer 		bdevsw_add(&dev,&fd_bdevsw, NULL);
19467146c13eSJulian Elischer 		fd_devsw_installed = 1;
19477146c13eSJulian Elischer 	}
19487146c13eSJulian Elischer }
194987f6c662SJulian Elischer 
195087f6c662SJulian Elischer SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL)
195187f6c662SJulian Elischer 
1952f5f7ba03SJordan K. Hubbard #endif
19533a2f7427SDavid Greenman /*
19543a2f7427SDavid Greenman  * Hello emacs, these are the
19553a2f7427SDavid Greenman  * Local Variables:
19563a2f7427SDavid Greenman  *  c-indent-level:               8
19573a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
19583a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
19593a2f7427SDavid Greenman  *  c-brace-offset:              -8
19603a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
19613a2f7427SDavid Greenman  *  c-argdecl-indent:             8
19623a2f7427SDavid Greenman  *  c-label-offset:              -8
19633a2f7427SDavid Greenman  *  c++-hanging-braces:           1
19643a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
19653a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
19663a2f7427SDavid Greenman  *  c++-friend-offset:            0
19673a2f7427SDavid Greenman  * End:
19683a2f7427SDavid Greenman  */
1969