Home | History | Annotate | Line # | Download | only in isa
fd.c revision 1.28
      1 /*	$NetBSD: fd.c,v 1.28 2002/10/02 02:09:17 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      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. All advertising materials mentioning features or use of this software
     55  *    must display the following acknowledgement:
     56  *	This product includes software developed by the University of
     57  *	California, Berkeley and its contributors.
     58  * 4. Neither the name of the University nor the names of its contributors
     59  *    may be used to endorse or promote products derived from this software
     60  *    without specific prior written permission.
     61  *
     62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  * SUCH DAMAGE.
     73  *
     74  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     75  */
     76 
     77 /*
     78  * Floppy formatting facilities merged from FreeBSD fd.c driver:
     79  *	Id: fd.c,v 1.53 1995/03/12 22:40:56 joerg Exp
     80  * which carries the same copyright/redistribution notice as shown above with
     81  * the addition of the following statement before the "Redistribution and
     82  * use ..." clause:
     83  *
     84  * Copyright (c) 1993, 1994 by
     85  *  jc (at) irbs.UUCP (John Capo)
     86  *  vak (at) zebub.msk.su (Serge Vakulenko)
     87  *  ache (at) astral.msk.su (Andrew A. Chernov)
     88  *
     89  * Copyright (c) 1993, 1994, 1995 by
     90  *  joerg_wunsch (at) uriah.sax.de (Joerg Wunsch)
     91  *  dufault (at) hda.com (Peter Dufault)
     92  */
     93 
     94 #include <sys/cdefs.h>
     95 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.28 2002/10/02 02:09:17 thorpej Exp $");
     96 
     97 #include "rnd.h"
     98 #include "opt_ddb.h"
     99 
    100 /*
    101  * XXX This driver should be properly MI'd some day, but this allows us
    102  * XXX to eliminate a lot of code duplication for now.
    103  */
    104 #if !defined(alpha) && !defined(algor) && !defined(atari) && \
    105     !defined(bebox) && !defined(evbmips) && !defined(i386) && \
    106     !defined(prep) && !defined(sandpoint) && !defined(x86_64)
    107 #error platform not supported by this driver, yet
    108 #endif
    109 
    110 #include <sys/param.h>
    111 #include <sys/systm.h>
    112 #include <sys/callout.h>
    113 #include <sys/kernel.h>
    114 #include <sys/file.h>
    115 #include <sys/ioctl.h>
    116 #include <sys/device.h>
    117 #include <sys/disklabel.h>
    118 #include <sys/dkstat.h>
    119 #include <sys/disk.h>
    120 #include <sys/buf.h>
    121 #include <sys/malloc.h>
    122 #include <sys/uio.h>
    123 #include <sys/syslog.h>
    124 #include <sys/queue.h>
    125 #include <sys/proc.h>
    126 #include <sys/fdio.h>
    127 #include <sys/conf.h>
    128 #if NRND > 0
    129 #include <sys/rnd.h>
    130 #endif
    131 
    132 #include <uvm/uvm_extern.h>
    133 
    134 #include <dev/cons.h>
    135 
    136 #include <machine/cpu.h>
    137 #include <machine/bus.h>
    138 
    139 #if defined(atari)
    140 /*
    141  * On the atari, it is configured as fdcisa
    142  */
    143 #define	FDCCF_DRIVE		FDCISACF_DRIVE
    144 #define	FDCCF_DRIVE_DEFAULT	FDCISACF_DRIVE_DEFAULT
    145 
    146 #define	fd_cd	fdisa_cd
    147 #define	fd_ca	fdisa_ca
    148 #endif /* atari */
    149 
    150 #include <machine/intr.h>
    151 
    152 #include <dev/isa/isavar.h>
    153 #include <dev/isa/isadmavar.h>
    154 
    155 #include <dev/isa/fdreg.h>
    156 #include <dev/isa/fdcvar.h>
    157 
    158 #if defined(i386)
    159 
    160 #include <dev/ic/mc146818reg.h>			/* for NVRAM access */
    161 #include <i386/isa/nvram.h>
    162 
    163 #include "mca.h"
    164 #if NMCA > 0
    165 #include <machine/mca_machdep.h>		/* for MCA_system */
    166 #endif
    167 
    168 #endif /* i386 */
    169 
    170 #define FDUNIT(dev)	(minor(dev) / 8)
    171 #define FDTYPE(dev)	(minor(dev) % 8)
    172 
    173 /* XXX misuse a flag to identify format operation */
    174 #define B_FORMAT B_XXX
    175 
    176 /* controller driver configuration */
    177 int fdprint __P((void *, const char *));
    178 
    179 /*
    180  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    181  * we tell them apart.
    182  */
    183 struct fd_type {
    184 	int	sectrac;	/* sectors per track */
    185 	int	heads;		/* number of heads */
    186 	int	seccyl;		/* sectors per cylinder */
    187 	int	secsize;	/* size code for sectors */
    188 	int	datalen;	/* data len when secsize = 0 */
    189 	int	steprate;	/* step rate and head unload time */
    190 	int	gap1;		/* gap len between sectors */
    191 	int	gap2;		/* formatting gap */
    192 	int	cyls;		/* total num of cylinders */
    193 	int	size;		/* size of disk in sectors */
    194 	int	step;		/* steps per cylinder */
    195 	int	rate;		/* transfer speed code */
    196 	u_char	fillbyte;	/* format fill byte */
    197 	u_char	interleave;	/* interleave factor (formatting) */
    198 	const char	*name;
    199 };
    200 
    201 #if NMCA > 0
    202 /* MCA - specific entries */
    203 const struct fd_type mca_fd_types[] = {
    204 	{ 18,2,36,2,0xff,0x0f,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette - XXX try 16ms step rate */
    205 	{  9,2,18,2,0xff,0x4f,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette - XXX try 24ms step rate */
    206 };
    207 #endif /* NMCA > 0 */
    208 
    209 /* The order of entries in the following table is important -- BEWARE! */
    210 
    211 #if defined(atari)
    212 const struct fd_type fd_types[] = {
    213 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
    214 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette */
    215 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"   }, /* 1.44MB diskette */
    216 };
    217 #else
    218 const struct fd_type fd_types[] = {
    219 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"   }, /* 1.44MB diskette */
    220 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB"    }, /* 1.2 MB AT-diskettes */
    221 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */
    222 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
    223 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette */
    224 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x"  }, /* 720kB in 1.2MB drive */
    225 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
    226 };
    227 #endif /* defined(atari) */
    228 
    229 /* software state, per disk (with up to 4 disks per ctlr) */
    230 struct fd_softc {
    231 	struct device sc_dev;
    232 	struct disk sc_dk;
    233 
    234 	const struct fd_type *sc_deftype; /* default type descriptor */
    235 	struct fd_type *sc_type;	/* current type descriptor */
    236 	struct fd_type sc_type_copy;	/* copy for fiddling when formatting */
    237 
    238 	struct callout sc_motoron_ch;
    239 	struct callout sc_motoroff_ch;
    240 
    241 	daddr_t	sc_blkno;	/* starting block number */
    242 	int sc_bcount;		/* byte count left */
    243  	int sc_opts;		/* user-set options */
    244 	int sc_skip;		/* bytes already transferred */
    245 	int sc_nblks;		/* number of blocks currently transferring */
    246 	int sc_nbytes;		/* number of bytes currently transferring */
    247 
    248 	int sc_drive;		/* physical unit number */
    249 	int sc_flags;
    250 #define	FD_OPEN		0x01		/* it's open */
    251 #define	FD_MOTOR	0x02		/* motor should be on */
    252 #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    253 	int sc_cylin;		/* where we think the head is */
    254 
    255 	void *sc_sdhook;	/* saved shutdown hook for drive. */
    256 
    257 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    258 	int sc_ops;		/* I/O ops since last switch */
    259 	struct bufq_state sc_q;	/* pending I/O requests */
    260 	int sc_active;		/* number of active I/O operations */
    261 
    262 #if NRND > 0
    263 	rndsource_element_t	rnd_source;
    264 #endif
    265 };
    266 
    267 int fdprobe __P((struct device *, struct cfdata *, void *));
    268 void fdattach __P((struct device *, struct device *, void *));
    269 
    270 extern struct cfdriver fd_cd;
    271 
    272 CFATTACH_DECL(fd, sizeof(struct fd_softc),
    273 	fdprobe, fdattach, NULL, NULL);
    274 
    275 dev_type_open(fdopen);
    276 dev_type_close(fdclose);
    277 dev_type_read(fdread);
    278 dev_type_write(fdwrite);
    279 dev_type_ioctl(fdioctl);
    280 dev_type_strategy(fdstrategy);
    281 
    282 const struct bdevsw fd_bdevsw = {
    283 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
    284 };
    285 
    286 const struct cdevsw fd_cdevsw = {
    287 	fdopen, fdclose, fdread, fdwrite, fdioctl,
    288 	nostop, notty, nopoll, nommap, D_DISK
    289 };
    290 
    291 void fdgetdisklabel __P((struct fd_softc *));
    292 int fd_get_parms __P((struct fd_softc *));
    293 void fdstart __P((struct fd_softc *));
    294 
    295 struct dkdriver fddkdriver = { fdstrategy };
    296 
    297 #if defined(i386)
    298 const struct fd_type *fd_nvtotype __P((char *, int, int));
    299 #endif /* i386 */
    300 void fd_set_motor __P((struct fdc_softc *fdc, int reset));
    301 void fd_motor_off __P((void *arg));
    302 void fd_motor_on __P((void *arg));
    303 int fdcresult __P((struct fdc_softc *fdc));
    304 void fdcstart __P((struct fdc_softc *fdc));
    305 void fdcstatus __P((struct device *dv, int n, char *s));
    306 void fdctimeout __P((void *arg));
    307 void fdcpseudointr __P((void *arg));
    308 void fdcretry __P((struct fdc_softc *fdc));
    309 void fdfinish __P((struct fd_softc *fd, struct buf *bp));
    310 __inline const struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
    311 int fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
    312 
    313 void	fd_mountroot_hook __P((struct device *));
    314 
    315 /*
    316  * Arguments passed between fdcattach and fdprobe.
    317  */
    318 struct fdc_attach_args {
    319 	int fa_drive;
    320 	const struct fd_type *fa_deftype;
    321 };
    322 
    323 /*
    324  * Print the location of a disk drive (called just before attaching the
    325  * the drive).  If `fdc' is not NULL, the drive was found but was not
    326  * in the system config file; print the drive name as well.
    327  * Return QUIET (config_find ignores this if the device was configured) to
    328  * avoid printing `fdN not configured' messages.
    329  */
    330 int
    331 fdprint(aux, fdc)
    332 	void *aux;
    333 	const char *fdc;
    334 {
    335 	register struct fdc_attach_args *fa = aux;
    336 
    337 	if (!fdc)
    338 		printf(" drive %d", fa->fa_drive);
    339 	return QUIET;
    340 }
    341 
    342 void
    343 fdcattach(fdc)
    344 	struct fdc_softc *fdc;
    345 {
    346 	struct fdc_attach_args fa;
    347 	bus_space_tag_t iot;
    348 	bus_space_handle_t ioh;
    349 #if defined(i386)
    350 	int type;
    351 #endif
    352 
    353 	iot = fdc->sc_iot;
    354 	ioh = fdc->sc_ioh;
    355 	callout_init(&fdc->sc_timo_ch);
    356 	callout_init(&fdc->sc_intr_ch);
    357 
    358 	fdc->sc_state = DEVIDLE;
    359 	TAILQ_INIT(&fdc->sc_drives);
    360 
    361 	fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq);
    362 
    363 	if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize,
    364 	    BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    365 		printf("%s: can't set up ISA DMA map\n",
    366 		    fdc->sc_dev.dv_xname);
    367 		return;
    368 	}
    369 
    370 	/*
    371 	 * Reset the controller to get it into a known state. Not all
    372 	 * probes necessarily need do this to discover the controller up
    373 	 * front, so don't assume anything.
    374 	 */
    375 
    376 	bus_space_write_1(iot, ioh, fdout, 0);
    377 	delay(100);
    378 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
    379 
    380 	/* see if it can handle a command */
    381 	if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) {
    382 		printf ("%s: can't reset controller\n", fdc->sc_dev.dv_xname);
    383 		return;
    384 	}
    385 	out_fdc(iot, ioh, 0xdf);
    386 	out_fdc(iot, ioh, 2);
    387 
    388 #if defined(i386)
    389 	/*
    390 	 * The NVRAM info only tells us about the first two disks on the
    391 	 * `primary' floppy controller.
    392 	 */
    393 	if (fdc->sc_dev.dv_unit == 0)
    394 		type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */
    395 	else
    396 		type = -1;
    397 #endif /* i386 */
    398 
    399 	/* physical limit: four drives per controller. */
    400 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    401 #if defined(i386)
    402 		if (type >= 0 && fa.fa_drive < 2)
    403 			fa.fa_deftype = fd_nvtotype(fdc->sc_dev.dv_xname,
    404 			    type, fa.fa_drive);
    405 		else
    406 			fa.fa_deftype = NULL;		/* unknown */
    407 #elif defined(atari)
    408 		/*
    409 		 * Atari has a different ordening, defaults to 1.44
    410 		 */
    411 		fa.fa_deftype = &fd_types[2];
    412 #else
    413 		/*
    414 		 * Default to 1.44MB on Alpha and BeBox.  How do we tell
    415 		 * on these platforms?
    416 		 */
    417 		fa.fa_deftype = &fd_types[0];
    418 #endif /* i386 */
    419 		(void)config_found(&fdc->sc_dev, (void *)&fa, fdprint);
    420 	}
    421 }
    422 
    423 int
    424 fdprobe(parent, match, aux)
    425 	struct device *parent;
    426 	struct cfdata *match;
    427 	void *aux;
    428 {
    429 	struct fdc_softc *fdc = (void *)parent;
    430 	struct cfdata *cf = match;
    431 	struct fdc_attach_args *fa = aux;
    432 	int drive = fa->fa_drive;
    433 	bus_space_tag_t iot = fdc->sc_iot;
    434 	bus_space_handle_t ioh = fdc->sc_ioh;
    435 	int n;
    436 
    437 	if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
    438 	    cf->cf_loc[FDCCF_DRIVE] != drive)
    439 		return 0;
    440 	/*
    441 	 * XXX
    442 	 * This is to work around some odd interactions between this driver
    443 	 * and SMC Ethernet cards.
    444 	 */
    445 	if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2)
    446 		return 0;
    447 
    448 	/* select drive and turn on motor */
    449 	bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
    450 	/* wait for motor to spin up */
    451 	delay(250000);
    452 	out_fdc(iot, ioh, NE7CMD_RECAL);
    453 	out_fdc(iot, ioh, drive);
    454 	/* wait for recalibrate */
    455 	delay(2000000);
    456 	out_fdc(iot, ioh, NE7CMD_SENSEI);
    457 	n = fdcresult(fdc);
    458 #ifdef FD_DEBUG
    459 	{
    460 		int i;
    461 		printf("fdprobe: status");
    462 		for (i = 0; i < n; i++)
    463 			printf(" %x", fdc->sc_status[i]);
    464 		printf("\n");
    465 	}
    466 #endif
    467 	/* turn off motor */
    468 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
    469 
    470 #if defined(bebox)	/* XXX What is this about? --thorpej (at) netbsd.org */
    471 	if (n != 2 || (fdc->sc_status[1] != 0))
    472 		return 0;
    473 #else
    474 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    475 		return 0;
    476 #endif /* bebox */
    477 
    478 	return 1;
    479 }
    480 
    481 /*
    482  * Controller is working, and drive responded.  Attach it.
    483  */
    484 void
    485 fdattach(parent, self, aux)
    486 	struct device *parent, *self;
    487 	void *aux;
    488 {
    489 	struct fdc_softc *fdc = (void *)parent;
    490 	struct fd_softc *fd = (void *)self;
    491 	struct fdc_attach_args *fa = aux;
    492 	const struct fd_type *type = fa->fa_deftype;
    493 	int drive = fa->fa_drive;
    494 
    495 	callout_init(&fd->sc_motoron_ch);
    496 	callout_init(&fd->sc_motoroff_ch);
    497 
    498 	/* XXX Allow `flags' to override device type? */
    499 
    500 	if (type)
    501 		printf(": %s, %d cyl, %d head, %d sec\n", type->name,
    502 		    type->cyls, type->heads, type->sectrac);
    503 	else
    504 		printf(": density unknown\n");
    505 
    506 	bufq_alloc(&fd->sc_q, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER);
    507 	fd->sc_cylin = -1;
    508 	fd->sc_drive = drive;
    509 	fd->sc_deftype = type;
    510 	fdc->sc_fd[drive] = fd;
    511 
    512 	/*
    513 	 * Initialize and attach the disk structure.
    514 	 */
    515 	fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
    516 	fd->sc_dk.dk_driver = &fddkdriver;
    517 	disk_attach(&fd->sc_dk);
    518 
    519 	/*
    520 	 * Establish a mountroot hook.
    521 	 */
    522 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
    523 
    524 	/* Needed to power off if the motor is on when we halt. */
    525 	fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
    526 
    527 #if NRND > 0
    528 	rnd_attach_source(&fd->rnd_source, fd->sc_dev.dv_xname,
    529 			  RND_TYPE_DISK, 0);
    530 #endif
    531 }
    532 
    533 #if defined(i386)
    534 /*
    535  * Translate nvram type into internal data structure.  Return NULL for
    536  * none/unknown/unusable.
    537  */
    538 const struct fd_type *
    539 fd_nvtotype(fdc, nvraminfo, drive)
    540 	char *fdc;
    541 	int nvraminfo, drive;
    542 {
    543 	int type;
    544 
    545 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
    546 	switch (type) {
    547 	case NVRAM_DISKETTE_NONE:
    548 		return NULL;
    549 	case NVRAM_DISKETTE_12M:
    550 		return &fd_types[1];
    551 	case NVRAM_DISKETTE_TYPE5:
    552 	case NVRAM_DISKETTE_TYPE6:
    553 		/* XXX We really ought to handle 2.88MB format. */
    554 	case NVRAM_DISKETTE_144M:
    555 #if NMCA > 0
    556 		if (MCA_system)
    557 			return &mca_fd_types[0];
    558 		else
    559 #endif /* NMCA > 0 */
    560 			return &fd_types[0];
    561 	case NVRAM_DISKETTE_360K:
    562 		return &fd_types[3];
    563 	case NVRAM_DISKETTE_720K:
    564 #if NMCA > 0
    565 		if (MCA_system)
    566 			return &mca_fd_types[1];
    567 		else
    568 #endif /* NMCA > 0 */
    569 			return &fd_types[4];
    570 	default:
    571 		printf("%s: drive %d: unknown device type 0x%x\n",
    572 		    fdc, drive, type);
    573 		return NULL;
    574 	}
    575 }
    576 #endif /* i386 */
    577 
    578 __inline const struct fd_type *
    579 fd_dev_to_type(fd, dev)
    580 	struct fd_softc *fd;
    581 	dev_t dev;
    582 {
    583 	u_int type = FDTYPE(dev);
    584 
    585 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    586 		return NULL;
    587 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    588 }
    589 
    590 void
    591 fdstrategy(bp)
    592 	register struct buf *bp;	/* IO operation to perform */
    593 {
    594 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(bp->b_dev));
    595 	int sz;
    596  	int s;
    597 
    598 	/* Valid unit, controller, and request? */
    599 	if (bp->b_blkno < 0 ||
    600 	    ((bp->b_bcount % FDC_BSIZE) != 0 &&
    601 	     (bp->b_flags & B_FORMAT) == 0)) {
    602 		bp->b_error = EINVAL;
    603 		goto bad;
    604 	}
    605 
    606 	/* If it's a null transfer, return immediately. */
    607 	if (bp->b_bcount == 0)
    608 		goto done;
    609 
    610 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    611 
    612 	if (bp->b_blkno + sz > fd->sc_type->size) {
    613 		sz = fd->sc_type->size - bp->b_blkno;
    614 		if (sz == 0) {
    615 			/* If exactly at end of disk, return EOF. */
    616 			goto done;
    617 		}
    618 		if (sz < 0) {
    619 			/* If past end of disk, return EINVAL. */
    620 			bp->b_error = EINVAL;
    621 			goto bad;
    622 		}
    623 		/* Otherwise, truncate request. */
    624 		bp->b_bcount = sz << DEV_BSHIFT;
    625 	}
    626 
    627 	bp->b_rawblkno = bp->b_blkno;
    628  	bp->b_cylinder =
    629 	    bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    630 
    631 #ifdef FD_DEBUG
    632 	printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld sz %d\n",
    633 	    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
    634 #endif
    635 
    636 	/* Queue transfer on drive, activate drive and controller if idle. */
    637 	s = splbio();
    638 	BUFQ_PUT(&fd->sc_q, bp);
    639 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
    640 	if (fd->sc_active == 0)
    641 		fdstart(fd);
    642 #ifdef DIAGNOSTIC
    643 	else {
    644 		struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    645 		if (fdc->sc_state == DEVIDLE) {
    646 			printf("fdstrategy: controller inactive\n");
    647 			fdcstart(fdc);
    648 		}
    649 	}
    650 #endif
    651 	splx(s);
    652 	return;
    653 
    654 bad:
    655 	bp->b_flags |= B_ERROR;
    656 done:
    657 	/* Toss transfer; we're done early. */
    658 	bp->b_resid = bp->b_bcount;
    659 	biodone(bp);
    660 }
    661 
    662 void
    663 fdstart(fd)
    664 	struct fd_softc *fd;
    665 {
    666 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    667 	int active = !TAILQ_EMPTY(&fdc->sc_drives);
    668 
    669 	/* Link into controller queue. */
    670 	fd->sc_active = 1;
    671 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    672 
    673 	/* If controller not already active, start it. */
    674 	if (!active)
    675 		fdcstart(fdc);
    676 }
    677 
    678 void
    679 fdfinish(fd, bp)
    680 	struct fd_softc *fd;
    681 	struct buf *bp;
    682 {
    683 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    684 
    685 	/*
    686 	 * Move this drive to the end of the queue to give others a `fair'
    687 	 * chance.  We only force a switch if N operations are completed while
    688 	 * another drive is waiting to be serviced, since there is a long motor
    689 	 * startup delay whenever we switch.
    690 	 */
    691 	(void)BUFQ_GET(&fd->sc_q);
    692 	if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
    693 		fd->sc_ops = 0;
    694 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    695 		if (BUFQ_PEEK(&fd->sc_q) != NULL)
    696 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    697 		else
    698 			fd->sc_active = 0;
    699 	}
    700 	bp->b_resid = fd->sc_bcount;
    701 	fd->sc_skip = 0;
    702 
    703 #if NRND > 0
    704 	rnd_add_uint32(&fd->rnd_source, bp->b_blkno);
    705 #endif
    706 
    707 	biodone(bp);
    708 	/* turn off motor 5s from now */
    709 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
    710 	fdc->sc_state = DEVIDLE;
    711 }
    712 
    713 int
    714 fdread(dev, uio, flags)
    715 	dev_t dev;
    716 	struct uio *uio;
    717 	int flags;
    718 {
    719 
    720 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    721 }
    722 
    723 int
    724 fdwrite(dev, uio, flags)
    725 	dev_t dev;
    726 	struct uio *uio;
    727 	int flags;
    728 {
    729 
    730 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    731 }
    732 
    733 void
    734 fd_set_motor(fdc, reset)
    735 	struct fdc_softc *fdc;
    736 	int reset;
    737 {
    738 	struct fd_softc *fd;
    739 	u_char status;
    740 	int n;
    741 
    742 	if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
    743 		status = fd->sc_drive;
    744 	else
    745 		status = 0;
    746 	if (!reset)
    747 		status |= FDO_FRST | FDO_FDMAEN;
    748 	for (n = 0; n < 4; n++)
    749 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    750 			status |= FDO_MOEN(n);
    751 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
    752 }
    753 
    754 void
    755 fd_motor_off(arg)
    756 	void *arg;
    757 {
    758 	struct fd_softc *fd = arg;
    759 	int s;
    760 
    761 	s = splbio();
    762 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    763 	fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0);
    764 	splx(s);
    765 }
    766 
    767 void
    768 fd_motor_on(arg)
    769 	void *arg;
    770 {
    771 	struct fd_softc *fd = arg;
    772 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    773 	int s;
    774 
    775 	s = splbio();
    776 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    777 	if ((TAILQ_FIRST(&fdc->sc_drives) == fd) &&(fdc->sc_state == MOTORWAIT))
    778 		(void) fdcintr(fdc);
    779 	splx(s);
    780 }
    781 
    782 int
    783 fdcresult(fdc)
    784 	struct fdc_softc *fdc;
    785 {
    786 	bus_space_tag_t iot = fdc->sc_iot;
    787 	bus_space_handle_t ioh = fdc->sc_ioh;
    788 	u_char i;
    789 	u_int j = 100000,
    790 	      n = 0;
    791 
    792 	for (; j; j--) {
    793 		i = bus_space_read_1(iot, ioh, fdsts) &
    794 		    (NE7_DIO | NE7_RQM | NE7_CB);
    795 		if (i == NE7_RQM)
    796 			return n;
    797 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    798 			if (n >= sizeof(fdc->sc_status)) {
    799 				log(LOG_ERR, "fdcresult: overrun\n");
    800 				return -1;
    801 			}
    802 			fdc->sc_status[n++] =
    803 			    bus_space_read_1(iot, ioh, fddata);
    804 		}
    805 		delay(10);
    806 	}
    807 	log(LOG_ERR, "fdcresult: timeout\n");
    808 	return -1;
    809 }
    810 
    811 int
    812 out_fdc(iot, ioh, x)
    813 	bus_space_tag_t iot;
    814 	bus_space_handle_t ioh;
    815 	u_char x;
    816 {
    817 	int i = 100000;
    818 
    819 	while ((bus_space_read_1(iot, ioh, fdsts) & NE7_DIO) && i-- > 0);
    820 	if (i <= 0)
    821 		return -1;
    822 	while ((bus_space_read_1(iot, ioh, fdsts) & NE7_RQM) == 0 && i-- > 0);
    823 	if (i <= 0)
    824 		return -1;
    825 	bus_space_write_1(iot, ioh, fddata, x);
    826 	return 0;
    827 }
    828 
    829 int
    830 fdopen(dev, flags, mode, p)
    831 	dev_t dev;
    832 	int flags;
    833 	int mode;
    834 	struct proc *p;
    835 {
    836 	struct fd_softc *fd;
    837 	const struct fd_type *type;
    838 
    839 	fd = device_lookup(&fd_cd, FDUNIT(dev));
    840 	if (fd == NULL)
    841 		return (ENXIO);
    842 
    843 	type = fd_dev_to_type(fd, dev);
    844 	if (type == NULL)
    845 		return ENXIO;
    846 
    847 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    848 	    memcmp(fd->sc_type, type, sizeof(*type)))
    849 		return EBUSY;
    850 
    851 	fd->sc_type_copy = *type;
    852 	fd->sc_type = &fd->sc_type_copy;
    853 	fd->sc_cylin = -1;
    854 	fd->sc_flags |= FD_OPEN;
    855 
    856 	return 0;
    857 }
    858 
    859 int
    860 fdclose(dev, flags, mode, p)
    861 	dev_t dev;
    862 	int flags;
    863 	int mode;
    864 	struct proc *p;
    865 {
    866 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
    867 
    868 	fd->sc_flags &= ~FD_OPEN;
    869 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
    870 	return 0;
    871 }
    872 
    873 void
    874 fdcstart(fdc)
    875 	struct fdc_softc *fdc;
    876 {
    877 
    878 #ifdef DIAGNOSTIC
    879 	/* only got here if controller's drive queue was inactive; should
    880 	   be in idle state */
    881 	if (fdc->sc_state != DEVIDLE) {
    882 		printf("fdcstart: not idle\n");
    883 		return;
    884 	}
    885 #endif
    886 	(void) fdcintr(fdc);
    887 }
    888 
    889 void
    890 fdcstatus(dv, n, s)
    891 	struct device *dv;
    892 	int n;
    893 	char *s;
    894 {
    895 	struct fdc_softc *fdc = (void *)dv->dv_parent;
    896 	char bits[64];
    897 
    898 	if (n == 0) {
    899 		out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
    900 		(void) fdcresult(fdc);
    901 		n = 2;
    902 	}
    903 
    904 	printf("%s: %s", dv->dv_xname, s);
    905 
    906 	switch (n) {
    907 	case 0:
    908 		printf("\n");
    909 		break;
    910 	case 2:
    911 		printf(" (st0 %s cyl %d)\n",
    912 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
    913 		    bits, sizeof(bits)), fdc->sc_status[1]);
    914 		break;
    915 	case 7:
    916 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
    917 		    NE7_ST0BITS, bits, sizeof(bits)));
    918 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
    919 		    NE7_ST1BITS, bits, sizeof(bits)));
    920 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
    921 		    NE7_ST2BITS, bits, sizeof(bits)));
    922 		printf(" cyl %d head %d sec %d)\n",
    923 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    924 		break;
    925 #ifdef DIAGNOSTIC
    926 	default:
    927 		printf("\nfdcstatus: weird size");
    928 		break;
    929 #endif
    930 	}
    931 }
    932 
    933 void
    934 fdctimeout(arg)
    935 	void *arg;
    936 {
    937 	struct fdc_softc *fdc = arg;
    938 	struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
    939 	int s;
    940 
    941 	s = splbio();
    942 #ifdef DEBUG
    943 	log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
    944 #endif
    945 	fdcstatus(&fd->sc_dev, 0, "timeout");
    946 
    947 	if (BUFQ_PEEK(&fd->sc_q) != NULL)
    948 		fdc->sc_state++;
    949 	else
    950 		fdc->sc_state = DEVIDLE;
    951 
    952 	(void) fdcintr(fdc);
    953 	splx(s);
    954 }
    955 
    956 void
    957 fdcpseudointr(arg)
    958 	void *arg;
    959 {
    960 	int s;
    961 
    962 	/* Just ensure it has the right spl. */
    963 	s = splbio();
    964 	(void) fdcintr(arg);
    965 	splx(s);
    966 }
    967 
    968 int
    969 fdcintr(arg)
    970 	void *arg;
    971 {
    972 	struct fdc_softc *fdc = arg;
    973 #define	st0	fdc->sc_status[0]
    974 #define	cyl	fdc->sc_status[1]
    975 	struct fd_softc *fd;
    976 	struct buf *bp;
    977 	bus_space_tag_t iot = fdc->sc_iot;
    978 	bus_space_handle_t ioh = fdc->sc_ioh;
    979 	int read, head, sec, i, nblks;
    980 	struct fd_type *type;
    981 	struct ne7_fd_formb *finfo = NULL;
    982 
    983 loop:
    984 	/* Is there a drive for the controller to do a transfer with? */
    985 	fd = TAILQ_FIRST(&fdc->sc_drives);
    986 	if (fd == NULL) {
    987 		fdc->sc_state = DEVIDLE;
    988  		return 1;
    989 	}
    990 
    991 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    992 	bp = BUFQ_PEEK(&fd->sc_q);
    993 	if (bp == NULL) {
    994 		fd->sc_ops = 0;
    995 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    996 		fd->sc_active = 0;
    997 		goto loop;
    998 	}
    999 
   1000 	if (bp->b_flags & B_FORMAT)
   1001 		finfo = (struct ne7_fd_formb *)bp->b_data;
   1002 
   1003 	switch (fdc->sc_state) {
   1004 	case DEVIDLE:
   1005 		fdc->sc_errors = 0;
   1006 		fd->sc_skip = 0;
   1007 		fd->sc_bcount = bp->b_bcount;
   1008 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
   1009 		callout_stop(&fd->sc_motoroff_ch);
   1010 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1011 			fdc->sc_state = MOTORWAIT;
   1012 			return 1;
   1013 		}
   1014 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1015 			/* Turn on the motor, being careful about pairing. */
   1016 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1017 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1018 				callout_stop(&ofd->sc_motoroff_ch);
   1019 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1020 			}
   1021 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1022 			fd_set_motor(fdc, 0);
   1023 			fdc->sc_state = MOTORWAIT;
   1024 			/* Allow .25s for motor to stabilize. */
   1025 			callout_reset(&fd->sc_motoron_ch, hz / 4,
   1026 			    fd_motor_on, fd);
   1027 			return 1;
   1028 		}
   1029 		/* Make sure the right drive is selected. */
   1030 		fd_set_motor(fdc, 0);
   1031 
   1032 		/* fall through */
   1033 	case DOSEEK:
   1034 	doseek:
   1035 		if (fd->sc_cylin == bp->b_cylinder)
   1036 			goto doio;
   1037 
   1038 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
   1039 		out_fdc(iot, ioh, fd->sc_type->steprate);
   1040 		out_fdc(iot, ioh, 6);		/* XXX head load time == 6ms */
   1041 
   1042 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
   1043 		out_fdc(iot, ioh, fd->sc_drive); /* drive number */
   1044 		out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
   1045 
   1046 		fd->sc_cylin = -1;
   1047 		fdc->sc_state = SEEKWAIT;
   1048 
   1049 		fd->sc_dk.dk_seek++;
   1050 		disk_busy(&fd->sc_dk);
   1051 
   1052 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
   1053 		return 1;
   1054 
   1055 	case DOIO:
   1056 	doio:
   1057 		type = fd->sc_type;
   1058 		if (finfo)
   1059 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
   1060 				      (char *)finfo;
   1061 		sec = fd->sc_blkno % type->seccyl;
   1062 		nblks = type->seccyl - sec;
   1063 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1064 		nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
   1065 		fd->sc_nblks = nblks;
   1066 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
   1067 		head = sec / type->sectrac;
   1068 		sec -= head * type->sectrac;
   1069 #ifdef DIAGNOSTIC
   1070 		{
   1071 			int block;
   1072 			block = (fd->sc_cylin * type->heads + head)
   1073 			    * type->sectrac + sec;
   1074 			if (block != fd->sc_blkno) {
   1075 				printf("fdcintr: block %d != blkno %d\n",
   1076 				    block, fd->sc_blkno);
   1077 #ifdef DDB
   1078 				 Debugger();
   1079 #endif
   1080 			}
   1081 		}
   1082 #endif
   1083 		read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
   1084 		isa_dmastart(fdc->sc_ic, fdc->sc_drq,
   1085 		    bp->b_data + fd->sc_skip, fd->sc_nbytes,
   1086 		    NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT);
   1087 		bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate);
   1088 #ifdef FD_DEBUG
   1089 		printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
   1090 			read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
   1091 			head, sec, nblks);
   1092 #endif
   1093 		if (finfo) {
   1094 			/* formatting */
   1095 			if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
   1096 				fdc->sc_errors = 4;
   1097 				fdcretry(fdc);
   1098 				goto loop;
   1099 			}
   1100 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
   1101 			out_fdc(iot, ioh, finfo->fd_formb_secshift);
   1102 			out_fdc(iot, ioh, finfo->fd_formb_nsecs);
   1103 			out_fdc(iot, ioh, finfo->fd_formb_gaplen);
   1104 			out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
   1105 		} else {
   1106 			if (read)
   1107 				out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
   1108 			else
   1109 				out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
   1110 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
   1111 			out_fdc(iot, ioh, fd->sc_cylin); /* track */
   1112 			out_fdc(iot, ioh, head);
   1113 			out_fdc(iot, ioh, sec + 1);	 /* sector +1 */
   1114 			out_fdc(iot, ioh, type->secsize);/* sector size */
   1115 			out_fdc(iot, ioh, type->sectrac);/* sectors/track */
   1116 			out_fdc(iot, ioh, type->gap1);	 /* gap1 size */
   1117 			out_fdc(iot, ioh, type->datalen);/* data length */
   1118 		}
   1119 		fdc->sc_state = IOCOMPLETE;
   1120 
   1121 		disk_busy(&fd->sc_dk);
   1122 
   1123 		/* allow 2 seconds for operation */
   1124 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
   1125 		return 1;				/* will return later */
   1126 
   1127 	case SEEKWAIT:
   1128 		callout_stop(&fdc->sc_timo_ch);
   1129 		fdc->sc_state = SEEKCOMPLETE;
   1130 		/* allow 1/50 second for heads to settle */
   1131 		callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
   1132 		return 1;
   1133 
   1134 	case SEEKCOMPLETE:
   1135 		disk_unbusy(&fd->sc_dk, 0);	/* no data on seek */
   1136 
   1137 		/* Make sure seek really happened. */
   1138 		out_fdc(iot, ioh, NE7CMD_SENSEI);
   1139 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
   1140 		    cyl != bp->b_cylinder * fd->sc_type->step) {
   1141 #ifdef FD_DEBUG
   1142 			fdcstatus(&fd->sc_dev, 2, "seek failed");
   1143 #endif
   1144 			fdcretry(fdc);
   1145 			goto loop;
   1146 		}
   1147 		fd->sc_cylin = bp->b_cylinder;
   1148 		goto doio;
   1149 
   1150 	case IOTIMEDOUT:
   1151 		isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
   1152 	case SEEKTIMEDOUT:
   1153 	case RECALTIMEDOUT:
   1154 	case RESETTIMEDOUT:
   1155 		fdcretry(fdc);
   1156 		goto loop;
   1157 
   1158 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1159 		callout_stop(&fdc->sc_timo_ch);
   1160 
   1161 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
   1162 
   1163 		if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
   1164 			isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
   1165 #ifdef FD_DEBUG
   1166 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
   1167 			    "read failed" : "write failed");
   1168 			printf("blkno %d nblks %d\n",
   1169 			    fd->sc_blkno, fd->sc_nblks);
   1170 #endif
   1171 			fdcretry(fdc);
   1172 			goto loop;
   1173 		}
   1174 		isa_dmadone(fdc->sc_ic, fdc->sc_drq);
   1175 		if (fdc->sc_errors) {
   1176 			diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
   1177 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1178 			printf("\n");
   1179 			fdc->sc_errors = 0;
   1180 		}
   1181 		fd->sc_blkno += fd->sc_nblks;
   1182 		fd->sc_skip += fd->sc_nbytes;
   1183 		fd->sc_bcount -= fd->sc_nbytes;
   1184 		if (!finfo && fd->sc_bcount > 0) {
   1185 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
   1186 			goto doseek;
   1187 		}
   1188 		fdfinish(fd, bp);
   1189 		goto loop;
   1190 
   1191 	case DORESET:
   1192 		/* try a reset, keep motor on */
   1193 		fd_set_motor(fdc, 1);
   1194 		delay(100);
   1195 		fd_set_motor(fdc, 0);
   1196 		fdc->sc_state = RESETCOMPLETE;
   1197 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
   1198 		return 1;			/* will return later */
   1199 
   1200 	case RESETCOMPLETE:
   1201 		callout_stop(&fdc->sc_timo_ch);
   1202 		/* clear the controller output buffer */
   1203 		for (i = 0; i < 4; i++) {
   1204 			out_fdc(iot, ioh, NE7CMD_SENSEI);
   1205 			(void) fdcresult(fdc);
   1206 		}
   1207 
   1208 		/* fall through */
   1209 	case DORECAL:
   1210 		out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
   1211 		out_fdc(iot, ioh, fd->sc_drive);
   1212 		fdc->sc_state = RECALWAIT;
   1213 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
   1214 		return 1;			/* will return later */
   1215 
   1216 	case RECALWAIT:
   1217 		callout_stop(&fdc->sc_timo_ch);
   1218 		fdc->sc_state = RECALCOMPLETE;
   1219 		/* allow 1/30 second for heads to settle */
   1220 		callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
   1221 		return 1;			/* will return later */
   1222 
   1223 	case RECALCOMPLETE:
   1224 		out_fdc(iot, ioh, NE7CMD_SENSEI);
   1225 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1226 #ifdef FD_DEBUG
   1227 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
   1228 #endif
   1229 			fdcretry(fdc);
   1230 			goto loop;
   1231 		}
   1232 		fd->sc_cylin = 0;
   1233 		goto doseek;
   1234 
   1235 	case MOTORWAIT:
   1236 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1237 			return 1;		/* time's not up yet */
   1238 		goto doseek;
   1239 
   1240 	default:
   1241 		fdcstatus(&fd->sc_dev, 0, "stray interrupt");
   1242 		return 1;
   1243 	}
   1244 #ifdef DIAGNOSTIC
   1245 	panic("fdcintr: impossible");
   1246 #endif
   1247 #undef	st0
   1248 #undef	cyl
   1249 }
   1250 
   1251 void
   1252 fdcretry(fdc)
   1253 	struct fdc_softc *fdc;
   1254 {
   1255 	char bits[64];
   1256 	struct fd_softc *fd;
   1257 	struct buf *bp;
   1258 
   1259 	fd = TAILQ_FIRST(&fdc->sc_drives);
   1260 	bp = BUFQ_PEEK(&fd->sc_q);
   1261 
   1262 	if (fd->sc_opts & FDOPT_NORETRY)
   1263 	    goto fail;
   1264 	switch (fdc->sc_errors) {
   1265 	case 0:
   1266 		/* try again */
   1267 		fdc->sc_state = DOSEEK;
   1268 		break;
   1269 
   1270 	case 1: case 2: case 3:
   1271 		/* didn't work; try recalibrating */
   1272 		fdc->sc_state = DORECAL;
   1273 		break;
   1274 
   1275 	case 4:
   1276 		/* still no go; reset the bastard */
   1277 		fdc->sc_state = DORESET;
   1278 		break;
   1279 
   1280 	default:
   1281 	fail:
   1282 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
   1283 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1284 				fd->sc_skip / FDC_BSIZE,
   1285 				(struct disklabel *)NULL);
   1286 
   1287 			printf(" (st0 %s",
   1288 			       bitmask_snprintf(fdc->sc_status[0],
   1289 						NE7_ST0BITS, bits,
   1290 						sizeof(bits)));
   1291 			printf(" st1 %s",
   1292 			       bitmask_snprintf(fdc->sc_status[1],
   1293 						NE7_ST1BITS, bits,
   1294 						sizeof(bits)));
   1295 			printf(" st2 %s",
   1296 			       bitmask_snprintf(fdc->sc_status[2],
   1297 						NE7_ST2BITS, bits,
   1298 						sizeof(bits)));
   1299 			printf(" cyl %d head %d sec %d)\n",
   1300 			       fdc->sc_status[3],
   1301 			       fdc->sc_status[4],
   1302 			       fdc->sc_status[5]);
   1303 		}
   1304 
   1305 		bp->b_flags |= B_ERROR;
   1306 		bp->b_error = EIO;
   1307 		fdfinish(fd, bp);
   1308 	}
   1309 	fdc->sc_errors++;
   1310 }
   1311 
   1312 int
   1313 fdioctl(dev, cmd, addr, flag, p)
   1314 	dev_t dev;
   1315 	u_long cmd;
   1316 	caddr_t addr;
   1317 	int flag;
   1318 	struct proc *p;
   1319 {
   1320 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
   1321 	struct fdformat_parms *form_parms;
   1322 	struct fdformat_cmd *form_cmd;
   1323 	struct ne7_fd_formb *fd_formb;
   1324 	struct disklabel buffer;
   1325 	int error;
   1326 	unsigned int scratch;
   1327 	int il[FD_MAX_NSEC + 1];
   1328 	register int i, j;
   1329 #ifdef __HAVE_OLD_DISKLABEL
   1330 	struct disklabel newlabel;
   1331 #endif
   1332 
   1333 	switch (cmd) {
   1334 	case DIOCGDINFO:
   1335 #ifdef __HAVE_OLD_DISKLABEL
   1336 	case ODIOCGDINFO:
   1337 #endif
   1338 		memset(&buffer, 0, sizeof(buffer));
   1339 
   1340 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1341 		buffer.d_type = DTYPE_FLOPPY;
   1342 		buffer.d_secsize = FDC_BSIZE;
   1343 
   1344 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1345 			return EINVAL;
   1346 
   1347 #ifdef __HAVE_OLD_DISKLABEL
   1348 		if (cmd == ODIOCGDINFO) {
   1349 			if (buffer.d_npartitions > OLDMAXPARTITIONS)
   1350 				return ENOTTY;
   1351 			memcpy(addr, &buffer, sizeof (struct olddisklabel));
   1352 		} else
   1353 #endif
   1354 		*(struct disklabel *)addr = buffer;
   1355 		return 0;
   1356 
   1357 	case DIOCWLABEL:
   1358 		if ((flag & FWRITE) == 0)
   1359 			return EBADF;
   1360 		/* XXX do something */
   1361 		return 0;
   1362 
   1363 	case DIOCWDINFO:
   1364 #ifdef __HAVE_OLD_DISKLABEL
   1365 	case ODIOCWDINFO:
   1366 #endif
   1367 	{
   1368 		struct disklabel *lp;
   1369 
   1370 		if ((flag & FWRITE) == 0)
   1371 			return EBADF;
   1372 #ifdef __HAVE_OLD_DISKLABEL
   1373 		if (cmd == ODIOCWDINFO) {
   1374 			memset(&newlabel, 0, sizeof newlabel);
   1375 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
   1376 			lp = &newlabel;
   1377 		} else
   1378 #endif
   1379 		lp = (struct disklabel *)addr;
   1380 
   1381 		error = setdisklabel(&buffer, lp, 0, NULL);
   1382 		if (error)
   1383 			return error;
   1384 
   1385 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1386 		return error;
   1387 	}
   1388 
   1389 	case FDIOCGETFORMAT:
   1390 		form_parms = (struct fdformat_parms *)addr;
   1391 		form_parms->fdformat_version = FDFORMAT_VERSION;
   1392 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
   1393 		form_parms->ncyl = fd->sc_type->cyls;
   1394 		form_parms->nspt = fd->sc_type->sectrac;
   1395 		form_parms->ntrk = fd->sc_type->heads;
   1396 		form_parms->stepspercyl = fd->sc_type->step;
   1397 		form_parms->gaplen = fd->sc_type->gap2;
   1398 		form_parms->fillbyte = fd->sc_type->fillbyte;
   1399 		form_parms->interleave = fd->sc_type->interleave;
   1400 		switch (fd->sc_type->rate) {
   1401 		case FDC_500KBPS:
   1402 			form_parms->xfer_rate = 500 * 1024;
   1403 			break;
   1404 		case FDC_300KBPS:
   1405 			form_parms->xfer_rate = 300 * 1024;
   1406 			break;
   1407 		case FDC_250KBPS:
   1408 			form_parms->xfer_rate = 250 * 1024;
   1409 			break;
   1410 		default:
   1411 			return EINVAL;
   1412 		}
   1413 		return 0;
   1414 
   1415 	case FDIOCSETFORMAT:
   1416 		if((flag & FWRITE) == 0)
   1417 			return EBADF;	/* must be opened for writing */
   1418 		form_parms = (struct fdformat_parms *)addr;
   1419 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
   1420 			return EINVAL;	/* wrong version of formatting prog */
   1421 
   1422 		scratch = form_parms->nbps >> 7;
   1423 		if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
   1424 		    scratch & ~(1 << (ffs(scratch)-1)))
   1425 			/* not a power-of-two multiple of 128 */
   1426 			return EINVAL;
   1427 
   1428 		switch (form_parms->xfer_rate) {
   1429 		case 500 * 1024:
   1430 			fd->sc_type->rate = FDC_500KBPS;
   1431 			break;
   1432 		case 300 * 1024:
   1433 			fd->sc_type->rate = FDC_300KBPS;
   1434 			break;
   1435 		case 250 * 1024:
   1436 			fd->sc_type->rate = FDC_250KBPS;
   1437 			break;
   1438 		default:
   1439 			return EINVAL;
   1440 		}
   1441 
   1442 		if (form_parms->nspt > FD_MAX_NSEC ||
   1443 		    form_parms->fillbyte > 0xff ||
   1444 		    form_parms->interleave > 0xff)
   1445 			return EINVAL;
   1446 		fd->sc_type->sectrac = form_parms->nspt;
   1447 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
   1448 			return EINVAL;
   1449 		fd->sc_type->heads = form_parms->ntrk;
   1450 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
   1451 		fd->sc_type->secsize = ffs(scratch)-1;
   1452 		fd->sc_type->gap2 = form_parms->gaplen;
   1453 		fd->sc_type->cyls = form_parms->ncyl;
   1454 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
   1455 		    form_parms->nbps / DEV_BSIZE;
   1456 		fd->sc_type->step = form_parms->stepspercyl;
   1457 		fd->sc_type->fillbyte = form_parms->fillbyte;
   1458 		fd->sc_type->interleave = form_parms->interleave;
   1459 		return 0;
   1460 
   1461 	case FDIOCFORMAT_TRACK:
   1462 		if((flag & FWRITE) == 0)
   1463 			return EBADF;	/* must be opened for writing */
   1464 		form_cmd = (struct fdformat_cmd *)addr;
   1465 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
   1466 			return EINVAL;	/* wrong version of formatting prog */
   1467 
   1468 		if (form_cmd->head >= fd->sc_type->heads ||
   1469 		    form_cmd->cylinder >= fd->sc_type->cyls) {
   1470 			return EINVAL;
   1471 		}
   1472 
   1473 		fd_formb = malloc(sizeof(struct ne7_fd_formb),
   1474 		    M_TEMP, M_NOWAIT);
   1475 		if (fd_formb == 0)
   1476 			return ENOMEM;
   1477 
   1478 		fd_formb->head = form_cmd->head;
   1479 		fd_formb->cyl = form_cmd->cylinder;
   1480 		fd_formb->transfer_rate = fd->sc_type->rate;
   1481 		fd_formb->fd_formb_secshift = fd->sc_type->secsize;
   1482 		fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
   1483 		fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
   1484 		fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
   1485 
   1486 		memset(il, 0, sizeof il);
   1487 		for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
   1488 			while (il[(j%fd_formb->fd_formb_nsecs)+1])
   1489 				j++;
   1490 			il[(j%fd_formb->fd_formb_nsecs)+1] = i;
   1491 			j += fd->sc_type->interleave;
   1492 		}
   1493 		for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
   1494 			fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
   1495 			fd_formb->fd_formb_headno(i) = form_cmd->head;
   1496 			fd_formb->fd_formb_secno(i) = il[i+1];
   1497 			fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
   1498 		}
   1499 
   1500 		error = fdformat(dev, fd_formb, p);
   1501 		free(fd_formb, M_TEMP);
   1502 		return error;
   1503 
   1504 	case FDIOCGETOPTS:		/* get drive options */
   1505 		*(int *)addr = fd->sc_opts;
   1506 		return 0;
   1507 
   1508 	case FDIOCSETOPTS:		/* set drive options */
   1509 		fd->sc_opts = *(int *)addr;
   1510 		return 0;
   1511 
   1512 	default:
   1513 		return ENOTTY;
   1514 	}
   1515 
   1516 #ifdef DIAGNOSTIC
   1517 	panic("fdioctl: impossible");
   1518 #endif
   1519 }
   1520 
   1521 int
   1522 fdformat(dev, finfo, p)
   1523 	dev_t dev;
   1524 	struct ne7_fd_formb *finfo;
   1525 	struct proc *p;
   1526 {
   1527 	int rv = 0, s;
   1528 	struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
   1529 	struct fd_type *type = fd->sc_type;
   1530 	struct buf *bp;
   1531 
   1532 	/* set up a buffer header for fdstrategy() */
   1533 	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT|M_ZERO);
   1534 	if(bp == 0)
   1535 		return ENOBUFS;
   1536 	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
   1537 	bp->b_proc = p;
   1538 	bp->b_dev = dev;
   1539 
   1540 	/*
   1541 	 * calculate a fake blkno, so fdstrategy() would initiate a
   1542 	 * seek to the requested cylinder
   1543 	 */
   1544 	bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
   1545 		       + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
   1546 
   1547 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
   1548 	bp->b_data = (caddr_t)finfo;
   1549 
   1550 #ifdef DEBUG
   1551 	printf("fdformat: blkno %x count %lx\n", bp->b_blkno, bp->b_bcount);
   1552 #endif
   1553 
   1554 	/* now do the format */
   1555 	fdstrategy(bp);
   1556 
   1557 	/* ...and wait for it to complete */
   1558 	s = splbio();
   1559 	while (!(bp->b_flags & B_DONE)) {
   1560 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
   1561 		if (rv == EWOULDBLOCK)
   1562 			break;
   1563 	}
   1564 	splx(s);
   1565 
   1566 	if (rv == EWOULDBLOCK) {
   1567 		/* timed out */
   1568 		rv = EIO;
   1569 		biodone(bp);
   1570 	}
   1571 	if(bp->b_flags & B_ERROR) {
   1572 		rv = bp->b_error;
   1573 	}
   1574 	free(bp, M_TEMP);
   1575 	return rv;
   1576 }
   1577 
   1578 /*
   1579  * Mountroot hook: prompt the user to enter the root file system
   1580  * floppy.
   1581  */
   1582 void
   1583 fd_mountroot_hook(dev)
   1584 	struct device *dev;
   1585 {
   1586 	int c;
   1587 
   1588 	printf("Insert filesystem floppy and press return.");
   1589 	cnpollc(1);
   1590 	for (;;) {
   1591 		c = cngetc();
   1592 		if ((c == '\r') || (c == '\n')) {
   1593 			printf("\n");
   1594 			break;
   1595 		}
   1596 	}
   1597 	cnpollc(0);
   1598 }
   1599