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