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