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