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