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