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