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