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