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