Home | History | Annotate | Line # | Download | only in dev
fdc.c revision 1.7
      1  1.7  christos /*	$NetBSD: fdc.c,v 1.7 2007/03/04 07:54:07 christos Exp $	*/
      2  1.1   jnemeth 
      3  1.1   jnemeth /*-
      4  1.1   jnemeth  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.1   jnemeth  * All rights reserved.
      6  1.1   jnemeth  *
      7  1.1   jnemeth  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1   jnemeth  * by Paul Kranenburg.
      9  1.1   jnemeth  *
     10  1.1   jnemeth  * Redistribution and use in source and binary forms, with or without
     11  1.1   jnemeth  * modification, are permitted provided that the following conditions
     12  1.1   jnemeth  * are met:
     13  1.1   jnemeth  * 1. Redistributions of source code must retain the above copyright
     14  1.1   jnemeth  *    notice, this list of conditions and the following disclaimer.
     15  1.1   jnemeth  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   jnemeth  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   jnemeth  *    documentation and/or other materials provided with the distribution.
     18  1.1   jnemeth  * 3. All advertising materials mentioning features or use of this software
     19  1.1   jnemeth  *    must display the following acknowledgement:
     20  1.1   jnemeth  *        This product includes software developed by the NetBSD
     21  1.1   jnemeth  *        Foundation, Inc. and its contributors.
     22  1.1   jnemeth  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1   jnemeth  *    contributors may be used to endorse or promote products derived
     24  1.1   jnemeth  *    from this software without specific prior written permission.
     25  1.1   jnemeth  *
     26  1.1   jnemeth  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1   jnemeth  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1   jnemeth  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1   jnemeth  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1   jnemeth  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1   jnemeth  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1   jnemeth  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1   jnemeth  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1   jnemeth  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1   jnemeth  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1   jnemeth  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1   jnemeth  */
     38  1.1   jnemeth 
     39  1.1   jnemeth /*-
     40  1.1   jnemeth  * Copyright (c) 1990 The Regents of the University of California.
     41  1.1   jnemeth  * All rights reserved.
     42  1.1   jnemeth  *
     43  1.1   jnemeth  * This code is derived from software contributed to Berkeley by
     44  1.1   jnemeth  * Don Ahn.
     45  1.1   jnemeth  *
     46  1.1   jnemeth  * Redistribution and use in source and binary forms, with or without
     47  1.1   jnemeth  * modification, are permitted provided that the following conditions
     48  1.1   jnemeth  * are met:
     49  1.1   jnemeth  * 1. Redistributions of source code must retain the above copyright
     50  1.1   jnemeth  *    notice, this list of conditions and the following disclaimer.
     51  1.1   jnemeth  * 2. Redistributions in binary form must reproduce the above copyright
     52  1.1   jnemeth  *    notice, this list of conditions and the following disclaimer in the
     53  1.1   jnemeth  *    documentation and/or other materials provided with the distribution.
     54  1.1   jnemeth  * 3. Neither the name of the University nor the names of its contributors
     55  1.1   jnemeth  *    may be used to endorse or promote products derived from this software
     56  1.1   jnemeth  *    without specific prior written permission.
     57  1.1   jnemeth  *
     58  1.1   jnemeth  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.1   jnemeth  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.1   jnemeth  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.1   jnemeth  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.1   jnemeth  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.1   jnemeth  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.1   jnemeth  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.1   jnemeth  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.1   jnemeth  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.1   jnemeth  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.1   jnemeth  * SUCH DAMAGE.
     69  1.1   jnemeth  *
     70  1.1   jnemeth  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     71  1.1   jnemeth  */
     72  1.1   jnemeth 
     73  1.1   jnemeth /*-
     74  1.1   jnemeth  * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
     75  1.1   jnemeth  *
     76  1.1   jnemeth  * This code is derived from software contributed to Berkeley by
     77  1.1   jnemeth  * Don Ahn.
     78  1.1   jnemeth  *
     79  1.1   jnemeth  * Redistribution and use in source and binary forms, with or without
     80  1.1   jnemeth  * modification, are permitted provided that the following conditions
     81  1.1   jnemeth  * are met:
     82  1.1   jnemeth  * 1. Redistributions of source code must retain the above copyright
     83  1.1   jnemeth  *    notice, this list of conditions and the following disclaimer.
     84  1.1   jnemeth  * 2. Redistributions in binary form must reproduce the above copyright
     85  1.1   jnemeth  *    notice, this list of conditions and the following disclaimer in the
     86  1.1   jnemeth  *    documentation and/or other materials provided with the distribution.
     87  1.1   jnemeth  * 3. All advertising materials mentioning features or use of this software
     88  1.1   jnemeth  *    must display the following acknowledgement:
     89  1.1   jnemeth  *	This product includes software developed by the University of
     90  1.1   jnemeth  *	California, Berkeley and its contributors.
     91  1.1   jnemeth  * 4. Neither the name of the University nor the names of its contributors
     92  1.1   jnemeth  *    may be used to endorse or promote products derived from this software
     93  1.1   jnemeth  *    without specific prior written permission.
     94  1.1   jnemeth  *
     95  1.1   jnemeth  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     96  1.1   jnemeth  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     97  1.1   jnemeth  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     98  1.1   jnemeth  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     99  1.1   jnemeth  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    100  1.1   jnemeth  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    101  1.1   jnemeth  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    102  1.1   jnemeth  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    103  1.1   jnemeth  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    104  1.1   jnemeth  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    105  1.1   jnemeth  * SUCH DAMAGE.
    106  1.1   jnemeth  *
    107  1.1   jnemeth  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
    108  1.1   jnemeth  */
    109  1.1   jnemeth 
    110  1.1   jnemeth #include <sys/cdefs.h>
    111  1.7  christos __KERNEL_RCSID(0, "$NetBSD: fdc.c,v 1.7 2007/03/04 07:54:07 christos Exp $");
    112  1.1   jnemeth 
    113  1.1   jnemeth #include "opt_ddb.h"
    114  1.1   jnemeth #include "opt_md.h"
    115  1.1   jnemeth 
    116  1.1   jnemeth #include <sys/param.h>
    117  1.1   jnemeth #include <sys/systm.h>
    118  1.1   jnemeth #include <sys/callout.h>
    119  1.1   jnemeth #include <sys/kernel.h>
    120  1.1   jnemeth #include <sys/file.h>
    121  1.1   jnemeth #include <sys/ioctl.h>
    122  1.1   jnemeth #include <sys/device.h>
    123  1.1   jnemeth #include <sys/disklabel.h>
    124  1.1   jnemeth #include <sys/disk.h>
    125  1.1   jnemeth #include <sys/fdio.h>
    126  1.1   jnemeth #include <sys/buf.h>
    127  1.1   jnemeth #include <sys/bufq.h>
    128  1.1   jnemeth #include <sys/malloc.h>
    129  1.1   jnemeth #include <sys/proc.h>
    130  1.1   jnemeth #include <sys/uio.h>
    131  1.1   jnemeth #include <sys/stat.h>
    132  1.1   jnemeth #include <sys/syslog.h>
    133  1.1   jnemeth #include <sys/queue.h>
    134  1.1   jnemeth #include <sys/conf.h>
    135  1.1   jnemeth 
    136  1.1   jnemeth #include <dev/cons.h>
    137  1.1   jnemeth 
    138  1.1   jnemeth #include <uvm/uvm_extern.h>
    139  1.1   jnemeth 
    140  1.1   jnemeth #include <machine/autoconf.h>
    141  1.1   jnemeth #include <machine/intr.h>
    142  1.1   jnemeth 
    143  1.3   jnemeth #ifdef SUN4
    144  1.3   jnemeth #include <sparc/sparc/auxreg.h>
    145  1.3   jnemeth #include <sparc/dev/fdreg.h>
    146  1.3   jnemeth #include <sparc/dev/fdvar.h>
    147  1.3   jnemeth #elif SUN4U
    148  1.1   jnemeth #include <dev/ebus/ebusreg.h>
    149  1.1   jnemeth #include <dev/ebus/ebusvar.h>
    150  1.1   jnemeth /* #include <sparc/sparc/auxreg.h> */
    151  1.1   jnemeth #include <sparc64/dev/auxioreg.h>
    152  1.1   jnemeth #include <sparc64/dev/auxiovar.h>
    153  1.1   jnemeth #include <sparc64/dev/fdcreg.h>
    154  1.1   jnemeth #include <sparc64/dev/fdcvar.h>
    155  1.3   jnemeth #endif
    156  1.1   jnemeth 
    157  1.4   jnemeth #include <prop/proplib.h>
    158  1.4   jnemeth 
    159  1.1   jnemeth #define FDUNIT(dev)	(minor(dev) / 8)
    160  1.1   jnemeth #define FDTYPE(dev)	(minor(dev) % 8)
    161  1.1   jnemeth 
    162  1.1   jnemeth #ifdef SUN4U
    163  1.1   jnemeth #define FTC_FLIP \
    164  1.1   jnemeth 	do { \
    165  1.1   jnemeth 		auxio_fd_control(AUXIO_LED_FTC); \
    166  1.1   jnemeth 		auxio_fd_control(0); \
    167  1.1   jnemeth 	} while (0)
    168  1.1   jnemeth #else
    169  1.1   jnemeth #define FTC_FLIP
    170  1.1   jnemeth #endif
    171  1.1   jnemeth 
    172  1.2   reinoud /* (mis)use device use flag to identify format operation */
    173  1.2   reinoud #define B_FORMAT B_DEVPRIVATE
    174  1.1   jnemeth 
    175  1.1   jnemeth #define FD_DEBUG
    176  1.1   jnemeth #ifdef FD_DEBUG
    177  1.1   jnemeth int	fdc_debug = 0;
    178  1.1   jnemeth #endif
    179  1.1   jnemeth 
    180  1.1   jnemeth enum fdc_state {
    181  1.1   jnemeth 	DEVIDLE = 0,
    182  1.1   jnemeth 	MOTORWAIT,	/*  1 */
    183  1.1   jnemeth 	DOSEEK,		/*  2 */
    184  1.1   jnemeth 	SEEKWAIT,	/*  3 */
    185  1.1   jnemeth 	SEEKTIMEDOUT,	/*  4 */
    186  1.1   jnemeth 	SEEKCOMPLETE,	/*  5 */
    187  1.1   jnemeth 	DOIO,		/*  6 */
    188  1.1   jnemeth 	IOCOMPLETE,	/*  7 */
    189  1.1   jnemeth 	IOTIMEDOUT,	/*  8 */
    190  1.1   jnemeth 	IOCLEANUPWAIT,	/*  9 */
    191  1.1   jnemeth 	IOCLEANUPTIMEDOUT,/*10 */
    192  1.1   jnemeth 	DORESET,	/* 11 */
    193  1.1   jnemeth 	RESETCOMPLETE,	/* 12 */
    194  1.1   jnemeth 	RESETTIMEDOUT,	/* 13 */
    195  1.1   jnemeth 	DORECAL,	/* 14 */
    196  1.1   jnemeth 	RECALWAIT,	/* 15 */
    197  1.1   jnemeth 	RECALTIMEDOUT,	/* 16 */
    198  1.1   jnemeth 	RECALCOMPLETE,	/* 17 */
    199  1.1   jnemeth 	DODSKCHG,	/* 18 */
    200  1.1   jnemeth 	DSKCHGWAIT,	/* 19 */
    201  1.1   jnemeth 	DSKCHGTIMEDOUT,	/* 20 */
    202  1.1   jnemeth };
    203  1.1   jnemeth 
    204  1.1   jnemeth /* software state, per controller */
    205  1.1   jnemeth struct fdc_softc {
    206  1.1   jnemeth 	struct device	sc_dev;		/* boilerplate */
    207  1.1   jnemeth 	bus_space_tag_t	sc_bustag;
    208  1.1   jnemeth 
    209  1.1   jnemeth 	struct callout sc_timo_ch;	/* timeout callout */
    210  1.1   jnemeth 	struct callout sc_intr_ch;	/* pseudo-intr callout */
    211  1.1   jnemeth 
    212  1.1   jnemeth 	struct fd_softc *sc_fd[4];	/* pointers to children */
    213  1.1   jnemeth 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    214  1.1   jnemeth 	enum fdc_state	sc_state;
    215  1.1   jnemeth 	int		sc_flags;
    216  1.1   jnemeth #define FDC_82077		0x01
    217  1.1   jnemeth #define FDC_NEEDHEADSETTLE	0x02
    218  1.1   jnemeth #define FDC_EIS			0x04
    219  1.1   jnemeth #define FDC_NEEDMOTORWAIT	0x08
    220  1.1   jnemeth #define FDC_NOEJECT		0x10
    221  1.1   jnemeth #define FDC_EBUS		0x20
    222  1.1   jnemeth 	int		sc_errors;		/* number of retries so far */
    223  1.1   jnemeth 	int		sc_overruns;		/* number of DMA overruns */
    224  1.1   jnemeth 	int		sc_cfg;			/* current configuration */
    225  1.1   jnemeth 	struct fdcio	sc_io;
    226  1.1   jnemeth #define sc_handle	sc_io.fdcio_handle
    227  1.1   jnemeth #define sc_reg_msr	sc_io.fdcio_reg_msr
    228  1.1   jnemeth #define sc_reg_fifo	sc_io.fdcio_reg_fifo
    229  1.1   jnemeth #define sc_reg_dor	sc_io.fdcio_reg_dor
    230  1.1   jnemeth #define sc_reg_dir	sc_io.fdcio_reg_dir
    231  1.1   jnemeth #define sc_reg_drs	sc_io.fdcio_reg_msr
    232  1.1   jnemeth #define sc_itask	sc_io.fdcio_itask
    233  1.1   jnemeth #define sc_istatus	sc_io.fdcio_istatus
    234  1.1   jnemeth #define sc_data		sc_io.fdcio_data
    235  1.1   jnemeth #define sc_tc		sc_io.fdcio_tc
    236  1.1   jnemeth #define sc_nstat	sc_io.fdcio_nstat
    237  1.1   jnemeth #define sc_status	sc_io.fdcio_status
    238  1.1   jnemeth #define sc_intrcnt	sc_io.fdcio_intrcnt
    239  1.1   jnemeth 
    240  1.1   jnemeth 	void		*sc_sicookie;	/* softintr(9) cookie */
    241  1.1   jnemeth };
    242  1.1   jnemeth 
    243  1.3   jnemeth #ifdef SUN4
    244  1.1   jnemeth extern	struct fdcio	*fdciop;	/* I/O descriptor used in fdintr.s */
    245  1.1   jnemeth #endif
    246  1.1   jnemeth 
    247  1.1   jnemeth /* controller driver configuration */
    248  1.3   jnemeth #ifdef SUN4
    249  1.3   jnemeth int	fdcmatch_mainbus(struct device *, struct cfdata *, void*);
    250  1.3   jnemeth int	fdcmatch_obio(struct device *, struct cfdata *, void *);
    251  1.3   jnemeth void	fdcattach_mainbus(struct device *, struct device *, void *);
    252  1.3   jnemeth void	fdcattach_obio(struct device *, struct device *, void *);
    253  1.3   jnemeth #elif SUN4U
    254  1.1   jnemeth int	fdcmatch_sbus(struct device *, struct cfdata *, void *);
    255  1.1   jnemeth int	fdcmatch_ebus(struct device *, struct cfdata *, void *);
    256  1.1   jnemeth void	fdcattach_sbus(struct device *, struct device *, void *);
    257  1.1   jnemeth void	fdcattach_ebus(struct device *, struct device *, void *);
    258  1.3   jnemeth #endif
    259  1.1   jnemeth 
    260  1.1   jnemeth int	fdcattach(struct fdc_softc *, int);
    261  1.1   jnemeth 
    262  1.3   jnemeth #ifdef SUN4
    263  1.3   jnemeth CFATTACH_DECL(fdc_mainbus, sizeof(struct fdc_softc),
    264  1.3   jnemeth     fdcmatch_mainbus, fdcattach_mainbus, NULL, NULL);
    265  1.3   jnemeth 
    266  1.3   jnemeth CFATTACH_DECL(fdc_obio, sizeof(struct fdc_softc),
    267  1.3   jnemeth     fdcmatch_obio, fdcattach_obio, NULL, NULL);
    268  1.3   jnemeth #elif SUN4U
    269  1.1   jnemeth CFATTACH_DECL(fdc_sbus, sizeof(struct fdc_softc),
    270  1.1   jnemeth     fdcmatch_sbus, fdcattach_sbus, NULL, NULL);
    271  1.1   jnemeth 
    272  1.1   jnemeth CFATTACH_DECL(fdc_ebus, sizeof(struct fdc_softc),
    273  1.1   jnemeth     fdcmatch_ebus, fdcattach_ebus, NULL, NULL);
    274  1.3   jnemeth #endif
    275  1.1   jnemeth 
    276  1.1   jnemeth inline struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
    277  1.1   jnemeth 
    278  1.1   jnemeth /*
    279  1.1   jnemeth  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    280  1.1   jnemeth  * we tell them apart.
    281  1.1   jnemeth  */
    282  1.1   jnemeth struct fd_type {
    283  1.1   jnemeth 	int	sectrac;	/* sectors per track */
    284  1.1   jnemeth 	int	heads;		/* number of heads */
    285  1.1   jnemeth 	int	seccyl;		/* sectors per cylinder */
    286  1.1   jnemeth 	int	secsize;	/* size code for sectors */
    287  1.1   jnemeth 	int	datalen;	/* data len when secsize = 0 */
    288  1.1   jnemeth 	int	steprate;	/* step rate and head unload time */
    289  1.1   jnemeth 	int	gap1;		/* gap len between sectors */
    290  1.1   jnemeth 	int	gap2;		/* formatting gap */
    291  1.1   jnemeth 	int	cylinders;	/* total num of cylinders */
    292  1.1   jnemeth 	int	size;		/* size of disk in sectors */
    293  1.1   jnemeth 	int	step;		/* steps per cylinder */
    294  1.1   jnemeth 	int	rate;		/* transfer speed code */
    295  1.1   jnemeth 	int	fillbyte;	/* format fill byte */
    296  1.1   jnemeth 	int	interleave;	/* interleave factor (formatting) */
    297  1.1   jnemeth 	const char *name;
    298  1.1   jnemeth };
    299  1.1   jnemeth 
    300  1.1   jnemeth /* The order of entries in the following table is important -- BEWARE! */
    301  1.1   jnemeth struct fd_type fd_types[] = {
    302  1.1   jnemeth 	{ 18,2,36,2,0xff,0xcf,0x1b,0x54,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette */
    303  1.1   jnemeth 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5" 720kB diskette */
    304  1.1   jnemeth 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
    305  1.1   jnemeth 	{  8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,0xf6,1, "1.2MB/NEC" } /* 1.2 MB japanese format */
    306  1.1   jnemeth };
    307  1.1   jnemeth 
    308  1.1   jnemeth /* software state, per disk (with up to 4 disks per ctlr) */
    309  1.1   jnemeth struct fd_softc {
    310  1.1   jnemeth 	struct device	sc_dv;		/* generic device info */
    311  1.1   jnemeth 	struct disk	sc_dk;		/* generic disk info */
    312  1.1   jnemeth 
    313  1.1   jnemeth 	struct fd_type *sc_deftype;	/* default type descriptor */
    314  1.1   jnemeth 	struct fd_type *sc_type;	/* current type descriptor */
    315  1.1   jnemeth 
    316  1.1   jnemeth 	struct callout sc_motoron_ch;
    317  1.1   jnemeth 	struct callout sc_motoroff_ch;
    318  1.1   jnemeth 
    319  1.1   jnemeth 	daddr_t	sc_blkno;	/* starting block number */
    320  1.1   jnemeth 	int sc_bcount;		/* byte count left */
    321  1.1   jnemeth 	int sc_skip;		/* bytes already transferred */
    322  1.1   jnemeth 	int sc_nblks;		/* number of blocks currently transferring */
    323  1.1   jnemeth 	int sc_nbytes;		/* number of bytes currently transferring */
    324  1.1   jnemeth 
    325  1.1   jnemeth 	int sc_drive;		/* physical unit number */
    326  1.1   jnemeth 	int sc_flags;
    327  1.1   jnemeth #define	FD_OPEN		0x01		/* it's open */
    328  1.1   jnemeth #define	FD_MOTOR	0x02		/* motor should be on */
    329  1.1   jnemeth #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    330  1.1   jnemeth 	int sc_cylin;		/* where we think the head is */
    331  1.1   jnemeth 	int sc_opts;		/* user-set options */
    332  1.1   jnemeth 
    333  1.1   jnemeth 	void	*sc_sdhook;	/* shutdownhook cookie */
    334  1.1   jnemeth 
    335  1.1   jnemeth 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    336  1.1   jnemeth 	int sc_ops;		/* I/O ops since last switch */
    337  1.1   jnemeth 	struct bufq_state *sc_q;/* pending I/O requests */
    338  1.1   jnemeth 	int sc_active;		/* number of active I/O requests */
    339  1.1   jnemeth };
    340  1.1   jnemeth 
    341  1.1   jnemeth /* floppy driver configuration */
    342  1.1   jnemeth int	fdmatch(struct device *, struct cfdata *, void *);
    343  1.1   jnemeth void	fdattach(struct device *, struct device *, void *);
    344  1.1   jnemeth 
    345  1.1   jnemeth CFATTACH_DECL(fd, sizeof(struct fd_softc),
    346  1.1   jnemeth     fdmatch, fdattach, NULL, NULL);
    347  1.1   jnemeth 
    348  1.1   jnemeth extern struct cfdriver fd_cd;
    349  1.1   jnemeth 
    350  1.1   jnemeth dev_type_open(fdopen);
    351  1.1   jnemeth dev_type_close(fdclose);
    352  1.1   jnemeth dev_type_read(fdread);
    353  1.1   jnemeth dev_type_write(fdwrite);
    354  1.1   jnemeth dev_type_ioctl(fdioctl);
    355  1.1   jnemeth dev_type_strategy(fdstrategy);
    356  1.1   jnemeth 
    357  1.1   jnemeth const struct bdevsw fd_bdevsw = {
    358  1.1   jnemeth 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
    359  1.1   jnemeth };
    360  1.1   jnemeth 
    361  1.1   jnemeth const struct cdevsw fd_cdevsw = {
    362  1.1   jnemeth 	fdopen, fdclose, fdread, fdwrite, fdioctl,
    363  1.1   jnemeth 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    364  1.1   jnemeth };
    365  1.1   jnemeth 
    366  1.1   jnemeth void fdgetdisklabel(dev_t);
    367  1.1   jnemeth int fd_get_parms(struct fd_softc *);
    368  1.1   jnemeth void fdstrategy(struct buf *);
    369  1.1   jnemeth void fdstart(struct fd_softc *);
    370  1.1   jnemeth int fdprint(void *, const char *);
    371  1.1   jnemeth 
    372  1.1   jnemeth struct dkdriver fddkdriver = { fdstrategy, NULL };
    373  1.1   jnemeth 
    374  1.1   jnemeth struct	fd_type *fd_nvtotype(char *, int, int);
    375  1.1   jnemeth void	fd_set_motor(struct fdc_softc *);
    376  1.1   jnemeth void	fd_motor_off(void *);
    377  1.1   jnemeth void	fd_motor_on(void *);
    378  1.1   jnemeth int	fdcresult(struct fdc_softc *);
    379  1.1   jnemeth int	fdc_wrfifo(struct fdc_softc *, uint8_t);
    380  1.1   jnemeth void	fdcstart(struct fdc_softc *);
    381  1.1   jnemeth void	fdcstatus(struct fdc_softc *, const char *);
    382  1.1   jnemeth void	fdc_reset(struct fdc_softc *);
    383  1.1   jnemeth int	fdc_diskchange(struct fdc_softc *);
    384  1.1   jnemeth void	fdctimeout(void *);
    385  1.1   jnemeth void	fdcpseudointr(void *);
    386  1.1   jnemeth int	fdc_c_hwintr(void *);
    387  1.1   jnemeth void	fdchwintr(void);
    388  1.1   jnemeth void	fdcswintr(void *);
    389  1.1   jnemeth int	fdcstate(struct fdc_softc *);
    390  1.1   jnemeth void	fdcretry(struct fdc_softc *);
    391  1.1   jnemeth void	fdfinish(struct fd_softc *, struct buf *);
    392  1.1   jnemeth int	fdformat(dev_t, struct ne7_fd_formb *, struct proc *);
    393  1.1   jnemeth void	fd_do_eject(struct fd_softc *);
    394  1.1   jnemeth void	fd_mountroot_hook(struct device *);
    395  1.1   jnemeth static int fdconf(struct fdc_softc *);
    396  1.1   jnemeth static void establish_chip_type(
    397  1.1   jnemeth 		struct fdc_softc *,
    398  1.1   jnemeth 		bus_space_tag_t,
    399  1.1   jnemeth 		bus_addr_t,
    400  1.1   jnemeth 		bus_size_t,
    401  1.1   jnemeth 		bus_space_handle_t);
    402  1.4   jnemeth static void	fd_set_properties(struct fd_softc *);
    403  1.1   jnemeth 
    404  1.1   jnemeth #ifdef MEMORY_DISK_HOOKS
    405  1.6  christos int	fd_read_md_image(size_t *, void **);
    406  1.1   jnemeth #endif
    407  1.1   jnemeth 
    408  1.3   jnemeth #ifdef SUN4
    409  1.3   jnemeth #define OBP_FDNAME	(CPU_ISSUN4M ? "SUNW,fdtwo" : "fd")
    410  1.3   jnemeth 
    411  1.3   jnemeth int
    412  1.3   jnemeth fdcmatch_mainbus(struct device *parent, struct cfdata *match, void *aux)
    413  1.3   jnemeth {
    414  1.3   jnemeth 	struct mainbus_attach_args *ma = aux;
    415  1.3   jnemeth 
    416  1.3   jnemeth 	/*
    417  1.3   jnemeth 	 * Floppy controller is on mainbus on sun4c.
    418  1.3   jnemeth 	 */
    419  1.3   jnemeth 	if (!CPU_ISSUN4C)
    420  1.3   jnemeth 		return 0;
    421  1.3   jnemeth 
    422  1.3   jnemeth 	/* sun4c PROMs call the controller "fd" */
    423  1.3   jnemeth 	if (strcmp("fd", ma->ma_name) != 0)
    424  1.3   jnemeth 		return 0;
    425  1.3   jnemeth 
    426  1.3   jnemeth 	return bus_space_probe(ma->ma_bustag,
    427  1.3   jnemeth 			       ma->ma_paddr,
    428  1.3   jnemeth 			       1,	/* probe size */
    429  1.3   jnemeth 			       0,	/* offset */
    430  1.3   jnemeth 			       0,	/* flags */
    431  1.3   jnemeth 			       NULL, NULL);
    432  1.3   jnemeth }
    433  1.3   jnemeth 
    434  1.3   jnemeth int
    435  1.3   jnemeth fdcmatch_obio(struct device *parent, struct cfdata *match, void *aux)
    436  1.3   jnemeth {
    437  1.3   jnemeth 	union obio_attach_args *uoba = aux;
    438  1.3   jnemeth 	struct sbus_attach_args *sa;
    439  1.3   jnemeth 
    440  1.3   jnemeth 	/*
    441  1.3   jnemeth 	 * Floppy controller is on obio on sun4m.
    442  1.3   jnemeth 	 */
    443  1.3   jnemeth 	if (uoba->uoba_isobio4 != 0)
    444  1.3   jnemeth 		return 0;
    445  1.3   jnemeth 
    446  1.3   jnemeth 	sa = &uoba->uoba_sbus;
    447  1.3   jnemeth 
    448  1.3   jnemeth 	/* sun4m PROMs call the controller "SUNW,fdtwo" */
    449  1.3   jnemeth 	if (strcmp("SUNW,fdtwo", sa->sa_name) != 0)
    450  1.3   jnemeth 		return 0;
    451  1.3   jnemeth 
    452  1.3   jnemeth 	return bus_space_probe(sa->sa_bustag,
    453  1.3   jnemeth 			sbus_bus_addr(sa->sa_bustag,
    454  1.3   jnemeth 					sa->sa_slot, sa->sa_offset),
    455  1.3   jnemeth 			1,	/* probe size */
    456  1.3   jnemeth 			0,	/* offset */
    457  1.3   jnemeth 			0,	/* flags */
    458  1.3   jnemeth 			NULL, NULL);
    459  1.3   jnemeth }
    460  1.3   jnemeth 
    461  1.3   jnemeth #elif SUN4U
    462  1.3   jnemeth 
    463  1.1   jnemeth int
    464  1.1   jnemeth fdcmatch_sbus(struct device *parent, struct cfdata *match, void *aux)
    465  1.1   jnemeth {
    466  1.1   jnemeth 	struct sbus_attach_args *sa = aux;
    467  1.1   jnemeth 
    468  1.1   jnemeth 	return strcmp("SUNW,fdtwo", sa->sa_name) == 0;
    469  1.1   jnemeth }
    470  1.1   jnemeth 
    471  1.1   jnemeth int
    472  1.1   jnemeth fdcmatch_ebus(struct device *parent, struct cfdata *match, void *aux)
    473  1.1   jnemeth {
    474  1.1   jnemeth 	struct ebus_attach_args *ea = aux;
    475  1.1   jnemeth 
    476  1.1   jnemeth 	return strcmp("fdthree", ea->ea_name) == 0;
    477  1.1   jnemeth }
    478  1.3   jnemeth #endif
    479  1.1   jnemeth 
    480  1.1   jnemeth static void
    481  1.1   jnemeth establish_chip_type(struct fdc_softc *fdc,
    482  1.1   jnemeth 		    bus_space_tag_t tag, bus_addr_t addr, bus_size_t size,
    483  1.1   jnemeth 		    bus_space_handle_t handle)
    484  1.1   jnemeth {
    485  1.1   jnemeth 	uint8_t v;
    486  1.1   jnemeth 
    487  1.1   jnemeth 	/*
    488  1.1   jnemeth 	 * This hack from Chris Torek: apparently DOR really
    489  1.1   jnemeth 	 * addresses MSR/DRS on a 82072.
    490  1.1   jnemeth 	 * We used to rely on the VERSION command to tell the
    491  1.1   jnemeth 	 * difference (which did not work).
    492  1.1   jnemeth 	 */
    493  1.1   jnemeth 
    494  1.1   jnemeth 	/* First, check the size of the register bank */
    495  1.1   jnemeth 	if (size < 8)
    496  1.1   jnemeth 		/* It isn't a 82077 */
    497  1.1   jnemeth 		return;
    498  1.1   jnemeth 
    499  1.3   jnemeth #ifdef SUN4
    500  1.1   jnemeth 	/* Then probe the DOR register offset */
    501  1.1   jnemeth 	if (bus_space_probe(tag, addr,
    502  1.1   jnemeth 			    1,			/* probe size */
    503  1.1   jnemeth 			    FDREG77_DOR,	/* offset */
    504  1.1   jnemeth 			    0,			/* flags */
    505  1.1   jnemeth 			    NULL, NULL) == 0) {
    506  1.1   jnemeth 
    507  1.1   jnemeth 		/* It isn't a 82077 */
    508  1.1   jnemeth 		return;
    509  1.1   jnemeth 	}
    510  1.1   jnemeth #endif
    511  1.1   jnemeth 
    512  1.1   jnemeth 	v = bus_space_read_1(tag, handle, FDREG77_DOR);
    513  1.1   jnemeth 	if (v == NE7_RQM) {
    514  1.1   jnemeth 		/*
    515  1.1   jnemeth 		 * Value in DOR looks like it's really MSR
    516  1.1   jnemeth 		 */
    517  1.1   jnemeth 		bus_space_write_1(tag, handle, FDREG77_DOR, FDC_250KBPS);
    518  1.1   jnemeth 		v = bus_space_read_1(tag, handle, FDREG77_DOR);
    519  1.1   jnemeth 		if (v == NE7_RQM) {
    520  1.1   jnemeth 			/*
    521  1.1   jnemeth 			 * The value in the DOR didn't stick;
    522  1.1   jnemeth 			 * it isn't a 82077
    523  1.1   jnemeth 			 */
    524  1.1   jnemeth 			return;
    525  1.1   jnemeth 		}
    526  1.1   jnemeth 	}
    527  1.1   jnemeth 
    528  1.1   jnemeth 	fdc->sc_flags |= FDC_82077;
    529  1.1   jnemeth }
    530  1.1   jnemeth 
    531  1.1   jnemeth /*
    532  1.1   jnemeth  * Arguments passed between fdcattach and fdprobe.
    533  1.1   jnemeth  */
    534  1.1   jnemeth struct fdc_attach_args {
    535  1.1   jnemeth 	int fa_drive;
    536  1.1   jnemeth 	struct fd_type *fa_deftype;
    537  1.1   jnemeth };
    538  1.1   jnemeth 
    539  1.1   jnemeth /*
    540  1.1   jnemeth  * Print the location of a disk drive (called just before attaching the
    541  1.1   jnemeth  * the drive).  If `fdc' is not NULL, the drive was found but was not
    542  1.1   jnemeth  * in the system config file; print the drive name as well.
    543  1.1   jnemeth  * Return QUIET (config_find ignores this if the device was configured) to
    544  1.1   jnemeth  * avoid printing `fdN not configured' messages.
    545  1.1   jnemeth  */
    546  1.1   jnemeth int
    547  1.1   jnemeth fdprint(void *aux, const char *fdc)
    548  1.1   jnemeth {
    549  1.1   jnemeth 	register struct fdc_attach_args *fa = aux;
    550  1.1   jnemeth 
    551  1.1   jnemeth 	if (!fdc)
    552  1.1   jnemeth 		aprint_normal(" drive %d", fa->fa_drive);
    553  1.1   jnemeth 	return QUIET;
    554  1.1   jnemeth }
    555  1.1   jnemeth 
    556  1.1   jnemeth /*
    557  1.1   jnemeth  * Configure several parameters and features on the FDC.
    558  1.1   jnemeth  * Return 0 on success.
    559  1.1   jnemeth  */
    560  1.1   jnemeth static int
    561  1.1   jnemeth fdconf(struct fdc_softc *fdc)
    562  1.1   jnemeth {
    563  1.1   jnemeth 	int	vroom;
    564  1.1   jnemeth 
    565  1.1   jnemeth 	if (fdc_wrfifo(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
    566  1.1   jnemeth 		return -1;
    567  1.1   jnemeth 
    568  1.1   jnemeth 	/*
    569  1.1   jnemeth 	 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
    570  1.1   jnemeth 	 * the PROM thinks is appropriate.
    571  1.1   jnemeth 	 */
    572  1.1   jnemeth 	if ((vroom = fdc->sc_status[7]) == 0)
    573  1.1   jnemeth 		vroom = 0x64;
    574  1.1   jnemeth 
    575  1.1   jnemeth 	/* Configure controller to use FIFO and Implied Seek */
    576  1.1   jnemeth 	if (fdc_wrfifo(fdc, NE7CMD_CFG) != 0)
    577  1.1   jnemeth 		return -1;
    578  1.1   jnemeth 	if (fdc_wrfifo(fdc, vroom) != 0)
    579  1.1   jnemeth 		return -1;
    580  1.1   jnemeth 	if (fdc_wrfifo(fdc, fdc->sc_cfg) != 0)
    581  1.1   jnemeth 		return -1;
    582  1.1   jnemeth 	if (fdc_wrfifo(fdc, 0) != 0)	/* PRETRK */
    583  1.1   jnemeth 		return -1;
    584  1.1   jnemeth 	/* No result phase for the NE7CMD_CFG command */
    585  1.1   jnemeth 
    586  1.1   jnemeth 	if ((fdc->sc_flags & FDC_82077) != 0) {
    587  1.1   jnemeth 		/* Lock configuration across soft resets. */
    588  1.1   jnemeth 		if (fdc_wrfifo(fdc, NE7CMD_LOCK | CFG_LOCK) != 0 ||
    589  1.1   jnemeth 		    fdcresult(fdc) != 1) {
    590  1.1   jnemeth #ifdef DEBUG
    591  1.1   jnemeth 			printf("fdconf: CFGLOCK failed");
    592  1.1   jnemeth #endif
    593  1.1   jnemeth 			return -1;
    594  1.1   jnemeth 		}
    595  1.1   jnemeth 	}
    596  1.1   jnemeth 
    597  1.1   jnemeth 	return 0;
    598  1.1   jnemeth #if 0
    599  1.1   jnemeth 	if (fdc_wrfifo(fdc, NE7CMD_VERSION) == 0 &&
    600  1.1   jnemeth 	    fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
    601  1.1   jnemeth 		if (fdc_debug)
    602  1.1   jnemeth 			printf("[version cmd]");
    603  1.1   jnemeth 	}
    604  1.1   jnemeth #endif
    605  1.1   jnemeth }
    606  1.1   jnemeth 
    607  1.3   jnemeth #ifdef SUN4
    608  1.3   jnemeth void
    609  1.3   jnemeth fdcattach_mainbus(struct device *parent, struct device *self, void *aux)
    610  1.3   jnemeth {
    611  1.3   jnemeth 	struct fdc_softc *fdc = (void *)self;
    612  1.3   jnemeth 	struct mainbus_attach_args *ma = aux;
    613  1.3   jnemeth 
    614  1.3   jnemeth 	fdc->sc_bustag = ma->ma_bustag;
    615  1.3   jnemeth 
    616  1.3   jnemeth 	if (bus_space_map(
    617  1.3   jnemeth 			ma->ma_bustag,
    618  1.3   jnemeth 			ma->ma_paddr,
    619  1.3   jnemeth 			ma->ma_size,
    620  1.3   jnemeth 			BUS_SPACE_MAP_LINEAR,
    621  1.3   jnemeth 			&fdc->sc_handle) != 0) {
    622  1.3   jnemeth 		printf("%s: cannot map registers\n", self->dv_xname);
    623  1.3   jnemeth 		return;
    624  1.3   jnemeth 	}
    625  1.3   jnemeth 
    626  1.3   jnemeth 	establish_chip_type(fdc,
    627  1.3   jnemeth 			    ma->ma_bustag,
    628  1.3   jnemeth 			    ma->ma_paddr,
    629  1.3   jnemeth 			    ma->ma_size,
    630  1.3   jnemeth 			    fdc->sc_handle);
    631  1.3   jnemeth 
    632  1.3   jnemeth 	if (fdcattach(fdc, ma->ma_pri) != 0)
    633  1.3   jnemeth 		bus_space_unmap(ma->ma_bustag, fdc->sc_handle, ma->ma_size);
    634  1.3   jnemeth }
    635  1.3   jnemeth 
    636  1.3   jnemeth void
    637  1.3   jnemeth fdcattach_obio(struct device *parent, struct device *self, void *aux)
    638  1.3   jnemeth {
    639  1.3   jnemeth 	struct fdc_softc *fdc = (void *)self;
    640  1.3   jnemeth 	union obio_attach_args *uoba = aux;
    641  1.3   jnemeth 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    642  1.3   jnemeth 
    643  1.3   jnemeth 	if (sa->sa_nintr == 0) {
    644  1.3   jnemeth 		printf(": no interrupt line configured\n");
    645  1.3   jnemeth 		return;
    646  1.3   jnemeth 	}
    647  1.3   jnemeth 
    648  1.3   jnemeth 	fdc->sc_bustag = sa->sa_bustag;
    649  1.3   jnemeth 
    650  1.3   jnemeth 	if (sbus_bus_map(sa->sa_bustag,
    651  1.3   jnemeth 			 sa->sa_slot, sa->sa_offset, sa->sa_size,
    652  1.3   jnemeth 			 BUS_SPACE_MAP_LINEAR, &fdc->sc_handle) != 0) {
    653  1.3   jnemeth 		printf("%s: cannot map control registers\n",
    654  1.3   jnemeth 			self->dv_xname);
    655  1.3   jnemeth 		return;
    656  1.3   jnemeth 	}
    657  1.3   jnemeth 
    658  1.3   jnemeth 	establish_chip_type(fdc,
    659  1.3   jnemeth 		sa->sa_bustag,
    660  1.3   jnemeth 		sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset),
    661  1.3   jnemeth 		sa->sa_size,
    662  1.3   jnemeth 		fdc->sc_handle);
    663  1.3   jnemeth 
    664  1.3   jnemeth 	if (strcmp(prom_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
    665  1.3   jnemeth 		print(": no drives attached\n");
    666  1.3   jnemeth 		return;
    667  1.3   jnemeth 	}
    668  1.3   jnemeth 
    669  1.3   jnemeth 	if (fdcattach(fdc, sa->sa_pri) != 0)
    670  1.3   jnemeth 		bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
    671  1.3   jnemeth }
    672  1.3   jnemeth 
    673  1.3   jnemeth #elif SUN4U
    674  1.3   jnemeth 
    675  1.1   jnemeth void
    676  1.1   jnemeth fdcattach_sbus(struct device *parent, struct device *self, void *aux)
    677  1.1   jnemeth {
    678  1.1   jnemeth 	struct fdc_softc *fdc = (void *)self;
    679  1.1   jnemeth 	struct sbus_attach_args *sa = aux;
    680  1.1   jnemeth 
    681  1.1   jnemeth 	if (sa->sa_nintr == 0) {
    682  1.1   jnemeth 		printf(": no interrupt line configured\n");
    683  1.1   jnemeth 		return;
    684  1.1   jnemeth 	}
    685  1.1   jnemeth 
    686  1.1   jnemeth 	if (auxio_fd_control(0) != 0) {
    687  1.1   jnemeth 		printf(": can't attach before auxio\n");
    688  1.1   jnemeth 		return;
    689  1.1   jnemeth 	}
    690  1.1   jnemeth 
    691  1.1   jnemeth 	fdc->sc_bustag = sa->sa_bustag;
    692  1.1   jnemeth 
    693  1.1   jnemeth 	if (bus_space_map(sa->sa_bustag, BUS_ADDR(sa->sa_slot, sa->sa_offset),
    694  1.1   jnemeth 			  sa->sa_size, 0, &fdc->sc_handle) != 0) {
    695  1.1   jnemeth 		printf(": cannot map control registers\n");
    696  1.1   jnemeth 		return;
    697  1.1   jnemeth 	}
    698  1.1   jnemeth 
    699  1.1   jnemeth 	establish_chip_type(fdc,
    700  1.1   jnemeth 			    sa->sa_bustag,
    701  1.1   jnemeth 			    BUS_ADDR(sa->sa_slot, sa->sa_offset),
    702  1.1   jnemeth 			    sa->sa_size,
    703  1.1   jnemeth 			    fdc->sc_handle);
    704  1.1   jnemeth 
    705  1.1   jnemeth 	if (strcmp(prom_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
    706  1.1   jnemeth 		printf(": no drives attached\n");
    707  1.1   jnemeth 		return;
    708  1.1   jnemeth 	}
    709  1.1   jnemeth 
    710  1.1   jnemeth 	if (prom_getproplen(sa->sa_node, "manual") >= 0)
    711  1.1   jnemeth 		fdc->sc_flags |= FDC_NOEJECT;
    712  1.1   jnemeth 
    713  1.1   jnemeth 
    714  1.1   jnemeth 	if (fdcattach(fdc, sa->sa_pri) != 0)
    715  1.1   jnemeth 		bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
    716  1.1   jnemeth }
    717  1.1   jnemeth 
    718  1.1   jnemeth void
    719  1.1   jnemeth fdcattach_ebus(struct device *parent, struct device *self, void *aux)
    720  1.1   jnemeth {
    721  1.1   jnemeth 	struct fdc_softc *fdc = (void *)self;
    722  1.1   jnemeth 	struct ebus_attach_args *ea = aux;
    723  1.1   jnemeth 	int map_vaddr;
    724  1.1   jnemeth 
    725  1.1   jnemeth 	if (ea->ea_nintr == 0) {
    726  1.1   jnemeth 		printf(": no interrupt line configured\n");
    727  1.1   jnemeth 		return;
    728  1.1   jnemeth 	}
    729  1.1   jnemeth 
    730  1.1   jnemeth 	if (ea->ea_nreg < 3) {
    731  1.1   jnemeth 		printf(": expected 3 registers, only got %d\n",
    732  1.1   jnemeth 		    ea->ea_nreg);
    733  1.1   jnemeth 		return;
    734  1.1   jnemeth 	}
    735  1.1   jnemeth 
    736  1.1   jnemeth 	fdc->sc_bustag = ea->ea_bustag;
    737  1.1   jnemeth 
    738  1.1   jnemeth 	if (ea->ea_nvaddr > 0) {
    739  1.1   jnemeth 		sparc_promaddr_to_handle(ea->ea_bustag,
    740  1.1   jnemeth 		    ea->ea_vaddr[0], &fdc->sc_handle);
    741  1.1   jnemeth 		map_vaddr = 1;
    742  1.1   jnemeth 	} else if (bus_space_map(fdc->sc_bustag,
    743  1.1   jnemeth 	    EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
    744  1.1   jnemeth 	    ea->ea_reg[0].size, 0, &fdc->sc_handle) == 0) {
    745  1.1   jnemeth 		map_vaddr = 0;
    746  1.1   jnemeth 	} else {
    747  1.1   jnemeth 		printf(": can't map control registers\n");
    748  1.1   jnemeth 		return;
    749  1.1   jnemeth 	}
    750  1.1   jnemeth 
    751  1.1   jnemeth 	establish_chip_type(fdc,
    752  1.1   jnemeth 		fdc->sc_bustag,
    753  1.1   jnemeth 		map_vaddr ? ea->ea_vaddr[0] :
    754  1.1   jnemeth 		    EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
    755  1.1   jnemeth 		ea->ea_reg[0].size,
    756  1.1   jnemeth 		fdc->sc_handle);
    757  1.1   jnemeth 
    758  1.1   jnemeth 	fdc->sc_flags |= FDC_EBUS;
    759  1.1   jnemeth 
    760  1.1   jnemeth 	if (prom_getproplen(ea->ea_node, "manual") >= 0)
    761  1.1   jnemeth 		fdc->sc_flags |= FDC_NOEJECT;
    762  1.1   jnemeth 
    763  1.5   jnemeth 	if (fdcattach(fdc, ea->ea_intr[0]) != 0)
    764  1.5   jnemeth 		if (map_vaddr == 0)
    765  1.5   jnemeth 			bus_space_unmap(ea->ea_bustag, fdc->sc_handle,
    766  1.5   jnemeth 			    ea->ea_reg[0].size);
    767  1.1   jnemeth }
    768  1.3   jnemeth #endif
    769  1.1   jnemeth 
    770  1.1   jnemeth int
    771  1.1   jnemeth fdcattach(struct fdc_softc *fdc, int pri)
    772  1.1   jnemeth {
    773  1.1   jnemeth 	struct fdc_attach_args fa;
    774  1.1   jnemeth 	int drive_attached;
    775  1.1   jnemeth 	char code;
    776  1.1   jnemeth 
    777  1.1   jnemeth 	callout_init(&fdc->sc_timo_ch);
    778  1.1   jnemeth 	callout_init(&fdc->sc_intr_ch);
    779  1.1   jnemeth 
    780  1.1   jnemeth 	fdc->sc_state = DEVIDLE;
    781  1.1   jnemeth 	fdc->sc_itask = FDC_ITASK_NONE;
    782  1.1   jnemeth 	fdc->sc_istatus = FDC_ISTATUS_NONE;
    783  1.1   jnemeth 	fdc->sc_flags |= FDC_EIS;
    784  1.1   jnemeth 	TAILQ_INIT(&fdc->sc_drives);
    785  1.1   jnemeth 
    786  1.1   jnemeth 	if ((fdc->sc_flags & FDC_82077) != 0) {
    787  1.1   jnemeth 		fdc->sc_reg_msr = FDREG77_MSR;
    788  1.1   jnemeth 		fdc->sc_reg_fifo = FDREG77_FIFO;
    789  1.1   jnemeth 		fdc->sc_reg_dor = FDREG77_DOR;
    790  1.1   jnemeth 		fdc->sc_reg_dir = FDREG77_DIR;
    791  1.1   jnemeth 		code = '7';
    792  1.1   jnemeth 		fdc->sc_flags |= FDC_NEEDMOTORWAIT;
    793  1.1   jnemeth 	} else {
    794  1.1   jnemeth 		fdc->sc_reg_msr = FDREG72_MSR;
    795  1.1   jnemeth 		fdc->sc_reg_fifo = FDREG72_FIFO;
    796  1.1   jnemeth 		fdc->sc_reg_dor = 0;
    797  1.1   jnemeth 		code = '2';
    798  1.1   jnemeth 	}
    799  1.1   jnemeth 
    800  1.1   jnemeth 	/*
    801  1.1   jnemeth 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
    802  1.1   jnemeth 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
    803  1.1   jnemeth 	 */
    804  1.1   jnemeth 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
    805  1.1   jnemeth 	if (fdconf(fdc) != 0) {
    806  1.1   jnemeth 		printf(": no drives attached\n");
    807  1.1   jnemeth 		return -1;
    808  1.1   jnemeth 	}
    809  1.1   jnemeth 
    810  1.3   jnemeth #ifdef SUN4
    811  1.3   jnemeth 	fdc->sc_sicookie = softintr_establish(IPL_BIO, fdcswintr, fdc);
    812  1.3   jnemeth #elif SUN4U
    813  1.1   jnemeth 	fdc->sc_sicookie = softintr_establish(IPL_FDSOFT, fdcswintr, fdc);
    814  1.3   jnemeth #endif
    815  1.1   jnemeth 	if (fdc->sc_sicookie == NULL) {
    816  1.1   jnemeth 		printf("\n%s: cannot register soft interrupt handler\n",
    817  1.1   jnemeth 			fdc->sc_dev.dv_xname);
    818  1.5   jnemeth 		callout_stop(&fdc->sc_timo_ch);
    819  1.5   jnemeth 		callout_stop(&fdc->sc_intr_ch);
    820  1.1   jnemeth 		return -1;
    821  1.1   jnemeth 	}
    822  1.3   jnemeth #ifdef SUN4
    823  1.3   jnemeth 	printf(" softpri %d: chip 8207%c\n", IPL_SOFTFDC, code);
    824  1.3   jnemeth #elif SUN4U
    825  1.1   jnemeth 	printf(" softpri %d: chip 8207%c", PIL_FDSOFT, code);
    826  1.1   jnemeth 	if (fdc->sc_flags & FDC_NOEJECT)
    827  1.1   jnemeth 		printf(": manual eject");
    828  1.1   jnemeth 	printf("\n");
    829  1.3   jnemeth #endif
    830  1.1   jnemeth 
    831  1.5   jnemeth #ifdef SUN4
    832  1.5   jnemeth 	fdciop = &fdc->sc_io;
    833  1.5   jnemeth 	if (bus_intr_establish2(fdc->sc_bustag, pri, 0
    834  1.5   jnemeth 				fdc_c_hwintr, fdc, fdchwintr) == NULL) {
    835  1.5   jnemeth #elif SUN4U
    836  1.5   jnemeth 	if (bus_intr_establish(fdc->sc_bustag, pri, IPL_BIO,
    837  1.5   jnemeth 				fdc_c_hwintr, fdc) == NULL) {
    838  1.5   jnemeth #endif
    839  1.5   jnemeth 		printf("\n%s: cannot register interrupt handler\n",
    840  1.5   jnemeth 			fdc->sc_dev.dv_xname);
    841  1.5   jnemeth 		callout_stop(&fdc->sc_timo_ch);
    842  1.5   jnemeth 		callout_stop(&fdc->sc_intr_ch);
    843  1.5   jnemeth 		softintr_disestablish(fdc->sc_sicookie);
    844  1.5   jnemeth 		return -1;
    845  1.5   jnemeth 	}
    846  1.5   jnemeth 
    847  1.1   jnemeth 	evcnt_attach_dynamic(&fdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    848  1.1   jnemeth 	    fdc->sc_dev.dv_xname, "intr");
    849  1.1   jnemeth 
    850  1.1   jnemeth 	/* physical limit: four drives per controller. */
    851  1.1   jnemeth 	drive_attached = 0;
    852  1.1   jnemeth 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    853  1.1   jnemeth 		fa.fa_deftype = NULL;		/* unknown */
    854  1.1   jnemeth 		fa.fa_deftype = &fd_types[0];	/* XXX */
    855  1.1   jnemeth 		if (config_found(&fdc->sc_dev, (void *)&fa, fdprint) != NULL)
    856  1.1   jnemeth 			drive_attached = 1;
    857  1.1   jnemeth 	}
    858  1.1   jnemeth 
    859  1.1   jnemeth 	if (drive_attached == 0) {
    860  1.1   jnemeth 		/* XXX - dis-establish interrupts here */
    861  1.1   jnemeth 		/* return -1; */
    862  1.1   jnemeth 	}
    863  1.1   jnemeth 
    864  1.1   jnemeth 	return 0;
    865  1.1   jnemeth }
    866  1.1   jnemeth 
    867  1.1   jnemeth int
    868  1.1   jnemeth fdmatch(struct device *parent, struct cfdata *match, void *aux)
    869  1.1   jnemeth {
    870  1.1   jnemeth 	struct fdc_softc *fdc = (void *)parent;
    871  1.1   jnemeth 	bus_space_tag_t t = fdc->sc_bustag;
    872  1.1   jnemeth 	bus_space_handle_t h = fdc->sc_handle;
    873  1.1   jnemeth 	struct fdc_attach_args *fa = aux;
    874  1.1   jnemeth 	int drive = fa->fa_drive;
    875  1.1   jnemeth 	int n, ok;
    876  1.1   jnemeth 
    877  1.1   jnemeth 	if (drive > 0)
    878  1.1   jnemeth 		/* XXX - for now, punt on more than one drive */
    879  1.1   jnemeth 		return 0;
    880  1.1   jnemeth 
    881  1.1   jnemeth 	if ((fdc->sc_flags & FDC_82077) != 0) {
    882  1.1   jnemeth 		/* select drive and turn on motor */
    883  1.1   jnemeth 		bus_space_write_1(t, h, fdc->sc_reg_dor,
    884  1.1   jnemeth 				  drive | FDO_FRST | FDO_MOEN(drive));
    885  1.1   jnemeth 		/* wait for motor to spin up */
    886  1.1   jnemeth 		delay(250000);
    887  1.3   jnemeth #ifdef SUN4
    888  1.1   jnemeth 	} else {
    889  1.1   jnemeth 		auxregbisc(AUXIO4C_FDS, 0);
    890  1.1   jnemeth #endif
    891  1.1   jnemeth 	}
    892  1.1   jnemeth 	fdc->sc_nstat = 0;
    893  1.1   jnemeth 	fdc_wrfifo(fdc, NE7CMD_RECAL);
    894  1.1   jnemeth 	fdc_wrfifo(fdc, drive);
    895  1.1   jnemeth 
    896  1.1   jnemeth 	/* Wait for recalibration to complete */
    897  1.1   jnemeth 	for (n = 0; n < 10000; n++) {
    898  1.1   jnemeth 		uint8_t v;
    899  1.1   jnemeth 
    900  1.1   jnemeth 		delay(1000);
    901  1.1   jnemeth 		v = bus_space_read_1(t, h, fdc->sc_reg_msr);
    902  1.1   jnemeth 		if ((v & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
    903  1.1   jnemeth 			/* wait a bit longer till device *really* is ready */
    904  1.1   jnemeth 			delay(100000);
    905  1.1   jnemeth 			if (fdc_wrfifo(fdc, NE7CMD_SENSEI))
    906  1.1   jnemeth 				break;
    907  1.1   jnemeth 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
    908  1.1   jnemeth 				/*
    909  1.1   jnemeth 				 * Got `invalid command'; we interpret it
    910  1.1   jnemeth 				 * to mean that the re-calibrate hasn't in
    911  1.1   jnemeth 				 * fact finished yet
    912  1.1   jnemeth 				 */
    913  1.1   jnemeth 				continue;
    914  1.1   jnemeth 			break;
    915  1.1   jnemeth 		}
    916  1.1   jnemeth 	}
    917  1.1   jnemeth 	n = fdc->sc_nstat;
    918  1.1   jnemeth #ifdef FD_DEBUG
    919  1.1   jnemeth 	if (fdc_debug) {
    920  1.1   jnemeth 		int i;
    921  1.1   jnemeth 		printf("fdprobe: %d stati:", n);
    922  1.1   jnemeth 		for (i = 0; i < n; i++)
    923  1.1   jnemeth 			printf(" 0x%x", fdc->sc_status[i]);
    924  1.1   jnemeth 		printf("\n");
    925  1.1   jnemeth 	}
    926  1.1   jnemeth #endif
    927  1.1   jnemeth 	ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
    928  1.1   jnemeth 
    929  1.1   jnemeth 	/* turn off motor */
    930  1.1   jnemeth 	if ((fdc->sc_flags & FDC_82077) != 0) {
    931  1.1   jnemeth 		/* deselect drive and turn motor off */
    932  1.1   jnemeth 		bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
    933  1.3   jnemeth #ifdef SUN4
    934  1.1   jnemeth 	} else {
    935  1.1   jnemeth 		auxregbisc(0, AUXIO4C_FDS);
    936  1.1   jnemeth #endif
    937  1.1   jnemeth 	}
    938  1.1   jnemeth 
    939  1.1   jnemeth 	return ok;
    940  1.1   jnemeth }
    941  1.1   jnemeth 
    942  1.1   jnemeth /*
    943  1.1   jnemeth  * Controller is working, and drive responded.  Attach it.
    944  1.1   jnemeth  */
    945  1.1   jnemeth void
    946  1.1   jnemeth fdattach(struct device *parent, struct device *self, void *aux)
    947  1.1   jnemeth {
    948  1.1   jnemeth 	struct fdc_softc *fdc = (void *)parent;
    949  1.1   jnemeth 	struct fd_softc *fd = (void *)self;
    950  1.1   jnemeth 	struct fdc_attach_args *fa = aux;
    951  1.1   jnemeth 	struct fd_type *type = fa->fa_deftype;
    952  1.1   jnemeth 	int drive = fa->fa_drive;
    953  1.1   jnemeth 
    954  1.1   jnemeth 	callout_init(&fd->sc_motoron_ch);
    955  1.1   jnemeth 	callout_init(&fd->sc_motoroff_ch);
    956  1.1   jnemeth 
    957  1.1   jnemeth 	/* XXX Allow `flags' to override device type? */
    958  1.1   jnemeth 
    959  1.1   jnemeth 	if (type)
    960  1.1   jnemeth 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    961  1.1   jnemeth 		    type->cylinders, type->heads, type->sectrac);
    962  1.1   jnemeth 	else
    963  1.1   jnemeth 		printf(": density unknown\n");
    964  1.1   jnemeth 
    965  1.1   jnemeth 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
    966  1.1   jnemeth 	fd->sc_cylin = -1;
    967  1.1   jnemeth 	fd->sc_drive = drive;
    968  1.1   jnemeth 	fd->sc_deftype = type;
    969  1.1   jnemeth 	fdc->sc_fd[drive] = fd;
    970  1.1   jnemeth 
    971  1.1   jnemeth 	fdc_wrfifo(fdc, NE7CMD_SPECIFY);
    972  1.1   jnemeth 	fdc_wrfifo(fdc, type->steprate);
    973  1.1   jnemeth 	/* XXX head load time == 6ms */
    974  1.1   jnemeth 	fdc_wrfifo(fdc, 6 | NE7_SPECIFY_NODMA);
    975  1.1   jnemeth 
    976  1.1   jnemeth 	/*
    977  1.1   jnemeth 	 * Initialize and attach the disk structure.
    978  1.1   jnemeth 	 */
    979  1.1   jnemeth 	fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
    980  1.1   jnemeth 	fd->sc_dk.dk_driver = &fddkdriver;
    981  1.1   jnemeth 	disk_attach(&fd->sc_dk);
    982  1.1   jnemeth 
    983  1.1   jnemeth 	/*
    984  1.1   jnemeth 	 * Establish a mountroot_hook anyway in case we booted
    985  1.1   jnemeth 	 * with RB_ASKNAME and get selected as the boot device.
    986  1.1   jnemeth 	 */
    987  1.1   jnemeth 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
    988  1.1   jnemeth 
    989  1.4   jnemeth 	fd_set_properties(fd);
    990  1.4   jnemeth 
    991  1.1   jnemeth 	/* Make sure the drive motor gets turned off at shutdown time. */
    992  1.1   jnemeth 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
    993  1.1   jnemeth }
    994  1.1   jnemeth 
    995  1.1   jnemeth inline struct fd_type *
    996  1.1   jnemeth fd_dev_to_type(struct fd_softc *fd, dev_t dev)
    997  1.1   jnemeth {
    998  1.1   jnemeth 	int type = FDTYPE(dev);
    999  1.1   jnemeth 
   1000  1.1   jnemeth 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
   1001  1.1   jnemeth 		return NULL;
   1002  1.1   jnemeth 	return type ? &fd_types[type - 1] : fd->sc_deftype;
   1003  1.1   jnemeth }
   1004  1.1   jnemeth 
   1005  1.1   jnemeth void
   1006  1.1   jnemeth fdstrategy(struct buf *bp)
   1007  1.1   jnemeth {
   1008  1.1   jnemeth 	struct fd_softc *fd;
   1009  1.1   jnemeth 	int unit = FDUNIT(bp->b_dev);
   1010  1.1   jnemeth 	int sz;
   1011  1.1   jnemeth  	int s;
   1012  1.1   jnemeth 
   1013  1.1   jnemeth 	/* Valid unit, controller, and request? */
   1014  1.1   jnemeth 	if (unit >= fd_cd.cd_ndevs ||
   1015  1.1   jnemeth 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
   1016  1.1   jnemeth 	    bp->b_blkno < 0 ||
   1017  1.1   jnemeth 	    (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
   1018  1.1   jnemeth 	      (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
   1019  1.1   jnemeth 	     (bp->b_flags & B_FORMAT) == 0)) {
   1020  1.1   jnemeth 		bp->b_error = EINVAL;
   1021  1.1   jnemeth 		goto bad;
   1022  1.1   jnemeth 	}
   1023  1.1   jnemeth 
   1024  1.1   jnemeth 	/* If it's a null transfer, return immediately. */
   1025  1.1   jnemeth 	if (bp->b_bcount == 0)
   1026  1.1   jnemeth 		goto done;
   1027  1.1   jnemeth 
   1028  1.1   jnemeth 	sz = howmany(bp->b_bcount, DEV_BSIZE);
   1029  1.1   jnemeth 
   1030  1.1   jnemeth 	if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
   1031  1.1   jnemeth 		sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
   1032  1.1   jnemeth 		     - bp->b_blkno;
   1033  1.1   jnemeth 		if (sz == 0) {
   1034  1.1   jnemeth 			/* If exactly at end of disk, return EOF. */
   1035  1.1   jnemeth 			bp->b_resid = bp->b_bcount;
   1036  1.1   jnemeth 			goto done;
   1037  1.1   jnemeth 		}
   1038  1.1   jnemeth 		if (sz < 0) {
   1039  1.1   jnemeth 			/* If past end of disk, return EINVAL. */
   1040  1.1   jnemeth 			bp->b_error = EINVAL;
   1041  1.1   jnemeth 			goto bad;
   1042  1.1   jnemeth 		}
   1043  1.1   jnemeth 		/* Otherwise, truncate request. */
   1044  1.1   jnemeth 		bp->b_bcount = sz << DEV_BSHIFT;
   1045  1.1   jnemeth 	}
   1046  1.1   jnemeth 
   1047  1.1   jnemeth 	bp->b_rawblkno = bp->b_blkno;
   1048  1.1   jnemeth  	bp->b_cylinder = (bp->b_blkno * DEV_BSIZE) /
   1049  1.1   jnemeth 		      (FD_BSIZE(fd) * fd->sc_type->seccyl);
   1050  1.1   jnemeth 
   1051  1.1   jnemeth #ifdef FD_DEBUG
   1052  1.1   jnemeth 	if (fdc_debug > 1)
   1053  1.1   jnemeth 	    printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld cylin %d\n",
   1054  1.1   jnemeth 		    (long long)bp->b_blkno, bp->b_bcount,
   1055  1.1   jnemeth 		    (long long)fd->sc_blkno, bp->b_cylinder);
   1056  1.1   jnemeth #endif
   1057  1.1   jnemeth 
   1058  1.1   jnemeth 	/* Queue transfer on drive, activate drive and controller if idle. */
   1059  1.1   jnemeth 	s = splbio();
   1060  1.1   jnemeth 	BUFQ_PUT(fd->sc_q, bp);
   1061  1.1   jnemeth 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
   1062  1.1   jnemeth 	if (fd->sc_active == 0)
   1063  1.1   jnemeth 		fdstart(fd);
   1064  1.1   jnemeth #ifdef DIAGNOSTIC
   1065  1.1   jnemeth 	else {
   1066  1.1   jnemeth 		struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1067  1.1   jnemeth 		if (fdc->sc_state == DEVIDLE) {
   1068  1.1   jnemeth 			printf("fdstrategy: controller inactive\n");
   1069  1.1   jnemeth 			fdcstart(fdc);
   1070  1.1   jnemeth 		}
   1071  1.1   jnemeth 	}
   1072  1.1   jnemeth #endif
   1073  1.1   jnemeth 	splx(s);
   1074  1.1   jnemeth 	return;
   1075  1.1   jnemeth 
   1076  1.1   jnemeth bad:
   1077  1.1   jnemeth 	bp->b_flags |= B_ERROR;
   1078  1.1   jnemeth done:
   1079  1.1   jnemeth 	/* Toss transfer; we're done early. */
   1080  1.1   jnemeth 	biodone(bp);
   1081  1.1   jnemeth }
   1082  1.1   jnemeth 
   1083  1.1   jnemeth void
   1084  1.1   jnemeth fdstart(struct fd_softc *fd)
   1085  1.1   jnemeth {
   1086  1.1   jnemeth 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1087  1.1   jnemeth 	int active = fdc->sc_drives.tqh_first != 0;
   1088  1.1   jnemeth 
   1089  1.1   jnemeth 	/* Link into controller queue. */
   1090  1.1   jnemeth 	fd->sc_active = 1;
   1091  1.1   jnemeth 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
   1092  1.1   jnemeth 
   1093  1.1   jnemeth 	/* If controller not already active, start it. */
   1094  1.1   jnemeth 	if (!active)
   1095  1.1   jnemeth 		fdcstart(fdc);
   1096  1.1   jnemeth }
   1097  1.1   jnemeth 
   1098  1.1   jnemeth void
   1099  1.1   jnemeth fdfinish(struct fd_softc *fd, struct buf *bp)
   1100  1.1   jnemeth {
   1101  1.1   jnemeth 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1102  1.1   jnemeth 
   1103  1.1   jnemeth 	/*
   1104  1.1   jnemeth 	 * Move this drive to the end of the queue to give others a `fair'
   1105  1.1   jnemeth 	 * chance.  We only force a switch if N operations are completed while
   1106  1.1   jnemeth 	 * another drive is waiting to be serviced, since there is a long motor
   1107  1.1   jnemeth 	 * startup delay whenever we switch.
   1108  1.1   jnemeth 	 */
   1109  1.1   jnemeth 	(void)BUFQ_GET(fd->sc_q);
   1110  1.1   jnemeth 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
   1111  1.1   jnemeth 		fd->sc_ops = 0;
   1112  1.1   jnemeth 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1113  1.1   jnemeth 		if (BUFQ_PEEK(fd->sc_q) != NULL) {
   1114  1.1   jnemeth 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
   1115  1.1   jnemeth 		} else
   1116  1.1   jnemeth 			fd->sc_active = 0;
   1117  1.1   jnemeth 	}
   1118  1.1   jnemeth 	bp->b_resid = fd->sc_bcount;
   1119  1.1   jnemeth 	fd->sc_skip = 0;
   1120  1.1   jnemeth 
   1121  1.1   jnemeth 	biodone(bp);
   1122  1.1   jnemeth 	/* turn off motor 5s from now */
   1123  1.1   jnemeth 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
   1124  1.1   jnemeth 	fdc->sc_state = DEVIDLE;
   1125  1.1   jnemeth }
   1126  1.1   jnemeth 
   1127  1.1   jnemeth void
   1128  1.1   jnemeth fdc_reset(struct fdc_softc *fdc)
   1129  1.1   jnemeth {
   1130  1.1   jnemeth 	bus_space_tag_t t = fdc->sc_bustag;
   1131  1.1   jnemeth 	bus_space_handle_t h = fdc->sc_handle;
   1132  1.1   jnemeth 
   1133  1.1   jnemeth 	if ((fdc->sc_flags & FDC_82077) != 0) {
   1134  1.1   jnemeth 		bus_space_write_1(t, h, fdc->sc_reg_dor,
   1135  1.1   jnemeth 				  FDO_FDMAEN | FDO_MOEN(0));
   1136  1.1   jnemeth 	}
   1137  1.1   jnemeth 
   1138  1.1   jnemeth 	bus_space_write_1(t, h, fdc->sc_reg_drs, DRS_RESET);
   1139  1.1   jnemeth 	delay(10);
   1140  1.1   jnemeth 	bus_space_write_1(t, h, fdc->sc_reg_drs, 0);
   1141  1.1   jnemeth 
   1142  1.1   jnemeth 	if ((fdc->sc_flags & FDC_82077) != 0) {
   1143  1.1   jnemeth 		bus_space_write_1(t, h, fdc->sc_reg_dor,
   1144  1.1   jnemeth 				  FDO_FRST | FDO_FDMAEN | FDO_DS);
   1145  1.1   jnemeth 	}
   1146  1.1   jnemeth #ifdef FD_DEBUG
   1147  1.1   jnemeth 	if (fdc_debug)
   1148  1.1   jnemeth 		printf("fdc reset\n");
   1149  1.1   jnemeth #endif
   1150  1.1   jnemeth }
   1151  1.1   jnemeth 
   1152  1.1   jnemeth void
   1153  1.1   jnemeth fd_set_motor(struct fdc_softc *fdc)
   1154  1.1   jnemeth {
   1155  1.1   jnemeth 	struct fd_softc *fd;
   1156  1.1   jnemeth 	u_char status;
   1157  1.1   jnemeth 	int n;
   1158  1.1   jnemeth 
   1159  1.1   jnemeth 	if ((fdc->sc_flags & FDC_82077) != 0) {
   1160  1.1   jnemeth 		status = FDO_FRST | FDO_FDMAEN;
   1161  1.1   jnemeth 		if ((fd = fdc->sc_drives.tqh_first) != NULL)
   1162  1.1   jnemeth 			status |= fd->sc_drive;
   1163  1.1   jnemeth 
   1164  1.1   jnemeth 		for (n = 0; n < 4; n++)
   1165  1.1   jnemeth 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
   1166  1.1   jnemeth 				status |= FDO_MOEN(n);
   1167  1.1   jnemeth 		bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
   1168  1.1   jnemeth 				  fdc->sc_reg_dor, status);
   1169  1.3   jnemeth #ifdef SUN4
   1170  1.1   jnemeth 	} else {
   1171  1.1   jnemeth 
   1172  1.1   jnemeth 		for (n = 0; n < 4; n++) {
   1173  1.1   jnemeth 			if ((fd = fdc->sc_fd[n]) != NULL  &&
   1174  1.1   jnemeth 			    (fd->sc_flags & FD_MOTOR) != 0) {
   1175  1.1   jnemeth 				auxregbisc(AUXIO4C_FDS, 0);
   1176  1.1   jnemeth 				return;
   1177  1.1   jnemeth 			}
   1178  1.1   jnemeth 		}
   1179  1.1   jnemeth 		auxregbisc(0, AUXIO4C_FDS);
   1180  1.1   jnemeth #endif
   1181  1.1   jnemeth 	}
   1182  1.1   jnemeth }
   1183  1.1   jnemeth 
   1184  1.1   jnemeth void
   1185  1.1   jnemeth fd_motor_off(void *arg)
   1186  1.1   jnemeth {
   1187  1.1   jnemeth 	struct fd_softc *fd = arg;
   1188  1.1   jnemeth 	int s;
   1189  1.1   jnemeth 
   1190  1.1   jnemeth 	s = splbio();
   1191  1.1   jnemeth 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1192  1.1   jnemeth 	fd_set_motor((struct fdc_softc *)device_parent(&fd->sc_dv));
   1193  1.1   jnemeth 	splx(s);
   1194  1.1   jnemeth }
   1195  1.1   jnemeth 
   1196  1.1   jnemeth void
   1197  1.1   jnemeth fd_motor_on(void *arg)
   1198  1.1   jnemeth {
   1199  1.1   jnemeth 	struct fd_softc *fd = arg;
   1200  1.1   jnemeth 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   1201  1.1   jnemeth 	int s;
   1202  1.1   jnemeth 
   1203  1.1   jnemeth 	s = splbio();
   1204  1.1   jnemeth 	fd->sc_flags &= ~FD_MOTOR_WAIT;
   1205  1.1   jnemeth 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
   1206  1.5   jnemeth 		(void)fdcstate(fdc);
   1207  1.1   jnemeth 	splx(s);
   1208  1.1   jnemeth }
   1209  1.1   jnemeth 
   1210  1.1   jnemeth /*
   1211  1.1   jnemeth  * Get status bytes off the FDC after a command has finished
   1212  1.1   jnemeth  * Returns the number of status bytes read; -1 on error.
   1213  1.1   jnemeth  * The return value is also stored in `sc_nstat'.
   1214  1.1   jnemeth  */
   1215  1.1   jnemeth int
   1216  1.1   jnemeth fdcresult(struct fdc_softc *fdc)
   1217  1.1   jnemeth {
   1218  1.1   jnemeth 	bus_space_tag_t t = fdc->sc_bustag;
   1219  1.1   jnemeth 	bus_space_handle_t h = fdc->sc_handle;
   1220  1.1   jnemeth 	int j, n = 0;
   1221  1.1   jnemeth 
   1222  1.1   jnemeth 	for (j = 10000; j; j--) {
   1223  1.1   jnemeth 		uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
   1224  1.1   jnemeth 		v &= (NE7_DIO | NE7_RQM | NE7_CB);
   1225  1.1   jnemeth 		if (v == NE7_RQM)
   1226  1.1   jnemeth 			return fdc->sc_nstat = n;
   1227  1.1   jnemeth 		if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
   1228  1.1   jnemeth 			if (n >= sizeof(fdc->sc_status)) {
   1229  1.1   jnemeth 				log(LOG_ERR, "fdcresult: overrun\n");
   1230  1.1   jnemeth 				return -1;
   1231  1.1   jnemeth 			}
   1232  1.1   jnemeth 			fdc->sc_status[n++] =
   1233  1.1   jnemeth 				bus_space_read_1(t, h, fdc->sc_reg_fifo);
   1234  1.1   jnemeth 		} else
   1235  1.1   jnemeth 			delay(1);
   1236  1.1   jnemeth 	}
   1237  1.1   jnemeth 
   1238  1.1   jnemeth 	log(LOG_ERR, "fdcresult: timeout\n");
   1239  1.1   jnemeth 	return fdc->sc_nstat = -1;
   1240  1.1   jnemeth }
   1241  1.1   jnemeth 
   1242  1.1   jnemeth /*
   1243  1.1   jnemeth  * Write a command byte to the FDC.
   1244  1.1   jnemeth  * Returns 0 on success; -1 on failure (i.e. timeout)
   1245  1.1   jnemeth  */
   1246  1.1   jnemeth int
   1247  1.1   jnemeth fdc_wrfifo(struct fdc_softc *fdc, uint8_t x)
   1248  1.1   jnemeth {
   1249  1.1   jnemeth 	bus_space_tag_t t = fdc->sc_bustag;
   1250  1.1   jnemeth 	bus_space_handle_t h = fdc->sc_handle;
   1251  1.1   jnemeth 	int i;
   1252  1.1   jnemeth 
   1253  1.1   jnemeth 	for (i = 100000; i-- > 0;) {
   1254  1.1   jnemeth 		uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
   1255  1.1   jnemeth 		if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM) {
   1256  1.1   jnemeth 			/* The chip is ready */
   1257  1.1   jnemeth 			bus_space_write_1(t, h, fdc->sc_reg_fifo, x);
   1258  1.1   jnemeth 			return 0;
   1259  1.1   jnemeth 		}
   1260  1.1   jnemeth 		delay(1);
   1261  1.1   jnemeth 	}
   1262  1.1   jnemeth 	return -1;
   1263  1.1   jnemeth }
   1264  1.1   jnemeth 
   1265  1.1   jnemeth int
   1266  1.1   jnemeth fdc_diskchange(struct fdc_softc *fdc)
   1267  1.1   jnemeth {
   1268  1.1   jnemeth 
   1269  1.3   jnemeth #ifdef SUN4
   1270  1.1   jnemeth 	if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
   1271  1.1   jnemeth #endif
   1272  1.1   jnemeth 		bus_space_tag_t t = fdc->sc_bustag;
   1273  1.1   jnemeth 		bus_space_handle_t h = fdc->sc_handle;
   1274  1.1   jnemeth 		uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_dir);
   1275  1.1   jnemeth 		return (v & FDI_DCHG) != 0;
   1276  1.3   jnemeth #ifdef SUN4
   1277  1.1   jnemeth 	} else if (CPU_ISSUN4C) {
   1278  1.1   jnemeth 		return (*AUXIO4C_REG & AUXIO4C_FDC) != 0;
   1279  1.1   jnemeth 	}
   1280  1.1   jnemeth 	return 0;
   1281  1.1   jnemeth #endif
   1282  1.1   jnemeth }
   1283  1.1   jnemeth 
   1284  1.1   jnemeth int
   1285  1.1   jnemeth fdopen(dev_t dev, int flags, int fmt, struct lwp *l)
   1286  1.1   jnemeth {
   1287  1.1   jnemeth  	int unit, pmask;
   1288  1.1   jnemeth 	struct fd_softc *fd;
   1289  1.1   jnemeth 	struct fd_type *type;
   1290  1.1   jnemeth 
   1291  1.1   jnemeth 	unit = FDUNIT(dev);
   1292  1.1   jnemeth 	if (unit >= fd_cd.cd_ndevs)
   1293  1.1   jnemeth 		return ENXIO;
   1294  1.1   jnemeth 	fd = fd_cd.cd_devs[unit];
   1295  1.1   jnemeth 	if (fd == NULL)
   1296  1.1   jnemeth 		return ENXIO;
   1297  1.1   jnemeth 	type = fd_dev_to_type(fd, dev);
   1298  1.1   jnemeth 	if (type == NULL)
   1299  1.1   jnemeth 		return ENXIO;
   1300  1.1   jnemeth 
   1301  1.1   jnemeth 	if ((fd->sc_flags & FD_OPEN) != 0 &&
   1302  1.1   jnemeth 	    fd->sc_type != type)
   1303  1.1   jnemeth 		return EBUSY;
   1304  1.1   jnemeth 
   1305  1.1   jnemeth 	fd->sc_type = type;
   1306  1.1   jnemeth 	fd->sc_cylin = -1;
   1307  1.1   jnemeth 	fd->sc_flags |= FD_OPEN;
   1308  1.1   jnemeth 
   1309  1.1   jnemeth 	/*
   1310  1.1   jnemeth 	 * Only update the disklabel if we're not open anywhere else.
   1311  1.1   jnemeth 	 */
   1312  1.1   jnemeth 	if (fd->sc_dk.dk_openmask == 0)
   1313  1.1   jnemeth 		fdgetdisklabel(dev);
   1314  1.1   jnemeth 
   1315  1.1   jnemeth 	pmask = (1 << DISKPART(dev));
   1316  1.1   jnemeth 
   1317  1.1   jnemeth 	switch (fmt) {
   1318  1.1   jnemeth 	case S_IFCHR:
   1319  1.1   jnemeth 		fd->sc_dk.dk_copenmask |= pmask;
   1320  1.1   jnemeth 		break;
   1321  1.1   jnemeth 
   1322  1.1   jnemeth 	case S_IFBLK:
   1323  1.1   jnemeth 		fd->sc_dk.dk_bopenmask |= pmask;
   1324  1.1   jnemeth 		break;
   1325  1.1   jnemeth 	}
   1326  1.1   jnemeth 	fd->sc_dk.dk_openmask =
   1327  1.1   jnemeth 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
   1328  1.1   jnemeth 
   1329  1.1   jnemeth 	return 0;
   1330  1.1   jnemeth }
   1331  1.1   jnemeth 
   1332  1.1   jnemeth int
   1333  1.1   jnemeth fdclose(dev_t dev, int flags, int fmt, struct lwp *l)
   1334  1.1   jnemeth {
   1335  1.1   jnemeth 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   1336  1.1   jnemeth 	int pmask = (1 << DISKPART(dev));
   1337  1.1   jnemeth 
   1338  1.1   jnemeth 	fd->sc_flags &= ~FD_OPEN;
   1339  1.1   jnemeth 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
   1340  1.1   jnemeth 
   1341  1.1   jnemeth 	switch (fmt) {
   1342  1.1   jnemeth 	case S_IFCHR:
   1343  1.1   jnemeth 		fd->sc_dk.dk_copenmask &= ~pmask;
   1344  1.1   jnemeth 		break;
   1345  1.1   jnemeth 
   1346  1.1   jnemeth 	case S_IFBLK:
   1347  1.1   jnemeth 		fd->sc_dk.dk_bopenmask &= ~pmask;
   1348  1.1   jnemeth 		break;
   1349  1.1   jnemeth 	}
   1350  1.1   jnemeth 	fd->sc_dk.dk_openmask =
   1351  1.1   jnemeth 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
   1352  1.1   jnemeth 
   1353  1.1   jnemeth 	return 0;
   1354  1.1   jnemeth }
   1355  1.1   jnemeth 
   1356  1.1   jnemeth int
   1357  1.1   jnemeth fdread(dev_t dev, struct uio *uio, int flag)
   1358  1.1   jnemeth {
   1359  1.1   jnemeth 
   1360  1.1   jnemeth         return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
   1361  1.1   jnemeth }
   1362  1.1   jnemeth 
   1363  1.1   jnemeth int
   1364  1.1   jnemeth fdwrite(dev_t dev, struct uio *uio, int flag)
   1365  1.1   jnemeth {
   1366  1.1   jnemeth 
   1367  1.1   jnemeth         return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
   1368  1.1   jnemeth }
   1369  1.1   jnemeth 
   1370  1.1   jnemeth void
   1371  1.1   jnemeth fdcstart(struct fdc_softc *fdc)
   1372  1.1   jnemeth {
   1373  1.1   jnemeth 
   1374  1.1   jnemeth #ifdef DIAGNOSTIC
   1375  1.1   jnemeth 	/* only got here if controller's drive queue was inactive; should
   1376  1.1   jnemeth 	   be in idle state */
   1377  1.1   jnemeth 	if (fdc->sc_state != DEVIDLE) {
   1378  1.1   jnemeth 		printf("fdcstart: not idle\n");
   1379  1.1   jnemeth 		return;
   1380  1.1   jnemeth 	}
   1381  1.1   jnemeth #endif
   1382  1.1   jnemeth 	(void)fdcstate(fdc);
   1383  1.1   jnemeth }
   1384  1.1   jnemeth 
   1385  1.1   jnemeth void
   1386  1.1   jnemeth fdcstatus(struct fdc_softc *fdc, const char *s)
   1387  1.1   jnemeth {
   1388  1.1   jnemeth 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
   1389  1.1   jnemeth 	int n;
   1390  1.1   jnemeth 	char bits[64];
   1391  1.1   jnemeth 
   1392  1.1   jnemeth 	/* Just print last status */
   1393  1.1   jnemeth 	n = fdc->sc_nstat;
   1394  1.1   jnemeth 
   1395  1.1   jnemeth #if 0
   1396  1.1   jnemeth 	/*
   1397  1.1   jnemeth 	 * A 82072 seems to return <invalid command> on
   1398  1.1   jnemeth 	 * gratuitous Sense Interrupt commands.
   1399  1.1   jnemeth 	 */
   1400  1.1   jnemeth 	if (n == 0 && (fdc->sc_flags & FDC_82077) != 0) {
   1401  1.1   jnemeth 		fdc_wrfifo(fdc, NE7CMD_SENSEI);
   1402  1.1   jnemeth 		(void)fdcresult(fdc);
   1403  1.1   jnemeth 		n = 2;
   1404  1.1   jnemeth 	}
   1405  1.1   jnemeth #endif
   1406  1.1   jnemeth 
   1407  1.1   jnemeth 	printf("%s: %s: state %d",
   1408  1.1   jnemeth 		fd ? fd->sc_dv.dv_xname : "fdc", s, fdc->sc_state);
   1409  1.1   jnemeth 
   1410  1.1   jnemeth 	switch (n) {
   1411  1.1   jnemeth 	case 0:
   1412  1.1   jnemeth 		printf("\n");
   1413  1.1   jnemeth 		break;
   1414  1.1   jnemeth 	case 2:
   1415  1.1   jnemeth 		printf(" (st0 %s cyl %d)\n",
   1416  1.1   jnemeth 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
   1417  1.1   jnemeth 		    bits, sizeof(bits)), fdc->sc_status[1]);
   1418  1.1   jnemeth 		break;
   1419  1.1   jnemeth 	case 7:
   1420  1.1   jnemeth 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
   1421  1.1   jnemeth 		    NE7_ST0BITS, bits, sizeof(bits)));
   1422  1.1   jnemeth 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
   1423  1.1   jnemeth 		    NE7_ST1BITS, bits, sizeof(bits)));
   1424  1.1   jnemeth 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
   1425  1.1   jnemeth 		    NE7_ST2BITS, bits, sizeof(bits)));
   1426  1.1   jnemeth 		printf(" cyl %d head %d sec %d)\n",
   1427  1.1   jnemeth 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1428  1.1   jnemeth 		break;
   1429  1.1   jnemeth #ifdef DIAGNOSTIC
   1430  1.1   jnemeth 	default:
   1431  1.1   jnemeth 		printf(" fdcstatus: weird size: %d\n", n);
   1432  1.1   jnemeth 		break;
   1433  1.1   jnemeth #endif
   1434  1.1   jnemeth 	}
   1435  1.1   jnemeth }
   1436  1.1   jnemeth 
   1437  1.1   jnemeth void
   1438  1.1   jnemeth fdctimeout(void *arg)
   1439  1.1   jnemeth {
   1440  1.1   jnemeth 	struct fdc_softc *fdc = arg;
   1441  1.1   jnemeth 	struct fd_softc *fd;
   1442  1.1   jnemeth 	int s;
   1443  1.1   jnemeth 
   1444  1.1   jnemeth 	s = splbio();
   1445  1.1   jnemeth 	fd = fdc->sc_drives.tqh_first;
   1446  1.1   jnemeth 	if (fd == NULL) {
   1447  1.1   jnemeth 		printf("%s: timeout but no I/O pending: state %d, istatus=%d\n",
   1448  1.1   jnemeth 			fdc->sc_dev.dv_xname,
   1449  1.1   jnemeth 			fdc->sc_state, fdc->sc_istatus);
   1450  1.1   jnemeth 		fdc->sc_state = DEVIDLE;
   1451  1.1   jnemeth 		goto out;
   1452  1.1   jnemeth 	}
   1453  1.1   jnemeth 
   1454  1.1   jnemeth 	if (BUFQ_PEEK(fd->sc_q) != NULL)
   1455  1.1   jnemeth 		fdc->sc_state++;
   1456  1.1   jnemeth 	else
   1457  1.1   jnemeth 		fdc->sc_state = DEVIDLE;
   1458  1.1   jnemeth 
   1459  1.1   jnemeth 	(void)fdcstate(fdc);
   1460  1.1   jnemeth out:
   1461  1.1   jnemeth 	splx(s);
   1462  1.1   jnemeth 
   1463  1.1   jnemeth }
   1464  1.1   jnemeth 
   1465  1.1   jnemeth void
   1466  1.1   jnemeth fdcpseudointr(void *arg)
   1467  1.1   jnemeth {
   1468  1.1   jnemeth 	struct fdc_softc *fdc = arg;
   1469  1.1   jnemeth 	int s;
   1470  1.1   jnemeth 
   1471  1.1   jnemeth 	/* Just ensure it has the right spl. */
   1472  1.1   jnemeth 	s = splbio();
   1473  1.1   jnemeth 	(void)fdcstate(fdc);
   1474  1.1   jnemeth 	splx(s);
   1475  1.1   jnemeth }
   1476  1.1   jnemeth 
   1477  1.1   jnemeth 
   1478  1.1   jnemeth /*
   1479  1.1   jnemeth  * hardware interrupt entry point: used only if no `fast trap' * (in-window)
   1480  1.1   jnemeth  * handler is available. Unfortunately, we have no reliable way to
   1481  1.1   jnemeth  * determine that the interrupt really came from the floppy controller;
   1482  1.1   jnemeth  * just hope that the other devices that share this interrupt level
   1483  1.1   jnemeth  * can do better..
   1484  1.1   jnemeth  */
   1485  1.1   jnemeth int
   1486  1.1   jnemeth fdc_c_hwintr(void *arg)
   1487  1.1   jnemeth {
   1488  1.1   jnemeth 	struct fdc_softc *fdc = arg;
   1489  1.1   jnemeth 	bus_space_tag_t t = fdc->sc_bustag;
   1490  1.1   jnemeth 	bus_space_handle_t h = fdc->sc_handle;
   1491  1.1   jnemeth 
   1492  1.1   jnemeth 	switch (fdc->sc_itask) {
   1493  1.1   jnemeth 	case FDC_ITASK_NONE:
   1494  1.1   jnemeth 		return 0;
   1495  1.1   jnemeth 	case FDC_ITASK_SENSEI:
   1496  1.1   jnemeth 		if (fdc_wrfifo(fdc, NE7CMD_SENSEI) != 0 || fdcresult(fdc) == -1)
   1497  1.1   jnemeth 			fdc->sc_istatus = FDC_ISTATUS_ERROR;
   1498  1.1   jnemeth 		else
   1499  1.1   jnemeth 			fdc->sc_istatus = FDC_ISTATUS_DONE;
   1500  1.1   jnemeth 		softintr_schedule(fdc->sc_sicookie);
   1501  1.1   jnemeth 		return 1;
   1502  1.1   jnemeth 	case FDC_ITASK_RESULT:
   1503  1.1   jnemeth 		if (fdcresult(fdc) == -1)
   1504  1.1   jnemeth 			fdc->sc_istatus = FDC_ISTATUS_ERROR;
   1505  1.1   jnemeth 		else
   1506  1.1   jnemeth 			fdc->sc_istatus = FDC_ISTATUS_DONE;
   1507  1.1   jnemeth 		softintr_schedule(fdc->sc_sicookie);
   1508  1.1   jnemeth 		return 1;
   1509  1.1   jnemeth 	case FDC_ITASK_DMA:
   1510  1.1   jnemeth 		/* Proceed with pseudo-DMA below */
   1511  1.1   jnemeth 		break;
   1512  1.1   jnemeth 	default:
   1513  1.1   jnemeth 		printf("fdc: stray hard interrupt: itask=%d\n", fdc->sc_itask);
   1514  1.1   jnemeth 		fdc->sc_istatus = FDC_ISTATUS_SPURIOUS;
   1515  1.1   jnemeth 		softintr_schedule(fdc->sc_sicookie);
   1516  1.1   jnemeth 		return 1;
   1517  1.1   jnemeth 	}
   1518  1.1   jnemeth 
   1519  1.1   jnemeth 	/*
   1520  1.1   jnemeth 	 * Pseudo DMA in progress
   1521  1.1   jnemeth 	 */
   1522  1.1   jnemeth 	for (;;) {
   1523  1.1   jnemeth 		uint8_t msr;
   1524  1.1   jnemeth 
   1525  1.1   jnemeth 		msr = bus_space_read_1(t, h, fdc->sc_reg_msr);
   1526  1.1   jnemeth 
   1527  1.1   jnemeth 		if ((msr & NE7_RQM) == 0)
   1528  1.1   jnemeth 			/* That's all this round */
   1529  1.1   jnemeth 			break;
   1530  1.1   jnemeth 
   1531  1.1   jnemeth 		if ((msr & NE7_NDM) == 0) {
   1532  1.1   jnemeth 			fdcresult(fdc);
   1533  1.1   jnemeth 			fdc->sc_istatus = FDC_ISTATUS_DONE;
   1534  1.1   jnemeth 			softintr_schedule(fdc->sc_sicookie);
   1535  1.1   jnemeth #ifdef FD_DEBUG
   1536  1.1   jnemeth 			if (fdc_debug > 1)
   1537  1.3   jnemeth #ifdef SUN4
   1538  1.3   jnemeth 				printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
   1539  1.3   jnemeth #elif SUN4U
   1540  1.1   jnemeth 				printf("fdc: overrun: msr = %x, tc = %d\n",
   1541  1.1   jnemeth 				    msr, fdc->sc_tc);
   1542  1.1   jnemeth #endif
   1543  1.3   jnemeth #endif
   1544  1.1   jnemeth 			break;
   1545  1.1   jnemeth 		}
   1546  1.1   jnemeth 
   1547  1.1   jnemeth 		/* Another byte can be transferred */
   1548  1.1   jnemeth 		if ((msr & NE7_DIO) != 0)
   1549  1.1   jnemeth 			*fdc->sc_data =
   1550  1.1   jnemeth 				bus_space_read_1(t, h, fdc->sc_reg_fifo);
   1551  1.1   jnemeth 		else
   1552  1.1   jnemeth 			bus_space_write_1(t, h, fdc->sc_reg_fifo,
   1553  1.1   jnemeth 					  *fdc->sc_data);
   1554  1.1   jnemeth 
   1555  1.1   jnemeth 		fdc->sc_data++;
   1556  1.1   jnemeth 		if (--fdc->sc_tc == 0) {
   1557  1.1   jnemeth 			fdc->sc_istatus = FDC_ISTATUS_DONE;
   1558  1.1   jnemeth 			FTC_FLIP;
   1559  1.1   jnemeth 			fdcresult(fdc);
   1560  1.1   jnemeth 			softintr_schedule(fdc->sc_sicookie);
   1561  1.1   jnemeth 			break;
   1562  1.1   jnemeth 		}
   1563  1.1   jnemeth 	}
   1564  1.1   jnemeth 	return 1;
   1565  1.1   jnemeth }
   1566  1.1   jnemeth 
   1567  1.1   jnemeth void
   1568  1.1   jnemeth fdcswintr(void *arg)
   1569  1.1   jnemeth {
   1570  1.1   jnemeth 	struct fdc_softc *fdc = arg;
   1571  1.1   jnemeth 
   1572  1.1   jnemeth 	if (fdc->sc_istatus == FDC_ISTATUS_NONE)
   1573  1.1   jnemeth 		/* This (software) interrupt is not for us */
   1574  1.1   jnemeth 		return;
   1575  1.1   jnemeth 
   1576  1.1   jnemeth 	switch (fdc->sc_istatus) {
   1577  1.1   jnemeth 	case FDC_ISTATUS_ERROR:
   1578  1.1   jnemeth 		printf("fdc: ierror status: state %d\n", fdc->sc_state);
   1579  1.1   jnemeth 		break;
   1580  1.1   jnemeth 	case FDC_ISTATUS_SPURIOUS:
   1581  1.1   jnemeth 		printf("fdc: spurious interrupt: state %d\n", fdc->sc_state);
   1582  1.1   jnemeth 		break;
   1583  1.1   jnemeth 	}
   1584  1.1   jnemeth 
   1585  1.1   jnemeth 	fdcstate(fdc);
   1586  1.1   jnemeth 	return;
   1587  1.1   jnemeth }
   1588  1.1   jnemeth 
   1589  1.1   jnemeth int
   1590  1.1   jnemeth fdcstate(struct fdc_softc *fdc)
   1591  1.1   jnemeth {
   1592  1.1   jnemeth 
   1593  1.1   jnemeth #define	st0	fdc->sc_status[0]
   1594  1.1   jnemeth #define	st1	fdc->sc_status[1]
   1595  1.1   jnemeth #define	cyl	fdc->sc_status[1]
   1596  1.1   jnemeth #define FDC_WRFIFO(fdc, c) do {			\
   1597  1.1   jnemeth 	if (fdc_wrfifo(fdc, (c))) {		\
   1598  1.1   jnemeth 		goto xxx;			\
   1599  1.1   jnemeth 	}					\
   1600  1.1   jnemeth } while(0)
   1601  1.1   jnemeth 
   1602  1.1   jnemeth 	struct fd_softc *fd;
   1603  1.1   jnemeth 	struct buf *bp;
   1604  1.1   jnemeth 	int read, head, sec, nblks;
   1605  1.1   jnemeth 	struct fd_type *type;
   1606  1.1   jnemeth 	struct ne7_fd_formb *finfo = NULL;
   1607  1.1   jnemeth 
   1608  1.1   jnemeth 	if (fdc->sc_istatus == FDC_ISTATUS_ERROR) {
   1609  1.1   jnemeth 		/* Prevent loop if the reset sequence produces errors */
   1610  1.1   jnemeth 		if (fdc->sc_state != RESETCOMPLETE &&
   1611  1.1   jnemeth 		    fdc->sc_state != RECALWAIT &&
   1612  1.1   jnemeth 		    fdc->sc_state != RECALCOMPLETE)
   1613  1.1   jnemeth 			fdc->sc_state = DORESET;
   1614  1.1   jnemeth 	}
   1615  1.1   jnemeth 
   1616  1.1   jnemeth 	/* Clear I task/status field */
   1617  1.1   jnemeth 	fdc->sc_istatus = FDC_ISTATUS_NONE;
   1618  1.1   jnemeth 	fdc->sc_itask = FDC_ITASK_NONE;
   1619  1.1   jnemeth 
   1620  1.1   jnemeth loop:
   1621  1.1   jnemeth 	/* Is there a drive for the controller to do a transfer with? */
   1622  1.1   jnemeth 	fd = fdc->sc_drives.tqh_first;
   1623  1.1   jnemeth 	if (fd == NULL) {
   1624  1.1   jnemeth 		fdc->sc_state = DEVIDLE;
   1625  1.1   jnemeth  		return 0;
   1626  1.1   jnemeth 	}
   1627  1.1   jnemeth 
   1628  1.1   jnemeth 	/* Is there a transfer to this drive?  If not, deactivate drive. */
   1629  1.1   jnemeth 	bp = BUFQ_PEEK(fd->sc_q);
   1630  1.1   jnemeth 	if (bp == NULL) {
   1631  1.1   jnemeth 		fd->sc_ops = 0;
   1632  1.1   jnemeth 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1633  1.1   jnemeth 		fd->sc_active = 0;
   1634  1.1   jnemeth 		goto loop;
   1635  1.1   jnemeth 	}
   1636  1.1   jnemeth 
   1637  1.1   jnemeth 	if (bp->b_flags & B_FORMAT)
   1638  1.1   jnemeth 		finfo = (struct ne7_fd_formb *)bp->b_data;
   1639  1.1   jnemeth 
   1640  1.1   jnemeth 	switch (fdc->sc_state) {
   1641  1.1   jnemeth 	case DEVIDLE:
   1642  1.1   jnemeth 		fdc->sc_errors = 0;
   1643  1.1   jnemeth 		fd->sc_skip = 0;
   1644  1.1   jnemeth 		fd->sc_bcount = bp->b_bcount;
   1645  1.1   jnemeth 		fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
   1646  1.1   jnemeth 		callout_stop(&fd->sc_motoroff_ch);
   1647  1.1   jnemeth 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1648  1.1   jnemeth 			fdc->sc_state = MOTORWAIT;
   1649  1.1   jnemeth 			return 1;
   1650  1.1   jnemeth 		}
   1651  1.1   jnemeth 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1652  1.1   jnemeth 			/* Turn on the motor, being careful about pairing. */
   1653  1.1   jnemeth 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1654  1.1   jnemeth 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1655  1.1   jnemeth 				callout_stop(&ofd->sc_motoroff_ch);
   1656  1.1   jnemeth 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1657  1.1   jnemeth 			}
   1658  1.1   jnemeth 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1659  1.1   jnemeth 			fd_set_motor(fdc);
   1660  1.1   jnemeth 			fdc->sc_state = MOTORWAIT;
   1661  1.1   jnemeth 			if ((fdc->sc_flags & FDC_NEEDMOTORWAIT) != 0) { /*XXX*/
   1662  1.1   jnemeth 				/* Allow .25s for motor to stabilize. */
   1663  1.1   jnemeth 				callout_reset(&fd->sc_motoron_ch, hz / 4,
   1664  1.1   jnemeth 				    fd_motor_on, fd);
   1665  1.1   jnemeth 			} else {
   1666  1.1   jnemeth 				fd->sc_flags &= ~FD_MOTOR_WAIT;
   1667  1.1   jnemeth 				goto loop;
   1668  1.1   jnemeth 			}
   1669  1.1   jnemeth 			return 1;
   1670  1.1   jnemeth 		}
   1671  1.1   jnemeth 		/* Make sure the right drive is selected. */
   1672  1.1   jnemeth 		fd_set_motor(fdc);
   1673  1.1   jnemeth 
   1674  1.1   jnemeth 		if (fdc_diskchange(fdc))
   1675  1.1   jnemeth 			goto dodskchg;
   1676  1.1   jnemeth 
   1677  1.1   jnemeth 		/*FALLTHROUGH*/
   1678  1.1   jnemeth 	case DOSEEK:
   1679  1.1   jnemeth 	doseek:
   1680  1.1   jnemeth 		if ((fdc->sc_flags & FDC_EIS) &&
   1681  1.1   jnemeth 		    (bp->b_flags & B_FORMAT) == 0) {
   1682  1.1   jnemeth 			fd->sc_cylin = bp->b_cylinder;
   1683  1.1   jnemeth 			/* We use implied seek */
   1684  1.1   jnemeth 			goto doio;
   1685  1.1   jnemeth 		}
   1686  1.1   jnemeth 
   1687  1.1   jnemeth 		if (fd->sc_cylin == bp->b_cylinder)
   1688  1.1   jnemeth 			goto doio;
   1689  1.1   jnemeth 
   1690  1.1   jnemeth 		fd->sc_cylin = -1;
   1691  1.1   jnemeth 		fdc->sc_state = SEEKWAIT;
   1692  1.1   jnemeth 		fdc->sc_nstat = 0;
   1693  1.1   jnemeth 
   1694  1.1   jnemeth 		iostat_seek(fd->sc_dk.dk_stats);
   1695  1.1   jnemeth 
   1696  1.1   jnemeth 		disk_busy(&fd->sc_dk);
   1697  1.1   jnemeth 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
   1698  1.1   jnemeth 
   1699  1.1   jnemeth 		/* specify command */
   1700  1.1   jnemeth 		FDC_WRFIFO(fdc, NE7CMD_SPECIFY);
   1701  1.1   jnemeth 		FDC_WRFIFO(fdc, fd->sc_type->steprate);
   1702  1.1   jnemeth 		/* XXX head load time == 6ms */
   1703  1.1   jnemeth 		FDC_WRFIFO(fdc, 6 | NE7_SPECIFY_NODMA);
   1704  1.1   jnemeth 
   1705  1.1   jnemeth 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1706  1.1   jnemeth 		/* seek function */
   1707  1.1   jnemeth 		FDC_WRFIFO(fdc, NE7CMD_SEEK);
   1708  1.1   jnemeth 		FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
   1709  1.1   jnemeth 		FDC_WRFIFO(fdc, bp->b_cylinder * fd->sc_type->step);
   1710  1.1   jnemeth 		return 1;
   1711  1.1   jnemeth 
   1712  1.1   jnemeth 	case DODSKCHG:
   1713  1.1   jnemeth 	dodskchg:
   1714  1.1   jnemeth 		/*
   1715  1.1   jnemeth 		 * Disk change: force a seek operation by going to cyl 1
   1716  1.1   jnemeth 		 * followed by a recalibrate.
   1717  1.1   jnemeth 		 */
   1718  1.1   jnemeth 		disk_busy(&fd->sc_dk);
   1719  1.1   jnemeth 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
   1720  1.1   jnemeth 		fd->sc_cylin = -1;
   1721  1.1   jnemeth 		fdc->sc_nstat = 0;
   1722  1.1   jnemeth 		fdc->sc_state = DSKCHGWAIT;
   1723  1.1   jnemeth 
   1724  1.1   jnemeth 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1725  1.1   jnemeth 		/* seek function */
   1726  1.1   jnemeth 		FDC_WRFIFO(fdc, NE7CMD_SEEK);
   1727  1.1   jnemeth 		FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
   1728  1.1   jnemeth 		FDC_WRFIFO(fdc, 1 * fd->sc_type->step);
   1729  1.1   jnemeth 		return 1;
   1730  1.1   jnemeth 
   1731  1.1   jnemeth 	case DSKCHGWAIT:
   1732  1.1   jnemeth 		callout_stop(&fdc->sc_timo_ch);
   1733  1.1   jnemeth 		disk_unbusy(&fd->sc_dk, 0, 0);
   1734  1.1   jnemeth 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1735  1.1   jnemeth 		    cyl != 1 * fd->sc_type->step) {
   1736  1.1   jnemeth 			fdcstatus(fdc, "dskchg seek failed");
   1737  1.1   jnemeth 			fdc->sc_state = DORESET;
   1738  1.1   jnemeth 		} else
   1739  1.1   jnemeth 			fdc->sc_state = DORECAL;
   1740  1.1   jnemeth 
   1741  1.1   jnemeth 		if (fdc_diskchange(fdc)) {
   1742  1.1   jnemeth 			printf("%s: cannot clear disk change status\n",
   1743  1.1   jnemeth 				fdc->sc_dev.dv_xname);
   1744  1.1   jnemeth 			fdc->sc_state = DORESET;
   1745  1.1   jnemeth 		}
   1746  1.1   jnemeth 		goto loop;
   1747  1.1   jnemeth 
   1748  1.1   jnemeth 	case DOIO:
   1749  1.1   jnemeth 	doio:
   1750  1.1   jnemeth 		if (finfo != NULL)
   1751  1.1   jnemeth 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
   1752  1.1   jnemeth 				      (char *)finfo;
   1753  1.1   jnemeth 		type = fd->sc_type;
   1754  1.1   jnemeth 		sec = fd->sc_blkno % type->seccyl;
   1755  1.1   jnemeth 		nblks = type->seccyl - sec;
   1756  1.1   jnemeth 		nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
   1757  1.1   jnemeth 		nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
   1758  1.1   jnemeth 		fd->sc_nblks = nblks;
   1759  1.1   jnemeth 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
   1760  1.1   jnemeth 		head = sec / type->sectrac;
   1761  1.1   jnemeth 		sec -= head * type->sectrac;
   1762  1.1   jnemeth #ifdef DIAGNOSTIC
   1763  1.1   jnemeth 		{int block;
   1764  1.1   jnemeth 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac +
   1765  1.1   jnemeth 			 sec;
   1766  1.1   jnemeth 		 if (block != fd->sc_blkno) {
   1767  1.1   jnemeth 			 printf("fdcintr: block %d != blkno %d\n", block,
   1768  1.1   jnemeth 			        (int)fd->sc_blkno);
   1769  1.1   jnemeth #ifdef DDB
   1770  1.1   jnemeth 			 Debugger();
   1771  1.1   jnemeth #endif
   1772  1.1   jnemeth 		 }}
   1773  1.1   jnemeth #endif
   1774  1.1   jnemeth 		read = bp->b_flags & B_READ;
   1775  1.1   jnemeth 
   1776  1.1   jnemeth 		/* Setup for pseudo DMA */
   1777  1.7  christos 		fdc->sc_data = (char *)bp->b_data + fd->sc_skip;
   1778  1.1   jnemeth 		fdc->sc_tc = fd->sc_nbytes;
   1779  1.1   jnemeth 
   1780  1.1   jnemeth 		bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
   1781  1.1   jnemeth 				  fdc->sc_reg_drs, type->rate);
   1782  1.1   jnemeth #ifdef FD_DEBUG
   1783  1.1   jnemeth 		if (fdc_debug > 1)
   1784  1.1   jnemeth 			printf("fdcstate: doio: %s drive %d "
   1785  1.1   jnemeth 				"track %d head %d sec %d nblks %d\n",
   1786  1.1   jnemeth 				finfo ? "format" :
   1787  1.1   jnemeth 					(read ? "read" : "write"),
   1788  1.1   jnemeth 				fd->sc_drive, fd->sc_cylin, head, sec, nblks);
   1789  1.1   jnemeth #endif
   1790  1.1   jnemeth 		fdc->sc_state = IOCOMPLETE;
   1791  1.1   jnemeth 		fdc->sc_itask = FDC_ITASK_DMA;
   1792  1.1   jnemeth 		fdc->sc_nstat = 0;
   1793  1.1   jnemeth 
   1794  1.1   jnemeth 		disk_busy(&fd->sc_dk);
   1795  1.1   jnemeth 
   1796  1.1   jnemeth 		/* allow 3 seconds for operation */
   1797  1.1   jnemeth 		callout_reset(&fdc->sc_timo_ch, 3 * hz, fdctimeout, fdc);
   1798  1.1   jnemeth 
   1799  1.1   jnemeth 		if (finfo != NULL) {
   1800  1.1   jnemeth 			/* formatting */
   1801  1.1   jnemeth 			FDC_WRFIFO(fdc, NE7CMD_FORMAT);
   1802  1.1   jnemeth 			FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
   1803  1.1   jnemeth 			FDC_WRFIFO(fdc, finfo->fd_formb_secshift);
   1804  1.1   jnemeth 			FDC_WRFIFO(fdc, finfo->fd_formb_nsecs);
   1805  1.1   jnemeth 			FDC_WRFIFO(fdc, finfo->fd_formb_gaplen);
   1806  1.1   jnemeth 			FDC_WRFIFO(fdc, finfo->fd_formb_fillbyte);
   1807  1.1   jnemeth 		} else {
   1808  1.1   jnemeth 			if (read)
   1809  1.1   jnemeth 				FDC_WRFIFO(fdc, NE7CMD_READ);
   1810  1.1   jnemeth 			else
   1811  1.1   jnemeth 				FDC_WRFIFO(fdc, NE7CMD_WRITE);
   1812  1.1   jnemeth 			FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
   1813  1.1   jnemeth 			FDC_WRFIFO(fdc, fd->sc_cylin);	/*track*/
   1814  1.1   jnemeth 			FDC_WRFIFO(fdc, head);
   1815  1.1   jnemeth 			FDC_WRFIFO(fdc, sec + 1);	/*sector+1*/
   1816  1.1   jnemeth 			FDC_WRFIFO(fdc, type->secsize);	/*sector size*/
   1817  1.1   jnemeth 			FDC_WRFIFO(fdc, type->sectrac);	/*secs/track*/
   1818  1.1   jnemeth 			FDC_WRFIFO(fdc, type->gap1);	/*gap1 size*/
   1819  1.1   jnemeth 			FDC_WRFIFO(fdc, type->datalen);	/*data length*/
   1820  1.1   jnemeth 		}
   1821  1.1   jnemeth 
   1822  1.1   jnemeth 		return 1;				/* will return later */
   1823  1.1   jnemeth 
   1824  1.1   jnemeth 	case SEEKWAIT:
   1825  1.1   jnemeth 		callout_stop(&fdc->sc_timo_ch);
   1826  1.1   jnemeth 		fdc->sc_state = SEEKCOMPLETE;
   1827  1.1   jnemeth 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1828  1.1   jnemeth 			/* allow 1/50 second for heads to settle */
   1829  1.1   jnemeth 			callout_reset(&fdc->sc_intr_ch, hz / 50,
   1830  1.1   jnemeth 			    fdcpseudointr, fdc);
   1831  1.1   jnemeth 			return 1;		/* will return later */
   1832  1.1   jnemeth 		}
   1833  1.1   jnemeth 		/*FALLTHROUGH*/
   1834  1.1   jnemeth 	case SEEKCOMPLETE:
   1835  1.1   jnemeth 		/* no data on seek */
   1836  1.1   jnemeth 		disk_unbusy(&fd->sc_dk, 0, 0);
   1837  1.1   jnemeth 
   1838  1.1   jnemeth 		/* Make sure seek really happened. */
   1839  1.1   jnemeth 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1840  1.1   jnemeth 		    cyl != bp->b_cylinder * fd->sc_type->step) {
   1841  1.1   jnemeth #ifdef FD_DEBUG
   1842  1.1   jnemeth 			if (fdc_debug)
   1843  1.1   jnemeth 				fdcstatus(fdc, "seek failed");
   1844  1.1   jnemeth #endif
   1845  1.1   jnemeth 			fdcretry(fdc);
   1846  1.1   jnemeth 			goto loop;
   1847  1.1   jnemeth 		}
   1848  1.1   jnemeth 		fd->sc_cylin = bp->b_cylinder;
   1849  1.1   jnemeth 		goto doio;
   1850  1.1   jnemeth 
   1851  1.1   jnemeth 	case IOTIMEDOUT:
   1852  1.1   jnemeth 		/*
   1853  1.1   jnemeth 		 * Try to abort the I/O operation without resetting
   1854  1.1   jnemeth 		 * the chip first.  Poke TC and arrange to pick up
   1855  1.1   jnemeth 		 * the timed out I/O command's status.
   1856  1.1   jnemeth 		 */
   1857  1.1   jnemeth 		fdc->sc_itask = FDC_ITASK_RESULT;
   1858  1.1   jnemeth 		fdc->sc_state = IOCLEANUPWAIT;
   1859  1.1   jnemeth 		fdc->sc_nstat = 0;
   1860  1.1   jnemeth 		/* 1/10 second should be enough */
   1861  1.1   jnemeth 		callout_reset(&fdc->sc_timo_ch, hz / 10, fdctimeout, fdc);
   1862  1.1   jnemeth 		FTC_FLIP;
   1863  1.1   jnemeth 		return 1;
   1864  1.1   jnemeth 
   1865  1.1   jnemeth 	case IOCLEANUPTIMEDOUT:
   1866  1.1   jnemeth 	case SEEKTIMEDOUT:
   1867  1.1   jnemeth 	case RECALTIMEDOUT:
   1868  1.1   jnemeth 	case RESETTIMEDOUT:
   1869  1.1   jnemeth 	case DSKCHGTIMEDOUT:
   1870  1.1   jnemeth 		fdcstatus(fdc, "timeout");
   1871  1.1   jnemeth 
   1872  1.1   jnemeth 		/* All other timeouts always roll through to a chip reset */
   1873  1.1   jnemeth 		fdcretry(fdc);
   1874  1.1   jnemeth 
   1875  1.1   jnemeth 		/* Force reset, no matter what fdcretry() says */
   1876  1.1   jnemeth 		fdc->sc_state = DORESET;
   1877  1.1   jnemeth 		goto loop;
   1878  1.1   jnemeth 
   1879  1.1   jnemeth 	case IOCLEANUPWAIT: /* IO FAILED, cleanup succeeded */
   1880  1.1   jnemeth 		callout_stop(&fdc->sc_timo_ch);
   1881  1.1   jnemeth 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
   1882  1.1   jnemeth 		    (bp->b_flags & B_READ));
   1883  1.1   jnemeth 		fdcretry(fdc);
   1884  1.1   jnemeth 		goto loop;
   1885  1.1   jnemeth 
   1886  1.1   jnemeth 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1887  1.1   jnemeth 		callout_stop(&fdc->sc_timo_ch);
   1888  1.1   jnemeth 
   1889  1.1   jnemeth 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
   1890  1.1   jnemeth 		    (bp->b_flags & B_READ));
   1891  1.1   jnemeth 
   1892  1.1   jnemeth 		if (fdc->sc_nstat != 7 || st1 != 0 ||
   1893  1.1   jnemeth 		    ((st0 & 0xf8) != 0 &&
   1894  1.1   jnemeth 		     ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
   1895  1.1   jnemeth #ifdef FD_DEBUG
   1896  1.1   jnemeth 			if (fdc_debug) {
   1897  1.1   jnemeth 				fdcstatus(fdc, bp->b_flags & B_READ ?
   1898  1.1   jnemeth 				     "read failed" : "write failed");
   1899  1.1   jnemeth 				printf("blkno %lld nblks %d nstat %d tc %d\n",
   1900  1.1   jnemeth 				       (long long)fd->sc_blkno, fd->sc_nblks,
   1901  1.1   jnemeth 				       fdc->sc_nstat, fdc->sc_tc);
   1902  1.1   jnemeth 			}
   1903  1.1   jnemeth #endif
   1904  1.1   jnemeth 			if (fdc->sc_nstat == 7 &&
   1905  1.1   jnemeth 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
   1906  1.1   jnemeth 
   1907  1.1   jnemeth 				/*
   1908  1.1   jnemeth 				 * Silently retry overruns if no other
   1909  1.1   jnemeth 				 * error bit is set. Adjust threshold.
   1910  1.1   jnemeth 				 */
   1911  1.1   jnemeth 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1912  1.1   jnemeth 				if (thr < 15) {
   1913  1.1   jnemeth 					thr++;
   1914  1.1   jnemeth 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1915  1.1   jnemeth 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1916  1.1   jnemeth #ifdef FD_DEBUG
   1917  1.1   jnemeth 					if (fdc_debug)
   1918  1.1   jnemeth 						printf("fdc: %d -> threshold\n",
   1919  1.1   jnemeth 						       thr);
   1920  1.1   jnemeth #endif
   1921  1.1   jnemeth 					fdconf(fdc);
   1922  1.1   jnemeth 					fdc->sc_overruns = 0;
   1923  1.1   jnemeth 				}
   1924  1.1   jnemeth 				if (++fdc->sc_overruns < 3) {
   1925  1.1   jnemeth 					fdc->sc_state = DOIO;
   1926  1.1   jnemeth 					goto loop;
   1927  1.1   jnemeth 				}
   1928  1.1   jnemeth 			}
   1929  1.1   jnemeth 			fdcretry(fdc);
   1930  1.1   jnemeth 			goto loop;
   1931  1.1   jnemeth 		}
   1932  1.1   jnemeth 		if (fdc->sc_errors) {
   1933  1.1   jnemeth 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1934  1.1   jnemeth 			    fd->sc_skip / FD_BSIZE(fd),
   1935  1.1   jnemeth 			    (struct disklabel *)NULL);
   1936  1.1   jnemeth 			printf("\n");
   1937  1.1   jnemeth 			fdc->sc_errors = 0;
   1938  1.1   jnemeth 		} else {
   1939  1.1   jnemeth 			if (--fdc->sc_overruns < -20) {
   1940  1.1   jnemeth 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1941  1.1   jnemeth 				if (thr > 0) {
   1942  1.1   jnemeth 					thr--;
   1943  1.1   jnemeth 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1944  1.1   jnemeth 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1945  1.1   jnemeth #ifdef FD_DEBUG
   1946  1.1   jnemeth 					if (fdc_debug)
   1947  1.1   jnemeth 						printf("fdc: %d -> threshold\n",
   1948  1.1   jnemeth 						       thr);
   1949  1.1   jnemeth #endif
   1950  1.1   jnemeth 					fdconf(fdc);
   1951  1.1   jnemeth 				}
   1952  1.1   jnemeth 				fdc->sc_overruns = 0;
   1953  1.1   jnemeth 			}
   1954  1.1   jnemeth 		}
   1955  1.1   jnemeth 		fd->sc_blkno += fd->sc_nblks;
   1956  1.1   jnemeth 		fd->sc_skip += fd->sc_nbytes;
   1957  1.1   jnemeth 		fd->sc_bcount -= fd->sc_nbytes;
   1958  1.1   jnemeth 		if (finfo == NULL && fd->sc_bcount > 0) {
   1959  1.1   jnemeth 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
   1960  1.1   jnemeth 			goto doseek;
   1961  1.1   jnemeth 		}
   1962  1.1   jnemeth 		fdfinish(fd, bp);
   1963  1.1   jnemeth 		goto loop;
   1964  1.1   jnemeth 
   1965  1.1   jnemeth 	case DORESET:
   1966  1.1   jnemeth 		/* try a reset, keep motor on */
   1967  1.1   jnemeth 		fd_set_motor(fdc);
   1968  1.1   jnemeth 		delay(100);
   1969  1.1   jnemeth 		fdc->sc_nstat = 0;
   1970  1.1   jnemeth 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1971  1.1   jnemeth 		fdc->sc_state = RESETCOMPLETE;
   1972  1.1   jnemeth 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
   1973  1.1   jnemeth 		fdc_reset(fdc);
   1974  1.1   jnemeth 		return 1;			/* will return later */
   1975  1.1   jnemeth 
   1976  1.1   jnemeth 	case RESETCOMPLETE:
   1977  1.1   jnemeth 		callout_stop(&fdc->sc_timo_ch);
   1978  1.1   jnemeth 		fdconf(fdc);
   1979  1.1   jnemeth 
   1980  1.1   jnemeth 		/* FALLTHROUGH */
   1981  1.1   jnemeth 	case DORECAL:
   1982  1.1   jnemeth 		fdc->sc_state = RECALWAIT;
   1983  1.1   jnemeth 		fdc->sc_itask = FDC_ITASK_SENSEI;
   1984  1.1   jnemeth 		fdc->sc_nstat = 0;
   1985  1.1   jnemeth 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
   1986  1.1   jnemeth 		/* recalibrate function */
   1987  1.1   jnemeth 		FDC_WRFIFO(fdc, NE7CMD_RECAL);
   1988  1.1   jnemeth 		FDC_WRFIFO(fdc, fd->sc_drive);
   1989  1.1   jnemeth 		return 1;			/* will return later */
   1990  1.1   jnemeth 
   1991  1.1   jnemeth 	case RECALWAIT:
   1992  1.1   jnemeth 		callout_stop(&fdc->sc_timo_ch);
   1993  1.1   jnemeth 		fdc->sc_state = RECALCOMPLETE;
   1994  1.1   jnemeth 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1995  1.1   jnemeth 			/* allow 1/30 second for heads to settle */
   1996  1.1   jnemeth 			callout_reset(&fdc->sc_intr_ch, hz / 30,
   1997  1.1   jnemeth 			    fdcpseudointr, fdc);
   1998  1.1   jnemeth 			return 1;		/* will return later */
   1999  1.1   jnemeth 		}
   2000  1.1   jnemeth 
   2001  1.1   jnemeth 	case RECALCOMPLETE:
   2002  1.1   jnemeth 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   2003  1.1   jnemeth #ifdef FD_DEBUG
   2004  1.1   jnemeth 			if (fdc_debug)
   2005  1.1   jnemeth 				fdcstatus(fdc, "recalibrate failed");
   2006  1.1   jnemeth #endif
   2007  1.1   jnemeth 			fdcretry(fdc);
   2008  1.1   jnemeth 			goto loop;
   2009  1.1   jnemeth 		}
   2010  1.1   jnemeth 		fd->sc_cylin = 0;
   2011  1.1   jnemeth 		goto doseek;
   2012  1.1   jnemeth 
   2013  1.1   jnemeth 	case MOTORWAIT:
   2014  1.1   jnemeth 		if (fd->sc_flags & FD_MOTOR_WAIT)
   2015  1.1   jnemeth 			return 1;		/* time's not up yet */
   2016  1.1   jnemeth 		goto doseek;
   2017  1.1   jnemeth 
   2018  1.1   jnemeth 	default:
   2019  1.1   jnemeth 		fdcstatus(fdc, "stray interrupt");
   2020  1.1   jnemeth 		return 1;
   2021  1.1   jnemeth 	}
   2022  1.1   jnemeth #ifdef DIAGNOSTIC
   2023  1.1   jnemeth 	panic("fdcintr: impossible");
   2024  1.1   jnemeth #endif
   2025  1.1   jnemeth 
   2026  1.1   jnemeth xxx:
   2027  1.1   jnemeth 	/*
   2028  1.1   jnemeth 	 * We get here if the chip locks up in FDC_WRFIFO()
   2029  1.1   jnemeth 	 * Cancel any operation and schedule a reset
   2030  1.1   jnemeth 	 */
   2031  1.1   jnemeth 	callout_stop(&fdc->sc_timo_ch);
   2032  1.1   jnemeth 	fdcretry(fdc);
   2033  1.1   jnemeth 	fdc->sc_state = DORESET;
   2034  1.1   jnemeth 	goto loop;
   2035  1.1   jnemeth 
   2036  1.1   jnemeth #undef	st0
   2037  1.1   jnemeth #undef	st1
   2038  1.1   jnemeth #undef	cyl
   2039  1.1   jnemeth }
   2040  1.1   jnemeth 
   2041  1.1   jnemeth void
   2042  1.1   jnemeth fdcretry(struct fdc_softc *fdc)
   2043  1.1   jnemeth {
   2044  1.1   jnemeth 	struct fd_softc *fd;
   2045  1.1   jnemeth 	struct buf *bp;
   2046  1.1   jnemeth 	int error = EIO;
   2047  1.1   jnemeth 
   2048  1.1   jnemeth 	fd = fdc->sc_drives.tqh_first;
   2049  1.1   jnemeth 	bp = BUFQ_PEEK(fd->sc_q);
   2050  1.1   jnemeth 
   2051  1.1   jnemeth 	fdc->sc_overruns = 0;
   2052  1.1   jnemeth 	if (fd->sc_opts & FDOPT_NORETRY)
   2053  1.1   jnemeth 		goto fail;
   2054  1.1   jnemeth 
   2055  1.1   jnemeth 	switch (fdc->sc_errors) {
   2056  1.1   jnemeth 	case 0:
   2057  1.1   jnemeth 		if (fdc->sc_nstat == 7 &&
   2058  1.1   jnemeth 		    (fdc->sc_status[0] & 0xd8) == 0x40 &&
   2059  1.1   jnemeth 		    (fdc->sc_status[1] & 0x2) == 0x2) {
   2060  1.1   jnemeth 			printf("%s: read-only medium\n", fd->sc_dv.dv_xname);
   2061  1.1   jnemeth 			error = EROFS;
   2062  1.1   jnemeth 			goto failsilent;
   2063  1.1   jnemeth 		}
   2064  1.1   jnemeth 		/* try again */
   2065  1.1   jnemeth 		fdc->sc_state =
   2066  1.1   jnemeth 			(fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
   2067  1.1   jnemeth 		break;
   2068  1.1   jnemeth 
   2069  1.1   jnemeth 	case 1: case 2: case 3:
   2070  1.1   jnemeth 		/* didn't work; try recalibrating */
   2071  1.1   jnemeth 		fdc->sc_state = DORECAL;
   2072  1.1   jnemeth 		break;
   2073  1.1   jnemeth 
   2074  1.1   jnemeth 	case 4:
   2075  1.1   jnemeth 		if (fdc->sc_nstat == 7 &&
   2076  1.1   jnemeth 		    fdc->sc_status[0] == 0 &&
   2077  1.1   jnemeth 		    fdc->sc_status[1] == 0 &&
   2078  1.1   jnemeth 		    fdc->sc_status[2] == 0) {
   2079  1.1   jnemeth 			/*
   2080  1.1   jnemeth 			 * We've retried a few times and we've got
   2081  1.1   jnemeth 			 * valid status and all three status bytes
   2082  1.1   jnemeth 			 * are zero.  Assume this condition is the
   2083  1.1   jnemeth 			 * result of no disk loaded into the drive.
   2084  1.1   jnemeth 			 */
   2085  1.1   jnemeth 			printf("%s: no medium?\n", fd->sc_dv.dv_xname);
   2086  1.1   jnemeth 			error = ENODEV;
   2087  1.1   jnemeth 			goto failsilent;
   2088  1.1   jnemeth 		}
   2089  1.1   jnemeth 
   2090  1.1   jnemeth 		/* still no go; reset the bastard */
   2091  1.1   jnemeth 		fdc->sc_state = DORESET;
   2092  1.1   jnemeth 		break;
   2093  1.1   jnemeth 
   2094  1.1   jnemeth 	default:
   2095  1.1   jnemeth 	fail:
   2096  1.1   jnemeth 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
   2097  1.1   jnemeth 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
   2098  1.1   jnemeth 				fd->sc_skip / FD_BSIZE(fd),
   2099  1.1   jnemeth 				(struct disklabel *)NULL);
   2100  1.1   jnemeth 			printf("\n");
   2101  1.1   jnemeth 			fdcstatus(fdc, "controller status");
   2102  1.1   jnemeth 		}
   2103  1.1   jnemeth 
   2104  1.1   jnemeth 	failsilent:
   2105  1.1   jnemeth 		bp->b_flags |= B_ERROR;
   2106  1.1   jnemeth 		bp->b_error = error;
   2107  1.1   jnemeth 		fdfinish(fd, bp);
   2108  1.1   jnemeth 	}
   2109  1.1   jnemeth 	fdc->sc_errors++;
   2110  1.1   jnemeth }
   2111  1.1   jnemeth 
   2112  1.1   jnemeth int
   2113  1.6  christos fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   2114  1.1   jnemeth {
   2115  1.1   jnemeth 	struct fd_softc *fd;
   2116  1.1   jnemeth 	struct fdc_softc *fdc;
   2117  1.1   jnemeth 	struct fdformat_parms *form_parms;
   2118  1.1   jnemeth 	struct fdformat_cmd *form_cmd;
   2119  1.1   jnemeth 	struct ne7_fd_formb *fd_formb;
   2120  1.1   jnemeth 	int il[FD_MAX_NSEC + 1];
   2121  1.1   jnemeth 	int unit;
   2122  1.1   jnemeth 	int i, j;
   2123  1.1   jnemeth 	int error;
   2124  1.1   jnemeth 
   2125  1.1   jnemeth 	unit = FDUNIT(dev);
   2126  1.1   jnemeth 	if (unit >= fd_cd.cd_ndevs)
   2127  1.1   jnemeth 		return ENXIO;
   2128  1.1   jnemeth 
   2129  1.1   jnemeth 	fd = fd_cd.cd_devs[FDUNIT(dev)];
   2130  1.1   jnemeth 	fdc = (struct fdc_softc *)device_parent(&fd->sc_dv);
   2131  1.1   jnemeth 
   2132  1.1   jnemeth 	switch (cmd) {
   2133  1.1   jnemeth 	case DIOCGDINFO:
   2134  1.1   jnemeth 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
   2135  1.1   jnemeth 		return 0;
   2136  1.1   jnemeth 
   2137  1.1   jnemeth 	case DIOCWLABEL:
   2138  1.1   jnemeth 		if ((flag & FWRITE) == 0)
   2139  1.1   jnemeth 			return EBADF;
   2140  1.1   jnemeth 		/* XXX do something */
   2141  1.1   jnemeth 		return 0;
   2142  1.1   jnemeth 
   2143  1.1   jnemeth 	case DIOCWDINFO:
   2144  1.1   jnemeth 		if ((flag & FWRITE) == 0)
   2145  1.1   jnemeth 			return EBADF;
   2146  1.1   jnemeth 
   2147  1.1   jnemeth 		error = setdisklabel(fd->sc_dk.dk_label,
   2148  1.1   jnemeth 				    (struct disklabel *)addr, 0,
   2149  1.1   jnemeth 				    fd->sc_dk.dk_cpulabel);
   2150  1.1   jnemeth 		if (error)
   2151  1.1   jnemeth 			return error;
   2152  1.1   jnemeth 
   2153  1.1   jnemeth 		error = writedisklabel(dev, fdstrategy,
   2154  1.1   jnemeth 				       fd->sc_dk.dk_label,
   2155  1.1   jnemeth 				       fd->sc_dk.dk_cpulabel);
   2156  1.1   jnemeth 		return error;
   2157  1.1   jnemeth 
   2158  1.1   jnemeth 	case DIOCLOCK:
   2159  1.1   jnemeth 		/*
   2160  1.1   jnemeth 		 * Nothing to do here, really.
   2161  1.1   jnemeth 		 */
   2162  1.1   jnemeth 		return 0;
   2163  1.1   jnemeth 
   2164  1.1   jnemeth 	case DIOCEJECT:
   2165  1.1   jnemeth 		if (*(int *)addr == 0) {
   2166  1.1   jnemeth 			int part = DISKPART(dev);
   2167  1.1   jnemeth 			/*
   2168  1.1   jnemeth 			 * Don't force eject: check that we are the only
   2169  1.1   jnemeth 			 * partition open. If so, unlock it.
   2170  1.1   jnemeth 			 */
   2171  1.1   jnemeth 			if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
   2172  1.1   jnemeth 			    fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
   2173  1.1   jnemeth 			    fd->sc_dk.dk_openmask) {
   2174  1.1   jnemeth 				return EBUSY;
   2175  1.1   jnemeth 			}
   2176  1.1   jnemeth 		}
   2177  1.1   jnemeth 		/* FALLTHROUGH */
   2178  1.1   jnemeth 	case ODIOCEJECT:
   2179  1.1   jnemeth 		if (fdc->sc_flags & FDC_NOEJECT)
   2180  1.1   jnemeth 			return EINVAL;
   2181  1.1   jnemeth 		fd_do_eject(fd);
   2182  1.1   jnemeth 		return 0;
   2183  1.1   jnemeth 
   2184  1.1   jnemeth 	case FDIOCGETFORMAT:
   2185  1.1   jnemeth 		form_parms = (struct fdformat_parms *)addr;
   2186  1.1   jnemeth 		form_parms->fdformat_version = FDFORMAT_VERSION;
   2187  1.1   jnemeth 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
   2188  1.1   jnemeth 		form_parms->ncyl = fd->sc_type->cylinders;
   2189  1.1   jnemeth 		form_parms->nspt = fd->sc_type->sectrac;
   2190  1.1   jnemeth 		form_parms->ntrk = fd->sc_type->heads;
   2191  1.1   jnemeth 		form_parms->stepspercyl = fd->sc_type->step;
   2192  1.1   jnemeth 		form_parms->gaplen = fd->sc_type->gap2;
   2193  1.1   jnemeth 		form_parms->fillbyte = fd->sc_type->fillbyte;
   2194  1.1   jnemeth 		form_parms->interleave = fd->sc_type->interleave;
   2195  1.1   jnemeth 		switch (fd->sc_type->rate) {
   2196  1.1   jnemeth 		case FDC_500KBPS:
   2197  1.1   jnemeth 			form_parms->xfer_rate = 500 * 1024;
   2198  1.1   jnemeth 			break;
   2199  1.1   jnemeth 		case FDC_300KBPS:
   2200  1.1   jnemeth 			form_parms->xfer_rate = 300 * 1024;
   2201  1.1   jnemeth 			break;
   2202  1.1   jnemeth 		case FDC_250KBPS:
   2203  1.1   jnemeth 			form_parms->xfer_rate = 250 * 1024;
   2204  1.1   jnemeth 			break;
   2205  1.1   jnemeth 		default:
   2206  1.1   jnemeth 			return EINVAL;
   2207  1.1   jnemeth 		}
   2208  1.1   jnemeth 		return 0;
   2209  1.1   jnemeth 
   2210  1.1   jnemeth 	case FDIOCSETFORMAT:
   2211  1.1   jnemeth 		if ((flag & FWRITE) == 0)
   2212  1.1   jnemeth 			return EBADF;	/* must be opened for writing */
   2213  1.1   jnemeth 
   2214  1.1   jnemeth 		form_parms = (struct fdformat_parms *)addr;
   2215  1.1   jnemeth 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
   2216  1.1   jnemeth 			return EINVAL;/* wrong version of formatting prog */
   2217  1.1   jnemeth 
   2218  1.1   jnemeth 		i = form_parms->nbps >> 7;
   2219  1.1   jnemeth 		if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
   2220  1.1   jnemeth 		    i & ~(1 << (ffs(i)-1)))
   2221  1.1   jnemeth 			/* not a power-of-two multiple of 128 */
   2222  1.1   jnemeth 			return EINVAL;
   2223  1.1   jnemeth 
   2224  1.1   jnemeth 		switch (form_parms->xfer_rate) {
   2225  1.1   jnemeth 		case 500 * 1024:
   2226  1.1   jnemeth 			fd->sc_type->rate = FDC_500KBPS;
   2227  1.1   jnemeth 			break;
   2228  1.1   jnemeth 		case 300 * 1024:
   2229  1.1   jnemeth 			fd->sc_type->rate = FDC_300KBPS;
   2230  1.1   jnemeth 			break;
   2231  1.1   jnemeth 		case 250 * 1024:
   2232  1.1   jnemeth 			fd->sc_type->rate = FDC_250KBPS;
   2233  1.1   jnemeth 			break;
   2234  1.1   jnemeth 		default:
   2235  1.1   jnemeth 			return EINVAL;
   2236  1.1   jnemeth 		}
   2237  1.1   jnemeth 
   2238  1.1   jnemeth 		if (form_parms->nspt > FD_MAX_NSEC ||
   2239  1.1   jnemeth 		    form_parms->fillbyte > 0xff ||
   2240  1.1   jnemeth 		    form_parms->interleave > 0xff)
   2241  1.1   jnemeth 			return EINVAL;
   2242  1.1   jnemeth 		fd->sc_type->sectrac = form_parms->nspt;
   2243  1.1   jnemeth 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
   2244  1.1   jnemeth 			return EINVAL;
   2245  1.1   jnemeth 		fd->sc_type->heads = form_parms->ntrk;
   2246  1.1   jnemeth 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
   2247  1.1   jnemeth 		fd->sc_type->secsize = ffs(i)-1;
   2248  1.1   jnemeth 		fd->sc_type->gap2 = form_parms->gaplen;
   2249  1.1   jnemeth 		fd->sc_type->cylinders = form_parms->ncyl;
   2250  1.1   jnemeth 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
   2251  1.1   jnemeth 			form_parms->nbps / DEV_BSIZE;
   2252  1.1   jnemeth 		fd->sc_type->step = form_parms->stepspercyl;
   2253  1.1   jnemeth 		fd->sc_type->fillbyte = form_parms->fillbyte;
   2254  1.1   jnemeth 		fd->sc_type->interleave = form_parms->interleave;
   2255  1.1   jnemeth 		return 0;
   2256  1.1   jnemeth 
   2257  1.1   jnemeth 	case FDIOCFORMAT_TRACK:
   2258  1.1   jnemeth 		if((flag & FWRITE) == 0)
   2259  1.1   jnemeth 			/* must be opened for writing */
   2260  1.1   jnemeth 			return EBADF;
   2261  1.1   jnemeth 		form_cmd = (struct fdformat_cmd *)addr;
   2262  1.1   jnemeth 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
   2263  1.1   jnemeth 			/* wrong version of formatting prog */
   2264  1.1   jnemeth 			return EINVAL;
   2265  1.1   jnemeth 
   2266  1.1   jnemeth 		if (form_cmd->head >= fd->sc_type->heads ||
   2267  1.1   jnemeth 		    form_cmd->cylinder >= fd->sc_type->cylinders) {
   2268  1.1   jnemeth 			return EINVAL;
   2269  1.1   jnemeth 		}
   2270  1.1   jnemeth 
   2271  1.1   jnemeth 		fd_formb = malloc(sizeof(struct ne7_fd_formb),
   2272  1.1   jnemeth 		    M_TEMP, M_NOWAIT);
   2273  1.1   jnemeth 		if (fd_formb == 0)
   2274  1.1   jnemeth 			return ENOMEM;
   2275  1.1   jnemeth 
   2276  1.1   jnemeth 		fd_formb->head = form_cmd->head;
   2277  1.1   jnemeth 		fd_formb->cyl = form_cmd->cylinder;
   2278  1.1   jnemeth 		fd_formb->transfer_rate = fd->sc_type->rate;
   2279  1.1   jnemeth 		fd_formb->fd_formb_secshift = fd->sc_type->secsize;
   2280  1.1   jnemeth 		fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
   2281  1.1   jnemeth 		fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
   2282  1.1   jnemeth 		fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
   2283  1.1   jnemeth 
   2284  1.1   jnemeth 		bzero(il, sizeof il);
   2285  1.1   jnemeth 		for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
   2286  1.1   jnemeth 			while (il[(j % fd_formb->fd_formb_nsecs) + 1])
   2287  1.1   jnemeth 				j++;
   2288  1.1   jnemeth 			il[(j % fd_formb->fd_formb_nsecs) + 1] = i;
   2289  1.1   jnemeth 			j += fd->sc_type->interleave;
   2290  1.1   jnemeth 		}
   2291  1.1   jnemeth 		for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
   2292  1.1   jnemeth 			fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
   2293  1.1   jnemeth 			fd_formb->fd_formb_headno(i) = form_cmd->head;
   2294  1.1   jnemeth 			fd_formb->fd_formb_secno(i) = il[i + 1];
   2295  1.1   jnemeth 			fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
   2296  1.1   jnemeth 		}
   2297  1.1   jnemeth 
   2298  1.1   jnemeth 		error = fdformat(dev, fd_formb, l->l_proc);
   2299  1.1   jnemeth 		free(fd_formb, M_TEMP);
   2300  1.1   jnemeth 		return error;
   2301  1.1   jnemeth 
   2302  1.1   jnemeth 	case FDIOCGETOPTS:		/* get drive options */
   2303  1.1   jnemeth 		*(int *)addr = fd->sc_opts;
   2304  1.1   jnemeth 		return 0;
   2305  1.1   jnemeth 
   2306  1.1   jnemeth 	case FDIOCSETOPTS:		/* set drive options */
   2307  1.1   jnemeth 		fd->sc_opts = *(int *)addr;
   2308  1.1   jnemeth 		return 0;
   2309  1.1   jnemeth 
   2310  1.1   jnemeth #ifdef FD_DEBUG
   2311  1.1   jnemeth 	case _IO('f', 100):
   2312  1.1   jnemeth 		fdc_wrfifo(fdc, NE7CMD_DUMPREG);
   2313  1.1   jnemeth 		fdcresult(fdc);
   2314  1.1   jnemeth 		printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
   2315  1.1   jnemeth 		for (i = 0; i < fdc->sc_nstat; i++)
   2316  1.1   jnemeth 			printf(" 0x%x", fdc->sc_status[i]);
   2317  1.1   jnemeth 		printf(">\n");
   2318  1.1   jnemeth 		return 0;
   2319  1.1   jnemeth 
   2320  1.1   jnemeth 	case _IOW('f', 101, int):
   2321  1.1   jnemeth 		fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   2322  1.1   jnemeth 		fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
   2323  1.1   jnemeth 		fdconf(fdc);
   2324  1.1   jnemeth 		return 0;
   2325  1.1   jnemeth 
   2326  1.1   jnemeth 	case _IO('f', 102):
   2327  1.1   jnemeth 		fdc_wrfifo(fdc, NE7CMD_SENSEI);
   2328  1.1   jnemeth 		fdcresult(fdc);
   2329  1.1   jnemeth 		printf("fdc: sensei(%d regs): <", fdc->sc_nstat);
   2330  1.1   jnemeth 		for (i=0; i< fdc->sc_nstat; i++)
   2331  1.1   jnemeth 			printf(" 0x%x", fdc->sc_status[i]);
   2332  1.1   jnemeth 		printf(">\n");
   2333  1.1   jnemeth 		return 0;
   2334  1.1   jnemeth #endif
   2335  1.1   jnemeth 	default:
   2336  1.1   jnemeth 		return ENOTTY;
   2337  1.1   jnemeth 	}
   2338  1.1   jnemeth 
   2339  1.1   jnemeth #ifdef DIAGNOSTIC
   2340  1.1   jnemeth 	panic("fdioctl: impossible");
   2341  1.1   jnemeth #endif
   2342  1.1   jnemeth }
   2343  1.1   jnemeth 
   2344  1.1   jnemeth int
   2345  1.1   jnemeth fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct proc *p)
   2346  1.1   jnemeth {
   2347  1.1   jnemeth 	int rv = 0;
   2348  1.1   jnemeth 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   2349  1.1   jnemeth 	struct fd_type *type = fd->sc_type;
   2350  1.1   jnemeth 	struct buf *bp;
   2351  1.1   jnemeth 
   2352  1.1   jnemeth 	/* set up a buffer header for fdstrategy() */
   2353  1.1   jnemeth 	bp = getiobuf_nowait();
   2354  1.1   jnemeth 	if (bp == NULL)
   2355  1.1   jnemeth 		return ENOBUFS;
   2356  1.1   jnemeth 
   2357  1.1   jnemeth 	bp->b_vp = NULL;
   2358  1.1   jnemeth 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
   2359  1.1   jnemeth 	bp->b_proc = p;
   2360  1.1   jnemeth 	bp->b_dev = dev;
   2361  1.1   jnemeth 
   2362  1.1   jnemeth 	/*
   2363  1.1   jnemeth 	 * Calculate a fake blkno, so fdstrategy() would initiate a
   2364  1.1   jnemeth 	 * seek to the requested cylinder.
   2365  1.1   jnemeth 	 */
   2366  1.1   jnemeth 	bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
   2367  1.1   jnemeth 		       + finfo->head * type->sectrac) * FD_BSIZE(fd))
   2368  1.1   jnemeth 		      / DEV_BSIZE;
   2369  1.1   jnemeth 
   2370  1.1   jnemeth 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
   2371  1.6  christos 	bp->b_data = (void *)finfo;
   2372  1.1   jnemeth 
   2373  1.1   jnemeth #ifdef FD_DEBUG
   2374  1.1   jnemeth 	if (fdc_debug) {
   2375  1.1   jnemeth 		int i;
   2376  1.1   jnemeth 
   2377  1.1   jnemeth 		printf("fdformat: blkno 0x%llx count %d\n",
   2378  1.1   jnemeth 			(unsigned long long)bp->b_blkno, bp->b_bcount);
   2379  1.1   jnemeth 
   2380  1.1   jnemeth 		printf("\tcyl:\t%d\n", finfo->cyl);
   2381  1.1   jnemeth 		printf("\thead:\t%d\n", finfo->head);
   2382  1.1   jnemeth 		printf("\tnsecs:\t%d\n", finfo->fd_formb_nsecs);
   2383  1.1   jnemeth 		printf("\tsshft:\t%d\n", finfo->fd_formb_secshift);
   2384  1.1   jnemeth 		printf("\tgaplen:\t%d\n", finfo->fd_formb_gaplen);
   2385  1.1   jnemeth 		printf("\ttrack data:");
   2386  1.1   jnemeth 		for (i = 0; i < finfo->fd_formb_nsecs; i++) {
   2387  1.1   jnemeth 			printf(" [c%d h%d s%d]",
   2388  1.1   jnemeth 					finfo->fd_formb_cylno(i),
   2389  1.1   jnemeth 					finfo->fd_formb_headno(i),
   2390  1.1   jnemeth 					finfo->fd_formb_secno(i) );
   2391  1.1   jnemeth 			if (finfo->fd_formb_secsize(i) != 2)
   2392  1.1   jnemeth 				printf("<sz:%d>", finfo->fd_formb_secsize(i));
   2393  1.1   jnemeth 		}
   2394  1.1   jnemeth 		printf("\n");
   2395  1.1   jnemeth 	}
   2396  1.1   jnemeth #endif
   2397  1.1   jnemeth 
   2398  1.1   jnemeth 	/* now do the format */
   2399  1.1   jnemeth 	fdstrategy(bp);
   2400  1.1   jnemeth 
   2401  1.1   jnemeth 	/* ...and wait for it to complete */
   2402  1.1   jnemeth 	rv = biowait(bp);
   2403  1.1   jnemeth 	putiobuf(bp);
   2404  1.1   jnemeth 	return rv;
   2405  1.1   jnemeth }
   2406  1.1   jnemeth 
   2407  1.1   jnemeth void
   2408  1.1   jnemeth fdgetdisklabel(dev_t dev)
   2409  1.1   jnemeth {
   2410  1.1   jnemeth 	int unit = FDUNIT(dev), i;
   2411  1.1   jnemeth 	struct fd_softc *fd = fd_cd.cd_devs[unit];
   2412  1.1   jnemeth 	struct disklabel *lp = fd->sc_dk.dk_label;
   2413  1.1   jnemeth 	struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
   2414  1.1   jnemeth 
   2415  1.1   jnemeth 	bzero(lp, sizeof(struct disklabel));
   2416  1.1   jnemeth 	bzero(lp, sizeof(struct cpu_disklabel));
   2417  1.1   jnemeth 
   2418  1.1   jnemeth 	lp->d_type = DTYPE_FLOPPY;
   2419  1.1   jnemeth 	lp->d_secsize = FD_BSIZE(fd);
   2420  1.1   jnemeth 	lp->d_secpercyl = fd->sc_type->seccyl;
   2421  1.1   jnemeth 	lp->d_nsectors = fd->sc_type->sectrac;
   2422  1.1   jnemeth 	lp->d_ncylinders = fd->sc_type->cylinders;
   2423  1.1   jnemeth 	lp->d_ntracks = fd->sc_type->heads;	/* Go figure... */
   2424  1.1   jnemeth 	lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
   2425  1.1   jnemeth 	lp->d_rpm = 300;	/* XXX like it matters... */
   2426  1.1   jnemeth 
   2427  1.1   jnemeth 	strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename));
   2428  1.1   jnemeth 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   2429  1.1   jnemeth 	lp->d_interleave = 1;
   2430  1.1   jnemeth 	lp->d_flags = D_REMOVABLE;
   2431  1.1   jnemeth 
   2432  1.1   jnemeth 	lp->d_partitions[RAW_PART].p_offset = 0;
   2433  1.1   jnemeth 	lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
   2434  1.1   jnemeth 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   2435  1.1   jnemeth 	lp->d_npartitions = RAW_PART + 1;
   2436  1.1   jnemeth 
   2437  1.1   jnemeth 	lp->d_magic = DISKMAGIC;
   2438  1.1   jnemeth 	lp->d_magic2 = DISKMAGIC;
   2439  1.1   jnemeth 	lp->d_checksum = dkcksum(lp);
   2440  1.1   jnemeth 
   2441  1.1   jnemeth 	/*
   2442  1.1   jnemeth 	 * Call the generic disklabel extraction routine.  If there's
   2443  1.1   jnemeth 	 * not a label there, fake it.
   2444  1.1   jnemeth 	 */
   2445  1.1   jnemeth 	if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
   2446  1.1   jnemeth 		strncpy(lp->d_packname, "default label",
   2447  1.1   jnemeth 		    sizeof(lp->d_packname));
   2448  1.1   jnemeth 		/*
   2449  1.1   jnemeth 		 * Reset the partition info; it might have gotten
   2450  1.1   jnemeth 		 * trashed in readdisklabel().
   2451  1.1   jnemeth 		 *
   2452  1.1   jnemeth 		 * XXX Why do we have to do this?  readdisklabel()
   2453  1.1   jnemeth 		 * should be safe...
   2454  1.1   jnemeth 		 */
   2455  1.1   jnemeth 		for (i = 0; i < MAXPARTITIONS; ++i) {
   2456  1.1   jnemeth 			lp->d_partitions[i].p_offset = 0;
   2457  1.1   jnemeth 			if (i == RAW_PART) {
   2458  1.1   jnemeth 				lp->d_partitions[i].p_size =
   2459  1.1   jnemeth 				    lp->d_secpercyl * lp->d_ncylinders;
   2460  1.1   jnemeth 				lp->d_partitions[i].p_fstype = FS_BSDFFS;
   2461  1.1   jnemeth 			} else {
   2462  1.1   jnemeth 				lp->d_partitions[i].p_size = 0;
   2463  1.1   jnemeth 				lp->d_partitions[i].p_fstype = FS_UNUSED;
   2464  1.1   jnemeth 			}
   2465  1.1   jnemeth 		}
   2466  1.1   jnemeth 		lp->d_npartitions = RAW_PART + 1;
   2467  1.1   jnemeth 	}
   2468  1.1   jnemeth }
   2469  1.1   jnemeth 
   2470  1.1   jnemeth void
   2471  1.1   jnemeth fd_do_eject(struct fd_softc *fd)
   2472  1.1   jnemeth {
   2473  1.1   jnemeth 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
   2474  1.1   jnemeth 
   2475  1.3   jnemeth #ifdef SUN4
   2476  1.1   jnemeth 	if (CPU_ISSUN4C) {
   2477  1.1   jnemeth 		auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
   2478  1.1   jnemeth 		delay(10);
   2479  1.1   jnemeth 		auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
   2480  1.1   jnemeth 		return;
   2481  1.1   jnemeth 	}
   2482  1.1   jnemeth 	if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
   2483  1.1   jnemeth #endif
   2484  1.1   jnemeth 		bus_space_tag_t t = fdc->sc_bustag;
   2485  1.1   jnemeth 		bus_space_handle_t h = fdc->sc_handle;
   2486  1.1   jnemeth 		uint8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
   2487  1.1   jnemeth 
   2488  1.1   jnemeth 		bus_space_write_1(t, h, fdc->sc_reg_dor, dor | FDO_EJ);
   2489  1.1   jnemeth 		delay(10);
   2490  1.1   jnemeth 		bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
   2491  1.1   jnemeth 		return;
   2492  1.3   jnemeth #ifdef SUN4
   2493  1.1   jnemeth 	}
   2494  1.1   jnemeth #endif
   2495  1.1   jnemeth }
   2496  1.1   jnemeth 
   2497  1.1   jnemeth /* ARGSUSED */
   2498  1.1   jnemeth void
   2499  1.1   jnemeth fd_mountroot_hook(struct device *dev)
   2500  1.1   jnemeth {
   2501  1.1   jnemeth 	int c;
   2502  1.1   jnemeth 
   2503  1.1   jnemeth 	fd_do_eject((struct fd_softc *)dev);
   2504  1.1   jnemeth 	printf("Insert filesystem floppy and press return.");
   2505  1.1   jnemeth 	for (;;) {
   2506  1.1   jnemeth 		c = cngetc();
   2507  1.1   jnemeth 		if ((c == '\r') || (c == '\n')) {
   2508  1.1   jnemeth 			printf("\n");
   2509  1.1   jnemeth 			break;
   2510  1.1   jnemeth 		}
   2511  1.1   jnemeth 	}
   2512  1.1   jnemeth }
   2513  1.1   jnemeth 
   2514  1.1   jnemeth #ifdef MEMORY_DISK_HOOKS
   2515  1.1   jnemeth 
   2516  1.1   jnemeth #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
   2517  1.1   jnemeth 
   2518  1.1   jnemeth int
   2519  1.6  christos fd_read_md_image(size_t	*sizep, void *	*addrp)
   2520  1.1   jnemeth {
   2521  1.1   jnemeth 	struct buf buf, *bp = &buf;
   2522  1.1   jnemeth 	dev_t dev;
   2523  1.1   jnemeth 	off_t offset;
   2524  1.6  christos 	void *addr;
   2525  1.1   jnemeth 
   2526  1.1   jnemeth 	dev = makedev(54,0);	/* XXX */
   2527  1.1   jnemeth 
   2528  1.6  christos 	MALLOC(addr, void *, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
   2529  1.1   jnemeth 	*addrp = addr;
   2530  1.1   jnemeth 
   2531  1.1   jnemeth 	if (fdopen(dev, 0, S_IFCHR, NULL))
   2532  1.1   jnemeth 		panic("fd: mountroot: fdopen");
   2533  1.1   jnemeth 
   2534  1.1   jnemeth 	offset = 0;
   2535  1.1   jnemeth 
   2536  1.1   jnemeth 	for (;;) {
   2537  1.1   jnemeth 		bp->b_dev = dev;
   2538  1.1   jnemeth 		bp->b_error = 0;
   2539  1.1   jnemeth 		bp->b_resid = 0;
   2540  1.1   jnemeth 		bp->b_proc = NULL;
   2541  1.1   jnemeth 		bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
   2542  1.1   jnemeth 		bp->b_blkno = btodb(offset);
   2543  1.1   jnemeth 		bp->b_bcount = DEV_BSIZE;
   2544  1.1   jnemeth 		bp->b_data = addr;
   2545  1.1   jnemeth 		fdstrategy(bp);
   2546  1.1   jnemeth 		while ((bp->b_flags & B_DONE) == 0) {
   2547  1.6  christos 			tsleep((void *)bp, PRIBIO + 1, "physio", 0);
   2548  1.1   jnemeth 		}
   2549  1.1   jnemeth 		if (bp->b_error)
   2550  1.1   jnemeth 			panic("fd: mountroot: fdread error %d", bp->b_error);
   2551  1.1   jnemeth 
   2552  1.1   jnemeth 		if (bp->b_resid != 0)
   2553  1.1   jnemeth 			break;
   2554  1.1   jnemeth 
   2555  1.1   jnemeth 		addr += DEV_BSIZE;
   2556  1.1   jnemeth 		offset += DEV_BSIZE;
   2557  1.1   jnemeth 		if (offset + DEV_BSIZE > FDMICROROOTSIZE)
   2558  1.1   jnemeth 			break;
   2559  1.1   jnemeth 	}
   2560  1.1   jnemeth 	(void)fdclose(dev, 0, S_IFCHR, NULL);
   2561  1.1   jnemeth 	*sizep = offset;
   2562  1.1   jnemeth 	fd_do_eject(fd_cd.cd_devs[FDUNIT(dev)]);
   2563  1.1   jnemeth 	return 0;
   2564  1.1   jnemeth }
   2565  1.1   jnemeth #endif /* MEMORY_DISK_HOOKS */
   2566  1.4   jnemeth 
   2567  1.4   jnemeth static void
   2568  1.4   jnemeth fd_set_properties(struct fd_softc *fd)
   2569  1.4   jnemeth {
   2570  1.4   jnemeth 	prop_dictionary_t disk_info, odisk_info, geom;
   2571  1.4   jnemeth 	struct fd_type *fdt;
   2572  1.4   jnemeth 	int secsize;
   2573  1.4   jnemeth 
   2574  1.4   jnemeth 	fdt = fd->sc_deftype;
   2575  1.4   jnemeth 
   2576  1.4   jnemeth 	disk_info = prop_dictionary_create();
   2577  1.4   jnemeth 
   2578  1.4   jnemeth 	geom = prop_dictionary_create();
   2579  1.4   jnemeth 
   2580  1.4   jnemeth 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
   2581  1.4   jnemeth 	    fdt->size);
   2582  1.4   jnemeth 
   2583  1.4   jnemeth 	switch (fdt->secsize) {
   2584  1.4   jnemeth 	case 2:
   2585  1.4   jnemeth 		secsize = 512;
   2586  1.4   jnemeth 		break;
   2587  1.4   jnemeth 	case 3:
   2588  1.4   jnemeth 		secsize = 1024;
   2589  1.4   jnemeth 		break;
   2590  1.4   jnemeth 	default:
   2591  1.4   jnemeth 		secsize = 0;
   2592  1.4   jnemeth 	}
   2593  1.4   jnemeth 
   2594  1.4   jnemeth 	prop_dictionary_set_uint32(geom, "sector-size",
   2595  1.4   jnemeth 	    secsize);
   2596  1.4   jnemeth 
   2597  1.4   jnemeth 	prop_dictionary_set_uint16(geom, "sectors-per-track",
   2598  1.4   jnemeth 	    fdt->sectrac);
   2599  1.4   jnemeth 
   2600  1.4   jnemeth 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
   2601  1.4   jnemeth 	    fdt->heads);
   2602  1.4   jnemeth 
   2603  1.4   jnemeth 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
   2604  1.4   jnemeth 	    fdt->cylinders);
   2605  1.4   jnemeth 
   2606  1.4   jnemeth 	prop_dictionary_set(disk_info, "geometry", geom);
   2607  1.4   jnemeth 	prop_object_release(geom);
   2608  1.4   jnemeth 
   2609  1.4   jnemeth 	prop_dictionary_set(device_properties(&fd->sc_dv),
   2610  1.4   jnemeth 	    "disk-info", disk_info);
   2611  1.4   jnemeth 
   2612  1.4   jnemeth 	/*
   2613  1.4   jnemeth 	 * Don't release disk_info here; we keep a reference to it.
   2614  1.4   jnemeth 	 * disk_detach() will release it when we go away.
   2615  1.4   jnemeth 	 */
   2616  1.4   jnemeth 
   2617  1.4   jnemeth 	odisk_info = fd->sc_dk.dk_info;
   2618  1.4   jnemeth 	fd->sc_dk.dk_info = disk_info;
   2619  1.4   jnemeth 	if (odisk_info)
   2620  1.4   jnemeth 		prop_object_release(odisk_info);
   2621  1.4   jnemeth }
   2622