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