Home | History | Annotate | Line # | Download | only in dev
hdfd.c revision 1.6
      1  1.6      leo /*	$NetBSD: hdfd.c,v 1.6 1997/01/01 21:12:56 leo Exp $	*/
      2  1.1      leo 
      3  1.1      leo /*-
      4  1.1      leo  * Copyright (c) 1996 Leo Weppelman
      5  1.1      leo  * Copyright (c) 1993, 1994, 1995, 1996
      6  1.1      leo  *	Charles M. Hannum.  All rights reserved.
      7  1.1      leo  * Copyright (c) 1990 The Regents of the University of California.
      8  1.1      leo  * All rights reserved.
      9  1.1      leo  *
     10  1.1      leo  * This code is derived from software contributed to Berkeley by
     11  1.1      leo  * Don Ahn.
     12  1.1      leo  *
     13  1.1      leo  * Redistribution and use in source and binary forms, with or without
     14  1.1      leo  * modification, are permitted provided that the following conditions
     15  1.1      leo  * are met:
     16  1.1      leo  * 1. Redistributions of source code must retain the above copyright
     17  1.1      leo  *    notice, this list of conditions and the following disclaimer.
     18  1.1      leo  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1      leo  *    notice, this list of conditions and the following disclaimer in the
     20  1.1      leo  *    documentation and/or other materials provided with the distribution.
     21  1.1      leo  * 3. All advertising materials mentioning features or use of this software
     22  1.1      leo  *    must display the following acknowledgement:
     23  1.1      leo  *	This product includes software developed by the University of
     24  1.1      leo  *	California, Berkeley and its contributors.
     25  1.1      leo  * 4. Neither the name of the University nor the names of its contributors
     26  1.1      leo  *    may be used to endorse or promote products derived from this software
     27  1.1      leo  *    without specific prior written permission.
     28  1.1      leo  *
     29  1.1      leo  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  1.1      leo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  1.1      leo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  1.1      leo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  1.1      leo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  1.1      leo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  1.1      leo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  1.1      leo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  1.1      leo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  1.1      leo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  1.1      leo  * SUCH DAMAGE.
     40  1.1      leo  *
     41  1.1      leo  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     42  1.1      leo  */
     43  1.1      leo 
     44  1.3      leo /*
     45  1.3      leo  * Floppy formatting facilities merged from FreeBSD fd.c driver:
     46  1.3      leo  *	Id: fd.c,v 1.53 1995/03/12 22:40:56 joerg Exp
     47  1.3      leo  * which carries the same copyright/redistribution notice as shown above with
     48  1.3      leo  * the addition of the following statement before the "Redistribution and
     49  1.3      leo  * use ..." clause:
     50  1.3      leo  *
     51  1.3      leo  * Copyright (c) 1993, 1994 by
     52  1.3      leo  *  jc (at) irbs.UUCP (John Capo)
     53  1.3      leo  *  vak (at) zebub.msk.su (Serge Vakulenko)
     54  1.3      leo  *  ache (at) astral.msk.su (Andrew A. Chernov)
     55  1.3      leo  *
     56  1.3      leo  * Copyright (c) 1993, 1994, 1995 by
     57  1.3      leo  *  joerg_wunsch (at) uriah.sax.de (Joerg Wunsch)
     58  1.3      leo  *  dufault (at) hda.com (Peter Dufault)
     59  1.3      leo  */
     60  1.3      leo 
     61  1.1      leo #include <sys/param.h>
     62  1.1      leo #include <sys/systm.h>
     63  1.1      leo #include <sys/kernel.h>
     64  1.1      leo #include <sys/file.h>
     65  1.1      leo #include <sys/ioctl.h>
     66  1.1      leo #include <sys/device.h>
     67  1.1      leo #include <sys/disklabel.h>
     68  1.1      leo #include <sys/dkstat.h>
     69  1.1      leo #include <sys/disk.h>
     70  1.1      leo #include <sys/buf.h>
     71  1.3      leo #include <sys/malloc.h>
     72  1.1      leo #include <sys/uio.h>
     73  1.1      leo #include <sys/syslog.h>
     74  1.1      leo #include <sys/queue.h>
     75  1.3      leo #include <sys/proc.h>
     76  1.3      leo #include <sys/fdio.h>
     77  1.1      leo #include <sys/conf.h>
     78  1.1      leo #include <sys/device.h>
     79  1.1      leo 
     80  1.1      leo #include <machine/cpu.h>
     81  1.1      leo #include <machine/bus.h>
     82  1.1      leo #include <machine/iomap.h>
     83  1.1      leo #include <machine/mfp.h>
     84  1.1      leo 
     85  1.1      leo #include <atari/dev/hdfdreg.h>
     86  1.6      leo #include <atari/atari/intr.h>
     87  1.1      leo #include <atari/atari/device.h>
     88  1.1      leo 
     89  1.1      leo /*
     90  1.1      leo  * {b,c}devsw[] function prototypes
     91  1.1      leo  */
     92  1.1      leo dev_type_open(fdopen);
     93  1.1      leo dev_type_close(fdclose);
     94  1.1      leo dev_type_read(fdread);
     95  1.1      leo dev_type_write(fdwrite);
     96  1.1      leo dev_type_ioctl(fdioctl);
     97  1.1      leo dev_type_size(fdsize);
     98  1.1      leo dev_type_dump(fddump);
     99  1.1      leo 
    100  1.1      leo volatile u_char	*fdio_addr;
    101  1.1      leo 
    102  1.1      leo #define wrt_fdc_reg(reg, val)	{ fdio_addr[reg] = val; }
    103  1.1      leo #define rd_fdc_reg(reg)		( fdio_addr[reg] )
    104  1.1      leo 
    105  1.1      leo #define	fdc_ienable()		MFP2->mf_ierb |= IB_DCHG;
    106  1.1      leo 
    107  1.1      leo /*
    108  1.1      leo  * Interface to the pseudo-dma handler
    109  1.1      leo  */
    110  1.1      leo void	fddma_intr(void);
    111  1.1      leo caddr_t	fddmaaddr  = NULL;
    112  1.1      leo int	fddmalen   = 0;
    113  1.1      leo 
    114  1.6      leo extern void	mfp_hdfd_nf __P((void)), mfp_hdfd_fifo __P((void));
    115  1.6      leo 
    116  1.1      leo /*
    117  1.1      leo  * Argument to fdcintr.....
    118  1.1      leo  */
    119  1.1      leo static void	*intr_arg = NULL; /* XXX: arg. to intr_establish() */
    120  1.1      leo 
    121  1.1      leo 
    122  1.6      leo 
    123  1.1      leo #define FDUNIT(dev)	(minor(dev) / 8)
    124  1.1      leo #define FDTYPE(dev)	(minor(dev) % 8)
    125  1.1      leo 
    126  1.3      leo /* XXX misuse a flag to identify format operation */
    127  1.3      leo #define B_FORMAT B_XXX
    128  1.3      leo 
    129  1.1      leo #define b_cylin b_resid
    130  1.1      leo 
    131  1.1      leo enum fdc_state {
    132  1.1      leo 	DEVIDLE = 0,
    133  1.1      leo 	MOTORWAIT,
    134  1.1      leo 	DOSEEK,
    135  1.1      leo 	SEEKWAIT,
    136  1.1      leo 	SEEKTIMEDOUT,
    137  1.1      leo 	SEEKCOMPLETE,
    138  1.1      leo 	DOIO,
    139  1.1      leo 	IOCOMPLETE,
    140  1.1      leo 	IOTIMEDOUT,
    141  1.1      leo 	DORESET,
    142  1.1      leo 	RESETCOMPLETE,
    143  1.1      leo 	RESETTIMEDOUT,
    144  1.1      leo 	DORECAL,
    145  1.1      leo 	RECALWAIT,
    146  1.1      leo 	RECALTIMEDOUT,
    147  1.1      leo 	RECALCOMPLETE,
    148  1.1      leo };
    149  1.1      leo 
    150  1.1      leo /* software state, per controller */
    151  1.1      leo struct fdc_softc {
    152  1.1      leo 	struct device	sc_dev;		/* boilerplate */
    153  1.1      leo 	struct fd_softc	*sc_fd[4];	/* pointers to children */
    154  1.1      leo 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    155  1.1      leo 	enum fdc_state	sc_state;
    156  1.1      leo 	int		sc_errors;	/* number of retries so far */
    157  1.1      leo 	int		sc_overruns;	/* number of overruns so far */
    158  1.1      leo 	u_char		sc_status[7];	/* copy of registers */
    159  1.1      leo };
    160  1.1      leo 
    161  1.1      leo /* controller driver configuration */
    162  1.5      leo int	fdcprobe __P((struct device *, struct cfdata *, void *));
    163  1.1      leo int	fdprint __P((void *, const char *));
    164  1.1      leo void	fdcattach __P((struct device *, struct device *, void *));
    165  1.1      leo 
    166  1.1      leo struct cfattach fdc_ca = {
    167  1.1      leo 	sizeof(struct fdc_softc), fdcprobe, fdcattach
    168  1.1      leo };
    169  1.1      leo 
    170  1.1      leo struct cfdriver fdc_cd = {
    171  1.1      leo 	NULL, "fdc", DV_DULL
    172  1.1      leo };
    173  1.1      leo 
    174  1.1      leo /*
    175  1.1      leo  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    176  1.1      leo  * we tell them apart.
    177  1.1      leo  */
    178  1.1      leo struct fd_type {
    179  1.1      leo 	int	sectrac;	/* sectors per track */
    180  1.1      leo 	int	heads;		/* number of heads */
    181  1.1      leo 	int	seccyl;		/* sectors per cylinder */
    182  1.1      leo 	int	secsize;	/* size code for sectors */
    183  1.1      leo 	int	datalen;	/* data len when secsize = 0 */
    184  1.1      leo 	int	steprate;	/* step rate and head unload time */
    185  1.1      leo 	int	gap1;		/* gap len between sectors */
    186  1.1      leo 	int	gap2;		/* formatting gap */
    187  1.1      leo 	int	tracks;		/* total num of tracks */
    188  1.1      leo 	int	size;		/* size of disk in sectors */
    189  1.1      leo 	int	step;		/* steps per cylinder */
    190  1.1      leo 	int	rate;		/* transfer speed code */
    191  1.3      leo 	u_char	fillbyte;	/* format fill byte */
    192  1.3      leo 	u_char	interleave;	/* interleave factor (formatting) */
    193  1.1      leo 	char	*name;
    194  1.1      leo };
    195  1.1      leo 
    196  1.1      leo /*
    197  1.1      leo  * The order of entries in the following table is important -- BEWARE!
    198  1.1      leo  * The order of the types is the same as for the TT/Falcon....
    199  1.1      leo  */
    200  1.1      leo struct fd_type fd_types[] = {
    201  1.1      leo         /* 360kB in 720kB drive */
    202  1.3      leo         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_125KBPS,0xf6,1,"360KB"  },
    203  1.1      leo         /* 3.5" 720kB diskette */
    204  1.3      leo         {  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_125KBPS,0xf6,1,"720KB"  },
    205  1.1      leo         /* 1.44MB diskette */
    206  1.3      leo         { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_250KBPS,0xf6,1,"1.44MB" },
    207  1.1      leo };
    208  1.1      leo 
    209  1.1      leo /* software state, per disk (with up to 4 disks per ctlr) */
    210  1.1      leo struct fd_softc {
    211  1.1      leo 	struct device	sc_dev;
    212  1.1      leo 	struct disk	sc_dk;
    213  1.1      leo 
    214  1.1      leo 	struct fd_type	*sc_deftype;	/* default type descriptor */
    215  1.1      leo 	struct fd_type	*sc_type;	/* current type descriptor */
    216  1.1      leo 
    217  1.1      leo 	daddr_t		sc_blkno;	/* starting block number */
    218  1.1      leo 	int		sc_bcount;	/* byte count left */
    219  1.3      leo  	int		sc_opts;	/* user-set options */
    220  1.1      leo 	int		sc_skip;	/* bytes already transferred */
    221  1.1      leo 	int		sc_nblks;	/* #blocks currently tranferring */
    222  1.1      leo 	int		sc_nbytes;	/* #bytes currently tranferring */
    223  1.1      leo 
    224  1.1      leo 	int		sc_drive;	/* physical unit number */
    225  1.1      leo 	int		sc_flags;
    226  1.1      leo #define	FD_OPEN		0x01		/* it's open */
    227  1.1      leo #define	FD_MOTOR	0x02		/* motor should be on */
    228  1.1      leo #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    229  1.1      leo 	int		sc_cylin;	/* where we think the head is */
    230  1.1      leo 
    231  1.1      leo 	void		*sc_sdhook;	/* saved shutdown hook for drive. */
    232  1.1      leo 
    233  1.1      leo 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    234  1.1      leo 	int		sc_ops;		/* I/O ops since last switch */
    235  1.1      leo 	struct buf	sc_q;		/* head of buf chain */
    236  1.1      leo };
    237  1.1      leo 
    238  1.1      leo /* floppy driver configuration */
    239  1.5      leo int	fdprobe __P((struct device *, struct cfdata *, void *));
    240  1.1      leo void	fdattach __P((struct device *, struct device *, void *));
    241  1.1      leo 
    242  1.1      leo struct cfattach hdfd_ca = {
    243  1.1      leo 	sizeof(struct fd_softc), fdprobe, fdattach
    244  1.1      leo };
    245  1.1      leo 
    246  1.1      leo struct cfdriver hdfd_cd = {
    247  1.1      leo 	NULL, "hdfd", DV_DISK
    248  1.1      leo };
    249  1.1      leo 
    250  1.1      leo void	fdstrategy __P((struct buf *));
    251  1.1      leo void	fdstart __P((struct fd_softc *));
    252  1.1      leo 
    253  1.1      leo struct dkdriver fddkdriver = { fdstrategy };
    254  1.1      leo 
    255  1.1      leo void	fd_set_motor __P((struct fdc_softc *fdc, int reset));
    256  1.1      leo void	fd_motor_off __P((void *arg));
    257  1.1      leo void	fd_motor_on __P((void *arg));
    258  1.1      leo int	fdcresult __P((struct fdc_softc *fdc));
    259  1.1      leo int	out_fdc __P((u_char x));
    260  1.4      leo void	fdc_ctrl_intr __P((struct clockframe));
    261  1.1      leo void	fdcstart __P((struct fdc_softc *fdc));
    262  1.1      leo void	fdcstatus __P((struct device *dv, int n, char *s));
    263  1.1      leo void	fdctimeout __P((void *arg));
    264  1.1      leo void	fdcpseudointr __P((void *arg));
    265  1.1      leo int	fdcintr __P((void *));
    266  1.1      leo void	fdcretry __P((struct fdc_softc *fdc));
    267  1.1      leo void	fdfinish __P((struct fd_softc *fd, struct buf *bp));
    268  1.3      leo int	fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
    269  1.3      leo 
    270  1.1      leo __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
    271  1.1      leo 
    272  1.1      leo int
    273  1.5      leo fdcprobe(parent, cfp, aux)
    274  1.5      leo 	struct device	*parent;
    275  1.5      leo 	struct cfdata	*cfp;
    276  1.5      leo 	void		*aux;
    277  1.1      leo {
    278  1.1      leo 	int		rv   = 0;
    279  1.1      leo 
    280  1.1      leo 	if(strcmp("fdc", aux) || cfp->cf_unit != 0)
    281  1.1      leo 		return(0);
    282  1.1      leo 
    283  1.1      leo 	if (!atari_realconfig)
    284  1.1      leo 		return 0;
    285  1.1      leo 
    286  1.1      leo 	if (bus_space_map(NULL, 0xfff00000, NBPG, 0, (caddr_t*)&fdio_addr)) {
    287  1.1      leo 		printf("fdcprobe: cannot map io-area\n");
    288  1.1      leo 		return (0);
    289  1.1      leo 	}
    290  1.1      leo 
    291  1.1      leo #ifdef FD_DEBUG
    292  1.1      leo 	printf("fdcprobe: I/O mapping done va: %p\n", fdio_addr);
    293  1.1      leo #endif
    294  1.1      leo 
    295  1.1      leo 	/* reset */
    296  1.1      leo 	wrt_fdc_reg(fdout, 0);
    297  1.1      leo 	delay(100);
    298  1.1      leo 	wrt_fdc_reg(fdout, FDO_FRST);
    299  1.1      leo 
    300  1.1      leo 	/* see if it can handle a command */
    301  1.1      leo 	if (out_fdc(NE7CMD_SPECIFY) < 0)
    302  1.1      leo 		goto out;
    303  1.1      leo 	out_fdc(0xdf);
    304  1.1      leo 	out_fdc(7);
    305  1.1      leo 
    306  1.1      leo 	rv = 1;
    307  1.1      leo 
    308  1.1      leo  out:
    309  1.1      leo 	if (rv == 0)
    310  1.1      leo 		bus_space_unmap(NULL, (caddr_t)fdio_addr, NBPG);
    311  1.1      leo 
    312  1.1      leo 	return rv;
    313  1.1      leo }
    314  1.1      leo 
    315  1.1      leo /*
    316  1.1      leo  * Arguments passed between fdcattach and fdprobe.
    317  1.1      leo  */
    318  1.1      leo struct fdc_attach_args {
    319  1.1      leo 	int fa_drive;
    320  1.1      leo 	struct fd_type *fa_deftype;
    321  1.1      leo };
    322  1.1      leo 
    323  1.1      leo /*
    324  1.1      leo  * Print the location of a disk drive (called just before attaching the
    325  1.1      leo  * the drive).  If `fdc' is not NULL, the drive was found but was not
    326  1.1      leo  * in the system config file; print the drive name as well.
    327  1.1      leo  * Return QUIET (config_find ignores this if the device was configured) to
    328  1.1      leo  * avoid printing `fdN not configured' messages.
    329  1.1      leo  */
    330  1.1      leo int
    331  1.1      leo fdprint(aux, fdc)
    332  1.1      leo 	void *aux;
    333  1.1      leo 	const char *fdc;
    334  1.1      leo {
    335  1.1      leo 	register struct fdc_attach_args *fa = aux;
    336  1.1      leo 
    337  1.1      leo 	if (!fdc)
    338  1.1      leo 		printf(" drive %d", fa->fa_drive);
    339  1.1      leo 	return QUIET;
    340  1.1      leo }
    341  1.1      leo 
    342  1.1      leo void
    343  1.1      leo fdcattach(parent, self, aux)
    344  1.1      leo 	struct device *parent, *self;
    345  1.1      leo 	void *aux;
    346  1.1      leo {
    347  1.1      leo 	struct fdc_softc	*fdc = (void *)self;
    348  1.1      leo 	struct fdc_attach_args	fa;
    349  1.1      leo 	int			has_fifo;
    350  1.1      leo 
    351  1.1      leo 	has_fifo = 0;
    352  1.1      leo 
    353  1.1      leo 	fdc->sc_state = DEVIDLE;
    354  1.1      leo 	TAILQ_INIT(&fdc->sc_drives);
    355  1.1      leo 
    356  1.1      leo 	out_fdc(NE7CMD_CONFIGURE);
    357  1.1      leo 	if (out_fdc(0) == 0) {
    358  1.1      leo 		out_fdc(0x1a);	/* No polling, fifo depth = 10	*/
    359  1.1      leo 		out_fdc(0);
    360  1.1      leo 
    361  1.1      leo 		/* Retain configuration across resets	*/
    362  1.1      leo 		out_fdc(NE7CMD_LOCK);
    363  1.1      leo 		(void)fdcresult(fdc);
    364  1.1      leo 		has_fifo = 1;
    365  1.1      leo 	}
    366  1.1      leo 	else {
    367  1.1      leo 		(void)rd_fdc_reg(fddata);
    368  1.1      leo 		printf(": no fifo");
    369  1.1      leo 	}
    370  1.1      leo 
    371  1.1      leo 	printf("\n");
    372  1.1      leo 
    373  1.6      leo 	if (intr_establish(22, USER_VEC|FAST_VEC, 0,
    374  1.6      leo 			   (hw_ifun_t)(has_fifo ? mfp_hdfd_fifo : mfp_hdfd_nf),
    375  1.6      leo 			   NULL) == NULL) {
    376  1.6      leo 		printf("fdcattach: Can't establish interrupt\n");
    377  1.6      leo 		return;
    378  1.1      leo 	}
    379  1.1      leo 
    380  1.1      leo 	/*
    381  1.1      leo 	 * Setup the interrupt logic.
    382  1.1      leo 	 */
    383  1.1      leo 	MFP2->mf_iprb &= ~IB_DCHG;
    384  1.1      leo 	MFP2->mf_imrb |= IB_DCHG;
    385  1.1      leo 	MFP2->mf_aer  |= 0x10; /* fdc int low->high */
    386  1.1      leo 
    387  1.1      leo 	/* physical limit: four drives per controller. */
    388  1.1      leo 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    389  1.1      leo 		/*
    390  1.1      leo 		 * XXX: Choose something sensible as a default...
    391  1.1      leo 		 */
    392  1.1      leo 		fa.fa_deftype = &fd_types[2]; /* 1.44MB */
    393  1.1      leo 		(void)config_found(self, (void *)&fa, fdprint);
    394  1.1      leo 	}
    395  1.1      leo }
    396  1.1      leo 
    397  1.1      leo int
    398  1.5      leo fdprobe(parent, cfp, aux)
    399  1.5      leo 	struct device	*parent;
    400  1.5      leo 	struct cfdata	*cfp;
    401  1.5      leo 	void		*aux;
    402  1.1      leo {
    403  1.1      leo 	struct fdc_softc	*fdc = (void *)parent;
    404  1.1      leo 	struct fdc_attach_args	*fa = aux;
    405  1.1      leo 	int			drive = fa->fa_drive;
    406  1.1      leo 	int			n;
    407  1.1      leo 
    408  1.5      leo 	if (cfp->cf_loc[0] != -1 && cfp->cf_loc[0] != drive)
    409  1.1      leo 		return 0;
    410  1.1      leo 	/*
    411  1.1      leo 	 * XXX
    412  1.1      leo 	 * This is to work around some odd interactions between this driver
    413  1.1      leo 	 * and SMC Ethernet cards.
    414  1.1      leo 	 */
    415  1.5      leo 	if (cfp->cf_loc[0] == -1 && drive >= 2)
    416  1.1      leo 		return 0;
    417  1.1      leo 
    418  1.1      leo 	/* select drive and turn on motor */
    419  1.1      leo 	wrt_fdc_reg(fdout, drive | FDO_FRST | FDO_MOEN(drive));
    420  1.1      leo 
    421  1.1      leo 	/* wait for motor to spin up */
    422  1.1      leo 	delay(250000);
    423  1.1      leo 	out_fdc(NE7CMD_RECAL);
    424  1.1      leo 	out_fdc(drive);
    425  1.1      leo 
    426  1.1      leo 	/* wait for recalibrate */
    427  1.1      leo 	delay(2000000);
    428  1.1      leo 	out_fdc(NE7CMD_SENSEI);
    429  1.1      leo 	n = fdcresult(fdc);
    430  1.1      leo 
    431  1.1      leo #ifdef FD_DEBUG
    432  1.1      leo 	{
    433  1.1      leo 		int i;
    434  1.1      leo 		printf("fdprobe: status");
    435  1.1      leo 		for (i = 0; i < n; i++)
    436  1.1      leo 			printf(" %x", fdc->sc_status[i]);
    437  1.1      leo 		printf("\n");
    438  1.1      leo 	}
    439  1.1      leo #endif
    440  1.1      leo 	intr_arg = (void*)fdc;
    441  1.1      leo 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    442  1.1      leo 		return 0;
    443  1.1      leo 	/* turn off motor */
    444  1.1      leo 	wrt_fdc_reg(fdout, FDO_FRST);
    445  1.1      leo 
    446  1.1      leo 	return 1;
    447  1.1      leo }
    448  1.1      leo 
    449  1.1      leo /*
    450  1.1      leo  * Controller is working, and drive responded.  Attach it.
    451  1.1      leo  */
    452  1.1      leo void
    453  1.1      leo fdattach(parent, self, aux)
    454  1.1      leo 	struct device *parent, *self;
    455  1.1      leo 	void *aux;
    456  1.1      leo {
    457  1.1      leo 	struct fdc_softc	*fdc  = (void *)parent;
    458  1.1      leo 	struct fd_softc		*fd   = (void *)self;
    459  1.1      leo 	struct fdc_attach_args	*fa   = aux;
    460  1.1      leo 	struct fd_type		*type = fa->fa_deftype;
    461  1.1      leo 	int			drive = fa->fa_drive;
    462  1.1      leo 
    463  1.1      leo 	/* XXX Allow `flags' to override device type? */
    464  1.1      leo 
    465  1.1      leo 	if (type)
    466  1.1      leo 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    467  1.1      leo 		    type->tracks, type->heads, type->sectrac);
    468  1.1      leo 	else
    469  1.1      leo 		printf(": density unknown\n");
    470  1.1      leo 
    471  1.1      leo 	fd->sc_cylin      = -1;
    472  1.1      leo 	fd->sc_drive      = drive;
    473  1.1      leo 	fd->sc_deftype    = type;
    474  1.1      leo 	fdc->sc_fd[drive] = fd;
    475  1.1      leo 
    476  1.1      leo 	/*
    477  1.1      leo 	 * Initialize and attach the disk structure.
    478  1.1      leo 	 */
    479  1.1      leo 	fd->sc_dk.dk_name   = fd->sc_dev.dv_xname;
    480  1.1      leo 	fd->sc_dk.dk_driver = &fddkdriver;
    481  1.1      leo 	disk_attach(&fd->sc_dk);
    482  1.1      leo 
    483  1.1      leo 	/* XXX Need to do some more fiddling with sc_dk. */
    484  1.1      leo 	dk_establish(&fd->sc_dk, &fd->sc_dev);
    485  1.1      leo 
    486  1.1      leo 	/* Needed to power off if the motor is on when we halt. */
    487  1.1      leo 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
    488  1.1      leo }
    489  1.1      leo 
    490  1.1      leo /*
    491  1.1      leo  * This is called from the assembly part of the interrupt handler
    492  1.1      leo  * when it is clear that the interrupt was not related to shoving
    493  1.1      leo  * data.
    494  1.1      leo  */
    495  1.1      leo void
    496  1.1      leo fdc_ctrl_intr(frame)
    497  1.4      leo 	struct clockframe frame;
    498  1.1      leo {
    499  1.1      leo 	int	s;
    500  1.1      leo 
    501  1.1      leo 	/*
    502  1.1      leo 	 * Disable further interrupts. The fdcintr() routine
    503  1.1      leo 	 * explicitely enables them when needed.
    504  1.1      leo 	 */
    505  1.1      leo 	MFP2->mf_ierb &= ~IB_DCHG;
    506  1.1      leo 
    507  1.1      leo 	/*
    508  1.1      leo 	 * Set fddmalen to zero so no pseudo-dma transfers will
    509  1.1      leo 	 * occur.
    510  1.1      leo 	 */
    511  1.1      leo 	fddmalen = 0;
    512  1.1      leo 
    513  1.4      leo 	if (!BASEPRI(frame.cf_sr)) {
    514  1.1      leo 		/*
    515  1.1      leo 		 * We don't want to stay on ipl6.....
    516  1.1      leo 		 */
    517  1.1      leo 		add_sicallback((si_farg)fdcpseudointr, intr_arg, 0);
    518  1.1      leo 	}
    519  1.1      leo 	else {
    520  1.1      leo 		s = splbio();
    521  1.1      leo 		(void) fdcintr(intr_arg);
    522  1.1      leo 		splx(s);
    523  1.1      leo 	}
    524  1.1      leo }
    525  1.1      leo 
    526  1.1      leo __inline struct fd_type *
    527  1.1      leo fd_dev_to_type(fd, dev)
    528  1.1      leo 	struct fd_softc *fd;
    529  1.1      leo 	dev_t dev;
    530  1.1      leo {
    531  1.1      leo 	int type = FDTYPE(dev);
    532  1.1      leo 
    533  1.1      leo 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    534  1.1      leo 		return NULL;
    535  1.1      leo 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    536  1.1      leo }
    537  1.1      leo 
    538  1.1      leo void
    539  1.1      leo fdstrategy(bp)
    540  1.1      leo 	register struct buf *bp;	/* IO operation to perform */
    541  1.1      leo {
    542  1.1      leo 	struct fd_softc *fd = hdfd_cd.cd_devs[FDUNIT(bp->b_dev)];
    543  1.1      leo 	int sz;
    544  1.1      leo  	int s;
    545  1.1      leo 
    546  1.1      leo 	/* Valid unit, controller, and request? */
    547  1.1      leo 	if (bp->b_blkno < 0 ||
    548  1.3      leo 	    ((bp->b_bcount % FDC_BSIZE) != 0 &&
    549  1.3      leo 	     (bp->b_flags & B_FORMAT) == 0)) {
    550  1.1      leo 		bp->b_error = EINVAL;
    551  1.1      leo 		goto bad;
    552  1.1      leo 	}
    553  1.1      leo 
    554  1.1      leo 	/* If it's a null transfer, return immediately. */
    555  1.1      leo 	if (bp->b_bcount == 0)
    556  1.1      leo 		goto done;
    557  1.1      leo 
    558  1.1      leo 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    559  1.1      leo 
    560  1.1      leo 	if (bp->b_blkno + sz > fd->sc_type->size) {
    561  1.1      leo 		sz = fd->sc_type->size - bp->b_blkno;
    562  1.1      leo 		if (sz == 0) {
    563  1.1      leo 			/* If exactly at end of disk, return EOF. */
    564  1.1      leo 			goto done;
    565  1.1      leo 		}
    566  1.1      leo 		if (sz < 0) {
    567  1.1      leo 			/* If past end of disk, return EINVAL. */
    568  1.1      leo 			bp->b_error = EINVAL;
    569  1.1      leo 			goto bad;
    570  1.1      leo 		}
    571  1.1      leo 		/* Otherwise, truncate request. */
    572  1.1      leo 		bp->b_bcount = sz << DEV_BSHIFT;
    573  1.1      leo 	}
    574  1.1      leo 
    575  1.1      leo  	bp->b_cylin = bp->b_blkno / (FDC_BSIZE/DEV_BSIZE) / fd->sc_type->seccyl;
    576  1.1      leo 
    577  1.1      leo #ifdef FD_DEBUG
    578  1.1      leo 	printf("fdstrategy: b_blkno %d b_bcount %ld blkno %ld cylin %ld sz"
    579  1.1      leo 		" %d\n", bp->b_blkno, bp->b_bcount, (long)fd->sc_blkno,
    580  1.1      leo 		bp->b_cylin, sz);
    581  1.1      leo #endif
    582  1.1      leo 
    583  1.1      leo 	/* Queue transfer on drive, activate drive and controller if idle. */
    584  1.1      leo 	s = splbio();
    585  1.1      leo 	disksort(&fd->sc_q, bp);
    586  1.1      leo 	untimeout(fd_motor_off, fd); /* a good idea */
    587  1.1      leo 	if (!fd->sc_q.b_active)
    588  1.1      leo 		fdstart(fd);
    589  1.1      leo #ifdef DIAGNOSTIC
    590  1.1      leo 	else {
    591  1.1      leo 		struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    592  1.1      leo 		if (fdc->sc_state == DEVIDLE) {
    593  1.1      leo 			printf("fdstrategy: controller inactive\n");
    594  1.1      leo 			fdcstart(fdc);
    595  1.1      leo 		}
    596  1.1      leo 	}
    597  1.1      leo #endif
    598  1.1      leo 	splx(s);
    599  1.1      leo 	return;
    600  1.1      leo 
    601  1.1      leo bad:
    602  1.1      leo 	bp->b_flags |= B_ERROR;
    603  1.1      leo done:
    604  1.1      leo 	/* Toss transfer; we're done early. */
    605  1.1      leo 	bp->b_resid = bp->b_bcount;
    606  1.1      leo 	biodone(bp);
    607  1.1      leo }
    608  1.1      leo 
    609  1.1      leo void
    610  1.1      leo fdstart(fd)
    611  1.1      leo 	struct fd_softc *fd;
    612  1.1      leo {
    613  1.1      leo 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    614  1.1      leo 	int active = fdc->sc_drives.tqh_first != 0;
    615  1.1      leo 
    616  1.1      leo 	/* Link into controller queue. */
    617  1.1      leo 	fd->sc_q.b_active = 1;
    618  1.1      leo 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    619  1.1      leo 
    620  1.1      leo 	/* If controller not already active, start it. */
    621  1.1      leo 	if (!active)
    622  1.1      leo 		fdcstart(fdc);
    623  1.1      leo }
    624  1.1      leo 
    625  1.1      leo void
    626  1.1      leo fdfinish(fd, bp)
    627  1.1      leo 	struct fd_softc *fd;
    628  1.1      leo 	struct buf *bp;
    629  1.1      leo {
    630  1.1      leo 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    631  1.1      leo 
    632  1.1      leo 	/*
    633  1.1      leo 	 * Move this drive to the end of the queue to give others a `fair'
    634  1.1      leo 	 * chance.  We only force a switch if N operations are completed while
    635  1.1      leo 	 * another drive is waiting to be serviced, since there is a long motor
    636  1.1      leo 	 * startup delay whenever we switch.
    637  1.1      leo 	 */
    638  1.1      leo 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    639  1.1      leo 		fd->sc_ops = 0;
    640  1.1      leo 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    641  1.1      leo 		if (bp->b_actf) {
    642  1.1      leo 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    643  1.1      leo 		} else
    644  1.1      leo 			fd->sc_q.b_active = 0;
    645  1.1      leo 	}
    646  1.1      leo 	bp->b_resid = fd->sc_bcount;
    647  1.1      leo 	fd->sc_skip = 0;
    648  1.1      leo 	fd->sc_q.b_actf = bp->b_actf;
    649  1.1      leo 
    650  1.1      leo 	biodone(bp);
    651  1.1      leo 	/* turn off motor 5s from now */
    652  1.1      leo 	timeout(fd_motor_off, fd, 5 * hz);
    653  1.1      leo 	fdc->sc_state = DEVIDLE;
    654  1.1      leo }
    655  1.1      leo 
    656  1.1      leo int
    657  1.1      leo fdread(dev, uio, flags)
    658  1.1      leo 	dev_t dev;
    659  1.1      leo 	struct uio *uio;
    660  1.1      leo 	int flags;
    661  1.1      leo {
    662  1.1      leo 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    663  1.1      leo }
    664  1.1      leo 
    665  1.1      leo int
    666  1.1      leo fdwrite(dev, uio, flags)
    667  1.1      leo 	dev_t dev;
    668  1.1      leo 	struct uio *uio;
    669  1.1      leo 	int flags;
    670  1.1      leo {
    671  1.1      leo 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    672  1.1      leo }
    673  1.1      leo 
    674  1.1      leo void
    675  1.1      leo fd_set_motor(fdc, reset)
    676  1.1      leo 	struct fdc_softc *fdc;
    677  1.1      leo 	int reset;
    678  1.1      leo {
    679  1.1      leo 	struct fd_softc *fd;
    680  1.1      leo 	u_char status;
    681  1.1      leo 	int n;
    682  1.1      leo 
    683  1.1      leo 	if ((fd = fdc->sc_drives.tqh_first) != NULL)
    684  1.1      leo 		status = fd->sc_drive;
    685  1.1      leo 	else
    686  1.1      leo 		status = 0;
    687  1.1      leo 	if (!reset)
    688  1.1      leo 		status |= FDO_FRST | FDO_FDMAEN;
    689  1.1      leo 	for (n = 0; n < 4; n++)
    690  1.1      leo 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    691  1.1      leo 			status |= FDO_MOEN(n);
    692  1.1      leo 	wrt_fdc_reg(fdout, status);
    693  1.1      leo }
    694  1.1      leo 
    695  1.1      leo void
    696  1.1      leo fd_motor_off(arg)
    697  1.1      leo 	void *arg;
    698  1.1      leo {
    699  1.1      leo 	struct fd_softc *fd = arg;
    700  1.1      leo 	int s;
    701  1.1      leo 
    702  1.1      leo 	s = splbio();
    703  1.1      leo 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    704  1.1      leo 	fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0);
    705  1.1      leo 	splx(s);
    706  1.1      leo }
    707  1.1      leo 
    708  1.1      leo void
    709  1.1      leo fd_motor_on(arg)
    710  1.1      leo 	void *arg;
    711  1.1      leo {
    712  1.1      leo 	struct fd_softc *fd = arg;
    713  1.1      leo 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    714  1.1      leo 	int s;
    715  1.1      leo 
    716  1.1      leo 	s = splbio();
    717  1.1      leo 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    718  1.1      leo 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
    719  1.1      leo 		(void) fdcintr(fdc);
    720  1.1      leo 	splx(s);
    721  1.1      leo }
    722  1.1      leo 
    723  1.1      leo int
    724  1.1      leo fdcresult(fdc)
    725  1.1      leo 	struct fdc_softc *fdc;
    726  1.1      leo {
    727  1.1      leo 	u_char i;
    728  1.1      leo 	int j = 100000,
    729  1.1      leo 	    n = 0;
    730  1.1      leo 
    731  1.1      leo 	for (; j; j--) {
    732  1.1      leo 		i = rd_fdc_reg(fdsts) & (NE7_DIO | NE7_RQM | NE7_CB);
    733  1.1      leo 		if (i == NE7_RQM)
    734  1.1      leo 			return n;
    735  1.1      leo 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    736  1.1      leo 			if (n >= sizeof(fdc->sc_status)) {
    737  1.1      leo 				log(LOG_ERR, "fdcresult: overrun\n");
    738  1.1      leo 				return -1;
    739  1.1      leo 			}
    740  1.1      leo 			fdc->sc_status[n++] = rd_fdc_reg(fddata);
    741  1.1      leo 		}
    742  1.3      leo 		else delay(10);
    743  1.1      leo 	}
    744  1.1      leo 	log(LOG_ERR, "fdcresult: timeout\n");
    745  1.1      leo 	return -1;
    746  1.1      leo }
    747  1.1      leo 
    748  1.1      leo int
    749  1.1      leo out_fdc(x)
    750  1.1      leo 	u_char x;
    751  1.1      leo {
    752  1.1      leo 	int i = 100000;
    753  1.1      leo 
    754  1.3      leo 	while (((rd_fdc_reg(fdsts) & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0)
    755  1.3      leo 		delay(1);
    756  1.1      leo 	if (i <= 0)
    757  1.1      leo 		return -1;
    758  1.1      leo 	wrt_fdc_reg(fddata, x);
    759  1.1      leo 	return 0;
    760  1.1      leo }
    761  1.1      leo 
    762  1.1      leo int
    763  1.1      leo fdopen(dev, flags, mode, p)
    764  1.1      leo 	dev_t dev;
    765  1.1      leo 	int flags;
    766  1.1      leo 	int mode;
    767  1.1      leo 	struct proc *p;
    768  1.1      leo {
    769  1.1      leo  	int unit;
    770  1.1      leo 	struct fd_softc *fd;
    771  1.1      leo 	struct fd_type *type;
    772  1.1      leo 
    773  1.1      leo 	unit = FDUNIT(dev);
    774  1.1      leo 	if (unit >= hdfd_cd.cd_ndevs)
    775  1.1      leo 		return ENXIO;
    776  1.1      leo 	fd = hdfd_cd.cd_devs[unit];
    777  1.1      leo 	if (fd == 0)
    778  1.1      leo 		return ENXIO;
    779  1.1      leo 	type = fd_dev_to_type(fd, dev);
    780  1.1      leo 	if (type == NULL)
    781  1.1      leo 		return ENXIO;
    782  1.1      leo 
    783  1.1      leo 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    784  1.1      leo 	    fd->sc_type != type)
    785  1.1      leo 		return EBUSY;
    786  1.1      leo 
    787  1.1      leo 	fd->sc_type = type;
    788  1.1      leo 	fd->sc_cylin = -1;
    789  1.1      leo 	fd->sc_flags |= FD_OPEN;
    790  1.1      leo 
    791  1.1      leo 	return 0;
    792  1.1      leo }
    793  1.1      leo 
    794  1.1      leo int
    795  1.1      leo fdclose(dev, flags, mode, p)
    796  1.1      leo 	dev_t dev;
    797  1.1      leo 	int flags;
    798  1.1      leo 	int mode;
    799  1.1      leo 	struct proc *p;
    800  1.1      leo {
    801  1.1      leo 	struct fd_softc *fd = hdfd_cd.cd_devs[FDUNIT(dev)];
    802  1.1      leo 
    803  1.1      leo 	fd->sc_flags &= ~FD_OPEN;
    804  1.3      leo 	fd->sc_opts  &= ~(FDOPT_NORETRY|FDOPT_SILENT);
    805  1.1      leo 	return 0;
    806  1.1      leo }
    807  1.1      leo 
    808  1.1      leo void
    809  1.1      leo fdcstart(fdc)
    810  1.1      leo 	struct fdc_softc *fdc;
    811  1.1      leo {
    812  1.1      leo 
    813  1.1      leo #ifdef DIAGNOSTIC
    814  1.1      leo 	/* only got here if controller's drive queue was inactive; should
    815  1.1      leo 	   be in idle state */
    816  1.1      leo 	if (fdc->sc_state != DEVIDLE) {
    817  1.1      leo 		printf("fdcstart: not idle\n");
    818  1.1      leo 		return;
    819  1.1      leo 	}
    820  1.1      leo #endif
    821  1.1      leo 	(void) fdcintr(fdc);
    822  1.1      leo }
    823  1.1      leo 
    824  1.1      leo void
    825  1.1      leo fdcstatus(dv, n, s)
    826  1.1      leo 	struct device *dv;
    827  1.1      leo 	int n;
    828  1.1      leo 	char *s;
    829  1.1      leo {
    830  1.1      leo 	struct fdc_softc *fdc = (void *)dv->dv_parent;
    831  1.2  thorpej 	char bits[64];
    832  1.1      leo 
    833  1.1      leo 	if (n == 0) {
    834  1.1      leo 		out_fdc(NE7CMD_SENSEI);
    835  1.1      leo 		(void) fdcresult(fdc);
    836  1.1      leo 		n = 2;
    837  1.1      leo 	}
    838  1.1      leo 
    839  1.1      leo 	printf("%s: %s", dv->dv_xname, s);
    840  1.1      leo 
    841  1.1      leo 	switch (n) {
    842  1.1      leo 	case 0:
    843  1.1      leo 		printf("\n");
    844  1.1      leo 		break;
    845  1.1      leo 	case 2:
    846  1.2  thorpej 		printf(" (st0 %s cyl %d)\n",
    847  1.2  thorpej 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
    848  1.2  thorpej 		    bits, sizeof(bits)), fdc->sc_status[1]);
    849  1.1      leo 		break;
    850  1.1      leo 	case 7:
    851  1.2  thorpej 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
    852  1.2  thorpej 		    NE7_ST0BITS, bits, sizeof(bits)));
    853  1.2  thorpej 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
    854  1.2  thorpej 		    NE7_ST1BITS, bits, sizeof(bits)));
    855  1.2  thorpej 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
    856  1.2  thorpej 		    NE7_ST2BITS, bits, sizeof(bits)));
    857  1.2  thorpej 		printf(" cyl %d head %d sec %d)\n",
    858  1.1      leo 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    859  1.1      leo 		break;
    860  1.1      leo #ifdef DIAGNOSTIC
    861  1.1      leo 	default:
    862  1.1      leo 		printf("\nfdcstatus: weird size");
    863  1.1      leo 		break;
    864  1.1      leo #endif
    865  1.1      leo 	}
    866  1.1      leo }
    867  1.1      leo 
    868  1.1      leo void
    869  1.1      leo fdctimeout(arg)
    870  1.1      leo 	void *arg;
    871  1.1      leo {
    872  1.1      leo 	struct fdc_softc *fdc = arg;
    873  1.1      leo 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
    874  1.1      leo 	int s;
    875  1.1      leo 
    876  1.1      leo 	s = splbio();
    877  1.1      leo 	fdcstatus(&fd->sc_dev, 0, "timeout");
    878  1.1      leo 
    879  1.1      leo 	if (fd->sc_q.b_actf)
    880  1.1      leo 		fdc->sc_state++;
    881  1.1      leo 	else
    882  1.1      leo 		fdc->sc_state = DEVIDLE;
    883  1.1      leo 
    884  1.1      leo 	(void) fdcintr(fdc);
    885  1.1      leo 	splx(s);
    886  1.1      leo }
    887  1.1      leo 
    888  1.1      leo void
    889  1.1      leo fdcpseudointr(arg)
    890  1.1      leo 	void *arg;
    891  1.1      leo {
    892  1.1      leo 	int s;
    893  1.1      leo 
    894  1.1      leo 	/* Just ensure it has the right spl. */
    895  1.1      leo 	s = splbio();
    896  1.1      leo 	(void) fdcintr(arg);
    897  1.1      leo 	splx(s);
    898  1.1      leo }
    899  1.1      leo 
    900  1.1      leo int
    901  1.1      leo fdcintr(arg)
    902  1.1      leo 	void *arg;
    903  1.1      leo {
    904  1.3      leo 	struct fdc_softc	*fdc = arg;
    905  1.1      leo #define	st0	fdc->sc_status[0]
    906  1.1      leo #define	st1	fdc->sc_status[1]
    907  1.1      leo #define	cyl	fdc->sc_status[1]
    908  1.3      leo 
    909  1.3      leo 	struct fd_softc		*fd;
    910  1.3      leo 	struct buf		*bp;
    911  1.3      leo 	int			read, head, sec, i, nblks;
    912  1.3      leo 	struct fd_type		*type;
    913  1.3      leo 	struct ne7_fd_formb	*finfo = NULL;
    914  1.1      leo 
    915  1.1      leo loop:
    916  1.1      leo 	/* Is there a drive for the controller to do a transfer with? */
    917  1.1      leo 	fd = fdc->sc_drives.tqh_first;
    918  1.1      leo 	if (fd == NULL) {
    919  1.1      leo 		fdc->sc_state = DEVIDLE;
    920  1.1      leo  		return 1;
    921  1.1      leo 	}
    922  1.1      leo 
    923  1.1      leo 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    924  1.1      leo 	bp = fd->sc_q.b_actf;
    925  1.1      leo 	if (bp == NULL) {
    926  1.1      leo 		fd->sc_ops = 0;
    927  1.1      leo 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    928  1.1      leo 		fd->sc_q.b_active = 0;
    929  1.1      leo 		goto loop;
    930  1.1      leo 	}
    931  1.1      leo 
    932  1.3      leo 	if (bp->b_flags & B_FORMAT)
    933  1.3      leo 		finfo = (struct ne7_fd_formb *)bp->b_data;
    934  1.3      leo 
    935  1.1      leo 	switch (fdc->sc_state) {
    936  1.1      leo 	case DEVIDLE:
    937  1.1      leo 		fdc->sc_errors = 0;
    938  1.1      leo 		fdc->sc_overruns = 0;
    939  1.1      leo 		fd->sc_skip = 0;
    940  1.1      leo 		fd->sc_bcount = bp->b_bcount;
    941  1.1      leo 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
    942  1.1      leo 		untimeout(fd_motor_off, fd);
    943  1.1      leo 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
    944  1.1      leo 			fdc->sc_state = MOTORWAIT;
    945  1.1      leo 			return 1;
    946  1.1      leo 		}
    947  1.1      leo 		if ((fd->sc_flags & FD_MOTOR) == 0) {
    948  1.1      leo 			/* Turn on the motor, being careful about pairing. */
    949  1.1      leo 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
    950  1.1      leo 			if (ofd && ofd->sc_flags & FD_MOTOR) {
    951  1.1      leo 				untimeout(fd_motor_off, ofd);
    952  1.1      leo 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    953  1.1      leo 			}
    954  1.1      leo 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
    955  1.1      leo 			fd_set_motor(fdc, 0);
    956  1.1      leo 			fdc->sc_state = MOTORWAIT;
    957  1.1      leo 			/* Allow .25s for motor to stabilize. */
    958  1.1      leo 			timeout(fd_motor_on, fd, hz / 4);
    959  1.1      leo 			return 1;
    960  1.1      leo 		}
    961  1.1      leo 		/* Make sure the right drive is selected. */
    962  1.1      leo 		fd_set_motor(fdc, 0);
    963  1.1      leo 
    964  1.1      leo 		/* fall through */
    965  1.1      leo 	case DOSEEK:
    966  1.1      leo 	doseek:
    967  1.1      leo 		if (fd->sc_cylin == bp->b_cylin)
    968  1.1      leo 			goto doio;
    969  1.1      leo 
    970  1.1      leo 		out_fdc(NE7CMD_SPECIFY);/* specify command */
    971  1.1      leo 		out_fdc(fd->sc_type->steprate);
    972  1.1      leo 		out_fdc(0x7);	/* XXX head load time == 6ms - non-dma */
    973  1.1      leo 
    974  1.1      leo 		fdc_ienable();
    975  1.1      leo 
    976  1.1      leo 		out_fdc(NE7CMD_SEEK);	/* seek function */
    977  1.1      leo 		out_fdc(fd->sc_drive);	/* drive number */
    978  1.1      leo 		out_fdc(bp->b_cylin * fd->sc_type->step);
    979  1.1      leo 
    980  1.1      leo 		fd->sc_cylin = -1;
    981  1.1      leo 		fdc->sc_state = SEEKWAIT;
    982  1.1      leo 
    983  1.1      leo 		fd->sc_dk.dk_seek++;
    984  1.1      leo 		disk_busy(&fd->sc_dk);
    985  1.1      leo 
    986  1.1      leo 		timeout(fdctimeout, fdc, 4 * hz);
    987  1.1      leo 		return 1;
    988  1.1      leo 
    989  1.1      leo 	case DOIO:
    990  1.1      leo 	doio:
    991  1.3      leo 		if (finfo)
    992  1.3      leo 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
    993  1.3      leo 				      (char *)finfo;
    994  1.3      leo 
    995  1.1      leo 		type  = fd->sc_type;
    996  1.1      leo 		sec   = fd->sc_blkno % type->seccyl;
    997  1.1      leo 		head  = sec / type->sectrac;
    998  1.1      leo 		sec  -= head * type->sectrac;
    999  1.1      leo 		nblks = type->sectrac - sec;
   1000  1.1      leo 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1001  1.1      leo 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
   1002  1.1      leo 		fd->sc_nblks  = nblks;
   1003  1.3      leo 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
   1004  1.1      leo #ifdef DIAGNOSTIC
   1005  1.1      leo 		{
   1006  1.1      leo 		     int block;
   1007  1.1      leo 
   1008  1.1      leo 		     block = (fd->sc_cylin * type->heads + head)
   1009  1.1      leo 				* type->sectrac + sec;
   1010  1.1      leo 		     if (block != fd->sc_blkno) {
   1011  1.1      leo 			 printf("fdcintr: block %d != blkno %d\n",
   1012  1.1      leo 						block, fd->sc_blkno);
   1013  1.1      leo #ifdef DDB
   1014  1.1      leo 			 Debugger();
   1015  1.1      leo #endif
   1016  1.1      leo 		     }
   1017  1.1      leo 		}
   1018  1.1      leo #endif
   1019  1.1      leo 		read = bp->b_flags & B_READ ? 1 : 0;
   1020  1.1      leo 
   1021  1.1      leo 		/*
   1022  1.1      leo 		 * Setup pseudo-dma address & count
   1023  1.1      leo 		 */
   1024  1.1      leo 		fddmaaddr = bp->b_data + fd->sc_skip;
   1025  1.1      leo 		fddmalen  = fd->sc_nbytes;
   1026  1.1      leo 
   1027  1.1      leo 		wrt_fdc_reg(fdctl, type->rate);
   1028  1.1      leo #ifdef FD_DEBUG
   1029  1.1      leo 		printf("fdcintr: %s drive %d track %d head %d sec %d"
   1030  1.1      leo 			" nblks %d\n", read ? "read" : "write",
   1031  1.1      leo 			fd->sc_drive, fd->sc_cylin, head, sec, nblks);
   1032  1.1      leo #endif
   1033  1.1      leo 		fdc_ienable();
   1034  1.1      leo 
   1035  1.3      leo 		if (finfo) {
   1036  1.3      leo 			/* formatting */
   1037  1.3      leo 			if (out_fdc(NE7CMD_FORMAT) < 0) {
   1038  1.3      leo 				fdc->sc_errors = 4;
   1039  1.3      leo 				fdcretry(fdc);
   1040  1.3      leo 				goto loop;
   1041  1.3      leo 			}
   1042  1.3      leo 			out_fdc((head << 2) | fd->sc_drive);
   1043  1.3      leo 			out_fdc(finfo->fd_formb_secshift);
   1044  1.3      leo 			out_fdc(finfo->fd_formb_nsecs);
   1045  1.3      leo 			out_fdc(finfo->fd_formb_gaplen);
   1046  1.3      leo 			out_fdc(finfo->fd_formb_fillbyte);
   1047  1.3      leo 		} else {
   1048  1.3      leo 			if (read)
   1049  1.3      leo 				out_fdc(NE7CMD_READ);	/* READ */
   1050  1.3      leo 			else
   1051  1.3      leo 				out_fdc(NE7CMD_WRITE);	/* WRITE */
   1052  1.3      leo 			out_fdc((head << 2) | fd->sc_drive);
   1053  1.3      leo 			out_fdc(fd->sc_cylin);		/* track	 */
   1054  1.3      leo 			out_fdc(head);			/* head		 */
   1055  1.3      leo 			out_fdc(sec + 1);		/* sector +1	 */
   1056  1.3      leo 			out_fdc(type->secsize);		/* sector size   */
   1057  1.3      leo 			out_fdc(sec + nblks);		/* last sectors	 */
   1058  1.3      leo 			out_fdc(type->gap1);		/* gap1 size	 */
   1059  1.3      leo 			out_fdc(type->datalen);		/* data length	 */
   1060  1.3      leo 		}
   1061  1.1      leo 		fdc->sc_state = IOCOMPLETE;
   1062  1.1      leo 
   1063  1.1      leo 		disk_busy(&fd->sc_dk);
   1064  1.1      leo 
   1065  1.1      leo 		/* allow 2 seconds for operation */
   1066  1.1      leo 		timeout(fdctimeout, fdc, 2 * hz);
   1067  1.1      leo 		return 1;				/* will return later */
   1068  1.1      leo 
   1069  1.1      leo 	case SEEKWAIT:
   1070  1.1      leo 		untimeout(fdctimeout, fdc);
   1071  1.1      leo 		fdc->sc_state = SEEKCOMPLETE;
   1072  1.1      leo 		/* allow 1/50 second for heads to settle */
   1073  1.1      leo 		timeout(fdcpseudointr, fdc, hz / 50);
   1074  1.1      leo 		return 1;
   1075  1.1      leo 
   1076  1.1      leo 	case SEEKCOMPLETE:
   1077  1.1      leo 		disk_unbusy(&fd->sc_dk, 0);	/* no data on seek */
   1078  1.1      leo 
   1079  1.1      leo 		/* Make sure seek really happened. */
   1080  1.1      leo 		out_fdc(NE7CMD_SENSEI);
   1081  1.1      leo 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
   1082  1.1      leo 		    cyl != bp->b_cylin * fd->sc_type->step) {
   1083  1.1      leo #ifdef FD_DEBUG
   1084  1.1      leo 			fdcstatus(&fd->sc_dev, 2, "seek failed");
   1085  1.1      leo #endif
   1086  1.1      leo 			fdcretry(fdc);
   1087  1.1      leo 			goto loop;
   1088  1.1      leo 		}
   1089  1.1      leo 		fd->sc_cylin = bp->b_cylin;
   1090  1.1      leo 		goto doio;
   1091  1.1      leo 
   1092  1.1      leo 	case IOTIMEDOUT:
   1093  1.1      leo 	case SEEKTIMEDOUT:
   1094  1.1      leo 	case RECALTIMEDOUT:
   1095  1.1      leo 	case RESETTIMEDOUT:
   1096  1.1      leo 		fdcretry(fdc);
   1097  1.1      leo 		goto loop;
   1098  1.1      leo 
   1099  1.1      leo 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1100  1.1      leo 		untimeout(fdctimeout, fdc);
   1101  1.1      leo 
   1102  1.1      leo 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
   1103  1.1      leo 
   1104  1.1      leo 		if (fdcresult(fdc) != 7 || (st1 & 0x37) != 0) {
   1105  1.1      leo 			/*
   1106  1.1      leo 			 * As the damn chip doesn't seem to have a FIFO,
   1107  1.1      leo 			 * accept a few overruns as a fact of life *sigh*
   1108  1.1      leo 			 */
   1109  1.1      leo 			if ((st1 & 0x10) && (++fdc->sc_overruns < 4)) {
   1110  1.1      leo 				fdc->sc_state = DOSEEK;
   1111  1.1      leo 				goto loop;
   1112  1.1      leo 			}
   1113  1.1      leo #ifdef FD_DEBUG
   1114  1.1      leo 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
   1115  1.1      leo 			    "read failed" : "write failed");
   1116  1.1      leo 			printf("blkno %d nblks %d\n",
   1117  1.1      leo 			    fd->sc_blkno, fd->sc_nblks);
   1118  1.1      leo #endif
   1119  1.1      leo 			fdcretry(fdc);
   1120  1.1      leo 			goto loop;
   1121  1.1      leo 		}
   1122  1.1      leo 		if (fdc->sc_errors) {
   1123  1.1      leo 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1124  1.1      leo 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1125  1.1      leo 			printf("\n");
   1126  1.1      leo 			fdc->sc_errors = 0;
   1127  1.1      leo 		}
   1128  1.1      leo 		fdc->sc_overruns = 0;
   1129  1.1      leo 		fd->sc_blkno += fd->sc_nblks;
   1130  1.1      leo 		fd->sc_skip += fd->sc_nbytes;
   1131  1.1      leo 		fd->sc_bcount -= fd->sc_nbytes;
   1132  1.3      leo 		if (!finfo && fd->sc_bcount > 0) {
   1133  1.1      leo 			bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
   1134  1.1      leo 			goto doseek;
   1135  1.1      leo 		}
   1136  1.1      leo 		fdfinish(fd, bp);
   1137  1.1      leo 		goto loop;
   1138  1.1      leo 
   1139  1.1      leo 	case DORESET:
   1140  1.1      leo 		/* try a reset, keep motor on */
   1141  1.1      leo 		fd_set_motor(fdc, 1);
   1142  1.1      leo 		delay(100);
   1143  1.1      leo 		fd_set_motor(fdc, 0);
   1144  1.1      leo 		fdc->sc_state = RESETCOMPLETE;
   1145  1.1      leo 		timeout(fdctimeout, fdc, hz / 2);
   1146  1.1      leo 		return 1;			/* will return later */
   1147  1.1      leo 
   1148  1.1      leo 	case RESETCOMPLETE:
   1149  1.1      leo 		untimeout(fdctimeout, fdc);
   1150  1.1      leo 		/* clear the controller output buffer */
   1151  1.1      leo 		for (i = 0; i < 4; i++) {
   1152  1.1      leo 			out_fdc(NE7CMD_SENSEI);
   1153  1.1      leo 			(void) fdcresult(fdc);
   1154  1.1      leo 		}
   1155  1.1      leo 
   1156  1.1      leo 		/* fall through */
   1157  1.1      leo 	case DORECAL:
   1158  1.1      leo 		fdc_ienable();
   1159  1.1      leo 
   1160  1.1      leo 		out_fdc(NE7CMD_RECAL);	/* recalibrate function */
   1161  1.1      leo 		out_fdc(fd->sc_drive);
   1162  1.1      leo 		fdc->sc_state = RECALWAIT;
   1163  1.1      leo 		timeout(fdctimeout, fdc, 5 * hz);
   1164  1.1      leo 		return 1;			/* will return later */
   1165  1.1      leo 
   1166  1.1      leo 	case RECALWAIT:
   1167  1.1      leo 		untimeout(fdctimeout, fdc);
   1168  1.1      leo 		fdc->sc_state = RECALCOMPLETE;
   1169  1.1      leo 		/* allow 1/30 second for heads to settle */
   1170  1.1      leo 		timeout(fdcpseudointr, fdc, hz / 30);
   1171  1.1      leo 		return 1;			/* will return later */
   1172  1.1      leo 
   1173  1.1      leo 	case RECALCOMPLETE:
   1174  1.1      leo 		out_fdc(NE7CMD_SENSEI);
   1175  1.1      leo 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1176  1.1      leo #ifdef FD_DEBUG
   1177  1.1      leo 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
   1178  1.1      leo #endif
   1179  1.1      leo 			fdcretry(fdc);
   1180  1.1      leo 			goto loop;
   1181  1.1      leo 		}
   1182  1.1      leo 		fd->sc_cylin = 0;
   1183  1.1      leo 		goto doseek;
   1184  1.1      leo 
   1185  1.1      leo 	case MOTORWAIT:
   1186  1.1      leo 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1187  1.1      leo 			return 1;		/* time's not up yet */
   1188  1.1      leo 		goto doseek;
   1189  1.1      leo 
   1190  1.1      leo 	default:
   1191  1.1      leo 		fdcstatus(&fd->sc_dev, 0, "stray interrupt");
   1192  1.1      leo 		return 1;
   1193  1.1      leo 	}
   1194  1.1      leo #ifdef DIAGNOSTIC
   1195  1.1      leo 	panic("fdcintr: impossible");
   1196  1.1      leo #endif
   1197  1.1      leo #undef	st0
   1198  1.1      leo #undef	st1
   1199  1.1      leo #undef	cyl
   1200  1.1      leo }
   1201  1.1      leo 
   1202  1.1      leo void
   1203  1.1      leo fdcretry(fdc)
   1204  1.1      leo 	struct fdc_softc *fdc;
   1205  1.1      leo {
   1206  1.2  thorpej 	char bits[64];
   1207  1.1      leo 	struct fd_softc *fd;
   1208  1.1      leo 	struct buf *bp;
   1209  1.1      leo 
   1210  1.1      leo 	fd = fdc->sc_drives.tqh_first;
   1211  1.1      leo 	bp = fd->sc_q.b_actf;
   1212  1.1      leo 
   1213  1.3      leo 	if (fd->sc_opts & FDOPT_NORETRY)
   1214  1.3      leo 	    goto fail;
   1215  1.3      leo 
   1216  1.1      leo 	switch (fdc->sc_errors) {
   1217  1.1      leo 	case 0:
   1218  1.1      leo 		/* try again */
   1219  1.1      leo 		fdc->sc_state = DOSEEK;
   1220  1.1      leo 		break;
   1221  1.1      leo 
   1222  1.1      leo 	case 1: case 2: case 3:
   1223  1.1      leo 		/* didn't work; try recalibrating */
   1224  1.1      leo 		fdc->sc_state = DORECAL;
   1225  1.1      leo 		break;
   1226  1.1      leo 
   1227  1.1      leo 	case 4:
   1228  1.1      leo 		/* still no go; reset the bastard */
   1229  1.1      leo 		fdc->sc_state = DORESET;
   1230  1.1      leo 		break;
   1231  1.1      leo 
   1232  1.1      leo 	default:
   1233  1.3      leo 	fail:
   1234  1.3      leo 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
   1235  1.3      leo 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1236  1.3      leo 				fd->sc_skip / FDC_BSIZE,
   1237  1.3      leo 				(struct disklabel *)NULL);
   1238  1.3      leo 
   1239  1.3      leo 			printf(" (st0 %s",
   1240  1.3      leo 			       bitmask_snprintf(fdc->sc_status[0],
   1241  1.3      leo 						NE7_ST0BITS, bits,
   1242  1.3      leo 						sizeof(bits)));
   1243  1.3      leo 			printf(" st1 %s",
   1244  1.3      leo 			       bitmask_snprintf(fdc->sc_status[1],
   1245  1.3      leo 						NE7_ST1BITS, bits,
   1246  1.3      leo 						sizeof(bits)));
   1247  1.3      leo 			printf(" st2 %s",
   1248  1.3      leo 			       bitmask_snprintf(fdc->sc_status[2],
   1249  1.3      leo 						NE7_ST2BITS, bits,
   1250  1.3      leo 						sizeof(bits)));
   1251  1.3      leo 			printf(" cyl %d head %d sec %d)\n",
   1252  1.3      leo 			       fdc->sc_status[3],
   1253  1.3      leo 			       fdc->sc_status[4],
   1254  1.3      leo 			       fdc->sc_status[5]);
   1255  1.3      leo 		}
   1256  1.1      leo 		bp->b_flags |= B_ERROR;
   1257  1.1      leo 		bp->b_error = EIO;
   1258  1.1      leo 		fdfinish(fd, bp);
   1259  1.1      leo 	}
   1260  1.1      leo 	fdc->sc_errors++;
   1261  1.1      leo }
   1262  1.1      leo 
   1263  1.1      leo int
   1264  1.1      leo fdsize(dev)
   1265  1.1      leo 	dev_t dev;
   1266  1.1      leo {
   1267  1.1      leo 
   1268  1.1      leo 	/* Swapping to floppies would not make sense. */
   1269  1.1      leo 	return -1;
   1270  1.1      leo }
   1271  1.1      leo 
   1272  1.1      leo int
   1273  1.1      leo fddump(dev, blkno, va, size)
   1274  1.1      leo 	dev_t dev;
   1275  1.1      leo 	daddr_t blkno;
   1276  1.1      leo 	caddr_t va;
   1277  1.1      leo 	size_t size;
   1278  1.1      leo {
   1279  1.1      leo 
   1280  1.1      leo 	/* Not implemented. */
   1281  1.1      leo 	return ENXIO;
   1282  1.1      leo }
   1283  1.1      leo 
   1284  1.1      leo int
   1285  1.1      leo fdioctl(dev, cmd, addr, flag, p)
   1286  1.1      leo 	dev_t dev;
   1287  1.1      leo 	u_long cmd;
   1288  1.1      leo 	caddr_t addr;
   1289  1.1      leo 	int flag;
   1290  1.1      leo 	struct proc *p;
   1291  1.1      leo {
   1292  1.1      leo 	struct fd_softc		*fd;
   1293  1.1      leo 	struct disklabel	buffer;
   1294  1.1      leo 	struct cpu_disklabel	cpulab;
   1295  1.3      leo 	int			error;
   1296  1.3      leo 	struct fdformat_parms	*form_parms;
   1297  1.3      leo 	struct fdformat_cmd	*form_cmd;
   1298  1.3      leo 	struct ne7_fd_formb	fd_formb;
   1299  1.3      leo 	unsigned int		scratch;
   1300  1.3      leo 	int			il[FD_MAX_NSEC + 1];
   1301  1.3      leo 	register int		i, j;
   1302  1.1      leo 
   1303  1.1      leo 	fd = hdfd_cd.cd_devs[FDUNIT(dev)];
   1304  1.1      leo 
   1305  1.1      leo 	switch (cmd) {
   1306  1.1      leo 	case DIOCGDINFO:
   1307  1.1      leo 		bzero(&buffer, sizeof(buffer));
   1308  1.1      leo 		bzero(&cpulab, sizeof(cpulab));
   1309  1.1      leo 
   1310  1.1      leo 		buffer.d_secpercyl  = fd->sc_type->seccyl;
   1311  1.1      leo 		buffer.d_type       = DTYPE_FLOPPY;
   1312  1.1      leo 		buffer.d_secsize    = FDC_BSIZE;
   1313  1.1      leo 		buffer.d_secperunit = fd->sc_type->size;
   1314  1.1      leo 
   1315  1.1      leo 		if (readdisklabel(dev, fdstrategy, &buffer, &cpulab) != NULL)
   1316  1.1      leo 			return EINVAL;
   1317  1.1      leo 		*(struct disklabel *)addr = buffer;
   1318  1.1      leo 		return 0;
   1319  1.1      leo 
   1320  1.1      leo 	case DIOCWLABEL:
   1321  1.1      leo 		if ((flag & FWRITE) == 0)
   1322  1.1      leo 			return EBADF;
   1323  1.1      leo 		/* XXX do something */
   1324  1.1      leo 		return 0;
   1325  1.1      leo 
   1326  1.1      leo 	case DIOCWDINFO:
   1327  1.1      leo 		if ((flag & FWRITE) == 0)
   1328  1.1      leo 			return EBADF;
   1329  1.1      leo 
   1330  1.3      leo 		error = setdisklabel(&buffer, (struct disklabel *)addr, 0,NULL);
   1331  1.1      leo 		if (error)
   1332  1.1      leo 			return error;
   1333  1.1      leo 
   1334  1.1      leo 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1335  1.1      leo 		return error;
   1336  1.1      leo 
   1337  1.3      leo 	case FDIOCGETFORMAT:
   1338  1.3      leo 		form_parms = (struct fdformat_parms *)addr;
   1339  1.3      leo 		form_parms->fdformat_version = FDFORMAT_VERSION;
   1340  1.3      leo 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
   1341  1.3      leo 		form_parms->ncyl = fd->sc_type->tracks;
   1342  1.3      leo 		form_parms->nspt = fd->sc_type->sectrac;
   1343  1.3      leo 		form_parms->ntrk = fd->sc_type->heads;
   1344  1.3      leo 		form_parms->stepspercyl = fd->sc_type->step;
   1345  1.3      leo 		form_parms->gaplen = fd->sc_type->gap2;
   1346  1.3      leo 		form_parms->fillbyte = fd->sc_type->fillbyte;
   1347  1.3      leo 		form_parms->interleave = fd->sc_type->interleave;
   1348  1.3      leo 		switch (fd->sc_type->rate) {
   1349  1.3      leo 		case FDC_500KBPS:
   1350  1.3      leo 			form_parms->xfer_rate = 500 * 1024;
   1351  1.3      leo 			break;
   1352  1.3      leo 		case FDC_300KBPS:
   1353  1.3      leo 			form_parms->xfer_rate = 300 * 1024;
   1354  1.3      leo 			break;
   1355  1.3      leo 		case FDC_250KBPS:
   1356  1.3      leo 			form_parms->xfer_rate = 250 * 1024;
   1357  1.3      leo 			break;
   1358  1.3      leo 		default:
   1359  1.3      leo 			return EINVAL;
   1360  1.3      leo 		}
   1361  1.3      leo 		return 0;
   1362  1.3      leo 
   1363  1.3      leo 	case FDIOCSETFORMAT:
   1364  1.3      leo 		if((flag & FWRITE) == 0)
   1365  1.3      leo 			return EBADF;	/* must be opened for writing */
   1366  1.3      leo 		form_parms = (struct fdformat_parms *)addr;
   1367  1.3      leo 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
   1368  1.3      leo 			return EINVAL;	/* wrong version of formatting prog */
   1369  1.3      leo 
   1370  1.3      leo 		scratch = form_parms->nbps >> 7;
   1371  1.3      leo 		if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
   1372  1.3      leo 		    scratch & ~(1 << (ffs(scratch)-1)))
   1373  1.3      leo 			/* not a power-of-two multiple of 128 */
   1374  1.3      leo 			return EINVAL;
   1375  1.3      leo 
   1376  1.3      leo 		switch (form_parms->xfer_rate) {
   1377  1.3      leo 		case 500 * 1024:
   1378  1.3      leo 			fd->sc_type->rate = FDC_500KBPS;
   1379  1.3      leo 			break;
   1380  1.3      leo 		case 300 * 1024:
   1381  1.3      leo 			fd->sc_type->rate = FDC_300KBPS;
   1382  1.3      leo 			break;
   1383  1.3      leo 		case 250 * 1024:
   1384  1.3      leo 			fd->sc_type->rate = FDC_250KBPS;
   1385  1.3      leo 			break;
   1386  1.3      leo 		default:
   1387  1.3      leo 			return EINVAL;
   1388  1.3      leo 		}
   1389  1.3      leo 
   1390  1.3      leo 		if (form_parms->nspt > FD_MAX_NSEC ||
   1391  1.3      leo 		    form_parms->fillbyte > 0xff ||
   1392  1.3      leo 		    form_parms->interleave > 0xff)
   1393  1.3      leo 			return EINVAL;
   1394  1.3      leo 		fd->sc_type->sectrac = form_parms->nspt;
   1395  1.3      leo 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
   1396  1.3      leo 			return EINVAL;
   1397  1.3      leo 		fd->sc_type->heads = form_parms->ntrk;
   1398  1.3      leo 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
   1399  1.3      leo 		fd->sc_type->secsize = ffs(scratch)-1;
   1400  1.3      leo 		fd->sc_type->gap2 = form_parms->gaplen;
   1401  1.3      leo 		fd->sc_type->tracks = form_parms->ncyl;
   1402  1.3      leo 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
   1403  1.3      leo 			form_parms->nbps / DEV_BSIZE;
   1404  1.3      leo 		fd->sc_type->step = form_parms->stepspercyl;
   1405  1.3      leo 		fd->sc_type->fillbyte = form_parms->fillbyte;
   1406  1.3      leo 		fd->sc_type->interleave = form_parms->interleave;
   1407  1.3      leo 		return 0;
   1408  1.3      leo 
   1409  1.3      leo 	case FDIOCFORMAT_TRACK:
   1410  1.3      leo 		if((flag & FWRITE) == 0)
   1411  1.3      leo 			return EBADF;	/* must be opened for writing */
   1412  1.3      leo 		form_cmd = (struct fdformat_cmd *)addr;
   1413  1.3      leo 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
   1414  1.3      leo 			return EINVAL;	/* wrong version of formatting prog */
   1415  1.3      leo 
   1416  1.3      leo 		if (form_cmd->head >= fd->sc_type->heads ||
   1417  1.3      leo 		    form_cmd->cylinder >= fd->sc_type->tracks) {
   1418  1.3      leo 			return EINVAL;
   1419  1.3      leo 		}
   1420  1.3      leo 
   1421  1.3      leo 		fd_formb.head = form_cmd->head;
   1422  1.3      leo 		fd_formb.cyl = form_cmd->cylinder;
   1423  1.3      leo 		fd_formb.transfer_rate = fd->sc_type->rate;
   1424  1.3      leo 		fd_formb.fd_formb_secshift = fd->sc_type->secsize;
   1425  1.3      leo 		fd_formb.fd_formb_nsecs = fd->sc_type->sectrac;
   1426  1.3      leo 		fd_formb.fd_formb_gaplen = fd->sc_type->gap2;
   1427  1.3      leo 		fd_formb.fd_formb_fillbyte = fd->sc_type->fillbyte;
   1428  1.3      leo 
   1429  1.3      leo 		bzero(il,sizeof il);
   1430  1.3      leo 		for (j = 0, i = 1; i <= fd_formb.fd_formb_nsecs; i++) {
   1431  1.3      leo 			while (il[(j%fd_formb.fd_formb_nsecs)+1])
   1432  1.3      leo 				j++;
   1433  1.3      leo 			il[(j%fd_formb.fd_formb_nsecs)+1] = i;
   1434  1.3      leo 			j += fd->sc_type->interleave;
   1435  1.3      leo 		}
   1436  1.3      leo 		for (i = 0; i < fd_formb.fd_formb_nsecs; i++) {
   1437  1.3      leo 			fd_formb.fd_formb_cylno(i) = form_cmd->cylinder;
   1438  1.3      leo 			fd_formb.fd_formb_headno(i) = form_cmd->head;
   1439  1.3      leo 			fd_formb.fd_formb_secno(i) = il[i+1];
   1440  1.3      leo 			fd_formb.fd_formb_secsize(i) = fd->sc_type->secsize;
   1441  1.3      leo 		}
   1442  1.3      leo 	case FDIOCGETOPTS:		/* get drive options */
   1443  1.3      leo 		*(int *)addr = fd->sc_opts;
   1444  1.3      leo 		return 0;
   1445  1.3      leo 
   1446  1.3      leo 	case FDIOCSETOPTS:		/* set drive options */
   1447  1.3      leo 		fd->sc_opts = *(int *)addr;
   1448  1.3      leo 		return 0;
   1449  1.3      leo 
   1450  1.3      leo 
   1451  1.1      leo 	default:
   1452  1.1      leo 		return ENOTTY;
   1453  1.1      leo 	}
   1454  1.1      leo 
   1455  1.1      leo #ifdef DIAGNOSTIC
   1456  1.1      leo 	panic("fdioctl: impossible");
   1457  1.1      leo #endif
   1458  1.3      leo }
   1459  1.3      leo 
   1460  1.3      leo int
   1461  1.3      leo fdformat(dev, finfo, p)
   1462  1.3      leo 	dev_t dev;
   1463  1.3      leo 	struct ne7_fd_formb *finfo;
   1464  1.3      leo 	struct proc *p;
   1465  1.3      leo {
   1466  1.3      leo 	int rv = 0, s;
   1467  1.3      leo 	struct fd_softc *fd = hdfd_cd.cd_devs[FDUNIT(dev)];
   1468  1.3      leo 	struct fd_type *type = fd->sc_type;
   1469  1.3      leo 	struct buf *bp;
   1470  1.3      leo 
   1471  1.3      leo 	/* set up a buffer header for fdstrategy() */
   1472  1.3      leo 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
   1473  1.3      leo 	if(bp == 0)
   1474  1.3      leo 		return ENOBUFS;
   1475  1.3      leo 	PHOLD(p);
   1476  1.3      leo 	bzero((void *)bp, sizeof(struct buf));
   1477  1.3      leo 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
   1478  1.3      leo 	bp->b_proc = p;
   1479  1.3      leo 	bp->b_dev = dev;
   1480  1.3      leo 
   1481  1.3      leo 	/*
   1482  1.3      leo 	 * calculate a fake blkno, so fdstrategy() would initiate a
   1483  1.3      leo 	 * seek to the requested cylinder
   1484  1.3      leo 	 */
   1485  1.3      leo 	bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
   1486  1.3      leo 		       + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
   1487  1.3      leo 
   1488  1.3      leo 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
   1489  1.3      leo 	bp->b_data = (caddr_t)finfo;
   1490  1.3      leo 
   1491  1.3      leo #ifdef DEBUG
   1492  1.3      leo 	printf("fdformat: blkno %x count %x\n", bp->b_blkno, bp->b_bcount);
   1493  1.3      leo #endif
   1494  1.3      leo 
   1495  1.3      leo 	/* now do the format */
   1496  1.3      leo 	fdstrategy(bp);
   1497  1.3      leo 
   1498  1.3      leo 	/* ...and wait for it to complete */
   1499  1.3      leo 	s = splbio();
   1500  1.3      leo 	while(!(bp->b_flags & B_DONE)) {
   1501  1.3      leo 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
   1502  1.3      leo 		if (rv == EWOULDBLOCK)
   1503  1.3      leo 			break;
   1504  1.3      leo 	}
   1505  1.3      leo 	splx(s);
   1506  1.3      leo 
   1507  1.3      leo 	if (rv == EWOULDBLOCK) {
   1508  1.3      leo 		/* timed out */
   1509  1.3      leo 		rv = EIO;
   1510  1.3      leo 		biodone(bp);
   1511  1.3      leo 	}
   1512  1.3      leo 	if(bp->b_flags & B_ERROR) {
   1513  1.3      leo 		rv = bp->b_error;
   1514  1.3      leo 	}
   1515  1.3      leo 	PRELE(p);
   1516  1.3      leo 	free(bp, M_TEMP);
   1517  1.3      leo 	return rv;
   1518  1.1      leo }
   1519