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