xref: /freebsd/sys/dev/fdc/fdc.c (revision 922006325bc565925fdcc293af63e34527194384)
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
4392200632SGarrett Wollman  *	$Id: fd.c,v 1.33 1994/10/10 01:12:25 phk 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 static int fdc_externalize(struct proc *, struct kern_devconf *, void *, size_t);
8592200632SGarrett Wollman 
8692200632SGarrett Wollman /*
8792200632SGarrett Wollman  * Templates for the kern_devconf structures used when we attach.
8892200632SGarrett Wollman  */
8992200632SGarrett Wollman static struct kern_devconf kdc_fd_template = {
9092200632SGarrett Wollman 	0, 0, 0,		/* filled in by kern_devconf.c */
9192200632SGarrett Wollman 	"fd", 0, { "fdc0", MDDT_DISK, 0 },
9292200632SGarrett Wollman 	fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN
9392200632SGarrett Wollman };
9492200632SGarrett Wollman 
9592200632SGarrett Wollman static struct kern_devconf kdc_fdc_template = {
9692200632SGarrett Wollman 	0, 0, 0,		/* filled in by kern_devconf.c */
9792200632SGarrett Wollman 	"fdc", 0, { "isa0", MDDT_ISA, 0 },
9892200632SGarrett Wollman 	fdc_externalize, 0, fdc_goaway, ISA_EXTERNALLEN
9992200632SGarrett Wollman };
10092200632SGarrett Wollman 
10192200632SGarrett Wollman static inline void
10292200632SGarrett Wollman fd_registerdev(int ctlr, int unit)
10392200632SGarrett Wollman {
10492200632SGarrett Wollman 	struct kern_devconf *kdc;
10592200632SGarrett Wollman 
10692200632SGarrett Wollman 	MALLOC(kdc, struct kern_devconf *, sizeof *kdc, M_TEMP, M_NOWAIT);
10792200632SGarrett Wollman 	if(!kdc) return;
10892200632SGarrett Wollman 	*kdc = kdc_fd_template;
10992200632SGarrett Wollman 	kdc->kdc_unit = unit;
11092200632SGarrett Wollman 	sprintf(kdc->kdc_md.mddc_parent, "fdc%d", ctlr);
11192200632SGarrett Wollman 	dev_attach(kdc);
11292200632SGarrett Wollman }
11392200632SGarrett Wollman 
11492200632SGarrett Wollman static int
11592200632SGarrett Wollman fdc_goaway(struct kern_devconf *kdc, int force)
11692200632SGarrett Wollman {
11792200632SGarrett Wollman 	if(force) {
11892200632SGarrett Wollman 		dev_detach(kdc);
11992200632SGarrett Wollman 		FREE(kdc, M_TEMP);
12092200632SGarrett Wollman 		return 0;
12192200632SGarrett Wollman 	} else {
12292200632SGarrett Wollman 		return EBUSY;	/* XXX fix */
12392200632SGarrett Wollman 	}
12492200632SGarrett Wollman }
12592200632SGarrett Wollman 
12692200632SGarrett Wollman static int
12792200632SGarrett Wollman fd_goaway(struct kern_devconf *kdc, int force)
12892200632SGarrett Wollman {
12992200632SGarrett Wollman 	dev_detach(kdc);
13092200632SGarrett Wollman 	FREE(kdc, M_TEMP);
13192200632SGarrett Wollman 	return 0;
13292200632SGarrett Wollman }
13392200632SGarrett Wollman 
134b2be795bSAndrey A. Chernov #define RAW_PART 2
1355b81b6b3SRodney W. Grimes #define b_cylin b_resid
1365b81b6b3SRodney W. Grimes 
137b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
138b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
1395b81b6b3SRodney W. Grimes 
1403a2f7427SDavid Greenman /*
1413a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
1423a2f7427SDavid Greenman  * on the controller
1433a2f7427SDavid Greenman  */
1443a2f7427SDavid Greenman #define id_physid id_scsiid
1453a2f7427SDavid Greenman 
146b39c878eSAndrey A. Chernov #define NUMTYPES 14
147b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
1487ca0641bSAndrey A. Chernov 
1493a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
150b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
151b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
152b99f0a4aSAndrew Moore #define FD_1720         1
153b99f0a4aSAndrew Moore #define FD_1480         2
154b99f0a4aSAndrew Moore #define FD_1440         3
155b99f0a4aSAndrew Moore #define FD_1200         4
156b99f0a4aSAndrew Moore #define FD_820          5
157b99f0a4aSAndrew Moore #define FD_800          6
158b99f0a4aSAndrew Moore #define FD_720          7
159b99f0a4aSAndrew Moore #define FD_360          8
160ed2fa05eSAndrey A. Chernov 
161b99f0a4aSAndrew Moore #define FD_1480in5_25   9
162b99f0a4aSAndrew Moore #define FD_1440in5_25   10
163b99f0a4aSAndrew Moore #define FD_820in5_25    11
164b99f0a4aSAndrew Moore #define FD_800in5_25    12
165b99f0a4aSAndrew Moore #define FD_720in5_25    13
166b99f0a4aSAndrew Moore #define FD_360in5_25    14
167b99f0a4aSAndrew Moore 
1687ca0641bSAndrey A. Chernov 
1695b81b6b3SRodney W. Grimes struct fd_type fd_types[NUMTYPES] =
1705b81b6b3SRodney W. Grimes {
171126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
172126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
173126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
174126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
175126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
176126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
177126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
178b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
179ed2fa05eSAndrey A. Chernov 
180126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
181126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
182126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
183126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
184126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
185126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1865b81b6b3SRodney W. Grimes };
1875b81b6b3SRodney W. Grimes 
188b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
1895b81b6b3SRodney W. Grimes /***********************************************************************\
1905b81b6b3SRodney W. Grimes * Per controller structure.						*
1915b81b6b3SRodney W. Grimes \***********************************************************************/
192b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
1935b81b6b3SRodney W. Grimes 
1945b81b6b3SRodney W. Grimes /***********************************************************************\
1955b81b6b3SRodney W. Grimes * Per drive structure.							*
196b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1975b81b6b3SRodney W. Grimes \***********************************************************************/
1985b81b6b3SRodney W. Grimes struct fd_data {
199b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
2005b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
2013a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
2025b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
2035b81b6b3SRodney W. Grimes 	int	flags;
2045b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
2055b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
2065b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
2075b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
2085b81b6b3SRodney W. Grimes 	int	skip;
2095b81b6b3SRodney W. Grimes 	int	hddrv;
2105b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
2113a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
21292200632SGarrett Wollman 	int	dkunit;		/* disk stats unit number */
2135b81b6b3SRodney W. Grimes } fd_data[NFD];
2145b81b6b3SRodney W. Grimes 
2155b81b6b3SRodney W. Grimes /***********************************************************************\
2165b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
2175b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
2185b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
2195b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
2205b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
2215b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
2225b81b6b3SRodney W. Grimes \***********************************************************************/
223b99f0a4aSAndrew Moore 
2243a2f7427SDavid Greenman #if NFT > 0
2253a2f7427SDavid Greenman int ftopen(dev_t, int);
2263a2f7427SDavid Greenman int ftintr(ftu_t ftu);
2273a2f7427SDavid Greenman int ftclose(dev_t, int);
2283a2f7427SDavid Greenman void ftstrategy(struct buf *);
2293a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
2303a2f7427SDavid Greenman int ftdump(dev_t);
2313a2f7427SDavid Greenman int ftsize(dev_t);
2323a2f7427SDavid Greenman int ftattach(struct isa_device *, struct isa_device *);
2333a2f7427SDavid Greenman #endif
2345b81b6b3SRodney W. Grimes 
2353a2f7427SDavid Greenman /* autoconfig functions */
2363a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
2373a2f7427SDavid Greenman static int fdattach(struct isa_device *);
2383a2f7427SDavid Greenman 
2393a2f7427SDavid Greenman /* exported functions */
2403a2f7427SDavid Greenman int fdsize (dev_t);
2413a2f7427SDavid Greenman void fdintr(fdcu_t);
2423a2f7427SDavid Greenman int Fdopen(dev_t, int);
2433a2f7427SDavid Greenman int fdclose(dev_t, int);
2443a2f7427SDavid Greenman void fdstrategy(struct buf *);
2453a2f7427SDavid Greenman int fdioctl(dev_t, int, caddr_t, int, struct proc *);
2463a2f7427SDavid Greenman 
2473a2f7427SDavid Greenman /* needed for ft driver, thus exported */
2483a2f7427SDavid Greenman int in_fdc(fdcu_t);
2493a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
2503a2f7427SDavid Greenman 
2513a2f7427SDavid Greenman /* internal functions */
2523a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
2533a2f7427SDavid Greenman #  define TURNON 1
2543a2f7427SDavid Greenman #  define TURNOFF 0
2553a2f7427SDavid Greenman static timeout_t fd_turnoff;
2563a2f7427SDavid Greenman static timeout_t fd_motor_on;
2573a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2583a2f7427SDavid Greenman static void fdc_reset(fdc_p);
2593a2f7427SDavid Greenman static void fdstart(fdcu_t);
2603a2f7427SDavid Greenman static timeout_t fd_timeout;
2613a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2623a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
263aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2643a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2653a2f7427SDavid Greenman 
266aaf08d94SGarrett Wollman 
2675b81b6b3SRodney W. Grimes #define DEVIDLE		0
2685b81b6b3SRodney W. Grimes #define FINDWORK	1
2695b81b6b3SRodney W. Grimes #define	DOSEEK		2
2705b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
2715b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
2725b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
2735b81b6b3SRodney W. Grimes #define	STARTRECAL	6
2745b81b6b3SRodney W. Grimes #define	RESETCTLR	7
2755b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
2765b81b6b3SRodney W. Grimes #define	RECALWAIT	9
2775b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
2785b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
2795b81b6b3SRodney W. Grimes 
2805b81b6b3SRodney W. Grimes #ifdef	DEBUG
2815b81b6b3SRodney W. Grimes char *fdstates[] =
2825b81b6b3SRodney W. Grimes {
2835b81b6b3SRodney W. Grimes "DEVIDLE",
2845b81b6b3SRodney W. Grimes "FINDWORK",
2855b81b6b3SRodney W. Grimes "DOSEEK",
2865b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2875b81b6b3SRodney W. Grimes "IOCOMPLETE",
2885b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2895b81b6b3SRodney W. Grimes "STARTRECAL",
2905b81b6b3SRodney W. Grimes "RESETCTLR",
2915b81b6b3SRodney W. Grimes "SEEKWAIT",
2925b81b6b3SRodney W. Grimes "RECALWAIT",
2935b81b6b3SRodney W. Grimes "MOTORWAIT",
2945b81b6b3SRodney W. Grimes "IOTIMEDOUT"
2955b81b6b3SRodney W. Grimes };
2965b81b6b3SRodney W. Grimes 
2973a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
2983a2f7427SDavid Greenman int	fd_debug = 0;
2995b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
3005b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
301381fe1aaSGarrett Wollman #else /* DEBUG */
3025b81b6b3SRodney W. Grimes #define TRACE0(arg)
3035b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
304381fe1aaSGarrett Wollman #endif /* DEBUG */
3055b81b6b3SRodney W. Grimes 
30692200632SGarrett Wollman struct isa_device *fdcdevs[NFDC];
30792200632SGarrett Wollman 
30892200632SGarrett Wollman /*
30992200632SGarrett Wollman  * Provide hw.devconf information.
31092200632SGarrett Wollman  */
31192200632SGarrett Wollman static int
31292200632SGarrett Wollman fd_externalize(struct proc *p, struct kern_devconf *kdc, void *userp, size_t len)
31392200632SGarrett Wollman {
31492200632SGarrett Wollman 	return disk_externalize(fd_data[kdc->kdc_unit].fdsu, userp, &len);
31592200632SGarrett Wollman }
31692200632SGarrett Wollman 
31792200632SGarrett Wollman static int
31892200632SGarrett Wollman fdc_externalize(struct proc *p, struct kern_devconf *kdc, void *userp, size_t len)
31992200632SGarrett Wollman {
32092200632SGarrett Wollman 	return isa_externalize(fdcdevs[kdc->kdc_unit], userp, &len);
32192200632SGarrett Wollman }
32292200632SGarrett Wollman 
3235b81b6b3SRodney W. Grimes /****************************************************************************/
3245b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
3255b81b6b3SRodney W. Grimes /****************************************************************************/
326b99f0a4aSAndrew Moore struct	isa_driver fdcdriver = {
327b99f0a4aSAndrew Moore 	fdprobe, fdattach, "fdc",
3285b81b6b3SRodney W. Grimes };
3295b81b6b3SRodney W. Grimes 
3305b81b6b3SRodney W. Grimes /*
3315b81b6b3SRodney W. Grimes  * probe for existance of controller
3325b81b6b3SRodney W. Grimes  */
3333a2f7427SDavid Greenman static int
3345b81b6b3SRodney W. Grimes fdprobe(dev)
3355b81b6b3SRodney W. Grimes 	struct isa_device *dev;
3365b81b6b3SRodney W. Grimes {
3375b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
3385b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
3395b81b6b3SRodney W. Grimes 	{
3405b81b6b3SRodney W. Grimes 		printf("fdc: same unit (%d) used multiple times\n", fdcu);
3415b81b6b3SRodney W. Grimes 		return 0;
3425b81b6b3SRodney W. Grimes 	}
3435b81b6b3SRodney W. Grimes 
34492200632SGarrett Wollman 	fdcdevs[fdcu] = dev;
3455b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
3465b81b6b3SRodney W. Grimes 
34716111cedSAndrew Moore 	/* First - lets reset the floppy controller */
3483a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
34916111cedSAndrew Moore 	DELAY(100);
3503a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
35116111cedSAndrew Moore 
3525b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
3535b81b6b3SRodney W. Grimes 	if (out_fdc(fdcu, NE7CMD_SPECIFY) < 0)
3545b81b6b3SRodney W. Grimes 	{
3555b81b6b3SRodney W. Grimes 		return(0);
3565b81b6b3SRodney W. Grimes 	}
3573a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_1(3, 240));
3583a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_2(2, 0));
3595b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
3605b81b6b3SRodney W. Grimes }
3615b81b6b3SRodney W. Grimes 
3625b81b6b3SRodney W. Grimes /*
3635b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
3645b81b6b3SRodney W. Grimes  */
3653a2f7427SDavid Greenman static int
3665b81b6b3SRodney W. Grimes fdattach(dev)
3675b81b6b3SRodney W. Grimes 	struct isa_device *dev;
3685b81b6b3SRodney W. Grimes {
3693a2f7427SDavid Greenman 	unsigned fdt;
3705b81b6b3SRodney W. Grimes 	fdu_t	fdu;
3715b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
3725b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
3735b81b6b3SRodney W. Grimes 	fd_p	fd;
3746b7bd95bSJoerg Wunsch 	int	fdsu, st0, i;
375b99f0a4aSAndrew Moore 	struct isa_device *fdup;
37692200632SGarrett Wollman 	struct kern_devconf *kdc;
37792200632SGarrett Wollman 
37892200632SGarrett Wollman 	MALLOC(kdc, struct kern_devconf *, sizeof *kdc, M_TEMP, M_NOWAIT);
37992200632SGarrett Wollman 	if(!kdc)
38092200632SGarrett Wollman 		return 0;
38192200632SGarrett Wollman 	*kdc = kdc_fdc_template;
38292200632SGarrett Wollman 	kdc->kdc_unit = fdcu;
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) {
5553a2f7427SDavid Greenman 		out_fdc(fdcu, NE7CMD_SPECIFY);
5563a2f7427SDavid Greenman 		out_fdc(fdcu, NE7_SPEC_1(3, 240));
5573a2f7427SDavid Greenman 		out_fdc(fdcu, NE7_SPEC_2(2, 0));
5583a2f7427SDavid Greenman 	}
5593a2f7427SDavid Greenman }
5603a2f7427SDavid Greenman 
5613a2f7427SDavid Greenman /* ARGSUSED */
562381fe1aaSGarrett Wollman static void
563d0917939SPaul Richards fd_turnoff(void *arg1)
5645b81b6b3SRodney W. Grimes {
565381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
566f5f7ba03SJordan K. Hubbard 	int	s;
567f5f7ba03SJordan K. Hubbard 
5685b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
569f5f7ba03SJordan K. Hubbard 	s = splbio();
5705b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
5713a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
572f5f7ba03SJordan K. Hubbard 	splx(s);
5735b81b6b3SRodney W. Grimes }
5745b81b6b3SRodney W. Grimes 
5753a2f7427SDavid Greenman /* ARGSUSED */
5763a2f7427SDavid Greenman static void
577d0917939SPaul Richards fd_motor_on(void *arg1)
5785b81b6b3SRodney W. Grimes {
579381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
580f5f7ba03SJordan K. Hubbard 	int	s;
581f5f7ba03SJordan K. Hubbard 
5825b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
583f5f7ba03SJordan K. Hubbard 	s = splbio();
5845b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
5855b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
5865b81b6b3SRodney W. Grimes 	{
587f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
5885b81b6b3SRodney W. Grimes 	}
589f5f7ba03SJordan K. Hubbard 	splx(s);
5905b81b6b3SRodney W. Grimes }
5915b81b6b3SRodney W. Grimes 
5923a2f7427SDavid Greenman static void
593f5f7ba03SJordan K. Hubbard fd_turnon(fdu)
594f5f7ba03SJordan K. Hubbard 	fdu_t fdu;
5955b81b6b3SRodney W. Grimes {
5965b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
5975b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
5985b81b6b3SRodney W. Grimes 	{
5993a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
6003a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
601d0917939SPaul Richards 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
6025b81b6b3SRodney W. Grimes 	}
6035b81b6b3SRodney W. Grimes }
6045b81b6b3SRodney W. Grimes 
605381fe1aaSGarrett Wollman static void
6063a2f7427SDavid Greenman fdc_reset(fdc)
6073a2f7427SDavid Greenman 	fdc_p fdc;
6085b81b6b3SRodney W. Grimes {
6093a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
6103a2f7427SDavid Greenman 
6113a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
6123a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
6133a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
6143a2f7427SDavid Greenman 	DELAY(100);
6153a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
6163a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
6173a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
6183a2f7427SDavid Greenman 	DELAY(100);
6193a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
6203a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
6213a2f7427SDavid Greenman 
6223a2f7427SDavid Greenman 	out_fdc(fdcu, NE7CMD_SPECIFY);
6233a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_1(3, 240));
6243a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_2(2, 0));
6255b81b6b3SRodney W. Grimes }
6265b81b6b3SRodney W. Grimes 
6275b81b6b3SRodney W. Grimes /****************************************************************************/
6285b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
6295b81b6b3SRodney W. Grimes /****************************************************************************/
6305b81b6b3SRodney W. Grimes int
631f5f7ba03SJordan K. Hubbard in_fdc(fdcu)
632f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
6335b81b6b3SRodney W. Grimes {
6345b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
6355b81b6b3SRodney W. Grimes 	int i, j = 100000;
6363a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
6375b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
6385b81b6b3SRodney W. Grimes 		if (i == NE7_RQM) return -1;
6395b81b6b3SRodney W. Grimes 	if (j <= 0)
6405b81b6b3SRodney W. Grimes 		return(-1);
6415b81b6b3SRodney W. Grimes #ifdef	DEBUG
6423a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
6433a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
6445b81b6b3SRodney W. Grimes 	return(i);
6455b81b6b3SRodney W. Grimes #else
6463a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
6475b81b6b3SRodney W. Grimes #endif
6485b81b6b3SRodney W. Grimes }
6495b81b6b3SRodney W. Grimes 
650381fe1aaSGarrett Wollman int
651f5f7ba03SJordan K. Hubbard out_fdc(fdcu, x)
652f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
653f5f7ba03SJordan K. Hubbard 	int x;
6545b81b6b3SRodney W. Grimes {
6555b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
6563b3837dbSRodney W. Grimes 	int i;
6575b81b6b3SRodney W. Grimes 
6583b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
6593b3837dbSRodney W. Grimes 	i = 100000;
6603a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
6613b3837dbSRodney W. Grimes 	if (i <= 0) return (-1);	/* Floppy timed out */
6623b3837dbSRodney W. Grimes 
6633b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
6643b3837dbSRodney W. Grimes 	i = 100000;
6653a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
6663b3837dbSRodney W. Grimes 	if (i <= 0) return (-1);	/* Floppy timed out */
6673b3837dbSRodney W. Grimes 
6683b3837dbSRodney W. Grimes 	/* Send the command and return */
6693a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
6703a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
6715b81b6b3SRodney W. Grimes 	return (0);
6725b81b6b3SRodney W. Grimes }
6735b81b6b3SRodney W. Grimes 
6745b81b6b3SRodney W. Grimes /****************************************************************************/
6755b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
6765b81b6b3SRodney W. Grimes /****************************************************************************/
677381fe1aaSGarrett Wollman int
6785b81b6b3SRodney W. Grimes Fdopen(dev, flags)
6795b81b6b3SRodney W. Grimes 	dev_t	dev;
6805b81b6b3SRodney W. Grimes 	int	flags;
6815b81b6b3SRodney W. Grimes {
6825b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
68320a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
684b99f0a4aSAndrew Moore 	fdc_p	fdc;
6855b81b6b3SRodney W. Grimes 
686b99f0a4aSAndrew Moore #if NFT > 0
687b99f0a4aSAndrew Moore 	/* check for a tape open */
688b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
689b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
690b99f0a4aSAndrew Moore #endif
6915b81b6b3SRodney W. Grimes 	/* check bounds */
692b99f0a4aSAndrew Moore 	if (fdu >= NFD)
693b99f0a4aSAndrew Moore 		return(ENXIO);
694b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
695b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
696b99f0a4aSAndrew Moore 		return(ENXIO);
697b99f0a4aSAndrew Moore 	if (type > NUMDENS)
698b99f0a4aSAndrew Moore 		return(ENXIO);
6997ca0641bSAndrey A. Chernov 	if (type == 0)
7007ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
7017ca0641bSAndrey A. Chernov 	else {
7027ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
703fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
7047ca0641bSAndrey A. Chernov 			case FD_360:
7057ca0641bSAndrey A. Chernov 				return(ENXIO);
706ed2fa05eSAndrey A. Chernov 			case FD_720:
707b39c878eSAndrey A. Chernov 				if (   type != FD_820
708b39c878eSAndrey A. Chernov 				    && type != FD_800
709ed2fa05eSAndrey A. Chernov 				   )
710ed2fa05eSAndrey A. Chernov 					return(ENXIO);
711ed2fa05eSAndrey A. Chernov 				break;
7127ca0641bSAndrey A. Chernov 			case FD_1200:
713b39c878eSAndrey A. Chernov 				switch (type) {
714b39c878eSAndrey A. Chernov 				case FD_1480:
715b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
716fa4700b4SAndrey A. Chernov 					break;
7177ca0641bSAndrey A. Chernov 				case FD_1440:
718b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
719b39c878eSAndrey A. Chernov 					break;
720b39c878eSAndrey A. Chernov 				case FD_820:
721b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
722b39c878eSAndrey A. Chernov 					break;
723b39c878eSAndrey A. Chernov 				case FD_800:
724b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
725b39c878eSAndrey A. Chernov 					break;
726b39c878eSAndrey A. Chernov 				case FD_720:
727b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
728b39c878eSAndrey A. Chernov 					break;
729b39c878eSAndrey A. Chernov 				case FD_360:
730b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
731b39c878eSAndrey A. Chernov 					break;
732b39c878eSAndrey A. Chernov 				default:
733b39c878eSAndrey A. Chernov 					return(ENXIO);
734b39c878eSAndrey A. Chernov 				}
735b39c878eSAndrey A. Chernov 				break;
736b39c878eSAndrey A. Chernov 			case FD_1440:
737b39c878eSAndrey A. Chernov 				if (   type != FD_1720
738b39c878eSAndrey A. Chernov 				    && type != FD_1480
739ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
740b39c878eSAndrey A. Chernov 				    && type != FD_820
741b39c878eSAndrey A. Chernov 				    && type != FD_800
742b39c878eSAndrey A. Chernov 				    && type != FD_720
7437ca0641bSAndrey A. Chernov 				    )
744dffff499SAndrey A. Chernov 					return(ENXIO);
745fa4700b4SAndrey A. Chernov 				break;
7467ca0641bSAndrey A. Chernov 			}
7477ca0641bSAndrey A. Chernov 		}
748fa4700b4SAndrey A. Chernov 	}
749b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
7505b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
7515b81b6b3SRodney W. Grimes 
7525b81b6b3SRodney W. Grimes 	return 0;
7535b81b6b3SRodney W. Grimes }
7545b81b6b3SRodney W. Grimes 
755381fe1aaSGarrett Wollman int
7565b81b6b3SRodney W. Grimes fdclose(dev, flags)
7575b81b6b3SRodney W. Grimes 	dev_t dev;
758381fe1aaSGarrett Wollman 	int flags;
7595b81b6b3SRodney W. Grimes {
7605b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
761b99f0a4aSAndrew Moore 
762b99f0a4aSAndrew Moore #if NFT > 0
7633a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
7643a2f7427SDavid Greenman 
765b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
7663a2f7427SDavid Greenman 		return ftclose(dev, flags);
767b99f0a4aSAndrew Moore #endif
7685b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
7693a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
7705b81b6b3SRodney W. Grimes 	return(0);
7715b81b6b3SRodney W. Grimes }
7725b81b6b3SRodney W. Grimes 
7735b81b6b3SRodney W. Grimes 
7743a2f7427SDavid Greenman /****************************************************************************/
7753a2f7427SDavid Greenman /*                               fdstrategy                                 */
7763a2f7427SDavid Greenman /****************************************************************************/
7773a2f7427SDavid Greenman void
7783a2f7427SDavid Greenman fdstrategy(struct buf *bp)
7793a2f7427SDavid Greenman {
7803a2f7427SDavid Greenman 	register struct buf *dp;
7813a2f7427SDavid Greenman 	long nblocks, blknum;
7823a2f7427SDavid Greenman  	int	s;
7833a2f7427SDavid Greenman  	fdcu_t	fdcu;
7843a2f7427SDavid Greenman  	fdu_t	fdu;
7853a2f7427SDavid Greenman  	fdc_p	fdc;
7863a2f7427SDavid Greenman  	fd_p	fd;
7873a2f7427SDavid Greenman 	size_t	fdblk;
7883a2f7427SDavid Greenman 
7893a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
7903a2f7427SDavid Greenman 	fd = &fd_data[fdu];
7913a2f7427SDavid Greenman 	fdc = fd->fdc;
7923a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
7933a2f7427SDavid Greenman 	fdblk = 128 << (fd->ft->secsize);
7943a2f7427SDavid Greenman 
7953a2f7427SDavid Greenman #if NFT > 0
7963a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
7973a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
7983a2f7427SDavid Greenman 		bp->b_error = ENXIO;
7993a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
8003a2f7427SDavid Greenman 		goto bad;
8013a2f7427SDavid Greenman 	}
8023a2f7427SDavid Greenman 	/* check for controller already busy with tape */
8033a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
8043a2f7427SDavid Greenman 		bp->b_error = EBUSY;
8053a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
8063a2f7427SDavid Greenman 		goto bad;
8073a2f7427SDavid Greenman 	}
8083a2f7427SDavid Greenman #endif
8093a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
8103a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
811702c623aSPoul-Henning Kamp 			printf("fdstrat: fdu = %d, blkno = %lu, bcount = %ld\n",
812702c623aSPoul-Henning Kamp 			       fdu, (u_long)bp->b_blkno, bp->b_bcount);
8133a2f7427SDavid Greenman 			bp->b_error = EINVAL;
8143a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
8153a2f7427SDavid Greenman 			goto bad;
8163a2f7427SDavid Greenman 		}
8173a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
8183a2f7427SDavid Greenman 			bp->b_error = EINVAL;
8193a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
8203a2f7427SDavid Greenman 			goto bad;
8213a2f7427SDavid Greenman 		}
8223a2f7427SDavid Greenman 	}
8233a2f7427SDavid Greenman 
8243a2f7427SDavid Greenman 	/*
8253a2f7427SDavid Greenman 	 * Set up block calculations.
8263a2f7427SDavid Greenman 	 */
8273a2f7427SDavid Greenman 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk;
8283a2f7427SDavid Greenman  	nblocks = fd->ft->size;
8293a2f7427SDavid Greenman 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
8303a2f7427SDavid Greenman 		if (blknum == nblocks) {
8313a2f7427SDavid Greenman 			bp->b_resid = bp->b_bcount;
8323a2f7427SDavid Greenman 		} else {
8333a2f7427SDavid Greenman 			bp->b_error = ENOSPC;
8343a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
8353a2f7427SDavid Greenman 		}
8363a2f7427SDavid Greenman 		goto bad;
8373a2f7427SDavid Greenman 	}
8383a2f7427SDavid Greenman  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
8393a2f7427SDavid Greenman 	dp = &(fdc->head);
8403a2f7427SDavid Greenman 	s = splbio();
8413a2f7427SDavid Greenman 	disksort(dp, bp);
8423a2f7427SDavid Greenman 	untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */
8433a2f7427SDavid Greenman 	fdstart(fdcu);
8443a2f7427SDavid Greenman 	splx(s);
8453a2f7427SDavid Greenman 	return;
8463a2f7427SDavid Greenman 
8473a2f7427SDavid Greenman bad:
8483a2f7427SDavid Greenman 	biodone(bp);
8493a2f7427SDavid Greenman }
8503a2f7427SDavid Greenman 
8515b81b6b3SRodney W. Grimes /***************************************************************\
8525b81b6b3SRodney W. Grimes *				fdstart				*
8535b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
8545b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
8555b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
8565b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
8575b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
8585b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
8595b81b6b3SRodney W. Grimes \***************************************************************/
860381fe1aaSGarrett Wollman static void
861f5f7ba03SJordan K. Hubbard fdstart(fdcu)
862f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
8635b81b6b3SRodney W. Grimes {
8645b81b6b3SRodney W. Grimes 	int s;
8655b81b6b3SRodney W. Grimes 
8665b81b6b3SRodney W. Grimes 	s = splbio();
8675b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
8685b81b6b3SRodney W. Grimes 	{
8695b81b6b3SRodney W. Grimes 		fdintr(fdcu);
8705b81b6b3SRodney W. Grimes 	}
8715b81b6b3SRodney W. Grimes 	splx(s);
8725b81b6b3SRodney W. Grimes }
8735b81b6b3SRodney W. Grimes 
8743a2f7427SDavid Greenman /* ARGSUSED */
875381fe1aaSGarrett Wollman static void
876d0917939SPaul Richards fd_timeout(void *arg1)
8775b81b6b3SRodney W. Grimes {
878381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
8795b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
8803a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
8815b81b6b3SRodney W. Grimes 	struct buf *dp, *bp;
882f5f7ba03SJordan K. Hubbard 	int s;
8835b81b6b3SRodney W. Grimes 
8845b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
8855b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
8865b81b6b3SRodney W. Grimes 
8873a2f7427SDavid Greenman 	/*
8883a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
8893a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
8903a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
8913a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
8923a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
8933a2f7427SDavid Greenman 	 */
8945b81b6b3SRodney W. Grimes 
8953a2f7427SDavid Greenman 	s = splbio();
8963a2f7427SDavid Greenman 
8973a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
8983a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
8993a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
9003a2f7427SDavid Greenman 	{
9013a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
9023a2f7427SDavid Greenman 		/* yup, it is; kill it now */
9033a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
9043a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
9053a2f7427SDavid Greenman 	}
9065b81b6b3SRodney W. Grimes 
9075b81b6b3SRodney W. Grimes 	if (bp)
9085b81b6b3SRodney W. Grimes 	{
9095b81b6b3SRodney W. Grimes 		retrier(fdcu);
9103a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
9115b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
9125b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
9135b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
9145b81b6b3SRodney W. Grimes 	}
9155b81b6b3SRodney W. Grimes 	else
9165b81b6b3SRodney W. Grimes 	{
9175b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
9185b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
9195b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
9205b81b6b3SRodney W. Grimes 	}
921f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
922f5f7ba03SJordan K. Hubbard 	splx(s);
9235b81b6b3SRodney W. Grimes }
9245b81b6b3SRodney W. Grimes 
9255b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
9263a2f7427SDavid Greenman /* ARGSUSED */
927381fe1aaSGarrett Wollman static void
928d0917939SPaul Richards fd_pseudointr(void *arg1)
9295b81b6b3SRodney W. Grimes {
930381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
9315b81b6b3SRodney W. Grimes 	int	s;
9323a2f7427SDavid Greenman 
9335b81b6b3SRodney W. Grimes 	s = splbio();
9345b81b6b3SRodney W. Grimes 	fdintr(fdcu);
9355b81b6b3SRodney W. Grimes 	splx(s);
9365b81b6b3SRodney W. Grimes }
9375b81b6b3SRodney W. Grimes 
9385b81b6b3SRodney W. Grimes /***********************************************************************\
9395b81b6b3SRodney W. Grimes *                                 fdintr				*
9405b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
9415b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
9425b81b6b3SRodney W. Grimes \***********************************************************************/
943381fe1aaSGarrett Wollman void
944381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
9455b81b6b3SRodney W. Grimes {
9465b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
947b99f0a4aSAndrew Moore #if NFT > 0
948b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
949b99f0a4aSAndrew Moore 
950b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
951b99f0a4aSAndrew Moore 		(ftintr(fdu));
952b99f0a4aSAndrew Moore 	else
953b99f0a4aSAndrew Moore #endif
954381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
955381fe1aaSGarrett Wollman 			;
9565b81b6b3SRodney W. Grimes }
9575b81b6b3SRodney W. Grimes 
9585b81b6b3SRodney W. Grimes /***********************************************************************\
9595b81b6b3SRodney W. Grimes * The controller state machine.						*
9605b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
9615b81b6b3SRodney W. Grimes \***********************************************************************/
9623a2f7427SDavid Greenman static int
963381fe1aaSGarrett Wollman fdstate(fdcu, fdc)
964f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
965f5f7ba03SJordan K. Hubbard 	fdc_p fdc;
9665b81b6b3SRodney W. Grimes {
9673a2f7427SDavid Greenman 	int read, format, head, sec = 0, i = 0, sectrac, st0, cyl, st3;
9685b81b6b3SRodney W. Grimes 	unsigned long blknum;
9695b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
9705b81b6b3SRodney W. Grimes 	fd_p fd;
9715b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
972b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
9733a2f7427SDavid Greenman 	size_t fdblk;
9745b81b6b3SRodney W. Grimes 
9755b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
9765b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
9775b81b6b3SRodney W. Grimes 	if(!bp)
9785b81b6b3SRodney W. Grimes 	{
9795b81b6b3SRodney W. Grimes 		/***********************************************\
9805b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
9815b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
9825b81b6b3SRodney W. Grimes 		\***********************************************/
9835b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
9845b81b6b3SRodney W. Grimes 		if(fdc->fd)
9855b81b6b3SRodney W. Grimes 		{
9863a2f7427SDavid Greenman 			printf("unexpected valid fd pointer (fdu = %d)\n",
9873a2f7427SDavid Greenman 			       fdc->fdu);
9885b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
9895b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
9905b81b6b3SRodney W. Grimes 		}
9915b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
9925b81b6b3SRodney W. Grimes  		return(0);
9935b81b6b3SRodney W. Grimes 	}
9945b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
9955b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
9963a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
9975b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
9985b81b6b3SRodney W. Grimes 	{
9995b81b6b3SRodney W. Grimes 		printf("confused fd pointers\n");
10005b81b6b3SRodney W. Grimes 	}
10015b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
1002b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
1003b39c878eSAndrey A. Chernov 	if(format)
1004b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
10055b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
10065b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
10075b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
1008d0917939SPaul Richards 	untimeout(fd_turnoff, (caddr_t)fdu);
1009d0917939SPaul Richards 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
10105b81b6b3SRodney W. Grimes 	switch (fdc->state)
10115b81b6b3SRodney W. Grimes 	{
10125b81b6b3SRodney W. Grimes 	case DEVIDLE:
10135b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
10145b81b6b3SRodney W. Grimes 		fdc->retry = 0;
10155b81b6b3SRodney W. Grimes 		fd->skip = 0;
10165b81b6b3SRodney W. Grimes 		fdc->fd = fd;
10175b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
10183a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
10193a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
10205b81b6b3SRodney W. Grimes 		/*******************************************************\
10215b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
10225b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
10235b81b6b3SRodney W. Grimes 		\*******************************************************/
10245b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
10255b81b6b3SRodney W. Grimes 		{
10265b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
10275b81b6b3SRodney W. Grimes 			return(0); /* come back later */
10285b81b6b3SRodney W. Grimes 		}
10295b81b6b3SRodney W. Grimes 		/*******************************************************\
10305b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
10315b81b6b3SRodney W. Grimes 		\*******************************************************/
10325b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
10335b81b6b3SRodney W. Grimes 		{
10345b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
10355b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
10365b81b6b3SRodney W. Grimes 			return(0);
10375b81b6b3SRodney W. Grimes 		}
10385b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
10395b81b6b3SRodney W. Grimes 		{
10403a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
10415b81b6b3SRodney W. Grimes 		}
10425b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
10435b81b6b3SRodney W. Grimes 		break;
10445b81b6b3SRodney W. Grimes 	case DOSEEK:
10455b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
10465b81b6b3SRodney W. Grimes 		{
10475b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
10485b81b6b3SRodney W. Grimes 			break;
10495b81b6b3SRodney W. Grimes 		}
10505b81b6b3SRodney W. Grimes 		out_fdc(fdcu, NE7CMD_SEEK);	/* Seek function */
10515b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fd->fdsu);	/* Drive number */
10525b81b6b3SRodney W. Grimes 		out_fdc(fdcu, bp->b_cylin * fd->ft->steptrac);
10535b81b6b3SRodney W. Grimes 		fd->track = -2;
10545b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
10555b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
10565b81b6b3SRodney W. Grimes 	case SEEKWAIT:
10575b81b6b3SRodney W. Grimes 		/* allow heads to settle */
10583a2f7427SDavid Greenman 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 32);
10595b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
10605b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
10615b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
10625b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
10635b81b6b3SRodney W. Grimes 		if(fd->track == -2)
10645b81b6b3SRodney W. Grimes 		{
10655b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
10663a2f7427SDavid Greenman 			do {
10675b81b6b3SRodney W. Grimes 				out_fdc(fdcu, NE7CMD_SENSEI);
10683a2f7427SDavid Greenman 				st0 = in_fdc(fdcu);
10695b81b6b3SRodney W. Grimes 				cyl = in_fdc(fdcu);
10703a2f7427SDavid Greenman 				/*
10713a2f7427SDavid Greenman 				 * if this was a "ready changed" interrupt,
10723a2f7427SDavid Greenman 				 * fetch status again (can happen after
10733a2f7427SDavid Greenman 				 * enabling controller from reset state)
10743a2f7427SDavid Greenman 				 */
10753a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
10763a2f7427SDavid Greenman 			if (0 == descyl)
10773a2f7427SDavid Greenman 			{
10783a2f7427SDavid Greenman 				/*
10793a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
10803a2f7427SDavid Greenman 				 * really there
10813a2f7427SDavid Greenman 				 */
10823a2f7427SDavid Greenman 				out_fdc(fdcu, NE7CMD_SENSED);
10833a2f7427SDavid Greenman 				out_fdc(fdcu, fdu);
10843a2f7427SDavid Greenman 				st3 = in_fdc(fdcu);
10853a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
10863a2f7427SDavid Greenman 					printf(
10873a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
10883a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
10893a2f7427SDavid Greenman 					if(fdc->retry < 3)
10903a2f7427SDavid Greenman 						fdc->retry = 3;
10913a2f7427SDavid Greenman 					return(retrier(fdcu));
10923a2f7427SDavid Greenman 				}
10933a2f7427SDavid Greenman 			}
10945b81b6b3SRodney W. Grimes 			if (cyl != descyl)
10955b81b6b3SRodney W. Grimes 			{
10963a2f7427SDavid Greenman 				printf(
10973a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
10983a2f7427SDavid Greenman 				       fdu, descyl, cyl, st0, NE7_ST0BITS);
10995b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
11005b81b6b3SRodney W. Grimes 			}
11015b81b6b3SRodney W. Grimes 		}
11025b81b6b3SRodney W. Grimes 
11035b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
1104b39c878eSAndrey A. Chernov 		if(format)
1105b39c878eSAndrey A. Chernov 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1106b39c878eSAndrey A. Chernov 				- (char *)finfo;
11075b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
11083a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
11093a2f7427SDavid Greenman 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
11103a2f7427SDavid Greenman 			+ fd->skip/fdblk;
11115b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
11125b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
11135b81b6b3SRodney W. Grimes 		head = sec / sectrac;
11145b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
11153a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
11163a2f7427SDavid Greenman 
11173a2f7427SDavid Greenman 		if(format || !read)
11183a2f7427SDavid Greenman 		{
11193a2f7427SDavid Greenman 			/* make sure the drive is writable */
11203a2f7427SDavid Greenman 			out_fdc(fdcu, NE7CMD_SENSED);
11213a2f7427SDavid Greenman 			out_fdc(fdcu, fdu);
11223a2f7427SDavid Greenman 			st3 = in_fdc(fdcu);
11233a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
11243a2f7427SDavid Greenman 			{
11253a2f7427SDavid Greenman 				/*
11263a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
11273a2f7427SDavid Greenman 				 * in order to force the current operation
11283a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
11293a2f7427SDavid Greenman 				 * error - all error handling is done
11303a2f7427SDavid Greenman 				 * by the retrier()
11313a2f7427SDavid Greenman 				 */
11323a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
11333a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
11343a2f7427SDavid Greenman 				fdc->status[2] = 0;
11353a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
11363a2f7427SDavid Greenman 				fdc->status[4] = head;
11373a2f7427SDavid Greenman 				fdc->status[5] = sec;
11383a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
11393a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
11403a2f7427SDavid Greenman 				return (1);
11413a2f7427SDavid Greenman 			}
11423a2f7427SDavid Greenman 		}
11435b81b6b3SRodney W. Grimes 
1144b39c878eSAndrey A. Chernov 		if(format)
1145b39c878eSAndrey A. Chernov 		{
1146b39c878eSAndrey A. Chernov 			/* formatting */
11473a2f7427SDavid Greenman 			out_fdc(fdcu, NE7CMD_FORMAT);
1148b39c878eSAndrey A. Chernov 			out_fdc(fdcu, head << 2 | fdu);
1149b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_secshift);
1150b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_nsecs);
1151b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_gaplen);
1152b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_fillbyte);
1153b39c878eSAndrey A. Chernov 		}
1154b39c878eSAndrey A. Chernov 		else
1155b39c878eSAndrey A. Chernov 		{
11565b81b6b3SRodney W. Grimes 			if (read)
11575b81b6b3SRodney W. Grimes 			{
11585b81b6b3SRodney W. Grimes 				out_fdc(fdcu, NE7CMD_READ);      /* READ */
11595b81b6b3SRodney W. Grimes 			}
11605b81b6b3SRodney W. Grimes 			else
11615b81b6b3SRodney W. Grimes 			{
11625b81b6b3SRodney W. Grimes 				out_fdc(fdcu, NE7CMD_WRITE);     /* WRITE */
11635b81b6b3SRodney W. Grimes 			}
11645b81b6b3SRodney W. Grimes 			out_fdc(fdcu, head << 2 | fdu);  /* head & unit */
11655b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->track);        /* track */
11665b81b6b3SRodney W. Grimes 			out_fdc(fdcu, head);
11675b81b6b3SRodney W. Grimes 			out_fdc(fdcu, sec);              /* sector XXX +1? */
11685b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->secsize);  /* sector size */
11695b81b6b3SRodney W. Grimes 			out_fdc(fdcu, sectrac);          /* sectors/track */
11705b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->gap);      /* gap size */
11715b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->datalen);  /* data length */
1172b39c878eSAndrey A. Chernov 		}
11735b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
11743a2f7427SDavid Greenman 		timeout(fd_timeout, (caddr_t)fdcu, hz);
11755b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
11765b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
1177d0917939SPaul Richards 		untimeout(fd_timeout, (caddr_t)fdcu);
11785b81b6b3SRodney W. Grimes 		for(i=0;i<7;i++)
11795b81b6b3SRodney W. Grimes 		{
11805b81b6b3SRodney W. Grimes 			fdc->status[i] = in_fdc(fdcu);
11815b81b6b3SRodney W. Grimes 		}
11823a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
11833a2f7427SDavid Greenman 		/* FALLTHROUGH */
11843a2f7427SDavid Greenman 	case IOTIMEDOUT:
11855b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
11863a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
11873a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
11885b81b6b3SRodney W. Grimes 		{
11893a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
11903a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1191b39c878eSAndrey A. Chernov                                 /*
11923a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
11933a2f7427SDavid Greenman 				 * and didn't release it in time for the
11943a2f7427SDavid Greenman 				 * next FDC transfer.
11953a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
11963a2f7427SDavid Greenman 				 * count. (vak)
1197b39c878eSAndrey A. Chernov                                  */
1198b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1199b39c878eSAndrey A. Chernov                                 return (1);
1200b39c878eSAndrey A. Chernov                         }
12013a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
12023a2f7427SDavid Greenman 				&& fdc->retry < 6)
12033a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
12043a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
12053a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
12063a2f7427SDavid Greenman 				&& fdc->retry < 3)
12073a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
12085b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
12095b81b6b3SRodney W. Grimes 		}
12105b81b6b3SRodney W. Grimes 		/* All OK */
12113a2f7427SDavid Greenman 		fd->skip += fdblk;
1212b39c878eSAndrey A. Chernov 		if (!format && fd->skip < bp->b_bcount)
12135b81b6b3SRodney W. Grimes 		{
12145b81b6b3SRodney W. Grimes 			/* set up next transfer */
12153a2f7427SDavid Greenman 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
12163a2f7427SDavid Greenman 				+ fd->skip/fdblk;
12173a2f7427SDavid Greenman 			bp->b_cylin =
12183a2f7427SDavid Greenman 				(blknum / (fd->ft->sectrac * fd->ft->heads));
12195b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
12205b81b6b3SRodney W. Grimes 		}
12215b81b6b3SRodney W. Grimes 		else
12225b81b6b3SRodney W. Grimes 		{
12235b81b6b3SRodney W. Grimes 			/* ALL DONE */
12245b81b6b3SRodney W. Grimes 			fd->skip = 0;
12255b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
122626f9a767SRodney W. Grimes 			dp->b_actf = bp->b_actf;
12275b81b6b3SRodney W. Grimes 			biodone(bp);
12285b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
12295b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
12305b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
12315b81b6b3SRodney W. Grimes 		}
12325b81b6b3SRodney W. Grimes 		return(1);
12335b81b6b3SRodney W. Grimes 	case RESETCTLR:
12343a2f7427SDavid Greenman 		fdc_reset(fdc);
12355b81b6b3SRodney W. Grimes 		fdc->retry++;
12365b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
12375b81b6b3SRodney W. Grimes 		break;
12385b81b6b3SRodney W. Grimes 	case STARTRECAL:
12395b81b6b3SRodney W. Grimes 		out_fdc(fdcu, NE7CMD_RECAL);	/* Recalibrate Function */
12405b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fdu);
12415b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
12425b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
12435b81b6b3SRodney W. Grimes 	case RECALWAIT:
12445b81b6b3SRodney W. Grimes 		/* allow heads to settle */
12453a2f7427SDavid Greenman 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 32);
12465b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
12475b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
12485b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
12493a2f7427SDavid Greenman 		do {
12505b81b6b3SRodney W. Grimes 			out_fdc(fdcu, NE7CMD_SENSEI);
12515b81b6b3SRodney W. Grimes 			st0 = in_fdc(fdcu);
12525b81b6b3SRodney W. Grimes 			cyl = in_fdc(fdcu);
12533a2f7427SDavid Greenman 			/*
12543a2f7427SDavid Greenman 			 * if this was a "ready changed" interrupt,
12553a2f7427SDavid Greenman 			 * fetch status again (can happen after
12563a2f7427SDavid Greenman 			 * enabling controller from reset state)
12573a2f7427SDavid Greenman 			 */
12583a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
12593a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
12605b81b6b3SRodney W. Grimes 		{
12615b81b6b3SRodney W. Grimes 			printf("fd%d: recal failed ST0 %b cyl %d\n", fdu,
12625b81b6b3SRodney W. Grimes 				st0, NE7_ST0BITS, cyl);
12633a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
12645b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
12655b81b6b3SRodney W. Grimes 		}
12665b81b6b3SRodney W. Grimes 		fd->track = 0;
12675b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
12685b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
12695b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
12705b81b6b3SRodney W. Grimes 	case MOTORWAIT:
12715b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
12725b81b6b3SRodney W. Grimes 		{
12735b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
12745b81b6b3SRodney W. Grimes 		}
12753a2f7427SDavid Greenman 		/*
12763a2f7427SDavid Greenman 		 * since the controller was off, it has lost its
12773a2f7427SDavid Greenman 		 * idea about the current track it were; thus,
12783a2f7427SDavid Greenman 		 * recalibrate the bastard
12793a2f7427SDavid Greenman 		 */
12803a2f7427SDavid Greenman 		fdc->state = STARTRECAL;
12815b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
12825b81b6b3SRodney W. Grimes 	default:
12835b81b6b3SRodney W. Grimes 		printf("Unexpected FD int->");
12845b81b6b3SRodney W. Grimes 		out_fdc(fdcu, NE7CMD_SENSEI);
12855b81b6b3SRodney W. Grimes 		st0 = in_fdc(fdcu);
12865b81b6b3SRodney W. Grimes 		cyl = in_fdc(fdcu);
12873a2f7427SDavid Greenman 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
12883a2f7427SDavid Greenman 		out_fdc(fdcu, NE7CMD_READID);
12895b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fd->fdsu);
12905b81b6b3SRodney W. Grimes 		for(i=0;i<7;i++) {
12915b81b6b3SRodney W. Grimes 			fdc->status[i] = in_fdc(fdcu);
12925b81b6b3SRodney W. Grimes 		}
12933a2f7427SDavid Greenman 		if(fdc->status[0] != -1)
12943a2f7427SDavid Greenman 			printf("intr status :%lx %lx %lx %lx %lx %lx %lx\n",
12955b81b6b3SRodney W. Grimes 			       fdc->status[0],
12965b81b6b3SRodney W. Grimes 			       fdc->status[1],
12975b81b6b3SRodney W. Grimes 			       fdc->status[2],
12985b81b6b3SRodney W. Grimes 			       fdc->status[3],
12995b81b6b3SRodney W. Grimes 			       fdc->status[4],
13005b81b6b3SRodney W. Grimes 			       fdc->status[5],
13015b81b6b3SRodney W. Grimes 			       fdc->status[6] );
13023a2f7427SDavid Greenman 		else
13033a2f7427SDavid Greenman 			printf("FDC timed out\n");
13045b81b6b3SRodney W. Grimes 		return(0);
13055b81b6b3SRodney W. Grimes 	}
13065b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
13075b81b6b3SRodney W. Grimes }
13085b81b6b3SRodney W. Grimes 
1309aaf08d94SGarrett Wollman static int
1310f5f7ba03SJordan K. Hubbard retrier(fdcu)
1311f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
13125b81b6b3SRodney W. Grimes {
13135b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
13145b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
13155b81b6b3SRodney W. Grimes 
13165b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
13175b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
13185b81b6b3SRodney W. Grimes 
13193a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
13203a2f7427SDavid Greenman 		goto fail;
13215b81b6b3SRodney W. Grimes 	switch(fdc->retry)
13225b81b6b3SRodney W. Grimes 	{
13235b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
13245b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
13255b81b6b3SRodney W. Grimes 		break;
13265b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
13275b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
13285b81b6b3SRodney W. Grimes 		break;
13295b81b6b3SRodney W. Grimes 	case 6:
13305b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
13315b81b6b3SRodney W. Grimes 		break;
13325b81b6b3SRodney W. Grimes 	case 7:
13335b81b6b3SRodney W. Grimes 		break;
13345b81b6b3SRodney W. Grimes 	default:
13353a2f7427SDavid Greenman 	fail:
13365b81b6b3SRodney W. Grimes 		{
13377ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
13387ca0641bSAndrey A. Chernov 			/* Trick diskerr */
13393a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
13403a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
134192ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
13423a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
13433a2f7427SDavid Greenman 				(struct disklabel *)NULL);
13447ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
134592ed385aSRodney W. Grimes 			printf(" (ST0 %b ", fdc->status[0], NE7_ST0BITS);
13465b81b6b3SRodney W. Grimes 			printf(" ST1 %b ", fdc->status[1], NE7_ST1BITS);
13475b81b6b3SRodney W. Grimes 			printf(" ST2 %b ", fdc->status[2], NE7_ST2BITS);
1348702c623aSPoul-Henning Kamp 			printf("cyl %ld hd %ld sec %ld)\n",
134992ed385aSRodney W. Grimes 			       fdc->status[3], fdc->status[4], fdc->status[5]);
13505b81b6b3SRodney W. Grimes 		}
13515b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
13525b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
13535b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
135426f9a767SRodney W. Grimes 		dp->b_actf = bp->b_actf;
13555b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
13565b81b6b3SRodney W. Grimes 		biodone(bp);
135792ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
13585b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
13595b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1360f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
136192ed385aSRodney W. Grimes 		return(1);
13625b81b6b3SRodney W. Grimes 	}
13635b81b6b3SRodney W. Grimes 	fdc->retry++;
13645b81b6b3SRodney W. Grimes 	return(1);
13655b81b6b3SRodney W. Grimes }
13665b81b6b3SRodney W. Grimes 
1367b39c878eSAndrey A. Chernov static int
1368b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1369b39c878eSAndrey A. Chernov 	dev_t dev;
1370b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1371b39c878eSAndrey A. Chernov 	struct proc *p;
1372b39c878eSAndrey A. Chernov {
1373b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1374b39c878eSAndrey A. Chernov  	fd_p	fd;
1375b39c878eSAndrey A. Chernov 
1376b39c878eSAndrey A. Chernov 	struct buf *bp;
1377b39c878eSAndrey A. Chernov 	int rv = 0, s;
13783a2f7427SDavid Greenman 	size_t fdblk;
1379b39c878eSAndrey A. Chernov 
1380b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1381b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
13823a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1383b39c878eSAndrey A. Chernov 
1384b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1385b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1386b39c878eSAndrey A. Chernov 	if(bp == 0)
1387b39c878eSAndrey A. Chernov 		return ENOBUFS;
1388b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1389b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1390b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1391b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1392b39c878eSAndrey A. Chernov 
1393b39c878eSAndrey A. Chernov 	/*
1394b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1395b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1396b39c878eSAndrey A. Chernov 	 */
1397b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
13983a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1399b39c878eSAndrey A. Chernov 
1400b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1401b39c878eSAndrey A. Chernov 	bp->b_un.b_addr = (caddr_t)finfo;
1402b39c878eSAndrey A. Chernov 
1403b39c878eSAndrey A. Chernov 	/* now do the format */
1404b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1405b39c878eSAndrey A. Chernov 
1406b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1407b39c878eSAndrey A. Chernov 	s = splbio();
1408b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1409b39c878eSAndrey A. Chernov 	{
1410b39c878eSAndrey A. Chernov 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1411b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1412b39c878eSAndrey A. Chernov 			break;
1413b39c878eSAndrey A. Chernov 	}
1414b39c878eSAndrey A. Chernov 	splx(s);
1415b39c878eSAndrey A. Chernov 
1416b39c878eSAndrey A. Chernov 	if(rv == EWOULDBLOCK)
1417b39c878eSAndrey A. Chernov 		/* timed out */
1418b39c878eSAndrey A. Chernov 		rv = EIO;
14193a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
14203a2f7427SDavid Greenman 		rv = bp->b_error;
14213a2f7427SDavid Greenman 	biodone(bp);
1422b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1423b39c878eSAndrey A. Chernov 	return rv;
1424b39c878eSAndrey A. Chernov }
1425b39c878eSAndrey A. Chernov 
1426f5f7ba03SJordan K. Hubbard /*
1427f5f7ba03SJordan K. Hubbard  *
14283a2f7427SDavid Greenman  * TODO: Think about allocating buffer off stack.
1429f5f7ba03SJordan K. Hubbard  *       Don't pass uncast 0's and NULL's to read/write/setdisklabel().
1430f5f7ba03SJordan K. Hubbard  *       Watch out for NetBSD's different *disklabel() interface.
1431b39c878eSAndrey A. Chernov  *
1432f5f7ba03SJordan K. Hubbard  */
14335b81b6b3SRodney W. Grimes 
1434f5f7ba03SJordan K. Hubbard int
1435b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1436f5f7ba03SJordan K. Hubbard 	dev_t dev;
1437f5f7ba03SJordan K. Hubbard 	int cmd;
1438f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1439f5f7ba03SJordan K. Hubbard 	int flag;
1440b39c878eSAndrey A. Chernov 	struct proc *p;
1441f5f7ba03SJordan K. Hubbard {
14423a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
14433a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
14443a2f7427SDavid Greenman 	size_t fdblk;
14453a2f7427SDavid Greenman 
1446f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1447f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1448f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
14493a2f7427SDavid Greenman 	int error = 0;
1450f5f7ba03SJordan K. Hubbard 
1451b99f0a4aSAndrew Moore #if NFT > 0
1452a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1453a60eff27SNate Williams 
1454a60eff27SNate Williams 	/* check for a tape ioctl */
1455a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1456b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1457b99f0a4aSAndrew Moore #endif
1458b99f0a4aSAndrew Moore 
14593a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1460f5f7ba03SJordan K. Hubbard 
1461f5f7ba03SJordan K. Hubbard 	switch (cmd)
1462f5f7ba03SJordan K. Hubbard 	{
1463f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1464f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1465f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
14663a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
146792ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1468f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1469f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1470f5f7ba03SJordan K. Hubbard 
1471f5f7ba03SJordan K. Hubbard 		if (readdisklabel(dev, fdstrategy, dl, NULL, 0, 0) == NULL)
1472f5f7ba03SJordan K. Hubbard 			error = 0;
1473f5f7ba03SJordan K. Hubbard 		else
1474f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1475f5f7ba03SJordan K. Hubbard 
1476f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1477f5f7ba03SJordan K. Hubbard 		break;
1478f5f7ba03SJordan K. Hubbard 
1479f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1480f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1481f5f7ba03SJordan K. Hubbard 			error = EBADF;
1482f5f7ba03SJordan K. Hubbard 		break;
1483f5f7ba03SJordan K. Hubbard 
1484f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1485f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1486f5f7ba03SJordan K. Hubbard 			error = EBADF;
1487f5f7ba03SJordan K. Hubbard 		break;
1488f5f7ba03SJordan K. Hubbard 
1489f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1490f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1491f5f7ba03SJordan K. Hubbard 		{
1492f5f7ba03SJordan K. Hubbard 			error = EBADF;
1493f5f7ba03SJordan K. Hubbard 			break;
1494f5f7ba03SJordan K. Hubbard 		}
1495f5f7ba03SJordan K. Hubbard 
1496f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1497f5f7ba03SJordan K. Hubbard 
14983a2f7427SDavid Greenman 		if ((error =
14993a2f7427SDavid Greenman 		     setdisklabel ((struct disklabel *)buffer, dl, 0, NULL)))
1500f5f7ba03SJordan K. Hubbard 			break;
1501f5f7ba03SJordan K. Hubbard 
1502b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
1503b39c878eSAndrey A. Chernov 				       (struct disklabel *)buffer, NULL);
1504b39c878eSAndrey A. Chernov 		break;
1505b39c878eSAndrey A. Chernov 
1506b39c878eSAndrey A. Chernov 	case FD_FORM:
1507b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1508b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1509b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1510b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1511b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1512b39c878eSAndrey A. Chernov 		else
1513b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1514b39c878eSAndrey A. Chernov 		break;
1515b39c878eSAndrey A. Chernov 
1516b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1517b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1518f5f7ba03SJordan K. Hubbard 		break;
1519f5f7ba03SJordan K. Hubbard 
15203a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
15213a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
15223a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
15233a2f7427SDavid Greenman 			return EPERM;
15243a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
15253a2f7427SDavid Greenman 		break;
15263a2f7427SDavid Greenman 
15273a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
15283a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
15293a2f7427SDavid Greenman 		break;
15303a2f7427SDavid Greenman 
15313a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
15323a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
15333a2f7427SDavid Greenman 		break;
15343a2f7427SDavid Greenman 
1535f5f7ba03SJordan K. Hubbard 	default:
15363a2f7427SDavid Greenman 		error = ENOTTY;
1537f5f7ba03SJordan K. Hubbard 		break;
1538f5f7ba03SJordan K. Hubbard 	}
1539f5f7ba03SJordan K. Hubbard 	return (error);
1540f5f7ba03SJordan K. Hubbard }
1541f5f7ba03SJordan K. Hubbard 
1542f5f7ba03SJordan K. Hubbard #endif
15433a2f7427SDavid Greenman /*
15443a2f7427SDavid Greenman  * Hello emacs, these are the
15453a2f7427SDavid Greenman  * Local Variables:
15463a2f7427SDavid Greenman  *  c-indent-level:               8
15473a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
15483a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
15493a2f7427SDavid Greenman  *  c-brace-offset:              -8
15503a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
15513a2f7427SDavid Greenman  *  c-argdecl-indent:             8
15523a2f7427SDavid Greenman  *  c-label-offset:              -8
15533a2f7427SDavid Greenman  *  c++-hanging-braces:           1
15543a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
15553a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
15563a2f7427SDavid Greenman  *  c++-friend-offset:            0
15573a2f7427SDavid Greenman  * End:
15583a2f7427SDavid Greenman  */
1559