xref: /freebsd/sys/dev/fdc/fdc.c (revision dc5df763e473f6204f45e438a84477b0f66668ed)
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  *
8dc5df763SJoerg Wunsch  * Portions Copyright (c) 1993, 1994, 1995 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)
123a2f7427SDavid Greenman  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
13dc5df763SJoerg Wunsch  *  dufault@hda.com (Peter Dufault)
143a2f7427SDavid Greenman  *
155b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
165b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
175b81b6b3SRodney W. Grimes  * are met:
185b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
195b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
205b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
215b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
225b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
235b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
245b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
255b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
265b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
275b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
285b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
295b81b6b3SRodney W. Grimes  *    without specific prior written permission.
305b81b6b3SRodney W. Grimes  *
315b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
325b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
335b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
345b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
355b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
365b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
375b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
385b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
395b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
405b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
415b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
425b81b6b3SRodney W. Grimes  *
43dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
44dc5df763SJoerg Wunsch  *	$Id: fd.c,v 1.46 1994/12/04 20:22:19 joerg Exp $
455b81b6b3SRodney W. Grimes  *
465b81b6b3SRodney W. Grimes  */
475b81b6b3SRodney W. Grimes 
48b99f0a4aSAndrew Moore #include "ft.h"
49b99f0a4aSAndrew Moore #if NFT < 1
50b99f0a4aSAndrew Moore #undef NFDC
51b99f0a4aSAndrew Moore #endif
525b81b6b3SRodney W. Grimes #include "fd.h"
535b81b6b3SRodney W. Grimes 
5485803790SJordan K. Hubbard /* Flags */
551ca6f46dSJordan K. Hubbard #define FT_PROBE		0x1
5685803790SJordan K. Hubbard 
57b99f0a4aSAndrew Moore #if NFDC > 0
58b99f0a4aSAndrew Moore 
59b99f0a4aSAndrew Moore #include <sys/param.h>
60b99f0a4aSAndrew Moore #include <sys/dkbad.h>
61b99f0a4aSAndrew Moore #include <sys/systm.h>
62b99f0a4aSAndrew Moore #include <sys/kernel.h>
63b99f0a4aSAndrew Moore #include <sys/conf.h>
64b99f0a4aSAndrew Moore #include <sys/file.h>
65b99f0a4aSAndrew Moore #include <sys/ioctl.h>
66b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h>
67b99f0a4aSAndrew Moore #include <sys/disklabel.h>
68b99f0a4aSAndrew Moore #include <sys/buf.h>
69b99f0a4aSAndrew Moore #include <sys/uio.h>
70b99f0a4aSAndrew Moore #include <sys/malloc.h>
713a2f7427SDavid Greenman #include <sys/proc.h>
72b99f0a4aSAndrew Moore #include <sys/syslog.h>
7392200632SGarrett Wollman #include <sys/devconf.h>
7492200632SGarrett Wollman #include <sys/dkstat.h>
75f540b106SGarrett Wollman #include <i386/isa/isa.h>
76f540b106SGarrett Wollman #include <i386/isa/isa_device.h>
77f540b106SGarrett Wollman #include <i386/isa/fdreg.h>
78f540b106SGarrett Wollman #include <i386/isa/fdc.h>
79f540b106SGarrett Wollman #include <i386/isa/rtc.h>
80dc5df763SJoerg Wunsch #include <machine/stdarg.h>
8187eafbcaSPoul-Henning Kamp #if NFT > 0
8287eafbcaSPoul-Henning Kamp #include <sys/ftape.h>
8387eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h>
8487eafbcaSPoul-Henning Kamp #endif
855b81b6b3SRodney W. Grimes 
8692200632SGarrett Wollman static int fd_goaway(struct kern_devconf *, int);
8792200632SGarrett Wollman static int fdc_goaway(struct kern_devconf *, int);
885e235068SJordan K. Hubbard static int fd_externalize(struct proc *, struct kern_devconf *, void *, size_t);
895e235068SJordan K. Hubbard 
905e235068SJordan K. Hubbard /*
915e235068SJordan K. Hubbard  * Templates for the kern_devconf structures used when we attach.
925e235068SJordan K. Hubbard  */
935e235068SJordan K. Hubbard static struct kern_devconf kdc_fd[NFD] = { {
945e235068SJordan K. Hubbard 	0, 0, 0,		/* filled in by kern_devconf.c */
955e235068SJordan K. Hubbard 	"fd", 0, { MDDT_DISK, 0 },
965e235068SJordan K. Hubbard 	fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN,
975e235068SJordan K. Hubbard 	0,			/* parent */
985e235068SJordan K. Hubbard 	0,			/* parentdata */
995e235068SJordan K. Hubbard 	DC_UNKNOWN,		/* state */
1005e235068SJordan K. Hubbard 	"floppy disk"
1015e235068SJordan K. Hubbard } };
1025e235068SJordan K. Hubbard 
1035e235068SJordan K. Hubbard struct kern_devconf kdc_fdc[NFDC] = { {
1045e235068SJordan K. Hubbard 	0, 0, 0,		/* filled in by kern_devconf.c */
1055e235068SJordan K. Hubbard 	"fdc", 0, { MDDT_ISA, 0, "bio" },
1065e235068SJordan K. Hubbard 	isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN,
1075e235068SJordan K. Hubbard 	0,			/* parent */
1085e235068SJordan K. Hubbard 	0,			/* parentdata */
1095e235068SJordan K. Hubbard 	DC_UNKNOWN,		/* state */
1105e235068SJordan K. Hubbard 	"floppy disk/tape controller"
1115e235068SJordan K. Hubbard } };
1125e235068SJordan K. Hubbard 
1135e235068SJordan K. Hubbard static inline void
1145e235068SJordan K. Hubbard fd_registerdev(int ctlr, int unit)
1155e235068SJordan K. Hubbard {
1165e235068SJordan K. Hubbard 	if(unit != 0)
1175e235068SJordan K. Hubbard 		kdc_fd[unit] = kdc_fd[0];
1185e235068SJordan K. Hubbard 
1195e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_unit = unit;
1205e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_parent = &kdc_fdc[ctlr];
1215e235068SJordan K. Hubbard 	kdc_fd[unit].kdc_parentdata = 0;
1225e235068SJordan K. Hubbard 	dev_attach(&kdc_fd[unit]);
1235e235068SJordan K. Hubbard }
1245e235068SJordan K. Hubbard 
1255e235068SJordan K. Hubbard static inline void
1265e235068SJordan K. Hubbard fdc_registerdev(struct isa_device *dvp)
1275e235068SJordan K. Hubbard {
1285e235068SJordan K. Hubbard 	int unit = dvp->id_unit;
1295e235068SJordan K. Hubbard 
1305e235068SJordan K. Hubbard 	if(unit != 0)
1315e235068SJordan K. Hubbard 		kdc_fdc[unit] = kdc_fdc[0];
1325e235068SJordan K. Hubbard 
1335e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_unit = unit;
1345e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_parent = &kdc_isa0;
1355e235068SJordan K. Hubbard 	kdc_fdc[unit].kdc_parentdata = dvp;
1365e235068SJordan K. Hubbard 	dev_attach(&kdc_fdc[unit]);
1375e235068SJordan K. Hubbard }
1385e235068SJordan K. Hubbard 
1395e235068SJordan K. Hubbard static int
1405e235068SJordan K. Hubbard fdc_goaway(struct kern_devconf *kdc, int force)
1415e235068SJordan K. Hubbard {
1425e235068SJordan K. Hubbard 	if(force) {
1435e235068SJordan K. Hubbard 		dev_detach(kdc);
1445e235068SJordan K. Hubbard 		return 0;
1455e235068SJordan K. Hubbard 	} else {
1465e235068SJordan K. Hubbard 		return EBUSY;	/* XXX fix */
1475e235068SJordan K. Hubbard 	}
1485e235068SJordan K. Hubbard }
1495e235068SJordan K. Hubbard 
1505e235068SJordan K. Hubbard static int
1515e235068SJordan K. Hubbard fd_goaway(struct kern_devconf *kdc, int force)
1525e235068SJordan K. Hubbard {
1535e235068SJordan K. Hubbard 	dev_detach(kdc);
1545e235068SJordan K. Hubbard 	return 0;
1555e235068SJordan K. Hubbard }
15692200632SGarrett Wollman 
157b2be795bSAndrey A. Chernov #define RAW_PART 2
1585b81b6b3SRodney W. Grimes #define b_cylin b_resid
1595b81b6b3SRodney W. Grimes 
160b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
161b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
1625b81b6b3SRodney W. Grimes 
1633a2f7427SDavid Greenman /*
1643a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
1653a2f7427SDavid Greenman  * on the controller
1663a2f7427SDavid Greenman  */
1673a2f7427SDavid Greenman #define id_physid id_scsiid
1683a2f7427SDavid Greenman 
169dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
170dc5df763SJoerg Wunsch #define FD_FAILED -1
171dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
172dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
173dc5df763SJoerg Wunsch 
174b39c878eSAndrey A. Chernov #define NUMTYPES 14
175b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1767ca0641bSAndrey A. Chernov 
1773a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
178b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
179b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
180b99f0a4aSAndrew Moore #define FD_1720         1
181b99f0a4aSAndrew Moore #define FD_1480         2
182b99f0a4aSAndrew Moore #define FD_1440         3
183b99f0a4aSAndrew Moore #define FD_1200         4
184b99f0a4aSAndrew Moore #define FD_820          5
185b99f0a4aSAndrew Moore #define FD_800          6
186b99f0a4aSAndrew Moore #define FD_720          7
187b99f0a4aSAndrew Moore #define FD_360          8
188ed2fa05eSAndrey A. Chernov 
189b99f0a4aSAndrew Moore #define FD_1480in5_25   9
190b99f0a4aSAndrew Moore #define FD_1440in5_25   10
191b99f0a4aSAndrew Moore #define FD_820in5_25    11
192b99f0a4aSAndrew Moore #define FD_800in5_25    12
193b99f0a4aSAndrew Moore #define FD_720in5_25    13
194b99f0a4aSAndrew Moore #define FD_360in5_25    14
195b99f0a4aSAndrew Moore 
1967ca0641bSAndrey A. Chernov 
1975b81b6b3SRodney W. Grimes struct fd_type fd_types[NUMTYPES] =
1985b81b6b3SRodney W. Grimes {
199126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
200126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
201126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
202126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
203126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
204126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
205126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
206b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
207ed2fa05eSAndrey A. Chernov 
208126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
209126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
210126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
211126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
212126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
213126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
2145b81b6b3SRodney W. Grimes };
2155b81b6b3SRodney W. Grimes 
216b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
2175b81b6b3SRodney W. Grimes /***********************************************************************\
2185b81b6b3SRodney W. Grimes * Per controller structure.						*
2195b81b6b3SRodney W. Grimes \***********************************************************************/
220b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
2215b81b6b3SRodney W. Grimes 
2225b81b6b3SRodney W. Grimes /***********************************************************************\
2235b81b6b3SRodney W. Grimes * Per drive structure.							*
224b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
2255b81b6b3SRodney W. Grimes \***********************************************************************/
2265b81b6b3SRodney W. Grimes struct fd_data {
227b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2285b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2293a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
2305b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
2315b81b6b3SRodney W. Grimes 	int	flags;
2325b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
2335b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
2345b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
2355b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
2365b81b6b3SRodney W. Grimes 	int	skip;
2375b81b6b3SRodney W. Grimes 	int	hddrv;
238dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
2395b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
2403a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
24192200632SGarrett Wollman 	int	dkunit;		/* disk stats unit number */
2425b81b6b3SRodney W. Grimes } fd_data[NFD];
2435b81b6b3SRodney W. Grimes 
2445b81b6b3SRodney W. Grimes /***********************************************************************\
2455b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
2465b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
2475b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
2485b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
2495b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
2505b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
2515b81b6b3SRodney W. Grimes \***********************************************************************/
252b99f0a4aSAndrew Moore 
2533a2f7427SDavid Greenman #if NFT > 0
2543a2f7427SDavid Greenman int ftopen(dev_t, int);
2553a2f7427SDavid Greenman int ftintr(ftu_t ftu);
2563a2f7427SDavid Greenman int ftclose(dev_t, int);
2573a2f7427SDavid Greenman void ftstrategy(struct buf *);
2583a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
2593a2f7427SDavid Greenman int ftdump(dev_t);
2603a2f7427SDavid Greenman int ftsize(dev_t);
2613a2f7427SDavid Greenman int ftattach(struct isa_device *, struct isa_device *);
2623a2f7427SDavid Greenman #endif
2635b81b6b3SRodney W. Grimes 
2643a2f7427SDavid Greenman /* autoconfig functions */
2653a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2663a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2673a2f7427SDavid Greenman 
2683a2f7427SDavid Greenman /* exported functions */
2693a2f7427SDavid Greenman int fdsize (dev_t);
2703a2f7427SDavid Greenman void fdintr(fdcu_t);
2713a2f7427SDavid Greenman int Fdopen(dev_t, int);
2723a2f7427SDavid Greenman int fdclose(dev_t, int);
2733a2f7427SDavid Greenman void fdstrategy(struct buf *);
2743a2f7427SDavid Greenman int fdioctl(dev_t, int, caddr_t, int, struct proc *);
2753a2f7427SDavid Greenman 
2763a2f7427SDavid Greenman /* needed for ft driver, thus exported */
2773a2f7427SDavid Greenman int in_fdc(fdcu_t);
2783a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
2793a2f7427SDavid Greenman 
2803a2f7427SDavid Greenman /* internal functions */
2813a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
2823a2f7427SDavid Greenman #  define TURNON 1
2833a2f7427SDavid Greenman #  define TURNOFF 0
2843a2f7427SDavid Greenman static timeout_t fd_turnoff;
2853a2f7427SDavid Greenman static timeout_t fd_motor_on;
2863a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2873a2f7427SDavid Greenman static void fdc_reset(fdc_p);
2883a2f7427SDavid Greenman static void fdstart(fdcu_t);
2893a2f7427SDavid Greenman static timeout_t fd_timeout;
2903a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2913a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
292aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2933a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2943a2f7427SDavid Greenman 
295aaf08d94SGarrett Wollman 
2965b81b6b3SRodney W. Grimes #define DEVIDLE		0
2975b81b6b3SRodney W. Grimes #define FINDWORK	1
2985b81b6b3SRodney W. Grimes #define	DOSEEK		2
2995b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
3005b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
3015b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
3025b81b6b3SRodney W. Grimes #define	STARTRECAL	6
3035b81b6b3SRodney W. Grimes #define	RESETCTLR	7
3045b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
3055b81b6b3SRodney W. Grimes #define	RECALWAIT	9
3065b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
3075b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
3085b81b6b3SRodney W. Grimes 
3095b81b6b3SRodney W. Grimes #ifdef	DEBUG
3105b81b6b3SRodney W. Grimes char *fdstates[] =
3115b81b6b3SRodney W. Grimes {
3125b81b6b3SRodney W. Grimes "DEVIDLE",
3135b81b6b3SRodney W. Grimes "FINDWORK",
3145b81b6b3SRodney W. Grimes "DOSEEK",
3155b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
3165b81b6b3SRodney W. Grimes "IOCOMPLETE",
3175b81b6b3SRodney W. Grimes "RECALCOMPLETE",
3185b81b6b3SRodney W. Grimes "STARTRECAL",
3195b81b6b3SRodney W. Grimes "RESETCTLR",
3205b81b6b3SRodney W. Grimes "SEEKWAIT",
3215b81b6b3SRodney W. Grimes "RECALWAIT",
3225b81b6b3SRodney W. Grimes "MOTORWAIT",
3235b81b6b3SRodney W. Grimes "IOTIMEDOUT"
3245b81b6b3SRodney W. Grimes };
3255b81b6b3SRodney W. Grimes 
3263a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
3273a2f7427SDavid Greenman int	fd_debug = 0;
3285b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
3295b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
330381fe1aaSGarrett Wollman #else /* DEBUG */
3315b81b6b3SRodney W. Grimes #define TRACE0(arg)
3325b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
333381fe1aaSGarrett Wollman #endif /* DEBUG */
3345b81b6b3SRodney W. Grimes 
33592200632SGarrett Wollman struct isa_device *fdcdevs[NFDC];
33692200632SGarrett Wollman 
33792200632SGarrett Wollman /*
33892200632SGarrett Wollman  * Provide hw.devconf information.
33992200632SGarrett Wollman  */
34092200632SGarrett Wollman static int
3415e235068SJordan K. Hubbard fd_externalize(struct proc *p, struct kern_devconf *kdc, void *userp, size_t len)
34292200632SGarrett Wollman {
34392200632SGarrett Wollman 	return disk_externalize(fd_data[kdc->kdc_unit].fdsu, userp, &len);
34492200632SGarrett Wollman }
34592200632SGarrett Wollman 
34692200632SGarrett Wollman static int
3475e235068SJordan K. Hubbard fdc_externalize(struct proc *p, struct kern_devconf *kdc, void *userp, size_t len)
34892200632SGarrett Wollman {
34992200632SGarrett Wollman 	return isa_externalize(fdcdevs[kdc->kdc_unit], userp, &len);
35092200632SGarrett Wollman }
35192200632SGarrett Wollman 
352dc5df763SJoerg Wunsch static int
353dc5df763SJoerg Wunsch fdc_err(fdcu_t fdcu, const char *s)
354dc5df763SJoerg Wunsch {
355dc5df763SJoerg Wunsch 	fdc_data[fdcu].fdc_errs++;
356dc5df763SJoerg Wunsch 	if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX)
357dc5df763SJoerg Wunsch 		printf("fdc%d: %s: ", fdcu, s);
358dc5df763SJoerg Wunsch 	else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX)
359dc5df763SJoerg Wunsch 		printf("fdc%d: too many errors, not logging any more\n",
360dc5df763SJoerg Wunsch 		       fdcu);
361dc5df763SJoerg Wunsch 
362dc5df763SJoerg Wunsch 	return FD_FAILED;
363dc5df763SJoerg Wunsch }
364dc5df763SJoerg Wunsch 
365dc5df763SJoerg Wunsch /*
366dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
367dc5df763SJoerg Wunsch  * Unit number,
368dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
369dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
370dc5df763SJoerg Wunsch  */
371dc5df763SJoerg Wunsch 
372dc5df763SJoerg Wunsch int
373dc5df763SJoerg Wunsch fd_cmd(fdcu_t fdcu, int n_out, ...)
374dc5df763SJoerg Wunsch {
375dc5df763SJoerg Wunsch 	u_char cmd;
376dc5df763SJoerg Wunsch 	int n_in;
377dc5df763SJoerg Wunsch 	int n;
378dc5df763SJoerg Wunsch 	va_list ap;
379dc5df763SJoerg Wunsch 
380dc5df763SJoerg Wunsch 	va_start(ap, n_out);
381dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
382dc5df763SJoerg Wunsch 	va_end(ap);
383dc5df763SJoerg Wunsch 	va_start(ap, n_out);
384dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
385dc5df763SJoerg Wunsch 	{
386dc5df763SJoerg Wunsch 		if (out_fdc(fdcu, va_arg(ap, int)) < 0)
387dc5df763SJoerg Wunsch 		{
388dc5df763SJoerg Wunsch 			char msg[50];
389dc5df763SJoerg Wunsch 			sprintf(msg,
390dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
391dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
392dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
393dc5df763SJoerg Wunsch 		}
394dc5df763SJoerg Wunsch 	}
395dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
396dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
397dc5df763SJoerg Wunsch 	{
398dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
399dc5df763SJoerg Wunsch 		if (fd_in(fdcu, ptr) < 0)
400dc5df763SJoerg Wunsch 		{
401dc5df763SJoerg Wunsch 			char msg[50];
402dc5df763SJoerg Wunsch 			sprintf(msg,
403dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
404dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
405dc5df763SJoerg Wunsch 			return fdc_err(fdcu, msg);
406dc5df763SJoerg Wunsch 		}
407dc5df763SJoerg Wunsch 	}
408dc5df763SJoerg Wunsch 
409dc5df763SJoerg Wunsch 	return 0;
410dc5df763SJoerg Wunsch }
411dc5df763SJoerg Wunsch 
412dc5df763SJoerg Wunsch int
413dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
414dc5df763SJoerg Wunsch {
415dc5df763SJoerg Wunsch 	int st3;
416dc5df763SJoerg Wunsch 
417dc5df763SJoerg Wunsch 	if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
418dc5df763SJoerg Wunsch 	{
419dc5df763SJoerg Wunsch 		return fdc_err(fdc->fdcu, "Sense Drive Status failed.\n");
420dc5df763SJoerg Wunsch 	}
421dc5df763SJoerg Wunsch 	if (st3p)
422dc5df763SJoerg Wunsch 		*st3p = st3;
423dc5df763SJoerg Wunsch 
424dc5df763SJoerg Wunsch 	return 0;
425dc5df763SJoerg Wunsch }
426dc5df763SJoerg Wunsch 
427dc5df763SJoerg Wunsch int
428dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
429dc5df763SJoerg Wunsch {
430dc5df763SJoerg Wunsch 	int st0, cyl;
431dc5df763SJoerg Wunsch 
432dc5df763SJoerg Wunsch 	int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0);
433dc5df763SJoerg Wunsch 
434dc5df763SJoerg Wunsch 	if (ret)
435dc5df763SJoerg Wunsch 	{
436dc5df763SJoerg Wunsch 		(void)fdc_err(fdc->fdcu,
437dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
438dc5df763SJoerg Wunsch 		return ret;
439dc5df763SJoerg Wunsch 	}
440dc5df763SJoerg Wunsch 
441dc5df763SJoerg Wunsch 	if (st0p)
442dc5df763SJoerg Wunsch 		*st0p = st0;
443dc5df763SJoerg Wunsch 
444dc5df763SJoerg Wunsch 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV)
445dc5df763SJoerg Wunsch 	{
446dc5df763SJoerg Wunsch 		/*
447dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
448dc5df763SJoerg Wunsch 		 */
449dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
450dc5df763SJoerg Wunsch 	}
451dc5df763SJoerg Wunsch 
452dc5df763SJoerg Wunsch 	if (fd_in(fdc->fdcu, &cyl) < 0)
453dc5df763SJoerg Wunsch 	{
454dc5df763SJoerg Wunsch 		return fdc_err(fdc->fdcu, "can't get cyl num\n");
455dc5df763SJoerg Wunsch 	}
456dc5df763SJoerg Wunsch 
457dc5df763SJoerg Wunsch 	if (cylp)
458dc5df763SJoerg Wunsch 		*cylp = cyl;
459dc5df763SJoerg Wunsch 
460dc5df763SJoerg Wunsch 	return 0;
461dc5df763SJoerg Wunsch }
462dc5df763SJoerg Wunsch 
463dc5df763SJoerg Wunsch 
464dc5df763SJoerg Wunsch int
465dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
466dc5df763SJoerg Wunsch {
467dc5df763SJoerg Wunsch 	int i, ret;
468dc5df763SJoerg Wunsch 	for (i = 0; i < 7; i++)
469dc5df763SJoerg Wunsch 	{
470dc5df763SJoerg Wunsch 		if ((ret = fd_in(fdc->fdcu, fdc->status + i)))
471dc5df763SJoerg Wunsch 			break;
472dc5df763SJoerg Wunsch 	}
473dc5df763SJoerg Wunsch 
474dc5df763SJoerg Wunsch 	if (ret == 0)
475dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
476dc5df763SJoerg Wunsch 	else
477dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
478dc5df763SJoerg Wunsch 
479dc5df763SJoerg Wunsch 	return ret;
480dc5df763SJoerg Wunsch }
481dc5df763SJoerg Wunsch 
4825b81b6b3SRodney W. Grimes /****************************************************************************/
4835b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
4845b81b6b3SRodney W. Grimes /****************************************************************************/
485b99f0a4aSAndrew Moore struct	isa_driver fdcdriver = {
486b99f0a4aSAndrew Moore 	fdprobe, fdattach, "fdc",
4875b81b6b3SRodney W. Grimes };
4885b81b6b3SRodney W. Grimes 
489dc5df763SJoerg Wunsch 
4905b81b6b3SRodney W. Grimes /*
4915b81b6b3SRodney W. Grimes  * probe for existance of controller
4925b81b6b3SRodney W. Grimes  */
4933a2f7427SDavid Greenman static int
494dc5df763SJoerg Wunsch fdprobe(struct isa_device *dev)
4955b81b6b3SRodney W. Grimes {
4965b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
4975b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
4985b81b6b3SRodney W. Grimes 	{
4995b81b6b3SRodney W. Grimes 		printf("fdc: same unit (%d) used multiple times\n", fdcu);
5005b81b6b3SRodney W. Grimes 		return 0;
5015b81b6b3SRodney W. Grimes 	}
5025b81b6b3SRodney W. Grimes 
50392200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
5045b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
5055b81b6b3SRodney W. Grimes 
50616111cedSAndrew Moore 	/* First - lets reset the floppy controller */
5073a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
50816111cedSAndrew Moore 	DELAY(100);
5093a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
51016111cedSAndrew Moore 
5115b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
512dc5df763SJoerg Wunsch 	if (fd_cmd(fdcu,
513dc5df763SJoerg Wunsch 		   3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
514dc5df763SJoerg Wunsch 		   0))
5155b81b6b3SRodney W. Grimes 	{
5165b81b6b3SRodney W. Grimes 		return(0);
5175b81b6b3SRodney W. Grimes 	}
5185b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
5195b81b6b3SRodney W. Grimes }
5205b81b6b3SRodney W. Grimes 
5215b81b6b3SRodney W. Grimes /*
5225b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
5235b81b6b3SRodney W. Grimes  */
5243a2f7427SDavid Greenman static int
525dc5df763SJoerg Wunsch fdattach(struct isa_device *dev)
5265b81b6b3SRodney W. Grimes {
5273a2f7427SDavid Greenman 	unsigned fdt;
5285b81b6b3SRodney W. Grimes 	fdu_t	fdu;
5295b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
5305b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
5315b81b6b3SRodney W. Grimes 	fd_p	fd;
532dc5df763SJoerg Wunsch 	int	fdsu, st0, st3, i;
533b99f0a4aSAndrew Moore 	struct isa_device *fdup;
534dc5df763SJoerg Wunsch 	int ic_type = 0;
53592200632SGarrett Wollman 
5362f86936aSGarrett Wollman 	fdc_registerdev(dev);
5375b81b6b3SRodney W. Grimes 
5385b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
5395b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
5405b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
5415b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
5423a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
5433a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
544b99f0a4aSAndrew Moore 	printf("fdc%d: ", fdcu);
5455b81b6b3SRodney W. Grimes 
5465b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
547b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
548b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
549b99f0a4aSAndrew Moore 			continue;
550b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
551b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
552b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
553b99f0a4aSAndrew Moore 			continue;
554b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
555b99f0a4aSAndrew Moore 		/* look up what bios thinks we have */
556b99f0a4aSAndrew Moore 		switch (fdu) {
557b99f0a4aSAndrew Moore 			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
558b99f0a4aSAndrew Moore 				break;
559b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
560b99f0a4aSAndrew Moore 				break;
561b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
562b99f0a4aSAndrew Moore 				break;
563b99f0a4aSAndrew Moore 		}
5645b81b6b3SRodney W. Grimes 		/* is there a unit? */
565b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
566b99f0a4aSAndrew Moore #if NFT > 0
567b99f0a4aSAndrew Moore 		|| (fdsu >= DRVS_PER_CTLR)) {
568b99f0a4aSAndrew Moore #else
569b99f0a4aSAndrew Moore 		) {
57056ef0285SAndrew Moore 			fd->type = NO_TYPE;
571b99f0a4aSAndrew Moore #endif
572b99f0a4aSAndrew Moore #if NFT > 0
573b99f0a4aSAndrew Moore 			/* If BIOS says no floppy, or > 2nd device */
574b99f0a4aSAndrew Moore 			/* Probe for and attach a floppy tape.     */
5751ca6f46dSJordan K. Hubbard 			if ((dev->id_flags & FT_PROBE) && ftattach(dev, fdup))
576b99f0a4aSAndrew Moore 				continue;
57756ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
578b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
57956ef0285SAndrew Moore #endif
5805b81b6b3SRodney W. Grimes 			continue;
581f5f7ba03SJordan K. Hubbard 		}
5825b81b6b3SRodney W. Grimes 
5835b81b6b3SRodney W. Grimes 		/* select it */
5843a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
5856b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
586dc5df763SJoerg Wunsch 
587dc5df763SJoerg Wunsch 		if (ic_type == 0 &&
588dc5df763SJoerg Wunsch 		    fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0)
589dc5df763SJoerg Wunsch 		{
590dc5df763SJoerg Wunsch 			ic_type = (u_char)ic_type;
591dc5df763SJoerg Wunsch 			switch( ic_type ) {
592dc5df763SJoerg Wunsch 			case 0x80:
593dc5df763SJoerg Wunsch 				printf("(NEC 765)");
594dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE765;
595dc5df763SJoerg Wunsch 				break;
596dc5df763SJoerg Wunsch 			case 0x81:
597dc5df763SJoerg Wunsch 				printf("(Intel 82077)");
598dc5df763SJoerg Wunsch 				fdc->fdct = FDC_I82077;
599dc5df763SJoerg Wunsch 				break;
600dc5df763SJoerg Wunsch 			case 0x90:
601dc5df763SJoerg Wunsch 				printf("(NEC 72065B)");
602dc5df763SJoerg Wunsch 				fdc->fdct = FDC_NE72065;
603dc5df763SJoerg Wunsch 				break;
604dc5df763SJoerg Wunsch 			default:
605dc5df763SJoerg Wunsch 				printf("(unknown IC type %02x)", ic_type);
606dc5df763SJoerg Wunsch 				fdc->fdct = FDC_UNKNOWN;
607dc5df763SJoerg Wunsch 				break;
6086b7bd95bSJoerg Wunsch 			}
609dc5df763SJoerg Wunsch 		}
610dc5df763SJoerg Wunsch 		if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) &&
611dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
612dc5df763SJoerg Wunsch 			/* if at track 0, first seek inwards */
613dc5df763SJoerg Wunsch 			/* seek some steps: */
614dc5df763SJoerg Wunsch 			(void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0);
615dc5df763SJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
616dc5df763SJoerg Wunsch 			(void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
617dc5df763SJoerg Wunsch 		}
618dc5df763SJoerg Wunsch 
619dc5df763SJoerg Wunsch 		/* If we're at track 0 first seek inwards. */
620dc5df763SJoerg Wunsch 		if ((fd_sense_drive_status(fdc, &st3) == 0) &&
621dc5df763SJoerg Wunsch 		    (st3 & NE7_ST3_T0)) {
622dc5df763SJoerg Wunsch 			/* Seek some steps... */
623dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
624dc5df763SJoerg Wunsch 				/* ...wait a moment... */
625dc5df763SJoerg Wunsch 				DELAY(300000);
626dc5df763SJoerg Wunsch 				/* make ctrlr happy: */
627dc5df763SJoerg Wunsch 				(void)fd_sense_int(fdc, 0, 0);
628dc5df763SJoerg Wunsch 			}
629dc5df763SJoerg Wunsch 		}
630dc5df763SJoerg Wunsch 
6316b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
6326b7bd95bSJoerg Wunsch 			/*
6336b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
6346b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
6356b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
6366b7bd95bSJoerg Wunsch 			 * more than 77 steps
6376b7bd95bSJoerg Wunsch 			 */
638dc5df763SJoerg Wunsch 			/* go back to 0: */
639dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
6406b7bd95bSJoerg Wunsch 				/* a second being enough for full stroke seek*/
6416b7bd95bSJoerg Wunsch 				DELAY(i == 0? 1000000: 300000);
6425b81b6b3SRodney W. Grimes 
6436b7bd95bSJoerg Wunsch 				/* anything responding? */
644dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, 0) == 0 &&
645dc5df763SJoerg Wunsch 				(st0 & NE7_ST0_EC) == 0)
6466b7bd95bSJoerg Wunsch 					break; /* already probed succesfully */
6476b7bd95bSJoerg Wunsch 			}
648dc5df763SJoerg Wunsch 		}
6496b7bd95bSJoerg Wunsch 
6503a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
6513a2f7427SDavid Greenman 
6523a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
6535b81b6b3SRodney W. Grimes 			continue;
6545b81b6b3SRodney W. Grimes 
655dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
656b99f0a4aSAndrew Moore 		fd->fdc = fdc;
657b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
6583a2f7427SDavid Greenman 		fd->options = 0;
659b99f0a4aSAndrew Moore 		printf(" [%d: fd%d: ", fdsu, fdu);
6605b81b6b3SRodney W. Grimes 
661b99f0a4aSAndrew Moore 		switch (fdt) {
6627ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
663b99f0a4aSAndrew Moore 			printf("1.2MB 5.25in]");
664b99f0a4aSAndrew Moore 			fd->type = FD_1200;
6657ca0641bSAndrey A. Chernov 			break;
6667ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
667b99f0a4aSAndrew Moore 			printf("1.44MB 3.5in]");
668b99f0a4aSAndrew Moore 			fd->type = FD_1440;
6697ca0641bSAndrey A. Chernov 			break;
670290dd077SJoerg Wunsch 		case RTCFDT_288M:
671290dd077SJoerg Wunsch 			printf("2.88MB 3.5in - 1.44MB mode]");
672290dd077SJoerg Wunsch 			fd->type = FD_1440;
673290dd077SJoerg Wunsch 			break;
6747ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
675b99f0a4aSAndrew Moore 			printf("360KB 5.25in]");
676b99f0a4aSAndrew Moore 			fd->type = FD_360;
6777ca0641bSAndrey A. Chernov 			break;
678ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
679b99f0a4aSAndrew Moore 			printf("720KB 3.5in]");
680b99f0a4aSAndrew Moore 			fd->type = FD_720;
681ed2fa05eSAndrey A. Chernov 			break;
6827ca0641bSAndrey A. Chernov 		default:
683b99f0a4aSAndrew Moore 			printf("unknown]");
684b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
6857ca0641bSAndrey A. Chernov 			break;
6865b81b6b3SRodney W. Grimes 		}
68792200632SGarrett Wollman 		fd_registerdev(fdcu, fdu);
68892200632SGarrett Wollman 		if(dk_ndrive < DK_NDRIVE) {
68992200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
69092200632SGarrett Wollman 			dk_wpms[dk_ndrive] = (500 * 1024 / 2) / 1000;
69192200632SGarrett Wollman 			fd->dkunit = dk_ndrive++;
69292200632SGarrett Wollman 		} else {
69392200632SGarrett Wollman 			fd->dkunit = -1;
69492200632SGarrett Wollman 		}
6955b81b6b3SRodney W. Grimes 	}
696b99f0a4aSAndrew Moore 	printf("\n");
6975b81b6b3SRodney W. Grimes 
6983a2f7427SDavid Greenman 	return (1);
6995b81b6b3SRodney W. Grimes }
7005b81b6b3SRodney W. Grimes 
7015b81b6b3SRodney W. Grimes int
702dc5df763SJoerg Wunsch fdsize(dev_t dev)
7035b81b6b3SRodney W. Grimes {
7045b81b6b3SRodney W. Grimes 	return(0);
7055b81b6b3SRodney W. Grimes }
7065b81b6b3SRodney W. Grimes 
7075b81b6b3SRodney W. Grimes /****************************************************************************/
7085b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
7095e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
7105b81b6b3SRodney W. Grimes /****************************************************************************/
7113a2f7427SDavid Greenman static void
712dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon)
7135b81b6b3SRodney W. Grimes {
7143a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
7153a2f7427SDavid Greenman 	int needspecify = 0;
7163a2f7427SDavid Greenman 
7173a2f7427SDavid Greenman 	if(turnon) {
7183a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
7193a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
7203a2f7427SDavid Greenman 	} else
7213a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
7223a2f7427SDavid Greenman 
7235e235068SJordan K. Hubbard 	if(!turnon
7245e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
7255e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
7265e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
7275e235068SJordan K. Hubbard 	else {
7283a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
7293a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
7303a2f7427SDavid Greenman 			needspecify = 1;
7313a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
7325b81b6b3SRodney W. Grimes 	}
7335b81b6b3SRodney W. Grimes 
7343a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
7353a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
7363a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
7373a2f7427SDavid Greenman 
7383a2f7427SDavid Greenman 	if(needspecify) {
739dc8603e3SJoerg Wunsch 		/*
740dc5df763SJoerg Wunsch 		 * XXX
741dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
742dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
743dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
744dc8603e3SJoerg Wunsch 		 */
745dc5df763SJoerg Wunsch 		(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
746dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
747dc5df763SJoerg Wunsch 			     0);
7483a2f7427SDavid Greenman 	}
7493a2f7427SDavid Greenman }
7503a2f7427SDavid Greenman 
7515e235068SJordan K. Hubbard /* ARGSUSED */
752381fe1aaSGarrett Wollman static void
753d0917939SPaul Richards fd_turnoff(void *arg1)
7545b81b6b3SRodney W. Grimes {
755381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
756f5f7ba03SJordan K. Hubbard 	int	s;
757f5f7ba03SJordan K. Hubbard 
7585b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
759f5f7ba03SJordan K. Hubbard 	s = splbio();
7605b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
7613a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
762f5f7ba03SJordan K. Hubbard 	splx(s);
7635b81b6b3SRodney W. Grimes }
7645b81b6b3SRodney W. Grimes 
7655e235068SJordan K. Hubbard /* ARGSUSED */
7663a2f7427SDavid Greenman static void
767d0917939SPaul Richards fd_motor_on(void *arg1)
7685b81b6b3SRodney W. Grimes {
769381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
770f5f7ba03SJordan K. Hubbard 	int	s;
771f5f7ba03SJordan K. Hubbard 
7725b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
773f5f7ba03SJordan K. Hubbard 	s = splbio();
7745b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
7755b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
7765b81b6b3SRodney W. Grimes 	{
777f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
7785b81b6b3SRodney W. Grimes 	}
779f5f7ba03SJordan K. Hubbard 	splx(s);
7805b81b6b3SRodney W. Grimes }
7815b81b6b3SRodney W. Grimes 
7823a2f7427SDavid Greenman static void
783dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu)
7845b81b6b3SRodney W. Grimes {
7855b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
7865b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
7875b81b6b3SRodney W. Grimes 	{
7883a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
7893a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
7905e235068SJordan K. Hubbard 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
7915b81b6b3SRodney W. Grimes 	}
7925b81b6b3SRodney W. Grimes }
7935b81b6b3SRodney W. Grimes 
794381fe1aaSGarrett Wollman static void
795dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
7965b81b6b3SRodney W. Grimes {
7973a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
7983a2f7427SDavid Greenman 
7993a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
8003a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8013a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
8023a2f7427SDavid Greenman 	DELAY(100);
8033a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
8043a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
8053a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
8063a2f7427SDavid Greenman 	DELAY(100);
8073a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
8083a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
8093a2f7427SDavid Greenman 
810dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
811dc5df763SJoerg Wunsch 	(void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY,
812dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
813dc5df763SJoerg Wunsch 		     0);
8145b81b6b3SRodney W. Grimes }
8155b81b6b3SRodney W. Grimes 
8165b81b6b3SRodney W. Grimes /****************************************************************************/
8175b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
8185b81b6b3SRodney W. Grimes /****************************************************************************/
8195b81b6b3SRodney W. Grimes int
820dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu)
8215b81b6b3SRodney W. Grimes {
8225b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
8235b81b6b3SRodney W. Grimes 	int i, j = 100000;
8243a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
8255b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
826dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
827dc5df763SJoerg Wunsch 			return fdc_err(fdcu, "ready for output in input");
8285b81b6b3SRodney W. Grimes 	if (j <= 0)
829dc5df763SJoerg Wunsch 		return fdc_err(fdcu, "input ready timeout");
8305b81b6b3SRodney W. Grimes #ifdef	DEBUG
8313a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
8323a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
8335b81b6b3SRodney W. Grimes 	return(i);
8345b81b6b3SRodney W. Grimes #else
8353a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
8365b81b6b3SRodney W. Grimes #endif
8375b81b6b3SRodney W. Grimes }
8385b81b6b3SRodney W. Grimes 
839dc5df763SJoerg Wunsch /*
840dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
841dc5df763SJoerg Wunsch  */
842381fe1aaSGarrett Wollman int
843dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr)
844dc5df763SJoerg Wunsch {
845dc5df763SJoerg Wunsch 	int baseport = fdc_data[fdcu].baseport;
846dc5df763SJoerg Wunsch 	int i, j = 100000;
847dc5df763SJoerg Wunsch 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
848dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
849dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
850dc5df763SJoerg Wunsch 			return fdc_err(fdcu, "ready for output in input");
851dc5df763SJoerg Wunsch 	if (j <= 0)
852dc5df763SJoerg Wunsch 		return fdc_err(fdcu, "input ready timeout");
853dc5df763SJoerg Wunsch #ifdef	DEBUG
854dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
855dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
856dc5df763SJoerg Wunsch 	*ptr = i;
857dc5df763SJoerg Wunsch 	return 0;
858dc5df763SJoerg Wunsch #else
859dc5df763SJoerg Wunsch 	i = inb(baseport+FDDATA);
860dc5df763SJoerg Wunsch 	if (ptr)
861dc5df763SJoerg Wunsch 		*ptr = i;
862dc5df763SJoerg Wunsch 	return 0;
863dc5df763SJoerg Wunsch #endif
864dc5df763SJoerg Wunsch }
865dc5df763SJoerg Wunsch 
866dc5df763SJoerg Wunsch int
867dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x)
8685b81b6b3SRodney W. Grimes {
8695b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
8703b3837dbSRodney W. Grimes 	int i;
8715b81b6b3SRodney W. Grimes 
8723b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
8733b3837dbSRodney W. Grimes 	i = 100000;
8743a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
875dc5df763SJoerg Wunsch 	if (i <= 0) return fdc_err(fdcu, "direction bit not set");
8763b3837dbSRodney W. Grimes 
8773b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
8783b3837dbSRodney W. Grimes 	i = 100000;
8793a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
880dc5df763SJoerg Wunsch 	if (i <= 0) return fdc_err(fdcu, "output ready timeout");
8813b3837dbSRodney W. Grimes 
8823b3837dbSRodney W. Grimes 	/* Send the command and return */
8833a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
8843a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
8855b81b6b3SRodney W. Grimes 	return (0);
8865b81b6b3SRodney W. Grimes }
8875b81b6b3SRodney W. Grimes 
8885b81b6b3SRodney W. Grimes /****************************************************************************/
8895b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
8905b81b6b3SRodney W. Grimes /****************************************************************************/
891381fe1aaSGarrett Wollman int
892dc5df763SJoerg Wunsch Fdopen(dev_t dev, int flags)
8935b81b6b3SRodney W. Grimes {
8945b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
89520a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
896b99f0a4aSAndrew Moore 	fdc_p	fdc;
8976febd9aaSJordan K. Hubbard 	int     st3;
8985b81b6b3SRodney W. Grimes 
899b99f0a4aSAndrew Moore #if NFT > 0
900b99f0a4aSAndrew Moore 	/* check for a tape open */
901b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
902b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
903b99f0a4aSAndrew Moore #endif
9045b81b6b3SRodney W. Grimes 	/* check bounds */
905b99f0a4aSAndrew Moore 	if (fdu >= NFD)
906b99f0a4aSAndrew Moore 		return(ENXIO);
907b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
908b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
909b99f0a4aSAndrew Moore 		return(ENXIO);
910b99f0a4aSAndrew Moore 	if (type > NUMDENS)
911b99f0a4aSAndrew Moore 		return(ENXIO);
9127ca0641bSAndrey A. Chernov 	if (type == 0)
9137ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
9147ca0641bSAndrey A. Chernov 	else {
9157ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
916fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
9177ca0641bSAndrey A. Chernov 			case FD_360:
9187ca0641bSAndrey A. Chernov 				return(ENXIO);
919ed2fa05eSAndrey A. Chernov 			case FD_720:
920b39c878eSAndrey A. Chernov 				if (   type != FD_820
921b39c878eSAndrey A. Chernov 				    && type != FD_800
922ed2fa05eSAndrey A. Chernov 				   )
923ed2fa05eSAndrey A. Chernov 					return(ENXIO);
924ed2fa05eSAndrey A. Chernov 				break;
9257ca0641bSAndrey A. Chernov 			case FD_1200:
926b39c878eSAndrey A. Chernov 				switch (type) {
927b39c878eSAndrey A. Chernov 				case FD_1480:
928b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
929fa4700b4SAndrey A. Chernov 					break;
9307ca0641bSAndrey A. Chernov 				case FD_1440:
931b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
932b39c878eSAndrey A. Chernov 					break;
933b39c878eSAndrey A. Chernov 				case FD_820:
934b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
935b39c878eSAndrey A. Chernov 					break;
936b39c878eSAndrey A. Chernov 				case FD_800:
937b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
938b39c878eSAndrey A. Chernov 					break;
939b39c878eSAndrey A. Chernov 				case FD_720:
940b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
941b39c878eSAndrey A. Chernov 					break;
942b39c878eSAndrey A. Chernov 				case FD_360:
943b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
944b39c878eSAndrey A. Chernov 					break;
945b39c878eSAndrey A. Chernov 				default:
946b39c878eSAndrey A. Chernov 					return(ENXIO);
947b39c878eSAndrey A. Chernov 				}
948b39c878eSAndrey A. Chernov 				break;
949b39c878eSAndrey A. Chernov 			case FD_1440:
950b39c878eSAndrey A. Chernov 				if (   type != FD_1720
951b39c878eSAndrey A. Chernov 				    && type != FD_1480
952ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
953b39c878eSAndrey A. Chernov 				    && type != FD_820
954b39c878eSAndrey A. Chernov 				    && type != FD_800
955b39c878eSAndrey A. Chernov 				    && type != FD_720
9567ca0641bSAndrey A. Chernov 				    )
957dffff499SAndrey A. Chernov 					return(ENXIO);
958fa4700b4SAndrey A. Chernov 				break;
9597ca0641bSAndrey A. Chernov 			}
9607ca0641bSAndrey A. Chernov 		}
961fa4700b4SAndrey A. Chernov 	}
9626febd9aaSJordan K. Hubbard 
9636febd9aaSJordan K. Hubbard 		/* select it */
9646febd9aaSJordan K. Hubbard 	set_motor(fdc->fdcu, fd_data[fdu].fdsu, TURNON);
9656febd9aaSJordan K. Hubbard 
9666febd9aaSJordan K. Hubbard 	if (flags & FWRITE) {
967dc5df763SJoerg Wunsch 		/*
968dc5df763SJoerg Wunsch 		 * Check to make sure the diskette is writable:
969dc5df763SJoerg Wunsch 		 */
970dc5df763SJoerg Wunsch 		int err = 0;
971dc5df763SJoerg Wunsch 
972dc5df763SJoerg Wunsch 		if (fd_sense_drive_status(fdc, &st3) != 0)
973dc5df763SJoerg Wunsch 			err = EIO;
974dc5df763SJoerg Wunsch 		else if ( (st3 & NE7_ST3_WP) )
975dc5df763SJoerg Wunsch 		{
976dc5df763SJoerg Wunsch 			printf("fd%d: drive is write protected.\n", fdu);
977dc5df763SJoerg Wunsch 			err = ENXIO;
978dc5df763SJoerg Wunsch 		}
979dc5df763SJoerg Wunsch 
980dc5df763SJoerg Wunsch 		if (err) {
9816febd9aaSJordan K. Hubbard 			set_motor(fdc->fdcu, fd_data[fdu].fdsu, TURNOFF);
982dc5df763SJoerg Wunsch 			return err;
9836febd9aaSJordan K. Hubbard   		}
9846febd9aaSJordan K. Hubbard 	}
9856febd9aaSJordan K. Hubbard 
9866febd9aaSJordan K. Hubbard 	set_motor(fdc->fdcu, fd_data[fdu].fdsu, TURNOFF);
9876febd9aaSJordan K. Hubbard 
988b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
9895b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
9905b81b6b3SRodney W. Grimes 
9915b81b6b3SRodney W. Grimes 	return 0;
9925b81b6b3SRodney W. Grimes }
9935b81b6b3SRodney W. Grimes 
994381fe1aaSGarrett Wollman int
995dc5df763SJoerg Wunsch fdclose(dev_t dev, int flags)
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;
10075b81b6b3SRodney W. Grimes 	return(0);
10085b81b6b3SRodney W. Grimes }
10095b81b6b3SRodney W. Grimes 
10105b81b6b3SRodney W. Grimes 
10113a2f7427SDavid Greenman /****************************************************************************/
10123a2f7427SDavid Greenman /*                               fdstrategy                                 */
10133a2f7427SDavid Greenman /****************************************************************************/
10143a2f7427SDavid Greenman void
10153a2f7427SDavid Greenman fdstrategy(struct buf *bp)
10163a2f7427SDavid Greenman {
10173a2f7427SDavid Greenman 	register struct buf *dp;
10183a2f7427SDavid Greenman 	long nblocks, blknum;
10193a2f7427SDavid Greenman  	int	s;
10203a2f7427SDavid Greenman  	fdcu_t	fdcu;
10213a2f7427SDavid Greenman  	fdu_t	fdu;
10223a2f7427SDavid Greenman  	fdc_p	fdc;
10233a2f7427SDavid Greenman  	fd_p	fd;
10243a2f7427SDavid Greenman 	size_t	fdblk;
10253a2f7427SDavid Greenman 
10263a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
10273a2f7427SDavid Greenman 	fd = &fd_data[fdu];
10283a2f7427SDavid Greenman 	fdc = fd->fdc;
10293a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
10303a2f7427SDavid Greenman 	fdblk = 128 << (fd->ft->secsize);
10313a2f7427SDavid Greenman 
10323a2f7427SDavid Greenman #if NFT > 0
10333a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
10343a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
10353a2f7427SDavid Greenman 		bp->b_error = ENXIO;
10363a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
10373a2f7427SDavid Greenman 		goto bad;
10383a2f7427SDavid Greenman 	}
10393a2f7427SDavid Greenman 	/* check for controller already busy with tape */
10403a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
10413a2f7427SDavid Greenman 		bp->b_error = EBUSY;
10423a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
10433a2f7427SDavid Greenman 		goto bad;
10443a2f7427SDavid Greenman 	}
10453a2f7427SDavid Greenman #endif
10463a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
10473a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
1048dc5df763SJoerg Wunsch 			printf(
1049dc5df763SJoerg Wunsch 		"fdstrat: fd%d: bad request blkno = %lu, bcount = %ld\n",
1050702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
10513a2f7427SDavid Greenman 			bp->b_error = EINVAL;
10523a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
10533a2f7427SDavid Greenman 			goto bad;
10543a2f7427SDavid Greenman 		}
10553a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
10563a2f7427SDavid Greenman 			bp->b_error = EINVAL;
10573a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
10583a2f7427SDavid Greenman 			goto bad;
10593a2f7427SDavid Greenman 		}
10603a2f7427SDavid Greenman 	}
10613a2f7427SDavid Greenman 
10623a2f7427SDavid Greenman 	/*
10633a2f7427SDavid Greenman 	 * Set up block calculations.
10643a2f7427SDavid Greenman 	 */
10653a2f7427SDavid Greenman 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk;
10663a2f7427SDavid Greenman  	nblocks = fd->ft->size;
10673a2f7427SDavid Greenman 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
10683a2f7427SDavid Greenman 		if (blknum == nblocks) {
10693a2f7427SDavid Greenman 			bp->b_resid = bp->b_bcount;
10703a2f7427SDavid Greenman 		} else {
10713a2f7427SDavid Greenman 			bp->b_error = ENOSPC;
10723a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
10733a2f7427SDavid Greenman 		}
10743a2f7427SDavid Greenman 		goto bad;
10753a2f7427SDavid Greenman 	}
10763a2f7427SDavid Greenman  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
10773a2f7427SDavid Greenman 	dp = &(fdc->head);
10783a2f7427SDavid Greenman 	s = splbio();
10793a2f7427SDavid Greenman 	disksort(dp, bp);
10805e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */
10813a2f7427SDavid Greenman 	fdstart(fdcu);
10823a2f7427SDavid Greenman 	splx(s);
10833a2f7427SDavid Greenman 	return;
10843a2f7427SDavid Greenman 
10853a2f7427SDavid Greenman bad:
10863a2f7427SDavid Greenman 	biodone(bp);
10873a2f7427SDavid Greenman }
10883a2f7427SDavid Greenman 
10895b81b6b3SRodney W. Grimes /***************************************************************\
10905b81b6b3SRodney W. Grimes *				fdstart				*
10915b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
10925b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
10935b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
10945b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
10955b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
10965b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
10975b81b6b3SRodney W. Grimes \***************************************************************/
1098381fe1aaSGarrett Wollman static void
1099dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu)
11005b81b6b3SRodney W. Grimes {
11015b81b6b3SRodney W. Grimes 	int s;
11025b81b6b3SRodney W. Grimes 
11035b81b6b3SRodney W. Grimes 	s = splbio();
11045b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
11055b81b6b3SRodney W. Grimes 	{
11065b81b6b3SRodney W. Grimes 		fdintr(fdcu);
11075b81b6b3SRodney W. Grimes 	}
11085b81b6b3SRodney W. Grimes 	splx(s);
11095b81b6b3SRodney W. Grimes }
11105b81b6b3SRodney W. Grimes 
11115e235068SJordan K. Hubbard /* ARGSUSED */
1112381fe1aaSGarrett Wollman static void
1113d0917939SPaul Richards fd_timeout(void *arg1)
11145b81b6b3SRodney W. Grimes {
1115381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
11165b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
11173a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
11185b81b6b3SRodney W. Grimes 	struct buf *dp, *bp;
1119f5f7ba03SJordan K. Hubbard 	int s;
11205b81b6b3SRodney W. Grimes 
11215b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
11225b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
11235b81b6b3SRodney W. Grimes 
11243a2f7427SDavid Greenman 	/*
11253a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
11263a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
11273a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
11283a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
11293a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
11303a2f7427SDavid Greenman 	 */
11315b81b6b3SRodney W. Grimes 
11323a2f7427SDavid Greenman 	s = splbio();
11333a2f7427SDavid Greenman 
11343a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
11353a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
11363a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
11373a2f7427SDavid Greenman 	{
11383a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
11393a2f7427SDavid Greenman 		/* yup, it is; kill it now */
11403a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
11413a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
11423a2f7427SDavid Greenman 	}
11435b81b6b3SRodney W. Grimes 
11445b81b6b3SRodney W. Grimes 	if (bp)
11455b81b6b3SRodney W. Grimes 	{
11465b81b6b3SRodney W. Grimes 		retrier(fdcu);
11473a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
11485b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
11495b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
11505b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
11515b81b6b3SRodney W. Grimes 	}
11525b81b6b3SRodney W. Grimes 	else
11535b81b6b3SRodney W. Grimes 	{
11545b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
11555b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
11565b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
11575b81b6b3SRodney W. Grimes 	}
1158f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
1159f5f7ba03SJordan K. Hubbard 	splx(s);
11605b81b6b3SRodney W. Grimes }
11615b81b6b3SRodney W. Grimes 
11625b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
11635e235068SJordan K. Hubbard /* ARGSUSED */
1164381fe1aaSGarrett Wollman static void
1165d0917939SPaul Richards fd_pseudointr(void *arg1)
11665b81b6b3SRodney W. Grimes {
1167381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
11685b81b6b3SRodney W. Grimes 	int	s;
11693a2f7427SDavid Greenman 
11705b81b6b3SRodney W. Grimes 	s = splbio();
11715b81b6b3SRodney W. Grimes 	fdintr(fdcu);
11725b81b6b3SRodney W. Grimes 	splx(s);
11735b81b6b3SRodney W. Grimes }
11745b81b6b3SRodney W. Grimes 
11755b81b6b3SRodney W. Grimes /***********************************************************************\
11765b81b6b3SRodney W. Grimes *                                 fdintr				*
11775b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
11785b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
11795b81b6b3SRodney W. Grimes \***********************************************************************/
1180381fe1aaSGarrett Wollman void
1181381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
11825b81b6b3SRodney W. Grimes {
11835b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
1184b99f0a4aSAndrew Moore #if NFT > 0
1185b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
1186b99f0a4aSAndrew Moore 
1187b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
1188b99f0a4aSAndrew Moore 		(ftintr(fdu));
1189b99f0a4aSAndrew Moore 	else
1190b99f0a4aSAndrew Moore #endif
1191381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
1192381fe1aaSGarrett Wollman 			;
11935b81b6b3SRodney W. Grimes }
11945b81b6b3SRodney W. Grimes 
11955b81b6b3SRodney W. Grimes /***********************************************************************\
11965b81b6b3SRodney W. Grimes * The controller state machine.						*
11975b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
11985b81b6b3SRodney W. Grimes \***********************************************************************/
11993a2f7427SDavid Greenman static int
1200dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc)
12015b81b6b3SRodney W. Grimes {
12023a2f7427SDavid Greenman 	int read, format, head, sec = 0, i = 0, sectrac, st0, cyl, st3;
12035b81b6b3SRodney W. Grimes 	unsigned long blknum;
12045b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
12055b81b6b3SRodney W. Grimes 	fd_p fd;
12065b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
1207b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
12083a2f7427SDavid Greenman 	size_t fdblk;
12095b81b6b3SRodney W. Grimes 
12105b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
12115b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
12125b81b6b3SRodney W. Grimes 	if(!bp)
12135b81b6b3SRodney W. Grimes 	{
12145b81b6b3SRodney W. Grimes 		/***********************************************\
12155b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
12165b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
12175b81b6b3SRodney W. Grimes 		\***********************************************/
12185b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
12195b81b6b3SRodney W. Grimes 		if(fdc->fd)
12205b81b6b3SRodney W. Grimes 		{
12213a2f7427SDavid Greenman 			printf("unexpected valid fd pointer (fdu = %d)\n",
12223a2f7427SDavid Greenman 			       fdc->fdu);
12235b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
12245b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
12255b81b6b3SRodney W. Grimes 		}
12265b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
12275b81b6b3SRodney W. Grimes  		return(0);
12285b81b6b3SRodney W. Grimes 	}
12295b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
12305b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
12313a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
12325b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
12335b81b6b3SRodney W. Grimes 	{
12345b81b6b3SRodney W. Grimes 		printf("confused fd pointers\n");
12355b81b6b3SRodney W. Grimes 	}
12365b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1237b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1238b39c878eSAndrey A. Chernov 	if(format)
1239b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
12405b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
12415b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
12425b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
12435e235068SJordan K. Hubbard 	untimeout(fd_turnoff, (caddr_t)fdu);
12445e235068SJordan K. Hubbard 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
12455b81b6b3SRodney W. Grimes 	switch (fdc->state)
12465b81b6b3SRodney W. Grimes 	{
12475b81b6b3SRodney W. Grimes 	case DEVIDLE:
12485b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
12495b81b6b3SRodney W. Grimes 		fdc->retry = 0;
12505b81b6b3SRodney W. Grimes 		fd->skip = 0;
12515b81b6b3SRodney W. Grimes 		fdc->fd = fd;
12525b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
12533a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
12543a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
12555b81b6b3SRodney W. Grimes 		/*******************************************************\
12565b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
12575b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
12585b81b6b3SRodney W. Grimes 		\*******************************************************/
12595b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
12605b81b6b3SRodney W. Grimes 		{
12615b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
12625b81b6b3SRodney W. Grimes 			return(0); /* come back later */
12635b81b6b3SRodney W. Grimes 		}
12645b81b6b3SRodney W. Grimes 		/*******************************************************\
12655b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
12665b81b6b3SRodney W. Grimes 		\*******************************************************/
12675b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
12685b81b6b3SRodney W. Grimes 		{
12695b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
12705b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
12715b81b6b3SRodney W. Grimes 			return(0);
12725b81b6b3SRodney W. Grimes 		}
12735b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
12745b81b6b3SRodney W. Grimes 		{
12753a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
12765b81b6b3SRodney W. Grimes 		}
12775e235068SJordan K. Hubbard 		fdc->state = DOSEEK;
12785b81b6b3SRodney W. Grimes 		break;
12795b81b6b3SRodney W. Grimes 	case DOSEEK:
12805b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
12815b81b6b3SRodney W. Grimes 		{
12825b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
12835b81b6b3SRodney W. Grimes 			break;
12845b81b6b3SRodney W. Grimes 		}
1285dc5df763SJoerg Wunsch 		if (fd_cmd(fdcu, 3, NE7CMD_SEEK,
1286dc5df763SJoerg Wunsch 			   fd->fdsu, bp->b_cylin * fd->ft->steptrac,
1287dc5df763SJoerg Wunsch 			   0))
1288dc8603e3SJoerg Wunsch 		{
1289dc8603e3SJoerg Wunsch 			/*
1290dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1291dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1292dc8603e3SJoerg Wunsch 			 */
1293dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1294dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1295dc8603e3SJoerg Wunsch 		}
1296dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
12975b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
12985b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
12995b81b6b3SRodney W. Grimes 	case SEEKWAIT:
13005b81b6b3SRodney W. Grimes 		/* allow heads to settle */
130104b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16);
13025b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
13035b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
13045b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
13055b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
1306dc5df763SJoerg Wunsch 		if(fd->track == FD_NO_TRACK)
13075b81b6b3SRodney W. Grimes 		{
13085b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
13093a2f7427SDavid Greenman 			do {
13103a2f7427SDavid Greenman 				/*
1311dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1312dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1313dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1314dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1315dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1316dc5df763SJoerg Wunsch 				 *
1317dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1318dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1319dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1320dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1321dc5df763SJoerg Wunsch 				 * real interrupt condition.
1322dc5df763SJoerg Wunsch 				 *
1323dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1324dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1325dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1326dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1327dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1328dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1329dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
13303a2f7427SDavid Greenman 				 */
1331dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1332dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1333dc5df763SJoerg Wunsch 					return 0;
1334dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1335dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1336dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
13373a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1338dc5df763SJoerg Wunsch 
13393a2f7427SDavid Greenman 			if (0 == descyl)
13403a2f7427SDavid Greenman 			{
1341dc5df763SJoerg Wunsch 				int failed = 0;
13423a2f7427SDavid Greenman 				/*
13433a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
13443a2f7427SDavid Greenman 				 * really there
13453a2f7427SDavid Greenman 				 */
1346dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1347dc5df763SJoerg Wunsch 					failed = 1;
13483a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
13493a2f7427SDavid Greenman 					printf(
13503a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
13513a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1352dc5df763SJoerg Wunsch 					failed = 1;
1353dc5df763SJoerg Wunsch 				}
1354dc5df763SJoerg Wunsch 
1355dc5df763SJoerg Wunsch 				if (failed)
1356dc5df763SJoerg Wunsch 				{
13573a2f7427SDavid Greenman 					if(fdc->retry < 3)
13583a2f7427SDavid Greenman 						fdc->retry = 3;
13593a2f7427SDavid Greenman 					return(retrier(fdcu));
13603a2f7427SDavid Greenman 				}
13613a2f7427SDavid Greenman 			}
1362dc5df763SJoerg Wunsch 
13635b81b6b3SRodney W. Grimes 			if (cyl != descyl)
13645b81b6b3SRodney W. Grimes 			{
13653a2f7427SDavid Greenman 				printf(
13663a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
13675e235068SJordan K. Hubbard 				       fdu, descyl, cyl, st0, NE7_ST0BITS);
13685b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
13695b81b6b3SRodney W. Grimes 			}
13705b81b6b3SRodney W. Grimes 		}
13715b81b6b3SRodney W. Grimes 
13725b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
1373b39c878eSAndrey A. Chernov 		if(format)
1374b39c878eSAndrey A. Chernov 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1375b39c878eSAndrey A. Chernov 				- (char *)finfo;
13765b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
13773a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
13783a2f7427SDavid Greenman 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
13793a2f7427SDavid Greenman 			+ fd->skip/fdblk;
13805b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
13815b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
13825b81b6b3SRodney W. Grimes 		head = sec / sectrac;
13835b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
13843a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
13853a2f7427SDavid Greenman 
13863a2f7427SDavid Greenman 		if(format || !read)
13873a2f7427SDavid Greenman 		{
13883a2f7427SDavid Greenman 			/* make sure the drive is writable */
1389dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1390dc8603e3SJoerg Wunsch 			{
1391dc8603e3SJoerg Wunsch 				/* stuck controller? */
1392dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1393dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1394dc8603e3SJoerg Wunsch 			}
13953a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
13963a2f7427SDavid Greenman 			{
13973a2f7427SDavid Greenman 				/*
13983a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
13993a2f7427SDavid Greenman 				 * in order to force the current operation
14003a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
14013a2f7427SDavid Greenman 				 * error - all error handling is done
14023a2f7427SDavid Greenman 				 * by the retrier()
14033a2f7427SDavid Greenman 				 */
14043a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
14053a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
14063a2f7427SDavid Greenman 				fdc->status[2] = 0;
14073a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
14083a2f7427SDavid Greenman 				fdc->status[4] = head;
14093a2f7427SDavid Greenman 				fdc->status[5] = sec;
14103a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
14113a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
14123a2f7427SDavid Greenman 				return (1);
14133a2f7427SDavid Greenman 			}
14143a2f7427SDavid Greenman 		}
14155b81b6b3SRodney W. Grimes 
1416b39c878eSAndrey A. Chernov 		if(format)
1417b39c878eSAndrey A. Chernov 		{
1418b39c878eSAndrey A. Chernov 			/* formatting */
1419dc5df763SJoerg Wunsch 			if(fd_cmd(fdcu, 6,
1420dc5df763SJoerg Wunsch 				  NE7CMD_FORMAT,
1421dc5df763SJoerg Wunsch 				  head << 2 | fdu,
1422dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1423dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1424dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
1425dc5df763SJoerg Wunsch 				  finfo->fd_formb_fillbyte,
1426dc5df763SJoerg Wunsch 				  0))
1427dc8603e3SJoerg Wunsch 			{
1428dc8603e3SJoerg Wunsch 				/* controller fell over */
1429dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1430dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1431dc8603e3SJoerg Wunsch 			}
1432b39c878eSAndrey A. Chernov 		}
1433b39c878eSAndrey A. Chernov 		else
1434b39c878eSAndrey A. Chernov 		{
1435dc5df763SJoerg Wunsch 			if (fd_cmd(fdcu, 9,
1436dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1437dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1438dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1439dc5df763SJoerg Wunsch 				   head,
1440dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1441dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1442dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1443dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1444dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
1445dc5df763SJoerg Wunsch 				   0))
14465b81b6b3SRodney W. Grimes 			{
1447dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1448dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1449dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
14505b81b6b3SRodney W. Grimes 			}
1451b39c878eSAndrey A. Chernov 		}
14525b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
14535e235068SJordan K. Hubbard 		timeout(fd_timeout, (caddr_t)fdcu, hz);
14545b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
14555b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
14565e235068SJordan K. Hubbard 		untimeout(fd_timeout, (caddr_t)fdcu);
1457dc5df763SJoerg Wunsch 
1458dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu))
14595b81b6b3SRodney W. Grimes 		{
1460dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
1461dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
1462dc5df763SJoerg Wunsch 			return retrier(fdcu);
14635b81b6b3SRodney W. Grimes   		}
1464dc5df763SJoerg Wunsch 
14653a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
1466dc5df763SJoerg Wunsch 
14673a2f7427SDavid Greenman 		/* FALLTHROUGH */
1468dc5df763SJoerg Wunsch 
14693a2f7427SDavid Greenman 	case IOTIMEDOUT:
14705b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
14713a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
14723a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
14735b81b6b3SRodney W. Grimes 		{
14743a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
14753a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1476b39c878eSAndrey A. Chernov                                 /*
14773a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
14783a2f7427SDavid Greenman 				 * and didn't release it in time for the
14793a2f7427SDavid Greenman 				 * next FDC transfer.
14803a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
14813a2f7427SDavid Greenman 				 * count. (vak)
1482b39c878eSAndrey A. Chernov                                  */
1483b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1484b39c878eSAndrey A. Chernov                                 return (1);
1485b39c878eSAndrey A. Chernov                         }
14863a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
14873a2f7427SDavid Greenman 				&& fdc->retry < 6)
14883a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
14893a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
14903a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
14913a2f7427SDavid Greenman 				&& fdc->retry < 3)
14923a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
14935b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
14945b81b6b3SRodney W. Grimes 		}
14955b81b6b3SRodney W. Grimes 		/* All OK */
14963a2f7427SDavid Greenman 		fd->skip += fdblk;
1497b39c878eSAndrey A. Chernov 		if (!format && fd->skip < bp->b_bcount)
14985b81b6b3SRodney W. Grimes 		{
14995b81b6b3SRodney W. Grimes 			/* set up next transfer */
15003a2f7427SDavid Greenman 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
15013a2f7427SDavid Greenman 				+ fd->skip/fdblk;
15023a2f7427SDavid Greenman 			bp->b_cylin =
15033a2f7427SDavid Greenman 				(blknum / (fd->ft->sectrac * fd->ft->heads));
15045b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
15055b81b6b3SRodney W. Grimes 		}
15065b81b6b3SRodney W. Grimes 		else
15075b81b6b3SRodney W. Grimes 		{
15085b81b6b3SRodney W. Grimes 			/* ALL DONE */
15095b81b6b3SRodney W. Grimes 			fd->skip = 0;
15105b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
151126f9a767SRodney W. Grimes 			dp->b_actf = bp->b_actf;
15125b81b6b3SRodney W. Grimes 			biodone(bp);
15135b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
15145b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
15155b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
15165b81b6b3SRodney W. Grimes 		}
15175b81b6b3SRodney W. Grimes 		return(1);
15185b81b6b3SRodney W. Grimes 	case RESETCTLR:
15193a2f7427SDavid Greenman 		fdc_reset(fdc);
15205b81b6b3SRodney W. Grimes 		fdc->retry++;
15215b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
15225b81b6b3SRodney W. Grimes 		break;
15235b81b6b3SRodney W. Grimes 	case STARTRECAL:
1524dc5df763SJoerg Wunsch 		if(fd_cmd(fdcu,
1525dc5df763SJoerg Wunsch 			  2, NE7CMD_RECAL, fdu,
1526dc5df763SJoerg Wunsch 			  0)) /* Recalibrate Function */
1527dc8603e3SJoerg Wunsch 		{
1528dc8603e3SJoerg Wunsch 			/* arrgl */
1529dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1530dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1531dc8603e3SJoerg Wunsch 		}
15325b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
15335b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15345b81b6b3SRodney W. Grimes 	case RECALWAIT:
15355b81b6b3SRodney W. Grimes 		/* allow heads to settle */
153604b734cfSPoul-Henning Kamp 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8);
15375b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
15385b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
15395b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
15403a2f7427SDavid Greenman 		do {
15413a2f7427SDavid Greenman 			/*
1542dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
15433a2f7427SDavid Greenman 			 */
1544dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1545dc5df763SJoerg Wunsch 				return 0;
1546dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
1547dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1548dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
15493a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
15503a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
15515b81b6b3SRodney W. Grimes 		{
1552dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1553dc8603e3SJoerg Wunsch 				/*
1554dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1555dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1556dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1557dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1558dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1559dc8603e3SJoerg Wunsch 				 * time in a line
1560dc8603e3SJoerg Wunsch 				 */
1561dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1562dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
15633a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
15645b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
15655b81b6b3SRodney W. Grimes 		}
15665b81b6b3SRodney W. Grimes 		fd->track = 0;
15675b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
15685b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
15695b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
15705b81b6b3SRodney W. Grimes 	case MOTORWAIT:
15715b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
15725b81b6b3SRodney W. Grimes 		{
15735b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
15745b81b6b3SRodney W. Grimes 		}
15755e235068SJordan K. Hubbard 		/*
15765e235068SJordan K. Hubbard 		 * since the controller was off, it has lost its
15775e235068SJordan K. Hubbard 		 * idea about the current track it were; thus,
15785e235068SJordan K. Hubbard 		 * recalibrate the bastard
15795e235068SJordan K. Hubbard 		 */
15805e235068SJordan K. Hubbard 		fdc->state = STARTRECAL;
15815b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
15825b81b6b3SRodney W. Grimes 	default:
1583dc5df763SJoerg Wunsch 		printf("fdc%d: Unexpected FD int->", fdcu);
1584dc5df763SJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
1585dc8603e3SJoerg Wunsch 		{
1586dc8603e3SJoerg Wunsch 			printf("[controller is dead now]\n");
1587dc8603e3SJoerg Wunsch 			return(0);
1588dc8603e3SJoerg Wunsch 		}
15893a2f7427SDavid Greenman 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
1590dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
1591dc5df763SJoerg Wunsch 			printf("FDC status :%lx %lx %lx %lx %lx %lx %lx\n",
15925b81b6b3SRodney W. Grimes 			       fdc->status[0],
15935b81b6b3SRodney W. Grimes 			       fdc->status[1],
15945b81b6b3SRodney W. Grimes 			       fdc->status[2],
15955b81b6b3SRodney W. Grimes 			       fdc->status[3],
15965b81b6b3SRodney W. Grimes 			       fdc->status[4],
15975b81b6b3SRodney W. Grimes 			       fdc->status[5],
15985b81b6b3SRodney W. Grimes 			       fdc->status[6] );
15993a2f7427SDavid Greenman 		else
1600dc5df763SJoerg Wunsch 			printf("No status available\n");
16015b81b6b3SRodney W. Grimes 		return(0);
16025b81b6b3SRodney W. Grimes 	}
16035b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
16045b81b6b3SRodney W. Grimes }
16055b81b6b3SRodney W. Grimes 
1606aaf08d94SGarrett Wollman static int
1607f5f7ba03SJordan K. Hubbard retrier(fdcu)
1608f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
16095b81b6b3SRodney W. Grimes {
16105b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
16115b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
16125b81b6b3SRodney W. Grimes 
16135b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
16145b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
16155b81b6b3SRodney W. Grimes 
16163a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
16173a2f7427SDavid Greenman 		goto fail;
16185b81b6b3SRodney W. Grimes 	switch(fdc->retry)
16195b81b6b3SRodney W. Grimes 	{
16205b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
16215b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
16225b81b6b3SRodney W. Grimes 		break;
16235b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
16245b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
16255b81b6b3SRodney W. Grimes 		break;
16265b81b6b3SRodney W. Grimes 	case 6:
16275b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
16285b81b6b3SRodney W. Grimes 		break;
16295b81b6b3SRodney W. Grimes 	case 7:
16305b81b6b3SRodney W. Grimes 		break;
16315b81b6b3SRodney W. Grimes 	default:
16323a2f7427SDavid Greenman 	fail:
16335b81b6b3SRodney W. Grimes 		{
16347ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
16357ca0641bSAndrey A. Chernov 			/* Trick diskerr */
16363a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
16373a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
163892ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
16393a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
16403a2f7427SDavid Greenman 				(struct disklabel *)NULL);
16417ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
1642dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
1643dc5df763SJoerg Wunsch 			{
1644dc5df763SJoerg Wunsch 				printf(
1645dc5df763SJoerg Wunsch 			" (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n",
1646dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
1647dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
1648dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
1649dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
1650dc5df763SJoerg Wunsch 				       fdc->status[5]);
1651dc5df763SJoerg Wunsch 			}
1652dc5df763SJoerg Wunsch 			else
1653dc5df763SJoerg Wunsch 				printf(" (No status)\n");
16545b81b6b3SRodney W. Grimes 		}
16555b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
16565b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
16575b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
165826f9a767SRodney W. Grimes 		dp->b_actf = bp->b_actf;
16595b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
16605b81b6b3SRodney W. Grimes 		biodone(bp);
166192ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
16625b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
16635b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1664f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
166592ed385aSRodney W. Grimes 		return(1);
16665b81b6b3SRodney W. Grimes 	}
16675b81b6b3SRodney W. Grimes 	fdc->retry++;
16685b81b6b3SRodney W. Grimes 	return(1);
16695b81b6b3SRodney W. Grimes }
16705b81b6b3SRodney W. Grimes 
1671b39c878eSAndrey A. Chernov static int
1672b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1673b39c878eSAndrey A. Chernov 	dev_t dev;
1674b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1675b39c878eSAndrey A. Chernov 	struct proc *p;
1676b39c878eSAndrey A. Chernov {
1677b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1678b39c878eSAndrey A. Chernov  	fd_p	fd;
1679b39c878eSAndrey A. Chernov 
1680b39c878eSAndrey A. Chernov 	struct buf *bp;
1681b39c878eSAndrey A. Chernov 	int rv = 0, s;
16823a2f7427SDavid Greenman 	size_t fdblk;
1683b39c878eSAndrey A. Chernov 
1684b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1685b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
16863a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1687b39c878eSAndrey A. Chernov 
1688b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1689b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1690b39c878eSAndrey A. Chernov 	if(bp == 0)
1691b39c878eSAndrey A. Chernov 		return ENOBUFS;
1692b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1693b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1694b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1695b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1696b39c878eSAndrey A. Chernov 
1697b39c878eSAndrey A. Chernov 	/*
1698b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1699b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1700b39c878eSAndrey A. Chernov 	 */
1701b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
17023a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1703b39c878eSAndrey A. Chernov 
1704b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
17055e235068SJordan K. Hubbard 	bp->b_un.b_addr = (caddr_t)finfo;
1706b39c878eSAndrey A. Chernov 
1707b39c878eSAndrey A. Chernov 	/* now do the format */
1708b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1709b39c878eSAndrey A. Chernov 
1710b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1711b39c878eSAndrey A. Chernov 	s = splbio();
1712b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1713b39c878eSAndrey A. Chernov 	{
17145e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1715b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1716b39c878eSAndrey A. Chernov 			break;
1717b39c878eSAndrey A. Chernov 	}
1718b39c878eSAndrey A. Chernov 	splx(s);
1719b39c878eSAndrey A. Chernov 
1720b39c878eSAndrey A. Chernov 	if(rv == EWOULDBLOCK)
1721b39c878eSAndrey A. Chernov 		/* timed out */
1722b39c878eSAndrey A. Chernov 		rv = EIO;
17233a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
17243a2f7427SDavid Greenman 		rv = bp->b_error;
17253a2f7427SDavid Greenman 	biodone(bp);
1726b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1727b39c878eSAndrey A. Chernov 	return rv;
1728b39c878eSAndrey A. Chernov }
1729b39c878eSAndrey A. Chernov 
1730f5f7ba03SJordan K. Hubbard /*
1731f5f7ba03SJordan K. Hubbard  *
17323a2f7427SDavid Greenman  * TODO: Think about allocating buffer off stack.
1733f5f7ba03SJordan K. Hubbard  *       Don't pass uncast 0's and NULL's to read/write/setdisklabel().
1734f5f7ba03SJordan K. Hubbard  *       Watch out for NetBSD's different *disklabel() interface.
1735b39c878eSAndrey A. Chernov  *
1736f5f7ba03SJordan K. Hubbard  */
17375b81b6b3SRodney W. Grimes 
1738f5f7ba03SJordan K. Hubbard int
1739b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1740f5f7ba03SJordan K. Hubbard 	dev_t dev;
1741f5f7ba03SJordan K. Hubbard 	int cmd;
1742f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1743f5f7ba03SJordan K. Hubbard 	int flag;
1744b39c878eSAndrey A. Chernov 	struct proc *p;
1745f5f7ba03SJordan K. Hubbard {
17463a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
17473a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
17483a2f7427SDavid Greenman 	size_t fdblk;
17493a2f7427SDavid Greenman 
1750f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1751f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1752f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
17533a2f7427SDavid Greenman 	int error = 0;
1754f5f7ba03SJordan K. Hubbard 
1755b99f0a4aSAndrew Moore #if NFT > 0
1756a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1757a60eff27SNate Williams 
1758a60eff27SNate Williams 	/* check for a tape ioctl */
1759a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1760b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1761b99f0a4aSAndrew Moore #endif
1762b99f0a4aSAndrew Moore 
17633a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1764f5f7ba03SJordan K. Hubbard 
1765f5f7ba03SJordan K. Hubbard 	switch (cmd)
1766f5f7ba03SJordan K. Hubbard 	{
1767f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1768f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1769f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
17703a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
177192ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1772f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1773f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1774f5f7ba03SJordan K. Hubbard 
177554c7241bSJordan K. Hubbard 		if (readdisklabel(dev, fdstrategy, dl, NULL, 0) == NULL)
1776f5f7ba03SJordan K. Hubbard 			error = 0;
1777f5f7ba03SJordan K. Hubbard 		else
1778f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1779f5f7ba03SJordan K. Hubbard 
1780f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1781f5f7ba03SJordan K. Hubbard 		break;
1782f5f7ba03SJordan K. Hubbard 
1783f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1784f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1785f5f7ba03SJordan K. Hubbard 			error = EBADF;
1786f5f7ba03SJordan K. Hubbard 		break;
1787f5f7ba03SJordan K. Hubbard 
1788f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1789f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1790f5f7ba03SJordan K. Hubbard 			error = EBADF;
1791f5f7ba03SJordan K. Hubbard 		break;
1792f5f7ba03SJordan K. Hubbard 
1793f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1794f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1795f5f7ba03SJordan K. Hubbard 		{
1796f5f7ba03SJordan K. Hubbard 			error = EBADF;
1797f5f7ba03SJordan K. Hubbard 			break;
1798f5f7ba03SJordan K. Hubbard 		}
1799f5f7ba03SJordan K. Hubbard 
1800f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1801f5f7ba03SJordan K. Hubbard 
18023a2f7427SDavid Greenman 		if ((error =
180354c7241bSJordan K. Hubbard 		     setdisklabel ((struct disklabel *)buffer, dl, 0)))
1804f5f7ba03SJordan K. Hubbard 			break;
1805f5f7ba03SJordan K. Hubbard 
1806b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
180754c7241bSJordan K. Hubbard 				       (struct disklabel *)buffer);
1808b39c878eSAndrey A. Chernov 		break;
1809b39c878eSAndrey A. Chernov 
1810b39c878eSAndrey A. Chernov 	case FD_FORM:
1811b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1812b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1813b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1814b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1815b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1816b39c878eSAndrey A. Chernov 		else
1817b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1818b39c878eSAndrey A. Chernov 		break;
1819b39c878eSAndrey A. Chernov 
1820b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1821b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1822f5f7ba03SJordan K. Hubbard 		break;
1823f5f7ba03SJordan K. Hubbard 
18243a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
18253a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
18263a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
18273a2f7427SDavid Greenman 			return EPERM;
18283a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
18293a2f7427SDavid Greenman 		break;
18303a2f7427SDavid Greenman 
18313a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
18323a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
18333a2f7427SDavid Greenman 		break;
18343a2f7427SDavid Greenman 
18353a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
18363a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
18373a2f7427SDavid Greenman 		break;
18383a2f7427SDavid Greenman 
1839f5f7ba03SJordan K. Hubbard 	default:
18403a2f7427SDavid Greenman 		error = ENOTTY;
1841f5f7ba03SJordan K. Hubbard 		break;
1842f5f7ba03SJordan K. Hubbard 	}
1843f5f7ba03SJordan K. Hubbard 	return (error);
1844f5f7ba03SJordan K. Hubbard }
1845f5f7ba03SJordan K. Hubbard 
1846f5f7ba03SJordan K. Hubbard #endif
18473a2f7427SDavid Greenman /*
18483a2f7427SDavid Greenman  * Hello emacs, these are the
18493a2f7427SDavid Greenman  * Local Variables:
18503a2f7427SDavid Greenman  *  c-indent-level:               8
18513a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
18523a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
18533a2f7427SDavid Greenman  *  c-brace-offset:              -8
18543a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
18553a2f7427SDavid Greenman  *  c-argdecl-indent:             8
18563a2f7427SDavid Greenman  *  c-label-offset:              -8
18573a2f7427SDavid Greenman  *  c++-hanging-braces:           1
18583a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
18593a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
18603a2f7427SDavid Greenman  *  c++-friend-offset:            0
18613a2f7427SDavid Greenman  * End:
18623a2f7427SDavid Greenman  */
1863