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