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