xref: /freebsd/sys/dev/fdc/fdc.c (revision 8335c1b8ae313ac877efbff961cb45735bf82a1b)
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
468335c1b8SBruce Evans  *	$Id: fd.c,v 1.79 1996/01/27 04:17:50 bde Exp $
475b81b6b3SRodney W. Grimes  *
485b81b6b3SRodney W. Grimes  */
495b81b6b3SRodney W. Grimes 
50b99f0a4aSAndrew Moore #include "ft.h"
51b99f0a4aSAndrew Moore #if NFT < 1
52b99f0a4aSAndrew Moore #undef NFDC
53b99f0a4aSAndrew Moore #endif
545b81b6b3SRodney W. Grimes #include "fd.h"
555b81b6b3SRodney W. Grimes 
56b99f0a4aSAndrew Moore #if NFDC > 0
57b99f0a4aSAndrew Moore 
58b99f0a4aSAndrew Moore #include <sys/param.h>
59b99f0a4aSAndrew Moore #include <sys/systm.h>
60b99f0a4aSAndrew Moore #include <sys/kernel.h>
61b99f0a4aSAndrew Moore #include <sys/conf.h>
62b99f0a4aSAndrew Moore #include <sys/file.h>
63b99f0a4aSAndrew Moore #include <sys/ioctl.h>
64671e2ceeSBruce Evans #include <machine/clock.h>
65b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h>
66b99f0a4aSAndrew Moore #include <sys/disklabel.h>
67671e2ceeSBruce Evans #include <sys/diskslice.h>
6816b04b6aSJoerg Wunsch #include <machine/cpu.h>
69b99f0a4aSAndrew Moore #include <sys/buf.h>
70b99f0a4aSAndrew Moore #include <sys/uio.h>
71b99f0a4aSAndrew Moore #include <sys/malloc.h>
723a2f7427SDavid Greenman #include <sys/proc.h>
73b99f0a4aSAndrew Moore #include <sys/syslog.h>
7492200632SGarrett Wollman #include <sys/devconf.h>
7592200632SGarrett Wollman #include <sys/dkstat.h>
76f540b106SGarrett Wollman #include <i386/isa/isa.h>
77f540b106SGarrett Wollman #include <i386/isa/isa_device.h>
78f540b106SGarrett Wollman #include <i386/isa/fdreg.h>
79f540b106SGarrett Wollman #include <i386/isa/fdc.h>
80f540b106SGarrett Wollman #include <i386/isa/rtc.h>
81dc5df763SJoerg Wunsch #include <machine/stdarg.h>
8287eafbcaSPoul-Henning Kamp #if NFT > 0
8387eafbcaSPoul-Henning Kamp #include <sys/ftape.h>
8487eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h>
8587eafbcaSPoul-Henning Kamp #endif
868af5d536SJulian Elischer #ifdef DEVFS
878af5d536SJulian Elischer #include <sys/devfsext.h>
888af5d536SJulian Elischer #endif
895b81b6b3SRodney W. Grimes 
9087f6c662SJulian Elischer 
9192200632SGarrett Wollman static int fd_goaway(struct kern_devconf *, int);
9292200632SGarrett Wollman static int fdc_goaway(struct kern_devconf *, int);
934b2af45fSPoul-Henning Kamp static int fd_externalize(struct kern_devconf *, struct sysctl_req *);
945e235068SJordan K. Hubbard 
955e235068SJordan K. Hubbard /*
965e235068SJordan K. Hubbard  * Templates for the kern_devconf structures used when we attach.
975e235068SJordan K. Hubbard  */
985e235068SJordan K. Hubbard static struct kern_devconf kdc_fd[NFD] = { {
995e235068SJordan K. Hubbard 	0, 0, 0,		/* filled in by kern_devconf.c */
1005e235068SJordan K. Hubbard 	"fd", 0, { MDDT_DISK, 0 },
1015e235068SJordan K. Hubbard 	fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN,
1025e235068SJordan K. Hubbard 	0,			/* parent */
1035e235068SJordan K. Hubbard 	0,			/* parentdata */
104dc16046fSJoerg Wunsch 	DC_UNCONFIGURED,	/* state */
1056c0081e9SGarrett Wollman 	"floppy disk",
1066c0081e9SGarrett Wollman 	DC_CLS_DISK		/* class */
1075e235068SJordan K. Hubbard } };
1085e235068SJordan K. Hubbard 
1095e235068SJordan K. Hubbard struct kern_devconf kdc_fdc[NFDC] = { {
1105e235068SJordan K. Hubbard 	0, 0, 0,		/* filled in by kern_devconf.c */
1115e235068SJordan K. Hubbard 	"fdc", 0, { MDDT_ISA, 0, "bio" },
1125e235068SJordan K. Hubbard 	isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN,
1135e235068SJordan K. Hubbard 	0,			/* parent */
1145e235068SJordan K. Hubbard 	0,			/* parentdata */
115dc16046fSJoerg Wunsch 	DC_UNCONFIGURED,	/* state */
1166c0081e9SGarrett Wollman 	"floppy disk/tape controller",
1176c0081e9SGarrett Wollman 	DC_CLS_MISC		/* class */
1185e235068SJordan K. Hubbard } };
1195e235068SJordan K. Hubbard 
1205e235068SJordan K. Hubbard static inline void
1215e235068SJordan K. Hubbard fd_registerdev(int ctlr, int unit)
1225e235068SJordan K. Hubbard {
1235e235068SJordan K. Hubbard 	if(unit != 0)
1245e235068SJordan K. Hubbard 		kdc_fd[unit] = kdc_fd[0];
1255e235068SJordan K. Hubbard 
1265e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_unit = unit;
1275e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_parent = &kdc_fdc[ctlr];
1285e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_parentdata = 0;
1295e235068SJordan K. Hubbard 	dev_attach(&kdc_fd[unit]);
1305e235068SJordan K. Hubbard }
1315e235068SJordan K. Hubbard 
1325e235068SJordan K. Hubbard static inline void
1335e235068SJordan K. Hubbard fdc_registerdev(struct isa_device *dvp)
1345e235068SJordan K. Hubbard {
1355e235068SJordan K. Hubbard 	int unit = dvp->id_unit;
1365e235068SJordan K. Hubbard 
1375e235068SJordan K. Hubbard 	if(unit != 0)
1385e235068SJordan K. Hubbard 		kdc_fdc[unit] = kdc_fdc[0];
1395e235068SJordan K. Hubbard 
1405e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_unit = unit;
1415e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_parent = &kdc_isa0;
1425e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_parentdata = dvp;
1435e235068SJordan K. Hubbard 	dev_attach(&kdc_fdc[unit]);
1445e235068SJordan K. Hubbard }
1455e235068SJordan K. Hubbard 
1465e235068SJordan K. Hubbard static int
1475e235068SJordan K. Hubbard fdc_goaway(struct kern_devconf *kdc, int force)
1485e235068SJordan K. Hubbard {
1495e235068SJordan K. Hubbard 	if(force) {
1505e235068SJordan K. Hubbard 		dev_detach(kdc);
1515e235068SJordan K. Hubbard 		return 0;
1525e235068SJordan K. Hubbard 	} else {
1535e235068SJordan K. Hubbard 		return EBUSY;	/* XXX fix */
1545e235068SJordan K. Hubbard 	}
1555e235068SJordan K. Hubbard }
1565e235068SJordan K. Hubbard 
1575e235068SJordan K. Hubbard static int
1585e235068SJordan K. Hubbard fd_goaway(struct kern_devconf *kdc, int force)
1595e235068SJordan K. Hubbard {
1605e235068SJordan K. Hubbard 	dev_detach(kdc);
1615e235068SJordan K. Hubbard 	return 0;
1625e235068SJordan K. Hubbard }
16392200632SGarrett Wollman 
164671e2ceeSBruce Evans #define	b_cylin	b_resid		/* XXX now spelled b_cylinder elsewhere */
1655b81b6b3SRodney W. Grimes 
166b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
167b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
1685b81b6b3SRodney W. Grimes 
1693a2f7427SDavid Greenman /*
1703a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
1713a2f7427SDavid Greenman  * on the controller
1723a2f7427SDavid Greenman  */
1733a2f7427SDavid Greenman #define id_physid id_scsiid
1743a2f7427SDavid Greenman 
175dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
176dc5df763SJoerg Wunsch #define FD_FAILED -1
177dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
178dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
179dc5df763SJoerg Wunsch 
180b39c878eSAndrey A. Chernov #define NUMTYPES 14
181b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1827ca0641bSAndrey A. Chernov 
1833a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
184b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
185b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
186b99f0a4aSAndrew Moore #define FD_1720         1
187b99f0a4aSAndrew Moore #define FD_1480         2
188b99f0a4aSAndrew Moore #define FD_1440         3
189b99f0a4aSAndrew Moore #define FD_1200         4
190b99f0a4aSAndrew Moore #define FD_820          5
191b99f0a4aSAndrew Moore #define FD_800          6
192b99f0a4aSAndrew Moore #define FD_720          7
193b99f0a4aSAndrew Moore #define FD_360          8
194ed2fa05eSAndrey A. Chernov 
195b99f0a4aSAndrew Moore #define FD_1480in5_25   9
196b99f0a4aSAndrew Moore #define FD_1440in5_25   10
197b99f0a4aSAndrew Moore #define FD_820in5_25    11
198b99f0a4aSAndrew Moore #define FD_800in5_25    12
199b99f0a4aSAndrew Moore #define FD_720in5_25    13
200b99f0a4aSAndrew Moore #define FD_360in5_25    14
201b99f0a4aSAndrew Moore 
2027ca0641bSAndrey A. Chernov 
2036f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
2045b81b6b3SRodney W. Grimes {
205126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
206126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
207126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
208126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
209126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
210126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
211126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
212b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
213ed2fa05eSAndrey A. Chernov 
214126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
215126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
216126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
217126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
218126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
219126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
2205b81b6b3SRodney W. Grimes };
2215b81b6b3SRodney W. Grimes 
222b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
223dc16046fSJoerg Wunsch 
2245b81b6b3SRodney W. Grimes /***********************************************************************\
2255b81b6b3SRodney W. Grimes * Per controller structure.						*
2265b81b6b3SRodney W. Grimes \***********************************************************************/
227b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
2285b81b6b3SRodney W. Grimes 
2295b81b6b3SRodney W. Grimes /***********************************************************************\
2305b81b6b3SRodney W. Grimes * Per drive structure.							*
231b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
2325b81b6b3SRodney W. Grimes \***********************************************************************/
2336f4e0bebSPoul-Henning Kamp static struct fd_data {
234b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2355b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2363a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
2375b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
2385b81b6b3SRodney W. Grimes 	int	flags;
2395b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
2405b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
2415b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
2425b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
2435b81b6b3SRodney W. Grimes 	int	skip;
2445b81b6b3SRodney W. Grimes 	int	hddrv;
245dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
2465b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
2473a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
24892200632SGarrett Wollman 	int	dkunit;		/* disk stats unit number */
24987f6c662SJulian Elischer #ifdef	DEVFS
250c8f2fe8dSBruce Evans 	void	*bdev;
251c8f2fe8dSBruce Evans 	void	*cdev;
25287f6c662SJulian Elischer #endif
2535b81b6b3SRodney W. Grimes } fd_data[NFD];
2545b81b6b3SRodney W. Grimes 
2555b81b6b3SRodney W. Grimes /***********************************************************************\
2565b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
2575b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
2585b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
2595b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
2605b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
2615b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
2625b81b6b3SRodney W. Grimes \***********************************************************************/
263b99f0a4aSAndrew Moore 
2643a2f7427SDavid Greenman #if NFT > 0
2653a2f7427SDavid Greenman int ftopen(dev_t, int);
2663a2f7427SDavid Greenman int ftintr(ftu_t ftu);
2673a2f7427SDavid Greenman int ftclose(dev_t, int);
2683a2f7427SDavid Greenman void ftstrategy(struct buf *);
2693a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
2703a2f7427SDavid Greenman int ftdump(dev_t);
2713a2f7427SDavid Greenman int ftsize(dev_t);
27274fa89f4SRodney W. Grimes int ftattach(struct isa_device *, struct isa_device *, int);
2733a2f7427SDavid Greenman #endif
2745b81b6b3SRodney W. Grimes 
2753a2f7427SDavid Greenman /* autoconfig functions */
2763a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2773a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2783a2f7427SDavid Greenman 
2793a2f7427SDavid Greenman /* needed for ft driver, thus exported */
2803a2f7427SDavid Greenman int in_fdc(fdcu_t);
2813a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
2823a2f7427SDavid Greenman 
2833a2f7427SDavid Greenman /* internal functions */
2843a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
2853a2f7427SDavid Greenman #  define TURNON 1
2863a2f7427SDavid Greenman #  define TURNOFF 0
2873a2f7427SDavid Greenman static timeout_t fd_turnoff;
2883a2f7427SDavid Greenman static timeout_t fd_motor_on;
2893a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2903a2f7427SDavid Greenman static void fdc_reset(fdc_p);
291b5e8ce9fSBruce Evans static int fd_in(fdcu_t, int *);
2923a2f7427SDavid Greenman static void fdstart(fdcu_t);
2933a2f7427SDavid Greenman static timeout_t fd_timeout;
2943a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2953a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
296aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2973a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2983a2f7427SDavid Greenman 
299aaf08d94SGarrett Wollman 
3005b81b6b3SRodney W. Grimes #define DEVIDLE		0
3015b81b6b3SRodney W. Grimes #define FINDWORK	1
3025b81b6b3SRodney W. Grimes #define	DOSEEK		2
3035b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
3045b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
3055b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
3065b81b6b3SRodney W. Grimes #define	STARTRECAL	6
3075b81b6b3SRodney W. Grimes #define	RESETCTLR	7
3085b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
3095b81b6b3SRodney W. Grimes #define	RECALWAIT	9
3105b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
3115b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
3125b81b6b3SRodney W. Grimes 
3135b81b6b3SRodney W. Grimes #ifdef	DEBUG
3145b81b6b3SRodney W. Grimes char *fdstates[] =
3155b81b6b3SRodney W. Grimes {
3165b81b6b3SRodney W. Grimes "DEVIDLE",
3175b81b6b3SRodney W. Grimes "FINDWORK",
3185b81b6b3SRodney W. Grimes "DOSEEK",
3195b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
3205b81b6b3SRodney W. Grimes "IOCOMPLETE",
3215b81b6b3SRodney W. Grimes "RECALCOMPLETE",
3225b81b6b3SRodney W. Grimes "STARTRECAL",
3235b81b6b3SRodney W. Grimes "RESETCTLR",
3245b81b6b3SRodney W. Grimes "SEEKWAIT",
3255b81b6b3SRodney W. Grimes "RECALWAIT",
3265b81b6b3SRodney W. Grimes "MOTORWAIT",
3275b81b6b3SRodney W. Grimes "IOTIMEDOUT"
3285b81b6b3SRodney W. Grimes };
3295b81b6b3SRodney W. Grimes 
3303a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
3313a2f7427SDavid Greenman int	fd_debug = 0;
3325b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
3335b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
334381fe1aaSGarrett Wollman #else /* DEBUG */
3355b81b6b3SRodney W. Grimes #define TRACE0(arg)
3365b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
337381fe1aaSGarrett Wollman #endif /* DEBUG */
3385b81b6b3SRodney W. Grimes 
339dc16046fSJoerg Wunsch /* autoconfig structure */
340dc16046fSJoerg Wunsch 
341dc16046fSJoerg Wunsch struct	isa_driver fdcdriver = {
342dc16046fSJoerg Wunsch 	fdprobe, fdattach, "fdc",
343dc16046fSJoerg Wunsch };
344dc16046fSJoerg Wunsch 
34587f6c662SJulian Elischer static	d_open_t	Fdopen;	/* NOTE, not fdopen */
34687f6c662SJulian Elischer static	d_close_t	fdclose;
34787f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
34887f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
34987f6c662SJulian Elischer 
35087f6c662SJulian Elischer #define CDEV_MAJOR 9
35187f6c662SJulian Elischer #define BDEV_MAJOR 2
35287f6c662SJulian Elischer extern	struct cdevsw fd_cdevsw;
353d2f265faSPoul-Henning Kamp static struct bdevsw fd_bdevsw =
35487f6c662SJulian Elischer 	{ Fdopen,	fdclose,	fdstrategy,	fdioctl,	/*2*/
355f332b8a1SBruce Evans 	  nodump,	nopsize,	0,	"fd",	&fd_cdevsw,	-1 };
35687f6c662SJulian Elischer 
357d2f265faSPoul-Henning Kamp static struct cdevsw fd_cdevsw =
35887f6c662SJulian Elischer 	{ Fdopen,	fdclose,	rawread,	rawwrite,	/*9*/
35987f6c662SJulian Elischer 	  fdioctl,	nostop,		nullreset,	nodevtotty,
36087f6c662SJulian Elischer 	  seltrue,	nommap,		fdstrategy,	"fd",
36187f6c662SJulian Elischer 	  &fd_bdevsw,	-1 };
36287f6c662SJulian Elischer 
3636f4e0bebSPoul-Henning Kamp static struct isa_device *fdcdevs[NFDC];
36492200632SGarrett Wollman 
36592200632SGarrett Wollman /*
36692200632SGarrett Wollman  * Provide hw.devconf information.
36792200632SGarrett Wollman  */
36892200632SGarrett Wollman static int
3694b2af45fSPoul-Henning Kamp fd_externalize(struct kern_devconf *kdc, struct sysctl_req *req)
37092200632SGarrett Wollman {
3714b2af45fSPoul-Henning Kamp 	return disk_externalize(fd_data[kdc->kdc_unit].fdsu, req);
37292200632SGarrett Wollman }
37392200632SGarrett Wollman 
374dc5df763SJoerg Wunsch static int
375dc5df763SJoerg Wunsch fdc_err(fdcu_t fdcu, const char *s)
376dc5df763SJoerg Wunsch {
377dc5df763SJoerg Wunsch 	fdc_data[fdcu].fdc_errs++;
37816b04b6aSJoerg Wunsch 	if(s) {
379dc5df763SJoerg Wunsch 		if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX)
3806a0e6f42SRodney W. Grimes 			printf("fdc%d: %s", fdcu, s);
381dc5df763SJoerg Wunsch 		else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX)
382dc5df763SJoerg Wunsch 			printf("fdc%d: too many errors, not logging any more\n",
383dc5df763SJoerg Wunsch 			       fdcu);
38416b04b6aSJoerg Wunsch 	}
385dc5df763SJoerg Wunsch 
386dc5df763SJoerg Wunsch 	return FD_FAILED;
387dc5df763SJoerg Wunsch }
388dc5df763SJoerg Wunsch 
389dc5df763SJoerg Wunsch /*
390dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
391dc5df763SJoerg Wunsch  * Unit number,
392dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
393dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
394dc5df763SJoerg Wunsch  */
395dc5df763SJoerg Wunsch 
3966f4e0bebSPoul-Henning Kamp static int
397dc5df763SJoerg Wunsch fd_cmd(fdcu_t fdcu, int n_out, ...)
398dc5df763SJoerg Wunsch {
399dc5df763SJoerg Wunsch 	u_char cmd;
400dc5df763SJoerg Wunsch 	int n_in;
401dc5df763SJoerg Wunsch 	int n;
402dc5df763SJoerg Wunsch 	va_list ap;
403dc5df763SJoerg Wunsch 
404dc5df763SJoerg Wunsch 	va_start(ap, n_out);
405dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
406dc5df763SJoerg Wunsch 	va_end(ap);
407dc5df763SJoerg Wunsch 	va_start(ap, n_out);
408dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
409dc5df763SJoerg Wunsch 	{
410dc5df763SJoerg Wunsch 		if (out_fdc(fdcu, va_arg(ap, int)) < 0)
411dc5df763SJoerg Wunsch 		{
412dc5df763SJoerg Wunsch 			char msg[50];
413dc5df763SJoerg Wunsch 			sprintf(msg,
414dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
415dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
416dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
417dc5df763SJoerg Wunsch 		}
418dc5df763SJoerg Wunsch 	}
419dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
420dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
421dc5df763SJoerg Wunsch 	{
422dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
423dc5df763SJoerg Wunsch 		if (fd_in(fdcu, ptr) < 0)
424dc5df763SJoerg Wunsch 		{
425dc5df763SJoerg Wunsch 			char msg[50];
426dc5df763SJoerg Wunsch 			sprintf(msg,
427dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
428dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
429dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
430dc5df763SJoerg Wunsch 		}
431dc5df763SJoerg Wunsch 	}
432dc5df763SJoerg Wunsch 
433dc5df763SJoerg Wunsch 	return 0;
434dc5df763SJoerg Wunsch }
435dc5df763SJoerg Wunsch 
4366f4e0bebSPoul-Henning Kamp static int
437dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
438dc5df763SJoerg Wunsch {
439dc5df763SJoerg Wunsch 	int st3;
440dc5df763SJoerg Wunsch 
441dc5df763SJoerg Wunsch 	if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
442dc5df763SJoerg Wunsch 	{
4436a0e6f42SRodney W. Grimes 		return fdc_err(fdc->fdcu, "Sense Drive Status failed\n");
444dc5df763SJoerg Wunsch 	}
445dc5df763SJoerg Wunsch 	if (st3p)
446dc5df763SJoerg Wunsch 		*st3p = st3;
447dc5df763SJoerg Wunsch 
448dc5df763SJoerg Wunsch 	return 0;
449dc5df763SJoerg Wunsch }
450dc5df763SJoerg Wunsch 
4516f4e0bebSPoul-Henning Kamp static int
452dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
453dc5df763SJoerg Wunsch {
454dc5df763SJoerg Wunsch 	int st0, cyl;
455dc5df763SJoerg Wunsch 
456dc5df763SJoerg Wunsch 	int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0);
457dc5df763SJoerg Wunsch 
458dc5df763SJoerg Wunsch 	if (ret)
459dc5df763SJoerg Wunsch 	{
460dc5df763SJoerg Wunsch 		(void)fdc_err(fdc->fdcu,
461dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
462dc5df763SJoerg Wunsch 		return ret;
463dc5df763SJoerg Wunsch 	}
464dc5df763SJoerg Wunsch 
465dc5df763SJoerg Wunsch 	if (st0p)
466dc5df763SJoerg Wunsch 		*st0p = st0;
467dc5df763SJoerg Wunsch 
468dc5df763SJoerg Wunsch 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV)
469dc5df763SJoerg Wunsch 	{
470dc5df763SJoerg Wunsch 		/*
471dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
472dc5df763SJoerg Wunsch 		 */
473dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
474dc5df763SJoerg Wunsch 	}
475dc5df763SJoerg Wunsch 
476dc5df763SJoerg Wunsch 	if (fd_in(fdc->fdcu, &cyl) < 0)
477dc5df763SJoerg Wunsch 	{
478dc5df763SJoerg Wunsch 		return fdc_err(fdc->fdcu, "can't get cyl num\n");
479dc5df763SJoerg Wunsch 	}
480dc5df763SJoerg Wunsch 
481dc5df763SJoerg Wunsch 	if (cylp)
482dc5df763SJoerg Wunsch 		*cylp = cyl;
483dc5df763SJoerg Wunsch 
484dc5df763SJoerg Wunsch 	return 0;
485dc5df763SJoerg Wunsch }
486dc5df763SJoerg Wunsch 
487dc5df763SJoerg Wunsch 
4886f4e0bebSPoul-Henning Kamp static int
489dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
490dc5df763SJoerg Wunsch {
491dc5df763SJoerg Wunsch 	int i, ret;
492b5e8ce9fSBruce Evans 
493dc5df763SJoerg Wunsch 	for (i = 0; i < 7; i++)
494dc5df763SJoerg Wunsch 	{
495b5e8ce9fSBruce Evans 		/*
496b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
497b5e8ce9fSBruce Evans 		 * from the hardware, but fdc_status wants u_longs and
498b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
499b5e8ce9fSBruce Evans 		 */
500b5e8ce9fSBruce Evans 		int status;
501b5e8ce9fSBruce Evans 
502b5e8ce9fSBruce Evans 		ret = fd_in(fdc->fdcu, &status);
503b5e8ce9fSBruce Evans 		fdc->status[i] = status;
504b5e8ce9fSBruce Evans 		if (ret != 0)
505dc5df763SJoerg Wunsch 			break;
506dc5df763SJoerg Wunsch 	}
507dc5df763SJoerg Wunsch 
508dc5df763SJoerg Wunsch 	if (ret == 0)
509dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
510dc5df763SJoerg Wunsch 	else
511dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
512dc5df763SJoerg Wunsch 
513dc5df763SJoerg Wunsch 	return ret;
514dc5df763SJoerg Wunsch }
515dc5df763SJoerg Wunsch 
5165b81b6b3SRodney W. Grimes /****************************************************************************/
5175b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
5185b81b6b3SRodney W. Grimes /****************************************************************************/
519dc5df763SJoerg Wunsch 
5205b81b6b3SRodney W. Grimes /*
5215b81b6b3SRodney W. Grimes  * probe for existance of controller
5225b81b6b3SRodney W. Grimes  */
5233a2f7427SDavid Greenman static int
524dc5df763SJoerg Wunsch fdprobe(struct isa_device *dev)
5255b81b6b3SRodney W. Grimes {
5265b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5275b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
5285b81b6b3SRodney W. Grimes 	{
5296a0e6f42SRodney W. Grimes 		printf("fdc%d: unit used multiple times\n", fdcu);
5305b81b6b3SRodney W. Grimes 		return 0;
5315b81b6b3SRodney W. Grimes 	}
5325b81b6b3SRodney W. Grimes 
53392200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
5345b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
5355b81b6b3SRodney W. Grimes 
5366c0081e9SGarrett Wollman #ifndef DEV_LKM
5376c0081e9SGarrett Wollman 	fdc_registerdev(dev);
5386c0081e9SGarrett Wollman #endif
5396c0081e9SGarrett Wollman 
54016111cedSAndrew Moore 	/* First - lets reset the floppy controller */
5413a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
54216111cedSAndrew Moore 	DELAY(100);
5433a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
54416111cedSAndrew Moore 
5455b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
546dc5df763SJoerg Wunsch 	if (fd_cmd(fdcu,
547dc5df763SJoerg Wunsch 		   3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
548dc5df763SJoerg Wunsch 		   0))
5495b81b6b3SRodney W. Grimes 	{
5505b81b6b3SRodney W. Grimes 		return(0);
5515b81b6b3SRodney W. Grimes 	}
552dc16046fSJoerg Wunsch 	kdc_fdc[fdcu].kdc_state = DC_IDLE;
5535b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
5545b81b6b3SRodney W. Grimes }
5555b81b6b3SRodney W. Grimes 
5565b81b6b3SRodney W. Grimes /*
5575b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
5585b81b6b3SRodney W. Grimes  */
5593a2f7427SDavid Greenman static int
560dc5df763SJoerg Wunsch fdattach(struct isa_device *dev)
5615b81b6b3SRodney W. Grimes {
5623a2f7427SDavid Greenman 	unsigned fdt;
5635b81b6b3SRodney W. Grimes 	fdu_t	fdu;
5645b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5655b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
5665b81b6b3SRodney W. Grimes 	fd_p	fd;
56774fa89f4SRodney W. Grimes 	int	fdsu, st0, st3, i, unithasfd;
568b99f0a4aSAndrew Moore 	struct isa_device *fdup;
569dc5df763SJoerg Wunsch 	int ic_type = 0;
570999422d7SJulian Elischer #ifdef	DEVFS
571c8f2fe8dSBruce Evans 	int	mynor;
572999422d7SJulian Elischer 	char	name[64];
573999422d7SJulian Elischer #endif	/* DEVFS */
57492200632SGarrett Wollman 
5755b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
5765b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
5775b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
578dd87702aSBruce Evans 	isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
5795b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
5803a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
5813a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
5825b81b6b3SRodney W. Grimes 
5835b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
584b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
585b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
586b99f0a4aSAndrew Moore 			continue;
587b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
588b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
589b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
590b99f0a4aSAndrew Moore 			continue;
591b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
592b99f0a4aSAndrew Moore 		/* look up what bios thinks we have */
593b99f0a4aSAndrew Moore 		switch (fdu) {
594b99f0a4aSAndrew Moore 			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
595b99f0a4aSAndrew Moore 				break;
596b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
597b99f0a4aSAndrew Moore 				break;
598b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
599b99f0a4aSAndrew Moore 				break;
600b99f0a4aSAndrew Moore 		}
6015b81b6b3SRodney W. Grimes 		/* is there a unit? */
602b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
603b99f0a4aSAndrew Moore #if NFT > 0
604b99f0a4aSAndrew Moore 		    || (fdsu >= DRVS_PER_CTLR)) {
605b99f0a4aSAndrew Moore #else
606b99f0a4aSAndrew Moore 		) {
60756ef0285SAndrew Moore 			fd->type = NO_TYPE;
608b99f0a4aSAndrew Moore #endif
609b99f0a4aSAndrew Moore #if NFT > 0
610b99f0a4aSAndrew Moore 			/* If BIOS says no floppy, or > 2nd device */
611b99f0a4aSAndrew Moore 			/* Probe for and attach a floppy tape.     */
61274fa89f4SRodney W. Grimes 			/* Tell FT if there was already a disk     */
61374fa89f4SRodney W. Grimes 			/* with this unit number found.            */
61474fa89f4SRodney W. Grimes 
61574fa89f4SRodney W. Grimes 			unithasfd = 0;
61674fa89f4SRodney W. Grimes 			if (fdu < NFD && fd->type != NO_TYPE)
61774fa89f4SRodney W. Grimes 				unithasfd = 1;
61885827d9cSJoerg Wunsch 			if (ftattach(dev, fdup, unithasfd))
619b99f0a4aSAndrew Moore 				continue;
62056ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
621b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
62256ef0285SAndrew Moore #endif
6235b81b6b3SRodney W. Grimes 			continue;
624f5f7ba03SJordan K. Hubbard 		}
6255b81b6b3SRodney W. Grimes 
6265b81b6b3SRodney W. Grimes 		/* select it */
6273a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
6286b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
629dc5df763SJoerg Wunsch 
630dc5df763SJoerg Wunsch 		if (ic_type == 0 &&
631dc5df763SJoerg Wunsch 		    fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
632dc5df763SJoerg Wunsch 		{
6336a0e6f42SRodney W. Grimes 			printf("fdc%d: ", fdcu);
634dc5df763SJoerg Wunsch 			ic_type = (u_char)ic_type;
635dc5df763SJoerg Wunsch 			switch( ic_type ) {
636dc5df763SJoerg Wunsch 			case 0x80:
6376a0e6f42SRodney W. Grimes 				printf("NEC 765\n");
638dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE765;
6396c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6406c0081e9SGarrett Wollman 					"NEC 765 floppy disk/tape controller";
641dc5df763SJoerg Wunsch 				break;
642dc5df763SJoerg Wunsch 			case 0x81:
6436a0e6f42SRodney W. Grimes 				printf("Intel 82077\n");
644dc5df763SJoerg Wunsch 				fdc->fdct = FDC_I82077;
6456c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6466c0081e9SGarrett Wollman 					"Intel 82077 floppy disk/tape controller";
647dc5df763SJoerg Wunsch 				break;
648dc5df763SJoerg Wunsch 			case 0x90:
6496a0e6f42SRodney W. Grimes 				printf("NEC 72065B\n");
650dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE72065;
6516c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6526c0081e9SGarrett Wollman 					"NEC 72065B floppy disk/tape controller";
653dc5df763SJoerg Wunsch 				break;
654dc5df763SJoerg Wunsch 			default:
6556a0e6f42SRodney W. Grimes 				printf("unknown IC type %02x\n", ic_type);
656dc5df763SJoerg Wunsch 				fdc->fdct = FDC_UNKNOWN;
657dc5df763SJoerg Wunsch 				break;
6586b7bd95bSJoerg Wunsch 			}
659dc5df763SJoerg Wunsch 		}
660dc5df763SJoerg Wunsch 		if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
661dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
662dc5df763SJoerg Wunsch 			/* if at track 0, first seek inwards */
663dc5df763SJoerg Wunsch 			/* seek some steps: */
664dc5df763SJoerg Wunsch 			(void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
665dc5df763SJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
666dc5df763SJoerg Wunsch 			(void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
667dc5df763SJoerg Wunsch 		}
668dc5df763SJoerg Wunsch 
669dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
670dc5df763SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
671dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
672dc5df763SJoerg Wunsch 			/* Seek some steps... */
673dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
674dc5df763SJoerg Wunsch 				/* ...wait a moment... */
675dc5df763SJoerg Wunsch 				DELAY(300000);
676dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
677dc5df763SJoerg Wunsch 				(void)fd_sense_int(fdc, 0, 0);
678dc5df763SJoerg Wunsch 			}
679dc5df763SJoerg Wunsch 		}
680dc5df763SJoerg Wunsch 
6816b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
6826b7bd95bSJoerg Wunsch 			/*
6836b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
6846b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
6856b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
6866b7bd95bSJoerg Wunsch 			 * more than 77 steps
6876b7bd95bSJoerg Wunsch 			 */
688dc5df763SJoerg Wunsch 			/* go back to 0: */
689dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
6906b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
6916b7bd95bSJoerg Wunsch 				DELAY(i == 0? 1000000: 300000);
6925b81b6b3SRodney W. Grimes 
6936b7bd95bSJoerg Wunsch 				/* anything responding? */
694dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
695dc5df763SJoerg Wunsch 				(st0 & NE7_ST0_EC) == 0)
6966b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
6976b7bd95bSJoerg Wunsch 			}
698dc5df763SJoerg Wunsch 		}
6996b7bd95bSJoerg Wunsch 
7003a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
7013a2f7427SDavid Greenman 
7023a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
7035b81b6b3SRodney W. Grimes 			continue;
7045b81b6b3SRodney W. Grimes 
705dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
706b99f0a4aSAndrew Moore 		fd->fdc = fdc;
707b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
7083a2f7427SDavid Greenman 		fd->options = 0;
7092d9d0204SRodney W. Grimes 		printf("fd%d: ", fdu);
7105b81b6b3SRodney W. Grimes 
7116c0081e9SGarrett Wollman 		fd_registerdev(fdcu, fdu);
712b99f0a4aSAndrew Moore 		switch (fdt) {
7137ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
7146a0e6f42SRodney W. Grimes 			printf("1.2MB 5.25in\n");
715b99f0a4aSAndrew Moore 			fd->type = FD_1200;
7166c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7176c0081e9SGarrett Wollman 				"1.2MB (1200K) 5.25in floppy disk drive";
718999422d7SJulian Elischer #ifdef	DEVFS
7194d92e198SJulian Elischer 			sprintf(name,"rfd%d.1200",fdu);
720999422d7SJulian Elischer #endif	/* DEVFS */
7217ca0641bSAndrey A. Chernov 			break;
7227ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
7236a0e6f42SRodney W. Grimes 			printf("1.44MB 3.5in\n");
724b99f0a4aSAndrew Moore 			fd->type = FD_1440;
7256c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7266c0081e9SGarrett Wollman 				"1.44MB (1440K) 3.5in floppy disk drive";
727999422d7SJulian Elischer #ifdef	DEVFS
7284d92e198SJulian Elischer 			sprintf(name,"rfd%d.1440",fdu);
729999422d7SJulian Elischer #endif	/* DEVFS */
7307ca0641bSAndrey A. Chernov 			break;
731290dd077SJoerg Wunsch 		case RTCFDT_288M:
73286a727d9SJoerg Wunsch 		case RTCFDT_288M_1:
7336a0e6f42SRodney W. Grimes 			printf("2.88MB 3.5in - 1.44MB mode\n");
734290dd077SJoerg Wunsch 			fd->type = FD_1440;
7356c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7366c0081e9SGarrett Wollman 				"2.88MB (2880K) 3.5in floppy disk drive in 1.44 mode";
737999422d7SJulian Elischer #ifdef	DEVFS
7384d92e198SJulian Elischer 			sprintf(name,"rfd%d.1440",fdu);
739999422d7SJulian Elischer #endif	/* DEVFS */
740290dd077SJoerg Wunsch 			break;
7417ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
7426a0e6f42SRodney W. Grimes 			printf("360KB 5.25in\n");
743b99f0a4aSAndrew Moore 			fd->type = FD_360;
7446c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7456c0081e9SGarrett Wollman 				"360KB 5.25in floppy disk drive";
746999422d7SJulian Elischer #ifdef	DEVFS
7474d92e198SJulian Elischer 			sprintf(name,"rfd%d.360",fdu);
748999422d7SJulian Elischer #endif	/* DEVFS */
7497ca0641bSAndrey A. Chernov 			break;
750ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
7516a0e6f42SRodney W. Grimes 			printf("720KB 3.5in\n");
752b99f0a4aSAndrew Moore 			fd->type = FD_720;
7536c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7546c0081e9SGarrett Wollman 				"720KB 3.5in floppy disk drive";
755999422d7SJulian Elischer #ifdef	DEVFS
7564d92e198SJulian Elischer 			sprintf(name,"rfd%d.720",fdu);
757999422d7SJulian Elischer #endif	/* DEVFS */
758ed2fa05eSAndrey A. Chernov 			break;
7597ca0641bSAndrey A. Chernov 		default:
7606a0e6f42SRodney W. Grimes 			printf("unknown\n");
761b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
762999422d7SJulian Elischer #ifdef	DEVFS
7634d92e198SJulian Elischer 			sprintf(name,"rfd%d.xxxx",fdu);
764999422d7SJulian Elischer #endif	/* DEVFS */
7657ca0641bSAndrey A. Chernov 			break;
7665b81b6b3SRodney W. Grimes 		}
767dc16046fSJoerg Wunsch 		kdc_fd[fdu].kdc_state = DC_IDLE;
768999422d7SJulian Elischer #ifdef DEVFS
769c8f2fe8dSBruce Evans 		mynor = 8 * fdu;
770c8f2fe8dSBruce Evans 		fd->bdev = devfs_add_devsw("/", name + 1, &fd_bdevsw, mynor,
771c8f2fe8dSBruce Evans 					   DV_BLK, 0, 0, 0640);
772c8f2fe8dSBruce Evans 		fd->cdev = devfs_add_devsw("/", name, &fd_cdevsw, mynor,
773c8f2fe8dSBruce Evans 					   DV_CHR, 0, 0, 0640);
774c8f2fe8dSBruce Evans 		sprintf(name, "rfd%d", fdu);
775c8f2fe8dSBruce Evans 		dev_link("/", name + 1, fd->bdev);
776c8f2fe8dSBruce Evans 		dev_link("/", name, fd->cdev);
777999422d7SJulian Elischer #endif /* DEVFS */
77892200632SGarrett Wollman 		if (dk_ndrive < DK_NDRIVE) {
77992200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
7806a0e6f42SRodney W. Grimes 			fd->dkunit = dk_ndrive++;
781671e2ceeSBruce Evans 			/*
782671e2ceeSBruce Evans 			 * XXX assume rate is FDC_500KBPS.
783671e2ceeSBruce Evans 			 */
784671e2ceeSBruce Evans 			dk_wpms[dk_ndrive] = 500000 / 8 / 2;
78592200632SGarrett Wollman 		} else {
78692200632SGarrett Wollman 			fd->dkunit = -1;
78792200632SGarrett Wollman 		}
7885b81b6b3SRodney W. Grimes 	}
7895b81b6b3SRodney W. Grimes 
7903a2f7427SDavid Greenman 	return (1);
7915b81b6b3SRodney W. Grimes }
7925b81b6b3SRodney W. Grimes 
7935b81b6b3SRodney W. Grimes /****************************************************************************/
7945b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
7955e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
7965b81b6b3SRodney W. Grimes /****************************************************************************/
7973a2f7427SDavid Greenman static void
798dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
7995b81b6b3SRodney W. Grimes {
8003a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
8013a2f7427SDavid Greenman 	int needspecify = 0;
8023a2f7427SDavid Greenman 
8033a2f7427SDavid Greenman 	if(turnon) {
8043a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
8053a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
8063a2f7427SDavid Greenman 	} else
8073a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
8083a2f7427SDavid Greenman 
8095e235068SJordan K. Hubbard 	if(!turnon
8105e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
8115e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
8125e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
8135e235068SJordan K. Hubbard 	else {
8143a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
8153a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
8163a2f7427SDavid Greenman 			needspecify = 1;
8173a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
8185b81b6b3SRodney W. Grimes 	}
8195b81b6b3SRodney W. Grimes 
8203a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
8213a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
822dc16046fSJoerg Wunsch 	kdc_fdc[fdcu].kdc_state = (fdout & FDO_FRST)? DC_BUSY: DC_IDLE;
8233a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
8243a2f7427SDavid Greenman 
8253a2f7427SDavid Greenman 	if(needspecify) {
826dc8603e3SJoerg Wunsch 		/*
827dc5df763SJoerg Wunsch 		 * XXX
828dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
829dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
830dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
831dc8603e3SJoerg Wunsch 		 */
832dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
833dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
834dc5df763SJoerg Wunsch 			     0);
8353a2f7427SDavid Greenman 	}
8363a2f7427SDavid Greenman }
8373a2f7427SDavid Greenman 
838381fe1aaSGarrett Wollman static void
839d0917939SPaul Richards fd_turnoff(void *arg1)
8405b81b6b3SRodney W. Grimes {
841381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
842f5f7ba03SJordan K. Hubbard 	int	s;
8435b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
844dc16046fSJoerg Wunsch 
845dc16046fSJoerg Wunsch 	TRACE1("[fd%d: turnoff]", fdu);
8468335c1b8SBruce Evans 
8478335c1b8SBruce Evans 	/*
8488335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
8498335c1b8SBruce Evans 	 * XXX shouldn't even schedule turnoff until drive is inactive
8508335c1b8SBruce Evans 	 * and nothing is queued on it.
8518335c1b8SBruce Evans 	 */
8528335c1b8SBruce Evans 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fdu) {
8538335c1b8SBruce Evans 		timeout(fd_turnoff, arg1, 4 * hz);
8548335c1b8SBruce Evans 		return;
8558335c1b8SBruce Evans 	}
8568335c1b8SBruce Evans 
857f5f7ba03SJordan K. Hubbard 	s = splbio();
8585b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
8593a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
860f5f7ba03SJordan K. Hubbard 	splx(s);
8615b81b6b3SRodney W. Grimes }
8625b81b6b3SRodney W. Grimes 
8633a2f7427SDavid Greenman static void
864d0917939SPaul Richards fd_motor_on(void *arg1)
8655b81b6b3SRodney W. Grimes {
866381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
867f5f7ba03SJordan K. Hubbard 	int	s;
868f5f7ba03SJordan K. Hubbard 
8695b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
870f5f7ba03SJordan K. Hubbard 	s = splbio();
8715b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
8725b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
8735b81b6b3SRodney W. Grimes 	{
874f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
8755b81b6b3SRodney W. Grimes 	}
876f5f7ba03SJordan K. Hubbard 	splx(s);
8775b81b6b3SRodney W. Grimes }
8785b81b6b3SRodney W. Grimes 
8793a2f7427SDavid Greenman static void
880dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
8815b81b6b3SRodney W. Grimes {
8825b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
8835b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
8845b81b6b3SRodney W. Grimes 	{
8853a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
8863a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
8875e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
8885b81b6b3SRodney W. Grimes 	}
8895b81b6b3SRodney W. Grimes }
8905b81b6b3SRodney W. Grimes 
891381fe1aaSGarrett Wollman static void
892dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
8935b81b6b3SRodney W. Grimes {
8943a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
8953a2f7427SDavid Greenman 
8963a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
8973a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8983a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8993a2f7427SDavid Greenman 	DELAY(100);
9003a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
9013a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
9023a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
9033a2f7427SDavid Greenman 	DELAY(100);
9043a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
9053a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
9063a2f7427SDavid Greenman 
907dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
908dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
909dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
910dc5df763SJoerg Wunsch 		     0);
9115b81b6b3SRodney W. Grimes }
9125b81b6b3SRodney W. Grimes 
9135b81b6b3SRodney W. Grimes /****************************************************************************/
9145b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
9155b81b6b3SRodney W. Grimes /****************************************************************************/
9165b81b6b3SRodney W. Grimes int
917dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
9185b81b6b3SRodney W. Grimes {
9195b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9205b81b6b3SRodney W. Grimes 	int i, j = 100000;
9213a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
9225b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
923dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9246a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
9255b81b6b3SRodney W. Grimes 	if (j <= 0)
92616b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
9275b81b6b3SRodney W. Grimes #ifdef	DEBUG
9283a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
9293a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
9305b81b6b3SRodney W. Grimes 	return(i);
9315b81b6b3SRodney W. Grimes #else
9323a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
9335b81b6b3SRodney W. Grimes #endif
9345b81b6b3SRodney W. Grimes }
9355b81b6b3SRodney W. Grimes 
936dc5df763SJoerg Wunsch /*
937dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
938dc5df763SJoerg Wunsch  */
939b5e8ce9fSBruce Evans static int
940dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
941dc5df763SJoerg Wunsch {
942dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
943dc5df763SJoerg Wunsch 	int i, j = 100000;
944dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
945dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
946dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9476a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
948dc5df763SJoerg Wunsch 	if (j <= 0)
94916b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0);
950dc5df763SJoerg Wunsch #ifdef	DEBUG
951dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
952dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
953dc5df763SJoerg Wunsch 	*ptr = i;
954dc5df763SJoerg Wunsch 	return 0;
955dc5df763SJoerg Wunsch #else
956dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
957dc5df763SJoerg Wunsch 	if (ptr)
958dc5df763SJoerg Wunsch 		*ptr = i;
959dc5df763SJoerg Wunsch 	return 0;
960dc5df763SJoerg Wunsch #endif
961dc5df763SJoerg Wunsch }
962dc5df763SJoerg Wunsch 
963dc5df763SJoerg Wunsch int
964dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
9655b81b6b3SRodney W. Grimes {
9665b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9673b3837dbSRodney W. Grimes 	int i;
9685b81b6b3SRodney W. Grimes 
9693b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
9703b3837dbSRodney W. Grimes 	i = 100000;
9713a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
9726a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
9733b3837dbSRodney W. Grimes 
9743b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
9753b3837dbSRodney W. Grimes 	i = 100000;
9763a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
97716b04b6aSJoerg Wunsch 	if (i <= 0)
97816b04b6aSJoerg Wunsch 		return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0);
9793b3837dbSRodney W. Grimes 
9803b3837dbSRodney W. Grimes 	/* Send the command and return */
9813a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
9823a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
9835b81b6b3SRodney W. Grimes 	return (0);
9845b81b6b3SRodney W. Grimes }
9855b81b6b3SRodney W. Grimes 
9865b81b6b3SRodney W. Grimes /****************************************************************************/
9875b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
9885b81b6b3SRodney W. Grimes /****************************************************************************/
989381fe1aaSGarrett Wollman int
990671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
9915b81b6b3SRodney W. Grimes {
9925b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
99320a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
994b99f0a4aSAndrew Moore 	fdc_p	fdc;
9955b81b6b3SRodney W. Grimes 
996b99f0a4aSAndrew Moore #if NFT > 0
997b99f0a4aSAndrew Moore 	/* check for a tape open */
998b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
999b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
1000b99f0a4aSAndrew Moore #endif
10015b81b6b3SRodney W. Grimes 	/* check bounds */
1002b99f0a4aSAndrew Moore 	if (fdu >= NFD)
1003b99f0a4aSAndrew Moore 		return(ENXIO);
1004b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
1005b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
1006b99f0a4aSAndrew Moore 		return(ENXIO);
1007b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1008b99f0a4aSAndrew Moore 		return(ENXIO);
10097ca0641bSAndrey A. Chernov 	if (type == 0)
10107ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
10117ca0641bSAndrey A. Chernov 	else {
10127ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
1013fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
10147ca0641bSAndrey A. Chernov 			case FD_360:
10157ca0641bSAndrey A. Chernov 				return(ENXIO);
1016ed2fa05eSAndrey A. Chernov 			case FD_720:
1017b39c878eSAndrey A. Chernov 				if (   type != FD_820
1018b39c878eSAndrey A. Chernov 				    && type != FD_800
1019ed2fa05eSAndrey A. Chernov 				   )
1020ed2fa05eSAndrey A. Chernov 					return(ENXIO);
1021ed2fa05eSAndrey A. Chernov 				break;
10227ca0641bSAndrey A. Chernov 			case FD_1200:
1023b39c878eSAndrey A. Chernov 				switch (type) {
1024b39c878eSAndrey A. Chernov 				case FD_1480:
1025b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1026fa4700b4SAndrey A. Chernov 					break;
10277ca0641bSAndrey A. Chernov 				case FD_1440:
1028b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1029b39c878eSAndrey A. Chernov 					break;
1030b39c878eSAndrey A. Chernov 				case FD_820:
1031b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1032b39c878eSAndrey A. Chernov 					break;
1033b39c878eSAndrey A. Chernov 				case FD_800:
1034b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1035b39c878eSAndrey A. Chernov 					break;
1036b39c878eSAndrey A. Chernov 				case FD_720:
1037b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1038b39c878eSAndrey A. Chernov 					break;
1039b39c878eSAndrey A. Chernov 				case FD_360:
1040b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1041b39c878eSAndrey A. Chernov 					break;
1042b39c878eSAndrey A. Chernov 				default:
1043b39c878eSAndrey A. Chernov 					return(ENXIO);
1044b39c878eSAndrey A. Chernov 				}
1045b39c878eSAndrey A. Chernov 				break;
1046b39c878eSAndrey A. Chernov 			case FD_1440:
1047b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1048b39c878eSAndrey A. Chernov 				    && type != FD_1480
1049ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1050b39c878eSAndrey A. Chernov 				    && type != FD_820
1051b39c878eSAndrey A. Chernov 				    && type != FD_800
1052b39c878eSAndrey A. Chernov 				    && type != FD_720
10537ca0641bSAndrey A. Chernov 				    )
1054dffff499SAndrey A. Chernov 					return(ENXIO);
1055fa4700b4SAndrey A. Chernov 				break;
10567ca0641bSAndrey A. Chernov 			}
10577ca0641bSAndrey A. Chernov 		}
1058fa4700b4SAndrey A. Chernov 	}
1059b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
10605b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
1061dc16046fSJoerg Wunsch 	kdc_fd[fdu].kdc_state = DC_BUSY;
10625b81b6b3SRodney W. Grimes 
10635b81b6b3SRodney W. Grimes 	return 0;
10645b81b6b3SRodney W. Grimes }
10655b81b6b3SRodney W. Grimes 
1066381fe1aaSGarrett Wollman int
1067671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
10685b81b6b3SRodney W. Grimes {
10695b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
1070b99f0a4aSAndrew Moore 
1071b99f0a4aSAndrew Moore #if NFT > 0
10723a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
10733a2f7427SDavid Greenman 
1074b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
10753a2f7427SDavid Greenman 		return ftclose(dev, flags);
1076b99f0a4aSAndrew Moore #endif
10775b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
10783a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
1079dc16046fSJoerg Wunsch 	kdc_fd[fdu].kdc_state = DC_IDLE;
1080dc16046fSJoerg Wunsch 
10815b81b6b3SRodney W. Grimes 	return(0);
10825b81b6b3SRodney W. Grimes }
10835b81b6b3SRodney W. Grimes 
10845b81b6b3SRodney W. Grimes 
10853a2f7427SDavid Greenman /****************************************************************************/
10863a2f7427SDavid Greenman /*                               fdstrategy                                 */
10873a2f7427SDavid Greenman /****************************************************************************/
10883a2f7427SDavid Greenman void
10893a2f7427SDavid Greenman fdstrategy(struct buf *bp)
10903a2f7427SDavid Greenman {
10913a2f7427SDavid Greenman 	register struct buf *dp;
10923a2f7427SDavid Greenman 	long nblocks, blknum;
10933a2f7427SDavid Greenman  	int	s;
10943a2f7427SDavid Greenman  	fdcu_t	fdcu;
10953a2f7427SDavid Greenman  	fdu_t	fdu;
10963a2f7427SDavid Greenman  	fdc_p	fdc;
10973a2f7427SDavid Greenman  	fd_p	fd;
10983a2f7427SDavid Greenman 	size_t	fdblk;
10993a2f7427SDavid Greenman 
11003a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
11013a2f7427SDavid Greenman 	fd = &fd_data[fdu];
11023a2f7427SDavid Greenman 	fdc = fd->fdc;
11033a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
11043a2f7427SDavid Greenman 
11053a2f7427SDavid Greenman #if NFT > 0
11063a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
11073a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
1108d3628763SRodney W. Grimes 		bp->b_error = ENODEV;
11093a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11103a2f7427SDavid Greenman 		goto bad;
11113a2f7427SDavid Greenman 	}
11123a2f7427SDavid Greenman 	/* check for controller already busy with tape */
11133a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
11143a2f7427SDavid Greenman 		bp->b_error = EBUSY;
11153a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
11163a2f7427SDavid Greenman 		goto bad;
11173a2f7427SDavid Greenman 	}
11183a2f7427SDavid Greenman #endif
1119d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
11203a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
11213a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1122dc5df763SJoerg Wunsch 			printf(
11236a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1124702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
11253a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11263a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11273a2f7427SDavid Greenman 			goto bad;
11283a2f7427SDavid Greenman 		}
11293a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
11303a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11313a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11323a2f7427SDavid Greenman 			goto bad;
11333a2f7427SDavid Greenman 		}
11343a2f7427SDavid Greenman 	}
11353a2f7427SDavid Greenman 
11363a2f7427SDavid Greenman 	/*
11373a2f7427SDavid Greenman 	 * Set up block calculations.
11383a2f7427SDavid Greenman 	 */
11393a2f7427SDavid Greenman 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk;
11403a2f7427SDavid Greenman  	nblocks = fd->ft->size;
11413a2f7427SDavid Greenman 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
11423a2f7427SDavid Greenman 		if (blknum == nblocks) {
11433a2f7427SDavid Greenman 			bp->b_resid = bp->b_bcount;
11443a2f7427SDavid Greenman 		} else {
11453a2f7427SDavid Greenman 			bp->b_error = ENOSPC;
11463a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11473a2f7427SDavid Greenman 		}
11483a2f7427SDavid Greenman 		goto bad;
11493a2f7427SDavid Greenman 	}
11503a2f7427SDavid Greenman  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
115127513ca7SBruce Evans  	bp->b_pblkno = bp->b_blkno;
11523a2f7427SDavid Greenman 	dp = &(fdc->head);
11533a2f7427SDavid Greenman 	s = splbio();
11543a2f7427SDavid Greenman 	disksort(dp, bp);
11555e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */
11563a2f7427SDavid Greenman 	fdstart(fdcu);
11573a2f7427SDavid Greenman 	splx(s);
11583a2f7427SDavid Greenman 	return;
11593a2f7427SDavid Greenman 
11603a2f7427SDavid Greenman bad:
11613a2f7427SDavid Greenman 	biodone(bp);
11623a2f7427SDavid Greenman }
11633a2f7427SDavid Greenman 
11645b81b6b3SRodney W. Grimes /***************************************************************\
11655b81b6b3SRodney W. Grimes *				fdstart				*
11665b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
11675b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
11685b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
11695b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
11705b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
11715b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
11725b81b6b3SRodney W. Grimes \***************************************************************/
1173381fe1aaSGarrett Wollman static void
1174dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
11755b81b6b3SRodney W. Grimes {
11765b81b6b3SRodney W. Grimes 	int s;
11775b81b6b3SRodney W. Grimes 
11785b81b6b3SRodney W. Grimes 	s = splbio();
11795b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
11805b81b6b3SRodney W. Grimes 	{
11815b81b6b3SRodney W. Grimes 		fdintr(fdcu);
11825b81b6b3SRodney W. Grimes 	}
11835b81b6b3SRodney W. Grimes 	splx(s);
11845b81b6b3SRodney W. Grimes }
11855b81b6b3SRodney W. Grimes 
1186381fe1aaSGarrett Wollman static void
1187d0917939SPaul Richards fd_timeout(void *arg1)
11885b81b6b3SRodney W. Grimes {
1189381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
11905b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
11913a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
11925b81b6b3SRodney W. Grimes 	struct buf *dp, *bp;
1193f5f7ba03SJordan K. Hubbard 	int s;
11945b81b6b3SRodney W. Grimes 
11955b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
11965b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
11975b81b6b3SRodney W. Grimes 
11983a2f7427SDavid Greenman 	/*
11993a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
12003a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
12013a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
12023a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
12033a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
12043a2f7427SDavid Greenman 	 */
12055b81b6b3SRodney W. Grimes 
12063a2f7427SDavid Greenman 	s = splbio();
12073a2f7427SDavid Greenman 
12083a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
12093a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
12103a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
12113a2f7427SDavid Greenman 	{
12123a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
12133a2f7427SDavid Greenman 		/* yup, it is; kill it now */
12143a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
12153a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
12163a2f7427SDavid Greenman 	}
12175b81b6b3SRodney W. Grimes 
12185b81b6b3SRodney W. Grimes 	if (bp)
12195b81b6b3SRodney W. Grimes 	{
12205b81b6b3SRodney W. Grimes 		retrier(fdcu);
12213a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
12225b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
12235b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
12245b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
12255b81b6b3SRodney W. Grimes 	}
12265b81b6b3SRodney W. Grimes 	else
12275b81b6b3SRodney W. Grimes 	{
12285b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
12295b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
12305b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
12315b81b6b3SRodney W. Grimes 	}
1232f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1233f5f7ba03SJordan K. Hubbard 	splx(s);
12345b81b6b3SRodney W. Grimes }
12355b81b6b3SRodney W. Grimes 
12365b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1237381fe1aaSGarrett Wollman static void
1238d0917939SPaul Richards fd_pseudointr(void *arg1)
12395b81b6b3SRodney W. Grimes {
1240381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
12415b81b6b3SRodney W. Grimes 	int	s;
12423a2f7427SDavid Greenman 
12435b81b6b3SRodney W. Grimes 	s = splbio();
12445b81b6b3SRodney W. Grimes 	fdintr(fdcu);
12455b81b6b3SRodney W. Grimes 	splx(s);
12465b81b6b3SRodney W. Grimes }
12475b81b6b3SRodney W. Grimes 
12485b81b6b3SRodney W. Grimes /***********************************************************************\
12495b81b6b3SRodney W. Grimes *                                 fdintr				*
12505b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
12515b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
12525b81b6b3SRodney W. Grimes \***********************************************************************/
1253381fe1aaSGarrett Wollman void
1254381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
12555b81b6b3SRodney W. Grimes {
12565b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1257b99f0a4aSAndrew Moore #if NFT > 0
1258b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1259b99f0a4aSAndrew Moore 
1260b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1261b99f0a4aSAndrew Moore 		(ftintr(fdu));
1262b99f0a4aSAndrew Moore 	else
1263b99f0a4aSAndrew Moore #endif
1264381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1265381fe1aaSGarrett Wollman 			;
12665b81b6b3SRodney W. Grimes }
12675b81b6b3SRodney W. Grimes 
12685b81b6b3SRodney W. Grimes /***********************************************************************\
12695b81b6b3SRodney W. Grimes * The controller state machine.						*
12705b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
12715b81b6b3SRodney W. Grimes \***********************************************************************/
12723a2f7427SDavid Greenman static int
1273dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
12745b81b6b3SRodney W. Grimes {
12754ccc87c5SPoul-Henning Kamp 	int read, format, head, sec = 0, sectrac, st0, cyl, st3;
12765b81b6b3SRodney W. Grimes 	unsigned long blknum;
12775b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
12785b81b6b3SRodney W. Grimes 	fd_p fd;
12795b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
1280b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
12813a2f7427SDavid Greenman 	size_t fdblk;
12825b81b6b3SRodney W. Grimes 
12835b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
12845b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
12855b81b6b3SRodney W. Grimes 	if(!bp)
12865b81b6b3SRodney W. Grimes 	{
12875b81b6b3SRodney W. Grimes 		/***********************************************\
12885b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
12895b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
12905b81b6b3SRodney W. Grimes 		\***********************************************/
12915b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
12925b81b6b3SRodney W. Grimes 		if(fdc->fd)
12935b81b6b3SRodney W. Grimes 		{
12946a0e6f42SRodney W. Grimes 			printf("fd%d: unexpected valid fd pointer\n",
12953a2f7427SDavid Greenman 			       fdc->fdu);
12965b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
12975b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
12985b81b6b3SRodney W. Grimes 		}
12995b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
13005b81b6b3SRodney W. Grimes  		return(0);
13015b81b6b3SRodney W. Grimes 	}
13025b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
13035b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
13043a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
13055b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
13065b81b6b3SRodney W. Grimes 	{
13076a0e6f42SRodney W. Grimes 		printf("fd%d: confused fd pointers\n", fdu);
13085b81b6b3SRodney W. Grimes 	}
13095b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1310b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1311b39c878eSAndrey A. Chernov 	if(format)
1312b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
13135b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
13145b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
13155b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
13165e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu);
13175e235068SJordan K. Hubbard 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
13185b81b6b3SRodney W. Grimes 	switch (fdc->state)
13195b81b6b3SRodney W. Grimes 	{
13205b81b6b3SRodney W. Grimes 	case DEVIDLE:
13215b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
13225b81b6b3SRodney W. Grimes 		fdc->retry = 0;
13235b81b6b3SRodney W. Grimes 		fd->skip = 0;
13245b81b6b3SRodney W. Grimes 		fdc->fd = fd;
13255b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
13263a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
13273a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
13285b81b6b3SRodney W. Grimes 		/*******************************************************\
13295b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
13305b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
13315b81b6b3SRodney W. Grimes 		\*******************************************************/
13325b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
13335b81b6b3SRodney W. Grimes 		{
13345b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13355b81b6b3SRodney W. Grimes 			return(0); /* come back later */
13365b81b6b3SRodney W. Grimes 		}
13375b81b6b3SRodney W. Grimes 		/*******************************************************\
13385b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
13395b81b6b3SRodney W. Grimes 		\*******************************************************/
13405b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
13415b81b6b3SRodney W. Grimes 		{
13425b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13435b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
13445b81b6b3SRodney W. Grimes 			return(0);
13455b81b6b3SRodney W. Grimes 		}
13465b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
13475b81b6b3SRodney W. Grimes 		{
13483a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
13495b81b6b3SRodney W. Grimes 		}
13505e235068SJordan K. Hubbard 		fdc->state = DOSEEK;
13515b81b6b3SRodney W. Grimes 		break;
13525b81b6b3SRodney W. Grimes 	case DOSEEK:
13535b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
13545b81b6b3SRodney W. Grimes 		{
13555b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
13565b81b6b3SRodney W. Grimes 			break;
13575b81b6b3SRodney W. Grimes 		}
1358dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1359dc5df763SJoerg Wunsch 			   fd->fdsu, bp->b_cylin * fd->ft->steptrac,
1360dc5df763SJoerg Wunsch 			   0))
1361dc8603e3SJoerg Wunsch 		{
1362dc8603e3SJoerg Wunsch 			/*
1363dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1364dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1365dc8603e3SJoerg Wunsch 			 */
1366dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1367dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1368dc8603e3SJoerg Wunsch 		}
1369dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
13705b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
13715b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13725b81b6b3SRodney W. Grimes 	case SEEKWAIT:
13735b81b6b3SRodney W. Grimes 		/* allow heads to settle */
137404b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
13755b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
13765b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13775b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
13785b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1379dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
13805b81b6b3SRodney W. Grimes 		{
13815b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
13823a2f7427SDavid Greenman 			do {
13833a2f7427SDavid Greenman 				/*
1384dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1385dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1386dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1387dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1388dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1389dc5df763SJoerg Wunsch 				 *
1390dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1391dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1392dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1393dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1394dc5df763SJoerg Wunsch 				 * real interrupt condition.
1395dc5df763SJoerg Wunsch 				 *
1396dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1397dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1398dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1399dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1400dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1401dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1402dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
14033a2f7427SDavid Greenman 				 */
1404dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1405dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1406dc5df763SJoerg Wunsch 					return 0;
1407dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1408dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1409dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
14103a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1411dc5df763SJoerg Wunsch 
14123a2f7427SDavid Greenman 			if (0 == descyl)
14133a2f7427SDavid Greenman 			{
1414dc5df763SJoerg Wunsch 				int failed = 0;
14153a2f7427SDavid Greenman 				/*
14163a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
14173a2f7427SDavid Greenman 				 * really there
14183a2f7427SDavid Greenman 				 */
1419dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1420dc5df763SJoerg Wunsch 					failed = 1;
14213a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
14223a2f7427SDavid Greenman 					printf(
14233a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
14243a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1425dc5df763SJoerg Wunsch 					failed = 1;
1426dc5df763SJoerg Wunsch 				}
1427dc5df763SJoerg Wunsch 
1428dc5df763SJoerg Wunsch 				if (failed)
1429dc5df763SJoerg Wunsch 				{
14303a2f7427SDavid Greenman 					if(fdc->retry < 3)
14313a2f7427SDavid Greenman 						fdc->retry = 3;
14323a2f7427SDavid Greenman 					return(retrier(fdcu));
14333a2f7427SDavid Greenman 				}
14343a2f7427SDavid Greenman 			}
1435dc5df763SJoerg Wunsch 
14365b81b6b3SRodney W. Grimes 			if (cyl != descyl)
14375b81b6b3SRodney W. Grimes 			{
14383a2f7427SDavid Greenman 				printf(
14393a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
14402d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
14415b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
14425b81b6b3SRodney W. Grimes 			}
14435b81b6b3SRodney W. Grimes 		}
14445b81b6b3SRodney W. Grimes 
14455b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
1446b39c878eSAndrey A. Chernov 		if(format)
1447b39c878eSAndrey A. Chernov 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1448b39c878eSAndrey A. Chernov 				- (char *)finfo;
14495b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
14503a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
14513a2f7427SDavid Greenman 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
14523a2f7427SDavid Greenman 			+ fd->skip/fdblk;
14535b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
14545b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
14555b81b6b3SRodney W. Grimes 		head = sec / sectrac;
14565b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
14573a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
14583a2f7427SDavid Greenman 
14593a2f7427SDavid Greenman 		if(format || !read)
14603a2f7427SDavid Greenman 		{
14613a2f7427SDavid Greenman 			/* make sure the drive is writable */
1462dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1463dc8603e3SJoerg Wunsch 			{
1464dc8603e3SJoerg Wunsch 				/* stuck controller? */
1465dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1466dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1467dc8603e3SJoerg Wunsch 			}
14683a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
14693a2f7427SDavid Greenman 			{
14703a2f7427SDavid Greenman 				/*
14713a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
14723a2f7427SDavid Greenman 				 * in order to force the current operation
14733a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
14743a2f7427SDavid Greenman 				 * error - all error handling is done
14753a2f7427SDavid Greenman 				 * by the retrier()
14763a2f7427SDavid Greenman 				 */
14773a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
14783a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
14793a2f7427SDavid Greenman 				fdc->status[2] = 0;
14803a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
14813a2f7427SDavid Greenman 				fdc->status[4] = head;
14823a2f7427SDavid Greenman 				fdc->status[5] = sec;
14833a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
14843a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
14853a2f7427SDavid Greenman 				return (1);
14863a2f7427SDavid Greenman 			}
14873a2f7427SDavid Greenman 		}
14885b81b6b3SRodney W. Grimes 
1489b39c878eSAndrey A. Chernov 		if(format)
1490b39c878eSAndrey A. Chernov 		{
1491b39c878eSAndrey A. Chernov 			/* formatting */
1492dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1493dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1494dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1495dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1496dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1497dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1498dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1499dc5df763SJoerg Wunsch 				  0))
1500dc8603e3SJoerg Wunsch 			{
1501dc8603e3SJoerg Wunsch 				/* controller fell over */
1502dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1503dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1504dc8603e3SJoerg Wunsch 			}
1505b39c878eSAndrey A. Chernov 		}
1506b39c878eSAndrey A. Chernov 		else
1507b39c878eSAndrey A. Chernov 		{
1508dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1509dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1510dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1511dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1512dc5df763SJoerg Wunsch 				   head,
1513dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1514dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1515dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1516dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1517dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1518dc5df763SJoerg Wunsch 				   0))
15195b81b6b3SRodney W. Grimes 			{
1520dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1521dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1522dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
15235b81b6b3SRodney W. Grimes 			}
1524b39c878eSAndrey A. Chernov 		}
15255b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
15265e235068SJordan K. Hubbard 		timeout(fd_timeout, (caddr_t)fdcu, hz);
15275b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15285b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
15295e235068SJordan K. Hubbard 		untimeout(fd_timeout, (caddr_t)fdcu);
1530dc5df763SJoerg Wunsch 
1531dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
15325b81b6b3SRodney W. Grimes 		{
1533dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1534dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1535dc5df763SJoerg Wunsch 			return retrier(fdcu);
15365b81b6b3SRodney W. Grimes   		}
1537dc5df763SJoerg Wunsch 
15383a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1539dc5df763SJoerg Wunsch 
15403a2f7427SDavid Greenman 		/* FALLTHROUGH */
1541dc5df763SJoerg Wunsch 
15423a2f7427SDavid Greenman 	case IOTIMEDOUT:
15435b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
15443a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
15453a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
15465b81b6b3SRodney W. Grimes 		{
15473a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15483a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1549b39c878eSAndrey A. Chernov                                 /*
15503a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
15513a2f7427SDavid Greenman 				 * and didn't release it in time for the
15523a2f7427SDavid Greenman 				 * next FDC transfer.
15533a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
15543a2f7427SDavid Greenman 				 * count. (vak)
1555b39c878eSAndrey A. Chernov                                  */
1556b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1557b39c878eSAndrey A. Chernov                                 return (1);
1558b39c878eSAndrey A. Chernov                         }
15593a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
15603a2f7427SDavid Greenman 				&& fdc->retry < 6)
15613a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
15623a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15633a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
15643a2f7427SDavid Greenman 				&& fdc->retry < 3)
15653a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
15665b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
15675b81b6b3SRodney W. Grimes 		}
15685b81b6b3SRodney W. Grimes 		/* All OK */
15693a2f7427SDavid Greenman 		fd->skip += fdblk;
1570b39c878eSAndrey A. Chernov 		if (!format && fd->skip < bp->b_bcount)
15715b81b6b3SRodney W. Grimes 		{
15725b81b6b3SRodney W. Grimes 			/* set up next transfer */
15733a2f7427SDavid Greenman 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
15743a2f7427SDavid Greenman 				+ fd->skip/fdblk;
15753a2f7427SDavid Greenman 			bp->b_cylin =
15763a2f7427SDavid Greenman 				(blknum / (fd->ft->sectrac * fd->ft->heads));
15775b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
15785b81b6b3SRodney W. Grimes 		}
15795b81b6b3SRodney W. Grimes 		else
15805b81b6b3SRodney W. Grimes 		{
15815b81b6b3SRodney W. Grimes 			/* ALL DONE */
15825b81b6b3SRodney W. Grimes 			fd->skip = 0;
15835b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
158426f9a767SRodney W. Grimes 			dp->b_actf = bp->b_actf;
15855b81b6b3SRodney W. Grimes 			biodone(bp);
15865b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
15875b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
15885b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
15895b81b6b3SRodney W. Grimes 		}
15905b81b6b3SRodney W. Grimes 		return(1);
15915b81b6b3SRodney W. Grimes 	case RESETCTLR:
15923a2f7427SDavid Greenman 		fdc_reset(fdc);
15935b81b6b3SRodney W. Grimes 		fdc->retry++;
15945b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
15955b81b6b3SRodney W. Grimes 		break;
15965b81b6b3SRodney W. Grimes 	case STARTRECAL:
1597dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1598dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1599dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1600dc8603e3SJoerg Wunsch 		{
1601dc8603e3SJoerg Wunsch 			/* arrgl */
1602dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1603dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1604dc8603e3SJoerg Wunsch 		}
16055b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
16065b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
16075b81b6b3SRodney W. Grimes 	case RECALWAIT:
16085b81b6b3SRodney W. Grimes 		/* allow heads to settle */
160904b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
16105b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
16115b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
16125b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
16133a2f7427SDavid Greenman 		do {
16143a2f7427SDavid Greenman 			/*
1615dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
16163a2f7427SDavid Greenman 			 */
1617dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1618dc5df763SJoerg Wunsch 				return 0;
1619dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1620dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1621dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
16223a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
16233a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
16245b81b6b3SRodney W. Grimes 		{
1625dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1626dc8603e3SJoerg Wunsch 				/*
1627dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1628dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1629dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1630dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1631dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1632dc8603e3SJoerg Wunsch 				 * time in a line
1633dc8603e3SJoerg Wunsch 				 */
1634dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1635dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
16363a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
16375b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
16385b81b6b3SRodney W. Grimes 		}
16395b81b6b3SRodney W. Grimes 		fd->track = 0;
16405b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
16415b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
16425b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16435b81b6b3SRodney W. Grimes 	case MOTORWAIT:
16445b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
16455b81b6b3SRodney W. Grimes 		{
16465b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
16475b81b6b3SRodney W. Grimes 		}
16485e235068SJordan K. Hubbard 		/*
16495e235068SJordan K. Hubbard 		 * since the controller was off, it has lost its
16505e235068SJordan K. Hubbard 		 * idea about the current track it were; thus,
16515e235068SJordan K. Hubbard 		 * recalibrate the bastard
16525e235068SJordan K. Hubbard 		 */
16535e235068SJordan K. Hubbard 		fdc->state = STARTRECAL;
16545b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16555b81b6b3SRodney W. Grimes 	default:
1656dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1657dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1658dac0f2dbSJoerg Wunsch 			printf("FDC status :%lx %lx %lx %lx %lx %lx %lx   ",
16595b81b6b3SRodney W. Grimes 			       fdc->status[0],
16605b81b6b3SRodney W. Grimes 			       fdc->status[1],
16615b81b6b3SRodney W. Grimes 			       fdc->status[2],
16625b81b6b3SRodney W. Grimes 			       fdc->status[3],
16635b81b6b3SRodney W. Grimes 			       fdc->status[4],
16645b81b6b3SRodney W. Grimes 			       fdc->status[5],
16655b81b6b3SRodney W. Grimes 			       fdc->status[6] );
16663a2f7427SDavid Greenman 		else
1667dac0f2dbSJoerg Wunsch 			printf("No status available   ");
1668dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1669dac0f2dbSJoerg Wunsch 		{
1670dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
16715b81b6b3SRodney W. Grimes 			return(0);
16725b81b6b3SRodney W. Grimes 		}
1673dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1674dac0f2dbSJoerg Wunsch 		return(0);
1675dac0f2dbSJoerg Wunsch 	}
1676dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
16775b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
16785b81b6b3SRodney W. Grimes }
16795b81b6b3SRodney W. Grimes 
1680aaf08d94SGarrett Wollman static int
1681f5f7ba03SJordan K. Hubbard retrier(fdcu)
1682f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
16835b81b6b3SRodney W. Grimes {
16845b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
16855b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
16865b81b6b3SRodney W. Grimes 
16875b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
16885b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
16895b81b6b3SRodney W. Grimes 
16903a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
16913a2f7427SDavid Greenman 		goto fail;
16925b81b6b3SRodney W. Grimes 	switch(fdc->retry)
16935b81b6b3SRodney W. Grimes 	{
16945b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
16955b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
16965b81b6b3SRodney W. Grimes 		break;
16975b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
16985b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
16995b81b6b3SRodney W. Grimes 		break;
17005b81b6b3SRodney W. Grimes 	case 6:
17015b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
17025b81b6b3SRodney W. Grimes 		break;
17035b81b6b3SRodney W. Grimes 	case 7:
17045b81b6b3SRodney W. Grimes 		break;
17055b81b6b3SRodney W. Grimes 	default:
17063a2f7427SDavid Greenman 	fail:
17075b81b6b3SRodney W. Grimes 		{
17087ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
17097ca0641bSAndrey A. Chernov 			/* Trick diskerr */
17103a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
17113a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
171292ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
17133a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
17143a2f7427SDavid Greenman 				(struct disklabel *)NULL);
17157ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
1716dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1717dc5df763SJoerg Wunsch 			{
1718dc5df763SJoerg Wunsch 				printf(
1719dc5df763SJoerg Wunsch 			" (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n",
1720dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1721dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1722dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1723dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1724dc5df763SJoerg Wunsch 				       fdc->status[5]);
1725dc5df763SJoerg Wunsch 			}
1726dc5df763SJoerg Wunsch 			else
1727dc5df763SJoerg Wunsch 				printf(" (No status)\n");
17285b81b6b3SRodney W. Grimes 		}
17295b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
17305b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
17315b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
173226f9a767SRodney W. Grimes 		dp->b_actf = bp->b_actf;
17335b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
17345b81b6b3SRodney W. Grimes 		biodone(bp);
173592ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
17365b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
17375b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1738f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
173992ed385aSRodney W. Grimes 		return(1);
17405b81b6b3SRodney W. Grimes 	}
17415b81b6b3SRodney W. Grimes 	fdc->retry++;
17425b81b6b3SRodney W. Grimes 	return(1);
17435b81b6b3SRodney W. Grimes }
17445b81b6b3SRodney W. Grimes 
1745b39c878eSAndrey A. Chernov static int
1746b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1747b39c878eSAndrey A. Chernov 	dev_t dev;
1748b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1749b39c878eSAndrey A. Chernov 	struct proc *p;
1750b39c878eSAndrey A. Chernov {
1751b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1752b39c878eSAndrey A. Chernov  	fd_p	fd;
1753b39c878eSAndrey A. Chernov 
1754b39c878eSAndrey A. Chernov 	struct buf *bp;
1755b39c878eSAndrey A. Chernov 	int rv = 0, s;
17563a2f7427SDavid Greenman 	size_t fdblk;
1757b39c878eSAndrey A. Chernov 
1758b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1759b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
17603a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1761b39c878eSAndrey A. Chernov 
1762b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1763b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1764b39c878eSAndrey A. Chernov 	if(bp == 0)
1765b39c878eSAndrey A. Chernov 		return ENOBUFS;
176682f5379bSJoerg Wunsch 	/*
176782f5379bSJoerg Wunsch 	 * keep the process from being swapped
176882f5379bSJoerg Wunsch 	 */
176982f5379bSJoerg Wunsch 	p->p_flag |= P_PHYSIO;
1770b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1771b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1772b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1773b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1774b39c878eSAndrey A. Chernov 
1775b39c878eSAndrey A. Chernov 	/*
1776b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1777b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1778b39c878eSAndrey A. Chernov 	 */
1779b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
17803a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1781b39c878eSAndrey A. Chernov 
1782b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
17835e235068SJordan K. Hubbard 	bp->b_un.b_addr = (caddr_t)finfo;
1784b39c878eSAndrey A. Chernov 
1785b39c878eSAndrey A. Chernov 	/* now do the format */
1786b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1787b39c878eSAndrey A. Chernov 
1788b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1789b39c878eSAndrey A. Chernov 	s = splbio();
1790b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1791b39c878eSAndrey A. Chernov 	{
17925e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1793b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1794b39c878eSAndrey A. Chernov 			break;
1795b39c878eSAndrey A. Chernov 	}
1796b39c878eSAndrey A. Chernov 	splx(s);
1797b39c878eSAndrey A. Chernov 
179882f5379bSJoerg Wunsch 	if(rv == EWOULDBLOCK) {
1799b39c878eSAndrey A. Chernov 		/* timed out */
1800b39c878eSAndrey A. Chernov 		rv = EIO;
180182f5379bSJoerg Wunsch 		biodone(bp);
180282f5379bSJoerg Wunsch 	}
18033a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
18043a2f7427SDavid Greenman 		rv = bp->b_error;
180582f5379bSJoerg Wunsch 	/*
180682f5379bSJoerg Wunsch 	 * allow the process to be swapped
180782f5379bSJoerg Wunsch 	 */
180882f5379bSJoerg Wunsch 	p->p_flag &= ~P_PHYSIO;
1809b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1810b39c878eSAndrey A. Chernov 	return rv;
1811b39c878eSAndrey A. Chernov }
1812b39c878eSAndrey A. Chernov 
1813f5f7ba03SJordan K. Hubbard /*
1814671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
1815f5f7ba03SJordan K. Hubbard  */
18165b81b6b3SRodney W. Grimes 
1817f5f7ba03SJordan K. Hubbard int
1818b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1819f5f7ba03SJordan K. Hubbard 	dev_t dev;
1820f5f7ba03SJordan K. Hubbard 	int cmd;
1821f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1822f5f7ba03SJordan K. Hubbard 	int flag;
1823b39c878eSAndrey A. Chernov 	struct proc *p;
1824f5f7ba03SJordan K. Hubbard {
18253a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
18263a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
18273a2f7427SDavid Greenman 	size_t fdblk;
18283a2f7427SDavid Greenman 
1829f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1830f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1831f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
18323a2f7427SDavid Greenman 	int error = 0;
1833f5f7ba03SJordan K. Hubbard 
1834b99f0a4aSAndrew Moore #if NFT > 0
1835a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1836a60eff27SNate Williams 
1837a60eff27SNate Williams 	/* check for a tape ioctl */
1838a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1839b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1840b99f0a4aSAndrew Moore #endif
1841b99f0a4aSAndrew Moore 
18423a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1843f5f7ba03SJordan K. Hubbard 
1844f5f7ba03SJordan K. Hubbard 	switch (cmd)
1845f5f7ba03SJordan K. Hubbard 	{
1846f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1847f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1848f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
18493a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
185092ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1851f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1852f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1853f5f7ba03SJordan K. Hubbard 
1854191e1a59SBruce Evans 		if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl)
1855191e1a59SBruce Evans 		    == NULL)
1856f5f7ba03SJordan K. Hubbard 			error = 0;
1857f5f7ba03SJordan K. Hubbard 		else
1858f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1859f5f7ba03SJordan K. Hubbard 
1860f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1861f5f7ba03SJordan K. Hubbard 		break;
1862f5f7ba03SJordan K. Hubbard 
1863f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1864f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1865f5f7ba03SJordan K. Hubbard 			error = EBADF;
1866f5f7ba03SJordan K. Hubbard 		break;
1867f5f7ba03SJordan K. Hubbard 
1868f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1869f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1870f5f7ba03SJordan K. Hubbard 			error = EBADF;
1871f5f7ba03SJordan K. Hubbard 		break;
1872f5f7ba03SJordan K. Hubbard 
1873f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1874f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1875f5f7ba03SJordan K. Hubbard 		{
1876f5f7ba03SJordan K. Hubbard 			error = EBADF;
1877f5f7ba03SJordan K. Hubbard 			break;
1878f5f7ba03SJordan K. Hubbard 		}
1879f5f7ba03SJordan K. Hubbard 
1880f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1881f5f7ba03SJordan K. Hubbard 
1882191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
1883191e1a59SBruce Evans 					  (u_long)0)) != 0)
1884f5f7ba03SJordan K. Hubbard 			break;
1885f5f7ba03SJordan K. Hubbard 
1886b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
188754c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
1888b39c878eSAndrey A. Chernov 		break;
1889b39c878eSAndrey A. Chernov 
1890b39c878eSAndrey A. Chernov 	case FD_FORM:
1891b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1892b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1893b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1894b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1895b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1896b39c878eSAndrey A. Chernov 		else
1897b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1898b39c878eSAndrey A. Chernov 		break;
1899b39c878eSAndrey A. Chernov 
1900b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1901b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1902f5f7ba03SJordan K. Hubbard 		break;
1903f5f7ba03SJordan K. Hubbard 
19043a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
19053a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
19063a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
19073a2f7427SDavid Greenman 			return EPERM;
19083a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
19093a2f7427SDavid Greenman 		break;
19103a2f7427SDavid Greenman 
19113a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
19123a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
19133a2f7427SDavid Greenman 		break;
19143a2f7427SDavid Greenman 
19153a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
19163a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
19173a2f7427SDavid Greenman 		break;
19183a2f7427SDavid Greenman 
1919f5f7ba03SJordan K. Hubbard 	default:
19203a2f7427SDavid Greenman 		error = ENOTTY;
1921f5f7ba03SJordan K. Hubbard 		break;
1922f5f7ba03SJordan K. Hubbard 	}
1923f5f7ba03SJordan K. Hubbard 	return (error);
1924f5f7ba03SJordan K. Hubbard }
1925f5f7ba03SJordan K. Hubbard 
19267146c13eSJulian Elischer 
19277146c13eSJulian Elischer static fd_devsw_installed = 0;
19287146c13eSJulian Elischer 
192987f6c662SJulian Elischer static void 	fd_drvinit(void *notused )
19307146c13eSJulian Elischer {
193187f6c662SJulian Elischer 	dev_t dev;
193287f6c662SJulian Elischer 
19337146c13eSJulian Elischer 	if( ! fd_devsw_installed ) {
193487f6c662SJulian Elischer 		dev = makedev(CDEV_MAJOR, 0);
193587f6c662SJulian Elischer 		cdevsw_add(&dev,&fd_cdevsw, NULL);
193687f6c662SJulian Elischer 		dev = makedev(BDEV_MAJOR, 0);
193787f6c662SJulian Elischer 		bdevsw_add(&dev,&fd_bdevsw, NULL);
19387146c13eSJulian Elischer 		fd_devsw_installed = 1;
19397146c13eSJulian Elischer 	}
19407146c13eSJulian Elischer }
194187f6c662SJulian Elischer 
194287f6c662SJulian Elischer SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL)
194387f6c662SJulian Elischer 
1944f5f7ba03SJordan K. Hubbard #endif
19453a2f7427SDavid Greenman /*
19463a2f7427SDavid Greenman  * Hello emacs, these are the
19473a2f7427SDavid Greenman  * Local Variables:
19483a2f7427SDavid Greenman  *  c-indent-level:               8
19493a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
19503a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
19513a2f7427SDavid Greenman  *  c-brace-offset:              -8
19523a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
19533a2f7427SDavid Greenman  *  c-argdecl-indent:             8
19543a2f7427SDavid Greenman  *  c-label-offset:              -8
19553a2f7427SDavid Greenman  *  c++-hanging-braces:           1
19563a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
19573a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
19583a2f7427SDavid Greenman  *  c++-friend-offset:            0
19593a2f7427SDavid Greenman  * End:
19603a2f7427SDavid Greenman  */
1961