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