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