xref: /freebsd/sys/dev/fdc/fdc.c (revision 6b7bd95b6d724c6bea5af622fd907511fec8e83c)
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
436b7bd95bSJoerg Wunsch  *	$Id: fd.c,v 1.30 1994/09/17 16:56:06 davidg 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>
69f540b106SGarrett Wollman #include <i386/isa/isa.h>
70f540b106SGarrett Wollman #include <i386/isa/isa_device.h>
71f540b106SGarrett Wollman #include <i386/isa/fdreg.h>
72f540b106SGarrett Wollman #include <i386/isa/fdc.h>
73f540b106SGarrett Wollman #include <i386/isa/rtc.h>
745b81b6b3SRodney W. Grimes 
75b2be795bSAndrey A. Chernov #define RAW_PART 2
765b81b6b3SRodney W. Grimes #define b_cylin b_resid
775b81b6b3SRodney W. Grimes 
78b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
79b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
805b81b6b3SRodney W. Grimes 
813a2f7427SDavid Greenman /*
823a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
833a2f7427SDavid Greenman  * on the controller
843a2f7427SDavid Greenman  */
853a2f7427SDavid Greenman #define id_physid id_scsiid
863a2f7427SDavid Greenman 
87b39c878eSAndrey A. Chernov #define NUMTYPES 14
88b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
897ca0641bSAndrey A. Chernov 
903a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
91b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
92b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
93b99f0a4aSAndrew Moore #define FD_1720         1
94b99f0a4aSAndrew Moore #define FD_1480         2
95b99f0a4aSAndrew Moore #define FD_1440         3
96b99f0a4aSAndrew Moore #define FD_1200         4
97b99f0a4aSAndrew Moore #define FD_820          5
98b99f0a4aSAndrew Moore #define FD_800          6
99b99f0a4aSAndrew Moore #define FD_720          7
100b99f0a4aSAndrew Moore #define FD_360          8
101ed2fa05eSAndrey A. Chernov 
102b99f0a4aSAndrew Moore #define FD_1480in5_25   9
103b99f0a4aSAndrew Moore #define FD_1440in5_25   10
104b99f0a4aSAndrew Moore #define FD_820in5_25    11
105b99f0a4aSAndrew Moore #define FD_800in5_25    12
106b99f0a4aSAndrew Moore #define FD_720in5_25    13
107b99f0a4aSAndrew Moore #define FD_360in5_25    14
108b99f0a4aSAndrew Moore 
1097ca0641bSAndrey A. Chernov 
1105b81b6b3SRodney W. Grimes struct fd_type fd_types[NUMTYPES] =
1115b81b6b3SRodney W. Grimes {
112126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
113126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
114126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
115126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
116126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
117126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
118126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
119b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
120ed2fa05eSAndrey A. Chernov 
121126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
122126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
123126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
124126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
125126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
126126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1275b81b6b3SRodney W. Grimes };
1285b81b6b3SRodney W. Grimes 
129b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
1305b81b6b3SRodney W. Grimes /***********************************************************************\
1315b81b6b3SRodney W. Grimes * Per controller structure.						*
1325b81b6b3SRodney W. Grimes \***********************************************************************/
133b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
1345b81b6b3SRodney W. Grimes 
1355b81b6b3SRodney W. Grimes /***********************************************************************\
1365b81b6b3SRodney W. Grimes * Per drive structure.							*
137b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1385b81b6b3SRodney W. Grimes \***********************************************************************/
1395b81b6b3SRodney W. Grimes struct fd_data {
140b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1415b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1423a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1435b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1445b81b6b3SRodney W. Grimes 	int	flags;
1455b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1465b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1475b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1485b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1495b81b6b3SRodney W. Grimes 	int	skip;
1505b81b6b3SRodney W. Grimes 	int	hddrv;
1515b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1523a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
1535b81b6b3SRodney W. Grimes } fd_data[NFD];
1545b81b6b3SRodney W. Grimes 
1555b81b6b3SRodney W. Grimes /***********************************************************************\
1565b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
1575b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
1585b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
1595b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
1605b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
1615b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
1625b81b6b3SRodney W. Grimes \***********************************************************************/
163b99f0a4aSAndrew Moore 
1643a2f7427SDavid Greenman #if NFT > 0
1653a2f7427SDavid Greenman int ftopen(dev_t, int);
1663a2f7427SDavid Greenman int ftintr(ftu_t ftu);
1673a2f7427SDavid Greenman int ftclose(dev_t, int);
1683a2f7427SDavid Greenman void ftstrategy(struct buf *);
1693a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
1703a2f7427SDavid Greenman int ftdump(dev_t);
1713a2f7427SDavid Greenman int ftsize(dev_t);
1723a2f7427SDavid Greenman int ftattach(struct isa_device *, struct isa_device *);
1733a2f7427SDavid Greenman #endif
1745b81b6b3SRodney W. Grimes 
1753a2f7427SDavid Greenman /* autoconfig functions */
1763a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
1773a2f7427SDavid Greenman static int fdattach(struct isa_device *);
1783a2f7427SDavid Greenman 
1793a2f7427SDavid Greenman /* exported functions */
1803a2f7427SDavid Greenman int fdsize (dev_t);
1813a2f7427SDavid Greenman void fdintr(fdcu_t);
1823a2f7427SDavid Greenman int Fdopen(dev_t, int);
1833a2f7427SDavid Greenman int fdclose(dev_t, int);
1843a2f7427SDavid Greenman void fdstrategy(struct buf *);
1853a2f7427SDavid Greenman int fdioctl(dev_t, int, caddr_t, int, struct proc *);
1863a2f7427SDavid Greenman 
1873a2f7427SDavid Greenman /* needed for ft driver, thus exported */
1883a2f7427SDavid Greenman int in_fdc(fdcu_t);
1893a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
1903a2f7427SDavid Greenman 
1913a2f7427SDavid Greenman /* internal functions */
1923a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
1933a2f7427SDavid Greenman #  define TURNON 1
1943a2f7427SDavid Greenman #  define TURNOFF 0
1953a2f7427SDavid Greenman static timeout_t fd_turnoff;
1963a2f7427SDavid Greenman static timeout_t fd_motor_on;
1973a2f7427SDavid Greenman static void fd_turnon(fdu_t);
1983a2f7427SDavid Greenman static void fdc_reset(fdc_p);
1993a2f7427SDavid Greenman static void fdstart(fdcu_t);
2003a2f7427SDavid Greenman static timeout_t fd_timeout;
2013a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2023a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
203aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2043a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2053a2f7427SDavid Greenman 
206aaf08d94SGarrett Wollman 
2075b81b6b3SRodney W. Grimes #define DEVIDLE		0
2085b81b6b3SRodney W. Grimes #define FINDWORK	1
2095b81b6b3SRodney W. Grimes #define	DOSEEK		2
2105b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
2115b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
2125b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
2135b81b6b3SRodney W. Grimes #define	STARTRECAL	6
2145b81b6b3SRodney W. Grimes #define	RESETCTLR	7
2155b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
2165b81b6b3SRodney W. Grimes #define	RECALWAIT	9
2175b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
2185b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
2195b81b6b3SRodney W. Grimes 
2205b81b6b3SRodney W. Grimes #ifdef	DEBUG
2215b81b6b3SRodney W. Grimes char *fdstates[] =
2225b81b6b3SRodney W. Grimes {
2235b81b6b3SRodney W. Grimes "DEVIDLE",
2245b81b6b3SRodney W. Grimes "FINDWORK",
2255b81b6b3SRodney W. Grimes "DOSEEK",
2265b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2275b81b6b3SRodney W. Grimes "IOCOMPLETE",
2285b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2295b81b6b3SRodney W. Grimes "STARTRECAL",
2305b81b6b3SRodney W. Grimes "RESETCTLR",
2315b81b6b3SRodney W. Grimes "SEEKWAIT",
2325b81b6b3SRodney W. Grimes "RECALWAIT",
2335b81b6b3SRodney W. Grimes "MOTORWAIT",
2345b81b6b3SRodney W. Grimes "IOTIMEDOUT"
2355b81b6b3SRodney W. Grimes };
2365b81b6b3SRodney W. Grimes 
2373a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
2383a2f7427SDavid Greenman int	fd_debug = 0;
2395b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2405b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
241381fe1aaSGarrett Wollman #else /* DEBUG */
2425b81b6b3SRodney W. Grimes #define TRACE0(arg)
2435b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
244381fe1aaSGarrett Wollman #endif /* DEBUG */
2455b81b6b3SRodney W. Grimes 
2465b81b6b3SRodney W. Grimes /****************************************************************************/
2475b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
2485b81b6b3SRodney W. Grimes /****************************************************************************/
249b99f0a4aSAndrew Moore struct	isa_driver fdcdriver = {
250b99f0a4aSAndrew Moore 	fdprobe, fdattach, "fdc",
2515b81b6b3SRodney W. Grimes };
2525b81b6b3SRodney W. Grimes 
2535b81b6b3SRodney W. Grimes /*
2545b81b6b3SRodney W. Grimes  * probe for existance of controller
2555b81b6b3SRodney W. Grimes  */
2563a2f7427SDavid Greenman static int
2575b81b6b3SRodney W. Grimes fdprobe(dev)
2585b81b6b3SRodney W. Grimes 	struct isa_device *dev;
2595b81b6b3SRodney W. Grimes {
2605b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
2615b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
2625b81b6b3SRodney W. Grimes 	{
2635b81b6b3SRodney W. Grimes 		printf("fdc: same unit (%d) used multiple times\n", fdcu);
2645b81b6b3SRodney W. Grimes 		return 0;
2655b81b6b3SRodney W. Grimes 	}
2665b81b6b3SRodney W. Grimes 
2675b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
2685b81b6b3SRodney W. Grimes 
26916111cedSAndrew Moore 	/* First - lets reset the floppy controller */
2703a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
27116111cedSAndrew Moore 	DELAY(100);
2723a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
27316111cedSAndrew Moore 
2745b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
2755b81b6b3SRodney W. Grimes 	if (out_fdc(fdcu, NE7CMD_SPECIFY) < 0)
2765b81b6b3SRodney W. Grimes 	{
2775b81b6b3SRodney W. Grimes 		return(0);
2785b81b6b3SRodney W. Grimes 	}
2793a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_1(3, 240));
2803a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_2(2, 0));
2815b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
2825b81b6b3SRodney W. Grimes }
2835b81b6b3SRodney W. Grimes 
2845b81b6b3SRodney W. Grimes /*
2855b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
2865b81b6b3SRodney W. Grimes  */
2873a2f7427SDavid Greenman static int
2885b81b6b3SRodney W. Grimes fdattach(dev)
2895b81b6b3SRodney W. Grimes 	struct isa_device *dev;
2905b81b6b3SRodney W. Grimes {
2913a2f7427SDavid Greenman 	unsigned fdt;
2925b81b6b3SRodney W. Grimes 	fdu_t	fdu;
2935b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
2945b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
2955b81b6b3SRodney W. Grimes 	fd_p	fd;
2966b7bd95bSJoerg Wunsch 	int	fdsu, st0, i;
297b99f0a4aSAndrew Moore 	struct isa_device *fdup;
2985b81b6b3SRodney W. Grimes 
2995b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
3005b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
3015b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
3025b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
3033a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
3043a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
305b99f0a4aSAndrew Moore 	printf("fdc%d:", fdcu);
3065b81b6b3SRodney W. Grimes 
3075b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
308b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
309b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
310b99f0a4aSAndrew Moore 			continue;
311b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
312b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
313b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
314b99f0a4aSAndrew Moore 			continue;
315b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
316b99f0a4aSAndrew Moore 				/* look up what bios thinks we have */
317b99f0a4aSAndrew Moore 		switch (fdu) {
318b99f0a4aSAndrew Moore 			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
319b99f0a4aSAndrew Moore 				break;
320b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
321b99f0a4aSAndrew Moore 				break;
322b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
323b99f0a4aSAndrew Moore 				break;
324b99f0a4aSAndrew Moore 		}
3255b81b6b3SRodney W. Grimes 		/* is there a unit? */
326b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
327b99f0a4aSAndrew Moore #if NFT > 0
328b99f0a4aSAndrew Moore 		|| (fdsu >= DRVS_PER_CTLR)) {
329b99f0a4aSAndrew Moore #else
330b99f0a4aSAndrew Moore 		) {
33156ef0285SAndrew Moore 			fd->type = NO_TYPE;
332b99f0a4aSAndrew Moore #endif
333b99f0a4aSAndrew Moore #if NFT > 0
334b99f0a4aSAndrew Moore 				/* If BIOS says no floppy, or > 2nd device */
335b99f0a4aSAndrew Moore 				/* Probe for and attach a floppy tape.     */
336b99f0a4aSAndrew Moore 			if (ftattach(dev, fdup))
337b99f0a4aSAndrew Moore 				continue;
33856ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
339b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
34056ef0285SAndrew Moore #endif
3415b81b6b3SRodney W. Grimes 			continue;
342f5f7ba03SJordan K. Hubbard 		}
3435b81b6b3SRodney W. Grimes 
3445b81b6b3SRodney W. Grimes 		/* select it */
3453a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
3466b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
3476b7bd95bSJoerg Wunsch 		out_fdc(fdcu, NE7CMD_SENSED);
3486b7bd95bSJoerg Wunsch 		out_fdc(fdcu, fdsu);
3496b7bd95bSJoerg Wunsch 		if(in_fdc(fdcu) & NE7_ST3_T0) {
3506b7bd95bSJoerg Wunsch 			/* if at track 0, first seek inwards */
3513a2f7427SDavid Greenman 			out_fdc(fdcu, NE7CMD_SEEK); /* seek some steps... */
3525b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fdsu);
3533a2f7427SDavid Greenman 			out_fdc(fdcu, 10);
3546b7bd95bSJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
3556b7bd95bSJoerg Wunsch 			out_fdc(fdcu, NE7CMD_SENSEI); /* make ctrlr happy */
3563a2f7427SDavid Greenman 			(void)in_fdc(fdcu);
3573a2f7427SDavid Greenman 			(void)in_fdc(fdcu);
3586b7bd95bSJoerg Wunsch 		}
3596b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
3606b7bd95bSJoerg Wunsch 			/*
3616b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
3626b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
3636b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
3646b7bd95bSJoerg Wunsch 			 * more than 77 steps
3656b7bd95bSJoerg Wunsch 			 */
3666b7bd95bSJoerg Wunsch 			out_fdc(fdcu, NE7CMD_RECAL); /* go back to 0 */
3673a2f7427SDavid Greenman 			out_fdc(fdcu, fdsu);
3686b7bd95bSJoerg Wunsch 			/* a second being enough for full stroke seek */
3696b7bd95bSJoerg Wunsch 			DELAY(i == 0? 1000000: 300000);
3705b81b6b3SRodney W. Grimes 
3716b7bd95bSJoerg Wunsch 			/* anything responding? */
3725b81b6b3SRodney W. Grimes 			out_fdc(fdcu, NE7CMD_SENSEI);
3735b81b6b3SRodney W. Grimes 			st0 = in_fdc(fdcu);
3743a2f7427SDavid Greenman 			(void)in_fdc(fdcu);
3756b7bd95bSJoerg Wunsch 
3766b7bd95bSJoerg Wunsch 			if ((st0 & NE7_ST0_EC) == 0)
3776b7bd95bSJoerg Wunsch 				break; /* already probed succesfully */
3786b7bd95bSJoerg Wunsch 		}
3796b7bd95bSJoerg Wunsch 
3803a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
3813a2f7427SDavid Greenman 
3823a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
3835b81b6b3SRodney W. Grimes 			continue;
3845b81b6b3SRodney W. Grimes 
385b99f0a4aSAndrew Moore 		fd->track = -2;
386b99f0a4aSAndrew Moore 		fd->fdc = fdc;
387b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
3883a2f7427SDavid Greenman 		fd->options = 0;
389b99f0a4aSAndrew Moore 		printf(" [%d: fd%d: ", fdsu, fdu);
3905b81b6b3SRodney W. Grimes 
391b99f0a4aSAndrew Moore 		switch (fdt) {
3927ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
393b99f0a4aSAndrew Moore 			printf("1.2MB 5.25in]");
394b99f0a4aSAndrew Moore 			fd->type = FD_1200;
3957ca0641bSAndrey A. Chernov 			break;
3967ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
397b99f0a4aSAndrew Moore 			printf("1.44MB 3.5in]");
398b99f0a4aSAndrew Moore 			fd->type = FD_1440;
3997ca0641bSAndrey A. Chernov 			break;
4007ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
401b99f0a4aSAndrew Moore 			printf("360KB 5.25in]");
402b99f0a4aSAndrew Moore 			fd->type = FD_360;
4037ca0641bSAndrey A. Chernov 			break;
404ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
405b99f0a4aSAndrew Moore 			printf("720KB 3.5in]");
406b99f0a4aSAndrew Moore 			fd->type = FD_720;
407ed2fa05eSAndrey A. Chernov 			break;
4087ca0641bSAndrey A. Chernov 		default:
409b99f0a4aSAndrew Moore 			printf("unknown]");
410b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
4117ca0641bSAndrey A. Chernov 			break;
4125b81b6b3SRodney W. Grimes 		}
4135b81b6b3SRodney W. Grimes 	}
414b99f0a4aSAndrew Moore 	printf("\n");
4155b81b6b3SRodney W. Grimes 
4163a2f7427SDavid Greenman 	return (1);
4175b81b6b3SRodney W. Grimes }
4185b81b6b3SRodney W. Grimes 
4195b81b6b3SRodney W. Grimes int
4205b81b6b3SRodney W. Grimes fdsize(dev)
4215b81b6b3SRodney W. Grimes 	dev_t	dev;
4225b81b6b3SRodney W. Grimes {
4235b81b6b3SRodney W. Grimes 	return(0);
4245b81b6b3SRodney W. Grimes }
4255b81b6b3SRodney W. Grimes 
4265b81b6b3SRodney W. Grimes /****************************************************************************/
4275b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
4285b81b6b3SRodney W. Grimes /*		remember to not deselect the drive we're working on         */
4295b81b6b3SRodney W. Grimes /****************************************************************************/
4303a2f7427SDavid Greenman static void
4313a2f7427SDavid Greenman set_motor(fdcu, fdsu, turnon)
432f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
4333a2f7427SDavid Greenman 	int fdsu;
4343a2f7427SDavid Greenman 	int turnon;
4355b81b6b3SRodney W. Grimes {
4363a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
4373a2f7427SDavid Greenman 	int needspecify = 0;
4383a2f7427SDavid Greenman 
4393a2f7427SDavid Greenman 	if(turnon) {
4403a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
4413a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
4423a2f7427SDavid Greenman 	} else
4433a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
4443a2f7427SDavid Greenman 
4453a2f7427SDavid Greenman 	if(!turnon
4463a2f7427SDavid Greenman 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
4473a2f7427SDavid Greenman 		/* gonna turn off the last drive, put FDC to bed */
4483a2f7427SDavid Greenman 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
4493a2f7427SDavid Greenman 	else {
4503a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
4513a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
4523a2f7427SDavid Greenman 			needspecify = 1;
4533a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
4545b81b6b3SRodney W. Grimes 	}
4555b81b6b3SRodney W. Grimes 
4563a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
4573a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
4583a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
4593a2f7427SDavid Greenman 
4603a2f7427SDavid Greenman 	if(needspecify) {
4613a2f7427SDavid Greenman 		out_fdc(fdcu, NE7CMD_SPECIFY);
4623a2f7427SDavid Greenman 		out_fdc(fdcu, NE7_SPEC_1(3, 240));
4633a2f7427SDavid Greenman 		out_fdc(fdcu, NE7_SPEC_2(2, 0));
4643a2f7427SDavid Greenman 	}
4653a2f7427SDavid Greenman }
4663a2f7427SDavid Greenman 
4673a2f7427SDavid Greenman /* ARGSUSED */
468381fe1aaSGarrett Wollman static void
469d0917939SPaul Richards fd_turnoff(void *arg1)
4705b81b6b3SRodney W. Grimes {
471381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
472f5f7ba03SJordan K. Hubbard 	int	s;
473f5f7ba03SJordan K. Hubbard 
4745b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
475f5f7ba03SJordan K. Hubbard 	s = splbio();
4765b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
4773a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
478f5f7ba03SJordan K. Hubbard 	splx(s);
4795b81b6b3SRodney W. Grimes }
4805b81b6b3SRodney W. Grimes 
4813a2f7427SDavid Greenman /* ARGSUSED */
4823a2f7427SDavid Greenman static void
483d0917939SPaul Richards fd_motor_on(void *arg1)
4845b81b6b3SRodney W. Grimes {
485381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
486f5f7ba03SJordan K. Hubbard 	int	s;
487f5f7ba03SJordan K. Hubbard 
4885b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
489f5f7ba03SJordan K. Hubbard 	s = splbio();
4905b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
4915b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
4925b81b6b3SRodney W. Grimes 	{
493f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
4945b81b6b3SRodney W. Grimes 	}
495f5f7ba03SJordan K. Hubbard 	splx(s);
4965b81b6b3SRodney W. Grimes }
4975b81b6b3SRodney W. Grimes 
4983a2f7427SDavid Greenman static void
499f5f7ba03SJordan K. Hubbard fd_turnon(fdu)
500f5f7ba03SJordan K. Hubbard 	fdu_t fdu;
5015b81b6b3SRodney W. Grimes {
5025b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
5035b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
5045b81b6b3SRodney W. Grimes 	{
5053a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
5063a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
507d0917939SPaul Richards 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
5085b81b6b3SRodney W. Grimes 	}
5095b81b6b3SRodney W. Grimes }
5105b81b6b3SRodney W. Grimes 
511381fe1aaSGarrett Wollman static void
5123a2f7427SDavid Greenman fdc_reset(fdc)
5133a2f7427SDavid Greenman 	fdc_p fdc;
5145b81b6b3SRodney W. Grimes {
5153a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
5163a2f7427SDavid Greenman 
5173a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
5183a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
5193a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
5203a2f7427SDavid Greenman 	DELAY(100);
5213a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
5223a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
5233a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
5243a2f7427SDavid Greenman 	DELAY(100);
5253a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
5263a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
5273a2f7427SDavid Greenman 
5283a2f7427SDavid Greenman 	out_fdc(fdcu, NE7CMD_SPECIFY);
5293a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_1(3, 240));
5303a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_2(2, 0));
5315b81b6b3SRodney W. Grimes }
5325b81b6b3SRodney W. Grimes 
5335b81b6b3SRodney W. Grimes /****************************************************************************/
5345b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
5355b81b6b3SRodney W. Grimes /****************************************************************************/
5365b81b6b3SRodney W. Grimes int
537f5f7ba03SJordan K. Hubbard in_fdc(fdcu)
538f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
5395b81b6b3SRodney W. Grimes {
5405b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
5415b81b6b3SRodney W. Grimes 	int i, j = 100000;
5423a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
5435b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
5445b81b6b3SRodney W. Grimes 		if (i == NE7_RQM) return -1;
5455b81b6b3SRodney W. Grimes 	if (j <= 0)
5465b81b6b3SRodney W. Grimes 		return(-1);
5475b81b6b3SRodney W. Grimes #ifdef	DEBUG
5483a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
5493a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
5505b81b6b3SRodney W. Grimes 	return(i);
5515b81b6b3SRodney W. Grimes #else
5523a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
5535b81b6b3SRodney W. Grimes #endif
5545b81b6b3SRodney W. Grimes }
5555b81b6b3SRodney W. Grimes 
556381fe1aaSGarrett Wollman int
557f5f7ba03SJordan K. Hubbard out_fdc(fdcu, x)
558f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
559f5f7ba03SJordan K. Hubbard 	int x;
5605b81b6b3SRodney W. Grimes {
5615b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
5623b3837dbSRodney W. Grimes 	int i;
5635b81b6b3SRodney W. Grimes 
5643b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
5653b3837dbSRodney W. Grimes 	i = 100000;
5663a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
5673b3837dbSRodney W. Grimes 	if (i <= 0) return (-1);	/* Floppy timed out */
5683b3837dbSRodney W. Grimes 
5693b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
5703b3837dbSRodney W. Grimes 	i = 100000;
5713a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
5723b3837dbSRodney W. Grimes 	if (i <= 0) return (-1);	/* Floppy timed out */
5733b3837dbSRodney W. Grimes 
5743b3837dbSRodney W. Grimes 	/* Send the command and return */
5753a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
5763a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
5775b81b6b3SRodney W. Grimes 	return (0);
5785b81b6b3SRodney W. Grimes }
5795b81b6b3SRodney W. Grimes 
5805b81b6b3SRodney W. Grimes /****************************************************************************/
5815b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
5825b81b6b3SRodney W. Grimes /****************************************************************************/
583381fe1aaSGarrett Wollman int
5845b81b6b3SRodney W. Grimes Fdopen(dev, flags)
5855b81b6b3SRodney W. Grimes 	dev_t	dev;
5865b81b6b3SRodney W. Grimes 	int	flags;
5875b81b6b3SRodney W. Grimes {
5885b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
58920a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
590b99f0a4aSAndrew Moore 	fdc_p	fdc;
5915b81b6b3SRodney W. Grimes 
592b99f0a4aSAndrew Moore #if NFT > 0
593b99f0a4aSAndrew Moore 	/* check for a tape open */
594b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
595b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
596b99f0a4aSAndrew Moore #endif
5975b81b6b3SRodney W. Grimes 	/* check bounds */
598b99f0a4aSAndrew Moore 	if (fdu >= NFD)
599b99f0a4aSAndrew Moore 		return(ENXIO);
600b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
601b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
602b99f0a4aSAndrew Moore 		return(ENXIO);
603b99f0a4aSAndrew Moore 	if (type > NUMDENS)
604b99f0a4aSAndrew Moore 		return(ENXIO);
6057ca0641bSAndrey A. Chernov 	if (type == 0)
6067ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
6077ca0641bSAndrey A. Chernov 	else {
6087ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
609fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
6107ca0641bSAndrey A. Chernov 			case FD_360:
6117ca0641bSAndrey A. Chernov 				return(ENXIO);
612ed2fa05eSAndrey A. Chernov 			case FD_720:
613b39c878eSAndrey A. Chernov 				if (   type != FD_820
614b39c878eSAndrey A. Chernov 				    && type != FD_800
615ed2fa05eSAndrey A. Chernov 				   )
616ed2fa05eSAndrey A. Chernov 					return(ENXIO);
617ed2fa05eSAndrey A. Chernov 				break;
6187ca0641bSAndrey A. Chernov 			case FD_1200:
619b39c878eSAndrey A. Chernov 				switch (type) {
620b39c878eSAndrey A. Chernov 				case FD_1480:
621b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
622fa4700b4SAndrey A. Chernov 					break;
6237ca0641bSAndrey A. Chernov 				case FD_1440:
624b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
625b39c878eSAndrey A. Chernov 					break;
626b39c878eSAndrey A. Chernov 				case FD_820:
627b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
628b39c878eSAndrey A. Chernov 					break;
629b39c878eSAndrey A. Chernov 				case FD_800:
630b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
631b39c878eSAndrey A. Chernov 					break;
632b39c878eSAndrey A. Chernov 				case FD_720:
633b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
634b39c878eSAndrey A. Chernov 					break;
635b39c878eSAndrey A. Chernov 				case FD_360:
636b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
637b39c878eSAndrey A. Chernov 					break;
638b39c878eSAndrey A. Chernov 				default:
639b39c878eSAndrey A. Chernov 					return(ENXIO);
640b39c878eSAndrey A. Chernov 				}
641b39c878eSAndrey A. Chernov 				break;
642b39c878eSAndrey A. Chernov 			case FD_1440:
643b39c878eSAndrey A. Chernov 				if (   type != FD_1720
644b39c878eSAndrey A. Chernov 				    && type != FD_1480
645ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
646b39c878eSAndrey A. Chernov 				    && type != FD_820
647b39c878eSAndrey A. Chernov 				    && type != FD_800
648b39c878eSAndrey A. Chernov 				    && type != FD_720
6497ca0641bSAndrey A. Chernov 				    )
650dffff499SAndrey A. Chernov 					return(ENXIO);
651fa4700b4SAndrey A. Chernov 				break;
6527ca0641bSAndrey A. Chernov 			}
6537ca0641bSAndrey A. Chernov 		}
654fa4700b4SAndrey A. Chernov 	}
655b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
6565b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
6575b81b6b3SRodney W. Grimes 
6585b81b6b3SRodney W. Grimes 	return 0;
6595b81b6b3SRodney W. Grimes }
6605b81b6b3SRodney W. Grimes 
661381fe1aaSGarrett Wollman int
6625b81b6b3SRodney W. Grimes fdclose(dev, flags)
6635b81b6b3SRodney W. Grimes 	dev_t dev;
664381fe1aaSGarrett Wollman 	int flags;
6655b81b6b3SRodney W. Grimes {
6665b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
667b99f0a4aSAndrew Moore 
668b99f0a4aSAndrew Moore #if NFT > 0
6693a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
6703a2f7427SDavid Greenman 
671b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
6723a2f7427SDavid Greenman 		return ftclose(dev, flags);
673b99f0a4aSAndrew Moore #endif
6745b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
6753a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
6765b81b6b3SRodney W. Grimes 	return(0);
6775b81b6b3SRodney W. Grimes }
6785b81b6b3SRodney W. Grimes 
6795b81b6b3SRodney W. Grimes 
6803a2f7427SDavid Greenman /****************************************************************************/
6813a2f7427SDavid Greenman /*                               fdstrategy                                 */
6823a2f7427SDavid Greenman /****************************************************************************/
6833a2f7427SDavid Greenman void
6843a2f7427SDavid Greenman fdstrategy(struct buf *bp)
6853a2f7427SDavid Greenman {
6863a2f7427SDavid Greenman 	register struct buf *dp;
6873a2f7427SDavid Greenman 	long nblocks, blknum;
6883a2f7427SDavid Greenman  	int	s;
6893a2f7427SDavid Greenman  	fdcu_t	fdcu;
6903a2f7427SDavid Greenman  	fdu_t	fdu;
6913a2f7427SDavid Greenman  	fdc_p	fdc;
6923a2f7427SDavid Greenman  	fd_p	fd;
6933a2f7427SDavid Greenman 	size_t	fdblk;
6943a2f7427SDavid Greenman 
6953a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
6963a2f7427SDavid Greenman 	fd = &fd_data[fdu];
6973a2f7427SDavid Greenman 	fdc = fd->fdc;
6983a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
6993a2f7427SDavid Greenman 	fdblk = 128 << (fd->ft->secsize);
7003a2f7427SDavid Greenman 
7013a2f7427SDavid Greenman #if NFT > 0
7023a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
7033a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
7043a2f7427SDavid Greenman 		bp->b_error = ENXIO;
7053a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
7063a2f7427SDavid Greenman 		goto bad;
7073a2f7427SDavid Greenman 	}
7083a2f7427SDavid Greenman 	/* check for controller already busy with tape */
7093a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
7103a2f7427SDavid Greenman 		bp->b_error = EBUSY;
7113a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
7123a2f7427SDavid Greenman 		goto bad;
7133a2f7427SDavid Greenman 	}
7143a2f7427SDavid Greenman #endif
7153a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
7163a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
7173a2f7427SDavid Greenman 			printf("fdstrat: fdu = %d, blkno = %d, bcount = %d\n",
7183a2f7427SDavid Greenman 			       fdu, bp->b_blkno, bp->b_bcount);
7193a2f7427SDavid Greenman 			bp->b_error = EINVAL;
7203a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
7213a2f7427SDavid Greenman 			goto bad;
7223a2f7427SDavid Greenman 		}
7233a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
7243a2f7427SDavid Greenman 			bp->b_error = EINVAL;
7253a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
7263a2f7427SDavid Greenman 			goto bad;
7273a2f7427SDavid Greenman 		}
7283a2f7427SDavid Greenman 	}
7293a2f7427SDavid Greenman 
7303a2f7427SDavid Greenman 	/*
7313a2f7427SDavid Greenman 	 * Set up block calculations.
7323a2f7427SDavid Greenman 	 */
7333a2f7427SDavid Greenman 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk;
7343a2f7427SDavid Greenman  	nblocks = fd->ft->size;
7353a2f7427SDavid Greenman 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
7363a2f7427SDavid Greenman 		if (blknum == nblocks) {
7373a2f7427SDavid Greenman 			bp->b_resid = bp->b_bcount;
7383a2f7427SDavid Greenman 		} else {
7393a2f7427SDavid Greenman 			bp->b_error = ENOSPC;
7403a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
7413a2f7427SDavid Greenman 		}
7423a2f7427SDavid Greenman 		goto bad;
7433a2f7427SDavid Greenman 	}
7443a2f7427SDavid Greenman  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
7453a2f7427SDavid Greenman 	dp = &(fdc->head);
7463a2f7427SDavid Greenman 	s = splbio();
7473a2f7427SDavid Greenman 	disksort(dp, bp);
7483a2f7427SDavid Greenman 	untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */
7493a2f7427SDavid Greenman 	fdstart(fdcu);
7503a2f7427SDavid Greenman 	splx(s);
7513a2f7427SDavid Greenman 	return;
7523a2f7427SDavid Greenman 
7533a2f7427SDavid Greenman bad:
7543a2f7427SDavid Greenman 	biodone(bp);
7553a2f7427SDavid Greenman }
7563a2f7427SDavid Greenman 
7575b81b6b3SRodney W. Grimes /***************************************************************\
7585b81b6b3SRodney W. Grimes *				fdstart				*
7595b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
7605b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
7615b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
7625b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
7635b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
7645b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
7655b81b6b3SRodney W. Grimes \***************************************************************/
766381fe1aaSGarrett Wollman static void
767f5f7ba03SJordan K. Hubbard fdstart(fdcu)
768f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
7695b81b6b3SRodney W. Grimes {
7705b81b6b3SRodney W. Grimes 	int s;
7715b81b6b3SRodney W. Grimes 
7725b81b6b3SRodney W. Grimes 	s = splbio();
7735b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
7745b81b6b3SRodney W. Grimes 	{
7755b81b6b3SRodney W. Grimes 		fdintr(fdcu);
7765b81b6b3SRodney W. Grimes 	}
7775b81b6b3SRodney W. Grimes 	splx(s);
7785b81b6b3SRodney W. Grimes }
7795b81b6b3SRodney W. Grimes 
7803a2f7427SDavid Greenman /* ARGSUSED */
781381fe1aaSGarrett Wollman static void
782d0917939SPaul Richards fd_timeout(void *arg1)
7835b81b6b3SRodney W. Grimes {
784381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
7855b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
7863a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
7875b81b6b3SRodney W. Grimes 	struct buf *dp, *bp;
788f5f7ba03SJordan K. Hubbard 	int s;
7895b81b6b3SRodney W. Grimes 
7905b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
7915b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
7925b81b6b3SRodney W. Grimes 
7933a2f7427SDavid Greenman 	/*
7943a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
7953a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
7963a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
7973a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
7983a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
7993a2f7427SDavid Greenman 	 */
8005b81b6b3SRodney W. Grimes 
8013a2f7427SDavid Greenman 	s = splbio();
8023a2f7427SDavid Greenman 
8033a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
8043a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
8053a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
8063a2f7427SDavid Greenman 	{
8073a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
8083a2f7427SDavid Greenman 		/* yup, it is; kill it now */
8093a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
8103a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
8113a2f7427SDavid Greenman 	}
8125b81b6b3SRodney W. Grimes 
8135b81b6b3SRodney W. Grimes 	if (bp)
8145b81b6b3SRodney W. Grimes 	{
8155b81b6b3SRodney W. Grimes 		retrier(fdcu);
8163a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
8175b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
8185b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
8195b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
8205b81b6b3SRodney W. Grimes 	}
8215b81b6b3SRodney W. Grimes 	else
8225b81b6b3SRodney W. Grimes 	{
8235b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
8245b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
8255b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
8265b81b6b3SRodney W. Grimes 	}
827f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
828f5f7ba03SJordan K. Hubbard 	splx(s);
8295b81b6b3SRodney W. Grimes }
8305b81b6b3SRodney W. Grimes 
8315b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
8323a2f7427SDavid Greenman /* ARGSUSED */
833381fe1aaSGarrett Wollman static void
834d0917939SPaul Richards fd_pseudointr(void *arg1)
8355b81b6b3SRodney W. Grimes {
836381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
8375b81b6b3SRodney W. Grimes 	int	s;
8383a2f7427SDavid Greenman 
8395b81b6b3SRodney W. Grimes 	s = splbio();
8405b81b6b3SRodney W. Grimes 	fdintr(fdcu);
8415b81b6b3SRodney W. Grimes 	splx(s);
8425b81b6b3SRodney W. Grimes }
8435b81b6b3SRodney W. Grimes 
8445b81b6b3SRodney W. Grimes /***********************************************************************\
8455b81b6b3SRodney W. Grimes *                                 fdintr				*
8465b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
8475b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
8485b81b6b3SRodney W. Grimes \***********************************************************************/
849381fe1aaSGarrett Wollman void
850381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
8515b81b6b3SRodney W. Grimes {
8525b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
853b99f0a4aSAndrew Moore #if NFT > 0
854b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
855b99f0a4aSAndrew Moore 
856b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
857b99f0a4aSAndrew Moore 		(ftintr(fdu));
858b99f0a4aSAndrew Moore 	else
859b99f0a4aSAndrew Moore #endif
860381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
861381fe1aaSGarrett Wollman 			;
8625b81b6b3SRodney W. Grimes }
8635b81b6b3SRodney W. Grimes 
8645b81b6b3SRodney W. Grimes /***********************************************************************\
8655b81b6b3SRodney W. Grimes * The controller state machine.						*
8665b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
8675b81b6b3SRodney W. Grimes \***********************************************************************/
8683a2f7427SDavid Greenman static int
869381fe1aaSGarrett Wollman fdstate(fdcu, fdc)
870f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
871f5f7ba03SJordan K. Hubbard 	fdc_p fdc;
8725b81b6b3SRodney W. Grimes {
8733a2f7427SDavid Greenman 	int read, format, head, sec = 0, i = 0, sectrac, st0, cyl, st3;
8745b81b6b3SRodney W. Grimes 	unsigned long blknum;
8755b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
8765b81b6b3SRodney W. Grimes 	fd_p fd;
8775b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
878b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
8793a2f7427SDavid Greenman 	size_t fdblk;
8805b81b6b3SRodney W. Grimes 
8815b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
8825b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
8835b81b6b3SRodney W. Grimes 	if(!bp)
8845b81b6b3SRodney W. Grimes 	{
8855b81b6b3SRodney W. Grimes 		/***********************************************\
8865b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
8875b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
8885b81b6b3SRodney W. Grimes 		\***********************************************/
8895b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
8905b81b6b3SRodney W. Grimes 		if(fdc->fd)
8915b81b6b3SRodney W. Grimes 		{
8923a2f7427SDavid Greenman 			printf("unexpected valid fd pointer (fdu = %d)\n",
8933a2f7427SDavid Greenman 			       fdc->fdu);
8945b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
8955b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
8965b81b6b3SRodney W. Grimes 		}
8975b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
8985b81b6b3SRodney W. Grimes  		return(0);
8995b81b6b3SRodney W. Grimes 	}
9005b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
9015b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
9023a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
9035b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
9045b81b6b3SRodney W. Grimes 	{
9055b81b6b3SRodney W. Grimes 		printf("confused fd pointers\n");
9065b81b6b3SRodney W. Grimes 	}
9075b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
908b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
909b39c878eSAndrey A. Chernov 	if(format)
910b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
9115b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
9125b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
9135b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
914d0917939SPaul Richards 	untimeout(fd_turnoff, (caddr_t)fdu);
915d0917939SPaul Richards 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
9165b81b6b3SRodney W. Grimes 	switch (fdc->state)
9175b81b6b3SRodney W. Grimes 	{
9185b81b6b3SRodney W. Grimes 	case DEVIDLE:
9195b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
9205b81b6b3SRodney W. Grimes 		fdc->retry = 0;
9215b81b6b3SRodney W. Grimes 		fd->skip = 0;
9225b81b6b3SRodney W. Grimes 		fdc->fd = fd;
9235b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
9243a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
9253a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
9265b81b6b3SRodney W. Grimes 		/*******************************************************\
9275b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
9285b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
9295b81b6b3SRodney W. Grimes 		\*******************************************************/
9305b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
9315b81b6b3SRodney W. Grimes 		{
9325b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
9335b81b6b3SRodney W. Grimes 			return(0); /* come back later */
9345b81b6b3SRodney W. Grimes 		}
9355b81b6b3SRodney W. Grimes 		/*******************************************************\
9365b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
9375b81b6b3SRodney W. Grimes 		\*******************************************************/
9385b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
9395b81b6b3SRodney W. Grimes 		{
9405b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
9415b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
9425b81b6b3SRodney W. Grimes 			return(0);
9435b81b6b3SRodney W. Grimes 		}
9445b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
9455b81b6b3SRodney W. Grimes 		{
9463a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
9475b81b6b3SRodney W. Grimes 		}
9485b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
9495b81b6b3SRodney W. Grimes 		break;
9505b81b6b3SRodney W. Grimes 	case DOSEEK:
9515b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
9525b81b6b3SRodney W. Grimes 		{
9535b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
9545b81b6b3SRodney W. Grimes 			break;
9555b81b6b3SRodney W. Grimes 		}
9565b81b6b3SRodney W. Grimes 		out_fdc(fdcu, NE7CMD_SEEK);	/* Seek function */
9575b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fd->fdsu);	/* Drive number */
9585b81b6b3SRodney W. Grimes 		out_fdc(fdcu, bp->b_cylin * fd->ft->steptrac);
9595b81b6b3SRodney W. Grimes 		fd->track = -2;
9605b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
9615b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
9625b81b6b3SRodney W. Grimes 	case SEEKWAIT:
9635b81b6b3SRodney W. Grimes 		/* allow heads to settle */
9643a2f7427SDavid Greenman 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 32);
9655b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
9665b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
9675b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
9685b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
9695b81b6b3SRodney W. Grimes 		if(fd->track == -2)
9705b81b6b3SRodney W. Grimes 		{
9715b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
9723a2f7427SDavid Greenman 			do {
9735b81b6b3SRodney W. Grimes 				out_fdc(fdcu, NE7CMD_SENSEI);
9743a2f7427SDavid Greenman 				st0 = in_fdc(fdcu);
9755b81b6b3SRodney W. Grimes 				cyl = in_fdc(fdcu);
9763a2f7427SDavid Greenman 				/*
9773a2f7427SDavid Greenman 				 * if this was a "ready changed" interrupt,
9783a2f7427SDavid Greenman 				 * fetch status again (can happen after
9793a2f7427SDavid Greenman 				 * enabling controller from reset state)
9803a2f7427SDavid Greenman 				 */
9813a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
9823a2f7427SDavid Greenman 			if (0 == descyl)
9833a2f7427SDavid Greenman 			{
9843a2f7427SDavid Greenman 				/*
9853a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
9863a2f7427SDavid Greenman 				 * really there
9873a2f7427SDavid Greenman 				 */
9883a2f7427SDavid Greenman 				out_fdc(fdcu, NE7CMD_SENSED);
9893a2f7427SDavid Greenman 				out_fdc(fdcu, fdu);
9903a2f7427SDavid Greenman 				st3 = in_fdc(fdcu);
9913a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
9923a2f7427SDavid Greenman 					printf(
9933a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
9943a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
9953a2f7427SDavid Greenman 					if(fdc->retry < 3)
9963a2f7427SDavid Greenman 						fdc->retry = 3;
9973a2f7427SDavid Greenman 					return(retrier(fdcu));
9983a2f7427SDavid Greenman 				}
9993a2f7427SDavid Greenman 			}
10005b81b6b3SRodney W. Grimes 			if (cyl != descyl)
10015b81b6b3SRodney W. Grimes 			{
10023a2f7427SDavid Greenman 				printf(
10033a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
10043a2f7427SDavid Greenman 				       fdu, descyl, cyl, st0, NE7_ST0BITS);
10055b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
10065b81b6b3SRodney W. Grimes 			}
10075b81b6b3SRodney W. Grimes 		}
10085b81b6b3SRodney W. Grimes 
10095b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
1010b39c878eSAndrey A. Chernov 		if(format)
1011b39c878eSAndrey A. Chernov 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1012b39c878eSAndrey A. Chernov 				- (char *)finfo;
10135b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
10143a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
10153a2f7427SDavid Greenman 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
10163a2f7427SDavid Greenman 			+ fd->skip/fdblk;
10175b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
10185b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
10195b81b6b3SRodney W. Grimes 		head = sec / sectrac;
10205b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
10213a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
10223a2f7427SDavid Greenman 
10233a2f7427SDavid Greenman 		if(format || !read)
10243a2f7427SDavid Greenman 		{
10253a2f7427SDavid Greenman 			/* make sure the drive is writable */
10263a2f7427SDavid Greenman 			out_fdc(fdcu, NE7CMD_SENSED);
10273a2f7427SDavid Greenman 			out_fdc(fdcu, fdu);
10283a2f7427SDavid Greenman 			st3 = in_fdc(fdcu);
10293a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
10303a2f7427SDavid Greenman 			{
10313a2f7427SDavid Greenman 				/*
10323a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
10333a2f7427SDavid Greenman 				 * in order to force the current operation
10343a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
10353a2f7427SDavid Greenman 				 * error - all error handling is done
10363a2f7427SDavid Greenman 				 * by the retrier()
10373a2f7427SDavid Greenman 				 */
10383a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
10393a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
10403a2f7427SDavid Greenman 				fdc->status[2] = 0;
10413a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
10423a2f7427SDavid Greenman 				fdc->status[4] = head;
10433a2f7427SDavid Greenman 				fdc->status[5] = sec;
10443a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
10453a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
10463a2f7427SDavid Greenman 				return (1);
10473a2f7427SDavid Greenman 			}
10483a2f7427SDavid Greenman 		}
10495b81b6b3SRodney W. Grimes 
1050b39c878eSAndrey A. Chernov 		if(format)
1051b39c878eSAndrey A. Chernov 		{
1052b39c878eSAndrey A. Chernov 			/* formatting */
10533a2f7427SDavid Greenman 			out_fdc(fdcu, NE7CMD_FORMAT);
1054b39c878eSAndrey A. Chernov 			out_fdc(fdcu, head << 2 | fdu);
1055b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_secshift);
1056b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_nsecs);
1057b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_gaplen);
1058b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_fillbyte);
1059b39c878eSAndrey A. Chernov 		}
1060b39c878eSAndrey A. Chernov 		else
1061b39c878eSAndrey A. Chernov 		{
10625b81b6b3SRodney W. Grimes 			if (read)
10635b81b6b3SRodney W. Grimes 			{
10645b81b6b3SRodney W. Grimes 				out_fdc(fdcu, NE7CMD_READ);      /* READ */
10655b81b6b3SRodney W. Grimes 			}
10665b81b6b3SRodney W. Grimes 			else
10675b81b6b3SRodney W. Grimes 			{
10685b81b6b3SRodney W. Grimes 				out_fdc(fdcu, NE7CMD_WRITE);     /* WRITE */
10695b81b6b3SRodney W. Grimes 			}
10705b81b6b3SRodney W. Grimes 			out_fdc(fdcu, head << 2 | fdu);  /* head & unit */
10715b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->track);        /* track */
10725b81b6b3SRodney W. Grimes 			out_fdc(fdcu, head);
10735b81b6b3SRodney W. Grimes 			out_fdc(fdcu, sec);              /* sector XXX +1? */
10745b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->secsize);  /* sector size */
10755b81b6b3SRodney W. Grimes 			out_fdc(fdcu, sectrac);          /* sectors/track */
10765b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->gap);      /* gap size */
10775b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->datalen);  /* data length */
1078b39c878eSAndrey A. Chernov 		}
10795b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
10803a2f7427SDavid Greenman 		timeout(fd_timeout, (caddr_t)fdcu, hz);
10815b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
10825b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
1083d0917939SPaul Richards 		untimeout(fd_timeout, (caddr_t)fdcu);
10845b81b6b3SRodney W. Grimes 		for(i=0;i<7;i++)
10855b81b6b3SRodney W. Grimes 		{
10865b81b6b3SRodney W. Grimes 			fdc->status[i] = in_fdc(fdcu);
10875b81b6b3SRodney W. Grimes 		}
10883a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
10893a2f7427SDavid Greenman 		/* FALLTHROUGH */
10903a2f7427SDavid Greenman 	case IOTIMEDOUT:
10915b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
10923a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
10933a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
10945b81b6b3SRodney W. Grimes 		{
10953a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
10963a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1097b39c878eSAndrey A. Chernov                                 /*
10983a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
10993a2f7427SDavid Greenman 				 * and didn't release it in time for the
11003a2f7427SDavid Greenman 				 * next FDC transfer.
11013a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
11023a2f7427SDavid Greenman 				 * count. (vak)
1103b39c878eSAndrey A. Chernov                                  */
1104b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1105b39c878eSAndrey A. Chernov                                 return (1);
1106b39c878eSAndrey A. Chernov                         }
11073a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
11083a2f7427SDavid Greenman 				&& fdc->retry < 6)
11093a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
11103a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
11113a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
11123a2f7427SDavid Greenman 				&& fdc->retry < 3)
11133a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
11145b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
11155b81b6b3SRodney W. Grimes 		}
11165b81b6b3SRodney W. Grimes 		/* All OK */
11173a2f7427SDavid Greenman 		fd->skip += fdblk;
1118b39c878eSAndrey A. Chernov 		if (!format && fd->skip < bp->b_bcount)
11195b81b6b3SRodney W. Grimes 		{
11205b81b6b3SRodney W. Grimes 			/* set up next transfer */
11213a2f7427SDavid Greenman 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
11223a2f7427SDavid Greenman 				+ fd->skip/fdblk;
11233a2f7427SDavid Greenman 			bp->b_cylin =
11243a2f7427SDavid Greenman 				(blknum / (fd->ft->sectrac * fd->ft->heads));
11255b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
11265b81b6b3SRodney W. Grimes 		}
11275b81b6b3SRodney W. Grimes 		else
11285b81b6b3SRodney W. Grimes 		{
11295b81b6b3SRodney W. Grimes 			/* ALL DONE */
11305b81b6b3SRodney W. Grimes 			fd->skip = 0;
11315b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
113226f9a767SRodney W. Grimes 			dp->b_actf = bp->b_actf;
11335b81b6b3SRodney W. Grimes 			biodone(bp);
11345b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
11355b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
11365b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
11375b81b6b3SRodney W. Grimes 		}
11385b81b6b3SRodney W. Grimes 		return(1);
11395b81b6b3SRodney W. Grimes 	case RESETCTLR:
11403a2f7427SDavid Greenman 		fdc_reset(fdc);
11415b81b6b3SRodney W. Grimes 		fdc->retry++;
11425b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
11435b81b6b3SRodney W. Grimes 		break;
11445b81b6b3SRodney W. Grimes 	case STARTRECAL:
11455b81b6b3SRodney W. Grimes 		out_fdc(fdcu, NE7CMD_RECAL);	/* Recalibrate Function */
11465b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fdu);
11475b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
11485b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
11495b81b6b3SRodney W. Grimes 	case RECALWAIT:
11505b81b6b3SRodney W. Grimes 		/* allow heads to settle */
11513a2f7427SDavid Greenman 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 32);
11525b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
11535b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
11545b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
11553a2f7427SDavid Greenman 		do {
11565b81b6b3SRodney W. Grimes 			out_fdc(fdcu, NE7CMD_SENSEI);
11575b81b6b3SRodney W. Grimes 			st0 = in_fdc(fdcu);
11585b81b6b3SRodney W. Grimes 			cyl = in_fdc(fdcu);
11593a2f7427SDavid Greenman 			/*
11603a2f7427SDavid Greenman 			 * if this was a "ready changed" interrupt,
11613a2f7427SDavid Greenman 			 * fetch status again (can happen after
11623a2f7427SDavid Greenman 			 * enabling controller from reset state)
11633a2f7427SDavid Greenman 			 */
11643a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
11653a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
11665b81b6b3SRodney W. Grimes 		{
11675b81b6b3SRodney W. Grimes 			printf("fd%d: recal failed ST0 %b cyl %d\n", fdu,
11685b81b6b3SRodney W. Grimes 				st0, NE7_ST0BITS, cyl);
11693a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
11705b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
11715b81b6b3SRodney W. Grimes 		}
11725b81b6b3SRodney W. Grimes 		fd->track = 0;
11735b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
11745b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
11755b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
11765b81b6b3SRodney W. Grimes 	case MOTORWAIT:
11775b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
11785b81b6b3SRodney W. Grimes 		{
11795b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
11805b81b6b3SRodney W. Grimes 		}
11813a2f7427SDavid Greenman 		/*
11823a2f7427SDavid Greenman 		 * since the controller was off, it has lost its
11833a2f7427SDavid Greenman 		 * idea about the current track it were; thus,
11843a2f7427SDavid Greenman 		 * recalibrate the bastard
11853a2f7427SDavid Greenman 		 */
11863a2f7427SDavid Greenman 		fdc->state = STARTRECAL;
11875b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
11885b81b6b3SRodney W. Grimes 	default:
11895b81b6b3SRodney W. Grimes 		printf("Unexpected FD int->");
11905b81b6b3SRodney W. Grimes 		out_fdc(fdcu, NE7CMD_SENSEI);
11915b81b6b3SRodney W. Grimes 		st0 = in_fdc(fdcu);
11925b81b6b3SRodney W. Grimes 		cyl = in_fdc(fdcu);
11933a2f7427SDavid Greenman 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
11943a2f7427SDavid Greenman 		out_fdc(fdcu, NE7CMD_READID);
11955b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fd->fdsu);
11965b81b6b3SRodney W. Grimes 		for(i=0;i<7;i++) {
11975b81b6b3SRodney W. Grimes 			fdc->status[i] = in_fdc(fdcu);
11985b81b6b3SRodney W. Grimes 		}
11993a2f7427SDavid Greenman 		if(fdc->status[0] != -1)
12003a2f7427SDavid Greenman 			printf("intr status :%lx %lx %lx %lx %lx %lx %lx\n",
12015b81b6b3SRodney W. Grimes 			       fdc->status[0],
12025b81b6b3SRodney W. Grimes 			       fdc->status[1],
12035b81b6b3SRodney W. Grimes 			       fdc->status[2],
12045b81b6b3SRodney W. Grimes 			       fdc->status[3],
12055b81b6b3SRodney W. Grimes 			       fdc->status[4],
12065b81b6b3SRodney W. Grimes 			       fdc->status[5],
12075b81b6b3SRodney W. Grimes 			       fdc->status[6] );
12083a2f7427SDavid Greenman 		else
12093a2f7427SDavid Greenman 			printf("FDC timed out\n");
12105b81b6b3SRodney W. Grimes 		return(0);
12115b81b6b3SRodney W. Grimes 	}
12125b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
12135b81b6b3SRodney W. Grimes }
12145b81b6b3SRodney W. Grimes 
1215aaf08d94SGarrett Wollman static int
1216f5f7ba03SJordan K. Hubbard retrier(fdcu)
1217f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
12185b81b6b3SRodney W. Grimes {
12195b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
12205b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
12215b81b6b3SRodney W. Grimes 
12225b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
12235b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
12245b81b6b3SRodney W. Grimes 
12253a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
12263a2f7427SDavid Greenman 		goto fail;
12275b81b6b3SRodney W. Grimes 	switch(fdc->retry)
12285b81b6b3SRodney W. Grimes 	{
12295b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
12305b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
12315b81b6b3SRodney W. Grimes 		break;
12325b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
12335b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
12345b81b6b3SRodney W. Grimes 		break;
12355b81b6b3SRodney W. Grimes 	case 6:
12365b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
12375b81b6b3SRodney W. Grimes 		break;
12385b81b6b3SRodney W. Grimes 	case 7:
12395b81b6b3SRodney W. Grimes 		break;
12405b81b6b3SRodney W. Grimes 	default:
12413a2f7427SDavid Greenman 	fail:
12425b81b6b3SRodney W. Grimes 		{
12437ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
12447ca0641bSAndrey A. Chernov 			/* Trick diskerr */
12453a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
12463a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
124792ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
12483a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
12493a2f7427SDavid Greenman 				(struct disklabel *)NULL);
12507ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
125192ed385aSRodney W. Grimes 			printf(" (ST0 %b ", fdc->status[0], NE7_ST0BITS);
12525b81b6b3SRodney W. Grimes 			printf(" ST1 %b ", fdc->status[1], NE7_ST1BITS);
12535b81b6b3SRodney W. Grimes 			printf(" ST2 %b ", fdc->status[2], NE7_ST2BITS);
12545b81b6b3SRodney W. Grimes 			printf("cyl %d hd %d sec %d)\n",
125592ed385aSRodney W. Grimes 			       fdc->status[3], fdc->status[4], fdc->status[5]);
12565b81b6b3SRodney W. Grimes 		}
12575b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
12585b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
12595b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
126026f9a767SRodney W. Grimes 		dp->b_actf = bp->b_actf;
12615b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
12625b81b6b3SRodney W. Grimes 		biodone(bp);
126392ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
12645b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
12655b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1266f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
126792ed385aSRodney W. Grimes 		return(1);
12685b81b6b3SRodney W. Grimes 	}
12695b81b6b3SRodney W. Grimes 	fdc->retry++;
12705b81b6b3SRodney W. Grimes 	return(1);
12715b81b6b3SRodney W. Grimes }
12725b81b6b3SRodney W. Grimes 
1273b39c878eSAndrey A. Chernov static int
1274b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1275b39c878eSAndrey A. Chernov 	dev_t dev;
1276b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1277b39c878eSAndrey A. Chernov 	struct proc *p;
1278b39c878eSAndrey A. Chernov {
1279b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1280b39c878eSAndrey A. Chernov  	fd_p	fd;
1281b39c878eSAndrey A. Chernov 
1282b39c878eSAndrey A. Chernov 	struct buf *bp;
1283b39c878eSAndrey A. Chernov 	int rv = 0, s;
12843a2f7427SDavid Greenman 	size_t fdblk;
1285b39c878eSAndrey A. Chernov 
1286b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1287b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
12883a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1289b39c878eSAndrey A. Chernov 
1290b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1291b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1292b39c878eSAndrey A. Chernov 	if(bp == 0)
1293b39c878eSAndrey A. Chernov 		return ENOBUFS;
1294b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1295b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1296b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1297b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1298b39c878eSAndrey A. Chernov 
1299b39c878eSAndrey A. Chernov 	/*
1300b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1301b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1302b39c878eSAndrey A. Chernov 	 */
1303b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
13043a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1305b39c878eSAndrey A. Chernov 
1306b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1307b39c878eSAndrey A. Chernov 	bp->b_un.b_addr = (caddr_t)finfo;
1308b39c878eSAndrey A. Chernov 
1309b39c878eSAndrey A. Chernov 	/* now do the format */
1310b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1311b39c878eSAndrey A. Chernov 
1312b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1313b39c878eSAndrey A. Chernov 	s = splbio();
1314b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1315b39c878eSAndrey A. Chernov 	{
1316b39c878eSAndrey A. Chernov 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1317b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1318b39c878eSAndrey A. Chernov 			break;
1319b39c878eSAndrey A. Chernov 	}
1320b39c878eSAndrey A. Chernov 	splx(s);
1321b39c878eSAndrey A. Chernov 
1322b39c878eSAndrey A. Chernov 	if(rv == EWOULDBLOCK)
1323b39c878eSAndrey A. Chernov 		/* timed out */
1324b39c878eSAndrey A. Chernov 		rv = EIO;
13253a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
13263a2f7427SDavid Greenman 		rv = bp->b_error;
13273a2f7427SDavid Greenman 	biodone(bp);
1328b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1329b39c878eSAndrey A. Chernov 	return rv;
1330b39c878eSAndrey A. Chernov }
1331b39c878eSAndrey A. Chernov 
1332f5f7ba03SJordan K. Hubbard /*
1333f5f7ba03SJordan K. Hubbard  *
13343a2f7427SDavid Greenman  * TODO: Think about allocating buffer off stack.
1335f5f7ba03SJordan K. Hubbard  *       Don't pass uncast 0's and NULL's to read/write/setdisklabel().
1336f5f7ba03SJordan K. Hubbard  *       Watch out for NetBSD's different *disklabel() interface.
1337b39c878eSAndrey A. Chernov  *
1338f5f7ba03SJordan K. Hubbard  */
13395b81b6b3SRodney W. Grimes 
1340f5f7ba03SJordan K. Hubbard int
1341b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1342f5f7ba03SJordan K. Hubbard 	dev_t dev;
1343f5f7ba03SJordan K. Hubbard 	int cmd;
1344f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1345f5f7ba03SJordan K. Hubbard 	int flag;
1346b39c878eSAndrey A. Chernov 	struct proc *p;
1347f5f7ba03SJordan K. Hubbard {
13483a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
13493a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
13503a2f7427SDavid Greenman 	size_t fdblk;
13513a2f7427SDavid Greenman 
1352f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1353f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1354f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
13553a2f7427SDavid Greenman 	int error = 0;
1356f5f7ba03SJordan K. Hubbard 
1357b99f0a4aSAndrew Moore #if NFT > 0
1358a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1359a60eff27SNate Williams 
1360a60eff27SNate Williams 	/* check for a tape ioctl */
1361a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1362b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1363b99f0a4aSAndrew Moore #endif
1364b99f0a4aSAndrew Moore 
13653a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1366f5f7ba03SJordan K. Hubbard 
1367f5f7ba03SJordan K. Hubbard 	switch (cmd)
1368f5f7ba03SJordan K. Hubbard 	{
1369f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1370f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1371f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
13723a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
137392ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1374f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1375f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1376f5f7ba03SJordan K. Hubbard 
1377f5f7ba03SJordan K. Hubbard 		if (readdisklabel(dev, fdstrategy, dl, NULL, 0, 0) == NULL)
1378f5f7ba03SJordan K. Hubbard 			error = 0;
1379f5f7ba03SJordan K. Hubbard 		else
1380f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1381f5f7ba03SJordan K. Hubbard 
1382f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1383f5f7ba03SJordan K. Hubbard 		break;
1384f5f7ba03SJordan K. Hubbard 
1385f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1386f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1387f5f7ba03SJordan K. Hubbard 			error = EBADF;
1388f5f7ba03SJordan K. Hubbard 		break;
1389f5f7ba03SJordan K. Hubbard 
1390f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1391f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1392f5f7ba03SJordan K. Hubbard 			error = EBADF;
1393f5f7ba03SJordan K. Hubbard 		break;
1394f5f7ba03SJordan K. Hubbard 
1395f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1396f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1397f5f7ba03SJordan K. Hubbard 		{
1398f5f7ba03SJordan K. Hubbard 			error = EBADF;
1399f5f7ba03SJordan K. Hubbard 			break;
1400f5f7ba03SJordan K. Hubbard 		}
1401f5f7ba03SJordan K. Hubbard 
1402f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1403f5f7ba03SJordan K. Hubbard 
14043a2f7427SDavid Greenman 		if ((error =
14053a2f7427SDavid Greenman 		     setdisklabel ((struct disklabel *)buffer, dl, 0, NULL)))
1406f5f7ba03SJordan K. Hubbard 			break;
1407f5f7ba03SJordan K. Hubbard 
1408b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
1409b39c878eSAndrey A. Chernov 				       (struct disklabel *)buffer, NULL);
1410b39c878eSAndrey A. Chernov 		break;
1411b39c878eSAndrey A. Chernov 
1412b39c878eSAndrey A. Chernov 	case FD_FORM:
1413b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1414b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1415b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1416b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1417b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1418b39c878eSAndrey A. Chernov 		else
1419b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1420b39c878eSAndrey A. Chernov 		break;
1421b39c878eSAndrey A. Chernov 
1422b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1423b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1424f5f7ba03SJordan K. Hubbard 		break;
1425f5f7ba03SJordan K. Hubbard 
14263a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
14273a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
14283a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
14293a2f7427SDavid Greenman 			return EPERM;
14303a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
14313a2f7427SDavid Greenman 		break;
14323a2f7427SDavid Greenman 
14333a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
14343a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
14353a2f7427SDavid Greenman 		break;
14363a2f7427SDavid Greenman 
14373a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
14383a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
14393a2f7427SDavid Greenman 		break;
14403a2f7427SDavid Greenman 
1441f5f7ba03SJordan K. Hubbard 	default:
14423a2f7427SDavid Greenman 		error = ENOTTY;
1443f5f7ba03SJordan K. Hubbard 		break;
1444f5f7ba03SJordan K. Hubbard 	}
1445f5f7ba03SJordan K. Hubbard 	return (error);
1446f5f7ba03SJordan K. Hubbard }
1447f5f7ba03SJordan K. Hubbard 
1448f5f7ba03SJordan K. Hubbard #endif
14493a2f7427SDavid Greenman /*
14503a2f7427SDavid Greenman  * Hello emacs, these are the
14513a2f7427SDavid Greenman  * Local Variables:
14523a2f7427SDavid Greenman  *  c-indent-level:               8
14533a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
14543a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
14553a2f7427SDavid Greenman  *  c-brace-offset:              -8
14563a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
14573a2f7427SDavid Greenman  *  c-argdecl-indent:             8
14583a2f7427SDavid Greenman  *  c-label-offset:              -8
14593a2f7427SDavid Greenman  *  c++-hanging-braces:           1
14603a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
14613a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
14623a2f7427SDavid Greenman  *  c++-friend-offset:            0
14633a2f7427SDavid Greenman  * End:
14643a2f7427SDavid Greenman  */
1465