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