xref: /freebsd/sys/dev/fdc/fdc.c (revision 4d92e19833832c97168f61a5f39d9b4da951bbee)
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
464d92e198SJulian Elischer  *	$Id: fd.c,v 1.74 1995/12/08 23:20:26 phk 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 
2035b81b6b3SRodney W. Grimes 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 \***********************************************************************/
2335b81b6b3SRodney W. Grimes 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
25087f6c662SJulian Elischer 	void	*rfd_devfs_token;
25187f6c662SJulian Elischer 	void	*fd_devfs_token;
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*/
35587f6c662SJulian Elischer 	  nxdump,	zerosize,	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 
36392200632SGarrett Wollman 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 
396dc5df763SJoerg Wunsch 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 
436dc5df763SJoerg Wunsch 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 
451dc5df763SJoerg Wunsch 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 
488dc5df763SJoerg Wunsch 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
571999422d7SJulian Elischer 	char	name[64];
572999422d7SJulian Elischer #endif	/* DEVFS */
57392200632SGarrett Wollman 
5745b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
5755b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
5765b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
5775b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
5783a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
5793a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
5805b81b6b3SRodney W. Grimes 
5815b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
582b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
583b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
584b99f0a4aSAndrew Moore 			continue;
585b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
586b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
587b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
588b99f0a4aSAndrew Moore 			continue;
589b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
590b99f0a4aSAndrew Moore 		/* look up what bios thinks we have */
591b99f0a4aSAndrew Moore 		switch (fdu) {
592b99f0a4aSAndrew Moore 			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
593b99f0a4aSAndrew Moore 				break;
594b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
595b99f0a4aSAndrew Moore 				break;
596b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
597b99f0a4aSAndrew Moore 				break;
598b99f0a4aSAndrew Moore 		}
5995b81b6b3SRodney W. Grimes 		/* is there a unit? */
600b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
601b99f0a4aSAndrew Moore #if NFT > 0
602b99f0a4aSAndrew Moore 		    || (fdsu >= DRVS_PER_CTLR)) {
603b99f0a4aSAndrew Moore #else
604b99f0a4aSAndrew Moore 		) {
60556ef0285SAndrew Moore 			fd->type = NO_TYPE;
606b99f0a4aSAndrew Moore #endif
607b99f0a4aSAndrew Moore #if NFT > 0
608b99f0a4aSAndrew Moore 			/* If BIOS says no floppy, or > 2nd device */
609b99f0a4aSAndrew Moore 			/* Probe for and attach a floppy tape.     */
61074fa89f4SRodney W. Grimes 			/* Tell FT if there was already a disk     */
61174fa89f4SRodney W. Grimes 			/* with this unit number found.            */
61274fa89f4SRodney W. Grimes 
61374fa89f4SRodney W. Grimes 			unithasfd = 0;
61474fa89f4SRodney W. Grimes 			if (fdu < NFD && fd->type != NO_TYPE)
61574fa89f4SRodney W. Grimes 				unithasfd = 1;
61685827d9cSJoerg Wunsch 			if (ftattach(dev, fdup, unithasfd))
617b99f0a4aSAndrew Moore 				continue;
61856ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
619b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
62056ef0285SAndrew Moore #endif
6215b81b6b3SRodney W. Grimes 			continue;
622f5f7ba03SJordan K. Hubbard 		}
6235b81b6b3SRodney W. Grimes 
6245b81b6b3SRodney W. Grimes 		/* select it */
6253a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
6266b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
627dc5df763SJoerg Wunsch 
628dc5df763SJoerg Wunsch 		if (ic_type == 0 &&
629dc5df763SJoerg Wunsch 		    fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
630dc5df763SJoerg Wunsch 		{
6316a0e6f42SRodney W. Grimes 			printf("fdc%d: ", fdcu);
632dc5df763SJoerg Wunsch 			ic_type = (u_char)ic_type;
633dc5df763SJoerg Wunsch 			switch( ic_type ) {
634dc5df763SJoerg Wunsch 			case 0x80:
6356a0e6f42SRodney W. Grimes 				printf("NEC 765\n");
636dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE765;
6376c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6386c0081e9SGarrett Wollman 					"NEC 765 floppy disk/tape controller";
639dc5df763SJoerg Wunsch 				break;
640dc5df763SJoerg Wunsch 			case 0x81:
6416a0e6f42SRodney W. Grimes 				printf("Intel 82077\n");
642dc5df763SJoerg Wunsch 				fdc->fdct = FDC_I82077;
6436c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6446c0081e9SGarrett Wollman 					"Intel 82077 floppy disk/tape controller";
645dc5df763SJoerg Wunsch 				break;
646dc5df763SJoerg Wunsch 			case 0x90:
6476a0e6f42SRodney W. Grimes 				printf("NEC 72065B\n");
648dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE72065;
6496c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6506c0081e9SGarrett Wollman 					"NEC 72065B floppy disk/tape controller";
651dc5df763SJoerg Wunsch 				break;
652dc5df763SJoerg Wunsch 			default:
6536a0e6f42SRodney W. Grimes 				printf("unknown IC type %02x\n", ic_type);
654dc5df763SJoerg Wunsch 				fdc->fdct = FDC_UNKNOWN;
655dc5df763SJoerg Wunsch 				break;
6566b7bd95bSJoerg Wunsch 			}
657dc5df763SJoerg Wunsch 		}
658dc5df763SJoerg Wunsch 		if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
659dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
660dc5df763SJoerg Wunsch 			/* if at track 0, first seek inwards */
661dc5df763SJoerg Wunsch 			/* seek some steps: */
662dc5df763SJoerg Wunsch 			(void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
663dc5df763SJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
664dc5df763SJoerg Wunsch 			(void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
665dc5df763SJoerg Wunsch 		}
666dc5df763SJoerg Wunsch 
667dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
668dc5df763SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
669dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
670dc5df763SJoerg Wunsch 			/* Seek some steps... */
671dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
672dc5df763SJoerg Wunsch 				/* ...wait a moment... */
673dc5df763SJoerg Wunsch 				DELAY(300000);
674dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
675dc5df763SJoerg Wunsch 				(void)fd_sense_int(fdc, 0, 0);
676dc5df763SJoerg Wunsch 			}
677dc5df763SJoerg Wunsch 		}
678dc5df763SJoerg Wunsch 
6796b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
6806b7bd95bSJoerg Wunsch 			/*
6816b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
6826b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
6836b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
6846b7bd95bSJoerg Wunsch 			 * more than 77 steps
6856b7bd95bSJoerg Wunsch 			 */
686dc5df763SJoerg Wunsch 			/* go back to 0: */
687dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
6886b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
6896b7bd95bSJoerg Wunsch 				DELAY(i == 0? 1000000: 300000);
6905b81b6b3SRodney W. Grimes 
6916b7bd95bSJoerg Wunsch 				/* anything responding? */
692dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
693dc5df763SJoerg Wunsch 				(st0 & NE7_ST0_EC) == 0)
6946b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
6956b7bd95bSJoerg Wunsch 			}
696dc5df763SJoerg Wunsch 		}
6976b7bd95bSJoerg Wunsch 
6983a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
6993a2f7427SDavid Greenman 
7003a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
7015b81b6b3SRodney W. Grimes 			continue;
7025b81b6b3SRodney W. Grimes 
703dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
704b99f0a4aSAndrew Moore 		fd->fdc = fdc;
705b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
7063a2f7427SDavid Greenman 		fd->options = 0;
7072d9d0204SRodney W. Grimes 		printf("fd%d: ", fdu);
7085b81b6b3SRodney W. Grimes 
7096c0081e9SGarrett Wollman 		fd_registerdev(fdcu, fdu);
710b99f0a4aSAndrew Moore 		switch (fdt) {
7117ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
7126a0e6f42SRodney W. Grimes 			printf("1.2MB 5.25in\n");
713b99f0a4aSAndrew Moore 			fd->type = FD_1200;
7146c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7156c0081e9SGarrett Wollman 				"1.2MB (1200K) 5.25in floppy disk drive";
716999422d7SJulian Elischer #ifdef	DEVFS
7174d92e198SJulian Elischer 			sprintf(name,"rfd%d.1200",fdu);
718999422d7SJulian Elischer #endif	/* DEVFS */
7197ca0641bSAndrey A. Chernov 			break;
7207ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
7216a0e6f42SRodney W. Grimes 			printf("1.44MB 3.5in\n");
722b99f0a4aSAndrew Moore 			fd->type = FD_1440;
7236c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7246c0081e9SGarrett Wollman 				"1.44MB (1440K) 3.5in floppy disk drive";
725999422d7SJulian Elischer #ifdef	DEVFS
7264d92e198SJulian Elischer 			sprintf(name,"rfd%d.1440",fdu);
727999422d7SJulian Elischer #endif	/* DEVFS */
7287ca0641bSAndrey A. Chernov 			break;
729290dd077SJoerg Wunsch 		case RTCFDT_288M:
73086a727d9SJoerg Wunsch 		case RTCFDT_288M_1:
7316a0e6f42SRodney W. Grimes 			printf("2.88MB 3.5in - 1.44MB mode\n");
732290dd077SJoerg Wunsch 			fd->type = FD_1440;
7336c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7346c0081e9SGarrett Wollman 				"2.88MB (2880K) 3.5in floppy disk drive in 1.44 mode";
735999422d7SJulian Elischer #ifdef	DEVFS
7364d92e198SJulian Elischer 			sprintf(name,"rfd%d.1440",fdu);
737999422d7SJulian Elischer #endif	/* DEVFS */
738290dd077SJoerg Wunsch 			break;
7397ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
7406a0e6f42SRodney W. Grimes 			printf("360KB 5.25in\n");
741b99f0a4aSAndrew Moore 			fd->type = FD_360;
7426c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7436c0081e9SGarrett Wollman 				"360KB 5.25in floppy disk drive";
744999422d7SJulian Elischer #ifdef	DEVFS
7454d92e198SJulian Elischer 			sprintf(name,"rfd%d.360",fdu);
746999422d7SJulian Elischer #endif	/* DEVFS */
7477ca0641bSAndrey A. Chernov 			break;
748ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
7496a0e6f42SRodney W. Grimes 			printf("720KB 3.5in\n");
750b99f0a4aSAndrew Moore 			fd->type = FD_720;
7516c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7526c0081e9SGarrett Wollman 				"720KB 3.5in floppy disk drive";
753999422d7SJulian Elischer #ifdef	DEVFS
7544d92e198SJulian Elischer 			sprintf(name,"rfd%d.720",fdu);
755999422d7SJulian Elischer #endif	/* DEVFS */
756ed2fa05eSAndrey A. Chernov 			break;
7577ca0641bSAndrey A. Chernov 		default:
7586a0e6f42SRodney W. Grimes 			printf("unknown\n");
759b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
760999422d7SJulian Elischer #ifdef	DEVFS
7614d92e198SJulian Elischer 			sprintf(name,"rfd%d.xxxx",fdu);
762999422d7SJulian Elischer #endif	/* DEVFS */
7637ca0641bSAndrey A. Chernov 			break;
7645b81b6b3SRodney W. Grimes 		}
765dc16046fSJoerg Wunsch 		kdc_fd[fdu].kdc_state = DC_IDLE;
766999422d7SJulian Elischer #ifdef DEVFS
76787f6c662SJulian Elischer 		fd->rfd_devfs_token = devfs_add_devsw(
76887f6c662SJulian Elischer 				"/",name,&fd_cdevsw, fdu * 8,
7698af5d536SJulian Elischer 				DV_CHR,0,0,0644);
77087f6c662SJulian Elischer 		fd->fd_devfs_token = devfs_add_devsw(
7714d92e198SJulian Elischer 				"/",name+1, &fd_bdevsw, fdu * 8,
7728af5d536SJulian Elischer 				DV_BLK,0,0,0644);
773999422d7SJulian Elischer #endif /* DEVFS */
77492200632SGarrett Wollman 		if (dk_ndrive < DK_NDRIVE) {
77592200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
7766a0e6f42SRodney W. Grimes 			fd->dkunit = dk_ndrive++;
777671e2ceeSBruce Evans 			/*
778671e2ceeSBruce Evans 			 * XXX assume rate is FDC_500KBPS.
779671e2ceeSBruce Evans 			 */
780671e2ceeSBruce Evans 			dk_wpms[dk_ndrive] = 500000 / 8 / 2;
78192200632SGarrett Wollman 		} else {
78292200632SGarrett Wollman 			fd->dkunit = -1;
78392200632SGarrett Wollman 		}
7845b81b6b3SRodney W. Grimes 	}
7855b81b6b3SRodney W. Grimes 
7863a2f7427SDavid Greenman 	return (1);
7875b81b6b3SRodney W. Grimes }
7885b81b6b3SRodney W. Grimes 
7895b81b6b3SRodney W. Grimes /****************************************************************************/
7905b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
7915e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
7925b81b6b3SRodney W. Grimes /****************************************************************************/
7933a2f7427SDavid Greenman static void
794dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
7955b81b6b3SRodney W. Grimes {
7963a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
7973a2f7427SDavid Greenman 	int needspecify = 0;
7983a2f7427SDavid Greenman 
7993a2f7427SDavid Greenman 	if(turnon) {
8003a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
8013a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
8023a2f7427SDavid Greenman 	} else
8033a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
8043a2f7427SDavid Greenman 
8055e235068SJordan K. Hubbard 	if(!turnon
8065e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
8075e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
8085e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
8095e235068SJordan K. Hubbard 	else {
8103a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
8113a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
8123a2f7427SDavid Greenman 			needspecify = 1;
8133a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
8145b81b6b3SRodney W. Grimes 	}
8155b81b6b3SRodney W. Grimes 
8163a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
8173a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
818dc16046fSJoerg Wunsch 	kdc_fdc[fdcu].kdc_state = (fdout & FDO_FRST)? DC_BUSY: DC_IDLE;
8193a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
8203a2f7427SDavid Greenman 
8213a2f7427SDavid Greenman 	if(needspecify) {
822dc8603e3SJoerg Wunsch 		/*
823dc5df763SJoerg Wunsch 		 * XXX
824dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
825dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
826dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
827dc8603e3SJoerg Wunsch 		 */
828dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
829dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
830dc5df763SJoerg Wunsch 			     0);
8313a2f7427SDavid Greenman 	}
8323a2f7427SDavid Greenman }
8333a2f7427SDavid Greenman 
834381fe1aaSGarrett Wollman static void
835d0917939SPaul Richards fd_turnoff(void *arg1)
8365b81b6b3SRodney W. Grimes {
837381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
838f5f7ba03SJordan K. Hubbard 	int	s;
8395b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
840dc16046fSJoerg Wunsch 
841dc16046fSJoerg Wunsch 	TRACE1("[fd%d: turnoff]", fdu);
842f5f7ba03SJordan K. Hubbard 	s = splbio();
8435b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
8443a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
845f5f7ba03SJordan K. Hubbard 	splx(s);
8465b81b6b3SRodney W. Grimes }
8475b81b6b3SRodney W. Grimes 
8483a2f7427SDavid Greenman static void
849d0917939SPaul Richards fd_motor_on(void *arg1)
8505b81b6b3SRodney W. Grimes {
851381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
852f5f7ba03SJordan K. Hubbard 	int	s;
853f5f7ba03SJordan K. Hubbard 
8545b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
855f5f7ba03SJordan K. Hubbard 	s = splbio();
8565b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
8575b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
8585b81b6b3SRodney W. Grimes 	{
859f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
8605b81b6b3SRodney W. Grimes 	}
861f5f7ba03SJordan K. Hubbard 	splx(s);
8625b81b6b3SRodney W. Grimes }
8635b81b6b3SRodney W. Grimes 
8643a2f7427SDavid Greenman static void
865dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
8665b81b6b3SRodney W. Grimes {
8675b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
8685b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
8695b81b6b3SRodney W. Grimes 	{
8703a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
8713a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
8725e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
8735b81b6b3SRodney W. Grimes 	}
8745b81b6b3SRodney W. Grimes }
8755b81b6b3SRodney W. Grimes 
876381fe1aaSGarrett Wollman static void
877dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
8785b81b6b3SRodney W. Grimes {
8793a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
8803a2f7427SDavid Greenman 
8813a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
8823a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8833a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8843a2f7427SDavid Greenman 	DELAY(100);
8853a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
8863a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
8873a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
8883a2f7427SDavid Greenman 	DELAY(100);
8893a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
8903a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
8913a2f7427SDavid Greenman 
892dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
893dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
894dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
895dc5df763SJoerg Wunsch 		     0);
8965b81b6b3SRodney W. Grimes }
8975b81b6b3SRodney W. Grimes 
8985b81b6b3SRodney W. Grimes /****************************************************************************/
8995b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
9005b81b6b3SRodney W. Grimes /****************************************************************************/
9015b81b6b3SRodney W. Grimes int
902dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
9035b81b6b3SRodney W. Grimes {
9045b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9055b81b6b3SRodney W. Grimes 	int i, j = 100000;
9063a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
9075b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
908dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9096a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
9105b81b6b3SRodney W. Grimes 	if (j <= 0)
91116b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
9125b81b6b3SRodney W. Grimes #ifdef	DEBUG
9133a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
9143a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
9155b81b6b3SRodney W. Grimes 	return(i);
9165b81b6b3SRodney W. Grimes #else
9173a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
9185b81b6b3SRodney W. Grimes #endif
9195b81b6b3SRodney W. Grimes }
9205b81b6b3SRodney W. Grimes 
921dc5df763SJoerg Wunsch /*
922dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
923dc5df763SJoerg Wunsch  */
924b5e8ce9fSBruce Evans static int
925dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
926dc5df763SJoerg Wunsch {
927dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
928dc5df763SJoerg Wunsch 	int i, j = 100000;
929dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
930dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
931dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9326a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
933dc5df763SJoerg Wunsch 	if (j <= 0)
93416b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
935dc5df763SJoerg Wunsch #ifdef	DEBUG
936dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
937dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
938dc5df763SJoerg Wunsch 	*ptr = i;
939dc5df763SJoerg Wunsch 	return 0;
940dc5df763SJoerg Wunsch #else
941dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
942dc5df763SJoerg Wunsch 	if (ptr)
943dc5df763SJoerg Wunsch 		*ptr = i;
944dc5df763SJoerg Wunsch 	return 0;
945dc5df763SJoerg Wunsch #endif
946dc5df763SJoerg Wunsch }
947dc5df763SJoerg Wunsch 
948dc5df763SJoerg Wunsch int
949dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
9505b81b6b3SRodney W. Grimes {
9515b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9523b3837dbSRodney W. Grimes 	int i;
9535b81b6b3SRodney W. Grimes 
9543b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
9553b3837dbSRodney W. Grimes 	i = 100000;
9563a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
9576a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
9583b3837dbSRodney W. Grimes 
9593b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
9603b3837dbSRodney W. Grimes 	i = 100000;
9613a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
96216b04b6aSJoerg Wunsch 	if (i <= 0)
96316b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0);
9643b3837dbSRodney W. Grimes 
9653b3837dbSRodney W. Grimes 	/* Send the command and return */
9663a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
9673a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
9685b81b6b3SRodney W. Grimes 	return (0);
9695b81b6b3SRodney W. Grimes }
9705b81b6b3SRodney W. Grimes 
9715b81b6b3SRodney W. Grimes /****************************************************************************/
9725b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
9735b81b6b3SRodney W. Grimes /****************************************************************************/
974381fe1aaSGarrett Wollman int
975671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
9765b81b6b3SRodney W. Grimes {
9775b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
97820a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
979b99f0a4aSAndrew Moore 	fdc_p	fdc;
9805b81b6b3SRodney W. Grimes 
981b99f0a4aSAndrew Moore #if NFT > 0
982b99f0a4aSAndrew Moore 	/* check for a tape open */
983b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
984b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
985b99f0a4aSAndrew Moore #endif
9865b81b6b3SRodney W. Grimes 	/* check bounds */
987b99f0a4aSAndrew Moore 	if (fdu >= NFD)
988b99f0a4aSAndrew Moore 		return(ENXIO);
989b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
990b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
991b99f0a4aSAndrew Moore 		return(ENXIO);
992b99f0a4aSAndrew Moore 	if (type > NUMDENS)
993b99f0a4aSAndrew Moore 		return(ENXIO);
9947ca0641bSAndrey A. Chernov 	if (type == 0)
9957ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
9967ca0641bSAndrey A. Chernov 	else {
9977ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
998fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
9997ca0641bSAndrey A. Chernov 			case FD_360:
10007ca0641bSAndrey A. Chernov 				return(ENXIO);
1001ed2fa05eSAndrey A. Chernov 			case FD_720:
1002b39c878eSAndrey A. Chernov 				if (   type != FD_820
1003b39c878eSAndrey A. Chernov 				    && type != FD_800
1004ed2fa05eSAndrey A. Chernov 				   )
1005ed2fa05eSAndrey A. Chernov 					return(ENXIO);
1006ed2fa05eSAndrey A. Chernov 				break;
10077ca0641bSAndrey A. Chernov 			case FD_1200:
1008b39c878eSAndrey A. Chernov 				switch (type) {
1009b39c878eSAndrey A. Chernov 				case FD_1480:
1010b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1011fa4700b4SAndrey A. Chernov 					break;
10127ca0641bSAndrey A. Chernov 				case FD_1440:
1013b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1014b39c878eSAndrey A. Chernov 					break;
1015b39c878eSAndrey A. Chernov 				case FD_820:
1016b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1017b39c878eSAndrey A. Chernov 					break;
1018b39c878eSAndrey A. Chernov 				case FD_800:
1019b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1020b39c878eSAndrey A. Chernov 					break;
1021b39c878eSAndrey A. Chernov 				case FD_720:
1022b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1023b39c878eSAndrey A. Chernov 					break;
1024b39c878eSAndrey A. Chernov 				case FD_360:
1025b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1026b39c878eSAndrey A. Chernov 					break;
1027b39c878eSAndrey A. Chernov 				default:
1028b39c878eSAndrey A. Chernov 					return(ENXIO);
1029b39c878eSAndrey A. Chernov 				}
1030b39c878eSAndrey A. Chernov 				break;
1031b39c878eSAndrey A. Chernov 			case FD_1440:
1032b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1033b39c878eSAndrey A. Chernov 				    && type != FD_1480
1034ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1035b39c878eSAndrey A. Chernov 				    && type != FD_820
1036b39c878eSAndrey A. Chernov 				    && type != FD_800
1037b39c878eSAndrey A. Chernov 				    && type != FD_720
10387ca0641bSAndrey A. Chernov 				    )
1039dffff499SAndrey A. Chernov 					return(ENXIO);
1040fa4700b4SAndrey A. Chernov 				break;
10417ca0641bSAndrey A. Chernov 			}
10427ca0641bSAndrey A. Chernov 		}
1043fa4700b4SAndrey A. Chernov 	}
1044b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
10455b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
1046dc16046fSJoerg Wunsch 	kdc_fd[fdu].kdc_state = DC_BUSY;
10475b81b6b3SRodney W. Grimes 
10485b81b6b3SRodney W. Grimes 	return 0;
10495b81b6b3SRodney W. Grimes }
10505b81b6b3SRodney W. Grimes 
1051381fe1aaSGarrett Wollman int
1052671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
10535b81b6b3SRodney W. Grimes {
10545b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
1055b99f0a4aSAndrew Moore 
1056b99f0a4aSAndrew Moore #if NFT > 0
10573a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
10583a2f7427SDavid Greenman 
1059b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
10603a2f7427SDavid Greenman 		return ftclose(dev, flags);
1061b99f0a4aSAndrew Moore #endif
10625b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
10633a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
1064dc16046fSJoerg Wunsch 	kdc_fd[fdu].kdc_state = DC_IDLE;
1065dc16046fSJoerg Wunsch 
10665b81b6b3SRodney W. Grimes 	return(0);
10675b81b6b3SRodney W. Grimes }
10685b81b6b3SRodney W. Grimes 
10695b81b6b3SRodney W. Grimes 
10703a2f7427SDavid Greenman /****************************************************************************/
10713a2f7427SDavid Greenman /*                               fdstrategy                                 */
10723a2f7427SDavid Greenman /****************************************************************************/
10733a2f7427SDavid Greenman void
10743a2f7427SDavid Greenman fdstrategy(struct buf *bp)
10753a2f7427SDavid Greenman {
10763a2f7427SDavid Greenman 	register struct buf *dp;
10773a2f7427SDavid Greenman 	long nblocks, blknum;
10783a2f7427SDavid Greenman  	int	s;
10793a2f7427SDavid Greenman  	fdcu_t	fdcu;
10803a2f7427SDavid Greenman  	fdu_t	fdu;
10813a2f7427SDavid Greenman  	fdc_p	fdc;
10823a2f7427SDavid Greenman  	fd_p	fd;
10833a2f7427SDavid Greenman 	size_t	fdblk;
10843a2f7427SDavid Greenman 
10853a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
10863a2f7427SDavid Greenman 	fd = &fd_data[fdu];
10873a2f7427SDavid Greenman 	fdc = fd->fdc;
10883a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
10893a2f7427SDavid Greenman 
10903a2f7427SDavid Greenman #if NFT > 0
10913a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
10923a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
1093d3628763SRodney W. Grimes 		bp->b_error = ENODEV;
10943a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
10953a2f7427SDavid Greenman 		goto bad;
10963a2f7427SDavid Greenman 	}
10973a2f7427SDavid Greenman 	/* check for controller already busy with tape */
10983a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
10993a2f7427SDavid Greenman 		bp->b_error = EBUSY;
11003a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11013a2f7427SDavid Greenman 		goto bad;
11023a2f7427SDavid Greenman 	}
11033a2f7427SDavid Greenman #endif
1104d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
11053a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
11063a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1107dc5df763SJoerg Wunsch 			printf(
11086a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1109702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
11103a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11113a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11123a2f7427SDavid Greenman 			goto bad;
11133a2f7427SDavid Greenman 		}
11143a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
11153a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11163a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11173a2f7427SDavid Greenman 			goto bad;
11183a2f7427SDavid Greenman 		}
11193a2f7427SDavid Greenman 	}
11203a2f7427SDavid Greenman 
11213a2f7427SDavid Greenman 	/*
11223a2f7427SDavid Greenman 	 * Set up block calculations.
11233a2f7427SDavid Greenman 	 */
11243a2f7427SDavid Greenman 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk;
11253a2f7427SDavid Greenman  	nblocks = fd->ft->size;
11263a2f7427SDavid Greenman 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
11273a2f7427SDavid Greenman 		if (blknum == nblocks) {
11283a2f7427SDavid Greenman 			bp->b_resid = bp->b_bcount;
11293a2f7427SDavid Greenman 		} else {
11303a2f7427SDavid Greenman 			bp->b_error = ENOSPC;
11313a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11323a2f7427SDavid Greenman 		}
11333a2f7427SDavid Greenman 		goto bad;
11343a2f7427SDavid Greenman 	}
11353a2f7427SDavid Greenman  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
113627513ca7SBruce Evans  	bp->b_pblkno = bp->b_blkno;
11373a2f7427SDavid Greenman 	dp = &(fdc->head);
11383a2f7427SDavid Greenman 	s = splbio();
11393a2f7427SDavid Greenman 	disksort(dp, bp);
11405e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */
11413a2f7427SDavid Greenman 	fdstart(fdcu);
11423a2f7427SDavid Greenman 	splx(s);
11433a2f7427SDavid Greenman 	return;
11443a2f7427SDavid Greenman 
11453a2f7427SDavid Greenman bad:
11463a2f7427SDavid Greenman 	biodone(bp);
11473a2f7427SDavid Greenman }
11483a2f7427SDavid Greenman 
11495b81b6b3SRodney W. Grimes /***************************************************************\
11505b81b6b3SRodney W. Grimes *				fdstart				*
11515b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
11525b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
11535b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
11545b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
11555b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
11565b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
11575b81b6b3SRodney W. Grimes \***************************************************************/
1158381fe1aaSGarrett Wollman static void
1159dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
11605b81b6b3SRodney W. Grimes {
11615b81b6b3SRodney W. Grimes 	int s;
11625b81b6b3SRodney W. Grimes 
11635b81b6b3SRodney W. Grimes 	s = splbio();
11645b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
11655b81b6b3SRodney W. Grimes 	{
11665b81b6b3SRodney W. Grimes 		fdintr(fdcu);
11675b81b6b3SRodney W. Grimes 	}
11685b81b6b3SRodney W. Grimes 	splx(s);
11695b81b6b3SRodney W. Grimes }
11705b81b6b3SRodney W. Grimes 
1171381fe1aaSGarrett Wollman static void
1172d0917939SPaul Richards fd_timeout(void *arg1)
11735b81b6b3SRodney W. Grimes {
1174381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
11755b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
11763a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
11775b81b6b3SRodney W. Grimes 	struct buf *dp, *bp;
1178f5f7ba03SJordan K. Hubbard 	int s;
11795b81b6b3SRodney W. Grimes 
11805b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
11815b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
11825b81b6b3SRodney W. Grimes 
11833a2f7427SDavid Greenman 	/*
11843a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
11853a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
11863a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
11873a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
11883a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
11893a2f7427SDavid Greenman 	 */
11905b81b6b3SRodney W. Grimes 
11913a2f7427SDavid Greenman 	s = splbio();
11923a2f7427SDavid Greenman 
11933a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
11943a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
11953a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
11963a2f7427SDavid Greenman 	{
11973a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
11983a2f7427SDavid Greenman 		/* yup, it is; kill it now */
11993a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
12003a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
12013a2f7427SDavid Greenman 	}
12025b81b6b3SRodney W. Grimes 
12035b81b6b3SRodney W. Grimes 	if (bp)
12045b81b6b3SRodney W. Grimes 	{
12055b81b6b3SRodney W. Grimes 		retrier(fdcu);
12063a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
12075b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
12085b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
12095b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
12105b81b6b3SRodney W. Grimes 	}
12115b81b6b3SRodney W. Grimes 	else
12125b81b6b3SRodney W. Grimes 	{
12135b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
12145b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
12155b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
12165b81b6b3SRodney W. Grimes 	}
1217f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1218f5f7ba03SJordan K. Hubbard 	splx(s);
12195b81b6b3SRodney W. Grimes }
12205b81b6b3SRodney W. Grimes 
12215b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1222381fe1aaSGarrett Wollman static void
1223d0917939SPaul Richards fd_pseudointr(void *arg1)
12245b81b6b3SRodney W. Grimes {
1225381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
12265b81b6b3SRodney W. Grimes 	int	s;
12273a2f7427SDavid Greenman 
12285b81b6b3SRodney W. Grimes 	s = splbio();
12295b81b6b3SRodney W. Grimes 	fdintr(fdcu);
12305b81b6b3SRodney W. Grimes 	splx(s);
12315b81b6b3SRodney W. Grimes }
12325b81b6b3SRodney W. Grimes 
12335b81b6b3SRodney W. Grimes /***********************************************************************\
12345b81b6b3SRodney W. Grimes *                                 fdintr				*
12355b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
12365b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
12375b81b6b3SRodney W. Grimes \***********************************************************************/
1238381fe1aaSGarrett Wollman void
1239381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
12405b81b6b3SRodney W. Grimes {
12415b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1242b99f0a4aSAndrew Moore #if NFT > 0
1243b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1244b99f0a4aSAndrew Moore 
1245b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1246b99f0a4aSAndrew Moore 		(ftintr(fdu));
1247b99f0a4aSAndrew Moore 	else
1248b99f0a4aSAndrew Moore #endif
1249381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1250381fe1aaSGarrett Wollman 			;
12515b81b6b3SRodney W. Grimes }
12525b81b6b3SRodney W. Grimes 
12535b81b6b3SRodney W. Grimes /***********************************************************************\
12545b81b6b3SRodney W. Grimes * The controller state machine.						*
12555b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
12565b81b6b3SRodney W. Grimes \***********************************************************************/
12573a2f7427SDavid Greenman static int
1258dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
12595b81b6b3SRodney W. Grimes {
12604ccc87c5SPoul-Henning Kamp 	int read, format, head, sec = 0, sectrac, st0, cyl, st3;
12615b81b6b3SRodney W. Grimes 	unsigned long blknum;
12625b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
12635b81b6b3SRodney W. Grimes 	fd_p fd;
12645b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
1265b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
12663a2f7427SDavid Greenman 	size_t fdblk;
12675b81b6b3SRodney W. Grimes 
12685b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
12695b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
12705b81b6b3SRodney W. Grimes 	if(!bp)
12715b81b6b3SRodney W. Grimes 	{
12725b81b6b3SRodney W. Grimes 		/***********************************************\
12735b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
12745b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
12755b81b6b3SRodney W. Grimes 		\***********************************************/
12765b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
12775b81b6b3SRodney W. Grimes 		if(fdc->fd)
12785b81b6b3SRodney W. Grimes 		{
12796a0e6f42SRodney W. Grimes 			printf("fd%d: unexpected valid fd pointer\n",
12803a2f7427SDavid Greenman 			       fdc->fdu);
12815b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
12825b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
12835b81b6b3SRodney W. Grimes 		}
12845b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
12855b81b6b3SRodney W. Grimes  		return(0);
12865b81b6b3SRodney W. Grimes 	}
12875b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
12885b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
12893a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
12905b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
12915b81b6b3SRodney W. Grimes 	{
12926a0e6f42SRodney W. Grimes 		printf("fd%d: confused fd pointers\n", fdu);
12935b81b6b3SRodney W. Grimes 	}
12945b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1295b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1296b39c878eSAndrey A. Chernov 	if(format)
1297b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
12985b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
12995b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
13005b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
13015e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu);
13025e235068SJordan K. Hubbard 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
13035b81b6b3SRodney W. Grimes 	switch (fdc->state)
13045b81b6b3SRodney W. Grimes 	{
13055b81b6b3SRodney W. Grimes 	case DEVIDLE:
13065b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
13075b81b6b3SRodney W. Grimes 		fdc->retry = 0;
13085b81b6b3SRodney W. Grimes 		fd->skip = 0;
13095b81b6b3SRodney W. Grimes 		fdc->fd = fd;
13105b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
13113a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
13123a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
13135b81b6b3SRodney W. Grimes 		/*******************************************************\
13145b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
13155b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
13165b81b6b3SRodney W. Grimes 		\*******************************************************/
13175b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
13185b81b6b3SRodney W. Grimes 		{
13195b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13205b81b6b3SRodney W. Grimes 			return(0); /* come back later */
13215b81b6b3SRodney W. Grimes 		}
13225b81b6b3SRodney W. Grimes 		/*******************************************************\
13235b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
13245b81b6b3SRodney W. Grimes 		\*******************************************************/
13255b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
13265b81b6b3SRodney W. Grimes 		{
13275b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13285b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
13295b81b6b3SRodney W. Grimes 			return(0);
13305b81b6b3SRodney W. Grimes 		}
13315b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
13325b81b6b3SRodney W. Grimes 		{
13333a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
13345b81b6b3SRodney W. Grimes 		}
13355e235068SJordan K. Hubbard 		fdc->state = DOSEEK;
13365b81b6b3SRodney W. Grimes 		break;
13375b81b6b3SRodney W. Grimes 	case DOSEEK:
13385b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
13395b81b6b3SRodney W. Grimes 		{
13405b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
13415b81b6b3SRodney W. Grimes 			break;
13425b81b6b3SRodney W. Grimes 		}
1343dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1344dc5df763SJoerg Wunsch 			   fd->fdsu, bp->b_cylin * fd->ft->steptrac,
1345dc5df763SJoerg Wunsch 			   0))
1346dc8603e3SJoerg Wunsch 		{
1347dc8603e3SJoerg Wunsch 			/*
1348dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1349dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1350dc8603e3SJoerg Wunsch 			 */
1351dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1352dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1353dc8603e3SJoerg Wunsch 		}
1354dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
13555b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
13565b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13575b81b6b3SRodney W. Grimes 	case SEEKWAIT:
13585b81b6b3SRodney W. Grimes 		/* allow heads to settle */
135904b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
13605b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
13615b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13625b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
13635b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1364dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
13655b81b6b3SRodney W. Grimes 		{
13665b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
13673a2f7427SDavid Greenman 			do {
13683a2f7427SDavid Greenman 				/*
1369dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1370dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1371dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1372dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1373dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1374dc5df763SJoerg Wunsch 				 *
1375dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1376dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1377dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1378dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1379dc5df763SJoerg Wunsch 				 * real interrupt condition.
1380dc5df763SJoerg Wunsch 				 *
1381dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1382dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1383dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1384dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1385dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1386dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1387dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
13883a2f7427SDavid Greenman 				 */
1389dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1390dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1391dc5df763SJoerg Wunsch 					return 0;
1392dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1393dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1394dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
13953a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1396dc5df763SJoerg Wunsch 
13973a2f7427SDavid Greenman 			if (0 == descyl)
13983a2f7427SDavid Greenman 			{
1399dc5df763SJoerg Wunsch 				int failed = 0;
14003a2f7427SDavid Greenman 				/*
14013a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
14023a2f7427SDavid Greenman 				 * really there
14033a2f7427SDavid Greenman 				 */
1404dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1405dc5df763SJoerg Wunsch 					failed = 1;
14063a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
14073a2f7427SDavid Greenman 					printf(
14083a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
14093a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1410dc5df763SJoerg Wunsch 					failed = 1;
1411dc5df763SJoerg Wunsch 				}
1412dc5df763SJoerg Wunsch 
1413dc5df763SJoerg Wunsch 				if (failed)
1414dc5df763SJoerg Wunsch 				{
14153a2f7427SDavid Greenman 					if(fdc->retry < 3)
14163a2f7427SDavid Greenman 						fdc->retry = 3;
14173a2f7427SDavid Greenman 					return(retrier(fdcu));
14183a2f7427SDavid Greenman 				}
14193a2f7427SDavid Greenman 			}
1420dc5df763SJoerg Wunsch 
14215b81b6b3SRodney W. Grimes 			if (cyl != descyl)
14225b81b6b3SRodney W. Grimes 			{
14233a2f7427SDavid Greenman 				printf(
14243a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
14252d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
14265b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
14275b81b6b3SRodney W. Grimes 			}
14285b81b6b3SRodney W. Grimes 		}
14295b81b6b3SRodney W. Grimes 
14305b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
1431b39c878eSAndrey A. Chernov 		if(format)
1432b39c878eSAndrey A. Chernov 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1433b39c878eSAndrey A. Chernov 				- (char *)finfo;
14345b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
14353a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
14363a2f7427SDavid Greenman 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
14373a2f7427SDavid Greenman 			+ fd->skip/fdblk;
14385b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
14395b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
14405b81b6b3SRodney W. Grimes 		head = sec / sectrac;
14415b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
14423a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
14433a2f7427SDavid Greenman 
14443a2f7427SDavid Greenman 		if(format || !read)
14453a2f7427SDavid Greenman 		{
14463a2f7427SDavid Greenman 			/* make sure the drive is writable */
1447dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1448dc8603e3SJoerg Wunsch 			{
1449dc8603e3SJoerg Wunsch 				/* stuck controller? */
1450dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1451dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1452dc8603e3SJoerg Wunsch 			}
14533a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
14543a2f7427SDavid Greenman 			{
14553a2f7427SDavid Greenman 				/*
14563a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
14573a2f7427SDavid Greenman 				 * in order to force the current operation
14583a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
14593a2f7427SDavid Greenman 				 * error - all error handling is done
14603a2f7427SDavid Greenman 				 * by the retrier()
14613a2f7427SDavid Greenman 				 */
14623a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
14633a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
14643a2f7427SDavid Greenman 				fdc->status[2] = 0;
14653a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
14663a2f7427SDavid Greenman 				fdc->status[4] = head;
14673a2f7427SDavid Greenman 				fdc->status[5] = sec;
14683a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
14693a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
14703a2f7427SDavid Greenman 				return (1);
14713a2f7427SDavid Greenman 			}
14723a2f7427SDavid Greenman 		}
14735b81b6b3SRodney W. Grimes 
1474b39c878eSAndrey A. Chernov 		if(format)
1475b39c878eSAndrey A. Chernov 		{
1476b39c878eSAndrey A. Chernov 			/* formatting */
1477dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1478dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1479dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1480dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1481dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1482dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1483dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1484dc5df763SJoerg Wunsch 				  0))
1485dc8603e3SJoerg Wunsch 			{
1486dc8603e3SJoerg Wunsch 				/* controller fell over */
1487dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1488dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1489dc8603e3SJoerg Wunsch 			}
1490b39c878eSAndrey A. Chernov 		}
1491b39c878eSAndrey A. Chernov 		else
1492b39c878eSAndrey A. Chernov 		{
1493dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1494dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1495dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1496dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1497dc5df763SJoerg Wunsch 				   head,
1498dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1499dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1500dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1501dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1502dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1503dc5df763SJoerg Wunsch 				   0))
15045b81b6b3SRodney W. Grimes 			{
1505dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1506dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1507dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
15085b81b6b3SRodney W. Grimes 			}
1509b39c878eSAndrey A. Chernov 		}
15105b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
15115e235068SJordan K. Hubbard 		timeout(fd_timeout, (caddr_t)fdcu, hz);
15125b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15135b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
15145e235068SJordan K. Hubbard 		untimeout(fd_timeout, (caddr_t)fdcu);
1515dc5df763SJoerg Wunsch 
1516dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
15175b81b6b3SRodney W. Grimes 		{
1518dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1519dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1520dc5df763SJoerg Wunsch 			return retrier(fdcu);
15215b81b6b3SRodney W. Grimes   		}
1522dc5df763SJoerg Wunsch 
15233a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1524dc5df763SJoerg Wunsch 
15253a2f7427SDavid Greenman 		/* FALLTHROUGH */
1526dc5df763SJoerg Wunsch 
15273a2f7427SDavid Greenman 	case IOTIMEDOUT:
15285b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
15293a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
15303a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
15315b81b6b3SRodney W. Grimes 		{
15323a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15333a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1534b39c878eSAndrey A. Chernov                                 /*
15353a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
15363a2f7427SDavid Greenman 				 * and didn't release it in time for the
15373a2f7427SDavid Greenman 				 * next FDC transfer.
15383a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
15393a2f7427SDavid Greenman 				 * count. (vak)
1540b39c878eSAndrey A. Chernov                                  */
1541b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1542b39c878eSAndrey A. Chernov                                 return (1);
1543b39c878eSAndrey A. Chernov                         }
15443a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
15453a2f7427SDavid Greenman 				&& fdc->retry < 6)
15463a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
15473a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15483a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
15493a2f7427SDavid Greenman 				&& fdc->retry < 3)
15503a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
15515b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
15525b81b6b3SRodney W. Grimes 		}
15535b81b6b3SRodney W. Grimes 		/* All OK */
15543a2f7427SDavid Greenman 		fd->skip += fdblk;
1555b39c878eSAndrey A. Chernov 		if (!format && fd->skip < bp->b_bcount)
15565b81b6b3SRodney W. Grimes 		{
15575b81b6b3SRodney W. Grimes 			/* set up next transfer */
15583a2f7427SDavid Greenman 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
15593a2f7427SDavid Greenman 				+ fd->skip/fdblk;
15603a2f7427SDavid Greenman 			bp->b_cylin =
15613a2f7427SDavid Greenman 				(blknum / (fd->ft->sectrac * fd->ft->heads));
15625b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
15635b81b6b3SRodney W. Grimes 		}
15645b81b6b3SRodney W. Grimes 		else
15655b81b6b3SRodney W. Grimes 		{
15665b81b6b3SRodney W. Grimes 			/* ALL DONE */
15675b81b6b3SRodney W. Grimes 			fd->skip = 0;
15685b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
156926f9a767SRodney W. Grimes 			dp->b_actf = bp->b_actf;
15705b81b6b3SRodney W. Grimes 			biodone(bp);
15715b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
15725b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
15735b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
15745b81b6b3SRodney W. Grimes 		}
15755b81b6b3SRodney W. Grimes 		return(1);
15765b81b6b3SRodney W. Grimes 	case RESETCTLR:
15773a2f7427SDavid Greenman 		fdc_reset(fdc);
15785b81b6b3SRodney W. Grimes 		fdc->retry++;
15795b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
15805b81b6b3SRodney W. Grimes 		break;
15815b81b6b3SRodney W. Grimes 	case STARTRECAL:
1582dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1583dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1584dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1585dc8603e3SJoerg Wunsch 		{
1586dc8603e3SJoerg Wunsch 			/* arrgl */
1587dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1588dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1589dc8603e3SJoerg Wunsch 		}
15905b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
15915b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15925b81b6b3SRodney W. Grimes 	case RECALWAIT:
15935b81b6b3SRodney W. Grimes 		/* allow heads to settle */
159404b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
15955b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
15965b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15975b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
15983a2f7427SDavid Greenman 		do {
15993a2f7427SDavid Greenman 			/*
1600dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
16013a2f7427SDavid Greenman 			 */
1602dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1603dc5df763SJoerg Wunsch 				return 0;
1604dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1605dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1606dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
16073a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
16083a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
16095b81b6b3SRodney W. Grimes 		{
1610dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1611dc8603e3SJoerg Wunsch 				/*
1612dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1613dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1614dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1615dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1616dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1617dc8603e3SJoerg Wunsch 				 * time in a line
1618dc8603e3SJoerg Wunsch 				 */
1619dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1620dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
16213a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
16225b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
16235b81b6b3SRodney W. Grimes 		}
16245b81b6b3SRodney W. Grimes 		fd->track = 0;
16255b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
16265b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
16275b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16285b81b6b3SRodney W. Grimes 	case MOTORWAIT:
16295b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
16305b81b6b3SRodney W. Grimes 		{
16315b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
16325b81b6b3SRodney W. Grimes 		}
16335e235068SJordan K. Hubbard 		/*
16345e235068SJordan K. Hubbard 		 * since the controller was off, it has lost its
16355e235068SJordan K. Hubbard 		 * idea about the current track it were; thus,
16365e235068SJordan K. Hubbard 		 * recalibrate the bastard
16375e235068SJordan K. Hubbard 		 */
16385e235068SJordan K. Hubbard 		fdc->state = STARTRECAL;
16395b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16405b81b6b3SRodney W. Grimes 	default:
1641dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1642dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1643dac0f2dbSJoerg Wunsch 			printf("FDC status :%lx %lx %lx %lx %lx %lx %lx   ",
16445b81b6b3SRodney W. Grimes 			       fdc->status[0],
16455b81b6b3SRodney W. Grimes 			       fdc->status[1],
16465b81b6b3SRodney W. Grimes 			       fdc->status[2],
16475b81b6b3SRodney W. Grimes 			       fdc->status[3],
16485b81b6b3SRodney W. Grimes 			       fdc->status[4],
16495b81b6b3SRodney W. Grimes 			       fdc->status[5],
16505b81b6b3SRodney W. Grimes 			       fdc->status[6] );
16513a2f7427SDavid Greenman 		else
1652dac0f2dbSJoerg Wunsch 			printf("No status available   ");
1653dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1654dac0f2dbSJoerg Wunsch 		{
1655dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
16565b81b6b3SRodney W. Grimes 			return(0);
16575b81b6b3SRodney W. Grimes 		}
1658dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1659dac0f2dbSJoerg Wunsch 		return(0);
1660dac0f2dbSJoerg Wunsch 	}
1661dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
16625b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
16635b81b6b3SRodney W. Grimes }
16645b81b6b3SRodney W. Grimes 
1665aaf08d94SGarrett Wollman static int
1666f5f7ba03SJordan K. Hubbard retrier(fdcu)
1667f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
16685b81b6b3SRodney W. Grimes {
16695b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
16705b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
16715b81b6b3SRodney W. Grimes 
16725b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
16735b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
16745b81b6b3SRodney W. Grimes 
16753a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
16763a2f7427SDavid Greenman 		goto fail;
16775b81b6b3SRodney W. Grimes 	switch(fdc->retry)
16785b81b6b3SRodney W. Grimes 	{
16795b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
16805b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
16815b81b6b3SRodney W. Grimes 		break;
16825b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
16835b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
16845b81b6b3SRodney W. Grimes 		break;
16855b81b6b3SRodney W. Grimes 	case 6:
16865b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
16875b81b6b3SRodney W. Grimes 		break;
16885b81b6b3SRodney W. Grimes 	case 7:
16895b81b6b3SRodney W. Grimes 		break;
16905b81b6b3SRodney W. Grimes 	default:
16913a2f7427SDavid Greenman 	fail:
16925b81b6b3SRodney W. Grimes 		{
16937ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
16947ca0641bSAndrey A. Chernov 			/* Trick diskerr */
16953a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
16963a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
169792ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
16983a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
16993a2f7427SDavid Greenman 				(struct disklabel *)NULL);
17007ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
1701dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1702dc5df763SJoerg Wunsch 			{
1703dc5df763SJoerg Wunsch 				printf(
1704dc5df763SJoerg Wunsch 			" (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n",
1705dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1706dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1707dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1708dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1709dc5df763SJoerg Wunsch 				       fdc->status[5]);
1710dc5df763SJoerg Wunsch 			}
1711dc5df763SJoerg Wunsch 			else
1712dc5df763SJoerg Wunsch 				printf(" (No status)\n");
17135b81b6b3SRodney W. Grimes 		}
17145b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
17155b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
17165b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
171726f9a767SRodney W. Grimes 		dp->b_actf = bp->b_actf;
17185b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
17195b81b6b3SRodney W. Grimes 		biodone(bp);
172092ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
17215b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
17225b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1723f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
172492ed385aSRodney W. Grimes 		return(1);
17255b81b6b3SRodney W. Grimes 	}
17265b81b6b3SRodney W. Grimes 	fdc->retry++;
17275b81b6b3SRodney W. Grimes 	return(1);
17285b81b6b3SRodney W. Grimes }
17295b81b6b3SRodney W. Grimes 
1730b39c878eSAndrey A. Chernov static int
1731b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1732b39c878eSAndrey A. Chernov 	dev_t dev;
1733b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1734b39c878eSAndrey A. Chernov 	struct proc *p;
1735b39c878eSAndrey A. Chernov {
1736b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1737b39c878eSAndrey A. Chernov  	fd_p	fd;
1738b39c878eSAndrey A. Chernov 
1739b39c878eSAndrey A. Chernov 	struct buf *bp;
1740b39c878eSAndrey A. Chernov 	int rv = 0, s;
17413a2f7427SDavid Greenman 	size_t fdblk;
1742b39c878eSAndrey A. Chernov 
1743b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1744b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
17453a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1746b39c878eSAndrey A. Chernov 
1747b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1748b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1749b39c878eSAndrey A. Chernov 	if(bp == 0)
1750b39c878eSAndrey A. Chernov 		return ENOBUFS;
175182f5379bSJoerg Wunsch 	/*
175282f5379bSJoerg Wunsch 	 * keep the process from being swapped
175382f5379bSJoerg Wunsch 	 */
175482f5379bSJoerg Wunsch 	p->p_flag |= P_PHYSIO;
1755b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1756b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1757b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1758b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1759b39c878eSAndrey A. Chernov 
1760b39c878eSAndrey A. Chernov 	/*
1761b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1762b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1763b39c878eSAndrey A. Chernov 	 */
1764b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
17653a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1766b39c878eSAndrey A. Chernov 
1767b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
17685e235068SJordan K. Hubbard 	bp->b_un.b_addr = (caddr_t)finfo;
1769b39c878eSAndrey A. Chernov 
1770b39c878eSAndrey A. Chernov 	/* now do the format */
1771b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1772b39c878eSAndrey A. Chernov 
1773b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1774b39c878eSAndrey A. Chernov 	s = splbio();
1775b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1776b39c878eSAndrey A. Chernov 	{
17775e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1778b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1779b39c878eSAndrey A. Chernov 			break;
1780b39c878eSAndrey A. Chernov 	}
1781b39c878eSAndrey A. Chernov 	splx(s);
1782b39c878eSAndrey A. Chernov 
178382f5379bSJoerg Wunsch 	if(rv == EWOULDBLOCK) {
1784b39c878eSAndrey A. Chernov 		/* timed out */
1785b39c878eSAndrey A. Chernov 		rv = EIO;
178682f5379bSJoerg Wunsch 		biodone(bp);
178782f5379bSJoerg Wunsch 	}
17883a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
17893a2f7427SDavid Greenman 		rv = bp->b_error;
179082f5379bSJoerg Wunsch 	/*
179182f5379bSJoerg Wunsch 	 * allow the process to be swapped
179282f5379bSJoerg Wunsch 	 */
179382f5379bSJoerg Wunsch 	p->p_flag &= ~P_PHYSIO;
1794b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1795b39c878eSAndrey A. Chernov 	return rv;
1796b39c878eSAndrey A. Chernov }
1797b39c878eSAndrey A. Chernov 
1798f5f7ba03SJordan K. Hubbard /*
1799671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
1800f5f7ba03SJordan K. Hubbard  */
18015b81b6b3SRodney W. Grimes 
1802f5f7ba03SJordan K. Hubbard int
1803b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1804f5f7ba03SJordan K. Hubbard 	dev_t dev;
1805f5f7ba03SJordan K. Hubbard 	int cmd;
1806f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1807f5f7ba03SJordan K. Hubbard 	int flag;
1808b39c878eSAndrey A. Chernov 	struct proc *p;
1809f5f7ba03SJordan K. Hubbard {
18103a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
18113a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
18123a2f7427SDavid Greenman 	size_t fdblk;
18133a2f7427SDavid Greenman 
1814f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1815f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1816f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
18173a2f7427SDavid Greenman 	int error = 0;
1818f5f7ba03SJordan K. Hubbard 
1819b99f0a4aSAndrew Moore #if NFT > 0
1820a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1821a60eff27SNate Williams 
1822a60eff27SNate Williams 	/* check for a tape ioctl */
1823a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1824b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1825b99f0a4aSAndrew Moore #endif
1826b99f0a4aSAndrew Moore 
18273a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1828f5f7ba03SJordan K. Hubbard 
1829f5f7ba03SJordan K. Hubbard 	switch (cmd)
1830f5f7ba03SJordan K. Hubbard 	{
1831f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1832f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1833f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
18343a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
183592ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1836f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1837f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1838f5f7ba03SJordan K. Hubbard 
1839191e1a59SBruce Evans 		if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl)
1840191e1a59SBruce Evans 		    == NULL)
1841f5f7ba03SJordan K. Hubbard 			error = 0;
1842f5f7ba03SJordan K. Hubbard 		else
1843f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1844f5f7ba03SJordan K. Hubbard 
1845f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1846f5f7ba03SJordan K. Hubbard 		break;
1847f5f7ba03SJordan K. Hubbard 
1848f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1849f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1850f5f7ba03SJordan K. Hubbard 			error = EBADF;
1851f5f7ba03SJordan K. Hubbard 		break;
1852f5f7ba03SJordan K. Hubbard 
1853f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1854f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1855f5f7ba03SJordan K. Hubbard 			error = EBADF;
1856f5f7ba03SJordan K. Hubbard 		break;
1857f5f7ba03SJordan K. Hubbard 
1858f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1859f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1860f5f7ba03SJordan K. Hubbard 		{
1861f5f7ba03SJordan K. Hubbard 			error = EBADF;
1862f5f7ba03SJordan K. Hubbard 			break;
1863f5f7ba03SJordan K. Hubbard 		}
1864f5f7ba03SJordan K. Hubbard 
1865f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1866f5f7ba03SJordan K. Hubbard 
1867191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
1868191e1a59SBruce Evans 					  (u_long)0)) != 0)
1869f5f7ba03SJordan K. Hubbard 			break;
1870f5f7ba03SJordan K. Hubbard 
1871b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
187254c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
1873b39c878eSAndrey A. Chernov 		break;
1874b39c878eSAndrey A. Chernov 
1875b39c878eSAndrey A. Chernov 	case FD_FORM:
1876b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1877b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1878b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1879b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1880b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1881b39c878eSAndrey A. Chernov 		else
1882b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1883b39c878eSAndrey A. Chernov 		break;
1884b39c878eSAndrey A. Chernov 
1885b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1886b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1887f5f7ba03SJordan K. Hubbard 		break;
1888f5f7ba03SJordan K. Hubbard 
18893a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
18903a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
18913a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
18923a2f7427SDavid Greenman 			return EPERM;
18933a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
18943a2f7427SDavid Greenman 		break;
18953a2f7427SDavid Greenman 
18963a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
18973a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
18983a2f7427SDavid Greenman 		break;
18993a2f7427SDavid Greenman 
19003a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
19013a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
19023a2f7427SDavid Greenman 		break;
19033a2f7427SDavid Greenman 
1904f5f7ba03SJordan K. Hubbard 	default:
19053a2f7427SDavid Greenman 		error = ENOTTY;
1906f5f7ba03SJordan K. Hubbard 		break;
1907f5f7ba03SJordan K. Hubbard 	}
1908f5f7ba03SJordan K. Hubbard 	return (error);
1909f5f7ba03SJordan K. Hubbard }
1910f5f7ba03SJordan K. Hubbard 
19117146c13eSJulian Elischer 
19127146c13eSJulian Elischer static fd_devsw_installed = 0;
19137146c13eSJulian Elischer 
191487f6c662SJulian Elischer static void 	fd_drvinit(void *notused )
19157146c13eSJulian Elischer {
191687f6c662SJulian Elischer 	dev_t dev;
191787f6c662SJulian Elischer 
19187146c13eSJulian Elischer 	if( ! fd_devsw_installed ) {
191987f6c662SJulian Elischer 		dev = makedev(CDEV_MAJOR, 0);
192087f6c662SJulian Elischer 		cdevsw_add(&dev,&fd_cdevsw, NULL);
192187f6c662SJulian Elischer 		dev = makedev(BDEV_MAJOR, 0);
192287f6c662SJulian Elischer 		bdevsw_add(&dev,&fd_bdevsw, NULL);
19237146c13eSJulian Elischer 		fd_devsw_installed = 1;
19247146c13eSJulian Elischer 	}
19257146c13eSJulian Elischer }
192687f6c662SJulian Elischer 
192787f6c662SJulian Elischer SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL)
192887f6c662SJulian Elischer 
1929f5f7ba03SJordan K. Hubbard #endif
19303a2f7427SDavid Greenman /*
19313a2f7427SDavid Greenman  * Hello emacs, these are the
19323a2f7427SDavid Greenman  * Local Variables:
19333a2f7427SDavid Greenman  *  c-indent-level:               8
19343a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
19353a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
19363a2f7427SDavid Greenman  *  c-brace-offset:              -8
19373a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
19383a2f7427SDavid Greenman  *  c-argdecl-indent:             8
19393a2f7427SDavid Greenman  *  c-label-offset:              -8
19403a2f7427SDavid Greenman  *  c++-hanging-braces:           1
19413a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
19423a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
19433a2f7427SDavid Greenman  *  c++-friend-offset:            0
19443a2f7427SDavid Greenman  * End:
19453a2f7427SDavid Greenman  */
1946