xref: /freebsd/sys/dev/fdc/fdc.c (revision 6a0e6f4266d7a34e75e51a00f8af18cb4f40b277)
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
466a0e6f42SRodney W. Grimes  *	$Id: fd.c,v 1.54 1995/03/16 18:11:59 bde Exp $
475b81b6b3SRodney W. Grimes  *
485b81b6b3SRodney W. Grimes  */
495b81b6b3SRodney W. Grimes 
50b99f0a4aSAndrew Moore #include "ft.h"
51b99f0a4aSAndrew Moore #if NFT < 1
52b99f0a4aSAndrew Moore #undef NFDC
53b99f0a4aSAndrew Moore #endif
545b81b6b3SRodney W. Grimes #include "fd.h"
555b81b6b3SRodney W. Grimes 
56ae099941SJordan K. Hubbard /* Flags */
57ae099941SJordan K. Hubbard #define FT_PROBE		0x1
58ae099941SJordan K. Hubbard 
59b99f0a4aSAndrew Moore #if NFDC > 0
60b99f0a4aSAndrew Moore 
61b99f0a4aSAndrew Moore #include <sys/param.h>
62b99f0a4aSAndrew Moore #include <sys/systm.h>
63b99f0a4aSAndrew Moore #include <sys/kernel.h>
64b99f0a4aSAndrew Moore #include <sys/conf.h>
65b99f0a4aSAndrew Moore #include <sys/file.h>
66b99f0a4aSAndrew Moore #include <sys/ioctl.h>
67671e2ceeSBruce Evans #include <machine/clock.h>
68b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h>
69b99f0a4aSAndrew Moore #include <sys/disklabel.h>
70671e2ceeSBruce Evans #include <sys/diskslice.h>
71b99f0a4aSAndrew Moore #include <sys/buf.h>
72b99f0a4aSAndrew Moore #include <sys/uio.h>
73b99f0a4aSAndrew Moore #include <sys/malloc.h>
743a2f7427SDavid Greenman #include <sys/proc.h>
75b99f0a4aSAndrew Moore #include <sys/syslog.h>
7692200632SGarrett Wollman #include <sys/devconf.h>
7792200632SGarrett Wollman #include <sys/dkstat.h>
78f540b106SGarrett Wollman #include <i386/isa/isa.h>
79f540b106SGarrett Wollman #include <i386/isa/isa_device.h>
80f540b106SGarrett Wollman #include <i386/isa/fdreg.h>
81f540b106SGarrett Wollman #include <i386/isa/fdc.h>
82f540b106SGarrett Wollman #include <i386/isa/rtc.h>
83dc5df763SJoerg Wunsch #include <machine/stdarg.h>
8487eafbcaSPoul-Henning Kamp #if NFT > 0
8587eafbcaSPoul-Henning Kamp #include <sys/ftape.h>
8687eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h>
8787eafbcaSPoul-Henning Kamp #endif
885b81b6b3SRodney W. Grimes 
8992200632SGarrett Wollman static int fd_goaway(struct kern_devconf *, int);
9092200632SGarrett Wollman static int fdc_goaway(struct kern_devconf *, int);
91dc16046fSJoerg Wunsch static int
92dc16046fSJoerg Wunsch fd_externalize(struct proc *, struct kern_devconf *, void *, size_t);
935e235068SJordan K. Hubbard 
945e235068SJordan K. Hubbard /*
955e235068SJordan K. Hubbard  * Templates for the kern_devconf structures used when we attach.
965e235068SJordan K. Hubbard  */
975e235068SJordan K. Hubbard static struct kern_devconf kdc_fd[NFD] = { {
985e235068SJordan K. Hubbard 	0, 0, 0,		/* filled in by kern_devconf.c */
995e235068SJordan K. Hubbard 	"fd", 0, { MDDT_DISK, 0 },
1005e235068SJordan K. Hubbard 	fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN,
1015e235068SJordan K. Hubbard 	0,			/* parent */
1025e235068SJordan K. Hubbard 	0,			/* parentdata */
103dc16046fSJoerg Wunsch 	DC_UNCONFIGURED,	/* state */
1045e235068SJordan K. Hubbard 	"floppy disk"
1055e235068SJordan K. Hubbard } };
1065e235068SJordan K. Hubbard 
1075e235068SJordan K. Hubbard struct kern_devconf kdc_fdc[NFDC] = { {
1085e235068SJordan K. Hubbard 	0, 0, 0,		/* filled in by kern_devconf.c */
1095e235068SJordan K. Hubbard 	"fdc", 0, { MDDT_ISA, 0, "bio" },
1105e235068SJordan K. Hubbard 	isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN,
1115e235068SJordan K. Hubbard 	0,			/* parent */
1125e235068SJordan K. Hubbard 	0,			/* parentdata */
113dc16046fSJoerg Wunsch 	DC_UNCONFIGURED,	/* state */
1145e235068SJordan K. Hubbard 	"floppy disk/tape controller"
1155e235068SJordan K. Hubbard } };
1165e235068SJordan K. Hubbard 
1175e235068SJordan K. Hubbard static inline void
1185e235068SJordan K. Hubbard fd_registerdev(int ctlr, int unit)
1195e235068SJordan K. Hubbard {
1205e235068SJordan K. Hubbard 	if(unit != 0)
1215e235068SJordan K. Hubbard 		kdc_fd[unit] = kdc_fd[0];
1225e235068SJordan K. Hubbard 
1235e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_unit = unit;
1245e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_parent = &kdc_fdc[ctlr];
1255e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_parentdata = 0;
1265e235068SJordan K. Hubbard 	dev_attach(&kdc_fd[unit]);
1275e235068SJordan K. Hubbard }
1285e235068SJordan K. Hubbard 
1295e235068SJordan K. Hubbard static inline void
1305e235068SJordan K. Hubbard fdc_registerdev(struct isa_device *dvp)
1315e235068SJordan K. Hubbard {
1325e235068SJordan K. Hubbard 	int unit = dvp->id_unit;
1335e235068SJordan K. Hubbard 
1345e235068SJordan K. Hubbard 	if(unit != 0)
1355e235068SJordan K. Hubbard 		kdc_fdc[unit] = kdc_fdc[0];
1365e235068SJordan K. Hubbard 
1375e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_unit = unit;
1385e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_parent = &kdc_isa0;
1395e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_parentdata = dvp;
1405e235068SJordan K. Hubbard 	dev_attach(&kdc_fdc[unit]);
1415e235068SJordan K. Hubbard }
1425e235068SJordan K. Hubbard 
1435e235068SJordan K. Hubbard static int
1445e235068SJordan K. Hubbard fdc_goaway(struct kern_devconf *kdc, int force)
1455e235068SJordan K. Hubbard {
1465e235068SJordan K. Hubbard 	if(force) {
1475e235068SJordan K. Hubbard 		dev_detach(kdc);
1485e235068SJordan K. Hubbard 		return 0;
1495e235068SJordan K. Hubbard 	} else {
1505e235068SJordan K. Hubbard 		return EBUSY;	/* XXX fix */
1515e235068SJordan K. Hubbard 	}
1525e235068SJordan K. Hubbard }
1535e235068SJordan K. Hubbard 
1545e235068SJordan K. Hubbard static int
1555e235068SJordan K. Hubbard fd_goaway(struct kern_devconf *kdc, int force)
1565e235068SJordan K. Hubbard {
1575e235068SJordan K. Hubbard 	dev_detach(kdc);
1585e235068SJordan K. Hubbard 	return 0;
1595e235068SJordan K. Hubbard }
16092200632SGarrett Wollman 
161671e2ceeSBruce Evans #define	b_cylin	b_resid		/* XXX now spelled b_cylinder elsewhere */
1625b81b6b3SRodney W. Grimes 
163b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
164b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
1655b81b6b3SRodney W. Grimes 
1663a2f7427SDavid Greenman /*
1673a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
1683a2f7427SDavid Greenman  * on the controller
1693a2f7427SDavid Greenman  */
1703a2f7427SDavid Greenman #define id_physid id_scsiid
1713a2f7427SDavid Greenman 
172dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
173dc5df763SJoerg Wunsch #define FD_FAILED -1
174dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
175dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
176dc5df763SJoerg Wunsch 
177b39c878eSAndrey A. Chernov #define NUMTYPES 14
178b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1797ca0641bSAndrey A. Chernov 
1803a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
181b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
182b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
183b99f0a4aSAndrew Moore #define FD_1720         1
184b99f0a4aSAndrew Moore #define FD_1480         2
185b99f0a4aSAndrew Moore #define FD_1440         3
186b99f0a4aSAndrew Moore #define FD_1200         4
187b99f0a4aSAndrew Moore #define FD_820          5
188b99f0a4aSAndrew Moore #define FD_800          6
189b99f0a4aSAndrew Moore #define FD_720          7
190b99f0a4aSAndrew Moore #define FD_360          8
191ed2fa05eSAndrey A. Chernov 
192b99f0a4aSAndrew Moore #define FD_1480in5_25   9
193b99f0a4aSAndrew Moore #define FD_1440in5_25   10
194b99f0a4aSAndrew Moore #define FD_820in5_25    11
195b99f0a4aSAndrew Moore #define FD_800in5_25    12
196b99f0a4aSAndrew Moore #define FD_720in5_25    13
197b99f0a4aSAndrew Moore #define FD_360in5_25    14
198b99f0a4aSAndrew Moore 
1997ca0641bSAndrey A. Chernov 
2005b81b6b3SRodney W. Grimes struct fd_type fd_types[NUMTYPES] =
2015b81b6b3SRodney W. Grimes {
202126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
203126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
204126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
205126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
206126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
207126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
208126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
209b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
210ed2fa05eSAndrey A. Chernov 
211126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
212126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
213126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
214126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
215126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
216126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
2175b81b6b3SRodney W. Grimes };
2185b81b6b3SRodney W. Grimes 
219b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
220dc16046fSJoerg Wunsch 
2215b81b6b3SRodney W. Grimes /***********************************************************************\
2225b81b6b3SRodney W. Grimes * Per controller structure.						*
2235b81b6b3SRodney W. Grimes \***********************************************************************/
224b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
2255b81b6b3SRodney W. Grimes 
2265b81b6b3SRodney W. Grimes /***********************************************************************\
2275b81b6b3SRodney W. Grimes * Per drive structure.							*
228b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
2295b81b6b3SRodney W. Grimes \***********************************************************************/
2305b81b6b3SRodney W. Grimes struct fd_data {
231b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2325b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2333a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
2345b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
2355b81b6b3SRodney W. Grimes 	int	flags;
2365b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
2375b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
2385b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
2395b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
2405b81b6b3SRodney W. Grimes 	int	skip;
2415b81b6b3SRodney W. Grimes 	int	hddrv;
242dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
2435b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
2443a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
24592200632SGarrett Wollman 	int	dkunit;		/* disk stats unit number */
2465b81b6b3SRodney W. Grimes } fd_data[NFD];
2475b81b6b3SRodney W. Grimes 
2485b81b6b3SRodney W. Grimes /***********************************************************************\
2495b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
2505b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
2515b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
2525b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
2535b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
2545b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
2555b81b6b3SRodney W. Grimes \***********************************************************************/
256b99f0a4aSAndrew Moore 
2573a2f7427SDavid Greenman #if NFT > 0
2583a2f7427SDavid Greenman int ftopen(dev_t, int);
2593a2f7427SDavid Greenman int ftintr(ftu_t ftu);
2603a2f7427SDavid Greenman int ftclose(dev_t, int);
2613a2f7427SDavid Greenman void ftstrategy(struct buf *);
2623a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
2633a2f7427SDavid Greenman int ftdump(dev_t);
2643a2f7427SDavid Greenman int ftsize(dev_t);
2653a2f7427SDavid Greenman int ftattach(struct isa_device *, struct isa_device *);
2663a2f7427SDavid Greenman #endif
2675b81b6b3SRodney W. Grimes 
2683a2f7427SDavid Greenman /* autoconfig functions */
2693a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2703a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2713a2f7427SDavid Greenman 
2723a2f7427SDavid Greenman /* exported functions */
2733a2f7427SDavid Greenman int fdsize (dev_t);
2743a2f7427SDavid Greenman void fdintr(fdcu_t);
275671e2ceeSBruce Evans int Fdopen(dev_t, int, int, struct proc *);
276671e2ceeSBruce Evans int fdclose(dev_t, int, int, struct proc *);
2773a2f7427SDavid Greenman void fdstrategy(struct buf *);
2783a2f7427SDavid Greenman int fdioctl(dev_t, int, caddr_t, int, struct proc *);
2793a2f7427SDavid Greenman 
2803a2f7427SDavid Greenman /* needed for ft driver, thus exported */
2813a2f7427SDavid Greenman int in_fdc(fdcu_t);
2823a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
2833a2f7427SDavid Greenman 
2843a2f7427SDavid Greenman /* internal functions */
2853a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
2863a2f7427SDavid Greenman #  define TURNON 1
2873a2f7427SDavid Greenman #  define TURNOFF 0
2883a2f7427SDavid Greenman static timeout_t fd_turnoff;
2893a2f7427SDavid Greenman static timeout_t fd_motor_on;
2903a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2913a2f7427SDavid Greenman static void fdc_reset(fdc_p);
292b5e8ce9fSBruce Evans static int fd_in(fdcu_t, int *);
2933a2f7427SDavid Greenman static void fdstart(fdcu_t);
2943a2f7427SDavid Greenman static timeout_t fd_timeout;
2953a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2963a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
297aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2983a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2993a2f7427SDavid Greenman 
300aaf08d94SGarrett Wollman 
3015b81b6b3SRodney W. Grimes #define DEVIDLE		0
3025b81b6b3SRodney W. Grimes #define FINDWORK	1
3035b81b6b3SRodney W. Grimes #define	DOSEEK		2
3045b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
3055b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
3065b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
3075b81b6b3SRodney W. Grimes #define	STARTRECAL	6
3085b81b6b3SRodney W. Grimes #define	RESETCTLR	7
3095b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
3105b81b6b3SRodney W. Grimes #define	RECALWAIT	9
3115b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
3125b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
3135b81b6b3SRodney W. Grimes 
3145b81b6b3SRodney W. Grimes #ifdef	DEBUG
3155b81b6b3SRodney W. Grimes char *fdstates[] =
3165b81b6b3SRodney W. Grimes {
3175b81b6b3SRodney W. Grimes "DEVIDLE",
3185b81b6b3SRodney W. Grimes "FINDWORK",
3195b81b6b3SRodney W. Grimes "DOSEEK",
3205b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
3215b81b6b3SRodney W. Grimes "IOCOMPLETE",
3225b81b6b3SRodney W. Grimes "RECALCOMPLETE",
3235b81b6b3SRodney W. Grimes "STARTRECAL",
3245b81b6b3SRodney W. Grimes "RESETCTLR",
3255b81b6b3SRodney W. Grimes "SEEKWAIT",
3265b81b6b3SRodney W. Grimes "RECALWAIT",
3275b81b6b3SRodney W. Grimes "MOTORWAIT",
3285b81b6b3SRodney W. Grimes "IOTIMEDOUT"
3295b81b6b3SRodney W. Grimes };
3305b81b6b3SRodney W. Grimes 
3313a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
3323a2f7427SDavid Greenman int	fd_debug = 0;
3335b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
3345b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
335381fe1aaSGarrett Wollman #else /* DEBUG */
3365b81b6b3SRodney W. Grimes #define TRACE0(arg)
3375b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
338381fe1aaSGarrett Wollman #endif /* DEBUG */
3395b81b6b3SRodney W. Grimes 
340dc16046fSJoerg Wunsch /* autoconfig structure */
341dc16046fSJoerg Wunsch 
342dc16046fSJoerg Wunsch struct	isa_driver fdcdriver = {
343dc16046fSJoerg Wunsch 	fdprobe, fdattach, "fdc",
344dc16046fSJoerg Wunsch };
345dc16046fSJoerg Wunsch 
34692200632SGarrett Wollman struct isa_device *fdcdevs[NFDC];
34792200632SGarrett Wollman 
34892200632SGarrett Wollman /*
34992200632SGarrett Wollman  * Provide hw.devconf information.
35092200632SGarrett Wollman  */
35192200632SGarrett Wollman static int
352dc16046fSJoerg Wunsch fd_externalize(struct proc *p, struct kern_devconf *kdc,
353dc16046fSJoerg Wunsch 	       void *userp, size_t len)
35492200632SGarrett Wollman {
35592200632SGarrett Wollman 	return disk_externalize(fd_data[kdc->kdc_unit].fdsu, userp, &len);
35692200632SGarrett Wollman }
35792200632SGarrett Wollman 
35892200632SGarrett Wollman static int
359dc16046fSJoerg Wunsch fdc_externalize(struct proc *p, struct kern_devconf *kdc,
360dc16046fSJoerg Wunsch 		void *userp, size_t len)
36192200632SGarrett Wollman {
36292200632SGarrett Wollman 	return isa_externalize(fdcdevs[kdc->kdc_unit], userp, &len);
36392200632SGarrett Wollman }
36492200632SGarrett Wollman 
365dc5df763SJoerg Wunsch static int
366dc5df763SJoerg Wunsch fdc_err(fdcu_t fdcu, const char *s)
367dc5df763SJoerg Wunsch {
368dc5df763SJoerg Wunsch 	fdc_data[fdcu].fdc_errs++;
369dc5df763SJoerg Wunsch 	if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX)
3706a0e6f42SRodney W. Grimes 		printf("fdc%d: %s", fdcu, s);
371dc5df763SJoerg Wunsch 	else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX)
372dc5df763SJoerg Wunsch 		printf("fdc%d: too many errors, not logging any more\n",
373dc5df763SJoerg Wunsch 		       fdcu);
374dc5df763SJoerg Wunsch 
375dc5df763SJoerg Wunsch 	return FD_FAILED;
376dc5df763SJoerg Wunsch }
377dc5df763SJoerg Wunsch 
378dc5df763SJoerg Wunsch /*
379dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
380dc5df763SJoerg Wunsch  * Unit number,
381dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
382dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
383dc5df763SJoerg Wunsch  */
384dc5df763SJoerg Wunsch 
385dc5df763SJoerg Wunsch int
386dc5df763SJoerg Wunsch fd_cmd(fdcu_t fdcu, int n_out, ...)
387dc5df763SJoerg Wunsch {
388dc5df763SJoerg Wunsch 	u_char cmd;
389dc5df763SJoerg Wunsch 	int n_in;
390dc5df763SJoerg Wunsch 	int n;
391dc5df763SJoerg Wunsch 	va_list ap;
392dc5df763SJoerg Wunsch 
393dc5df763SJoerg Wunsch 	va_start(ap, n_out);
394dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
395dc5df763SJoerg Wunsch 	va_end(ap);
396dc5df763SJoerg Wunsch 	va_start(ap, n_out);
397dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
398dc5df763SJoerg Wunsch 	{
399dc5df763SJoerg Wunsch 		if (out_fdc(fdcu, va_arg(ap, int)) < 0)
400dc5df763SJoerg Wunsch 		{
401dc5df763SJoerg Wunsch 			char msg[50];
402dc5df763SJoerg Wunsch 			sprintf(msg,
403dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
404dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
405dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
406dc5df763SJoerg Wunsch 		}
407dc5df763SJoerg Wunsch 	}
408dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
409dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
410dc5df763SJoerg Wunsch 	{
411dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
412dc5df763SJoerg Wunsch 		if (fd_in(fdcu, ptr) < 0)
413dc5df763SJoerg Wunsch 		{
414dc5df763SJoerg Wunsch 			char msg[50];
415dc5df763SJoerg Wunsch 			sprintf(msg,
416dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
417dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
418dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
419dc5df763SJoerg Wunsch 		}
420dc5df763SJoerg Wunsch 	}
421dc5df763SJoerg Wunsch 
422dc5df763SJoerg Wunsch 	return 0;
423dc5df763SJoerg Wunsch }
424dc5df763SJoerg Wunsch 
425dc5df763SJoerg Wunsch int
426dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
427dc5df763SJoerg Wunsch {
428dc5df763SJoerg Wunsch 	int st3;
429dc5df763SJoerg Wunsch 
430dc5df763SJoerg Wunsch 	if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
431dc5df763SJoerg Wunsch 	{
4326a0e6f42SRodney W. Grimes 		return fdc_err(fdc->fdcu, "Sense Drive Status failed\n");
433dc5df763SJoerg Wunsch 	}
434dc5df763SJoerg Wunsch 	if (st3p)
435dc5df763SJoerg Wunsch 		*st3p = st3;
436dc5df763SJoerg Wunsch 
437dc5df763SJoerg Wunsch 	return 0;
438dc5df763SJoerg Wunsch }
439dc5df763SJoerg Wunsch 
440dc5df763SJoerg Wunsch int
441dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
442dc5df763SJoerg Wunsch {
443dc5df763SJoerg Wunsch 	int st0, cyl;
444dc5df763SJoerg Wunsch 
445dc5df763SJoerg Wunsch 	int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0);
446dc5df763SJoerg Wunsch 
447dc5df763SJoerg Wunsch 	if (ret)
448dc5df763SJoerg Wunsch 	{
449dc5df763SJoerg Wunsch 		(void)fdc_err(fdc->fdcu,
450dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
451dc5df763SJoerg Wunsch 		return ret;
452dc5df763SJoerg Wunsch 	}
453dc5df763SJoerg Wunsch 
454dc5df763SJoerg Wunsch 	if (st0p)
455dc5df763SJoerg Wunsch 		*st0p = st0;
456dc5df763SJoerg Wunsch 
457dc5df763SJoerg Wunsch 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV)
458dc5df763SJoerg Wunsch 	{
459dc5df763SJoerg Wunsch 		/*
460dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
461dc5df763SJoerg Wunsch 		 */
462dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
463dc5df763SJoerg Wunsch 	}
464dc5df763SJoerg Wunsch 
465dc5df763SJoerg Wunsch 	if (fd_in(fdc->fdcu, &cyl) < 0)
466dc5df763SJoerg Wunsch 	{
467dc5df763SJoerg Wunsch 		return fdc_err(fdc->fdcu, "can't get cyl num\n");
468dc5df763SJoerg Wunsch 	}
469dc5df763SJoerg Wunsch 
470dc5df763SJoerg Wunsch 	if (cylp)
471dc5df763SJoerg Wunsch 		*cylp = cyl;
472dc5df763SJoerg Wunsch 
473dc5df763SJoerg Wunsch 	return 0;
474dc5df763SJoerg Wunsch }
475dc5df763SJoerg Wunsch 
476dc5df763SJoerg Wunsch 
477dc5df763SJoerg Wunsch int
478dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
479dc5df763SJoerg Wunsch {
480dc5df763SJoerg Wunsch 	int i, ret;
481b5e8ce9fSBruce Evans 
482dc5df763SJoerg Wunsch 	for (i = 0; i < 7; i++)
483dc5df763SJoerg Wunsch 	{
484b5e8ce9fSBruce Evans 		/*
485b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
486b5e8ce9fSBruce Evans 		 * from the hardware, but fdc_status wants u_longs and
487b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
488b5e8ce9fSBruce Evans 		 */
489b5e8ce9fSBruce Evans 		int status;
490b5e8ce9fSBruce Evans 
491b5e8ce9fSBruce Evans 		ret = fd_in(fdc->fdcu, &status);
492b5e8ce9fSBruce Evans 		fdc->status[i] = status;
493b5e8ce9fSBruce Evans 		if (ret != 0)
494dc5df763SJoerg Wunsch 			break;
495dc5df763SJoerg Wunsch 	}
496dc5df763SJoerg Wunsch 
497dc5df763SJoerg Wunsch 	if (ret == 0)
498dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
499dc5df763SJoerg Wunsch 	else
500dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
501dc5df763SJoerg Wunsch 
502dc5df763SJoerg Wunsch 	return ret;
503dc5df763SJoerg Wunsch }
504dc5df763SJoerg Wunsch 
5055b81b6b3SRodney W. Grimes /****************************************************************************/
5065b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
5075b81b6b3SRodney W. Grimes /****************************************************************************/
508dc5df763SJoerg Wunsch 
5095b81b6b3SRodney W. Grimes /*
5105b81b6b3SRodney W. Grimes  * probe for existance of controller
5115b81b6b3SRodney W. Grimes  */
5123a2f7427SDavid Greenman static int
513dc5df763SJoerg Wunsch fdprobe(struct isa_device *dev)
5145b81b6b3SRodney W. Grimes {
5155b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5165b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
5175b81b6b3SRodney W. Grimes 	{
5186a0e6f42SRodney W. Grimes 		printf("fdc%d: unit used multiple times\n", fdcu);
5195b81b6b3SRodney W. Grimes 		return 0;
5205b81b6b3SRodney W. Grimes 	}
5215b81b6b3SRodney W. Grimes 
52292200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
5235b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
5245b81b6b3SRodney W. Grimes 
52516111cedSAndrew Moore 	/* First - lets reset the floppy controller */
5263a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
52716111cedSAndrew Moore 	DELAY(100);
5283a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
52916111cedSAndrew Moore 
5305b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
531dc5df763SJoerg Wunsch 	if (fd_cmd(fdcu,
532dc5df763SJoerg Wunsch 		   3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
533dc5df763SJoerg Wunsch 		   0))
5345b81b6b3SRodney W. Grimes 	{
5355b81b6b3SRodney W. Grimes 		return(0);
5365b81b6b3SRodney W. Grimes 	}
537dc16046fSJoerg Wunsch 	kdc_fdc[fdcu].kdc_state = DC_IDLE;
5385b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
5395b81b6b3SRodney W. Grimes }
5405b81b6b3SRodney W. Grimes 
5415b81b6b3SRodney W. Grimes /*
5425b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
5435b81b6b3SRodney W. Grimes  */
5443a2f7427SDavid Greenman static int
545dc5df763SJoerg Wunsch fdattach(struct isa_device *dev)
5465b81b6b3SRodney W. Grimes {
5473a2f7427SDavid Greenman 	unsigned fdt;
5485b81b6b3SRodney W. Grimes 	fdu_t	fdu;
5495b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5505b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
5515b81b6b3SRodney W. Grimes 	fd_p	fd;
552dc5df763SJoerg Wunsch 	int	fdsu, st0, st3, i;
553b99f0a4aSAndrew Moore 	struct isa_device *fdup;
554dc5df763SJoerg Wunsch 	int ic_type = 0;
55592200632SGarrett Wollman 
5562f86936aSGarrett Wollman 	fdc_registerdev(dev);
5575b81b6b3SRodney W. Grimes 
5585b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
5595b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
5605b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
5615b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
5623a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
5633a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
5645b81b6b3SRodney W. Grimes 
5655b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
566b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
567b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
568b99f0a4aSAndrew Moore 			continue;
569b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
570b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
571b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
572b99f0a4aSAndrew Moore 			continue;
573b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
574b99f0a4aSAndrew Moore 		/* look up what bios thinks we have */
575b99f0a4aSAndrew Moore 		switch (fdu) {
576b99f0a4aSAndrew Moore 			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
577b99f0a4aSAndrew Moore 				break;
578b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
579b99f0a4aSAndrew Moore 				break;
580b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
581b99f0a4aSAndrew Moore 				break;
582b99f0a4aSAndrew Moore 		}
5835b81b6b3SRodney W. Grimes 		/* is there a unit? */
584b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
585b99f0a4aSAndrew Moore #if NFT > 0
586b99f0a4aSAndrew Moore 		    || (fdsu >= DRVS_PER_CTLR)) {
587b99f0a4aSAndrew Moore #else
588b99f0a4aSAndrew Moore 		) {
58956ef0285SAndrew Moore 			fd->type = NO_TYPE;
590b99f0a4aSAndrew Moore #endif
591b99f0a4aSAndrew Moore #if NFT > 0
592b99f0a4aSAndrew Moore 			/* If BIOS says no floppy, or > 2nd device */
593b99f0a4aSAndrew Moore 			/* Probe for and attach a floppy tape.     */
594ae099941SJordan K. Hubbard 			if ((dev->id_flags & FT_PROBE) && ftattach(dev, fdup))
595b99f0a4aSAndrew Moore 				continue;
59656ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
597b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
59856ef0285SAndrew Moore #endif
5995b81b6b3SRodney W. Grimes 			continue;
600f5f7ba03SJordan K. Hubbard 		}
6015b81b6b3SRodney W. Grimes 
6025b81b6b3SRodney W. Grimes 		/* select it */
6033a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
6046b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
605dc5df763SJoerg Wunsch 
606dc5df763SJoerg Wunsch 		if (ic_type == 0 &&
607dc5df763SJoerg Wunsch 		    fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
608dc5df763SJoerg Wunsch 		{
6096a0e6f42SRodney W. Grimes 			printf("fdc%d: ", fdcu);
610dc5df763SJoerg Wunsch 			ic_type = (u_char)ic_type;
611dc5df763SJoerg Wunsch 			switch( ic_type ) {
612dc5df763SJoerg Wunsch 			case 0x80:
6136a0e6f42SRodney W. Grimes 				printf("NEC 765\n");
614dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE765;
615dc5df763SJoerg Wunsch 				break;
616dc5df763SJoerg Wunsch 			case 0x81:
6176a0e6f42SRodney W. Grimes 				printf("Intel 82077\n");
618dc5df763SJoerg Wunsch 				fdc->fdct = FDC_I82077;
619dc5df763SJoerg Wunsch 				break;
620dc5df763SJoerg Wunsch 			case 0x90:
6216a0e6f42SRodney W. Grimes 				printf("NEC 72065B\n");
622dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE72065;
623dc5df763SJoerg Wunsch 				break;
624dc5df763SJoerg Wunsch 			default:
6256a0e6f42SRodney W. Grimes 				printf("unknown IC type %02x\n", ic_type);
626dc5df763SJoerg Wunsch 				fdc->fdct = FDC_UNKNOWN;
627dc5df763SJoerg Wunsch 				break;
6286b7bd95bSJoerg Wunsch 			}
629dc5df763SJoerg Wunsch 		}
630dc5df763SJoerg Wunsch 		if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
631dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
632dc5df763SJoerg Wunsch 			/* if at track 0, first seek inwards */
633dc5df763SJoerg Wunsch 			/* seek some steps: */
634dc5df763SJoerg Wunsch 			(void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
635dc5df763SJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
636dc5df763SJoerg Wunsch 			(void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
637dc5df763SJoerg Wunsch 		}
638dc5df763SJoerg Wunsch 
639dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
640dc5df763SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
641dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
642dc5df763SJoerg Wunsch 			/* Seek some steps... */
643dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
644dc5df763SJoerg Wunsch 				/* ...wait a moment... */
645dc5df763SJoerg Wunsch 				DELAY(300000);
646dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
647dc5df763SJoerg Wunsch 				(void)fd_sense_int(fdc, 0, 0);
648dc5df763SJoerg Wunsch 			}
649dc5df763SJoerg Wunsch 		}
650dc5df763SJoerg Wunsch 
6516b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
6526b7bd95bSJoerg Wunsch 			/*
6536b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
6546b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
6556b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
6566b7bd95bSJoerg Wunsch 			 * more than 77 steps
6576b7bd95bSJoerg Wunsch 			 */
658dc5df763SJoerg Wunsch 			/* go back to 0: */
659dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
6606b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
6616b7bd95bSJoerg Wunsch 				DELAY(i == 0? 1000000: 300000);
6625b81b6b3SRodney W. Grimes 
6636b7bd95bSJoerg Wunsch 				/* anything responding? */
664dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
665dc5df763SJoerg Wunsch 				(st0 & NE7_ST0_EC) == 0)
6666b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
6676b7bd95bSJoerg Wunsch 			}
668dc5df763SJoerg Wunsch 		}
6696b7bd95bSJoerg Wunsch 
6703a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
6713a2f7427SDavid Greenman 
6723a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
6735b81b6b3SRodney W. Grimes 			continue;
6745b81b6b3SRodney W. Grimes 
675dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
676b99f0a4aSAndrew Moore 		fd->fdc = fdc;
677b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
6783a2f7427SDavid Greenman 		fd->options = 0;
6796a0e6f42SRodney W. Grimes 		printf("fd%d: ", fdsu, fdu);
6805b81b6b3SRodney W. Grimes 
681b99f0a4aSAndrew Moore 		switch (fdt) {
6827ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
6836a0e6f42SRodney W. Grimes 			printf("1.2MB 5.25in\n");
684b99f0a4aSAndrew Moore 			fd->type = FD_1200;
6857ca0641bSAndrey A. Chernov 			break;
6867ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
6876a0e6f42SRodney W. Grimes 			printf("1.44MB 3.5in\n");
688b99f0a4aSAndrew Moore 			fd->type = FD_1440;
6897ca0641bSAndrey A. Chernov 			break;
690290dd077SJoerg Wunsch 		case RTCFDT_288M:
6916a0e6f42SRodney W. Grimes 			printf("2.88MB 3.5in - 1.44MB mode\n");
692290dd077SJoerg Wunsch 			fd->type = FD_1440;
693290dd077SJoerg Wunsch 			break;
6947ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
6956a0e6f42SRodney W. Grimes 			printf("360KB 5.25in\n");
696b99f0a4aSAndrew Moore 			fd->type = FD_360;
6977ca0641bSAndrey A. Chernov 			break;
698ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
6996a0e6f42SRodney W. Grimes 			printf("720KB 3.5in\n");
700b99f0a4aSAndrew Moore 			fd->type = FD_720;
701ed2fa05eSAndrey A. Chernov 			break;
7027ca0641bSAndrey A. Chernov 		default:
7036a0e6f42SRodney W. Grimes 			printf("unknown\n");
704b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
7057ca0641bSAndrey A. Chernov 			break;
7065b81b6b3SRodney W. Grimes 		}
70792200632SGarrett Wollman 		fd_registerdev(fdcu, fdu);
708dc16046fSJoerg Wunsch 		kdc_fd[fdu].kdc_state = DC_IDLE;
70992200632SGarrett Wollman 		if (dk_ndrive < DK_NDRIVE) {
71092200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
7116a0e6f42SRodney W. Grimes 			fd->dkunit = dk_ndrive++;
712671e2ceeSBruce Evans 			/*
713671e2ceeSBruce Evans 			 * XXX assume rate is FDC_500KBPS.
714671e2ceeSBruce Evans 			 */
715671e2ceeSBruce Evans 			dk_wpms[dk_ndrive] = 500000 / 8 / 2;
71692200632SGarrett Wollman 		} else {
71792200632SGarrett Wollman 			fd->dkunit = -1;
71892200632SGarrett Wollman 		}
7195b81b6b3SRodney W. Grimes 	}
7205b81b6b3SRodney W. Grimes 
7213a2f7427SDavid Greenman 	return (1);
7225b81b6b3SRodney W. Grimes }
7235b81b6b3SRodney W. Grimes 
7245b81b6b3SRodney W. Grimes int
725dc5df763SJoerg Wunsch fdsize(dev_t dev)
7265b81b6b3SRodney W. Grimes {
7275b81b6b3SRodney W. Grimes 	return(0);
7285b81b6b3SRodney W. Grimes }
7295b81b6b3SRodney W. Grimes 
7305b81b6b3SRodney W. Grimes /****************************************************************************/
7315b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
7325e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
7335b81b6b3SRodney W. Grimes /****************************************************************************/
7343a2f7427SDavid Greenman static void
735dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
7365b81b6b3SRodney W. Grimes {
7373a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
7383a2f7427SDavid Greenman 	int needspecify = 0;
7393a2f7427SDavid Greenman 
7403a2f7427SDavid Greenman 	if(turnon) {
7413a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
7423a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
7433a2f7427SDavid Greenman 	} else
7443a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
7453a2f7427SDavid Greenman 
7465e235068SJordan K. Hubbard 	if(!turnon
7475e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
7485e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
7495e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
7505e235068SJordan K. Hubbard 	else {
7513a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
7523a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
7533a2f7427SDavid Greenman 			needspecify = 1;
7543a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
7555b81b6b3SRodney W. Grimes 	}
7565b81b6b3SRodney W. Grimes 
7573a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
7583a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
759dc16046fSJoerg Wunsch 	kdc_fdc[fdcu].kdc_state = (fdout & FDO_FRST)? DC_BUSY: DC_IDLE;
7603a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
7613a2f7427SDavid Greenman 
7623a2f7427SDavid Greenman 	if(needspecify) {
763dc8603e3SJoerg Wunsch 		/*
764dc5df763SJoerg Wunsch 		 * XXX
765dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
766dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
767dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
768dc8603e3SJoerg Wunsch 		 */
769dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
770dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
771dc5df763SJoerg Wunsch 			     0);
7723a2f7427SDavid Greenman 	}
7733a2f7427SDavid Greenman }
7743a2f7427SDavid Greenman 
7755e235068SJordan K. Hubbard /* ARGSUSED */
776381fe1aaSGarrett Wollman static void
777d0917939SPaul Richards fd_turnoff(void *arg1)
7785b81b6b3SRodney W. Grimes {
779381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
780f5f7ba03SJordan K. Hubbard 	int	s;
7815b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
782dc16046fSJoerg Wunsch 
783dc16046fSJoerg Wunsch 	TRACE1("[fd%d: turnoff]", fdu);
784f5f7ba03SJordan K. Hubbard 	s = splbio();
7855b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
7863a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
787f5f7ba03SJordan K. Hubbard 	splx(s);
7885b81b6b3SRodney W. Grimes }
7895b81b6b3SRodney W. Grimes 
7905e235068SJordan K. Hubbard /* ARGSUSED */
7913a2f7427SDavid Greenman static void
792d0917939SPaul Richards fd_motor_on(void *arg1)
7935b81b6b3SRodney W. Grimes {
794381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
795f5f7ba03SJordan K. Hubbard 	int	s;
796f5f7ba03SJordan K. Hubbard 
7975b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
798f5f7ba03SJordan K. Hubbard 	s = splbio();
7995b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
8005b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
8015b81b6b3SRodney W. Grimes 	{
802f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
8035b81b6b3SRodney W. Grimes 	}
804f5f7ba03SJordan K. Hubbard 	splx(s);
8055b81b6b3SRodney W. Grimes }
8065b81b6b3SRodney W. Grimes 
8073a2f7427SDavid Greenman static void
808dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
8095b81b6b3SRodney W. Grimes {
8105b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
8115b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
8125b81b6b3SRodney W. Grimes 	{
8133a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
8143a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
8155e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
8165b81b6b3SRodney W. Grimes 	}
8175b81b6b3SRodney W. Grimes }
8185b81b6b3SRodney W. Grimes 
819381fe1aaSGarrett Wollman static void
820dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
8215b81b6b3SRodney W. Grimes {
8223a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
8233a2f7427SDavid Greenman 
8243a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
8253a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8263a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8273a2f7427SDavid Greenman 	DELAY(100);
8283a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
8293a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
8303a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
8313a2f7427SDavid Greenman 	DELAY(100);
8323a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
8333a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
8343a2f7427SDavid Greenman 
835dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
836dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
837dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
838dc5df763SJoerg Wunsch 		     0);
8395b81b6b3SRodney W. Grimes }
8405b81b6b3SRodney W. Grimes 
8415b81b6b3SRodney W. Grimes /****************************************************************************/
8425b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
8435b81b6b3SRodney W. Grimes /****************************************************************************/
8445b81b6b3SRodney W. Grimes int
845dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
8465b81b6b3SRodney W. Grimes {
8475b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
8485b81b6b3SRodney W. Grimes 	int i, j = 100000;
8493a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
8505b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
851dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
8526a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
8535b81b6b3SRodney W. Grimes 	if (j <= 0)
8546a0e6f42SRodney W. Grimes 		return fdc_err(fdcu, "input ready timeout\n");
8555b81b6b3SRodney W. Grimes #ifdef	DEBUG
8563a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
8573a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
8585b81b6b3SRodney W. Grimes 	return(i);
8595b81b6b3SRodney W. Grimes #else
8603a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
8615b81b6b3SRodney W. Grimes #endif
8625b81b6b3SRodney W. Grimes }
8635b81b6b3SRodney W. Grimes 
864dc5df763SJoerg Wunsch /*
865dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
866dc5df763SJoerg Wunsch  */
867b5e8ce9fSBruce Evans static int
868dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
869dc5df763SJoerg Wunsch {
870dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
871dc5df763SJoerg Wunsch 	int i, j = 100000;
872dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
873dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
874dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
8756a0e6f42SRodney W. Grimes 			return fdc_err(fdcu, "ready for output in input\n");
876dc5df763SJoerg Wunsch 	if (j <= 0)
8776a0e6f42SRodney W. Grimes 		return fdc_err(fdcu, "input ready timeout\n");
878dc5df763SJoerg Wunsch #ifdef	DEBUG
879dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
880dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
881dc5df763SJoerg Wunsch 	*ptr = i;
882dc5df763SJoerg Wunsch 	return 0;
883dc5df763SJoerg Wunsch #else
884dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
885dc5df763SJoerg Wunsch 	if (ptr)
886dc5df763SJoerg Wunsch 		*ptr = i;
887dc5df763SJoerg Wunsch 	return 0;
888dc5df763SJoerg Wunsch #endif
889dc5df763SJoerg Wunsch }
890dc5df763SJoerg Wunsch 
891dc5df763SJoerg Wunsch int
892dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
8935b81b6b3SRodney W. Grimes {
8945b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
8953b3837dbSRodney W. Grimes 	int i;
8965b81b6b3SRodney W. Grimes 
8973b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
8983b3837dbSRodney W. Grimes 	i = 100000;
8993a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
9006a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "direction bit not set\n");
9013b3837dbSRodney W. Grimes 
9023b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
9033b3837dbSRodney W. Grimes 	i = 100000;
9043a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
9056a0e6f42SRodney W. Grimes 	if (i <= 0) return fdc_err(fdcu, "output ready timeout\n");
9063b3837dbSRodney W. Grimes 
9073b3837dbSRodney W. Grimes 	/* Send the command and return */
9083a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
9093a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
9105b81b6b3SRodney W. Grimes 	return (0);
9115b81b6b3SRodney W. Grimes }
9125b81b6b3SRodney W. Grimes 
9135b81b6b3SRodney W. Grimes /****************************************************************************/
9145b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
9155b81b6b3SRodney W. Grimes /****************************************************************************/
916381fe1aaSGarrett Wollman int
917671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
9185b81b6b3SRodney W. Grimes {
9195b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
92020a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
921b99f0a4aSAndrew Moore 	fdc_p	fdc;
9226febd9aaSJordan K. Hubbard 	int     st3;
9235b81b6b3SRodney W. Grimes 
924b99f0a4aSAndrew Moore #if NFT > 0
925b99f0a4aSAndrew Moore 	/* check for a tape open */
926b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
927b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
928b99f0a4aSAndrew Moore #endif
9295b81b6b3SRodney W. Grimes 	/* check bounds */
930b99f0a4aSAndrew Moore 	if (fdu >= NFD)
931b99f0a4aSAndrew Moore 		return(ENXIO);
932b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
933b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
934b99f0a4aSAndrew Moore 		return(ENXIO);
935b99f0a4aSAndrew Moore 	if (type > NUMDENS)
936b99f0a4aSAndrew Moore 		return(ENXIO);
9377ca0641bSAndrey A. Chernov 	if (type == 0)
9387ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
9397ca0641bSAndrey A. Chernov 	else {
9407ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
941fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
9427ca0641bSAndrey A. Chernov 			case FD_360:
9437ca0641bSAndrey A. Chernov 				return(ENXIO);
944ed2fa05eSAndrey A. Chernov 			case FD_720:
945b39c878eSAndrey A. Chernov 				if (   type != FD_820
946b39c878eSAndrey A. Chernov 				    && type != FD_800
947ed2fa05eSAndrey A. Chernov 				   )
948ed2fa05eSAndrey A. Chernov 					return(ENXIO);
949ed2fa05eSAndrey A. Chernov 				break;
9507ca0641bSAndrey A. Chernov 			case FD_1200:
951b39c878eSAndrey A. Chernov 				switch (type) {
952b39c878eSAndrey A. Chernov 				case FD_1480:
953b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
954fa4700b4SAndrey A. Chernov 					break;
9557ca0641bSAndrey A. Chernov 				case FD_1440:
956b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
957b39c878eSAndrey A. Chernov 					break;
958b39c878eSAndrey A. Chernov 				case FD_820:
959b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
960b39c878eSAndrey A. Chernov 					break;
961b39c878eSAndrey A. Chernov 				case FD_800:
962b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
963b39c878eSAndrey A. Chernov 					break;
964b39c878eSAndrey A. Chernov 				case FD_720:
965b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
966b39c878eSAndrey A. Chernov 					break;
967b39c878eSAndrey A. Chernov 				case FD_360:
968b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
969b39c878eSAndrey A. Chernov 					break;
970b39c878eSAndrey A. Chernov 				default:
971b39c878eSAndrey A. Chernov 					return(ENXIO);
972b39c878eSAndrey A. Chernov 				}
973b39c878eSAndrey A. Chernov 				break;
974b39c878eSAndrey A. Chernov 			case FD_1440:
975b39c878eSAndrey A. Chernov 				if (   type != FD_1720
976b39c878eSAndrey A. Chernov 				    && type != FD_1480
977ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
978b39c878eSAndrey A. Chernov 				    && type != FD_820
979b39c878eSAndrey A. Chernov 				    && type != FD_800
980b39c878eSAndrey A. Chernov 				    && type != FD_720
9817ca0641bSAndrey A. Chernov 				    )
982dffff499SAndrey A. Chernov 					return(ENXIO);
983fa4700b4SAndrey A. Chernov 				break;
9847ca0641bSAndrey A. Chernov 			}
9857ca0641bSAndrey A. Chernov 		}
986fa4700b4SAndrey A. Chernov 	}
987b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
9885b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
989dc16046fSJoerg Wunsch 	kdc_fd[fdu].kdc_state = DC_BUSY;
9905b81b6b3SRodney W. Grimes 
9915b81b6b3SRodney W. Grimes 	return 0;
9925b81b6b3SRodney W. Grimes }
9935b81b6b3SRodney W. Grimes 
994381fe1aaSGarrett Wollman int
995671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
9965b81b6b3SRodney W. Grimes {
9975b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
998b99f0a4aSAndrew Moore 
999b99f0a4aSAndrew Moore #if NFT > 0
10003a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
10013a2f7427SDavid Greenman 
1002b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
10033a2f7427SDavid Greenman 		return ftclose(dev, flags);
1004b99f0a4aSAndrew Moore #endif
10055b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
10063a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
1007dc16046fSJoerg Wunsch 	kdc_fd[fdu].kdc_state = DC_IDLE;
1008dc16046fSJoerg Wunsch 
10095b81b6b3SRodney W. Grimes 	return(0);
10105b81b6b3SRodney W. Grimes }
10115b81b6b3SRodney W. Grimes 
10125b81b6b3SRodney W. Grimes 
10133a2f7427SDavid Greenman /****************************************************************************/
10143a2f7427SDavid Greenman /*                               fdstrategy                                 */
10153a2f7427SDavid Greenman /****************************************************************************/
10163a2f7427SDavid Greenman void
10173a2f7427SDavid Greenman fdstrategy(struct buf *bp)
10183a2f7427SDavid Greenman {
10193a2f7427SDavid Greenman 	register struct buf *dp;
10203a2f7427SDavid Greenman 	long nblocks, blknum;
10213a2f7427SDavid Greenman  	int	s;
10223a2f7427SDavid Greenman  	fdcu_t	fdcu;
10233a2f7427SDavid Greenman  	fdu_t	fdu;
10243a2f7427SDavid Greenman  	fdc_p	fdc;
10253a2f7427SDavid Greenman  	fd_p	fd;
10263a2f7427SDavid Greenman 	size_t	fdblk;
10273a2f7427SDavid Greenman 
10283a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
10293a2f7427SDavid Greenman 	fd = &fd_data[fdu];
10303a2f7427SDavid Greenman 	fdc = fd->fdc;
10313a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
10323a2f7427SDavid Greenman 	fdblk = 128 << (fd->ft->secsize);
10333a2f7427SDavid Greenman 
10343a2f7427SDavid Greenman #if NFT > 0
10353a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
10363a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
10373a2f7427SDavid Greenman 		bp->b_error = ENXIO;
10383a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
10393a2f7427SDavid Greenman 		goto bad;
10403a2f7427SDavid Greenman 	}
10413a2f7427SDavid Greenman 	/* check for controller already busy with tape */
10423a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
10433a2f7427SDavid Greenman 		bp->b_error = EBUSY;
10443a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
10453a2f7427SDavid Greenman 		goto bad;
10463a2f7427SDavid Greenman 	}
10473a2f7427SDavid Greenman #endif
10483a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
10493a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1050dc5df763SJoerg Wunsch 			printf(
10516a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
1052702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
10533a2f7427SDavid Greenman 			bp->b_error = EINVAL;
10543a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
10553a2f7427SDavid Greenman 			goto bad;
10563a2f7427SDavid Greenman 		}
10573a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
10583a2f7427SDavid Greenman 			bp->b_error = EINVAL;
10593a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
10603a2f7427SDavid Greenman 			goto bad;
10613a2f7427SDavid Greenman 		}
10623a2f7427SDavid Greenman 	}
10633a2f7427SDavid Greenman 
10643a2f7427SDavid Greenman 	/*
10653a2f7427SDavid Greenman 	 * Set up block calculations.
10663a2f7427SDavid Greenman 	 */
10673a2f7427SDavid Greenman 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk;
10683a2f7427SDavid Greenman  	nblocks = fd->ft->size;
10693a2f7427SDavid Greenman 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
10703a2f7427SDavid Greenman 		if (blknum == nblocks) {
10713a2f7427SDavid Greenman 			bp->b_resid = bp->b_bcount;
10723a2f7427SDavid Greenman 		} else {
10733a2f7427SDavid Greenman 			bp->b_error = ENOSPC;
10743a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
10753a2f7427SDavid Greenman 		}
10763a2f7427SDavid Greenman 		goto bad;
10773a2f7427SDavid Greenman 	}
10783a2f7427SDavid Greenman  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
10793a2f7427SDavid Greenman 	dp = &(fdc->head);
10803a2f7427SDavid Greenman 	s = splbio();
10813a2f7427SDavid Greenman 	disksort(dp, bp);
10825e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */
10833a2f7427SDavid Greenman 	fdstart(fdcu);
10843a2f7427SDavid Greenman 	splx(s);
10853a2f7427SDavid Greenman 	return;
10863a2f7427SDavid Greenman 
10873a2f7427SDavid Greenman bad:
10883a2f7427SDavid Greenman 	biodone(bp);
10893a2f7427SDavid Greenman }
10903a2f7427SDavid Greenman 
10915b81b6b3SRodney W. Grimes /***************************************************************\
10925b81b6b3SRodney W. Grimes *				fdstart				*
10935b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
10945b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
10955b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
10965b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
10975b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
10985b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
10995b81b6b3SRodney W. Grimes \***************************************************************/
1100381fe1aaSGarrett Wollman static void
1101dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
11025b81b6b3SRodney W. Grimes {
11035b81b6b3SRodney W. Grimes 	int s;
11045b81b6b3SRodney W. Grimes 
11055b81b6b3SRodney W. Grimes 	s = splbio();
11065b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
11075b81b6b3SRodney W. Grimes 	{
11085b81b6b3SRodney W. Grimes 		fdintr(fdcu);
11095b81b6b3SRodney W. Grimes 	}
11105b81b6b3SRodney W. Grimes 	splx(s);
11115b81b6b3SRodney W. Grimes }
11125b81b6b3SRodney W. Grimes 
11135e235068SJordan K. Hubbard /* ARGSUSED */
1114381fe1aaSGarrett Wollman static void
1115d0917939SPaul Richards fd_timeout(void *arg1)
11165b81b6b3SRodney W. Grimes {
1117381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
11185b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
11193a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
11205b81b6b3SRodney W. Grimes 	struct buf *dp, *bp;
1121f5f7ba03SJordan K. Hubbard 	int s;
11225b81b6b3SRodney W. Grimes 
11235b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
11245b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
11255b81b6b3SRodney W. Grimes 
11263a2f7427SDavid Greenman 	/*
11273a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
11283a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
11293a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
11303a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
11313a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
11323a2f7427SDavid Greenman 	 */
11335b81b6b3SRodney W. Grimes 
11343a2f7427SDavid Greenman 	s = splbio();
11353a2f7427SDavid Greenman 
11363a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
11373a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
11383a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
11393a2f7427SDavid Greenman 	{
11403a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
11413a2f7427SDavid Greenman 		/* yup, it is; kill it now */
11423a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
11433a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
11443a2f7427SDavid Greenman 	}
11455b81b6b3SRodney W. Grimes 
11465b81b6b3SRodney W. Grimes 	if (bp)
11475b81b6b3SRodney W. Grimes 	{
11485b81b6b3SRodney W. Grimes 		retrier(fdcu);
11493a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
11505b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
11515b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
11525b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
11535b81b6b3SRodney W. Grimes 	}
11545b81b6b3SRodney W. Grimes 	else
11555b81b6b3SRodney W. Grimes 	{
11565b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
11575b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
11585b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
11595b81b6b3SRodney W. Grimes 	}
1160f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1161f5f7ba03SJordan K. Hubbard 	splx(s);
11625b81b6b3SRodney W. Grimes }
11635b81b6b3SRodney W. Grimes 
11645b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
11655e235068SJordan K. Hubbard /* ARGSUSED */
1166381fe1aaSGarrett Wollman static void
1167d0917939SPaul Richards fd_pseudointr(void *arg1)
11685b81b6b3SRodney W. Grimes {
1169381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
11705b81b6b3SRodney W. Grimes 	int	s;
11713a2f7427SDavid Greenman 
11725b81b6b3SRodney W. Grimes 	s = splbio();
11735b81b6b3SRodney W. Grimes 	fdintr(fdcu);
11745b81b6b3SRodney W. Grimes 	splx(s);
11755b81b6b3SRodney W. Grimes }
11765b81b6b3SRodney W. Grimes 
11775b81b6b3SRodney W. Grimes /***********************************************************************\
11785b81b6b3SRodney W. Grimes *                                 fdintr				*
11795b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
11805b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
11815b81b6b3SRodney W. Grimes \***********************************************************************/
1182381fe1aaSGarrett Wollman void
1183381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
11845b81b6b3SRodney W. Grimes {
11855b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1186b99f0a4aSAndrew Moore #if NFT > 0
1187b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1188b99f0a4aSAndrew Moore 
1189b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1190b99f0a4aSAndrew Moore 		(ftintr(fdu));
1191b99f0a4aSAndrew Moore 	else
1192b99f0a4aSAndrew Moore #endif
1193381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1194381fe1aaSGarrett Wollman 			;
11955b81b6b3SRodney W. Grimes }
11965b81b6b3SRodney W. Grimes 
11975b81b6b3SRodney W. Grimes /***********************************************************************\
11985b81b6b3SRodney W. Grimes * The controller state machine.						*
11995b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
12005b81b6b3SRodney W. Grimes \***********************************************************************/
12013a2f7427SDavid Greenman static int
1202dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
12035b81b6b3SRodney W. Grimes {
12043a2f7427SDavid Greenman 	int read, format, head, sec = 0, i = 0, sectrac, st0, cyl, st3;
12055b81b6b3SRodney W. Grimes 	unsigned long blknum;
12065b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
12075b81b6b3SRodney W. Grimes 	fd_p fd;
12085b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
1209b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
12103a2f7427SDavid Greenman 	size_t fdblk;
12115b81b6b3SRodney W. Grimes 
12125b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
12135b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
12145b81b6b3SRodney W. Grimes 	if(!bp)
12155b81b6b3SRodney W. Grimes 	{
12165b81b6b3SRodney W. Grimes 		/***********************************************\
12175b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
12185b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
12195b81b6b3SRodney W. Grimes 		\***********************************************/
12205b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
12215b81b6b3SRodney W. Grimes 		if(fdc->fd)
12225b81b6b3SRodney W. Grimes 		{
12236a0e6f42SRodney W. Grimes 			printf("fd%d: unexpected valid fd pointer\n",
12243a2f7427SDavid Greenman 			       fdc->fdu);
12255b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
12265b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
12275b81b6b3SRodney W. Grimes 		}
12285b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
12295b81b6b3SRodney W. Grimes  		return(0);
12305b81b6b3SRodney W. Grimes 	}
12315b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
12325b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
12333a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
12345b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
12355b81b6b3SRodney W. Grimes 	{
12366a0e6f42SRodney W. Grimes 		printf("fd%d: confused fd pointers\n", fdu);
12375b81b6b3SRodney W. Grimes 	}
12385b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1239b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1240b39c878eSAndrey A. Chernov 	if(format)
1241b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
12425b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
12435b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
12445b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
12455e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu);
12465e235068SJordan K. Hubbard 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
12475b81b6b3SRodney W. Grimes 	switch (fdc->state)
12485b81b6b3SRodney W. Grimes 	{
12495b81b6b3SRodney W. Grimes 	case DEVIDLE:
12505b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
12515b81b6b3SRodney W. Grimes 		fdc->retry = 0;
12525b81b6b3SRodney W. Grimes 		fd->skip = 0;
12535b81b6b3SRodney W. Grimes 		fdc->fd = fd;
12545b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
12553a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
12563a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
12575b81b6b3SRodney W. Grimes 		/*******************************************************\
12585b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
12595b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
12605b81b6b3SRodney W. Grimes 		\*******************************************************/
12615b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
12625b81b6b3SRodney W. Grimes 		{
12635b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
12645b81b6b3SRodney W. Grimes 			return(0); /* come back later */
12655b81b6b3SRodney W. Grimes 		}
12665b81b6b3SRodney W. Grimes 		/*******************************************************\
12675b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
12685b81b6b3SRodney W. Grimes 		\*******************************************************/
12695b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
12705b81b6b3SRodney W. Grimes 		{
12715b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
12725b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
12735b81b6b3SRodney W. Grimes 			return(0);
12745b81b6b3SRodney W. Grimes 		}
12755b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
12765b81b6b3SRodney W. Grimes 		{
12773a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
12785b81b6b3SRodney W. Grimes 		}
12795e235068SJordan K. Hubbard 		fdc->state = DOSEEK;
12805b81b6b3SRodney W. Grimes 		break;
12815b81b6b3SRodney W. Grimes 	case DOSEEK:
12825b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
12835b81b6b3SRodney W. Grimes 		{
12845b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
12855b81b6b3SRodney W. Grimes 			break;
12865b81b6b3SRodney W. Grimes 		}
1287dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1288dc5df763SJoerg Wunsch 			   fd->fdsu, bp->b_cylin * fd->ft->steptrac,
1289dc5df763SJoerg Wunsch 			   0))
1290dc8603e3SJoerg Wunsch 		{
1291dc8603e3SJoerg Wunsch 			/*
1292dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1293dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1294dc8603e3SJoerg Wunsch 			 */
1295dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1296dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1297dc8603e3SJoerg Wunsch 		}
1298dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
12995b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
13005b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13015b81b6b3SRodney W. Grimes 	case SEEKWAIT:
13025b81b6b3SRodney W. Grimes 		/* allow heads to settle */
130304b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
13045b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
13055b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13065b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
13075b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1308dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
13095b81b6b3SRodney W. Grimes 		{
13105b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
13113a2f7427SDavid Greenman 			do {
13123a2f7427SDavid Greenman 				/*
1313dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1314dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1315dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1316dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1317dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1318dc5df763SJoerg Wunsch 				 *
1319dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1320dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1321dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1322dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1323dc5df763SJoerg Wunsch 				 * real interrupt condition.
1324dc5df763SJoerg Wunsch 				 *
1325dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1326dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1327dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1328dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1329dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1330dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1331dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
13323a2f7427SDavid Greenman 				 */
1333dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1334dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1335dc5df763SJoerg Wunsch 					return 0;
1336dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1337dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1338dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
13393a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1340dc5df763SJoerg Wunsch 
13413a2f7427SDavid Greenman 			if (0 == descyl)
13423a2f7427SDavid Greenman 			{
1343dc5df763SJoerg Wunsch 				int failed = 0;
13443a2f7427SDavid Greenman 				/*
13453a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
13463a2f7427SDavid Greenman 				 * really there
13473a2f7427SDavid Greenman 				 */
1348dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1349dc5df763SJoerg Wunsch 					failed = 1;
13503a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
13513a2f7427SDavid Greenman 					printf(
13523a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
13533a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1354dc5df763SJoerg Wunsch 					failed = 1;
1355dc5df763SJoerg Wunsch 				}
1356dc5df763SJoerg Wunsch 
1357dc5df763SJoerg Wunsch 				if (failed)
1358dc5df763SJoerg Wunsch 				{
13593a2f7427SDavid Greenman 					if(fdc->retry < 3)
13603a2f7427SDavid Greenman 						fdc->retry = 3;
13613a2f7427SDavid Greenman 					return(retrier(fdcu));
13623a2f7427SDavid Greenman 				}
13633a2f7427SDavid Greenman 			}
1364dc5df763SJoerg Wunsch 
13655b81b6b3SRodney W. Grimes 			if (cyl != descyl)
13665b81b6b3SRodney W. Grimes 			{
13673a2f7427SDavid Greenman 				printf(
13683a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
13695e235068SJordan K. Hubbard 				       fdu, descyl, cyl, st0, NE7_ST0BITS);
13705b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
13715b81b6b3SRodney W. Grimes 			}
13725b81b6b3SRodney W. Grimes 		}
13735b81b6b3SRodney W. Grimes 
13745b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
1375b39c878eSAndrey A. Chernov 		if(format)
1376b39c878eSAndrey A. Chernov 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1377b39c878eSAndrey A. Chernov 				- (char *)finfo;
13785b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
13793a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
13803a2f7427SDavid Greenman 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
13813a2f7427SDavid Greenman 			+ fd->skip/fdblk;
13825b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
13835b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
13845b81b6b3SRodney W. Grimes 		head = sec / sectrac;
13855b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
13863a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
13873a2f7427SDavid Greenman 
13883a2f7427SDavid Greenman 		if(format || !read)
13893a2f7427SDavid Greenman 		{
13903a2f7427SDavid Greenman 			/* make sure the drive is writable */
1391dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1392dc8603e3SJoerg Wunsch 			{
1393dc8603e3SJoerg Wunsch 				/* stuck controller? */
1394dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1395dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1396dc8603e3SJoerg Wunsch 			}
13973a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
13983a2f7427SDavid Greenman 			{
13993a2f7427SDavid Greenman 				/*
14003a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
14013a2f7427SDavid Greenman 				 * in order to force the current operation
14023a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
14033a2f7427SDavid Greenman 				 * error - all error handling is done
14043a2f7427SDavid Greenman 				 * by the retrier()
14053a2f7427SDavid Greenman 				 */
14063a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
14073a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
14083a2f7427SDavid Greenman 				fdc->status[2] = 0;
14093a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
14103a2f7427SDavid Greenman 				fdc->status[4] = head;
14113a2f7427SDavid Greenman 				fdc->status[5] = sec;
14123a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
14133a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
14143a2f7427SDavid Greenman 				return (1);
14153a2f7427SDavid Greenman 			}
14163a2f7427SDavid Greenman 		}
14175b81b6b3SRodney W. Grimes 
1418b39c878eSAndrey A. Chernov 		if(format)
1419b39c878eSAndrey A. Chernov 		{
1420b39c878eSAndrey A. Chernov 			/* formatting */
1421dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1422dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1423dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1424dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1425dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1426dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1427dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1428dc5df763SJoerg Wunsch 				  0))
1429dc8603e3SJoerg Wunsch 			{
1430dc8603e3SJoerg Wunsch 				/* controller fell over */
1431dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1432dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1433dc8603e3SJoerg Wunsch 			}
1434b39c878eSAndrey A. Chernov 		}
1435b39c878eSAndrey A. Chernov 		else
1436b39c878eSAndrey A. Chernov 		{
1437dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1438dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1439dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1440dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1441dc5df763SJoerg Wunsch 				   head,
1442dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1443dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1444dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1445dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1446dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1447dc5df763SJoerg Wunsch 				   0))
14485b81b6b3SRodney W. Grimes 			{
1449dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1450dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1451dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
14525b81b6b3SRodney W. Grimes 			}
1453b39c878eSAndrey A. Chernov 		}
14545b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
14555e235068SJordan K. Hubbard 		timeout(fd_timeout, (caddr_t)fdcu, hz);
14565b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
14575b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
14585e235068SJordan K. Hubbard 		untimeout(fd_timeout, (caddr_t)fdcu);
1459dc5df763SJoerg Wunsch 
1460dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
14615b81b6b3SRodney W. Grimes 		{
1462dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1463dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1464dc5df763SJoerg Wunsch 			return retrier(fdcu);
14655b81b6b3SRodney W. Grimes   		}
1466dc5df763SJoerg Wunsch 
14673a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1468dc5df763SJoerg Wunsch 
14693a2f7427SDavid Greenman 		/* FALLTHROUGH */
1470dc5df763SJoerg Wunsch 
14713a2f7427SDavid Greenman 	case IOTIMEDOUT:
14725b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
14733a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
14743a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
14755b81b6b3SRodney W. Grimes 		{
14763a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
14773a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1478b39c878eSAndrey A. Chernov                                 /*
14793a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
14803a2f7427SDavid Greenman 				 * and didn't release it in time for the
14813a2f7427SDavid Greenman 				 * next FDC transfer.
14823a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
14833a2f7427SDavid Greenman 				 * count. (vak)
1484b39c878eSAndrey A. Chernov                                  */
1485b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1486b39c878eSAndrey A. Chernov                                 return (1);
1487b39c878eSAndrey A. Chernov                         }
14883a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
14893a2f7427SDavid Greenman 				&& fdc->retry < 6)
14903a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
14913a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
14923a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
14933a2f7427SDavid Greenman 				&& fdc->retry < 3)
14943a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
14955b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
14965b81b6b3SRodney W. Grimes 		}
14975b81b6b3SRodney W. Grimes 		/* All OK */
14983a2f7427SDavid Greenman 		fd->skip += fdblk;
1499b39c878eSAndrey A. Chernov 		if (!format && fd->skip < bp->b_bcount)
15005b81b6b3SRodney W. Grimes 		{
15015b81b6b3SRodney W. Grimes 			/* set up next transfer */
15023a2f7427SDavid Greenman 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
15033a2f7427SDavid Greenman 				+ fd->skip/fdblk;
15043a2f7427SDavid Greenman 			bp->b_cylin =
15053a2f7427SDavid Greenman 				(blknum / (fd->ft->sectrac * fd->ft->heads));
15065b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
15075b81b6b3SRodney W. Grimes 		}
15085b81b6b3SRodney W. Grimes 		else
15095b81b6b3SRodney W. Grimes 		{
15105b81b6b3SRodney W. Grimes 			/* ALL DONE */
15115b81b6b3SRodney W. Grimes 			fd->skip = 0;
15125b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
151326f9a767SRodney W. Grimes 			dp->b_actf = bp->b_actf;
15145b81b6b3SRodney W. Grimes 			biodone(bp);
15155b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
15165b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
15175b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
15185b81b6b3SRodney W. Grimes 		}
15195b81b6b3SRodney W. Grimes 		return(1);
15205b81b6b3SRodney W. Grimes 	case RESETCTLR:
15213a2f7427SDavid Greenman 		fdc_reset(fdc);
15225b81b6b3SRodney W. Grimes 		fdc->retry++;
15235b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
15245b81b6b3SRodney W. Grimes 		break;
15255b81b6b3SRodney W. Grimes 	case STARTRECAL:
1526dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1527dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1528dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1529dc8603e3SJoerg Wunsch 		{
1530dc8603e3SJoerg Wunsch 			/* arrgl */
1531dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1532dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1533dc8603e3SJoerg Wunsch 		}
15345b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
15355b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15365b81b6b3SRodney W. Grimes 	case RECALWAIT:
15375b81b6b3SRodney W. Grimes 		/* allow heads to settle */
153804b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
15395b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
15405b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15415b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
15423a2f7427SDavid Greenman 		do {
15433a2f7427SDavid Greenman 			/*
1544dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
15453a2f7427SDavid Greenman 			 */
1546dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1547dc5df763SJoerg Wunsch 				return 0;
1548dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1549dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1550dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
15513a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
15523a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
15535b81b6b3SRodney W. Grimes 		{
1554dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1555dc8603e3SJoerg Wunsch 				/*
1556dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1557dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1558dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1559dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1560dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1561dc8603e3SJoerg Wunsch 				 * time in a line
1562dc8603e3SJoerg Wunsch 				 */
1563dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1564dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
15653a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
15665b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
15675b81b6b3SRodney W. Grimes 		}
15685b81b6b3SRodney W. Grimes 		fd->track = 0;
15695b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
15705b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
15715b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
15725b81b6b3SRodney W. Grimes 	case MOTORWAIT:
15735b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
15745b81b6b3SRodney W. Grimes 		{
15755b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
15765b81b6b3SRodney W. Grimes 		}
15775e235068SJordan K. Hubbard 		/*
15785e235068SJordan K. Hubbard 		 * since the controller was off, it has lost its
15795e235068SJordan K. Hubbard 		 * idea about the current track it were; thus,
15805e235068SJordan K. Hubbard 		 * recalibrate the bastard
15815e235068SJordan K. Hubbard 		 */
15825e235068SJordan K. Hubbard 		fdc->state = STARTRECAL;
15835b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
15845b81b6b3SRodney W. Grimes 	default:
1585dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1586dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1587dac0f2dbSJoerg Wunsch 			printf("FDC status :%lx %lx %lx %lx %lx %lx %lx   ",
15885b81b6b3SRodney W. Grimes 			       fdc->status[0],
15895b81b6b3SRodney W. Grimes 			       fdc->status[1],
15905b81b6b3SRodney W. Grimes 			       fdc->status[2],
15915b81b6b3SRodney W. Grimes 			       fdc->status[3],
15925b81b6b3SRodney W. Grimes 			       fdc->status[4],
15935b81b6b3SRodney W. Grimes 			       fdc->status[5],
15945b81b6b3SRodney W. Grimes 			       fdc->status[6] );
15953a2f7427SDavid Greenman 		else
1596dac0f2dbSJoerg Wunsch 			printf("No status available   ");
1597dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1598dac0f2dbSJoerg Wunsch 		{
1599dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
16005b81b6b3SRodney W. Grimes 			return(0);
16015b81b6b3SRodney W. Grimes 		}
1602dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1603dac0f2dbSJoerg Wunsch 		return(0);
1604dac0f2dbSJoerg Wunsch 	}
1605dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
16065b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
16075b81b6b3SRodney W. Grimes }
16085b81b6b3SRodney W. Grimes 
1609aaf08d94SGarrett Wollman static int
1610f5f7ba03SJordan K. Hubbard retrier(fdcu)
1611f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
16125b81b6b3SRodney W. Grimes {
16135b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
16145b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
16155b81b6b3SRodney W. Grimes 
16165b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
16175b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
16185b81b6b3SRodney W. Grimes 
16193a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
16203a2f7427SDavid Greenman 		goto fail;
16215b81b6b3SRodney W. Grimes 	switch(fdc->retry)
16225b81b6b3SRodney W. Grimes 	{
16235b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
16245b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
16255b81b6b3SRodney W. Grimes 		break;
16265b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
16275b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
16285b81b6b3SRodney W. Grimes 		break;
16295b81b6b3SRodney W. Grimes 	case 6:
16305b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
16315b81b6b3SRodney W. Grimes 		break;
16325b81b6b3SRodney W. Grimes 	case 7:
16335b81b6b3SRodney W. Grimes 		break;
16345b81b6b3SRodney W. Grimes 	default:
16353a2f7427SDavid Greenman 	fail:
16365b81b6b3SRodney W. Grimes 		{
16377ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
16387ca0641bSAndrey A. Chernov 			/* Trick diskerr */
16393a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
16403a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
164192ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
16423a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
16433a2f7427SDavid Greenman 				(struct disklabel *)NULL);
16447ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
1645dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1646dc5df763SJoerg Wunsch 			{
1647dc5df763SJoerg Wunsch 				printf(
1648dc5df763SJoerg Wunsch 			" (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n",
1649dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1650dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1651dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1652dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1653dc5df763SJoerg Wunsch 				       fdc->status[5]);
1654dc5df763SJoerg Wunsch 			}
1655dc5df763SJoerg Wunsch 			else
1656dc5df763SJoerg Wunsch 				printf(" (No status)\n");
16575b81b6b3SRodney W. Grimes 		}
16585b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
16595b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
16605b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
166126f9a767SRodney W. Grimes 		dp->b_actf = bp->b_actf;
16625b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
16635b81b6b3SRodney W. Grimes 		biodone(bp);
166492ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
16655b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
16665b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1667f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
166892ed385aSRodney W. Grimes 		return(1);
16695b81b6b3SRodney W. Grimes 	}
16705b81b6b3SRodney W. Grimes 	fdc->retry++;
16715b81b6b3SRodney W. Grimes 	return(1);
16725b81b6b3SRodney W. Grimes }
16735b81b6b3SRodney W. Grimes 
1674b39c878eSAndrey A. Chernov static int
1675b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1676b39c878eSAndrey A. Chernov 	dev_t dev;
1677b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1678b39c878eSAndrey A. Chernov 	struct proc *p;
1679b39c878eSAndrey A. Chernov {
1680b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1681b39c878eSAndrey A. Chernov  	fd_p	fd;
1682b39c878eSAndrey A. Chernov 
1683b39c878eSAndrey A. Chernov 	struct buf *bp;
1684b39c878eSAndrey A. Chernov 	int rv = 0, s;
16853a2f7427SDavid Greenman 	size_t fdblk;
1686b39c878eSAndrey A. Chernov 
1687b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1688b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
16893a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1690b39c878eSAndrey A. Chernov 
1691b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1692b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1693b39c878eSAndrey A. Chernov 	if(bp == 0)
1694b39c878eSAndrey A. Chernov 		return ENOBUFS;
169582f5379bSJoerg Wunsch 	/*
169682f5379bSJoerg Wunsch 	 * keep the process from being swapped
169782f5379bSJoerg Wunsch 	 */
169882f5379bSJoerg Wunsch 	p->p_flag |= P_PHYSIO;
1699b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1700b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1701b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1702b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1703b39c878eSAndrey A. Chernov 
1704b39c878eSAndrey A. Chernov 	/*
1705b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1706b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1707b39c878eSAndrey A. Chernov 	 */
1708b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
17093a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1710b39c878eSAndrey A. Chernov 
1711b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
17125e235068SJordan K. Hubbard 	bp->b_un.b_addr = (caddr_t)finfo;
1713b39c878eSAndrey A. Chernov 
1714b39c878eSAndrey A. Chernov 	/* now do the format */
1715b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1716b39c878eSAndrey A. Chernov 
1717b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1718b39c878eSAndrey A. Chernov 	s = splbio();
1719b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1720b39c878eSAndrey A. Chernov 	{
17215e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1722b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1723b39c878eSAndrey A. Chernov 			break;
1724b39c878eSAndrey A. Chernov 	}
1725b39c878eSAndrey A. Chernov 	splx(s);
1726b39c878eSAndrey A. Chernov 
172782f5379bSJoerg Wunsch 	if(rv == EWOULDBLOCK) {
1728b39c878eSAndrey A. Chernov 		/* timed out */
1729b39c878eSAndrey A. Chernov 		rv = EIO;
173082f5379bSJoerg Wunsch 		biodone(bp);
173182f5379bSJoerg Wunsch 	}
17323a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
17333a2f7427SDavid Greenman 		rv = bp->b_error;
173482f5379bSJoerg Wunsch 	/*
173582f5379bSJoerg Wunsch 	 * allow the process to be swapped
173682f5379bSJoerg Wunsch 	 */
173782f5379bSJoerg Wunsch 	p->p_flag &= ~P_PHYSIO;
1738b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1739b39c878eSAndrey A. Chernov 	return rv;
1740b39c878eSAndrey A. Chernov }
1741b39c878eSAndrey A. Chernov 
1742f5f7ba03SJordan K. Hubbard /*
1743671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
1744f5f7ba03SJordan K. Hubbard  */
17455b81b6b3SRodney W. Grimes 
1746f5f7ba03SJordan K. Hubbard int
1747b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1748f5f7ba03SJordan K. Hubbard 	dev_t dev;
1749f5f7ba03SJordan K. Hubbard 	int cmd;
1750f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1751f5f7ba03SJordan K. Hubbard 	int flag;
1752b39c878eSAndrey A. Chernov 	struct proc *p;
1753f5f7ba03SJordan K. Hubbard {
17543a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
17553a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
17563a2f7427SDavid Greenman 	size_t fdblk;
17573a2f7427SDavid Greenman 
1758f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1759f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1760f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
17613a2f7427SDavid Greenman 	int error = 0;
1762f5f7ba03SJordan K. Hubbard 
1763b99f0a4aSAndrew Moore #if NFT > 0
1764a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1765a60eff27SNate Williams 
1766a60eff27SNate Williams 	/* check for a tape ioctl */
1767a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1768b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1769b99f0a4aSAndrew Moore #endif
1770b99f0a4aSAndrew Moore 
17713a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1772f5f7ba03SJordan K. Hubbard 
1773f5f7ba03SJordan K. Hubbard 	switch (cmd)
1774f5f7ba03SJordan K. Hubbard 	{
1775f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1776f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1777f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
17783a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
177992ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1780f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1781f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1782f5f7ba03SJordan K. Hubbard 
178354c7241bSJordan K. Hubbard 		if (readdisklabel(dev, fdstrategy, dl, NULL, 0) == NULL)
1784f5f7ba03SJordan K. Hubbard 			error = 0;
1785f5f7ba03SJordan K. Hubbard 		else
1786f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1787f5f7ba03SJordan K. Hubbard 
1788f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1789f5f7ba03SJordan K. Hubbard 		break;
1790f5f7ba03SJordan K. Hubbard 
1791f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1792f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1793f5f7ba03SJordan K. Hubbard 			error = EBADF;
1794f5f7ba03SJordan K. Hubbard 		break;
1795f5f7ba03SJordan K. Hubbard 
1796f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1797f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1798f5f7ba03SJordan K. Hubbard 			error = EBADF;
1799f5f7ba03SJordan K. Hubbard 		break;
1800f5f7ba03SJordan K. Hubbard 
1801f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1802f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1803f5f7ba03SJordan K. Hubbard 		{
1804f5f7ba03SJordan K. Hubbard 			error = EBADF;
1805f5f7ba03SJordan K. Hubbard 			break;
1806f5f7ba03SJordan K. Hubbard 		}
1807f5f7ba03SJordan K. Hubbard 
1808f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1809f5f7ba03SJordan K. Hubbard 
18103a2f7427SDavid Greenman 		if ((error =
181154c7241bSJordan K. Hubbard 		     setdisklabel ((struct disklabel *)buffer, dl, 0)))
1812f5f7ba03SJordan K. Hubbard 			break;
1813f5f7ba03SJordan K. Hubbard 
1814b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
181554c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
1816b39c878eSAndrey A. Chernov 		break;
1817b39c878eSAndrey A. Chernov 
1818b39c878eSAndrey A. Chernov 	case FD_FORM:
1819b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1820b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1821b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1822b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1823b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1824b39c878eSAndrey A. Chernov 		else
1825b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1826b39c878eSAndrey A. Chernov 		break;
1827b39c878eSAndrey A. Chernov 
1828b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1829b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1830f5f7ba03SJordan K. Hubbard 		break;
1831f5f7ba03SJordan K. Hubbard 
18323a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
18333a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
18343a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
18353a2f7427SDavid Greenman 			return EPERM;
18363a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
18373a2f7427SDavid Greenman 		break;
18383a2f7427SDavid Greenman 
18393a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
18403a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
18413a2f7427SDavid Greenman 		break;
18423a2f7427SDavid Greenman 
18433a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
18443a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
18453a2f7427SDavid Greenman 		break;
18463a2f7427SDavid Greenman 
1847f5f7ba03SJordan K. Hubbard 	default:
18483a2f7427SDavid Greenman 		error = ENOTTY;
1849f5f7ba03SJordan K. Hubbard 		break;
1850f5f7ba03SJordan K. Hubbard 	}
1851f5f7ba03SJordan K. Hubbard 	return (error);
1852f5f7ba03SJordan K. Hubbard }
1853f5f7ba03SJordan K. Hubbard 
1854f5f7ba03SJordan K. Hubbard #endif
18553a2f7427SDavid Greenman /*
18563a2f7427SDavid Greenman  * Hello emacs, these are the
18573a2f7427SDavid Greenman  * Local Variables:
18583a2f7427SDavid Greenman  *  c-indent-level:               8
18593a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
18603a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
18613a2f7427SDavid Greenman  *  c-brace-offset:              -8
18623a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
18633a2f7427SDavid Greenman  *  c-argdecl-indent:             8
18643a2f7427SDavid Greenman  *  c-label-offset:              -8
18653a2f7427SDavid Greenman  *  c++-hanging-braces:           1
18663a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
18673a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
18683a2f7427SDavid Greenman  *  c++-friend-offset:            0
18693a2f7427SDavid Greenman  * End:
18703a2f7427SDavid Greenman  */
1871