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