xref: /freebsd/sys/dev/fdc/fdc.c (revision 85827d9c1321f6d6847d3fece628915b2ef111c3)
15b81b6b3SRodney W. Grimes /*-
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
4685827d9cSJoerg Wunsch  *	$Id: fd.c,v 1.58 1995/04/20 03:17:44 julian 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>
68b99f0a4aSAndrew Moore #include <sys/buf.h>
69b99f0a4aSAndrew Moore #include <sys/uio.h>
70b99f0a4aSAndrew Moore #include <sys/malloc.h>
713a2f7427SDavid Greenman #include <sys/proc.h>
72b99f0a4aSAndrew Moore #include <sys/syslog.h>
7392200632SGarrett Wollman #include <sys/devconf.h>
7492200632SGarrett Wollman #include <sys/dkstat.h>
75f540b106SGarrett Wollman #include <i386/isa/isa.h>
76f540b106SGarrett Wollman #include <i386/isa/isa_device.h>
77f540b106SGarrett Wollman #include <i386/isa/fdreg.h>
78f540b106SGarrett Wollman #include <i386/isa/fdc.h>
79f540b106SGarrett Wollman #include <i386/isa/rtc.h>
80dc5df763SJoerg Wunsch #include <machine/stdarg.h>
8187eafbcaSPoul-Henning Kamp #if NFT > 0
8287eafbcaSPoul-Henning Kamp #include <sys/ftape.h>
8387eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h>
8487eafbcaSPoul-Henning Kamp #endif
855b81b6b3SRodney W. Grimes 
8692200632SGarrett Wollman static int fd_goaway(struct kern_devconf *, int);
8792200632SGarrett Wollman static int fdc_goaway(struct kern_devconf *, int);
88dc16046fSJoerg Wunsch static int
89dc16046fSJoerg Wunsch fd_externalize(struct proc *, struct kern_devconf *, void *, size_t);
905e235068SJordan K. Hubbard 
915e235068SJordan K. Hubbard /*
925e235068SJordan K. Hubbard  * Templates for the kern_devconf structures used when we attach.
935e235068SJordan K. Hubbard  */
945e235068SJordan K. Hubbard static struct kern_devconf kdc_fd[NFD] = { {
955e235068SJordan K. Hubbard 	0, 0, 0,		/* filled in by kern_devconf.c */
965e235068SJordan K. Hubbard 	"fd", 0, { MDDT_DISK, 0 },
975e235068SJordan K. Hubbard 	fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN,
985e235068SJordan K. Hubbard 	0,			/* parent */
995e235068SJordan K. Hubbard 	0,			/* parentdata */
100dc16046fSJoerg Wunsch 	DC_UNCONFIGURED,	/* state */
1016c0081e9SGarrett Wollman 	"floppy disk",
1026c0081e9SGarrett Wollman 	DC_CLS_DISK		/* class */
1035e235068SJordan K. Hubbard } };
1045e235068SJordan K. Hubbard 
1055e235068SJordan K. Hubbard struct kern_devconf kdc_fdc[NFDC] = { {
1065e235068SJordan K. Hubbard 	0, 0, 0,		/* filled in by kern_devconf.c */
1075e235068SJordan K. Hubbard 	"fdc", 0, { MDDT_ISA, 0, "bio" },
1085e235068SJordan K. Hubbard 	isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN,
1095e235068SJordan K. Hubbard 	0,			/* parent */
1105e235068SJordan K. Hubbard 	0,			/* parentdata */
111dc16046fSJoerg Wunsch 	DC_UNCONFIGURED,	/* state */
1126c0081e9SGarrett Wollman 	"floppy disk/tape controller",
1136c0081e9SGarrett Wollman 	DC_CLS_MISC		/* class */
1145e235068SJordan K. Hubbard } };
1155e235068SJordan K. Hubbard 
1165e235068SJordan K. Hubbard static inline void
1175e235068SJordan K. Hubbard fd_registerdev(int ctlr, int unit)
1185e235068SJordan K. Hubbard {
1195e235068SJordan K. Hubbard 	if(unit != 0)
1205e235068SJordan K. Hubbard 		kdc_fd[unit] = kdc_fd[0];
1215e235068SJordan K. Hubbard 
1225e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_unit = unit;
1235e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_parent = &kdc_fdc[ctlr];
1245e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_parentdata = 0;
1255e235068SJordan K. Hubbard 	dev_attach(&kdc_fd[unit]);
1265e235068SJordan K. Hubbard }
1275e235068SJordan K. Hubbard 
1285e235068SJordan K. Hubbard static inline void
1295e235068SJordan K. Hubbard fdc_registerdev(struct isa_device *dvp)
1305e235068SJordan K. Hubbard {
1315e235068SJordan K. Hubbard 	int unit = dvp->id_unit;
1325e235068SJordan K. Hubbard 
1335e235068SJordan K. Hubbard 	if(unit != 0)
1345e235068SJordan K. Hubbard 		kdc_fdc[unit] = kdc_fdc[0];
1355e235068SJordan K. Hubbard 
1365e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_unit = unit;
1375e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_parent = &kdc_isa0;
1385e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_parentdata = dvp;
1395e235068SJordan K. Hubbard 	dev_attach(&kdc_fdc[unit]);
1405e235068SJordan K. Hubbard }
1415e235068SJordan K. Hubbard 
1425e235068SJordan K. Hubbard static int
1435e235068SJordan K. Hubbard fdc_goaway(struct kern_devconf *kdc, int force)
1445e235068SJordan K. Hubbard {
1455e235068SJordan K. Hubbard 	if(force) {
1465e235068SJordan K. Hubbard 		dev_detach(kdc);
1475e235068SJordan K. Hubbard 		return 0;
1485e235068SJordan K. Hubbard 	} else {
1495e235068SJordan K. Hubbard 		return EBUSY;	/* XXX fix */
1505e235068SJordan K. Hubbard 	}
1515e235068SJordan K. Hubbard }
1525e235068SJordan K. Hubbard 
1535e235068SJordan K. Hubbard static int
1545e235068SJordan K. Hubbard fd_goaway(struct kern_devconf *kdc, int force)
1555e235068SJordan K. Hubbard {
1565e235068SJordan K. Hubbard 	dev_detach(kdc);
1575e235068SJordan K. Hubbard 	return 0;
1585e235068SJordan K. Hubbard }
15992200632SGarrett Wollman 
160671e2ceeSBruce Evans #define	b_cylin	b_resid		/* XXX now spelled b_cylinder elsewhere */
1615b81b6b3SRodney W. Grimes 
162b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
163b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
1645b81b6b3SRodney W. Grimes 
1653a2f7427SDavid Greenman /*
1663a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
1673a2f7427SDavid Greenman  * on the controller
1683a2f7427SDavid Greenman  */
1693a2f7427SDavid Greenman #define id_physid id_scsiid
1703a2f7427SDavid Greenman 
171dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
172dc5df763SJoerg Wunsch #define FD_FAILED -1
173dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
174dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
175dc5df763SJoerg Wunsch 
176b39c878eSAndrey A. Chernov #define NUMTYPES 14
177b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1787ca0641bSAndrey A. Chernov 
1793a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
180b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
181b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
182b99f0a4aSAndrew Moore #define FD_1720         1
183b99f0a4aSAndrew Moore #define FD_1480         2
184b99f0a4aSAndrew Moore #define FD_1440         3
185b99f0a4aSAndrew Moore #define FD_1200         4
186b99f0a4aSAndrew Moore #define FD_820          5
187b99f0a4aSAndrew Moore #define FD_800          6
188b99f0a4aSAndrew Moore #define FD_720          7
189b99f0a4aSAndrew Moore #define FD_360          8
190ed2fa05eSAndrey A. Chernov 
191b99f0a4aSAndrew Moore #define FD_1480in5_25   9
192b99f0a4aSAndrew Moore #define FD_1440in5_25   10
193b99f0a4aSAndrew Moore #define FD_820in5_25    11
194b99f0a4aSAndrew Moore #define FD_800in5_25    12
195b99f0a4aSAndrew Moore #define FD_720in5_25    13
196b99f0a4aSAndrew Moore #define FD_360in5_25    14
197b99f0a4aSAndrew Moore 
1987ca0641bSAndrey A. Chernov 
1995b81b6b3SRodney W. Grimes struct fd_type fd_types[NUMTYPES] =
2005b81b6b3SRodney W. Grimes {
201126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
202126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
203126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
204126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
205126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
206126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
207126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
208b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
209ed2fa05eSAndrey A. Chernov 
210126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
211126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
212126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
213126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
214126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
215126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
2165b81b6b3SRodney W. Grimes };
2175b81b6b3SRodney W. Grimes 
218b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
219dc16046fSJoerg Wunsch 
2205b81b6b3SRodney W. Grimes /***********************************************************************\
2215b81b6b3SRodney W. Grimes * Per controller structure.						*
2225b81b6b3SRodney W. Grimes \***********************************************************************/
223b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
2245b81b6b3SRodney W. Grimes 
2255b81b6b3SRodney W. Grimes /***********************************************************************\
2265b81b6b3SRodney W. Grimes * Per drive structure.							*
227b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
2285b81b6b3SRodney W. Grimes \***********************************************************************/
2295b81b6b3SRodney W. Grimes struct fd_data {
230b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2315b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2323a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
2335b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
2345b81b6b3SRodney W. Grimes 	int	flags;
2355b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
2365b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
2375b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
2385b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
2395b81b6b3SRodney W. Grimes 	int	skip;
2405b81b6b3SRodney W. Grimes 	int	hddrv;
241dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
2425b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
2433a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
24492200632SGarrett Wollman 	int	dkunit;		/* disk stats unit number */
2455b81b6b3SRodney W. Grimes } fd_data[NFD];
2465b81b6b3SRodney W. Grimes 
2475b81b6b3SRodney W. Grimes /***********************************************************************\
2485b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
2495b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
2505b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
2515b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
2525b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
2535b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
2545b81b6b3SRodney W. Grimes \***********************************************************************/
255b99f0a4aSAndrew Moore 
2563a2f7427SDavid Greenman #if NFT > 0
2573a2f7427SDavid Greenman int ftopen(dev_t, int);
2583a2f7427SDavid Greenman int ftintr(ftu_t ftu);
2593a2f7427SDavid Greenman int ftclose(dev_t, int);
2603a2f7427SDavid Greenman void ftstrategy(struct buf *);
2613a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
2623a2f7427SDavid Greenman int ftdump(dev_t);
2633a2f7427SDavid Greenman int ftsize(dev_t);
26474fa89f4SRodney W. Grimes int ftattach(struct isa_device *, struct isa_device *, int);
2653a2f7427SDavid Greenman #endif
2665b81b6b3SRodney W. Grimes 
2673a2f7427SDavid Greenman /* autoconfig functions */
2683a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2693a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2703a2f7427SDavid Greenman 
2713a2f7427SDavid Greenman /* exported functions */
2723a2f7427SDavid Greenman int fdsize (dev_t);
2733a2f7427SDavid Greenman void fdintr(fdcu_t);
274671e2ceeSBruce Evans int Fdopen(dev_t, int, int, struct proc *);
275671e2ceeSBruce Evans int fdclose(dev_t, int, int, struct proc *);
2763a2f7427SDavid Greenman void fdstrategy(struct buf *);
2773a2f7427SDavid Greenman int fdioctl(dev_t, int, caddr_t, int, struct proc *);
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 
34592200632SGarrett Wollman struct isa_device *fdcdevs[NFDC];
34692200632SGarrett Wollman 
34792200632SGarrett Wollman /*
34892200632SGarrett Wollman  * Provide hw.devconf information.
34992200632SGarrett Wollman  */
35092200632SGarrett Wollman static int
351dc16046fSJoerg Wunsch fd_externalize(struct proc *p, struct kern_devconf *kdc,
352dc16046fSJoerg Wunsch 	       void *userp, size_t len)
35392200632SGarrett Wollman {
35492200632SGarrett Wollman 	return disk_externalize(fd_data[kdc->kdc_unit].fdsu, userp, &len);
35592200632SGarrett Wollman }
35692200632SGarrett Wollman 
35792200632SGarrett Wollman static int
358dc16046fSJoerg Wunsch fdc_externalize(struct proc *p, struct kern_devconf *kdc,
359dc16046fSJoerg Wunsch 		void *userp, size_t len)
36092200632SGarrett Wollman {
36192200632SGarrett Wollman 	return isa_externalize(fdcdevs[kdc->kdc_unit], userp, &len);
36292200632SGarrett Wollman }
36392200632SGarrett Wollman 
364dc5df763SJoerg Wunsch static int
365dc5df763SJoerg Wunsch fdc_err(fdcu_t fdcu, const char *s)
366dc5df763SJoerg Wunsch {
367dc5df763SJoerg Wunsch 	fdc_data[fdcu].fdc_errs++;
368dc5df763SJoerg Wunsch 	if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX)
3696a0e6f42SRodney W. Grimes 		printf("fdc%d: %s", fdcu, s);
370dc5df763SJoerg Wunsch 	else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX)
371dc5df763SJoerg Wunsch 		printf("fdc%d: too many errors, not logging any more\n",
372dc5df763SJoerg Wunsch 		       fdcu);
373dc5df763SJoerg Wunsch 
374dc5df763SJoerg Wunsch 	return FD_FAILED;
375dc5df763SJoerg Wunsch }
376dc5df763SJoerg Wunsch 
377dc5df763SJoerg Wunsch /*
378dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
379dc5df763SJoerg Wunsch  * Unit number,
380dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
381dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
382dc5df763SJoerg Wunsch  */
383dc5df763SJoerg Wunsch 
384dc5df763SJoerg Wunsch int
385dc5df763SJoerg Wunsch fd_cmd(fdcu_t fdcu, int n_out, ...)
386dc5df763SJoerg Wunsch {
387dc5df763SJoerg Wunsch 	u_char cmd;
388dc5df763SJoerg Wunsch 	int n_in;
389dc5df763SJoerg Wunsch 	int n;
390dc5df763SJoerg Wunsch 	va_list ap;
391dc5df763SJoerg Wunsch 
392dc5df763SJoerg Wunsch 	va_start(ap, n_out);
393dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
394dc5df763SJoerg Wunsch 	va_end(ap);
395dc5df763SJoerg Wunsch 	va_start(ap, n_out);
396dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
397dc5df763SJoerg Wunsch 	{
398dc5df763SJoerg Wunsch 		if (out_fdc(fdcu, va_arg(ap, int)) < 0)
399dc5df763SJoerg Wunsch 		{
400dc5df763SJoerg Wunsch 			char msg[50];
401dc5df763SJoerg Wunsch 			sprintf(msg,
402dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
403dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
404dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
405dc5df763SJoerg Wunsch 		}
406dc5df763SJoerg Wunsch 	}
407dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
408dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
409dc5df763SJoerg Wunsch 	{
410dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
411dc5df763SJoerg Wunsch 		if (fd_in(fdcu, ptr) < 0)
412dc5df763SJoerg Wunsch 		{
413dc5df763SJoerg Wunsch 			char msg[50];
414dc5df763SJoerg Wunsch 			sprintf(msg,
415dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
416dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
417dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
418dc5df763SJoerg Wunsch 		}
419dc5df763SJoerg Wunsch 	}
420dc5df763SJoerg Wunsch 
421dc5df763SJoerg Wunsch 	return 0;
422dc5df763SJoerg Wunsch }
423dc5df763SJoerg Wunsch 
424dc5df763SJoerg Wunsch int
425dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
426dc5df763SJoerg Wunsch {
427dc5df763SJoerg Wunsch 	int st3;
428dc5df763SJoerg Wunsch 
429dc5df763SJoerg Wunsch 	if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
430dc5df763SJoerg Wunsch 	{
4316a0e6f42SRodney W. Grimes 		return fdc_err(fdc->fdcu, "Sense Drive Status failed\n");
432dc5df763SJoerg Wunsch 	}
433dc5df763SJoerg Wunsch 	if (st3p)
434dc5df763SJoerg Wunsch 		*st3p = st3;
435dc5df763SJoerg Wunsch 
436dc5df763SJoerg Wunsch 	return 0;
437dc5df763SJoerg Wunsch }
438dc5df763SJoerg Wunsch 
439dc5df763SJoerg Wunsch int
440dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
441dc5df763SJoerg Wunsch {
442dc5df763SJoerg Wunsch 	int st0, cyl;
443dc5df763SJoerg Wunsch 
444dc5df763SJoerg Wunsch 	int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0);
445dc5df763SJoerg Wunsch 
446dc5df763SJoerg Wunsch 	if (ret)
447dc5df763SJoerg Wunsch 	{
448dc5df763SJoerg Wunsch 		(void)fdc_err(fdc->fdcu,
449dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
450dc5df763SJoerg Wunsch 		return ret;
451dc5df763SJoerg Wunsch 	}
452dc5df763SJoerg Wunsch 
453dc5df763SJoerg Wunsch 	if (st0p)
454dc5df763SJoerg Wunsch 		*st0p = st0;
455dc5df763SJoerg Wunsch 
456dc5df763SJoerg Wunsch 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV)
457dc5df763SJoerg Wunsch 	{
458dc5df763SJoerg Wunsch 		/*
459dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
460dc5df763SJoerg Wunsch 		 */
461dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
462dc5df763SJoerg Wunsch 	}
463dc5df763SJoerg Wunsch 
464dc5df763SJoerg Wunsch 	if (fd_in(fdc->fdcu, &cyl) < 0)
465dc5df763SJoerg Wunsch 	{
466dc5df763SJoerg Wunsch 		return fdc_err(fdc->fdcu, "can't get cyl num\n");
467dc5df763SJoerg Wunsch 	}
468dc5df763SJoerg Wunsch 
469dc5df763SJoerg Wunsch 	if (cylp)
470dc5df763SJoerg Wunsch 		*cylp = cyl;
471dc5df763SJoerg Wunsch 
472dc5df763SJoerg Wunsch 	return 0;
473dc5df763SJoerg Wunsch }
474dc5df763SJoerg Wunsch 
475dc5df763SJoerg Wunsch 
476dc5df763SJoerg Wunsch int
477dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
478dc5df763SJoerg Wunsch {
479dc5df763SJoerg Wunsch 	int i, ret;
480b5e8ce9fSBruce Evans 
481dc5df763SJoerg Wunsch 	for (i = 0; i < 7; i++)
482dc5df763SJoerg Wunsch 	{
483b5e8ce9fSBruce Evans 		/*
484b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
485b5e8ce9fSBruce Evans 		 * from the hardware, but fdc_status wants u_longs and
486b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
487b5e8ce9fSBruce Evans 		 */
488b5e8ce9fSBruce Evans 		int status;
489b5e8ce9fSBruce Evans 
490b5e8ce9fSBruce Evans 		ret = fd_in(fdc->fdcu, &status);
491b5e8ce9fSBruce Evans 		fdc->status[i] = status;
492b5e8ce9fSBruce Evans 		if (ret != 0)
493dc5df763SJoerg Wunsch 			break;
494dc5df763SJoerg Wunsch 	}
495dc5df763SJoerg Wunsch 
496dc5df763SJoerg Wunsch 	if (ret == 0)
497dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
498dc5df763SJoerg Wunsch 	else
499dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
500dc5df763SJoerg Wunsch 
501dc5df763SJoerg Wunsch 	return ret;
502dc5df763SJoerg Wunsch }
503dc5df763SJoerg Wunsch 
5045b81b6b3SRodney W. Grimes /****************************************************************************/
5055b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
5065b81b6b3SRodney W. Grimes /****************************************************************************/
507dc5df763SJoerg Wunsch 
5085b81b6b3SRodney W. Grimes /*
5095b81b6b3SRodney W. Grimes  * probe for existance of controller
5105b81b6b3SRodney W. Grimes  */
5113a2f7427SDavid Greenman static int
512dc5df763SJoerg Wunsch fdprobe(struct isa_device *dev)
5135b81b6b3SRodney W. Grimes {
5145b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5155b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
5165b81b6b3SRodney W. Grimes 	{
5176a0e6f42SRodney W. Grimes 		printf("fdc%d: unit used multiple times\n", fdcu);
5185b81b6b3SRodney W. Grimes 		return 0;
5195b81b6b3SRodney W. Grimes 	}
5205b81b6b3SRodney W. Grimes 
52192200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
5225b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
5235b81b6b3SRodney W. Grimes 
5246c0081e9SGarrett Wollman #ifndef DEV_LKM
5256c0081e9SGarrett Wollman 	fdc_registerdev(dev);
5266c0081e9SGarrett Wollman #endif
5276c0081e9SGarrett Wollman 
52816111cedSAndrew Moore 	/* First - lets reset the floppy controller */
5293a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
53016111cedSAndrew Moore 	DELAY(100);
5313a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
53216111cedSAndrew Moore 
5335b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
534dc5df763SJoerg Wunsch 	if (fd_cmd(fdcu,
535dc5df763SJoerg Wunsch 		   3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
536dc5df763SJoerg Wunsch 		   0))
5375b81b6b3SRodney W. Grimes 	{
5385b81b6b3SRodney W. Grimes 		return(0);
5395b81b6b3SRodney W. Grimes 	}
540dc16046fSJoerg Wunsch 	kdc_fdc[fdcu].kdc_state = DC_IDLE;
5415b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
5425b81b6b3SRodney W. Grimes }
5435b81b6b3SRodney W. Grimes 
5445b81b6b3SRodney W. Grimes /*
5455b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
5465b81b6b3SRodney W. Grimes  */
5473a2f7427SDavid Greenman static int
548dc5df763SJoerg Wunsch fdattach(struct isa_device *dev)
5495b81b6b3SRodney W. Grimes {
5503a2f7427SDavid Greenman 	unsigned fdt;
5515b81b6b3SRodney W. Grimes 	fdu_t	fdu;
5525b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5535b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
5545b81b6b3SRodney W. Grimes 	fd_p	fd;
55574fa89f4SRodney W. Grimes 	int	fdsu, st0, st3, i, unithasfd;
556b99f0a4aSAndrew Moore 	struct isa_device *fdup;
557dc5df763SJoerg Wunsch 	int ic_type = 0;
558999422d7SJulian Elischer #ifdef	DEVFS
559999422d7SJulian Elischer 	char	name[64];
560999422d7SJulian Elischer 	caddr_t key;
561999422d7SJulian Elischer #endif	/* DEVFS */
56292200632SGarrett Wollman 
5635b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
5645b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
5655b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
5665b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
5673a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
5683a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
5695b81b6b3SRodney W. Grimes 
5705b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
571b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
572b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
573b99f0a4aSAndrew Moore 			continue;
574b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
575b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
576b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
577b99f0a4aSAndrew Moore 			continue;
578b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
579b99f0a4aSAndrew Moore 		/* look up what bios thinks we have */
580b99f0a4aSAndrew Moore 		switch (fdu) {
581b99f0a4aSAndrew Moore 			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
582b99f0a4aSAndrew Moore 				break;
583b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
584b99f0a4aSAndrew Moore 				break;
585b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
586b99f0a4aSAndrew Moore 				break;
587b99f0a4aSAndrew Moore 		}
5885b81b6b3SRodney W. Grimes 		/* is there a unit? */
589b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
590b99f0a4aSAndrew Moore #if NFT > 0
591b99f0a4aSAndrew Moore 		    || (fdsu >= DRVS_PER_CTLR)) {
592b99f0a4aSAndrew Moore #else
593b99f0a4aSAndrew Moore 		) {
59456ef0285SAndrew Moore 			fd->type = NO_TYPE;
595b99f0a4aSAndrew Moore #endif
596b99f0a4aSAndrew Moore #if NFT > 0
597b99f0a4aSAndrew Moore 			/* If BIOS says no floppy, or > 2nd device */
598b99f0a4aSAndrew Moore 			/* Probe for and attach a floppy tape.     */
59974fa89f4SRodney W. Grimes 			/* Tell FT if there was already a disk     */
60074fa89f4SRodney W. Grimes 			/* with this unit number found.            */
60174fa89f4SRodney W. Grimes 
60274fa89f4SRodney W. Grimes 			unithasfd = 0;
60374fa89f4SRodney W. Grimes 			if (fdu < NFD && fd->type != NO_TYPE)
60474fa89f4SRodney W. Grimes 				unithasfd = 1;
60585827d9cSJoerg Wunsch 			if (ftattach(dev, fdup, unithasfd))
606b99f0a4aSAndrew Moore 				continue;
60756ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
608b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
60956ef0285SAndrew Moore #endif
6105b81b6b3SRodney W. Grimes 			continue;
611f5f7ba03SJordan K. Hubbard 		}
6125b81b6b3SRodney W. Grimes 
6135b81b6b3SRodney W. Grimes 		/* select it */
6143a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
6156b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
616dc5df763SJoerg Wunsch 
617dc5df763SJoerg Wunsch 		if (ic_type == 0 &&
618dc5df763SJoerg Wunsch 		    fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
619dc5df763SJoerg Wunsch 		{
6206a0e6f42SRodney W. Grimes 			printf("fdc%d: ", fdcu);
621dc5df763SJoerg Wunsch 			ic_type = (u_char)ic_type;
622dc5df763SJoerg Wunsch 			switch( ic_type ) {
623dc5df763SJoerg Wunsch 			case 0x80:
6246a0e6f42SRodney W. Grimes 				printf("NEC 765\n");
625dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE765;
6266c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6276c0081e9SGarrett Wollman 					"NEC 765 floppy disk/tape controller";
628dc5df763SJoerg Wunsch 				break;
629dc5df763SJoerg Wunsch 			case 0x81:
6306a0e6f42SRodney W. Grimes 				printf("Intel 82077\n");
631dc5df763SJoerg Wunsch 				fdc->fdct = FDC_I82077;
6326c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6336c0081e9SGarrett Wollman 					"Intel 82077 floppy disk/tape controller";
634dc5df763SJoerg Wunsch 				break;
635dc5df763SJoerg Wunsch 			case 0x90:
6366a0e6f42SRodney W. Grimes 				printf("NEC 72065B\n");
637dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE72065;
6386c0081e9SGarrett Wollman 				kdc_fdc[fdcu].kdc_description =
6396c0081e9SGarrett Wollman 					"NEC 72065B floppy disk/tape controller";
640dc5df763SJoerg Wunsch 				break;
641dc5df763SJoerg Wunsch 			default:
6426a0e6f42SRodney W. Grimes 				printf("unknown IC type %02x\n", ic_type);
643dc5df763SJoerg Wunsch 				fdc->fdct = FDC_UNKNOWN;
644dc5df763SJoerg Wunsch 				break;
6456b7bd95bSJoerg Wunsch 			}
646dc5df763SJoerg Wunsch 		}
647dc5df763SJoerg Wunsch 		if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
648dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
649dc5df763SJoerg Wunsch 			/* if at track 0, first seek inwards */
650dc5df763SJoerg Wunsch 			/* seek some steps: */
651dc5df763SJoerg Wunsch 			(void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
652dc5df763SJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
653dc5df763SJoerg Wunsch 			(void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
654dc5df763SJoerg Wunsch 		}
655dc5df763SJoerg Wunsch 
656dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
657dc5df763SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
658dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
659dc5df763SJoerg Wunsch 			/* Seek some steps... */
660dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
661dc5df763SJoerg Wunsch 				/* ...wait a moment... */
662dc5df763SJoerg Wunsch 				DELAY(300000);
663dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
664dc5df763SJoerg Wunsch 				(void)fd_sense_int(fdc, 0, 0);
665dc5df763SJoerg Wunsch 			}
666dc5df763SJoerg Wunsch 		}
667dc5df763SJoerg Wunsch 
6686b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
6696b7bd95bSJoerg Wunsch 			/*
6706b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
6716b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
6726b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
6736b7bd95bSJoerg Wunsch 			 * more than 77 steps
6746b7bd95bSJoerg Wunsch 			 */
675dc5df763SJoerg Wunsch 			/* go back to 0: */
676dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
6776b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
6786b7bd95bSJoerg Wunsch 				DELAY(i == 0? 1000000: 300000);
6795b81b6b3SRodney W. Grimes 
6806b7bd95bSJoerg Wunsch 				/* anything responding? */
681dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
682dc5df763SJoerg Wunsch 				(st0 & NE7_ST0_EC) == 0)
6836b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
6846b7bd95bSJoerg Wunsch 			}
685dc5df763SJoerg Wunsch 		}
6866b7bd95bSJoerg Wunsch 
6873a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
6883a2f7427SDavid Greenman 
6893a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
6905b81b6b3SRodney W. Grimes 			continue;
6915b81b6b3SRodney W. Grimes 
692dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
693b99f0a4aSAndrew Moore 		fd->fdc = fdc;
694b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
6953a2f7427SDavid Greenman 		fd->options = 0;
6966a0e6f42SRodney W. Grimes 		printf("fd%d: ", fdsu, fdu);
6975b81b6b3SRodney W. Grimes 
6986c0081e9SGarrett Wollman 		fd_registerdev(fdcu, fdu);
699b99f0a4aSAndrew Moore 		switch (fdt) {
7007ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
7016a0e6f42SRodney W. Grimes 			printf("1.2MB 5.25in\n");
702b99f0a4aSAndrew Moore 			fd->type = FD_1200;
7036c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7046c0081e9SGarrett Wollman 				"1.2MB (1200K) 5.25in floppy disk drive";
705999422d7SJulian Elischer #ifdef	DEVFS
706999422d7SJulian Elischer 			sprintf(name,"fd%d.1200",fdu);
707999422d7SJulian Elischer #endif	/* DEVFS */
7087ca0641bSAndrey A. Chernov 			break;
7097ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
7106a0e6f42SRodney W. Grimes 			printf("1.44MB 3.5in\n");
711b99f0a4aSAndrew Moore 			fd->type = FD_1440;
7126c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7136c0081e9SGarrett Wollman 				"1.44MB (1440K) 3.5in floppy disk drive";
714999422d7SJulian Elischer #ifdef	DEVFS
715999422d7SJulian Elischer 			sprintf(name,"fd%d.1440",fdu);
716999422d7SJulian Elischer #endif	/* DEVFS */
7177ca0641bSAndrey A. Chernov 			break;
718290dd077SJoerg Wunsch 		case RTCFDT_288M:
7196a0e6f42SRodney W. Grimes 			printf("2.88MB 3.5in - 1.44MB mode\n");
720290dd077SJoerg Wunsch 			fd->type = FD_1440;
7216c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7226c0081e9SGarrett Wollman 				"2.88MB (2880K) 3.5in floppy disk drive in 1.44 mode";
723999422d7SJulian Elischer #ifdef	DEVFS
724999422d7SJulian Elischer 			sprintf(name,"fd%d.1440",fdu);
725999422d7SJulian Elischer #endif	/* DEVFS */
726290dd077SJoerg Wunsch 			break;
7277ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
7286a0e6f42SRodney W. Grimes 			printf("360KB 5.25in\n");
729b99f0a4aSAndrew Moore 			fd->type = FD_360;
7306c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7316c0081e9SGarrett Wollman 				"360KB 5.25in floppy disk drive";
732999422d7SJulian Elischer #ifdef	DEVFS
733999422d7SJulian Elischer 			sprintf(name,"fd%d.360",fdu);
734999422d7SJulian Elischer #endif	/* DEVFS */
7357ca0641bSAndrey A. Chernov 			break;
736ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
7376a0e6f42SRodney W. Grimes 			printf("720KB 3.5in\n");
738b99f0a4aSAndrew Moore 			fd->type = FD_720;
7396c0081e9SGarrett Wollman 			kdc_fd[fdu].kdc_description =
7406c0081e9SGarrett Wollman 				"720KB 3.5in floppy disk drive";
741999422d7SJulian Elischer #ifdef	DEVFS
742999422d7SJulian Elischer 			sprintf(name,"fd%d.720",fdu);
743999422d7SJulian Elischer #endif	/* DEVFS */
744ed2fa05eSAndrey A. Chernov 			break;
7457ca0641bSAndrey A. Chernov 		default:
7466a0e6f42SRodney W. Grimes 			printf("unknown\n");
747b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
748999422d7SJulian Elischer #ifdef	DEVFS
749999422d7SJulian Elischer 			sprintf(name,"fd%d.xxxx",fdu);
750999422d7SJulian Elischer #endif	/* DEVFS */
7517ca0641bSAndrey A. Chernov 			break;
7525b81b6b3SRodney W. Grimes 		}
753dc16046fSJoerg Wunsch 		kdc_fd[fdu].kdc_state = DC_IDLE;
754999422d7SJulian Elischer #ifdef DEVFS
755999422d7SJulian Elischer 		key = dev_add("/disks/rfloppy",name,(caddr_t)Fdopen,fdu * 8,
756999422d7SJulian Elischer 			0,0,0,0644);
757999422d7SJulian Elischer 		key = dev_add("/disks/floppy",name,(caddr_t)Fdopen,fdu * 8,
758999422d7SJulian Elischer 			1,0,0,0644);
759999422d7SJulian Elischer #endif /* DEVFS */
76092200632SGarrett Wollman 		if (dk_ndrive < DK_NDRIVE) {
76192200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
7626a0e6f42SRodney W. Grimes 			fd->dkunit = dk_ndrive++;
763671e2ceeSBruce Evans 			/*
764671e2ceeSBruce Evans 			 * XXX assume rate is FDC_500KBPS.
765671e2ceeSBruce Evans 			 */
766671e2ceeSBruce Evans 			dk_wpms[dk_ndrive] = 500000 / 8 / 2;
76792200632SGarrett Wollman 		} else {
76892200632SGarrett Wollman 			fd->dkunit = -1;
76992200632SGarrett Wollman 		}
7705b81b6b3SRodney W. Grimes 	}
7715b81b6b3SRodney W. Grimes 
7723a2f7427SDavid Greenman 	return (1);
7735b81b6b3SRodney W. Grimes }
7745b81b6b3SRodney W. Grimes 
7755b81b6b3SRodney W. Grimes int
776dc5df763SJoerg Wunsch fdsize(dev_t dev)
7775b81b6b3SRodney W. Grimes {
7785b81b6b3SRodney W. Grimes 	return(0);
7795b81b6b3SRodney W. Grimes }
7805b81b6b3SRodney W. Grimes 
7815b81b6b3SRodney W. Grimes /****************************************************************************/
7825b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
7835e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
7845b81b6b3SRodney W. Grimes /****************************************************************************/
7853a2f7427SDavid Greenman static void
786dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
7875b81b6b3SRodney W. Grimes {
7883a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
7893a2f7427SDavid Greenman 	int needspecify = 0;
7903a2f7427SDavid Greenman 
7913a2f7427SDavid Greenman 	if(turnon) {
7923a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
7933a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
7943a2f7427SDavid Greenman 	} else
7953a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
7963a2f7427SDavid Greenman 
7975e235068SJordan K. Hubbard 	if(!turnon
7985e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
7995e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
8005e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
8015e235068SJordan K. Hubbard 	else {
8023a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
8033a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
8043a2f7427SDavid Greenman 			needspecify = 1;
8053a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
8065b81b6b3SRodney W. Grimes 	}
8075b81b6b3SRodney W. Grimes 
8083a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
8093a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
810dc16046fSJoerg Wunsch 	kdc_fdc[fdcu].kdc_state = (fdout & FDO_FRST)? DC_BUSY: DC_IDLE;
8113a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
8123a2f7427SDavid Greenman 
8133a2f7427SDavid Greenman 	if(needspecify) {
814dc8603e3SJoerg Wunsch 		/*
815dc5df763SJoerg Wunsch 		 * XXX
816dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
817dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
818dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
819dc8603e3SJoerg Wunsch 		 */
820dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
821dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
822dc5df763SJoerg Wunsch 			     0);
8233a2f7427SDavid Greenman 	}
8243a2f7427SDavid Greenman }
8253a2f7427SDavid Greenman 
8265e235068SJordan K. Hubbard /* ARGSUSED */
827381fe1aaSGarrett Wollman static void
828d0917939SPaul Richards fd_turnoff(void *arg1)
8295b81b6b3SRodney W. Grimes {
830381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
831f5f7ba03SJordan K. Hubbard 	int	s;
8325b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
833dc16046fSJoerg Wunsch 
834dc16046fSJoerg Wunsch 	TRACE1("[fd%d: turnoff]", fdu);
835f5f7ba03SJordan K. Hubbard 	s = splbio();
8365b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
8373a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
838f5f7ba03SJordan K. Hubbard 	splx(s);
8395b81b6b3SRodney W. Grimes }
8405b81b6b3SRodney W. Grimes 
8415e235068SJordan K. Hubbard /* ARGSUSED */
8423a2f7427SDavid Greenman static void
843d0917939SPaul Richards fd_motor_on(void *arg1)
8445b81b6b3SRodney W. Grimes {
845381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
846f5f7ba03SJordan K. Hubbard 	int	s;
847f5f7ba03SJordan K. Hubbard 
8485b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
849f5f7ba03SJordan K. Hubbard 	s = splbio();
8505b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
8515b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
8525b81b6b3SRodney W. Grimes 	{
853f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
8545b81b6b3SRodney W. Grimes 	}
855f5f7ba03SJordan K. Hubbard 	splx(s);
8565b81b6b3SRodney W. Grimes }
8575b81b6b3SRodney W. Grimes 
8583a2f7427SDavid Greenman static void
859dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
8605b81b6b3SRodney W. Grimes {
8615b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
8625b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
8635b81b6b3SRodney W. Grimes 	{
8643a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
8653a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
8665e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
8675b81b6b3SRodney W. Grimes 	}
8685b81b6b3SRodney W. Grimes }
8695b81b6b3SRodney W. Grimes 
870381fe1aaSGarrett Wollman static void
871dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
8725b81b6b3SRodney W. Grimes {
8733a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
8743a2f7427SDavid Greenman 
8753a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
8763a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8773a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8783a2f7427SDavid Greenman 	DELAY(100);
8793a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
8803a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
8813a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
8823a2f7427SDavid Greenman 	DELAY(100);
8833a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
8843a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
8853a2f7427SDavid Greenman 
886dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
887dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
888dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
889dc5df763SJoerg Wunsch 		     0);
8905b81b6b3SRodney W. Grimes }
8915b81b6b3SRodney W. Grimes 
8925b81b6b3SRodney W. Grimes /****************************************************************************/
8935b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
8945b81b6b3SRodney W. Grimes /****************************************************************************/
8955b81b6b3SRodney W. Grimes int
896dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
8975b81b6b3SRodney W. Grimes {
8985b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
8995b81b6b3SRodney W. Grimes 	int i, j = 100000;
9003a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
9015b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
902dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9036a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
9045b81b6b3SRodney W. Grimes 	if (j <= 0)
9056a0e6f42SRodney W. Grimes 		return fdc_err(fdcu, "input ready timeout\n");
9065b81b6b3SRodney W. Grimes #ifdef	DEBUG
9073a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
9083a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
9095b81b6b3SRodney W. Grimes 	return(i);
9105b81b6b3SRodney W. Grimes #else
9113a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
9125b81b6b3SRodney W. Grimes #endif
9135b81b6b3SRodney W. Grimes }
9145b81b6b3SRodney W. Grimes 
915dc5df763SJoerg Wunsch /*
916dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
917dc5df763SJoerg Wunsch  */
918b5e8ce9fSBruce Evans static int
919dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
920dc5df763SJoerg Wunsch {
921dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
922dc5df763SJoerg Wunsch 	int i, j = 100000;
923dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
924dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
925dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
9266a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
927dc5df763SJoerg Wunsch 	if (j <= 0)
9286a0e6f42SRodney W. Grimes 		return fdc_err(fdcu, "input ready timeout\n");
929dc5df763SJoerg Wunsch #ifdef	DEBUG
930dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
931dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
932dc5df763SJoerg Wunsch 	*ptr = i;
933dc5df763SJoerg Wunsch 	return 0;
934dc5df763SJoerg Wunsch #else
935dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
936dc5df763SJoerg Wunsch 	if (ptr)
937dc5df763SJoerg Wunsch 		*ptr = i;
938dc5df763SJoerg Wunsch 	return 0;
939dc5df763SJoerg Wunsch #endif
940dc5df763SJoerg Wunsch }
941dc5df763SJoerg Wunsch 
942dc5df763SJoerg Wunsch int
943dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
9445b81b6b3SRodney W. Grimes {
9455b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
9463b3837dbSRodney W. Grimes 	int i;
9475b81b6b3SRodney W. Grimes 
9483b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
9493b3837dbSRodney W. Grimes 	i = 100000;
9503a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
9516a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
9523b3837dbSRodney W. Grimes 
9533b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
9543b3837dbSRodney W. Grimes 	i = 100000;
9553a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
9566a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "output ready timeout\n");
9573b3837dbSRodney W. Grimes 
9583b3837dbSRodney W. Grimes 	/* Send the command and return */
9593a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
9603a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
9615b81b6b3SRodney W. Grimes 	return (0);
9625b81b6b3SRodney W. Grimes }
9635b81b6b3SRodney W. Grimes 
9645b81b6b3SRodney W. Grimes /****************************************************************************/
9655b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
9665b81b6b3SRodney W. Grimes /****************************************************************************/
967381fe1aaSGarrett Wollman int
968671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
9695b81b6b3SRodney W. Grimes {
9705b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
97120a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
972b99f0a4aSAndrew Moore 	fdc_p	fdc;
9736febd9aaSJordan K. Hubbard 	int     st3;
9745b81b6b3SRodney W. Grimes 
975b99f0a4aSAndrew Moore #if NFT > 0
976b99f0a4aSAndrew Moore 	/* check for a tape open */
977b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
978b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
979b99f0a4aSAndrew Moore #endif
9805b81b6b3SRodney W. Grimes 	/* check bounds */
981b99f0a4aSAndrew Moore 	if (fdu >= NFD)
982b99f0a4aSAndrew Moore 		return(ENXIO);
983b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
984b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
985b99f0a4aSAndrew Moore 		return(ENXIO);
986b99f0a4aSAndrew Moore 	if (type > NUMDENS)
987b99f0a4aSAndrew Moore 		return(ENXIO);
9887ca0641bSAndrey A. Chernov 	if (type == 0)
9897ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
9907ca0641bSAndrey A. Chernov 	else {
9917ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
992fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
9937ca0641bSAndrey A. Chernov 			case FD_360:
9947ca0641bSAndrey A. Chernov 				return(ENXIO);
995ed2fa05eSAndrey A. Chernov 			case FD_720:
996b39c878eSAndrey A. Chernov 				if (   type != FD_820
997b39c878eSAndrey A. Chernov 				    && type != FD_800
998ed2fa05eSAndrey A. Chernov 				   )
999ed2fa05eSAndrey A. Chernov 					return(ENXIO);
1000ed2fa05eSAndrey A. Chernov 				break;
10017ca0641bSAndrey A. Chernov 			case FD_1200:
1002b39c878eSAndrey A. Chernov 				switch (type) {
1003b39c878eSAndrey A. Chernov 				case FD_1480:
1004b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1005fa4700b4SAndrey A. Chernov 					break;
10067ca0641bSAndrey A. Chernov 				case FD_1440:
1007b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1008b39c878eSAndrey A. Chernov 					break;
1009b39c878eSAndrey A. Chernov 				case FD_820:
1010b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1011b39c878eSAndrey A. Chernov 					break;
1012b39c878eSAndrey A. Chernov 				case FD_800:
1013b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1014b39c878eSAndrey A. Chernov 					break;
1015b39c878eSAndrey A. Chernov 				case FD_720:
1016b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1017b39c878eSAndrey A. Chernov 					break;
1018b39c878eSAndrey A. Chernov 				case FD_360:
1019b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1020b39c878eSAndrey A. Chernov 					break;
1021b39c878eSAndrey A. Chernov 				default:
1022b39c878eSAndrey A. Chernov 					return(ENXIO);
1023b39c878eSAndrey A. Chernov 				}
1024b39c878eSAndrey A. Chernov 				break;
1025b39c878eSAndrey A. Chernov 			case FD_1440:
1026b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1027b39c878eSAndrey A. Chernov 				    && type != FD_1480
1028ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1029b39c878eSAndrey A. Chernov 				    && type != FD_820
1030b39c878eSAndrey A. Chernov 				    && type != FD_800
1031b39c878eSAndrey A. Chernov 				    && type != FD_720
10327ca0641bSAndrey A. Chernov 				    )
1033dffff499SAndrey A. Chernov 					return(ENXIO);
1034fa4700b4SAndrey A. Chernov 				break;
10357ca0641bSAndrey A. Chernov 			}
10367ca0641bSAndrey A. Chernov 		}
1037fa4700b4SAndrey A. Chernov 	}
1038b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
10395b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
1040dc16046fSJoerg Wunsch 	kdc_fd[fdu].kdc_state = DC_BUSY;
10415b81b6b3SRodney W. Grimes 
10425b81b6b3SRodney W. Grimes 	return 0;
10435b81b6b3SRodney W. Grimes }
10445b81b6b3SRodney W. Grimes 
1045381fe1aaSGarrett Wollman int
1046671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
10475b81b6b3SRodney W. Grimes {
10485b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
1049b99f0a4aSAndrew Moore 
1050b99f0a4aSAndrew Moore #if NFT > 0
10513a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
10523a2f7427SDavid Greenman 
1053b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
10543a2f7427SDavid Greenman 		return ftclose(dev, flags);
1055b99f0a4aSAndrew Moore #endif
10565b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
10573a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
1058dc16046fSJoerg Wunsch 	kdc_fd[fdu].kdc_state = DC_IDLE;
1059dc16046fSJoerg Wunsch 
10605b81b6b3SRodney W. Grimes 	return(0);
10615b81b6b3SRodney W. Grimes }
10625b81b6b3SRodney W. Grimes 
10635b81b6b3SRodney W. Grimes 
10643a2f7427SDavid Greenman /****************************************************************************/
10653a2f7427SDavid Greenman /*                               fdstrategy                                 */
10663a2f7427SDavid Greenman /****************************************************************************/
10673a2f7427SDavid Greenman void
10683a2f7427SDavid Greenman fdstrategy(struct buf *bp)
10693a2f7427SDavid Greenman {
10703a2f7427SDavid Greenman 	register struct buf *dp;
10713a2f7427SDavid Greenman 	long nblocks, blknum;
10723a2f7427SDavid Greenman  	int	s;
10733a2f7427SDavid Greenman  	fdcu_t	fdcu;
10743a2f7427SDavid Greenman  	fdu_t	fdu;
10753a2f7427SDavid Greenman  	fdc_p	fdc;
10763a2f7427SDavid Greenman  	fd_p	fd;
10773a2f7427SDavid Greenman 	size_t	fdblk;
10783a2f7427SDavid Greenman 
10793a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
10803a2f7427SDavid Greenman 	fd = &fd_data[fdu];
10813a2f7427SDavid Greenman 	fdc = fd->fdc;
10823a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
10833a2f7427SDavid Greenman 	fdblk = 128 << (fd->ft->secsize);
10843a2f7427SDavid Greenman 
10853a2f7427SDavid Greenman #if NFT > 0
10863a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
10873a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
10883a2f7427SDavid Greenman 		bp->b_error = ENXIO;
10893a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
10903a2f7427SDavid Greenman 		goto bad;
10913a2f7427SDavid Greenman 	}
10923a2f7427SDavid Greenman 	/* check for controller already busy with tape */
10933a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
10943a2f7427SDavid Greenman 		bp->b_error = EBUSY;
10953a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
10963a2f7427SDavid Greenman 		goto bad;
10973a2f7427SDavid Greenman 	}
10983a2f7427SDavid Greenman #endif
10993a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
11003a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1101dc5df763SJoerg Wunsch 			printf(
11026a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1103702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
11043a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11053a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11063a2f7427SDavid Greenman 			goto bad;
11073a2f7427SDavid Greenman 		}
11083a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
11093a2f7427SDavid Greenman 			bp->b_error = EINVAL;
11103a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11113a2f7427SDavid Greenman 			goto bad;
11123a2f7427SDavid Greenman 		}
11133a2f7427SDavid Greenman 	}
11143a2f7427SDavid Greenman 
11153a2f7427SDavid Greenman 	/*
11163a2f7427SDavid Greenman 	 * Set up block calculations.
11173a2f7427SDavid Greenman 	 */
11183a2f7427SDavid Greenman 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk;
11193a2f7427SDavid Greenman  	nblocks = fd->ft->size;
11203a2f7427SDavid Greenman 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
11213a2f7427SDavid Greenman 		if (blknum == nblocks) {
11223a2f7427SDavid Greenman 			bp->b_resid = bp->b_bcount;
11233a2f7427SDavid Greenman 		} else {
11243a2f7427SDavid Greenman 			bp->b_error = ENOSPC;
11253a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
11263a2f7427SDavid Greenman 		}
11273a2f7427SDavid Greenman 		goto bad;
11283a2f7427SDavid Greenman 	}
11293a2f7427SDavid Greenman  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
11303a2f7427SDavid Greenman 	dp = &(fdc->head);
11313a2f7427SDavid Greenman 	s = splbio();
11323a2f7427SDavid Greenman 	disksort(dp, bp);
11335e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */
11343a2f7427SDavid Greenman 	fdstart(fdcu);
11353a2f7427SDavid Greenman 	splx(s);
11363a2f7427SDavid Greenman 	return;
11373a2f7427SDavid Greenman 
11383a2f7427SDavid Greenman bad:
11393a2f7427SDavid Greenman 	biodone(bp);
11403a2f7427SDavid Greenman }
11413a2f7427SDavid Greenman 
11425b81b6b3SRodney W. Grimes /***************************************************************\
11435b81b6b3SRodney W. Grimes *				fdstart				*
11445b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
11455b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
11465b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
11475b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
11485b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
11495b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
11505b81b6b3SRodney W. Grimes \***************************************************************/
1151381fe1aaSGarrett Wollman static void
1152dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
11535b81b6b3SRodney W. Grimes {
11545b81b6b3SRodney W. Grimes 	int s;
11555b81b6b3SRodney W. Grimes 
11565b81b6b3SRodney W. Grimes 	s = splbio();
11575b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
11585b81b6b3SRodney W. Grimes 	{
11595b81b6b3SRodney W. Grimes 		fdintr(fdcu);
11605b81b6b3SRodney W. Grimes 	}
11615b81b6b3SRodney W. Grimes 	splx(s);
11625b81b6b3SRodney W. Grimes }
11635b81b6b3SRodney W. Grimes 
11645e235068SJordan K. Hubbard /* ARGSUSED */
1165381fe1aaSGarrett Wollman static void
1166d0917939SPaul Richards fd_timeout(void *arg1)
11675b81b6b3SRodney W. Grimes {
1168381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
11695b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
11703a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
11715b81b6b3SRodney W. Grimes 	struct buf *dp, *bp;
1172f5f7ba03SJordan K. Hubbard 	int s;
11735b81b6b3SRodney W. Grimes 
11745b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
11755b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
11765b81b6b3SRodney W. Grimes 
11773a2f7427SDavid Greenman 	/*
11783a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
11793a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
11803a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
11813a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
11823a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
11833a2f7427SDavid Greenman 	 */
11845b81b6b3SRodney W. Grimes 
11853a2f7427SDavid Greenman 	s = splbio();
11863a2f7427SDavid Greenman 
11873a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
11883a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
11893a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
11903a2f7427SDavid Greenman 	{
11913a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
11923a2f7427SDavid Greenman 		/* yup, it is; kill it now */
11933a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
11943a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
11953a2f7427SDavid Greenman 	}
11965b81b6b3SRodney W. Grimes 
11975b81b6b3SRodney W. Grimes 	if (bp)
11985b81b6b3SRodney W. Grimes 	{
11995b81b6b3SRodney W. Grimes 		retrier(fdcu);
12003a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
12015b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
12025b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
12035b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
12045b81b6b3SRodney W. Grimes 	}
12055b81b6b3SRodney W. Grimes 	else
12065b81b6b3SRodney W. Grimes 	{
12075b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
12085b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
12095b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
12105b81b6b3SRodney W. Grimes 	}
1211f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1212f5f7ba03SJordan K. Hubbard 	splx(s);
12135b81b6b3SRodney W. Grimes }
12145b81b6b3SRodney W. Grimes 
12155b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
12165e235068SJordan K. Hubbard /* ARGSUSED */
1217381fe1aaSGarrett Wollman static void
1218d0917939SPaul Richards fd_pseudointr(void *arg1)
12195b81b6b3SRodney W. Grimes {
1220381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
12215b81b6b3SRodney W. Grimes 	int	s;
12223a2f7427SDavid Greenman 
12235b81b6b3SRodney W. Grimes 	s = splbio();
12245b81b6b3SRodney W. Grimes 	fdintr(fdcu);
12255b81b6b3SRodney W. Grimes 	splx(s);
12265b81b6b3SRodney W. Grimes }
12275b81b6b3SRodney W. Grimes 
12285b81b6b3SRodney W. Grimes /***********************************************************************\
12295b81b6b3SRodney W. Grimes *                                 fdintr				*
12305b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
12315b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
12325b81b6b3SRodney W. Grimes \***********************************************************************/
1233381fe1aaSGarrett Wollman void
1234381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
12355b81b6b3SRodney W. Grimes {
12365b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1237b99f0a4aSAndrew Moore #if NFT > 0
1238b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1239b99f0a4aSAndrew Moore 
1240b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1241b99f0a4aSAndrew Moore 		(ftintr(fdu));
1242b99f0a4aSAndrew Moore 	else
1243b99f0a4aSAndrew Moore #endif
1244381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1245381fe1aaSGarrett Wollman 			;
12465b81b6b3SRodney W. Grimes }
12475b81b6b3SRodney W. Grimes 
12485b81b6b3SRodney W. Grimes /***********************************************************************\
12495b81b6b3SRodney W. Grimes * The controller state machine.						*
12505b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
12515b81b6b3SRodney W. Grimes \***********************************************************************/
12523a2f7427SDavid Greenman static int
1253dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
12545b81b6b3SRodney W. Grimes {
12553a2f7427SDavid Greenman 	int read, format, head, sec = 0, i = 0, sectrac, st0, cyl, st3;
12565b81b6b3SRodney W. Grimes 	unsigned long blknum;
12575b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
12585b81b6b3SRodney W. Grimes 	fd_p fd;
12595b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
1260b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
12613a2f7427SDavid Greenman 	size_t fdblk;
12625b81b6b3SRodney W. Grimes 
12635b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
12645b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
12655b81b6b3SRodney W. Grimes 	if(!bp)
12665b81b6b3SRodney W. Grimes 	{
12675b81b6b3SRodney W. Grimes 		/***********************************************\
12685b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
12695b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
12705b81b6b3SRodney W. Grimes 		\***********************************************/
12715b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
12725b81b6b3SRodney W. Grimes 		if(fdc->fd)
12735b81b6b3SRodney W. Grimes 		{
12746a0e6f42SRodney W. Grimes 			printf("fd%d: unexpected valid fd pointer\n",
12753a2f7427SDavid Greenman 			       fdc->fdu);
12765b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
12775b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
12785b81b6b3SRodney W. Grimes 		}
12795b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
12805b81b6b3SRodney W. Grimes  		return(0);
12815b81b6b3SRodney W. Grimes 	}
12825b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
12835b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
12843a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
12855b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
12865b81b6b3SRodney W. Grimes 	{
12876a0e6f42SRodney W. Grimes 		printf("fd%d: confused fd pointers\n", fdu);
12885b81b6b3SRodney W. Grimes 	}
12895b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1290b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1291b39c878eSAndrey A. Chernov 	if(format)
1292b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
12935b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
12945b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
12955b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
12965e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu);
12975e235068SJordan K. Hubbard 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
12985b81b6b3SRodney W. Grimes 	switch (fdc->state)
12995b81b6b3SRodney W. Grimes 	{
13005b81b6b3SRodney W. Grimes 	case DEVIDLE:
13015b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
13025b81b6b3SRodney W. Grimes 		fdc->retry = 0;
13035b81b6b3SRodney W. Grimes 		fd->skip = 0;
13045b81b6b3SRodney W. Grimes 		fdc->fd = fd;
13055b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
13063a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
13073a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
13085b81b6b3SRodney W. Grimes 		/*******************************************************\
13095b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
13105b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
13115b81b6b3SRodney W. Grimes 		\*******************************************************/
13125b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
13135b81b6b3SRodney W. Grimes 		{
13145b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13155b81b6b3SRodney W. Grimes 			return(0); /* come back later */
13165b81b6b3SRodney W. Grimes 		}
13175b81b6b3SRodney W. Grimes 		/*******************************************************\
13185b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
13195b81b6b3SRodney W. Grimes 		\*******************************************************/
13205b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
13215b81b6b3SRodney W. Grimes 		{
13225b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
13235b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
13245b81b6b3SRodney W. Grimes 			return(0);
13255b81b6b3SRodney W. Grimes 		}
13265b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
13275b81b6b3SRodney W. Grimes 		{
13283a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
13295b81b6b3SRodney W. Grimes 		}
13305e235068SJordan K. Hubbard 		fdc->state = DOSEEK;
13315b81b6b3SRodney W. Grimes 		break;
13325b81b6b3SRodney W. Grimes 	case DOSEEK:
13335b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
13345b81b6b3SRodney W. Grimes 		{
13355b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
13365b81b6b3SRodney W. Grimes 			break;
13375b81b6b3SRodney W. Grimes 		}
1338dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1339dc5df763SJoerg Wunsch 			   fd->fdsu, bp->b_cylin * fd->ft->steptrac,
1340dc5df763SJoerg Wunsch 			   0))
1341dc8603e3SJoerg Wunsch 		{
1342dc8603e3SJoerg Wunsch 			/*
1343dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1344dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1345dc8603e3SJoerg Wunsch 			 */
1346dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1347dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1348dc8603e3SJoerg Wunsch 		}
1349dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
13505b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
13515b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13525b81b6b3SRodney W. Grimes 	case SEEKWAIT:
13535b81b6b3SRodney W. Grimes 		/* allow heads to settle */
135404b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
13555b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
13565b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13575b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
13585b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1359dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
13605b81b6b3SRodney W. Grimes 		{
13615b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
13623a2f7427SDavid Greenman 			do {
13633a2f7427SDavid Greenman 				/*
1364dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1365dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1366dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1367dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1368dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1369dc5df763SJoerg Wunsch 				 *
1370dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1371dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1372dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1373dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1374dc5df763SJoerg Wunsch 				 * real interrupt condition.
1375dc5df763SJoerg Wunsch 				 *
1376dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1377dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1378dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1379dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1380dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1381dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1382dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
13833a2f7427SDavid Greenman 				 */
1384dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1385dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1386dc5df763SJoerg Wunsch 					return 0;
1387dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1388dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1389dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
13903a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1391dc5df763SJoerg Wunsch 
13923a2f7427SDavid Greenman 			if (0 == descyl)
13933a2f7427SDavid Greenman 			{
1394dc5df763SJoerg Wunsch 				int failed = 0;
13953a2f7427SDavid Greenman 				/*
13963a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
13973a2f7427SDavid Greenman 				 * really there
13983a2f7427SDavid Greenman 				 */
1399dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1400dc5df763SJoerg Wunsch 					failed = 1;
14013a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
14023a2f7427SDavid Greenman 					printf(
14033a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
14043a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1405dc5df763SJoerg Wunsch 					failed = 1;
1406dc5df763SJoerg Wunsch 				}
1407dc5df763SJoerg Wunsch 
1408dc5df763SJoerg Wunsch 				if (failed)
1409dc5df763SJoerg Wunsch 				{
14103a2f7427SDavid Greenman 					if(fdc->retry < 3)
14113a2f7427SDavid Greenman 						fdc->retry = 3;
14123a2f7427SDavid Greenman 					return(retrier(fdcu));
14133a2f7427SDavid Greenman 				}
14143a2f7427SDavid Greenman 			}
1415dc5df763SJoerg Wunsch 
14165b81b6b3SRodney W. Grimes 			if (cyl != descyl)
14175b81b6b3SRodney W. Grimes 			{
14183a2f7427SDavid Greenman 				printf(
14193a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
14205e235068SJordan K. Hubbard 				       fdu, descyl, cyl, st0, NE7_ST0BITS);
14215b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
14225b81b6b3SRodney W. Grimes 			}
14235b81b6b3SRodney W. Grimes 		}
14245b81b6b3SRodney W. Grimes 
14255b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
1426b39c878eSAndrey A. Chernov 		if(format)
1427b39c878eSAndrey A. Chernov 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1428b39c878eSAndrey A. Chernov 				- (char *)finfo;
14295b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
14303a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
14313a2f7427SDavid Greenman 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
14323a2f7427SDavid Greenman 			+ fd->skip/fdblk;
14335b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
14345b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
14355b81b6b3SRodney W. Grimes 		head = sec / sectrac;
14365b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
14373a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
14383a2f7427SDavid Greenman 
14393a2f7427SDavid Greenman 		if(format || !read)
14403a2f7427SDavid Greenman 		{
14413a2f7427SDavid Greenman 			/* make sure the drive is writable */
1442dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1443dc8603e3SJoerg Wunsch 			{
1444dc8603e3SJoerg Wunsch 				/* stuck controller? */
1445dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1446dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1447dc8603e3SJoerg Wunsch 			}
14483a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
14493a2f7427SDavid Greenman 			{
14503a2f7427SDavid Greenman 				/*
14513a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
14523a2f7427SDavid Greenman 				 * in order to force the current operation
14533a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
14543a2f7427SDavid Greenman 				 * error - all error handling is done
14553a2f7427SDavid Greenman 				 * by the retrier()
14563a2f7427SDavid Greenman 				 */
14573a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
14583a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
14593a2f7427SDavid Greenman 				fdc->status[2] = 0;
14603a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
14613a2f7427SDavid Greenman 				fdc->status[4] = head;
14623a2f7427SDavid Greenman 				fdc->status[5] = sec;
14633a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
14643a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
14653a2f7427SDavid Greenman 				return (1);
14663a2f7427SDavid Greenman 			}
14673a2f7427SDavid Greenman 		}
14685b81b6b3SRodney W. Grimes 
1469b39c878eSAndrey A. Chernov 		if(format)
1470b39c878eSAndrey A. Chernov 		{
1471b39c878eSAndrey A. Chernov 			/* formatting */
1472dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1473dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1474dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1475dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1476dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1477dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1478dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1479dc5df763SJoerg Wunsch 				  0))
1480dc8603e3SJoerg Wunsch 			{
1481dc8603e3SJoerg Wunsch 				/* controller fell over */
1482dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1483dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1484dc8603e3SJoerg Wunsch 			}
1485b39c878eSAndrey A. Chernov 		}
1486b39c878eSAndrey A. Chernov 		else
1487b39c878eSAndrey A. Chernov 		{
1488dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1489dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1490dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1491dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1492dc5df763SJoerg Wunsch 				   head,
1493dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1494dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1495dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1496dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1497dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1498dc5df763SJoerg Wunsch 				   0))
14995b81b6b3SRodney W. Grimes 			{
1500dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1501dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1502dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
15035b81b6b3SRodney W. Grimes 			}
1504b39c878eSAndrey A. Chernov 		}
15055b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
15065e235068SJordan K. Hubbard 		timeout(fd_timeout, (caddr_t)fdcu, hz);
15075b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15085b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
15095e235068SJordan K. Hubbard 		untimeout(fd_timeout, (caddr_t)fdcu);
1510dc5df763SJoerg Wunsch 
1511dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
15125b81b6b3SRodney W. Grimes 		{
1513dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1514dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1515dc5df763SJoerg Wunsch 			return retrier(fdcu);
15165b81b6b3SRodney W. Grimes   		}
1517dc5df763SJoerg Wunsch 
15183a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1519dc5df763SJoerg Wunsch 
15203a2f7427SDavid Greenman 		/* FALLTHROUGH */
1521dc5df763SJoerg Wunsch 
15223a2f7427SDavid Greenman 	case IOTIMEDOUT:
15235b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
15243a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
15253a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
15265b81b6b3SRodney W. Grimes 		{
15273a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15283a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1529b39c878eSAndrey A. Chernov                                 /*
15303a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
15313a2f7427SDavid Greenman 				 * and didn't release it in time for the
15323a2f7427SDavid Greenman 				 * next FDC transfer.
15333a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
15343a2f7427SDavid Greenman 				 * count. (vak)
1535b39c878eSAndrey A. Chernov                                  */
1536b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1537b39c878eSAndrey A. Chernov                                 return (1);
1538b39c878eSAndrey A. Chernov                         }
15393a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
15403a2f7427SDavid Greenman 				&& fdc->retry < 6)
15413a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
15423a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
15433a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
15443a2f7427SDavid Greenman 				&& fdc->retry < 3)
15453a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
15465b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
15475b81b6b3SRodney W. Grimes 		}
15485b81b6b3SRodney W. Grimes 		/* All OK */
15493a2f7427SDavid Greenman 		fd->skip += fdblk;
1550b39c878eSAndrey A. Chernov 		if (!format && fd->skip < bp->b_bcount)
15515b81b6b3SRodney W. Grimes 		{
15525b81b6b3SRodney W. Grimes 			/* set up next transfer */
15533a2f7427SDavid Greenman 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
15543a2f7427SDavid Greenman 				+ fd->skip/fdblk;
15553a2f7427SDavid Greenman 			bp->b_cylin =
15563a2f7427SDavid Greenman 				(blknum / (fd->ft->sectrac * fd->ft->heads));
15575b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
15585b81b6b3SRodney W. Grimes 		}
15595b81b6b3SRodney W. Grimes 		else
15605b81b6b3SRodney W. Grimes 		{
15615b81b6b3SRodney W. Grimes 			/* ALL DONE */
15625b81b6b3SRodney W. Grimes 			fd->skip = 0;
15635b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
156426f9a767SRodney W. Grimes 			dp->b_actf = bp->b_actf;
15655b81b6b3SRodney W. Grimes 			biodone(bp);
15665b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
15675b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
15685b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
15695b81b6b3SRodney W. Grimes 		}
15705b81b6b3SRodney W. Grimes 		return(1);
15715b81b6b3SRodney W. Grimes 	case RESETCTLR:
15723a2f7427SDavid Greenman 		fdc_reset(fdc);
15735b81b6b3SRodney W. Grimes 		fdc->retry++;
15745b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
15755b81b6b3SRodney W. Grimes 		break;
15765b81b6b3SRodney W. Grimes 	case STARTRECAL:
1577dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1578dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1579dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1580dc8603e3SJoerg Wunsch 		{
1581dc8603e3SJoerg Wunsch 			/* arrgl */
1582dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1583dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1584dc8603e3SJoerg Wunsch 		}
15855b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
15865b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15875b81b6b3SRodney W. Grimes 	case RECALWAIT:
15885b81b6b3SRodney W. Grimes 		/* allow heads to settle */
158904b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
15905b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
15915b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15925b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
15933a2f7427SDavid Greenman 		do {
15943a2f7427SDavid Greenman 			/*
1595dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
15963a2f7427SDavid Greenman 			 */
1597dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1598dc5df763SJoerg Wunsch 				return 0;
1599dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1600dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1601dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
16023a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
16033a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
16045b81b6b3SRodney W. Grimes 		{
1605dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1606dc8603e3SJoerg Wunsch 				/*
1607dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1608dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1609dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1610dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1611dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1612dc8603e3SJoerg Wunsch 				 * time in a line
1613dc8603e3SJoerg Wunsch 				 */
1614dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1615dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
16163a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
16175b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
16185b81b6b3SRodney W. Grimes 		}
16195b81b6b3SRodney W. Grimes 		fd->track = 0;
16205b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
16215b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
16225b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16235b81b6b3SRodney W. Grimes 	case MOTORWAIT:
16245b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
16255b81b6b3SRodney W. Grimes 		{
16265b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
16275b81b6b3SRodney W. Grimes 		}
16285e235068SJordan K. Hubbard 		/*
16295e235068SJordan K. Hubbard 		 * since the controller was off, it has lost its
16305e235068SJordan K. Hubbard 		 * idea about the current track it were; thus,
16315e235068SJordan K. Hubbard 		 * recalibrate the bastard
16325e235068SJordan K. Hubbard 		 */
16335e235068SJordan K. Hubbard 		fdc->state = STARTRECAL;
16345b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
16355b81b6b3SRodney W. Grimes 	default:
1636dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1637dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1638dac0f2dbSJoerg Wunsch 			printf("FDC status :%lx %lx %lx %lx %lx %lx %lx   ",
16395b81b6b3SRodney W. Grimes 			       fdc->status[0],
16405b81b6b3SRodney W. Grimes 			       fdc->status[1],
16415b81b6b3SRodney W. Grimes 			       fdc->status[2],
16425b81b6b3SRodney W. Grimes 			       fdc->status[3],
16435b81b6b3SRodney W. Grimes 			       fdc->status[4],
16445b81b6b3SRodney W. Grimes 			       fdc->status[5],
16455b81b6b3SRodney W. Grimes 			       fdc->status[6] );
16463a2f7427SDavid Greenman 		else
1647dac0f2dbSJoerg Wunsch 			printf("No status available   ");
1648dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1649dac0f2dbSJoerg Wunsch 		{
1650dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
16515b81b6b3SRodney W. Grimes 			return(0);
16525b81b6b3SRodney W. Grimes 		}
1653dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1654dac0f2dbSJoerg Wunsch 		return(0);
1655dac0f2dbSJoerg Wunsch 	}
1656dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
16575b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
16585b81b6b3SRodney W. Grimes }
16595b81b6b3SRodney W. Grimes 
1660aaf08d94SGarrett Wollman static int
1661f5f7ba03SJordan K. Hubbard retrier(fdcu)
1662f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
16635b81b6b3SRodney W. Grimes {
16645b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
16655b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
16665b81b6b3SRodney W. Grimes 
16675b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
16685b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
16695b81b6b3SRodney W. Grimes 
16703a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
16713a2f7427SDavid Greenman 		goto fail;
16725b81b6b3SRodney W. Grimes 	switch(fdc->retry)
16735b81b6b3SRodney W. Grimes 	{
16745b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
16755b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
16765b81b6b3SRodney W. Grimes 		break;
16775b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
16785b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
16795b81b6b3SRodney W. Grimes 		break;
16805b81b6b3SRodney W. Grimes 	case 6:
16815b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
16825b81b6b3SRodney W. Grimes 		break;
16835b81b6b3SRodney W. Grimes 	case 7:
16845b81b6b3SRodney W. Grimes 		break;
16855b81b6b3SRodney W. Grimes 	default:
16863a2f7427SDavid Greenman 	fail:
16875b81b6b3SRodney W. Grimes 		{
16887ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
16897ca0641bSAndrey A. Chernov 			/* Trick diskerr */
16903a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
16913a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
169292ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
16933a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
16943a2f7427SDavid Greenman 				(struct disklabel *)NULL);
16957ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
1696dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1697dc5df763SJoerg Wunsch 			{
1698dc5df763SJoerg Wunsch 				printf(
1699dc5df763SJoerg Wunsch 			" (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n",
1700dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1701dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1702dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1703dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1704dc5df763SJoerg Wunsch 				       fdc->status[5]);
1705dc5df763SJoerg Wunsch 			}
1706dc5df763SJoerg Wunsch 			else
1707dc5df763SJoerg Wunsch 				printf(" (No status)\n");
17085b81b6b3SRodney W. Grimes 		}
17095b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
17105b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
17115b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
171226f9a767SRodney W. Grimes 		dp->b_actf = bp->b_actf;
17135b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
17145b81b6b3SRodney W. Grimes 		biodone(bp);
171592ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
17165b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
17175b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1718f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
171992ed385aSRodney W. Grimes 		return(1);
17205b81b6b3SRodney W. Grimes 	}
17215b81b6b3SRodney W. Grimes 	fdc->retry++;
17225b81b6b3SRodney W. Grimes 	return(1);
17235b81b6b3SRodney W. Grimes }
17245b81b6b3SRodney W. Grimes 
1725b39c878eSAndrey A. Chernov static int
1726b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1727b39c878eSAndrey A. Chernov 	dev_t dev;
1728b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1729b39c878eSAndrey A. Chernov 	struct proc *p;
1730b39c878eSAndrey A. Chernov {
1731b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1732b39c878eSAndrey A. Chernov  	fd_p	fd;
1733b39c878eSAndrey A. Chernov 
1734b39c878eSAndrey A. Chernov 	struct buf *bp;
1735b39c878eSAndrey A. Chernov 	int rv = 0, s;
17363a2f7427SDavid Greenman 	size_t fdblk;
1737b39c878eSAndrey A. Chernov 
1738b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1739b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
17403a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1741b39c878eSAndrey A. Chernov 
1742b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1743b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1744b39c878eSAndrey A. Chernov 	if(bp == 0)
1745b39c878eSAndrey A. Chernov 		return ENOBUFS;
174682f5379bSJoerg Wunsch 	/*
174782f5379bSJoerg Wunsch 	 * keep the process from being swapped
174882f5379bSJoerg Wunsch 	 */
174982f5379bSJoerg Wunsch 	p->p_flag |= P_PHYSIO;
1750b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1751b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1752b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1753b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1754b39c878eSAndrey A. Chernov 
1755b39c878eSAndrey A. Chernov 	/*
1756b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1757b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1758b39c878eSAndrey A. Chernov 	 */
1759b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
17603a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1761b39c878eSAndrey A. Chernov 
1762b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
17635e235068SJordan K. Hubbard 	bp->b_un.b_addr = (caddr_t)finfo;
1764b39c878eSAndrey A. Chernov 
1765b39c878eSAndrey A. Chernov 	/* now do the format */
1766b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1767b39c878eSAndrey A. Chernov 
1768b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1769b39c878eSAndrey A. Chernov 	s = splbio();
1770b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1771b39c878eSAndrey A. Chernov 	{
17725e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1773b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1774b39c878eSAndrey A. Chernov 			break;
1775b39c878eSAndrey A. Chernov 	}
1776b39c878eSAndrey A. Chernov 	splx(s);
1777b39c878eSAndrey A. Chernov 
177882f5379bSJoerg Wunsch 	if(rv == EWOULDBLOCK) {
1779b39c878eSAndrey A. Chernov 		/* timed out */
1780b39c878eSAndrey A. Chernov 		rv = EIO;
178182f5379bSJoerg Wunsch 		biodone(bp);
178282f5379bSJoerg Wunsch 	}
17833a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
17843a2f7427SDavid Greenman 		rv = bp->b_error;
178582f5379bSJoerg Wunsch 	/*
178682f5379bSJoerg Wunsch 	 * allow the process to be swapped
178782f5379bSJoerg Wunsch 	 */
178882f5379bSJoerg Wunsch 	p->p_flag &= ~P_PHYSIO;
1789b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1790b39c878eSAndrey A. Chernov 	return rv;
1791b39c878eSAndrey A. Chernov }
1792b39c878eSAndrey A. Chernov 
1793f5f7ba03SJordan K. Hubbard /*
1794671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
1795f5f7ba03SJordan K. Hubbard  */
17965b81b6b3SRodney W. Grimes 
1797f5f7ba03SJordan K. Hubbard int
1798b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1799f5f7ba03SJordan K. Hubbard 	dev_t dev;
1800f5f7ba03SJordan K. Hubbard 	int cmd;
1801f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1802f5f7ba03SJordan K. Hubbard 	int flag;
1803b39c878eSAndrey A. Chernov 	struct proc *p;
1804f5f7ba03SJordan K. Hubbard {
18053a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
18063a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
18073a2f7427SDavid Greenman 	size_t fdblk;
18083a2f7427SDavid Greenman 
1809f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1810f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1811f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
18123a2f7427SDavid Greenman 	int error = 0;
1813f5f7ba03SJordan K. Hubbard 
1814b99f0a4aSAndrew Moore #if NFT > 0
1815a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1816a60eff27SNate Williams 
1817a60eff27SNate Williams 	/* check for a tape ioctl */
1818a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1819b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1820b99f0a4aSAndrew Moore #endif
1821b99f0a4aSAndrew Moore 
18223a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1823f5f7ba03SJordan K. Hubbard 
1824f5f7ba03SJordan K. Hubbard 	switch (cmd)
1825f5f7ba03SJordan K. Hubbard 	{
1826f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1827f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1828f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
18293a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
183092ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1831f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1832f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1833f5f7ba03SJordan K. Hubbard 
183454c7241bSJordan K. Hubbard 		if (readdisklabel(dev, fdstrategy, dl, NULL, 0) == NULL)
1835f5f7ba03SJordan K. Hubbard 			error = 0;
1836f5f7ba03SJordan K. Hubbard 		else
1837f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1838f5f7ba03SJordan K. Hubbard 
1839f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1840f5f7ba03SJordan K. Hubbard 		break;
1841f5f7ba03SJordan K. Hubbard 
1842f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1843f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1844f5f7ba03SJordan K. Hubbard 			error = EBADF;
1845f5f7ba03SJordan K. Hubbard 		break;
1846f5f7ba03SJordan K. Hubbard 
1847f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1848f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1849f5f7ba03SJordan K. Hubbard 			error = EBADF;
1850f5f7ba03SJordan K. Hubbard 		break;
1851f5f7ba03SJordan K. Hubbard 
1852f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1853f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1854f5f7ba03SJordan K. Hubbard 		{
1855f5f7ba03SJordan K. Hubbard 			error = EBADF;
1856f5f7ba03SJordan K. Hubbard 			break;
1857f5f7ba03SJordan K. Hubbard 		}
1858f5f7ba03SJordan K. Hubbard 
1859f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1860f5f7ba03SJordan K. Hubbard 
18613a2f7427SDavid Greenman 		if ((error =
186254c7241bSJordan K. Hubbard 		     setdisklabel ((struct disklabel *)buffer, dl, 0)))
1863f5f7ba03SJordan K. Hubbard 			break;
1864f5f7ba03SJordan K. Hubbard 
1865b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
186654c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
1867b39c878eSAndrey A. Chernov 		break;
1868b39c878eSAndrey A. Chernov 
1869b39c878eSAndrey A. Chernov 	case FD_FORM:
1870b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1871b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1872b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1873b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1874b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1875b39c878eSAndrey A. Chernov 		else
1876b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1877b39c878eSAndrey A. Chernov 		break;
1878b39c878eSAndrey A. Chernov 
1879b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1880b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1881f5f7ba03SJordan K. Hubbard 		break;
1882f5f7ba03SJordan K. Hubbard 
18833a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
18843a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
18853a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
18863a2f7427SDavid Greenman 			return EPERM;
18873a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
18883a2f7427SDavid Greenman 		break;
18893a2f7427SDavid Greenman 
18903a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
18913a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
18923a2f7427SDavid Greenman 		break;
18933a2f7427SDavid Greenman 
18943a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
18953a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
18963a2f7427SDavid Greenman 		break;
18973a2f7427SDavid Greenman 
1898f5f7ba03SJordan K. Hubbard 	default:
18993a2f7427SDavid Greenman 		error = ENOTTY;
1900f5f7ba03SJordan K. Hubbard 		break;
1901f5f7ba03SJordan K. Hubbard 	}
1902f5f7ba03SJordan K. Hubbard 	return (error);
1903f5f7ba03SJordan K. Hubbard }
1904f5f7ba03SJordan K. Hubbard 
1905f5f7ba03SJordan K. Hubbard #endif
19063a2f7427SDavid Greenman /*
19073a2f7427SDavid Greenman  * Hello emacs, these are the
19083a2f7427SDavid Greenman  * Local Variables:
19093a2f7427SDavid Greenman  *  c-indent-level:               8
19103a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
19113a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
19123a2f7427SDavid Greenman  *  c-brace-offset:              -8
19133a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
19143a2f7427SDavid Greenman  *  c-argdecl-indent:             8
19153a2f7427SDavid Greenman  *  c-label-offset:              -8
19163a2f7427SDavid Greenman  *  c++-hanging-braces:           1
19173a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
19183a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
19193a2f7427SDavid Greenman  *  c++-friend-offset:            0
19203a2f7427SDavid Greenman  * End:
19213a2f7427SDavid Greenman  */
1922