Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.55
      1  1.55   thorpej /*	$NetBSD: fd.c,v 1.55 1998/01/12 20:23:49 thorpej Exp $	*/
      2   1.1        pk 
      3   1.1        pk /*-
      4   1.1        pk  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
      5   1.1        pk  * Copyright (c) 1995 Paul Kranenburg.
      6   1.1        pk  * Copyright (c) 1990 The Regents of the University of California.
      7   1.1        pk  * All rights reserved.
      8   1.1        pk  *
      9   1.1        pk  * This code is derived from software contributed to Berkeley by
     10   1.1        pk  * Don Ahn.
     11   1.1        pk  *
     12   1.1        pk  * Redistribution and use in source and binary forms, with or without
     13   1.1        pk  * modification, are permitted provided that the following conditions
     14   1.1        pk  * are met:
     15   1.1        pk  * 1. Redistributions of source code must retain the above copyright
     16   1.1        pk  *    notice, this list of conditions and the following disclaimer.
     17   1.1        pk  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1        pk  *    notice, this list of conditions and the following disclaimer in the
     19   1.1        pk  *    documentation and/or other materials provided with the distribution.
     20   1.1        pk  * 3. All advertising materials mentioning features or use of this software
     21   1.1        pk  *    must display the following acknowledgement:
     22   1.1        pk  *	This product includes software developed by the University of
     23   1.1        pk  *	California, Berkeley and its contributors.
     24   1.1        pk  * 4. Neither the name of the University nor the names of its contributors
     25   1.1        pk  *    may be used to endorse or promote products derived from this software
     26   1.1        pk  *    without specific prior written permission.
     27   1.1        pk  *
     28   1.1        pk  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.1        pk  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.1        pk  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.1        pk  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.1        pk  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.1        pk  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.1        pk  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.1        pk  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.1        pk  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.1        pk  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.1        pk  * SUCH DAMAGE.
     39   1.1        pk  *
     40   1.1        pk  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     41   1.1        pk  */
     42   1.1        pk 
     43   1.1        pk #include <sys/param.h>
     44   1.1        pk #include <sys/systm.h>
     45   1.1        pk #include <sys/kernel.h>
     46   1.1        pk #include <sys/file.h>
     47   1.1        pk #include <sys/ioctl.h>
     48   1.1        pk #include <sys/device.h>
     49   1.1        pk #include <sys/disklabel.h>
     50   1.1        pk #include <sys/dkstat.h>
     51   1.1        pk #include <sys/disk.h>
     52  1.42        pk #include <sys/fdio.h>
     53   1.1        pk #include <sys/buf.h>
     54  1.42        pk #include <sys/malloc.h>
     55  1.42        pk #include <sys/proc.h>
     56   1.1        pk #include <sys/uio.h>
     57  1.19   thorpej #include <sys/stat.h>
     58   1.1        pk #include <sys/syslog.h>
     59   1.1        pk #include <sys/queue.h>
     60  1.30  christos #include <sys/conf.h>
     61  1.24  christos 
     62  1.24  christos #include <dev/cons.h>
     63   1.1        pk 
     64   1.1        pk #include <machine/cpu.h>
     65   1.1        pk #include <machine/autoconf.h>
     66  1.30  christos #include <machine/conf.h>
     67  1.30  christos 
     68   1.2        pk #include <sparc/sparc/auxreg.h>
     69   1.1        pk #include <sparc/dev/fdreg.h>
     70   1.2        pk #include <sparc/dev/fdvar.h>
     71   1.1        pk 
     72   1.1        pk #define FDUNIT(dev)	(minor(dev) / 8)
     73   1.1        pk #define FDTYPE(dev)	(minor(dev) % 8)
     74   1.1        pk 
     75  1.42        pk /* XXX misuse a flag to identify format operation */
     76  1.42        pk #define B_FORMAT B_XXX
     77   1.1        pk #define b_cylin b_resid
     78   1.1        pk 
     79   1.2        pk #define FD_DEBUG
     80   1.2        pk #ifdef FD_DEBUG
     81   1.2        pk int	fdc_debug = 0;
     82   1.2        pk #endif
     83   1.2        pk 
     84   1.1        pk enum fdc_state {
     85   1.1        pk 	DEVIDLE = 0,
     86   1.1        pk 	MOTORWAIT,
     87   1.1        pk 	DOSEEK,
     88   1.1        pk 	SEEKWAIT,
     89   1.1        pk 	SEEKTIMEDOUT,
     90   1.1        pk 	SEEKCOMPLETE,
     91   1.1        pk 	DOIO,
     92   1.2        pk 	IOCOMPLETE,
     93   1.1        pk 	IOTIMEDOUT,
     94   1.1        pk 	DORESET,
     95   1.1        pk 	RESETCOMPLETE,
     96   1.1        pk 	RESETTIMEDOUT,
     97   1.1        pk 	DORECAL,
     98   1.1        pk 	RECALWAIT,
     99   1.1        pk 	RECALTIMEDOUT,
    100   1.1        pk 	RECALCOMPLETE,
    101   1.1        pk };
    102   1.1        pk 
    103   1.1        pk /* software state, per controller */
    104   1.1        pk struct fdc_softc {
    105  1.14        pk 	struct device	sc_dev;		/* boilerplate */
    106   1.1        pk 	struct intrhand sc_sih;
    107   1.1        pk 	struct intrhand sc_hih;
    108   1.1        pk 	caddr_t		sc_reg;
    109   1.1        pk 	struct fd_softc *sc_fd[4];	/* pointers to children */
    110   1.1        pk 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    111   1.1        pk 	enum fdc_state	sc_state;
    112   1.2        pk 	int		sc_flags;
    113   1.2        pk #define FDC_82077		0x01
    114   1.2        pk #define FDC_NEEDHEADSETTLE	0x02
    115   1.2        pk #define FDC_EIS			0x04
    116   1.2        pk 	int		sc_errors;		/* number of retries so far */
    117   1.6        pk 	int		sc_overruns;		/* number of DMA overruns */
    118   1.2        pk 	int		sc_cfg;			/* current configuration */
    119   1.2        pk 	struct fdcio	sc_io;
    120   1.2        pk #define sc_reg_msr	sc_io.fdcio_reg_msr
    121   1.2        pk #define sc_reg_fifo	sc_io.fdcio_reg_fifo
    122   1.2        pk #define sc_reg_dor	sc_io.fdcio_reg_dor
    123   1.2        pk #define sc_reg_drs	sc_io.fdcio_reg_msr
    124   1.2        pk #define sc_istate	sc_io.fdcio_istate
    125   1.2        pk #define sc_data		sc_io.fdcio_data
    126   1.2        pk #define sc_tc		sc_io.fdcio_tc
    127   1.2        pk #define sc_nstat	sc_io.fdcio_nstat
    128   1.2        pk #define sc_status	sc_io.fdcio_status
    129   1.3        pk #define sc_intrcnt	sc_io.fdcio_intrcnt
    130   1.1        pk };
    131   1.1        pk 
    132   1.2        pk #ifndef FDC_C_HANDLER
    133   1.2        pk extern	struct fdcio	*fdciop;
    134   1.2        pk #endif
    135   1.2        pk 
    136   1.1        pk /* controller driver configuration */
    137  1.44        pk int	fdcmatch __P((struct device *, struct cfdata *, void *));
    138   1.1        pk void	fdcattach __P((struct device *, struct device *, void *));
    139   1.1        pk 
    140  1.26   thorpej struct cfattach fdc_ca = {
    141  1.26   thorpej 	sizeof(struct fdc_softc), fdcmatch, fdcattach
    142  1.26   thorpej };
    143  1.24  christos 
    144  1.24  christos __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
    145  1.24  christos 
    146   1.1        pk /*
    147   1.1        pk  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    148   1.1        pk  * we tell them apart.
    149   1.1        pk  */
    150   1.1        pk struct fd_type {
    151   1.1        pk 	int	sectrac;	/* sectors per track */
    152   1.1        pk 	int	heads;		/* number of heads */
    153   1.1        pk 	int	seccyl;		/* sectors per cylinder */
    154   1.1        pk 	int	secsize;	/* size code for sectors */
    155   1.1        pk 	int	datalen;	/* data len when secsize = 0 */
    156   1.1        pk 	int	steprate;	/* step rate and head unload time */
    157   1.1        pk 	int	gap1;		/* gap len between sectors */
    158   1.1        pk 	int	gap2;		/* formatting gap */
    159  1.49        pk 	int	cylinders;	/* total num of cylinders */
    160   1.1        pk 	int	size;		/* size of disk in sectors */
    161   1.1        pk 	int	step;		/* steps per cylinder */
    162   1.1        pk 	int	rate;		/* transfer speed code */
    163  1.42        pk 	int	fillbyte;	/* format fill byte */
    164  1.42        pk 	int	interleave;	/* interleave factor (formatting) */
    165   1.1        pk 	char	*name;
    166   1.1        pk };
    167   1.1        pk 
    168   1.1        pk /* The order of entries in the following table is important -- BEWARE! */
    169   1.1        pk struct fd_type fd_types[] = {
    170  1.42        pk 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette */
    171  1.42        pk 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5" 720kB diskette */
    172  1.42        pk 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
    173  1.53        pk 	{  8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,0xf6,1, "1.2MB/NEC" } /* 1.2 MB japanese format */
    174   1.1        pk };
    175   1.1        pk 
    176   1.1        pk /* software state, per disk (with up to 4 disks per ctlr) */
    177   1.1        pk struct fd_softc {
    178  1.16   thorpej 	struct device	sc_dv;		/* generic device info */
    179  1.16   thorpej 	struct disk	sc_dk;		/* generic disk info */
    180   1.1        pk 
    181   1.1        pk 	struct fd_type *sc_deftype;	/* default type descriptor */
    182   1.1        pk 	struct fd_type *sc_type;	/* current type descriptor */
    183   1.1        pk 
    184   1.1        pk 	daddr_t	sc_blkno;	/* starting block number */
    185   1.1        pk 	int sc_bcount;		/* byte count left */
    186   1.1        pk 	int sc_skip;		/* bytes already transferred */
    187   1.1        pk 	int sc_nblks;		/* number of blocks currently tranferring */
    188   1.1        pk 	int sc_nbytes;		/* number of bytes currently tranferring */
    189   1.1        pk 
    190   1.1        pk 	int sc_drive;		/* physical unit number */
    191   1.1        pk 	int sc_flags;
    192   1.1        pk #define	FD_OPEN		0x01		/* it's open */
    193   1.1        pk #define	FD_MOTOR	0x02		/* motor should be on */
    194   1.1        pk #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    195   1.1        pk 	int sc_cylin;		/* where we think the head is */
    196  1.42        pk 	int sc_opts;		/* user-set options */
    197   1.1        pk 
    198  1.20   thorpej 	void	*sc_sdhook;	/* shutdownhook cookie */
    199  1.20   thorpej 
    200   1.1        pk 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    201   1.1        pk 	int sc_ops;		/* I/O ops since last switch */
    202   1.1        pk 	struct buf sc_q;	/* head of buf chain */
    203   1.1        pk };
    204   1.1        pk 
    205   1.1        pk /* floppy driver configuration */
    206  1.44        pk int	fdmatch __P((struct device *, struct cfdata *, void *));
    207   1.1        pk void	fdattach __P((struct device *, struct device *, void *));
    208   1.1        pk 
    209  1.26   thorpej struct cfattach fd_ca = {
    210  1.26   thorpej 	sizeof(struct fd_softc), fdmatch, fdattach
    211  1.26   thorpej };
    212  1.26   thorpej 
    213  1.55   thorpej extern struct cfdriver fd_cd;
    214   1.1        pk 
    215  1.19   thorpej void fdgetdisklabel __P((dev_t));
    216   1.1        pk int fd_get_parms __P((struct fd_softc *));
    217   1.1        pk void fdstrategy __P((struct buf *));
    218   1.1        pk void fdstart __P((struct fd_softc *));
    219  1.37       cgd int fdprint __P((void *, const char *));
    220   1.1        pk 
    221   1.1        pk struct dkdriver fddkdriver = { fdstrategy };
    222   1.1        pk 
    223   1.1        pk struct	fd_type *fd_nvtotype __P((char *, int, int));
    224  1.12        pk void	fd_set_motor __P((struct fdc_softc *fdc));
    225   1.1        pk void	fd_motor_off __P((void *arg));
    226   1.1        pk void	fd_motor_on __P((void *arg));
    227   1.1        pk int	fdcresult __P((struct fdc_softc *fdc));
    228   1.1        pk int	out_fdc __P((struct fdc_softc *fdc, u_char x));
    229   1.1        pk void	fdcstart __P((struct fdc_softc *fdc));
    230   1.1        pk void	fdcstatus __P((struct device *dv, int n, char *s));
    231  1.12        pk void	fdc_reset __P((struct fdc_softc *fdc));
    232   1.1        pk void	fdctimeout __P((void *arg));
    233   1.1        pk void	fdcpseudointr __P((void *arg));
    234   1.2        pk #ifdef FDC_C_HANDLER
    235   1.1        pk int	fdchwintr __P((struct fdc_softc *));
    236   1.2        pk #else
    237   1.2        pk void	fdchwintr __P((void));
    238   1.2        pk #endif
    239   1.1        pk int	fdcswintr __P((struct fdc_softc *));
    240  1.42        pk int	fdcstate __P((struct fdc_softc *));
    241   1.1        pk void	fdcretry __P((struct fdc_softc *fdc));
    242   1.1        pk void	fdfinish __P((struct fd_softc *fd, struct buf *bp));
    243  1.42        pk int	fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
    244  1.50        pk void	fd_do_eject __P((struct fd_softc *));
    245  1.19   thorpej void	fd_mountroot_hook __P((struct device *));
    246  1.24  christos static void fdconf __P((struct fdc_softc *));
    247   1.1        pk 
    248   1.2        pk #if PIL_FDSOFT == 4
    249   1.1        pk #define IE_FDSOFT	IE_L4
    250   1.2        pk #else
    251   1.2        pk #error 4
    252   1.2        pk #endif
    253   1.1        pk 
    254  1.42        pk #ifdef FDC_C_HANDLER
    255  1.42        pk #if defined(SUN4M)
    256  1.42        pk #define FD_SET_SWINTR do {		\
    257  1.42        pk 	if (CPU_ISSUN4M)		\
    258  1.42        pk 		raise(0, PIL_FDSOFT);	\
    259  1.42        pk 	else				\
    260  1.42        pk 		ienab_bis(IE_L4);	\
    261  1.42        pk } while(0)
    262  1.42        pk #else
    263  1.42        pk #define AUDIO_SET_SWINTR ienab_bis(IE_FDSOFT)
    264  1.42        pk #endif /* defined(SUN4M) */
    265  1.42        pk #endif /* FDC_C_HANDLER */
    266  1.42        pk 
    267  1.23        pk #define OBP_FDNAME	(CPU_ISSUN4M ? "SUNW,fdtwo" : "fd")
    268  1.10        pk 
    269   1.1        pk int
    270   1.1        pk fdcmatch(parent, match, aux)
    271   1.1        pk 	struct device *parent;
    272  1.44        pk 	struct cfdata *match;
    273  1.44        pk 	void *aux;
    274   1.1        pk {
    275   1.1        pk 	register struct confargs *ca = aux;
    276   1.1        pk 	register struct romaux *ra = &ca->ca_ra;
    277   1.1        pk 
    278  1.23        pk 	/*
    279  1.23        pk 	 * Floppy doesn't exist on sun4.
    280  1.23        pk 	 */
    281  1.23        pk 	if (CPU_ISSUN4)
    282  1.23        pk 		return (0);
    283  1.23        pk 
    284  1.23        pk 	/*
    285  1.23        pk 	 * Floppy controller is on mainbus on sun4c.
    286  1.23        pk 	 */
    287  1.23        pk 	if ((CPU_ISSUN4C) && (ca->ca_bustype != BUS_MAIN))
    288  1.23        pk 		return (0);
    289  1.23        pk 
    290  1.23        pk 	/*
    291  1.23        pk 	 * Floppy controller is on obio on sun4m.
    292  1.23        pk 	 */
    293  1.23        pk 	if ((CPU_ISSUN4M) && (ca->ca_bustype != BUS_OBIO))
    294  1.23        pk 		return (0);
    295  1.23        pk 
    296  1.10        pk 	/* Sun PROMs call the controller an "fd" or "SUNW,fdtwo" */
    297  1.10        pk 	if (strcmp(OBP_FDNAME, ra->ra_name))
    298   1.1        pk 		return (0);
    299  1.23        pk 
    300  1.23        pk 	if (ca->ca_ra.ra_vaddr &&
    301  1.23        pk 	    probeget(ca->ca_ra.ra_vaddr, 1) == -1) {
    302  1.23        pk 		return (0);
    303   1.5        pk 	}
    304   1.1        pk 
    305  1.23        pk 	return (1);
    306   1.1        pk }
    307   1.1        pk 
    308   1.1        pk /*
    309   1.1        pk  * Arguments passed between fdcattach and fdprobe.
    310   1.1        pk  */
    311   1.1        pk struct fdc_attach_args {
    312   1.1        pk 	int fa_drive;
    313  1.31        pk 	struct bootpath *fa_bootpath;
    314   1.1        pk 	struct fd_type *fa_deftype;
    315   1.1        pk };
    316   1.1        pk 
    317   1.1        pk /*
    318   1.1        pk  * Print the location of a disk drive (called just before attaching the
    319   1.1        pk  * the drive).  If `fdc' is not NULL, the drive was found but was not
    320   1.1        pk  * in the system config file; print the drive name as well.
    321   1.1        pk  * Return QUIET (config_find ignores this if the device was configured) to
    322   1.1        pk  * avoid printing `fdN not configured' messages.
    323   1.1        pk  */
    324   1.1        pk int
    325   1.1        pk fdprint(aux, fdc)
    326   1.1        pk 	void *aux;
    327  1.37       cgd 	const char *fdc;
    328   1.1        pk {
    329   1.1        pk 	register struct fdc_attach_args *fa = aux;
    330   1.1        pk 
    331   1.1        pk 	if (!fdc)
    332  1.39  christos 		printf(" drive %d", fa->fa_drive);
    333  1.42        pk 	return (QUIET);
    334   1.1        pk }
    335   1.1        pk 
    336   1.1        pk static void
    337   1.1        pk fdconf(fdc)
    338   1.1        pk 	struct fdc_softc *fdc;
    339   1.1        pk {
    340   1.1        pk 	int	vroom;
    341   1.1        pk 
    342   1.1        pk 	if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
    343   1.1        pk 		return;
    344   1.1        pk 
    345   1.1        pk 	/*
    346   1.1        pk 	 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
    347   1.1        pk 	 * the PROM thinks is appropriate.
    348   1.1        pk 	 */
    349   1.1        pk 	if ((vroom = fdc->sc_status[7]) == 0)
    350   1.1        pk 		vroom = 0x64;
    351   1.1        pk 
    352   1.1        pk 	/* Configure controller to use FIFO and Implied Seek */
    353   1.1        pk 	out_fdc(fdc, NE7CMD_CFG);
    354   1.1        pk 	out_fdc(fdc, vroom);
    355   1.2        pk 	out_fdc(fdc, fdc->sc_cfg);
    356   1.1        pk 	out_fdc(fdc, 0); /* PRETRK */
    357   1.1        pk 	/* No result phase */
    358   1.1        pk }
    359   1.1        pk 
    360   1.1        pk void
    361   1.1        pk fdcattach(parent, self, aux)
    362   1.1        pk 	struct device *parent, *self;
    363   1.1        pk 	void *aux;
    364   1.1        pk {
    365   1.1        pk 	register struct confargs *ca = aux;
    366   1.1        pk 	struct fdc_softc *fdc = (void *)self;
    367   1.1        pk 	struct fdc_attach_args fa;
    368  1.23        pk 	struct bootpath *bp;
    369  1.24  christos 	int pri;
    370   1.8        pk 	char code;
    371   1.1        pk 
    372   1.1        pk 	if (ca->ca_ra.ra_vaddr)
    373   1.1        pk 		fdc->sc_reg = (caddr_t)ca->ca_ra.ra_vaddr;
    374   1.1        pk 	else
    375  1.15        pk 		fdc->sc_reg = (caddr_t)mapiodev(ca->ca_ra.ra_reg, 0,
    376  1.51        pk 						ca->ca_ra.ra_len);
    377   1.1        pk 
    378   1.2        pk 	fdc->sc_state = DEVIDLE;
    379   1.2        pk 	fdc->sc_istate = ISTATE_IDLE;
    380   1.2        pk 	fdc->sc_flags |= FDC_EIS;
    381   1.2        pk 	TAILQ_INIT(&fdc->sc_drives);
    382   1.2        pk 
    383   1.1        pk 	pri = ca->ca_ra.ra_intr[0].int_pri;
    384   1.2        pk #ifdef FDC_C_HANDLER
    385   1.1        pk 	fdc->sc_hih.ih_fun = (void *)fdchwintr;
    386   1.1        pk 	fdc->sc_hih.ih_arg = fdc;
    387   1.1        pk 	intr_establish(pri, &fdc->sc_hih);
    388   1.2        pk #else
    389   1.2        pk 	fdciop = &fdc->sc_io;
    390   1.2        pk 	intr_fasttrap(pri, fdchwintr);
    391   1.2        pk #endif
    392   1.1        pk 	fdc->sc_sih.ih_fun = (void *)fdcswintr;
    393   1.1        pk 	fdc->sc_sih.ih_arg = fdc;
    394   1.2        pk 	intr_establish(PIL_FDSOFT, &fdc->sc_sih);
    395   1.1        pk 
    396   1.8        pk 	/* Assume a 82077 */
    397   1.8        pk 	fdc->sc_reg_msr = &((struct fdreg_77 *)fdc->sc_reg)->fd_msr;
    398   1.8        pk 	fdc->sc_reg_fifo = &((struct fdreg_77 *)fdc->sc_reg)->fd_fifo;
    399   1.8        pk 	fdc->sc_reg_dor = &((struct fdreg_77 *)fdc->sc_reg)->fd_dor;
    400   1.8        pk 
    401   1.8        pk 	code = '7';
    402   1.8        pk 	if (*fdc->sc_reg_dor == NE7_RQM) {
    403   1.8        pk 		/*
    404   1.8        pk 		 * This hack from Chris Torek: apparently DOR really
    405   1.8        pk 		 * addresses MSR/DRS on a 82072.
    406   1.8        pk 		 * We used to rely on the VERSION command to tell the
    407   1.8        pk 		 * difference (which did not work).
    408   1.8        pk 		 */
    409   1.8        pk 		*fdc->sc_reg_dor = FDC_250KBPS;
    410   1.8        pk 		if (*fdc->sc_reg_dor == NE7_RQM)
    411   1.8        pk 			code = '2';
    412   1.5        pk 	}
    413   1.8        pk 	if (code == '7') {
    414   1.1        pk 		fdc->sc_flags |= FDC_82077;
    415   1.1        pk 	} else {
    416   1.8        pk 		fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr;
    417   1.8        pk 		fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo;
    418   1.8        pk 		fdc->sc_reg_dor = 0;
    419   1.1        pk 	}
    420   1.1        pk 
    421   1.8        pk #ifdef FD_DEBUG
    422   1.8        pk 	if (out_fdc(fdc, NE7CMD_VERSION) == 0 &&
    423   1.8        pk 	    fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
    424   1.8        pk 		if (fdc_debug)
    425  1.39  christos 			printf("[version cmd]");
    426   1.8        pk 	}
    427   1.8        pk #endif
    428   1.8        pk 
    429   1.2        pk 	/*
    430   1.2        pk 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
    431   1.6        pk 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
    432   1.2        pk 	 */
    433   1.6        pk 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
    434   1.1        pk 	fdconf(fdc);
    435   1.1        pk 
    436   1.1        pk 	if (fdc->sc_flags & FDC_82077) {
    437   1.1        pk 		/* Lock configuration across soft resets. */
    438   1.1        pk 		out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK);
    439   1.1        pk 		if (fdcresult(fdc) != 1)
    440  1.39  christos 			printf(" CFGLOCK: unexpected response");
    441   1.1        pk 	}
    442   1.3        pk 
    443  1.14        pk 	evcnt_attach(&fdc->sc_dev, "intr", &fdc->sc_intrcnt);
    444   1.1        pk 
    445  1.39  christos 	printf(" pri %d, softpri %d: chip 8207%c\n", pri, PIL_FDSOFT, code);
    446   1.1        pk 
    447  1.10        pk 	/*
    448  1.10        pk 	 * Controller and drives are represented by one and the same
    449  1.10        pk 	 * Openprom node, so we can as well check for the floppy boots here.
    450  1.10        pk 	 */
    451  1.31        pk 	fa.fa_bootpath = 0;
    452  1.23        pk 	if ((bp = ca->ca_ra.ra_bp) && strcmp(bp->name, OBP_FDNAME) == 0) {
    453  1.35        pk 
    454  1.23        pk 		switch (ca->ca_bustype) {
    455  1.23        pk 		case BUS_MAIN:
    456  1.35        pk 			/*
    457  1.35        pk 			 * We can get the bootpath in several different
    458  1.35        pk 			 * formats! The faked v1 bootpath looks like /fd@0,0.
    459  1.35        pk 			 * The v2 bootpath is either just /fd0, in which case
    460  1.35        pk 			 * `bp->val[0]' will have been set to -1, or /fd@x,y
    461  1.35        pk 			 * where <x,y> is the prom address specifier.
    462  1.35        pk 			 */
    463  1.35        pk 			if (((bp->val[0] == ca->ca_ra.ra_iospace) &&
    464  1.35        pk 			     (bp->val[1] == (int)ca->ca_ra.ra_paddr)) ||
    465  1.35        pk 
    466  1.35        pk 			    ((bp->val[0] == -1) &&	/* v2: /fd0 */
    467  1.23        pk 			     (bp->val[1] == 0)) ||
    468  1.35        pk 
    469  1.35        pk 			    ((bp->val[0] == 0) &&	/* v1: /fd@0,0 */
    470  1.35        pk 			     (bp->val[1] == 0))
    471  1.35        pk 			   )
    472  1.31        pk 				fa.fa_bootpath = bp;
    473  1.23        pk 			break;
    474  1.23        pk 
    475  1.23        pk 		case BUS_OBIO:
    476  1.35        pk 			/*
    477  1.35        pk 			 * floppy controller on obio (such as on the sun4m),
    478  1.35        pk 			 * e.g.: `/obio0/SUNW,fdtwo@0,700000'.
    479  1.35        pk 			 * We use "slot, offset" to determine if this is the
    480  1.35        pk 			 * right one.
    481  1.35        pk 			 */
    482  1.23        pk 			if ((bp->val[0] == ca->ca_slot) &&
    483  1.23        pk 			    (bp->val[1] == ca->ca_offset))
    484  1.31        pk 				fa.fa_bootpath = bp;
    485  1.23        pk 			break;
    486  1.23        pk 		}
    487  1.29        pk 
    488  1.19   thorpej 	}
    489  1.10        pk 
    490   1.1        pk 	/* physical limit: four drives per controller. */
    491   1.1        pk 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    492   1.1        pk 		fa.fa_deftype = NULL;		/* unknown */
    493   1.1        pk 	fa.fa_deftype = &fd_types[0];		/* XXX */
    494   1.1        pk 		(void)config_found(self, (void *)&fa, fdprint);
    495   1.1        pk 	}
    496  1.16   thorpej 
    497  1.16   thorpej 	bootpath_store(1, NULL);
    498   1.1        pk }
    499   1.1        pk 
    500   1.1        pk int
    501   1.1        pk fdmatch(parent, match, aux)
    502   1.1        pk 	struct device *parent;
    503  1.44        pk 	struct cfdata *match;
    504  1.44        pk 	void *aux;
    505   1.1        pk {
    506   1.1        pk 	struct fdc_softc *fdc = (void *)parent;
    507   1.1        pk 	struct fdc_attach_args *fa = aux;
    508   1.1        pk 	int drive = fa->fa_drive;
    509  1.33        pk 	int n, ok;
    510   1.1        pk 
    511   1.8        pk 	if (drive > 0)
    512  1.50        pk 		/* XXX - for now, punt on more than one drive */
    513  1.42        pk 		return (0);
    514   1.8        pk 
    515   1.1        pk 	if (fdc->sc_flags & FDC_82077) {
    516   1.1        pk 		/* select drive and turn on motor */
    517   1.1        pk 		*fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive);
    518   1.1        pk 		/* wait for motor to spin up */
    519   1.1        pk 		delay(250000);
    520   1.1        pk 	} else {
    521  1.49        pk 		auxregbisc(AUXIO4C_FDS, 0);
    522   1.1        pk 	}
    523   1.1        pk 	fdc->sc_nstat = 0;
    524   1.1        pk 	out_fdc(fdc, NE7CMD_RECAL);
    525   1.1        pk 	out_fdc(fdc, drive);
    526   1.1        pk 	/* wait for recalibrate */
    527  1.33        pk 	for (n = 0; n < 10000; n++) {
    528  1.33        pk 		delay(1000);
    529   1.2        pk 		if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
    530   1.2        pk 			/* wait a bit longer till device *really* is ready */
    531   1.2        pk 			delay(100000);
    532   1.2        pk 			if (out_fdc(fdc, NE7CMD_SENSEI))
    533   1.2        pk 				break;
    534   1.7        pk 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
    535   1.2        pk 				/*
    536   1.2        pk 				 * Got `invalid command'; we interpret it
    537   1.2        pk 				 * to mean that the re-calibrate hasn't in
    538   1.2        pk 				 * fact finished yet
    539   1.2        pk 				 */
    540   1.2        pk 				continue;
    541   1.1        pk 			break;
    542   1.1        pk 		}
    543   1.1        pk 	}
    544   1.1        pk 	n = fdc->sc_nstat;
    545   1.1        pk #ifdef FD_DEBUG
    546   1.2        pk 	if (fdc_debug) {
    547   1.1        pk 		int i;
    548  1.39  christos 		printf("fdprobe: %d stati:", n);
    549   1.1        pk 		for (i = 0; i < n; i++)
    550  1.52      fair 			printf(" 0x%x", fdc->sc_status[i]);
    551  1.39  christos 		printf("\n");
    552   1.1        pk 	}
    553   1.1        pk #endif
    554  1.33        pk 	ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
    555  1.33        pk 
    556   1.1        pk 	/* turn off motor */
    557   1.1        pk 	if (fdc->sc_flags & FDC_82077) {
    558  1.50        pk 		/* deselect drive and turn motor off */
    559  1.50        pk 		*fdc->sc_reg_dor = FDO_FRST | FDO_DS;
    560   1.1        pk 	} else {
    561  1.49        pk 		auxregbisc(0, AUXIO4C_FDS);
    562   1.1        pk 	}
    563   1.1        pk 
    564  1.42        pk 	return (ok);
    565   1.1        pk }
    566   1.1        pk 
    567   1.1        pk /*
    568   1.1        pk  * Controller is working, and drive responded.  Attach it.
    569   1.1        pk  */
    570   1.1        pk void
    571   1.1        pk fdattach(parent, self, aux)
    572   1.1        pk 	struct device *parent, *self;
    573   1.1        pk 	void *aux;
    574   1.1        pk {
    575   1.1        pk 	struct fdc_softc *fdc = (void *)parent;
    576   1.1        pk 	struct fd_softc *fd = (void *)self;
    577   1.1        pk 	struct fdc_attach_args *fa = aux;
    578   1.1        pk 	struct fd_type *type = fa->fa_deftype;
    579   1.1        pk 	int drive = fa->fa_drive;
    580   1.1        pk 
    581   1.1        pk 	/* XXX Allow `flags' to override device type? */
    582   1.1        pk 
    583   1.1        pk 	if (type)
    584  1.39  christos 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    585  1.49        pk 		    type->cylinders, type->heads, type->sectrac);
    586   1.1        pk 	else
    587  1.39  christos 		printf(": density unknown\n");
    588   1.1        pk 
    589   1.1        pk 	fd->sc_cylin = -1;
    590   1.1        pk 	fd->sc_drive = drive;
    591   1.1        pk 	fd->sc_deftype = type;
    592   1.1        pk 	fdc->sc_fd[drive] = fd;
    593  1.16   thorpej 
    594  1.50        pk 	out_fdc(fdc, NE7CMD_SPECIFY);
    595  1.50        pk 	out_fdc(fdc, type->steprate);
    596  1.50        pk 	out_fdc(fdc, 6 | NE7_SPECIFY_NODMA);
    597  1.50        pk 
    598  1.16   thorpej 	/*
    599  1.16   thorpej 	 * Initialize and attach the disk structure.
    600  1.16   thorpej 	 */
    601  1.16   thorpej 	fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
    602   1.1        pk 	fd->sc_dk.dk_driver = &fddkdriver;
    603  1.16   thorpej 	disk_attach(&fd->sc_dk);
    604  1.10        pk 
    605  1.13        pk 	/*
    606  1.13        pk 	 * We're told if we're the boot device in fdcattach().
    607  1.13        pk 	 */
    608  1.31        pk 	if (fa->fa_bootpath)
    609  1.31        pk 		fa->fa_bootpath->dev = &fd->sc_dv;
    610  1.13        pk 
    611  1.19   thorpej 	/*
    612  1.19   thorpej 	 * Establish a mountroot_hook anyway in case we booted
    613  1.19   thorpej 	 * with RB_ASKNAME and get selected as the boot device.
    614  1.19   thorpej 	 */
    615  1.47   thorpej 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
    616  1.20   thorpej 
    617  1.20   thorpej 	/* Make sure the drive motor gets turned off at shutdown time. */
    618  1.20   thorpej 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
    619  1.19   thorpej 
    620   1.1        pk 	/* XXX Need to do some more fiddling with sc_dk. */
    621  1.16   thorpej 	dk_establish(&fd->sc_dk, &fd->sc_dv);
    622   1.1        pk }
    623   1.1        pk 
    624  1.24  christos __inline struct fd_type *
    625   1.1        pk fd_dev_to_type(fd, dev)
    626   1.1        pk 	struct fd_softc *fd;
    627   1.1        pk 	dev_t dev;
    628   1.1        pk {
    629   1.1        pk 	int type = FDTYPE(dev);
    630   1.1        pk 
    631   1.1        pk 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    632  1.42        pk 		return (NULL);
    633  1.42        pk 	return (type ? &fd_types[type - 1] : fd->sc_deftype);
    634   1.1        pk }
    635   1.1        pk 
    636   1.1        pk void
    637   1.1        pk fdstrategy(bp)
    638   1.1        pk 	register struct buf *bp;	/* IO operation to perform */
    639   1.1        pk {
    640   1.1        pk 	struct fd_softc *fd;
    641   1.1        pk 	int unit = FDUNIT(bp->b_dev);
    642   1.1        pk 	int sz;
    643   1.1        pk  	int s;
    644   1.1        pk 
    645   1.1        pk 	/* Valid unit, controller, and request? */
    646  1.26   thorpej 	if (unit >= fd_cd.cd_ndevs ||
    647  1.26   thorpej 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
    648   1.1        pk 	    bp->b_blkno < 0 ||
    649  1.53        pk 	    (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
    650  1.53        pk 	      (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
    651  1.43        pk 	     (bp->b_flags & B_FORMAT) == 0)) {
    652   1.1        pk 		bp->b_error = EINVAL;
    653   1.1        pk 		goto bad;
    654   1.1        pk 	}
    655   1.1        pk 
    656   1.1        pk 	/* If it's a null transfer, return immediately. */
    657   1.1        pk 	if (bp->b_bcount == 0)
    658   1.1        pk 		goto done;
    659   1.1        pk 
    660  1.53        pk 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    661   1.1        pk 
    662  1.53        pk 	if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
    663  1.53        pk 		sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
    664  1.53        pk 		     - bp->b_blkno;
    665   1.1        pk 		if (sz == 0) {
    666   1.1        pk 			/* If exactly at end of disk, return EOF. */
    667   1.1        pk 			bp->b_resid = bp->b_bcount;
    668   1.1        pk 			goto done;
    669   1.1        pk 		}
    670   1.1        pk 		if (sz < 0) {
    671   1.1        pk 			/* If past end of disk, return EINVAL. */
    672   1.1        pk 			bp->b_error = EINVAL;
    673   1.1        pk 			goto bad;
    674   1.1        pk 		}
    675   1.1        pk 		/* Otherwise, truncate request. */
    676   1.1        pk 		bp->b_bcount = sz << DEV_BSHIFT;
    677   1.1        pk 	}
    678   1.1        pk 
    679  1.53        pk  	bp->b_cylin = (bp->b_blkno * DEV_BSIZE) /
    680  1.53        pk 		      (FD_BSIZE(fd) * fd->sc_type->seccyl);
    681   1.1        pk 
    682   1.1        pk #ifdef FD_DEBUG
    683   1.2        pk 	if (fdc_debug > 1)
    684  1.39  christos 	    printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n",
    685   1.2        pk 		    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
    686   1.1        pk #endif
    687   1.1        pk 
    688   1.1        pk 	/* Queue transfer on drive, activate drive and controller if idle. */
    689   1.1        pk 	s = splbio();
    690   1.1        pk 	disksort(&fd->sc_q, bp);
    691   1.1        pk 	untimeout(fd_motor_off, fd); /* a good idea */
    692   1.1        pk 	if (!fd->sc_q.b_active)
    693   1.1        pk 		fdstart(fd);
    694   1.1        pk #ifdef DIAGNOSTIC
    695   1.1        pk 	else {
    696  1.16   thorpej 		struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
    697   1.1        pk 		if (fdc->sc_state == DEVIDLE) {
    698  1.39  christos 			printf("fdstrategy: controller inactive\n");
    699   1.1        pk 			fdcstart(fdc);
    700   1.1        pk 		}
    701   1.1        pk 	}
    702   1.1        pk #endif
    703   1.1        pk 	splx(s);
    704   1.1        pk 	return;
    705   1.1        pk 
    706   1.1        pk bad:
    707   1.1        pk 	bp->b_flags |= B_ERROR;
    708   1.1        pk done:
    709   1.1        pk 	/* Toss transfer; we're done early. */
    710   1.1        pk 	biodone(bp);
    711   1.1        pk }
    712   1.1        pk 
    713   1.1        pk void
    714   1.1        pk fdstart(fd)
    715   1.1        pk 	struct fd_softc *fd;
    716   1.1        pk {
    717  1.16   thorpej 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
    718   1.1        pk 	int active = fdc->sc_drives.tqh_first != 0;
    719   1.1        pk 
    720   1.1        pk 	/* Link into controller queue. */
    721   1.1        pk 	fd->sc_q.b_active = 1;
    722   1.1        pk 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    723   1.1        pk 
    724   1.1        pk 	/* If controller not already active, start it. */
    725   1.1        pk 	if (!active)
    726   1.1        pk 		fdcstart(fdc);
    727   1.1        pk }
    728   1.1        pk 
    729   1.1        pk void
    730   1.1        pk fdfinish(fd, bp)
    731   1.1        pk 	struct fd_softc *fd;
    732   1.1        pk 	struct buf *bp;
    733   1.1        pk {
    734  1.16   thorpej 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
    735   1.1        pk 
    736   1.1        pk 	/*
    737   1.1        pk 	 * Move this drive to the end of the queue to give others a `fair'
    738   1.1        pk 	 * chance.  We only force a switch if N operations are completed while
    739   1.1        pk 	 * another drive is waiting to be serviced, since there is a long motor
    740   1.1        pk 	 * startup delay whenever we switch.
    741   1.1        pk 	 */
    742   1.1        pk 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    743   1.1        pk 		fd->sc_ops = 0;
    744   1.1        pk 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    745   1.1        pk 		if (bp->b_actf) {
    746   1.1        pk 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    747   1.1        pk 		} else
    748   1.1        pk 			fd->sc_q.b_active = 0;
    749   1.1        pk 	}
    750   1.1        pk 	bp->b_resid = fd->sc_bcount;
    751   1.1        pk 	fd->sc_skip = 0;
    752   1.1        pk 	fd->sc_q.b_actf = bp->b_actf;
    753  1.16   thorpej 
    754   1.1        pk 	biodone(bp);
    755   1.1        pk 	/* turn off motor 5s from now */
    756   1.1        pk 	timeout(fd_motor_off, fd, 5 * hz);
    757   1.1        pk 	fdc->sc_state = DEVIDLE;
    758   1.1        pk }
    759   1.1        pk 
    760   1.1        pk void
    761  1.12        pk fdc_reset(fdc)
    762  1.12        pk 	struct fdc_softc *fdc;
    763  1.12        pk {
    764  1.12        pk 	if (fdc->sc_flags & FDC_82077) {
    765  1.50        pk 		*fdc->sc_reg_dor = FDO_FDMAEN | FDO_MOEN(0);
    766  1.12        pk 	}
    767  1.12        pk 
    768  1.12        pk 	*fdc->sc_reg_drs = DRS_RESET;
    769  1.12        pk 	delay(10);
    770  1.12        pk 	*fdc->sc_reg_drs = 0;
    771  1.50        pk 
    772  1.50        pk 	if (fdc->sc_flags & FDC_82077) {
    773  1.50        pk 		*fdc->sc_reg_dor = FDO_FRST | FDO_FDMAEN | FDO_DS;
    774  1.50        pk 	}
    775  1.12        pk #ifdef FD_DEBUG
    776  1.12        pk 	if (fdc_debug)
    777  1.39  christos 		printf("fdc reset\n");
    778  1.12        pk #endif
    779  1.12        pk }
    780  1.12        pk 
    781  1.12        pk void
    782  1.12        pk fd_set_motor(fdc)
    783   1.1        pk 	struct fdc_softc *fdc;
    784   1.1        pk {
    785   1.1        pk 	struct fd_softc *fd;
    786   1.1        pk 	u_char status;
    787   1.1        pk 	int n;
    788   1.1        pk 
    789   1.1        pk 	if (fdc->sc_flags & FDC_82077) {
    790  1.12        pk 		status = FDO_FRST | FDO_FDMAEN;
    791  1.24  christos 		if ((fd = fdc->sc_drives.tqh_first) != NULL)
    792  1.12        pk 			status |= fd->sc_drive;
    793  1.12        pk 
    794   1.1        pk 		for (n = 0; n < 4; n++)
    795   1.1        pk 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    796   1.1        pk 				status |= FDO_MOEN(n);
    797   1.1        pk 		*fdc->sc_reg_dor = status;
    798   1.1        pk 	} else {
    799   1.1        pk 		int on = 0;
    800   1.1        pk 
    801   1.1        pk 		for (n = 0; n < 4; n++)
    802   1.1        pk 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    803   1.1        pk 				on = 1;
    804   1.1        pk 		if (on) {
    805  1.49        pk 			auxregbisc(AUXIO4C_FDS, 0);
    806   1.1        pk 		} else {
    807  1.49        pk 			auxregbisc(0, AUXIO4C_FDS);
    808   1.1        pk 		}
    809   1.1        pk 	}
    810   1.1        pk }
    811   1.1        pk 
    812   1.1        pk void
    813   1.1        pk fd_motor_off(arg)
    814   1.1        pk 	void *arg;
    815   1.1        pk {
    816   1.1        pk 	struct fd_softc *fd = arg;
    817   1.1        pk 	int s;
    818   1.1        pk 
    819   1.1        pk 	s = splbio();
    820   1.1        pk 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    821  1.16   thorpej 	fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
    822   1.1        pk 	splx(s);
    823   1.1        pk }
    824   1.1        pk 
    825   1.1        pk void
    826   1.1        pk fd_motor_on(arg)
    827   1.1        pk 	void *arg;
    828   1.1        pk {
    829   1.1        pk 	struct fd_softc *fd = arg;
    830  1.16   thorpej 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
    831   1.1        pk 	int s;
    832   1.1        pk 
    833   1.1        pk 	s = splbio();
    834   1.1        pk 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    835   1.1        pk 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
    836  1.42        pk 		(void) fdcstate(fdc);
    837   1.1        pk 	splx(s);
    838   1.1        pk }
    839   1.1        pk 
    840   1.1        pk int
    841   1.1        pk fdcresult(fdc)
    842   1.1        pk 	struct fdc_softc *fdc;
    843   1.1        pk {
    844   1.1        pk 	u_char i;
    845   1.1        pk 	int j = 100000,
    846   1.1        pk 	    n = 0;
    847   1.1        pk 
    848   1.1        pk 	for (; j; j--) {
    849   1.1        pk 		i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
    850   1.1        pk 		if (i == NE7_RQM)
    851   1.1        pk 			return (fdc->sc_nstat = n);
    852   1.1        pk 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    853   1.1        pk 			if (n >= sizeof(fdc->sc_status)) {
    854   1.1        pk 				log(LOG_ERR, "fdcresult: overrun\n");
    855  1.42        pk 				return (-1);
    856   1.1        pk 			}
    857   1.2        pk 			fdc->sc_status[n++] = *fdc->sc_reg_fifo;
    858  1.41        pk 		} else
    859  1.41        pk 			delay(10);
    860   1.1        pk 	}
    861   1.1        pk 	log(LOG_ERR, "fdcresult: timeout\n");
    862   1.1        pk 	return (fdc->sc_nstat = -1);
    863   1.1        pk }
    864   1.1        pk 
    865   1.1        pk int
    866   1.1        pk out_fdc(fdc, x)
    867   1.1        pk 	struct fdc_softc *fdc;
    868   1.1        pk 	u_char x;
    869   1.1        pk {
    870   1.1        pk 	int i = 100000;
    871   1.1        pk 
    872  1.41        pk 	while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0)
    873  1.41        pk 		delay(1);
    874   1.1        pk 	if (i <= 0)
    875  1.42        pk 		return (-1);
    876   1.1        pk 
    877   1.2        pk 	*fdc->sc_reg_fifo = x;
    878  1.42        pk 	return (0);
    879   1.1        pk }
    880   1.1        pk 
    881   1.1        pk int
    882  1.24  christos fdopen(dev, flags, fmt, p)
    883   1.1        pk 	dev_t dev;
    884  1.19   thorpej 	int flags, fmt;
    885  1.19   thorpej 	struct proc *p;
    886   1.1        pk {
    887  1.19   thorpej  	int unit, pmask;
    888   1.1        pk 	struct fd_softc *fd;
    889   1.1        pk 	struct fd_type *type;
    890   1.1        pk 
    891   1.1        pk 	unit = FDUNIT(dev);
    892  1.26   thorpej 	if (unit >= fd_cd.cd_ndevs)
    893  1.42        pk 		return (ENXIO);
    894  1.26   thorpej 	fd = fd_cd.cd_devs[unit];
    895   1.1        pk 	if (fd == 0)
    896  1.42        pk 		return (ENXIO);
    897   1.1        pk 	type = fd_dev_to_type(fd, dev);
    898   1.1        pk 	if (type == NULL)
    899  1.42        pk 		return (ENXIO);
    900   1.1        pk 
    901   1.1        pk 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    902   1.1        pk 	    fd->sc_type != type)
    903  1.42        pk 		return (EBUSY);
    904   1.1        pk 
    905   1.1        pk 	fd->sc_type = type;
    906   1.1        pk 	fd->sc_cylin = -1;
    907   1.1        pk 	fd->sc_flags |= FD_OPEN;
    908   1.1        pk 
    909  1.19   thorpej 	/*
    910  1.19   thorpej 	 * Only update the disklabel if we're not open anywhere else.
    911  1.19   thorpej 	 */
    912  1.19   thorpej 	if (fd->sc_dk.dk_openmask == 0)
    913  1.19   thorpej 		fdgetdisklabel(dev);
    914  1.19   thorpej 
    915  1.19   thorpej 	pmask = (1 << DISKPART(dev));
    916  1.19   thorpej 
    917  1.19   thorpej 	switch (fmt) {
    918  1.19   thorpej 	case S_IFCHR:
    919  1.19   thorpej 		fd->sc_dk.dk_copenmask |= pmask;
    920  1.19   thorpej 		break;
    921  1.19   thorpej 
    922  1.19   thorpej 	case S_IFBLK:
    923  1.19   thorpej 		fd->sc_dk.dk_bopenmask |= pmask;
    924  1.19   thorpej 		break;
    925  1.19   thorpej 	}
    926  1.19   thorpej 	fd->sc_dk.dk_openmask =
    927  1.19   thorpej 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
    928  1.19   thorpej 
    929  1.42        pk 	return (0);
    930   1.1        pk }
    931   1.1        pk 
    932   1.1        pk int
    933  1.19   thorpej fdclose(dev, flags, fmt, p)
    934   1.1        pk 	dev_t dev;
    935  1.19   thorpej 	int flags, fmt;
    936  1.19   thorpej 	struct proc *p;
    937   1.1        pk {
    938  1.26   thorpej 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
    939  1.19   thorpej 	int pmask = (1 << DISKPART(dev));
    940   1.1        pk 
    941   1.1        pk 	fd->sc_flags &= ~FD_OPEN;
    942  1.42        pk 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
    943  1.19   thorpej 
    944  1.19   thorpej 	switch (fmt) {
    945  1.19   thorpej 	case S_IFCHR:
    946  1.19   thorpej 		fd->sc_dk.dk_copenmask &= ~pmask;
    947  1.19   thorpej 		break;
    948  1.19   thorpej 
    949  1.19   thorpej 	case S_IFBLK:
    950  1.19   thorpej 		fd->sc_dk.dk_bopenmask &= ~pmask;
    951  1.19   thorpej 		break;
    952  1.19   thorpej 	}
    953  1.29        pk 	fd->sc_dk.dk_openmask =
    954  1.19   thorpej 	    fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
    955  1.19   thorpej 
    956  1.42        pk 	return (0);
    957   1.9        pk }
    958   1.9        pk 
    959   1.9        pk int
    960  1.24  christos fdread(dev, uio, flag)
    961   1.9        pk         dev_t dev;
    962   1.9        pk         struct uio *uio;
    963  1.24  christos 	int flag;
    964   1.9        pk {
    965   1.9        pk 
    966   1.9        pk         return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    967   1.9        pk }
    968   1.9        pk 
    969   1.9        pk int
    970  1.24  christos fdwrite(dev, uio, flag)
    971   1.9        pk         dev_t dev;
    972   1.9        pk         struct uio *uio;
    973  1.24  christos 	int flag;
    974   1.9        pk {
    975   1.9        pk 
    976   1.9        pk         return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    977   1.1        pk }
    978   1.1        pk 
    979   1.1        pk void
    980   1.1        pk fdcstart(fdc)
    981   1.1        pk 	struct fdc_softc *fdc;
    982   1.1        pk {
    983   1.1        pk 
    984   1.1        pk #ifdef DIAGNOSTIC
    985   1.1        pk 	/* only got here if controller's drive queue was inactive; should
    986   1.1        pk 	   be in idle state */
    987   1.1        pk 	if (fdc->sc_state != DEVIDLE) {
    988  1.39  christos 		printf("fdcstart: not idle\n");
    989   1.1        pk 		return;
    990   1.1        pk 	}
    991   1.1        pk #endif
    992  1.42        pk 	(void) fdcstate(fdc);
    993   1.1        pk }
    994   1.1        pk 
    995   1.1        pk void
    996   1.1        pk fdcstatus(dv, n, s)
    997   1.1        pk 	struct device *dv;
    998   1.1        pk 	int n;
    999   1.1        pk 	char *s;
   1000   1.1        pk {
   1001   1.1        pk 	struct fdc_softc *fdc = (void *)dv->dv_parent;
   1002  1.40   thorpej 	char bits[64];
   1003   1.1        pk #if 0
   1004   1.1        pk 	/*
   1005   1.1        pk 	 * A 82072 seems to return <invalid command> on
   1006   1.1        pk 	 * gratuitous Sense Interrupt commands.
   1007   1.1        pk 	 */
   1008   1.1        pk 	if (n == 0 && (fdc->sc_flags & FDC_82077)) {
   1009   1.1        pk 		out_fdc(fdc, NE7CMD_SENSEI);
   1010   1.1        pk 		(void) fdcresult(fdc);
   1011   1.1        pk 		n = 2;
   1012   1.1        pk 	}
   1013   1.1        pk #endif
   1014   1.1        pk 
   1015   1.1        pk 	/* Just print last status */
   1016   1.1        pk 	n = fdc->sc_nstat;
   1017   1.1        pk 
   1018  1.39  christos 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
   1019   1.1        pk 
   1020   1.1        pk 	switch (n) {
   1021   1.1        pk 	case 0:
   1022  1.39  christos 		printf("\n");
   1023   1.1        pk 		break;
   1024   1.1        pk 	case 2:
   1025  1.40   thorpej 		printf(" (st0 %s cyl %d)\n",
   1026  1.40   thorpej 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
   1027  1.40   thorpej 		    bits, sizeof(bits)), fdc->sc_status[1]);
   1028   1.1        pk 		break;
   1029   1.1        pk 	case 7:
   1030  1.40   thorpej 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
   1031  1.40   thorpej 		    NE7_ST0BITS, bits, sizeof(bits)));
   1032  1.40   thorpej 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
   1033  1.40   thorpej 		    NE7_ST1BITS, bits, sizeof(bits)));
   1034  1.40   thorpej 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
   1035  1.40   thorpej 		    NE7_ST2BITS, bits, sizeof(bits)));
   1036  1.40   thorpej 		printf(" cyl %d head %d sec %d)\n",
   1037   1.1        pk 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1038   1.1        pk 		break;
   1039   1.1        pk #ifdef DIAGNOSTIC
   1040   1.1        pk 	default:
   1041  1.39  christos 		printf(" fdcstatus: weird size: %d\n", n);
   1042   1.1        pk 		break;
   1043   1.1        pk #endif
   1044   1.1        pk 	}
   1045   1.1        pk }
   1046   1.1        pk 
   1047   1.1        pk void
   1048   1.1        pk fdctimeout(arg)
   1049   1.1        pk 	void *arg;
   1050   1.1        pk {
   1051   1.1        pk 	struct fdc_softc *fdc = arg;
   1052   1.1        pk 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
   1053   1.1        pk 	int s;
   1054   1.1        pk 
   1055   1.1        pk 	s = splbio();
   1056  1.16   thorpej 	fdcstatus(&fd->sc_dv, 0, "timeout");
   1057   1.1        pk 
   1058   1.1        pk 	if (fd->sc_q.b_actf)
   1059   1.1        pk 		fdc->sc_state++;
   1060   1.1        pk 	else
   1061   1.1        pk 		fdc->sc_state = DEVIDLE;
   1062   1.1        pk 
   1063  1.42        pk 	(void) fdcstate(fdc);
   1064   1.1        pk 	splx(s);
   1065   1.1        pk }
   1066   1.1        pk 
   1067   1.2        pk void
   1068   1.2        pk fdcpseudointr(arg)
   1069   1.2        pk 	void *arg;
   1070   1.2        pk {
   1071   1.2        pk 	struct fdc_softc *fdc = arg;
   1072   1.2        pk 	int s;
   1073   1.2        pk 
   1074   1.2        pk 	/* Just ensure it has the right spl. */
   1075   1.2        pk 	s = splbio();
   1076  1.42        pk 	(void) fdcstate(fdc);
   1077   1.2        pk 	splx(s);
   1078   1.2        pk }
   1079   1.2        pk 
   1080   1.2        pk 
   1081   1.2        pk #ifdef FDC_C_HANDLER
   1082   1.1        pk /*
   1083   1.1        pk  * hardware interrupt entry point: must be converted to `fast'
   1084   1.1        pk  * (in-window) handler.
   1085   1.1        pk  */
   1086   1.1        pk int
   1087   1.1        pk fdchwintr(fdc)
   1088   1.1        pk 	struct fdc_softc *fdc;
   1089   1.1        pk {
   1090   1.1        pk 
   1091   1.2        pk 	switch (fdc->sc_istate) {
   1092  1.42        pk 	case ISTATE_IDLE:
   1093  1.42        pk 		return (0);
   1094   1.2        pk 	case ISTATE_SENSEI:
   1095   1.1        pk 		out_fdc(fdc, NE7CMD_SENSEI);
   1096   1.1        pk 		fdcresult(fdc);
   1097   1.2        pk 		fdc->sc_istate = ISTATE_IDLE;
   1098  1.42        pk 		FD_SET_SWINTR;
   1099  1.42        pk 		return (1);
   1100   1.2        pk 	case ISTATE_SPURIOUS:
   1101   1.1        pk 		fdcresult(fdc);
   1102   1.2        pk 		fdc->sc_istate = ISTATE_SPURIOUS;
   1103  1.39  christos 		printf("fdc: stray hard interrupt... ");
   1104  1.42        pk 		FD_SET_SWINTR;
   1105  1.42        pk 		return (1);
   1106   1.2        pk 	case ISTATE_DMA:
   1107   1.2        pk 		break;
   1108   1.2        pk 	default:
   1109  1.39  christos 		printf("fdc: goofed ...\n");
   1110  1.42        pk 		return (1);
   1111   1.1        pk 	}
   1112   1.1        pk 
   1113   1.1        pk 	for (;;) {
   1114   1.1        pk 		register int msr;
   1115   1.1        pk 
   1116   1.1        pk 		msr = *fdc->sc_reg_msr;
   1117   1.1        pk 
   1118   1.1        pk 		if ((msr & NE7_RQM) == 0)
   1119   1.1        pk 			break;
   1120   1.1        pk 
   1121   1.1        pk 		if ((msr & NE7_NDM) == 0) {
   1122   1.1        pk 			fdcresult(fdc);
   1123   1.2        pk 			fdc->sc_istate = ISTATE_IDLE;
   1124   1.1        pk 			ienab_bis(IE_FDSOFT);
   1125  1.39  christos 			printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
   1126   1.1        pk 			break;
   1127   1.1        pk 		}
   1128   1.1        pk 
   1129   1.1        pk 		if (msr & NE7_DIO) {
   1130   1.2        pk 			*fdc->sc_data++ = *fdc->sc_reg_fifo;
   1131   1.1        pk 		} else {
   1132   1.2        pk 			*fdc->sc_reg_fifo = *fdc->sc_data++;
   1133   1.1        pk 		}
   1134   1.1        pk 		if (--fdc->sc_tc == 0) {
   1135  1.50        pk 			fdc->sc_istate = ISTATE_DONE;
   1136  1.49        pk 			FTC_FLIP;
   1137   1.1        pk 			fdcresult(fdc);
   1138  1.42        pk 			FD_SET_SWINTR;
   1139   1.1        pk 			break;
   1140   1.1        pk 		}
   1141   1.1        pk 	}
   1142  1.42        pk 	return (1);
   1143   1.1        pk }
   1144   1.2        pk #endif
   1145   1.1        pk 
   1146   1.1        pk int
   1147   1.1        pk fdcswintr(fdc)
   1148   1.1        pk 	struct fdc_softc *fdc;
   1149   1.1        pk {
   1150  1.42        pk 	int s;
   1151  1.42        pk 
   1152  1.42        pk 	if (fdc->sc_istate != ISTATE_DONE)
   1153  1.42        pk 		return (0);
   1154  1.42        pk 
   1155  1.42        pk 	fdc->sc_istate = ISTATE_IDLE;
   1156  1.42        pk 	s = splbio();
   1157  1.42        pk 	fdcstate(fdc);
   1158  1.42        pk 	splx(s);
   1159  1.42        pk 	return (1);
   1160  1.42        pk }
   1161  1.42        pk 
   1162  1.42        pk int
   1163  1.42        pk fdcstate(fdc)
   1164  1.42        pk 	struct fdc_softc *fdc;
   1165  1.42        pk {
   1166   1.1        pk #define	st0	fdc->sc_status[0]
   1167   1.1        pk #define	st1	fdc->sc_status[1]
   1168   1.1        pk #define	cyl	fdc->sc_status[1]
   1169   1.2        pk #define OUT_FDC(fdc, c, s) \
   1170   1.2        pk     do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
   1171   1.2        pk 
   1172   1.1        pk 	struct fd_softc *fd;
   1173   1.1        pk 	struct buf *bp;
   1174  1.24  christos 	int read, head, sec, nblks;
   1175   1.1        pk 	struct fd_type *type;
   1176  1.43        pk 	struct ne7_fd_formb *finfo = NULL;
   1177   1.1        pk 
   1178  1.12        pk 
   1179   1.2        pk 	if (fdc->sc_istate != ISTATE_IDLE) {
   1180   1.2        pk 		/* Trouble... */
   1181  1.39  christos 		printf("fdc: spurious interrupt: state %d, istate=%d\n",
   1182  1.12        pk 			fdc->sc_state, fdc->sc_istate);
   1183   1.2        pk 		fdc->sc_istate = ISTATE_IDLE;
   1184  1.12        pk 		if (fdc->sc_state == RESETCOMPLETE ||
   1185  1.12        pk 		    fdc->sc_state == RESETTIMEDOUT) {
   1186  1.12        pk 			panic("fdcintr: spurious interrupt can't be cleared");
   1187  1.12        pk 		}
   1188   1.2        pk 		goto doreset;
   1189   1.2        pk 	}
   1190   1.2        pk 
   1191  1.12        pk loop:
   1192   1.1        pk 	/* Is there a drive for the controller to do a transfer with? */
   1193   1.1        pk 	fd = fdc->sc_drives.tqh_first;
   1194   1.1        pk 	if (fd == NULL) {
   1195   1.1        pk 		fdc->sc_state = DEVIDLE;
   1196  1.42        pk  		return (0);
   1197   1.1        pk 	}
   1198   1.1        pk 
   1199   1.1        pk 	/* Is there a transfer to this drive?  If not, deactivate drive. */
   1200   1.1        pk 	bp = fd->sc_q.b_actf;
   1201   1.1        pk 	if (bp == NULL) {
   1202   1.1        pk 		fd->sc_ops = 0;
   1203   1.1        pk 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1204   1.1        pk 		fd->sc_q.b_active = 0;
   1205   1.1        pk 		goto loop;
   1206   1.1        pk 	}
   1207   1.1        pk 
   1208  1.43        pk 	if (bp->b_flags & B_FORMAT)
   1209  1.43        pk 		finfo = (struct ne7_fd_formb *)bp->b_data;
   1210  1.43        pk 
   1211   1.1        pk 	switch (fdc->sc_state) {
   1212   1.1        pk 	case DEVIDLE:
   1213   1.1        pk 		fdc->sc_errors = 0;
   1214   1.1        pk 		fd->sc_skip = 0;
   1215   1.1        pk 		fd->sc_bcount = bp->b_bcount;
   1216  1.53        pk 		fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
   1217   1.1        pk 		untimeout(fd_motor_off, fd);
   1218   1.1        pk 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1219   1.1        pk 			fdc->sc_state = MOTORWAIT;
   1220  1.42        pk 			return (1);
   1221   1.1        pk 		}
   1222   1.1        pk 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1223   1.1        pk 			/* Turn on the motor, being careful about pairing. */
   1224   1.1        pk 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1225   1.1        pk 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1226   1.1        pk 				untimeout(fd_motor_off, ofd);
   1227   1.1        pk 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1228   1.1        pk 			}
   1229   1.1        pk 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1230  1.12        pk 			fd_set_motor(fdc);
   1231   1.1        pk 			fdc->sc_state = MOTORWAIT;
   1232   1.2        pk 			if (fdc->sc_flags & FDC_82077) { /* XXX */
   1233   1.2        pk 				/* Allow .25s for motor to stabilize. */
   1234   1.2        pk 				timeout(fd_motor_on, fd, hz / 4);
   1235   1.2        pk 			} else {
   1236   1.2        pk 				fd->sc_flags &= ~FD_MOTOR_WAIT;
   1237   1.2        pk 				goto loop;
   1238   1.2        pk 			}
   1239  1.42        pk 			return (1);
   1240   1.1        pk 		}
   1241   1.1        pk 		/* Make sure the right drive is selected. */
   1242  1.12        pk 		fd_set_motor(fdc);
   1243   1.1        pk 
   1244  1.49        pk 		/*FALLTHROUGH*/
   1245   1.1        pk 	case DOSEEK:
   1246   1.1        pk 	doseek:
   1247  1.43        pk 		if ((fdc->sc_flags & FDC_EIS) &&
   1248  1.43        pk 		    (bp->b_flags & B_FORMAT) == 0) {
   1249   1.2        pk 			fd->sc_cylin = bp->b_cylin;
   1250   1.2        pk 			/* We use implied seek */
   1251   1.2        pk 			goto doio;
   1252   1.2        pk 		}
   1253   1.2        pk 
   1254   1.1        pk 		if (fd->sc_cylin == bp->b_cylin)
   1255   1.1        pk 			goto doio;
   1256   1.1        pk 
   1257   1.2        pk 		/* specify command */
   1258   1.2        pk 		OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
   1259   1.2        pk 		OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
   1260  1.50        pk 		/* XXX head load time == 6ms */
   1261  1.50        pk 		OUT_FDC(fdc, 6 | NE7_SPECIFY_NODMA, SEEKTIMEDOUT);
   1262   1.2        pk 
   1263   1.2        pk 		fdc->sc_istate = ISTATE_SENSEI;
   1264   1.2        pk 		/* seek function */
   1265   1.2        pk 		OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
   1266   1.2        pk 		OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
   1267   1.2        pk 		OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT);
   1268   1.1        pk 
   1269   1.1        pk 		fd->sc_cylin = -1;
   1270   1.1        pk 		fdc->sc_state = SEEKWAIT;
   1271   1.1        pk 		fdc->sc_nstat = 0;
   1272  1.18   thorpej 
   1273  1.18   thorpej 		fd->sc_dk.dk_seek++;
   1274  1.18   thorpej 		disk_busy(&fd->sc_dk);
   1275  1.18   thorpej 
   1276   1.1        pk 		timeout(fdctimeout, fdc, 4 * hz);
   1277  1.42        pk 		return (1);
   1278   1.1        pk 
   1279   1.1        pk 	case DOIO:
   1280   1.1        pk 	doio:
   1281  1.50        pk 		if (finfo != NULL)
   1282  1.43        pk 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
   1283  1.43        pk 				      (char *)finfo;
   1284   1.1        pk 		type = fd->sc_type;
   1285   1.1        pk 		sec = fd->sc_blkno % type->seccyl;
   1286   1.1        pk 		nblks = type->seccyl - sec;
   1287  1.53        pk 		nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
   1288  1.53        pk 		nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
   1289   1.1        pk 		fd->sc_nblks = nblks;
   1290  1.53        pk 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
   1291   1.1        pk 		head = sec / type->sectrac;
   1292   1.1        pk 		sec -= head * type->sectrac;
   1293   1.1        pk #ifdef DIAGNOSTIC
   1294   1.1        pk 		{int block;
   1295   1.1        pk 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
   1296   1.1        pk 		 if (block != fd->sc_blkno) {
   1297  1.39  christos 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
   1298   1.1        pk #ifdef DDB
   1299   1.1        pk 			 Debugger();
   1300   1.1        pk #endif
   1301   1.1        pk 		 }}
   1302   1.1        pk #endif
   1303   1.1        pk 		read = bp->b_flags & B_READ;
   1304   1.1        pk 
   1305   1.1        pk 		/* Setup for pseudo DMA */
   1306   1.2        pk 		fdc->sc_data = bp->b_data + fd->sc_skip;
   1307   1.1        pk 		fdc->sc_tc = fd->sc_nbytes;
   1308   1.1        pk 
   1309   1.1        pk 		*fdc->sc_reg_drs = type->rate;
   1310   1.1        pk #ifdef FD_DEBUG
   1311   1.2        pk 		if (fdc_debug > 1)
   1312  1.39  christos 			printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
   1313   1.2        pk 				read ? "read" : "write", fd->sc_drive,
   1314   1.2        pk 				fd->sc_cylin, head, sec, nblks);
   1315   1.1        pk #endif
   1316   1.2        pk 		fdc->sc_state = IOCOMPLETE;
   1317   1.2        pk 		fdc->sc_istate = ISTATE_DMA;
   1318   1.1        pk 		fdc->sc_nstat = 0;
   1319  1.50        pk 		if (finfo != NULL) {
   1320  1.43        pk 			/* formatting */
   1321  1.43        pk 			OUT_FDC(fdc, NE7CMD_FORMAT, IOTIMEDOUT);
   1322  1.43        pk 			OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
   1323  1.43        pk 			OUT_FDC(fdc, finfo->fd_formb_secshift, IOTIMEDOUT);
   1324  1.43        pk 			OUT_FDC(fdc, finfo->fd_formb_nsecs, IOTIMEDOUT);
   1325  1.43        pk 			OUT_FDC(fdc, finfo->fd_formb_gaplen, IOTIMEDOUT);
   1326  1.43        pk 			OUT_FDC(fdc, finfo->fd_formb_fillbyte, IOTIMEDOUT);
   1327  1.43        pk 		} else {
   1328  1.43        pk 			if (read)
   1329  1.43        pk 				OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT);
   1330  1.43        pk 			else
   1331  1.43        pk 				OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT);
   1332  1.43        pk 			OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
   1333  1.43        pk 			OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT);	/*track*/
   1334  1.43        pk 			OUT_FDC(fdc, head, IOTIMEDOUT);
   1335  1.43        pk 			OUT_FDC(fdc, sec + 1, IOTIMEDOUT);	/*sector+1*/
   1336  1.43        pk 			OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/*sector size*/
   1337  1.43        pk 			OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/*secs/track*/
   1338  1.43        pk 			OUT_FDC(fdc, type->gap1, IOTIMEDOUT);	/*gap1 size*/
   1339  1.43        pk 			OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/*data length*/
   1340  1.43        pk 		}
   1341  1.18   thorpej 
   1342  1.18   thorpej 		disk_busy(&fd->sc_dk);
   1343  1.18   thorpej 
   1344   1.1        pk 		/* allow 2 seconds for operation */
   1345   1.1        pk 		timeout(fdctimeout, fdc, 2 * hz);
   1346  1.42        pk 		return (1);				/* will return later */
   1347   1.1        pk 
   1348   1.1        pk 	case SEEKWAIT:
   1349   1.1        pk 		untimeout(fdctimeout, fdc);
   1350   1.1        pk 		fdc->sc_state = SEEKCOMPLETE;
   1351   1.2        pk 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1352   1.2        pk 			/* allow 1/50 second for heads to settle */
   1353   1.2        pk 			timeout(fdcpseudointr, fdc, hz / 50);
   1354  1.42        pk 			return (1);		/* will return later */
   1355   1.2        pk 		}
   1356  1.49        pk 		/*FALLTHROUGH*/
   1357   1.1        pk 	case SEEKCOMPLETE:
   1358  1.18   thorpej 		disk_unbusy(&fd->sc_dk, 0);	/* no data on seek */
   1359  1.18   thorpej 
   1360   1.1        pk 		/* Make sure seek really happened. */
   1361   1.1        pk 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1362   1.1        pk 		    cyl != bp->b_cylin * fd->sc_type->step) {
   1363   1.1        pk #ifdef FD_DEBUG
   1364   1.2        pk 			if (fdc_debug)
   1365  1.16   thorpej 				fdcstatus(&fd->sc_dv, 2, "seek failed");
   1366   1.1        pk #endif
   1367   1.1        pk 			fdcretry(fdc);
   1368   1.1        pk 			goto loop;
   1369   1.1        pk 		}
   1370   1.1        pk 		fd->sc_cylin = bp->b_cylin;
   1371   1.1        pk 		goto doio;
   1372   1.1        pk 
   1373   1.1        pk 	case IOTIMEDOUT:
   1374  1.49        pk 		FTC_FLIP;
   1375   1.1        pk 		(void)fdcresult(fdc);
   1376  1.49        pk 		/*FALLTHROUGH*/
   1377   1.1        pk 	case SEEKTIMEDOUT:
   1378   1.1        pk 	case RECALTIMEDOUT:
   1379   1.1        pk 	case RESETTIMEDOUT:
   1380   1.1        pk 		fdcretry(fdc);
   1381   1.1        pk 		goto loop;
   1382   1.1        pk 
   1383   1.1        pk 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1384   1.1        pk 		untimeout(fdctimeout, fdc);
   1385  1.18   thorpej 
   1386  1.18   thorpej 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
   1387  1.18   thorpej 
   1388  1.50        pk 		if (fdc->sc_nstat != 7 || st1 != 0 ||
   1389  1.50        pk 		    ((st0 & 0xf8) != 0 &&
   1390  1.50        pk 		     ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
   1391   1.1        pk #ifdef FD_DEBUG
   1392   1.2        pk 			if (fdc_debug) {
   1393  1.16   thorpej 				fdcstatus(&fd->sc_dv, 7,
   1394   1.2        pk 					bp->b_flags & B_READ
   1395   1.2        pk 					? "read failed" : "write failed");
   1396  1.50        pk 				printf("blkno %d nblks %d nstat %d tc %d\n",
   1397  1.50        pk 				       fd->sc_blkno, fd->sc_nblks,
   1398  1.50        pk 				       fdc->sc_nstat, fdc->sc_tc);
   1399   1.2        pk 			}
   1400   1.1        pk #endif
   1401   1.6        pk 			if (fdc->sc_nstat == 7 &&
   1402   1.6        pk 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
   1403   1.6        pk 
   1404   1.6        pk 				/*
   1405   1.6        pk 				 * Silently retry overruns if no other
   1406   1.6        pk 				 * error bit is set. Adjust threshold.
   1407   1.6        pk 				 */
   1408   1.2        pk 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1409   1.6        pk 				if (thr < 15) {
   1410   1.6        pk 					thr++;
   1411   1.6        pk 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1412   1.6        pk 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1413   1.2        pk #ifdef FD_DEBUG
   1414   1.6        pk 					if (fdc_debug)
   1415  1.39  christos 						printf("fdc: %d -> threshold\n", thr);
   1416   1.2        pk #endif
   1417   1.6        pk 					fdconf(fdc);
   1418   1.6        pk 					fdc->sc_overruns = 0;
   1419   1.6        pk 				}
   1420  1.34        pk 				if (++fdc->sc_overruns < 3) {
   1421  1.34        pk 					fdc->sc_state = DOIO;
   1422   1.6        pk 					goto loop;
   1423  1.34        pk 				}
   1424   1.2        pk 			}
   1425   1.1        pk 			fdcretry(fdc);
   1426   1.1        pk 			goto loop;
   1427   1.1        pk 		}
   1428   1.1        pk 		if (fdc->sc_errors) {
   1429   1.1        pk 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1430  1.53        pk 			    fd->sc_skip / FD_BSIZE(fd),
   1431  1.53        pk 			    (struct disklabel *)NULL);
   1432  1.39  christos 			printf("\n");
   1433   1.1        pk 			fdc->sc_errors = 0;
   1434   1.6        pk 		} else {
   1435  1.12        pk 			if (--fdc->sc_overruns < -20) {
   1436   1.6        pk 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1437   1.6        pk 				if (thr > 0) {
   1438   1.6        pk 					thr--;
   1439   1.6        pk 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1440   1.6        pk 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1441   1.6        pk #ifdef FD_DEBUG
   1442   1.6        pk 					if (fdc_debug)
   1443  1.39  christos 						printf("fdc: %d -> threshold\n", thr);
   1444   1.6        pk #endif
   1445   1.6        pk 					fdconf(fdc);
   1446   1.6        pk 				}
   1447   1.6        pk 				fdc->sc_overruns = 0;
   1448   1.6        pk 			}
   1449   1.1        pk 		}
   1450   1.1        pk 		fd->sc_blkno += fd->sc_nblks;
   1451   1.1        pk 		fd->sc_skip += fd->sc_nbytes;
   1452   1.1        pk 		fd->sc_bcount -= fd->sc_nbytes;
   1453  1.50        pk 		if (finfo == NULL && fd->sc_bcount > 0) {
   1454   1.1        pk 			bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
   1455   1.1        pk 			goto doseek;
   1456   1.1        pk 		}
   1457   1.1        pk 		fdfinish(fd, bp);
   1458   1.1        pk 		goto loop;
   1459   1.1        pk 
   1460   1.1        pk 	case DORESET:
   1461   1.2        pk 	doreset:
   1462   1.1        pk 		/* try a reset, keep motor on */
   1463  1.12        pk 		fd_set_motor(fdc);
   1464   1.1        pk 		delay(100);
   1465  1.12        pk 		fdc_reset(fdc);
   1466  1.12        pk 		fdc->sc_nstat = 0;
   1467  1.12        pk 		fdc->sc_istate = ISTATE_SENSEI;
   1468   1.1        pk 		fdc->sc_state = RESETCOMPLETE;
   1469   1.1        pk 		timeout(fdctimeout, fdc, hz / 2);
   1470  1.42        pk 		return (1);			/* will return later */
   1471   1.1        pk 
   1472   1.1        pk 	case RESETCOMPLETE:
   1473   1.1        pk 		untimeout(fdctimeout, fdc);
   1474  1.12        pk 		fdconf(fdc);
   1475   1.1        pk 
   1476   1.1        pk 		/* fall through */
   1477   1.1        pk 	case DORECAL:
   1478   1.1        pk 		fdc->sc_state = RECALWAIT;
   1479   1.2        pk 		fdc->sc_istate = ISTATE_SENSEI;
   1480   1.1        pk 		fdc->sc_nstat = 0;
   1481   1.2        pk 		/* recalibrate function */
   1482   1.2        pk 		OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
   1483   1.2        pk 		OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
   1484   1.1        pk 		timeout(fdctimeout, fdc, 5 * hz);
   1485  1.42        pk 		return (1);			/* will return later */
   1486   1.1        pk 
   1487   1.1        pk 	case RECALWAIT:
   1488   1.1        pk 		untimeout(fdctimeout, fdc);
   1489   1.1        pk 		fdc->sc_state = RECALCOMPLETE;
   1490   1.2        pk 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1491   1.2        pk 			/* allow 1/30 second for heads to settle */
   1492   1.2        pk 			timeout(fdcpseudointr, fdc, hz / 30);
   1493  1.42        pk 			return (1);		/* will return later */
   1494   1.2        pk 		}
   1495   1.1        pk 
   1496   1.1        pk 	case RECALCOMPLETE:
   1497   1.1        pk 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1498   1.1        pk #ifdef FD_DEBUG
   1499   1.2        pk 			if (fdc_debug)
   1500  1.16   thorpej 				fdcstatus(&fd->sc_dv, 2, "recalibrate failed");
   1501   1.1        pk #endif
   1502   1.1        pk 			fdcretry(fdc);
   1503   1.1        pk 			goto loop;
   1504   1.1        pk 		}
   1505   1.1        pk 		fd->sc_cylin = 0;
   1506   1.1        pk 		goto doseek;
   1507   1.1        pk 
   1508   1.1        pk 	case MOTORWAIT:
   1509   1.1        pk 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1510  1.42        pk 			return (1);		/* time's not up yet */
   1511   1.1        pk 		goto doseek;
   1512   1.1        pk 
   1513   1.1        pk 	default:
   1514  1.16   thorpej 		fdcstatus(&fd->sc_dv, 0, "stray interrupt");
   1515  1.42        pk 		return (1);
   1516   1.1        pk 	}
   1517   1.1        pk #ifdef DIAGNOSTIC
   1518   1.1        pk 	panic("fdcintr: impossible");
   1519   1.1        pk #endif
   1520   1.1        pk #undef	st0
   1521   1.1        pk #undef	st1
   1522   1.1        pk #undef	cyl
   1523   1.1        pk }
   1524   1.1        pk 
   1525   1.1        pk void
   1526   1.1        pk fdcretry(fdc)
   1527   1.1        pk 	struct fdc_softc *fdc;
   1528   1.1        pk {
   1529  1.40   thorpej 	char bits[64];
   1530   1.1        pk 	struct fd_softc *fd;
   1531   1.1        pk 	struct buf *bp;
   1532   1.1        pk 
   1533   1.1        pk 	fd = fdc->sc_drives.tqh_first;
   1534   1.1        pk 	bp = fd->sc_q.b_actf;
   1535   1.6        pk 
   1536   1.6        pk 	fdc->sc_overruns = 0;
   1537  1.42        pk 	if (fd->sc_opts & FDOPT_NORETRY)
   1538  1.42        pk 		goto fail;
   1539   1.1        pk 
   1540   1.1        pk 	switch (fdc->sc_errors) {
   1541   1.1        pk 	case 0:
   1542   1.1        pk 		/* try again */
   1543   1.2        pk 		fdc->sc_state =
   1544  1.22   thorpej 			(fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
   1545   1.1        pk 		break;
   1546   1.1        pk 
   1547   1.1        pk 	case 1: case 2: case 3:
   1548   1.1        pk 		/* didn't work; try recalibrating */
   1549   1.1        pk 		fdc->sc_state = DORECAL;
   1550   1.1        pk 		break;
   1551   1.1        pk 
   1552   1.1        pk 	case 4:
   1553   1.1        pk 		/* still no go; reset the bastard */
   1554   1.1        pk 		fdc->sc_state = DORESET;
   1555   1.1        pk 		break;
   1556   1.1        pk 
   1557   1.1        pk 	default:
   1558  1.42        pk 	fail:
   1559  1.42        pk 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
   1560  1.42        pk 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1561  1.53        pk 				fd->sc_skip / FD_BSIZE(fd),
   1562  1.42        pk 				(struct disklabel *)NULL);
   1563  1.42        pk 
   1564  1.42        pk 			printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
   1565  1.42        pk 				NE7_ST0BITS, bits, sizeof(bits)));
   1566  1.42        pk 			printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
   1567  1.42        pk 				NE7_ST1BITS, bits, sizeof(bits)));
   1568  1.42        pk 			printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
   1569  1.42        pk 				NE7_ST2BITS, bits, sizeof(bits)));
   1570  1.42        pk 			printf(" cyl %d head %d sec %d)\n",
   1571  1.42        pk 				fdc->sc_status[3], fdc->sc_status[4],
   1572  1.42        pk 				fdc->sc_status[5]);
   1573  1.42        pk 		}
   1574   1.1        pk 
   1575   1.1        pk 		bp->b_flags |= B_ERROR;
   1576   1.1        pk 		bp->b_error = EIO;
   1577   1.1        pk 		fdfinish(fd, bp);
   1578   1.1        pk 	}
   1579   1.1        pk 	fdc->sc_errors++;
   1580   1.1        pk }
   1581   1.1        pk 
   1582   1.1        pk int
   1583   1.1        pk fdsize(dev)
   1584   1.1        pk 	dev_t dev;
   1585   1.1        pk {
   1586   1.1        pk 
   1587   1.1        pk 	/* Swapping to floppies would not make sense. */
   1588  1.42        pk 	return (-1);
   1589   1.1        pk }
   1590   1.1        pk 
   1591   1.1        pk int
   1592  1.24  christos fddump(dev, blkno, va, size)
   1593  1.24  christos 	dev_t dev;
   1594  1.24  christos 	daddr_t blkno;
   1595  1.24  christos 	caddr_t va;
   1596  1.24  christos 	size_t size;
   1597   1.1        pk {
   1598   1.1        pk 
   1599   1.1        pk 	/* Not implemented. */
   1600  1.42        pk 	return (EINVAL);
   1601   1.1        pk }
   1602   1.1        pk 
   1603   1.1        pk int
   1604  1.24  christos fdioctl(dev, cmd, addr, flag, p)
   1605   1.1        pk 	dev_t dev;
   1606   1.1        pk 	u_long cmd;
   1607   1.1        pk 	caddr_t addr;
   1608   1.1        pk 	int flag;
   1609  1.24  christos 	struct proc *p;
   1610   1.1        pk {
   1611  1.26   thorpej 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   1612  1.42        pk 	struct fdformat_parms *form_parms;
   1613  1.42        pk 	struct fdformat_cmd *form_cmd;
   1614  1.42        pk 	struct ne7_fd_formb fd_formb;
   1615  1.42        pk 	int il[FD_MAX_NSEC + 1];
   1616  1.42        pk 	int i, j;
   1617   1.1        pk 	int error;
   1618   1.1        pk 
   1619   1.1        pk 	switch (cmd) {
   1620   1.1        pk 	case DIOCGDINFO:
   1621  1.16   thorpej 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
   1622   1.1        pk 		return 0;
   1623   1.1        pk 
   1624   1.1        pk 	case DIOCWLABEL:
   1625   1.1        pk 		if ((flag & FWRITE) == 0)
   1626   1.1        pk 			return EBADF;
   1627   1.1        pk 		/* XXX do something */
   1628  1.42        pk 		return (0);
   1629   1.1        pk 
   1630   1.1        pk 	case DIOCWDINFO:
   1631   1.1        pk 		if ((flag & FWRITE) == 0)
   1632  1.42        pk 			return (EBADF);
   1633   1.1        pk 
   1634  1.16   thorpej 		error = setdisklabel(fd->sc_dk.dk_label,
   1635  1.11        pk 				    (struct disklabel *)addr, 0,
   1636  1.16   thorpej 				    fd->sc_dk.dk_cpulabel);
   1637   1.1        pk 		if (error)
   1638  1.42        pk 			return (error);
   1639   1.1        pk 
   1640  1.11        pk 		error = writedisklabel(dev, fdstrategy,
   1641  1.16   thorpej 				       fd->sc_dk.dk_label,
   1642  1.16   thorpej 				       fd->sc_dk.dk_cpulabel);
   1643  1.42        pk 		return (error);
   1644  1.21   thorpej 
   1645  1.21   thorpej 	case DIOCLOCK:
   1646  1.21   thorpej 		/*
   1647  1.21   thorpej 		 * Nothing to do here, really.
   1648  1.21   thorpej 		 */
   1649  1.42        pk 		return (0);
   1650   1.1        pk 
   1651  1.13        pk 	case DIOCEJECT:
   1652  1.50        pk 		fd_do_eject(fd);
   1653  1.42        pk 		return (0);
   1654  1.42        pk 
   1655  1.42        pk 	case FDIOCGETFORMAT:
   1656  1.42        pk 		form_parms = (struct fdformat_parms *)addr;
   1657  1.42        pk 		form_parms->fdformat_version = FDFORMAT_VERSION;
   1658  1.42        pk 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
   1659  1.49        pk 		form_parms->ncyl = fd->sc_type->cylinders;
   1660  1.42        pk 		form_parms->nspt = fd->sc_type->sectrac;
   1661  1.42        pk 		form_parms->ntrk = fd->sc_type->heads;
   1662  1.42        pk 		form_parms->stepspercyl = fd->sc_type->step;
   1663  1.42        pk 		form_parms->gaplen = fd->sc_type->gap2;
   1664  1.42        pk 		form_parms->fillbyte = fd->sc_type->fillbyte;
   1665  1.42        pk 		form_parms->interleave = fd->sc_type->interleave;
   1666  1.42        pk 		switch (fd->sc_type->rate) {
   1667  1.42        pk 		case FDC_500KBPS:
   1668  1.42        pk 			form_parms->xfer_rate = 500 * 1024;
   1669  1.42        pk 			break;
   1670  1.42        pk 		case FDC_300KBPS:
   1671  1.42        pk 			form_parms->xfer_rate = 300 * 1024;
   1672  1.42        pk 			break;
   1673  1.42        pk 		case FDC_250KBPS:
   1674  1.42        pk 			form_parms->xfer_rate = 250 * 1024;
   1675  1.42        pk 			break;
   1676  1.42        pk 		default:
   1677  1.42        pk 			return (EINVAL);
   1678  1.42        pk 		}
   1679  1.43        pk 		return (0);
   1680  1.42        pk 
   1681  1.42        pk 	case FDIOCSETFORMAT:
   1682  1.42        pk 		if ((flag & FWRITE) == 0)
   1683  1.42        pk 			return (EBADF);	/* must be opened for writing */
   1684  1.42        pk 
   1685  1.42        pk 		form_parms = (struct fdformat_parms *)addr;
   1686  1.42        pk 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
   1687  1.42        pk 			return (EINVAL);/* wrong version of formatting prog */
   1688  1.42        pk 
   1689  1.42        pk 		i = form_parms->nbps >> 7;
   1690  1.42        pk 		if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
   1691  1.42        pk 		    i & ~(1 << (ffs(i)-1)))
   1692  1.42        pk 			/* not a power-of-two multiple of 128 */
   1693  1.42        pk 			return (EINVAL);
   1694  1.42        pk 
   1695  1.42        pk 		switch (form_parms->xfer_rate) {
   1696  1.42        pk 		case 500 * 1024:
   1697  1.42        pk 			fd->sc_type->rate = FDC_500KBPS;
   1698  1.42        pk 			break;
   1699  1.42        pk 		case 300 * 1024:
   1700  1.42        pk 			fd->sc_type->rate = FDC_300KBPS;
   1701  1.42        pk 			break;
   1702  1.42        pk 		case 250 * 1024:
   1703  1.42        pk 			fd->sc_type->rate = FDC_250KBPS;
   1704  1.42        pk 			break;
   1705  1.42        pk 		default:
   1706  1.42        pk 			return (EINVAL);
   1707  1.42        pk 		}
   1708  1.42        pk 
   1709  1.42        pk 		if (form_parms->nspt > FD_MAX_NSEC ||
   1710  1.42        pk 		    form_parms->fillbyte > 0xff ||
   1711  1.42        pk 		    form_parms->interleave > 0xff)
   1712  1.42        pk 			return EINVAL;
   1713  1.42        pk 		fd->sc_type->sectrac = form_parms->nspt;
   1714  1.42        pk 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
   1715  1.42        pk 			return EINVAL;
   1716  1.42        pk 		fd->sc_type->heads = form_parms->ntrk;
   1717  1.42        pk 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
   1718  1.42        pk 		fd->sc_type->secsize = ffs(i)-1;
   1719  1.42        pk 		fd->sc_type->gap2 = form_parms->gaplen;
   1720  1.49        pk 		fd->sc_type->cylinders = form_parms->ncyl;
   1721  1.42        pk 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
   1722  1.42        pk 			form_parms->nbps / DEV_BSIZE;
   1723  1.42        pk 		fd->sc_type->step = form_parms->stepspercyl;
   1724  1.42        pk 		fd->sc_type->fillbyte = form_parms->fillbyte;
   1725  1.42        pk 		fd->sc_type->interleave = form_parms->interleave;
   1726  1.49        pk 		return (0);
   1727  1.16   thorpej 
   1728  1.42        pk 	case FDIOCFORMAT_TRACK:
   1729  1.42        pk 		if((flag & FWRITE) == 0)
   1730  1.42        pk 			/* must be opened for writing */
   1731  1.42        pk 			return (EBADF);
   1732  1.42        pk 		form_cmd = (struct fdformat_cmd *)addr;
   1733  1.42        pk 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
   1734  1.42        pk 			/* wrong version of formatting prog */
   1735  1.42        pk 			return (EINVAL);
   1736  1.42        pk 
   1737  1.42        pk 		if (form_cmd->head >= fd->sc_type->heads ||
   1738  1.49        pk 		    form_cmd->cylinder >= fd->sc_type->cylinders) {
   1739  1.42        pk 			return (EINVAL);
   1740  1.42        pk 		}
   1741  1.42        pk 
   1742  1.42        pk 		fd_formb.head = form_cmd->head;
   1743  1.42        pk 		fd_formb.cyl = form_cmd->cylinder;
   1744  1.42        pk 		fd_formb.transfer_rate = fd->sc_type->rate;
   1745  1.42        pk 		fd_formb.fd_formb_secshift = fd->sc_type->secsize;
   1746  1.42        pk 		fd_formb.fd_formb_nsecs = fd->sc_type->sectrac;
   1747  1.42        pk 		fd_formb.fd_formb_gaplen = fd->sc_type->gap2;
   1748  1.42        pk 		fd_formb.fd_formb_fillbyte = fd->sc_type->fillbyte;
   1749  1.42        pk 
   1750  1.49        pk 		bzero(il, sizeof il);
   1751  1.42        pk 		for (j = 0, i = 1; i <= fd_formb.fd_formb_nsecs; i++) {
   1752  1.49        pk 			while (il[(j%fd_formb.fd_formb_nsecs) + 1])
   1753  1.42        pk 				j++;
   1754  1.49        pk 			il[(j%fd_formb.fd_formb_nsecs) + 1] = i;
   1755  1.42        pk 			j += fd->sc_type->interleave;
   1756  1.42        pk 		}
   1757  1.42        pk 		for (i = 0; i < fd_formb.fd_formb_nsecs; i++) {
   1758  1.42        pk 			fd_formb.fd_formb_cylno(i) = form_cmd->cylinder;
   1759  1.42        pk 			fd_formb.fd_formb_headno(i) = form_cmd->head;
   1760  1.42        pk 			fd_formb.fd_formb_secno(i) = il[i+1];
   1761  1.42        pk 			fd_formb.fd_formb_secsize(i) = fd->sc_type->secsize;
   1762  1.42        pk 		}
   1763  1.42        pk 
   1764  1.42        pk 		return fdformat(dev, &fd_formb, p);
   1765  1.42        pk 
   1766  1.42        pk 	case FDIOCGETOPTS:		/* get drive options */
   1767  1.42        pk 		*(int *)addr = fd->sc_opts;
   1768  1.42        pk 		return (0);
   1769  1.42        pk 
   1770  1.42        pk 	case FDIOCSETOPTS:		/* set drive options */
   1771  1.42        pk 		fd->sc_opts = *(int *)addr;
   1772  1.42        pk 		return (0);
   1773  1.42        pk 
   1774   1.1        pk #ifdef DEBUG
   1775   1.1        pk 	case _IO('f', 100):
   1776   1.1        pk 		{
   1777   1.1        pk 		int i;
   1778   1.1        pk 		struct fdc_softc *fdc = (struct fdc_softc *)
   1779  1.16   thorpej 					fd->sc_dv.dv_parent;
   1780   1.1        pk 
   1781   1.1        pk 		out_fdc(fdc, NE7CMD_DUMPREG);
   1782   1.1        pk 		fdcresult(fdc);
   1783  1.39  christos 		printf("dumpreg(%d regs): <", fdc->sc_nstat);
   1784   1.1        pk 		for (i = 0; i < fdc->sc_nstat; i++)
   1785  1.52      fair 			printf(" 0x%x", fdc->sc_status[i]);
   1786  1.39  christos 		printf(">\n");
   1787   1.1        pk 		}
   1788   1.1        pk 
   1789  1.42        pk 		return (0);
   1790   1.1        pk 	case _IOW('f', 101, int):
   1791  1.16   thorpej 		((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg &=
   1792   1.2        pk 			~CFG_THRHLD_MASK;
   1793  1.16   thorpej 		((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg |=
   1794   1.2        pk 			(*(int *)addr & CFG_THRHLD_MASK);
   1795  1.24  christos 		fdconf((struct fdc_softc *) fd->sc_dv.dv_parent);
   1796  1.42        pk 		return (0);
   1797   1.1        pk 	case _IO('f', 102):
   1798   1.1        pk 		{
   1799   1.1        pk 		int i;
   1800   1.1        pk 		struct fdc_softc *fdc = (struct fdc_softc *)
   1801  1.16   thorpej 					fd->sc_dv.dv_parent;
   1802   1.1        pk 		out_fdc(fdc, NE7CMD_SENSEI);
   1803   1.1        pk 		fdcresult(fdc);
   1804  1.39  christos 		printf("sensei(%d regs): <", fdc->sc_nstat);
   1805   1.1        pk 		for (i=0; i< fdc->sc_nstat; i++)
   1806  1.39  christos 			printf(" 0x%x", fdc->sc_status[i]);
   1807   1.1        pk 		}
   1808  1.39  christos 		printf(">\n");
   1809  1.42        pk 		return (0);
   1810   1.1        pk #endif
   1811   1.1        pk 	default:
   1812  1.42        pk 		return (ENOTTY);
   1813   1.1        pk 	}
   1814   1.1        pk 
   1815   1.1        pk #ifdef DIAGNOSTIC
   1816   1.1        pk 	panic("fdioctl: impossible");
   1817   1.1        pk #endif
   1818  1.19   thorpej }
   1819  1.19   thorpej 
   1820  1.42        pk int
   1821  1.42        pk fdformat(dev, finfo, p)
   1822  1.42        pk 	dev_t dev;
   1823  1.42        pk 	struct ne7_fd_formb *finfo;
   1824  1.42        pk 	struct proc *p;
   1825  1.42        pk {
   1826  1.42        pk 	int rv = 0, s;
   1827  1.42        pk 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   1828  1.42        pk 	struct fd_type *type = fd->sc_type;
   1829  1.42        pk 	struct buf *bp;
   1830  1.42        pk 
   1831  1.42        pk 	/* set up a buffer header for fdstrategy() */
   1832  1.42        pk 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
   1833  1.42        pk 	if (bp == 0)
   1834  1.42        pk 		return (ENOBUFS);
   1835  1.42        pk 
   1836  1.42        pk 	PHOLD(p);
   1837  1.42        pk 	bzero((void *)bp, sizeof(struct buf));
   1838  1.42        pk 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
   1839  1.42        pk 	bp->b_proc = p;
   1840  1.42        pk 	bp->b_dev = dev;
   1841  1.42        pk 
   1842  1.42        pk 	/*
   1843  1.49        pk 	 * Calculate a fake blkno, so fdstrategy() would initiate a
   1844  1.49        pk 	 * seek to the requested cylinder.
   1845  1.42        pk 	 */
   1846  1.53        pk 	bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
   1847  1.53        pk 		       + finfo->head * type->sectrac) * FD_BSIZE(fd))
   1848  1.53        pk 		      / DEV_BSIZE;
   1849  1.42        pk 
   1850  1.42        pk 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
   1851  1.42        pk 	bp->b_data = (caddr_t)finfo;
   1852  1.42        pk 
   1853  1.42        pk #ifdef FD_DEBUG
   1854  1.42        pk 	if (fdc_debug)
   1855  1.52      fair 		printf("fdformat: blkno 0x%x count %ld\n",
   1856  1.42        pk 			bp->b_blkno, bp->b_bcount);
   1857  1.42        pk #endif
   1858  1.42        pk 
   1859  1.42        pk 	/* now do the format */
   1860  1.42        pk 	fdstrategy(bp);
   1861  1.42        pk 
   1862  1.42        pk 	/* ...and wait for it to complete */
   1863  1.42        pk 	s = splbio();
   1864  1.42        pk 	while (!(bp->b_flags & B_DONE)) {
   1865  1.42        pk 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
   1866  1.42        pk 		if (rv == EWOULDBLOCK)
   1867  1.42        pk 			break;
   1868  1.42        pk 	}
   1869  1.42        pk 	splx(s);
   1870  1.42        pk 
   1871  1.42        pk 	if (rv == EWOULDBLOCK) {
   1872  1.42        pk 		/* timed out */
   1873  1.42        pk 		rv = EIO;
   1874  1.42        pk 		biodone(bp);
   1875  1.42        pk 	}
   1876  1.42        pk 	if (bp->b_flags & B_ERROR) {
   1877  1.42        pk 		rv = bp->b_error;
   1878  1.42        pk 	}
   1879  1.42        pk 	PRELE(p);
   1880  1.42        pk 	free(bp, M_TEMP);
   1881  1.42        pk 	return (rv);
   1882  1.42        pk }
   1883  1.42        pk 
   1884  1.19   thorpej void
   1885  1.19   thorpej fdgetdisklabel(dev)
   1886  1.19   thorpej 	dev_t dev;
   1887  1.19   thorpej {
   1888  1.19   thorpej 	int unit = FDUNIT(dev), i;
   1889  1.26   thorpej 	struct fd_softc *fd = fd_cd.cd_devs[unit];
   1890  1.19   thorpej 	struct disklabel *lp = fd->sc_dk.dk_label;
   1891  1.19   thorpej 	struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
   1892  1.19   thorpej 
   1893  1.19   thorpej 	bzero(lp, sizeof(struct disklabel));
   1894  1.19   thorpej 	bzero(lp, sizeof(struct cpu_disklabel));
   1895  1.19   thorpej 
   1896  1.19   thorpej 	lp->d_type = DTYPE_FLOPPY;
   1897  1.53        pk 	lp->d_secsize = FD_BSIZE(fd);
   1898  1.19   thorpej 	lp->d_secpercyl = fd->sc_type->seccyl;
   1899  1.19   thorpej 	lp->d_nsectors = fd->sc_type->sectrac;
   1900  1.49        pk 	lp->d_ncylinders = fd->sc_type->cylinders;
   1901  1.19   thorpej 	lp->d_ntracks = fd->sc_type->heads;	/* Go figure... */
   1902  1.19   thorpej 	lp->d_rpm = 3600;	/* XXX like it matters... */
   1903  1.19   thorpej 
   1904  1.19   thorpej 	strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename));
   1905  1.19   thorpej 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1906  1.19   thorpej 	lp->d_interleave = 1;
   1907  1.19   thorpej 
   1908  1.19   thorpej 	lp->d_partitions[RAW_PART].p_offset = 0;
   1909  1.19   thorpej 	lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
   1910  1.19   thorpej 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1911  1.19   thorpej 	lp->d_npartitions = RAW_PART + 1;
   1912  1.19   thorpej 
   1913  1.19   thorpej 	lp->d_magic = DISKMAGIC;
   1914  1.19   thorpej 	lp->d_magic2 = DISKMAGIC;
   1915  1.19   thorpej 	lp->d_checksum = dkcksum(lp);
   1916  1.19   thorpej 
   1917  1.19   thorpej 	/*
   1918  1.19   thorpej 	 * Call the generic disklabel extraction routine.  If there's
   1919  1.19   thorpej 	 * not a label there, fake it.
   1920  1.19   thorpej 	 */
   1921  1.19   thorpej 	if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
   1922  1.19   thorpej 		strncpy(lp->d_packname, "default label",
   1923  1.19   thorpej 		    sizeof(lp->d_packname));
   1924  1.19   thorpej 		/*
   1925  1.19   thorpej 		 * Reset the partition info; it might have gotten
   1926  1.19   thorpej 		 * trashed in readdisklabel().
   1927  1.19   thorpej 		 *
   1928  1.19   thorpej 		 * XXX Why do we have to do this?  readdisklabel()
   1929  1.19   thorpej 		 * should be safe...
   1930  1.19   thorpej 		 */
   1931  1.19   thorpej 		for (i = 0; i < MAXPARTITIONS; ++i) {
   1932  1.19   thorpej 			lp->d_partitions[i].p_offset = 0;
   1933  1.19   thorpej 			if (i == RAW_PART) {
   1934  1.19   thorpej 				lp->d_partitions[i].p_size =
   1935  1.19   thorpej 				    lp->d_secpercyl * lp->d_ncylinders;
   1936  1.19   thorpej 				lp->d_partitions[i].p_fstype = FS_BSDFFS;
   1937  1.19   thorpej 			} else {
   1938  1.19   thorpej 				lp->d_partitions[i].p_size = 0;
   1939  1.19   thorpej 				lp->d_partitions[i].p_fstype = FS_UNUSED;
   1940  1.19   thorpej 			}
   1941  1.19   thorpej 		}
   1942  1.19   thorpej 		lp->d_npartitions = RAW_PART + 1;
   1943  1.19   thorpej 	}
   1944  1.19   thorpej }
   1945  1.19   thorpej 
   1946  1.19   thorpej void
   1947  1.50        pk fd_do_eject(fd)
   1948  1.50        pk 	struct fd_softc *fd;
   1949  1.19   thorpej {
   1950  1.50        pk 	struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
   1951  1.50        pk 
   1952  1.49        pk 	if (CPU_ISSUN4C) {
   1953  1.49        pk 		auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
   1954  1.49        pk 		delay(10);
   1955  1.49        pk 		auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
   1956  1.50        pk 		return;
   1957  1.49        pk 	}
   1958  1.50        pk 	if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077)) {
   1959  1.50        pk 		int dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
   1960  1.50        pk 		*fdc->sc_reg_dor = dor | FDO_EJ;
   1961  1.50        pk 		delay(10);
   1962  1.50        pk 		*fdc->sc_reg_dor = FDO_FRST | FDO_DS;
   1963  1.50        pk 		return;
   1964  1.49        pk 	}
   1965  1.19   thorpej }
   1966  1.19   thorpej 
   1967  1.45        pk #ifdef MEMORY_DISK_HOOKS
   1968  1.45        pk int	fd_read_md_image __P((size_t *, caddr_t *));
   1969  1.36        pk #endif
   1970  1.36        pk 
   1971  1.19   thorpej /* ARGSUSED */
   1972  1.19   thorpej void
   1973  1.19   thorpej fd_mountroot_hook(dev)
   1974  1.19   thorpej 	struct device *dev;
   1975  1.19   thorpej {
   1976  1.19   thorpej 	int c;
   1977  1.19   thorpej 
   1978  1.50        pk 	fd_do_eject((struct fd_softc *)dev);
   1979  1.39  christos 	printf("Insert filesystem floppy and press return.");
   1980  1.19   thorpej 	for (;;) {
   1981  1.19   thorpej 		c = cngetc();
   1982  1.19   thorpej 		if ((c == '\r') || (c == '\n')) {
   1983  1.39  christos 			printf("\n");
   1984  1.36        pk 			break;
   1985  1.36        pk 		}
   1986  1.36        pk 	}
   1987  1.46        pk #ifdef MEMORY_DISK_HOOKS
   1988  1.36        pk 	{
   1989  1.45        pk 	extern int (*md_read_image) __P((size_t *, caddr_t *));
   1990  1.45        pk 	md_read_image = fd_read_md_image;
   1991  1.36        pk 	}
   1992  1.36        pk #endif
   1993  1.36        pk }
   1994  1.36        pk 
   1995  1.45        pk #ifdef MEMORY_DISK_HOOKS
   1996  1.36        pk 
   1997  1.36        pk #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
   1998  1.36        pk 
   1999  1.36        pk int
   2000  1.45        pk fd_read_md_image(sizep, addrp)
   2001  1.36        pk 	size_t	*sizep;
   2002  1.36        pk 	caddr_t	*addrp;
   2003  1.36        pk {
   2004  1.36        pk 	struct buf buf, *bp = &buf;
   2005  1.36        pk 	dev_t dev;
   2006  1.36        pk 	off_t offset;
   2007  1.36        pk 	caddr_t addr;
   2008  1.36        pk 
   2009  1.36        pk 	dev = makedev(54,0);	/* XXX */
   2010  1.36        pk 
   2011  1.36        pk 	MALLOC(addr, caddr_t, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
   2012  1.36        pk 	*addrp = addr;
   2013  1.36        pk 
   2014  1.36        pk 	if (fdopen(dev, 0, S_IFCHR, NULL))
   2015  1.36        pk 		panic("fd: mountroot: fdopen");
   2016  1.36        pk 
   2017  1.36        pk 	offset = 0;
   2018  1.36        pk 
   2019  1.36        pk 	for (;;) {
   2020  1.36        pk 		bp->b_dev = dev;
   2021  1.36        pk 		bp->b_error = 0;
   2022  1.36        pk 		bp->b_resid = 0;
   2023  1.36        pk 		bp->b_proc = NULL;
   2024  1.36        pk 		bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
   2025  1.36        pk 		bp->b_blkno = btodb(offset);
   2026  1.36        pk 		bp->b_bcount = DEV_BSIZE;
   2027  1.36        pk 		bp->b_data = addr;
   2028  1.36        pk 		fdstrategy(bp);
   2029  1.36        pk 		while ((bp->b_flags & B_DONE) == 0) {
   2030  1.36        pk 			tsleep((caddr_t)bp, PRIBIO + 1, "physio", 0);
   2031  1.19   thorpej 		}
   2032  1.36        pk 		if (bp->b_error)
   2033  1.36        pk 			panic("fd: mountroot: fdread error %d", bp->b_error);
   2034  1.36        pk 
   2035  1.36        pk 		if (bp->b_resid != 0)
   2036  1.36        pk 			break;
   2037  1.36        pk 
   2038  1.36        pk 		addr += DEV_BSIZE;
   2039  1.36        pk 		offset += DEV_BSIZE;
   2040  1.36        pk 		if (offset + DEV_BSIZE > FDMICROROOTSIZE)
   2041  1.36        pk 			break;
   2042  1.19   thorpej 	}
   2043  1.36        pk 	(void)fdclose(dev, 0, S_IFCHR, NULL);
   2044  1.36        pk 	*sizep = offset;
   2045  1.50        pk 	fd_do_eject(fd_cd.cd_devs[FDUNIT(dev)]);
   2046  1.42        pk 	return (0);
   2047   1.1        pk }
   2048  1.36        pk #endif
   2049