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