xref: /freebsd/sys/dev/fdc/fdc.c (revision 87eafbcacd0f66b638e7bb730d2df94d341e5670)
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
4387eafbcaSPoul-Henning Kamp  *	$Id: fd.c,v 1.31 1994/09/17 18:08:36 joerg 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>
7487eafbcaSPoul-Henning Kamp #if NFT > 0
7587eafbcaSPoul-Henning Kamp #include <sys/ftape.h>
7687eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h>
7787eafbcaSPoul-Henning Kamp #endif
785b81b6b3SRodney W. Grimes 
79b2be795bSAndrey A. Chernov #define RAW_PART 2
805b81b6b3SRodney W. Grimes #define b_cylin b_resid
815b81b6b3SRodney W. Grimes 
82b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
83b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX
845b81b6b3SRodney W. Grimes 
853a2f7427SDavid Greenman /*
863a2f7427SDavid Greenman  * this biotab field doubles as a field for the physical unit number
873a2f7427SDavid Greenman  * on the controller
883a2f7427SDavid Greenman  */
893a2f7427SDavid Greenman #define id_physid id_scsiid
903a2f7427SDavid Greenman 
91b39c878eSAndrey A. Chernov #define NUMTYPES 14
92b39c878eSAndrey A. Chernov #define NUMDENS  (NUMTYPES - 6)
937ca0641bSAndrey A. Chernov 
943a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
95b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
96b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
97b99f0a4aSAndrew Moore #define FD_1720         1
98b99f0a4aSAndrew Moore #define FD_1480         2
99b99f0a4aSAndrew Moore #define FD_1440         3
100b99f0a4aSAndrew Moore #define FD_1200         4
101b99f0a4aSAndrew Moore #define FD_820          5
102b99f0a4aSAndrew Moore #define FD_800          6
103b99f0a4aSAndrew Moore #define FD_720          7
104b99f0a4aSAndrew Moore #define FD_360          8
105ed2fa05eSAndrey A. Chernov 
106b99f0a4aSAndrew Moore #define FD_1480in5_25   9
107b99f0a4aSAndrew Moore #define FD_1440in5_25   10
108b99f0a4aSAndrew Moore #define FD_820in5_25    11
109b99f0a4aSAndrew Moore #define FD_800in5_25    12
110b99f0a4aSAndrew Moore #define FD_720in5_25    13
111b99f0a4aSAndrew Moore #define FD_360in5_25    14
112b99f0a4aSAndrew Moore 
1137ca0641bSAndrey A. Chernov 
1145b81b6b3SRodney W. Grimes struct fd_type fd_types[NUMTYPES] =
1155b81b6b3SRodney W. Grimes {
116126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
117126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
118126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
119126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
120126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
121126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
122126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
123b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
124ed2fa05eSAndrey A. Chernov 
125126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
126126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
127126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
128126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
129126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
130126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1315b81b6b3SRodney W. Grimes };
1325b81b6b3SRodney W. Grimes 
133b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
1345b81b6b3SRodney W. Grimes /***********************************************************************\
1355b81b6b3SRodney W. Grimes * Per controller structure.						*
1365b81b6b3SRodney W. Grimes \***********************************************************************/
137b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC];
1385b81b6b3SRodney W. Grimes 
1395b81b6b3SRodney W. Grimes /***********************************************************************\
1405b81b6b3SRodney W. Grimes * Per drive structure.							*
141b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1425b81b6b3SRodney W. Grimes \***********************************************************************/
1435b81b6b3SRodney W. Grimes struct fd_data {
144b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1455b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1463a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1475b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1485b81b6b3SRodney W. Grimes 	int	flags;
1495b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1505b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1515b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1525b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1535b81b6b3SRodney W. Grimes 	int	skip;
1545b81b6b3SRodney W. Grimes 	int	hddrv;
1555b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1563a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
1575b81b6b3SRodney W. Grimes } fd_data[NFD];
1585b81b6b3SRodney W. Grimes 
1595b81b6b3SRodney W. Grimes /***********************************************************************\
1605b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
1615b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
1625b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
1635b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
1645b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
1655b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
1665b81b6b3SRodney W. Grimes \***********************************************************************/
167b99f0a4aSAndrew Moore 
1683a2f7427SDavid Greenman #if NFT > 0
1693a2f7427SDavid Greenman int ftopen(dev_t, int);
1703a2f7427SDavid Greenman int ftintr(ftu_t ftu);
1713a2f7427SDavid Greenman int ftclose(dev_t, int);
1723a2f7427SDavid Greenman void ftstrategy(struct buf *);
1733a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *);
1743a2f7427SDavid Greenman int ftdump(dev_t);
1753a2f7427SDavid Greenman int ftsize(dev_t);
1763a2f7427SDavid Greenman int ftattach(struct isa_device *, struct isa_device *);
1773a2f7427SDavid Greenman #endif
1785b81b6b3SRodney W. Grimes 
1793a2f7427SDavid Greenman /* autoconfig functions */
1803a2f7427SDavid Greenman static int fdprobe(struct isa_device *);
1813a2f7427SDavid Greenman static int fdattach(struct isa_device *);
1823a2f7427SDavid Greenman 
1833a2f7427SDavid Greenman /* exported functions */
1843a2f7427SDavid Greenman int fdsize (dev_t);
1853a2f7427SDavid Greenman void fdintr(fdcu_t);
1863a2f7427SDavid Greenman int Fdopen(dev_t, int);
1873a2f7427SDavid Greenman int fdclose(dev_t, int);
1883a2f7427SDavid Greenman void fdstrategy(struct buf *);
1893a2f7427SDavid Greenman int fdioctl(dev_t, int, caddr_t, int, struct proc *);
1903a2f7427SDavid Greenman 
1913a2f7427SDavid Greenman /* needed for ft driver, thus exported */
1923a2f7427SDavid Greenman int in_fdc(fdcu_t);
1933a2f7427SDavid Greenman int out_fdc(fdcu_t, int);
1943a2f7427SDavid Greenman 
1953a2f7427SDavid Greenman /* internal functions */
1963a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int);
1973a2f7427SDavid Greenman #  define TURNON 1
1983a2f7427SDavid Greenman #  define TURNOFF 0
1993a2f7427SDavid Greenman static timeout_t fd_turnoff;
2003a2f7427SDavid Greenman static timeout_t fd_motor_on;
2013a2f7427SDavid Greenman static void fd_turnon(fdu_t);
2023a2f7427SDavid Greenman static void fdc_reset(fdc_p);
2033a2f7427SDavid Greenman static void fdstart(fdcu_t);
2043a2f7427SDavid Greenman static timeout_t fd_timeout;
2053a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2063a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p);
207aaf08d94SGarrett Wollman static int retrier(fdcu_t);
2083a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2093a2f7427SDavid Greenman 
210aaf08d94SGarrett Wollman 
2115b81b6b3SRodney W. Grimes #define DEVIDLE		0
2125b81b6b3SRodney W. Grimes #define FINDWORK	1
2135b81b6b3SRodney W. Grimes #define	DOSEEK		2
2145b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
2155b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
2165b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
2175b81b6b3SRodney W. Grimes #define	STARTRECAL	6
2185b81b6b3SRodney W. Grimes #define	RESETCTLR	7
2195b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
2205b81b6b3SRodney W. Grimes #define	RECALWAIT	9
2215b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
2225b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
2235b81b6b3SRodney W. Grimes 
2245b81b6b3SRodney W. Grimes #ifdef	DEBUG
2255b81b6b3SRodney W. Grimes char *fdstates[] =
2265b81b6b3SRodney W. Grimes {
2275b81b6b3SRodney W. Grimes "DEVIDLE",
2285b81b6b3SRodney W. Grimes "FINDWORK",
2295b81b6b3SRodney W. Grimes "DOSEEK",
2305b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2315b81b6b3SRodney W. Grimes "IOCOMPLETE",
2325b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2335b81b6b3SRodney W. Grimes "STARTRECAL",
2345b81b6b3SRodney W. Grimes "RESETCTLR",
2355b81b6b3SRodney W. Grimes "SEEKWAIT",
2365b81b6b3SRodney W. Grimes "RECALWAIT",
2375b81b6b3SRodney W. Grimes "MOTORWAIT",
2385b81b6b3SRodney W. Grimes "IOTIMEDOUT"
2395b81b6b3SRodney W. Grimes };
2405b81b6b3SRodney W. Grimes 
2413a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
2423a2f7427SDavid Greenman int	fd_debug = 0;
2435b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2445b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
245381fe1aaSGarrett Wollman #else /* DEBUG */
2465b81b6b3SRodney W. Grimes #define TRACE0(arg)
2475b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
248381fe1aaSGarrett Wollman #endif /* DEBUG */
2495b81b6b3SRodney W. Grimes 
2505b81b6b3SRodney W. Grimes /****************************************************************************/
2515b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
2525b81b6b3SRodney W. Grimes /****************************************************************************/
253b99f0a4aSAndrew Moore struct	isa_driver fdcdriver = {
254b99f0a4aSAndrew Moore 	fdprobe, fdattach, "fdc",
2555b81b6b3SRodney W. Grimes };
2565b81b6b3SRodney W. Grimes 
2575b81b6b3SRodney W. Grimes /*
2585b81b6b3SRodney W. Grimes  * probe for existance of controller
2595b81b6b3SRodney W. Grimes  */
2603a2f7427SDavid Greenman static int
2615b81b6b3SRodney W. Grimes fdprobe(dev)
2625b81b6b3SRodney W. Grimes 	struct isa_device *dev;
2635b81b6b3SRodney W. Grimes {
2645b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
2655b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].flags & FDC_ATTACHED)
2665b81b6b3SRodney W. Grimes 	{
2675b81b6b3SRodney W. Grimes 		printf("fdc: same unit (%d) used multiple times\n", fdcu);
2685b81b6b3SRodney W. Grimes 		return 0;
2695b81b6b3SRodney W. Grimes 	}
2705b81b6b3SRodney W. Grimes 
2715b81b6b3SRodney W. Grimes 	fdc_data[fdcu].baseport = dev->id_iobase;
2725b81b6b3SRodney W. Grimes 
27316111cedSAndrew Moore 	/* First - lets reset the floppy controller */
2743a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, 0);
27516111cedSAndrew Moore 	DELAY(100);
2763a2f7427SDavid Greenman 	outb(dev->id_iobase+FDOUT, FDO_FRST);
27716111cedSAndrew Moore 
2785b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
2795b81b6b3SRodney W. Grimes 	if (out_fdc(fdcu, NE7CMD_SPECIFY) < 0)
2805b81b6b3SRodney W. Grimes 	{
2815b81b6b3SRodney W. Grimes 		return(0);
2825b81b6b3SRodney W. Grimes 	}
2833a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_1(3, 240));
2843a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_2(2, 0));
2855b81b6b3SRodney W. Grimes 	return (IO_FDCSIZE);
2865b81b6b3SRodney W. Grimes }
2875b81b6b3SRodney W. Grimes 
2885b81b6b3SRodney W. Grimes /*
2895b81b6b3SRodney W. Grimes  * wire controller into system, look for floppy units
2905b81b6b3SRodney W. Grimes  */
2913a2f7427SDavid Greenman static int
2925b81b6b3SRodney W. Grimes fdattach(dev)
2935b81b6b3SRodney W. Grimes 	struct isa_device *dev;
2945b81b6b3SRodney W. Grimes {
2953a2f7427SDavid Greenman 	unsigned fdt;
2965b81b6b3SRodney W. Grimes 	fdu_t	fdu;
2975b81b6b3SRodney W. Grimes 	fdcu_t	fdcu = dev->id_unit;
2985b81b6b3SRodney W. Grimes 	fdc_p	fdc = fdc_data + fdcu;
2995b81b6b3SRodney W. Grimes 	fd_p	fd;
3006b7bd95bSJoerg Wunsch 	int	fdsu, st0, i;
301b99f0a4aSAndrew Moore 	struct isa_device *fdup;
3025b81b6b3SRodney W. Grimes 
3035b81b6b3SRodney W. Grimes 	fdc->fdcu = fdcu;
3045b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
3055b81b6b3SRodney W. Grimes 	fdc->dmachan = dev->id_drq;
3065b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
3073a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
3083a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, ((fdc->fdout = 0)));
309b99f0a4aSAndrew Moore 	printf("fdc%d:", fdcu);
3105b81b6b3SRodney W. Grimes 
3115b81b6b3SRodney W. Grimes 	/* check for each floppy drive */
312b99f0a4aSAndrew Moore 	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
313b99f0a4aSAndrew Moore 		if (fdup->id_iobase != dev->id_iobase)
314b99f0a4aSAndrew Moore 			continue;
315b99f0a4aSAndrew Moore 		fdu = fdup->id_unit;
316b99f0a4aSAndrew Moore 		fd = &fd_data[fdu];
317b99f0a4aSAndrew Moore 		if (fdu >= (NFD+NFT))
318b99f0a4aSAndrew Moore 			continue;
319b99f0a4aSAndrew Moore 		fdsu = fdup->id_physid;
320b99f0a4aSAndrew Moore 				/* look up what bios thinks we have */
321b99f0a4aSAndrew Moore 		switch (fdu) {
322b99f0a4aSAndrew Moore 			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
323b99f0a4aSAndrew Moore 				break;
324b99f0a4aSAndrew Moore 			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
325b99f0a4aSAndrew Moore 				break;
326b99f0a4aSAndrew Moore 			default: fdt = RTCFDT_NONE;
327b99f0a4aSAndrew Moore 				break;
328b99f0a4aSAndrew Moore 		}
3295b81b6b3SRodney W. Grimes 		/* is there a unit? */
330b99f0a4aSAndrew Moore 		if ((fdt == RTCFDT_NONE)
331b99f0a4aSAndrew Moore #if NFT > 0
332b99f0a4aSAndrew Moore 		|| (fdsu >= DRVS_PER_CTLR)) {
333b99f0a4aSAndrew Moore #else
334b99f0a4aSAndrew Moore 		) {
33556ef0285SAndrew Moore 			fd->type = NO_TYPE;
336b99f0a4aSAndrew Moore #endif
337b99f0a4aSAndrew Moore #if NFT > 0
338b99f0a4aSAndrew Moore 				/* If BIOS says no floppy, or > 2nd device */
339b99f0a4aSAndrew Moore 				/* Probe for and attach a floppy tape.     */
340b99f0a4aSAndrew Moore 			if (ftattach(dev, fdup))
341b99f0a4aSAndrew Moore 				continue;
34256ef0285SAndrew Moore 			if (fdsu < DRVS_PER_CTLR)
343b99f0a4aSAndrew Moore 				fd->type = NO_TYPE;
34456ef0285SAndrew Moore #endif
3455b81b6b3SRodney W. Grimes 			continue;
346f5f7ba03SJordan K. Hubbard 		}
3475b81b6b3SRodney W. Grimes 
3485b81b6b3SRodney W. Grimes 		/* select it */
3493a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNON);
3506b7bd95bSJoerg Wunsch 		DELAY(1000000);	/* 1 sec */
3516b7bd95bSJoerg Wunsch 		out_fdc(fdcu, NE7CMD_SENSED);
3526b7bd95bSJoerg Wunsch 		out_fdc(fdcu, fdsu);
3536b7bd95bSJoerg Wunsch 		if(in_fdc(fdcu) & NE7_ST3_T0) {
3546b7bd95bSJoerg Wunsch 			/* if at track 0, first seek inwards */
3553a2f7427SDavid Greenman 			out_fdc(fdcu, NE7CMD_SEEK); /* seek some steps... */
3565b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fdsu);
3573a2f7427SDavid Greenman 			out_fdc(fdcu, 10);
3586b7bd95bSJoerg Wunsch 			DELAY(300000); /* ...wait a moment... */
3596b7bd95bSJoerg Wunsch 			out_fdc(fdcu, NE7CMD_SENSEI); /* make ctrlr happy */
3603a2f7427SDavid Greenman 			(void)in_fdc(fdcu);
3613a2f7427SDavid Greenman 			(void)in_fdc(fdcu);
3626b7bd95bSJoerg Wunsch 		}
3636b7bd95bSJoerg Wunsch 		for(i = 0; i < 2; i++) {
3646b7bd95bSJoerg Wunsch 			/*
3656b7bd95bSJoerg Wunsch 			 * we must recalibrate twice, just in case the
3666b7bd95bSJoerg Wunsch 			 * heads have been beyond cylinder 76, since most
3676b7bd95bSJoerg Wunsch 			 * FDCs still barf when attempting to recalibrate
3686b7bd95bSJoerg Wunsch 			 * more than 77 steps
3696b7bd95bSJoerg Wunsch 			 */
3706b7bd95bSJoerg Wunsch 			out_fdc(fdcu, NE7CMD_RECAL); /* go back to 0 */
3713a2f7427SDavid Greenman 			out_fdc(fdcu, fdsu);
3726b7bd95bSJoerg Wunsch 			/* a second being enough for full stroke seek */
3736b7bd95bSJoerg Wunsch 			DELAY(i == 0? 1000000: 300000);
3745b81b6b3SRodney W. Grimes 
3756b7bd95bSJoerg Wunsch 			/* anything responding? */
3765b81b6b3SRodney W. Grimes 			out_fdc(fdcu, NE7CMD_SENSEI);
3775b81b6b3SRodney W. Grimes 			st0 = in_fdc(fdcu);
3783a2f7427SDavid Greenman 			(void)in_fdc(fdcu);
3796b7bd95bSJoerg Wunsch 
3806b7bd95bSJoerg Wunsch 			if ((st0 & NE7_ST0_EC) == 0)
3816b7bd95bSJoerg Wunsch 				break; /* already probed succesfully */
3826b7bd95bSJoerg Wunsch 		}
3836b7bd95bSJoerg Wunsch 
3843a2f7427SDavid Greenman 		set_motor(fdcu, fdsu, TURNOFF);
3853a2f7427SDavid Greenman 
3863a2f7427SDavid Greenman 		if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
3875b81b6b3SRodney W. Grimes 			continue;
3885b81b6b3SRodney W. Grimes 
389b99f0a4aSAndrew Moore 		fd->track = -2;
390b99f0a4aSAndrew Moore 		fd->fdc = fdc;
391b99f0a4aSAndrew Moore 		fd->fdsu = fdsu;
3923a2f7427SDavid Greenman 		fd->options = 0;
393b99f0a4aSAndrew Moore 		printf(" [%d: fd%d: ", fdsu, fdu);
3945b81b6b3SRodney W. Grimes 
395b99f0a4aSAndrew Moore 		switch (fdt) {
3967ca0641bSAndrey A. Chernov 		case RTCFDT_12M:
397b99f0a4aSAndrew Moore 			printf("1.2MB 5.25in]");
398b99f0a4aSAndrew Moore 			fd->type = FD_1200;
3997ca0641bSAndrey A. Chernov 			break;
4007ca0641bSAndrey A. Chernov 		case RTCFDT_144M:
401b99f0a4aSAndrew Moore 			printf("1.44MB 3.5in]");
402b99f0a4aSAndrew Moore 			fd->type = FD_1440;
4037ca0641bSAndrey A. Chernov 			break;
4047ca0641bSAndrey A. Chernov 		case RTCFDT_360K:
405b99f0a4aSAndrew Moore 			printf("360KB 5.25in]");
406b99f0a4aSAndrew Moore 			fd->type = FD_360;
4077ca0641bSAndrey A. Chernov 			break;
408ed2fa05eSAndrey A. Chernov 		case RTCFDT_720K:
409b99f0a4aSAndrew Moore 			printf("720KB 3.5in]");
410b99f0a4aSAndrew Moore 			fd->type = FD_720;
411ed2fa05eSAndrey A. Chernov 			break;
4127ca0641bSAndrey A. Chernov 		default:
413b99f0a4aSAndrew Moore 			printf("unknown]");
414b99f0a4aSAndrew Moore 			fd->type = NO_TYPE;
4157ca0641bSAndrey A. Chernov 			break;
4165b81b6b3SRodney W. Grimes 		}
4175b81b6b3SRodney W. Grimes 	}
418b99f0a4aSAndrew Moore 	printf("\n");
4195b81b6b3SRodney W. Grimes 
4203a2f7427SDavid Greenman 	return (1);
4215b81b6b3SRodney W. Grimes }
4225b81b6b3SRodney W. Grimes 
4235b81b6b3SRodney W. Grimes int
4245b81b6b3SRodney W. Grimes fdsize(dev)
4255b81b6b3SRodney W. Grimes 	dev_t	dev;
4265b81b6b3SRodney W. Grimes {
4275b81b6b3SRodney W. Grimes 	return(0);
4285b81b6b3SRodney W. Grimes }
4295b81b6b3SRodney W. Grimes 
4305b81b6b3SRodney W. Grimes /****************************************************************************/
4315b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
4325b81b6b3SRodney W. Grimes /*		remember to not deselect the drive we're working on         */
4335b81b6b3SRodney W. Grimes /****************************************************************************/
4343a2f7427SDavid Greenman static void
4353a2f7427SDavid Greenman set_motor(fdcu, fdsu, turnon)
436f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
4373a2f7427SDavid Greenman 	int fdsu;
4383a2f7427SDavid Greenman 	int turnon;
4395b81b6b3SRodney W. Grimes {
4403a2f7427SDavid Greenman 	int fdout = fdc_data[fdcu].fdout;
4413a2f7427SDavid Greenman 	int needspecify = 0;
4423a2f7427SDavid Greenman 
4433a2f7427SDavid Greenman 	if(turnon) {
4443a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
4453a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
4463a2f7427SDavid Greenman 	} else
4473a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
4483a2f7427SDavid Greenman 
4493a2f7427SDavid Greenman 	if(!turnon
4503a2f7427SDavid Greenman 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
4513a2f7427SDavid Greenman 		/* gonna turn off the last drive, put FDC to bed */
4523a2f7427SDavid Greenman 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
4533a2f7427SDavid Greenman 	else {
4543a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
4553a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
4563a2f7427SDavid Greenman 			needspecify = 1;
4573a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
4585b81b6b3SRodney W. Grimes 	}
4595b81b6b3SRodney W. Grimes 
4603a2f7427SDavid Greenman 	outb(fdc_data[fdcu].baseport+FDOUT, fdout);
4613a2f7427SDavid Greenman 	fdc_data[fdcu].fdout = fdout;
4623a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
4633a2f7427SDavid Greenman 
4643a2f7427SDavid Greenman 	if(needspecify) {
4653a2f7427SDavid Greenman 		out_fdc(fdcu, NE7CMD_SPECIFY);
4663a2f7427SDavid Greenman 		out_fdc(fdcu, NE7_SPEC_1(3, 240));
4673a2f7427SDavid Greenman 		out_fdc(fdcu, NE7_SPEC_2(2, 0));
4683a2f7427SDavid Greenman 	}
4693a2f7427SDavid Greenman }
4703a2f7427SDavid Greenman 
4713a2f7427SDavid Greenman /* ARGSUSED */
472381fe1aaSGarrett Wollman static void
473d0917939SPaul Richards fd_turnoff(void *arg1)
4745b81b6b3SRodney W. Grimes {
475381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
476f5f7ba03SJordan K. Hubbard 	int	s;
477f5f7ba03SJordan K. Hubbard 
4785b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
479f5f7ba03SJordan K. Hubbard 	s = splbio();
4805b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
4813a2f7427SDavid Greenman 	set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF);
482f5f7ba03SJordan K. Hubbard 	splx(s);
4835b81b6b3SRodney W. Grimes }
4845b81b6b3SRodney W. Grimes 
4853a2f7427SDavid Greenman /* ARGSUSED */
4863a2f7427SDavid Greenman static void
487d0917939SPaul Richards fd_motor_on(void *arg1)
4885b81b6b3SRodney W. Grimes {
489381fe1aaSGarrett Wollman 	fdu_t fdu = (fdu_t)arg1;
490f5f7ba03SJordan K. Hubbard 	int	s;
491f5f7ba03SJordan K. Hubbard 
4925b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
493f5f7ba03SJordan K. Hubbard 	s = splbio();
4945b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
4955b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
4965b81b6b3SRodney W. Grimes 	{
497f5f7ba03SJordan K. Hubbard 		fdintr(fd->fdc->fdcu);
4985b81b6b3SRodney W. Grimes 	}
499f5f7ba03SJordan K. Hubbard 	splx(s);
5005b81b6b3SRodney W. Grimes }
5015b81b6b3SRodney W. Grimes 
5023a2f7427SDavid Greenman static void
503f5f7ba03SJordan K. Hubbard fd_turnon(fdu)
504f5f7ba03SJordan K. Hubbard 	fdu_t fdu;
5055b81b6b3SRodney W. Grimes {
5065b81b6b3SRodney W. Grimes 	fd_p fd = fd_data + fdu;
5075b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
5085b81b6b3SRodney W. Grimes 	{
5093a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
5103a2f7427SDavid Greenman 		set_motor(fd->fdc->fdcu, fd->fdsu, TURNON);
511d0917939SPaul Richards 		timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
5125b81b6b3SRodney W. Grimes 	}
5135b81b6b3SRodney W. Grimes }
5145b81b6b3SRodney W. Grimes 
515381fe1aaSGarrett Wollman static void
5163a2f7427SDavid Greenman fdc_reset(fdc)
5173a2f7427SDavid Greenman 	fdc_p fdc;
5185b81b6b3SRodney W. Grimes {
5193a2f7427SDavid Greenman 	fdcu_t fdcu = fdc->fdcu;
5203a2f7427SDavid Greenman 
5213a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
5223a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
5233a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
5243a2f7427SDavid Greenman 	DELAY(100);
5253a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
5263a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN);
5273a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
5283a2f7427SDavid Greenman 	DELAY(100);
5293a2f7427SDavid Greenman 	outb(fdc->baseport + FDOUT, fdc->fdout);
5303a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
5313a2f7427SDavid Greenman 
5323a2f7427SDavid Greenman 	out_fdc(fdcu, NE7CMD_SPECIFY);
5333a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_1(3, 240));
5343a2f7427SDavid Greenman 	out_fdc(fdcu, NE7_SPEC_2(2, 0));
5355b81b6b3SRodney W. Grimes }
5365b81b6b3SRodney W. Grimes 
5375b81b6b3SRodney W. Grimes /****************************************************************************/
5385b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
5395b81b6b3SRodney W. Grimes /****************************************************************************/
5405b81b6b3SRodney W. Grimes int
541f5f7ba03SJordan K. Hubbard in_fdc(fdcu)
542f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
5435b81b6b3SRodney W. Grimes {
5445b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
5455b81b6b3SRodney W. Grimes 	int i, j = 100000;
5463a2f7427SDavid Greenman 	while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM))
5475b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
5485b81b6b3SRodney W. Grimes 		if (i == NE7_RQM) return -1;
5495b81b6b3SRodney W. Grimes 	if (j <= 0)
5505b81b6b3SRodney W. Grimes 		return(-1);
5515b81b6b3SRodney W. Grimes #ifdef	DEBUG
5523a2f7427SDavid Greenman 	i = inb(baseport+FDDATA);
5533a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
5545b81b6b3SRodney W. Grimes 	return(i);
5555b81b6b3SRodney W. Grimes #else
5563a2f7427SDavid Greenman 	return inb(baseport+FDDATA);
5575b81b6b3SRodney W. Grimes #endif
5585b81b6b3SRodney W. Grimes }
5595b81b6b3SRodney W. Grimes 
560381fe1aaSGarrett Wollman int
561f5f7ba03SJordan K. Hubbard out_fdc(fdcu, x)
562f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
563f5f7ba03SJordan K. Hubbard 	int x;
5645b81b6b3SRodney W. Grimes {
5655b81b6b3SRodney W. Grimes 	int baseport = fdc_data[fdcu].baseport;
5663b3837dbSRodney W. Grimes 	int i;
5675b81b6b3SRodney W. Grimes 
5683b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
5693b3837dbSRodney W. Grimes 	i = 100000;
5703a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0);
5713b3837dbSRodney W. Grimes 	if (i <= 0) return (-1);	/* Floppy timed out */
5723b3837dbSRodney W. Grimes 
5733b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
5743b3837dbSRodney W. Grimes 	i = 100000;
5753a2f7427SDavid Greenman 	while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0);
5763b3837dbSRodney W. Grimes 	if (i <= 0) return (-1);	/* Floppy timed out */
5773b3837dbSRodney W. Grimes 
5783b3837dbSRodney W. Grimes 	/* Send the command and return */
5793a2f7427SDavid Greenman 	outb(baseport+FDDATA, x);
5803a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
5815b81b6b3SRodney W. Grimes 	return (0);
5825b81b6b3SRodney W. Grimes }
5835b81b6b3SRodney W. Grimes 
5845b81b6b3SRodney W. Grimes /****************************************************************************/
5855b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
5865b81b6b3SRodney W. Grimes /****************************************************************************/
587381fe1aaSGarrett Wollman int
5885b81b6b3SRodney W. Grimes Fdopen(dev, flags)
5895b81b6b3SRodney W. Grimes 	dev_t	dev;
5905b81b6b3SRodney W. Grimes 	int	flags;
5915b81b6b3SRodney W. Grimes {
5925b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
59320a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
594b99f0a4aSAndrew Moore 	fdc_p	fdc;
5955b81b6b3SRodney W. Grimes 
596b99f0a4aSAndrew Moore #if NFT > 0
597b99f0a4aSAndrew Moore 	/* check for a tape open */
598b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
599b99f0a4aSAndrew Moore 		return(ftopen(dev, flags));
600b99f0a4aSAndrew Moore #endif
6015b81b6b3SRodney W. Grimes 	/* check bounds */
602b99f0a4aSAndrew Moore 	if (fdu >= NFD)
603b99f0a4aSAndrew Moore 		return(ENXIO);
604b99f0a4aSAndrew Moore 	fdc = fd_data[fdu].fdc;
605b99f0a4aSAndrew Moore 	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
606b99f0a4aSAndrew Moore 		return(ENXIO);
607b99f0a4aSAndrew Moore 	if (type > NUMDENS)
608b99f0a4aSAndrew Moore 		return(ENXIO);
6097ca0641bSAndrey A. Chernov 	if (type == 0)
6107ca0641bSAndrey A. Chernov 		type = fd_data[fdu].type;
6117ca0641bSAndrey A. Chernov 	else {
6127ca0641bSAndrey A. Chernov 		if (type != fd_data[fdu].type) {
613fa4700b4SAndrey A. Chernov 			switch (fd_data[fdu].type) {
6147ca0641bSAndrey A. Chernov 			case FD_360:
6157ca0641bSAndrey A. Chernov 				return(ENXIO);
616ed2fa05eSAndrey A. Chernov 			case FD_720:
617b39c878eSAndrey A. Chernov 				if (   type != FD_820
618b39c878eSAndrey A. Chernov 				    && type != FD_800
619ed2fa05eSAndrey A. Chernov 				   )
620ed2fa05eSAndrey A. Chernov 					return(ENXIO);
621ed2fa05eSAndrey A. Chernov 				break;
6227ca0641bSAndrey A. Chernov 			case FD_1200:
623b39c878eSAndrey A. Chernov 				switch (type) {
624b39c878eSAndrey A. Chernov 				case FD_1480:
625b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
626fa4700b4SAndrey A. Chernov 					break;
6277ca0641bSAndrey A. Chernov 				case FD_1440:
628b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
629b39c878eSAndrey A. Chernov 					break;
630b39c878eSAndrey A. Chernov 				case FD_820:
631b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
632b39c878eSAndrey A. Chernov 					break;
633b39c878eSAndrey A. Chernov 				case FD_800:
634b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
635b39c878eSAndrey A. Chernov 					break;
636b39c878eSAndrey A. Chernov 				case FD_720:
637b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
638b39c878eSAndrey A. Chernov 					break;
639b39c878eSAndrey A. Chernov 				case FD_360:
640b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
641b39c878eSAndrey A. Chernov 					break;
642b39c878eSAndrey A. Chernov 				default:
643b39c878eSAndrey A. Chernov 					return(ENXIO);
644b39c878eSAndrey A. Chernov 				}
645b39c878eSAndrey A. Chernov 				break;
646b39c878eSAndrey A. Chernov 			case FD_1440:
647b39c878eSAndrey A. Chernov 				if (   type != FD_1720
648b39c878eSAndrey A. Chernov 				    && type != FD_1480
649ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
650b39c878eSAndrey A. Chernov 				    && type != FD_820
651b39c878eSAndrey A. Chernov 				    && type != FD_800
652b39c878eSAndrey A. Chernov 				    && type != FD_720
6537ca0641bSAndrey A. Chernov 				    )
654dffff499SAndrey A. Chernov 					return(ENXIO);
655fa4700b4SAndrey A. Chernov 				break;
6567ca0641bSAndrey A. Chernov 			}
6577ca0641bSAndrey A. Chernov 		}
658fa4700b4SAndrey A. Chernov 	}
659b99f0a4aSAndrew Moore 	fd_data[fdu].ft = fd_types + type - 1;
6605b81b6b3SRodney W. Grimes 	fd_data[fdu].flags |= FD_OPEN;
6615b81b6b3SRodney W. Grimes 
6625b81b6b3SRodney W. Grimes 	return 0;
6635b81b6b3SRodney W. Grimes }
6645b81b6b3SRodney W. Grimes 
665381fe1aaSGarrett Wollman int
6665b81b6b3SRodney W. Grimes fdclose(dev, flags)
6675b81b6b3SRodney W. Grimes 	dev_t dev;
668381fe1aaSGarrett Wollman 	int flags;
6695b81b6b3SRodney W. Grimes {
6705b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
671b99f0a4aSAndrew Moore 
672b99f0a4aSAndrew Moore #if NFT > 0
6733a2f7427SDavid Greenman 	int type = FDTYPE(minor(dev));
6743a2f7427SDavid Greenman 
675b99f0a4aSAndrew Moore 	if (type & F_TAPE_TYPE)
6763a2f7427SDavid Greenman 		return ftclose(dev, flags);
677b99f0a4aSAndrew Moore #endif
6785b81b6b3SRodney W. Grimes 	fd_data[fdu].flags &= ~FD_OPEN;
6793a2f7427SDavid Greenman 	fd_data[fdu].options &= ~FDOPT_NORETRY;
6805b81b6b3SRodney W. Grimes 	return(0);
6815b81b6b3SRodney W. Grimes }
6825b81b6b3SRodney W. Grimes 
6835b81b6b3SRodney W. Grimes 
6843a2f7427SDavid Greenman /****************************************************************************/
6853a2f7427SDavid Greenman /*                               fdstrategy                                 */
6863a2f7427SDavid Greenman /****************************************************************************/
6873a2f7427SDavid Greenman void
6883a2f7427SDavid Greenman fdstrategy(struct buf *bp)
6893a2f7427SDavid Greenman {
6903a2f7427SDavid Greenman 	register struct buf *dp;
6913a2f7427SDavid Greenman 	long nblocks, blknum;
6923a2f7427SDavid Greenman  	int	s;
6933a2f7427SDavid Greenman  	fdcu_t	fdcu;
6943a2f7427SDavid Greenman  	fdu_t	fdu;
6953a2f7427SDavid Greenman  	fdc_p	fdc;
6963a2f7427SDavid Greenman  	fd_p	fd;
6973a2f7427SDavid Greenman 	size_t	fdblk;
6983a2f7427SDavid Greenman 
6993a2f7427SDavid Greenman  	fdu = FDUNIT(minor(bp->b_dev));
7003a2f7427SDavid Greenman 	fd = &fd_data[fdu];
7013a2f7427SDavid Greenman 	fdc = fd->fdc;
7023a2f7427SDavid Greenman 	fdcu = fdc->fdcu;
7033a2f7427SDavid Greenman 	fdblk = 128 << (fd->ft->secsize);
7043a2f7427SDavid Greenman 
7053a2f7427SDavid Greenman #if NFT > 0
7063a2f7427SDavid Greenman 	if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) {
7073a2f7427SDavid Greenman 		/* ft tapes do not (yet) support strategy i/o */
7083a2f7427SDavid Greenman 		bp->b_error = ENXIO;
7093a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
7103a2f7427SDavid Greenman 		goto bad;
7113a2f7427SDavid Greenman 	}
7123a2f7427SDavid Greenman 	/* check for controller already busy with tape */
7133a2f7427SDavid Greenman 	if (fdc->flags & FDC_TAPE_BUSY) {
7143a2f7427SDavid Greenman 		bp->b_error = EBUSY;
7153a2f7427SDavid Greenman 		bp->b_flags |= B_ERROR;
7163a2f7427SDavid Greenman 		goto bad;
7173a2f7427SDavid Greenman 	}
7183a2f7427SDavid Greenman #endif
7193a2f7427SDavid Greenman 	if (!(bp->b_flags & B_FORMAT)) {
7203a2f7427SDavid Greenman 		if ((fdu >= NFD) || (bp->b_blkno < 0)) {
7213a2f7427SDavid Greenman 			printf("fdstrat: fdu = %d, blkno = %d, bcount = %d\n",
7223a2f7427SDavid Greenman 			       fdu, bp->b_blkno, bp->b_bcount);
7233a2f7427SDavid Greenman 			bp->b_error = EINVAL;
7243a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
7253a2f7427SDavid Greenman 			goto bad;
7263a2f7427SDavid Greenman 		}
7273a2f7427SDavid Greenman 		if ((bp->b_bcount % fdblk) != 0) {
7283a2f7427SDavid Greenman 			bp->b_error = EINVAL;
7293a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
7303a2f7427SDavid Greenman 			goto bad;
7313a2f7427SDavid Greenman 		}
7323a2f7427SDavid Greenman 	}
7333a2f7427SDavid Greenman 
7343a2f7427SDavid Greenman 	/*
7353a2f7427SDavid Greenman 	 * Set up block calculations.
7363a2f7427SDavid Greenman 	 */
7373a2f7427SDavid Greenman 	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk;
7383a2f7427SDavid Greenman  	nblocks = fd->ft->size;
7393a2f7427SDavid Greenman 	if (blknum + (bp->b_bcount / fdblk) > nblocks) {
7403a2f7427SDavid Greenman 		if (blknum == nblocks) {
7413a2f7427SDavid Greenman 			bp->b_resid = bp->b_bcount;
7423a2f7427SDavid Greenman 		} else {
7433a2f7427SDavid Greenman 			bp->b_error = ENOSPC;
7443a2f7427SDavid Greenman 			bp->b_flags |= B_ERROR;
7453a2f7427SDavid Greenman 		}
7463a2f7427SDavid Greenman 		goto bad;
7473a2f7427SDavid Greenman 	}
7483a2f7427SDavid Greenman  	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
7493a2f7427SDavid Greenman 	dp = &(fdc->head);
7503a2f7427SDavid Greenman 	s = splbio();
7513a2f7427SDavid Greenman 	disksort(dp, bp);
7523a2f7427SDavid Greenman 	untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */
7533a2f7427SDavid Greenman 	fdstart(fdcu);
7543a2f7427SDavid Greenman 	splx(s);
7553a2f7427SDavid Greenman 	return;
7563a2f7427SDavid Greenman 
7573a2f7427SDavid Greenman bad:
7583a2f7427SDavid Greenman 	biodone(bp);
7593a2f7427SDavid Greenman }
7603a2f7427SDavid Greenman 
7615b81b6b3SRodney W. Grimes /***************************************************************\
7625b81b6b3SRodney W. Grimes *				fdstart				*
7635b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
7645b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
7655b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
7665b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
7675b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
7685b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
7695b81b6b3SRodney W. Grimes \***************************************************************/
770381fe1aaSGarrett Wollman static void
771f5f7ba03SJordan K. Hubbard fdstart(fdcu)
772f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
7735b81b6b3SRodney W. Grimes {
7745b81b6b3SRodney W. Grimes 	int s;
7755b81b6b3SRodney W. Grimes 
7765b81b6b3SRodney W. Grimes 	s = splbio();
7775b81b6b3SRodney W. Grimes 	if(fdc_data[fdcu].state == DEVIDLE)
7785b81b6b3SRodney W. Grimes 	{
7795b81b6b3SRodney W. Grimes 		fdintr(fdcu);
7805b81b6b3SRodney W. Grimes 	}
7815b81b6b3SRodney W. Grimes 	splx(s);
7825b81b6b3SRodney W. Grimes }
7835b81b6b3SRodney W. Grimes 
7843a2f7427SDavid Greenman /* ARGSUSED */
785381fe1aaSGarrett Wollman static void
786d0917939SPaul Richards fd_timeout(void *arg1)
7875b81b6b3SRodney W. Grimes {
788381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
7895b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc_data[fdcu].fdu;
7903a2f7427SDavid Greenman 	int baseport = fdc_data[fdcu].baseport;
7915b81b6b3SRodney W. Grimes 	struct buf *dp, *bp;
792f5f7ba03SJordan K. Hubbard 	int s;
7935b81b6b3SRodney W. Grimes 
7945b81b6b3SRodney W. Grimes 	dp = &fdc_data[fdcu].head;
7955b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
7965b81b6b3SRodney W. Grimes 
7973a2f7427SDavid Greenman 	/*
7983a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
7993a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
8003a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
8013a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
8023a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
8033a2f7427SDavid Greenman 	 */
8045b81b6b3SRodney W. Grimes 
8053a2f7427SDavid Greenman 	s = splbio();
8063a2f7427SDavid Greenman 
8073a2f7427SDavid Greenman 	TRACE1("fd%d[fd_timeout()]", fdu);
8083a2f7427SDavid Greenman 	/* See if the controller is still busy (patiently awaiting data) */
8093a2f7427SDavid Greenman 	if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB)
8103a2f7427SDavid Greenman 	{
8113a2f7427SDavid Greenman 		TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS));
8123a2f7427SDavid Greenman 		/* yup, it is; kill it now */
8133a2f7427SDavid Greenman 		fdc_reset(&fdc_data[fdcu]);
8143a2f7427SDavid Greenman 		printf("fd%d: Operation timeout\n", fdu);
8153a2f7427SDavid Greenman 	}
8165b81b6b3SRodney W. Grimes 
8175b81b6b3SRodney W. Grimes 	if (bp)
8185b81b6b3SRodney W. Grimes 	{
8195b81b6b3SRodney W. Grimes 		retrier(fdcu);
8203a2f7427SDavid Greenman 		fdc_data[fdcu].status[0] = NE7_ST0_IC_RC;
8215b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = IOTIMEDOUT;
8225b81b6b3SRodney W. Grimes 		if( fdc_data[fdcu].retry < 6)
8235b81b6b3SRodney W. Grimes 			fdc_data[fdcu].retry = 6;
8245b81b6b3SRodney W. Grimes 	}
8255b81b6b3SRodney W. Grimes 	else
8265b81b6b3SRodney W. Grimes 	{
8275b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fd = (fd_p) 0;
8285b81b6b3SRodney W. Grimes 		fdc_data[fdcu].fdu = -1;
8295b81b6b3SRodney W. Grimes 		fdc_data[fdcu].state = DEVIDLE;
8305b81b6b3SRodney W. Grimes 	}
831f5f7ba03SJordan K. Hubbard 	fdintr(fdcu);
832f5f7ba03SJordan K. Hubbard 	splx(s);
8335b81b6b3SRodney W. Grimes }
8345b81b6b3SRodney W. Grimes 
8355b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
8363a2f7427SDavid Greenman /* ARGSUSED */
837381fe1aaSGarrett Wollman static void
838d0917939SPaul Richards fd_pseudointr(void *arg1)
8395b81b6b3SRodney W. Grimes {
840381fe1aaSGarrett Wollman 	fdcu_t fdcu = (fdcu_t)arg1;
8415b81b6b3SRodney W. Grimes 	int	s;
8423a2f7427SDavid Greenman 
8435b81b6b3SRodney W. Grimes 	s = splbio();
8445b81b6b3SRodney W. Grimes 	fdintr(fdcu);
8455b81b6b3SRodney W. Grimes 	splx(s);
8465b81b6b3SRodney W. Grimes }
8475b81b6b3SRodney W. Grimes 
8485b81b6b3SRodney W. Grimes /***********************************************************************\
8495b81b6b3SRodney W. Grimes *                                 fdintr				*
8505b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
8515b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
8525b81b6b3SRodney W. Grimes \***********************************************************************/
853381fe1aaSGarrett Wollman void
854381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu)
8555b81b6b3SRodney W. Grimes {
8565b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
857b99f0a4aSAndrew Moore #if NFT > 0
858b99f0a4aSAndrew Moore 	fdu_t fdu = fdc->fdu;
859b99f0a4aSAndrew Moore 
860b99f0a4aSAndrew Moore 	if (fdc->flags & FDC_TAPE_BUSY)
861b99f0a4aSAndrew Moore 		(ftintr(fdu));
862b99f0a4aSAndrew Moore 	else
863b99f0a4aSAndrew Moore #endif
864381fe1aaSGarrett Wollman 		while(fdstate(fdcu, fdc))
865381fe1aaSGarrett Wollman 			;
8665b81b6b3SRodney W. Grimes }
8675b81b6b3SRodney W. Grimes 
8685b81b6b3SRodney W. Grimes /***********************************************************************\
8695b81b6b3SRodney W. Grimes * The controller state machine.						*
8705b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
8715b81b6b3SRodney W. Grimes \***********************************************************************/
8723a2f7427SDavid Greenman static int
873381fe1aaSGarrett Wollman fdstate(fdcu, fdc)
874f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
875f5f7ba03SJordan K. Hubbard 	fdc_p fdc;
8765b81b6b3SRodney W. Grimes {
8773a2f7427SDavid Greenman 	int read, format, head, sec = 0, i = 0, sectrac, st0, cyl, st3;
8785b81b6b3SRodney W. Grimes 	unsigned long blknum;
8795b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
8805b81b6b3SRodney W. Grimes 	fd_p fd;
8815b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
882b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
8833a2f7427SDavid Greenman 	size_t fdblk;
8845b81b6b3SRodney W. Grimes 
8855b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
8865b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
8875b81b6b3SRodney W. Grimes 	if(!bp)
8885b81b6b3SRodney W. Grimes 	{
8895b81b6b3SRodney W. Grimes 		/***********************************************\
8905b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
8915b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
8925b81b6b3SRodney W. Grimes 		\***********************************************/
8935b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
8945b81b6b3SRodney W. Grimes 		if(fdc->fd)
8955b81b6b3SRodney W. Grimes 		{
8963a2f7427SDavid Greenman 			printf("unexpected valid fd pointer (fdu = %d)\n",
8973a2f7427SDavid Greenman 			       fdc->fdu);
8985b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
8995b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
9005b81b6b3SRodney W. Grimes 		}
9015b81b6b3SRodney W. Grimes 		TRACE1("[fdc%d IDLE]", fdcu);
9025b81b6b3SRodney W. Grimes  		return(0);
9035b81b6b3SRodney W. Grimes 	}
9045b81b6b3SRodney W. Grimes 	fdu = FDUNIT(minor(bp->b_dev));
9055b81b6b3SRodney W. Grimes 	fd = fd_data + fdu;
9063a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
9075b81b6b3SRodney W. Grimes 	if (fdc->fd && (fd != fdc->fd))
9085b81b6b3SRodney W. Grimes 	{
9095b81b6b3SRodney W. Grimes 		printf("confused fd pointers\n");
9105b81b6b3SRodney W. Grimes 	}
9115b81b6b3SRodney W. Grimes 	read = bp->b_flags & B_READ;
912b39c878eSAndrey A. Chernov 	format = bp->b_flags & B_FORMAT;
913b39c878eSAndrey A. Chernov 	if(format)
914b39c878eSAndrey A. Chernov 		finfo = (struct fd_formb *)bp->b_un.b_addr;
9155b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
9165b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
9175b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
918d0917939SPaul Richards 	untimeout(fd_turnoff, (caddr_t)fdu);
919d0917939SPaul Richards 	timeout(fd_turnoff, (caddr_t)fdu, 4 * hz);
9205b81b6b3SRodney W. Grimes 	switch (fdc->state)
9215b81b6b3SRodney W. Grimes 	{
9225b81b6b3SRodney W. Grimes 	case DEVIDLE:
9235b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
9245b81b6b3SRodney W. Grimes 		fdc->retry = 0;
9255b81b6b3SRodney W. Grimes 		fd->skip = 0;
9265b81b6b3SRodney W. Grimes 		fdc->fd = fd;
9275b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
9283a2f7427SDavid Greenman 		outb(fdc->baseport+FDCTL, fd->ft->trans);
9293a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
9305b81b6b3SRodney W. Grimes 		/*******************************************************\
9315b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
9325b81b6b3SRodney W. Grimes 		* it will start up in it's own good time		*
9335b81b6b3SRodney W. Grimes 		\*******************************************************/
9345b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
9355b81b6b3SRodney W. Grimes 		{
9365b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
9375b81b6b3SRodney W. Grimes 			return(0); /* come back later */
9385b81b6b3SRodney W. Grimes 		}
9395b81b6b3SRodney W. Grimes 		/*******************************************************\
9405b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
9415b81b6b3SRodney W. Grimes 		\*******************************************************/
9425b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
9435b81b6b3SRodney W. Grimes 		{
9445b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
9455b81b6b3SRodney W. Grimes 			fd_turnon(fdu);
9465b81b6b3SRodney W. Grimes 			return(0);
9475b81b6b3SRodney W. Grimes 		}
9485b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
9495b81b6b3SRodney W. Grimes 		{
9503a2f7427SDavid Greenman 			set_motor(fdcu, fd->fdsu, TURNON);
9515b81b6b3SRodney W. Grimes 		}
9525b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
9535b81b6b3SRodney W. Grimes 		break;
9545b81b6b3SRodney W. Grimes 	case DOSEEK:
9555b81b6b3SRodney W. Grimes 		if (bp->b_cylin == fd->track)
9565b81b6b3SRodney W. Grimes 		{
9575b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
9585b81b6b3SRodney W. Grimes 			break;
9595b81b6b3SRodney W. Grimes 		}
9605b81b6b3SRodney W. Grimes 		out_fdc(fdcu, NE7CMD_SEEK);	/* Seek function */
9615b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fd->fdsu);	/* Drive number */
9625b81b6b3SRodney W. Grimes 		out_fdc(fdcu, bp->b_cylin * fd->ft->steptrac);
9635b81b6b3SRodney W. Grimes 		fd->track = -2;
9645b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
9655b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
9665b81b6b3SRodney W. Grimes 	case SEEKWAIT:
9675b81b6b3SRodney W. Grimes 		/* allow heads to settle */
9683a2f7427SDavid Greenman 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 32);
9695b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
9705b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
9715b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
9725b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
9735b81b6b3SRodney W. Grimes 		if(fd->track == -2)
9745b81b6b3SRodney W. Grimes 		{
9755b81b6b3SRodney W. Grimes 			int descyl = bp->b_cylin * fd->ft->steptrac;
9763a2f7427SDavid Greenman 			do {
9775b81b6b3SRodney W. Grimes 				out_fdc(fdcu, NE7CMD_SENSEI);
9783a2f7427SDavid Greenman 				st0 = in_fdc(fdcu);
9795b81b6b3SRodney W. Grimes 				cyl = in_fdc(fdcu);
9803a2f7427SDavid Greenman 				/*
9813a2f7427SDavid Greenman 				 * if this was a "ready changed" interrupt,
9823a2f7427SDavid Greenman 				 * fetch status again (can happen after
9833a2f7427SDavid Greenman 				 * enabling controller from reset state)
9843a2f7427SDavid Greenman 				 */
9853a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
9863a2f7427SDavid Greenman 			if (0 == descyl)
9873a2f7427SDavid Greenman 			{
9883a2f7427SDavid Greenman 				/*
9893a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
9903a2f7427SDavid Greenman 				 * really there
9913a2f7427SDavid Greenman 				 */
9923a2f7427SDavid Greenman 				out_fdc(fdcu, NE7CMD_SENSED);
9933a2f7427SDavid Greenman 				out_fdc(fdcu, fdu);
9943a2f7427SDavid Greenman 				st3 = in_fdc(fdcu);
9953a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
9963a2f7427SDavid Greenman 					printf(
9973a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
9983a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
9993a2f7427SDavid Greenman 					if(fdc->retry < 3)
10003a2f7427SDavid Greenman 						fdc->retry = 3;
10013a2f7427SDavid Greenman 					return(retrier(fdcu));
10023a2f7427SDavid Greenman 				}
10033a2f7427SDavid Greenman 			}
10045b81b6b3SRodney W. Grimes 			if (cyl != descyl)
10055b81b6b3SRodney W. Grimes 			{
10063a2f7427SDavid Greenman 				printf(
10073a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
10083a2f7427SDavid Greenman 				       fdu, descyl, cyl, st0, NE7_ST0BITS);
10095b81b6b3SRodney W. Grimes 				return(retrier(fdcu));
10105b81b6b3SRodney W. Grimes 			}
10115b81b6b3SRodney W. Grimes 		}
10125b81b6b3SRodney W. Grimes 
10135b81b6b3SRodney W. Grimes 		fd->track = bp->b_cylin;
1014b39c878eSAndrey A. Chernov 		if(format)
1015b39c878eSAndrey A. Chernov 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1016b39c878eSAndrey A. Chernov 				- (char *)finfo;
10175b81b6b3SRodney W. Grimes 		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
10183a2f7427SDavid Greenman 			format ? bp->b_bcount : fdblk, fdc->dmachan);
10193a2f7427SDavid Greenman 		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
10203a2f7427SDavid Greenman 			+ fd->skip/fdblk;
10215b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
10225b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
10235b81b6b3SRodney W. Grimes 		head = sec / sectrac;
10245b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
10253a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
10263a2f7427SDavid Greenman 
10273a2f7427SDavid Greenman 		if(format || !read)
10283a2f7427SDavid Greenman 		{
10293a2f7427SDavid Greenman 			/* make sure the drive is writable */
10303a2f7427SDavid Greenman 			out_fdc(fdcu, NE7CMD_SENSED);
10313a2f7427SDavid Greenman 			out_fdc(fdcu, fdu);
10323a2f7427SDavid Greenman 			st3 = in_fdc(fdcu);
10333a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
10343a2f7427SDavid Greenman 			{
10353a2f7427SDavid Greenman 				/*
10363a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
10373a2f7427SDavid Greenman 				 * in order to force the current operation
10383a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
10393a2f7427SDavid Greenman 				 * error - all error handling is done
10403a2f7427SDavid Greenman 				 * by the retrier()
10413a2f7427SDavid Greenman 				 */
10423a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
10433a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
10443a2f7427SDavid Greenman 				fdc->status[2] = 0;
10453a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
10463a2f7427SDavid Greenman 				fdc->status[4] = head;
10473a2f7427SDavid Greenman 				fdc->status[5] = sec;
10483a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
10493a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
10503a2f7427SDavid Greenman 				return (1);
10513a2f7427SDavid Greenman 			}
10523a2f7427SDavid Greenman 		}
10535b81b6b3SRodney W. Grimes 
1054b39c878eSAndrey A. Chernov 		if(format)
1055b39c878eSAndrey A. Chernov 		{
1056b39c878eSAndrey A. Chernov 			/* formatting */
10573a2f7427SDavid Greenman 			out_fdc(fdcu, NE7CMD_FORMAT);
1058b39c878eSAndrey A. Chernov 			out_fdc(fdcu, head << 2 | fdu);
1059b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_secshift);
1060b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_nsecs);
1061b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_gaplen);
1062b39c878eSAndrey A. Chernov 			out_fdc(fdcu, finfo->fd_formb_fillbyte);
1063b39c878eSAndrey A. Chernov 		}
1064b39c878eSAndrey A. Chernov 		else
1065b39c878eSAndrey A. Chernov 		{
10665b81b6b3SRodney W. Grimes 			if (read)
10675b81b6b3SRodney W. Grimes 			{
10685b81b6b3SRodney W. Grimes 				out_fdc(fdcu, NE7CMD_READ);      /* READ */
10695b81b6b3SRodney W. Grimes 			}
10705b81b6b3SRodney W. Grimes 			else
10715b81b6b3SRodney W. Grimes 			{
10725b81b6b3SRodney W. Grimes 				out_fdc(fdcu, NE7CMD_WRITE);     /* WRITE */
10735b81b6b3SRodney W. Grimes 			}
10745b81b6b3SRodney W. Grimes 			out_fdc(fdcu, head << 2 | fdu);  /* head & unit */
10755b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->track);        /* track */
10765b81b6b3SRodney W. Grimes 			out_fdc(fdcu, head);
10775b81b6b3SRodney W. Grimes 			out_fdc(fdcu, sec);              /* sector XXX +1? */
10785b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->secsize);  /* sector size */
10795b81b6b3SRodney W. Grimes 			out_fdc(fdcu, sectrac);          /* sectors/track */
10805b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->gap);      /* gap size */
10815b81b6b3SRodney W. Grimes 			out_fdc(fdcu, fd->ft->datalen);  /* data length */
1082b39c878eSAndrey A. Chernov 		}
10835b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
10843a2f7427SDavid Greenman 		timeout(fd_timeout, (caddr_t)fdcu, hz);
10855b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
10865b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
1087d0917939SPaul Richards 		untimeout(fd_timeout, (caddr_t)fdcu);
10885b81b6b3SRodney W. Grimes 		for(i=0;i<7;i++)
10895b81b6b3SRodney W. Grimes 		{
10905b81b6b3SRodney W. Grimes 			fdc->status[i] = in_fdc(fdcu);
10915b81b6b3SRodney W. Grimes 		}
10923a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
10933a2f7427SDavid Greenman 		/* FALLTHROUGH */
10943a2f7427SDavid Greenman 	case IOTIMEDOUT:
10955b81b6b3SRodney W. Grimes 		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
10963a2f7427SDavid Greenman 			    format ? bp->b_bcount : fdblk, fdc->dmachan);
10973a2f7427SDavid Greenman 		if (fdc->status[0] & NE7_ST0_IC)
10985b81b6b3SRodney W. Grimes 		{
10993a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
11003a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
1101b39c878eSAndrey A. Chernov                                 /*
11023a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
11033a2f7427SDavid Greenman 				 * and didn't release it in time for the
11043a2f7427SDavid Greenman 				 * next FDC transfer.
11053a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
11063a2f7427SDavid Greenman 				 * count. (vak)
1107b39c878eSAndrey A. Chernov                                  */
1108b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
1109b39c878eSAndrey A. Chernov                                 return (1);
1110b39c878eSAndrey A. Chernov                         }
11113a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
11123a2f7427SDavid Greenman 				&& fdc->retry < 6)
11133a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
11143a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
11153a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
11163a2f7427SDavid Greenman 				&& fdc->retry < 3)
11173a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
11185b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
11195b81b6b3SRodney W. Grimes 		}
11205b81b6b3SRodney W. Grimes 		/* All OK */
11213a2f7427SDavid Greenman 		fd->skip += fdblk;
1122b39c878eSAndrey A. Chernov 		if (!format && fd->skip < bp->b_bcount)
11235b81b6b3SRodney W. Grimes 		{
11245b81b6b3SRodney W. Grimes 			/* set up next transfer */
11253a2f7427SDavid Greenman 			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk
11263a2f7427SDavid Greenman 				+ fd->skip/fdblk;
11273a2f7427SDavid Greenman 			bp->b_cylin =
11283a2f7427SDavid Greenman 				(blknum / (fd->ft->sectrac * fd->ft->heads));
11295b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
11305b81b6b3SRodney W. Grimes 		}
11315b81b6b3SRodney W. Grimes 		else
11325b81b6b3SRodney W. Grimes 		{
11335b81b6b3SRodney W. Grimes 			/* ALL DONE */
11345b81b6b3SRodney W. Grimes 			fd->skip = 0;
11355b81b6b3SRodney W. Grimes 			bp->b_resid = 0;
113626f9a767SRodney W. Grimes 			dp->b_actf = bp->b_actf;
11375b81b6b3SRodney W. Grimes 			biodone(bp);
11385b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
11395b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
11405b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
11415b81b6b3SRodney W. Grimes 		}
11425b81b6b3SRodney W. Grimes 		return(1);
11435b81b6b3SRodney W. Grimes 	case RESETCTLR:
11443a2f7427SDavid Greenman 		fdc_reset(fdc);
11455b81b6b3SRodney W. Grimes 		fdc->retry++;
11465b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
11475b81b6b3SRodney W. Grimes 		break;
11485b81b6b3SRodney W. Grimes 	case STARTRECAL:
11495b81b6b3SRodney W. Grimes 		out_fdc(fdcu, NE7CMD_RECAL);	/* Recalibrate Function */
11505b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fdu);
11515b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
11525b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
11535b81b6b3SRodney W. Grimes 	case RECALWAIT:
11545b81b6b3SRodney W. Grimes 		/* allow heads to settle */
11553a2f7427SDavid Greenman 		timeout(fd_pseudointr, (caddr_t)fdcu, hz / 32);
11565b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
11575b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
11585b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
11593a2f7427SDavid Greenman 		do {
11605b81b6b3SRodney W. Grimes 			out_fdc(fdcu, NE7CMD_SENSEI);
11615b81b6b3SRodney W. Grimes 			st0 = in_fdc(fdcu);
11625b81b6b3SRodney W. Grimes 			cyl = in_fdc(fdcu);
11633a2f7427SDavid Greenman 			/*
11643a2f7427SDavid Greenman 			 * if this was a "ready changed" interrupt,
11653a2f7427SDavid Greenman 			 * fetch status again (can happen after
11663a2f7427SDavid Greenman 			 * enabling controller from reset state)
11673a2f7427SDavid Greenman 			 */
11683a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
11693a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
11705b81b6b3SRodney W. Grimes 		{
11715b81b6b3SRodney W. Grimes 			printf("fd%d: recal failed ST0 %b cyl %d\n", fdu,
11725b81b6b3SRodney W. Grimes 				st0, NE7_ST0BITS, cyl);
11733a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
11745b81b6b3SRodney W. Grimes 			return(retrier(fdcu));
11755b81b6b3SRodney W. Grimes 		}
11765b81b6b3SRodney W. Grimes 		fd->track = 0;
11775b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
11785b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
11795b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
11805b81b6b3SRodney W. Grimes 	case MOTORWAIT:
11815b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
11825b81b6b3SRodney W. Grimes 		{
11835b81b6b3SRodney W. Grimes 			return(0); /* time's not up yet */
11845b81b6b3SRodney W. Grimes 		}
11853a2f7427SDavid Greenman 		/*
11863a2f7427SDavid Greenman 		 * since the controller was off, it has lost its
11873a2f7427SDavid Greenman 		 * idea about the current track it were; thus,
11883a2f7427SDavid Greenman 		 * recalibrate the bastard
11893a2f7427SDavid Greenman 		 */
11903a2f7427SDavid Greenman 		fdc->state = STARTRECAL;
11915b81b6b3SRodney W. Grimes 		return(1);	/* will return immediatly */
11925b81b6b3SRodney W. Grimes 	default:
11935b81b6b3SRodney W. Grimes 		printf("Unexpected FD int->");
11945b81b6b3SRodney W. Grimes 		out_fdc(fdcu, NE7CMD_SENSEI);
11955b81b6b3SRodney W. Grimes 		st0 = in_fdc(fdcu);
11965b81b6b3SRodney W. Grimes 		cyl = in_fdc(fdcu);
11973a2f7427SDavid Greenman 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
11983a2f7427SDavid Greenman 		out_fdc(fdcu, NE7CMD_READID);
11995b81b6b3SRodney W. Grimes 		out_fdc(fdcu, fd->fdsu);
12005b81b6b3SRodney W. Grimes 		for(i=0;i<7;i++) {
12015b81b6b3SRodney W. Grimes 			fdc->status[i] = in_fdc(fdcu);
12025b81b6b3SRodney W. Grimes 		}
12033a2f7427SDavid Greenman 		if(fdc->status[0] != -1)
12043a2f7427SDavid Greenman 			printf("intr status :%lx %lx %lx %lx %lx %lx %lx\n",
12055b81b6b3SRodney W. Grimes 			       fdc->status[0],
12065b81b6b3SRodney W. Grimes 			       fdc->status[1],
12075b81b6b3SRodney W. Grimes 			       fdc->status[2],
12085b81b6b3SRodney W. Grimes 			       fdc->status[3],
12095b81b6b3SRodney W. Grimes 			       fdc->status[4],
12105b81b6b3SRodney W. Grimes 			       fdc->status[5],
12115b81b6b3SRodney W. Grimes 			       fdc->status[6] );
12123a2f7427SDavid Greenman 		else
12133a2f7427SDavid Greenman 			printf("FDC timed out\n");
12145b81b6b3SRodney W. Grimes 		return(0);
12155b81b6b3SRodney W. Grimes 	}
12165b81b6b3SRodney W. Grimes 	return(1); /* Come back immediatly to new state */
12175b81b6b3SRodney W. Grimes }
12185b81b6b3SRodney W. Grimes 
1219aaf08d94SGarrett Wollman static int
1220f5f7ba03SJordan K. Hubbard retrier(fdcu)
1221f5f7ba03SJordan K. Hubbard 	fdcu_t fdcu;
12225b81b6b3SRodney W. Grimes {
12235b81b6b3SRodney W. Grimes 	fdc_p fdc = fdc_data + fdcu;
12245b81b6b3SRodney W. Grimes 	register struct buf *dp, *bp;
12255b81b6b3SRodney W. Grimes 
12265b81b6b3SRodney W. Grimes 	dp = &(fdc->head);
12275b81b6b3SRodney W. Grimes 	bp = dp->b_actf;
12285b81b6b3SRodney W. Grimes 
12293a2f7427SDavid Greenman 	if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY)
12303a2f7427SDavid Greenman 		goto fail;
12315b81b6b3SRodney W. Grimes 	switch(fdc->retry)
12325b81b6b3SRodney W. Grimes 	{
12335b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
12345b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
12355b81b6b3SRodney W. Grimes 		break;
12365b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
12375b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
12385b81b6b3SRodney W. Grimes 		break;
12395b81b6b3SRodney W. Grimes 	case 6:
12405b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
12415b81b6b3SRodney W. Grimes 		break;
12425b81b6b3SRodney W. Grimes 	case 7:
12435b81b6b3SRodney W. Grimes 		break;
12445b81b6b3SRodney W. Grimes 	default:
12453a2f7427SDavid Greenman 	fail:
12465b81b6b3SRodney W. Grimes 		{
12477ca0641bSAndrey A. Chernov 			dev_t sav_b_dev = bp->b_dev;
12487ca0641bSAndrey A. Chernov 			/* Trick diskerr */
12493a2f7427SDavid Greenman 			bp->b_dev = makedev(major(bp->b_dev),
12503a2f7427SDavid Greenman 					    (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART);
125192ed385aSRodney W. Grimes 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
12523a2f7427SDavid Greenman 				fdc->fd->skip / DEV_BSIZE,
12533a2f7427SDavid Greenman 				(struct disklabel *)NULL);
12547ca0641bSAndrey A. Chernov 			bp->b_dev = sav_b_dev;
125592ed385aSRodney W. Grimes 			printf(" (ST0 %b ", fdc->status[0], NE7_ST0BITS);
12565b81b6b3SRodney W. Grimes 			printf(" ST1 %b ", fdc->status[1], NE7_ST1BITS);
12575b81b6b3SRodney W. Grimes 			printf(" ST2 %b ", fdc->status[2], NE7_ST2BITS);
12585b81b6b3SRodney W. Grimes 			printf("cyl %d hd %d sec %d)\n",
125992ed385aSRodney W. Grimes 			       fdc->status[3], fdc->status[4], fdc->status[5]);
12605b81b6b3SRodney W. Grimes 		}
12615b81b6b3SRodney W. Grimes 		bp->b_flags |= B_ERROR;
12625b81b6b3SRodney W. Grimes 		bp->b_error = EIO;
12635b81b6b3SRodney W. Grimes 		bp->b_resid = bp->b_bcount - fdc->fd->skip;
126426f9a767SRodney W. Grimes 		dp->b_actf = bp->b_actf;
12655b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
12665b81b6b3SRodney W. Grimes 		biodone(bp);
126792ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
12685b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
12695b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
1270f5f7ba03SJordan K. Hubbard 		/* XXX abort current command, if any.  */
127192ed385aSRodney W. Grimes 		return(1);
12725b81b6b3SRodney W. Grimes 	}
12735b81b6b3SRodney W. Grimes 	fdc->retry++;
12745b81b6b3SRodney W. Grimes 	return(1);
12755b81b6b3SRodney W. Grimes }
12765b81b6b3SRodney W. Grimes 
1277b39c878eSAndrey A. Chernov static int
1278b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
1279b39c878eSAndrey A. Chernov 	dev_t dev;
1280b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
1281b39c878eSAndrey A. Chernov 	struct proc *p;
1282b39c878eSAndrey A. Chernov {
1283b39c878eSAndrey A. Chernov  	fdu_t	fdu;
1284b39c878eSAndrey A. Chernov  	fd_p	fd;
1285b39c878eSAndrey A. Chernov 
1286b39c878eSAndrey A. Chernov 	struct buf *bp;
1287b39c878eSAndrey A. Chernov 	int rv = 0, s;
12883a2f7427SDavid Greenman 	size_t fdblk;
1289b39c878eSAndrey A. Chernov 
1290b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
1291b39c878eSAndrey A. Chernov 	fd = &fd_data[fdu];
12923a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1293b39c878eSAndrey A. Chernov 
1294b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
1295b39c878eSAndrey A. Chernov 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1296b39c878eSAndrey A. Chernov 	if(bp == 0)
1297b39c878eSAndrey A. Chernov 		return ENOBUFS;
1298b39c878eSAndrey A. Chernov 	bzero((void *)bp, sizeof(struct buf));
1299b39c878eSAndrey A. Chernov 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1300b39c878eSAndrey A. Chernov 	bp->b_proc = p;
1301b39c878eSAndrey A. Chernov 	bp->b_dev = dev;
1302b39c878eSAndrey A. Chernov 
1303b39c878eSAndrey A. Chernov 	/*
1304b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
1305b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
1306b39c878eSAndrey A. Chernov 	 */
1307b39c878eSAndrey A. Chernov 	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
13083a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
1309b39c878eSAndrey A. Chernov 
1310b39c878eSAndrey A. Chernov 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1311b39c878eSAndrey A. Chernov 	bp->b_un.b_addr = (caddr_t)finfo;
1312b39c878eSAndrey A. Chernov 
1313b39c878eSAndrey A. Chernov 	/* now do the format */
1314b39c878eSAndrey A. Chernov 	fdstrategy(bp);
1315b39c878eSAndrey A. Chernov 
1316b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
1317b39c878eSAndrey A. Chernov 	s = splbio();
1318b39c878eSAndrey A. Chernov 	while(!(bp->b_flags & B_DONE))
1319b39c878eSAndrey A. Chernov 	{
1320b39c878eSAndrey A. Chernov 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1321b39c878eSAndrey A. Chernov 		if(rv == EWOULDBLOCK)
1322b39c878eSAndrey A. Chernov 			break;
1323b39c878eSAndrey A. Chernov 	}
1324b39c878eSAndrey A. Chernov 	splx(s);
1325b39c878eSAndrey A. Chernov 
1326b39c878eSAndrey A. Chernov 	if(rv == EWOULDBLOCK)
1327b39c878eSAndrey A. Chernov 		/* timed out */
1328b39c878eSAndrey A. Chernov 		rv = EIO;
13293a2f7427SDavid Greenman 	if(bp->b_flags & B_ERROR)
13303a2f7427SDavid Greenman 		rv = bp->b_error;
13313a2f7427SDavid Greenman 	biodone(bp);
1332b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
1333b39c878eSAndrey A. Chernov 	return rv;
1334b39c878eSAndrey A. Chernov }
1335b39c878eSAndrey A. Chernov 
1336f5f7ba03SJordan K. Hubbard /*
1337f5f7ba03SJordan K. Hubbard  *
13383a2f7427SDavid Greenman  * TODO: Think about allocating buffer off stack.
1339f5f7ba03SJordan K. Hubbard  *       Don't pass uncast 0's and NULL's to read/write/setdisklabel().
1340f5f7ba03SJordan K. Hubbard  *       Watch out for NetBSD's different *disklabel() interface.
1341b39c878eSAndrey A. Chernov  *
1342f5f7ba03SJordan K. Hubbard  */
13435b81b6b3SRodney W. Grimes 
1344f5f7ba03SJordan K. Hubbard int
1345b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
1346f5f7ba03SJordan K. Hubbard 	dev_t dev;
1347f5f7ba03SJordan K. Hubbard 	int cmd;
1348f5f7ba03SJordan K. Hubbard 	caddr_t addr;
1349f5f7ba03SJordan K. Hubbard 	int flag;
1350b39c878eSAndrey A. Chernov 	struct proc *p;
1351f5f7ba03SJordan K. Hubbard {
13523a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
13533a2f7427SDavid Greenman  	fd_p	fd = &fd_data[fdu];
13543a2f7427SDavid Greenman 	size_t fdblk;
13553a2f7427SDavid Greenman 
1356f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
1357f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
1358f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
13593a2f7427SDavid Greenman 	int error = 0;
1360f5f7ba03SJordan K. Hubbard 
1361b99f0a4aSAndrew Moore #if NFT > 0
1362a60eff27SNate Williams 	int type = FDTYPE(minor(dev));
1363a60eff27SNate Williams 
1364a60eff27SNate Williams 	/* check for a tape ioctl */
1365a60eff27SNate Williams 	if (type & F_TAPE_TYPE)
1366b99f0a4aSAndrew Moore 		return ftioctl(dev, cmd, addr, flag, p);
1367b99f0a4aSAndrew Moore #endif
1368b99f0a4aSAndrew Moore 
13693a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1370f5f7ba03SJordan K. Hubbard 
1371f5f7ba03SJordan K. Hubbard 	switch (cmd)
1372f5f7ba03SJordan K. Hubbard 	{
1373f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
1374f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
1375f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
13763a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
137792ed385aSRodney W. Grimes 		fdt = fd_data[FDUNIT(minor(dev))].ft;
1378f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
1379f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
1380f5f7ba03SJordan K. Hubbard 
1381f5f7ba03SJordan K. Hubbard 		if (readdisklabel(dev, fdstrategy, dl, NULL, 0, 0) == NULL)
1382f5f7ba03SJordan K. Hubbard 			error = 0;
1383f5f7ba03SJordan K. Hubbard 		else
1384f5f7ba03SJordan K. Hubbard 			error = EINVAL;
1385f5f7ba03SJordan K. Hubbard 
1386f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
1387f5f7ba03SJordan K. Hubbard 		break;
1388f5f7ba03SJordan K. Hubbard 
1389f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
1390f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1391f5f7ba03SJordan K. Hubbard 			error = EBADF;
1392f5f7ba03SJordan K. Hubbard 		break;
1393f5f7ba03SJordan K. Hubbard 
1394f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
1395f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1396f5f7ba03SJordan K. Hubbard 			error = EBADF;
1397f5f7ba03SJordan K. Hubbard 		break;
1398f5f7ba03SJordan K. Hubbard 
1399f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
1400f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
1401f5f7ba03SJordan K. Hubbard 		{
1402f5f7ba03SJordan K. Hubbard 			error = EBADF;
1403f5f7ba03SJordan K. Hubbard 			break;
1404f5f7ba03SJordan K. Hubbard 		}
1405f5f7ba03SJordan K. Hubbard 
1406f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
1407f5f7ba03SJordan K. Hubbard 
14083a2f7427SDavid Greenman 		if ((error =
14093a2f7427SDavid Greenman 		     setdisklabel ((struct disklabel *)buffer, dl, 0, NULL)))
1410f5f7ba03SJordan K. Hubbard 			break;
1411f5f7ba03SJordan K. Hubbard 
1412b39c878eSAndrey A. Chernov 		error = writedisklabel(dev, fdstrategy,
1413b39c878eSAndrey A. Chernov 				       (struct disklabel *)buffer, NULL);
1414b39c878eSAndrey A. Chernov 		break;
1415b39c878eSAndrey A. Chernov 
1416b39c878eSAndrey A. Chernov 	case FD_FORM:
1417b39c878eSAndrey A. Chernov 		if((flag & FWRITE) == 0)
1418b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
1419b39c878eSAndrey A. Chernov 		else if(((struct fd_formb *)addr)->format_version !=
1420b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
1421b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
1422b39c878eSAndrey A. Chernov 		else
1423b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
1424b39c878eSAndrey A. Chernov 		break;
1425b39c878eSAndrey A. Chernov 
1426b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
1427b39c878eSAndrey A. Chernov 		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
1428f5f7ba03SJordan K. Hubbard 		break;
1429f5f7ba03SJordan K. Hubbard 
14303a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
14313a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
14323a2f7427SDavid Greenman 		if(suser(p->p_ucred, &p->p_acflag) != 0)
14333a2f7427SDavid Greenman 			return EPERM;
14343a2f7427SDavid Greenman 		*fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr;
14353a2f7427SDavid Greenman 		break;
14363a2f7427SDavid Greenman 
14373a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
14383a2f7427SDavid Greenman 		*(int *)addr = fd_data[FDUNIT(minor(dev))].options;
14393a2f7427SDavid Greenman 		break;
14403a2f7427SDavid Greenman 
14413a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
14423a2f7427SDavid Greenman 		fd_data[FDUNIT(minor(dev))].options = *(int *)addr;
14433a2f7427SDavid Greenman 		break;
14443a2f7427SDavid Greenman 
1445f5f7ba03SJordan K. Hubbard 	default:
14463a2f7427SDavid Greenman 		error = ENOTTY;
1447f5f7ba03SJordan K. Hubbard 		break;
1448f5f7ba03SJordan K. Hubbard 	}
1449f5f7ba03SJordan K. Hubbard 	return (error);
1450f5f7ba03SJordan K. Hubbard }
1451f5f7ba03SJordan K. Hubbard 
1452f5f7ba03SJordan K. Hubbard #endif
14533a2f7427SDavid Greenman /*
14543a2f7427SDavid Greenman  * Hello emacs, these are the
14553a2f7427SDavid Greenman  * Local Variables:
14563a2f7427SDavid Greenman  *  c-indent-level:               8
14573a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
14583a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
14593a2f7427SDavid Greenman  *  c-brace-offset:              -8
14603a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
14613a2f7427SDavid Greenman  *  c-argdecl-indent:             8
14623a2f7427SDavid Greenman  *  c-label-offset:              -8
14633a2f7427SDavid Greenman  *  c++-hanging-braces:           1
14643a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
14653a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
14663a2f7427SDavid Greenman  *  c++-friend-offset:            0
14673a2f7427SDavid Greenman  * End:
14683a2f7427SDavid Greenman  */
1469