xref: /freebsd/sys/dev/fdc/fdc.c (revision dc8603e3ecde7521fc8b2784a29385728048087c)
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  *
83a2f7427SDavid Greenman  * Portions Copyright (c) 1993, 1994 by
93a2f7427SDavid Greenman  *  jc@irbs.UUCP (John Capo)
103a2f7427SDavid Greenman  *  vak@zebub.msk.su (Serge Vakulenko)
113a2f7427SDavid Greenman  *  ache@astral.msk.su (Andrew A. Chernov)
123a2f7427SDavid Greenman  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
133a2f7427SDavid Greenman  *
145b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
155b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
165b81b6b3SRodney W. Grimes  * are met:
175b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
185b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
195b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
205b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
215b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
225b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
235b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
245b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
255b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
265b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
275b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
285b81b6b3SRodney W. Grimes  *    without specific prior written permission.
295b81b6b3SRodney W. Grimes  *
305b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
315b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
325b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
335b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
345b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
355b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
365b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
375b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
385b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
395b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
405b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
415b81b6b3SRodney W. Grimes  *
42dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
43dc8603e3SJoerg Wunsch  *	$Id: fd.c,v 1.35 1994/10/19 01:58:56 wollman Exp $
445b81b6b3SRodney W. Grimes  *
455b81b6b3SRodney W. Grimes  */
465b81b6b3SRodney W. Grimes 
47b99f0a4aSAndrew Moore #include "ft.h"
48b99f0a4aSAndrew Moore #if NFT < 1
49b99f0a4aSAndrew Moore #undef NFDC
50b99f0a4aSAndrew Moore #endif
515b81b6b3SRodney W. Grimes #include "fd.h"
525b81b6b3SRodney W. Grimes 
53b99f0a4aSAndrew Moore #if NFDC > 0
54b99f0a4aSAndrew Moore 
55b99f0a4aSAndrew Moore #include <sys/param.h>
56b99f0a4aSAndrew Moore #include <sys/dkbad.h>
57b99f0a4aSAndrew Moore #include <sys/systm.h>
58b99f0a4aSAndrew Moore #include <sys/kernel.h>
59b99f0a4aSAndrew Moore #include <sys/conf.h>
60b99f0a4aSAndrew Moore #include <sys/file.h>
61b99f0a4aSAndrew Moore #include <sys/ioctl.h>
62b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h>
63b99f0a4aSAndrew Moore #include <sys/disklabel.h>
64b99f0a4aSAndrew Moore #include <sys/buf.h>
65b99f0a4aSAndrew Moore #include <sys/uio.h>
66b99f0a4aSAndrew Moore #include <sys/malloc.h>
673a2f7427SDavid Greenman #include <sys/proc.h>
68b99f0a4aSAndrew Moore #include <sys/syslog.h>
6992200632SGarrett Wollman #include <sys/devconf.h>
7092200632SGarrett Wollman #include <sys/dkstat.h>
71f540b106SGarrett Wollman #include <i386/isa/isa.h>
72f540b106SGarrett Wollman #include <i386/isa/isa_device.h>
73f540b106SGarrett Wollman #include <i386/isa/fdreg.h>
74f540b106SGarrett Wollman #include <i386/isa/fdc.h>
75f540b106SGarrett Wollman #include <i386/isa/rtc.h>
7687eafbcaSPoul-Henning Kamp #if NFT > 0
7787eafbcaSPoul-Henning Kamp #include <sys/ftape.h>
7887eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h>
7987eafbcaSPoul-Henning Kamp #endif
805b81b6b3SRodney W. Grimes 
8192200632SGarrett Wollman static int fd_goaway(struct kern_devconf *, int);
8292200632SGarrett Wollman static int fdc_goaway(struct kern_devconf *, int);
8392200632SGarrett Wollman static int fd_externalize(struct proc *, struct kern_devconf *, void *, size_t);
8492200632SGarrett Wollman 
8592200632SGarrett Wollman /*
8692200632SGarrett Wollman  * Templates for the kern_devconf structures used when we attach.
8792200632SGarrett Wollman  */
8892200632SGarrett Wollman static struct kern_devconf kdc_fd_template = {
8992200632SGarrett Wollman 	0, 0, 0,		/* filled in by kern_devconf.c */
9092200632SGarrett Wollman 	"fd", 0, { "fdc0", MDDT_DISK, 0 },
9192200632SGarrett Wollman 	fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN
9292200632SGarrett Wollman };
9392200632SGarrett Wollman 
9492200632SGarrett Wollman static struct kern_devconf kdc_fdc_template = {
9592200632SGarrett Wollman 	0, 0, 0,		/* filled in by kern_devconf.c */
9692200632SGarrett Wollman 	"fdc", 0, { "isa0", MDDT_ISA, 0 },
97f1d81093SGarrett Wollman 	isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN
9892200632SGarrett Wollman };
9992200632SGarrett Wollman 
10092200632SGarrett Wollman static inline void
10192200632SGarrett Wollman fd_registerdev(int ctlr, int unit)
10292200632SGarrett Wollman {
10392200632SGarrett Wollman 	struct kern_devconf *kdc;
10492200632SGarrett Wollman 
10592200632SGarrett Wollman 	MALLOC(kdc, struct kern_devconf *, sizeof *kdc, M_TEMP, M_NOWAIT);
10692200632SGarrett Wollman 	if(!kdc) return;
10792200632SGarrett Wollman 	*kdc = kdc_fd_template;
10892200632SGarrett Wollman 	kdc->kdc_unit = unit;
10992200632SGarrett Wollman 	sprintf(kdc->kdc_md.mddc_parent, "fdc%d", ctlr);
11092200632SGarrett Wollman 	dev_attach(kdc);
11192200632SGarrett Wollman }
11292200632SGarrett Wollman 
11392200632SGarrett Wollman static int
11492200632SGarrett Wollman fdc_goaway(struct kern_devconf *kdc, int force)
11592200632SGarrett Wollman {
11692200632SGarrett Wollman 	if(force) {
11792200632SGarrett Wollman 		dev_detach(kdc);
11892200632SGarrett Wollman 		FREE(kdc, M_TEMP);
11992200632SGarrett Wollman 		return 0;
12092200632SGarrett Wollman 	} else {
12192200632SGarrett Wollman 		return EBUSY;	/* XXX fix */
12292200632SGarrett Wollman 	}
12392200632SGarrett Wollman }
12492200632SGarrett Wollman 
12592200632SGarrett Wollman static int
12692200632SGarrett Wollman fd_goaway(struct kern_devconf *kdc, int force)
12792200632SGarrett Wollman {
12892200632SGarrett Wollman 	dev_detach(kdc);
12992200632SGarrett Wollman 	FREE(kdc, M_TEMP);
13092200632SGarrett Wollman 	return 0;
13192200632SGarrett Wollman }
13292200632SGarrett Wollman 
133b2be795bSAndrey A. Chernov #define RAW_PART 2
1345b81b6b3SRodney W. Grimes #define b_cylin b_resid
1355b81b6b3SRodney W. Grimes 
136b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
137b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
1385b81b6b3SRodney W. Grimes 
1393a2f7427SDavid Greenman /*
1403a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
1413a2f7427SDavid Greenman  * on the controller
1423a2f7427SDavid Greenman  */
1433a2f7427SDavid Greenman #define id_physid id_scsiid
1443a2f7427SDavid Greenman 
145b39c878eSAndrey A. Chernov #define NUMTYPES 14
146b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1477ca0641bSAndrey A. Chernov 
1483a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
149b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
150b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
151b99f0a4aSAndrew Moore #define FD_1720         1
152b99f0a4aSAndrew Moore #define FD_1480         2
153b99f0a4aSAndrew Moore #define FD_1440         3
154b99f0a4aSAndrew Moore #define FD_1200         4
155b99f0a4aSAndrew Moore #define FD_820          5
156b99f0a4aSAndrew Moore #define FD_800          6
157b99f0a4aSAndrew Moore #define FD_720          7
158b99f0a4aSAndrew Moore #define FD_360          8
159ed2fa05eSAndrey A. Chernov 
160b99f0a4aSAndrew Moore #define FD_1480in5_25   9
161b99f0a4aSAndrew Moore #define FD_1440in5_25   10
162b99f0a4aSAndrew Moore #define FD_820in5_25    11
163b99f0a4aSAndrew Moore #define FD_800in5_25    12
164b99f0a4aSAndrew Moore #define FD_720in5_25    13
165b99f0a4aSAndrew Moore #define FD_360in5_25    14
166b99f0a4aSAndrew Moore 
1677ca0641bSAndrey A. Chernov 
1685b81b6b3SRodney W. Grimes struct fd_type fd_types[NUMTYPES] =
1695b81b6b3SRodney W. Grimes {
170126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
171126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
172126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
173126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
174126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
175126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
176126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
177b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
178ed2fa05eSAndrey A. Chernov 
179126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
180126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
181126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
182126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
183126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
184126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1855b81b6b3SRodney W. Grimes };
1865b81b6b3SRodney W. Grimes 
187b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
1885b81b6b3SRodney W. Grimes /***********************************************************************\
1895b81b6b3SRodney W. Grimes * Per controller structure.						*
1905b81b6b3SRodney W. Grimes \***********************************************************************/
191b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
1925b81b6b3SRodney W. Grimes 
1935b81b6b3SRodney W. Grimes /***********************************************************************\
1945b81b6b3SRodney W. Grimes * Per drive structure.							*
195b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1965b81b6b3SRodney W. Grimes \***********************************************************************/
1975b81b6b3SRodney W. Grimes struct fd_data {
198b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1995b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2003a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
2015b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
2025b81b6b3SRodney W. Grimes 	int	flags;
2035b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
2045b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
2055b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
2065b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
2075b81b6b3SRodney W. Grimes 	int	skip;
2085b81b6b3SRodney W. Grimes 	int	hddrv;
2095b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
2103a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
21192200632SGarrett Wollman 	int	dkunit;		/* disk stats unit number */
2125b81b6b3SRodney W. Grimes } fd_data[NFD];
2135b81b6b3SRodney W. Grimes 
2145b81b6b3SRodney W. Grimes /***********************************************************************\
2155b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
2165b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
2175b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
2185b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
2195b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
2205b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
2215b81b6b3SRodney W. Grimes \***********************************************************************/
222b99f0a4aSAndrew Moore 
2233a2f7427SDavid Greenman #if NFT > 0
2243a2f7427SDavid Greenman int ftopen(dev_t, int);
2253a2f7427SDavid Greenman int ftintr(ftu_t ftu);
2263a2f7427SDavid Greenman int ftclose(dev_t, int);
2273a2f7427SDavid Greenman void ftstrategy(struct buf *);
2283a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
2293a2f7427SDavid Greenman int ftdump(dev_t);
2303a2f7427SDavid Greenman int ftsize(dev_t);
2313a2f7427SDavid Greenman int ftattach(struct isa_device *, struct isa_device *);
2323a2f7427SDavid Greenman #endif
2335b81b6b3SRodney W. Grimes 
2343a2f7427SDavid Greenman /* autoconfig functions */
2353a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2363a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2373a2f7427SDavid Greenman 
2383a2f7427SDavid Greenman /* exported functions */
2393a2f7427SDavid Greenman int fdsize (dev_t);
2403a2f7427SDavid Greenman void fdintr(fdcu_t);
2413a2f7427SDavid Greenman int Fdopen(dev_t, int);
2423a2f7427SDavid Greenman int fdclose(dev_t, int);
2433a2f7427SDavid Greenman void fdstrategy(struct buf *);
2443a2f7427SDavid Greenman int fdioctl(dev_t, int, caddr_t, int, struct proc *);
2453a2f7427SDavid Greenman 
2463a2f7427SDavid Greenman /* needed for ft driver, thus exported */
2473a2f7427SDavid Greenman int in_fdc(fdcu_t);
2483a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
2493a2f7427SDavid Greenman 
2503a2f7427SDavid Greenman /* internal functions */
2513a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
2523a2f7427SDavid Greenman #  define TURNON 1
2533a2f7427SDavid Greenman #  define TURNOFF 0
2543a2f7427SDavid Greenman static timeout_t fd_turnoff;
2553a2f7427SDavid Greenman static timeout_t fd_motor_on;
2563a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2573a2f7427SDavid Greenman static void fdc_reset(fdc_p);
2583a2f7427SDavid Greenman static void fdstart(fdcu_t);
2593a2f7427SDavid Greenman static timeout_t fd_timeout;
2603a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2613a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
262aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2633a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2643a2f7427SDavid Greenman 
265aaf08d94SGarrett Wollman 
2665b81b6b3SRodney W. Grimes #define DEVIDLE		0
2675b81b6b3SRodney W. Grimes #define FINDWORK	1
2685b81b6b3SRodney W. Grimes #define	DOSEEK		2
2695b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
2705b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
2715b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
2725b81b6b3SRodney W. Grimes #define	STARTRECAL	6
2735b81b6b3SRodney W. Grimes #define	RESETCTLR	7
2745b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
2755b81b6b3SRodney W. Grimes #define	RECALWAIT	9
2765b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
2775b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
2785b81b6b3SRodney W. Grimes 
2795b81b6b3SRodney W. Grimes #ifdef	DEBUG
2805b81b6b3SRodney W. Grimes char *fdstates[] =
2815b81b6b3SRodney W. Grimes {
2825b81b6b3SRodney W. Grimes "DEVIDLE",
2835b81b6b3SRodney W. Grimes "FINDWORK",
2845b81b6b3SRodney W. Grimes "DOSEEK",
2855b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2865b81b6b3SRodney W. Grimes "IOCOMPLETE",
2875b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2885b81b6b3SRodney W. Grimes "STARTRECAL",
2895b81b6b3SRodney W. Grimes "RESETCTLR",
2905b81b6b3SRodney W. Grimes "SEEKWAIT",
2915b81b6b3SRodney W. Grimes "RECALWAIT",
2925b81b6b3SRodney W. Grimes "MOTORWAIT",
2935b81b6b3SRodney W. Grimes "IOTIMEDOUT"
2945b81b6b3SRodney W. Grimes };
2955b81b6b3SRodney W. Grimes 
2963a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
2973a2f7427SDavid Greenman int	fd_debug = 0;
2985b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2995b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
300381fe1aaSGarrett Wollman #else /* DEBUG */
3015b81b6b3SRodney W. Grimes #define TRACE0(arg)
3025b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
303381fe1aaSGarrett Wollman #endif /* DEBUG */
3045b81b6b3SRodney W. Grimes 
30592200632SGarrett Wollman struct isa_device *fdcdevs[NFDC];
30692200632SGarrett Wollman 
30792200632SGarrett Wollman /*
30892200632SGarrett Wollman  * Provide hw.devconf information.
30992200632SGarrett Wollman  */
31092200632SGarrett Wollman static int
31192200632SGarrett Wollman fd_externalize(struct proc *p, struct kern_devconf *kdc, void *userp, size_t len)
31292200632SGarrett Wollman {
31392200632SGarrett Wollman 	return disk_externalize(fd_data[kdc->kdc_unit].fdsu, userp, &len);
31492200632SGarrett Wollman }
31592200632SGarrett Wollman 
31692200632SGarrett Wollman static int
31792200632SGarrett Wollman fdc_externalize(struct proc *p, struct kern_devconf *kdc, void *userp, size_t len)
31892200632SGarrett Wollman {
31992200632SGarrett Wollman 	return isa_externalize(fdcdevs[kdc->kdc_unit], userp, &len);
32092200632SGarrett Wollman }
32192200632SGarrett Wollman 
3225b81b6b3SRodney W. Grimes /****************************************************************************/
3235b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
3245b81b6b3SRodney W. Grimes /****************************************************************************/
325b99f0a4aSAndrew Moore struct	isa_driver fdcdriver = {
326b99f0a4aSAndrew Moore 	fdprobe, fdattach, "fdc",
3275b81b6b3SRodney W. Grimes };
3285b81b6b3SRodney W. Grimes 
3295b81b6b3SRodney W. Grimes /*
3305b81b6b3SRodney W. Grimes  * probe for existance of controller
3315b81b6b3SRodney W. Grimes  */
3323a2f7427SDavid Greenman static int
3335b81b6b3SRodney W. Grimes fdprobe(dev)
3345b81b6b3SRodney W. Grimes 	struct isa_device *dev;
3355b81b6b3SRodney W. Grimes {
3365b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
3375b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
3385b81b6b3SRodney W. Grimes 	{
3395b81b6b3SRodney W. Grimes 		printf("fdc: same unit (%d) used multiple times\n", fdcu);
3405b81b6b3SRodney W. Grimes 		return 0;
3415b81b6b3SRodney W. Grimes 	}
3425b81b6b3SRodney W. Grimes 
34392200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
3445b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
3455b81b6b3SRodney W. Grimes 
34616111cedSAndrew Moore 	/* First - lets reset the floppy controller */
3473a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
34816111cedSAndrew Moore 	DELAY(100);
3493a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
35016111cedSAndrew Moore 
3515b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
3525b81b6b3SRodney W. Grimes 	if (out_fdc(fdcu, NE7CMD_SPECIFY) < 0)
3535b81b6b3SRodney W. Grimes 	{
3545b81b6b3SRodney W. Grimes 		return(0);
3555b81b6b3SRodney W. Grimes 	}
3563a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_1(3, 240));
3573a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_2(2, 0));
3585b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
3595b81b6b3SRodney W. Grimes }
3605b81b6b3SRodney W. Grimes 
3615b81b6b3SRodney W. Grimes /*
3625b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
3635b81b6b3SRodney W. Grimes  */
3643a2f7427SDavid Greenman static int
3655b81b6b3SRodney W. Grimes fdattach(dev)
3665b81b6b3SRodney W. Grimes 	struct isa_device *dev;
3675b81b6b3SRodney W. Grimes {
3683a2f7427SDavid Greenman 	unsigned fdt;
3695b81b6b3SRodney W. Grimes 	fdu_t	fdu;
3705b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
3715b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
3725b81b6b3SRodney W. Grimes 	fd_p	fd;
3736b7bd95bSJoerg Wunsch 	int	fdsu, st0, i;
374b99f0a4aSAndrew Moore 	struct isa_device *fdup;
37592200632SGarrett Wollman 	struct kern_devconf *kdc;
37692200632SGarrett Wollman 
37792200632SGarrett Wollman 	MALLOC(kdc, struct kern_devconf *, sizeof *kdc, M_TEMP, M_NOWAIT);
37892200632SGarrett Wollman 	if(!kdc)
37992200632SGarrett Wollman 		return 0;
38092200632SGarrett Wollman 	*kdc = kdc_fdc_template;
38192200632SGarrett Wollman 	kdc->kdc_unit = fdcu;
382f1d81093SGarrett Wollman 	kdc->kdc_isa = dev;
38392200632SGarrett Wollman 	dev_attach(kdc);
3845b81b6b3SRodney W. Grimes 
3855b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
3865b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
3875b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
3885b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
3893a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
3903a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
391b99f0a4aSAndrew Moore 	printf("fdc%d:", fdcu);
3925b81b6b3SRodney W. Grimes 
3935b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
394b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
395b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
396b99f0a4aSAndrew Moore 			continue;
397b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
398b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
399b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
400b99f0a4aSAndrew Moore 			continue;
401b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
402b99f0a4aSAndrew Moore 				/* look up what bios thinks we have */
403b99f0a4aSAndrew Moore 		switch (fdu) {
404b99f0a4aSAndrew Moore 			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
405b99f0a4aSAndrew Moore 				break;
406b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
407b99f0a4aSAndrew Moore 				break;
408b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
409b99f0a4aSAndrew Moore 				break;
410b99f0a4aSAndrew Moore 		}
4115b81b6b3SRodney W. Grimes 		/* is there a unit? */
412b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
413b99f0a4aSAndrew Moore #if NFT > 0
414b99f0a4aSAndrew Moore 		|| (fdsu >= DRVS_PER_CTLR)) {
415b99f0a4aSAndrew Moore #else
416b99f0a4aSAndrew Moore 		) {
41756ef0285SAndrew Moore 			fd->type = NO_TYPE;
418b99f0a4aSAndrew Moore #endif
419b99f0a4aSAndrew Moore #if NFT > 0
420b99f0a4aSAndrew Moore 				/* If BIOS says no floppy, or > 2nd device */
421b99f0a4aSAndrew Moore 				/* Probe for and attach a floppy tape.     */
422b99f0a4aSAndrew Moore 			if (ftattach(dev, fdup))
423b99f0a4aSAndrew Moore 				continue;
42456ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
425b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
42656ef0285SAndrew Moore #endif
4275b81b6b3SRodney W. Grimes 			continue;
428f5f7ba03SJordan K. Hubbard 		}
4295b81b6b3SRodney W. Grimes 
4305b81b6b3SRodney W. Grimes 		/* select it */
4313a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
4326b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
4336b7bd95bSJoerg Wunsch 		out_fdc(fdcu, NE7CMD_SENSED);
4346b7bd95bSJoerg Wunsch 		out_fdc(fdcu, fdsu);
4356b7bd95bSJoerg Wunsch 		if(in_fdc(fdcu) & NE7_ST3_T0) {
4366b7bd95bSJoerg Wunsch 			/* if at track 0, first seek inwards */
4373a2f7427SDavid Greenman 			out_fdc(fdcu, NE7CMD_SEEK); /* seek some steps... */
4385b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fdsu);
4393a2f7427SDavid Greenman 			out_fdc(fdcu, 10);
4406b7bd95bSJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
4416b7bd95bSJoerg Wunsch 			out_fdc(fdcu, NE7CMD_SENSEI); /* make ctrlr happy */
4423a2f7427SDavid Greenman 			(void)in_fdc(fdcu);
4433a2f7427SDavid Greenman 			(void)in_fdc(fdcu);
4446b7bd95bSJoerg Wunsch 		}
4456b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
4466b7bd95bSJoerg Wunsch 			/*
4476b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
4486b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
4496b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
4506b7bd95bSJoerg Wunsch 			 * more than 77 steps
4516b7bd95bSJoerg Wunsch 			 */
4526b7bd95bSJoerg Wunsch 			out_fdc(fdcu, NE7CMD_RECAL); /* go back to 0 */
4533a2f7427SDavid Greenman 			out_fdc(fdcu, fdsu);
4546b7bd95bSJoerg Wunsch 			/* a second being enough for full stroke seek */
4556b7bd95bSJoerg Wunsch 			DELAY(i == 0? 1000000: 300000);
4565b81b6b3SRodney W. Grimes 
4576b7bd95bSJoerg Wunsch 			/* anything responding? */
4585b81b6b3SRodney W. Grimes 			out_fdc(fdcu, NE7CMD_SENSEI);
4595b81b6b3SRodney W. Grimes 			st0 = in_fdc(fdcu);
4603a2f7427SDavid Greenman 			(void)in_fdc(fdcu);
4616b7bd95bSJoerg Wunsch 
4626b7bd95bSJoerg Wunsch 			if ((st0 & NE7_ST0_EC) == 0)
4636b7bd95bSJoerg Wunsch 				break; /* already probed succesfully */
4646b7bd95bSJoerg Wunsch 		}
4656b7bd95bSJoerg Wunsch 
4663a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
4673a2f7427SDavid Greenman 
4683a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
4695b81b6b3SRodney W. Grimes 			continue;
4705b81b6b3SRodney W. Grimes 
471b99f0a4aSAndrew Moore 		fd->track = -2;
472b99f0a4aSAndrew Moore 		fd->fdc = fdc;
473b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
4743a2f7427SDavid Greenman 		fd->options = 0;
475b99f0a4aSAndrew Moore 		printf(" [%d: fd%d: ", fdsu, fdu);
4765b81b6b3SRodney W. Grimes 
477b99f0a4aSAndrew Moore 		switch (fdt) {
4787ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
479b99f0a4aSAndrew Moore 			printf("1.2MB 5.25in]");
480b99f0a4aSAndrew Moore 			fd->type = FD_1200;
4817ca0641bSAndrey A. Chernov 			break;
4827ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
483b99f0a4aSAndrew Moore 			printf("1.44MB 3.5in]");
484b99f0a4aSAndrew Moore 			fd->type = FD_1440;
4857ca0641bSAndrey A. Chernov 			break;
4867ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
487b99f0a4aSAndrew Moore 			printf("360KB 5.25in]");
488b99f0a4aSAndrew Moore 			fd->type = FD_360;
4897ca0641bSAndrey A. Chernov 			break;
490ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
491b99f0a4aSAndrew Moore 			printf("720KB 3.5in]");
492b99f0a4aSAndrew Moore 			fd->type = FD_720;
493ed2fa05eSAndrey A. Chernov 			break;
4947ca0641bSAndrey A. Chernov 		default:
495b99f0a4aSAndrew Moore 			printf("unknown]");
496b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
4977ca0641bSAndrey A. Chernov 			break;
4985b81b6b3SRodney W. Grimes 		}
49992200632SGarrett Wollman 		fd_registerdev(fdcu, fdu);
50092200632SGarrett Wollman 		if(dk_ndrive < DK_NDRIVE) {
50192200632SGarrett Wollman 			sprintf(dk_names[dk_ndrive], "fd%d", fdu);
50292200632SGarrett Wollman 			dk_wpms[dk_ndrive] = (500 * 1024 / 2) / 1000;
50392200632SGarrett Wollman 			fd->dkunit = dk_ndrive++;
50492200632SGarrett Wollman 		} else {
50592200632SGarrett Wollman 			fd->dkunit = -1;
50692200632SGarrett Wollman 		}
5075b81b6b3SRodney W. Grimes 	}
508b99f0a4aSAndrew Moore 	printf("\n");
5095b81b6b3SRodney W. Grimes 
5103a2f7427SDavid Greenman 	return (1);
5115b81b6b3SRodney W. Grimes }
5125b81b6b3SRodney W. Grimes 
5135b81b6b3SRodney W. Grimes int
5145b81b6b3SRodney W. Grimes fdsize(dev)
5155b81b6b3SRodney W. Grimes 	dev_t	dev;
5165b81b6b3SRodney W. Grimes {
5175b81b6b3SRodney W. Grimes 	return(0);
5185b81b6b3SRodney W. Grimes }
5195b81b6b3SRodney W. Grimes 
5205b81b6b3SRodney W. Grimes /****************************************************************************/
5215b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
5225b81b6b3SRodney W. Grimes /*		remember to not deselect the drive we're working on         */
5235b81b6b3SRodney W. Grimes /****************************************************************************/
5243a2f7427SDavid Greenman static void
5253a2f7427SDavid Greenman set_motor(fdcu, fdsu, turnon)
526f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
5273a2f7427SDavid Greenman 	int fdsu;
5283a2f7427SDavid Greenman 	int turnon;
5295b81b6b3SRodney W. Grimes {
5303a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
5313a2f7427SDavid Greenman 	int needspecify = 0;
5323a2f7427SDavid Greenman 
5333a2f7427SDavid Greenman 	if(turnon) {
5343a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
5353a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
5363a2f7427SDavid Greenman 	} else
5373a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
5383a2f7427SDavid Greenman 
5393a2f7427SDavid Greenman 	if(!turnon
5403a2f7427SDavid Greenman 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
5413a2f7427SDavid Greenman 		/* gonna turn off the last drive, put FDC to bed */
5423a2f7427SDavid Greenman 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
5433a2f7427SDavid Greenman 	else {
5443a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
5453a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
5463a2f7427SDavid Greenman 			needspecify = 1;
5473a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
5485b81b6b3SRodney W. Grimes 	}
5495b81b6b3SRodney W. Grimes 
5503a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
5513a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
5523a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
5533a2f7427SDavid Greenman 
5543a2f7427SDavid Greenman 	if(needspecify) {
555dc8603e3SJoerg Wunsch 		/*
556dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
557dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
558dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
559dc8603e3SJoerg Wunsch 		 */
5603a2f7427SDavid Greenman 		out_fdc(fdcu, NE7CMD_SPECIFY);
5613a2f7427SDavid Greenman 		out_fdc(fdcu, NE7_SPEC_1(3, 240));
5623a2f7427SDavid Greenman 		out_fdc(fdcu, NE7_SPEC_2(2, 0));
5633a2f7427SDavid Greenman 	}
5643a2f7427SDavid Greenman }
5653a2f7427SDavid Greenman 
5663a2f7427SDavid Greenman /* ARGSUSED */
567381fe1aaSGarrett Wollman static void
568d0917939SPaul Richards fd_turnoff(void *arg1)
5695b81b6b3SRodney W. Grimes {
570381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
571f5f7ba03SJordan K. Hubbard 	int	s;
572f5f7ba03SJordan K. Hubbard 
5735b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
574f5f7ba03SJordan K. Hubbard 	s = splbio();
5755b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
5763a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
577f5f7ba03SJordan K. Hubbard 	splx(s);
5785b81b6b3SRodney W. Grimes }
5795b81b6b3SRodney W. Grimes 
5803a2f7427SDavid Greenman /* ARGSUSED */
5813a2f7427SDavid Greenman static void
582d0917939SPaul Richards fd_motor_on(void *arg1)
5835b81b6b3SRodney W. Grimes {
584381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
585f5f7ba03SJordan K. Hubbard 	int	s;
586f5f7ba03SJordan K. Hubbard 
5875b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
588f5f7ba03SJordan K. Hubbard 	s = splbio();
5895b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
5905b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
5915b81b6b3SRodney W. Grimes 	{
592f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
5935b81b6b3SRodney W. Grimes 	}
594f5f7ba03SJordan K. Hubbard 	splx(s);
5955b81b6b3SRodney W. Grimes }
5965b81b6b3SRodney W. Grimes 
5973a2f7427SDavid Greenman static void
598f5f7ba03SJordan K. Hubbard fd_turnon(fdu)
599f5f7ba03SJordan K. Hubbard 	fdu_t fdu;
6005b81b6b3SRodney W. Grimes {
6015b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
6025b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
6035b81b6b3SRodney W. Grimes 	{
6043a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
6053a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
606d0917939SPaul Richards 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
6075b81b6b3SRodney W. Grimes 	}
6085b81b6b3SRodney W. Grimes }
6095b81b6b3SRodney W. Grimes 
610381fe1aaSGarrett Wollman static void
6113a2f7427SDavid Greenman fdc_reset(fdc)
6123a2f7427SDavid Greenman 	fdc_p fdc;
6135b81b6b3SRodney W. Grimes {
6143a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
6153a2f7427SDavid Greenman 
6163a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
6173a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
6183a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
6193a2f7427SDavid Greenman 	DELAY(100);
6203a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
6213a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
6223a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
6233a2f7427SDavid Greenman 	DELAY(100);
6243a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
6253a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
6263a2f7427SDavid Greenman 
627dc8603e3SJoerg Wunsch 	/* after a reset, silently believe the FDC will accept commands */
6283a2f7427SDavid Greenman 	out_fdc(fdcu, NE7CMD_SPECIFY);
6293a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_1(3, 240));
6303a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_2(2, 0));
6315b81b6b3SRodney W. Grimes }
6325b81b6b3SRodney W. Grimes 
6335b81b6b3SRodney W. Grimes /****************************************************************************/
6345b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
6355b81b6b3SRodney W. Grimes /****************************************************************************/
6365b81b6b3SRodney W. Grimes int
637f5f7ba03SJordan K. Hubbard in_fdc(fdcu)
638f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
6395b81b6b3SRodney W. Grimes {
6405b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
6415b81b6b3SRodney W. Grimes 	int i, j = 100000;
6423a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
6435b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
6445b81b6b3SRodney W. Grimes 		if (i == NE7_RQM) return -1;
6455b81b6b3SRodney W. Grimes 	if (j <= 0)
6465b81b6b3SRodney W. Grimes 		return(-1);
6475b81b6b3SRodney W. Grimes #ifdef	DEBUG
6483a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
6493a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
6505b81b6b3SRodney W. Grimes 	return(i);
6515b81b6b3SRodney W. Grimes #else
6523a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
6535b81b6b3SRodney W. Grimes #endif
6545b81b6b3SRodney W. Grimes }
6555b81b6b3SRodney W. Grimes 
656381fe1aaSGarrett Wollman int
657f5f7ba03SJordan K. Hubbard out_fdc(fdcu, x)
658f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
659f5f7ba03SJordan K. Hubbard 	int x;
6605b81b6b3SRodney W. Grimes {
6615b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
6623b3837dbSRodney W. Grimes 	int i;
6635b81b6b3SRodney W. Grimes 
6643b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
6653b3837dbSRodney W. Grimes 	i = 100000;
6663a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
6673b3837dbSRodney W. Grimes 	if (i <= 0) return (-1);	/* Floppy timed out */
6683b3837dbSRodney W. Grimes 
6693b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
6703b3837dbSRodney W. Grimes 	i = 100000;
6713a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
6723b3837dbSRodney W. Grimes 	if (i <= 0) return (-1);	/* Floppy timed out */
6733b3837dbSRodney W. Grimes 
6743b3837dbSRodney W. Grimes 	/* Send the command and return */
6753a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
6763a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
6775b81b6b3SRodney W. Grimes 	return (0);
6785b81b6b3SRodney W. Grimes }
6795b81b6b3SRodney W. Grimes 
6805b81b6b3SRodney W. Grimes /****************************************************************************/
6815b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
6825b81b6b3SRodney W. Grimes /****************************************************************************/
683381fe1aaSGarrett Wollman int
6845b81b6b3SRodney W. Grimes Fdopen(dev, flags)
6855b81b6b3SRodney W. Grimes 	dev_t	dev;
6865b81b6b3SRodney W. Grimes 	int	flags;
6875b81b6b3SRodney W. Grimes {
6885b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
68920a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
690b99f0a4aSAndrew Moore 	fdc_p	fdc;
6915b81b6b3SRodney W. Grimes 
692b99f0a4aSAndrew Moore #if NFT > 0
693b99f0a4aSAndrew Moore 	/* check for a tape open */
694b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
695b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
696b99f0a4aSAndrew Moore #endif
6975b81b6b3SRodney W. Grimes 	/* check bounds */
698b99f0a4aSAndrew Moore 	if (fdu >= NFD)
699b99f0a4aSAndrew Moore 		return(ENXIO);
700b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
701b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
702b99f0a4aSAndrew Moore 		return(ENXIO);
703b99f0a4aSAndrew Moore 	if (type > NUMDENS)
704b99f0a4aSAndrew Moore 		return(ENXIO);
7057ca0641bSAndrey A. Chernov 	if (type == 0)
7067ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
7077ca0641bSAndrey A. Chernov 	else {
7087ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
709fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
7107ca0641bSAndrey A. Chernov 			case FD_360:
7117ca0641bSAndrey A. Chernov 				return(ENXIO);
712ed2fa05eSAndrey A. Chernov 			case FD_720:
713b39c878eSAndrey A. Chernov 				if (   type != FD_820
714b39c878eSAndrey A. Chernov 				    && type != FD_800
715ed2fa05eSAndrey A. Chernov 				   )
716ed2fa05eSAndrey A. Chernov 					return(ENXIO);
717ed2fa05eSAndrey A. Chernov 				break;
7187ca0641bSAndrey A. Chernov 			case FD_1200:
719b39c878eSAndrey A. Chernov 				switch (type) {
720b39c878eSAndrey A. Chernov 				case FD_1480:
721b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
722fa4700b4SAndrey A. Chernov 					break;
7237ca0641bSAndrey A. Chernov 				case FD_1440:
724b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
725b39c878eSAndrey A. Chernov 					break;
726b39c878eSAndrey A. Chernov 				case FD_820:
727b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
728b39c878eSAndrey A. Chernov 					break;
729b39c878eSAndrey A. Chernov 				case FD_800:
730b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
731b39c878eSAndrey A. Chernov 					break;
732b39c878eSAndrey A. Chernov 				case FD_720:
733b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
734b39c878eSAndrey A. Chernov 					break;
735b39c878eSAndrey A. Chernov 				case FD_360:
736b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
737b39c878eSAndrey A. Chernov 					break;
738b39c878eSAndrey A. Chernov 				default:
739b39c878eSAndrey A. Chernov 					return(ENXIO);
740b39c878eSAndrey A. Chernov 				}
741b39c878eSAndrey A. Chernov 				break;
742b39c878eSAndrey A. Chernov 			case FD_1440:
743b39c878eSAndrey A. Chernov 				if (   type != FD_1720
744b39c878eSAndrey A. Chernov 				    && type != FD_1480
745ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
746b39c878eSAndrey A. Chernov 				    && type != FD_820
747b39c878eSAndrey A. Chernov 				    && type != FD_800
748b39c878eSAndrey A. Chernov 				    && type != FD_720
7497ca0641bSAndrey A. Chernov 				    )
750dffff499SAndrey A. Chernov 					return(ENXIO);
751fa4700b4SAndrey A. Chernov 				break;
7527ca0641bSAndrey A. Chernov 			}
7537ca0641bSAndrey A. Chernov 		}
754fa4700b4SAndrey A. Chernov 	}
755b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
7565b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
7575b81b6b3SRodney W. Grimes 
7585b81b6b3SRodney W. Grimes 	return 0;
7595b81b6b3SRodney W. Grimes }
7605b81b6b3SRodney W. Grimes 
761381fe1aaSGarrett Wollman int
7625b81b6b3SRodney W. Grimes fdclose(dev, flags)
7635b81b6b3SRodney W. Grimes 	dev_t dev;
764381fe1aaSGarrett Wollman 	int flags;
7655b81b6b3SRodney W. Grimes {
7665b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
767b99f0a4aSAndrew Moore 
768b99f0a4aSAndrew Moore #if NFT > 0
7693a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
7703a2f7427SDavid Greenman 
771b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
7723a2f7427SDavid Greenman 		return ftclose(dev, flags);
773b99f0a4aSAndrew Moore #endif
7745b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
7753a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
7765b81b6b3SRodney W. Grimes 	return(0);
7775b81b6b3SRodney W. Grimes }
7785b81b6b3SRodney W. Grimes 
7795b81b6b3SRodney W. Grimes 
7803a2f7427SDavid Greenman /****************************************************************************/
7813a2f7427SDavid Greenman /*                               fdstrategy                                 */
7823a2f7427SDavid Greenman /****************************************************************************/
7833a2f7427SDavid Greenman void
7843a2f7427SDavid Greenman fdstrategy(struct buf *bp)
7853a2f7427SDavid Greenman {
7863a2f7427SDavid Greenman 	register struct buf *dp;
7873a2f7427SDavid Greenman 	long nblocks, blknum;
7883a2f7427SDavid Greenman  	int	s;
7893a2f7427SDavid Greenman  	fdcu_t	fdcu;
7903a2f7427SDavid Greenman  	fdu_t	fdu;
7913a2f7427SDavid Greenman  	fdc_p	fdc;
7923a2f7427SDavid Greenman  	fd_p	fd;
7933a2f7427SDavid Greenman 	size_t	fdblk;
7943a2f7427SDavid Greenman 
7953a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
7963a2f7427SDavid Greenman 	fd = &fd_data[fdu];
7973a2f7427SDavid Greenman 	fdc = fd->fdc;
7983a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
7993a2f7427SDavid Greenman 	fdblk = 128 << (fd->ft->secsize);
8003a2f7427SDavid Greenman 
8013a2f7427SDavid Greenman #if NFT > 0
8023a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
8033a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
8043a2f7427SDavid Greenman 		bp->b_error = ENXIO;
8053a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
8063a2f7427SDavid Greenman 		goto bad;
8073a2f7427SDavid Greenman 	}
8083a2f7427SDavid Greenman 	/* check for controller already busy with tape */
8093a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
8103a2f7427SDavid Greenman 		bp->b_error = EBUSY;
8113a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
8123a2f7427SDavid Greenman 		goto bad;
8133a2f7427SDavid Greenman 	}
8143a2f7427SDavid Greenman #endif
8153a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
8163a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
817702c623aSPoul-Henning Kamp 			printf("fdstrat: fdu = %d, blkno = %lu, bcount = %ld\n",
818702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
8193a2f7427SDavid Greenman 			bp->b_error = EINVAL;
8203a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
8213a2f7427SDavid Greenman 			goto bad;
8223a2f7427SDavid Greenman 		}
8233a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
8243a2f7427SDavid Greenman 			bp->b_error = EINVAL;
8253a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
8263a2f7427SDavid Greenman 			goto bad;
8273a2f7427SDavid Greenman 		}
8283a2f7427SDavid Greenman 	}
8293a2f7427SDavid Greenman 
8303a2f7427SDavid Greenman 	/*
8313a2f7427SDavid Greenman 	 * Set up block calculations.
8323a2f7427SDavid Greenman 	 */
8333a2f7427SDavid Greenman 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk;
8343a2f7427SDavid Greenman  	nblocks = fd->ft->size;
8353a2f7427SDavid Greenman 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
8363a2f7427SDavid Greenman 		if (blknum == nblocks) {
8373a2f7427SDavid Greenman 			bp->b_resid = bp->b_bcount;
8383a2f7427SDavid Greenman 		} else {
8393a2f7427SDavid Greenman 			bp->b_error = ENOSPC;
8403a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
8413a2f7427SDavid Greenman 		}
8423a2f7427SDavid Greenman 		goto bad;
8433a2f7427SDavid Greenman 	}
8443a2f7427SDavid Greenman  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
8453a2f7427SDavid Greenman 	dp = &(fdc->head);
8463a2f7427SDavid Greenman 	s = splbio();
8473a2f7427SDavid Greenman 	disksort(dp, bp);
8483a2f7427SDavid Greenman 	untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */
8493a2f7427SDavid Greenman 	fdstart(fdcu);
8503a2f7427SDavid Greenman 	splx(s);
8513a2f7427SDavid Greenman 	return;
8523a2f7427SDavid Greenman 
8533a2f7427SDavid Greenman bad:
8543a2f7427SDavid Greenman 	biodone(bp);
8553a2f7427SDavid Greenman }
8563a2f7427SDavid Greenman 
8575b81b6b3SRodney W. Grimes /***************************************************************\
8585b81b6b3SRodney W. Grimes *				fdstart				*
8595b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
8605b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
8615b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
8625b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
8635b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
8645b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
8655b81b6b3SRodney W. Grimes \***************************************************************/
866381fe1aaSGarrett Wollman static void
867f5f7ba03SJordan K. Hubbard fdstart(fdcu)
868f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
8695b81b6b3SRodney W. Grimes {
8705b81b6b3SRodney W. Grimes 	int s;
8715b81b6b3SRodney W. Grimes 
8725b81b6b3SRodney W. Grimes 	s = splbio();
8735b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
8745b81b6b3SRodney W. Grimes 	{
8755b81b6b3SRodney W. Grimes 		fdintr(fdcu);
8765b81b6b3SRodney W. Grimes 	}
8775b81b6b3SRodney W. Grimes 	splx(s);
8785b81b6b3SRodney W. Grimes }
8795b81b6b3SRodney W. Grimes 
8803a2f7427SDavid Greenman /* ARGSUSED */
881381fe1aaSGarrett Wollman static void
882d0917939SPaul Richards fd_timeout(void *arg1)
8835b81b6b3SRodney W. Grimes {
884381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
8855b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
8863a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
8875b81b6b3SRodney W. Grimes 	struct buf *dp, *bp;
888f5f7ba03SJordan K. Hubbard 	int s;
8895b81b6b3SRodney W. Grimes 
8905b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
8915b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
8925b81b6b3SRodney W. Grimes 
8933a2f7427SDavid Greenman 	/*
8943a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
8953a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
8963a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
8973a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
8983a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
8993a2f7427SDavid Greenman 	 */
9005b81b6b3SRodney W. Grimes 
9013a2f7427SDavid Greenman 	s = splbio();
9023a2f7427SDavid Greenman 
9033a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
9043a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
9053a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
9063a2f7427SDavid Greenman 	{
9073a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
9083a2f7427SDavid Greenman 		/* yup, it is; kill it now */
9093a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
9103a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
9113a2f7427SDavid Greenman 	}
9125b81b6b3SRodney W. Grimes 
9135b81b6b3SRodney W. Grimes 	if (bp)
9145b81b6b3SRodney W. Grimes 	{
9155b81b6b3SRodney W. Grimes 		retrier(fdcu);
9163a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
9175b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
9185b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
9195b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
9205b81b6b3SRodney W. Grimes 	}
9215b81b6b3SRodney W. Grimes 	else
9225b81b6b3SRodney W. Grimes 	{
9235b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
9245b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
9255b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
9265b81b6b3SRodney W. Grimes 	}
927f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
928f5f7ba03SJordan K. Hubbard 	splx(s);
9295b81b6b3SRodney W. Grimes }
9305b81b6b3SRodney W. Grimes 
9315b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
9323a2f7427SDavid Greenman /* ARGSUSED */
933381fe1aaSGarrett Wollman static void
934d0917939SPaul Richards fd_pseudointr(void *arg1)
9355b81b6b3SRodney W. Grimes {
936381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
9375b81b6b3SRodney W. Grimes 	int	s;
9383a2f7427SDavid Greenman 
9395b81b6b3SRodney W. Grimes 	s = splbio();
9405b81b6b3SRodney W. Grimes 	fdintr(fdcu);
9415b81b6b3SRodney W. Grimes 	splx(s);
9425b81b6b3SRodney W. Grimes }
9435b81b6b3SRodney W. Grimes 
9445b81b6b3SRodney W. Grimes /***********************************************************************\
9455b81b6b3SRodney W. Grimes *                                 fdintr				*
9465b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
9475b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
9485b81b6b3SRodney W. Grimes \***********************************************************************/
949381fe1aaSGarrett Wollman void
950381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
9515b81b6b3SRodney W. Grimes {
9525b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
953b99f0a4aSAndrew Moore #if NFT > 0
954b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
955b99f0a4aSAndrew Moore 
956b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
957b99f0a4aSAndrew Moore 		(ftintr(fdu));
958b99f0a4aSAndrew Moore 	else
959b99f0a4aSAndrew Moore #endif
960381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
961381fe1aaSGarrett Wollman 			;
9625b81b6b3SRodney W. Grimes }
9635b81b6b3SRodney W. Grimes 
9645b81b6b3SRodney W. Grimes /***********************************************************************\
9655b81b6b3SRodney W. Grimes * The controller state machine.						*
9665b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
9675b81b6b3SRodney W. Grimes \***********************************************************************/
9683a2f7427SDavid Greenman static int
969381fe1aaSGarrett Wollman fdstate(fdcu, fdc)
970f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
971f5f7ba03SJordan K. Hubbard 	fdc_p fdc;
9725b81b6b3SRodney W. Grimes {
9733a2f7427SDavid Greenman 	int read, format, head, sec = 0, i = 0, sectrac, st0, cyl, st3;
9745b81b6b3SRodney W. Grimes 	unsigned long blknum;
9755b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
9765b81b6b3SRodney W. Grimes 	fd_p fd;
9775b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
978b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
9793a2f7427SDavid Greenman 	size_t fdblk;
9805b81b6b3SRodney W. Grimes 
9815b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
9825b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
9835b81b6b3SRodney W. Grimes 	if(!bp)
9845b81b6b3SRodney W. Grimes 	{
9855b81b6b3SRodney W. Grimes 		/***********************************************\
9865b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
9875b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
9885b81b6b3SRodney W. Grimes 		\***********************************************/
9895b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
9905b81b6b3SRodney W. Grimes 		if(fdc->fd)
9915b81b6b3SRodney W. Grimes 		{
9923a2f7427SDavid Greenman 			printf("unexpected valid fd pointer (fdu = %d)\n",
9933a2f7427SDavid Greenman 			       fdc->fdu);
9945b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
9955b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
9965b81b6b3SRodney W. Grimes 		}
9975b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
9985b81b6b3SRodney W. Grimes  		return(0);
9995b81b6b3SRodney W. Grimes 	}
10005b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
10015b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
10023a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
10035b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
10045b81b6b3SRodney W. Grimes 	{
10055b81b6b3SRodney W. Grimes 		printf("confused fd pointers\n");
10065b81b6b3SRodney W. Grimes 	}
10075b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1008b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1009b39c878eSAndrey A. Chernov 	if(format)
1010b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
10115b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
10125b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
10135b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
1014d0917939SPaul Richards 	untimeout(fd_turnoff, (caddr_t)fdu);
1015d0917939SPaul Richards 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
10165b81b6b3SRodney W. Grimes 	switch (fdc->state)
10175b81b6b3SRodney W. Grimes 	{
10185b81b6b3SRodney W. Grimes 	case DEVIDLE:
10195b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
10205b81b6b3SRodney W. Grimes 		fdc->retry = 0;
10215b81b6b3SRodney W. Grimes 		fd->skip = 0;
10225b81b6b3SRodney W. Grimes 		fdc->fd = fd;
10235b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
10243a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
10253a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
10265b81b6b3SRodney W. Grimes 		/*******************************************************\
10275b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
10285b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
10295b81b6b3SRodney W. Grimes 		\*******************************************************/
10305b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
10315b81b6b3SRodney W. Grimes 		{
10325b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
10335b81b6b3SRodney W. Grimes 			return(0); /* come back later */
10345b81b6b3SRodney W. Grimes 		}
10355b81b6b3SRodney W. Grimes 		/*******************************************************\
10365b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
10375b81b6b3SRodney W. Grimes 		\*******************************************************/
10385b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
10395b81b6b3SRodney W. Grimes 		{
10405b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
10415b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
10425b81b6b3SRodney W. Grimes 			return(0);
10435b81b6b3SRodney W. Grimes 		}
10445b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
10455b81b6b3SRodney W. Grimes 		{
10463a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
10475b81b6b3SRodney W. Grimes 		}
10485b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
10495b81b6b3SRodney W. Grimes 		break;
10505b81b6b3SRodney W. Grimes 	case DOSEEK:
10515b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
10525b81b6b3SRodney W. Grimes 		{
10535b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
10545b81b6b3SRodney W. Grimes 			break;
10555b81b6b3SRodney W. Grimes 		}
1056dc8603e3SJoerg Wunsch 		if(out_fdc(fdcu, NE7CMD_SEEK) < 0) /* Seek function */
1057dc8603e3SJoerg Wunsch 		{
1058dc8603e3SJoerg Wunsch 			/*
1059dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1060dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1061dc8603e3SJoerg Wunsch 			 */
1062dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
1063dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1064dc8603e3SJoerg Wunsch 		}
10655b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fd->fdsu);	/* Drive number */
10665b81b6b3SRodney W. Grimes 		out_fdc(fdcu, bp->b_cylin * fd->ft->steptrac);
10675b81b6b3SRodney W. Grimes 		fd->track = -2;
10685b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
10695b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
10705b81b6b3SRodney W. Grimes 	case SEEKWAIT:
10715b81b6b3SRodney W. Grimes 		/* allow heads to settle */
10723a2f7427SDavid Greenman 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 32);
10735b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
10745b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
10755b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
10765b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
10775b81b6b3SRodney W. Grimes 		if(fd->track == -2)
10785b81b6b3SRodney W. Grimes 		{
10795b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
10803a2f7427SDavid Greenman 			do {
10815b81b6b3SRodney W. Grimes 				out_fdc(fdcu, NE7CMD_SENSEI);
10823a2f7427SDavid Greenman 				st0 = in_fdc(fdcu);
10835b81b6b3SRodney W. Grimes 				cyl = in_fdc(fdcu);
10843a2f7427SDavid Greenman 				/*
10853a2f7427SDavid Greenman 				 * if this was a "ready changed" interrupt,
10863a2f7427SDavid Greenman 				 * fetch status again (can happen after
10873a2f7427SDavid Greenman 				 * enabling controller from reset state)
10883a2f7427SDavid Greenman 				 */
10893a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
10903a2f7427SDavid Greenman 			if (0 == descyl)
10913a2f7427SDavid Greenman 			{
10923a2f7427SDavid Greenman 				/*
10933a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
10943a2f7427SDavid Greenman 				 * really there
10953a2f7427SDavid Greenman 				 */
10963a2f7427SDavid Greenman 				out_fdc(fdcu, NE7CMD_SENSED);
10973a2f7427SDavid Greenman 				out_fdc(fdcu, fdu);
10983a2f7427SDavid Greenman 				st3 = in_fdc(fdcu);
10993a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
11003a2f7427SDavid Greenman 					printf(
11013a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
11023a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
11033a2f7427SDavid Greenman 					if(fdc->retry < 3)
11043a2f7427SDavid Greenman 						fdc->retry = 3;
11053a2f7427SDavid Greenman 					return(retrier(fdcu));
11063a2f7427SDavid Greenman 				}
11073a2f7427SDavid Greenman 			}
11085b81b6b3SRodney W. Grimes 			if (cyl != descyl)
11095b81b6b3SRodney W. Grimes 			{
11103a2f7427SDavid Greenman 				printf(
11113a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
11123a2f7427SDavid Greenman 				       fdu, descyl, cyl, st0, NE7_ST0BITS);
11135b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
11145b81b6b3SRodney W. Grimes 			}
11155b81b6b3SRodney W. Grimes 		}
11165b81b6b3SRodney W. Grimes 
11175b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
1118b39c878eSAndrey A. Chernov 		if(format)
1119b39c878eSAndrey A. Chernov 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1120b39c878eSAndrey A. Chernov 				- (char *)finfo;
11215b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
11223a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
11233a2f7427SDavid Greenman 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
11243a2f7427SDavid Greenman 			+ fd->skip/fdblk;
11255b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
11265b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
11275b81b6b3SRodney W. Grimes 		head = sec / sectrac;
11285b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
11293a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
11303a2f7427SDavid Greenman 
11313a2f7427SDavid Greenman 		if(format || !read)
11323a2f7427SDavid Greenman 		{
11333a2f7427SDavid Greenman 			/* make sure the drive is writable */
1134dc8603e3SJoerg Wunsch 			if(out_fdc(fdcu, NE7CMD_SENSED) < 0)
1135dc8603e3SJoerg Wunsch 			{
1136dc8603e3SJoerg Wunsch 				/* stuck controller? */
1137dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
1138dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1139dc8603e3SJoerg Wunsch 			}
11403a2f7427SDavid Greenman 			out_fdc(fdcu, fdu);
11413a2f7427SDavid Greenman 			st3 = in_fdc(fdcu);
11423a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
11433a2f7427SDavid Greenman 			{
11443a2f7427SDavid Greenman 				/*
11453a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
11463a2f7427SDavid Greenman 				 * in order to force the current operation
11473a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
11483a2f7427SDavid Greenman 				 * error - all error handling is done
11493a2f7427SDavid Greenman 				 * by the retrier()
11503a2f7427SDavid Greenman 				 */
11513a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
11523a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
11533a2f7427SDavid Greenman 				fdc->status[2] = 0;
11543a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
11553a2f7427SDavid Greenman 				fdc->status[4] = head;
11563a2f7427SDavid Greenman 				fdc->status[5] = sec;
11573a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
11583a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
11593a2f7427SDavid Greenman 				return (1);
11603a2f7427SDavid Greenman 			}
11613a2f7427SDavid Greenman 		}
11625b81b6b3SRodney W. Grimes 
1163b39c878eSAndrey A. Chernov 		if(format)
1164b39c878eSAndrey A. Chernov 		{
1165b39c878eSAndrey A. Chernov 			/* formatting */
1166dc8603e3SJoerg Wunsch 			if(out_fdc(fdcu, NE7CMD_FORMAT) < 0)
1167dc8603e3SJoerg Wunsch 			{
1168dc8603e3SJoerg Wunsch 				/* controller fell over */
1169dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1170dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
1171dc8603e3SJoerg Wunsch 			}
1172b39c878eSAndrey A. Chernov 			out_fdc(fdcu, head << 2 | fdu);
1173b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_secshift);
1174b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_nsecs);
1175b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_gaplen);
1176b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_fillbyte);
1177b39c878eSAndrey A. Chernov 		}
1178b39c878eSAndrey A. Chernov 		else
1179b39c878eSAndrey A. Chernov 		{
1180dc8603e3SJoerg Wunsch 			if ((read && out_fdc(fdcu, NE7CMD_READ) < 0)
1181dc8603e3SJoerg Wunsch 			    || (!read /* i.e., write */
1182dc8603e3SJoerg Wunsch 				&& out_fdc(fdcu, NE7CMD_WRITE) < 0))
11835b81b6b3SRodney W. Grimes 			{
1184dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
1185dc8603e3SJoerg Wunsch 				fdc->retry = 6;
1186dc8603e3SJoerg Wunsch 				return(retrier(fdcu));
11875b81b6b3SRodney W. Grimes 			}
11885b81b6b3SRodney W. Grimes 			out_fdc(fdcu, head << 2 | fdu);  /* head & unit */
11895b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->track);        /* track */
11905b81b6b3SRodney W. Grimes 			out_fdc(fdcu, head);
11915b81b6b3SRodney W. Grimes 			out_fdc(fdcu, sec);              /* sector XXX +1? */
11925b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->secsize);  /* sector size */
11935b81b6b3SRodney W. Grimes 			out_fdc(fdcu, sectrac);          /* sectors/track */
11945b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->gap);      /* gap size */
11955b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->datalen);  /* data length */
1196b39c878eSAndrey A. Chernov 		}
11975b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
11983a2f7427SDavid Greenman 		timeout(fd_timeout, (caddr_t)fdcu, hz);
11995b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
12005b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
1201d0917939SPaul Richards 		untimeout(fd_timeout, (caddr_t)fdcu);
12025b81b6b3SRodney W. Grimes 		for(i=0;i<7;i++)
12035b81b6b3SRodney W. Grimes 		{
12045b81b6b3SRodney W. Grimes 			fdc->status[i] = in_fdc(fdcu);
12055b81b6b3SRodney W. Grimes 		}
12063a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
12073a2f7427SDavid Greenman 		/* FALLTHROUGH */
12083a2f7427SDavid Greenman 	case IOTIMEDOUT:
12095b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
12103a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
12113a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
12125b81b6b3SRodney W. Grimes 		{
12133a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
12143a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1215b39c878eSAndrey A. Chernov                                 /*
12163a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
12173a2f7427SDavid Greenman 				 * and didn't release it in time for the
12183a2f7427SDavid Greenman 				 * next FDC transfer.
12193a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
12203a2f7427SDavid Greenman 				 * count. (vak)
1221b39c878eSAndrey A. Chernov                                  */
1222b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1223b39c878eSAndrey A. Chernov                                 return (1);
1224b39c878eSAndrey A. Chernov                         }
12253a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
12263a2f7427SDavid Greenman 				&& fdc->retry < 6)
12273a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
12283a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
12293a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
12303a2f7427SDavid Greenman 				&& fdc->retry < 3)
12313a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
12325b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
12335b81b6b3SRodney W. Grimes 		}
12345b81b6b3SRodney W. Grimes 		/* All OK */
12353a2f7427SDavid Greenman 		fd->skip += fdblk;
1236b39c878eSAndrey A. Chernov 		if (!format && fd->skip < bp->b_bcount)
12375b81b6b3SRodney W. Grimes 		{
12385b81b6b3SRodney W. Grimes 			/* set up next transfer */
12393a2f7427SDavid Greenman 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
12403a2f7427SDavid Greenman 				+ fd->skip/fdblk;
12413a2f7427SDavid Greenman 			bp->b_cylin =
12423a2f7427SDavid Greenman 				(blknum / (fd->ft->sectrac * fd->ft->heads));
12435b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
12445b81b6b3SRodney W. Grimes 		}
12455b81b6b3SRodney W. Grimes 		else
12465b81b6b3SRodney W. Grimes 		{
12475b81b6b3SRodney W. Grimes 			/* ALL DONE */
12485b81b6b3SRodney W. Grimes 			fd->skip = 0;
12495b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
125026f9a767SRodney W. Grimes 			dp->b_actf = bp->b_actf;
12515b81b6b3SRodney W. Grimes 			biodone(bp);
12525b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
12535b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
12545b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
12555b81b6b3SRodney W. Grimes 		}
12565b81b6b3SRodney W. Grimes 		return(1);
12575b81b6b3SRodney W. Grimes 	case RESETCTLR:
12583a2f7427SDavid Greenman 		fdc_reset(fdc);
12595b81b6b3SRodney W. Grimes 		fdc->retry++;
12605b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
12615b81b6b3SRodney W. Grimes 		break;
12625b81b6b3SRodney W. Grimes 	case STARTRECAL:
1263dc8603e3SJoerg Wunsch 		if(out_fdc(fdcu, NE7CMD_RECAL) < 0) /* Recalibrate Function */
1264dc8603e3SJoerg Wunsch 		{
1265dc8603e3SJoerg Wunsch 			/* arrgl */
1266dc8603e3SJoerg Wunsch 			fdc->retry = 6;
1267dc8603e3SJoerg Wunsch 			return(retrier(fdcu));
1268dc8603e3SJoerg Wunsch 		}
12695b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fdu);
12705b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
12715b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
12725b81b6b3SRodney W. Grimes 	case RECALWAIT:
12735b81b6b3SRodney W. Grimes 		/* allow heads to settle */
12743a2f7427SDavid Greenman 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 32);
12755b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
12765b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
12775b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
12783a2f7427SDavid Greenman 		do {
12795b81b6b3SRodney W. Grimes 			out_fdc(fdcu, NE7CMD_SENSEI);
12805b81b6b3SRodney W. Grimes 			st0 = in_fdc(fdcu);
12815b81b6b3SRodney W. Grimes 			cyl = in_fdc(fdcu);
12823a2f7427SDavid Greenman 			/*
12833a2f7427SDavid Greenman 			 * if this was a "ready changed" interrupt,
12843a2f7427SDavid Greenman 			 * fetch status again (can happen after
12853a2f7427SDavid Greenman 			 * enabling controller from reset state)
12863a2f7427SDavid Greenman 			 */
12873a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
12883a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
12895b81b6b3SRodney W. Grimes 		{
1290dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
1291dc8603e3SJoerg Wunsch 				/*
1292dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
1293dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
1294dc8603e3SJoerg Wunsch 				 * since people used to complain much about
1295dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
1296dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
1297dc8603e3SJoerg Wunsch 				 * time in a line
1298dc8603e3SJoerg Wunsch 				 */
1299dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
1300dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
13013a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
13025b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
13035b81b6b3SRodney W. Grimes 		}
13045b81b6b3SRodney W. Grimes 		fd->track = 0;
13055b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
13065b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
13075b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
13085b81b6b3SRodney W. Grimes 	case MOTORWAIT:
13095b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
13105b81b6b3SRodney W. Grimes 		{
13115b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
13125b81b6b3SRodney W. Grimes 		}
13133a2f7427SDavid Greenman 		/*
13143a2f7427SDavid Greenman 		 * since the controller was off, it has lost its
13153a2f7427SDavid Greenman 		 * idea about the current track it were; thus,
13163a2f7427SDavid Greenman 		 * recalibrate the bastard
13173a2f7427SDavid Greenman 		 */
13183a2f7427SDavid Greenman 		fdc->state = STARTRECAL;
13195b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
13205b81b6b3SRodney W. Grimes 	default:
13215b81b6b3SRodney W. Grimes 		printf("Unexpected FD int->");
1322dc8603e3SJoerg Wunsch 		if(out_fdc(fdcu, NE7CMD_SENSEI) < 0)
1323dc8603e3SJoerg Wunsch 		{
1324dc8603e3SJoerg Wunsch 			printf("[controller is dead now]\n");
1325dc8603e3SJoerg Wunsch 			return(0);
1326dc8603e3SJoerg Wunsch 		}
13275b81b6b3SRodney W. Grimes 		st0 = in_fdc(fdcu);
13285b81b6b3SRodney W. Grimes 		cyl = in_fdc(fdcu);
13293a2f7427SDavid Greenman 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
13303a2f7427SDavid Greenman 		out_fdc(fdcu, NE7CMD_READID);
13315b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fd->fdsu);
13325b81b6b3SRodney W. Grimes 		for(i=0;i<7;i++) {
13335b81b6b3SRodney W. Grimes 			fdc->status[i] = in_fdc(fdcu);
13345b81b6b3SRodney W. Grimes 		}
13353a2f7427SDavid Greenman 		if(fdc->status[0] != -1)
13363a2f7427SDavid Greenman 			printf("intr status :%lx %lx %lx %lx %lx %lx %lx\n",
13375b81b6b3SRodney W. Grimes 			       fdc->status[0],
13385b81b6b3SRodney W. Grimes 			       fdc->status[1],
13395b81b6b3SRodney W. Grimes 			       fdc->status[2],
13405b81b6b3SRodney W. Grimes 			       fdc->status[3],
13415b81b6b3SRodney W. Grimes 			       fdc->status[4],
13425b81b6b3SRodney W. Grimes 			       fdc->status[5],
13435b81b6b3SRodney W. Grimes 			       fdc->status[6] );
13443a2f7427SDavid Greenman 		else
13453a2f7427SDavid Greenman 			printf("FDC timed out\n");
13465b81b6b3SRodney W. Grimes 		return(0);
13475b81b6b3SRodney W. Grimes 	}
13485b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
13495b81b6b3SRodney W. Grimes }
13505b81b6b3SRodney W. Grimes 
1351aaf08d94SGarrett Wollman static int
1352f5f7ba03SJordan K. Hubbard retrier(fdcu)
1353f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
13545b81b6b3SRodney W. Grimes {
13555b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
13565b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
13575b81b6b3SRodney W. Grimes 
13585b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
13595b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
13605b81b6b3SRodney W. Grimes 
13613a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
13623a2f7427SDavid Greenman 		goto fail;
13635b81b6b3SRodney W. Grimes 	switch(fdc->retry)
13645b81b6b3SRodney W. Grimes 	{
13655b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
13665b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
13675b81b6b3SRodney W. Grimes 		break;
13685b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
13695b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
13705b81b6b3SRodney W. Grimes 		break;
13715b81b6b3SRodney W. Grimes 	case 6:
13725b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
13735b81b6b3SRodney W. Grimes 		break;
13745b81b6b3SRodney W. Grimes 	case 7:
13755b81b6b3SRodney W. Grimes 		break;
13765b81b6b3SRodney W. Grimes 	default:
13773a2f7427SDavid Greenman 	fail:
13785b81b6b3SRodney W. Grimes 		{
13797ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
13807ca0641bSAndrey A. Chernov 			/* Trick diskerr */
13813a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
13823a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
138392ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
13843a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
13853a2f7427SDavid Greenman 				(struct disklabel *)NULL);
13867ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
138792ed385aSRodney W. Grimes 			printf(" (ST0 %b ", fdc->status[0], NE7_ST0BITS);
13885b81b6b3SRodney W. Grimes 			printf(" ST1 %b ", fdc->status[1], NE7_ST1BITS);
13895b81b6b3SRodney W. Grimes 			printf(" ST2 %b ", fdc->status[2], NE7_ST2BITS);
1390702c623aSPoul-Henning Kamp 			printf("cyl %ld hd %ld sec %ld)\n",
139192ed385aSRodney W. Grimes 			       fdc->status[3], fdc->status[4], fdc->status[5]);
13925b81b6b3SRodney W. Grimes 		}
13935b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
13945b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
13955b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
139626f9a767SRodney W. Grimes 		dp->b_actf = bp->b_actf;
13975b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
13985b81b6b3SRodney W. Grimes 		biodone(bp);
139992ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
14005b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
14015b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1402f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
140392ed385aSRodney W. Grimes 		return(1);
14045b81b6b3SRodney W. Grimes 	}
14055b81b6b3SRodney W. Grimes 	fdc->retry++;
14065b81b6b3SRodney W. Grimes 	return(1);
14075b81b6b3SRodney W. Grimes }
14085b81b6b3SRodney W. Grimes 
1409b39c878eSAndrey A. Chernov static int
1410b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1411b39c878eSAndrey A. Chernov 	dev_t dev;
1412b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1413b39c878eSAndrey A. Chernov 	struct proc *p;
1414b39c878eSAndrey A. Chernov {
1415b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1416b39c878eSAndrey A. Chernov  	fd_p	fd;
1417b39c878eSAndrey A. Chernov 
1418b39c878eSAndrey A. Chernov 	struct buf *bp;
1419b39c878eSAndrey A. Chernov 	int rv = 0, s;
14203a2f7427SDavid Greenman 	size_t fdblk;
1421b39c878eSAndrey A. Chernov 
1422b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1423b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
14243a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1425b39c878eSAndrey A. Chernov 
1426b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1427b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1428b39c878eSAndrey A. Chernov 	if(bp == 0)
1429b39c878eSAndrey A. Chernov 		return ENOBUFS;
1430b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1431b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1432b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1433b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1434b39c878eSAndrey A. Chernov 
1435b39c878eSAndrey A. Chernov 	/*
1436b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1437b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1438b39c878eSAndrey A. Chernov 	 */
1439b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
14403a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1441b39c878eSAndrey A. Chernov 
1442b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1443b39c878eSAndrey A. Chernov 	bp->b_un.b_addr = (caddr_t)finfo;
1444b39c878eSAndrey A. Chernov 
1445b39c878eSAndrey A. Chernov 	/* now do the format */
1446b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1447b39c878eSAndrey A. Chernov 
1448b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1449b39c878eSAndrey A. Chernov 	s = splbio();
1450b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1451b39c878eSAndrey A. Chernov 	{
1452b39c878eSAndrey A. Chernov 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1453b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1454b39c878eSAndrey A. Chernov 			break;
1455b39c878eSAndrey A. Chernov 	}
1456b39c878eSAndrey A. Chernov 	splx(s);
1457b39c878eSAndrey A. Chernov 
1458b39c878eSAndrey A. Chernov 	if(rv == EWOULDBLOCK)
1459b39c878eSAndrey A. Chernov 		/* timed out */
1460b39c878eSAndrey A. Chernov 		rv = EIO;
14613a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
14623a2f7427SDavid Greenman 		rv = bp->b_error;
14633a2f7427SDavid Greenman 	biodone(bp);
1464b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1465b39c878eSAndrey A. Chernov 	return rv;
1466b39c878eSAndrey A. Chernov }
1467b39c878eSAndrey A. Chernov 
1468f5f7ba03SJordan K. Hubbard /*
1469f5f7ba03SJordan K. Hubbard  *
14703a2f7427SDavid Greenman  * TODO: Think about allocating buffer off stack.
1471f5f7ba03SJordan K. Hubbard  *       Don't pass uncast 0's and NULL's to read/write/setdisklabel().
1472f5f7ba03SJordan K. Hubbard  *       Watch out for NetBSD's different *disklabel() interface.
1473b39c878eSAndrey A. Chernov  *
1474f5f7ba03SJordan K. Hubbard  */
14755b81b6b3SRodney W. Grimes 
1476f5f7ba03SJordan K. Hubbard int
1477b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1478f5f7ba03SJordan K. Hubbard 	dev_t dev;
1479f5f7ba03SJordan K. Hubbard 	int cmd;
1480f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1481f5f7ba03SJordan K. Hubbard 	int flag;
1482b39c878eSAndrey A. Chernov 	struct proc *p;
1483f5f7ba03SJordan K. Hubbard {
14843a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
14853a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
14863a2f7427SDavid Greenman 	size_t fdblk;
14873a2f7427SDavid Greenman 
1488f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1489f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1490f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
14913a2f7427SDavid Greenman 	int error = 0;
1492f5f7ba03SJordan K. Hubbard 
1493b99f0a4aSAndrew Moore #if NFT > 0
1494a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1495a60eff27SNate Williams 
1496a60eff27SNate Williams 	/* check for a tape ioctl */
1497a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1498b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1499b99f0a4aSAndrew Moore #endif
1500b99f0a4aSAndrew Moore 
15013a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1502f5f7ba03SJordan K. Hubbard 
1503f5f7ba03SJordan K. Hubbard 	switch (cmd)
1504f5f7ba03SJordan K. Hubbard 	{
1505f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1506f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1507f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
15083a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
150992ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1510f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1511f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1512f5f7ba03SJordan K. Hubbard 
1513f5f7ba03SJordan K. Hubbard 		if (readdisklabel(dev, fdstrategy, dl, NULL, 0, 0) == NULL)
1514f5f7ba03SJordan K. Hubbard 			error = 0;
1515f5f7ba03SJordan K. Hubbard 		else
1516f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1517f5f7ba03SJordan K. Hubbard 
1518f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1519f5f7ba03SJordan K. Hubbard 		break;
1520f5f7ba03SJordan K. Hubbard 
1521f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1522f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1523f5f7ba03SJordan K. Hubbard 			error = EBADF;
1524f5f7ba03SJordan K. Hubbard 		break;
1525f5f7ba03SJordan K. Hubbard 
1526f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1527f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1528f5f7ba03SJordan K. Hubbard 			error = EBADF;
1529f5f7ba03SJordan K. Hubbard 		break;
1530f5f7ba03SJordan K. Hubbard 
1531f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1532f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1533f5f7ba03SJordan K. Hubbard 		{
1534f5f7ba03SJordan K. Hubbard 			error = EBADF;
1535f5f7ba03SJordan K. Hubbard 			break;
1536f5f7ba03SJordan K. Hubbard 		}
1537f5f7ba03SJordan K. Hubbard 
1538f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1539f5f7ba03SJordan K. Hubbard 
15403a2f7427SDavid Greenman 		if ((error =
15413a2f7427SDavid Greenman 		     setdisklabel ((struct disklabel *)buffer, dl, 0, NULL)))
1542f5f7ba03SJordan K. Hubbard 			break;
1543f5f7ba03SJordan K. Hubbard 
1544b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
1545b39c878eSAndrey A. Chernov 				       (struct disklabel *)buffer, NULL);
1546b39c878eSAndrey A. Chernov 		break;
1547b39c878eSAndrey A. Chernov 
1548b39c878eSAndrey A. Chernov 	case FD_FORM:
1549b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1550b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1551b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1552b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1553b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1554b39c878eSAndrey A. Chernov 		else
1555b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1556b39c878eSAndrey A. Chernov 		break;
1557b39c878eSAndrey A. Chernov 
1558b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1559b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1560f5f7ba03SJordan K. Hubbard 		break;
1561f5f7ba03SJordan K. Hubbard 
15623a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
15633a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
15643a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
15653a2f7427SDavid Greenman 			return EPERM;
15663a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
15673a2f7427SDavid Greenman 		break;
15683a2f7427SDavid Greenman 
15693a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
15703a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
15713a2f7427SDavid Greenman 		break;
15723a2f7427SDavid Greenman 
15733a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
15743a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
15753a2f7427SDavid Greenman 		break;
15763a2f7427SDavid Greenman 
1577f5f7ba03SJordan K. Hubbard 	default:
15783a2f7427SDavid Greenman 		error = ENOTTY;
1579f5f7ba03SJordan K. Hubbard 		break;
1580f5f7ba03SJordan K. Hubbard 	}
1581f5f7ba03SJordan K. Hubbard 	return (error);
1582f5f7ba03SJordan K. Hubbard }
1583f5f7ba03SJordan K. Hubbard 
1584f5f7ba03SJordan K. Hubbard #endif
15853a2f7427SDavid Greenman /*
15863a2f7427SDavid Greenman  * Hello emacs, these are the
15873a2f7427SDavid Greenman  * Local Variables:
15883a2f7427SDavid Greenman  *  c-indent-level:               8
15893a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
15903a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
15913a2f7427SDavid Greenman  *  c-brace-offset:              -8
15923a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
15933a2f7427SDavid Greenman  *  c-argdecl-indent:             8
15943a2f7427SDavid Greenman  *  c-label-offset:              -8
15953a2f7427SDavid Greenman  *  c++-hanging-braces:           1
15963a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
15973a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
15983a2f7427SDavid Greenman  *  c++-friend-offset:            0
15993a2f7427SDavid Greenman  * End:
16003a2f7427SDavid Greenman  */
1601